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 static std::vector
<CYHook 
*> &GetHooks() { 
  54     static std::vector
<CYHook 
*> hooks
; 
  58 CYRegisterHook::CYRegisterHook(CYHook 
*hook
) { 
  59     GetHooks().push_back(hook
); 
  62 /* JavaScript Properties {{{ */ 
  63 bool CYHasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) { 
  64     return JSObjectHasProperty(context
, object
, name
); 
  67 JSValueRef 
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) { 
  68     return _jsccall(JSObjectGetPropertyAtIndex
, context
, object
, index
); 
  71 JSValueRef 
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) { 
  72     return _jsccall(JSObjectGetProperty
, context
, object
, name
); 
  75 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) { 
  76     _jsccall(JSObjectSetPropertyAtIndex
, context
, object
, index
, value
); 
  79 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) { 
  80     _jsccall(JSObjectSetProperty
, context
, object
, name
, value
, attributes
); 
  83 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef (*callback
)(JSContextRef
, JSObjectRef
, JSObjectRef
, size_t, const JSValueRef
[], JSValueRef 
*), JSPropertyAttributes attributes
) { 
  84     CYSetProperty(context
, object
, name
, JSObjectMakeFunctionWithCallback(context
, name
, callback
), attributes
); 
  87 void CYSetPrototype(JSContextRef context
, JSObjectRef object
, JSValueRef value
) { 
  88     JSObjectSetPrototype(context
, object
, value
); 
  89     _assert(CYIsStrictEqual(context
, JSObjectGetPrototype(context
, object
), value
)); 
  92 /* JavaScript Strings {{{ */ 
  93 JSStringRef 
CYCopyJSString(const char *value
) { 
  94     return value 
== NULL 
? NULL 
: JSStringCreateWithUTF8CString(value
); 
  97 JSStringRef 
CYCopyJSString(JSStringRef value
) { 
  98     return value 
== NULL 
? NULL 
: JSStringRetain(value
); 
 101 JSStringRef 
CYCopyJSString(CYUTF8String value
) { 
 102     // XXX: this is very wrong; it needs to convert to UTF16 and then create from there 
 103     return CYCopyJSString(value
.data
); 
 106 JSStringRef 
CYCopyJSString(JSContextRef context
, JSValueRef value
) { 
 107     if (JSValueIsNull(context
, value
)) 
 109     return _jsccall(JSValueToStringCopy
, context
, value
); 
 112 static CYUTF16String 
CYCastUTF16String(JSStringRef value
) { 
 113     return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
)); 
 116 CYUTF8String 
CYPoolUTF8String(CYPool 
&pool
, JSContextRef context
, JSStringRef value
) { 
 117     return CYPoolUTF8String(pool
, CYCastUTF16String(value
)); 
 120 const char *CYPoolCString(CYPool 
&pool
, JSContextRef context
, JSStringRef value
) { 
 121     CYUTF8String 
utf8(CYPoolUTF8String(pool
, context
, value
)); 
 122     _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
); 
 126 const char *CYPoolCString(CYPool 
&pool
, JSContextRef context
, JSValueRef value
) { 
 127     return JSValueIsNull(context
, value
) ? NULL 
: CYPoolCString(pool
, context
, CYJSString(context
, value
)); 
 130 /* Index Offsets {{{ */ 
 131 size_t CYGetIndex(CYPool 
&pool
, JSContextRef context
, JSStringRef value
) { 
 132     return CYGetIndex(CYPoolUTF8String(pool
, context
, value
)); 
 136 static JSClassRef All_
; 
 137 static JSClassRef Context_
; 
 139 static JSClassRef Global_
; 
 140 static JSClassRef Pointer_
; 
 141 static JSClassRef Struct_
; 
 146 JSStringRef length_s
; 
 147 JSStringRef message_s
; 
 150 JSStringRef prototype_s
; 
 152 JSStringRef splice_s
; 
 153 JSStringRef toCYON_s
; 
 154 JSStringRef toJSON_s
; 
 155 JSStringRef toPointer_s
; 
 156 JSStringRef toString_s
; 
 158 static JSStringRef Result_
; 
 160 void CYFinalize(JSObjectRef object
) { 
 161     CYData 
*internal(reinterpret_cast<CYData 
*>(JSObjectGetPrivate(object
))); 
 162     _assert(internal
->count_ 
!= _not(unsigned)); 
 163     if (--internal
->count_ 
== 0) 
 167 void Structor_(CYPool 
&pool
, sig::Type 
*&type
) { 
 169         type
->primitive 
== sig::pointer_P 
&& 
 170         type
->data
.data
.type
->primitive 
== sig::struct_P 
&& 
 171         type
->data
.data
.type
->name 
!= NULL 
&& 
 172         strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0 
 174         type
->primitive 
= sig::typename_P
; 
 175         type
->data
.data
.type 
= NULL
; 
 179     if (type
->primitive 
!= sig::struct_P 
|| type
->name 
== NULL
) 
 182     size_t length(strlen(type
->name
)); 
 183     char keyed
[length 
+ 2]; 
 184     memcpy(keyed 
+ 1, type
->name
, length 
+ 1); 
 186     static const char *modes 
= "34"; 
 187     for (size_t i(0); i 
!= 2; ++i
) { 
 191         if (CYBridgeEntry 
*entry 
= CYBridgeHash(keyed
, length 
+ 1)) 
 194                     sig::Parse(pool
, &type
->data
.signature
, entry
->value_
, &Structor_
); 
 198                     sig::Signature signature
; 
 199                     sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
); 
 200                     type 
= signature
.elements
[0].type
; 
 206 JSClassRef 
Type_privateData::Class_
; 
 211     JSGlobalContextRef context_
; 
 213     Context(JSGlobalContextRef context
) : 
 222     Type_privateData 
*type_
; 
 225     Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type 
*type
) : 
 226         CYOwned(value
, context
, owner
), 
 227         type_(new(*pool_
) Type_privateData(type
)), 
 233 struct Struct_privateData 
: 
 236     Type_privateData 
*type_
; 
 238     Struct_privateData(JSContextRef context
, JSObjectRef owner
) : 
 239         CYOwned(NULL
, context
, owner
) 
 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 bool CYIsEqual(JSContextRef context
, JSValueRef lhs
, JSValueRef rhs
) { 
 323     return _jsccall(JSValueIsEqual
, context
, lhs
, rhs
); 
 326 bool CYIsStrictEqual(JSContextRef context
, JSValueRef lhs
, JSValueRef rhs
) { 
 327     return JSValueIsStrictEqual(context
, lhs
, rhs
); 
 330 size_t CYArrayLength(JSContextRef context
, JSObjectRef array
) { 
 331     return CYCastDouble(context
, CYGetProperty(context
, array
, length_s
)); 
 334 JSValueRef 
CYArrayGet(JSContextRef context
, JSObjectRef array
, size_t index
) { 
 335     return _jsccall(JSObjectGetPropertyAtIndex
, context
, array
, index
); 
 338 void CYArrayPush(JSContextRef context
, JSObjectRef array
, JSValueRef value
) { 
 339     JSValueRef arguments
[1]; 
 340     arguments
[0] = value
; 
 341     JSObjectRef 
Array(CYGetCachedObject(context
, CYJSString("Array_prototype"))); 
 342     _jsccall(JSObjectCallAsFunction
, context
, CYCastJSObject(context
, CYGetProperty(context
, Array
, push_s
)), array
, 1, arguments
); 
 345 static JSValueRef 
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 350         printf("%s\n", CYPoolCString(pool
, context
, arguments
[0])); 
 353     return CYJSUndefined(context
); 
 356 static size_t Nonce_(0); 
 358 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 360     const char *name(pool
.strcat(CYPoolCString(pool
, context
, arguments
[0]), pool
.itoa(Nonce_
++), NULL
)); 
 361     return CYCastJSValue(context
, name
); 
 364 static void (*JSSynchronousGarbageCollectForDebugging$
)(JSContextRef
); 
 366 void CYGarbageCollect(JSContextRef context
) { 
 367     (JSSynchronousGarbageCollectForDebugging$ 
?: &JSGarbageCollect
)(context
); 
 370 static JSValueRef 
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 371     CYGarbageCollect(context
); 
 372     return CYJSUndefined(context
); 
 375 const char *CYPoolCCYON(CYPool 
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> &objects
, JSValueRef 
*exception
) { CYTry 
{ 
 376     switch (JSType type 
= JSValueGetType(context
, value
)) { 
 377         case kJSTypeUndefined
: 
 382             return CYCastBool(context
, value
) ? "true" : "false"; 
 384         case kJSTypeNumber
: { 
 385             std::ostringstream str
; 
 386             CYNumerify(str
, CYCastDouble(context
, value
)); 
 387             std::string 
value(str
.str()); 
 388             return pool
.strmemdup(value
.c_str(), value
.size()); 
 391         case kJSTypeString
: { 
 392             std::ostringstream str
; 
 393             CYUTF8String 
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
))); 
 394             CYStringify(str
, string
.data
, string
.size
); 
 395             std::string 
value(str
.str()); 
 396             return pool
.strmemdup(value
.c_str(), value
.size()); 
 400             return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
, objects
); 
 402             throw CYJSError(context
, "JSValueGetType() == 0x%x", type
); 
 406 const char *CYPoolCCYON(CYPool 
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> &objects
) { 
 407     return _jsccall(CYPoolCCYON
, pool
, context
, value
, objects
); 
 410 const char *CYPoolCCYON(CYPool 
&pool
, JSContextRef context
, JSValueRef value
, std::set
<void *> *objects
) { 
 412         return CYPoolCCYON(pool
, context
, value
, *objects
); 
 414         std::set
<void *> objects
; 
 415         return CYPoolCCYON(pool
, context
, value
, objects
); 
 419 const char *CYPoolCCYON(CYPool 
&pool
, JSContextRef context
, JSObjectRef object
, std::set
<void *> &objects
) { 
 420     JSValueRef 
toCYON(CYGetProperty(context
, object
, toCYON_s
)); 
 421     if (CYIsCallable(context
, toCYON
)) { 
 422         // XXX: this needs to be abstracted behind some kind of function 
 423         JSValueRef arguments
[1] = {CYCastJSValue(context
, static_cast<double>(reinterpret_cast<uintptr_t>(&objects
)))}; 
 424         JSValueRef 
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 1, arguments
)); 
 425         _assert(value 
!= NULL
); 
 426         return CYPoolCString(pool
, context
, value
); 
 429     JSValueRef 
toJSON(CYGetProperty(context
, object
, toJSON_s
)); 
 430     if (CYIsCallable(context
, toJSON
)) { 
 431         JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))}; 
 432         return _jsccall(CYPoolCCYON
, pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), objects
); 
 435     if (JSObjectIsFunction(context
, object
)) { 
 436         JSValueRef 
toString(CYGetProperty(context
, object
, toString_s
)); 
 437         if (CYIsCallable(context
, toString
)) { 
 438             JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))}; 
 439             JSValueRef 
value(CYCallAsFunction(context
, (JSObjectRef
) toString
, object
, 1, arguments
)); 
 440             _assert(value 
!= NULL
); 
 441             return CYPoolCString(pool
, context
, value
); 
 445     _assert(objects
.insert(object
).second
); 
 447     std::ostringstream str
; 
 451     // XXX: this is, sadly, going to leak 
 452     JSPropertyNameArrayRef 
names(JSObjectCopyPropertyNames(context
, object
)); 
 456     for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index 
!= count
; ++index
) { 
 462         JSStringRef 
name(JSPropertyNameArrayGetNameAtIndex(names
, index
)); 
 463         CYUTF8String 
string(CYPoolUTF8String(pool
, context
, name
)); 
 468             CYStringify(str
, string
.data
, string
.size
); 
 473             JSValueRef 
value(CYGetProperty(context
, object
, name
)); 
 474             str 
<< CYPoolCCYON(pool
, context
, value
, objects
); 
 475         } catch (const CYException 
&error
) { 
 480     JSPropertyNameArrayRelease(names
); 
 484     std::string 
string(str
.str()); 
 485     return pool
.strmemdup(string
.c_str(), string
.size()); 
 488 std::set
<void *> *CYCastObjects(JSContextRef context
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) { 
 491     return CYCastPointer
<std::set
<void *> *>(context
, arguments
[0]); 
 494 static JSValueRef 
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 495     std::set
<void *> *objects(CYCastObjects(context
, _this
, count
, arguments
)); 
 496     // XXX: this is horribly inefficient 
 497     std::set
<void *> backup
; 
 502     std::ostringstream str
; 
 506     JSValueRef 
length(CYGetProperty(context
, _this
, length_s
)); 
 509     for (size_t index(0), count(CYCastDouble(context
, length
)); index 
!= count
; ++index
) { 
 516             JSValueRef 
value(CYGetProperty(context
, _this
, index
)); 
 517             if (!JSValueIsUndefined(context
, value
)) 
 518                 str 
<< CYPoolCCYON(pool
, context
, value
, *objects
); 
 523         } catch (const CYException 
&error
) { 
 530     std::string 
value(str
.str()); 
 531     return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size()))); 
 534 static JSValueRef 
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 536     std::ostringstream str
; 
 538     CYUTF8String 
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
))); 
 539     CYStringify(str
, string
.data
, string
.size
); 
 541     std::string 
value(str
.str()); 
 542     return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size()))); 
 545 JSObjectRef 
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type 
*type
, ffi_type 
*ffi
, JSObjectRef owner
) { 
 546     Pointer 
*internal(new Pointer(pointer
, context
, owner
, length
, type
)); 
 547     return JSObjectMake(context
, Pointer_
, internal
); 
 550 static JSObjectRef 
CYMakeFunctor(JSContextRef context
, void (*function
)(), const sig::Signature 
&signature
) { 
 551     return JSObjectMake(context
, Functor_
, new cy::Functor(signature
, function
)); 
 554 static JSObjectRef 
CYMakeFunctor(JSContextRef context
, const char *symbol
, const char *encoding
, void **cache
) { 
 555     cy::Functor 
*internal
; 
 557         internal 
= reinterpret_cast<cy::Functor 
*>(*cache
); 
 559         void (*function
)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol
))); 
 560         if (function 
== NULL
) 
 563         internal 
= new cy::Functor(encoding
, function
); 
 568     return JSObjectMake(context
, Functor_
, internal
); 
 571 static bool CYGetOffset(CYPool 
&pool
, JSContextRef context
, JSStringRef value
, ssize_t 
&index
) { 
 572     return CYGetOffset(CYPoolCString(pool
, context
, value
), index
); 
 575 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) { 
 576     switch (JSValueGetType(context
, value
)) { 
 579         case kJSTypeObject
: { 
 580             JSObjectRef 
object((JSObjectRef
) value
); 
 581             if (JSValueIsObjectOfClass(context
, value
, Pointer_
)) { 
 582                 Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(object
))); 
 583                 return internal
->value_
; 
 585             JSValueRef 
toPointer(CYGetProperty(context
, object
, toPointer_s
)); 
 586             if (CYIsCallable(context
, toPointer
)) { 
 587                 JSValueRef 
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
)); 
 588                 _assert(value 
!= NULL
); 
 589                 return CYCastPointer_(context
, value
); 
 592             double number(CYCastDouble(context
, value
)); 
 593             if (std::isnan(number
)) 
 594                 throw CYJSError(context
, "cannot convert value to pointer"); 
 595             return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
))); 
 599 void CYPoolFFI(CYPool 
*pool
, JSContextRef context
, sig::Type 
*type
, ffi_type 
*ffi
, void *data
, JSValueRef value
) { 
 600     switch (type
->primitive
) { 
 602             *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
); 
 605 #define CYPoolFFI_(primitive, native) \ 
 606         case sig::primitive ## _P: \ 
 607             *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \ 
 610         CYPoolFFI_(uchar
, unsigned char) 
 611         CYPoolFFI_(char, char) 
 612         CYPoolFFI_(ushort
, unsigned short) 
 613         CYPoolFFI_(short, short) 
 614         CYPoolFFI_(ulong
, unsigned long) 
 615         CYPoolFFI_(long, long) 
 616         CYPoolFFI_(uint
, unsigned int) 
 618         CYPoolFFI_(ulonglong
, unsigned long long) 
 619         CYPoolFFI_(longlong
, long long) 
 620         CYPoolFFI_(float, float) 
 621         CYPoolFFI_(double, double) 
 624             uint8_t *base(reinterpret_cast<uint8_t *>(data
)); 
 625             JSObjectRef 
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value 
: NULL
); 
 626             for (size_t index(0); index 
!= type
->data
.data
.size
; ++index
) { 
 627                 ffi_type 
*field(ffi
->elements
[index
]); 
 630                 if (aggregate 
== NULL
) 
 633                     rhs 
= CYGetProperty(context
, aggregate
, index
); 
 634                     if (JSValueIsUndefined(context
, rhs
)) 
 635                         throw CYJSError(context
, "unable to extract array value"); 
 638                 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
); 
 645             *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
); 
 649             _assert(pool 
!= NULL
); 
 650             *reinterpret_cast<const char **>(data
) = CYPoolCString(*pool
, context
, value
); 
 653         case sig::struct_P
: { 
 654             uint8_t *base(reinterpret_cast<uint8_t *>(data
)); 
 655             JSObjectRef 
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value 
: NULL
); 
 656             for (size_t index(0); index 
!= type
->data
.signature
.count
; ++index
) { 
 657                 sig::Element 
*element(&type
->data
.signature
.elements
[index
]); 
 658                 ffi_type 
*field(ffi
->elements
[index
]); 
 661                 if (aggregate 
== NULL
) 
 664                     rhs 
= CYGetProperty(context
, aggregate
, index
); 
 665                     if (JSValueIsUndefined(context
, rhs
)) { 
 666                         if (element
->name 
!= NULL
) 
 667                             rhs 
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
)); 
 670                         if (JSValueIsUndefined(context
, rhs
)) undefined
: 
 671                             throw CYJSError(context
, "unable to extract structure value"); 
 675                 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
); 
 685             for (CYHook 
*hook 
: GetHooks()) 
 686                 if (hook
->PoolFFI 
!= NULL
) 
 687                     if ((*hook
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
)) 
 690             CYThrow("unimplemented signature code: '%c''\n", type
->primitive
); 
 694 JSValueRef 
CYFromFFI(JSContextRef context
, sig::Type 
*type
, ffi_type 
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) { 
 695     switch (type
->primitive
) { 
 697             return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
)); 
 699 #define CYFromFFI_(primitive, native) \ 
 700         case sig::primitive ## _P: \ 
 701             return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \ 
 703         CYFromFFI_(uchar, unsigned char) 
 704         CYFromFFI_(char, char) 
 705         CYFromFFI_(ushort
, unsigned short) 
 706         CYFromFFI_(short, short) 
 707         CYFromFFI_(ulong
, unsigned long) 
 708         CYFromFFI_(long, long) 
 709         CYFromFFI_(uint
, unsigned int) 
 711         CYFromFFI_(ulonglong
, unsigned long long) 
 712         CYFromFFI_(longlong
, long long) 
 713         CYFromFFI_(float, float) 
 714         CYFromFFI_(double, double) 
 717             if (void *pointer 
= data
) 
 718                 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
); 
 722             if (void *pointer 
= *reinterpret_cast<void **>(data
)) 
 723                 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
); 
 727             if (char *utf8 
= *reinterpret_cast<char **>(data
)) 
 728                 return CYCastJSValue(context
, utf8
); 
 732             return CYMakeStruct(context
, data
, type
, ffi
, owner
); 
 734             return CYJSUndefined(context
); 
 737             return CYJSNull(context
); 
 739             for (CYHook 
*hook 
: GetHooks()) 
 740                 if (hook
->FromFFI 
!= NULL
) 
 741                     if (JSValueRef value 
= (*hook
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
)) 
 744             CYThrow("unimplemented signature code: '%c''\n", type
->primitive
); 
 748 void CYExecuteClosure(ffi_cif 
*cif
, void *result
, void **arguments
, void *arg
, JSValueRef (*adapter
)(JSContextRef
, size_t, JSValueRef
[], JSObjectRef
)) { 
 749     Closure_privateData 
*internal(reinterpret_cast<Closure_privateData 
*>(arg
)); 
 751     JSContextRef 
context(internal
->context_
); 
 753     size_t count(internal
->cif_
.nargs
); 
 754     JSValueRef values
[count
]; 
 756     for (size_t index(0); index 
!= count
; ++index
) 
 757         values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]); 
 759     JSValueRef 
value(adapter(context
, count
, values
, internal
->function_
)); 
 760     CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
); 
 763 static JSValueRef 
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
) { 
 764     return CYCallAsFunction(context
, function
, NULL
, count
, values
); 
 767 static void FunctionClosure_(ffi_cif 
*cif
, void *result
, void **arguments
, void *arg
) { 
 768     CYExecuteClosure(cif
, result
, arguments
, arg
, &FunctionAdapter_
); 
 771 Closure_privateData 
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const sig::Signature 
&signature
, void (*callback
)(ffi_cif 
*, void *, void **, void *)) { 
 772     // XXX: in case of exceptions this will leak 
 773     // XXX: in point of fact, this may /need/ to leak :( 
 774     Closure_privateData 
*internal(new Closure_privateData(context
, function
, signature
)); 
 776 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__)) 
 778     ffi_closure 
*writable(reinterpret_cast<ffi_closure 
*>(ffi_closure_alloc(sizeof(ffi_closure
), &executable
))); 
 780     ffi_status 
status(ffi_prep_closure_loc(writable
, &internal
->cif_
, callback
, internal
, executable
)); 
 781     _assert(status 
== FFI_OK
); 
 783     internal
->value_ 
= executable
; 
 785     ffi_closure 
*closure((ffi_closure 
*) _syscall(mmap( 
 786         NULL
, sizeof(ffi_closure
), 
 787         PROT_READ 
| PROT_WRITE
, MAP_ANON 
| MAP_PRIVATE
, 
 791     ffi_status 
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
)); 
 792     _assert(status 
== FFI_OK
); 
 794     _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ 
| PROT_EXEC
)); 
 796     internal
->value_ 
= closure
; 
 802 static JSObjectRef 
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const sig::Signature 
&signature
) { 
 803     Closure_privateData 
*internal(CYMakeFunctor_(context
, function
, signature
, &FunctionClosure_
)); 
 804     JSObjectRef 
object(JSObjectMake(context
, Functor_
, internal
)); 
 805     // XXX: see above notes about needing to leak 
 806     JSValueProtect(CYGetJSContext(context
), object
); 
 810 JSObjectRef 
CYGetCachedObject(JSContextRef context
, JSStringRef name
) { 
 811     return CYCastJSObject(context
, CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
)); 
 814 static JSObjectRef 
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const sig::Signature 
&signature
) { 
 815     JSObjectRef 
Function(CYGetCachedObject(context
, CYJSString("Function"))); 
 817     bool function(_jsccall(JSValueIsInstanceOfConstructor
, context
, value
, Function
)); 
 819         JSObjectRef 
function(CYCastJSObject(context
, value
)); 
 820         return CYMakeFunctor(context
, function
, signature
); 
 822         void (*function
)()(CYCastPointer
<void (*)()>(context
, value
)); 
 823         return CYMakeFunctor(context
, function
, signature
); 
 827 static bool Index_(CYPool 
&pool
, JSContextRef context
, Struct_privateData 
*internal
, JSStringRef property
, ssize_t 
&index
, uint8_t *&base
) { 
 828     Type_privateData 
*typical(internal
->type_
); 
 829     sig::Type 
*type(typical
->type_
); 
 833     const char *name(CYPoolCString(pool
, context
, property
)); 
 834     size_t length(strlen(name
)); 
 835     double number(CYCastDouble(name
, length
)); 
 837     size_t count(type
->data
.signature
.count
); 
 839     if (std::isnan(number
)) { 
 840         if (property 
== NULL
) 
 843         sig::Element 
*elements(type
->data
.signature
.elements
); 
 845         for (size_t local(0); local 
!= count
; ++local
) { 
 846             sig::Element 
*element(&elements
[local
]); 
 847             if (element
->name 
!= NULL 
&& strcmp(name
, element
->name
) == 0) { 
 855         index 
= static_cast<ssize_t
>(number
); 
 856         if (index 
!= number 
|| index 
< 0 || static_cast<size_t>(index
) >= count
) 
 861     ffi_type 
**elements(typical
->GetFFI()->elements
); 
 863     base 
= reinterpret_cast<uint8_t *>(internal
->value_
); 
 864     for (ssize_t 
local(0); local 
!= index
; ++local
) 
 865         base 
+= elements
[local
]->size
; 
 870 static JSValueRef 
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
 872     Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(object
))); 
 874     if (JSStringIsEqual(property
, length_s
)) 
 875         return internal
->length_ 
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
); 
 877     Type_privateData 
*typical(internal
->type_
); 
 878     if (typical
->type_ 
== NULL
) 
 880     sig::Type 
&type(*typical
->type_
); 
 883     if (JSStringIsEqualToUTF8CString(property
, "$cyi")) 
 885     else if (!CYGetOffset(pool
, context
, property
, offset
)) 
 888     if (type
.primitive 
== sig::function_P
) 
 889         return CYMakeFunctor(context
, reinterpret_cast<void (*)()>(internal
->value_
), type
.data
.signature
); 
 891     ffi_type 
*ffi(typical
->GetFFI()); 
 893     uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
)); 
 894     base 
+= ffi
->size 
* offset
; 
 896     JSObjectRef 
owner(internal
->GetOwner() ?: object
); 
 897     return CYFromFFI(context
, &type
, ffi
, base
, false, owner
); 
 900 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef 
*exception
) { CYTry 
{ 
 902     Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(object
))); 
 903     Type_privateData 
*typical(internal
->type_
); 
 905     if (typical
->type_ 
== NULL
) 
 909     if (JSStringIsEqualToUTF8CString(property
, "$cyi")) 
 911     else if (!CYGetOffset(pool
, context
, property
, offset
)) 
 914     ffi_type 
*ffi(typical
->GetFFI()); 
 916     uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
)); 
 917     base 
+= ffi
->size 
* offset
; 
 919     CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
); 
 923 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 924     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(_this
))); 
 925     Type_privateData 
*typical(internal
->type_
); 
 926     return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
); 
 929 static JSValueRef 
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
 931     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(object
))); 
 932     Type_privateData 
*typical(internal
->type_
); 
 937     if (!Index_(pool
, context
, internal
, property
, index
, base
)) 
 940     JSObjectRef 
owner(internal
->GetOwner() ?: object
); 
 942     return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
); 
 945 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef 
*exception
) { CYTry 
{ 
 947     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(object
))); 
 948     Type_privateData 
*typical(internal
->type_
); 
 953     if (!Index_(pool
, context
, internal
, property
, index
, base
)) 
 956     CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
); 
 960 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) { 
 961     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(object
))); 
 962     Type_privateData 
*typical(internal
->type_
); 
 963     sig::Type 
*type(typical
->type_
); 
 968     size_t count(type
->data
.signature
.count
); 
 969     sig::Element 
*elements(type
->data
.signature
.elements
); 
 973     for (size_t index(0); index 
!= count
; ++index
) { 
 975         name 
= elements
[index
].name
; 
 978             sprintf(number
, "%zu", index
); 
 982         JSPropertyNameAccumulatorAddName(names
, CYJSString(name
)); 
 986 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
)()) { 
 987     if (setups 
+ count 
!= signature
->count 
- 1) 
 988         throw CYJSError(context
, "incorrect number of arguments to ffi function"); 
 990     size_t size(setups 
+ count
); 
 992     memcpy(values
, setup
, sizeof(void *) * setups
); 
 994     for (size_t index(setups
); index 
!= size
; ++index
) { 
 995         sig::Element 
*element(&signature
->elements
[index 
+ 1]); 
 996         ffi_type 
*ffi(cif
->arg_types
[index
]); 
 998         values
[index
] = new(pool
) uint8_t[ffi
->size
]; 
 999         CYPoolFFI(&pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index 
- setups
]); 
1002     uint8_t value
[cif
->rtype
->size
]; 
1004     for (CYHook 
*hook 
: GetHooks()) 
1005         if (hook
->CallFunction 
!= NULL
) { 
1006             // XXX: this only supports one hook, but it is a bad idea anyway 
1007             (*hook
->CallFunction
)(context
, cif
, function
, value
, values
); 
1010     ffi_call(cif
, function
, value
, values
); 
1013     return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
); 
1016 static JSValueRef 
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1018     cy::Functor 
*internal(reinterpret_cast<cy::Functor 
*>(JSObjectGetPrivate(object
))); 
1019     return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, &internal
->signature_
, &internal
->cif_
, internal
->GetValue()); 
1022 JSObjectRef 
CYMakeType(JSContextRef context
, const char *encoding
) { 
1023     Type_privateData 
*internal(new Type_privateData(encoding
)); 
1024     return JSObjectMake(context
, Type_privateData::Class_
, internal
); 
1027 JSObjectRef 
CYMakeType(JSContextRef context
, sig::Type 
*type
) { 
1028     Type_privateData 
*internal(new Type_privateData(type
)); 
1029     return JSObjectMake(context
, Type_privateData::Class_
, internal
); 
1032 JSObjectRef 
CYMakeType(JSContextRef context
, sig::Signature 
*signature
) { 
1039     type
.primitive 
= sig::function_P
; 
1040     sig::Copy(pool
, type
.data
.signature
, *signature
); 
1042     return CYMakeType(context
, &type
); 
1045 static bool All_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) { 
1046     JSObjectRef 
global(CYGetGlobalObject(context
)); 
1047     JSObjectRef 
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript")))); 
1048     JSObjectRef 
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls")))); 
1050     for (size_t i(0), count(CYArrayLength(context
, alls
)); i 
!= count
; ++i
) 
1051         if (JSObjectRef space 
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count 
- i 
- 1))) 
1052             if (CYHasProperty(context
, space
, property
)) 
1056     CYUTF8String 
name(CYPoolUTF8String(pool
, context
, property
)); 
1058     size_t length(name
.size
); 
1059     char keyed
[length 
+ 2]; 
1060     memcpy(keyed 
+ 1, name
.data
, length 
+ 1); 
1062     static const char *modes 
= "0124"; 
1063     for (size_t i(0); i 
!= 4; ++i
) { 
1064         keyed
[0] = modes
[i
]; 
1065         if (CYBridgeHash(keyed
, length 
+ 1) != NULL
) 
1072 static JSValueRef 
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
1073     JSObjectRef 
global(CYGetGlobalObject(context
)); 
1074     JSObjectRef 
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript")))); 
1075     JSObjectRef 
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls")))); 
1077     for (size_t i(0), count(CYArrayLength(context
, alls
)); i 
!= count
; ++i
) 
1078         if (JSObjectRef space 
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count 
- i 
- 1))) 
1079             if (JSValueRef value 
= CYGetProperty(context
, space
, property
)) 
1080                 if (!JSValueIsUndefined(context
, value
)) 
1084     CYUTF8String 
name(CYPoolUTF8String(pool
, context
, property
)); 
1086     size_t length(name
.size
); 
1087     char keyed
[length 
+ 2]; 
1088     memcpy(keyed 
+ 1, name
.data
, length 
+ 1); 
1090     static const char *modes 
= "0124"; 
1091     for (size_t i(0); i 
!= 4; ++i
) { 
1092         char mode(modes
[i
]); 
1095         if (CYBridgeEntry 
*entry 
= CYBridgeHash(keyed
, length 
+ 1)) 
1098                     return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
); 
1101                     return CYMakeFunctor(context
, name
.data
, entry
->value_
, &entry
->cache_
); 
1104                     if (void *symbol 
= CYCastSymbol(name
.data
)) { 
1105                         // XXX: this is horrendously inefficient 
1106                         sig::Signature signature
; 
1107                         sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
); 
1109                         sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
); 
1110                         return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
); 
1113                 // XXX: implement case 3 
1115                     return CYMakeType(context
, entry
->value_
); 
1122 static void All_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) { 
1123     JSObjectRef 
global(CYGetGlobalObject(context
)); 
1124     JSObjectRef 
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript")))); 
1125     JSObjectRef 
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls")))); 
1127     for (size_t i(0), count(CYArrayLength(context
, alls
)); i 
!= count
; ++i
) 
1128         if (JSObjectRef space 
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count 
- i 
- 1))) { 
1129             JSPropertyNameArrayRef 
subset(JSObjectCopyPropertyNames(context
, space
)); 
1130             for (size_t index(0), count(JSPropertyNameArrayGetCount(subset
)); index 
!= count
; ++index
) 
1131                 JSPropertyNameAccumulatorAddName(names
, JSPropertyNameArrayGetNameAtIndex(subset
, index
)); 
1132             JSPropertyNameArrayRelease(subset
); 
1136 static JSObjectRef 
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1138         throw CYJSError(context
, "incorrect number of arguments to Pointer constructor"); 
1142     void *value(CYCastPointer
<void *>(context
, arguments
[0])); 
1143     const char *type(CYPoolCString(pool
, context
, arguments
[1])); 
1145     sig::Signature signature
; 
1146     sig::Parse(pool
, &signature
, type
, &Structor_
); 
1148     return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
); 
1151 static JSObjectRef 
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1153         throw CYJSError(context
, "incorrect number of arguments to Type constructor"); 
1155     const char *type(CYPoolCString(pool
, context
, arguments
[0])); 
1156     return CYMakeType(context
, type
); 
1159 static JSValueRef Type_callAsFunction_$
With(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], sig::Primitive primitive
, JSValueRef 
*exception
) { CYTry 
{ 
1160     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1168     type
.primitive 
= primitive
; 
1169     type
.data
.signature
.elements 
= new(pool
) sig::Element
[1 + count
]; 
1170     type
.data
.signature
.count 
= 1 + count
; 
1172     type
.data
.signature
.elements
[0].name 
= NULL
; 
1173     type
.data
.signature
.elements
[0].type 
= internal
->type_
; 
1174     type
.data
.signature
.elements
[0].offset 
= _not(size_t); 
1176     for (size_t i(0); i 
!= count
; ++i
) { 
1177         sig::Element 
&element(type
.data
.signature
.elements
[i 
+ 1]); 
1178         element
.name 
= NULL
; 
1179         element
.offset 
= _not(size_t); 
1181         JSObjectRef 
object(CYCastJSObject(context
, arguments
[i
])); 
1182         _assert(JSValueIsObjectOfClass(context
, object
, Type_privateData::Class_
)); 
1183         Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1185         element
.type 
= internal
->type_
; 
1188     return CYMakeType(context
, &type
); 
1191 static JSValueRef 
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1193         throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf"); 
1194     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1197     size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0]))); 
1198     if (index 
== _not(size_t)) 
1199         throw CYJSError(context
, "invalid array size used with Type.arrayOf"); 
1205     type
.primitive 
= sig::array_P
; 
1206     type
.data
.data
.type 
= internal
->type_
; 
1207     type
.data
.data
.size 
= index
; 
1209     return CYMakeType(context
, &type
); 
1212 static JSValueRef 
Type_callAsFunction_blockWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
1213     return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::block_P
, exception
); 
1216 static JSValueRef 
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1218         throw CYJSError(context
, "incorrect number of arguments to Type.constant"); 
1219     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1221     sig::Type 
type(*internal
->type_
); 
1222     type
.flags 
|= JOC_TYPE_CONST
; 
1223     return CYMakeType(context
, &type
); 
1226 static JSValueRef 
Type_callAsFunction_long(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1228         throw CYJSError(context
, "incorrect number of arguments to Type.long"); 
1229     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1231     sig::Type 
type(*internal
->type_
); 
1233     switch (type
.primitive
) { 
1234         case sig::short_P
: type
.primitive 
= sig::int_P
; break; 
1235         case sig::int_P
: type
.primitive 
= sig::long_P
; break; 
1236         case sig::long_P
: type
.primitive 
= sig::longlong_P
; break; 
1237         default: throw CYJSError(context
, "invalid type argument to Type.long"); 
1240     return CYMakeType(context
, &type
); 
1243 static JSValueRef 
Type_callAsFunction_short(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1245         throw CYJSError(context
, "incorrect number of arguments to Type.short"); 
1246     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1248     sig::Type 
type(*internal
->type_
); 
1250     switch (type
.primitive
) { 
1251         case sig::int_P
: type
.primitive 
= sig::short_P
; break; 
1252         case sig::long_P
: type
.primitive 
= sig::int_P
; break; 
1253         case sig::longlong_P
: type
.primitive 
= sig::long_P
; break; 
1254         default: throw CYJSError(context
, "invalid type argument to Type.short"); 
1257     return CYMakeType(context
, &type
); 
1260 static JSValueRef 
Type_callAsFunction_signed(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1262         throw CYJSError(context
, "incorrect number of arguments to Type.signed"); 
1263     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1265     sig::Type 
type(*internal
->type_
); 
1267     switch (type
.primitive
) { 
1268         case sig::char_P
: case sig::uchar_P
: type
.primitive 
= sig::char_P
; break; 
1269         case sig::short_P
: case sig::ushort_P
: type
.primitive 
= sig::short_P
; break; 
1270         case sig::int_P
: case sig::uint_P
: type
.primitive 
= sig::int_P
; break; 
1271         case sig::long_P
: case sig::ulong_P
: type
.primitive 
= sig::long_P
; break; 
1272         case sig::longlong_P
: case sig::ulonglong_P
: type
.primitive 
= sig::longlong_P
; break; 
1273         default: throw CYJSError(context
, "invalid type argument to Type.signed"); 
1276     return CYMakeType(context
, &type
); 
1279 static JSValueRef 
Type_callAsFunction_unsigned(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1281         throw CYJSError(context
, "incorrect number of arguments to Type.unsigned"); 
1282     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1284     sig::Type 
type(*internal
->type_
); 
1286     switch (type
.primitive
) { 
1287         case sig::char_P
: case sig::uchar_P
: type
.primitive 
= sig::uchar_P
; break; 
1288         case sig::short_P
: case sig::ushort_P
: type
.primitive 
= sig::ushort_P
; break; 
1289         case sig::int_P
: case sig::uint_P
: type
.primitive 
= sig::uint_P
; break; 
1290         case sig::long_P
: case sig::ulong_P
: type
.primitive 
= sig::ulong_P
; break; 
1291         case sig::longlong_P
: case sig::ulonglong_P
: type
.primitive 
= sig::ulonglong_P
; break; 
1292         default: throw CYJSError(context
, "invalid type argument to Type.unsigned"); 
1295     return CYMakeType(context
, &type
); 
1298 static JSValueRef 
Type_callAsFunction_functionWith(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
1299     return Type_callAsFunction_$
With(context
, object
, _this
, count
, arguments
, sig::function_P
, exception
); 
1302 static JSValueRef 
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1304         throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo"); 
1305     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1310     if (internal
->type_
->primitive 
== sig::char_P
) { 
1311         type
.flags 
= internal
->type_
->flags
; 
1312         type
.primitive 
= sig::string_P
; 
1313         type
.data
.data
.type 
= NULL
; 
1314         type
.data
.data
.size 
= 0; 
1317         type
.primitive 
= sig::pointer_P
; 
1318         type
.data
.data
.type 
= internal
->type_
; 
1319         type
.data
.data
.size 
= 0; 
1322     return CYMakeType(context
, &type
); 
1325 static JSValueRef 
Type_callAsFunction_withName(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1327         throw CYJSError(context
, "incorrect number of arguments to Type.withName"); 
1328     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1331     const char *name(CYPoolCString(pool
, context
, arguments
[0])); 
1333     sig::Type 
type(*internal
->type_
); 
1335     return CYMakeType(context
, &type
); 
1338 static JSValueRef 
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1340         throw CYJSError(context
, "incorrect number of arguments to type cast function"); 
1341     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1343     if (internal
->type_
->primitive 
== sig::function_P
) 
1344         return CYMakeFunctor(context
, arguments
[0], internal
->type_
->data
.signature
); 
1346     sig::Type 
*type(internal
->type_
); 
1347     ffi_type 
*ffi(internal
->GetFFI()); 
1349     uint8_t value
[ffi
->size
]; 
1351     CYPoolFFI(&pool
, context
, type
, ffi
, value
, arguments
[0]); 
1352     return CYFromFFI(context
, type
, ffi
, value
); 
1355 static JSObjectRef 
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1357         throw CYJSError(context
, "incorrect number of arguments to Type allocator"); 
1358     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1360     sig::Type 
*type(internal
->type_
); 
1363     if (type
->primitive 
!= sig::array_P
) 
1364         length 
= _not(size_t); 
1366         length 
= type
->data
.data
.size
; 
1367         type 
= type
->data
.data
.type
; 
1370     void *value(calloc(1, internal
->GetFFI()->size
)); 
1371     return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
); 
1374 static JSObjectRef 
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1376         throw CYJSError(context
, "incorrect number of arguments to Functor constructor"); 
1378     const char *encoding(CYPoolCString(pool
, context
, arguments
[1])); 
1379     sig::Signature signature
; 
1380     sig::Parse(pool
, &signature
, encoding
, &Structor_
); 
1381     return CYMakeFunctor(context
, arguments
[0], signature
); 
1384 static JSValueRef 
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1385     CYValue 
*internal(reinterpret_cast<CYValue 
*>(JSObjectGetPrivate(_this
))); 
1386     return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
)); 
1389 static JSValueRef 
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
1390     return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
); 
1393 static JSValueRef 
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1394     CYValue 
*internal(reinterpret_cast<CYValue 
*>(JSObjectGetPrivate(_this
))); 
1396     sprintf(string
, "%p", internal
->value_
); 
1397     return CYCastJSValue(context
, string
); 
1400 static JSValueRef 
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1401     std::set
<void *> *objects(CYCastObjects(context
, _this
, count
, arguments
)); 
1403     Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(_this
))); 
1404     if (internal
->length_ 
!= _not(size_t)) { 
1405         JSObjectRef 
Array(CYGetCachedObject(context
, CYJSString("Array_prototype"))); 
1406         JSObjectRef 
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
))); 
1407         return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
); 
1408     } else if (internal
->type_
->type_ 
== NULL
) pointer
: { 
1410         sprintf(string
, "%p", internal
->value_
); 
1411         return CYCastJSValue(context
, string
); 
1413         JSValueRef 
value(CYGetProperty(context
, _this
, cyi_s
)); 
1414         if (JSValueIsUndefined(context
, value
)) 
1417         return CYCastJSValue(context
, pool
.strcat("&", CYPoolCCYON(pool
, context
, value
, objects
), NULL
)); 
1418     } catch (const CYException 
&e
) { 
1423 static JSValueRef 
Pointer_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
1424     Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(object
))); 
1425     return CYMakeType(context
, internal
->type_
->type_
); 
1428 static JSValueRef 
Functor_getProperty_type(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
1429     cy::Functor 
*internal(reinterpret_cast<cy::Functor 
*>(JSObjectGetPrivate(object
))); 
1430     return CYMakeType(context
, &internal
->signature_
); 
1433 static JSValueRef 
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
1434     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1435     return CYCastJSValue(context
, internal
->GetFFI()->alignment
); 
1438 static JSValueRef 
Type_getProperty_name(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
1439     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1440     return CYCastJSValue(context
, internal
->type_
->name
); 
1443 static JSValueRef 
Type_getProperty_size(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
1444     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1445     return CYCastJSValue(context
, internal
->GetFFI()->size
); 
1448 static JSValueRef 
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1449     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1451     const char *type(sig::Unparse(pool
, internal
->type_
)); 
1452     return CYCastJSValue(context
, CYJSString(type
)); 
1455 static JSValueRef 
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1456     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1458     std::ostringstream out
; 
1460     CYOutput 
output(out
, options
); 
1461     (new(pool
) CYEncodedType(Decode(pool
, internal
->type_
)))->Output(output
, CYNoFlags
); 
1462     return CYCastJSValue(context
, CYJSString(out
.str().c_str())); 
1465 static JSValueRef 
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
1466     return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
); 
1469 static JSStaticFunction Pointer_staticFunctions
[4] = { 
1470     {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1471     {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1472     {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1476 static JSStaticValue Pointer_staticValues
[2] = { 
1477     {"type", &Pointer_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly 
| kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1478     {NULL
, NULL
, NULL
, 0} 
1481 static JSStaticFunction Struct_staticFunctions
[2] = { 
1482     {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1486 static JSStaticFunction Functor_staticFunctions
[4] = { 
1487     {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1488     {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1489     {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1494     JSStaticFunction 
const * const Functor::StaticFunctions 
= Functor_staticFunctions
; 
1497 static JSStaticValue Functor_staticValues
[2] = { 
1498     {"type", &Functor_getProperty_type
, NULL
, kJSPropertyAttributeReadOnly 
| kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1499     {NULL
, NULL
, NULL
, 0} 
1503     JSStaticValue 
const * const Functor::StaticValues 
= Functor_staticValues
; 
1506 static JSStaticValue Type_staticValues
[4] = { 
1507     {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly 
| kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1508     {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly 
| kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1509     {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly 
| kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1510     {NULL
, NULL
, NULL
, 0} 
1513 static JSStaticFunction Type_staticFunctions
[14] = { 
1514     {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1515     {"blockWith", &Type_callAsFunction_blockWith
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1516     {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1517     {"functionWith", &Type_callAsFunction_functionWith
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1518     {"long", &Type_callAsFunction_long
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1519     {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1520     {"short", &Type_callAsFunction_short
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1521     {"signed", &Type_callAsFunction_signed
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1522     {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1523     {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1524     {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1525     {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1526     {"unsigned", &Type_callAsFunction_unsigned
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1530 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef 
*); 
1532 void CYSetArgs(int argc
, const char *argv
[]) { 
1533     JSContextRef 
context(CYGetJSContext()); 
1534     JSValueRef args
[argc
]; 
1535     for (int i(0); i 
!= argc
; ++i
) 
1536         args
[i
] = CYCastJSValue(context
, argv
[i
]); 
1539     if (JSObjectMakeArray$ 
!= NULL
) 
1540         array 
= _jsccall(*JSObjectMakeArray$
, context
, argc
, args
); 
1542         JSObjectRef 
Array(CYGetCachedObject(context
, CYJSString("Array"))); 
1543         JSValueRef 
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
)); 
1544         array 
= CYCastJSObject(context
, value
); 
1547     JSObjectRef 
System(CYGetCachedObject(context
, CYJSString("System"))); 
1548     CYSetProperty(context
, System
, CYJSString("args"), array
); 
1551 JSObjectRef 
CYGetGlobalObject(JSContextRef context
) { 
1552     return JSContextGetGlobalObject(context
); 
1555 // XXX: this is neither exceptin safe nor even terribly sane 
1556 class ExecutionHandle 
{ 
1558     JSContextRef context_
; 
1559     std::vector
<void *> handles_
; 
1562     ExecutionHandle(JSContextRef context
) : 
1565         handles_
.resize(GetHooks().size()); 
1566         for (size_t i(0); i 
!= GetHooks().size(); ++i
) { 
1567             CYHook 
*hook(GetHooks()[i
]); 
1568             if (hook
->ExecuteStart 
!= NULL
) 
1569                 handles_
[i
] = (*hook
->ExecuteStart
)(context_
); 
1575     ~ExecutionHandle() { 
1576         for (size_t i(GetHooks().size()); i 
!= 0; --i
) { 
1577             CYHook 
*hook(GetHooks()[i
-1]); 
1578             if (hook
->ExecuteEnd 
!= NULL
) 
1579                 (*hook
->ExecuteEnd
)(context_
, handles_
[i
-1]); 
1584 const char *CYExecute(JSContextRef context
, CYPool 
&pool
, CYUTF8String code
) { 
1585     JSValueRef 
exception(NULL
); 
1587     ExecutionHandle 
handle(context
); 
1589     JSValueRef result
; try { 
1590         result 
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
); 
1591     } catch (const char *error
) { 
1595     if (exception 
!= NULL
) error
: 
1596         return CYPoolCString(pool
, context
, CYJSString(context
, exception
)); 
1598     if (JSValueIsUndefined(context
, result
)) 
1601     const char *json
; try { 
1602         std::set
<void *> objects
; 
1603         json 
= CYPoolCCYON(pool
, context
, result
, objects
, &exception
); 
1604     } catch (const char *error
) { 
1608     if (exception 
!= NULL
) 
1611     CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
); 
1616 static bool initialized_ 
= false; 
1618 void CYInitializeDynamic() { 
1620         initialized_ 
= true; 
1623     JSObjectMakeArray$ 
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef 
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray")); 
1624     JSSynchronousGarbageCollectForDebugging$ 
= reinterpret_cast<void (*)(JSContextRef
)>(dlsym(RTLD_DEFAULT
, "JSSynchronousGarbageCollectForDebugging")); 
1626     JSClassDefinition definition
; 
1628     definition 
= kJSClassDefinitionEmpty
; 
1629     definition
.className 
= "All"; 
1630     definition
.hasProperty 
= &All_hasProperty
; 
1631     definition
.getProperty 
= &All_getProperty
; 
1632     definition
.getPropertyNames 
= &All_getPropertyNames
; 
1633     All_ 
= JSClassCreate(&definition
); 
1635     definition 
= kJSClassDefinitionEmpty
; 
1636     definition
.className 
= "Context"; 
1637     definition
.finalize 
= &CYFinalize
; 
1638     Context_ 
= JSClassCreate(&definition
); 
1640     definition 
= kJSClassDefinitionEmpty
; 
1641     definition
.className 
= "Functor"; 
1642     definition
.staticFunctions 
= cy::Functor::StaticFunctions
; 
1643     definition
.staticValues 
= Functor_staticValues
; 
1644     definition
.callAsFunction 
= &Functor_callAsFunction
; 
1645     definition
.finalize 
= &CYFinalize
; 
1646     Functor_ 
= JSClassCreate(&definition
); 
1648     definition 
= kJSClassDefinitionEmpty
; 
1649     definition
.className 
= "Pointer"; 
1650     definition
.staticFunctions 
= Pointer_staticFunctions
; 
1651     definition
.staticValues 
= Pointer_staticValues
; 
1652     definition
.getProperty 
= &Pointer_getProperty
; 
1653     definition
.setProperty 
= &Pointer_setProperty
; 
1654     definition
.finalize 
= &CYFinalize
; 
1655     Pointer_ 
= JSClassCreate(&definition
); 
1657     definition 
= kJSClassDefinitionEmpty
; 
1658     definition
.className 
= "Struct"; 
1659     definition
.staticFunctions 
= Struct_staticFunctions
; 
1660     definition
.getProperty 
= &Struct_getProperty
; 
1661     definition
.setProperty 
= &Struct_setProperty
; 
1662     definition
.getPropertyNames 
= &Struct_getPropertyNames
; 
1663     definition
.finalize 
= &CYFinalize
; 
1664     Struct_ 
= JSClassCreate(&definition
); 
1666     definition 
= kJSClassDefinitionEmpty
; 
1667     definition
.className 
= "Type"; 
1668     definition
.staticValues 
= Type_staticValues
; 
1669     definition
.staticFunctions 
= Type_staticFunctions
; 
1670     definition
.callAsFunction 
= &Type_callAsFunction
; 
1671     definition
.callAsConstructor 
= &Type_callAsConstructor
; 
1672     definition
.finalize 
= &CYFinalize
; 
1673     Type_privateData::Class_ 
= JSClassCreate(&definition
); 
1675     definition 
= kJSClassDefinitionEmpty
; 
1676     definition
.className 
= "Global"; 
1677     //definition.getProperty = &Global_getProperty; 
1678     Global_ 
= JSClassCreate(&definition
); 
1680     Array_s 
= JSStringCreateWithUTF8CString("Array"); 
1681     cy_s 
= JSStringCreateWithUTF8CString("$cy"); 
1682     cyi_s 
= JSStringCreateWithUTF8CString("$cyi"); 
1683     length_s 
= JSStringCreateWithUTF8CString("length"); 
1684     message_s 
= JSStringCreateWithUTF8CString("message"); 
1685     name_s 
= JSStringCreateWithUTF8CString("name"); 
1686     pop_s 
= JSStringCreateWithUTF8CString("pop"); 
1687     prototype_s 
= JSStringCreateWithUTF8CString("prototype"); 
1688     push_s 
= JSStringCreateWithUTF8CString("push"); 
1689     splice_s 
= JSStringCreateWithUTF8CString("splice"); 
1690     toCYON_s 
= JSStringCreateWithUTF8CString("toCYON"); 
1691     toJSON_s 
= JSStringCreateWithUTF8CString("toJSON"); 
1692     toPointer_s 
= JSStringCreateWithUTF8CString("toPointer"); 
1693     toString_s 
= JSStringCreateWithUTF8CString("toString"); 
1695     Result_ 
= JSStringCreateWithUTF8CString("_"); 
1697     for (CYHook 
*hook 
: GetHooks()) 
1698         if (hook
->Initialize 
!= NULL
) 
1699             (*hook
->Initialize
)(); 
1702 void CYThrow(JSContextRef context
, JSValueRef value
) { 
1704         throw CYJSError(context
, value
); 
1707 const char *CYJSError::PoolCString(CYPool 
&pool
) const { 
1708     std::set
<void *> objects
; 
1709     // XXX: this used to be CYPoolCString 
1710     return CYPoolCCYON(pool
, context_
, value_
, objects
); 
1713 JSValueRef 
CYJSError::CastJSValue(JSContextRef context
) const { 
1714     // XXX: what if the context is different? 
1718 JSValueRef 
CYCastJSError(JSContextRef context
, const char *message
) { 
1719     JSObjectRef 
Error(CYGetCachedObject(context
, CYJSString("Error"))); 
1720     JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)}; 
1721     return _jsccall(JSObjectCallAsConstructor
, context
, Error
, 1, arguments
); 
1724 JSValueRef 
CYPoolError::CastJSValue(JSContextRef context
) const { 
1725     return CYCastJSError(context
, message_
); 
1728 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) { 
1729     _assert(context 
!= NULL
); 
1734     va_start(args
, format
); 
1735     // XXX: there might be a beter way to think about this 
1736     const char *message(pool
.vsprintf(64, format
, args
)); 
1739     value_ 
= CYCastJSError(context
, message
); 
1742 JSGlobalContextRef 
CYGetJSContext(JSContextRef context
) { 
1743     return reinterpret_cast<Context 
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
; 
1746 extern "C" bool CydgetMemoryParse(const uint16_t **data
, size_t *size
); 
1748 void *CYMapFile(const char *path
, size_t *psize
) { 
1749     int fd(_syscall_(open(path
, O_RDONLY
), 1, {ENOENT
})); 
1754     _syscall(fstat(fd
, &stat
)); 
1755     size_t size(stat
.st_size
); 
1760     _syscall(base 
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, 0)); 
1762     _syscall(close(fd
)); 
1766 static JSValueRef 
require(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1767     _assert(count 
== 1); 
1771     _assert(dladdr(reinterpret_cast<void *>(&require
), &addr
) != 0); 
1772     char *lib(pool
.strdup(addr
.dli_fname
)); 
1774     char *slash(strrchr(lib
, '/')); 
1775     _assert(slash 
!= NULL
); 
1778     CYJSString 
property("exports"); 
1781     const char *name(CYPoolCString(pool
, context
, arguments
[0])); 
1782     const char *path(pool
.strcat(lib
, "/cycript0.9/", name
, ".cy", NULL
)); 
1784     CYJSString 
key(path
); 
1785     JSObjectRef 
modules(CYGetCachedObject(context
, CYJSString("modules"))); 
1786     JSValueRef 
cache(CYGetProperty(context
, modules
, key
)); 
1788     if (!JSValueIsUndefined(context
, cache
)) 
1789         module = CYCastJSObject(context
, cache
); 
1792         code
.data 
= reinterpret_cast<char *>(CYMapFile(path
, &code
.size
)); 
1794         if (code
.data 
== NULL
) { 
1795             if (strchr(name
, '/') == NULL 
&& ( 
1797                 dlopen(pool
.strcat("/System/Library/Frameworks/", name
, ".framework/", name
, NULL
), RTLD_LAZY 
| RTLD_GLOBAL
) != NULL 
|| 
1798                 dlopen(pool
.strcat("/System/Library/PrivateFrameworks/", name
, ".framework/", name
, NULL
), RTLD_LAZY 
| RTLD_GLOBAL
) != NULL 
|| 
1801                 return CYJSUndefined(NULL
); 
1803             CYThrow("Can't find module: %s", name
); 
1806         module = JSObjectMake(context
, NULL
, NULL
); 
1807         CYSetProperty(context
, modules
, key
, module); 
1809         JSObjectRef 
exports(JSObjectMake(context
, NULL
, NULL
)); 
1810         CYSetProperty(context
, module, property
, exports
); 
1812         std::stringstream wrap
; 
1813         wrap 
<< "(function (exports, require, module) { " << code 
<< "\n});"; 
1814         code 
= CYPoolCode(pool
, wrap
); 
1816         JSValueRef 
value(_jsccall(JSEvaluateScript
, context
, CYJSString(code
), NULL
, NULL
, 0)); 
1817         JSObjectRef 
function(CYCastJSObject(context
, value
)); 
1819         JSValueRef arguments
[3] = { exports
, JSObjectMakeFunctionWithCallback(context
, CYJSString("require"), &require
), module }; 
1820         CYCallAsFunction(context
, function
, NULL
, 3, arguments
); 
1823     return CYGetProperty(context
, module, property
); 
1826 extern "C" void CYSetupContext(JSGlobalContextRef context
) { 
1827     CYInitializeDynamic(); 
1829     JSObjectRef 
global(CYGetGlobalObject(context
)); 
1831     JSObjectRef 
cy(JSObjectMake(context
, Context_
, new Context(context
))); 
1832     CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
); 
1834 /* Cache Globals {{{ */ 
1835     JSObjectRef 
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array")))); 
1836     CYSetProperty(context
, cy
, CYJSString("Array"), Array
); 
1838     JSObjectRef 
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
))); 
1839     CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
); 
1841     JSObjectRef 
Boolean(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Boolean")))); 
1842     CYSetProperty(context
, cy
, CYJSString("Boolean"), Boolean
); 
1844     JSObjectRef 
Boolean_prototype(CYCastJSObject(context
, CYGetProperty(context
, Boolean
, prototype_s
))); 
1845     CYSetProperty(context
, cy
, CYJSString("Boolean_prototype"), Boolean_prototype
); 
1847     JSObjectRef 
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error")))); 
1848     CYSetProperty(context
, cy
, CYJSString("Error"), Error
); 
1850     JSObjectRef 
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function")))); 
1851     CYSetProperty(context
, cy
, CYJSString("Function"), Function
); 
1853     JSObjectRef 
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
))); 
1854     CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
); 
1856     JSObjectRef 
Number(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Number")))); 
1857     CYSetProperty(context
, cy
, CYJSString("Number"), Number
); 
1859     JSObjectRef 
Number_prototype(CYCastJSObject(context
, CYGetProperty(context
, Number
, prototype_s
))); 
1860     CYSetProperty(context
, cy
, CYJSString("Number_prototype"), Number_prototype
); 
1862     JSObjectRef 
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object")))); 
1863     CYSetProperty(context
, cy
, CYJSString("Object"), Object
); 
1865     JSObjectRef 
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
))); 
1866     CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
); 
1868     JSObjectRef 
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String")))); 
1869     CYSetProperty(context
, cy
, CYJSString("String"), String
); 
1871     JSObjectRef 
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
))); 
1872     CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
); 
1875     CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
); 
1876     CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
); 
1878     JSObjectRef 
cycript(JSObjectMake(context
, NULL
, NULL
)); 
1879     CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
); 
1880     CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
); 
1882     JSObjectRef 
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
)); 
1883     CYSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
); 
1884     CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
); 
1886     CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
)); 
1887     CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
)); 
1889     JSObjectRef 
modules(JSObjectMake(context
, NULL
, NULL
)); 
1890     CYSetProperty(context
, cy
, CYJSString("modules"), modules
); 
1892     JSObjectRef 
all(JSObjectMake(context
, All_
, NULL
)); 
1893     CYSetProperty(context
, cycript
, CYJSString("all"), all
); 
1895     JSObjectRef 
alls(_jsccall(JSObjectCallAsConstructor
, context
, Array
, 0, NULL
)); 
1896     CYSetProperty(context
, cycript
, CYJSString("alls"), alls
); 
1899         JSObjectRef 
last(NULL
), curr(global
); 
1901         goto next
; for (JSValueRef next
;;) { 
1902             if (JSValueIsNull(context
, next
)) 
1905             curr 
= CYCastJSObject(context
, next
); 
1907             next 
= JSObjectGetPrototype(context
, curr
); 
1910         CYSetPrototype(context
, last
, all
); 
1913     CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
, kJSPropertyAttributeDontEnum
); 
1915     JSObjectRef 
System(JSObjectMake(context
, NULL
, NULL
)); 
1916     CYSetProperty(context
, cy
, CYJSString("System"), System
); 
1918     CYSetProperty(context
, all
, CYJSString("require"), &require
, kJSPropertyAttributeDontEnum
); 
1920     CYSetProperty(context
, global
, CYJSString("system"), System
); 
1921     CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
)); 
1922     //CYSetProperty(context, System, CYJSString("global"), global); 
1923     CYSetProperty(context
, System
, CYJSString("print"), &System_print
); 
1925     if (CYBridgeEntry 
*entry 
= CYBridgeHash("1dlerror", 8)) 
1926         entry
->cache_ 
= new cy::Functor(entry
->value_
, reinterpret_cast<void (*)()>(&dlerror
)); 
1928     for (CYHook 
*hook 
: GetHooks()) 
1929         if (hook
->SetupContext 
!= NULL
) 
1930             (*hook
->SetupContext
)(context
); 
1932     CYArrayPush(context
, alls
, cycript
); 
1935 static JSGlobalContextRef context_
; 
1937 JSGlobalContextRef 
CYGetJSContext() { 
1938     CYInitializeDynamic(); 
1940     if (context_ 
== NULL
) { 
1941         context_ 
= JSGlobalContextCreate(Global_
); 
1942         CYSetupContext(context_
); 
1948 void CYDestroyContext() { 
1949     if (context_ 
== NULL
) 
1951     JSGlobalContextRelease(context_
);