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 void (*JSSynchronousGarbageCollectForDebugging$
)(JSContextRef
);
347 void CYGarbageCollect(JSContextRef context
) {
348 (JSSynchronousGarbageCollectForDebugging$
?: &JSGarbageCollect
)(context
);
351 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
352 CYGarbageCollect(context
);
353 return CYJSUndefined(context
);
356 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
357 switch (JSType type
= JSValueGetType(context
, value
)) {
358 case kJSTypeUndefined
:
363 return CYCastBool(context
, value
) ? "true" : "false";
365 case kJSTypeNumber
: {
366 std::ostringstream str
;
367 CYNumerify(str
, CYCastDouble(context
, value
));
368 std::string
value(str
.str());
369 return pool
.strmemdup(value
.c_str(), value
.size());
372 case kJSTypeString
: {
373 std::ostringstream str
;
374 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
375 CYStringify(str
, string
.data
, string
.size
);
376 std::string
value(str
.str());
377 return pool
.strmemdup(value
.c_str(), value
.size());
381 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
383 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
387 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
388 return _jsccall(CYPoolCCYON
, pool
, context
, value
);
391 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSObjectRef object
) {
392 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
393 if (CYIsCallable(context
, toCYON
)) {
394 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
395 _assert(value
!= NULL
);
396 return CYPoolCString(pool
, context
, value
);
399 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
400 if (CYIsCallable(context
, toJSON
)) {
401 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
402 return _jsccall(CYPoolCCYON
, pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
));
405 if (JSObjectIsFunction(context
, object
)) {
406 JSValueRef
toString(CYGetProperty(context
, object
, toString_s
));
407 if (CYIsCallable(context
, toString
)) {
408 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
409 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toString
, object
, 1, arguments
));
410 _assert(value
!= NULL
);
411 return CYPoolCString(pool
, context
, value
);
415 std::ostringstream str
;
419 // XXX: this is, sadly, going to leak
420 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
424 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
430 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
431 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
436 CYStringify(str
, string
.data
, string
.size
);
441 JSValueRef
value(CYGetProperty(context
, object
, name
));
442 str
<< CYPoolCCYON(pool
, context
, value
);
443 } catch (const CYException
&error
) {
448 JSPropertyNameArrayRelease(names
);
452 std::string
string(str
.str());
453 return pool
.strmemdup(string
.c_str(), string
.size());
456 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
458 std::ostringstream str
;
462 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
465 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
472 JSValueRef
value(CYGetProperty(context
, _this
, index
));
473 if (!JSValueIsUndefined(context
, value
))
474 str
<< CYPoolCCYON(pool
, context
, value
);
479 } catch (const CYException
&error
) {
486 std::string
value(str
.str());
487 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
490 static JSValueRef
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
492 std::ostringstream str
;
494 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
)));
495 CYStringify(str
, string
.data
, string
.size
);
497 std::string
value(str
.str());
498 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
501 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
502 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
503 return JSObjectMake(context
, Pointer_
, internal
);
506 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const sig::Signature
&signature
) {
507 return JSObjectMake(context
, Functor_
, new cy::Functor(signature
, function
));
510 static JSObjectRef
CYMakeFunctor(JSContextRef context
, const char *symbol
, const char *encoding
, void **cache
) {
511 cy::Functor
*internal
;
513 internal
= reinterpret_cast<cy::Functor
*>(*cache
);
515 void (*function
)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol
)));
516 if (function
== NULL
)
519 internal
= new cy::Functor(encoding
, function
);
524 return JSObjectMake(context
, Functor_
, internal
);
527 static bool CYGetOffset(CYPool
&pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
528 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
531 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
532 switch (JSValueGetType(context
, value
)) {
535 case kJSTypeObject
: {
536 JSObjectRef
object((JSObjectRef
) value
);
537 if (JSValueIsObjectOfClass(context
, value
, Pointer_
)) {
538 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
539 return internal
->value_
;
541 JSValueRef
toPointer(CYGetProperty(context
, object
, toPointer_s
));
542 if (CYIsCallable(context
, toPointer
)) {
543 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
));
544 _assert(value
!= NULL
);
545 return CYCastPointer_(context
, value
);
548 double number(CYCastDouble(context
, value
));
549 if (std::isnan(number
))
550 throw CYJSError(context
, "cannot convert value to pointer");
551 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
555 void CYPoolFFI(CYPool
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
556 switch (type
->primitive
) {
558 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
561 #define CYPoolFFI_(primitive, native) \
562 case sig::primitive ## _P: \
563 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
566 CYPoolFFI_(uchar
, unsigned char)
567 CYPoolFFI_(char, char)
568 CYPoolFFI_(ushort
, unsigned short)
569 CYPoolFFI_(short, short)
570 CYPoolFFI_(ulong
, unsigned long)
571 CYPoolFFI_(long, long)
572 CYPoolFFI_(uint
, unsigned int)
574 CYPoolFFI_(ulonglong
, unsigned long long)
575 CYPoolFFI_(longlong
, long long)
576 CYPoolFFI_(float, float)
577 CYPoolFFI_(double, double)
580 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
581 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
582 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
583 ffi_type
*field(ffi
->elements
[index
]);
586 if (aggregate
== NULL
)
589 rhs
= CYGetProperty(context
, aggregate
, index
);
590 if (JSValueIsUndefined(context
, rhs
))
591 throw CYJSError(context
, "unable to extract array value");
594 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
601 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
605 _assert(pool
!= NULL
);
606 *reinterpret_cast<const char **>(data
) = CYPoolCString(*pool
, context
, value
);
609 case sig::struct_P
: {
610 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
611 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
612 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
613 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
614 ffi_type
*field(ffi
->elements
[index
]);
617 if (aggregate
== NULL
)
620 rhs
= CYGetProperty(context
, aggregate
, index
);
621 if (JSValueIsUndefined(context
, rhs
)) {
622 if (element
->name
!= NULL
)
623 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
626 if (JSValueIsUndefined(context
, rhs
)) undefined
:
627 throw CYJSError(context
, "unable to extract structure value");
631 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
641 if (hooks_
!= NULL
&& hooks_
->PoolFFI
!= NULL
)
642 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
645 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
649 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
650 switch (type
->primitive
) {
652 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
654 #define CYFromFFI_(primitive, native) \
655 case sig::primitive ## _P: \
656 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
658 CYFromFFI_(uchar, unsigned char)
659 CYFromFFI_(char, char)
660 CYFromFFI_(ushort
, unsigned short)
661 CYFromFFI_(short, short)
662 CYFromFFI_(ulong
, unsigned long)
663 CYFromFFI_(long, long)
664 CYFromFFI_(uint
, unsigned int)
666 CYFromFFI_(ulonglong
, unsigned long long)
667 CYFromFFI_(longlong
, long long)
668 CYFromFFI_(float, float)
669 CYFromFFI_(double, double)
672 if (void *pointer
= data
)
673 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
677 if (void *pointer
= *reinterpret_cast<void **>(data
))
678 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
682 if (char *utf8
= *reinterpret_cast<char **>(data
))
683 return CYCastJSValue(context
, utf8
);
687 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
689 return CYJSUndefined(context
);
692 return CYJSNull(context
);
694 if (hooks_
!= NULL
&& hooks_
->FromFFI
!= NULL
)
695 if (JSValueRef value
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
698 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
702 void CYExecuteClosure(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
, JSValueRef (*adapter
)(JSContextRef
, size_t, JSValueRef
[], JSObjectRef
)) {
703 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
705 JSContextRef
context(internal
->context_
);
707 size_t count(internal
->cif_
.nargs
);
708 JSValueRef values
[count
];
710 for (size_t index(0); index
!= count
; ++index
)
711 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
713 JSValueRef
value(adapter(context
, count
, values
, internal
->function_
));
714 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
717 static JSValueRef
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
) {
718 return CYCallAsFunction(context
, function
, NULL
, count
, values
);
721 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
722 CYExecuteClosure(cif
, result
, arguments
, arg
, &FunctionAdapter_
);
725 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
726 // XXX: in case of exceptions this will leak
727 // XXX: in point of fact, this may /need/ to leak :(
728 Closure_privateData
*internal(new Closure_privateData(context
, function
, signature
));
730 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
732 ffi_closure
*writable(reinterpret_cast<ffi_closure
*>(ffi_closure_alloc(sizeof(ffi_closure
), &executable
)));
734 ffi_status
status(ffi_prep_closure_loc(writable
, &internal
->cif_
, callback
, internal
, executable
));
735 _assert(status
== FFI_OK
);
737 internal
->value_
= executable
;
739 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
740 NULL
, sizeof(ffi_closure
),
741 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
745 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
746 _assert(status
== FFI_OK
);
748 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
750 internal
->value_
= closure
;
756 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
) {
757 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, signature
, &FunctionClosure_
));
758 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
759 // XXX: see above notes about needing to leak
760 JSValueProtect(CYGetJSContext(context
), object
);
764 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
765 return CYCastJSObject(context
, CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
));
768 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const sig::Signature
&signature
) {
769 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
771 bool function(_jsccall(JSValueIsInstanceOfConstructor
, context
, value
, Function
));
773 JSObjectRef
function(CYCastJSObject(context
, value
));
774 return CYMakeFunctor(context
, function
, signature
);
776 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
777 return CYMakeFunctor(context
, function
, signature
);
781 static bool Index_(CYPool
&pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
782 Type_privateData
*typical(internal
->type_
);
783 sig::Type
*type(typical
->type_
);
787 const char *name(CYPoolCString(pool
, context
, property
));
788 size_t length(strlen(name
));
789 double number(CYCastDouble(name
, length
));
791 size_t count(type
->data
.signature
.count
);
793 if (std::isnan(number
)) {
794 if (property
== NULL
)
797 sig::Element
*elements(type
->data
.signature
.elements
);
799 for (size_t local(0); local
!= count
; ++local
) {
800 sig::Element
*element(&elements
[local
]);
801 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
809 index
= static_cast<ssize_t
>(number
);
810 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
815 ffi_type
**elements(typical
->GetFFI()->elements
);
817 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
818 for (ssize_t
local(0); local
!= index
; ++local
)
819 base
+= elements
[local
]->size
;
824 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
826 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
828 if (JSStringIsEqual(property
, length_s
))
829 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
831 Type_privateData
*typical(internal
->type_
);
832 if (typical
->type_
== NULL
)
834 sig::Type
&type(*typical
->type_
);
837 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
839 else if (!CYGetOffset(pool
, context
, property
, offset
))
842 if (type
.primitive
== sig::function_P
)
843 return CYMakeFunctor(context
, reinterpret_cast<void (*)()>(internal
->value_
), type
.data
.signature
);
845 ffi_type
*ffi(typical
->GetFFI());
847 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
848 base
+= ffi
->size
* offset
;
850 JSObjectRef
owner(internal
->GetOwner() ?: object
);
851 return CYFromFFI(context
, &type
, ffi
, base
, false, owner
);
854 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
856 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
857 Type_privateData
*typical(internal
->type_
);
859 if (typical
->type_
== NULL
)
863 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
865 else if (!CYGetOffset(pool
, context
, property
, offset
))
868 ffi_type
*ffi(typical
->GetFFI());
870 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
871 base
+= ffi
->size
* offset
;
873 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
877 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
878 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
879 Type_privateData
*typical(internal
->type_
);
880 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
883 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
885 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
886 Type_privateData
*typical(internal
->type_
);
891 if (!Index_(pool
, context
, internal
, property
, index
, base
))
894 JSObjectRef
owner(internal
->GetOwner() ?: object
);
896 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
899 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
901 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
902 Type_privateData
*typical(internal
->type_
);
907 if (!Index_(pool
, context
, internal
, property
, index
, base
))
910 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
914 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
915 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
916 Type_privateData
*typical(internal
->type_
);
917 sig::Type
*type(typical
->type_
);
922 size_t count(type
->data
.signature
.count
);
923 sig::Element
*elements(type
->data
.signature
.elements
);
927 for (size_t index(0); index
!= count
; ++index
) {
929 name
= elements
[index
].name
;
932 sprintf(number
, "%zu", index
);
936 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
940 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
)()) {
941 if (setups
+ count
!= signature
->count
- 1)
942 throw CYJSError(context
, "incorrect number of arguments to ffi function");
944 size_t size(setups
+ count
);
946 memcpy(values
, setup
, sizeof(void *) * setups
);
948 for (size_t index(setups
); index
!= size
; ++index
) {
949 sig::Element
*element(&signature
->elements
[index
+ 1]);
950 ffi_type
*ffi(cif
->arg_types
[index
]);
952 values
[index
] = new(pool
) uint8_t[ffi
->size
];
953 CYPoolFFI(&pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
956 uint8_t value
[cif
->rtype
->size
];
958 if (hooks_
!= NULL
&& hooks_
->CallFunction
!= NULL
)
959 (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
);
961 ffi_call(cif
, function
, value
, values
);
963 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
966 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
968 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
969 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
972 JSObjectRef
CYMakeType(JSContextRef context
, const char *encoding
) {
973 Type_privateData
*internal(new Type_privateData(encoding
));
974 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
977 JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
978 Type_privateData
*internal(new Type_privateData(type
));
979 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
982 JSObjectRef
CYMakeType(JSContextRef context
, sig::Signature
*signature
) {
989 type
.primitive
= sig::function_P
;
990 sig::Copy(pool
, type
.data
.signature
, *signature
);
992 return CYMakeType(context
, &type
);
995 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
996 JSObjectRef
global(CYGetGlobalObject(context
));
997 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
998 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1000 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1001 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1002 if (JSValueRef value
= CYGetProperty(context
, space
, property
))
1003 if (!JSValueIsUndefined(context
, value
))
1007 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
1009 size_t length(name
.size
);
1010 char keyed
[length
+ 2];
1011 memcpy(keyed
+ 1, name
.data
, length
+ 1);
1013 static const char *modes
= "0124";
1014 for (size_t i(0); i
!= 4; ++i
) {
1015 char mode(modes
[i
]);
1018 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
1021 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
);
1024 return CYMakeFunctor(context
, name
.data
, entry
->value_
, &entry
->cache_
);
1027 if (void *symbol
= CYCastSymbol(name
.data
)) {
1028 // XXX: this is horrendously inefficient
1029 sig::Signature signature
;
1030 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
1032 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1033 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
1036 // XXX: implement case 3
1038 return CYMakeType(context
, entry
->value_
);
1045 static void All_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1046 JSObjectRef
global(CYGetGlobalObject(context
));
1047 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1048 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1050 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1051 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1))) {
1052 JSPropertyNameArrayRef
subset(JSObjectCopyPropertyNames(context
, space
));
1053 for (size_t index(0), count(JSPropertyNameArrayGetCount(subset
)); index
!= count
; ++index
)
1054 JSPropertyNameAccumulatorAddName(names
, JSPropertyNameArrayGetNameAtIndex(subset
, index
));
1055 JSPropertyNameArrayRelease(subset
);
1059 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1061 throw CYJSError(context
, "incorrect number of arguments to Pointer constructor");
1065 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1066 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1068 sig::Signature signature
;
1069 sig::Parse(pool
, &signature
, type
, &Structor_
);
1071 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1074 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1076 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1078 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1079 return CYMakeType(context
, type
);
1082 static JSValueRef Type_callAsFunction_$
With(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], sig::Primitive primitive
, JSValueRef
*exception
) { CYTry
{
1083 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1091 type
.primitive
= primitive
;
1092 type
.data
.signature
.elements
= new(pool
) sig::Element
[1 + count
];
1093 type
.data
.signature
.count
= 1 + count
;
1095 type
.data
.signature
.elements
[0].name
= NULL
;
1096 type
.data
.signature
.elements
[0].type
= internal
->type_
;
1097 type
.data
.signature
.elements
[0].offset
= _not(size_t);
1099 for (size_t i(0); i
!= count
; ++i
) {
1100 sig::Element
&element(type
.data
.signature
.elements
[i
+ 1]);
1101 element
.name
= NULL
;
1102 element
.offset
= _not(size_t);
1104 JSObjectRef
object(CYCastJSObject(context
, arguments
[i
]));
1105 _assert(JSValueIsObjectOfClass(context
, object
, Type_privateData::Class_
));
1106 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1108 element
.type
= internal
->type_
;
1111 return CYMakeType(context
, &type
);
1114 static JSValueRef
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1116 throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf");
1117 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1120 size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0])));
1121 if (index
== _not(size_t))
1122 throw CYJSError(context
, "invalid array size used with Type.arrayOf");
1128 type
.primitive
= sig::array_P
;
1129 type
.data
.data
.type
= internal
->type_
;
1130 type
.data
.data
.size
= index
;
1132 return CYMakeType(context
, &type
);
1135 static JSValueRef
Type_callAsFunction_blockWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1136 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::block_P
, exception
);
1139 static JSValueRef
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1141 throw CYJSError(context
, "incorrect number of arguments to Type.constant");
1142 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1144 sig::Type
type(*internal
->type_
);
1145 type
.flags
|= JOC_TYPE_CONST
;
1146 return CYMakeType(context
, &type
);
1149 static JSValueRef
Type_callAsFunction_long(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1151 throw CYJSError(context
, "incorrect number of arguments to Type.long");
1152 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1154 sig::Type
type(*internal
->type_
);
1156 switch (type
.primitive
) {
1157 case sig::short_P
: type
.primitive
= sig::int_P
; break;
1158 case sig::int_P
: type
.primitive
= sig::long_P
; break;
1159 case sig::long_P
: type
.primitive
= sig::longlong_P
; break;
1160 default: throw CYJSError(context
, "invalid type argument to Type.long");
1163 return CYMakeType(context
, &type
);
1166 static JSValueRef
Type_callAsFunction_short(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1168 throw CYJSError(context
, "incorrect number of arguments to Type.short");
1169 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1171 sig::Type
type(*internal
->type_
);
1173 switch (type
.primitive
) {
1174 case sig::int_P
: type
.primitive
= sig::short_P
; break;
1175 case sig::long_P
: type
.primitive
= sig::int_P
; break;
1176 case sig::longlong_P
: type
.primitive
= sig::long_P
; break;
1177 default: throw CYJSError(context
, "invalid type argument to Type.short");
1180 return CYMakeType(context
, &type
);
1183 static JSValueRef
Type_callAsFunction_signed(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.signed");
1186 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1188 sig::Type
type(*internal
->type_
);
1190 switch (type
.primitive
) {
1191 case sig::char_P
: case sig::uchar_P
: type
.primitive
= sig::char_P
; break;
1192 case sig::short_P
: case sig::ushort_P
: type
.primitive
= sig::short_P
; break;
1193 case sig::int_P
: case sig::uint_P
: type
.primitive
= sig::int_P
; break;
1194 case sig::long_P
: case sig::ulong_P
: type
.primitive
= sig::long_P
; break;
1195 case sig::longlong_P
: case sig::ulonglong_P
: type
.primitive
= sig::longlong_P
; break;
1196 default: throw CYJSError(context
, "invalid type argument to Type.signed");
1199 return CYMakeType(context
, &type
);
1202 static JSValueRef
Type_callAsFunction_unsigned(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1204 throw CYJSError(context
, "incorrect number of arguments to Type.unsigned");
1205 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1207 sig::Type
type(*internal
->type_
);
1209 switch (type
.primitive
) {
1210 case sig::char_P
: case sig::uchar_P
: type
.primitive
= sig::uchar_P
; break;
1211 case sig::short_P
: case sig::ushort_P
: type
.primitive
= sig::ushort_P
; break;
1212 case sig::int_P
: case sig::uint_P
: type
.primitive
= sig::uint_P
; break;
1213 case sig::long_P
: case sig::ulong_P
: type
.primitive
= sig::ulong_P
; break;
1214 case sig::longlong_P
: case sig::ulonglong_P
: type
.primitive
= sig::ulonglong_P
; break;
1215 default: throw CYJSError(context
, "invalid type argument to Type.unsigned");
1218 return CYMakeType(context
, &type
);
1221 static JSValueRef
Type_callAsFunction_functionWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1222 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::function_P
, exception
);
1225 static JSValueRef
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1227 throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo");
1228 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1233 if (internal
->type_
->primitive
== sig::char_P
) {
1234 type
.flags
= internal
->type_
->flags
;
1235 type
.primitive
= sig::string_P
;
1236 type
.data
.data
.type
= NULL
;
1237 type
.data
.data
.size
= 0;
1240 type
.primitive
= sig::pointer_P
;
1241 type
.data
.data
.type
= internal
->type_
;
1242 type
.data
.data
.size
= 0;
1245 return CYMakeType(context
, &type
);
1248 static JSValueRef
Type_callAsFunction_withName(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1250 throw CYJSError(context
, "incorrect number of arguments to Type.withName");
1251 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1254 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1256 sig::Type
type(*internal
->type_
);
1258 return CYMakeType(context
, &type
);
1261 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1263 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1264 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1266 if (internal
->type_
->primitive
== sig::function_P
)
1267 return CYMakeFunctor(context
, arguments
[0], internal
->type_
->data
.signature
);
1269 sig::Type
*type(internal
->type_
);
1270 ffi_type
*ffi(internal
->GetFFI());
1272 uint8_t value
[ffi
->size
];
1274 CYPoolFFI(&pool
, context
, type
, ffi
, value
, arguments
[0]);
1275 return CYFromFFI(context
, type
, ffi
, value
);
1278 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1280 throw CYJSError(context
, "incorrect number of arguments to Type allocator");
1281 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1283 sig::Type
*type(internal
->type_
);
1286 if (type
->primitive
!= sig::array_P
)
1287 length
= _not(size_t);
1289 length
= type
->data
.data
.size
;
1290 type
= type
->data
.data
.type
;
1293 void *value(calloc(1, internal
->GetFFI()->size
));
1294 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1297 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1299 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1301 const char *encoding(CYPoolCString(pool
, context
, arguments
[1]));
1302 sig::Signature signature
;
1303 sig::Parse(pool
, &signature
, encoding
, &Structor_
);
1304 return CYMakeFunctor(context
, arguments
[0], signature
);
1307 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1308 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1309 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1312 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1313 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1316 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1317 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1319 sprintf(string
, "%p", internal
->value_
);
1320 return CYCastJSValue(context
, string
);
1323 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1324 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1325 if (internal
->length_
!= _not(size_t)) {
1326 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
1327 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1328 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1329 } else if (internal
->type_
->type_
== NULL
) pointer
: {
1331 sprintf(string
, "%p", internal
->value_
);
1332 return CYCastJSValue(context
, string
);
1334 JSValueRef
value(CYGetProperty(context
, _this
, cyi_s
));
1335 if (JSValueIsUndefined(context
, value
))
1338 return CYCastJSValue(context
, pool
.strcat("&", CYPoolCCYON(pool
, context
, value
), NULL
));
1339 } catch (const CYException
&e
) {
1344 static JSValueRef
Pointer_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1345 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1346 return CYMakeType(context
, internal
->type_
->type_
);
1349 static JSValueRef
Functor_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1350 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1351 return CYMakeType(context
, &internal
->signature_
);
1354 static JSValueRef
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1355 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1356 return CYCastJSValue(context
, internal
->GetFFI()->alignment
);
1359 static JSValueRef
Type_getProperty_name(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1360 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1361 return CYCastJSValue(context
, internal
->type_
->name
);
1364 static JSValueRef
Type_getProperty_size(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1365 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1366 return CYCastJSValue(context
, internal
->GetFFI()->size
);
1369 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1370 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1372 const char *type(sig::Unparse(pool
, internal
->type_
));
1373 return CYCastJSValue(context
, CYJSString(type
));
1376 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1377 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1379 std::ostringstream out
;
1381 CYOutput
output(out
, options
);
1382 (new(pool
) CYEncodedType(Decode(pool
, internal
->type_
)))->Output(output
, CYNoFlags
);
1383 return CYCastJSValue(context
, CYJSString(out
.str().c_str()));
1386 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1387 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1390 static JSStaticFunction Pointer_staticFunctions
[4] = {
1391 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1392 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1393 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1397 static JSStaticValue Pointer_staticValues
[2] = {
1398 {"type", &Pointer_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1399 {NULL
, NULL
, NULL
, 0}
1402 static JSStaticFunction Struct_staticFunctions
[2] = {
1403 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1407 static JSStaticFunction Functor_staticFunctions
[4] = {
1408 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1409 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1410 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1415 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1418 static JSStaticValue Functor_staticValues
[2] = {
1419 {"type", &Functor_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1420 {NULL
, NULL
, NULL
, 0}
1423 static JSStaticValue Type_staticValues
[4] = {
1424 {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1425 {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1426 {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1427 {NULL
, NULL
, NULL
, 0}
1430 static JSStaticFunction Type_staticFunctions
[14] = {
1431 {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1432 {"blockWith", &Type_callAsFunction_blockWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1433 {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1434 {"functionWith", &Type_callAsFunction_functionWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1435 {"long", &Type_callAsFunction_long
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1436 {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1437 {"short", &Type_callAsFunction_short
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1438 {"signed", &Type_callAsFunction_signed
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1439 {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1440 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1441 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1442 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1443 {"unsigned", &Type_callAsFunction_unsigned
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1447 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1449 void CYSetArgs(int argc
, const char *argv
[]) {
1450 JSContextRef
context(CYGetJSContext());
1451 JSValueRef args
[argc
];
1452 for (int i(0); i
!= argc
; ++i
)
1453 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1456 if (JSObjectMakeArray$
!= NULL
)
1457 array
= _jsccall(*JSObjectMakeArray$
, context
, argc
, args
);
1459 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1460 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1461 array
= CYCastJSObject(context
, value
);
1464 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1465 CYSetProperty(context
, System
, CYJSString("args"), array
);
1468 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1469 return JSContextGetGlobalObject(context
);
1472 class ExecutionHandle
{
1474 JSContextRef context_
;
1478 ExecutionHandle(JSContextRef context
) :
1481 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1482 handle_
= (*hooks_
->ExecuteStart
)(context_
);
1487 ~ExecutionHandle() {
1488 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1489 (*hooks_
->ExecuteEnd
)(context_
, handle_
);
1493 const char *CYExecute(JSContextRef context
, CYPool
&pool
, CYUTF8String code
) {
1494 JSValueRef
exception(NULL
);
1496 ExecutionHandle
handle(context
);
1498 JSValueRef result
; try {
1499 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1500 } catch (const char *error
) {
1504 if (exception
!= NULL
) error
:
1505 return CYPoolCString(pool
, context
, CYJSString(context
, exception
));
1507 if (JSValueIsUndefined(context
, result
))
1510 const char *json
; try {
1511 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1512 } catch (const char *error
) {
1516 if (exception
!= NULL
)
1519 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1524 static bool initialized_
= false;
1526 void CYInitializeDynamic() {
1528 initialized_
= true;
1531 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1532 JSSynchronousGarbageCollectForDebugging$
= reinterpret_cast<void (*)(JSContextRef
)>(dlsym(RTLD_DEFAULT
, "JSSynchronousGarbageCollectForDebugging"));
1534 JSClassDefinition definition
;
1536 definition
= kJSClassDefinitionEmpty
;
1537 definition
.className
= "All";
1538 definition
.getProperty
= &All_getProperty
;
1539 definition
.getPropertyNames
= &All_getPropertyNames
;
1540 All_
= JSClassCreate(&definition
);
1542 definition
= kJSClassDefinitionEmpty
;
1543 definition
.className
= "Context";
1544 definition
.finalize
= &CYFinalize
;
1545 Context_
= JSClassCreate(&definition
);
1547 definition
= kJSClassDefinitionEmpty
;
1548 definition
.className
= "Functor";
1549 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1550 definition
.staticValues
= Functor_staticValues
;
1551 definition
.callAsFunction
= &Functor_callAsFunction
;
1552 definition
.finalize
= &CYFinalize
;
1553 Functor_
= JSClassCreate(&definition
);
1555 definition
= kJSClassDefinitionEmpty
;
1556 definition
.className
= "Pointer";
1557 definition
.staticFunctions
= Pointer_staticFunctions
;
1558 definition
.staticValues
= Pointer_staticValues
;
1559 definition
.getProperty
= &Pointer_getProperty
;
1560 definition
.setProperty
= &Pointer_setProperty
;
1561 definition
.finalize
= &CYFinalize
;
1562 Pointer_
= JSClassCreate(&definition
);
1564 definition
= kJSClassDefinitionEmpty
;
1565 definition
.className
= "Struct";
1566 definition
.staticFunctions
= Struct_staticFunctions
;
1567 definition
.getProperty
= &Struct_getProperty
;
1568 definition
.setProperty
= &Struct_setProperty
;
1569 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1570 definition
.finalize
= &CYFinalize
;
1571 Struct_
= JSClassCreate(&definition
);
1573 definition
= kJSClassDefinitionEmpty
;
1574 definition
.className
= "Type";
1575 definition
.staticValues
= Type_staticValues
;
1576 definition
.staticFunctions
= Type_staticFunctions
;
1577 definition
.callAsFunction
= &Type_callAsFunction
;
1578 definition
.callAsConstructor
= &Type_callAsConstructor
;
1579 definition
.finalize
= &CYFinalize
;
1580 Type_privateData::Class_
= JSClassCreate(&definition
);
1582 definition
= kJSClassDefinitionEmpty
;
1583 definition
.className
= "Global";
1584 //definition.getProperty = &Global_getProperty;
1585 Global_
= JSClassCreate(&definition
);
1587 Array_s
= JSStringCreateWithUTF8CString("Array");
1588 cy_s
= JSStringCreateWithUTF8CString("$cy");
1589 cyi_s
= JSStringCreateWithUTF8CString("$cyi");
1590 length_s
= JSStringCreateWithUTF8CString("length");
1591 message_s
= JSStringCreateWithUTF8CString("message");
1592 name_s
= JSStringCreateWithUTF8CString("name");
1593 pop_s
= JSStringCreateWithUTF8CString("pop");
1594 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1595 push_s
= JSStringCreateWithUTF8CString("push");
1596 splice_s
= JSStringCreateWithUTF8CString("splice");
1597 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1598 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1599 toPointer_s
= JSStringCreateWithUTF8CString("toPointer");
1600 toString_s
= JSStringCreateWithUTF8CString("toString");
1602 Result_
= JSStringCreateWithUTF8CString("_");
1604 if (hooks_
!= NULL
&& hooks_
->Initialize
!= NULL
)
1605 (*hooks_
->Initialize
)();
1608 void CYThrow(JSContextRef context
, JSValueRef value
) {
1610 throw CYJSError(context
, value
);
1613 const char *CYJSError::PoolCString(CYPool
&pool
) const {
1614 // XXX: this used to be CYPoolCString
1615 return CYPoolCCYON(pool
, context_
, value_
);
1618 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1619 // XXX: what if the context is different?
1623 JSValueRef
CYCastJSError(JSContextRef context
, const char *message
) {
1624 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString("Error")));
1625 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1626 return _jsccall(JSObjectCallAsConstructor
, context
, Error
, 1, arguments
);
1629 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1630 return CYCastJSError(context
, message_
);
1633 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1634 _assert(context
!= NULL
);
1639 va_start(args
, format
);
1640 // XXX: there might be a beter way to think about this
1641 const char *message(pool
.vsprintf(64, format
, args
));
1644 value_
= CYCastJSError(context
, message
);
1647 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1648 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1651 extern "C" bool CydgetMemoryParse(const uint16_t **data
, size_t *size
);
1653 void *CYMapFile(const char *path
, size_t *psize
) {
1655 _syscall(fd
= open(path
, O_RDONLY
));
1658 _syscall(fstat(fd
, &stat
));
1659 size_t size(stat
.st_size
);
1664 _syscall(base
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, 0));
1666 _syscall(close(fd
));
1670 static void CYRunSetups(JSContextRef context
) {
1671 std::string
folder("/etc/cycript/setup.d");
1672 DIR *setups(opendir(folder
.c_str()));
1679 _syscall(readdir_r(setups
, &setup
, &result
));
1683 _assert(result
== &setup
);
1685 const char *name(setup
.d_name
);
1686 size_t length(strlen(name
));
1692 if (memcmp(name
+ length
- 3, ".cy", 3) != 0)
1695 std::string
script(folder
+ "/" + name
);
1697 utf8
.data
= reinterpret_cast<char *>(CYMapFile(script
.c_str(), &utf8
.size
));
1700 CYUTF16String
utf16(CYPoolUTF16String(pool
, utf8
));
1701 munmap(const_cast<char *>(utf8
.data
), utf8
.size
);
1703 // XXX: this should not be used
1704 CydgetMemoryParse(&utf16
.data
, &utf16
.size
);
1706 CYExecute(context
, pool
, CYPoolUTF8String(pool
, utf16
));
1707 free(const_cast<uint16_t *>(utf16
.data
));
1710 _syscall(closedir(setups
));
1713 static JSValueRef
require(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1714 _assert(count
== 1);
1718 _assert(dladdr(reinterpret_cast<void *>(&require
), &addr
) != 0);
1719 char *lib(pool
.strdup(addr
.dli_fname
));
1721 char *slash(strrchr(lib
, '/'));
1722 _assert(slash
!= NULL
);
1725 CYJSString
property("exports");
1728 const char *path(pool
.strcat(lib
, "/cycript/", CYPoolCString(pool
, context
, arguments
[0]), ".cy", NULL
));
1729 CYJSString
key(path
);
1730 JSObjectRef
modules(CYGetCachedObject(context
, CYJSString("modules")));
1731 JSValueRef
cache(CYGetProperty(context
, modules
, key
));
1733 if (!JSValueIsUndefined(context
, cache
))
1734 module = CYCastJSObject(context
, cache
);
1737 code
.data
= reinterpret_cast<char *>(CYMapFile(path
, &code
.size
));
1739 std::stringstream wrap
;
1740 wrap
<< "(function (exports, require, module) { " << code
<< "\n});";
1741 code
= CYPoolCode(pool
, wrap
.str().c_str());
1743 JSValueRef
value(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0));
1744 JSObjectRef
function(CYCastJSObject(context
, value
));
1746 module = JSObjectMake(context
, NULL
, NULL
);
1747 JSObjectRef
exports(JSObjectMake(context
, NULL
, NULL
));
1748 CYSetProperty(context
, module, property
, exports
);
1750 JSValueRef arguments
[3] = { exports
, JSObjectMakeFunctionWithCallback(context
, CYJSString("require"), &require
), module };
1751 CYCallAsFunction(context
, function
, NULL
, 3, arguments
);
1752 CYSetProperty(context
, modules
, key
, module);
1755 return CYGetProperty(context
, module, property
);
1758 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
1759 CYInitializeDynamic();
1761 JSObjectRef
global(CYGetGlobalObject(context
));
1763 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
1764 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
1766 /* Cache Globals {{{ */
1767 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
1768 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
1770 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
1771 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
1773 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
1774 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
1776 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
1777 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
1779 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
1780 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
1782 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1783 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
1785 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
1786 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
1788 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
1789 CYSetProperty(context
, cy
, CYJSString("String"), String
);
1791 JSObjectRef
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
)));
1792 CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
);
1795 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1796 CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1798 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1799 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1800 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
1802 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1803 JSObjectSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
1804 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
1806 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1807 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1809 JSObjectRef
modules(JSObjectMake(context
, NULL
, NULL
));
1810 CYSetProperty(context
, cy
, CYJSString("modules"), modules
);
1812 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
1813 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
1815 JSObjectRef
alls(_jsccall(JSObjectCallAsConstructor
, context
, Array
, 0, NULL
));
1816 CYSetProperty(context
, cycript
, CYJSString("alls"), alls
);
1819 JSObjectRef
last(NULL
), curr(global
);
1821 goto next
; for (JSValueRef next
;;) {
1822 if (JSValueIsNull(context
, next
))
1825 curr
= CYCastJSObject(context
, next
);
1827 next
= JSObjectGetPrototype(context
, curr
);
1830 JSObjectSetPrototype(context
, last
, all
);
1833 CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
, kJSPropertyAttributeDontEnum
);
1835 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
1836 CYSetProperty(context
, cy
, CYJSString("System"), System
);
1838 CYSetProperty(context
, all
, CYJSString("require"), &require
, kJSPropertyAttributeDontEnum
);
1840 CYSetProperty(context
, global
, CYJSString("system"), System
);
1841 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
1842 //CYSetProperty(context, System, CYJSString("global"), global);
1843 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
1845 if (CYBridgeEntry
*entry
= CYBridgeHash("1dlerror", 8))
1846 entry
->cache_
= new cy::Functor(entry
->value_
, reinterpret_cast<void (*)()>(&dlerror
));
1848 if (hooks_
!= NULL
&& hooks_
->SetupContext
!= NULL
)
1849 (*hooks_
->SetupContext
)(context
);
1851 CYArrayPush(context
, alls
, cycript
);
1853 CYRunSetups(context
);
1856 static JSGlobalContextRef context_
;
1858 JSGlobalContextRef
CYGetJSContext() {
1859 CYInitializeDynamic();
1861 if (context_
== NULL
) {
1862 context_
= JSGlobalContextCreate(Global_
);
1863 CYSetupContext(context_
);
1869 void CYDestroyContext() {
1870 if (context_
== NULL
)
1872 JSGlobalContextRelease(context_
);