1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 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"
48 #include "Execute.hpp"
49 #include "Internal.hpp"
50 #include "JavaScript.hpp"
51 #include "Pooling.hpp"
54 const char *sqlite3_column_string(sqlite3_stmt
*stmt
, int n
) {
55 return reinterpret_cast<const char *>(sqlite3_column_text(stmt
, n
));
58 char *sqlite3_column_pooled(CYPool
&pool
, sqlite3_stmt
*stmt
, int n
) {
59 if (const char *value
= sqlite3_column_string(stmt
, n
))
60 return pool
.strdup(value
);
64 static std::vector
<CYHook
*> &GetHooks() {
65 static std::vector
<CYHook
*> hooks
;
69 CYRegisterHook::CYRegisterHook(CYHook
*hook
) {
70 GetHooks().push_back(hook
);
73 /* JavaScript Properties {{{ */
74 bool CYHasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
75 return JSObjectHasProperty(context
, object
, name
);
78 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) {
79 return _jsccall(JSObjectGetPropertyAtIndex
, context
, object
, index
);
82 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
83 return _jsccall(JSObjectGetProperty
, context
, object
, name
);
86 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) {
87 _jsccall(JSObjectSetPropertyAtIndex
, context
, object
, index
, value
);
90 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) {
91 _jsccall(JSObjectSetProperty
, context
, object
, name
, value
, attributes
);
94 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef (*callback
)(JSContextRef
, JSObjectRef
, JSObjectRef
, size_t, const JSValueRef
[], JSValueRef
*), JSPropertyAttributes attributes
) {
95 CYSetProperty(context
, object
, name
, JSObjectMakeFunctionWithCallback(context
, name
, callback
), attributes
);
98 JSObjectRef
CYGetPrototype(JSContextRef context
, JSObjectRef object
) {
99 return CYCastJSObject(context
, JSObjectGetPrototype(context
, object
));
102 void CYSetPrototype(JSContextRef context
, JSObjectRef object
, JSValueRef value
) {
103 _assert(!JSValueIsUndefined(context
, value
));
104 JSObjectSetPrototype(context
, object
, value
);
105 _assert(CYIsStrictEqual(context
, JSObjectGetPrototype(context
, object
), value
));
108 /* JavaScript Strings {{{ */
109 JSStringRef
CYCopyJSString(const char *value
) {
110 return value
== NULL
? NULL
: JSStringCreateWithUTF8CString(value
);
113 JSStringRef
CYCopyJSString(JSStringRef value
) {
114 return value
== NULL
? NULL
: JSStringRetain(value
);
117 JSStringRef
CYCopyJSString(CYUTF8String value
) {
118 if (memchr(value
.data
, '\0', value
.size
) != NULL
) {
120 return CYCopyJSString(CYPoolUTF16String(pool
, value
));
121 } else if (value
.data
[value
.size
] != '\0') {
123 return CYCopyJSString(pool
.strmemdup(value
.data
, value
.size
));
125 return CYCopyJSString(value
.data
);
129 JSStringRef
CYCopyJSString(const std::string
&value
) {
130 return CYCopyJSString(CYUTF8String(value
.c_str(), value
.size()));
133 JSStringRef
CYCopyJSString(CYUTF16String value
) {
134 return JSStringCreateWithCharacters(value
.data
, value
.size
);
137 JSStringRef
CYCopyJSString(JSContextRef context
, JSValueRef value
) {
138 if (JSValueIsNull(context
, value
))
140 return _jsccall(JSValueToStringCopy
, context
, value
);
143 CYUTF16String
CYCastUTF16String(JSStringRef value
) {
144 return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
));
147 const char *CYPoolCString(CYPool
&pool
, CYUTF8String utf8
) {
148 return pool
.strndup(utf8
.data
, utf8
.size
);
151 CYUTF8String
CYPoolUTF8String(CYPool
&pool
, CYUTF8String utf8
) {
152 return {pool
.strndup(utf8
.data
, utf8
.size
), utf8
.size
};
155 _visible CYUTF8String
CYPoolUTF8String(CYPool
&pool
, const std::string
&value
) {
156 return {pool
.strndup(value
.data(), value
.size()), value
.size()};
159 CYUTF8String
CYPoolUTF8String(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
160 return CYPoolUTF8String(pool
, CYCastUTF16String(value
));
163 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
164 CYUTF8String
utf8(CYPoolUTF8String(pool
, context
, value
));
165 _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
);
169 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
170 return JSValueIsNull(context
, value
) ? NULL
: CYPoolCString(pool
, context
, CYJSString(context
, value
));
173 /* Index Offsets {{{ */
174 size_t CYGetIndex(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
175 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
179 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
181 JSObjectRef
CYObjectMakeArray(JSContextRef context
, size_t length
, const JSValueRef values
[]) {
182 if (JSObjectMakeArray$
!= NULL
)
183 return _jsccall(*JSObjectMakeArray$
, context
, length
, values
);
184 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
185 bool wat(length
== 1 && JSValueGetType(context
, values
[0]) == kJSTypeNumber
);
186 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, wat
? 0 : length
, values
));
187 JSObjectRef
object(CYCastJSObject(context
, value
));
188 if (wat
) CYArrayPush(context
, object
, 1, values
);
192 static JSClassRef All_
;
193 JSClassRef
cy::Functor::Class_
;
194 static JSClassRef Global_
;
197 JSStringRef constructor_s
;
201 JSStringRef length_s
;
202 JSStringRef message_s
;
205 JSStringRef prototype_s
;
207 JSStringRef splice_s
;
208 JSStringRef toCYON_s
;
209 JSStringRef toJSON_s
;
210 JSStringRef toPointer_s
;
211 JSStringRef toString_s
;
214 static sqlite3
*database_
;
216 static JSStringRef Result_
;
218 void CYFinalize(JSObjectRef object
) {
219 CYData
*internal(reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
)));
220 if (internal
== NULL
)
222 _assert(internal
->count_
!= _not(unsigned));
223 if (--internal
->count_
== 0)
227 sig::Type
*Structor_(CYPool
&pool
, sig::Aggregate
*aggregate
) {
235 JSGlobalContextRef context_
;
237 Context(JSGlobalContextRef context
) :
248 Type_privateData
*type_
;
251 CArray(void *value
, size_t length
, const sig::Type
&type
, ffi_type
*ffi
, JSContextRef context
, JSObjectRef owner
) :
253 owner_(context
, owner
),
254 type_(new(*pool_
) Type_privateData(type
, ffi
)),
266 CString(char *value
, JSContextRef context
, JSObjectRef owner
) :
268 owner_(context
, owner
)
278 Type_privateData
*type_
;
280 Pointer(void *value
, const sig::Type
&type
, JSContextRef context
, JSObjectRef owner
) :
282 owner_(context
, owner
),
283 type_(new(*pool_
) Type_privateData(type
))
287 Pointer(void *value
, const char *encoding
, JSContextRef context
, JSObjectRef owner
) :
289 owner_(context
, owner
),
290 type_(new(*pool_
) Type_privateData(encoding
))
295 struct Struct_privateData
:
300 Type_privateData
*type_
;
302 Struct_privateData(void *value
, const sig::Type
&type
, ffi_type
*ffi
, JSContextRef context
, JSObjectRef owner
) :
304 owner_(context
, owner
),
305 type_(new(*pool_
) Type_privateData(type
, ffi
))
308 size_t size(ffi
->size
);
309 void *copy(pool_
->malloc
<void>(size
, ffi
->alignment
));
310 memcpy(copy
, value_
, size
);
316 static void *CYCastSymbol(const char *name
) {
317 for (CYHook
*hook
: GetHooks())
318 if (hook
->CastSymbol
!= NULL
)
319 if (void *value
= (*hook
->CastSymbol
)(name
))
321 return dlsym(RTLD_DEFAULT
, name
);
324 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
325 return JSValueMakeBoolean(context
, value
);
328 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
329 return JSValueMakeNumber(context
, value
);
332 #define CYCastJSValue_(Type_) \
333 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
334 _assert(static_cast<Type_>(static_cast<double>(value)) == value); \
335 return JSValueMakeNumber(context, static_cast<double>(value)); \
338 CYCastJSValue_(long double)
339 CYCastJSValue_(signed short int)
340 CYCastJSValue_(unsigned short int)
341 CYCastJSValue_(signed int)
342 CYCastJSValue_(unsigned int)
343 CYCastJSValue_(signed long int)
344 CYCastJSValue_(unsigned long int)
345 CYCastJSValue_(signed long long int)
346 CYCastJSValue_(unsigned long long int)
348 #ifdef __SIZEOF_INT128__
349 CYCastJSValue_(signed __int128
)
350 CYCastJSValue_(unsigned __int128
)
353 JSValueRef
CYJSUndefined(JSContextRef context
) {
354 return JSValueMakeUndefined(context
);
357 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
358 return _jsccall(JSValueToNumber
, context
, value
);
361 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
362 return JSValueToBoolean(context
, value
);
365 JSValueRef
CYJSNull(JSContextRef context
) {
366 return JSValueMakeNull(context
);
369 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
370 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
373 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
374 return CYCastJSValue(context
, CYJSString(value
));
377 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
378 return _jsccall(JSValueToObject
, context
, value
);
381 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
382 return _jsccall(JSObjectCallAsFunction
, context
, function
, _this
, count
, arguments
);
385 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
386 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
389 bool CYIsEqual(JSContextRef context
, JSValueRef lhs
, JSValueRef rhs
) {
390 return _jsccall(JSValueIsEqual
, context
, lhs
, rhs
);
393 bool CYIsStrictEqual(JSContextRef context
, JSValueRef lhs
, JSValueRef rhs
) {
394 return JSValueIsStrictEqual(context
, lhs
, rhs
);
397 size_t CYArrayLength(JSContextRef context
, JSObjectRef array
) {
398 return CYCastDouble(context
, CYGetProperty(context
, array
, length_s
));
401 JSValueRef
CYArrayGet(JSContextRef context
, JSObjectRef array
, size_t index
) {
402 return _jsccall(JSObjectGetPropertyAtIndex
, context
, array
, index
);
405 void CYArrayPush(JSContextRef context
, JSObjectRef array
, size_t length
, const JSValueRef arguments
[]) {
406 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
407 _jsccall(JSObjectCallAsFunction
, context
, CYCastJSObject(context
, CYGetProperty(context
, Array
, push_s
)), array
, length
, arguments
);
410 void CYArrayPush(JSContextRef context
, JSObjectRef array
, JSValueRef value
) {
411 return CYArrayPush(context
, array
, 1, &value
);
414 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
421 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, arguments
[0])));
422 fwrite(string
.data
, string
.size
, 1, file
);
426 return CYJSUndefined(context
);
429 static JSValueRef
Global_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
433 for (size_t i(0); i
!= count
; ++i
) {
436 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, arguments
[i
])));
437 fwrite(string
.data
, string
.size
, 1, file
);
442 return CYJSUndefined(context
);
445 static void (*JSSynchronousGarbageCollectForDebugging$
)(JSContextRef
);
447 _visible
void CYGarbageCollect(JSContextRef context
) {
448 (JSSynchronousGarbageCollectForDebugging$
?: &JSGarbageCollect
)(context
);
451 static JSValueRef
Cycript_compile_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
453 CYUTF8String
before(CYPoolUTF8String(pool
, context
, CYJSString(context
, arguments
[0])));
454 CYUTF8String
after(CYPoolCode(pool
, before
));
455 return CYCastJSValue(context
, CYJSString(after
));
456 } CYCatch_(NULL
, "SyntaxError") }
458 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
459 CYGarbageCollect(context
);
460 return CYJSUndefined(context
);
463 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> &objects
, JSValueRef
*exception
) { CYTry
{
464 switch (JSType type
= JSValueGetType(context
, value
)) {
465 case kJSTypeUndefined
:
470 return CYCastBool(context
, value
) ? "true" : "false";
472 case kJSTypeNumber
: {
473 std::ostringstream str
;
474 CYNumerify(str
, CYCastDouble(context
, value
));
475 std::string
value(str
.str());
476 return pool
.strmemdup(value
.c_str(), value
.size());
479 case kJSTypeString
: {
480 std::ostringstream str
;
481 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
482 CYStringify(str
, string
.data
, string
.size
, CYStringifyModeCycript
);
483 std::string
value(str
.str());
484 return pool
.strmemdup(value
.c_str(), value
.size());
488 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
, objects
);
490 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
494 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> &objects
) {
495 return _jsccall(CYPoolCCYON
, pool
, context
, value
, objects
);
498 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> *objects
) {
500 return CYPoolCCYON(pool
, context
, value
, *objects
);
502 std::set
<void *> objects
;
503 return CYPoolCCYON(pool
, context
, value
, objects
);
507 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSObjectRef object
, std::set
<void *> &objects
) {
508 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
509 if (CYIsCallable(context
, toCYON
)) {
510 // XXX: this needs to be abstracted behind some kind of function
511 JSValueRef arguments
[1] = {CYCastJSValue(context
, reinterpret_cast<uintptr_t>(&objects
))};
512 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 1, arguments
));
513 _assert(value
!= NULL
);
514 return CYPoolCString(pool
, context
, value
);
517 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
518 if (CYIsCallable(context
, toJSON
)) {
519 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
520 return _jsccall(CYPoolCCYON
, pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), objects
);
523 if (JSObjectIsFunction(context
, object
)) {
524 JSValueRef
toString(CYGetProperty(context
, object
, toString_s
));
525 if (CYIsCallable(context
, toString
)) {
526 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
527 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toString
, object
, 1, arguments
));
528 _assert(value
!= NULL
);
529 return CYPoolCString(pool
, context
, value
);
533 _assert(objects
.insert(object
).second
);
535 std::ostringstream str
;
537 JSValueRef
value(CYGetProperty(context
, object
, constructor_s
));
538 if (JSValueIsObject(context
, value
)) {
539 JSObjectRef
constructor(CYCastJSObject(context
, value
));
540 JSValueRef
theory(CYGetProperty(context
, constructor
, prototype_s
));
541 JSValueRef
practice(JSObjectGetPrototype(context
, object
));
543 if (CYIsStrictEqual(context
, theory
, practice
)) {
544 JSValueRef
name(CYGetProperty(context
, constructor
, name_s
));
545 if (!JSValueIsUndefined(context
, name
)) {
546 auto utf8(CYPoolUTF8String(pool
, context
, CYJSString(context
, name
)));
547 if (utf8
!= "Object")
548 str
<< "new" << ' ' << utf8
;
555 // XXX: this is, sadly, going to leak
556 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
560 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
566 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
567 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
572 CYStringify(str
, string
.data
, string
.size
, CYStringifyModeLegacy
);
577 JSValueRef
value(CYGetProperty(context
, object
, name
));
578 str
<< CYPoolCCYON(pool
, context
, value
, objects
);
579 } catch (const CYException
&error
) {
584 JSPropertyNameArrayRelease(names
);
588 std::string
string(str
.str());
589 return pool
.strmemdup(string
.c_str(), string
.size());
592 std::set
<void *> *CYCastObjects(JSContextRef context
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
595 return CYCastPointer
<std::set
<void *> *>(context
, arguments
[0]);
598 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
599 std::set
<void *> *objects(CYCastObjects(context
, _this
, count
, arguments
));
600 // XXX: this is horribly inefficient
601 std::set
<void *> backup
;
606 std::ostringstream str
;
610 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
613 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
620 JSValueRef
value(CYGetProperty(context
, _this
, index
));
621 if (!JSValueIsUndefined(context
, value
))
622 str
<< CYPoolCCYON(pool
, context
, value
, *objects
);
627 } catch (const CYException
&error
) {
634 std::string
value(str
.str());
635 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
638 static JSValueRef
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
640 std::ostringstream str
;
642 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
)));
643 CYStringify(str
, string
.data
, string
.size
, CYStringifyModeCycript
);
645 std::string
value(str
.str());
646 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
649 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, const sig::Type
&type
, ffi_type
*ffi
, JSObjectRef owner
) {
650 return CYPrivate
<Pointer
>::Make(context
, pointer
, type
, context
, owner
);
653 static JSValueRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), bool variadic
, const sig::Signature
&signature
) {
654 if (function
== NULL
)
655 return CYJSNull(context
);
656 return JSObjectMake(context
, cy::Functor::Class_
, new cy::Functor(function
, variadic
, signature
));
659 // XXX: remove this, as it is really stupid
660 static JSObjectRef
CYMakeFunctor(JSContextRef context
, const char *symbol
, const char *encoding
) {
661 void (*function
)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol
)));
662 if (function
== NULL
)
665 cy::Functor
*internal(new cy::Functor(function
, encoding
));
667 return JSObjectMake(context
, cy::Functor::Class_
, internal
);
670 bool CYGetOffset(CYPool
&pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
671 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
674 // XXX: this is a horrible back added for CFType
675 void *CYCastPointerEx_(JSContextRef context
, JSObjectRef value
) {
676 JSObjectRef
object((JSObjectRef
) value
);
677 if (JSValueIsObjectOfClass(context
, value
, CYPrivate
<Pointer
>::Class_
)) {
678 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
679 return internal
->value_
;
682 JSValueRef
toPointer(CYGetProperty(context
, object
, toPointer_s
));
683 if (CYIsCallable(context
, toPointer
)) {
684 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
));
685 _assert(value
!= NULL
);
686 return CYCastPointer_(context
, value
);
692 void *CYCastPointer_(JSContextRef context
, JSValueRef value
, bool *guess
) {
695 else switch (JSValueGetType(context
, value
)) {
698 case kJSTypeObject
: {
699 JSObjectRef
object((JSObjectRef
) value
);
700 if (JSValueIsObjectOfClass(context
, value
, CYPrivate
<Pointer
>::Class_
)) {
701 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
702 return internal
->value_
;
704 JSValueRef
toPointer(CYGetProperty(context
, object
, toPointer_s
));
705 if (CYIsCallable(context
, toPointer
)) {
706 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
));
707 _assert(value
!= NULL
);
708 return CYCastPointer_(context
, value
, guess
);
714 double number(CYCastDouble(context
, value
));
715 if (!std::isnan(number
))
716 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
718 throw CYJSError(context
, "cannot convert value to pointer");
726 static JSValueRef
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
);
730 // XXX: this is somehow not quite a template :/
733 void Primitive
<bool>::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
734 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
737 #define CYPoolFFI_(Type_) \
739 void Primitive<Type_>::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const { \
740 *reinterpret_cast<Type_ *>(data) = CYCastDouble(context, value); \
746 CYPoolFFI_(long double)
748 CYPoolFFI_(signed char)
749 CYPoolFFI_(signed int)
750 CYPoolFFI_(signed long int)
751 CYPoolFFI_(signed long long int)
752 CYPoolFFI_(signed short int)
754 CYPoolFFI_(unsigned char)
755 CYPoolFFI_(unsigned int)
756 CYPoolFFI_(unsigned long int)
757 CYPoolFFI_(unsigned long long int)
758 CYPoolFFI_(unsigned short int)
760 #ifdef __SIZEOF_INT128__
761 CYPoolFFI_(signed __int128
)
762 CYPoolFFI_(unsigned __int128
)
766 void Primitive
<char>::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
767 if (JSValueGetType(context
, value
) != kJSTypeString
)
768 *reinterpret_cast<char *>(data
) = CYCastDouble(context
, value
);
770 CYJSString
script(context
, value
);
771 auto string(CYCastUTF16String(script
));
772 _assert(string
.size
== 1);
773 _assert((string
.data
[0] & 0xff) == string
.data
[0]);
774 *reinterpret_cast<char *>(data
) = string
.data
[0];
778 void Void::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
779 _assert(JSValueIsUndefined(context
, value
));
782 void Unknown::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
786 void String::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
788 *reinterpret_cast<const char **>(data
) = CYCastPointer
<const char *>(context
, value
, &guess
);
789 if (guess
&& pool
!= NULL
)
790 *reinterpret_cast<const char **>(data
) = CYPoolCString(*pool
, context
, value
);
793 void Bits::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
797 static void CYArrayCopy(CYPool
*pool
, JSContextRef context
, uint8_t *base
, size_t length
, const sig::Type
&type
, ffi_type
*ffi
, JSObjectRef object
) {
798 for (size_t index(0); index
!= length
; ++index
) {
799 JSValueRef
rhs(CYGetProperty(context
, object
, index
));
800 if (JSValueIsUndefined(context
, rhs
))
801 throw CYJSError(context
, "unable to extract array value");
802 type
.PoolFFI(pool
, context
, ffi
, base
, rhs
);
807 void Pointer::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
809 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
, &guess
);
810 if (!guess
|| pool
== NULL
|| !JSValueIsObject(context
, value
))
813 JSObjectRef
object(CYCastJSObject(context
, value
));
815 if (sig::Function
*function
= dynamic_cast<sig::Function
*>(&type
)) {
816 _assert(!function
->variadic
);
817 auto internal(CYMakeFunctor_(context
, object
, function
->signature
, &FunctionAdapter_
));
818 // XXX: see notes in Library.cpp about needing to leak
819 *reinterpret_cast<void (**)()>(data
) = internal
->value_
;
820 } else if (CYHasProperty(context
, object
, length_s
)) {
821 size_t length(CYArrayLength(context
, object
));
822 ffi_type
*element(type
.GetFFI(*pool
));
823 size_t size(element
->size
* length
);
824 uint8_t *base(pool
->malloc
<uint8_t>(size
, element
->alignment
));
825 CYArrayCopy(pool
, context
, base
, length
, type
, element
, object
);
826 *reinterpret_cast<void **>(data
) = base
;
830 void Array::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
833 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
834 CYArrayCopy(pool
, context
, base
, size
, type
, ffi
->elements
[0], CYCastJSObject(context
, value
));
837 void Enum::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
838 return type
.PoolFFI(pool
, context
, ffi
, data
, value
);
841 void Aggregate::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
843 _assert(signature
.count
!= _not(size_t));
846 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
847 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
848 for (size_t index(0); index
!= signature
.count
; ++index
) {
849 sig::Element
*element(&signature
.elements
[index
]);
850 ffi_type
*field(ffi
->elements
[index
]);
853 if (aggregate
== NULL
)
856 rhs
= CYGetProperty(context
, aggregate
, index
);
857 if (JSValueIsUndefined(context
, rhs
)) {
858 if (element
->name
!= NULL
)
859 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
862 if (JSValueIsUndefined(context
, rhs
)) undefined
:
863 throw CYJSError(context
, "unable to extract structure value");
867 element
->type
->PoolFFI(pool
, context
, field
, base
+ offset
, rhs
);
868 offset
+= field
->size
;
869 CYAlign(offset
, field
->alignment
);
873 void Function::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
877 #define CYFromFFI_(Type_) \
879 JSValueRef Primitive<Type_>::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { \
880 JSValueRef value(CYCastJSValue(context, *reinterpret_cast<Type_ *>(data))); \
881 JSObjectRef typed(_jsccall(JSObjectCallAsConstructor, context, CYGetCachedObject(context, CYJSString("Number")), 1, &value)); \
882 CYSetProperty(context, typed, cyt_s, CYMakeType(context, *this), kJSPropertyAttributeDontEnum); \
889 CYFromFFI_(long double)
891 CYFromFFI_(signed char)
892 CYFromFFI_(signed int)
893 CYFromFFI_(signed long int)
894 CYFromFFI_(signed long long int)
895 CYFromFFI_(signed short int)
897 CYFromFFI_(unsigned char)
898 CYFromFFI_(unsigned int)
899 CYFromFFI_(unsigned long int)
900 CYFromFFI_(unsigned long long int)
901 CYFromFFI_(unsigned short int)
903 #ifdef __SIZEOF_INT128__
904 CYFromFFI_(signed __int128
)
905 CYFromFFI_(unsigned __int128
)
909 JSValueRef Primitive
<bool>::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
910 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
914 JSValueRef Primitive
<char>::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
915 uint16_t string(uint8_t(*reinterpret_cast<char *>(data
)));
916 JSValueRef
value(CYCastJSValue(context
, CYJSString(CYUTF16String(&string
, 1))));
917 JSObjectRef
typed(_jsccall(JSObjectCallAsConstructor
, context
, CYGetCachedObject(context
, CYJSString("String")), 1, &value
));
918 CYSetProperty(context
, typed
, cyt_s
, CYMakeType(context
, sig::Primitive
<char>()), kJSPropertyAttributeDontEnum
);
919 CYSetPrototype(context
, typed
, CYGetCachedValue(context
, CYJSString("Character_prototype")));
923 JSValueRef
Void::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
924 return CYJSUndefined(context
);
927 JSValueRef
Unknown::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
931 JSValueRef
String::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
932 if (char *value
= *reinterpret_cast<char **>(data
))
933 return CYPrivate
<CString
>::Make(context
, value
, context
, owner
);
934 return CYJSNull(context
);
937 JSValueRef
Bits::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
941 JSValueRef
Pointer::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
942 if (void *value
= *reinterpret_cast<void **>(data
))
943 return CYMakePointer(context
, value
, type
, NULL
, owner
);
944 return CYJSNull(context
);
947 JSValueRef
Array::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
948 return CYPrivate
<CArray
>::Make(context
, data
, size
, type
, ffi
->elements
[0], context
, owner
);
951 JSValueRef
Enum::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
952 return type
.FromFFI(context
, ffi
, data
, initialize
, owner
);
955 JSValueRef
Aggregate::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
957 _assert(signature
.count
!= _not(size_t));
958 return CYPrivate
<Struct_privateData
>::Make(context
, data
, *this, ffi
, context
, owner
);
961 JSValueRef
Function::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
962 return CYMakeFunctor(context
, reinterpret_cast<void (*)()>(data
), variadic
, signature
);
967 void CYExecuteClosure(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
968 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
970 JSContextRef
context(internal
->function_
);
972 size_t count(internal
->cif_
.nargs
);
973 JSValueRef values
[count
];
975 for (size_t index(0); index
!= count
; ++index
)
976 values
[index
] = internal
->signature_
.elements
[1 + index
].type
->FromFFI(context
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
978 JSValueRef
value(internal
->adapter_(context
, count
, values
, internal
->function_
));
979 internal
->signature_
.elements
[0].type
->PoolFFI(NULL
, context
, internal
->cif_
.rtype
, result
, value
);
982 static JSValueRef
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
) {
983 return CYCallAsFunction(context
, function
, NULL
, count
, values
);
986 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
987 static void CYFreeFunctor(void *data
) {
988 ffi_closure_free(data
);
991 static void CYFreeFunctor(void *data
) {
992 _syscall(munmap(data
, sizeof(ffi_closure
)));
996 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
, JSValueRef (*adapter
)(JSContextRef
, size_t, JSValueRef
[], JSObjectRef
)) {
997 // XXX: in case of exceptions this will leak
998 Closure_privateData
*internal(new Closure_privateData(context
, function
, adapter
, signature
));
1000 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
1002 ffi_closure
*writable(reinterpret_cast<ffi_closure
*>(ffi_closure_alloc(sizeof(ffi_closure
), &executable
)));
1004 ffi_status
status(ffi_prep_closure_loc(writable
, &internal
->cif_
, &CYExecuteClosure
, internal
, executable
));
1005 _assert(status
== FFI_OK
);
1007 internal
->pool_
->atexit(&CYFreeFunctor
, writable
);
1008 internal
->value_
= reinterpret_cast<void (*)()>(executable
);
1010 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
1011 NULL
, sizeof(ffi_closure
),
1012 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
1016 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, &CYExecuteClosure
, internal
));
1017 _assert(status
== FFI_OK
);
1019 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
1021 internal
->pool_
->atexit(&CYFreeFunctor
, closure
);
1022 internal
->value_
= reinterpret_cast<void (*)()>(closure
);
1028 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
) {
1029 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, signature
, &FunctionAdapter_
));
1030 JSObjectRef
object(JSObjectMake(context
, cy::Functor::Class_
, internal
));
1031 // XXX: see above notes about needing to leak
1032 JSValueProtect(CYGetJSContext(context
), object
);
1036 JSValueRef
CYGetCachedValue(JSContextRef context
, JSStringRef name
) {
1037 return CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
);
1040 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
1041 return CYCastJSObject(context
, CYGetCachedValue(context
, name
));
1044 static JSValueRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, bool variadic
, const sig::Signature
&signature
) {
1045 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
1047 bool function(_jsccall(JSValueIsInstanceOfConstructor
, context
, value
, Function
));
1049 JSObjectRef
function(CYCastJSObject(context
, value
));
1050 return CYMakeFunctor(context
, function
, signature
);
1052 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
1053 return CYMakeFunctor(context
, function
, variadic
, signature
);
1057 static JSValueRef
CString_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1059 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(object
)));
1062 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
1064 else if (!CYGetOffset(pool
, context
, property
, offset
))
1067 sig::Primitive
<char> type
;
1068 return type
.FromFFI(context
, type
.GetFFI(pool
), internal
->value_
+ offset
, false, NULL
);
1071 static bool CString_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1073 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(object
)));
1076 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
1078 else if (!CYGetOffset(pool
, context
, property
, offset
))
1081 sig::Primitive
<char> type
;
1082 type
.PoolFFI(NULL
, context
, type
.GetFFI(pool
), internal
->value_
+ offset
, value
);
1086 static bool Index_(CYPool
&pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
1087 Type_privateData
*typical(internal
->type_
);
1088 sig::Aggregate
*type(static_cast<sig::Aggregate
*>(typical
->type_
));
1092 const char *name(CYPoolCString(pool
, context
, property
));
1093 size_t length(strlen(name
));
1094 double number(CYCastDouble(name
, length
));
1096 size_t count(type
->signature
.count
);
1098 if (std::isnan(number
)) {
1099 if (property
== NULL
)
1102 sig::Element
*elements(type
->signature
.elements
);
1104 for (size_t local(0); local
!= count
; ++local
) {
1105 sig::Element
*element(&elements
[local
]);
1106 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
1114 index
= static_cast<ssize_t
>(number
);
1115 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
1120 ffi_type
**elements(typical
->GetFFI()->elements
);
1123 for (ssize_t
local(0); local
!= index
; ++local
) {
1124 offset
+= elements
[local
]->size
;
1125 CYAlign(offset
, elements
[local
+ 1]->alignment
);
1128 base
= reinterpret_cast<uint8_t *>(internal
->value_
) + offset
;
1132 static void *Offset_(CYPool
&pool
, JSContextRef context
, JSStringRef property
, void *data
, ffi_type
*ffi
) {
1134 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
1136 else if (!CYGetOffset(pool
, context
, property
, offset
))
1138 return reinterpret_cast<uint8_t *>(data
) + ffi
->size
* offset
;
1141 static JSValueRef
Offset_getProperty(CYPool
&pool
, JSContextRef context
, JSStringRef property
, void *data
, Type_privateData
*typical
, JSObjectRef owner
) {
1142 ffi_type
*ffi(typical
->GetFFI());
1143 void *base(Offset_(pool
, context
, property
, data
, ffi
));
1146 return typical
->type_
->FromFFI(context
, ffi
, base
, false, owner
);
1149 static bool Offset_setProperty(CYPool
&pool
, JSContextRef context
, JSStringRef property
, void *data
, Type_privateData
*typical
, JSValueRef value
) {
1150 ffi_type
*ffi(typical
->GetFFI());
1151 void *base(Offset_(pool
, context
, property
, data
, ffi
));
1155 typical
->type_
->PoolFFI(NULL
, context
, ffi
, base
, value
);
1159 static JSValueRef
CArray_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1161 CArray
*internal(reinterpret_cast<CArray
*>(JSObjectGetPrivate(object
)));
1162 if (JSStringIsEqual(property
, length_s
))
1163 return CYCastJSValue(context
, internal
->length_
);
1164 Type_privateData
*typical(internal
->type_
);
1165 JSObjectRef
owner(internal
->owner_
?: object
);
1166 return Offset_getProperty(pool
, context
, property
, internal
->value_
, typical
, owner
);
1169 static bool CArray_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1171 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1172 Type_privateData
*typical(internal
->type_
);
1173 return Offset_setProperty(pool
, context
, property
, internal
->value_
, typical
, value
);
1176 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1178 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1180 Type_privateData
*typical(internal
->type_
);
1182 if (sig::Function
*function
= dynamic_cast<sig::Function
*>(typical
->type_
)) {
1183 if (!JSStringIsEqualToUTF8CString(property
, "$cyi"))
1185 return CYMakeFunctor(context
, reinterpret_cast<void (*)()>(internal
->value_
), function
->variadic
, function
->signature
);
1188 JSObjectRef
owner(internal
->owner_
?: object
);
1189 return Offset_getProperty(pool
, context
, property
, internal
->value_
, typical
, owner
);
1192 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1194 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1195 Type_privateData
*typical(internal
->type_
);
1196 return Offset_setProperty(pool
, context
, property
, internal
->value_
, typical
, value
);
1199 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1200 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
1201 Type_privateData
*typical(internal
->type_
);
1202 return CYMakePointer(context
, internal
->value_
, *typical
->type_
, typical
->ffi_
, _this
);
1205 static JSValueRef Struct_getProperty_$
cyt(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1206 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1207 return CYMakeType(context
, *internal
->type_
->type_
);
1210 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1212 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1213 Type_privateData
*typical(internal
->type_
);
1214 sig::Aggregate
*type(static_cast<sig::Aggregate
*>(typical
->type_
));
1219 if (!Index_(pool
, context
, internal
, property
, index
, base
))
1222 JSObjectRef
owner(internal
->owner_
?: object
);
1224 return type
->signature
.elements
[index
].type
->FromFFI(context
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
1227 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1229 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1230 Type_privateData
*typical(internal
->type_
);
1231 sig::Aggregate
*type(static_cast<sig::Aggregate
*>(typical
->type_
));
1236 if (!Index_(pool
, context
, internal
, property
, index
, base
))
1239 type
->signature
.elements
[index
].type
->PoolFFI(NULL
, context
, typical
->GetFFI()->elements
[index
], base
, value
);
1243 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1244 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1245 Type_privateData
*typical(internal
->type_
);
1246 sig::Aggregate
*type(static_cast<sig::Aggregate
*>(typical
->type_
));
1251 size_t count(type
->signature
.count
);
1252 sig::Element
*elements(type
->signature
.elements
);
1256 for (size_t index(0); index
!= count
; ++index
) {
1258 name
= elements
[index
].name
;
1261 sprintf(number
, "%zu", index
);
1265 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
1269 static sig::Void Void_
;
1270 static sig::Pointer
PointerToVoid_(Void_
);
1272 static sig::Type
*CYGetType(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
1273 if (JSValueIsNull(context
, value
))
1274 return &PointerToVoid_
;
1275 JSObjectRef
object(CYCastJSObject(context
, value
));
1276 JSObjectRef
type(CYCastJSObject(context
, CYGetProperty(context
, object
, cyt_s
)));
1277 _assert(JSValueIsObjectOfClass(context
, type
, CYPrivate
<Type_privateData
>::Class_
));
1278 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(type
)));
1279 return internal
->type_
;
1282 void CYCallFunction(CYPool
&pool
, JSContextRef context
, ffi_cif
*cif
, void (*function
)(), void *value
, void **values
) {
1283 ffi_call(cif
, function
, value
, values
);
1286 JSValueRef
CYCallFunction(CYPool
&pool
, JSContextRef context
, size_t setups
, void *setup
[], size_t count
, const JSValueRef arguments
[], bool initialize
, bool variadic
, const sig::Signature
&signature
, ffi_cif
*cif
, void (*function
)()) {
1287 size_t have(setups
+ count
);
1288 size_t need(signature
.count
- 1);
1291 throw CYJSError(context
, "insufficient number of arguments to ffi function");
1294 sig::Element
*elements(signature
.elements
);
1298 throw CYJSError(context
, "exorbitant number of arguments to ffi function");
1300 elements
= new (pool
) sig::Element
[have
+ 1];
1301 memcpy(elements
, signature
.elements
, sizeof(sig::Element
) * (need
+ 1));
1303 for (size_t index(need
); index
!= have
; ++index
) {
1304 sig::Element
&element(elements
[index
+ 1]);
1305 element
.name
= NULL
;
1306 element
.offset
= _not(size_t);
1307 element
.type
= CYGetType(pool
, context
, arguments
[index
- setups
]);
1310 sig::Signature extended
;
1311 extended
.elements
= elements
;
1312 extended
.count
= have
+ 1;
1313 sig::sig_ffi_cif(pool
, signature
.count
, extended
, &corrected
);
1318 memcpy(values
, setup
, sizeof(void *) * setups
);
1320 for (size_t index(setups
); index
!= have
; ++index
) {
1321 sig::Element
&element(elements
[index
+ 1]);
1322 ffi_type
*ffi(cif
->arg_types
[index
]);
1323 values
[index
] = pool
.malloc
<uint8_t>(ffi
->size
, ffi
->alignment
);
1324 element
.type
->PoolFFI(&pool
, context
, ffi
, values
[index
], arguments
[index
- setups
]);
1327 CYBuffer
buffer(context
);
1328 uint8_t *value(buffer
->malloc
<uint8_t>(std::max
<size_t>(cif
->rtype
->size
, sizeof(ffi_arg
)), std::max
<size_t>(cif
->rtype
->alignment
, alignof(ffi_arg
))));
1330 void (*call
)(CYPool
&, JSContextRef
, ffi_cif
*, void (*)(), void *, void **) = &CYCallFunction
;
1331 // XXX: this only supports one hook, but it is a bad idea anyway
1332 for (CYHook
*hook
: GetHooks())
1333 if (hook
->CallFunction
!= NULL
)
1334 call
= hook
->CallFunction
;
1336 call(pool
, context
, cif
, function
, value
, values
);
1337 return signature
.elements
[0].type
->FromFFI(context
, cif
->rtype
, value
, initialize
, buffer
);
1340 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1342 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1343 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, internal
->variadic_
, internal
->signature_
, &internal
->cif_
, internal
->value_
);
1346 static JSValueRef
Pointer_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1347 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1348 if (dynamic_cast<sig::Function
*>(internal
->type_
->type_
) == NULL
)
1349 throw CYJSError(context
, "cannot call a pointer to non-function");
1350 JSObjectRef
functor(CYCastJSObject(context
, CYGetProperty(context
, object
, cyi_s
)));
1351 return CYCallAsFunction(context
, functor
, _this
, count
, arguments
);
1354 JSObjectRef
CYMakeType(JSContextRef context
, const sig::Type
&type
) {
1355 return CYPrivate
<Type_privateData
>::Make(context
, type
);
1358 extern "C" bool CYBridgeHash(CYPool
&pool
, CYUTF8String name
, const char *&code
, unsigned &flags
) {
1359 sqlite3_stmt
*statement
;
1361 _sqlcall(sqlite3_prepare(database_
,
1363 "\"cache\".\"code\", "
1364 "\"cache\".\"flags\" "
1367 " \"cache\".\"system\" & " CY_SYSTEM
" == " CY_SYSTEM
" and"
1368 " \"cache\".\"name\" = ?"
1370 , -1, &statement
, NULL
));
1372 _sqlcall(sqlite3_bind_text(statement
, 1, name
.data
, name
.size
, SQLITE_STATIC
));
1375 if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
)
1379 code
= sqlite3_column_pooled(pool
, statement
, 0);
1380 flags
= sqlite3_column_int(statement
, 1);
1383 _sqlcall(sqlite3_finalize(statement
));
1387 static bool All_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
1388 if (JSStringIsEqualToUTF8CString(property
, "errno"))
1391 JSObjectRef
global(CYGetGlobalObject(context
));
1392 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1393 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1395 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1396 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1397 if (CYHasProperty(context
, space
, property
))
1403 if (CYBridgeHash(pool
, CYPoolUTF8String(pool
, context
, property
), code
, flags
))
1409 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1410 if (JSStringIsEqualToUTF8CString(property
, "errno"))
1411 return CYCastJSValue(context
, errno
);
1413 JSObjectRef
global(CYGetGlobalObject(context
));
1414 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1415 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1417 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1418 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1419 if (JSValueRef value
= CYGetProperty(context
, space
, property
))
1420 if (!JSValueIsUndefined(context
, value
))
1426 if (CYBridgeHash(pool
, CYPoolUTF8String(pool
, context
, property
), code
, flags
)) {
1427 CYUTF8String parsed
;
1430 parsed
= CYPoolCode(pool
, code
);
1431 } catch (const CYException
&error
) {
1432 CYThrow("%s", pool
.strcat("error caching ", CYPoolCString(pool
, context
, property
), ": ", error
.PoolCString(pool
), NULL
));
1435 JSObjectRef
cache(CYGetCachedObject(context
, CYJSString("cache")));
1438 if (flags
== CYBridgeType
) {
1439 stub
= CYMakeType(context
, sig::Void());
1440 CYSetProperty(context
, cache
, property
, stub
);
1444 JSValueRef
value(_jsccall(JSEvaluateScript
, context
, CYJSString(parsed
), NULL
, NULL
, 0));
1447 case CYBridgeVoid
: {
1450 case CYBridgeHold
: {
1451 CYSetProperty(context
, cache
, property
, value
);
1454 case CYBridgeType
: {
1455 JSObjectRef
swap(CYCastJSObject(context
, value
));
1456 void *source(JSObjectGetPrivate(swap
));
1457 _assert(source
!= NULL
);
1458 void *target(JSObjectGetPrivate(stub
));
1459 _assert(JSObjectSetPrivate(swap
, target
));
1460 _assert(JSObjectSetPrivate(stub
, source
));
1471 static JSValueRef
All_complete_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1472 _assert(count
== 1 || count
== 2);
1474 CYUTF8String
prefix(CYPoolUTF8String(pool
, context
, CYJSString(context
, arguments
[0])));
1476 JSObjectRef
array(NULL
);
1479 CYArrayBuilder
<1024> values(context
, array
);
1481 sqlite3_stmt
*statement
;
1483 if (prefix
.size
== 0)
1484 _sqlcall(sqlite3_prepare(database_
,
1486 "\"cache\".\"name\" "
1489 " \"cache\".\"system\" & " CY_SYSTEM
" == " CY_SYSTEM
1490 , -1, &statement
, NULL
));
1492 _sqlcall(sqlite3_prepare(database_
,
1494 "\"cache\".\"name\" "
1497 " \"cache\".\"name\" >= ? and \"cache\".\"name\" < ? and "
1498 " \"cache\".\"system\" & " CY_SYSTEM
" == " CY_SYSTEM
1499 , -1, &statement
, NULL
));
1501 _sqlcall(sqlite3_bind_text(statement
, 1, prefix
.data
, prefix
.size
, SQLITE_STATIC
));
1503 char *after(pool
.strndup(prefix
.data
, prefix
.size
));
1504 ++after
[prefix
.size
- 1];
1505 _sqlcall(sqlite3_bind_text(statement
, 2, after
, prefix
.size
, SQLITE_STATIC
));
1508 while (_sqlcall(sqlite3_step(statement
)) != SQLITE_DONE
)
1509 values(CYCastJSValue(context
, CYJSString(sqlite3_column_string(statement
, 0))));
1511 _sqlcall(sqlite3_finalize(statement
));
1517 static void All_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1518 JSObjectRef
global(CYGetGlobalObject(context
));
1519 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1520 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1522 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1523 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1))) {
1524 JSPropertyNameArrayRef
subset(JSObjectCopyPropertyNames(context
, space
));
1525 for (size_t index(0), count(JSPropertyNameArrayGetCount(subset
)); index
!= count
; ++index
)
1526 JSPropertyNameAccumulatorAddName(names
, JSPropertyNameArrayGetNameAtIndex(subset
, index
));
1527 JSPropertyNameArrayRelease(subset
);
1531 static JSObjectRef
CArray_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1535 static JSObjectRef
CString_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1539 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1543 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1547 } else if (count
== 1) {
1548 switch (JSValueGetType(context
, arguments
[0])) {
1549 case kJSTypeString
: {
1550 const char *encoding(CYPoolCString(pool
, context
, arguments
[0]));
1551 sig::Signature signature
;
1552 sig::Parse(pool
, &signature
, encoding
, &Structor_
);
1553 return CYMakeType(context
, *signature
.elements
[0].type
);
1556 case kJSTypeObject
: {
1557 // XXX: accept a set of enum constants and /guess/ at their size
1562 throw CYJSError(context
, "incorrect kind of argument to new Type");
1564 } else if (count
== 2) {
1565 JSObjectRef
types(CYCastJSObject(context
, arguments
[0]));
1566 size_t count(CYArrayLength(context
, types
));
1568 JSObjectRef
names(CYCastJSObject(context
, arguments
[1]));
1570 sig::Aggregate
type(false);
1571 type
.signature
.elements
= new(pool
) sig::Element
[count
];
1572 type
.signature
.count
= count
;
1574 for (size_t i(0); i
!= count
; ++i
) {
1575 sig::Element
&element(type
.signature
.elements
[i
]);
1576 element
.offset
= _not(size_t);
1578 JSValueRef
name(CYArrayGet(context
, names
, i
));
1579 if (JSValueIsUndefined(context
, name
))
1580 element
.name
= NULL
;
1582 element
.name
= CYPoolCString(pool
, context
, name
);
1584 JSObjectRef
object(CYCastJSObject(context
, CYArrayGet(context
, types
, i
)));
1585 _assert(JSValueIsObjectOfClass(context
, object
, CYPrivate
<Type_privateData
>::Class_
));
1586 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1587 element
.type
= internal
->type_
;
1588 _assert(element
.type
!= NULL
);
1591 return CYMakeType(context
, type
);
1593 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1597 static JSValueRef Type_callAsFunction_$
With(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], sig::Callable
&type
, JSValueRef
*exception
) { CYTry
{
1598 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1602 type
.signature
.elements
= new(pool
) sig::Element
[1 + count
];
1603 type
.signature
.count
= 1 + count
;
1605 type
.signature
.elements
[0].name
= NULL
;
1606 type
.signature
.elements
[0].type
= internal
->type_
;
1607 type
.signature
.elements
[0].offset
= _not(size_t);
1609 for (size_t i(0); i
!= count
; ++i
) {
1610 sig::Element
&element(type
.signature
.elements
[i
+ 1]);
1611 element
.name
= NULL
;
1612 element
.offset
= _not(size_t);
1614 JSObjectRef
object(CYCastJSObject(context
, arguments
[i
]));
1615 _assert(JSValueIsObjectOfClass(context
, object
, CYPrivate
<Type_privateData
>::Class_
));
1616 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1618 element
.type
= internal
->type_
;
1621 return CYMakeType(context
, type
);
1624 static JSValueRef
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1626 throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf");
1627 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1630 size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0])));
1631 if (index
== _not(size_t))
1632 throw CYJSError(context
, "invalid array size used with Type.arrayOf");
1634 sig::Array
type(*internal
->type_
, index
);
1635 return CYMakeType(context
, type
);
1638 static JSValueRef
Type_callAsFunction_blockWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1639 #ifdef CY_OBJECTIVEC
1641 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, type
, exception
);
1647 static JSValueRef
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1649 throw CYJSError(context
, "incorrect number of arguments to Type.constant");
1650 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1653 sig::Type
*type(internal
->type_
->Copy(pool
));
1654 type
->flags
|= JOC_TYPE_CONST
;
1655 return CYMakeType(context
, *type
);
1658 static JSValueRef
Type_callAsFunction_enumFor(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1660 throw CYJSError(context
, "incorrect number of arguments to Type.enumFor");
1661 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1665 JSObjectRef
constants(CYCastJSObject(context
, arguments
[0]));
1667 // XXX: this is, sadly, going to leak
1668 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, constants
));
1670 size_t count(JSPropertyNameArrayGetCount(names
));
1672 sig::Enum
type(*internal
->type_
, count
);
1673 type
.constants
= new(pool
) sig::Constant
[count
];
1675 for (size_t index(0); index
!= count
; ++index
) {
1676 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
1677 JSValueRef
value(CYGetProperty(context
, constants
, name
));
1678 _assert(JSValueGetType(context
, value
) == kJSTypeNumber
);
1679 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
1680 type
.constants
[index
].name
= string
.data
;
1681 type
.constants
[index
].value
= CYCastDouble(context
, value
);
1684 JSPropertyNameArrayRelease(names
);
1686 return CYMakeType(context
, type
);
1689 static JSValueRef
Type_callAsFunction_functionWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1690 bool variadic(count
!= 0 && JSValueIsNull(context
, arguments
[count
- 1]));
1691 sig::Function
type(variadic
);
1692 return Type_callAsFunction_$
With(context
, object
, _this
, variadic
? count
- 1 : count
, arguments
, type
, exception
);
1695 static JSValueRef
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1697 throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo");
1698 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1700 if (dynamic_cast<sig::Primitive
<char> *>(internal
->type_
) != NULL
)
1701 return CYMakeType(context
, sig::String((internal
->type_
->flags
& JOC_TYPE_CONST
) != 0));
1703 return CYMakeType(context
, sig::Pointer(*internal
->type_
));
1706 static JSValueRef
Type_callAsFunction_withName(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1708 throw CYJSError(context
, "incorrect number of arguments to Type.withName");
1709 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1712 return CYMakeType(context
, *internal
->type_
->Copy(pool
, CYPoolCString(pool
, context
, arguments
[0])));
1715 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1717 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1718 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1720 if (sig::Function
*function
= dynamic_cast<sig::Function
*>(internal
->type_
))
1721 return CYMakeFunctor(context
, arguments
[0], function
->variadic
, function
->signature
);
1723 CYBuffer
buffer(context
);
1725 sig::Type
*type(internal
->type_
);
1726 ffi_type
*ffi(internal
->GetFFI());
1729 if (_this
== NULL
|| CYIsStrictEqual(context
, _this
, CYGetGlobalObject(context
)))
1730 data
= buffer
->malloc
<void>(ffi
->size
, ffi
->alignment
);
1732 CYSetProperty(context
, buffer
, CYJSString("$cyo"), _this
, kJSPropertyAttributeDontEnum
);
1733 data
= CYCastPointer
<void *>(context
, _this
);
1736 type
->PoolFFI(buffer
, context
, ffi
, data
, arguments
[0]);
1737 JSValueRef
value(type
->FromFFI(context
, ffi
, data
, false, buffer
));
1741 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1743 throw CYJSError(context
, "incorrect number of arguments to Type allocator");
1744 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1746 JSObjectRef
pointer(CYMakePointer(context
, NULL
, *internal
->type_
, NULL
, NULL
));
1747 Pointer
*value(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(pointer
)));
1749 sig::Type
*type(internal
->type_
);
1750 ffi_type
*ffi(internal
->GetFFI());
1751 value
->value_
= value
->pool_
->malloc
<void>(ffi
->size
, ffi
->alignment
);
1754 memset(value
->value_
, 0, ffi
->size
);
1756 type
->PoolFFI(value
->pool_
, context
, ffi
, value
->value_
, arguments
[0]);
1761 // XXX: I don't even think the user should be allowed to do this
1762 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1764 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1766 const char *encoding(CYPoolCString(pool
, context
, arguments
[1]));
1767 sig::Signature signature
;
1768 sig::Parse(pool
, &signature
, encoding
, &Structor_
);
1769 // XXX: this can try to return null, and I guess then it just fails
1770 return CYCastJSObject(context
, CYMakeFunctor(context
, arguments
[0], false, signature
));
1773 static JSValueRef
CArray_callAsFunction_toPointer(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1774 CArray
*internal(reinterpret_cast<CArray
*>(JSObjectGetPrivate(_this
)));
1775 JSObjectRef
owner(internal
->owner_
?: object
);
1776 return CYMakePointer(context
, internal
->value_
, *internal
->type_
->type_
, NULL
, owner
);
1779 static JSValueRef
CString_callAsFunction_toPointer(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1780 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(_this
)));
1781 JSObjectRef
owner(internal
->owner_
?: object
);
1782 return CYMakePointer(context
, internal
->value_
, sig::Primitive
<char>(), NULL
, owner
);
1785 static JSValueRef Functor_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1787 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(_this
)));
1789 sig::Function
type(internal
->variadic_
);
1790 sig::Copy(pool
, type
.signature
, internal
->signature_
);
1792 return CYMakePointer(context
, reinterpret_cast<void *>(internal
->value_
), type
, NULL
, NULL
);
1795 static JSValueRef
Pointer_callAsFunction_toPointer(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1799 static JSValueRef
CArray_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1800 CArray
*internal(reinterpret_cast<CArray
*>(JSObjectGetPrivate(_this
)));
1801 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1804 static JSValueRef
Pointer_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1805 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1806 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1809 static JSValueRef
Functor_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1810 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(_this
)));
1811 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1814 static JSValueRef
Functor_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1815 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(_this
)));
1816 uint8_t *value(reinterpret_cast<uint8_t *>(internal
->value_
));
1817 _assert(value
!= NULL
);
1821 sig::Function
function(internal
->variadic_
);
1822 sig::Copy(pool
, function
.signature
, internal
->signature_
);
1824 CYPropertyName
*name(internal
->GetName(pool
));
1825 auto typed(CYDecodeType(pool
, &function
));
1827 std::ostringstream str
;
1829 CYOutput
output(*str
.rdbuf(), options
);
1830 output
.pretty_
= true;
1831 (new(pool
) CYExternalExpression(new(pool
) CYString("C"), typed
, name
))->Output(output
, CYNoFlags
);
1832 return CYCastJSValue(context
, CYJSString(str
.str()));
1835 CYPropertyName
*cy::Functor::GetName(CYPool
&pool
) const {
1837 if (dladdr(reinterpret_cast<void *>(value_
), &info
) == 0 || strcmp(info
.dli_sname
, "<redacted>") == 0)
1838 return new(pool
) CYNumber(reinterpret_cast<uintptr_t>(value_
));
1840 std::ostringstream str
;
1841 str
<< info
.dli_sname
;
1842 off_t
offset(reinterpret_cast<uint8_t *>(value_
) - reinterpret_cast<uint8_t *>(info
.dli_saddr
));
1844 str
<< "+0x" << std::hex
<< offset
;
1845 return new(pool
) CYString(pool
.strdup(str
.str().c_str()));
1848 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1849 std::set
<void *> *objects(CYCastObjects(context
, _this
, count
, arguments
));
1851 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1854 JSValueRef
value(CYGetProperty(context
, _this
, cyi_s
));
1855 if (!JSValueIsUndefined(context
, value
)) {
1857 return CYCastJSValue(context
, pool
.strcat("&", CYPoolCCYON(pool
, context
, value
, objects
), NULL
));
1859 } catch (const CYException
&e
) {
1860 // XXX: it might be interesting to include this error
1864 std::ostringstream str
;
1866 sig::Pointer
type(*internal
->type_
->type_
);
1869 CYOutput
output(*str
.rdbuf(), options
);
1870 (new(pool
) CYTypeExpression(CYDecodeType(pool
, &type
)))->Output(output
, CYNoFlags
);
1872 str
<< "(" << internal
->value_
<< ")";
1873 std::string
value(str
.str());
1874 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
1877 static JSValueRef
CString_getProperty_length(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1878 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(object
)));
1879 return CYCastJSValue(context
, strlen(internal
->value_
));
1882 static JSValueRef CString_getProperty_$
cyt(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1883 return CYMakeType(context
, sig::String(true));
1886 static JSValueRef CArray_getProperty_$
cyt(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1887 CArray
*internal(reinterpret_cast<CArray
*>(JSObjectGetPrivate(object
)));
1888 sig::Array
type(*internal
->type_
->type_
, internal
->length_
);
1889 return CYMakeType(context
, type
);
1892 static JSValueRef Pointer_getProperty_$
cyt(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1893 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1894 sig::Pointer
type(*internal
->type_
->type_
);
1895 return CYMakeType(context
, type
);
1898 static JSValueRef
CString_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1899 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(_this
)));
1900 const char *string(internal
->value_
);
1901 std::ostringstream str
;
1906 CYStringify(str
, string
, strlen(string
), CYStringifyModeNative
);
1908 std::string
value(str
.str());
1909 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
1912 static JSValueRef
CString_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1913 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(_this
)));
1914 return CYCastJSValue(context
, internal
->value_
);
1917 static JSValueRef Functor_getProperty_$
cyt(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1918 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1920 sig::Function
type(internal
->variadic_
);
1921 sig::Copy(pool
, type
.signature
, internal
->signature_
);
1922 return CYMakeType(context
, type
);
1925 static JSValueRef
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1926 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1927 return CYCastJSValue(context
, internal
->GetFFI()->alignment
);
1930 static JSValueRef
Type_getProperty_name(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1931 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1932 return CYCastJSValue(context
, internal
->type_
->GetName());
1935 static JSValueRef
Type_getProperty_size(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1936 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1937 return CYCastJSValue(context
, internal
->GetFFI()->size
);
1940 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1941 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1943 const char *type(sig::Unparse(pool
, internal
->type_
));
1944 return CYCastJSValue(context
, CYJSString(type
));
1947 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1948 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1952 CYOutput
output(out
, options
);
1953 output
.pretty_
= true;
1954 (new(pool
) CYTypeExpression(CYDecodeType(pool
, internal
->type_
->Copy(pool
, ""))))->Output(output
, CYNoFlags
);
1955 return CYCastJSValue(context
, CYJSString(out
.str().c_str()));
1958 static JSStaticFunction All_staticFunctions
[2] = {
1959 {"cy$complete", &All_complete_callAsFunction
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1963 static JSStaticFunction CArray_staticFunctions
[3] = {
1964 {"toPointer", &CArray_callAsFunction_toPointer
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1965 {"valueOf", &CArray_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1969 static JSStaticValue CArray_staticValues
[2] = {
1970 {"$cyt", &CArray_getProperty_$cyt
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1971 {NULL
, NULL
, NULL
, 0}
1974 static JSStaticFunction CString_staticFunctions
[5] = {
1975 {"toCYON", &CString_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1976 {"toPointer", &CString_callAsFunction_toPointer
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1977 {"toString", &CString_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1978 {"valueOf", &CString_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1982 static JSStaticValue CString_staticValues
[3] = {
1983 {"length", &CString_getProperty_length
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1984 {"$cyt", &CString_getProperty_$cyt
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1985 {NULL
, NULL
, NULL
, 0}
1988 static JSStaticFunction Pointer_staticFunctions
[4] = {
1989 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1990 {"toPointer", &Pointer_callAsFunction_toPointer
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1991 {"valueOf", &Pointer_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1995 static JSStaticValue Pointer_staticValues
[2] = {
1996 {"$cyt", &Pointer_getProperty_$cyt
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1997 {NULL
, NULL
, NULL
, 0}
2000 static JSStaticFunction Struct_staticFunctions
[2] = {
2001 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2005 static JSStaticValue Struct_staticValues
[2] = {
2006 {"$cyt", &Struct_getProperty_$cyt
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2007 {NULL
, NULL
, NULL
, 0}
2010 static JSStaticFunction Functor_staticFunctions
[5] = {
2011 {"$cya", &Functor_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2012 {"toCYON", &Functor_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2013 {"toPointer", &Functor_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2014 {"valueOf", &Functor_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2018 static JSStaticValue Functor_staticValues
[2] = {
2019 {"$cyt", &Functor_getProperty_$cyt
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2020 {NULL
, NULL
, NULL
, 0}
2023 static JSStaticValue Type_staticValues
[4] = {
2024 {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2025 {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2026 {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2027 {NULL
, NULL
, NULL
, 0}
2030 static JSStaticFunction Type_staticFunctions
[10] = {
2031 {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2032 {"blockWith", &Type_callAsFunction_blockWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2033 {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2034 {"enumFor", &Type_callAsFunction_enumFor
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2035 {"functionWith", &Type_callAsFunction_functionWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2036 {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2037 {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2038 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2039 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2043 _visible
void CYSetArgs(const char *argv0
, const char *script
, int argc
, const char *argv
[]) {
2044 JSContextRef
context(CYGetJSContext());
2045 JSValueRef args
[argc
+ 2];
2046 for (int i(0); i
!= argc
; ++i
)
2047 args
[i
+ 2] = CYCastJSValue(context
, argv
[i
]);
2054 args
[1] = CYCastJSValue(context
, CYJSString(script
));
2057 args
[offset
] = CYCastJSValue(context
, CYJSString(argv0
));
2059 CYSetProperty(context
, CYGetCachedObject(context
, CYJSString("System")), CYJSString("args"), CYObjectMakeArray(context
, argc
, args
+ 2));
2060 CYSetProperty(context
, CYGetCachedObject(context
, CYJSString("process")), CYJSString("argv"), CYObjectMakeArray(context
, argc
+ 2 - offset
, args
+ offset
));
2063 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
2064 return JSContextGetGlobalObject(context
);
2067 // XXX: this is neither exceptin safe nor even terribly sane
2068 class ExecutionHandle
{
2070 JSContextRef context_
;
2071 std::vector
<void *> handles_
;
2074 ExecutionHandle(JSContextRef context
) :
2077 handles_
.resize(GetHooks().size());
2078 for (size_t i(0); i
!= GetHooks().size(); ++i
) {
2079 CYHook
*hook(GetHooks()[i
]);
2080 if (hook
->ExecuteStart
!= NULL
)
2081 handles_
[i
] = (*hook
->ExecuteStart
)(context_
);
2087 ~ExecutionHandle() {
2088 for (size_t i(GetHooks().size()); i
!= 0; --i
) {
2089 CYHook
*hook(GetHooks()[i
-1]);
2090 if (hook
->ExecuteEnd
!= NULL
)
2091 (*hook
->ExecuteEnd
)(context_
, handles_
[i
-1]);
2097 static volatile bool cancel_
;
2099 static bool CYShouldTerminate(JSContextRef context
, void *arg
) {
2104 _visible
const char *CYExecute(JSContextRef context
, CYPool
&pool
, CYUTF8String code
) {
2105 ExecutionHandle
handle(context
);
2109 if (&JSContextGroupSetExecutionTimeLimit
!= NULL
)
2110 JSContextGroupSetExecutionTimeLimit(JSContextGetGroup(context
), 0.5, &CYShouldTerminate
, NULL
);
2114 JSValueRef
result(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0));
2115 if (JSValueIsUndefined(context
, result
))
2118 std::set
<void *> objects
;
2119 const char *json(_jsccall(CYPoolCCYON
, pool
, context
, result
, objects
));
2120 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
2123 } catch (const CYException
&error
) {
2124 return pool
.strcat("throw ", error
.PoolCString(pool
), NULL
);
2129 _visible
void CYCancel() {
2134 const char *CYPoolLibraryPath(CYPool
&pool
);
2136 static bool initialized_
= false;
2138 void CYInitializeDynamic() {
2140 initialized_
= true;
2144 const char *db(pool
.strcat(CYPoolLibraryPath(pool
), "/libcycript.db", NULL
));
2145 _sqlcall(sqlite3_open_v2(db
, &database_
, SQLITE_OPEN_READONLY
, NULL
));
2147 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
2148 JSSynchronousGarbageCollectForDebugging$
= reinterpret_cast<void (*)(JSContextRef
)>(dlsym(RTLD_DEFAULT
, "JSSynchronousGarbageCollectForDebugging"));
2150 JSClassDefinition definition
;
2152 definition
= kJSClassDefinitionEmpty
;
2153 definition
.attributes
= kJSClassAttributeNoAutomaticPrototype
;
2154 definition
.className
= "All";
2155 definition
.staticFunctions
= All_staticFunctions
;
2156 definition
.hasProperty
= &All_hasProperty
;
2157 definition
.getProperty
= &All_getProperty
;
2158 definition
.getPropertyNames
= &All_getPropertyNames
;
2159 All_
= JSClassCreate(&definition
);
2161 definition
= kJSClassDefinitionEmpty
;
2162 definition
.attributes
= kJSClassAttributeNoAutomaticPrototype
;
2163 definition
.className
= "Context";
2164 definition
.finalize
= &CYFinalize
;
2165 CYPrivate
<Context
>::Class_
= JSClassCreate(&definition
);
2167 definition
= kJSClassDefinitionEmpty
;
2168 definition
.className
= "CArray";
2169 definition
.staticFunctions
= CArray_staticFunctions
;
2170 definition
.staticValues
= CArray_staticValues
;
2171 definition
.getProperty
= &CArray_getProperty
;
2172 definition
.setProperty
= &CArray_setProperty
;
2173 definition
.finalize
= &CYFinalize
;
2174 CYPrivate
<CArray
>::Class_
= JSClassCreate(&definition
);
2176 definition
= kJSClassDefinitionEmpty
;
2177 definition
.className
= "CString";
2178 definition
.staticFunctions
= CString_staticFunctions
;
2179 definition
.staticValues
= CString_staticValues
;
2180 definition
.getProperty
= &CString_getProperty
;
2181 definition
.setProperty
= &CString_setProperty
;
2182 definition
.finalize
= &CYFinalize
;
2183 CYPrivate
<CString
>::Class_
= JSClassCreate(&definition
);
2185 definition
= kJSClassDefinitionEmpty
;
2186 definition
.className
= "Functor";
2187 definition
.staticFunctions
= Functor_staticFunctions
;
2188 definition
.staticValues
= Functor_staticValues
;
2189 definition
.callAsFunction
= &Functor_callAsFunction
;
2190 definition
.finalize
= &CYFinalize
;
2191 cy::Functor::Class_
= JSClassCreate(&definition
);
2193 definition
= kJSClassDefinitionEmpty
;
2194 definition
.className
= "Pointer";
2195 definition
.staticFunctions
= Pointer_staticFunctions
;
2196 definition
.staticValues
= Pointer_staticValues
;
2197 definition
.callAsFunction
= &Pointer_callAsFunction
;
2198 definition
.getProperty
= &Pointer_getProperty
;
2199 definition
.setProperty
= &Pointer_setProperty
;
2200 definition
.finalize
= &CYFinalize
;
2201 CYPrivate
<Pointer
>::Class_
= JSClassCreate(&definition
);
2203 definition
= kJSClassDefinitionEmpty
;
2204 definition
.className
= "Root";
2205 definition
.finalize
= &CYFinalize
;
2206 CYPrivate
<CYRoot
>::Class_
= JSClassCreate(&definition
);
2208 definition
= kJSClassDefinitionEmpty
;
2209 definition
.className
= "Struct";
2210 definition
.staticFunctions
= Struct_staticFunctions
;
2211 definition
.staticValues
= Struct_staticValues
;
2212 definition
.getProperty
= &Struct_getProperty
;
2213 definition
.setProperty
= &Struct_setProperty
;
2214 definition
.getPropertyNames
= &Struct_getPropertyNames
;
2215 definition
.finalize
= &CYFinalize
;
2216 CYPrivate
<Struct_privateData
>::Class_
= JSClassCreate(&definition
);
2218 definition
= kJSClassDefinitionEmpty
;
2219 definition
.className
= "Type";
2220 definition
.staticValues
= Type_staticValues
;
2221 definition
.staticFunctions
= Type_staticFunctions
;
2222 definition
.callAsFunction
= &Type_callAsFunction
;
2223 definition
.callAsConstructor
= &Type_callAsConstructor
;
2224 definition
.finalize
= &CYFinalize
;
2225 CYPrivate
<Type_privateData
>::Class_
= JSClassCreate(&definition
);
2227 definition
= kJSClassDefinitionEmpty
;
2228 definition
.className
= "Global";
2229 //definition.getProperty = &Global_getProperty;
2230 Global_
= JSClassCreate(&definition
);
2232 Array_s
= JSStringCreateWithUTF8CString("Array");
2233 constructor_s
= JSStringCreateWithUTF8CString("constructor");
2234 cy_s
= JSStringCreateWithUTF8CString("$cy");
2235 cyi_s
= JSStringCreateWithUTF8CString("$cyi");
2236 cyt_s
= JSStringCreateWithUTF8CString("$cyt");
2237 length_s
= JSStringCreateWithUTF8CString("length");
2238 message_s
= JSStringCreateWithUTF8CString("message");
2239 name_s
= JSStringCreateWithUTF8CString("name");
2240 pop_s
= JSStringCreateWithUTF8CString("pop");
2241 prototype_s
= JSStringCreateWithUTF8CString("prototype");
2242 push_s
= JSStringCreateWithUTF8CString("push");
2243 splice_s
= JSStringCreateWithUTF8CString("splice");
2244 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
2245 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
2246 toPointer_s
= JSStringCreateWithUTF8CString("toPointer");
2247 toString_s
= JSStringCreateWithUTF8CString("toString");
2248 weak_s
= JSStringCreateWithUTF8CString("weak");
2250 Result_
= JSStringCreateWithUTF8CString("_");
2252 for (CYHook
*hook
: GetHooks())
2253 if (hook
->Initialize
!= NULL
)
2254 (*hook
->Initialize
)();
2257 void CYThrow(JSContextRef context
, JSValueRef value
) {
2259 throw CYJSError(context
, value
);
2262 const char *CYJSError::PoolCString(CYPool
&pool
) const {
2263 std::set
<void *> objects
;
2264 // XXX: this used to be CYPoolCString
2265 return CYPoolCCYON(pool
, context_
, value_
, objects
);
2268 JSValueRef
CYJSError::CastJSValue(JSContextRef context
, const char *name
) const {
2269 // XXX: what if the context is different? or the name? I dunno. ("epic" :/)
2273 JSValueRef
CYCastJSError(JSContextRef context
, const char *name
, const char *message
) {
2274 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString(name
)));
2275 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
2276 return _jsccall(JSObjectCallAsConstructor
, context
, Error
, 1, arguments
);
2279 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
, const char *name
) const {
2280 return CYCastJSError(context
, name
, message_
);
2283 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
2284 _assert(context
!= NULL
);
2289 va_start(args
, format
);
2290 // XXX: there might be a beter way to think about this
2291 const char *message(pool
.vsprintf(64, format
, args
));
2294 value_
= CYCastJSError(context
, "Error", message
);
2297 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
2298 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
2302 char *CYPoolLibraryPath_(CYPool
&pool
) {
2303 FILE *maps(fopen("/proc/self/maps", "r"));
2304 struct F
{ FILE *f
; F(FILE *f
) : f(f
) {}
2305 ~F() { fclose(f
); } } f(maps
);
2307 size_t function(reinterpret_cast<size_t>(&CYPoolLibraryPath
));
2310 size_t start
; size_t end
; char flags
[8]; unsigned long long offset
;
2311 int major
; int minor
; unsigned long long inode
; char file
[1024];
2312 int count(fscanf(maps
, "%zx-%zx %7s %llx %x:%x %llu%[ ]%1024[^\n]\n",
2313 &start
, &end
, flags
, &offset
, &major
, &minor
, &inode
, file
, file
));
2314 if (count
< 8) break; else if (start
<= function
&& function
< end
)
2315 return pool
.strdup(file
);
2321 char *CYPoolLibraryPath_(CYPool
&pool
) {
2323 _assert(dladdr(reinterpret_cast<void *>(&CYPoolLibraryPath
), &addr
) != 0);
2324 return pool
.strdup(addr
.dli_fname
);
2328 const char *CYPoolLibraryPath(CYPool
&pool
) {
2329 char *lib(CYPoolLibraryPath_(pool
));
2331 char *slash(strrchr(lib
, '/'));
2336 slash
= strrchr(lib
, '/');
2337 if (slash
!= NULL
) {
2338 if (strcmp(slash
, "/.libs") == 0)
2340 } else if (strcmp(lib
, ".libs") == 0)
2346 static JSValueRef
require_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
2347 _assert(count
== 1);
2350 CYUTF8String
name(CYPoolUTF8String(pool
, context
, CYJSString(context
, arguments
[0])));
2351 if (memchr(name
.data
, '/', name
.size
) == NULL
&& (
2353 dlopen(pool
.strcat("/System/Library/Frameworks/", name
.data
, ".framework/", name
.data
, NULL
), RTLD_LAZY
| RTLD_GLOBAL
) != NULL
||
2354 dlopen(pool
.strcat("/System/Library/PrivateFrameworks/", name
.data
, ".framework/", name
.data
, NULL
), RTLD_LAZY
| RTLD_GLOBAL
) != NULL
||
2357 return CYJSUndefined(context
);
2362 sqlite3_stmt
*statement
;
2364 _sqlcall(sqlite3_prepare(database_
,
2366 "\"module\".\"code\", "
2367 "\"module\".\"flags\" "
2370 " \"module\".\"name\" = ?"
2372 , -1, &statement
, NULL
));
2374 _sqlcall(sqlite3_bind_text(statement
, 1, name
.data
, name
.size
, SQLITE_STATIC
));
2376 if (_sqlcall(sqlite3_step(statement
)) != SQLITE_DONE
) {
2377 code
.data
= static_cast<const char *>(sqlite3_column_blob(statement
, 0));
2378 code
.size
= sqlite3_column_bytes(statement
, 0);
2379 path
= CYJSString(name
);
2380 code
= CYPoolUTF8String(pool
, code
);
2382 JSObjectRef
resolve(CYCastJSObject(context
, CYGetProperty(context
, object
, CYJSString("resolve"))));
2383 path
= CYJSString(context
, CYCallAsFunction(context
, resolve
, NULL
, 1, arguments
));
2386 _sqlcall(sqlite3_finalize(statement
));
2388 CYJSString
property("exports");
2390 JSObjectRef
modules(CYGetCachedObject(context
, CYJSString("modules")));
2391 JSValueRef
cache(CYGetProperty(context
, modules
, path
));
2394 if (!JSValueIsUndefined(context
, cache
)) {
2395 JSObjectRef
module(CYCastJSObject(context
, cache
));
2396 result
= CYGetProperty(context
, module, property
);
2398 if (code
.data
== NULL
) {
2399 code
= CYPoolFileUTF8String(pool
, CYPoolCString(pool
, context
, path
));
2400 _assert(code
.data
!= NULL
);
2403 size_t length(name
.size
);
2404 if (length
>= 5 && strncmp(name
.data
+ length
- 5, ".json", 5) == 0) {
2405 JSObjectRef
JSON(CYGetCachedObject(context
, CYJSString("JSON")));
2406 JSObjectRef
parse(CYCastJSObject(context
, CYGetProperty(context
, JSON
, CYJSString("parse"))));
2407 JSValueRef arguments
[1] = { CYCastJSValue(context
, CYJSString(code
)) };
2408 result
= CYCallAsFunction(context
, parse
, JSON
, 1, arguments
);
2410 JSObjectRef
module(JSObjectMake(context
, NULL
, NULL
));
2411 CYSetProperty(context
, modules
, path
, module);
2413 JSObjectRef
exports(JSObjectMake(context
, NULL
, NULL
));
2414 CYSetProperty(context
, module, property
, exports
);
2416 std::stringstream wrap
;
2417 wrap
<< "(function (exports, require, module, __filename) { " << code
<< "\n});";
2418 code
= CYPoolCode(pool
, *wrap
.rdbuf());
2420 JSValueRef
value(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0));
2421 JSObjectRef
function(CYCastJSObject(context
, value
));
2423 JSValueRef arguments
[4] = { exports
, object
, module, CYCastJSValue(context
, path
) };
2424 CYCallAsFunction(context
, function
, NULL
, 4, arguments
);
2425 result
= CYGetProperty(context
, module, property
);
2432 static bool CYRunScript(JSGlobalContextRef context
, const char *path
) {
2434 CYUTF8String
code(CYPoolFileUTF8String(pool
, pool
.strcat(CYPoolLibraryPath(pool
), path
, NULL
)));
2435 if (code
.data
== NULL
)
2438 code
= CYPoolCode(pool
, code
);
2439 _jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0);
2443 extern "C" void CYDestroyWeak(JSWeakObjectMapRef weak
, void *data
) {
2446 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
2447 CYInitializeDynamic();
2449 JSObjectRef
global(CYGetGlobalObject(context
));
2451 JSObjectRef
cy(CYPrivate
<Context
>::Make(context
, context
));
2452 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
2454 /* Cache Globals {{{ */
2455 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
2456 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
2458 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
2459 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
2461 JSObjectRef
Boolean(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Boolean"))));
2462 CYSetProperty(context
, cy
, CYJSString("Boolean"), Boolean
);
2464 JSObjectRef
Boolean_prototype(CYCastJSObject(context
, CYGetProperty(context
, Boolean
, prototype_s
)));
2465 CYSetProperty(context
, cy
, CYJSString("Boolean_prototype"), Boolean_prototype
);
2467 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
2468 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
2470 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
2471 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
2473 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
2474 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
2476 JSObjectRef
JSON(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("JSON"))));
2477 CYSetProperty(context
, cy
, CYJSString("JSON"), JSON
);
2479 JSObjectRef
Number(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Number"))));
2480 CYSetProperty(context
, cy
, CYJSString("Number"), Number
);
2482 JSObjectRef
Number_prototype(CYCastJSObject(context
, CYGetProperty(context
, Number
, prototype_s
)));
2483 CYSetProperty(context
, cy
, CYJSString("Number_prototype"), Number_prototype
);
2485 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
2486 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
2488 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
2489 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
2491 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
2492 CYSetProperty(context
, cy
, CYJSString("String"), String
);
2494 JSObjectRef
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
)));
2495 CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
);
2497 JSObjectRef
SyntaxError(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("SyntaxError"))));
2498 CYSetProperty(context
, cy
, CYJSString("SyntaxError"), SyntaxError
);
2501 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
2502 CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
2504 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
2505 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
2506 CYSetProperty(context
, cycript
, CYJSString("compile"), &Cycript_compile_callAsFunction
);
2507 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
2509 JSObjectRef
CArray(JSObjectMakeConstructor(context
, CYPrivate
<::CArray
>::Class_
, &CArray_new
));
2510 CYSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, CArray
, prototype_s
)), Array_prototype
);
2511 CYSetProperty(context
, cycript
, CYJSString("CArray"), CArray
);
2513 JSObjectRef
CString(JSObjectMakeConstructor(context
, CYPrivate
<::CString
>::Class_
, &CString_new
));
2514 CYSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, CString
, prototype_s
)), String_prototype
);
2515 CYSetProperty(context
, cycript
, CYJSString("CString"), CString
);
2517 JSObjectRef
Functor(JSObjectMakeConstructor(context
, cy::Functor::Class_
, &Functor_new
));
2518 JSObjectRef
Functor_prototype(CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)));
2519 CYSetPrototype(context
, Functor_prototype
, Function_prototype
);
2520 CYSetProperty(context
, cy
, CYJSString("Functor_prototype"), Functor_prototype
);
2521 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
2523 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, CYPrivate
<Pointer
>::Class_
, &Pointer_new
));
2525 JSObjectRef
Type(JSObjectMakeConstructor(context
, CYPrivate
<Type_privateData
>::Class_
, &Type_new
));
2526 JSObjectRef
Type_prototype(CYCastJSObject(context
, CYGetProperty(context
, Type
, prototype_s
)));
2527 CYSetPrototype(context
, Type_prototype
, Function_prototype
);
2528 CYSetProperty(context
, cy
, CYJSString("Type_prototype"), Type_prototype
);
2529 CYSetProperty(context
, cycript
, CYJSString("Type"), Type
);
2531 JSObjectRef
Character_prototype(JSObjectMake(context
, NULL
, NULL
));
2532 CYSetPrototype(context
, Character_prototype
, String_prototype
);
2533 CYSetProperty(context
, cy
, CYJSString("Character_prototype"), Character_prototype
);
2534 CYSetProperty(context
, Character_prototype
, CYJSString("valueOf"), _jsccall(JSEvaluateScript
, context
, CYJSString("(function(){return this.charCodeAt(0);})"), NULL
, NULL
, 0));
2536 JSObjectRef
modules(JSObjectMake(context
, NULL
, NULL
));
2537 CYSetProperty(context
, cy
, CYJSString("modules"), modules
);
2539 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
2540 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
2542 JSObjectRef
cache(JSObjectMake(context
, NULL
, NULL
));
2543 CYSetProperty(context
, cy
, CYJSString("cache"), cache
);
2544 CYSetPrototype(context
, cache
, all
);
2546 JSObjectRef
alls(_jsccall(JSObjectCallAsConstructor
, context
, Array
, 0, NULL
));
2547 CYSetProperty(context
, cycript
, CYJSString("alls"), alls
);
2550 JSObjectRef
last(NULL
), curr(global
);
2552 goto next
; for (JSValueRef next
;;) {
2553 if (JSValueIsNull(context
, next
))
2556 curr
= CYCastJSObject(context
, next
);
2558 next
= JSObjectGetPrototype(context
, curr
);
2561 CYSetPrototype(context
, last
, cache
);
2565 if (&JSWeakObjectMapCreate
!= NULL
) {
2566 JSWeakObjectMapRef
weak(JSWeakObjectMapCreate(context
, NULL
, &CYDestroyWeak
));
2567 CYSetProperty(context
, cy
, weak_s
, CYCastJSValue(context
, reinterpret_cast<uintptr_t>(weak
)));
2571 CYSetProperty(context
, String_prototype
, cyt_s
, CYMakeType(context
, sig::String(true)), kJSPropertyAttributeDontEnum
);
2573 CYSetProperty(context
, cache
, CYJSString("dlerror"), CYMakeFunctor(context
, "dlerror", "*"), kJSPropertyAttributeDontEnum
);
2574 CYSetProperty(context
, cache
, CYJSString("RTLD_DEFAULT"), CYCastJSValue(context
, reinterpret_cast<intptr_t>(RTLD_DEFAULT
)), kJSPropertyAttributeDontEnum
);
2575 CYSetProperty(context
, cache
, CYJSString("dlsym"), CYMakeFunctor(context
, "dlsym", "^v^v*"), kJSPropertyAttributeDontEnum
);
2577 CYSetProperty(context
, cache
, CYJSString("NULL"), CYJSNull(context
), kJSPropertyAttributeDontEnum
);
2579 CYSetProperty(context
, cache
, CYJSString("bool"), CYMakeType(context
, sig::Primitive
<bool>()), kJSPropertyAttributeDontEnum
);
2580 CYSetProperty(context
, cache
, CYJSString("char"), CYMakeType(context
, sig::Primitive
<char>()), kJSPropertyAttributeDontEnum
);
2581 CYSetProperty(context
, cache
, CYJSString("schar"), CYMakeType(context
, sig::Primitive
<signed char>()), kJSPropertyAttributeDontEnum
);
2582 CYSetProperty(context
, cache
, CYJSString("uchar"), CYMakeType(context
, sig::Primitive
<unsigned char>()), kJSPropertyAttributeDontEnum
);
2584 CYSetProperty(context
, cache
, CYJSString("short"), CYMakeType(context
, sig::Primitive
<short>()), kJSPropertyAttributeDontEnum
);
2585 CYSetProperty(context
, cache
, CYJSString("int"), CYMakeType(context
, sig::Primitive
<int>()), kJSPropertyAttributeDontEnum
);
2586 CYSetProperty(context
, cache
, CYJSString("long"), CYMakeType(context
, sig::Primitive
<long>()), kJSPropertyAttributeDontEnum
);
2587 CYSetProperty(context
, cache
, CYJSString("longlong"), CYMakeType(context
, sig::Primitive
<long long>()), kJSPropertyAttributeDontEnum
);
2589 CYSetProperty(context
, cache
, CYJSString("ushort"), CYMakeType(context
, sig::Primitive
<unsigned short>()), kJSPropertyAttributeDontEnum
);
2590 CYSetProperty(context
, cache
, CYJSString("uint"), CYMakeType(context
, sig::Primitive
<unsigned int>()), kJSPropertyAttributeDontEnum
);
2591 CYSetProperty(context
, cache
, CYJSString("ulong"), CYMakeType(context
, sig::Primitive
<unsigned long>()), kJSPropertyAttributeDontEnum
);
2592 CYSetProperty(context
, cache
, CYJSString("ulonglong"), CYMakeType(context
, sig::Primitive
<unsigned long long>()), kJSPropertyAttributeDontEnum
);
2594 #ifdef __SIZEOF_INT128__
2595 CYSetProperty(context
, cache
, CYJSString("int128"), CYMakeType(context
, sig::Primitive
<__int128
>()), kJSPropertyAttributeDontEnum
);
2596 CYSetProperty(context
, cache
, CYJSString("uint128"), CYMakeType(context
, sig::Primitive
<unsigned __int128
>()), kJSPropertyAttributeDontEnum
);
2599 CYSetProperty(context
, cache
, CYJSString("float"), CYMakeType(context
, sig::Primitive
<float>()), kJSPropertyAttributeDontEnum
);
2600 CYSetProperty(context
, cache
, CYJSString("double"), CYMakeType(context
, sig::Primitive
<double>()), kJSPropertyAttributeDontEnum
);
2601 CYSetProperty(context
, cache
, CYJSString("longdouble"), CYMakeType(context
, sig::Primitive
<long double>()), kJSPropertyAttributeDontEnum
);
2603 CYSetProperty(context
, global
, CYJSString("require"), &require_callAsFunction
, kJSPropertyAttributeDontEnum
);
2605 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
2606 CYSetProperty(context
, all
, CYJSString("system"), System
);
2607 System
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("system")));
2608 CYSetProperty(context
, cy
, CYJSString("System"), System
);
2610 JSObjectRef
process(JSObjectMake(context
, NULL
, NULL
));
2611 CYSetProperty(context
, global
, CYJSString("process"), process
);
2612 CYSetProperty(context
, cy
, CYJSString("process"), process
);
2614 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
2615 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
2617 CYSetProperty(context
, global
, CYJSString("global"), global
);
2618 CYSetProperty(context
, global
, CYJSString("print"), &Global_print
);
2620 for (CYHook
*hook
: GetHooks())
2621 if (hook
->SetupContext
!= NULL
)
2622 (*hook
->SetupContext
)(context
);
2624 CYArrayPush(context
, alls
, cycript
);
2626 CYRunScript(context
, "/libcycript.cy");
2629 static JSGlobalContextRef context_
;
2631 _visible JSGlobalContextRef
CYGetJSContext() {
2632 CYInitializeDynamic();
2634 if (context_
== NULL
) {
2635 context_
= JSGlobalContextCreate(Global_
);
2636 CYSetupContext(context_
);
2642 _visible
void CYDestroyContext() {
2643 if (context_
== NULL
)
2645 JSGlobalContextRelease(context_
);