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 void *CYCastPointer_(JSContextRef context
, JSValueRef value
, bool *guess
) {
677 else switch (JSValueGetType(context
, value
)) {
680 case kJSTypeObject
: {
681 JSObjectRef
object((JSObjectRef
) value
);
682 if (JSValueIsObjectOfClass(context
, value
, CYPrivate
<Pointer
>::Class_
)) {
683 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
684 return internal
->value_
;
686 JSValueRef
toPointer(CYGetProperty(context
, object
, toPointer_s
));
687 if (CYIsCallable(context
, toPointer
)) {
688 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
));
689 _assert(value
!= NULL
);
690 return CYCastPointer_(context
, value
, guess
);
696 double number(CYCastDouble(context
, value
));
697 if (!std::isnan(number
))
698 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
700 throw CYJSError(context
, "cannot convert value to pointer");
708 static JSValueRef
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
);
712 // XXX: this is somehow not quite a template :/
715 void Primitive
<bool>::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
716 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
719 #define CYPoolFFI_(Type_) \
721 void Primitive<Type_>::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const { \
722 *reinterpret_cast<Type_ *>(data) = CYCastDouble(context, value); \
728 CYPoolFFI_(long double)
730 CYPoolFFI_(signed char)
731 CYPoolFFI_(signed int)
732 CYPoolFFI_(signed long int)
733 CYPoolFFI_(signed long long int)
734 CYPoolFFI_(signed short int)
736 CYPoolFFI_(unsigned char)
737 CYPoolFFI_(unsigned int)
738 CYPoolFFI_(unsigned long int)
739 CYPoolFFI_(unsigned long long int)
740 CYPoolFFI_(unsigned short int)
742 #ifdef __SIZEOF_INT128__
743 CYPoolFFI_(signed __int128
)
744 CYPoolFFI_(unsigned __int128
)
748 void Primitive
<char>::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
749 if (JSValueGetType(context
, value
) != kJSTypeString
)
750 *reinterpret_cast<char *>(data
) = CYCastDouble(context
, value
);
752 CYJSString
script(context
, value
);
753 auto string(CYCastUTF16String(script
));
754 _assert(string
.size
== 1);
755 _assert((string
.data
[0] & 0xff) == string
.data
[0]);
756 *reinterpret_cast<char *>(data
) = string
.data
[0];
760 void Void::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
761 _assert(JSValueIsUndefined(context
, value
));
764 void Unknown::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
768 void String::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
770 *reinterpret_cast<const char **>(data
) = CYCastPointer
<const char *>(context
, value
, &guess
);
771 if (guess
&& pool
!= NULL
)
772 *reinterpret_cast<const char **>(data
) = CYPoolCString(*pool
, context
, value
);
775 void Bits::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
779 static void CYArrayCopy(CYPool
*pool
, JSContextRef context
, uint8_t *base
, size_t length
, const sig::Type
&type
, ffi_type
*ffi
, JSObjectRef object
) {
780 for (size_t index(0); index
!= length
; ++index
) {
781 JSValueRef
rhs(CYGetProperty(context
, object
, index
));
782 if (JSValueIsUndefined(context
, rhs
))
783 throw CYJSError(context
, "unable to extract array value");
784 type
.PoolFFI(pool
, context
, ffi
, base
, rhs
);
789 void Pointer::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
791 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
, &guess
);
792 if (!guess
|| pool
== NULL
|| !JSValueIsObject(context
, value
))
795 JSObjectRef
object(CYCastJSObject(context
, value
));
797 if (sig::Function
*function
= dynamic_cast<sig::Function
*>(&type
)) {
798 _assert(!function
->variadic
);
799 auto internal(CYMakeFunctor_(context
, object
, function
->signature
, &FunctionAdapter_
));
800 // XXX: see notes in Library.cpp about needing to leak
801 *reinterpret_cast<void (**)()>(data
) = internal
->value_
;
802 } else if (CYHasProperty(context
, object
, length_s
)) {
803 size_t length(CYArrayLength(context
, object
));
804 ffi_type
*element(type
.GetFFI(*pool
));
805 size_t size(element
->size
* length
);
806 uint8_t *base(pool
->malloc
<uint8_t>(size
, element
->alignment
));
807 CYArrayCopy(pool
, context
, base
, length
, type
, element
, object
);
808 *reinterpret_cast<void **>(data
) = base
;
812 void Array::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
815 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
816 CYArrayCopy(pool
, context
, base
, size
, type
, ffi
->elements
[0], CYCastJSObject(context
, value
));
819 void Enum::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
820 return type
.PoolFFI(pool
, context
, ffi
, data
, value
);
823 void Aggregate::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
825 _assert(signature
.count
!= _not(size_t));
828 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
829 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
830 for (size_t index(0); index
!= signature
.count
; ++index
) {
831 sig::Element
*element(&signature
.elements
[index
]);
832 ffi_type
*field(ffi
->elements
[index
]);
835 if (aggregate
== NULL
)
838 rhs
= CYGetProperty(context
, aggregate
, index
);
839 if (JSValueIsUndefined(context
, rhs
)) {
840 if (element
->name
!= NULL
)
841 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
844 if (JSValueIsUndefined(context
, rhs
)) undefined
:
845 throw CYJSError(context
, "unable to extract structure value");
849 element
->type
->PoolFFI(pool
, context
, field
, base
+ offset
, rhs
);
850 offset
+= field
->size
;
851 CYAlign(offset
, field
->alignment
);
855 void Function::PoolFFI(CYPool
*pool
, JSContextRef context
, ffi_type
*ffi
, void *data
, JSValueRef value
) const {
859 #define CYFromFFI_(Type_) \
861 JSValueRef Primitive<Type_>::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { \
862 JSValueRef value(CYCastJSValue(context, *reinterpret_cast<Type_ *>(data))); \
863 JSObjectRef typed(_jsccall(JSObjectCallAsConstructor, context, CYGetCachedObject(context, CYJSString("Number")), 1, &value)); \
864 CYSetProperty(context, typed, cyt_s, CYMakeType(context, *this), kJSPropertyAttributeDontEnum); \
871 CYFromFFI_(long double)
873 CYFromFFI_(signed char)
874 CYFromFFI_(signed int)
875 CYFromFFI_(signed long int)
876 CYFromFFI_(signed long long int)
877 CYFromFFI_(signed short int)
879 CYFromFFI_(unsigned char)
880 CYFromFFI_(unsigned int)
881 CYFromFFI_(unsigned long int)
882 CYFromFFI_(unsigned long long int)
883 CYFromFFI_(unsigned short int)
885 #ifdef __SIZEOF_INT128__
886 CYFromFFI_(signed __int128
)
887 CYFromFFI_(unsigned __int128
)
891 JSValueRef Primitive
<bool>::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
892 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
896 JSValueRef Primitive
<char>::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
897 uint16_t string(uint8_t(*reinterpret_cast<char *>(data
)));
898 JSValueRef
value(CYCastJSValue(context
, CYJSString(CYUTF16String(&string
, 1))));
899 JSObjectRef
typed(_jsccall(JSObjectCallAsConstructor
, context
, CYGetCachedObject(context
, CYJSString("String")), 1, &value
));
900 CYSetProperty(context
, typed
, cyt_s
, CYMakeType(context
, sig::Primitive
<char>()), kJSPropertyAttributeDontEnum
);
901 CYSetPrototype(context
, typed
, CYGetCachedValue(context
, CYJSString("Character_prototype")));
905 JSValueRef
Void::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
906 return CYJSUndefined(context
);
909 JSValueRef
Unknown::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
913 JSValueRef
String::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
914 if (char *value
= *reinterpret_cast<char **>(data
))
915 return CYPrivate
<CString
>::Make(context
, value
, context
, owner
);
916 return CYJSNull(context
);
919 JSValueRef
Bits::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
923 JSValueRef
Pointer::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
924 if (void *value
= *reinterpret_cast<void **>(data
))
925 return CYMakePointer(context
, value
, type
, NULL
, owner
);
926 return CYJSNull(context
);
929 JSValueRef
Array::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
930 return CYPrivate
<CArray
>::Make(context
, data
, size
, type
, ffi
->elements
[0], context
, owner
);
933 JSValueRef
Enum::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
934 return type
.FromFFI(context
, ffi
, data
, initialize
, owner
);
937 JSValueRef
Aggregate::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
939 _assert(signature
.count
!= _not(size_t));
940 return CYPrivate
<Struct_privateData
>::Make(context
, data
, *this, ffi
, context
, owner
);
943 JSValueRef
Function::FromFFI(JSContextRef context
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) const {
944 return CYMakeFunctor(context
, reinterpret_cast<void (*)()>(data
), variadic
, signature
);
949 void CYExecuteClosure(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
950 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
952 JSContextRef
context(internal
->function_
);
954 size_t count(internal
->cif_
.nargs
);
955 JSValueRef values
[count
];
957 for (size_t index(0); index
!= count
; ++index
)
958 values
[index
] = internal
->signature_
.elements
[1 + index
].type
->FromFFI(context
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
960 JSValueRef
value(internal
->adapter_(context
, count
, values
, internal
->function_
));
961 internal
->signature_
.elements
[0].type
->PoolFFI(NULL
, context
, internal
->cif_
.rtype
, result
, value
);
964 static JSValueRef
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
) {
965 return CYCallAsFunction(context
, function
, NULL
, count
, values
);
968 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
969 static void CYFreeFunctor(void *data
) {
970 ffi_closure_free(data
);
973 static void CYFreeFunctor(void *data
) {
974 _syscall(munmap(data
, sizeof(ffi_closure
)));
978 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
, JSValueRef (*adapter
)(JSContextRef
, size_t, JSValueRef
[], JSObjectRef
)) {
979 // XXX: in case of exceptions this will leak
980 Closure_privateData
*internal(new Closure_privateData(context
, function
, adapter
, signature
));
982 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
984 ffi_closure
*writable(reinterpret_cast<ffi_closure
*>(ffi_closure_alloc(sizeof(ffi_closure
), &executable
)));
986 ffi_status
status(ffi_prep_closure_loc(writable
, &internal
->cif_
, &CYExecuteClosure
, internal
, executable
));
987 _assert(status
== FFI_OK
);
989 internal
->pool_
->atexit(&CYFreeFunctor
, writable
);
990 internal
->value_
= reinterpret_cast<void (*)()>(executable
);
992 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
993 NULL
, sizeof(ffi_closure
),
994 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
998 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, &CYExecuteClosure
, internal
));
999 _assert(status
== FFI_OK
);
1001 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
1003 internal
->pool_
->atexit(&CYFreeFunctor
, closure
);
1004 internal
->value_
= reinterpret_cast<void (*)()>(closure
);
1010 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
) {
1011 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, signature
, &FunctionAdapter_
));
1012 JSObjectRef
object(JSObjectMake(context
, cy::Functor::Class_
, internal
));
1013 // XXX: see above notes about needing to leak
1014 JSValueProtect(CYGetJSContext(context
), object
);
1018 JSValueRef
CYGetCachedValue(JSContextRef context
, JSStringRef name
) {
1019 return CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
);
1022 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
1023 return CYCastJSObject(context
, CYGetCachedValue(context
, name
));
1026 static JSValueRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, bool variadic
, const sig::Signature
&signature
) {
1027 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
1029 bool function(_jsccall(JSValueIsInstanceOfConstructor
, context
, value
, Function
));
1031 JSObjectRef
function(CYCastJSObject(context
, value
));
1032 return CYMakeFunctor(context
, function
, signature
);
1034 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
1035 return CYMakeFunctor(context
, function
, variadic
, signature
);
1039 static JSValueRef
CString_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1041 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(object
)));
1044 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
1046 else if (!CYGetOffset(pool
, context
, property
, offset
))
1049 sig::Primitive
<char> type
;
1050 return type
.FromFFI(context
, type
.GetFFI(pool
), internal
->value_
+ offset
, false, NULL
);
1053 static bool CString_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1055 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(object
)));
1058 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
1060 else if (!CYGetOffset(pool
, context
, property
, offset
))
1063 sig::Primitive
<char> type
;
1064 type
.PoolFFI(NULL
, context
, type
.GetFFI(pool
), internal
->value_
+ offset
, value
);
1068 static bool Index_(CYPool
&pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
1069 Type_privateData
*typical(internal
->type_
);
1070 sig::Aggregate
*type(static_cast<sig::Aggregate
*>(typical
->type_
));
1074 const char *name(CYPoolCString(pool
, context
, property
));
1075 size_t length(strlen(name
));
1076 double number(CYCastDouble(name
, length
));
1078 size_t count(type
->signature
.count
);
1080 if (std::isnan(number
)) {
1081 if (property
== NULL
)
1084 sig::Element
*elements(type
->signature
.elements
);
1086 for (size_t local(0); local
!= count
; ++local
) {
1087 sig::Element
*element(&elements
[local
]);
1088 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
1096 index
= static_cast<ssize_t
>(number
);
1097 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
1102 ffi_type
**elements(typical
->GetFFI()->elements
);
1105 for (ssize_t
local(0); local
!= index
; ++local
) {
1106 offset
+= elements
[local
]->size
;
1107 CYAlign(offset
, elements
[local
+ 1]->alignment
);
1110 base
= reinterpret_cast<uint8_t *>(internal
->value_
) + offset
;
1114 static void *Offset_(CYPool
&pool
, JSContextRef context
, JSStringRef property
, void *data
, ffi_type
*ffi
) {
1116 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
1118 else if (!CYGetOffset(pool
, context
, property
, offset
))
1120 return reinterpret_cast<uint8_t *>(data
) + ffi
->size
* offset
;
1123 static JSValueRef
Offset_getProperty(CYPool
&pool
, JSContextRef context
, JSStringRef property
, void *data
, Type_privateData
*typical
, JSObjectRef owner
) {
1124 ffi_type
*ffi(typical
->GetFFI());
1125 void *base(Offset_(pool
, context
, property
, data
, ffi
));
1128 return typical
->type_
->FromFFI(context
, ffi
, base
, false, owner
);
1131 static bool Offset_setProperty(CYPool
&pool
, JSContextRef context
, JSStringRef property
, void *data
, Type_privateData
*typical
, JSValueRef value
) {
1132 ffi_type
*ffi(typical
->GetFFI());
1133 void *base(Offset_(pool
, context
, property
, data
, ffi
));
1137 typical
->type_
->PoolFFI(NULL
, context
, ffi
, base
, value
);
1141 static JSValueRef
CArray_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1143 CArray
*internal(reinterpret_cast<CArray
*>(JSObjectGetPrivate(object
)));
1144 if (JSStringIsEqual(property
, length_s
))
1145 return CYCastJSValue(context
, internal
->length_
);
1146 Type_privateData
*typical(internal
->type_
);
1147 JSObjectRef
owner(internal
->owner_
?: object
);
1148 return Offset_getProperty(pool
, context
, property
, internal
->value_
, typical
, owner
);
1151 static bool CArray_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1153 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1154 Type_privateData
*typical(internal
->type_
);
1155 return Offset_setProperty(pool
, context
, property
, internal
->value_
, typical
, value
);
1158 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1160 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1162 Type_privateData
*typical(internal
->type_
);
1164 if (sig::Function
*function
= dynamic_cast<sig::Function
*>(typical
->type_
)) {
1165 if (!JSStringIsEqualToUTF8CString(property
, "$cyi"))
1167 return CYMakeFunctor(context
, reinterpret_cast<void (*)()>(internal
->value_
), function
->variadic
, function
->signature
);
1170 JSObjectRef
owner(internal
->owner_
?: object
);
1171 return Offset_getProperty(pool
, context
, property
, internal
->value_
, typical
, owner
);
1174 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1176 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1177 Type_privateData
*typical(internal
->type_
);
1178 return Offset_setProperty(pool
, context
, property
, internal
->value_
, typical
, value
);
1181 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1182 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
1183 Type_privateData
*typical(internal
->type_
);
1184 return CYMakePointer(context
, internal
->value_
, *typical
->type_
, typical
->ffi_
, _this
);
1187 static JSValueRef Struct_getProperty_$
cyt(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1188 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1189 return CYMakeType(context
, *internal
->type_
->type_
);
1192 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1194 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1195 Type_privateData
*typical(internal
->type_
);
1196 sig::Aggregate
*type(static_cast<sig::Aggregate
*>(typical
->type_
));
1201 if (!Index_(pool
, context
, internal
, property
, index
, base
))
1204 JSObjectRef
owner(internal
->owner_
?: object
);
1206 return type
->signature
.elements
[index
].type
->FromFFI(context
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
1209 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1211 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1212 Type_privateData
*typical(internal
->type_
);
1213 sig::Aggregate
*type(static_cast<sig::Aggregate
*>(typical
->type_
));
1218 if (!Index_(pool
, context
, internal
, property
, index
, base
))
1221 type
->signature
.elements
[index
].type
->PoolFFI(NULL
, context
, typical
->GetFFI()->elements
[index
], base
, value
);
1225 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1226 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1227 Type_privateData
*typical(internal
->type_
);
1228 sig::Aggregate
*type(static_cast<sig::Aggregate
*>(typical
->type_
));
1233 size_t count(type
->signature
.count
);
1234 sig::Element
*elements(type
->signature
.elements
);
1238 for (size_t index(0); index
!= count
; ++index
) {
1240 name
= elements
[index
].name
;
1243 sprintf(number
, "%zu", index
);
1247 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
1251 static sig::Void Void_
;
1252 static sig::Pointer
PointerToVoid_(Void_
);
1254 static sig::Type
*CYGetType(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
1255 if (JSValueIsNull(context
, value
))
1256 return &PointerToVoid_
;
1257 JSObjectRef
object(CYCastJSObject(context
, value
));
1258 JSObjectRef
type(CYCastJSObject(context
, CYGetProperty(context
, object
, cyt_s
)));
1259 _assert(JSValueIsObjectOfClass(context
, type
, CYPrivate
<Type_privateData
>::Class_
));
1260 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(type
)));
1261 return internal
->type_
;
1264 void CYCallFunction(CYPool
&pool
, JSContextRef context
, ffi_cif
*cif
, void (*function
)(), void *value
, void **values
) {
1265 ffi_call(cif
, function
, value
, values
);
1268 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
)()) {
1269 size_t have(setups
+ count
);
1270 size_t need(signature
.count
- 1);
1273 throw CYJSError(context
, "insufficient number of arguments to ffi function");
1276 sig::Element
*elements(signature
.elements
);
1280 throw CYJSError(context
, "exorbitant number of arguments to ffi function");
1282 elements
= new (pool
) sig::Element
[have
+ 1];
1283 memcpy(elements
, signature
.elements
, sizeof(sig::Element
) * (need
+ 1));
1285 for (size_t index(need
); index
!= have
; ++index
) {
1286 sig::Element
&element(elements
[index
+ 1]);
1287 element
.name
= NULL
;
1288 element
.offset
= _not(size_t);
1289 element
.type
= CYGetType(pool
, context
, arguments
[index
- setups
]);
1292 sig::Signature extended
;
1293 extended
.elements
= elements
;
1294 extended
.count
= have
+ 1;
1295 sig::sig_ffi_cif(pool
, signature
.count
, extended
, &corrected
);
1300 memcpy(values
, setup
, sizeof(void *) * setups
);
1302 for (size_t index(setups
); index
!= have
; ++index
) {
1303 sig::Element
&element(elements
[index
+ 1]);
1304 ffi_type
*ffi(cif
->arg_types
[index
]);
1305 values
[index
] = pool
.malloc
<uint8_t>(ffi
->size
, ffi
->alignment
);
1306 element
.type
->PoolFFI(&pool
, context
, ffi
, values
[index
], arguments
[index
- setups
]);
1309 CYBuffer
buffer(context
);
1310 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
))));
1312 void (*call
)(CYPool
&, JSContextRef
, ffi_cif
*, void (*)(), void *, void **) = &CYCallFunction
;
1313 // XXX: this only supports one hook, but it is a bad idea anyway
1314 for (CYHook
*hook
: GetHooks())
1315 if (hook
->CallFunction
!= NULL
)
1316 call
= hook
->CallFunction
;
1318 call(pool
, context
, cif
, function
, value
, values
);
1319 return signature
.elements
[0].type
->FromFFI(context
, cif
->rtype
, value
, initialize
, buffer
);
1322 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1324 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1325 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, internal
->variadic_
, internal
->signature_
, &internal
->cif_
, internal
->value_
);
1328 static JSValueRef
Pointer_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1329 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1330 if (dynamic_cast<sig::Function
*>(internal
->type_
->type_
) == NULL
)
1331 throw CYJSError(context
, "cannot call a pointer to non-function");
1332 JSObjectRef
functor(CYCastJSObject(context
, CYGetProperty(context
, object
, cyi_s
)));
1333 return CYCallAsFunction(context
, functor
, _this
, count
, arguments
);
1336 JSObjectRef
CYMakeType(JSContextRef context
, const sig::Type
&type
) {
1337 return CYPrivate
<Type_privateData
>::Make(context
, type
);
1340 extern "C" bool CYBridgeHash(CYPool
&pool
, CYUTF8String name
, const char *&code
, unsigned &flags
) {
1341 sqlite3_stmt
*statement
;
1343 _sqlcall(sqlite3_prepare(database_
,
1345 "\"cache\".\"code\", "
1346 "\"cache\".\"flags\" "
1349 " \"cache\".\"system\" & " CY_SYSTEM
" == " CY_SYSTEM
" and"
1350 " \"cache\".\"name\" = ?"
1352 , -1, &statement
, NULL
));
1354 _sqlcall(sqlite3_bind_text(statement
, 1, name
.data
, name
.size
, SQLITE_STATIC
));
1357 if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
)
1361 code
= sqlite3_column_pooled(pool
, statement
, 0);
1362 flags
= sqlite3_column_int(statement
, 1);
1365 _sqlcall(sqlite3_finalize(statement
));
1369 static bool All_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
1370 if (JSStringIsEqualToUTF8CString(property
, "errno"))
1373 JSObjectRef
global(CYGetGlobalObject(context
));
1374 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1375 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1377 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1378 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1379 if (CYHasProperty(context
, space
, property
))
1385 if (CYBridgeHash(pool
, CYPoolUTF8String(pool
, context
, property
), code
, flags
))
1391 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1392 if (JSStringIsEqualToUTF8CString(property
, "errno"))
1393 return CYCastJSValue(context
, errno
);
1395 JSObjectRef
global(CYGetGlobalObject(context
));
1396 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1397 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1399 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1400 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1401 if (JSValueRef value
= CYGetProperty(context
, space
, property
))
1402 if (!JSValueIsUndefined(context
, value
))
1408 if (CYBridgeHash(pool
, CYPoolUTF8String(pool
, context
, property
), code
, flags
)) {
1409 CYUTF8String parsed
;
1412 parsed
= CYPoolCode(pool
, code
);
1413 } catch (const CYException
&error
) {
1414 CYThrow("%s", pool
.strcat("error caching ", CYPoolCString(pool
, context
, property
), ": ", error
.PoolCString(pool
), NULL
));
1417 JSObjectRef
cache(CYGetCachedObject(context
, CYJSString("cache")));
1420 if (flags
== CYBridgeType
) {
1421 stub
= CYMakeType(context
, sig::Void());
1422 CYSetProperty(context
, cache
, property
, stub
);
1426 JSValueRef
value(_jsccall(JSEvaluateScript
, context
, CYJSString(parsed
), NULL
, NULL
, 0));
1429 case CYBridgeVoid
: {
1432 case CYBridgeHold
: {
1433 CYSetProperty(context
, cache
, property
, value
);
1436 case CYBridgeType
: {
1437 JSObjectRef
swap(CYCastJSObject(context
, value
));
1438 void *source(JSObjectGetPrivate(swap
));
1439 _assert(source
!= NULL
);
1440 void *target(JSObjectGetPrivate(stub
));
1441 _assert(JSObjectSetPrivate(swap
, target
));
1442 _assert(JSObjectSetPrivate(stub
, source
));
1453 static JSValueRef
All_complete_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1454 _assert(count
== 1 || count
== 2);
1456 CYUTF8String
prefix(CYPoolUTF8String(pool
, context
, CYJSString(context
, arguments
[0])));
1458 JSObjectRef
array(NULL
);
1461 CYArrayBuilder
<1024> values(context
, array
);
1463 sqlite3_stmt
*statement
;
1465 if (prefix
.size
== 0)
1466 _sqlcall(sqlite3_prepare(database_
,
1468 "\"cache\".\"name\" "
1471 " \"cache\".\"system\" & " CY_SYSTEM
" == " CY_SYSTEM
1472 , -1, &statement
, NULL
));
1474 _sqlcall(sqlite3_prepare(database_
,
1476 "\"cache\".\"name\" "
1479 " \"cache\".\"name\" >= ? and \"cache\".\"name\" < ? and "
1480 " \"cache\".\"system\" & " CY_SYSTEM
" == " CY_SYSTEM
1481 , -1, &statement
, NULL
));
1483 _sqlcall(sqlite3_bind_text(statement
, 1, prefix
.data
, prefix
.size
, SQLITE_STATIC
));
1485 char *after(pool
.strndup(prefix
.data
, prefix
.size
));
1486 ++after
[prefix
.size
- 1];
1487 _sqlcall(sqlite3_bind_text(statement
, 2, after
, prefix
.size
, SQLITE_STATIC
));
1490 while (_sqlcall(sqlite3_step(statement
)) != SQLITE_DONE
)
1491 values(CYCastJSValue(context
, CYJSString(sqlite3_column_string(statement
, 0))));
1493 _sqlcall(sqlite3_finalize(statement
));
1499 static void All_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1500 JSObjectRef
global(CYGetGlobalObject(context
));
1501 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1502 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1504 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1505 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1))) {
1506 JSPropertyNameArrayRef
subset(JSObjectCopyPropertyNames(context
, space
));
1507 for (size_t index(0), count(JSPropertyNameArrayGetCount(subset
)); index
!= count
; ++index
)
1508 JSPropertyNameAccumulatorAddName(names
, JSPropertyNameArrayGetNameAtIndex(subset
, index
));
1509 JSPropertyNameArrayRelease(subset
);
1513 static JSObjectRef
CArray_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1517 static JSObjectRef
CString_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1521 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1525 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1529 } else if (count
== 1) {
1530 switch (JSValueGetType(context
, arguments
[0])) {
1531 case kJSTypeString
: {
1532 const char *encoding(CYPoolCString(pool
, context
, arguments
[0]));
1533 sig::Signature signature
;
1534 sig::Parse(pool
, &signature
, encoding
, &Structor_
);
1535 return CYMakeType(context
, *signature
.elements
[0].type
);
1538 case kJSTypeObject
: {
1539 // XXX: accept a set of enum constants and /guess/ at their size
1544 throw CYJSError(context
, "incorrect kind of argument to new Type");
1546 } else if (count
== 2) {
1547 JSObjectRef
types(CYCastJSObject(context
, arguments
[0]));
1548 size_t count(CYArrayLength(context
, types
));
1550 JSObjectRef
names(CYCastJSObject(context
, arguments
[1]));
1552 sig::Aggregate
type(false);
1553 type
.signature
.elements
= new(pool
) sig::Element
[count
];
1554 type
.signature
.count
= count
;
1556 for (size_t i(0); i
!= count
; ++i
) {
1557 sig::Element
&element(type
.signature
.elements
[i
]);
1558 element
.offset
= _not(size_t);
1560 JSValueRef
name(CYArrayGet(context
, names
, i
));
1561 if (JSValueIsUndefined(context
, name
))
1562 element
.name
= NULL
;
1564 element
.name
= CYPoolCString(pool
, context
, name
);
1566 JSObjectRef
object(CYCastJSObject(context
, CYArrayGet(context
, types
, i
)));
1567 _assert(JSValueIsObjectOfClass(context
, object
, CYPrivate
<Type_privateData
>::Class_
));
1568 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1569 element
.type
= internal
->type_
;
1570 _assert(element
.type
!= NULL
);
1573 return CYMakeType(context
, type
);
1575 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1579 static JSValueRef Type_callAsFunction_$
With(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], sig::Callable
&type
, JSValueRef
*exception
) { CYTry
{
1580 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1584 type
.signature
.elements
= new(pool
) sig::Element
[1 + count
];
1585 type
.signature
.count
= 1 + count
;
1587 type
.signature
.elements
[0].name
= NULL
;
1588 type
.signature
.elements
[0].type
= internal
->type_
;
1589 type
.signature
.elements
[0].offset
= _not(size_t);
1591 for (size_t i(0); i
!= count
; ++i
) {
1592 sig::Element
&element(type
.signature
.elements
[i
+ 1]);
1593 element
.name
= NULL
;
1594 element
.offset
= _not(size_t);
1596 JSObjectRef
object(CYCastJSObject(context
, arguments
[i
]));
1597 _assert(JSValueIsObjectOfClass(context
, object
, CYPrivate
<Type_privateData
>::Class_
));
1598 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1600 element
.type
= internal
->type_
;
1603 return CYMakeType(context
, type
);
1606 static JSValueRef
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1608 throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf");
1609 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1612 size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0])));
1613 if (index
== _not(size_t))
1614 throw CYJSError(context
, "invalid array size used with Type.arrayOf");
1616 sig::Array
type(*internal
->type_
, index
);
1617 return CYMakeType(context
, type
);
1620 static JSValueRef
Type_callAsFunction_blockWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1621 #ifdef CY_OBJECTIVEC
1623 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, type
, exception
);
1629 static JSValueRef
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1631 throw CYJSError(context
, "incorrect number of arguments to Type.constant");
1632 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1635 sig::Type
*type(internal
->type_
->Copy(pool
));
1636 type
->flags
|= JOC_TYPE_CONST
;
1637 return CYMakeType(context
, *type
);
1640 static JSValueRef
Type_callAsFunction_enumFor(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1642 throw CYJSError(context
, "incorrect number of arguments to Type.enumFor");
1643 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1647 JSObjectRef
constants(CYCastJSObject(context
, arguments
[0]));
1649 // XXX: this is, sadly, going to leak
1650 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, constants
));
1652 size_t count(JSPropertyNameArrayGetCount(names
));
1654 sig::Enum
type(*internal
->type_
, count
);
1655 type
.constants
= new(pool
) sig::Constant
[count
];
1657 for (size_t index(0); index
!= count
; ++index
) {
1658 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
1659 JSValueRef
value(CYGetProperty(context
, constants
, name
));
1660 _assert(JSValueGetType(context
, value
) == kJSTypeNumber
);
1661 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
1662 type
.constants
[index
].name
= string
.data
;
1663 type
.constants
[index
].value
= CYCastDouble(context
, value
);
1666 JSPropertyNameArrayRelease(names
);
1668 return CYMakeType(context
, type
);
1671 static JSValueRef
Type_callAsFunction_functionWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1672 bool variadic(count
!= 0 && JSValueIsNull(context
, arguments
[count
- 1]));
1673 sig::Function
type(variadic
);
1674 return Type_callAsFunction_$
With(context
, object
, _this
, variadic
? count
- 1 : count
, arguments
, type
, exception
);
1677 static JSValueRef
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1679 throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo");
1680 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1682 if (dynamic_cast<sig::Primitive
<char> *>(internal
->type_
) != NULL
)
1683 return CYMakeType(context
, sig::String((internal
->type_
->flags
& JOC_TYPE_CONST
) != 0));
1685 return CYMakeType(context
, sig::Pointer(*internal
->type_
));
1688 static JSValueRef
Type_callAsFunction_withName(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1690 throw CYJSError(context
, "incorrect number of arguments to Type.withName");
1691 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1694 return CYMakeType(context
, *internal
->type_
->Copy(pool
, CYPoolCString(pool
, context
, arguments
[0])));
1697 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1699 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1700 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1702 if (sig::Function
*function
= dynamic_cast<sig::Function
*>(internal
->type_
))
1703 return CYMakeFunctor(context
, arguments
[0], function
->variadic
, function
->signature
);
1705 CYBuffer
buffer(context
);
1707 sig::Type
*type(internal
->type_
);
1708 ffi_type
*ffi(internal
->GetFFI());
1711 if (_this
== NULL
|| CYIsStrictEqual(context
, _this
, CYGetGlobalObject(context
)))
1712 data
= buffer
->malloc
<void>(ffi
->size
, ffi
->alignment
);
1714 CYSetProperty(context
, buffer
, CYJSString("$cyo"), _this
, kJSPropertyAttributeDontEnum
);
1715 data
= CYCastPointer
<void *>(context
, _this
);
1718 type
->PoolFFI(buffer
, context
, ffi
, data
, arguments
[0]);
1719 JSValueRef
value(type
->FromFFI(context
, ffi
, data
, false, buffer
));
1723 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1725 throw CYJSError(context
, "incorrect number of arguments to Type allocator");
1726 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1728 JSObjectRef
pointer(CYMakePointer(context
, NULL
, *internal
->type_
, NULL
, NULL
));
1729 Pointer
*value(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(pointer
)));
1731 sig::Type
*type(internal
->type_
);
1732 ffi_type
*ffi(internal
->GetFFI());
1733 value
->value_
= value
->pool_
->malloc
<void>(ffi
->size
, ffi
->alignment
);
1736 memset(value
->value_
, 0, ffi
->size
);
1738 type
->PoolFFI(value
->pool_
, context
, ffi
, value
->value_
, arguments
[0]);
1743 // XXX: I don't even think the user should be allowed to do this
1744 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1746 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1748 const char *encoding(CYPoolCString(pool
, context
, arguments
[1]));
1749 sig::Signature signature
;
1750 sig::Parse(pool
, &signature
, encoding
, &Structor_
);
1751 // XXX: this can try to return null, and I guess then it just fails
1752 return CYCastJSObject(context
, CYMakeFunctor(context
, arguments
[0], false, signature
));
1755 static JSValueRef
CArray_callAsFunction_toPointer(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1756 CArray
*internal(reinterpret_cast<CArray
*>(JSObjectGetPrivate(_this
)));
1757 JSObjectRef
owner(internal
->owner_
?: object
);
1758 return CYMakePointer(context
, internal
->value_
, *internal
->type_
->type_
, NULL
, owner
);
1761 static JSValueRef
CString_callAsFunction_toPointer(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1762 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(_this
)));
1763 JSObjectRef
owner(internal
->owner_
?: object
);
1764 return CYMakePointer(context
, internal
->value_
, sig::Primitive
<char>(), NULL
, owner
);
1767 static JSValueRef Functor_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1769 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(_this
)));
1771 sig::Function
type(internal
->variadic_
);
1772 sig::Copy(pool
, type
.signature
, internal
->signature_
);
1774 return CYMakePointer(context
, reinterpret_cast<void *>(internal
->value_
), type
, NULL
, NULL
);
1777 static JSValueRef
Pointer_callAsFunction_toPointer(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1781 static JSValueRef
CArray_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1782 CArray
*internal(reinterpret_cast<CArray
*>(JSObjectGetPrivate(_this
)));
1783 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1786 static JSValueRef
Pointer_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1787 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1788 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1791 static JSValueRef
Functor_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1792 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(_this
)));
1793 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1796 static JSValueRef
Functor_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1797 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(_this
)));
1798 uint8_t *value(reinterpret_cast<uint8_t *>(internal
->value_
));
1799 _assert(value
!= NULL
);
1803 sig::Function
function(internal
->variadic_
);
1804 sig::Copy(pool
, function
.signature
, internal
->signature_
);
1806 CYPropertyName
*name(internal
->GetName(pool
));
1807 auto typed(CYDecodeType(pool
, &function
));
1809 std::ostringstream str
;
1811 CYOutput
output(*str
.rdbuf(), options
);
1812 output
.pretty_
= true;
1813 (new(pool
) CYExternalExpression(new(pool
) CYString("C"), typed
, name
))->Output(output
, CYNoFlags
);
1814 return CYCastJSValue(context
, CYJSString(str
.str()));
1817 CYPropertyName
*cy::Functor::GetName(CYPool
&pool
) const {
1819 if (dladdr(reinterpret_cast<void *>(value_
), &info
) == 0 || strcmp(info
.dli_sname
, "<redacted>") == 0)
1820 return new(pool
) CYNumber(reinterpret_cast<uintptr_t>(value_
));
1822 std::ostringstream str
;
1823 str
<< info
.dli_sname
;
1824 off_t
offset(reinterpret_cast<uint8_t *>(value_
) - reinterpret_cast<uint8_t *>(info
.dli_saddr
));
1826 str
<< "+0x" << std::hex
<< offset
;
1827 return new(pool
) CYString(pool
.strdup(str
.str().c_str()));
1830 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1831 std::set
<void *> *objects(CYCastObjects(context
, _this
, count
, arguments
));
1833 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1836 JSValueRef
value(CYGetProperty(context
, _this
, cyi_s
));
1837 if (!JSValueIsUndefined(context
, value
)) {
1839 return CYCastJSValue(context
, pool
.strcat("&", CYPoolCCYON(pool
, context
, value
, objects
), NULL
));
1841 } catch (const CYException
&e
) {
1842 // XXX: it might be interesting to include this error
1846 std::ostringstream str
;
1848 sig::Pointer
type(*internal
->type_
->type_
);
1851 CYOutput
output(*str
.rdbuf(), options
);
1852 (new(pool
) CYTypeExpression(CYDecodeType(pool
, &type
)))->Output(output
, CYNoFlags
);
1854 str
<< "(" << internal
->value_
<< ")";
1855 std::string
value(str
.str());
1856 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
1859 static JSValueRef
CString_getProperty_length(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1860 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(object
)));
1861 return CYCastJSValue(context
, strlen(internal
->value_
));
1864 static JSValueRef CString_getProperty_$
cyt(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1865 return CYMakeType(context
, sig::String(true));
1868 static JSValueRef CArray_getProperty_$
cyt(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1869 CArray
*internal(reinterpret_cast<CArray
*>(JSObjectGetPrivate(object
)));
1870 sig::Array
type(*internal
->type_
->type_
, internal
->length_
);
1871 return CYMakeType(context
, type
);
1874 static JSValueRef Pointer_getProperty_$
cyt(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1875 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1876 sig::Pointer
type(*internal
->type_
->type_
);
1877 return CYMakeType(context
, type
);
1880 static JSValueRef
CString_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1881 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(_this
)));
1882 const char *string(internal
->value_
);
1883 std::ostringstream str
;
1888 CYStringify(str
, string
, strlen(string
), CYStringifyModeNative
);
1890 std::string
value(str
.str());
1891 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
1894 static JSValueRef
CString_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1895 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(_this
)));
1896 return CYCastJSValue(context
, internal
->value_
);
1899 static JSValueRef Functor_getProperty_$
cyt(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1900 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1902 sig::Function
type(internal
->variadic_
);
1903 sig::Copy(pool
, type
.signature
, internal
->signature_
);
1904 return CYMakeType(context
, type
);
1907 static JSValueRef
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1908 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1909 return CYCastJSValue(context
, internal
->GetFFI()->alignment
);
1912 static JSValueRef
Type_getProperty_name(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1913 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1914 return CYCastJSValue(context
, internal
->type_
->GetName());
1917 static JSValueRef
Type_getProperty_size(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1918 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1919 return CYCastJSValue(context
, internal
->GetFFI()->size
);
1922 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1923 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1925 const char *type(sig::Unparse(pool
, internal
->type_
));
1926 return CYCastJSValue(context
, CYJSString(type
));
1929 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1930 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1934 CYOutput
output(out
, options
);
1935 output
.pretty_
= true;
1936 (new(pool
) CYTypeExpression(CYDecodeType(pool
, internal
->type_
->Copy(pool
, ""))))->Output(output
, CYNoFlags
);
1937 return CYCastJSValue(context
, CYJSString(out
.str().c_str()));
1940 static JSStaticFunction All_staticFunctions
[2] = {
1941 {"cy$complete", &All_complete_callAsFunction
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1945 static JSStaticFunction CArray_staticFunctions
[3] = {
1946 {"toPointer", &CArray_callAsFunction_toPointer
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1947 {"valueOf", &CArray_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1951 static JSStaticValue CArray_staticValues
[2] = {
1952 {"$cyt", &CArray_getProperty_$cyt
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1953 {NULL
, NULL
, NULL
, 0}
1956 static JSStaticFunction CString_staticFunctions
[5] = {
1957 {"toCYON", &CString_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1958 {"toPointer", &CString_callAsFunction_toPointer
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1959 {"toString", &CString_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1960 {"valueOf", &CString_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1964 static JSStaticValue CString_staticValues
[3] = {
1965 {"length", &CString_getProperty_length
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1966 {"$cyt", &CString_getProperty_$cyt
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1967 {NULL
, NULL
, NULL
, 0}
1970 static JSStaticFunction Pointer_staticFunctions
[4] = {
1971 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1972 {"toPointer", &Pointer_callAsFunction_toPointer
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1973 {"valueOf", &Pointer_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1977 static JSStaticValue Pointer_staticValues
[2] = {
1978 {"$cyt", &Pointer_getProperty_$cyt
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1979 {NULL
, NULL
, NULL
, 0}
1982 static JSStaticFunction Struct_staticFunctions
[2] = {
1983 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1987 static JSStaticValue Struct_staticValues
[2] = {
1988 {"$cyt", &Struct_getProperty_$cyt
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1989 {NULL
, NULL
, NULL
, 0}
1992 static JSStaticFunction Functor_staticFunctions
[5] = {
1993 {"$cya", &Functor_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1994 {"toCYON", &Functor_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1995 {"toPointer", &Functor_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1996 {"valueOf", &Functor_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2000 static JSStaticValue Functor_staticValues
[2] = {
2001 {"$cyt", &Functor_getProperty_$cyt
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2002 {NULL
, NULL
, NULL
, 0}
2005 static JSStaticValue Type_staticValues
[4] = {
2006 {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2007 {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2008 {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2009 {NULL
, NULL
, NULL
, 0}
2012 static JSStaticFunction Type_staticFunctions
[10] = {
2013 {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2014 {"blockWith", &Type_callAsFunction_blockWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2015 {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2016 {"enumFor", &Type_callAsFunction_enumFor
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2017 {"functionWith", &Type_callAsFunction_functionWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2018 {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2019 {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2020 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2021 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
2025 _visible
void CYSetArgs(const char *argv0
, const char *script
, int argc
, const char *argv
[]) {
2026 JSContextRef
context(CYGetJSContext());
2027 JSValueRef args
[argc
+ 2];
2028 for (int i(0); i
!= argc
; ++i
)
2029 args
[i
+ 2] = CYCastJSValue(context
, argv
[i
]);
2036 args
[1] = CYCastJSValue(context
, CYJSString(script
));
2039 args
[offset
] = CYCastJSValue(context
, CYJSString(argv0
));
2041 CYSetProperty(context
, CYGetCachedObject(context
, CYJSString("System")), CYJSString("args"), CYObjectMakeArray(context
, argc
, args
+ 2));
2042 CYSetProperty(context
, CYGetCachedObject(context
, CYJSString("process")), CYJSString("argv"), CYObjectMakeArray(context
, argc
+ 2 - offset
, args
+ offset
));
2045 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
2046 return JSContextGetGlobalObject(context
);
2049 // XXX: this is neither exceptin safe nor even terribly sane
2050 class ExecutionHandle
{
2052 JSContextRef context_
;
2053 std::vector
<void *> handles_
;
2056 ExecutionHandle(JSContextRef context
) :
2059 handles_
.resize(GetHooks().size());
2060 for (size_t i(0); i
!= GetHooks().size(); ++i
) {
2061 CYHook
*hook(GetHooks()[i
]);
2062 if (hook
->ExecuteStart
!= NULL
)
2063 handles_
[i
] = (*hook
->ExecuteStart
)(context_
);
2069 ~ExecutionHandle() {
2070 for (size_t i(GetHooks().size()); i
!= 0; --i
) {
2071 CYHook
*hook(GetHooks()[i
-1]);
2072 if (hook
->ExecuteEnd
!= NULL
)
2073 (*hook
->ExecuteEnd
)(context_
, handles_
[i
-1]);
2079 static volatile bool cancel_
;
2081 static bool CYShouldTerminate(JSContextRef context
, void *arg
) {
2086 _visible
const char *CYExecute(JSContextRef context
, CYPool
&pool
, CYUTF8String code
) {
2087 ExecutionHandle
handle(context
);
2091 if (&JSContextGroupSetExecutionTimeLimit
!= NULL
)
2092 JSContextGroupSetExecutionTimeLimit(JSContextGetGroup(context
), 0.5, &CYShouldTerminate
, NULL
);
2096 JSValueRef
result(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0));
2097 if (JSValueIsUndefined(context
, result
))
2100 std::set
<void *> objects
;
2101 const char *json(_jsccall(CYPoolCCYON
, pool
, context
, result
, objects
));
2102 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
2105 } catch (const CYException
&error
) {
2106 return pool
.strcat("throw ", error
.PoolCString(pool
), NULL
);
2111 _visible
void CYCancel() {
2116 const char *CYPoolLibraryPath(CYPool
&pool
);
2118 static bool initialized_
= false;
2120 void CYInitializeDynamic() {
2122 initialized_
= true;
2126 const char *db(pool
.strcat(CYPoolLibraryPath(pool
), "/libcycript.db", NULL
));
2127 _sqlcall(sqlite3_open_v2(db
, &database_
, SQLITE_OPEN_READONLY
, NULL
));
2129 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
2130 JSSynchronousGarbageCollectForDebugging$
= reinterpret_cast<void (*)(JSContextRef
)>(dlsym(RTLD_DEFAULT
, "JSSynchronousGarbageCollectForDebugging"));
2132 JSClassDefinition definition
;
2134 definition
= kJSClassDefinitionEmpty
;
2135 definition
.attributes
= kJSClassAttributeNoAutomaticPrototype
;
2136 definition
.className
= "All";
2137 definition
.staticFunctions
= All_staticFunctions
;
2138 definition
.hasProperty
= &All_hasProperty
;
2139 definition
.getProperty
= &All_getProperty
;
2140 definition
.getPropertyNames
= &All_getPropertyNames
;
2141 All_
= JSClassCreate(&definition
);
2143 definition
= kJSClassDefinitionEmpty
;
2144 definition
.attributes
= kJSClassAttributeNoAutomaticPrototype
;
2145 definition
.className
= "Context";
2146 definition
.finalize
= &CYFinalize
;
2147 CYPrivate
<Context
>::Class_
= JSClassCreate(&definition
);
2149 definition
= kJSClassDefinitionEmpty
;
2150 definition
.className
= "CArray";
2151 definition
.staticFunctions
= CArray_staticFunctions
;
2152 definition
.staticValues
= CArray_staticValues
;
2153 definition
.getProperty
= &CArray_getProperty
;
2154 definition
.setProperty
= &CArray_setProperty
;
2155 definition
.finalize
= &CYFinalize
;
2156 CYPrivate
<CArray
>::Class_
= JSClassCreate(&definition
);
2158 definition
= kJSClassDefinitionEmpty
;
2159 definition
.className
= "CString";
2160 definition
.staticFunctions
= CString_staticFunctions
;
2161 definition
.staticValues
= CString_staticValues
;
2162 definition
.getProperty
= &CString_getProperty
;
2163 definition
.setProperty
= &CString_setProperty
;
2164 definition
.finalize
= &CYFinalize
;
2165 CYPrivate
<CString
>::Class_
= JSClassCreate(&definition
);
2167 definition
= kJSClassDefinitionEmpty
;
2168 definition
.className
= "Functor";
2169 definition
.staticFunctions
= Functor_staticFunctions
;
2170 definition
.staticValues
= Functor_staticValues
;
2171 definition
.callAsFunction
= &Functor_callAsFunction
;
2172 definition
.finalize
= &CYFinalize
;
2173 cy::Functor::Class_
= JSClassCreate(&definition
);
2175 definition
= kJSClassDefinitionEmpty
;
2176 definition
.className
= "Pointer";
2177 definition
.staticFunctions
= Pointer_staticFunctions
;
2178 definition
.staticValues
= Pointer_staticValues
;
2179 definition
.callAsFunction
= &Pointer_callAsFunction
;
2180 definition
.getProperty
= &Pointer_getProperty
;
2181 definition
.setProperty
= &Pointer_setProperty
;
2182 definition
.finalize
= &CYFinalize
;
2183 CYPrivate
<Pointer
>::Class_
= JSClassCreate(&definition
);
2185 definition
= kJSClassDefinitionEmpty
;
2186 definition
.className
= "Root";
2187 definition
.finalize
= &CYFinalize
;
2188 CYPrivate
<CYRoot
>::Class_
= JSClassCreate(&definition
);
2190 definition
= kJSClassDefinitionEmpty
;
2191 definition
.className
= "Struct";
2192 definition
.staticFunctions
= Struct_staticFunctions
;
2193 definition
.staticValues
= Struct_staticValues
;
2194 definition
.getProperty
= &Struct_getProperty
;
2195 definition
.setProperty
= &Struct_setProperty
;
2196 definition
.getPropertyNames
= &Struct_getPropertyNames
;
2197 definition
.finalize
= &CYFinalize
;
2198 CYPrivate
<Struct_privateData
>::Class_
= JSClassCreate(&definition
);
2200 definition
= kJSClassDefinitionEmpty
;
2201 definition
.className
= "Type";
2202 definition
.staticValues
= Type_staticValues
;
2203 definition
.staticFunctions
= Type_staticFunctions
;
2204 definition
.callAsFunction
= &Type_callAsFunction
;
2205 definition
.callAsConstructor
= &Type_callAsConstructor
;
2206 definition
.finalize
= &CYFinalize
;
2207 CYPrivate
<Type_privateData
>::Class_
= JSClassCreate(&definition
);
2209 definition
= kJSClassDefinitionEmpty
;
2210 definition
.className
= "Global";
2211 //definition.getProperty = &Global_getProperty;
2212 Global_
= JSClassCreate(&definition
);
2214 Array_s
= JSStringCreateWithUTF8CString("Array");
2215 constructor_s
= JSStringCreateWithUTF8CString("constructor");
2216 cy_s
= JSStringCreateWithUTF8CString("$cy");
2217 cyi_s
= JSStringCreateWithUTF8CString("$cyi");
2218 cyt_s
= JSStringCreateWithUTF8CString("$cyt");
2219 length_s
= JSStringCreateWithUTF8CString("length");
2220 message_s
= JSStringCreateWithUTF8CString("message");
2221 name_s
= JSStringCreateWithUTF8CString("name");
2222 pop_s
= JSStringCreateWithUTF8CString("pop");
2223 prototype_s
= JSStringCreateWithUTF8CString("prototype");
2224 push_s
= JSStringCreateWithUTF8CString("push");
2225 splice_s
= JSStringCreateWithUTF8CString("splice");
2226 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
2227 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
2228 toPointer_s
= JSStringCreateWithUTF8CString("toPointer");
2229 toString_s
= JSStringCreateWithUTF8CString("toString");
2230 weak_s
= JSStringCreateWithUTF8CString("weak");
2232 Result_
= JSStringCreateWithUTF8CString("_");
2234 for (CYHook
*hook
: GetHooks())
2235 if (hook
->Initialize
!= NULL
)
2236 (*hook
->Initialize
)();
2239 void CYThrow(JSContextRef context
, JSValueRef value
) {
2241 throw CYJSError(context
, value
);
2244 const char *CYJSError::PoolCString(CYPool
&pool
) const {
2245 std::set
<void *> objects
;
2246 // XXX: this used to be CYPoolCString
2247 return CYPoolCCYON(pool
, context_
, value_
, objects
);
2250 JSValueRef
CYJSError::CastJSValue(JSContextRef context
, const char *name
) const {
2251 // XXX: what if the context is different? or the name? I dunno. ("epic" :/)
2255 JSValueRef
CYCastJSError(JSContextRef context
, const char *name
, const char *message
) {
2256 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString(name
)));
2257 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
2258 return _jsccall(JSObjectCallAsConstructor
, context
, Error
, 1, arguments
);
2261 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
, const char *name
) const {
2262 return CYCastJSError(context
, name
, message_
);
2265 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
2266 _assert(context
!= NULL
);
2271 va_start(args
, format
);
2272 // XXX: there might be a beter way to think about this
2273 const char *message(pool
.vsprintf(64, format
, args
));
2276 value_
= CYCastJSError(context
, "Error", message
);
2279 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
2280 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
2284 char *CYPoolLibraryPath_(CYPool
&pool
) {
2285 FILE *maps(fopen("/proc/self/maps", "r"));
2286 struct F
{ FILE *f
; F(FILE *f
) : f(f
) {}
2287 ~F() { fclose(f
); } } f(maps
);
2289 size_t function(reinterpret_cast<size_t>(&CYPoolLibraryPath
));
2292 size_t start
; size_t end
; char flags
[8]; unsigned long long offset
;
2293 int major
; int minor
; unsigned long long inode
; char file
[1024];
2294 int count(fscanf(maps
, "%zx-%zx %7s %llx %x:%x %llu%[ ]%1024[^\n]\n",
2295 &start
, &end
, flags
, &offset
, &major
, &minor
, &inode
, file
, file
));
2296 if (count
< 8) break; else if (start
<= function
&& function
< end
)
2297 return pool
.strdup(file
);
2303 char *CYPoolLibraryPath_(CYPool
&pool
) {
2305 _assert(dladdr(reinterpret_cast<void *>(&CYPoolLibraryPath
), &addr
) != 0);
2306 return pool
.strdup(addr
.dli_fname
);
2310 const char *CYPoolLibraryPath(CYPool
&pool
) {
2311 char *lib(CYPoolLibraryPath_(pool
));
2313 char *slash(strrchr(lib
, '/'));
2318 slash
= strrchr(lib
, '/');
2319 if (slash
!= NULL
) {
2320 if (strcmp(slash
, "/.libs") == 0)
2322 } else if (strcmp(lib
, ".libs") == 0)
2328 static JSValueRef
require_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
2329 _assert(count
== 1);
2332 CYUTF8String
name(CYPoolUTF8String(pool
, context
, CYJSString(context
, arguments
[0])));
2333 if (memchr(name
.data
, '/', name
.size
) == NULL
&& (
2335 dlopen(pool
.strcat("/System/Library/Frameworks/", name
.data
, ".framework/", name
.data
, NULL
), RTLD_LAZY
| RTLD_GLOBAL
) != NULL
||
2336 dlopen(pool
.strcat("/System/Library/PrivateFrameworks/", name
.data
, ".framework/", name
.data
, NULL
), RTLD_LAZY
| RTLD_GLOBAL
) != NULL
||
2339 return CYJSUndefined(context
);
2344 sqlite3_stmt
*statement
;
2346 _sqlcall(sqlite3_prepare(database_
,
2348 "\"module\".\"code\", "
2349 "\"module\".\"flags\" "
2352 " \"module\".\"name\" = ?"
2354 , -1, &statement
, NULL
));
2356 _sqlcall(sqlite3_bind_text(statement
, 1, name
.data
, name
.size
, SQLITE_STATIC
));
2358 if (_sqlcall(sqlite3_step(statement
)) != SQLITE_DONE
) {
2359 code
.data
= static_cast<const char *>(sqlite3_column_blob(statement
, 0));
2360 code
.size
= sqlite3_column_bytes(statement
, 0);
2361 path
= CYJSString(name
);
2362 code
= CYPoolUTF8String(pool
, code
);
2364 JSObjectRef
resolve(CYCastJSObject(context
, CYGetProperty(context
, object
, CYJSString("resolve"))));
2365 path
= CYJSString(context
, CYCallAsFunction(context
, resolve
, NULL
, 1, arguments
));
2368 _sqlcall(sqlite3_finalize(statement
));
2370 CYJSString
property("exports");
2372 JSObjectRef
modules(CYGetCachedObject(context
, CYJSString("modules")));
2373 JSValueRef
cache(CYGetProperty(context
, modules
, path
));
2376 if (!JSValueIsUndefined(context
, cache
)) {
2377 JSObjectRef
module(CYCastJSObject(context
, cache
));
2378 result
= CYGetProperty(context
, module, property
);
2380 if (code
.data
== NULL
) {
2381 code
= CYPoolFileUTF8String(pool
, CYPoolCString(pool
, context
, path
));
2382 _assert(code
.data
!= NULL
);
2385 size_t length(name
.size
);
2386 if (length
>= 5 && strncmp(name
.data
+ length
- 5, ".json", 5) == 0) {
2387 JSObjectRef
JSON(CYGetCachedObject(context
, CYJSString("JSON")));
2388 JSObjectRef
parse(CYCastJSObject(context
, CYGetProperty(context
, JSON
, CYJSString("parse"))));
2389 JSValueRef arguments
[1] = { CYCastJSValue(context
, CYJSString(code
)) };
2390 result
= CYCallAsFunction(context
, parse
, JSON
, 1, arguments
);
2392 JSObjectRef
module(JSObjectMake(context
, NULL
, NULL
));
2393 CYSetProperty(context
, modules
, path
, module);
2395 JSObjectRef
exports(JSObjectMake(context
, NULL
, NULL
));
2396 CYSetProperty(context
, module, property
, exports
);
2398 std::stringstream wrap
;
2399 wrap
<< "(function (exports, require, module, __filename) { " << code
<< "\n});";
2400 code
= CYPoolCode(pool
, *wrap
.rdbuf());
2402 JSValueRef
value(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0));
2403 JSObjectRef
function(CYCastJSObject(context
, value
));
2405 JSValueRef arguments
[4] = { exports
, object
, module, CYCastJSValue(context
, path
) };
2406 CYCallAsFunction(context
, function
, NULL
, 4, arguments
);
2407 result
= CYGetProperty(context
, module, property
);
2414 static bool CYRunScript(JSGlobalContextRef context
, const char *path
) {
2416 CYUTF8String
code(CYPoolFileUTF8String(pool
, pool
.strcat(CYPoolLibraryPath(pool
), path
, NULL
)));
2417 if (code
.data
== NULL
)
2420 code
= CYPoolCode(pool
, code
);
2421 _jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0);
2425 extern "C" void CYDestroyWeak(JSWeakObjectMapRef weak
, void *data
) {
2428 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
2429 CYInitializeDynamic();
2431 JSObjectRef
global(CYGetGlobalObject(context
));
2433 JSObjectRef
cy(CYPrivate
<Context
>::Make(context
, context
));
2434 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
2436 /* Cache Globals {{{ */
2437 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
2438 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
2440 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
2441 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
2443 JSObjectRef
Boolean(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Boolean"))));
2444 CYSetProperty(context
, cy
, CYJSString("Boolean"), Boolean
);
2446 JSObjectRef
Boolean_prototype(CYCastJSObject(context
, CYGetProperty(context
, Boolean
, prototype_s
)));
2447 CYSetProperty(context
, cy
, CYJSString("Boolean_prototype"), Boolean_prototype
);
2449 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
2450 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
2452 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
2453 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
2455 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
2456 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
2458 JSObjectRef
JSON(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("JSON"))));
2459 CYSetProperty(context
, cy
, CYJSString("JSON"), JSON
);
2461 JSObjectRef
Number(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Number"))));
2462 CYSetProperty(context
, cy
, CYJSString("Number"), Number
);
2464 JSObjectRef
Number_prototype(CYCastJSObject(context
, CYGetProperty(context
, Number
, prototype_s
)));
2465 CYSetProperty(context
, cy
, CYJSString("Number_prototype"), Number_prototype
);
2467 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
2468 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
2470 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
2471 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
2473 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
2474 CYSetProperty(context
, cy
, CYJSString("String"), String
);
2476 JSObjectRef
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
)));
2477 CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
);
2479 JSObjectRef
SyntaxError(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("SyntaxError"))));
2480 CYSetProperty(context
, cy
, CYJSString("SyntaxError"), SyntaxError
);
2483 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
2484 CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
2486 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
2487 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
2488 CYSetProperty(context
, cycript
, CYJSString("compile"), &Cycript_compile_callAsFunction
);
2489 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
2491 JSObjectRef
CArray(JSObjectMakeConstructor(context
, CYPrivate
<::CArray
>::Class_
, &CArray_new
));
2492 CYSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, CArray
, prototype_s
)), Array_prototype
);
2493 CYSetProperty(context
, cycript
, CYJSString("CArray"), CArray
);
2495 JSObjectRef
CString(JSObjectMakeConstructor(context
, CYPrivate
<::CString
>::Class_
, &CString_new
));
2496 CYSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, CString
, prototype_s
)), String_prototype
);
2497 CYSetProperty(context
, cycript
, CYJSString("CString"), CString
);
2499 JSObjectRef
Functor(JSObjectMakeConstructor(context
, cy::Functor::Class_
, &Functor_new
));
2500 JSObjectRef
Functor_prototype(CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)));
2501 CYSetPrototype(context
, Functor_prototype
, Function_prototype
);
2502 CYSetProperty(context
, cy
, CYJSString("Functor_prototype"), Functor_prototype
);
2503 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
2505 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, CYPrivate
<Pointer
>::Class_
, &Pointer_new
));
2507 JSObjectRef
Type(JSObjectMakeConstructor(context
, CYPrivate
<Type_privateData
>::Class_
, &Type_new
));
2508 JSObjectRef
Type_prototype(CYCastJSObject(context
, CYGetProperty(context
, Type
, prototype_s
)));
2509 CYSetPrototype(context
, Type_prototype
, Function_prototype
);
2510 CYSetProperty(context
, cy
, CYJSString("Type_prototype"), Type_prototype
);
2511 CYSetProperty(context
, cycript
, CYJSString("Type"), Type
);
2513 JSObjectRef
Character_prototype(JSObjectMake(context
, NULL
, NULL
));
2514 CYSetPrototype(context
, Character_prototype
, String_prototype
);
2515 CYSetProperty(context
, cy
, CYJSString("Character_prototype"), Character_prototype
);
2516 CYSetProperty(context
, Character_prototype
, CYJSString("valueOf"), _jsccall(JSEvaluateScript
, context
, CYJSString("(function(){return this.charCodeAt(0);})"), NULL
, NULL
, 0));
2518 JSObjectRef
modules(JSObjectMake(context
, NULL
, NULL
));
2519 CYSetProperty(context
, cy
, CYJSString("modules"), modules
);
2521 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
2522 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
2524 JSObjectRef
cache(JSObjectMake(context
, NULL
, NULL
));
2525 CYSetProperty(context
, cy
, CYJSString("cache"), cache
);
2526 CYSetPrototype(context
, cache
, all
);
2528 JSObjectRef
alls(_jsccall(JSObjectCallAsConstructor
, context
, Array
, 0, NULL
));
2529 CYSetProperty(context
, cycript
, CYJSString("alls"), alls
);
2532 JSObjectRef
last(NULL
), curr(global
);
2534 goto next
; for (JSValueRef next
;;) {
2535 if (JSValueIsNull(context
, next
))
2538 curr
= CYCastJSObject(context
, next
);
2540 next
= JSObjectGetPrototype(context
, curr
);
2543 CYSetPrototype(context
, last
, cache
);
2547 if (&JSWeakObjectMapCreate
!= NULL
) {
2548 JSWeakObjectMapRef
weak(JSWeakObjectMapCreate(context
, NULL
, &CYDestroyWeak
));
2549 CYSetProperty(context
, cy
, weak_s
, CYCastJSValue(context
, reinterpret_cast<uintptr_t>(weak
)));
2553 CYSetProperty(context
, String_prototype
, cyt_s
, CYMakeType(context
, sig::String(true)), kJSPropertyAttributeDontEnum
);
2555 CYSetProperty(context
, cache
, CYJSString("dlerror"), CYMakeFunctor(context
, "dlerror", "*"), kJSPropertyAttributeDontEnum
);
2556 CYSetProperty(context
, cache
, CYJSString("RTLD_DEFAULT"), CYCastJSValue(context
, reinterpret_cast<intptr_t>(RTLD_DEFAULT
)), kJSPropertyAttributeDontEnum
);
2557 CYSetProperty(context
, cache
, CYJSString("dlsym"), CYMakeFunctor(context
, "dlsym", "^v^v*"), kJSPropertyAttributeDontEnum
);
2559 CYSetProperty(context
, cache
, CYJSString("NULL"), CYJSNull(context
), kJSPropertyAttributeDontEnum
);
2561 CYSetProperty(context
, cache
, CYJSString("bool"), CYMakeType(context
, sig::Primitive
<bool>()), kJSPropertyAttributeDontEnum
);
2562 CYSetProperty(context
, cache
, CYJSString("char"), CYMakeType(context
, sig::Primitive
<char>()), kJSPropertyAttributeDontEnum
);
2563 CYSetProperty(context
, cache
, CYJSString("schar"), CYMakeType(context
, sig::Primitive
<signed char>()), kJSPropertyAttributeDontEnum
);
2564 CYSetProperty(context
, cache
, CYJSString("uchar"), CYMakeType(context
, sig::Primitive
<unsigned char>()), kJSPropertyAttributeDontEnum
);
2566 CYSetProperty(context
, cache
, CYJSString("short"), CYMakeType(context
, sig::Primitive
<short>()), kJSPropertyAttributeDontEnum
);
2567 CYSetProperty(context
, cache
, CYJSString("int"), CYMakeType(context
, sig::Primitive
<int>()), kJSPropertyAttributeDontEnum
);
2568 CYSetProperty(context
, cache
, CYJSString("long"), CYMakeType(context
, sig::Primitive
<long>()), kJSPropertyAttributeDontEnum
);
2569 CYSetProperty(context
, cache
, CYJSString("longlong"), CYMakeType(context
, sig::Primitive
<long long>()), kJSPropertyAttributeDontEnum
);
2571 CYSetProperty(context
, cache
, CYJSString("ushort"), CYMakeType(context
, sig::Primitive
<unsigned short>()), kJSPropertyAttributeDontEnum
);
2572 CYSetProperty(context
, cache
, CYJSString("uint"), CYMakeType(context
, sig::Primitive
<unsigned int>()), kJSPropertyAttributeDontEnum
);
2573 CYSetProperty(context
, cache
, CYJSString("ulong"), CYMakeType(context
, sig::Primitive
<unsigned long>()), kJSPropertyAttributeDontEnum
);
2574 CYSetProperty(context
, cache
, CYJSString("ulonglong"), CYMakeType(context
, sig::Primitive
<unsigned long long>()), kJSPropertyAttributeDontEnum
);
2576 #ifdef __SIZEOF_INT128__
2577 CYSetProperty(context
, cache
, CYJSString("int128"), CYMakeType(context
, sig::Primitive
<__int128
>()), kJSPropertyAttributeDontEnum
);
2578 CYSetProperty(context
, cache
, CYJSString("uint128"), CYMakeType(context
, sig::Primitive
<unsigned __int128
>()), kJSPropertyAttributeDontEnum
);
2581 CYSetProperty(context
, cache
, CYJSString("float"), CYMakeType(context
, sig::Primitive
<float>()), kJSPropertyAttributeDontEnum
);
2582 CYSetProperty(context
, cache
, CYJSString("double"), CYMakeType(context
, sig::Primitive
<double>()), kJSPropertyAttributeDontEnum
);
2583 CYSetProperty(context
, cache
, CYJSString("longdouble"), CYMakeType(context
, sig::Primitive
<long double>()), kJSPropertyAttributeDontEnum
);
2585 CYSetProperty(context
, global
, CYJSString("require"), &require_callAsFunction
, kJSPropertyAttributeDontEnum
);
2587 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
2588 CYSetProperty(context
, all
, CYJSString("system"), System
);
2589 System
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("system")));
2590 CYSetProperty(context
, cy
, CYJSString("System"), System
);
2592 JSObjectRef
process(JSObjectMake(context
, NULL
, NULL
));
2593 CYSetProperty(context
, global
, CYJSString("process"), process
);
2594 CYSetProperty(context
, cy
, CYJSString("process"), process
);
2596 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
2597 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
2599 CYSetProperty(context
, global
, CYJSString("global"), global
);
2600 CYSetProperty(context
, global
, CYJSString("print"), &Global_print
);
2602 for (CYHook
*hook
: GetHooks())
2603 if (hook
->SetupContext
!= NULL
)
2604 (*hook
->SetupContext
)(context
);
2606 CYArrayPush(context
, alls
, cycript
);
2608 CYRunScript(context
, "/libcycript.cy");
2611 static JSGlobalContextRef context_
;
2613 _visible JSGlobalContextRef
CYGetJSContext() {
2614 CYInitializeDynamic();
2616 if (context_
== NULL
) {
2617 context_
= JSGlobalContextCreate(Global_
);
2618 CYSetupContext(context_
);
2624 _visible
void CYDestroyContext() {
2625 if (context_
== NULL
)
2627 JSGlobalContextRelease(context_
);