1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
7 * Cycript is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
12 * Cycript is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
22 #include "Internal.hpp"
26 #include "cycript.hpp"
28 #include "sig/parse.hpp"
29 #include "sig/ffi_type.hpp"
31 #include "Pooling.hpp"
32 #include "Execute.hpp"
46 #include "JavaScript.hpp"
49 struct CYHooks
*hooks_
;
51 /* JavaScript Properties {{{ */
52 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) {
53 return _jsccall(JSObjectGetPropertyAtIndex
, context
, object
, index
);
56 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
57 return _jsccall(JSObjectGetProperty
, context
, object
, name
);
60 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) {
61 _jsccall(JSObjectSetPropertyAtIndex
, context
, object
, index
, value
);
64 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) {
65 _jsccall(JSObjectSetProperty
, context
, object
, name
, value
, attributes
);
68 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef (*callback
)(JSContextRef
, JSObjectRef
, JSObjectRef
, size_t, const JSValueRef
[], JSValueRef
*), JSPropertyAttributes attributes
) {
69 CYSetProperty(context
, object
, name
, JSObjectMakeFunctionWithCallback(context
, name
, callback
), attributes
);
72 /* JavaScript Strings {{{ */
73 JSStringRef
CYCopyJSString(const char *value
) {
74 return value
== NULL
? NULL
: JSStringCreateWithUTF8CString(value
);
77 JSStringRef
CYCopyJSString(JSStringRef value
) {
78 return value
== NULL
? NULL
: JSStringRetain(value
);
81 JSStringRef
CYCopyJSString(CYUTF8String value
) {
82 // XXX: this is very wrong; it needs to convert to UTF16 and then create from there
83 return CYCopyJSString(value
.data
);
86 JSStringRef
CYCopyJSString(JSContextRef context
, JSValueRef value
) {
87 if (JSValueIsNull(context
, value
))
89 return _jsccall(JSValueToStringCopy
, context
, value
);
92 static CYUTF16String
CYCastUTF16String(JSStringRef value
) {
93 return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
));
96 CYUTF8String
CYPoolUTF8String(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
97 return CYPoolUTF8String(pool
, CYCastUTF16String(value
));
100 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
101 CYUTF8String
utf8(CYPoolUTF8String(pool
, context
, value
));
102 _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
);
106 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
107 return JSValueIsNull(context
, value
) ? NULL
: CYPoolCString(pool
, context
, CYJSString(context
, value
));
110 /* Index Offsets {{{ */
111 size_t CYGetIndex(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
112 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
116 static JSClassRef All_
;
117 static JSClassRef Context_
;
119 static JSClassRef Global_
;
120 static JSClassRef Pointer_
;
121 static JSClassRef Struct_
;
125 JSStringRef length_s
;
126 JSStringRef message_s
;
129 JSStringRef prototype_s
;
131 JSStringRef splice_s
;
132 JSStringRef toCYON_s
;
133 JSStringRef toJSON_s
;
134 JSStringRef toPointer_s
;
135 JSStringRef toString_s
;
137 static JSStringRef Result_
;
139 void CYFinalize(JSObjectRef object
) {
140 CYData
*internal(reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
)));
141 _assert(internal
->count_
!= _not(unsigned));
142 if (--internal
->count_
== 0)
146 void Structor_(CYPool
&pool
, sig::Type
*&type
) {
148 type
->primitive
== sig::pointer_P
&&
149 type
->data
.data
.type
!= NULL
&&
150 type
->data
.data
.type
->primitive
== sig::struct_P
&&
151 type
->data
.data
.type
->name
!= NULL
&&
152 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
154 type
->primitive
= sig::typename_P
;
155 type
->data
.data
.type
= NULL
;
159 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
162 size_t length(strlen(type
->name
));
163 char keyed
[length
+ 2];
164 memcpy(keyed
+ 1, type
->name
, length
+ 1);
166 static const char *modes
= "34";
167 for (size_t i(0); i
!= 2; ++i
) {
171 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
174 sig::Parse(pool
, &type
->data
.signature
, entry
->value_
, &Structor_
);
178 sig::Signature signature
;
179 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
180 type
= signature
.elements
[0].type
;
186 JSClassRef
Type_privateData::Class_
;
191 JSGlobalContextRef context_
;
193 Context(JSGlobalContextRef context
) :
202 Type_privateData
*type_
;
205 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type
*type
) :
206 CYOwned(value
, context
, owner
),
207 type_(new(*pool_
) Type_privateData(type
)),
213 struct Struct_privateData
:
216 Type_privateData
*type_
;
218 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
219 CYOwned(NULL
, context
, owner
)
224 typedef std::map
<const char *, Type_privateData
*, CYCStringLess
> TypeMap
;
225 static TypeMap Types_
;
227 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
228 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
229 CYPool
&pool(*internal
->pool_
);
230 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
231 internal
->type_
= typical
;
234 internal
->value_
= data
;
236 size_t size(typical
->GetFFI()->size
);
237 void *copy(internal
->pool_
->malloc
<void>(size
));
238 memcpy(copy
, data
, size
);
239 internal
->value_
= copy
;
242 return JSObjectMake(context
, Struct_
, internal
);
245 static void *CYCastSymbol(const char *name
) {
246 return dlsym(RTLD_DEFAULT
, name
);
249 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
250 return JSValueMakeBoolean(context
, value
);
253 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
254 return JSValueMakeNumber(context
, value
);
257 #define CYCastJSValue_(Type_) \
258 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
259 return JSValueMakeNumber(context, static_cast<double>(value)); \
263 CYCastJSValue_(unsigned int)
264 CYCastJSValue_(long int)
265 CYCastJSValue_(long unsigned int)
266 CYCastJSValue_(long long int)
267 CYCastJSValue_(long long unsigned int)
269 JSValueRef
CYJSUndefined(JSContextRef context
) {
270 return JSValueMakeUndefined(context
);
273 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
274 return _jsccall(JSValueToNumber
, context
, value
);
277 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
278 return JSValueToBoolean(context
, value
);
281 JSValueRef
CYJSNull(JSContextRef context
) {
282 return JSValueMakeNull(context
);
285 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
286 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
289 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
290 return CYCastJSValue(context
, CYJSString(value
));
293 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
294 return _jsccall(JSValueToObject
, context
, value
);
297 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
298 return _jsccall(JSObjectCallAsFunction
, context
, function
, _this
, count
, arguments
);
301 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
302 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
305 size_t CYArrayLength(JSContextRef context
, JSObjectRef array
) {
306 return CYCastDouble(context
, CYGetProperty(context
, array
, length_s
));
309 JSValueRef
CYArrayGet(JSContextRef context
, JSObjectRef array
, size_t index
) {
310 return _jsccall(JSObjectGetPropertyAtIndex
, context
, array
, index
);
313 void CYArrayPush(JSContextRef context
, JSObjectRef array
, JSValueRef value
) {
314 JSValueRef arguments
[1];
315 arguments
[0] = value
;
316 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
317 _jsccall(JSObjectCallAsFunction
, context
, CYCastJSObject(context
, CYGetProperty(context
, Array
, push_s
)), array
, 1, arguments
);
320 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
325 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
328 return CYJSUndefined(context
);
331 static size_t Nonce_(0);
333 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
335 const char *name(pool
.strcat(CYPoolCString(pool
, context
, arguments
[0]), pool
.itoa(Nonce_
++), NULL
));
336 return CYCastJSValue(context
, name
);
339 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
340 JSGarbageCollect(context
);
341 return CYJSUndefined(context
);
344 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
345 switch (JSType type
= JSValueGetType(context
, value
)) {
346 case kJSTypeUndefined
:
351 return CYCastBool(context
, value
) ? "true" : "false";
353 case kJSTypeNumber
: {
354 std::ostringstream str
;
355 CYNumerify(str
, CYCastDouble(context
, value
));
356 std::string
value(str
.str());
357 return pool
.strmemdup(value
.c_str(), value
.size());
360 case kJSTypeString
: {
361 std::ostringstream str
;
362 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
363 CYStringify(str
, string
.data
, string
.size
);
364 std::string
value(str
.str());
365 return pool
.strmemdup(value
.c_str(), value
.size());
369 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
371 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
375 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
376 return _jsccall(CYPoolCCYON
, pool
, context
, value
);
379 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSObjectRef object
) {
380 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
381 if (CYIsCallable(context
, toCYON
)) {
382 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
383 _assert(value
!= NULL
);
384 return CYPoolCString(pool
, context
, value
);
387 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
388 if (CYIsCallable(context
, toJSON
)) {
389 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
390 return _jsccall(CYPoolCCYON
, pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
));
393 if (JSObjectIsFunction(context
, object
)) {
394 JSValueRef
toString(CYGetProperty(context
, object
, toString_s
));
395 if (CYIsCallable(context
, toString
)) {
396 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
397 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toString
, object
, 1, arguments
));
398 _assert(value
!= NULL
);
399 return CYPoolCString(pool
, context
, value
);
403 std::ostringstream str
;
407 // XXX: this is, sadly, going to leak
408 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
412 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
418 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
419 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
424 CYStringify(str
, string
.data
, string
.size
);
429 JSValueRef
value(CYGetProperty(context
, object
, name
));
430 str
<< CYPoolCCYON(pool
, context
, value
);
431 } catch (const CYException
&error
) {
436 JSPropertyNameArrayRelease(names
);
440 std::string
string(str
.str());
441 return pool
.strmemdup(string
.c_str(), string
.size());
444 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
446 std::ostringstream str
;
450 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
453 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
454 JSValueRef
value(CYGetProperty(context
, _this
, index
));
461 if (!JSValueIsUndefined(context
, value
))
462 str
<< CYPoolCCYON(pool
, context
, value
);
471 std::string
value(str
.str());
472 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
475 static JSValueRef
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
477 std::ostringstream str
;
479 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
)));
480 CYStringify(str
, string
.data
, string
.size
);
482 std::string
value(str
.str());
483 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
486 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
487 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
488 return JSObjectMake(context
, Pointer_
, internal
);
491 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const char *type
) {
492 return JSObjectMake(context
, Functor_
, new cy::Functor(type
, function
));
495 static JSObjectRef
CYMakeFunctor(JSContextRef context
, const char *symbol
, const char *type
, void **cache
) {
496 cy::Functor
*internal
;
498 internal
= reinterpret_cast<cy::Functor
*>(*cache
);
500 void (*function
)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol
)));
501 if (function
== NULL
)
504 internal
= new cy::Functor(type
, function
);
509 return JSObjectMake(context
, Functor_
, internal
);
512 static bool CYGetOffset(CYPool
&pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
513 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
516 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
517 switch (JSValueGetType(context
, value
)) {
520 case kJSTypeObject
: {
521 JSObjectRef
object((JSObjectRef
) value
);
522 if (JSValueIsObjectOfClass(context
, value
, Pointer_
)) {
523 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
524 return internal
->value_
;
526 JSValueRef
toPointer(CYGetProperty(context
, object
, toPointer_s
));
527 if (CYIsCallable(context
, toPointer
)) {
528 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
));
529 _assert(value
!= NULL
);
530 return CYCastPointer_(context
, value
);
533 double number(CYCastDouble(context
, value
));
534 if (std::isnan(number
))
535 throw CYJSError(context
, "cannot convert value to pointer");
536 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
540 void CYPoolFFI(CYPool
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
541 switch (type
->primitive
) {
543 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
546 #define CYPoolFFI_(primitive, native) \
547 case sig::primitive ## _P: \
548 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
551 CYPoolFFI_(uchar
, unsigned char)
552 CYPoolFFI_(char, char)
553 CYPoolFFI_(ushort
, unsigned short)
554 CYPoolFFI_(short, short)
555 CYPoolFFI_(ulong
, unsigned long)
556 CYPoolFFI_(long, long)
557 CYPoolFFI_(uint
, unsigned int)
559 CYPoolFFI_(ulonglong
, unsigned long long)
560 CYPoolFFI_(longlong
, long long)
561 CYPoolFFI_(float, float)
562 CYPoolFFI_(double, double)
565 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
566 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
567 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
568 ffi_type
*field(ffi
->elements
[index
]);
571 if (aggregate
== NULL
)
574 rhs
= CYGetProperty(context
, aggregate
, index
);
575 if (JSValueIsUndefined(context
, rhs
))
576 throw CYJSError(context
, "unable to extract array value");
579 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
586 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
590 _assert(pool
!= NULL
);
591 *reinterpret_cast<const char **>(data
) = CYPoolCString(*pool
, context
, value
);
594 case sig::struct_P
: {
595 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
596 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
597 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
598 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
599 ffi_type
*field(ffi
->elements
[index
]);
602 if (aggregate
== NULL
)
605 rhs
= CYGetProperty(context
, aggregate
, index
);
606 if (JSValueIsUndefined(context
, rhs
)) {
607 if (element
->name
!= NULL
)
608 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
611 if (JSValueIsUndefined(context
, rhs
)) undefined
:
612 throw CYJSError(context
, "unable to extract structure value");
616 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
626 if (hooks_
!= NULL
&& hooks_
->PoolFFI
!= NULL
)
627 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
630 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
634 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
635 switch (type
->primitive
) {
637 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
639 #define CYFromFFI_(primitive, native) \
640 case sig::primitive ## _P: \
641 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
643 CYFromFFI_(uchar, unsigned char)
644 CYFromFFI_(char, char)
645 CYFromFFI_(ushort
, unsigned short)
646 CYFromFFI_(short, short)
647 CYFromFFI_(ulong
, unsigned long)
648 CYFromFFI_(long, long)
649 CYFromFFI_(uint
, unsigned int)
651 CYFromFFI_(ulonglong
, unsigned long long)
652 CYFromFFI_(longlong
, long long)
653 CYFromFFI_(float, float)
654 CYFromFFI_(double, double)
657 if (void *pointer
= data
)
658 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
662 if (void *pointer
= *reinterpret_cast<void **>(data
))
663 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
667 if (char *utf8
= *reinterpret_cast<char **>(data
))
668 return CYCastJSValue(context
, utf8
);
672 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
674 return CYJSUndefined(context
);
677 return CYJSNull(context
);
679 if (hooks_
!= NULL
&& hooks_
->FromFFI
!= NULL
)
680 if (JSValueRef value
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
683 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
687 void CYExecuteClosure(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
, JSValueRef (*adapter
)(JSContextRef
, size_t, JSValueRef
[], JSObjectRef
)) {
688 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
690 JSContextRef
context(internal
->context_
);
692 size_t count(internal
->cif_
.nargs
);
693 JSValueRef values
[count
];
695 for (size_t index(0); index
!= count
; ++index
)
696 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
698 JSValueRef
value(adapter(context
, count
, values
, internal
->function_
));
699 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
702 static JSValueRef
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
) {
703 return CYCallAsFunction(context
, function
, NULL
, count
, values
);
706 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
707 CYExecuteClosure(cif
, result
, arguments
, arg
, &FunctionAdapter_
);
710 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const char *type
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
711 // XXX: in case of exceptions this will leak
712 // XXX: in point of fact, this may /need/ to leak :(
713 Closure_privateData
*internal(new Closure_privateData(context
, function
, type
));
715 #if defined(__APPLE__) && defined(__arm__)
717 ffi_closure
*writable(reinterpret_cast<ffi_closure
*>(ffi_closure_alloc(sizeof(ffi_closure
), &executable
)));
719 ffi_status
status(ffi_prep_closure_loc(writable
, &internal
->cif_
, callback
, internal
, executable
));
720 _assert(status
== FFI_OK
);
722 internal
->value_
= executable
;
724 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
725 NULL
, sizeof(ffi_closure
),
726 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
730 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
731 _assert(status
== FFI_OK
);
733 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
735 internal
->value_
= closure
;
741 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const char *type
) {
742 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, type
, &FunctionClosure_
));
743 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
744 // XXX: see above notes about needing to leak
745 JSValueProtect(CYGetJSContext(context
), object
);
749 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
750 return CYCastJSObject(context
, CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
));
753 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const char *type
) {
754 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
756 bool function(_jsccall(JSValueIsInstanceOfConstructor
, context
, value
, Function
));
758 JSObjectRef
function(CYCastJSObject(context
, value
));
759 return CYMakeFunctor(context
, function
, type
);
761 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
762 return CYMakeFunctor(context
, function
, type
);
766 static bool Index_(CYPool
&pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
767 Type_privateData
*typical(internal
->type_
);
768 sig::Type
*type(typical
->type_
);
772 const char *name(CYPoolCString(pool
, context
, property
));
773 size_t length(strlen(name
));
774 double number(CYCastDouble(name
, length
));
776 size_t count(type
->data
.signature
.count
);
778 if (std::isnan(number
)) {
779 if (property
== NULL
)
782 sig::Element
*elements(type
->data
.signature
.elements
);
784 for (size_t local(0); local
!= count
; ++local
) {
785 sig::Element
*element(&elements
[local
]);
786 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
794 index
= static_cast<ssize_t
>(number
);
795 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
800 ffi_type
**elements(typical
->GetFFI()->elements
);
802 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
803 for (ssize_t
local(0); local
!= index
; ++local
)
804 base
+= elements
[local
]->size
;
809 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
811 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
813 if (JSStringIsEqual(property
, length_s
))
814 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
816 Type_privateData
*typical(internal
->type_
);
818 if (typical
->type_
== NULL
)
822 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
824 else if (!CYGetOffset(pool
, context
, property
, offset
))
827 ffi_type
*ffi(typical
->GetFFI());
829 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
830 base
+= ffi
->size
* offset
;
832 JSObjectRef
owner(internal
->GetOwner() ?: object
);
833 return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
);
836 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
838 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
839 Type_privateData
*typical(internal
->type_
);
841 if (typical
->type_
== NULL
)
845 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
847 else if (!CYGetOffset(pool
, context
, property
, offset
))
850 ffi_type
*ffi(typical
->GetFFI());
852 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
853 base
+= ffi
->size
* offset
;
855 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
859 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
860 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
861 Type_privateData
*typical(internal
->type_
);
862 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
865 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
867 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
868 Type_privateData
*typical(internal
->type_
);
873 if (!Index_(pool
, context
, internal
, property
, index
, base
))
876 JSObjectRef
owner(internal
->GetOwner() ?: object
);
878 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
881 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
883 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
884 Type_privateData
*typical(internal
->type_
);
889 if (!Index_(pool
, context
, internal
, property
, index
, base
))
892 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
896 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
897 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
898 Type_privateData
*typical(internal
->type_
);
899 sig::Type
*type(typical
->type_
);
904 size_t count(type
->data
.signature
.count
);
905 sig::Element
*elements(type
->data
.signature
.elements
);
909 for (size_t index(0); index
!= count
; ++index
) {
911 name
= elements
[index
].name
;
914 sprintf(number
, "%zu", index
);
918 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
922 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
)()) {
923 if (setups
+ count
!= signature
->count
- 1)
924 throw CYJSError(context
, "incorrect number of arguments to ffi function");
926 size_t size(setups
+ count
);
928 memcpy(values
, setup
, sizeof(void *) * setups
);
930 for (size_t index(setups
); index
!= size
; ++index
) {
931 sig::Element
*element(&signature
->elements
[index
+ 1]);
932 ffi_type
*ffi(cif
->arg_types
[index
]);
934 values
[index
] = new(pool
) uint8_t[ffi
->size
];
935 CYPoolFFI(&pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
938 uint8_t value
[cif
->rtype
->size
];
940 if (hooks_
!= NULL
&& hooks_
->CallFunction
!= NULL
)
941 (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
);
943 ffi_call(cif
, function
, value
, values
);
945 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
948 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
950 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
951 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
954 JSObjectRef
CYMakeType(JSContextRef context
, const char *type
) {
955 Type_privateData
*internal(new Type_privateData(type
));
956 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
959 JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
960 Type_privateData
*internal(new Type_privateData(type
));
961 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
964 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
965 JSObjectRef
global(CYGetGlobalObject(context
));
966 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
967 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
969 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
970 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
971 if (JSValueRef value
= CYGetProperty(context
, space
, property
))
972 if (!JSValueIsUndefined(context
, value
))
976 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
978 size_t length(name
.size
);
979 char keyed
[length
+ 2];
980 memcpy(keyed
+ 1, name
.data
, length
+ 1);
982 static const char *modes
= "0124";
983 for (size_t i(0); i
!= 4; ++i
) {
987 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
990 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
);
993 return CYMakeFunctor(context
, name
.data
, entry
->value_
, &entry
->cache_
);
996 if (void *symbol
= CYCastSymbol(name
.data
)) {
997 // XXX: this is horrendously inefficient
998 sig::Signature signature
;
999 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
1001 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1002 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
1005 // XXX: implement case 3
1007 return CYMakeType(context
, entry
->value_
);
1014 static void All_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1015 JSObjectRef
global(CYGetGlobalObject(context
));
1016 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1017 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1019 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1020 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1))) {
1021 JSPropertyNameArrayRef
subset(JSObjectCopyPropertyNames(context
, space
));
1022 for (size_t index(0), count(JSPropertyNameArrayGetCount(subset
)); index
!= count
; ++index
)
1023 JSPropertyNameAccumulatorAddName(names
, JSPropertyNameArrayGetNameAtIndex(subset
, index
));
1024 JSPropertyNameArrayRelease(subset
);
1028 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1030 throw CYJSError(context
, "incorrect number of arguments to Pointer constructor");
1034 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1035 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1037 sig::Signature signature
;
1038 sig::Parse(pool
, &signature
, type
, &Structor_
);
1040 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1043 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1045 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1047 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1048 return CYMakeType(context
, type
);
1051 static JSValueRef
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1053 throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf");
1054 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1057 size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0])));
1058 if (index
== _not(size_t))
1059 throw CYJSError(context
, "invalid array size used with Type.arrayOf");
1065 type
.primitive
= sig::array_P
;
1066 type
.data
.data
.type
= internal
->type_
;
1067 type
.data
.data
.size
= index
;
1069 return CYMakeType(context
, &type
);
1072 static JSValueRef
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1074 throw CYJSError(context
, "incorrect number of arguments to Type.constant");
1075 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1077 sig::Type
type(*internal
->type_
);
1078 type
.flags
|= JOC_TYPE_CONST
;
1079 return CYMakeType(context
, &type
);
1082 static JSValueRef
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1084 throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo");
1085 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1091 type
.primitive
= sig::pointer_P
;
1092 type
.data
.data
.type
= internal
->type_
;
1093 type
.data
.data
.size
= 0;
1095 return CYMakeType(context
, &type
);
1098 static JSValueRef
Type_callAsFunction_withName(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1100 throw CYJSError(context
, "incorrect number of arguments to Type.withName");
1101 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1104 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1106 sig::Type
type(*internal
->type_
);
1108 return CYMakeType(context
, &type
);
1111 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1113 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1114 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1116 sig::Type
*type(internal
->type_
);
1117 ffi_type
*ffi(internal
->GetFFI());
1119 uint8_t value
[ffi
->size
];
1121 CYPoolFFI(&pool
, context
, type
, ffi
, value
, arguments
[0]);
1122 return CYFromFFI(context
, type
, ffi
, value
);
1125 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1127 throw CYJSError(context
, "incorrect number of arguments to Type allocator");
1128 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1130 sig::Type
*type(internal
->type_
);
1133 if (type
->primitive
!= sig::array_P
)
1134 length
= _not(size_t);
1136 length
= type
->data
.data
.size
;
1137 type
= type
->data
.data
.type
;
1140 void *value(malloc(internal
->GetFFI()->size
));
1141 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1144 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1146 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1148 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1149 return CYMakeFunctor(context
, arguments
[0], type
);
1152 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1153 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1154 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1157 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1158 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1161 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1162 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1164 sprintf(string
, "%p", internal
->value_
);
1165 return CYCastJSValue(context
, string
);
1168 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1169 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1170 if (internal
->length_
!= _not(size_t)) {
1171 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
1172 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1173 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1176 sprintf(string
, "%p", internal
->value_
);
1177 return CYCastJSValue(context
, string
);
1181 static JSValueRef
Functor_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1182 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1184 return CYCastJSValue(context
, Unparse(pool
, &internal
->signature_
));
1187 static JSValueRef
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1188 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1189 return CYCastJSValue(context
, internal
->GetFFI()->alignment
);
1192 static JSValueRef
Type_getProperty_name(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1193 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1194 return CYCastJSValue(context
, internal
->type_
->name
);
1197 static JSValueRef
Type_getProperty_size(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1198 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1199 return CYCastJSValue(context
, internal
->GetFFI()->size
);
1202 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1203 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1205 const char *type(sig::Unparse(pool
, internal
->type_
));
1206 return CYCastJSValue(context
, CYJSString(type
));
1209 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1210 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1212 const char *type(sig::Unparse(pool
, internal
->type_
));
1213 std::ostringstream str
;
1214 CYStringify(str
, type
, strlen(type
));
1215 char *cyon(pool
.strcat("new Type(", str
.str().c_str(), ")", NULL
));
1216 return CYCastJSValue(context
, CYJSString(cyon
));
1219 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1220 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1223 static JSStaticFunction Pointer_staticFunctions
[4] = {
1224 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1225 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1226 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1230 static JSStaticFunction Struct_staticFunctions
[2] = {
1231 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1235 static JSStaticFunction Functor_staticFunctions
[4] = {
1236 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1237 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1238 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1243 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1246 static JSStaticValue Functor_staticValues
[2] = {
1247 {"type", &Functor_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1248 {NULL
, NULL
, NULL
, 0}
1251 static JSStaticValue Type_staticValues
[4] = {
1252 {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1253 {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1254 {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1255 {NULL
, NULL
, NULL
, 0}
1258 static JSStaticFunction Type_staticFunctions
[8] = {
1259 {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1260 {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1261 {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1262 {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1263 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1264 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1265 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1269 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1271 void CYSetArgs(int argc
, const char *argv
[]) {
1272 JSContextRef
context(CYGetJSContext());
1273 JSValueRef args
[argc
];
1274 for (int i(0); i
!= argc
; ++i
)
1275 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1278 if (JSObjectMakeArray$
!= NULL
)
1279 array
= _jsccall(*JSObjectMakeArray$
, context
, argc
, args
);
1281 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1282 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1283 array
= CYCastJSObject(context
, value
);
1286 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1287 CYSetProperty(context
, System
, CYJSString("args"), array
);
1290 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1291 return JSContextGetGlobalObject(context
);
1294 const char *CYExecute(CYPool
&pool
, CYUTF8String code
) {
1295 JSContextRef
context(CYGetJSContext());
1296 JSValueRef
exception(NULL
);
1299 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1300 handle
= (*hooks_
->ExecuteStart
)(context
);
1308 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1309 } catch (const char *error
) {
1313 if (exception
!= NULL
) error
:
1314 return CYPoolCString(pool
, context
, CYJSString(context
, exception
));
1316 if (JSValueIsUndefined(context
, result
))
1321 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1322 } catch (const char *error
) {
1326 if (exception
!= NULL
)
1329 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1334 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1335 (*hooks_
->ExecuteEnd
)(context
, handle
);
1340 extern "C" void CydgetSetupContext(JSGlobalContextRef context
) {
1341 CYSetupContext(context
);
1344 static bool initialized_
= false;
1346 void CYInitializeDynamic() {
1348 initialized_
= true;
1351 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1353 JSClassDefinition definition
;
1355 definition
= kJSClassDefinitionEmpty
;
1356 definition
.className
= "All";
1357 definition
.getProperty
= &All_getProperty
;
1358 definition
.getPropertyNames
= &All_getPropertyNames
;
1359 All_
= JSClassCreate(&definition
);
1361 definition
= kJSClassDefinitionEmpty
;
1362 definition
.className
= "Context";
1363 definition
.finalize
= &CYFinalize
;
1364 Context_
= JSClassCreate(&definition
);
1366 definition
= kJSClassDefinitionEmpty
;
1367 definition
.className
= "Functor";
1368 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1369 definition
.staticValues
= Functor_staticValues
;
1370 definition
.callAsFunction
= &Functor_callAsFunction
;
1371 definition
.finalize
= &CYFinalize
;
1372 Functor_
= JSClassCreate(&definition
);
1374 definition
= kJSClassDefinitionEmpty
;
1375 definition
.className
= "Pointer";
1376 definition
.staticFunctions
= Pointer_staticFunctions
;
1377 definition
.getProperty
= &Pointer_getProperty
;
1378 definition
.setProperty
= &Pointer_setProperty
;
1379 definition
.finalize
= &CYFinalize
;
1380 Pointer_
= JSClassCreate(&definition
);
1382 definition
= kJSClassDefinitionEmpty
;
1383 definition
.className
= "Struct";
1384 definition
.staticFunctions
= Struct_staticFunctions
;
1385 definition
.getProperty
= &Struct_getProperty
;
1386 definition
.setProperty
= &Struct_setProperty
;
1387 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1388 definition
.finalize
= &CYFinalize
;
1389 Struct_
= JSClassCreate(&definition
);
1391 definition
= kJSClassDefinitionEmpty
;
1392 definition
.className
= "Type";
1393 definition
.staticValues
= Type_staticValues
;
1394 definition
.staticFunctions
= Type_staticFunctions
;
1395 definition
.callAsFunction
= &Type_callAsFunction
;
1396 definition
.callAsConstructor
= &Type_callAsConstructor
;
1397 definition
.finalize
= &CYFinalize
;
1398 Type_privateData::Class_
= JSClassCreate(&definition
);
1400 definition
= kJSClassDefinitionEmpty
;
1401 definition
.className
= "Global";
1402 //definition.getProperty = &Global_getProperty;
1403 Global_
= JSClassCreate(&definition
);
1405 Array_s
= JSStringCreateWithUTF8CString("Array");
1406 cy_s
= JSStringCreateWithUTF8CString("$cy");
1407 length_s
= JSStringCreateWithUTF8CString("length");
1408 message_s
= JSStringCreateWithUTF8CString("message");
1409 name_s
= JSStringCreateWithUTF8CString("name");
1410 pop_s
= JSStringCreateWithUTF8CString("pop");
1411 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1412 push_s
= JSStringCreateWithUTF8CString("push");
1413 splice_s
= JSStringCreateWithUTF8CString("splice");
1414 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1415 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1416 toPointer_s
= JSStringCreateWithUTF8CString("toPointer");
1417 toString_s
= JSStringCreateWithUTF8CString("toString");
1419 Result_
= JSStringCreateWithUTF8CString("_");
1421 if (hooks_
!= NULL
&& hooks_
->Initialize
!= NULL
)
1422 (*hooks_
->Initialize
)();
1425 void CYThrow(JSContextRef context
, JSValueRef value
) {
1427 throw CYJSError(context
, value
);
1430 const char *CYJSError::PoolCString(CYPool
&pool
) const {
1431 // XXX: this used to be CYPoolCString
1432 return CYPoolCCYON(pool
, context_
, value_
);
1435 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1436 // XXX: what if the context is different?
1440 JSValueRef
CYCastJSError(JSContextRef context
, const char *message
) {
1441 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString("Error")));
1442 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1443 return _jsccall(JSObjectCallAsConstructor
, context
, Error
, 1, arguments
);
1446 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1447 return CYCastJSError(context
, message_
);
1450 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1451 _assert(context
!= NULL
);
1456 va_start(args
, format
);
1457 // XXX: there might be a beter way to think about this
1458 const char *message(pool
.vsprintf(64, format
, args
));
1461 value_
= CYCastJSError(context
, message
);
1464 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1465 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1468 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
1469 CYInitializeDynamic();
1471 JSObjectRef
global(CYGetGlobalObject(context
));
1473 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
1474 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
1476 /* Cache Globals {{{ */
1477 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
1478 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
1480 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
1481 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
1483 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
1484 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
1486 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
1487 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
1489 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
1490 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
1492 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1493 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
1495 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
1496 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
1498 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
1499 CYSetProperty(context
, cy
, CYJSString("String"), String
);
1501 JSObjectRef
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
)));
1502 CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
);
1505 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1506 CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1508 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1509 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1510 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
1512 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1513 JSObjectSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
1514 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
1516 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1517 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1519 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
1520 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
1522 JSObjectRef
alls(_jsccall(JSObjectCallAsConstructor
, context
, Array
, 0, NULL
));
1523 CYSetProperty(context
, cycript
, CYJSString("alls"), alls
);
1526 JSObjectRef
last(NULL
), curr(global
);
1528 goto next
; for (JSValueRef next
;;) {
1529 if (JSValueIsNull(context
, next
))
1532 curr
= CYCastJSObject(context
, next
);
1534 next
= JSObjectGetPrototype(context
, curr
);
1537 JSObjectSetPrototype(context
, last
, all
);
1540 CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
, kJSPropertyAttributeDontEnum
);
1542 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
1543 CYSetProperty(context
, cy
, CYJSString("System"), System
);
1545 CYSetProperty(context
, global
, CYJSString("system"), System
);
1546 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
1547 //CYSetProperty(context, System, CYJSString("global"), global);
1548 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
1550 if (CYBridgeEntry
*entry
= CYBridgeHash("1dlerror", 8))
1551 entry
->cache_
= new cy::Functor(entry
->value_
, reinterpret_cast<void (*)()>(&dlerror
));
1553 if (hooks_
!= NULL
&& hooks_
->SetupContext
!= NULL
)
1554 (*hooks_
->SetupContext
)(context
);
1556 CYArrayPush(context
, alls
, cycript
);
1559 JSGlobalContextRef
CYGetJSContext() {
1560 CYInitializeDynamic();
1562 static JSGlobalContextRef context_
;
1564 if (context_
== NULL
) {
1565 context_
= JSGlobalContextCreate(Global_
);
1566 CYSetupContext(context_
);