]>
Commit | Line | Data |
---|---|---|
b4aa79af | 1 | /* Cycript - Remove Execution Server and Disassembler |
c1582939 JF |
2 | * Copyright (C) 2009 Jay Freeman (saurik) |
3 | */ | |
4 | ||
62ca2b82 | 5 | /* Modified BSD License {{{ */ |
c1582939 JF |
6 | /* |
7 | * Redistribution and use in source and binary | |
8 | * forms, with or without modification, are permitted | |
9 | * provided that the following conditions are met: | |
10 | * | |
11 | * 1. Redistributions of source code must retain the | |
12 | * above copyright notice, this list of conditions | |
13 | * and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the | |
15 | * above copyright notice, this list of conditions | |
16 | * and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the | |
18 | * distribution. | |
19 | * 3. The name of the author may not be used to endorse | |
20 | * or promote products derived from this software | |
21 | * without specific prior written permission. | |
22 | * | |
23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' | |
24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | |
25 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE | |
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
29 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
30 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
33 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | |
34 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
35 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
36 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
37 | */ | |
62ca2b82 | 38 | /* }}} */ |
c1582939 | 39 | |
8d9b5eed JF |
40 | #define _GNU_SOURCE |
41 | ||
c1582939 | 42 | #include <substrate.h> |
30ddc20c | 43 | #include "cycript.hpp" |
c1582939 | 44 | |
ea2d184c JF |
45 | #include "sig/parse.hpp" |
46 | #include "sig/ffi_type.hpp" | |
47 | ||
5999c315 | 48 | #include "Pooling.hpp" |
057f943f | 49 | #include "Struct.hpp" |
ea2d184c | 50 | |
c1582939 JF |
51 | #include <CoreFoundation/CoreFoundation.h> |
52 | #include <CoreFoundation/CFLogUtilities.h> | |
53 | ||
c1582939 JF |
54 | #include <WebKit/WebScriptObject.h> |
55 | ||
b09da87b | 56 | #include <sys/mman.h> |
c1582939 | 57 | |
8d9b5eed JF |
58 | #include <iostream> |
59 | #include <ext/stdio_filebuf.h> | |
a2d9403c JF |
60 | #include <set> |
61 | #include <map> | |
8d9b5eed | 62 | |
967067aa | 63 | #include <sstream> |
bd17e6f3 JF |
64 | #include <cmath> |
65 | ||
e5332278 | 66 | #include "Parser.hpp" |
63b4c5a8 | 67 | #include "Cycript.tab.hh" |
e5332278 | 68 | |
967067aa JF |
69 | #include <apr-1/apr_thread_proc.h> |
70 | ||
ea2d184c JF |
71 | #undef _assert |
72 | #undef _trace | |
73 | ||
c1582939 | 74 | #define _assert(test) do { \ |
f5e9be24 JF |
75 | if (!(test)) \ |
76 | @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"_assert(%s):%s(%u):%s", #test, __FILE__, __LINE__, __FUNCTION__] userInfo:nil]; \ | |
c1582939 JF |
77 | } while (false) |
78 | ||
79 | #define _trace() do { \ | |
62ca2b82 | 80 | CFLog(kCFLogLevelNotice, CFSTR("_trace():%u"), __LINE__); \ |
c1582939 JF |
81 | } while (false) |
82 | ||
4cf49641 JF |
83 | #define CYPoolTry { \ |
84 | id _saved(nil); \ | |
85 | NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \ | |
86 | @try | |
87 | #define CYPoolCatch(value) \ | |
88 | @catch (NSException *error) { \ | |
89 | _saved = [error retain]; \ | |
90 | @throw; \ | |
91 | return value; \ | |
92 | } @finally { \ | |
93 | [_pool release]; \ | |
94 | if (_saved != nil) \ | |
95 | [_saved autorelease]; \ | |
96 | } \ | |
97 | } | |
98 | ||
478d4ed0 | 99 | static JSGlobalContextRef Context_; |
b09da87b | 100 | static JSObjectRef System_; |
c239b9f8 | 101 | static JSObjectRef ObjectiveC_; |
ea2d184c | 102 | |
88c977fa JF |
103 | static JSClassRef Functor_; |
104 | static JSClassRef Instance_; | |
9b5527f0 | 105 | static JSClassRef Internal_; |
dc68b74c | 106 | static JSClassRef Message_; |
88c977fa | 107 | static JSClassRef Pointer_; |
dc68b74c | 108 | static JSClassRef Prototype_; |
953647c1 | 109 | static JSClassRef Runtime_; |
88c977fa | 110 | static JSClassRef Selector_; |
953647c1 | 111 | static JSClassRef Struct_; |
bce8339b | 112 | static JSClassRef Type_; |
ea2d184c | 113 | |
c239b9f8 JF |
114 | static JSClassRef ObjectiveC_Classes_; |
115 | static JSClassRef ObjectiveC_Image_Classes_; | |
116 | static JSClassRef ObjectiveC_Images_; | |
117 | static JSClassRef ObjectiveC_Protocols_; | |
118 | ||
c1582939 | 119 | static JSObjectRef Array_; |
dea834b0 | 120 | static JSObjectRef Function_; |
ea2d184c | 121 | |
967067aa JF |
122 | static JSStringRef Result_; |
123 | ||
c1582939 | 124 | static JSStringRef length_; |
b4aa79af JF |
125 | static JSStringRef message_; |
126 | static JSStringRef name_; | |
dc68b74c | 127 | static JSStringRef prototype_; |
b4aa79af JF |
128 | static JSStringRef toCYON_; |
129 | static JSStringRef toJSON_; | |
ea2d184c | 130 | |
faf69207 JF |
131 | static JSObjectRef Array_prototype_; |
132 | static JSObjectRef Array_pop_; | |
133 | static JSObjectRef Array_push_; | |
134 | static JSObjectRef Array_splice_; | |
135 | ||
136 | static Class NSArray_; | |
c1582939 | 137 | static Class NSCFBoolean_; |
c239b9f8 JF |
138 | static Class NSCFType_; |
139 | static Class NSMessageBuilder_; | |
140 | static Class NSZombie_; | |
141 | static Class Object_; | |
c1582939 | 142 | |
953647c1 | 143 | static NSArray *Bridge_; |
88c977fa | 144 | |
1ef7d061 JF |
145 | static void Finalize(JSObjectRef object) { |
146 | delete reinterpret_cast<CYData *>(JSObjectGetPrivate(object)); | |
147 | } | |
953647c1 | 148 | |
7b184c00 JF |
149 | class Type_privateData; |
150 | ||
61933e16 | 151 | struct CYValue : |
953647c1 | 152 | CYData |
61933e16 JF |
153 | { |
154 | void *value_; | |
155 | ||
156 | CYValue() { | |
157 | } | |
158 | ||
159 | CYValue(void *value) : | |
160 | value_(value) | |
161 | { | |
162 | } | |
9b5527f0 JF |
163 | |
164 | CYValue(const CYValue &rhs) : | |
165 | value_(rhs.value_) | |
166 | { | |
167 | } | |
7b184c00 JF |
168 | |
169 | virtual Type_privateData *GetType() const { | |
170 | return NULL; | |
171 | } | |
61933e16 JF |
172 | }; |
173 | ||
174 | struct Selector_privateData : | |
175 | CYValue | |
953647c1 | 176 | { |
953647c1 | 177 | Selector_privateData(SEL value) : |
61933e16 | 178 | CYValue(value) |
478d4ed0 JF |
179 | { |
180 | } | |
181 | ||
182 | SEL GetValue() const { | |
183 | return reinterpret_cast<SEL>(value_); | |
184 | } | |
7b184c00 JF |
185 | |
186 | virtual Type_privateData *GetType() const; | |
478d4ed0 JF |
187 | }; |
188 | ||
2b52f27e | 189 | struct Instance : |
61933e16 | 190 | CYValue |
bd17e6f3 | 191 | { |
2b52f27e JF |
192 | enum Flags { |
193 | None = 0, | |
194 | Transient = (1 << 0), | |
195 | Uninitialized = (1 << 1), | |
196 | }; | |
478d4ed0 | 197 | |
2b52f27e JF |
198 | Flags flags_; |
199 | ||
200 | Instance(id value, Flags flags) : | |
61933e16 | 201 | CYValue(value), |
2b52f27e | 202 | flags_(flags) |
478d4ed0 JF |
203 | { |
204 | } | |
205 | ||
2b52f27e JF |
206 | virtual ~Instance() { |
207 | if ((flags_ & Transient) == 0) | |
61933e16 JF |
208 | // XXX: does this handle background threads correctly? |
209 | [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0]; | |
478d4ed0 JF |
210 | } |
211 | ||
dc68b74c | 212 | static JSObjectRef Make(JSContextRef context, id object, Flags flags = None) { |
faf69207 JF |
213 | JSObjectRef value(JSObjectMake(context, Instance_, new Instance(object, flags))); |
214 | if (object != nil) | |
215 | for (Class _class(object_getClass(object)); _class != nil; _class = class_getSuperclass(_class)) | |
216 | if (_class == NSArray_) { | |
217 | JSObjectSetPrototype(context, value, Array_prototype_); | |
218 | break; | |
219 | } | |
220 | return value; | |
2b52f27e JF |
221 | } |
222 | ||
478d4ed0 JF |
223 | id GetValue() const { |
224 | return reinterpret_cast<id>(value_); | |
225 | } | |
2b52f27e JF |
226 | |
227 | bool IsUninitialized() const { | |
228 | return (flags_ & Uninitialized) != 0; | |
229 | } | |
7b184c00 JF |
230 | |
231 | virtual Type_privateData *GetType() const; | |
478d4ed0 JF |
232 | }; |
233 | ||
dc68b74c JF |
234 | struct Prototype : |
235 | CYValue | |
236 | { | |
237 | Prototype(Class value) : | |
238 | CYValue(value) | |
239 | { | |
240 | } | |
241 | ||
faf69207 | 242 | static JSObjectRef Make(JSContextRef context, Class _class, bool array = false) { |
9e562cfc | 243 | JSObjectRef value(JSObjectMake(context, Prototype_, new Prototype(_class))); |
faf69207 JF |
244 | if (_class == NSArray_) |
245 | array = true; | |
9e562cfc | 246 | if (Class super = class_getSuperclass(_class)) |
faf69207 JF |
247 | JSObjectSetPrototype(context, value, Prototype::Make(context, super, array)); |
248 | /*else if (array) | |
249 | JSObjectSetPrototype(context, value, Array_prototype_);*/ | |
9e562cfc | 250 | return value; |
dc68b74c JF |
251 | } |
252 | ||
253 | Class GetValue() const { | |
254 | return reinterpret_cast<Class>(value_); | |
255 | } | |
256 | }; | |
257 | ||
9b5527f0 JF |
258 | struct Internal : |
259 | CYValue | |
260 | { | |
261 | JSObjectRef owner_; | |
262 | ||
263 | Internal(id value, JSObjectRef owner) : | |
264 | CYValue(value), | |
265 | owner_(owner) | |
266 | { | |
267 | } | |
268 | ||
269 | static JSObjectRef Make(JSContextRef context, id object, JSObjectRef owner) { | |
270 | return JSObjectMake(context, Internal_, new Internal(object, owner)); | |
271 | } | |
272 | ||
273 | id GetValue() const { | |
274 | return reinterpret_cast<id>(value_); | |
275 | } | |
276 | }; | |
277 | ||
bd17e6f3 JF |
278 | namespace sig { |
279 | ||
280 | void Copy(apr_pool_t *pool, Type &lhs, Type &rhs); | |
281 | ||
282 | void Copy(apr_pool_t *pool, Element &lhs, Element &rhs) { | |
283 | lhs.name = apr_pstrdup(pool, rhs.name); | |
284 | if (rhs.type == NULL) | |
285 | lhs.type = NULL; | |
286 | else { | |
287 | lhs.type = new(pool) Type; | |
288 | Copy(pool, *lhs.type, *rhs.type); | |
289 | } | |
290 | lhs.offset = rhs.offset; | |
291 | } | |
292 | ||
293 | void Copy(apr_pool_t *pool, Signature &lhs, Signature &rhs) { | |
294 | size_t count(rhs.count); | |
295 | lhs.count = count; | |
296 | lhs.elements = new(pool) Element[count]; | |
297 | for (size_t index(0); index != count; ++index) | |
298 | Copy(pool, lhs.elements[index], rhs.elements[index]); | |
299 | } | |
300 | ||
301 | void Copy(apr_pool_t *pool, Type &lhs, Type &rhs) { | |
302 | lhs.primitive = rhs.primitive; | |
303 | lhs.name = apr_pstrdup(pool, rhs.name); | |
304 | lhs.flags = rhs.flags; | |
305 | ||
306 | if (sig::IsAggregate(rhs.primitive)) | |
307 | Copy(pool, lhs.data.signature, rhs.data.signature); | |
308 | else { | |
309 | if (rhs.data.data.type != NULL) { | |
310 | lhs.data.data.type = new(pool) Type; | |
311 | Copy(pool, *lhs.data.data.type, *rhs.data.data.type); | |
312 | } | |
313 | ||
314 | lhs.data.data.size = rhs.data.data.size; | |
315 | } | |
316 | } | |
317 | ||
318 | void Copy(apr_pool_t *pool, ffi_type &lhs, ffi_type &rhs) { | |
319 | lhs.size = rhs.size; | |
320 | lhs.alignment = rhs.alignment; | |
321 | lhs.type = rhs.type; | |
322 | if (rhs.elements == NULL) | |
323 | lhs.elements = NULL; | |
324 | else { | |
325 | size_t count(0); | |
326 | while (rhs.elements[count] != NULL) | |
327 | ++count; | |
328 | ||
329 | lhs.elements = new(pool) ffi_type *[count + 1]; | |
330 | lhs.elements[count] = NULL; | |
331 | ||
332 | for (size_t index(0); index != count; ++index) { | |
333 | // XXX: if these are libffi native then you can just take them | |
334 | ffi_type *ffi(new(pool) ffi_type); | |
335 | lhs.elements[index] = ffi; | |
336 | sig::Copy(pool, *ffi, *rhs.elements[index]); | |
337 | } | |
338 | } | |
339 | } | |
340 | ||
341 | } | |
342 | ||
f33b048a JF |
343 | struct CStringMapLess : |
344 | std::binary_function<const char *, const char *, bool> | |
345 | { | |
346 | _finline bool operator ()(const char *lhs, const char *rhs) const { | |
347 | return strcmp(lhs, rhs) < 0; | |
348 | } | |
349 | }; | |
350 | ||
bce8339b JF |
351 | void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type *&type) { |
352 | if (name == NULL) | |
353 | return; | |
ff783f8c | 354 | |
bce8339b JF |
355 | CYPoolTry { |
356 | if (NSMutableArray *entry = [[Bridge_ objectAtIndex:2] objectForKey:[NSString stringWithUTF8String:name]]) | |
357 | switch ([[entry objectAtIndex:0] intValue]) { | |
358 | case 0: { | |
359 | sig::Parse(pool, &type->data.signature, [[entry objectAtIndex:1] UTF8String], &Structor_); | |
360 | } break; | |
361 | ||
362 | case 1: { | |
363 | sig::Signature signature; | |
364 | sig::Parse(pool, &signature, [[entry objectAtIndex:1] UTF8String], &Structor_); | |
365 | type = signature.elements[0].type; | |
366 | } break; | |
367 | } | |
368 | } CYPoolCatch() | |
369 | } | |
370 | ||
371 | struct Type_privateData : | |
372 | CYData | |
373 | { | |
7b184c00 JF |
374 | static Type_privateData *Object; |
375 | static Type_privateData *Selector; | |
376 | ||
ff783f8c | 377 | ffi_type *ffi_; |
930aa21b | 378 | sig::Type *type_; |
bd17e6f3 | 379 | |
bce8339b JF |
380 | void Set(sig::Type *type) { |
381 | type_ = new(pool_) sig::Type; | |
382 | sig::Copy(pool_, *type_, *type); | |
383 | } | |
384 | ||
c239b9f8 | 385 | Type_privateData(apr_pool_t *pool, const char *type) : |
ff783f8c JF |
386 | ffi_(NULL) |
387 | { | |
c239b9f8 JF |
388 | if (pool != NULL) |
389 | pool_ = pool; | |
390 | ||
bce8339b JF |
391 | sig::Signature signature; |
392 | sig::Parse(pool_, &signature, type, &Structor_); | |
393 | type_ = signature.elements[0].type; | |
ff783f8c JF |
394 | } |
395 | ||
bce8339b JF |
396 | Type_privateData(sig::Type *type) : |
397 | ffi_(NULL) | |
ff783f8c | 398 | { |
bce8339b JF |
399 | if (type != NULL) |
400 | Set(type); | |
401 | } | |
402 | ||
403 | Type_privateData(sig::Type *type, ffi_type *ffi) { | |
404 | ffi_ = new(pool_) ffi_type; | |
405 | sig::Copy(pool_, *ffi_, *ffi); | |
406 | Set(type); | |
ff783f8c JF |
407 | } |
408 | ||
409 | ffi_type *GetFFI() { | |
410 | if (ffi_ == NULL) { | |
411 | ffi_ = new(pool_) ffi_type; | |
412 | ||
413 | sig::Element element; | |
414 | element.name = NULL; | |
930aa21b | 415 | element.type = type_; |
ff783f8c JF |
416 | element.offset = 0; |
417 | ||
418 | sig::Signature signature; | |
419 | signature.elements = &element; | |
420 | signature.count = 1; | |
421 | ||
422 | ffi_cif cif; | |
423 | sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature, &cif); | |
424 | *ffi_ = *cif.rtype; | |
425 | } | |
426 | ||
427 | return ffi_; | |
428 | } | |
429 | }; | |
430 | ||
7b184c00 JF |
431 | Type_privateData *Type_privateData::Object; |
432 | Type_privateData *Type_privateData::Selector; | |
433 | ||
434 | Type_privateData *Instance::GetType() const { | |
435 | return Type_privateData::Object; | |
436 | } | |
437 | ||
438 | Type_privateData *Selector_privateData::GetType() const { | |
439 | return Type_privateData::Selector; | |
440 | } | |
441 | ||
ff783f8c | 442 | struct Pointer : |
61933e16 | 443 | CYValue |
ff783f8c JF |
444 | { |
445 | JSObjectRef owner_; | |
446 | Type_privateData *type_; | |
447 | ||
448 | Pointer(void *value, sig::Type *type, JSObjectRef owner) : | |
61933e16 | 449 | CYValue(value), |
ff783f8c | 450 | owner_(owner), |
bce8339b | 451 | type_(new(pool_) Type_privateData(type)) |
ff783f8c | 452 | { |
bd17e6f3 JF |
453 | } |
454 | }; | |
455 | ||
456 | struct Struct_privateData : | |
61933e16 | 457 | CYValue |
bd17e6f3 JF |
458 | { |
459 | JSObjectRef owner_; | |
460 | Type_privateData *type_; | |
461 | ||
ff783f8c JF |
462 | Struct_privateData(JSObjectRef owner) : |
463 | owner_(owner) | |
464 | { | |
bd17e6f3 JF |
465 | } |
466 | }; | |
467 | ||
bd17e6f3 JF |
468 | typedef std::map<const char *, Type_privateData *, CStringMapLess> TypeMap; |
469 | static TypeMap Types_; | |
470 | ||
471 | JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef owner) { | |
ff783f8c | 472 | Struct_privateData *internal(new Struct_privateData(owner)); |
bd17e6f3 | 473 | apr_pool_t *pool(internal->pool_); |
bce8339b | 474 | Type_privateData *typical(new(pool) Type_privateData(type, ffi)); |
bd17e6f3 JF |
475 | internal->type_ = typical; |
476 | ||
ff783f8c | 477 | if (owner != NULL) |
bd17e6f3 | 478 | internal->value_ = data; |
ff783f8c JF |
479 | else { |
480 | size_t size(typical->GetFFI()->size); | |
bd17e6f3 JF |
481 | void *copy(apr_palloc(internal->pool_, size)); |
482 | memcpy(copy, data, size); | |
483 | internal->value_ = copy; | |
484 | } | |
485 | ||
bd17e6f3 JF |
486 | return JSObjectMake(context, Struct_, internal); |
487 | } | |
488 | ||
f33b048a | 489 | struct Functor_privateData : |
61933e16 | 490 | CYValue |
f33b048a JF |
491 | { |
492 | sig::Signature signature_; | |
493 | ffi_cif cif_; | |
494 | ||
dc68b74c | 495 | |
f33b048a | 496 | Functor_privateData(const char *type, void (*value)()) : |
61933e16 | 497 | CYValue(reinterpret_cast<void *>(value)) |
f33b048a JF |
498 | { |
499 | sig::Parse(pool_, &signature_, type, &Structor_); | |
500 | sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_); | |
501 | } | |
dc68b74c JF |
502 | |
503 | void (*GetValue())() const { | |
504 | return reinterpret_cast<void (*)()>(value_); | |
505 | } | |
f33b048a JF |
506 | }; |
507 | ||
dc68b74c | 508 | struct Closure_privateData : |
f33b048a JF |
509 | Functor_privateData |
510 | { | |
511 | JSContextRef context_; | |
512 | JSObjectRef function_; | |
513 | ||
dc68b74c | 514 | Closure_privateData(const char *type) : |
f33b048a JF |
515 | Functor_privateData(type, NULL) |
516 | { | |
517 | } | |
518 | }; | |
519 | ||
dc68b74c JF |
520 | struct Message_privateData : |
521 | Functor_privateData | |
522 | { | |
523 | SEL sel_; | |
524 | ||
525 | Message_privateData(SEL sel, const char *type, IMP value = NULL) : | |
526 | Functor_privateData(type, reinterpret_cast<void (*)()>(value)), | |
527 | sel_(sel) | |
528 | { | |
529 | } | |
530 | }; | |
531 | ||
f7c38a29 | 532 | JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) { |
2b52f27e JF |
533 | Instance::Flags flags; |
534 | ||
535 | if (transient) | |
536 | flags = Instance::Transient; | |
537 | else { | |
538 | flags = Instance::None; | |
478d4ed0 | 539 | object = [object retain]; |
2b52f27e JF |
540 | } |
541 | ||
542 | return Instance::Make(context, object, flags); | |
478d4ed0 JF |
543 | } |
544 | ||
545 | const char *CYPoolCString(apr_pool_t *pool, NSString *value) { | |
546 | if (pool == NULL) | |
547 | return [value UTF8String]; | |
548 | else { | |
549 | size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1); | |
550 | char *string(new(pool) char[size]); | |
551 | if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding]) | |
4afefdd9 | 552 | @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"[NSString getCString:maxLength:encoding:] == NO" userInfo:nil]; |
478d4ed0 JF |
553 | return string; |
554 | } | |
b09da87b JF |
555 | } |
556 | ||
557 | JSValueRef CYCastJSValue(JSContextRef context, bool value) { | |
558 | return JSValueMakeBoolean(context, value); | |
559 | } | |
560 | ||
561 | JSValueRef CYCastJSValue(JSContextRef context, double value) { | |
562 | return JSValueMakeNumber(context, value); | |
563 | } | |
564 | ||
565 | #define CYCastJSValue_(Type_) \ | |
566 | JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \ | |
567 | return JSValueMakeNumber(context, static_cast<double>(value)); \ | |
568 | } | |
569 | ||
570 | CYCastJSValue_(int) | |
571 | CYCastJSValue_(unsigned int) | |
572 | CYCastJSValue_(long int) | |
573 | CYCastJSValue_(long unsigned int) | |
574 | CYCastJSValue_(long long int) | |
575 | CYCastJSValue_(long long unsigned int) | |
576 | ||
577 | JSValueRef CYJSUndefined(JSContextRef context) { | |
578 | return JSValueMakeUndefined(context); | |
0c862573 JF |
579 | } |
580 | ||
faf69207 JF |
581 | size_t CYGetIndex(const char *value) { |
582 | if (value[0] != '0') { | |
583 | char *end; | |
584 | size_t index(strtoul(value, &end, 10)); | |
585 | if (value + strlen(value) == end) | |
586 | return index; | |
587 | } else if (value[1] == '\0') | |
588 | return 0; | |
589 | return _not(size_t); | |
590 | } | |
591 | ||
592 | size_t CYGetIndex(apr_pool_t *pool, NSString *value) { | |
593 | return CYGetIndex(CYPoolCString(pool, value)); | |
594 | } | |
595 | ||
596 | bool CYGetOffset(const char *value, ssize_t &index) { | |
ff783f8c | 597 | if (value[0] != '0') { |
283e7e33 | 598 | char *end; |
ff783f8c | 599 | index = strtol(value, &end, 10); |
283e7e33 | 600 | if (value + strlen(value) == end) |
ff783f8c JF |
601 | return true; |
602 | } else if (value[1] == '\0') { | |
603 | index = 0; | |
604 | return true; | |
283e7e33 JF |
605 | } |
606 | ||
ff783f8c | 607 | return false; |
283e7e33 JF |
608 | } |
609 | ||
faf69207 JF |
610 | bool CYGetOffset(apr_pool_t *pool, NSString *value, ssize_t &index) { |
611 | return CYGetOffset(CYPoolCString(pool, value), index); | |
283e7e33 JF |
612 | } |
613 | ||
c239b9f8 JF |
614 | NSString *CYPoolNSCYON(apr_pool_t *pool, id value); |
615 | ||
107e3ed0 | 616 | @interface NSMethodSignature (Cycript) |
7ba62cfd JF |
617 | - (NSString *) _typeString; |
618 | @end | |
619 | ||
107e3ed0 | 620 | @interface NSObject (Cycript) |
b4aa79af | 621 | |
61933e16 | 622 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context; |
b4aa79af JF |
623 | - (JSType) cy$JSType; |
624 | ||
625 | - (NSObject *) cy$toJSON:(NSString *)key; | |
626 | - (NSString *) cy$toCYON; | |
f33b048a | 627 | - (NSString *) cy$toKey; |
b4aa79af | 628 | |
7b184c00 | 629 | - (bool) cy$hasProperty:(NSString *)name; |
cc103044 JF |
630 | - (NSObject *) cy$getProperty:(NSString *)name; |
631 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value; | |
632 | - (bool) cy$deleteProperty:(NSString *)name; | |
b4aa79af | 633 | |
c1582939 JF |
634 | @end |
635 | ||
f7c38a29 JF |
636 | @protocol Cycript |
637 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context; | |
638 | @end | |
639 | ||
107e3ed0 | 640 | @interface NSString (Cycript) |
88c977fa JF |
641 | - (void *) cy$symbol; |
642 | @end | |
643 | ||
4afefdd9 JF |
644 | struct PropertyAttributes { |
645 | CYPool pool_; | |
646 | ||
647 | const char *name; | |
648 | ||
649 | const char *variable; | |
650 | ||
651 | const char *getter_; | |
652 | const char *setter_; | |
653 | ||
654 | bool readonly; | |
655 | bool copy; | |
656 | bool retain; | |
657 | bool nonatomic; | |
658 | bool dynamic; | |
659 | bool weak; | |
660 | bool garbage; | |
661 | ||
662 | PropertyAttributes(objc_property_t property) : | |
663 | variable(NULL), | |
664 | getter_(NULL), | |
665 | setter_(NULL), | |
666 | readonly(false), | |
667 | copy(false), | |
668 | retain(false), | |
669 | nonatomic(false), | |
670 | dynamic(false), | |
671 | weak(false), | |
672 | garbage(false) | |
673 | { | |
674 | name = property_getName(property); | |
675 | const char *attributes(property_getAttributes(property)); | |
676 | ||
677 | for (char *state, *token(apr_strtok(apr_pstrdup(pool_, attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) { | |
678 | switch (*token) { | |
679 | case 'R': readonly = true; break; | |
680 | case 'C': copy = true; break; | |
681 | case '&': retain = true; break; | |
682 | case 'N': nonatomic = true; break; | |
683 | case 'G': getter_ = token + 1; break; | |
684 | case 'S': setter_ = token + 1; break; | |
685 | case 'V': variable = token + 1; break; | |
686 | } | |
687 | } | |
688 | ||
689 | /*if (variable == NULL) { | |
690 | variable = property_getName(property); | |
691 | size_t size(strlen(variable)); | |
692 | char *name(new(pool_) char[size + 2]); | |
693 | name[0] = '_'; | |
694 | memcpy(name + 1, variable, size); | |
695 | name[size + 1] = '\0'; | |
696 | variable = name; | |
697 | }*/ | |
698 | } | |
699 | ||
700 | const char *Getter() { | |
701 | if (getter_ == NULL) | |
702 | getter_ = apr_pstrdup(pool_, name); | |
703 | return getter_; | |
704 | } | |
705 | ||
706 | const char *Setter() { | |
707 | if (setter_ == NULL && !readonly) { | |
708 | size_t length(strlen(name)); | |
709 | ||
710 | char *temp(new(pool_) char[length + 5]); | |
711 | temp[0] = 's'; | |
712 | temp[1] = 'e'; | |
713 | temp[2] = 't'; | |
714 | ||
715 | if (length != 0) { | |
716 | temp[3] = toupper(name[0]); | |
717 | memcpy(temp + 4, name + 1, length - 1); | |
718 | } | |
719 | ||
720 | temp[length + 3] = ':'; | |
721 | temp[length + 4] = '\0'; | |
722 | setter_ = temp; | |
723 | } | |
724 | ||
725 | return setter_; | |
726 | } | |
727 | ||
728 | }; | |
729 | ||
c239b9f8 JF |
730 | @implementation NSProxy (Cycript) |
731 | ||
732 | - (NSObject *) cy$toJSON:(NSString *)key { | |
733 | return [self description]; | |
734 | } | |
735 | ||
736 | - (NSString *) cy$toCYON { | |
737 | return [[self cy$toJSON:@""] cy$toCYON]; | |
738 | } | |
739 | ||
740 | @end | |
741 | ||
107e3ed0 | 742 | @implementation NSObject (Cycript) |
62ca2b82 | 743 | |
61933e16 JF |
744 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { |
745 | return CYMakeInstance(context, self, false); | |
746 | } | |
747 | ||
b4aa79af JF |
748 | - (JSType) cy$JSType { |
749 | return kJSTypeObject; | |
6b8a9500 JF |
750 | } |
751 | ||
b4aa79af | 752 | - (NSObject *) cy$toJSON:(NSString *)key { |
62ca2b82 JF |
753 | return [self description]; |
754 | } | |
755 | ||
b4aa79af JF |
756 | - (NSString *) cy$toCYON { |
757 | return [[self cy$toJSON:@""] cy$toCYON]; | |
758 | } | |
759 | ||
f33b048a JF |
760 | - (NSString *) cy$toKey { |
761 | return [self cy$toCYON]; | |
762 | } | |
763 | ||
7b184c00 JF |
764 | - (bool) cy$hasProperty:(NSString *)name { |
765 | return false; | |
766 | } | |
767 | ||
cc103044 | 768 | - (NSObject *) cy$getProperty:(NSString *)name { |
cc103044 JF |
769 | return nil; |
770 | } | |
771 | ||
772 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
cc103044 JF |
773 | return false; |
774 | } | |
775 | ||
776 | - (bool) cy$deleteProperty:(NSString *)name { | |
cc103044 JF |
777 | return false; |
778 | } | |
779 | ||
62ca2b82 | 780 | @end |
c1582939 | 781 | |
c239b9f8 JF |
782 | NSString *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { |
783 | return [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease]; | |
784 | } | |
785 | ||
107e3ed0 | 786 | @implementation WebUndefined (Cycript) |
62ca2b82 | 787 | |
b4aa79af JF |
788 | - (JSType) cy$JSType { |
789 | return kJSTypeUndefined; | |
790 | } | |
791 | ||
792 | - (NSObject *) cy$toJSON:(NSString *)key { | |
793 | return self; | |
6b8a9500 JF |
794 | } |
795 | ||
b4aa79af | 796 | - (NSString *) cy$toCYON { |
c1582939 | 797 | return @"undefined"; |
62ca2b82 JF |
798 | } |
799 | ||
2b52f27e | 800 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { |
b09da87b | 801 | return CYJSUndefined(context); |
62ca2b82 JF |
802 | } |
803 | ||
804 | @end | |
c1582939 | 805 | |
478d4ed0 JF |
806 | @implementation NSNull (Cycript) |
807 | ||
b4aa79af JF |
808 | - (JSType) cy$JSType { |
809 | return kJSTypeNull; | |
810 | } | |
811 | ||
812 | - (NSObject *) cy$toJSON:(NSString *)key { | |
813 | return self; | |
814 | } | |
815 | ||
816 | - (NSString *) cy$toCYON { | |
478d4ed0 JF |
817 | return @"null"; |
818 | } | |
819 | ||
820 | @end | |
821 | ||
107e3ed0 | 822 | @implementation NSArray (Cycript) |
62ca2b82 | 823 | |
b4aa79af | 824 | - (NSString *) cy$toCYON { |
c1582939 JF |
825 | NSMutableString *json([[[NSMutableString alloc] init] autorelease]); |
826 | [json appendString:@"["]; | |
827 | ||
828 | bool comma(false); | |
62ca2b82 | 829 | for (id object in self) { |
c1582939 JF |
830 | if (comma) |
831 | [json appendString:@","]; | |
832 | else | |
833 | comma = true; | |
7b184c00 | 834 | if (object == nil || [object cy$JSType] != kJSTypeUndefined) |
c239b9f8 | 835 | [json appendString:CYPoolNSCYON(NULL, object)]; |
6b8a9500 JF |
836 | else { |
837 | [json appendString:@","]; | |
838 | comma = false; | |
839 | } | |
c1582939 JF |
840 | } |
841 | ||
842 | [json appendString:@"]"]; | |
843 | return json; | |
62ca2b82 JF |
844 | } |
845 | ||
7b184c00 JF |
846 | - (bool) cy$hasProperty:(NSString *)name { |
847 | if ([name isEqualToString:@"length"]) | |
848 | return true; | |
849 | ||
faf69207 JF |
850 | size_t index(CYGetIndex(NULL, name)); |
851 | if (index == _not(size_t) || index >= [self count]) | |
7b184c00 JF |
852 | return [super cy$hasProperty:name]; |
853 | else | |
854 | return true; | |
855 | } | |
856 | ||
cc103044 | 857 | - (NSObject *) cy$getProperty:(NSString *)name { |
4afefdd9 JF |
858 | if ([name isEqualToString:@"length"]) |
859 | return [NSNumber numberWithUnsignedInteger:[self count]]; | |
860 | ||
faf69207 JF |
861 | size_t index(CYGetIndex(NULL, name)); |
862 | if (index == _not(size_t) || index >= [self count]) | |
cc103044 JF |
863 | return [super cy$getProperty:name]; |
864 | else | |
865 | return [self objectAtIndex:index]; | |
866 | } | |
867 | ||
868 | @end | |
869 | ||
870 | @implementation NSMutableArray (Cycript) | |
871 | ||
872 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
faf69207 JF |
873 | if ([name isEqualToString:@"length"]) { |
874 | // XXX: is this not intelligent? | |
875 | NSUInteger size([(NSNumber *)value unsignedIntegerValue]); | |
876 | NSUInteger count([self count]); | |
877 | if (size < count) | |
878 | [self removeObjectsInRange:NSMakeRange(size, count - size)]; | |
879 | else if (size != count) { | |
880 | WebUndefined *undefined([WebUndefined undefined]); | |
881 | for (size_t i(count); i != size; ++i) | |
882 | [self addObject:undefined]; | |
883 | } | |
884 | return true; | |
885 | } | |
886 | ||
887 | size_t index(CYGetIndex(NULL, name)); | |
888 | if (index == _not(size_t)) | |
cc103044 | 889 | return [super cy$setProperty:name to:value]; |
faf69207 JF |
890 | |
891 | id object(value ?: [NSNull null]); | |
892 | ||
893 | size_t count([self count]); | |
894 | if (index < count) | |
895 | [self replaceObjectAtIndex:index withObject:object]; | |
cc103044 | 896 | else { |
faf69207 JF |
897 | if (index != count) { |
898 | WebUndefined *undefined([WebUndefined undefined]); | |
899 | for (size_t i(count); i != index; ++i) | |
900 | [self addObject:undefined]; | |
901 | } | |
902 | ||
903 | [self addObject:object]; | |
cc103044 | 904 | } |
faf69207 JF |
905 | |
906 | return true; | |
cc103044 JF |
907 | } |
908 | ||
909 | - (bool) cy$deleteProperty:(NSString *)name { | |
faf69207 JF |
910 | size_t index(CYGetIndex(NULL, name)); |
911 | if (index == _not(size_t) || index >= [self count]) | |
cc103044 | 912 | return [super cy$deleteProperty:name]; |
faf69207 JF |
913 | [self replaceObjectAtIndex:index withObject:[WebUndefined undefined]]; |
914 | return true; | |
cc103044 JF |
915 | } |
916 | ||
62ca2b82 JF |
917 | @end |
918 | ||
107e3ed0 | 919 | @implementation NSDictionary (Cycript) |
62ca2b82 | 920 | |
b4aa79af | 921 | - (NSString *) cy$toCYON { |
62ca2b82 | 922 | NSMutableString *json([[[NSMutableString alloc] init] autorelease]); |
f33b048a | 923 | [json appendString:@"{"]; |
62ca2b82 JF |
924 | |
925 | bool comma(false); | |
926 | for (id key in self) { | |
927 | if (comma) | |
928 | [json appendString:@","]; | |
929 | else | |
930 | comma = true; | |
f33b048a | 931 | [json appendString:[key cy$toKey]]; |
62ca2b82 JF |
932 | [json appendString:@":"]; |
933 | NSObject *object([self objectForKey:key]); | |
c239b9f8 | 934 | [json appendString:CYPoolNSCYON(NULL, object)]; |
62ca2b82 JF |
935 | } |
936 | ||
f33b048a | 937 | [json appendString:@"}"]; |
62ca2b82 JF |
938 | return json; |
939 | } | |
940 | ||
7b184c00 JF |
941 | - (bool) cy$hasProperty:(NSString *)name { |
942 | return [self objectForKey:name] != nil; | |
943 | } | |
944 | ||
cc103044 JF |
945 | - (NSObject *) cy$getProperty:(NSString *)name { |
946 | return [self objectForKey:name]; | |
947 | } | |
948 | ||
949 | @end | |
950 | ||
951 | @implementation NSMutableDictionary (Cycript) | |
952 | ||
953 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
b6ea08b6 | 954 | [self setObject:(value ?: [NSNull null]) forKey:name]; |
cc103044 JF |
955 | return true; |
956 | } | |
957 | ||
958 | - (bool) cy$deleteProperty:(NSString *)name { | |
959 | if ([self objectForKey:name] == nil) | |
960 | return false; | |
961 | else { | |
962 | [self removeObjectForKey:name]; | |
963 | return true; | |
964 | } | |
965 | } | |
966 | ||
62ca2b82 | 967 | @end |
c1582939 | 968 | |
107e3ed0 | 969 | @implementation NSNumber (Cycript) |
62ca2b82 | 970 | |
b4aa79af JF |
971 | - (JSType) cy$JSType { |
972 | // XXX: this just seems stupid | |
973 | return [self class] == NSCFBoolean_ ? kJSTypeBoolean : kJSTypeNumber; | |
974 | } | |
975 | ||
976 | - (NSObject *) cy$toJSON:(NSString *)key { | |
977 | return self; | |
978 | } | |
979 | ||
980 | - (NSString *) cy$toCYON { | |
981 | return [self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false"; | |
62ca2b82 JF |
982 | } |
983 | ||
2b52f27e | 984 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { |
b4aa79af | 985 | return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]); |
62ca2b82 JF |
986 | } |
987 | ||
988 | @end | |
c1582939 | 989 | |
107e3ed0 | 990 | @implementation NSString (Cycript) |
62ca2b82 | 991 | |
b4aa79af JF |
992 | - (JSType) cy$JSType { |
993 | return kJSTypeString; | |
994 | } | |
995 | ||
996 | - (NSObject *) cy$toJSON:(NSString *)key { | |
997 | return self; | |
998 | } | |
999 | ||
1000 | - (NSString *) cy$toCYON { | |
f33b048a | 1001 | // XXX: this should use the better code from Output.cpp |
c1582939 JF |
1002 | CFMutableStringRef json(CFStringCreateMutableCopy(kCFAllocatorDefault, 0, (CFStringRef) self)); |
1003 | ||
c1582939 JF |
1004 | CFStringFindAndReplace(json, CFSTR("\\"), CFSTR("\\\\"), CFRangeMake(0, CFStringGetLength(json)), 0); |
1005 | CFStringFindAndReplace(json, CFSTR("\""), CFSTR("\\\""), CFRangeMake(0, CFStringGetLength(json)), 0); | |
1006 | CFStringFindAndReplace(json, CFSTR("\t"), CFSTR("\\t"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
1007 | CFStringFindAndReplace(json, CFSTR("\r"), CFSTR("\\r"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
1008 | CFStringFindAndReplace(json, CFSTR("\n"), CFSTR("\\n"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
1009 | ||
1010 | CFStringInsert(json, 0, CFSTR("\"")); | |
1011 | CFStringAppend(json, CFSTR("\"")); | |
1012 | ||
62ca2b82 JF |
1013 | return [reinterpret_cast<const NSString *>(json) autorelease]; |
1014 | } | |
1015 | ||
f33b048a JF |
1016 | - (NSString *) cy$toKey { |
1017 | const char *value([self UTF8String]); | |
1018 | size_t size(strlen(value)); | |
1019 | ||
283e7e33 | 1020 | if (size == 0) |
f33b048a | 1021 | goto cyon; |
283e7e33 JF |
1022 | |
1023 | if (DigitRange_[value[0]]) { | |
faf69207 JF |
1024 | size_t index(CYGetIndex(NULL, self)); |
1025 | if (index == _not(size_t)) | |
f33b048a | 1026 | goto cyon; |
283e7e33 JF |
1027 | } else { |
1028 | if (!WordStartRange_[value[0]]) | |
1029 | goto cyon; | |
1030 | for (size_t i(1); i != size; ++i) | |
1031 | if (!WordEndRange_[value[i]]) | |
1032 | goto cyon; | |
1033 | } | |
1034 | ||
f33b048a JF |
1035 | return self; |
1036 | ||
1037 | cyon: | |
1038 | return [self cy$toCYON]; | |
1039 | } | |
1040 | ||
88c977fa | 1041 | - (void *) cy$symbol { |
478d4ed0 JF |
1042 | CYPool pool; |
1043 | return dlsym(RTLD_DEFAULT, CYPoolCString(pool, self)); | |
88c977fa JF |
1044 | } |
1045 | ||
62ca2b82 JF |
1046 | @end |
1047 | ||
faf69207 | 1048 | @interface CYJSObject : NSMutableDictionary { |
62ca2b82 JF |
1049 | JSObjectRef object_; |
1050 | JSContextRef context_; | |
1051 | } | |
1052 | ||
1053 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; | |
1054 | ||
b4aa79af JF |
1055 | - (NSString *) cy$toJSON:(NSString *)key; |
1056 | ||
62ca2b82 JF |
1057 | - (NSUInteger) count; |
1058 | - (id) objectForKey:(id)key; | |
1059 | - (NSEnumerator *) keyEnumerator; | |
1060 | - (void) setObject:(id)object forKey:(id)key; | |
1061 | - (void) removeObjectForKey:(id)key; | |
1062 | ||
1063 | @end | |
c1582939 | 1064 | |
faf69207 | 1065 | @interface CYJSArray : NSMutableArray { |
c1582939 JF |
1066 | JSObjectRef object_; |
1067 | JSContextRef context_; | |
1068 | } | |
1069 | ||
1070 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; | |
1071 | ||
1072 | - (NSUInteger) count; | |
1073 | - (id) objectAtIndex:(NSUInteger)index; | |
1074 | ||
faf69207 JF |
1075 | - (void) addObject:(id)anObject; |
1076 | - (void) insertObject:(id)anObject atIndex:(NSUInteger)index; | |
1077 | - (void) removeLastObject; | |
1078 | - (void) removeObjectAtIndex:(NSUInteger)index; | |
1079 | - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject; | |
1080 | ||
c1582939 JF |
1081 | @end |
1082 | ||
283e7e33 JF |
1083 | CYRange DigitRange_ (0x3ff000000000000LLU, 0x000000000000000LLU); // 0-9 |
1084 | CYRange WordStartRange_(0x000001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$ | |
1085 | CYRange WordEndRange_ (0x3ff001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$0-9 | |
dea834b0 | 1086 | |
4cf49641 JF |
1087 | #define CYTry \ |
1088 | @try | |
0c862573 JF |
1089 | #define CYCatch \ |
1090 | @catch (id error) { \ | |
1091 | CYThrow(context, error, exception); \ | |
1092 | return NULL; \ | |
1093 | } | |
1094 | ||
ea2d184c JF |
1095 | void CYThrow(JSContextRef context, JSValueRef value); |
1096 | ||
b09da87b JF |
1097 | apr_status_t CYPoolRelease_(void *data) { |
1098 | id object(reinterpret_cast<id>(data)); | |
1099 | [object release]; | |
1100 | return APR_SUCCESS; | |
1101 | } | |
1102 | ||
1103 | id CYPoolRelease(apr_pool_t *pool, id object) { | |
b4aa79af JF |
1104 | if (object == nil) |
1105 | return nil; | |
1106 | else if (pool == NULL) | |
b09da87b JF |
1107 | return [object autorelease]; |
1108 | else { | |
1109 | apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null); | |
1110 | return object; | |
1111 | } | |
1112 | } | |
1113 | ||
953647c1 JF |
1114 | CFTypeRef CYPoolRelease(apr_pool_t *pool, CFTypeRef object) { |
1115 | return (CFTypeRef) CYPoolRelease(pool, (id) object); | |
1116 | } | |
1117 | ||
2b52f27e | 1118 | id CYCastNSObject_(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { |
ea2d184c JF |
1119 | JSValueRef exception(NULL); |
1120 | bool array(JSValueIsInstanceOfConstructor(context, object, Array_, &exception)); | |
1121 | CYThrow(context, exception); | |
b09da87b JF |
1122 | id value(array ? [CYJSArray alloc] : [CYJSObject alloc]); |
1123 | return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]); | |
62ca2b82 JF |
1124 | } |
1125 | ||
2b52f27e JF |
1126 | id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { |
1127 | if (!JSValueIsObjectOfClass(context, object, Instance_)) | |
1128 | return CYCastNSObject_(pool, context, object); | |
1129 | else { | |
9e562cfc JF |
1130 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); |
1131 | return internal->GetValue(); | |
2b52f27e JF |
1132 | } |
1133 | } | |
1134 | ||
77e87a6c | 1135 | JSStringRef CYCopyJSString(id value) { |
b09da87b | 1136 | return value == NULL ? NULL : JSStringCreateWithCFString(reinterpret_cast<CFStringRef>([value description])); |
62ca2b82 JF |
1137 | } |
1138 | ||
77e87a6c | 1139 | JSStringRef CYCopyJSString(const char *value) { |
b09da87b | 1140 | return value == NULL ? NULL : JSStringCreateWithUTF8CString(value); |
77e87a6c JF |
1141 | } |
1142 | ||
1143 | JSStringRef CYCopyJSString(JSStringRef value) { | |
b09da87b | 1144 | return value == NULL ? NULL : JSStringRetain(value); |
77e87a6c JF |
1145 | } |
1146 | ||
1147 | JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value) { | |
b09da87b JF |
1148 | if (JSValueIsNull(context, value)) |
1149 | return NULL; | |
62ca2b82 | 1150 | JSValueRef exception(NULL); |
ea2d184c JF |
1151 | JSStringRef string(JSValueToStringCopy(context, value, &exception)); |
1152 | CYThrow(context, exception); | |
77e87a6c JF |
1153 | return string; |
1154 | } | |
1155 | ||
cf7d4c69 | 1156 | class CYJSString { |
77e87a6c JF |
1157 | private: |
1158 | JSStringRef string_; | |
1159 | ||
b09da87b | 1160 | void Clear_() { |
283e7e33 JF |
1161 | if (string_ != NULL) |
1162 | JSStringRelease(string_); | |
b09da87b JF |
1163 | } |
1164 | ||
77e87a6c | 1165 | public: |
b09da87b JF |
1166 | CYJSString(const CYJSString &rhs) : |
1167 | string_(CYCopyJSString(rhs.string_)) | |
1168 | { | |
1169 | } | |
1170 | ||
77e87a6c | 1171 | template <typename Arg0_> |
b09da87b JF |
1172 | CYJSString(Arg0_ arg0) : |
1173 | string_(CYCopyJSString(arg0)) | |
1174 | { | |
77e87a6c JF |
1175 | } |
1176 | ||
1177 | template <typename Arg0_, typename Arg1_> | |
b09da87b JF |
1178 | CYJSString(Arg0_ arg0, Arg1_ arg1) : |
1179 | string_(CYCopyJSString(arg0, arg1)) | |
1180 | { | |
1181 | } | |
1182 | ||
1183 | CYJSString &operator =(const CYJSString &rhs) { | |
1184 | Clear_(); | |
1185 | string_ = CYCopyJSString(rhs.string_); | |
1186 | return *this; | |
77e87a6c JF |
1187 | } |
1188 | ||
cf7d4c69 | 1189 | ~CYJSString() { |
b09da87b JF |
1190 | Clear_(); |
1191 | } | |
1192 | ||
1193 | void Clear() { | |
1194 | Clear_(); | |
1195 | string_ = NULL; | |
77e87a6c JF |
1196 | } |
1197 | ||
1198 | operator JSStringRef() const { | |
1199 | return string_; | |
1200 | } | |
1201 | }; | |
1202 | ||
1203 | CFStringRef CYCopyCFString(JSStringRef value) { | |
1204 | return JSStringCopyCFString(kCFAllocatorDefault, value); | |
1205 | } | |
1206 | ||
1207 | CFStringRef CYCopyCFString(JSContextRef context, JSValueRef value) { | |
cf7d4c69 | 1208 | return CYCopyCFString(CYJSString(context, value)); |
c1582939 JF |
1209 | } |
1210 | ||
bd17e6f3 JF |
1211 | double CYCastDouble(const char *value, size_t size) { |
1212 | char *end; | |
1213 | double number(strtod(value, &end)); | |
1214 | if (end != value + size) | |
1215 | return NAN; | |
1216 | return number; | |
1217 | } | |
1218 | ||
1219 | double CYCastDouble(const char *value) { | |
1220 | return CYCastDouble(value, strlen(value)); | |
1221 | } | |
1222 | ||
f610e1a0 | 1223 | double CYCastDouble(JSContextRef context, JSValueRef value) { |
0c862573 JF |
1224 | JSValueRef exception(NULL); |
1225 | double number(JSValueToNumber(context, value, &exception)); | |
1226 | CYThrow(context, exception); | |
f610e1a0 JF |
1227 | return number; |
1228 | } | |
1229 | ||
1230 | CFNumberRef CYCopyCFNumber(JSContextRef context, JSValueRef value) { | |
1231 | double number(CYCastDouble(context, value)); | |
0c862573 JF |
1232 | return CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &number); |
1233 | } | |
1234 | ||
953647c1 JF |
1235 | CFStringRef CYCopyCFString(const char *value) { |
1236 | return CFStringCreateWithCString(kCFAllocatorDefault, value, kCFStringEncodingUTF8); | |
1237 | } | |
1238 | ||
1239 | NSString *CYCastNSString(apr_pool_t *pool, const char *value) { | |
1240 | return (NSString *) CYPoolRelease(pool, CYCopyCFString(value)); | |
1241 | } | |
1242 | ||
b09da87b | 1243 | NSString *CYCastNSString(apr_pool_t *pool, JSStringRef value) { |
953647c1 | 1244 | return (NSString *) CYPoolRelease(pool, CYCopyCFString(value)); |
b09da87b JF |
1245 | } |
1246 | ||
1247 | bool CYCastBool(JSContextRef context, JSValueRef value) { | |
1248 | return JSValueToBoolean(context, value); | |
62ca2b82 JF |
1249 | } |
1250 | ||
b09da87b JF |
1251 | CFTypeRef CYCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) { |
1252 | CFTypeRef object; | |
1253 | bool copy; | |
1254 | ||
f610e1a0 | 1255 | switch (JSType type = JSValueGetType(context, value)) { |
c1582939 | 1256 | case kJSTypeUndefined: |
b09da87b JF |
1257 | object = [WebUndefined undefined]; |
1258 | copy = false; | |
1259 | break; | |
1260 | ||
c1582939 | 1261 | case kJSTypeNull: |
b09da87b JF |
1262 | return NULL; |
1263 | break; | |
1264 | ||
c1582939 | 1265 | case kJSTypeBoolean: |
b09da87b JF |
1266 | object = CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse; |
1267 | copy = false; | |
1268 | break; | |
1269 | ||
0c862573 | 1270 | case kJSTypeNumber: |
b09da87b JF |
1271 | object = CYCopyCFNumber(context, value); |
1272 | copy = true; | |
1273 | break; | |
1274 | ||
62ca2b82 | 1275 | case kJSTypeString: |
b09da87b JF |
1276 | object = CYCopyCFString(context, value); |
1277 | copy = true; | |
1278 | break; | |
1279 | ||
c1582939 | 1280 | case kJSTypeObject: |
b09da87b JF |
1281 | // XXX: this might could be more efficient |
1282 | object = (CFTypeRef) CYCastNSObject(pool, context, (JSObjectRef) value); | |
1283 | copy = false; | |
1284 | break; | |
1285 | ||
c1582939 | 1286 | default: |
f5e9be24 | 1287 | @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"JSValueGetType() == 0x%x", type] userInfo:nil]; |
b09da87b | 1288 | break; |
c1582939 | 1289 | } |
b09da87b JF |
1290 | |
1291 | if (cast != copy) | |
1292 | return object; | |
1293 | else if (copy) | |
953647c1 | 1294 | return CYPoolRelease(pool, object); |
b09da87b JF |
1295 | else |
1296 | return CFRetain(object); | |
1297 | } | |
1298 | ||
1299 | CFTypeRef CYCastCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
1300 | return CYCFType(pool, context, value, true); | |
1301 | } | |
1302 | ||
1303 | CFTypeRef CYCopyCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
1304 | return CYCFType(pool, context, value, false); | |
c1582939 JF |
1305 | } |
1306 | ||
62ca2b82 | 1307 | NSArray *CYCastNSArray(JSPropertyNameArrayRef names) { |
b09da87b | 1308 | CYPool pool; |
62ca2b82 JF |
1309 | size_t size(JSPropertyNameArrayGetCount(names)); |
1310 | NSMutableArray *array([NSMutableArray arrayWithCapacity:size]); | |
1311 | for (size_t index(0); index != size; ++index) | |
b09da87b | 1312 | [array addObject:CYCastNSString(pool, JSPropertyNameArrayGetNameAtIndex(names, index))]; |
62ca2b82 JF |
1313 | return array; |
1314 | } | |
1315 | ||
b09da87b JF |
1316 | id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) { |
1317 | return reinterpret_cast<const NSObject *>(CYCastCFType(pool, context, value)); | |
c1582939 JF |
1318 | } |
1319 | ||
ea2d184c | 1320 | void CYThrow(JSContextRef context, JSValueRef value) { |
62ca2b82 JF |
1321 | if (value == NULL) |
1322 | return; | |
b09da87b JF |
1323 | @throw CYCastNSObject(NULL, context, value); |
1324 | } | |
1325 | ||
1326 | JSValueRef CYJSNull(JSContextRef context) { | |
1327 | return JSValueMakeNull(context); | |
1328 | } | |
1329 | ||
1330 | JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value) { | |
1331 | return value == NULL ? CYJSNull(context) : JSValueMakeString(context, value); | |
1332 | } | |
1333 | ||
1334 | JSValueRef CYCastJSValue(JSContextRef context, const char *value) { | |
1335 | return CYCastJSValue(context, CYJSString(value)); | |
62ca2b82 JF |
1336 | } |
1337 | ||
2b52f27e | 1338 | JSValueRef CYCastJSValue(JSContextRef context, id value) { |
f7c38a29 JF |
1339 | if (value == nil) |
1340 | return CYJSNull(context); | |
1341 | else if ([value respondsToSelector:@selector(cy$JSValueInContext:)]) | |
1342 | return [value cy$JSValueInContext:context]; | |
1343 | else | |
1344 | return CYMakeInstance(context, value, false); | |
62ca2b82 JF |
1345 | } |
1346 | ||
dea834b0 JF |
1347 | JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) { |
1348 | JSValueRef exception(NULL); | |
1349 | JSObjectRef object(JSValueToObject(context, value, &exception)); | |
1350 | CYThrow(context, exception); | |
1351 | return object; | |
1352 | } | |
1353 | ||
bd17e6f3 JF |
1354 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index) { |
1355 | JSValueRef exception(NULL); | |
1356 | JSValueRef value(JSObjectGetPropertyAtIndex(context, object, index, &exception)); | |
1357 | CYThrow(context, exception); | |
1358 | return value; | |
1359 | } | |
1360 | ||
dea834b0 JF |
1361 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name) { |
1362 | JSValueRef exception(NULL); | |
1363 | JSValueRef value(JSObjectGetProperty(context, object, name, &exception)); | |
1364 | CYThrow(context, exception); | |
1365 | return value; | |
1366 | } | |
1367 | ||
faf69207 JF |
1368 | void CYSetProperty(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value) { |
1369 | JSValueRef exception(NULL); | |
1370 | JSObjectSetPropertyAtIndex(context, object, index, value, &exception); | |
1371 | CYThrow(context, exception); | |
1372 | } | |
1373 | ||
dea834b0 JF |
1374 | void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value) { |
1375 | JSValueRef exception(NULL); | |
1376 | JSObjectSetProperty(context, object, name, value, kJSPropertyAttributeNone, &exception); | |
1377 | CYThrow(context, exception); | |
1378 | } | |
1379 | ||
30ddc20c | 1380 | void CYThrow(JSContextRef context, id error, JSValueRef *exception) { |
4cf49641 JF |
1381 | if (exception == NULL) |
1382 | throw error; | |
7ba62cfd JF |
1383 | *exception = CYCastJSValue(context, error); |
1384 | } | |
1385 | ||
b4aa79af JF |
1386 | JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, JSValueRef arguments[]) { |
1387 | JSValueRef exception(NULL); | |
1388 | JSValueRef value(JSObjectCallAsFunction(context, function, _this, count, arguments, &exception)); | |
1389 | CYThrow(context, exception); | |
1390 | return value; | |
1391 | } | |
1392 | ||
1393 | bool CYIsCallable(JSContextRef context, JSValueRef value) { | |
1394 | // XXX: this isn't actually correct | |
1395 | return value != NULL && JSValueIsObject(context, value); | |
1396 | } | |
1397 | ||
b21525c7 | 1398 | @implementation CYJSObject |
62ca2b82 JF |
1399 | |
1400 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { | |
1401 | if ((self = [super init]) != nil) { | |
1402 | object_ = object; | |
1403 | context_ = context; | |
1404 | } return self; | |
1405 | } | |
1406 | ||
b4aa79af JF |
1407 | - (NSObject *) cy$toJSON:(NSString *)key { |
1408 | JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_)); | |
1409 | if (!CYIsCallable(context_, toJSON)) | |
1410 | return [super cy$toJSON:key]; | |
1411 | else { | |
1412 | JSValueRef arguments[1] = {CYCastJSValue(context_, key)}; | |
1413 | JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toJSON, object_, 1, arguments)); | |
1414 | // XXX: do I really want an NSNull here?! | |
1415 | return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; | |
1416 | } | |
1417 | } | |
1418 | ||
1419 | - (NSString *) cy$toCYON { | |
1420 | JSValueRef toCYON(CYGetProperty(context_, object_, toCYON_)); | |
1421 | if (!CYIsCallable(context_, toCYON)) | |
1422 | return [super cy$toCYON]; | |
1423 | else { | |
1424 | JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toCYON, object_, 0, NULL)); | |
1425 | return CYCastNSString(NULL, CYJSString(context_, value)); | |
1426 | } | |
1427 | } | |
1428 | ||
62ca2b82 JF |
1429 | - (NSUInteger) count { |
1430 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); | |
1431 | size_t size(JSPropertyNameArrayGetCount(names)); | |
1432 | JSPropertyNameArrayRelease(names); | |
1433 | return size; | |
1434 | } | |
1435 | ||
1436 | - (id) objectForKey:(id)key { | |
478d4ed0 | 1437 | return CYCastNSObject(NULL, context_, CYGetProperty(context_, object_, CYJSString(key))) ?: [NSNull null]; |
62ca2b82 JF |
1438 | } |
1439 | ||
1440 | - (NSEnumerator *) keyEnumerator { | |
1441 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); | |
1442 | NSEnumerator *enumerator([CYCastNSArray(names) objectEnumerator]); | |
1443 | JSPropertyNameArrayRelease(names); | |
1444 | return enumerator; | |
1445 | } | |
1446 | ||
1447 | - (void) setObject:(id)object forKey:(id)key { | |
dea834b0 | 1448 | CYSetProperty(context_, object_, CYJSString(key), CYCastJSValue(context_, object)); |
62ca2b82 JF |
1449 | } |
1450 | ||
1451 | - (void) removeObjectForKey:(id)key { | |
1452 | JSValueRef exception(NULL); | |
2b52f27e | 1453 | (void) JSObjectDeleteProperty(context_, object_, CYJSString(key), &exception); |
62ca2b82 JF |
1454 | CYThrow(context_, exception); |
1455 | } | |
1456 | ||
1457 | @end | |
1458 | ||
b21525c7 | 1459 | @implementation CYJSArray |
c1582939 JF |
1460 | |
1461 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { | |
1462 | if ((self = [super init]) != nil) { | |
1463 | object_ = object; | |
1464 | context_ = context; | |
1465 | } return self; | |
1466 | } | |
1467 | ||
1468 | - (NSUInteger) count { | |
dea834b0 | 1469 | return CYCastDouble(context_, CYGetProperty(context_, object_, length_)); |
c1582939 JF |
1470 | } |
1471 | ||
1472 | - (id) objectAtIndex:(NSUInteger)index { | |
9a2db8b2 JF |
1473 | size_t bounds([self count]); |
1474 | if (index >= bounds) | |
1475 | @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray objectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; | |
62ca2b82 JF |
1476 | JSValueRef exception(NULL); |
1477 | JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception)); | |
1478 | CYThrow(context_, exception); | |
478d4ed0 | 1479 | return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; |
c1582939 JF |
1480 | } |
1481 | ||
faf69207 JF |
1482 | - (void) addObject:(id)object { |
1483 | JSValueRef exception(NULL); | |
1484 | JSValueRef arguments[1]; | |
1485 | arguments[0] = CYCastJSValue(context_, object); | |
1486 | JSObjectCallAsFunction(context_, Array_push_, object_, 1, arguments, &exception); | |
1487 | CYThrow(context_, exception); | |
1488 | } | |
1489 | ||
1490 | - (void) insertObject:(id)object atIndex:(NSUInteger)index { | |
9a2db8b2 JF |
1491 | size_t bounds([self count] + 1); |
1492 | if (index >= bounds) | |
1493 | @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray insertObject:atIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; | |
faf69207 JF |
1494 | JSValueRef exception(NULL); |
1495 | JSValueRef arguments[3]; | |
1496 | arguments[0] = CYCastJSValue(context_, index); | |
1497 | arguments[1] = CYCastJSValue(context_, 0); | |
1498 | arguments[2] = CYCastJSValue(context_, object); | |
1499 | JSObjectCallAsFunction(context_, Array_splice_, object_, 3, arguments, &exception); | |
1500 | CYThrow(context_, exception); | |
1501 | } | |
1502 | ||
1503 | - (void) removeLastObject { | |
1504 | JSValueRef exception(NULL); | |
1505 | JSObjectCallAsFunction(context_, Array_pop_, object_, 0, NULL, &exception); | |
1506 | CYThrow(context_, exception); | |
1507 | } | |
1508 | ||
1509 | - (void) removeObjectAtIndex:(NSUInteger)index { | |
9a2db8b2 JF |
1510 | size_t bounds([self count]); |
1511 | if (index >= bounds) | |
1512 | @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray removeObjectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; | |
faf69207 JF |
1513 | JSValueRef exception(NULL); |
1514 | JSValueRef arguments[2]; | |
1515 | arguments[0] = CYCastJSValue(context_, index); | |
1516 | arguments[1] = CYCastJSValue(context_, 1); | |
1517 | JSObjectCallAsFunction(context_, Array_splice_, object_, 2, arguments, &exception); | |
1518 | CYThrow(context_, exception); | |
1519 | } | |
1520 | ||
1521 | - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)object { | |
9a2db8b2 JF |
1522 | size_t bounds([self count]); |
1523 | if (index >= bounds) | |
1524 | @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray replaceObjectAtIndex:withObject:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil]; | |
faf69207 JF |
1525 | CYSetProperty(context_, object_, index, CYCastJSValue(context_, object)); |
1526 | } | |
1527 | ||
c1582939 JF |
1528 | @end |
1529 | ||
c239b9f8 | 1530 | NSString *CYCopyNSCYON(id value) { |
c239b9f8 JF |
1531 | NSString *string; |
1532 | ||
7b184c00 JF |
1533 | if (value == nil) |
1534 | string = @"nil"; | |
1535 | else { | |
1536 | Class _class(object_getClass(value)); | |
1537 | SEL sel(@selector(cy$toCYON)); | |
1538 | ||
1539 | if (Method toCYON = class_getInstanceMethod(_class, sel)) | |
1540 | string = reinterpret_cast<NSString *(*)(id, SEL)>(method_getImplementation(toCYON))(value, sel); | |
1541 | else if (Method methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) { | |
1542 | if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil) | |
1543 | string = [value cy$toCYON]; | |
1544 | else goto fail; | |
1545 | } else fail: { | |
1546 | if (value == NSZombie_) | |
1547 | string = @"_NSZombie_"; | |
1548 | else if (_class == NSZombie_) | |
1549 | string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value]; | |
1550 | // XXX: frowny /in/ the pants | |
1551 | else if (value == NSMessageBuilder_ || value == Object_) | |
1552 | string = nil; | |
1553 | else | |
1554 | string = [NSString stringWithFormat:@"%@", value]; | |
1555 | } | |
1556 | ||
1557 | // XXX: frowny pants | |
1558 | if (string == nil) | |
1559 | string = @"undefined"; | |
c239b9f8 JF |
1560 | } |
1561 | ||
c239b9f8 JF |
1562 | return [string retain]; |
1563 | } | |
1564 | ||
1565 | NSString *CYCopyNSCYON(JSContextRef context, JSValueRef value, JSValueRef *exception) { | |
e80b023d JF |
1566 | if (JSValueIsNull(context, value)) |
1567 | return [@"null" retain]; | |
1568 | ||
4cf49641 JF |
1569 | CYTry { |
1570 | CYPoolTry { | |
7b184c00 | 1571 | return CYCopyNSCYON(CYCastNSObject(NULL, context, value)); |
4cf49641 JF |
1572 | } CYPoolCatch(NULL) |
1573 | } CYCatch | |
c1582939 JF |
1574 | } |
1575 | ||
c239b9f8 JF |
1576 | NSString *CYPoolNSCYON(apr_pool_t *pool, id value) { |
1577 | return CYPoolRelease(pool, static_cast<id>(CYCopyNSCYON(value))); | |
1578 | } | |
1579 | ||
1580 | const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception) { | |
1581 | if (NSString *json = CYCopyNSCYON(context, value, exception)) { | |
4cf49641 JF |
1582 | const char *string(CYPoolCString(pool, json)); |
1583 | [json release]; | |
1584 | return string; | |
1585 | } else return NULL; | |
478d4ed0 JF |
1586 | } |
1587 | ||
18401062 | 1588 | // XXX: use objc_getAssociatedObject and objc_setAssociatedObject on 10.6 |
61933e16 JF |
1589 | struct CYInternal : |
1590 | CYData | |
1591 | { | |
1592 | JSObjectRef object_; | |
1593 | ||
1594 | CYInternal() : | |
1595 | object_(NULL) | |
1596 | { | |
1597 | } | |
1598 | ||
1599 | ~CYInternal() { | |
1600 | // XXX: delete object_? ;( | |
1601 | } | |
1602 | ||
1603 | static CYInternal *Get(id self) { | |
1604 | CYInternal *internal(NULL); | |
1605 | if (object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)) == NULL) { | |
1606 | // XXX: do something epic? ;P | |
1607 | } | |
1608 | ||
1609 | return internal; | |
1610 | } | |
1611 | ||
1612 | static CYInternal *Set(id self) { | |
1613 | CYInternal *internal(NULL); | |
1614 | if (Ivar ivar = object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal))) { | |
1615 | if (internal == NULL) { | |
1616 | internal = new CYInternal(); | |
1617 | object_setIvar(self, ivar, reinterpret_cast<id>(internal)); | |
1618 | } | |
1619 | } else { | |
1620 | // XXX: do something epic? ;P | |
1621 | } | |
1622 | ||
1623 | return internal; | |
1624 | } | |
1625 | ||
7b184c00 JF |
1626 | bool HasProperty(JSContextRef context, JSStringRef name) { |
1627 | if (object_ == NULL) | |
1628 | return false; | |
1629 | return JSObjectHasProperty(context, object_, name); | |
1630 | } | |
1631 | ||
61933e16 JF |
1632 | JSValueRef GetProperty(JSContextRef context, JSStringRef name) { |
1633 | if (object_ == NULL) | |
1634 | return NULL; | |
1635 | return CYGetProperty(context, object_, name); | |
1636 | } | |
1637 | ||
1638 | void SetProperty(JSContextRef context, JSStringRef name, JSValueRef value) { | |
1639 | if (object_ == NULL) | |
1640 | object_ = JSObjectMake(context, NULL, NULL); | |
1641 | CYSetProperty(context, object_, name, value); | |
1642 | } | |
1643 | }; | |
1644 | ||
dea834b0 | 1645 | JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) { |
9e562cfc JF |
1646 | Selector_privateData *internal(new Selector_privateData(sel)); |
1647 | return JSObjectMake(context, Selector_, internal); | |
dea834b0 JF |
1648 | } |
1649 | ||
9b5527f0 | 1650 | JSObjectRef CYMakePointer(JSContextRef context, void *pointer, sig::Type *type, ffi_type *ffi, JSObjectRef owner) { |
9e562cfc JF |
1651 | Pointer *internal(new Pointer(pointer, type, owner)); |
1652 | return JSObjectMake(context, Pointer_, internal); | |
dea834b0 JF |
1653 | } |
1654 | ||
b09da87b | 1655 | JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) { |
9e562cfc JF |
1656 | Functor_privateData *internal(new Functor_privateData(type, function)); |
1657 | return JSObjectMake(context, Functor_, internal); | |
88c977fa JF |
1658 | } |
1659 | ||
18401062 | 1660 | const char *CYPoolCString(apr_pool_t *pool, JSStringRef value) { |
bd17e6f3 JF |
1661 | if (pool == NULL) { |
1662 | const char *string([CYCastNSString(NULL, value) UTF8String]); | |
bd17e6f3 JF |
1663 | return string; |
1664 | } else { | |
478d4ed0 JF |
1665 | size_t size(JSStringGetMaximumUTF8CStringSize(value)); |
1666 | char *string(new(pool) char[size]); | |
1667 | JSStringGetUTF8CString(value, string, size); | |
1668 | return string; | |
1669 | } | |
7ba62cfd JF |
1670 | } |
1671 | ||
18401062 JF |
1672 | const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) { |
1673 | return JSValueIsNull(context, value) ? NULL : CYPoolCString(pool, CYJSString(context, value)); | |
77e87a6c JF |
1674 | } |
1675 | ||
faf69207 JF |
1676 | bool CYGetOffset(apr_pool_t *pool, JSStringRef value, ssize_t &index) { |
1677 | return CYGetOffset(CYPoolCString(pool, value), index); | |
ff783f8c JF |
1678 | } |
1679 | ||
f610e1a0 | 1680 | // XXX: this macro is unhygenic |
b21525c7 | 1681 | #define CYCastCString(context, value) ({ \ |
b09da87b JF |
1682 | char *utf8; \ |
1683 | if (value == NULL) \ | |
1684 | utf8 = NULL; \ | |
478d4ed0 | 1685 | else if (JSStringRef string = CYCopyJSString(context, value)) { \ |
b09da87b JF |
1686 | size_t size(JSStringGetMaximumUTF8CStringSize(string)); \ |
1687 | utf8 = reinterpret_cast<char *>(alloca(size)); \ | |
1688 | JSStringGetUTF8CString(string, utf8, size); \ | |
1689 | JSStringRelease(string); \ | |
478d4ed0 JF |
1690 | } else \ |
1691 | utf8 = NULL; \ | |
b21525c7 JF |
1692 | utf8; \ |
1693 | }) | |
1694 | ||
b09da87b | 1695 | void *CYCastPointer_(JSContextRef context, JSValueRef value) { |
b21525c7 JF |
1696 | switch (JSValueGetType(context, value)) { |
1697 | case kJSTypeNull: | |
1698 | return NULL; | |
953647c1 | 1699 | /*case kJSTypeString: |
b21525c7 | 1700 | return dlsym(RTLD_DEFAULT, CYCastCString(context, value)); |
b21525c7 | 1701 | case kJSTypeObject: |
88c977fa | 1702 | if (JSValueIsObjectOfClass(context, value, Pointer_)) { |
9e562cfc JF |
1703 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value))); |
1704 | return internal->value_; | |
953647c1 | 1705 | }*/ |
b21525c7 | 1706 | default: |
953647c1 | 1707 | double number(CYCastDouble(context, value)); |
bd17e6f3 | 1708 | if (std::isnan(number)) |
953647c1 JF |
1709 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"cannot convert value to pointer" userInfo:nil]; |
1710 | return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number))); | |
7ba62cfd JF |
1711 | } |
1712 | } | |
1713 | ||
b09da87b JF |
1714 | template <typename Type_> |
1715 | _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) { | |
1716 | return reinterpret_cast<Type_>(CYCastPointer_(context, value)); | |
1717 | } | |
1718 | ||
4afefdd9 JF |
1719 | SEL CYCastSEL(JSContextRef context, JSValueRef value) { |
1720 | if (JSValueIsObjectOfClass(context, value, Selector_)) { | |
9e562cfc JF |
1721 | Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value))); |
1722 | return reinterpret_cast<SEL>(internal->value_); | |
4afefdd9 JF |
1723 | } else |
1724 | return CYCastPointer<SEL>(context, value); | |
1725 | } | |
1726 | ||
bd17e6f3 | 1727 | void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { |
ea2d184c JF |
1728 | switch (type->primitive) { |
1729 | case sig::boolean_P: | |
1730 | *reinterpret_cast<bool *>(data) = JSValueToBoolean(context, value); | |
1731 | break; | |
1732 | ||
43cb3d68 | 1733 | #define CYPoolFFI_(primitive, native) \ |
f610e1a0 JF |
1734 | case sig::primitive ## _P: \ |
1735 | *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \ | |
1736 | break; | |
ea2d184c | 1737 | |
43cb3d68 JF |
1738 | CYPoolFFI_(uchar, unsigned char) |
1739 | CYPoolFFI_(char, char) | |
1740 | CYPoolFFI_(ushort, unsigned short) | |
1741 | CYPoolFFI_(short, short) | |
1742 | CYPoolFFI_(ulong, unsigned long) | |
1743 | CYPoolFFI_(long, long) | |
1744 | CYPoolFFI_(uint, unsigned int) | |
1745 | CYPoolFFI_(int, int) | |
1746 | CYPoolFFI_(ulonglong, unsigned long long) | |
1747 | CYPoolFFI_(longlong, long long) | |
1748 | CYPoolFFI_(float, float) | |
1749 | CYPoolFFI_(double, double) | |
ea2d184c JF |
1750 | |
1751 | case sig::object_P: | |
1752 | case sig::typename_P: | |
b09da87b | 1753 | *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value); |
7ba62cfd JF |
1754 | break; |
1755 | ||
ea2d184c | 1756 | case sig::selector_P: |
7ba62cfd JF |
1757 | *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value); |
1758 | break; | |
ea2d184c | 1759 | |
b21525c7 | 1760 | case sig::pointer_P: |
b09da87b | 1761 | *reinterpret_cast<void **>(data) = CYCastPointer<void *>(context, value); |
b21525c7 | 1762 | break; |
ea2d184c | 1763 | |
77e87a6c | 1764 | case sig::string_P: |
478d4ed0 | 1765 | *reinterpret_cast<const char **>(data) = CYPoolCString(pool, context, value); |
77e87a6c | 1766 | break; |
7ba62cfd | 1767 | |
bd17e6f3 JF |
1768 | case sig::struct_P: { |
1769 | uint8_t *base(reinterpret_cast<uint8_t *>(data)); | |
f33b048a | 1770 | JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL); |
bd17e6f3 | 1771 | for (size_t index(0); index != type->data.signature.count; ++index) { |
f33b048a JF |
1772 | sig::Element *element(&type->data.signature.elements[index]); |
1773 | ffi_type *field(ffi->elements[index]); | |
1774 | ||
1775 | JSValueRef rhs; | |
1776 | if (aggregate == NULL) | |
1777 | rhs = value; | |
1778 | else { | |
1779 | rhs = CYGetProperty(context, aggregate, index); | |
1780 | if (JSValueIsUndefined(context, rhs)) { | |
283e7e33 JF |
1781 | if (element->name != NULL) |
1782 | rhs = CYGetProperty(context, aggregate, CYJSString(element->name)); | |
1783 | else | |
1784 | goto undefined; | |
1785 | if (JSValueIsUndefined(context, rhs)) undefined: | |
f33b048a JF |
1786 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"unable to extract structure value" userInfo:nil]; |
1787 | } | |
1788 | } | |
1789 | ||
1790 | CYPoolFFI(pool, context, element->type, field, base, rhs); | |
4e8c99fb | 1791 | // XXX: alignment? |
f33b048a | 1792 | base += field->size; |
bd17e6f3 JF |
1793 | } |
1794 | } break; | |
ea2d184c JF |
1795 | |
1796 | case sig::void_P: | |
1797 | break; | |
1798 | ||
bd17e6f3 | 1799 | default: |
43cb3d68 | 1800 | NSLog(@"CYPoolFFI(%c)\n", type->primitive); |
ea2d184c JF |
1801 | _assert(false); |
1802 | } | |
1803 | } | |
1804 | ||
c239b9f8 | 1805 | JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL) { |
ea2d184c JF |
1806 | JSValueRef value; |
1807 | ||
1808 | switch (type->primitive) { | |
1809 | case sig::boolean_P: | |
b09da87b | 1810 | value = CYCastJSValue(context, *reinterpret_cast<bool *>(data)); |
ea2d184c JF |
1811 | break; |
1812 | ||
1813 | #define CYFromFFI_(primitive, native) \ | |
1814 | case sig::primitive ## _P: \ | |
b09da87b | 1815 | value = CYCastJSValue(context, *reinterpret_cast<native *>(data)); \ |
ea2d184c JF |
1816 | break; |
1817 | ||
1818 | CYFromFFI_(uchar, unsigned char) | |
1819 | CYFromFFI_(char, char) | |
1820 | CYFromFFI_(ushort, unsigned short) | |
1821 | CYFromFFI_(short, short) | |
1822 | CYFromFFI_(ulong, unsigned long) | |
1823 | CYFromFFI_(long, long) | |
1824 | CYFromFFI_(uint, unsigned int) | |
1825 | CYFromFFI_(int, int) | |
1826 | CYFromFFI_(ulonglong, unsigned long long) | |
1827 | CYFromFFI_(longlong, long long) | |
1828 | CYFromFFI_(float, float) | |
1829 | CYFromFFI_(double, double) | |
1830 | ||
2b52f27e JF |
1831 | case sig::object_P: { |
1832 | if (id object = *reinterpret_cast<id *>(data)) { | |
1833 | value = CYCastJSValue(context, object); | |
1834 | if (initialize) | |
1835 | [object release]; | |
1836 | } else goto null; | |
1837 | } break; | |
dea834b0 | 1838 | |
b09da87b | 1839 | case sig::typename_P: |
4cf49641 | 1840 | value = CYMakeInstance(context, *reinterpret_cast<Class *>(data), true); |
b09da87b JF |
1841 | break; |
1842 | ||
dea834b0 JF |
1843 | case sig::selector_P: |
1844 | if (SEL sel = *reinterpret_cast<SEL *>(data)) | |
1845 | value = CYMakeSelector(context, sel); | |
1846 | else goto null; | |
1847 | break; | |
1848 | ||
1849 | case sig::pointer_P: | |
1850 | if (void *pointer = *reinterpret_cast<void **>(data)) | |
9b5527f0 | 1851 | value = CYMakePointer(context, pointer, type->data.data.type, ffi, owner); |
dea834b0 JF |
1852 | else goto null; |
1853 | break; | |
1854 | ||
1855 | case sig::string_P: | |
f610e1a0 | 1856 | if (char *utf8 = *reinterpret_cast<char **>(data)) |
b09da87b | 1857 | value = CYCastJSValue(context, utf8); |
f610e1a0 | 1858 | else goto null; |
dea834b0 | 1859 | break; |
ea2d184c JF |
1860 | |
1861 | case sig::struct_P: | |
bd17e6f3 JF |
1862 | value = CYMakeStruct(context, data, type, ffi, owner); |
1863 | break; | |
ea2d184c JF |
1864 | |
1865 | case sig::void_P: | |
b09da87b | 1866 | value = CYJSUndefined(context); |
f610e1a0 JF |
1867 | break; |
1868 | ||
1869 | null: | |
b09da87b | 1870 | value = CYJSNull(context); |
ea2d184c JF |
1871 | break; |
1872 | ||
bd17e6f3 | 1873 | default: |
ea2d184c JF |
1874 | NSLog(@"CYFromFFI(%c)\n", type->primitive); |
1875 | _assert(false); | |
1876 | } | |
1877 | ||
1878 | return value; | |
1879 | } | |
1880 | ||
8a199b13 JF |
1881 | static bool CYImplements(id object, Class _class, SEL selector, bool devoid) { |
1882 | if (Method method = class_getInstanceMethod(_class, selector)) { | |
1883 | if (!devoid) | |
1884 | return true; | |
1885 | char type[16]; | |
1886 | method_getReturnType(method, type, sizeof(type)); | |
1887 | if (type[0] != 'v') | |
1888 | return true; | |
1889 | } | |
1890 | ||
7b184c00 | 1891 | // XXX: possibly use a more "awesome" check? |
8a199b13 | 1892 | return false; |
7b184c00 JF |
1893 | } |
1894 | ||
dc68b74c JF |
1895 | const char *CYPoolTypeEncoding(apr_pool_t *pool, Class _class, SEL sel, Method method) { |
1896 | if (method != NULL) | |
1897 | return method_getTypeEncoding(method); | |
1898 | else if (NSString *type = [[Bridge_ objectAtIndex:1] objectForKey:CYCastNSString(pool, sel_getName(sel))]) | |
1899 | return CYPoolCString(pool, type); | |
1900 | else | |
1901 | return NULL; | |
1902 | } | |
1903 | ||
1904 | void FunctionClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) { | |
9e562cfc | 1905 | Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg)); |
dc68b74c | 1906 | |
9e562cfc | 1907 | JSContextRef context(internal->context_); |
dc68b74c | 1908 | |
9e562cfc | 1909 | size_t count(internal->cif_.nargs); |
dc68b74c JF |
1910 | JSValueRef values[count]; |
1911 | ||
1912 | for (size_t index(0); index != count; ++index) | |
9e562cfc | 1913 | values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]); |
dc68b74c | 1914 | |
9e562cfc JF |
1915 | JSValueRef value(CYCallAsFunction(context, internal->function_, NULL, count, values)); |
1916 | CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value); | |
dc68b74c JF |
1917 | } |
1918 | ||
1919 | void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) { | |
9e562cfc | 1920 | Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg)); |
dc68b74c | 1921 | |
9e562cfc | 1922 | JSContextRef context(internal->context_); |
dc68b74c | 1923 | |
9e562cfc | 1924 | size_t count(internal->cif_.nargs); |
dc68b74c JF |
1925 | JSValueRef values[count]; |
1926 | ||
1927 | for (size_t index(0); index != count; ++index) | |
9e562cfc | 1928 | values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]); |
dc68b74c JF |
1929 | |
1930 | JSObjectRef _this(CYCastJSObject(context, values[0])); | |
1931 | ||
9e562cfc JF |
1932 | JSValueRef value(CYCallAsFunction(context, internal->function_, _this, count - 2, values + 2)); |
1933 | CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value); | |
dc68b74c JF |
1934 | } |
1935 | ||
1936 | Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const char *type, void (*callback)(ffi_cif *, void *, void **, void *)) { | |
1937 | // XXX: in case of exceptions this will leak | |
1938 | // XXX: in point of fact, this may /need/ to leak :( | |
1939 | Closure_privateData *internal(new Closure_privateData(type)); | |
1940 | ||
1941 | ffi_closure *closure((ffi_closure *) _syscall(mmap( | |
1942 | NULL, sizeof(ffi_closure), | |
1943 | PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, | |
1944 | -1, 0 | |
1945 | ))); | |
1946 | ||
1947 | ffi_status status(ffi_prep_closure(closure, &internal->cif_, callback, internal)); | |
1948 | _assert(status == FFI_OK); | |
1949 | ||
1950 | _syscall(mprotect(closure, sizeof(*closure), PROT_READ | PROT_EXEC)); | |
1951 | ||
1952 | internal->value_ = closure; | |
1953 | ||
1954 | internal->context_ = CYGetJSContext(); | |
1955 | internal->function_ = function; | |
1956 | ||
1957 | return internal; | |
1958 | } | |
1959 | ||
1960 | JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) { | |
1961 | Closure_privateData *internal(CYMakeFunctor_(context, function, type, &FunctionClosure_)); | |
1962 | return JSObjectMake(context, Functor_, internal); | |
1963 | } | |
1964 | ||
1965 | static JSObjectRef CYMakeFunctor(JSContextRef context, JSValueRef value, const char *type) { | |
1966 | JSValueRef exception(NULL); | |
1967 | bool function(JSValueIsInstanceOfConstructor(context, value, Function_, &exception)); | |
1968 | CYThrow(context, exception); | |
1969 | ||
1970 | if (function) { | |
1971 | JSObjectRef function(CYCastJSObject(context, value)); | |
1972 | return CYMakeFunctor(context, function, type); | |
1973 | } else { | |
1974 | void (*function)()(CYCastPointer<void (*)()>(context, value)); | |
1975 | return CYMakeFunctor(context, function, type); | |
1976 | } | |
1977 | } | |
1978 | ||
1979 | static JSObjectRef CYMakeMessage(JSContextRef context, SEL sel, IMP imp, const char *type) { | |
1980 | Message_privateData *internal(new Message_privateData(sel, type, imp)); | |
1981 | return JSObjectMake(context, Message_, internal); | |
1982 | } | |
1983 | ||
1984 | static IMP CYMakeMessage(JSContextRef context, JSValueRef value, const char *type) { | |
1985 | JSObjectRef function(CYCastJSObject(context, value)); | |
1986 | Closure_privateData *internal(CYMakeFunctor_(context, function, type, &MessageClosure_)); | |
1987 | return reinterpret_cast<IMP>(internal->GetValue()); | |
1988 | } | |
1989 | ||
1990 | static bool Prototype_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { | |
1991 | Prototype *internal(reinterpret_cast<Prototype *>(JSObjectGetPrivate(object))); | |
1992 | Class _class(internal->GetValue()); | |
1993 | ||
1994 | CYPool pool; | |
1995 | const char *name(CYPoolCString(pool, property)); | |
1996 | ||
1997 | if (SEL sel = sel_getUid(name)) | |
1998 | if (class_getInstanceMethod(_class, sel) != NULL) | |
1999 | return true; | |
2000 | ||
2001 | return false; | |
2002 | } | |
2003 | ||
2004 | static JSValueRef Prototype_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2005 | Prototype *internal(reinterpret_cast<Prototype *>(JSObjectGetPrivate(object))); | |
2006 | Class _class(internal->GetValue()); | |
2007 | ||
2008 | CYPool pool; | |
2009 | const char *name(CYPoolCString(pool, property)); | |
2010 | ||
2011 | if (SEL sel = sel_getUid(name)) | |
2012 | if (Method method = class_getInstanceMethod(_class, sel)) | |
2013 | return CYMakeMessage(context, sel, method_getImplementation(method), method_getTypeEncoding(method)); | |
2014 | ||
2015 | return NULL; | |
2016 | } | |
2017 | ||
2018 | static bool Prototype_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
2019 | Prototype *internal(reinterpret_cast<Prototype *>(JSObjectGetPrivate(object))); | |
2020 | Class _class(internal->GetValue()); | |
2021 | ||
2022 | CYPool pool; | |
2023 | const char *name(CYPoolCString(pool, property)); | |
2024 | ||
2025 | SEL sel(sel_registerName(name)); | |
2026 | ||
2027 | Method method(class_getInstanceMethod(_class, sel)); | |
2028 | ||
2029 | const char *type; | |
2030 | IMP imp; | |
2031 | ||
2032 | if (JSValueIsObjectOfClass(context, value, Message_)) { | |
2033 | Message_privateData *message(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate((JSObjectRef) value))); | |
2034 | type = sig::Unparse(pool, &message->signature_); | |
2035 | imp = reinterpret_cast<IMP>(message->GetValue()); | |
2036 | } else { | |
2037 | type = CYPoolTypeEncoding(pool, _class, sel, method); | |
2038 | imp = CYMakeMessage(context, value, type); | |
2039 | } | |
2040 | ||
2041 | if (method != NULL) | |
2042 | method_setImplementation(method, imp); | |
2043 | else | |
2044 | class_replaceMethod(_class, sel, imp, type); | |
2045 | ||
2046 | return true; | |
2047 | } | |
2048 | ||
2049 | #if !__OBJC2__ | |
2050 | static bool Prototype_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2051 | Prototype *internal(reinterpret_cast<Prototype *>(JSObjectGetPrivate(object))); | |
2052 | Class _class(internal->GetValue()); | |
2053 | ||
2054 | CYPool pool; | |
2055 | const char *name(CYPoolCString(pool, property)); | |
2056 | ||
2057 | if (SEL sel = sel_getUid(name)) | |
2058 | if (Method method = class_getInstanceMethod(_class, sel)) { | |
2059 | objc_method_list list = {NULL, 1, {method}}; | |
2060 | class_removeMethods(_class, &list); | |
2061 | return true; | |
2062 | } | |
2063 | ||
2064 | return false; | |
2065 | } | |
2066 | #endif | |
2067 | ||
2068 | static void Prototype_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
2069 | Prototype *internal(reinterpret_cast<Prototype *>(JSObjectGetPrivate(object))); | |
2070 | Class _class(internal->GetValue()); | |
2071 | ||
2072 | unsigned int size; | |
2073 | Method *data(class_copyMethodList(_class, &size)); | |
2074 | for (size_t i(0); i != size; ++i) | |
2075 | JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(data[i])))); | |
2076 | free(data); | |
2077 | } | |
2078 | ||
7b184c00 JF |
2079 | static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { |
2080 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
2081 | id self(internal->GetValue()); | |
2082 | ||
2083 | if (JSStringIsEqualToUTF8CString(property, "$cyi")) | |
2084 | return true; | |
2085 | ||
c239b9f8 | 2086 | CYPool pool; |
7b184c00 JF |
2087 | NSString *name(CYCastNSString(pool, property)); |
2088 | ||
2089 | if (CYInternal *internal = CYInternal::Get(self)) | |
2090 | if (internal->HasProperty(context, property)) | |
2091 | return true; | |
2092 | ||
2093 | CYPoolTry { | |
2094 | if ([self cy$hasProperty:name]) | |
2095 | return true; | |
2096 | } CYPoolCatch(false) | |
2097 | ||
2098 | const char *string(CYPoolCString(pool, name)); | |
2099 | Class _class(object_getClass(self)); | |
2100 | ||
2101 | if (class_getProperty(_class, string) != NULL) | |
2102 | return true; | |
2103 | ||
2104 | if (SEL sel = sel_getUid(string)) | |
8a199b13 | 2105 | if (CYImplements(self, _class, sel, true)) |
7b184c00 JF |
2106 | return true; |
2107 | ||
2108 | return false; | |
2109 | } | |
2110 | ||
2111 | static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2112 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
2113 | id self(internal->GetValue()); | |
2114 | ||
2115 | if (JSStringIsEqualToUTF8CString(property, "$cyi")) | |
2116 | return Internal::Make(context, self, object); | |
c239b9f8 JF |
2117 | |
2118 | CYTry { | |
7b184c00 | 2119 | CYPool pool; |
c239b9f8 JF |
2120 | NSString *name(CYCastNSString(pool, property)); |
2121 | ||
2122 | if (CYInternal *internal = CYInternal::Get(self)) | |
2123 | if (JSValueRef value = internal->GetProperty(context, property)) | |
2124 | return value; | |
2125 | ||
2126 | CYPoolTry { | |
2127 | if (NSObject *data = [self cy$getProperty:name]) | |
2128 | return CYCastJSValue(context, data); | |
2129 | } CYPoolCatch(NULL) | |
2130 | ||
2131 | const char *string(CYPoolCString(pool, name)); | |
8953777c | 2132 | Class _class(object_getClass(self)); |
c239b9f8 | 2133 | |
8953777c | 2134 | if (objc_property_t property = class_getProperty(_class, string)) { |
c239b9f8 JF |
2135 | PropertyAttributes attributes(property); |
2136 | SEL sel(sel_registerName(attributes.Getter())); | |
2137 | return CYSendMessage(pool, context, self, sel, 0, NULL, false, exception); | |
2138 | } | |
2139 | ||
8953777c | 2140 | if (SEL sel = sel_getUid(string)) |
8a199b13 | 2141 | if (CYImplements(self, _class, sel, true)) |
8953777c JF |
2142 | return CYSendMessage(pool, context, self, sel, 0, NULL, false, exception); |
2143 | ||
c239b9f8 JF |
2144 | return NULL; |
2145 | } CYCatch | |
2146 | } | |
2147 | ||
2148 | static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
dc68b74c JF |
2149 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); |
2150 | id self(internal->GetValue()); | |
2151 | ||
c239b9f8 JF |
2152 | CYPool pool; |
2153 | ||
2154 | CYTry { | |
c239b9f8 JF |
2155 | NSString *name(CYCastNSString(pool, property)); |
2156 | NSString *data(CYCastNSObject(pool, context, value)); | |
2157 | ||
2158 | CYPoolTry { | |
2159 | if ([self cy$setProperty:name to:data]) | |
2160 | return true; | |
2161 | } CYPoolCatch(NULL) | |
2162 | ||
2163 | const char *string(CYPoolCString(pool, name)); | |
8953777c | 2164 | Class _class(object_getClass(self)); |
c239b9f8 | 2165 | |
8953777c | 2166 | if (objc_property_t property = class_getProperty(_class, string)) { |
c239b9f8 JF |
2167 | PropertyAttributes attributes(property); |
2168 | if (const char *setter = attributes.Setter()) { | |
2169 | SEL sel(sel_registerName(setter)); | |
2170 | JSValueRef arguments[1] = {value}; | |
2171 | CYSendMessage(pool, context, self, sel, 1, arguments, false, exception); | |
2172 | return true; | |
2173 | } | |
2174 | } | |
2175 | ||
8953777c JF |
2176 | size_t length(strlen(string)); |
2177 | ||
2178 | char set[length + 5]; | |
2179 | ||
2180 | set[0] = 's'; | |
2181 | set[1] = 'e'; | |
2182 | set[2] = 't'; | |
2183 | ||
2184 | if (string[0] != '\0') { | |
2185 | set[3] = toupper(string[0]); | |
2186 | memcpy(set + 4, string + 1, length - 1); | |
2187 | } | |
2188 | ||
2189 | set[length + 3] = ':'; | |
2190 | set[length + 4] = '\0'; | |
2191 | ||
2192 | if (SEL sel = sel_getUid(set)) | |
8a199b13 | 2193 | if (CYImplements(self, _class, sel, false)) { |
8953777c JF |
2194 | JSValueRef arguments[1] = {value}; |
2195 | CYSendMessage(pool, context, self, sel, 1, arguments, false, exception); | |
2196 | } | |
2197 | ||
c239b9f8 JF |
2198 | if (CYInternal *internal = CYInternal::Set(self)) { |
2199 | internal->SetProperty(context, property, value); | |
2200 | return true; | |
2201 | } | |
2202 | ||
2203 | return false; | |
2204 | } CYCatch | |
2205 | } | |
2206 | ||
2207 | static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
dc68b74c JF |
2208 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); |
2209 | id self(internal->GetValue()); | |
2210 | ||
c239b9f8 JF |
2211 | CYTry { |
2212 | CYPoolTry { | |
c239b9f8 JF |
2213 | NSString *name(CYCastNSString(NULL, property)); |
2214 | return [self cy$deleteProperty:name]; | |
2215 | } CYPoolCatch(NULL) | |
2216 | } CYCatch | |
2217 | } | |
2218 | ||
2219 | static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
dc68b74c JF |
2220 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); |
2221 | id self(internal->GetValue()); | |
2222 | ||
c239b9f8 | 2223 | CYPool pool; |
c239b9f8 JF |
2224 | Class _class(object_getClass(self)); |
2225 | ||
2226 | { | |
2227 | unsigned int size; | |
2228 | objc_property_t *data(class_copyPropertyList(_class, &size)); | |
2229 | for (size_t i(0); i != size; ++i) | |
2230 | JSPropertyNameAccumulatorAddName(names, CYJSString(property_getName(data[i]))); | |
2231 | free(data); | |
2232 | } | |
9b5527f0 JF |
2233 | } |
2234 | ||
2235 | static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2236 | CYTry { | |
9e562cfc JF |
2237 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); |
2238 | JSObjectRef value(Instance::Make(context, [internal->GetValue() alloc], Instance::Uninitialized)); | |
9b5527f0 JF |
2239 | return value; |
2240 | } CYCatch | |
2241 | } | |
2242 | ||
7b184c00 JF |
2243 | static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { |
2244 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
2245 | CYPool pool; | |
2246 | ||
2247 | id self(internal->GetValue()); | |
2248 | const char *name(CYPoolCString(pool, property)); | |
2249 | ||
2250 | if (object_getInstanceVariable(self, name, NULL) != NULL) | |
2251 | return true; | |
2252 | ||
2253 | return false; | |
2254 | } | |
2255 | ||
9b5527f0 JF |
2256 | static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
2257 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
2258 | CYPool pool; | |
2259 | ||
2260 | CYTry { | |
2261 | id self(internal->GetValue()); | |
2262 | const char *name(CYPoolCString(pool, property)); | |
2263 | ||
2264 | if (Ivar ivar = object_getInstanceVariable(self, name, NULL)) { | |
2265 | Type_privateData type(pool, ivar_getTypeEncoding(ivar)); | |
2266 | return CYFromFFI(context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar)); | |
2267 | } | |
2268 | ||
2269 | return NULL; | |
2270 | } CYCatch | |
2271 | } | |
2272 | ||
2273 | static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
2274 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
2275 | CYPool pool; | |
2276 | ||
2277 | CYTry { | |
2278 | id self(internal->GetValue()); | |
2279 | const char *name(CYPoolCString(pool, property)); | |
2280 | ||
2281 | if (Ivar ivar = object_getInstanceVariable(self, name, NULL)) { | |
2282 | Type_privateData type(pool, ivar_getTypeEncoding(ivar)); | |
2283 | CYPoolFFI(pool, context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar), value); | |
2284 | return true; | |
2285 | } | |
2286 | ||
2287 | return false; | |
2288 | } CYCatch | |
2289 | } | |
2290 | ||
9e562cfc JF |
2291 | static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) { |
2292 | if (Class super = class_getSuperclass(_class)) | |
2293 | Internal_getPropertyNames_(super, names); | |
2294 | ||
2295 | unsigned int size; | |
2296 | Ivar *data(class_copyIvarList(_class, &size)); | |
2297 | for (size_t i(0); i != size; ++i) | |
2298 | JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i]))); | |
2299 | free(data); | |
2300 | } | |
2301 | ||
9b5527f0 JF |
2302 | static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { |
2303 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
2304 | CYPool pool; | |
2305 | ||
2306 | id self(internal->GetValue()); | |
2307 | Class _class(object_getClass(self)); | |
c239b9f8 | 2308 | |
9e562cfc | 2309 | Internal_getPropertyNames_(_class, names); |
c239b9f8 JF |
2310 | } |
2311 | ||
9b5527f0 JF |
2312 | static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
2313 | Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object))); | |
2314 | return internal->owner_; | |
c239b9f8 JF |
2315 | } |
2316 | ||
9e20b0b7 | 2317 | bool Index_(apr_pool_t *pool, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) { |
bd17e6f3 | 2318 | Type_privateData *typical(internal->type_); |
930aa21b JF |
2319 | sig::Type *type(typical->type_); |
2320 | if (type == NULL) | |
2321 | return false; | |
bd17e6f3 | 2322 | |
18401062 JF |
2323 | const char *name(CYPoolCString(pool, property)); |
2324 | size_t length(strlen(name)); | |
9e20b0b7 JF |
2325 | double number(CYCastDouble(name, length)); |
2326 | ||
930aa21b | 2327 | size_t count(type->data.signature.count); |
f33b048a | 2328 | |
e0dc20ec JF |
2329 | if (std::isnan(number)) { |
2330 | if (property == NULL) | |
2331 | return false; | |
9e20b0b7 | 2332 | |
930aa21b | 2333 | sig::Element *elements(type->data.signature.elements); |
f33b048a | 2334 | |
283e7e33 JF |
2335 | for (size_t local(0); local != count; ++local) { |
2336 | sig::Element *element(&elements[local]); | |
2337 | if (element->name != NULL && strcmp(name, element->name) == 0) { | |
f33b048a JF |
2338 | index = local; |
2339 | goto base; | |
2340 | } | |
283e7e33 | 2341 | } |
f33b048a | 2342 | |
9e20b0b7 | 2343 | return false; |
e0dc20ec JF |
2344 | } else { |
2345 | index = static_cast<ssize_t>(number); | |
f33b048a | 2346 | if (index != number || index < 0 || static_cast<size_t>(index) >= count) |
e0dc20ec JF |
2347 | return false; |
2348 | } | |
bd17e6f3 | 2349 | |
f33b048a | 2350 | base: |
ff783f8c JF |
2351 | ffi_type **elements(typical->GetFFI()->elements); |
2352 | ||
bd17e6f3 JF |
2353 | base = reinterpret_cast<uint8_t *>(internal->value_); |
2354 | for (ssize_t local(0); local != index; ++local) | |
ff783f8c | 2355 | base += elements[local]->size; |
9e20b0b7 JF |
2356 | |
2357 | return true; | |
bd17e6f3 JF |
2358 | } |
2359 | ||
9b5527f0 | 2360 | static JSValueRef Pointer_getIndex(JSContextRef context, JSObjectRef object, size_t index, JSValueRef *exception) { |
ff783f8c JF |
2361 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); |
2362 | Type_privateData *typical(internal->type_); | |
2363 | ||
ff783f8c JF |
2364 | ffi_type *ffi(typical->GetFFI()); |
2365 | ||
2366 | uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_)); | |
2367 | base += ffi->size * index; | |
2368 | ||
2369 | JSObjectRef owner(internal->owner_ ?: object); | |
2370 | ||
2371 | CYTry { | |
930aa21b | 2372 | return CYFromFFI(context, typical->type_, ffi, base, false, owner); |
ff783f8c JF |
2373 | } CYCatch |
2374 | } | |
2375 | ||
9b5527f0 | 2376 | static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
f37b3d2b JF |
2377 | CYPool pool; |
2378 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
2379 | Type_privateData *typical(internal->type_); | |
2380 | ||
930aa21b JF |
2381 | if (typical->type_ == NULL) |
2382 | return NULL; | |
2383 | ||
faf69207 JF |
2384 | ssize_t offset; |
2385 | if (!CYGetOffset(pool, property, offset)) | |
f37b3d2b JF |
2386 | return NULL; |
2387 | ||
faf69207 | 2388 | return Pointer_getIndex(context, object, offset, exception); |
9b5527f0 JF |
2389 | } |
2390 | ||
2391 | static JSValueRef Pointer_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2392 | return Pointer_getIndex(context, object, 0, exception); | |
2393 | } | |
2394 | ||
2395 | static bool Pointer_setIndex(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value, JSValueRef *exception) { | |
2396 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
2397 | Type_privateData *typical(internal->type_); | |
2398 | ||
f37b3d2b JF |
2399 | ffi_type *ffi(typical->GetFFI()); |
2400 | ||
2401 | uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_)); | |
2402 | base += ffi->size * index; | |
2403 | ||
2404 | CYTry { | |
930aa21b | 2405 | CYPoolFFI(NULL, context, typical->type_, ffi, base, value); |
f37b3d2b JF |
2406 | return true; |
2407 | } CYCatch | |
2408 | } | |
2409 | ||
9b5527f0 JF |
2410 | static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { |
2411 | CYPool pool; | |
2412 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
2413 | Type_privateData *typical(internal->type_); | |
2414 | ||
2415 | if (typical->type_ == NULL) | |
2416 | return NULL; | |
2417 | ||
faf69207 JF |
2418 | ssize_t offset; |
2419 | if (!CYGetOffset(pool, property, offset)) | |
9b5527f0 JF |
2420 | return NULL; |
2421 | ||
faf69207 | 2422 | return Pointer_setIndex(context, object, offset, value, exception); |
9b5527f0 JF |
2423 | } |
2424 | ||
2425 | static bool Pointer_setProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
2426 | return Pointer_setIndex(context, object, 0, value, exception); | |
2427 | } | |
2428 | ||
2429 | static JSValueRef Struct_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2430 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(_this))); | |
2431 | Type_privateData *typical(internal->type_); | |
2432 | return CYMakePointer(context, internal->value_, typical->type_, typical->ffi_, _this); | |
2433 | } | |
2434 | ||
bd17e6f3 | 2435 | static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
f33b048a JF |
2436 | CYPool pool; |
2437 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
2438 | Type_privateData *typical(internal->type_); | |
bd17e6f3 | 2439 | |
f33b048a JF |
2440 | ssize_t index; |
2441 | uint8_t *base; | |
bd17e6f3 | 2442 | |
f33b048a JF |
2443 | if (!Index_(pool, internal, property, index, base)) |
2444 | return NULL; | |
bd17e6f3 | 2445 | |
ff783f8c JF |
2446 | JSObjectRef owner(internal->owner_ ?: object); |
2447 | ||
f33b048a | 2448 | CYTry { |
930aa21b | 2449 | return CYFromFFI(context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, false, owner); |
bd17e6f3 JF |
2450 | } CYCatch |
2451 | } | |
2452 | ||
2453 | static bool Struct_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
f33b048a JF |
2454 | CYPool pool; |
2455 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
2456 | Type_privateData *typical(internal->type_); | |
bd17e6f3 | 2457 | |
f33b048a JF |
2458 | ssize_t index; |
2459 | uint8_t *base; | |
bd17e6f3 | 2460 | |
f33b048a JF |
2461 | if (!Index_(pool, internal, property, index, base)) |
2462 | return false; | |
bd17e6f3 | 2463 | |
f33b048a | 2464 | CYTry { |
930aa21b | 2465 | CYPoolFFI(NULL, context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, value); |
bd17e6f3 JF |
2466 | return true; |
2467 | } CYCatch | |
2468 | } | |
2469 | ||
f33b048a JF |
2470 | static void Struct_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { |
2471 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
2472 | Type_privateData *typical(internal->type_); | |
930aa21b JF |
2473 | sig::Type *type(typical->type_); |
2474 | ||
2475 | if (type == NULL) | |
2476 | return; | |
f33b048a | 2477 | |
930aa21b JF |
2478 | size_t count(type->data.signature.count); |
2479 | sig::Element *elements(type->data.signature.elements); | |
f33b048a | 2480 | |
283e7e33 JF |
2481 | char number[32]; |
2482 | ||
2483 | for (size_t index(0); index != count; ++index) { | |
2484 | const char *name; | |
2485 | name = elements[index].name; | |
2486 | ||
2487 | if (name == NULL) { | |
2488 | sprintf(number, "%lu", index); | |
2489 | name = number; | |
2490 | } | |
2491 | ||
2492 | JSPropertyNameAccumulatorAddName(names, CYJSString(name)); | |
2493 | } | |
f33b048a JF |
2494 | } |
2495 | ||
2b52f27e | 2496 | JSValueRef CYCallFunction(apr_pool_t *pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) { |
4cf49641 | 2497 | CYTry { |
4afefdd9 | 2498 | if (setups + count != signature->count - 1) |
f5e9be24 | 2499 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to ffi function" userInfo:nil]; |
85a33bf5 | 2500 | |
4afefdd9 JF |
2501 | size_t size(setups + count); |
2502 | void *values[size]; | |
2503 | memcpy(values, setup, sizeof(void *) * setups); | |
ea2d184c | 2504 | |
4afefdd9 | 2505 | for (size_t index(setups); index != size; ++index) { |
7ba62cfd | 2506 | sig::Element *element(&signature->elements[index + 1]); |
bd17e6f3 | 2507 | ffi_type *ffi(cif->arg_types[index]); |
77e87a6c | 2508 | // XXX: alignment? |
bd17e6f3 | 2509 | values[index] = new(pool) uint8_t[ffi->size]; |
4afefdd9 | 2510 | CYPoolFFI(pool, context, element->type, ffi, values[index], arguments[index - setups]); |
7ba62cfd | 2511 | } |
ea2d184c | 2512 | |
7ba62cfd JF |
2513 | uint8_t value[cif->rtype->size]; |
2514 | ffi_call(cif, function, value, values); | |
2515 | ||
2b52f27e | 2516 | return CYFromFFI(context, signature->elements[0].type, cif->rtype, value, initialize); |
0c862573 | 2517 | } CYCatch |
7ba62cfd | 2518 | } |
ea2d184c | 2519 | |
c239b9f8 JF |
2520 | static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
2521 | CYTry { | |
2522 | CYPool pool; | |
2523 | NSString *name(CYCastNSString(pool, property)); | |
2524 | if (Class _class = NSClassFromString(name)) | |
2525 | return CYMakeInstance(context, _class, true); | |
2526 | return NULL; | |
2527 | } CYCatch | |
2528 | } | |
2529 | ||
2530 | static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
2531 | size_t size(objc_getClassList(NULL, 0)); | |
2532 | Class *data(reinterpret_cast<Class *>(malloc(sizeof(Class) * size))); | |
2533 | ||
2534 | get: | |
2535 | size_t writ(objc_getClassList(data, size)); | |
2536 | if (size < writ) { | |
2537 | size = writ; | |
2538 | if (Class *copy = reinterpret_cast<Class *>(realloc(data, sizeof(Class) * writ))) { | |
2539 | data = copy; | |
2540 | goto get; | |
2541 | } else goto done; | |
2542 | } | |
2543 | ||
2544 | for (size_t i(0); i != writ; ++i) | |
2545 | JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(data[i]))); | |
2546 | ||
2547 | done: | |
2548 | free(data); | |
2549 | } | |
2550 | ||
2551 | static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2552 | const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object))); | |
2553 | ||
2554 | CYTry { | |
2555 | CYPool pool; | |
2556 | const char *name(CYPoolCString(pool, property)); | |
2557 | unsigned int size; | |
2558 | const char **data(objc_copyClassNamesForImage(internal, &size)); | |
2559 | JSValueRef value; | |
2560 | for (size_t i(0); i != size; ++i) | |
2561 | if (strcmp(name, data[i]) == 0) { | |
2562 | if (Class _class = objc_getClass(name)) { | |
2563 | value = CYMakeInstance(context, _class, true); | |
2564 | goto free; | |
2565 | } else | |
2566 | break; | |
2567 | } | |
2568 | value = NULL; | |
2569 | free: | |
2570 | free(data); | |
2571 | return value; | |
2572 | } CYCatch | |
2573 | } | |
2574 | ||
2575 | static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
2576 | const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object))); | |
2577 | unsigned int size; | |
2578 | const char **data(objc_copyClassNamesForImage(internal, &size)); | |
2579 | for (size_t i(0); i != size; ++i) | |
2580 | JSPropertyNameAccumulatorAddName(names, CYJSString(data[i])); | |
2581 | free(data); | |
2582 | } | |
2583 | ||
2584 | static JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2585 | CYTry { | |
2586 | CYPool pool; | |
2587 | const char *name(CYPoolCString(pool, property)); | |
2588 | unsigned int size; | |
2589 | const char **data(objc_copyImageNames(&size)); | |
2590 | for (size_t i(0); i != size; ++i) | |
2591 | if (strcmp(name, data[i]) == 0) { | |
2592 | name = data[i]; | |
2593 | goto free; | |
2594 | } | |
2595 | name = NULL; | |
2596 | free: | |
2597 | free(data); | |
2598 | if (name == NULL) | |
2599 | return NULL; | |
2600 | JSObjectRef value(JSObjectMake(context, NULL, NULL)); | |
2601 | CYSetProperty(context, value, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Image_Classes_, const_cast<char *>(name))); | |
2602 | return value; | |
2603 | } CYCatch | |
2604 | } | |
2605 | ||
2606 | static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
2607 | unsigned int size; | |
2608 | const char **data(objc_copyImageNames(&size)); | |
2609 | for (size_t i(0); i != size; ++i) | |
2610 | JSPropertyNameAccumulatorAddName(names, CYJSString(data[i])); | |
2611 | free(data); | |
2612 | } | |
2613 | ||
2614 | static JSValueRef ObjectiveC_Protocols_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2615 | CYTry { | |
2616 | CYPool pool; | |
2617 | NSString *name(CYCastNSString(pool, property)); | |
2618 | if (Protocol *protocol = NSProtocolFromString(name)) | |
2619 | return CYMakeInstance(context, protocol, true); | |
2620 | return NULL; | |
2621 | } CYCatch | |
2622 | } | |
2623 | ||
2624 | static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
2625 | unsigned int size; | |
2626 | Protocol **data(objc_copyProtocolList(&size)); | |
2627 | for (size_t i(0); i != size; ++i) | |
2628 | JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i]))); | |
2629 | free(data); | |
2630 | } | |
2631 | ||
953647c1 | 2632 | static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
7b184c00 | 2633 | if (JSStringIsEqualToUTF8CString(property, "nil")) |
dc68b74c | 2634 | return Instance::Make(context, nil); |
7b184c00 | 2635 | |
4cf49641 | 2636 | CYTry { |
b09da87b JF |
2637 | CYPool pool; |
2638 | NSString *name(CYCastNSString(pool, property)); | |
f610e1a0 | 2639 | if (Class _class = NSClassFromString(name)) |
4cf49641 | 2640 | return CYMakeInstance(context, _class, true); |
953647c1 | 2641 | if (NSMutableArray *entry = [[Bridge_ objectAtIndex:0] objectForKey:name]) |
707bcb93 JF |
2642 | switch ([[entry objectAtIndex:0] intValue]) { |
2643 | case 0: | |
057f943f | 2644 | return JSEvaluateScript(CYGetJSContext(), CYJSString([entry objectAtIndex:1]), NULL, NULL, 0, NULL); |
707bcb93 | 2645 | case 1: |
478d4ed0 | 2646 | return CYMakeFunctor(context, reinterpret_cast<void (*)()>([name cy$symbol]), CYPoolCString(pool, [entry objectAtIndex:1])); |
707bcb93 | 2647 | case 2: |
bd17e6f3 | 2648 | // XXX: this is horrendously inefficient |
707bcb93 | 2649 | sig::Signature signature; |
f33b048a | 2650 | sig::Parse(pool, &signature, CYPoolCString(pool, [entry objectAtIndex:1]), &Structor_); |
bd17e6f3 JF |
2651 | ffi_cif cif; |
2652 | sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); | |
c239b9f8 | 2653 | return CYFromFFI(context, signature.elements[0].type, cif.rtype, [name cy$symbol]); |
707bcb93 JF |
2654 | } |
2655 | return NULL; | |
2656 | } CYCatch | |
2657 | } | |
2658 | ||
04450da0 JF |
2659 | bool stret(ffi_type *ffi_type) { |
2660 | return ffi_type->type == FFI_TYPE_STRUCT && ( | |
2661 | ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE || | |
2662 | struct_forward_array[ffi_type->size] != 0 | |
2663 | ); | |
2664 | } | |
2665 | ||
b09da87b JF |
2666 | extern "C" { |
2667 | int *_NSGetArgc(void); | |
2668 | char ***_NSGetArgv(void); | |
2669 | int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName); | |
2670 | } | |
2671 | ||
2672 | static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
4cf49641 | 2673 | CYTry { |
b09da87b JF |
2674 | NSLog(@"%s", CYCastCString(context, arguments[0])); |
2675 | return CYJSUndefined(context); | |
2676 | } CYCatch | |
2677 | } | |
2678 | ||
2b52f27e | 2679 | JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { |
7ba62cfd | 2680 | const char *type; |
ea2d184c | 2681 | |
4afefdd9 JF |
2682 | Class _class(object_getClass(self)); |
2683 | if (Method method = class_getInstanceMethod(_class, _cmd)) | |
2684 | type = method_getTypeEncoding(method); | |
2685 | else { | |
bce8339b JF |
2686 | CYTry { |
2687 | CYPoolTry { | |
2688 | NSMethodSignature *method([self methodSignatureForSelector:_cmd]); | |
2689 | if (method == nil) | |
2690 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self] userInfo:nil]; | |
2691 | type = CYPoolCString(pool, [method _typeString]); | |
2692 | } CYPoolCatch(NULL) | |
2693 | } CYCatch | |
4afefdd9 JF |
2694 | } |
2695 | ||
2696 | void *setup[2]; | |
2697 | setup[0] = &self; | |
2698 | setup[1] = &_cmd; | |
2699 | ||
2700 | sig::Signature signature; | |
f33b048a | 2701 | sig::Parse(pool, &signature, type, &Structor_); |
4afefdd9 JF |
2702 | |
2703 | ffi_cif cif; | |
2704 | sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); | |
2705 | ||
2706 | void (*function)() = stret(cif.rtype) ? reinterpret_cast<void (*)()>(&objc_msgSend_stret) : reinterpret_cast<void (*)()>(&objc_msgSend); | |
2b52f27e | 2707 | return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function); |
4afefdd9 JF |
2708 | } |
2709 | ||
2710 | static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
478d4ed0 JF |
2711 | CYPool pool; |
2712 | ||
2b52f27e JF |
2713 | bool uninitialized; |
2714 | ||
4afefdd9 JF |
2715 | id self; |
2716 | SEL _cmd; | |
2717 | ||
4cf49641 | 2718 | CYTry { |
85a33bf5 | 2719 | if (count < 2) |
f5e9be24 | 2720 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"too few arguments to objc_msgSend" userInfo:nil]; |
85a33bf5 | 2721 | |
2b52f27e | 2722 | if (JSValueIsObjectOfClass(context, arguments[0], Instance_)) { |
9e562cfc JF |
2723 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0]))); |
2724 | self = internal->GetValue(); | |
2725 | uninitialized = internal->IsUninitialized(); | |
2b52f27e | 2726 | if (uninitialized) |
9e562cfc | 2727 | internal->value_ = nil; |
2b52f27e JF |
2728 | } else { |
2729 | self = CYCastNSObject(pool, context, arguments[0]); | |
2730 | uninitialized = false; | |
2731 | } | |
2732 | ||
7ba62cfd | 2733 | if (self == nil) |
b09da87b | 2734 | return CYJSNull(context); |
7ba62cfd | 2735 | |
4afefdd9 | 2736 | _cmd = CYCastSEL(context, arguments[1]); |
0c862573 | 2737 | } CYCatch |
ea2d184c | 2738 | |
2b52f27e | 2739 | return CYSendMessage(pool, context, self, _cmd, count - 2, arguments + 2, uninitialized, exception); |
7ba62cfd JF |
2740 | } |
2741 | ||
272c3da3 | 2742 | MSHook(void, CYDealloc, id self, SEL sel) { |
61933e16 JF |
2743 | CYInternal *internal; |
2744 | object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)); | |
2745 | if (internal != NULL) | |
2746 | delete internal; | |
272c3da3 | 2747 | _CYDealloc(self, sel); |
61933e16 | 2748 | } |
f7c38a29 | 2749 | |
61933e16 JF |
2750 | MSHook(void, objc_registerClassPair, Class _class) { |
2751 | Class super(class_getSuperclass(_class)); | |
2752 | if (super == NULL || class_getInstanceVariable(super, "cy$internal_") == NULL) { | |
2753 | class_addIvar(_class, "cy$internal_", sizeof(CYInternal *), log2(sizeof(CYInternal *)), "^{CYInternal}"); | |
272c3da3 | 2754 | MSHookMessage(_class, @selector(dealloc), MSHake(CYDealloc)); |
f7c38a29 JF |
2755 | } |
2756 | ||
61933e16 | 2757 | _objc_registerClassPair(_class); |
f7c38a29 JF |
2758 | } |
2759 | ||
61933e16 | 2760 | static JSValueRef objc_registerClassPair_(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
f7c38a29 | 2761 | CYTry { |
3a1b79a7 JF |
2762 | if (count != 1) |
2763 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to objc_registerClassPair" userInfo:nil]; | |
f7c38a29 | 2764 | CYPool pool; |
3a1b79a7 | 2765 | Class _class(CYCastNSObject(pool, context, arguments[0])); |
61933e16 | 2766 | $objc_registerClassPair(_class); |
f7c38a29 JF |
2767 | return CYJSUndefined(context); |
2768 | } CYCatch | |
2769 | } | |
2770 | ||
dea834b0 JF |
2771 | static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
2772 | JSValueRef setup[count + 2]; | |
2773 | setup[0] = _this; | |
2774 | setup[1] = object; | |
4afefdd9 | 2775 | memcpy(setup + 2, arguments, sizeof(JSValueRef) * count); |
dea834b0 JF |
2776 | return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception); |
2777 | } | |
2778 | ||
dc68b74c JF |
2779 | static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
2780 | CYPool pool; | |
2781 | Message_privateData *internal(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate(object))); | |
2782 | ||
2783 | // XXX: handle Instance::Uninitialized? | |
2784 | id self(CYCastNSObject(pool, context, _this)); | |
2785 | ||
2786 | void *setup[2]; | |
2787 | setup[0] = &self; | |
2788 | setup[1] = &internal->sel_; | |
2789 | ||
2790 | return CYCallFunction(pool, context, 2, setup, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue()); | |
2791 | } | |
2792 | ||
dea834b0 | 2793 | static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
4afefdd9 | 2794 | CYPool pool; |
dc68b74c JF |
2795 | Functor_privateData *internal(reinterpret_cast<Functor_privateData *>(JSObjectGetPrivate(object))); |
2796 | return CYCallFunction(pool, context, 0, NULL, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue()); | |
ea2d184c JF |
2797 | } |
2798 | ||
b09da87b | 2799 | JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
4cf49641 | 2800 | CYTry { |
dea834b0 JF |
2801 | if (count != 1) |
2802 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector constructor" userInfo:nil]; | |
2803 | const char *name(CYCastCString(context, arguments[0])); | |
2804 | return CYMakeSelector(context, sel_registerName(name)); | |
2805 | } CYCatch | |
2806 | } | |
2807 | ||
f7c38a29 JF |
2808 | JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
2809 | CYTry { | |
2810 | if (count != 2) | |
2811 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Functor constructor" userInfo:nil]; | |
2812 | ||
2813 | void *value(CYCastPointer<void *>(context, arguments[0])); | |
2814 | const char *type(CYCastCString(context, arguments[1])); | |
2815 | ||
2816 | CYPool pool; | |
2817 | ||
2818 | sig::Signature signature; | |
2819 | sig::Parse(pool, &signature, type, &Structor_); | |
2820 | ||
9b5527f0 | 2821 | return CYMakePointer(context, value, signature.elements[0].type, NULL, NULL); |
f7c38a29 JF |
2822 | } CYCatch |
2823 | } | |
2824 | ||
bce8339b | 2825 | JSObjectRef CYMakeType(JSContextRef context, JSObjectRef object, const char *type) { |
c239b9f8 | 2826 | Type_privateData *internal(new Type_privateData(NULL, type)); |
bce8339b JF |
2827 | return JSObjectMake(context, Type_, internal); |
2828 | } | |
2829 | ||
2830 | JSObjectRef Type_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2831 | CYTry { | |
2832 | if (count != 1) | |
2833 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Type constructor" userInfo:nil]; | |
2834 | const char *type(CYCastCString(context, arguments[0])); | |
2835 | return CYMakeType(context, object, type); | |
2836 | } CYCatch | |
2837 | } | |
2838 | ||
2839 | static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2840 | CYTry { | |
2841 | if (count != 1) | |
2842 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to type cast function" userInfo:nil]; | |
2843 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); | |
2844 | sig::Type *type(internal->type_); | |
2845 | ffi_type *ffi(internal->GetFFI()); | |
2846 | // XXX: alignment? | |
2847 | uint8_t value[ffi->size]; | |
2848 | CYPool pool; | |
2849 | CYPoolFFI(pool, context, type, ffi, value, arguments[0]); | |
c239b9f8 | 2850 | return CYFromFFI(context, type, ffi, value); |
bce8339b JF |
2851 | } CYCatch |
2852 | } | |
2853 | ||
2854 | static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2855 | CYTry { | |
2856 | if (count > 1) | |
2857 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to type cast function" userInfo:nil]; | |
2858 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); | |
e5bc40db | 2859 | size_t size(count == 0 ? 0 : CYCastDouble(context, arguments[0])); |
bce8339b | 2860 | // XXX: alignment? |
e5bc40db | 2861 | void *value(malloc(internal->GetFFI()->size * size)); |
9b5527f0 | 2862 | return CYMakePointer(context, value, internal->type_, internal->ffi_, NULL); |
bce8339b JF |
2863 | } CYCatch |
2864 | } | |
2865 | ||
7b184c00 JF |
2866 | JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
2867 | CYTry { | |
2868 | if (count > 1) | |
2869 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Instance constructor" userInfo:nil]; | |
2870 | id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0])); | |
dc68b74c | 2871 | return Instance::Make(context, self); |
7b184c00 JF |
2872 | } CYCatch |
2873 | } | |
2874 | ||
b09da87b | 2875 | JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
4cf49641 | 2876 | CYTry { |
b21525c7 | 2877 | if (count != 2) |
dea834b0 | 2878 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Functor constructor" userInfo:nil]; |
b21525c7 | 2879 | const char *type(CYCastCString(context, arguments[1])); |
dc68b74c | 2880 | return CYMakeFunctor(context, arguments[0], type); |
0c862573 | 2881 | } CYCatch |
ea2d184c JF |
2882 | } |
2883 | ||
61933e16 JF |
2884 | JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
2885 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(object))); | |
2886 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_)); | |
04450da0 JF |
2887 | } |
2888 | ||
7b184c00 JF |
2889 | static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
2890 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this))); | |
2891 | Type_privateData *typical(internal->GetType()); | |
2892 | ||
2893 | sig::Type *type; | |
2894 | ffi_type *ffi; | |
2895 | ||
2896 | if (typical == NULL) { | |
2897 | type = NULL; | |
2898 | ffi = NULL; | |
2899 | } else { | |
2900 | type = typical->type_; | |
2901 | ffi = typical->ffi_; | |
2902 | } | |
2903 | ||
2904 | return CYMakePointer(context, &internal->value_, type, ffi, object); | |
2905 | } | |
2906 | ||
61933e16 | 2907 | static JSValueRef CYValue_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
7b184c00 JF |
2908 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this))); |
2909 | ||
953647c1 | 2910 | CYTry { |
61933e16 | 2911 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_)); |
953647c1 JF |
2912 | } CYCatch |
2913 | } | |
2914 | ||
61933e16 JF |
2915 | static JSValueRef CYValue_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
2916 | return CYValue_callAsFunction_valueOf(context, object, _this, count, arguments, exception); | |
b4aa79af JF |
2917 | } |
2918 | ||
61933e16 | 2919 | static JSValueRef CYValue_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
7b184c00 JF |
2920 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this))); |
2921 | char string[32]; | |
2922 | sprintf(string, "%p", internal->value_); | |
2923 | ||
4afefdd9 | 2924 | CYTry { |
4afefdd9 JF |
2925 | return CYCastJSValue(context, string); |
2926 | } CYCatch | |
2927 | } | |
2928 | ||
dc68b74c JF |
2929 | static JSValueRef Instance_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
2930 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
2931 | return Instance::Make(context, object_getClass(internal->GetValue())); | |
2932 | } | |
2933 | ||
2934 | static JSValueRef Instance_getProperty_prototype(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
2935 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
2936 | id self(internal->GetValue()); | |
2937 | // XXX: this is a lame object_isClass | |
2938 | if (class_getInstanceMethod(object_getClass(self), @selector(alloc)) == NULL) | |
2939 | return CYJSUndefined(context); | |
2940 | return Prototype::Make(context, self); | |
2941 | } | |
2942 | ||
b4aa79af | 2943 | static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
7b184c00 JF |
2944 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); |
2945 | ||
b4aa79af | 2946 | CYTry { |
b4aa79af | 2947 | CYPoolTry { |
7b184c00 | 2948 | return CYCastJSValue(context, CYJSString(CYPoolNSCYON(NULL, internal->GetValue()))); |
b4aa79af JF |
2949 | } CYPoolCatch(NULL) |
2950 | } CYCatch | |
2951 | } | |
2952 | ||
2953 | static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
7b184c00 JF |
2954 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); |
2955 | ||
b4aa79af | 2956 | CYTry { |
b4aa79af JF |
2957 | CYPoolTry { |
2958 | NSString *key(count == 0 ? nil : CYCastNSString(NULL, CYJSString(context, arguments[0]))); | |
61933e16 | 2959 | return CYCastJSValue(context, CYJSString([internal->GetValue() cy$toJSON:key])); |
b4aa79af JF |
2960 | } CYPoolCatch(NULL) |
2961 | } CYCatch | |
2962 | } | |
2963 | ||
4cf49641 | 2964 | static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
9e562cfc | 2965 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); |
7b184c00 | 2966 | |
4cf49641 | 2967 | CYTry { |
4e8c99fb | 2968 | CYPoolTry { |
9e562cfc | 2969 | return CYCastJSValue(context, CYJSString([internal->GetValue() description])); |
4cf49641 | 2970 | } CYPoolCatch(NULL) |
478d4ed0 JF |
2971 | } CYCatch |
2972 | } | |
2973 | ||
2974 | static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
9e562cfc | 2975 | Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); |
7b184c00 | 2976 | |
4cf49641 | 2977 | CYTry { |
9e562cfc | 2978 | return CYCastJSValue(context, sel_getName(internal->GetValue())); |
478d4ed0 JF |
2979 | } CYCatch |
2980 | } | |
2981 | ||
b4aa79af JF |
2982 | static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
2983 | return Selector_callAsFunction_toString(context, object, _this, count, arguments, exception); | |
2984 | } | |
2985 | ||
2986 | static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
7b184c00 JF |
2987 | Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); |
2988 | const char *name(sel_getName(internal->GetValue())); | |
2989 | ||
b4aa79af | 2990 | CYTry { |
b4aa79af JF |
2991 | CYPoolTry { |
2992 | return CYCastJSValue(context, CYJSString([NSString stringWithFormat:@"@selector(%s)", name])); | |
2993 | } CYPoolCatch(NULL) | |
2994 | } CYCatch | |
2995 | } | |
2996 | ||
b09da87b | 2997 | static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
4cf49641 | 2998 | CYTry { |
e5bc40db | 2999 | if (count != 1) |
b09da87b JF |
3000 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector.type" userInfo:nil]; |
3001 | CYPool pool; | |
e5bc40db | 3002 | Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); |
b09da87b | 3003 | Class _class(CYCastNSObject(pool, context, arguments[0])); |
e5bc40db | 3004 | SEL sel(internal->GetValue()); |
dc68b74c JF |
3005 | Method method(class_getInstanceMethod(_class, sel)); |
3006 | const char *type(CYPoolTypeEncoding(pool, _class, sel, method)); | |
3007 | return type == NULL ? CYJSNull(context) : CYCastJSValue(context, CYJSString(type)); | |
b09da87b JF |
3008 | } CYCatch |
3009 | } | |
3010 | ||
e5bc40db JF |
3011 | static JSValueRef Type_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
3012 | CYTry { | |
3013 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
3014 | CYPool pool; | |
3015 | const char *type(sig::Unparse(pool, internal->type_)); | |
3016 | CYPoolTry { | |
3017 | return CYCastJSValue(context, CYJSString(type)); | |
3018 | } CYPoolCatch(NULL) | |
3019 | } CYCatch | |
3020 | } | |
3021 | ||
3022 | static JSValueRef Type_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3023 | CYTry { | |
3024 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
3025 | CYPool pool; | |
3026 | const char *type(sig::Unparse(pool, internal->type_)); | |
3027 | CYPoolTry { | |
3028 | return CYCastJSValue(context, CYJSString([NSString stringWithFormat:@"new Type(%@)", [[NSString stringWithUTF8String:type] cy$toCYON]])); | |
3029 | } CYPoolCatch(NULL) | |
3030 | } CYCatch | |
3031 | } | |
3032 | ||
3033 | static JSValueRef Type_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
3034 | return Type_callAsFunction_toString(context, object, _this, count, arguments, exception); | |
3035 | } | |
3036 | ||
61933e16 JF |
3037 | static JSStaticValue CYValue_staticValues[2] = { |
3038 | {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete}, | |
04450da0 JF |
3039 | {NULL, NULL, NULL, 0} |
3040 | }; | |
3041 | ||
9b5527f0 | 3042 | static JSStaticValue Pointer_staticValues[2] = { |
7b184c00 | 3043 | {"$cyi", &Pointer_getProperty_$cyi, &Pointer_setProperty_$cyi, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
9b5527f0 JF |
3044 | {NULL, NULL, NULL, 0} |
3045 | }; | |
3046 | ||
4afefdd9 | 3047 | static JSStaticFunction Pointer_staticFunctions[4] = { |
61933e16 JF |
3048 | {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
3049 | {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3050 | {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
ff783f8c JF |
3051 | {NULL, NULL, 0} |
3052 | }; | |
3053 | ||
9b5527f0 JF |
3054 | static JSStaticFunction Struct_staticFunctions[2] = { |
3055 | {"$cya", &Struct_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3056 | {NULL, NULL, 0} | |
3057 | }; | |
3058 | ||
ff783f8c | 3059 | static JSStaticFunction Functor_staticFunctions[4] = { |
61933e16 JF |
3060 | {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
3061 | {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3062 | {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
953647c1 JF |
3063 | {NULL, NULL, 0} |
3064 | }; | |
3065 | ||
dc68b74c | 3066 | static JSStaticValue Instance_staticValues[4] = { |
9e562cfc JF |
3067 | {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
3068 | {"prototype", &Instance_getProperty_prototype, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3069 | {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
9b5527f0 JF |
3070 | {NULL, NULL, NULL, 0} |
3071 | }; | |
3072 | ||
7b184c00 JF |
3073 | static JSStaticFunction Instance_staticFunctions[5] = { |
3074 | {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
b4aa79af JF |
3075 | {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
3076 | {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
478d4ed0 JF |
3077 | {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
3078 | {NULL, NULL, 0} | |
3079 | }; | |
3080 | ||
9b5527f0 JF |
3081 | static JSStaticFunction Internal_staticFunctions[2] = { |
3082 | {"$cya", &Internal_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3083 | {NULL, NULL, 0} | |
3084 | }; | |
3085 | ||
b4aa79af JF |
3086 | static JSStaticFunction Selector_staticFunctions[5] = { |
3087 | {"toCYON", &Selector_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3088 | {"toJSON", &Selector_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
478d4ed0 | 3089 | {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
b09da87b JF |
3090 | {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
3091 | {NULL, NULL, 0} | |
3092 | }; | |
3093 | ||
9b5527f0 | 3094 | static JSStaticFunction Type_staticFunctions[4] = { |
e5bc40db JF |
3095 | {"toCYON", &Type_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
3096 | {"toJSON", &Type_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3097 | {"toString", &Type_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
3098 | {NULL, NULL, 0} | |
3099 | }; | |
3100 | ||
5999c315 | 3101 | CYDriver::CYDriver(const std::string &filename) : |
db5e2840 | 3102 | state_(CYClear), |
e7ed5354 JF |
3103 | data_(NULL), |
3104 | size_(0), | |
48e3be8a | 3105 | file_(NULL), |
5999c315 JF |
3106 | filename_(filename), |
3107 | source_(NULL) | |
3108 | { | |
924f67b2 JF |
3109 | ScannerInit(); |
3110 | } | |
3111 | ||
5999c315 | 3112 | CYDriver::~CYDriver() { |
924f67b2 JF |
3113 | ScannerDestroy(); |
3114 | } | |
3115 | ||
5befe15e JF |
3116 | void cy::parser::error(const cy::parser::location_type &location, const std::string &message) { |
3117 | CYDriver::Error error; | |
3118 | error.location_ = location; | |
3119 | error.message_ = message; | |
3120 | driver.errors_.push_back(error); | |
63b4c5a8 JF |
3121 | } |
3122 | ||
b09da87b JF |
3123 | void CYSetArgs(int argc, const char *argv[]) { |
3124 | JSContextRef context(CYGetJSContext()); | |
3125 | JSValueRef args[argc]; | |
3126 | for (int i(0); i != argc; ++i) | |
3127 | args[i] = CYCastJSValue(context, argv[i]); | |
3128 | JSValueRef exception(NULL); | |
3129 | JSObjectRef array(JSObjectMakeArray(context, argc, args, &exception)); | |
3130 | CYThrow(context, exception); | |
3131 | CYSetProperty(context, System_, CYJSString("args"), array); | |
3132 | } | |
3133 | ||
579ed526 JF |
3134 | JSObjectRef CYGetGlobalObject(JSContextRef context) { |
3135 | return JSContextGetGlobalObject(context); | |
3136 | } | |
3137 | ||
967067aa | 3138 | const char *CYExecute(apr_pool_t *pool, const char *code) { _pooled |
967067aa | 3139 | JSContextRef context(CYGetJSContext()); |
c239b9f8 | 3140 | JSValueRef exception(NULL), result; |
967067aa | 3141 | |
c239b9f8 JF |
3142 | try { |
3143 | result = JSEvaluateScript(context, CYJSString(code), NULL, NULL, 0, &exception); | |
3144 | } catch (const char *error) { | |
3145 | return error; | |
3146 | } | |
967067aa JF |
3147 | |
3148 | if (exception != NULL) { error: | |
3149 | result = exception; | |
3150 | exception = NULL; | |
3151 | } | |
3152 | ||
3153 | if (JSValueIsUndefined(context, result)) | |
3154 | return NULL; | |
3155 | ||
c239b9f8 | 3156 | const char *json(CYPoolCCYON(pool, context, result, &exception)); |
967067aa JF |
3157 | if (exception != NULL) |
3158 | goto error; | |
3159 | ||
3160 | CYSetProperty(context, CYGetGlobalObject(context), Result_, result); | |
3161 | return json; | |
3162 | } | |
3163 | ||
3164 | bool CYRecvAll_(int socket, uint8_t *data, size_t size) { | |
3165 | while (size != 0) if (size_t writ = _syscall(recv(socket, data, size, 0))) { | |
3166 | data += writ; | |
3167 | size -= writ; | |
3168 | } else | |
3169 | return false; | |
3170 | return true; | |
3171 | } | |
3172 | ||
3173 | bool CYSendAll_(int socket, const uint8_t *data, size_t size) { | |
3174 | while (size != 0) if (size_t writ = _syscall(send(socket, data, size, 0))) { | |
3175 | data += writ; | |
3176 | size -= writ; | |
3177 | } else | |
3178 | return false; | |
3179 | return true; | |
3180 | } | |
3181 | ||
967067aa JF |
3182 | apr_pool_t *Pool_; |
3183 | ||
3184 | struct CYExecute_ { | |
3185 | apr_pool_t *pool_; | |
3186 | const char * volatile data_; | |
3187 | }; | |
3188 | ||
3189 | // XXX: this is "tre lame" | |
3190 | @interface CYClient_ : NSObject { | |
3191 | } | |
3192 | ||
3193 | - (void) execute:(NSValue *)value; | |
3194 | ||
3195 | @end | |
3196 | ||
3197 | @implementation CYClient_ | |
3198 | ||
3199 | - (void) execute:(NSValue *)value { | |
3200 | CYExecute_ *execute(reinterpret_cast<CYExecute_ *>([value pointerValue])); | |
c239b9f8 JF |
3201 | const char *data(execute->data_); |
3202 | execute->data_ = NULL; | |
3203 | execute->data_ = CYExecute(execute->pool_, data); | |
967067aa JF |
3204 | } |
3205 | ||
3206 | @end | |
3207 | ||
3208 | struct CYClient : | |
3209 | CYData | |
3210 | { | |
3211 | int socket_; | |
3212 | apr_thread_t *thread_; | |
3213 | ||
3214 | CYClient(int socket) : | |
3215 | socket_(socket) | |
3216 | { | |
3217 | } | |
3218 | ||
3219 | ~CYClient() { | |
3220 | _syscall(close(socket_)); | |
3221 | } | |
3222 | ||
3223 | void Handle() { _pooled | |
3224 | CYClient_ *client = [[[CYClient_ alloc] init] autorelease]; | |
3225 | ||
3226 | for (;;) { | |
3227 | size_t size; | |
3228 | if (!CYRecvAll(socket_, &size, sizeof(size))) | |
3229 | return; | |
3230 | ||
3231 | CYPool pool; | |
3232 | char *data(new(pool) char[size + 1]); | |
3233 | if (!CYRecvAll(socket_, data, size)) | |
3234 | return; | |
3235 | data[size] = '\0'; | |
3236 | ||
3237 | CYDriver driver(""); | |
3238 | cy::parser parser(driver); | |
3239 | ||
3240 | driver.data_ = data; | |
3241 | driver.size_ = size; | |
3242 | ||
3243 | const char *json; | |
3244 | if (parser.parse() != 0 || !driver.errors_.empty()) { | |
3245 | json = NULL; | |
3246 | size = _not(size_t); | |
3247 | } else { | |
3248 | std::ostringstream str; | |
3249 | driver.source_->Show(str); | |
3250 | std::string code(str.str()); | |
3251 | CYExecute_ execute = {pool, code.c_str()}; | |
3252 | [client performSelectorOnMainThread:@selector(execute:) withObject:[NSValue valueWithPointer:&execute] waitUntilDone:YES]; | |
3253 | json = execute.data_; | |
3254 | size = json == NULL ? _not(size_t) : strlen(json); | |
3255 | } | |
3256 | ||
3257 | if (!CYSendAll(socket_, &size, sizeof(size))) | |
3258 | return; | |
3259 | if (json != NULL) | |
3260 | if (!CYSendAll(socket_, json, size)) | |
3261 | return; | |
3262 | } | |
3263 | } | |
3264 | }; | |
3265 | ||
3266 | static void * APR_THREAD_FUNC OnClient(apr_thread_t *thread, void *data) { | |
3267 | CYClient *client(reinterpret_cast<CYClient *>(data)); | |
3268 | client->Handle(); | |
3269 | delete client; | |
3270 | return NULL; | |
3271 | } | |
3272 | ||
1ef7d061 JF |
3273 | extern "C" void CYHandleClient(apr_pool_t *pool, int socket) { |
3274 | CYClient *client(new(pool) CYClient(socket)); | |
3275 | apr_threadattr_t *attr; | |
3276 | _aprcall(apr_threadattr_create(&attr, client->pool_)); | |
3277 | _aprcall(apr_thread_create(&client->thread_, attr, &OnClient, client, client->pool_)); | |
bce8339b JF |
3278 | } |
3279 | ||
7ba62cfd | 3280 | MSInitialize { _pooled |
967067aa JF |
3281 | _aprcall(apr_initialize()); |
3282 | _aprcall(apr_pool_create(&Pool_, NULL)); | |
ea2d184c | 3283 | |
7b184c00 JF |
3284 | Type_privateData::Object = new(Pool_) Type_privateData(Pool_, "@"); |
3285 | Type_privateData::Selector = new(Pool_) Type_privateData(Pool_, ":"); | |
3286 | ||
953647c1 | 3287 | Bridge_ = [[NSMutableArray arrayWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain]; |
c239b9f8 | 3288 | |
faf69207 | 3289 | NSArray_ = objc_getClass("NSArray"); |
c1582939 | 3290 | NSCFBoolean_ = objc_getClass("NSCFBoolean"); |
c239b9f8 JF |
3291 | NSCFType_ = objc_getClass("NSCFType"); |
3292 | NSMessageBuilder_ = objc_getClass("NSMessageBuilder"); | |
3293 | NSZombie_ = objc_getClass("_NSZombie_"); | |
3294 | Object_ = objc_getClass("Object"); | |
967067aa JF |
3295 | } |
3296 | ||
3297 | JSGlobalContextRef CYGetJSContext() { | |
3298 | if (Context_ == NULL) { | |
3299 | JSClassDefinition definition; | |
3300 | ||
967067aa JF |
3301 | definition = kJSClassDefinitionEmpty; |
3302 | definition.className = "Functor"; | |
3303 | definition.staticFunctions = Functor_staticFunctions; | |
3304 | definition.callAsFunction = &Functor_callAsFunction; | |
1ef7d061 | 3305 | definition.finalize = &Finalize; |
967067aa JF |
3306 | Functor_ = JSClassCreate(&definition); |
3307 | ||
3308 | definition = kJSClassDefinitionEmpty; | |
bce8339b | 3309 | definition.className = "Instance"; |
9b5527f0 | 3310 | definition.staticValues = Instance_staticValues; |
bce8339b | 3311 | definition.staticFunctions = Instance_staticFunctions; |
7b184c00 | 3312 | definition.hasProperty = &Instance_hasProperty; |
bce8339b JF |
3313 | definition.getProperty = &Instance_getProperty; |
3314 | definition.setProperty = &Instance_setProperty; | |
3315 | definition.deleteProperty = &Instance_deleteProperty; | |
c239b9f8 | 3316 | definition.getPropertyNames = &Instance_getPropertyNames; |
bce8339b | 3317 | definition.callAsConstructor = &Instance_callAsConstructor; |
1ef7d061 | 3318 | definition.finalize = &Finalize; |
bce8339b JF |
3319 | Instance_ = JSClassCreate(&definition); |
3320 | ||
9b5527f0 JF |
3321 | definition = kJSClassDefinitionEmpty; |
3322 | definition.className = "Internal"; | |
3323 | definition.staticFunctions = Internal_staticFunctions; | |
7b184c00 | 3324 | definition.hasProperty = &Internal_hasProperty; |
9b5527f0 JF |
3325 | definition.getProperty = &Internal_getProperty; |
3326 | definition.setProperty = &Internal_setProperty; | |
3327 | definition.getPropertyNames = &Internal_getPropertyNames; | |
1ef7d061 | 3328 | definition.finalize = &Finalize; |
9b5527f0 JF |
3329 | Internal_ = JSClassCreate(&definition); |
3330 | ||
dc68b74c JF |
3331 | definition = kJSClassDefinitionEmpty; |
3332 | definition.className = "Message"; | |
3333 | definition.staticFunctions = Functor_staticFunctions; | |
3334 | definition.callAsFunction = &Message_callAsFunction; | |
3335 | definition.finalize = &Finalize; | |
3336 | Message_ = JSClassCreate(&definition); | |
3337 | ||
bce8339b JF |
3338 | definition = kJSClassDefinitionEmpty; |
3339 | definition.className = "Pointer"; | |
9b5527f0 | 3340 | definition.staticValues = Pointer_staticValues; |
bce8339b JF |
3341 | definition.staticFunctions = Pointer_staticFunctions; |
3342 | definition.getProperty = &Pointer_getProperty; | |
3343 | definition.setProperty = &Pointer_setProperty; | |
1ef7d061 | 3344 | definition.finalize = &Finalize; |
bce8339b | 3345 | Pointer_ = JSClassCreate(&definition); |
967067aa | 3346 | |
dc68b74c JF |
3347 | definition = kJSClassDefinitionEmpty; |
3348 | definition.className = "Prototype"; | |
3349 | definition.hasProperty = &Prototype_hasProperty; | |
3350 | definition.getProperty = &Prototype_getProperty; | |
3351 | definition.setProperty = &Prototype_setProperty; | |
3352 | #if !__OBJC2__ | |
3353 | definition.deleteProperty = &Prototype_deleteProperty; | |
3354 | #endif | |
3355 | definition.getPropertyNames = &Prototype_getPropertyNames; | |
3356 | definition.finalize = &Finalize; | |
3357 | Prototype_ = JSClassCreate(&definition); | |
3358 | ||
967067aa JF |
3359 | definition = kJSClassDefinitionEmpty; |
3360 | definition.className = "Selector"; | |
3361 | definition.staticValues = CYValue_staticValues; | |
967067aa JF |
3362 | definition.staticFunctions = Selector_staticFunctions; |
3363 | definition.callAsFunction = &Selector_callAsFunction; | |
1ef7d061 | 3364 | definition.finalize = &Finalize; |
967067aa JF |
3365 | Selector_ = JSClassCreate(&definition); |
3366 | ||
3367 | definition = kJSClassDefinitionEmpty; | |
bce8339b | 3368 | definition.className = "Struct"; |
9b5527f0 | 3369 | definition.staticFunctions = Struct_staticFunctions; |
bce8339b JF |
3370 | definition.getProperty = &Struct_getProperty; |
3371 | definition.setProperty = &Struct_setProperty; | |
3372 | definition.getPropertyNames = &Struct_getPropertyNames; | |
1ef7d061 | 3373 | definition.finalize = &Finalize; |
bce8339b JF |
3374 | Struct_ = JSClassCreate(&definition); |
3375 | ||
3376 | definition = kJSClassDefinitionEmpty; | |
3377 | definition.className = "Type"; | |
e5bc40db | 3378 | definition.staticFunctions = Type_staticFunctions; |
c239b9f8 | 3379 | //definition.getProperty = &Type_getProperty; |
bce8339b JF |
3380 | definition.callAsFunction = &Type_callAsFunction; |
3381 | definition.callAsConstructor = &Type_callAsConstructor; | |
1ef7d061 | 3382 | definition.finalize = &Finalize; |
bce8339b | 3383 | Type_ = JSClassCreate(&definition); |
967067aa JF |
3384 | |
3385 | definition = kJSClassDefinitionEmpty; | |
3386 | definition.className = "Runtime"; | |
3387 | definition.getProperty = &Runtime_getProperty; | |
3388 | Runtime_ = JSClassCreate(&definition); | |
3389 | ||
c239b9f8 JF |
3390 | definition = kJSClassDefinitionEmpty; |
3391 | definition.className = "ObjectiveC::Classes"; | |
3392 | definition.getProperty = &ObjectiveC_Classes_getProperty; | |
3393 | definition.getPropertyNames = &ObjectiveC_Classes_getPropertyNames; | |
3394 | ObjectiveC_Classes_ = JSClassCreate(&definition); | |
3395 | ||
3396 | definition = kJSClassDefinitionEmpty; | |
3397 | definition.className = "ObjectiveC::Images"; | |
3398 | definition.getProperty = &ObjectiveC_Images_getProperty; | |
3399 | definition.getPropertyNames = &ObjectiveC_Images_getPropertyNames; | |
3400 | ObjectiveC_Images_ = JSClassCreate(&definition); | |
3401 | ||
3402 | definition = kJSClassDefinitionEmpty; | |
3403 | definition.className = "ObjectiveC::Image::Classes"; | |
3404 | definition.getProperty = &ObjectiveC_Image_Classes_getProperty; | |
3405 | definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames; | |
1ef7d061 | 3406 | definition.finalize = &Finalize; |
c239b9f8 JF |
3407 | ObjectiveC_Image_Classes_ = JSClassCreate(&definition); |
3408 | ||
3409 | definition = kJSClassDefinitionEmpty; | |
3410 | definition.className = "ObjectiveC::Protocols"; | |
3411 | definition.getProperty = &ObjectiveC_Protocols_getProperty; | |
3412 | definition.getPropertyNames = &ObjectiveC_Protocols_getPropertyNames; | |
3413 | ObjectiveC_Protocols_ = JSClassCreate(&definition); | |
3414 | ||
967067aa JF |
3415 | definition = kJSClassDefinitionEmpty; |
3416 | //definition.getProperty = &Global_getProperty; | |
3417 | JSClassRef Global(JSClassCreate(&definition)); | |
3418 | ||
3419 | JSGlobalContextRef context(JSGlobalContextCreate(Global)); | |
3420 | Context_ = context; | |
3421 | ||
3422 | JSObjectRef global(CYGetGlobalObject(context)); | |
3423 | ||
3424 | JSObjectSetPrototype(context, global, JSObjectMake(context, Runtime_, NULL)); | |
c239b9f8 JF |
3425 | ObjectiveC_ = JSObjectMake(context, NULL, NULL); |
3426 | CYSetProperty(context, global, CYJSString("ObjectiveC"), ObjectiveC_); | |
3427 | ||
3428 | CYSetProperty(context, ObjectiveC_, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Classes_, NULL)); | |
3429 | CYSetProperty(context, ObjectiveC_, CYJSString("images"), JSObjectMake(context, ObjectiveC_Images_, NULL)); | |
3430 | CYSetProperty(context, ObjectiveC_, CYJSString("protocols"), JSObjectMake(context, ObjectiveC_Protocols_, NULL)); | |
967067aa | 3431 | |
dc68b74c JF |
3432 | Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array"))); |
3433 | Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function"))); | |
3434 | ||
3435 | length_ = JSStringCreateWithUTF8CString("length"); | |
3436 | message_ = JSStringCreateWithUTF8CString("message"); | |
3437 | name_ = JSStringCreateWithUTF8CString("name"); | |
3438 | prototype_ = JSStringCreateWithUTF8CString("prototype"); | |
3439 | toCYON_ = JSStringCreateWithUTF8CString("toCYON"); | |
3440 | toJSON_ = JSStringCreateWithUTF8CString("toJSON"); | |
3441 | ||
faf69207 JF |
3442 | Array_prototype_ = CYCastJSObject(context, CYGetProperty(context, Array_, prototype_)); |
3443 | Array_pop_ = CYCastJSObject(context, CYGetProperty(context, Array_prototype_, CYJSString("pop"))); | |
3444 | Array_push_ = CYCastJSObject(context, CYGetProperty(context, Array_prototype_, CYJSString("push"))); | |
3445 | Array_splice_ = CYCastJSObject(context, CYGetProperty(context, Array_prototype_, CYJSString("splice"))); | |
3446 | ||
dc68b74c JF |
3447 | JSObjectRef Functor(JSObjectMakeConstructor(context, Functor_, &Functor_new)); |
3448 | JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL)); | |
9e562cfc | 3449 | JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new)); |
dc68b74c JF |
3450 | |
3451 | JSValueRef function(CYGetProperty(context, Function_, prototype_)); | |
3452 | JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Message, prototype_), function); | |
3453 | JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Functor, prototype_), function); | |
9e562cfc | 3454 | JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Selector, prototype_), function); |
dc68b74c JF |
3455 | |
3456 | CYSetProperty(context, global, CYJSString("Functor"), Functor); | |
7b184c00 | 3457 | CYSetProperty(context, global, CYJSString("Instance"), JSObjectMakeConstructor(context, Instance_, &Instance_new)); |
967067aa | 3458 | CYSetProperty(context, global, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new)); |
9e562cfc | 3459 | CYSetProperty(context, global, CYJSString("Selector"), Selector); |
bce8339b | 3460 | CYSetProperty(context, global, CYJSString("Type"), JSObjectMakeConstructor(context, Type_, &Type_new)); |
967067aa JF |
3461 | |
3462 | MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair)); | |
3463 | ||
c239b9f8 JF |
3464 | class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON), "@12@0:4@8"); |
3465 | ||
967067aa JF |
3466 | CYSetProperty(context, global, CYJSString("objc_registerClassPair"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_registerClassPair"), &objc_registerClassPair_)); |
3467 | CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend)); | |
3468 | ||
3469 | System_ = JSObjectMake(context, NULL, NULL); | |
3470 | CYSetProperty(context, global, CYJSString("system"), System_); | |
3471 | CYSetProperty(context, System_, CYJSString("args"), CYJSNull(context)); | |
3472 | //CYSetProperty(context, System_, CYJSString("global"), global); | |
3473 | ||
3474 | CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print)); | |
3475 | ||
3476 | Result_ = JSStringCreateWithUTF8CString("_"); | |
967067aa JF |
3477 | } |
3478 | ||
3479 | return Context_; | |
c1582939 | 3480 | } |