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