1 /* Cycript - Inlining/Optimizing JavaScript Compiler
2 * Copyright (C) 2009 Jay Freeman (saurik)
5 /* Modified BSD License {{{ */
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include "Internal.hpp"
47 #include "cycript.hpp"
49 #include "sig/parse.hpp"
50 #include "sig/ffi_type.hpp"
52 #include "Pooling.hpp"
57 #include <ext/stdio_filebuf.h>
65 #include "Cycript.tab.hh"
68 #include "JavaScript.hpp"
73 catch (NSException *error) { \
74 CYThrow(context, error, exception); \
81 char *sqlite3_column_pooled(apr_pool_t
*pool
, sqlite3_stmt
*stmt
, int n
) {
82 if (const unsigned char *value
= sqlite3_column_text(stmt
, n
))
83 return apr_pstrdup(pool
, (const char *) value
);
87 struct CYHooks
*hooks_
;
89 /* JavaScript Properties {{{ */
90 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) {
91 JSValueRef
exception(NULL
);
92 JSValueRef
value(JSObjectGetPropertyAtIndex(context
, object
, index
, &exception
));
93 CYThrow(context
, exception
);
97 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
98 JSValueRef
exception(NULL
);
99 JSValueRef
value(JSObjectGetProperty(context
, object
, name
, &exception
));
100 CYThrow(context
, exception
);
104 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) {
105 JSValueRef
exception(NULL
);
106 JSObjectSetPropertyAtIndex(context
, object
, index
, value
, &exception
);
107 CYThrow(context
, exception
);
110 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) {
111 JSValueRef
exception(NULL
);
112 JSObjectSetProperty(context
, object
, name
, value
, attributes
, &exception
);
113 CYThrow(context
, exception
);
116 /* JavaScript Strings {{{ */
117 JSStringRef
CYCopyJSString(const char *value
) {
118 return value
== NULL
? NULL
: JSStringCreateWithUTF8CString(value
);
121 JSStringRef
CYCopyJSString(JSStringRef value
) {
122 return value
== NULL
? NULL
: JSStringRetain(value
);
125 JSStringRef
CYCopyJSString(CYUTF8String value
) {
126 // XXX: this is very wrong
127 return CYCopyJSString(value
.data
);
130 JSStringRef
CYCopyJSString(JSContextRef context
, JSValueRef value
) {
131 if (JSValueIsNull(context
, value
))
133 JSValueRef
exception(NULL
);
134 JSStringRef
string(JSValueToStringCopy(context
, value
, &exception
));
135 CYThrow(context
, exception
);
139 static CYUTF16String
CYCastUTF16String(JSStringRef value
) {
140 return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
));
143 template <typename Type_
>
144 _finline
size_t iconv_(size_t (*iconv
)(iconv_t
, Type_
, size_t *, char **, size_t *), iconv_t cd
, char **inbuf
, size_t *inbytesleft
, char **outbuf
, size_t *outbytesleft
) {
145 return iconv(cd
, const_cast<Type_
>(inbuf
), inbytesleft
, outbuf
, outbytesleft
);
148 CYUTF8String
CYPoolUTF8String(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
149 _assert(pool
!= NULL
);
151 CYUTF16String
utf16(CYCastUTF16String(value
));
152 const char *in(reinterpret_cast<const char *>(utf16
.data
));
155 iconv_t
conversion(_syscall(iconv_open("UTF-8", "UCS-2")));
157 iconv_t
conversion(_syscall(iconv_open("UTF-8", "UCS-2-INTERNAL")));
160 size_t size(JSStringGetMaximumUTF8CStringSize(value
));
161 char *out(new(pool
) char[size
]);
162 CYUTF8String
utf8(out
, size
);
164 size
= utf16
.size
* 2;
165 _syscall(iconv_(&iconv
, conversion
, const_cast<char **>(&in
), &size
, &out
, &utf8
.size
));
168 utf8
.size
= out
- utf8
.data
;
170 _syscall(iconv_close(conversion
));
175 const char *CYPoolCString(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
176 CYUTF8String
utf8(CYPoolUTF8String(pool
, context
, value
));
177 _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
);
181 const char *CYPoolCString(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
) {
182 return JSValueIsNull(context
, value
) ? NULL
: CYPoolCString(pool
, context
, CYJSString(context
, value
));
186 /* Index Offsets {{{ */
187 size_t CYGetIndex(const CYUTF8String
&value
) {
188 if (value
.data
[0] != '0') {
190 for (size_t i(0); i
!= value
.size
; ++i
) {
191 if (!DigitRange_
[value
.data
[i
]])
194 index
+= value
.data
[i
] - '0';
197 } else if (value
.size
== 1)
203 size_t CYGetIndex(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
204 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
207 // XXX: this isn't actually right
208 bool CYGetOffset(const char *value
, ssize_t
&index
) {
209 if (value
[0] != '0') {
211 index
= strtol(value
, &end
, 10);
212 if (value
+ strlen(value
) == end
)
214 } else if (value
[1] == '\0') {
223 /* JavaScript *ify {{{ */
224 void CYStringify(std::ostringstream
&str
, const char *data
, size_t size
) {
225 unsigned quot(0), apos(0);
226 for (const char *value(data
), *end(data
+ size
); value
!= end
; ++value
)
229 else if (*value
== '\'')
232 bool single(quot
> apos
);
234 str
<< (single
? '\'' : '"');
236 for (const char *value(data
), *end(data
+ size
); value
!= end
; ++value
)
238 case '\\': str
<< "\\\\"; break;
239 case '\b': str
<< "\\b"; break;
240 case '\f': str
<< "\\f"; break;
241 case '\n': str
<< "\\n"; break;
242 case '\r': str
<< "\\r"; break;
243 case '\t': str
<< "\\t"; break;
244 case '\v': str
<< "\\v"; break;
259 // this test is designed to be "awewsome", generating neither warnings nor incorrect results
260 if (*value
< 0x20 || *value
>= 0x7f)
261 str
<< "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(uint8_t(*value
));
266 str
<< (single
? '\'' : '"');
269 void CYNumerify(std::ostringstream
&str
, double value
) {
271 // XXX: I want this to print 1e3 rather than 1000
272 sprintf(string
, "%.17g", value
);
276 bool CYIsKey(CYUTF8String value
) {
277 const char *data(value
.data
);
278 size_t size(value
.size
);
283 if (DigitRange_
[data
[0]]) {
284 size_t index(CYGetIndex(value
));
285 if (index
== _not(size_t))
288 if (!WordStartRange_
[data
[0]])
290 for (size_t i(1); i
!= size
; ++i
)
291 if (!WordEndRange_
[data
[i
]])
299 static JSGlobalContextRef Context_
;
300 static JSObjectRef System_
;
302 static JSClassRef Functor_
;
303 static JSClassRef Pointer_
;
304 static JSClassRef Runtime_
;
305 static JSClassRef Struct_
;
307 static JSStringRef Result_
;
311 JSObjectRef Function_
;
315 JSStringRef message_
;
317 JSStringRef prototype_
;
321 JSObjectRef Object_prototype_
;
322 JSObjectRef Function_prototype_
;
324 JSObjectRef Array_prototype_
;
325 JSObjectRef Array_pop_
;
326 JSObjectRef Array_push_
;
327 JSObjectRef Array_splice_
;
331 void CYFinalize(JSObjectRef object
) {
332 delete reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
));
335 struct CStringMapLess
:
336 std::binary_function
<const char *, const char *, bool>
338 _finline
bool operator ()(const char *lhs
, const char *rhs
) const {
339 return strcmp(lhs
, rhs
) < 0;
343 void Structor_(apr_pool_t
*pool
, sig::Type
*&type
) {
345 type
->primitive
== sig::pointer_P
&&
346 type
->data
.data
.type
!= NULL
&&
347 type
->data
.data
.type
->primitive
== sig::struct_P
&&
348 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
350 type
->primitive
= sig::typename_P
;
351 type
->data
.data
.type
= NULL
;
355 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
358 sqlite3_stmt
*statement
;
360 _sqlcall(sqlite3_prepare(Bridge_
,
362 "\"bridge\".\"mode\", "
363 "\"bridge\".\"value\" "
366 " \"bridge\".\"mode\" in (3, 4) and"
367 " \"bridge\".\"name\" = ?"
369 , -1, &statement
, NULL
));
371 _sqlcall(sqlite3_bind_text(statement
, 1, type
->name
, -1, SQLITE_STATIC
));
376 if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
) {
380 mode
= sqlite3_column_int(statement
, 0);
381 value
= sqlite3_column_pooled(pool
, statement
, 1);
384 _sqlcall(sqlite3_finalize(statement
));
393 sig::Parse(pool
, &type
->data
.signature
, value
, &Structor_
);
397 sig::Signature signature
;
398 sig::Parse(pool
, &signature
, value
, &Structor_
);
399 type
= signature
.elements
[0].type
;
404 JSClassRef
Type_privateData::Class_
;
409 Type_privateData
*type_
;
411 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, sig::Type
*type
) :
412 CYOwned(value
, context
, owner
),
413 type_(new(pool_
) Type_privateData(type
))
418 struct Struct_privateData
:
421 Type_privateData
*type_
;
423 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
424 CYOwned(NULL
, context
, owner
)
429 typedef std::map
<const char *, Type_privateData
*, CStringMapLess
> TypeMap
;
430 static TypeMap Types_
;
432 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
433 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
434 apr_pool_t
*pool(internal
->pool_
);
435 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
436 internal
->type_
= typical
;
439 internal
->value_
= data
;
441 size_t size(typical
->GetFFI()->size
);
442 void *copy(apr_palloc(internal
->pool_
, size
));
443 memcpy(copy
, data
, size
);
444 internal
->value_
= copy
;
447 return JSObjectMake(context
, Struct_
, internal
);
450 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
451 return JSValueMakeBoolean(context
, value
);
454 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
455 return JSValueMakeNumber(context
, value
);
458 #define CYCastJSValue_(Type_) \
459 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
460 return JSValueMakeNumber(context, static_cast<double>(value)); \
464 CYCastJSValue_(unsigned int)
465 CYCastJSValue_(long int)
466 CYCastJSValue_(long unsigned int)
467 CYCastJSValue_(long long int)
468 CYCastJSValue_(long long unsigned int)
470 JSValueRef
CYJSUndefined(JSContextRef context
) {
471 return JSValueMakeUndefined(context
);
474 double CYCastDouble(const char *value
, size_t size
) {
476 double number(strtod(value
, &end
));
477 if (end
!= value
+ size
)
482 double CYCastDouble(const char *value
) {
483 return CYCastDouble(value
, strlen(value
));
486 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
487 JSValueRef
exception(NULL
);
488 double number(JSValueToNumber(context
, value
, &exception
));
489 CYThrow(context
, exception
);
493 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
494 return JSValueToBoolean(context
, value
);
497 JSValueRef
CYJSNull(JSContextRef context
) {
498 return JSValueMakeNull(context
);
501 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
502 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
505 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
506 return CYCastJSValue(context
, CYJSString(value
));
509 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
510 JSValueRef
exception(NULL
);
511 JSObjectRef
object(JSValueToObject(context
, value
, &exception
));
512 CYThrow(context
, exception
);
516 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, JSValueRef arguments
[]) {
517 JSValueRef
exception(NULL
);
518 JSValueRef
value(JSObjectCallAsFunction(context
, function
, _this
, count
, arguments
, &exception
));
519 CYThrow(context
, exception
);
523 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
524 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
527 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
532 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
535 return CYJSUndefined(context
);
538 static size_t Nonce_(0);
540 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
542 const char *name(apr_psprintf(pool
, "%s%"APR_SIZE_T_FMT
"", CYPoolCString(pool
, context
, arguments
[0]), Nonce_
++));
543 return CYCastJSValue(context
, name
);
546 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
547 JSGarbageCollect(context
);
548 return CYJSUndefined(context
);
551 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
552 switch (JSType type
= JSValueGetType(context
, value
)) {
553 case kJSTypeUndefined
:
558 return CYCastBool(context
, value
) ? "true" : "false";
560 case kJSTypeNumber
: {
561 std::ostringstream str
;
562 CYNumerify(str
, CYCastDouble(context
, value
));
563 std::string
value(str
.str());
564 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
567 case kJSTypeString
: {
568 std::ostringstream str
;
569 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
570 CYStringify(str
, string
.data
, string
.size
);
571 std::string
value(str
.str());
572 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
576 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
578 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
582 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
) {
583 JSValueRef
exception(NULL
);
584 const char *cyon(CYPoolCCYON(pool
, context
, value
, &exception
));
585 CYThrow(context
, exception
);
589 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSObjectRef object
) {
590 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_
));
591 if (CYIsCallable(context
, toCYON
)) {
592 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
593 return CYPoolCString(pool
, context
, value
);
596 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_
));
597 if (CYIsCallable(context
, toJSON
)) {
598 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
599 JSValueRef
exception(NULL
);
600 const char *cyon(CYPoolCCYON(pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), &exception
));
601 CYThrow(context
, exception
);
605 std::ostringstream str
;
609 // XXX: this is, sadly, going to leak
610 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
614 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
615 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
616 JSValueRef
value(CYGetProperty(context
, object
, name
));
623 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
627 CYStringify(str
, string
.data
, string
.size
);
629 str
<< ':' << CYPoolCCYON(pool
, context
, value
);
634 JSPropertyNameArrayRelease(names
);
636 std::string
string(str
.str());
637 return apr_pstrmemdup(pool
, string
.c_str(), string
.size());
640 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
642 std::ostringstream str
;
646 JSValueRef
length(CYGetProperty(context
, _this
, length_
));
649 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
650 JSValueRef
value(CYGetProperty(context
, _this
, index
));
657 if (!JSValueIsUndefined(context
, value
))
658 str
<< CYPoolCCYON(pool
, context
, value
);
667 std::string
value(str
.str());
668 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
671 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
672 Pointer
*internal(new Pointer(pointer
, context
, owner
, type
));
673 return JSObjectMake(context
, Pointer_
, internal
);
676 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const char *type
) {
677 cy::Functor
*internal(new cy::Functor(type
, function
));
678 return JSObjectMake(context
, Functor_
, internal
);
681 static bool CYGetOffset(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
682 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
685 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
686 switch (JSValueGetType(context
, value
)) {
689 /*case kJSTypeObject:
690 if (JSValueIsObjectOfClass(context, value, Pointer_)) {
691 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value)));
692 return internal->value_;
695 double number(CYCastDouble(context
, value
));
696 if (std::isnan(number
))
697 throw CYJSError(context
, "cannot convert value to pointer");
698 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
702 void CYPoolFFI(apr_pool_t
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
703 switch (type
->primitive
) {
705 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
708 #define CYPoolFFI_(primitive, native) \
709 case sig::primitive ## _P: \
710 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
713 CYPoolFFI_(uchar
, unsigned char)
714 CYPoolFFI_(char, char)
715 CYPoolFFI_(ushort
, unsigned short)
716 CYPoolFFI_(short, short)
717 CYPoolFFI_(ulong
, unsigned long)
718 CYPoolFFI_(long, long)
719 CYPoolFFI_(uint
, unsigned int)
721 CYPoolFFI_(ulonglong
, unsigned long long)
722 CYPoolFFI_(longlong
, long long)
723 CYPoolFFI_(float, float)
724 CYPoolFFI_(double, double)
727 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
731 *reinterpret_cast<const char **>(data
) = CYPoolCString(pool
, context
, value
);
734 case sig::struct_P
: {
735 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
736 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
737 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
738 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
739 ffi_type
*field(ffi
->elements
[index
]);
742 if (aggregate
== NULL
)
745 rhs
= CYGetProperty(context
, aggregate
, index
);
746 if (JSValueIsUndefined(context
, rhs
)) {
747 if (element
->name
!= NULL
)
748 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
751 if (JSValueIsUndefined(context
, rhs
)) undefined
:
752 throw CYJSError(context
, "unable to extract structure value");
756 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
766 if (hooks_
!= NULL
&& hooks_
->PoolFFI
!= NULL
)
767 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
770 fprintf(stderr
, "CYPoolFFI(%c)\n", type
->primitive
);
775 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
776 switch (type
->primitive
) {
778 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
780 #define CYFromFFI_(primitive, native) \
781 case sig::primitive ## _P: \
782 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
784 CYFromFFI_(uchar, unsigned char)
785 CYFromFFI_(char, char)
786 CYFromFFI_(ushort
, unsigned short)
787 CYFromFFI_(short, short)
788 CYFromFFI_(ulong
, unsigned long)
789 CYFromFFI_(long, long)
790 CYFromFFI_(uint
, unsigned int)
792 CYFromFFI_(ulonglong
, unsigned long long)
793 CYFromFFI_(longlong
, long long)
794 CYFromFFI_(float, float)
795 CYFromFFI_(double, double)
798 if (void *pointer
= *reinterpret_cast<void **>(data
))
799 return CYMakePointer(context
, pointer
, type
->data
.data
.type
, ffi
, owner
);
803 if (char *utf8
= *reinterpret_cast<char **>(data
))
804 return CYCastJSValue(context
, utf8
);
808 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
810 return CYJSUndefined(context
);
813 return CYJSNull(context
);
815 if (hooks_
!= NULL
&& hooks_
->FromFFI
!= NULL
)
816 if (JSValueRef value
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
819 fprintf(stderr
, "CYFromFFI(%c)\n", type
->primitive
);
824 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
825 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
827 JSContextRef
context(internal
->context_
);
829 size_t count(internal
->cif_
.nargs
);
830 JSValueRef values
[count
];
832 for (size_t index(0); index
!= count
; ++index
)
833 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
835 JSValueRef
value(CYCallAsFunction(context
, internal
->function_
, NULL
, count
, values
));
836 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
839 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const char *type
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
840 // XXX: in case of exceptions this will leak
841 // XXX: in point of fact, this may /need/ to leak :(
842 Closure_privateData
*internal(new Closure_privateData(CYGetJSContext(), function
, type
));
844 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
845 NULL
, sizeof(ffi_closure
),
846 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
850 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
851 _assert(status
== FFI_OK
);
853 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
855 internal
->value_
= closure
;
860 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const char *type
) {
861 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, type
, &FunctionClosure_
));
862 return JSObjectMake(context
, Functor_
, internal
);
865 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const char *type
) {
866 JSValueRef
exception(NULL
);
867 bool function(JSValueIsInstanceOfConstructor(context
, value
, Function_
, &exception
));
868 CYThrow(context
, exception
);
871 JSObjectRef
function(CYCastJSObject(context
, value
));
872 return CYMakeFunctor(context
, function
, type
);
874 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
875 return CYMakeFunctor(context
, function
, type
);
879 static bool Index_(apr_pool_t
*pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
880 Type_privateData
*typical(internal
->type_
);
881 sig::Type
*type(typical
->type_
);
885 const char *name(CYPoolCString(pool
, context
, property
));
886 size_t length(strlen(name
));
887 double number(CYCastDouble(name
, length
));
889 size_t count(type
->data
.signature
.count
);
891 if (std::isnan(number
)) {
892 if (property
== NULL
)
895 sig::Element
*elements(type
->data
.signature
.elements
);
897 for (size_t local(0); local
!= count
; ++local
) {
898 sig::Element
*element(&elements
[local
]);
899 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
907 index
= static_cast<ssize_t
>(number
);
908 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
913 ffi_type
**elements(typical
->GetFFI()->elements
);
915 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
916 for (ssize_t
local(0); local
!= index
; ++local
)
917 base
+= elements
[local
]->size
;
922 static JSValueRef
Pointer_getIndex(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef
*exception
) { CYTry
{
923 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
924 Type_privateData
*typical(internal
->type_
);
926 ffi_type
*ffi(typical
->GetFFI());
928 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
929 base
+= ffi
->size
* index
;
931 JSObjectRef
owner(internal
->GetOwner() ?: object
);
932 return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
);
935 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) {
937 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
938 Type_privateData
*typical(internal
->type_
);
940 if (typical
->type_
== NULL
)
944 if (!CYGetOffset(pool
, context
, property
, offset
))
947 return Pointer_getIndex(context
, object
, offset
, exception
);
950 static JSValueRef Pointer_getProperty_$
cyi(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) {
951 return Pointer_getIndex(context
, object
, 0, exception
);
954 static bool Pointer_setIndex(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
955 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
956 Type_privateData
*typical(internal
->type_
);
958 ffi_type
*ffi(typical
->GetFFI());
960 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
961 base
+= ffi
->size
* index
;
963 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
967 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) {
969 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
970 Type_privateData
*typical(internal
->type_
);
972 if (typical
->type_
== NULL
)
976 if (!CYGetOffset(pool
, context
, property
, offset
))
979 return Pointer_setIndex(context
, object
, offset
, value
, exception
);
982 static bool Pointer_setProperty_$
cyi(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) {
983 return Pointer_setIndex(context
, object
, 0, value
, exception
);
986 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
987 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
988 Type_privateData
*typical(internal
->type_
);
989 return CYMakePointer(context
, internal
->value_
, typical
->type_
, typical
->ffi_
, _this
);
992 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
994 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
995 Type_privateData
*typical(internal
->type_
);
1000 if (!Index_(pool
, context
, internal
, property
, index
, base
))
1003 JSObjectRef
owner(internal
->GetOwner() ?: object
);
1005 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
1008 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1010 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1011 Type_privateData
*typical(internal
->type_
);
1016 if (!Index_(pool
, context
, internal
, property
, index
, base
))
1019 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
1023 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1024 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
1025 Type_privateData
*typical(internal
->type_
);
1026 sig::Type
*type(typical
->type_
);
1031 size_t count(type
->data
.signature
.count
);
1032 sig::Element
*elements(type
->data
.signature
.elements
);
1036 for (size_t index(0); index
!= count
; ++index
) {
1038 name
= elements
[index
].name
;
1041 sprintf(number
, "%zu", index
);
1045 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
1049 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
{
1050 if (setups
+ count
!= signature
->count
- 1)
1051 throw CYJSError(context
, "incorrect number of arguments to ffi function");
1053 size_t size(setups
+ count
);
1055 memcpy(values
, setup
, sizeof(void *) * setups
);
1057 for (size_t index(setups
); index
!= size
; ++index
) {
1058 sig::Element
*element(&signature
->elements
[index
+ 1]);
1059 ffi_type
*ffi(cif
->arg_types
[index
]);
1061 values
[index
] = new(pool
) uint8_t[ffi
->size
];
1062 CYPoolFFI(pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
1065 uint8_t value
[cif
->rtype
->size
];
1067 if (hooks_
!= NULL
&& hooks_
->CallFunction
!= NULL
)
1068 (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
);
1070 ffi_call(cif
, function
, value
, values
);
1072 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
1075 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1077 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1078 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, exception
, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
1081 static JSObjectRef
CYMakeType(JSContextRef context
, const char *type
) {
1082 Type_privateData
*internal(new Type_privateData(NULL
, type
));
1083 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1086 static JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
1087 Type_privateData
*internal(new Type_privateData(type
));
1088 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1091 static void *CYCastSymbol(const char *name
) {
1092 return dlsym(RTLD_DEFAULT
, name
);
1095 static JSValueRef
Runtime_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1097 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
1099 if (hooks_
!= NULL
&& hooks_
->RuntimeProperty
!= NULL
)
1100 if (JSValueRef value
= (*hooks_
->RuntimeProperty
)(context
, name
))
1103 sqlite3_stmt
*statement
;
1105 _sqlcall(sqlite3_prepare(Bridge_
,
1107 "\"bridge\".\"mode\", "
1108 "\"bridge\".\"value\" "
1111 " \"bridge\".\"name\" = ?"
1113 , -1, &statement
, NULL
));
1115 _sqlcall(sqlite3_bind_text(statement
, 1, name
.data
, name
.size
, SQLITE_STATIC
));
1120 if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
) {
1124 mode
= sqlite3_column_int(statement
, 0);
1125 value
= sqlite3_column_pooled(pool
, statement
, 1);
1128 _sqlcall(sqlite3_finalize(statement
));
1137 return JSEvaluateScript(CYGetJSContext(), CYJSString(value
), NULL
, NULL
, 0, NULL
);
1140 if (void (*symbol
)() = reinterpret_cast<void (*)()>(CYCastSymbol(name
.data
)))
1141 return CYMakeFunctor(context
, symbol
, value
);
1145 if (void *symbol
= CYCastSymbol(name
.data
)) {
1146 // XXX: this is horrendously inefficient
1147 sig::Signature signature
;
1148 sig::Parse(pool
, &signature
, value
, &Structor_
);
1150 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1151 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
1154 // XXX: implement case 3
1156 return CYMakeType(context
, value
);
1160 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1162 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1166 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1167 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1169 sig::Signature signature
;
1170 sig::Parse(pool
, &signature
, type
, &Structor_
);
1172 return CYMakePointer(context
, value
, signature
.elements
[0].type
, NULL
, NULL
);
1175 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1177 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1179 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1180 return CYMakeType(context
, type
);
1183 static JSValueRef
Type_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1184 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1188 if (JSStringIsEqualToUTF8CString(property
, "$cyi")) {
1189 type
.primitive
= sig::pointer_P
;
1190 type
.data
.data
.size
= 0;
1193 size_t index(CYGetIndex(pool
, context
, property
));
1194 if (index
== _not(size_t))
1196 type
.primitive
= sig::array_P
;
1197 type
.data
.data
.size
= index
;
1203 type
.data
.data
.type
= internal
->type_
;
1205 return CYMakeType(context
, &type
);
1208 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1209 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1212 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1213 sig::Type
*type(internal
->type_
);
1214 ffi_type
*ffi(internal
->GetFFI());
1216 uint8_t value
[ffi
->size
];
1218 CYPoolFFI(pool
, context
, type
, ffi
, value
, arguments
[0]);
1219 return CYFromFFI(context
, type
, ffi
, value
);
1222 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1224 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1225 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1227 sig::Type
*type(internal
->type_
);
1230 if (type
->primitive
!= sig::array_P
)
1233 size
= type
->data
.data
.size
;
1234 type
= type
->data
.data
.type
;
1237 void *value(malloc(internal
->GetFFI()->size
));
1238 return CYMakePointer(context
, value
, type
, NULL
, NULL
);
1241 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1243 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1245 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1246 return CYMakeFunctor(context
, arguments
[0], type
);
1249 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1250 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1251 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1254 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1255 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1258 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1259 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1261 sprintf(string
, "%p", internal
->value_
);
1263 return CYCastJSValue(context
, string
);
1266 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1267 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1269 const char *type(sig::Unparse(pool
, internal
->type_
));
1270 return CYCastJSValue(context
, CYJSString(type
));
1273 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1274 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1276 const char *type(sig::Unparse(pool
, internal
->type_
));
1277 size_t size(strlen(type
));
1278 char *cyon(new(pool
) char[12 + size
+ 1]);
1279 memcpy(cyon
, "new Type(\"", 10);
1280 cyon
[12 + size
] = '\0';
1281 cyon
[12 + size
- 2] = '"';
1282 cyon
[12 + size
- 1] = ')';
1283 memcpy(cyon
+ 10, type
, size
);
1284 return CYCastJSValue(context
, CYJSString(cyon
));
1287 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1288 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1291 static JSStaticValue Pointer_staticValues
[2] = {
1292 {"$cyi", &Pointer_getProperty_$cyi
, &Pointer_setProperty_$cyi
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1293 {NULL
, NULL
, NULL
, 0}
1296 static JSStaticFunction Pointer_staticFunctions
[4] = {
1297 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1298 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1299 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1303 static JSStaticFunction Struct_staticFunctions
[2] = {
1304 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1308 static JSStaticFunction Functor_staticFunctions
[4] = {
1309 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1310 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1311 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1316 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1319 static JSStaticFunction Type_staticFunctions
[4] = {
1320 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1321 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1322 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1326 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1328 void CYSetArgs(int argc
, const char *argv
[]) {
1329 JSContextRef
context(CYGetJSContext());
1330 JSValueRef args
[argc
];
1331 for (int i(0); i
!= argc
; ++i
)
1332 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1335 if (JSObjectMakeArray$
!= NULL
) {
1336 JSValueRef
exception(NULL
);
1337 array
= (*JSObjectMakeArray$
)(context
, argc
, args
, &exception
);
1338 CYThrow(context
, exception
);
1340 JSValueRef
value(CYCallAsFunction(context
, Array_
, NULL
, argc
, args
));
1341 array
= CYCastJSObject(context
, value
);
1344 CYSetProperty(context
, System_
, CYJSString("args"), array
);
1347 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1348 return JSContextGetGlobalObject(context
);
1351 const char *CYExecute(apr_pool_t
*pool
, const char *code
) {
1352 JSContextRef
context(CYGetJSContext());
1353 JSValueRef
exception(NULL
), result
;
1356 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1357 handle
= (*hooks_
->ExecuteStart
)(context
);
1364 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1365 } catch (const char *error
) {
1369 if (exception
!= NULL
) { error
:
1374 if (JSValueIsUndefined(context
, result
))
1378 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1379 } catch (const char *error
) {
1383 if (exception
!= NULL
)
1386 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1388 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1389 (*hooks_
->ExecuteEnd
)(context
, handle
);
1393 static apr_pool_t
*Pool_
;
1395 static bool initialized_
;
1397 void CYInitialize() {
1399 initialized_
= true;
1402 _aprcall(apr_initialize());
1403 _aprcall(apr_pool_create(&Pool_
, NULL
));
1404 _sqlcall(sqlite3_open("/usr/lib/libcycript.db", &Bridge_
));
1406 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1409 apr_pool_t
*CYGetGlobalPool() {
1414 void CYThrow(JSContextRef context
, JSValueRef value
) {
1416 throw CYJSError(context
, value
);
1419 const char *CYJSError::PoolCString(apr_pool_t
*pool
) const {
1420 return CYPoolCString(pool
, context_
, value_
);
1423 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1424 // XXX: what if the context is different?
1428 void CYThrow(const char *format
, ...) {
1430 va_start (args
, format
);
1431 throw CYPoolError(format
, args
);
1432 // XXX: does this matter? :(
1436 const char *CYPoolError::PoolCString(apr_pool_t
*pool
) const {
1437 return apr_pstrdup(pool
, message_
);
1440 CYPoolError::CYPoolError(const char *format
, ...) {
1442 va_start (args
, format
);
1443 message_
= apr_pvsprintf(pool_
, format
, args
);
1447 CYPoolError::CYPoolError(const char *format
, va_list args
) {
1448 message_
= apr_pvsprintf(pool_
, format
, args
);
1451 JSValueRef
CYCastJSError(JSContextRef context
, const char *message
) {
1452 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1454 JSValueRef
exception(NULL
);
1455 JSValueRef
value(JSObjectCallAsConstructor(context
, Error_
, 1, arguments
, &exception
));
1456 CYThrow(context
, exception
);
1461 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1462 return CYCastJSError(context
, message_
);
1465 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1466 if (context
== NULL
)
1467 context
= CYGetJSContext();
1472 va_start (args
, format
);
1473 const char *message(apr_pvsprintf(pool
, format
, args
));
1476 value_
= CYCastJSError(context
, message
);
1479 JSGlobalContextRef
CYGetJSContext() {
1482 if (Context_
== NULL
) {
1483 JSClassDefinition definition
;
1485 definition
= kJSClassDefinitionEmpty
;
1486 definition
.className
= "Functor";
1487 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1488 definition
.callAsFunction
= &Functor_callAsFunction
;
1489 definition
.finalize
= &CYFinalize
;
1490 Functor_
= JSClassCreate(&definition
);
1492 definition
= kJSClassDefinitionEmpty
;
1493 definition
.className
= "Pointer";
1494 definition
.staticValues
= Pointer_staticValues
;
1495 definition
.staticFunctions
= Pointer_staticFunctions
;
1496 definition
.getProperty
= &Pointer_getProperty
;
1497 definition
.setProperty
= &Pointer_setProperty
;
1498 definition
.finalize
= &CYFinalize
;
1499 Pointer_
= JSClassCreate(&definition
);
1501 definition
= kJSClassDefinitionEmpty
;
1502 definition
.className
= "Struct";
1503 definition
.staticFunctions
= Struct_staticFunctions
;
1504 definition
.getProperty
= &Struct_getProperty
;
1505 definition
.setProperty
= &Struct_setProperty
;
1506 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1507 definition
.finalize
= &CYFinalize
;
1508 Struct_
= JSClassCreate(&definition
);
1510 definition
= kJSClassDefinitionEmpty
;
1511 definition
.className
= "Type";
1512 definition
.staticFunctions
= Type_staticFunctions
;
1513 definition
.getProperty
= &Type_getProperty
;
1514 definition
.callAsFunction
= &Type_callAsFunction
;
1515 definition
.callAsConstructor
= &Type_callAsConstructor
;
1516 definition
.finalize
= &CYFinalize
;
1517 Type_privateData::Class_
= JSClassCreate(&definition
);
1519 definition
= kJSClassDefinitionEmpty
;
1520 definition
.className
= "Runtime";
1521 definition
.getProperty
= &Runtime_getProperty
;
1522 Runtime_
= JSClassCreate(&definition
);
1524 definition
= kJSClassDefinitionEmpty
;
1525 //definition.getProperty = &Global_getProperty;
1526 JSClassRef
Global(JSClassCreate(&definition
));
1528 JSGlobalContextRef
context(JSGlobalContextCreate(Global
));
1530 JSObjectRef
global(CYGetGlobalObject(context
));
1532 JSObjectSetPrototype(context
, global
, JSObjectMake(context
, Runtime_
, NULL
));
1534 Array_
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array")));
1535 JSValueProtect(context
, Array_
);
1537 Error_
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error")));
1538 JSValueProtect(context
, Error_
);
1540 Function_
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function")));
1541 JSValueProtect(context
, Function_
);
1543 String_
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String")));
1544 JSValueProtect(context
, String_
);
1546 length_
= JSStringCreateWithUTF8CString("length");
1547 message_
= JSStringCreateWithUTF8CString("message");
1548 name_
= JSStringCreateWithUTF8CString("name");
1549 prototype_
= JSStringCreateWithUTF8CString("prototype");
1550 toCYON_
= JSStringCreateWithUTF8CString("toCYON");
1551 toJSON_
= JSStringCreateWithUTF8CString("toJSON");
1553 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1554 Object_prototype_
= CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_
));
1555 JSValueProtect(context
, Object_prototype_
);
1557 Array_prototype_
= CYCastJSObject(context
, CYGetProperty(context
, Array_
, prototype_
));
1558 Array_pop_
= CYCastJSObject(context
, CYGetProperty(context
, Array_prototype_
, CYJSString("pop")));
1559 Array_push_
= CYCastJSObject(context
, CYGetProperty(context
, Array_prototype_
, CYJSString("push")));
1560 Array_splice_
= CYCastJSObject(context
, CYGetProperty(context
, Array_prototype_
, CYJSString("splice")));
1562 CYSetProperty(context
, Array_prototype_
, toCYON_
, JSObjectMakeFunctionWithCallback(context
, toCYON_
, &Array_callAsFunction_toCYON
), kJSPropertyAttributeDontEnum
);
1564 JSValueProtect(context
, Array_prototype_
);
1565 JSValueProtect(context
, Array_pop_
);
1566 JSValueProtect(context
, Array_push_
);
1567 JSValueProtect(context
, Array_splice_
);
1569 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1571 Function_prototype_
= (JSObjectRef
) CYGetProperty(context
, Function_
, prototype_
);
1572 JSValueProtect(context
, Function_prototype_
);
1574 JSObjectSetPrototype(context
, (JSObjectRef
) CYGetProperty(context
, Functor
, prototype_
), Function_prototype_
);
1576 CYSetProperty(context
, global
, CYJSString("Functor"), Functor
);
1577 CYSetProperty(context
, global
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1578 CYSetProperty(context
, global
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1580 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1581 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1582 CYSetProperty(context
, cycript
, CYJSString("gc"), JSObjectMakeFunctionWithCallback(context
, CYJSString("gc"), &Cycript_gc_callAsFunction
));
1584 CYSetProperty(context
, global
, CYJSString("$cyq"), JSObjectMakeFunctionWithCallback(context
, CYJSString("$cyq"), &$cyq
));
1586 System_
= JSObjectMake(context
, NULL
, NULL
);
1587 JSValueProtect(context
, System_
);
1589 CYSetProperty(context
, global
, CYJSString("system"), System_
);
1590 CYSetProperty(context
, System_
, CYJSString("args"), CYJSNull(context
));
1591 //CYSetProperty(context, System_, CYJSString("global"), global);
1593 CYSetProperty(context
, System_
, CYJSString("print"), JSObjectMakeFunctionWithCallback(context
, CYJSString("print"), &System_print
));
1595 Result_
= JSStringCreateWithUTF8CString("_");
1597 if (hooks_
!= NULL
&& hooks_
->SetupContext
!= NULL
)
1598 (*hooks_
->SetupContext
)(context
);