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" 
  51 #include "JavaScript.hpp" 
  54 struct CYHooks 
*hooks_
; 
  56 /* JavaScript Properties {{{ */ 
  57 JSValueRef 
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) { 
  58     return _jsccall(JSObjectGetPropertyAtIndex
, context
, object
, index
); 
  61 JSValueRef 
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) { 
  62     return _jsccall(JSObjectGetProperty
, context
, object
, name
); 
  65 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) { 
  66     _jsccall(JSObjectSetPropertyAtIndex
, context
, object
, index
, value
); 
  69 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) { 
  70     _jsccall(JSObjectSetProperty
, context
, object
, name
, value
, attributes
); 
  73 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef (*callback
)(JSContextRef
, JSObjectRef
, JSObjectRef
, size_t, const JSValueRef
[], JSValueRef 
*), JSPropertyAttributes attributes
) { 
  74     CYSetProperty(context
, object
, name
, JSObjectMakeFunctionWithCallback(context
, name
, callback
), attributes
); 
  77 /* JavaScript Strings {{{ */ 
  78 JSStringRef 
CYCopyJSString(const char *value
) { 
  79     return value 
== NULL 
? NULL 
: JSStringCreateWithUTF8CString(value
); 
  82 JSStringRef 
CYCopyJSString(JSStringRef value
) { 
  83     return value 
== NULL 
? NULL 
: JSStringRetain(value
); 
  86 JSStringRef 
CYCopyJSString(CYUTF8String value
) { 
  87     // XXX: this is very wrong; it needs to convert to UTF16 and then create from there 
  88     return CYCopyJSString(value
.data
); 
  91 JSStringRef 
CYCopyJSString(JSContextRef context
, JSValueRef value
) { 
  92     if (JSValueIsNull(context
, value
)) 
  94     return _jsccall(JSValueToStringCopy
, context
, value
); 
  97 static CYUTF16String 
CYCastUTF16String(JSStringRef value
) { 
  98     return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
)); 
 101 CYUTF8String 
CYPoolUTF8String(CYPool 
&pool
, JSContextRef context
, JSStringRef value
) { 
 102     return CYPoolUTF8String(pool
, CYCastUTF16String(value
)); 
 105 const char *CYPoolCString(CYPool 
&pool
, JSContextRef context
, JSStringRef value
) { 
 106     CYUTF8String 
utf8(CYPoolUTF8String(pool
, context
, value
)); 
 107     _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
); 
 111 const char *CYPoolCString(CYPool 
&pool
, JSContextRef context
, JSValueRef value
) { 
 112     return JSValueIsNull(context
, value
) ? NULL 
: CYPoolCString(pool
, context
, CYJSString(context
, value
)); 
 115 /* Index Offsets {{{ */ 
 116 size_t CYGetIndex(CYPool 
&pool
, JSContextRef context
, JSStringRef value
) { 
 117     return CYGetIndex(CYPoolUTF8String(pool
, context
, value
)); 
 121 static JSClassRef All_
; 
 122 static JSClassRef Context_
; 
 124 static JSClassRef Global_
; 
 125 static JSClassRef Pointer_
; 
 126 static JSClassRef Struct_
; 
 130 JSStringRef length_s
; 
 131 JSStringRef message_s
; 
 134 JSStringRef prototype_s
; 
 136 JSStringRef splice_s
; 
 137 JSStringRef toCYON_s
; 
 138 JSStringRef toJSON_s
; 
 139 JSStringRef toPointer_s
; 
 140 JSStringRef toString_s
; 
 142 static JSStringRef Result_
; 
 144 void CYFinalize(JSObjectRef object
) { 
 145     CYData 
*internal(reinterpret_cast<CYData 
*>(JSObjectGetPrivate(object
))); 
 146     _assert(internal
->count_ 
!= _not(unsigned)); 
 147     if (--internal
->count_ 
== 0) 
 151 void Structor_(CYPool 
&pool
, sig::Type 
*&type
) { 
 153         type
->primitive 
== sig::pointer_P 
&& 
 154         type
->data
.data
.type 
!= NULL 
&& 
 155         type
->data
.data
.type
->primitive 
== sig::struct_P 
&& 
 156         type
->data
.data
.type
->name 
!= NULL 
&& 
 157         strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0 
 159         type
->primitive 
= sig::typename_P
; 
 160         type
->data
.data
.type 
= NULL
; 
 164     if (type
->primitive 
!= sig::struct_P 
|| type
->name 
== NULL
) 
 167     size_t length(strlen(type
->name
)); 
 168     char keyed
[length 
+ 2]; 
 169     memcpy(keyed 
+ 1, type
->name
, length 
+ 1); 
 171     static const char *modes 
= "34"; 
 172     for (size_t i(0); i 
!= 2; ++i
) { 
 176         if (CYBridgeEntry 
*entry 
= CYBridgeHash(keyed
, length 
+ 1)) 
 179                     sig::Parse(pool
, &type
->data
.signature
, entry
->value_
, &Structor_
); 
 183                     sig::Signature signature
; 
 184                     sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
); 
 185                     type 
= signature
.elements
[0].type
; 
 191 JSClassRef 
Type_privateData::Class_
; 
 196     JSGlobalContextRef context_
; 
 198     Context(JSGlobalContextRef context
) : 
 207     Type_privateData 
*type_
; 
 210     Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type 
*type
) : 
 211         CYOwned(value
, context
, owner
), 
 212         type_(new(*pool_
) Type_privateData(type
)), 
 218 struct Struct_privateData 
: 
 221     Type_privateData 
*type_
; 
 223     Struct_privateData(JSContextRef context
, JSObjectRef owner
) : 
 224         CYOwned(NULL
, context
, owner
) 
 229 typedef std::map
<const char *, Type_privateData 
*, CYCStringLess
> TypeMap
; 
 230 static TypeMap Types_
; 
 232 JSObjectRef 
CYMakeStruct(JSContextRef context
, void *data
, sig::Type 
*type
, ffi_type 
*ffi
, JSObjectRef owner
) { 
 233     Struct_privateData 
*internal(new Struct_privateData(context
, owner
)); 
 234     CYPool 
&pool(*internal
->pool_
); 
 235     Type_privateData 
*typical(new(pool
) Type_privateData(type
, ffi
)); 
 236     internal
->type_ 
= typical
; 
 239         internal
->value_ 
= data
; 
 241         size_t size(typical
->GetFFI()->size
); 
 242         void *copy(internal
->pool_
->malloc
<void>(size
)); 
 243         memcpy(copy
, data
, size
); 
 244         internal
->value_ 
= copy
; 
 247     return JSObjectMake(context
, Struct_
, internal
); 
 250 static void *CYCastSymbol(const char *name
) { 
 251     return dlsym(RTLD_DEFAULT
, name
); 
 254 JSValueRef 
CYCastJSValue(JSContextRef context
, bool value
) { 
 255     return JSValueMakeBoolean(context
, value
); 
 258 JSValueRef 
CYCastJSValue(JSContextRef context
, double value
) { 
 259     return JSValueMakeNumber(context
, value
); 
 262 #define CYCastJSValue_(Type_) \ 
 263     JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \ 
 264         return JSValueMakeNumber(context, static_cast<double>(value)); \ 
 268 CYCastJSValue_(unsigned int) 
 269 CYCastJSValue_(long int) 
 270 CYCastJSValue_(long unsigned int) 
 271 CYCastJSValue_(long long int) 
 272 CYCastJSValue_(long long unsigned int) 
 274 JSValueRef 
CYJSUndefined(JSContextRef context
) { 
 275     return JSValueMakeUndefined(context
); 
 278 double CYCastDouble(JSContextRef context
, JSValueRef value
) { 
 279     return _jsccall(JSValueToNumber
, context
, value
); 
 282 bool CYCastBool(JSContextRef context
, JSValueRef value
) { 
 283     return JSValueToBoolean(context
, value
); 
 286 JSValueRef 
CYJSNull(JSContextRef context
) { 
 287     return JSValueMakeNull(context
); 
 290 JSValueRef 
CYCastJSValue(JSContextRef context
, JSStringRef value
) { 
 291     return value 
== NULL 
? CYJSNull(context
) : JSValueMakeString(context
, value
); 
 294 JSValueRef 
CYCastJSValue(JSContextRef context
, const char *value
) { 
 295     return CYCastJSValue(context
, CYJSString(value
)); 
 298 JSObjectRef 
CYCastJSObject(JSContextRef context
, JSValueRef value
) { 
 299     return _jsccall(JSValueToObject
, context
, value
); 
 302 JSValueRef 
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) { 
 303     return _jsccall(JSObjectCallAsFunction
, context
, function
, _this
, count
, arguments
); 
 306 bool CYIsCallable(JSContextRef context
, JSValueRef value
) { 
 307     return value 
!= NULL 
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
); 
 310 size_t CYArrayLength(JSContextRef context
, JSObjectRef array
) { 
 311     return CYCastDouble(context
, CYGetProperty(context
, array
, length_s
)); 
 314 JSValueRef 
CYArrayGet(JSContextRef context
, JSObjectRef array
, size_t index
) { 
 315     return _jsccall(JSObjectGetPropertyAtIndex
, context
, array
, index
); 
 318 void CYArrayPush(JSContextRef context
, JSObjectRef array
, JSValueRef value
) { 
 319     JSValueRef arguments
[1]; 
 320     arguments
[0] = value
; 
 321     JSObjectRef 
Array(CYGetCachedObject(context
, CYJSString("Array_prototype"))); 
 322     _jsccall(JSObjectCallAsFunction
, context
, CYCastJSObject(context
, CYGetProperty(context
, Array
, push_s
)), array
, 1, arguments
); 
 325 static JSValueRef 
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 330         printf("%s\n", CYPoolCString(pool
, context
, arguments
[0])); 
 333     return CYJSUndefined(context
); 
 336 static size_t Nonce_(0); 
 338 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 340     const char *name(pool
.strcat(CYPoolCString(pool
, context
, arguments
[0]), pool
.itoa(Nonce_
++), NULL
)); 
 341     return CYCastJSValue(context
, name
); 
 344 static JSValueRef 
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 345     JSGarbageCollect(context
); 
 346     return CYJSUndefined(context
); 
 349 const char *CYPoolCCYON(CYPool 
&pool
, JSContextRef context
, JSValueRef value
, JSValueRef 
*exception
) { CYTry 
{ 
 350     switch (JSType type 
= JSValueGetType(context
, value
)) { 
 351         case kJSTypeUndefined
: 
 356             return CYCastBool(context
, value
) ? "true" : "false"; 
 358         case kJSTypeNumber
: { 
 359             std::ostringstream str
; 
 360             CYNumerify(str
, CYCastDouble(context
, value
)); 
 361             std::string 
value(str
.str()); 
 362             return pool
.strmemdup(value
.c_str(), value
.size()); 
 365         case kJSTypeString
: { 
 366             std::ostringstream str
; 
 367             CYUTF8String 
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
))); 
 368             CYStringify(str
, string
.data
, string
.size
); 
 369             std::string 
value(str
.str()); 
 370             return pool
.strmemdup(value
.c_str(), value
.size()); 
 374             return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
); 
 376             throw CYJSError(context
, "JSValueGetType() == 0x%x", type
); 
 380 const char *CYPoolCCYON(CYPool 
&pool
, JSContextRef context
, JSValueRef value
) { 
 381     return _jsccall(CYPoolCCYON
, pool
, context
, value
); 
 384 const char *CYPoolCCYON(CYPool 
&pool
, JSContextRef context
, JSObjectRef object
) { 
 385     JSValueRef 
toCYON(CYGetProperty(context
, object
, toCYON_s
)); 
 386     if (CYIsCallable(context
, toCYON
)) { 
 387         JSValueRef 
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
)); 
 388         _assert(value 
!= NULL
); 
 389         return CYPoolCString(pool
, context
, value
); 
 392     JSValueRef 
toJSON(CYGetProperty(context
, object
, toJSON_s
)); 
 393     if (CYIsCallable(context
, toJSON
)) { 
 394         JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))}; 
 395         return _jsccall(CYPoolCCYON
, pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
)); 
 398     if (JSObjectIsFunction(context
, object
)) { 
 399         JSValueRef 
toString(CYGetProperty(context
, object
, toString_s
)); 
 400         if (CYIsCallable(context
, toString
)) { 
 401             JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))}; 
 402             JSValueRef 
value(CYCallAsFunction(context
, (JSObjectRef
) toString
, object
, 1, arguments
)); 
 403             _assert(value 
!= NULL
); 
 404             return CYPoolCString(pool
, context
, value
); 
 408     std::ostringstream str
; 
 412     // XXX: this is, sadly, going to leak 
 413     JSPropertyNameArrayRef 
names(JSObjectCopyPropertyNames(context
, object
)); 
 417     for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index 
!= count
; ++index
) { 
 423         JSStringRef 
name(JSPropertyNameArrayGetNameAtIndex(names
, index
)); 
 424         CYUTF8String 
string(CYPoolUTF8String(pool
, context
, name
)); 
 429             CYStringify(str
, string
.data
, string
.size
); 
 434             JSValueRef 
value(CYGetProperty(context
, object
, name
)); 
 435             str 
<< CYPoolCCYON(pool
, context
, value
); 
 436         } catch (const CYException 
&error
) { 
 441     JSPropertyNameArrayRelease(names
); 
 445     std::string 
string(str
.str()); 
 446     return pool
.strmemdup(string
.c_str(), string
.size()); 
 449 static JSValueRef 
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 451     std::ostringstream str
; 
 455     JSValueRef 
length(CYGetProperty(context
, _this
, length_s
)); 
 458     for (size_t index(0), count(CYCastDouble(context
, length
)); index 
!= count
; ++index
) { 
 465             JSValueRef 
value(CYGetProperty(context
, _this
, index
)); 
 466             if (!JSValueIsUndefined(context
, value
)) 
 467                 str 
<< CYPoolCCYON(pool
, context
, value
); 
 472         } catch (const CYException 
&error
) { 
 479     std::string 
value(str
.str()); 
 480     return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size()))); 
 483 static JSValueRef 
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 485     std::ostringstream str
; 
 487     CYUTF8String 
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
))); 
 488     CYStringify(str
, string
.data
, string
.size
); 
 490     std::string 
value(str
.str()); 
 491     return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size()))); 
 494 JSObjectRef 
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type 
*type
, ffi_type 
*ffi
, JSObjectRef owner
) { 
 495     Pointer 
*internal(new Pointer(pointer
, context
, owner
, length
, type
)); 
 496     return JSObjectMake(context
, Pointer_
, internal
); 
 499 static JSObjectRef 
CYMakeFunctor(JSContextRef context
, void (*function
)(), const sig::Signature 
&signature
) { 
 500     return JSObjectMake(context
, Functor_
, new cy::Functor(signature
, function
)); 
 503 static JSObjectRef 
CYMakeFunctor(JSContextRef context
, const char *symbol
, const char *encoding
, void **cache
) { 
 504     cy::Functor 
*internal
; 
 506         internal 
= reinterpret_cast<cy::Functor 
*>(*cache
); 
 508         void (*function
)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol
))); 
 509         if (function 
== NULL
) 
 512         internal 
= new cy::Functor(encoding
, function
); 
 517     return JSObjectMake(context
, Functor_
, internal
); 
 520 static bool CYGetOffset(CYPool 
&pool
, JSContextRef context
, JSStringRef value
, ssize_t 
&index
) { 
 521     return CYGetOffset(CYPoolCString(pool
, context
, value
), index
); 
 524 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) { 
 525     switch (JSValueGetType(context
, value
)) { 
 528         case kJSTypeObject
: { 
 529             JSObjectRef 
object((JSObjectRef
) value
); 
 530             if (JSValueIsObjectOfClass(context
, value
, Pointer_
)) { 
 531                 Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(object
))); 
 532                 return internal
->value_
; 
 534             JSValueRef 
toPointer(CYGetProperty(context
, object
, toPointer_s
)); 
 535             if (CYIsCallable(context
, toPointer
)) { 
 536                 JSValueRef 
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
)); 
 537                 _assert(value 
!= NULL
); 
 538                 return CYCastPointer_(context
, value
); 
 541             double number(CYCastDouble(context
, value
)); 
 542             if (std::isnan(number
)) 
 543                 throw CYJSError(context
, "cannot convert value to pointer"); 
 544             return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
))); 
 548 void CYPoolFFI(CYPool 
*pool
, JSContextRef context
, sig::Type 
*type
, ffi_type 
*ffi
, void *data
, JSValueRef value
) { 
 549     switch (type
->primitive
) { 
 551             *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
); 
 554 #define CYPoolFFI_(primitive, native) \ 
 555         case sig::primitive ## _P: \ 
 556             *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \ 
 559         CYPoolFFI_(uchar
, unsigned char) 
 560         CYPoolFFI_(char, char) 
 561         CYPoolFFI_(ushort
, unsigned short) 
 562         CYPoolFFI_(short, short) 
 563         CYPoolFFI_(ulong
, unsigned long) 
 564         CYPoolFFI_(long, long) 
 565         CYPoolFFI_(uint
, unsigned int) 
 567         CYPoolFFI_(ulonglong
, unsigned long long) 
 568         CYPoolFFI_(longlong
, long long) 
 569         CYPoolFFI_(float, float) 
 570         CYPoolFFI_(double, double) 
 573             uint8_t *base(reinterpret_cast<uint8_t *>(data
)); 
 574             JSObjectRef 
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value 
: NULL
); 
 575             for (size_t index(0); index 
!= type
->data
.data
.size
; ++index
) { 
 576                 ffi_type 
*field(ffi
->elements
[index
]); 
 579                 if (aggregate 
== NULL
) 
 582                     rhs 
= CYGetProperty(context
, aggregate
, index
); 
 583                     if (JSValueIsUndefined(context
, rhs
)) 
 584                         throw CYJSError(context
, "unable to extract array value"); 
 587                 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
); 
 594             *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
); 
 598             _assert(pool 
!= NULL
); 
 599             *reinterpret_cast<const char **>(data
) = CYPoolCString(*pool
, context
, value
); 
 602         case sig::struct_P
: { 
 603             uint8_t *base(reinterpret_cast<uint8_t *>(data
)); 
 604             JSObjectRef 
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value 
: NULL
); 
 605             for (size_t index(0); index 
!= type
->data
.signature
.count
; ++index
) { 
 606                 sig::Element 
*element(&type
->data
.signature
.elements
[index
]); 
 607                 ffi_type 
*field(ffi
->elements
[index
]); 
 610                 if (aggregate 
== NULL
) 
 613                     rhs 
= CYGetProperty(context
, aggregate
, index
); 
 614                     if (JSValueIsUndefined(context
, rhs
)) { 
 615                         if (element
->name 
!= NULL
) 
 616                             rhs 
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
)); 
 619                         if (JSValueIsUndefined(context
, rhs
)) undefined
: 
 620                             throw CYJSError(context
, "unable to extract structure value"); 
 624                 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
); 
 634             if (hooks_ 
!= NULL 
&& hooks_
->PoolFFI 
!= NULL
) 
 635                 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
)) 
 638             CYThrow("unimplemented signature code: '%c''\n", type
->primitive
); 
 642 JSValueRef 
CYFromFFI(JSContextRef context
, sig::Type 
*type
, ffi_type 
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) { 
 643     switch (type
->primitive
) { 
 645             return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
)); 
 647 #define CYFromFFI_(primitive, native) \ 
 648         case sig::primitive ## _P: \ 
 649             return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \ 
 651         CYFromFFI_(uchar, unsigned char) 
 652         CYFromFFI_(char, char) 
 653         CYFromFFI_(ushort
, unsigned short) 
 654         CYFromFFI_(short, short) 
 655         CYFromFFI_(ulong
, unsigned long) 
 656         CYFromFFI_(long, long) 
 657         CYFromFFI_(uint
, unsigned int) 
 659         CYFromFFI_(ulonglong
, unsigned long long) 
 660         CYFromFFI_(longlong
, long long) 
 661         CYFromFFI_(float, float) 
 662         CYFromFFI_(double, double) 
 665             if (void *pointer 
= data
) 
 666                 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
); 
 670             if (void *pointer 
= *reinterpret_cast<void **>(data
)) 
 671                 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
); 
 675             if (char *utf8 
= *reinterpret_cast<char **>(data
)) 
 676                 return CYCastJSValue(context
, utf8
); 
 680             return CYMakeStruct(context
, data
, type
, ffi
, owner
); 
 682             return CYJSUndefined(context
); 
 685             return CYJSNull(context
); 
 687             if (hooks_ 
!= NULL 
&& hooks_
->FromFFI 
!= NULL
) 
 688                 if (JSValueRef value 
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
)) 
 691             CYThrow("unimplemented signature code: '%c''\n", type
->primitive
); 
 695 void CYExecuteClosure(ffi_cif 
*cif
, void *result
, void **arguments
, void *arg
, JSValueRef (*adapter
)(JSContextRef
, size_t, JSValueRef
[], JSObjectRef
)) { 
 696     Closure_privateData 
*internal(reinterpret_cast<Closure_privateData 
*>(arg
)); 
 698     JSContextRef 
context(internal
->context_
); 
 700     size_t count(internal
->cif_
.nargs
); 
 701     JSValueRef values
[count
]; 
 703     for (size_t index(0); index 
!= count
; ++index
) 
 704         values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]); 
 706     JSValueRef 
value(adapter(context
, count
, values
, internal
->function_
)); 
 707     CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
); 
 710 static JSValueRef 
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
) { 
 711     return CYCallAsFunction(context
, function
, NULL
, count
, values
); 
 714 static void FunctionClosure_(ffi_cif 
*cif
, void *result
, void **arguments
, void *arg
) { 
 715     CYExecuteClosure(cif
, result
, arguments
, arg
, &FunctionAdapter_
); 
 718 Closure_privateData 
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const sig::Signature 
&signature
, void (*callback
)(ffi_cif 
*, void *, void **, void *)) { 
 719     // XXX: in case of exceptions this will leak 
 720     // XXX: in point of fact, this may /need/ to leak :( 
 721     Closure_privateData 
*internal(new Closure_privateData(context
, function
, signature
)); 
 723 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__)) 
 725     ffi_closure 
*writable(reinterpret_cast<ffi_closure 
*>(ffi_closure_alloc(sizeof(ffi_closure
), &executable
))); 
 727     ffi_status 
status(ffi_prep_closure_loc(writable
, &internal
->cif_
, callback
, internal
, executable
)); 
 728     _assert(status 
== FFI_OK
); 
 730     internal
->value_ 
= executable
; 
 732     ffi_closure 
*closure((ffi_closure 
*) _syscall(mmap( 
 733         NULL
, sizeof(ffi_closure
), 
 734         PROT_READ 
| PROT_WRITE
, MAP_ANON 
| MAP_PRIVATE
, 
 738     ffi_status 
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
)); 
 739     _assert(status 
== FFI_OK
); 
 741     _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ 
| PROT_EXEC
)); 
 743     internal
->value_ 
= closure
; 
 749 static JSObjectRef 
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const sig::Signature 
&signature
) { 
 750     Closure_privateData 
*internal(CYMakeFunctor_(context
, function
, signature
, &FunctionClosure_
)); 
 751     JSObjectRef 
object(JSObjectMake(context
, Functor_
, internal
)); 
 752     // XXX: see above notes about needing to leak 
 753     JSValueProtect(CYGetJSContext(context
), object
); 
 757 JSObjectRef 
CYGetCachedObject(JSContextRef context
, JSStringRef name
) { 
 758     return CYCastJSObject(context
, CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
)); 
 761 static JSObjectRef 
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const sig::Signature 
&signature
) { 
 762     JSObjectRef 
Function(CYGetCachedObject(context
, CYJSString("Function"))); 
 764     bool function(_jsccall(JSValueIsInstanceOfConstructor
, context
, value
, Function
)); 
 766         JSObjectRef 
function(CYCastJSObject(context
, value
)); 
 767         return CYMakeFunctor(context
, function
, signature
); 
 769         void (*function
)()(CYCastPointer
<void (*)()>(context
, value
)); 
 770         return CYMakeFunctor(context
, function
, signature
); 
 774 static bool Index_(CYPool 
&pool
, JSContextRef context
, Struct_privateData 
*internal
, JSStringRef property
, ssize_t 
&index
, uint8_t *&base
) { 
 775     Type_privateData 
*typical(internal
->type_
); 
 776     sig::Type 
*type(typical
->type_
); 
 780     const char *name(CYPoolCString(pool
, context
, property
)); 
 781     size_t length(strlen(name
)); 
 782     double number(CYCastDouble(name
, length
)); 
 784     size_t count(type
->data
.signature
.count
); 
 786     if (std::isnan(number
)) { 
 787         if (property 
== NULL
) 
 790         sig::Element 
*elements(type
->data
.signature
.elements
); 
 792         for (size_t local(0); local 
!= count
; ++local
) { 
 793             sig::Element 
*element(&elements
[local
]); 
 794             if (element
->name 
!= NULL 
&& strcmp(name
, element
->name
) == 0) { 
 802         index 
= static_cast<ssize_t
>(number
); 
 803         if (index 
!= number 
|| index 
< 0 || static_cast<size_t>(index
) >= count
) 
 808     ffi_type 
**elements(typical
->GetFFI()->elements
); 
 810     base 
= reinterpret_cast<uint8_t *>(internal
->value_
); 
 811     for (ssize_t 
local(0); local 
!= index
; ++local
) 
 812         base 
+= elements
[local
]->size
; 
 817 static JSValueRef 
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
 819     Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(object
))); 
 821     if (JSStringIsEqual(property
, length_s
)) 
 822         return internal
->length_ 
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
); 
 824     Type_privateData 
*typical(internal
->type_
); 
 826     if (typical
->type_ 
== NULL
) 
 830     if (JSStringIsEqualToUTF8CString(property
, "$cyi")) 
 832     else if (!CYGetOffset(pool
, context
, property
, offset
)) 
 835     ffi_type 
*ffi(typical
->GetFFI()); 
 837     uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
)); 
 838     base 
+= ffi
->size 
* offset
; 
 840     JSObjectRef 
owner(internal
->GetOwner() ?: object
); 
 841     return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
); 
 844 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef 
*exception
) { CYTry 
{ 
 846     Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(object
))); 
 847     Type_privateData 
*typical(internal
->type_
); 
 849     if (typical
->type_ 
== NULL
) 
 853     if (JSStringIsEqualToUTF8CString(property
, "$cyi")) 
 855     else if (!CYGetOffset(pool
, context
, property
, offset
)) 
 858     ffi_type 
*ffi(typical
->GetFFI()); 
 860     uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
)); 
 861     base 
+= ffi
->size 
* offset
; 
 863     CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
); 
 867 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 868     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(_this
))); 
 869     Type_privateData 
*typical(internal
->type_
); 
 870     return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
); 
 873 static JSValueRef 
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
 875     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(object
))); 
 876     Type_privateData 
*typical(internal
->type_
); 
 881     if (!Index_(pool
, context
, internal
, property
, index
, base
)) 
 884     JSObjectRef 
owner(internal
->GetOwner() ?: object
); 
 886     return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
); 
 889 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef 
*exception
) { CYTry 
{ 
 891     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(object
))); 
 892     Type_privateData 
*typical(internal
->type_
); 
 897     if (!Index_(pool
, context
, internal
, property
, index
, base
)) 
 900     CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
); 
 904 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) { 
 905     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(object
))); 
 906     Type_privateData 
*typical(internal
->type_
); 
 907     sig::Type 
*type(typical
->type_
); 
 912     size_t count(type
->data
.signature
.count
); 
 913     sig::Element 
*elements(type
->data
.signature
.elements
); 
 917     for (size_t index(0); index 
!= count
; ++index
) { 
 919         name 
= elements
[index
].name
; 
 922             sprintf(number
, "%zu", index
); 
 926         JSPropertyNameAccumulatorAddName(names
, CYJSString(name
)); 
 930 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
)()) { 
 931     if (setups 
+ count 
!= signature
->count 
- 1) 
 932         throw CYJSError(context
, "incorrect number of arguments to ffi function"); 
 934     size_t size(setups 
+ count
); 
 936     memcpy(values
, setup
, sizeof(void *) * setups
); 
 938     for (size_t index(setups
); index 
!= size
; ++index
) { 
 939         sig::Element 
*element(&signature
->elements
[index 
+ 1]); 
 940         ffi_type 
*ffi(cif
->arg_types
[index
]); 
 942         values
[index
] = new(pool
) uint8_t[ffi
->size
]; 
 943         CYPoolFFI(&pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index 
- setups
]); 
 946     uint8_t value
[cif
->rtype
->size
]; 
 948     if (hooks_ 
!= NULL 
&& hooks_
->CallFunction 
!= NULL
) 
 949         (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
); 
 951         ffi_call(cif
, function
, value
, values
); 
 953     return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
); 
 956 static JSValueRef 
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 958     cy::Functor 
*internal(reinterpret_cast<cy::Functor 
*>(JSObjectGetPrivate(object
))); 
 959     return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, &internal
->signature_
, &internal
->cif_
, internal
->GetValue()); 
 962 JSObjectRef 
CYMakeType(JSContextRef context
, const char *encoding
) { 
 963     Type_privateData 
*internal(new Type_privateData(encoding
)); 
 964     return JSObjectMake(context
, Type_privateData::Class_
, internal
); 
 967 JSObjectRef 
CYMakeType(JSContextRef context
, sig::Type 
*type
) { 
 968     Type_privateData 
*internal(new Type_privateData(type
)); 
 969     return JSObjectMake(context
, Type_privateData::Class_
, internal
); 
 972 JSObjectRef 
CYMakeType(JSContextRef context
, sig::Signature 
*signature
) { 
 979     type
.primitive 
= sig::function_P
; 
 980     sig::Copy(pool
, type
.data
.signature
, *signature
); 
 982     return CYMakeType(context
, &type
); 
 985 static JSValueRef 
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
 986     JSObjectRef 
global(CYGetGlobalObject(context
)); 
 987     JSObjectRef 
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript")))); 
 988     JSObjectRef 
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls")))); 
 990     for (size_t i(0), count(CYArrayLength(context
, alls
)); i 
!= count
; ++i
) 
 991         if (JSObjectRef space 
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count 
- i 
- 1))) 
 992             if (JSValueRef value 
= CYGetProperty(context
, space
, property
)) 
 993                 if (!JSValueIsUndefined(context
, value
)) 
 997     CYUTF8String 
name(CYPoolUTF8String(pool
, context
, property
)); 
 999     size_t length(name
.size
); 
1000     char keyed
[length 
+ 2]; 
1001     memcpy(keyed 
+ 1, name
.data
, length 
+ 1); 
1003     static const char *modes 
= "0124"; 
1004     for (size_t i(0); i 
!= 4; ++i
) { 
1005         char mode(modes
[i
]); 
1008         if (CYBridgeEntry 
*entry 
= CYBridgeHash(keyed
, length 
+ 1)) 
1011                     return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
); 
1014                     return CYMakeFunctor(context
, name
.data
, entry
->value_
, &entry
->cache_
); 
1017                     if (void *symbol 
= CYCastSymbol(name
.data
)) { 
1018                         // XXX: this is horrendously inefficient 
1019                         sig::Signature signature
; 
1020                         sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
); 
1022                         sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
); 
1023                         return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
); 
1026                 // XXX: implement case 3 
1028                     return CYMakeType(context
, entry
->value_
); 
1035 static void All_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) { 
1036     JSObjectRef 
global(CYGetGlobalObject(context
)); 
1037     JSObjectRef 
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript")))); 
1038     JSObjectRef 
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls")))); 
1040     for (size_t i(0), count(CYArrayLength(context
, alls
)); i 
!= count
; ++i
) 
1041         if (JSObjectRef space 
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count 
- i 
- 1))) { 
1042             JSPropertyNameArrayRef 
subset(JSObjectCopyPropertyNames(context
, space
)); 
1043             for (size_t index(0), count(JSPropertyNameArrayGetCount(subset
)); index 
!= count
; ++index
) 
1044                 JSPropertyNameAccumulatorAddName(names
, JSPropertyNameArrayGetNameAtIndex(subset
, index
)); 
1045             JSPropertyNameArrayRelease(subset
); 
1049 static JSObjectRef 
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1051         throw CYJSError(context
, "incorrect number of arguments to Pointer constructor"); 
1055     void *value(CYCastPointer
<void *>(context
, arguments
[0])); 
1056     const char *type(CYPoolCString(pool
, context
, arguments
[1])); 
1058     sig::Signature signature
; 
1059     sig::Parse(pool
, &signature
, type
, &Structor_
); 
1061     return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
); 
1064 static JSObjectRef 
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1066         throw CYJSError(context
, "incorrect number of arguments to Type constructor"); 
1068     const char *type(CYPoolCString(pool
, context
, arguments
[0])); 
1069     return CYMakeType(context
, type
); 
1072 static JSValueRef 
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1074         throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf"); 
1075     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1078     size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0]))); 
1079     if (index 
== _not(size_t)) 
1080         throw CYJSError(context
, "invalid array size used with Type.arrayOf"); 
1086     type
.primitive 
= sig::array_P
; 
1087     type
.data
.data
.type 
= internal
->type_
; 
1088     type
.data
.data
.size 
= index
; 
1090     return CYMakeType(context
, &type
); 
1093 static JSValueRef 
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1095         throw CYJSError(context
, "incorrect number of arguments to Type.constant"); 
1096     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1098     sig::Type 
type(*internal
->type_
); 
1099     type
.flags 
|= JOC_TYPE_CONST
; 
1100     return CYMakeType(context
, &type
); 
1103 static JSValueRef 
Type_callAsFunction_functionWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1104     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1112     type
.primitive 
= sig::function_P
; 
1113     type
.data
.signature
.elements 
= new(pool
) sig::Element
[1 + count
]; 
1114     type
.data
.signature
.count 
= 1 + count
; 
1116     type
.data
.signature
.elements
[0].name 
= NULL
; 
1117     type
.data
.signature
.elements
[0].type 
= internal
->type_
; 
1118     type
.data
.signature
.elements
[0].offset 
= _not(size_t); 
1120     for (size_t i(0); i 
!= count
; ++i
) { 
1121         sig::Element 
&element(type
.data
.signature
.elements
[i 
+ 1]); 
1122         element
.name 
= NULL
; 
1123         element
.offset 
= _not(size_t); 
1125         JSObjectRef 
object(CYCastJSObject(context
, arguments
[i
])); 
1126         _assert(JSValueIsObjectOfClass(context
, object
, Type_privateData::Class_
)); 
1127         Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1129         element
.type 
= internal
->type_
; 
1132     return CYMakeType(context
, &type
); 
1135 static JSValueRef 
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1137         throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo"); 
1138     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1143     if (internal
->type_
->primitive 
== sig::char_P
) { 
1144         type
.flags 
= internal
->type_
->flags
; 
1145         type
.primitive 
= sig::string_P
; 
1146         type
.data
.data
.type 
= NULL
; 
1147         type
.data
.data
.size 
= 0; 
1150         type
.primitive 
= sig::pointer_P
; 
1151         type
.data
.data
.type 
= internal
->type_
; 
1152         type
.data
.data
.size 
= 0; 
1155     return CYMakeType(context
, &type
); 
1158 static JSValueRef 
Type_callAsFunction_withName(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1160         throw CYJSError(context
, "incorrect number of arguments to Type.withName"); 
1161     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1164     const char *name(CYPoolCString(pool
, context
, arguments
[0])); 
1166     sig::Type 
type(*internal
->type_
); 
1168     return CYMakeType(context
, &type
); 
1171 static JSValueRef 
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1173         throw CYJSError(context
, "incorrect number of arguments to type cast function"); 
1174     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1176     if (internal
->type_
->primitive 
== sig::function_P
) 
1177         return CYMakeFunctor(context
, arguments
[0], internal
->type_
->data
.signature
); 
1179     sig::Type 
*type(internal
->type_
); 
1180     ffi_type 
*ffi(internal
->GetFFI()); 
1182     uint8_t value
[ffi
->size
]; 
1184     CYPoolFFI(&pool
, context
, type
, ffi
, value
, arguments
[0]); 
1185     return CYFromFFI(context
, type
, ffi
, value
); 
1188 static JSObjectRef 
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1190         throw CYJSError(context
, "incorrect number of arguments to Type allocator"); 
1191     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1193     sig::Type 
*type(internal
->type_
); 
1196     if (type
->primitive 
!= sig::array_P
) 
1197         length 
= _not(size_t); 
1199         length 
= type
->data
.data
.size
; 
1200         type 
= type
->data
.data
.type
; 
1203     void *value(calloc(1, internal
->GetFFI()->size
)); 
1204     return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
); 
1207 static JSObjectRef 
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1209         throw CYJSError(context
, "incorrect number of arguments to Functor constructor"); 
1211     const char *encoding(CYPoolCString(pool
, context
, arguments
[1])); 
1212     sig::Signature signature
; 
1213     sig::Parse(pool
, &signature
, encoding
, &Structor_
); 
1214     return CYMakeFunctor(context
, arguments
[0], signature
); 
1217 static JSValueRef 
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1218     CYValue 
*internal(reinterpret_cast<CYValue 
*>(JSObjectGetPrivate(_this
))); 
1219     return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
)); 
1222 static JSValueRef 
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
1223     return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
); 
1226 static JSValueRef 
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1227     CYValue 
*internal(reinterpret_cast<CYValue 
*>(JSObjectGetPrivate(_this
))); 
1229     sprintf(string
, "%p", internal
->value_
); 
1230     return CYCastJSValue(context
, string
); 
1233 static JSValueRef 
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1234     Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(_this
))); 
1235     if (internal
->length_ 
!= _not(size_t)) { 
1236         JSObjectRef 
Array(CYGetCachedObject(context
, CYJSString("Array_prototype"))); 
1237         JSObjectRef 
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
))); 
1238         return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
); 
1241         sprintf(string
, "%p", internal
->value_
); 
1242         return CYCastJSValue(context
, string
); 
1246 static JSValueRef 
Functor_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
1247     cy::Functor 
*internal(reinterpret_cast<cy::Functor 
*>(JSObjectGetPrivate(object
))); 
1248     return CYMakeType(context
, &internal
->signature_
); 
1251 static JSValueRef 
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
1252     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1253     return CYCastJSValue(context
, internal
->GetFFI()->alignment
); 
1256 static JSValueRef 
Type_getProperty_name(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
1257     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1258     return CYCastJSValue(context
, internal
->type_
->name
); 
1261 static JSValueRef 
Type_getProperty_size(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
1262     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1263     return CYCastJSValue(context
, internal
->GetFFI()->size
); 
1266 static JSValueRef 
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1267     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1269     const char *type(sig::Unparse(pool
, internal
->type_
)); 
1270     return CYCastJSValue(context
, CYJSString(type
)); 
1273 static JSValueRef 
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1274     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1276     std::ostringstream out
; 
1278     CYOutput 
output(out
, options
); 
1279     (new(pool
) CYEncodedType(Decode(pool
, internal
->type_
)))->Output(output
, CYNoFlags
); 
1280     return CYCastJSValue(context
, CYJSString(out
.str().c_str())); 
1283 static JSValueRef 
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
1284     return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
); 
1287 static JSStaticFunction Pointer_staticFunctions
[4] = { 
1288     {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1289     {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1290     {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1294 static JSStaticFunction Struct_staticFunctions
[2] = { 
1295     {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1299 static JSStaticFunction Functor_staticFunctions
[4] = { 
1300     {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1301     {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1302     {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1307     JSStaticFunction 
const * const Functor::StaticFunctions 
= Functor_staticFunctions
; 
1310 static JSStaticValue Functor_staticValues
[2] = { 
1311     {"type", &Functor_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly 
| kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1312     {NULL
, NULL
, NULL
, 0} 
1315 static JSStaticValue Type_staticValues
[4] = { 
1316     {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly 
| kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1317     {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly 
| kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1318     {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly 
| kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1319     {NULL
, NULL
, NULL
, 0} 
1322 static JSStaticFunction Type_staticFunctions
[9] = { 
1323     {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1324     {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1325     {"functionWith", &Type_callAsFunction_functionWith
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1326     {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1327     {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1328     {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1329     {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1330     {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1334 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef 
*); 
1336 void CYSetArgs(int argc
, const char *argv
[]) { 
1337     JSContextRef 
context(CYGetJSContext()); 
1338     JSValueRef args
[argc
]; 
1339     for (int i(0); i 
!= argc
; ++i
) 
1340         args
[i
] = CYCastJSValue(context
, argv
[i
]); 
1343     if (JSObjectMakeArray$ 
!= NULL
) 
1344         array 
= _jsccall(*JSObjectMakeArray$
, context
, argc
, args
); 
1346         JSObjectRef 
Array(CYGetCachedObject(context
, CYJSString("Array"))); 
1347         JSValueRef 
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
)); 
1348         array 
= CYCastJSObject(context
, value
); 
1351     JSObjectRef 
System(CYGetCachedObject(context
, CYJSString("System"))); 
1352     CYSetProperty(context
, System
, CYJSString("args"), array
); 
1355 JSObjectRef 
CYGetGlobalObject(JSContextRef context
) { 
1356     return JSContextGetGlobalObject(context
); 
1359 const char *CYExecute(JSContextRef context
, CYPool 
&pool
, CYUTF8String code
) { 
1360     JSValueRef 
exception(NULL
); 
1363     if (hooks_ 
!= NULL 
&& hooks_
->ExecuteStart 
!= NULL
) 
1364         handle 
= (*hooks_
->ExecuteStart
)(context
); 
1372         result 
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
); 
1373     } catch (const char *error
) { 
1377     if (exception 
!= NULL
) error
: 
1378         return CYPoolCString(pool
, context
, CYJSString(context
, exception
)); 
1380     if (JSValueIsUndefined(context
, result
)) 
1385         json 
= CYPoolCCYON(pool
, context
, result
, &exception
); 
1386     } catch (const char *error
) { 
1390     if (exception 
!= NULL
) 
1393     CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
); 
1398         if (hooks_ 
!= NULL 
&& hooks_
->ExecuteEnd 
!= NULL
) 
1399             (*hooks_
->ExecuteEnd
)(context
, handle
); 
1404 extern "C" void CydgetSetupContext(JSGlobalContextRef context
) { 
1405     CYSetupContext(context
); 
1408 static bool initialized_ 
= false; 
1410 void CYInitializeDynamic() { 
1412         initialized_ 
= true; 
1415     JSObjectMakeArray$ 
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef 
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray")); 
1417     JSClassDefinition definition
; 
1419     definition 
= kJSClassDefinitionEmpty
; 
1420     definition
.className 
= "All"; 
1421     definition
.getProperty 
= &All_getProperty
; 
1422     definition
.getPropertyNames 
= &All_getPropertyNames
; 
1423     All_ 
= JSClassCreate(&definition
); 
1425     definition 
= kJSClassDefinitionEmpty
; 
1426     definition
.className 
= "Context"; 
1427     definition
.finalize 
= &CYFinalize
; 
1428     Context_ 
= JSClassCreate(&definition
); 
1430     definition 
= kJSClassDefinitionEmpty
; 
1431     definition
.className 
= "Functor"; 
1432     definition
.staticFunctions 
= cy::Functor::StaticFunctions
; 
1433     definition
.staticValues 
= Functor_staticValues
; 
1434     definition
.callAsFunction 
= &Functor_callAsFunction
; 
1435     definition
.finalize 
= &CYFinalize
; 
1436     Functor_ 
= JSClassCreate(&definition
); 
1438     definition 
= kJSClassDefinitionEmpty
; 
1439     definition
.className 
= "Pointer"; 
1440     definition
.staticFunctions 
= Pointer_staticFunctions
; 
1441     definition
.getProperty 
= &Pointer_getProperty
; 
1442     definition
.setProperty 
= &Pointer_setProperty
; 
1443     definition
.finalize 
= &CYFinalize
; 
1444     Pointer_ 
= JSClassCreate(&definition
); 
1446     definition 
= kJSClassDefinitionEmpty
; 
1447     definition
.className 
= "Struct"; 
1448     definition
.staticFunctions 
= Struct_staticFunctions
; 
1449     definition
.getProperty 
= &Struct_getProperty
; 
1450     definition
.setProperty 
= &Struct_setProperty
; 
1451     definition
.getPropertyNames 
= &Struct_getPropertyNames
; 
1452     definition
.finalize 
= &CYFinalize
; 
1453     Struct_ 
= JSClassCreate(&definition
); 
1455     definition 
= kJSClassDefinitionEmpty
; 
1456     definition
.className 
= "Type"; 
1457     definition
.staticValues 
= Type_staticValues
; 
1458     definition
.staticFunctions 
= Type_staticFunctions
; 
1459     definition
.callAsFunction 
= &Type_callAsFunction
; 
1460     definition
.callAsConstructor 
= &Type_callAsConstructor
; 
1461     definition
.finalize 
= &CYFinalize
; 
1462     Type_privateData::Class_ 
= JSClassCreate(&definition
); 
1464     definition 
= kJSClassDefinitionEmpty
; 
1465     definition
.className 
= "Global"; 
1466     //definition.getProperty = &Global_getProperty; 
1467     Global_ 
= JSClassCreate(&definition
); 
1469     Array_s 
= JSStringCreateWithUTF8CString("Array"); 
1470     cy_s 
= JSStringCreateWithUTF8CString("$cy"); 
1471     length_s 
= JSStringCreateWithUTF8CString("length"); 
1472     message_s 
= JSStringCreateWithUTF8CString("message"); 
1473     name_s 
= JSStringCreateWithUTF8CString("name"); 
1474     pop_s 
= JSStringCreateWithUTF8CString("pop"); 
1475     prototype_s 
= JSStringCreateWithUTF8CString("prototype"); 
1476     push_s 
= JSStringCreateWithUTF8CString("push"); 
1477     splice_s 
= JSStringCreateWithUTF8CString("splice"); 
1478     toCYON_s 
= JSStringCreateWithUTF8CString("toCYON"); 
1479     toJSON_s 
= JSStringCreateWithUTF8CString("toJSON"); 
1480     toPointer_s 
= JSStringCreateWithUTF8CString("toPointer"); 
1481     toString_s 
= JSStringCreateWithUTF8CString("toString"); 
1483     Result_ 
= JSStringCreateWithUTF8CString("_"); 
1485     if (hooks_ 
!= NULL 
&& hooks_
->Initialize 
!= NULL
) 
1486         (*hooks_
->Initialize
)(); 
1489 void CYThrow(JSContextRef context
, JSValueRef value
) { 
1491         throw CYJSError(context
, value
); 
1494 const char *CYJSError::PoolCString(CYPool 
&pool
) const { 
1495     // XXX: this used to be CYPoolCString 
1496     return CYPoolCCYON(pool
, context_
, value_
); 
1499 JSValueRef 
CYJSError::CastJSValue(JSContextRef context
) const { 
1500     // XXX: what if the context is different? 
1504 JSValueRef 
CYCastJSError(JSContextRef context
, const char *message
) { 
1505     JSObjectRef 
Error(CYGetCachedObject(context
, CYJSString("Error"))); 
1506     JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)}; 
1507     return _jsccall(JSObjectCallAsConstructor
, context
, Error
, 1, arguments
); 
1510 JSValueRef 
CYPoolError::CastJSValue(JSContextRef context
) const { 
1511     return CYCastJSError(context
, message_
); 
1514 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) { 
1515     _assert(context 
!= NULL
); 
1520     va_start(args
, format
); 
1521     // XXX: there might be a beter way to think about this 
1522     const char *message(pool
.vsprintf(64, format
, args
)); 
1525     value_ 
= CYCastJSError(context
, message
); 
1528 JSGlobalContextRef 
CYGetJSContext(JSContextRef context
) { 
1529     return reinterpret_cast<Context 
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
; 
1532 extern "C" bool CydgetMemoryParse(const uint16_t **data
, size_t *size
); 
1534 void *CYMapFile(const char *path
, size_t *psize
) { 
1536     _syscall(fd 
= open(path
, O_RDONLY
)); 
1539     _syscall(fstat(fd
, &stat
)); 
1540     size_t size(stat
.st_size
); 
1545     _syscall(base 
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, 0)); 
1547     _syscall(close(fd
)); 
1551 static void CYRunSetups(JSContextRef context
) { 
1552     std::string 
folder("/etc/cycript/setup.d"); 
1553     DIR *setups(opendir(folder
.c_str())); 
1560         _syscall(readdir_r(setups
, &setup
, &result
)); 
1564         _assert(result 
== &setup
); 
1566         const char *name(setup
.d_name
); 
1567         size_t length(strlen(name
)); 
1573         if (memcmp(name 
+ length 
- 3, ".cy", 3) != 0) 
1576         std::string 
script(folder 
+ "/" + name
); 
1578         utf8
.data 
= reinterpret_cast<char *>(CYMapFile(script
.c_str(), &utf8
.size
)); 
1581         CYUTF16String 
utf16(CYPoolUTF16String(pool
, utf8
)); 
1582         munmap(const_cast<char *>(utf8
.data
), utf8
.size
); 
1584         if (CydgetMemoryParse(&utf16
.data
, &utf16
.size
)) 
1585             CYExecute(context
, pool
, CYPoolUTF8String(pool
, utf16
)); 
1586         free(const_cast<uint16_t *>(utf16
.data
)); 
1589     _syscall(closedir(setups
)); 
1592 static JSValueRef 
require(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1593     _assert(count 
== 1); 
1597     _assert(dladdr(reinterpret_cast<void *>(&require
), &addr
) != 0); 
1598     char *lib(pool
.strdup(addr
.dli_fname
)); 
1600     char *slash(strrchr(lib
, '/')); 
1601     _assert(slash 
!= NULL
); 
1604     CYJSString 
property("exports"); 
1607     const char *path(pool
.strcat(lib
, "/cycript/", CYPoolCString(pool
, context
, arguments
[0]), ".cy", NULL
)); 
1608     CYJSString 
key(path
); 
1609     JSObjectRef 
modules(CYGetCachedObject(context
, CYJSString("modules"))); 
1610     JSValueRef 
cache(CYGetProperty(context
, modules
, key
)); 
1612     if (!JSValueIsUndefined(context
, cache
)) 
1613         module = CYCastJSObject(context
, cache
); 
1616         code
.data 
= reinterpret_cast<char *>(CYMapFile(path
, &code
.size
)); 
1618         std::stringstream wrap
; 
1619         wrap 
<< "(function (exports, require, module) { " << code 
<< "\n});"; 
1620         code 
= CYPoolCode(pool
, wrap
.str().c_str()); 
1622         JSValueRef 
value(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0)); 
1623         JSObjectRef 
function(CYCastJSObject(context
, value
)); 
1625         module = JSObjectMake(context
, NULL
, NULL
); 
1626         JSObjectRef 
exports(JSObjectMake(context
, NULL
, NULL
)); 
1627         CYSetProperty(context
, module, property
, exports
); 
1629         JSValueRef arguments
[3] = { exports
, JSObjectMakeFunctionWithCallback(context
, CYJSString("require"), &require
), module }; 
1630         CYCallAsFunction(context
, function
, NULL
, 3, arguments
); 
1631         CYSetProperty(context
, modules
, key
, module); 
1634     return CYGetProperty(context
, module, property
); 
1637 extern "C" void CYSetupContext(JSGlobalContextRef context
) { 
1638     CYInitializeDynamic(); 
1640     JSObjectRef 
global(CYGetGlobalObject(context
)); 
1642     JSObjectRef 
cy(JSObjectMake(context
, Context_
, new Context(context
))); 
1643     CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
); 
1645 /* Cache Globals {{{ */ 
1646     JSObjectRef 
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array")))); 
1647     CYSetProperty(context
, cy
, CYJSString("Array"), Array
); 
1649     JSObjectRef 
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
))); 
1650     CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
); 
1652     JSObjectRef 
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error")))); 
1653     CYSetProperty(context
, cy
, CYJSString("Error"), Error
); 
1655     JSObjectRef 
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function")))); 
1656     CYSetProperty(context
, cy
, CYJSString("Function"), Function
); 
1658     JSObjectRef 
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
))); 
1659     CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
); 
1661     JSObjectRef 
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object")))); 
1662     CYSetProperty(context
, cy
, CYJSString("Object"), Object
); 
1664     JSObjectRef 
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
))); 
1665     CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
); 
1667     JSObjectRef 
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String")))); 
1668     CYSetProperty(context
, cy
, CYJSString("String"), String
); 
1670     JSObjectRef 
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
))); 
1671     CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
); 
1674     CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
); 
1675     CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
); 
1677     JSObjectRef 
cycript(JSObjectMake(context
, NULL
, NULL
)); 
1678     CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
); 
1679     CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
); 
1681     JSObjectRef 
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
)); 
1682     JSObjectSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
); 
1683     CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
); 
1685     CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
)); 
1686     CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
)); 
1688     JSObjectRef 
modules(JSObjectMake(context
, NULL
, NULL
)); 
1689     CYSetProperty(context
, cy
, CYJSString("modules"), modules
); 
1691     JSObjectRef 
all(JSObjectMake(context
, All_
, NULL
)); 
1692     CYSetProperty(context
, cycript
, CYJSString("all"), all
); 
1694     JSObjectRef 
alls(_jsccall(JSObjectCallAsConstructor
, context
, Array
, 0, NULL
)); 
1695     CYSetProperty(context
, cycript
, CYJSString("alls"), alls
); 
1698         JSObjectRef 
last(NULL
), curr(global
); 
1700         goto next
; for (JSValueRef next
;;) { 
1701             if (JSValueIsNull(context
, next
)) 
1704             curr 
= CYCastJSObject(context
, next
); 
1706             next 
= JSObjectGetPrototype(context
, curr
); 
1709         JSObjectSetPrototype(context
, last
, all
); 
1712     CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
, kJSPropertyAttributeDontEnum
); 
1714     JSObjectRef 
System(JSObjectMake(context
, NULL
, NULL
)); 
1715     CYSetProperty(context
, cy
, CYJSString("System"), System
); 
1717     CYSetProperty(context
, all
, CYJSString("require"), &require
, kJSPropertyAttributeDontEnum
); 
1719     CYSetProperty(context
, global
, CYJSString("system"), System
); 
1720     CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
)); 
1721     //CYSetProperty(context, System, CYJSString("global"), global); 
1722     CYSetProperty(context
, System
, CYJSString("print"), &System_print
); 
1724     if (CYBridgeEntry 
*entry 
= CYBridgeHash("1dlerror", 8)) 
1725         entry
->cache_ 
= new cy::Functor(entry
->value_
, reinterpret_cast<void (*)()>(&dlerror
)); 
1727     if (hooks_ 
!= NULL 
&& hooks_
->SetupContext 
!= NULL
) 
1728         (*hooks_
->SetupContext
)(context
); 
1730     CYArrayPush(context
, alls
, cycript
); 
1732     CYRunSetups(context
); 
1735 JSGlobalContextRef 
CYGetJSContext() { 
1736     CYInitializeDynamic(); 
1738     static JSGlobalContextRef context_
; 
1740     if (context_ 
== NULL
) { 
1741         context_ 
= JSGlobalContextCreate(Global_
); 
1742         CYSetupContext(context_
);