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 size_t index(strtoul(value
.data
, &end
, 10));
191 if (value
.data
+ value
.size
== end
)
193 } else if (value
.data
[1] == '\0')
198 size_t CYGetIndex(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
199 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
202 bool CYGetOffset(const char *value
, ssize_t
&index
) {
203 if (value
[0] != '0') {
205 index
= strtol(value
, &end
, 10);
206 if (value
+ strlen(value
) == end
)
208 } else if (value
[1] == '\0') {
217 /* JavaScript *ify {{{ */
218 void CYStringify(std::ostringstream
&str
, const char *data
, size_t size
) {
219 unsigned quot(0), apos(0);
220 for (const char *value(data
), *end(data
+ size
); value
!= end
; ++value
)
223 else if (*value
== '\'')
226 bool single(quot
> apos
);
228 str
<< (single
? '\'' : '"');
230 for (const char *value(data
), *end(data
+ size
); value
!= end
; ++value
)
232 case '\\': str
<< "\\\\"; break;
233 case '\b': str
<< "\\b"; break;
234 case '\f': str
<< "\\f"; break;
235 case '\n': str
<< "\\n"; break;
236 case '\r': str
<< "\\r"; break;
237 case '\t': str
<< "\\t"; break;
238 case '\v': str
<< "\\v"; break;
253 // this test is designed to be "awewsome", generating neither warnings nor incorrect results
254 if (*value
< 0x20 || *value
>= 0x7f)
255 str
<< "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(uint8_t(*value
));
260 str
<< (single
? '\'' : '"');
263 void CYNumerify(std::ostringstream
&str
, double value
) {
265 // XXX: I want this to print 1e3 rather than 1000
266 sprintf(string
, "%.17g", value
);
270 bool CYIsKey(CYUTF8String value
) {
271 const char *data(value
.data
);
272 size_t size(value
.size
);
277 if (DigitRange_
[data
[0]]) {
278 size_t index(CYGetIndex(value
));
279 if (index
== _not(size_t))
282 if (!WordStartRange_
[data
[0]])
284 for (size_t i(1); i
!= size
; ++i
)
285 if (!WordEndRange_
[data
[i
]])
293 static JSGlobalContextRef Context_
;
294 static JSObjectRef System_
;
296 static JSClassRef Functor_
;
297 static JSClassRef Pointer_
;
298 static JSClassRef Runtime_
;
299 static JSClassRef Struct_
;
301 static JSStringRef Result_
;
305 JSObjectRef Function_
;
309 JSStringRef message_
;
311 JSStringRef prototype_
;
315 JSObjectRef Object_prototype_
;
316 JSObjectRef Function_prototype_
;
318 JSObjectRef Array_prototype_
;
319 JSObjectRef Array_pop_
;
320 JSObjectRef Array_push_
;
321 JSObjectRef Array_splice_
;
325 void CYFinalize(JSObjectRef object
) {
326 delete reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
));
329 struct CStringMapLess
:
330 std::binary_function
<const char *, const char *, bool>
332 _finline
bool operator ()(const char *lhs
, const char *rhs
) const {
333 return strcmp(lhs
, rhs
) < 0;
337 void Structor_(apr_pool_t
*pool
, const char *name
, const char *types
, sig::Type
*&type
) {
341 sqlite3_stmt
*statement
;
343 _sqlcall(sqlite3_prepare(Bridge_
,
345 "\"bridge\".\"mode\", "
346 "\"bridge\".\"value\" "
349 " \"bridge\".\"mode\" in (3, 4) and"
350 " \"bridge\".\"name\" = ?"
352 , -1, &statement
, NULL
));
354 _sqlcall(sqlite3_bind_text(statement
, 1, name
, -1, SQLITE_STATIC
));
359 if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
) {
363 mode
= sqlite3_column_int(statement
, 0);
364 value
= sqlite3_column_pooled(pool
, statement
, 1);
367 _sqlcall(sqlite3_finalize(statement
));
376 sig::Parse(pool
, &type
->data
.signature
, value
, &Structor_
);
380 sig::Signature signature
;
381 sig::Parse(pool
, &signature
, value
, &Structor_
);
382 type
= signature
.elements
[0].type
;
387 JSClassRef
Type_privateData::Class_
;
392 Type_privateData
*type_
;
394 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, sig::Type
*type
) :
395 CYOwned(value
, context
, owner
),
396 type_(new(pool_
) Type_privateData(type
))
401 struct Struct_privateData
:
404 Type_privateData
*type_
;
406 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
407 CYOwned(NULL
, context
, owner
)
412 typedef std::map
<const char *, Type_privateData
*, CStringMapLess
> TypeMap
;
413 static TypeMap Types_
;
415 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
416 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
417 apr_pool_t
*pool(internal
->pool_
);
418 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
419 internal
->type_
= typical
;
422 internal
->value_
= data
;
424 size_t size(typical
->GetFFI()->size
);
425 void *copy(apr_palloc(internal
->pool_
, size
));
426 memcpy(copy
, data
, size
);
427 internal
->value_
= copy
;
430 return JSObjectMake(context
, Struct_
, internal
);
433 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
434 return JSValueMakeBoolean(context
, value
);
437 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
438 return JSValueMakeNumber(context
, value
);
441 #define CYCastJSValue_(Type_) \
442 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
443 return JSValueMakeNumber(context, static_cast<double>(value)); \
447 CYCastJSValue_(unsigned int)
448 CYCastJSValue_(long int)
449 CYCastJSValue_(long unsigned int)
450 CYCastJSValue_(long long int)
451 CYCastJSValue_(long long unsigned int)
453 JSValueRef
CYJSUndefined(JSContextRef context
) {
454 return JSValueMakeUndefined(context
);
457 double CYCastDouble(const char *value
, size_t size
) {
459 double number(strtod(value
, &end
));
460 if (end
!= value
+ size
)
465 double CYCastDouble(const char *value
) {
466 return CYCastDouble(value
, strlen(value
));
469 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
470 JSValueRef
exception(NULL
);
471 double number(JSValueToNumber(context
, value
, &exception
));
472 CYThrow(context
, exception
);
476 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
477 return JSValueToBoolean(context
, value
);
480 JSValueRef
CYJSNull(JSContextRef context
) {
481 return JSValueMakeNull(context
);
484 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
485 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
488 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
489 return CYCastJSValue(context
, CYJSString(value
));
492 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
493 JSValueRef
exception(NULL
);
494 JSObjectRef
object(JSValueToObject(context
, value
, &exception
));
495 CYThrow(context
, exception
);
499 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, JSValueRef arguments
[]) {
500 JSValueRef
exception(NULL
);
501 JSValueRef
value(JSObjectCallAsFunction(context
, function
, _this
, count
, arguments
, &exception
));
502 CYThrow(context
, exception
);
506 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
507 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
510 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
515 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
518 return CYJSUndefined(context
);
521 static size_t Nonce_(0);
523 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
525 const char *name(apr_psprintf(pool
, "%s%zu", CYPoolCString(pool
, context
, arguments
[0]), Nonce_
++));
526 return CYCastJSValue(context
, name
);
529 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
530 JSGarbageCollect(context
);
531 return CYJSUndefined(context
);
534 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
535 switch (JSType type
= JSValueGetType(context
, value
)) {
536 case kJSTypeUndefined
:
541 return CYCastBool(context
, value
) ? "true" : "false";
543 case kJSTypeNumber
: {
544 std::ostringstream str
;
545 CYNumerify(str
, CYCastDouble(context
, value
));
546 std::string
value(str
.str());
547 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
550 case kJSTypeString
: {
551 std::ostringstream str
;
552 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
553 CYStringify(str
, string
.data
, string
.size
);
554 std::string
value(str
.str());
555 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
559 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
561 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
565 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
) {
566 JSValueRef
exception(NULL
);
567 const char *cyon(CYPoolCCYON(pool
, context
, value
, &exception
));
568 CYThrow(context
, exception
);
572 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSObjectRef object
) {
573 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_
));
574 if (CYIsCallable(context
, toCYON
)) {
575 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
576 return CYPoolCString(pool
, context
, value
);
579 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_
));
580 if (CYIsCallable(context
, toJSON
)) {
581 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
582 JSValueRef
exception(NULL
);
583 const char *cyon(CYPoolCCYON(pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), &exception
));
584 CYThrow(context
, exception
);
588 std::ostringstream str
;
592 // XXX: this is, sadly, going to leak
593 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
597 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
598 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
599 JSValueRef
value(CYGetProperty(context
, object
, name
));
606 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
610 CYStringify(str
, string
.data
, string
.size
);
612 str
<< ':' << CYPoolCCYON(pool
, context
, value
);
617 JSPropertyNameArrayRelease(names
);
619 std::string
string(str
.str());
620 return apr_pstrmemdup(pool
, string
.c_str(), string
.size());
623 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
625 std::ostringstream str
;
629 JSValueRef
length(CYGetProperty(context
, _this
, length_
));
632 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
633 JSValueRef
value(CYGetProperty(context
, _this
, index
));
640 if (!JSValueIsUndefined(context
, value
))
641 str
<< CYPoolCCYON(pool
, context
, value
);
650 std::string
value(str
.str());
651 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
654 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
655 Pointer
*internal(new Pointer(pointer
, context
, owner
, type
));
656 return JSObjectMake(context
, Pointer_
, internal
);
659 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const char *type
) {
660 cy::Functor
*internal(new cy::Functor(type
, function
));
661 return JSObjectMake(context
, Functor_
, internal
);
664 static bool CYGetOffset(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
665 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
668 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
669 switch (JSValueGetType(context
, value
)) {
672 /*case kJSTypeObject:
673 if (JSValueIsObjectOfClass(context, value, Pointer_)) {
674 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value)));
675 return internal->value_;
678 double number(CYCastDouble(context
, value
));
679 if (std::isnan(number
))
680 throw CYJSError(context
, "cannot convert value to pointer");
681 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
685 void CYPoolFFI(apr_pool_t
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
686 switch (type
->primitive
) {
688 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
691 #define CYPoolFFI_(primitive, native) \
692 case sig::primitive ## _P: \
693 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
696 CYPoolFFI_(uchar
, unsigned char)
697 CYPoolFFI_(char, char)
698 CYPoolFFI_(ushort
, unsigned short)
699 CYPoolFFI_(short, short)
700 CYPoolFFI_(ulong
, unsigned long)
701 CYPoolFFI_(long, long)
702 CYPoolFFI_(uint
, unsigned int)
704 CYPoolFFI_(ulonglong
, unsigned long long)
705 CYPoolFFI_(longlong
, long long)
706 CYPoolFFI_(float, float)
707 CYPoolFFI_(double, double)
710 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
714 *reinterpret_cast<const char **>(data
) = CYPoolCString(pool
, context
, value
);
717 case sig::struct_P
: {
718 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
719 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
720 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
721 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
722 ffi_type
*field(ffi
->elements
[index
]);
725 if (aggregate
== NULL
)
728 rhs
= CYGetProperty(context
, aggregate
, index
);
729 if (JSValueIsUndefined(context
, rhs
)) {
730 if (element
->name
!= NULL
)
731 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
734 if (JSValueIsUndefined(context
, rhs
)) undefined
:
735 throw CYJSError(context
, "unable to extract structure value");
739 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
749 if (hooks_
!= NULL
&& hooks_
->PoolFFI
!= NULL
)
750 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
753 fprintf(stderr
, "CYPoolFFI(%c)\n", type
->primitive
);
758 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
759 switch (type
->primitive
) {
761 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
763 #define CYFromFFI_(primitive, native) \
764 case sig::primitive ## _P: \
765 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
767 CYFromFFI_(uchar, unsigned char)
768 CYFromFFI_(char, char)
769 CYFromFFI_(ushort
, unsigned short)
770 CYFromFFI_(short, short)
771 CYFromFFI_(ulong
, unsigned long)
772 CYFromFFI_(long, long)
773 CYFromFFI_(uint
, unsigned int)
775 CYFromFFI_(ulonglong
, unsigned long long)
776 CYFromFFI_(longlong
, long long)
777 CYFromFFI_(float, float)
778 CYFromFFI_(double, double)
781 if (void *pointer
= *reinterpret_cast<void **>(data
))
782 return CYMakePointer(context
, pointer
, type
->data
.data
.type
, ffi
, owner
);
786 if (char *utf8
= *reinterpret_cast<char **>(data
))
787 return CYCastJSValue(context
, utf8
);
791 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
793 return CYJSUndefined(context
);
796 return CYJSNull(context
);
798 if (hooks_
!= NULL
&& hooks_
->FromFFI
!= NULL
)
799 if (JSValueRef value
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
802 fprintf(stderr
, "CYFromFFI(%c)\n", type
->primitive
);
807 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
808 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
810 JSContextRef
context(internal
->context_
);
812 size_t count(internal
->cif_
.nargs
);
813 JSValueRef values
[count
];
815 for (size_t index(0); index
!= count
; ++index
)
816 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
818 JSValueRef
value(CYCallAsFunction(context
, internal
->function_
, NULL
, count
, values
));
819 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
822 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const char *type
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
823 // XXX: in case of exceptions this will leak
824 // XXX: in point of fact, this may /need/ to leak :(
825 Closure_privateData
*internal(new Closure_privateData(CYGetJSContext(), function
, type
));
827 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
828 NULL
, sizeof(ffi_closure
),
829 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
833 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
834 _assert(status
== FFI_OK
);
836 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
838 internal
->value_
= closure
;
843 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const char *type
) {
844 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, type
, &FunctionClosure_
));
845 return JSObjectMake(context
, Functor_
, internal
);
848 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const char *type
) {
849 JSValueRef
exception(NULL
);
850 bool function(JSValueIsInstanceOfConstructor(context
, value
, Function_
, &exception
));
851 CYThrow(context
, exception
);
854 JSObjectRef
function(CYCastJSObject(context
, value
));
855 return CYMakeFunctor(context
, function
, type
);
857 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
858 return CYMakeFunctor(context
, function
, type
);
862 static bool Index_(apr_pool_t
*pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
863 Type_privateData
*typical(internal
->type_
);
864 sig::Type
*type(typical
->type_
);
868 const char *name(CYPoolCString(pool
, context
, property
));
869 size_t length(strlen(name
));
870 double number(CYCastDouble(name
, length
));
872 size_t count(type
->data
.signature
.count
);
874 if (std::isnan(number
)) {
875 if (property
== NULL
)
878 sig::Element
*elements(type
->data
.signature
.elements
);
880 for (size_t local(0); local
!= count
; ++local
) {
881 sig::Element
*element(&elements
[local
]);
882 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
890 index
= static_cast<ssize_t
>(number
);
891 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
896 ffi_type
**elements(typical
->GetFFI()->elements
);
898 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
899 for (ssize_t
local(0); local
!= index
; ++local
)
900 base
+= elements
[local
]->size
;
905 static JSValueRef
Pointer_getIndex(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef
*exception
) { CYTry
{
906 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
907 Type_privateData
*typical(internal
->type_
);
909 ffi_type
*ffi(typical
->GetFFI());
911 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
912 base
+= ffi
->size
* index
;
914 JSObjectRef
owner(internal
->GetOwner() ?: object
);
915 return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
);
918 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) {
920 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
921 Type_privateData
*typical(internal
->type_
);
923 if (typical
->type_
== NULL
)
927 if (!CYGetOffset(pool
, context
, property
, offset
))
930 return Pointer_getIndex(context
, object
, offset
, exception
);
933 static JSValueRef Pointer_getProperty_$
cyi(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) {
934 return Pointer_getIndex(context
, object
, 0, exception
);
937 static bool Pointer_setIndex(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
938 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
939 Type_privateData
*typical(internal
->type_
);
941 ffi_type
*ffi(typical
->GetFFI());
943 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
944 base
+= ffi
->size
* index
;
946 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
950 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) {
952 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
953 Type_privateData
*typical(internal
->type_
);
955 if (typical
->type_
== NULL
)
959 if (!CYGetOffset(pool
, context
, property
, offset
))
962 return Pointer_setIndex(context
, object
, offset
, value
, exception
);
965 static bool Pointer_setProperty_$
cyi(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) {
966 return Pointer_setIndex(context
, object
, 0, value
, exception
);
969 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
970 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
971 Type_privateData
*typical(internal
->type_
);
972 return CYMakePointer(context
, internal
->value_
, typical
->type_
, typical
->ffi_
, _this
);
975 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
977 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
978 Type_privateData
*typical(internal
->type_
);
983 if (!Index_(pool
, context
, internal
, property
, index
, base
))
986 JSObjectRef
owner(internal
->GetOwner() ?: object
);
988 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
991 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
993 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
994 Type_privateData
*typical(internal
->type_
);
999 if (!Index_(pool
, context
, internal
, property
, index
, base
))
1002 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
1006 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1007 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1008 Type_privateData
*typical(internal
->type_
);
1009 sig::Type
*type(typical
->type_
);
1014 size_t count(type
->data
.signature
.count
);
1015 sig::Element
*elements(type
->data
.signature
.elements
);
1019 for (size_t index(0); index
!= count
; ++index
) {
1021 name
= elements
[index
].name
;
1024 sprintf(number
, "%lu", index
);
1028 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
1032 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
{
1033 if (setups
+ count
!= signature
->count
- 1)
1034 throw CYJSError(context
, "incorrect number of arguments to ffi function");
1036 size_t size(setups
+ count
);
1038 memcpy(values
, setup
, sizeof(void *) * setups
);
1040 for (size_t index(setups
); index
!= size
; ++index
) {
1041 sig::Element
*element(&signature
->elements
[index
+ 1]);
1042 ffi_type
*ffi(cif
->arg_types
[index
]);
1044 values
[index
] = new(pool
) uint8_t[ffi
->size
];
1045 CYPoolFFI(pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
1048 uint8_t value
[cif
->rtype
->size
];
1050 if (hooks_
!= NULL
&& hooks_
->CallFunction
!= NULL
)
1051 (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
);
1053 ffi_call(cif
, function
, value
, values
);
1055 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
1058 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1060 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1061 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, exception
, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
1064 static JSObjectRef
CYMakeType(JSContextRef context
, const char *type
) {
1065 Type_privateData
*internal(new Type_privateData(NULL
, type
));
1066 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1069 static JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
1070 Type_privateData
*internal(new Type_privateData(type
));
1071 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1074 static void *CYCastSymbol(const char *name
) {
1075 return dlsym(RTLD_DEFAULT
, name
);
1078 static JSValueRef
Runtime_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1080 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
1082 if (hooks_
!= NULL
&& hooks_
->RuntimeProperty
!= NULL
)
1083 if (JSValueRef value
= (*hooks_
->RuntimeProperty
)(context
, name
))
1086 sqlite3_stmt
*statement
;
1088 _sqlcall(sqlite3_prepare(Bridge_
,
1090 "\"bridge\".\"mode\", "
1091 "\"bridge\".\"value\" "
1094 " \"bridge\".\"name\" = ?"
1096 , -1, &statement
, NULL
));
1098 _sqlcall(sqlite3_bind_text(statement
, 1, name
.data
, name
.size
, SQLITE_STATIC
));
1103 if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
) {
1107 mode
= sqlite3_column_int(statement
, 0);
1108 value
= sqlite3_column_pooled(pool
, statement
, 1);
1111 _sqlcall(sqlite3_finalize(statement
));
1120 return JSEvaluateScript(CYGetJSContext(), CYJSString(value
), NULL
, NULL
, 0, NULL
);
1122 return CYMakeFunctor(context
, reinterpret_cast<void (*)()>(CYCastSymbol(name
.data
)), value
);
1125 // XXX: this is horrendously inefficient
1126 sig::Signature signature
;
1127 sig::Parse(pool
, &signature
, value
, &Structor_
);
1129 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1130 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, CYCastSymbol(name
.data
));
1133 // XXX: implement case 3
1135 return CYMakeType(context
, value
);
1139 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1141 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1145 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1146 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1148 sig::Signature signature
;
1149 sig::Parse(pool
, &signature
, type
, &Structor_
);
1151 return CYMakePointer(context
, value
, signature
.elements
[0].type
, NULL
, NULL
);
1154 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1156 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1158 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1159 return CYMakeType(context
, type
);
1162 static JSValueRef
Type_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1163 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1167 if (JSStringIsEqualToUTF8CString(property
, "$cyi")) {
1168 type
.primitive
= sig::pointer_P
;
1169 type
.data
.data
.size
= 0;
1172 size_t index(CYGetIndex(pool
, context
, property
));
1173 if (index
== _not(size_t))
1175 type
.primitive
= sig::array_P
;
1176 type
.data
.data
.size
= index
;
1182 type
.data
.data
.type
= internal
->type_
;
1184 return CYMakeType(context
, &type
);
1187 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1188 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1191 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1192 sig::Type
*type(internal
->type_
);
1193 ffi_type
*ffi(internal
->GetFFI());
1195 uint8_t value
[ffi
->size
];
1197 CYPoolFFI(pool
, context
, type
, ffi
, value
, arguments
[0]);
1198 return CYFromFFI(context
, type
, ffi
, value
);
1201 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1203 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1204 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1206 sig::Type
*type(internal
->type_
);
1209 if (type
->primitive
!= sig::array_P
)
1212 size
= type
->data
.data
.size
;
1213 type
= type
->data
.data
.type
;
1216 void *value(malloc(internal
->GetFFI()->size
));
1217 return CYMakePointer(context
, value
, type
, NULL
, NULL
);
1220 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1222 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1224 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1225 return CYMakeFunctor(context
, arguments
[0], type
);
1228 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1229 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1230 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1233 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1234 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1237 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1238 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1240 sprintf(string
, "%p", internal
->value_
);
1242 return CYCastJSValue(context
, string
);
1245 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1246 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1248 const char *type(sig::Unparse(pool
, internal
->type_
));
1249 return CYCastJSValue(context
, CYJSString(type
));
1252 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1253 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1255 const char *type(sig::Unparse(pool
, internal
->type_
));
1256 size_t size(strlen(type
));
1257 char *cyon(new(pool
) char[12 + size
+ 1]);
1258 memcpy(cyon
, "new Type(\"", 10);
1259 cyon
[12 + size
] = '\0';
1260 cyon
[12 + size
- 2] = '"';
1261 cyon
[12 + size
- 1] = ')';
1262 memcpy(cyon
+ 10, type
, size
);
1263 return CYCastJSValue(context
, CYJSString(cyon
));
1266 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1267 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1270 static JSStaticValue Pointer_staticValues
[2] = {
1271 {"$cyi", &Pointer_getProperty_$cyi
, &Pointer_setProperty_$cyi
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1272 {NULL
, NULL
, NULL
, 0}
1275 static JSStaticFunction Pointer_staticFunctions
[4] = {
1276 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1277 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1278 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1282 static JSStaticFunction Struct_staticFunctions
[2] = {
1283 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1287 static JSStaticFunction Functor_staticFunctions
[4] = {
1288 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1289 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1290 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1295 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1298 static JSStaticFunction Type_staticFunctions
[4] = {
1299 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1300 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1301 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1305 static JSObjectRef (*$JSObjectMakeArray
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1307 void CYSetArgs(int argc
, const char *argv
[]) {
1308 JSContextRef
context(CYGetJSContext());
1309 JSValueRef args
[argc
];
1310 for (int i(0); i
!= argc
; ++i
)
1311 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1314 if ($JSObjectMakeArray
!= NULL
) {
1315 JSValueRef
exception(NULL
);
1316 array
= (*$JSObjectMakeArray
)(context
, argc
, args
, &exception
);
1317 CYThrow(context
, exception
);
1319 JSValueRef
value(CYCallAsFunction(context
, Array_
, NULL
, argc
, args
));
1320 array
= CYCastJSObject(context
, value
);
1323 CYSetProperty(context
, System_
, CYJSString("args"), array
);
1326 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1327 return JSContextGetGlobalObject(context
);
1330 const char *CYExecute(apr_pool_t
*pool
, const char *code
) {
1331 JSContextRef
context(CYGetJSContext());
1332 JSValueRef
exception(NULL
), result
;
1335 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1336 handle
= (*hooks_
->ExecuteStart
)(context
);
1343 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1344 } catch (const char *error
) {
1348 if (exception
!= NULL
) { error
:
1353 if (JSValueIsUndefined(context
, result
))
1357 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1358 } catch (const char *error
) {
1362 if (exception
!= NULL
)
1365 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1367 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1368 (*hooks_
->ExecuteEnd
)(context
, handle
);
1372 static apr_pool_t
*Pool_
;
1374 static bool initialized_
;
1376 void CYInitialize() {
1378 initialized_
= true;
1381 _aprcall(apr_initialize());
1382 _aprcall(apr_pool_create(&Pool_
, NULL
));
1383 _sqlcall(sqlite3_open("/usr/lib/libcycript.db", &Bridge_
));
1385 $JSObjectMakeArray
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1388 apr_pool_t
*CYGetGlobalPool() {
1393 void CYThrow(JSContextRef context
, JSValueRef value
) {
1395 throw CYJSError(context
, value
);
1398 const char *CYJSError::PoolCString(apr_pool_t
*pool
) const {
1399 return CYPoolCString(pool
, context_
, value_
);
1402 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1403 // XXX: what if the context is different?
1407 void CYThrow(const char *format
, ...) {
1409 va_start (args
, format
);
1410 throw CYPoolError(format
, args
);
1411 // XXX: does this matter? :(
1415 const char *CYPoolError::PoolCString(apr_pool_t
*pool
) const {
1416 return apr_pstrdup(pool
, message_
);
1419 CYPoolError::CYPoolError(const char *format
, ...) {
1421 va_start (args
, format
);
1422 message_
= apr_pvsprintf(pool_
, format
, args
);
1426 CYPoolError::CYPoolError(const char *format
, va_list args
) {
1427 message_
= apr_pvsprintf(pool_
, format
, args
);
1430 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1431 return CYCastJSValue(context
, message_
);
1434 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1435 if (context
== NULL
)
1436 context
= CYGetJSContext();
1441 va_start (args
, format
);
1442 const char *message(apr_pvsprintf(pool
, format
, args
));
1445 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(message
))};
1447 JSValueRef
exception(NULL
);
1448 value_
= JSObjectCallAsConstructor(context
, Error_
, 1, arguments
, &exception
);
1449 CYThrow(context
, exception
);
1452 JSGlobalContextRef
CYGetJSContext() {
1455 if (Context_
== NULL
) {
1456 JSClassDefinition definition
;
1458 definition
= kJSClassDefinitionEmpty
;
1459 definition
.className
= "Functor";
1460 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1461 definition
.callAsFunction
= &Functor_callAsFunction
;
1462 definition
.finalize
= &CYFinalize
;
1463 Functor_
= JSClassCreate(&definition
);
1465 definition
= kJSClassDefinitionEmpty
;
1466 definition
.className
= "Pointer";
1467 definition
.staticValues
= Pointer_staticValues
;
1468 definition
.staticFunctions
= Pointer_staticFunctions
;
1469 definition
.getProperty
= &Pointer_getProperty
;
1470 definition
.setProperty
= &Pointer_setProperty
;
1471 definition
.finalize
= &CYFinalize
;
1472 Pointer_
= JSClassCreate(&definition
);
1474 definition
= kJSClassDefinitionEmpty
;
1475 definition
.className
= "Struct";
1476 definition
.staticFunctions
= Struct_staticFunctions
;
1477 definition
.getProperty
= &Struct_getProperty
;
1478 definition
.setProperty
= &Struct_setProperty
;
1479 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1480 definition
.finalize
= &CYFinalize
;
1481 Struct_
= JSClassCreate(&definition
);
1483 definition
= kJSClassDefinitionEmpty
;
1484 definition
.className
= "Type";
1485 definition
.staticFunctions
= Type_staticFunctions
;
1486 definition
.getProperty
= &Type_getProperty
;
1487 definition
.callAsFunction
= &Type_callAsFunction
;
1488 definition
.callAsConstructor
= &Type_callAsConstructor
;
1489 definition
.finalize
= &CYFinalize
;
1490 Type_privateData::Class_
= JSClassCreate(&definition
);
1492 definition
= kJSClassDefinitionEmpty
;
1493 definition
.className
= "Runtime";
1494 definition
.getProperty
= &Runtime_getProperty
;
1495 Runtime_
= JSClassCreate(&definition
);
1497 definition
= kJSClassDefinitionEmpty
;
1498 //definition.getProperty = &Global_getProperty;
1499 JSClassRef
Global(JSClassCreate(&definition
));
1501 JSGlobalContextRef
context(JSGlobalContextCreate(Global
));
1503 JSObjectRef
global(CYGetGlobalObject(context
));
1505 JSObjectSetPrototype(context
, global
, JSObjectMake(context
, Runtime_
, NULL
));
1507 Array_
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array")));
1508 JSValueProtect(context
, Array_
);
1510 Error_
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error")));
1511 JSValueProtect(context
, Error_
);
1513 Function_
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function")));
1514 JSValueProtect(context
, Function_
);
1516 String_
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String")));
1517 JSValueProtect(context
, String_
);
1519 length_
= JSStringCreateWithUTF8CString("length");
1520 message_
= JSStringCreateWithUTF8CString("message");
1521 name_
= JSStringCreateWithUTF8CString("name");
1522 prototype_
= JSStringCreateWithUTF8CString("prototype");
1523 toCYON_
= JSStringCreateWithUTF8CString("toCYON");
1524 toJSON_
= JSStringCreateWithUTF8CString("toJSON");
1526 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1527 Object_prototype_
= CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_
));
1528 JSValueProtect(context
, Object_prototype_
);
1530 Array_prototype_
= CYCastJSObject(context
, CYGetProperty(context
, Array_
, prototype_
));
1531 Array_pop_
= CYCastJSObject(context
, CYGetProperty(context
, Array_prototype_
, CYJSString("pop")));
1532 Array_push_
= CYCastJSObject(context
, CYGetProperty(context
, Array_prototype_
, CYJSString("push")));
1533 Array_splice_
= CYCastJSObject(context
, CYGetProperty(context
, Array_prototype_
, CYJSString("splice")));
1535 CYSetProperty(context
, Array_prototype_
, toCYON_
, JSObjectMakeFunctionWithCallback(context
, toCYON_
, &Array_callAsFunction_toCYON
), kJSPropertyAttributeDontEnum
);
1537 JSValueProtect(context
, Array_prototype_
);
1538 JSValueProtect(context
, Array_pop_
);
1539 JSValueProtect(context
, Array_push_
);
1540 JSValueProtect(context
, Array_splice_
);
1542 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1544 Function_prototype_
= (JSObjectRef
) CYGetProperty(context
, Function_
, prototype_
);
1545 JSValueProtect(context
, Function_prototype_
);
1547 JSObjectSetPrototype(context
, (JSObjectRef
) CYGetProperty(context
, Functor
, prototype_
), Function_prototype_
);
1549 CYSetProperty(context
, global
, CYJSString("Functor"), Functor
);
1550 CYSetProperty(context
, global
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1551 CYSetProperty(context
, global
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1553 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1554 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1555 CYSetProperty(context
, cycript
, CYJSString("gc"), JSObjectMakeFunctionWithCallback(context
, CYJSString("gc"), &Cycript_gc_callAsFunction
));
1557 CYSetProperty(context
, global
, CYJSString("$cyq"), JSObjectMakeFunctionWithCallback(context
, CYJSString("$cyq"), &$cyq
));
1559 System_
= JSObjectMake(context
, NULL
, NULL
);
1560 JSValueProtect(context
, System_
);
1562 CYSetProperty(context
, global
, CYJSString("system"), System_
);
1563 CYSetProperty(context
, System_
, CYJSString("args"), CYJSNull(context
));
1564 //CYSetProperty(context, System_, CYJSString("global"), global);
1566 CYSetProperty(context
, System_
, CYJSString("print"), JSObjectMakeFunctionWithCallback(context
, CYJSString("print"), &System_print
));
1568 Result_
= JSStringCreateWithUTF8CString("_");
1570 if (hooks_
!= NULL
&& hooks_
->SetupContext
!= NULL
)
1571 (*hooks_
->SetupContext
)(context
);