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 void Structor_(apr_pool_t
*pool
, sig::Type
*&type
) {
186 type
->primitive
== sig::pointer_P
&&
187 type
->data
.data
.type
!= NULL
&&
188 type
->data
.data
.type
->primitive
== sig::struct_P
&&
189 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
191 type
->primitive
= sig::typename_P
;
192 type
->data
.data
.type
= NULL
;
196 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
199 sqlite3_stmt
*statement
;
201 _sqlcall(sqlite3_prepare(Bridge_
,
203 "\"bridge\".\"mode\", "
204 "\"bridge\".\"value\" "
207 " \"bridge\".\"mode\" in (3, 4) and"
208 " \"bridge\".\"name\" = ?"
210 , -1, &statement
, NULL
));
212 _sqlcall(sqlite3_bind_text(statement
, 1, type
->name
, -1, SQLITE_STATIC
));
217 if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
) {
221 mode
= sqlite3_column_int(statement
, 0);
222 value
= sqlite3_column_pooled(pool
, statement
, 1);
225 _sqlcall(sqlite3_finalize(statement
));
234 sig::Parse(pool
, &type
->data
.signature
, value
, &Structor_
);
238 sig::Signature signature
;
239 sig::Parse(pool
, &signature
, value
, &Structor_
);
240 type
= signature
.elements
[0].type
;
245 JSClassRef
Type_privateData::Class_
;
250 JSGlobalContextRef context_
;
252 Context(JSGlobalContextRef context
) :
261 Type_privateData
*type_
;
264 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type
*type
) :
265 CYOwned(value
, context
, owner
),
266 type_(new(pool_
) Type_privateData(type
)),
272 struct Struct_privateData
:
275 Type_privateData
*type_
;
277 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
278 CYOwned(NULL
, context
, owner
)
283 typedef std::map
<const char *, Type_privateData
*, CYCStringLess
> TypeMap
;
284 static TypeMap Types_
;
286 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
287 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
288 apr_pool_t
*pool(internal
->pool_
);
289 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
290 internal
->type_
= typical
;
293 internal
->value_
= data
;
295 size_t size(typical
->GetFFI()->size
);
296 void *copy(apr_palloc(internal
->pool_
, size
));
297 memcpy(copy
, data
, size
);
298 internal
->value_
= copy
;
301 return JSObjectMake(context
, Struct_
, internal
);
304 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
305 return JSValueMakeBoolean(context
, value
);
308 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
309 return JSValueMakeNumber(context
, value
);
312 #define CYCastJSValue_(Type_) \
313 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
314 return JSValueMakeNumber(context, static_cast<double>(value)); \
318 CYCastJSValue_(unsigned int)
319 CYCastJSValue_(long int)
320 CYCastJSValue_(long unsigned int)
321 CYCastJSValue_(long long int)
322 CYCastJSValue_(long long unsigned int)
324 JSValueRef
CYJSUndefined(JSContextRef context
) {
325 return JSValueMakeUndefined(context
);
328 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
329 JSValueRef
exception(NULL
);
330 double number(JSValueToNumber(context
, value
, &exception
));
331 CYThrow(context
, exception
);
335 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
336 return JSValueToBoolean(context
, value
);
339 JSValueRef
CYJSNull(JSContextRef context
) {
340 return JSValueMakeNull(context
);
343 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
344 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
347 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
348 return CYCastJSValue(context
, CYJSString(value
));
351 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
352 JSValueRef
exception(NULL
);
353 JSObjectRef
object(JSValueToObject(context
, value
, &exception
));
354 CYThrow(context
, exception
);
358 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
359 JSValueRef
exception(NULL
);
360 JSValueRef
value(JSObjectCallAsFunction(context
, function
, _this
, count
, arguments
, &exception
));
361 CYThrow(context
, exception
);
365 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
366 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
369 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
374 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
377 return CYJSUndefined(context
);
380 static size_t Nonce_(0);
382 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
384 const char *name(apr_psprintf(pool
, "%s%"APR_SIZE_T_FMT
"", CYPoolCString(pool
, context
, arguments
[0]), Nonce_
++));
385 return CYCastJSValue(context
, name
);
388 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
389 JSGarbageCollect(context
);
390 return CYJSUndefined(context
);
393 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
394 switch (JSType type
= JSValueGetType(context
, value
)) {
395 case kJSTypeUndefined
:
400 return CYCastBool(context
, value
) ? "true" : "false";
402 case kJSTypeNumber
: {
403 std::ostringstream str
;
404 CYNumerify(str
, CYCastDouble(context
, value
));
405 std::string
value(str
.str());
406 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
409 case kJSTypeString
: {
410 std::ostringstream str
;
411 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
412 CYStringify(str
, string
.data
, string
.size
);
413 std::string
value(str
.str());
414 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
418 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
420 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
424 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
) {
425 JSValueRef
exception(NULL
);
426 const char *cyon(CYPoolCCYON(pool
, context
, value
, &exception
));
427 CYThrow(context
, exception
);
431 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSObjectRef object
) {
432 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
433 if (CYIsCallable(context
, toCYON
)) {
434 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
435 _assert(value
!= NULL
);
436 return CYPoolCString(pool
, context
, value
);
439 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
440 if (CYIsCallable(context
, toJSON
)) {
441 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
442 JSValueRef
exception(NULL
);
443 const char *cyon(CYPoolCCYON(pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), &exception
));
444 CYThrow(context
, exception
);
448 std::ostringstream str
;
452 // XXX: this is, sadly, going to leak
453 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
457 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
458 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
459 JSValueRef
value(CYGetProperty(context
, object
, name
));
466 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
470 CYStringify(str
, string
.data
, string
.size
);
472 str
<< ':' << CYPoolCCYON(pool
, context
, value
);
477 JSPropertyNameArrayRelease(names
);
479 std::string
string(str
.str());
480 return apr_pstrmemdup(pool
, string
.c_str(), string
.size());
483 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
485 std::ostringstream str
;
489 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
492 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
493 JSValueRef
value(CYGetProperty(context
, _this
, index
));
500 if (!JSValueIsUndefined(context
, value
))
501 str
<< CYPoolCCYON(pool
, context
, value
);
510 std::string
value(str
.str());
511 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
514 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
515 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
516 return JSObjectMake(context
, Pointer_
, internal
);
519 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const char *type
) {
520 cy::Functor
*internal(new cy::Functor(type
, function
));
521 return JSObjectMake(context
, Functor_
, internal
);
524 static bool CYGetOffset(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
525 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
528 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
529 switch (JSValueGetType(context
, value
)) {
532 /*case kJSTypeObject:
533 if (JSValueIsObjectOfClass(context, value, Pointer_)) {
534 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value)));
535 return internal->value_;
538 double number(CYCastDouble(context
, value
));
539 if (std::isnan(number
))
540 throw CYJSError(context
, "cannot convert value to pointer");
541 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
545 void CYPoolFFI(apr_pool_t
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
546 switch (type
->primitive
) {
548 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
551 #define CYPoolFFI_(primitive, native) \
552 case sig::primitive ## _P: \
553 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
556 CYPoolFFI_(uchar
, unsigned char)
557 CYPoolFFI_(char, char)
558 CYPoolFFI_(ushort
, unsigned short)
559 CYPoolFFI_(short, short)
560 CYPoolFFI_(ulong
, unsigned long)
561 CYPoolFFI_(long, long)
562 CYPoolFFI_(uint
, unsigned int)
564 CYPoolFFI_(ulonglong
, unsigned long long)
565 CYPoolFFI_(longlong
, long long)
566 CYPoolFFI_(float, float)
567 CYPoolFFI_(double, double)
570 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
571 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
572 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
573 ffi_type
*field(ffi
->elements
[index
]);
576 if (aggregate
== NULL
)
579 rhs
= CYGetProperty(context
, aggregate
, index
);
580 if (JSValueIsUndefined(context
, rhs
))
581 throw CYJSError(context
, "unable to extract array value");
584 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
591 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
595 *reinterpret_cast<const char **>(data
) = CYPoolCString(pool
, context
, value
);
598 case sig::struct_P
: {
599 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
600 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
601 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
602 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
603 ffi_type
*field(ffi
->elements
[index
]);
606 if (aggregate
== NULL
)
609 rhs
= CYGetProperty(context
, aggregate
, index
);
610 if (JSValueIsUndefined(context
, rhs
)) {
611 if (element
->name
!= NULL
)
612 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
615 if (JSValueIsUndefined(context
, rhs
)) undefined
:
616 throw CYJSError(context
, "unable to extract structure value");
620 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
630 if (hooks_
!= NULL
&& hooks_
->PoolFFI
!= NULL
)
631 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
634 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
638 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
639 switch (type
->primitive
) {
641 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
643 #define CYFromFFI_(primitive, native) \
644 case sig::primitive ## _P: \
645 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
647 CYFromFFI_(uchar, unsigned char)
648 CYFromFFI_(char, char)
649 CYFromFFI_(ushort
, unsigned short)
650 CYFromFFI_(short, short)
651 CYFromFFI_(ulong
, unsigned long)
652 CYFromFFI_(long, long)
653 CYFromFFI_(uint
, unsigned int)
655 CYFromFFI_(ulonglong
, unsigned long long)
656 CYFromFFI_(longlong
, long long)
657 CYFromFFI_(float, float)
658 CYFromFFI_(double, double)
661 if (void *pointer
= data
)
662 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
666 if (void *pointer
= *reinterpret_cast<void **>(data
))
667 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
671 if (char *utf8
= *reinterpret_cast<char **>(data
))
672 return CYCastJSValue(context
, utf8
);
676 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
678 return CYJSUndefined(context
);
681 return CYJSNull(context
);
683 if (hooks_
!= NULL
&& hooks_
->FromFFI
!= NULL
)
684 if (JSValueRef value
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
687 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
691 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
692 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
694 JSContextRef
context(internal
->context_
);
696 size_t count(internal
->cif_
.nargs
);
697 JSValueRef values
[count
];
699 for (size_t index(0); index
!= count
; ++index
)
700 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
702 JSValueRef
value(CYCallAsFunction(context
, internal
->function_
, NULL
, count
, values
));
703 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
706 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const char *type
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
707 // XXX: in case of exceptions this will leak
708 // XXX: in point of fact, this may /need/ to leak :(
709 Closure_privateData
*internal(new Closure_privateData(context
, function
, type
));
711 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
712 NULL
, sizeof(ffi_closure
),
713 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
717 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
718 _assert(status
== FFI_OK
);
720 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
722 internal
->value_
= closure
;
727 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const char *type
) {
728 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, type
, &FunctionClosure_
));
729 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
730 // XXX: see above notes about needing to leak
731 JSValueProtect(CYGetJSContext(context
), object
);
735 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
736 return CYCastJSObject(context
, CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
));
739 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const char *type
) {
740 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
742 JSValueRef
exception(NULL
);
743 bool function(JSValueIsInstanceOfConstructor(context
, value
, Function
, &exception
));
744 CYThrow(context
, exception
);
747 JSObjectRef
function(CYCastJSObject(context
, value
));
748 return CYMakeFunctor(context
, function
, type
);
750 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
751 return CYMakeFunctor(context
, function
, type
);
755 static bool Index_(apr_pool_t
*pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
756 Type_privateData
*typical(internal
->type_
);
757 sig::Type
*type(typical
->type_
);
761 const char *name(CYPoolCString(pool
, context
, property
));
762 size_t length(strlen(name
));
763 double number(CYCastDouble(name
, length
));
765 size_t count(type
->data
.signature
.count
);
767 if (std::isnan(number
)) {
768 if (property
== NULL
)
771 sig::Element
*elements(type
->data
.signature
.elements
);
773 for (size_t local(0); local
!= count
; ++local
) {
774 sig::Element
*element(&elements
[local
]);
775 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
783 index
= static_cast<ssize_t
>(number
);
784 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
789 ffi_type
**elements(typical
->GetFFI()->elements
);
791 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
792 for (ssize_t
local(0); local
!= index
; ++local
)
793 base
+= elements
[local
]->size
;
798 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
800 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
802 if (JSStringIsEqual(property
, length_s
))
803 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
805 Type_privateData
*typical(internal
->type_
);
807 if (typical
->type_
== NULL
)
811 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
813 else if (!CYGetOffset(pool
, context
, property
, offset
))
816 ffi_type
*ffi(typical
->GetFFI());
818 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
819 base
+= ffi
->size
* offset
;
821 JSObjectRef
owner(internal
->GetOwner() ?: object
);
822 return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
);
825 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
827 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
828 Type_privateData
*typical(internal
->type_
);
830 if (typical
->type_
== NULL
)
834 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
836 else if (!CYGetOffset(pool
, context
, property
, offset
))
839 ffi_type
*ffi(typical
->GetFFI());
841 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
842 base
+= ffi
->size
* offset
;
844 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
848 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
849 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
850 Type_privateData
*typical(internal
->type_
);
851 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
854 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
856 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
857 Type_privateData
*typical(internal
->type_
);
862 if (!Index_(pool
, context
, internal
, property
, index
, base
))
865 JSObjectRef
owner(internal
->GetOwner() ?: object
);
867 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
870 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
872 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
873 Type_privateData
*typical(internal
->type_
);
878 if (!Index_(pool
, context
, internal
, property
, index
, base
))
881 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
885 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
886 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
887 Type_privateData
*typical(internal
->type_
);
888 sig::Type
*type(typical
->type_
);
893 size_t count(type
->data
.signature
.count
);
894 sig::Element
*elements(type
->data
.signature
.elements
);
898 for (size_t index(0); index
!= count
; ++index
) {
900 name
= elements
[index
].name
;
903 sprintf(number
, "%zu", index
);
907 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
911 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
{
912 if (setups
+ count
!= signature
->count
- 1)
913 throw CYJSError(context
, "incorrect number of arguments to ffi function");
915 size_t size(setups
+ count
);
917 memcpy(values
, setup
, sizeof(void *) * setups
);
919 for (size_t index(setups
); index
!= size
; ++index
) {
920 sig::Element
*element(&signature
->elements
[index
+ 1]);
921 ffi_type
*ffi(cif
->arg_types
[index
]);
923 values
[index
] = new(pool
) uint8_t[ffi
->size
];
924 CYPoolFFI(pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
927 uint8_t value
[cif
->rtype
->size
];
929 if (hooks_
!= NULL
&& hooks_
->CallFunction
!= NULL
)
930 (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
);
932 ffi_call(cif
, function
, value
, values
);
934 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
937 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
939 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
940 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, exception
, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
943 static JSObjectRef
CYMakeType(JSContextRef context
, const char *type
) {
944 Type_privateData
*internal(new Type_privateData(type
));
945 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
948 static JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
949 Type_privateData
*internal(new Type_privateData(type
));
950 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
953 static void *CYCastSymbol(const char *name
) {
954 return dlsym(RTLD_DEFAULT
, name
);
957 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
958 JSObjectRef
global(CYGetGlobalObject(context
));
959 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
960 if (JSValueRef value
= CYGetProperty(context
, cycript
, property
))
961 if (!JSValueIsUndefined(context
, value
))
965 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
967 if (hooks_
!= NULL
&& hooks_
->RuntimeProperty
!= NULL
)
968 if (JSValueRef value
= (*hooks_
->RuntimeProperty
)(context
, name
))
971 sqlite3_stmt
*statement
;
973 _sqlcall(sqlite3_prepare(Bridge_
,
975 "\"bridge\".\"mode\", "
976 "\"bridge\".\"value\" "
979 " \"bridge\".\"name\" = ?"
981 , -1, &statement
, NULL
));
983 _sqlcall(sqlite3_bind_text(statement
, 1, name
.data
, name
.size
, SQLITE_STATIC
));
988 if (_sqlcall(sqlite3_step(statement
)) == SQLITE_DONE
) {
992 mode
= sqlite3_column_int(statement
, 0);
993 value
= sqlite3_column_pooled(pool
, statement
, 1);
996 _sqlcall(sqlite3_finalize(statement
));
1000 CYThrow("invalid mode from bridge table: %d", mode
);
1005 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(value
), NULL
, NULL
, 0, NULL
);
1008 if (void (*symbol
)() = reinterpret_cast<void (*)()>(CYCastSymbol(name
.data
)))
1009 return CYMakeFunctor(context
, symbol
, value
);
1013 if (void *symbol
= CYCastSymbol(name
.data
)) {
1014 // XXX: this is horrendously inefficient
1015 sig::Signature signature
;
1016 sig::Parse(pool
, &signature
, value
, &Structor_
);
1018 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1019 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
1022 // XXX: implement case 3
1024 return CYMakeType(context
, value
);
1028 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1030 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1034 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1035 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1037 sig::Signature signature
;
1038 sig::Parse(pool
, &signature
, type
, &Structor_
);
1040 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1043 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1045 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1047 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1048 return CYMakeType(context
, type
);
1051 static JSValueRef
Type_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1052 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1056 if (JSStringIsEqualToUTF8CString(property
, "$cyi")) {
1057 type
.primitive
= sig::pointer_P
;
1058 type
.data
.data
.size
= 0;
1061 size_t index(CYGetIndex(pool
, context
, property
));
1062 if (index
== _not(size_t))
1064 type
.primitive
= sig::array_P
;
1065 type
.data
.data
.size
= index
;
1071 type
.data
.data
.type
= internal
->type_
;
1073 return CYMakeType(context
, &type
);
1076 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1077 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1080 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1081 sig::Type
*type(internal
->type_
);
1082 ffi_type
*ffi(internal
->GetFFI());
1084 uint8_t value
[ffi
->size
];
1086 CYPoolFFI(pool
, context
, type
, ffi
, value
, arguments
[0]);
1087 return CYFromFFI(context
, type
, ffi
, value
);
1090 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1092 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1093 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1095 sig::Type
*type(internal
->type_
);
1098 if (type
->primitive
!= sig::array_P
)
1099 length
= _not(size_t);
1101 length
= type
->data
.data
.size
;
1102 type
= type
->data
.data
.type
;
1105 void *value(malloc(internal
->GetFFI()->size
));
1106 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1109 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1111 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1113 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1114 return CYMakeFunctor(context
, arguments
[0], type
);
1117 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1118 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1119 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1122 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1123 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1126 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1127 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1129 sprintf(string
, "%p", internal
->value_
);
1130 return CYCastJSValue(context
, string
);
1133 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1134 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1135 if (internal
->length_
!= _not(size_t)) {
1136 JSObjectRef
Array(CYGetCachedObject(context
, Array_s
));
1137 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1138 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1141 sprintf(string
, "%p", internal
->value_
);
1142 return CYCastJSValue(context
, string
);
1146 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1147 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1149 const char *type(sig::Unparse(pool
, internal
->type_
));
1150 return CYCastJSValue(context
, CYJSString(type
));
1153 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1154 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1156 const char *type(sig::Unparse(pool
, internal
->type_
));
1157 size_t size(strlen(type
));
1158 char *cyon(new(pool
) char[12 + size
+ 1]);
1159 memcpy(cyon
, "new Type(\"", 10);
1160 cyon
[12 + size
] = '\0';
1161 cyon
[12 + size
- 2] = '"';
1162 cyon
[12 + size
- 1] = ')';
1163 memcpy(cyon
+ 10, type
, size
);
1164 return CYCastJSValue(context
, CYJSString(cyon
));
1167 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1168 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1171 static JSStaticFunction Pointer_staticFunctions
[4] = {
1172 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1173 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1174 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1178 static JSStaticFunction Struct_staticFunctions
[2] = {
1179 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1183 static JSStaticFunction Functor_staticFunctions
[4] = {
1184 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1185 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1186 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1191 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1194 static JSStaticFunction Type_staticFunctions
[4] = {
1195 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1196 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1197 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1201 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1203 void CYSetArgs(int argc
, const char *argv
[]) {
1204 JSContextRef
context(CYGetJSContext());
1205 JSValueRef args
[argc
];
1206 for (int i(0); i
!= argc
; ++i
)
1207 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1210 if (JSObjectMakeArray$
!= NULL
) {
1211 JSValueRef
exception(NULL
);
1212 array
= (*JSObjectMakeArray$
)(context
, argc
, args
, &exception
);
1213 CYThrow(context
, exception
);
1215 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1216 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1217 array
= CYCastJSObject(context
, value
);
1220 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1221 CYSetProperty(context
, System
, CYJSString("args"), array
);
1224 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1225 return JSContextGetGlobalObject(context
);
1228 const char *CYExecute(apr_pool_t
*pool
, const char *code
) {
1229 JSContextRef
context(CYGetJSContext());
1230 JSValueRef
exception(NULL
), result
;
1233 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1234 handle
= (*hooks_
->ExecuteStart
)(context
);
1241 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1242 } catch (const char *error
) {
1246 if (exception
!= NULL
) { error
:
1251 if (JSValueIsUndefined(context
, result
))
1255 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1256 } catch (const char *error
) {
1260 if (exception
!= NULL
)
1263 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1265 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1266 (*hooks_
->ExecuteEnd
)(context
, handle
);
1270 extern "C" void CydgetSetupContext(JSGlobalContextRef context
) {
1271 CYSetupContext(context
);
1274 static bool initialized_
= false;
1276 void CYInitializeDynamic() {
1278 initialized_
= true;
1281 CYInitializeStatic();
1283 _sqlcall(sqlite3_open("/usr/lib/libcycript.db", &Bridge_
));
1285 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1287 JSClassDefinition definition
;
1289 definition
= kJSClassDefinitionEmpty
;
1290 definition
.className
= "All";
1291 definition
.getProperty
= &All_getProperty
;
1292 All_
= JSClassCreate(&definition
);
1294 definition
= kJSClassDefinitionEmpty
;
1295 definition
.className
= "Context";
1296 definition
.finalize
= &CYFinalize
;
1297 Context_
= JSClassCreate(&definition
);
1299 definition
= kJSClassDefinitionEmpty
;
1300 definition
.className
= "Functor";
1301 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1302 definition
.callAsFunction
= &Functor_callAsFunction
;
1303 definition
.finalize
= &CYFinalize
;
1304 Functor_
= JSClassCreate(&definition
);
1306 definition
= kJSClassDefinitionEmpty
;
1307 definition
.className
= "Pointer";
1308 definition
.staticFunctions
= Pointer_staticFunctions
;
1309 definition
.getProperty
= &Pointer_getProperty
;
1310 definition
.setProperty
= &Pointer_setProperty
;
1311 definition
.finalize
= &CYFinalize
;
1312 Pointer_
= JSClassCreate(&definition
);
1314 definition
= kJSClassDefinitionEmpty
;
1315 definition
.className
= "Struct";
1316 definition
.staticFunctions
= Struct_staticFunctions
;
1317 definition
.getProperty
= &Struct_getProperty
;
1318 definition
.setProperty
= &Struct_setProperty
;
1319 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1320 definition
.finalize
= &CYFinalize
;
1321 Struct_
= JSClassCreate(&definition
);
1323 definition
= kJSClassDefinitionEmpty
;
1324 definition
.className
= "Type";
1325 definition
.staticFunctions
= Type_staticFunctions
;
1326 definition
.getProperty
= &Type_getProperty
;
1327 definition
.callAsFunction
= &Type_callAsFunction
;
1328 definition
.callAsConstructor
= &Type_callAsConstructor
;
1329 definition
.finalize
= &CYFinalize
;
1330 Type_privateData::Class_
= JSClassCreate(&definition
);
1332 definition
= kJSClassDefinitionEmpty
;
1333 //definition.getProperty = &Global_getProperty;
1334 Global_
= JSClassCreate(&definition
);
1336 Array_s
= JSStringCreateWithUTF8CString("Array");
1337 cy_s
= JSStringCreateWithUTF8CString("$cy");
1338 length_s
= JSStringCreateWithUTF8CString("length");
1339 message_s
= JSStringCreateWithUTF8CString("message");
1340 name_s
= JSStringCreateWithUTF8CString("name");
1341 pop_s
= JSStringCreateWithUTF8CString("pop");
1342 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1343 push_s
= JSStringCreateWithUTF8CString("push");
1344 splice_s
= JSStringCreateWithUTF8CString("splice");
1345 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1346 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1348 Result_
= JSStringCreateWithUTF8CString("_");
1350 if (hooks_
!= NULL
&& hooks_
->Initialize
!= NULL
)
1351 (*hooks_
->Initialize
)();
1354 void CYThrow(JSContextRef context
, JSValueRef value
) {
1356 throw CYJSError(context
, value
);
1359 const char *CYJSError::PoolCString(apr_pool_t
*pool
) const {
1360 // XXX: this used to be CYPoolCString
1361 return CYPoolCCYON(pool
, context_
, value_
);
1364 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1365 // XXX: what if the context is different?
1369 JSValueRef
CYCastJSError(JSContextRef context
, const char *message
) {
1370 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString("Error")));
1372 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1374 JSValueRef
exception(NULL
);
1375 JSValueRef
value(JSObjectCallAsConstructor(context
, Error
, 1, arguments
, &exception
));
1376 CYThrow(context
, exception
);
1381 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1382 return CYCastJSError(context
, message_
);
1385 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1386 _assert(context
!= NULL
);
1391 va_start(args
, format
);
1392 const char *message(apr_pvsprintf(pool
, format
, args
));
1395 value_
= CYCastJSError(context
, message
);
1398 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1399 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1402 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
1403 CYInitializeDynamic();
1405 JSObjectRef
global(CYGetGlobalObject(context
));
1407 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
1408 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
1410 /* Cache Globals {{{ */
1411 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
1412 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
1414 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
1415 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
1417 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
1418 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
1420 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
1421 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
1423 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
1424 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
1426 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1427 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
1429 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
1430 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
1432 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
1433 CYSetProperty(context
, cy
, CYJSString("String"), String
);
1436 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1438 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1439 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1440 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
1442 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1443 JSObjectSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
1444 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
1446 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1447 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1449 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
1450 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
1452 JSObjectRef
last(NULL
), curr(global
);
1454 goto next
; for (JSValueRef next
;;) {
1455 if (JSValueIsNull(context
, next
))
1458 curr
= CYCastJSObject(context
, next
);
1460 next
= JSObjectGetPrototype(context
, curr
);
1463 JSObjectSetPrototype(context
, last
, all
);
1465 CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
);
1467 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
1468 CYSetProperty(context
, cy
, CYJSString("System"), Function
);
1470 CYSetProperty(context
, global
, CYJSString("system"), System
);
1471 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
1472 //CYSetProperty(context, System, CYJSString("global"), global);
1473 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
1475 if (hooks_
!= NULL
&& hooks_
->SetupContext
!= NULL
)
1476 (*hooks_
->SetupContext
)(context
);
1479 JSGlobalContextRef
CYGetJSContext() {
1480 CYInitializeDynamic();
1482 static JSGlobalContextRef context_
;
1484 if (context_
== NULL
) {
1485 context_
= JSGlobalContextCreate(Global_
);
1486 CYSetupContext(context_
);