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