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 CYData
*internal(reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
)));
173 if (--internal
->count_
== 0)
177 void Structor_(apr_pool_t
*pool
, sig::Type
*&type
) {
179 type
->primitive
== sig::pointer_P
&&
180 type
->data
.data
.type
!= NULL
&&
181 type
->data
.data
.type
->primitive
== sig::struct_P
&&
182 type
->data
.data
.type
->name
!= NULL
&&
183 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
185 type
->primitive
= sig::typename_P
;
186 type
->data
.data
.type
= NULL
;
190 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
193 size_t length(strlen(type
->name
));
194 char keyed
[length
+ 2];
195 memcpy(keyed
+ 1, type
->name
, length
+ 1);
197 static const char *modes
= "34";
198 for (size_t i(0); i
!= 2; ++i
) {
202 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
205 sig::Parse(pool
, &type
->data
.signature
, entry
->value_
, &Structor_
);
209 sig::Signature signature
;
210 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
211 type
= signature
.elements
[0].type
;
217 JSClassRef
Type_privateData::Class_
;
222 JSGlobalContextRef context_
;
224 Context(JSGlobalContextRef context
) :
233 Type_privateData
*type_
;
236 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type
*type
) :
237 CYOwned(value
, context
, owner
),
238 type_(new(pool_
) Type_privateData(type
)),
244 struct Struct_privateData
:
247 Type_privateData
*type_
;
249 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
250 CYOwned(NULL
, context
, owner
)
255 typedef std::map
<const char *, Type_privateData
*, CYCStringLess
> TypeMap
;
256 static TypeMap Types_
;
258 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
259 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
260 apr_pool_t
*pool(internal
->pool_
);
261 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
262 internal
->type_
= typical
;
265 internal
->value_
= data
;
267 size_t size(typical
->GetFFI()->size
);
268 void *copy(apr_palloc(internal
->pool_
, size
));
269 memcpy(copy
, data
, size
);
270 internal
->value_
= copy
;
273 return JSObjectMake(context
, Struct_
, internal
);
276 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
277 return JSValueMakeBoolean(context
, value
);
280 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
281 return JSValueMakeNumber(context
, value
);
284 #define CYCastJSValue_(Type_) \
285 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
286 return JSValueMakeNumber(context, static_cast<double>(value)); \
290 CYCastJSValue_(unsigned int)
291 CYCastJSValue_(long int)
292 CYCastJSValue_(long unsigned int)
293 CYCastJSValue_(long long int)
294 CYCastJSValue_(long long unsigned int)
296 JSValueRef
CYJSUndefined(JSContextRef context
) {
297 return JSValueMakeUndefined(context
);
300 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
301 JSValueRef
exception(NULL
);
302 double number(JSValueToNumber(context
, value
, &exception
));
303 CYThrow(context
, exception
);
307 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
308 return JSValueToBoolean(context
, value
);
311 JSValueRef
CYJSNull(JSContextRef context
) {
312 return JSValueMakeNull(context
);
315 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
316 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
319 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
320 return CYCastJSValue(context
, CYJSString(value
));
323 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
324 JSValueRef
exception(NULL
);
325 JSObjectRef
object(JSValueToObject(context
, value
, &exception
));
326 CYThrow(context
, exception
);
330 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
331 JSValueRef
exception(NULL
);
332 JSValueRef
value(JSObjectCallAsFunction(context
, function
, _this
, count
, arguments
, &exception
));
333 CYThrow(context
, exception
);
337 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
338 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
341 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
346 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
349 return CYJSUndefined(context
);
352 static size_t Nonce_(0);
354 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
356 const char *name(apr_psprintf(pool
, "%s%"APR_SIZE_T_FMT
"", CYPoolCString(pool
, context
, arguments
[0]), Nonce_
++));
357 return CYCastJSValue(context
, name
);
360 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
361 JSGarbageCollect(context
);
362 return CYJSUndefined(context
);
365 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
366 switch (JSType type
= JSValueGetType(context
, value
)) {
367 case kJSTypeUndefined
:
372 return CYCastBool(context
, value
) ? "true" : "false";
374 case kJSTypeNumber
: {
375 std::ostringstream str
;
376 CYNumerify(str
, CYCastDouble(context
, value
));
377 std::string
value(str
.str());
378 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
381 case kJSTypeString
: {
382 std::ostringstream str
;
383 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
384 CYStringify(str
, string
.data
, string
.size
);
385 std::string
value(str
.str());
386 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
390 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
392 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
396 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
) {
397 JSValueRef
exception(NULL
);
398 const char *cyon(CYPoolCCYON(pool
, context
, value
, &exception
));
399 CYThrow(context
, exception
);
403 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSObjectRef object
) {
404 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
405 if (CYIsCallable(context
, toCYON
)) {
406 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
407 _assert(value
!= NULL
);
408 return CYPoolCString(pool
, context
, value
);
411 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
412 if (CYIsCallable(context
, toJSON
)) {
413 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
414 JSValueRef
exception(NULL
);
415 const char *cyon(CYPoolCCYON(pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), &exception
));
416 CYThrow(context
, exception
);
420 std::ostringstream str
;
424 // XXX: this is, sadly, going to leak
425 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
429 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
430 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
431 JSValueRef
value(CYGetProperty(context
, object
, name
));
438 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
442 CYStringify(str
, string
.data
, string
.size
);
444 str
<< ':' << CYPoolCCYON(pool
, context
, value
);
449 JSPropertyNameArrayRelease(names
);
451 std::string
string(str
.str());
452 return apr_pstrmemdup(pool
, string
.c_str(), string
.size());
455 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
457 std::ostringstream str
;
461 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
464 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
465 JSValueRef
value(CYGetProperty(context
, _this
, index
));
472 if (!JSValueIsUndefined(context
, value
))
473 str
<< CYPoolCCYON(pool
, context
, value
);
482 std::string
value(str
.str());
483 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
486 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
487 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
488 return JSObjectMake(context
, Pointer_
, internal
);
491 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const char *type
, void **cache
= NULL
) {
492 cy::Functor
*internal
;
494 if (cache
!= NULL
&& *cache
!= NULL
) {
495 internal
= reinterpret_cast<cy::Functor
*>(*cache
);
498 internal
= new cy::Functor(type
, function
);
506 return JSObjectMake(context
, Functor_
, internal
);
509 static bool CYGetOffset(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
510 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
513 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
514 switch (JSValueGetType(context
, value
)) {
517 /*case kJSTypeObject:
518 if (JSValueIsObjectOfClass(context, value, Pointer_)) {
519 Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value)));
520 return internal->value_;
523 double number(CYCastDouble(context
, value
));
524 if (std::isnan(number
))
525 throw CYJSError(context
, "cannot convert value to pointer");
526 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
530 void CYPoolFFI(apr_pool_t
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
531 switch (type
->primitive
) {
533 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
536 #define CYPoolFFI_(primitive, native) \
537 case sig::primitive ## _P: \
538 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
541 CYPoolFFI_(uchar
, unsigned char)
542 CYPoolFFI_(char, char)
543 CYPoolFFI_(ushort
, unsigned short)
544 CYPoolFFI_(short, short)
545 CYPoolFFI_(ulong
, unsigned long)
546 CYPoolFFI_(long, long)
547 CYPoolFFI_(uint
, unsigned int)
549 CYPoolFFI_(ulonglong
, unsigned long long)
550 CYPoolFFI_(longlong
, long long)
551 CYPoolFFI_(float, float)
552 CYPoolFFI_(double, double)
555 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
556 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
557 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
558 ffi_type
*field(ffi
->elements
[index
]);
561 if (aggregate
== NULL
)
564 rhs
= CYGetProperty(context
, aggregate
, index
);
565 if (JSValueIsUndefined(context
, rhs
))
566 throw CYJSError(context
, "unable to extract array value");
569 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
576 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
580 *reinterpret_cast<const char **>(data
) = CYPoolCString(pool
, context
, value
);
583 case sig::struct_P
: {
584 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
585 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
586 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
587 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
588 ffi_type
*field(ffi
->elements
[index
]);
591 if (aggregate
== NULL
)
594 rhs
= CYGetProperty(context
, aggregate
, index
);
595 if (JSValueIsUndefined(context
, rhs
)) {
596 if (element
->name
!= NULL
)
597 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
600 if (JSValueIsUndefined(context
, rhs
)) undefined
:
601 throw CYJSError(context
, "unable to extract structure value");
605 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
615 if (hooks_
!= NULL
&& hooks_
->PoolFFI
!= NULL
)
616 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
619 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
623 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
624 switch (type
->primitive
) {
626 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
628 #define CYFromFFI_(primitive, native) \
629 case sig::primitive ## _P: \
630 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
632 CYFromFFI_(uchar, unsigned char)
633 CYFromFFI_(char, char)
634 CYFromFFI_(ushort
, unsigned short)
635 CYFromFFI_(short, short)
636 CYFromFFI_(ulong
, unsigned long)
637 CYFromFFI_(long, long)
638 CYFromFFI_(uint
, unsigned int)
640 CYFromFFI_(ulonglong
, unsigned long long)
641 CYFromFFI_(longlong
, long long)
642 CYFromFFI_(float, float)
643 CYFromFFI_(double, double)
646 if (void *pointer
= data
)
647 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
651 if (void *pointer
= *reinterpret_cast<void **>(data
))
652 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
656 if (char *utf8
= *reinterpret_cast<char **>(data
))
657 return CYCastJSValue(context
, utf8
);
661 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
663 return CYJSUndefined(context
);
666 return CYJSNull(context
);
668 if (hooks_
!= NULL
&& hooks_
->FromFFI
!= NULL
)
669 if (JSValueRef value
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
672 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
676 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
677 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
679 JSContextRef
context(internal
->context_
);
681 size_t count(internal
->cif_
.nargs
);
682 JSValueRef values
[count
];
684 for (size_t index(0); index
!= count
; ++index
)
685 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
687 JSValueRef
value(CYCallAsFunction(context
, internal
->function_
, NULL
, count
, values
));
688 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
691 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const char *type
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
692 // XXX: in case of exceptions this will leak
693 // XXX: in point of fact, this may /need/ to leak :(
694 Closure_privateData
*internal(new Closure_privateData(context
, function
, type
));
696 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
697 NULL
, sizeof(ffi_closure
),
698 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
702 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
703 _assert(status
== FFI_OK
);
705 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
707 internal
->value_
= closure
;
712 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const char *type
) {
713 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, type
, &FunctionClosure_
));
714 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
715 // XXX: see above notes about needing to leak
716 JSValueProtect(CYGetJSContext(context
), object
);
720 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
721 return CYCastJSObject(context
, CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
));
724 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const char *type
) {
725 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
727 JSValueRef
exception(NULL
);
728 bool function(JSValueIsInstanceOfConstructor(context
, value
, Function
, &exception
));
729 CYThrow(context
, exception
);
732 JSObjectRef
function(CYCastJSObject(context
, value
));
733 return CYMakeFunctor(context
, function
, type
);
735 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
736 return CYMakeFunctor(context
, function
, type
);
740 static bool Index_(apr_pool_t
*pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
741 Type_privateData
*typical(internal
->type_
);
742 sig::Type
*type(typical
->type_
);
746 const char *name(CYPoolCString(pool
, context
, property
));
747 size_t length(strlen(name
));
748 double number(CYCastDouble(name
, length
));
750 size_t count(type
->data
.signature
.count
);
752 if (std::isnan(number
)) {
753 if (property
== NULL
)
756 sig::Element
*elements(type
->data
.signature
.elements
);
758 for (size_t local(0); local
!= count
; ++local
) {
759 sig::Element
*element(&elements
[local
]);
760 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
768 index
= static_cast<ssize_t
>(number
);
769 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
774 ffi_type
**elements(typical
->GetFFI()->elements
);
776 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
777 for (ssize_t
local(0); local
!= index
; ++local
)
778 base
+= elements
[local
]->size
;
783 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
785 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
787 if (JSStringIsEqual(property
, length_s
))
788 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
790 Type_privateData
*typical(internal
->type_
);
792 if (typical
->type_
== NULL
)
796 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
798 else if (!CYGetOffset(pool
, context
, property
, offset
))
801 ffi_type
*ffi(typical
->GetFFI());
803 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
804 base
+= ffi
->size
* offset
;
806 JSObjectRef
owner(internal
->GetOwner() ?: object
);
807 return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
);
810 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
812 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
813 Type_privateData
*typical(internal
->type_
);
815 if (typical
->type_
== NULL
)
819 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
821 else if (!CYGetOffset(pool
, context
, property
, offset
))
824 ffi_type
*ffi(typical
->GetFFI());
826 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
827 base
+= ffi
->size
* offset
;
829 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
833 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
834 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
835 Type_privateData
*typical(internal
->type_
);
836 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
839 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, 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 JSObjectRef
owner(internal
->GetOwner() ?: object
);
852 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
855 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
857 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
858 Type_privateData
*typical(internal
->type_
);
863 if (!Index_(pool
, context
, internal
, property
, index
, base
))
866 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
870 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
871 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
872 Type_privateData
*typical(internal
->type_
);
873 sig::Type
*type(typical
->type_
);
878 size_t count(type
->data
.signature
.count
);
879 sig::Element
*elements(type
->data
.signature
.elements
);
883 for (size_t index(0); index
!= count
; ++index
) {
885 name
= elements
[index
].name
;
888 sprintf(number
, "%zu", index
);
892 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
896 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
{
897 if (setups
+ count
!= signature
->count
- 1)
898 throw CYJSError(context
, "incorrect number of arguments to ffi function");
900 size_t size(setups
+ count
);
902 memcpy(values
, setup
, sizeof(void *) * setups
);
904 for (size_t index(setups
); index
!= size
; ++index
) {
905 sig::Element
*element(&signature
->elements
[index
+ 1]);
906 ffi_type
*ffi(cif
->arg_types
[index
]);
908 values
[index
] = new(pool
) uint8_t[ffi
->size
];
909 CYPoolFFI(pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
912 uint8_t value
[cif
->rtype
->size
];
914 if (hooks_
!= NULL
&& hooks_
->CallFunction
!= NULL
)
915 (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
);
917 ffi_call(cif
, function
, value
, values
);
919 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
922 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
924 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
925 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, exception
, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
928 static JSObjectRef
CYMakeType(JSContextRef context
, const char *type
) {
929 Type_privateData
*internal(new Type_privateData(type
));
930 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
933 static JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
934 Type_privateData
*internal(new Type_privateData(type
));
935 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
938 static void *CYCastSymbol(const char *name
) {
939 return dlsym(RTLD_DEFAULT
, name
);
942 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
943 JSObjectRef
global(CYGetGlobalObject(context
));
944 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
945 if (JSValueRef value
= CYGetProperty(context
, cycript
, property
))
946 if (!JSValueIsUndefined(context
, value
))
950 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
952 if (hooks_
!= NULL
&& hooks_
->RuntimeProperty
!= NULL
)
953 if (JSValueRef value
= (*hooks_
->RuntimeProperty
)(context
, name
))
956 size_t length(name
.size
);
957 char keyed
[length
+ 2];
958 memcpy(keyed
+ 1, name
.data
, length
+ 1);
960 static const char *modes
= "0124";
961 for (size_t i(0); i
!= 4; ++i
) {
965 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
968 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
);
971 if (void (*symbol
)() = reinterpret_cast<void (*)()>(CYCastSymbol(name
.data
)))
972 return CYMakeFunctor(context
, symbol
, entry
->value_
, &entry
->cache_
);
976 if (void *symbol
= CYCastSymbol(name
.data
)) {
977 // XXX: this is horrendously inefficient
978 sig::Signature signature
;
979 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
981 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
982 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
985 // XXX: implement case 3
987 return CYMakeType(context
, entry
->value_
);
994 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
996 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1000 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1001 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1003 sig::Signature signature
;
1004 sig::Parse(pool
, &signature
, type
, &Structor_
);
1006 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1009 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1011 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1013 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1014 return CYMakeType(context
, type
);
1017 static JSValueRef
Type_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1018 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1022 if (JSStringIsEqualToUTF8CString(property
, "$cyi")) {
1023 type
.primitive
= sig::pointer_P
;
1024 type
.data
.data
.size
= 0;
1027 size_t index(CYGetIndex(pool
, context
, property
));
1028 if (index
== _not(size_t))
1030 type
.primitive
= sig::array_P
;
1031 type
.data
.data
.size
= index
;
1037 type
.data
.data
.type
= internal
->type_
;
1039 return CYMakeType(context
, &type
);
1042 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1043 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1046 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1047 sig::Type
*type(internal
->type_
);
1048 ffi_type
*ffi(internal
->GetFFI());
1050 uint8_t value
[ffi
->size
];
1052 CYPoolFFI(pool
, context
, type
, ffi
, value
, arguments
[0]);
1053 return CYFromFFI(context
, type
, ffi
, value
);
1056 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1058 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1059 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1061 sig::Type
*type(internal
->type_
);
1064 if (type
->primitive
!= sig::array_P
)
1065 length
= _not(size_t);
1067 length
= type
->data
.data
.size
;
1068 type
= type
->data
.data
.type
;
1071 void *value(malloc(internal
->GetFFI()->size
));
1072 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1075 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1077 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1079 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1080 return CYMakeFunctor(context
, arguments
[0], type
);
1083 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1084 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1085 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1088 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1089 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1092 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1093 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1095 sprintf(string
, "%p", internal
->value_
);
1096 return CYCastJSValue(context
, string
);
1099 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1100 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1101 if (internal
->length_
!= _not(size_t)) {
1102 JSObjectRef
Array(CYGetCachedObject(context
, Array_s
));
1103 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1104 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1107 sprintf(string
, "%p", internal
->value_
);
1108 return CYCastJSValue(context
, string
);
1112 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1113 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1115 const char *type(sig::Unparse(pool
, internal
->type_
));
1116 return CYCastJSValue(context
, CYJSString(type
));
1119 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1120 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1122 const char *type(sig::Unparse(pool
, internal
->type_
));
1123 size_t size(strlen(type
));
1124 char *cyon(new(pool
) char[12 + size
+ 1]);
1125 memcpy(cyon
, "new Type(\"", 10);
1126 cyon
[12 + size
] = '\0';
1127 cyon
[12 + size
- 2] = '"';
1128 cyon
[12 + size
- 1] = ')';
1129 memcpy(cyon
+ 10, type
, size
);
1130 return CYCastJSValue(context
, CYJSString(cyon
));
1133 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1134 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1137 static JSStaticFunction Pointer_staticFunctions
[4] = {
1138 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1139 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1140 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1144 static JSStaticFunction Struct_staticFunctions
[2] = {
1145 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1149 static JSStaticFunction Functor_staticFunctions
[4] = {
1150 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1151 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1152 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1157 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1160 static JSStaticFunction Type_staticFunctions
[4] = {
1161 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1162 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1163 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1167 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1169 void CYSetArgs(int argc
, const char *argv
[]) {
1170 JSContextRef
context(CYGetJSContext());
1171 JSValueRef args
[argc
];
1172 for (int i(0); i
!= argc
; ++i
)
1173 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1176 if (JSObjectMakeArray$
!= NULL
) {
1177 JSValueRef
exception(NULL
);
1178 array
= (*JSObjectMakeArray$
)(context
, argc
, args
, &exception
);
1179 CYThrow(context
, exception
);
1181 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1182 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1183 array
= CYCastJSObject(context
, value
);
1186 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1187 CYSetProperty(context
, System
, CYJSString("args"), array
);
1190 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1191 return JSContextGetGlobalObject(context
);
1194 const char *CYExecute(apr_pool_t
*pool
, const char *code
) {
1195 JSContextRef
context(CYGetJSContext());
1196 JSValueRef
exception(NULL
), result
;
1199 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1200 handle
= (*hooks_
->ExecuteStart
)(context
);
1207 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1208 } catch (const char *error
) {
1212 if (exception
!= NULL
) { error
:
1217 if (JSValueIsUndefined(context
, result
))
1221 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1222 } catch (const char *error
) {
1226 if (exception
!= NULL
)
1229 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1231 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1232 (*hooks_
->ExecuteEnd
)(context
, handle
);
1236 extern "C" void CydgetSetupContext(JSGlobalContextRef context
) {
1237 CYSetupContext(context
);
1240 static bool initialized_
= false;
1242 void CYInitializeDynamic() {
1244 initialized_
= true;
1247 CYInitializeStatic();
1249 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1251 JSClassDefinition definition
;
1253 definition
= kJSClassDefinitionEmpty
;
1254 definition
.className
= "All";
1255 definition
.getProperty
= &All_getProperty
;
1256 All_
= JSClassCreate(&definition
);
1258 definition
= kJSClassDefinitionEmpty
;
1259 definition
.className
= "Context";
1260 definition
.finalize
= &CYFinalize
;
1261 Context_
= JSClassCreate(&definition
);
1263 definition
= kJSClassDefinitionEmpty
;
1264 definition
.className
= "Functor";
1265 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1266 definition
.callAsFunction
= &Functor_callAsFunction
;
1267 definition
.finalize
= &CYFinalize
;
1268 Functor_
= JSClassCreate(&definition
);
1270 definition
= kJSClassDefinitionEmpty
;
1271 definition
.className
= "Pointer";
1272 definition
.staticFunctions
= Pointer_staticFunctions
;
1273 definition
.getProperty
= &Pointer_getProperty
;
1274 definition
.setProperty
= &Pointer_setProperty
;
1275 definition
.finalize
= &CYFinalize
;
1276 Pointer_
= JSClassCreate(&definition
);
1278 definition
= kJSClassDefinitionEmpty
;
1279 definition
.className
= "Struct";
1280 definition
.staticFunctions
= Struct_staticFunctions
;
1281 definition
.getProperty
= &Struct_getProperty
;
1282 definition
.setProperty
= &Struct_setProperty
;
1283 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1284 definition
.finalize
= &CYFinalize
;
1285 Struct_
= JSClassCreate(&definition
);
1287 definition
= kJSClassDefinitionEmpty
;
1288 definition
.className
= "Type";
1289 definition
.staticFunctions
= Type_staticFunctions
;
1290 definition
.getProperty
= &Type_getProperty
;
1291 definition
.callAsFunction
= &Type_callAsFunction
;
1292 definition
.callAsConstructor
= &Type_callAsConstructor
;
1293 definition
.finalize
= &CYFinalize
;
1294 Type_privateData::Class_
= JSClassCreate(&definition
);
1296 definition
= kJSClassDefinitionEmpty
;
1297 //definition.getProperty = &Global_getProperty;
1298 Global_
= JSClassCreate(&definition
);
1300 Array_s
= JSStringCreateWithUTF8CString("Array");
1301 cy_s
= JSStringCreateWithUTF8CString("$cy");
1302 length_s
= JSStringCreateWithUTF8CString("length");
1303 message_s
= JSStringCreateWithUTF8CString("message");
1304 name_s
= JSStringCreateWithUTF8CString("name");
1305 pop_s
= JSStringCreateWithUTF8CString("pop");
1306 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1307 push_s
= JSStringCreateWithUTF8CString("push");
1308 splice_s
= JSStringCreateWithUTF8CString("splice");
1309 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1310 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1312 Result_
= JSStringCreateWithUTF8CString("_");
1314 if (hooks_
!= NULL
&& hooks_
->Initialize
!= NULL
)
1315 (*hooks_
->Initialize
)();
1318 void CYThrow(JSContextRef context
, JSValueRef value
) {
1320 throw CYJSError(context
, value
);
1323 const char *CYJSError::PoolCString(apr_pool_t
*pool
) const {
1324 // XXX: this used to be CYPoolCString
1325 return CYPoolCCYON(pool
, context_
, value_
);
1328 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1329 // XXX: what if the context is different?
1333 JSValueRef
CYCastJSError(JSContextRef context
, const char *message
) {
1334 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString("Error")));
1336 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1338 JSValueRef
exception(NULL
);
1339 JSValueRef
value(JSObjectCallAsConstructor(context
, Error
, 1, arguments
, &exception
));
1340 CYThrow(context
, exception
);
1345 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1346 return CYCastJSError(context
, message_
);
1349 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1350 _assert(context
!= NULL
);
1355 va_start(args
, format
);
1356 const char *message(apr_pvsprintf(pool
, format
, args
));
1359 value_
= CYCastJSError(context
, message
);
1362 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1363 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1366 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
1367 CYInitializeDynamic();
1369 JSObjectRef
global(CYGetGlobalObject(context
));
1371 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
1372 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
1374 /* Cache Globals {{{ */
1375 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
1376 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
1378 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
1379 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
1381 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
1382 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
1384 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
1385 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
1387 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
1388 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
1390 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1391 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
1393 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
1394 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
1396 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
1397 CYSetProperty(context
, cy
, CYJSString("String"), String
);
1400 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1402 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1403 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1404 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
1406 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1407 JSObjectSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
1408 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
1410 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1411 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1413 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
1414 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
1416 JSObjectRef
last(NULL
), curr(global
);
1418 goto next
; for (JSValueRef next
;;) {
1419 if (JSValueIsNull(context
, next
))
1422 curr
= CYCastJSObject(context
, next
);
1424 next
= JSObjectGetPrototype(context
, curr
);
1427 JSObjectSetPrototype(context
, last
, all
);
1429 CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
);
1431 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
1432 CYSetProperty(context
, cy
, CYJSString("System"), System
);
1434 CYSetProperty(context
, global
, CYJSString("system"), System
);
1435 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
1436 //CYSetProperty(context, System, CYJSString("global"), global);
1437 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
1439 if (hooks_
!= NULL
&& hooks_
->SetupContext
!= NULL
)
1440 (*hooks_
->SetupContext
)(context
);
1443 JSGlobalContextRef
CYGetJSContext() {
1444 CYInitializeDynamic();
1446 static JSGlobalContextRef context_
;
1448 if (context_
== NULL
) {
1449 context_
= JSGlobalContextCreate(Global_
);
1450 CYSetupContext(context_
);