1 /* Cycript - Optimizing JavaScript Compiler/Runtime 
   2  * Copyright (C) 2009-2010  Jay Freeman (saurik) 
   5 /* GNU Lesser General Public License, Version 3 {{{ */ 
   7  * Cycript is free software: you can redistribute it and/or modify it under 
   8  * the terms of the GNU Lesser General Public License as published by the 
   9  * Free Software Foundation, either version 3 of the License, or (at your 
  10  * option) any later version. 
  12  * Cycript is distributed in the hope that it will be useful, but WITHOUT 
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
  15  * License for more details. 
  17  * You should have received a copy of the GNU Lesser General Public License 
  18  * along with Cycript.  If not, see <http://www.gnu.org/licenses/>. 
  22 #include "Internal.hpp" 
  27 #include "cycript.hpp" 
  29 #include "sig/parse.hpp" 
  30 #include "sig/ffi_type.hpp" 
  32 #include "Pooling.hpp" 
  33 #include "Execute.hpp" 
  38 #include <ext/stdio_filebuf.h> 
  46 #include "Cycript.tab.hh" 
  49 #include "JavaScript.hpp" 
  52 struct CYHooks 
*hooks_
; 
  54 /* JavaScript Properties {{{ */ 
  55 JSValueRef 
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) { 
  56     JSValueRef 
exception(NULL
); 
  57     JSValueRef 
value(JSObjectGetPropertyAtIndex(context
, object
, index
, &exception
)); 
  58     CYThrow(context
, exception
); 
  62 JSValueRef 
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) { 
  63     JSValueRef 
exception(NULL
); 
  64     JSValueRef 
value(JSObjectGetProperty(context
, object
, name
, &exception
)); 
  65     CYThrow(context
, exception
); 
  69 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) { 
  70     JSValueRef 
exception(NULL
); 
  71     JSObjectSetPropertyAtIndex(context
, object
, index
, value
, &exception
); 
  72     CYThrow(context
, exception
); 
  75 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) { 
  76     JSValueRef 
exception(NULL
); 
  77     JSObjectSetProperty(context
, object
, name
, value
, attributes
, &exception
); 
  78     CYThrow(context
, exception
); 
  81 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef (*callback
)(JSContextRef
, JSObjectRef
, JSObjectRef
, size_t, const JSValueRef
[], JSValueRef 
*), JSPropertyAttributes attributes
) { 
  82     CYSetProperty(context
, object
, name
, JSObjectMakeFunctionWithCallback(context
, name
, callback
), attributes
); 
  85 /* JavaScript Strings {{{ */ 
  86 JSStringRef 
CYCopyJSString(const char *value
) { 
  87     return value 
== NULL 
? NULL 
: JSStringCreateWithUTF8CString(value
); 
  90 JSStringRef 
CYCopyJSString(JSStringRef value
) { 
  91     return value 
== NULL 
? NULL 
: JSStringRetain(value
); 
  94 JSStringRef 
CYCopyJSString(CYUTF8String value
) { 
  95     // XXX: this is very wrong; it needs to convert to UTF16 and then create from there 
  96     return CYCopyJSString(value
.data
); 
  99 JSStringRef 
CYCopyJSString(JSContextRef context
, JSValueRef value
) { 
 100     if (JSValueIsNull(context
, value
)) 
 102     JSValueRef 
exception(NULL
); 
 103     JSStringRef 
string(JSValueToStringCopy(context
, value
, &exception
)); 
 104     CYThrow(context
, exception
); 
 108 static CYUTF16String 
CYCastUTF16String(JSStringRef value
) { 
 109     return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
)); 
 112 CYUTF8String 
CYPoolUTF8String(apr_pool_t 
*pool
, JSContextRef context
, JSStringRef value
) { 
 113     return CYPoolUTF8String(pool
, CYCastUTF16String(value
)); 
 116 const char *CYPoolCString(apr_pool_t 
*pool
, JSContextRef context
, JSStringRef value
) { 
 117     CYUTF8String 
utf8(CYPoolUTF8String(pool
, context
, value
)); 
 118     _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
); 
 122 const char *CYPoolCString(apr_pool_t 
*pool
, JSContextRef context
, JSValueRef value
) { 
 123     return JSValueIsNull(context
, value
) ? NULL 
: CYPoolCString(pool
, context
, CYJSString(context
, value
)); 
 126 /* Index Offsets {{{ */ 
 127 size_t CYGetIndex(apr_pool_t 
*pool
, JSContextRef context
, JSStringRef value
) { 
 128     return CYGetIndex(CYPoolUTF8String(pool
, context
, value
)); 
 132 static JSClassRef All_
; 
 133 static JSClassRef Context_
; 
 134 static JSClassRef Functor_
; 
 135 static JSClassRef Global_
; 
 136 static JSClassRef Pointer_
; 
 137 static JSClassRef Struct_
; 
 141 JSStringRef length_s
; 
 142 JSStringRef message_s
; 
 145 JSStringRef prototype_s
; 
 147 JSStringRef splice_s
; 
 148 JSStringRef toCYON_s
; 
 149 JSStringRef toJSON_s
; 
 150 JSStringRef toPointer_s
; 
 151 JSStringRef toString_s
; 
 153 static JSStringRef Result_
; 
 155 void CYFinalize(JSObjectRef object
) { 
 156     CYData 
*internal(reinterpret_cast<CYData 
*>(JSObjectGetPrivate(object
))); 
 157     if (--internal
->count_ 
== 0) 
 161 void Structor_(apr_pool_t 
*pool
, sig::Type 
*&type
) { 
 163         type
->primitive 
== sig::pointer_P 
&& 
 164         type
->data
.data
.type 
!= NULL 
&& 
 165         type
->data
.data
.type
->primitive 
== sig::struct_P 
&& 
 166         type
->data
.data
.type
->name 
!= NULL 
&& 
 167         strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0 
 169         type
->primitive 
= sig::typename_P
; 
 170         type
->data
.data
.type 
= NULL
; 
 174     if (type
->primitive 
!= sig::struct_P 
|| type
->name 
== NULL
) 
 177     size_t length(strlen(type
->name
)); 
 178     char keyed
[length 
+ 2]; 
 179     memcpy(keyed 
+ 1, type
->name
, length 
+ 1); 
 181     static const char *modes 
= "34"; 
 182     for (size_t i(0); i 
!= 2; ++i
) { 
 186         if (CYBridgeEntry 
*entry 
= CYBridgeHash(keyed
, length 
+ 1)) 
 189                     sig::Parse(pool
, &type
->data
.signature
, entry
->value_
, &Structor_
); 
 193                     sig::Signature signature
; 
 194                     sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
); 
 195                     type 
= signature
.elements
[0].type
; 
 201 JSClassRef 
Type_privateData::Class_
; 
 206     JSGlobalContextRef context_
; 
 208     Context(JSGlobalContextRef context
) : 
 217     Type_privateData 
*type_
; 
 220     Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type 
*type
) : 
 221         CYOwned(value
, context
, owner
), 
 222         type_(new(pool_
) Type_privateData(type
)), 
 228 struct Struct_privateData 
: 
 231     Type_privateData 
*type_
; 
 233     Struct_privateData(JSContextRef context
, JSObjectRef owner
) : 
 234         CYOwned(NULL
, context
, owner
) 
 239 typedef std::map
<const char *, Type_privateData 
*, CYCStringLess
> TypeMap
; 
 240 static TypeMap Types_
; 
 242 JSObjectRef 
CYMakeStruct(JSContextRef context
, void *data
, sig::Type 
*type
, ffi_type 
*ffi
, JSObjectRef owner
) { 
 243     Struct_privateData 
*internal(new Struct_privateData(context
, owner
)); 
 244     apr_pool_t 
*pool(internal
->pool_
); 
 245     Type_privateData 
*typical(new(pool
) Type_privateData(type
, ffi
)); 
 246     internal
->type_ 
= typical
; 
 249         internal
->value_ 
= data
; 
 251         size_t size(typical
->GetFFI()->size
); 
 252         void *copy(apr_palloc(internal
->pool_
, size
)); 
 253         memcpy(copy
, data
, size
); 
 254         internal
->value_ 
= copy
; 
 257     return JSObjectMake(context
, Struct_
, internal
); 
 260 JSValueRef 
CYCastJSValue(JSContextRef context
, bool value
) { 
 261     return JSValueMakeBoolean(context
, value
); 
 264 JSValueRef 
CYCastJSValue(JSContextRef context
, double value
) { 
 265     return JSValueMakeNumber(context
, value
); 
 268 #define CYCastJSValue_(Type_) \ 
 269     JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \ 
 270         return JSValueMakeNumber(context, static_cast<double>(value)); \ 
 274 CYCastJSValue_(unsigned int) 
 275 CYCastJSValue_(long int) 
 276 CYCastJSValue_(long unsigned int) 
 277 CYCastJSValue_(long long int) 
 278 CYCastJSValue_(long long unsigned int) 
 280 JSValueRef 
CYJSUndefined(JSContextRef context
) { 
 281     return JSValueMakeUndefined(context
); 
 284 double CYCastDouble(JSContextRef context
, JSValueRef value
) { 
 285     JSValueRef 
exception(NULL
); 
 286     double number(JSValueToNumber(context
, value
, &exception
)); 
 287     CYThrow(context
, exception
); 
 291 bool CYCastBool(JSContextRef context
, JSValueRef value
) { 
 292     return JSValueToBoolean(context
, value
); 
 295 JSValueRef 
CYJSNull(JSContextRef context
) { 
 296     return JSValueMakeNull(context
); 
 299 JSValueRef 
CYCastJSValue(JSContextRef context
, JSStringRef value
) { 
 300     return value 
== NULL 
? CYJSNull(context
) : JSValueMakeString(context
, value
); 
 303 JSValueRef 
CYCastJSValue(JSContextRef context
, const char *value
) { 
 304     return CYCastJSValue(context
, CYJSString(value
)); 
 307 JSObjectRef 
CYCastJSObject(JSContextRef context
, JSValueRef value
) { 
 308     JSValueRef 
exception(NULL
); 
 309     JSObjectRef 
object(JSValueToObject(context
, value
, &exception
)); 
 310     CYThrow(context
, exception
); 
 314 JSValueRef 
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) { 
 315     JSValueRef 
exception(NULL
); 
 316     JSValueRef 
value(JSObjectCallAsFunction(context
, function
, _this
, count
, arguments
, &exception
)); 
 317     CYThrow(context
, exception
); 
 321 bool CYIsCallable(JSContextRef context
, JSValueRef value
) { 
 322     return value 
!= NULL 
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
); 
 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
) { 
 340     const char *name(apr_psprintf(pool
, "%s%"APR_SIZE_T_FMT
"", CYPoolCString(pool
, context
, arguments
[0]), Nonce_
++)); 
 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
) { 
 345     JSGarbageCollect(context
); 
 346     return CYJSUndefined(context
); 
 349 const char *CYPoolCCYON(apr_pool_t 
*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 apr_pstrmemdup(pool
, 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 apr_pstrmemdup(pool
, value
.c_str(), value
.size()); 
 374             return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
); 
 376             throw CYJSError(context
, "JSValueGetType() == 0x%x", type
); 
 380 const char *CYPoolCCYON(apr_pool_t 
*pool
, JSContextRef context
, JSValueRef value
) { 
 381     JSValueRef 
exception(NULL
); 
 382     const char *cyon(CYPoolCCYON(pool
, context
, value
, &exception
)); 
 383     CYThrow(context
, exception
); 
 387 const char *CYPoolCCYON(apr_pool_t 
*pool
, JSContextRef context
, JSObjectRef object
) { 
 388     JSValueRef 
toCYON(CYGetProperty(context
, object
, toCYON_s
)); 
 389     if (CYIsCallable(context
, toCYON
)) { 
 390         JSValueRef 
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
)); 
 391         _assert(value 
!= NULL
); 
 392         return CYPoolCString(pool
, context
, value
); 
 395     JSValueRef 
toJSON(CYGetProperty(context
, object
, toJSON_s
)); 
 396     if (CYIsCallable(context
, toJSON
)) { 
 397         JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))}; 
 398         JSValueRef 
exception(NULL
); 
 399         const char *cyon(CYPoolCCYON(pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), &exception
)); 
 400         CYThrow(context
, exception
); 
 404     std::ostringstream str
; 
 408     // XXX: this is, sadly, going to leak 
 409     JSPropertyNameArrayRef 
names(JSObjectCopyPropertyNames(context
, object
)); 
 413     for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index 
!= count
; ++index
) { 
 414         JSStringRef 
name(JSPropertyNameArrayGetNameAtIndex(names
, index
)); 
 415         JSValueRef 
value(CYGetProperty(context
, object
, name
)); 
 422         CYUTF8String 
string(CYPoolUTF8String(pool
, context
, name
)); 
 426             CYStringify(str
, string
.data
, string
.size
); 
 428         str 
<< ':' << CYPoolCCYON(pool
, context
, value
); 
 433     JSPropertyNameArrayRelease(names
); 
 435     std::string 
string(str
.str()); 
 436     return apr_pstrmemdup(pool
, string
.c_str(), string
.size()); 
 439 static JSValueRef 
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 441     std::ostringstream str
; 
 445     JSValueRef 
length(CYGetProperty(context
, _this
, length_s
)); 
 448     for (size_t index(0), count(CYCastDouble(context
, length
)); index 
!= count
; ++index
) { 
 449         JSValueRef 
value(CYGetProperty(context
, _this
, index
)); 
 456         if (!JSValueIsUndefined(context
, value
)) 
 457             str 
<< CYPoolCCYON(pool
, context
, value
); 
 466     std::string 
value(str
.str()); 
 467     return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size()))); 
 470 static JSValueRef 
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 472     std::ostringstream str
; 
 474     CYUTF8String 
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
))); 
 475     CYStringify(str
, string
.data
, string
.size
); 
 477     std::string 
value(str
.str()); 
 478     return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size()))); 
 481 JSObjectRef 
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type 
*type
, ffi_type 
*ffi
, JSObjectRef owner
) { 
 482     Pointer 
*internal(new Pointer(pointer
, context
, owner
, length
, type
)); 
 483     return JSObjectMake(context
, Pointer_
, internal
); 
 486 static JSObjectRef 
CYMakeFunctor(JSContextRef context
, void (*function
)(), const char *type
, void **cache 
= NULL
) { 
 487     cy::Functor 
*internal
; 
 489     if (cache 
!= NULL 
&& *cache 
!= NULL
) { 
 490         internal 
= reinterpret_cast<cy::Functor 
*>(*cache
); 
 493         internal 
= new cy::Functor(type
, function
); 
 501     return JSObjectMake(context
, Functor_
, internal
); 
 504 static bool CYGetOffset(apr_pool_t 
*pool
, JSContextRef context
, JSStringRef value
, ssize_t 
&index
) { 
 505     return CYGetOffset(CYPoolCString(pool
, context
, value
), index
); 
 508 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) { 
 509     switch (JSValueGetType(context
, value
)) { 
 512         case kJSTypeObject
: { 
 513             JSObjectRef 
object((JSObjectRef
) value
); 
 514             if (JSValueIsObjectOfClass(context
, value
, Pointer_
)) { 
 515                 Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(object
))); 
 516                 return internal
->value_
; 
 518             JSValueRef 
toPointer(CYGetProperty(context
, object
, toPointer_s
)); 
 519             if (CYIsCallable(context
, toPointer
)) { 
 520                 JSValueRef 
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
)); 
 521                 _assert(value 
!= NULL
); 
 522                 return CYCastPointer_(context
, value
); 
 525             double number(CYCastDouble(context
, value
)); 
 526             if (std::isnan(number
)) 
 527                 throw CYJSError(context
, "cannot convert value to pointer"); 
 528             return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
))); 
 532 void CYPoolFFI(apr_pool_t 
*pool
, JSContextRef context
, sig::Type 
*type
, ffi_type 
*ffi
, void *data
, JSValueRef value
) { 
 533     switch (type
->primitive
) { 
 535             *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
); 
 538 #define CYPoolFFI_(primitive, native) \ 
 539         case sig::primitive ## _P: \ 
 540             *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \ 
 543         CYPoolFFI_(uchar
, unsigned char) 
 544         CYPoolFFI_(char, char) 
 545         CYPoolFFI_(ushort
, unsigned short) 
 546         CYPoolFFI_(short, short) 
 547         CYPoolFFI_(ulong
, unsigned long) 
 548         CYPoolFFI_(long, long) 
 549         CYPoolFFI_(uint
, unsigned int) 
 551         CYPoolFFI_(ulonglong
, unsigned long long) 
 552         CYPoolFFI_(longlong
, long long) 
 553         CYPoolFFI_(float, float) 
 554         CYPoolFFI_(double, double) 
 557             uint8_t *base(reinterpret_cast<uint8_t *>(data
)); 
 558             JSObjectRef 
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value 
: NULL
); 
 559             for (size_t index(0); index 
!= type
->data
.data
.size
; ++index
) { 
 560                 ffi_type 
*field(ffi
->elements
[index
]); 
 563                 if (aggregate 
== NULL
) 
 566                     rhs 
= CYGetProperty(context
, aggregate
, index
); 
 567                     if (JSValueIsUndefined(context
, rhs
)) 
 568                         throw CYJSError(context
, "unable to extract array value"); 
 571                 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
); 
 578             *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
); 
 582             *reinterpret_cast<const char **>(data
) = CYPoolCString(pool
, context
, value
); 
 585         case sig::struct_P
: { 
 586             uint8_t *base(reinterpret_cast<uint8_t *>(data
)); 
 587             JSObjectRef 
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value 
: NULL
); 
 588             for (size_t index(0); index 
!= type
->data
.signature
.count
; ++index
) { 
 589                 sig::Element 
*element(&type
->data
.signature
.elements
[index
]); 
 590                 ffi_type 
*field(ffi
->elements
[index
]); 
 593                 if (aggregate 
== NULL
) 
 596                     rhs 
= CYGetProperty(context
, aggregate
, index
); 
 597                     if (JSValueIsUndefined(context
, rhs
)) { 
 598                         if (element
->name 
!= NULL
) 
 599                             rhs 
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
)); 
 602                         if (JSValueIsUndefined(context
, rhs
)) undefined
: 
 603                             throw CYJSError(context
, "unable to extract structure value"); 
 607                 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
); 
 617             if (hooks_ 
!= NULL 
&& hooks_
->PoolFFI 
!= NULL
) 
 618                 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
)) 
 621             CYThrow("unimplemented signature code: '%c''\n", type
->primitive
); 
 625 JSValueRef 
CYFromFFI(JSContextRef context
, sig::Type 
*type
, ffi_type 
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) { 
 626     switch (type
->primitive
) { 
 628             return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
)); 
 630 #define CYFromFFI_(primitive, native) \ 
 631         case sig::primitive ## _P: \ 
 632             return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \ 
 634         CYFromFFI_(uchar, unsigned char) 
 635         CYFromFFI_(char, char) 
 636         CYFromFFI_(ushort
, unsigned short) 
 637         CYFromFFI_(short, short) 
 638         CYFromFFI_(ulong
, unsigned long) 
 639         CYFromFFI_(long, long) 
 640         CYFromFFI_(uint
, unsigned int) 
 642         CYFromFFI_(ulonglong
, unsigned long long) 
 643         CYFromFFI_(longlong
, long long) 
 644         CYFromFFI_(float, float) 
 645         CYFromFFI_(double, double) 
 648             if (void *pointer 
= data
) 
 649                 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
); 
 653             if (void *pointer 
= *reinterpret_cast<void **>(data
)) 
 654                 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
); 
 658             if (char *utf8 
= *reinterpret_cast<char **>(data
)) 
 659                 return CYCastJSValue(context
, utf8
); 
 663             return CYMakeStruct(context
, data
, type
, ffi
, owner
); 
 665             return CYJSUndefined(context
); 
 668             return CYJSNull(context
); 
 670             if (hooks_ 
!= NULL 
&& hooks_
->FromFFI 
!= NULL
) 
 671                 if (JSValueRef value 
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
)) 
 674             CYThrow("unimplemented signature code: '%c''\n", type
->primitive
); 
 678 static void FunctionClosure_(ffi_cif 
*cif
, void *result
, void **arguments
, void *arg
) { 
 679     Closure_privateData 
*internal(reinterpret_cast<Closure_privateData 
*>(arg
)); 
 681     JSContextRef 
context(internal
->context_
); 
 683     size_t count(internal
->cif_
.nargs
); 
 684     JSValueRef values
[count
]; 
 686     for (size_t index(0); index 
!= count
; ++index
) 
 687         values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]); 
 689     JSValueRef 
value(CYCallAsFunction(context
, internal
->function_
, NULL
, count
, values
)); 
 690     CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
); 
 693 Closure_privateData 
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const char *type
, void (*callback
)(ffi_cif 
*, void *, void **, void *)) { 
 694     // XXX: in case of exceptions this will leak 
 695     // XXX: in point of fact, this may /need/ to leak :( 
 696     Closure_privateData 
*internal(new Closure_privateData(context
, function
, type
)); 
 698     ffi_closure 
*closure((ffi_closure 
*) _syscall(mmap( 
 699         NULL
, sizeof(ffi_closure
), 
 700         PROT_READ 
| PROT_WRITE
, MAP_ANON 
| MAP_PRIVATE
, 
 704     ffi_status 
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
)); 
 705     _assert(status 
== FFI_OK
); 
 707     _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ 
| PROT_EXEC
)); 
 709     internal
->value_ 
= closure
; 
 714 static JSObjectRef 
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const char *type
) { 
 715     Closure_privateData 
*internal(CYMakeFunctor_(context
, function
, type
, &FunctionClosure_
)); 
 716     JSObjectRef 
object(JSObjectMake(context
, Functor_
, internal
)); 
 717     // XXX: see above notes about needing to leak 
 718     JSValueProtect(CYGetJSContext(context
), object
); 
 722 JSObjectRef 
CYGetCachedObject(JSContextRef context
, JSStringRef name
) { 
 723     return CYCastJSObject(context
, CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
)); 
 726 static JSObjectRef 
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const char *type
) { 
 727     JSObjectRef 
Function(CYGetCachedObject(context
, CYJSString("Function"))); 
 729     JSValueRef 
exception(NULL
); 
 730     bool function(JSValueIsInstanceOfConstructor(context
, value
, Function
, &exception
)); 
 731     CYThrow(context
, exception
); 
 734         JSObjectRef 
function(CYCastJSObject(context
, value
)); 
 735         return CYMakeFunctor(context
, function
, type
); 
 737         void (*function
)()(CYCastPointer
<void (*)()>(context
, value
)); 
 738         return CYMakeFunctor(context
, function
, type
); 
 742 static bool Index_(apr_pool_t 
*pool
, JSContextRef context
, Struct_privateData 
*internal
, JSStringRef property
, ssize_t 
&index
, uint8_t *&base
) { 
 743     Type_privateData 
*typical(internal
->type_
); 
 744     sig::Type 
*type(typical
->type_
); 
 748     const char *name(CYPoolCString(pool
, context
, property
)); 
 749     size_t length(strlen(name
)); 
 750     double number(CYCastDouble(name
, length
)); 
 752     size_t count(type
->data
.signature
.count
); 
 754     if (std::isnan(number
)) { 
 755         if (property 
== NULL
) 
 758         sig::Element 
*elements(type
->data
.signature
.elements
); 
 760         for (size_t local(0); local 
!= count
; ++local
) { 
 761             sig::Element 
*element(&elements
[local
]); 
 762             if (element
->name 
!= NULL 
&& strcmp(name
, element
->name
) == 0) { 
 770         index 
= static_cast<ssize_t
>(number
); 
 771         if (index 
!= number 
|| index 
< 0 || static_cast<size_t>(index
) >= count
) 
 776     ffi_type 
**elements(typical
->GetFFI()->elements
); 
 778     base 
= reinterpret_cast<uint8_t *>(internal
->value_
); 
 779     for (ssize_t 
local(0); local 
!= index
; ++local
) 
 780         base 
+= elements
[local
]->size
; 
 785 static JSValueRef 
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
 787     Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(object
))); 
 789     if (JSStringIsEqual(property
, length_s
)) 
 790         return internal
->length_ 
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
); 
 792     Type_privateData 
*typical(internal
->type_
); 
 794     if (typical
->type_ 
== NULL
) 
 798     if (JSStringIsEqualToUTF8CString(property
, "$cyi")) 
 800     else if (!CYGetOffset(pool
, context
, property
, offset
)) 
 803     ffi_type 
*ffi(typical
->GetFFI()); 
 805     uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
)); 
 806     base 
+= ffi
->size 
* offset
; 
 808     JSObjectRef 
owner(internal
->GetOwner() ?: object
); 
 809     return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
); 
 812 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef 
*exception
) { CYTry 
{ 
 814     Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(object
))); 
 815     Type_privateData 
*typical(internal
->type_
); 
 817     if (typical
->type_ 
== NULL
) 
 821     if (JSStringIsEqualToUTF8CString(property
, "$cyi")) 
 823     else if (!CYGetOffset(pool
, context
, property
, offset
)) 
 826     ffi_type 
*ffi(typical
->GetFFI()); 
 828     uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
)); 
 829     base 
+= ffi
->size 
* offset
; 
 831     CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
); 
 835 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
 836     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(_this
))); 
 837     Type_privateData 
*typical(internal
->type_
); 
 838     return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
); 
 841 static JSValueRef 
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
 843     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(object
))); 
 844     Type_privateData 
*typical(internal
->type_
); 
 849     if (!Index_(pool
, context
, internal
, property
, index
, base
)) 
 852     JSObjectRef 
owner(internal
->GetOwner() ?: object
); 
 854     return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
); 
 857 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef 
*exception
) { CYTry 
{ 
 859     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(object
))); 
 860     Type_privateData 
*typical(internal
->type_
); 
 865     if (!Index_(pool
, context
, internal
, property
, index
, base
)) 
 868     CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
); 
 872 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) { 
 873     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(object
))); 
 874     Type_privateData 
*typical(internal
->type_
); 
 875     sig::Type 
*type(typical
->type_
); 
 880     size_t count(type
->data
.signature
.count
); 
 881     sig::Element 
*elements(type
->data
.signature
.elements
); 
 885     for (size_t index(0); index 
!= count
; ++index
) { 
 887         name 
= elements
[index
].name
; 
 890             sprintf(number
, "%zu", index
); 
 894         JSPropertyNameAccumulatorAddName(names
, CYJSString(name
)); 
 898 JSValueRef 
CYCallFunction(apr_pool_t 
*pool
, JSContextRef context
, size_t setups
, void *setup
[], size_t count
, const JSValueRef arguments
[], bool initialize
, JSValueRef 
*exception
, sig::Signature 
*signature
, ffi_cif 
*cif
, void (*function
)()) { CYTry 
{ 
 899     if (setups 
+ count 
!= signature
->count 
- 1) 
 900         throw CYJSError(context
, "incorrect number of arguments to ffi function"); 
 902     size_t size(setups 
+ count
); 
 904     memcpy(values
, setup
, sizeof(void *) * setups
); 
 906     for (size_t index(setups
); index 
!= size
; ++index
) { 
 907         sig::Element 
*element(&signature
->elements
[index 
+ 1]); 
 908         ffi_type 
*ffi(cif
->arg_types
[index
]); 
 910         values
[index
] = new(pool
) uint8_t[ffi
->size
]; 
 911         CYPoolFFI(pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index 
- setups
]); 
 914     uint8_t value
[cif
->rtype
->size
]; 
 916     if (hooks_ 
!= NULL 
&& hooks_
->CallFunction 
!= NULL
) 
 917         (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
); 
 919         ffi_call(cif
, function
, value
, values
); 
 921     return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
); 
 924 static JSValueRef 
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
 926     cy::Functor 
*internal(reinterpret_cast<cy::Functor 
*>(JSObjectGetPrivate(object
))); 
 927     return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, exception
, &internal
->signature_
, &internal
->cif_
, internal
->GetValue()); 
 930 static JSObjectRef 
CYMakeType(JSContextRef context
, const char *type
) { 
 931     Type_privateData 
*internal(new Type_privateData(type
)); 
 932     return JSObjectMake(context
, Type_privateData::Class_
, internal
); 
 935 static JSObjectRef 
CYMakeType(JSContextRef context
, sig::Type 
*type
) { 
 936     Type_privateData 
*internal(new Type_privateData(type
)); 
 937     return JSObjectMake(context
, Type_privateData::Class_
, internal
); 
 940 static void *CYCastSymbol(const char *name
) { 
 941     return dlsym(RTLD_DEFAULT
, name
); 
 944 static JSValueRef 
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
 945     JSObjectRef 
global(CYGetGlobalObject(context
)); 
 946     JSObjectRef 
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript")))); 
 947     if (JSValueRef value 
= CYGetProperty(context
, cycript
, property
)) 
 948         if (!JSValueIsUndefined(context
, value
)) 
 952     CYUTF8String 
name(CYPoolUTF8String(pool
, context
, property
)); 
 954     if (hooks_ 
!= NULL 
&& hooks_
->RuntimeProperty 
!= NULL
) 
 955         if (JSValueRef value 
= (*hooks_
->RuntimeProperty
)(context
, name
)) 
 958     size_t length(name
.size
); 
 959     char keyed
[length 
+ 2]; 
 960     memcpy(keyed 
+ 1, name
.data
, length 
+ 1); 
 962     static const char *modes 
= "0124"; 
 963     for (size_t i(0); i 
!= 4; ++i
) { 
 967         if (CYBridgeEntry 
*entry 
= CYBridgeHash(keyed
, length 
+ 1)) 
 970                     return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
); 
 973                     if (void (*symbol
)() = reinterpret_cast<void (*)()>(CYCastSymbol(name
.data
))) 
 974                         return CYMakeFunctor(context
, symbol
, entry
->value_
, &entry
->cache_
); 
 978                     if (void *symbol 
= CYCastSymbol(name
.data
)) { 
 979                         // XXX: this is horrendously inefficient 
 980                         sig::Signature signature
; 
 981                         sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
); 
 983                         sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
); 
 984                         return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
); 
 987                 // XXX: implement case 3 
 989                     return CYMakeType(context
, entry
->value_
); 
 996 static JSObjectRef 
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 998         throw CYJSError(context
, "incorrect number of arguments to Functor constructor"); 
1002     void *value(CYCastPointer
<void *>(context
, arguments
[0])); 
1003     const char *type(CYPoolCString(pool
, context
, arguments
[1])); 
1005     sig::Signature signature
; 
1006     sig::Parse(pool
, &signature
, type
, &Structor_
); 
1008     return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
); 
1011 static JSObjectRef 
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1013         throw CYJSError(context
, "incorrect number of arguments to Type constructor"); 
1015     const char *type(CYPoolCString(pool
, context
, arguments
[0])); 
1016     return CYMakeType(context
, type
); 
1019 static JSValueRef 
Type_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
1020     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1024     if (JSStringIsEqualToUTF8CString(property
, "$cyi")) { 
1025         type
.primitive 
= sig::pointer_P
; 
1026         type
.data
.data
.size 
= 0; 
1029         size_t index(CYGetIndex(pool
, context
, property
)); 
1030         if (index 
== _not(size_t)) 
1032         type
.primitive 
= sig::array_P
; 
1033         type
.data
.data
.size 
= index
; 
1039     type
.data
.data
.type 
= internal
->type_
; 
1041     return CYMakeType(context
, &type
); 
1044 static JSValueRef 
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1045     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1048         throw CYJSError(context
, "incorrect number of arguments to type cast function"); 
1049     sig::Type 
*type(internal
->type_
); 
1050     ffi_type 
*ffi(internal
->GetFFI()); 
1052     uint8_t value
[ffi
->size
]; 
1054     CYPoolFFI(pool
, context
, type
, ffi
, value
, arguments
[0]); 
1055     return CYFromFFI(context
, type
, ffi
, value
); 
1058 static JSObjectRef 
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1060         throw CYJSError(context
, "incorrect number of arguments to type cast function"); 
1061     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1063     sig::Type 
*type(internal
->type_
); 
1066     if (type
->primitive 
!= sig::array_P
) 
1067         length 
= _not(size_t); 
1069         length 
= type
->data
.data
.size
; 
1070         type 
= type
->data
.data
.type
; 
1073     void *value(malloc(internal
->GetFFI()->size
)); 
1074     return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
); 
1077 static JSObjectRef 
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1079         throw CYJSError(context
, "incorrect number of arguments to Functor constructor"); 
1081     const char *type(CYPoolCString(pool
, context
, arguments
[1])); 
1082     return CYMakeFunctor(context
, arguments
[0], type
); 
1085 static JSValueRef 
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1086     CYValue 
*internal(reinterpret_cast<CYValue 
*>(JSObjectGetPrivate(_this
))); 
1087     return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
)); 
1090 static JSValueRef 
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
1091     return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
); 
1094 static JSValueRef 
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1095     CYValue 
*internal(reinterpret_cast<CYValue 
*>(JSObjectGetPrivate(_this
))); 
1097     sprintf(string
, "%p", internal
->value_
); 
1098     return CYCastJSValue(context
, string
); 
1101 static JSValueRef 
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1102     Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(_this
))); 
1103     if (internal
->length_ 
!= _not(size_t)) { 
1104         JSObjectRef 
Array(CYGetCachedObject(context
, Array_s
)); 
1105         JSObjectRef 
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
))); 
1106         return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
); 
1109         sprintf(string
, "%p", internal
->value_
); 
1110         return CYCastJSValue(context
, string
); 
1114 static JSValueRef 
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1115     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1117     const char *type(sig::Unparse(pool
, internal
->type_
)); 
1118     return CYCastJSValue(context
, CYJSString(type
)); 
1121 static JSValueRef 
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1122     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1124     const char *type(sig::Unparse(pool
, internal
->type_
)); 
1125     size_t size(strlen(type
)); 
1126     char *cyon(new(pool
) char[12 + size 
+ 1]); 
1127     memcpy(cyon
, "new Type(\"", 10); 
1128     cyon
[12 + size
] = '\0'; 
1129     cyon
[12 + size 
- 2] = '"'; 
1130     cyon
[12 + size 
- 1] = ')'; 
1131     memcpy(cyon 
+ 10, type
, size
); 
1132     return CYCastJSValue(context
, CYJSString(cyon
)); 
1135 static JSValueRef 
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
1136     return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
); 
1139 static JSStaticFunction Pointer_staticFunctions
[4] = { 
1140     {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1141     {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1142     {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1146 static JSStaticFunction Struct_staticFunctions
[2] = { 
1147     {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1151 static JSStaticFunction Functor_staticFunctions
[4] = { 
1152     {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1153     {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1154     {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1159     JSStaticFunction 
const * const Functor::StaticFunctions 
= Functor_staticFunctions
; 
1162 static JSStaticFunction Type_staticFunctions
[4] = { 
1163     {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1164     {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1165     {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1169 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef 
*); 
1171 void CYSetArgs(int argc
, const char *argv
[]) { 
1172     JSContextRef 
context(CYGetJSContext()); 
1173     JSValueRef args
[argc
]; 
1174     for (int i(0); i 
!= argc
; ++i
) 
1175         args
[i
] = CYCastJSValue(context
, argv
[i
]); 
1178     if (JSObjectMakeArray$ 
!= NULL
) { 
1179         JSValueRef 
exception(NULL
); 
1180         array 
= (*JSObjectMakeArray$
)(context
, argc
, args
, &exception
); 
1181         CYThrow(context
, exception
); 
1183         JSObjectRef 
Array(CYGetCachedObject(context
, CYJSString("Array"))); 
1184         JSValueRef 
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
)); 
1185         array 
= CYCastJSObject(context
, value
); 
1188     JSObjectRef 
System(CYGetCachedObject(context
, CYJSString("System"))); 
1189     CYSetProperty(context
, System
, CYJSString("args"), array
); 
1192 JSObjectRef 
CYGetGlobalObject(JSContextRef context
) { 
1193     return JSContextGetGlobalObject(context
); 
1196 const char *CYExecute(apr_pool_t 
*pool
, CYUTF8String code
) { 
1197     JSContextRef 
context(CYGetJSContext()); 
1198     JSValueRef 
exception(NULL
), result
; 
1201     if (hooks_ 
!= NULL 
&& hooks_
->ExecuteStart 
!= NULL
) 
1202         handle 
= (*hooks_
->ExecuteStart
)(context
); 
1209         result 
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
); 
1210     } catch (const char *error
) { 
1214     if (exception 
!= NULL
) { error
: 
1219     if (JSValueIsUndefined(context
, result
)) 
1223         json 
= CYPoolCCYON(pool
, context
, result
, &exception
); 
1224     } catch (const char *error
) { 
1228     if (exception 
!= NULL
) 
1231     CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
); 
1233     if (hooks_ 
!= NULL 
&& hooks_
->ExecuteEnd 
!= NULL
) 
1234         (*hooks_
->ExecuteEnd
)(context
, handle
); 
1238 extern "C" void CydgetSetupContext(JSGlobalContextRef context
) { 
1239     CYSetupContext(context
); 
1242 static bool initialized_ 
= false; 
1244 void CYInitializeDynamic() { 
1246         initialized_ 
= true; 
1249     CYInitializeStatic(); 
1251     JSObjectMakeArray$ 
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef 
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray")); 
1253     JSClassDefinition definition
; 
1255     definition 
= kJSClassDefinitionEmpty
; 
1256     definition
.className 
= "All"; 
1257     definition
.getProperty 
= &All_getProperty
; 
1258     All_ 
= JSClassCreate(&definition
); 
1260     definition 
= kJSClassDefinitionEmpty
; 
1261     definition
.className 
= "Context"; 
1262     definition
.finalize 
= &CYFinalize
; 
1263     Context_ 
= JSClassCreate(&definition
); 
1265     definition 
= kJSClassDefinitionEmpty
; 
1266     definition
.className 
= "Functor"; 
1267     definition
.staticFunctions 
= cy::Functor::StaticFunctions
; 
1268     definition
.callAsFunction 
= &Functor_callAsFunction
; 
1269     definition
.finalize 
= &CYFinalize
; 
1270     Functor_ 
= JSClassCreate(&definition
); 
1272     definition 
= kJSClassDefinitionEmpty
; 
1273     definition
.className 
= "Pointer"; 
1274     definition
.staticFunctions 
= Pointer_staticFunctions
; 
1275     definition
.getProperty 
= &Pointer_getProperty
; 
1276     definition
.setProperty 
= &Pointer_setProperty
; 
1277     definition
.finalize 
= &CYFinalize
; 
1278     Pointer_ 
= JSClassCreate(&definition
); 
1280     definition 
= kJSClassDefinitionEmpty
; 
1281     definition
.className 
= "Struct"; 
1282     definition
.staticFunctions 
= Struct_staticFunctions
; 
1283     definition
.getProperty 
= &Struct_getProperty
; 
1284     definition
.setProperty 
= &Struct_setProperty
; 
1285     definition
.getPropertyNames 
= &Struct_getPropertyNames
; 
1286     definition
.finalize 
= &CYFinalize
; 
1287     Struct_ 
= JSClassCreate(&definition
); 
1289     definition 
= kJSClassDefinitionEmpty
; 
1290     definition
.className 
= "Type"; 
1291     definition
.staticFunctions 
= Type_staticFunctions
; 
1292     definition
.getProperty 
= &Type_getProperty
; 
1293     definition
.callAsFunction 
= &Type_callAsFunction
; 
1294     definition
.callAsConstructor 
= &Type_callAsConstructor
; 
1295     definition
.finalize 
= &CYFinalize
; 
1296     Type_privateData::Class_ 
= JSClassCreate(&definition
); 
1298     definition 
= kJSClassDefinitionEmpty
; 
1299     definition
.className 
= "Global"; 
1300     //definition.getProperty = &Global_getProperty; 
1301     Global_ 
= JSClassCreate(&definition
); 
1303     Array_s 
= JSStringCreateWithUTF8CString("Array"); 
1304     cy_s 
= JSStringCreateWithUTF8CString("$cy"); 
1305     length_s 
= JSStringCreateWithUTF8CString("length"); 
1306     message_s 
= JSStringCreateWithUTF8CString("message"); 
1307     name_s 
= JSStringCreateWithUTF8CString("name"); 
1308     pop_s 
= JSStringCreateWithUTF8CString("pop"); 
1309     prototype_s 
= JSStringCreateWithUTF8CString("prototype"); 
1310     push_s 
= JSStringCreateWithUTF8CString("push"); 
1311     splice_s 
= JSStringCreateWithUTF8CString("splice"); 
1312     toCYON_s 
= JSStringCreateWithUTF8CString("toCYON"); 
1313     toJSON_s 
= JSStringCreateWithUTF8CString("toJSON"); 
1314     toPointer_s 
= JSStringCreateWithUTF8CString("toPointer"); 
1315     toString_s 
= JSStringCreateWithUTF8CString("toString"); 
1317     Result_ 
= JSStringCreateWithUTF8CString("_"); 
1319     if (hooks_ 
!= NULL 
&& hooks_
->Initialize 
!= NULL
) 
1320         (*hooks_
->Initialize
)(); 
1323 void CYThrow(JSContextRef context
, JSValueRef value
) { 
1325         throw CYJSError(context
, value
); 
1328 const char *CYJSError::PoolCString(apr_pool_t 
*pool
) const { 
1329     // XXX: this used to be CYPoolCString 
1330     return CYPoolCCYON(pool
, context_
, value_
); 
1333 JSValueRef 
CYJSError::CastJSValue(JSContextRef context
) const { 
1334     // XXX: what if the context is different? 
1338 JSValueRef 
CYCastJSError(JSContextRef context
, const char *message
) { 
1339     JSObjectRef 
Error(CYGetCachedObject(context
, CYJSString("Error"))); 
1341     JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)}; 
1343     JSValueRef 
exception(NULL
); 
1344     JSValueRef 
value(JSObjectCallAsConstructor(context
, Error
, 1, arguments
, &exception
)); 
1345     CYThrow(context
, exception
); 
1350 JSValueRef 
CYPoolError::CastJSValue(JSContextRef context
) const { 
1351     return CYCastJSError(context
, message_
); 
1354 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) { 
1355     _assert(context 
!= NULL
); 
1360     va_start(args
, format
); 
1361     const char *message(apr_pvsprintf(pool
, format
, args
)); 
1364     value_ 
= CYCastJSError(context
, message
); 
1367 JSGlobalContextRef 
CYGetJSContext(JSContextRef context
) { 
1368     return reinterpret_cast<Context 
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
; 
1371 extern "C" void CYSetupContext(JSGlobalContextRef context
) { 
1372     CYInitializeDynamic(); 
1374     JSObjectRef 
global(CYGetGlobalObject(context
)); 
1376     JSObjectRef 
cy(JSObjectMake(context
, Context_
, new Context(context
))); 
1377     CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
); 
1379 /* Cache Globals {{{ */ 
1380     JSObjectRef 
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array")))); 
1381     CYSetProperty(context
, cy
, CYJSString("Array"), Array
); 
1383     JSObjectRef 
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
))); 
1384     CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
); 
1386     JSObjectRef 
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error")))); 
1387     CYSetProperty(context
, cy
, CYJSString("Error"), Error
); 
1389     JSObjectRef 
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function")))); 
1390     CYSetProperty(context
, cy
, CYJSString("Function"), Function
); 
1392     JSObjectRef 
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
))); 
1393     CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
); 
1395     JSObjectRef 
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object")))); 
1396     CYSetProperty(context
, cy
, CYJSString("Object"), Object
); 
1398     JSObjectRef 
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
))); 
1399     CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
); 
1401     JSObjectRef 
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String")))); 
1402     CYSetProperty(context
, cy
, CYJSString("String"), String
); 
1404     JSObjectRef 
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
))); 
1405     CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
); 
1408     CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
); 
1409     CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
); 
1411     JSObjectRef 
cycript(JSObjectMake(context
, NULL
, NULL
)); 
1412     CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
); 
1413     CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
); 
1415     JSObjectRef 
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
)); 
1416     JSObjectSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
); 
1417     CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
); 
1419     CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
)); 
1420     CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
)); 
1422     JSObjectRef 
all(JSObjectMake(context
, All_
, NULL
)); 
1423     CYSetProperty(context
, cycript
, CYJSString("all"), all
); 
1426         JSObjectRef 
last(NULL
), curr(global
); 
1428         goto next
; for (JSValueRef next
;;) { 
1429             if (JSValueIsNull(context
, next
)) 
1432             curr 
= CYCastJSObject(context
, next
); 
1434             next 
= JSObjectGetPrototype(context
, curr
); 
1437         JSObjectSetPrototype(context
, last
, all
); 
1440     CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
, kJSPropertyAttributeDontEnum
); 
1442     JSObjectRef 
System(JSObjectMake(context
, NULL
, NULL
)); 
1443     CYSetProperty(context
, cy
, CYJSString("System"), System
); 
1445     CYSetProperty(context
, global
, CYJSString("system"), System
); 
1446     CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
)); 
1447     //CYSetProperty(context, System, CYJSString("global"), global); 
1448     CYSetProperty(context
, System
, CYJSString("print"), &System_print
); 
1450     if (hooks_ 
!= NULL 
&& hooks_
->SetupContext 
!= NULL
) 
1451         (*hooks_
->SetupContext
)(context
); 
1454 JSGlobalContextRef 
CYGetJSContext() { 
1455     CYInitializeDynamic(); 
1457     static JSGlobalContextRef context_
; 
1459     if (context_ 
== NULL
) { 
1460         context_ 
= JSGlobalContextCreate(Global_
); 
1461         CYSetupContext(context_
);