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"
71 char *sqlite3_column_pooled(apr_pool_t
*pool
, sqlite3_stmt
*stmt
, int n
) {
72 if (const unsigned char *value
= sqlite3_column_text(stmt
, n
))
73 return apr_pstrdup(pool
, (const char *) value
);
77 struct CYHooks
*hooks_
;
79 /* JavaScript Properties {{{ */
80 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) {
81 JSValueRef
exception(NULL
);
82 JSValueRef
value(JSObjectGetPropertyAtIndex(context
, object
, index
, &exception
));
83 CYThrow(context
, exception
);
87 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
88 JSValueRef
exception(NULL
);
89 JSValueRef
value(JSObjectGetProperty(context
, object
, name
, &exception
));
90 CYThrow(context
, exception
);
94 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) {
95 JSValueRef
exception(NULL
);
96 JSObjectSetPropertyAtIndex(context
, object
, index
, value
, &exception
);
97 CYThrow(context
, exception
);
100 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) {
101 JSValueRef
exception(NULL
);
102 JSObjectSetProperty(context
, object
, name
, value
, attributes
, &exception
);
103 CYThrow(context
, exception
);
106 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef (*callback
)(JSContextRef
, JSObjectRef
, JSObjectRef
, size_t, const JSValueRef
[], JSValueRef
*), JSPropertyAttributes attributes
) {
107 CYSetProperty(context
, object
, name
, JSObjectMakeFunctionWithCallback(context
, name
, callback
), attributes
);
110 /* JavaScript Strings {{{ */
111 JSStringRef
CYCopyJSString(const char *value
) {
112 return value
== NULL
? NULL
: JSStringCreateWithUTF8CString(value
);
115 JSStringRef
CYCopyJSString(JSStringRef value
) {
116 return value
== NULL
? NULL
: JSStringRetain(value
);
119 JSStringRef
CYCopyJSString(CYUTF8String value
) {
120 // XXX: this is very wrong; it needs to convert to UTF16 and then create from there
121 return CYCopyJSString(value
.data
);
124 JSStringRef
CYCopyJSString(JSContextRef context
, JSValueRef value
) {
125 if (JSValueIsNull(context
, value
))
127 JSValueRef
exception(NULL
);
128 JSStringRef
string(JSValueToStringCopy(context
, value
, &exception
));
129 CYThrow(context
, exception
);
133 static CYUTF16String
CYCastUTF16String(JSStringRef value
) {
134 return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
));
137 CYUTF8String
CYPoolUTF8String(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
138 return CYPoolUTF8String(pool
, CYCastUTF16String(value
));
141 const char *CYPoolCString(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
142 CYUTF8String
utf8(CYPoolUTF8String(pool
, context
, value
));
143 _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
);
147 const char *CYPoolCString(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
) {
148 return JSValueIsNull(context
, value
) ? NULL
: CYPoolCString(pool
, context
, CYJSString(context
, value
));
151 /* Index Offsets {{{ */
152 size_t CYGetIndex(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
153 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
157 static JSClassRef All_
;
158 static JSClassRef Context_
;
159 static JSClassRef Functor_
;
160 static JSClassRef Global_
;
161 static JSClassRef Pointer_
;
162 static JSClassRef Struct_
;
166 JSStringRef length_s
;
167 JSStringRef message_s
;
170 JSStringRef prototype_s
;
172 JSStringRef splice_s
;
173 JSStringRef toCYON_s
;
174 JSStringRef toJSON_s
;
176 static JSStringRef Result_
;
180 void CYFinalize(JSObjectRef object
) {
181 delete reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
));
184 struct CStringMapLess
:
185 std::binary_function
<const char *, const char *, bool>
187 _finline
bool operator ()(const char *lhs
, const char *rhs
) const {
188 return strcmp(lhs
, rhs
) < 0;
192 void Structor_(apr_pool_t
*pool
, sig::Type
*&type
) {
194 type
->primitive
== sig::pointer_P
&&
195 type
->data
.data
.type
!= NULL
&&
196 type
->data
.data
.type
->primitive
== sig::struct_P
&&
197 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
199 type
->primitive
= sig::typename_P
;
200 type
->data
.data
.type
= NULL
;
204 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
207 sqlite3_stmt
*statement
;
209 _sqlcall(sqlite3_prepare(Bridge_
,
211 "\"bridge\".\"mode\", "
212 "\"bridge\".\"value\" "
215 " \"bridge\".\"mode\" in (3, 4) and"
216 " \"bridge\".\"name\" = ?"
218 , -1, &statement
, NULL
));
220 _sqlcall(sqlite3_bind_text(statement
, 1, type
->name
, -1, SQLITE_STATIC
));
225 if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
) {
229 mode
= sqlite3_column_int(statement
, 0);
230 value
= sqlite3_column_pooled(pool
, statement
, 1);
233 _sqlcall(sqlite3_finalize(statement
));
242 sig::Parse(pool
, &type
->data
.signature
, value
, &Structor_
);
246 sig::Signature signature
;
247 sig::Parse(pool
, &signature
, value
, &Structor_
);
248 type
= signature
.elements
[0].type
;
253 JSClassRef
Type_privateData::Class_
;
258 JSGlobalContextRef context_
;
260 Context(JSGlobalContextRef context
) :
269 Type_privateData
*type_
;
272 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type
*type
) :
273 CYOwned(value
, context
, owner
),
274 type_(new(pool_
) Type_privateData(type
)),
280 struct Struct_privateData
:
283 Type_privateData
*type_
;
285 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
286 CYOwned(NULL
, context
, owner
)
291 typedef std::map
<const char *, Type_privateData
*, CStringMapLess
> TypeMap
;
292 static TypeMap Types_
;
294 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
295 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
296 apr_pool_t
*pool(internal
->pool_
);
297 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
298 internal
->type_
= typical
;
301 internal
->value_
= data
;
303 size_t size(typical
->GetFFI()->size
);
304 void *copy(apr_palloc(internal
->pool_
, size
));
305 memcpy(copy
, data
, size
);
306 internal
->value_
= copy
;
309 return JSObjectMake(context
, Struct_
, internal
);
312 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
313 return JSValueMakeBoolean(context
, value
);
316 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
317 return JSValueMakeNumber(context
, value
);
320 #define CYCastJSValue_(Type_) \
321 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
322 return JSValueMakeNumber(context, static_cast<double>(value)); \
326 CYCastJSValue_(unsigned int)
327 CYCastJSValue_(long int)
328 CYCastJSValue_(long unsigned int)
329 CYCastJSValue_(long long int)
330 CYCastJSValue_(long long unsigned int)
332 JSValueRef
CYJSUndefined(JSContextRef context
) {
333 return JSValueMakeUndefined(context
);
336 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
337 JSValueRef
exception(NULL
);
338 double number(JSValueToNumber(context
, value
, &exception
));
339 CYThrow(context
, exception
);
343 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
344 return JSValueToBoolean(context
, value
);
347 JSValueRef
CYJSNull(JSContextRef context
) {
348 return JSValueMakeNull(context
);
351 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
352 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
355 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
356 return CYCastJSValue(context
, CYJSString(value
));
359 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
360 JSValueRef
exception(NULL
);
361 JSObjectRef
object(JSValueToObject(context
, value
, &exception
));
362 CYThrow(context
, exception
);
366 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
367 JSValueRef
exception(NULL
);
368 JSValueRef
value(JSObjectCallAsFunction(context
, function
, _this
, count
, arguments
, &exception
));
369 CYThrow(context
, exception
);
373 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
374 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
377 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
382 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
385 return CYJSUndefined(context
);
388 static size_t Nonce_(0);
390 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
392 const char *name(apr_psprintf(pool
, "%s%"APR_SIZE_T_FMT
"", CYPoolCString(pool
, context
, arguments
[0]), Nonce_
++));
393 return CYCastJSValue(context
, name
);
396 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
397 JSGarbageCollect(context
);
398 return CYJSUndefined(context
);
401 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
402 switch (JSType type
= JSValueGetType(context
, value
)) {
403 case kJSTypeUndefined
:
408 return CYCastBool(context
, value
) ? "true" : "false";
410 case kJSTypeNumber
: {
411 std::ostringstream str
;
412 CYNumerify(str
, CYCastDouble(context
, value
));
413 std::string
value(str
.str());
414 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
417 case kJSTypeString
: {
418 std::ostringstream str
;
419 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
420 CYStringify(str
, string
.data
, string
.size
);
421 std::string
value(str
.str());
422 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
426 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
428 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
432 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
) {
433 JSValueRef
exception(NULL
);
434 const char *cyon(CYPoolCCYON(pool
, context
, value
, &exception
));
435 CYThrow(context
, exception
);
439 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSObjectRef object
) {
440 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
441 if (CYIsCallable(context
, toCYON
)) {
442 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
443 _assert(value
!= NULL
);
444 return CYPoolCString(pool
, context
, value
);
447 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
448 if (CYIsCallable(context
, toJSON
)) {
449 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
450 JSValueRef
exception(NULL
);
451 const char *cyon(CYPoolCCYON(pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), &exception
));
452 CYThrow(context
, exception
);
456 std::ostringstream str
;
460 // XXX: this is, sadly, going to leak
461 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
465 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
466 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
467 JSValueRef
value(CYGetProperty(context
, object
, name
));
474 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
478 CYStringify(str
, string
.data
, string
.size
);
480 str
<< ':' << CYPoolCCYON(pool
, context
, value
);
485 JSPropertyNameArrayRelease(names
);
487 std::string
string(str
.str());
488 return apr_pstrmemdup(pool
, string
.c_str(), string
.size());
491 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
493 std::ostringstream str
;
497 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
500 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
501 JSValueRef
value(CYGetProperty(context
, _this
, index
));
508 if (!JSValueIsUndefined(context
, value
))
509 str
<< CYPoolCCYON(pool
, context
, value
);
518 std::string
value(str
.str());
519 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
522 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
523 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
524 return JSObjectMake(context
, Pointer_
, internal
);
527 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const char *type
) {
528 cy::Functor
*internal(new cy::Functor(type
, function
));
529 return JSObjectMake(context
, Functor_
, internal
);
532 static bool CYGetOffset(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
533 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
536 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
537 switch (JSValueGetType(context
, value
)) {
540 /*case kJSTypeObject:
541 if (JSValueIsObjectOfClass(context, value, Pointer_)) {
542 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value)));
543 return internal->value_;
546 double number(CYCastDouble(context
, value
));
547 if (std::isnan(number
))
548 throw CYJSError(context
, "cannot convert value to pointer");
549 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
553 void CYPoolFFI(apr_pool_t
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
554 switch (type
->primitive
) {
556 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
559 #define CYPoolFFI_(primitive, native) \
560 case sig::primitive ## _P: \
561 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
564 CYPoolFFI_(uchar
, unsigned char)
565 CYPoolFFI_(char, char)
566 CYPoolFFI_(ushort
, unsigned short)
567 CYPoolFFI_(short, short)
568 CYPoolFFI_(ulong
, unsigned long)
569 CYPoolFFI_(long, long)
570 CYPoolFFI_(uint
, unsigned int)
572 CYPoolFFI_(ulonglong
, unsigned long long)
573 CYPoolFFI_(longlong
, long long)
574 CYPoolFFI_(float, float)
575 CYPoolFFI_(double, double)
578 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
579 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
580 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
581 ffi_type
*field(ffi
->elements
[index
]);
584 if (aggregate
== NULL
)
587 rhs
= CYGetProperty(context
, aggregate
, index
);
588 if (JSValueIsUndefined(context
, rhs
))
589 throw CYJSError(context
, "unable to extract array value");
592 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
599 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
603 *reinterpret_cast<const char **>(data
) = CYPoolCString(pool
, context
, value
);
606 case sig::struct_P
: {
607 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
608 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
609 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
610 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
611 ffi_type
*field(ffi
->elements
[index
]);
614 if (aggregate
== NULL
)
617 rhs
= CYGetProperty(context
, aggregate
, index
);
618 if (JSValueIsUndefined(context
, rhs
)) {
619 if (element
->name
!= NULL
)
620 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
623 if (JSValueIsUndefined(context
, rhs
)) undefined
:
624 throw CYJSError(context
, "unable to extract structure value");
628 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
638 if (hooks_
!= NULL
&& hooks_
->PoolFFI
!= NULL
)
639 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
642 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
646 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
647 switch (type
->primitive
) {
649 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
651 #define CYFromFFI_(primitive, native) \
652 case sig::primitive ## _P: \
653 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
655 CYFromFFI_(uchar, unsigned char)
656 CYFromFFI_(char, char)
657 CYFromFFI_(ushort
, unsigned short)
658 CYFromFFI_(short, short)
659 CYFromFFI_(ulong
, unsigned long)
660 CYFromFFI_(long, long)
661 CYFromFFI_(uint
, unsigned int)
663 CYFromFFI_(ulonglong
, unsigned long long)
664 CYFromFFI_(longlong
, long long)
665 CYFromFFI_(float, float)
666 CYFromFFI_(double, double)
669 if (void *pointer
= data
)
670 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
674 if (void *pointer
= *reinterpret_cast<void **>(data
))
675 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
679 if (char *utf8
= *reinterpret_cast<char **>(data
))
680 return CYCastJSValue(context
, utf8
);
684 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
686 return CYJSUndefined(context
);
689 return CYJSNull(context
);
691 if (hooks_
!= NULL
&& hooks_
->FromFFI
!= NULL
)
692 if (JSValueRef value
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
695 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
699 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
700 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
702 JSContextRef
context(internal
->context_
);
704 size_t count(internal
->cif_
.nargs
);
705 JSValueRef values
[count
];
707 for (size_t index(0); index
!= count
; ++index
)
708 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
710 JSValueRef
value(CYCallAsFunction(context
, internal
->function_
, NULL
, count
, values
));
711 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
714 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const char *type
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
715 // XXX: in case of exceptions this will leak
716 // XXX: in point of fact, this may /need/ to leak :(
717 Closure_privateData
*internal(new Closure_privateData(context
, function
, type
));
719 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
720 NULL
, sizeof(ffi_closure
),
721 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
725 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
726 _assert(status
== FFI_OK
);
728 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
730 internal
->value_
= closure
;
735 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const char *type
) {
736 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, type
, &FunctionClosure_
));
737 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
738 // XXX: see above notes about needing to leak
739 JSValueProtect(CYGetJSContext(context
), object
);
743 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
744 return CYCastJSObject(context
, CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
));
747 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const char *type
) {
748 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
750 JSValueRef
exception(NULL
);
751 bool function(JSValueIsInstanceOfConstructor(context
, value
, Function
, &exception
));
752 CYThrow(context
, exception
);
755 JSObjectRef
function(CYCastJSObject(context
, value
));
756 return CYMakeFunctor(context
, function
, type
);
758 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
759 return CYMakeFunctor(context
, function
, type
);
763 static bool Index_(apr_pool_t
*pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
764 Type_privateData
*typical(internal
->type_
);
765 sig::Type
*type(typical
->type_
);
769 const char *name(CYPoolCString(pool
, context
, property
));
770 size_t length(strlen(name
));
771 double number(CYCastDouble(name
, length
));
773 size_t count(type
->data
.signature
.count
);
775 if (std::isnan(number
)) {
776 if (property
== NULL
)
779 sig::Element
*elements(type
->data
.signature
.elements
);
781 for (size_t local(0); local
!= count
; ++local
) {
782 sig::Element
*element(&elements
[local
]);
783 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
791 index
= static_cast<ssize_t
>(number
);
792 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
797 ffi_type
**elements(typical
->GetFFI()->elements
);
799 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
800 for (ssize_t
local(0); local
!= index
; ++local
)
801 base
+= elements
[local
]->size
;
806 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
808 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
810 if (JSStringIsEqual(property
, length_s
))
811 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
813 Type_privateData
*typical(internal
->type_
);
815 if (typical
->type_
== NULL
)
819 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
821 else if (!CYGetOffset(pool
, context
, property
, offset
))
824 ffi_type
*ffi(typical
->GetFFI());
826 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
827 base
+= ffi
->size
* offset
;
829 JSObjectRef
owner(internal
->GetOwner() ?: object
);
830 return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
);
833 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
835 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
836 Type_privateData
*typical(internal
->type_
);
838 if (typical
->type_
== NULL
)
842 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
844 else if (!CYGetOffset(pool
, context
, property
, offset
))
847 ffi_type
*ffi(typical
->GetFFI());
849 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
850 base
+= ffi
->size
* offset
;
852 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
856 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
857 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
858 Type_privateData
*typical(internal
->type_
);
859 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
862 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
864 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
865 Type_privateData
*typical(internal
->type_
);
870 if (!Index_(pool
, context
, internal
, property
, index
, base
))
873 JSObjectRef
owner(internal
->GetOwner() ?: object
);
875 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
878 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
880 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
881 Type_privateData
*typical(internal
->type_
);
886 if (!Index_(pool
, context
, internal
, property
, index
, base
))
889 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
893 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
894 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
895 Type_privateData
*typical(internal
->type_
);
896 sig::Type
*type(typical
->type_
);
901 size_t count(type
->data
.signature
.count
);
902 sig::Element
*elements(type
->data
.signature
.elements
);
906 for (size_t index(0); index
!= count
; ++index
) {
908 name
= elements
[index
].name
;
911 sprintf(number
, "%zu", index
);
915 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
919 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
{
920 if (setups
+ count
!= signature
->count
- 1)
921 throw CYJSError(context
, "incorrect number of arguments to ffi function");
923 size_t size(setups
+ count
);
925 memcpy(values
, setup
, sizeof(void *) * setups
);
927 for (size_t index(setups
); index
!= size
; ++index
) {
928 sig::Element
*element(&signature
->elements
[index
+ 1]);
929 ffi_type
*ffi(cif
->arg_types
[index
]);
931 values
[index
] = new(pool
) uint8_t[ffi
->size
];
932 CYPoolFFI(pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
935 uint8_t value
[cif
->rtype
->size
];
937 if (hooks_
!= NULL
&& hooks_
->CallFunction
!= NULL
)
938 (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
);
940 ffi_call(cif
, function
, value
, values
);
942 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
945 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
947 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
948 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, exception
, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
951 static JSObjectRef
CYMakeType(JSContextRef context
, const char *type
) {
952 Type_privateData
*internal(new Type_privateData(type
));
953 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
956 static JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
957 Type_privateData
*internal(new Type_privateData(type
));
958 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
961 static void *CYCastSymbol(const char *name
) {
962 return dlsym(RTLD_DEFAULT
, name
);
965 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
966 JSObjectRef
global(CYGetGlobalObject(context
));
967 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
968 if (JSValueRef value
= CYGetProperty(context
, cycript
, property
))
969 if (!JSValueIsUndefined(context
, value
))
973 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
975 if (hooks_
!= NULL
&& hooks_
->RuntimeProperty
!= NULL
)
976 if (JSValueRef value
= (*hooks_
->RuntimeProperty
)(context
, name
))
979 sqlite3_stmt
*statement
;
981 _sqlcall(sqlite3_prepare(Bridge_
,
983 "\"bridge\".\"mode\", "
984 "\"bridge\".\"value\" "
987 " \"bridge\".\"name\" = ?"
989 , -1, &statement
, NULL
));
991 _sqlcall(sqlite3_bind_text(statement
, 1, name
.data
, name
.size
, SQLITE_STATIC
));
996 if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
) {
1000 mode
= sqlite3_column_int(statement
, 0);
1001 value
= sqlite3_column_pooled(pool
, statement
, 1);
1004 _sqlcall(sqlite3_finalize(statement
));
1008 CYThrow("invalid mode from bridge table: %d", mode
);
1013 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(value
), NULL
, NULL
, 0, NULL
);
1016 if (void (*symbol
)() = reinterpret_cast<void (*)()>(CYCastSymbol(name
.data
)))
1017 return CYMakeFunctor(context
, symbol
, value
);
1021 if (void *symbol
= CYCastSymbol(name
.data
)) {
1022 // XXX: this is horrendously inefficient
1023 sig::Signature signature
;
1024 sig::Parse(pool
, &signature
, value
, &Structor_
);
1026 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1027 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
1030 // XXX: implement case 3
1032 return CYMakeType(context
, value
);
1036 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1038 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1042 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1043 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1045 sig::Signature signature
;
1046 sig::Parse(pool
, &signature
, type
, &Structor_
);
1048 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1051 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1053 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1055 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1056 return CYMakeType(context
, type
);
1059 static JSValueRef
Type_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1060 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1064 if (JSStringIsEqualToUTF8CString(property
, "$cyi")) {
1065 type
.primitive
= sig::pointer_P
;
1066 type
.data
.data
.size
= 0;
1069 size_t index(CYGetIndex(pool
, context
, property
));
1070 if (index
== _not(size_t))
1072 type
.primitive
= sig::array_P
;
1073 type
.data
.data
.size
= index
;
1079 type
.data
.data
.type
= internal
->type_
;
1081 return CYMakeType(context
, &type
);
1084 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1085 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1088 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1089 sig::Type
*type(internal
->type_
);
1090 ffi_type
*ffi(internal
->GetFFI());
1092 uint8_t value
[ffi
->size
];
1094 CYPoolFFI(pool
, context
, type
, ffi
, value
, arguments
[0]);
1095 return CYFromFFI(context
, type
, ffi
, value
);
1098 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1100 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1101 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1103 sig::Type
*type(internal
->type_
);
1106 if (type
->primitive
!= sig::array_P
)
1107 length
= _not(size_t);
1109 length
= type
->data
.data
.size
;
1110 type
= type
->data
.data
.type
;
1113 void *value(malloc(internal
->GetFFI()->size
));
1114 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1117 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1119 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1121 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1122 return CYMakeFunctor(context
, arguments
[0], type
);
1125 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1126 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1127 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1130 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1131 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1134 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1135 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1137 sprintf(string
, "%p", internal
->value_
);
1138 return CYCastJSValue(context
, string
);
1141 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1142 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1143 if (internal
->length_
!= _not(size_t)) {
1144 JSObjectRef
Array(CYGetCachedObject(context
, Array_s
));
1145 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1146 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1149 sprintf(string
, "%p", internal
->value_
);
1150 return CYCastJSValue(context
, string
);
1154 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1155 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1157 const char *type(sig::Unparse(pool
, internal
->type_
));
1158 return CYCastJSValue(context
, CYJSString(type
));
1161 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1162 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1164 const char *type(sig::Unparse(pool
, internal
->type_
));
1165 size_t size(strlen(type
));
1166 char *cyon(new(pool
) char[12 + size
+ 1]);
1167 memcpy(cyon
, "new Type(\"", 10);
1168 cyon
[12 + size
] = '\0';
1169 cyon
[12 + size
- 2] = '"';
1170 cyon
[12 + size
- 1] = ')';
1171 memcpy(cyon
+ 10, type
, size
);
1172 return CYCastJSValue(context
, CYJSString(cyon
));
1175 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1176 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1179 static JSStaticFunction Pointer_staticFunctions
[4] = {
1180 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1181 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1182 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1186 static JSStaticFunction Struct_staticFunctions
[2] = {
1187 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1191 static JSStaticFunction Functor_staticFunctions
[4] = {
1192 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1193 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1194 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1199 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1202 static JSStaticFunction Type_staticFunctions
[4] = {
1203 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1204 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1205 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1209 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1211 void CYSetArgs(int argc
, const char *argv
[]) {
1212 JSContextRef
context(CYGetJSContext());
1213 JSValueRef args
[argc
];
1214 for (int i(0); i
!= argc
; ++i
)
1215 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1218 if (JSObjectMakeArray$
!= NULL
) {
1219 JSValueRef
exception(NULL
);
1220 array
= (*JSObjectMakeArray$
)(context
, argc
, args
, &exception
);
1221 CYThrow(context
, exception
);
1223 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1224 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1225 array
= CYCastJSObject(context
, value
);
1228 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1229 CYSetProperty(context
, System
, CYJSString("args"), array
);
1232 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1233 return JSContextGetGlobalObject(context
);
1236 const char *CYExecute(apr_pool_t
*pool
, const char *code
) {
1237 JSContextRef
context(CYGetJSContext());
1238 JSValueRef
exception(NULL
), result
;
1241 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1242 handle
= (*hooks_
->ExecuteStart
)(context
);
1249 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1250 } catch (const char *error
) {
1254 if (exception
!= NULL
) { error
:
1259 if (JSValueIsUndefined(context
, result
))
1263 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1264 } catch (const char *error
) {
1268 if (exception
!= NULL
)
1271 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1273 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1274 (*hooks_
->ExecuteEnd
)(context
, handle
);
1278 extern "C" void CydgetSetupContext(JSGlobalContextRef context
) {
1279 CYSetupContext(context
);
1282 static bool initialized_
= false;
1284 void CYInitializeDynamic() {
1286 initialized_
= true;
1289 CYInitializeStatic();
1291 _sqlcall(sqlite3_open("/usr/lib/libcycript.db", &Bridge_
));
1293 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1295 JSClassDefinition definition
;
1297 definition
= kJSClassDefinitionEmpty
;
1298 definition
.className
= "All";
1299 definition
.getProperty
= &All_getProperty
;
1300 All_
= JSClassCreate(&definition
);
1302 definition
= kJSClassDefinitionEmpty
;
1303 definition
.className
= "Context";
1304 definition
.finalize
= &CYFinalize
;
1305 Context_
= JSClassCreate(&definition
);
1307 definition
= kJSClassDefinitionEmpty
;
1308 definition
.className
= "Functor";
1309 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1310 definition
.callAsFunction
= &Functor_callAsFunction
;
1311 definition
.finalize
= &CYFinalize
;
1312 Functor_
= JSClassCreate(&definition
);
1314 definition
= kJSClassDefinitionEmpty
;
1315 definition
.className
= "Pointer";
1316 definition
.staticFunctions
= Pointer_staticFunctions
;
1317 definition
.getProperty
= &Pointer_getProperty
;
1318 definition
.setProperty
= &Pointer_setProperty
;
1319 definition
.finalize
= &CYFinalize
;
1320 Pointer_
= JSClassCreate(&definition
);
1322 definition
= kJSClassDefinitionEmpty
;
1323 definition
.className
= "Struct";
1324 definition
.staticFunctions
= Struct_staticFunctions
;
1325 definition
.getProperty
= &Struct_getProperty
;
1326 definition
.setProperty
= &Struct_setProperty
;
1327 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1328 definition
.finalize
= &CYFinalize
;
1329 Struct_
= JSClassCreate(&definition
);
1331 definition
= kJSClassDefinitionEmpty
;
1332 definition
.className
= "Type";
1333 definition
.staticFunctions
= Type_staticFunctions
;
1334 definition
.getProperty
= &Type_getProperty
;
1335 definition
.callAsFunction
= &Type_callAsFunction
;
1336 definition
.callAsConstructor
= &Type_callAsConstructor
;
1337 definition
.finalize
= &CYFinalize
;
1338 Type_privateData::Class_
= JSClassCreate(&definition
);
1340 definition
= kJSClassDefinitionEmpty
;
1341 //definition.getProperty = &Global_getProperty;
1342 Global_
= JSClassCreate(&definition
);
1344 Array_s
= JSStringCreateWithUTF8CString("Array");
1345 cy_s
= JSStringCreateWithUTF8CString("$cy");
1346 length_s
= JSStringCreateWithUTF8CString("length");
1347 message_s
= JSStringCreateWithUTF8CString("message");
1348 name_s
= JSStringCreateWithUTF8CString("name");
1349 pop_s
= JSStringCreateWithUTF8CString("pop");
1350 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1351 push_s
= JSStringCreateWithUTF8CString("push");
1352 splice_s
= JSStringCreateWithUTF8CString("splice");
1353 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1354 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1356 Result_
= JSStringCreateWithUTF8CString("_");
1358 if (hooks_
!= NULL
&& hooks_
->Initialize
!= NULL
)
1359 (*hooks_
->Initialize
)();
1362 void CYThrow(JSContextRef context
, JSValueRef value
) {
1364 throw CYJSError(context
, value
);
1367 const char *CYJSError::PoolCString(apr_pool_t
*pool
) const {
1368 // XXX: this used to be CYPoolCString
1369 return CYPoolCCYON(pool
, context_
, value_
);
1372 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1373 // XXX: what if the context is different?
1377 JSValueRef
CYCastJSError(JSContextRef context
, const char *message
) {
1378 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString("Error")));
1380 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1382 JSValueRef
exception(NULL
);
1383 JSValueRef
value(JSObjectCallAsConstructor(context
, Error
, 1, arguments
, &exception
));
1384 CYThrow(context
, exception
);
1389 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1390 return CYCastJSError(context
, message_
);
1393 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1394 _assert(context
!= NULL
);
1399 va_start(args
, format
);
1400 const char *message(apr_pvsprintf(pool
, format
, args
));
1403 value_
= CYCastJSError(context
, message
);
1406 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1407 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1410 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
1411 CYInitializeDynamic();
1413 JSObjectRef
global(CYGetGlobalObject(context
));
1415 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
1416 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
1418 /* Cache Globals {{{ */
1419 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
1420 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
1422 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
1423 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
1425 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
1426 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
1428 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
1429 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
1431 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
1432 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
1434 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1435 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
1437 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
1438 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
1440 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
1441 CYSetProperty(context
, cy
, CYJSString("String"), String
);
1444 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1446 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1447 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1448 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
1450 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1451 JSObjectSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
1452 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
1454 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1455 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1457 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
1458 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
1460 JSObjectRef
last(NULL
), curr(global
);
1462 goto next
; for (JSValueRef next
;;) {
1463 if (JSValueIsNull(context
, next
))
1466 curr
= CYCastJSObject(context
, next
);
1468 next
= JSObjectGetPrototype(context
, curr
);
1471 JSObjectSetPrototype(context
, last
, all
);
1473 CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
);
1475 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
1476 CYSetProperty(context
, cy
, CYJSString("System"), Function
);
1478 CYSetProperty(context
, global
, CYJSString("system"), System
);
1479 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
1480 //CYSetProperty(context, System, CYJSString("global"), global);
1481 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
1483 if (hooks_
!= NULL
&& hooks_
->SetupContext
!= NULL
)
1484 (*hooks_
->SetupContext
)(context
);
1487 JSGlobalContextRef
CYGetJSContext() {
1488 CYInitializeDynamic();
1490 static JSGlobalContextRef context_
;
1492 if (context_
== NULL
) {
1493 context_
= JSGlobalContextCreate(Global_
);
1494 CYSetupContext(context_
);