1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2014 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. 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 std::vector
<CYHook
*> hooks_
;
55 CYRegisterHook::CYRegisterHook(CYHook
*hook
) {
56 hooks_
.push_back(hook
);
59 /* JavaScript Properties {{{ */
60 bool CYHasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
61 return JSObjectHasProperty(context
, object
, name
);
64 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) {
65 return _jsccall(JSObjectGetPropertyAtIndex
, context
, object
, index
);
68 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
69 return _jsccall(JSObjectGetProperty
, context
, object
, name
);
72 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) {
73 _jsccall(JSObjectSetPropertyAtIndex
, context
, object
, index
, value
);
76 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) {
77 _jsccall(JSObjectSetProperty
, context
, object
, name
, value
, attributes
);
80 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef (*callback
)(JSContextRef
, JSObjectRef
, JSObjectRef
, size_t, const JSValueRef
[], JSValueRef
*), JSPropertyAttributes attributes
) {
81 CYSetProperty(context
, object
, name
, JSObjectMakeFunctionWithCallback(context
, name
, callback
), attributes
);
84 void CYSetPrototype(JSContextRef context
, JSObjectRef object
, JSValueRef value
) {
85 JSObjectSetPrototype(context
, object
, value
);
86 _assert(JSObjectGetPrototype(context
, object
) == value
);
89 /* JavaScript Strings {{{ */
90 JSStringRef
CYCopyJSString(const char *value
) {
91 return value
== NULL
? NULL
: JSStringCreateWithUTF8CString(value
);
94 JSStringRef
CYCopyJSString(JSStringRef value
) {
95 return value
== NULL
? NULL
: JSStringRetain(value
);
98 JSStringRef
CYCopyJSString(CYUTF8String value
) {
99 // XXX: this is very wrong; it needs to convert to UTF16 and then create from there
100 return CYCopyJSString(value
.data
);
103 JSStringRef
CYCopyJSString(JSContextRef context
, JSValueRef value
) {
104 if (JSValueIsNull(context
, value
))
106 return _jsccall(JSValueToStringCopy
, context
, value
);
109 static CYUTF16String
CYCastUTF16String(JSStringRef value
) {
110 return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
));
113 CYUTF8String
CYPoolUTF8String(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
114 return CYPoolUTF8String(pool
, CYCastUTF16String(value
));
117 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
118 CYUTF8String
utf8(CYPoolUTF8String(pool
, context
, value
));
119 _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
);
123 const char *CYPoolCString(CYPool
&pool
, JSContextRef context
, JSValueRef value
) {
124 return JSValueIsNull(context
, value
) ? NULL
: CYPoolCString(pool
, context
, CYJSString(context
, value
));
127 /* Index Offsets {{{ */
128 size_t CYGetIndex(CYPool
&pool
, JSContextRef context
, JSStringRef value
) {
129 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
133 static JSClassRef All_
;
134 static JSClassRef Context_
;
136 static JSClassRef Global_
;
137 static JSClassRef Pointer_
;
138 static JSClassRef Struct_
;
143 JSStringRef length_s
;
144 JSStringRef message_s
;
147 JSStringRef prototype_s
;
149 JSStringRef splice_s
;
150 JSStringRef toCYON_s
;
151 JSStringRef toJSON_s
;
152 JSStringRef toPointer_s
;
153 JSStringRef toString_s
;
155 static JSStringRef Result_
;
157 void CYFinalize(JSObjectRef object
) {
158 CYData
*internal(reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
)));
159 _assert(internal
->count_
!= _not(unsigned));
160 if (--internal
->count_
== 0)
164 void Structor_(CYPool
&pool
, sig::Type
*&type
) {
166 type
->primitive
== sig::pointer_P
&&
167 type
->data
.data
.type
->primitive
== sig::struct_P
&&
168 type
->data
.data
.type
->name
!= NULL
&&
169 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
171 type
->primitive
= sig::typename_P
;
172 type
->data
.data
.type
= NULL
;
176 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
179 size_t length(strlen(type
->name
));
180 char keyed
[length
+ 2];
181 memcpy(keyed
+ 1, type
->name
, length
+ 1);
183 static const char *modes
= "34";
184 for (size_t i(0); i
!= 2; ++i
) {
188 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
191 sig::Parse(pool
, &type
->data
.signature
, entry
->value_
, &Structor_
);
195 sig::Signature signature
;
196 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
197 type
= signature
.elements
[0].type
;
203 JSClassRef
Type_privateData::Class_
;
208 JSGlobalContextRef context_
;
210 Context(JSGlobalContextRef context
) :
219 Type_privateData
*type_
;
222 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type
*type
) :
223 CYOwned(value
, context
, owner
),
224 type_(new(*pool_
) Type_privateData(type
)),
230 struct Struct_privateData
:
233 Type_privateData
*type_
;
235 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
236 CYOwned(NULL
, context
, owner
)
241 typedef std::map
<const char *, Type_privateData
*, CYCStringLess
> TypeMap
;
242 static TypeMap Types_
;
244 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
245 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
246 CYPool
&pool(*internal
->pool_
);
247 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
248 internal
->type_
= typical
;
251 internal
->value_
= data
;
253 size_t size(typical
->GetFFI()->size
);
254 void *copy(internal
->pool_
->malloc
<void>(size
));
255 memcpy(copy
, data
, size
);
256 internal
->value_
= copy
;
259 return JSObjectMake(context
, Struct_
, internal
);
262 static void *CYCastSymbol(const char *name
) {
263 return dlsym(RTLD_DEFAULT
, name
);
266 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
267 return JSValueMakeBoolean(context
, value
);
270 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
271 return JSValueMakeNumber(context
, value
);
274 #define CYCastJSValue_(Type_) \
275 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
276 return JSValueMakeNumber(context, static_cast<double>(value)); \
280 CYCastJSValue_(unsigned int)
281 CYCastJSValue_(long int)
282 CYCastJSValue_(long unsigned int)
283 CYCastJSValue_(long long int)
284 CYCastJSValue_(long long unsigned int)
286 JSValueRef
CYJSUndefined(JSContextRef context
) {
287 return JSValueMakeUndefined(context
);
290 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
291 return _jsccall(JSValueToNumber
, context
, value
);
294 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
295 return JSValueToBoolean(context
, value
);
298 JSValueRef
CYJSNull(JSContextRef context
) {
299 return JSValueMakeNull(context
);
302 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
303 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
306 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
307 return CYCastJSValue(context
, CYJSString(value
));
310 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
311 return _jsccall(JSValueToObject
, context
, value
);
314 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
315 return _jsccall(JSObjectCallAsFunction
, context
, function
, _this
, count
, arguments
);
318 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
319 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
322 size_t CYArrayLength(JSContextRef context
, JSObjectRef array
) {
323 return CYCastDouble(context
, CYGetProperty(context
, array
, length_s
));
326 JSValueRef
CYArrayGet(JSContextRef context
, JSObjectRef array
, size_t index
) {
327 return _jsccall(JSObjectGetPropertyAtIndex
, context
, array
, index
);
330 void CYArrayPush(JSContextRef context
, JSObjectRef array
, JSValueRef value
) {
331 JSValueRef arguments
[1];
332 arguments
[0] = value
;
333 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
334 _jsccall(JSObjectCallAsFunction
, context
, CYCastJSObject(context
, CYGetProperty(context
, Array
, push_s
)), array
, 1, arguments
);
337 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
342 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
345 return CYJSUndefined(context
);
348 static size_t Nonce_(0);
350 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
352 const char *name(pool
.strcat(CYPoolCString(pool
, context
, arguments
[0]), pool
.itoa(Nonce_
++), NULL
));
353 return CYCastJSValue(context
, name
);
356 static void (*JSSynchronousGarbageCollectForDebugging$
)(JSContextRef
);
358 void CYGarbageCollect(JSContextRef context
) {
359 (JSSynchronousGarbageCollectForDebugging$
?: &JSGarbageCollect
)(context
);
362 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
363 CYGarbageCollect(context
);
364 return CYJSUndefined(context
);
367 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> &objects
, JSValueRef
*exception
) { CYTry
{
368 switch (JSType type
= JSValueGetType(context
, value
)) {
369 case kJSTypeUndefined
:
374 return CYCastBool(context
, value
) ? "true" : "false";
376 case kJSTypeNumber
: {
377 std::ostringstream str
;
378 CYNumerify(str
, CYCastDouble(context
, value
));
379 std::string
value(str
.str());
380 return pool
.strmemdup(value
.c_str(), value
.size());
383 case kJSTypeString
: {
384 std::ostringstream str
;
385 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
386 CYStringify(str
, string
.data
, string
.size
);
387 std::string
value(str
.str());
388 return pool
.strmemdup(value
.c_str(), value
.size());
392 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
, objects
);
394 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
398 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> &objects
) {
399 return _jsccall(CYPoolCCYON
, pool
, context
, value
, objects
);
402 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> *objects
) {
404 return CYPoolCCYON(pool
, context
, value
, *objects
);
406 std::set
<void *> objects
;
407 return CYPoolCCYON(pool
, context
, value
, objects
);
411 const char *CYPoolCCYON(CYPool
&pool
, JSContextRef context
, JSObjectRef object
, std::set
<void *> &objects
) {
412 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
413 if (CYIsCallable(context
, toCYON
)) {
414 // XXX: this needs to be abstracted behind some kind of function
415 JSValueRef arguments
[1] = {CYCastJSValue(context
, static_cast<double>(reinterpret_cast<uintptr_t>(&objects
)))};
416 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 1, arguments
));
417 _assert(value
!= NULL
);
418 return CYPoolCString(pool
, context
, value
);
421 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
422 if (CYIsCallable(context
, toJSON
)) {
423 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
424 return _jsccall(CYPoolCCYON
, pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), objects
);
427 if (JSObjectIsFunction(context
, object
)) {
428 JSValueRef
toString(CYGetProperty(context
, object
, toString_s
));
429 if (CYIsCallable(context
, toString
)) {
430 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
431 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toString
, object
, 1, arguments
));
432 _assert(value
!= NULL
);
433 return CYPoolCString(pool
, context
, value
);
437 _assert(objects
.insert(object
).second
);
439 std::ostringstream str
;
443 // XXX: this is, sadly, going to leak
444 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
448 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
454 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
455 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
460 CYStringify(str
, string
.data
, string
.size
);
465 JSValueRef
value(CYGetProperty(context
, object
, name
));
466 str
<< CYPoolCCYON(pool
, context
, value
, objects
);
467 } catch (const CYException
&error
) {
472 JSPropertyNameArrayRelease(names
);
476 std::string
string(str
.str());
477 return pool
.strmemdup(string
.c_str(), string
.size());
480 std::set
<void *> *CYCastObjects(JSContextRef context
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
483 return CYCastPointer
<std::set
<void *> *>(context
, arguments
[0]);
486 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
487 std::set
<void *> *objects(CYCastObjects(context
, _this
, count
, arguments
));
488 // XXX: this is horribly inefficient
489 std::set
<void *> backup
;
494 std::ostringstream str
;
498 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
501 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
508 JSValueRef
value(CYGetProperty(context
, _this
, index
));
509 if (!JSValueIsUndefined(context
, value
))
510 str
<< CYPoolCCYON(pool
, context
, value
, *objects
);
515 } catch (const CYException
&error
) {
522 std::string
value(str
.str());
523 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
526 static JSValueRef
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
528 std::ostringstream str
;
530 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
)));
531 CYStringify(str
, string
.data
, string
.size
);
533 std::string
value(str
.str());
534 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
537 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
538 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
539 return JSObjectMake(context
, Pointer_
, internal
);
542 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const sig::Signature
&signature
) {
543 return JSObjectMake(context
, Functor_
, new cy::Functor(signature
, function
));
546 static JSObjectRef
CYMakeFunctor(JSContextRef context
, const char *symbol
, const char *encoding
, void **cache
) {
547 cy::Functor
*internal
;
549 internal
= reinterpret_cast<cy::Functor
*>(*cache
);
551 void (*function
)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol
)));
552 if (function
== NULL
)
555 internal
= new cy::Functor(encoding
, function
);
560 return JSObjectMake(context
, Functor_
, internal
);
563 static bool CYGetOffset(CYPool
&pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
564 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
567 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
568 switch (JSValueGetType(context
, value
)) {
571 case kJSTypeObject
: {
572 JSObjectRef
object((JSObjectRef
) value
);
573 if (JSValueIsObjectOfClass(context
, value
, Pointer_
)) {
574 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
575 return internal
->value_
;
577 JSValueRef
toPointer(CYGetProperty(context
, object
, toPointer_s
));
578 if (CYIsCallable(context
, toPointer
)) {
579 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
));
580 _assert(value
!= NULL
);
581 return CYCastPointer_(context
, value
);
584 double number(CYCastDouble(context
, value
));
585 if (std::isnan(number
))
586 throw CYJSError(context
, "cannot convert value to pointer");
587 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
591 void CYPoolFFI(CYPool
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
592 switch (type
->primitive
) {
594 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
597 #define CYPoolFFI_(primitive, native) \
598 case sig::primitive ## _P: \
599 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
602 CYPoolFFI_(uchar
, unsigned char)
603 CYPoolFFI_(char, char)
604 CYPoolFFI_(ushort
, unsigned short)
605 CYPoolFFI_(short, short)
606 CYPoolFFI_(ulong
, unsigned long)
607 CYPoolFFI_(long, long)
608 CYPoolFFI_(uint
, unsigned int)
610 CYPoolFFI_(ulonglong
, unsigned long long)
611 CYPoolFFI_(longlong
, long long)
612 CYPoolFFI_(float, float)
613 CYPoolFFI_(double, double)
616 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
617 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
618 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
619 ffi_type
*field(ffi
->elements
[index
]);
622 if (aggregate
== NULL
)
625 rhs
= CYGetProperty(context
, aggregate
, index
);
626 if (JSValueIsUndefined(context
, rhs
))
627 throw CYJSError(context
, "unable to extract array value");
630 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
637 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
641 _assert(pool
!= NULL
);
642 *reinterpret_cast<const char **>(data
) = CYPoolCString(*pool
, context
, value
);
645 case sig::struct_P
: {
646 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
647 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
648 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
649 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
650 ffi_type
*field(ffi
->elements
[index
]);
653 if (aggregate
== NULL
)
656 rhs
= CYGetProperty(context
, aggregate
, index
);
657 if (JSValueIsUndefined(context
, rhs
)) {
658 if (element
->name
!= NULL
)
659 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
662 if (JSValueIsUndefined(context
, rhs
)) undefined
:
663 throw CYJSError(context
, "unable to extract structure value");
667 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
677 for (CYHook
*hook
: hooks_
)
678 if (hook
->PoolFFI
!= NULL
)
679 if ((*hook
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
682 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
686 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
687 switch (type
->primitive
) {
689 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
691 #define CYFromFFI_(primitive, native) \
692 case sig::primitive ## _P: \
693 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
695 CYFromFFI_(uchar, unsigned char)
696 CYFromFFI_(char, char)
697 CYFromFFI_(ushort
, unsigned short)
698 CYFromFFI_(short, short)
699 CYFromFFI_(ulong
, unsigned long)
700 CYFromFFI_(long, long)
701 CYFromFFI_(uint
, unsigned int)
703 CYFromFFI_(ulonglong
, unsigned long long)
704 CYFromFFI_(longlong
, long long)
705 CYFromFFI_(float, float)
706 CYFromFFI_(double, double)
709 if (void *pointer
= data
)
710 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
714 if (void *pointer
= *reinterpret_cast<void **>(data
))
715 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
719 if (char *utf8
= *reinterpret_cast<char **>(data
))
720 return CYCastJSValue(context
, utf8
);
724 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
726 return CYJSUndefined(context
);
729 return CYJSNull(context
);
731 for (CYHook
*hook
: hooks_
)
732 if (hook
->FromFFI
!= NULL
)
733 if (JSValueRef value
= (*hook
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
736 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
740 void CYExecuteClosure(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
, JSValueRef (*adapter
)(JSContextRef
, size_t, JSValueRef
[], JSObjectRef
)) {
741 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
743 JSContextRef
context(internal
->context_
);
745 size_t count(internal
->cif_
.nargs
);
746 JSValueRef values
[count
];
748 for (size_t index(0); index
!= count
; ++index
)
749 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
751 JSValueRef
value(adapter(context
, count
, values
, internal
->function_
));
752 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
755 static JSValueRef
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
) {
756 return CYCallAsFunction(context
, function
, NULL
, count
, values
);
759 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
760 CYExecuteClosure(cif
, result
, arguments
, arg
, &FunctionAdapter_
);
763 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
764 // XXX: in case of exceptions this will leak
765 // XXX: in point of fact, this may /need/ to leak :(
766 Closure_privateData
*internal(new Closure_privateData(context
, function
, signature
));
768 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
770 ffi_closure
*writable(reinterpret_cast<ffi_closure
*>(ffi_closure_alloc(sizeof(ffi_closure
), &executable
)));
772 ffi_status
status(ffi_prep_closure_loc(writable
, &internal
->cif_
, callback
, internal
, executable
));
773 _assert(status
== FFI_OK
);
775 internal
->value_
= executable
;
777 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
778 NULL
, sizeof(ffi_closure
),
779 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
783 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
784 _assert(status
== FFI_OK
);
786 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
788 internal
->value_
= closure
;
794 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const sig::Signature
&signature
) {
795 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, signature
, &FunctionClosure_
));
796 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
797 // XXX: see above notes about needing to leak
798 JSValueProtect(CYGetJSContext(context
), object
);
802 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
803 return CYCastJSObject(context
, CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
));
806 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const sig::Signature
&signature
) {
807 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
809 bool function(_jsccall(JSValueIsInstanceOfConstructor
, context
, value
, Function
));
811 JSObjectRef
function(CYCastJSObject(context
, value
));
812 return CYMakeFunctor(context
, function
, signature
);
814 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
815 return CYMakeFunctor(context
, function
, signature
);
819 static bool Index_(CYPool
&pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
820 Type_privateData
*typical(internal
->type_
);
821 sig::Type
*type(typical
->type_
);
825 const char *name(CYPoolCString(pool
, context
, property
));
826 size_t length(strlen(name
));
827 double number(CYCastDouble(name
, length
));
829 size_t count(type
->data
.signature
.count
);
831 if (std::isnan(number
)) {
832 if (property
== NULL
)
835 sig::Element
*elements(type
->data
.signature
.elements
);
837 for (size_t local(0); local
!= count
; ++local
) {
838 sig::Element
*element(&elements
[local
]);
839 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
847 index
= static_cast<ssize_t
>(number
);
848 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
853 ffi_type
**elements(typical
->GetFFI()->elements
);
855 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
856 for (ssize_t
local(0); local
!= index
; ++local
)
857 base
+= elements
[local
]->size
;
862 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
864 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
866 if (JSStringIsEqual(property
, length_s
))
867 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
869 Type_privateData
*typical(internal
->type_
);
870 if (typical
->type_
== NULL
)
872 sig::Type
&type(*typical
->type_
);
875 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
877 else if (!CYGetOffset(pool
, context
, property
, offset
))
880 if (type
.primitive
== sig::function_P
)
881 return CYMakeFunctor(context
, reinterpret_cast<void (*)()>(internal
->value_
), type
.data
.signature
);
883 ffi_type
*ffi(typical
->GetFFI());
885 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
886 base
+= ffi
->size
* offset
;
888 JSObjectRef
owner(internal
->GetOwner() ?: object
);
889 return CYFromFFI(context
, &type
, ffi
, base
, false, owner
);
892 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
894 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
895 Type_privateData
*typical(internal
->type_
);
897 if (typical
->type_
== NULL
)
901 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
903 else if (!CYGetOffset(pool
, context
, property
, offset
))
906 ffi_type
*ffi(typical
->GetFFI());
908 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
909 base
+= ffi
->size
* offset
;
911 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
915 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
916 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
917 Type_privateData
*typical(internal
->type_
);
918 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
921 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
923 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
924 Type_privateData
*typical(internal
->type_
);
929 if (!Index_(pool
, context
, internal
, property
, index
, base
))
932 JSObjectRef
owner(internal
->GetOwner() ?: object
);
934 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
937 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
939 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
940 Type_privateData
*typical(internal
->type_
);
945 if (!Index_(pool
, context
, internal
, property
, index
, base
))
948 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
952 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
953 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
954 Type_privateData
*typical(internal
->type_
);
955 sig::Type
*type(typical
->type_
);
960 size_t count(type
->data
.signature
.count
);
961 sig::Element
*elements(type
->data
.signature
.elements
);
965 for (size_t index(0); index
!= count
; ++index
) {
967 name
= elements
[index
].name
;
970 sprintf(number
, "%zu", index
);
974 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
978 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
)()) {
979 if (setups
+ count
!= signature
->count
- 1)
980 throw CYJSError(context
, "incorrect number of arguments to ffi function");
982 size_t size(setups
+ count
);
984 memcpy(values
, setup
, sizeof(void *) * setups
);
986 for (size_t index(setups
); index
!= size
; ++index
) {
987 sig::Element
*element(&signature
->elements
[index
+ 1]);
988 ffi_type
*ffi(cif
->arg_types
[index
]);
990 values
[index
] = new(pool
) uint8_t[ffi
->size
];
991 CYPoolFFI(&pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
994 uint8_t value
[cif
->rtype
->size
];
996 for (CYHook
*hook
: hooks_
)
997 if (hook
->CallFunction
!= NULL
) {
998 // XXX: this only supports one hook, but it is a bad idea anyway
999 (*hook
->CallFunction
)(context
, cif
, function
, value
, values
);
1002 ffi_call(cif
, function
, value
, values
);
1005 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
1008 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1010 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1011 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
1014 JSObjectRef
CYMakeType(JSContextRef context
, const char *encoding
) {
1015 Type_privateData
*internal(new Type_privateData(encoding
));
1016 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1019 JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
1020 Type_privateData
*internal(new Type_privateData(type
));
1021 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1024 JSObjectRef
CYMakeType(JSContextRef context
, sig::Signature
*signature
) {
1031 type
.primitive
= sig::function_P
;
1032 sig::Copy(pool
, type
.data
.signature
, *signature
);
1034 return CYMakeType(context
, &type
);
1037 static bool All_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
1038 JSObjectRef
global(CYGetGlobalObject(context
));
1039 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1040 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1042 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1043 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1044 if (CYHasProperty(context
, space
, property
))
1048 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
1050 size_t length(name
.size
);
1051 char keyed
[length
+ 2];
1052 memcpy(keyed
+ 1, name
.data
, length
+ 1);
1054 static const char *modes
= "0124";
1055 for (size_t i(0); i
!= 4; ++i
) {
1056 keyed
[0] = modes
[i
];
1057 if (CYBridgeHash(keyed
, length
+ 1) != NULL
)
1064 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1065 JSObjectRef
global(CYGetGlobalObject(context
));
1066 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1067 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1069 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1070 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
1071 if (JSValueRef value
= CYGetProperty(context
, space
, property
))
1072 if (!JSValueIsUndefined(context
, value
))
1076 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
1078 size_t length(name
.size
);
1079 char keyed
[length
+ 2];
1080 memcpy(keyed
+ 1, name
.data
, length
+ 1);
1082 static const char *modes
= "0124";
1083 for (size_t i(0); i
!= 4; ++i
) {
1084 char mode(modes
[i
]);
1087 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
1090 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
);
1093 return CYMakeFunctor(context
, name
.data
, entry
->value_
, &entry
->cache_
);
1096 if (void *symbol
= CYCastSymbol(name
.data
)) {
1097 // XXX: this is horrendously inefficient
1098 sig::Signature signature
;
1099 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
1101 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1102 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
1105 // XXX: implement case 3
1107 return CYMakeType(context
, entry
->value_
);
1114 static void All_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1115 JSObjectRef
global(CYGetGlobalObject(context
));
1116 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1117 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1119 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1120 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1))) {
1121 JSPropertyNameArrayRef
subset(JSObjectCopyPropertyNames(context
, space
));
1122 for (size_t index(0), count(JSPropertyNameArrayGetCount(subset
)); index
!= count
; ++index
)
1123 JSPropertyNameAccumulatorAddName(names
, JSPropertyNameArrayGetNameAtIndex(subset
, index
));
1124 JSPropertyNameArrayRelease(subset
);
1128 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1130 throw CYJSError(context
, "incorrect number of arguments to Pointer constructor");
1134 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1135 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1137 sig::Signature signature
;
1138 sig::Parse(pool
, &signature
, type
, &Structor_
);
1140 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1143 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1145 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1147 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1148 return CYMakeType(context
, type
);
1151 static JSValueRef Type_callAsFunction_$
With(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], sig::Primitive primitive
, JSValueRef
*exception
) { CYTry
{
1152 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1160 type
.primitive
= primitive
;
1161 type
.data
.signature
.elements
= new(pool
) sig::Element
[1 + count
];
1162 type
.data
.signature
.count
= 1 + count
;
1164 type
.data
.signature
.elements
[0].name
= NULL
;
1165 type
.data
.signature
.elements
[0].type
= internal
->type_
;
1166 type
.data
.signature
.elements
[0].offset
= _not(size_t);
1168 for (size_t i(0); i
!= count
; ++i
) {
1169 sig::Element
&element(type
.data
.signature
.elements
[i
+ 1]);
1170 element
.name
= NULL
;
1171 element
.offset
= _not(size_t);
1173 JSObjectRef
object(CYCastJSObject(context
, arguments
[i
]));
1174 _assert(JSValueIsObjectOfClass(context
, object
, Type_privateData::Class_
));
1175 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1177 element
.type
= internal
->type_
;
1180 return CYMakeType(context
, &type
);
1183 static JSValueRef
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1185 throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf");
1186 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1189 size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0])));
1190 if (index
== _not(size_t))
1191 throw CYJSError(context
, "invalid array size used with Type.arrayOf");
1197 type
.primitive
= sig::array_P
;
1198 type
.data
.data
.type
= internal
->type_
;
1199 type
.data
.data
.size
= index
;
1201 return CYMakeType(context
, &type
);
1204 static JSValueRef
Type_callAsFunction_blockWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1205 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::block_P
, exception
);
1208 static JSValueRef
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1210 throw CYJSError(context
, "incorrect number of arguments to Type.constant");
1211 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1213 sig::Type
type(*internal
->type_
);
1214 type
.flags
|= JOC_TYPE_CONST
;
1215 return CYMakeType(context
, &type
);
1218 static JSValueRef
Type_callAsFunction_long(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1220 throw CYJSError(context
, "incorrect number of arguments to Type.long");
1221 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1223 sig::Type
type(*internal
->type_
);
1225 switch (type
.primitive
) {
1226 case sig::short_P
: type
.primitive
= sig::int_P
; break;
1227 case sig::int_P
: type
.primitive
= sig::long_P
; break;
1228 case sig::long_P
: type
.primitive
= sig::longlong_P
; break;
1229 default: throw CYJSError(context
, "invalid type argument to Type.long");
1232 return CYMakeType(context
, &type
);
1235 static JSValueRef
Type_callAsFunction_short(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1237 throw CYJSError(context
, "incorrect number of arguments to Type.short");
1238 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1240 sig::Type
type(*internal
->type_
);
1242 switch (type
.primitive
) {
1243 case sig::int_P
: type
.primitive
= sig::short_P
; break;
1244 case sig::long_P
: type
.primitive
= sig::int_P
; break;
1245 case sig::longlong_P
: type
.primitive
= sig::long_P
; break;
1246 default: throw CYJSError(context
, "invalid type argument to Type.short");
1249 return CYMakeType(context
, &type
);
1252 static JSValueRef
Type_callAsFunction_signed(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1254 throw CYJSError(context
, "incorrect number of arguments to Type.signed");
1255 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1257 sig::Type
type(*internal
->type_
);
1259 switch (type
.primitive
) {
1260 case sig::char_P
: case sig::uchar_P
: type
.primitive
= sig::char_P
; break;
1261 case sig::short_P
: case sig::ushort_P
: type
.primitive
= sig::short_P
; break;
1262 case sig::int_P
: case sig::uint_P
: type
.primitive
= sig::int_P
; break;
1263 case sig::long_P
: case sig::ulong_P
: type
.primitive
= sig::long_P
; break;
1264 case sig::longlong_P
: case sig::ulonglong_P
: type
.primitive
= sig::longlong_P
; break;
1265 default: throw CYJSError(context
, "invalid type argument to Type.signed");
1268 return CYMakeType(context
, &type
);
1271 static JSValueRef
Type_callAsFunction_unsigned(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1273 throw CYJSError(context
, "incorrect number of arguments to Type.unsigned");
1274 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1276 sig::Type
type(*internal
->type_
);
1278 switch (type
.primitive
) {
1279 case sig::char_P
: case sig::uchar_P
: type
.primitive
= sig::uchar_P
; break;
1280 case sig::short_P
: case sig::ushort_P
: type
.primitive
= sig::ushort_P
; break;
1281 case sig::int_P
: case sig::uint_P
: type
.primitive
= sig::uint_P
; break;
1282 case sig::long_P
: case sig::ulong_P
: type
.primitive
= sig::ulong_P
; break;
1283 case sig::longlong_P
: case sig::ulonglong_P
: type
.primitive
= sig::ulonglong_P
; break;
1284 default: throw CYJSError(context
, "invalid type argument to Type.unsigned");
1287 return CYMakeType(context
, &type
);
1290 static JSValueRef
Type_callAsFunction_functionWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1291 return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::function_P
, exception
);
1294 static JSValueRef
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1296 throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo");
1297 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1302 if (internal
->type_
->primitive
== sig::char_P
) {
1303 type
.flags
= internal
->type_
->flags
;
1304 type
.primitive
= sig::string_P
;
1305 type
.data
.data
.type
= NULL
;
1306 type
.data
.data
.size
= 0;
1309 type
.primitive
= sig::pointer_P
;
1310 type
.data
.data
.type
= internal
->type_
;
1311 type
.data
.data
.size
= 0;
1314 return CYMakeType(context
, &type
);
1317 static JSValueRef
Type_callAsFunction_withName(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1319 throw CYJSError(context
, "incorrect number of arguments to Type.withName");
1320 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1323 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1325 sig::Type
type(*internal
->type_
);
1327 return CYMakeType(context
, &type
);
1330 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1332 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1333 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1335 if (internal
->type_
->primitive
== sig::function_P
)
1336 return CYMakeFunctor(context
, arguments
[0], internal
->type_
->data
.signature
);
1338 sig::Type
*type(internal
->type_
);
1339 ffi_type
*ffi(internal
->GetFFI());
1341 uint8_t value
[ffi
->size
];
1343 CYPoolFFI(&pool
, context
, type
, ffi
, value
, arguments
[0]);
1344 return CYFromFFI(context
, type
, ffi
, value
);
1347 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1349 throw CYJSError(context
, "incorrect number of arguments to Type allocator");
1350 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1352 sig::Type
*type(internal
->type_
);
1355 if (type
->primitive
!= sig::array_P
)
1356 length
= _not(size_t);
1358 length
= type
->data
.data
.size
;
1359 type
= type
->data
.data
.type
;
1362 void *value(calloc(1, internal
->GetFFI()->size
));
1363 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1366 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1368 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1370 const char *encoding(CYPoolCString(pool
, context
, arguments
[1]));
1371 sig::Signature signature
;
1372 sig::Parse(pool
, &signature
, encoding
, &Structor_
);
1373 return CYMakeFunctor(context
, arguments
[0], signature
);
1376 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1377 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1378 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1381 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1382 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1385 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1386 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1388 sprintf(string
, "%p", internal
->value_
);
1389 return CYCastJSValue(context
, string
);
1392 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1393 std::set
<void *> *objects(CYCastObjects(context
, _this
, count
, arguments
));
1395 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1396 if (internal
->length_
!= _not(size_t)) {
1397 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array_prototype")));
1398 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1399 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1400 } else if (internal
->type_
->type_
== NULL
) pointer
: {
1402 sprintf(string
, "%p", internal
->value_
);
1403 return CYCastJSValue(context
, string
);
1405 JSValueRef
value(CYGetProperty(context
, _this
, cyi_s
));
1406 if (JSValueIsUndefined(context
, value
))
1409 return CYCastJSValue(context
, pool
.strcat("&", CYPoolCCYON(pool
, context
, value
, objects
), NULL
));
1410 } catch (const CYException
&e
) {
1415 static JSValueRef
Pointer_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1416 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
1417 return CYMakeType(context
, internal
->type_
->type_
);
1420 static JSValueRef
Functor_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1421 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1422 return CYMakeType(context
, &internal
->signature_
);
1425 static JSValueRef
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1426 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1427 return CYCastJSValue(context
, internal
->GetFFI()->alignment
);
1430 static JSValueRef
Type_getProperty_name(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1431 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1432 return CYCastJSValue(context
, internal
->type_
->name
);
1435 static JSValueRef
Type_getProperty_size(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1436 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1437 return CYCastJSValue(context
, internal
->GetFFI()->size
);
1440 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1441 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1443 const char *type(sig::Unparse(pool
, internal
->type_
));
1444 return CYCastJSValue(context
, CYJSString(type
));
1447 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1448 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1450 std::ostringstream out
;
1452 CYOutput
output(out
, options
);
1453 (new(pool
) CYEncodedType(Decode(pool
, internal
->type_
)))->Output(output
, CYNoFlags
);
1454 return CYCastJSValue(context
, CYJSString(out
.str().c_str()));
1457 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1458 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1461 static JSStaticFunction Pointer_staticFunctions
[4] = {
1462 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1463 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1464 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1468 static JSStaticValue Pointer_staticValues
[2] = {
1469 {"type", &Pointer_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1470 {NULL
, NULL
, NULL
, 0}
1473 static JSStaticFunction Struct_staticFunctions
[2] = {
1474 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1478 static JSStaticFunction Functor_staticFunctions
[4] = {
1479 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1480 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1481 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1486 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1489 static JSStaticValue Functor_staticValues
[2] = {
1490 {"type", &Functor_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1491 {NULL
, NULL
, NULL
, 0}
1495 JSStaticValue
const * const Functor::StaticValues
= Functor_staticValues
;
1498 static JSStaticValue Type_staticValues
[4] = {
1499 {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1500 {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1501 {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1502 {NULL
, NULL
, NULL
, 0}
1505 static JSStaticFunction Type_staticFunctions
[14] = {
1506 {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1507 {"blockWith", &Type_callAsFunction_blockWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1508 {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1509 {"functionWith", &Type_callAsFunction_functionWith
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1510 {"long", &Type_callAsFunction_long
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1511 {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1512 {"short", &Type_callAsFunction_short
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1513 {"signed", &Type_callAsFunction_signed
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1514 {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1515 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1516 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1517 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1518 {"unsigned", &Type_callAsFunction_unsigned
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1522 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1524 void CYSetArgs(int argc
, const char *argv
[]) {
1525 JSContextRef
context(CYGetJSContext());
1526 JSValueRef args
[argc
];
1527 for (int i(0); i
!= argc
; ++i
)
1528 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1531 if (JSObjectMakeArray$
!= NULL
)
1532 array
= _jsccall(*JSObjectMakeArray$
, context
, argc
, args
);
1534 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1535 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1536 array
= CYCastJSObject(context
, value
);
1539 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1540 CYSetProperty(context
, System
, CYJSString("args"), array
);
1543 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1544 return JSContextGetGlobalObject(context
);
1547 // XXX: this is neither exceptin safe nor even terribly sane
1548 class ExecutionHandle
{
1550 JSContextRef context_
;
1551 std::vector
<void *> handles_
;
1554 ExecutionHandle(JSContextRef context
) :
1557 handles_
.resize(hooks_
.size());
1558 for (size_t i(0); i
!= hooks_
.size(); ++i
) {
1559 CYHook
*hook(hooks_
[i
]);
1560 if (hook
->ExecuteStart
!= NULL
)
1561 handles_
[i
] = (*hook
->ExecuteStart
)(context_
);
1567 ~ExecutionHandle() {
1568 for (size_t i(hooks_
.size()); i
!= 0; --i
) {
1569 CYHook
*hook(hooks_
[i
-1]);
1570 if (hook
->ExecuteEnd
!= NULL
)
1571 (*hook
->ExecuteEnd
)(context_
, handles_
[i
-1]);
1576 const char *CYExecute(JSContextRef context
, CYPool
&pool
, CYUTF8String code
) {
1577 JSValueRef
exception(NULL
);
1579 ExecutionHandle
handle(context
);
1581 JSValueRef result
; try {
1582 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1583 } catch (const char *error
) {
1587 if (exception
!= NULL
) error
:
1588 return CYPoolCString(pool
, context
, CYJSString(context
, exception
));
1590 if (JSValueIsUndefined(context
, result
))
1593 const char *json
; try {
1594 std::set
<void *> objects
;
1595 json
= CYPoolCCYON(pool
, context
, result
, objects
, &exception
);
1596 } catch (const char *error
) {
1600 if (exception
!= NULL
)
1603 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1608 static bool initialized_
= false;
1610 void CYInitializeDynamic() {
1612 initialized_
= true;
1615 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1616 JSSynchronousGarbageCollectForDebugging$
= reinterpret_cast<void (*)(JSContextRef
)>(dlsym(RTLD_DEFAULT
, "JSSynchronousGarbageCollectForDebugging"));
1618 JSClassDefinition definition
;
1620 definition
= kJSClassDefinitionEmpty
;
1621 definition
.className
= "All";
1622 definition
.hasProperty
= &All_hasProperty
;
1623 definition
.getProperty
= &All_getProperty
;
1624 definition
.getPropertyNames
= &All_getPropertyNames
;
1625 All_
= JSClassCreate(&definition
);
1627 definition
= kJSClassDefinitionEmpty
;
1628 definition
.className
= "Context";
1629 definition
.finalize
= &CYFinalize
;
1630 Context_
= JSClassCreate(&definition
);
1632 definition
= kJSClassDefinitionEmpty
;
1633 definition
.className
= "Functor";
1634 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1635 definition
.staticValues
= Functor_staticValues
;
1636 definition
.callAsFunction
= &Functor_callAsFunction
;
1637 definition
.finalize
= &CYFinalize
;
1638 Functor_
= JSClassCreate(&definition
);
1640 definition
= kJSClassDefinitionEmpty
;
1641 definition
.className
= "Pointer";
1642 definition
.staticFunctions
= Pointer_staticFunctions
;
1643 definition
.staticValues
= Pointer_staticValues
;
1644 definition
.getProperty
= &Pointer_getProperty
;
1645 definition
.setProperty
= &Pointer_setProperty
;
1646 definition
.finalize
= &CYFinalize
;
1647 Pointer_
= JSClassCreate(&definition
);
1649 definition
= kJSClassDefinitionEmpty
;
1650 definition
.className
= "Struct";
1651 definition
.staticFunctions
= Struct_staticFunctions
;
1652 definition
.getProperty
= &Struct_getProperty
;
1653 definition
.setProperty
= &Struct_setProperty
;
1654 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1655 definition
.finalize
= &CYFinalize
;
1656 Struct_
= JSClassCreate(&definition
);
1658 definition
= kJSClassDefinitionEmpty
;
1659 definition
.className
= "Type";
1660 definition
.staticValues
= Type_staticValues
;
1661 definition
.staticFunctions
= Type_staticFunctions
;
1662 definition
.callAsFunction
= &Type_callAsFunction
;
1663 definition
.callAsConstructor
= &Type_callAsConstructor
;
1664 definition
.finalize
= &CYFinalize
;
1665 Type_privateData::Class_
= JSClassCreate(&definition
);
1667 definition
= kJSClassDefinitionEmpty
;
1668 definition
.className
= "Global";
1669 //definition.getProperty = &Global_getProperty;
1670 Global_
= JSClassCreate(&definition
);
1672 Array_s
= JSStringCreateWithUTF8CString("Array");
1673 cy_s
= JSStringCreateWithUTF8CString("$cy");
1674 cyi_s
= JSStringCreateWithUTF8CString("$cyi");
1675 length_s
= JSStringCreateWithUTF8CString("length");
1676 message_s
= JSStringCreateWithUTF8CString("message");
1677 name_s
= JSStringCreateWithUTF8CString("name");
1678 pop_s
= JSStringCreateWithUTF8CString("pop");
1679 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1680 push_s
= JSStringCreateWithUTF8CString("push");
1681 splice_s
= JSStringCreateWithUTF8CString("splice");
1682 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1683 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1684 toPointer_s
= JSStringCreateWithUTF8CString("toPointer");
1685 toString_s
= JSStringCreateWithUTF8CString("toString");
1687 Result_
= JSStringCreateWithUTF8CString("_");
1689 for (CYHook
*hook
: hooks_
)
1690 if (hook
->Initialize
!= NULL
)
1691 (*hook
->Initialize
)();
1694 void CYThrow(JSContextRef context
, JSValueRef value
) {
1696 throw CYJSError(context
, value
);
1699 const char *CYJSError::PoolCString(CYPool
&pool
) const {
1700 std::set
<void *> objects
;
1701 // XXX: this used to be CYPoolCString
1702 return CYPoolCCYON(pool
, context_
, value_
, objects
);
1705 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1706 // XXX: what if the context is different?
1710 JSValueRef
CYCastJSError(JSContextRef context
, const char *message
) {
1711 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString("Error")));
1712 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1713 return _jsccall(JSObjectCallAsConstructor
, context
, Error
, 1, arguments
);
1716 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1717 return CYCastJSError(context
, message_
);
1720 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1721 _assert(context
!= NULL
);
1726 va_start(args
, format
);
1727 // XXX: there might be a beter way to think about this
1728 const char *message(pool
.vsprintf(64, format
, args
));
1731 value_
= CYCastJSError(context
, message
);
1734 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1735 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1738 extern "C" bool CydgetMemoryParse(const uint16_t **data
, size_t *size
);
1740 void *CYMapFile(const char *path
, size_t *psize
) {
1741 int fd(_syscall_(open(path
, O_RDONLY
), 1, {ENOENT
}));
1746 _syscall(fstat(fd
, &stat
));
1747 size_t size(stat
.st_size
);
1752 _syscall(base
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, 0));
1754 _syscall(close(fd
));
1758 static JSValueRef
require(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1759 _assert(count
== 1);
1763 _assert(dladdr(reinterpret_cast<void *>(&require
), &addr
) != 0);
1764 char *lib(pool
.strdup(addr
.dli_fname
));
1766 char *slash(strrchr(lib
, '/'));
1767 _assert(slash
!= NULL
);
1770 CYJSString
property("exports");
1773 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1774 const char *path(pool
.strcat(lib
, "/cycript0.9/", name
, ".cy", NULL
));
1776 CYJSString
key(path
);
1777 JSObjectRef
modules(CYGetCachedObject(context
, CYJSString("modules")));
1778 JSValueRef
cache(CYGetProperty(context
, modules
, key
));
1780 if (!JSValueIsUndefined(context
, cache
))
1781 module = CYCastJSObject(context
, cache
);
1784 code
.data
= reinterpret_cast<char *>(CYMapFile(path
, &code
.size
));
1786 if (code
.data
== NULL
) {
1787 if (strchr(name
, '/') == NULL
&& (
1789 dlopen(pool
.strcat("/System/Library/Frameworks/", name
, ".framework/", name
, NULL
), RTLD_LAZY
| RTLD_GLOBAL
) != NULL
||
1790 dlopen(pool
.strcat("/System/Library/PrivateFrameworks/", name
, ".framework/", name
, NULL
), RTLD_LAZY
| RTLD_GLOBAL
) != NULL
||
1793 return CYJSUndefined(NULL
);
1795 CYThrow("Can't find module: %s", name
);
1798 module = JSObjectMake(context
, NULL
, NULL
);
1799 CYSetProperty(context
, modules
, key
, module);
1801 JSObjectRef
exports(JSObjectMake(context
, NULL
, NULL
));
1802 CYSetProperty(context
, module, property
, exports
);
1804 std::stringstream wrap
;
1805 wrap
<< "(function (exports, require, module) { " << code
<< "\n});";
1806 code
= CYPoolCode(pool
, wrap
);
1808 JSValueRef
value(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0));
1809 JSObjectRef
function(CYCastJSObject(context
, value
));
1811 JSValueRef arguments
[3] = { exports
, JSObjectMakeFunctionWithCallback(context
, CYJSString("require"), &require
), module };
1812 CYCallAsFunction(context
, function
, NULL
, 3, arguments
);
1815 return CYGetProperty(context
, module, property
);
1818 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
1819 CYInitializeDynamic();
1821 JSObjectRef
global(CYGetGlobalObject(context
));
1823 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
1824 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
1826 /* Cache Globals {{{ */
1827 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
1828 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
1830 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
1831 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
1833 JSObjectRef
Boolean(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Boolean"))));
1834 CYSetProperty(context
, cy
, CYJSString("Boolean"), Boolean
);
1836 JSObjectRef
Boolean_prototype(CYCastJSObject(context
, CYGetProperty(context
, Boolean
, prototype_s
)));
1837 CYSetProperty(context
, cy
, CYJSString("Boolean_prototype"), Boolean_prototype
);
1839 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
1840 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
1842 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
1843 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
1845 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
1846 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
1848 JSObjectRef
Number(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Number"))));
1849 CYSetProperty(context
, cy
, CYJSString("Number"), Number
);
1851 JSObjectRef
Number_prototype(CYCastJSObject(context
, CYGetProperty(context
, Number
, prototype_s
)));
1852 CYSetProperty(context
, cy
, CYJSString("Number_prototype"), Number_prototype
);
1854 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1855 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
1857 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
1858 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
1860 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
1861 CYSetProperty(context
, cy
, CYJSString("String"), String
);
1863 JSObjectRef
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
)));
1864 CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
);
1867 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1868 CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1870 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1871 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1872 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
1874 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1875 CYSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
1876 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
1878 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1879 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1881 JSObjectRef
modules(JSObjectMake(context
, NULL
, NULL
));
1882 CYSetProperty(context
, cy
, CYJSString("modules"), modules
);
1884 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
1885 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
1887 JSObjectRef
alls(_jsccall(JSObjectCallAsConstructor
, context
, Array
, 0, NULL
));
1888 CYSetProperty(context
, cycript
, CYJSString("alls"), alls
);
1891 JSObjectRef
last(NULL
), curr(global
);
1893 goto next
; for (JSValueRef next
;;) {
1894 if (JSValueIsNull(context
, next
))
1897 curr
= CYCastJSObject(context
, next
);
1899 next
= JSObjectGetPrototype(context
, curr
);
1902 CYSetPrototype(context
, last
, all
);
1905 CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
, kJSPropertyAttributeDontEnum
);
1907 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
1908 CYSetProperty(context
, cy
, CYJSString("System"), System
);
1910 CYSetProperty(context
, all
, CYJSString("require"), &require
, kJSPropertyAttributeDontEnum
);
1912 CYSetProperty(context
, global
, CYJSString("system"), System
);
1913 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
1914 //CYSetProperty(context, System, CYJSString("global"), global);
1915 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
1917 if (CYBridgeEntry
*entry
= CYBridgeHash("1dlerror", 8))
1918 entry
->cache_
= new cy::Functor(entry
->value_
, reinterpret_cast<void (*)()>(&dlerror
));
1920 for (CYHook
*hook
: hooks_
)
1921 if (hook
->SetupContext
!= NULL
)
1922 (*hook
->SetupContext
)(context
);
1924 CYArrayPush(context
, alls
, cycript
);
1927 static JSGlobalContextRef context_
;
1929 JSGlobalContextRef
CYGetJSContext() {
1930 CYInitializeDynamic();
1932 if (context_
== NULL
) {
1933 context_
= JSGlobalContextCreate(Global_
);
1934 CYSetupContext(context_
);
1940 void CYDestroyContext() {
1941 if (context_
== NULL
)
1943 JSGlobalContextRelease(context_
);