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