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 JSValueRef
exception(NULL
);
54 JSValueRef
value(JSObjectGetPropertyAtIndex(context
, object
, index
, &exception
));
55 CYThrow(context
, exception
);
59 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
60 JSValueRef
exception(NULL
);
61 JSValueRef
value(JSObjectGetProperty(context
, object
, name
, &exception
));
62 CYThrow(context
, exception
);
66 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) {
67 JSValueRef
exception(NULL
);
68 JSObjectSetPropertyAtIndex(context
, object
, index
, value
, &exception
);
69 CYThrow(context
, exception
);
72 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) {
73 JSValueRef
exception(NULL
);
74 JSObjectSetProperty(context
, object
, name
, value
, attributes
, &exception
);
75 CYThrow(context
, exception
);
78 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef (*callback
)(JSContextRef
, JSObjectRef
, JSObjectRef
, size_t, const JSValueRef
[], JSValueRef
*), JSPropertyAttributes attributes
) {
79 CYSetProperty(context
, object
, name
, JSObjectMakeFunctionWithCallback(context
, name
, callback
), attributes
);
82 /* JavaScript Strings {{{ */
83 JSStringRef
CYCopyJSString(const char *value
) {
84 return value
== NULL
? NULL
: JSStringCreateWithUTF8CString(value
);
87 JSStringRef
CYCopyJSString(JSStringRef value
) {
88 return value
== NULL
? NULL
: JSStringRetain(value
);
91 JSStringRef
CYCopyJSString(CYUTF8String value
) {
92 // XXX: this is very wrong; it needs to convert to UTF16 and then create from there
93 return CYCopyJSString(value
.data
);
96 JSStringRef
CYCopyJSString(JSContextRef context
, JSValueRef value
) {
97 if (JSValueIsNull(context
, value
))
99 JSValueRef
exception(NULL
);
100 JSStringRef
string(JSValueToStringCopy(context
, value
, &exception
));
101 CYThrow(context
, exception
);
105 static CYUTF16String
CYCastUTF16String(JSStringRef value
) {
106 return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
));
109 CYUTF8String
CYPoolUTF8String(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
110 return CYPoolUTF8String(pool
, CYCastUTF16String(value
));
113 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
114 CYUTF8String
utf8(CYPoolUTF8String(pool
, context
, value
));
115 _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
);
119 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
120 return JSValueIsNull(context
, value
) ? NULL
: CYPoolCString(pool
, context
, CYJSString(context
, value
));
123 /* Index Offsets {{{ */
124 size_t CYGetIndex(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
125 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
129 static JSClassRef All_
;
130 static JSClassRef Context_
;
132 static JSClassRef Global_
;
133 static JSClassRef Pointer_
;
134 static JSClassRef Struct_
;
138 JSStringRef length_s
;
139 JSStringRef message_s
;
142 JSStringRef prototype_s
;
144 JSStringRef splice_s
;
145 JSStringRef toCYON_s
;
146 JSStringRef toJSON_s
;
147 JSStringRef toPointer_s
;
148 JSStringRef toString_s
;
150 static JSStringRef Result_
;
152 void CYFinalize(JSObjectRef object
) {
153 CYData
*internal(reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
)));
154 _assert(internal
->count_
!= _not(unsigned));
155 if (--internal
->count_
== 0)
159 void Structor_(CYPool
&pool
, sig::Type
*&type
) {
161 type
->primitive
== sig::pointer_P
&&
162 type
->data
.data
.type
!= NULL
&&
163 type
->data
.data
.type
->primitive
== sig::struct_P
&&
164 type
->data
.data
.type
->name
!= NULL
&&
165 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
167 type
->primitive
= sig::typename_P
;
168 type
->data
.data
.type
= NULL
;
172 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
175 size_t length(strlen(type
->name
));
176 char keyed
[length
+ 2];
177 memcpy(keyed
+ 1, type
->name
, length
+ 1);
179 static const char *modes
= "34";
180 for (size_t i(0); i
!= 2; ++i
) {
184 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
187 sig::Parse(pool
, &type
->data
.signature
, entry
->value_
, &Structor_
);
191 sig::Signature signature
;
192 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
193 type
= signature
.elements
[0].type
;
199 JSClassRef
Type_privateData::Class_
;
204 JSGlobalContextRef context_
;
206 Context(JSGlobalContextRef context
) :
215 Type_privateData
*type_
;
218 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type
*type
) :
219 CYOwned(value
, context
, owner
),
220 type_(new(*pool_
) Type_privateData(type
)),
226 struct Struct_privateData
:
229 Type_privateData
*type_
;
231 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
232 CYOwned(NULL
, context
, owner
)
237 typedef std::map
<const char *, Type_privateData
*, CYCStringLess
> TypeMap
;
238 static TypeMap Types_
;
240 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
241 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
242 CYPool
&pool(*internal
->pool_
);
243 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
244 internal
->type_
= typical
;
247 internal
->value_
= data
;
249 size_t size(typical
->GetFFI()->size
);
250 void *copy(internal
->pool_
->malloc
<void>(size
));
251 memcpy(copy
, data
, size
);
252 internal
->value_
= copy
;
255 return JSObjectMake(context
, Struct_
, internal
);
258 static void *CYCastSymbol(const char *name
) {
259 return dlsym(RTLD_DEFAULT
, name
);
262 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
263 return JSValueMakeBoolean(context
, value
);
266 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
267 return JSValueMakeNumber(context
, value
);
270 #define CYCastJSValue_(Type_) \
271 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
272 return JSValueMakeNumber(context, static_cast<double>(value)); \
276 CYCastJSValue_(unsigned int)
277 CYCastJSValue_(long int)
278 CYCastJSValue_(long unsigned int)
279 CYCastJSValue_(long long int)
280 CYCastJSValue_(long long unsigned int)
282 JSValueRef
CYJSUndefined(JSContextRef context
) {
283 return JSValueMakeUndefined(context
);
286 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
287 JSValueRef
exception(NULL
);
288 double number(JSValueToNumber(context
, value
, &exception
));
289 CYThrow(context
, exception
);
293 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
294 return JSValueToBoolean(context
, value
);
297 JSValueRef
CYJSNull(JSContextRef context
) {
298 return JSValueMakeNull(context
);
301 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
302 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
305 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
306 return CYCastJSValue(context
, CYJSString(value
));
309 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
310 JSValueRef
exception(NULL
);
311 JSObjectRef
object(JSValueToObject(context
, value
, &exception
));
312 CYThrow(context
, exception
);
316 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
317 JSValueRef
exception(NULL
);
318 JSValueRef
value(JSObjectCallAsFunction(context
, function
, _this
, count
, arguments
, &exception
));
319 CYThrow(context
, exception
);
323 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
324 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
327 size_t CYArrayLength(JSContextRef context
, JSObjectRef array
) {
328 return CYCastDouble(context
, CYGetProperty(context
, array
, length_s
));
331 JSValueRef
CYArrayGet(JSContextRef context
, JSObjectRef array
, size_t index
) {
332 JSValueRef
exception(NULL
);
333 JSValueRef
value(JSObjectGetPropertyAtIndex(context
, array
, index
, &exception
));
334 CYThrow(context
, exception
);
338 void CYArrayPush(JSContextRef context
, JSObjectRef array
, JSValueRef value
) {
339 JSValueRef
exception(NULL
);
340 JSValueRef arguments
[1];
341 arguments
[0] = value
;
342 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
343 JSObjectCallAsFunction(context
, CYCastJSObject(context
, CYGetProperty(context
, Array
, push_s
)), array
, 1, arguments
, &exception
);
344 CYThrow(context
, exception
);
347 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
352 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
355 return CYJSUndefined(context
);
358 static size_t Nonce_(0);
360 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
362 const char *name(pool
.strcat(CYPoolCString(pool
, context
, arguments
[0]), pool
.itoa(Nonce_
++), NULL
));
363 return CYCastJSValue(context
, name
);
366 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
367 JSGarbageCollect(context
);
368 return CYJSUndefined(context
);
371 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
372 switch (JSType type
= JSValueGetType(context
, value
)) {
373 case kJSTypeUndefined
:
378 return CYCastBool(context
, value
) ? "true" : "false";
380 case kJSTypeNumber
: {
381 std::ostringstream str
;
382 CYNumerify(str
, CYCastDouble(context
, value
));
383 std::string
value(str
.str());
384 return pool
.strmemdup(value
.c_str(), value
.size());
387 case kJSTypeString
: {
388 std::ostringstream str
;
389 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
390 CYStringify(str
, string
.data
, string
.size
);
391 std::string
value(str
.str());
392 return pool
.strmemdup(value
.c_str(), value
.size());
396 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
398 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
402 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
403 JSValueRef
exception(NULL
);
404 const char *cyon(CYPoolCCYON(pool
, context
, value
, &exception
));
405 CYThrow(context
, exception
);
409 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSObjectRef object
) {
410 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
411 if (CYIsCallable(context
, toCYON
)) {
412 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
413 _assert(value
!= NULL
);
414 return CYPoolCString(pool
, context
, value
);
417 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
418 if (CYIsCallable(context
, toJSON
)) {
419 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
420 JSValueRef
exception(NULL
);
421 const char *cyon(CYPoolCCYON(pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), &exception
));
422 CYThrow(context
, exception
);
426 if (JSObjectIsFunction(context
, object
)) {
427 JSValueRef
toString(CYGetProperty(context
, object
, toString_s
));
428 if (CYIsCallable(context
, toString
)) {
429 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
430 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toString
, object
, 1, arguments
));
431 _assert(value
!= NULL
);
432 return CYPoolCString(pool
, context
, value
);
436 std::ostringstream str
;
440 // XXX: this is, sadly, going to leak
441 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
445 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
451 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
452 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
457 CYStringify(str
, string
.data
, string
.size
);
462 JSValueRef
value(CYGetProperty(context
, object
, name
));
463 str
<< CYPoolCCYON(pool
, context
, value
);
464 } catch (const CYException
&error
) {
469 JSPropertyNameArrayRelease(names
);
473 std::string
string(str
.str());
474 return pool
.strmemdup(string
.c_str(), string
.size());
477 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
479 std::ostringstream str
;
483 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
486 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
487 JSValueRef
value(CYGetProperty(context
, _this
, index
));
494 if (!JSValueIsUndefined(context
, value
))
495 str
<< CYPoolCCYON(pool
, context
, value
);
504 std::string
value(str
.str());
505 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
508 static JSValueRef
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
510 std::ostringstream str
;
512 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
)));
513 CYStringify(str
, string
.data
, string
.size
);
515 std::string
value(str
.str());
516 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
519 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
520 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
521 return JSObjectMake(context
, Pointer_
, internal
);
524 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const char *type
) {
525 return JSObjectMake(context
, Functor_
, new cy::Functor(type
, function
));
528 static JSObjectRef
CYMakeFunctor(JSContextRef context
, const char *symbol
, const char *type
, void **cache
) {
529 cy::Functor
*internal
;
531 internal
= reinterpret_cast<cy::Functor
*>(*cache
);
533 void (*function
)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol
)));
534 if (function
== NULL
)
537 internal
= new cy::Functor(type
, function
);
542 return JSObjectMake(context
, Functor_
, internal
);
545 static bool CYGetOffset(CYPool
&pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
546 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
549 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
550 switch (JSValueGetType(context
, value
)) {
553 case kJSTypeObject
: {
554 JSObjectRef
object((JSObjectRef
) value
);
555 if (JSValueIsObjectOfClass(context
, value
, Pointer_
)) {
556 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
557 return internal
->value_
;
559 JSValueRef
toPointer(CYGetProperty(context
, object
, toPointer_s
));
560 if (CYIsCallable(context
, toPointer
)) {
561 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
));
562 _assert(value
!= NULL
);
563 return CYCastPointer_(context
, value
);
566 double number(CYCastDouble(context
, value
));
567 if (std::isnan(number
))
568 throw CYJSError(context
, "cannot convert value to pointer");
569 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
573 void CYPoolFFI(CYPool
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
574 switch (type
->primitive
) {
576 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
579 #define CYPoolFFI_(primitive, native) \
580 case sig::primitive ## _P: \
581 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
584 CYPoolFFI_(uchar
, unsigned char)
585 CYPoolFFI_(char, char)
586 CYPoolFFI_(ushort
, unsigned short)
587 CYPoolFFI_(short, short)
588 CYPoolFFI_(ulong
, unsigned long)
589 CYPoolFFI_(long, long)
590 CYPoolFFI_(uint
, unsigned int)
592 CYPoolFFI_(ulonglong
, unsigned long long)
593 CYPoolFFI_(longlong
, long long)
594 CYPoolFFI_(float, float)
595 CYPoolFFI_(double, double)
598 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
599 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
600 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
601 ffi_type
*field(ffi
->elements
[index
]);
604 if (aggregate
== NULL
)
607 rhs
= CYGetProperty(context
, aggregate
, index
);
608 if (JSValueIsUndefined(context
, rhs
))
609 throw CYJSError(context
, "unable to extract array value");
612 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
619 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
623 _assert(pool
!= NULL
);
624 *reinterpret_cast<const char **>(data
) = CYPoolCString(*pool
, context
, value
);
627 case sig::struct_P
: {
628 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
629 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
630 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
631 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
632 ffi_type
*field(ffi
->elements
[index
]);
635 if (aggregate
== NULL
)
638 rhs
= CYGetProperty(context
, aggregate
, index
);
639 if (JSValueIsUndefined(context
, rhs
)) {
640 if (element
->name
!= NULL
)
641 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
644 if (JSValueIsUndefined(context
, rhs
)) undefined
:
645 throw CYJSError(context
, "unable to extract structure value");
649 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
659 if (hooks_
!= NULL
&& hooks_
->PoolFFI
!= NULL
)
660 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
663 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
667 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
668 switch (type
->primitive
) {
670 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
672 #define CYFromFFI_(primitive, native) \
673 case sig::primitive ## _P: \
674 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
676 CYFromFFI_(uchar, unsigned char)
677 CYFromFFI_(char, char)
678 CYFromFFI_(ushort
, unsigned short)
679 CYFromFFI_(short, short)
680 CYFromFFI_(ulong
, unsigned long)
681 CYFromFFI_(long, long)
682 CYFromFFI_(uint
, unsigned int)
684 CYFromFFI_(ulonglong
, unsigned long long)
685 CYFromFFI_(longlong
, long long)
686 CYFromFFI_(float, float)
687 CYFromFFI_(double, double)
690 if (void *pointer
= data
)
691 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
695 if (void *pointer
= *reinterpret_cast<void **>(data
))
696 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
700 if (char *utf8
= *reinterpret_cast<char **>(data
))
701 return CYCastJSValue(context
, utf8
);
705 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
707 return CYJSUndefined(context
);
710 return CYJSNull(context
);
712 if (hooks_
!= NULL
&& hooks_
->FromFFI
!= NULL
)
713 if (JSValueRef value
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
716 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
720 void CYExecuteClosure(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
, JSValueRef (*adapter
)(JSContextRef
, size_t, JSValueRef
[], JSObjectRef
)) {
721 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
723 JSContextRef
context(internal
->context_
);
725 size_t count(internal
->cif_
.nargs
);
726 JSValueRef values
[count
];
728 for (size_t index(0); index
!= count
; ++index
)
729 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
731 JSValueRef
value(adapter(context
, count
, values
, internal
->function_
));
732 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
735 static JSValueRef
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
) {
736 return CYCallAsFunction(context
, function
, NULL
, count
, values
);
739 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
740 CYExecuteClosure(cif
, result
, arguments
, arg
, &FunctionAdapter_
);
743 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const char *type
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
744 // XXX: in case of exceptions this will leak
745 // XXX: in point of fact, this may /need/ to leak :(
746 Closure_privateData
*internal(new Closure_privateData(context
, function
, type
));
748 #if defined(__APPLE__) && defined(__arm__)
750 ffi_closure
*writable(reinterpret_cast<ffi_closure
*>(ffi_closure_alloc(sizeof(ffi_closure
), &executable
)));
752 ffi_status
status(ffi_prep_closure_loc(writable
, &internal
->cif_
, callback
, internal
, executable
));
753 _assert(status
== FFI_OK
);
755 internal
->value_
= executable
;
757 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
758 NULL
, sizeof(ffi_closure
),
759 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
763 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
764 _assert(status
== FFI_OK
);
766 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
768 internal
->value_
= closure
;
774 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const char *type
) {
775 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, type
, &FunctionClosure_
));
776 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
777 // XXX: see above notes about needing to leak
778 JSValueProtect(CYGetJSContext(context
), object
);
782 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
783 return CYCastJSObject(context
, CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
));
786 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const char *type
) {
787 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
789 JSValueRef
exception(NULL
);
790 bool function(JSValueIsInstanceOfConstructor(context
, value
, Function
, &exception
));
791 CYThrow(context
, exception
);
794 JSObjectRef
function(CYCastJSObject(context
, value
));
795 return CYMakeFunctor(context
, function
, type
);
797 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
798 return CYMakeFunctor(context
, function
, type
);
802 static bool Index_(CYPool
&pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
803 Type_privateData
*typical(internal
->type_
);
804 sig::Type
*type(typical
->type_
);
808 const char *name(CYPoolCString(pool
, context
, property
));
809 size_t length(strlen(name
));
810 double number(CYCastDouble(name
, length
));
812 size_t count(type
->data
.signature
.count
);
814 if (std::isnan(number
)) {
815 if (property
== NULL
)
818 sig::Element
*elements(type
->data
.signature
.elements
);
820 for (size_t local(0); local
!= count
; ++local
) {
821 sig::Element
*element(&elements
[local
]);
822 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
830 index
= static_cast<ssize_t
>(number
);
831 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
836 ffi_type
**elements(typical
->GetFFI()->elements
);
838 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
839 for (ssize_t
local(0); local
!= index
; ++local
)
840 base
+= elements
[local
]->size
;
845 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
847 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
849 if (JSStringIsEqual(property
, length_s
))
850 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
852 Type_privateData
*typical(internal
->type_
);
854 if (typical
->type_
== NULL
)
858 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
860 else if (!CYGetOffset(pool
, context
, property
, offset
))
863 ffi_type
*ffi(typical
->GetFFI());
865 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
866 base
+= ffi
->size
* offset
;
868 JSObjectRef
owner(internal
->GetOwner() ?: object
);
869 return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
);
872 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
874 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
875 Type_privateData
*typical(internal
->type_
);
877 if (typical
->type_
== NULL
)
881 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
883 else if (!CYGetOffset(pool
, context
, property
, offset
))
886 ffi_type
*ffi(typical
->GetFFI());
888 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
889 base
+= ffi
->size
* offset
;
891 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
895 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
896 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
897 Type_privateData
*typical(internal
->type_
);
898 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
901 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
903 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
904 Type_privateData
*typical(internal
->type_
);
909 if (!Index_(pool
, context
, internal
, property
, index
, base
))
912 JSObjectRef
owner(internal
->GetOwner() ?: object
);
914 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
917 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
919 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
920 Type_privateData
*typical(internal
->type_
);
925 if (!Index_(pool
, context
, internal
, property
, index
, base
))
928 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
932 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
933 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
934 Type_privateData
*typical(internal
->type_
);
935 sig::Type
*type(typical
->type_
);
940 size_t count(type
->data
.signature
.count
);
941 sig::Element
*elements(type
->data
.signature
.elements
);
945 for (size_t index(0); index
!= count
; ++index
) {
947 name
= elements
[index
].name
;
950 sprintf(number
, "%zu", index
);
954 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
958 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
)()) {
959 if (setups
+ count
!= signature
->count
- 1)
960 throw CYJSError(context
, "incorrect number of arguments to ffi function");
962 size_t size(setups
+ count
);
964 memcpy(values
, setup
, sizeof(void *) * setups
);
966 for (size_t index(setups
); index
!= size
; ++index
) {
967 sig::Element
*element(&signature
->elements
[index
+ 1]);
968 ffi_type
*ffi(cif
->arg_types
[index
]);
970 values
[index
] = new(pool
) uint8_t[ffi
->size
];
971 CYPoolFFI(&pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
974 uint8_t value
[cif
->rtype
->size
];
976 if (hooks_
!= NULL
&& hooks_
->CallFunction
!= NULL
)
977 (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
);
979 ffi_call(cif
, function
, value
, values
);
981 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
984 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
986 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
987 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
990 JSObjectRef
CYMakeType(JSContextRef context
, const char *type
) {
991 Type_privateData
*internal(new Type_privateData(type
));
992 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
995 JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
996 Type_privateData
*internal(new Type_privateData(type
));
997 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1000 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1001 JSObjectRef
global(CYGetGlobalObject(context
));
1002 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1003 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1005 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1006 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1007 if (JSValueRef value
= CYGetProperty(context
, space
, property
))
1008 if (!JSValueIsUndefined(context
, value
))
1012 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
1014 size_t length(name
.size
);
1015 char keyed
[length
+ 2];
1016 memcpy(keyed
+ 1, name
.data
, length
+ 1);
1018 static const char *modes
= "0124";
1019 for (size_t i(0); i
!= 4; ++i
) {
1020 char mode(modes
[i
]);
1023 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
1026 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
);
1029 return CYMakeFunctor(context
, name
.data
, entry
->value_
, &entry
->cache_
);
1032 if (void *symbol
= CYCastSymbol(name
.data
)) {
1033 // XXX: this is horrendously inefficient
1034 sig::Signature signature
;
1035 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
1037 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1038 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
1041 // XXX: implement case 3
1043 return CYMakeType(context
, entry
->value_
);
1050 static void All_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1051 JSObjectRef
global(CYGetGlobalObject(context
));
1052 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1053 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1055 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1056 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1))) {
1057 JSPropertyNameArrayRef
subset(JSObjectCopyPropertyNames(context
, space
));
1058 for (size_t index(0), count(JSPropertyNameArrayGetCount(subset
)); index
!= count
; ++index
)
1059 JSPropertyNameAccumulatorAddName(names
, JSPropertyNameArrayGetNameAtIndex(subset
, index
));
1060 JSPropertyNameArrayRelease(subset
);
1064 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1066 throw CYJSError(context
, "incorrect number of arguments to Pointer constructor");
1070 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1071 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1073 sig::Signature signature
;
1074 sig::Parse(pool
, &signature
, type
, &Structor_
);
1076 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1079 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1081 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1083 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1084 return CYMakeType(context
, type
);
1087 static JSValueRef
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1089 throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf");
1090 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1093 size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0])));
1094 if (index
== _not(size_t))
1095 throw CYJSError(context
, "invalid array size used with Type.arrayOf");
1101 type
.primitive
= sig::array_P
;
1102 type
.data
.data
.type
= internal
->type_
;
1103 type
.data
.data
.size
= index
;
1105 return CYMakeType(context
, &type
);
1108 static JSValueRef
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1110 throw CYJSError(context
, "incorrect number of arguments to Type.constant");
1111 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1113 sig::Type
type(*internal
->type_
);
1114 type
.flags
|= JOC_TYPE_CONST
;
1115 return CYMakeType(context
, &type
);
1118 static JSValueRef
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1120 throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo");
1121 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1127 type
.primitive
= sig::pointer_P
;
1128 type
.data
.data
.type
= internal
->type_
;
1129 type
.data
.data
.size
= 0;
1131 return CYMakeType(context
, &type
);
1134 static JSValueRef
Type_callAsFunction_withName(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1136 throw CYJSError(context
, "incorrect number of arguments to Type.withName");
1137 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1140 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1142 sig::Type
type(*internal
->type_
);
1144 return CYMakeType(context
, &type
);
1147 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1149 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1150 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1152 sig::Type
*type(internal
->type_
);
1153 ffi_type
*ffi(internal
->GetFFI());
1155 uint8_t value
[ffi
->size
];
1157 CYPoolFFI(&pool
, context
, type
, ffi
, value
, arguments
[0]);
1158 return CYFromFFI(context
, type
, ffi
, value
);
1161 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1163 throw CYJSError(context
, "incorrect number of arguments to Type allocator");
1164 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1166 sig::Type
*type(internal
->type_
);
1169 if (type
->primitive
!= sig::array_P
)
1170 length
= _not(size_t);
1172 length
= type
->data
.data
.size
;
1173 type
= type
->data
.data
.type
;
1176 void *value(malloc(internal
->GetFFI()->size
));
1177 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1180 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1182 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1184 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1185 return CYMakeFunctor(context
, arguments
[0], type
);
1188 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1189 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1190 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1193 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1194 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1197 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1198 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1200 sprintf(string
, "%p", internal
->value_
);
1201 return CYCastJSValue(context
, string
);
1204 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1205 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1206 if (internal
->length_
!= _not(size_t)) {
1207 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
1208 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1209 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1212 sprintf(string
, "%p", internal
->value_
);
1213 return CYCastJSValue(context
, string
);
1217 static JSValueRef
Functor_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1218 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1220 return CYCastJSValue(context
, Unparse(pool
, &internal
->signature_
));
1223 static JSValueRef
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1224 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1225 return CYCastJSValue(context
, internal
->GetFFI()->alignment
);
1228 static JSValueRef
Type_getProperty_name(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1229 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1230 return CYCastJSValue(context
, internal
->type_
->name
);
1233 static JSValueRef
Type_getProperty_size(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1234 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1235 return CYCastJSValue(context
, internal
->GetFFI()->size
);
1238 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1239 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1241 const char *type(sig::Unparse(pool
, internal
->type_
));
1242 return CYCastJSValue(context
, CYJSString(type
));
1245 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1246 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1248 const char *type(sig::Unparse(pool
, internal
->type_
));
1249 std::ostringstream str
;
1250 CYStringify(str
, type
, strlen(type
));
1251 char *cyon(pool
.strcat("new Type(", str
.str().c_str(), ")", NULL
));
1252 return CYCastJSValue(context
, CYJSString(cyon
));
1255 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1256 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1259 static JSStaticFunction Pointer_staticFunctions
[4] = {
1260 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1261 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1262 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1266 static JSStaticFunction Struct_staticFunctions
[2] = {
1267 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1271 static JSStaticFunction Functor_staticFunctions
[4] = {
1272 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1273 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1274 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1279 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1282 static JSStaticValue Functor_staticValues
[2] = {
1283 {"type", &Functor_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1284 {NULL
, NULL
, NULL
, 0}
1287 static JSStaticValue Type_staticValues
[4] = {
1288 {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1289 {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1290 {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1291 {NULL
, NULL
, NULL
, 0}
1294 static JSStaticFunction Type_staticFunctions
[8] = {
1295 {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1296 {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1297 {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1298 {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1299 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1300 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1301 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1305 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1307 void CYSetArgs(int argc
, const char *argv
[]) {
1308 JSContextRef
context(CYGetJSContext());
1309 JSValueRef args
[argc
];
1310 for (int i(0); i
!= argc
; ++i
)
1311 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1314 if (JSObjectMakeArray$
!= NULL
) {
1315 JSValueRef
exception(NULL
);
1316 array
= (*JSObjectMakeArray$
)(context
, argc
, args
, &exception
);
1317 CYThrow(context
, exception
);
1319 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1320 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1321 array
= CYCastJSObject(context
, value
);
1324 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1325 CYSetProperty(context
, System
, CYJSString("args"), array
);
1328 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1329 return JSContextGetGlobalObject(context
);
1332 const char *CYExecute(CYPool
&pool
, CYUTF8String code
) {
1333 JSContextRef
context(CYGetJSContext());
1334 JSValueRef
exception(NULL
), result
;
1337 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1338 handle
= (*hooks_
->ExecuteStart
)(context
);
1345 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1346 } catch (const char *error
) {
1350 if (exception
!= NULL
) error
:
1351 return CYPoolCString(pool
, context
, CYJSString(context
, exception
));
1353 if (JSValueIsUndefined(context
, result
))
1357 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1358 } catch (const char *error
) {
1362 if (exception
!= NULL
)
1365 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1367 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1368 (*hooks_
->ExecuteEnd
)(context
, handle
);
1372 extern "C" void CydgetSetupContext(JSGlobalContextRef context
) {
1373 CYSetupContext(context
);
1376 static bool initialized_
= false;
1378 void CYInitializeDynamic() {
1380 initialized_
= true;
1383 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1385 JSClassDefinition definition
;
1387 definition
= kJSClassDefinitionEmpty
;
1388 definition
.className
= "All";
1389 definition
.getProperty
= &All_getProperty
;
1390 definition
.getPropertyNames
= &All_getPropertyNames
;
1391 All_
= JSClassCreate(&definition
);
1393 definition
= kJSClassDefinitionEmpty
;
1394 definition
.className
= "Context";
1395 definition
.finalize
= &CYFinalize
;
1396 Context_
= JSClassCreate(&definition
);
1398 definition
= kJSClassDefinitionEmpty
;
1399 definition
.className
= "Functor";
1400 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1401 definition
.staticValues
= Functor_staticValues
;
1402 definition
.callAsFunction
= &Functor_callAsFunction
;
1403 definition
.finalize
= &CYFinalize
;
1404 Functor_
= JSClassCreate(&definition
);
1406 definition
= kJSClassDefinitionEmpty
;
1407 definition
.className
= "Pointer";
1408 definition
.staticFunctions
= Pointer_staticFunctions
;
1409 definition
.getProperty
= &Pointer_getProperty
;
1410 definition
.setProperty
= &Pointer_setProperty
;
1411 definition
.finalize
= &CYFinalize
;
1412 Pointer_
= JSClassCreate(&definition
);
1414 definition
= kJSClassDefinitionEmpty
;
1415 definition
.className
= "Struct";
1416 definition
.staticFunctions
= Struct_staticFunctions
;
1417 definition
.getProperty
= &Struct_getProperty
;
1418 definition
.setProperty
= &Struct_setProperty
;
1419 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1420 definition
.finalize
= &CYFinalize
;
1421 Struct_
= JSClassCreate(&definition
);
1423 definition
= kJSClassDefinitionEmpty
;
1424 definition
.className
= "Type";
1425 definition
.staticValues
= Type_staticValues
;
1426 definition
.staticFunctions
= Type_staticFunctions
;
1427 definition
.callAsFunction
= &Type_callAsFunction
;
1428 definition
.callAsConstructor
= &Type_callAsConstructor
;
1429 definition
.finalize
= &CYFinalize
;
1430 Type_privateData::Class_
= JSClassCreate(&definition
);
1432 definition
= kJSClassDefinitionEmpty
;
1433 definition
.className
= "Global";
1434 //definition.getProperty = &Global_getProperty;
1435 Global_
= JSClassCreate(&definition
);
1437 Array_s
= JSStringCreateWithUTF8CString("Array");
1438 cy_s
= JSStringCreateWithUTF8CString("$cy");
1439 length_s
= JSStringCreateWithUTF8CString("length");
1440 message_s
= JSStringCreateWithUTF8CString("message");
1441 name_s
= JSStringCreateWithUTF8CString("name");
1442 pop_s
= JSStringCreateWithUTF8CString("pop");
1443 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1444 push_s
= JSStringCreateWithUTF8CString("push");
1445 splice_s
= JSStringCreateWithUTF8CString("splice");
1446 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1447 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1448 toPointer_s
= JSStringCreateWithUTF8CString("toPointer");
1449 toString_s
= JSStringCreateWithUTF8CString("toString");
1451 Result_
= JSStringCreateWithUTF8CString("_");
1453 if (hooks_
!= NULL
&& hooks_
->Initialize
!= NULL
)
1454 (*hooks_
->Initialize
)();
1457 void CYThrow(JSContextRef context
, JSValueRef value
) {
1459 throw CYJSError(context
, value
);
1462 const char *CYJSError::PoolCString(CYPool
&pool
) const {
1463 // XXX: this used to be CYPoolCString
1464 return CYPoolCCYON(pool
, context_
, value_
);
1467 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1468 // XXX: what if the context is different?
1472 JSValueRef
CYCastJSError(JSContextRef context
, const char *message
) {
1473 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString("Error")));
1475 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1477 JSValueRef
exception(NULL
);
1478 JSValueRef
value(JSObjectCallAsConstructor(context
, Error
, 1, arguments
, &exception
));
1479 CYThrow(context
, exception
);
1484 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1485 return CYCastJSError(context
, message_
);
1488 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1489 _assert(context
!= NULL
);
1494 va_start(args
, format
);
1495 // XXX: there might be a beter way to think about this
1496 const char *message(pool
.vsprintf(64, format
, args
));
1499 value_
= CYCastJSError(context
, message
);
1502 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1503 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1506 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
1507 JSValueRef
exception(NULL
);
1509 CYInitializeDynamic();
1511 JSObjectRef
global(CYGetGlobalObject(context
));
1513 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
1514 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
1516 /* Cache Globals {{{ */
1517 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
1518 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
1520 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
1521 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
1523 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
1524 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
1526 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
1527 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
1529 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
1530 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
1532 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1533 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
1535 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
1536 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
1538 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
1539 CYSetProperty(context
, cy
, CYJSString("String"), String
);
1541 JSObjectRef
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
)));
1542 CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
);
1545 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1546 CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1548 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1549 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1550 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
1552 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1553 JSObjectSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
1554 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
1556 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1557 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1559 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
1560 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
1562 JSObjectRef
alls(JSObjectCallAsConstructor(context
, Array
, 0, NULL
, &exception
));
1563 CYThrow(context
, exception
);
1564 CYSetProperty(context
, cycript
, CYJSString("alls"), alls
);
1567 JSObjectRef
last(NULL
), curr(global
);
1569 goto next
; for (JSValueRef next
;;) {
1570 if (JSValueIsNull(context
, next
))
1573 curr
= CYCastJSObject(context
, next
);
1575 next
= JSObjectGetPrototype(context
, curr
);
1578 JSObjectSetPrototype(context
, last
, all
);
1581 CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
, kJSPropertyAttributeDontEnum
);
1583 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
1584 CYSetProperty(context
, cy
, CYJSString("System"), System
);
1586 CYSetProperty(context
, global
, CYJSString("system"), System
);
1587 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
1588 //CYSetProperty(context, System, CYJSString("global"), global);
1589 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
1591 if (CYBridgeEntry
*entry
= CYBridgeHash("1dlerror", 8))
1592 entry
->cache_
= new cy::Functor(entry
->value_
, reinterpret_cast<void (*)()>(&dlerror
));
1594 if (hooks_
!= NULL
&& hooks_
->SetupContext
!= NULL
)
1595 (*hooks_
->SetupContext
)(context
);
1597 CYArrayPush(context
, alls
, cycript
);
1600 JSGlobalContextRef
CYGetJSContext() {
1601 CYInitializeDynamic();
1603 static JSGlobalContextRef context_
;
1605 if (context_
== NULL
) {
1606 context_
= JSGlobalContextCreate(Global_
);
1607 CYSetupContext(context_
);