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 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef (*callback
)(JSContextRef
, JSObjectRef
, JSObjectRef
, size_t, const JSValueRef
[], JSValueRef 
*), JSPropertyAttributes attributes 
= kJSPropertyAttributeNone
) { 
 117     CYSetProperty(context
, object
, name
, JSObjectMakeFunctionWithCallback(context
, name
, callback
), attributes
); 
 120 /* JavaScript Strings {{{ */ 
 121 JSStringRef 
CYCopyJSString(const char *value
) { 
 122     return value 
== NULL 
? NULL 
: JSStringCreateWithUTF8CString(value
); 
 125 JSStringRef 
CYCopyJSString(JSStringRef value
) { 
 126     return value 
== NULL 
? NULL 
: JSStringRetain(value
); 
 129 JSStringRef 
CYCopyJSString(CYUTF8String value
) { 
 130     // XXX: this is very wrong 
 131     return CYCopyJSString(value
.data
); 
 134 JSStringRef 
CYCopyJSString(JSContextRef context
, JSValueRef value
) { 
 135     if (JSValueIsNull(context
, value
)) 
 137     JSValueRef 
exception(NULL
); 
 138     JSStringRef 
string(JSValueToStringCopy(context
, value
, &exception
)); 
 139     CYThrow(context
, exception
); 
 143 static CYUTF16String 
CYCastUTF16String(JSStringRef value
) { 
 144     return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
)); 
 147 template <typename Type_
> 
 148 _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
) { 
 149     return iconv(cd
, const_cast<Type_
>(inbuf
), inbytesleft
, outbuf
, outbytesleft
); 
 152 CYUTF8String 
CYPoolUTF8String(apr_pool_t 
*pool
, JSContextRef context
, JSStringRef value
) { 
 153     _assert(pool 
!= NULL
); 
 155     CYUTF16String 
utf16(CYCastUTF16String(value
)); 
 156     const char *in(reinterpret_cast<const char *>(utf16
.data
)); 
 159     iconv_t 
conversion(_syscall(iconv_open("UTF-8", "UCS-2"))); 
 161     iconv_t 
conversion(_syscall(iconv_open("UTF-8", "UCS-2-INTERNAL"))); 
 164     size_t size(JSStringGetMaximumUTF8CStringSize(value
)); 
 165     char *out(new(pool
) char[size
]); 
 166     CYUTF8String 
utf8(out
, size
); 
 168     size 
= utf16
.size 
* 2; 
 169     _syscall(iconv_(&iconv
, conversion
, const_cast<char **>(&in
), &size
, &out
, &utf8
.size
)); 
 172     utf8
.size 
= out 
- utf8
.data
; 
 174     _syscall(iconv_close(conversion
)); 
 179 const char *CYPoolCString(apr_pool_t 
*pool
, JSContextRef context
, JSStringRef value
) { 
 180     CYUTF8String 
utf8(CYPoolUTF8String(pool
, context
, value
)); 
 181     _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
); 
 185 const char *CYPoolCString(apr_pool_t 
*pool
, JSContextRef context
, JSValueRef value
) { 
 186     return JSValueIsNull(context
, value
) ? NULL 
: CYPoolCString(pool
, context
, CYJSString(context
, value
)); 
 190 /* Index Offsets {{{ */ 
 191 size_t CYGetIndex(const CYUTF8String 
&value
) { 
 192     if (value
.data
[0] != '0') { 
 194         for (size_t i(0); i 
!= value
.size
; ++i
) { 
 195             if (!DigitRange_
[value
.data
[i
]]) 
 198             index 
+= value
.data
[i
] - '0'; 
 201     } else if (value
.size 
== 1) 
 207 size_t CYGetIndex(apr_pool_t 
*pool
, JSContextRef context
, JSStringRef value
) { 
 208     return CYGetIndex(CYPoolUTF8String(pool
, context
, value
)); 
 211 // XXX: this isn't actually right 
 212 bool CYGetOffset(const char *value
, ssize_t 
&index
) { 
 213     if (value
[0] != '0') { 
 215         index 
= strtol(value
, &end
, 10); 
 216         if (value 
+ strlen(value
) == end
) 
 218     } else if (value
[1] == '\0') { 
 227 /* JavaScript *ify {{{ */ 
 228 void CYStringify(std::ostringstream 
&str
, const char *data
, size_t size
) { 
 229     unsigned quot(0), apos(0); 
 230     for (const char *value(data
), *end(data 
+ size
); value 
!= end
; ++value
) 
 233         else if (*value 
== '\'') 
 236     bool single(quot 
> apos
); 
 238     str 
<< (single 
? '\'' : '"'); 
 240     for (const char *value(data
), *end(data 
+ size
); value 
!= end
; ++value
) 
 242             case '\\': str 
<< "\\\\"; break; 
 243             case '\b': str 
<< "\\b"; break; 
 244             case '\f': str 
<< "\\f"; break; 
 245             case '\n': str 
<< "\\n"; break; 
 246             case '\r': str 
<< "\\r"; break; 
 247             case '\t': str 
<< "\\t"; break; 
 248             case '\v': str 
<< "\\v"; break; 
 263                 // this test is designed to be "awewsome", generating neither warnings nor incorrect results 
 264                 if (*value 
< 0x20 || *value 
>= 0x7f) 
 265                     str 
<< "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(uint8_t(*value
)); 
 270     str 
<< (single 
? '\'' : '"'); 
 273 void CYNumerify(std::ostringstream 
&str
, double value
) { 
 275     // XXX: I want this to print 1e3 rather than 1000 
 276     sprintf(string
, "%.17g", value
); 
 280 bool CYIsKey(CYUTF8String value
) { 
 281     const char *data(value
.data
); 
 282     size_t size(value
.size
); 
 287     if (DigitRange_
[data
[0]]) { 
 288         size_t index(CYGetIndex(value
)); 
 289         if (index 
== _not(size_t)) 
 292         if (!WordStartRange_
[data
[0]]) 
 294         for (size_t i(1); i 
!= size
; ++i
) 
 295             if (!WordEndRange_
[data
[i
]]) 
 303 static JSGlobalContextRef Context_
; 
 304 static JSObjectRef System_
; 
 306 static JSClassRef Functor_
; 
 307 static JSClassRef Global_
; 
 308 static JSClassRef Pointer_
; 
 309 static JSClassRef Runtime_
; 
 310 static JSClassRef Struct_
; 
 314 JSObjectRef Function_
; 
 317 JSObjectRef Array_prototype_
; 
 318 JSObjectRef Function_prototype_
; 
 319 JSObjectRef Object_prototype_
; 
 321 JSStringRef length_s
; 
 322 JSStringRef message_s
; 
 325 JSStringRef prototype_s
; 
 327 JSStringRef splice_s
; 
 328 JSStringRef toCYON_s
; 
 329 JSStringRef toJSON_s
; 
 331 static JSStringRef Result_
; 
 335 void CYFinalize(JSObjectRef object
) { 
 336     delete reinterpret_cast<CYData 
*>(JSObjectGetPrivate(object
)); 
 339 struct CStringMapLess 
: 
 340     std::binary_function
<const char *, const char *, bool> 
 342     _finline 
bool operator ()(const char *lhs
, const char *rhs
) const { 
 343         return strcmp(lhs
, rhs
) < 0; 
 347 void Structor_(apr_pool_t 
*pool
, sig::Type 
*&type
) { 
 349         type
->primitive 
== sig::pointer_P 
&& 
 350         type
->data
.data
.type 
!= NULL 
&& 
 351         type
->data
.data
.type
->primitive 
== sig::struct_P 
&& 
 352         strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0 
 354         type
->primitive 
= sig::typename_P
; 
 355         type
->data
.data
.type 
= NULL
; 
 359     if (type
->primitive 
!= sig::struct_P 
|| type
->name 
== NULL
) 
 362     sqlite3_stmt 
*statement
; 
 364     _sqlcall(sqlite3_prepare(Bridge_
, 
 366             "\"bridge\".\"mode\", " 
 367             "\"bridge\".\"value\" " 
 370             " \"bridge\".\"mode\" in (3, 4) and" 
 371             " \"bridge\".\"name\" = ?" 
 373     , -1, &statement
, NULL
)); 
 375     _sqlcall(sqlite3_bind_text(statement
, 1, type
->name
, -1, SQLITE_STATIC
)); 
 380     if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
) { 
 384         mode 
= sqlite3_column_int(statement
, 0); 
 385         value 
= sqlite3_column_pooled(pool
, statement
, 1); 
 388     _sqlcall(sqlite3_finalize(statement
)); 
 397             sig::Parse(pool
, &type
->data
.signature
, value
, &Structor_
); 
 401             sig::Signature signature
; 
 402             sig::Parse(pool
, &signature
, value
, &Structor_
); 
 403             type 
= signature
.elements
[0].type
; 
 408 JSClassRef 
Type_privateData::Class_
; 
 413     Type_privateData 
*type_
; 
 416     Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type 
*type
) : 
 417         CYOwned(value
, context
, owner
), 
 418         type_(new(pool_
) Type_privateData(type
)), 
 424 struct Struct_privateData 
: 
 427     Type_privateData 
*type_
; 
 429     Struct_privateData(JSContextRef context
, JSObjectRef owner
) : 
 430         CYOwned(NULL
, context
, owner
) 
 435 typedef std::map
<const char *, Type_privateData 
*, CStringMapLess
> TypeMap
; 
 436 static TypeMap Types_
; 
 438 JSObjectRef 
CYMakeStruct(JSContextRef context
, void *data
, sig::Type 
*type
, ffi_type 
*ffi
, JSObjectRef owner
) { 
 439     Struct_privateData 
*internal(new Struct_privateData(context
, owner
)); 
 440     apr_pool_t 
*pool(internal
->pool_
); 
 441     Type_privateData 
*typical(new(pool
) Type_privateData(type
, ffi
)); 
 442     internal
->type_ 
= typical
; 
 445         internal
->value_ 
= data
; 
 447         size_t size(typical
->GetFFI()->size
); 
 448         void *copy(apr_palloc(internal
->pool_
, size
)); 
 449         memcpy(copy
, data
, size
); 
 450         internal
->value_ 
= copy
; 
 453     return JSObjectMake(context
, Struct_
, internal
); 
 456 JSValueRef 
CYCastJSValue(JSContextRef context
, bool value
) { 
 457     return JSValueMakeBoolean(context
, value
); 
 460 JSValueRef 
CYCastJSValue(JSContextRef context
, double value
) { 
 461     return JSValueMakeNumber(context
, value
); 
 464 #define CYCastJSValue_(Type_) \ 
 465     JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \ 
 466         return JSValueMakeNumber(context, static_cast<double>(value)); \ 
 470 CYCastJSValue_(unsigned int) 
 471 CYCastJSValue_(long int) 
 472 CYCastJSValue_(long unsigned int) 
 473 CYCastJSValue_(long long int) 
 474 CYCastJSValue_(long long unsigned int) 
 476 JSValueRef 
CYJSUndefined(JSContextRef context
) { 
 477     return JSValueMakeUndefined(context
); 
 480 double CYCastDouble(const char *value
, size_t size
) { 
 482     double number(strtod(value
, &end
)); 
 483     if (end 
!= value 
+ size
) 
 488 double CYCastDouble(const char *value
) { 
 489     return CYCastDouble(value
, strlen(value
)); 
 492 double CYCastDouble(JSContextRef context
, JSValueRef value
) { 
 493     JSValueRef 
exception(NULL
); 
 494     double number(JSValueToNumber(context
, value
, &exception
)); 
 495     CYThrow(context
, exception
); 
 499 bool CYCastBool(JSContextRef context
, JSValueRef value
) { 
 500     return JSValueToBoolean(context
, value
); 
 503 JSValueRef 
CYJSNull(JSContextRef context
) { 
 504     return JSValueMakeNull(context
); 
 507 JSValueRef 
CYCastJSValue(JSContextRef context
, JSStringRef value
) { 
 508     return value 
== NULL 
? CYJSNull(context
) : JSValueMakeString(context
, value
); 
 511 JSValueRef 
CYCastJSValue(JSContextRef context
, const char *value
) { 
 512     return CYCastJSValue(context
, CYJSString(value
)); 
 515 JSObjectRef 
CYCastJSObject(JSContextRef context
, JSValueRef value
) { 
 516     JSValueRef 
exception(NULL
); 
 517     JSObjectRef 
object(JSValueToObject(context
, value
, &exception
)); 
 518     CYThrow(context
, exception
); 
 522 JSValueRef 
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, JSValueRef arguments
[]) { 
 523     JSValueRef 
exception(NULL
); 
 524     JSValueRef 
value(JSObjectCallAsFunction(context
, function
, _this
, count
, arguments
, &exception
)); 
 525     CYThrow(context
, exception
); 
 529 bool CYIsCallable(JSContextRef context
, JSValueRef value
) { 
 530     return value 
!= NULL 
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
); 
 533 static JSValueRef 
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 538         printf("%s\n", CYPoolCString(pool
, context
, arguments
[0])); 
 541     return CYJSUndefined(context
); 
 544 static size_t Nonce_(0); 
 546 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
 548     const char *name(apr_psprintf(pool
, "%s%"APR_SIZE_T_FMT
"", CYPoolCString(pool
, context
, arguments
[0]), Nonce_
++)); 
 549     return CYCastJSValue(context
, name
); 
 552 static JSValueRef 
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
 553     JSGarbageCollect(context
); 
 554     return CYJSUndefined(context
); 
 557 const char *CYPoolCCYON(apr_pool_t 
*pool
, JSContextRef context
, JSValueRef value
, JSValueRef 
*exception
) { CYTry 
{ 
 558     switch (JSType type 
= JSValueGetType(context
, value
)) { 
 559         case kJSTypeUndefined
: 
 564             return CYCastBool(context
, value
) ? "true" : "false"; 
 566         case kJSTypeNumber
: { 
 567             std::ostringstream str
; 
 568             CYNumerify(str
, CYCastDouble(context
, value
)); 
 569             std::string 
value(str
.str()); 
 570             return apr_pstrmemdup(pool
, value
.c_str(), value
.size()); 
 573         case kJSTypeString
: { 
 574             std::ostringstream str
; 
 575             CYUTF8String 
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
))); 
 576             CYStringify(str
, string
.data
, string
.size
); 
 577             std::string 
value(str
.str()); 
 578             return apr_pstrmemdup(pool
, value
.c_str(), value
.size()); 
 582             return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
); 
 584             throw CYJSError(context
, "JSValueGetType() == 0x%x", type
); 
 588 const char *CYPoolCCYON(apr_pool_t 
*pool
, JSContextRef context
, JSValueRef value
) { 
 589     JSValueRef 
exception(NULL
); 
 590     const char *cyon(CYPoolCCYON(pool
, context
, value
, &exception
)); 
 591     CYThrow(context
, exception
); 
 595 const char *CYPoolCCYON(apr_pool_t 
*pool
, JSContextRef context
, JSObjectRef object
) { 
 596     JSValueRef 
toCYON(CYGetProperty(context
, object
, toCYON_s
)); 
 597     if (CYIsCallable(context
, toCYON
)) { 
 598         JSValueRef 
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
)); 
 599         return CYPoolCString(pool
, context
, value
); 
 602     JSValueRef 
toJSON(CYGetProperty(context
, object
, toJSON_s
)); 
 603     if (CYIsCallable(context
, toJSON
)) { 
 604         JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))}; 
 605         JSValueRef 
exception(NULL
); 
 606         const char *cyon(CYPoolCCYON(pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), &exception
)); 
 607         CYThrow(context
, exception
); 
 611     std::ostringstream str
; 
 615     // XXX: this is, sadly, going to leak 
 616     JSPropertyNameArrayRef 
names(JSObjectCopyPropertyNames(context
, object
)); 
 620     for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index 
!= count
; ++index
) { 
 621         JSStringRef 
name(JSPropertyNameArrayGetNameAtIndex(names
, index
)); 
 622         JSValueRef 
value(CYGetProperty(context
, object
, name
)); 
 629         CYUTF8String 
string(CYPoolUTF8String(pool
, context
, name
)); 
 633             CYStringify(str
, string
.data
, string
.size
); 
 635         str 
<< ':' << CYPoolCCYON(pool
, context
, value
); 
 640     JSPropertyNameArrayRelease(names
); 
 642     std::string 
string(str
.str()); 
 643     return apr_pstrmemdup(pool
, string
.c_str(), string
.size()); 
 646 static JSValueRef 
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
 648     std::ostringstream str
; 
 652     JSValueRef 
length(CYGetProperty(context
, _this
, length_s
)); 
 655     for (size_t index(0), count(CYCastDouble(context
, length
)); index 
!= count
; ++index
) { 
 656         JSValueRef 
value(CYGetProperty(context
, _this
, index
)); 
 663         if (!JSValueIsUndefined(context
, value
)) 
 664             str 
<< CYPoolCCYON(pool
, context
, value
); 
 673     std::string 
value(str
.str()); 
 674     return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size()))); 
 677 JSObjectRef 
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type 
*type
, ffi_type 
*ffi
, JSObjectRef owner
) { 
 678     Pointer 
*internal(new Pointer(pointer
, context
, owner
, length
, type
)); 
 679     return JSObjectMake(context
, Pointer_
, internal
); 
 682 static JSObjectRef 
CYMakeFunctor(JSContextRef context
, void (*function
)(), const char *type
) { 
 683     cy::Functor 
*internal(new cy::Functor(type
, function
)); 
 684     return JSObjectMake(context
, Functor_
, internal
); 
 687 static bool CYGetOffset(apr_pool_t 
*pool
, JSContextRef context
, JSStringRef value
, ssize_t 
&index
) { 
 688     return CYGetOffset(CYPoolCString(pool
, context
, value
), index
); 
 691 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) { 
 692     switch (JSValueGetType(context
, value
)) { 
 695         /*case kJSTypeObject: 
 696             if (JSValueIsObjectOfClass(context, value, Pointer_)) { 
 697                 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value))); 
 698                 return internal->value_; 
 701             double number(CYCastDouble(context
, value
)); 
 702             if (std::isnan(number
)) 
 703                 throw CYJSError(context
, "cannot convert value to pointer"); 
 704             return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
))); 
 708 void CYPoolFFI(apr_pool_t 
*pool
, JSContextRef context
, sig::Type 
*type
, ffi_type 
*ffi
, void *data
, JSValueRef value
) { 
 709     switch (type
->primitive
) { 
 711             *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
); 
 714 #define CYPoolFFI_(primitive, native) \ 
 715         case sig::primitive ## _P: \ 
 716             *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \ 
 719         CYPoolFFI_(uchar
, unsigned char) 
 720         CYPoolFFI_(char, char) 
 721         CYPoolFFI_(ushort
, unsigned short) 
 722         CYPoolFFI_(short, short) 
 723         CYPoolFFI_(ulong
, unsigned long) 
 724         CYPoolFFI_(long, long) 
 725         CYPoolFFI_(uint
, unsigned int) 
 727         CYPoolFFI_(ulonglong
, unsigned long long) 
 728         CYPoolFFI_(longlong
, long long) 
 729         CYPoolFFI_(float, float) 
 730         CYPoolFFI_(double, double) 
 733             uint8_t *base(reinterpret_cast<uint8_t *>(data
)); 
 734             JSObjectRef 
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value 
: NULL
); 
 735             for (size_t index(0); index 
!= type
->data
.data
.size
; ++index
) { 
 736                 ffi_type 
*field(ffi
->elements
[index
]); 
 739                 if (aggregate 
== NULL
) 
 742                     rhs 
= CYGetProperty(context
, aggregate
, index
); 
 743                     if (JSValueIsUndefined(context
, rhs
)) 
 744                         throw CYJSError(context
, "unable to extract array value"); 
 747                 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
); 
 754             *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
); 
 758             *reinterpret_cast<const char **>(data
) = CYPoolCString(pool
, context
, value
); 
 761         case sig::struct_P
: { 
 762             uint8_t *base(reinterpret_cast<uint8_t *>(data
)); 
 763             JSObjectRef 
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value 
: NULL
); 
 764             for (size_t index(0); index 
!= type
->data
.signature
.count
; ++index
) { 
 765                 sig::Element 
*element(&type
->data
.signature
.elements
[index
]); 
 766                 ffi_type 
*field(ffi
->elements
[index
]); 
 769                 if (aggregate 
== NULL
) 
 772                     rhs 
= CYGetProperty(context
, aggregate
, index
); 
 773                     if (JSValueIsUndefined(context
, rhs
)) { 
 774                         if (element
->name 
!= NULL
) 
 775                             rhs 
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
)); 
 778                         if (JSValueIsUndefined(context
, rhs
)) undefined
: 
 779                             throw CYJSError(context
, "unable to extract structure value"); 
 783                 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
); 
 793             if (hooks_ 
!= NULL 
&& hooks_
->PoolFFI 
!= NULL
) 
 794                 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
)) 
 797             CYThrow("unimplemented signature code: '%c''\n", type
->primitive
); 
 801 JSValueRef 
CYFromFFI(JSContextRef context
, sig::Type 
*type
, ffi_type 
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) { 
 802     switch (type
->primitive
) { 
 804             return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
)); 
 806 #define CYFromFFI_(primitive, native) \ 
 807         case sig::primitive ## _P: \ 
 808             return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \ 
 810         CYFromFFI_(uchar, unsigned char) 
 811         CYFromFFI_(char, char) 
 812         CYFromFFI_(ushort
, unsigned short) 
 813         CYFromFFI_(short, short) 
 814         CYFromFFI_(ulong
, unsigned long) 
 815         CYFromFFI_(long, long) 
 816         CYFromFFI_(uint
, unsigned int) 
 818         CYFromFFI_(ulonglong
, unsigned long long) 
 819         CYFromFFI_(longlong
, long long) 
 820         CYFromFFI_(float, float) 
 821         CYFromFFI_(double, double) 
 824             if (void *pointer 
= data
) 
 825                 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
); 
 829             if (void *pointer 
= *reinterpret_cast<void **>(data
)) 
 830                 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
); 
 834             if (char *utf8 
= *reinterpret_cast<char **>(data
)) 
 835                 return CYCastJSValue(context
, utf8
); 
 839             return CYMakeStruct(context
, data
, type
, ffi
, owner
); 
 841             return CYJSUndefined(context
); 
 844             return CYJSNull(context
); 
 846             if (hooks_ 
!= NULL 
&& hooks_
->FromFFI 
!= NULL
) 
 847                 if (JSValueRef value 
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
)) 
 850             CYThrow("unimplemented signature code: '%c''\n", type
->primitive
); 
 854 static void FunctionClosure_(ffi_cif 
*cif
, void *result
, void **arguments
, void *arg
) { 
 855     Closure_privateData 
*internal(reinterpret_cast<Closure_privateData 
*>(arg
)); 
 857     JSContextRef 
context(internal
->context_
); 
 859     size_t count(internal
->cif_
.nargs
); 
 860     JSValueRef values
[count
]; 
 862     for (size_t index(0); index 
!= count
; ++index
) 
 863         values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]); 
 865     JSValueRef 
value(CYCallAsFunction(context
, internal
->function_
, NULL
, count
, values
)); 
 866     CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
); 
 869 Closure_privateData 
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const char *type
, void (*callback
)(ffi_cif 
*, void *, void **, void *)) { 
 870     // XXX: in case of exceptions this will leak 
 871     // XXX: in point of fact, this may /need/ to leak :( 
 872     Closure_privateData 
*internal(new Closure_privateData(context
, function
, type
)); 
 874     ffi_closure 
*closure((ffi_closure 
*) _syscall(mmap( 
 875         NULL
, sizeof(ffi_closure
), 
 876         PROT_READ 
| PROT_WRITE
, MAP_ANON 
| MAP_PRIVATE
, 
 880     ffi_status 
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
)); 
 881     _assert(status 
== FFI_OK
); 
 883     _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ 
| PROT_EXEC
)); 
 885     internal
->value_ 
= closure
; 
 890 static JSObjectRef 
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const char *type
) { 
 891     Closure_privateData 
*internal(CYMakeFunctor_(context
, function
, type
, &FunctionClosure_
)); 
 892     return JSObjectMake(context
, Functor_
, internal
); 
 895 static JSObjectRef 
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const char *type
) { 
 896     JSValueRef 
exception(NULL
); 
 897     bool function(JSValueIsInstanceOfConstructor(context
, value
, Function_
, &exception
)); 
 898     CYThrow(context
, exception
); 
 901         JSObjectRef 
function(CYCastJSObject(context
, value
)); 
 902         return CYMakeFunctor(context
, function
, type
); 
 904         void (*function
)()(CYCastPointer
<void (*)()>(context
, value
)); 
 905         return CYMakeFunctor(context
, function
, type
); 
 909 static bool Index_(apr_pool_t 
*pool
, JSContextRef context
, Struct_privateData 
*internal
, JSStringRef property
, ssize_t 
&index
, uint8_t *&base
) { 
 910     Type_privateData 
*typical(internal
->type_
); 
 911     sig::Type 
*type(typical
->type_
); 
 915     const char *name(CYPoolCString(pool
, context
, property
)); 
 916     size_t length(strlen(name
)); 
 917     double number(CYCastDouble(name
, length
)); 
 919     size_t count(type
->data
.signature
.count
); 
 921     if (std::isnan(number
)) { 
 922         if (property 
== NULL
) 
 925         sig::Element 
*elements(type
->data
.signature
.elements
); 
 927         for (size_t local(0); local 
!= count
; ++local
) { 
 928             sig::Element 
*element(&elements
[local
]); 
 929             if (element
->name 
!= NULL 
&& strcmp(name
, element
->name
) == 0) { 
 937         index 
= static_cast<ssize_t
>(number
); 
 938         if (index 
!= number 
|| index 
< 0 || static_cast<size_t>(index
) >= count
) 
 943     ffi_type 
**elements(typical
->GetFFI()->elements
); 
 945     base 
= reinterpret_cast<uint8_t *>(internal
->value_
); 
 946     for (ssize_t 
local(0); local 
!= index
; ++local
) 
 947         base 
+= elements
[local
]->size
; 
 952 static JSValueRef 
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
 954     Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(object
))); 
 956     if (JSStringIsEqual(property
, length_s
)) 
 957         return internal
->length_ 
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
); 
 959     Type_privateData 
*typical(internal
->type_
); 
 961     if (typical
->type_ 
== NULL
) 
 965     if (JSStringIsEqualToUTF8CString(property
, "$cyi")) 
 967     else if (!CYGetOffset(pool
, context
, property
, offset
)) 
 970     ffi_type 
*ffi(typical
->GetFFI()); 
 972     uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
)); 
 973     base 
+= ffi
->size 
* offset
; 
 975     JSObjectRef 
owner(internal
->GetOwner() ?: object
); 
 976     return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
); 
 979 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef 
*exception
) { CYTry 
{ 
 981     Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(object
))); 
 982     Type_privateData 
*typical(internal
->type_
); 
 984     if (typical
->type_ 
== NULL
) 
 988     if (JSStringIsEqualToUTF8CString(property
, "$cyi")) 
 990     else if (!CYGetOffset(pool
, context
, property
, offset
)) 
 993     ffi_type 
*ffi(typical
->GetFFI()); 
 995     uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
)); 
 996     base 
+= ffi
->size 
* offset
; 
 998     CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
); 
1002 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
1003     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(_this
))); 
1004     Type_privateData 
*typical(internal
->type_
); 
1005     return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
); 
1008 static JSValueRef 
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
1010     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(object
))); 
1011     Type_privateData 
*typical(internal
->type_
); 
1016     if (!Index_(pool
, context
, internal
, property
, index
, base
)) 
1019     JSObjectRef 
owner(internal
->GetOwner() ?: object
); 
1021     return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
); 
1024 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef 
*exception
) { CYTry 
{ 
1026     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(object
))); 
1027     Type_privateData 
*typical(internal
->type_
); 
1032     if (!Index_(pool
, context
, internal
, property
, index
, base
)) 
1035     CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
); 
1039 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) { 
1040     Struct_privateData 
*internal(reinterpret_cast<Struct_privateData 
*>(JSObjectGetPrivate(object
))); 
1041     Type_privateData 
*typical(internal
->type_
); 
1042     sig::Type 
*type(typical
->type_
); 
1047     size_t count(type
->data
.signature
.count
); 
1048     sig::Element 
*elements(type
->data
.signature
.elements
); 
1052     for (size_t index(0); index 
!= count
; ++index
) { 
1054         name 
= elements
[index
].name
; 
1057             sprintf(number
, "%zu", index
); 
1061         JSPropertyNameAccumulatorAddName(names
, CYJSString(name
)); 
1065 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 
{ 
1066     if (setups 
+ count 
!= signature
->count 
- 1) 
1067         throw CYJSError(context
, "incorrect number of arguments to ffi function"); 
1069     size_t size(setups 
+ count
); 
1071     memcpy(values
, setup
, sizeof(void *) * setups
); 
1073     for (size_t index(setups
); index 
!= size
; ++index
) { 
1074         sig::Element 
*element(&signature
->elements
[index 
+ 1]); 
1075         ffi_type 
*ffi(cif
->arg_types
[index
]); 
1077         values
[index
] = new(pool
) uint8_t[ffi
->size
]; 
1078         CYPoolFFI(pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index 
- setups
]); 
1081     uint8_t value
[cif
->rtype
->size
]; 
1083     if (hooks_ 
!= NULL 
&& hooks_
->CallFunction 
!= NULL
) 
1084         (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
); 
1086         ffi_call(cif
, function
, value
, values
); 
1088     return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
); 
1091 static JSValueRef 
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
1093     cy::Functor 
*internal(reinterpret_cast<cy::Functor 
*>(JSObjectGetPrivate(object
))); 
1094     return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, exception
, &internal
->signature_
, &internal
->cif_
, internal
->GetValue()); 
1097 static JSObjectRef 
CYMakeType(JSContextRef context
, const char *type
) { 
1098     Type_privateData 
*internal(new Type_privateData(type
)); 
1099     return JSObjectMake(context
, Type_privateData::Class_
, internal
); 
1102 static JSObjectRef 
CYMakeType(JSContextRef context
, sig::Type 
*type
) { 
1103     Type_privateData 
*internal(new Type_privateData(type
)); 
1104     return JSObjectMake(context
, Type_privateData::Class_
, internal
); 
1107 static void *CYCastSymbol(const char *name
) { 
1108     return dlsym(RTLD_DEFAULT
, name
); 
1111 static JSValueRef 
Runtime_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
1113     CYUTF8String 
name(CYPoolUTF8String(pool
, context
, property
)); 
1115     if (hooks_ 
!= NULL 
&& hooks_
->RuntimeProperty 
!= NULL
) 
1116         if (JSValueRef value 
= (*hooks_
->RuntimeProperty
)(context
, name
)) 
1119     sqlite3_stmt 
*statement
; 
1121     _sqlcall(sqlite3_prepare(Bridge_
, 
1123             "\"bridge\".\"mode\", " 
1124             "\"bridge\".\"value\" " 
1127             " \"bridge\".\"name\" = ?" 
1129     , -1, &statement
, NULL
)); 
1131     _sqlcall(sqlite3_bind_text(statement
, 1, name
.data
, name
.size
, SQLITE_STATIC
)); 
1136     if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
) { 
1140         mode 
= sqlite3_column_int(statement
, 0); 
1141         value 
= sqlite3_column_pooled(pool
, statement
, 1); 
1144     _sqlcall(sqlite3_finalize(statement
)); 
1153             return JSEvaluateScript(CYGetJSContext(context
), CYJSString(value
), NULL
, NULL
, 0, NULL
); 
1156             if (void (*symbol
)() = reinterpret_cast<void (*)()>(CYCastSymbol(name
.data
))) 
1157                 return CYMakeFunctor(context
, symbol
, value
); 
1161             if (void *symbol 
= CYCastSymbol(name
.data
)) { 
1162                 // XXX: this is horrendously inefficient 
1163                 sig::Signature signature
; 
1164                 sig::Parse(pool
, &signature
, value
, &Structor_
); 
1166                 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
); 
1167                 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
); 
1170         // XXX: implement case 3 
1172             return CYMakeType(context
, value
); 
1176 static JSObjectRef 
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1178         throw CYJSError(context
, "incorrect number of arguments to Functor constructor"); 
1182     void *value(CYCastPointer
<void *>(context
, arguments
[0])); 
1183     const char *type(CYPoolCString(pool
, context
, arguments
[1])); 
1185     sig::Signature signature
; 
1186     sig::Parse(pool
, &signature
, type
, &Structor_
); 
1188     return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
); 
1191 static JSObjectRef 
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1193         throw CYJSError(context
, "incorrect number of arguments to Type constructor"); 
1195     const char *type(CYPoolCString(pool
, context
, arguments
[0])); 
1196     return CYMakeType(context
, type
); 
1199 static JSValueRef 
Type_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef 
*exception
) { CYTry 
{ 
1200     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1204     if (JSStringIsEqualToUTF8CString(property
, "$cyi")) { 
1205         type
.primitive 
= sig::pointer_P
; 
1206         type
.data
.data
.size 
= 0; 
1209         size_t index(CYGetIndex(pool
, context
, property
)); 
1210         if (index 
== _not(size_t)) 
1212         type
.primitive 
= sig::array_P
; 
1213         type
.data
.data
.size 
= index
; 
1219     type
.data
.data
.type 
= internal
->type_
; 
1221     return CYMakeType(context
, &type
); 
1224 static JSValueRef 
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1225     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1228         throw CYJSError(context
, "incorrect number of arguments to type cast function"); 
1229     sig::Type 
*type(internal
->type_
); 
1230     ffi_type 
*ffi(internal
->GetFFI()); 
1232     uint8_t value
[ffi
->size
]; 
1234     CYPoolFFI(pool
, context
, type
, ffi
, value
, arguments
[0]); 
1235     return CYFromFFI(context
, type
, ffi
, value
); 
1238 static JSObjectRef 
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1240         throw CYJSError(context
, "incorrect number of arguments to type cast function"); 
1241     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(object
))); 
1243     sig::Type 
*type(internal
->type_
); 
1246     if (type
->primitive 
!= sig::array_P
) 
1247         length 
= _not(size_t); 
1249         length 
= type
->data
.data
.size
; 
1250         type 
= type
->data
.data
.type
; 
1253     void *value(malloc(internal
->GetFFI()->size
)); 
1254     return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
); 
1257 static JSObjectRef 
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1259         throw CYJSError(context
, "incorrect number of arguments to Functor constructor"); 
1261     const char *type(CYPoolCString(pool
, context
, arguments
[1])); 
1262     return CYMakeFunctor(context
, arguments
[0], type
); 
1265 static JSValueRef 
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1266     CYValue 
*internal(reinterpret_cast<CYValue 
*>(JSObjectGetPrivate(_this
))); 
1267     return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
)); 
1270 static JSValueRef 
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
1271     return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
); 
1274 static JSValueRef 
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1275     CYValue 
*internal(reinterpret_cast<CYValue 
*>(JSObjectGetPrivate(_this
))); 
1277     sprintf(string
, "%p", internal
->value_
); 
1278     return CYCastJSValue(context
, string
); 
1281 static JSValueRef 
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1282     Pointer 
*internal(reinterpret_cast<Pointer 
*>(JSObjectGetPrivate(_this
))); 
1283     if (internal
->length_ 
!= _not(size_t)) 
1284         // XXX: maybe dynamically look up Array.toCYON? 
1285         return Array_callAsFunction_toCYON(context
, object
, _this
, count
, arguments
, exception
); 
1288         sprintf(string
, "%p", internal
->value_
); 
1289         return CYCastJSValue(context
, string
); 
1293 static JSValueRef 
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1294     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1296     const char *type(sig::Unparse(pool
, internal
->type_
)); 
1297     return CYCastJSValue(context
, CYJSString(type
)); 
1300 static JSValueRef 
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { CYTry 
{ 
1301     Type_privateData 
*internal(reinterpret_cast<Type_privateData 
*>(JSObjectGetPrivate(_this
))); 
1303     const char *type(sig::Unparse(pool
, internal
->type_
)); 
1304     size_t size(strlen(type
)); 
1305     char *cyon(new(pool
) char[12 + size 
+ 1]); 
1306     memcpy(cyon
, "new Type(\"", 10); 
1307     cyon
[12 + size
] = '\0'; 
1308     cyon
[12 + size 
- 2] = '"'; 
1309     cyon
[12 + size 
- 1] = ')'; 
1310     memcpy(cyon 
+ 10, type
, size
); 
1311     return CYCastJSValue(context
, CYJSString(cyon
)); 
1314 static JSValueRef 
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef 
*exception
) { 
1315     return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
); 
1318 static JSStaticFunction Pointer_staticFunctions
[4] = { 
1319     {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1320     {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1321     {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1325 static JSStaticFunction Struct_staticFunctions
[2] = { 
1326     {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1330 static JSStaticFunction Functor_staticFunctions
[4] = { 
1331     {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1332     {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1333     {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1338     JSStaticFunction 
const * const Functor::StaticFunctions 
= Functor_staticFunctions
; 
1341 static JSStaticFunction Type_staticFunctions
[4] = { 
1342     {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1343     {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1344     {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum 
| kJSPropertyAttributeDontDelete
}, 
1348 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef 
*); 
1350 void CYSetArgs(int argc
, const char *argv
[]) { 
1351     JSContextRef 
context(CYGetJSContext()); 
1352     JSValueRef args
[argc
]; 
1353     for (int i(0); i 
!= argc
; ++i
) 
1354         args
[i
] = CYCastJSValue(context
, argv
[i
]); 
1357     if (JSObjectMakeArray$ 
!= NULL
) { 
1358         JSValueRef 
exception(NULL
); 
1359         array 
= (*JSObjectMakeArray$
)(context
, argc
, args
, &exception
); 
1360         CYThrow(context
, exception
); 
1362         JSValueRef 
value(CYCallAsFunction(context
, Array_
, NULL
, argc
, args
)); 
1363         array 
= CYCastJSObject(context
, value
); 
1366     CYSetProperty(context
, System_
, CYJSString("args"), array
); 
1369 JSObjectRef 
CYGetGlobalObject(JSContextRef context
) { 
1370     return JSContextGetGlobalObject(context
); 
1373 const char *CYExecute(apr_pool_t 
*pool
, const char *code
) { 
1374     JSContextRef 
context(CYGetJSContext()); 
1375     JSValueRef 
exception(NULL
), result
; 
1378     if (hooks_ 
!= NULL 
&& hooks_
->ExecuteStart 
!= NULL
) 
1379         handle 
= (*hooks_
->ExecuteStart
)(context
); 
1386         result 
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
); 
1387     } catch (const char *error
) { 
1391     if (exception 
!= NULL
) { error
: 
1396     if (JSValueIsUndefined(context
, result
)) 
1400         json 
= CYPoolCCYON(pool
, context
, result
, &exception
); 
1401     } catch (const char *error
) { 
1405     if (exception 
!= NULL
) 
1408     CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
); 
1410     if (hooks_ 
!= NULL 
&& hooks_
->ExecuteEnd 
!= NULL
) 
1411         (*hooks_
->ExecuteEnd
)(context
, handle
); 
1415 static apr_pool_t 
*Pool_
; 
1417 static bool initialized_
; 
1419 void CYInitialize() { 
1421         initialized_ 
= true; 
1424     _aprcall(apr_initialize()); 
1425     _aprcall(apr_pool_create(&Pool_
, NULL
)); 
1426     _sqlcall(sqlite3_open("/usr/lib/libcycript.db", &Bridge_
)); 
1428     JSObjectMakeArray$ 
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef 
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray")); 
1430     JSClassDefinition definition
; 
1432     definition 
= kJSClassDefinitionEmpty
; 
1433     definition
.className 
= "Functor"; 
1434     definition
.staticFunctions 
= cy::Functor::StaticFunctions
; 
1435     definition
.callAsFunction 
= &Functor_callAsFunction
; 
1436     definition
.finalize 
= &CYFinalize
; 
1437     Functor_ 
= JSClassCreate(&definition
); 
1439     definition 
= kJSClassDefinitionEmpty
; 
1440     definition
.className 
= "Pointer"; 
1441     definition
.staticFunctions 
= Pointer_staticFunctions
; 
1442     definition
.getProperty 
= &Pointer_getProperty
; 
1443     definition
.setProperty 
= &Pointer_setProperty
; 
1444     definition
.finalize 
= &CYFinalize
; 
1445     Pointer_ 
= JSClassCreate(&definition
); 
1447     definition 
= kJSClassDefinitionEmpty
; 
1448     definition
.className 
= "Struct"; 
1449     definition
.staticFunctions 
= Struct_staticFunctions
; 
1450     definition
.getProperty 
= &Struct_getProperty
; 
1451     definition
.setProperty 
= &Struct_setProperty
; 
1452     definition
.getPropertyNames 
= &Struct_getPropertyNames
; 
1453     definition
.finalize 
= &CYFinalize
; 
1454     Struct_ 
= JSClassCreate(&definition
); 
1456     definition 
= kJSClassDefinitionEmpty
; 
1457     definition
.className 
= "Type"; 
1458     definition
.staticFunctions 
= Type_staticFunctions
; 
1459     definition
.getProperty 
= &Type_getProperty
; 
1460     definition
.callAsFunction 
= &Type_callAsFunction
; 
1461     definition
.callAsConstructor 
= &Type_callAsConstructor
; 
1462     definition
.finalize 
= &CYFinalize
; 
1463     Type_privateData::Class_ 
= JSClassCreate(&definition
); 
1465     definition 
= kJSClassDefinitionEmpty
; 
1466     definition
.className 
= "Runtime"; 
1467     definition
.getProperty 
= &Runtime_getProperty
; 
1468     Runtime_ 
= JSClassCreate(&definition
); 
1470     definition 
= kJSClassDefinitionEmpty
; 
1471     //definition.getProperty = &Global_getProperty; 
1472     Global_ 
= JSClassCreate(&definition
); 
1474     length_s 
= JSStringCreateWithUTF8CString("length"); 
1475     message_s 
= JSStringCreateWithUTF8CString("message"); 
1476     name_s 
= JSStringCreateWithUTF8CString("name"); 
1477     pop_s 
= JSStringCreateWithUTF8CString("pop"); 
1478     prototype_s 
= JSStringCreateWithUTF8CString("prototype"); 
1479     push_s 
= JSStringCreateWithUTF8CString("push"); 
1480     splice_s 
= JSStringCreateWithUTF8CString("splice"); 
1481     toCYON_s 
= JSStringCreateWithUTF8CString("toCYON"); 
1482     toJSON_s 
= JSStringCreateWithUTF8CString("toJSON"); 
1484     Result_ 
= JSStringCreateWithUTF8CString("_"); 
1486     if (hooks_ 
!= NULL 
&& hooks_
->Initialize 
!= NULL
) 
1487         (*hooks_
->Initialize
)(); 
1490 apr_pool_t 
*CYGetGlobalPool() { 
1495 void CYThrow(JSContextRef context
, JSValueRef value
) { 
1497         throw CYJSError(context
, value
); 
1500 const char *CYJSError::PoolCString(apr_pool_t 
*pool
) const { 
1501     return CYPoolCString(pool
, context_
, value_
); 
1504 JSValueRef 
CYJSError::CastJSValue(JSContextRef context
) const { 
1505     // XXX: what if the context is different? 
1509 void CYThrow(const char *format
, ...) { 
1511     va_start (args
, format
); 
1512     throw CYPoolError(format
, args
); 
1513     // XXX: does this matter? :( 
1517 const char *CYPoolError::PoolCString(apr_pool_t 
*pool
) const { 
1518     return apr_pstrdup(pool
, message_
); 
1521 CYPoolError::CYPoolError(const char *format
, ...) { 
1523     va_start (args
, format
); 
1524     message_ 
= apr_pvsprintf(pool_
, format
, args
); 
1528 CYPoolError::CYPoolError(const char *format
, va_list args
) { 
1529     message_ 
= apr_pvsprintf(pool_
, format
, args
); 
1532 JSValueRef 
CYCastJSError(JSContextRef context
, const char *message
) { 
1533     JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)}; 
1535     JSValueRef 
exception(NULL
); 
1536     JSValueRef 
value(JSObjectCallAsConstructor(context
, Error_
, 1, arguments
, &exception
)); 
1537     CYThrow(context
, exception
); 
1542 JSValueRef 
CYPoolError::CastJSValue(JSContextRef context
) const { 
1543     return CYCastJSError(context
, message_
); 
1546 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) { 
1547     _assert(context 
!= NULL
); 
1552     va_start (args
, format
); 
1553     const char *message(apr_pvsprintf(pool
, format
, args
)); 
1556     value_ 
= CYCastJSError(context
, message
); 
1559 JSGlobalContextRef 
CYGetJSContext(JSContextRef context
) { 
1560     // XXX: do something better 
1564 void CYSetupContext(JSGlobalContextRef context
) { 
1565     JSObjectRef 
global(CYGetGlobalObject(context
)); 
1567     Array_ 
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))); 
1568     JSValueProtect(context
, Array_
); 
1569     Array_prototype_ 
= CYCastJSObject(context
, CYGetProperty(context
, Array_
, prototype_s
)); 
1570     JSValueProtect(context
, Array_prototype_
); 
1572     Error_ 
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))); 
1573     JSValueProtect(context
, Error_
); 
1575     Function_ 
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))); 
1576     JSValueProtect(context
, Function_
); 
1577     Function_prototype_ 
= (JSObjectRef
) CYGetProperty(context
, Function_
, prototype_s
); 
1578     JSValueProtect(context
, Function_prototype_
); 
1580     JSObjectRef 
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object")))); 
1581     Object_prototype_ 
= CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)); 
1582     JSValueProtect(context
, Object_prototype_
); 
1584     String_ 
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))); 
1585     JSValueProtect(context
, String_
); 
1587     JSObjectSetPrototype(context
, global
, JSObjectMake(context
, Runtime_
, NULL
)); 
1589     CYSetProperty(context
, Array_prototype_
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
); 
1591     JSObjectRef 
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
)); 
1593     JSObjectSetPrototype(context
, (JSObjectRef
) CYGetProperty(context
, Functor
, prototype_s
), Function_prototype_
); 
1595     CYSetProperty(context
, global
, CYJSString("Functor"), Functor
); 
1596     CYSetProperty(context
, global
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
)); 
1597     CYSetProperty(context
, global
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
)); 
1599     JSObjectRef 
cycript(JSObjectMake(context
, NULL
, NULL
)); 
1600     CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
); 
1601     CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
); 
1603     CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
); 
1605     System_ 
= JSObjectMake(context
, NULL
, NULL
); 
1606     JSValueProtect(context
, System_
); 
1608     CYSetProperty(context
, global
, CYJSString("system"), System_
); 
1609     CYSetProperty(context
, System_
, CYJSString("args"), CYJSNull(context
)); 
1610     //CYSetProperty(context, System_, CYJSString("global"), global); 
1611     CYSetProperty(context
, System_
, CYJSString("print"), &System_print
); 
1613     if (hooks_ 
!= NULL 
&& hooks_
->SetupContext 
!= NULL
) 
1614         (*hooks_
->SetupContext
)(context
); 
1617 JSGlobalContextRef 
CYGetJSContext() { 
1620     if (Context_ 
== NULL
) { 
1621         Context_ 
= JSGlobalContextCreate(Global_
); 
1622         CYSetupContext(Context_
);