1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "cycript.hpp"
39 #include "sig/parse.hpp"
40 #include "sig/ffi_type.hpp"
45 #include "Execute.hpp"
46 #include "Internal.hpp"
47 #include "JavaScript.hpp"
48 #include "Pooling.hpp"
51 static std::vector
<CYHook
*> &GetHooks() {
52 static std::vector
<CYHook
*> hooks
;
56 CYRegisterHook::CYRegisterHook(CYHook
*hook
) {
57 GetHooks().push_back(hook
);
60 /* JavaScript Properties {{{ */
61 bool CYHasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
62 return JSObjectHasProperty(context
, object
, name
);
65 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) {
66 return _jsccall(JSObjectGetPropertyAtIndex
, context
, object
, index
);
69 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
70 return _jsccall(JSObjectGetProperty
, context
, object
, name
);
73 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) {
74 _jsccall(JSObjectSetPropertyAtIndex
, context
, object
, index
, value
);
77 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) {
78 _jsccall(JSObjectSetProperty
, context
, object
, name
, value
, attributes
);
81 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef (*callback
)(JSContextRef
, JSObjectRef
, JSObjectRef
, size_t, const JSValueRef
[], JSValueRef
*), JSPropertyAttributes attributes
) {
82 CYSetProperty(context
, object
, name
, JSObjectMakeFunctionWithCallback(context
, name
, callback
), attributes
);
85 void CYSetPrototype(JSContextRef context
, JSObjectRef object
, JSValueRef value
) {
86 JSObjectSetPrototype(context
, object
, value
);
87 _assert(CYIsStrictEqual(context
, JSObjectGetPrototype(context
, object
), value
));
90 /* JavaScript Strings {{{ */
91 JSStringRef
CYCopyJSString(const char *value
) {
92 return value
== NULL
? NULL
: JSStringCreateWithUTF8CString(value
);
95 JSStringRef
CYCopyJSString(JSStringRef value
) {
96 return value
== NULL
? NULL
: JSStringRetain(value
);
99 JSStringRef
CYCopyJSString(CYUTF8String value
) {
100 if (memchr(value
.data
, '\0', value
.size
) != NULL
) {
102 return CYCopyJSString(pool
.memdup(value
.data
, value
.size
));
103 } else if (value
.data
[value
.size
] != '\0') {
105 return CYCopyJSString(CYPoolUTF16String(pool
, value
));
107 return CYCopyJSString(value
.data
);
111 JSStringRef
CYCopyJSString(CYUTF16String value
) {
112 return JSStringCreateWithCharacters(value
.data
, value
.size
);
115 JSStringRef
CYCopyJSString(JSContextRef context
, JSValueRef value
) {
116 if (JSValueIsNull(context
, value
))
118 return _jsccall(JSValueToStringCopy
, context
, value
);
121 static CYUTF16String
CYCastUTF16String(JSStringRef value
) {
122 return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
));
125 CYUTF8String
CYPoolUTF8String(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
126 return CYPoolUTF8String(pool
, CYCastUTF16String(value
));
129 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
130 CYUTF8String
utf8(CYPoolUTF8String(pool
, context
, value
));
131 _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
);
135 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
136 return JSValueIsNull(context
, value
) ? NULL
: CYPoolCString(pool
, context
, CYJSString(context
, value
));
139 /* Index Offsets {{{ */
140 size_t CYGetIndex(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
141 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
145 static JSClassRef All_
;
146 static JSClassRef Context_
;
147 static JSClassRef CString_
;
149 static JSClassRef Global_
;
150 static JSClassRef Pointer_
;
151 static JSClassRef Struct_
;
156 JSStringRef length_s
;
157 JSStringRef message_s
;
160 JSStringRef prototype_s
;
162 JSStringRef splice_s
;
163 JSStringRef toCYON_s
;
164 JSStringRef toJSON_s
;
165 JSStringRef toPointer_s
;
166 JSStringRef toString_s
;
169 static JSStringRef Result_
;
171 void CYFinalize(JSObjectRef object
) {
172 CYData
*internal(reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
)));
173 _assert(internal
->count_
!= _not(unsigned));
174 if (--internal
->count_
== 0)
178 void Structor_(CYPool
&pool
, sig::Type
*&type
) {
180 type
->primitive
== sig::pointer_P
&&
181 type
->data
.data
.type
->primitive
== sig::struct_P
&&
182 type
->data
.data
.type
->name
!= NULL
&&
183 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
185 type
->primitive
= sig::typename_P
;
186 type
->data
.data
.type
= NULL
;
190 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
193 size_t length(strlen(type
->name
));
194 char keyed
[length
+ 2];
195 memcpy(keyed
+ 1, type
->name
, length
+ 1);
197 static const char *modes
= "34";
198 for (size_t i(0); i
!= 2; ++i
) {
202 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
205 sig::Parse(pool
, &type
->data
.signature
, entry
->value_
, &Structor_
);
209 sig::Signature signature
;
210 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
211 type
= signature
.elements
[0].type
;
217 JSClassRef
Type_privateData::Class_
;
222 JSGlobalContextRef context_
;
224 Context(JSGlobalContextRef context
) :
233 CString(char *value
, JSContextRef context
, JSObjectRef owner
) :
234 CYOwned(value
, context
, owner
)
242 Type_privateData
*type_
;
245 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type
*type
) :
246 CYOwned(value
, context
, owner
),
247 type_(new(*pool_
) Type_privateData(type
)),
253 struct Struct_privateData
:
256 Type_privateData
*type_
;
258 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
259 CYOwned(NULL
, context
, owner
)
264 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
265 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
266 CYPool
&pool(*internal
->pool_
);
267 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
268 internal
->type_
= typical
;
271 internal
->value_
= data
;
273 size_t size(typical
->GetFFI()->size
);
274 void *copy(internal
->pool_
->malloc
<void>(size
));
275 memcpy(copy
, data
, size
);
276 internal
->value_
= copy
;
279 return JSObjectMake(context
, Struct_
, internal
);
282 static void *CYCastSymbol(const char *name
) {
283 for (CYHook
*hook
: GetHooks())
284 if (hook
->CastSymbol
!= NULL
)
285 if (void *value
= (*hook
->CastSymbol
)(name
))
287 return dlsym(RTLD_DEFAULT
, name
);
290 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
291 return JSValueMakeBoolean(context
, value
);
294 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
295 return JSValueMakeNumber(context
, value
);
298 #define CYCastJSValue_(Type_) \
299 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
300 return JSValueMakeNumber(context, static_cast<double>(value)); \
304 CYCastJSValue_(unsigned int)
305 CYCastJSValue_(long int)
306 CYCastJSValue_(long unsigned int)
307 CYCastJSValue_(long long int)
308 CYCastJSValue_(long long unsigned int)
310 JSValueRef
CYJSUndefined(JSContextRef context
) {
311 return JSValueMakeUndefined(context
);
314 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
315 return _jsccall(JSValueToNumber
, context
, value
);
318 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
319 return JSValueToBoolean(context
, value
);
322 JSValueRef
CYJSNull(JSContextRef context
) {
323 return JSValueMakeNull(context
);
326 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
327 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
330 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
331 return CYCastJSValue(context
, CYJSString(value
));
334 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
335 return _jsccall(JSValueToObject
, context
, value
);
338 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
339 return _jsccall(JSObjectCallAsFunction
, context
, function
, _this
, count
, arguments
);
342 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
343 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
346 bool CYIsEqual(JSContextRef context
, JSValueRef lhs
, JSValueRef rhs
) {
347 return _jsccall(JSValueIsEqual
, context
, lhs
, rhs
);
350 bool CYIsStrictEqual(JSContextRef context
, JSValueRef lhs
, JSValueRef rhs
) {
351 return JSValueIsStrictEqual(context
, lhs
, rhs
);
354 size_t CYArrayLength(JSContextRef context
, JSObjectRef array
) {
355 return CYCastDouble(context
, CYGetProperty(context
, array
, length_s
));
358 JSValueRef
CYArrayGet(JSContextRef context
, JSObjectRef array
, size_t index
) {
359 return _jsccall(JSObjectGetPropertyAtIndex
, context
, array
, index
);
362 void CYArrayPush(JSContextRef context
, JSObjectRef array
, JSValueRef value
) {
363 JSValueRef arguments
[1];
364 arguments
[0] = value
;
365 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
366 _jsccall(JSObjectCallAsFunction
, context
, CYCastJSObject(context
, CYGetProperty(context
, Array
, push_s
)), array
, 1, arguments
);
369 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
376 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, arguments
[0])));
377 fwrite(string
.data
, string
.size
, 1, file
);
381 return CYJSUndefined(context
);
384 static void (*JSSynchronousGarbageCollectForDebugging$
)(JSContextRef
);
386 _visible
void CYGarbageCollect(JSContextRef context
) {
387 (JSSynchronousGarbageCollectForDebugging$
?: &JSGarbageCollect
)(context
);
390 static JSValueRef
Cycript_compile_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
392 CYUTF8String
before(CYPoolUTF8String(pool
, context
, CYJSString(context
, arguments
[0])));
393 std::stringbuf
value(std::string(before
.data
, before
.size
));
394 CYUTF8String
after(CYPoolCode(pool
, value
));
395 return CYCastJSValue(context
, CYJSString(after
));
396 } CYCatch_(NULL
, "SyntaxError") }
398 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
399 CYGarbageCollect(context
);
400 return CYJSUndefined(context
);
403 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> &objects
, JSValueRef
*exception
) { CYTry
{
404 switch (JSType type
= JSValueGetType(context
, value
)) {
405 case kJSTypeUndefined
:
410 return CYCastBool(context
, value
) ? "true" : "false";
412 case kJSTypeNumber
: {
413 std::ostringstream str
;
414 CYNumerify(str
, CYCastDouble(context
, value
));
415 std::string
value(str
.str());
416 return pool
.strmemdup(value
.c_str(), value
.size());
419 case kJSTypeString
: {
420 std::ostringstream str
;
421 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
422 CYStringify(str
, string
.data
, string
.size
);
423 std::string
value(str
.str());
424 return pool
.strmemdup(value
.c_str(), value
.size());
428 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
, objects
);
430 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
434 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> &objects
) {
435 return _jsccall(CYPoolCCYON
, pool
, context
, value
, objects
);
438 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> *objects
) {
440 return CYPoolCCYON(pool
, context
, value
, *objects
);
442 std::set
<void *> objects
;
443 return CYPoolCCYON(pool
, context
, value
, objects
);
447 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSObjectRef object
, std::set
<void *> &objects
) {
448 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
449 if (CYIsCallable(context
, toCYON
)) {
450 // XXX: this needs to be abstracted behind some kind of function
451 JSValueRef arguments
[1] = {CYCastJSValue(context
, reinterpret_cast<uintptr_t>(&objects
))};
452 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 1, arguments
));
453 _assert(value
!= NULL
);
454 return CYPoolCString(pool
, context
, value
);
457 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
458 if (CYIsCallable(context
, toJSON
)) {
459 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
460 return _jsccall(CYPoolCCYON
, pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), objects
);
463 if (JSObjectIsFunction(context
, object
)) {
464 JSValueRef
toString(CYGetProperty(context
, object
, toString_s
));
465 if (CYIsCallable(context
, toString
)) {
466 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
467 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toString
, object
, 1, arguments
));
468 _assert(value
!= NULL
);
469 return CYPoolCString(pool
, context
, value
);
473 _assert(objects
.insert(object
).second
);
475 std::ostringstream str
;
479 // XXX: this is, sadly, going to leak
480 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
484 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
490 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
491 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
496 CYStringify(str
, string
.data
, string
.size
);
501 JSValueRef
value(CYGetProperty(context
, object
, name
));
502 str
<< CYPoolCCYON(pool
, context
, value
, objects
);
503 } catch (const CYException
&error
) {
508 JSPropertyNameArrayRelease(names
);
512 std::string
string(str
.str());
513 return pool
.strmemdup(string
.c_str(), string
.size());
516 std::set
<void *> *CYCastObjects(JSContextRef context
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
519 return CYCastPointer
<std::set
<void *> *>(context
, arguments
[0]);
522 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
523 std::set
<void *> *objects(CYCastObjects(context
, _this
, count
, arguments
));
524 // XXX: this is horribly inefficient
525 std::set
<void *> backup
;
530 std::ostringstream str
;
534 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
537 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
544 JSValueRef
value(CYGetProperty(context
, _this
, index
));
545 if (!JSValueIsUndefined(context
, value
))
546 str
<< CYPoolCCYON(pool
, context
, value
, *objects
);
551 } catch (const CYException
&error
) {
558 std::string
value(str
.str());
559 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
562 static JSValueRef
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
564 std::ostringstream str
;
566 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
)));
567 CYStringify(str
, string
.data
, string
.size
);
569 std::string
value(str
.str());
570 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
573 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
574 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
575 return JSObjectMake(context
, Pointer_
, internal
);
578 JSObjectRef
CYMakeCString(JSContextRef context
, char *pointer
, JSObjectRef owner
) {
579 CString
*internal(new CString(pointer
, context
, owner
));
580 return JSObjectMake(context
, CString_
, internal
);
583 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const sig::Signature
&signature
) {
584 return JSObjectMake(context
, Functor_
, new cy::Functor(signature
, function
));
587 static JSObjectRef
CYMakeFunctor(JSContextRef context
, const char *symbol
, const char *encoding
, void **cache
) {
588 cy::Functor
*internal
;
590 internal
= reinterpret_cast<cy::Functor
*>(*cache
);
592 void (*function
)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol
)));
593 if (function
== NULL
)
596 internal
= new cy::Functor(encoding
, function
);
601 return JSObjectMake(context
, Functor_
, internal
);
604 static bool CYGetOffset(CYPool
&pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
605 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
608 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
611 else switch (JSValueGetType(context
, value
)) {
614 case kJSTypeObject
: {
615 JSObjectRef
object((JSObjectRef
) value
);
616 if (JSValueIsObjectOfClass(context
, value
, CString_
)) {
617 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(object
)));
618 return internal
->value_
;
620 if (JSValueIsObjectOfClass(context
, value
, Pointer_
)) {
621 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
622 return internal
->value_
;
624 JSValueRef
toPointer(CYGetProperty(context
, object
, toPointer_s
));
625 if (CYIsCallable(context
, toPointer
)) {
626 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
));
627 _assert(value
!= NULL
);
628 return CYCastPointer_(context
, value
);
631 double number(CYCastDouble(context
, value
));
632 if (std::isnan(number
))
633 throw CYJSError(context
, "cannot convert value to pointer");
634 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
638 void CYPoolFFI(CYPool
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
639 switch (type
->primitive
) {
641 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
644 #define CYPoolFFI_(primitive, native) \
645 case sig::primitive ## _P: \
646 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
649 CYPoolFFI_(uchar
, unsigned char)
650 CYPoolFFI_(char, char)
651 CYPoolFFI_(ushort
, unsigned short)
652 CYPoolFFI_(short, short)
653 CYPoolFFI_(ulong
, unsigned long)
654 CYPoolFFI_(long, long)
655 CYPoolFFI_(uint
, unsigned int)
657 CYPoolFFI_(ulonglong
, unsigned long long)
658 CYPoolFFI_(longlong
, long long)
659 CYPoolFFI_(float, float)
660 CYPoolFFI_(double, double)
663 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
664 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
665 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
666 ffi_type
*field(ffi
->elements
[index
]);
669 if (aggregate
== NULL
)
672 rhs
= CYGetProperty(context
, aggregate
, index
);
673 if (JSValueIsUndefined(context
, rhs
))
674 throw CYJSError(context
, "unable to extract array value");
677 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
684 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
688 _assert(pool
!= NULL
);
689 *reinterpret_cast<const char **>(data
) = CYPoolCString(*pool
, context
, value
);
692 case sig::struct_P
: {
693 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
694 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
695 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
696 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
697 ffi_type
*field(ffi
->elements
[index
]);
700 if (aggregate
== NULL
)
703 rhs
= CYGetProperty(context
, aggregate
, index
);
704 if (JSValueIsUndefined(context
, rhs
)) {
705 if (element
->name
!= NULL
)
706 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
709 if (JSValueIsUndefined(context
, rhs
)) undefined
:
710 throw CYJSError(context
, "unable to extract structure value");
714 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
724 for (CYHook
*hook
: GetHooks())
725 if (hook
->PoolFFI
!= NULL
)
726 if ((*hook
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
729 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
733 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
734 switch (type
->primitive
) {
736 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
738 #define CYFromFFI_(primitive, native) \
739 case sig::primitive ## _P: \
740 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
742 CYFromFFI_(uchar, unsigned char)
743 CYFromFFI_(char, char)
744 CYFromFFI_(ushort
, unsigned short)
745 CYFromFFI_(short, short)
746 CYFromFFI_(ulong
, unsigned long)
747 CYFromFFI_(long, long)
748 CYFromFFI_(uint
, unsigned int)
750 CYFromFFI_(ulonglong
, unsigned long long)
751 CYFromFFI_(longlong
, long long)
752 CYFromFFI_(float, float)
753 CYFromFFI_(double, double)
756 if (void *pointer
= data
)
757 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
761 if (void *pointer
= *reinterpret_cast<void **>(data
))
762 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
766 if (char *pointer
= *reinterpret_cast<char **>(data
))
767 return CYMakeCString(context
, pointer
, owner
);
771 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
773 return CYJSUndefined(context
);
776 return CYJSNull(context
);
778 for (CYHook
*hook
: GetHooks())
779 if (hook
->FromFFI
!= NULL
)
780 if (JSValueRef value
= (*hook
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
783 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
787 void CYExecuteClosure(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
788 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
790 JSContextRef
context(internal
->context_
);
792 size_t count(internal
->cif_
.nargs
);
793 JSValueRef values
[count
];
795 for (size_t index(0); index
!= count
; ++index
)
796 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
798 JSValueRef
value(internal
->adapter_(context
, count
, values
, internal
->function_
));
799 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
802 static JSValueRef
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
) {
803 return CYCallAsFunction(context
, function
, NULL
, count
, values
);
806 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
, JSValueRef (*adapter
)(JSContextRef
, size_t, JSValueRef
[], JSObjectRef
)) {
807 // XXX: in case of exceptions this will leak
808 // XXX: in point of fact, this may /need/ to leak :(
809 Closure_privateData
*internal(new Closure_privateData(context
, function
, adapter
, signature
));
811 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
813 ffi_closure
*writable(reinterpret_cast<ffi_closure
*>(ffi_closure_alloc(sizeof(ffi_closure
), &executable
)));
815 ffi_status
status(ffi_prep_closure_loc(writable
, &internal
->cif_
, &CYExecuteClosure
, internal
, executable
));
816 _assert(status
== FFI_OK
);
818 internal
->value_
= executable
;
820 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
821 NULL
, sizeof(ffi_closure
),
822 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
826 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, &CYExecuteClosure
, internal
));
827 _assert(status
== FFI_OK
);
829 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
831 internal
->value_
= closure
;
837 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
) {
838 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, signature
, &FunctionAdapter_
));
839 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
840 // XXX: see above notes about needing to leak
841 JSValueProtect(CYGetJSContext(context
), object
);
845 JSValueRef
CYGetCachedValue(JSContextRef context
, JSStringRef name
) {
846 return CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
);
849 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
850 return CYCastJSObject(context
, CYGetCachedValue(context
, name
));
853 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const sig::Signature
&signature
) {
854 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
856 bool function(_jsccall(JSValueIsInstanceOfConstructor
, context
, value
, Function
));
858 JSObjectRef
function(CYCastJSObject(context
, value
));
859 return CYMakeFunctor(context
, function
, signature
);
861 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
862 return CYMakeFunctor(context
, function
, signature
);
866 static JSValueRef
CString_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
868 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(object
)));
869 char *string(static_cast<char *>(internal
->value_
));
872 if (!CYGetOffset(pool
, context
, property
, offset
))
875 return CYCastJSValue(context
, CYJSString(CYUTF8String(&string
[offset
], 1)));
878 static bool CString_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
880 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(object
)));
881 char *string(static_cast<char *>(internal
->value_
));
884 if (!CYGetOffset(pool
, context
, property
, offset
))
887 const char *data(CYPoolCString(pool
, context
, value
));
888 string
[offset
] = *data
;
892 static bool Index_(CYPool
&pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
893 Type_privateData
*typical(internal
->type_
);
894 sig::Type
*type(typical
->type_
);
898 const char *name(CYPoolCString(pool
, context
, property
));
899 size_t length(strlen(name
));
900 double number(CYCastDouble(name
, length
));
902 size_t count(type
->data
.signature
.count
);
904 if (std::isnan(number
)) {
905 if (property
== NULL
)
908 sig::Element
*elements(type
->data
.signature
.elements
);
910 for (size_t local(0); local
!= count
; ++local
) {
911 sig::Element
*element(&elements
[local
]);
912 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
920 index
= static_cast<ssize_t
>(number
);
921 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
926 ffi_type
**elements(typical
->GetFFI()->elements
);
928 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
929 for (ssize_t
local(0); local
!= index
; ++local
)
930 base
+= elements
[local
]->size
;
935 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
937 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
939 if (JSStringIsEqual(property
, length_s
))
940 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
942 Type_privateData
*typical(internal
->type_
);
943 if (typical
->type_
== NULL
)
945 sig::Type
&type(*typical
->type_
);
948 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
950 else if (!CYGetOffset(pool
, context
, property
, offset
))
953 if (type
.primitive
== sig::function_P
)
954 return CYMakeFunctor(context
, reinterpret_cast<void (*)()>(internal
->value_
), type
.data
.signature
);
956 ffi_type
*ffi(typical
->GetFFI());
958 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
959 base
+= ffi
->size
* offset
;
961 JSObjectRef
owner(internal
->GetOwner() ?: object
);
962 return CYFromFFI(context
, &type
, ffi
, base
, false, owner
);
965 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
967 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
968 Type_privateData
*typical(internal
->type_
);
970 if (typical
->type_
== NULL
)
974 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
976 else if (!CYGetOffset(pool
, context
, property
, offset
))
979 ffi_type
*ffi(typical
->GetFFI());
981 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
982 base
+= ffi
->size
* offset
;
984 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
988 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
989 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
990 Type_privateData
*typical(internal
->type_
);
991 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
994 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
996 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
997 Type_privateData
*typical(internal
->type_
);
1002 if (!Index_(pool
, context
, internal
, property
, index
, base
))
1005 JSObjectRef
owner(internal
->GetOwner() ?: object
);
1007 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
1010 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1012 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1013 Type_privateData
*typical(internal
->type_
);
1018 if (!Index_(pool
, context
, internal
, property
, index
, base
))
1021 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
1025 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1026 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1027 Type_privateData
*typical(internal
->type_
);
1028 sig::Type
*type(typical
->type_
);
1033 size_t count(type
->data
.signature
.count
);
1034 sig::Element
*elements(type
->data
.signature
.elements
);
1038 for (size_t index(0); index
!= count
; ++index
) {
1040 name
= elements
[index
].name
;
1043 sprintf(number
, "%zu", index
);
1047 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
1051 void CYCallFunction(CYPool
&pool
, JSContextRef context
, ffi_cif
*cif
, void (*function
)(), void *value
, void **values
) {
1052 ffi_call(cif
, function
, value
, values
);
1055 JSValueRef
CYCallFunction(CYPool
&pool
, JSContextRef context
, size_t setups
, void *setup
[], size_t count
, const JSValueRef arguments
[], bool initialize
, sig::Signature
*signature
, ffi_cif
*cif
, void (*function
)()) {
1056 if (setups
+ count
!= signature
->count
- 1)
1057 throw CYJSError(context
, "incorrect number of arguments to ffi function");
1059 size_t size(setups
+ count
);
1061 memcpy(values
, setup
, sizeof(void *) * setups
);
1063 for (size_t index(setups
); index
!= size
; ++index
) {
1064 sig::Element
*element(&signature
->elements
[index
+ 1]);
1065 ffi_type
*ffi(cif
->arg_types
[index
]);
1067 values
[index
] = new(pool
) uint8_t[ffi
->size
];
1068 CYPoolFFI(&pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
1071 uint8_t value
[cif
->rtype
->size
];
1073 void (*call
)(CYPool
&, JSContextRef
, ffi_cif
*, void (*)(), void *, void **) = &CYCallFunction
;
1074 // XXX: this only supports one hook, but it is a bad idea anyway
1075 for (CYHook
*hook
: GetHooks())
1076 if (hook
->CallFunction
!= NULL
)
1077 call
= hook
->CallFunction
;
1079 call(pool
, context
, cif
, function
, value
, values
);
1080 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
1083 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1085 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1086 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
1089 JSObjectRef
CYMakeType(JSContextRef context
, const char *encoding
) {
1090 Type_privateData
*internal(new Type_privateData(encoding
));
1091 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1094 JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
1095 Type_privateData
*internal(new Type_privateData(type
));
1096 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1099 JSObjectRef
CYMakeType(JSContextRef context
, sig::Signature
*signature
) {
1106 type
.primitive
= sig::function_P
;
1107 sig::Copy(pool
, type
.data
.signature
, *signature
);
1109 return CYMakeType(context
, &type
);
1112 static bool All_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
1113 JSObjectRef
global(CYGetGlobalObject(context
));
1114 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1115 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1117 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1118 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1119 if (CYHasProperty(context
, space
, property
))
1123 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
1125 size_t length(name
.size
);
1126 char keyed
[length
+ 2];
1127 memcpy(keyed
+ 1, name
.data
, length
+ 1);
1129 static const char *modes
= "0124";
1130 for (size_t i(0); i
!= 4; ++i
) {
1131 keyed
[0] = modes
[i
];
1132 if (CYBridgeHash(keyed
, length
+ 1) != NULL
)
1139 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1140 JSObjectRef
global(CYGetGlobalObject(context
));
1141 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1142 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1144 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1145 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1146 if (JSValueRef value
= CYGetProperty(context
, space
, property
))
1147 if (!JSValueIsUndefined(context
, value
))
1151 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
1153 size_t length(name
.size
);
1154 char keyed
[length
+ 2];
1155 memcpy(keyed
+ 1, name
.data
, length
+ 1);
1157 static const char *modes
= "0124";
1158 for (size_t i(0); i
!= 4; ++i
) {
1159 char mode(modes
[i
]);
1162 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
1165 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
);
1168 return CYMakeFunctor(context
, name
.data
, entry
->value_
, &entry
->cache_
);
1171 if (void *symbol
= CYCastSymbol(name
.data
)) {
1172 // XXX: this is horrendously inefficient
1173 sig::Signature signature
;
1174 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
1176 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1177 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
1180 // XXX: implement case 3
1182 return CYMakeType(context
, entry
->value_
);
1189 static void All_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1190 JSObjectRef
global(CYGetGlobalObject(context
));
1191 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1192 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1194 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1195 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1))) {
1196 JSPropertyNameArrayRef
subset(JSObjectCopyPropertyNames(context
, space
));
1197 for (size_t index(0), count(JSPropertyNameArrayGetCount(subset
)); index
!= count
; ++index
)
1198 JSPropertyNameAccumulatorAddName(names
, JSPropertyNameArrayGetNameAtIndex(subset
, index
));
1199 JSPropertyNameArrayRelease(subset
);
1203 static JSObjectRef
CString_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1205 throw CYJSError(context
, "incorrect number of arguments to CString constructor");
1206 char *value(CYCastPointer
<char *>(context
, arguments
[0]));
1207 return CYMakeCString(context
, value
, NULL
);
1210 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1212 throw CYJSError(context
, "incorrect number of arguments to Pointer constructor");
1216 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1217 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1219 sig::Signature signature
;
1220 sig::Parse(pool
, &signature
, type
, &Structor_
);
1222 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1225 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1229 } else if (count
== 1) {
1230 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1231 return CYMakeType(context
, type
);
1232 } else if (count
== 2) {
1233 JSObjectRef
types(CYCastJSObject(context
, arguments
[0]));
1234 size_t count(CYArrayLength(context
, types
));
1236 JSObjectRef
names(CYCastJSObject(context
, arguments
[1]));
1242 type
.primitive
= sig::struct_P
;
1243 type
.data
.signature
.elements
= new(pool
) sig::Element
[count
];
1244 type
.data
.signature
.count
= count
;
1246 for (size_t i(0); i
!= count
; ++i
) {
1247 sig::Element
&element(type
.data
.signature
.elements
[i
]);
1248 element
.offset
= _not(size_t);
1250 JSValueRef
name(CYArrayGet(context
, names
, i
));
1251 if (JSValueIsUndefined(context
, name
))
1252 element
.name
= NULL
;
1254 element
.name
= CYPoolCString(pool
, context
, name
);
1256 JSObjectRef
object(CYCastJSObject(context
, CYArrayGet(context
, types
, i
)));
1257 _assert(JSValueIsObjectOfClass(context
, object
, Type_privateData::Class_
));
1258 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1259 element
.type
= internal
->type_
;
1262 return CYMakeType(context
, &type
);
1264 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1268 static JSValueRef Type_callAsFunction_$
With(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], sig::Primitive primitive
, JSValueRef
*exception
) { CYTry
{
1269 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1277 type
.primitive
= primitive
;
1278 type
.data
.signature
.elements
= new(pool
) sig::Element
[1 + count
];
1279 type
.data
.signature
.count
= 1 + count
;
1281 type
.data
.signature
.elements
[0].name
= NULL
;
1282 type
.data
.signature
.elements
[0].type
= internal
->type_
;
1283 type
.data
.signature
.elements
[0].offset
= _not(size_t);
1285 for (size_t i(0); i
!= count
; ++i
) {
1286 sig::Element
&element(type
.data
.signature
.elements
[i
+ 1]);
1287 element
.name
= NULL
;
1288 element
.offset
= _not(size_t);
1290 JSObjectRef
object(CYCastJSObject(context
, arguments
[i
]));
1291 _assert(JSValueIsObjectOfClass(context
, object
, Type_privateData::Class_
));
1292 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1294 element
.type
= internal
->type_
;
1297 return CYMakeType(context
, &type
);
1300 static JSValueRef
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1302 throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf");
1303 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1306 size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0])));
1307 if (index
== _not(size_t))
1308 throw CYJSError(context
, "invalid array size used with Type.arrayOf");
1314 type
.primitive
= sig::array_P
;
1315 type
.data
.data
.type
= internal
->type_
;
1316 type
.data
.data
.size
= index
;
1318 return CYMakeType(context
, &type
);
1321 static JSValueRef
Type_callAsFunction_blockWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1322 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::block_P
, exception
);
1325 static JSValueRef
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1327 throw CYJSError(context
, "incorrect number of arguments to Type.constant");
1328 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1330 sig::Type
type(*internal
->type_
);
1331 type
.flags
|= JOC_TYPE_CONST
;
1332 return CYMakeType(context
, &type
);
1335 static JSValueRef
Type_callAsFunction_long(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1337 throw CYJSError(context
, "incorrect number of arguments to Type.long");
1338 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1340 sig::Type
type(*internal
->type_
);
1342 switch (type
.primitive
) {
1343 case sig::short_P
: type
.primitive
= sig::int_P
; break;
1344 case sig::int_P
: type
.primitive
= sig::long_P
; break;
1345 case sig::long_P
: type
.primitive
= sig::longlong_P
; break;
1346 default: throw CYJSError(context
, "invalid type argument to Type.long");
1349 return CYMakeType(context
, &type
);
1352 static JSValueRef
Type_callAsFunction_short(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1354 throw CYJSError(context
, "incorrect number of arguments to Type.short");
1355 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1357 sig::Type
type(*internal
->type_
);
1359 switch (type
.primitive
) {
1360 case sig::int_P
: type
.primitive
= sig::short_P
; break;
1361 case sig::long_P
: type
.primitive
= sig::int_P
; break;
1362 case sig::longlong_P
: type
.primitive
= sig::long_P
; break;
1363 default: throw CYJSError(context
, "invalid type argument to Type.short");
1366 return CYMakeType(context
, &type
);
1369 static JSValueRef
Type_callAsFunction_signed(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1371 throw CYJSError(context
, "incorrect number of arguments to Type.signed");
1372 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1374 sig::Type
type(*internal
->type_
);
1376 switch (type
.primitive
) {
1377 case sig::char_P
: case sig::uchar_P
: type
.primitive
= sig::char_P
; break;
1378 case sig::short_P
: case sig::ushort_P
: type
.primitive
= sig::short_P
; break;
1379 case sig::int_P
: case sig::uint_P
: type
.primitive
= sig::int_P
; break;
1380 case sig::long_P
: case sig::ulong_P
: type
.primitive
= sig::long_P
; break;
1381 case sig::longlong_P
: case sig::ulonglong_P
: type
.primitive
= sig::longlong_P
; break;
1382 default: throw CYJSError(context
, "invalid type argument to Type.signed");
1385 return CYMakeType(context
, &type
);
1388 static JSValueRef
Type_callAsFunction_unsigned(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1390 throw CYJSError(context
, "incorrect number of arguments to Type.unsigned");
1391 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1393 sig::Type
type(*internal
->type_
);
1395 switch (type
.primitive
) {
1396 case sig::char_P
: case sig::uchar_P
: type
.primitive
= sig::uchar_P
; break;
1397 case sig::short_P
: case sig::ushort_P
: type
.primitive
= sig::ushort_P
; break;
1398 case sig::int_P
: case sig::uint_P
: type
.primitive
= sig::uint_P
; break;
1399 case sig::long_P
: case sig::ulong_P
: type
.primitive
= sig::ulong_P
; break;
1400 case sig::longlong_P
: case sig::ulonglong_P
: type
.primitive
= sig::ulonglong_P
; break;
1401 default: throw CYJSError(context
, "invalid type argument to Type.unsigned");
1404 return CYMakeType(context
, &type
);
1407 static JSValueRef
Type_callAsFunction_functionWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1408 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::function_P
, exception
);
1411 static JSValueRef
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1413 throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo");
1414 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1419 if (internal
->type_
->primitive
== sig::char_P
) {
1420 type
.flags
= internal
->type_
->flags
;
1421 type
.primitive
= sig::string_P
;
1422 type
.data
.data
.type
= NULL
;
1423 type
.data
.data
.size
= 0;
1426 type
.primitive
= sig::pointer_P
;
1427 type
.data
.data
.type
= internal
->type_
;
1428 type
.data
.data
.size
= 0;
1431 return CYMakeType(context
, &type
);
1434 static JSValueRef
Type_callAsFunction_withName(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1436 throw CYJSError(context
, "incorrect number of arguments to Type.withName");
1437 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1440 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1442 sig::Type
type(*internal
->type_
);
1444 return CYMakeType(context
, &type
);
1447 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1449 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1450 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1452 if (internal
->type_
->primitive
== sig::function_P
)
1453 return CYMakeFunctor(context
, arguments
[0], internal
->type_
->data
.signature
);
1455 sig::Type
*type(internal
->type_
);
1456 ffi_type
*ffi(internal
->GetFFI());
1458 uint8_t value
[ffi
->size
];
1460 CYPoolFFI(&pool
, context
, type
, ffi
, value
, arguments
[0]);
1461 return CYFromFFI(context
, type
, ffi
, value
);
1464 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1466 throw CYJSError(context
, "incorrect number of arguments to Type allocator");
1467 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1469 sig::Type
*type(internal
->type_
);
1472 if (type
->primitive
!= sig::array_P
)
1473 length
= _not(size_t);
1475 length
= type
->data
.data
.size
;
1476 type
= type
->data
.data
.type
;
1479 void *value(calloc(1, internal
->GetFFI()->size
));
1480 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1483 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1485 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1487 const char *encoding(CYPoolCString(pool
, context
, arguments
[1]));
1488 sig::Signature signature
;
1489 sig::Parse(pool
, &signature
, encoding
, &Structor_
);
1490 return CYMakeFunctor(context
, arguments
[0], signature
);
1493 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1494 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1495 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1498 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1499 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1502 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1503 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1505 sprintf(string
, "%p", internal
->value_
);
1506 return CYCastJSValue(context
, string
);
1509 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1510 std::set
<void *> *objects(CYCastObjects(context
, _this
, count
, arguments
));
1512 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1513 if (internal
->length_
!= _not(size_t)) {
1514 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
1515 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1516 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1517 } else if (internal
->type_
->type_
== NULL
) pointer
: {
1519 sprintf(string
, "%p", internal
->value_
);
1520 return CYCastJSValue(context
, string
);
1522 JSValueRef
value(CYGetProperty(context
, _this
, cyi_s
));
1523 if (JSValueIsUndefined(context
, value
))
1526 return CYCastJSValue(context
, pool
.strcat("&", CYPoolCCYON(pool
, context
, value
, objects
), NULL
));
1527 } catch (const CYException
&e
) {
1532 static JSValueRef
CString_getProperty_length(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1533 CString
*internal(reinterpret_cast<CString
*>(JSObjectGetPrivate(object
)));
1534 char *string(static_cast<char *>(internal
->value_
));
1535 return CYCastJSValue(context
, strlen(string
));
1538 static JSValueRef
CString_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1543 type
.primitive
= sig::char_P
;
1544 type
.data
.data
.type
= NULL
;
1545 type
.data
.data
.size
= 0;
1547 return CYMakeType(context
, &type
);
1550 static JSValueRef
Pointer_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1551 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1552 return CYMakeType(context
, internal
->type_
->type_
);
1555 static JSValueRef
CString_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1556 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1557 const char *string(static_cast<const char *>(internal
->value_
));
1558 std::ostringstream str
;
1560 CYStringify(str
, string
, strlen(string
));
1561 std::string
value(str
.str());
1562 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
1565 static JSValueRef
CString_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1566 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1567 const char *string(static_cast<const char *>(internal
->value_
));
1568 return CYCastJSValue(context
, string
);
1571 static JSValueRef
Functor_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1572 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1573 return CYMakeType(context
, &internal
->signature_
);
1576 static JSValueRef
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1577 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1578 return CYCastJSValue(context
, internal
->GetFFI()->alignment
);
1581 static JSValueRef
Type_getProperty_name(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1582 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1583 return CYCastJSValue(context
, internal
->type_
->name
);
1586 static JSValueRef
Type_getProperty_size(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1587 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1588 return CYCastJSValue(context
, internal
->GetFFI()->size
);
1591 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1592 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1594 const char *type(sig::Unparse(pool
, internal
->type_
));
1595 return CYCastJSValue(context
, CYJSString(type
));
1598 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1599 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1603 CYOutput
output(out
, options
);
1604 (new(pool
) CYTypeExpression(Decode(pool
, internal
->type_
)))->Output(output
, CYNoFlags
);
1605 return CYCastJSValue(context
, CYJSString(out
.str().c_str()));
1608 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1609 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1612 static JSStaticFunction CString_staticFunctions
[5] = {
1613 {"toCYON", &CString_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1614 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1615 {"toString", &CString_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1616 {"valueOf", &CString_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1620 static JSStaticValue CString_staticValues
[3] = {
1621 {"length", &CString_getProperty_length
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1622 {"type", &CString_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1623 {NULL
, NULL
, NULL
, 0}
1626 static JSStaticFunction Pointer_staticFunctions
[4] = {
1627 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1628 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1629 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1633 static JSStaticValue Pointer_staticValues
[2] = {
1634 {"type", &Pointer_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1635 {NULL
, NULL
, NULL
, 0}
1638 static JSStaticFunction Struct_staticFunctions
[2] = {
1639 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1643 static JSStaticFunction Functor_staticFunctions
[4] = {
1644 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1645 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1646 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1651 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1654 static JSStaticValue Functor_staticValues
[2] = {
1655 {"type", &Functor_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1656 {NULL
, NULL
, NULL
, 0}
1660 JSStaticValue
const * const Functor::StaticValues
= Functor_staticValues
;
1663 static JSStaticValue Type_staticValues
[4] = {
1664 {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1665 {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1666 {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1667 {NULL
, NULL
, NULL
, 0}
1670 static JSStaticFunction Type_staticFunctions
[14] = {
1671 {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1672 {"blockWith", &Type_callAsFunction_blockWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1673 {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1674 {"functionWith", &Type_callAsFunction_functionWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1675 {"long", &Type_callAsFunction_long
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1676 {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1677 {"short", &Type_callAsFunction_short
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1678 {"signed", &Type_callAsFunction_signed
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1679 {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1680 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1681 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1682 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1683 {"unsigned", &Type_callAsFunction_unsigned
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1687 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1689 _visible
void CYSetArgs(int argc
, const char *argv
[]) {
1690 JSContextRef
context(CYGetJSContext());
1691 JSValueRef args
[argc
];
1692 for (int i(0); i
!= argc
; ++i
)
1693 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1696 if (JSObjectMakeArray$
!= NULL
)
1697 array
= _jsccall(*JSObjectMakeArray$
, context
, argc
, args
);
1699 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1700 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1701 array
= CYCastJSObject(context
, value
);
1704 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1705 CYSetProperty(context
, System
, CYJSString("args"), array
);
1708 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1709 return JSContextGetGlobalObject(context
);
1712 // XXX: this is neither exceptin safe nor even terribly sane
1713 class ExecutionHandle
{
1715 JSContextRef context_
;
1716 std::vector
<void *> handles_
;
1719 ExecutionHandle(JSContextRef context
) :
1722 handles_
.resize(GetHooks().size());
1723 for (size_t i(0); i
!= GetHooks().size(); ++i
) {
1724 CYHook
*hook(GetHooks()[i
]);
1725 if (hook
->ExecuteStart
!= NULL
)
1726 handles_
[i
] = (*hook
->ExecuteStart
)(context_
);
1732 ~ExecutionHandle() {
1733 for (size_t i(GetHooks().size()); i
!= 0; --i
) {
1734 CYHook
*hook(GetHooks()[i
-1]);
1735 if (hook
->ExecuteEnd
!= NULL
)
1736 (*hook
->ExecuteEnd
)(context_
, handles_
[i
-1]);
1741 static volatile bool cancel_
;
1743 static bool CYShouldTerminate(JSContextRef context
, void *arg
) {
1747 _visible
const char *CYExecute(JSContextRef context
, CYPool
&pool
, CYUTF8String code
) {
1748 ExecutionHandle
handle(context
);
1751 if (&JSContextGroupSetExecutionTimeLimit
!= NULL
)
1752 JSContextGroupSetExecutionTimeLimit(JSContextGetGroup(context
), 0.5, &CYShouldTerminate
, NULL
);
1755 JSValueRef
result(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0));
1756 if (JSValueIsUndefined(context
, result
))
1759 std::set
<void *> objects
;
1760 const char *json(_jsccall(CYPoolCCYON
, pool
, context
, result
, objects
));
1761 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1764 } catch (const CYException
&error
) {
1765 return pool
.strcat("throw ", error
.PoolCString(pool
), NULL
);
1769 _visible
void CYCancel() {
1773 static bool initialized_
= false;
1775 void CYInitializeDynamic() {
1777 initialized_
= true;
1780 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1781 JSSynchronousGarbageCollectForDebugging$
= reinterpret_cast<void (*)(JSContextRef
)>(dlsym(RTLD_DEFAULT
, "JSSynchronousGarbageCollectForDebugging"));
1783 JSClassDefinition definition
;
1785 definition
= kJSClassDefinitionEmpty
;
1786 definition
.className
= "All";
1787 definition
.hasProperty
= &All_hasProperty
;
1788 definition
.getProperty
= &All_getProperty
;
1789 definition
.getPropertyNames
= &All_getPropertyNames
;
1790 All_
= JSClassCreate(&definition
);
1792 definition
= kJSClassDefinitionEmpty
;
1793 definition
.className
= "Context";
1794 definition
.finalize
= &CYFinalize
;
1795 Context_
= JSClassCreate(&definition
);
1797 definition
= kJSClassDefinitionEmpty
;
1798 definition
.className
= "CString";
1799 definition
.staticFunctions
= CString_staticFunctions
;
1800 definition
.staticValues
= CString_staticValues
;
1801 definition
.getProperty
= &CString_getProperty
;
1802 definition
.setProperty
= &CString_setProperty
;
1803 definition
.finalize
= &CYFinalize
;
1804 CString_
= JSClassCreate(&definition
);
1806 definition
= kJSClassDefinitionEmpty
;
1807 definition
.className
= "Functor";
1808 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1809 definition
.staticValues
= Functor_staticValues
;
1810 definition
.callAsFunction
= &Functor_callAsFunction
;
1811 definition
.finalize
= &CYFinalize
;
1812 Functor_
= JSClassCreate(&definition
);
1814 definition
= kJSClassDefinitionEmpty
;
1815 definition
.className
= "Pointer";
1816 definition
.staticFunctions
= Pointer_staticFunctions
;
1817 definition
.staticValues
= Pointer_staticValues
;
1818 definition
.getProperty
= &Pointer_getProperty
;
1819 definition
.setProperty
= &Pointer_setProperty
;
1820 definition
.finalize
= &CYFinalize
;
1821 Pointer_
= JSClassCreate(&definition
);
1823 definition
= kJSClassDefinitionEmpty
;
1824 definition
.className
= "Struct";
1825 definition
.staticFunctions
= Struct_staticFunctions
;
1826 definition
.getProperty
= &Struct_getProperty
;
1827 definition
.setProperty
= &Struct_setProperty
;
1828 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1829 definition
.finalize
= &CYFinalize
;
1830 Struct_
= JSClassCreate(&definition
);
1832 definition
= kJSClassDefinitionEmpty
;
1833 definition
.className
= "Type";
1834 definition
.staticValues
= Type_staticValues
;
1835 definition
.staticFunctions
= Type_staticFunctions
;
1836 definition
.callAsFunction
= &Type_callAsFunction
;
1837 definition
.callAsConstructor
= &Type_callAsConstructor
;
1838 definition
.finalize
= &CYFinalize
;
1839 Type_privateData::Class_
= JSClassCreate(&definition
);
1841 definition
= kJSClassDefinitionEmpty
;
1842 definition
.className
= "Global";
1843 //definition.getProperty = &Global_getProperty;
1844 Global_
= JSClassCreate(&definition
);
1846 Array_s
= JSStringCreateWithUTF8CString("Array");
1847 cy_s
= JSStringCreateWithUTF8CString("$cy");
1848 cyi_s
= JSStringCreateWithUTF8CString("$cyi");
1849 length_s
= JSStringCreateWithUTF8CString("length");
1850 message_s
= JSStringCreateWithUTF8CString("message");
1851 name_s
= JSStringCreateWithUTF8CString("name");
1852 pop_s
= JSStringCreateWithUTF8CString("pop");
1853 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1854 push_s
= JSStringCreateWithUTF8CString("push");
1855 splice_s
= JSStringCreateWithUTF8CString("splice");
1856 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1857 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1858 toPointer_s
= JSStringCreateWithUTF8CString("toPointer");
1859 toString_s
= JSStringCreateWithUTF8CString("toString");
1860 weak_s
= JSStringCreateWithUTF8CString("weak");
1862 Result_
= JSStringCreateWithUTF8CString("_");
1864 for (CYHook
*hook
: GetHooks())
1865 if (hook
->Initialize
!= NULL
)
1866 (*hook
->Initialize
)();
1869 void CYThrow(JSContextRef context
, JSValueRef value
) {
1871 throw CYJSError(context
, value
);
1874 const char *CYJSError::PoolCString(CYPool
&pool
) const {
1875 std::set
<void *> objects
;
1876 // XXX: this used to be CYPoolCString
1877 return CYPoolCCYON(pool
, context_
, value_
, objects
);
1880 JSValueRef
CYJSError::CastJSValue(JSContextRef context
, const char *name
) const {
1881 // XXX: what if the context is different? or the name? I dunno. ("epic" :/)
1885 JSValueRef
CYCastJSError(JSContextRef context
, const char *name
, const char *message
) {
1886 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString(name
)));
1887 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1888 return _jsccall(JSObjectCallAsConstructor
, context
, Error
, 1, arguments
);
1891 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
, const char *name
) const {
1892 return CYCastJSError(context
, name
, message_
);
1895 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1896 _assert(context
!= NULL
);
1901 va_start(args
, format
);
1902 // XXX: there might be a beter way to think about this
1903 const char *message(pool
.vsprintf(64, format
, args
));
1906 value_
= CYCastJSError(context
, "Error", message
);
1909 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1910 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1917 CYFile(void *data
, size_t size
) :
1924 static void CYFileExit(void *data
) {
1925 CYFile
*file(reinterpret_cast<CYFile
*>(data
));
1926 _syscall(munmap(file
->data_
, file
->size_
));
1929 static void *CYPoolFile(CYPool
&pool
, const char *path
, size_t *psize
) {
1930 int fd(_syscall_(open(path
, O_RDONLY
), 1, ENOENT
));
1935 _syscall(fstat(fd
, &stat
));
1936 size_t size(stat
.st_size
);
1941 _syscall(base
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, 0));
1943 CYFile
*file(new (pool
) CYFile(base
, size
));
1944 pool
.atexit(&CYFileExit
, file
);
1946 _syscall(close(fd
));
1950 static CYUTF8String
CYPoolFileUTF8String(CYPool
&pool
, const char *path
) {
1952 data
.data
= reinterpret_cast<char *>(CYPoolFile(pool
, path
, &data
.size
));
1956 static const char *CYPoolLibraryPath(CYPool
&pool
) {
1958 _assert(dladdr(reinterpret_cast<void *>(&CYPoolLibraryPath
), &addr
) != 0);
1959 char *lib(pool
.strdup(addr
.dli_fname
));
1961 char *slash(strrchr(lib
, '/'));
1962 _assert(slash
!= NULL
);
1968 static JSValueRef
require(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1969 _assert(count
== 1);
1972 const char *lib(CYPoolLibraryPath(pool
));
1974 CYJSString
property("exports");
1977 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1978 const char *path(pool
.strcat(lib
, "/cycript0.9/", name
, ".cy", NULL
));
1980 CYJSString
key(path
);
1981 JSObjectRef
modules(CYGetCachedObject(context
, CYJSString("modules")));
1982 JSValueRef
cache(CYGetProperty(context
, modules
, key
));
1984 if (!JSValueIsUndefined(context
, cache
))
1985 module = CYCastJSObject(context
, cache
);
1987 CYUTF8String
code(CYPoolFileUTF8String(pool
, path
));
1989 if (code
.data
== NULL
) {
1990 if (strchr(name
, '/') == NULL
&& (
1992 dlopen(pool
.strcat("/System/Library/Frameworks/", name
, ".framework/", name
, NULL
), RTLD_LAZY
| RTLD_GLOBAL
) != NULL
||
1993 dlopen(pool
.strcat("/System/Library/PrivateFrameworks/", name
, ".framework/", name
, NULL
), RTLD_LAZY
| RTLD_GLOBAL
) != NULL
||
1996 return CYJSUndefined(NULL
);
1998 CYThrow("Can't find module: %s", name
);
2001 module = JSObjectMake(context
, NULL
, NULL
);
2002 CYSetProperty(context
, modules
, key
, module);
2004 JSObjectRef
exports(JSObjectMake(context
, NULL
, NULL
));
2005 CYSetProperty(context
, module, property
, exports
);
2007 std::stringstream wrap
;
2008 wrap
<< "(function (exports, require, module) { " << code
<< "\n});";
2009 code
= CYPoolCode(pool
, *wrap
.rdbuf());
2011 JSValueRef
value(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0));
2012 JSObjectRef
function(CYCastJSObject(context
, value
));
2014 JSValueRef arguments
[3] = { exports
, JSObjectMakeFunctionWithCallback(context
, CYJSString("require"), &require
), module };
2015 CYCallAsFunction(context
, function
, NULL
, 3, arguments
);
2018 return CYGetProperty(context
, module, property
);
2021 static bool CYRunScript(JSGlobalContextRef context
, const char *path
) {
2023 CYUTF8String
code(CYPoolFileUTF8String(pool
, path
));
2024 if (code
.data
== NULL
)
2027 CYStream
stream(code
.data
, code
.data
+ code
.size
);
2028 code
= CYPoolCode(pool
, stream
);
2029 _jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0);
2033 extern "C" void CYDestroyWeak(JSWeakObjectMapRef weak
, void *data
) {
2036 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
2037 CYInitializeDynamic();
2039 JSObjectRef
global(CYGetGlobalObject(context
));
2041 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
2042 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
2044 /* Cache Globals {{{ */
2045 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
2046 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
2048 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
2049 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
2051 JSObjectRef
Boolean(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Boolean"))));
2052 CYSetProperty(context
, cy
, CYJSString("Boolean"), Boolean
);
2054 JSObjectRef
Boolean_prototype(CYCastJSObject(context
, CYGetProperty(context
, Boolean
, prototype_s
)));
2055 CYSetProperty(context
, cy
, CYJSString("Boolean_prototype"), Boolean_prototype
);
2057 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
2058 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
2060 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
2061 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
2063 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
2064 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
2066 JSObjectRef
Number(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Number"))));
2067 CYSetProperty(context
, cy
, CYJSString("Number"), Number
);
2069 JSObjectRef
Number_prototype(CYCastJSObject(context
, CYGetProperty(context
, Number
, prototype_s
)));
2070 CYSetProperty(context
, cy
, CYJSString("Number_prototype"), Number_prototype
);
2072 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
2073 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
2075 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
2076 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
2078 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
2079 CYSetProperty(context
, cy
, CYJSString("String"), String
);
2081 JSObjectRef
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
)));
2082 CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
);
2084 JSObjectRef
SyntaxError(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("SyntaxError"))));
2085 CYSetProperty(context
, cy
, CYJSString("SyntaxError"), SyntaxError
);
2088 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
2089 CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
2091 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
2092 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
2093 CYSetProperty(context
, cycript
, CYJSString("compile"), &Cycript_compile_callAsFunction
);
2094 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
2096 JSObjectRef
CString(JSObjectMakeConstructor(context
, CString_
, &CString_new
));
2097 CYSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, CString
, prototype_s
)), String_prototype
);
2098 CYSetProperty(context
, cycript
, CYJSString("CString"), CString
);
2100 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
2101 CYSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
2102 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
2104 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
2105 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
2107 JSObjectRef
modules(JSObjectMake(context
, NULL
, NULL
));
2108 CYSetProperty(context
, cy
, CYJSString("modules"), modules
);
2110 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
2111 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
2113 JSObjectRef
alls(_jsccall(JSObjectCallAsConstructor
, context
, Array
, 0, NULL
));
2114 CYSetProperty(context
, cycript
, CYJSString("alls"), alls
);
2117 JSObjectRef
last(NULL
), curr(global
);
2119 goto next
; for (JSValueRef next
;;) {
2120 if (JSValueIsNull(context
, next
))
2123 curr
= CYCastJSObject(context
, next
);
2125 next
= JSObjectGetPrototype(context
, curr
);
2128 CYSetPrototype(context
, last
, all
);
2131 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
2132 CYSetProperty(context
, cy
, CYJSString("System"), System
);
2134 CYSetProperty(context
, all
, CYJSString("require"), &require
, kJSPropertyAttributeDontEnum
);
2136 CYSetProperty(context
, global
, CYJSString("system"), System
);
2137 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
2138 //CYSetProperty(context, System, CYJSString("global"), global);
2139 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
2142 if (&JSWeakObjectMapCreate
!= NULL
) {
2143 JSWeakObjectMapRef
weak(JSWeakObjectMapCreate(context
, NULL
, &CYDestroyWeak
));
2144 CYSetProperty(context
, cy
, weak_s
, CYCastJSValue(context
, reinterpret_cast<uintptr_t>(weak
)));
2148 if (CYBridgeEntry
*entry
= CYBridgeHash("1dlerror", 8))
2149 entry
->cache_
= new cy::Functor(entry
->value_
, reinterpret_cast<void (*)()>(&dlerror
));
2151 CYRunScript(context
, "libcycript.cy");
2153 for (CYHook
*hook
: GetHooks())
2154 if (hook
->SetupContext
!= NULL
)
2155 (*hook
->SetupContext
)(context
);
2157 CYArrayPush(context
, alls
, cycript
);
2160 static JSGlobalContextRef context_
;
2162 _visible JSGlobalContextRef
CYGetJSContext() {
2163 CYInitializeDynamic();
2165 if (context_
== NULL
) {
2166 context_
= JSGlobalContextCreate(Global_
);
2167 CYSetupContext(context_
);
2173 _visible
void CYDestroyContext() {
2174 if (context_
== NULL
)
2176 JSGlobalContextRelease(context_
);