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 "cycript.hpp"
39 #include "sig/parse.hpp"
40 #include "sig/ffi_type.hpp"
45 #include "Execute.hpp"
46 #include "Internal.hpp"
47 #include "JavaScript.hpp"
48 #include "Pooling.hpp"
51 static std::vector
<CYHook
*> &GetHooks() {
52 static std::vector
<CYHook
*> hooks
;
56 CYRegisterHook::CYRegisterHook(CYHook
*hook
) {
57 GetHooks().push_back(hook
);
60 /* JavaScript Properties {{{ */
61 bool CYHasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
62 return JSObjectHasProperty(context
, object
, name
);
65 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) {
66 return _jsccall(JSObjectGetPropertyAtIndex
, context
, object
, index
);
69 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
70 return _jsccall(JSObjectGetProperty
, context
, object
, name
);
73 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) {
74 _jsccall(JSObjectSetPropertyAtIndex
, context
, object
, index
, value
);
77 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) {
78 _jsccall(JSObjectSetProperty
, context
, object
, name
, value
, attributes
);
81 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef (*callback
)(JSContextRef
, JSObjectRef
, JSObjectRef
, size_t, const JSValueRef
[], JSValueRef
*), JSPropertyAttributes attributes
) {
82 CYSetProperty(context
, object
, name
, JSObjectMakeFunctionWithCallback(context
, name
, callback
), attributes
);
85 void CYSetPrototype(JSContextRef context
, JSObjectRef object
, JSValueRef value
) {
86 JSObjectSetPrototype(context
, object
, value
);
87 _assert(CYIsStrictEqual(context
, JSObjectGetPrototype(context
, object
), value
));
90 /* JavaScript Strings {{{ */
91 JSStringRef
CYCopyJSString(const char *value
) {
92 return value
== NULL
? NULL
: JSStringCreateWithUTF8CString(value
);
95 JSStringRef
CYCopyJSString(JSStringRef value
) {
96 return value
== NULL
? NULL
: JSStringRetain(value
);
99 JSStringRef
CYCopyJSString(CYUTF8String value
) {
100 // XXX: this is very wrong; it needs to convert to UTF16 and then create from there
101 return CYCopyJSString(value
.data
);
104 JSStringRef
CYCopyJSString(JSContextRef context
, JSValueRef value
) {
105 if (JSValueIsNull(context
, value
))
107 return _jsccall(JSValueToStringCopy
, context
, value
);
110 static CYUTF16String
CYCastUTF16String(JSStringRef value
) {
111 return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
));
114 CYUTF8String
CYPoolUTF8String(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
115 return CYPoolUTF8String(pool
, CYCastUTF16String(value
));
118 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
119 CYUTF8String
utf8(CYPoolUTF8String(pool
, context
, value
));
120 _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
);
124 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
125 return JSValueIsNull(context
, value
) ? NULL
: CYPoolCString(pool
, context
, CYJSString(context
, value
));
128 /* Index Offsets {{{ */
129 size_t CYGetIndex(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
130 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
134 static JSClassRef All_
;
135 static JSClassRef Context_
;
137 static JSClassRef Global_
;
138 static JSClassRef Pointer_
;
139 static JSClassRef Struct_
;
144 JSStringRef length_s
;
145 JSStringRef message_s
;
148 JSStringRef prototype_s
;
150 JSStringRef splice_s
;
151 JSStringRef toCYON_s
;
152 JSStringRef toJSON_s
;
153 JSStringRef toPointer_s
;
154 JSStringRef toString_s
;
157 static JSStringRef Result_
;
159 void CYFinalize(JSObjectRef object
) {
160 CYData
*internal(reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
)));
161 _assert(internal
->count_
!= _not(unsigned));
162 if (--internal
->count_
== 0)
166 void Structor_(CYPool
&pool
, sig::Type
*&type
) {
168 type
->primitive
== sig::pointer_P
&&
169 type
->data
.data
.type
->primitive
== sig::struct_P
&&
170 type
->data
.data
.type
->name
!= NULL
&&
171 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
173 type
->primitive
= sig::typename_P
;
174 type
->data
.data
.type
= NULL
;
178 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
181 size_t length(strlen(type
->name
));
182 char keyed
[length
+ 2];
183 memcpy(keyed
+ 1, type
->name
, length
+ 1);
185 static const char *modes
= "34";
186 for (size_t i(0); i
!= 2; ++i
) {
190 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
193 sig::Parse(pool
, &type
->data
.signature
, entry
->value_
, &Structor_
);
197 sig::Signature signature
;
198 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
199 type
= signature
.elements
[0].type
;
205 JSClassRef
Type_privateData::Class_
;
210 JSGlobalContextRef context_
;
212 Context(JSGlobalContextRef context
) :
221 Type_privateData
*type_
;
224 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type
*type
) :
225 CYOwned(value
, context
, owner
),
226 type_(new(*pool_
) Type_privateData(type
)),
232 struct Struct_privateData
:
235 Type_privateData
*type_
;
237 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
238 CYOwned(NULL
, context
, owner
)
243 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
244 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
245 CYPool
&pool(*internal
->pool_
);
246 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
247 internal
->type_
= typical
;
250 internal
->value_
= data
;
252 size_t size(typical
->GetFFI()->size
);
253 void *copy(internal
->pool_
->malloc
<void>(size
));
254 memcpy(copy
, data
, size
);
255 internal
->value_
= copy
;
258 return JSObjectMake(context
, Struct_
, internal
);
261 static void *CYCastSymbol(const char *name
) {
262 for (CYHook
*hook
: GetHooks())
263 if (hook
->CastSymbol
!= NULL
)
264 if (void *value
= (*hook
->CastSymbol
)(name
))
266 return dlsym(RTLD_DEFAULT
, name
);
269 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
270 return JSValueMakeBoolean(context
, value
);
273 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
274 return JSValueMakeNumber(context
, value
);
277 #define CYCastJSValue_(Type_) \
278 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
279 return JSValueMakeNumber(context, static_cast<double>(value)); \
283 CYCastJSValue_(unsigned int)
284 CYCastJSValue_(long int)
285 CYCastJSValue_(long unsigned int)
286 CYCastJSValue_(long long int)
287 CYCastJSValue_(long long unsigned int)
289 JSValueRef
CYJSUndefined(JSContextRef context
) {
290 return JSValueMakeUndefined(context
);
293 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
294 return _jsccall(JSValueToNumber
, context
, value
);
297 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
298 return JSValueToBoolean(context
, value
);
301 JSValueRef
CYJSNull(JSContextRef context
) {
302 return JSValueMakeNull(context
);
305 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
306 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
309 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
310 return CYCastJSValue(context
, CYJSString(value
));
313 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
314 return _jsccall(JSValueToObject
, context
, value
);
317 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
318 return _jsccall(JSObjectCallAsFunction
, context
, function
, _this
, count
, arguments
);
321 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
322 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
325 bool CYIsEqual(JSContextRef context
, JSValueRef lhs
, JSValueRef rhs
) {
326 return _jsccall(JSValueIsEqual
, context
, lhs
, rhs
);
329 bool CYIsStrictEqual(JSContextRef context
, JSValueRef lhs
, JSValueRef rhs
) {
330 return JSValueIsStrictEqual(context
, lhs
, rhs
);
333 size_t CYArrayLength(JSContextRef context
, JSObjectRef array
) {
334 return CYCastDouble(context
, CYGetProperty(context
, array
, length_s
));
337 JSValueRef
CYArrayGet(JSContextRef context
, JSObjectRef array
, size_t index
) {
338 return _jsccall(JSObjectGetPropertyAtIndex
, context
, array
, index
);
341 void CYArrayPush(JSContextRef context
, JSObjectRef array
, JSValueRef value
) {
342 JSValueRef arguments
[1];
343 arguments
[0] = value
;
344 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
345 _jsccall(JSObjectCallAsFunction
, context
, CYCastJSObject(context
, CYGetProperty(context
, Array
, push_s
)), array
, 1, arguments
);
348 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
355 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, arguments
[0])));
356 fwrite(string
.data
, string
.size
, 1, file
);
360 return CYJSUndefined(context
);
363 static void (*JSSynchronousGarbageCollectForDebugging$
)(JSContextRef
);
365 _visible
void CYGarbageCollect(JSContextRef context
) {
366 (JSSynchronousGarbageCollectForDebugging$
?: &JSGarbageCollect
)(context
);
369 static JSValueRef
Cycript_compile_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
371 CYUTF8String
before(CYPoolUTF8String(pool
, context
, CYJSString(context
, arguments
[0])));
372 std::stringstream
value(std::string(before
.data
, before
.size
));
373 CYUTF8String
after(CYPoolCode(pool
, value
));
374 return CYCastJSValue(context
, CYJSString(after
));
375 } CYCatch_(NULL
, "SyntaxError") }
377 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
378 CYGarbageCollect(context
);
379 return CYJSUndefined(context
);
382 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> &objects
, JSValueRef
*exception
) { CYTry
{
383 switch (JSType type
= JSValueGetType(context
, value
)) {
384 case kJSTypeUndefined
:
389 return CYCastBool(context
, value
) ? "true" : "false";
391 case kJSTypeNumber
: {
392 std::ostringstream str
;
393 CYNumerify(str
, CYCastDouble(context
, value
));
394 std::string
value(str
.str());
395 return pool
.strmemdup(value
.c_str(), value
.size());
398 case kJSTypeString
: {
399 std::ostringstream str
;
400 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
401 CYStringify(str
, string
.data
, string
.size
);
402 std::string
value(str
.str());
403 return pool
.strmemdup(value
.c_str(), value
.size());
407 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
, objects
);
409 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
413 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> &objects
) {
414 return _jsccall(CYPoolCCYON
, pool
, context
, value
, objects
);
417 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> *objects
) {
419 return CYPoolCCYON(pool
, context
, value
, *objects
);
421 std::set
<void *> objects
;
422 return CYPoolCCYON(pool
, context
, value
, objects
);
426 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSObjectRef object
, std::set
<void *> &objects
) {
427 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
428 if (CYIsCallable(context
, toCYON
)) {
429 // XXX: this needs to be abstracted behind some kind of function
430 JSValueRef arguments
[1] = {CYCastJSValue(context
, reinterpret_cast<uintptr_t>(&objects
))};
431 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 1, arguments
));
432 _assert(value
!= NULL
);
433 return CYPoolCString(pool
, context
, value
);
436 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
437 if (CYIsCallable(context
, toJSON
)) {
438 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
439 return _jsccall(CYPoolCCYON
, pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), objects
);
442 if (JSObjectIsFunction(context
, object
)) {
443 JSValueRef
toString(CYGetProperty(context
, object
, toString_s
));
444 if (CYIsCallable(context
, toString
)) {
445 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
446 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toString
, object
, 1, arguments
));
447 _assert(value
!= NULL
);
448 return CYPoolCString(pool
, context
, value
);
452 _assert(objects
.insert(object
).second
);
454 std::ostringstream str
;
458 // XXX: this is, sadly, going to leak
459 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
463 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
469 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
470 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
475 CYStringify(str
, string
.data
, string
.size
);
480 JSValueRef
value(CYGetProperty(context
, object
, name
));
481 str
<< CYPoolCCYON(pool
, context
, value
, objects
);
482 } catch (const CYException
&error
) {
487 JSPropertyNameArrayRelease(names
);
491 std::string
string(str
.str());
492 return pool
.strmemdup(string
.c_str(), string
.size());
495 std::set
<void *> *CYCastObjects(JSContextRef context
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
498 return CYCastPointer
<std::set
<void *> *>(context
, arguments
[0]);
501 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
502 std::set
<void *> *objects(CYCastObjects(context
, _this
, count
, arguments
));
503 // XXX: this is horribly inefficient
504 std::set
<void *> backup
;
509 std::ostringstream str
;
513 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
516 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
523 JSValueRef
value(CYGetProperty(context
, _this
, index
));
524 if (!JSValueIsUndefined(context
, value
))
525 str
<< CYPoolCCYON(pool
, context
, value
, *objects
);
530 } catch (const CYException
&error
) {
537 std::string
value(str
.str());
538 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
541 static JSValueRef
Error_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
543 std::ostringstream str
;
545 str
<< "new " << CYPoolUTF8String(pool
, context
, CYJSString(context
, CYGetProperty(context
, _this
, name_s
))) << "(";
547 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, CYGetProperty(context
, _this
, message_s
))));
548 CYStringify(str
, string
.data
, string
.size
);
552 std::string
value(str
.str());
553 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
556 static JSValueRef
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
558 std::ostringstream str
;
560 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
)));
561 CYStringify(str
, string
.data
, string
.size
);
563 std::string
value(str
.str());
564 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
567 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
568 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
569 return JSObjectMake(context
, Pointer_
, internal
);
572 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const sig::Signature
&signature
) {
573 return JSObjectMake(context
, Functor_
, new cy::Functor(signature
, function
));
576 static JSObjectRef
CYMakeFunctor(JSContextRef context
, const char *symbol
, const char *encoding
, void **cache
) {
577 cy::Functor
*internal
;
579 internal
= reinterpret_cast<cy::Functor
*>(*cache
);
581 void (*function
)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol
)));
582 if (function
== NULL
)
585 internal
= new cy::Functor(encoding
, function
);
590 return JSObjectMake(context
, Functor_
, internal
);
593 static bool CYGetOffset(CYPool
&pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
594 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
597 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
600 else switch (JSValueGetType(context
, value
)) {
603 case kJSTypeObject
: {
604 JSObjectRef
object((JSObjectRef
) value
);
605 if (JSValueIsObjectOfClass(context
, value
, Pointer_
)) {
606 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
607 return internal
->value_
;
609 JSValueRef
toPointer(CYGetProperty(context
, object
, toPointer_s
));
610 if (CYIsCallable(context
, toPointer
)) {
611 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
));
612 _assert(value
!= NULL
);
613 return CYCastPointer_(context
, value
);
616 double number(CYCastDouble(context
, value
));
617 if (std::isnan(number
))
618 throw CYJSError(context
, "cannot convert value to pointer");
619 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
623 void CYPoolFFI(CYPool
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
624 switch (type
->primitive
) {
626 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
629 #define CYPoolFFI_(primitive, native) \
630 case sig::primitive ## _P: \
631 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
634 CYPoolFFI_(uchar
, unsigned char)
635 CYPoolFFI_(char, char)
636 CYPoolFFI_(ushort
, unsigned short)
637 CYPoolFFI_(short, short)
638 CYPoolFFI_(ulong
, unsigned long)
639 CYPoolFFI_(long, long)
640 CYPoolFFI_(uint
, unsigned int)
642 CYPoolFFI_(ulonglong
, unsigned long long)
643 CYPoolFFI_(longlong
, long long)
644 CYPoolFFI_(float, float)
645 CYPoolFFI_(double, double)
648 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
649 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
650 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
651 ffi_type
*field(ffi
->elements
[index
]);
654 if (aggregate
== NULL
)
657 rhs
= CYGetProperty(context
, aggregate
, index
);
658 if (JSValueIsUndefined(context
, rhs
))
659 throw CYJSError(context
, "unable to extract array value");
662 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
669 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
673 _assert(pool
!= NULL
);
674 *reinterpret_cast<const char **>(data
) = CYPoolCString(*pool
, context
, value
);
677 case sig::struct_P
: {
678 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
679 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
680 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
681 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
682 ffi_type
*field(ffi
->elements
[index
]);
685 if (aggregate
== NULL
)
688 rhs
= CYGetProperty(context
, aggregate
, index
);
689 if (JSValueIsUndefined(context
, rhs
)) {
690 if (element
->name
!= NULL
)
691 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
694 if (JSValueIsUndefined(context
, rhs
)) undefined
:
695 throw CYJSError(context
, "unable to extract structure value");
699 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
709 for (CYHook
*hook
: GetHooks())
710 if (hook
->PoolFFI
!= NULL
)
711 if ((*hook
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
714 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
718 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
719 switch (type
->primitive
) {
721 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
723 #define CYFromFFI_(primitive, native) \
724 case sig::primitive ## _P: \
725 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
727 CYFromFFI_(uchar, unsigned char)
728 CYFromFFI_(char, char)
729 CYFromFFI_(ushort
, unsigned short)
730 CYFromFFI_(short, short)
731 CYFromFFI_(ulong
, unsigned long)
732 CYFromFFI_(long, long)
733 CYFromFFI_(uint
, unsigned int)
735 CYFromFFI_(ulonglong
, unsigned long long)
736 CYFromFFI_(longlong
, long long)
737 CYFromFFI_(float, float)
738 CYFromFFI_(double, double)
741 if (void *pointer
= data
)
742 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
746 if (void *pointer
= *reinterpret_cast<void **>(data
))
747 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
751 if (char *utf8
= *reinterpret_cast<char **>(data
))
752 return CYCastJSValue(context
, utf8
);
756 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
758 return CYJSUndefined(context
);
761 return CYJSNull(context
);
763 for (CYHook
*hook
: GetHooks())
764 if (hook
->FromFFI
!= NULL
)
765 if (JSValueRef value
= (*hook
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
768 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
772 void CYExecuteClosure(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
773 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
775 JSContextRef
context(internal
->context_
);
777 size_t count(internal
->cif_
.nargs
);
778 JSValueRef values
[count
];
780 for (size_t index(0); index
!= count
; ++index
)
781 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
783 JSValueRef
value(internal
->adapter_(context
, count
, values
, internal
->function_
));
784 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
787 static JSValueRef
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
) {
788 return CYCallAsFunction(context
, function
, NULL
, count
, values
);
791 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
, JSValueRef (*adapter
)(JSContextRef
, size_t, JSValueRef
[], JSObjectRef
)) {
792 // XXX: in case of exceptions this will leak
793 // XXX: in point of fact, this may /need/ to leak :(
794 Closure_privateData
*internal(new Closure_privateData(context
, function
, adapter
, signature
));
796 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
798 ffi_closure
*writable(reinterpret_cast<ffi_closure
*>(ffi_closure_alloc(sizeof(ffi_closure
), &executable
)));
800 ffi_status
status(ffi_prep_closure_loc(writable
, &internal
->cif_
, &CYExecuteClosure
, internal
, executable
));
801 _assert(status
== FFI_OK
);
803 internal
->value_
= executable
;
805 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
806 NULL
, sizeof(ffi_closure
),
807 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
811 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, &CYExecuteClosure
, internal
));
812 _assert(status
== FFI_OK
);
814 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
816 internal
->value_
= closure
;
822 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
) {
823 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, signature
, &FunctionAdapter_
));
824 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
825 // XXX: see above notes about needing to leak
826 JSValueProtect(CYGetJSContext(context
), object
);
830 JSValueRef
CYGetCachedValue(JSContextRef context
, JSStringRef name
) {
831 return CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
);
834 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
835 return CYCastJSObject(context
, CYGetCachedValue(context
, name
));
838 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const sig::Signature
&signature
) {
839 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
841 bool function(_jsccall(JSValueIsInstanceOfConstructor
, context
, value
, Function
));
843 JSObjectRef
function(CYCastJSObject(context
, value
));
844 return CYMakeFunctor(context
, function
, signature
);
846 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
847 return CYMakeFunctor(context
, function
, signature
);
851 static bool Index_(CYPool
&pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
852 Type_privateData
*typical(internal
->type_
);
853 sig::Type
*type(typical
->type_
);
857 const char *name(CYPoolCString(pool
, context
, property
));
858 size_t length(strlen(name
));
859 double number(CYCastDouble(name
, length
));
861 size_t count(type
->data
.signature
.count
);
863 if (std::isnan(number
)) {
864 if (property
== NULL
)
867 sig::Element
*elements(type
->data
.signature
.elements
);
869 for (size_t local(0); local
!= count
; ++local
) {
870 sig::Element
*element(&elements
[local
]);
871 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
879 index
= static_cast<ssize_t
>(number
);
880 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
885 ffi_type
**elements(typical
->GetFFI()->elements
);
887 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
888 for (ssize_t
local(0); local
!= index
; ++local
)
889 base
+= elements
[local
]->size
;
894 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
896 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
898 if (JSStringIsEqual(property
, length_s
))
899 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
901 Type_privateData
*typical(internal
->type_
);
902 if (typical
->type_
== NULL
)
904 sig::Type
&type(*typical
->type_
);
907 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
909 else if (!CYGetOffset(pool
, context
, property
, offset
))
912 if (type
.primitive
== sig::function_P
)
913 return CYMakeFunctor(context
, reinterpret_cast<void (*)()>(internal
->value_
), type
.data
.signature
);
915 ffi_type
*ffi(typical
->GetFFI());
917 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
918 base
+= ffi
->size
* offset
;
920 JSObjectRef
owner(internal
->GetOwner() ?: object
);
921 return CYFromFFI(context
, &type
, ffi
, base
, false, owner
);
924 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
926 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
927 Type_privateData
*typical(internal
->type_
);
929 if (typical
->type_
== NULL
)
933 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
935 else if (!CYGetOffset(pool
, context
, property
, offset
))
938 ffi_type
*ffi(typical
->GetFFI());
940 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
941 base
+= ffi
->size
* offset
;
943 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
947 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
948 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
949 Type_privateData
*typical(internal
->type_
);
950 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
953 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
955 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
956 Type_privateData
*typical(internal
->type_
);
961 if (!Index_(pool
, context
, internal
, property
, index
, base
))
964 JSObjectRef
owner(internal
->GetOwner() ?: object
);
966 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
969 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
971 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
972 Type_privateData
*typical(internal
->type_
);
977 if (!Index_(pool
, context
, internal
, property
, index
, base
))
980 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
984 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
985 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
986 Type_privateData
*typical(internal
->type_
);
987 sig::Type
*type(typical
->type_
);
992 size_t count(type
->data
.signature
.count
);
993 sig::Element
*elements(type
->data
.signature
.elements
);
997 for (size_t index(0); index
!= count
; ++index
) {
999 name
= elements
[index
].name
;
1002 sprintf(number
, "%zu", index
);
1006 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
1010 void CYCallFunction(CYPool
&pool
, JSContextRef context
, ffi_cif
*cif
, void (*function
)(), void *value
, void **values
) {
1011 ffi_call(cif
, function
, value
, values
);
1014 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
)()) {
1015 if (setups
+ count
!= signature
->count
- 1)
1016 throw CYJSError(context
, "incorrect number of arguments to ffi function");
1018 size_t size(setups
+ count
);
1020 memcpy(values
, setup
, sizeof(void *) * setups
);
1022 for (size_t index(setups
); index
!= size
; ++index
) {
1023 sig::Element
*element(&signature
->elements
[index
+ 1]);
1024 ffi_type
*ffi(cif
->arg_types
[index
]);
1026 values
[index
] = new(pool
) uint8_t[ffi
->size
];
1027 CYPoolFFI(&pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
1030 uint8_t value
[cif
->rtype
->size
];
1032 void (*call
)(CYPool
&, JSContextRef
, ffi_cif
*, void (*)(), void *, void **) = &CYCallFunction
;
1033 // XXX: this only supports one hook, but it is a bad idea anyway
1034 for (CYHook
*hook
: GetHooks())
1035 if (hook
->CallFunction
!= NULL
)
1036 call
= hook
->CallFunction
;
1038 call(pool
, context
, cif
, function
, value
, values
);
1039 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
1042 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1044 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1045 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
1048 JSObjectRef
CYMakeType(JSContextRef context
, const char *encoding
) {
1049 Type_privateData
*internal(new Type_privateData(encoding
));
1050 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1053 JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
1054 Type_privateData
*internal(new Type_privateData(type
));
1055 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1058 JSObjectRef
CYMakeType(JSContextRef context
, sig::Signature
*signature
) {
1065 type
.primitive
= sig::function_P
;
1066 sig::Copy(pool
, type
.data
.signature
, *signature
);
1068 return CYMakeType(context
, &type
);
1071 static bool All_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
1072 JSObjectRef
global(CYGetGlobalObject(context
));
1073 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1074 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1076 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1077 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1078 if (CYHasProperty(context
, space
, property
))
1082 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
1084 size_t length(name
.size
);
1085 char keyed
[length
+ 2];
1086 memcpy(keyed
+ 1, name
.data
, length
+ 1);
1088 static const char *modes
= "0124";
1089 for (size_t i(0); i
!= 4; ++i
) {
1090 keyed
[0] = modes
[i
];
1091 if (CYBridgeHash(keyed
, length
+ 1) != NULL
)
1098 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1099 JSObjectRef
global(CYGetGlobalObject(context
));
1100 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1101 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1103 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1104 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1105 if (JSValueRef value
= CYGetProperty(context
, space
, property
))
1106 if (!JSValueIsUndefined(context
, value
))
1110 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
1112 size_t length(name
.size
);
1113 char keyed
[length
+ 2];
1114 memcpy(keyed
+ 1, name
.data
, length
+ 1);
1116 static const char *modes
= "0124";
1117 for (size_t i(0); i
!= 4; ++i
) {
1118 char mode(modes
[i
]);
1121 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
1124 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
);
1127 return CYMakeFunctor(context
, name
.data
, entry
->value_
, &entry
->cache_
);
1130 if (void *symbol
= CYCastSymbol(name
.data
)) {
1131 // XXX: this is horrendously inefficient
1132 sig::Signature signature
;
1133 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
1135 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1136 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
1139 // XXX: implement case 3
1141 return CYMakeType(context
, entry
->value_
);
1148 static void All_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1149 JSObjectRef
global(CYGetGlobalObject(context
));
1150 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1151 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1153 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1154 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1))) {
1155 JSPropertyNameArrayRef
subset(JSObjectCopyPropertyNames(context
, space
));
1156 for (size_t index(0), count(JSPropertyNameArrayGetCount(subset
)); index
!= count
; ++index
)
1157 JSPropertyNameAccumulatorAddName(names
, JSPropertyNameArrayGetNameAtIndex(subset
, index
));
1158 JSPropertyNameArrayRelease(subset
);
1162 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1164 throw CYJSError(context
, "incorrect number of arguments to Pointer constructor");
1168 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1169 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1171 sig::Signature signature
;
1172 sig::Parse(pool
, &signature
, type
, &Structor_
);
1174 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1177 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1179 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1181 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1182 return CYMakeType(context
, type
);
1185 static JSValueRef Type_callAsFunction_$
With(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], sig::Primitive primitive
, JSValueRef
*exception
) { CYTry
{
1186 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1194 type
.primitive
= primitive
;
1195 type
.data
.signature
.elements
= new(pool
) sig::Element
[1 + count
];
1196 type
.data
.signature
.count
= 1 + count
;
1198 type
.data
.signature
.elements
[0].name
= NULL
;
1199 type
.data
.signature
.elements
[0].type
= internal
->type_
;
1200 type
.data
.signature
.elements
[0].offset
= _not(size_t);
1202 for (size_t i(0); i
!= count
; ++i
) {
1203 sig::Element
&element(type
.data
.signature
.elements
[i
+ 1]);
1204 element
.name
= NULL
;
1205 element
.offset
= _not(size_t);
1207 JSObjectRef
object(CYCastJSObject(context
, arguments
[i
]));
1208 _assert(JSValueIsObjectOfClass(context
, object
, Type_privateData::Class_
));
1209 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1211 element
.type
= internal
->type_
;
1214 return CYMakeType(context
, &type
);
1217 static JSValueRef
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1219 throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf");
1220 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1223 size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0])));
1224 if (index
== _not(size_t))
1225 throw CYJSError(context
, "invalid array size used with Type.arrayOf");
1231 type
.primitive
= sig::array_P
;
1232 type
.data
.data
.type
= internal
->type_
;
1233 type
.data
.data
.size
= index
;
1235 return CYMakeType(context
, &type
);
1238 static JSValueRef
Type_callAsFunction_blockWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1239 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::block_P
, exception
);
1242 static JSValueRef
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1244 throw CYJSError(context
, "incorrect number of arguments to Type.constant");
1245 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1247 sig::Type
type(*internal
->type_
);
1248 type
.flags
|= JOC_TYPE_CONST
;
1249 return CYMakeType(context
, &type
);
1252 static JSValueRef
Type_callAsFunction_long(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1254 throw CYJSError(context
, "incorrect number of arguments to Type.long");
1255 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1257 sig::Type
type(*internal
->type_
);
1259 switch (type
.primitive
) {
1260 case sig::short_P
: type
.primitive
= sig::int_P
; break;
1261 case sig::int_P
: type
.primitive
= sig::long_P
; break;
1262 case sig::long_P
: type
.primitive
= sig::longlong_P
; break;
1263 default: throw CYJSError(context
, "invalid type argument to Type.long");
1266 return CYMakeType(context
, &type
);
1269 static JSValueRef
Type_callAsFunction_short(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1271 throw CYJSError(context
, "incorrect number of arguments to Type.short");
1272 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1274 sig::Type
type(*internal
->type_
);
1276 switch (type
.primitive
) {
1277 case sig::int_P
: type
.primitive
= sig::short_P
; break;
1278 case sig::long_P
: type
.primitive
= sig::int_P
; break;
1279 case sig::longlong_P
: type
.primitive
= sig::long_P
; break;
1280 default: throw CYJSError(context
, "invalid type argument to Type.short");
1283 return CYMakeType(context
, &type
);
1286 static JSValueRef
Type_callAsFunction_signed(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1288 throw CYJSError(context
, "incorrect number of arguments to Type.signed");
1289 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1291 sig::Type
type(*internal
->type_
);
1293 switch (type
.primitive
) {
1294 case sig::char_P
: case sig::uchar_P
: type
.primitive
= sig::char_P
; break;
1295 case sig::short_P
: case sig::ushort_P
: type
.primitive
= sig::short_P
; break;
1296 case sig::int_P
: case sig::uint_P
: type
.primitive
= sig::int_P
; break;
1297 case sig::long_P
: case sig::ulong_P
: type
.primitive
= sig::long_P
; break;
1298 case sig::longlong_P
: case sig::ulonglong_P
: type
.primitive
= sig::longlong_P
; break;
1299 default: throw CYJSError(context
, "invalid type argument to Type.signed");
1302 return CYMakeType(context
, &type
);
1305 static JSValueRef
Type_callAsFunction_unsigned(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1307 throw CYJSError(context
, "incorrect number of arguments to Type.unsigned");
1308 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1310 sig::Type
type(*internal
->type_
);
1312 switch (type
.primitive
) {
1313 case sig::char_P
: case sig::uchar_P
: type
.primitive
= sig::uchar_P
; break;
1314 case sig::short_P
: case sig::ushort_P
: type
.primitive
= sig::ushort_P
; break;
1315 case sig::int_P
: case sig::uint_P
: type
.primitive
= sig::uint_P
; break;
1316 case sig::long_P
: case sig::ulong_P
: type
.primitive
= sig::ulong_P
; break;
1317 case sig::longlong_P
: case sig::ulonglong_P
: type
.primitive
= sig::ulonglong_P
; break;
1318 default: throw CYJSError(context
, "invalid type argument to Type.unsigned");
1321 return CYMakeType(context
, &type
);
1324 static JSValueRef
Type_callAsFunction_functionWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1325 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::function_P
, exception
);
1328 static JSValueRef
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1330 throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo");
1331 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1336 if (internal
->type_
->primitive
== sig::char_P
) {
1337 type
.flags
= internal
->type_
->flags
;
1338 type
.primitive
= sig::string_P
;
1339 type
.data
.data
.type
= NULL
;
1340 type
.data
.data
.size
= 0;
1343 type
.primitive
= sig::pointer_P
;
1344 type
.data
.data
.type
= internal
->type_
;
1345 type
.data
.data
.size
= 0;
1348 return CYMakeType(context
, &type
);
1351 static JSValueRef
Type_callAsFunction_withName(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.withName");
1354 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1357 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1359 sig::Type
type(*internal
->type_
);
1361 return CYMakeType(context
, &type
);
1364 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1366 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1367 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1369 if (internal
->type_
->primitive
== sig::function_P
)
1370 return CYMakeFunctor(context
, arguments
[0], internal
->type_
->data
.signature
);
1372 sig::Type
*type(internal
->type_
);
1373 ffi_type
*ffi(internal
->GetFFI());
1375 uint8_t value
[ffi
->size
];
1377 CYPoolFFI(&pool
, context
, type
, ffi
, value
, arguments
[0]);
1378 return CYFromFFI(context
, type
, ffi
, value
);
1381 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1383 throw CYJSError(context
, "incorrect number of arguments to Type allocator");
1384 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1386 sig::Type
*type(internal
->type_
);
1389 if (type
->primitive
!= sig::array_P
)
1390 length
= _not(size_t);
1392 length
= type
->data
.data
.size
;
1393 type
= type
->data
.data
.type
;
1396 void *value(calloc(1, internal
->GetFFI()->size
));
1397 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1400 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1402 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1404 const char *encoding(CYPoolCString(pool
, context
, arguments
[1]));
1405 sig::Signature signature
;
1406 sig::Parse(pool
, &signature
, encoding
, &Structor_
);
1407 return CYMakeFunctor(context
, arguments
[0], signature
);
1410 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1411 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1412 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1415 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1416 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1419 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1420 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1422 sprintf(string
, "%p", internal
->value_
);
1423 return CYCastJSValue(context
, string
);
1426 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1427 std::set
<void *> *objects(CYCastObjects(context
, _this
, count
, arguments
));
1429 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1430 if (internal
->length_
!= _not(size_t)) {
1431 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
1432 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1433 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1434 } else if (internal
->type_
->type_
== NULL
) pointer
: {
1436 sprintf(string
, "%p", internal
->value_
);
1437 return CYCastJSValue(context
, string
);
1439 JSValueRef
value(CYGetProperty(context
, _this
, cyi_s
));
1440 if (JSValueIsUndefined(context
, value
))
1443 return CYCastJSValue(context
, pool
.strcat("&", CYPoolCCYON(pool
, context
, value
, objects
), NULL
));
1444 } catch (const CYException
&e
) {
1449 static JSValueRef
Pointer_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1450 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1451 return CYMakeType(context
, internal
->type_
->type_
);
1454 static JSValueRef
Functor_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1455 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1456 return CYMakeType(context
, &internal
->signature_
);
1459 static JSValueRef
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1460 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1461 return CYCastJSValue(context
, internal
->GetFFI()->alignment
);
1464 static JSValueRef
Type_getProperty_name(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1465 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1466 return CYCastJSValue(context
, internal
->type_
->name
);
1469 static JSValueRef
Type_getProperty_size(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1470 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1471 return CYCastJSValue(context
, internal
->GetFFI()->size
);
1474 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1475 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1477 const char *type(sig::Unparse(pool
, internal
->type_
));
1478 return CYCastJSValue(context
, CYJSString(type
));
1481 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1482 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1486 CYOutput
output(out
, options
);
1487 (new(pool
) CYEncodedType(Decode(pool
, internal
->type_
)))->Output(output
, CYNoFlags
);
1488 return CYCastJSValue(context
, CYJSString(out
.str().c_str()));
1491 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1492 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1495 static JSStaticFunction Pointer_staticFunctions
[4] = {
1496 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1497 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1498 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1502 static JSStaticValue Pointer_staticValues
[2] = {
1503 {"type", &Pointer_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1504 {NULL
, NULL
, NULL
, 0}
1507 static JSStaticFunction Struct_staticFunctions
[2] = {
1508 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1512 static JSStaticFunction Functor_staticFunctions
[4] = {
1513 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1514 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1515 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1520 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1523 static JSStaticValue Functor_staticValues
[2] = {
1524 {"type", &Functor_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1525 {NULL
, NULL
, NULL
, 0}
1529 JSStaticValue
const * const Functor::StaticValues
= Functor_staticValues
;
1532 static JSStaticValue Type_staticValues
[4] = {
1533 {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1534 {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1535 {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1536 {NULL
, NULL
, NULL
, 0}
1539 static JSStaticFunction Type_staticFunctions
[14] = {
1540 {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1541 {"blockWith", &Type_callAsFunction_blockWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1542 {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1543 {"functionWith", &Type_callAsFunction_functionWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1544 {"long", &Type_callAsFunction_long
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1545 {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1546 {"short", &Type_callAsFunction_short
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1547 {"signed", &Type_callAsFunction_signed
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1548 {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1549 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1550 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1551 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1552 {"unsigned", &Type_callAsFunction_unsigned
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1556 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1558 _visible
void CYSetArgs(int argc
, const char *argv
[]) {
1559 JSContextRef
context(CYGetJSContext());
1560 JSValueRef args
[argc
];
1561 for (int i(0); i
!= argc
; ++i
)
1562 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1565 if (JSObjectMakeArray$
!= NULL
)
1566 array
= _jsccall(*JSObjectMakeArray$
, context
, argc
, args
);
1568 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1569 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1570 array
= CYCastJSObject(context
, value
);
1573 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1574 CYSetProperty(context
, System
, CYJSString("args"), array
);
1577 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1578 return JSContextGetGlobalObject(context
);
1581 // XXX: this is neither exceptin safe nor even terribly sane
1582 class ExecutionHandle
{
1584 JSContextRef context_
;
1585 std::vector
<void *> handles_
;
1588 ExecutionHandle(JSContextRef context
) :
1591 handles_
.resize(GetHooks().size());
1592 for (size_t i(0); i
!= GetHooks().size(); ++i
) {
1593 CYHook
*hook(GetHooks()[i
]);
1594 if (hook
->ExecuteStart
!= NULL
)
1595 handles_
[i
] = (*hook
->ExecuteStart
)(context_
);
1601 ~ExecutionHandle() {
1602 for (size_t i(GetHooks().size()); i
!= 0; --i
) {
1603 CYHook
*hook(GetHooks()[i
-1]);
1604 if (hook
->ExecuteEnd
!= NULL
)
1605 (*hook
->ExecuteEnd
)(context_
, handles_
[i
-1]);
1610 static volatile bool cancel_
;
1612 static bool CYShouldTerminate(JSContextRef context
, void *arg
) {
1616 _visible
const char *CYExecute(JSContextRef context
, CYPool
&pool
, CYUTF8String code
) {
1617 ExecutionHandle
handle(context
);
1620 if (&JSContextGroupSetExecutionTimeLimit
!= NULL
)
1621 JSContextGroupSetExecutionTimeLimit(JSContextGetGroup(context
), 0.5, &CYShouldTerminate
, NULL
);
1624 JSValueRef
result(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0));
1625 if (JSValueIsUndefined(context
, result
))
1628 std::set
<void *> objects
;
1629 const char *json(_jsccall(CYPoolCCYON
, pool
, context
, result
, objects
));
1630 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1633 } catch (const CYException
&error
) {
1634 return pool
.strcat("throw ", error
.PoolCString(pool
), NULL
);
1638 _visible
void CYCancel() {
1642 static bool initialized_
= false;
1644 void CYInitializeDynamic() {
1646 initialized_
= true;
1649 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1650 JSSynchronousGarbageCollectForDebugging$
= reinterpret_cast<void (*)(JSContextRef
)>(dlsym(RTLD_DEFAULT
, "JSSynchronousGarbageCollectForDebugging"));
1652 JSClassDefinition definition
;
1654 definition
= kJSClassDefinitionEmpty
;
1655 definition
.className
= "All";
1656 definition
.hasProperty
= &All_hasProperty
;
1657 definition
.getProperty
= &All_getProperty
;
1658 definition
.getPropertyNames
= &All_getPropertyNames
;
1659 All_
= JSClassCreate(&definition
);
1661 definition
= kJSClassDefinitionEmpty
;
1662 definition
.className
= "Context";
1663 definition
.finalize
= &CYFinalize
;
1664 Context_
= JSClassCreate(&definition
);
1666 definition
= kJSClassDefinitionEmpty
;
1667 definition
.className
= "Functor";
1668 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1669 definition
.staticValues
= Functor_staticValues
;
1670 definition
.callAsFunction
= &Functor_callAsFunction
;
1671 definition
.finalize
= &CYFinalize
;
1672 Functor_
= JSClassCreate(&definition
);
1674 definition
= kJSClassDefinitionEmpty
;
1675 definition
.className
= "Pointer";
1676 definition
.staticFunctions
= Pointer_staticFunctions
;
1677 definition
.staticValues
= Pointer_staticValues
;
1678 definition
.getProperty
= &Pointer_getProperty
;
1679 definition
.setProperty
= &Pointer_setProperty
;
1680 definition
.finalize
= &CYFinalize
;
1681 Pointer_
= JSClassCreate(&definition
);
1683 definition
= kJSClassDefinitionEmpty
;
1684 definition
.className
= "Struct";
1685 definition
.staticFunctions
= Struct_staticFunctions
;
1686 definition
.getProperty
= &Struct_getProperty
;
1687 definition
.setProperty
= &Struct_setProperty
;
1688 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1689 definition
.finalize
= &CYFinalize
;
1690 Struct_
= JSClassCreate(&definition
);
1692 definition
= kJSClassDefinitionEmpty
;
1693 definition
.className
= "Type";
1694 definition
.staticValues
= Type_staticValues
;
1695 definition
.staticFunctions
= Type_staticFunctions
;
1696 definition
.callAsFunction
= &Type_callAsFunction
;
1697 definition
.callAsConstructor
= &Type_callAsConstructor
;
1698 definition
.finalize
= &CYFinalize
;
1699 Type_privateData::Class_
= JSClassCreate(&definition
);
1701 definition
= kJSClassDefinitionEmpty
;
1702 definition
.className
= "Global";
1703 //definition.getProperty = &Global_getProperty;
1704 Global_
= JSClassCreate(&definition
);
1706 Array_s
= JSStringCreateWithUTF8CString("Array");
1707 cy_s
= JSStringCreateWithUTF8CString("$cy");
1708 cyi_s
= JSStringCreateWithUTF8CString("$cyi");
1709 length_s
= JSStringCreateWithUTF8CString("length");
1710 message_s
= JSStringCreateWithUTF8CString("message");
1711 name_s
= JSStringCreateWithUTF8CString("name");
1712 pop_s
= JSStringCreateWithUTF8CString("pop");
1713 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1714 push_s
= JSStringCreateWithUTF8CString("push");
1715 splice_s
= JSStringCreateWithUTF8CString("splice");
1716 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1717 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1718 toPointer_s
= JSStringCreateWithUTF8CString("toPointer");
1719 toString_s
= JSStringCreateWithUTF8CString("toString");
1720 weak_s
= JSStringCreateWithUTF8CString("weak");
1722 Result_
= JSStringCreateWithUTF8CString("_");
1724 for (CYHook
*hook
: GetHooks())
1725 if (hook
->Initialize
!= NULL
)
1726 (*hook
->Initialize
)();
1729 void CYThrow(JSContextRef context
, JSValueRef value
) {
1731 throw CYJSError(context
, value
);
1734 const char *CYJSError::PoolCString(CYPool
&pool
) const {
1735 std::set
<void *> objects
;
1736 // XXX: this used to be CYPoolCString
1737 return CYPoolCCYON(pool
, context_
, value_
, objects
);
1740 JSValueRef
CYJSError::CastJSValue(JSContextRef context
, const char *name
) const {
1741 // XXX: what if the context is different? or the name? I dunno. ("epic" :/)
1745 JSValueRef
CYCastJSError(JSContextRef context
, const char *name
, const char *message
) {
1746 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString(name
)));
1747 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1748 return _jsccall(JSObjectCallAsConstructor
, context
, Error
, 1, arguments
);
1751 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
, const char *name
) const {
1752 return CYCastJSError(context
, name
, message_
);
1755 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1756 _assert(context
!= NULL
);
1761 va_start(args
, format
);
1762 // XXX: there might be a beter way to think about this
1763 const char *message(pool
.vsprintf(64, format
, args
));
1766 value_
= CYCastJSError(context
, "Error", message
);
1769 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1770 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1773 void *CYMapFile(const char *path
, size_t *psize
) {
1774 int fd(_syscall_(open(path
, O_RDONLY
), 1, ENOENT
));
1779 _syscall(fstat(fd
, &stat
));
1780 size_t size(stat
.st_size
);
1785 _syscall(base
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, 0));
1787 _syscall(close(fd
));
1791 static JSValueRef
require(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1792 _assert(count
== 1);
1796 _assert(dladdr(reinterpret_cast<void *>(&require
), &addr
) != 0);
1797 char *lib(pool
.strdup(addr
.dli_fname
));
1799 char *slash(strrchr(lib
, '/'));
1800 _assert(slash
!= NULL
);
1803 CYJSString
property("exports");
1806 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1807 const char *path(pool
.strcat(lib
, "/cycript0.9/", name
, ".cy", NULL
));
1809 CYJSString
key(path
);
1810 JSObjectRef
modules(CYGetCachedObject(context
, CYJSString("modules")));
1811 JSValueRef
cache(CYGetProperty(context
, modules
, key
));
1813 if (!JSValueIsUndefined(context
, cache
))
1814 module = CYCastJSObject(context
, cache
);
1817 code
.data
= reinterpret_cast<char *>(CYMapFile(path
, &code
.size
));
1819 if (code
.data
== NULL
) {
1820 if (strchr(name
, '/') == NULL
&& (
1822 dlopen(pool
.strcat("/System/Library/Frameworks/", name
, ".framework/", name
, NULL
), RTLD_LAZY
| RTLD_GLOBAL
) != NULL
||
1823 dlopen(pool
.strcat("/System/Library/PrivateFrameworks/", name
, ".framework/", name
, NULL
), RTLD_LAZY
| RTLD_GLOBAL
) != NULL
||
1826 return CYJSUndefined(NULL
);
1828 CYThrow("Can't find module: %s", name
);
1831 module = JSObjectMake(context
, NULL
, NULL
);
1832 CYSetProperty(context
, modules
, key
, module);
1834 JSObjectRef
exports(JSObjectMake(context
, NULL
, NULL
));
1835 CYSetProperty(context
, module, property
, exports
);
1837 std::stringstream wrap
;
1838 wrap
<< "(function (exports, require, module) { " << code
<< "\n});";
1839 code
= CYPoolCode(pool
, wrap
);
1841 JSValueRef
value(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0));
1842 JSObjectRef
function(CYCastJSObject(context
, value
));
1844 JSValueRef arguments
[3] = { exports
, JSObjectMakeFunctionWithCallback(context
, CYJSString("require"), &require
), module };
1845 CYCallAsFunction(context
, function
, NULL
, 3, arguments
);
1848 return CYGetProperty(context
, module, property
);
1851 extern "C" void CYDestroyWeak(JSWeakObjectMapRef weak
, void *data
) {
1854 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
1855 CYInitializeDynamic();
1857 JSObjectRef
global(CYGetGlobalObject(context
));
1859 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
1860 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
1862 /* Cache Globals {{{ */
1863 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
1864 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
1866 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
1867 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
1869 JSObjectRef
Boolean(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Boolean"))));
1870 CYSetProperty(context
, cy
, CYJSString("Boolean"), Boolean
);
1872 JSObjectRef
Boolean_prototype(CYCastJSObject(context
, CYGetProperty(context
, Boolean
, prototype_s
)));
1873 CYSetProperty(context
, cy
, CYJSString("Boolean_prototype"), Boolean_prototype
);
1875 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
1876 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
1878 JSObjectRef
Error_prototype(CYCastJSObject(context
, CYGetProperty(context
, Error
, prototype_s
)));
1879 CYSetProperty(context
, cy
, CYJSString("Error_prototype"), Error_prototype
);
1881 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
1882 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
1884 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
1885 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
1887 JSObjectRef
Number(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Number"))));
1888 CYSetProperty(context
, cy
, CYJSString("Number"), Number
);
1890 JSObjectRef
Number_prototype(CYCastJSObject(context
, CYGetProperty(context
, Number
, prototype_s
)));
1891 CYSetProperty(context
, cy
, CYJSString("Number_prototype"), Number_prototype
);
1893 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1894 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
1896 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
1897 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
1899 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
1900 CYSetProperty(context
, cy
, CYJSString("String"), String
);
1902 JSObjectRef
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
)));
1903 CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
);
1905 JSObjectRef
SyntaxError(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("SyntaxError"))));
1906 CYSetProperty(context
, cy
, CYJSString("SyntaxError"), SyntaxError
);
1909 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1910 CYSetProperty(context
, Error_prototype
, toCYON_s
, &Error_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1911 CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1913 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1914 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1915 CYSetProperty(context
, cycript
, CYJSString("compile"), &Cycript_compile_callAsFunction
);
1916 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
1918 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1919 CYSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
1920 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
1922 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1923 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1925 JSObjectRef
modules(JSObjectMake(context
, NULL
, NULL
));
1926 CYSetProperty(context
, cy
, CYJSString("modules"), modules
);
1928 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
1929 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
1931 JSObjectRef
alls(_jsccall(JSObjectCallAsConstructor
, context
, Array
, 0, NULL
));
1932 CYSetProperty(context
, cycript
, CYJSString("alls"), alls
);
1935 JSObjectRef
last(NULL
), curr(global
);
1937 goto next
; for (JSValueRef next
;;) {
1938 if (JSValueIsNull(context
, next
))
1941 curr
= CYCastJSObject(context
, next
);
1943 next
= JSObjectGetPrototype(context
, curr
);
1946 CYSetPrototype(context
, last
, all
);
1949 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
1950 CYSetProperty(context
, cy
, CYJSString("System"), System
);
1952 CYSetProperty(context
, all
, CYJSString("require"), &require
, kJSPropertyAttributeDontEnum
);
1954 CYSetProperty(context
, global
, CYJSString("system"), System
);
1955 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
1956 //CYSetProperty(context, System, CYJSString("global"), global);
1957 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
1960 if (&JSWeakObjectMapCreate
!= NULL
) {
1961 JSWeakObjectMapRef
weak(JSWeakObjectMapCreate(context
, NULL
, &CYDestroyWeak
));
1962 CYSetProperty(context
, cy
, weak_s
, CYCastJSValue(context
, reinterpret_cast<uintptr_t>(weak
)));
1966 if (CYBridgeEntry
*entry
= CYBridgeHash("1dlerror", 8))
1967 entry
->cache_
= new cy::Functor(entry
->value_
, reinterpret_cast<void (*)()>(&dlerror
));
1969 for (CYHook
*hook
: GetHooks())
1970 if (hook
->SetupContext
!= NULL
)
1971 (*hook
->SetupContext
)(context
);
1973 CYArrayPush(context
, alls
, cycript
);
1976 static JSGlobalContextRef context_
;
1978 _visible JSGlobalContextRef
CYGetJSContext() {
1979 CYInitializeDynamic();
1981 if (context_
== NULL
) {
1982 context_
= JSGlobalContextCreate(Global_
);
1983 CYSetupContext(context_
);
1989 _visible
void CYDestroyContext() {
1990 if (context_
== NULL
)
1992 JSGlobalContextRelease(context_
);