1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
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 static std::vector
<CYHook
*> &GetHooks() {
54 static std::vector
<CYHook
*> hooks
;
58 CYRegisterHook::CYRegisterHook(CYHook
*hook
) {
59 GetHooks().push_back(hook
);
62 /* JavaScript Properties {{{ */
63 bool CYHasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
64 return JSObjectHasProperty(context
, object
, name
);
67 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) {
68 return _jsccall(JSObjectGetPropertyAtIndex
, context
, object
, index
);
71 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
72 return _jsccall(JSObjectGetProperty
, context
, object
, name
);
75 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) {
76 _jsccall(JSObjectSetPropertyAtIndex
, context
, object
, index
, value
);
79 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) {
80 _jsccall(JSObjectSetProperty
, context
, object
, name
, value
, attributes
);
83 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef (*callback
)(JSContextRef
, JSObjectRef
, JSObjectRef
, size_t, const JSValueRef
[], JSValueRef
*), JSPropertyAttributes attributes
) {
84 CYSetProperty(context
, object
, name
, JSObjectMakeFunctionWithCallback(context
, name
, callback
), attributes
);
87 void CYSetPrototype(JSContextRef context
, JSObjectRef object
, JSValueRef value
) {
88 JSObjectSetPrototype(context
, object
, value
);
89 _assert(CYIsStrictEqual(context
, JSObjectGetPrototype(context
, object
), value
));
92 /* JavaScript Strings {{{ */
93 JSStringRef
CYCopyJSString(const char *value
) {
94 return value
== NULL
? NULL
: JSStringCreateWithUTF8CString(value
);
97 JSStringRef
CYCopyJSString(JSStringRef value
) {
98 return value
== NULL
? NULL
: JSStringRetain(value
);
101 JSStringRef
CYCopyJSString(CYUTF8String value
) {
102 // XXX: this is very wrong; it needs to convert to UTF16 and then create from there
103 return CYCopyJSString(value
.data
);
106 JSStringRef
CYCopyJSString(JSContextRef context
, JSValueRef value
) {
107 if (JSValueIsNull(context
, value
))
109 return _jsccall(JSValueToStringCopy
, context
, value
);
112 static CYUTF16String
CYCastUTF16String(JSStringRef value
) {
113 return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
));
116 CYUTF8String
CYPoolUTF8String(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
117 return CYPoolUTF8String(pool
, CYCastUTF16String(value
));
120 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
121 CYUTF8String
utf8(CYPoolUTF8String(pool
, context
, value
));
122 _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
);
126 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
127 return JSValueIsNull(context
, value
) ? NULL
: CYPoolCString(pool
, context
, CYJSString(context
, value
));
130 /* Index Offsets {{{ */
131 size_t CYGetIndex(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
132 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
136 static JSClassRef All_
;
137 static JSClassRef Context_
;
139 static JSClassRef Global_
;
140 static JSClassRef Pointer_
;
141 static JSClassRef Struct_
;
146 JSStringRef length_s
;
147 JSStringRef message_s
;
150 JSStringRef prototype_s
;
152 JSStringRef splice_s
;
153 JSStringRef toCYON_s
;
154 JSStringRef toJSON_s
;
155 JSStringRef toPointer_s
;
156 JSStringRef toString_s
;
159 static JSStringRef Result_
;
161 void CYFinalize(JSObjectRef object
) {
162 CYData
*internal(reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
)));
163 _assert(internal
->count_
!= _not(unsigned));
164 if (--internal
->count_
== 0)
168 void Structor_(CYPool
&pool
, sig::Type
*&type
) {
170 type
->primitive
== sig::pointer_P
&&
171 type
->data
.data
.type
->primitive
== sig::struct_P
&&
172 type
->data
.data
.type
->name
!= NULL
&&
173 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
175 type
->primitive
= sig::typename_P
;
176 type
->data
.data
.type
= NULL
;
180 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
183 size_t length(strlen(type
->name
));
184 char keyed
[length
+ 2];
185 memcpy(keyed
+ 1, type
->name
, length
+ 1);
187 static const char *modes
= "34";
188 for (size_t i(0); i
!= 2; ++i
) {
192 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
195 sig::Parse(pool
, &type
->data
.signature
, entry
->value_
, &Structor_
);
199 sig::Signature signature
;
200 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
201 type
= signature
.elements
[0].type
;
207 JSClassRef
Type_privateData::Class_
;
212 JSGlobalContextRef context_
;
214 Context(JSGlobalContextRef context
) :
223 Type_privateData
*type_
;
226 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type
*type
) :
227 CYOwned(value
, context
, owner
),
228 type_(new(*pool_
) Type_privateData(type
)),
234 struct Struct_privateData
:
237 Type_privateData
*type_
;
239 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
240 CYOwned(NULL
, context
, owner
)
245 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
246 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
247 CYPool
&pool(*internal
->pool_
);
248 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
249 internal
->type_
= typical
;
252 internal
->value_
= data
;
254 size_t size(typical
->GetFFI()->size
);
255 void *copy(internal
->pool_
->malloc
<void>(size
));
256 memcpy(copy
, data
, size
);
257 internal
->value_
= copy
;
260 return JSObjectMake(context
, Struct_
, internal
);
263 static void *CYCastSymbol(const char *name
) {
264 for (CYHook
*hook
: GetHooks())
265 if (hook
->CastSymbol
!= NULL
)
266 if (void *value
= (*hook
->CastSymbol
)(name
))
268 return dlsym(RTLD_DEFAULT
, name
);
271 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
272 return JSValueMakeBoolean(context
, value
);
275 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
276 return JSValueMakeNumber(context
, value
);
279 #define CYCastJSValue_(Type_) \
280 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
281 return JSValueMakeNumber(context, static_cast<double>(value)); \
285 CYCastJSValue_(unsigned int)
286 CYCastJSValue_(long int)
287 CYCastJSValue_(long unsigned int)
288 CYCastJSValue_(long long int)
289 CYCastJSValue_(long long unsigned int)
291 JSValueRef
CYJSUndefined(JSContextRef context
) {
292 return JSValueMakeUndefined(context
);
295 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
296 return _jsccall(JSValueToNumber
, context
, value
);
299 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
300 return JSValueToBoolean(context
, value
);
303 JSValueRef
CYJSNull(JSContextRef context
) {
304 return JSValueMakeNull(context
);
307 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
308 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
311 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
312 return CYCastJSValue(context
, CYJSString(value
));
315 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
316 return _jsccall(JSValueToObject
, context
, value
);
319 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
320 return _jsccall(JSObjectCallAsFunction
, context
, function
, _this
, count
, arguments
);
323 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
324 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
327 bool CYIsEqual(JSContextRef context
, JSValueRef lhs
, JSValueRef rhs
) {
328 return _jsccall(JSValueIsEqual
, context
, lhs
, rhs
);
331 bool CYIsStrictEqual(JSContextRef context
, JSValueRef lhs
, JSValueRef rhs
) {
332 return JSValueIsStrictEqual(context
, lhs
, rhs
);
335 size_t CYArrayLength(JSContextRef context
, JSObjectRef array
) {
336 return CYCastDouble(context
, CYGetProperty(context
, array
, length_s
));
339 JSValueRef
CYArrayGet(JSContextRef context
, JSObjectRef array
, size_t index
) {
340 return _jsccall(JSObjectGetPropertyAtIndex
, context
, array
, index
);
343 void CYArrayPush(JSContextRef context
, JSObjectRef array
, JSValueRef value
) {
344 JSValueRef arguments
[1];
345 arguments
[0] = value
;
346 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
347 _jsccall(JSObjectCallAsFunction
, context
, CYCastJSObject(context
, CYGetProperty(context
, Array
, push_s
)), array
, 1, arguments
);
350 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
355 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
358 return CYJSUndefined(context
);
361 static size_t Nonce_(0);
363 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
365 const char *name(pool
.strcat(CYPoolCString(pool
, context
, arguments
[0]), pool
.itoa(Nonce_
++), NULL
));
366 return CYCastJSValue(context
, name
);
369 static void (*JSSynchronousGarbageCollectForDebugging$
)(JSContextRef
);
371 void CYGarbageCollect(JSContextRef context
) {
372 (JSSynchronousGarbageCollectForDebugging$
?: &JSGarbageCollect
)(context
);
375 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
376 CYGarbageCollect(context
);
377 return CYJSUndefined(context
);
380 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> &objects
, JSValueRef
*exception
) { CYTry
{
381 switch (JSType type
= JSValueGetType(context
, value
)) {
382 case kJSTypeUndefined
:
387 return CYCastBool(context
, value
) ? "true" : "false";
389 case kJSTypeNumber
: {
390 std::ostringstream str
;
391 CYNumerify(str
, CYCastDouble(context
, value
));
392 std::string
value(str
.str());
393 return pool
.strmemdup(value
.c_str(), value
.size());
396 case kJSTypeString
: {
397 std::ostringstream str
;
398 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
399 CYStringify(str
, string
.data
, string
.size
);
400 std::string
value(str
.str());
401 return pool
.strmemdup(value
.c_str(), value
.size());
405 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
, objects
);
407 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
411 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> &objects
) {
412 return _jsccall(CYPoolCCYON
, pool
, context
, value
, objects
);
415 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> *objects
) {
417 return CYPoolCCYON(pool
, context
, value
, *objects
);
419 std::set
<void *> objects
;
420 return CYPoolCCYON(pool
, context
, value
, objects
);
424 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSObjectRef object
, std::set
<void *> &objects
) {
425 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
426 if (CYIsCallable(context
, toCYON
)) {
427 // XXX: this needs to be abstracted behind some kind of function
428 JSValueRef arguments
[1] = {CYCastJSValue(context
, static_cast<double>(reinterpret_cast<uintptr_t>(&objects
)))};
429 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 1, arguments
));
430 _assert(value
!= NULL
);
431 return CYPoolCString(pool
, context
, value
);
434 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
435 if (CYIsCallable(context
, toJSON
)) {
436 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
437 return _jsccall(CYPoolCCYON
, pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), objects
);
440 if (JSObjectIsFunction(context
, object
)) {
441 JSValueRef
toString(CYGetProperty(context
, object
, toString_s
));
442 if (CYIsCallable(context
, toString
)) {
443 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
444 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toString
, object
, 1, arguments
));
445 _assert(value
!= NULL
);
446 return CYPoolCString(pool
, context
, value
);
450 _assert(objects
.insert(object
).second
);
452 std::ostringstream str
;
456 // XXX: this is, sadly, going to leak
457 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
461 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
467 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
468 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
473 CYStringify(str
, string
.data
, string
.size
);
478 JSValueRef
value(CYGetProperty(context
, object
, name
));
479 str
<< CYPoolCCYON(pool
, context
, value
, objects
);
480 } catch (const CYException
&error
) {
485 JSPropertyNameArrayRelease(names
);
489 std::string
string(str
.str());
490 return pool
.strmemdup(string
.c_str(), string
.size());
493 std::set
<void *> *CYCastObjects(JSContextRef context
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
496 return CYCastPointer
<std::set
<void *> *>(context
, arguments
[0]);
499 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
500 std::set
<void *> *objects(CYCastObjects(context
, _this
, count
, arguments
));
501 // XXX: this is horribly inefficient
502 std::set
<void *> backup
;
507 std::ostringstream str
;
511 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
514 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
521 JSValueRef
value(CYGetProperty(context
, _this
, index
));
522 if (!JSValueIsUndefined(context
, value
))
523 str
<< CYPoolCCYON(pool
, context
, value
, *objects
);
528 } catch (const CYException
&error
) {
535 std::string
value(str
.str());
536 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
539 static JSValueRef
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
541 std::ostringstream str
;
543 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
)));
544 CYStringify(str
, string
.data
, string
.size
);
546 std::string
value(str
.str());
547 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
550 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
551 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
552 return JSObjectMake(context
, Pointer_
, internal
);
555 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const sig::Signature
&signature
) {
556 return JSObjectMake(context
, Functor_
, new cy::Functor(signature
, function
));
559 static JSObjectRef
CYMakeFunctor(JSContextRef context
, const char *symbol
, const char *encoding
, void **cache
) {
560 cy::Functor
*internal
;
562 internal
= reinterpret_cast<cy::Functor
*>(*cache
);
564 void (*function
)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol
)));
565 if (function
== NULL
)
568 internal
= new cy::Functor(encoding
, function
);
573 return JSObjectMake(context
, Functor_
, internal
);
576 static bool CYGetOffset(CYPool
&pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
577 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
580 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
583 else switch (JSValueGetType(context
, value
)) {
586 case kJSTypeObject
: {
587 JSObjectRef
object((JSObjectRef
) value
);
588 if (JSValueIsObjectOfClass(context
, value
, Pointer_
)) {
589 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
590 return internal
->value_
;
592 JSValueRef
toPointer(CYGetProperty(context
, object
, toPointer_s
));
593 if (CYIsCallable(context
, toPointer
)) {
594 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
));
595 _assert(value
!= NULL
);
596 return CYCastPointer_(context
, value
);
599 double number(CYCastDouble(context
, value
));
600 if (std::isnan(number
))
601 throw CYJSError(context
, "cannot convert value to pointer");
602 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
606 void CYPoolFFI(CYPool
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
607 switch (type
->primitive
) {
609 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
612 #define CYPoolFFI_(primitive, native) \
613 case sig::primitive ## _P: \
614 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
617 CYPoolFFI_(uchar
, unsigned char)
618 CYPoolFFI_(char, char)
619 CYPoolFFI_(ushort
, unsigned short)
620 CYPoolFFI_(short, short)
621 CYPoolFFI_(ulong
, unsigned long)
622 CYPoolFFI_(long, long)
623 CYPoolFFI_(uint
, unsigned int)
625 CYPoolFFI_(ulonglong
, unsigned long long)
626 CYPoolFFI_(longlong
, long long)
627 CYPoolFFI_(float, float)
628 CYPoolFFI_(double, double)
631 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
632 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
633 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
634 ffi_type
*field(ffi
->elements
[index
]);
637 if (aggregate
== NULL
)
640 rhs
= CYGetProperty(context
, aggregate
, index
);
641 if (JSValueIsUndefined(context
, rhs
))
642 throw CYJSError(context
, "unable to extract array value");
645 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
652 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
656 _assert(pool
!= NULL
);
657 *reinterpret_cast<const char **>(data
) = CYPoolCString(*pool
, context
, value
);
660 case sig::struct_P
: {
661 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
662 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
663 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
664 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
665 ffi_type
*field(ffi
->elements
[index
]);
668 if (aggregate
== NULL
)
671 rhs
= CYGetProperty(context
, aggregate
, index
);
672 if (JSValueIsUndefined(context
, rhs
)) {
673 if (element
->name
!= NULL
)
674 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
677 if (JSValueIsUndefined(context
, rhs
)) undefined
:
678 throw CYJSError(context
, "unable to extract structure value");
682 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
692 for (CYHook
*hook
: GetHooks())
693 if (hook
->PoolFFI
!= NULL
)
694 if ((*hook
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
697 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
701 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
702 switch (type
->primitive
) {
704 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
706 #define CYFromFFI_(primitive, native) \
707 case sig::primitive ## _P: \
708 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
710 CYFromFFI_(uchar, unsigned char)
711 CYFromFFI_(char, char)
712 CYFromFFI_(ushort
, unsigned short)
713 CYFromFFI_(short, short)
714 CYFromFFI_(ulong
, unsigned long)
715 CYFromFFI_(long, long)
716 CYFromFFI_(uint
, unsigned int)
718 CYFromFFI_(ulonglong
, unsigned long long)
719 CYFromFFI_(longlong
, long long)
720 CYFromFFI_(float, float)
721 CYFromFFI_(double, double)
724 if (void *pointer
= data
)
725 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
729 if (void *pointer
= *reinterpret_cast<void **>(data
))
730 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
734 if (char *utf8
= *reinterpret_cast<char **>(data
))
735 return CYCastJSValue(context
, utf8
);
739 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
741 return CYJSUndefined(context
);
744 return CYJSNull(context
);
746 for (CYHook
*hook
: GetHooks())
747 if (hook
->FromFFI
!= NULL
)
748 if (JSValueRef value
= (*hook
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
751 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
755 void CYExecuteClosure(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
, JSValueRef (*adapter
)(JSContextRef
, size_t, JSValueRef
[], JSObjectRef
)) {
756 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
758 JSContextRef
context(internal
->context_
);
760 size_t count(internal
->cif_
.nargs
);
761 JSValueRef values
[count
];
763 for (size_t index(0); index
!= count
; ++index
)
764 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
766 JSValueRef
value(adapter(context
, count
, values
, internal
->function_
));
767 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
770 static JSValueRef
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
) {
771 return CYCallAsFunction(context
, function
, NULL
, count
, values
);
774 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
775 CYExecuteClosure(cif
, result
, arguments
, arg
, &FunctionAdapter_
);
778 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
779 // XXX: in case of exceptions this will leak
780 // XXX: in point of fact, this may /need/ to leak :(
781 Closure_privateData
*internal(new Closure_privateData(context
, function
, signature
));
783 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
785 ffi_closure
*writable(reinterpret_cast<ffi_closure
*>(ffi_closure_alloc(sizeof(ffi_closure
), &executable
)));
787 ffi_status
status(ffi_prep_closure_loc(writable
, &internal
->cif_
, callback
, internal
, executable
));
788 _assert(status
== FFI_OK
);
790 internal
->value_
= executable
;
792 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
793 NULL
, sizeof(ffi_closure
),
794 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
798 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
799 _assert(status
== FFI_OK
);
801 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
803 internal
->value_
= closure
;
809 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
) {
810 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, signature
, &FunctionClosure_
));
811 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
812 // XXX: see above notes about needing to leak
813 JSValueProtect(CYGetJSContext(context
), object
);
817 JSValueRef
CYGetCachedValue(JSContextRef context
, JSStringRef name
) {
818 return CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
);
821 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
822 return CYCastJSObject(context
, CYGetCachedValue(context
, name
));
825 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const sig::Signature
&signature
) {
826 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
828 bool function(_jsccall(JSValueIsInstanceOfConstructor
, context
, value
, Function
));
830 JSObjectRef
function(CYCastJSObject(context
, value
));
831 return CYMakeFunctor(context
, function
, signature
);
833 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
834 return CYMakeFunctor(context
, function
, signature
);
838 static bool Index_(CYPool
&pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
839 Type_privateData
*typical(internal
->type_
);
840 sig::Type
*type(typical
->type_
);
844 const char *name(CYPoolCString(pool
, context
, property
));
845 size_t length(strlen(name
));
846 double number(CYCastDouble(name
, length
));
848 size_t count(type
->data
.signature
.count
);
850 if (std::isnan(number
)) {
851 if (property
== NULL
)
854 sig::Element
*elements(type
->data
.signature
.elements
);
856 for (size_t local(0); local
!= count
; ++local
) {
857 sig::Element
*element(&elements
[local
]);
858 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
866 index
= static_cast<ssize_t
>(number
);
867 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
872 ffi_type
**elements(typical
->GetFFI()->elements
);
874 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
875 for (ssize_t
local(0); local
!= index
; ++local
)
876 base
+= elements
[local
]->size
;
881 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
883 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
885 if (JSStringIsEqual(property
, length_s
))
886 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
888 Type_privateData
*typical(internal
->type_
);
889 if (typical
->type_
== NULL
)
891 sig::Type
&type(*typical
->type_
);
894 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
896 else if (!CYGetOffset(pool
, context
, property
, offset
))
899 if (type
.primitive
== sig::function_P
)
900 return CYMakeFunctor(context
, reinterpret_cast<void (*)()>(internal
->value_
), type
.data
.signature
);
902 ffi_type
*ffi(typical
->GetFFI());
904 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
905 base
+= ffi
->size
* offset
;
907 JSObjectRef
owner(internal
->GetOwner() ?: object
);
908 return CYFromFFI(context
, &type
, ffi
, base
, false, owner
);
911 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
913 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
914 Type_privateData
*typical(internal
->type_
);
916 if (typical
->type_
== NULL
)
920 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
922 else if (!CYGetOffset(pool
, context
, property
, offset
))
925 ffi_type
*ffi(typical
->GetFFI());
927 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
928 base
+= ffi
->size
* offset
;
930 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
934 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
935 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
936 Type_privateData
*typical(internal
->type_
);
937 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
940 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
942 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
943 Type_privateData
*typical(internal
->type_
);
948 if (!Index_(pool
, context
, internal
, property
, index
, base
))
951 JSObjectRef
owner(internal
->GetOwner() ?: object
);
953 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
956 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
958 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
959 Type_privateData
*typical(internal
->type_
);
964 if (!Index_(pool
, context
, internal
, property
, index
, base
))
967 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
971 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
972 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
973 Type_privateData
*typical(internal
->type_
);
974 sig::Type
*type(typical
->type_
);
979 size_t count(type
->data
.signature
.count
);
980 sig::Element
*elements(type
->data
.signature
.elements
);
984 for (size_t index(0); index
!= count
; ++index
) {
986 name
= elements
[index
].name
;
989 sprintf(number
, "%zu", index
);
993 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
997 void CYCallFunction(CYPool
&pool
, JSContextRef context
, ffi_cif
*cif
, void (*function
)(), void *value
, void **values
) {
998 ffi_call(cif
, function
, value
, values
);
1001 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
)()) {
1002 if (setups
+ count
!= signature
->count
- 1)
1003 throw CYJSError(context
, "incorrect number of arguments to ffi function");
1005 size_t size(setups
+ count
);
1007 memcpy(values
, setup
, sizeof(void *) * setups
);
1009 for (size_t index(setups
); index
!= size
; ++index
) {
1010 sig::Element
*element(&signature
->elements
[index
+ 1]);
1011 ffi_type
*ffi(cif
->arg_types
[index
]);
1013 values
[index
] = new(pool
) uint8_t[ffi
->size
];
1014 CYPoolFFI(&pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
1017 uint8_t value
[cif
->rtype
->size
];
1019 void (*call
)(CYPool
&, JSContextRef
, ffi_cif
*, void (*)(), void *, void **) = &CYCallFunction
;
1020 // XXX: this only supports one hook, but it is a bad idea anyway
1021 for (CYHook
*hook
: GetHooks())
1022 if (hook
->CallFunction
!= NULL
)
1023 call
= hook
->CallFunction
;
1025 call(pool
, context
, cif
, function
, value
, values
);
1026 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
1029 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1031 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1032 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
1035 JSObjectRef
CYMakeType(JSContextRef context
, const char *encoding
) {
1036 Type_privateData
*internal(new Type_privateData(encoding
));
1037 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1040 JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
1041 Type_privateData
*internal(new Type_privateData(type
));
1042 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1045 JSObjectRef
CYMakeType(JSContextRef context
, sig::Signature
*signature
) {
1052 type
.primitive
= sig::function_P
;
1053 sig::Copy(pool
, type
.data
.signature
, *signature
);
1055 return CYMakeType(context
, &type
);
1058 static bool All_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
1059 JSObjectRef
global(CYGetGlobalObject(context
));
1060 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1061 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1063 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1064 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1065 if (CYHasProperty(context
, space
, property
))
1069 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
1071 size_t length(name
.size
);
1072 char keyed
[length
+ 2];
1073 memcpy(keyed
+ 1, name
.data
, length
+ 1);
1075 static const char *modes
= "0124";
1076 for (size_t i(0); i
!= 4; ++i
) {
1077 keyed
[0] = modes
[i
];
1078 if (CYBridgeHash(keyed
, length
+ 1) != NULL
)
1085 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1086 JSObjectRef
global(CYGetGlobalObject(context
));
1087 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1088 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1090 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1091 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1092 if (JSValueRef value
= CYGetProperty(context
, space
, property
))
1093 if (!JSValueIsUndefined(context
, value
))
1097 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
1099 size_t length(name
.size
);
1100 char keyed
[length
+ 2];
1101 memcpy(keyed
+ 1, name
.data
, length
+ 1);
1103 static const char *modes
= "0124";
1104 for (size_t i(0); i
!= 4; ++i
) {
1105 char mode(modes
[i
]);
1108 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
1111 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
);
1114 return CYMakeFunctor(context
, name
.data
, entry
->value_
, &entry
->cache_
);
1117 if (void *symbol
= CYCastSymbol(name
.data
)) {
1118 // XXX: this is horrendously inefficient
1119 sig::Signature signature
;
1120 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
1122 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1123 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
1126 // XXX: implement case 3
1128 return CYMakeType(context
, entry
->value_
);
1135 static void All_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1136 JSObjectRef
global(CYGetGlobalObject(context
));
1137 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1138 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1140 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1141 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1))) {
1142 JSPropertyNameArrayRef
subset(JSObjectCopyPropertyNames(context
, space
));
1143 for (size_t index(0), count(JSPropertyNameArrayGetCount(subset
)); index
!= count
; ++index
)
1144 JSPropertyNameAccumulatorAddName(names
, JSPropertyNameArrayGetNameAtIndex(subset
, index
));
1145 JSPropertyNameArrayRelease(subset
);
1149 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1151 throw CYJSError(context
, "incorrect number of arguments to Pointer constructor");
1155 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1156 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1158 sig::Signature signature
;
1159 sig::Parse(pool
, &signature
, type
, &Structor_
);
1161 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1164 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1166 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1168 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1169 return CYMakeType(context
, type
);
1172 static JSValueRef Type_callAsFunction_$
With(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], sig::Primitive primitive
, JSValueRef
*exception
) { CYTry
{
1173 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1181 type
.primitive
= primitive
;
1182 type
.data
.signature
.elements
= new(pool
) sig::Element
[1 + count
];
1183 type
.data
.signature
.count
= 1 + count
;
1185 type
.data
.signature
.elements
[0].name
= NULL
;
1186 type
.data
.signature
.elements
[0].type
= internal
->type_
;
1187 type
.data
.signature
.elements
[0].offset
= _not(size_t);
1189 for (size_t i(0); i
!= count
; ++i
) {
1190 sig::Element
&element(type
.data
.signature
.elements
[i
+ 1]);
1191 element
.name
= NULL
;
1192 element
.offset
= _not(size_t);
1194 JSObjectRef
object(CYCastJSObject(context
, arguments
[i
]));
1195 _assert(JSValueIsObjectOfClass(context
, object
, Type_privateData::Class_
));
1196 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1198 element
.type
= internal
->type_
;
1201 return CYMakeType(context
, &type
);
1204 static JSValueRef
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1206 throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf");
1207 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1210 size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0])));
1211 if (index
== _not(size_t))
1212 throw CYJSError(context
, "invalid array size used with Type.arrayOf");
1218 type
.primitive
= sig::array_P
;
1219 type
.data
.data
.type
= internal
->type_
;
1220 type
.data
.data
.size
= index
;
1222 return CYMakeType(context
, &type
);
1225 static JSValueRef
Type_callAsFunction_blockWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1226 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::block_P
, exception
);
1229 static JSValueRef
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1231 throw CYJSError(context
, "incorrect number of arguments to Type.constant");
1232 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1234 sig::Type
type(*internal
->type_
);
1235 type
.flags
|= JOC_TYPE_CONST
;
1236 return CYMakeType(context
, &type
);
1239 static JSValueRef
Type_callAsFunction_long(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1241 throw CYJSError(context
, "incorrect number of arguments to Type.long");
1242 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1244 sig::Type
type(*internal
->type_
);
1246 switch (type
.primitive
) {
1247 case sig::short_P
: type
.primitive
= sig::int_P
; break;
1248 case sig::int_P
: type
.primitive
= sig::long_P
; break;
1249 case sig::long_P
: type
.primitive
= sig::longlong_P
; break;
1250 default: throw CYJSError(context
, "invalid type argument to Type.long");
1253 return CYMakeType(context
, &type
);
1256 static JSValueRef
Type_callAsFunction_short(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1258 throw CYJSError(context
, "incorrect number of arguments to Type.short");
1259 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1261 sig::Type
type(*internal
->type_
);
1263 switch (type
.primitive
) {
1264 case sig::int_P
: type
.primitive
= sig::short_P
; break;
1265 case sig::long_P
: type
.primitive
= sig::int_P
; break;
1266 case sig::longlong_P
: type
.primitive
= sig::long_P
; break;
1267 default: throw CYJSError(context
, "invalid type argument to Type.short");
1270 return CYMakeType(context
, &type
);
1273 static JSValueRef
Type_callAsFunction_signed(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1275 throw CYJSError(context
, "incorrect number of arguments to Type.signed");
1276 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1278 sig::Type
type(*internal
->type_
);
1280 switch (type
.primitive
) {
1281 case sig::char_P
: case sig::uchar_P
: type
.primitive
= sig::char_P
; break;
1282 case sig::short_P
: case sig::ushort_P
: type
.primitive
= sig::short_P
; break;
1283 case sig::int_P
: case sig::uint_P
: type
.primitive
= sig::int_P
; break;
1284 case sig::long_P
: case sig::ulong_P
: type
.primitive
= sig::long_P
; break;
1285 case sig::longlong_P
: case sig::ulonglong_P
: type
.primitive
= sig::longlong_P
; break;
1286 default: throw CYJSError(context
, "invalid type argument to Type.signed");
1289 return CYMakeType(context
, &type
);
1292 static JSValueRef
Type_callAsFunction_unsigned(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1294 throw CYJSError(context
, "incorrect number of arguments to Type.unsigned");
1295 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1297 sig::Type
type(*internal
->type_
);
1299 switch (type
.primitive
) {
1300 case sig::char_P
: case sig::uchar_P
: type
.primitive
= sig::uchar_P
; break;
1301 case sig::short_P
: case sig::ushort_P
: type
.primitive
= sig::ushort_P
; break;
1302 case sig::int_P
: case sig::uint_P
: type
.primitive
= sig::uint_P
; break;
1303 case sig::long_P
: case sig::ulong_P
: type
.primitive
= sig::ulong_P
; break;
1304 case sig::longlong_P
: case sig::ulonglong_P
: type
.primitive
= sig::ulonglong_P
; break;
1305 default: throw CYJSError(context
, "invalid type argument to Type.unsigned");
1308 return CYMakeType(context
, &type
);
1311 static JSValueRef
Type_callAsFunction_functionWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1312 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::function_P
, exception
);
1315 static JSValueRef
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1317 throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo");
1318 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1323 if (internal
->type_
->primitive
== sig::char_P
) {
1324 type
.flags
= internal
->type_
->flags
;
1325 type
.primitive
= sig::string_P
;
1326 type
.data
.data
.type
= NULL
;
1327 type
.data
.data
.size
= 0;
1330 type
.primitive
= sig::pointer_P
;
1331 type
.data
.data
.type
= internal
->type_
;
1332 type
.data
.data
.size
= 0;
1335 return CYMakeType(context
, &type
);
1338 static JSValueRef
Type_callAsFunction_withName(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1340 throw CYJSError(context
, "incorrect number of arguments to Type.withName");
1341 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1344 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1346 sig::Type
type(*internal
->type_
);
1348 return CYMakeType(context
, &type
);
1351 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1353 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1354 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1356 if (internal
->type_
->primitive
== sig::function_P
)
1357 return CYMakeFunctor(context
, arguments
[0], internal
->type_
->data
.signature
);
1359 sig::Type
*type(internal
->type_
);
1360 ffi_type
*ffi(internal
->GetFFI());
1362 uint8_t value
[ffi
->size
];
1364 CYPoolFFI(&pool
, context
, type
, ffi
, value
, arguments
[0]);
1365 return CYFromFFI(context
, type
, ffi
, value
);
1368 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1370 throw CYJSError(context
, "incorrect number of arguments to Type allocator");
1371 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1373 sig::Type
*type(internal
->type_
);
1376 if (type
->primitive
!= sig::array_P
)
1377 length
= _not(size_t);
1379 length
= type
->data
.data
.size
;
1380 type
= type
->data
.data
.type
;
1383 void *value(calloc(1, internal
->GetFFI()->size
));
1384 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1387 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1389 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1391 const char *encoding(CYPoolCString(pool
, context
, arguments
[1]));
1392 sig::Signature signature
;
1393 sig::Parse(pool
, &signature
, encoding
, &Structor_
);
1394 return CYMakeFunctor(context
, arguments
[0], signature
);
1397 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1398 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1399 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1402 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1403 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1406 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1407 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1409 sprintf(string
, "%p", internal
->value_
);
1410 return CYCastJSValue(context
, string
);
1413 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1414 std::set
<void *> *objects(CYCastObjects(context
, _this
, count
, arguments
));
1416 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1417 if (internal
->length_
!= _not(size_t)) {
1418 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
1419 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1420 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1421 } else if (internal
->type_
->type_
== NULL
) pointer
: {
1423 sprintf(string
, "%p", internal
->value_
);
1424 return CYCastJSValue(context
, string
);
1426 JSValueRef
value(CYGetProperty(context
, _this
, cyi_s
));
1427 if (JSValueIsUndefined(context
, value
))
1430 return CYCastJSValue(context
, pool
.strcat("&", CYPoolCCYON(pool
, context
, value
, objects
), NULL
));
1431 } catch (const CYException
&e
) {
1436 static JSValueRef
Pointer_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1437 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1438 return CYMakeType(context
, internal
->type_
->type_
);
1441 static JSValueRef
Functor_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1442 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1443 return CYMakeType(context
, &internal
->signature_
);
1446 static JSValueRef
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1447 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1448 return CYCastJSValue(context
, internal
->GetFFI()->alignment
);
1451 static JSValueRef
Type_getProperty_name(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1452 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1453 return CYCastJSValue(context
, internal
->type_
->name
);
1456 static JSValueRef
Type_getProperty_size(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1457 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1458 return CYCastJSValue(context
, internal
->GetFFI()->size
);
1461 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1462 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1464 const char *type(sig::Unparse(pool
, internal
->type_
));
1465 return CYCastJSValue(context
, CYJSString(type
));
1468 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1469 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1471 std::ostringstream out
;
1473 CYOutput
output(out
, options
);
1474 (new(pool
) CYEncodedType(Decode(pool
, internal
->type_
)))->Output(output
, CYNoFlags
);
1475 return CYCastJSValue(context
, CYJSString(out
.str().c_str()));
1478 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1479 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1482 static JSStaticFunction Pointer_staticFunctions
[4] = {
1483 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1484 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1485 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1489 static JSStaticValue Pointer_staticValues
[2] = {
1490 {"type", &Pointer_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1491 {NULL
, NULL
, NULL
, 0}
1494 static JSStaticFunction Struct_staticFunctions
[2] = {
1495 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1499 static JSStaticFunction Functor_staticFunctions
[4] = {
1500 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1501 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1502 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1507 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1510 static JSStaticValue Functor_staticValues
[2] = {
1511 {"type", &Functor_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1512 {NULL
, NULL
, NULL
, 0}
1516 JSStaticValue
const * const Functor::StaticValues
= Functor_staticValues
;
1519 static JSStaticValue Type_staticValues
[4] = {
1520 {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1521 {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1522 {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1523 {NULL
, NULL
, NULL
, 0}
1526 static JSStaticFunction Type_staticFunctions
[14] = {
1527 {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1528 {"blockWith", &Type_callAsFunction_blockWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1529 {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1530 {"functionWith", &Type_callAsFunction_functionWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1531 {"long", &Type_callAsFunction_long
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1532 {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1533 {"short", &Type_callAsFunction_short
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1534 {"signed", &Type_callAsFunction_signed
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1535 {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1536 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1537 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1538 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1539 {"unsigned", &Type_callAsFunction_unsigned
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1543 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1545 void CYSetArgs(int argc
, const char *argv
[]) {
1546 JSContextRef
context(CYGetJSContext());
1547 JSValueRef args
[argc
];
1548 for (int i(0); i
!= argc
; ++i
)
1549 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1552 if (JSObjectMakeArray$
!= NULL
)
1553 array
= _jsccall(*JSObjectMakeArray$
, context
, argc
, args
);
1555 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1556 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1557 array
= CYCastJSObject(context
, value
);
1560 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1561 CYSetProperty(context
, System
, CYJSString("args"), array
);
1564 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1565 return JSContextGetGlobalObject(context
);
1568 // XXX: this is neither exceptin safe nor even terribly sane
1569 class ExecutionHandle
{
1571 JSContextRef context_
;
1572 std::vector
<void *> handles_
;
1575 ExecutionHandle(JSContextRef context
) :
1578 handles_
.resize(GetHooks().size());
1579 for (size_t i(0); i
!= GetHooks().size(); ++i
) {
1580 CYHook
*hook(GetHooks()[i
]);
1581 if (hook
->ExecuteStart
!= NULL
)
1582 handles_
[i
] = (*hook
->ExecuteStart
)(context_
);
1588 ~ExecutionHandle() {
1589 for (size_t i(GetHooks().size()); i
!= 0; --i
) {
1590 CYHook
*hook(GetHooks()[i
-1]);
1591 if (hook
->ExecuteEnd
!= NULL
)
1592 (*hook
->ExecuteEnd
)(context_
, handles_
[i
-1]);
1597 const char *CYExecute(JSContextRef context
, CYPool
&pool
, CYUTF8String code
) {
1598 JSValueRef
exception(NULL
);
1600 ExecutionHandle
handle(context
);
1602 JSValueRef result
; try {
1603 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1604 } catch (const char *error
) {
1608 if (exception
!= NULL
) error
:
1609 return CYPoolCString(pool
, context
, CYJSString(context
, exception
));
1611 if (JSValueIsUndefined(context
, result
))
1614 const char *json
; try {
1615 std::set
<void *> objects
;
1616 json
= CYPoolCCYON(pool
, context
, result
, objects
, &exception
);
1617 } catch (const char *error
) {
1621 if (exception
!= NULL
)
1624 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1629 static bool initialized_
= false;
1631 void CYInitializeDynamic() {
1633 initialized_
= true;
1636 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1637 JSSynchronousGarbageCollectForDebugging$
= reinterpret_cast<void (*)(JSContextRef
)>(dlsym(RTLD_DEFAULT
, "JSSynchronousGarbageCollectForDebugging"));
1639 JSClassDefinition definition
;
1641 definition
= kJSClassDefinitionEmpty
;
1642 definition
.className
= "All";
1643 definition
.hasProperty
= &All_hasProperty
;
1644 definition
.getProperty
= &All_getProperty
;
1645 definition
.getPropertyNames
= &All_getPropertyNames
;
1646 All_
= JSClassCreate(&definition
);
1648 definition
= kJSClassDefinitionEmpty
;
1649 definition
.className
= "Context";
1650 definition
.finalize
= &CYFinalize
;
1651 Context_
= JSClassCreate(&definition
);
1653 definition
= kJSClassDefinitionEmpty
;
1654 definition
.className
= "Functor";
1655 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1656 definition
.staticValues
= Functor_staticValues
;
1657 definition
.callAsFunction
= &Functor_callAsFunction
;
1658 definition
.finalize
= &CYFinalize
;
1659 Functor_
= JSClassCreate(&definition
);
1661 definition
= kJSClassDefinitionEmpty
;
1662 definition
.className
= "Pointer";
1663 definition
.staticFunctions
= Pointer_staticFunctions
;
1664 definition
.staticValues
= Pointer_staticValues
;
1665 definition
.getProperty
= &Pointer_getProperty
;
1666 definition
.setProperty
= &Pointer_setProperty
;
1667 definition
.finalize
= &CYFinalize
;
1668 Pointer_
= JSClassCreate(&definition
);
1670 definition
= kJSClassDefinitionEmpty
;
1671 definition
.className
= "Struct";
1672 definition
.staticFunctions
= Struct_staticFunctions
;
1673 definition
.getProperty
= &Struct_getProperty
;
1674 definition
.setProperty
= &Struct_setProperty
;
1675 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1676 definition
.finalize
= &CYFinalize
;
1677 Struct_
= JSClassCreate(&definition
);
1679 definition
= kJSClassDefinitionEmpty
;
1680 definition
.className
= "Type";
1681 definition
.staticValues
= Type_staticValues
;
1682 definition
.staticFunctions
= Type_staticFunctions
;
1683 definition
.callAsFunction
= &Type_callAsFunction
;
1684 definition
.callAsConstructor
= &Type_callAsConstructor
;
1685 definition
.finalize
= &CYFinalize
;
1686 Type_privateData::Class_
= JSClassCreate(&definition
);
1688 definition
= kJSClassDefinitionEmpty
;
1689 definition
.className
= "Global";
1690 //definition.getProperty = &Global_getProperty;
1691 Global_
= JSClassCreate(&definition
);
1693 Array_s
= JSStringCreateWithUTF8CString("Array");
1694 cy_s
= JSStringCreateWithUTF8CString("$cy");
1695 cyi_s
= JSStringCreateWithUTF8CString("$cyi");
1696 length_s
= JSStringCreateWithUTF8CString("length");
1697 message_s
= JSStringCreateWithUTF8CString("message");
1698 name_s
= JSStringCreateWithUTF8CString("name");
1699 pop_s
= JSStringCreateWithUTF8CString("pop");
1700 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1701 push_s
= JSStringCreateWithUTF8CString("push");
1702 splice_s
= JSStringCreateWithUTF8CString("splice");
1703 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1704 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1705 toPointer_s
= JSStringCreateWithUTF8CString("toPointer");
1706 toString_s
= JSStringCreateWithUTF8CString("toString");
1707 weak_s
= JSStringCreateWithUTF8CString("weak");
1709 Result_
= JSStringCreateWithUTF8CString("_");
1711 for (CYHook
*hook
: GetHooks())
1712 if (hook
->Initialize
!= NULL
)
1713 (*hook
->Initialize
)();
1716 void CYThrow(JSContextRef context
, JSValueRef value
) {
1718 throw CYJSError(context
, value
);
1721 const char *CYJSError::PoolCString(CYPool
&pool
) const {
1722 std::set
<void *> objects
;
1723 // XXX: this used to be CYPoolCString
1724 return CYPoolCCYON(pool
, context_
, value_
, objects
);
1727 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1728 // XXX: what if the context is different?
1732 JSValueRef
CYCastJSError(JSContextRef context
, const char *message
) {
1733 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString("Error")));
1734 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1735 return _jsccall(JSObjectCallAsConstructor
, context
, Error
, 1, arguments
);
1738 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1739 return CYCastJSError(context
, message_
);
1742 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1743 _assert(context
!= NULL
);
1748 va_start(args
, format
);
1749 // XXX: there might be a beter way to think about this
1750 const char *message(pool
.vsprintf(64, format
, args
));
1753 value_
= CYCastJSError(context
, message
);
1756 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1757 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1760 extern "C" bool CydgetMemoryParse(const uint16_t **data
, size_t *size
);
1762 void *CYMapFile(const char *path
, size_t *psize
) {
1763 int fd(_syscall_(open(path
, O_RDONLY
), 1, ENOENT
));
1768 _syscall(fstat(fd
, &stat
));
1769 size_t size(stat
.st_size
);
1774 _syscall(base
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, 0));
1776 _syscall(close(fd
));
1780 static JSValueRef
require(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1781 _assert(count
== 1);
1785 _assert(dladdr(reinterpret_cast<void *>(&require
), &addr
) != 0);
1786 char *lib(pool
.strdup(addr
.dli_fname
));
1788 char *slash(strrchr(lib
, '/'));
1789 _assert(slash
!= NULL
);
1792 CYJSString
property("exports");
1795 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1796 const char *path(pool
.strcat(lib
, "/cycript0.9/", name
, ".cy", NULL
));
1798 CYJSString
key(path
);
1799 JSObjectRef
modules(CYGetCachedObject(context
, CYJSString("modules")));
1800 JSValueRef
cache(CYGetProperty(context
, modules
, key
));
1802 if (!JSValueIsUndefined(context
, cache
))
1803 module = CYCastJSObject(context
, cache
);
1806 code
.data
= reinterpret_cast<char *>(CYMapFile(path
, &code
.size
));
1808 if (code
.data
== NULL
) {
1809 if (strchr(name
, '/') == NULL
&& (
1811 dlopen(pool
.strcat("/System/Library/Frameworks/", name
, ".framework/", name
, NULL
), RTLD_LAZY
| RTLD_GLOBAL
) != NULL
||
1812 dlopen(pool
.strcat("/System/Library/PrivateFrameworks/", name
, ".framework/", name
, NULL
), RTLD_LAZY
| RTLD_GLOBAL
) != NULL
||
1815 return CYJSUndefined(NULL
);
1817 CYThrow("Can't find module: %s", name
);
1820 module = JSObjectMake(context
, NULL
, NULL
);
1821 CYSetProperty(context
, modules
, key
, module);
1823 JSObjectRef
exports(JSObjectMake(context
, NULL
, NULL
));
1824 CYSetProperty(context
, module, property
, exports
);
1826 std::stringstream wrap
;
1827 wrap
<< "(function (exports, require, module) { " << code
<< "\n});";
1828 code
= CYPoolCode(pool
, wrap
);
1830 JSValueRef
value(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0));
1831 JSObjectRef
function(CYCastJSObject(context
, value
));
1833 JSValueRef arguments
[3] = { exports
, JSObjectMakeFunctionWithCallback(context
, CYJSString("require"), &require
), module };
1834 CYCallAsFunction(context
, function
, NULL
, 3, arguments
);
1837 return CYGetProperty(context
, module, property
);
1840 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
1841 CYInitializeDynamic();
1843 JSObjectRef
global(CYGetGlobalObject(context
));
1845 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
1846 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
1848 /* Cache Globals {{{ */
1849 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
1850 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
1852 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
1853 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
1855 JSObjectRef
Boolean(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Boolean"))));
1856 CYSetProperty(context
, cy
, CYJSString("Boolean"), Boolean
);
1858 JSObjectRef
Boolean_prototype(CYCastJSObject(context
, CYGetProperty(context
, Boolean
, prototype_s
)));
1859 CYSetProperty(context
, cy
, CYJSString("Boolean_prototype"), Boolean_prototype
);
1861 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
1862 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
1864 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
1865 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
1867 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
1868 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
1870 JSObjectRef
Number(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Number"))));
1871 CYSetProperty(context
, cy
, CYJSString("Number"), Number
);
1873 JSObjectRef
Number_prototype(CYCastJSObject(context
, CYGetProperty(context
, Number
, prototype_s
)));
1874 CYSetProperty(context
, cy
, CYJSString("Number_prototype"), Number_prototype
);
1876 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1877 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
1879 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
1880 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
1882 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
1883 CYSetProperty(context
, cy
, CYJSString("String"), String
);
1885 JSObjectRef
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
)));
1886 CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
);
1889 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1890 CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1892 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1893 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1894 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
1896 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1897 CYSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
1898 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
1900 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1901 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1903 JSObjectRef
modules(JSObjectMake(context
, NULL
, NULL
));
1904 CYSetProperty(context
, cy
, CYJSString("modules"), modules
);
1906 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
1907 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
1909 JSObjectRef
alls(_jsccall(JSObjectCallAsConstructor
, context
, Array
, 0, NULL
));
1910 CYSetProperty(context
, cycript
, CYJSString("alls"), alls
);
1913 JSObjectRef
last(NULL
), curr(global
);
1915 goto next
; for (JSValueRef next
;;) {
1916 if (JSValueIsNull(context
, next
))
1919 curr
= CYCastJSObject(context
, next
);
1921 next
= JSObjectGetPrototype(context
, curr
);
1924 CYSetPrototype(context
, last
, all
);
1927 CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
, kJSPropertyAttributeDontEnum
);
1929 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
1930 CYSetProperty(context
, cy
, CYJSString("System"), System
);
1932 CYSetProperty(context
, all
, CYJSString("require"), &require
, kJSPropertyAttributeDontEnum
);
1934 CYSetProperty(context
, global
, CYJSString("system"), System
);
1935 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
1936 //CYSetProperty(context, System, CYJSString("global"), global);
1937 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
1940 if (&JSWeakObjectMapCreate
!= NULL
) {
1941 JSWeakObjectMapRef
weak(JSWeakObjectMapCreate(context
, NULL
, NULL
));
1942 CYSetProperty(context
, cy
, weak_s
, CYCastJSValue(context
, reinterpret_cast<uintptr_t>(weak
)));
1946 if (CYBridgeEntry
*entry
= CYBridgeHash("1dlerror", 8))
1947 entry
->cache_
= new cy::Functor(entry
->value_
, reinterpret_cast<void (*)()>(&dlerror
));
1949 for (CYHook
*hook
: GetHooks())
1950 if (hook
->SetupContext
!= NULL
)
1951 (*hook
->SetupContext
)(context
);
1953 CYArrayPush(context
, alls
, cycript
);
1956 static JSGlobalContextRef context_
;
1958 JSGlobalContextRef
CYGetJSContext() {
1959 CYInitializeDynamic();
1961 if (context_
== NULL
) {
1962 context_
= JSGlobalContextCreate(Global_
);
1963 CYSetupContext(context_
);
1969 void CYDestroyContext() {
1970 if (context_
== NULL
)
1972 JSGlobalContextRelease(context_
);