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 static CYUTF8String
CYPoolUTF8String(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
144 _assert(pool
!= NULL
);
146 CYUTF16String
utf16(CYCastUTF16String(value
));
147 const char *in(reinterpret_cast<const char *>(utf16
.data
));
149 iconv_t
conversion(_syscall(iconv_open("UTF-8", "UCS-2-INTERNAL")));
151 size_t size(JSStringGetMaximumUTF8CStringSize(value
));
152 char *out(new(pool
) char[size
]);
153 CYUTF8String
utf8(out
, size
);
155 size
= utf16
.size
* 2;
156 _syscall(iconv(conversion
, const_cast<char **>(&in
), &size
, &out
, &utf8
.size
));
159 utf8
.size
= out
- utf8
.data
;
161 _syscall(iconv_close(conversion
));
166 const char *CYPoolCString(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
167 CYUTF8String
utf8(CYPoolUTF8String(pool
, context
, value
));
168 _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
);
172 const char *CYPoolCString(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
) {
173 return JSValueIsNull(context
, value
) ? NULL
: CYPoolCString(pool
, context
, CYJSString(context
, value
));
177 /* Index Offsets {{{ */
178 size_t CYGetIndex(const CYUTF8String
&value
) {
179 if (value
.data
[0] != '0') {
181 size_t index(strtoul(value
.data
, &end
, 10));
182 if (value
.data
+ value
.size
== end
)
184 } else if (value
.data
[1] == '\0')
189 size_t CYGetIndex(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
190 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
193 bool CYGetOffset(const char *value
, ssize_t
&index
) {
194 if (value
[0] != '0') {
196 index
= strtol(value
, &end
, 10);
197 if (value
+ strlen(value
) == end
)
199 } else if (value
[1] == '\0') {
208 /* JavaScript *ify {{{ */
209 void CYStringify(std::ostringstream
&str
, const char *data
, size_t size
) {
210 unsigned quot(0), apos(0);
211 for (const char *value(data
), *end(data
+ size
); value
!= end
; ++value
)
214 else if (*value
== '\'')
217 bool single(quot
> apos
);
219 str
<< (single
? '\'' : '"');
221 for (const char *value(data
), *end(data
+ size
); value
!= end
; ++value
)
223 case '\\': str
<< "\\\\"; break;
224 case '\b': str
<< "\\b"; break;
225 case '\f': str
<< "\\f"; break;
226 case '\n': str
<< "\\n"; break;
227 case '\r': str
<< "\\r"; break;
228 case '\t': str
<< "\\t"; break;
229 case '\v': str
<< "\\v"; break;
244 if (*value
< 0x20 || *value
>= 0x7f)
245 str
<< "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value
);
250 str
<< (single
? '\'' : '"');
253 void CYNumerify(std::ostringstream
&str
, double value
) {
255 // XXX: I want this to print 1e3 rather than 1000
256 sprintf(string
, "%.17g", value
);
260 bool CYIsKey(CYUTF8String value
) {
261 const char *data(value
.data
);
262 size_t size(value
.size
);
267 if (DigitRange_
[data
[0]]) {
268 size_t index(CYGetIndex(value
));
269 if (index
== _not(size_t))
272 if (!WordStartRange_
[data
[0]])
274 for (size_t i(1); i
!= size
; ++i
)
275 if (!WordEndRange_
[data
[i
]])
283 static JSGlobalContextRef Context_
;
284 static JSObjectRef System_
;
286 static JSClassRef Functor_
;
287 static JSClassRef Pointer_
;
288 static JSClassRef Runtime_
;
289 static JSClassRef Struct_
;
291 static JSStringRef Result_
;
295 JSObjectRef Function_
;
299 JSStringRef message_
;
301 JSStringRef prototype_
;
305 JSObjectRef Object_prototype_
;
306 JSObjectRef Function_prototype_
;
308 JSObjectRef Array_prototype_
;
309 JSObjectRef Array_pop_
;
310 JSObjectRef Array_push_
;
311 JSObjectRef Array_splice_
;
315 void CYFinalize(JSObjectRef object
) {
316 delete reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
));
319 struct CStringMapLess
:
320 std::binary_function
<const char *, const char *, bool>
322 _finline
bool operator ()(const char *lhs
, const char *rhs
) const {
323 return strcmp(lhs
, rhs
) < 0;
327 void Structor_(apr_pool_t
*pool
, const char *name
, const char *types
, sig::Type
*&type
) {
331 sqlite3_stmt
*statement
;
333 _sqlcall(sqlite3_prepare(Bridge_
,
335 "\"bridge\".\"mode\", "
336 "\"bridge\".\"value\" "
339 " \"bridge\".\"mode\" in (3, 4) and"
340 " \"bridge\".\"name\" = ?"
342 , -1, &statement
, NULL
));
344 _sqlcall(sqlite3_bind_text(statement
, 1, name
, -1, SQLITE_STATIC
));
349 if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
) {
353 mode
= sqlite3_column_int(statement
, 0);
354 value
= sqlite3_column_pooled(pool
, statement
, 1);
357 _sqlcall(sqlite3_finalize(statement
));
366 sig::Parse(pool
, &type
->data
.signature
, value
, &Structor_
);
370 sig::Signature signature
;
371 sig::Parse(pool
, &signature
, value
, &Structor_
);
372 type
= signature
.elements
[0].type
;
377 JSClassRef
Type_privateData::Class_
;
382 Type_privateData
*type_
;
384 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, sig::Type
*type
) :
385 CYOwned(value
, context
, owner
),
386 type_(new(pool_
) Type_privateData(type
))
391 struct Struct_privateData
:
394 Type_privateData
*type_
;
396 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
397 CYOwned(NULL
, context
, owner
)
402 typedef std::map
<const char *, Type_privateData
*, CStringMapLess
> TypeMap
;
403 static TypeMap Types_
;
405 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
406 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
407 apr_pool_t
*pool(internal
->pool_
);
408 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
409 internal
->type_
= typical
;
412 internal
->value_
= data
;
414 size_t size(typical
->GetFFI()->size
);
415 void *copy(apr_palloc(internal
->pool_
, size
));
416 memcpy(copy
, data
, size
);
417 internal
->value_
= copy
;
420 return JSObjectMake(context
, Struct_
, internal
);
423 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
424 return JSValueMakeBoolean(context
, value
);
427 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
428 return JSValueMakeNumber(context
, value
);
431 #define CYCastJSValue_(Type_) \
432 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
433 return JSValueMakeNumber(context, static_cast<double>(value)); \
437 CYCastJSValue_(unsigned int)
438 CYCastJSValue_(long int)
439 CYCastJSValue_(long unsigned int)
440 CYCastJSValue_(long long int)
441 CYCastJSValue_(long long unsigned int)
443 JSValueRef
CYJSUndefined(JSContextRef context
) {
444 return JSValueMakeUndefined(context
);
447 double CYCastDouble(const char *value
, size_t size
) {
449 double number(strtod(value
, &end
));
450 if (end
!= value
+ size
)
455 double CYCastDouble(const char *value
) {
456 return CYCastDouble(value
, strlen(value
));
459 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
460 JSValueRef
exception(NULL
);
461 double number(JSValueToNumber(context
, value
, &exception
));
462 CYThrow(context
, exception
);
466 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
467 return JSValueToBoolean(context
, value
);
470 JSValueRef
CYJSNull(JSContextRef context
) {
471 return JSValueMakeNull(context
);
474 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
475 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
478 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
479 return CYCastJSValue(context
, CYJSString(value
));
482 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
483 JSValueRef
exception(NULL
);
484 JSObjectRef
object(JSValueToObject(context
, value
, &exception
));
485 CYThrow(context
, exception
);
489 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, JSValueRef arguments
[]) {
490 JSValueRef
exception(NULL
);
491 JSValueRef
value(JSObjectCallAsFunction(context
, function
, _this
, count
, arguments
, &exception
));
492 CYThrow(context
, exception
);
496 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
497 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
500 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
505 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
508 return CYJSUndefined(context
);
511 static size_t Nonce_(0);
513 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
515 const char *name(apr_psprintf(pool
, "%s%zu", CYPoolCString(pool
, context
, arguments
[0]), Nonce_
++));
516 return CYCastJSValue(context
, name
);
519 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
520 JSGarbageCollect(context
);
521 return CYJSUndefined(context
);
524 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
525 switch (JSType type
= JSValueGetType(context
, value
)) {
526 case kJSTypeUndefined
:
531 return CYCastBool(context
, value
) ? "true" : "false";
533 case kJSTypeNumber
: {
534 std::ostringstream str
;
535 CYNumerify(str
, CYCastDouble(context
, value
));
536 std::string
value(str
.str());
537 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
540 case kJSTypeString
: {
541 std::ostringstream str
;
542 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
543 CYStringify(str
, string
.data
, string
.size
);
544 std::string
value(str
.str());
545 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
549 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
551 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
555 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
) {
556 JSValueRef
exception(NULL
);
557 const char *cyon(CYPoolCCYON(pool
, context
, value
, &exception
));
558 CYThrow(context
, exception
);
562 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSObjectRef object
) {
563 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_
));
564 if (CYIsCallable(context
, toCYON
)) {
565 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
566 return CYPoolCString(pool
, context
, value
);
569 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_
));
570 if (CYIsCallable(context
, toJSON
)) {
571 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
572 JSValueRef
exception(NULL
);
573 const char *cyon(CYPoolCCYON(pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), &exception
));
574 CYThrow(context
, exception
);
578 std::ostringstream str
;
582 // XXX: this is, sadly, going to leak
583 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
587 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
588 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
589 JSValueRef
value(CYGetProperty(context
, object
, name
));
596 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
600 CYStringify(str
, string
.data
, string
.size
);
602 str
<< ':' << CYPoolCCYON(pool
, context
, value
);
607 JSPropertyNameArrayRelease(names
);
609 std::string
string(str
.str());
610 return apr_pstrmemdup(pool
, string
.c_str(), string
.size());
613 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
615 std::ostringstream str
;
619 JSValueRef
length(CYGetProperty(context
, _this
, length_
));
622 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
623 JSValueRef
value(CYGetProperty(context
, _this
, index
));
630 if (!JSValueIsUndefined(context
, value
))
631 str
<< CYPoolCCYON(pool
, context
, value
);
640 std::string
value(str
.str());
641 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
644 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
645 Pointer
*internal(new Pointer(pointer
, context
, owner
, type
));
646 return JSObjectMake(context
, Pointer_
, internal
);
649 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const char *type
) {
650 cy::Functor
*internal(new cy::Functor(type
, function
));
651 return JSObjectMake(context
, Functor_
, internal
);
654 static bool CYGetOffset(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
655 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
658 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
659 switch (JSValueGetType(context
, value
)) {
662 /*case kJSTypeObject:
663 if (JSValueIsObjectOfClass(context, value, Pointer_)) {
664 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value)));
665 return internal->value_;
668 double number(CYCastDouble(context
, value
));
669 if (std::isnan(number
))
670 throw CYJSError(context
, "cannot convert value to pointer");
671 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
675 void CYPoolFFI(apr_pool_t
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
676 switch (type
->primitive
) {
678 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
681 #define CYPoolFFI_(primitive, native) \
682 case sig::primitive ## _P: \
683 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
686 CYPoolFFI_(uchar
, unsigned char)
687 CYPoolFFI_(char, char)
688 CYPoolFFI_(ushort
, unsigned short)
689 CYPoolFFI_(short, short)
690 CYPoolFFI_(ulong
, unsigned long)
691 CYPoolFFI_(long, long)
692 CYPoolFFI_(uint
, unsigned int)
694 CYPoolFFI_(ulonglong
, unsigned long long)
695 CYPoolFFI_(longlong
, long long)
696 CYPoolFFI_(float, float)
697 CYPoolFFI_(double, double)
700 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
704 *reinterpret_cast<const char **>(data
) = CYPoolCString(pool
, context
, value
);
707 case sig::struct_P
: {
708 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
709 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
710 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
711 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
712 ffi_type
*field(ffi
->elements
[index
]);
715 if (aggregate
== NULL
)
718 rhs
= CYGetProperty(context
, aggregate
, index
);
719 if (JSValueIsUndefined(context
, rhs
)) {
720 if (element
->name
!= NULL
)
721 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
724 if (JSValueIsUndefined(context
, rhs
)) undefined
:
725 throw CYJSError(context
, "unable to extract structure value");
729 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
739 if (hooks_
!= NULL
&& hooks_
->PoolFFI
!= NULL
)
740 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
743 fprintf(stderr
, "CYPoolFFI(%c)\n", type
->primitive
);
748 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
749 switch (type
->primitive
) {
751 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
753 #define CYFromFFI_(primitive, native) \
754 case sig::primitive ## _P: \
755 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
757 CYFromFFI_(uchar, unsigned char)
758 CYFromFFI_(char, char)
759 CYFromFFI_(ushort
, unsigned short)
760 CYFromFFI_(short, short)
761 CYFromFFI_(ulong
, unsigned long)
762 CYFromFFI_(long, long)
763 CYFromFFI_(uint
, unsigned int)
765 CYFromFFI_(ulonglong
, unsigned long long)
766 CYFromFFI_(longlong
, long long)
767 CYFromFFI_(float, float)
768 CYFromFFI_(double, double)
771 if (void *pointer
= *reinterpret_cast<void **>(data
))
772 return CYMakePointer(context
, pointer
, type
->data
.data
.type
, ffi
, owner
);
776 if (char *utf8
= *reinterpret_cast<char **>(data
))
777 return CYCastJSValue(context
, utf8
);
781 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
783 return CYJSUndefined(context
);
786 return CYJSNull(context
);
788 if (hooks_
!= NULL
&& hooks_
->FromFFI
!= NULL
)
789 if (JSValueRef value
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
792 fprintf(stderr
, "CYFromFFI(%c)\n", type
->primitive
);
797 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
798 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
800 JSContextRef
context(internal
->context_
);
802 size_t count(internal
->cif_
.nargs
);
803 JSValueRef values
[count
];
805 for (size_t index(0); index
!= count
; ++index
)
806 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
808 JSValueRef
value(CYCallAsFunction(context
, internal
->function_
, NULL
, count
, values
));
809 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
812 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const char *type
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
813 // XXX: in case of exceptions this will leak
814 // XXX: in point of fact, this may /need/ to leak :(
815 Closure_privateData
*internal(new Closure_privateData(CYGetJSContext(), function
, type
));
817 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
818 NULL
, sizeof(ffi_closure
),
819 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
823 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
824 _assert(status
== FFI_OK
);
826 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
828 internal
->value_
= closure
;
833 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const char *type
) {
834 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, type
, &FunctionClosure_
));
835 return JSObjectMake(context
, Functor_
, internal
);
838 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const char *type
) {
839 JSValueRef
exception(NULL
);
840 bool function(JSValueIsInstanceOfConstructor(context
, value
, Function_
, &exception
));
841 CYThrow(context
, exception
);
844 JSObjectRef
function(CYCastJSObject(context
, value
));
845 return CYMakeFunctor(context
, function
, type
);
847 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
848 return CYMakeFunctor(context
, function
, type
);
852 static bool Index_(apr_pool_t
*pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
853 Type_privateData
*typical(internal
->type_
);
854 sig::Type
*type(typical
->type_
);
858 const char *name(CYPoolCString(pool
, context
, property
));
859 size_t length(strlen(name
));
860 double number(CYCastDouble(name
, length
));
862 size_t count(type
->data
.signature
.count
);
864 if (std::isnan(number
)) {
865 if (property
== NULL
)
868 sig::Element
*elements(type
->data
.signature
.elements
);
870 for (size_t local(0); local
!= count
; ++local
) {
871 sig::Element
*element(&elements
[local
]);
872 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
880 index
= static_cast<ssize_t
>(number
);
881 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
886 ffi_type
**elements(typical
->GetFFI()->elements
);
888 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
889 for (ssize_t
local(0); local
!= index
; ++local
)
890 base
+= elements
[local
]->size
;
895 static JSValueRef
Pointer_getIndex(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef
*exception
) { CYTry
{
896 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
897 Type_privateData
*typical(internal
->type_
);
899 ffi_type
*ffi(typical
->GetFFI());
901 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
902 base
+= ffi
->size
* index
;
904 JSObjectRef
owner(internal
->GetOwner() ?: object
);
905 return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
);
908 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) {
910 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
911 Type_privateData
*typical(internal
->type_
);
913 if (typical
->type_
== NULL
)
917 if (!CYGetOffset(pool
, context
, property
, offset
))
920 return Pointer_getIndex(context
, object
, offset
, exception
);
923 static JSValueRef Pointer_getProperty_$
cyi(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) {
924 return Pointer_getIndex(context
, object
, 0, exception
);
927 static bool Pointer_setIndex(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
928 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
929 Type_privateData
*typical(internal
->type_
);
931 ffi_type
*ffi(typical
->GetFFI());
933 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
934 base
+= ffi
->size
* index
;
936 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
940 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) {
942 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
943 Type_privateData
*typical(internal
->type_
);
945 if (typical
->type_
== NULL
)
949 if (!CYGetOffset(pool
, context
, property
, offset
))
952 return Pointer_setIndex(context
, object
, offset
, value
, exception
);
955 static bool Pointer_setProperty_$
cyi(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) {
956 return Pointer_setIndex(context
, object
, 0, value
, exception
);
959 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
960 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
961 Type_privateData
*typical(internal
->type_
);
962 return CYMakePointer(context
, internal
->value_
, typical
->type_
, typical
->ffi_
, _this
);
965 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
967 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
968 Type_privateData
*typical(internal
->type_
);
973 if (!Index_(pool
, context
, internal
, property
, index
, base
))
976 JSObjectRef
owner(internal
->GetOwner() ?: object
);
978 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
981 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
983 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
984 Type_privateData
*typical(internal
->type_
);
989 if (!Index_(pool
, context
, internal
, property
, index
, base
))
992 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
996 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
997 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
998 Type_privateData
*typical(internal
->type_
);
999 sig::Type
*type(typical
->type_
);
1004 size_t count(type
->data
.signature
.count
);
1005 sig::Element
*elements(type
->data
.signature
.elements
);
1009 for (size_t index(0); index
!= count
; ++index
) {
1011 name
= elements
[index
].name
;
1014 sprintf(number
, "%lu", index
);
1018 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
1022 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
{
1023 if (setups
+ count
!= signature
->count
- 1)
1024 throw CYJSError(context
, "incorrect number of arguments to ffi function");
1026 size_t size(setups
+ count
);
1028 memcpy(values
, setup
, sizeof(void *) * setups
);
1030 for (size_t index(setups
); index
!= size
; ++index
) {
1031 sig::Element
*element(&signature
->elements
[index
+ 1]);
1032 ffi_type
*ffi(cif
->arg_types
[index
]);
1034 values
[index
] = new(pool
) uint8_t[ffi
->size
];
1035 CYPoolFFI(pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
1038 uint8_t value
[cif
->rtype
->size
];
1039 ffi_call(cif
, function
, value
, values
);
1041 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
1044 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1046 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
1047 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, exception
, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
1050 static JSObjectRef
CYMakeType(JSContextRef context
, const char *type
) {
1051 Type_privateData
*internal(new Type_privateData(NULL
, type
));
1052 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1055 static JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
1056 Type_privateData
*internal(new Type_privateData(type
));
1057 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
1060 static void *CYCastSymbol(const char *name
) {
1061 return dlsym(RTLD_DEFAULT
, name
);
1064 static JSValueRef
Runtime_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1066 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
1068 if (hooks_
!= NULL
&& hooks_
->RuntimeProperty
!= NULL
)
1069 if (JSValueRef value
= (*hooks_
->RuntimeProperty
)(context
, name
))
1072 sqlite3_stmt
*statement
;
1074 _sqlcall(sqlite3_prepare(Bridge_
,
1076 "\"bridge\".\"mode\", "
1077 "\"bridge\".\"value\" "
1080 " \"bridge\".\"name\" = ?"
1082 , -1, &statement
, NULL
));
1084 _sqlcall(sqlite3_bind_text(statement
, 1, name
.data
, name
.size
, SQLITE_STATIC
));
1089 if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
) {
1093 mode
= sqlite3_column_int(statement
, 0);
1094 value
= sqlite3_column_pooled(pool
, statement
, 1);
1097 _sqlcall(sqlite3_finalize(statement
));
1106 return JSEvaluateScript(CYGetJSContext(), CYJSString(value
), NULL
, NULL
, 0, NULL
);
1108 return CYMakeFunctor(context
, reinterpret_cast<void (*)()>(CYCastSymbol(name
.data
)), value
);
1111 // XXX: this is horrendously inefficient
1112 sig::Signature signature
;
1113 sig::Parse(pool
, &signature
, value
, &Structor_
);
1115 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1116 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, CYCastSymbol(name
.data
));
1119 // XXX: implement case 3
1121 return CYMakeType(context
, value
);
1125 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1127 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1131 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1132 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1134 sig::Signature signature
;
1135 sig::Parse(pool
, &signature
, type
, &Structor_
);
1137 return CYMakePointer(context
, value
, signature
.elements
[0].type
, NULL
, NULL
);
1140 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1142 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1144 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1145 return CYMakeType(context
, type
);
1148 static JSValueRef
Type_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1149 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1153 if (JSStringIsEqualToUTF8CString(property
, "$cyi")) {
1154 type
.primitive
= sig::pointer_P
;
1155 type
.data
.data
.size
= 0;
1158 size_t index(CYGetIndex(pool
, context
, property
));
1159 if (index
== _not(size_t))
1161 type
.primitive
= sig::array_P
;
1162 type
.data
.data
.size
= index
;
1168 type
.data
.data
.type
= internal
->type_
;
1170 return CYMakeType(context
, &type
);
1173 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1174 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1177 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1178 sig::Type
*type(internal
->type_
);
1179 ffi_type
*ffi(internal
->GetFFI());
1181 uint8_t value
[ffi
->size
];
1183 CYPoolFFI(pool
, context
, type
, ffi
, value
, arguments
[0]);
1184 return CYFromFFI(context
, type
, ffi
, value
);
1187 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1189 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1190 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1192 sig::Type
*type(internal
->type_
);
1195 if (type
->primitive
!= sig::array_P
)
1198 size
= type
->data
.data
.size
;
1199 type
= type
->data
.data
.type
;
1202 void *value(malloc(internal
->GetFFI()->size
));
1203 return CYMakePointer(context
, value
, type
, NULL
, NULL
);
1206 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1208 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1210 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1211 return CYMakeFunctor(context
, arguments
[0], type
);
1214 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1215 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1216 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1219 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1220 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1223 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1224 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1226 sprintf(string
, "%p", internal
->value_
);
1228 return CYCastJSValue(context
, string
);
1231 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1232 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1234 const char *type(sig::Unparse(pool
, internal
->type_
));
1235 return CYCastJSValue(context
, CYJSString(type
));
1238 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1239 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1241 const char *type(sig::Unparse(pool
, internal
->type_
));
1242 size_t size(strlen(type
));
1243 char *cyon(new(pool
) char[12 + size
+ 1]);
1244 memcpy(cyon
, "new Type(\"", 10);
1245 cyon
[12 + size
] = '\0';
1246 cyon
[12 + size
- 2] = '"';
1247 cyon
[12 + size
- 1] = ')';
1248 memcpy(cyon
+ 10, type
, size
);
1249 return CYCastJSValue(context
, CYJSString(cyon
));
1252 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1253 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1256 static JSStaticValue Pointer_staticValues
[2] = {
1257 {"$cyi", &Pointer_getProperty_$cyi
, &Pointer_setProperty_$cyi
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1258 {NULL
, NULL
, NULL
, 0}
1261 static JSStaticFunction Pointer_staticFunctions
[4] = {
1262 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1263 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1264 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1268 static JSStaticFunction Struct_staticFunctions
[2] = {
1269 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1273 static JSStaticFunction Functor_staticFunctions
[4] = {
1274 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1275 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1276 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1281 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1284 static JSStaticFunction Type_staticFunctions
[4] = {
1285 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1286 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1287 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1291 void CYSetArgs(int argc
, const char *argv
[]) {
1292 JSContextRef
context(CYGetJSContext());
1293 JSValueRef args
[argc
];
1294 for (int i(0); i
!= argc
; ++i
)
1295 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1296 JSValueRef
exception(NULL
);
1297 JSObjectRef
array(JSObjectMakeArray(context
, argc
, args
, &exception
));
1298 CYThrow(context
, exception
);
1299 CYSetProperty(context
, System_
, CYJSString("args"), array
);
1302 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1303 return JSContextGetGlobalObject(context
);
1306 const char *CYExecute(apr_pool_t
*pool
, const char *code
) {
1307 JSContextRef
context(CYGetJSContext());
1308 JSValueRef
exception(NULL
), result
;
1311 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1312 handle
= (*hooks_
->ExecuteStart
)();
1319 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1320 } catch (const char *error
) {
1324 if (exception
!= NULL
) { error
:
1329 if (JSValueIsUndefined(context
, result
))
1333 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1334 } catch (const char *error
) {
1338 if (exception
!= NULL
)
1341 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1343 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1344 (*hooks_
->ExecuteEnd
)(handle
);
1348 static apr_pool_t
*Pool_
;
1350 static bool initialized_
;
1352 void CYInitialize() {
1354 initialized_
= true;
1357 _aprcall(apr_initialize());
1358 _aprcall(apr_pool_create(&Pool_
, NULL
));
1359 _sqlcall(sqlite3_open("/usr/lib/libcycript.db", &Bridge_
));
1362 apr_pool_t
*CYGetGlobalPool() {
1367 void CYThrow(JSContextRef context
, JSValueRef value
) {
1369 throw CYJSError(context
, value
);
1372 const char *CYJSError::PoolCString(apr_pool_t
*pool
) const {
1373 return CYPoolCString(pool
, context_
, value_
);
1376 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1377 // XXX: what if the context is different?
1381 void CYThrow(const char *format
, ...) {
1383 va_start (args
, format
);
1384 throw CYPoolError(format
, args
);
1385 // XXX: does this matter? :(
1389 const char *CYPoolError::PoolCString(apr_pool_t
*pool
) const {
1390 return apr_pstrdup(pool
, message_
);
1393 CYPoolError::CYPoolError(const char *format
, ...) {
1395 va_start (args
, format
);
1396 message_
= apr_pvsprintf(pool_
, format
, args
);
1400 CYPoolError::CYPoolError(const char *format
, va_list args
) {
1401 message_
= apr_pvsprintf(pool_
, format
, args
);
1404 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1405 return CYCastJSValue(context
, message_
);
1408 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1409 if (context
== NULL
)
1410 context
= CYGetJSContext();
1415 va_start (args
, format
);
1416 const char *message(apr_pvsprintf(pool
, format
, args
));
1419 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(message
))};
1421 JSValueRef
exception(NULL
);
1422 value_
= JSObjectCallAsConstructor(context
, Error_
, 1, arguments
, &exception
);
1423 CYThrow(context
, exception
);
1426 void CYObjectiveC(JSContextRef context
, JSObjectRef global
);
1428 JSGlobalContextRef
CYGetJSContext() {
1431 if (Context_
== NULL
) {
1432 JSClassDefinition definition
;
1434 definition
= kJSClassDefinitionEmpty
;
1435 definition
.className
= "Functor";
1436 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1437 definition
.callAsFunction
= &Functor_callAsFunction
;
1438 definition
.finalize
= &CYFinalize
;
1439 Functor_
= JSClassCreate(&definition
);
1441 definition
= kJSClassDefinitionEmpty
;
1442 definition
.className
= "Pointer";
1443 definition
.staticValues
= Pointer_staticValues
;
1444 definition
.staticFunctions
= Pointer_staticFunctions
;
1445 definition
.getProperty
= &Pointer_getProperty
;
1446 definition
.setProperty
= &Pointer_setProperty
;
1447 definition
.finalize
= &CYFinalize
;
1448 Pointer_
= JSClassCreate(&definition
);
1450 definition
= kJSClassDefinitionEmpty
;
1451 definition
.className
= "Struct";
1452 definition
.staticFunctions
= Struct_staticFunctions
;
1453 definition
.getProperty
= &Struct_getProperty
;
1454 definition
.setProperty
= &Struct_setProperty
;
1455 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1456 definition
.finalize
= &CYFinalize
;
1457 Struct_
= JSClassCreate(&definition
);
1459 definition
= kJSClassDefinitionEmpty
;
1460 definition
.className
= "Type";
1461 definition
.staticFunctions
= Type_staticFunctions
;
1462 definition
.getProperty
= &Type_getProperty
;
1463 definition
.callAsFunction
= &Type_callAsFunction
;
1464 definition
.callAsConstructor
= &Type_callAsConstructor
;
1465 definition
.finalize
= &CYFinalize
;
1466 Type_privateData::Class_
= JSClassCreate(&definition
);
1468 definition
= kJSClassDefinitionEmpty
;
1469 definition
.className
= "Runtime";
1470 definition
.getProperty
= &Runtime_getProperty
;
1471 Runtime_
= JSClassCreate(&definition
);
1473 definition
= kJSClassDefinitionEmpty
;
1474 //definition.getProperty = &Global_getProperty;
1475 JSClassRef
Global(JSClassCreate(&definition
));
1477 JSGlobalContextRef
context(JSGlobalContextCreate(Global
));
1479 JSObjectRef
global(CYGetGlobalObject(context
));
1481 JSObjectSetPrototype(context
, global
, JSObjectMake(context
, Runtime_
, NULL
));
1483 Array_
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array")));
1484 JSValueProtect(context
, Array_
);
1486 Error_
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error")));
1487 JSValueProtect(context
, Error_
);
1489 Function_
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function")));
1490 JSValueProtect(context
, Function_
);
1492 String_
= CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String")));
1493 JSValueProtect(context
, String_
);
1495 length_
= JSStringCreateWithUTF8CString("length");
1496 message_
= JSStringCreateWithUTF8CString("message");
1497 name_
= JSStringCreateWithUTF8CString("name");
1498 prototype_
= JSStringCreateWithUTF8CString("prototype");
1499 toCYON_
= JSStringCreateWithUTF8CString("toCYON");
1500 toJSON_
= JSStringCreateWithUTF8CString("toJSON");
1502 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1503 Object_prototype_
= CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_
));
1504 JSValueProtect(context
, Object_prototype_
);
1506 Array_prototype_
= CYCastJSObject(context
, CYGetProperty(context
, Array_
, prototype_
));
1507 Array_pop_
= CYCastJSObject(context
, CYGetProperty(context
, Array_prototype_
, CYJSString("pop")));
1508 Array_push_
= CYCastJSObject(context
, CYGetProperty(context
, Array_prototype_
, CYJSString("push")));
1509 Array_splice_
= CYCastJSObject(context
, CYGetProperty(context
, Array_prototype_
, CYJSString("splice")));
1511 CYSetProperty(context
, Array_prototype_
, toCYON_
, JSObjectMakeFunctionWithCallback(context
, toCYON_
, &Array_callAsFunction_toCYON
), kJSPropertyAttributeDontEnum
);
1513 JSValueProtect(context
, Array_prototype_
);
1514 JSValueProtect(context
, Array_pop_
);
1515 JSValueProtect(context
, Array_push_
);
1516 JSValueProtect(context
, Array_splice_
);
1518 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1520 Function_prototype_
= (JSObjectRef
) CYGetProperty(context
, Function_
, prototype_
);
1521 JSValueProtect(context
, Function_prototype_
);
1523 JSObjectSetPrototype(context
, (JSObjectRef
) CYGetProperty(context
, Functor
, prototype_
), Function_prototype_
);
1525 CYSetProperty(context
, global
, CYJSString("Functor"), Functor
);
1526 CYSetProperty(context
, global
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1527 CYSetProperty(context
, global
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1529 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1530 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1531 CYSetProperty(context
, cycript
, CYJSString("gc"), JSObjectMakeFunctionWithCallback(context
, CYJSString("gc"), &Cycript_gc_callAsFunction
));
1533 CYSetProperty(context
, global
, CYJSString("$cyq"), JSObjectMakeFunctionWithCallback(context
, CYJSString("$cyq"), &$cyq
));
1535 System_
= JSObjectMake(context
, NULL
, NULL
);
1536 JSValueProtect(context
, System_
);
1538 CYSetProperty(context
, global
, CYJSString("system"), System_
);
1539 CYSetProperty(context
, System_
, CYJSString("args"), CYJSNull(context
));
1540 //CYSetProperty(context, System_, CYJSString("global"), global);
1542 CYSetProperty(context
, System_
, CYJSString("print"), JSObjectMakeFunctionWithCallback(context
, CYJSString("print"), &System_print
));
1544 Result_
= JSStringCreateWithUTF8CString("_");
1546 CYObjectiveC(context
, global
);