]>
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 <unistd.h> |
52 | ||
53 | #include <CoreFoundation/CoreFoundation.h> | |
54 | #include <CoreFoundation/CFLogUtilities.h> | |
55 | ||
c1582939 JF |
56 | #include <WebKit/WebScriptObject.h> |
57 | ||
58 | #include <sys/types.h> | |
59 | #include <sys/socket.h> | |
60 | #include <netinet/in.h> | |
b09da87b | 61 | #include <sys/mman.h> |
c1582939 | 62 | |
8d9b5eed JF |
63 | #include <iostream> |
64 | #include <ext/stdio_filebuf.h> | |
a2d9403c JF |
65 | #include <set> |
66 | #include <map> | |
8d9b5eed | 67 | |
bd17e6f3 JF |
68 | #include <cmath> |
69 | ||
e5332278 | 70 | #include "Parser.hpp" |
63b4c5a8 | 71 | #include "Cycript.tab.hh" |
e5332278 | 72 | |
ea2d184c JF |
73 | #undef _assert |
74 | #undef _trace | |
75 | ||
c1582939 | 76 | #define _assert(test) do { \ |
f5e9be24 JF |
77 | if (!(test)) \ |
78 | @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"_assert(%s):%s(%u):%s", #test, __FILE__, __LINE__, __FUNCTION__] userInfo:nil]; \ | |
c1582939 JF |
79 | } while (false) |
80 | ||
81 | #define _trace() do { \ | |
62ca2b82 | 82 | CFLog(kCFLogLevelNotice, CFSTR("_trace():%u"), __LINE__); \ |
c1582939 JF |
83 | } while (false) |
84 | ||
4cf49641 JF |
85 | #define CYPoolTry { \ |
86 | id _saved(nil); \ | |
87 | NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \ | |
88 | @try | |
89 | #define CYPoolCatch(value) \ | |
90 | @catch (NSException *error) { \ | |
91 | _saved = [error retain]; \ | |
92 | @throw; \ | |
93 | return value; \ | |
94 | } @finally { \ | |
95 | [_pool release]; \ | |
96 | if (_saved != nil) \ | |
97 | [_saved autorelease]; \ | |
98 | } \ | |
99 | } | |
100 | ||
478d4ed0 | 101 | static JSGlobalContextRef Context_; |
b09da87b | 102 | static JSObjectRef System_; |
ea2d184c | 103 | |
88c977fa JF |
104 | static JSClassRef Functor_; |
105 | static JSClassRef Instance_; | |
106 | static JSClassRef Pointer_; | |
953647c1 | 107 | static JSClassRef Runtime_; |
88c977fa | 108 | static JSClassRef Selector_; |
953647c1 | 109 | static JSClassRef Struct_; |
ea2d184c | 110 | |
c1582939 | 111 | static JSObjectRef Array_; |
dea834b0 | 112 | static JSObjectRef Function_; |
ea2d184c | 113 | |
c1582939 | 114 | static JSStringRef length_; |
b4aa79af JF |
115 | static JSStringRef message_; |
116 | static JSStringRef name_; | |
117 | static JSStringRef toCYON_; | |
118 | static JSStringRef toJSON_; | |
ea2d184c | 119 | |
c1582939 JF |
120 | static Class NSCFBoolean_; |
121 | ||
953647c1 | 122 | static NSArray *Bridge_; |
88c977fa | 123 | |
953647c1 | 124 | struct CYData { |
478d4ed0 | 125 | apr_pool_t *pool_; |
953647c1 JF |
126 | |
127 | virtual ~CYData() { | |
128 | } | |
478d4ed0 | 129 | |
61933e16 | 130 | static void *operator new(size_t size) { |
478d4ed0 JF |
131 | apr_pool_t *pool; |
132 | apr_pool_create(&pool, NULL); | |
133 | void *data(apr_palloc(pool, size)); | |
953647c1 | 134 | reinterpret_cast<CYData *>(data)->pool_ = pool; |
61933e16 JF |
135 | return data; |
136 | } | |
137 | ||
138 | static void operator delete(void *data) { | |
139 | apr_pool_destroy(reinterpret_cast<CYData *>(data)->pool_); | |
478d4ed0 JF |
140 | } |
141 | ||
953647c1 | 142 | static void Finalize(JSObjectRef object) { |
61933e16 | 143 | delete reinterpret_cast<CYData *>(JSObjectGetPrivate(object)); |
478d4ed0 | 144 | } |
953647c1 JF |
145 | }; |
146 | ||
61933e16 | 147 | struct CYValue : |
953647c1 | 148 | CYData |
61933e16 JF |
149 | { |
150 | void *value_; | |
151 | ||
152 | CYValue() { | |
153 | } | |
154 | ||
155 | CYValue(void *value) : | |
156 | value_(value) | |
157 | { | |
158 | } | |
159 | }; | |
160 | ||
161 | struct Selector_privateData : | |
162 | CYValue | |
953647c1 | 163 | { |
953647c1 | 164 | Selector_privateData(SEL value) : |
61933e16 | 165 | CYValue(value) |
478d4ed0 JF |
166 | { |
167 | } | |
168 | ||
169 | SEL GetValue() const { | |
170 | return reinterpret_cast<SEL>(value_); | |
171 | } | |
172 | }; | |
173 | ||
2b52f27e | 174 | struct Instance : |
61933e16 | 175 | CYValue |
bd17e6f3 | 176 | { |
2b52f27e JF |
177 | enum Flags { |
178 | None = 0, | |
179 | Transient = (1 << 0), | |
180 | Uninitialized = (1 << 1), | |
181 | }; | |
478d4ed0 | 182 | |
2b52f27e JF |
183 | Flags flags_; |
184 | ||
185 | Instance(id value, Flags flags) : | |
61933e16 | 186 | CYValue(value), |
2b52f27e | 187 | flags_(flags) |
478d4ed0 JF |
188 | { |
189 | } | |
190 | ||
2b52f27e JF |
191 | virtual ~Instance() { |
192 | if ((flags_ & Transient) == 0) | |
61933e16 JF |
193 | // XXX: does this handle background threads correctly? |
194 | [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0]; | |
478d4ed0 JF |
195 | } |
196 | ||
2b52f27e JF |
197 | static JSObjectRef Make(JSContextRef context, id object, Flags flags) { |
198 | return JSObjectMake(context, Instance_, new Instance(object, flags)); | |
199 | } | |
200 | ||
478d4ed0 JF |
201 | id GetValue() const { |
202 | return reinterpret_cast<id>(value_); | |
203 | } | |
2b52f27e JF |
204 | |
205 | bool IsUninitialized() const { | |
206 | return (flags_ & Uninitialized) != 0; | |
207 | } | |
478d4ed0 JF |
208 | }; |
209 | ||
bd17e6f3 JF |
210 | namespace sig { |
211 | ||
212 | void Copy(apr_pool_t *pool, Type &lhs, Type &rhs); | |
213 | ||
214 | void Copy(apr_pool_t *pool, Element &lhs, Element &rhs) { | |
215 | lhs.name = apr_pstrdup(pool, rhs.name); | |
216 | if (rhs.type == NULL) | |
217 | lhs.type = NULL; | |
218 | else { | |
219 | lhs.type = new(pool) Type; | |
220 | Copy(pool, *lhs.type, *rhs.type); | |
221 | } | |
222 | lhs.offset = rhs.offset; | |
223 | } | |
224 | ||
225 | void Copy(apr_pool_t *pool, Signature &lhs, Signature &rhs) { | |
226 | size_t count(rhs.count); | |
227 | lhs.count = count; | |
228 | lhs.elements = new(pool) Element[count]; | |
229 | for (size_t index(0); index != count; ++index) | |
230 | Copy(pool, lhs.elements[index], rhs.elements[index]); | |
231 | } | |
232 | ||
233 | void Copy(apr_pool_t *pool, Type &lhs, Type &rhs) { | |
234 | lhs.primitive = rhs.primitive; | |
235 | lhs.name = apr_pstrdup(pool, rhs.name); | |
236 | lhs.flags = rhs.flags; | |
237 | ||
238 | if (sig::IsAggregate(rhs.primitive)) | |
239 | Copy(pool, lhs.data.signature, rhs.data.signature); | |
240 | else { | |
241 | if (rhs.data.data.type != NULL) { | |
242 | lhs.data.data.type = new(pool) Type; | |
243 | Copy(pool, *lhs.data.data.type, *rhs.data.data.type); | |
244 | } | |
245 | ||
246 | lhs.data.data.size = rhs.data.data.size; | |
247 | } | |
248 | } | |
249 | ||
250 | void Copy(apr_pool_t *pool, ffi_type &lhs, ffi_type &rhs) { | |
251 | lhs.size = rhs.size; | |
252 | lhs.alignment = rhs.alignment; | |
253 | lhs.type = rhs.type; | |
254 | if (rhs.elements == NULL) | |
255 | lhs.elements = NULL; | |
256 | else { | |
257 | size_t count(0); | |
258 | while (rhs.elements[count] != NULL) | |
259 | ++count; | |
260 | ||
261 | lhs.elements = new(pool) ffi_type *[count + 1]; | |
262 | lhs.elements[count] = NULL; | |
263 | ||
264 | for (size_t index(0); index != count; ++index) { | |
265 | // XXX: if these are libffi native then you can just take them | |
266 | ffi_type *ffi(new(pool) ffi_type); | |
267 | lhs.elements[index] = ffi; | |
268 | sig::Copy(pool, *ffi, *rhs.elements[index]); | |
269 | } | |
270 | } | |
271 | } | |
272 | ||
273 | } | |
274 | ||
f33b048a JF |
275 | struct CStringMapLess : |
276 | std::binary_function<const char *, const char *, bool> | |
277 | { | |
278 | _finline bool operator ()(const char *lhs, const char *rhs) const { | |
279 | return strcmp(lhs, rhs) < 0; | |
280 | } | |
281 | }; | |
282 | ||
bd17e6f3 | 283 | struct Type_privateData { |
ff783f8c JF |
284 | apr_pool_t *pool_; |
285 | ||
286 | ffi_type *ffi_; | |
930aa21b | 287 | sig::Type *type_; |
bd17e6f3 | 288 | |
ff783f8c JF |
289 | Type_privateData(apr_pool_t *pool, sig::Type *type) : |
290 | pool_(pool), | |
291 | ffi_(NULL) | |
292 | { | |
930aa21b JF |
293 | if (type != NULL) { |
294 | type_ = new(pool) sig::Type; | |
295 | sig::Copy(pool, *type_, *type); | |
296 | } | |
ff783f8c JF |
297 | } |
298 | ||
299 | Type_privateData(apr_pool_t *pool, sig::Type *type, ffi_type *ffi) : | |
300 | pool_(pool) | |
301 | { | |
302 | ffi_ = new(pool) ffi_type; | |
303 | sig::Copy(pool, *ffi_, *ffi); | |
930aa21b JF |
304 | type_ = new(pool) sig::Type; |
305 | sig::Copy(pool, *type_, *type); | |
ff783f8c JF |
306 | } |
307 | ||
308 | ffi_type *GetFFI() { | |
309 | if (ffi_ == NULL) { | |
310 | ffi_ = new(pool_) ffi_type; | |
311 | ||
312 | sig::Element element; | |
313 | element.name = NULL; | |
930aa21b | 314 | element.type = type_; |
ff783f8c JF |
315 | element.offset = 0; |
316 | ||
317 | sig::Signature signature; | |
318 | signature.elements = &element; | |
319 | signature.count = 1; | |
320 | ||
321 | ffi_cif cif; | |
322 | sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature, &cif); | |
323 | *ffi_ = *cif.rtype; | |
324 | } | |
325 | ||
326 | return ffi_; | |
327 | } | |
328 | }; | |
329 | ||
330 | struct Pointer : | |
61933e16 | 331 | CYValue |
ff783f8c JF |
332 | { |
333 | JSObjectRef owner_; | |
334 | Type_privateData *type_; | |
335 | ||
336 | Pointer(void *value, sig::Type *type, JSObjectRef owner) : | |
61933e16 | 337 | CYValue(value), |
ff783f8c JF |
338 | owner_(owner), |
339 | type_(new(pool_) Type_privateData(pool_, type)) | |
340 | { | |
bd17e6f3 JF |
341 | } |
342 | }; | |
343 | ||
344 | struct Struct_privateData : | |
61933e16 | 345 | CYValue |
bd17e6f3 JF |
346 | { |
347 | JSObjectRef owner_; | |
348 | Type_privateData *type_; | |
349 | ||
ff783f8c JF |
350 | Struct_privateData(JSObjectRef owner) : |
351 | owner_(owner) | |
352 | { | |
bd17e6f3 JF |
353 | } |
354 | }; | |
355 | ||
bd17e6f3 JF |
356 | typedef std::map<const char *, Type_privateData *, CStringMapLess> TypeMap; |
357 | static TypeMap Types_; | |
358 | ||
359 | JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef owner) { | |
ff783f8c | 360 | Struct_privateData *internal(new Struct_privateData(owner)); |
bd17e6f3 JF |
361 | apr_pool_t *pool(internal->pool_); |
362 | Type_privateData *typical(new(pool) Type_privateData(pool, type, ffi)); | |
363 | internal->type_ = typical; | |
364 | ||
ff783f8c | 365 | if (owner != NULL) |
bd17e6f3 | 366 | internal->value_ = data; |
ff783f8c JF |
367 | else { |
368 | size_t size(typical->GetFFI()->size); | |
bd17e6f3 JF |
369 | void *copy(apr_palloc(internal->pool_, size)); |
370 | memcpy(copy, data, size); | |
371 | internal->value_ = copy; | |
372 | } | |
373 | ||
bd17e6f3 JF |
374 | return JSObjectMake(context, Struct_, internal); |
375 | } | |
376 | ||
f7c38a29 | 377 | void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type *&type) { |
d63c39cc JF |
378 | if (name == NULL) |
379 | return; | |
380 | ||
f33b048a | 381 | CYPoolTry { |
61933e16 | 382 | if (NSMutableArray *entry = [[Bridge_ objectAtIndex:2] objectForKey:[NSString stringWithUTF8String:name]]) |
f33b048a | 383 | switch ([[entry objectAtIndex:0] intValue]) { |
61933e16 JF |
384 | case 0: { |
385 | sig::Parse(pool, &type->data.signature, [[entry objectAtIndex:1] UTF8String], &Structor_); | |
386 | } break; | |
387 | ||
388 | case 1: { | |
389 | sig::Signature signature; | |
390 | sig::Parse(pool, &signature, [[entry objectAtIndex:1] UTF8String], &Structor_); | |
391 | type = signature.elements[0].type; | |
392 | } break; | |
f33b048a | 393 | } |
f33b048a JF |
394 | } CYPoolCatch() |
395 | } | |
396 | ||
397 | struct Functor_privateData : | |
61933e16 | 398 | CYValue |
f33b048a JF |
399 | { |
400 | sig::Signature signature_; | |
401 | ffi_cif cif_; | |
402 | ||
403 | Functor_privateData(const char *type, void (*value)()) : | |
61933e16 | 404 | CYValue(reinterpret_cast<void *>(value)) |
f33b048a JF |
405 | { |
406 | sig::Parse(pool_, &signature_, type, &Structor_); | |
407 | sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_); | |
408 | } | |
409 | }; | |
410 | ||
411 | struct ffoData : | |
412 | Functor_privateData | |
413 | { | |
414 | JSContextRef context_; | |
415 | JSObjectRef function_; | |
416 | ||
417 | ffoData(const char *type) : | |
418 | Functor_privateData(type, NULL) | |
419 | { | |
420 | } | |
421 | }; | |
422 | ||
f7c38a29 | 423 | JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) { |
2b52f27e JF |
424 | Instance::Flags flags; |
425 | ||
426 | if (transient) | |
427 | flags = Instance::Transient; | |
428 | else { | |
429 | flags = Instance::None; | |
478d4ed0 | 430 | object = [object retain]; |
2b52f27e JF |
431 | } |
432 | ||
433 | return Instance::Make(context, object, flags); | |
478d4ed0 JF |
434 | } |
435 | ||
436 | const char *CYPoolCString(apr_pool_t *pool, NSString *value) { | |
437 | if (pool == NULL) | |
438 | return [value UTF8String]; | |
439 | else { | |
440 | size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1); | |
441 | char *string(new(pool) char[size]); | |
442 | if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding]) | |
4afefdd9 | 443 | @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"[NSString getCString:maxLength:encoding:] == NO" userInfo:nil]; |
478d4ed0 JF |
444 | return string; |
445 | } | |
b09da87b JF |
446 | } |
447 | ||
448 | JSValueRef CYCastJSValue(JSContextRef context, bool value) { | |
449 | return JSValueMakeBoolean(context, value); | |
450 | } | |
451 | ||
452 | JSValueRef CYCastJSValue(JSContextRef context, double value) { | |
453 | return JSValueMakeNumber(context, value); | |
454 | } | |
455 | ||
456 | #define CYCastJSValue_(Type_) \ | |
457 | JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \ | |
458 | return JSValueMakeNumber(context, static_cast<double>(value)); \ | |
459 | } | |
460 | ||
461 | CYCastJSValue_(int) | |
462 | CYCastJSValue_(unsigned int) | |
463 | CYCastJSValue_(long int) | |
464 | CYCastJSValue_(long unsigned int) | |
465 | CYCastJSValue_(long long int) | |
466 | CYCastJSValue_(long long unsigned int) | |
467 | ||
468 | JSValueRef CYJSUndefined(JSContextRef context) { | |
469 | return JSValueMakeUndefined(context); | |
0c862573 JF |
470 | } |
471 | ||
ff783f8c JF |
472 | bool CYGetIndex(const char *value, ssize_t &index) { |
473 | if (value[0] != '0') { | |
283e7e33 | 474 | char *end; |
ff783f8c | 475 | index = strtol(value, &end, 10); |
283e7e33 | 476 | if (value + strlen(value) == end) |
ff783f8c JF |
477 | return true; |
478 | } else if (value[1] == '\0') { | |
479 | index = 0; | |
480 | return true; | |
283e7e33 JF |
481 | } |
482 | ||
ff783f8c | 483 | return false; |
283e7e33 JF |
484 | } |
485 | ||
ff783f8c JF |
486 | bool CYGetIndex(apr_pool_t *pool, NSString *value, ssize_t &index) { |
487 | return CYGetIndex(CYPoolCString(pool, value), index); | |
283e7e33 JF |
488 | } |
489 | ||
107e3ed0 | 490 | @interface NSMethodSignature (Cycript) |
7ba62cfd JF |
491 | - (NSString *) _typeString; |
492 | @end | |
493 | ||
107e3ed0 | 494 | @interface NSObject (Cycript) |
b4aa79af | 495 | |
61933e16 | 496 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context; |
b4aa79af JF |
497 | - (JSType) cy$JSType; |
498 | ||
499 | - (NSObject *) cy$toJSON:(NSString *)key; | |
500 | - (NSString *) cy$toCYON; | |
f33b048a | 501 | - (NSString *) cy$toKey; |
b4aa79af | 502 | |
cc103044 JF |
503 | - (NSObject *) cy$getProperty:(NSString *)name; |
504 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value; | |
505 | - (bool) cy$deleteProperty:(NSString *)name; | |
b4aa79af | 506 | |
c1582939 JF |
507 | @end |
508 | ||
f7c38a29 JF |
509 | @protocol Cycript |
510 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context; | |
511 | @end | |
512 | ||
107e3ed0 | 513 | @interface NSString (Cycript) |
88c977fa JF |
514 | - (void *) cy$symbol; |
515 | @end | |
516 | ||
4afefdd9 JF |
517 | struct PropertyAttributes { |
518 | CYPool pool_; | |
519 | ||
520 | const char *name; | |
521 | ||
522 | const char *variable; | |
523 | ||
524 | const char *getter_; | |
525 | const char *setter_; | |
526 | ||
527 | bool readonly; | |
528 | bool copy; | |
529 | bool retain; | |
530 | bool nonatomic; | |
531 | bool dynamic; | |
532 | bool weak; | |
533 | bool garbage; | |
534 | ||
535 | PropertyAttributes(objc_property_t property) : | |
536 | variable(NULL), | |
537 | getter_(NULL), | |
538 | setter_(NULL), | |
539 | readonly(false), | |
540 | copy(false), | |
541 | retain(false), | |
542 | nonatomic(false), | |
543 | dynamic(false), | |
544 | weak(false), | |
545 | garbage(false) | |
546 | { | |
547 | name = property_getName(property); | |
548 | const char *attributes(property_getAttributes(property)); | |
549 | ||
550 | for (char *state, *token(apr_strtok(apr_pstrdup(pool_, attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) { | |
551 | switch (*token) { | |
552 | case 'R': readonly = true; break; | |
553 | case 'C': copy = true; break; | |
554 | case '&': retain = true; break; | |
555 | case 'N': nonatomic = true; break; | |
556 | case 'G': getter_ = token + 1; break; | |
557 | case 'S': setter_ = token + 1; break; | |
558 | case 'V': variable = token + 1; break; | |
559 | } | |
560 | } | |
561 | ||
562 | /*if (variable == NULL) { | |
563 | variable = property_getName(property); | |
564 | size_t size(strlen(variable)); | |
565 | char *name(new(pool_) char[size + 2]); | |
566 | name[0] = '_'; | |
567 | memcpy(name + 1, variable, size); | |
568 | name[size + 1] = '\0'; | |
569 | variable = name; | |
570 | }*/ | |
571 | } | |
572 | ||
573 | const char *Getter() { | |
574 | if (getter_ == NULL) | |
575 | getter_ = apr_pstrdup(pool_, name); | |
576 | return getter_; | |
577 | } | |
578 | ||
579 | const char *Setter() { | |
580 | if (setter_ == NULL && !readonly) { | |
581 | size_t length(strlen(name)); | |
582 | ||
583 | char *temp(new(pool_) char[length + 5]); | |
584 | temp[0] = 's'; | |
585 | temp[1] = 'e'; | |
586 | temp[2] = 't'; | |
587 | ||
588 | if (length != 0) { | |
589 | temp[3] = toupper(name[0]); | |
590 | memcpy(temp + 4, name + 1, length - 1); | |
591 | } | |
592 | ||
593 | temp[length + 3] = ':'; | |
594 | temp[length + 4] = '\0'; | |
595 | setter_ = temp; | |
596 | } | |
597 | ||
598 | return setter_; | |
599 | } | |
600 | ||
601 | }; | |
602 | ||
107e3ed0 | 603 | @implementation NSObject (Cycript) |
62ca2b82 | 604 | |
61933e16 JF |
605 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { |
606 | return CYMakeInstance(context, self, false); | |
607 | } | |
608 | ||
b4aa79af JF |
609 | - (JSType) cy$JSType { |
610 | return kJSTypeObject; | |
6b8a9500 JF |
611 | } |
612 | ||
b4aa79af | 613 | - (NSObject *) cy$toJSON:(NSString *)key { |
62ca2b82 JF |
614 | return [self description]; |
615 | } | |
616 | ||
b4aa79af JF |
617 | - (NSString *) cy$toCYON { |
618 | return [[self cy$toJSON:@""] cy$toCYON]; | |
619 | } | |
620 | ||
f33b048a JF |
621 | - (NSString *) cy$toKey { |
622 | return [self cy$toCYON]; | |
623 | } | |
624 | ||
cc103044 | 625 | - (NSObject *) cy$getProperty:(NSString *)name { |
4afefdd9 JF |
626 | /*if (![name isEqualToString:@"prototype"]) |
627 | NSLog(@"get:%@", name);*/ | |
cc103044 JF |
628 | return nil; |
629 | } | |
630 | ||
631 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
4afefdd9 | 632 | //NSLog(@"set:%@", name); |
cc103044 JF |
633 | return false; |
634 | } | |
635 | ||
636 | - (bool) cy$deleteProperty:(NSString *)name { | |
4afefdd9 | 637 | //NSLog(@"delete:%@", name); |
cc103044 JF |
638 | return false; |
639 | } | |
640 | ||
62ca2b82 | 641 | @end |
c1582939 | 642 | |
107e3ed0 | 643 | @implementation WebUndefined (Cycript) |
62ca2b82 | 644 | |
b4aa79af JF |
645 | - (JSType) cy$JSType { |
646 | return kJSTypeUndefined; | |
647 | } | |
648 | ||
649 | - (NSObject *) cy$toJSON:(NSString *)key { | |
650 | return self; | |
6b8a9500 JF |
651 | } |
652 | ||
b4aa79af | 653 | - (NSString *) cy$toCYON { |
c1582939 | 654 | return @"undefined"; |
62ca2b82 JF |
655 | } |
656 | ||
2b52f27e | 657 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { |
b09da87b | 658 | return CYJSUndefined(context); |
62ca2b82 JF |
659 | } |
660 | ||
661 | @end | |
c1582939 | 662 | |
478d4ed0 JF |
663 | @implementation NSNull (Cycript) |
664 | ||
b4aa79af JF |
665 | - (JSType) cy$JSType { |
666 | return kJSTypeNull; | |
667 | } | |
668 | ||
669 | - (NSObject *) cy$toJSON:(NSString *)key { | |
670 | return self; | |
671 | } | |
672 | ||
673 | - (NSString *) cy$toCYON { | |
478d4ed0 JF |
674 | return @"null"; |
675 | } | |
676 | ||
677 | @end | |
678 | ||
107e3ed0 | 679 | @implementation NSArray (Cycript) |
62ca2b82 | 680 | |
b4aa79af | 681 | - (NSString *) cy$toCYON { |
c1582939 JF |
682 | NSMutableString *json([[[NSMutableString alloc] init] autorelease]); |
683 | [json appendString:@"["]; | |
684 | ||
685 | bool comma(false); | |
62ca2b82 | 686 | for (id object in self) { |
c1582939 JF |
687 | if (comma) |
688 | [json appendString:@","]; | |
689 | else | |
690 | comma = true; | |
b4aa79af JF |
691 | if ([object cy$JSType] != kJSTypeUndefined) |
692 | [json appendString:[object cy$toCYON]]; | |
6b8a9500 JF |
693 | else { |
694 | [json appendString:@","]; | |
695 | comma = false; | |
696 | } | |
c1582939 JF |
697 | } |
698 | ||
699 | [json appendString:@"]"]; | |
700 | return json; | |
62ca2b82 JF |
701 | } |
702 | ||
cc103044 | 703 | - (NSObject *) cy$getProperty:(NSString *)name { |
4afefdd9 JF |
704 | if ([name isEqualToString:@"length"]) |
705 | return [NSNumber numberWithUnsignedInteger:[self count]]; | |
706 | ||
ff783f8c JF |
707 | ssize_t index; |
708 | if (!CYGetIndex(NULL, name, index) || index < 0 || index >= static_cast<ssize_t>([self count])) | |
cc103044 JF |
709 | return [super cy$getProperty:name]; |
710 | else | |
711 | return [self objectAtIndex:index]; | |
712 | } | |
713 | ||
714 | @end | |
715 | ||
716 | @implementation NSMutableArray (Cycript) | |
717 | ||
718 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
ff783f8c JF |
719 | ssize_t index; |
720 | if (!CYGetIndex(NULL, name, index) || index < 0 || index >= static_cast<ssize_t>([self count])) | |
cc103044 JF |
721 | return [super cy$setProperty:name to:value]; |
722 | else { | |
b6ea08b6 | 723 | [self replaceObjectAtIndex:index withObject:(value ?: [NSNull null])]; |
cc103044 JF |
724 | return true; |
725 | } | |
726 | } | |
727 | ||
728 | - (bool) cy$deleteProperty:(NSString *)name { | |
ff783f8c JF |
729 | ssize_t index; |
730 | if (!CYGetIndex(NULL, name, index) || index < 0 || index >= static_cast<ssize_t>([self count])) | |
cc103044 JF |
731 | return [super cy$deleteProperty:name]; |
732 | else { | |
733 | [self removeObjectAtIndex:index]; | |
734 | return true; | |
735 | } | |
736 | } | |
737 | ||
62ca2b82 JF |
738 | @end |
739 | ||
107e3ed0 | 740 | @implementation NSDictionary (Cycript) |
62ca2b82 | 741 | |
b4aa79af | 742 | - (NSString *) cy$toCYON { |
62ca2b82 | 743 | NSMutableString *json([[[NSMutableString alloc] init] autorelease]); |
f33b048a | 744 | [json appendString:@"{"]; |
62ca2b82 JF |
745 | |
746 | bool comma(false); | |
747 | for (id key in self) { | |
748 | if (comma) | |
749 | [json appendString:@","]; | |
750 | else | |
751 | comma = true; | |
f33b048a | 752 | [json appendString:[key cy$toKey]]; |
62ca2b82 JF |
753 | [json appendString:@":"]; |
754 | NSObject *object([self objectForKey:key]); | |
b4aa79af | 755 | [json appendString:[object cy$toCYON]]; |
62ca2b82 JF |
756 | } |
757 | ||
f33b048a | 758 | [json appendString:@"}"]; |
62ca2b82 JF |
759 | return json; |
760 | } | |
761 | ||
cc103044 JF |
762 | - (NSObject *) cy$getProperty:(NSString *)name { |
763 | return [self objectForKey:name]; | |
764 | } | |
765 | ||
766 | @end | |
767 | ||
768 | @implementation NSMutableDictionary (Cycript) | |
769 | ||
770 | - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value { | |
b6ea08b6 | 771 | [self setObject:(value ?: [NSNull null]) forKey:name]; |
cc103044 JF |
772 | return true; |
773 | } | |
774 | ||
775 | - (bool) cy$deleteProperty:(NSString *)name { | |
776 | if ([self objectForKey:name] == nil) | |
777 | return false; | |
778 | else { | |
779 | [self removeObjectForKey:name]; | |
780 | return true; | |
781 | } | |
782 | } | |
783 | ||
62ca2b82 | 784 | @end |
c1582939 | 785 | |
107e3ed0 | 786 | @implementation NSNumber (Cycript) |
62ca2b82 | 787 | |
b4aa79af JF |
788 | - (JSType) cy$JSType { |
789 | // XXX: this just seems stupid | |
790 | return [self class] == NSCFBoolean_ ? kJSTypeBoolean : kJSTypeNumber; | |
791 | } | |
792 | ||
793 | - (NSObject *) cy$toJSON:(NSString *)key { | |
794 | return self; | |
795 | } | |
796 | ||
797 | - (NSString *) cy$toCYON { | |
798 | return [self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false"; | |
62ca2b82 JF |
799 | } |
800 | ||
2b52f27e | 801 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { |
b4aa79af | 802 | return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]); |
62ca2b82 JF |
803 | } |
804 | ||
805 | @end | |
c1582939 | 806 | |
107e3ed0 | 807 | @implementation NSString (Cycript) |
62ca2b82 | 808 | |
b4aa79af JF |
809 | - (JSType) cy$JSType { |
810 | return kJSTypeString; | |
811 | } | |
812 | ||
813 | - (NSObject *) cy$toJSON:(NSString *)key { | |
814 | return self; | |
815 | } | |
816 | ||
817 | - (NSString *) cy$toCYON { | |
f33b048a | 818 | // XXX: this should use the better code from Output.cpp |
c1582939 JF |
819 | CFMutableStringRef json(CFStringCreateMutableCopy(kCFAllocatorDefault, 0, (CFStringRef) self)); |
820 | ||
c1582939 JF |
821 | CFStringFindAndReplace(json, CFSTR("\\"), CFSTR("\\\\"), CFRangeMake(0, CFStringGetLength(json)), 0); |
822 | CFStringFindAndReplace(json, CFSTR("\""), CFSTR("\\\""), CFRangeMake(0, CFStringGetLength(json)), 0); | |
823 | CFStringFindAndReplace(json, CFSTR("\t"), CFSTR("\\t"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
824 | CFStringFindAndReplace(json, CFSTR("\r"), CFSTR("\\r"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
825 | CFStringFindAndReplace(json, CFSTR("\n"), CFSTR("\\n"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
826 | ||
827 | CFStringInsert(json, 0, CFSTR("\"")); | |
828 | CFStringAppend(json, CFSTR("\"")); | |
829 | ||
62ca2b82 JF |
830 | return [reinterpret_cast<const NSString *>(json) autorelease]; |
831 | } | |
832 | ||
f33b048a JF |
833 | - (NSString *) cy$toKey { |
834 | const char *value([self UTF8String]); | |
835 | size_t size(strlen(value)); | |
836 | ||
283e7e33 | 837 | if (size == 0) |
f33b048a | 838 | goto cyon; |
283e7e33 JF |
839 | |
840 | if (DigitRange_[value[0]]) { | |
ff783f8c JF |
841 | ssize_t index; |
842 | if (!CYGetIndex(NULL, self, index) || index < 0) | |
f33b048a | 843 | goto cyon; |
283e7e33 JF |
844 | } else { |
845 | if (!WordStartRange_[value[0]]) | |
846 | goto cyon; | |
847 | for (size_t i(1); i != size; ++i) | |
848 | if (!WordEndRange_[value[i]]) | |
849 | goto cyon; | |
850 | } | |
851 | ||
f33b048a JF |
852 | return self; |
853 | ||
854 | cyon: | |
855 | return [self cy$toCYON]; | |
856 | } | |
857 | ||
88c977fa | 858 | - (void *) cy$symbol { |
478d4ed0 JF |
859 | CYPool pool; |
860 | return dlsym(RTLD_DEFAULT, CYPoolCString(pool, self)); | |
88c977fa JF |
861 | } |
862 | ||
62ca2b82 JF |
863 | @end |
864 | ||
b21525c7 | 865 | @interface CYJSObject : NSDictionary { |
62ca2b82 JF |
866 | JSObjectRef object_; |
867 | JSContextRef context_; | |
868 | } | |
869 | ||
870 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; | |
871 | ||
b4aa79af JF |
872 | - (NSString *) cy$toJSON:(NSString *)key; |
873 | ||
62ca2b82 JF |
874 | - (NSUInteger) count; |
875 | - (id) objectForKey:(id)key; | |
876 | - (NSEnumerator *) keyEnumerator; | |
877 | - (void) setObject:(id)object forKey:(id)key; | |
878 | - (void) removeObjectForKey:(id)key; | |
879 | ||
880 | @end | |
c1582939 | 881 | |
b21525c7 | 882 | @interface CYJSArray : NSArray { |
c1582939 JF |
883 | JSObjectRef object_; |
884 | JSContextRef context_; | |
885 | } | |
886 | ||
887 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; | |
888 | ||
889 | - (NSUInteger) count; | |
890 | - (id) objectAtIndex:(NSUInteger)index; | |
891 | ||
892 | @end | |
893 | ||
283e7e33 JF |
894 | CYRange DigitRange_ (0x3ff000000000000LLU, 0x000000000000000LLU); // 0-9 |
895 | CYRange WordStartRange_(0x000001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$ | |
896 | CYRange WordEndRange_ (0x3ff001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$0-9 | |
dea834b0 | 897 | |
478d4ed0 | 898 | JSGlobalContextRef CYGetJSContext() { |
ea2d184c | 899 | return Context_; |
62ca2b82 JF |
900 | } |
901 | ||
4cf49641 JF |
902 | #define CYTry \ |
903 | @try | |
0c862573 JF |
904 | #define CYCatch \ |
905 | @catch (id error) { \ | |
906 | CYThrow(context, error, exception); \ | |
907 | return NULL; \ | |
908 | } | |
909 | ||
ea2d184c JF |
910 | void CYThrow(JSContextRef context, JSValueRef value); |
911 | ||
b09da87b JF |
912 | apr_status_t CYPoolRelease_(void *data) { |
913 | id object(reinterpret_cast<id>(data)); | |
914 | [object release]; | |
915 | return APR_SUCCESS; | |
916 | } | |
917 | ||
918 | id CYPoolRelease(apr_pool_t *pool, id object) { | |
b4aa79af JF |
919 | if (object == nil) |
920 | return nil; | |
921 | else if (pool == NULL) | |
b09da87b JF |
922 | return [object autorelease]; |
923 | else { | |
924 | apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null); | |
925 | return object; | |
926 | } | |
927 | } | |
928 | ||
953647c1 JF |
929 | CFTypeRef CYPoolRelease(apr_pool_t *pool, CFTypeRef object) { |
930 | return (CFTypeRef) CYPoolRelease(pool, (id) object); | |
931 | } | |
932 | ||
2b52f27e | 933 | id CYCastNSObject_(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { |
ea2d184c JF |
934 | JSValueRef exception(NULL); |
935 | bool array(JSValueIsInstanceOfConstructor(context, object, Array_, &exception)); | |
936 | CYThrow(context, exception); | |
b09da87b JF |
937 | id value(array ? [CYJSArray alloc] : [CYJSObject alloc]); |
938 | return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]); | |
62ca2b82 JF |
939 | } |
940 | ||
2b52f27e JF |
941 | id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { |
942 | if (!JSValueIsObjectOfClass(context, object, Instance_)) | |
943 | return CYCastNSObject_(pool, context, object); | |
944 | else { | |
945 | Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); | |
946 | return data->GetValue(); | |
947 | } | |
948 | } | |
949 | ||
77e87a6c | 950 | JSStringRef CYCopyJSString(id value) { |
b09da87b | 951 | return value == NULL ? NULL : JSStringCreateWithCFString(reinterpret_cast<CFStringRef>([value description])); |
62ca2b82 JF |
952 | } |
953 | ||
77e87a6c | 954 | JSStringRef CYCopyJSString(const char *value) { |
b09da87b | 955 | return value == NULL ? NULL : JSStringCreateWithUTF8CString(value); |
77e87a6c JF |
956 | } |
957 | ||
958 | JSStringRef CYCopyJSString(JSStringRef value) { | |
b09da87b | 959 | return value == NULL ? NULL : JSStringRetain(value); |
77e87a6c JF |
960 | } |
961 | ||
962 | JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value) { | |
b09da87b JF |
963 | if (JSValueIsNull(context, value)) |
964 | return NULL; | |
62ca2b82 | 965 | JSValueRef exception(NULL); |
ea2d184c JF |
966 | JSStringRef string(JSValueToStringCopy(context, value, &exception)); |
967 | CYThrow(context, exception); | |
77e87a6c JF |
968 | return string; |
969 | } | |
970 | ||
cf7d4c69 | 971 | class CYJSString { |
77e87a6c JF |
972 | private: |
973 | JSStringRef string_; | |
974 | ||
b09da87b | 975 | void Clear_() { |
283e7e33 JF |
976 | if (string_ != NULL) |
977 | JSStringRelease(string_); | |
b09da87b JF |
978 | } |
979 | ||
77e87a6c | 980 | public: |
b09da87b JF |
981 | CYJSString(const CYJSString &rhs) : |
982 | string_(CYCopyJSString(rhs.string_)) | |
983 | { | |
984 | } | |
985 | ||
77e87a6c | 986 | template <typename Arg0_> |
b09da87b JF |
987 | CYJSString(Arg0_ arg0) : |
988 | string_(CYCopyJSString(arg0)) | |
989 | { | |
77e87a6c JF |
990 | } |
991 | ||
992 | template <typename Arg0_, typename Arg1_> | |
b09da87b JF |
993 | CYJSString(Arg0_ arg0, Arg1_ arg1) : |
994 | string_(CYCopyJSString(arg0, arg1)) | |
995 | { | |
996 | } | |
997 | ||
998 | CYJSString &operator =(const CYJSString &rhs) { | |
999 | Clear_(); | |
1000 | string_ = CYCopyJSString(rhs.string_); | |
1001 | return *this; | |
77e87a6c JF |
1002 | } |
1003 | ||
cf7d4c69 | 1004 | ~CYJSString() { |
b09da87b JF |
1005 | Clear_(); |
1006 | } | |
1007 | ||
1008 | void Clear() { | |
1009 | Clear_(); | |
1010 | string_ = NULL; | |
77e87a6c JF |
1011 | } |
1012 | ||
1013 | operator JSStringRef() const { | |
1014 | return string_; | |
1015 | } | |
1016 | }; | |
1017 | ||
1018 | CFStringRef CYCopyCFString(JSStringRef value) { | |
1019 | return JSStringCopyCFString(kCFAllocatorDefault, value); | |
1020 | } | |
1021 | ||
1022 | CFStringRef CYCopyCFString(JSContextRef context, JSValueRef value) { | |
cf7d4c69 | 1023 | return CYCopyCFString(CYJSString(context, value)); |
c1582939 JF |
1024 | } |
1025 | ||
bd17e6f3 JF |
1026 | double CYCastDouble(const char *value, size_t size) { |
1027 | char *end; | |
1028 | double number(strtod(value, &end)); | |
1029 | if (end != value + size) | |
1030 | return NAN; | |
1031 | return number; | |
1032 | } | |
1033 | ||
1034 | double CYCastDouble(const char *value) { | |
1035 | return CYCastDouble(value, strlen(value)); | |
1036 | } | |
1037 | ||
f610e1a0 | 1038 | double CYCastDouble(JSContextRef context, JSValueRef value) { |
0c862573 JF |
1039 | JSValueRef exception(NULL); |
1040 | double number(JSValueToNumber(context, value, &exception)); | |
1041 | CYThrow(context, exception); | |
f610e1a0 JF |
1042 | return number; |
1043 | } | |
1044 | ||
1045 | CFNumberRef CYCopyCFNumber(JSContextRef context, JSValueRef value) { | |
1046 | double number(CYCastDouble(context, value)); | |
0c862573 JF |
1047 | return CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &number); |
1048 | } | |
1049 | ||
953647c1 JF |
1050 | CFStringRef CYCopyCFString(const char *value) { |
1051 | return CFStringCreateWithCString(kCFAllocatorDefault, value, kCFStringEncodingUTF8); | |
1052 | } | |
1053 | ||
1054 | NSString *CYCastNSString(apr_pool_t *pool, const char *value) { | |
1055 | return (NSString *) CYPoolRelease(pool, CYCopyCFString(value)); | |
1056 | } | |
1057 | ||
b09da87b | 1058 | NSString *CYCastNSString(apr_pool_t *pool, JSStringRef value) { |
953647c1 | 1059 | return (NSString *) CYPoolRelease(pool, CYCopyCFString(value)); |
b09da87b JF |
1060 | } |
1061 | ||
1062 | bool CYCastBool(JSContextRef context, JSValueRef value) { | |
1063 | return JSValueToBoolean(context, value); | |
62ca2b82 JF |
1064 | } |
1065 | ||
b09da87b JF |
1066 | CFTypeRef CYCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) { |
1067 | CFTypeRef object; | |
1068 | bool copy; | |
1069 | ||
f610e1a0 | 1070 | switch (JSType type = JSValueGetType(context, value)) { |
c1582939 | 1071 | case kJSTypeUndefined: |
b09da87b JF |
1072 | object = [WebUndefined undefined]; |
1073 | copy = false; | |
1074 | break; | |
1075 | ||
c1582939 | 1076 | case kJSTypeNull: |
b09da87b JF |
1077 | return NULL; |
1078 | break; | |
1079 | ||
c1582939 | 1080 | case kJSTypeBoolean: |
b09da87b JF |
1081 | object = CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse; |
1082 | copy = false; | |
1083 | break; | |
1084 | ||
0c862573 | 1085 | case kJSTypeNumber: |
b09da87b JF |
1086 | object = CYCopyCFNumber(context, value); |
1087 | copy = true; | |
1088 | break; | |
1089 | ||
62ca2b82 | 1090 | case kJSTypeString: |
b09da87b JF |
1091 | object = CYCopyCFString(context, value); |
1092 | copy = true; | |
1093 | break; | |
1094 | ||
c1582939 | 1095 | case kJSTypeObject: |
b09da87b JF |
1096 | // XXX: this might could be more efficient |
1097 | object = (CFTypeRef) CYCastNSObject(pool, context, (JSObjectRef) value); | |
1098 | copy = false; | |
1099 | break; | |
1100 | ||
c1582939 | 1101 | default: |
f5e9be24 | 1102 | @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"JSValueGetType() == 0x%x", type] userInfo:nil]; |
b09da87b | 1103 | break; |
c1582939 | 1104 | } |
b09da87b JF |
1105 | |
1106 | if (cast != copy) | |
1107 | return object; | |
1108 | else if (copy) | |
953647c1 | 1109 | return CYPoolRelease(pool, object); |
b09da87b JF |
1110 | else |
1111 | return CFRetain(object); | |
1112 | } | |
1113 | ||
1114 | CFTypeRef CYCastCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
1115 | return CYCFType(pool, context, value, true); | |
1116 | } | |
1117 | ||
1118 | CFTypeRef CYCopyCFType(apr_pool_t *pool, JSContextRef context, JSValueRef value) { | |
1119 | return CYCFType(pool, context, value, false); | |
c1582939 JF |
1120 | } |
1121 | ||
62ca2b82 | 1122 | NSArray *CYCastNSArray(JSPropertyNameArrayRef names) { |
b09da87b | 1123 | CYPool pool; |
62ca2b82 JF |
1124 | size_t size(JSPropertyNameArrayGetCount(names)); |
1125 | NSMutableArray *array([NSMutableArray arrayWithCapacity:size]); | |
1126 | for (size_t index(0); index != size; ++index) | |
b09da87b | 1127 | [array addObject:CYCastNSString(pool, JSPropertyNameArrayGetNameAtIndex(names, index))]; |
62ca2b82 JF |
1128 | return array; |
1129 | } | |
1130 | ||
b09da87b JF |
1131 | id CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) { |
1132 | return reinterpret_cast<const NSObject *>(CYCastCFType(pool, context, value)); | |
c1582939 JF |
1133 | } |
1134 | ||
ea2d184c | 1135 | void CYThrow(JSContextRef context, JSValueRef value) { |
62ca2b82 JF |
1136 | if (value == NULL) |
1137 | return; | |
b09da87b JF |
1138 | @throw CYCastNSObject(NULL, context, value); |
1139 | } | |
1140 | ||
1141 | JSValueRef CYJSNull(JSContextRef context) { | |
1142 | return JSValueMakeNull(context); | |
1143 | } | |
1144 | ||
1145 | JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value) { | |
1146 | return value == NULL ? CYJSNull(context) : JSValueMakeString(context, value); | |
1147 | } | |
1148 | ||
1149 | JSValueRef CYCastJSValue(JSContextRef context, const char *value) { | |
1150 | return CYCastJSValue(context, CYJSString(value)); | |
62ca2b82 JF |
1151 | } |
1152 | ||
2b52f27e | 1153 | JSValueRef CYCastJSValue(JSContextRef context, id value) { |
f7c38a29 JF |
1154 | if (value == nil) |
1155 | return CYJSNull(context); | |
1156 | else if ([value respondsToSelector:@selector(cy$JSValueInContext:)]) | |
1157 | return [value cy$JSValueInContext:context]; | |
1158 | else | |
1159 | return CYMakeInstance(context, value, false); | |
62ca2b82 JF |
1160 | } |
1161 | ||
dea834b0 JF |
1162 | JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) { |
1163 | JSValueRef exception(NULL); | |
1164 | JSObjectRef object(JSValueToObject(context, value, &exception)); | |
1165 | CYThrow(context, exception); | |
1166 | return object; | |
1167 | } | |
1168 | ||
bd17e6f3 JF |
1169 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index) { |
1170 | JSValueRef exception(NULL); | |
1171 | JSValueRef value(JSObjectGetPropertyAtIndex(context, object, index, &exception)); | |
1172 | CYThrow(context, exception); | |
1173 | return value; | |
1174 | } | |
1175 | ||
dea834b0 JF |
1176 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name) { |
1177 | JSValueRef exception(NULL); | |
1178 | JSValueRef value(JSObjectGetProperty(context, object, name, &exception)); | |
1179 | CYThrow(context, exception); | |
1180 | return value; | |
1181 | } | |
1182 | ||
1183 | void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value) { | |
1184 | JSValueRef exception(NULL); | |
1185 | JSObjectSetProperty(context, object, name, value, kJSPropertyAttributeNone, &exception); | |
1186 | CYThrow(context, exception); | |
1187 | } | |
1188 | ||
30ddc20c | 1189 | void CYThrow(JSContextRef context, id error, JSValueRef *exception) { |
4cf49641 JF |
1190 | if (exception == NULL) |
1191 | throw error; | |
7ba62cfd JF |
1192 | *exception = CYCastJSValue(context, error); |
1193 | } | |
1194 | ||
b4aa79af JF |
1195 | JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, JSValueRef arguments[]) { |
1196 | JSValueRef exception(NULL); | |
1197 | JSValueRef value(JSObjectCallAsFunction(context, function, _this, count, arguments, &exception)); | |
1198 | CYThrow(context, exception); | |
1199 | return value; | |
1200 | } | |
1201 | ||
1202 | bool CYIsCallable(JSContextRef context, JSValueRef value) { | |
1203 | // XXX: this isn't actually correct | |
1204 | return value != NULL && JSValueIsObject(context, value); | |
1205 | } | |
1206 | ||
b21525c7 | 1207 | @implementation CYJSObject |
62ca2b82 JF |
1208 | |
1209 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { | |
1210 | if ((self = [super init]) != nil) { | |
1211 | object_ = object; | |
1212 | context_ = context; | |
1213 | } return self; | |
1214 | } | |
1215 | ||
b4aa79af JF |
1216 | - (NSObject *) cy$toJSON:(NSString *)key { |
1217 | JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_)); | |
1218 | if (!CYIsCallable(context_, toJSON)) | |
1219 | return [super cy$toJSON:key]; | |
1220 | else { | |
1221 | JSValueRef arguments[1] = {CYCastJSValue(context_, key)}; | |
1222 | JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toJSON, object_, 1, arguments)); | |
1223 | // XXX: do I really want an NSNull here?! | |
1224 | return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; | |
1225 | } | |
1226 | } | |
1227 | ||
1228 | - (NSString *) cy$toCYON { | |
1229 | JSValueRef toCYON(CYGetProperty(context_, object_, toCYON_)); | |
1230 | if (!CYIsCallable(context_, toCYON)) | |
1231 | return [super cy$toCYON]; | |
1232 | else { | |
1233 | JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toCYON, object_, 0, NULL)); | |
1234 | return CYCastNSString(NULL, CYJSString(context_, value)); | |
1235 | } | |
1236 | } | |
1237 | ||
62ca2b82 JF |
1238 | - (NSUInteger) count { |
1239 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); | |
1240 | size_t size(JSPropertyNameArrayGetCount(names)); | |
1241 | JSPropertyNameArrayRelease(names); | |
1242 | return size; | |
1243 | } | |
1244 | ||
1245 | - (id) objectForKey:(id)key { | |
478d4ed0 | 1246 | return CYCastNSObject(NULL, context_, CYGetProperty(context_, object_, CYJSString(key))) ?: [NSNull null]; |
62ca2b82 JF |
1247 | } |
1248 | ||
1249 | - (NSEnumerator *) keyEnumerator { | |
1250 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); | |
1251 | NSEnumerator *enumerator([CYCastNSArray(names) objectEnumerator]); | |
1252 | JSPropertyNameArrayRelease(names); | |
1253 | return enumerator; | |
1254 | } | |
1255 | ||
1256 | - (void) setObject:(id)object forKey:(id)key { | |
dea834b0 | 1257 | CYSetProperty(context_, object_, CYJSString(key), CYCastJSValue(context_, object)); |
62ca2b82 JF |
1258 | } |
1259 | ||
1260 | - (void) removeObjectForKey:(id)key { | |
1261 | JSValueRef exception(NULL); | |
2b52f27e | 1262 | (void) JSObjectDeleteProperty(context_, object_, CYJSString(key), &exception); |
62ca2b82 JF |
1263 | CYThrow(context_, exception); |
1264 | } | |
1265 | ||
1266 | @end | |
1267 | ||
b21525c7 | 1268 | @implementation CYJSArray |
c1582939 JF |
1269 | |
1270 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { | |
1271 | if ((self = [super init]) != nil) { | |
1272 | object_ = object; | |
1273 | context_ = context; | |
1274 | } return self; | |
1275 | } | |
1276 | ||
1277 | - (NSUInteger) count { | |
dea834b0 | 1278 | return CYCastDouble(context_, CYGetProperty(context_, object_, length_)); |
c1582939 JF |
1279 | } |
1280 | ||
1281 | - (id) objectAtIndex:(NSUInteger)index { | |
62ca2b82 JF |
1282 | JSValueRef exception(NULL); |
1283 | JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception)); | |
1284 | CYThrow(context_, exception); | |
478d4ed0 | 1285 | return CYCastNSObject(NULL, context_, value) ?: [NSNull null]; |
c1582939 JF |
1286 | } |
1287 | ||
1288 | @end | |
1289 | ||
b4aa79af | 1290 | CFStringRef CYCopyCYONString(JSContextRef context, JSValueRef value, JSValueRef *exception) { |
4cf49641 JF |
1291 | CYTry { |
1292 | CYPoolTry { | |
b4aa79af JF |
1293 | id object(CYCastNSObject(NULL, context, value) ?: [NSNull null]); |
1294 | return reinterpret_cast<CFStringRef>([[object cy$toCYON] retain]); | |
4cf49641 JF |
1295 | } CYPoolCatch(NULL) |
1296 | } CYCatch | |
c1582939 JF |
1297 | } |
1298 | ||
b4aa79af JF |
1299 | const char *CYPoolCYONString(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception) { |
1300 | if (NSString *json = (NSString *) CYCopyCYONString(context, value, exception)) { | |
4cf49641 JF |
1301 | const char *string(CYPoolCString(pool, json)); |
1302 | [json release]; | |
1303 | return string; | |
1304 | } else return NULL; | |
478d4ed0 JF |
1305 | } |
1306 | ||
61933e16 JF |
1307 | struct CYInternal : |
1308 | CYData | |
1309 | { | |
1310 | JSObjectRef object_; | |
1311 | ||
1312 | CYInternal() : | |
1313 | object_(NULL) | |
1314 | { | |
1315 | } | |
1316 | ||
1317 | ~CYInternal() { | |
1318 | // XXX: delete object_? ;( | |
1319 | } | |
1320 | ||
1321 | static CYInternal *Get(id self) { | |
1322 | CYInternal *internal(NULL); | |
1323 | if (object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)) == NULL) { | |
1324 | // XXX: do something epic? ;P | |
1325 | } | |
1326 | ||
1327 | return internal; | |
1328 | } | |
1329 | ||
1330 | static CYInternal *Set(id self) { | |
1331 | CYInternal *internal(NULL); | |
1332 | if (Ivar ivar = object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal))) { | |
1333 | if (internal == NULL) { | |
1334 | internal = new CYInternal(); | |
1335 | object_setIvar(self, ivar, reinterpret_cast<id>(internal)); | |
1336 | } | |
1337 | } else { | |
1338 | // XXX: do something epic? ;P | |
1339 | } | |
1340 | ||
1341 | return internal; | |
1342 | } | |
1343 | ||
1344 | JSValueRef GetProperty(JSContextRef context, JSStringRef name) { | |
1345 | if (object_ == NULL) | |
1346 | return NULL; | |
1347 | return CYGetProperty(context, object_, name); | |
1348 | } | |
1349 | ||
1350 | void SetProperty(JSContextRef context, JSStringRef name, JSValueRef value) { | |
1351 | if (object_ == NULL) | |
1352 | object_ = JSObjectMake(context, NULL, NULL); | |
1353 | CYSetProperty(context, object_, name, value); | |
1354 | } | |
1355 | }; | |
1356 | ||
b09da87b | 1357 | static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
4afefdd9 JF |
1358 | CYPool pool; |
1359 | ||
4cf49641 | 1360 | CYTry { |
cc103044 | 1361 | NSString *self(CYCastNSObject(pool, context, object)); |
b09da87b | 1362 | NSString *name(CYCastNSString(pool, property)); |
4afefdd9 | 1363 | |
61933e16 JF |
1364 | if (CYInternal *internal = CYInternal::Get(self)) |
1365 | if (JSValueRef value = internal->GetProperty(context, property)) | |
1366 | return value; | |
1367 | ||
4afefdd9 JF |
1368 | CYPoolTry { |
1369 | if (NSObject *data = [self cy$getProperty:name]) | |
1370 | return CYCastJSValue(context, data); | |
1371 | } CYPoolCatch(NULL) | |
1372 | ||
1373 | if (objc_property_t property = class_getProperty(object_getClass(self), [name UTF8String])) { | |
1374 | PropertyAttributes attributes(property); | |
1375 | SEL sel(sel_registerName(attributes.Getter())); | |
2b52f27e | 1376 | return CYSendMessage(pool, context, self, sel, 0, NULL, false, exception); |
4afefdd9 JF |
1377 | } |
1378 | ||
1379 | return NULL; | |
f610e1a0 | 1380 | } CYCatch |
c1582939 JF |
1381 | } |
1382 | ||
b09da87b | 1383 | static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { |
4afefdd9 JF |
1384 | CYPool pool; |
1385 | ||
4cf49641 | 1386 | CYTry { |
cc103044 | 1387 | NSString *self(CYCastNSObject(pool, context, object)); |
b09da87b | 1388 | NSString *name(CYCastNSString(pool, property)); |
cc103044 | 1389 | NSString *data(CYCastNSObject(pool, context, value)); |
4afefdd9 JF |
1390 | |
1391 | CYPoolTry { | |
1392 | if ([self cy$setProperty:name to:data]) | |
1393 | return true; | |
1394 | } CYPoolCatch(NULL) | |
1395 | ||
1396 | if (objc_property_t property = class_getProperty(object_getClass(self), [name UTF8String])) { | |
1397 | PropertyAttributes attributes(property); | |
1398 | if (const char *setter = attributes.Setter()) { | |
1399 | SEL sel(sel_registerName(setter)); | |
1400 | JSValueRef arguments[1] = {value}; | |
2b52f27e | 1401 | CYSendMessage(pool, context, self, sel, 1, arguments, false, exception); |
4afefdd9 JF |
1402 | return true; |
1403 | } | |
1404 | } | |
1405 | ||
61933e16 JF |
1406 | if (CYInternal *internal = CYInternal::Set(self)) { |
1407 | internal->SetProperty(context, property, value); | |
1408 | return true; | |
1409 | } | |
1410 | ||
4afefdd9 | 1411 | return false; |
b09da87b JF |
1412 | } CYCatch |
1413 | } | |
1414 | ||
1415 | static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { | |
4cf49641 | 1416 | CYTry { |
4afefdd9 JF |
1417 | CYPoolTry { |
1418 | NSString *self(CYCastNSObject(NULL, context, object)); | |
1419 | NSString *name(CYCastNSString(NULL, property)); | |
1420 | return [self cy$deleteProperty:name]; | |
1421 | } CYPoolCatch(NULL) | |
b09da87b JF |
1422 | } CYCatch |
1423 | } | |
1424 | ||
b09da87b | 1425 | static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
4cf49641 | 1426 | CYTry { |
2b52f27e JF |
1427 | Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))); |
1428 | JSObjectRef value(Instance::Make(context, [data->GetValue() alloc], Instance::Uninitialized)); | |
1429 | return value; | |
0c862573 JF |
1430 | } CYCatch |
1431 | } | |
1432 | ||
dea834b0 | 1433 | JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) { |
953647c1 | 1434 | Selector_privateData *data(new Selector_privateData(sel)); |
dea834b0 JF |
1435 | return JSObjectMake(context, Selector_, data); |
1436 | } | |
1437 | ||
ff783f8c JF |
1438 | JSObjectRef CYMakePointer(JSContextRef context, void *pointer, sig::Type *type, JSObjectRef owner) { |
1439 | Pointer *data(new Pointer(pointer, type, owner)); | |
dea834b0 JF |
1440 | return JSObjectMake(context, Pointer_, data); |
1441 | } | |
1442 | ||
b09da87b | 1443 | JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) { |
953647c1 | 1444 | Functor_privateData *data(new Functor_privateData(type, function)); |
88c977fa JF |
1445 | return JSObjectMake(context, Functor_, data); |
1446 | } | |
1447 | ||
bd17e6f3 JF |
1448 | const char *CYPoolCString(apr_pool_t *pool, JSStringRef value, size_t *length = NULL) { |
1449 | if (pool == NULL) { | |
1450 | const char *string([CYCastNSString(NULL, value) UTF8String]); | |
1451 | if (length != NULL) | |
1452 | *length = strlen(string); | |
1453 | return string; | |
1454 | } else { | |
478d4ed0 JF |
1455 | size_t size(JSStringGetMaximumUTF8CStringSize(value)); |
1456 | char *string(new(pool) char[size]); | |
1457 | JSStringGetUTF8CString(value, string, size); | |
bd17e6f3 JF |
1458 | // XXX: this is ironic |
1459 | if (length != NULL) | |
1460 | *length = strlen(string); | |
478d4ed0 JF |
1461 | return string; |
1462 | } | |
7ba62cfd JF |
1463 | } |
1464 | ||
bd17e6f3 JF |
1465 | const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value, size_t *length = NULL) { |
1466 | if (!JSValueIsNull(context, value)) | |
1467 | return CYPoolCString(pool, CYJSString(context, value), length); | |
1468 | else { | |
1469 | if (length != NULL) | |
1470 | *length = 0; | |
b09da87b | 1471 | return NULL; |
bd17e6f3 | 1472 | } |
77e87a6c JF |
1473 | } |
1474 | ||
ff783f8c JF |
1475 | bool CYGetIndex(apr_pool_t *pool, JSStringRef value, ssize_t &index) { |
1476 | return CYGetIndex(CYPoolCString(pool, value), index); | |
1477 | } | |
1478 | ||
f610e1a0 | 1479 | // XXX: this macro is unhygenic |
b21525c7 | 1480 | #define CYCastCString(context, value) ({ \ |
b09da87b JF |
1481 | char *utf8; \ |
1482 | if (value == NULL) \ | |
1483 | utf8 = NULL; \ | |
478d4ed0 | 1484 | else if (JSStringRef string = CYCopyJSString(context, value)) { \ |
b09da87b JF |
1485 | size_t size(JSStringGetMaximumUTF8CStringSize(string)); \ |
1486 | utf8 = reinterpret_cast<char *>(alloca(size)); \ | |
1487 | JSStringGetUTF8CString(string, utf8, size); \ | |
1488 | JSStringRelease(string); \ | |
478d4ed0 JF |
1489 | } else \ |
1490 | utf8 = NULL; \ | |
b21525c7 JF |
1491 | utf8; \ |
1492 | }) | |
1493 | ||
b09da87b | 1494 | void *CYCastPointer_(JSContextRef context, JSValueRef value) { |
b21525c7 JF |
1495 | switch (JSValueGetType(context, value)) { |
1496 | case kJSTypeNull: | |
1497 | return NULL; | |
953647c1 | 1498 | /*case kJSTypeString: |
b21525c7 | 1499 | return dlsym(RTLD_DEFAULT, CYCastCString(context, value)); |
b21525c7 | 1500 | case kJSTypeObject: |
88c977fa | 1501 | if (JSValueIsObjectOfClass(context, value, Pointer_)) { |
ff783f8c | 1502 | Pointer *data(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value))); |
b21525c7 | 1503 | return data->value_; |
953647c1 | 1504 | }*/ |
b21525c7 | 1505 | default: |
953647c1 | 1506 | double number(CYCastDouble(context, value)); |
bd17e6f3 | 1507 | if (std::isnan(number)) |
953647c1 JF |
1508 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"cannot convert value to pointer" userInfo:nil]; |
1509 | return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number))); | |
7ba62cfd JF |
1510 | } |
1511 | } | |
1512 | ||
b09da87b JF |
1513 | template <typename Type_> |
1514 | _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) { | |
1515 | return reinterpret_cast<Type_>(CYCastPointer_(context, value)); | |
1516 | } | |
1517 | ||
4afefdd9 JF |
1518 | SEL CYCastSEL(JSContextRef context, JSValueRef value) { |
1519 | if (JSValueIsObjectOfClass(context, value, Selector_)) { | |
1520 | Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value))); | |
1521 | return reinterpret_cast<SEL>(data->value_); | |
1522 | } else | |
1523 | return CYCastPointer<SEL>(context, value); | |
1524 | } | |
1525 | ||
bd17e6f3 | 1526 | void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { |
ea2d184c JF |
1527 | switch (type->primitive) { |
1528 | case sig::boolean_P: | |
1529 | *reinterpret_cast<bool *>(data) = JSValueToBoolean(context, value); | |
1530 | break; | |
1531 | ||
43cb3d68 | 1532 | #define CYPoolFFI_(primitive, native) \ |
f610e1a0 JF |
1533 | case sig::primitive ## _P: \ |
1534 | *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \ | |
1535 | break; | |
ea2d184c | 1536 | |
43cb3d68 JF |
1537 | CYPoolFFI_(uchar, unsigned char) |
1538 | CYPoolFFI_(char, char) | |
1539 | CYPoolFFI_(ushort, unsigned short) | |
1540 | CYPoolFFI_(short, short) | |
1541 | CYPoolFFI_(ulong, unsigned long) | |
1542 | CYPoolFFI_(long, long) | |
1543 | CYPoolFFI_(uint, unsigned int) | |
1544 | CYPoolFFI_(int, int) | |
1545 | CYPoolFFI_(ulonglong, unsigned long long) | |
1546 | CYPoolFFI_(longlong, long long) | |
1547 | CYPoolFFI_(float, float) | |
1548 | CYPoolFFI_(double, double) | |
ea2d184c JF |
1549 | |
1550 | case sig::object_P: | |
1551 | case sig::typename_P: | |
b09da87b | 1552 | *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value); |
7ba62cfd JF |
1553 | break; |
1554 | ||
ea2d184c | 1555 | case sig::selector_P: |
7ba62cfd JF |
1556 | *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value); |
1557 | break; | |
ea2d184c | 1558 | |
b21525c7 | 1559 | case sig::pointer_P: |
b09da87b | 1560 | *reinterpret_cast<void **>(data) = CYCastPointer<void *>(context, value); |
b21525c7 | 1561 | break; |
ea2d184c | 1562 | |
77e87a6c | 1563 | case sig::string_P: |
478d4ed0 | 1564 | *reinterpret_cast<const char **>(data) = CYPoolCString(pool, context, value); |
77e87a6c | 1565 | break; |
7ba62cfd | 1566 | |
bd17e6f3 JF |
1567 | case sig::struct_P: { |
1568 | uint8_t *base(reinterpret_cast<uint8_t *>(data)); | |
f33b048a | 1569 | JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL); |
bd17e6f3 | 1570 | for (size_t index(0); index != type->data.signature.count; ++index) { |
f33b048a JF |
1571 | sig::Element *element(&type->data.signature.elements[index]); |
1572 | ffi_type *field(ffi->elements[index]); | |
1573 | ||
1574 | JSValueRef rhs; | |
1575 | if (aggregate == NULL) | |
1576 | rhs = value; | |
1577 | else { | |
1578 | rhs = CYGetProperty(context, aggregate, index); | |
1579 | if (JSValueIsUndefined(context, rhs)) { | |
283e7e33 JF |
1580 | if (element->name != NULL) |
1581 | rhs = CYGetProperty(context, aggregate, CYJSString(element->name)); | |
1582 | else | |
1583 | goto undefined; | |
1584 | if (JSValueIsUndefined(context, rhs)) undefined: | |
f33b048a JF |
1585 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"unable to extract structure value" userInfo:nil]; |
1586 | } | |
1587 | } | |
1588 | ||
1589 | CYPoolFFI(pool, context, element->type, field, base, rhs); | |
4e8c99fb | 1590 | // XXX: alignment? |
f33b048a | 1591 | base += field->size; |
bd17e6f3 JF |
1592 | } |
1593 | } break; | |
ea2d184c JF |
1594 | |
1595 | case sig::void_P: | |
1596 | break; | |
1597 | ||
bd17e6f3 | 1598 | default: |
43cb3d68 | 1599 | NSLog(@"CYPoolFFI(%c)\n", type->primitive); |
ea2d184c JF |
1600 | _assert(false); |
1601 | } | |
1602 | } | |
1603 | ||
2b52f27e | 1604 | JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner = NULL) { |
ea2d184c JF |
1605 | JSValueRef value; |
1606 | ||
1607 | switch (type->primitive) { | |
1608 | case sig::boolean_P: | |
b09da87b | 1609 | value = CYCastJSValue(context, *reinterpret_cast<bool *>(data)); |
ea2d184c JF |
1610 | break; |
1611 | ||
1612 | #define CYFromFFI_(primitive, native) \ | |
1613 | case sig::primitive ## _P: \ | |
b09da87b | 1614 | value = CYCastJSValue(context, *reinterpret_cast<native *>(data)); \ |
ea2d184c JF |
1615 | break; |
1616 | ||
1617 | CYFromFFI_(uchar, unsigned char) | |
1618 | CYFromFFI_(char, char) | |
1619 | CYFromFFI_(ushort, unsigned short) | |
1620 | CYFromFFI_(short, short) | |
1621 | CYFromFFI_(ulong, unsigned long) | |
1622 | CYFromFFI_(long, long) | |
1623 | CYFromFFI_(uint, unsigned int) | |
1624 | CYFromFFI_(int, int) | |
1625 | CYFromFFI_(ulonglong, unsigned long long) | |
1626 | CYFromFFI_(longlong, long long) | |
1627 | CYFromFFI_(float, float) | |
1628 | CYFromFFI_(double, double) | |
1629 | ||
2b52f27e JF |
1630 | case sig::object_P: { |
1631 | if (id object = *reinterpret_cast<id *>(data)) { | |
1632 | value = CYCastJSValue(context, object); | |
1633 | if (initialize) | |
1634 | [object release]; | |
1635 | } else goto null; | |
1636 | } break; | |
dea834b0 | 1637 | |
b09da87b | 1638 | case sig::typename_P: |
4cf49641 | 1639 | value = CYMakeInstance(context, *reinterpret_cast<Class *>(data), true); |
b09da87b JF |
1640 | break; |
1641 | ||
dea834b0 JF |
1642 | case sig::selector_P: |
1643 | if (SEL sel = *reinterpret_cast<SEL *>(data)) | |
1644 | value = CYMakeSelector(context, sel); | |
1645 | else goto null; | |
1646 | break; | |
1647 | ||
1648 | case sig::pointer_P: | |
1649 | if (void *pointer = *reinterpret_cast<void **>(data)) | |
ff783f8c | 1650 | value = CYMakePointer(context, pointer, type->data.data.type, owner); |
dea834b0 JF |
1651 | else goto null; |
1652 | break; | |
1653 | ||
1654 | case sig::string_P: | |
f610e1a0 | 1655 | if (char *utf8 = *reinterpret_cast<char **>(data)) |
b09da87b | 1656 | value = CYCastJSValue(context, utf8); |
f610e1a0 | 1657 | else goto null; |
dea834b0 | 1658 | break; |
ea2d184c JF |
1659 | |
1660 | case sig::struct_P: | |
bd17e6f3 JF |
1661 | value = CYMakeStruct(context, data, type, ffi, owner); |
1662 | break; | |
ea2d184c JF |
1663 | |
1664 | case sig::void_P: | |
b09da87b | 1665 | value = CYJSUndefined(context); |
f610e1a0 JF |
1666 | break; |
1667 | ||
1668 | null: | |
b09da87b | 1669 | value = CYJSNull(context); |
ea2d184c JF |
1670 | break; |
1671 | ||
bd17e6f3 | 1672 | default: |
ea2d184c JF |
1673 | NSLog(@"CYFromFFI(%c)\n", type->primitive); |
1674 | _assert(false); | |
1675 | } | |
1676 | ||
1677 | return value; | |
1678 | } | |
1679 | ||
9e20b0b7 | 1680 | bool Index_(apr_pool_t *pool, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) { |
bd17e6f3 | 1681 | Type_privateData *typical(internal->type_); |
930aa21b JF |
1682 | sig::Type *type(typical->type_); |
1683 | if (type == NULL) | |
1684 | return false; | |
bd17e6f3 | 1685 | |
9e20b0b7 JF |
1686 | size_t length; |
1687 | const char *name(CYPoolCString(pool, property, &length)); | |
1688 | double number(CYCastDouble(name, length)); | |
1689 | ||
930aa21b | 1690 | size_t count(type->data.signature.count); |
f33b048a | 1691 | |
e0dc20ec JF |
1692 | if (std::isnan(number)) { |
1693 | if (property == NULL) | |
1694 | return false; | |
9e20b0b7 | 1695 | |
930aa21b | 1696 | sig::Element *elements(type->data.signature.elements); |
f33b048a | 1697 | |
283e7e33 JF |
1698 | for (size_t local(0); local != count; ++local) { |
1699 | sig::Element *element(&elements[local]); | |
1700 | if (element->name != NULL && strcmp(name, element->name) == 0) { | |
f33b048a JF |
1701 | index = local; |
1702 | goto base; | |
1703 | } | |
283e7e33 | 1704 | } |
f33b048a | 1705 | |
9e20b0b7 | 1706 | return false; |
e0dc20ec JF |
1707 | } else { |
1708 | index = static_cast<ssize_t>(number); | |
f33b048a | 1709 | if (index != number || index < 0 || static_cast<size_t>(index) >= count) |
e0dc20ec JF |
1710 | return false; |
1711 | } | |
bd17e6f3 | 1712 | |
f33b048a | 1713 | base: |
ff783f8c JF |
1714 | ffi_type **elements(typical->GetFFI()->elements); |
1715 | ||
bd17e6f3 JF |
1716 | base = reinterpret_cast<uint8_t *>(internal->value_); |
1717 | for (ssize_t local(0); local != index; ++local) | |
ff783f8c | 1718 | base += elements[local]->size; |
9e20b0b7 JF |
1719 | |
1720 | return true; | |
bd17e6f3 JF |
1721 | } |
1722 | ||
ff783f8c JF |
1723 | static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
1724 | CYPool pool; | |
1725 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
1726 | Type_privateData *typical(internal->type_); | |
1727 | ||
930aa21b JF |
1728 | if (typical->type_ == NULL) |
1729 | return NULL; | |
1730 | ||
ff783f8c JF |
1731 | ssize_t index; |
1732 | if (!CYGetIndex(pool, property, index)) | |
1733 | return NULL; | |
1734 | ||
1735 | ffi_type *ffi(typical->GetFFI()); | |
1736 | ||
1737 | uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_)); | |
1738 | base += ffi->size * index; | |
1739 | ||
1740 | JSObjectRef owner(internal->owner_ ?: object); | |
1741 | ||
1742 | CYTry { | |
930aa21b | 1743 | return CYFromFFI(context, typical->type_, ffi, base, false, owner); |
ff783f8c JF |
1744 | } CYCatch |
1745 | } | |
1746 | ||
f37b3d2b JF |
1747 | static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { |
1748 | CYPool pool; | |
1749 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
1750 | Type_privateData *typical(internal->type_); | |
1751 | ||
930aa21b JF |
1752 | if (typical->type_ == NULL) |
1753 | return NULL; | |
1754 | ||
f37b3d2b JF |
1755 | ssize_t index; |
1756 | if (!CYGetIndex(pool, property, index)) | |
1757 | return NULL; | |
1758 | ||
1759 | ffi_type *ffi(typical->GetFFI()); | |
1760 | ||
1761 | uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_)); | |
1762 | base += ffi->size * index; | |
1763 | ||
1764 | CYTry { | |
930aa21b | 1765 | CYPoolFFI(NULL, context, typical->type_, ffi, base, value); |
f37b3d2b JF |
1766 | return true; |
1767 | } CYCatch | |
1768 | } | |
1769 | ||
bd17e6f3 | 1770 | static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
f33b048a JF |
1771 | CYPool pool; |
1772 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
1773 | Type_privateData *typical(internal->type_); | |
bd17e6f3 | 1774 | |
f33b048a JF |
1775 | ssize_t index; |
1776 | uint8_t *base; | |
bd17e6f3 | 1777 | |
f33b048a JF |
1778 | if (!Index_(pool, internal, property, index, base)) |
1779 | return NULL; | |
bd17e6f3 | 1780 | |
ff783f8c JF |
1781 | JSObjectRef owner(internal->owner_ ?: object); |
1782 | ||
f33b048a | 1783 | CYTry { |
930aa21b | 1784 | return CYFromFFI(context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, false, owner); |
bd17e6f3 JF |
1785 | } CYCatch |
1786 | } | |
1787 | ||
1788 | static bool Struct_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { | |
f33b048a JF |
1789 | CYPool pool; |
1790 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
1791 | Type_privateData *typical(internal->type_); | |
bd17e6f3 | 1792 | |
f33b048a JF |
1793 | ssize_t index; |
1794 | uint8_t *base; | |
bd17e6f3 | 1795 | |
f33b048a JF |
1796 | if (!Index_(pool, internal, property, index, base)) |
1797 | return false; | |
bd17e6f3 | 1798 | |
f33b048a | 1799 | CYTry { |
930aa21b | 1800 | CYPoolFFI(NULL, context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, value); |
bd17e6f3 JF |
1801 | return true; |
1802 | } CYCatch | |
1803 | } | |
1804 | ||
f33b048a JF |
1805 | static void Struct_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { |
1806 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
1807 | Type_privateData *typical(internal->type_); | |
930aa21b JF |
1808 | sig::Type *type(typical->type_); |
1809 | ||
1810 | if (type == NULL) | |
1811 | return; | |
f33b048a | 1812 | |
930aa21b JF |
1813 | size_t count(type->data.signature.count); |
1814 | sig::Element *elements(type->data.signature.elements); | |
f33b048a | 1815 | |
283e7e33 JF |
1816 | char number[32]; |
1817 | ||
1818 | for (size_t index(0); index != count; ++index) { | |
1819 | const char *name; | |
1820 | name = elements[index].name; | |
1821 | ||
1822 | if (name == NULL) { | |
1823 | sprintf(number, "%lu", index); | |
1824 | name = number; | |
1825 | } | |
1826 | ||
1827 | JSPropertyNameAccumulatorAddName(names, CYJSString(name)); | |
1828 | } | |
f33b048a JF |
1829 | } |
1830 | ||
2b52f27e | 1831 | 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 | 1832 | CYTry { |
4afefdd9 | 1833 | if (setups + count != signature->count - 1) |
f5e9be24 | 1834 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to ffi function" userInfo:nil]; |
85a33bf5 | 1835 | |
4afefdd9 JF |
1836 | size_t size(setups + count); |
1837 | void *values[size]; | |
1838 | memcpy(values, setup, sizeof(void *) * setups); | |
ea2d184c | 1839 | |
4afefdd9 | 1840 | for (size_t index(setups); index != size; ++index) { |
7ba62cfd | 1841 | sig::Element *element(&signature->elements[index + 1]); |
bd17e6f3 | 1842 | ffi_type *ffi(cif->arg_types[index]); |
77e87a6c | 1843 | // XXX: alignment? |
bd17e6f3 | 1844 | values[index] = new(pool) uint8_t[ffi->size]; |
4afefdd9 | 1845 | CYPoolFFI(pool, context, element->type, ffi, values[index], arguments[index - setups]); |
7ba62cfd | 1846 | } |
ea2d184c | 1847 | |
7ba62cfd JF |
1848 | uint8_t value[cif->rtype->size]; |
1849 | ffi_call(cif, function, value, values); | |
1850 | ||
2b52f27e | 1851 | return CYFromFFI(context, signature->elements[0].type, cif->rtype, value, initialize); |
0c862573 | 1852 | } CYCatch |
7ba62cfd | 1853 | } |
ea2d184c | 1854 | |
478d4ed0 | 1855 | void Closure_(ffi_cif *cif, void *result, void **arguments, void *arg) { |
478d4ed0 JF |
1856 | ffoData *data(reinterpret_cast<ffoData *>(arg)); |
1857 | ||
1858 | JSContextRef context(data->context_); | |
1859 | ||
1860 | size_t count(data->cif_.nargs); | |
1861 | JSValueRef values[count]; | |
1862 | ||
1863 | for (size_t index(0); index != count; ++index) | |
2b52f27e | 1864 | values[index] = CYFromFFI(context, data->signature_.elements[1 + index].type, data->cif_.arg_types[index], arguments[index], false); |
478d4ed0 | 1865 | |
b4aa79af | 1866 | JSValueRef value(CYCallAsFunction(context, data->function_, NULL, count, values)); |
bd17e6f3 | 1867 | CYPoolFFI(NULL, context, data->signature_.elements[0].type, data->cif_.rtype, result, value); |
478d4ed0 JF |
1868 | } |
1869 | ||
1870 | JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) { | |
1871 | // XXX: in case of exceptions this will leak | |
1872 | ffoData *data(new ffoData(type)); | |
1873 | ||
1874 | ffi_closure *closure; | |
1875 | _syscall(closure = (ffi_closure *) mmap( | |
1876 | NULL, sizeof(ffi_closure), | |
1877 | PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, | |
1878 | -1, 0 | |
1879 | )); | |
1880 | ||
1881 | ffi_status status(ffi_prep_closure(closure, &data->cif_, &Closure_, data)); | |
1882 | _assert(status == FFI_OK); | |
1883 | ||
1884 | _syscall(mprotect(closure, sizeof(*closure), PROT_READ | PROT_EXEC)); | |
1885 | ||
1886 | data->value_ = closure; | |
1887 | ||
1888 | data->context_ = CYGetJSContext(); | |
1889 | data->function_ = function; | |
1890 | ||
1891 | return JSObjectMake(context, Functor_, data); | |
1892 | } | |
1893 | ||
953647c1 | 1894 | static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
4cf49641 | 1895 | CYTry { |
b09da87b JF |
1896 | CYPool pool; |
1897 | NSString *name(CYCastNSString(pool, property)); | |
f610e1a0 | 1898 | if (Class _class = NSClassFromString(name)) |
4cf49641 | 1899 | return CYMakeInstance(context, _class, true); |
953647c1 | 1900 | if (NSMutableArray *entry = [[Bridge_ objectAtIndex:0] objectForKey:name]) |
707bcb93 JF |
1901 | switch ([[entry objectAtIndex:0] intValue]) { |
1902 | case 0: | |
057f943f | 1903 | return JSEvaluateScript(CYGetJSContext(), CYJSString([entry objectAtIndex:1]), NULL, NULL, 0, NULL); |
707bcb93 | 1904 | case 1: |
478d4ed0 | 1905 | return CYMakeFunctor(context, reinterpret_cast<void (*)()>([name cy$symbol]), CYPoolCString(pool, [entry objectAtIndex:1])); |
707bcb93 | 1906 | case 2: |
bd17e6f3 | 1907 | // XXX: this is horrendously inefficient |
707bcb93 | 1908 | sig::Signature signature; |
f33b048a | 1909 | sig::Parse(pool, &signature, CYPoolCString(pool, [entry objectAtIndex:1]), &Structor_); |
bd17e6f3 JF |
1910 | ffi_cif cif; |
1911 | sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); | |
2b52f27e | 1912 | return CYFromFFI(context, signature.elements[0].type, cif.rtype, [name cy$symbol], false); |
707bcb93 JF |
1913 | } |
1914 | return NULL; | |
1915 | } CYCatch | |
1916 | } | |
1917 | ||
04450da0 JF |
1918 | bool stret(ffi_type *ffi_type) { |
1919 | return ffi_type->type == FFI_TYPE_STRUCT && ( | |
1920 | ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE || | |
1921 | struct_forward_array[ffi_type->size] != 0 | |
1922 | ); | |
1923 | } | |
1924 | ||
b09da87b JF |
1925 | extern "C" { |
1926 | int *_NSGetArgc(void); | |
1927 | char ***_NSGetArgv(void); | |
1928 | int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName); | |
1929 | } | |
1930 | ||
1931 | static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
4cf49641 | 1932 | CYTry { |
b09da87b JF |
1933 | NSLog(@"%s", CYCastCString(context, arguments[0])); |
1934 | return CYJSUndefined(context); | |
1935 | } CYCatch | |
1936 | } | |
1937 | ||
2b52f27e | 1938 | JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { |
7ba62cfd | 1939 | const char *type; |
ea2d184c | 1940 | |
4afefdd9 JF |
1941 | Class _class(object_getClass(self)); |
1942 | if (Method method = class_getInstanceMethod(_class, _cmd)) | |
1943 | type = method_getTypeEncoding(method); | |
1944 | else { | |
1945 | CYPoolTry { | |
1946 | NSMethodSignature *method([self methodSignatureForSelector:_cmd]); | |
1947 | if (method == nil) | |
1948 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self] userInfo:nil]; | |
1949 | type = CYPoolCString(pool, [method _typeString]); | |
1950 | } CYPoolCatch(NULL) | |
1951 | } | |
1952 | ||
1953 | void *setup[2]; | |
1954 | setup[0] = &self; | |
1955 | setup[1] = &_cmd; | |
1956 | ||
1957 | sig::Signature signature; | |
f33b048a | 1958 | sig::Parse(pool, &signature, type, &Structor_); |
4afefdd9 JF |
1959 | |
1960 | ffi_cif cif; | |
1961 | sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); | |
1962 | ||
1963 | void (*function)() = stret(cif.rtype) ? reinterpret_cast<void (*)()>(&objc_msgSend_stret) : reinterpret_cast<void (*)()>(&objc_msgSend); | |
2b52f27e | 1964 | return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function); |
4afefdd9 JF |
1965 | } |
1966 | ||
1967 | static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
478d4ed0 JF |
1968 | CYPool pool; |
1969 | ||
2b52f27e JF |
1970 | bool uninitialized; |
1971 | ||
4afefdd9 JF |
1972 | id self; |
1973 | SEL _cmd; | |
1974 | ||
4cf49641 | 1975 | CYTry { |
85a33bf5 | 1976 | if (count < 2) |
f5e9be24 | 1977 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"too few arguments to objc_msgSend" userInfo:nil]; |
85a33bf5 | 1978 | |
2b52f27e JF |
1979 | if (JSValueIsObjectOfClass(context, arguments[0], Instance_)) { |
1980 | Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0]))); | |
1981 | self = data->GetValue(); | |
1982 | uninitialized = data->IsUninitialized(); | |
1983 | if (uninitialized) | |
1984 | data->value_ = nil; | |
1985 | } else { | |
1986 | self = CYCastNSObject(pool, context, arguments[0]); | |
1987 | uninitialized = false; | |
1988 | } | |
1989 | ||
7ba62cfd | 1990 | if (self == nil) |
b09da87b | 1991 | return CYJSNull(context); |
7ba62cfd | 1992 | |
4afefdd9 | 1993 | _cmd = CYCastSEL(context, arguments[1]); |
0c862573 | 1994 | } CYCatch |
ea2d184c | 1995 | |
2b52f27e | 1996 | return CYSendMessage(pool, context, self, _cmd, count - 2, arguments + 2, uninitialized, exception); |
7ba62cfd JF |
1997 | } |
1998 | ||
61933e16 JF |
1999 | void dealloc_(id self, SEL sel) { |
2000 | CYInternal *internal; | |
2001 | object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)); | |
2002 | if (internal != NULL) | |
2003 | delete internal; | |
2004 | if (Method dealloc = class_getInstanceMethod(object_getClass(self), @selector(cy$dealloc))) | |
2005 | reinterpret_cast<void (*)(id, SEL)>(method_getImplementation(dealloc))(self, sel); | |
2006 | } | |
f7c38a29 | 2007 | |
61933e16 JF |
2008 | MSHook(void, objc_registerClassPair, Class _class) { |
2009 | Class super(class_getSuperclass(_class)); | |
2010 | if (super == NULL || class_getInstanceVariable(super, "cy$internal_") == NULL) { | |
2011 | class_addIvar(_class, "cy$internal_", sizeof(CYInternal *), log2(sizeof(CYInternal *)), "^{CYInternal}"); | |
2012 | if (!class_addMethod(_class, @selector(dealloc), reinterpret_cast<IMP>(&dealloc_), "v8@0:4")) { | |
2013 | Method dealloc(class_getInstanceMethod(_class, @selector(dealloc))); | |
2014 | class_addMethod(_class, @selector(cy$dealloc), method_getImplementation(dealloc), method_getTypeEncoding(dealloc)); | |
2015 | method_setImplementation(dealloc, reinterpret_cast<IMP>(&dealloc_)); | |
2016 | } | |
f7c38a29 JF |
2017 | } |
2018 | ||
61933e16 | 2019 | _objc_registerClassPair(_class); |
f7c38a29 JF |
2020 | } |
2021 | ||
61933e16 | 2022 | static JSValueRef objc_registerClassPair_(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
f7c38a29 JF |
2023 | CYTry { |
2024 | CYPool pool; | |
f7c38a29 | 2025 | Class _class(CYCastNSObject(pool, context, object)); |
61933e16 | 2026 | $objc_registerClassPair(_class); |
f7c38a29 JF |
2027 | return CYJSUndefined(context); |
2028 | } CYCatch | |
2029 | } | |
2030 | ||
dea834b0 JF |
2031 | static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
2032 | JSValueRef setup[count + 2]; | |
2033 | setup[0] = _this; | |
2034 | setup[1] = object; | |
4afefdd9 | 2035 | memcpy(setup + 2, arguments, sizeof(JSValueRef) * count); |
dea834b0 JF |
2036 | return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception); |
2037 | } | |
2038 | ||
2039 | static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
4afefdd9 | 2040 | CYPool pool; |
953647c1 | 2041 | Functor_privateData *data(reinterpret_cast<Functor_privateData *>(JSObjectGetPrivate(object))); |
2b52f27e | 2042 | return CYCallFunction(pool, context, 0, NULL, count, arguments, false, exception, &data->signature_, &data->cif_, reinterpret_cast<void (*)()>(data->value_)); |
ea2d184c JF |
2043 | } |
2044 | ||
b09da87b | 2045 | JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
4cf49641 | 2046 | CYTry { |
dea834b0 JF |
2047 | if (count != 1) |
2048 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector constructor" userInfo:nil]; | |
2049 | const char *name(CYCastCString(context, arguments[0])); | |
2050 | return CYMakeSelector(context, sel_registerName(name)); | |
2051 | } CYCatch | |
2052 | } | |
2053 | ||
f7c38a29 JF |
2054 | JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
2055 | CYTry { | |
2056 | if (count != 2) | |
2057 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Functor constructor" userInfo:nil]; | |
2058 | ||
2059 | void *value(CYCastPointer<void *>(context, arguments[0])); | |
2060 | const char *type(CYCastCString(context, arguments[1])); | |
2061 | ||
2062 | CYPool pool; | |
2063 | ||
2064 | sig::Signature signature; | |
2065 | sig::Parse(pool, &signature, type, &Structor_); | |
2066 | ||
2067 | return CYMakePointer(context, value, signature.elements[0].type, NULL); | |
2068 | } CYCatch | |
2069 | } | |
2070 | ||
b09da87b | 2071 | JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
4cf49641 | 2072 | CYTry { |
b21525c7 | 2073 | if (count != 2) |
dea834b0 | 2074 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Functor constructor" userInfo:nil]; |
b21525c7 | 2075 | const char *type(CYCastCString(context, arguments[1])); |
b09da87b JF |
2076 | JSValueRef exception(NULL); |
2077 | if (JSValueIsInstanceOfConstructor(context, arguments[0], Function_, &exception)) { | |
2078 | JSObjectRef function(CYCastJSObject(context, arguments[0])); | |
2079 | return CYMakeFunctor(context, function, type); | |
2080 | } else if (exception != NULL) { | |
2081 | return NULL; | |
2082 | } else { | |
2083 | void (*function)()(CYCastPointer<void (*)()>(context, arguments[0])); | |
2084 | return CYMakeFunctor(context, function, type); | |
2085 | } | |
0c862573 | 2086 | } CYCatch |
ea2d184c JF |
2087 | } |
2088 | ||
61933e16 JF |
2089 | JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
2090 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(object))); | |
2091 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_)); | |
04450da0 JF |
2092 | } |
2093 | ||
dea834b0 JF |
2094 | JSValueRef Selector_getProperty_prototype(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
2095 | return Function_; | |
2096 | } | |
2097 | ||
61933e16 | 2098 | static JSValueRef CYValue_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
953647c1 | 2099 | CYTry { |
61933e16 JF |
2100 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this))); |
2101 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_)); | |
953647c1 JF |
2102 | } CYCatch |
2103 | } | |
2104 | ||
61933e16 JF |
2105 | static JSValueRef CYValue_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
2106 | return CYValue_callAsFunction_valueOf(context, object, _this, count, arguments, exception); | |
b4aa79af JF |
2107 | } |
2108 | ||
61933e16 | 2109 | static JSValueRef CYValue_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
4afefdd9 | 2110 | CYTry { |
61933e16 | 2111 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this))); |
4afefdd9 | 2112 | char string[32]; |
61933e16 | 2113 | sprintf(string, "%p", internal->value_); |
4afefdd9 JF |
2114 | return CYCastJSValue(context, string); |
2115 | } CYCatch | |
2116 | } | |
2117 | ||
b4aa79af JF |
2118 | static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
2119 | CYTry { | |
61933e16 | 2120 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); |
b4aa79af | 2121 | CYPoolTry { |
61933e16 | 2122 | return CYCastJSValue(context, CYJSString([internal->GetValue() cy$toCYON])); |
b4aa79af JF |
2123 | } CYPoolCatch(NULL) |
2124 | } CYCatch | |
2125 | } | |
2126 | ||
2127 | static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2128 | CYTry { | |
61933e16 | 2129 | Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); |
b4aa79af JF |
2130 | CYPoolTry { |
2131 | NSString *key(count == 0 ? nil : CYCastNSString(NULL, CYJSString(context, arguments[0]))); | |
61933e16 | 2132 | return CYCastJSValue(context, CYJSString([internal->GetValue() cy$toJSON:key])); |
b4aa79af JF |
2133 | } CYPoolCatch(NULL) |
2134 | } CYCatch | |
2135 | } | |
2136 | ||
4cf49641 JF |
2137 | static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
2138 | CYTry { | |
2b52f27e | 2139 | Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this))); |
4e8c99fb JF |
2140 | CYPoolTry { |
2141 | return CYCastJSValue(context, CYJSString([data->GetValue() description])); | |
4cf49641 | 2142 | } CYPoolCatch(NULL) |
478d4ed0 JF |
2143 | } CYCatch |
2144 | } | |
2145 | ||
2146 | static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
4cf49641 | 2147 | CYTry { |
953647c1 | 2148 | Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); |
478d4ed0 JF |
2149 | return CYCastJSValue(context, sel_getName(data->GetValue())); |
2150 | } CYCatch | |
2151 | } | |
2152 | ||
b4aa79af JF |
2153 | static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
2154 | return Selector_callAsFunction_toString(context, object, _this, count, arguments, exception); | |
2155 | } | |
2156 | ||
2157 | static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
2158 | CYTry { | |
2159 | Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); | |
2160 | const char *name(sel_getName(data->GetValue())); | |
2161 | CYPoolTry { | |
2162 | return CYCastJSValue(context, CYJSString([NSString stringWithFormat:@"@selector(%s)", name])); | |
2163 | } CYPoolCatch(NULL) | |
2164 | } CYCatch | |
2165 | } | |
2166 | ||
b09da87b | 2167 | static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
4cf49641 | 2168 | CYTry { |
b09da87b JF |
2169 | if (count != 2) |
2170 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector.type" userInfo:nil]; | |
2171 | CYPool pool; | |
953647c1 | 2172 | Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this))); |
b09da87b JF |
2173 | Class _class(CYCastNSObject(pool, context, arguments[0])); |
2174 | bool instance(CYCastBool(context, arguments[1])); | |
2175 | SEL sel(data->GetValue()); | |
2176 | if (Method method = (*(instance ? &class_getInstanceMethod : class_getClassMethod))(_class, sel)) | |
2177 | return CYCastJSValue(context, method_getTypeEncoding(method)); | |
953647c1 | 2178 | else if (NSString *type = [[Bridge_ objectAtIndex:1] objectForKey:CYCastNSString(pool, sel_getName(sel))]) |
b09da87b JF |
2179 | return CYCastJSValue(context, CYJSString(type)); |
2180 | else | |
2181 | return CYJSNull(context); | |
2182 | } CYCatch | |
2183 | } | |
2184 | ||
61933e16 JF |
2185 | static JSStaticValue CYValue_staticValues[2] = { |
2186 | {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete}, | |
04450da0 JF |
2187 | {NULL, NULL, NULL, 0} |
2188 | }; | |
2189 | ||
4afefdd9 | 2190 | static JSStaticFunction Pointer_staticFunctions[4] = { |
61933e16 JF |
2191 | {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
2192 | {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2193 | {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
ff783f8c JF |
2194 | {NULL, NULL, 0} |
2195 | }; | |
2196 | ||
2197 | static JSStaticFunction Functor_staticFunctions[4] = { | |
61933e16 JF |
2198 | {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
2199 | {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2200 | {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
953647c1 JF |
2201 | {NULL, NULL, 0} |
2202 | }; | |
2203 | ||
dea834b0 JF |
2204 | /*static JSStaticValue Selector_staticValues[2] = { |
2205 | {"prototype", &Selector_getProperty_prototype, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete}, | |
2206 | {NULL, NULL, NULL, 0} | |
2207 | };*/ | |
2208 | ||
b4aa79af JF |
2209 | static JSStaticFunction Instance_staticFunctions[4] = { |
2210 | {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2211 | {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
478d4ed0 JF |
2212 | {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
2213 | {NULL, NULL, 0} | |
2214 | }; | |
2215 | ||
b4aa79af JF |
2216 | static JSStaticFunction Selector_staticFunctions[5] = { |
2217 | {"toCYON", &Selector_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2218 | {"toJSON", &Selector_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
478d4ed0 | 2219 | {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
b09da87b JF |
2220 | {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
2221 | {NULL, NULL, 0} | |
2222 | }; | |
2223 | ||
5999c315 | 2224 | CYDriver::CYDriver(const std::string &filename) : |
db5e2840 | 2225 | state_(CYClear), |
e7ed5354 JF |
2226 | data_(NULL), |
2227 | size_(0), | |
5999c315 JF |
2228 | filename_(filename), |
2229 | source_(NULL) | |
2230 | { | |
924f67b2 JF |
2231 | ScannerInit(); |
2232 | } | |
2233 | ||
5999c315 | 2234 | CYDriver::~CYDriver() { |
924f67b2 JF |
2235 | ScannerDestroy(); |
2236 | } | |
2237 | ||
5befe15e JF |
2238 | void cy::parser::error(const cy::parser::location_type &location, const std::string &message) { |
2239 | CYDriver::Error error; | |
2240 | error.location_ = location; | |
2241 | error.message_ = message; | |
2242 | driver.errors_.push_back(error); | |
63b4c5a8 JF |
2243 | } |
2244 | ||
b09da87b JF |
2245 | void CYSetArgs(int argc, const char *argv[]) { |
2246 | JSContextRef context(CYGetJSContext()); | |
2247 | JSValueRef args[argc]; | |
2248 | for (int i(0); i != argc; ++i) | |
2249 | args[i] = CYCastJSValue(context, argv[i]); | |
2250 | JSValueRef exception(NULL); | |
2251 | JSObjectRef array(JSObjectMakeArray(context, argc, args, &exception)); | |
2252 | CYThrow(context, exception); | |
2253 | CYSetProperty(context, System_, CYJSString("args"), array); | |
2254 | } | |
2255 | ||
579ed526 JF |
2256 | JSObjectRef CYGetGlobalObject(JSContextRef context) { |
2257 | return JSContextGetGlobalObject(context); | |
2258 | } | |
2259 | ||
7ba62cfd | 2260 | MSInitialize { _pooled |
ea2d184c JF |
2261 | apr_initialize(); |
2262 | ||
953647c1 JF |
2263 | Bridge_ = [[NSMutableArray arrayWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain]; |
2264 | ||
c1582939 JF |
2265 | NSCFBoolean_ = objc_getClass("NSCFBoolean"); |
2266 | ||
c1582939 JF |
2267 | JSClassDefinition definition; |
2268 | ||
2269 | definition = kJSClassDefinitionEmpty; | |
88c977fa | 2270 | definition.className = "Pointer"; |
953647c1 | 2271 | definition.staticFunctions = Pointer_staticFunctions; |
ff783f8c | 2272 | definition.getProperty = &Pointer_getProperty; |
f37b3d2b | 2273 | definition.setProperty = &Pointer_setProperty; |
953647c1 | 2274 | definition.finalize = &CYData::Finalize; |
88c977fa | 2275 | Pointer_ = JSClassCreate(&definition); |
c1582939 JF |
2276 | |
2277 | definition = kJSClassDefinitionEmpty; | |
88c977fa | 2278 | definition.className = "Functor"; |
ff783f8c | 2279 | definition.staticFunctions = Functor_staticFunctions; |
dea834b0 | 2280 | definition.callAsFunction = &Functor_callAsFunction; |
953647c1 | 2281 | definition.finalize = &CYData::Finalize; |
88c977fa | 2282 | Functor_ = JSClassCreate(&definition); |
7ba62cfd | 2283 | |
953647c1 JF |
2284 | definition = kJSClassDefinitionEmpty; |
2285 | definition.className = "Struct"; | |
bd17e6f3 JF |
2286 | definition.getProperty = &Struct_getProperty; |
2287 | definition.setProperty = &Struct_setProperty; | |
f33b048a | 2288 | definition.getPropertyNames = &Struct_getPropertyNames; |
953647c1 JF |
2289 | definition.finalize = &CYData::Finalize; |
2290 | Struct_ = JSClassCreate(&definition); | |
2291 | ||
77e87a6c | 2292 | definition = kJSClassDefinitionEmpty; |
88c977fa | 2293 | definition.className = "Selector"; |
61933e16 | 2294 | definition.staticValues = CYValue_staticValues; |
dea834b0 | 2295 | //definition.staticValues = Selector_staticValues; |
b09da87b | 2296 | definition.staticFunctions = Selector_staticFunctions; |
dea834b0 | 2297 | definition.callAsFunction = &Selector_callAsFunction; |
953647c1 | 2298 | definition.finalize = &CYData::Finalize; |
88c977fa | 2299 | Selector_ = JSClassCreate(&definition); |
77e87a6c | 2300 | |
7ba62cfd | 2301 | definition = kJSClassDefinitionEmpty; |
b09da87b | 2302 | definition.className = "Instance"; |
61933e16 | 2303 | definition.staticValues = CYValue_staticValues; |
478d4ed0 | 2304 | definition.staticFunctions = Instance_staticFunctions; |
88c977fa | 2305 | definition.getProperty = &Instance_getProperty; |
b09da87b JF |
2306 | definition.setProperty = &Instance_setProperty; |
2307 | definition.deleteProperty = &Instance_deleteProperty; | |
88c977fa | 2308 | definition.callAsConstructor = &Instance_callAsConstructor; |
953647c1 | 2309 | definition.finalize = &CYData::Finalize; |
88c977fa | 2310 | Instance_ = JSClassCreate(&definition); |
7ba62cfd JF |
2311 | |
2312 | definition = kJSClassDefinitionEmpty; | |
953647c1 JF |
2313 | definition.className = "Runtime"; |
2314 | definition.getProperty = &Runtime_getProperty; | |
2315 | Runtime_ = JSClassCreate(&definition); | |
2316 | ||
2317 | definition = kJSClassDefinitionEmpty; | |
2318 | //definition.getProperty = &Global_getProperty; | |
88c977fa | 2319 | JSClassRef Global(JSClassCreate(&definition)); |
c1582939 | 2320 | |
478d4ed0 | 2321 | JSGlobalContextRef context(JSGlobalContextCreate(Global)); |
ea2d184c JF |
2322 | Context_ = context; |
2323 | ||
579ed526 | 2324 | JSObjectRef global(CYGetGlobalObject(context)); |
c1582939 | 2325 | |
953647c1 | 2326 | JSObjectSetPrototype(context, global, JSObjectMake(context, Runtime_, NULL)); |
579ed526 | 2327 | CYSetProperty(context, global, CYJSString("ObjectiveC"), JSObjectMake(context, Runtime_, NULL)); |
953647c1 | 2328 | |
b09da87b | 2329 | CYSetProperty(context, global, CYJSString("Functor"), JSObjectMakeConstructor(context, Functor_, &Functor_new)); |
f7c38a29 JF |
2330 | CYSetProperty(context, global, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new)); |
2331 | CYSetProperty(context, global, CYJSString("Selector"), JSObjectMakeConstructor(context, Selector_, &Selector_new)); | |
7ba62cfd | 2332 | |
61933e16 JF |
2333 | MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair)); |
2334 | ||
2335 | CYSetProperty(context, global, CYJSString("objc_registerClassPair"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_registerClassPair"), &objc_registerClassPair_)); | |
dea834b0 | 2336 | CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend)); |
7ba62cfd | 2337 | |
b09da87b JF |
2338 | System_ = JSObjectMake(context, NULL, NULL); |
2339 | CYSetProperty(context, global, CYJSString("system"), System_); | |
2340 | CYSetProperty(context, System_, CYJSString("args"), CYJSNull(context)); | |
579ed526 | 2341 | //CYSetProperty(context, System_, CYJSString("global"), global); |
b09da87b JF |
2342 | |
2343 | CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print)); | |
2344 | ||
c1582939 | 2345 | length_ = JSStringCreateWithUTF8CString("length"); |
b4aa79af JF |
2346 | message_ = JSStringCreateWithUTF8CString("message"); |
2347 | name_ = JSStringCreateWithUTF8CString("name"); | |
2348 | toCYON_ = JSStringCreateWithUTF8CString("toCYON"); | |
2349 | toJSON_ = JSStringCreateWithUTF8CString("toJSON"); | |
c1582939 | 2350 | |
dea834b0 JF |
2351 | Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array"))); |
2352 | Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function"))); | |
c1582939 | 2353 | } |