]> git.saurik.com Git - cycript.git/blob - Execute.cpp
f21d6da216d9881dd287d30074540b997518a581
[cycript.git] / Execute.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
7 * Cycript is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cycript is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include "Internal.hpp"
23
24 #include <dlfcn.h>
25 #include <dirent.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28
29 #include "cycript.hpp"
30
31 #include "sig/parse.hpp"
32 #include "sig/ffi_type.hpp"
33
34 #include "Pooling.hpp"
35 #include "Execute.hpp"
36
37 #include <sys/mman.h>
38 #include <sys/stat.h>
39
40 #include <iostream>
41 #include <set>
42 #include <map>
43 #include <iomanip>
44 #include <sstream>
45 #include <cmath>
46
47 #include "Parser.hpp"
48
49 #include "Decode.hpp"
50 #include "Error.hpp"
51 #include "JavaScript.hpp"
52 #include "String.hpp"
53
54 struct CYHooks *hooks_;
55
56 /* JavaScript Properties {{{ */
57 JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index) {
58 return _jsccall(JSObjectGetPropertyAtIndex, context, object, index);
59 }
60
61 JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name) {
62 return _jsccall(JSObjectGetProperty, context, object, name);
63 }
64
65 void CYSetProperty(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value) {
66 _jsccall(JSObjectSetPropertyAtIndex, context, object, index, value);
67 }
68
69 void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value, JSPropertyAttributes attributes) {
70 _jsccall(JSObjectSetProperty, context, object, name, value, attributes);
71 }
72
73 void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef (*callback)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *), JSPropertyAttributes attributes) {
74 CYSetProperty(context, object, name, JSObjectMakeFunctionWithCallback(context, name, callback), attributes);
75 }
76 /* }}} */
77 /* JavaScript Strings {{{ */
78 JSStringRef CYCopyJSString(const char *value) {
79 return value == NULL ? NULL : JSStringCreateWithUTF8CString(value);
80 }
81
82 JSStringRef CYCopyJSString(JSStringRef value) {
83 return value == NULL ? NULL : JSStringRetain(value);
84 }
85
86 JSStringRef CYCopyJSString(CYUTF8String value) {
87 // XXX: this is very wrong; it needs to convert to UTF16 and then create from there
88 return CYCopyJSString(value.data);
89 }
90
91 JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value) {
92 if (JSValueIsNull(context, value))
93 return NULL;
94 return _jsccall(JSValueToStringCopy, context, value);
95 }
96
97 static CYUTF16String CYCastUTF16String(JSStringRef value) {
98 return CYUTF16String(JSStringGetCharactersPtr(value), JSStringGetLength(value));
99 }
100
101 CYUTF8String CYPoolUTF8String(CYPool &pool, JSContextRef context, JSStringRef value) {
102 return CYPoolUTF8String(pool, CYCastUTF16String(value));
103 }
104
105 const char *CYPoolCString(CYPool &pool, JSContextRef context, JSStringRef value) {
106 CYUTF8String utf8(CYPoolUTF8String(pool, context, value));
107 _assert(memchr(utf8.data, '\0', utf8.size) == NULL);
108 return utf8.data;
109 }
110
111 const char *CYPoolCString(CYPool &pool, JSContextRef context, JSValueRef value) {
112 return JSValueIsNull(context, value) ? NULL : CYPoolCString(pool, context, CYJSString(context, value));
113 }
114 /* }}} */
115 /* Index Offsets {{{ */
116 size_t CYGetIndex(CYPool &pool, JSContextRef context, JSStringRef value) {
117 return CYGetIndex(CYPoolUTF8String(pool, context, value));
118 }
119 /* }}} */
120
121 static JSClassRef All_;
122 static JSClassRef Context_;
123 JSClassRef Functor_;
124 static JSClassRef Global_;
125 static JSClassRef Pointer_;
126 static JSClassRef Struct_;
127
128 JSStringRef Array_s;
129 JSStringRef cy_s;
130 JSStringRef length_s;
131 JSStringRef message_s;
132 JSStringRef name_s;
133 JSStringRef pop_s;
134 JSStringRef prototype_s;
135 JSStringRef push_s;
136 JSStringRef splice_s;
137 JSStringRef toCYON_s;
138 JSStringRef toJSON_s;
139 JSStringRef toPointer_s;
140 JSStringRef toString_s;
141
142 static JSStringRef Result_;
143
144 void CYFinalize(JSObjectRef object) {
145 CYData *internal(reinterpret_cast<CYData *>(JSObjectGetPrivate(object)));
146 _assert(internal->count_ != _not(unsigned));
147 if (--internal->count_ == 0)
148 delete internal;
149 }
150
151 void Structor_(CYPool &pool, sig::Type *&type) {
152 if (
153 type->primitive == sig::pointer_P &&
154 type->data.data.type != NULL &&
155 type->data.data.type->primitive == sig::struct_P &&
156 type->data.data.type->name != NULL &&
157 strcmp(type->data.data.type->name, "_objc_class") == 0
158 ) {
159 type->primitive = sig::typename_P;
160 type->data.data.type = NULL;
161 return;
162 }
163
164 if (type->primitive != sig::struct_P || type->name == NULL)
165 return;
166
167 size_t length(strlen(type->name));
168 char keyed[length + 2];
169 memcpy(keyed + 1, type->name, length + 1);
170
171 static const char *modes = "34";
172 for (size_t i(0); i != 2; ++i) {
173 char mode(modes[i]);
174 keyed[0] = mode;
175
176 if (CYBridgeEntry *entry = CYBridgeHash(keyed, length + 1))
177 switch (mode) {
178 case '3':
179 sig::Parse(pool, &type->data.signature, entry->value_, &Structor_);
180 break;
181
182 case '4': {
183 sig::Signature signature;
184 sig::Parse(pool, &signature, entry->value_, &Structor_);
185 type = signature.elements[0].type;
186 } break;
187 }
188 }
189 }
190
191 JSClassRef Type_privateData::Class_;
192
193 struct Context :
194 CYData
195 {
196 JSGlobalContextRef context_;
197
198 Context(JSGlobalContextRef context) :
199 context_(context)
200 {
201 }
202 };
203
204 struct Pointer :
205 CYOwned
206 {
207 Type_privateData *type_;
208 size_t length_;
209
210 Pointer(void *value, JSContextRef context, JSObjectRef owner, size_t length, sig::Type *type) :
211 CYOwned(value, context, owner),
212 type_(new(*pool_) Type_privateData(type)),
213 length_(length)
214 {
215 }
216 };
217
218 struct Struct_privateData :
219 CYOwned
220 {
221 Type_privateData *type_;
222
223 Struct_privateData(JSContextRef context, JSObjectRef owner) :
224 CYOwned(NULL, context, owner)
225 {
226 }
227 };
228
229 typedef std::map<const char *, Type_privateData *, CYCStringLess> TypeMap;
230 static TypeMap Types_;
231
232 JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
233 Struct_privateData *internal(new Struct_privateData(context, owner));
234 CYPool &pool(*internal->pool_);
235 Type_privateData *typical(new(pool) Type_privateData(type, ffi));
236 internal->type_ = typical;
237
238 if (owner != NULL)
239 internal->value_ = data;
240 else {
241 size_t size(typical->GetFFI()->size);
242 void *copy(internal->pool_->malloc<void>(size));
243 memcpy(copy, data, size);
244 internal->value_ = copy;
245 }
246
247 return JSObjectMake(context, Struct_, internal);
248 }
249
250 static void *CYCastSymbol(const char *name) {
251 return dlsym(RTLD_DEFAULT, name);
252 }
253
254 JSValueRef CYCastJSValue(JSContextRef context, bool value) {
255 return JSValueMakeBoolean(context, value);
256 }
257
258 JSValueRef CYCastJSValue(JSContextRef context, double value) {
259 return JSValueMakeNumber(context, value);
260 }
261
262 #define CYCastJSValue_(Type_) \
263 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
264 return JSValueMakeNumber(context, static_cast<double>(value)); \
265 }
266
267 CYCastJSValue_(int)
268 CYCastJSValue_(unsigned int)
269 CYCastJSValue_(long int)
270 CYCastJSValue_(long unsigned int)
271 CYCastJSValue_(long long int)
272 CYCastJSValue_(long long unsigned int)
273
274 JSValueRef CYJSUndefined(JSContextRef context) {
275 return JSValueMakeUndefined(context);
276 }
277
278 double CYCastDouble(JSContextRef context, JSValueRef value) {
279 return _jsccall(JSValueToNumber, context, value);
280 }
281
282 bool CYCastBool(JSContextRef context, JSValueRef value) {
283 return JSValueToBoolean(context, value);
284 }
285
286 JSValueRef CYJSNull(JSContextRef context) {
287 return JSValueMakeNull(context);
288 }
289
290 JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value) {
291 return value == NULL ? CYJSNull(context) : JSValueMakeString(context, value);
292 }
293
294 JSValueRef CYCastJSValue(JSContextRef context, const char *value) {
295 return CYCastJSValue(context, CYJSString(value));
296 }
297
298 JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) {
299 return _jsccall(JSValueToObject, context, value);
300 }
301
302 JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, const JSValueRef arguments[]) {
303 return _jsccall(JSObjectCallAsFunction, context, function, _this, count, arguments);
304 }
305
306 bool CYIsCallable(JSContextRef context, JSValueRef value) {
307 return value != NULL && JSValueIsObject(context, value) && JSObjectIsFunction(context, (JSObjectRef) value);
308 }
309
310 size_t CYArrayLength(JSContextRef context, JSObjectRef array) {
311 return CYCastDouble(context, CYGetProperty(context, array, length_s));
312 }
313
314 JSValueRef CYArrayGet(JSContextRef context, JSObjectRef array, size_t index) {
315 return _jsccall(JSObjectGetPropertyAtIndex, context, array, index);
316 }
317
318 void CYArrayPush(JSContextRef context, JSObjectRef array, JSValueRef value) {
319 JSValueRef arguments[1];
320 arguments[0] = value;
321 JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array_prototype")));
322 _jsccall(JSObjectCallAsFunction, context, CYCastJSObject(context, CYGetProperty(context, Array, push_s)), array, 1, arguments);
323 }
324
325 static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
326 if (count == 0)
327 printf("\n");
328 else {
329 CYPool pool;
330 printf("%s\n", CYPoolCString(pool, context, arguments[0]));
331 }
332
333 return CYJSUndefined(context);
334 } CYCatch(NULL) }
335
336 static size_t Nonce_(0);
337
338 static JSValueRef $cyq(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
339 CYPool pool;
340 const char *name(pool.strcat(CYPoolCString(pool, context, arguments[0]), pool.itoa(Nonce_++), NULL));
341 return CYCastJSValue(context, name);
342 } CYCatch(NULL) }
343
344 static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
345 JSGarbageCollect(context);
346 return CYJSUndefined(context);
347 } CYCatch(NULL) }
348
349 const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value, JSValueRef *exception) { CYTry {
350 switch (JSType type = JSValueGetType(context, value)) {
351 case kJSTypeUndefined:
352 return "undefined";
353 case kJSTypeNull:
354 return "null";
355 case kJSTypeBoolean:
356 return CYCastBool(context, value) ? "true" : "false";
357
358 case kJSTypeNumber: {
359 std::ostringstream str;
360 CYNumerify(str, CYCastDouble(context, value));
361 std::string value(str.str());
362 return pool.strmemdup(value.c_str(), value.size());
363 } break;
364
365 case kJSTypeString: {
366 std::ostringstream str;
367 CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, value)));
368 CYStringify(str, string.data, string.size);
369 std::string value(str.str());
370 return pool.strmemdup(value.c_str(), value.size());
371 } break;
372
373 case kJSTypeObject:
374 return CYPoolCCYON(pool, context, (JSObjectRef) value);
375 default:
376 throw CYJSError(context, "JSValueGetType() == 0x%x", type);
377 }
378 } CYCatch(NULL) }
379
380 const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value) {
381 return _jsccall(CYPoolCCYON, pool, context, value);
382 }
383
384 const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object) {
385 JSValueRef toCYON(CYGetProperty(context, object, toCYON_s));
386 if (CYIsCallable(context, toCYON)) {
387 JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toCYON, object, 0, NULL));
388 _assert(value != NULL);
389 return CYPoolCString(pool, context, value);
390 }
391
392 JSValueRef toJSON(CYGetProperty(context, object, toJSON_s));
393 if (CYIsCallable(context, toJSON)) {
394 JSValueRef arguments[1] = {CYCastJSValue(context, CYJSString(""))};
395 return _jsccall(CYPoolCCYON, pool, context, CYCallAsFunction(context, (JSObjectRef) toJSON, object, 1, arguments));
396 }
397
398 if (JSObjectIsFunction(context, object)) {
399 JSValueRef toString(CYGetProperty(context, object, toString_s));
400 if (CYIsCallable(context, toString)) {
401 JSValueRef arguments[1] = {CYCastJSValue(context, CYJSString(""))};
402 JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toString, object, 1, arguments));
403 _assert(value != NULL);
404 return CYPoolCString(pool, context, value);
405 }
406 }
407
408 std::ostringstream str;
409
410 str << '{';
411
412 // XXX: this is, sadly, going to leak
413 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context, object));
414
415 bool comma(false);
416
417 for (size_t index(0), count(JSPropertyNameArrayGetCount(names)); index != count; ++index) {
418 if (comma)
419 str << ',';
420 else
421 comma = true;
422
423 JSStringRef name(JSPropertyNameArrayGetNameAtIndex(names, index));
424 CYUTF8String string(CYPoolUTF8String(pool, context, name));
425
426 if (CYIsKey(string))
427 str << string.data;
428 else
429 CYStringify(str, string.data, string.size);
430
431 str << ':';
432
433 try {
434 JSValueRef value(CYGetProperty(context, object, name));
435 str << CYPoolCCYON(pool, context, value);
436 } catch (const CYException &error) {
437 str << "@error";
438 }
439 }
440
441 JSPropertyNameArrayRelease(names);
442
443 str << '}';
444
445 std::string string(str.str());
446 return pool.strmemdup(string.c_str(), string.size());
447 }
448
449 static JSValueRef Array_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
450 CYPool pool;
451 std::ostringstream str;
452
453 str << '[';
454
455 JSValueRef length(CYGetProperty(context, _this, length_s));
456 bool comma(false);
457
458 for (size_t index(0), count(CYCastDouble(context, length)); index != count; ++index) {
459 if (comma)
460 str << ',';
461 else
462 comma = true;
463
464 try {
465 JSValueRef value(CYGetProperty(context, _this, index));
466 if (!JSValueIsUndefined(context, value))
467 str << CYPoolCCYON(pool, context, value);
468 else {
469 str << ',';
470 comma = false;
471 }
472 } catch (const CYException &error) {
473 str << "@error";
474 }
475 }
476
477 str << ']';
478
479 std::string value(str.str());
480 return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size())));
481 } CYCatch(NULL) }
482
483 static JSValueRef String_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
484 CYPool pool;
485 std::ostringstream str;
486
487 CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, _this)));
488 CYStringify(str, string.data, string.size);
489
490 std::string value(str.str());
491 return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size())));
492 } CYCatch(NULL) }
493
494 JSObjectRef CYMakePointer(JSContextRef context, void *pointer, size_t length, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
495 Pointer *internal(new Pointer(pointer, context, owner, length, type));
496 return JSObjectMake(context, Pointer_, internal);
497 }
498
499 static JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *encoding) {
500 return JSObjectMake(context, Functor_, new cy::Functor(encoding, function));
501 }
502
503 static JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), sig::Signature &signature) {
504 return JSObjectMake(context, Functor_, new cy::Functor(signature, function));
505 }
506
507 static JSObjectRef CYMakeFunctor(JSContextRef context, const char *symbol, const char *type, void **cache) {
508 cy::Functor *internal;
509 if (*cache != NULL)
510 internal = reinterpret_cast<cy::Functor *>(*cache);
511 else {
512 void (*function)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol)));
513 if (function == NULL)
514 return NULL;
515
516 internal = new cy::Functor(type, function);
517 *cache = internal;
518 }
519
520 ++internal->count_;
521 return JSObjectMake(context, Functor_, internal);
522 }
523
524 static bool CYGetOffset(CYPool &pool, JSContextRef context, JSStringRef value, ssize_t &index) {
525 return CYGetOffset(CYPoolCString(pool, context, value), index);
526 }
527
528 void *CYCastPointer_(JSContextRef context, JSValueRef value) {
529 switch (JSValueGetType(context, value)) {
530 case kJSTypeNull:
531 return NULL;
532 case kJSTypeObject: {
533 JSObjectRef object((JSObjectRef) value);
534 if (JSValueIsObjectOfClass(context, value, Pointer_)) {
535 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
536 return internal->value_;
537 }
538 JSValueRef toPointer(CYGetProperty(context, object, toPointer_s));
539 if (CYIsCallable(context, toPointer)) {
540 JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toPointer, object, 0, NULL));
541 _assert(value != NULL);
542 return CYCastPointer_(context, value);
543 }
544 } default:
545 double number(CYCastDouble(context, value));
546 if (std::isnan(number))
547 throw CYJSError(context, "cannot convert value to pointer");
548 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number)));
549 }
550 }
551
552 void CYPoolFFI(CYPool *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) {
553 switch (type->primitive) {
554 case sig::boolean_P:
555 *reinterpret_cast<bool *>(data) = JSValueToBoolean(context, value);
556 break;
557
558 #define CYPoolFFI_(primitive, native) \
559 case sig::primitive ## _P: \
560 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
561 break;
562
563 CYPoolFFI_(uchar, unsigned char)
564 CYPoolFFI_(char, char)
565 CYPoolFFI_(ushort, unsigned short)
566 CYPoolFFI_(short, short)
567 CYPoolFFI_(ulong, unsigned long)
568 CYPoolFFI_(long, long)
569 CYPoolFFI_(uint, unsigned int)
570 CYPoolFFI_(int, int)
571 CYPoolFFI_(ulonglong, unsigned long long)
572 CYPoolFFI_(longlong, long long)
573 CYPoolFFI_(float, float)
574 CYPoolFFI_(double, double)
575
576 case sig::array_P: {
577 uint8_t *base(reinterpret_cast<uint8_t *>(data));
578 JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL);
579 for (size_t index(0); index != type->data.data.size; ++index) {
580 ffi_type *field(ffi->elements[index]);
581
582 JSValueRef rhs;
583 if (aggregate == NULL)
584 rhs = value;
585 else {
586 rhs = CYGetProperty(context, aggregate, index);
587 if (JSValueIsUndefined(context, rhs))
588 throw CYJSError(context, "unable to extract array value");
589 }
590
591 CYPoolFFI(pool, context, type->data.data.type, field, base, rhs);
592 // XXX: alignment?
593 base += field->size;
594 }
595 } break;
596
597 case sig::pointer_P:
598 *reinterpret_cast<void **>(data) = CYCastPointer<void *>(context, value);
599 break;
600
601 case sig::string_P:
602 _assert(pool != NULL);
603 *reinterpret_cast<const char **>(data) = CYPoolCString(*pool, context, value);
604 break;
605
606 case sig::struct_P: {
607 uint8_t *base(reinterpret_cast<uint8_t *>(data));
608 JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL);
609 for (size_t index(0); index != type->data.signature.count; ++index) {
610 sig::Element *element(&type->data.signature.elements[index]);
611 ffi_type *field(ffi->elements[index]);
612
613 JSValueRef rhs;
614 if (aggregate == NULL)
615 rhs = value;
616 else {
617 rhs = CYGetProperty(context, aggregate, index);
618 if (JSValueIsUndefined(context, rhs)) {
619 if (element->name != NULL)
620 rhs = CYGetProperty(context, aggregate, CYJSString(element->name));
621 else
622 goto undefined;
623 if (JSValueIsUndefined(context, rhs)) undefined:
624 throw CYJSError(context, "unable to extract structure value");
625 }
626 }
627
628 CYPoolFFI(pool, context, element->type, field, base, rhs);
629 // XXX: alignment?
630 base += field->size;
631 }
632 } break;
633
634 case sig::void_P:
635 break;
636
637 default:
638 if (hooks_ != NULL && hooks_->PoolFFI != NULL)
639 if ((*hooks_->PoolFFI)(pool, context, type, ffi, data, value))
640 return;
641
642 CYThrow("unimplemented signature code: '%c''\n", type->primitive);
643 }
644 }
645
646 JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) {
647 switch (type->primitive) {
648 case sig::boolean_P:
649 return CYCastJSValue(context, *reinterpret_cast<bool *>(data));
650
651 #define CYFromFFI_(primitive, native) \
652 case sig::primitive ## _P: \
653 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
654
655 CYFromFFI_(uchar, unsigned char)
656 CYFromFFI_(char, char)
657 CYFromFFI_(ushort, unsigned short)
658 CYFromFFI_(short, short)
659 CYFromFFI_(ulong, unsigned long)
660 CYFromFFI_(long, long)
661 CYFromFFI_(uint, unsigned int)
662 CYFromFFI_(int, int)
663 CYFromFFI_(ulonglong, unsigned long long)
664 CYFromFFI_(longlong, long long)
665 CYFromFFI_(float, float)
666 CYFromFFI_(double, double)
667
668 case sig::array_P:
669 if (void *pointer = data)
670 return CYMakePointer(context, pointer, type->data.data.size, type->data.data.type, NULL, owner);
671 else goto null;
672
673 case sig::pointer_P:
674 if (void *pointer = *reinterpret_cast<void **>(data))
675 return CYMakePointer(context, pointer, _not(size_t), type->data.data.type, NULL, owner);
676 else goto null;
677
678 case sig::string_P:
679 if (char *utf8 = *reinterpret_cast<char **>(data))
680 return CYCastJSValue(context, utf8);
681 else goto null;
682
683 case sig::struct_P:
684 return CYMakeStruct(context, data, type, ffi, owner);
685 case sig::void_P:
686 return CYJSUndefined(context);
687
688 null:
689 return CYJSNull(context);
690 default:
691 if (hooks_ != NULL && hooks_->FromFFI != NULL)
692 if (JSValueRef value = (*hooks_->FromFFI)(context, type, ffi, data, initialize, owner))
693 return value;
694
695 CYThrow("unimplemented signature code: '%c''\n", type->primitive);
696 }
697 }
698
699 void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)) {
700 Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg));
701
702 JSContextRef context(internal->context_);
703
704 size_t count(internal->cif_.nargs);
705 JSValueRef values[count];
706
707 for (size_t index(0); index != count; ++index)
708 values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]);
709
710 JSValueRef value(adapter(context, count, values, internal->function_));
711 CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value);
712 }
713
714 static JSValueRef FunctionAdapter_(JSContextRef context, size_t count, JSValueRef values[], JSObjectRef function) {
715 return CYCallAsFunction(context, function, NULL, count, values);
716 }
717
718 static void FunctionClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
719 CYExecuteClosure(cif, result, arguments, arg, &FunctionAdapter_);
720 }
721
722 Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const char *type, void (*callback)(ffi_cif *, void *, void **, void *)) {
723 // XXX: in case of exceptions this will leak
724 // XXX: in point of fact, this may /need/ to leak :(
725 Closure_privateData *internal(new Closure_privateData(context, function, type));
726
727 #if defined(__APPLE__) && defined(__arm__)
728 void *executable;
729 ffi_closure *writable(reinterpret_cast<ffi_closure *>(ffi_closure_alloc(sizeof(ffi_closure), &executable)));
730
731 ffi_status status(ffi_prep_closure_loc(writable, &internal->cif_, callback, internal, executable));
732 _assert(status == FFI_OK);
733
734 internal->value_ = executable;
735 #else
736 ffi_closure *closure((ffi_closure *) _syscall(mmap(
737 NULL, sizeof(ffi_closure),
738 PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
739 -1, 0
740 )));
741
742 ffi_status status(ffi_prep_closure(closure, &internal->cif_, callback, internal));
743 _assert(status == FFI_OK);
744
745 _syscall(mprotect(closure, sizeof(*closure), PROT_READ | PROT_EXEC));
746
747 internal->value_ = closure;
748 #endif
749
750 return internal;
751 }
752
753 static JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) {
754 Closure_privateData *internal(CYMakeFunctor_(context, function, type, &FunctionClosure_));
755 JSObjectRef object(JSObjectMake(context, Functor_, internal));
756 // XXX: see above notes about needing to leak
757 JSValueProtect(CYGetJSContext(context), object);
758 return object;
759 }
760
761 JSObjectRef CYGetCachedObject(JSContextRef context, JSStringRef name) {
762 return CYCastJSObject(context, CYGetProperty(context, CYCastJSObject(context, CYGetProperty(context, CYGetGlobalObject(context), cy_s)), name));
763 }
764
765 static JSObjectRef CYMakeFunctor(JSContextRef context, JSValueRef value, const char *type) {
766 JSObjectRef Function(CYGetCachedObject(context, CYJSString("Function")));
767
768 bool function(_jsccall(JSValueIsInstanceOfConstructor, context, value, Function));
769 if (function) {
770 JSObjectRef function(CYCastJSObject(context, value));
771 return CYMakeFunctor(context, function, type);
772 } else {
773 void (*function)()(CYCastPointer<void (*)()>(context, value));
774 return CYMakeFunctor(context, function, type);
775 }
776 }
777
778 static bool Index_(CYPool &pool, JSContextRef context, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) {
779 Type_privateData *typical(internal->type_);
780 sig::Type *type(typical->type_);
781 if (type == NULL)
782 return false;
783
784 const char *name(CYPoolCString(pool, context, property));
785 size_t length(strlen(name));
786 double number(CYCastDouble(name, length));
787
788 size_t count(type->data.signature.count);
789
790 if (std::isnan(number)) {
791 if (property == NULL)
792 return false;
793
794 sig::Element *elements(type->data.signature.elements);
795
796 for (size_t local(0); local != count; ++local) {
797 sig::Element *element(&elements[local]);
798 if (element->name != NULL && strcmp(name, element->name) == 0) {
799 index = local;
800 goto base;
801 }
802 }
803
804 return false;
805 } else {
806 index = static_cast<ssize_t>(number);
807 if (index != number || index < 0 || static_cast<size_t>(index) >= count)
808 return false;
809 }
810
811 base:
812 ffi_type **elements(typical->GetFFI()->elements);
813
814 base = reinterpret_cast<uint8_t *>(internal->value_);
815 for (ssize_t local(0); local != index; ++local)
816 base += elements[local]->size;
817
818 return true;
819 }
820
821 static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
822 CYPool pool;
823 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
824
825 if (JSStringIsEqual(property, length_s))
826 return internal->length_ == _not(size_t) ? CYJSUndefined(context) : CYCastJSValue(context, internal->length_);
827
828 Type_privateData *typical(internal->type_);
829
830 if (typical->type_ == NULL)
831 return NULL;
832
833 ssize_t offset;
834 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
835 offset = 0;
836 else if (!CYGetOffset(pool, context, property, offset))
837 return NULL;
838
839 ffi_type *ffi(typical->GetFFI());
840
841 uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_));
842 base += ffi->size * offset;
843
844 JSObjectRef owner(internal->GetOwner() ?: object);
845 return CYFromFFI(context, typical->type_, ffi, base, false, owner);
846 } CYCatch(NULL) }
847
848 static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
849 CYPool pool;
850 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
851 Type_privateData *typical(internal->type_);
852
853 if (typical->type_ == NULL)
854 return false;
855
856 ssize_t offset;
857 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
858 offset = 0;
859 else if (!CYGetOffset(pool, context, property, offset))
860 return false;
861
862 ffi_type *ffi(typical->GetFFI());
863
864 uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_));
865 base += ffi->size * offset;
866
867 CYPoolFFI(NULL, context, typical->type_, ffi, base, value);
868 return true;
869 } CYCatch(false) }
870
871 static JSValueRef Struct_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
872 Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(_this)));
873 Type_privateData *typical(internal->type_);
874 return CYMakePointer(context, internal->value_, _not(size_t), typical->type_, typical->ffi_, _this);
875 } CYCatch(NULL) }
876
877 static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
878 CYPool pool;
879 Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object)));
880 Type_privateData *typical(internal->type_);
881
882 ssize_t index;
883 uint8_t *base;
884
885 if (!Index_(pool, context, internal, property, index, base))
886 return NULL;
887
888 JSObjectRef owner(internal->GetOwner() ?: object);
889
890 return CYFromFFI(context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, false, owner);
891 } CYCatch(NULL) }
892
893 static bool Struct_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
894 CYPool pool;
895 Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object)));
896 Type_privateData *typical(internal->type_);
897
898 ssize_t index;
899 uint8_t *base;
900
901 if (!Index_(pool, context, internal, property, index, base))
902 return false;
903
904 CYPoolFFI(NULL, context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, value);
905 return true;
906 } CYCatch(false) }
907
908 static void Struct_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
909 Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object)));
910 Type_privateData *typical(internal->type_);
911 sig::Type *type(typical->type_);
912
913 if (type == NULL)
914 return;
915
916 size_t count(type->data.signature.count);
917 sig::Element *elements(type->data.signature.elements);
918
919 char number[32];
920
921 for (size_t index(0); index != count; ++index) {
922 const char *name;
923 name = elements[index].name;
924
925 if (name == NULL) {
926 sprintf(number, "%zu", index);
927 name = number;
928 }
929
930 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
931 }
932 }
933
934 JSValueRef CYCallFunction(CYPool &pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, sig::Signature *signature, ffi_cif *cif, void (*function)()) {
935 if (setups + count != signature->count - 1)
936 throw CYJSError(context, "incorrect number of arguments to ffi function");
937
938 size_t size(setups + count);
939 void *values[size];
940 memcpy(values, setup, sizeof(void *) * setups);
941
942 for (size_t index(setups); index != size; ++index) {
943 sig::Element *element(&signature->elements[index + 1]);
944 ffi_type *ffi(cif->arg_types[index]);
945 // XXX: alignment?
946 values[index] = new(pool) uint8_t[ffi->size];
947 CYPoolFFI(&pool, context, element->type, ffi, values[index], arguments[index - setups]);
948 }
949
950 uint8_t value[cif->rtype->size];
951
952 if (hooks_ != NULL && hooks_->CallFunction != NULL)
953 (*hooks_->CallFunction)(context, cif, function, value, values);
954 else
955 ffi_call(cif, function, value, values);
956
957 return CYFromFFI(context, signature->elements[0].type, cif->rtype, value, initialize);
958 }
959
960 static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
961 CYPool pool;
962 cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(object)));
963 return CYCallFunction(pool, context, 0, NULL, count, arguments, false, &internal->signature_, &internal->cif_, internal->GetValue());
964 } CYCatch(NULL) }
965
966 JSObjectRef CYMakeType(JSContextRef context, const char *encoding) {
967 Type_privateData *internal(new Type_privateData(encoding));
968 return JSObjectMake(context, Type_privateData::Class_, internal);
969 }
970
971 JSObjectRef CYMakeType(JSContextRef context, sig::Type *type) {
972 Type_privateData *internal(new Type_privateData(type));
973 return JSObjectMake(context, Type_privateData::Class_, internal);
974 }
975
976 JSObjectRef CYMakeType(JSContextRef context, sig::Signature *signature) {
977 CYPool pool;
978
979 sig::Type type;
980 type.name = NULL;
981 type.flags = 0;
982
983 type.primitive = sig::function_P;
984 sig::Copy(pool, type.data.signature, *signature);
985
986 return CYMakeType(context, &type);
987 }
988
989 static JSValueRef All_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
990 JSObjectRef global(CYGetGlobalObject(context));
991 JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript"))));
992 JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls"))));
993
994 for (size_t i(0), count(CYArrayLength(context, alls)); i != count; ++i)
995 if (JSObjectRef space = CYCastJSObject(context, CYArrayGet(context, alls, count - i - 1)))
996 if (JSValueRef value = CYGetProperty(context, space, property))
997 if (!JSValueIsUndefined(context, value))
998 return value;
999
1000 CYPool pool;
1001 CYUTF8String name(CYPoolUTF8String(pool, context, property));
1002
1003 size_t length(name.size);
1004 char keyed[length + 2];
1005 memcpy(keyed + 1, name.data, length + 1);
1006
1007 static const char *modes = "0124";
1008 for (size_t i(0); i != 4; ++i) {
1009 char mode(modes[i]);
1010 keyed[0] = mode;
1011
1012 if (CYBridgeEntry *entry = CYBridgeHash(keyed, length + 1))
1013 switch (mode) {
1014 case '0':
1015 return JSEvaluateScript(CYGetJSContext(context), CYJSString(entry->value_), NULL, NULL, 0, NULL);
1016
1017 case '1':
1018 return CYMakeFunctor(context, name.data, entry->value_, &entry->cache_);
1019
1020 case '2':
1021 if (void *symbol = CYCastSymbol(name.data)) {
1022 // XXX: this is horrendously inefficient
1023 sig::Signature signature;
1024 sig::Parse(pool, &signature, entry->value_, &Structor_);
1025 ffi_cif cif;
1026 sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
1027 return CYFromFFI(context, signature.elements[0].type, cif.rtype, symbol);
1028 } else return NULL;
1029
1030 // XXX: implement case 3
1031 case '4':
1032 return CYMakeType(context, entry->value_);
1033 }
1034 }
1035
1036 return NULL;
1037 } CYCatch(NULL) }
1038
1039 static void All_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1040 JSObjectRef global(CYGetGlobalObject(context));
1041 JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript"))));
1042 JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls"))));
1043
1044 for (size_t i(0), count(CYArrayLength(context, alls)); i != count; ++i)
1045 if (JSObjectRef space = CYCastJSObject(context, CYArrayGet(context, alls, count - i - 1))) {
1046 JSPropertyNameArrayRef subset(JSObjectCopyPropertyNames(context, space));
1047 for (size_t index(0), count(JSPropertyNameArrayGetCount(subset)); index != count; ++index)
1048 JSPropertyNameAccumulatorAddName(names, JSPropertyNameArrayGetNameAtIndex(subset, index));
1049 JSPropertyNameArrayRelease(subset);
1050 }
1051 }
1052
1053 static JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1054 if (count != 2)
1055 throw CYJSError(context, "incorrect number of arguments to Pointer constructor");
1056
1057 CYPool pool;
1058
1059 void *value(CYCastPointer<void *>(context, arguments[0]));
1060 const char *type(CYPoolCString(pool, context, arguments[1]));
1061
1062 sig::Signature signature;
1063 sig::Parse(pool, &signature, type, &Structor_);
1064
1065 return CYMakePointer(context, value, _not(size_t), signature.elements[0].type, NULL, NULL);
1066 } CYCatch(NULL) }
1067
1068 static JSObjectRef Type_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1069 if (count != 1)
1070 throw CYJSError(context, "incorrect number of arguments to Type constructor");
1071 CYPool pool;
1072 const char *type(CYPoolCString(pool, context, arguments[0]));
1073 return CYMakeType(context, type);
1074 } CYCatch(NULL) }
1075
1076 static JSValueRef Type_callAsFunction_arrayOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1077 if (count != 1)
1078 throw CYJSError(context, "incorrect number of arguments to Type.arrayOf");
1079 Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
1080
1081 CYPool pool;
1082 size_t index(CYGetIndex(pool, context, CYJSString(context, arguments[0])));
1083 if (index == _not(size_t))
1084 throw CYJSError(context, "invalid array size used with Type.arrayOf");
1085
1086 sig::Type type;
1087 type.name = NULL;
1088 type.flags = 0;
1089
1090 type.primitive = sig::array_P;
1091 type.data.data.type = internal->type_;
1092 type.data.data.size = index;
1093
1094 return CYMakeType(context, &type);
1095 } CYCatch(NULL) }
1096
1097 static JSValueRef Type_callAsFunction_constant(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1098 if (count != 0)
1099 throw CYJSError(context, "incorrect number of arguments to Type.constant");
1100 Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
1101
1102 sig::Type type(*internal->type_);
1103 type.flags |= JOC_TYPE_CONST;
1104 return CYMakeType(context, &type);
1105 } CYCatch(NULL) }
1106
1107 static JSValueRef Type_callAsFunction_functionWith(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1108 Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
1109
1110 CYPool pool;
1111
1112 sig::Type type;
1113 type.name = NULL;
1114 type.flags = 0;
1115
1116 type.primitive = sig::function_P;
1117 type.data.signature.elements = new(pool) sig::Element[1 + count];
1118 type.data.signature.count = 1 + count;
1119
1120 type.data.signature.elements[0].name = NULL;
1121 type.data.signature.elements[0].type = internal->type_;
1122 type.data.signature.elements[0].offset = _not(size_t);
1123
1124 for (size_t i(0); i != count; ++i) {
1125 sig::Element &element(type.data.signature.elements[i + 1]);
1126 element.name = NULL;
1127 element.offset = _not(size_t);
1128
1129 JSObjectRef object(CYCastJSObject(context, arguments[i]));
1130 _assert(JSValueIsObjectOfClass(context, object, Type_privateData::Class_));
1131 Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
1132
1133 element.type = internal->type_;
1134 }
1135
1136 return CYMakeType(context, &type);
1137 } CYCatch(NULL) }
1138
1139 static JSValueRef Type_callAsFunction_pointerTo(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1140 if (count != 0)
1141 throw CYJSError(context, "incorrect number of arguments to Type.pointerTo");
1142 Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
1143
1144 sig::Type type;
1145 type.name = NULL;
1146
1147 if (internal->type_->primitive == sig::char_P) {
1148 type.flags = internal->type_->flags;
1149 type.primitive = sig::string_P;
1150 type.data.data.type = NULL;
1151 type.data.data.size = 0;
1152 } else {
1153 type.flags = 0;
1154 type.primitive = sig::pointer_P;
1155 type.data.data.type = internal->type_;
1156 type.data.data.size = 0;
1157 }
1158
1159 return CYMakeType(context, &type);
1160 } CYCatch(NULL) }
1161
1162 static JSValueRef Type_callAsFunction_withName(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1163 if (count != 1)
1164 throw CYJSError(context, "incorrect number of arguments to Type.withName");
1165 Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
1166
1167 CYPool pool;
1168 const char *name(CYPoolCString(pool, context, arguments[0]));
1169
1170 sig::Type type(*internal->type_);
1171 type.name = name;
1172 return CYMakeType(context, &type);
1173 } CYCatch(NULL) }
1174
1175 static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1176 if (count != 1)
1177 throw CYJSError(context, "incorrect number of arguments to type cast function");
1178 Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
1179
1180 if (internal->type_->primitive == sig::function_P)
1181 return CYMakeFunctor(context, reinterpret_cast<void (*)()>(static_cast<uintptr_t>(CYCastDouble(context, arguments[0]))), internal->type_->data.signature);
1182
1183 sig::Type *type(internal->type_);
1184 ffi_type *ffi(internal->GetFFI());
1185 // XXX: alignment?
1186 uint8_t value[ffi->size];
1187 CYPool pool;
1188 CYPoolFFI(&pool, context, type, ffi, value, arguments[0]);
1189 return CYFromFFI(context, type, ffi, value);
1190 } CYCatch(NULL) }
1191
1192 static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1193 if (count != 0)
1194 throw CYJSError(context, "incorrect number of arguments to Type allocator");
1195 Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
1196
1197 sig::Type *type(internal->type_);
1198 size_t length;
1199
1200 if (type->primitive != sig::array_P)
1201 length = _not(size_t);
1202 else {
1203 length = type->data.data.size;
1204 type = type->data.data.type;
1205 }
1206
1207 void *value(malloc(internal->GetFFI()->size));
1208 return CYMakePointer(context, value, length, type, NULL, NULL);
1209 } CYCatch(NULL) }
1210
1211 static JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1212 if (count != 2)
1213 throw CYJSError(context, "incorrect number of arguments to Functor constructor");
1214 CYPool pool;
1215 const char *type(CYPoolCString(pool, context, arguments[1]));
1216 return CYMakeFunctor(context, arguments[0], type);
1217 } CYCatch(NULL) }
1218
1219 static JSValueRef CYValue_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1220 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
1221 return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_));
1222 } CYCatch(NULL) }
1223
1224 static JSValueRef CYValue_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
1225 return CYValue_callAsFunction_valueOf(context, object, _this, count, arguments, exception);
1226 }
1227
1228 static JSValueRef CYValue_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1229 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
1230 char string[32];
1231 sprintf(string, "%p", internal->value_);
1232 return CYCastJSValue(context, string);
1233 } CYCatch(NULL) }
1234
1235 static JSValueRef Pointer_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1236 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(_this)));
1237 if (internal->length_ != _not(size_t)) {
1238 JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array_prototype")));
1239 JSObjectRef toCYON(CYCastJSObject(context, CYGetProperty(context, Array, toCYON_s)));
1240 return CYCallAsFunction(context, toCYON, _this, count, arguments);
1241 } else {
1242 char string[32];
1243 sprintf(string, "%p", internal->value_);
1244 return CYCastJSValue(context, string);
1245 }
1246 } CYCatch(NULL) }
1247
1248 static JSValueRef Functor_getProperty_type(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1249 cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(object)));
1250 return CYMakeType(context, &internal->signature_);
1251 } CYCatch(NULL) }
1252
1253 static JSValueRef Type_getProperty_alignment(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1254 Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
1255 return CYCastJSValue(context, internal->GetFFI()->alignment);
1256 } CYCatch(NULL) }
1257
1258 static JSValueRef Type_getProperty_name(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1259 Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
1260 return CYCastJSValue(context, internal->type_->name);
1261 } CYCatch(NULL) }
1262
1263 static JSValueRef Type_getProperty_size(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1264 Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
1265 return CYCastJSValue(context, internal->GetFFI()->size);
1266 } CYCatch(NULL) }
1267
1268 static JSValueRef Type_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1269 Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
1270 CYPool pool;
1271 const char *type(sig::Unparse(pool, internal->type_));
1272 return CYCastJSValue(context, CYJSString(type));
1273 } CYCatch(NULL) }
1274
1275 static JSValueRef Type_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1276 Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
1277 CYLocalPool pool;
1278 std::ostringstream out;
1279 CYOptions options;
1280 CYOutput output(out, options);
1281 (new(pool) CYEncodedType(Decode(pool, internal->type_)))->Output(output, CYNoFlags);
1282 return CYCastJSValue(context, CYJSString(out.str().c_str()));
1283 } CYCatch(NULL) }
1284
1285 static JSValueRef Type_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
1286 return Type_callAsFunction_toString(context, object, _this, count, arguments, exception);
1287 }
1288
1289 static JSStaticFunction Pointer_staticFunctions[4] = {
1290 {"toCYON", &Pointer_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1291 {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1292 {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1293 {NULL, NULL, 0}
1294 };
1295
1296 static JSStaticFunction Struct_staticFunctions[2] = {
1297 {"$cya", &Struct_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1298 {NULL, NULL, 0}
1299 };
1300
1301 static JSStaticFunction Functor_staticFunctions[4] = {
1302 {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1303 {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1304 {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1305 {NULL, NULL, 0}
1306 };
1307
1308 namespace cy {
1309 JSStaticFunction const * const Functor::StaticFunctions = Functor_staticFunctions;
1310 }
1311
1312 static JSStaticValue Functor_staticValues[2] = {
1313 {"type", &Functor_getProperty_type, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1314 {NULL, NULL, NULL, 0}
1315 };
1316
1317 static JSStaticValue Type_staticValues[4] = {
1318 {"alignment", &Type_getProperty_alignment, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1319 {"name", &Type_getProperty_name, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1320 {"size", &Type_getProperty_size, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1321 {NULL, NULL, NULL, 0}
1322 };
1323
1324 static JSStaticFunction Type_staticFunctions[9] = {
1325 {"arrayOf", &Type_callAsFunction_arrayOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1326 {"constant", &Type_callAsFunction_constant, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1327 {"functionWith", &Type_callAsFunction_functionWith, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1328 {"pointerTo", &Type_callAsFunction_pointerTo, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1329 {"withName", &Type_callAsFunction_withName, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1330 {"toCYON", &Type_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1331 {"toJSON", &Type_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1332 {"toString", &Type_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
1333 {NULL, NULL, 0}
1334 };
1335
1336 static JSObjectRef (*JSObjectMakeArray$)(JSContextRef, size_t, const JSValueRef[], JSValueRef *);
1337
1338 void CYSetArgs(int argc, const char *argv[]) {
1339 JSContextRef context(CYGetJSContext());
1340 JSValueRef args[argc];
1341 for (int i(0); i != argc; ++i)
1342 args[i] = CYCastJSValue(context, argv[i]);
1343
1344 JSObjectRef array;
1345 if (JSObjectMakeArray$ != NULL)
1346 array = _jsccall(*JSObjectMakeArray$, context, argc, args);
1347 else {
1348 JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array")));
1349 JSValueRef value(CYCallAsFunction(context, Array, NULL, argc, args));
1350 array = CYCastJSObject(context, value);
1351 }
1352
1353 JSObjectRef System(CYGetCachedObject(context, CYJSString("System")));
1354 CYSetProperty(context, System, CYJSString("args"), array);
1355 }
1356
1357 JSObjectRef CYGetGlobalObject(JSContextRef context) {
1358 return JSContextGetGlobalObject(context);
1359 }
1360
1361 const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) {
1362 JSValueRef exception(NULL);
1363
1364 void *handle;
1365 if (hooks_ != NULL && hooks_->ExecuteStart != NULL)
1366 handle = (*hooks_->ExecuteStart)(context);
1367 else
1368 handle = NULL;
1369
1370 try {
1371
1372 JSValueRef result;
1373 try {
1374 result = JSEvaluateScript(context, CYJSString(code), NULL, NULL, 0, &exception);
1375 } catch (const char *error) {
1376 return error;
1377 }
1378
1379 if (exception != NULL) error:
1380 return CYPoolCString(pool, context, CYJSString(context, exception));
1381
1382 if (JSValueIsUndefined(context, result))
1383 return NULL;
1384
1385 const char *json;
1386 try {
1387 json = CYPoolCCYON(pool, context, result, &exception);
1388 } catch (const char *error) {
1389 return error;
1390 }
1391
1392 if (exception != NULL)
1393 goto error;
1394
1395 CYSetProperty(context, CYGetGlobalObject(context), Result_, result);
1396
1397 return json;
1398
1399 } catch (...) {
1400 if (hooks_ != NULL && hooks_->ExecuteEnd != NULL)
1401 (*hooks_->ExecuteEnd)(context, handle);
1402 throw;
1403 }
1404 }
1405
1406 extern "C" void CydgetSetupContext(JSGlobalContextRef context) {
1407 CYSetupContext(context);
1408 }
1409
1410 static bool initialized_ = false;
1411
1412 void CYInitializeDynamic() {
1413 if (!initialized_)
1414 initialized_ = true;
1415 else return;
1416
1417 JSObjectMakeArray$ = reinterpret_cast<JSObjectRef (*)(JSContextRef, size_t, const JSValueRef[], JSValueRef *)>(dlsym(RTLD_DEFAULT, "JSObjectMakeArray"));
1418
1419 JSClassDefinition definition;
1420
1421 definition = kJSClassDefinitionEmpty;
1422 definition.className = "All";
1423 definition.getProperty = &All_getProperty;
1424 definition.getPropertyNames = &All_getPropertyNames;
1425 All_ = JSClassCreate(&definition);
1426
1427 definition = kJSClassDefinitionEmpty;
1428 definition.className = "Context";
1429 definition.finalize = &CYFinalize;
1430 Context_ = JSClassCreate(&definition);
1431
1432 definition = kJSClassDefinitionEmpty;
1433 definition.className = "Functor";
1434 definition.staticFunctions = cy::Functor::StaticFunctions;
1435 definition.staticValues = Functor_staticValues;
1436 definition.callAsFunction = &Functor_callAsFunction;
1437 definition.finalize = &CYFinalize;
1438 Functor_ = JSClassCreate(&definition);
1439
1440 definition = kJSClassDefinitionEmpty;
1441 definition.className = "Pointer";
1442 definition.staticFunctions = Pointer_staticFunctions;
1443 definition.getProperty = &Pointer_getProperty;
1444 definition.setProperty = &Pointer_setProperty;
1445 definition.finalize = &CYFinalize;
1446 Pointer_ = JSClassCreate(&definition);
1447
1448 definition = kJSClassDefinitionEmpty;
1449 definition.className = "Struct";
1450 definition.staticFunctions = Struct_staticFunctions;
1451 definition.getProperty = &Struct_getProperty;
1452 definition.setProperty = &Struct_setProperty;
1453 definition.getPropertyNames = &Struct_getPropertyNames;
1454 definition.finalize = &CYFinalize;
1455 Struct_ = JSClassCreate(&definition);
1456
1457 definition = kJSClassDefinitionEmpty;
1458 definition.className = "Type";
1459 definition.staticValues = Type_staticValues;
1460 definition.staticFunctions = Type_staticFunctions;
1461 definition.callAsFunction = &Type_callAsFunction;
1462 definition.callAsConstructor = &Type_callAsConstructor;
1463 definition.finalize = &CYFinalize;
1464 Type_privateData::Class_ = JSClassCreate(&definition);
1465
1466 definition = kJSClassDefinitionEmpty;
1467 definition.className = "Global";
1468 //definition.getProperty = &Global_getProperty;
1469 Global_ = JSClassCreate(&definition);
1470
1471 Array_s = JSStringCreateWithUTF8CString("Array");
1472 cy_s = JSStringCreateWithUTF8CString("$cy");
1473 length_s = JSStringCreateWithUTF8CString("length");
1474 message_s = JSStringCreateWithUTF8CString("message");
1475 name_s = JSStringCreateWithUTF8CString("name");
1476 pop_s = JSStringCreateWithUTF8CString("pop");
1477 prototype_s = JSStringCreateWithUTF8CString("prototype");
1478 push_s = JSStringCreateWithUTF8CString("push");
1479 splice_s = JSStringCreateWithUTF8CString("splice");
1480 toCYON_s = JSStringCreateWithUTF8CString("toCYON");
1481 toJSON_s = JSStringCreateWithUTF8CString("toJSON");
1482 toPointer_s = JSStringCreateWithUTF8CString("toPointer");
1483 toString_s = JSStringCreateWithUTF8CString("toString");
1484
1485 Result_ = JSStringCreateWithUTF8CString("_");
1486
1487 if (hooks_ != NULL && hooks_->Initialize != NULL)
1488 (*hooks_->Initialize)();
1489 }
1490
1491 void CYThrow(JSContextRef context, JSValueRef value) {
1492 if (value != NULL)
1493 throw CYJSError(context, value);
1494 }
1495
1496 const char *CYJSError::PoolCString(CYPool &pool) const {
1497 // XXX: this used to be CYPoolCString
1498 return CYPoolCCYON(pool, context_, value_);
1499 }
1500
1501 JSValueRef CYJSError::CastJSValue(JSContextRef context) const {
1502 // XXX: what if the context is different?
1503 return value_;
1504 }
1505
1506 JSValueRef CYCastJSError(JSContextRef context, const char *message) {
1507 JSObjectRef Error(CYGetCachedObject(context, CYJSString("Error")));
1508 JSValueRef arguments[1] = {CYCastJSValue(context, message)};
1509 return _jsccall(JSObjectCallAsConstructor, context, Error, 1, arguments);
1510 }
1511
1512 JSValueRef CYPoolError::CastJSValue(JSContextRef context) const {
1513 return CYCastJSError(context, message_);
1514 }
1515
1516 CYJSError::CYJSError(JSContextRef context, const char *format, ...) {
1517 _assert(context != NULL);
1518
1519 CYPool pool;
1520
1521 va_list args;
1522 va_start(args, format);
1523 // XXX: there might be a beter way to think about this
1524 const char *message(pool.vsprintf(64, format, args));
1525 va_end(args);
1526
1527 value_ = CYCastJSError(context, message);
1528 }
1529
1530 JSGlobalContextRef CYGetJSContext(JSContextRef context) {
1531 return reinterpret_cast<Context *>(JSObjectGetPrivate(CYCastJSObject(context, CYGetProperty(context, CYGetGlobalObject(context), cy_s))))->context_;
1532 }
1533
1534 extern "C" bool CydgetMemoryParse(const uint16_t **data, size_t *size);
1535
1536 void *CYMapFile(const char *path, size_t *psize) {
1537 int fd;
1538 _syscall(fd = open(path, O_RDONLY));
1539
1540 struct stat stat;
1541 _syscall(fstat(fd, &stat));
1542 size_t size(stat.st_size);
1543
1544 *psize = size;
1545
1546 void *base;
1547 _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0));
1548
1549 _syscall(close(fd));
1550 return base;
1551 }
1552
1553 static void CYRunSetups(JSContextRef context) {
1554 std::string folder("/etc/cycript/setup.d");
1555 DIR *setups(opendir(folder.c_str()));
1556 if (setups == NULL)
1557 return;
1558
1559 for (;;) {
1560 dirent setup;
1561 dirent *result;
1562 _syscall(readdir_r(setups, &setup, &result));
1563
1564 if (result == NULL)
1565 break;
1566 _assert(result == &setup);
1567
1568 const char *name(setup.d_name);
1569 size_t length(strlen(name));
1570 if (length < 4)
1571 continue;
1572
1573 if (name[0] == '.')
1574 continue;
1575 if (memcmp(name + length - 3, ".cy", 3) != 0)
1576 continue;
1577
1578 std::string script(folder + "/" + name);
1579 CYUTF8String utf8;
1580 utf8.data = reinterpret_cast<char *>(CYMapFile(script.c_str(), &utf8.size));
1581
1582 CYPool pool;
1583 CYUTF16String utf16(CYPoolUTF16String(pool, utf8));
1584 munmap(const_cast<char *>(utf8.data), utf8.size);
1585
1586 if (CydgetMemoryParse(&utf16.data, &utf16.size))
1587 CYExecute(context, pool, CYPoolUTF8String(pool, utf16));
1588 free(const_cast<uint16_t *>(utf16.data));
1589 }
1590
1591 _syscall(closedir(setups));
1592 }
1593
1594 extern "C" void CYSetupContext(JSGlobalContextRef context) {
1595 CYInitializeDynamic();
1596
1597 JSObjectRef global(CYGetGlobalObject(context));
1598
1599 JSObjectRef cy(JSObjectMake(context, Context_, new Context(context)));
1600 CYSetProperty(context, global, cy_s, cy, kJSPropertyAttributeDontEnum);
1601
1602 /* Cache Globals {{{ */
1603 JSObjectRef Array(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array"))));
1604 CYSetProperty(context, cy, CYJSString("Array"), Array);
1605
1606 JSObjectRef Array_prototype(CYCastJSObject(context, CYGetProperty(context, Array, prototype_s)));
1607 CYSetProperty(context, cy, CYJSString("Array_prototype"), Array_prototype);
1608
1609 JSObjectRef Error(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Error"))));
1610 CYSetProperty(context, cy, CYJSString("Error"), Error);
1611
1612 JSObjectRef Function(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function"))));
1613 CYSetProperty(context, cy, CYJSString("Function"), Function);
1614
1615 JSObjectRef Function_prototype(CYCastJSObject(context, CYGetProperty(context, Function, prototype_s)));
1616 CYSetProperty(context, cy, CYJSString("Function_prototype"), Function_prototype);
1617
1618 JSObjectRef Object(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Object"))));
1619 CYSetProperty(context, cy, CYJSString("Object"), Object);
1620
1621 JSObjectRef Object_prototype(CYCastJSObject(context, CYGetProperty(context, Object, prototype_s)));
1622 CYSetProperty(context, cy, CYJSString("Object_prototype"), Object_prototype);
1623
1624 JSObjectRef String(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("String"))));
1625 CYSetProperty(context, cy, CYJSString("String"), String);
1626
1627 JSObjectRef String_prototype(CYCastJSObject(context, CYGetProperty(context, String, prototype_s)));
1628 CYSetProperty(context, cy, CYJSString("String_prototype"), String_prototype);
1629 /* }}} */
1630
1631 CYSetProperty(context, Array_prototype, toCYON_s, &Array_callAsFunction_toCYON, kJSPropertyAttributeDontEnum);
1632 CYSetProperty(context, String_prototype, toCYON_s, &String_callAsFunction_toCYON, kJSPropertyAttributeDontEnum);
1633
1634 JSObjectRef cycript(JSObjectMake(context, NULL, NULL));
1635 CYSetProperty(context, global, CYJSString("Cycript"), cycript);
1636 CYSetProperty(context, cycript, CYJSString("gc"), &Cycript_gc_callAsFunction);
1637
1638 JSObjectRef Functor(JSObjectMakeConstructor(context, Functor_, &Functor_new));
1639 JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Functor, prototype_s)), Function_prototype);
1640 CYSetProperty(context, cycript, CYJSString("Functor"), Functor);
1641
1642 CYSetProperty(context, cycript, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new));
1643 CYSetProperty(context, cycript, CYJSString("Type"), JSObjectMakeConstructor(context, Type_privateData::Class_, &Type_new));
1644
1645 JSObjectRef all(JSObjectMake(context, All_, NULL));
1646 CYSetProperty(context, cycript, CYJSString("all"), all);
1647
1648 JSObjectRef alls(_jsccall(JSObjectCallAsConstructor, context, Array, 0, NULL));
1649 CYSetProperty(context, cycript, CYJSString("alls"), alls);
1650
1651 if (true) {
1652 JSObjectRef last(NULL), curr(global);
1653
1654 goto next; for (JSValueRef next;;) {
1655 if (JSValueIsNull(context, next))
1656 break;
1657 last = curr;
1658 curr = CYCastJSObject(context, next);
1659 next:
1660 next = JSObjectGetPrototype(context, curr);
1661 }
1662
1663 JSObjectSetPrototype(context, last, all);
1664 }
1665
1666 CYSetProperty(context, global, CYJSString("$cyq"), &$cyq, kJSPropertyAttributeDontEnum);
1667
1668 JSObjectRef System(JSObjectMake(context, NULL, NULL));
1669 CYSetProperty(context, cy, CYJSString("System"), System);
1670
1671 CYSetProperty(context, global, CYJSString("system"), System);
1672 CYSetProperty(context, System, CYJSString("args"), CYJSNull(context));
1673 //CYSetProperty(context, System, CYJSString("global"), global);
1674 CYSetProperty(context, System, CYJSString("print"), &System_print);
1675
1676 if (CYBridgeEntry *entry = CYBridgeHash("1dlerror", 8))
1677 entry->cache_ = new cy::Functor(entry->value_, reinterpret_cast<void (*)()>(&dlerror));
1678
1679 if (hooks_ != NULL && hooks_->SetupContext != NULL)
1680 (*hooks_->SetupContext)(context);
1681
1682 CYArrayPush(context, alls, cycript);
1683
1684 CYRunSetups(context);
1685 }
1686
1687 JSGlobalContextRef CYGetJSContext() {
1688 CYInitializeDynamic();
1689
1690 static JSGlobalContextRef context_;
1691
1692 if (context_ == NULL) {
1693 context_ = JSGlobalContextCreate(Global_);
1694 CYSetupContext(context_);
1695 }
1696
1697 return context_;
1698 }