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"
27 #include "cycript.hpp"
29 #include "sig/parse.hpp"
30 #include "sig/ffi_type.hpp"
32 #include "Pooling.hpp"
33 #include "Execute.hpp"
38 #include <ext/stdio_filebuf.h>
46 #include "Cycript.tab.hh"
49 #include "JavaScript.hpp"
52 struct CYHooks
*hooks_
;
54 /* JavaScript Properties {{{ */
55 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) {
56 JSValueRef
exception(NULL
);
57 JSValueRef
value(JSObjectGetPropertyAtIndex(context
, object
, index
, &exception
));
58 CYThrow(context
, exception
);
62 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
63 JSValueRef
exception(NULL
);
64 JSValueRef
value(JSObjectGetProperty(context
, object
, name
, &exception
));
65 CYThrow(context
, exception
);
69 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) {
70 JSValueRef
exception(NULL
);
71 JSObjectSetPropertyAtIndex(context
, object
, index
, value
, &exception
);
72 CYThrow(context
, exception
);
75 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) {
76 JSValueRef
exception(NULL
);
77 JSObjectSetProperty(context
, object
, name
, value
, attributes
, &exception
);
78 CYThrow(context
, exception
);
81 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef (*callback
)(JSContextRef
, JSObjectRef
, JSObjectRef
, size_t, const JSValueRef
[], JSValueRef
*), JSPropertyAttributes attributes
) {
82 CYSetProperty(context
, object
, name
, JSObjectMakeFunctionWithCallback(context
, name
, callback
), attributes
);
85 /* JavaScript Strings {{{ */
86 JSStringRef
CYCopyJSString(const char *value
) {
87 return value
== NULL
? NULL
: JSStringCreateWithUTF8CString(value
);
90 JSStringRef
CYCopyJSString(JSStringRef value
) {
91 return value
== NULL
? NULL
: JSStringRetain(value
);
94 JSStringRef
CYCopyJSString(CYUTF8String value
) {
95 // XXX: this is very wrong; it needs to convert to UTF16 and then create from there
96 return CYCopyJSString(value
.data
);
99 JSStringRef
CYCopyJSString(JSContextRef context
, JSValueRef value
) {
100 if (JSValueIsNull(context
, value
))
102 JSValueRef
exception(NULL
);
103 JSStringRef
string(JSValueToStringCopy(context
, value
, &exception
));
104 CYThrow(context
, exception
);
108 static CYUTF16String
CYCastUTF16String(JSStringRef value
) {
109 return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
));
112 CYUTF8String
CYPoolUTF8String(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
113 return CYPoolUTF8String(pool
, CYCastUTF16String(value
));
116 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
117 CYUTF8String
utf8(CYPoolUTF8String(pool
, context
, value
));
118 _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
);
122 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
123 return JSValueIsNull(context
, value
) ? NULL
: CYPoolCString(pool
, context
, CYJSString(context
, value
));
126 /* Index Offsets {{{ */
127 size_t CYGetIndex(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
128 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
132 static JSClassRef All_
;
133 static JSClassRef Context_
;
135 static JSClassRef Global_
;
136 static JSClassRef Pointer_
;
137 static JSClassRef Struct_
;
141 JSStringRef length_s
;
142 JSStringRef message_s
;
145 JSStringRef prototype_s
;
147 JSStringRef splice_s
;
148 JSStringRef toCYON_s
;
149 JSStringRef toJSON_s
;
150 JSStringRef toPointer_s
;
151 JSStringRef toString_s
;
153 static JSStringRef Result_
;
155 void CYFinalize(JSObjectRef object
) {
156 CYData
*internal(reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
)));
157 _assert(internal
->count_
!= _not(unsigned));
158 if (--internal
->count_
== 0)
162 void Structor_(CYPool
&pool
, sig::Type
*&type
) {
164 type
->primitive
== sig::pointer_P
&&
165 type
->data
.data
.type
!= NULL
&&
166 type
->data
.data
.type
->primitive
== sig::struct_P
&&
167 type
->data
.data
.type
->name
!= NULL
&&
168 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
170 type
->primitive
= sig::typename_P
;
171 type
->data
.data
.type
= NULL
;
175 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
178 size_t length(strlen(type
->name
));
179 char keyed
[length
+ 2];
180 memcpy(keyed
+ 1, type
->name
, length
+ 1);
182 static const char *modes
= "34";
183 for (size_t i(0); i
!= 2; ++i
) {
187 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
190 sig::Parse(pool
, &type
->data
.signature
, entry
->value_
, &Structor_
);
194 sig::Signature signature
;
195 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
196 type
= signature
.elements
[0].type
;
202 JSClassRef
Type_privateData::Class_
;
207 JSGlobalContextRef context_
;
209 Context(JSGlobalContextRef context
) :
218 Type_privateData
*type_
;
221 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type
*type
) :
222 CYOwned(value
, context
, owner
),
223 type_(new(*pool_
) Type_privateData(type
)),
229 struct Struct_privateData
:
232 Type_privateData
*type_
;
234 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
235 CYOwned(NULL
, context
, owner
)
240 typedef std::map
<const char *, Type_privateData
*, CYCStringLess
> TypeMap
;
241 static TypeMap Types_
;
243 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
244 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
245 CYPool
&pool(*internal
->pool_
);
246 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
247 internal
->type_
= typical
;
250 internal
->value_
= data
;
252 size_t size(typical
->GetFFI()->size
);
253 void *copy(internal
->pool_
->malloc
<void>(size
));
254 memcpy(copy
, data
, size
);
255 internal
->value_
= copy
;
258 return JSObjectMake(context
, Struct_
, internal
);
261 static void *CYCastSymbol(const char *name
) {
262 return dlsym(RTLD_DEFAULT
, name
);
265 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
266 return JSValueMakeBoolean(context
, value
);
269 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
270 return JSValueMakeNumber(context
, value
);
273 #define CYCastJSValue_(Type_) \
274 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
275 return JSValueMakeNumber(context, static_cast<double>(value)); \
279 CYCastJSValue_(unsigned int)
280 CYCastJSValue_(long int)
281 CYCastJSValue_(long unsigned int)
282 CYCastJSValue_(long long int)
283 CYCastJSValue_(long long unsigned int)
285 JSValueRef
CYJSUndefined(JSContextRef context
) {
286 return JSValueMakeUndefined(context
);
289 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
290 JSValueRef
exception(NULL
);
291 double number(JSValueToNumber(context
, value
, &exception
));
292 CYThrow(context
, exception
);
296 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
297 return JSValueToBoolean(context
, value
);
300 JSValueRef
CYJSNull(JSContextRef context
) {
301 return JSValueMakeNull(context
);
304 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
305 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
308 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
309 return CYCastJSValue(context
, CYJSString(value
));
312 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
313 JSValueRef
exception(NULL
);
314 JSObjectRef
object(JSValueToObject(context
, value
, &exception
));
315 CYThrow(context
, exception
);
319 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
320 JSValueRef
exception(NULL
);
321 JSValueRef
value(JSObjectCallAsFunction(context
, function
, _this
, count
, arguments
, &exception
));
322 CYThrow(context
, exception
);
326 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
327 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
330 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
335 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
338 return CYJSUndefined(context
);
341 static size_t Nonce_(0);
343 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
345 const char *name(pool
.strcat(CYPoolCString(pool
, context
, arguments
[0]), pool
.itoa(Nonce_
++), NULL
));
346 return CYCastJSValue(context
, name
);
349 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
350 JSGarbageCollect(context
);
351 return CYJSUndefined(context
);
354 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
355 switch (JSType type
= JSValueGetType(context
, value
)) {
356 case kJSTypeUndefined
:
361 return CYCastBool(context
, value
) ? "true" : "false";
363 case kJSTypeNumber
: {
364 std::ostringstream str
;
365 CYNumerify(str
, CYCastDouble(context
, value
));
366 std::string
value(str
.str());
367 return pool
.strmemdup(value
.c_str(), value
.size());
370 case kJSTypeString
: {
371 std::ostringstream str
;
372 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
373 CYStringify(str
, string
.data
, string
.size
);
374 std::string
value(str
.str());
375 return pool
.strmemdup(value
.c_str(), value
.size());
379 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
381 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
385 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
386 JSValueRef
exception(NULL
);
387 const char *cyon(CYPoolCCYON(pool
, context
, value
, &exception
));
388 CYThrow(context
, exception
);
392 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSObjectRef object
) {
393 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
394 if (CYIsCallable(context
, toCYON
)) {
395 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
396 _assert(value
!= NULL
);
397 return CYPoolCString(pool
, context
, value
);
400 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
401 if (CYIsCallable(context
, toJSON
)) {
402 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
403 JSValueRef
exception(NULL
);
404 const char *cyon(CYPoolCCYON(pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), &exception
));
405 CYThrow(context
, exception
);
409 if (JSObjectIsFunction(context
, object
)) {
410 JSValueRef
toString(CYGetProperty(context
, object
, toString_s
));
411 if (CYIsCallable(context
, toString
)) {
412 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
413 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toString
, object
, 1, arguments
));
414 _assert(value
!= NULL
);
415 return CYPoolCString(pool
, context
, value
);
419 std::ostringstream str
;
423 // XXX: this is, sadly, going to leak
424 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
428 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
429 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
430 JSValueRef
value(CYGetProperty(context
, object
, name
));
437 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
441 CYStringify(str
, string
.data
, string
.size
);
443 str
<< ':' << CYPoolCCYON(pool
, context
, value
);
448 JSPropertyNameArrayRelease(names
);
450 std::string
string(str
.str());
451 return pool
.strmemdup(string
.c_str(), string
.size());
454 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
456 std::ostringstream str
;
460 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
463 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
464 JSValueRef
value(CYGetProperty(context
, _this
, index
));
471 if (!JSValueIsUndefined(context
, value
))
472 str
<< CYPoolCCYON(pool
, context
, value
);
481 std::string
value(str
.str());
482 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
485 static JSValueRef
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
487 std::ostringstream str
;
489 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
)));
490 CYStringify(str
, string
.data
, string
.size
);
492 std::string
value(str
.str());
493 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
496 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
497 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
498 return JSObjectMake(context
, Pointer_
, internal
);
501 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const char *type
) {
502 return JSObjectMake(context
, Functor_
, new cy::Functor(type
, function
));
505 static JSObjectRef
CYMakeFunctor(JSContextRef context
, const char *symbol
, const char *type
, void **cache
) {
506 cy::Functor
*internal
;
508 internal
= reinterpret_cast<cy::Functor
*>(*cache
);
510 void (*function
)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol
)));
511 if (function
== NULL
)
514 internal
= new cy::Functor(type
, function
);
519 return JSObjectMake(context
, Functor_
, internal
);
522 static bool CYGetOffset(CYPool
&pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
523 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
526 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
527 switch (JSValueGetType(context
, value
)) {
530 case kJSTypeObject
: {
531 JSObjectRef
object((JSObjectRef
) value
);
532 if (JSValueIsObjectOfClass(context
, value
, Pointer_
)) {
533 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
534 return internal
->value_
;
536 JSValueRef
toPointer(CYGetProperty(context
, object
, toPointer_s
));
537 if (CYIsCallable(context
, toPointer
)) {
538 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
));
539 _assert(value
!= NULL
);
540 return CYCastPointer_(context
, value
);
543 double number(CYCastDouble(context
, value
));
544 if (std::isnan(number
))
545 throw CYJSError(context
, "cannot convert value to pointer");
546 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
550 void CYPoolFFI(CYPool
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
551 switch (type
->primitive
) {
553 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
556 #define CYPoolFFI_(primitive, native) \
557 case sig::primitive ## _P: \
558 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
561 CYPoolFFI_(uchar
, unsigned char)
562 CYPoolFFI_(char, char)
563 CYPoolFFI_(ushort
, unsigned short)
564 CYPoolFFI_(short, short)
565 CYPoolFFI_(ulong
, unsigned long)
566 CYPoolFFI_(long, long)
567 CYPoolFFI_(uint
, unsigned int)
569 CYPoolFFI_(ulonglong
, unsigned long long)
570 CYPoolFFI_(longlong
, long long)
571 CYPoolFFI_(float, float)
572 CYPoolFFI_(double, double)
575 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
576 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
577 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
578 ffi_type
*field(ffi
->elements
[index
]);
581 if (aggregate
== NULL
)
584 rhs
= CYGetProperty(context
, aggregate
, index
);
585 if (JSValueIsUndefined(context
, rhs
))
586 throw CYJSError(context
, "unable to extract array value");
589 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
596 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
600 _assert(pool
!= NULL
);
601 *reinterpret_cast<const char **>(data
) = CYPoolCString(*pool
, context
, value
);
604 case sig::struct_P
: {
605 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
606 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
607 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
608 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
609 ffi_type
*field(ffi
->elements
[index
]);
612 if (aggregate
== NULL
)
615 rhs
= CYGetProperty(context
, aggregate
, index
);
616 if (JSValueIsUndefined(context
, rhs
)) {
617 if (element
->name
!= NULL
)
618 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
621 if (JSValueIsUndefined(context
, rhs
)) undefined
:
622 throw CYJSError(context
, "unable to extract structure value");
626 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
636 if (hooks_
!= NULL
&& hooks_
->PoolFFI
!= NULL
)
637 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
640 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
644 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
645 switch (type
->primitive
) {
647 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
649 #define CYFromFFI_(primitive, native) \
650 case sig::primitive ## _P: \
651 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
653 CYFromFFI_(uchar, unsigned char)
654 CYFromFFI_(char, char)
655 CYFromFFI_(ushort
, unsigned short)
656 CYFromFFI_(short, short)
657 CYFromFFI_(ulong
, unsigned long)
658 CYFromFFI_(long, long)
659 CYFromFFI_(uint
, unsigned int)
661 CYFromFFI_(ulonglong
, unsigned long long)
662 CYFromFFI_(longlong
, long long)
663 CYFromFFI_(float, float)
664 CYFromFFI_(double, double)
667 if (void *pointer
= data
)
668 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
672 if (void *pointer
= *reinterpret_cast<void **>(data
))
673 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
677 if (char *utf8
= *reinterpret_cast<char **>(data
))
678 return CYCastJSValue(context
, utf8
);
682 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
684 return CYJSUndefined(context
);
687 return CYJSNull(context
);
689 if (hooks_
!= NULL
&& hooks_
->FromFFI
!= NULL
)
690 if (JSValueRef value
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
693 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
697 void CYExecuteClosure(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
, JSValueRef (*adapter
)(JSContextRef
, size_t, JSValueRef
[], JSObjectRef
)) {
698 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
700 JSContextRef
context(internal
->context_
);
702 size_t count(internal
->cif_
.nargs
);
703 JSValueRef values
[count
];
705 for (size_t index(0); index
!= count
; ++index
)
706 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
708 JSValueRef
value(adapter(context
, count
, values
, internal
->function_
));
709 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
712 static JSValueRef
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
) {
713 return CYCallAsFunction(context
, function
, NULL
, count
, values
);
716 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
717 CYExecuteClosure(cif
, result
, arguments
, arg
, &FunctionAdapter_
);
720 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const char *type
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
721 // XXX: in case of exceptions this will leak
722 // XXX: in point of fact, this may /need/ to leak :(
723 Closure_privateData
*internal(new Closure_privateData(context
, function
, type
));
725 #if defined(__APPLE__) && defined(__arm__)
727 ffi_closure
*writable(reinterpret_cast<ffi_closure
*>(ffi_closure_alloc(sizeof(ffi_closure
), &executable
)));
729 ffi_status
status(ffi_prep_closure_loc(writable
, &internal
->cif_
, callback
, internal
, executable
));
730 _assert(status
== FFI_OK
);
732 internal
->value_
= executable
;
734 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
735 NULL
, sizeof(ffi_closure
),
736 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
740 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
741 _assert(status
== FFI_OK
);
743 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
745 internal
->value_
= closure
;
751 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const char *type
) {
752 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, type
, &FunctionClosure_
));
753 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
754 // XXX: see above notes about needing to leak
755 JSValueProtect(CYGetJSContext(context
), object
);
759 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
760 return CYCastJSObject(context
, CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
));
763 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const char *type
) {
764 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
766 JSValueRef
exception(NULL
);
767 bool function(JSValueIsInstanceOfConstructor(context
, value
, Function
, &exception
));
768 CYThrow(context
, exception
);
771 JSObjectRef
function(CYCastJSObject(context
, value
));
772 return CYMakeFunctor(context
, function
, type
);
774 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
775 return CYMakeFunctor(context
, function
, type
);
779 static bool Index_(CYPool
&pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
780 Type_privateData
*typical(internal
->type_
);
781 sig::Type
*type(typical
->type_
);
785 const char *name(CYPoolCString(pool
, context
, property
));
786 size_t length(strlen(name
));
787 double number(CYCastDouble(name
, length
));
789 size_t count(type
->data
.signature
.count
);
791 if (std::isnan(number
)) {
792 if (property
== NULL
)
795 sig::Element
*elements(type
->data
.signature
.elements
);
797 for (size_t local(0); local
!= count
; ++local
) {
798 sig::Element
*element(&elements
[local
]);
799 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
807 index
= static_cast<ssize_t
>(number
);
808 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
813 ffi_type
**elements(typical
->GetFFI()->elements
);
815 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
816 for (ssize_t
local(0); local
!= index
; ++local
)
817 base
+= elements
[local
]->size
;
822 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
824 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
826 if (JSStringIsEqual(property
, length_s
))
827 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
829 Type_privateData
*typical(internal
->type_
);
831 if (typical
->type_
== NULL
)
835 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
837 else if (!CYGetOffset(pool
, context
, property
, offset
))
840 ffi_type
*ffi(typical
->GetFFI());
842 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
843 base
+= ffi
->size
* offset
;
845 JSObjectRef
owner(internal
->GetOwner() ?: object
);
846 return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
);
849 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
851 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
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 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
872 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
873 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
874 Type_privateData
*typical(internal
->type_
);
875 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
878 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
880 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
881 Type_privateData
*typical(internal
->type_
);
886 if (!Index_(pool
, context
, internal
, property
, index
, base
))
889 JSObjectRef
owner(internal
->GetOwner() ?: object
);
891 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
894 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
896 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
897 Type_privateData
*typical(internal
->type_
);
902 if (!Index_(pool
, context
, internal
, property
, index
, base
))
905 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
909 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
910 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
911 Type_privateData
*typical(internal
->type_
);
912 sig::Type
*type(typical
->type_
);
917 size_t count(type
->data
.signature
.count
);
918 sig::Element
*elements(type
->data
.signature
.elements
);
922 for (size_t index(0); index
!= count
; ++index
) {
924 name
= elements
[index
].name
;
927 sprintf(number
, "%zu", index
);
931 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
935 JSValueRef
CYCallFunction(CYPool
&pool
, JSContextRef context
, size_t setups
, void *setup
[], size_t count
, const JSValueRef arguments
[], bool initialize
, JSValueRef
*exception
, sig::Signature
*signature
, ffi_cif
*cif
, void (*function
)()) { CYTry
{
936 if (setups
+ count
!= signature
->count
- 1)
937 throw CYJSError(context
, "incorrect number of arguments to ffi function");
939 size_t size(setups
+ count
);
941 memcpy(values
, setup
, sizeof(void *) * setups
);
943 for (size_t index(setups
); index
!= size
; ++index
) {
944 sig::Element
*element(&signature
->elements
[index
+ 1]);
945 ffi_type
*ffi(cif
->arg_types
[index
]);
947 values
[index
] = new(pool
) uint8_t[ffi
->size
];
948 CYPoolFFI(&pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
951 uint8_t value
[cif
->rtype
->size
];
953 if (hooks_
!= NULL
&& hooks_
->CallFunction
!= NULL
)
954 (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
);
956 ffi_call(cif
, function
, value
, values
);
958 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
961 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
963 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
964 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, exception
, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
967 JSObjectRef
CYMakeType(JSContextRef context
, const char *type
) {
968 Type_privateData
*internal(new Type_privateData(type
));
969 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
972 JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
973 Type_privateData
*internal(new Type_privateData(type
));
974 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
977 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
978 JSObjectRef
global(CYGetGlobalObject(context
));
979 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
980 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
982 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
983 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
984 if (JSValueRef value
= CYGetProperty(context
, space
, property
))
985 if (!JSValueIsUndefined(context
, value
))
989 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
991 size_t length(name
.size
);
992 char keyed
[length
+ 2];
993 memcpy(keyed
+ 1, name
.data
, length
+ 1);
995 static const char *modes
= "0124";
996 for (size_t i(0); i
!= 4; ++i
) {
1000 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
1003 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
);
1006 return CYMakeFunctor(context
, name
.data
, entry
->value_
, &entry
->cache_
);
1009 if (void *symbol
= CYCastSymbol(name
.data
)) {
1010 // XXX: this is horrendously inefficient
1011 sig::Signature signature
;
1012 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
1014 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1015 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
1018 // XXX: implement case 3
1020 return CYMakeType(context
, entry
->value_
);
1027 static void All_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1028 JSObjectRef
global(CYGetGlobalObject(context
));
1029 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1030 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1032 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1033 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1))) {
1034 JSPropertyNameArrayRef
subset(JSObjectCopyPropertyNames(context
, space
));
1035 for (size_t index(0), count(JSPropertyNameArrayGetCount(subset
)); index
!= count
; ++index
)
1036 JSPropertyNameAccumulatorAddName(names
, JSPropertyNameArrayGetNameAtIndex(subset
, index
));
1037 JSPropertyNameArrayRelease(subset
);
1041 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1043 throw CYJSError(context
, "incorrect number of arguments to Pointer constructor");
1047 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1048 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1050 sig::Signature signature
;
1051 sig::Parse(pool
, &signature
, type
, &Structor_
);
1053 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1056 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1058 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1060 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1061 return CYMakeType(context
, type
);
1064 static JSValueRef
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1066 throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf");
1067 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1070 size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0])));
1071 if (index
== _not(size_t))
1072 throw CYJSError(context
, "invalid array size used with Type.arrayOf");
1078 type
.primitive
= sig::array_P
;
1079 type
.data
.data
.type
= internal
->type_
;
1080 type
.data
.data
.size
= index
;
1082 return CYMakeType(context
, &type
);
1085 static JSValueRef
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1087 throw CYJSError(context
, "incorrect number of arguments to Type.constant");
1088 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1090 sig::Type
type(*internal
->type_
);
1091 type
.flags
|= JOC_TYPE_CONST
;
1092 return CYMakeType(context
, &type
);
1095 static JSValueRef
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1097 throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo");
1098 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1104 type
.primitive
= sig::pointer_P
;
1105 type
.data
.data
.type
= internal
->type_
;
1106 type
.data
.data
.size
= 0;
1108 return CYMakeType(context
, &type
);
1111 static JSValueRef
Type_callAsFunction_withName(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.withName");
1114 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1117 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1119 sig::Type
type(*internal
->type_
);
1121 return CYMakeType(context
, &type
);
1124 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1126 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1127 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1129 sig::Type
*type(internal
->type_
);
1130 ffi_type
*ffi(internal
->GetFFI());
1132 uint8_t value
[ffi
->size
];
1134 CYPoolFFI(&pool
, context
, type
, ffi
, value
, arguments
[0]);
1135 return CYFromFFI(context
, type
, ffi
, value
);
1138 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1140 throw CYJSError(context
, "incorrect number of arguments to Type allocator");
1141 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1143 sig::Type
*type(internal
->type_
);
1146 if (type
->primitive
!= sig::array_P
)
1147 length
= _not(size_t);
1149 length
= type
->data
.data
.size
;
1150 type
= type
->data
.data
.type
;
1153 void *value(malloc(internal
->GetFFI()->size
));
1154 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1157 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1159 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1161 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1162 return CYMakeFunctor(context
, arguments
[0], type
);
1165 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1166 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1167 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1170 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1171 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1174 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1175 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1177 sprintf(string
, "%p", internal
->value_
);
1178 return CYCastJSValue(context
, string
);
1181 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1182 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1183 if (internal
->length_
!= _not(size_t)) {
1184 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
1185 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1186 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1189 sprintf(string
, "%p", internal
->value_
);
1190 return CYCastJSValue(context
, string
);
1194 static JSValueRef
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) {
1195 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1196 return CYCastJSValue(context
, internal
->GetFFI()->alignment
);
1199 static JSValueRef
Type_getProperty_name(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) {
1200 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1201 return CYCastJSValue(context
, internal
->type_
->name
);
1204 static JSValueRef
Type_getProperty_size(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) {
1205 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1206 return CYCastJSValue(context
, internal
->GetFFI()->size
);
1209 static JSValueRef
Type_callAsFunction_toString(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 return CYCastJSValue(context
, CYJSString(type
));
1216 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1217 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1219 const char *type(sig::Unparse(pool
, internal
->type_
));
1220 std::ostringstream str
;
1221 CYStringify(str
, type
, strlen(type
));
1222 char *cyon(pool
.strcat("new Type(", str
.str().c_str(), ")", NULL
));
1223 return CYCastJSValue(context
, CYJSString(cyon
));
1226 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1227 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1230 static JSStaticFunction Pointer_staticFunctions
[4] = {
1231 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1232 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1233 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1237 static JSStaticFunction Struct_staticFunctions
[2] = {
1238 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1242 static JSStaticFunction Functor_staticFunctions
[4] = {
1243 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1244 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1245 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1250 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1253 static JSStaticValue Type_staticValues
[4] = {
1254 {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1255 {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1256 {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1257 {NULL
, NULL
, NULL
, 0}
1260 static JSStaticFunction Type_staticFunctions
[8] = {
1261 {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1262 {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1263 {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1264 {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1265 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1266 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1267 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1271 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1273 void CYSetArgs(int argc
, const char *argv
[]) {
1274 JSContextRef
context(CYGetJSContext());
1275 JSValueRef args
[argc
];
1276 for (int i(0); i
!= argc
; ++i
)
1277 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1280 if (JSObjectMakeArray$
!= NULL
) {
1281 JSValueRef
exception(NULL
);
1282 array
= (*JSObjectMakeArray$
)(context
, argc
, args
, &exception
);
1283 CYThrow(context
, exception
);
1285 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1286 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1287 array
= CYCastJSObject(context
, value
);
1290 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1291 CYSetProperty(context
, System
, CYJSString("args"), array
);
1294 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1295 return JSContextGetGlobalObject(context
);
1298 const char *CYExecute(CYPool
&pool
, CYUTF8String code
) {
1299 JSContextRef
context(CYGetJSContext());
1300 JSValueRef
exception(NULL
), result
;
1303 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1304 handle
= (*hooks_
->ExecuteStart
)(context
);
1311 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1312 } catch (const char *error
) {
1316 if (exception
!= NULL
) error
:
1317 return CYPoolCString(pool
, context
, CYJSString(context
, exception
));
1319 if (JSValueIsUndefined(context
, result
))
1323 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1324 } catch (const char *error
) {
1328 if (exception
!= NULL
)
1331 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1333 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1334 (*hooks_
->ExecuteEnd
)(context
, handle
);
1338 extern "C" void CydgetSetupContext(JSGlobalContextRef context
) {
1339 CYSetupContext(context
);
1342 static bool initialized_
= false;
1344 void CYInitializeDynamic() {
1346 initialized_
= true;
1349 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1351 JSClassDefinition definition
;
1353 definition
= kJSClassDefinitionEmpty
;
1354 definition
.className
= "All";
1355 definition
.getProperty
= &All_getProperty
;
1356 definition
.getPropertyNames
= &All_getPropertyNames
;
1357 All_
= JSClassCreate(&definition
);
1359 definition
= kJSClassDefinitionEmpty
;
1360 definition
.className
= "Context";
1361 definition
.finalize
= &CYFinalize
;
1362 Context_
= JSClassCreate(&definition
);
1364 definition
= kJSClassDefinitionEmpty
;
1365 definition
.className
= "Functor";
1366 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1367 definition
.callAsFunction
= &Functor_callAsFunction
;
1368 definition
.finalize
= &CYFinalize
;
1369 Functor_
= JSClassCreate(&definition
);
1371 definition
= kJSClassDefinitionEmpty
;
1372 definition
.className
= "Pointer";
1373 definition
.staticFunctions
= Pointer_staticFunctions
;
1374 definition
.getProperty
= &Pointer_getProperty
;
1375 definition
.setProperty
= &Pointer_setProperty
;
1376 definition
.finalize
= &CYFinalize
;
1377 Pointer_
= JSClassCreate(&definition
);
1379 definition
= kJSClassDefinitionEmpty
;
1380 definition
.className
= "Struct";
1381 definition
.staticFunctions
= Struct_staticFunctions
;
1382 definition
.getProperty
= &Struct_getProperty
;
1383 definition
.setProperty
= &Struct_setProperty
;
1384 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1385 definition
.finalize
= &CYFinalize
;
1386 Struct_
= JSClassCreate(&definition
);
1388 definition
= kJSClassDefinitionEmpty
;
1389 definition
.className
= "Type";
1390 definition
.staticValues
= Type_staticValues
;
1391 definition
.staticFunctions
= Type_staticFunctions
;
1392 definition
.callAsFunction
= &Type_callAsFunction
;
1393 definition
.callAsConstructor
= &Type_callAsConstructor
;
1394 definition
.finalize
= &CYFinalize
;
1395 Type_privateData::Class_
= JSClassCreate(&definition
);
1397 definition
= kJSClassDefinitionEmpty
;
1398 definition
.className
= "Global";
1399 //definition.getProperty = &Global_getProperty;
1400 Global_
= JSClassCreate(&definition
);
1402 Array_s
= JSStringCreateWithUTF8CString("Array");
1403 cy_s
= JSStringCreateWithUTF8CString("$cy");
1404 length_s
= JSStringCreateWithUTF8CString("length");
1405 message_s
= JSStringCreateWithUTF8CString("message");
1406 name_s
= JSStringCreateWithUTF8CString("name");
1407 pop_s
= JSStringCreateWithUTF8CString("pop");
1408 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1409 push_s
= JSStringCreateWithUTF8CString("push");
1410 splice_s
= JSStringCreateWithUTF8CString("splice");
1411 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1412 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1413 toPointer_s
= JSStringCreateWithUTF8CString("toPointer");
1414 toString_s
= JSStringCreateWithUTF8CString("toString");
1416 Result_
= JSStringCreateWithUTF8CString("_");
1418 if (hooks_
!= NULL
&& hooks_
->Initialize
!= NULL
)
1419 (*hooks_
->Initialize
)();
1422 void CYThrow(JSContextRef context
, JSValueRef value
) {
1424 throw CYJSError(context
, value
);
1427 const char *CYJSError::PoolCString(CYPool
&pool
) const {
1428 // XXX: this used to be CYPoolCString
1429 return CYPoolCCYON(pool
, context_
, value_
);
1432 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1433 // XXX: what if the context is different?
1437 JSValueRef
CYCastJSError(JSContextRef context
, const char *message
) {
1438 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString("Error")));
1440 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1442 JSValueRef
exception(NULL
);
1443 JSValueRef
value(JSObjectCallAsConstructor(context
, Error
, 1, arguments
, &exception
));
1444 CYThrow(context
, exception
);
1449 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1450 return CYCastJSError(context
, message_
);
1453 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1454 _assert(context
!= NULL
);
1459 va_start(args
, format
);
1460 // XXX: there might be a beter way to think about this
1461 const char *message(pool
.vsprintf(64, format
, args
));
1464 value_
= CYCastJSError(context
, message
);
1467 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1468 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1471 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
1472 JSValueRef
exception(NULL
);
1474 CYInitializeDynamic();
1476 JSObjectRef
global(CYGetGlobalObject(context
));
1478 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
1479 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
1481 /* Cache Globals {{{ */
1482 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
1483 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
1485 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
1486 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
1488 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
1489 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
1491 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
1492 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
1494 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
1495 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
1497 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1498 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
1500 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
1501 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
1503 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
1504 CYSetProperty(context
, cy
, CYJSString("String"), String
);
1506 JSObjectRef
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
)));
1507 CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
);
1510 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1511 CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1513 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1514 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1515 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
1517 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1518 JSObjectSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
1519 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
1521 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1522 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1524 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
1525 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
1527 JSObjectRef
alls(JSObjectCallAsConstructor(context
, Array
, 0, NULL
, &exception
));
1528 CYThrow(context
, exception
);
1529 CYSetProperty(context
, cycript
, CYJSString("alls"), alls
);
1532 JSObjectRef
last(NULL
), curr(global
);
1534 goto next
; for (JSValueRef next
;;) {
1535 if (JSValueIsNull(context
, next
))
1538 curr
= CYCastJSObject(context
, next
);
1540 next
= JSObjectGetPrototype(context
, curr
);
1543 JSObjectSetPrototype(context
, last
, all
);
1546 CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
, kJSPropertyAttributeDontEnum
);
1548 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
1549 CYSetProperty(context
, cy
, CYJSString("System"), System
);
1551 CYSetProperty(context
, global
, CYJSString("system"), System
);
1552 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
1553 //CYSetProperty(context, System, CYJSString("global"), global);
1554 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
1556 if (CYBridgeEntry
*entry
= CYBridgeHash("1dlerror", 8))
1557 entry
->cache_
= new cy::Functor(entry
->value_
, reinterpret_cast<void (*)()>(&dlerror
));
1559 if (hooks_
!= NULL
&& hooks_
->SetupContext
!= NULL
)
1560 (*hooks_
->SetupContext
)(context
);
1562 CYArrayPush(context
, alls
, cycript
);
1565 JSGlobalContextRef
CYGetJSContext() {
1566 CYInitializeDynamic();
1568 static JSGlobalContextRef context_
;
1570 if (context_
== NULL
) {
1571 context_
= JSGlobalContextCreate(Global_
);
1572 CYSetupContext(context_
);