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