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
, JSValueRef value
) {
376 JSValueRef arguments
[1];
377 arguments
[0] = value
;
378 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
379 _jsccall(JSObjectCallAsFunction
, context
, CYCastJSObject(context
, CYGetProperty(context
, Array
, push_s
)), array
, 1, arguments
);
382 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
389 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, arguments
[0])));
390 fwrite(string
.data
, string
.size
, 1, file
);
394 return CYJSUndefined(context
);
397 static void (*JSSynchronousGarbageCollectForDebugging$
)(JSContextRef
);
399 _visible
void CYGarbageCollect(JSContextRef context
) {
400 (JSSynchronousGarbageCollectForDebugging$
?: &JSGarbageCollect
)(context
);
403 static JSValueRef
Cycript_compile_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
405 CYUTF8String
before(CYPoolUTF8String(pool
, context
, CYJSString(context
, arguments
[0])));
406 std::stringbuf
value(std::string(before
.data
, before
.size
));
407 CYUTF8String
after(CYPoolCode(pool
, value
));
408 return CYCastJSValue(context
, CYJSString(after
));
409 } CYCatch_(NULL
, "SyntaxError") }
411 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
412 CYGarbageCollect(context
);
413 return CYJSUndefined(context
);
416 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> &objects
, JSValueRef
*exception
) { CYTry
{
417 switch (JSType type
= JSValueGetType(context
, value
)) {
418 case kJSTypeUndefined
:
423 return CYCastBool(context
, value
) ? "true" : "false";
425 case kJSTypeNumber
: {
426 std::ostringstream str
;
427 CYNumerify(str
, CYCastDouble(context
, value
));
428 std::string
value(str
.str());
429 return pool
.strmemdup(value
.c_str(), value
.size());
432 case kJSTypeString
: {
433 std::ostringstream str
;
434 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
435 CYStringify(str
, string
.data
, string
.size
);
436 std::string
value(str
.str());
437 return pool
.strmemdup(value
.c_str(), value
.size());
441 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
, objects
);
443 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
447 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> &objects
) {
448 return _jsccall(CYPoolCCYON
, pool
, context
, value
, objects
);
451 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> *objects
) {
453 return CYPoolCCYON(pool
, context
, value
, *objects
);
455 std::set
<void *> objects
;
456 return CYPoolCCYON(pool
, context
, value
, objects
);
460 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSObjectRef object
, std::set
<void *> &objects
) {
461 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
462 if (CYIsCallable(context
, toCYON
)) {
463 // XXX: this needs to be abstracted behind some kind of function
464 JSValueRef arguments
[1] = {CYCastJSValue(context
, reinterpret_cast<uintptr_t>(&objects
))};
465 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 1, arguments
));
466 _assert(value
!= NULL
);
467 return CYPoolCString(pool
, context
, value
);
470 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
471 if (CYIsCallable(context
, toJSON
)) {
472 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
473 return _jsccall(CYPoolCCYON
, pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), objects
);
476 if (JSObjectIsFunction(context
, object
)) {
477 JSValueRef
toString(CYGetProperty(context
, object
, toString_s
));
478 if (CYIsCallable(context
, toString
)) {
479 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
480 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toString
, object
, 1, arguments
));
481 _assert(value
!= NULL
);
482 return CYPoolCString(pool
, context
, value
);
486 _assert(objects
.insert(object
).second
);
488 std::ostringstream str
;
492 // XXX: this is, sadly, going to leak
493 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
497 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
503 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
504 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
509 CYStringify(str
, string
.data
, string
.size
);
514 JSValueRef
value(CYGetProperty(context
, object
, name
));
515 str
<< CYPoolCCYON(pool
, context
, value
, objects
);
516 } catch (const CYException
&error
) {
521 JSPropertyNameArrayRelease(names
);
525 std::string
string(str
.str());
526 return pool
.strmemdup(string
.c_str(), string
.size());
529 std::set
<void *> *CYCastObjects(JSContextRef context
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
532 return CYCastPointer
<std::set
<void *> *>(context
, arguments
[0]);
535 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
536 std::set
<void *> *objects(CYCastObjects(context
, _this
, count
, arguments
));
537 // XXX: this is horribly inefficient
538 std::set
<void *> backup
;
543 std::ostringstream str
;
547 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
550 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
557 JSValueRef
value(CYGetProperty(context
, _this
, index
));
558 if (!JSValueIsUndefined(context
, value
))
559 str
<< CYPoolCCYON(pool
, context
, value
, *objects
);
564 } catch (const CYException
&error
) {
571 std::string
value(str
.str());
572 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
575 static JSValueRef
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
577 std::ostringstream str
;
579 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
)));
580 CYStringify(str
, string
.data
, string
.size
);
582 std::string
value(str
.str());
583 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
586 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
587 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
588 return JSObjectMake(context
, Pointer_
, internal
);
591 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, const char *encoding
, JSObjectRef owner
) {
592 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, encoding
));
593 return JSObjectMake(context
, Pointer_
, internal
);
596 JSObjectRef
CYMakeCString(JSContextRef context
, char *pointer
, JSObjectRef owner
) {
597 CString
*internal(new CString(pointer
, context
, owner
));
598 return JSObjectMake(context
, CString_
, internal
);
601 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const sig::Signature
&signature
) {
602 return JSObjectMake(context
, Functor_
, new cy::Functor(signature
, function
));
605 static JSObjectRef
CYMakeFunctor(JSContextRef context
, const char *symbol
, const char *encoding
) {
606 void (*function
)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol
)));
607 if (function
== NULL
)
610 cy::Functor
*internal(new cy::Functor(encoding
, function
));
612 return JSObjectMake(context
, Functor_
, internal
);
615 static bool CYGetOffset(CYPool
&pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
616 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
619 void *CYCastPointer_(JSContextRef context
, JSValueRef value
, bool *guess
) {
622 else switch (JSValueGetType(context
, value
)) {
625 case kJSTypeObject
: {
626 JSObjectRef
object((JSObjectRef
) value
);
627 if (JSValueIsObjectOfClass(context
, value
, Pointer_
)) {
628 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
629 return internal
->value_
;
631 JSValueRef
toPointer(CYGetProperty(context
, object
, toPointer_s
));
632 if (CYIsCallable(context
, toPointer
)) {
633 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
));
634 _assert(value
!= NULL
);
635 return CYCastPointer_(context
, value
, guess
);
641 double number(CYCastDouble(context
, value
));
642 if (!std::isnan(number
))
643 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
645 throw CYJSError(context
, "cannot convert value to pointer");
653 void CYPoolFFI(CYPool
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
654 switch (type
->primitive
) {
656 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
659 #define CYPoolFFI_(primitive, native) \
660 case sig::primitive ## _P: \
661 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
664 CYPoolFFI_(uchar
, unsigned char)
665 CYPoolFFI_(char, char)
666 CYPoolFFI_(ushort
, unsigned short)
667 CYPoolFFI_(short, short)
668 CYPoolFFI_(ulong
, unsigned long)
669 CYPoolFFI_(long, long)
670 CYPoolFFI_(uint
, unsigned int)
672 CYPoolFFI_(ulonglong
, unsigned long long)
673 CYPoolFFI_(longlong
, long long)
674 CYPoolFFI_(float, float)
675 CYPoolFFI_(double, double)
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
.data
.size
; ++index
) {
681 ffi_type
*field(ffi
->elements
[index
]);
684 if (aggregate
== NULL
)
687 rhs
= CYGetProperty(context
, aggregate
, index
);
688 if (JSValueIsUndefined(context
, rhs
))
689 throw CYJSError(context
, "unable to extract array value");
692 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
699 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
702 case sig::string_P
: {
704 *reinterpret_cast<const char **>(data
) = CYCastPointer
<const char *>(context
, value
, &guess
);
705 if (guess
&& pool
!= NULL
)
706 *reinterpret_cast<const char **>(data
) = CYPoolCString(*pool
, context
, value
);
709 case sig::struct_P
: {
710 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
711 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
712 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
713 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
714 ffi_type
*field(ffi
->elements
[index
]);
717 if (aggregate
== NULL
)
720 rhs
= CYGetProperty(context
, aggregate
, index
);
721 if (JSValueIsUndefined(context
, rhs
)) {
722 if (element
->name
!= NULL
)
723 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
726 if (JSValueIsUndefined(context
, rhs
)) undefined
:
727 throw CYJSError(context
, "unable to extract structure value");
731 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
741 for (CYHook
*hook
: GetHooks())
742 if (hook
->PoolFFI
!= NULL
)
743 if ((*hook
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
746 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
750 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
751 switch (type
->primitive
) {
753 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
755 #define CYFromFFI_(primitive, native) \
756 case sig::primitive ## _P: \
757 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
759 CYFromFFI_(uchar, unsigned char)
760 CYFromFFI_(char, char)
761 CYFromFFI_(ushort
, unsigned short)
762 CYFromFFI_(short, short)
763 CYFromFFI_(ulong
, unsigned long)
764 CYFromFFI_(long, long)
765 CYFromFFI_(uint
, unsigned int)
767 CYFromFFI_(ulonglong
, unsigned long long)
768 CYFromFFI_(longlong
, long long)
769 CYFromFFI_(float, float)
770 CYFromFFI_(double, double)
773 if (void *pointer
= data
)
774 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
778 if (void *pointer
= *reinterpret_cast<void **>(data
))
779 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
783 if (char *pointer
= *reinterpret_cast<char **>(data
))
784 return CYMakeCString(context
, pointer
, owner
);
788 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
790 return CYJSUndefined(context
);
793 return CYJSNull(context
);
795 for (CYHook
*hook
: GetHooks())
796 if (hook
->FromFFI
!= NULL
)
797 if (JSValueRef value
= (*hook
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
800 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
804 void CYExecuteClosure(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
805 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
807 JSContextRef
context(internal
->context_
);
809 size_t count(internal
->cif_
.nargs
);
810 JSValueRef values
[count
];
812 for (size_t index(0); index
!= count
; ++index
)
813 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
815 JSValueRef
value(internal
->adapter_(context
, count
, values
, internal
->function_
));
816 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
819 static JSValueRef
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
) {
820 return CYCallAsFunction(context
, function
, NULL
, count
, values
);
823 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
, JSValueRef (*adapter
)(JSContextRef
, size_t, JSValueRef
[], JSObjectRef
)) {
824 // XXX: in case of exceptions this will leak
825 // XXX: in point of fact, this may /need/ to leak :(
826 Closure_privateData
*internal(new Closure_privateData(context
, function
, adapter
, signature
));
828 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
830 ffi_closure
*writable(reinterpret_cast<ffi_closure
*>(ffi_closure_alloc(sizeof(ffi_closure
), &executable
)));
832 ffi_status
status(ffi_prep_closure_loc(writable
, &internal
->cif_
, &CYExecuteClosure
, internal
, executable
));
833 _assert(status
== FFI_OK
);
835 internal
->value_
= executable
;
837 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
838 NULL
, sizeof(ffi_closure
),
839 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
843 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, &CYExecuteClosure
, internal
));
844 _assert(status
== FFI_OK
);
846 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
848 internal
->value_
= closure
;
854 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
) {
855 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, signature
, &FunctionAdapter_
));
856 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
857 // XXX: see above notes about needing to leak
858 JSValueProtect(CYGetJSContext(context
), object
);
862 JSValueRef
CYGetCachedValue(JSContextRef context
, JSStringRef name
) {
863 return CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
);
866 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
867 return CYCastJSObject(context
, CYGetCachedValue(context
, name
));
870 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const sig::Signature
&signature
) {
871 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
873 bool function(_jsccall(JSValueIsInstanceOfConstructor
, context
, value
, Function
));
875 JSObjectRef
function(CYCastJSObject(context
, value
));
876 return CYMakeFunctor(context
, function
, signature
);
878 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
879 return CYMakeFunctor(context
, function
, signature
);
883 static JSValueRef
CString_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
885 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(object
)));
886 char *string(static_cast<char *>(internal
->value_
));
889 if (!CYGetOffset(pool
, context
, property
, offset
))
892 return CYCastJSValue(context
, CYJSString(CYUTF8String(&string
[offset
], 1)));
895 static bool CString_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
897 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(object
)));
898 char *string(static_cast<char *>(internal
->value_
));
901 if (!CYGetOffset(pool
, context
, property
, offset
))
904 const char *data(CYPoolCString(pool
, context
, value
));
905 string
[offset
] = *data
;
909 static bool Index_(CYPool
&pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
910 Type_privateData
*typical(internal
->type_
);
911 sig::Type
*type(typical
->type_
);
915 const char *name(CYPoolCString(pool
, context
, property
));
916 size_t length(strlen(name
));
917 double number(CYCastDouble(name
, length
));
919 size_t count(type
->data
.signature
.count
);
921 if (std::isnan(number
)) {
922 if (property
== NULL
)
925 sig::Element
*elements(type
->data
.signature
.elements
);
927 for (size_t local(0); local
!= count
; ++local
) {
928 sig::Element
*element(&elements
[local
]);
929 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
937 index
= static_cast<ssize_t
>(number
);
938 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
943 ffi_type
**elements(typical
->GetFFI()->elements
);
946 for (ssize_t
local(0); local
!= index
; ++local
) {
947 offset
+= elements
[local
]->size
;
948 CYAlign(offset
, elements
[local
+ 1]->alignment
);
951 base
= reinterpret_cast<uint8_t *>(internal
->value_
) + offset
;
955 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
957 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
959 if (JSStringIsEqual(property
, length_s
))
960 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
962 Type_privateData
*typical(internal
->type_
);
963 if (typical
->type_
== NULL
)
965 sig::Type
&type(*typical
->type_
);
968 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
970 else if (!CYGetOffset(pool
, context
, property
, offset
))
973 if (type
.primitive
== sig::function_P
)
974 return CYMakeFunctor(context
, reinterpret_cast<void (*)()>(internal
->value_
), type
.data
.signature
);
976 ffi_type
*ffi(typical
->GetFFI());
978 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
979 base
+= ffi
->size
* offset
;
981 JSObjectRef
owner(internal
->GetOwner() ?: object
);
982 return CYFromFFI(context
, &type
, ffi
, base
, false, owner
);
985 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
987 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
988 Type_privateData
*typical(internal
->type_
);
990 if (typical
->type_
== NULL
)
994 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
996 else if (!CYGetOffset(pool
, context
, property
, offset
))
999 ffi_type
*ffi(typical
->GetFFI());
1001 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
1002 base
+= ffi
->size
* offset
;
1004 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
1008 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1009 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
1010 Type_privateData
*typical(internal
->type_
);
1011 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
1014 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1016 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1017 Type_privateData
*typical(internal
->type_
);
1022 if (!Index_(pool
, context
, internal
, property
, index
, base
))
1025 JSObjectRef
owner(internal
->GetOwner() ?: object
);
1027 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
1030 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1032 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1033 Type_privateData
*typical(internal
->type_
);
1038 if (!Index_(pool
, context
, internal
, property
, index
, base
))
1041 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
1045 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1046 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1047 Type_privateData
*typical(internal
->type_
);
1048 sig::Type
*type(typical
->type_
);
1053 size_t count(type
->data
.signature
.count
);
1054 sig::Element
*elements(type
->data
.signature
.elements
);
1058 for (size_t index(0); index
!= count
; ++index
) {
1060 name
= elements
[index
].name
;
1063 sprintf(number
, "%zu", index
);
1067 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
1071 void CYCallFunction(CYPool
&pool
, JSContextRef context
, ffi_cif
*cif
, void (*function
)(), void *value
, void **values
) {
1072 ffi_call(cif
, function
, value
, values
);
1075 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
)()) {
1076 if (setups
+ count
!= signature
->count
- 1)
1077 throw CYJSError(context
, "incorrect number of arguments to ffi function");
1079 size_t size(setups
+ count
);
1081 memcpy(values
, setup
, sizeof(void *) * setups
);
1083 for (size_t index(setups
); index
!= size
; ++index
) {
1084 sig::Element
*element(&signature
->elements
[index
+ 1]);
1085 ffi_type
*ffi(cif
->arg_types
[index
]);
1087 values
[index
] = new(pool
) uint8_t[ffi
->size
];
1088 CYPoolFFI(&pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
1091 uint8_t value
[cif
->rtype
->size
];
1093 void (*call
)(CYPool
&, JSContextRef
, ffi_cif
*, void (*)(), void *, void **) = &CYCallFunction
;
1094 // XXX: this only supports one hook, but it is a bad idea anyway
1095 for (CYHook
*hook
: GetHooks())
1096 if (hook
->CallFunction
!= NULL
)
1097 call
= hook
->CallFunction
;
1099 call(pool
, context
, cif
, function
, value
, values
);
1100 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
1103 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1105 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1106 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
1109 static JSValueRef
Pointer_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1110 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1111 if (internal
->type_
->type_
->primitive
!= sig::function_P
)
1112 throw CYJSError(context
, "cannot call a pointer to non-function");
1113 JSObjectRef
functor(CYCastJSObject(context
, CYGetProperty(context
, object
, cyi_s
)));
1114 return CYCallAsFunction(context
, functor
, _this
, count
, arguments
);
1117 JSObjectRef
CYMakeType(JSContextRef context
, const char *encoding
) {
1118 Type_privateData
*internal(new Type_privateData(encoding
));
1119 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1122 JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
1123 Type_privateData
*internal(new Type_privateData(type
));
1124 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1127 JSObjectRef
CYMakeType(JSContextRef context
, sig::Signature
*signature
) {
1134 type
.primitive
= sig::function_P
;
1135 sig::Copy(pool
, type
.data
.signature
, *signature
);
1137 return CYMakeType(context
, &type
);
1140 extern "C" bool CYBridgeHash(CYPool
&pool
, CYUTF8String name
, const char *&code
, unsigned &flags
) {
1141 sqlite3_stmt
*statement
;
1143 _sqlcall(sqlite3_prepare(database_
,
1145 "\"cache\".\"code\", "
1146 "\"cache\".\"flags\" "
1149 " \"cache\".\"system\" & " CY_SYSTEM
" == " CY_SYSTEM
" and"
1150 " \"cache\".\"name\" = ?"
1152 , -1, &statement
, NULL
));
1154 _sqlcall(sqlite3_bind_text(statement
, 1, name
.data
, name
.size
, SQLITE_STATIC
));
1157 if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
)
1161 code
= sqlite3_column_pooled(pool
, statement
, 0);
1162 flags
= sqlite3_column_int(statement
, 1);
1165 _sqlcall(sqlite3_finalize(statement
));
1169 static bool All_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
1170 if (JSStringIsEqualToUTF8CString(property
, "errno"))
1173 JSObjectRef
global(CYGetGlobalObject(context
));
1174 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1175 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1177 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1178 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1179 if (CYHasProperty(context
, space
, property
))
1185 if (CYBridgeHash(pool
, CYPoolUTF8String(pool
, context
, property
), code
, flags
))
1191 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1192 if (JSStringIsEqualToUTF8CString(property
, "errno"))
1193 return CYCastJSValue(context
, errno
);
1195 JSObjectRef
global(CYGetGlobalObject(context
));
1196 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1197 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1199 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1200 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1201 if (JSValueRef value
= CYGetProperty(context
, space
, property
))
1202 if (!JSValueIsUndefined(context
, value
))
1208 if (CYBridgeHash(pool
, CYPoolUTF8String(pool
, context
, property
), code
, flags
)) {
1209 JSValueRef
result(_jsccall(JSEvaluateScript
, context
, CYJSString(CYPoolCode(pool
, code
)), NULL
, NULL
, 0));
1212 JSObjectRef
cache(CYGetCachedObject(context
, CYJSString("cache")));
1213 CYSetProperty(context
, cache
, property
, result
);
1222 static JSValueRef
All_complete_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1223 _assert(count
== 1);
1225 CYUTF8String
prefix(CYPoolUTF8String(pool
, context
, CYJSString(context
, arguments
[0])));
1227 std::vector
<JSValueRef
> values
;
1229 sqlite3_stmt
*statement
;
1231 _sqlcall(sqlite3_prepare(database_
,
1233 "\"cache\".\"name\" "
1236 " \"cache\".\"system\" & " CY_SYSTEM
" == " CY_SYSTEM
" and"
1237 " \"cache\".\"name\" like ? || '%'"
1238 , -1, &statement
, NULL
));
1240 _sqlcall(sqlite3_bind_text(statement
, 1, prefix
.data
, prefix
.size
, SQLITE_STATIC
));
1242 while (_sqlcall(sqlite3_step(statement
)) != SQLITE_DONE
)
1243 values
.push_back(CYCastJSValue(context
, CYJSString(sqlite3_column_string(statement
, 0))));
1245 _sqlcall(sqlite3_finalize(statement
));
1247 return CYObjectMakeArray(context
, values
.size(), values
.data());
1250 static void All_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1251 JSObjectRef
global(CYGetGlobalObject(context
));
1252 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1253 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1255 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1256 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1))) {
1257 JSPropertyNameArrayRef
subset(JSObjectCopyPropertyNames(context
, space
));
1258 for (size_t index(0), count(JSPropertyNameArrayGetCount(subset
)); index
!= count
; ++index
)
1259 JSPropertyNameAccumulatorAddName(names
, JSPropertyNameArrayGetNameAtIndex(subset
, index
));
1260 JSPropertyNameArrayRelease(subset
);
1264 static JSObjectRef
CString_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1266 throw CYJSError(context
, "incorrect number of arguments to CString constructor");
1267 char *value(CYCastPointer
<char *>(context
, arguments
[0]));
1268 return CYMakeCString(context
, value
, NULL
);
1271 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1273 throw CYJSError(context
, "incorrect number of arguments to Pointer constructor");
1277 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1278 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1280 sig::Signature signature
;
1281 sig::Parse(pool
, &signature
, type
, &Structor_
);
1283 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1286 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1290 } else if (count
== 1) {
1291 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1292 return CYMakeType(context
, type
);
1293 } else if (count
== 2) {
1294 JSObjectRef
types(CYCastJSObject(context
, arguments
[0]));
1295 size_t count(CYArrayLength(context
, types
));
1297 JSObjectRef
names(CYCastJSObject(context
, arguments
[1]));
1303 type
.primitive
= sig::struct_P
;
1304 type
.data
.signature
.elements
= new(pool
) sig::Element
[count
];
1305 type
.data
.signature
.count
= count
;
1307 for (size_t i(0); i
!= count
; ++i
) {
1308 sig::Element
&element(type
.data
.signature
.elements
[i
]);
1309 element
.offset
= _not(size_t);
1311 JSValueRef
name(CYArrayGet(context
, names
, i
));
1312 if (JSValueIsUndefined(context
, name
))
1313 element
.name
= NULL
;
1315 element
.name
= CYPoolCString(pool
, context
, name
);
1317 JSObjectRef
object(CYCastJSObject(context
, CYArrayGet(context
, types
, i
)));
1318 _assert(JSValueIsObjectOfClass(context
, object
, Type_privateData::Class_
));
1319 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1320 element
.type
= internal
->type_
;
1323 return CYMakeType(context
, &type
);
1325 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1329 static JSValueRef Type_callAsFunction_$
With(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], sig::Primitive primitive
, JSValueRef
*exception
) { CYTry
{
1330 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1338 type
.primitive
= primitive
;
1339 type
.data
.signature
.elements
= new(pool
) sig::Element
[1 + count
];
1340 type
.data
.signature
.count
= 1 + count
;
1342 type
.data
.signature
.elements
[0].name
= NULL
;
1343 type
.data
.signature
.elements
[0].type
= internal
->type_
;
1344 type
.data
.signature
.elements
[0].offset
= _not(size_t);
1346 for (size_t i(0); i
!= count
; ++i
) {
1347 sig::Element
&element(type
.data
.signature
.elements
[i
+ 1]);
1348 element
.name
= NULL
;
1349 element
.offset
= _not(size_t);
1351 JSObjectRef
object(CYCastJSObject(context
, arguments
[i
]));
1352 _assert(JSValueIsObjectOfClass(context
, object
, Type_privateData::Class_
));
1353 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1355 element
.type
= internal
->type_
;
1358 return CYMakeType(context
, &type
);
1361 static JSValueRef
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1363 throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf");
1364 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1367 size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0])));
1368 if (index
== _not(size_t))
1369 throw CYJSError(context
, "invalid array size used with Type.arrayOf");
1375 type
.primitive
= sig::array_P
;
1376 type
.data
.data
.type
= internal
->type_
;
1377 type
.data
.data
.size
= index
;
1379 return CYMakeType(context
, &type
);
1382 static JSValueRef
Type_callAsFunction_blockWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1383 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::block_P
, exception
);
1386 static JSValueRef
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1388 throw CYJSError(context
, "incorrect number of arguments to Type.constant");
1389 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1391 sig::Type
type(*internal
->type_
);
1392 type
.flags
|= JOC_TYPE_CONST
;
1393 return CYMakeType(context
, &type
);
1396 static JSValueRef
Type_callAsFunction_long(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1398 throw CYJSError(context
, "incorrect number of arguments to Type.long");
1399 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1401 sig::Type
type(*internal
->type_
);
1403 switch (type
.primitive
) {
1404 case sig::short_P
: type
.primitive
= sig::int_P
; break;
1405 case sig::int_P
: type
.primitive
= sig::long_P
; break;
1406 case sig::long_P
: type
.primitive
= sig::longlong_P
; break;
1407 default: throw CYJSError(context
, "invalid type argument to Type.long");
1410 return CYMakeType(context
, &type
);
1413 static JSValueRef
Type_callAsFunction_short(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1415 throw CYJSError(context
, "incorrect number of arguments to Type.short");
1416 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1418 sig::Type
type(*internal
->type_
);
1420 switch (type
.primitive
) {
1421 case sig::int_P
: type
.primitive
= sig::short_P
; break;
1422 case sig::long_P
: type
.primitive
= sig::int_P
; break;
1423 case sig::longlong_P
: type
.primitive
= sig::long_P
; break;
1424 default: throw CYJSError(context
, "invalid type argument to Type.short");
1427 return CYMakeType(context
, &type
);
1430 static JSValueRef
Type_callAsFunction_signed(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1432 throw CYJSError(context
, "incorrect number of arguments to Type.signed");
1433 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1435 sig::Type
type(*internal
->type_
);
1437 switch (type
.primitive
) {
1438 case sig::char_P
: case sig::uchar_P
: type
.primitive
= sig::char_P
; break;
1439 case sig::short_P
: case sig::ushort_P
: type
.primitive
= sig::short_P
; break;
1440 case sig::int_P
: case sig::uint_P
: type
.primitive
= sig::int_P
; break;
1441 case sig::long_P
: case sig::ulong_P
: type
.primitive
= sig::long_P
; break;
1442 case sig::longlong_P
: case sig::ulonglong_P
: type
.primitive
= sig::longlong_P
; break;
1443 default: throw CYJSError(context
, "invalid type argument to Type.signed");
1446 return CYMakeType(context
, &type
);
1449 static JSValueRef
Type_callAsFunction_unsigned(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1451 throw CYJSError(context
, "incorrect number of arguments to Type.unsigned");
1452 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1454 sig::Type
type(*internal
->type_
);
1456 switch (type
.primitive
) {
1457 case sig::char_P
: case sig::uchar_P
: type
.primitive
= sig::uchar_P
; break;
1458 case sig::short_P
: case sig::ushort_P
: type
.primitive
= sig::ushort_P
; break;
1459 case sig::int_P
: case sig::uint_P
: type
.primitive
= sig::uint_P
; break;
1460 case sig::long_P
: case sig::ulong_P
: type
.primitive
= sig::ulong_P
; break;
1461 case sig::longlong_P
: case sig::ulonglong_P
: type
.primitive
= sig::ulonglong_P
; break;
1462 default: throw CYJSError(context
, "invalid type argument to Type.unsigned");
1465 return CYMakeType(context
, &type
);
1468 static JSValueRef
Type_callAsFunction_functionWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1469 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::function_P
, exception
);
1472 static JSValueRef
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1474 throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo");
1475 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1480 if (internal
->type_
->primitive
== sig::char_P
) {
1481 type
.flags
= internal
->type_
->flags
;
1482 type
.primitive
= sig::string_P
;
1483 type
.data
.data
.type
= NULL
;
1484 type
.data
.data
.size
= 0;
1487 type
.primitive
= sig::pointer_P
;
1488 type
.data
.data
.type
= internal
->type_
;
1489 type
.data
.data
.size
= 0;
1492 return CYMakeType(context
, &type
);
1495 static JSValueRef
Type_callAsFunction_withName(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1497 throw CYJSError(context
, "incorrect number of arguments to Type.withName");
1498 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1501 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1503 sig::Type
type(*internal
->type_
);
1505 return CYMakeType(context
, &type
);
1508 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1510 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1511 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1513 if (internal
->type_
->primitive
== sig::function_P
)
1514 return CYMakeFunctor(context
, arguments
[0], internal
->type_
->data
.signature
);
1516 sig::Type
*type(internal
->type_
);
1517 ffi_type
*ffi(internal
->GetFFI());
1519 uint8_t value
[ffi
->size
];
1521 CYPoolFFI(&pool
, context
, type
, ffi
, value
, arguments
[0]);
1522 return CYFromFFI(context
, type
, ffi
, value
);
1525 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1527 throw CYJSError(context
, "incorrect number of arguments to Type allocator");
1528 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1530 sig::Type
*type(internal
->type_
);
1533 if (type
->primitive
!= sig::array_P
)
1534 length
= _not(size_t);
1536 length
= type
->data
.data
.size
;
1537 type
= type
->data
.data
.type
;
1540 JSObjectRef
pointer(CYMakePointer(context
, NULL
, length
, type
, NULL
, NULL
));
1541 Pointer
*value(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(pointer
)));
1542 value
->value_
= value
->pool_
->malloc
<void>(internal
->GetFFI()->size
);
1543 memset(value
->value_
, 0, internal
->GetFFI()->size
);
1547 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1549 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1551 const char *encoding(CYPoolCString(pool
, context
, arguments
[1]));
1552 sig::Signature signature
;
1553 sig::Parse(pool
, &signature
, encoding
, &Structor_
);
1554 return CYMakeFunctor(context
, arguments
[0], signature
);
1557 static JSValueRef
CString_callAsFunction_toPointer(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1558 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(_this
)));
1564 type
.primitive
= sig::char_P
;
1565 type
.data
.data
.type
= NULL
;
1566 type
.data
.data
.size
= 0;
1568 return CYMakePointer(context
, internal
->value_
, _not(size_t), &type
, NULL
, NULL
);
1571 static JSValueRef Functor_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1573 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(_this
)));
1579 type
.primitive
= sig::function_P
;
1580 sig::Copy(pool
, type
.data
.signature
, internal
->signature_
);
1582 return CYMakePointer(context
, internal
->value_
, _not(size_t), &type
, NULL
, NULL
);
1585 static JSValueRef
Pointer_callAsFunction_toPointer(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1589 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1590 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1591 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1594 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1595 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1598 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1599 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1600 std::ostringstream str
;
1602 if (internal
->value_
== NULL
)
1604 else if (dladdr(internal
->value_
, &info
) == 0)
1605 str
<< internal
->value_
;
1607 str
<< info
.dli_sname
;
1608 off_t
offset(static_cast<char *>(internal
->value_
) - static_cast<char *>(info
.dli_saddr
));
1610 str
<< "+0x" << std::hex
<< offset
;
1612 std::string
value(str
.str());
1613 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
1616 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1617 std::set
<void *> *objects(CYCastObjects(context
, _this
, count
, arguments
));
1619 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1620 if (internal
->length_
!= _not(size_t)) {
1621 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
1622 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1623 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1624 } else if (internal
->type_
->type_
== NULL
) pointer
: {
1626 std::ostringstream str
;
1632 type
.primitive
= sig::pointer_P
;
1633 type
.data
.data
.type
= internal
->type_
->type_
;
1634 type
.data
.data
.size
= 0;
1637 CYOutput
output(*str
.rdbuf(), options
);
1638 (new(pool
) CYTypeExpression(Decode(pool
, &type
)))->Output(output
, CYNoFlags
);
1640 str
<< "(" << internal
->value_
<< ")";
1641 std::string
value(str
.str());
1642 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
1644 JSValueRef
value(CYGetProperty(context
, _this
, cyi_s
));
1645 if (JSValueIsUndefined(context
, value
))
1648 return CYCastJSValue(context
, pool
.strcat("&", CYPoolCCYON(pool
, context
, value
, objects
), NULL
));
1649 } catch (const CYException
&e
) {
1654 static JSValueRef
CString_getProperty_length(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1655 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(object
)));
1656 char *string(static_cast<char *>(internal
->value_
));
1657 return CYCastJSValue(context
, strlen(string
));
1660 static JSValueRef
CString_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1665 type
.primitive
= sig::char_P
;
1666 type
.data
.data
.type
= NULL
;
1667 type
.data
.data
.size
= 0;
1669 return CYMakeType(context
, &type
);
1672 static JSValueRef
Pointer_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1673 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1674 return CYMakeType(context
, internal
->type_
->type_
);
1677 static JSValueRef
CString_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1678 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1679 const char *string(static_cast<const char *>(internal
->value_
));
1680 std::ostringstream str
;
1682 CYStringify(str
, string
, strlen(string
), true);
1683 std::string
value(str
.str());
1684 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
1687 static JSValueRef
CString_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1688 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1689 const char *string(static_cast<const char *>(internal
->value_
));
1690 return CYCastJSValue(context
, string
);
1693 static JSValueRef
Functor_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1694 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1695 return CYMakeType(context
, &internal
->signature_
);
1698 static JSValueRef
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1699 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1700 return CYCastJSValue(context
, internal
->GetFFI()->alignment
);
1703 static JSValueRef
Type_getProperty_name(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1704 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1705 return CYCastJSValue(context
, internal
->type_
->name
);
1708 static JSValueRef
Type_getProperty_size(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1709 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1710 return CYCastJSValue(context
, internal
->GetFFI()->size
);
1713 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1714 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1716 const char *type(sig::Unparse(pool
, internal
->type_
));
1717 return CYCastJSValue(context
, CYJSString(type
));
1720 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1721 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1725 CYOutput
output(out
, options
);
1726 (new(pool
) CYTypeExpression(Decode(pool
, internal
->type_
)))->Output(output
, CYNoFlags
);
1727 return CYCastJSValue(context
, CYJSString(out
.str().c_str()));
1730 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1731 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1734 static JSStaticFunction All_staticFunctions
[2] = {
1735 {"cy$complete", &All_complete_callAsFunction
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1739 static JSStaticFunction CString_staticFunctions
[6] = {
1740 {"toCYON", &CString_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1741 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1742 {"toPointer", &CString_callAsFunction_toPointer
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1743 {"toString", &CString_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1744 {"valueOf", &CString_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1748 static JSStaticValue CString_staticValues
[3] = {
1749 {"length", &CString_getProperty_length
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1750 {"type", &CString_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1751 {NULL
, NULL
, NULL
, 0}
1754 static JSStaticFunction Pointer_staticFunctions
[5] = {
1755 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1756 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1757 {"toPointer", &Pointer_callAsFunction_toPointer
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1758 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1762 static JSStaticValue Pointer_staticValues
[2] = {
1763 {"type", &Pointer_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1764 {NULL
, NULL
, NULL
, 0}
1767 static JSStaticFunction Struct_staticFunctions
[2] = {
1768 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1772 static JSStaticFunction Functor_staticFunctions
[5] = {
1773 {"$cya", &Functor_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1774 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1775 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1776 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1781 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1784 static JSStaticValue Functor_staticValues
[2] = {
1785 {"type", &Functor_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1786 {NULL
, NULL
, NULL
, 0}
1790 JSStaticValue
const * const Functor::StaticValues
= Functor_staticValues
;
1793 static JSStaticValue Type_staticValues
[4] = {
1794 {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1795 {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1796 {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1797 {NULL
, NULL
, NULL
, 0}
1800 static JSStaticFunction Type_staticFunctions
[14] = {
1801 {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1802 {"blockWith", &Type_callAsFunction_blockWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1803 {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1804 {"functionWith", &Type_callAsFunction_functionWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1805 {"long", &Type_callAsFunction_long
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1806 {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1807 {"short", &Type_callAsFunction_short
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1808 {"signed", &Type_callAsFunction_signed
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1809 {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1810 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1811 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1812 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1813 {"unsigned", &Type_callAsFunction_unsigned
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1817 _visible
void CYSetArgs(int argc
, const char *argv
[]) {
1818 JSContextRef
context(CYGetJSContext());
1819 JSValueRef args
[argc
];
1820 for (int i(0); i
!= argc
; ++i
)
1821 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1823 JSObjectRef
array(CYObjectMakeArray(context
, argc
, args
));
1824 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1825 CYSetProperty(context
, System
, CYJSString("args"), array
);
1828 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1829 return JSContextGetGlobalObject(context
);
1832 // XXX: this is neither exceptin safe nor even terribly sane
1833 class ExecutionHandle
{
1835 JSContextRef context_
;
1836 std::vector
<void *> handles_
;
1839 ExecutionHandle(JSContextRef context
) :
1842 handles_
.resize(GetHooks().size());
1843 for (size_t i(0); i
!= GetHooks().size(); ++i
) {
1844 CYHook
*hook(GetHooks()[i
]);
1845 if (hook
->ExecuteStart
!= NULL
)
1846 handles_
[i
] = (*hook
->ExecuteStart
)(context_
);
1852 ~ExecutionHandle() {
1853 for (size_t i(GetHooks().size()); i
!= 0; --i
) {
1854 CYHook
*hook(GetHooks()[i
-1]);
1855 if (hook
->ExecuteEnd
!= NULL
)
1856 (*hook
->ExecuteEnd
)(context_
, handles_
[i
-1]);
1861 static volatile bool cancel_
;
1863 static bool CYShouldTerminate(JSContextRef context
, void *arg
) {
1867 _visible
const char *CYExecute(JSContextRef context
, CYPool
&pool
, CYUTF8String code
) {
1868 ExecutionHandle
handle(context
);
1871 if (&JSContextGroupSetExecutionTimeLimit
!= NULL
)
1872 JSContextGroupSetExecutionTimeLimit(JSContextGetGroup(context
), 0.5, &CYShouldTerminate
, NULL
);
1875 JSValueRef
result(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0));
1876 if (JSValueIsUndefined(context
, result
))
1879 std::set
<void *> objects
;
1880 const char *json(_jsccall(CYPoolCCYON
, pool
, context
, result
, objects
));
1881 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1884 } catch (const CYException
&error
) {
1885 return pool
.strcat("throw ", error
.PoolCString(pool
), NULL
);
1889 _visible
void CYCancel() {
1893 static const char *CYPoolLibraryPath(CYPool
&pool
);
1895 static bool initialized_
= false;
1897 void CYInitializeDynamic() {
1899 initialized_
= true;
1903 const char *db(pool
.strcat(CYPoolLibraryPath(pool
), "/libcycript.db", NULL
));
1904 _sqlcall(sqlite3_open_v2(db
, &database_
, SQLITE_OPEN_READONLY
, NULL
));
1906 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1907 JSSynchronousGarbageCollectForDebugging$
= reinterpret_cast<void (*)(JSContextRef
)>(dlsym(RTLD_DEFAULT
, "JSSynchronousGarbageCollectForDebugging"));
1909 JSClassDefinition definition
;
1911 definition
= kJSClassDefinitionEmpty
;
1912 definition
.className
= "All";
1913 definition
.staticFunctions
= All_staticFunctions
;
1914 definition
.hasProperty
= &All_hasProperty
;
1915 definition
.getProperty
= &All_getProperty
;
1916 definition
.getPropertyNames
= &All_getPropertyNames
;
1917 All_
= JSClassCreate(&definition
);
1919 definition
= kJSClassDefinitionEmpty
;
1920 definition
.className
= "Context";
1921 definition
.finalize
= &CYFinalize
;
1922 Context_
= JSClassCreate(&definition
);
1924 definition
= kJSClassDefinitionEmpty
;
1925 definition
.className
= "CString";
1926 definition
.staticFunctions
= CString_staticFunctions
;
1927 definition
.staticValues
= CString_staticValues
;
1928 definition
.getProperty
= &CString_getProperty
;
1929 definition
.setProperty
= &CString_setProperty
;
1930 definition
.finalize
= &CYFinalize
;
1931 CString_
= JSClassCreate(&definition
);
1933 definition
= kJSClassDefinitionEmpty
;
1934 definition
.className
= "Functor";
1935 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1936 definition
.staticValues
= Functor_staticValues
;
1937 definition
.callAsFunction
= &Functor_callAsFunction
;
1938 definition
.finalize
= &CYFinalize
;
1939 Functor_
= JSClassCreate(&definition
);
1941 definition
= kJSClassDefinitionEmpty
;
1942 definition
.className
= "Pointer";
1943 definition
.staticFunctions
= Pointer_staticFunctions
;
1944 definition
.staticValues
= Pointer_staticValues
;
1945 definition
.callAsFunction
= &Pointer_callAsFunction
;
1946 definition
.getProperty
= &Pointer_getProperty
;
1947 definition
.setProperty
= &Pointer_setProperty
;
1948 definition
.finalize
= &CYFinalize
;
1949 Pointer_
= JSClassCreate(&definition
);
1951 definition
= kJSClassDefinitionEmpty
;
1952 definition
.className
= "Struct";
1953 definition
.staticFunctions
= Struct_staticFunctions
;
1954 definition
.getProperty
= &Struct_getProperty
;
1955 definition
.setProperty
= &Struct_setProperty
;
1956 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1957 definition
.finalize
= &CYFinalize
;
1958 Struct_
= JSClassCreate(&definition
);
1960 definition
= kJSClassDefinitionEmpty
;
1961 definition
.className
= "Type";
1962 definition
.staticValues
= Type_staticValues
;
1963 definition
.staticFunctions
= Type_staticFunctions
;
1964 definition
.callAsFunction
= &Type_callAsFunction
;
1965 definition
.callAsConstructor
= &Type_callAsConstructor
;
1966 definition
.finalize
= &CYFinalize
;
1967 Type_privateData::Class_
= JSClassCreate(&definition
);
1969 definition
= kJSClassDefinitionEmpty
;
1970 definition
.className
= "Global";
1971 //definition.getProperty = &Global_getProperty;
1972 Global_
= JSClassCreate(&definition
);
1974 Array_s
= JSStringCreateWithUTF8CString("Array");
1975 cy_s
= JSStringCreateWithUTF8CString("$cy");
1976 cyi_s
= JSStringCreateWithUTF8CString("$cyi");
1977 length_s
= JSStringCreateWithUTF8CString("length");
1978 message_s
= JSStringCreateWithUTF8CString("message");
1979 name_s
= JSStringCreateWithUTF8CString("name");
1980 pop_s
= JSStringCreateWithUTF8CString("pop");
1981 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1982 push_s
= JSStringCreateWithUTF8CString("push");
1983 splice_s
= JSStringCreateWithUTF8CString("splice");
1984 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1985 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1986 toPointer_s
= JSStringCreateWithUTF8CString("toPointer");
1987 toString_s
= JSStringCreateWithUTF8CString("toString");
1988 weak_s
= JSStringCreateWithUTF8CString("weak");
1990 Result_
= JSStringCreateWithUTF8CString("_");
1992 for (CYHook
*hook
: GetHooks())
1993 if (hook
->Initialize
!= NULL
)
1994 (*hook
->Initialize
)();
1997 void CYThrow(JSContextRef context
, JSValueRef value
) {
1999 throw CYJSError(context
, value
);
2002 const char *CYJSError::PoolCString(CYPool
&pool
) const {
2003 std::set
<void *> objects
;
2004 // XXX: this used to be CYPoolCString
2005 return CYPoolCCYON(pool
, context_
, value_
, objects
);
2008 JSValueRef
CYJSError::CastJSValue(JSContextRef context
, const char *name
) const {
2009 // XXX: what if the context is different? or the name? I dunno. ("epic" :/)
2013 JSValueRef
CYCastJSError(JSContextRef context
, const char *name
, const char *message
) {
2014 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString(name
)));
2015 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
2016 return _jsccall(JSObjectCallAsConstructor
, context
, Error
, 1, arguments
);
2019 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
, const char *name
) const {
2020 return CYCastJSError(context
, name
, message_
);
2023 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
2024 _assert(context
!= NULL
);
2029 va_start(args
, format
);
2030 // XXX: there might be a beter way to think about this
2031 const char *message(pool
.vsprintf(64, format
, args
));
2034 value_
= CYCastJSError(context
, "Error", message
);
2037 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
2038 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
2041 static const char *CYPoolLibraryPath(CYPool
&pool
) {
2043 _assert(dladdr(reinterpret_cast<void *>(&CYPoolLibraryPath
), &addr
) != 0);
2044 char *lib(pool
.strdup(addr
.dli_fname
));
2046 char *slash(strrchr(lib
, '/'));
2047 _assert(slash
!= NULL
);
2053 static JSValueRef
require_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
2054 _assert(count
== 1);
2057 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
2058 if (strchr(name
, '/') == NULL
&& (
2060 dlopen(pool
.strcat("/System/Library/Frameworks/", name
, ".framework/", name
, NULL
), RTLD_LAZY
| RTLD_GLOBAL
) != NULL
||
2061 dlopen(pool
.strcat("/System/Library/PrivateFrameworks/", name
, ".framework/", name
, NULL
), RTLD_LAZY
| RTLD_GLOBAL
) != NULL
||
2064 return CYJSUndefined(context
);
2066 JSObjectRef
resolve(CYCastJSObject(context
, CYGetProperty(context
, object
, CYJSString("resolve"))));
2067 CYJSString
path(context
, CYCallAsFunction(context
, resolve
, NULL
, 1, arguments
));
2069 CYJSString
property("exports");
2071 JSObjectRef
modules(CYGetCachedObject(context
, CYJSString("modules")));
2072 JSValueRef
cache(CYGetProperty(context
, modules
, path
));
2075 if (!JSValueIsUndefined(context
, cache
)) {
2076 JSObjectRef
module(CYCastJSObject(context
, cache
));
2077 result
= CYGetProperty(context
, module, property
);
2079 CYUTF8String
code(CYPoolFileUTF8String(pool
, CYPoolCString(pool
, context
, path
)));
2080 _assert(code
.data
!= NULL
);
2082 size_t length(strlen(name
));
2083 if (length
>= 5 && strcmp(name
+ length
- 5, ".json") == 0) {
2084 JSObjectRef
JSON(CYGetCachedObject(context
, CYJSString("JSON")));
2085 JSObjectRef
parse(CYCastJSObject(context
, CYGetProperty(context
, JSON
, CYJSString("parse"))));
2086 JSValueRef arguments
[1] = { CYCastJSValue(context
, CYJSString(code
)) };
2087 result
= CYCallAsFunction(context
, parse
, JSON
, 1, arguments
);
2089 JSObjectRef
module(JSObjectMake(context
, NULL
, NULL
));
2090 CYSetProperty(context
, modules
, path
, module);
2092 JSObjectRef
exports(JSObjectMake(context
, NULL
, NULL
));
2093 CYSetProperty(context
, module, property
, exports
);
2095 std::stringstream wrap
;
2096 wrap
<< "(function (exports, require, module, __filename) { " << code
<< "\n});";
2097 code
= CYPoolCode(pool
, *wrap
.rdbuf());
2099 JSValueRef
value(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0));
2100 JSObjectRef
function(CYCastJSObject(context
, value
));
2102 JSValueRef arguments
[4] = { exports
, object
, module, CYCastJSValue(context
, path
) };
2103 CYCallAsFunction(context
, function
, NULL
, 4, arguments
);
2104 result
= CYGetProperty(context
, module, property
);
2111 static bool CYRunScript(JSGlobalContextRef context
, const char *path
) {
2113 CYUTF8String
code(CYPoolFileUTF8String(pool
, path
));
2114 if (code
.data
== NULL
)
2117 code
= CYPoolCode(pool
, code
);
2118 _jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0);
2122 extern "C" void CYDestroyWeak(JSWeakObjectMapRef weak
, void *data
) {
2125 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
2126 CYInitializeDynamic();
2128 JSObjectRef
global(CYGetGlobalObject(context
));
2130 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
2131 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
2133 /* Cache Globals {{{ */
2134 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
2135 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
2137 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
2138 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
2140 JSObjectRef
Boolean(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Boolean"))));
2141 CYSetProperty(context
, cy
, CYJSString("Boolean"), Boolean
);
2143 JSObjectRef
Boolean_prototype(CYCastJSObject(context
, CYGetProperty(context
, Boolean
, prototype_s
)));
2144 CYSetProperty(context
, cy
, CYJSString("Boolean_prototype"), Boolean_prototype
);
2146 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
2147 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
2149 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
2150 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
2152 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
2153 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
2155 JSObjectRef
JSON(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("JSON"))));
2156 CYSetProperty(context
, cy
, CYJSString("JSON"), JSON
);
2158 JSObjectRef
Number(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Number"))));
2159 CYSetProperty(context
, cy
, CYJSString("Number"), Number
);
2161 JSObjectRef
Number_prototype(CYCastJSObject(context
, CYGetProperty(context
, Number
, prototype_s
)));
2162 CYSetProperty(context
, cy
, CYJSString("Number_prototype"), Number_prototype
);
2164 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
2165 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
2167 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
2168 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
2170 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
2171 CYSetProperty(context
, cy
, CYJSString("String"), String
);
2173 JSObjectRef
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
)));
2174 CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
);
2176 JSObjectRef
SyntaxError(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("SyntaxError"))));
2177 CYSetProperty(context
, cy
, CYJSString("SyntaxError"), SyntaxError
);
2180 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
2181 CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
2183 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
2184 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
2185 CYSetProperty(context
, cycript
, CYJSString("compile"), &Cycript_compile_callAsFunction
);
2186 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
2188 JSObjectRef
CString(JSObjectMakeConstructor(context
, CString_
, &CString_new
));
2189 CYSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, CString
, prototype_s
)), String_prototype
);
2190 CYSetProperty(context
, cycript
, CYJSString("CString"), CString
);
2192 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
2193 CYSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
2194 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
2196 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
2197 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
2199 JSObjectRef
modules(JSObjectMake(context
, NULL
, NULL
));
2200 CYSetProperty(context
, cy
, CYJSString("modules"), modules
);
2202 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
2203 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
2205 JSObjectRef
cache(JSObjectMake(context
, NULL
, NULL
));
2206 CYSetProperty(context
, cy
, CYJSString("cache"), cache
);
2207 CYSetPrototype(context
, cache
, all
);
2209 JSObjectRef
alls(_jsccall(JSObjectCallAsConstructor
, context
, Array
, 0, NULL
));
2210 CYSetProperty(context
, cycript
, CYJSString("alls"), alls
);
2213 JSObjectRef
last(NULL
), curr(global
);
2215 goto next
; for (JSValueRef next
;;) {
2216 if (JSValueIsNull(context
, next
))
2219 curr
= CYCastJSObject(context
, next
);
2221 next
= JSObjectGetPrototype(context
, curr
);
2224 CYSetPrototype(context
, last
, cache
);
2227 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
2228 CYSetProperty(context
, cy
, CYJSString("System"), System
);
2230 CYSetProperty(context
, global
, CYJSString("require"), &require_callAsFunction
, kJSPropertyAttributeDontEnum
);
2232 CYSetProperty(context
, global
, CYJSString("system"), System
);
2233 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
2234 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
2236 CYSetProperty(context
, global
, CYJSString("global"), global
);
2239 if (&JSWeakObjectMapCreate
!= NULL
) {
2240 JSWeakObjectMapRef
weak(JSWeakObjectMapCreate(context
, NULL
, &CYDestroyWeak
));
2241 CYSetProperty(context
, cy
, weak_s
, CYCastJSValue(context
, reinterpret_cast<uintptr_t>(weak
)));
2245 CYSetProperty(context
, cache
, CYJSString("dlerror"), CYMakeFunctor(context
, "dlerror", "*"), kJSPropertyAttributeDontEnum
);
2246 CYSetProperty(context
, cache
, CYJSString("RTLD_DEFAULT"), CYCastJSValue(context
, reinterpret_cast<intptr_t>(RTLD_DEFAULT
)), kJSPropertyAttributeDontEnum
);
2247 CYSetProperty(context
, cache
, CYJSString("dlsym"), CYMakeFunctor(context
, "dlsym", "^v^v*"), kJSPropertyAttributeDontEnum
);
2249 CYSetProperty(context
, cache
, CYJSString("NULL"), CYJSNull(context
), kJSPropertyAttributeDontEnum
);
2251 CYSetProperty(context
, cache
, CYJSString("bool"), CYMakeType(context
, "B"), kJSPropertyAttributeDontEnum
);
2252 CYSetProperty(context
, cache
, CYJSString("char"), CYMakeType(context
, "c"), kJSPropertyAttributeDontEnum
);
2253 CYSetProperty(context
, cache
, CYJSString("short"), CYMakeType(context
, "s"), kJSPropertyAttributeDontEnum
);
2254 CYSetProperty(context
, cache
, CYJSString("int"), CYMakeType(context
, "i"), kJSPropertyAttributeDontEnum
);
2255 CYSetProperty(context
, cache
, CYJSString("long"), CYMakeType(context
, "l"), kJSPropertyAttributeDontEnum
);
2256 CYSetProperty(context
, cache
, CYJSString("float"), CYMakeType(context
, "f"), kJSPropertyAttributeDontEnum
);
2257 CYSetProperty(context
, cache
, CYJSString("double"), CYMakeType(context
, "d"), kJSPropertyAttributeDontEnum
);
2259 for (CYHook
*hook
: GetHooks())
2260 if (hook
->SetupContext
!= NULL
)
2261 (*hook
->SetupContext
)(context
);
2263 CYArrayPush(context
, alls
, cycript
);
2265 CYRunScript(context
, "libcycript.cy");
2268 static JSGlobalContextRef context_
;
2270 _visible JSGlobalContextRef
CYGetJSContext() {
2271 CYInitializeDynamic();
2273 if (context_
== NULL
) {
2274 context_
= JSGlobalContextCreate(Global_
);
2275 CYSetupContext(context_
);
2281 _visible
void CYDestroyContext() {
2282 if (context_
== NULL
)
2284 JSGlobalContextRelease(context_
);