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