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"
50 #include "JavaScript.hpp"
53 struct CYHooks
*hooks_
;
55 /* JavaScript Properties {{{ */
56 bool CYHasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
57 return JSObjectHasProperty(context
, object
, name
);
60 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) {
61 return _jsccall(JSObjectGetPropertyAtIndex
, context
, object
, index
);
64 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
65 return _jsccall(JSObjectGetProperty
, context
, object
, name
);
68 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) {
69 _jsccall(JSObjectSetPropertyAtIndex
, context
, object
, index
, value
);
72 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) {
73 _jsccall(JSObjectSetProperty
, context
, object
, name
, value
, attributes
);
76 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef (*callback
)(JSContextRef
, JSObjectRef
, JSObjectRef
, size_t, const JSValueRef
[], JSValueRef
*), JSPropertyAttributes attributes
) {
77 CYSetProperty(context
, object
, name
, JSObjectMakeFunctionWithCallback(context
, name
, callback
), attributes
);
80 void CYSetPrototype(JSContextRef context
, JSObjectRef object
, JSValueRef value
) {
81 JSObjectSetPrototype(context
, object
, value
);
82 _assert(JSObjectGetPrototype(context
, object
) == value
);
85 /* JavaScript Strings {{{ */
86 JSStringRef
CYCopyJSString(const char *value
) {
87 return value
== NULL
? NULL
: JSStringCreateWithUTF8CString(value
);
90 JSStringRef
CYCopyJSString(JSStringRef value
) {
91 return value
== NULL
? NULL
: JSStringRetain(value
);
94 JSStringRef
CYCopyJSString(CYUTF8String value
) {
95 // XXX: this is very wrong; it needs to convert to UTF16 and then create from there
96 return CYCopyJSString(value
.data
);
99 JSStringRef
CYCopyJSString(JSContextRef context
, JSValueRef value
) {
100 if (JSValueIsNull(context
, value
))
102 return _jsccall(JSValueToStringCopy
, context
, value
);
105 static CYUTF16String
CYCastUTF16String(JSStringRef value
) {
106 return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
));
109 CYUTF8String
CYPoolUTF8String(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
110 return CYPoolUTF8String(pool
, CYCastUTF16String(value
));
113 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
114 CYUTF8String
utf8(CYPoolUTF8String(pool
, context
, value
));
115 _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
);
119 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
120 return JSValueIsNull(context
, value
) ? NULL
: CYPoolCString(pool
, context
, CYJSString(context
, value
));
123 /* Index Offsets {{{ */
124 size_t CYGetIndex(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
125 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
129 static JSClassRef All_
;
130 static JSClassRef Context_
;
132 static JSClassRef Global_
;
133 static JSClassRef Pointer_
;
134 static JSClassRef Struct_
;
139 JSStringRef length_s
;
140 JSStringRef message_s
;
143 JSStringRef prototype_s
;
145 JSStringRef splice_s
;
146 JSStringRef toCYON_s
;
147 JSStringRef toJSON_s
;
148 JSStringRef toPointer_s
;
149 JSStringRef toString_s
;
151 static JSStringRef Result_
;
153 void CYFinalize(JSObjectRef object
) {
154 CYData
*internal(reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
)));
155 _assert(internal
->count_
!= _not(unsigned));
156 if (--internal
->count_
== 0)
160 void Structor_(CYPool
&pool
, sig::Type
*&type
) {
162 type
->primitive
== sig::pointer_P
&&
163 type
->data
.data
.type
->primitive
== sig::struct_P
&&
164 type
->data
.data
.type
->name
!= NULL
&&
165 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
167 type
->primitive
= sig::typename_P
;
168 type
->data
.data
.type
= NULL
;
172 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
175 size_t length(strlen(type
->name
));
176 char keyed
[length
+ 2];
177 memcpy(keyed
+ 1, type
->name
, length
+ 1);
179 static const char *modes
= "34";
180 for (size_t i(0); i
!= 2; ++i
) {
184 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
187 sig::Parse(pool
, &type
->data
.signature
, entry
->value_
, &Structor_
);
191 sig::Signature signature
;
192 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
193 type
= signature
.elements
[0].type
;
199 JSClassRef
Type_privateData::Class_
;
204 JSGlobalContextRef context_
;
206 Context(JSGlobalContextRef context
) :
215 Type_privateData
*type_
;
218 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type
*type
) :
219 CYOwned(value
, context
, owner
),
220 type_(new(*pool_
) Type_privateData(type
)),
226 struct Struct_privateData
:
229 Type_privateData
*type_
;
231 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
232 CYOwned(NULL
, context
, owner
)
237 typedef std::map
<const char *, Type_privateData
*, CYCStringLess
> TypeMap
;
238 static TypeMap Types_
;
240 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
241 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
242 CYPool
&pool(*internal
->pool_
);
243 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
244 internal
->type_
= typical
;
247 internal
->value_
= data
;
249 size_t size(typical
->GetFFI()->size
);
250 void *copy(internal
->pool_
->malloc
<void>(size
));
251 memcpy(copy
, data
, size
);
252 internal
->value_
= copy
;
255 return JSObjectMake(context
, Struct_
, internal
);
258 static void *CYCastSymbol(const char *name
) {
259 return dlsym(RTLD_DEFAULT
, name
);
262 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
263 return JSValueMakeBoolean(context
, value
);
266 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
267 return JSValueMakeNumber(context
, value
);
270 #define CYCastJSValue_(Type_) \
271 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
272 return JSValueMakeNumber(context, static_cast<double>(value)); \
276 CYCastJSValue_(unsigned int)
277 CYCastJSValue_(long int)
278 CYCastJSValue_(long unsigned int)
279 CYCastJSValue_(long long int)
280 CYCastJSValue_(long long unsigned int)
282 JSValueRef
CYJSUndefined(JSContextRef context
) {
283 return JSValueMakeUndefined(context
);
286 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
287 return _jsccall(JSValueToNumber
, context
, value
);
290 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
291 return JSValueToBoolean(context
, value
);
294 JSValueRef
CYJSNull(JSContextRef context
) {
295 return JSValueMakeNull(context
);
298 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
299 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
302 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
303 return CYCastJSValue(context
, CYJSString(value
));
306 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
307 return _jsccall(JSValueToObject
, context
, value
);
310 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
311 return _jsccall(JSObjectCallAsFunction
, context
, function
, _this
, count
, arguments
);
314 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
315 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
318 size_t CYArrayLength(JSContextRef context
, JSObjectRef array
) {
319 return CYCastDouble(context
, CYGetProperty(context
, array
, length_s
));
322 JSValueRef
CYArrayGet(JSContextRef context
, JSObjectRef array
, size_t index
) {
323 return _jsccall(JSObjectGetPropertyAtIndex
, context
, array
, index
);
326 void CYArrayPush(JSContextRef context
, JSObjectRef array
, JSValueRef value
) {
327 JSValueRef arguments
[1];
328 arguments
[0] = value
;
329 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
330 _jsccall(JSObjectCallAsFunction
, context
, CYCastJSObject(context
, CYGetProperty(context
, Array
, push_s
)), array
, 1, arguments
);
333 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
338 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
341 return CYJSUndefined(context
);
344 static size_t Nonce_(0);
346 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
348 const char *name(pool
.strcat(CYPoolCString(pool
, context
, arguments
[0]), pool
.itoa(Nonce_
++), NULL
));
349 return CYCastJSValue(context
, name
);
352 static void (*JSSynchronousGarbageCollectForDebugging$
)(JSContextRef
);
354 void CYGarbageCollect(JSContextRef context
) {
355 (JSSynchronousGarbageCollectForDebugging$
?: &JSGarbageCollect
)(context
);
358 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
359 CYGarbageCollect(context
);
360 return CYJSUndefined(context
);
363 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
364 switch (JSType type
= JSValueGetType(context
, value
)) {
365 case kJSTypeUndefined
:
370 return CYCastBool(context
, value
) ? "true" : "false";
372 case kJSTypeNumber
: {
373 std::ostringstream str
;
374 CYNumerify(str
, CYCastDouble(context
, value
));
375 std::string
value(str
.str());
376 return pool
.strmemdup(value
.c_str(), value
.size());
379 case kJSTypeString
: {
380 std::ostringstream str
;
381 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
382 CYStringify(str
, string
.data
, string
.size
);
383 std::string
value(str
.str());
384 return pool
.strmemdup(value
.c_str(), value
.size());
388 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
390 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
394 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
395 return _jsccall(CYPoolCCYON
, pool
, context
, value
);
398 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSObjectRef object
) {
399 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
400 if (CYIsCallable(context
, toCYON
)) {
401 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
402 _assert(value
!= NULL
);
403 return CYPoolCString(pool
, context
, value
);
406 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
407 if (CYIsCallable(context
, toJSON
)) {
408 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
409 return _jsccall(CYPoolCCYON
, pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
));
412 if (JSObjectIsFunction(context
, object
)) {
413 JSValueRef
toString(CYGetProperty(context
, object
, toString_s
));
414 if (CYIsCallable(context
, toString
)) {
415 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
416 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toString
, object
, 1, arguments
));
417 _assert(value
!= NULL
);
418 return CYPoolCString(pool
, context
, value
);
422 std::ostringstream str
;
426 // XXX: this is, sadly, going to leak
427 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
431 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
437 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
438 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
443 CYStringify(str
, string
.data
, string
.size
);
448 JSValueRef
value(CYGetProperty(context
, object
, name
));
449 str
<< CYPoolCCYON(pool
, context
, value
);
450 } catch (const CYException
&error
) {
455 JSPropertyNameArrayRelease(names
);
459 std::string
string(str
.str());
460 return pool
.strmemdup(string
.c_str(), string
.size());
463 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
465 std::ostringstream str
;
469 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
472 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
479 JSValueRef
value(CYGetProperty(context
, _this
, index
));
480 if (!JSValueIsUndefined(context
, value
))
481 str
<< CYPoolCCYON(pool
, context
, value
);
486 } catch (const CYException
&error
) {
493 std::string
value(str
.str());
494 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
497 static JSValueRef
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
499 std::ostringstream str
;
501 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
)));
502 CYStringify(str
, string
.data
, string
.size
);
504 std::string
value(str
.str());
505 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
508 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
509 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
510 return JSObjectMake(context
, Pointer_
, internal
);
513 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const sig::Signature
&signature
) {
514 return JSObjectMake(context
, Functor_
, new cy::Functor(signature
, function
));
517 static JSObjectRef
CYMakeFunctor(JSContextRef context
, const char *symbol
, const char *encoding
, void **cache
) {
518 cy::Functor
*internal
;
520 internal
= reinterpret_cast<cy::Functor
*>(*cache
);
522 void (*function
)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol
)));
523 if (function
== NULL
)
526 internal
= new cy::Functor(encoding
, function
);
531 return JSObjectMake(context
, Functor_
, internal
);
534 static bool CYGetOffset(CYPool
&pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
535 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
538 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
539 switch (JSValueGetType(context
, value
)) {
542 case kJSTypeObject
: {
543 JSObjectRef
object((JSObjectRef
) value
);
544 if (JSValueIsObjectOfClass(context
, value
, Pointer_
)) {
545 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
546 return internal
->value_
;
548 JSValueRef
toPointer(CYGetProperty(context
, object
, toPointer_s
));
549 if (CYIsCallable(context
, toPointer
)) {
550 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
));
551 _assert(value
!= NULL
);
552 return CYCastPointer_(context
, value
);
555 double number(CYCastDouble(context
, value
));
556 if (std::isnan(number
))
557 throw CYJSError(context
, "cannot convert value to pointer");
558 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
562 void CYPoolFFI(CYPool
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
563 switch (type
->primitive
) {
565 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
568 #define CYPoolFFI_(primitive, native) \
569 case sig::primitive ## _P: \
570 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
573 CYPoolFFI_(uchar
, unsigned char)
574 CYPoolFFI_(char, char)
575 CYPoolFFI_(ushort
, unsigned short)
576 CYPoolFFI_(short, short)
577 CYPoolFFI_(ulong
, unsigned long)
578 CYPoolFFI_(long, long)
579 CYPoolFFI_(uint
, unsigned int)
581 CYPoolFFI_(ulonglong
, unsigned long long)
582 CYPoolFFI_(longlong
, long long)
583 CYPoolFFI_(float, float)
584 CYPoolFFI_(double, double)
587 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
588 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
589 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
590 ffi_type
*field(ffi
->elements
[index
]);
593 if (aggregate
== NULL
)
596 rhs
= CYGetProperty(context
, aggregate
, index
);
597 if (JSValueIsUndefined(context
, rhs
))
598 throw CYJSError(context
, "unable to extract array value");
601 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
608 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
612 _assert(pool
!= NULL
);
613 *reinterpret_cast<const char **>(data
) = CYPoolCString(*pool
, context
, value
);
616 case sig::struct_P
: {
617 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
618 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
619 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
620 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
621 ffi_type
*field(ffi
->elements
[index
]);
624 if (aggregate
== NULL
)
627 rhs
= CYGetProperty(context
, aggregate
, index
);
628 if (JSValueIsUndefined(context
, rhs
)) {
629 if (element
->name
!= NULL
)
630 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
633 if (JSValueIsUndefined(context
, rhs
)) undefined
:
634 throw CYJSError(context
, "unable to extract structure value");
638 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
648 if (hooks_
!= NULL
&& hooks_
->PoolFFI
!= NULL
)
649 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
652 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
656 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
657 switch (type
->primitive
) {
659 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
661 #define CYFromFFI_(primitive, native) \
662 case sig::primitive ## _P: \
663 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
665 CYFromFFI_(uchar, unsigned char)
666 CYFromFFI_(char, char)
667 CYFromFFI_(ushort
, unsigned short)
668 CYFromFFI_(short, short)
669 CYFromFFI_(ulong
, unsigned long)
670 CYFromFFI_(long, long)
671 CYFromFFI_(uint
, unsigned int)
673 CYFromFFI_(ulonglong
, unsigned long long)
674 CYFromFFI_(longlong
, long long)
675 CYFromFFI_(float, float)
676 CYFromFFI_(double, double)
679 if (void *pointer
= data
)
680 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
684 if (void *pointer
= *reinterpret_cast<void **>(data
))
685 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
689 if (char *utf8
= *reinterpret_cast<char **>(data
))
690 return CYCastJSValue(context
, utf8
);
694 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
696 return CYJSUndefined(context
);
699 return CYJSNull(context
);
701 if (hooks_
!= NULL
&& hooks_
->FromFFI
!= NULL
)
702 if (JSValueRef value
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
705 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
709 void CYExecuteClosure(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
, JSValueRef (*adapter
)(JSContextRef
, size_t, JSValueRef
[], JSObjectRef
)) {
710 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
712 JSContextRef
context(internal
->context_
);
714 size_t count(internal
->cif_
.nargs
);
715 JSValueRef values
[count
];
717 for (size_t index(0); index
!= count
; ++index
)
718 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
720 JSValueRef
value(adapter(context
, count
, values
, internal
->function_
));
721 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
724 static JSValueRef
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
) {
725 return CYCallAsFunction(context
, function
, NULL
, count
, values
);
728 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
729 CYExecuteClosure(cif
, result
, arguments
, arg
, &FunctionAdapter_
);
732 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
733 // XXX: in case of exceptions this will leak
734 // XXX: in point of fact, this may /need/ to leak :(
735 Closure_privateData
*internal(new Closure_privateData(context
, function
, signature
));
737 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
739 ffi_closure
*writable(reinterpret_cast<ffi_closure
*>(ffi_closure_alloc(sizeof(ffi_closure
), &executable
)));
741 ffi_status
status(ffi_prep_closure_loc(writable
, &internal
->cif_
, callback
, internal
, executable
));
742 _assert(status
== FFI_OK
);
744 internal
->value_
= executable
;
746 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
747 NULL
, sizeof(ffi_closure
),
748 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
752 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
753 _assert(status
== FFI_OK
);
755 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
757 internal
->value_
= closure
;
763 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
) {
764 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, signature
, &FunctionClosure_
));
765 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
766 // XXX: see above notes about needing to leak
767 JSValueProtect(CYGetJSContext(context
), object
);
771 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
772 return CYCastJSObject(context
, CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
));
775 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const sig::Signature
&signature
) {
776 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
778 bool function(_jsccall(JSValueIsInstanceOfConstructor
, context
, value
, Function
));
780 JSObjectRef
function(CYCastJSObject(context
, value
));
781 return CYMakeFunctor(context
, function
, signature
);
783 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
784 return CYMakeFunctor(context
, function
, signature
);
788 static bool Index_(CYPool
&pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
789 Type_privateData
*typical(internal
->type_
);
790 sig::Type
*type(typical
->type_
);
794 const char *name(CYPoolCString(pool
, context
, property
));
795 size_t length(strlen(name
));
796 double number(CYCastDouble(name
, length
));
798 size_t count(type
->data
.signature
.count
);
800 if (std::isnan(number
)) {
801 if (property
== NULL
)
804 sig::Element
*elements(type
->data
.signature
.elements
);
806 for (size_t local(0); local
!= count
; ++local
) {
807 sig::Element
*element(&elements
[local
]);
808 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
816 index
= static_cast<ssize_t
>(number
);
817 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
822 ffi_type
**elements(typical
->GetFFI()->elements
);
824 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
825 for (ssize_t
local(0); local
!= index
; ++local
)
826 base
+= elements
[local
]->size
;
831 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
833 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
835 if (JSStringIsEqual(property
, length_s
))
836 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
838 Type_privateData
*typical(internal
->type_
);
839 if (typical
->type_
== NULL
)
841 sig::Type
&type(*typical
->type_
);
844 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
846 else if (!CYGetOffset(pool
, context
, property
, offset
))
849 if (type
.primitive
== sig::function_P
)
850 return CYMakeFunctor(context
, reinterpret_cast<void (*)()>(internal
->value_
), type
.data
.signature
);
852 ffi_type
*ffi(typical
->GetFFI());
854 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
855 base
+= ffi
->size
* offset
;
857 JSObjectRef
owner(internal
->GetOwner() ?: object
);
858 return CYFromFFI(context
, &type
, ffi
, base
, false, owner
);
861 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
863 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
864 Type_privateData
*typical(internal
->type_
);
866 if (typical
->type_
== NULL
)
870 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
872 else if (!CYGetOffset(pool
, context
, property
, offset
))
875 ffi_type
*ffi(typical
->GetFFI());
877 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
878 base
+= ffi
->size
* offset
;
880 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
884 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
885 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
886 Type_privateData
*typical(internal
->type_
);
887 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
890 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
892 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
893 Type_privateData
*typical(internal
->type_
);
898 if (!Index_(pool
, context
, internal
, property
, index
, base
))
901 JSObjectRef
owner(internal
->GetOwner() ?: object
);
903 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
906 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
908 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
909 Type_privateData
*typical(internal
->type_
);
914 if (!Index_(pool
, context
, internal
, property
, index
, base
))
917 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
921 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
922 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
923 Type_privateData
*typical(internal
->type_
);
924 sig::Type
*type(typical
->type_
);
929 size_t count(type
->data
.signature
.count
);
930 sig::Element
*elements(type
->data
.signature
.elements
);
934 for (size_t index(0); index
!= count
; ++index
) {
936 name
= elements
[index
].name
;
939 sprintf(number
, "%zu", index
);
943 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
947 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
)()) {
948 if (setups
+ count
!= signature
->count
- 1)
949 throw CYJSError(context
, "incorrect number of arguments to ffi function");
951 size_t size(setups
+ count
);
953 memcpy(values
, setup
, sizeof(void *) * setups
);
955 for (size_t index(setups
); index
!= size
; ++index
) {
956 sig::Element
*element(&signature
->elements
[index
+ 1]);
957 ffi_type
*ffi(cif
->arg_types
[index
]);
959 values
[index
] = new(pool
) uint8_t[ffi
->size
];
960 CYPoolFFI(&pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
963 uint8_t value
[cif
->rtype
->size
];
965 if (hooks_
!= NULL
&& hooks_
->CallFunction
!= NULL
)
966 (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
);
968 ffi_call(cif
, function
, value
, values
);
970 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
973 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
975 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
976 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
979 JSObjectRef
CYMakeType(JSContextRef context
, const char *encoding
) {
980 Type_privateData
*internal(new Type_privateData(encoding
));
981 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
984 JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
985 Type_privateData
*internal(new Type_privateData(type
));
986 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
989 JSObjectRef
CYMakeType(JSContextRef context
, sig::Signature
*signature
) {
996 type
.primitive
= sig::function_P
;
997 sig::Copy(pool
, type
.data
.signature
, *signature
);
999 return CYMakeType(context
, &type
);
1002 static bool All_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
1003 JSObjectRef
global(CYGetGlobalObject(context
));
1004 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1005 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1007 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1008 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1009 if (CYHasProperty(context
, space
, property
))
1013 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
1015 size_t length(name
.size
);
1016 char keyed
[length
+ 2];
1017 memcpy(keyed
+ 1, name
.data
, length
+ 1);
1019 static const char *modes
= "0124";
1020 for (size_t i(0); i
!= 4; ++i
) {
1021 keyed
[0] = modes
[i
];
1022 if (CYBridgeHash(keyed
, length
+ 1) != NULL
)
1029 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1030 JSObjectRef
global(CYGetGlobalObject(context
));
1031 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1032 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1034 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1035 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1036 if (JSValueRef value
= CYGetProperty(context
, space
, property
))
1037 if (!JSValueIsUndefined(context
, value
))
1041 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
1043 size_t length(name
.size
);
1044 char keyed
[length
+ 2];
1045 memcpy(keyed
+ 1, name
.data
, length
+ 1);
1047 static const char *modes
= "0124";
1048 for (size_t i(0); i
!= 4; ++i
) {
1049 char mode(modes
[i
]);
1052 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
1055 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
);
1058 return CYMakeFunctor(context
, name
.data
, entry
->value_
, &entry
->cache_
);
1061 if (void *symbol
= CYCastSymbol(name
.data
)) {
1062 // XXX: this is horrendously inefficient
1063 sig::Signature signature
;
1064 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
1066 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1067 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
1070 // XXX: implement case 3
1072 return CYMakeType(context
, entry
->value_
);
1079 static void All_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1080 JSObjectRef
global(CYGetGlobalObject(context
));
1081 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1082 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1084 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1085 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1))) {
1086 JSPropertyNameArrayRef
subset(JSObjectCopyPropertyNames(context
, space
));
1087 for (size_t index(0), count(JSPropertyNameArrayGetCount(subset
)); index
!= count
; ++index
)
1088 JSPropertyNameAccumulatorAddName(names
, JSPropertyNameArrayGetNameAtIndex(subset
, index
));
1089 JSPropertyNameArrayRelease(subset
);
1093 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1095 throw CYJSError(context
, "incorrect number of arguments to Pointer constructor");
1099 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1100 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1102 sig::Signature signature
;
1103 sig::Parse(pool
, &signature
, type
, &Structor_
);
1105 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1108 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1110 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1112 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1113 return CYMakeType(context
, type
);
1116 static JSValueRef Type_callAsFunction_$
With(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], sig::Primitive primitive
, JSValueRef
*exception
) { CYTry
{
1117 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1125 type
.primitive
= primitive
;
1126 type
.data
.signature
.elements
= new(pool
) sig::Element
[1 + count
];
1127 type
.data
.signature
.count
= 1 + count
;
1129 type
.data
.signature
.elements
[0].name
= NULL
;
1130 type
.data
.signature
.elements
[0].type
= internal
->type_
;
1131 type
.data
.signature
.elements
[0].offset
= _not(size_t);
1133 for (size_t i(0); i
!= count
; ++i
) {
1134 sig::Element
&element(type
.data
.signature
.elements
[i
+ 1]);
1135 element
.name
= NULL
;
1136 element
.offset
= _not(size_t);
1138 JSObjectRef
object(CYCastJSObject(context
, arguments
[i
]));
1139 _assert(JSValueIsObjectOfClass(context
, object
, Type_privateData::Class_
));
1140 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1142 element
.type
= internal
->type_
;
1145 return CYMakeType(context
, &type
);
1148 static JSValueRef
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1150 throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf");
1151 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1154 size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0])));
1155 if (index
== _not(size_t))
1156 throw CYJSError(context
, "invalid array size used with Type.arrayOf");
1162 type
.primitive
= sig::array_P
;
1163 type
.data
.data
.type
= internal
->type_
;
1164 type
.data
.data
.size
= index
;
1166 return CYMakeType(context
, &type
);
1169 static JSValueRef
Type_callAsFunction_blockWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1170 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::block_P
, exception
);
1173 static JSValueRef
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1175 throw CYJSError(context
, "incorrect number of arguments to Type.constant");
1176 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1178 sig::Type
type(*internal
->type_
);
1179 type
.flags
|= JOC_TYPE_CONST
;
1180 return CYMakeType(context
, &type
);
1183 static JSValueRef
Type_callAsFunction_long(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.long");
1186 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1188 sig::Type
type(*internal
->type_
);
1190 switch (type
.primitive
) {
1191 case sig::short_P
: type
.primitive
= sig::int_P
; break;
1192 case sig::int_P
: type
.primitive
= sig::long_P
; break;
1193 case sig::long_P
: type
.primitive
= sig::longlong_P
; break;
1194 default: throw CYJSError(context
, "invalid type argument to Type.long");
1197 return CYMakeType(context
, &type
);
1200 static JSValueRef
Type_callAsFunction_short(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1202 throw CYJSError(context
, "incorrect number of arguments to Type.short");
1203 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1205 sig::Type
type(*internal
->type_
);
1207 switch (type
.primitive
) {
1208 case sig::int_P
: type
.primitive
= sig::short_P
; break;
1209 case sig::long_P
: type
.primitive
= sig::int_P
; break;
1210 case sig::longlong_P
: type
.primitive
= sig::long_P
; break;
1211 default: throw CYJSError(context
, "invalid type argument to Type.short");
1214 return CYMakeType(context
, &type
);
1217 static JSValueRef
Type_callAsFunction_signed(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1219 throw CYJSError(context
, "incorrect number of arguments to Type.signed");
1220 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1222 sig::Type
type(*internal
->type_
);
1224 switch (type
.primitive
) {
1225 case sig::char_P
: case sig::uchar_P
: type
.primitive
= sig::char_P
; break;
1226 case sig::short_P
: case sig::ushort_P
: type
.primitive
= sig::short_P
; break;
1227 case sig::int_P
: case sig::uint_P
: type
.primitive
= sig::int_P
; break;
1228 case sig::long_P
: case sig::ulong_P
: type
.primitive
= sig::long_P
; break;
1229 case sig::longlong_P
: case sig::ulonglong_P
: type
.primitive
= sig::longlong_P
; break;
1230 default: throw CYJSError(context
, "invalid type argument to Type.signed");
1233 return CYMakeType(context
, &type
);
1236 static JSValueRef
Type_callAsFunction_unsigned(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1238 throw CYJSError(context
, "incorrect number of arguments to Type.unsigned");
1239 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1241 sig::Type
type(*internal
->type_
);
1243 switch (type
.primitive
) {
1244 case sig::char_P
: case sig::uchar_P
: type
.primitive
= sig::uchar_P
; break;
1245 case sig::short_P
: case sig::ushort_P
: type
.primitive
= sig::ushort_P
; break;
1246 case sig::int_P
: case sig::uint_P
: type
.primitive
= sig::uint_P
; break;
1247 case sig::long_P
: case sig::ulong_P
: type
.primitive
= sig::ulong_P
; break;
1248 case sig::longlong_P
: case sig::ulonglong_P
: type
.primitive
= sig::ulonglong_P
; break;
1249 default: throw CYJSError(context
, "invalid type argument to Type.unsigned");
1252 return CYMakeType(context
, &type
);
1255 static JSValueRef
Type_callAsFunction_functionWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1256 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::function_P
, exception
);
1259 static JSValueRef
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1261 throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo");
1262 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1267 if (internal
->type_
->primitive
== sig::char_P
) {
1268 type
.flags
= internal
->type_
->flags
;
1269 type
.primitive
= sig::string_P
;
1270 type
.data
.data
.type
= NULL
;
1271 type
.data
.data
.size
= 0;
1274 type
.primitive
= sig::pointer_P
;
1275 type
.data
.data
.type
= internal
->type_
;
1276 type
.data
.data
.size
= 0;
1279 return CYMakeType(context
, &type
);
1282 static JSValueRef
Type_callAsFunction_withName(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1284 throw CYJSError(context
, "incorrect number of arguments to Type.withName");
1285 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1288 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1290 sig::Type
type(*internal
->type_
);
1292 return CYMakeType(context
, &type
);
1295 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1297 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1298 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1300 if (internal
->type_
->primitive
== sig::function_P
)
1301 return CYMakeFunctor(context
, arguments
[0], internal
->type_
->data
.signature
);
1303 sig::Type
*type(internal
->type_
);
1304 ffi_type
*ffi(internal
->GetFFI());
1306 uint8_t value
[ffi
->size
];
1308 CYPoolFFI(&pool
, context
, type
, ffi
, value
, arguments
[0]);
1309 return CYFromFFI(context
, type
, ffi
, value
);
1312 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1314 throw CYJSError(context
, "incorrect number of arguments to Type allocator");
1315 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1317 sig::Type
*type(internal
->type_
);
1320 if (type
->primitive
!= sig::array_P
)
1321 length
= _not(size_t);
1323 length
= type
->data
.data
.size
;
1324 type
= type
->data
.data
.type
;
1327 void *value(calloc(1, internal
->GetFFI()->size
));
1328 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1331 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1333 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1335 const char *encoding(CYPoolCString(pool
, context
, arguments
[1]));
1336 sig::Signature signature
;
1337 sig::Parse(pool
, &signature
, encoding
, &Structor_
);
1338 return CYMakeFunctor(context
, arguments
[0], signature
);
1341 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1342 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1343 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1346 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1347 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1350 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1351 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1353 sprintf(string
, "%p", internal
->value_
);
1354 return CYCastJSValue(context
, string
);
1357 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1358 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1359 if (internal
->length_
!= _not(size_t)) {
1360 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
1361 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1362 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1363 } else if (internal
->type_
->type_
== NULL
) pointer
: {
1365 sprintf(string
, "%p", internal
->value_
);
1366 return CYCastJSValue(context
, string
);
1368 JSValueRef
value(CYGetProperty(context
, _this
, cyi_s
));
1369 if (JSValueIsUndefined(context
, value
))
1372 return CYCastJSValue(context
, pool
.strcat("&", CYPoolCCYON(pool
, context
, value
), NULL
));
1373 } catch (const CYException
&e
) {
1378 static JSValueRef
Pointer_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1379 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1380 return CYMakeType(context
, internal
->type_
->type_
);
1383 static JSValueRef
Functor_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1384 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1385 return CYMakeType(context
, &internal
->signature_
);
1388 static JSValueRef
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1389 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1390 return CYCastJSValue(context
, internal
->GetFFI()->alignment
);
1393 static JSValueRef
Type_getProperty_name(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1394 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1395 return CYCastJSValue(context
, internal
->type_
->name
);
1398 static JSValueRef
Type_getProperty_size(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1399 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1400 return CYCastJSValue(context
, internal
->GetFFI()->size
);
1403 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1404 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1406 const char *type(sig::Unparse(pool
, internal
->type_
));
1407 return CYCastJSValue(context
, CYJSString(type
));
1410 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1411 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1413 std::ostringstream out
;
1415 CYOutput
output(out
, options
);
1416 (new(pool
) CYEncodedType(Decode(pool
, internal
->type_
)))->Output(output
, CYNoFlags
);
1417 return CYCastJSValue(context
, CYJSString(out
.str().c_str()));
1420 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1421 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1424 static JSStaticFunction Pointer_staticFunctions
[4] = {
1425 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1426 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1427 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1431 static JSStaticValue Pointer_staticValues
[2] = {
1432 {"type", &Pointer_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1433 {NULL
, NULL
, NULL
, 0}
1436 static JSStaticFunction Struct_staticFunctions
[2] = {
1437 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1441 static JSStaticFunction Functor_staticFunctions
[4] = {
1442 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1443 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1444 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1449 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1452 static JSStaticValue Functor_staticValues
[2] = {
1453 {"type", &Functor_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1454 {NULL
, NULL
, NULL
, 0}
1458 JSStaticValue
const * const Functor::StaticValues
= Functor_staticValues
;
1461 static JSStaticValue Type_staticValues
[4] = {
1462 {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1463 {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1464 {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1465 {NULL
, NULL
, NULL
, 0}
1468 static JSStaticFunction Type_staticFunctions
[14] = {
1469 {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1470 {"blockWith", &Type_callAsFunction_blockWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1471 {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1472 {"functionWith", &Type_callAsFunction_functionWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1473 {"long", &Type_callAsFunction_long
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1474 {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1475 {"short", &Type_callAsFunction_short
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1476 {"signed", &Type_callAsFunction_signed
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1477 {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1478 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1479 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1480 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1481 {"unsigned", &Type_callAsFunction_unsigned
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1485 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1487 void CYSetArgs(int argc
, const char *argv
[]) {
1488 JSContextRef
context(CYGetJSContext());
1489 JSValueRef args
[argc
];
1490 for (int i(0); i
!= argc
; ++i
)
1491 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1494 if (JSObjectMakeArray$
!= NULL
)
1495 array
= _jsccall(*JSObjectMakeArray$
, context
, argc
, args
);
1497 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1498 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1499 array
= CYCastJSObject(context
, value
);
1502 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1503 CYSetProperty(context
, System
, CYJSString("args"), array
);
1506 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1507 return JSContextGetGlobalObject(context
);
1510 class ExecutionHandle
{
1512 JSContextRef context_
;
1516 ExecutionHandle(JSContextRef context
) :
1519 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1520 handle_
= (*hooks_
->ExecuteStart
)(context_
);
1525 ~ExecutionHandle() {
1526 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1527 (*hooks_
->ExecuteEnd
)(context_
, handle_
);
1531 const char *CYExecute(JSContextRef context
, CYPool
&pool
, CYUTF8String code
) {
1532 JSValueRef
exception(NULL
);
1534 ExecutionHandle
handle(context
);
1536 JSValueRef result
; try {
1537 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1538 } catch (const char *error
) {
1542 if (exception
!= NULL
) error
:
1543 return CYPoolCString(pool
, context
, CYJSString(context
, exception
));
1545 if (JSValueIsUndefined(context
, result
))
1548 const char *json
; try {
1549 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1550 } catch (const char *error
) {
1554 if (exception
!= NULL
)
1557 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1562 static bool initialized_
= false;
1564 void CYInitializeDynamic() {
1566 initialized_
= true;
1569 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1570 JSSynchronousGarbageCollectForDebugging$
= reinterpret_cast<void (*)(JSContextRef
)>(dlsym(RTLD_DEFAULT
, "JSSynchronousGarbageCollectForDebugging"));
1572 JSClassDefinition definition
;
1574 definition
= kJSClassDefinitionEmpty
;
1575 definition
.className
= "All";
1576 definition
.hasProperty
= &All_hasProperty
;
1577 definition
.getProperty
= &All_getProperty
;
1578 definition
.getPropertyNames
= &All_getPropertyNames
;
1579 All_
= JSClassCreate(&definition
);
1581 definition
= kJSClassDefinitionEmpty
;
1582 definition
.className
= "Context";
1583 definition
.finalize
= &CYFinalize
;
1584 Context_
= JSClassCreate(&definition
);
1586 definition
= kJSClassDefinitionEmpty
;
1587 definition
.className
= "Functor";
1588 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1589 definition
.staticValues
= Functor_staticValues
;
1590 definition
.callAsFunction
= &Functor_callAsFunction
;
1591 definition
.finalize
= &CYFinalize
;
1592 Functor_
= JSClassCreate(&definition
);
1594 definition
= kJSClassDefinitionEmpty
;
1595 definition
.className
= "Pointer";
1596 definition
.staticFunctions
= Pointer_staticFunctions
;
1597 definition
.staticValues
= Pointer_staticValues
;
1598 definition
.getProperty
= &Pointer_getProperty
;
1599 definition
.setProperty
= &Pointer_setProperty
;
1600 definition
.finalize
= &CYFinalize
;
1601 Pointer_
= JSClassCreate(&definition
);
1603 definition
= kJSClassDefinitionEmpty
;
1604 definition
.className
= "Struct";
1605 definition
.staticFunctions
= Struct_staticFunctions
;
1606 definition
.getProperty
= &Struct_getProperty
;
1607 definition
.setProperty
= &Struct_setProperty
;
1608 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1609 definition
.finalize
= &CYFinalize
;
1610 Struct_
= JSClassCreate(&definition
);
1612 definition
= kJSClassDefinitionEmpty
;
1613 definition
.className
= "Type";
1614 definition
.staticValues
= Type_staticValues
;
1615 definition
.staticFunctions
= Type_staticFunctions
;
1616 definition
.callAsFunction
= &Type_callAsFunction
;
1617 definition
.callAsConstructor
= &Type_callAsConstructor
;
1618 definition
.finalize
= &CYFinalize
;
1619 Type_privateData::Class_
= JSClassCreate(&definition
);
1621 definition
= kJSClassDefinitionEmpty
;
1622 definition
.className
= "Global";
1623 //definition.getProperty = &Global_getProperty;
1624 Global_
= JSClassCreate(&definition
);
1626 Array_s
= JSStringCreateWithUTF8CString("Array");
1627 cy_s
= JSStringCreateWithUTF8CString("$cy");
1628 cyi_s
= JSStringCreateWithUTF8CString("$cyi");
1629 length_s
= JSStringCreateWithUTF8CString("length");
1630 message_s
= JSStringCreateWithUTF8CString("message");
1631 name_s
= JSStringCreateWithUTF8CString("name");
1632 pop_s
= JSStringCreateWithUTF8CString("pop");
1633 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1634 push_s
= JSStringCreateWithUTF8CString("push");
1635 splice_s
= JSStringCreateWithUTF8CString("splice");
1636 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1637 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1638 toPointer_s
= JSStringCreateWithUTF8CString("toPointer");
1639 toString_s
= JSStringCreateWithUTF8CString("toString");
1641 Result_
= JSStringCreateWithUTF8CString("_");
1643 if (hooks_
!= NULL
&& hooks_
->Initialize
!= NULL
)
1644 (*hooks_
->Initialize
)();
1647 void CYThrow(JSContextRef context
, JSValueRef value
) {
1649 throw CYJSError(context
, value
);
1652 const char *CYJSError::PoolCString(CYPool
&pool
) const {
1653 // XXX: this used to be CYPoolCString
1654 return CYPoolCCYON(pool
, context_
, value_
);
1657 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1658 // XXX: what if the context is different?
1662 JSValueRef
CYCastJSError(JSContextRef context
, const char *message
) {
1663 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString("Error")));
1664 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1665 return _jsccall(JSObjectCallAsConstructor
, context
, Error
, 1, arguments
);
1668 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1669 return CYCastJSError(context
, message_
);
1672 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1673 _assert(context
!= NULL
);
1678 va_start(args
, format
);
1679 // XXX: there might be a beter way to think about this
1680 const char *message(pool
.vsprintf(64, format
, args
));
1683 value_
= CYCastJSError(context
, message
);
1686 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1687 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1690 extern "C" bool CydgetMemoryParse(const uint16_t **data
, size_t *size
);
1692 void *CYMapFile(const char *path
, size_t *psize
) {
1693 int fd(_syscall_(open(path
, O_RDONLY
), 1, {ENOENT
}));
1698 _syscall(fstat(fd
, &stat
));
1699 size_t size(stat
.st_size
);
1704 _syscall(base
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, 0));
1706 _syscall(close(fd
));
1710 static JSValueRef
require(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1711 _assert(count
== 1);
1715 _assert(dladdr(reinterpret_cast<void *>(&require
), &addr
) != 0);
1716 char *lib(pool
.strdup(addr
.dli_fname
));
1718 char *slash(strrchr(lib
, '/'));
1719 _assert(slash
!= NULL
);
1722 CYJSString
property("exports");
1725 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1726 const char *path(pool
.strcat(lib
, "/cycript0.9/", name
, ".cy", NULL
));
1728 CYJSString
key(path
);
1729 JSObjectRef
modules(CYGetCachedObject(context
, CYJSString("modules")));
1730 JSValueRef
cache(CYGetProperty(context
, modules
, key
));
1732 if (!JSValueIsUndefined(context
, cache
))
1733 module = CYCastJSObject(context
, cache
);
1736 code
.data
= reinterpret_cast<char *>(CYMapFile(path
, &code
.size
));
1738 if (code
.data
== NULL
) {
1739 if (strchr(name
, '/') == NULL
&& (
1740 dlopen(pool
.strcat("/System/Library/Frameworks/", name
, ".framework/", name
, NULL
), RTLD_LAZY
| RTLD_GLOBAL
) != NULL
||
1741 dlopen(pool
.strcat("/System/Library/PrivateFrameworks/", name
, ".framework/", name
, NULL
), RTLD_LAZY
| RTLD_GLOBAL
) != NULL
||
1743 return CYJSUndefined(NULL
);
1745 CYThrow("Can't find module: %s", name
);
1748 module = JSObjectMake(context
, NULL
, NULL
);
1749 CYSetProperty(context
, modules
, key
, module);
1751 JSObjectRef
exports(JSObjectMake(context
, NULL
, NULL
));
1752 CYSetProperty(context
, module, property
, exports
);
1754 std::stringstream wrap
;
1755 wrap
<< "(function (exports, require, module) { " << code
<< "\n});";
1756 code
= CYPoolCode(pool
, wrap
);
1758 JSValueRef
value(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0));
1759 JSObjectRef
function(CYCastJSObject(context
, value
));
1761 JSValueRef arguments
[3] = { exports
, JSObjectMakeFunctionWithCallback(context
, CYJSString("require"), &require
), module };
1762 CYCallAsFunction(context
, function
, NULL
, 3, arguments
);
1765 return CYGetProperty(context
, module, property
);
1768 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
1769 CYInitializeDynamic();
1771 JSObjectRef
global(CYGetGlobalObject(context
));
1773 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
1774 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
1776 /* Cache Globals {{{ */
1777 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
1778 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
1780 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
1781 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
1783 JSObjectRef
Boolean(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Boolean"))));
1784 CYSetProperty(context
, cy
, CYJSString("Boolean"), Boolean
);
1786 JSObjectRef
Boolean_prototype(CYCastJSObject(context
, CYGetProperty(context
, Boolean
, prototype_s
)));
1787 CYSetProperty(context
, cy
, CYJSString("Boolean_prototype"), Boolean_prototype
);
1789 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
1790 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
1792 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
1793 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
1795 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
1796 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
1798 JSObjectRef
Number(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Number"))));
1799 CYSetProperty(context
, cy
, CYJSString("Number"), Number
);
1801 JSObjectRef
Number_prototype(CYCastJSObject(context
, CYGetProperty(context
, Number
, prototype_s
)));
1802 CYSetProperty(context
, cy
, CYJSString("Number_prototype"), Number_prototype
);
1804 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1805 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
1807 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
1808 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
1810 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
1811 CYSetProperty(context
, cy
, CYJSString("String"), String
);
1813 JSObjectRef
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
)));
1814 CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
);
1817 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1818 CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1820 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1821 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1822 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
1824 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1825 CYSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
1826 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
1828 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1829 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1831 JSObjectRef
modules(JSObjectMake(context
, NULL
, NULL
));
1832 CYSetProperty(context
, cy
, CYJSString("modules"), modules
);
1834 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
1835 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
1837 JSObjectRef
alls(_jsccall(JSObjectCallAsConstructor
, context
, Array
, 0, NULL
));
1838 CYSetProperty(context
, cycript
, CYJSString("alls"), alls
);
1841 JSObjectRef
last(NULL
), curr(global
);
1843 goto next
; for (JSValueRef next
;;) {
1844 if (JSValueIsNull(context
, next
))
1847 curr
= CYCastJSObject(context
, next
);
1849 next
= JSObjectGetPrototype(context
, curr
);
1852 CYSetPrototype(context
, last
, all
);
1855 CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
, kJSPropertyAttributeDontEnum
);
1857 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
1858 CYSetProperty(context
, cy
, CYJSString("System"), System
);
1860 CYSetProperty(context
, all
, CYJSString("require"), &require
, kJSPropertyAttributeDontEnum
);
1862 CYSetProperty(context
, global
, CYJSString("system"), System
);
1863 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
1864 //CYSetProperty(context, System, CYJSString("global"), global);
1865 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
1867 if (CYBridgeEntry
*entry
= CYBridgeHash("1dlerror", 8))
1868 entry
->cache_
= new cy::Functor(entry
->value_
, reinterpret_cast<void (*)()>(&dlerror
));
1870 if (hooks_
!= NULL
&& hooks_
->SetupContext
!= NULL
)
1871 (*hooks_
->SetupContext
)(context
);
1873 CYArrayPush(context
, alls
, cycript
);
1876 static JSGlobalContextRef context_
;
1878 JSGlobalContextRef
CYGetJSContext() {
1879 CYInitializeDynamic();
1881 if (context_
== NULL
) {
1882 context_
= JSGlobalContextCreate(Global_
);
1883 CYSetupContext(context_
);
1889 void CYDestroyContext() {
1890 if (context_
== NULL
)
1892 JSGlobalContextRelease(context_
);