1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2012 Jay Freeman (saurik)
5 /* GNU Lesser General Public License, Version 3 {{{ */
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
22 #include "Internal.hpp"
27 #include "cycript.hpp"
29 #include "sig/parse.hpp"
30 #include "sig/ffi_type.hpp"
32 #include "Pooling.hpp"
33 #include "Execute.hpp"
38 #include <ext/stdio_filebuf.h>
46 #include "Cycript.tab.hh"
49 #include "JavaScript.hpp"
52 struct CYHooks
*hooks_
;
54 /* JavaScript Properties {{{ */
55 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, size_t index
) {
56 JSValueRef
exception(NULL
);
57 JSValueRef
value(JSObjectGetPropertyAtIndex(context
, object
, index
, &exception
));
58 CYThrow(context
, exception
);
62 JSValueRef
CYGetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
) {
63 JSValueRef
exception(NULL
);
64 JSValueRef
value(JSObjectGetProperty(context
, object
, name
, &exception
));
65 CYThrow(context
, exception
);
69 void CYSetProperty(JSContextRef context
, JSObjectRef object
, size_t index
, JSValueRef value
) {
70 JSValueRef
exception(NULL
);
71 JSObjectSetPropertyAtIndex(context
, object
, index
, value
, &exception
);
72 CYThrow(context
, exception
);
75 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef value
, JSPropertyAttributes attributes
) {
76 JSValueRef
exception(NULL
);
77 JSObjectSetProperty(context
, object
, name
, value
, attributes
, &exception
);
78 CYThrow(context
, exception
);
81 void CYSetProperty(JSContextRef context
, JSObjectRef object
, JSStringRef name
, JSValueRef (*callback
)(JSContextRef
, JSObjectRef
, JSObjectRef
, size_t, const JSValueRef
[], JSValueRef
*), JSPropertyAttributes attributes
) {
82 CYSetProperty(context
, object
, name
, JSObjectMakeFunctionWithCallback(context
, name
, callback
), attributes
);
85 /* JavaScript Strings {{{ */
86 JSStringRef
CYCopyJSString(const char *value
) {
87 return value
== NULL
? NULL
: JSStringCreateWithUTF8CString(value
);
90 JSStringRef
CYCopyJSString(JSStringRef value
) {
91 return value
== NULL
? NULL
: JSStringRetain(value
);
94 JSStringRef
CYCopyJSString(CYUTF8String value
) {
95 // XXX: this is very wrong; it needs to convert to UTF16 and then create from there
96 return CYCopyJSString(value
.data
);
99 JSStringRef
CYCopyJSString(JSContextRef context
, JSValueRef value
) {
100 if (JSValueIsNull(context
, value
))
102 JSValueRef
exception(NULL
);
103 JSStringRef
string(JSValueToStringCopy(context
, value
, &exception
));
104 CYThrow(context
, exception
);
108 static CYUTF16String
CYCastUTF16String(JSStringRef value
) {
109 return CYUTF16String(JSStringGetCharactersPtr(value
), JSStringGetLength(value
));
112 CYUTF8String
CYPoolUTF8String(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
113 return CYPoolUTF8String(pool
, CYCastUTF16String(value
));
116 const char *CYPoolCString(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
117 CYUTF8String
utf8(CYPoolUTF8String(pool
, context
, value
));
118 _assert(memchr(utf8
.data
, '\0', utf8
.size
) == NULL
);
122 const char *CYPoolCString(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
) {
123 return JSValueIsNull(context
, value
) ? NULL
: CYPoolCString(pool
, context
, CYJSString(context
, value
));
126 /* Index Offsets {{{ */
127 size_t CYGetIndex(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
) {
128 return CYGetIndex(CYPoolUTF8String(pool
, context
, value
));
132 static JSClassRef All_
;
133 static JSClassRef Context_
;
135 static JSClassRef Global_
;
136 static JSClassRef Pointer_
;
137 static JSClassRef Struct_
;
141 JSStringRef length_s
;
142 JSStringRef message_s
;
145 JSStringRef prototype_s
;
147 JSStringRef splice_s
;
148 JSStringRef toCYON_s
;
149 JSStringRef toJSON_s
;
150 JSStringRef toPointer_s
;
151 JSStringRef toString_s
;
153 static JSStringRef Result_
;
155 void CYFinalize(JSObjectRef object
) {
156 CYData
*internal(reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
)));
157 if (--internal
->count_
== 0)
161 void Structor_(apr_pool_t
*pool
, sig::Type
*&type
) {
163 type
->primitive
== sig::pointer_P
&&
164 type
->data
.data
.type
!= NULL
&&
165 type
->data
.data
.type
->primitive
== sig::struct_P
&&
166 type
->data
.data
.type
->name
!= NULL
&&
167 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
169 type
->primitive
= sig::typename_P
;
170 type
->data
.data
.type
= NULL
;
174 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
177 size_t length(strlen(type
->name
));
178 char keyed
[length
+ 2];
179 memcpy(keyed
+ 1, type
->name
, length
+ 1);
181 static const char *modes
= "34";
182 for (size_t i(0); i
!= 2; ++i
) {
186 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
189 sig::Parse(pool
, &type
->data
.signature
, entry
->value_
, &Structor_
);
193 sig::Signature signature
;
194 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
195 type
= signature
.elements
[0].type
;
201 JSClassRef
Type_privateData::Class_
;
206 JSGlobalContextRef context_
;
208 Context(JSGlobalContextRef context
) :
217 Type_privateData
*type_
;
220 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type
*type
) :
221 CYOwned(value
, context
, owner
),
222 type_(new(pool_
) Type_privateData(type
)),
228 struct Struct_privateData
:
231 Type_privateData
*type_
;
233 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
234 CYOwned(NULL
, context
, owner
)
239 typedef std::map
<const char *, Type_privateData
*, CYCStringLess
> TypeMap
;
240 static TypeMap Types_
;
242 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
243 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
244 apr_pool_t
*pool(internal
->pool_
);
245 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
246 internal
->type_
= typical
;
249 internal
->value_
= data
;
251 size_t size(typical
->GetFFI()->size
);
252 void *copy(apr_palloc(internal
->pool_
, size
));
253 memcpy(copy
, data
, size
);
254 internal
->value_
= copy
;
257 return JSObjectMake(context
, Struct_
, internal
);
260 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
261 return JSValueMakeBoolean(context
, value
);
264 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
265 return JSValueMakeNumber(context
, value
);
268 #define CYCastJSValue_(Type_) \
269 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
270 return JSValueMakeNumber(context, static_cast<double>(value)); \
274 CYCastJSValue_(unsigned int)
275 CYCastJSValue_(long int)
276 CYCastJSValue_(long unsigned int)
277 CYCastJSValue_(long long int)
278 CYCastJSValue_(long long unsigned int)
280 JSValueRef
CYJSUndefined(JSContextRef context
) {
281 return JSValueMakeUndefined(context
);
284 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
285 JSValueRef
exception(NULL
);
286 double number(JSValueToNumber(context
, value
, &exception
));
287 CYThrow(context
, exception
);
291 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
292 return JSValueToBoolean(context
, value
);
295 JSValueRef
CYJSNull(JSContextRef context
) {
296 return JSValueMakeNull(context
);
299 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
300 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
303 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
304 return CYCastJSValue(context
, CYJSString(value
));
307 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
308 JSValueRef
exception(NULL
);
309 JSObjectRef
object(JSValueToObject(context
, value
, &exception
));
310 CYThrow(context
, exception
);
314 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
315 JSValueRef
exception(NULL
);
316 JSValueRef
value(JSObjectCallAsFunction(context
, function
, _this
, count
, arguments
, &exception
));
317 CYThrow(context
, exception
);
321 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
322 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
325 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
330 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
333 return CYJSUndefined(context
);
336 static size_t Nonce_(0);
338 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
340 const char *name(apr_psprintf(pool
, "%s%"APR_SIZE_T_FMT
"", CYPoolCString(pool
, context
, arguments
[0]), Nonce_
++));
341 return CYCastJSValue(context
, name
);
344 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
345 JSGarbageCollect(context
);
346 return CYJSUndefined(context
);
349 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
350 switch (JSType type
= JSValueGetType(context
, value
)) {
351 case kJSTypeUndefined
:
356 return CYCastBool(context
, value
) ? "true" : "false";
358 case kJSTypeNumber
: {
359 std::ostringstream str
;
360 CYNumerify(str
, CYCastDouble(context
, value
));
361 std::string
value(str
.str());
362 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
365 case kJSTypeString
: {
366 std::ostringstream str
;
367 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
368 CYStringify(str
, string
.data
, string
.size
);
369 std::string
value(str
.str());
370 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
374 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
376 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
380 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
) {
381 JSValueRef
exception(NULL
);
382 const char *cyon(CYPoolCCYON(pool
, context
, value
, &exception
));
383 CYThrow(context
, exception
);
387 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSObjectRef object
) {
388 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
389 if (CYIsCallable(context
, toCYON
)) {
390 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
391 _assert(value
!= NULL
);
392 return CYPoolCString(pool
, context
, value
);
395 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
396 if (CYIsCallable(context
, toJSON
)) {
397 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
398 JSValueRef
exception(NULL
);
399 const char *cyon(CYPoolCCYON(pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), &exception
));
400 CYThrow(context
, exception
);
404 if (JSObjectIsFunction(context
, object
)) {
405 JSValueRef
toString(CYGetProperty(context
, object
, toString_s
));
406 if (CYIsCallable(context
, toString
)) {
407 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
408 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toString
, object
, 1, arguments
));
409 _assert(value
!= NULL
);
410 return CYPoolCString(pool
, context
, value
);
414 std::ostringstream str
;
418 // XXX: this is, sadly, going to leak
419 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
423 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
424 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
425 JSValueRef
value(CYGetProperty(context
, object
, name
));
432 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
436 CYStringify(str
, string
.data
, string
.size
);
438 str
<< ':' << CYPoolCCYON(pool
, context
, value
);
443 JSPropertyNameArrayRelease(names
);
445 std::string
string(str
.str());
446 return apr_pstrmemdup(pool
, string
.c_str(), string
.size());
449 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
451 std::ostringstream str
;
455 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
458 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
459 JSValueRef
value(CYGetProperty(context
, _this
, index
));
466 if (!JSValueIsUndefined(context
, value
))
467 str
<< CYPoolCCYON(pool
, context
, value
);
476 std::string
value(str
.str());
477 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
480 static JSValueRef
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
482 std::ostringstream str
;
484 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
)));
485 CYStringify(str
, string
.data
, string
.size
);
487 std::string
value(str
.str());
488 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
491 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
492 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
493 return JSObjectMake(context
, Pointer_
, internal
);
496 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const char *type
, void **cache
= NULL
) {
497 cy::Functor
*internal
;
499 if (cache
!= NULL
&& *cache
!= NULL
) {
500 internal
= reinterpret_cast<cy::Functor
*>(*cache
);
503 internal
= new cy::Functor(type
, function
);
511 return JSObjectMake(context
, Functor_
, internal
);
514 static bool CYGetOffset(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
515 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
518 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
519 switch (JSValueGetType(context
, value
)) {
522 case kJSTypeObject
: {
523 JSObjectRef
object((JSObjectRef
) value
);
524 if (JSValueIsObjectOfClass(context
, value
, Pointer_
)) {
525 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
526 return internal
->value_
;
528 JSValueRef
toPointer(CYGetProperty(context
, object
, toPointer_s
));
529 if (CYIsCallable(context
, toPointer
)) {
530 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
));
531 _assert(value
!= NULL
);
532 return CYCastPointer_(context
, value
);
535 double number(CYCastDouble(context
, value
));
536 if (std::isnan(number
))
537 throw CYJSError(context
, "cannot convert value to pointer");
538 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
542 void CYPoolFFI(apr_pool_t
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
543 switch (type
->primitive
) {
545 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
548 #define CYPoolFFI_(primitive, native) \
549 case sig::primitive ## _P: \
550 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
553 CYPoolFFI_(uchar
, unsigned char)
554 CYPoolFFI_(char, char)
555 CYPoolFFI_(ushort
, unsigned short)
556 CYPoolFFI_(short, short)
557 CYPoolFFI_(ulong
, unsigned long)
558 CYPoolFFI_(long, long)
559 CYPoolFFI_(uint
, unsigned int)
561 CYPoolFFI_(ulonglong
, unsigned long long)
562 CYPoolFFI_(longlong
, long long)
563 CYPoolFFI_(float, float)
564 CYPoolFFI_(double, double)
567 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
568 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
569 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
570 ffi_type
*field(ffi
->elements
[index
]);
573 if (aggregate
== NULL
)
576 rhs
= CYGetProperty(context
, aggregate
, index
);
577 if (JSValueIsUndefined(context
, rhs
))
578 throw CYJSError(context
, "unable to extract array value");
581 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
588 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
592 *reinterpret_cast<const char **>(data
) = CYPoolCString(pool
, context
, value
);
595 case sig::struct_P
: {
596 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
597 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
598 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
599 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
600 ffi_type
*field(ffi
->elements
[index
]);
603 if (aggregate
== NULL
)
606 rhs
= CYGetProperty(context
, aggregate
, index
);
607 if (JSValueIsUndefined(context
, rhs
)) {
608 if (element
->name
!= NULL
)
609 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
612 if (JSValueIsUndefined(context
, rhs
)) undefined
:
613 throw CYJSError(context
, "unable to extract structure value");
617 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
627 if (hooks_
!= NULL
&& hooks_
->PoolFFI
!= NULL
)
628 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
631 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
635 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
636 switch (type
->primitive
) {
638 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
640 #define CYFromFFI_(primitive, native) \
641 case sig::primitive ## _P: \
642 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
644 CYFromFFI_(uchar, unsigned char)
645 CYFromFFI_(char, char)
646 CYFromFFI_(ushort
, unsigned short)
647 CYFromFFI_(short, short)
648 CYFromFFI_(ulong
, unsigned long)
649 CYFromFFI_(long, long)
650 CYFromFFI_(uint
, unsigned int)
652 CYFromFFI_(ulonglong
, unsigned long long)
653 CYFromFFI_(longlong
, long long)
654 CYFromFFI_(float, float)
655 CYFromFFI_(double, double)
658 if (void *pointer
= data
)
659 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
663 if (void *pointer
= *reinterpret_cast<void **>(data
))
664 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
668 if (char *utf8
= *reinterpret_cast<char **>(data
))
669 return CYCastJSValue(context
, utf8
);
673 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
675 return CYJSUndefined(context
);
678 return CYJSNull(context
);
680 if (hooks_
!= NULL
&& hooks_
->FromFFI
!= NULL
)
681 if (JSValueRef value
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
684 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
688 void CYExecuteClosure(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
, JSValueRef (*adapter
)(JSContextRef
, size_t, JSValueRef
[], JSObjectRef
)) {
689 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
691 JSContextRef
context(internal
->context_
);
693 size_t count(internal
->cif_
.nargs
);
694 JSValueRef values
[count
];
696 for (size_t index(0); index
!= count
; ++index
)
697 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
699 JSValueRef
value(adapter(context
, count
, values
, internal
->function_
));
700 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
703 static JSValueRef
FunctionAdapter_(JSContextRef context
, size_t count
, JSValueRef values
[], JSObjectRef function
) {
704 return CYCallAsFunction(context
, function
, NULL
, count
, values
);
707 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
708 CYExecuteClosure(cif
, result
, arguments
, arg
, &FunctionAdapter_
);
711 Closure_privateData
*CYMakeFunctor_(JSContextRef context
, JSObjectRef function
, const char *type
, void (*callback
)(ffi_cif
*, void *, void **, void *)) {
712 // XXX: in case of exceptions this will leak
713 // XXX: in point of fact, this may /need/ to leak :(
714 Closure_privateData
*internal(new Closure_privateData(context
, function
, type
));
716 #if defined(__APPLE__) && defined(__arm__)
718 ffi_closure
*writable(reinterpret_cast<ffi_closure
*>(ffi_closure_alloc(sizeof(ffi_closure
), &executable
)));
720 ffi_status
status(ffi_prep_closure_loc(writable
, &internal
->cif_
, callback
, internal
, executable
));
721 _assert(status
== FFI_OK
);
723 internal
->value_
= executable
;
725 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
726 NULL
, sizeof(ffi_closure
),
727 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
731 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
732 _assert(status
== FFI_OK
);
734 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
736 internal
->value_
= closure
;
742 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const char *type
) {
743 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, type
, &FunctionClosure_
));
744 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
745 // XXX: see above notes about needing to leak
746 JSValueProtect(CYGetJSContext(context
), object
);
750 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
751 return CYCastJSObject(context
, CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
));
754 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const char *type
) {
755 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
757 JSValueRef
exception(NULL
);
758 bool function(JSValueIsInstanceOfConstructor(context
, value
, Function
, &exception
));
759 CYThrow(context
, exception
);
762 JSObjectRef
function(CYCastJSObject(context
, value
));
763 return CYMakeFunctor(context
, function
, type
);
765 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
766 return CYMakeFunctor(context
, function
, type
);
770 static bool Index_(apr_pool_t
*pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
771 Type_privateData
*typical(internal
->type_
);
772 sig::Type
*type(typical
->type_
);
776 const char *name(CYPoolCString(pool
, context
, property
));
777 size_t length(strlen(name
));
778 double number(CYCastDouble(name
, length
));
780 size_t count(type
->data
.signature
.count
);
782 if (std::isnan(number
)) {
783 if (property
== NULL
)
786 sig::Element
*elements(type
->data
.signature
.elements
);
788 for (size_t local(0); local
!= count
; ++local
) {
789 sig::Element
*element(&elements
[local
]);
790 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
798 index
= static_cast<ssize_t
>(number
);
799 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
804 ffi_type
**elements(typical
->GetFFI()->elements
);
806 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
807 for (ssize_t
local(0); local
!= index
; ++local
)
808 base
+= elements
[local
]->size
;
813 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
815 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
817 if (JSStringIsEqual(property
, length_s
))
818 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
820 Type_privateData
*typical(internal
->type_
);
822 if (typical
->type_
== NULL
)
826 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
828 else if (!CYGetOffset(pool
, context
, property
, offset
))
831 ffi_type
*ffi(typical
->GetFFI());
833 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
834 base
+= ffi
->size
* offset
;
836 JSObjectRef
owner(internal
->GetOwner() ?: object
);
837 return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
);
840 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
842 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
843 Type_privateData
*typical(internal
->type_
);
845 if (typical
->type_
== NULL
)
849 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
851 else if (!CYGetOffset(pool
, context
, property
, offset
))
854 ffi_type
*ffi(typical
->GetFFI());
856 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
857 base
+= ffi
->size
* offset
;
859 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
863 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
864 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
865 Type_privateData
*typical(internal
->type_
);
866 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
869 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
871 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
872 Type_privateData
*typical(internal
->type_
);
877 if (!Index_(pool
, context
, internal
, property
, index
, base
))
880 JSObjectRef
owner(internal
->GetOwner() ?: object
);
882 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
885 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
887 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
888 Type_privateData
*typical(internal
->type_
);
893 if (!Index_(pool
, context
, internal
, property
, index
, base
))
896 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
900 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
901 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
902 Type_privateData
*typical(internal
->type_
);
903 sig::Type
*type(typical
->type_
);
908 size_t count(type
->data
.signature
.count
);
909 sig::Element
*elements(type
->data
.signature
.elements
);
913 for (size_t index(0); index
!= count
; ++index
) {
915 name
= elements
[index
].name
;
918 sprintf(number
, "%zu", index
);
922 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
926 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
{
927 if (setups
+ count
!= signature
->count
- 1)
928 throw CYJSError(context
, "incorrect number of arguments to ffi function");
930 size_t size(setups
+ count
);
932 memcpy(values
, setup
, sizeof(void *) * setups
);
934 for (size_t index(setups
); index
!= size
; ++index
) {
935 sig::Element
*element(&signature
->elements
[index
+ 1]);
936 ffi_type
*ffi(cif
->arg_types
[index
]);
938 values
[index
] = new(pool
) uint8_t[ffi
->size
];
939 CYPoolFFI(pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
942 uint8_t value
[cif
->rtype
->size
];
944 if (hooks_
!= NULL
&& hooks_
->CallFunction
!= NULL
)
945 (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
);
947 ffi_call(cif
, function
, value
, values
);
949 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
952 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
954 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
955 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, exception
, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
958 JSObjectRef
CYMakeType(JSContextRef context
, const char *type
) {
959 Type_privateData
*internal(new Type_privateData(type
));
960 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
963 JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
964 Type_privateData
*internal(new Type_privateData(type
));
965 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
968 static void *CYCastSymbol(const char *name
) {
969 return dlsym(RTLD_DEFAULT
, name
);
972 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
973 JSObjectRef
global(CYGetGlobalObject(context
));
974 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
975 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
977 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
978 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1)))
979 if (JSValueRef value
= CYGetProperty(context
, space
, property
))
980 if (!JSValueIsUndefined(context
, value
))
984 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
986 size_t length(name
.size
);
987 char keyed
[length
+ 2];
988 memcpy(keyed
+ 1, name
.data
, length
+ 1);
990 static const char *modes
= "0124";
991 for (size_t i(0); i
!= 4; ++i
) {
995 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
998 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
);
1001 if (void (*symbol
)() = reinterpret_cast<void (*)()>(CYCastSymbol(name
.data
)))
1002 return CYMakeFunctor(context
, symbol
, entry
->value_
, &entry
->cache_
);
1006 if (void *symbol
= CYCastSymbol(name
.data
)) {
1007 // XXX: this is horrendously inefficient
1008 sig::Signature signature
;
1009 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
1011 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1012 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
1015 // XXX: implement case 3
1017 return CYMakeType(context
, entry
->value_
);
1024 static void All_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1025 JSObjectRef
global(CYGetGlobalObject(context
));
1026 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1027 JSObjectRef
alls(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("alls"))));
1029 for (size_t i(0), count(CYArrayLength(context
, alls
)); i
!= count
; ++i
)
1030 if (JSObjectRef space
= CYCastJSObject(context
, CYArrayGet(context
, alls
, count
- i
- 1))) {
1031 JSPropertyNameArrayRef
subset(JSObjectCopyPropertyNames(context
, space
));
1032 for (size_t index(0), count(JSPropertyNameArrayGetCount(subset
)); index
!= count
; ++index
)
1033 JSPropertyNameAccumulatorAddName(names
, JSPropertyNameArrayGetNameAtIndex(subset
, index
));
1034 JSPropertyNameArrayRelease(subset
);
1038 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1040 throw CYJSError(context
, "incorrect number of arguments to Pointer constructor");
1044 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1045 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1047 sig::Signature signature
;
1048 sig::Parse(pool
, &signature
, type
, &Structor_
);
1050 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1053 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1055 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1057 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1058 return CYMakeType(context
, type
);
1061 static JSValueRef
Type_callAsFunction_arrayOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1063 throw CYJSError(context
, "incorrect number of arguments to Type.arrayOf");
1064 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1067 size_t index(CYGetIndex(pool
, context
, CYJSString(context
, arguments
[0])));
1068 if (index
== _not(size_t))
1069 throw CYJSError(context
, "invalid array size used with Type.arrayOf");
1075 type
.primitive
= sig::array_P
;
1076 type
.data
.data
.type
= internal
->type_
;
1077 type
.data
.data
.size
= index
;
1079 return CYMakeType(context
, &type
);
1082 static JSValueRef
Type_callAsFunction_constant(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1084 throw CYJSError(context
, "incorrect number of arguments to Type.constant");
1085 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1087 sig::Type
type(*internal
->type_
);
1088 type
.flags
|= JOC_TYPE_CONST
;
1089 return CYMakeType(context
, &type
);
1092 static JSValueRef
Type_callAsFunction_pointerTo(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1094 throw CYJSError(context
, "incorrect number of arguments to Type.pointerTo");
1095 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1101 type
.primitive
= sig::pointer_P
;
1102 type
.data
.data
.type
= internal
->type_
;
1103 type
.data
.data
.size
= 0;
1105 return CYMakeType(context
, &type
);
1108 static JSValueRef
Type_callAsFunction_withName(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1110 throw CYJSError(context
, "incorrect number of arguments to Type.withName");
1111 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1114 const char *name(CYPoolCString(pool
, context
, arguments
[0]));
1116 sig::Type
type(*internal
->type_
);
1118 return CYMakeType(context
, &type
);
1121 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1123 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1124 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1126 sig::Type
*type(internal
->type_
);
1127 ffi_type
*ffi(internal
->GetFFI());
1129 uint8_t value
[ffi
->size
];
1131 CYPoolFFI(pool
, context
, type
, ffi
, value
, arguments
[0]);
1132 return CYFromFFI(context
, type
, ffi
, value
);
1135 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1137 throw CYJSError(context
, "incorrect number of arguments to Type allocator");
1138 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1140 sig::Type
*type(internal
->type_
);
1143 if (type
->primitive
!= sig::array_P
)
1144 length
= _not(size_t);
1146 length
= type
->data
.data
.size
;
1147 type
= type
->data
.data
.type
;
1150 void *value(malloc(internal
->GetFFI()->size
));
1151 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1154 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1156 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1158 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1159 return CYMakeFunctor(context
, arguments
[0], type
);
1162 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1163 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1164 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1167 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1168 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1171 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1172 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1174 sprintf(string
, "%p", internal
->value_
);
1175 return CYCastJSValue(context
, string
);
1178 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1179 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1180 if (internal
->length_
!= _not(size_t)) {
1181 JSObjectRef
Array(CYGetCachedObject(context
, Array_s
));
1182 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1183 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1186 sprintf(string
, "%p", internal
->value_
);
1187 return CYCastJSValue(context
, string
);
1191 static JSValueRef
Type_getProperty_alignment(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) {
1192 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1193 return CYCastJSValue(context
, internal
->GetFFI()->alignment
);
1196 static JSValueRef
Type_getProperty_name(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) {
1197 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1198 return CYCastJSValue(context
, internal
->type_
->name
);
1201 static JSValueRef
Type_getProperty_size(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) {
1202 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1203 return CYCastJSValue(context
, internal
->GetFFI()->size
);
1206 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1207 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1209 const char *type(sig::Unparse(pool
, internal
->type_
));
1210 return CYCastJSValue(context
, CYJSString(type
));
1213 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1214 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1216 const char *type(sig::Unparse(pool
, internal
->type_
));
1217 std::ostringstream str
;
1218 CYStringify(str
, type
, strlen(type
));
1219 char *cyon(apr_pstrcat(pool
, "new Type(", str
.str().c_str(), ")", NULL
));
1220 return CYCastJSValue(context
, CYJSString(cyon
));
1223 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1224 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1227 static JSStaticFunction Pointer_staticFunctions
[4] = {
1228 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1229 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1230 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1234 static JSStaticFunction Struct_staticFunctions
[2] = {
1235 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1239 static JSStaticFunction Functor_staticFunctions
[4] = {
1240 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1241 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1242 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1247 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1250 static JSStaticValue Type_staticValues
[4] = {
1251 {"alignment", &Type_getProperty_alignment
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1252 {"name", &Type_getProperty_name
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1253 {"size", &Type_getProperty_size
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1254 {NULL
, NULL
, NULL
, 0}
1257 static JSStaticFunction Type_staticFunctions
[8] = {
1258 {"arrayOf", &Type_callAsFunction_arrayOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1259 {"constant", &Type_callAsFunction_constant
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1260 {"pointerTo", &Type_callAsFunction_pointerTo
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1261 {"withName", &Type_callAsFunction_withName
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1262 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1263 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1264 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1268 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1270 void CYSetArgs(int argc
, const char *argv
[]) {
1271 JSContextRef
context(CYGetJSContext());
1272 JSValueRef args
[argc
];
1273 for (int i(0); i
!= argc
; ++i
)
1274 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1277 if (JSObjectMakeArray$
!= NULL
) {
1278 JSValueRef
exception(NULL
);
1279 array
= (*JSObjectMakeArray$
)(context
, argc
, args
, &exception
);
1280 CYThrow(context
, exception
);
1282 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1283 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1284 array
= CYCastJSObject(context
, value
);
1287 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1288 CYSetProperty(context
, System
, CYJSString("args"), array
);
1291 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1292 return JSContextGetGlobalObject(context
);
1295 const char *CYExecute(apr_pool_t
*pool
, CYUTF8String code
) {
1296 JSContextRef
context(CYGetJSContext());
1297 JSValueRef
exception(NULL
), result
;
1300 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1301 handle
= (*hooks_
->ExecuteStart
)(context
);
1308 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1309 } catch (const char *error
) {
1313 if (exception
!= NULL
) error
:
1314 return CYPoolCString(pool
, context
, CYJSString(context
, exception
));
1316 if (JSValueIsUndefined(context
, result
))
1320 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1321 } catch (const char *error
) {
1325 if (exception
!= NULL
)
1328 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1330 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1331 (*hooks_
->ExecuteEnd
)(context
, handle
);
1335 extern "C" void CydgetSetupContext(JSGlobalContextRef context
) {
1336 CYSetupContext(context
);
1339 static bool initialized_
= false;
1341 void CYInitializeDynamic() {
1343 initialized_
= true;
1346 CYInitializeStatic();
1348 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1350 JSClassDefinition definition
;
1352 definition
= kJSClassDefinitionEmpty
;
1353 definition
.className
= "All";
1354 definition
.getProperty
= &All_getProperty
;
1355 definition
.getPropertyNames
= &All_getPropertyNames
;
1356 All_
= JSClassCreate(&definition
);
1358 definition
= kJSClassDefinitionEmpty
;
1359 definition
.className
= "Context";
1360 definition
.finalize
= &CYFinalize
;
1361 Context_
= JSClassCreate(&definition
);
1363 definition
= kJSClassDefinitionEmpty
;
1364 definition
.className
= "Functor";
1365 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1366 definition
.callAsFunction
= &Functor_callAsFunction
;
1367 definition
.finalize
= &CYFinalize
;
1368 Functor_
= JSClassCreate(&definition
);
1370 definition
= kJSClassDefinitionEmpty
;
1371 definition
.className
= "Pointer";
1372 definition
.staticFunctions
= Pointer_staticFunctions
;
1373 definition
.getProperty
= &Pointer_getProperty
;
1374 definition
.setProperty
= &Pointer_setProperty
;
1375 definition
.finalize
= &CYFinalize
;
1376 Pointer_
= JSClassCreate(&definition
);
1378 definition
= kJSClassDefinitionEmpty
;
1379 definition
.className
= "Struct";
1380 definition
.staticFunctions
= Struct_staticFunctions
;
1381 definition
.getProperty
= &Struct_getProperty
;
1382 definition
.setProperty
= &Struct_setProperty
;
1383 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1384 definition
.finalize
= &CYFinalize
;
1385 Struct_
= JSClassCreate(&definition
);
1387 definition
= kJSClassDefinitionEmpty
;
1388 definition
.className
= "Type";
1389 definition
.staticValues
= Type_staticValues
;
1390 definition
.staticFunctions
= Type_staticFunctions
;
1391 definition
.callAsFunction
= &Type_callAsFunction
;
1392 definition
.callAsConstructor
= &Type_callAsConstructor
;
1393 definition
.finalize
= &CYFinalize
;
1394 Type_privateData::Class_
= JSClassCreate(&definition
);
1396 definition
= kJSClassDefinitionEmpty
;
1397 definition
.className
= "Global";
1398 //definition.getProperty = &Global_getProperty;
1399 Global_
= JSClassCreate(&definition
);
1401 Array_s
= JSStringCreateWithUTF8CString("Array");
1402 cy_s
= JSStringCreateWithUTF8CString("$cy");
1403 length_s
= JSStringCreateWithUTF8CString("length");
1404 message_s
= JSStringCreateWithUTF8CString("message");
1405 name_s
= JSStringCreateWithUTF8CString("name");
1406 pop_s
= JSStringCreateWithUTF8CString("pop");
1407 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1408 push_s
= JSStringCreateWithUTF8CString("push");
1409 splice_s
= JSStringCreateWithUTF8CString("splice");
1410 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1411 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1412 toPointer_s
= JSStringCreateWithUTF8CString("toPointer");
1413 toString_s
= JSStringCreateWithUTF8CString("toString");
1415 Result_
= JSStringCreateWithUTF8CString("_");
1417 if (hooks_
!= NULL
&& hooks_
->Initialize
!= NULL
)
1418 (*hooks_
->Initialize
)();
1421 void CYThrow(JSContextRef context
, JSValueRef value
) {
1423 throw CYJSError(context
, value
);
1426 const char *CYJSError::PoolCString(apr_pool_t
*pool
) const {
1427 // XXX: this used to be CYPoolCString
1428 return CYPoolCCYON(pool
, context_
, value_
);
1431 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1432 // XXX: what if the context is different?
1436 JSValueRef
CYCastJSError(JSContextRef context
, const char *message
) {
1437 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString("Error")));
1439 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1441 JSValueRef
exception(NULL
);
1442 JSValueRef
value(JSObjectCallAsConstructor(context
, Error
, 1, arguments
, &exception
));
1443 CYThrow(context
, exception
);
1448 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1449 return CYCastJSError(context
, message_
);
1452 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1453 _assert(context
!= NULL
);
1458 va_start(args
, format
);
1459 const char *message(apr_pvsprintf(pool
, format
, args
));
1462 value_
= CYCastJSError(context
, message
);
1465 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1466 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1469 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
1470 JSValueRef
exception(NULL
);
1472 CYInitializeDynamic();
1474 JSObjectRef
global(CYGetGlobalObject(context
));
1476 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
1477 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
1479 /* Cache Globals {{{ */
1480 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
1481 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
1483 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
1484 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
1486 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
1487 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
1489 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
1490 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
1492 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
1493 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
1495 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1496 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
1498 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
1499 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
1501 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
1502 CYSetProperty(context
, cy
, CYJSString("String"), String
);
1504 JSObjectRef
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
)));
1505 CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
);
1508 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1509 CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1511 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1512 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1513 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
1515 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1516 JSObjectSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
1517 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
1519 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1520 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1522 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
1523 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
1525 JSObjectRef
alls(JSObjectCallAsConstructor(context
, Array
, 0, NULL
, &exception
));
1526 CYThrow(context
, exception
);
1527 CYSetProperty(context
, cycript
, CYJSString("alls"), alls
);
1530 JSObjectRef
last(NULL
), curr(global
);
1532 goto next
; for (JSValueRef next
;;) {
1533 if (JSValueIsNull(context
, next
))
1536 curr
= CYCastJSObject(context
, next
);
1538 next
= JSObjectGetPrototype(context
, curr
);
1541 JSObjectSetPrototype(context
, last
, all
);
1544 CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
, kJSPropertyAttributeDontEnum
);
1546 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
1547 CYSetProperty(context
, cy
, CYJSString("System"), System
);
1549 CYSetProperty(context
, global
, CYJSString("system"), System
);
1550 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
1551 //CYSetProperty(context, System, CYJSString("global"), global);
1552 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
1554 if (hooks_
!= NULL
&& hooks_
->SetupContext
!= NULL
)
1555 (*hooks_
->SetupContext
)(context
);
1557 CYArrayPush(context
, alls
, cycript
);
1560 JSGlobalContextRef
CYGetJSContext() {
1561 CYInitializeDynamic();
1563 static JSGlobalContextRef context_
;
1565 if (context_
== NULL
) {
1566 context_
= JSGlobalContextCreate(Global_
);
1567 CYSetupContext(context_
);