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.
40 #include "Internal.hpp"
45 #include "cycript.hpp"
47 #include "sig/parse.hpp"
48 #include "sig/ffi_type.hpp"
50 #include "Pooling.hpp"
51 #include "Execute.hpp"
56 #include <ext/stdio_filebuf.h>
64 #include "Cycript.tab.hh"
67 #include "JavaScript.hpp"
70 struct CYHooks
*hooks_
;
72 /* JavaScript Properties {{{ */
73 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) {
74 JSValueRef
exception(NULL
);
75 JSValueRef
value(JSObjectGetPropertyAtIndex(context
, object
, index
, &exception
));
76 CYThrow(context
, exception
);
80 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
81 JSValueRef
exception(NULL
);
82 JSValueRef
value(JSObjectGetProperty(context
, object
, name
, &exception
));
83 CYThrow(context
, exception
);
87 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) {
88 JSValueRef
exception(NULL
);
89 JSObjectSetPropertyAtIndex(context
, object
, index
, value
, &exception
);
90 CYThrow(context
, exception
);
93 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) {
94 JSValueRef
exception(NULL
);
95 JSObjectSetProperty(context
, object
, name
, value
, attributes
, &exception
);
96 CYThrow(context
, exception
);
99 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef (*callback
)(JSContextRef
, JSObjectRef
, JSObjectRef
, size_t, const JSValueRef
[], JSValueRef
*), JSPropertyAttributes attributes
) {
100 CYSetProperty(context
, object
, name
, JSObjectMakeFunctionWithCallback(context
, name
, callback
), attributes
);
103 /* JavaScript Strings {{{ */
104 JSStringRef
CYCopyJSString(const char *value
) {
105 return value
== NULL
? NULL
: JSStringCreateWithUTF8CString(value
);
108 JSStringRef
CYCopyJSString(JSStringRef value
) {
109 return value
== NULL
? NULL
: JSStringRetain(value
);
112 JSStringRef
CYCopyJSString(CYUTF8String value
) {
113 // XXX: this is very wrong; it needs to convert to UTF16 and then create from there
114 return CYCopyJSString(value
.data
);
117 JSStringRef
CYCopyJSString(JSContextRef context
, JSValueRef value
) {
118 if (JSValueIsNull(context
, value
))
120 JSValueRef
exception(NULL
);
121 JSStringRef
string(JSValueToStringCopy(context
, value
, &exception
));
122 CYThrow(context
, exception
);
126 static CYUTF16String
CYCastUTF16String(JSStringRef value
) {
127 return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
));
130 CYUTF8String
CYPoolUTF8String(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
131 return CYPoolUTF8String(pool
, CYCastUTF16String(value
));
134 const char *CYPoolCString(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
135 CYUTF8String
utf8(CYPoolUTF8String(pool
, context
, value
));
136 _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
);
140 const char *CYPoolCString(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
) {
141 return JSValueIsNull(context
, value
) ? NULL
: CYPoolCString(pool
, context
, CYJSString(context
, value
));
144 /* Index Offsets {{{ */
145 size_t CYGetIndex(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
146 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
150 static JSClassRef All_
;
151 static JSClassRef Context_
;
152 static JSClassRef Functor_
;
153 static JSClassRef Global_
;
154 static JSClassRef Pointer_
;
155 static JSClassRef Struct_
;
159 JSStringRef length_s
;
160 JSStringRef message_s
;
163 JSStringRef prototype_s
;
165 JSStringRef splice_s
;
166 JSStringRef toCYON_s
;
167 JSStringRef toJSON_s
;
169 static JSStringRef Result_
;
171 void CYFinalize(JSObjectRef object
) {
172 delete reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
));
175 void Structor_(apr_pool_t
*pool
, sig::Type
*&type
) {
177 type
->primitive
== sig::pointer_P
&&
178 type
->data
.data
.type
!= NULL
&&
179 type
->data
.data
.type
->primitive
== sig::struct_P
&&
180 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
182 type
->primitive
= sig::typename_P
;
183 type
->data
.data
.type
= NULL
;
187 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
190 size_t length(strlen(type
->name
));
191 char keyed
[length
+ 2];
192 memcpy(keyed
+ 1, type
->name
, length
+ 1);
194 static const char *modes
= "34";
195 for (size_t i(0); i
!= 2; ++i
) {
199 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
202 sig::Parse(pool
, &type
->data
.signature
, entry
->value_
, &Structor_
);
206 sig::Signature signature
;
207 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
208 type
= signature
.elements
[0].type
;
214 JSClassRef
Type_privateData::Class_
;
219 JSGlobalContextRef context_
;
221 Context(JSGlobalContextRef context
) :
230 Type_privateData
*type_
;
233 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type
*type
) :
234 CYOwned(value
, context
, owner
),
235 type_(new(pool_
) Type_privateData(type
)),
241 struct Struct_privateData
:
244 Type_privateData
*type_
;
246 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
247 CYOwned(NULL
, context
, owner
)
252 typedef std::map
<const char *, Type_privateData
*, CYCStringLess
> TypeMap
;
253 static TypeMap Types_
;
255 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
256 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
257 apr_pool_t
*pool(internal
->pool_
);
258 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
259 internal
->type_
= typical
;
262 internal
->value_
= data
;
264 size_t size(typical
->GetFFI()->size
);
265 void *copy(apr_palloc(internal
->pool_
, size
));
266 memcpy(copy
, data
, size
);
267 internal
->value_
= copy
;
270 return JSObjectMake(context
, Struct_
, internal
);
273 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
274 return JSValueMakeBoolean(context
, value
);
277 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
278 return JSValueMakeNumber(context
, value
);
281 #define CYCastJSValue_(Type_) \
282 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
283 return JSValueMakeNumber(context, static_cast<double>(value)); \
287 CYCastJSValue_(unsigned int)
288 CYCastJSValue_(long int)
289 CYCastJSValue_(long unsigned int)
290 CYCastJSValue_(long long int)
291 CYCastJSValue_(long long unsigned int)
293 JSValueRef
CYJSUndefined(JSContextRef context
) {
294 return JSValueMakeUndefined(context
);
297 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
298 JSValueRef
exception(NULL
);
299 double number(JSValueToNumber(context
, value
, &exception
));
300 CYThrow(context
, exception
);
304 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
305 return JSValueToBoolean(context
, value
);
308 JSValueRef
CYJSNull(JSContextRef context
) {
309 return JSValueMakeNull(context
);
312 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
313 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
316 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
317 return CYCastJSValue(context
, CYJSString(value
));
320 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
321 JSValueRef
exception(NULL
);
322 JSObjectRef
object(JSValueToObject(context
, value
, &exception
));
323 CYThrow(context
, exception
);
327 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
328 JSValueRef
exception(NULL
);
329 JSValueRef
value(JSObjectCallAsFunction(context
, function
, _this
, count
, arguments
, &exception
));
330 CYThrow(context
, exception
);
334 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
335 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
338 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
343 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
346 return CYJSUndefined(context
);
349 static size_t Nonce_(0);
351 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
353 const char *name(apr_psprintf(pool
, "%s%"APR_SIZE_T_FMT
"", CYPoolCString(pool
, context
, arguments
[0]), Nonce_
++));
354 return CYCastJSValue(context
, name
);
357 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
358 JSGarbageCollect(context
);
359 return CYJSUndefined(context
);
362 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
363 switch (JSType type
= JSValueGetType(context
, value
)) {
364 case kJSTypeUndefined
:
369 return CYCastBool(context
, value
) ? "true" : "false";
371 case kJSTypeNumber
: {
372 std::ostringstream str
;
373 CYNumerify(str
, CYCastDouble(context
, value
));
374 std::string
value(str
.str());
375 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
378 case kJSTypeString
: {
379 std::ostringstream str
;
380 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
381 CYStringify(str
, string
.data
, string
.size
);
382 std::string
value(str
.str());
383 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
387 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
389 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
393 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
) {
394 JSValueRef
exception(NULL
);
395 const char *cyon(CYPoolCCYON(pool
, context
, value
, &exception
));
396 CYThrow(context
, exception
);
400 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSObjectRef object
) {
401 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
402 if (CYIsCallable(context
, toCYON
)) {
403 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
404 _assert(value
!= NULL
);
405 return CYPoolCString(pool
, context
, value
);
408 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
409 if (CYIsCallable(context
, toJSON
)) {
410 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
411 JSValueRef
exception(NULL
);
412 const char *cyon(CYPoolCCYON(pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), &exception
));
413 CYThrow(context
, exception
);
417 std::ostringstream str
;
421 // XXX: this is, sadly, going to leak
422 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
426 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
427 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
428 JSValueRef
value(CYGetProperty(context
, object
, name
));
435 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
439 CYStringify(str
, string
.data
, string
.size
);
441 str
<< ':' << CYPoolCCYON(pool
, context
, value
);
446 JSPropertyNameArrayRelease(names
);
448 std::string
string(str
.str());
449 return apr_pstrmemdup(pool
, string
.c_str(), string
.size());
452 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
454 std::ostringstream str
;
458 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
461 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
462 JSValueRef
value(CYGetProperty(context
, _this
, index
));
469 if (!JSValueIsUndefined(context
, value
))
470 str
<< CYPoolCCYON(pool
, context
, value
);
479 std::string
value(str
.str());
480 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
483 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
484 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
485 return JSObjectMake(context
, Pointer_
, internal
);
488 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const char *type
) {
489 cy::Functor
*internal(new cy::Functor(type
, function
));
490 return JSObjectMake(context
, Functor_
, internal
);
493 static bool CYGetOffset(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
494 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
497 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
498 switch (JSValueGetType(context
, value
)) {
501 /*case kJSTypeObject:
502 if (JSValueIsObjectOfClass(context, value, Pointer_)) {
503 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value)));
504 return internal->value_;
507 double number(CYCastDouble(context
, value
));
508 if (std::isnan(number
))
509 throw CYJSError(context
, "cannot convert value to pointer");
510 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
514 void CYPoolFFI(apr_pool_t
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
515 switch (type
->primitive
) {
517 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
520 #define CYPoolFFI_(primitive, native) \
521 case sig::primitive ## _P: \
522 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
525 CYPoolFFI_(uchar
, unsigned char)
526 CYPoolFFI_(char, char)
527 CYPoolFFI_(ushort
, unsigned short)
528 CYPoolFFI_(short, short)
529 CYPoolFFI_(ulong
, unsigned long)
530 CYPoolFFI_(long, long)
531 CYPoolFFI_(uint
, unsigned int)
533 CYPoolFFI_(ulonglong
, unsigned long long)
534 CYPoolFFI_(longlong
, long long)
535 CYPoolFFI_(float, float)
536 CYPoolFFI_(double, double)
539 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
540 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
541 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
542 ffi_type
*field(ffi
->elements
[index
]);
545 if (aggregate
== NULL
)
548 rhs
= CYGetProperty(context
, aggregate
, index
);
549 if (JSValueIsUndefined(context
, rhs
))
550 throw CYJSError(context
, "unable to extract array value");
553 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
560 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
564 *reinterpret_cast<const char **>(data
) = CYPoolCString(pool
, context
, value
);
567 case sig::struct_P
: {
568 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
569 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
570 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
571 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
572 ffi_type
*field(ffi
->elements
[index
]);
575 if (aggregate
== NULL
)
578 rhs
= CYGetProperty(context
, aggregate
, index
);
579 if (JSValueIsUndefined(context
, rhs
)) {
580 if (element
->name
!= NULL
)
581 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
584 if (JSValueIsUndefined(context
, rhs
)) undefined
:
585 throw CYJSError(context
, "unable to extract structure value");
589 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
599 if (hooks_
!= NULL
&& hooks_
->PoolFFI
!= NULL
)
600 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
603 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
607 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
608 switch (type
->primitive
) {
610 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
612 #define CYFromFFI_(primitive, native) \
613 case sig::primitive ## _P: \
614 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
616 CYFromFFI_(uchar, unsigned char)
617 CYFromFFI_(char, char)
618 CYFromFFI_(ushort
, unsigned short)
619 CYFromFFI_(short, short)
620 CYFromFFI_(ulong
, unsigned long)
621 CYFromFFI_(long, long)
622 CYFromFFI_(uint
, unsigned int)
624 CYFromFFI_(ulonglong
, unsigned long long)
625 CYFromFFI_(longlong
, long long)
626 CYFromFFI_(float, float)
627 CYFromFFI_(double, double)
630 if (void *pointer
= data
)
631 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
635 if (void *pointer
= *reinterpret_cast<void **>(data
))
636 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
640 if (char *utf8
= *reinterpret_cast<char **>(data
))
641 return CYCastJSValue(context
, utf8
);
645 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
647 return CYJSUndefined(context
);
650 return CYJSNull(context
);
652 if (hooks_
!= NULL
&& hooks_
->FromFFI
!= NULL
)
653 if (JSValueRef value
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
656 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
660 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
661 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
663 JSContextRef
context(internal
->context_
);
665 size_t count(internal
->cif_
.nargs
);
666 JSValueRef values
[count
];
668 for (size_t index(0); index
!= count
; ++index
)
669 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
671 JSValueRef
value(CYCallAsFunction(context
, internal
->function_
, NULL
, count
, values
));
672 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
675 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const char *type
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
676 // XXX: in case of exceptions this will leak
677 // XXX: in point of fact, this may /need/ to leak :(
678 Closure_privateData
*internal(new Closure_privateData(context
, function
, type
));
680 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
681 NULL
, sizeof(ffi_closure
),
682 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
686 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
687 _assert(status
== FFI_OK
);
689 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
691 internal
->value_
= closure
;
696 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const char *type
) {
697 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, type
, &FunctionClosure_
));
698 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
699 // XXX: see above notes about needing to leak
700 JSValueProtect(CYGetJSContext(context
), object
);
704 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
705 return CYCastJSObject(context
, CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
));
708 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const char *type
) {
709 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
711 JSValueRef
exception(NULL
);
712 bool function(JSValueIsInstanceOfConstructor(context
, value
, Function
, &exception
));
713 CYThrow(context
, exception
);
716 JSObjectRef
function(CYCastJSObject(context
, value
));
717 return CYMakeFunctor(context
, function
, type
);
719 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
720 return CYMakeFunctor(context
, function
, type
);
724 static bool Index_(apr_pool_t
*pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
725 Type_privateData
*typical(internal
->type_
);
726 sig::Type
*type(typical
->type_
);
730 const char *name(CYPoolCString(pool
, context
, property
));
731 size_t length(strlen(name
));
732 double number(CYCastDouble(name
, length
));
734 size_t count(type
->data
.signature
.count
);
736 if (std::isnan(number
)) {
737 if (property
== NULL
)
740 sig::Element
*elements(type
->data
.signature
.elements
);
742 for (size_t local(0); local
!= count
; ++local
) {
743 sig::Element
*element(&elements
[local
]);
744 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
752 index
= static_cast<ssize_t
>(number
);
753 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
758 ffi_type
**elements(typical
->GetFFI()->elements
);
760 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
761 for (ssize_t
local(0); local
!= index
; ++local
)
762 base
+= elements
[local
]->size
;
767 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
769 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
771 if (JSStringIsEqual(property
, length_s
))
772 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
774 Type_privateData
*typical(internal
->type_
);
776 if (typical
->type_
== NULL
)
780 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
782 else if (!CYGetOffset(pool
, context
, property
, offset
))
785 ffi_type
*ffi(typical
->GetFFI());
787 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
788 base
+= ffi
->size
* offset
;
790 JSObjectRef
owner(internal
->GetOwner() ?: object
);
791 return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
);
794 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
796 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
797 Type_privateData
*typical(internal
->type_
);
799 if (typical
->type_
== NULL
)
803 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
805 else if (!CYGetOffset(pool
, context
, property
, offset
))
808 ffi_type
*ffi(typical
->GetFFI());
810 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
811 base
+= ffi
->size
* offset
;
813 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
817 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
818 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
819 Type_privateData
*typical(internal
->type_
);
820 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
823 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
825 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
826 Type_privateData
*typical(internal
->type_
);
831 if (!Index_(pool
, context
, internal
, property
, index
, base
))
834 JSObjectRef
owner(internal
->GetOwner() ?: object
);
836 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
839 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
841 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
842 Type_privateData
*typical(internal
->type_
);
847 if (!Index_(pool
, context
, internal
, property
, index
, base
))
850 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
854 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
855 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
856 Type_privateData
*typical(internal
->type_
);
857 sig::Type
*type(typical
->type_
);
862 size_t count(type
->data
.signature
.count
);
863 sig::Element
*elements(type
->data
.signature
.elements
);
867 for (size_t index(0); index
!= count
; ++index
) {
869 name
= elements
[index
].name
;
872 sprintf(number
, "%zu", index
);
876 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
880 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
{
881 if (setups
+ count
!= signature
->count
- 1)
882 throw CYJSError(context
, "incorrect number of arguments to ffi function");
884 size_t size(setups
+ count
);
886 memcpy(values
, setup
, sizeof(void *) * setups
);
888 for (size_t index(setups
); index
!= size
; ++index
) {
889 sig::Element
*element(&signature
->elements
[index
+ 1]);
890 ffi_type
*ffi(cif
->arg_types
[index
]);
892 values
[index
] = new(pool
) uint8_t[ffi
->size
];
893 CYPoolFFI(pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
896 uint8_t value
[cif
->rtype
->size
];
898 if (hooks_
!= NULL
&& hooks_
->CallFunction
!= NULL
)
899 (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
);
901 ffi_call(cif
, function
, value
, values
);
903 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
906 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
908 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
909 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, exception
, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
912 static JSObjectRef
CYMakeType(JSContextRef context
, const char *type
) {
913 Type_privateData
*internal(new Type_privateData(type
));
914 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
917 static JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
918 Type_privateData
*internal(new Type_privateData(type
));
919 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
922 static void *CYCastSymbol(const char *name
) {
923 return dlsym(RTLD_DEFAULT
, name
);
926 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
927 JSObjectRef
global(CYGetGlobalObject(context
));
928 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
929 if (JSValueRef value
= CYGetProperty(context
, cycript
, property
))
930 if (!JSValueIsUndefined(context
, value
))
934 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
936 if (hooks_
!= NULL
&& hooks_
->RuntimeProperty
!= NULL
)
937 if (JSValueRef value
= (*hooks_
->RuntimeProperty
)(context
, name
))
940 size_t length(name
.size
);
941 char keyed
[length
+ 2];
942 memcpy(keyed
+ 1, name
.data
, length
+ 1);
944 static const char *modes
= "0124";
945 for (size_t i(0); i
!= 4; ++i
) {
949 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
952 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
);
955 if (void (*symbol
)() = reinterpret_cast<void (*)()>(CYCastSymbol(name
.data
)))
956 return CYMakeFunctor(context
, symbol
, entry
->value_
);
960 if (void *symbol
= CYCastSymbol(name
.data
)) {
961 // XXX: this is horrendously inefficient
962 sig::Signature signature
;
963 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
965 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
966 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
969 // XXX: implement case 3
971 return CYMakeType(context
, entry
->value_
);
978 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
980 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
984 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
985 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
987 sig::Signature signature
;
988 sig::Parse(pool
, &signature
, type
, &Structor_
);
990 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
993 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
995 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
997 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
998 return CYMakeType(context
, type
);
1001 static JSValueRef
Type_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1002 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1006 if (JSStringIsEqualToUTF8CString(property
, "$cyi")) {
1007 type
.primitive
= sig::pointer_P
;
1008 type
.data
.data
.size
= 0;
1011 size_t index(CYGetIndex(pool
, context
, property
));
1012 if (index
== _not(size_t))
1014 type
.primitive
= sig::array_P
;
1015 type
.data
.data
.size
= index
;
1021 type
.data
.data
.type
= internal
->type_
;
1023 return CYMakeType(context
, &type
);
1026 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1027 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1030 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1031 sig::Type
*type(internal
->type_
);
1032 ffi_type
*ffi(internal
->GetFFI());
1034 uint8_t value
[ffi
->size
];
1036 CYPoolFFI(pool
, context
, type
, ffi
, value
, arguments
[0]);
1037 return CYFromFFI(context
, type
, ffi
, value
);
1040 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1042 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1043 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1045 sig::Type
*type(internal
->type_
);
1048 if (type
->primitive
!= sig::array_P
)
1049 length
= _not(size_t);
1051 length
= type
->data
.data
.size
;
1052 type
= type
->data
.data
.type
;
1055 void *value(malloc(internal
->GetFFI()->size
));
1056 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1059 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1061 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1063 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1064 return CYMakeFunctor(context
, arguments
[0], type
);
1067 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1068 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1069 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1072 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1073 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1076 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1077 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1079 sprintf(string
, "%p", internal
->value_
);
1080 return CYCastJSValue(context
, string
);
1083 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1084 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1085 if (internal
->length_
!= _not(size_t)) {
1086 JSObjectRef
Array(CYGetCachedObject(context
, Array_s
));
1087 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1088 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1091 sprintf(string
, "%p", internal
->value_
);
1092 return CYCastJSValue(context
, string
);
1096 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1097 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1099 const char *type(sig::Unparse(pool
, internal
->type_
));
1100 return CYCastJSValue(context
, CYJSString(type
));
1103 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1104 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1106 const char *type(sig::Unparse(pool
, internal
->type_
));
1107 size_t size(strlen(type
));
1108 char *cyon(new(pool
) char[12 + size
+ 1]);
1109 memcpy(cyon
, "new Type(\"", 10);
1110 cyon
[12 + size
] = '\0';
1111 cyon
[12 + size
- 2] = '"';
1112 cyon
[12 + size
- 1] = ')';
1113 memcpy(cyon
+ 10, type
, size
);
1114 return CYCastJSValue(context
, CYJSString(cyon
));
1117 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1118 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1121 static JSStaticFunction Pointer_staticFunctions
[4] = {
1122 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1123 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1124 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1128 static JSStaticFunction Struct_staticFunctions
[2] = {
1129 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1133 static JSStaticFunction Functor_staticFunctions
[4] = {
1134 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1135 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1136 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1141 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1144 static JSStaticFunction Type_staticFunctions
[4] = {
1145 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1146 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1147 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1151 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1153 void CYSetArgs(int argc
, const char *argv
[]) {
1154 JSContextRef
context(CYGetJSContext());
1155 JSValueRef args
[argc
];
1156 for (int i(0); i
!= argc
; ++i
)
1157 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1160 if (JSObjectMakeArray$
!= NULL
) {
1161 JSValueRef
exception(NULL
);
1162 array
= (*JSObjectMakeArray$
)(context
, argc
, args
, &exception
);
1163 CYThrow(context
, exception
);
1165 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1166 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1167 array
= CYCastJSObject(context
, value
);
1170 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1171 CYSetProperty(context
, System
, CYJSString("args"), array
);
1174 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1175 return JSContextGetGlobalObject(context
);
1178 const char *CYExecute(apr_pool_t
*pool
, const char *code
) {
1179 JSContextRef
context(CYGetJSContext());
1180 JSValueRef
exception(NULL
), result
;
1183 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1184 handle
= (*hooks_
->ExecuteStart
)(context
);
1191 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1192 } catch (const char *error
) {
1196 if (exception
!= NULL
) { error
:
1201 if (JSValueIsUndefined(context
, result
))
1205 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1206 } catch (const char *error
) {
1210 if (exception
!= NULL
)
1213 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1215 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1216 (*hooks_
->ExecuteEnd
)(context
, handle
);
1220 extern "C" void CydgetSetupContext(JSGlobalContextRef context
) {
1221 CYSetupContext(context
);
1224 static bool initialized_
= false;
1226 void CYInitializeDynamic() {
1228 initialized_
= true;
1231 CYInitializeStatic();
1233 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1235 JSClassDefinition definition
;
1237 definition
= kJSClassDefinitionEmpty
;
1238 definition
.className
= "All";
1239 definition
.getProperty
= &All_getProperty
;
1240 All_
= JSClassCreate(&definition
);
1242 definition
= kJSClassDefinitionEmpty
;
1243 definition
.className
= "Context";
1244 definition
.finalize
= &CYFinalize
;
1245 Context_
= JSClassCreate(&definition
);
1247 definition
= kJSClassDefinitionEmpty
;
1248 definition
.className
= "Functor";
1249 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1250 definition
.callAsFunction
= &Functor_callAsFunction
;
1251 definition
.finalize
= &CYFinalize
;
1252 Functor_
= JSClassCreate(&definition
);
1254 definition
= kJSClassDefinitionEmpty
;
1255 definition
.className
= "Pointer";
1256 definition
.staticFunctions
= Pointer_staticFunctions
;
1257 definition
.getProperty
= &Pointer_getProperty
;
1258 definition
.setProperty
= &Pointer_setProperty
;
1259 definition
.finalize
= &CYFinalize
;
1260 Pointer_
= JSClassCreate(&definition
);
1262 definition
= kJSClassDefinitionEmpty
;
1263 definition
.className
= "Struct";
1264 definition
.staticFunctions
= Struct_staticFunctions
;
1265 definition
.getProperty
= &Struct_getProperty
;
1266 definition
.setProperty
= &Struct_setProperty
;
1267 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1268 definition
.finalize
= &CYFinalize
;
1269 Struct_
= JSClassCreate(&definition
);
1271 definition
= kJSClassDefinitionEmpty
;
1272 definition
.className
= "Type";
1273 definition
.staticFunctions
= Type_staticFunctions
;
1274 definition
.getProperty
= &Type_getProperty
;
1275 definition
.callAsFunction
= &Type_callAsFunction
;
1276 definition
.callAsConstructor
= &Type_callAsConstructor
;
1277 definition
.finalize
= &CYFinalize
;
1278 Type_privateData::Class_
= JSClassCreate(&definition
);
1280 definition
= kJSClassDefinitionEmpty
;
1281 //definition.getProperty = &Global_getProperty;
1282 Global_
= JSClassCreate(&definition
);
1284 Array_s
= JSStringCreateWithUTF8CString("Array");
1285 cy_s
= JSStringCreateWithUTF8CString("$cy");
1286 length_s
= JSStringCreateWithUTF8CString("length");
1287 message_s
= JSStringCreateWithUTF8CString("message");
1288 name_s
= JSStringCreateWithUTF8CString("name");
1289 pop_s
= JSStringCreateWithUTF8CString("pop");
1290 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1291 push_s
= JSStringCreateWithUTF8CString("push");
1292 splice_s
= JSStringCreateWithUTF8CString("splice");
1293 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1294 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1296 Result_
= JSStringCreateWithUTF8CString("_");
1298 if (hooks_
!= NULL
&& hooks_
->Initialize
!= NULL
)
1299 (*hooks_
->Initialize
)();
1302 void CYThrow(JSContextRef context
, JSValueRef value
) {
1304 throw CYJSError(context
, value
);
1307 const char *CYJSError::PoolCString(apr_pool_t
*pool
) const {
1308 // XXX: this used to be CYPoolCString
1309 return CYPoolCCYON(pool
, context_
, value_
);
1312 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1313 // XXX: what if the context is different?
1317 JSValueRef
CYCastJSError(JSContextRef context
, const char *message
) {
1318 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString("Error")));
1320 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1322 JSValueRef
exception(NULL
);
1323 JSValueRef
value(JSObjectCallAsConstructor(context
, Error
, 1, arguments
, &exception
));
1324 CYThrow(context
, exception
);
1329 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1330 return CYCastJSError(context
, message_
);
1333 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1334 _assert(context
!= NULL
);
1339 va_start(args
, format
);
1340 const char *message(apr_pvsprintf(pool
, format
, args
));
1343 value_
= CYCastJSError(context
, message
);
1346 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1347 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1350 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
1351 CYInitializeDynamic();
1353 JSObjectRef
global(CYGetGlobalObject(context
));
1355 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
1356 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
1358 /* Cache Globals {{{ */
1359 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
1360 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
1362 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
1363 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
1365 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
1366 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
1368 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
1369 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
1371 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
1372 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
1374 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1375 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
1377 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
1378 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
1380 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
1381 CYSetProperty(context
, cy
, CYJSString("String"), String
);
1384 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1386 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1387 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1388 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
1390 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1391 JSObjectSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
1392 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
1394 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1395 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1397 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
1398 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
1400 JSObjectRef
last(NULL
), curr(global
);
1402 goto next
; for (JSValueRef next
;;) {
1403 if (JSValueIsNull(context
, next
))
1406 curr
= CYCastJSObject(context
, next
);
1408 next
= JSObjectGetPrototype(context
, curr
);
1411 JSObjectSetPrototype(context
, last
, all
);
1413 CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
);
1415 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
1416 CYSetProperty(context
, cy
, CYJSString("System"), Function
);
1418 CYSetProperty(context
, global
, CYJSString("system"), System
);
1419 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
1420 //CYSetProperty(context, System, CYJSString("global"), global);
1421 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
1423 if (hooks_
!= NULL
&& hooks_
->SetupContext
!= NULL
)
1424 (*hooks_
->SetupContext
)(context
);
1427 JSGlobalContextRef
CYGetJSContext() {
1428 CYInitializeDynamic();
1430 static JSGlobalContextRef context_
;
1432 if (context_
== NULL
) {
1433 context_
= JSGlobalContextCreate(Global_
);
1434 CYSetupContext(context_
);