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>
64 #include <sys/types.h>
65 #include <sys/socket.h>
66 #include <netinet/in.h>
70 #include "Cycript.tab.hh"
73 #include "JavaScript.hpp"
78 catch (NSException *error) { \
79 CYThrow(context, error, exception); \
86 char *sqlite3_column_pooled(apr_pool_t
*pool
, sqlite3_stmt
*stmt
, int n
) {
87 if (const unsigned char *value
= sqlite3_column_text(stmt
, n
))
88 return apr_pstrdup(pool
, (const char *) value
);
92 struct CYHooks
*hooks_
;
94 /* JavaScript Properties {{{ */
95 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) {
96 JSValueRef
exception(NULL
);
97 JSValueRef
value(JSObjectGetPropertyAtIndex(context
, object
, index
, &exception
));
98 CYThrow(context
, exception
);
102 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
103 JSValueRef
exception(NULL
);
104 JSValueRef
value(JSObjectGetProperty(context
, object
, name
, &exception
));
105 CYThrow(context
, exception
);
109 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) {
110 JSValueRef
exception(NULL
);
111 JSObjectSetPropertyAtIndex(context
, object
, index
, value
, &exception
);
112 CYThrow(context
, exception
);
115 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) {
116 JSValueRef
exception(NULL
);
117 JSObjectSetProperty(context
, object
, name
, value
, attributes
, &exception
);
118 CYThrow(context
, exception
);
121 /* JavaScript Strings {{{ */
122 JSStringRef
CYCopyJSString(const char *value
) {
123 return value
== NULL
? NULL
: JSStringCreateWithUTF8CString(value
);
126 JSStringRef
CYCopyJSString(JSStringRef value
) {
127 return value
== NULL
? NULL
: JSStringRetain(value
);
130 JSStringRef
CYCopyJSString(CYUTF8String value
) {
131 // XXX: this is very wrong
132 return CYCopyJSString(value
.data
);
135 JSStringRef
CYCopyJSString(JSContextRef context
, JSValueRef value
) {
136 if (JSValueIsNull(context
, value
))
138 JSValueRef
exception(NULL
);
139 JSStringRef
string(JSValueToStringCopy(context
, value
, &exception
));
140 CYThrow(context
, exception
);
144 static CYUTF16String
CYCastUTF16String(JSStringRef value
) {
145 return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
));
148 template <typename Type_
>
149 _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
) {
150 return iconv(cd
, const_cast<Type_
>(inbuf
), inbytesleft
, outbuf
, outbytesleft
);
153 CYUTF8String
CYPoolUTF8String(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
154 _assert(pool
!= NULL
);
156 CYUTF16String
utf16(CYCastUTF16String(value
));
157 const char *in(reinterpret_cast<const char *>(utf16
.data
));
160 iconv_t
conversion(_syscall(iconv_open("UTF-8", "UCS-2")));
162 iconv_t
conversion(_syscall(iconv_open("UTF-8", "UCS-2-INTERNAL")));
165 size_t size(JSStringGetMaximumUTF8CStringSize(value
));
166 char *out(new(pool
) char[size
]);
167 CYUTF8String
utf8(out
, size
);
169 size
= utf16
.size
* 2;
170 _syscall(iconv_(&iconv
, conversion
, const_cast<char **>(&in
), &size
, &out
, &utf8
.size
));
173 utf8
.size
= out
- utf8
.data
;
175 _syscall(iconv_close(conversion
));
180 const char *CYPoolCString(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
181 CYUTF8String
utf8(CYPoolUTF8String(pool
, context
, value
));
182 _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
);
186 const char *CYPoolCString(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
) {
187 return JSValueIsNull(context
, value
) ? NULL
: CYPoolCString(pool
, context
, CYJSString(context
, value
));
191 /* Index Offsets {{{ */
192 size_t CYGetIndex(const CYUTF8String
&value
) {
193 if (value
.data
[0] != '0') {
195 size_t index(strtoul(value
.data
, &end
, 10));
196 if (value
.data
+ value
.size
== end
)
198 } else if (value
.data
[1] == '\0')
203 size_t CYGetIndex(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
204 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
207 bool CYGetOffset(const char *value
, ssize_t
&index
) {
208 if (value
[0] != '0') {
210 index
= strtol(value
, &end
, 10);
211 if (value
+ strlen(value
) == end
)
213 } else if (value
[1] == '\0') {
222 /* JavaScript *ify {{{ */
223 void CYStringify(std::ostringstream
&str
, const char *data
, size_t size
) {
224 unsigned quot(0), apos(0);
225 for (const char *value(data
), *end(data
+ size
); value
!= end
; ++value
)
228 else if (*value
== '\'')
231 bool single(quot
> apos
);
233 str
<< (single
? '\'' : '"');
235 for (const char *value(data
), *end(data
+ size
); value
!= end
; ++value
)
237 case '\\': str
<< "\\\\"; break;
238 case '\b': str
<< "\\b"; break;
239 case '\f': str
<< "\\f"; break;
240 case '\n': str
<< "\\n"; break;
241 case '\r': str
<< "\\r"; break;
242 case '\t': str
<< "\\t"; break;
243 case '\v': str
<< "\\v"; break;
258 // this test is designed to be "awewsome", generating neither warnings nor incorrect results
259 if (*value
< 0x20 || *value
>= 0x7f)
260 str
<< "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(uint8_t(*value
));
265 str
<< (single
? '\'' : '"');
268 void CYNumerify(std::ostringstream
&str
, double value
) {
270 // XXX: I want this to print 1e3 rather than 1000
271 sprintf(string
, "%.17g", value
);
275 bool CYIsKey(CYUTF8String value
) {
276 const char *data(value
.data
);
277 size_t size(value
.size
);
282 if (DigitRange_
[data
[0]]) {
283 size_t index(CYGetIndex(value
));
284 if (index
== _not(size_t))
287 if (!WordStartRange_
[data
[0]])
289 for (size_t i(1); i
!= size
; ++i
)
290 if (!WordEndRange_
[data
[i
]])
298 static JSGlobalContextRef Context_
;
299 static JSObjectRef System_
;
301 static JSClassRef Functor_
;
302 static JSClassRef Pointer_
;
303 static JSClassRef Runtime_
;
304 static JSClassRef Struct_
;
306 static JSStringRef Result_
;
310 JSObjectRef Function_
;
314 JSStringRef message_
;
316 JSStringRef prototype_
;
320 JSObjectRef Object_prototype_
;
321 JSObjectRef Function_prototype_
;
323 JSObjectRef Array_prototype_
;
324 JSObjectRef Array_pop_
;
325 JSObjectRef Array_push_
;
326 JSObjectRef Array_splice_
;
330 void CYFinalize(JSObjectRef object
) {
331 delete reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
));
334 struct CStringMapLess
:
335 std::binary_function
<const char *, const char *, bool>
337 _finline
bool operator ()(const char *lhs
, const char *rhs
) const {
338 return strcmp(lhs
, rhs
) < 0;
342 void Structor_(apr_pool_t
*pool
, const char *name
, const char *types
, sig::Type
*&type
) {
346 sqlite3_stmt
*statement
;
348 _sqlcall(sqlite3_prepare(Bridge_
,
350 "\"bridge\".\"mode\", "
351 "\"bridge\".\"value\" "
354 " \"bridge\".\"mode\" in (3, 4) and"
355 " \"bridge\".\"name\" = ?"
357 , -1, &statement
, NULL
));
359 _sqlcall(sqlite3_bind_text(statement
, 1, name
, -1, SQLITE_STATIC
));
364 if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
) {
368 mode
= sqlite3_column_int(statement
, 0);
369 value
= sqlite3_column_pooled(pool
, statement
, 1);
372 _sqlcall(sqlite3_finalize(statement
));
381 sig::Parse(pool
, &type
->data
.signature
, value
, &Structor_
);
385 sig::Signature signature
;
386 sig::Parse(pool
, &signature
, value
, &Structor_
);
387 type
= signature
.elements
[0].type
;
392 JSClassRef
Type_privateData::Class_
;
397 Type_privateData
*type_
;
399 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, sig::Type
*type
) :
400 CYOwned(value
, context
, owner
),
401 type_(new(pool_
) Type_privateData(type
))
406 struct Struct_privateData
:
409 Type_privateData
*type_
;
411 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
412 CYOwned(NULL
, context
, owner
)
417 typedef std::map
<const char *, Type_privateData
*, CStringMapLess
> TypeMap
;
418 static TypeMap Types_
;
420 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
421 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
422 apr_pool_t
*pool(internal
->pool_
);
423 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
424 internal
->type_
= typical
;
427 internal
->value_
= data
;
429 size_t size(typical
->GetFFI()->size
);
430 void *copy(apr_palloc(internal
->pool_
, size
));
431 memcpy(copy
, data
, size
);
432 internal
->value_
= copy
;
435 return JSObjectMake(context
, Struct_
, internal
);
438 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
439 return JSValueMakeBoolean(context
, value
);
442 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
443 return JSValueMakeNumber(context
, value
);
446 #define CYCastJSValue_(Type_) \
447 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
448 return JSValueMakeNumber(context, static_cast<double>(value)); \
452 CYCastJSValue_(unsigned int)
453 CYCastJSValue_(long int)
454 CYCastJSValue_(long unsigned int)
455 CYCastJSValue_(long long int)
456 CYCastJSValue_(long long unsigned int)
458 JSValueRef
CYJSUndefined(JSContextRef context
) {
459 return JSValueMakeUndefined(context
);
462 double CYCastDouble(const char *value
, size_t size
) {
464 double number(strtod(value
, &end
));
465 if (end
!= value
+ size
)
470 double CYCastDouble(const char *value
) {
471 return CYCastDouble(value
, strlen(value
));
474 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
475 JSValueRef
exception(NULL
);
476 double number(JSValueToNumber(context
, value
, &exception
));
477 CYThrow(context
, exception
);
481 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
482 return JSValueToBoolean(context
, value
);
485 JSValueRef
CYJSNull(JSContextRef context
) {
486 return JSValueMakeNull(context
);
489 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
490 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
493 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
494 return CYCastJSValue(context
, CYJSString(value
));
497 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
498 JSValueRef
exception(NULL
);
499 JSObjectRef
object(JSValueToObject(context
, value
, &exception
));
500 CYThrow(context
, exception
);
504 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, JSValueRef arguments
[]) {
505 JSValueRef
exception(NULL
);
506 JSValueRef
value(JSObjectCallAsFunction(context
, function
, _this
, count
, arguments
, &exception
));
507 CYThrow(context
, exception
);
511 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
512 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
515 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
520 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
523 return CYJSUndefined(context
);
526 static size_t Nonce_(0);
528 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
530 const char *name(apr_psprintf(pool
, "%s%zu", CYPoolCString(pool
, context
, arguments
[0]), Nonce_
++));
531 return CYCastJSValue(context
, name
);
534 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
535 JSGarbageCollect(context
);
536 return CYJSUndefined(context
);
539 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
540 switch (JSType type
= JSValueGetType(context
, value
)) {
541 case kJSTypeUndefined
:
546 return CYCastBool(context
, value
) ? "true" : "false";
548 case kJSTypeNumber
: {
549 std::ostringstream str
;
550 CYNumerify(str
, CYCastDouble(context
, value
));
551 std::string
value(str
.str());
552 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
555 case kJSTypeString
: {
556 std::ostringstream str
;
557 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
558 CYStringify(str
, string
.data
, string
.size
);
559 std::string
value(str
.str());
560 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
564 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
566 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
570 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
) {
571 JSValueRef
exception(NULL
);
572 const char *cyon(CYPoolCCYON(pool
, context
, value
, &exception
));
573 CYThrow(context
, exception
);
577 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSObjectRef object
) {
578 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_
));
579 if (CYIsCallable(context
, toCYON
)) {
580 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
581 return CYPoolCString(pool
, context
, value
);
584 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_
));
585 if (CYIsCallable(context
, toJSON
)) {
586 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
587 JSValueRef
exception(NULL
);
588 const char *cyon(CYPoolCCYON(pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), &exception
));
589 CYThrow(context
, exception
);
593 std::ostringstream str
;
597 // XXX: this is, sadly, going to leak
598 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
602 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
603 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
604 JSValueRef
value(CYGetProperty(context
, object
, name
));
611 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
615 CYStringify(str
, string
.data
, string
.size
);
617 str
<< ':' << CYPoolCCYON(pool
, context
, value
);
622 JSPropertyNameArrayRelease(names
);
624 std::string
string(str
.str());
625 return apr_pstrmemdup(pool
, string
.c_str(), string
.size());
628 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
630 std::ostringstream str
;
634 JSValueRef
length(CYGetProperty(context
, _this
, length_
));
637 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
638 JSValueRef
value(CYGetProperty(context
, _this
, index
));
645 if (!JSValueIsUndefined(context
, value
))
646 str
<< CYPoolCCYON(pool
, context
, value
);
655 std::string
value(str
.str());
656 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
659 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
660 Pointer
*internal(new Pointer(pointer
, context
, owner
, type
));
661 return JSObjectMake(context
, Pointer_
, internal
);
664 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const char *type
) {
665 cy::Functor
*internal(new cy::Functor(type
, function
));
666 return JSObjectMake(context
, Functor_
, internal
);
669 static bool CYGetOffset(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
670 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
673 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
674 switch (JSValueGetType(context
, value
)) {
677 /*case kJSTypeObject:
678 if (JSValueIsObjectOfClass(context, value, Pointer_)) {
679 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value)));
680 return internal->value_;
683 double number(CYCastDouble(context
, value
));
684 if (std::isnan(number
))
685 throw CYJSError(context
, "cannot convert value to pointer");
686 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
690 void CYPoolFFI(apr_pool_t
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
691 switch (type
->primitive
) {
693 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
696 #define CYPoolFFI_(primitive, native) \
697 case sig::primitive ## _P: \
698 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
701 CYPoolFFI_(uchar
, unsigned char)
702 CYPoolFFI_(char, char)
703 CYPoolFFI_(ushort
, unsigned short)
704 CYPoolFFI_(short, short)
705 CYPoolFFI_(ulong
, unsigned long)
706 CYPoolFFI_(long, long)
707 CYPoolFFI_(uint
, unsigned int)
709 CYPoolFFI_(ulonglong
, unsigned long long)
710 CYPoolFFI_(longlong
, long long)
711 CYPoolFFI_(float, float)
712 CYPoolFFI_(double, double)
715 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
719 *reinterpret_cast<const char **>(data
) = CYPoolCString(pool
, context
, value
);
722 case sig::struct_P
: {
723 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
724 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
725 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
726 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
727 ffi_type
*field(ffi
->elements
[index
]);
730 if (aggregate
== NULL
)
733 rhs
= CYGetProperty(context
, aggregate
, index
);
734 if (JSValueIsUndefined(context
, rhs
)) {
735 if (element
->name
!= NULL
)
736 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
739 if (JSValueIsUndefined(context
, rhs
)) undefined
:
740 throw CYJSError(context
, "unable to extract structure value");
744 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
754 if (hooks_
!= NULL
&& hooks_
->PoolFFI
!= NULL
)
755 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
758 fprintf(stderr
, "CYPoolFFI(%c)\n", type
->primitive
);
763 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
764 switch (type
->primitive
) {
766 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
768 #define CYFromFFI_(primitive, native) \
769 case sig::primitive ## _P: \
770 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
772 CYFromFFI_(uchar, unsigned char)
773 CYFromFFI_(char, char)
774 CYFromFFI_(ushort
, unsigned short)
775 CYFromFFI_(short, short)
776 CYFromFFI_(ulong
, unsigned long)
777 CYFromFFI_(long, long)
778 CYFromFFI_(uint
, unsigned int)
780 CYFromFFI_(ulonglong
, unsigned long long)
781 CYFromFFI_(longlong
, long long)
782 CYFromFFI_(float, float)
783 CYFromFFI_(double, double)
786 if (void *pointer
= *reinterpret_cast<void **>(data
))
787 return CYMakePointer(context
, pointer
, type
->data
.data
.type
, ffi
, owner
);
791 if (char *utf8
= *reinterpret_cast<char **>(data
))
792 return CYCastJSValue(context
, utf8
);
796 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
798 return CYJSUndefined(context
);
801 return CYJSNull(context
);
803 if (hooks_
!= NULL
&& hooks_
->FromFFI
!= NULL
)
804 if (JSValueRef value
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
807 fprintf(stderr
, "CYFromFFI(%c)\n", type
->primitive
);
812 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
813 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
815 JSContextRef
context(internal
->context_
);
817 size_t count(internal
->cif_
.nargs
);
818 JSValueRef values
[count
];
820 for (size_t index(0); index
!= count
; ++index
)
821 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
823 JSValueRef
value(CYCallAsFunction(context
, internal
->function_
, NULL
, count
, values
));
824 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
827 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const char *type
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
828 // XXX: in case of exceptions this will leak
829 // XXX: in point of fact, this may /need/ to leak :(
830 Closure_privateData
*internal(new Closure_privateData(CYGetJSContext(), function
, type
));
832 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
833 NULL
, sizeof(ffi_closure
),
834 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
838 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
839 _assert(status
== FFI_OK
);
841 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
843 internal
->value_
= closure
;
848 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const char *type
) {
849 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, type
, &FunctionClosure_
));
850 return JSObjectMake(context
, Functor_
, internal
);
853 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const char *type
) {
854 JSValueRef
exception(NULL
);
855 bool function(JSValueIsInstanceOfConstructor(context
, value
, Function_
, &exception
));
856 CYThrow(context
, exception
);
859 JSObjectRef
function(CYCastJSObject(context
, value
));
860 return CYMakeFunctor(context
, function
, type
);
862 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
863 return CYMakeFunctor(context
, function
, type
);
867 static bool Index_(apr_pool_t
*pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
868 Type_privateData
*typical(internal
->type_
);
869 sig::Type
*type(typical
->type_
);
873 const char *name(CYPoolCString(pool
, context
, property
));
874 size_t length(strlen(name
));
875 double number(CYCastDouble(name
, length
));
877 size_t count(type
->data
.signature
.count
);
879 if (std::isnan(number
)) {
880 if (property
== NULL
)
883 sig::Element
*elements(type
->data
.signature
.elements
);
885 for (size_t local(0); local
!= count
; ++local
) {
886 sig::Element
*element(&elements
[local
]);
887 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
895 index
= static_cast<ssize_t
>(number
);
896 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
901 ffi_type
**elements(typical
->GetFFI()->elements
);
903 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
904 for (ssize_t
local(0); local
!= index
; ++local
)
905 base
+= elements
[local
]->size
;
910 static JSValueRef
Pointer_getIndex(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef
*exception
) { CYTry
{
911 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
912 Type_privateData
*typical(internal
->type_
);
914 ffi_type
*ffi(typical
->GetFFI());
916 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
917 base
+= ffi
->size
* index
;
919 JSObjectRef
owner(internal
->GetOwner() ?: object
);
920 return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
);
923 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) {
925 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
926 Type_privateData
*typical(internal
->type_
);
928 if (typical
->type_
== NULL
)
932 if (!CYGetOffset(pool
, context
, property
, offset
))
935 return Pointer_getIndex(context
, object
, offset
, exception
);
938 static JSValueRef Pointer_getProperty_$
cyi(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) {
939 return Pointer_getIndex(context
, object
, 0, exception
);
942 static bool Pointer_setIndex(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
943 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
944 Type_privateData
*typical(internal
->type_
);
946 ffi_type
*ffi(typical
->GetFFI());
948 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
949 base
+= ffi
->size
* index
;
951 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
955 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) {
957 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
958 Type_privateData
*typical(internal
->type_
);
960 if (typical
->type_
== NULL
)
964 if (!CYGetOffset(pool
, context
, property
, offset
))
967 return Pointer_setIndex(context
, object
, offset
, value
, exception
);
970 static bool Pointer_setProperty_$
cyi(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) {
971 return Pointer_setIndex(context
, object
, 0, value
, exception
);
974 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
975 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
976 Type_privateData
*typical(internal
->type_
);
977 return CYMakePointer(context
, internal
->value_
, typical
->type_
, typical
->ffi_
, _this
);
980 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
982 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
983 Type_privateData
*typical(internal
->type_
);
988 if (!Index_(pool
, context
, internal
, property
, index
, base
))
991 JSObjectRef
owner(internal
->GetOwner() ?: object
);
993 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
996 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
998 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
999 Type_privateData
*typical(internal
->type_
);
1004 if (!Index_(pool
, context
, internal
, property
, index
, base
))
1007 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
1011 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1012 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1013 Type_privateData
*typical(internal
->type_
);
1014 sig::Type
*type(typical
->type_
);
1019 size_t count(type
->data
.signature
.count
);
1020 sig::Element
*elements(type
->data
.signature
.elements
);
1024 for (size_t index(0); index
!= count
; ++index
) {
1026 name
= elements
[index
].name
;
1029 sprintf(number
, "%lu", index
);
1033 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
1037 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
{
1038 if (setups
+ count
!= signature
->count
- 1)
1039 throw CYJSError(context
, "incorrect number of arguments to ffi function");
1041 size_t size(setups
+ count
);
1043 memcpy(values
, setup
, sizeof(void *) * setups
);
1045 for (size_t index(setups
); index
!= size
; ++index
) {
1046 sig::Element
*element(&signature
->elements
[index
+ 1]);
1047 ffi_type
*ffi(cif
->arg_types
[index
]);
1049 values
[index
] = new(pool
) uint8_t[ffi
->size
];
1050 CYPoolFFI(pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
1053 uint8_t value
[cif
->rtype
->size
];
1055 if (hooks_
!= NULL
&& hooks_
->CallFunction
!= NULL
)
1056 (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
);
1058 ffi_call(cif
, function
, value
, values
);
1060 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
1063 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1065 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1066 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, exception
, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
1069 static JSObjectRef
CYMakeType(JSContextRef context
, const char *type
) {
1070 Type_privateData
*internal(new Type_privateData(NULL
, type
));
1071 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1074 static JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
1075 Type_privateData
*internal(new Type_privateData(type
));
1076 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1079 static void *CYCastSymbol(const char *name
) {
1080 return dlsym(RTLD_DEFAULT
, name
);
1083 static JSValueRef
Runtime_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1085 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
1087 if (hooks_
!= NULL
&& hooks_
->RuntimeProperty
!= NULL
)
1088 if (JSValueRef value
= (*hooks_
->RuntimeProperty
)(context
, name
))
1091 sqlite3_stmt
*statement
;
1093 _sqlcall(sqlite3_prepare(Bridge_
,
1095 "\"bridge\".\"mode\", "
1096 "\"bridge\".\"value\" "
1099 " \"bridge\".\"name\" = ?"
1101 , -1, &statement
, NULL
));
1103 _sqlcall(sqlite3_bind_text(statement
, 1, name
.data
, name
.size
, SQLITE_STATIC
));
1108 if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
) {
1112 mode
= sqlite3_column_int(statement
, 0);
1113 value
= sqlite3_column_pooled(pool
, statement
, 1);
1116 _sqlcall(sqlite3_finalize(statement
));
1125 return JSEvaluateScript(CYGetJSContext(), CYJSString(value
), NULL
, NULL
, 0, NULL
);
1127 return CYMakeFunctor(context
, reinterpret_cast<void (*)()>(CYCastSymbol(name
.data
)), value
);
1130 // XXX: this is horrendously inefficient
1131 sig::Signature signature
;
1132 sig::Parse(pool
, &signature
, value
, &Structor_
);
1134 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1135 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, CYCastSymbol(name
.data
));
1138 // XXX: implement case 3
1140 return CYMakeType(context
, value
);
1144 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1146 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1150 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1151 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1153 sig::Signature signature
;
1154 sig::Parse(pool
, &signature
, type
, &Structor_
);
1156 return CYMakePointer(context
, value
, signature
.elements
[0].type
, NULL
, NULL
);
1159 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1161 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1163 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1164 return CYMakeType(context
, type
);
1167 static JSValueRef
Type_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1168 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1172 if (JSStringIsEqualToUTF8CString(property
, "$cyi")) {
1173 type
.primitive
= sig::pointer_P
;
1174 type
.data
.data
.size
= 0;
1177 size_t index(CYGetIndex(pool
, context
, property
));
1178 if (index
== _not(size_t))
1180 type
.primitive
= sig::array_P
;
1181 type
.data
.data
.size
= index
;
1187 type
.data
.data
.type
= internal
->type_
;
1189 return CYMakeType(context
, &type
);
1192 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1193 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1196 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1197 sig::Type
*type(internal
->type_
);
1198 ffi_type
*ffi(internal
->GetFFI());
1200 uint8_t value
[ffi
->size
];
1202 CYPoolFFI(pool
, context
, type
, ffi
, value
, arguments
[0]);
1203 return CYFromFFI(context
, type
, ffi
, value
);
1206 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1208 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1209 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1211 sig::Type
*type(internal
->type_
);
1214 if (type
->primitive
!= sig::array_P
)
1217 size
= type
->data
.data
.size
;
1218 type
= type
->data
.data
.type
;
1221 void *value(malloc(internal
->GetFFI()->size
));
1222 return CYMakePointer(context
, value
, type
, NULL
, NULL
);
1225 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1227 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1229 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1230 return CYMakeFunctor(context
, arguments
[0], type
);
1233 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1234 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1235 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1238 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1239 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1242 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1243 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1245 sprintf(string
, "%p", internal
->value_
);
1247 return CYCastJSValue(context
, string
);
1250 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1251 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1253 const char *type(sig::Unparse(pool
, internal
->type_
));
1254 return CYCastJSValue(context
, CYJSString(type
));
1257 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1258 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1260 const char *type(sig::Unparse(pool
, internal
->type_
));
1261 size_t size(strlen(type
));
1262 char *cyon(new(pool
) char[12 + size
+ 1]);
1263 memcpy(cyon
, "new Type(\"", 10);
1264 cyon
[12 + size
] = '\0';
1265 cyon
[12 + size
- 2] = '"';
1266 cyon
[12 + size
- 1] = ')';
1267 memcpy(cyon
+ 10, type
, size
);
1268 return CYCastJSValue(context
, CYJSString(cyon
));
1271 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1272 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1275 static JSStaticValue Pointer_staticValues
[2] = {
1276 {"$cyi", &Pointer_getProperty_$cyi
, &Pointer_setProperty_$cyi
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1277 {NULL
, NULL
, NULL
, 0}
1280 static JSStaticFunction Pointer_staticFunctions
[4] = {
1281 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1282 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1283 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1287 static JSStaticFunction Struct_staticFunctions
[2] = {
1288 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1292 static JSStaticFunction Functor_staticFunctions
[4] = {
1293 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1294 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1295 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1300 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1303 static JSStaticFunction Type_staticFunctions
[4] = {
1304 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1305 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1306 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1310 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1312 void CYSetArgs(int argc
, const char *argv
[]) {
1313 JSContextRef
context(CYGetJSContext());
1314 JSValueRef args
[argc
];
1315 for (int i(0); i
!= argc
; ++i
)
1316 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1319 if (JSObjectMakeArray$
!= NULL
) {
1320 JSValueRef
exception(NULL
);
1321 array
= (*JSObjectMakeArray$
)(context
, argc
, args
, &exception
);
1322 CYThrow(context
, exception
);
1324 JSValueRef
value(CYCallAsFunction(context
, Array_
, NULL
, argc
, args
));
1325 array
= CYCastJSObject(context
, value
);
1328 CYSetProperty(context
, System_
, CYJSString("args"), array
);
1331 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1332 return JSContextGetGlobalObject(context
);
1335 const char *CYExecute(apr_pool_t
*pool
, const char *code
) {
1336 JSContextRef
context(CYGetJSContext());
1337 JSValueRef
exception(NULL
), result
;
1340 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1341 handle
= (*hooks_
->ExecuteStart
)(context
);
1348 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1349 } catch (const char *error
) {
1353 if (exception
!= NULL
) { error
:
1358 if (JSValueIsUndefined(context
, result
))
1362 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1363 } catch (const char *error
) {
1367 if (exception
!= NULL
)
1370 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1372 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1373 (*hooks_
->ExecuteEnd
)(context
, handle
);
1377 static apr_pool_t
*Pool_
;
1379 static bool initialized_
;
1381 void CYInitialize() {
1383 initialized_
= true;
1386 _aprcall(apr_initialize());
1387 _aprcall(apr_pool_create(&Pool_
, NULL
));
1388 _sqlcall(sqlite3_open("/usr/lib/libcycript.db", &Bridge_
));
1390 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1393 apr_pool_t
*CYGetGlobalPool() {
1398 void CYThrow(JSContextRef context
, JSValueRef value
) {
1400 throw CYJSError(context
, value
);
1403 const char *CYJSError::PoolCString(apr_pool_t
*pool
) const {
1404 return CYPoolCString(pool
, context_
, value_
);
1407 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1408 // XXX: what if the context is different?
1412 void CYThrow(const char *format
, ...) {
1414 va_start (args
, format
);
1415 throw CYPoolError(format
, args
);
1416 // XXX: does this matter? :(
1420 const char *CYPoolError::PoolCString(apr_pool_t
*pool
) const {
1421 return apr_pstrdup(pool
, message_
);
1424 CYPoolError::CYPoolError(const char *format
, ...) {
1426 va_start (args
, format
);
1427 message_
= apr_pvsprintf(pool_
, format
, args
);
1431 CYPoolError::CYPoolError(const char *format
, va_list args
) {
1432 message_
= apr_pvsprintf(pool_
, format
, args
);
1435 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1436 return CYCastJSValue(context
, message_
);
1439 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1440 if (context
== NULL
)
1441 context
= CYGetJSContext();
1446 va_start (args
, format
);
1447 const char *message(apr_pvsprintf(pool
, format
, args
));
1450 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(message
))};
1452 JSValueRef
exception(NULL
);
1453 value_
= JSObjectCallAsConstructor(context
, Error_
, 1, arguments
, &exception
);
1454 CYThrow(context
, exception
);
1457 JSGlobalContextRef
CYGetJSContext() {
1460 if (Context_
== NULL
) {
1461 JSClassDefinition definition
;
1463 definition
= kJSClassDefinitionEmpty
;
1464 definition
.className
= "Functor";
1465 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1466 definition
.callAsFunction
= &Functor_callAsFunction
;
1467 definition
.finalize
= &CYFinalize
;
1468 Functor_
= JSClassCreate(&definition
);
1470 definition
= kJSClassDefinitionEmpty
;
1471 definition
.className
= "Pointer";
1472 definition
.staticValues
= Pointer_staticValues
;
1473 definition
.staticFunctions
= Pointer_staticFunctions
;
1474 definition
.getProperty
= &Pointer_getProperty
;
1475 definition
.setProperty
= &Pointer_setProperty
;
1476 definition
.finalize
= &CYFinalize
;
1477 Pointer_
= JSClassCreate(&definition
);
1479 definition
= kJSClassDefinitionEmpty
;
1480 definition
.className
= "Struct";
1481 definition
.staticFunctions
= Struct_staticFunctions
;
1482 definition
.getProperty
= &Struct_getProperty
;
1483 definition
.setProperty
= &Struct_setProperty
;
1484 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1485 definition
.finalize
= &CYFinalize
;
1486 Struct_
= JSClassCreate(&definition
);
1488 definition
= kJSClassDefinitionEmpty
;
1489 definition
.className
= "Type";
1490 definition
.staticFunctions
= Type_staticFunctions
;
1491 definition
.getProperty
= &Type_getProperty
;
1492 definition
.callAsFunction
= &Type_callAsFunction
;
1493 definition
.callAsConstructor
= &Type_callAsConstructor
;
1494 definition
.finalize
= &CYFinalize
;
1495 Type_privateData::Class_
= JSClassCreate(&definition
);
1497 definition
= kJSClassDefinitionEmpty
;
1498 definition
.className
= "Runtime";
1499 definition
.getProperty
= &Runtime_getProperty
;
1500 Runtime_
= JSClassCreate(&definition
);
1502 definition
= kJSClassDefinitionEmpty
;
1503 //definition.getProperty = &Global_getProperty;
1504 JSClassRef
Global(JSClassCreate(&definition
));
1506 JSGlobalContextRef
context(JSGlobalContextCreate(Global
));
1508 JSObjectRef
global(CYGetGlobalObject(context
));
1510 JSObjectSetPrototype(context
, global
, JSObjectMake(context
, Runtime_
, NULL
));
1512 Array_
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array")));
1513 JSValueProtect(context
, Array_
);
1515 Error_
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error")));
1516 JSValueProtect(context
, Error_
);
1518 Function_
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function")));
1519 JSValueProtect(context
, Function_
);
1521 String_
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String")));
1522 JSValueProtect(context
, String_
);
1524 length_
= JSStringCreateWithUTF8CString("length");
1525 message_
= JSStringCreateWithUTF8CString("message");
1526 name_
= JSStringCreateWithUTF8CString("name");
1527 prototype_
= JSStringCreateWithUTF8CString("prototype");
1528 toCYON_
= JSStringCreateWithUTF8CString("toCYON");
1529 toJSON_
= JSStringCreateWithUTF8CString("toJSON");
1531 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1532 Object_prototype_
= CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_
));
1533 JSValueProtect(context
, Object_prototype_
);
1535 Array_prototype_
= CYCastJSObject(context
, CYGetProperty(context
, Array_
, prototype_
));
1536 Array_pop_
= CYCastJSObject(context
, CYGetProperty(context
, Array_prototype_
, CYJSString("pop")));
1537 Array_push_
= CYCastJSObject(context
, CYGetProperty(context
, Array_prototype_
, CYJSString("push")));
1538 Array_splice_
= CYCastJSObject(context
, CYGetProperty(context
, Array_prototype_
, CYJSString("splice")));
1540 CYSetProperty(context
, Array_prototype_
, toCYON_
, JSObjectMakeFunctionWithCallback(context
, toCYON_
, &Array_callAsFunction_toCYON
), kJSPropertyAttributeDontEnum
);
1542 JSValueProtect(context
, Array_prototype_
);
1543 JSValueProtect(context
, Array_pop_
);
1544 JSValueProtect(context
, Array_push_
);
1545 JSValueProtect(context
, Array_splice_
);
1547 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1549 Function_prototype_
= (JSObjectRef
) CYGetProperty(context
, Function_
, prototype_
);
1550 JSValueProtect(context
, Function_prototype_
);
1552 JSObjectSetPrototype(context
, (JSObjectRef
) CYGetProperty(context
, Functor
, prototype_
), Function_prototype_
);
1554 CYSetProperty(context
, global
, CYJSString("Functor"), Functor
);
1555 CYSetProperty(context
, global
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1556 CYSetProperty(context
, global
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1558 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1559 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1560 CYSetProperty(context
, cycript
, CYJSString("gc"), JSObjectMakeFunctionWithCallback(context
, CYJSString("gc"), &Cycript_gc_callAsFunction
));
1562 CYSetProperty(context
, global
, CYJSString("$cyq"), JSObjectMakeFunctionWithCallback(context
, CYJSString("$cyq"), &$cyq
));
1564 System_
= JSObjectMake(context
, NULL
, NULL
);
1565 JSValueProtect(context
, System_
);
1567 CYSetProperty(context
, global
, CYJSString("system"), System_
);
1568 CYSetProperty(context
, System_
, CYJSString("args"), CYJSNull(context
));
1569 //CYSetProperty(context, System_, CYJSString("global"), global);
1571 CYSetProperty(context
, System_
, CYJSString("print"), JSObjectMakeFunctionWithCallback(context
, CYJSString("print"), &System_print
));
1573 Result_
= JSStringCreateWithUTF8CString("_");
1575 if (hooks_
!= NULL
&& hooks_
->SetupContext
!= NULL
)
1576 (*hooks_
->SetupContext
)(context
);