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"
41 #include "sig/parse.hpp"
42 #include "sig/ffi_type.hpp"
47 #include "Execute.hpp"
48 #include "Internal.hpp"
49 #include "JavaScript.hpp"
50 #include "Pooling.hpp"
53 const char *sqlite3_column_string(sqlite3_stmt
*stmt
, int n
) {
54 return reinterpret_cast<const char *>(sqlite3_column_text(stmt
, n
));
57 char *sqlite3_column_pooled(CYPool
&pool
, sqlite3_stmt
*stmt
, int n
) {
58 if (const char *value
= sqlite3_column_string(stmt
, n
))
59 return pool
.strdup(value
);
63 static std::vector
<CYHook
*> &GetHooks() {
64 static std::vector
<CYHook
*> hooks
;
68 CYRegisterHook::CYRegisterHook(CYHook
*hook
) {
69 GetHooks().push_back(hook
);
72 /* JavaScript Properties {{{ */
73 bool CYHasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
74 return JSObjectHasProperty(context
, object
, name
);
77 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) {
78 return _jsccall(JSObjectGetPropertyAtIndex
, context
, object
, index
);
81 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
82 return _jsccall(JSObjectGetProperty
, context
, object
, name
);
85 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) {
86 _jsccall(JSObjectSetPropertyAtIndex
, context
, object
, index
, value
);
89 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) {
90 _jsccall(JSObjectSetProperty
, context
, object
, name
, value
, attributes
);
93 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef (*callback
)(JSContextRef
, JSObjectRef
, JSObjectRef
, size_t, const JSValueRef
[], JSValueRef
*), JSPropertyAttributes attributes
) {
94 CYSetProperty(context
, object
, name
, JSObjectMakeFunctionWithCallback(context
, name
, callback
), attributes
);
97 void CYSetPrototype(JSContextRef context
, JSObjectRef object
, JSValueRef value
) {
98 JSObjectSetPrototype(context
, object
, value
);
99 _assert(CYIsStrictEqual(context
, JSObjectGetPrototype(context
, object
), value
));
102 /* JavaScript Strings {{{ */
103 JSStringRef
CYCopyJSString(const char *value
) {
104 return value
== NULL
? NULL
: JSStringCreateWithUTF8CString(value
);
107 JSStringRef
CYCopyJSString(JSStringRef value
) {
108 return value
== NULL
? NULL
: JSStringRetain(value
);
111 JSStringRef
CYCopyJSString(CYUTF8String value
) {
112 if (memchr(value
.data
, '\0', value
.size
) != NULL
) {
114 return CYCopyJSString(pool
.memdup(value
.data
, value
.size
));
115 } else if (value
.data
[value
.size
] != '\0') {
117 return CYCopyJSString(CYPoolUTF16String(pool
, value
));
119 return CYCopyJSString(value
.data
);
123 JSStringRef
CYCopyJSString(CYUTF16String value
) {
124 return JSStringCreateWithCharacters(value
.data
, value
.size
);
127 JSStringRef
CYCopyJSString(JSContextRef context
, JSValueRef value
) {
128 if (JSValueIsNull(context
, value
))
130 return _jsccall(JSValueToStringCopy
, context
, value
);
133 static CYUTF16String
CYCastUTF16String(JSStringRef value
) {
134 return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
));
137 CYUTF8String
CYPoolUTF8String(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
138 return CYPoolUTF8String(pool
, CYCastUTF16String(value
));
141 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
142 CYUTF8String
utf8(CYPoolUTF8String(pool
, context
, value
));
143 _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
);
147 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
148 return JSValueIsNull(context
, value
) ? NULL
: CYPoolCString(pool
, context
, CYJSString(context
, value
));
151 /* Index Offsets {{{ */
152 size_t CYGetIndex(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
153 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
157 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
159 static JSObjectRef
CYObjectMakeArray(JSContextRef context
, size_t length
, const JSValueRef values
[]) {
160 if (JSObjectMakeArray$
!= NULL
)
161 return _jsccall(*JSObjectMakeArray$
, context
, length
, values
);
163 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
164 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, length
, values
));
165 return CYCastJSObject(context
, value
);
169 static JSClassRef All_
;
170 static JSClassRef Context_
;
171 static JSClassRef CString_
;
173 static JSClassRef Global_
;
174 static JSClassRef Pointer_
;
175 static JSClassRef Struct_
;
180 JSStringRef length_s
;
181 JSStringRef message_s
;
184 JSStringRef prototype_s
;
186 JSStringRef splice_s
;
187 JSStringRef toCYON_s
;
188 JSStringRef toJSON_s
;
189 JSStringRef toPointer_s
;
190 JSStringRef toString_s
;
193 static sqlite3
*database_
;
195 static JSStringRef Result_
;
197 void CYFinalize(JSObjectRef object
) {
198 CYData
*internal(reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
)));
199 _assert(internal
->count_
!= _not(unsigned));
200 if (--internal
->count_
== 0)
204 void Structor_(CYPool
&pool
, sig::Type
*&type
) {
206 type
->primitive
== sig::pointer_P
&&
207 type
->data
.data
.type
->primitive
== sig::struct_P
&&
208 type
->data
.data
.type
->name
!= NULL
&&
209 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
211 type
->primitive
= sig::typename_P
;
212 type
->data
.data
.type
= NULL
;
216 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
222 JSClassRef
Type_privateData::Class_
;
227 JSGlobalContextRef context_
;
229 Context(JSGlobalContextRef context
) :
238 CString(char *value
, JSContextRef context
, JSObjectRef owner
) :
239 CYOwned(value
, context
, owner
)
247 Type_privateData
*type_
;
250 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type
*type
) :
251 CYOwned(value
, context
, owner
),
252 type_(new(*pool_
) Type_privateData(type
)),
257 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, const char *encoding
) :
258 CYOwned(value
, context
, owner
),
259 type_(new(*pool_
) Type_privateData(encoding
)),
265 struct Struct_privateData
:
268 Type_privateData
*type_
;
270 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
271 CYOwned(NULL
, context
, owner
)
276 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
277 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
278 CYPool
&pool(*internal
->pool_
);
279 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
280 internal
->type_
= typical
;
283 internal
->value_
= data
;
285 size_t size(typical
->GetFFI()->size
);
286 void *copy(internal
->pool_
->malloc
<void>(size
));
287 memcpy(copy
, data
, size
);
288 internal
->value_
= copy
;
291 return JSObjectMake(context
, Struct_
, internal
);
294 static void *CYCastSymbol(const char *name
) {
295 for (CYHook
*hook
: GetHooks())
296 if (hook
->CastSymbol
!= NULL
)
297 if (void *value
= (*hook
->CastSymbol
)(name
))
299 return dlsym(RTLD_DEFAULT
, name
);
302 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
303 return JSValueMakeBoolean(context
, value
);
306 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
307 return JSValueMakeNumber(context
, value
);
310 #define CYCastJSValue_(Type_) \
311 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
312 _assert(static_cast<Type_>(static_cast<double>(value)) == value); \
313 return JSValueMakeNumber(context, static_cast<double>(value)); \
317 CYCastJSValue_(unsigned int)
318 CYCastJSValue_(long int)
319 CYCastJSValue_(long unsigned int)
320 CYCastJSValue_(long long int)
321 CYCastJSValue_(long long unsigned int)
323 JSValueRef
CYJSUndefined(JSContextRef context
) {
324 return JSValueMakeUndefined(context
);
327 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
328 return _jsccall(JSValueToNumber
, context
, value
);
331 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
332 return JSValueToBoolean(context
, value
);
335 JSValueRef
CYJSNull(JSContextRef context
) {
336 return JSValueMakeNull(context
);
339 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
340 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
343 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
344 return CYCastJSValue(context
, CYJSString(value
));
347 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
348 return _jsccall(JSValueToObject
, context
, value
);
351 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
352 return _jsccall(JSObjectCallAsFunction
, context
, function
, _this
, count
, arguments
);
355 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
356 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
359 bool CYIsEqual(JSContextRef context
, JSValueRef lhs
, JSValueRef rhs
) {
360 return _jsccall(JSValueIsEqual
, context
, lhs
, rhs
);
363 bool CYIsStrictEqual(JSContextRef context
, JSValueRef lhs
, JSValueRef rhs
) {
364 return JSValueIsStrictEqual(context
, lhs
, rhs
);
367 size_t CYArrayLength(JSContextRef context
, JSObjectRef array
) {
368 return CYCastDouble(context
, CYGetProperty(context
, array
, length_s
));
371 JSValueRef
CYArrayGet(JSContextRef context
, JSObjectRef array
, size_t index
) {
372 return _jsccall(JSObjectGetPropertyAtIndex
, context
, array
, index
);
375 void CYArrayPush(JSContextRef context
, JSObjectRef array
, size_t length
, const JSValueRef arguments
[]) {
376 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
377 _jsccall(JSObjectCallAsFunction
, context
, CYCastJSObject(context
, CYGetProperty(context
, Array
, push_s
)), array
, length
, arguments
);
380 void CYArrayPush(JSContextRef context
, JSObjectRef array
, JSValueRef value
) {
381 return CYArrayPush(context
, array
, 1, &value
);
384 template <size_t Size_
>
385 class CYArrayBuilder
{
387 JSContextRef context_
;
390 JSValueRef values_
[Size_
];
394 array_
= CYObjectMakeArray(context_
, size_
, values_
);
396 CYArrayPush(context_
, array_
, size_
, values_
);
400 CYArrayBuilder(JSContextRef context
, JSObjectRef
&array
) :
411 void operator ()(JSValueRef value
) {
412 if (size_
== Size_
) {
417 values_
[size_
++] = value
;
421 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
428 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, arguments
[0])));
429 fwrite(string
.data
, string
.size
, 1, file
);
433 return CYJSUndefined(context
);
436 static void (*JSSynchronousGarbageCollectForDebugging$
)(JSContextRef
);
438 _visible
void CYGarbageCollect(JSContextRef context
) {
439 (JSSynchronousGarbageCollectForDebugging$
?: &JSGarbageCollect
)(context
);
442 static JSValueRef
Cycript_compile_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
444 CYUTF8String
before(CYPoolUTF8String(pool
, context
, CYJSString(context
, arguments
[0])));
445 std::stringbuf
value(std::string(before
.data
, before
.size
));
446 CYUTF8String
after(CYPoolCode(pool
, value
));
447 return CYCastJSValue(context
, CYJSString(after
));
448 } CYCatch_(NULL
, "SyntaxError") }
450 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
451 CYGarbageCollect(context
);
452 return CYJSUndefined(context
);
455 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> &objects
, JSValueRef
*exception
) { CYTry
{
456 switch (JSType type
= JSValueGetType(context
, value
)) {
457 case kJSTypeUndefined
:
462 return CYCastBool(context
, value
) ? "true" : "false";
464 case kJSTypeNumber
: {
465 std::ostringstream str
;
466 CYNumerify(str
, CYCastDouble(context
, value
));
467 std::string
value(str
.str());
468 return pool
.strmemdup(value
.c_str(), value
.size());
471 case kJSTypeString
: {
472 std::ostringstream str
;
473 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
474 CYStringify(str
, string
.data
, string
.size
);
475 std::string
value(str
.str());
476 return pool
.strmemdup(value
.c_str(), value
.size());
480 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
, objects
);
482 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
486 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> &objects
) {
487 return _jsccall(CYPoolCCYON
, pool
, context
, value
, objects
);
490 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> *objects
) {
492 return CYPoolCCYON(pool
, context
, value
, *objects
);
494 std::set
<void *> objects
;
495 return CYPoolCCYON(pool
, context
, value
, objects
);
499 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSObjectRef object
, std::set
<void *> &objects
) {
500 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
501 if (CYIsCallable(context
, toCYON
)) {
502 // XXX: this needs to be abstracted behind some kind of function
503 JSValueRef arguments
[1] = {CYCastJSValue(context
, reinterpret_cast<uintptr_t>(&objects
))};
504 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 1, arguments
));
505 _assert(value
!= NULL
);
506 return CYPoolCString(pool
, context
, value
);
509 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
510 if (CYIsCallable(context
, toJSON
)) {
511 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
512 return _jsccall(CYPoolCCYON
, pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), objects
);
515 if (JSObjectIsFunction(context
, object
)) {
516 JSValueRef
toString(CYGetProperty(context
, object
, toString_s
));
517 if (CYIsCallable(context
, toString
)) {
518 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
519 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toString
, object
, 1, arguments
));
520 _assert(value
!= NULL
);
521 return CYPoolCString(pool
, context
, value
);
525 _assert(objects
.insert(object
).second
);
527 std::ostringstream str
;
531 // XXX: this is, sadly, going to leak
532 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
536 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
542 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
543 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
548 CYStringify(str
, string
.data
, string
.size
);
553 JSValueRef
value(CYGetProperty(context
, object
, name
));
554 str
<< CYPoolCCYON(pool
, context
, value
, objects
);
555 } catch (const CYException
&error
) {
560 JSPropertyNameArrayRelease(names
);
564 std::string
string(str
.str());
565 return pool
.strmemdup(string
.c_str(), string
.size());
568 std::set
<void *> *CYCastObjects(JSContextRef context
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
571 return CYCastPointer
<std::set
<void *> *>(context
, arguments
[0]);
574 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
575 std::set
<void *> *objects(CYCastObjects(context
, _this
, count
, arguments
));
576 // XXX: this is horribly inefficient
577 std::set
<void *> backup
;
582 std::ostringstream str
;
586 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
589 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
596 JSValueRef
value(CYGetProperty(context
, _this
, index
));
597 if (!JSValueIsUndefined(context
, value
))
598 str
<< CYPoolCCYON(pool
, context
, value
, *objects
);
603 } catch (const CYException
&error
) {
610 std::string
value(str
.str());
611 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
614 static JSValueRef
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
616 std::ostringstream str
;
618 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
)));
619 CYStringify(str
, string
.data
, string
.size
);
621 std::string
value(str
.str());
622 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
625 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
626 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
627 return JSObjectMake(context
, Pointer_
, internal
);
630 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, const char *encoding
, JSObjectRef owner
) {
631 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, encoding
));
632 return JSObjectMake(context
, Pointer_
, internal
);
635 JSObjectRef
CYMakeCString(JSContextRef context
, char *pointer
, JSObjectRef owner
) {
636 CString
*internal(new CString(pointer
, context
, owner
));
637 return JSObjectMake(context
, CString_
, internal
);
640 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const sig::Signature
&signature
) {
641 return JSObjectMake(context
, Functor_
, new cy::Functor(signature
, function
));
644 static JSObjectRef
CYMakeFunctor(JSContextRef context
, const char *symbol
, const char *encoding
) {
645 void (*function
)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol
)));
646 if (function
== NULL
)
649 cy::Functor
*internal(new cy::Functor(encoding
, function
));
651 return JSObjectMake(context
, Functor_
, internal
);
654 static bool CYGetOffset(CYPool
&pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
655 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
658 void *CYCastPointer_(JSContextRef context
, JSValueRef value
, bool *guess
) {
661 else switch (JSValueGetType(context
, value
)) {
664 case kJSTypeObject
: {
665 JSObjectRef
object((JSObjectRef
) value
);
666 if (JSValueIsObjectOfClass(context
, value
, Pointer_
)) {
667 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
668 return internal
->value_
;
670 JSValueRef
toPointer(CYGetProperty(context
, object
, toPointer_s
));
671 if (CYIsCallable(context
, toPointer
)) {
672 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
));
673 _assert(value
!= NULL
);
674 return CYCastPointer_(context
, value
, guess
);
680 double number(CYCastDouble(context
, value
));
681 if (!std::isnan(number
))
682 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
684 throw CYJSError(context
, "cannot convert value to pointer");
692 void CYPoolFFI(CYPool
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
693 switch (type
->primitive
) {
695 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
698 #define CYPoolFFI_(primitive, native) \
699 case sig::primitive ## _P: \
700 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
703 CYPoolFFI_(uchar
, unsigned char)
704 CYPoolFFI_(schar
, signed char)
705 CYPoolFFI_(ushort
, unsigned short)
706 CYPoolFFI_(short, short)
707 CYPoolFFI_(ulong
, unsigned long)
708 CYPoolFFI_(long, long)
709 CYPoolFFI_(uint
, unsigned int)
711 CYPoolFFI_(ulonglong
, unsigned long long)
712 CYPoolFFI_(longlong
, long long)
713 CYPoolFFI_(float, float)
714 CYPoolFFI_(double, double)
717 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
718 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
719 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
720 ffi_type
*field(ffi
->elements
[index
]);
723 if (aggregate
== NULL
)
726 rhs
= CYGetProperty(context
, aggregate
, index
);
727 if (JSValueIsUndefined(context
, rhs
))
728 throw CYJSError(context
, "unable to extract array value");
731 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
738 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
741 case sig::string_P
: {
743 *reinterpret_cast<const char **>(data
) = CYCastPointer
<const char *>(context
, value
, &guess
);
744 if (guess
&& pool
!= NULL
)
745 *reinterpret_cast<const char **>(data
) = CYPoolCString(*pool
, context
, value
);
748 case sig::struct_P
: {
749 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
750 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
751 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
752 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
753 ffi_type
*field(ffi
->elements
[index
]);
756 if (aggregate
== NULL
)
759 rhs
= CYGetProperty(context
, aggregate
, index
);
760 if (JSValueIsUndefined(context
, rhs
)) {
761 if (element
->name
!= NULL
)
762 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
765 if (JSValueIsUndefined(context
, rhs
)) undefined
:
766 throw CYJSError(context
, "unable to extract structure value");
770 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
779 // XXX: implement a conversion from a single character string?
780 CYPoolFFI_(char, char)
783 for (CYHook
*hook
: GetHooks())
784 if (hook
->PoolFFI
!= NULL
)
785 if ((*hook
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
788 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
792 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
793 switch (type
->primitive
) {
795 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
797 #define CYFromFFI_(primitive, native) \
798 case sig::primitive ## _P: \
799 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
801 CYFromFFI_(uchar, unsigned char)
802 CYFromFFI_(schar
, signed char)
803 CYFromFFI_(ushort
, unsigned short)
804 CYFromFFI_(short, short)
805 CYFromFFI_(ulong
, unsigned long)
806 CYFromFFI_(long, long)
807 CYFromFFI_(uint
, unsigned int)
809 CYFromFFI_(ulonglong
, unsigned long long)
810 CYFromFFI_(longlong
, long long)
811 CYFromFFI_(float, float)
812 CYFromFFI_(double, double)
815 if (void *pointer
= data
)
816 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
820 if (void *pointer
= *reinterpret_cast<void **>(data
))
821 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
825 if (char *pointer
= *reinterpret_cast<char **>(data
))
826 return CYMakeCString(context
, pointer
, owner
);
830 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
832 return CYJSUndefined(context
);
834 CYFromFFI_(char, char)
837 return CYJSNull(context
);
839 for (CYHook
*hook
: GetHooks())
840 if (hook
->FromFFI
!= NULL
)
841 if (JSValueRef value
= (*hook
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
844 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
848 void CYExecuteClosure(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
849 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
851 JSContextRef
context(internal
->context_
);
853 size_t count(internal
->cif_
.nargs
);
854 JSValueRef values
[count
];
856 for (size_t index(0); index
!= count
; ++index
)
857 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
859 JSValueRef
value(internal
->adapter_(context
, count
, values
, internal
->function_
));
860 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
863 static JSValueRef
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
) {
864 return CYCallAsFunction(context
, function
, NULL
, count
, values
);
867 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
, JSValueRef (*adapter
)(JSContextRef
, size_t, JSValueRef
[], JSObjectRef
)) {
868 // XXX: in case of exceptions this will leak
869 // XXX: in point of fact, this may /need/ to leak :(
870 Closure_privateData
*internal(new Closure_privateData(context
, function
, adapter
, signature
));
872 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
874 ffi_closure
*writable(reinterpret_cast<ffi_closure
*>(ffi_closure_alloc(sizeof(ffi_closure
), &executable
)));
876 ffi_status
status(ffi_prep_closure_loc(writable
, &internal
->cif_
, &CYExecuteClosure
, internal
, executable
));
877 _assert(status
== FFI_OK
);
879 internal
->value_
= executable
;
881 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
882 NULL
, sizeof(ffi_closure
),
883 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
887 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, &CYExecuteClosure
, internal
));
888 _assert(status
== FFI_OK
);
890 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
892 internal
->value_
= closure
;
898 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
) {
899 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, signature
, &FunctionAdapter_
));
900 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
901 // XXX: see above notes about needing to leak
902 JSValueProtect(CYGetJSContext(context
), object
);
906 JSValueRef
CYGetCachedValue(JSContextRef context
, JSStringRef name
) {
907 return CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
);
910 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
911 return CYCastJSObject(context
, CYGetCachedValue(context
, name
));
914 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const sig::Signature
&signature
) {
915 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
917 bool function(_jsccall(JSValueIsInstanceOfConstructor
, context
, value
, Function
));
919 JSObjectRef
function(CYCastJSObject(context
, value
));
920 return CYMakeFunctor(context
, function
, signature
);
922 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
923 return CYMakeFunctor(context
, function
, signature
);
927 static JSValueRef
CString_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
929 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(object
)));
930 char *string(static_cast<char *>(internal
->value_
));
933 if (!CYGetOffset(pool
, context
, property
, offset
))
936 return CYCastJSValue(context
, CYJSString(CYUTF8String(&string
[offset
], 1)));
939 static bool CString_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
941 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(object
)));
942 char *string(static_cast<char *>(internal
->value_
));
945 if (!CYGetOffset(pool
, context
, property
, offset
))
948 const char *data(CYPoolCString(pool
, context
, value
));
949 string
[offset
] = *data
;
953 static bool Index_(CYPool
&pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
954 Type_privateData
*typical(internal
->type_
);
955 sig::Type
*type(typical
->type_
);
959 const char *name(CYPoolCString(pool
, context
, property
));
960 size_t length(strlen(name
));
961 double number(CYCastDouble(name
, length
));
963 size_t count(type
->data
.signature
.count
);
965 if (std::isnan(number
)) {
966 if (property
== NULL
)
969 sig::Element
*elements(type
->data
.signature
.elements
);
971 for (size_t local(0); local
!= count
; ++local
) {
972 sig::Element
*element(&elements
[local
]);
973 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
981 index
= static_cast<ssize_t
>(number
);
982 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
987 ffi_type
**elements(typical
->GetFFI()->elements
);
990 for (ssize_t
local(0); local
!= index
; ++local
) {
991 offset
+= elements
[local
]->size
;
992 CYAlign(offset
, elements
[local
+ 1]->alignment
);
995 base
= reinterpret_cast<uint8_t *>(internal
->value_
) + offset
;
999 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1001 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1003 if (JSStringIsEqual(property
, length_s
))
1004 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
1006 Type_privateData
*typical(internal
->type_
);
1007 if (typical
->type_
== NULL
)
1009 sig::Type
&type(*typical
->type_
);
1012 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
1014 else if (!CYGetOffset(pool
, context
, property
, offset
))
1017 if (type
.primitive
== sig::function_P
)
1018 return CYMakeFunctor(context
, reinterpret_cast<void (*)()>(internal
->value_
), type
.data
.signature
);
1020 ffi_type
*ffi(typical
->GetFFI());
1022 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
1023 base
+= ffi
->size
* offset
;
1025 JSObjectRef
owner(internal
->GetOwner() ?: object
);
1026 return CYFromFFI(context
, &type
, ffi
, base
, false, owner
);
1029 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1031 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1032 Type_privateData
*typical(internal
->type_
);
1034 if (typical
->type_
== NULL
)
1038 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
1040 else if (!CYGetOffset(pool
, context
, property
, offset
))
1043 ffi_type
*ffi(typical
->GetFFI());
1045 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
1046 base
+= ffi
->size
* offset
;
1048 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
1052 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1053 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
1054 Type_privateData
*typical(internal
->type_
);
1055 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
1058 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1060 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1061 Type_privateData
*typical(internal
->type_
);
1066 if (!Index_(pool
, context
, internal
, property
, index
, base
))
1069 JSObjectRef
owner(internal
->GetOwner() ?: object
);
1071 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
1074 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1076 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1077 Type_privateData
*typical(internal
->type_
);
1082 if (!Index_(pool
, context
, internal
, property
, index
, base
))
1085 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
1089 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1090 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1091 Type_privateData
*typical(internal
->type_
);
1092 sig::Type
*type(typical
->type_
);
1097 size_t count(type
->data
.signature
.count
);
1098 sig::Element
*elements(type
->data
.signature
.elements
);
1102 for (size_t index(0); index
!= count
; ++index
) {
1104 name
= elements
[index
].name
;
1107 sprintf(number
, "%zu", index
);
1111 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
1115 void CYCallFunction(CYPool
&pool
, JSContextRef context
, ffi_cif
*cif
, void (*function
)(), void *value
, void **values
) {
1116 ffi_call(cif
, function
, value
, values
);
1119 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
)()) {
1120 if (setups
+ count
!= signature
->count
- 1)
1121 throw CYJSError(context
, "incorrect number of arguments to ffi function");
1123 size_t size(setups
+ count
);
1125 memcpy(values
, setup
, sizeof(void *) * setups
);
1127 for (size_t index(setups
); index
!= size
; ++index
) {
1128 sig::Element
*element(&signature
->elements
[index
+ 1]);
1129 ffi_type
*ffi(cif
->arg_types
[index
]);
1131 values
[index
] = new(pool
) uint8_t[ffi
->size
];
1132 CYPoolFFI(&pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
1135 uint8_t value
[cif
->rtype
->size
];
1137 void (*call
)(CYPool
&, JSContextRef
, ffi_cif
*, void (*)(), void *, void **) = &CYCallFunction
;
1138 // XXX: this only supports one hook, but it is a bad idea anyway
1139 for (CYHook
*hook
: GetHooks())
1140 if (hook
->CallFunction
!= NULL
)
1141 call
= hook
->CallFunction
;
1143 call(pool
, context
, cif
, function
, value
, values
);
1144 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
1147 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1149 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1150 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
1153 static JSValueRef
Pointer_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1154 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1155 if (internal
->type_
->type_
->primitive
!= sig::function_P
)
1156 throw CYJSError(context
, "cannot call a pointer to non-function");
1157 JSObjectRef
functor(CYCastJSObject(context
, CYGetProperty(context
, object
, cyi_s
)));
1158 return CYCallAsFunction(context
, functor
, _this
, count
, arguments
);
1161 JSObjectRef
CYMakeType(JSContextRef context
, sig::Primitive primitive
) {
1162 Type_privateData
*internal(new Type_privateData(primitive
));
1163 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1166 JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
1167 Type_privateData
*internal(new Type_privateData(type
));
1168 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1171 JSObjectRef
CYMakeType(JSContextRef context
, sig::Signature
*signature
) {
1178 type
.primitive
= sig::function_P
;
1179 sig::Copy(pool
, type
.data
.signature
, *signature
);
1181 return CYMakeType(context
, &type
);
1184 extern "C" bool CYBridgeHash(CYPool
&pool
, CYUTF8String name
, const char *&code
, unsigned &flags
) {
1185 sqlite3_stmt
*statement
;
1187 _sqlcall(sqlite3_prepare(database_
,
1189 "\"cache\".\"code\", "
1190 "\"cache\".\"flags\" "
1193 " \"cache\".\"system\" & " CY_SYSTEM
" == " CY_SYSTEM
" and"
1194 " \"cache\".\"name\" = ?"
1196 , -1, &statement
, NULL
));
1198 _sqlcall(sqlite3_bind_text(statement
, 1, name
.data
, name
.size
, SQLITE_STATIC
));
1201 if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
)
1205 code
= sqlite3_column_pooled(pool
, statement
, 0);
1206 flags
= sqlite3_column_int(statement
, 1);
1209 _sqlcall(sqlite3_finalize(statement
));
1213 static bool All_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
1214 if (JSStringIsEqualToUTF8CString(property
, "errno"))
1217 JSObjectRef
global(CYGetGlobalObject(context
));
1218 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1219 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1221 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1222 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1223 if (CYHasProperty(context
, space
, property
))
1229 if (CYBridgeHash(pool
, CYPoolUTF8String(pool
, context
, property
), code
, flags
))
1235 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1236 if (JSStringIsEqualToUTF8CString(property
, "errno"))
1237 return CYCastJSValue(context
, errno
);
1239 JSObjectRef
global(CYGetGlobalObject(context
));
1240 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1241 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1243 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1244 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1245 if (JSValueRef value
= CYGetProperty(context
, space
, property
))
1246 if (!JSValueIsUndefined(context
, value
))
1252 if (CYBridgeHash(pool
, CYPoolUTF8String(pool
, context
, property
), code
, flags
)) {
1253 CYUTF8String parsed
;
1256 parsed
= CYPoolCode(pool
, code
);
1257 } catch (const CYException
&error
) {
1258 CYThrow("%s", pool
.strcat("error caching ", CYPoolCString(pool
, context
, property
), ": ", error
.PoolCString(pool
), NULL
));
1261 JSValueRef
result(_jsccall(JSEvaluateScript
, context
, CYJSString(parsed
), NULL
, NULL
, 0));
1264 JSObjectRef
cache(CYGetCachedObject(context
, CYJSString("cache")));
1265 CYSetProperty(context
, cache
, property
, result
);
1274 static JSValueRef
All_complete_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1275 _assert(count
== 1);
1277 CYUTF8String
prefix(CYPoolUTF8String(pool
, context
, CYJSString(context
, arguments
[0])));
1279 JSObjectRef
array(NULL
);
1282 CYArrayBuilder
<1024> values(context
, array
);
1284 sqlite3_stmt
*statement
;
1286 if (prefix
.size
== 0)
1287 _sqlcall(sqlite3_prepare(database_
,
1289 "\"cache\".\"name\" "
1292 " \"cache\".\"system\" & " CY_SYSTEM
" == " CY_SYSTEM
1293 , -1, &statement
, NULL
));
1295 _sqlcall(sqlite3_prepare(database_
,
1297 "\"cache\".\"name\" "
1300 " \"cache\".\"name\" >= ? and \"cache\".\"name\" < ? and "
1301 " \"cache\".\"system\" & " CY_SYSTEM
" == " CY_SYSTEM
1302 , -1, &statement
, NULL
));
1304 _sqlcall(sqlite3_bind_text(statement
, 1, prefix
.data
, prefix
.size
, SQLITE_STATIC
));
1306 char *after(pool
.strndup(prefix
.data
, prefix
.size
));
1307 ++after
[prefix
.size
- 1];
1308 _sqlcall(sqlite3_bind_text(statement
, 2, after
, prefix
.size
, SQLITE_STATIC
));
1311 while (_sqlcall(sqlite3_step(statement
)) != SQLITE_DONE
)
1312 values(CYCastJSValue(context
, CYJSString(sqlite3_column_string(statement
, 0))));
1314 _sqlcall(sqlite3_finalize(statement
));
1320 static void All_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1321 JSObjectRef
global(CYGetGlobalObject(context
));
1322 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1323 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1325 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1326 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1))) {
1327 JSPropertyNameArrayRef
subset(JSObjectCopyPropertyNames(context
, space
));
1328 for (size_t index(0), count(JSPropertyNameArrayGetCount(subset
)); index
!= count
; ++index
)
1329 JSPropertyNameAccumulatorAddName(names
, JSPropertyNameArrayGetNameAtIndex(subset
, index
));
1330 JSPropertyNameArrayRelease(subset
);
1334 static JSObjectRef
CString_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1336 throw CYJSError(context
, "incorrect number of arguments to CString constructor");
1337 char *value(CYCastPointer
<char *>(context
, arguments
[0]));
1338 return CYMakeCString(context
, value
, NULL
);
1341 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1343 throw CYJSError(context
, "incorrect number of arguments to Pointer constructor");
1347 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1348 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1350 sig::Signature signature
;
1351 sig::Parse(pool
, &signature
, type
, &Structor_
);
1353 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1356 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1360 } else if (count
== 1) {
1361 const char *encoding(CYPoolCString(pool
, context
, arguments
[0]));
1362 sig::Signature signature
;
1363 sig::Parse(pool
, &signature
, encoding
, &Structor_
);
1364 return CYMakeType(context
, signature
.elements
[0].type
);
1365 } else if (count
== 2) {
1366 JSObjectRef
types(CYCastJSObject(context
, arguments
[0]));
1367 size_t count(CYArrayLength(context
, types
));
1369 JSObjectRef
names(CYCastJSObject(context
, arguments
[1]));
1375 type
.primitive
= sig::struct_P
;
1376 type
.data
.signature
.elements
= new(pool
) sig::Element
[count
];
1377 type
.data
.signature
.count
= count
;
1379 for (size_t i(0); i
!= count
; ++i
) {
1380 sig::Element
&element(type
.data
.signature
.elements
[i
]);
1381 element
.offset
= _not(size_t);
1383 JSValueRef
name(CYArrayGet(context
, names
, i
));
1384 if (JSValueIsUndefined(context
, name
))
1385 element
.name
= NULL
;
1387 element
.name
= CYPoolCString(pool
, context
, name
);
1389 JSObjectRef
object(CYCastJSObject(context
, CYArrayGet(context
, types
, i
)));
1390 _assert(JSValueIsObjectOfClass(context
, object
, Type_privateData::Class_
));
1391 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1392 element
.type
= internal
->type_
;
1395 return CYMakeType(context
, &type
);
1397 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1401 static JSValueRef Type_callAsFunction_$
With(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], sig::Primitive primitive
, JSValueRef
*exception
) { CYTry
{
1402 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1410 type
.primitive
= primitive
;
1411 type
.data
.signature
.elements
= new(pool
) sig::Element
[1 + count
];
1412 type
.data
.signature
.count
= 1 + count
;
1414 type
.data
.signature
.elements
[0].name
= NULL
;
1415 type
.data
.signature
.elements
[0].type
= internal
->type_
;
1416 type
.data
.signature
.elements
[0].offset
= _not(size_t);
1418 for (size_t i(0); i
!= count
; ++i
) {
1419 sig::Element
&element(type
.data
.signature
.elements
[i
+ 1]);
1420 element
.name
= NULL
;
1421 element
.offset
= _not(size_t);
1423 JSObjectRef
object(CYCastJSObject(context
, arguments
[i
]));
1424 _assert(JSValueIsObjectOfClass(context
, object
, Type_privateData::Class_
));
1425 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1427 element
.type
= internal
->type_
;
1430 return CYMakeType(context
, &type
);
1433 static JSValueRef
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1435 throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf");
1436 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1439 size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0])));
1440 if (index
== _not(size_t))
1441 throw CYJSError(context
, "invalid array size used with Type.arrayOf");
1447 type
.primitive
= sig::array_P
;
1448 type
.data
.data
.type
= internal
->type_
;
1449 type
.data
.data
.size
= index
;
1451 return CYMakeType(context
, &type
);
1454 static JSValueRef
Type_callAsFunction_blockWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1455 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::block_P
, exception
);
1458 static JSValueRef
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1460 throw CYJSError(context
, "incorrect number of arguments to Type.constant");
1461 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1463 sig::Type
type(*internal
->type_
);
1464 type
.flags
|= JOC_TYPE_CONST
;
1465 return CYMakeType(context
, &type
);
1468 static JSValueRef
Type_callAsFunction_long(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1470 throw CYJSError(context
, "incorrect number of arguments to Type.long");
1471 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1473 sig::Type
type(*internal
->type_
);
1475 switch (type
.primitive
) {
1476 case sig::short_P
: type
.primitive
= sig::int_P
; break;
1477 case sig::int_P
: type
.primitive
= sig::long_P
; break;
1478 case sig::long_P
: type
.primitive
= sig::longlong_P
; break;
1479 default: throw CYJSError(context
, "invalid type argument to Type.long");
1482 return CYMakeType(context
, &type
);
1485 static JSValueRef
Type_callAsFunction_short(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1487 throw CYJSError(context
, "incorrect number of arguments to Type.short");
1488 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1490 sig::Type
type(*internal
->type_
);
1492 switch (type
.primitive
) {
1493 case sig::int_P
: type
.primitive
= sig::short_P
; break;
1494 case sig::long_P
: type
.primitive
= sig::int_P
; break;
1495 case sig::longlong_P
: type
.primitive
= sig::long_P
; break;
1496 default: throw CYJSError(context
, "invalid type argument to Type.short");
1499 return CYMakeType(context
, &type
);
1502 static JSValueRef
Type_callAsFunction_signed(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1504 throw CYJSError(context
, "incorrect number of arguments to Type.signed");
1505 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1507 sig::Type
type(*internal
->type_
);
1509 switch (type
.primitive
) {
1510 case sig::char_P
: case sig::schar_P
: case sig::uchar_P
: type
.primitive
= sig::schar_P
; break;
1511 case sig::short_P
: case sig::ushort_P
: type
.primitive
= sig::short_P
; break;
1512 case sig::int_P
: case sig::uint_P
: type
.primitive
= sig::int_P
; break;
1513 case sig::long_P
: case sig::ulong_P
: type
.primitive
= sig::long_P
; break;
1514 case sig::longlong_P
: case sig::ulonglong_P
: type
.primitive
= sig::longlong_P
; break;
1515 default: throw CYJSError(context
, "invalid type argument to Type.signed");
1518 return CYMakeType(context
, &type
);
1521 static JSValueRef
Type_callAsFunction_unsigned(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1523 throw CYJSError(context
, "incorrect number of arguments to Type.unsigned");
1524 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1526 sig::Type
type(*internal
->type_
);
1528 switch (type
.primitive
) {
1529 case sig::char_P
: case sig::schar_P
: case sig::uchar_P
: type
.primitive
= sig::uchar_P
; break;
1530 case sig::short_P
: case sig::ushort_P
: type
.primitive
= sig::ushort_P
; break;
1531 case sig::int_P
: case sig::uint_P
: type
.primitive
= sig::uint_P
; break;
1532 case sig::long_P
: case sig::ulong_P
: type
.primitive
= sig::ulong_P
; break;
1533 case sig::longlong_P
: case sig::ulonglong_P
: type
.primitive
= sig::ulonglong_P
; break;
1534 default: throw CYJSError(context
, "invalid type argument to Type.unsigned");
1537 return CYMakeType(context
, &type
);
1540 static JSValueRef
Type_callAsFunction_functionWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1541 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::function_P
, exception
);
1544 static JSValueRef
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1546 throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo");
1547 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1552 if (internal
->type_
->primitive
== sig::char_P
) {
1553 type
.flags
= internal
->type_
->flags
;
1554 type
.primitive
= sig::string_P
;
1555 type
.data
.data
.type
= NULL
;
1556 type
.data
.data
.size
= 0;
1559 type
.primitive
= sig::pointer_P
;
1560 type
.data
.data
.type
= internal
->type_
;
1561 type
.data
.data
.size
= 0;
1564 return CYMakeType(context
, &type
);
1567 static JSValueRef
Type_callAsFunction_withName(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1569 throw CYJSError(context
, "incorrect number of arguments to Type.withName");
1570 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1573 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1575 sig::Type
type(*internal
->type_
);
1577 return CYMakeType(context
, &type
);
1580 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1582 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1583 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1585 if (internal
->type_
->primitive
== sig::function_P
)
1586 return CYMakeFunctor(context
, arguments
[0], internal
->type_
->data
.signature
);
1588 sig::Type
*type(internal
->type_
);
1589 ffi_type
*ffi(internal
->GetFFI());
1591 uint8_t value
[ffi
->size
];
1593 CYPoolFFI(&pool
, context
, type
, ffi
, value
, arguments
[0]);
1594 return CYFromFFI(context
, type
, ffi
, value
);
1597 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1599 throw CYJSError(context
, "incorrect number of arguments to Type allocator");
1600 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1602 sig::Type
*type(internal
->type_
);
1605 if (type
->primitive
!= sig::array_P
)
1606 length
= _not(size_t);
1608 length
= type
->data
.data
.size
;
1609 type
= type
->data
.data
.type
;
1612 JSObjectRef
pointer(CYMakePointer(context
, NULL
, length
, type
, NULL
, NULL
));
1613 Pointer
*value(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(pointer
)));
1614 value
->value_
= value
->pool_
->malloc
<void>(internal
->GetFFI()->size
);
1615 memset(value
->value_
, 0, internal
->GetFFI()->size
);
1619 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1621 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1623 const char *encoding(CYPoolCString(pool
, context
, arguments
[1]));
1624 sig::Signature signature
;
1625 sig::Parse(pool
, &signature
, encoding
, &Structor_
);
1626 return CYMakeFunctor(context
, arguments
[0], signature
);
1629 static JSValueRef
CString_callAsFunction_toPointer(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1630 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(_this
)));
1636 type
.primitive
= sig::char_P
;
1637 type
.data
.data
.type
= NULL
;
1638 type
.data
.data
.size
= 0;
1640 return CYMakePointer(context
, internal
->value_
, _not(size_t), &type
, NULL
, NULL
);
1643 static JSValueRef Functor_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1645 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(_this
)));
1651 type
.primitive
= sig::function_P
;
1652 sig::Copy(pool
, type
.data
.signature
, internal
->signature_
);
1654 return CYMakePointer(context
, internal
->value_
, _not(size_t), &type
, NULL
, NULL
);
1657 static JSValueRef
Pointer_callAsFunction_toPointer(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1661 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1662 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1663 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1666 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1667 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1670 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1671 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1672 std::ostringstream str
;
1674 if (internal
->value_
== NULL
)
1676 else if (dladdr(internal
->value_
, &info
) == 0)
1677 str
<< internal
->value_
;
1679 str
<< info
.dli_sname
;
1680 off_t
offset(static_cast<char *>(internal
->value_
) - static_cast<char *>(info
.dli_saddr
));
1682 str
<< "+0x" << std::hex
<< offset
;
1684 std::string
value(str
.str());
1685 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
1688 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1689 std::set
<void *> *objects(CYCastObjects(context
, _this
, count
, arguments
));
1691 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1692 if (internal
->length_
!= _not(size_t)) {
1693 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
1694 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1695 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1696 } else if (internal
->type_
->type_
== NULL
) pointer
: {
1698 std::ostringstream str
;
1704 type
.primitive
= sig::pointer_P
;
1705 type
.data
.data
.type
= internal
->type_
->type_
;
1706 type
.data
.data
.size
= 0;
1709 CYOutput
output(*str
.rdbuf(), options
);
1710 (new(pool
) CYTypeExpression(Decode(pool
, &type
)))->Output(output
, CYNoFlags
);
1712 str
<< "(" << internal
->value_
<< ")";
1713 std::string
value(str
.str());
1714 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
1716 JSValueRef
value(CYGetProperty(context
, _this
, cyi_s
));
1717 if (JSValueIsUndefined(context
, value
))
1720 return CYCastJSValue(context
, pool
.strcat("&", CYPoolCCYON(pool
, context
, value
, objects
), NULL
));
1721 } catch (const CYException
&e
) {
1726 static JSValueRef
CString_getProperty_length(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1727 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(object
)));
1728 char *string(static_cast<char *>(internal
->value_
));
1729 return CYCastJSValue(context
, strlen(string
));
1732 static JSValueRef
CString_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1737 type
.primitive
= sig::char_P
;
1738 type
.data
.data
.type
= NULL
;
1739 type
.data
.data
.size
= 0;
1741 return CYMakeType(context
, &type
);
1744 static JSValueRef
Pointer_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1745 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1746 return CYMakeType(context
, internal
->type_
->type_
);
1749 static JSValueRef
CString_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1750 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1751 const char *string(static_cast<const char *>(internal
->value_
));
1752 std::ostringstream str
;
1754 CYStringify(str
, string
, strlen(string
), true);
1755 std::string
value(str
.str());
1756 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
1759 static JSValueRef
CString_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1760 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1761 const char *string(static_cast<const char *>(internal
->value_
));
1762 return CYCastJSValue(context
, string
);
1765 static JSValueRef
Functor_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1766 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1767 return CYMakeType(context
, &internal
->signature_
);
1770 static JSValueRef
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1771 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1772 return CYCastJSValue(context
, internal
->GetFFI()->alignment
);
1775 static JSValueRef
Type_getProperty_name(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1776 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1777 return CYCastJSValue(context
, internal
->type_
->name
);
1780 static JSValueRef
Type_getProperty_size(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1781 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1782 return CYCastJSValue(context
, internal
->GetFFI()->size
);
1785 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1786 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1788 const char *type(sig::Unparse(pool
, internal
->type_
));
1789 return CYCastJSValue(context
, CYJSString(type
));
1792 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1793 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1797 CYOutput
output(out
, options
);
1798 (new(pool
) CYTypeExpression(Decode(pool
, internal
->type_
)))->Output(output
, CYNoFlags
);
1799 return CYCastJSValue(context
, CYJSString(out
.str().c_str()));
1802 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1803 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1806 static JSStaticFunction All_staticFunctions
[2] = {
1807 {"cy$complete", &All_complete_callAsFunction
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1811 static JSStaticFunction CString_staticFunctions
[6] = {
1812 {"toCYON", &CString_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1813 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1814 {"toPointer", &CString_callAsFunction_toPointer
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1815 {"toString", &CString_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1816 {"valueOf", &CString_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1820 static JSStaticValue CString_staticValues
[3] = {
1821 {"length", &CString_getProperty_length
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1822 {"type", &CString_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1823 {NULL
, NULL
, NULL
, 0}
1826 static JSStaticFunction Pointer_staticFunctions
[5] = {
1827 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1828 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1829 {"toPointer", &Pointer_callAsFunction_toPointer
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1830 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1834 static JSStaticValue Pointer_staticValues
[2] = {
1835 {"type", &Pointer_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1836 {NULL
, NULL
, NULL
, 0}
1839 static JSStaticFunction Struct_staticFunctions
[2] = {
1840 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1844 static JSStaticFunction Functor_staticFunctions
[5] = {
1845 {"$cya", &Functor_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1846 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1847 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1848 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1853 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1856 static JSStaticValue Functor_staticValues
[2] = {
1857 {"type", &Functor_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1858 {NULL
, NULL
, NULL
, 0}
1862 JSStaticValue
const * const Functor::StaticValues
= Functor_staticValues
;
1865 static JSStaticValue Type_staticValues
[4] = {
1866 {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1867 {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1868 {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1869 {NULL
, NULL
, NULL
, 0}
1872 static JSStaticFunction Type_staticFunctions
[14] = {
1873 {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1874 {"blockWith", &Type_callAsFunction_blockWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1875 {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1876 {"functionWith", &Type_callAsFunction_functionWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1877 {"long", &Type_callAsFunction_long
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1878 {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1879 {"short", &Type_callAsFunction_short
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1880 {"signed", &Type_callAsFunction_signed
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1881 {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1882 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1883 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1884 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1885 {"unsigned", &Type_callAsFunction_unsigned
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1889 _visible
void CYSetArgs(int argc
, const char *argv
[]) {
1890 JSContextRef
context(CYGetJSContext());
1891 JSValueRef args
[argc
];
1892 for (int i(0); i
!= argc
; ++i
)
1893 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1895 JSObjectRef
array(CYObjectMakeArray(context
, argc
, args
));
1896 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1897 CYSetProperty(context
, System
, CYJSString("args"), array
);
1900 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1901 return JSContextGetGlobalObject(context
);
1904 // XXX: this is neither exceptin safe nor even terribly sane
1905 class ExecutionHandle
{
1907 JSContextRef context_
;
1908 std::vector
<void *> handles_
;
1911 ExecutionHandle(JSContextRef context
) :
1914 handles_
.resize(GetHooks().size());
1915 for (size_t i(0); i
!= GetHooks().size(); ++i
) {
1916 CYHook
*hook(GetHooks()[i
]);
1917 if (hook
->ExecuteStart
!= NULL
)
1918 handles_
[i
] = (*hook
->ExecuteStart
)(context_
);
1924 ~ExecutionHandle() {
1925 for (size_t i(GetHooks().size()); i
!= 0; --i
) {
1926 CYHook
*hook(GetHooks()[i
-1]);
1927 if (hook
->ExecuteEnd
!= NULL
)
1928 (*hook
->ExecuteEnd
)(context_
, handles_
[i
-1]);
1933 static volatile bool cancel_
;
1935 static bool CYShouldTerminate(JSContextRef context
, void *arg
) {
1939 _visible
const char *CYExecute(JSContextRef context
, CYPool
&pool
, CYUTF8String code
) {
1940 ExecutionHandle
handle(context
);
1943 if (&JSContextGroupSetExecutionTimeLimit
!= NULL
)
1944 JSContextGroupSetExecutionTimeLimit(JSContextGetGroup(context
), 0.5, &CYShouldTerminate
, NULL
);
1947 JSValueRef
result(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0));
1948 if (JSValueIsUndefined(context
, result
))
1951 std::set
<void *> objects
;
1952 const char *json(_jsccall(CYPoolCCYON
, pool
, context
, result
, objects
));
1953 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1956 } catch (const CYException
&error
) {
1957 return pool
.strcat("throw ", error
.PoolCString(pool
), NULL
);
1961 _visible
void CYCancel() {
1965 static const char *CYPoolLibraryPath(CYPool
&pool
);
1967 static bool initialized_
= false;
1969 void CYInitializeDynamic() {
1971 initialized_
= true;
1975 const char *db(pool
.strcat(CYPoolLibraryPath(pool
), "/libcycript.db", NULL
));
1976 _sqlcall(sqlite3_open_v2(db
, &database_
, SQLITE_OPEN_READONLY
, NULL
));
1978 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1979 JSSynchronousGarbageCollectForDebugging$
= reinterpret_cast<void (*)(JSContextRef
)>(dlsym(RTLD_DEFAULT
, "JSSynchronousGarbageCollectForDebugging"));
1981 JSClassDefinition definition
;
1983 definition
= kJSClassDefinitionEmpty
;
1984 definition
.className
= "All";
1985 definition
.staticFunctions
= All_staticFunctions
;
1986 definition
.hasProperty
= &All_hasProperty
;
1987 definition
.getProperty
= &All_getProperty
;
1988 definition
.getPropertyNames
= &All_getPropertyNames
;
1989 All_
= JSClassCreate(&definition
);
1991 definition
= kJSClassDefinitionEmpty
;
1992 definition
.className
= "Context";
1993 definition
.finalize
= &CYFinalize
;
1994 Context_
= JSClassCreate(&definition
);
1996 definition
= kJSClassDefinitionEmpty
;
1997 definition
.className
= "CString";
1998 definition
.staticFunctions
= CString_staticFunctions
;
1999 definition
.staticValues
= CString_staticValues
;
2000 definition
.getProperty
= &CString_getProperty
;
2001 definition
.setProperty
= &CString_setProperty
;
2002 definition
.finalize
= &CYFinalize
;
2003 CString_
= JSClassCreate(&definition
);
2005 definition
= kJSClassDefinitionEmpty
;
2006 definition
.className
= "Functor";
2007 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
2008 definition
.staticValues
= Functor_staticValues
;
2009 definition
.callAsFunction
= &Functor_callAsFunction
;
2010 definition
.finalize
= &CYFinalize
;
2011 Functor_
= JSClassCreate(&definition
);
2013 definition
= kJSClassDefinitionEmpty
;
2014 definition
.className
= "Pointer";
2015 definition
.staticFunctions
= Pointer_staticFunctions
;
2016 definition
.staticValues
= Pointer_staticValues
;
2017 definition
.callAsFunction
= &Pointer_callAsFunction
;
2018 definition
.getProperty
= &Pointer_getProperty
;
2019 definition
.setProperty
= &Pointer_setProperty
;
2020 definition
.finalize
= &CYFinalize
;
2021 Pointer_
= JSClassCreate(&definition
);
2023 definition
= kJSClassDefinitionEmpty
;
2024 definition
.className
= "Struct";
2025 definition
.staticFunctions
= Struct_staticFunctions
;
2026 definition
.getProperty
= &Struct_getProperty
;
2027 definition
.setProperty
= &Struct_setProperty
;
2028 definition
.getPropertyNames
= &Struct_getPropertyNames
;
2029 definition
.finalize
= &CYFinalize
;
2030 Struct_
= JSClassCreate(&definition
);
2032 definition
= kJSClassDefinitionEmpty
;
2033 definition
.className
= "Type";
2034 definition
.staticValues
= Type_staticValues
;
2035 definition
.staticFunctions
= Type_staticFunctions
;
2036 definition
.callAsFunction
= &Type_callAsFunction
;
2037 definition
.callAsConstructor
= &Type_callAsConstructor
;
2038 definition
.finalize
= &CYFinalize
;
2039 Type_privateData::Class_
= JSClassCreate(&definition
);
2041 definition
= kJSClassDefinitionEmpty
;
2042 definition
.className
= "Global";
2043 //definition.getProperty = &Global_getProperty;
2044 Global_
= JSClassCreate(&definition
);
2046 Array_s
= JSStringCreateWithUTF8CString("Array");
2047 cy_s
= JSStringCreateWithUTF8CString("$cy");
2048 cyi_s
= JSStringCreateWithUTF8CString("$cyi");
2049 length_s
= JSStringCreateWithUTF8CString("length");
2050 message_s
= JSStringCreateWithUTF8CString("message");
2051 name_s
= JSStringCreateWithUTF8CString("name");
2052 pop_s
= JSStringCreateWithUTF8CString("pop");
2053 prototype_s
= JSStringCreateWithUTF8CString("prototype");
2054 push_s
= JSStringCreateWithUTF8CString("push");
2055 splice_s
= JSStringCreateWithUTF8CString("splice");
2056 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
2057 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
2058 toPointer_s
= JSStringCreateWithUTF8CString("toPointer");
2059 toString_s
= JSStringCreateWithUTF8CString("toString");
2060 weak_s
= JSStringCreateWithUTF8CString("weak");
2062 Result_
= JSStringCreateWithUTF8CString("_");
2064 for (CYHook
*hook
: GetHooks())
2065 if (hook
->Initialize
!= NULL
)
2066 (*hook
->Initialize
)();
2069 void CYThrow(JSContextRef context
, JSValueRef value
) {
2071 throw CYJSError(context
, value
);
2074 const char *CYJSError::PoolCString(CYPool
&pool
) const {
2075 std::set
<void *> objects
;
2076 // XXX: this used to be CYPoolCString
2077 return CYPoolCCYON(pool
, context_
, value_
, objects
);
2080 JSValueRef
CYJSError::CastJSValue(JSContextRef context
, const char *name
) const {
2081 // XXX: what if the context is different? or the name? I dunno. ("epic" :/)
2085 JSValueRef
CYCastJSError(JSContextRef context
, const char *name
, const char *message
) {
2086 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString(name
)));
2087 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
2088 return _jsccall(JSObjectCallAsConstructor
, context
, Error
, 1, arguments
);
2091 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
, const char *name
) const {
2092 return CYCastJSError(context
, name
, message_
);
2095 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
2096 _assert(context
!= NULL
);
2101 va_start(args
, format
);
2102 // XXX: there might be a beter way to think about this
2103 const char *message(pool
.vsprintf(64, format
, args
));
2106 value_
= CYCastJSError(context
, "Error", message
);
2109 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
2110 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
2113 static const char *CYPoolLibraryPath(CYPool
&pool
) {
2115 _assert(dladdr(reinterpret_cast<void *>(&CYPoolLibraryPath
), &addr
) != 0);
2116 char *lib(pool
.strdup(addr
.dli_fname
));
2118 char *slash(strrchr(lib
, '/'));
2119 _assert(slash
!= NULL
);
2122 slash
= strrchr(lib
, '/');
2123 if (slash
!= NULL
&& strcmp(slash
, "/.libs") == 0)
2129 static JSValueRef
require_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
2130 _assert(count
== 1);
2133 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
2134 if (strchr(name
, '/') == NULL
&& (
2136 dlopen(pool
.strcat("/System/Library/Frameworks/", name
, ".framework/", name
, NULL
), RTLD_LAZY
| RTLD_GLOBAL
) != NULL
||
2137 dlopen(pool
.strcat("/System/Library/PrivateFrameworks/", name
, ".framework/", name
, NULL
), RTLD_LAZY
| RTLD_GLOBAL
) != NULL
||
2140 return CYJSUndefined(context
);
2142 JSObjectRef
resolve(CYCastJSObject(context
, CYGetProperty(context
, object
, CYJSString("resolve"))));
2143 CYJSString
path(context
, CYCallAsFunction(context
, resolve
, NULL
, 1, arguments
));
2145 CYJSString
property("exports");
2147 JSObjectRef
modules(CYGetCachedObject(context
, CYJSString("modules")));
2148 JSValueRef
cache(CYGetProperty(context
, modules
, path
));
2151 if (!JSValueIsUndefined(context
, cache
)) {
2152 JSObjectRef
module(CYCastJSObject(context
, cache
));
2153 result
= CYGetProperty(context
, module, property
);
2155 CYUTF8String
code(CYPoolFileUTF8String(pool
, CYPoolCString(pool
, context
, path
)));
2156 _assert(code
.data
!= NULL
);
2158 size_t length(strlen(name
));
2159 if (length
>= 5 && strcmp(name
+ length
- 5, ".json") == 0) {
2160 JSObjectRef
JSON(CYGetCachedObject(context
, CYJSString("JSON")));
2161 JSObjectRef
parse(CYCastJSObject(context
, CYGetProperty(context
, JSON
, CYJSString("parse"))));
2162 JSValueRef arguments
[1] = { CYCastJSValue(context
, CYJSString(code
)) };
2163 result
= CYCallAsFunction(context
, parse
, JSON
, 1, arguments
);
2165 JSObjectRef
module(JSObjectMake(context
, NULL
, NULL
));
2166 CYSetProperty(context
, modules
, path
, module);
2168 JSObjectRef
exports(JSObjectMake(context
, NULL
, NULL
));
2169 CYSetProperty(context
, module, property
, exports
);
2171 std::stringstream wrap
;
2172 wrap
<< "(function (exports, require, module, __filename) { " << code
<< "\n});";
2173 code
= CYPoolCode(pool
, *wrap
.rdbuf());
2175 JSValueRef
value(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0));
2176 JSObjectRef
function(CYCastJSObject(context
, value
));
2178 JSValueRef arguments
[4] = { exports
, object
, module, CYCastJSValue(context
, path
) };
2179 CYCallAsFunction(context
, function
, NULL
, 4, arguments
);
2180 result
= CYGetProperty(context
, module, property
);
2187 static bool CYRunScript(JSGlobalContextRef context
, const char *path
) {
2189 CYUTF8String
code(CYPoolFileUTF8String(pool
, pool
.strcat(CYPoolLibraryPath(pool
), path
, NULL
)));
2190 if (code
.data
== NULL
)
2193 code
= CYPoolCode(pool
, code
);
2194 _jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0);
2198 extern "C" void CYDestroyWeak(JSWeakObjectMapRef weak
, void *data
) {
2201 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
2202 CYInitializeDynamic();
2204 JSObjectRef
global(CYGetGlobalObject(context
));
2206 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
2207 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
2209 /* Cache Globals {{{ */
2210 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
2211 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
2213 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
2214 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
2216 JSObjectRef
Boolean(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Boolean"))));
2217 CYSetProperty(context
, cy
, CYJSString("Boolean"), Boolean
);
2219 JSObjectRef
Boolean_prototype(CYCastJSObject(context
, CYGetProperty(context
, Boolean
, prototype_s
)));
2220 CYSetProperty(context
, cy
, CYJSString("Boolean_prototype"), Boolean_prototype
);
2222 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
2223 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
2225 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
2226 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
2228 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
2229 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
2231 JSObjectRef
JSON(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("JSON"))));
2232 CYSetProperty(context
, cy
, CYJSString("JSON"), JSON
);
2234 JSObjectRef
Number(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Number"))));
2235 CYSetProperty(context
, cy
, CYJSString("Number"), Number
);
2237 JSObjectRef
Number_prototype(CYCastJSObject(context
, CYGetProperty(context
, Number
, prototype_s
)));
2238 CYSetProperty(context
, cy
, CYJSString("Number_prototype"), Number_prototype
);
2240 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
2241 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
2243 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
2244 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
2246 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
2247 CYSetProperty(context
, cy
, CYJSString("String"), String
);
2249 JSObjectRef
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
)));
2250 CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
);
2252 JSObjectRef
SyntaxError(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("SyntaxError"))));
2253 CYSetProperty(context
, cy
, CYJSString("SyntaxError"), SyntaxError
);
2256 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
2257 CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
2259 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
2260 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
2261 CYSetProperty(context
, cycript
, CYJSString("compile"), &Cycript_compile_callAsFunction
);
2262 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
2264 JSObjectRef
CString(JSObjectMakeConstructor(context
, CString_
, &CString_new
));
2265 CYSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, CString
, prototype_s
)), String_prototype
);
2266 CYSetProperty(context
, cycript
, CYJSString("CString"), CString
);
2268 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
2269 CYSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
2270 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
2272 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
2273 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
2275 JSObjectRef
modules(JSObjectMake(context
, NULL
, NULL
));
2276 CYSetProperty(context
, cy
, CYJSString("modules"), modules
);
2278 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
2279 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
2281 JSObjectRef
cache(JSObjectMake(context
, NULL
, NULL
));
2282 CYSetProperty(context
, cy
, CYJSString("cache"), cache
);
2283 CYSetPrototype(context
, cache
, all
);
2285 JSObjectRef
alls(_jsccall(JSObjectCallAsConstructor
, context
, Array
, 0, NULL
));
2286 CYSetProperty(context
, cycript
, CYJSString("alls"), alls
);
2289 JSObjectRef
last(NULL
), curr(global
);
2291 goto next
; for (JSValueRef next
;;) {
2292 if (JSValueIsNull(context
, next
))
2295 curr
= CYCastJSObject(context
, next
);
2297 next
= JSObjectGetPrototype(context
, curr
);
2300 CYSetPrototype(context
, last
, cache
);
2303 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
2304 CYSetProperty(context
, cy
, CYJSString("System"), System
);
2306 CYSetProperty(context
, global
, CYJSString("require"), &require_callAsFunction
, kJSPropertyAttributeDontEnum
);
2308 CYSetProperty(context
, global
, CYJSString("system"), System
);
2309 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
2310 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
2312 CYSetProperty(context
, global
, CYJSString("global"), global
);
2315 if (&JSWeakObjectMapCreate
!= NULL
) {
2316 JSWeakObjectMapRef
weak(JSWeakObjectMapCreate(context
, NULL
, &CYDestroyWeak
));
2317 CYSetProperty(context
, cy
, weak_s
, CYCastJSValue(context
, reinterpret_cast<uintptr_t>(weak
)));
2321 CYSetProperty(context
, cache
, CYJSString("dlerror"), CYMakeFunctor(context
, "dlerror", "*"), kJSPropertyAttributeDontEnum
);
2322 CYSetProperty(context
, cache
, CYJSString("RTLD_DEFAULT"), CYCastJSValue(context
, reinterpret_cast<intptr_t>(RTLD_DEFAULT
)), kJSPropertyAttributeDontEnum
);
2323 CYSetProperty(context
, cache
, CYJSString("dlsym"), CYMakeFunctor(context
, "dlsym", "^v^v*"), kJSPropertyAttributeDontEnum
);
2325 CYSetProperty(context
, cache
, CYJSString("NULL"), CYJSNull(context
), kJSPropertyAttributeDontEnum
);
2327 CYSetProperty(context
, cache
, CYJSString("bool"), CYMakeType(context
, sig::boolean_P
), kJSPropertyAttributeDontEnum
);
2328 CYSetProperty(context
, cache
, CYJSString("char"), CYMakeType(context
, sig::char_P
), kJSPropertyAttributeDontEnum
);
2329 CYSetProperty(context
, cache
, CYJSString("short"), CYMakeType(context
, sig::short_P
), kJSPropertyAttributeDontEnum
);
2330 CYSetProperty(context
, cache
, CYJSString("int"), CYMakeType(context
, sig::int_P
), kJSPropertyAttributeDontEnum
);
2331 CYSetProperty(context
, cache
, CYJSString("long"), CYMakeType(context
, sig::long_P
), kJSPropertyAttributeDontEnum
);
2332 CYSetProperty(context
, cache
, CYJSString("float"), CYMakeType(context
, sig::float_P
), kJSPropertyAttributeDontEnum
);
2333 CYSetProperty(context
, cache
, CYJSString("double"), CYMakeType(context
, sig::double_P
), kJSPropertyAttributeDontEnum
);
2335 for (CYHook
*hook
: GetHooks())
2336 if (hook
->SetupContext
!= NULL
)
2337 (*hook
->SetupContext
)(context
);
2339 CYArrayPush(context
, alls
, cycript
);
2341 CYRunScript(context
, "/libcycript.cy");
2344 static JSGlobalContextRef context_
;
2346 _visible JSGlobalContextRef
CYGetJSContext() {
2347 CYInitializeDynamic();
2349 if (context_
== NULL
) {
2350 context_
= JSGlobalContextCreate(Global_
);
2351 CYSetupContext(context_
);
2357 _visible
void CYDestroyContext() {
2358 if (context_
== NULL
)
2360 JSGlobalContextRelease(context_
);