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