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
;
168 JSStringRef toPointer_s
;
169 JSStringRef toString_s
;
171 static JSStringRef Result_
;
173 void CYFinalize(JSObjectRef object
) {
174 CYData
*internal(reinterpret_cast<CYData
*>(JSObjectGetPrivate(object
)));
175 if (--internal
->count_
== 0)
179 void Structor_(apr_pool_t
*pool
, sig::Type
*&type
) {
181 type
->primitive
== sig::pointer_P
&&
182 type
->data
.data
.type
!= NULL
&&
183 type
->data
.data
.type
->primitive
== sig::struct_P
&&
184 type
->data
.data
.type
->name
!= NULL
&&
185 strcmp(type
->data
.data
.type
->name
, "_objc_class") == 0
187 type
->primitive
= sig::typename_P
;
188 type
->data
.data
.type
= NULL
;
192 if (type
->primitive
!= sig::struct_P
|| type
->name
== NULL
)
195 size_t length(strlen(type
->name
));
196 char keyed
[length
+ 2];
197 memcpy(keyed
+ 1, type
->name
, length
+ 1);
199 static const char *modes
= "34";
200 for (size_t i(0); i
!= 2; ++i
) {
204 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
207 sig::Parse(pool
, &type
->data
.signature
, entry
->value_
, &Structor_
);
211 sig::Signature signature
;
212 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
213 type
= signature
.elements
[0].type
;
219 JSClassRef
Type_privateData::Class_
;
224 JSGlobalContextRef context_
;
226 Context(JSGlobalContextRef context
) :
235 Type_privateData
*type_
;
238 Pointer(void *value
, JSContextRef context
, JSObjectRef owner
, size_t length
, sig::Type
*type
) :
239 CYOwned(value
, context
, owner
),
240 type_(new(pool_
) Type_privateData(type
)),
246 struct Struct_privateData
:
249 Type_privateData
*type_
;
251 Struct_privateData(JSContextRef context
, JSObjectRef owner
) :
252 CYOwned(NULL
, context
, owner
)
257 typedef std::map
<const char *, Type_privateData
*, CYCStringLess
> TypeMap
;
258 static TypeMap Types_
;
260 JSObjectRef
CYMakeStruct(JSContextRef context
, void *data
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
261 Struct_privateData
*internal(new Struct_privateData(context
, owner
));
262 apr_pool_t
*pool(internal
->pool_
);
263 Type_privateData
*typical(new(pool
) Type_privateData(type
, ffi
));
264 internal
->type_
= typical
;
267 internal
->value_
= data
;
269 size_t size(typical
->GetFFI()->size
);
270 void *copy(apr_palloc(internal
->pool_
, size
));
271 memcpy(copy
, data
, size
);
272 internal
->value_
= copy
;
275 return JSObjectMake(context
, Struct_
, internal
);
278 JSValueRef
CYCastJSValue(JSContextRef context
, bool value
) {
279 return JSValueMakeBoolean(context
, value
);
282 JSValueRef
CYCastJSValue(JSContextRef context
, double value
) {
283 return JSValueMakeNumber(context
, value
);
286 #define CYCastJSValue_(Type_) \
287 JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
288 return JSValueMakeNumber(context, static_cast<double>(value)); \
292 CYCastJSValue_(unsigned int)
293 CYCastJSValue_(long int)
294 CYCastJSValue_(long unsigned int)
295 CYCastJSValue_(long long int)
296 CYCastJSValue_(long long unsigned int)
298 JSValueRef
CYJSUndefined(JSContextRef context
) {
299 return JSValueMakeUndefined(context
);
302 double CYCastDouble(JSContextRef context
, JSValueRef value
) {
303 JSValueRef
exception(NULL
);
304 double number(JSValueToNumber(context
, value
, &exception
));
305 CYThrow(context
, exception
);
309 bool CYCastBool(JSContextRef context
, JSValueRef value
) {
310 return JSValueToBoolean(context
, value
);
313 JSValueRef
CYJSNull(JSContextRef context
) {
314 return JSValueMakeNull(context
);
317 JSValueRef
CYCastJSValue(JSContextRef context
, JSStringRef value
) {
318 return value
== NULL
? CYJSNull(context
) : JSValueMakeString(context
, value
);
321 JSValueRef
CYCastJSValue(JSContextRef context
, const char *value
) {
322 return CYCastJSValue(context
, CYJSString(value
));
325 JSObjectRef
CYCastJSObject(JSContextRef context
, JSValueRef value
) {
326 JSValueRef
exception(NULL
);
327 JSObjectRef
object(JSValueToObject(context
, value
, &exception
));
328 CYThrow(context
, exception
);
332 JSValueRef
CYCallAsFunction(JSContextRef context
, JSObjectRef function
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[]) {
333 JSValueRef
exception(NULL
);
334 JSValueRef
value(JSObjectCallAsFunction(context
, function
, _this
, count
, arguments
, &exception
));
335 CYThrow(context
, exception
);
339 bool CYIsCallable(JSContextRef context
, JSValueRef value
) {
340 return value
!= NULL
&& JSValueIsObject(context
, value
) && JSObjectIsFunction(context
, (JSObjectRef
) value
);
343 static JSValueRef
System_print(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
348 printf("%s\n", CYPoolCString(pool
, context
, arguments
[0]));
351 return CYJSUndefined(context
);
354 static size_t Nonce_(0);
356 static JSValueRef $
cyq(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
358 const char *name(apr_psprintf(pool
, "%s%"APR_SIZE_T_FMT
"", CYPoolCString(pool
, context
, arguments
[0]), Nonce_
++));
359 return CYCastJSValue(context
, name
);
362 static JSValueRef
Cycript_gc_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
363 JSGarbageCollect(context
);
364 return CYJSUndefined(context
);
367 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
368 switch (JSType type
= JSValueGetType(context
, value
)) {
369 case kJSTypeUndefined
:
374 return CYCastBool(context
, value
) ? "true" : "false";
376 case kJSTypeNumber
: {
377 std::ostringstream str
;
378 CYNumerify(str
, CYCastDouble(context
, value
));
379 std::string
value(str
.str());
380 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
383 case kJSTypeString
: {
384 std::ostringstream str
;
385 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, value
)));
386 CYStringify(str
, string
.data
, string
.size
);
387 std::string
value(str
.str());
388 return apr_pstrmemdup(pool
, value
.c_str(), value
.size());
392 return CYPoolCCYON(pool
, context
, (JSObjectRef
) value
);
394 throw CYJSError(context
, "JSValueGetType() == 0x%x", type
);
398 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSValueRef value
) {
399 JSValueRef
exception(NULL
);
400 const char *cyon(CYPoolCCYON(pool
, context
, value
, &exception
));
401 CYThrow(context
, exception
);
405 const char *CYPoolCCYON(apr_pool_t
*pool
, JSContextRef context
, JSObjectRef object
) {
406 JSValueRef
toCYON(CYGetProperty(context
, object
, toCYON_s
));
407 if (CYIsCallable(context
, toCYON
)) {
408 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toCYON
, object
, 0, NULL
));
409 _assert(value
!= NULL
);
410 return CYPoolCString(pool
, context
, value
);
413 JSValueRef
toJSON(CYGetProperty(context
, object
, toJSON_s
));
414 if (CYIsCallable(context
, toJSON
)) {
415 JSValueRef arguments
[1] = {CYCastJSValue(context
, CYJSString(""))};
416 JSValueRef
exception(NULL
);
417 const char *cyon(CYPoolCCYON(pool
, context
, CYCallAsFunction(context
, (JSObjectRef
) toJSON
, object
, 1, arguments
), &exception
));
418 CYThrow(context
, exception
);
422 std::ostringstream str
;
426 // XXX: this is, sadly, going to leak
427 JSPropertyNameArrayRef
names(JSObjectCopyPropertyNames(context
, object
));
431 for (size_t index(0), count(JSPropertyNameArrayGetCount(names
)); index
!= count
; ++index
) {
432 JSStringRef
name(JSPropertyNameArrayGetNameAtIndex(names
, index
));
433 JSValueRef
value(CYGetProperty(context
, object
, name
));
440 CYUTF8String
string(CYPoolUTF8String(pool
, context
, name
));
444 CYStringify(str
, string
.data
, string
.size
);
446 str
<< ':' << CYPoolCCYON(pool
, context
, value
);
451 JSPropertyNameArrayRelease(names
);
453 std::string
string(str
.str());
454 return apr_pstrmemdup(pool
, string
.c_str(), string
.size());
457 static JSValueRef
Array_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
459 std::ostringstream str
;
463 JSValueRef
length(CYGetProperty(context
, _this
, length_s
));
466 for (size_t index(0), count(CYCastDouble(context
, length
)); index
!= count
; ++index
) {
467 JSValueRef
value(CYGetProperty(context
, _this
, index
));
474 if (!JSValueIsUndefined(context
, value
))
475 str
<< CYPoolCCYON(pool
, context
, value
);
484 std::string
value(str
.str());
485 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
488 static JSValueRef
String_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
490 std::ostringstream str
;
492 CYUTF8String
string(CYPoolUTF8String(pool
, context
, CYJSString(context
, _this
)));
493 CYStringify(str
, string
.data
, string
.size
);
495 std::string
value(str
.str());
496 return CYCastJSValue(context
, CYJSString(CYUTF8String(value
.c_str(), value
.size())));
499 JSObjectRef
CYMakePointer(JSContextRef context
, void *pointer
, size_t length
, sig::Type
*type
, ffi_type
*ffi
, JSObjectRef owner
) {
500 Pointer
*internal(new Pointer(pointer
, context
, owner
, length
, type
));
501 return JSObjectMake(context
, Pointer_
, internal
);
504 static JSObjectRef
CYMakeFunctor(JSContextRef context
, void (*function
)(), const char *type
, void **cache
= NULL
) {
505 cy::Functor
*internal
;
507 if (cache
!= NULL
&& *cache
!= NULL
) {
508 internal
= reinterpret_cast<cy::Functor
*>(*cache
);
511 internal
= new cy::Functor(type
, function
);
519 return JSObjectMake(context
, Functor_
, internal
);
522 static bool CYGetOffset(apr_pool_t
*pool
, JSContextRef context
, JSStringRef value
, ssize_t
&index
) {
523 return CYGetOffset(CYPoolCString(pool
, context
, value
), index
);
526 void *CYCastPointer_(JSContextRef context
, JSValueRef value
) {
527 switch (JSValueGetType(context
, value
)) {
530 case kJSTypeObject
: {
531 JSObjectRef
object((JSObjectRef
) value
);
532 if (JSValueIsObjectOfClass(context
, value
, Pointer_
)) {
533 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
534 return internal
->value_
;
536 JSValueRef
toPointer(CYGetProperty(context
, object
, toPointer_s
));
537 if (CYIsCallable(context
, toPointer
)) {
538 JSValueRef
value(CYCallAsFunction(context
, (JSObjectRef
) toPointer
, object
, 0, NULL
));
539 _assert(value
!= NULL
);
540 return CYCastPointer_(context
, value
);
543 double number(CYCastDouble(context
, value
));
544 if (std::isnan(number
))
545 throw CYJSError(context
, "cannot convert value to pointer");
546 return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number
)));
550 void CYPoolFFI(apr_pool_t
*pool
, JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, JSValueRef value
) {
551 switch (type
->primitive
) {
553 *reinterpret_cast<bool *>(data
) = JSValueToBoolean(context
, value
);
556 #define CYPoolFFI_(primitive, native) \
557 case sig::primitive ## _P: \
558 *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
561 CYPoolFFI_(uchar
, unsigned char)
562 CYPoolFFI_(char, char)
563 CYPoolFFI_(ushort
, unsigned short)
564 CYPoolFFI_(short, short)
565 CYPoolFFI_(ulong
, unsigned long)
566 CYPoolFFI_(long, long)
567 CYPoolFFI_(uint
, unsigned int)
569 CYPoolFFI_(ulonglong
, unsigned long long)
570 CYPoolFFI_(longlong
, long long)
571 CYPoolFFI_(float, float)
572 CYPoolFFI_(double, double)
575 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
576 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
577 for (size_t index(0); index
!= type
->data
.data
.size
; ++index
) {
578 ffi_type
*field(ffi
->elements
[index
]);
581 if (aggregate
== NULL
)
584 rhs
= CYGetProperty(context
, aggregate
, index
);
585 if (JSValueIsUndefined(context
, rhs
))
586 throw CYJSError(context
, "unable to extract array value");
589 CYPoolFFI(pool
, context
, type
->data
.data
.type
, field
, base
, rhs
);
596 *reinterpret_cast<void **>(data
) = CYCastPointer
<void *>(context
, value
);
600 *reinterpret_cast<const char **>(data
) = CYPoolCString(pool
, context
, value
);
603 case sig::struct_P
: {
604 uint8_t *base(reinterpret_cast<uint8_t *>(data
));
605 JSObjectRef
aggregate(JSValueIsObject(context
, value
) ? (JSObjectRef
) value
: NULL
);
606 for (size_t index(0); index
!= type
->data
.signature
.count
; ++index
) {
607 sig::Element
*element(&type
->data
.signature
.elements
[index
]);
608 ffi_type
*field(ffi
->elements
[index
]);
611 if (aggregate
== NULL
)
614 rhs
= CYGetProperty(context
, aggregate
, index
);
615 if (JSValueIsUndefined(context
, rhs
)) {
616 if (element
->name
!= NULL
)
617 rhs
= CYGetProperty(context
, aggregate
, CYJSString(element
->name
));
620 if (JSValueIsUndefined(context
, rhs
)) undefined
:
621 throw CYJSError(context
, "unable to extract structure value");
625 CYPoolFFI(pool
, context
, element
->type
, field
, base
, rhs
);
635 if (hooks_
!= NULL
&& hooks_
->PoolFFI
!= NULL
)
636 if ((*hooks_
->PoolFFI
)(pool
, context
, type
, ffi
, data
, value
))
639 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
643 JSValueRef
CYFromFFI(JSContextRef context
, sig::Type
*type
, ffi_type
*ffi
, void *data
, bool initialize
, JSObjectRef owner
) {
644 switch (type
->primitive
) {
646 return CYCastJSValue(context
, *reinterpret_cast<bool *>(data
));
648 #define CYFromFFI_(primitive, native) \
649 case sig::primitive ## _P: \
650 return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
652 CYFromFFI_(uchar, unsigned char)
653 CYFromFFI_(char, char)
654 CYFromFFI_(ushort
, unsigned short)
655 CYFromFFI_(short, short)
656 CYFromFFI_(ulong
, unsigned long)
657 CYFromFFI_(long, long)
658 CYFromFFI_(uint
, unsigned int)
660 CYFromFFI_(ulonglong
, unsigned long long)
661 CYFromFFI_(longlong
, long long)
662 CYFromFFI_(float, float)
663 CYFromFFI_(double, double)
666 if (void *pointer
= data
)
667 return CYMakePointer(context
, pointer
, type
->data
.data
.size
, type
->data
.data
.type
, NULL
, owner
);
671 if (void *pointer
= *reinterpret_cast<void **>(data
))
672 return CYMakePointer(context
, pointer
, _not(size_t), type
->data
.data
.type
, NULL
, owner
);
676 if (char *utf8
= *reinterpret_cast<char **>(data
))
677 return CYCastJSValue(context
, utf8
);
681 return CYMakeStruct(context
, data
, type
, ffi
, owner
);
683 return CYJSUndefined(context
);
686 return CYJSNull(context
);
688 if (hooks_
!= NULL
&& hooks_
->FromFFI
!= NULL
)
689 if (JSValueRef value
= (*hooks_
->FromFFI
)(context
, type
, ffi
, data
, initialize
, owner
))
692 CYThrow("unimplemented signature code: '%c''\n", type
->primitive
);
696 static void FunctionClosure_(ffi_cif
*cif
, void *result
, void **arguments
, void *arg
) {
697 Closure_privateData
*internal(reinterpret_cast<Closure_privateData
*>(arg
));
699 JSContextRef
context(internal
->context_
);
701 size_t count(internal
->cif_
.nargs
);
702 JSValueRef values
[count
];
704 for (size_t index(0); index
!= count
; ++index
)
705 values
[index
] = CYFromFFI(context
, internal
->signature_
.elements
[1 + index
].type
, internal
->cif_
.arg_types
[index
], arguments
[index
]);
707 JSValueRef
value(CYCallAsFunction(context
, internal
->function_
, NULL
, count
, values
));
708 CYPoolFFI(NULL
, context
, internal
->signature_
.elements
[0].type
, internal
->cif_
.rtype
, result
, value
);
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 ffi_closure
*closure((ffi_closure
*) _syscall(mmap(
717 NULL
, sizeof(ffi_closure
),
718 PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
,
722 ffi_status
status(ffi_prep_closure(closure
, &internal
->cif_
, callback
, internal
));
723 _assert(status
== FFI_OK
);
725 _syscall(mprotect(closure
, sizeof(*closure
), PROT_READ
| PROT_EXEC
));
727 internal
->value_
= closure
;
732 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSObjectRef function
, const char *type
) {
733 Closure_privateData
*internal(CYMakeFunctor_(context
, function
, type
, &FunctionClosure_
));
734 JSObjectRef
object(JSObjectMake(context
, Functor_
, internal
));
735 // XXX: see above notes about needing to leak
736 JSValueProtect(CYGetJSContext(context
), object
);
740 JSObjectRef
CYGetCachedObject(JSContextRef context
, JSStringRef name
) {
741 return CYCastJSObject(context
, CYGetProperty(context
, CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
)), name
));
744 static JSObjectRef
CYMakeFunctor(JSContextRef context
, JSValueRef value
, const char *type
) {
745 JSObjectRef
Function(CYGetCachedObject(context
, CYJSString("Function")));
747 JSValueRef
exception(NULL
);
748 bool function(JSValueIsInstanceOfConstructor(context
, value
, Function
, &exception
));
749 CYThrow(context
, exception
);
752 JSObjectRef
function(CYCastJSObject(context
, value
));
753 return CYMakeFunctor(context
, function
, type
);
755 void (*function
)()(CYCastPointer
<void (*)()>(context
, value
));
756 return CYMakeFunctor(context
, function
, type
);
760 static bool Index_(apr_pool_t
*pool
, JSContextRef context
, Struct_privateData
*internal
, JSStringRef property
, ssize_t
&index
, uint8_t *&base
) {
761 Type_privateData
*typical(internal
->type_
);
762 sig::Type
*type(typical
->type_
);
766 const char *name(CYPoolCString(pool
, context
, property
));
767 size_t length(strlen(name
));
768 double number(CYCastDouble(name
, length
));
770 size_t count(type
->data
.signature
.count
);
772 if (std::isnan(number
)) {
773 if (property
== NULL
)
776 sig::Element
*elements(type
->data
.signature
.elements
);
778 for (size_t local(0); local
!= count
; ++local
) {
779 sig::Element
*element(&elements
[local
]);
780 if (element
->name
!= NULL
&& strcmp(name
, element
->name
) == 0) {
788 index
= static_cast<ssize_t
>(number
);
789 if (index
!= number
|| index
< 0 || static_cast<size_t>(index
) >= count
)
794 ffi_type
**elements(typical
->GetFFI()->elements
);
796 base
= reinterpret_cast<uint8_t *>(internal
->value_
);
797 for (ssize_t
local(0); local
!= index
; ++local
)
798 base
+= elements
[local
]->size
;
803 static JSValueRef
Pointer_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
805 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
807 if (JSStringIsEqual(property
, length_s
))
808 return internal
->length_
== _not(size_t) ? CYJSUndefined(context
) : CYCastJSValue(context
, internal
->length_
);
810 Type_privateData
*typical(internal
->type_
);
812 if (typical
->type_
== NULL
)
816 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
818 else if (!CYGetOffset(pool
, context
, property
, offset
))
821 ffi_type
*ffi(typical
->GetFFI());
823 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
824 base
+= ffi
->size
* offset
;
826 JSObjectRef
owner(internal
->GetOwner() ?: object
);
827 return CYFromFFI(context
, typical
->type_
, ffi
, base
, false, owner
);
830 static bool Pointer_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
832 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(object
)));
833 Type_privateData
*typical(internal
->type_
);
835 if (typical
->type_
== NULL
)
839 if (JSStringIsEqualToUTF8CString(property
, "$cyi"))
841 else if (!CYGetOffset(pool
, context
, property
, offset
))
844 ffi_type
*ffi(typical
->GetFFI());
846 uint8_t *base(reinterpret_cast<uint8_t *>(internal
->value_
));
847 base
+= ffi
->size
* offset
;
849 CYPoolFFI(NULL
, context
, typical
->type_
, ffi
, base
, value
);
853 static JSValueRef Struct_callAsFunction_$
cya(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
854 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(_this
)));
855 Type_privateData
*typical(internal
->type_
);
856 return CYMakePointer(context
, internal
->value_
, _not(size_t), typical
->type_
, typical
->ffi_
, _this
);
859 static JSValueRef
Struct_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
861 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
862 Type_privateData
*typical(internal
->type_
);
867 if (!Index_(pool
, context
, internal
, property
, index
, base
))
870 JSObjectRef
owner(internal
->GetOwner() ?: object
);
872 return CYFromFFI(context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, false, owner
);
875 static bool Struct_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
877 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
878 Type_privateData
*typical(internal
->type_
);
883 if (!Index_(pool
, context
, internal
, property
, index
, base
))
886 CYPoolFFI(NULL
, context
, typical
->type_
->data
.signature
.elements
[index
].type
, typical
->GetFFI()->elements
[index
], base
, value
);
890 static void Struct_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
891 Struct_privateData
*internal(reinterpret_cast<Struct_privateData
*>(JSObjectGetPrivate(object
)));
892 Type_privateData
*typical(internal
->type_
);
893 sig::Type
*type(typical
->type_
);
898 size_t count(type
->data
.signature
.count
);
899 sig::Element
*elements(type
->data
.signature
.elements
);
903 for (size_t index(0); index
!= count
; ++index
) {
905 name
= elements
[index
].name
;
908 sprintf(number
, "%zu", index
);
912 JSPropertyNameAccumulatorAddName(names
, CYJSString(name
));
916 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
{
917 if (setups
+ count
!= signature
->count
- 1)
918 throw CYJSError(context
, "incorrect number of arguments to ffi function");
920 size_t size(setups
+ count
);
922 memcpy(values
, setup
, sizeof(void *) * setups
);
924 for (size_t index(setups
); index
!= size
; ++index
) {
925 sig::Element
*element(&signature
->elements
[index
+ 1]);
926 ffi_type
*ffi(cif
->arg_types
[index
]);
928 values
[index
] = new(pool
) uint8_t[ffi
->size
];
929 CYPoolFFI(pool
, context
, element
->type
, ffi
, values
[index
], arguments
[index
- setups
]);
932 uint8_t value
[cif
->rtype
->size
];
934 if (hooks_
!= NULL
&& hooks_
->CallFunction
!= NULL
)
935 (*hooks_
->CallFunction
)(context
, cif
, function
, value
, values
);
937 ffi_call(cif
, function
, value
, values
);
939 return CYFromFFI(context
, signature
->elements
[0].type
, cif
->rtype
, value
, initialize
);
942 static JSValueRef
Functor_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
944 cy::Functor
*internal(reinterpret_cast<cy::Functor
*>(JSObjectGetPrivate(object
)));
945 return CYCallFunction(pool
, context
, 0, NULL
, count
, arguments
, false, exception
, &internal
->signature_
, &internal
->cif_
, internal
->GetValue());
948 static JSObjectRef
CYMakeType(JSContextRef context
, const char *type
) {
949 Type_privateData
*internal(new Type_privateData(type
));
950 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
953 static JSObjectRef
CYMakeType(JSContextRef context
, sig::Type
*type
) {
954 Type_privateData
*internal(new Type_privateData(type
));
955 return JSObjectMake(context
, Type_privateData::Class_
, internal
);
958 static void *CYCastSymbol(const char *name
) {
959 return dlsym(RTLD_DEFAULT
, name
);
962 static JSValueRef
All_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
963 JSObjectRef
global(CYGetGlobalObject(context
));
964 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
965 if (JSValueRef value
= CYGetProperty(context
, cycript
, property
))
966 if (!JSValueIsUndefined(context
, value
))
970 CYUTF8String
name(CYPoolUTF8String(pool
, context
, property
));
972 if (hooks_
!= NULL
&& hooks_
->RuntimeProperty
!= NULL
)
973 if (JSValueRef value
= (*hooks_
->RuntimeProperty
)(context
, name
))
976 size_t length(name
.size
);
977 char keyed
[length
+ 2];
978 memcpy(keyed
+ 1, name
.data
, length
+ 1);
980 static const char *modes
= "0124";
981 for (size_t i(0); i
!= 4; ++i
) {
985 if (CYBridgeEntry
*entry
= CYBridgeHash(keyed
, length
+ 1))
988 return JSEvaluateScript(CYGetJSContext(context
), CYJSString(entry
->value_
), NULL
, NULL
, 0, NULL
);
991 if (void (*symbol
)() = reinterpret_cast<void (*)()>(CYCastSymbol(name
.data
)))
992 return CYMakeFunctor(context
, symbol
, entry
->value_
, &entry
->cache_
);
996 if (void *symbol
= CYCastSymbol(name
.data
)) {
997 // XXX: this is horrendously inefficient
998 sig::Signature signature
;
999 sig::Parse(pool
, &signature
, entry
->value_
, &Structor_
);
1001 sig::sig_ffi_cif(pool
, &sig::ObjectiveC
, &signature
, &cif
);
1002 return CYFromFFI(context
, signature
.elements
[0].type
, cif
.rtype
, symbol
);
1005 // XXX: implement case 3
1007 return CYMakeType(context
, entry
->value_
);
1014 static JSObjectRef
Pointer_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1016 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1020 void *value(CYCastPointer
<void *>(context
, arguments
[0]));
1021 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1023 sig::Signature signature
;
1024 sig::Parse(pool
, &signature
, type
, &Structor_
);
1026 return CYMakePointer(context
, value
, _not(size_t), signature
.elements
[0].type
, NULL
, NULL
);
1029 static JSObjectRef
Type_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1031 throw CYJSError(context
, "incorrect number of arguments to Type constructor");
1033 const char *type(CYPoolCString(pool
, context
, arguments
[0]));
1034 return CYMakeType(context
, type
);
1037 static JSValueRef
Type_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1038 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1042 if (JSStringIsEqualToUTF8CString(property
, "$cyi")) {
1043 type
.primitive
= sig::pointer_P
;
1044 type
.data
.data
.size
= 0;
1047 size_t index(CYGetIndex(pool
, context
, property
));
1048 if (index
== _not(size_t))
1050 type
.primitive
= sig::array_P
;
1051 type
.data
.data
.size
= index
;
1057 type
.data
.data
.type
= internal
->type_
;
1059 return CYMakeType(context
, &type
);
1062 static JSValueRef
Type_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1063 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1066 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1067 sig::Type
*type(internal
->type_
);
1068 ffi_type
*ffi(internal
->GetFFI());
1070 uint8_t value
[ffi
->size
];
1072 CYPoolFFI(pool
, context
, type
, ffi
, value
, arguments
[0]);
1073 return CYFromFFI(context
, type
, ffi
, value
);
1076 static JSObjectRef
Type_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1078 throw CYJSError(context
, "incorrect number of arguments to type cast function");
1079 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(object
)));
1081 sig::Type
*type(internal
->type_
);
1084 if (type
->primitive
!= sig::array_P
)
1085 length
= _not(size_t);
1087 length
= type
->data
.data
.size
;
1088 type
= type
->data
.data
.type
;
1091 void *value(malloc(internal
->GetFFI()->size
));
1092 return CYMakePointer(context
, value
, length
, type
, NULL
, NULL
);
1095 static JSObjectRef
Functor_new(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1097 throw CYJSError(context
, "incorrect number of arguments to Functor constructor");
1099 const char *type(CYPoolCString(pool
, context
, arguments
[1]));
1100 return CYMakeFunctor(context
, arguments
[0], type
);
1103 static JSValueRef
CYValue_callAsFunction_valueOf(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1104 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1105 return CYCastJSValue(context
, reinterpret_cast<uintptr_t>(internal
->value_
));
1108 static JSValueRef
CYValue_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1109 return CYValue_callAsFunction_valueOf(context
, object
, _this
, count
, arguments
, exception
);
1112 static JSValueRef
CYValue_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1113 CYValue
*internal(reinterpret_cast<CYValue
*>(JSObjectGetPrivate(_this
)));
1115 sprintf(string
, "%p", internal
->value_
);
1116 return CYCastJSValue(context
, string
);
1119 static JSValueRef
Pointer_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1120 Pointer
*internal(reinterpret_cast<Pointer
*>(JSObjectGetPrivate(_this
)));
1121 if (internal
->length_
!= _not(size_t)) {
1122 JSObjectRef
Array(CYGetCachedObject(context
, Array_s
));
1123 JSObjectRef
toCYON(CYCastJSObject(context
, CYGetProperty(context
, Array
, toCYON_s
)));
1124 return CYCallAsFunction(context
, toCYON
, _this
, count
, arguments
);
1127 sprintf(string
, "%p", internal
->value_
);
1128 return CYCastJSValue(context
, string
);
1132 static JSValueRef
Type_callAsFunction_toString(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1133 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1135 const char *type(sig::Unparse(pool
, internal
->type_
));
1136 return CYCastJSValue(context
, CYJSString(type
));
1139 static JSValueRef
Type_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1140 Type_privateData
*internal(reinterpret_cast<Type_privateData
*>(JSObjectGetPrivate(_this
)));
1142 const char *type(sig::Unparse(pool
, internal
->type_
));
1143 size_t size(strlen(type
));
1144 char *cyon(new(pool
) char[12 + size
+ 1]);
1145 memcpy(cyon
, "new Type(\"", 10);
1146 cyon
[12 + size
] = '\0';
1147 cyon
[12 + size
- 2] = '"';
1148 cyon
[12 + size
- 1] = ')';
1149 memcpy(cyon
+ 10, type
, size
);
1150 return CYCastJSValue(context
, CYJSString(cyon
));
1153 static JSValueRef
Type_callAsFunction_toJSON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) {
1154 return Type_callAsFunction_toString(context
, object
, _this
, count
, arguments
, exception
);
1157 static JSStaticFunction Pointer_staticFunctions
[4] = {
1158 {"toCYON", &Pointer_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1159 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1160 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1164 static JSStaticFunction Struct_staticFunctions
[2] = {
1165 {"$cya", &Struct_callAsFunction_$cya
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1169 static JSStaticFunction Functor_staticFunctions
[4] = {
1170 {"toCYON", &CYValue_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1171 {"toJSON", &CYValue_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1172 {"valueOf", &CYValue_callAsFunction_valueOf
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1177 JSStaticFunction
const * const Functor::StaticFunctions
= Functor_staticFunctions
;
1180 static JSStaticFunction Type_staticFunctions
[4] = {
1181 {"toCYON", &Type_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1182 {"toJSON", &Type_callAsFunction_toJSON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1183 {"toString", &Type_callAsFunction_toString
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1187 static JSObjectRef (*JSObjectMakeArray$
)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*);
1189 void CYSetArgs(int argc
, const char *argv
[]) {
1190 JSContextRef
context(CYGetJSContext());
1191 JSValueRef args
[argc
];
1192 for (int i(0); i
!= argc
; ++i
)
1193 args
[i
] = CYCastJSValue(context
, argv
[i
]);
1196 if (JSObjectMakeArray$
!= NULL
) {
1197 JSValueRef
exception(NULL
);
1198 array
= (*JSObjectMakeArray$
)(context
, argc
, args
, &exception
);
1199 CYThrow(context
, exception
);
1201 JSObjectRef
Array(CYGetCachedObject(context
, CYJSString("Array")));
1202 JSValueRef
value(CYCallAsFunction(context
, Array
, NULL
, argc
, args
));
1203 array
= CYCastJSObject(context
, value
);
1206 JSObjectRef
System(CYGetCachedObject(context
, CYJSString("System")));
1207 CYSetProperty(context
, System
, CYJSString("args"), array
);
1210 JSObjectRef
CYGetGlobalObject(JSContextRef context
) {
1211 return JSContextGetGlobalObject(context
);
1214 const char *CYExecute(apr_pool_t
*pool
, CYUTF8String code
) {
1215 JSContextRef
context(CYGetJSContext());
1216 JSValueRef
exception(NULL
), result
;
1219 if (hooks_
!= NULL
&& hooks_
->ExecuteStart
!= NULL
)
1220 handle
= (*hooks_
->ExecuteStart
)(context
);
1227 result
= JSEvaluateScript(context
, CYJSString(code
), NULL
, NULL
, 0, &exception
);
1228 } catch (const char *error
) {
1232 if (exception
!= NULL
) { error
:
1237 if (JSValueIsUndefined(context
, result
))
1241 json
= CYPoolCCYON(pool
, context
, result
, &exception
);
1242 } catch (const char *error
) {
1246 if (exception
!= NULL
)
1249 CYSetProperty(context
, CYGetGlobalObject(context
), Result_
, result
);
1251 if (hooks_
!= NULL
&& hooks_
->ExecuteEnd
!= NULL
)
1252 (*hooks_
->ExecuteEnd
)(context
, handle
);
1256 extern "C" void CydgetSetupContext(JSGlobalContextRef context
) {
1257 CYSetupContext(context
);
1260 static bool initialized_
= false;
1262 void CYInitializeDynamic() {
1264 initialized_
= true;
1267 CYInitializeStatic();
1269 JSObjectMakeArray$
= reinterpret_cast<JSObjectRef (*)(JSContextRef
, size_t, const JSValueRef
[], JSValueRef
*)>(dlsym(RTLD_DEFAULT
, "JSObjectMakeArray"));
1271 JSClassDefinition definition
;
1273 definition
= kJSClassDefinitionEmpty
;
1274 definition
.className
= "All";
1275 definition
.getProperty
= &All_getProperty
;
1276 All_
= JSClassCreate(&definition
);
1278 definition
= kJSClassDefinitionEmpty
;
1279 definition
.className
= "Context";
1280 definition
.finalize
= &CYFinalize
;
1281 Context_
= JSClassCreate(&definition
);
1283 definition
= kJSClassDefinitionEmpty
;
1284 definition
.className
= "Functor";
1285 definition
.staticFunctions
= cy::Functor::StaticFunctions
;
1286 definition
.callAsFunction
= &Functor_callAsFunction
;
1287 definition
.finalize
= &CYFinalize
;
1288 Functor_
= JSClassCreate(&definition
);
1290 definition
= kJSClassDefinitionEmpty
;
1291 definition
.className
= "Pointer";
1292 definition
.staticFunctions
= Pointer_staticFunctions
;
1293 definition
.getProperty
= &Pointer_getProperty
;
1294 definition
.setProperty
= &Pointer_setProperty
;
1295 definition
.finalize
= &CYFinalize
;
1296 Pointer_
= JSClassCreate(&definition
);
1298 definition
= kJSClassDefinitionEmpty
;
1299 definition
.className
= "Struct";
1300 definition
.staticFunctions
= Struct_staticFunctions
;
1301 definition
.getProperty
= &Struct_getProperty
;
1302 definition
.setProperty
= &Struct_setProperty
;
1303 definition
.getPropertyNames
= &Struct_getPropertyNames
;
1304 definition
.finalize
= &CYFinalize
;
1305 Struct_
= JSClassCreate(&definition
);
1307 definition
= kJSClassDefinitionEmpty
;
1308 definition
.className
= "Type";
1309 definition
.staticFunctions
= Type_staticFunctions
;
1310 definition
.getProperty
= &Type_getProperty
;
1311 definition
.callAsFunction
= &Type_callAsFunction
;
1312 definition
.callAsConstructor
= &Type_callAsConstructor
;
1313 definition
.finalize
= &CYFinalize
;
1314 Type_privateData::Class_
= JSClassCreate(&definition
);
1316 definition
= kJSClassDefinitionEmpty
;
1317 definition
.className
= "Global";
1318 //definition.getProperty = &Global_getProperty;
1319 Global_
= JSClassCreate(&definition
);
1321 Array_s
= JSStringCreateWithUTF8CString("Array");
1322 cy_s
= JSStringCreateWithUTF8CString("$cy");
1323 length_s
= JSStringCreateWithUTF8CString("length");
1324 message_s
= JSStringCreateWithUTF8CString("message");
1325 name_s
= JSStringCreateWithUTF8CString("name");
1326 pop_s
= JSStringCreateWithUTF8CString("pop");
1327 prototype_s
= JSStringCreateWithUTF8CString("prototype");
1328 push_s
= JSStringCreateWithUTF8CString("push");
1329 splice_s
= JSStringCreateWithUTF8CString("splice");
1330 toCYON_s
= JSStringCreateWithUTF8CString("toCYON");
1331 toJSON_s
= JSStringCreateWithUTF8CString("toJSON");
1332 toPointer_s
= JSStringCreateWithUTF8CString("toPointer");
1333 toString_s
= JSStringCreateWithUTF8CString("toString");
1335 Result_
= JSStringCreateWithUTF8CString("_");
1337 if (hooks_
!= NULL
&& hooks_
->Initialize
!= NULL
)
1338 (*hooks_
->Initialize
)();
1341 void CYThrow(JSContextRef context
, JSValueRef value
) {
1343 throw CYJSError(context
, value
);
1346 const char *CYJSError::PoolCString(apr_pool_t
*pool
) const {
1347 // XXX: this used to be CYPoolCString
1348 return CYPoolCCYON(pool
, context_
, value_
);
1351 JSValueRef
CYJSError::CastJSValue(JSContextRef context
) const {
1352 // XXX: what if the context is different?
1356 JSValueRef
CYCastJSError(JSContextRef context
, const char *message
) {
1357 JSObjectRef
Error(CYGetCachedObject(context
, CYJSString("Error")));
1359 JSValueRef arguments
[1] = {CYCastJSValue(context
, message
)};
1361 JSValueRef
exception(NULL
);
1362 JSValueRef
value(JSObjectCallAsConstructor(context
, Error
, 1, arguments
, &exception
));
1363 CYThrow(context
, exception
);
1368 JSValueRef
CYPoolError::CastJSValue(JSContextRef context
) const {
1369 return CYCastJSError(context
, message_
);
1372 CYJSError::CYJSError(JSContextRef context
, const char *format
, ...) {
1373 _assert(context
!= NULL
);
1378 va_start(args
, format
);
1379 const char *message(apr_pvsprintf(pool
, format
, args
));
1382 value_
= CYCastJSError(context
, message
);
1385 JSGlobalContextRef
CYGetJSContext(JSContextRef context
) {
1386 return reinterpret_cast<Context
*>(JSObjectGetPrivate(CYCastJSObject(context
, CYGetProperty(context
, CYGetGlobalObject(context
), cy_s
))))->context_
;
1389 extern "C" void CYSetupContext(JSGlobalContextRef context
) {
1390 CYInitializeDynamic();
1392 JSObjectRef
global(CYGetGlobalObject(context
));
1394 JSObjectRef
cy(JSObjectMake(context
, Context_
, new Context(context
)));
1395 CYSetProperty(context
, global
, cy_s
, cy
, kJSPropertyAttributeDontEnum
);
1397 /* Cache Globals {{{ */
1398 JSObjectRef
Array(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Array"))));
1399 CYSetProperty(context
, cy
, CYJSString("Array"), Array
);
1401 JSObjectRef
Array_prototype(CYCastJSObject(context
, CYGetProperty(context
, Array
, prototype_s
)));
1402 CYSetProperty(context
, cy
, CYJSString("Array_prototype"), Array_prototype
);
1404 JSObjectRef
Error(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Error"))));
1405 CYSetProperty(context
, cy
, CYJSString("Error"), Error
);
1407 JSObjectRef
Function(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Function"))));
1408 CYSetProperty(context
, cy
, CYJSString("Function"), Function
);
1410 JSObjectRef
Function_prototype(CYCastJSObject(context
, CYGetProperty(context
, Function
, prototype_s
)));
1411 CYSetProperty(context
, cy
, CYJSString("Function_prototype"), Function_prototype
);
1413 JSObjectRef
Object(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Object"))));
1414 CYSetProperty(context
, cy
, CYJSString("Object"), Object
);
1416 JSObjectRef
Object_prototype(CYCastJSObject(context
, CYGetProperty(context
, Object
, prototype_s
)));
1417 CYSetProperty(context
, cy
, CYJSString("Object_prototype"), Object_prototype
);
1419 JSObjectRef
String(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("String"))));
1420 CYSetProperty(context
, cy
, CYJSString("String"), String
);
1422 JSObjectRef
String_prototype(CYCastJSObject(context
, CYGetProperty(context
, String
, prototype_s
)));
1423 CYSetProperty(context
, cy
, CYJSString("String_prototype"), String_prototype
);
1426 CYSetProperty(context
, Array_prototype
, toCYON_s
, &Array_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1427 CYSetProperty(context
, String_prototype
, toCYON_s
, &String_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
);
1429 JSObjectRef
cycript(JSObjectMake(context
, NULL
, NULL
));
1430 CYSetProperty(context
, global
, CYJSString("Cycript"), cycript
);
1431 CYSetProperty(context
, cycript
, CYJSString("gc"), &Cycript_gc_callAsFunction
);
1433 JSObjectRef
Functor(JSObjectMakeConstructor(context
, Functor_
, &Functor_new
));
1434 JSObjectSetPrototype(context
, CYCastJSObject(context
, CYGetProperty(context
, Functor
, prototype_s
)), Function_prototype
);
1435 CYSetProperty(context
, cycript
, CYJSString("Functor"), Functor
);
1437 CYSetProperty(context
, cycript
, CYJSString("Pointer"), JSObjectMakeConstructor(context
, Pointer_
, &Pointer_new
));
1438 CYSetProperty(context
, cycript
, CYJSString("Type"), JSObjectMakeConstructor(context
, Type_privateData::Class_
, &Type_new
));
1440 JSObjectRef
all(JSObjectMake(context
, All_
, NULL
));
1441 CYSetProperty(context
, cycript
, CYJSString("all"), all
);
1444 JSObjectRef
last(NULL
), curr(global
);
1446 goto next
; for (JSValueRef next
;;) {
1447 if (JSValueIsNull(context
, next
))
1450 curr
= CYCastJSObject(context
, next
);
1452 next
= JSObjectGetPrototype(context
, curr
);
1455 JSObjectSetPrototype(context
, last
, all
);
1458 CYSetProperty(context
, global
, CYJSString("$cyq"), &$cyq
, kJSPropertyAttributeDontEnum
);
1460 JSObjectRef
System(JSObjectMake(context
, NULL
, NULL
));
1461 CYSetProperty(context
, cy
, CYJSString("System"), System
);
1463 CYSetProperty(context
, global
, CYJSString("system"), System
);
1464 CYSetProperty(context
, System
, CYJSString("args"), CYJSNull(context
));
1465 //CYSetProperty(context, System, CYJSString("global"), global);
1466 CYSetProperty(context
, System
, CYJSString("print"), &System_print
);
1468 if (hooks_
!= NULL
&& hooks_
->SetupContext
!= NULL
)
1469 (*hooks_
->SetupContext
)(context
);
1472 JSGlobalContextRef
CYGetJSContext() {
1473 CYInitializeDynamic();
1475 static JSGlobalContextRef context_
;
1477 if (context_
== NULL
) {
1478 context_
= JSGlobalContextCreate(Global_
);
1479 CYSetupContext(context_
);