1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
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.
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.
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/>.
22 #include "Internal.hpp"
29 #include "cycript.hpp"
31 #include "sig/parse.hpp"
32 #include "sig/ffi_type.hpp"
34 #include "Pooling.hpp"
35 #include "Execute.hpp"
51 #include "JavaScript.hpp"
54 struct CYHooks
*hooks_
;
56 /* JavaScript Properties {{{ */
57 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) {
58 return _jsccall(JSObjectGetPropertyAtIndex
, context
, object
, index
);
61 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
62 return _jsccall(JSObjectGetProperty
, context
, object
, name
);
65 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) {
66 _jsccall(JSObjectSetPropertyAtIndex
, context
, object
, index
, value
);
69 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) {
70 _jsccall(JSObjectSetProperty
, context
, object
, name
, value
, attributes
);
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
);
77 /* JavaScript Strings {{{ */
78 JSStringRef
CYCopyJSString(const char *value
) {
79 return value
== NULL
? NULL
: JSStringCreateWithUTF8CString(value
);
82 JSStringRef
CYCopyJSString(JSStringRef value
) {
83 return value
== NULL
? NULL
: JSStringRetain(value
);
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
);
91 JSStringRef
CYCopyJSString(JSContextRef context
, JSValueRef value
) {
92 if (JSValueIsNull(context
, value
))
94 return _jsccall(JSValueToStringCopy
, context
, value
);
97 static CYUTF16String
CYCastUTF16String(JSStringRef value
) {
98 return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
));
101 CYUTF8String
CYPoolUTF8String(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
102 return CYPoolUTF8String(pool
, CYCastUTF16String(value
));
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
);
111 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
112 return JSValueIsNull(context
, value
) ? NULL
: CYPoolCString(pool
, context
, CYJSString(context
, value
));
115 /* Index Offsets {{{ */
116 size_t CYGetIndex(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
117 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
121 static JSClassRef All_
;
122 static JSClassRef Context_
;
124 static JSClassRef Global_
;
125 static JSClassRef Pointer_
;
126 static JSClassRef Struct_
;
131 JSStringRef length_s
;
132 JSStringRef message_s
;
135 JSStringRef prototype_s
;
137 JSStringRef splice_s
;
138 JSStringRef toCYON_s
;
139 JSStringRef toJSON_s
;
140 JSStringRef toPointer_s
;
141 JSStringRef toString_s
;
143 static JSStringRef Result_
;
145 void CYFinalize(JSObjectRef object
) {
146 CYData
*internal(reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
)));
147 _assert(internal
->count_
!= _not(unsigned));
148 if (--internal
->count_
== 0)
152 void Structor_(CYPool
&pool
, sig::Type
*&type
) {
154 type
->primitive
== sig::pointer_P
&&
155 type
->data
.data
.type
!= NULL
&&
156 type
->data
.data
.type
->primitive
== sig::struct_P
&&
157 type
->data
.data
.type
->name
!= NULL
&&
158 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
160 type
->primitive
= sig::typename_P
;
161 type
->data
.data
.type
= NULL
;
165 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
168 size_t length(strlen(type
->name
));
169 char keyed
[length
+ 2];
170 memcpy(keyed
+ 1, type
->name
, length
+ 1);
172 static const char *modes
= "34";
173 for (size_t i(0); i
!= 2; ++i
) {
177 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
180 sig::Parse(pool
, &type
->data
.signature
, entry
->value_
, &Structor_
);
184 sig::Signature signature
;
185 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
186 type
= signature
.elements
[0].type
;
192 JSClassRef
Type_privateData::Class_
;
197 JSGlobalContextRef context_
;
199 Context(JSGlobalContextRef context
) :
208 Type_privateData
*type_
;
211 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type
*type
) :
212 CYOwned(value
, context
, owner
),
213 type_(new(*pool_
) Type_privateData(type
)),
219 struct Struct_privateData
:
222 Type_privateData
*type_
;
224 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
225 CYOwned(NULL
, context
, owner
)
230 typedef std::map
<const char *, Type_privateData
*, CYCStringLess
> TypeMap
;
231 static TypeMap Types_
;
233 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
234 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
235 CYPool
&pool(*internal
->pool_
);
236 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
237 internal
->type_
= typical
;
240 internal
->value_
= data
;
242 size_t size(typical
->GetFFI()->size
);
243 void *copy(internal
->pool_
->malloc
<void>(size
));
244 memcpy(copy
, data
, size
);
245 internal
->value_
= copy
;
248 return JSObjectMake(context
, Struct_
, internal
);
251 static void *CYCastSymbol(const char *name
) {
252 return dlsym(RTLD_DEFAULT
, name
);
255 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
256 return JSValueMakeBoolean(context
, value
);
259 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
260 return JSValueMakeNumber(context
, value
);
263 #define CYCastJSValue_(Type_) \
264 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
265 return JSValueMakeNumber(context, static_cast<double>(value)); \
269 CYCastJSValue_(unsigned int)
270 CYCastJSValue_(long int)
271 CYCastJSValue_(long unsigned int)
272 CYCastJSValue_(long long int)
273 CYCastJSValue_(long long unsigned int)
275 JSValueRef
CYJSUndefined(JSContextRef context
) {
276 return JSValueMakeUndefined(context
);
279 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
280 return _jsccall(JSValueToNumber
, context
, value
);
283 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
284 return JSValueToBoolean(context
, value
);
287 JSValueRef
CYJSNull(JSContextRef context
) {
288 return JSValueMakeNull(context
);
291 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
292 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
295 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
296 return CYCastJSValue(context
, CYJSString(value
));
299 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
300 return _jsccall(JSValueToObject
, context
, value
);
303 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
304 return _jsccall(JSObjectCallAsFunction
, context
, function
, _this
, count
, arguments
);
307 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
308 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
311 size_t CYArrayLength(JSContextRef context
, JSObjectRef array
) {
312 return CYCastDouble(context
, CYGetProperty(context
, array
, length_s
));
315 JSValueRef
CYArrayGet(JSContextRef context
, JSObjectRef array
, size_t index
) {
316 return _jsccall(JSObjectGetPropertyAtIndex
, context
, array
, index
);
319 void CYArrayPush(JSContextRef context
, JSObjectRef array
, JSValueRef value
) {
320 JSValueRef arguments
[1];
321 arguments
[0] = value
;
322 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
323 _jsccall(JSObjectCallAsFunction
, context
, CYCastJSObject(context
, CYGetProperty(context
, Array
, push_s
)), array
, 1, arguments
);
326 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
331 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
334 return CYJSUndefined(context
);
337 static size_t Nonce_(0);
339 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
341 const char *name(pool
.strcat(CYPoolCString(pool
, context
, arguments
[0]), pool
.itoa(Nonce_
++), NULL
));
342 return CYCastJSValue(context
, name
);
345 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
346 JSGarbageCollect(context
);
347 return CYJSUndefined(context
);
350 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
351 switch (JSType type
= JSValueGetType(context
, value
)) {
352 case kJSTypeUndefined
:
357 return CYCastBool(context
, value
) ? "true" : "false";
359 case kJSTypeNumber
: {
360 std::ostringstream str
;
361 CYNumerify(str
, CYCastDouble(context
, value
));
362 std::string
value(str
.str());
363 return pool
.strmemdup(value
.c_str(), value
.size());
366 case kJSTypeString
: {
367 std::ostringstream str
;
368 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
369 CYStringify(str
, string
.data
, string
.size
);
370 std::string
value(str
.str());
371 return pool
.strmemdup(value
.c_str(), value
.size());
375 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
377 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
381 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
382 return _jsccall(CYPoolCCYON
, pool
, context
, value
);
385 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSObjectRef object
) {
386 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
387 if (CYIsCallable(context
, toCYON
)) {
388 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
389 _assert(value
!= NULL
);
390 return CYPoolCString(pool
, context
, value
);
393 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
394 if (CYIsCallable(context
, toJSON
)) {
395 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
396 return _jsccall(CYPoolCCYON
, pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
));
399 if (JSObjectIsFunction(context
, object
)) {
400 JSValueRef
toString(CYGetProperty(context
, object
, toString_s
));
401 if (CYIsCallable(context
, toString
)) {
402 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
403 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toString
, object
, 1, arguments
));
404 _assert(value
!= NULL
);
405 return CYPoolCString(pool
, context
, value
);
409 std::ostringstream str
;
413 // XXX: this is, sadly, going to leak
414 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
418 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
424 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
425 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
430 CYStringify(str
, string
.data
, string
.size
);
435 JSValueRef
value(CYGetProperty(context
, object
, name
));
436 str
<< CYPoolCCYON(pool
, context
, value
);
437 } catch (const CYException
&error
) {
442 JSPropertyNameArrayRelease(names
);
446 std::string
string(str
.str());
447 return pool
.strmemdup(string
.c_str(), string
.size());
450 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
452 std::ostringstream str
;
456 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
459 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
466 JSValueRef
value(CYGetProperty(context
, _this
, index
));
467 if (!JSValueIsUndefined(context
, value
))
468 str
<< CYPoolCCYON(pool
, context
, value
);
473 } catch (const CYException
&error
) {
480 std::string
value(str
.str());
481 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
484 static JSValueRef
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
486 std::ostringstream str
;
488 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
)));
489 CYStringify(str
, string
.data
, string
.size
);
491 std::string
value(str
.str());
492 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
495 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
496 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
497 return JSObjectMake(context
, Pointer_
, internal
);
500 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const sig::Signature
&signature
) {
501 return JSObjectMake(context
, Functor_
, new cy::Functor(signature
, function
));
504 static JSObjectRef
CYMakeFunctor(JSContextRef context
, const char *symbol
, const char *encoding
, void **cache
) {
505 cy::Functor
*internal
;
507 internal
= reinterpret_cast<cy::Functor
*>(*cache
);
509 void (*function
)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol
)));
510 if (function
== NULL
)
513 internal
= new cy::Functor(encoding
, function
);
518 return JSObjectMake(context
, Functor_
, internal
);
521 static bool CYGetOffset(CYPool
&pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
522 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
525 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
526 switch (JSValueGetType(context
, value
)) {
529 case kJSTypeObject
: {
530 JSObjectRef
object((JSObjectRef
) value
);
531 if (JSValueIsObjectOfClass(context
, value
, Pointer_
)) {
532 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
533 return internal
->value_
;
535 JSValueRef
toPointer(CYGetProperty(context
, object
, toPointer_s
));
536 if (CYIsCallable(context
, toPointer
)) {
537 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
));
538 _assert(value
!= NULL
);
539 return CYCastPointer_(context
, value
);
542 double number(CYCastDouble(context
, value
));
543 if (std::isnan(number
))
544 throw CYJSError(context
, "cannot convert value to pointer");
545 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
549 void CYPoolFFI(CYPool
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
550 switch (type
->primitive
) {
552 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
555 #define CYPoolFFI_(primitive, native) \
556 case sig::primitive ## _P: \
557 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
560 CYPoolFFI_(uchar
, unsigned char)
561 CYPoolFFI_(char, char)
562 CYPoolFFI_(ushort
, unsigned short)
563 CYPoolFFI_(short, short)
564 CYPoolFFI_(ulong
, unsigned long)
565 CYPoolFFI_(long, long)
566 CYPoolFFI_(uint
, unsigned int)
568 CYPoolFFI_(ulonglong
, unsigned long long)
569 CYPoolFFI_(longlong
, long long)
570 CYPoolFFI_(float, float)
571 CYPoolFFI_(double, double)
574 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
575 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
576 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
577 ffi_type
*field(ffi
->elements
[index
]);
580 if (aggregate
== NULL
)
583 rhs
= CYGetProperty(context
, aggregate
, index
);
584 if (JSValueIsUndefined(context
, rhs
))
585 throw CYJSError(context
, "unable to extract array value");
588 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
595 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
599 _assert(pool
!= NULL
);
600 *reinterpret_cast<const char **>(data
) = CYPoolCString(*pool
, context
, value
);
603 case sig::struct_P
: {
604 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
605 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
606 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
607 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
608 ffi_type
*field(ffi
->elements
[index
]);
611 if (aggregate
== NULL
)
614 rhs
= CYGetProperty(context
, aggregate
, index
);
615 if (JSValueIsUndefined(context
, rhs
)) {
616 if (element
->name
!= NULL
)
617 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
620 if (JSValueIsUndefined(context
, rhs
)) undefined
:
621 throw CYJSError(context
, "unable to extract structure value");
625 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
635 if (hooks_
!= NULL
&& hooks_
->PoolFFI
!= NULL
)
636 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
639 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
643 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
644 switch (type
->primitive
) {
646 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
648 #define CYFromFFI_(primitive, native) \
649 case sig::primitive ## _P: \
650 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
652 CYFromFFI_(uchar, unsigned char)
653 CYFromFFI_(char, char)
654 CYFromFFI_(ushort
, unsigned short)
655 CYFromFFI_(short, short)
656 CYFromFFI_(ulong
, unsigned long)
657 CYFromFFI_(long, long)
658 CYFromFFI_(uint
, unsigned int)
660 CYFromFFI_(ulonglong
, unsigned long long)
661 CYFromFFI_(longlong
, long long)
662 CYFromFFI_(float, float)
663 CYFromFFI_(double, double)
666 if (void *pointer
= data
)
667 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
671 if (void *pointer
= *reinterpret_cast<void **>(data
))
672 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
676 if (char *utf8
= *reinterpret_cast<char **>(data
))
677 return CYCastJSValue(context
, utf8
);
681 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
683 return CYJSUndefined(context
);
686 return CYJSNull(context
);
688 if (hooks_
!= NULL
&& hooks_
->FromFFI
!= NULL
)
689 if (JSValueRef value
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
692 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
696 void CYExecuteClosure(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
, JSValueRef (*adapter
)(JSContextRef
, size_t, JSValueRef
[], JSObjectRef
)) {
697 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
699 JSContextRef
context(internal
->context_
);
701 size_t count(internal
->cif_
.nargs
);
702 JSValueRef values
[count
];
704 for (size_t index(0); index
!= count
; ++index
)
705 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
707 JSValueRef
value(adapter(context
, count
, values
, internal
->function_
));
708 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
711 static JSValueRef
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
) {
712 return CYCallAsFunction(context
, function
, NULL
, count
, values
);
715 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
716 CYExecuteClosure(cif
, result
, arguments
, arg
, &FunctionAdapter_
);
719 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
720 // XXX: in case of exceptions this will leak
721 // XXX: in point of fact, this may /need/ to leak :(
722 Closure_privateData
*internal(new Closure_privateData(context
, function
, signature
));
724 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
726 ffi_closure
*writable(reinterpret_cast<ffi_closure
*>(ffi_closure_alloc(sizeof(ffi_closure
), &executable
)));
728 ffi_status
status(ffi_prep_closure_loc(writable
, &internal
->cif_
, callback
, internal
, executable
));
729 _assert(status
== FFI_OK
);
731 internal
->value_
= executable
;
733 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
734 NULL
, sizeof(ffi_closure
),
735 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
739 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
740 _assert(status
== FFI_OK
);
742 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
744 internal
->value_
= closure
;
750 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
) {
751 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, signature
, &FunctionClosure_
));
752 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
753 // XXX: see above notes about needing to leak
754 JSValueProtect(CYGetJSContext(context
), object
);
758 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
759 return CYCastJSObject(context
, CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
));
762 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const sig::Signature
&signature
) {
763 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
765 bool function(_jsccall(JSValueIsInstanceOfConstructor
, context
, value
, Function
));
767 JSObjectRef
function(CYCastJSObject(context
, value
));
768 return CYMakeFunctor(context
, function
, signature
);
770 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
771 return CYMakeFunctor(context
, function
, signature
);
775 static bool Index_(CYPool
&pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
776 Type_privateData
*typical(internal
->type_
);
777 sig::Type
*type(typical
->type_
);
781 const char *name(CYPoolCString(pool
, context
, property
));
782 size_t length(strlen(name
));
783 double number(CYCastDouble(name
, length
));
785 size_t count(type
->data
.signature
.count
);
787 if (std::isnan(number
)) {
788 if (property
== NULL
)
791 sig::Element
*elements(type
->data
.signature
.elements
);
793 for (size_t local(0); local
!= count
; ++local
) {
794 sig::Element
*element(&elements
[local
]);
795 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
803 index
= static_cast<ssize_t
>(number
);
804 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
809 ffi_type
**elements(typical
->GetFFI()->elements
);
811 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
812 for (ssize_t
local(0); local
!= index
; ++local
)
813 base
+= elements
[local
]->size
;
818 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
820 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
822 if (JSStringIsEqual(property
, length_s
))
823 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
825 Type_privateData
*typical(internal
->type_
);
826 if (typical
->type_
== NULL
)
828 sig::Type
&type(*typical
->type_
);
831 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
833 else if (!CYGetOffset(pool
, context
, property
, offset
))
836 if (type
.primitive
== sig::function_P
)
837 return CYMakeFunctor(context
, reinterpret_cast<void (*)()>(internal
->value_
), type
.data
.signature
);
839 ffi_type
*ffi(typical
->GetFFI());
841 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
842 base
+= ffi
->size
* offset
;
844 JSObjectRef
owner(internal
->GetOwner() ?: object
);
845 return CYFromFFI(context
, &type
, ffi
, base
, false, owner
);
848 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
850 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
851 Type_privateData
*typical(internal
->type_
);
853 if (typical
->type_
== NULL
)
857 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
859 else if (!CYGetOffset(pool
, context
, property
, offset
))
862 ffi_type
*ffi(typical
->GetFFI());
864 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
865 base
+= ffi
->size
* offset
;
867 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
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
);
877 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
879 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
880 Type_privateData
*typical(internal
->type_
);
885 if (!Index_(pool
, context
, internal
, property
, index
, base
))
888 JSObjectRef
owner(internal
->GetOwner() ?: object
);
890 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
893 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
895 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
896 Type_privateData
*typical(internal
->type_
);
901 if (!Index_(pool
, context
, internal
, property
, index
, base
))
904 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
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_
);
916 size_t count(type
->data
.signature
.count
);
917 sig::Element
*elements(type
->data
.signature
.elements
);
921 for (size_t index(0); index
!= count
; ++index
) {
923 name
= elements
[index
].name
;
926 sprintf(number
, "%zu", index
);
930 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
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");
938 size_t size(setups
+ count
);
940 memcpy(values
, setup
, sizeof(void *) * setups
);
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
]);
946 values
[index
] = new(pool
) uint8_t[ffi
->size
];
947 CYPoolFFI(&pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
950 uint8_t value
[cif
->rtype
->size
];
952 if (hooks_
!= NULL
&& hooks_
->CallFunction
!= NULL
)
953 (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
);
955 ffi_call(cif
, function
, value
, values
);
957 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
960 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
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());
966 JSObjectRef
CYMakeType(JSContextRef context
, const char *encoding
) {
967 Type_privateData
*internal(new Type_privateData(encoding
));
968 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
971 JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
972 Type_privateData
*internal(new Type_privateData(type
));
973 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
976 JSObjectRef
CYMakeType(JSContextRef context
, sig::Signature
*signature
) {
983 type
.primitive
= sig::function_P
;
984 sig::Copy(pool
, type
.data
.signature
, *signature
);
986 return CYMakeType(context
, &type
);
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"))));
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
))
1001 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
1003 size_t length(name
.size
);
1004 char keyed
[length
+ 2];
1005 memcpy(keyed
+ 1, name
.data
, length
+ 1);
1007 static const char *modes
= "0124";
1008 for (size_t i(0); i
!= 4; ++i
) {
1009 char mode(modes
[i
]);
1012 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
1015 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
);
1018 return CYMakeFunctor(context
, name
.data
, entry
->value_
, &entry
->cache_
);
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_
);
1026 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1027 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
1030 // XXX: implement case 3
1032 return CYMakeType(context
, entry
->value_
);
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"))));
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
);
1053 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1055 throw CYJSError(context
, "incorrect number of arguments to Pointer constructor");
1059 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1060 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1062 sig::Signature signature
;
1063 sig::Parse(pool
, &signature
, type
, &Structor_
);
1065 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1068 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1070 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1072 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1073 return CYMakeType(context
, type
);
1076 static JSValueRef Type_callAsFunction_$
With(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], sig::Primitive primitive
, JSValueRef
*exception
) { CYTry
{
1077 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1085 type
.primitive
= primitive
;
1086 type
.data
.signature
.elements
= new(pool
) sig::Element
[1 + count
];
1087 type
.data
.signature
.count
= 1 + count
;
1089 type
.data
.signature
.elements
[0].name
= NULL
;
1090 type
.data
.signature
.elements
[0].type
= internal
->type_
;
1091 type
.data
.signature
.elements
[0].offset
= _not(size_t);
1093 for (size_t i(0); i
!= count
; ++i
) {
1094 sig::Element
&element(type
.data
.signature
.elements
[i
+ 1]);
1095 element
.name
= NULL
;
1096 element
.offset
= _not(size_t);
1098 JSObjectRef
object(CYCastJSObject(context
, arguments
[i
]));
1099 _assert(JSValueIsObjectOfClass(context
, object
, Type_privateData::Class_
));
1100 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1102 element
.type
= internal
->type_
;
1105 return CYMakeType(context
, &type
);
1108 static JSValueRef
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1110 throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf");
1111 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1114 size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0])));
1115 if (index
== _not(size_t))
1116 throw CYJSError(context
, "invalid array size used with Type.arrayOf");
1122 type
.primitive
= sig::array_P
;
1123 type
.data
.data
.type
= internal
->type_
;
1124 type
.data
.data
.size
= index
;
1126 return CYMakeType(context
, &type
);
1129 static JSValueRef
Type_callAsFunction_blockWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1130 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::block_P
, exception
);
1133 static JSValueRef
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1135 throw CYJSError(context
, "incorrect number of arguments to Type.constant");
1136 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1138 sig::Type
type(*internal
->type_
);
1139 type
.flags
|= JOC_TYPE_CONST
;
1140 return CYMakeType(context
, &type
);
1143 static JSValueRef
Type_callAsFunction_functionWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1144 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::function_P
, exception
);
1147 static JSValueRef
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1149 throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo");
1150 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1155 if (internal
->type_
->primitive
== sig::char_P
) {
1156 type
.flags
= internal
->type_
->flags
;
1157 type
.primitive
= sig::string_P
;
1158 type
.data
.data
.type
= NULL
;
1159 type
.data
.data
.size
= 0;
1162 type
.primitive
= sig::pointer_P
;
1163 type
.data
.data
.type
= internal
->type_
;
1164 type
.data
.data
.size
= 0;
1167 return CYMakeType(context
, &type
);
1170 static JSValueRef
Type_callAsFunction_withName(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1172 throw CYJSError(context
, "incorrect number of arguments to Type.withName");
1173 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1176 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1178 sig::Type
type(*internal
->type_
);
1180 return CYMakeType(context
, &type
);
1183 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1185 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1186 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1188 if (internal
->type_
->primitive
== sig::function_P
)
1189 return CYMakeFunctor(context
, arguments
[0], internal
->type_
->data
.signature
);
1191 sig::Type
*type(internal
->type_
);
1192 ffi_type
*ffi(internal
->GetFFI());
1194 uint8_t value
[ffi
->size
];
1196 CYPoolFFI(&pool
, context
, type
, ffi
, value
, arguments
[0]);
1197 return CYFromFFI(context
, type
, ffi
, value
);
1200 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1202 throw CYJSError(context
, "incorrect number of arguments to Type allocator");
1203 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1205 sig::Type
*type(internal
->type_
);
1208 if (type
->primitive
!= sig::array_P
)
1209 length
= _not(size_t);
1211 length
= type
->data
.data
.size
;
1212 type
= type
->data
.data
.type
;
1215 void *value(calloc(1, internal
->GetFFI()->size
));
1216 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1219 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1221 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1223 const char *encoding(CYPoolCString(pool
, context
, arguments
[1]));
1224 sig::Signature signature
;
1225 sig::Parse(pool
, &signature
, encoding
, &Structor_
);
1226 return CYMakeFunctor(context
, arguments
[0], signature
);
1229 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1230 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1231 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1234 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1235 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1238 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1239 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1241 sprintf(string
, "%p", internal
->value_
);
1242 return CYCastJSValue(context
, string
);
1245 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1246 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1247 if (internal
->length_
!= _not(size_t)) {
1248 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
1249 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1250 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1253 return CYCastJSValue(context
, pool
.strcat("&", CYPoolCCYON(pool
, context
, CYGetProperty(context
, _this
, cyi_s
)), NULL
));
1254 } catch (const CYException
&e
) {
1256 sprintf(string
, "%p", internal
->value_
);
1257 return CYCastJSValue(context
, string
);
1261 static JSValueRef
Pointer_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1262 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1263 return CYMakeType(context
, internal
->type_
->type_
);
1266 static JSValueRef
Functor_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1267 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1268 return CYMakeType(context
, &internal
->signature_
);
1271 static JSValueRef
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1272 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1273 return CYCastJSValue(context
, internal
->GetFFI()->alignment
);
1276 static JSValueRef
Type_getProperty_name(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1277 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1278 return CYCastJSValue(context
, internal
->type_
->name
);
1281 static JSValueRef
Type_getProperty_size(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1282 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1283 return CYCastJSValue(context
, internal
->GetFFI()->size
);
1286 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1287 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1289 const char *type(sig::Unparse(pool
, internal
->type_
));
1290 return CYCastJSValue(context
, CYJSString(type
));
1293 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1294 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1296 std::ostringstream out
;
1298 CYOutput
output(out
, options
);
1299 (new(pool
) CYEncodedType(Decode(pool
, internal
->type_
)))->Output(output
, CYNoFlags
);
1300 return CYCastJSValue(context
, CYJSString(out
.str().c_str()));
1303 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1304 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1307 static JSStaticFunction Pointer_staticFunctions
[4] = {
1308 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1309 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1310 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1314 static JSStaticValue Pointer_staticValues
[2] = {
1315 {"type", &Pointer_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1316 {NULL
, NULL
, NULL
, 0}
1319 static JSStaticFunction Struct_staticFunctions
[2] = {
1320 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1324 static JSStaticFunction Functor_staticFunctions
[4] = {
1325 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1326 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1327 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1332 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1335 static JSStaticValue Functor_staticValues
[2] = {
1336 {"type", &Functor_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1337 {NULL
, NULL
, NULL
, 0}
1340 static JSStaticValue Type_staticValues
[4] = {
1341 {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1342 {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1343 {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1344 {NULL
, NULL
, NULL
, 0}
1347 static JSStaticFunction Type_staticFunctions
[10] = {
1348 {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1349 {"blockWith", &Type_callAsFunction_blockWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1350 {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1351 {"functionWith", &Type_callAsFunction_functionWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1352 {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1353 {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1354 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1355 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1356 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1360 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1362 void CYSetArgs(int argc
, const char *argv
[]) {
1363 JSContextRef
context(CYGetJSContext());
1364 JSValueRef args
[argc
];
1365 for (int i(0); i
!= argc
; ++i
)
1366 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1369 if (JSObjectMakeArray$
!= NULL
)
1370 array
= _jsccall(*JSObjectMakeArray$
, context
, argc
, args
);
1372 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1373 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1374 array
= CYCastJSObject(context
, value
);
1377 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1378 CYSetProperty(context
, System
, CYJSString("args"), array
);
1381 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1382 return JSContextGetGlobalObject(context
);
1385 const char *CYExecute(JSContextRef context
, CYPool
&pool
, CYUTF8String code
) {
1386 JSValueRef
exception(NULL
);
1389 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1390 handle
= (*hooks_
->ExecuteStart
)(context
);
1398 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1399 } catch (const char *error
) {
1403 if (exception
!= NULL
) error
:
1404 return CYPoolCString(pool
, context
, CYJSString(context
, exception
));
1406 if (JSValueIsUndefined(context
, result
))
1411 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1412 } catch (const char *error
) {
1416 if (exception
!= NULL
)
1419 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1424 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1425 (*hooks_
->ExecuteEnd
)(context
, handle
);
1430 static bool initialized_
= false;
1432 void CYInitializeDynamic() {
1434 initialized_
= true;
1437 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1439 JSClassDefinition definition
;
1441 definition
= kJSClassDefinitionEmpty
;
1442 definition
.className
= "All";
1443 definition
.getProperty
= &All_getProperty
;
1444 definition
.getPropertyNames
= &All_getPropertyNames
;
1445 All_
= JSClassCreate(&definition
);
1447 definition
= kJSClassDefinitionEmpty
;
1448 definition
.className
= "Context";
1449 definition
.finalize
= &CYFinalize
;
1450 Context_
= JSClassCreate(&definition
);
1452 definition
= kJSClassDefinitionEmpty
;
1453 definition
.className
= "Functor";
1454 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1455 definition
.staticValues
= Functor_staticValues
;
1456 definition
.callAsFunction
= &Functor_callAsFunction
;
1457 definition
.finalize
= &CYFinalize
;
1458 Functor_
= JSClassCreate(&definition
);
1460 definition
= kJSClassDefinitionEmpty
;
1461 definition
.className
= "Pointer";
1462 definition
.staticFunctions
= Pointer_staticFunctions
;
1463 definition
.staticValues
= Pointer_staticValues
;
1464 definition
.getProperty
= &Pointer_getProperty
;
1465 definition
.setProperty
= &Pointer_setProperty
;
1466 definition
.finalize
= &CYFinalize
;
1467 Pointer_
= JSClassCreate(&definition
);
1469 definition
= kJSClassDefinitionEmpty
;
1470 definition
.className
= "Struct";
1471 definition
.staticFunctions
= Struct_staticFunctions
;
1472 definition
.getProperty
= &Struct_getProperty
;
1473 definition
.setProperty
= &Struct_setProperty
;
1474 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1475 definition
.finalize
= &CYFinalize
;
1476 Struct_
= JSClassCreate(&definition
);
1478 definition
= kJSClassDefinitionEmpty
;
1479 definition
.className
= "Type";
1480 definition
.staticValues
= Type_staticValues
;
1481 definition
.staticFunctions
= Type_staticFunctions
;
1482 definition
.callAsFunction
= &Type_callAsFunction
;
1483 definition
.callAsConstructor
= &Type_callAsConstructor
;
1484 definition
.finalize
= &CYFinalize
;
1485 Type_privateData::Class_
= JSClassCreate(&definition
);
1487 definition
= kJSClassDefinitionEmpty
;
1488 definition
.className
= "Global";
1489 //definition.getProperty = &Global_getProperty;
1490 Global_
= JSClassCreate(&definition
);
1492 Array_s
= JSStringCreateWithUTF8CString("Array");
1493 cy_s
= JSStringCreateWithUTF8CString("$cy");
1494 cyi_s
= JSStringCreateWithUTF8CString("$cyi");
1495 length_s
= JSStringCreateWithUTF8CString("length");
1496 message_s
= JSStringCreateWithUTF8CString("message");
1497 name_s
= JSStringCreateWithUTF8CString("name");
1498 pop_s
= JSStringCreateWithUTF8CString("pop");
1499 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1500 push_s
= JSStringCreateWithUTF8CString("push");
1501 splice_s
= JSStringCreateWithUTF8CString("splice");
1502 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1503 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1504 toPointer_s
= JSStringCreateWithUTF8CString("toPointer");
1505 toString_s
= JSStringCreateWithUTF8CString("toString");
1507 Result_
= JSStringCreateWithUTF8CString("_");
1509 if (hooks_
!= NULL
&& hooks_
->Initialize
!= NULL
)
1510 (*hooks_
->Initialize
)();
1513 void CYThrow(JSContextRef context
, JSValueRef value
) {
1515 throw CYJSError(context
, value
);
1518 const char *CYJSError::PoolCString(CYPool
&pool
) const {
1519 // XXX: this used to be CYPoolCString
1520 return CYPoolCCYON(pool
, context_
, value_
);
1523 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1524 // XXX: what if the context is different?
1528 JSValueRef
CYCastJSError(JSContextRef context
, const char *message
) {
1529 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString("Error")));
1530 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1531 return _jsccall(JSObjectCallAsConstructor
, context
, Error
, 1, arguments
);
1534 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1535 return CYCastJSError(context
, message_
);
1538 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1539 _assert(context
!= NULL
);
1544 va_start(args
, format
);
1545 // XXX: there might be a beter way to think about this
1546 const char *message(pool
.vsprintf(64, format
, args
));
1549 value_
= CYCastJSError(context
, message
);
1552 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1553 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1556 extern "C" bool CydgetMemoryParse(const uint16_t **data
, size_t *size
);
1558 void *CYMapFile(const char *path
, size_t *psize
) {
1560 _syscall(fd
= open(path
, O_RDONLY
));
1563 _syscall(fstat(fd
, &stat
));
1564 size_t size(stat
.st_size
);
1569 _syscall(base
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, 0));
1571 _syscall(close(fd
));
1575 static void CYRunSetups(JSContextRef context
) {
1576 std::string
folder("/etc/cycript/setup.d");
1577 DIR *setups(opendir(folder
.c_str()));
1584 _syscall(readdir_r(setups
, &setup
, &result
));
1588 _assert(result
== &setup
);
1590 const char *name(setup
.d_name
);
1591 size_t length(strlen(name
));
1597 if (memcmp(name
+ length
- 3, ".cy", 3) != 0)
1600 std::string
script(folder
+ "/" + name
);
1602 utf8
.data
= reinterpret_cast<char *>(CYMapFile(script
.c_str(), &utf8
.size
));
1605 CYUTF16String
utf16(CYPoolUTF16String(pool
, utf8
));
1606 munmap(const_cast<char *>(utf8
.data
), utf8
.size
);
1608 // XXX: this should not be used
1609 CydgetMemoryParse(&utf16
.data
, &utf16
.size
);
1611 CYExecute(context
, pool
, CYPoolUTF8String(pool
, utf16
));
1612 free(const_cast<uint16_t *>(utf16
.data
));
1615 _syscall(closedir(setups
));
1618 static JSValueRef
require(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1619 _assert(count
== 1);
1623 _assert(dladdr(reinterpret_cast<void *>(&require
), &addr
) != 0);
1624 char *lib(pool
.strdup(addr
.dli_fname
));
1626 char *slash(strrchr(lib
, '/'));
1627 _assert(slash
!= NULL
);
1630 CYJSString
property("exports");
1633 const char *path(pool
.strcat(lib
, "/cycript/", CYPoolCString(pool
, context
, arguments
[0]), ".cy", NULL
));
1634 CYJSString
key(path
);
1635 JSObjectRef
modules(CYGetCachedObject(context
, CYJSString("modules")));
1636 JSValueRef
cache(CYGetProperty(context
, modules
, key
));
1638 if (!JSValueIsUndefined(context
, cache
))
1639 module = CYCastJSObject(context
, cache
);
1642 code
.data
= reinterpret_cast<char *>(CYMapFile(path
, &code
.size
));
1644 std::stringstream wrap
;
1645 wrap
<< "(function (exports, require, module) { " << code
<< "\n});";
1646 code
= CYPoolCode(pool
, wrap
.str().c_str());
1648 JSValueRef
value(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0));
1649 JSObjectRef
function(CYCastJSObject(context
, value
));
1651 module = JSObjectMake(context
, NULL
, NULL
);
1652 JSObjectRef
exports(JSObjectMake(context
, NULL
, NULL
));
1653 CYSetProperty(context
, module, property
, exports
);
1655 JSValueRef arguments
[3] = { exports
, JSObjectMakeFunctionWithCallback(context
, CYJSString("require"), &require
), module };
1656 CYCallAsFunction(context
, function
, NULL
, 3, arguments
);
1657 CYSetProperty(context
, modules
, key
, module);
1660 return CYGetProperty(context
, module, property
);
1663 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
1664 CYInitializeDynamic();
1666 JSObjectRef
global(CYGetGlobalObject(context
));
1668 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
1669 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
1671 /* Cache Globals {{{ */
1672 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
1673 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
1675 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
1676 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
1678 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
1679 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
1681 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
1682 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
1684 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
1685 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
1687 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1688 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
1690 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
1691 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
1693 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
1694 CYSetProperty(context
, cy
, CYJSString("String"), String
);
1696 JSObjectRef
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
)));
1697 CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
);
1700 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1701 CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1703 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1704 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1705 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
1707 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1708 JSObjectSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
1709 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
1711 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1712 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1714 JSObjectRef
modules(JSObjectMake(context
, NULL
, NULL
));
1715 CYSetProperty(context
, cy
, CYJSString("modules"), modules
);
1717 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
1718 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
1720 JSObjectRef
alls(_jsccall(JSObjectCallAsConstructor
, context
, Array
, 0, NULL
));
1721 CYSetProperty(context
, cycript
, CYJSString("alls"), alls
);
1724 JSObjectRef
last(NULL
), curr(global
);
1726 goto next
; for (JSValueRef next
;;) {
1727 if (JSValueIsNull(context
, next
))
1730 curr
= CYCastJSObject(context
, next
);
1732 next
= JSObjectGetPrototype(context
, curr
);
1735 JSObjectSetPrototype(context
, last
, all
);
1738 CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
, kJSPropertyAttributeDontEnum
);
1740 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
1741 CYSetProperty(context
, cy
, CYJSString("System"), System
);
1743 CYSetProperty(context
, all
, CYJSString("require"), &require
, kJSPropertyAttributeDontEnum
);
1745 CYSetProperty(context
, global
, CYJSString("system"), System
);
1746 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
1747 //CYSetProperty(context, System, CYJSString("global"), global);
1748 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
1750 if (CYBridgeEntry
*entry
= CYBridgeHash("1dlerror", 8))
1751 entry
->cache_
= new cy::Functor(entry
->value_
, reinterpret_cast<void (*)()>(&dlerror
));
1753 if (hooks_
!= NULL
&& hooks_
->SetupContext
!= NULL
)
1754 (*hooks_
->SetupContext
)(context
);
1756 CYArrayPush(context
, alls
, cycript
);
1758 CYRunSetups(context
);
1761 JSGlobalContextRef
CYGetJSContext() {
1762 CYInitializeDynamic();
1764 static JSGlobalContextRef context_
;
1766 if (context_
== NULL
) {
1767 context_
= JSGlobalContextCreate(Global_
);
1768 CYSetupContext(context_
);