]>
Commit | Line | Data |
---|---|---|
b3378a02 | 1 | /* Cycript - Optimizing JavaScript Compiler/Runtime |
c15969fd | 2 | * Copyright (C) 2009-2013 Jay Freeman (saurik) |
9cad30fa JF |
3 | */ |
4 | ||
c15969fd | 5 | /* GNU General Public License, Version 3 {{{ */ |
9cad30fa | 6 | /* |
c15969fd JF |
7 | * Cycript is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published | |
9 | * by the Free Software Foundation, either version 3 of the License, | |
10 | * or (at your option) any later version. | |
9cad30fa | 11 | * |
c15969fd JF |
12 | * Cycript is distributed in the hope that it will be useful, but |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
9cad30fa | 16 | * |
c15969fd | 17 | * You should have received a copy of the GNU General Public License |
b3378a02 JF |
18 | * along with Cycript. If not, see <http://www.gnu.org/licenses/>. |
19 | **/ | |
9cad30fa JF |
20 | /* }}} */ |
21 | ||
9cad30fa JF |
22 | #include "Internal.hpp" |
23 | ||
24 | #include <dlfcn.h> | |
9cad30fa JF |
25 | |
26 | #include "cycript.hpp" | |
27 | ||
28 | #include "sig/parse.hpp" | |
29 | #include "sig/ffi_type.hpp" | |
30 | ||
31 | #include "Pooling.hpp" | |
2f51d6ab | 32 | #include "Execute.hpp" |
9cad30fa JF |
33 | |
34 | #include <sys/mman.h> | |
35 | ||
36 | #include <iostream> | |
9cad30fa JF |
37 | #include <set> |
38 | #include <map> | |
39 | #include <iomanip> | |
40 | #include <sstream> | |
41 | #include <cmath> | |
42 | ||
43 | #include "Parser.hpp" | |
9cad30fa JF |
44 | |
45 | #include "Error.hpp" | |
46 | #include "JavaScript.hpp" | |
47 | #include "String.hpp" | |
48 | ||
9cad30fa JF |
49 | struct CYHooks *hooks_; |
50 | ||
51 | /* JavaScript Properties {{{ */ | |
52 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index) { | |
f1b5a47f | 53 | return _jsccall(JSObjectGetPropertyAtIndex, context, object, index); |
9cad30fa JF |
54 | } |
55 | ||
56 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name) { | |
f1b5a47f | 57 | return _jsccall(JSObjectGetProperty, context, object, name); |
9cad30fa JF |
58 | } |
59 | ||
60 | void CYSetProperty(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value) { | |
f1b5a47f | 61 | _jsccall(JSObjectSetPropertyAtIndex, context, object, index, value); |
9cad30fa JF |
62 | } |
63 | ||
64 | void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value, JSPropertyAttributes attributes) { | |
f1b5a47f | 65 | _jsccall(JSObjectSetProperty, context, object, name, value, attributes); |
9cad30fa JF |
66 | } |
67 | ||
68 | void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef (*callback)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *), JSPropertyAttributes attributes) { | |
69 | CYSetProperty(context, object, name, JSObjectMakeFunctionWithCallback(context, name, callback), attributes); | |
70 | } | |
71 | /* }}} */ | |
72 | /* JavaScript Strings {{{ */ | |
73 | JSStringRef CYCopyJSString(const char *value) { | |
74 | return value == NULL ? NULL : JSStringCreateWithUTF8CString(value); | |
75 | } | |
76 | ||
77 | JSStringRef CYCopyJSString(JSStringRef value) { | |
78 | return value == NULL ? NULL : JSStringRetain(value); | |
79 | } | |
80 | ||
81 | JSStringRef CYCopyJSString(CYUTF8String value) { | |
82 | // XXX: this is very wrong; it needs to convert to UTF16 and then create from there | |
83 | return CYCopyJSString(value.data); | |
84 | } | |
85 | ||
86 | JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value) { | |
87 | if (JSValueIsNull(context, value)) | |
88 | return NULL; | |
f1b5a47f | 89 | return _jsccall(JSValueToStringCopy, context, value); |
9cad30fa JF |
90 | } |
91 | ||
92 | static CYUTF16String CYCastUTF16String(JSStringRef value) { | |
93 | return CYUTF16String(JSStringGetCharactersPtr(value), JSStringGetLength(value)); | |
94 | } | |
95 | ||
b799113b | 96 | CYUTF8String CYPoolUTF8String(CYPool &pool, JSContextRef context, JSStringRef value) { |
9cad30fa JF |
97 | return CYPoolUTF8String(pool, CYCastUTF16String(value)); |
98 | } | |
99 | ||
b799113b | 100 | const char *CYPoolCString(CYPool &pool, JSContextRef context, JSStringRef value) { |
9cad30fa JF |
101 | CYUTF8String utf8(CYPoolUTF8String(pool, context, value)); |
102 | _assert(memchr(utf8.data, '\0', utf8.size) == NULL); | |
103 | return utf8.data; | |
104 | } | |
105 | ||
b799113b | 106 | const char *CYPoolCString(CYPool &pool, JSContextRef context, JSValueRef value) { |
9cad30fa JF |
107 | return JSValueIsNull(context, value) ? NULL : CYPoolCString(pool, context, CYJSString(context, value)); |
108 | } | |
109 | /* }}} */ | |
110 | /* Index Offsets {{{ */ | |
b799113b | 111 | size_t CYGetIndex(CYPool &pool, JSContextRef context, JSStringRef value) { |
9cad30fa JF |
112 | return CYGetIndex(CYPoolUTF8String(pool, context, value)); |
113 | } | |
114 | /* }}} */ | |
115 | ||
116 | static JSClassRef All_; | |
117 | static JSClassRef Context_; | |
3f9ae37c | 118 | JSClassRef Functor_; |
9cad30fa JF |
119 | static JSClassRef Global_; |
120 | static JSClassRef Pointer_; | |
121 | static JSClassRef Struct_; | |
122 | ||
123 | JSStringRef Array_s; | |
124 | JSStringRef cy_s; | |
125 | JSStringRef length_s; | |
126 | JSStringRef message_s; | |
127 | JSStringRef name_s; | |
128 | JSStringRef pop_s; | |
129 | JSStringRef prototype_s; | |
130 | JSStringRef push_s; | |
131 | JSStringRef splice_s; | |
132 | JSStringRef toCYON_s; | |
133 | JSStringRef toJSON_s; | |
20ded97a | 134 | JSStringRef toPointer_s; |
4cb8aa43 | 135 | JSStringRef toString_s; |
9cad30fa JF |
136 | |
137 | static JSStringRef Result_; | |
138 | ||
9cad30fa | 139 | void CYFinalize(JSObjectRef object) { |
1850a470 | 140 | CYData *internal(reinterpret_cast<CYData *>(JSObjectGetPrivate(object))); |
b799113b | 141 | _assert(internal->count_ != _not(unsigned)); |
1850a470 JF |
142 | if (--internal->count_ == 0) |
143 | delete internal; | |
9cad30fa JF |
144 | } |
145 | ||
b799113b | 146 | void Structor_(CYPool &pool, sig::Type *&type) { |
9cad30fa JF |
147 | if ( |
148 | type->primitive == sig::pointer_P && | |
149 | type->data.data.type != NULL && | |
150 | type->data.data.type->primitive == sig::struct_P && | |
1648ddb9 | 151 | type->data.data.type->name != NULL && |
9cad30fa JF |
152 | strcmp(type->data.data.type->name, "_objc_class") == 0 |
153 | ) { | |
154 | type->primitive = sig::typename_P; | |
155 | type->data.data.type = NULL; | |
156 | return; | |
157 | } | |
158 | ||
159 | if (type->primitive != sig::struct_P || type->name == NULL) | |
160 | return; | |
161 | ||
2f51d6ab JF |
162 | size_t length(strlen(type->name)); |
163 | char keyed[length + 2]; | |
164 | memcpy(keyed + 1, type->name, length + 1); | |
165 | ||
166 | static const char *modes = "34"; | |
167 | for (size_t i(0); i != 2; ++i) { | |
168 | char mode(modes[i]); | |
169 | keyed[0] = mode; | |
170 | ||
171 | if (CYBridgeEntry *entry = CYBridgeHash(keyed, length + 1)) | |
172 | switch (mode) { | |
173 | case '3': | |
174 | sig::Parse(pool, &type->data.signature, entry->value_, &Structor_); | |
175 | break; | |
176 | ||
177 | case '4': { | |
178 | sig::Signature signature; | |
179 | sig::Parse(pool, &signature, entry->value_, &Structor_); | |
180 | type = signature.elements[0].type; | |
181 | } break; | |
182 | } | |
9cad30fa JF |
183 | } |
184 | } | |
185 | ||
186 | JSClassRef Type_privateData::Class_; | |
187 | ||
188 | struct Context : | |
189 | CYData | |
190 | { | |
191 | JSGlobalContextRef context_; | |
192 | ||
193 | Context(JSGlobalContextRef context) : | |
194 | context_(context) | |
195 | { | |
196 | } | |
197 | }; | |
198 | ||
199 | struct Pointer : | |
200 | CYOwned | |
201 | { | |
202 | Type_privateData *type_; | |
203 | size_t length_; | |
204 | ||
205 | Pointer(void *value, JSContextRef context, JSObjectRef owner, size_t length, sig::Type *type) : | |
206 | CYOwned(value, context, owner), | |
b799113b | 207 | type_(new(*pool_) Type_privateData(type)), |
9cad30fa JF |
208 | length_(length) |
209 | { | |
210 | } | |
211 | }; | |
212 | ||
213 | struct Struct_privateData : | |
214 | CYOwned | |
215 | { | |
216 | Type_privateData *type_; | |
217 | ||
218 | Struct_privateData(JSContextRef context, JSObjectRef owner) : | |
219 | CYOwned(NULL, context, owner) | |
220 | { | |
221 | } | |
222 | }; | |
223 | ||
14ec9e00 | 224 | typedef std::map<const char *, Type_privateData *, CYCStringLess> TypeMap; |
9cad30fa JF |
225 | static TypeMap Types_; |
226 | ||
227 | JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef owner) { | |
228 | Struct_privateData *internal(new Struct_privateData(context, owner)); | |
b799113b | 229 | CYPool &pool(*internal->pool_); |
9cad30fa JF |
230 | Type_privateData *typical(new(pool) Type_privateData(type, ffi)); |
231 | internal->type_ = typical; | |
232 | ||
233 | if (owner != NULL) | |
234 | internal->value_ = data; | |
235 | else { | |
236 | size_t size(typical->GetFFI()->size); | |
0cbeddf8 | 237 | void *copy(internal->pool_->malloc<void>(size)); |
9cad30fa JF |
238 | memcpy(copy, data, size); |
239 | internal->value_ = copy; | |
240 | } | |
241 | ||
242 | return JSObjectMake(context, Struct_, internal); | |
243 | } | |
244 | ||
9a98069f JF |
245 | static void *CYCastSymbol(const char *name) { |
246 | return dlsym(RTLD_DEFAULT, name); | |
247 | } | |
248 | ||
9cad30fa JF |
249 | JSValueRef CYCastJSValue(JSContextRef context, bool value) { |
250 | return JSValueMakeBoolean(context, value); | |
251 | } | |
252 | ||
253 | JSValueRef CYCastJSValue(JSContextRef context, double value) { | |
254 | return JSValueMakeNumber(context, value); | |
255 | } | |
256 | ||
257 | #define CYCastJSValue_(Type_) \ | |
258 | JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \ | |
259 | return JSValueMakeNumber(context, static_cast<double>(value)); \ | |
260 | } | |
261 | ||
262 | CYCastJSValue_(int) | |
263 | CYCastJSValue_(unsigned int) | |
264 | CYCastJSValue_(long int) | |
265 | CYCastJSValue_(long unsigned int) | |
266 | CYCastJSValue_(long long int) | |
267 | CYCastJSValue_(long long unsigned int) | |
268 | ||
269 | JSValueRef CYJSUndefined(JSContextRef context) { | |
270 | return JSValueMakeUndefined(context); | |
271 | } | |
272 | ||
273 | double CYCastDouble(JSContextRef context, JSValueRef value) { | |
f1b5a47f | 274 | return _jsccall(JSValueToNumber, context, value); |
9cad30fa JF |
275 | } |
276 | ||
277 | bool CYCastBool(JSContextRef context, JSValueRef value) { | |
278 | return JSValueToBoolean(context, value); | |
279 | } | |
280 | ||
281 | JSValueRef CYJSNull(JSContextRef context) { | |
282 | return JSValueMakeNull(context); | |
283 | } | |
284 | ||
285 | JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value) { | |
286 | return value == NULL ? CYJSNull(context) : JSValueMakeString(context, value); | |
287 | } | |
288 | ||
289 | JSValueRef CYCastJSValue(JSContextRef context, const char *value) { | |
290 | return CYCastJSValue(context, CYJSString(value)); | |
291 | } | |
292 | ||
293 | JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) { | |
f1b5a47f | 294 | return _jsccall(JSValueToObject, context, value); |
9cad30fa JF |
295 | } |
296 | ||
297 | JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, const JSValueRef arguments[]) { | |
f1b5a47f | 298 | return _jsccall(JSObjectCallAsFunction, context, function, _this, count, arguments); |
9cad30fa JF |
299 | } |
300 | ||
301 | bool CYIsCallable(JSContextRef context, JSValueRef value) { | |
302 | return value != NULL && JSValueIsObject(context, value) && JSObjectIsFunction(context, (JSObjectRef) value); | |
303 | } | |
304 | ||
55f12f6a JF |
305 | size_t CYArrayLength(JSContextRef context, JSObjectRef array) { |
306 | return CYCastDouble(context, CYGetProperty(context, array, length_s)); | |
307 | } | |
308 | ||
309 | JSValueRef CYArrayGet(JSContextRef context, JSObjectRef array, size_t index) { | |
f1b5a47f | 310 | return _jsccall(JSObjectGetPropertyAtIndex, context, array, index); |
55f12f6a JF |
311 | } |
312 | ||
313 | void CYArrayPush(JSContextRef context, JSObjectRef array, JSValueRef value) { | |
55f12f6a JF |
314 | JSValueRef arguments[1]; |
315 | arguments[0] = value; | |
316 | JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array_prototype"))); | |
f1b5a47f | 317 | _jsccall(JSObjectCallAsFunction, context, CYCastJSObject(context, CYGetProperty(context, Array, push_s)), array, 1, arguments); |
55f12f6a JF |
318 | } |
319 | ||
9cad30fa JF |
320 | static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
321 | if (count == 0) | |
322 | printf("\n"); | |
323 | else { | |
324 | CYPool pool; | |
325 | printf("%s\n", CYPoolCString(pool, context, arguments[0])); | |
326 | } | |
327 | ||
328 | return CYJSUndefined(context); | |
55c6d6ab | 329 | } CYCatch(NULL) } |
9cad30fa JF |
330 | |
331 | static size_t Nonce_(0); | |
332 | ||
62d94d32 | 333 | static JSValueRef $cyq(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
9cad30fa | 334 | CYPool pool; |
0cbeddf8 | 335 | const char *name(pool.strcat(CYPoolCString(pool, context, arguments[0]), pool.itoa(Nonce_++), NULL)); |
9cad30fa | 336 | return CYCastJSValue(context, name); |
62d94d32 | 337 | } CYCatch(NULL) } |
9cad30fa | 338 | |
62d94d32 | 339 | static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
9cad30fa JF |
340 | JSGarbageCollect(context); |
341 | return CYJSUndefined(context); | |
62d94d32 | 342 | } CYCatch(NULL) } |
9cad30fa | 343 | |
b799113b | 344 | const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value, JSValueRef *exception) { CYTry { |
9cad30fa JF |
345 | switch (JSType type = JSValueGetType(context, value)) { |
346 | case kJSTypeUndefined: | |
347 | return "undefined"; | |
348 | case kJSTypeNull: | |
349 | return "null"; | |
350 | case kJSTypeBoolean: | |
351 | return CYCastBool(context, value) ? "true" : "false"; | |
352 | ||
353 | case kJSTypeNumber: { | |
354 | std::ostringstream str; | |
355 | CYNumerify(str, CYCastDouble(context, value)); | |
356 | std::string value(str.str()); | |
b799113b | 357 | return pool.strmemdup(value.c_str(), value.size()); |
9cad30fa JF |
358 | } break; |
359 | ||
360 | case kJSTypeString: { | |
361 | std::ostringstream str; | |
362 | CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, value))); | |
363 | CYStringify(str, string.data, string.size); | |
364 | std::string value(str.str()); | |
b799113b | 365 | return pool.strmemdup(value.c_str(), value.size()); |
9cad30fa JF |
366 | } break; |
367 | ||
368 | case kJSTypeObject: | |
369 | return CYPoolCCYON(pool, context, (JSObjectRef) value); | |
370 | default: | |
371 | throw CYJSError(context, "JSValueGetType() == 0x%x", type); | |
372 | } | |
55c6d6ab | 373 | } CYCatch(NULL) } |
9cad30fa | 374 | |
b799113b | 375 | const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value) { |
f1b5a47f | 376 | return _jsccall(CYPoolCCYON, pool, context, value); |
9cad30fa JF |
377 | } |
378 | ||
b799113b | 379 | const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object) { |
9cad30fa JF |
380 | JSValueRef toCYON(CYGetProperty(context, object, toCYON_s)); |
381 | if (CYIsCallable(context, toCYON)) { | |
382 | JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toCYON, object, 0, NULL)); | |
383 | _assert(value != NULL); | |
384 | return CYPoolCString(pool, context, value); | |
385 | } | |
386 | ||
387 | JSValueRef toJSON(CYGetProperty(context, object, toJSON_s)); | |
388 | if (CYIsCallable(context, toJSON)) { | |
389 | JSValueRef arguments[1] = {CYCastJSValue(context, CYJSString(""))}; | |
f1b5a47f | 390 | return _jsccall(CYPoolCCYON, pool, context, CYCallAsFunction(context, (JSObjectRef) toJSON, object, 1, arguments)); |
9cad30fa JF |
391 | } |
392 | ||
005b2e9f JF |
393 | if (JSObjectIsFunction(context, object)) { |
394 | JSValueRef toString(CYGetProperty(context, object, toString_s)); | |
395 | if (CYIsCallable(context, toString)) { | |
396 | JSValueRef arguments[1] = {CYCastJSValue(context, CYJSString(""))}; | |
397 | JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toString, object, 1, arguments)); | |
398 | _assert(value != NULL); | |
399 | return CYPoolCString(pool, context, value); | |
400 | } | |
401 | } | |
402 | ||
9cad30fa JF |
403 | std::ostringstream str; |
404 | ||
405 | str << '{'; | |
406 | ||
407 | // XXX: this is, sadly, going to leak | |
408 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context, object)); | |
409 | ||
410 | bool comma(false); | |
411 | ||
412 | for (size_t index(0), count(JSPropertyNameArrayGetCount(names)); index != count; ++index) { | |
9cad30fa JF |
413 | if (comma) |
414 | str << ','; | |
415 | else | |
416 | comma = true; | |
417 | ||
dc5d7cf4 | 418 | JSStringRef name(JSPropertyNameArrayGetNameAtIndex(names, index)); |
9cad30fa | 419 | CYUTF8String string(CYPoolUTF8String(pool, context, name)); |
dc5d7cf4 | 420 | |
9cad30fa JF |
421 | if (CYIsKey(string)) |
422 | str << string.data; | |
423 | else | |
424 | CYStringify(str, string.data, string.size); | |
425 | ||
dc5d7cf4 | 426 | str << ':'; |
9cad30fa | 427 | |
dc5d7cf4 JF |
428 | try { |
429 | JSValueRef value(CYGetProperty(context, object, name)); | |
430 | str << CYPoolCCYON(pool, context, value); | |
431 | } catch (const CYException &error) { | |
432 | str << "@error"; | |
433 | } | |
434 | } | |
9cad30fa JF |
435 | |
436 | JSPropertyNameArrayRelease(names); | |
437 | ||
dc5d7cf4 JF |
438 | str << '}'; |
439 | ||
9cad30fa | 440 | std::string string(str.str()); |
b799113b | 441 | return pool.strmemdup(string.c_str(), string.size()); |
9cad30fa JF |
442 | } |
443 | ||
444 | static JSValueRef Array_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
445 | CYPool pool; | |
446 | std::ostringstream str; | |
447 | ||
448 | str << '['; | |
449 | ||
450 | JSValueRef length(CYGetProperty(context, _this, length_s)); | |
451 | bool comma(false); | |
452 | ||
453 | for (size_t index(0), count(CYCastDouble(context, length)); index != count; ++index) { | |
454 | JSValueRef value(CYGetProperty(context, _this, index)); | |
455 | ||
456 | if (comma) | |
457 | str << ','; | |
458 | else | |
459 | comma = true; | |
460 | ||
461 | if (!JSValueIsUndefined(context, value)) | |
462 | str << CYPoolCCYON(pool, context, value); | |
463 | else { | |
464 | str << ','; | |
465 | comma = false; | |
466 | } | |
467 | } | |
468 | ||
469 | str << ']'; | |
470 | ||
471 | std::string value(str.str()); | |
472 | return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size()))); | |
55c6d6ab | 473 | } CYCatch(NULL) } |
9cad30fa | 474 | |
4cb8aa43 JF |
475 | static JSValueRef String_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
476 | CYPool pool; | |
477 | std::ostringstream str; | |
478 | ||
479 | CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, _this))); | |
480 | CYStringify(str, string.data, string.size); | |
481 | ||
482 | std::string value(str.str()); | |
483 | return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size()))); | |
55c6d6ab | 484 | } CYCatch(NULL) } |
4cb8aa43 | 485 | |
9cad30fa JF |
486 | JSObjectRef CYMakePointer(JSContextRef context, void *pointer, size_t length, sig::Type *type, ffi_type *ffi, JSObjectRef owner) { |
487 | Pointer *internal(new Pointer(pointer, context, owner, length, type)); | |
488 | return JSObjectMake(context, Pointer_, internal); | |
489 | } | |
490 | ||
9a98069f JF |
491 | static JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) { |
492 | return JSObjectMake(context, Functor_, new cy::Functor(type, function)); | |
493 | } | |
1850a470 | 494 | |
9a98069f JF |
495 | static JSObjectRef CYMakeFunctor(JSContextRef context, const char *symbol, const char *type, void **cache) { |
496 | cy::Functor *internal; | |
497 | if (*cache != NULL) | |
1850a470 | 498 | internal = reinterpret_cast<cy::Functor *>(*cache); |
9a98069f JF |
499 | else { |
500 | void (*function)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol))); | |
501 | if (function == NULL) | |
502 | return NULL; | |
1850a470 | 503 | |
9a98069f JF |
504 | internal = new cy::Functor(type, function); |
505 | *cache = internal; | |
1850a470 JF |
506 | } |
507 | ||
9a98069f | 508 | ++internal->count_; |
9cad30fa JF |
509 | return JSObjectMake(context, Functor_, internal); |
510 | } | |
511 | ||
b799113b | 512 | static bool CYGetOffset(CYPool &pool, JSContextRef context, JSStringRef value, ssize_t &index) { |
9cad30fa JF |
513 | return CYGetOffset(CYPoolCString(pool, context, value), index); |
514 | } | |
515 | ||
516 | void *CYCastPointer_(JSContextRef context, JSValueRef value) { | |
517 | switch (JSValueGetType(context, value)) { | |
518 | case kJSTypeNull: | |
519 | return NULL; | |
20ded97a JF |
520 | case kJSTypeObject: { |
521 | JSObjectRef object((JSObjectRef) value); | |
9cad30fa | 522 | if (JSValueIsObjectOfClass(context, value, Pointer_)) { |
20ded97a | 523 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); |
9cad30fa | 524 | return internal->value_; |
20ded97a JF |
525 | } |
526 | JSValueRef toPointer(CYGetProperty(context, object, toPointer_s)); | |
527 | if (CYIsCallable(context, toPointer)) { | |
528 | JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toPointer, object, 0, NULL)); | |
529 | _assert(value != NULL); | |
530 | return CYCastPointer_(context, value); | |
531 | } | |
532 | } default: | |
9cad30fa JF |
533 | double number(CYCastDouble(context, value)); |
534 | if (std::isnan(number)) | |
535 | throw CYJSError(context, "cannot convert value to pointer"); | |
536 | return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number))); | |
537 | } | |
538 | } | |
539 | ||
b799113b | 540 | void CYPoolFFI(CYPool *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { |
9cad30fa JF |
541 | switch (type->primitive) { |
542 | case sig::boolean_P: | |
543 | *reinterpret_cast<bool *>(data) = JSValueToBoolean(context, value); | |
544 | break; | |
545 | ||
546 | #define CYPoolFFI_(primitive, native) \ | |
547 | case sig::primitive ## _P: \ | |
548 | *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \ | |
549 | break; | |
550 | ||
551 | CYPoolFFI_(uchar, unsigned char) | |
552 | CYPoolFFI_(char, char) | |
553 | CYPoolFFI_(ushort, unsigned short) | |
554 | CYPoolFFI_(short, short) | |
555 | CYPoolFFI_(ulong, unsigned long) | |
556 | CYPoolFFI_(long, long) | |
557 | CYPoolFFI_(uint, unsigned int) | |
558 | CYPoolFFI_(int, int) | |
559 | CYPoolFFI_(ulonglong, unsigned long long) | |
560 | CYPoolFFI_(longlong, long long) | |
561 | CYPoolFFI_(float, float) | |
562 | CYPoolFFI_(double, double) | |
563 | ||
564 | case sig::array_P: { | |
565 | uint8_t *base(reinterpret_cast<uint8_t *>(data)); | |
566 | JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL); | |
567 | for (size_t index(0); index != type->data.data.size; ++index) { | |
568 | ffi_type *field(ffi->elements[index]); | |
569 | ||
570 | JSValueRef rhs; | |
571 | if (aggregate == NULL) | |
572 | rhs = value; | |
573 | else { | |
574 | rhs = CYGetProperty(context, aggregate, index); | |
575 | if (JSValueIsUndefined(context, rhs)) | |
576 | throw CYJSError(context, "unable to extract array value"); | |
577 | } | |
578 | ||
579 | CYPoolFFI(pool, context, type->data.data.type, field, base, rhs); | |
580 | // XXX: alignment? | |
581 | base += field->size; | |
582 | } | |
583 | } break; | |
584 | ||
585 | case sig::pointer_P: | |
586 | *reinterpret_cast<void **>(data) = CYCastPointer<void *>(context, value); | |
587 | break; | |
588 | ||
589 | case sig::string_P: | |
b799113b JF |
590 | _assert(pool != NULL); |
591 | *reinterpret_cast<const char **>(data) = CYPoolCString(*pool, context, value); | |
9cad30fa JF |
592 | break; |
593 | ||
594 | case sig::struct_P: { | |
595 | uint8_t *base(reinterpret_cast<uint8_t *>(data)); | |
596 | JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL); | |
597 | for (size_t index(0); index != type->data.signature.count; ++index) { | |
598 | sig::Element *element(&type->data.signature.elements[index]); | |
599 | ffi_type *field(ffi->elements[index]); | |
600 | ||
601 | JSValueRef rhs; | |
602 | if (aggregate == NULL) | |
603 | rhs = value; | |
604 | else { | |
605 | rhs = CYGetProperty(context, aggregate, index); | |
606 | if (JSValueIsUndefined(context, rhs)) { | |
607 | if (element->name != NULL) | |
608 | rhs = CYGetProperty(context, aggregate, CYJSString(element->name)); | |
609 | else | |
610 | goto undefined; | |
611 | if (JSValueIsUndefined(context, rhs)) undefined: | |
612 | throw CYJSError(context, "unable to extract structure value"); | |
613 | } | |
614 | } | |
615 | ||
616 | CYPoolFFI(pool, context, element->type, field, base, rhs); | |
617 | // XXX: alignment? | |
618 | base += field->size; | |
619 | } | |
620 | } break; | |
621 | ||
622 | case sig::void_P: | |
623 | break; | |
624 | ||
625 | default: | |
626 | if (hooks_ != NULL && hooks_->PoolFFI != NULL) | |
627 | if ((*hooks_->PoolFFI)(pool, context, type, ffi, data, value)) | |
628 | return; | |
629 | ||
630 | CYThrow("unimplemented signature code: '%c''\n", type->primitive); | |
631 | } | |
632 | } | |
633 | ||
634 | JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) { | |
635 | switch (type->primitive) { | |
636 | case sig::boolean_P: | |
637 | return CYCastJSValue(context, *reinterpret_cast<bool *>(data)); | |
638 | ||
639 | #define CYFromFFI_(primitive, native) \ | |
640 | case sig::primitive ## _P: \ | |
641 | return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \ | |
642 | ||
643 | CYFromFFI_(uchar, unsigned char) | |
644 | CYFromFFI_(char, char) | |
645 | CYFromFFI_(ushort, unsigned short) | |
646 | CYFromFFI_(short, short) | |
647 | CYFromFFI_(ulong, unsigned long) | |
648 | CYFromFFI_(long, long) | |
649 | CYFromFFI_(uint, unsigned int) | |
650 | CYFromFFI_(int, int) | |
651 | CYFromFFI_(ulonglong, unsigned long long) | |
652 | CYFromFFI_(longlong, long long) | |
653 | CYFromFFI_(float, float) | |
654 | CYFromFFI_(double, double) | |
655 | ||
656 | case sig::array_P: | |
657 | if (void *pointer = data) | |
658 | return CYMakePointer(context, pointer, type->data.data.size, type->data.data.type, NULL, owner); | |
659 | else goto null; | |
660 | ||
661 | case sig::pointer_P: | |
662 | if (void *pointer = *reinterpret_cast<void **>(data)) | |
663 | return CYMakePointer(context, pointer, _not(size_t), type->data.data.type, NULL, owner); | |
664 | else goto null; | |
665 | ||
666 | case sig::string_P: | |
667 | if (char *utf8 = *reinterpret_cast<char **>(data)) | |
668 | return CYCastJSValue(context, utf8); | |
669 | else goto null; | |
670 | ||
671 | case sig::struct_P: | |
672 | return CYMakeStruct(context, data, type, ffi, owner); | |
673 | case sig::void_P: | |
674 | return CYJSUndefined(context); | |
675 | ||
676 | null: | |
677 | return CYJSNull(context); | |
678 | default: | |
679 | if (hooks_ != NULL && hooks_->FromFFI != NULL) | |
680 | if (JSValueRef value = (*hooks_->FromFFI)(context, type, ffi, data, initialize, owner)) | |
681 | return value; | |
682 | ||
683 | CYThrow("unimplemented signature code: '%c''\n", type->primitive); | |
684 | } | |
685 | } | |
686 | ||
9ec7dd18 | 687 | void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)) { |
9cad30fa JF |
688 | Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg)); |
689 | ||
690 | JSContextRef context(internal->context_); | |
691 | ||
692 | size_t count(internal->cif_.nargs); | |
693 | JSValueRef values[count]; | |
694 | ||
695 | for (size_t index(0); index != count; ++index) | |
696 | values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]); | |
697 | ||
9ec7dd18 | 698 | JSValueRef value(adapter(context, count, values, internal->function_)); |
9cad30fa JF |
699 | CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value); |
700 | } | |
701 | ||
9ec7dd18 JF |
702 | static JSValueRef FunctionAdapter_(JSContextRef context, size_t count, JSValueRef values[], JSObjectRef function) { |
703 | return CYCallAsFunction(context, function, NULL, count, values); | |
704 | } | |
705 | ||
706 | static void FunctionClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) { | |
707 | CYExecuteClosure(cif, result, arguments, arg, &FunctionAdapter_); | |
708 | } | |
709 | ||
9cad30fa JF |
710 | Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const char *type, void (*callback)(ffi_cif *, void *, void **, void *)) { |
711 | // XXX: in case of exceptions this will leak | |
712 | // XXX: in point of fact, this may /need/ to leak :( | |
713 | Closure_privateData *internal(new Closure_privateData(context, function, type)); | |
714 | ||
c5bce670 JF |
715 | #if defined(__APPLE__) && defined(__arm__) |
716 | void *executable; | |
717 | ffi_closure *writable(reinterpret_cast<ffi_closure *>(ffi_closure_alloc(sizeof(ffi_closure), &executable))); | |
718 | ||
719 | ffi_status status(ffi_prep_closure_loc(writable, &internal->cif_, callback, internal, executable)); | |
720 | _assert(status == FFI_OK); | |
721 | ||
722 | internal->value_ = executable; | |
723 | #else | |
9cad30fa JF |
724 | ffi_closure *closure((ffi_closure *) _syscall(mmap( |
725 | NULL, sizeof(ffi_closure), | |
726 | PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, | |
727 | -1, 0 | |
728 | ))); | |
729 | ||
730 | ffi_status status(ffi_prep_closure(closure, &internal->cif_, callback, internal)); | |
731 | _assert(status == FFI_OK); | |
732 | ||
733 | _syscall(mprotect(closure, sizeof(*closure), PROT_READ | PROT_EXEC)); | |
734 | ||
735 | internal->value_ = closure; | |
c5bce670 | 736 | #endif |
9cad30fa JF |
737 | |
738 | return internal; | |
739 | } | |
740 | ||
741 | static JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) { | |
742 | Closure_privateData *internal(CYMakeFunctor_(context, function, type, &FunctionClosure_)); | |
743 | JSObjectRef object(JSObjectMake(context, Functor_, internal)); | |
744 | // XXX: see above notes about needing to leak | |
745 | JSValueProtect(CYGetJSContext(context), object); | |
746 | return object; | |
747 | } | |
748 | ||
749 | JSObjectRef CYGetCachedObject(JSContextRef context, JSStringRef name) { | |
750 | return CYCastJSObject(context, CYGetProperty(context, CYCastJSObject(context, CYGetProperty(context, CYGetGlobalObject(context), cy_s)), name)); | |
751 | } | |
752 | ||
753 | static JSObjectRef CYMakeFunctor(JSContextRef context, JSValueRef value, const char *type) { | |
754 | JSObjectRef Function(CYGetCachedObject(context, CYJSString("Function"))); | |
755 | ||
f1b5a47f | 756 | bool function(_jsccall(JSValueIsInstanceOfConstructor, context, value, Function)); |
9cad30fa JF |
757 | if (function) { |
758 | JSObjectRef function(CYCastJSObject(context, value)); | |
759 | return CYMakeFunctor(context, function, type); | |
760 | } else { | |
761 | void (*function)()(CYCastPointer<void (*)()>(context, value)); | |
762 | return CYMakeFunctor(context, function, type); | |
763 | } | |
764 | } | |
765 | ||
b799113b | 766 | static bool Index_(CYPool &pool, JSContextRef context, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) { |
9cad30fa JF |
767 | Type_privateData *typical(internal->type_); |
768 | sig::Type *type(typical->type_); | |
769 | if (type == NULL) | |
770 | return false; | |
771 | ||
772 | const char *name(CYPoolCString(pool, context, property)); | |
773 | size_t length(strlen(name)); | |
774 | double number(CYCastDouble(name, length)); | |
775 | ||
776 | size_t count(type->data.signature.count); | |
777 | ||
778 | if (std::isnan(number)) { | |
779 | if (property == NULL) | |
780 | return false; | |
781 | ||
782 | sig::Element *elements(type->data.signature.elements); | |
783 | ||
784 | for (size_t local(0); local != count; ++local) { | |
785 | sig::Element *element(&elements[local]); | |
786 | if (element->name != NULL && strcmp(name, element->name) == 0) { | |
787 | index = local; | |
788 | goto base; | |
789 | } | |
790 | } | |
791 | ||
792 | return false; | |
793 | } else { | |
794 | index = static_cast<ssize_t>(number); | |
795 | if (index != number || index < 0 || static_cast<size_t>(index) >= count) | |
796 | return false; | |
797 | } | |
798 | ||
799 | base: | |
800 | ffi_type **elements(typical->GetFFI()->elements); | |
801 | ||
802 | base = reinterpret_cast<uint8_t *>(internal->value_); | |
803 | for (ssize_t local(0); local != index; ++local) | |
804 | base += elements[local]->size; | |
805 | ||
806 | return true; | |
807 | } | |
808 | ||
809 | static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
810 | CYPool pool; | |
811 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
812 | ||
813 | if (JSStringIsEqual(property, length_s)) | |
814 | return internal->length_ == _not(size_t) ? CYJSUndefined(context) : CYCastJSValue(context, internal->length_); | |
815 | ||
816 | Type_privateData *typical(internal->type_); | |
817 | ||
818 | if (typical->type_ == NULL) | |
819 | return NULL; | |
820 | ||
821 | ssize_t offset; | |
822 | if (JSStringIsEqualToUTF8CString(property, "$cyi")) | |
823 | offset = 0; | |
824 | else if (!CYGetOffset(pool, context, property, offset)) | |
825 | return NULL; | |
826 | ||
827 | ffi_type *ffi(typical->GetFFI()); | |
828 | ||
829 | uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_)); | |
830 | base += ffi->size * offset; | |
831 | ||
832 | JSObjectRef owner(internal->GetOwner() ?: object); | |
833 | return CYFromFFI(context, typical->type_, ffi, base, false, owner); | |
55c6d6ab | 834 | } CYCatch(NULL) } |
9cad30fa JF |
835 | |
836 | static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { | |
837 | CYPool pool; | |
838 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
839 | Type_privateData *typical(internal->type_); | |
840 | ||
841 | if (typical->type_ == NULL) | |
55c6d6ab | 842 | return false; |
9cad30fa JF |
843 | |
844 | ssize_t offset; | |
845 | if (JSStringIsEqualToUTF8CString(property, "$cyi")) | |
846 | offset = 0; | |
847 | else if (!CYGetOffset(pool, context, property, offset)) | |
55c6d6ab | 848 | return false; |
9cad30fa JF |
849 | |
850 | ffi_type *ffi(typical->GetFFI()); | |
851 | ||
852 | uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_)); | |
853 | base += ffi->size * offset; | |
854 | ||
855 | CYPoolFFI(NULL, context, typical->type_, ffi, base, value); | |
856 | return true; | |
55c6d6ab | 857 | } CYCatch(false) } |
9cad30fa | 858 | |
62d94d32 | 859 | static JSValueRef Struct_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
9cad30fa JF |
860 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(_this))); |
861 | Type_privateData *typical(internal->type_); | |
862 | return CYMakePointer(context, internal->value_, _not(size_t), typical->type_, typical->ffi_, _this); | |
62d94d32 | 863 | } CYCatch(NULL) } |
9cad30fa JF |
864 | |
865 | static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
866 | CYPool pool; | |
867 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
868 | Type_privateData *typical(internal->type_); | |
869 | ||
870 | ssize_t index; | |
871 | uint8_t *base; | |
872 | ||
873 | if (!Index_(pool, context, internal, property, index, base)) | |
874 | return NULL; | |
875 | ||
876 | JSObjectRef owner(internal->GetOwner() ?: object); | |
877 | ||
878 | return CYFromFFI(context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, false, owner); | |
55c6d6ab | 879 | } CYCatch(NULL) } |
9cad30fa JF |
880 | |
881 | static bool Struct_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { | |
882 | CYPool pool; | |
883 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
884 | Type_privateData *typical(internal->type_); | |
885 | ||
886 | ssize_t index; | |
887 | uint8_t *base; | |
888 | ||
889 | if (!Index_(pool, context, internal, property, index, base)) | |
890 | return false; | |
891 | ||
892 | CYPoolFFI(NULL, context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, value); | |
893 | return true; | |
55c6d6ab | 894 | } CYCatch(false) } |
9cad30fa JF |
895 | |
896 | static void Struct_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
897 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
898 | Type_privateData *typical(internal->type_); | |
899 | sig::Type *type(typical->type_); | |
900 | ||
901 | if (type == NULL) | |
902 | return; | |
903 | ||
904 | size_t count(type->data.signature.count); | |
905 | sig::Element *elements(type->data.signature.elements); | |
906 | ||
907 | char number[32]; | |
908 | ||
909 | for (size_t index(0); index != count; ++index) { | |
910 | const char *name; | |
911 | name = elements[index].name; | |
912 | ||
913 | if (name == NULL) { | |
914 | sprintf(number, "%zu", index); | |
915 | name = number; | |
916 | } | |
917 | ||
918 | JSPropertyNameAccumulatorAddName(names, CYJSString(name)); | |
919 | } | |
920 | } | |
921 | ||
9d512587 | 922 | JSValueRef CYCallFunction(CYPool &pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, sig::Signature *signature, ffi_cif *cif, void (*function)()) { |
9cad30fa JF |
923 | if (setups + count != signature->count - 1) |
924 | throw CYJSError(context, "incorrect number of arguments to ffi function"); | |
925 | ||
926 | size_t size(setups + count); | |
927 | void *values[size]; | |
928 | memcpy(values, setup, sizeof(void *) * setups); | |
929 | ||
930 | for (size_t index(setups); index != size; ++index) { | |
931 | sig::Element *element(&signature->elements[index + 1]); | |
932 | ffi_type *ffi(cif->arg_types[index]); | |
933 | // XXX: alignment? | |
934 | values[index] = new(pool) uint8_t[ffi->size]; | |
b799113b | 935 | CYPoolFFI(&pool, context, element->type, ffi, values[index], arguments[index - setups]); |
9cad30fa JF |
936 | } |
937 | ||
938 | uint8_t value[cif->rtype->size]; | |
939 | ||
940 | if (hooks_ != NULL && hooks_->CallFunction != NULL) | |
941 | (*hooks_->CallFunction)(context, cif, function, value, values); | |
942 | else | |
943 | ffi_call(cif, function, value, values); | |
944 | ||
945 | return CYFromFFI(context, signature->elements[0].type, cif->rtype, value, initialize); | |
9d512587 | 946 | } |
9cad30fa | 947 | |
62d94d32 | 948 | static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
9cad30fa JF |
949 | CYPool pool; |
950 | cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(object))); | |
9d512587 | 951 | return CYCallFunction(pool, context, 0, NULL, count, arguments, false, &internal->signature_, &internal->cif_, internal->GetValue()); |
62d94d32 | 952 | } CYCatch(NULL) } |
9cad30fa | 953 | |
8a23fb2e | 954 | JSObjectRef CYMakeType(JSContextRef context, const char *type) { |
9cad30fa JF |
955 | Type_privateData *internal(new Type_privateData(type)); |
956 | return JSObjectMake(context, Type_privateData::Class_, internal); | |
957 | } | |
958 | ||
8a23fb2e | 959 | JSObjectRef CYMakeType(JSContextRef context, sig::Type *type) { |
9cad30fa JF |
960 | Type_privateData *internal(new Type_privateData(type)); |
961 | return JSObjectMake(context, Type_privateData::Class_, internal); | |
962 | } | |
963 | ||
9cad30fa JF |
964 | static JSValueRef All_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { |
965 | JSObjectRef global(CYGetGlobalObject(context)); | |
966 | JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript")))); | |
26ef7a82 JF |
967 | JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls")))); |
968 | ||
969 | for (size_t i(0), count(CYArrayLength(context, alls)); i != count; ++i) | |
970 | if (JSObjectRef space = CYCastJSObject(context, CYArrayGet(context, alls, count - i - 1))) | |
971 | if (JSValueRef value = CYGetProperty(context, space, property)) | |
972 | if (!JSValueIsUndefined(context, value)) | |
973 | return value; | |
9cad30fa JF |
974 | |
975 | CYPool pool; | |
976 | CYUTF8String name(CYPoolUTF8String(pool, context, property)); | |
977 | ||
2f51d6ab JF |
978 | size_t length(name.size); |
979 | char keyed[length + 2]; | |
980 | memcpy(keyed + 1, name.data, length + 1); | |
981 | ||
982 | static const char *modes = "0124"; | |
983 | for (size_t i(0); i != 4; ++i) { | |
984 | char mode(modes[i]); | |
985 | keyed[0] = mode; | |
986 | ||
987 | if (CYBridgeEntry *entry = CYBridgeHash(keyed, length + 1)) | |
988 | switch (mode) { | |
989 | case '0': | |
990 | return JSEvaluateScript(CYGetJSContext(context), CYJSString(entry->value_), NULL, NULL, 0, NULL); | |
991 | ||
992 | case '1': | |
9a98069f | 993 | return CYMakeFunctor(context, name.data, entry->value_, &entry->cache_); |
2f51d6ab JF |
994 | |
995 | case '2': | |
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_); | |
1000 | ffi_cif cif; | |
1001 | sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); | |
1002 | return CYFromFFI(context, signature.elements[0].type, cif.rtype, symbol); | |
1003 | } else return NULL; | |
1004 | ||
1005 | // XXX: implement case 3 | |
1006 | case '4': | |
1007 | return CYMakeType(context, entry->value_); | |
1008 | } | |
9cad30fa JF |
1009 | } |
1010 | ||
2f51d6ab | 1011 | return NULL; |
55c6d6ab | 1012 | } CYCatch(NULL) } |
9cad30fa | 1013 | |
26ef7a82 JF |
1014 | static void All_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { |
1015 | JSObjectRef global(CYGetGlobalObject(context)); | |
1016 | JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript")))); | |
1017 | JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls")))); | |
1018 | ||
1019 | for (size_t i(0), count(CYArrayLength(context, alls)); i != count; ++i) | |
1020 | if (JSObjectRef space = CYCastJSObject(context, CYArrayGet(context, alls, count - i - 1))) { | |
1021 | JSPropertyNameArrayRef subset(JSObjectCopyPropertyNames(context, space)); | |
1022 | for (size_t index(0), count(JSPropertyNameArrayGetCount(subset)); index != count; ++index) | |
1023 | JSPropertyNameAccumulatorAddName(names, JSPropertyNameArrayGetNameAtIndex(subset, index)); | |
1024 | JSPropertyNameArrayRelease(subset); | |
1025 | } | |
1026 | } | |
1027 | ||
9cad30fa JF |
1028 | static JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
1029 | if (count != 2) | |
5d422750 | 1030 | throw CYJSError(context, "incorrect number of arguments to Pointer constructor"); |
9cad30fa JF |
1031 | |
1032 | CYPool pool; | |
1033 | ||
1034 | void *value(CYCastPointer<void *>(context, arguments[0])); | |
1035 | const char *type(CYPoolCString(pool, context, arguments[1])); | |
1036 | ||
1037 | sig::Signature signature; | |
1038 | sig::Parse(pool, &signature, type, &Structor_); | |
1039 | ||
1040 | return CYMakePointer(context, value, _not(size_t), signature.elements[0].type, NULL, NULL); | |
55c6d6ab | 1041 | } CYCatch(NULL) } |
9cad30fa JF |
1042 | |
1043 | static JSObjectRef Type_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1044 | if (count != 1) | |
1045 | throw CYJSError(context, "incorrect number of arguments to Type constructor"); | |
1046 | CYPool pool; | |
1047 | const char *type(CYPoolCString(pool, context, arguments[0])); | |
1048 | return CYMakeType(context, type); | |
55c6d6ab | 1049 | } CYCatch(NULL) } |
9cad30fa | 1050 | |
85e90410 JF |
1051 | static JSValueRef Type_callAsFunction_arrayOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
1052 | if (count != 1) | |
1053 | throw CYJSError(context, "incorrect number of arguments to Type.arrayOf"); | |
1054 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1055 | ||
1056 | CYPool pool; | |
1057 | size_t index(CYGetIndex(pool, context, CYJSString(context, arguments[0]))); | |
1058 | if (index == _not(size_t)) | |
1059 | throw CYJSError(context, "invalid array size used with Type.arrayOf"); | |
9cad30fa JF |
1060 | |
1061 | sig::Type type; | |
85e90410 JF |
1062 | type.name = NULL; |
1063 | type.flags = 0; | |
9cad30fa | 1064 | |
85e90410 JF |
1065 | type.primitive = sig::array_P; |
1066 | type.data.data.type = internal->type_; | |
1067 | type.data.data.size = index; | |
1068 | ||
1069 | return CYMakeType(context, &type); | |
55c6d6ab | 1070 | } CYCatch(NULL) } |
9cad30fa | 1071 | |
fbc17268 JF |
1072 | static JSValueRef Type_callAsFunction_constant(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
1073 | if (count != 0) | |
1074 | throw CYJSError(context, "incorrect number of arguments to Type.constant"); | |
1075 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1076 | ||
1077 | sig::Type type(*internal->type_); | |
1078 | type.flags |= JOC_TYPE_CONST; | |
1079 | return CYMakeType(context, &type); | |
55c6d6ab | 1080 | } CYCatch(NULL) } |
fbc17268 | 1081 | |
85e90410 JF |
1082 | static JSValueRef Type_callAsFunction_pointerTo(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
1083 | if (count != 0) | |
1084 | throw CYJSError(context, "incorrect number of arguments to Type.pointerTo"); | |
1085 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1086 | ||
1087 | sig::Type type; | |
9cad30fa JF |
1088 | type.name = NULL; |
1089 | type.flags = 0; | |
1090 | ||
85e90410 | 1091 | type.primitive = sig::pointer_P; |
9cad30fa | 1092 | type.data.data.type = internal->type_; |
85e90410 | 1093 | type.data.data.size = 0; |
9cad30fa JF |
1094 | |
1095 | return CYMakeType(context, &type); | |
55c6d6ab | 1096 | } CYCatch(NULL) } |
9cad30fa | 1097 | |
21d5f610 JF |
1098 | static JSValueRef Type_callAsFunction_withName(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
1099 | if (count != 1) | |
1100 | throw CYJSError(context, "incorrect number of arguments to Type.withName"); | |
1101 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1102 | ||
1103 | CYPool pool; | |
1104 | const char *name(CYPoolCString(pool, context, arguments[0])); | |
1105 | ||
1106 | sig::Type type(*internal->type_); | |
1107 | type.name = name; | |
1108 | return CYMakeType(context, &type); | |
55c6d6ab | 1109 | } CYCatch(NULL) } |
21d5f610 | 1110 | |
9cad30fa | 1111 | static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
9cad30fa JF |
1112 | if (count != 1) |
1113 | throw CYJSError(context, "incorrect number of arguments to type cast function"); | |
1c3dd2c3 JF |
1114 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); |
1115 | ||
9cad30fa JF |
1116 | sig::Type *type(internal->type_); |
1117 | ffi_type *ffi(internal->GetFFI()); | |
1118 | // XXX: alignment? | |
1119 | uint8_t value[ffi->size]; | |
1120 | CYPool pool; | |
b799113b | 1121 | CYPoolFFI(&pool, context, type, ffi, value, arguments[0]); |
9cad30fa | 1122 | return CYFromFFI(context, type, ffi, value); |
55c6d6ab | 1123 | } CYCatch(NULL) } |
9cad30fa JF |
1124 | |
1125 | static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1126 | if (count != 0) | |
767e8255 | 1127 | throw CYJSError(context, "incorrect number of arguments to Type allocator"); |
9cad30fa JF |
1128 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); |
1129 | ||
1130 | sig::Type *type(internal->type_); | |
1131 | size_t length; | |
1132 | ||
1133 | if (type->primitive != sig::array_P) | |
1134 | length = _not(size_t); | |
1135 | else { | |
1136 | length = type->data.data.size; | |
1137 | type = type->data.data.type; | |
1138 | } | |
1139 | ||
1140 | void *value(malloc(internal->GetFFI()->size)); | |
1141 | return CYMakePointer(context, value, length, type, NULL, NULL); | |
55c6d6ab | 1142 | } CYCatch(NULL) } |
9cad30fa JF |
1143 | |
1144 | static JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1145 | if (count != 2) | |
1146 | throw CYJSError(context, "incorrect number of arguments to Functor constructor"); | |
1147 | CYPool pool; | |
1148 | const char *type(CYPoolCString(pool, context, arguments[1])); | |
1149 | return CYMakeFunctor(context, arguments[0], type); | |
55c6d6ab | 1150 | } CYCatch(NULL) } |
9cad30fa JF |
1151 | |
1152 | static JSValueRef CYValue_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1153 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this))); | |
1154 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_)); | |
55c6d6ab | 1155 | } CYCatch(NULL) } |
9cad30fa JF |
1156 | |
1157 | static JSValueRef CYValue_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
1158 | return CYValue_callAsFunction_valueOf(context, object, _this, count, arguments, exception); | |
1159 | } | |
1160 | ||
1161 | static JSValueRef CYValue_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1162 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this))); | |
1163 | char string[32]; | |
1164 | sprintf(string, "%p", internal->value_); | |
1165 | return CYCastJSValue(context, string); | |
55c6d6ab | 1166 | } CYCatch(NULL) } |
9cad30fa JF |
1167 | |
1168 | static JSValueRef Pointer_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1169 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(_this))); | |
1170 | if (internal->length_ != _not(size_t)) { | |
3b1534c0 | 1171 | JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array_prototype"))); |
9cad30fa JF |
1172 | JSObjectRef toCYON(CYCastJSObject(context, CYGetProperty(context, Array, toCYON_s))); |
1173 | return CYCallAsFunction(context, toCYON, _this, count, arguments); | |
1174 | } else { | |
1175 | char string[32]; | |
1176 | sprintf(string, "%p", internal->value_); | |
1177 | return CYCastJSValue(context, string); | |
1178 | } | |
55c6d6ab | 1179 | } CYCatch(NULL) } |
9cad30fa | 1180 | |
62d94d32 | 1181 | static JSValueRef Functor_getProperty_type(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { |
7d894647 JF |
1182 | cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(object))); |
1183 | CYPool pool; | |
1184 | return CYCastJSValue(context, Unparse(pool, &internal->signature_)); | |
62d94d32 | 1185 | } CYCatch(NULL) } |
7d894647 | 1186 | |
62d94d32 | 1187 | static JSValueRef Type_getProperty_alignment(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { |
74e8c566 JF |
1188 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); |
1189 | return CYCastJSValue(context, internal->GetFFI()->alignment); | |
62d94d32 | 1190 | } CYCatch(NULL) } |
74e8c566 | 1191 | |
62d94d32 | 1192 | static JSValueRef Type_getProperty_name(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { |
21d5f610 JF |
1193 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); |
1194 | return CYCastJSValue(context, internal->type_->name); | |
62d94d32 | 1195 | } CYCatch(NULL) } |
21d5f610 | 1196 | |
62d94d32 | 1197 | static JSValueRef Type_getProperty_size(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { |
74e8c566 JF |
1198 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); |
1199 | return CYCastJSValue(context, internal->GetFFI()->size); | |
62d94d32 | 1200 | } CYCatch(NULL) } |
74e8c566 | 1201 | |
9cad30fa JF |
1202 | static JSValueRef Type_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
1203 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1204 | CYPool pool; | |
1205 | const char *type(sig::Unparse(pool, internal->type_)); | |
1206 | return CYCastJSValue(context, CYJSString(type)); | |
55c6d6ab | 1207 | } CYCatch(NULL) } |
9cad30fa JF |
1208 | |
1209 | static JSValueRef Type_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1210 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1211 | CYPool pool; | |
1212 | const char *type(sig::Unparse(pool, internal->type_)); | |
4a8523e5 JF |
1213 | std::ostringstream str; |
1214 | CYStringify(str, type, strlen(type)); | |
0cbeddf8 | 1215 | char *cyon(pool.strcat("new Type(", str.str().c_str(), ")", NULL)); |
9cad30fa | 1216 | return CYCastJSValue(context, CYJSString(cyon)); |
55c6d6ab | 1217 | } CYCatch(NULL) } |
9cad30fa JF |
1218 | |
1219 | static JSValueRef Type_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
1220 | return Type_callAsFunction_toString(context, object, _this, count, arguments, exception); | |
1221 | } | |
1222 | ||
1223 | static JSStaticFunction Pointer_staticFunctions[4] = { | |
1224 | {"toCYON", &Pointer_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1225 | {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1226 | {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1227 | {NULL, NULL, 0} | |
1228 | }; | |
1229 | ||
1230 | static JSStaticFunction Struct_staticFunctions[2] = { | |
1231 | {"$cya", &Struct_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1232 | {NULL, NULL, 0} | |
1233 | }; | |
1234 | ||
1235 | static JSStaticFunction Functor_staticFunctions[4] = { | |
1236 | {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1237 | {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1238 | {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1239 | {NULL, NULL, 0} | |
1240 | }; | |
1241 | ||
1242 | namespace cy { | |
1243 | JSStaticFunction const * const Functor::StaticFunctions = Functor_staticFunctions; | |
1244 | } | |
1245 | ||
7d894647 JF |
1246 | static JSStaticValue Functor_staticValues[2] = { |
1247 | {"type", &Functor_getProperty_type, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1248 | {NULL, NULL, NULL, 0} | |
1249 | }; | |
1250 | ||
21d5f610 | 1251 | static JSStaticValue Type_staticValues[4] = { |
74e8c566 | 1252 | {"alignment", &Type_getProperty_alignment, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
21d5f610 | 1253 | {"name", &Type_getProperty_name, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
74e8c566 JF |
1254 | {"size", &Type_getProperty_size, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
1255 | {NULL, NULL, NULL, 0} | |
1256 | }; | |
1257 | ||
21d5f610 | 1258 | static JSStaticFunction Type_staticFunctions[8] = { |
85e90410 | 1259 | {"arrayOf", &Type_callAsFunction_arrayOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
fbc17268 | 1260 | {"constant", &Type_callAsFunction_constant, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
85e90410 | 1261 | {"pointerTo", &Type_callAsFunction_pointerTo, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
21d5f610 | 1262 | {"withName", &Type_callAsFunction_withName, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
9cad30fa JF |
1263 | {"toCYON", &Type_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
1264 | {"toJSON", &Type_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1265 | {"toString", &Type_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1266 | {NULL, NULL, 0} | |
1267 | }; | |
1268 | ||
1269 | static JSObjectRef (*JSObjectMakeArray$)(JSContextRef, size_t, const JSValueRef[], JSValueRef *); | |
1270 | ||
1271 | void CYSetArgs(int argc, const char *argv[]) { | |
1272 | JSContextRef context(CYGetJSContext()); | |
1273 | JSValueRef args[argc]; | |
1274 | for (int i(0); i != argc; ++i) | |
1275 | args[i] = CYCastJSValue(context, argv[i]); | |
1276 | ||
1277 | JSObjectRef array; | |
f1b5a47f JF |
1278 | if (JSObjectMakeArray$ != NULL) |
1279 | array = _jsccall(*JSObjectMakeArray$, context, argc, args); | |
1280 | else { | |
9cad30fa JF |
1281 | JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array"))); |
1282 | JSValueRef value(CYCallAsFunction(context, Array, NULL, argc, args)); | |
1283 | array = CYCastJSObject(context, value); | |
1284 | } | |
1285 | ||
1286 | JSObjectRef System(CYGetCachedObject(context, CYJSString("System"))); | |
1287 | CYSetProperty(context, System, CYJSString("args"), array); | |
1288 | } | |
1289 | ||
1290 | JSObjectRef CYGetGlobalObject(JSContextRef context) { | |
1291 | return JSContextGetGlobalObject(context); | |
1292 | } | |
1293 | ||
b799113b | 1294 | const char *CYExecute(CYPool &pool, CYUTF8String code) { |
9cad30fa | 1295 | JSContextRef context(CYGetJSContext()); |
4ad7388f | 1296 | JSValueRef exception(NULL); |
9cad30fa JF |
1297 | |
1298 | void *handle; | |
1299 | if (hooks_ != NULL && hooks_->ExecuteStart != NULL) | |
1300 | handle = (*hooks_->ExecuteStart)(context); | |
1301 | else | |
1302 | handle = NULL; | |
1303 | ||
4ad7388f | 1304 | try { |
9cad30fa | 1305 | |
4ad7388f | 1306 | JSValueRef result; |
9cad30fa JF |
1307 | try { |
1308 | result = JSEvaluateScript(context, CYJSString(code), NULL, NULL, 0, &exception); | |
1309 | } catch (const char *error) { | |
1310 | return error; | |
1311 | } | |
1312 | ||
9cc84a5a JF |
1313 | if (exception != NULL) error: |
1314 | return CYPoolCString(pool, context, CYJSString(context, exception)); | |
9cad30fa JF |
1315 | |
1316 | if (JSValueIsUndefined(context, result)) | |
1317 | return NULL; | |
1318 | ||
4ad7388f | 1319 | const char *json; |
9cad30fa JF |
1320 | try { |
1321 | json = CYPoolCCYON(pool, context, result, &exception); | |
1322 | } catch (const char *error) { | |
1323 | return error; | |
1324 | } | |
1325 | ||
1326 | if (exception != NULL) | |
1327 | goto error; | |
1328 | ||
1329 | CYSetProperty(context, CYGetGlobalObject(context), Result_, result); | |
1330 | ||
9cad30fa | 1331 | return json; |
4ad7388f JF |
1332 | |
1333 | } catch (...) { | |
1334 | if (hooks_ != NULL && hooks_->ExecuteEnd != NULL) | |
1335 | (*hooks_->ExecuteEnd)(context, handle); | |
1336 | throw; | |
1337 | } | |
9cad30fa JF |
1338 | } |
1339 | ||
1340 | extern "C" void CydgetSetupContext(JSGlobalContextRef context) { | |
1341 | CYSetupContext(context); | |
1342 | } | |
1343 | ||
09eee478 JF |
1344 | static bool initialized_ = false; |
1345 | ||
9cad30fa | 1346 | void CYInitializeDynamic() { |
09eee478 JF |
1347 | if (!initialized_) |
1348 | initialized_ = true; | |
1349 | else return; | |
1350 | ||
9cad30fa JF |
1351 | JSObjectMakeArray$ = reinterpret_cast<JSObjectRef (*)(JSContextRef, size_t, const JSValueRef[], JSValueRef *)>(dlsym(RTLD_DEFAULT, "JSObjectMakeArray")); |
1352 | ||
1353 | JSClassDefinition definition; | |
1354 | ||
1355 | definition = kJSClassDefinitionEmpty; | |
1356 | definition.className = "All"; | |
1357 | definition.getProperty = &All_getProperty; | |
26ef7a82 | 1358 | definition.getPropertyNames = &All_getPropertyNames; |
9cad30fa JF |
1359 | All_ = JSClassCreate(&definition); |
1360 | ||
1361 | definition = kJSClassDefinitionEmpty; | |
1362 | definition.className = "Context"; | |
1363 | definition.finalize = &CYFinalize; | |
1364 | Context_ = JSClassCreate(&definition); | |
1365 | ||
1366 | definition = kJSClassDefinitionEmpty; | |
1367 | definition.className = "Functor"; | |
1368 | definition.staticFunctions = cy::Functor::StaticFunctions; | |
7d894647 | 1369 | definition.staticValues = Functor_staticValues; |
9cad30fa JF |
1370 | definition.callAsFunction = &Functor_callAsFunction; |
1371 | definition.finalize = &CYFinalize; | |
1372 | Functor_ = JSClassCreate(&definition); | |
1373 | ||
1374 | definition = kJSClassDefinitionEmpty; | |
1375 | definition.className = "Pointer"; | |
1376 | definition.staticFunctions = Pointer_staticFunctions; | |
1377 | definition.getProperty = &Pointer_getProperty; | |
1378 | definition.setProperty = &Pointer_setProperty; | |
1379 | definition.finalize = &CYFinalize; | |
1380 | Pointer_ = JSClassCreate(&definition); | |
1381 | ||
1382 | definition = kJSClassDefinitionEmpty; | |
1383 | definition.className = "Struct"; | |
1384 | definition.staticFunctions = Struct_staticFunctions; | |
1385 | definition.getProperty = &Struct_getProperty; | |
1386 | definition.setProperty = &Struct_setProperty; | |
1387 | definition.getPropertyNames = &Struct_getPropertyNames; | |
1388 | definition.finalize = &CYFinalize; | |
1389 | Struct_ = JSClassCreate(&definition); | |
1390 | ||
1391 | definition = kJSClassDefinitionEmpty; | |
1392 | definition.className = "Type"; | |
74e8c566 | 1393 | definition.staticValues = Type_staticValues; |
9cad30fa | 1394 | definition.staticFunctions = Type_staticFunctions; |
9cad30fa JF |
1395 | definition.callAsFunction = &Type_callAsFunction; |
1396 | definition.callAsConstructor = &Type_callAsConstructor; | |
1397 | definition.finalize = &CYFinalize; | |
1398 | Type_privateData::Class_ = JSClassCreate(&definition); | |
1399 | ||
1400 | definition = kJSClassDefinitionEmpty; | |
56a66df3 | 1401 | definition.className = "Global"; |
9cad30fa JF |
1402 | //definition.getProperty = &Global_getProperty; |
1403 | Global_ = JSClassCreate(&definition); | |
1404 | ||
1405 | Array_s = JSStringCreateWithUTF8CString("Array"); | |
1406 | cy_s = JSStringCreateWithUTF8CString("$cy"); | |
1407 | length_s = JSStringCreateWithUTF8CString("length"); | |
1408 | message_s = JSStringCreateWithUTF8CString("message"); | |
1409 | name_s = JSStringCreateWithUTF8CString("name"); | |
1410 | pop_s = JSStringCreateWithUTF8CString("pop"); | |
1411 | prototype_s = JSStringCreateWithUTF8CString("prototype"); | |
1412 | push_s = JSStringCreateWithUTF8CString("push"); | |
1413 | splice_s = JSStringCreateWithUTF8CString("splice"); | |
1414 | toCYON_s = JSStringCreateWithUTF8CString("toCYON"); | |
1415 | toJSON_s = JSStringCreateWithUTF8CString("toJSON"); | |
20ded97a | 1416 | toPointer_s = JSStringCreateWithUTF8CString("toPointer"); |
4cb8aa43 | 1417 | toString_s = JSStringCreateWithUTF8CString("toString"); |
9cad30fa JF |
1418 | |
1419 | Result_ = JSStringCreateWithUTF8CString("_"); | |
1420 | ||
1421 | if (hooks_ != NULL && hooks_->Initialize != NULL) | |
1422 | (*hooks_->Initialize)(); | |
1423 | } | |
1424 | ||
1425 | void CYThrow(JSContextRef context, JSValueRef value) { | |
1426 | if (value != NULL) | |
1427 | throw CYJSError(context, value); | |
1428 | } | |
1429 | ||
b799113b | 1430 | const char *CYJSError::PoolCString(CYPool &pool) const { |
9cad30fa JF |
1431 | // XXX: this used to be CYPoolCString |
1432 | return CYPoolCCYON(pool, context_, value_); | |
1433 | } | |
1434 | ||
1435 | JSValueRef CYJSError::CastJSValue(JSContextRef context) const { | |
1436 | // XXX: what if the context is different? | |
1437 | return value_; | |
1438 | } | |
1439 | ||
1440 | JSValueRef CYCastJSError(JSContextRef context, const char *message) { | |
1441 | JSObjectRef Error(CYGetCachedObject(context, CYJSString("Error"))); | |
9cad30fa | 1442 | JSValueRef arguments[1] = {CYCastJSValue(context, message)}; |
f1b5a47f | 1443 | return _jsccall(JSObjectCallAsConstructor, context, Error, 1, arguments); |
9cad30fa JF |
1444 | } |
1445 | ||
1446 | JSValueRef CYPoolError::CastJSValue(JSContextRef context) const { | |
1447 | return CYCastJSError(context, message_); | |
1448 | } | |
1449 | ||
1450 | CYJSError::CYJSError(JSContextRef context, const char *format, ...) { | |
1451 | _assert(context != NULL); | |
1452 | ||
1453 | CYPool pool; | |
1454 | ||
1455 | va_list args; | |
1456 | va_start(args, format); | |
0cbeddf8 JF |
1457 | // XXX: there might be a beter way to think about this |
1458 | const char *message(pool.vsprintf(64, format, args)); | |
9cad30fa JF |
1459 | va_end(args); |
1460 | ||
1461 | value_ = CYCastJSError(context, message); | |
1462 | } | |
1463 | ||
1464 | JSGlobalContextRef CYGetJSContext(JSContextRef context) { | |
1465 | return reinterpret_cast<Context *>(JSObjectGetPrivate(CYCastJSObject(context, CYGetProperty(context, CYGetGlobalObject(context), cy_s))))->context_; | |
1466 | } | |
1467 | ||
1468 | extern "C" void CYSetupContext(JSGlobalContextRef context) { | |
1469 | CYInitializeDynamic(); | |
1470 | ||
1471 | JSObjectRef global(CYGetGlobalObject(context)); | |
1472 | ||
1473 | JSObjectRef cy(JSObjectMake(context, Context_, new Context(context))); | |
1474 | CYSetProperty(context, global, cy_s, cy, kJSPropertyAttributeDontEnum); | |
1475 | ||
1476 | /* Cache Globals {{{ */ | |
1477 | JSObjectRef Array(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array")))); | |
1478 | CYSetProperty(context, cy, CYJSString("Array"), Array); | |
1479 | ||
1480 | JSObjectRef Array_prototype(CYCastJSObject(context, CYGetProperty(context, Array, prototype_s))); | |
1481 | CYSetProperty(context, cy, CYJSString("Array_prototype"), Array_prototype); | |
1482 | ||
1483 | JSObjectRef Error(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Error")))); | |
1484 | CYSetProperty(context, cy, CYJSString("Error"), Error); | |
1485 | ||
1486 | JSObjectRef Function(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function")))); | |
1487 | CYSetProperty(context, cy, CYJSString("Function"), Function); | |
1488 | ||
1489 | JSObjectRef Function_prototype(CYCastJSObject(context, CYGetProperty(context, Function, prototype_s))); | |
1490 | CYSetProperty(context, cy, CYJSString("Function_prototype"), Function_prototype); | |
1491 | ||
1492 | JSObjectRef Object(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Object")))); | |
1493 | CYSetProperty(context, cy, CYJSString("Object"), Object); | |
1494 | ||
1495 | JSObjectRef Object_prototype(CYCastJSObject(context, CYGetProperty(context, Object, prototype_s))); | |
1496 | CYSetProperty(context, cy, CYJSString("Object_prototype"), Object_prototype); | |
1497 | ||
1498 | JSObjectRef String(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("String")))); | |
1499 | CYSetProperty(context, cy, CYJSString("String"), String); | |
4cb8aa43 JF |
1500 | |
1501 | JSObjectRef String_prototype(CYCastJSObject(context, CYGetProperty(context, String, prototype_s))); | |
1502 | CYSetProperty(context, cy, CYJSString("String_prototype"), String_prototype); | |
9cad30fa JF |
1503 | /* }}} */ |
1504 | ||
1505 | CYSetProperty(context, Array_prototype, toCYON_s, &Array_callAsFunction_toCYON, kJSPropertyAttributeDontEnum); | |
4cb8aa43 | 1506 | CYSetProperty(context, String_prototype, toCYON_s, &String_callAsFunction_toCYON, kJSPropertyAttributeDontEnum); |
9cad30fa JF |
1507 | |
1508 | JSObjectRef cycript(JSObjectMake(context, NULL, NULL)); | |
1509 | CYSetProperty(context, global, CYJSString("Cycript"), cycript); | |
1510 | CYSetProperty(context, cycript, CYJSString("gc"), &Cycript_gc_callAsFunction); | |
1511 | ||
1512 | JSObjectRef Functor(JSObjectMakeConstructor(context, Functor_, &Functor_new)); | |
1513 | JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Functor, prototype_s)), Function_prototype); | |
1514 | CYSetProperty(context, cycript, CYJSString("Functor"), Functor); | |
1515 | ||
1516 | CYSetProperty(context, cycript, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new)); | |
b63701b2 | 1517 | CYSetProperty(context, cycript, CYJSString("Type"), JSObjectMakeConstructor(context, Type_privateData::Class_, &Type_new)); |
9cad30fa JF |
1518 | |
1519 | JSObjectRef all(JSObjectMake(context, All_, NULL)); | |
1520 | CYSetProperty(context, cycript, CYJSString("all"), all); | |
1521 | ||
f1b5a47f | 1522 | JSObjectRef alls(_jsccall(JSObjectCallAsConstructor, context, Array, 0, NULL)); |
26ef7a82 JF |
1523 | CYSetProperty(context, cycript, CYJSString("alls"), alls); |
1524 | ||
56a66df3 JF |
1525 | if (true) { |
1526 | JSObjectRef last(NULL), curr(global); | |
9cad30fa | 1527 | |
56a66df3 JF |
1528 | goto next; for (JSValueRef next;;) { |
1529 | if (JSValueIsNull(context, next)) | |
1530 | break; | |
1531 | last = curr; | |
1532 | curr = CYCastJSObject(context, next); | |
1533 | next: | |
1534 | next = JSObjectGetPrototype(context, curr); | |
1535 | } | |
9cad30fa | 1536 | |
56a66df3 JF |
1537 | JSObjectSetPrototype(context, last, all); |
1538 | } | |
9cad30fa | 1539 | |
56a66df3 | 1540 | CYSetProperty(context, global, CYJSString("$cyq"), &$cyq, kJSPropertyAttributeDontEnum); |
9cad30fa JF |
1541 | |
1542 | JSObjectRef System(JSObjectMake(context, NULL, NULL)); | |
cdc80ff2 | 1543 | CYSetProperty(context, cy, CYJSString("System"), System); |
9cad30fa JF |
1544 | |
1545 | CYSetProperty(context, global, CYJSString("system"), System); | |
1546 | CYSetProperty(context, System, CYJSString("args"), CYJSNull(context)); | |
1547 | //CYSetProperty(context, System, CYJSString("global"), global); | |
1548 | CYSetProperty(context, System, CYJSString("print"), &System_print); | |
1549 | ||
a621ae76 JF |
1550 | if (CYBridgeEntry *entry = CYBridgeHash("1dlerror", 8)) |
1551 | entry->cache_ = new cy::Functor(entry->value_, reinterpret_cast<void (*)()>(&dlerror)); | |
1552 | ||
9cad30fa JF |
1553 | if (hooks_ != NULL && hooks_->SetupContext != NULL) |
1554 | (*hooks_->SetupContext)(context); | |
26ef7a82 JF |
1555 | |
1556 | CYArrayPush(context, alls, cycript); | |
9cad30fa JF |
1557 | } |
1558 | ||
1559 | JSGlobalContextRef CYGetJSContext() { | |
1560 | CYInitializeDynamic(); | |
1561 | ||
1562 | static JSGlobalContextRef context_; | |
1563 | ||
1564 | if (context_ == NULL) { | |
1565 | context_ = JSGlobalContextCreate(Global_); | |
1566 | CYSetupContext(context_); | |
1567 | } | |
1568 | ||
1569 | return context_; | |
1570 | } |