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"
29 #include "cycript.hpp"
31 #include "sig/parse.hpp"
32 #include "sig/ffi_type.hpp"
34 #include "Pooling.hpp"
35 #include "Execute.hpp"
50 #include "JavaScript.hpp"
53 struct CYHooks
*hooks_
;
55 /* JavaScript Properties {{{ */
56 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) {
57 return _jsccall(JSObjectGetPropertyAtIndex
, context
, object
, index
);
60 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
61 return _jsccall(JSObjectGetProperty
, context
, object
, name
);
64 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) {
65 _jsccall(JSObjectSetPropertyAtIndex
, context
, object
, index
, value
);
68 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) {
69 _jsccall(JSObjectSetProperty
, context
, object
, name
, value
, attributes
);
72 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef (*callback
)(JSContextRef
, JSObjectRef
, JSObjectRef
, size_t, const JSValueRef
[], JSValueRef
*), JSPropertyAttributes attributes
) {
73 CYSetProperty(context
, object
, name
, JSObjectMakeFunctionWithCallback(context
, name
, callback
), attributes
);
76 /* JavaScript Strings {{{ */
77 JSStringRef
CYCopyJSString(const char *value
) {
78 return value
== NULL
? NULL
: JSStringCreateWithUTF8CString(value
);
81 JSStringRef
CYCopyJSString(JSStringRef value
) {
82 return value
== NULL
? NULL
: JSStringRetain(value
);
85 JSStringRef
CYCopyJSString(CYUTF8String value
) {
86 // XXX: this is very wrong; it needs to convert to UTF16 and then create from there
87 return CYCopyJSString(value
.data
);
90 JSStringRef
CYCopyJSString(JSContextRef context
, JSValueRef value
) {
91 if (JSValueIsNull(context
, value
))
93 return _jsccall(JSValueToStringCopy
, context
, value
);
96 static CYUTF16String
CYCastUTF16String(JSStringRef value
) {
97 return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
));
100 CYUTF8String
CYPoolUTF8String(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
101 return CYPoolUTF8String(pool
, CYCastUTF16String(value
));
104 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
105 CYUTF8String
utf8(CYPoolUTF8String(pool
, context
, value
));
106 _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
);
110 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
111 return JSValueIsNull(context
, value
) ? NULL
: CYPoolCString(pool
, context
, CYJSString(context
, value
));
114 /* Index Offsets {{{ */
115 size_t CYGetIndex(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
116 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
120 static JSClassRef All_
;
121 static JSClassRef Context_
;
123 static JSClassRef Global_
;
124 static JSClassRef Pointer_
;
125 static JSClassRef Struct_
;
129 JSStringRef length_s
;
130 JSStringRef message_s
;
133 JSStringRef prototype_s
;
135 JSStringRef splice_s
;
136 JSStringRef toCYON_s
;
137 JSStringRef toJSON_s
;
138 JSStringRef toPointer_s
;
139 JSStringRef toString_s
;
141 static JSStringRef Result_
;
143 void CYFinalize(JSObjectRef object
) {
144 CYData
*internal(reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
)));
145 _assert(internal
->count_
!= _not(unsigned));
146 if (--internal
->count_
== 0)
150 void Structor_(CYPool
&pool
, sig::Type
*&type
) {
152 type
->primitive
== sig::pointer_P
&&
153 type
->data
.data
.type
!= NULL
&&
154 type
->data
.data
.type
->primitive
== sig::struct_P
&&
155 type
->data
.data
.type
->name
!= NULL
&&
156 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
158 type
->primitive
= sig::typename_P
;
159 type
->data
.data
.type
= NULL
;
163 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
166 size_t length(strlen(type
->name
));
167 char keyed
[length
+ 2];
168 memcpy(keyed
+ 1, type
->name
, length
+ 1);
170 static const char *modes
= "34";
171 for (size_t i(0); i
!= 2; ++i
) {
175 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
178 sig::Parse(pool
, &type
->data
.signature
, entry
->value_
, &Structor_
);
182 sig::Signature signature
;
183 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
184 type
= signature
.elements
[0].type
;
190 JSClassRef
Type_privateData::Class_
;
195 JSGlobalContextRef context_
;
197 Context(JSGlobalContextRef context
) :
206 Type_privateData
*type_
;
209 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type
*type
) :
210 CYOwned(value
, context
, owner
),
211 type_(new(*pool_
) Type_privateData(type
)),
217 struct Struct_privateData
:
220 Type_privateData
*type_
;
222 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
223 CYOwned(NULL
, context
, owner
)
228 typedef std::map
<const char *, Type_privateData
*, CYCStringLess
> TypeMap
;
229 static TypeMap Types_
;
231 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
232 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
233 CYPool
&pool(*internal
->pool_
);
234 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
235 internal
->type_
= typical
;
238 internal
->value_
= data
;
240 size_t size(typical
->GetFFI()->size
);
241 void *copy(internal
->pool_
->malloc
<void>(size
));
242 memcpy(copy
, data
, size
);
243 internal
->value_
= copy
;
246 return JSObjectMake(context
, Struct_
, internal
);
249 static void *CYCastSymbol(const char *name
) {
250 return dlsym(RTLD_DEFAULT
, name
);
253 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
254 return JSValueMakeBoolean(context
, value
);
257 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
258 return JSValueMakeNumber(context
, value
);
261 #define CYCastJSValue_(Type_) \
262 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
263 return JSValueMakeNumber(context, static_cast<double>(value)); \
267 CYCastJSValue_(unsigned int)
268 CYCastJSValue_(long int)
269 CYCastJSValue_(long unsigned int)
270 CYCastJSValue_(long long int)
271 CYCastJSValue_(long long unsigned int)
273 JSValueRef
CYJSUndefined(JSContextRef context
) {
274 return JSValueMakeUndefined(context
);
277 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
278 return _jsccall(JSValueToNumber
, context
, value
);
281 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
282 return JSValueToBoolean(context
, value
);
285 JSValueRef
CYJSNull(JSContextRef context
) {
286 return JSValueMakeNull(context
);
289 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
290 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
293 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
294 return CYCastJSValue(context
, CYJSString(value
));
297 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
298 return _jsccall(JSValueToObject
, context
, value
);
301 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
302 return _jsccall(JSObjectCallAsFunction
, context
, function
, _this
, count
, arguments
);
305 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
306 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
309 size_t CYArrayLength(JSContextRef context
, JSObjectRef array
) {
310 return CYCastDouble(context
, CYGetProperty(context
, array
, length_s
));
313 JSValueRef
CYArrayGet(JSContextRef context
, JSObjectRef array
, size_t index
) {
314 return _jsccall(JSObjectGetPropertyAtIndex
, context
, array
, index
);
317 void CYArrayPush(JSContextRef context
, JSObjectRef array
, JSValueRef value
) {
318 JSValueRef arguments
[1];
319 arguments
[0] = value
;
320 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
321 _jsccall(JSObjectCallAsFunction
, context
, CYCastJSObject(context
, CYGetProperty(context
, Array
, push_s
)), array
, 1, arguments
);
324 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
329 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
332 return CYJSUndefined(context
);
335 static size_t Nonce_(0);
337 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
339 const char *name(pool
.strcat(CYPoolCString(pool
, context
, arguments
[0]), pool
.itoa(Nonce_
++), NULL
));
340 return CYCastJSValue(context
, name
);
343 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
344 JSGarbageCollect(context
);
345 return CYJSUndefined(context
);
348 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
349 switch (JSType type
= JSValueGetType(context
, value
)) {
350 case kJSTypeUndefined
:
355 return CYCastBool(context
, value
) ? "true" : "false";
357 case kJSTypeNumber
: {
358 std::ostringstream str
;
359 CYNumerify(str
, CYCastDouble(context
, value
));
360 std::string
value(str
.str());
361 return pool
.strmemdup(value
.c_str(), value
.size());
364 case kJSTypeString
: {
365 std::ostringstream str
;
366 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
367 CYStringify(str
, string
.data
, string
.size
);
368 std::string
value(str
.str());
369 return pool
.strmemdup(value
.c_str(), value
.size());
373 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
375 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
379 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
380 return _jsccall(CYPoolCCYON
, pool
, context
, value
);
383 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSObjectRef object
) {
384 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
385 if (CYIsCallable(context
, toCYON
)) {
386 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
387 _assert(value
!= NULL
);
388 return CYPoolCString(pool
, context
, value
);
391 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
392 if (CYIsCallable(context
, toJSON
)) {
393 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
394 return _jsccall(CYPoolCCYON
, pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
));
397 if (JSObjectIsFunction(context
, object
)) {
398 JSValueRef
toString(CYGetProperty(context
, object
, toString_s
));
399 if (CYIsCallable(context
, toString
)) {
400 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
401 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toString
, object
, 1, arguments
));
402 _assert(value
!= NULL
);
403 return CYPoolCString(pool
, context
, value
);
407 std::ostringstream str
;
411 // XXX: this is, sadly, going to leak
412 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
416 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
422 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
423 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
428 CYStringify(str
, string
.data
, string
.size
);
433 JSValueRef
value(CYGetProperty(context
, object
, name
));
434 str
<< CYPoolCCYON(pool
, context
, value
);
435 } catch (const CYException
&error
) {
440 JSPropertyNameArrayRelease(names
);
444 std::string
string(str
.str());
445 return pool
.strmemdup(string
.c_str(), string
.size());
448 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
450 std::ostringstream str
;
454 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
457 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
464 JSValueRef
value(CYGetProperty(context
, _this
, index
));
465 if (!JSValueIsUndefined(context
, value
))
466 str
<< CYPoolCCYON(pool
, context
, value
);
471 } catch (const CYException
&error
) {
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 bool function(_jsccall(JSValueIsInstanceOfConstructor
, context
, value
, Function
));
765 JSObjectRef
function(CYCastJSObject(context
, value
));
766 return CYMakeFunctor(context
, function
, type
);
768 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
769 return CYMakeFunctor(context
, function
, type
);
773 static bool Index_(CYPool
&pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
774 Type_privateData
*typical(internal
->type_
);
775 sig::Type
*type(typical
->type_
);
779 const char *name(CYPoolCString(pool
, context
, property
));
780 size_t length(strlen(name
));
781 double number(CYCastDouble(name
, length
));
783 size_t count(type
->data
.signature
.count
);
785 if (std::isnan(number
)) {
786 if (property
== NULL
)
789 sig::Element
*elements(type
->data
.signature
.elements
);
791 for (size_t local(0); local
!= count
; ++local
) {
792 sig::Element
*element(&elements
[local
]);
793 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
801 index
= static_cast<ssize_t
>(number
);
802 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
807 ffi_type
**elements(typical
->GetFFI()->elements
);
809 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
810 for (ssize_t
local(0); local
!= index
; ++local
)
811 base
+= elements
[local
]->size
;
816 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
818 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
820 if (JSStringIsEqual(property
, length_s
))
821 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
823 Type_privateData
*typical(internal
->type_
);
825 if (typical
->type_
== NULL
)
829 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
831 else if (!CYGetOffset(pool
, context
, property
, offset
))
834 ffi_type
*ffi(typical
->GetFFI());
836 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
837 base
+= ffi
->size
* offset
;
839 JSObjectRef
owner(internal
->GetOwner() ?: object
);
840 return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
);
843 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
845 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
846 Type_privateData
*typical(internal
->type_
);
848 if (typical
->type_
== NULL
)
852 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
854 else if (!CYGetOffset(pool
, context
, property
, offset
))
857 ffi_type
*ffi(typical
->GetFFI());
859 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
860 base
+= ffi
->size
* offset
;
862 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
866 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
867 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
868 Type_privateData
*typical(internal
->type_
);
869 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
872 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
874 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
875 Type_privateData
*typical(internal
->type_
);
880 if (!Index_(pool
, context
, internal
, property
, index
, base
))
883 JSObjectRef
owner(internal
->GetOwner() ?: object
);
885 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
888 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
890 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
891 Type_privateData
*typical(internal
->type_
);
896 if (!Index_(pool
, context
, internal
, property
, index
, base
))
899 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
903 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
904 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
905 Type_privateData
*typical(internal
->type_
);
906 sig::Type
*type(typical
->type_
);
911 size_t count(type
->data
.signature
.count
);
912 sig::Element
*elements(type
->data
.signature
.elements
);
916 for (size_t index(0); index
!= count
; ++index
) {
918 name
= elements
[index
].name
;
921 sprintf(number
, "%zu", index
);
925 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
929 JSValueRef
CYCallFunction(CYPool
&pool
, JSContextRef context
, size_t setups
, void *setup
[], size_t count
, const JSValueRef arguments
[], bool initialize
, sig::Signature
*signature
, ffi_cif
*cif
, void (*function
)()) {
930 if (setups
+ count
!= signature
->count
- 1)
931 throw CYJSError(context
, "incorrect number of arguments to ffi function");
933 size_t size(setups
+ count
);
935 memcpy(values
, setup
, sizeof(void *) * setups
);
937 for (size_t index(setups
); index
!= size
; ++index
) {
938 sig::Element
*element(&signature
->elements
[index
+ 1]);
939 ffi_type
*ffi(cif
->arg_types
[index
]);
941 values
[index
] = new(pool
) uint8_t[ffi
->size
];
942 CYPoolFFI(&pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
945 uint8_t value
[cif
->rtype
->size
];
947 if (hooks_
!= NULL
&& hooks_
->CallFunction
!= NULL
)
948 (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
);
950 ffi_call(cif
, function
, value
, values
);
952 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
955 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
957 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
958 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
961 JSObjectRef
CYMakeType(JSContextRef context
, const char *type
) {
962 Type_privateData
*internal(new Type_privateData(type
));
963 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
966 JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
967 Type_privateData
*internal(new Type_privateData(type
));
968 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
971 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
972 JSObjectRef
global(CYGetGlobalObject(context
));
973 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
974 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
976 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
977 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
978 if (JSValueRef value
= CYGetProperty(context
, space
, property
))
979 if (!JSValueIsUndefined(context
, value
))
983 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
985 size_t length(name
.size
);
986 char keyed
[length
+ 2];
987 memcpy(keyed
+ 1, name
.data
, length
+ 1);
989 static const char *modes
= "0124";
990 for (size_t i(0); i
!= 4; ++i
) {
994 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
997 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
);
1000 return CYMakeFunctor(context
, name
.data
, entry
->value_
, &entry
->cache_
);
1003 if (void *symbol
= CYCastSymbol(name
.data
)) {
1004 // XXX: this is horrendously inefficient
1005 sig::Signature signature
;
1006 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
1008 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1009 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
1012 // XXX: implement case 3
1014 return CYMakeType(context
, entry
->value_
);
1021 static void All_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1022 JSObjectRef
global(CYGetGlobalObject(context
));
1023 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1024 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1026 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1027 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1))) {
1028 JSPropertyNameArrayRef
subset(JSObjectCopyPropertyNames(context
, space
));
1029 for (size_t index(0), count(JSPropertyNameArrayGetCount(subset
)); index
!= count
; ++index
)
1030 JSPropertyNameAccumulatorAddName(names
, JSPropertyNameArrayGetNameAtIndex(subset
, index
));
1031 JSPropertyNameArrayRelease(subset
);
1035 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1037 throw CYJSError(context
, "incorrect number of arguments to Pointer constructor");
1041 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1042 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1044 sig::Signature signature
;
1045 sig::Parse(pool
, &signature
, type
, &Structor_
);
1047 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1050 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1052 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1054 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1055 return CYMakeType(context
, type
);
1058 static JSValueRef
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1060 throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf");
1061 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1064 size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0])));
1065 if (index
== _not(size_t))
1066 throw CYJSError(context
, "invalid array size used with Type.arrayOf");
1072 type
.primitive
= sig::array_P
;
1073 type
.data
.data
.type
= internal
->type_
;
1074 type
.data
.data
.size
= index
;
1076 return CYMakeType(context
, &type
);
1079 static JSValueRef
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1081 throw CYJSError(context
, "incorrect number of arguments to Type.constant");
1082 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1084 sig::Type
type(*internal
->type_
);
1085 type
.flags
|= JOC_TYPE_CONST
;
1086 return CYMakeType(context
, &type
);
1089 static JSValueRef
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1091 throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo");
1092 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1098 type
.primitive
= sig::pointer_P
;
1099 type
.data
.data
.type
= internal
->type_
;
1100 type
.data
.data
.size
= 0;
1102 return CYMakeType(context
, &type
);
1105 static JSValueRef
Type_callAsFunction_withName(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1107 throw CYJSError(context
, "incorrect number of arguments to Type.withName");
1108 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1111 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1113 sig::Type
type(*internal
->type_
);
1115 return CYMakeType(context
, &type
);
1118 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1120 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1121 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1123 sig::Type
*type(internal
->type_
);
1124 ffi_type
*ffi(internal
->GetFFI());
1126 uint8_t value
[ffi
->size
];
1128 CYPoolFFI(&pool
, context
, type
, ffi
, value
, arguments
[0]);
1129 return CYFromFFI(context
, type
, ffi
, value
);
1132 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1134 throw CYJSError(context
, "incorrect number of arguments to Type allocator");
1135 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1137 sig::Type
*type(internal
->type_
);
1140 if (type
->primitive
!= sig::array_P
)
1141 length
= _not(size_t);
1143 length
= type
->data
.data
.size
;
1144 type
= type
->data
.data
.type
;
1147 void *value(malloc(internal
->GetFFI()->size
));
1148 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1151 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1153 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1155 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1156 return CYMakeFunctor(context
, arguments
[0], type
);
1159 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1160 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1161 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1164 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1165 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1168 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1169 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1171 sprintf(string
, "%p", internal
->value_
);
1172 return CYCastJSValue(context
, string
);
1175 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1176 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1177 if (internal
->length_
!= _not(size_t)) {
1178 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
1179 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1180 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1183 sprintf(string
, "%p", internal
->value_
);
1184 return CYCastJSValue(context
, string
);
1188 static JSValueRef
Functor_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1189 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1191 return CYCastJSValue(context
, Unparse(pool
, &internal
->signature_
));
1194 static JSValueRef
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
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
) { CYTry
{
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
) { CYTry
{
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 Functor_staticValues
[2] = {
1254 {"type", &Functor_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1255 {NULL
, NULL
, NULL
, 0}
1258 static JSStaticValue Type_staticValues
[4] = {
1259 {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1260 {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1261 {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1262 {NULL
, NULL
, NULL
, 0}
1265 static JSStaticFunction Type_staticFunctions
[8] = {
1266 {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1267 {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1268 {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1269 {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1270 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1271 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1272 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1276 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1278 void CYSetArgs(int argc
, const char *argv
[]) {
1279 JSContextRef
context(CYGetJSContext());
1280 JSValueRef args
[argc
];
1281 for (int i(0); i
!= argc
; ++i
)
1282 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1285 if (JSObjectMakeArray$
!= NULL
)
1286 array
= _jsccall(*JSObjectMakeArray$
, context
, argc
, args
);
1288 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1289 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1290 array
= CYCastJSObject(context
, value
);
1293 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1294 CYSetProperty(context
, System
, CYJSString("args"), array
);
1297 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1298 return JSContextGetGlobalObject(context
);
1301 const char *CYExecute(JSContextRef context
, CYPool
&pool
, CYUTF8String code
) {
1302 JSValueRef
exception(NULL
);
1305 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1306 handle
= (*hooks_
->ExecuteStart
)(context
);
1314 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1315 } catch (const char *error
) {
1319 if (exception
!= NULL
) error
:
1320 return CYPoolCString(pool
, context
, CYJSString(context
, exception
));
1322 if (JSValueIsUndefined(context
, result
))
1327 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1328 } catch (const char *error
) {
1332 if (exception
!= NULL
)
1335 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1340 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1341 (*hooks_
->ExecuteEnd
)(context
, handle
);
1346 extern "C" void CydgetSetupContext(JSGlobalContextRef context
) {
1347 CYSetupContext(context
);
1350 static bool initialized_
= false;
1352 void CYInitializeDynamic() {
1354 initialized_
= true;
1357 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1359 JSClassDefinition definition
;
1361 definition
= kJSClassDefinitionEmpty
;
1362 definition
.className
= "All";
1363 definition
.getProperty
= &All_getProperty
;
1364 definition
.getPropertyNames
= &All_getPropertyNames
;
1365 All_
= JSClassCreate(&definition
);
1367 definition
= kJSClassDefinitionEmpty
;
1368 definition
.className
= "Context";
1369 definition
.finalize
= &CYFinalize
;
1370 Context_
= JSClassCreate(&definition
);
1372 definition
= kJSClassDefinitionEmpty
;
1373 definition
.className
= "Functor";
1374 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1375 definition
.staticValues
= Functor_staticValues
;
1376 definition
.callAsFunction
= &Functor_callAsFunction
;
1377 definition
.finalize
= &CYFinalize
;
1378 Functor_
= JSClassCreate(&definition
);
1380 definition
= kJSClassDefinitionEmpty
;
1381 definition
.className
= "Pointer";
1382 definition
.staticFunctions
= Pointer_staticFunctions
;
1383 definition
.getProperty
= &Pointer_getProperty
;
1384 definition
.setProperty
= &Pointer_setProperty
;
1385 definition
.finalize
= &CYFinalize
;
1386 Pointer_
= JSClassCreate(&definition
);
1388 definition
= kJSClassDefinitionEmpty
;
1389 definition
.className
= "Struct";
1390 definition
.staticFunctions
= Struct_staticFunctions
;
1391 definition
.getProperty
= &Struct_getProperty
;
1392 definition
.setProperty
= &Struct_setProperty
;
1393 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1394 definition
.finalize
= &CYFinalize
;
1395 Struct_
= JSClassCreate(&definition
);
1397 definition
= kJSClassDefinitionEmpty
;
1398 definition
.className
= "Type";
1399 definition
.staticValues
= Type_staticValues
;
1400 definition
.staticFunctions
= Type_staticFunctions
;
1401 definition
.callAsFunction
= &Type_callAsFunction
;
1402 definition
.callAsConstructor
= &Type_callAsConstructor
;
1403 definition
.finalize
= &CYFinalize
;
1404 Type_privateData::Class_
= JSClassCreate(&definition
);
1406 definition
= kJSClassDefinitionEmpty
;
1407 definition
.className
= "Global";
1408 //definition.getProperty = &Global_getProperty;
1409 Global_
= JSClassCreate(&definition
);
1411 Array_s
= JSStringCreateWithUTF8CString("Array");
1412 cy_s
= JSStringCreateWithUTF8CString("$cy");
1413 length_s
= JSStringCreateWithUTF8CString("length");
1414 message_s
= JSStringCreateWithUTF8CString("message");
1415 name_s
= JSStringCreateWithUTF8CString("name");
1416 pop_s
= JSStringCreateWithUTF8CString("pop");
1417 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1418 push_s
= JSStringCreateWithUTF8CString("push");
1419 splice_s
= JSStringCreateWithUTF8CString("splice");
1420 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1421 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1422 toPointer_s
= JSStringCreateWithUTF8CString("toPointer");
1423 toString_s
= JSStringCreateWithUTF8CString("toString");
1425 Result_
= JSStringCreateWithUTF8CString("_");
1427 if (hooks_
!= NULL
&& hooks_
->Initialize
!= NULL
)
1428 (*hooks_
->Initialize
)();
1431 void CYThrow(JSContextRef context
, JSValueRef value
) {
1433 throw CYJSError(context
, value
);
1436 const char *CYJSError::PoolCString(CYPool
&pool
) const {
1437 // XXX: this used to be CYPoolCString
1438 return CYPoolCCYON(pool
, context_
, value_
);
1441 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1442 // XXX: what if the context is different?
1446 JSValueRef
CYCastJSError(JSContextRef context
, const char *message
) {
1447 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString("Error")));
1448 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1449 return _jsccall(JSObjectCallAsConstructor
, context
, Error
, 1, arguments
);
1452 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1453 return CYCastJSError(context
, message_
);
1456 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1457 _assert(context
!= NULL
);
1462 va_start(args
, format
);
1463 // XXX: there might be a beter way to think about this
1464 const char *message(pool
.vsprintf(64, format
, args
));
1467 value_
= CYCastJSError(context
, message
);
1470 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1471 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1474 extern "C" bool CydgetMemoryParse(const uint16_t **data
, size_t *size
);
1476 void *CYMapFile(const char *path
, size_t *psize
) {
1478 _syscall(fd
= open(path
, O_RDONLY
));
1481 _syscall(fstat(fd
, &stat
));
1482 size_t size(stat
.st_size
);
1487 _syscall(base
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, 0));
1489 _syscall(close(fd
));
1493 static void CYRunSetups(JSContextRef context
) {
1494 std::string
folder("/etc/cycript/setup.d");
1495 DIR *setups(opendir(folder
.c_str()));
1502 _syscall(readdir_r(setups
, &setup
, &result
));
1506 _assert(result
== &setup
);
1508 const char *name(setup
.d_name
);
1509 size_t length(strlen(name
));
1515 if (memcmp(name
+ length
- 3, ".cy", 3) != 0)
1518 std::string
script(folder
+ "/" + name
);
1520 utf8
.data
= reinterpret_cast<char *>(CYMapFile(script
.c_str(), &utf8
.size
));
1523 CYUTF16String
utf16(CYPoolUTF16String(pool
, utf8
));
1524 munmap(const_cast<char *>(utf8
.data
), utf8
.size
);
1526 if (CydgetMemoryParse(&utf16
.data
, &utf16
.size
))
1527 CYExecute(context
, pool
, CYPoolUTF8String(pool
, utf16
));
1528 free(const_cast<uint16_t *>(utf16
.data
));
1531 _syscall(closedir(setups
));
1534 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
1535 CYInitializeDynamic();
1537 JSObjectRef
global(CYGetGlobalObject(context
));
1539 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
1540 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
1542 /* Cache Globals {{{ */
1543 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
1544 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
1546 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
1547 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
1549 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
1550 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
1552 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
1553 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
1555 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
1556 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
1558 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1559 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
1561 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
1562 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
1564 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
1565 CYSetProperty(context
, cy
, CYJSString("String"), String
);
1567 JSObjectRef
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
)));
1568 CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
);
1571 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1572 CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1574 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1575 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1576 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
1578 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1579 JSObjectSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
1580 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
1582 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1583 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1585 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
1586 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
1588 JSObjectRef
alls(_jsccall(JSObjectCallAsConstructor
, context
, Array
, 0, NULL
));
1589 CYSetProperty(context
, cycript
, CYJSString("alls"), alls
);
1592 JSObjectRef
last(NULL
), curr(global
);
1594 goto next
; for (JSValueRef next
;;) {
1595 if (JSValueIsNull(context
, next
))
1598 curr
= CYCastJSObject(context
, next
);
1600 next
= JSObjectGetPrototype(context
, curr
);
1603 JSObjectSetPrototype(context
, last
, all
);
1606 CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
, kJSPropertyAttributeDontEnum
);
1608 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
1609 CYSetProperty(context
, cy
, CYJSString("System"), System
);
1611 CYSetProperty(context
, global
, CYJSString("system"), System
);
1612 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
1613 //CYSetProperty(context, System, CYJSString("global"), global);
1614 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
1616 if (CYBridgeEntry
*entry
= CYBridgeHash("1dlerror", 8))
1617 entry
->cache_
= new cy::Functor(entry
->value_
, reinterpret_cast<void (*)()>(&dlerror
));
1619 if (hooks_
!= NULL
&& hooks_
->SetupContext
!= NULL
)
1620 (*hooks_
->SetupContext
)(context
);
1622 CYArrayPush(context
, alls
, cycript
);
1624 CYRunSetups(context
);
1627 JSGlobalContextRef
CYGetJSContext() {
1628 CYInitializeDynamic();
1630 static JSGlobalContextRef context_
;
1632 if (context_
== NULL
) {
1633 context_
= JSGlobalContextCreate(Global_
);
1634 CYSetupContext(context_
);