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 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
332 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
335 return CYJSUndefined(context
);
338 static size_t Nonce_(0);
340 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
342 const char *name(pool
.strcat(CYPoolCString(pool
, context
, arguments
[0]), pool
.itoa(Nonce_
++), NULL
));
343 return CYCastJSValue(context
, name
);
346 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
347 JSGarbageCollect(context
);
348 return CYJSUndefined(context
);
351 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
352 switch (JSType type
= JSValueGetType(context
, value
)) {
353 case kJSTypeUndefined
:
358 return CYCastBool(context
, value
) ? "true" : "false";
360 case kJSTypeNumber
: {
361 std::ostringstream str
;
362 CYNumerify(str
, CYCastDouble(context
, value
));
363 std::string
value(str
.str());
364 return pool
.strmemdup(value
.c_str(), value
.size());
367 case kJSTypeString
: {
368 std::ostringstream str
;
369 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
370 CYStringify(str
, string
.data
, string
.size
);
371 std::string
value(str
.str());
372 return pool
.strmemdup(value
.c_str(), value
.size());
376 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
378 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
382 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
383 JSValueRef
exception(NULL
);
384 const char *cyon(CYPoolCCYON(pool
, context
, value
, &exception
));
385 CYThrow(context
, exception
);
389 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSObjectRef object
) {
390 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
391 if (CYIsCallable(context
, toCYON
)) {
392 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
393 _assert(value
!= NULL
);
394 return CYPoolCString(pool
, context
, value
);
397 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
398 if (CYIsCallable(context
, toJSON
)) {
399 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
400 JSValueRef
exception(NULL
);
401 const char *cyon(CYPoolCCYON(pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), &exception
));
402 CYThrow(context
, exception
);
406 if (JSObjectIsFunction(context
, object
)) {
407 JSValueRef
toString(CYGetProperty(context
, object
, toString_s
));
408 if (CYIsCallable(context
, toString
)) {
409 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
410 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toString
, object
, 1, arguments
));
411 _assert(value
!= NULL
);
412 return CYPoolCString(pool
, context
, value
);
416 std::ostringstream str
;
420 // XXX: this is, sadly, going to leak
421 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
425 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
426 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
427 JSValueRef
value(CYGetProperty(context
, object
, name
));
434 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
438 CYStringify(str
, string
.data
, string
.size
);
440 str
<< ':' << CYPoolCCYON(pool
, context
, value
);
445 JSPropertyNameArrayRelease(names
);
447 std::string
string(str
.str());
448 return pool
.strmemdup(string
.c_str(), string
.size());
451 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
453 std::ostringstream str
;
457 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
460 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
461 JSValueRef
value(CYGetProperty(context
, _this
, index
));
468 if (!JSValueIsUndefined(context
, value
))
469 str
<< CYPoolCCYON(pool
, context
, value
);
478 std::string
value(str
.str());
479 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
482 static JSValueRef
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
484 std::ostringstream str
;
486 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
)));
487 CYStringify(str
, string
.data
, string
.size
);
489 std::string
value(str
.str());
490 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
493 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
494 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
495 return JSObjectMake(context
, Pointer_
, internal
);
498 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const char *type
) {
499 return JSObjectMake(context
, Functor_
, new cy::Functor(type
, function
));
502 static JSObjectRef
CYMakeFunctor(JSContextRef context
, const char *symbol
, const char *type
, void **cache
) {
503 cy::Functor
*internal
;
505 internal
= reinterpret_cast<cy::Functor
*>(*cache
);
507 void (*function
)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol
)));
508 if (function
== NULL
)
511 internal
= new cy::Functor(type
, function
);
516 return JSObjectMake(context
, Functor_
, internal
);
519 static bool CYGetOffset(CYPool
&pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
520 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
523 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
524 switch (JSValueGetType(context
, value
)) {
527 case kJSTypeObject
: {
528 JSObjectRef
object((JSObjectRef
) value
);
529 if (JSValueIsObjectOfClass(context
, value
, Pointer_
)) {
530 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
531 return internal
->value_
;
533 JSValueRef
toPointer(CYGetProperty(context
, object
, toPointer_s
));
534 if (CYIsCallable(context
, toPointer
)) {
535 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
));
536 _assert(value
!= NULL
);
537 return CYCastPointer_(context
, value
);
540 double number(CYCastDouble(context
, value
));
541 if (std::isnan(number
))
542 throw CYJSError(context
, "cannot convert value to pointer");
543 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
547 void CYPoolFFI(CYPool
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
548 switch (type
->primitive
) {
550 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
553 #define CYPoolFFI_(primitive, native) \
554 case sig::primitive ## _P: \
555 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
558 CYPoolFFI_(uchar
, unsigned char)
559 CYPoolFFI_(char, char)
560 CYPoolFFI_(ushort
, unsigned short)
561 CYPoolFFI_(short, short)
562 CYPoolFFI_(ulong
, unsigned long)
563 CYPoolFFI_(long, long)
564 CYPoolFFI_(uint
, unsigned int)
566 CYPoolFFI_(ulonglong
, unsigned long long)
567 CYPoolFFI_(longlong
, long long)
568 CYPoolFFI_(float, float)
569 CYPoolFFI_(double, double)
572 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
573 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
574 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
575 ffi_type
*field(ffi
->elements
[index
]);
578 if (aggregate
== NULL
)
581 rhs
= CYGetProperty(context
, aggregate
, index
);
582 if (JSValueIsUndefined(context
, rhs
))
583 throw CYJSError(context
, "unable to extract array value");
586 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
593 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
597 _assert(pool
!= NULL
);
598 *reinterpret_cast<const char **>(data
) = CYPoolCString(*pool
, context
, value
);
601 case sig::struct_P
: {
602 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
603 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
604 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
605 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
606 ffi_type
*field(ffi
->elements
[index
]);
609 if (aggregate
== NULL
)
612 rhs
= CYGetProperty(context
, aggregate
, index
);
613 if (JSValueIsUndefined(context
, rhs
)) {
614 if (element
->name
!= NULL
)
615 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
618 if (JSValueIsUndefined(context
, rhs
)) undefined
:
619 throw CYJSError(context
, "unable to extract structure value");
623 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
633 if (hooks_
!= NULL
&& hooks_
->PoolFFI
!= NULL
)
634 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
637 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
641 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
642 switch (type
->primitive
) {
644 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
646 #define CYFromFFI_(primitive, native) \
647 case sig::primitive ## _P: \
648 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
650 CYFromFFI_(uchar, unsigned char)
651 CYFromFFI_(char, char)
652 CYFromFFI_(ushort
, unsigned short)
653 CYFromFFI_(short, short)
654 CYFromFFI_(ulong
, unsigned long)
655 CYFromFFI_(long, long)
656 CYFromFFI_(uint
, unsigned int)
658 CYFromFFI_(ulonglong
, unsigned long long)
659 CYFromFFI_(longlong
, long long)
660 CYFromFFI_(float, float)
661 CYFromFFI_(double, double)
664 if (void *pointer
= data
)
665 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
669 if (void *pointer
= *reinterpret_cast<void **>(data
))
670 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
674 if (char *utf8
= *reinterpret_cast<char **>(data
))
675 return CYCastJSValue(context
, utf8
);
679 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
681 return CYJSUndefined(context
);
684 return CYJSNull(context
);
686 if (hooks_
!= NULL
&& hooks_
->FromFFI
!= NULL
)
687 if (JSValueRef value
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
690 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
694 void CYExecuteClosure(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
, JSValueRef (*adapter
)(JSContextRef
, size_t, JSValueRef
[], JSObjectRef
)) {
695 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
697 JSContextRef
context(internal
->context_
);
699 size_t count(internal
->cif_
.nargs
);
700 JSValueRef values
[count
];
702 for (size_t index(0); index
!= count
; ++index
)
703 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
705 JSValueRef
value(adapter(context
, count
, values
, internal
->function_
));
706 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
709 static JSValueRef
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
) {
710 return CYCallAsFunction(context
, function
, NULL
, count
, values
);
713 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
714 CYExecuteClosure(cif
, result
, arguments
, arg
, &FunctionAdapter_
);
717 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const char *type
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
718 // XXX: in case of exceptions this will leak
719 // XXX: in point of fact, this may /need/ to leak :(
720 Closure_privateData
*internal(new Closure_privateData(context
, function
, type
));
722 #if defined(__APPLE__) && defined(__arm__)
724 ffi_closure
*writable(reinterpret_cast<ffi_closure
*>(ffi_closure_alloc(sizeof(ffi_closure
), &executable
)));
726 ffi_status
status(ffi_prep_closure_loc(writable
, &internal
->cif_
, callback
, internal
, executable
));
727 _assert(status
== FFI_OK
);
729 internal
->value_
= executable
;
731 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
732 NULL
, sizeof(ffi_closure
),
733 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
737 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
738 _assert(status
== FFI_OK
);
740 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
742 internal
->value_
= closure
;
748 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const char *type
) {
749 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, type
, &FunctionClosure_
));
750 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
751 // XXX: see above notes about needing to leak
752 JSValueProtect(CYGetJSContext(context
), object
);
756 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
757 return CYCastJSObject(context
, CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
));
760 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const char *type
) {
761 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
763 JSValueRef
exception(NULL
);
764 bool function(JSValueIsInstanceOfConstructor(context
, value
, Function
, &exception
));
765 CYThrow(context
, exception
);
768 JSObjectRef
function(CYCastJSObject(context
, value
));
769 return CYMakeFunctor(context
, function
, type
);
771 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
772 return CYMakeFunctor(context
, function
, type
);
776 static bool Index_(CYPool
&pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
777 Type_privateData
*typical(internal
->type_
);
778 sig::Type
*type(typical
->type_
);
782 const char *name(CYPoolCString(pool
, context
, property
));
783 size_t length(strlen(name
));
784 double number(CYCastDouble(name
, length
));
786 size_t count(type
->data
.signature
.count
);
788 if (std::isnan(number
)) {
789 if (property
== NULL
)
792 sig::Element
*elements(type
->data
.signature
.elements
);
794 for (size_t local(0); local
!= count
; ++local
) {
795 sig::Element
*element(&elements
[local
]);
796 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
804 index
= static_cast<ssize_t
>(number
);
805 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
810 ffi_type
**elements(typical
->GetFFI()->elements
);
812 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
813 for (ssize_t
local(0); local
!= index
; ++local
)
814 base
+= elements
[local
]->size
;
819 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
821 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
823 if (JSStringIsEqual(property
, length_s
))
824 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
826 Type_privateData
*typical(internal
->type_
);
828 if (typical
->type_
== NULL
)
832 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
834 else if (!CYGetOffset(pool
, context
, property
, offset
))
837 ffi_type
*ffi(typical
->GetFFI());
839 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
840 base
+= ffi
->size
* offset
;
842 JSObjectRef
owner(internal
->GetOwner() ?: object
);
843 return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
);
846 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
848 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
849 Type_privateData
*typical(internal
->type_
);
851 if (typical
->type_
== NULL
)
855 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
857 else if (!CYGetOffset(pool
, context
, property
, offset
))
860 ffi_type
*ffi(typical
->GetFFI());
862 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
863 base
+= ffi
->size
* offset
;
865 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
869 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
870 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
871 Type_privateData
*typical(internal
->type_
);
872 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
875 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
877 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
878 Type_privateData
*typical(internal
->type_
);
883 if (!Index_(pool
, context
, internal
, property
, index
, base
))
886 JSObjectRef
owner(internal
->GetOwner() ?: object
);
888 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
891 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
893 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
894 Type_privateData
*typical(internal
->type_
);
899 if (!Index_(pool
, context
, internal
, property
, index
, base
))
902 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
906 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
907 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
908 Type_privateData
*typical(internal
->type_
);
909 sig::Type
*type(typical
->type_
);
914 size_t count(type
->data
.signature
.count
);
915 sig::Element
*elements(type
->data
.signature
.elements
);
919 for (size_t index(0); index
!= count
; ++index
) {
921 name
= elements
[index
].name
;
924 sprintf(number
, "%zu", index
);
928 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
932 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
{
933 if (setups
+ count
!= signature
->count
- 1)
934 throw CYJSError(context
, "incorrect number of arguments to ffi function");
936 size_t size(setups
+ count
);
938 memcpy(values
, setup
, sizeof(void *) * setups
);
940 for (size_t index(setups
); index
!= size
; ++index
) {
941 sig::Element
*element(&signature
->elements
[index
+ 1]);
942 ffi_type
*ffi(cif
->arg_types
[index
]);
944 values
[index
] = new(pool
) uint8_t[ffi
->size
];
945 CYPoolFFI(&pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
948 uint8_t value
[cif
->rtype
->size
];
950 if (hooks_
!= NULL
&& hooks_
->CallFunction
!= NULL
)
951 (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
);
953 ffi_call(cif
, function
, value
, values
);
955 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
958 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
960 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
961 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, exception
, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
964 JSObjectRef
CYMakeType(JSContextRef context
, const char *type
) {
965 Type_privateData
*internal(new Type_privateData(type
));
966 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
969 JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
970 Type_privateData
*internal(new Type_privateData(type
));
971 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
974 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
975 JSObjectRef
global(CYGetGlobalObject(context
));
976 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
977 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
979 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
980 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
981 if (JSValueRef value
= CYGetProperty(context
, space
, property
))
982 if (!JSValueIsUndefined(context
, value
))
986 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
988 size_t length(name
.size
);
989 char keyed
[length
+ 2];
990 memcpy(keyed
+ 1, name
.data
, length
+ 1);
992 static const char *modes
= "0124";
993 for (size_t i(0); i
!= 4; ++i
) {
997 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
1000 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
);
1003 return CYMakeFunctor(context
, name
.data
, entry
->value_
, &entry
->cache_
);
1006 if (void *symbol
= CYCastSymbol(name
.data
)) {
1007 // XXX: this is horrendously inefficient
1008 sig::Signature signature
;
1009 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
1011 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1012 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
1015 // XXX: implement case 3
1017 return CYMakeType(context
, entry
->value_
);
1024 static void All_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1025 JSObjectRef
global(CYGetGlobalObject(context
));
1026 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1027 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1029 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1030 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1))) {
1031 JSPropertyNameArrayRef
subset(JSObjectCopyPropertyNames(context
, space
));
1032 for (size_t index(0), count(JSPropertyNameArrayGetCount(subset
)); index
!= count
; ++index
)
1033 JSPropertyNameAccumulatorAddName(names
, JSPropertyNameArrayGetNameAtIndex(subset
, index
));
1034 JSPropertyNameArrayRelease(subset
);
1038 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1040 throw CYJSError(context
, "incorrect number of arguments to Pointer constructor");
1044 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1045 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1047 sig::Signature signature
;
1048 sig::Parse(pool
, &signature
, type
, &Structor_
);
1050 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1053 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1055 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1057 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1058 return CYMakeType(context
, type
);
1061 static JSValueRef
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1063 throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf");
1064 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1067 size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0])));
1068 if (index
== _not(size_t))
1069 throw CYJSError(context
, "invalid array size used with Type.arrayOf");
1075 type
.primitive
= sig::array_P
;
1076 type
.data
.data
.type
= internal
->type_
;
1077 type
.data
.data
.size
= index
;
1079 return CYMakeType(context
, &type
);
1082 static JSValueRef
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1084 throw CYJSError(context
, "incorrect number of arguments to Type.constant");
1085 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1087 sig::Type
type(*internal
->type_
);
1088 type
.flags
|= JOC_TYPE_CONST
;
1089 return CYMakeType(context
, &type
);
1092 static JSValueRef
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1094 throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo");
1095 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1101 type
.primitive
= sig::pointer_P
;
1102 type
.data
.data
.type
= internal
->type_
;
1103 type
.data
.data
.size
= 0;
1105 return CYMakeType(context
, &type
);
1108 static JSValueRef
Type_callAsFunction_withName(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.withName");
1111 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1114 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1116 sig::Type
type(*internal
->type_
);
1118 return CYMakeType(context
, &type
);
1121 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1123 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1124 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1126 sig::Type
*type(internal
->type_
);
1127 ffi_type
*ffi(internal
->GetFFI());
1129 uint8_t value
[ffi
->size
];
1131 CYPoolFFI(&pool
, context
, type
, ffi
, value
, arguments
[0]);
1132 return CYFromFFI(context
, type
, ffi
, value
);
1135 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1137 throw CYJSError(context
, "incorrect number of arguments to Type allocator");
1138 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1140 sig::Type
*type(internal
->type_
);
1143 if (type
->primitive
!= sig::array_P
)
1144 length
= _not(size_t);
1146 length
= type
->data
.data
.size
;
1147 type
= type
->data
.data
.type
;
1150 void *value(malloc(internal
->GetFFI()->size
));
1151 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1154 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1156 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1158 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1159 return CYMakeFunctor(context
, arguments
[0], type
);
1162 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1163 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1164 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1167 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1168 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1171 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1172 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1174 sprintf(string
, "%p", internal
->value_
);
1175 return CYCastJSValue(context
, string
);
1178 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1179 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1180 if (internal
->length_
!= _not(size_t)) {
1181 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
1182 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1183 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1186 sprintf(string
, "%p", internal
->value_
);
1187 return CYCastJSValue(context
, string
);
1191 static JSValueRef
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) {
1192 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1193 return CYCastJSValue(context
, internal
->GetFFI()->alignment
);
1196 static JSValueRef
Type_getProperty_name(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) {
1197 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1198 return CYCastJSValue(context
, internal
->type_
->name
);
1201 static JSValueRef
Type_getProperty_size(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) {
1202 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1203 return CYCastJSValue(context
, internal
->GetFFI()->size
);
1206 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1207 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1209 const char *type(sig::Unparse(pool
, internal
->type_
));
1210 return CYCastJSValue(context
, CYJSString(type
));
1213 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1214 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1216 const char *type(sig::Unparse(pool
, internal
->type_
));
1217 std::ostringstream str
;
1218 CYStringify(str
, type
, strlen(type
));
1219 char *cyon(pool
.strcat("new Type(", str
.str().c_str(), ")", NULL
));
1220 return CYCastJSValue(context
, CYJSString(cyon
));
1223 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1224 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1227 static JSStaticFunction Pointer_staticFunctions
[4] = {
1228 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1229 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1230 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1234 static JSStaticFunction Struct_staticFunctions
[2] = {
1235 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1239 static JSStaticFunction Functor_staticFunctions
[4] = {
1240 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1241 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1242 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1247 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1250 static JSStaticValue Type_staticValues
[4] = {
1251 {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1252 {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1253 {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1254 {NULL
, NULL
, NULL
, 0}
1257 static JSStaticFunction Type_staticFunctions
[8] = {
1258 {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1259 {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1260 {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1261 {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1262 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1263 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1264 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1268 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1270 void CYSetArgs(int argc
, const char *argv
[]) {
1271 JSContextRef
context(CYGetJSContext());
1272 JSValueRef args
[argc
];
1273 for (int i(0); i
!= argc
; ++i
)
1274 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1277 if (JSObjectMakeArray$
!= NULL
) {
1278 JSValueRef
exception(NULL
);
1279 array
= (*JSObjectMakeArray$
)(context
, argc
, args
, &exception
);
1280 CYThrow(context
, exception
);
1282 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1283 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1284 array
= CYCastJSObject(context
, value
);
1287 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1288 CYSetProperty(context
, System
, CYJSString("args"), array
);
1291 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1292 return JSContextGetGlobalObject(context
);
1295 const char *CYExecute(CYPool
&pool
, CYUTF8String code
) {
1296 JSContextRef
context(CYGetJSContext());
1297 JSValueRef
exception(NULL
), result
;
1300 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1301 handle
= (*hooks_
->ExecuteStart
)(context
);
1308 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1309 } catch (const char *error
) {
1313 if (exception
!= NULL
) error
:
1314 return CYPoolCString(pool
, context
, CYJSString(context
, exception
));
1316 if (JSValueIsUndefined(context
, result
))
1320 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1321 } catch (const char *error
) {
1325 if (exception
!= NULL
)
1328 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1330 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1331 (*hooks_
->ExecuteEnd
)(context
, handle
);
1335 extern "C" void CydgetSetupContext(JSGlobalContextRef context
) {
1336 CYSetupContext(context
);
1339 static bool initialized_
= false;
1341 void CYInitializeDynamic() {
1343 initialized_
= true;
1346 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1348 JSClassDefinition definition
;
1350 definition
= kJSClassDefinitionEmpty
;
1351 definition
.className
= "All";
1352 definition
.getProperty
= &All_getProperty
;
1353 definition
.getPropertyNames
= &All_getPropertyNames
;
1354 All_
= JSClassCreate(&definition
);
1356 definition
= kJSClassDefinitionEmpty
;
1357 definition
.className
= "Context";
1358 definition
.finalize
= &CYFinalize
;
1359 Context_
= JSClassCreate(&definition
);
1361 definition
= kJSClassDefinitionEmpty
;
1362 definition
.className
= "Functor";
1363 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1364 definition
.callAsFunction
= &Functor_callAsFunction
;
1365 definition
.finalize
= &CYFinalize
;
1366 Functor_
= JSClassCreate(&definition
);
1368 definition
= kJSClassDefinitionEmpty
;
1369 definition
.className
= "Pointer";
1370 definition
.staticFunctions
= Pointer_staticFunctions
;
1371 definition
.getProperty
= &Pointer_getProperty
;
1372 definition
.setProperty
= &Pointer_setProperty
;
1373 definition
.finalize
= &CYFinalize
;
1374 Pointer_
= JSClassCreate(&definition
);
1376 definition
= kJSClassDefinitionEmpty
;
1377 definition
.className
= "Struct";
1378 definition
.staticFunctions
= Struct_staticFunctions
;
1379 definition
.getProperty
= &Struct_getProperty
;
1380 definition
.setProperty
= &Struct_setProperty
;
1381 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1382 definition
.finalize
= &CYFinalize
;
1383 Struct_
= JSClassCreate(&definition
);
1385 definition
= kJSClassDefinitionEmpty
;
1386 definition
.className
= "Type";
1387 definition
.staticValues
= Type_staticValues
;
1388 definition
.staticFunctions
= Type_staticFunctions
;
1389 definition
.callAsFunction
= &Type_callAsFunction
;
1390 definition
.callAsConstructor
= &Type_callAsConstructor
;
1391 definition
.finalize
= &CYFinalize
;
1392 Type_privateData::Class_
= JSClassCreate(&definition
);
1394 definition
= kJSClassDefinitionEmpty
;
1395 definition
.className
= "Global";
1396 //definition.getProperty = &Global_getProperty;
1397 Global_
= JSClassCreate(&definition
);
1399 Array_s
= JSStringCreateWithUTF8CString("Array");
1400 cy_s
= JSStringCreateWithUTF8CString("$cy");
1401 length_s
= JSStringCreateWithUTF8CString("length");
1402 message_s
= JSStringCreateWithUTF8CString("message");
1403 name_s
= JSStringCreateWithUTF8CString("name");
1404 pop_s
= JSStringCreateWithUTF8CString("pop");
1405 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1406 push_s
= JSStringCreateWithUTF8CString("push");
1407 splice_s
= JSStringCreateWithUTF8CString("splice");
1408 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1409 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1410 toPointer_s
= JSStringCreateWithUTF8CString("toPointer");
1411 toString_s
= JSStringCreateWithUTF8CString("toString");
1413 Result_
= JSStringCreateWithUTF8CString("_");
1415 if (hooks_
!= NULL
&& hooks_
->Initialize
!= NULL
)
1416 (*hooks_
->Initialize
)();
1419 void CYThrow(JSContextRef context
, JSValueRef value
) {
1421 throw CYJSError(context
, value
);
1424 const char *CYJSError::PoolCString(CYPool
&pool
) const {
1425 // XXX: this used to be CYPoolCString
1426 return CYPoolCCYON(pool
, context_
, value_
);
1429 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1430 // XXX: what if the context is different?
1434 JSValueRef
CYCastJSError(JSContextRef context
, const char *message
) {
1435 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString("Error")));
1437 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1439 JSValueRef
exception(NULL
);
1440 JSValueRef
value(JSObjectCallAsConstructor(context
, Error
, 1, arguments
, &exception
));
1441 CYThrow(context
, exception
);
1446 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1447 return CYCastJSError(context
, message_
);
1450 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1451 _assert(context
!= NULL
);
1456 va_start(args
, format
);
1457 // XXX: there might be a beter way to think about this
1458 const char *message(pool
.vsprintf(64, format
, args
));
1461 value_
= CYCastJSError(context
, message
);
1464 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1465 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1468 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
1469 JSValueRef
exception(NULL
);
1471 CYInitializeDynamic();
1473 JSObjectRef
global(CYGetGlobalObject(context
));
1475 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
1476 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
1478 /* Cache Globals {{{ */
1479 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
1480 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
1482 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
1483 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
1485 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
1486 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
1488 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
1489 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
1491 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
1492 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
1494 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1495 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
1497 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
1498 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
1500 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
1501 CYSetProperty(context
, cy
, CYJSString("String"), String
);
1503 JSObjectRef
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
)));
1504 CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
);
1507 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1508 CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1510 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1511 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1512 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
1514 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1515 JSObjectSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
1516 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
1518 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1519 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1521 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
1522 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
1524 JSObjectRef
alls(JSObjectCallAsConstructor(context
, Array
, 0, NULL
, &exception
));
1525 CYThrow(context
, exception
);
1526 CYSetProperty(context
, cycript
, CYJSString("alls"), alls
);
1529 JSObjectRef
last(NULL
), curr(global
);
1531 goto next
; for (JSValueRef next
;;) {
1532 if (JSValueIsNull(context
, next
))
1535 curr
= CYCastJSObject(context
, next
);
1537 next
= JSObjectGetPrototype(context
, curr
);
1540 JSObjectSetPrototype(context
, last
, all
);
1543 CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
, kJSPropertyAttributeDontEnum
);
1545 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
1546 CYSetProperty(context
, cy
, CYJSString("System"), System
);
1548 CYSetProperty(context
, global
, CYJSString("system"), System
);
1549 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
1550 //CYSetProperty(context, System, CYJSString("global"), global);
1551 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
1553 if (CYBridgeEntry
*entry
= CYBridgeHash("1dlerror", 8))
1554 entry
->cache_
= new cy::Functor(entry
->value_
, reinterpret_cast<void (*)()>(&dlerror
));
1556 if (hooks_
!= NULL
&& hooks_
->SetupContext
!= NULL
)
1557 (*hooks_
->SetupContext
)(context
);
1559 CYArrayPush(context
, alls
, cycript
);
1562 JSGlobalContextRef
CYGetJSContext() {
1563 CYInitializeDynamic();
1565 static JSGlobalContextRef context_
;
1567 if (context_
== NULL
) {
1568 context_
= JSGlobalContextCreate(Global_
);
1569 CYSetupContext(context_
);