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 type
->data
.data
.type
->name
!= NULL
&&
181 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
183 type
->primitive
= sig::typename_P
;
184 type
->data
.data
.type
= NULL
;
188 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
191 size_t length(strlen(type
->name
));
192 char keyed
[length
+ 2];
193 memcpy(keyed
+ 1, type
->name
, length
+ 1);
195 static const char *modes
= "34";
196 for (size_t i(0); i
!= 2; ++i
) {
200 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
203 sig::Parse(pool
, &type
->data
.signature
, entry
->value_
, &Structor_
);
207 sig::Signature signature
;
208 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
209 type
= signature
.elements
[0].type
;
215 JSClassRef
Type_privateData::Class_
;
220 JSGlobalContextRef context_
;
222 Context(JSGlobalContextRef context
) :
231 Type_privateData
*type_
;
234 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type
*type
) :
235 CYOwned(value
, context
, owner
),
236 type_(new(pool_
) Type_privateData(type
)),
242 struct Struct_privateData
:
245 Type_privateData
*type_
;
247 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
248 CYOwned(NULL
, context
, owner
)
253 typedef std::map
<const char *, Type_privateData
*, CYCStringLess
> TypeMap
;
254 static TypeMap Types_
;
256 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
257 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
258 apr_pool_t
*pool(internal
->pool_
);
259 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
260 internal
->type_
= typical
;
263 internal
->value_
= data
;
265 size_t size(typical
->GetFFI()->size
);
266 void *copy(apr_palloc(internal
->pool_
, size
));
267 memcpy(copy
, data
, size
);
268 internal
->value_
= copy
;
271 return JSObjectMake(context
, Struct_
, internal
);
274 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
275 return JSValueMakeBoolean(context
, value
);
278 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
279 return JSValueMakeNumber(context
, value
);
282 #define CYCastJSValue_(Type_) \
283 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
284 return JSValueMakeNumber(context, static_cast<double>(value)); \
288 CYCastJSValue_(unsigned int)
289 CYCastJSValue_(long int)
290 CYCastJSValue_(long unsigned int)
291 CYCastJSValue_(long long int)
292 CYCastJSValue_(long long unsigned int)
294 JSValueRef
CYJSUndefined(JSContextRef context
) {
295 return JSValueMakeUndefined(context
);
298 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
299 JSValueRef
exception(NULL
);
300 double number(JSValueToNumber(context
, value
, &exception
));
301 CYThrow(context
, exception
);
305 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
306 return JSValueToBoolean(context
, value
);
309 JSValueRef
CYJSNull(JSContextRef context
) {
310 return JSValueMakeNull(context
);
313 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
314 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
317 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
318 return CYCastJSValue(context
, CYJSString(value
));
321 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
322 JSValueRef
exception(NULL
);
323 JSObjectRef
object(JSValueToObject(context
, value
, &exception
));
324 CYThrow(context
, exception
);
328 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
329 JSValueRef
exception(NULL
);
330 JSValueRef
value(JSObjectCallAsFunction(context
, function
, _this
, count
, arguments
, &exception
));
331 CYThrow(context
, exception
);
335 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
336 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
339 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
344 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
347 return CYJSUndefined(context
);
350 static size_t Nonce_(0);
352 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
354 const char *name(apr_psprintf(pool
, "%s%"APR_SIZE_T_FMT
"", CYPoolCString(pool
, context
, arguments
[0]), Nonce_
++));
355 return CYCastJSValue(context
, name
);
358 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
359 JSGarbageCollect(context
);
360 return CYJSUndefined(context
);
363 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
364 switch (JSType type
= JSValueGetType(context
, value
)) {
365 case kJSTypeUndefined
:
370 return CYCastBool(context
, value
) ? "true" : "false";
372 case kJSTypeNumber
: {
373 std::ostringstream str
;
374 CYNumerify(str
, CYCastDouble(context
, value
));
375 std::string
value(str
.str());
376 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
379 case kJSTypeString
: {
380 std::ostringstream str
;
381 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
382 CYStringify(str
, string
.data
, string
.size
);
383 std::string
value(str
.str());
384 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
388 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
390 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
394 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
) {
395 JSValueRef
exception(NULL
);
396 const char *cyon(CYPoolCCYON(pool
, context
, value
, &exception
));
397 CYThrow(context
, exception
);
401 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSObjectRef object
) {
402 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
403 if (CYIsCallable(context
, toCYON
)) {
404 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
405 _assert(value
!= NULL
);
406 return CYPoolCString(pool
, context
, value
);
409 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
410 if (CYIsCallable(context
, toJSON
)) {
411 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
412 JSValueRef
exception(NULL
);
413 const char *cyon(CYPoolCCYON(pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), &exception
));
414 CYThrow(context
, exception
);
418 std::ostringstream str
;
422 // XXX: this is, sadly, going to leak
423 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
427 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
428 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
429 JSValueRef
value(CYGetProperty(context
, object
, name
));
436 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
440 CYStringify(str
, string
.data
, string
.size
);
442 str
<< ':' << CYPoolCCYON(pool
, context
, value
);
447 JSPropertyNameArrayRelease(names
);
449 std::string
string(str
.str());
450 return apr_pstrmemdup(pool
, string
.c_str(), string
.size());
453 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
455 std::ostringstream str
;
459 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
462 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
463 JSValueRef
value(CYGetProperty(context
, _this
, index
));
470 if (!JSValueIsUndefined(context
, value
))
471 str
<< CYPoolCCYON(pool
, context
, value
);
480 std::string
value(str
.str());
481 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
484 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
485 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
486 return JSObjectMake(context
, Pointer_
, internal
);
489 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const char *type
) {
490 cy::Functor
*internal(new cy::Functor(type
, function
));
491 return JSObjectMake(context
, Functor_
, internal
);
494 static bool CYGetOffset(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
495 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
498 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
499 switch (JSValueGetType(context
, value
)) {
502 /*case kJSTypeObject:
503 if (JSValueIsObjectOfClass(context, value, Pointer_)) {
504 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value)));
505 return internal->value_;
508 double number(CYCastDouble(context
, value
));
509 if (std::isnan(number
))
510 throw CYJSError(context
, "cannot convert value to pointer");
511 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
515 void CYPoolFFI(apr_pool_t
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
516 switch (type
->primitive
) {
518 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
521 #define CYPoolFFI_(primitive, native) \
522 case sig::primitive ## _P: \
523 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
526 CYPoolFFI_(uchar
, unsigned char)
527 CYPoolFFI_(char, char)
528 CYPoolFFI_(ushort
, unsigned short)
529 CYPoolFFI_(short, short)
530 CYPoolFFI_(ulong
, unsigned long)
531 CYPoolFFI_(long, long)
532 CYPoolFFI_(uint
, unsigned int)
534 CYPoolFFI_(ulonglong
, unsigned long long)
535 CYPoolFFI_(longlong
, long long)
536 CYPoolFFI_(float, float)
537 CYPoolFFI_(double, double)
540 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
541 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
542 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
543 ffi_type
*field(ffi
->elements
[index
]);
546 if (aggregate
== NULL
)
549 rhs
= CYGetProperty(context
, aggregate
, index
);
550 if (JSValueIsUndefined(context
, rhs
))
551 throw CYJSError(context
, "unable to extract array value");
554 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
561 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
565 *reinterpret_cast<const char **>(data
) = CYPoolCString(pool
, context
, value
);
568 case sig::struct_P
: {
569 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
570 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
571 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
572 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
573 ffi_type
*field(ffi
->elements
[index
]);
576 if (aggregate
== NULL
)
579 rhs
= CYGetProperty(context
, aggregate
, index
);
580 if (JSValueIsUndefined(context
, rhs
)) {
581 if (element
->name
!= NULL
)
582 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
585 if (JSValueIsUndefined(context
, rhs
)) undefined
:
586 throw CYJSError(context
, "unable to extract structure value");
590 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
600 if (hooks_
!= NULL
&& hooks_
->PoolFFI
!= NULL
)
601 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
604 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
608 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
609 switch (type
->primitive
) {
611 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
613 #define CYFromFFI_(primitive, native) \
614 case sig::primitive ## _P: \
615 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
617 CYFromFFI_(uchar, unsigned char)
618 CYFromFFI_(char, char)
619 CYFromFFI_(ushort
, unsigned short)
620 CYFromFFI_(short, short)
621 CYFromFFI_(ulong
, unsigned long)
622 CYFromFFI_(long, long)
623 CYFromFFI_(uint
, unsigned int)
625 CYFromFFI_(ulonglong
, unsigned long long)
626 CYFromFFI_(longlong
, long long)
627 CYFromFFI_(float, float)
628 CYFromFFI_(double, double)
631 if (void *pointer
= data
)
632 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
636 if (void *pointer
= *reinterpret_cast<void **>(data
))
637 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
641 if (char *utf8
= *reinterpret_cast<char **>(data
))
642 return CYCastJSValue(context
, utf8
);
646 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
648 return CYJSUndefined(context
);
651 return CYJSNull(context
);
653 if (hooks_
!= NULL
&& hooks_
->FromFFI
!= NULL
)
654 if (JSValueRef value
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
657 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
661 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
662 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
664 JSContextRef
context(internal
->context_
);
666 size_t count(internal
->cif_
.nargs
);
667 JSValueRef values
[count
];
669 for (size_t index(0); index
!= count
; ++index
)
670 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
672 JSValueRef
value(CYCallAsFunction(context
, internal
->function_
, NULL
, count
, values
));
673 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
676 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const char *type
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
677 // XXX: in case of exceptions this will leak
678 // XXX: in point of fact, this may /need/ to leak :(
679 Closure_privateData
*internal(new Closure_privateData(context
, function
, type
));
681 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
682 NULL
, sizeof(ffi_closure
),
683 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
687 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
688 _assert(status
== FFI_OK
);
690 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
692 internal
->value_
= closure
;
697 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const char *type
) {
698 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, type
, &FunctionClosure_
));
699 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
700 // XXX: see above notes about needing to leak
701 JSValueProtect(CYGetJSContext(context
), object
);
705 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
706 return CYCastJSObject(context
, CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
));
709 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const char *type
) {
710 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
712 JSValueRef
exception(NULL
);
713 bool function(JSValueIsInstanceOfConstructor(context
, value
, Function
, &exception
));
714 CYThrow(context
, exception
);
717 JSObjectRef
function(CYCastJSObject(context
, value
));
718 return CYMakeFunctor(context
, function
, type
);
720 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
721 return CYMakeFunctor(context
, function
, type
);
725 static bool Index_(apr_pool_t
*pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
726 Type_privateData
*typical(internal
->type_
);
727 sig::Type
*type(typical
->type_
);
731 const char *name(CYPoolCString(pool
, context
, property
));
732 size_t length(strlen(name
));
733 double number(CYCastDouble(name
, length
));
735 size_t count(type
->data
.signature
.count
);
737 if (std::isnan(number
)) {
738 if (property
== NULL
)
741 sig::Element
*elements(type
->data
.signature
.elements
);
743 for (size_t local(0); local
!= count
; ++local
) {
744 sig::Element
*element(&elements
[local
]);
745 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
753 index
= static_cast<ssize_t
>(number
);
754 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
759 ffi_type
**elements(typical
->GetFFI()->elements
);
761 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
762 for (ssize_t
local(0); local
!= index
; ++local
)
763 base
+= elements
[local
]->size
;
768 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
770 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
772 if (JSStringIsEqual(property
, length_s
))
773 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
775 Type_privateData
*typical(internal
->type_
);
777 if (typical
->type_
== NULL
)
781 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
783 else if (!CYGetOffset(pool
, context
, property
, offset
))
786 ffi_type
*ffi(typical
->GetFFI());
788 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
789 base
+= ffi
->size
* offset
;
791 JSObjectRef
owner(internal
->GetOwner() ?: object
);
792 return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
);
795 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
797 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
798 Type_privateData
*typical(internal
->type_
);
800 if (typical
->type_
== NULL
)
804 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
806 else if (!CYGetOffset(pool
, context
, property
, offset
))
809 ffi_type
*ffi(typical
->GetFFI());
811 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
812 base
+= ffi
->size
* offset
;
814 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
818 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
819 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
820 Type_privateData
*typical(internal
->type_
);
821 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
824 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
826 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
827 Type_privateData
*typical(internal
->type_
);
832 if (!Index_(pool
, context
, internal
, property
, index
, base
))
835 JSObjectRef
owner(internal
->GetOwner() ?: object
);
837 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
840 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
842 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
843 Type_privateData
*typical(internal
->type_
);
848 if (!Index_(pool
, context
, internal
, property
, index
, base
))
851 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
855 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
856 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
857 Type_privateData
*typical(internal
->type_
);
858 sig::Type
*type(typical
->type_
);
863 size_t count(type
->data
.signature
.count
);
864 sig::Element
*elements(type
->data
.signature
.elements
);
868 for (size_t index(0); index
!= count
; ++index
) {
870 name
= elements
[index
].name
;
873 sprintf(number
, "%zu", index
);
877 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
881 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
{
882 if (setups
+ count
!= signature
->count
- 1)
883 throw CYJSError(context
, "incorrect number of arguments to ffi function");
885 size_t size(setups
+ count
);
887 memcpy(values
, setup
, sizeof(void *) * setups
);
889 for (size_t index(setups
); index
!= size
; ++index
) {
890 sig::Element
*element(&signature
->elements
[index
+ 1]);
891 ffi_type
*ffi(cif
->arg_types
[index
]);
893 values
[index
] = new(pool
) uint8_t[ffi
->size
];
894 CYPoolFFI(pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
897 uint8_t value
[cif
->rtype
->size
];
899 if (hooks_
!= NULL
&& hooks_
->CallFunction
!= NULL
)
900 (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
);
902 ffi_call(cif
, function
, value
, values
);
904 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
907 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
909 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
910 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, exception
, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
913 static JSObjectRef
CYMakeType(JSContextRef context
, const char *type
) {
914 Type_privateData
*internal(new Type_privateData(type
));
915 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
918 static JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
919 Type_privateData
*internal(new Type_privateData(type
));
920 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
923 static void *CYCastSymbol(const char *name
) {
924 return dlsym(RTLD_DEFAULT
, name
);
927 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
928 JSObjectRef
global(CYGetGlobalObject(context
));
929 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
930 if (JSValueRef value
= CYGetProperty(context
, cycript
, property
))
931 if (!JSValueIsUndefined(context
, value
))
935 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
937 if (hooks_
!= NULL
&& hooks_
->RuntimeProperty
!= NULL
)
938 if (JSValueRef value
= (*hooks_
->RuntimeProperty
)(context
, name
))
941 size_t length(name
.size
);
942 char keyed
[length
+ 2];
943 memcpy(keyed
+ 1, name
.data
, length
+ 1);
945 static const char *modes
= "0124";
946 for (size_t i(0); i
!= 4; ++i
) {
950 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
953 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
);
956 if (void (*symbol
)() = reinterpret_cast<void (*)()>(CYCastSymbol(name
.data
)))
957 return CYMakeFunctor(context
, symbol
, entry
->value_
);
961 if (void *symbol
= CYCastSymbol(name
.data
)) {
962 // XXX: this is horrendously inefficient
963 sig::Signature signature
;
964 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
966 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
967 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
970 // XXX: implement case 3
972 return CYMakeType(context
, entry
->value_
);
979 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
981 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
985 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
986 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
988 sig::Signature signature
;
989 sig::Parse(pool
, &signature
, type
, &Structor_
);
991 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
994 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
996 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
998 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
999 return CYMakeType(context
, type
);
1002 static JSValueRef
Type_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1003 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1007 if (JSStringIsEqualToUTF8CString(property
, "$cyi")) {
1008 type
.primitive
= sig::pointer_P
;
1009 type
.data
.data
.size
= 0;
1012 size_t index(CYGetIndex(pool
, context
, property
));
1013 if (index
== _not(size_t))
1015 type
.primitive
= sig::array_P
;
1016 type
.data
.data
.size
= index
;
1022 type
.data
.data
.type
= internal
->type_
;
1024 return CYMakeType(context
, &type
);
1027 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1028 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1031 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1032 sig::Type
*type(internal
->type_
);
1033 ffi_type
*ffi(internal
->GetFFI());
1035 uint8_t value
[ffi
->size
];
1037 CYPoolFFI(pool
, context
, type
, ffi
, value
, arguments
[0]);
1038 return CYFromFFI(context
, type
, ffi
, value
);
1041 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1043 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1044 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1046 sig::Type
*type(internal
->type_
);
1049 if (type
->primitive
!= sig::array_P
)
1050 length
= _not(size_t);
1052 length
= type
->data
.data
.size
;
1053 type
= type
->data
.data
.type
;
1056 void *value(malloc(internal
->GetFFI()->size
));
1057 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1060 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1062 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1064 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1065 return CYMakeFunctor(context
, arguments
[0], type
);
1068 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1069 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1070 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1073 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1074 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1077 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1078 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1080 sprintf(string
, "%p", internal
->value_
);
1081 return CYCastJSValue(context
, string
);
1084 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1085 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1086 if (internal
->length_
!= _not(size_t)) {
1087 JSObjectRef
Array(CYGetCachedObject(context
, Array_s
));
1088 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1089 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1092 sprintf(string
, "%p", internal
->value_
);
1093 return CYCastJSValue(context
, string
);
1097 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1098 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1100 const char *type(sig::Unparse(pool
, internal
->type_
));
1101 return CYCastJSValue(context
, CYJSString(type
));
1104 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1105 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1107 const char *type(sig::Unparse(pool
, internal
->type_
));
1108 size_t size(strlen(type
));
1109 char *cyon(new(pool
) char[12 + size
+ 1]);
1110 memcpy(cyon
, "new Type(\"", 10);
1111 cyon
[12 + size
] = '\0';
1112 cyon
[12 + size
- 2] = '"';
1113 cyon
[12 + size
- 1] = ')';
1114 memcpy(cyon
+ 10, type
, size
);
1115 return CYCastJSValue(context
, CYJSString(cyon
));
1118 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1119 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1122 static JSStaticFunction Pointer_staticFunctions
[4] = {
1123 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1124 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1125 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1129 static JSStaticFunction Struct_staticFunctions
[2] = {
1130 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1134 static JSStaticFunction Functor_staticFunctions
[4] = {
1135 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1136 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1137 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1142 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1145 static JSStaticFunction Type_staticFunctions
[4] = {
1146 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1147 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1148 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1152 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1154 void CYSetArgs(int argc
, const char *argv
[]) {
1155 JSContextRef
context(CYGetJSContext());
1156 JSValueRef args
[argc
];
1157 for (int i(0); i
!= argc
; ++i
)
1158 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1161 if (JSObjectMakeArray$
!= NULL
) {
1162 JSValueRef
exception(NULL
);
1163 array
= (*JSObjectMakeArray$
)(context
, argc
, args
, &exception
);
1164 CYThrow(context
, exception
);
1166 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1167 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1168 array
= CYCastJSObject(context
, value
);
1171 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1172 CYSetProperty(context
, System
, CYJSString("args"), array
);
1175 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1176 return JSContextGetGlobalObject(context
);
1179 const char *CYExecute(apr_pool_t
*pool
, const char *code
) {
1180 JSContextRef
context(CYGetJSContext());
1181 JSValueRef
exception(NULL
), result
;
1184 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1185 handle
= (*hooks_
->ExecuteStart
)(context
);
1192 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1193 } catch (const char *error
) {
1197 if (exception
!= NULL
) { error
:
1202 if (JSValueIsUndefined(context
, result
))
1206 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1207 } catch (const char *error
) {
1211 if (exception
!= NULL
)
1214 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1216 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1217 (*hooks_
->ExecuteEnd
)(context
, handle
);
1221 extern "C" void CydgetSetupContext(JSGlobalContextRef context
) {
1222 CYSetupContext(context
);
1225 static bool initialized_
= false;
1227 void CYInitializeDynamic() {
1229 initialized_
= true;
1232 CYInitializeStatic();
1234 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1236 JSClassDefinition definition
;
1238 definition
= kJSClassDefinitionEmpty
;
1239 definition
.className
= "All";
1240 definition
.getProperty
= &All_getProperty
;
1241 All_
= JSClassCreate(&definition
);
1243 definition
= kJSClassDefinitionEmpty
;
1244 definition
.className
= "Context";
1245 definition
.finalize
= &CYFinalize
;
1246 Context_
= JSClassCreate(&definition
);
1248 definition
= kJSClassDefinitionEmpty
;
1249 definition
.className
= "Functor";
1250 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1251 definition
.callAsFunction
= &Functor_callAsFunction
;
1252 definition
.finalize
= &CYFinalize
;
1253 Functor_
= JSClassCreate(&definition
);
1255 definition
= kJSClassDefinitionEmpty
;
1256 definition
.className
= "Pointer";
1257 definition
.staticFunctions
= Pointer_staticFunctions
;
1258 definition
.getProperty
= &Pointer_getProperty
;
1259 definition
.setProperty
= &Pointer_setProperty
;
1260 definition
.finalize
= &CYFinalize
;
1261 Pointer_
= JSClassCreate(&definition
);
1263 definition
= kJSClassDefinitionEmpty
;
1264 definition
.className
= "Struct";
1265 definition
.staticFunctions
= Struct_staticFunctions
;
1266 definition
.getProperty
= &Struct_getProperty
;
1267 definition
.setProperty
= &Struct_setProperty
;
1268 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1269 definition
.finalize
= &CYFinalize
;
1270 Struct_
= JSClassCreate(&definition
);
1272 definition
= kJSClassDefinitionEmpty
;
1273 definition
.className
= "Type";
1274 definition
.staticFunctions
= Type_staticFunctions
;
1275 definition
.getProperty
= &Type_getProperty
;
1276 definition
.callAsFunction
= &Type_callAsFunction
;
1277 definition
.callAsConstructor
= &Type_callAsConstructor
;
1278 definition
.finalize
= &CYFinalize
;
1279 Type_privateData::Class_
= JSClassCreate(&definition
);
1281 definition
= kJSClassDefinitionEmpty
;
1282 //definition.getProperty = &Global_getProperty;
1283 Global_
= JSClassCreate(&definition
);
1285 Array_s
= JSStringCreateWithUTF8CString("Array");
1286 cy_s
= JSStringCreateWithUTF8CString("$cy");
1287 length_s
= JSStringCreateWithUTF8CString("length");
1288 message_s
= JSStringCreateWithUTF8CString("message");
1289 name_s
= JSStringCreateWithUTF8CString("name");
1290 pop_s
= JSStringCreateWithUTF8CString("pop");
1291 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1292 push_s
= JSStringCreateWithUTF8CString("push");
1293 splice_s
= JSStringCreateWithUTF8CString("splice");
1294 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1295 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1297 Result_
= JSStringCreateWithUTF8CString("_");
1299 if (hooks_
!= NULL
&& hooks_
->Initialize
!= NULL
)
1300 (*hooks_
->Initialize
)();
1303 void CYThrow(JSContextRef context
, JSValueRef value
) {
1305 throw CYJSError(context
, value
);
1308 const char *CYJSError::PoolCString(apr_pool_t
*pool
) const {
1309 // XXX: this used to be CYPoolCString
1310 return CYPoolCCYON(pool
, context_
, value_
);
1313 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1314 // XXX: what if the context is different?
1318 JSValueRef
CYCastJSError(JSContextRef context
, const char *message
) {
1319 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString("Error")));
1321 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1323 JSValueRef
exception(NULL
);
1324 JSValueRef
value(JSObjectCallAsConstructor(context
, Error
, 1, arguments
, &exception
));
1325 CYThrow(context
, exception
);
1330 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1331 return CYCastJSError(context
, message_
);
1334 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1335 _assert(context
!= NULL
);
1340 va_start(args
, format
);
1341 const char *message(apr_pvsprintf(pool
, format
, args
));
1344 value_
= CYCastJSError(context
, message
);
1347 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1348 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1351 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
1352 CYInitializeDynamic();
1354 JSObjectRef
global(CYGetGlobalObject(context
));
1356 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
1357 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
1359 /* Cache Globals {{{ */
1360 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
1361 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
1363 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
1364 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
1366 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
1367 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
1369 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
1370 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
1372 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
1373 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
1375 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1376 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
1378 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
1379 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
1381 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
1382 CYSetProperty(context
, cy
, CYJSString("String"), String
);
1385 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1387 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1388 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1389 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
1391 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1392 JSObjectSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
1393 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
1395 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1396 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1398 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
1399 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
1401 JSObjectRef
last(NULL
), curr(global
);
1403 goto next
; for (JSValueRef next
;;) {
1404 if (JSValueIsNull(context
, next
))
1407 curr
= CYCastJSObject(context
, next
);
1409 next
= JSObjectGetPrototype(context
, curr
);
1412 JSObjectSetPrototype(context
, last
, all
);
1414 CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
);
1416 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
1417 CYSetProperty(context
, cy
, CYJSString("System"), Function
);
1419 CYSetProperty(context
, global
, CYJSString("system"), System
);
1420 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
1421 //CYSetProperty(context, System, CYJSString("global"), global);
1422 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
1424 if (hooks_
!= NULL
&& hooks_
->SetupContext
!= NULL
)
1425 (*hooks_
->SetupContext
)(context
);
1428 JSGlobalContextRef
CYGetJSContext() {
1429 CYInitializeDynamic();
1431 static JSGlobalContextRef context_
;
1433 if (context_
== NULL
) {
1434 context_
= JSGlobalContextCreate(Global_
);
1435 CYSetupContext(context_
);