1 /* Cycript - Inlining/Optimizing JavaScript Compiler 
   2  * Copyright (C) 2009  Jay Freeman (saurik) 
   5 /* Modified BSD License {{{ */ 
   7  *        Redistribution and use in source and binary 
   8  * forms, with or without modification, are permitted 
   9  * provided that the following conditions are met: 
  11  * 1. Redistributions of source code must retain the 
  12  *    above copyright notice, this list of conditions 
  13  *    and the following disclaimer. 
  14  * 2. Redistributions in binary form must reproduce the 
  15  *    above copyright notice, this list of conditions 
  16  *    and the following disclaimer in the documentation 
  17  *    and/or other materials provided with the 
  19  * 3. The name of the author may not be used to endorse 
  20  *    or promote products derived from this software 
  21  *    without specific prior written permission. 
  23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' 
  24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
  25  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
  26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  27  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE 
  28  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
  30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  31  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
  32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
  33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
  34  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
  35  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  36  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
  42 #include "Internal.hpp" 
  47 #include "cycript.hpp" 
  49 #include "sig/parse.hpp" 
  50 #include "sig/ffi_type.hpp" 
  52 #include "Pooling.hpp" 
  57 #include <ext/stdio_filebuf.h> 
  65 #include "Cycript.tab.hh" 
  68 #include "JavaScript.hpp" 
  73     catch (NSException *error) { \ 
  74         CYThrow(context, error, exception); \ 
  81 char *sqlite3_column_pooled(apr_pool_t 
*pool
, sqlite3_stmt 
*stmt
, int n
) { 
  82     if (const unsigned char *value 
= sqlite3_column_text(stmt
, n
)) 
  83         return apr_pstrdup(pool
, (const char *) value
); 
  87 struct CYHooks 
*hooks_
; 
  89 /* JavaScript Properties {{{ */ 
  90 JSValueRef 
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) { 
  91     JSValueRef 
exception(NULL
); 
  92     JSValueRef 
value(JSObjectGetPropertyAtIndex(context
, object
, index
, &exception
)); 
  93     CYThrow(context
, exception
); 
  97 JSValueRef 
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) { 
  98     JSValueRef 
exception(NULL
); 
  99     JSValueRef 
value(JSObjectGetProperty(context
, object
, name
, &exception
)); 
 100     CYThrow(context
, exception
); 
 104 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) { 
 105     JSValueRef 
exception(NULL
); 
 106     JSObjectSetPropertyAtIndex(context
, object
, index
, value
, &exception
); 
 107     CYThrow(context
, exception
); 
 110 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) { 
 111     JSValueRef 
exception(NULL
); 
 112     JSObjectSetProperty(context
, object
, name
, value
, attributes
, &exception
); 
 113     CYThrow(context
, exception
); 
 116 /* JavaScript Strings {{{ */ 
 117 JSStringRef 
CYCopyJSString(const char *value
) { 
 118     return value 
== NULL 
? NULL 
: JSStringCreateWithUTF8CString(value
); 
 121 JSStringRef 
CYCopyJSString(JSStringRef value
) { 
 122     return value 
== NULL 
? NULL 
: JSStringRetain(value
); 
 125 JSStringRef 
CYCopyJSString(CYUTF8String value
) { 
 126     // XXX: this is very wrong 
 127     return CYCopyJSString(value
.data
); 
 130 JSStringRef 
CYCopyJSString(JSContextRef context
, JSValueRef value
) { 
 131     if (JSValueIsNull(context
, value
)) 
 133     JSValueRef 
exception(NULL
); 
 134     JSStringRef 
string(JSValueToStringCopy(context
, value
, &exception
)); 
 135     CYThrow(context
, exception
); 
 139 static CYUTF16String 
CYCastUTF16String(JSStringRef value
) { 
 140     return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
)); 
 143 template <typename Type_
> 
 144 _finline 
size_t iconv_(size_t (*iconv
)(iconv_t
, Type_
, size_t *, char **, size_t *), iconv_t cd
, char **inbuf
, size_t *inbytesleft
, char **outbuf
, size_t *outbytesleft
) { 
 145     return iconv(cd
, const_cast<Type_
>(inbuf
), inbytesleft
, outbuf
, outbytesleft
); 
 148 CYUTF8String 
CYPoolUTF8String(apr_pool_t 
*pool
, JSContextRef context
, JSStringRef value
) { 
 149     _assert(pool 
!= NULL
); 
 151     CYUTF16String 
utf16(CYCastUTF16String(value
)); 
 152     const char *in(reinterpret_cast<const char *>(utf16
.data
)); 
 155     iconv_t 
conversion(_syscall(iconv_open("UTF-8", "UCS-2"))); 
 157     iconv_t 
conversion(_syscall(iconv_open("UTF-8", "UCS-2-INTERNAL"))); 
 160     size_t size(JSStringGetMaximumUTF8CStringSize(value
)); 
 161     char *out(new(pool
) char[size
]); 
 162     CYUTF8String 
utf8(out
, size
); 
 164     size 
= utf16
.size 
* 2; 
 165     _syscall(iconv_(&iconv
, conversion
, const_cast<char **>(&in
), &size
, &out
, &utf8
.size
)); 
 168     utf8
.size 
= out 
- utf8
.data
; 
 170     _syscall(iconv_close(conversion
)); 
 175 const char *CYPoolCString(apr_pool_t 
*pool
, JSContextRef context
, JSStringRef value
) { 
 176     CYUTF8String 
utf8(CYPoolUTF8String(pool
, context
, value
)); 
 177     _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
); 
 181 const char *CYPoolCString(apr_pool_t 
*pool
, JSContextRef context
, JSValueRef value
) { 
 182     return JSValueIsNull(context
, value
) ? NULL 
: CYPoolCString(pool
, context
, CYJSString(context
, value
)); 
 186 /* Index Offsets {{{ */ 
 187 size_t CYGetIndex(const CYUTF8String 
&value
) { 
 188     if (value
.data
[0] != '0') { 
 190         for (size_t i(0); i 
!= value
.size
; ++i
) { 
 191             if (!DigitRange_
[value
.data
[i
]]) 
 194             index 
+= value
.data
[i
] - '0'; 
 197     } else if (value
.size 
== 1) 
 203 size_t CYGetIndex(apr_pool_t 
*pool
, JSContextRef context
, JSStringRef value
) { 
 204     return CYGetIndex(CYPoolUTF8String(pool
, context
, value
)); 
 207 // XXX: this isn't actually right 
 208 bool CYGetOffset(const char *value
, ssize_t 
&index
) { 
 209     if (value
[0] != '0') { 
 211         index 
= strtol(value
, &end
, 10); 
 212         if (value 
+ strlen(value
) == end
) 
 214     } else if (value
[1] == '\0') { 
 223 /* JavaScript *ify {{{ */ 
 224 void CYStringify(std::ostringstream 
&str
, const char *data
, size_t size
) { 
 225     unsigned quot(0), apos(0); 
 226     for (const char *value(data
), *end(data 
+ size
); value 
!= end
; ++value
) 
 229         else if (*value 
== '\'') 
 232     bool single(quot 
> apos
); 
 234     str 
<< (single 
? '\'' : '"'); 
 236     for (const char *value(data
), *end(data 
+ size
); value 
!= end
; ++value
) 
 238             case '\\': str 
<< "\\\\"; break; 
 239             case '\b': str 
<< "\\b"; break; 
 240             case '\f': str 
<< "\\f"; break; 
 241             case '\n': str 
<< "\\n"; break; 
 242             case '\r': str 
<< "\\r"; break; 
 243             case '\t': str 
<< "\\t"; break; 
 244             case '\v': str 
<< "\\v"; break; 
 259                 // this test is designed to be "awewsome", generating neither warnings nor incorrect results 
 260                 if (*value 
< 0x20 || *value 
>= 0x7f) 
 261                     str 
<< "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(uint8_t(*value
)); 
 266     str 
<< (single 
? '\'' : '"'); 
 269 void CYNumerify(std::ostringstream 
&str
, double value
) { 
 271     // XXX: I want this to print 1e3 rather than 1000 
 272     sprintf(string
, "%.17g", value
); 
 276 bool CYIsKey(CYUTF8String value
) { 
 277     const char *data(value
.data
); 
 278     size_t size(value
.size
); 
 283     if (DigitRange_
[data
[0]]) { 
 284         size_t index(CYGetIndex(value
)); 
 285         if (index 
== _not(size_t)) 
 288         if (!WordStartRange_
[data
[0]]) 
 290         for (size_t i(1); i 
!= size
; ++i
) 
 291             if (!WordEndRange_
[data
[i
]]) 
 299 static JSGlobalContextRef Context_
; 
 300 static JSObjectRef System_
; 
 302 static JSClassRef Functor_
; 
 303 static JSClassRef Global_
; 
 304 static JSClassRef Pointer_
; 
 305 static JSClassRef Runtime_
; 
 306 static JSClassRef Struct_
; 
 308 static JSStringRef Result_
; 
 312 JSObjectRef Function_
; 
 316 JSStringRef message_
; 
 318 JSStringRef prototype_
; 
 322 JSObjectRef Object_prototype_
; 
 323 JSObjectRef Function_prototype_
; 
 325 JSObjectRef Array_prototype_
; 
 326 JSObjectRef Array_pop_
; 
 327 JSObjectRef Array_push_
; 
 328 JSObjectRef Array_splice_
; 
 332 void CYFinalize(JSObjectRef object
) { 
 333     delete reinterpret_cast<CYData 
*>(JSObjectGetPrivate(object
)); 
 336 struct CStringMapLess 
: 
 337     std::binary_function
<const char *, const char *, bool> 
 339     _finline 
bool operator ()(const char *lhs
, const char *rhs
) const { 
 340         return strcmp(lhs
, rhs
) < 0; 
 344 void Structor_(apr_pool_t 
*pool
, sig::Type 
*&type
) { 
 346         type
->primitive 
== sig::pointer_P 
&& 
 347         type
->data
.data
.type 
!= NULL 
&& 
 348         type
->data
.data
.type
->primitive 
== sig::struct_P 
&& 
 349         strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0 
 351         type
->primitive 
= sig::typename_P
; 
 352         type
->data
.data
.type 
= NULL
; 
 356     if (type
->primitive 
!= sig::struct_P 
|| type
->name 
== NULL
) 
 359     sqlite3_stmt 
*statement
; 
 361     _sqlcall(sqlite3_prepare(Bridge_
, 
 363             "\"bridge\".\"mode\", " 
 364             "\"bridge\".\"value\" " 
 367             " \"bridge\".\"mode\" in (3, 4) and" 
 368             " \"bridge\".\"name\" = ?" 
 370     , -1, &statement
, NULL
)); 
 372     _sqlcall(sqlite3_bind_text(statement
, 1, type
->name
, -1, SQLITE_STATIC
)); 
 377     if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
) { 
 381         mode 
= sqlite3_column_int(statement
, 0); 
 382         value 
= sqlite3_column_pooled(pool
, statement
, 1); 
 385     _sqlcall(sqlite3_finalize(statement
)); 
 394             sig::Parse(pool
, &type
->data
.signature
, value
, &Structor_
); 
 398             sig::Signature signature
; 
 399             sig::Parse(pool
, &signature
, value
, &Structor_
); 
 400             type 
= signature
.elements
[0].type
; 
 405 JSClassRef 
Type_privateData::Class_
; 
 410     Type_privateData 
*type_
; 
 413     Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type 
*type
) : 
 414         CYOwned(value
, context
, owner
), 
 415         type_(new(pool_
) Type_privateData(type
)), 
 421 struct Struct_privateData 
: 
 424     Type_privateData 
*type_
; 
 426     Struct_privateData(JSContextRef context
, JSObjectRef owner
) : 
 427         CYOwned(NULL
, context
, owner
) 
 432 typedef std::map
<const char *, Type_privateData 
*, CStringMapLess
> TypeMap
; 
 433 static TypeMap Types_
; 
 435 JSObjectRef 
CYMakeStruct(JSContextRef context
, void *data
, sig::Type 
*type
, ffi_type 
*ffi
, JSObjectRef owner
) { 
 436     Struct_privateData 
*internal(new Struct_privateData(context
, owner
)); 
 437     apr_pool_t 
*pool(internal
->pool_
); 
 438     Type_privateData 
*typical(new(pool
) Type_privateData(type
, ffi
)); 
 439     internal
->type_ 
= typical
; 
 442         internal
->value_ 
= data
; 
 444         size_t size(typical
->GetFFI()->size
); 
 445         void *copy(apr_palloc(internal
->pool_
, size
)); 
 446         memcpy(copy
, data
, size
); 
 447         internal
->value_ 
= copy
; 
 450     return JSObjectMake(context
, Struct_
, internal
); 
 453 JSValueRef 
CYCastJSValue(JSContextRef context
, bool value
) { 
 454     return JSValueMakeBoolean(context
, value
); 
 457 JSValueRef 
CYCastJSValue(JSContextRef context
, double value
) { 
 458     return JSValueMakeNumber(context
, value
); 
 461 #define CYCastJSValue_(Type_) \ 
 462     JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \ 
 463         return JSValueMakeNumber(context, static_cast<double>(value)); \ 
 467 CYCastJSValue_(unsigned int) 
 468 CYCastJSValue_(long int) 
 469 CYCastJSValue_(long unsigned int) 
 470 CYCastJSValue_(long long int) 
 471 CYCastJSValue_(long long unsigned int) 
 473 JSValueRef 
CYJSUndefined(JSContextRef context
) { 
 474     return JSValueMakeUndefined(context
); 
 477 double CYCastDouble(const char *value
, size_t size
) { 
 479     double number(strtod(value
, &end
)); 
 480     if (end 
!= value 
+ size
) 
 485 double CYCastDouble(const char *value
) { 
 486     return CYCastDouble(value
, strlen(value
)); 
 489 double CYCastDouble(JSContextRef context
, JSValueRef value
) { 
 490     JSValueRef 
exception(NULL
); 
 491     double number(JSValueToNumber(context
, value
, &exception
)); 
 492     CYThrow(context
, exception
); 
 496 bool CYCastBool(JSContextRef context
, JSValueRef value
) { 
 497     return JSValueToBoolean(context
, value
); 
 500 JSValueRef 
CYJSNull(JSContextRef context
) { 
 501     return JSValueMakeNull(context
); 
 504 JSValueRef 
CYCastJSValue(JSContextRef context
, JSStringRef value
) { 
 505     return value 
== NULL 
? CYJSNull(context
) : JSValueMakeString(context
, value
); 
 508 JSValueRef 
CYCastJSValue(JSContextRef context
, const char *value
) { 
 509     return CYCastJSValue(context
, CYJSString(value
)); 
 512 JSObjectRef 
CYCastJSObject(JSContextRef context
, JSValueRef value
) { 
 513     JSValueRef 
exception(NULL
); 
 514     JSObjectRef 
object(JSValueToObject(context
, value
, &exception
)); 
 515     CYThrow(context
, exception
); 
 519 JSValueRef 
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, JSValueRef arguments
[]) { 
 520     JSValueRef 
exception(NULL
); 
 521     JSValueRef 
value(JSObjectCallAsFunction(context
, function
, _this
, count
, arguments
, &exception
)); 
 522     CYThrow(context
, exception
); 
 526 bool CYIsCallable(JSContextRef context
, JSValueRef value
) { 
 527     return value 
!= NULL 
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
); 
 530 static JSValueRef 
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 535         printf("%s\n", CYPoolCString(pool
, context
, arguments
[0])); 
 538     return CYJSUndefined(context
); 
 541 static size_t Nonce_(0); 
 543 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
 545     const char *name(apr_psprintf(pool
, "%s%"APR_SIZE_T_FMT
"", CYPoolCString(pool
, context
, arguments
[0]), Nonce_
++)); 
 546     return CYCastJSValue(context
, name
); 
 549 static JSValueRef 
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
 550     JSGarbageCollect(context
); 
 551     return CYJSUndefined(context
); 
 554 const char *CYPoolCCYON(apr_pool_t 
*pool
, JSContextRef context
, JSValueRef value
, JSValueRef 
*exception
) { CYTry 
{ 
 555     switch (JSType type 
= JSValueGetType(context
, value
)) { 
 556         case kJSTypeUndefined
: 
 561             return CYCastBool(context
, value
) ? "true" : "false"; 
 563         case kJSTypeNumber
: { 
 564             std::ostringstream str
; 
 565             CYNumerify(str
, CYCastDouble(context
, value
)); 
 566             std::string 
value(str
.str()); 
 567             return apr_pstrmemdup(pool
, value
.c_str(), value
.size()); 
 570         case kJSTypeString
: { 
 571             std::ostringstream str
; 
 572             CYUTF8String 
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
))); 
 573             CYStringify(str
, string
.data
, string
.size
); 
 574             std::string 
value(str
.str()); 
 575             return apr_pstrmemdup(pool
, value
.c_str(), value
.size()); 
 579             return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
); 
 581             throw CYJSError(context
, "JSValueGetType() == 0x%x", type
); 
 585 const char *CYPoolCCYON(apr_pool_t 
*pool
, JSContextRef context
, JSValueRef value
) { 
 586     JSValueRef 
exception(NULL
); 
 587     const char *cyon(CYPoolCCYON(pool
, context
, value
, &exception
)); 
 588     CYThrow(context
, exception
); 
 592 const char *CYPoolCCYON(apr_pool_t 
*pool
, JSContextRef context
, JSObjectRef object
) { 
 593     JSValueRef 
toCYON(CYGetProperty(context
, object
, toCYON_
)); 
 594     if (CYIsCallable(context
, toCYON
)) { 
 595         JSValueRef 
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
)); 
 596         return CYPoolCString(pool
, context
, value
); 
 599     JSValueRef 
toJSON(CYGetProperty(context
, object
, toJSON_
)); 
 600     if (CYIsCallable(context
, toJSON
)) { 
 601         JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))}; 
 602         JSValueRef 
exception(NULL
); 
 603         const char *cyon(CYPoolCCYON(pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), &exception
)); 
 604         CYThrow(context
, exception
); 
 608     std::ostringstream str
; 
 612     // XXX: this is, sadly, going to leak 
 613     JSPropertyNameArrayRef 
names(JSObjectCopyPropertyNames(context
, object
)); 
 617     for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index 
!= count
; ++index
) { 
 618         JSStringRef 
name(JSPropertyNameArrayGetNameAtIndex(names
, index
)); 
 619         JSValueRef 
value(CYGetProperty(context
, object
, name
)); 
 626         CYUTF8String 
string(CYPoolUTF8String(pool
, context
, name
)); 
 630             CYStringify(str
, string
.data
, string
.size
); 
 632         str 
<< ':' << CYPoolCCYON(pool
, context
, value
); 
 637     JSPropertyNameArrayRelease(names
); 
 639     std::string 
string(str
.str()); 
 640     return apr_pstrmemdup(pool
, string
.c_str(), string
.size()); 
 643 static JSValueRef 
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 645     std::ostringstream str
; 
 649     JSValueRef 
length(CYGetProperty(context
, _this
, length_
)); 
 652     for (size_t index(0), count(CYCastDouble(context
, length
)); index 
!= count
; ++index
) { 
 653         JSValueRef 
value(CYGetProperty(context
, _this
, index
)); 
 660         if (!JSValueIsUndefined(context
, value
)) 
 661             str 
<< CYPoolCCYON(pool
, context
, value
); 
 670     std::string 
value(str
.str()); 
 671     return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size()))); 
 674 JSObjectRef 
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type 
*type
, ffi_type 
*ffi
, JSObjectRef owner
) { 
 675     Pointer 
*internal(new Pointer(pointer
, context
, owner
, length
, type
)); 
 676     return JSObjectMake(context
, Pointer_
, internal
); 
 679 static JSObjectRef 
CYMakeFunctor(JSContextRef context
, void (*function
)(), const char *type
) { 
 680     cy::Functor 
*internal(new cy::Functor(type
, function
)); 
 681     return JSObjectMake(context
, Functor_
, internal
); 
 684 static bool CYGetOffset(apr_pool_t 
*pool
, JSContextRef context
, JSStringRef value
, ssize_t 
&index
) { 
 685     return CYGetOffset(CYPoolCString(pool
, context
, value
), index
); 
 688 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) { 
 689     switch (JSValueGetType(context
, value
)) { 
 692         /*case kJSTypeObject: 
 693             if (JSValueIsObjectOfClass(context, value, Pointer_)) { 
 694                 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value))); 
 695                 return internal->value_; 
 698             double number(CYCastDouble(context
, value
)); 
 699             if (std::isnan(number
)) 
 700                 throw CYJSError(context
, "cannot convert value to pointer"); 
 701             return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
))); 
 705 void CYPoolFFI(apr_pool_t 
*pool
, JSContextRef context
, sig::Type 
*type
, ffi_type 
*ffi
, void *data
, JSValueRef value
) { 
 706     switch (type
->primitive
) { 
 708             *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
); 
 711 #define CYPoolFFI_(primitive, native) \ 
 712         case sig::primitive ## _P: \ 
 713             *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \ 
 716         CYPoolFFI_(uchar
, unsigned char) 
 717         CYPoolFFI_(char, char) 
 718         CYPoolFFI_(ushort
, unsigned short) 
 719         CYPoolFFI_(short, short) 
 720         CYPoolFFI_(ulong
, unsigned long) 
 721         CYPoolFFI_(long, long) 
 722         CYPoolFFI_(uint
, unsigned int) 
 724         CYPoolFFI_(ulonglong
, unsigned long long) 
 725         CYPoolFFI_(longlong
, long long) 
 726         CYPoolFFI_(float, float) 
 727         CYPoolFFI_(double, double) 
 730             uint8_t *base(reinterpret_cast<uint8_t *>(data
)); 
 731             JSObjectRef 
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value 
: NULL
); 
 732             for (size_t index(0); index 
!= type
->data
.data
.size
; ++index
) { 
 733                 ffi_type 
*field(ffi
->elements
[index
]); 
 736                 if (aggregate 
== NULL
) 
 739                     rhs 
= CYGetProperty(context
, aggregate
, index
); 
 740                     if (JSValueIsUndefined(context
, rhs
)) 
 741                         throw CYJSError(context
, "unable to extract array value"); 
 744                 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
); 
 751             *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
); 
 755             *reinterpret_cast<const char **>(data
) = CYPoolCString(pool
, context
, value
); 
 758         case sig::struct_P
: { 
 759             uint8_t *base(reinterpret_cast<uint8_t *>(data
)); 
 760             JSObjectRef 
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value 
: NULL
); 
 761             for (size_t index(0); index 
!= type
->data
.signature
.count
; ++index
) { 
 762                 sig::Element 
*element(&type
->data
.signature
.elements
[index
]); 
 763                 ffi_type 
*field(ffi
->elements
[index
]); 
 766                 if (aggregate 
== NULL
) 
 769                     rhs 
= CYGetProperty(context
, aggregate
, index
); 
 770                     if (JSValueIsUndefined(context
, rhs
)) { 
 771                         if (element
->name 
!= NULL
) 
 772                             rhs 
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
)); 
 775                         if (JSValueIsUndefined(context
, rhs
)) undefined
: 
 776                             throw CYJSError(context
, "unable to extract structure value"); 
 780                 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
); 
 790             if (hooks_ 
!= NULL 
&& hooks_
->PoolFFI 
!= NULL
) 
 791                 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
)) 
 794             CYThrow("unimplemented signature code: '%c''\n", type
->primitive
); 
 798 JSValueRef 
CYFromFFI(JSContextRef context
, sig::Type 
*type
, ffi_type 
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) { 
 799     switch (type
->primitive
) { 
 801             return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
)); 
 803 #define CYFromFFI_(primitive, native) \ 
 804         case sig::primitive ## _P: \ 
 805             return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \ 
 807         CYFromFFI_(uchar, unsigned char) 
 808         CYFromFFI_(char, char) 
 809         CYFromFFI_(ushort
, unsigned short) 
 810         CYFromFFI_(short, short) 
 811         CYFromFFI_(ulong
, unsigned long) 
 812         CYFromFFI_(long, long) 
 813         CYFromFFI_(uint
, unsigned int) 
 815         CYFromFFI_(ulonglong
, unsigned long long) 
 816         CYFromFFI_(longlong
, long long) 
 817         CYFromFFI_(float, float) 
 818         CYFromFFI_(double, double) 
 821             if (void *pointer 
= data
) 
 822                 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
); 
 826             if (void *pointer 
= *reinterpret_cast<void **>(data
)) 
 827                 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
); 
 831             if (char *utf8 
= *reinterpret_cast<char **>(data
)) 
 832                 return CYCastJSValue(context
, utf8
); 
 836             return CYMakeStruct(context
, data
, type
, ffi
, owner
); 
 838             return CYJSUndefined(context
); 
 841             return CYJSNull(context
); 
 843             if (hooks_ 
!= NULL 
&& hooks_
->FromFFI 
!= NULL
) 
 844                 if (JSValueRef value 
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
)) 
 847             CYThrow("unimplemented signature code: '%c''\n", type
->primitive
); 
 851 static void FunctionClosure_(ffi_cif 
*cif
, void *result
, void **arguments
, void *arg
) { 
 852     Closure_privateData 
*internal(reinterpret_cast<Closure_privateData 
*>(arg
)); 
 854     JSContextRef 
context(internal
->context_
); 
 856     size_t count(internal
->cif_
.nargs
); 
 857     JSValueRef values
[count
]; 
 859     for (size_t index(0); index 
!= count
; ++index
) 
 860         values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]); 
 862     JSValueRef 
value(CYCallAsFunction(context
, internal
->function_
, NULL
, count
, values
)); 
 863     CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
); 
 866 Closure_privateData 
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const char *type
, void (*callback
)(ffi_cif 
*, void *, void **, void *)) { 
 867     // XXX: in case of exceptions this will leak 
 868     // XXX: in point of fact, this may /need/ to leak :( 
 869     Closure_privateData 
*internal(new Closure_privateData(context
, function
, type
)); 
 871     ffi_closure 
*closure((ffi_closure 
*) _syscall(mmap( 
 872         NULL
, sizeof(ffi_closure
), 
 873         PROT_READ 
| PROT_WRITE
, MAP_ANON 
| MAP_PRIVATE
, 
 877     ffi_status 
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
)); 
 878     _assert(status 
== FFI_OK
); 
 880     _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ 
| PROT_EXEC
)); 
 882     internal
->value_ 
= closure
; 
 887 static JSObjectRef 
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const char *type
) { 
 888     Closure_privateData 
*internal(CYMakeFunctor_(context
, function
, type
, &FunctionClosure_
)); 
 889     return JSObjectMake(context
, Functor_
, internal
); 
 892 static JSObjectRef 
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const char *type
) { 
 893     JSValueRef 
exception(NULL
); 
 894     bool function(JSValueIsInstanceOfConstructor(context
, value
, Function_
, &exception
)); 
 895     CYThrow(context
, exception
); 
 898         JSObjectRef 
function(CYCastJSObject(context
, value
)); 
 899         return CYMakeFunctor(context
, function
, type
); 
 901         void (*function
)()(CYCastPointer
<void (*)()>(context
, value
)); 
 902         return CYMakeFunctor(context
, function
, type
); 
 906 static bool Index_(apr_pool_t 
*pool
, JSContextRef context
, Struct_privateData 
*internal
, JSStringRef property
, ssize_t 
&index
, uint8_t *&base
) { 
 907     Type_privateData 
*typical(internal
->type_
); 
 908     sig::Type 
*type(typical
->type_
); 
 912     const char *name(CYPoolCString(pool
, context
, property
)); 
 913     size_t length(strlen(name
)); 
 914     double number(CYCastDouble(name
, length
)); 
 916     size_t count(type
->data
.signature
.count
); 
 918     if (std::isnan(number
)) { 
 919         if (property 
== NULL
) 
 922         sig::Element 
*elements(type
->data
.signature
.elements
); 
 924         for (size_t local(0); local 
!= count
; ++local
) { 
 925             sig::Element 
*element(&elements
[local
]); 
 926             if (element
->name 
!= NULL 
&& strcmp(name
, element
->name
) == 0) { 
 934         index 
= static_cast<ssize_t
>(number
); 
 935         if (index 
!= number 
|| index 
< 0 || static_cast<size_t>(index
) >= count
) 
 940     ffi_type 
**elements(typical
->GetFFI()->elements
); 
 942     base 
= reinterpret_cast<uint8_t *>(internal
->value_
); 
 943     for (ssize_t 
local(0); local 
!= index
; ++local
) 
 944         base 
+= elements
[local
]->size
; 
 949 static JSValueRef 
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
 951     Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(object
))); 
 953     if (JSStringIsEqual(property
, length_
)) 
 954         return internal
->length_ 
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
); 
 956     Type_privateData 
*typical(internal
->type_
); 
 958     if (typical
->type_ 
== NULL
) 
 962     if (JSStringIsEqualToUTF8CString(property
, "$cyi")) 
 964     else if (!CYGetOffset(pool
, context
, property
, offset
)) 
 967     ffi_type 
*ffi(typical
->GetFFI()); 
 969     uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
)); 
 970     base 
+= ffi
->size 
* offset
; 
 972     JSObjectRef 
owner(internal
->GetOwner() ?: object
); 
 973     return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
); 
 976 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef 
*exception
) { CYTry 
{ 
 978     Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(object
))); 
 979     Type_privateData 
*typical(internal
->type_
); 
 981     if (typical
->type_ 
== NULL
) 
 985     if (JSStringIsEqualToUTF8CString(property
, "$cyi")) 
 987     else if (!CYGetOffset(pool
, context
, property
, offset
)) 
 990     ffi_type 
*ffi(typical
->GetFFI()); 
 992     uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
)); 
 993     base 
+= ffi
->size 
* offset
; 
 995     CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
); 
 999 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
1000     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(_this
))); 
1001     Type_privateData 
*typical(internal
->type_
); 
1002     return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
); 
1005 static JSValueRef 
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
1007     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(object
))); 
1008     Type_privateData 
*typical(internal
->type_
); 
1013     if (!Index_(pool
, context
, internal
, property
, index
, base
)) 
1016     JSObjectRef 
owner(internal
->GetOwner() ?: object
); 
1018     return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
); 
1021 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef 
*exception
) { CYTry 
{ 
1023     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(object
))); 
1024     Type_privateData 
*typical(internal
->type_
); 
1029     if (!Index_(pool
, context
, internal
, property
, index
, base
)) 
1032     CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
); 
1036 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) { 
1037     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(object
))); 
1038     Type_privateData 
*typical(internal
->type_
); 
1039     sig::Type 
*type(typical
->type_
); 
1044     size_t count(type
->data
.signature
.count
); 
1045     sig::Element 
*elements(type
->data
.signature
.elements
); 
1049     for (size_t index(0); index 
!= count
; ++index
) { 
1051         name 
= elements
[index
].name
; 
1054             sprintf(number
, "%zu", index
); 
1058         JSPropertyNameAccumulatorAddName(names
, CYJSString(name
)); 
1062 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 
{ 
1063     if (setups 
+ count 
!= signature
->count 
- 1) 
1064         throw CYJSError(context
, "incorrect number of arguments to ffi function"); 
1066     size_t size(setups 
+ count
); 
1068     memcpy(values
, setup
, sizeof(void *) * setups
); 
1070     for (size_t index(setups
); index 
!= size
; ++index
) { 
1071         sig::Element 
*element(&signature
->elements
[index 
+ 1]); 
1072         ffi_type 
*ffi(cif
->arg_types
[index
]); 
1074         values
[index
] = new(pool
) uint8_t[ffi
->size
]; 
1075         CYPoolFFI(pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index 
- setups
]); 
1078     uint8_t value
[cif
->rtype
->size
]; 
1080     if (hooks_ 
!= NULL 
&& hooks_
->CallFunction 
!= NULL
) 
1081         (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
); 
1083         ffi_call(cif
, function
, value
, values
); 
1085     return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
); 
1088 static JSValueRef 
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
1090     cy::Functor 
*internal(reinterpret_cast<cy::Functor 
*>(JSObjectGetPrivate(object
))); 
1091     return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, exception
, &internal
->signature_
, &internal
->cif_
, internal
->GetValue()); 
1094 static JSObjectRef 
CYMakeType(JSContextRef context
, const char *type
) { 
1095     Type_privateData 
*internal(new Type_privateData(type
)); 
1096     return JSObjectMake(context
, Type_privateData::Class_
, internal
); 
1099 static JSObjectRef 
CYMakeType(JSContextRef context
, sig::Type 
*type
) { 
1100     Type_privateData 
*internal(new Type_privateData(type
)); 
1101     return JSObjectMake(context
, Type_privateData::Class_
, internal
); 
1104 static void *CYCastSymbol(const char *name
) { 
1105     return dlsym(RTLD_DEFAULT
, name
); 
1108 static JSValueRef 
Runtime_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
1110     CYUTF8String 
name(CYPoolUTF8String(pool
, context
, property
)); 
1112     if (hooks_ 
!= NULL 
&& hooks_
->RuntimeProperty 
!= NULL
) 
1113         if (JSValueRef value 
= (*hooks_
->RuntimeProperty
)(context
, name
)) 
1116     sqlite3_stmt 
*statement
; 
1118     _sqlcall(sqlite3_prepare(Bridge_
, 
1120             "\"bridge\".\"mode\", " 
1121             "\"bridge\".\"value\" " 
1124             " \"bridge\".\"name\" = ?" 
1126     , -1, &statement
, NULL
)); 
1128     _sqlcall(sqlite3_bind_text(statement
, 1, name
.data
, name
.size
, SQLITE_STATIC
)); 
1133     if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
) { 
1137         mode 
= sqlite3_column_int(statement
, 0); 
1138         value 
= sqlite3_column_pooled(pool
, statement
, 1); 
1141     _sqlcall(sqlite3_finalize(statement
)); 
1150             return JSEvaluateScript(CYGetJSContext(context
), CYJSString(value
), NULL
, NULL
, 0, NULL
); 
1153             if (void (*symbol
)() = reinterpret_cast<void (*)()>(CYCastSymbol(name
.data
))) 
1154                 return CYMakeFunctor(context
, symbol
, value
); 
1158             if (void *symbol 
= CYCastSymbol(name
.data
)) { 
1159                 // XXX: this is horrendously inefficient 
1160                 sig::Signature signature
; 
1161                 sig::Parse(pool
, &signature
, value
, &Structor_
); 
1163                 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
); 
1164                 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
); 
1167         // XXX: implement case 3 
1169             return CYMakeType(context
, value
); 
1173 static JSObjectRef 
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1175         throw CYJSError(context
, "incorrect number of arguments to Functor constructor"); 
1179     void *value(CYCastPointer
<void *>(context
, arguments
[0])); 
1180     const char *type(CYPoolCString(pool
, context
, arguments
[1])); 
1182     sig::Signature signature
; 
1183     sig::Parse(pool
, &signature
, type
, &Structor_
); 
1185     return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
); 
1188 static JSObjectRef 
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1190         throw CYJSError(context
, "incorrect number of arguments to Type constructor"); 
1192     const char *type(CYPoolCString(pool
, context
, arguments
[0])); 
1193     return CYMakeType(context
, type
); 
1196 static JSValueRef 
Type_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
1197     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1201     if (JSStringIsEqualToUTF8CString(property
, "$cyi")) { 
1202         type
.primitive 
= sig::pointer_P
; 
1203         type
.data
.data
.size 
= 0; 
1206         size_t index(CYGetIndex(pool
, context
, property
)); 
1207         if (index 
== _not(size_t)) 
1209         type
.primitive 
= sig::array_P
; 
1210         type
.data
.data
.size 
= index
; 
1216     type
.data
.data
.type 
= internal
->type_
; 
1218     return CYMakeType(context
, &type
); 
1221 static JSValueRef 
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1222     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1225         throw CYJSError(context
, "incorrect number of arguments to type cast function"); 
1226     sig::Type 
*type(internal
->type_
); 
1227     ffi_type 
*ffi(internal
->GetFFI()); 
1229     uint8_t value
[ffi
->size
]; 
1231     CYPoolFFI(pool
, context
, type
, ffi
, value
, arguments
[0]); 
1232     return CYFromFFI(context
, type
, ffi
, value
); 
1235 static JSObjectRef 
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1237         throw CYJSError(context
, "incorrect number of arguments to type cast function"); 
1238     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1240     sig::Type 
*type(internal
->type_
); 
1243     if (type
->primitive 
!= sig::array_P
) 
1244         length 
= _not(size_t); 
1246         length 
= type
->data
.data
.size
; 
1247         type 
= type
->data
.data
.type
; 
1250     void *value(malloc(internal
->GetFFI()->size
)); 
1251     return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
); 
1254 static JSObjectRef 
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1256         throw CYJSError(context
, "incorrect number of arguments to Functor constructor"); 
1258     const char *type(CYPoolCString(pool
, context
, arguments
[1])); 
1259     return CYMakeFunctor(context
, arguments
[0], type
); 
1262 static JSValueRef 
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1263     CYValue 
*internal(reinterpret_cast<CYValue 
*>(JSObjectGetPrivate(_this
))); 
1264     return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
)); 
1267 static JSValueRef 
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
1268     return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
); 
1271 static JSValueRef 
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1272     CYValue 
*internal(reinterpret_cast<CYValue 
*>(JSObjectGetPrivate(_this
))); 
1274     sprintf(string
, "%p", internal
->value_
); 
1275     return CYCastJSValue(context
, string
); 
1278 static JSValueRef 
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1279     Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(_this
))); 
1280     if (internal
->length_ 
!= _not(size_t)) 
1281         // XXX: maybe dynamically look up Array.toCYON? 
1282         return Array_callAsFunction_toCYON(context
, object
, _this
, count
, arguments
, exception
); 
1285         sprintf(string
, "%p", internal
->value_
); 
1286         return CYCastJSValue(context
, string
); 
1290 static JSValueRef 
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1291     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1293     const char *type(sig::Unparse(pool
, internal
->type_
)); 
1294     return CYCastJSValue(context
, CYJSString(type
)); 
1297 static JSValueRef 
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1298     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1300     const char *type(sig::Unparse(pool
, internal
->type_
)); 
1301     size_t size(strlen(type
)); 
1302     char *cyon(new(pool
) char[12 + size 
+ 1]); 
1303     memcpy(cyon
, "new Type(\"", 10); 
1304     cyon
[12 + size
] = '\0'; 
1305     cyon
[12 + size 
- 2] = '"'; 
1306     cyon
[12 + size 
- 1] = ')'; 
1307     memcpy(cyon 
+ 10, type
, size
); 
1308     return CYCastJSValue(context
, CYJSString(cyon
)); 
1311 static JSValueRef 
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
1312     return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
); 
1315 static JSStaticFunction Pointer_staticFunctions
[4] = { 
1316     {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1317     {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1318     {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1322 static JSStaticFunction Struct_staticFunctions
[2] = { 
1323     {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1327 static JSStaticFunction Functor_staticFunctions
[4] = { 
1328     {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1329     {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1330     {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1335     JSStaticFunction 
const * const Functor::StaticFunctions 
= Functor_staticFunctions
; 
1338 static JSStaticFunction Type_staticFunctions
[4] = { 
1339     {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1340     {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1341     {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1345 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef 
*); 
1347 void CYSetArgs(int argc
, const char *argv
[]) { 
1348     JSContextRef 
context(CYGetJSContext()); 
1349     JSValueRef args
[argc
]; 
1350     for (int i(0); i 
!= argc
; ++i
) 
1351         args
[i
] = CYCastJSValue(context
, argv
[i
]); 
1354     if (JSObjectMakeArray$ 
!= NULL
) { 
1355         JSValueRef 
exception(NULL
); 
1356         array 
= (*JSObjectMakeArray$
)(context
, argc
, args
, &exception
); 
1357         CYThrow(context
, exception
); 
1359         JSValueRef 
value(CYCallAsFunction(context
, Array_
, NULL
, argc
, args
)); 
1360         array 
= CYCastJSObject(context
, value
); 
1363     CYSetProperty(context
, System_
, CYJSString("args"), array
); 
1366 JSObjectRef 
CYGetGlobalObject(JSContextRef context
) { 
1367     return JSContextGetGlobalObject(context
); 
1370 const char *CYExecute(apr_pool_t 
*pool
, const char *code
) { 
1371     JSContextRef 
context(CYGetJSContext()); 
1372     JSValueRef 
exception(NULL
), result
; 
1375     if (hooks_ 
!= NULL 
&& hooks_
->ExecuteStart 
!= NULL
) 
1376         handle 
= (*hooks_
->ExecuteStart
)(context
); 
1383         result 
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
); 
1384     } catch (const char *error
) { 
1388     if (exception 
!= NULL
) { error
: 
1393     if (JSValueIsUndefined(context
, result
)) 
1397         json 
= CYPoolCCYON(pool
, context
, result
, &exception
); 
1398     } catch (const char *error
) { 
1402     if (exception 
!= NULL
) 
1405     CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
); 
1407     if (hooks_ 
!= NULL 
&& hooks_
->ExecuteEnd 
!= NULL
) 
1408         (*hooks_
->ExecuteEnd
)(context
, handle
); 
1412 static apr_pool_t 
*Pool_
; 
1414 static bool initialized_
; 
1416 void CYInitialize() { 
1418         initialized_ 
= true; 
1421     _aprcall(apr_initialize()); 
1422     _aprcall(apr_pool_create(&Pool_
, NULL
)); 
1423     _sqlcall(sqlite3_open("/usr/lib/libcycript.db", &Bridge_
)); 
1425     JSObjectMakeArray$ 
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef 
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray")); 
1427     JSClassDefinition definition
; 
1429     definition 
= kJSClassDefinitionEmpty
; 
1430     definition
.className 
= "Functor"; 
1431     definition
.staticFunctions 
= cy::Functor::StaticFunctions
; 
1432     definition
.callAsFunction 
= &Functor_callAsFunction
; 
1433     definition
.finalize 
= &CYFinalize
; 
1434     Functor_ 
= JSClassCreate(&definition
); 
1436     definition 
= kJSClassDefinitionEmpty
; 
1437     definition
.className 
= "Pointer"; 
1438     definition
.staticFunctions 
= Pointer_staticFunctions
; 
1439     definition
.getProperty 
= &Pointer_getProperty
; 
1440     definition
.setProperty 
= &Pointer_setProperty
; 
1441     definition
.finalize 
= &CYFinalize
; 
1442     Pointer_ 
= JSClassCreate(&definition
); 
1444     definition 
= kJSClassDefinitionEmpty
; 
1445     definition
.className 
= "Struct"; 
1446     definition
.staticFunctions 
= Struct_staticFunctions
; 
1447     definition
.getProperty 
= &Struct_getProperty
; 
1448     definition
.setProperty 
= &Struct_setProperty
; 
1449     definition
.getPropertyNames 
= &Struct_getPropertyNames
; 
1450     definition
.finalize 
= &CYFinalize
; 
1451     Struct_ 
= JSClassCreate(&definition
); 
1453     definition 
= kJSClassDefinitionEmpty
; 
1454     definition
.className 
= "Type"; 
1455     definition
.staticFunctions 
= Type_staticFunctions
; 
1456     definition
.getProperty 
= &Type_getProperty
; 
1457     definition
.callAsFunction 
= &Type_callAsFunction
; 
1458     definition
.callAsConstructor 
= &Type_callAsConstructor
; 
1459     definition
.finalize 
= &CYFinalize
; 
1460     Type_privateData::Class_ 
= JSClassCreate(&definition
); 
1462     definition 
= kJSClassDefinitionEmpty
; 
1463     definition
.className 
= "Runtime"; 
1464     definition
.getProperty 
= &Runtime_getProperty
; 
1465     Runtime_ 
= JSClassCreate(&definition
); 
1467     definition 
= kJSClassDefinitionEmpty
; 
1468     //definition.getProperty = &Global_getProperty; 
1469     Global_ 
= JSClassCreate(&definition
); 
1471     length_ 
= JSStringCreateWithUTF8CString("length"); 
1472     message_ 
= JSStringCreateWithUTF8CString("message"); 
1473     name_ 
= JSStringCreateWithUTF8CString("name"); 
1474     prototype_ 
= JSStringCreateWithUTF8CString("prototype"); 
1475     toCYON_ 
= JSStringCreateWithUTF8CString("toCYON"); 
1476     toJSON_ 
= JSStringCreateWithUTF8CString("toJSON"); 
1478     Result_ 
= JSStringCreateWithUTF8CString("_"); 
1480     if (hooks_ 
!= NULL 
&& hooks_
->Initialize 
!= NULL
) 
1481         (*hooks_
->Initialize
)(); 
1484 apr_pool_t 
*CYGetGlobalPool() { 
1489 void CYThrow(JSContextRef context
, JSValueRef value
) { 
1491         throw CYJSError(context
, value
); 
1494 const char *CYJSError::PoolCString(apr_pool_t 
*pool
) const { 
1495     return CYPoolCString(pool
, context_
, value_
); 
1498 JSValueRef 
CYJSError::CastJSValue(JSContextRef context
) const { 
1499     // XXX: what if the context is different? 
1503 void CYThrow(const char *format
, ...) { 
1505     va_start (args
, format
); 
1506     throw CYPoolError(format
, args
); 
1507     // XXX: does this matter? :( 
1511 const char *CYPoolError::PoolCString(apr_pool_t 
*pool
) const { 
1512     return apr_pstrdup(pool
, message_
); 
1515 CYPoolError::CYPoolError(const char *format
, ...) { 
1517     va_start (args
, format
); 
1518     message_ 
= apr_pvsprintf(pool_
, format
, args
); 
1522 CYPoolError::CYPoolError(const char *format
, va_list args
) { 
1523     message_ 
= apr_pvsprintf(pool_
, format
, args
); 
1526 JSValueRef 
CYCastJSError(JSContextRef context
, const char *message
) { 
1527     JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)}; 
1529     JSValueRef 
exception(NULL
); 
1530     JSValueRef 
value(JSObjectCallAsConstructor(context
, Error_
, 1, arguments
, &exception
)); 
1531     CYThrow(context
, exception
); 
1536 JSValueRef 
CYPoolError::CastJSValue(JSContextRef context
) const { 
1537     return CYCastJSError(context
, message_
); 
1540 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) { 
1541     _assert(context 
!= NULL
); 
1546     va_start (args
, format
); 
1547     const char *message(apr_pvsprintf(pool
, format
, args
)); 
1550     value_ 
= CYCastJSError(context
, message
); 
1553 JSGlobalContextRef 
CYGetJSContext(JSContextRef context
) { 
1554     // XXX: do something better 
1558 void CYSetupContext(JSGlobalContextRef context
) { 
1559     JSObjectRef 
global(CYGetGlobalObject(context
)); 
1561     JSObjectSetPrototype(context
, global
, JSObjectMake(context
, Runtime_
, NULL
)); 
1563     Array_ 
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))); 
1564     JSValueProtect(context
, Array_
); 
1566     Error_ 
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))); 
1567     JSValueProtect(context
, Error_
); 
1569     Function_ 
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))); 
1570     JSValueProtect(context
, Function_
); 
1572     String_ 
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))); 
1573     JSValueProtect(context
, String_
); 
1575     JSObjectRef 
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object")))); 
1576     Object_prototype_ 
= CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_
)); 
1577     JSValueProtect(context
, Object_prototype_
); 
1579     Array_prototype_ 
= CYCastJSObject(context
, CYGetProperty(context
, Array_
, prototype_
)); 
1580     Array_pop_ 
= CYCastJSObject(context
, CYGetProperty(context
, Array_prototype_
, CYJSString("pop"))); 
1581     Array_push_ 
= CYCastJSObject(context
, CYGetProperty(context
, Array_prototype_
, CYJSString("push"))); 
1582     Array_splice_ 
= CYCastJSObject(context
, CYGetProperty(context
, Array_prototype_
, CYJSString("splice"))); 
1584     CYSetProperty(context
, Array_prototype_
, toCYON_
, JSObjectMakeFunctionWithCallback(context
, toCYON_
, &Array_callAsFunction_toCYON
), kJSPropertyAttributeDontEnum
); 
1586     JSValueProtect(context
, Array_prototype_
); 
1587     JSValueProtect(context
, Array_pop_
); 
1588     JSValueProtect(context
, Array_push_
); 
1589     JSValueProtect(context
, Array_splice_
); 
1591     JSObjectRef 
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
)); 
1593     Function_prototype_ 
= (JSObjectRef
) CYGetProperty(context
, Function_
, prototype_
); 
1594     JSValueProtect(context
, Function_prototype_
); 
1596     JSObjectSetPrototype(context
, (JSObjectRef
) CYGetProperty(context
, Functor
, prototype_
), Function_prototype_
); 
1598     CYSetProperty(context
, global
, CYJSString("Functor"), Functor
); 
1599     CYSetProperty(context
, global
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
)); 
1600     CYSetProperty(context
, global
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
)); 
1602     JSObjectRef 
cycript(JSObjectMake(context
, NULL
, NULL
)); 
1603     CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
); 
1604     CYSetProperty(context
, cycript
, CYJSString("gc"), JSObjectMakeFunctionWithCallback(context
, CYJSString("gc"), &Cycript_gc_callAsFunction
)); 
1606     CYSetProperty(context
, global
, CYJSString("$cyq"), JSObjectMakeFunctionWithCallback(context
, CYJSString("$cyq"), &$cyq
)); 
1608     System_ 
= JSObjectMake(context
, NULL
, NULL
); 
1609     JSValueProtect(context
, System_
); 
1611     CYSetProperty(context
, global
, CYJSString("system"), System_
); 
1612     CYSetProperty(context
, System_
, CYJSString("args"), CYJSNull(context
)); 
1613     //CYSetProperty(context, System_, CYJSString("global"), global); 
1615     CYSetProperty(context
, System_
, CYJSString("print"), JSObjectMakeFunctionWithCallback(context
, CYJSString("print"), &System_print
)); 
1617     if (hooks_ 
!= NULL 
&& hooks_
->SetupContext 
!= NULL
) 
1618         (*hooks_
->SetupContext
)(context
); 
1621 JSGlobalContextRef 
CYGetJSContext() { 
1624     if (Context_ 
== NULL
) { 
1625         Context_ 
= JSGlobalContextCreate(Global_
); 
1626         CYSetupContext(Context_
);