]>
Commit | Line | Data |
---|---|---|
1 | /* Cycript - The Truly Universal Scripting Language | |
2 | * Copyright (C) 2009-2016 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 <sqlite3.h> | |
40 | ||
41 | #include "sig/parse.hpp" | |
42 | #include "sig/ffi_type.hpp" | |
43 | ||
44 | #include "Bridge.hpp" | |
45 | #include "Code.hpp" | |
46 | #include "Decode.hpp" | |
47 | #include "Error.hpp" | |
48 | #include "Execute.hpp" | |
49 | #include "Internal.hpp" | |
50 | #include "JavaScript.hpp" | |
51 | #include "Pooling.hpp" | |
52 | #include "String.hpp" | |
53 | ||
54 | const char *sqlite3_column_string(sqlite3_stmt *stmt, int n) { | |
55 | return reinterpret_cast<const char *>(sqlite3_column_text(stmt, n)); | |
56 | } | |
57 | ||
58 | char *sqlite3_column_pooled(CYPool &pool, sqlite3_stmt *stmt, int n) { | |
59 | if (const char *value = sqlite3_column_string(stmt, n)) | |
60 | return pool.strdup(value); | |
61 | else return NULL; | |
62 | } | |
63 | ||
64 | static std::vector<CYHook *> &GetHooks() { | |
65 | static std::vector<CYHook *> hooks; | |
66 | return hooks; | |
67 | } | |
68 | ||
69 | CYRegisterHook::CYRegisterHook(CYHook *hook) { | |
70 | GetHooks().push_back(hook); | |
71 | } | |
72 | ||
73 | /* JavaScript Properties {{{ */ | |
74 | bool CYHasProperty(JSContextRef context, JSObjectRef object, JSStringRef name) { | |
75 | return JSObjectHasProperty(context, object, name); | |
76 | } | |
77 | ||
78 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index) { | |
79 | return _jsccall(JSObjectGetPropertyAtIndex, context, object, index); | |
80 | } | |
81 | ||
82 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name) { | |
83 | return _jsccall(JSObjectGetProperty, context, object, name); | |
84 | } | |
85 | ||
86 | void CYSetProperty(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value) { | |
87 | _jsccall(JSObjectSetPropertyAtIndex, context, object, index, value); | |
88 | } | |
89 | ||
90 | void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value, JSPropertyAttributes attributes) { | |
91 | _jsccall(JSObjectSetProperty, context, object, name, value, attributes); | |
92 | } | |
93 | ||
94 | void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef (*callback)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *), JSPropertyAttributes attributes) { | |
95 | CYSetProperty(context, object, name, JSObjectMakeFunctionWithCallback(context, name, callback), attributes); | |
96 | } | |
97 | ||
98 | JSObjectRef CYGetPrototype(JSContextRef context, JSObjectRef object) { | |
99 | return CYCastJSObject(context, JSObjectGetPrototype(context, object)); | |
100 | } | |
101 | ||
102 | void CYSetPrototype(JSContextRef context, JSObjectRef object, JSValueRef value) { | |
103 | _assert(!JSValueIsUndefined(context, value)); | |
104 | JSObjectSetPrototype(context, object, value); | |
105 | _assert(CYIsStrictEqual(context, JSObjectGetPrototype(context, object), value)); | |
106 | } | |
107 | /* }}} */ | |
108 | /* JavaScript Strings {{{ */ | |
109 | JSStringRef CYCopyJSString(const char *value) { | |
110 | return value == NULL ? NULL : JSStringCreateWithUTF8CString(value); | |
111 | } | |
112 | ||
113 | JSStringRef CYCopyJSString(JSStringRef value) { | |
114 | return value == NULL ? NULL : JSStringRetain(value); | |
115 | } | |
116 | ||
117 | JSStringRef CYCopyJSString(CYUTF8String value) { | |
118 | if (memchr(value.data, '\0', value.size) != NULL) { | |
119 | CYPool pool; | |
120 | return CYCopyJSString(CYPoolUTF16String(pool, value)); | |
121 | } else if (value.data[value.size] != '\0') { | |
122 | CYPool pool; | |
123 | return CYCopyJSString(pool.strmemdup(value.data, value.size)); | |
124 | } else { | |
125 | return CYCopyJSString(value.data); | |
126 | } | |
127 | } | |
128 | ||
129 | JSStringRef CYCopyJSString(const std::string &value) { | |
130 | return CYCopyJSString(CYUTF8String(value.c_str(), value.size())); | |
131 | } | |
132 | ||
133 | JSStringRef CYCopyJSString(CYUTF16String value) { | |
134 | return JSStringCreateWithCharacters(value.data, value.size); | |
135 | } | |
136 | ||
137 | JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value) { | |
138 | if (JSValueIsNull(context, value)) | |
139 | return NULL; | |
140 | return _jsccall(JSValueToStringCopy, context, value); | |
141 | } | |
142 | ||
143 | CYUTF16String CYCastUTF16String(JSStringRef value) { | |
144 | return CYUTF16String(JSStringGetCharactersPtr(value), JSStringGetLength(value)); | |
145 | } | |
146 | ||
147 | const char *CYPoolCString(CYPool &pool, CYUTF8String utf8) { | |
148 | return pool.strndup(utf8.data, utf8.size); | |
149 | } | |
150 | ||
151 | CYUTF8String CYPoolUTF8String(CYPool &pool, CYUTF8String utf8) { | |
152 | return {pool.strndup(utf8.data, utf8.size), utf8.size}; | |
153 | } | |
154 | ||
155 | _visible CYUTF8String CYPoolUTF8String(CYPool &pool, const std::string &value) { | |
156 | return {pool.strndup(value.data(), value.size()), value.size()}; | |
157 | } | |
158 | ||
159 | CYUTF8String CYPoolUTF8String(CYPool &pool, JSContextRef context, JSStringRef value) { | |
160 | return CYPoolUTF8String(pool, CYCastUTF16String(value)); | |
161 | } | |
162 | ||
163 | const char *CYPoolCString(CYPool &pool, JSContextRef context, JSStringRef value) { | |
164 | CYUTF8String utf8(CYPoolUTF8String(pool, context, value)); | |
165 | _assert(memchr(utf8.data, '\0', utf8.size) == NULL); | |
166 | return utf8.data; | |
167 | } | |
168 | ||
169 | const char *CYPoolCString(CYPool &pool, JSContextRef context, JSValueRef value) { | |
170 | return JSValueIsNull(context, value) ? NULL : CYPoolCString(pool, context, CYJSString(context, value)); | |
171 | } | |
172 | /* }}} */ | |
173 | /* Index Offsets {{{ */ | |
174 | size_t CYGetIndex(CYPool &pool, JSContextRef context, JSStringRef value) { | |
175 | return CYGetIndex(CYPoolUTF8String(pool, context, value)); | |
176 | } | |
177 | /* }}} */ | |
178 | ||
179 | static JSObjectRef (*JSObjectMakeArray$)(JSContextRef, size_t, const JSValueRef[], JSValueRef *); | |
180 | ||
181 | JSObjectRef CYObjectMakeArray(JSContextRef context, size_t length, const JSValueRef values[]) { | |
182 | if (JSObjectMakeArray$ != NULL) | |
183 | return _jsccall(*JSObjectMakeArray$, context, length, values); | |
184 | JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array"))); | |
185 | bool wat(length == 1 && JSValueGetType(context, values[0]) == kJSTypeNumber); | |
186 | JSValueRef value(CYCallAsFunction(context, Array, NULL, wat ? 0 : length, values)); | |
187 | JSObjectRef object(CYCastJSObject(context, value)); | |
188 | if (wat) CYArrayPush(context, object, 1, values); | |
189 | return object; | |
190 | } | |
191 | ||
192 | static JSClassRef All_; | |
193 | JSClassRef cy::Functor::Class_; | |
194 | static JSClassRef Global_; | |
195 | ||
196 | JSStringRef Array_s; | |
197 | JSStringRef constructor_s; | |
198 | JSStringRef cy_s; | |
199 | JSStringRef cyi_s; | |
200 | JSStringRef cyt_s; | |
201 | JSStringRef cyt__s; | |
202 | JSStringRef length_s; | |
203 | JSStringRef message_s; | |
204 | JSStringRef name_s; | |
205 | JSStringRef pop_s; | |
206 | JSStringRef prototype_s; | |
207 | JSStringRef push_s; | |
208 | JSStringRef splice_s; | |
209 | JSStringRef toCYON_s; | |
210 | JSStringRef toJSON_s; | |
211 | JSStringRef toPointer_s; | |
212 | JSStringRef toString_s; | |
213 | JSStringRef weak_s; | |
214 | ||
215 | static sqlite3 *database_; | |
216 | ||
217 | static JSStringRef Result_; | |
218 | ||
219 | void CYFinalize(JSObjectRef object) { | |
220 | CYData *internal(reinterpret_cast<CYData *>(JSObjectGetPrivate(object))); | |
221 | if (internal == NULL) | |
222 | return; | |
223 | _assert(internal->count_ != _not(unsigned)); | |
224 | if (--internal->count_ == 0) | |
225 | delete internal; | |
226 | } | |
227 | ||
228 | sig::Type *Structor_(CYPool &pool, sig::Aggregate *aggregate) { | |
229 | //_assert(false); | |
230 | return aggregate; | |
231 | } | |
232 | ||
233 | struct Context : | |
234 | CYRoot | |
235 | { | |
236 | JSGlobalContextRef context_; | |
237 | ||
238 | Context(JSGlobalContextRef context) : | |
239 | context_(context) | |
240 | { | |
241 | } | |
242 | }; | |
243 | ||
244 | struct CArray : | |
245 | CYRoot | |
246 | { | |
247 | void *value_; | |
248 | CYProtect owner_; | |
249 | Type_privateData *type_; | |
250 | size_t length_; | |
251 | ||
252 | CArray(void *value, size_t length, const sig::Type &type, ffi_type *ffi, JSContextRef context, JSObjectRef owner) : | |
253 | value_(value), | |
254 | owner_(context, owner), | |
255 | type_(new(*pool_) Type_privateData(type, ffi)), | |
256 | length_(length) | |
257 | { | |
258 | } | |
259 | }; | |
260 | ||
261 | struct CString : | |
262 | CYRoot | |
263 | { | |
264 | char *value_; | |
265 | CYProtect owner_; | |
266 | ||
267 | CString(char *value, JSContextRef context, JSObjectRef owner) : | |
268 | value_(value), | |
269 | owner_(context, owner) | |
270 | { | |
271 | } | |
272 | }; | |
273 | ||
274 | struct Pointer : | |
275 | CYRoot | |
276 | { | |
277 | void *value_; | |
278 | CYProtect owner_; | |
279 | Type_privateData *type_; | |
280 | ||
281 | Pointer(void *value, const sig::Type &type, JSContextRef context, JSObjectRef owner) : | |
282 | value_(value), | |
283 | owner_(context, owner), | |
284 | type_(new(*pool_) Type_privateData(type)) | |
285 | { | |
286 | } | |
287 | ||
288 | Pointer(void *value, const char *encoding, JSContextRef context, JSObjectRef owner) : | |
289 | value_(value), | |
290 | owner_(context, owner), | |
291 | type_(new(*pool_) Type_privateData(encoding)) | |
292 | { | |
293 | } | |
294 | }; | |
295 | ||
296 | struct Struct_privateData : | |
297 | CYRoot | |
298 | { | |
299 | void *value_; | |
300 | CYProtect owner_; | |
301 | Type_privateData *type_; | |
302 | ||
303 | Struct_privateData(void *value, const sig::Type &type, ffi_type *ffi, JSContextRef context, JSObjectRef owner) : | |
304 | value_(value), | |
305 | owner_(context, owner), | |
306 | type_(new(*pool_) Type_privateData(type, ffi)) | |
307 | { | |
308 | if (owner == NULL) { | |
309 | size_t size(ffi->size); | |
310 | void *copy(pool_->malloc<void>(size, ffi->alignment)); | |
311 | memcpy(copy, value_, size); | |
312 | value_ = copy; | |
313 | } | |
314 | } | |
315 | }; | |
316 | ||
317 | static void *CYCastSymbol(const char *name) { | |
318 | for (CYHook *hook : GetHooks()) | |
319 | if (hook->CastSymbol != NULL) | |
320 | if (void *value = (*hook->CastSymbol)(name)) | |
321 | return value; | |
322 | return dlsym(RTLD_DEFAULT, name); | |
323 | } | |
324 | ||
325 | JSValueRef CYCastJSValue(JSContextRef context, bool value) { | |
326 | return JSValueMakeBoolean(context, value); | |
327 | } | |
328 | ||
329 | JSValueRef CYCastJSValue(JSContextRef context, double value) { | |
330 | return JSValueMakeNumber(context, value); | |
331 | } | |
332 | ||
333 | #define CYCastJSValue_(Type_) \ | |
334 | JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \ | |
335 | _assert(static_cast<Type_>(static_cast<double>(value)) == value); \ | |
336 | return JSValueMakeNumber(context, static_cast<double>(value)); \ | |
337 | } | |
338 | ||
339 | CYCastJSValue_(long double) | |
340 | CYCastJSValue_(signed short int) | |
341 | CYCastJSValue_(unsigned short int) | |
342 | CYCastJSValue_(signed int) | |
343 | CYCastJSValue_(unsigned int) | |
344 | CYCastJSValue_(signed long int) | |
345 | CYCastJSValue_(unsigned long int) | |
346 | CYCastJSValue_(signed long long int) | |
347 | CYCastJSValue_(unsigned long long int) | |
348 | ||
349 | #ifdef __SIZEOF_INT128__ | |
350 | CYCastJSValue_(signed __int128) | |
351 | CYCastJSValue_(unsigned __int128) | |
352 | #endif | |
353 | ||
354 | JSValueRef CYJSUndefined(JSContextRef context) { | |
355 | return JSValueMakeUndefined(context); | |
356 | } | |
357 | ||
358 | double CYCastDouble(JSContextRef context, JSValueRef value) { | |
359 | return _jsccall(JSValueToNumber, context, value); | |
360 | } | |
361 | ||
362 | bool CYCastBool(JSContextRef context, JSValueRef value) { | |
363 | return JSValueToBoolean(context, value); | |
364 | } | |
365 | ||
366 | JSValueRef CYJSNull(JSContextRef context) { | |
367 | return JSValueMakeNull(context); | |
368 | } | |
369 | ||
370 | JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value) { | |
371 | return value == NULL ? CYJSNull(context) : JSValueMakeString(context, value); | |
372 | } | |
373 | ||
374 | JSValueRef CYCastJSValue(JSContextRef context, const char *value) { | |
375 | return CYCastJSValue(context, CYJSString(value)); | |
376 | } | |
377 | ||
378 | JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) { | |
379 | return _jsccall(JSValueToObject, context, value); | |
380 | } | |
381 | ||
382 | JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, const JSValueRef arguments[]) { | |
383 | return _jsccall(JSObjectCallAsFunction, context, function, _this, count, arguments); | |
384 | } | |
385 | ||
386 | bool CYIsCallable(JSContextRef context, JSValueRef value) { | |
387 | return value != NULL && JSValueIsObject(context, value) && JSObjectIsFunction(context, (JSObjectRef) value); | |
388 | } | |
389 | ||
390 | bool CYIsEqual(JSContextRef context, JSValueRef lhs, JSValueRef rhs) { | |
391 | return _jsccall(JSValueIsEqual, context, lhs, rhs); | |
392 | } | |
393 | ||
394 | bool CYIsStrictEqual(JSContextRef context, JSValueRef lhs, JSValueRef rhs) { | |
395 | return JSValueIsStrictEqual(context, lhs, rhs); | |
396 | } | |
397 | ||
398 | size_t CYArrayLength(JSContextRef context, JSObjectRef array) { | |
399 | return CYCastDouble(context, CYGetProperty(context, array, length_s)); | |
400 | } | |
401 | ||
402 | JSValueRef CYArrayGet(JSContextRef context, JSObjectRef array, size_t index) { | |
403 | return _jsccall(JSObjectGetPropertyAtIndex, context, array, index); | |
404 | } | |
405 | ||
406 | void CYArrayPush(JSContextRef context, JSObjectRef array, size_t length, const JSValueRef arguments[]) { | |
407 | JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array_prototype"))); | |
408 | _jsccall(JSObjectCallAsFunction, context, CYCastJSObject(context, CYGetProperty(context, Array, push_s)), array, length, arguments); | |
409 | } | |
410 | ||
411 | void CYArrayPush(JSContextRef context, JSObjectRef array, JSValueRef value) { | |
412 | return CYArrayPush(context, array, 1, &value); | |
413 | } | |
414 | ||
415 | static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
416 | FILE *file(stdout); | |
417 | ||
418 | if (count == 0) | |
419 | fputc('\n', file); | |
420 | else { | |
421 | CYPool pool; | |
422 | CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, arguments[0]))); | |
423 | fwrite(string.data, string.size, 1, file); | |
424 | } | |
425 | ||
426 | fflush(file); | |
427 | return CYJSUndefined(context); | |
428 | } CYCatch(NULL) } | |
429 | ||
430 | static JSValueRef Global_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
431 | FILE *file(stdout); | |
432 | CYPool pool; | |
433 | ||
434 | for (size_t i(0); i != count; ++i) { | |
435 | if (i != 0) | |
436 | fputc(' ', file); | |
437 | CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, arguments[i]))); | |
438 | fwrite(string.data, string.size, 1, file); | |
439 | } | |
440 | ||
441 | fputc('\n', file); | |
442 | fflush(file); | |
443 | return CYJSUndefined(context); | |
444 | } CYCatch(NULL) } | |
445 | ||
446 | static void (*JSSynchronousGarbageCollectForDebugging$)(JSContextRef); | |
447 | ||
448 | _visible void CYGarbageCollect(JSContextRef context) { | |
449 | (JSSynchronousGarbageCollectForDebugging$ ?: &JSGarbageCollect)(context); | |
450 | } | |
451 | ||
452 | static JSValueRef Cycript_compile_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
453 | CYPool pool; | |
454 | CYUTF8String before(CYPoolUTF8String(pool, context, CYJSString(context, arguments[0]))); | |
455 | CYUTF8String after(CYPoolCode(pool, before)); | |
456 | return CYCastJSValue(context, CYJSString(after)); | |
457 | } CYCatch_(NULL, "SyntaxError") } | |
458 | ||
459 | static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
460 | CYGarbageCollect(context); | |
461 | return CYJSUndefined(context); | |
462 | } CYCatch(NULL) } | |
463 | ||
464 | const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value, std::set<void *> &objects, JSValueRef *exception) { CYTry { | |
465 | switch (JSType type = JSValueGetType(context, value)) { | |
466 | case kJSTypeUndefined: | |
467 | return "undefined"; | |
468 | case kJSTypeNull: | |
469 | return "null"; | |
470 | case kJSTypeBoolean: | |
471 | return CYCastBool(context, value) ? "true" : "false"; | |
472 | ||
473 | case kJSTypeNumber: { | |
474 | std::ostringstream str; | |
475 | CYNumerify(str, CYCastDouble(context, value)); | |
476 | std::string value(str.str()); | |
477 | return pool.strmemdup(value.c_str(), value.size()); | |
478 | } break; | |
479 | ||
480 | case kJSTypeString: { | |
481 | std::ostringstream str; | |
482 | CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, value))); | |
483 | CYStringify(str, string.data, string.size, CYStringifyModeCycript); | |
484 | std::string value(str.str()); | |
485 | return pool.strmemdup(value.c_str(), value.size()); | |
486 | } break; | |
487 | ||
488 | case kJSTypeObject: | |
489 | return CYPoolCCYON(pool, context, (JSObjectRef) value, objects); | |
490 | default: | |
491 | throw CYJSError(context, "JSValueGetType() == 0x%x", type); | |
492 | } | |
493 | } CYCatch(NULL) } | |
494 | ||
495 | const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value, std::set<void *> &objects) { | |
496 | return _jsccall(CYPoolCCYON, pool, context, value, objects); | |
497 | } | |
498 | ||
499 | const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value, std::set<void *> *objects) { | |
500 | if (objects != NULL) | |
501 | return CYPoolCCYON(pool, context, value, *objects); | |
502 | else { | |
503 | std::set<void *> objects; | |
504 | return CYPoolCCYON(pool, context, value, objects); | |
505 | } | |
506 | } | |
507 | ||
508 | const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object, std::set<void *> &objects) { | |
509 | JSValueRef toCYON(CYGetProperty(context, object, toCYON_s)); | |
510 | if (CYIsCallable(context, toCYON)) { | |
511 | // XXX: this needs to be abstracted behind some kind of function | |
512 | JSValueRef arguments[1] = {CYCastJSValue(context, reinterpret_cast<uintptr_t>(&objects))}; | |
513 | JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toCYON, object, 1, arguments)); | |
514 | _assert(value != NULL); | |
515 | return CYPoolCString(pool, context, value); | |
516 | } | |
517 | ||
518 | JSValueRef toJSON(CYGetProperty(context, object, toJSON_s)); | |
519 | if (CYIsCallable(context, toJSON)) { | |
520 | JSValueRef arguments[1] = {CYCastJSValue(context, CYJSString(""))}; | |
521 | return _jsccall(CYPoolCCYON, pool, context, CYCallAsFunction(context, (JSObjectRef) toJSON, object, 1, arguments), objects); | |
522 | } | |
523 | ||
524 | if (JSObjectIsFunction(context, object)) { | |
525 | JSValueRef toString(CYGetProperty(context, object, toString_s)); | |
526 | if (CYIsCallable(context, toString)) { | |
527 | JSValueRef arguments[1] = {CYCastJSValue(context, CYJSString(""))}; | |
528 | JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toString, object, 1, arguments)); | |
529 | _assert(value != NULL); | |
530 | return CYPoolCString(pool, context, value); | |
531 | } | |
532 | } | |
533 | ||
534 | _assert(objects.insert(object).second); | |
535 | ||
536 | std::ostringstream str; | |
537 | ||
538 | JSValueRef value(CYGetProperty(context, object, constructor_s)); | |
539 | if (JSValueIsObject(context, value)) { | |
540 | JSObjectRef constructor(CYCastJSObject(context, value)); | |
541 | JSValueRef theory(CYGetProperty(context, constructor, prototype_s)); | |
542 | JSValueRef practice(JSObjectGetPrototype(context, object)); | |
543 | ||
544 | if (CYIsStrictEqual(context, theory, practice)) { | |
545 | JSValueRef name(CYGetProperty(context, constructor, name_s)); | |
546 | if (!JSValueIsUndefined(context, name)) { | |
547 | auto utf8(CYPoolUTF8String(pool, context, CYJSString(context, name))); | |
548 | if (utf8 != "Object") | |
549 | str << "new" << ' ' << utf8; | |
550 | } | |
551 | } | |
552 | } | |
553 | ||
554 | str << '{'; | |
555 | ||
556 | // XXX: this is, sadly, going to leak | |
557 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context, object)); | |
558 | ||
559 | bool comma(false); | |
560 | ||
561 | for (size_t index(0), count(JSPropertyNameArrayGetCount(names)); index != count; ++index) { | |
562 | if (comma) | |
563 | str << ','; | |
564 | else | |
565 | comma = true; | |
566 | ||
567 | JSStringRef name(JSPropertyNameArrayGetNameAtIndex(names, index)); | |
568 | CYUTF8String string(CYPoolUTF8String(pool, context, name)); | |
569 | ||
570 | if (CYIsKey(string)) | |
571 | str << string.data; | |
572 | else | |
573 | CYStringify(str, string.data, string.size, CYStringifyModeLegacy); | |
574 | ||
575 | str << ':'; | |
576 | ||
577 | try { | |
578 | JSValueRef value(CYGetProperty(context, object, name)); | |
579 | str << CYPoolCCYON(pool, context, value, objects); | |
580 | } catch (const CYException &error) { | |
581 | str << "@error"; | |
582 | } | |
583 | } | |
584 | ||
585 | JSPropertyNameArrayRelease(names); | |
586 | ||
587 | str << '}'; | |
588 | ||
589 | std::string string(str.str()); | |
590 | return pool.strmemdup(string.c_str(), string.size()); | |
591 | } | |
592 | ||
593 | std::set<void *> *CYCastObjects(JSContextRef context, JSObjectRef _this, size_t count, const JSValueRef arguments[]) { | |
594 | if (count == 0) | |
595 | return NULL; | |
596 | return CYCastPointer<std::set<void *> *>(context, arguments[0]); | |
597 | } | |
598 | ||
599 | static JSValueRef Array_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
600 | std::set<void *> *objects(CYCastObjects(context, _this, count, arguments)); | |
601 | // XXX: this is horribly inefficient | |
602 | std::set<void *> backup; | |
603 | if (objects == NULL) | |
604 | objects = &backup; | |
605 | ||
606 | CYPool pool; | |
607 | std::ostringstream str; | |
608 | ||
609 | str << '['; | |
610 | ||
611 | JSValueRef length(CYGetProperty(context, _this, length_s)); | |
612 | bool comma(false); | |
613 | ||
614 | for (size_t index(0), count(CYCastDouble(context, length)); index != count; ++index) { | |
615 | if (comma) | |
616 | str << ','; | |
617 | else | |
618 | comma = true; | |
619 | ||
620 | try { | |
621 | JSValueRef value(CYGetProperty(context, _this, index)); | |
622 | if (!JSValueIsUndefined(context, value)) | |
623 | str << CYPoolCCYON(pool, context, value, *objects); | |
624 | else { | |
625 | str << ','; | |
626 | comma = false; | |
627 | } | |
628 | } catch (const CYException &error) { | |
629 | str << "@error"; | |
630 | } | |
631 | } | |
632 | ||
633 | str << ']'; | |
634 | ||
635 | std::string value(str.str()); | |
636 | return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size()))); | |
637 | } CYCatch(NULL) } | |
638 | ||
639 | static JSValueRef String_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
640 | CYPool pool; | |
641 | std::ostringstream str; | |
642 | ||
643 | CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, _this))); | |
644 | CYStringify(str, string.data, string.size, CYStringifyModeCycript); | |
645 | ||
646 | std::string value(str.str()); | |
647 | return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size()))); | |
648 | } CYCatch(NULL) } | |
649 | ||
650 | JSObjectRef CYMakePointer(JSContextRef context, void *pointer, const sig::Type &type, ffi_type *ffi, JSObjectRef owner) { | |
651 | return CYPrivate<Pointer>::Make(context, pointer, type, context, owner); | |
652 | } | |
653 | ||
654 | static JSValueRef CYMakeFunctor(JSContextRef context, void (*function)(), bool variadic, const sig::Signature &signature) { | |
655 | if (function == NULL) | |
656 | return CYJSNull(context); | |
657 | return JSObjectMake(context, cy::Functor::Class_, new cy::Functor(function, variadic, signature)); | |
658 | } | |
659 | ||
660 | // XXX: remove this, as it is really stupid | |
661 | static JSObjectRef CYMakeFunctor(JSContextRef context, const char *symbol, const char *encoding) { | |
662 | void (*function)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol))); | |
663 | if (function == NULL) | |
664 | return NULL; | |
665 | ||
666 | cy::Functor *internal(new cy::Functor(function, encoding)); | |
667 | ++internal->count_; | |
668 | return JSObjectMake(context, cy::Functor::Class_, internal); | |
669 | } | |
670 | ||
671 | bool CYGetOffset(CYPool &pool, JSContextRef context, JSStringRef value, ssize_t &index) { | |
672 | return CYGetOffset(CYPoolCString(pool, context, value), index); | |
673 | } | |
674 | ||
675 | // XXX: this is a horrible back added for CFType | |
676 | void *CYCastPointerEx_(JSContextRef context, JSObjectRef value) { | |
677 | JSObjectRef object((JSObjectRef) value); | |
678 | if (JSValueIsObjectOfClass(context, value, CYPrivate<Pointer>::Class_)) { | |
679 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
680 | return internal->value_; | |
681 | } | |
682 | ||
683 | JSValueRef toPointer(CYGetProperty(context, object, toPointer_s)); | |
684 | if (CYIsCallable(context, toPointer)) { | |
685 | JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toPointer, object, 0, NULL)); | |
686 | _assert(value != NULL); | |
687 | return CYCastPointer_(context, value); | |
688 | } | |
689 | ||
690 | return NULL; | |
691 | } | |
692 | ||
693 | void *CYCastPointer_(JSContextRef context, JSValueRef value, bool *guess) { | |
694 | if (value == NULL) | |
695 | return NULL; | |
696 | else switch (JSValueGetType(context, value)) { | |
697 | case kJSTypeNull: | |
698 | return NULL; | |
699 | case kJSTypeObject: { | |
700 | JSObjectRef object((JSObjectRef) value); | |
701 | if (JSValueIsObjectOfClass(context, value, CYPrivate<Pointer>::Class_)) { | |
702 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
703 | return internal->value_; | |
704 | } | |
705 | JSValueRef toPointer(CYGetProperty(context, object, toPointer_s)); | |
706 | if (CYIsCallable(context, toPointer)) { | |
707 | JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toPointer, object, 0, NULL)); | |
708 | _assert(value != NULL); | |
709 | return CYCastPointer_(context, value, guess); | |
710 | } | |
711 | } default: | |
712 | if (guess != NULL) | |
713 | *guess = true; | |
714 | case kJSTypeNumber: | |
715 | double number(CYCastDouble(context, value)); | |
716 | if (!std::isnan(number)) | |
717 | return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number))); | |
718 | if (guess == NULL) | |
719 | throw CYJSError(context, "cannot convert value to pointer"); | |
720 | else { | |
721 | *guess = true; | |
722 | return NULL; | |
723 | } | |
724 | } | |
725 | } | |
726 | ||
727 | static JSValueRef FunctionAdapter_(JSContextRef context, size_t count, JSValueRef values[], JSObjectRef function); | |
728 | ||
729 | namespace sig { | |
730 | ||
731 | // XXX: this is somehow not quite a template :/ | |
732 | ||
733 | template <> | |
734 | void Primitive<bool>::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const { | |
735 | *reinterpret_cast<bool *>(data) = JSValueToBoolean(context, value); | |
736 | } | |
737 | ||
738 | #define CYPoolFFI_(Type_) \ | |
739 | template <> \ | |
740 | void Primitive<Type_>::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const { \ | |
741 | *reinterpret_cast<Type_ *>(data) = CYCastDouble(context, value); \ | |
742 | } | |
743 | ||
744 | CYPoolFFI_(wchar_t) | |
745 | CYPoolFFI_(float) | |
746 | CYPoolFFI_(double) | |
747 | CYPoolFFI_(long double) | |
748 | ||
749 | CYPoolFFI_(signed char) | |
750 | CYPoolFFI_(signed int) | |
751 | CYPoolFFI_(signed long int) | |
752 | CYPoolFFI_(signed long long int) | |
753 | CYPoolFFI_(signed short int) | |
754 | ||
755 | CYPoolFFI_(unsigned char) | |
756 | CYPoolFFI_(unsigned int) | |
757 | CYPoolFFI_(unsigned long int) | |
758 | CYPoolFFI_(unsigned long long int) | |
759 | CYPoolFFI_(unsigned short int) | |
760 | ||
761 | #ifdef __SIZEOF_INT128__ | |
762 | CYPoolFFI_(signed __int128) | |
763 | CYPoolFFI_(unsigned __int128) | |
764 | #endif | |
765 | ||
766 | template <> | |
767 | void Primitive<char>::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const { | |
768 | if (JSValueGetType(context, value) != kJSTypeString) | |
769 | *reinterpret_cast<char *>(data) = CYCastDouble(context, value); | |
770 | else { | |
771 | CYJSString script(context, value); | |
772 | auto string(CYCastUTF16String(script)); | |
773 | _assert(string.size == 1); | |
774 | _assert((string.data[0] & 0xff) == string.data[0]); | |
775 | *reinterpret_cast<char *>(data) = string.data[0]; | |
776 | } | |
777 | } | |
778 | ||
779 | void Void::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const { | |
780 | _assert(JSValueIsUndefined(context, value)); | |
781 | } | |
782 | ||
783 | void Unknown::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const { | |
784 | _assert(false); | |
785 | } | |
786 | ||
787 | void String::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const { | |
788 | bool guess(false); | |
789 | *reinterpret_cast<const char **>(data) = CYCastPointer<const char *>(context, value, &guess); | |
790 | if (guess && pool != NULL) | |
791 | *reinterpret_cast<const char **>(data) = CYPoolCString(*pool, context, value); | |
792 | } | |
793 | ||
794 | void Bits::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const { | |
795 | _assert(false); | |
796 | } | |
797 | ||
798 | static void CYArrayCopy(CYPool *pool, JSContextRef context, uint8_t *base, size_t length, const sig::Type &type, ffi_type *ffi, JSObjectRef object) { | |
799 | for (size_t index(0); index != length; ++index) { | |
800 | JSValueRef rhs(CYGetProperty(context, object, index)); | |
801 | if (JSValueIsUndefined(context, rhs)) | |
802 | throw CYJSError(context, "unable to extract array value"); | |
803 | type.PoolFFI(pool, context, ffi, base, rhs); | |
804 | base += ffi->size; | |
805 | } | |
806 | } | |
807 | ||
808 | void Pointer::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const { | |
809 | bool guess(false); | |
810 | *reinterpret_cast<void **>(data) = CYCastPointer<void *>(context, value, &guess); | |
811 | if (!guess || pool == NULL || !JSValueIsObject(context, value)) | |
812 | return; | |
813 | ||
814 | JSObjectRef object(CYCastJSObject(context, value)); | |
815 | ||
816 | if (sig::Function *function = dynamic_cast<sig::Function *>(&type)) { | |
817 | _assert(!function->variadic); | |
818 | auto internal(CYMakeFunctor_(context, object, function->signature, &FunctionAdapter_)); | |
819 | // XXX: see notes in Library.cpp about needing to leak | |
820 | *reinterpret_cast<void (**)()>(data) = internal->value_; | |
821 | } else if (CYHasProperty(context, object, length_s)) { | |
822 | size_t length(CYArrayLength(context, object)); | |
823 | ffi_type *element(type.GetFFI(*pool)); | |
824 | size_t size(element->size * length); | |
825 | uint8_t *base(pool->malloc<uint8_t>(size, element->alignment)); | |
826 | CYArrayCopy(pool, context, base, length, type, element, object); | |
827 | *reinterpret_cast<void **>(data) = base; | |
828 | } | |
829 | } | |
830 | ||
831 | void Array::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const { | |
832 | if (size == 0) | |
833 | return; | |
834 | uint8_t *base(reinterpret_cast<uint8_t *>(data)); | |
835 | CYArrayCopy(pool, context, base, size, type, ffi->elements[0], CYCastJSObject(context, value)); | |
836 | } | |
837 | ||
838 | void Enum::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const { | |
839 | return type.PoolFFI(pool, context, ffi, data, value); | |
840 | } | |
841 | ||
842 | void Aggregate::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const { | |
843 | _assert(!overlap); | |
844 | _assert(signature.count != _not(size_t)); | |
845 | ||
846 | size_t offset(0); | |
847 | uint8_t *base(reinterpret_cast<uint8_t *>(data)); | |
848 | JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL); | |
849 | for (size_t index(0); index != signature.count; ++index) { | |
850 | sig::Element *element(&signature.elements[index]); | |
851 | ffi_type *field(ffi->elements[index]); | |
852 | ||
853 | JSValueRef rhs; | |
854 | if (aggregate == NULL) | |
855 | rhs = value; | |
856 | else { | |
857 | rhs = CYGetProperty(context, aggregate, index); | |
858 | if (JSValueIsUndefined(context, rhs)) { | |
859 | if (element->name != NULL) | |
860 | rhs = CYGetProperty(context, aggregate, CYJSString(element->name)); | |
861 | else | |
862 | goto undefined; | |
863 | if (JSValueIsUndefined(context, rhs)) undefined: | |
864 | throw CYJSError(context, "unable to extract structure value"); | |
865 | } | |
866 | } | |
867 | ||
868 | element->type->PoolFFI(pool, context, field, base + offset, rhs); | |
869 | offset += field->size; | |
870 | CYAlign(offset, field->alignment); | |
871 | } | |
872 | } | |
873 | ||
874 | void Function::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const { | |
875 | _assert(false); | |
876 | } | |
877 | ||
878 | // XXX: this code is getting worse, not better :/ | |
879 | ||
880 | #define CYFromFFI_(Type_) \ | |
881 | template <> \ | |
882 | JSValueRef Primitive<Type_>::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { \ | |
883 | JSValueRef value(CYCastJSValue(context, *reinterpret_cast<Type_ *>(data))); \ | |
884 | JSObjectRef typed(_jsccall(JSObjectCallAsConstructor, context, CYGetCachedObject(context, CYJSString("Number")), 1, &value)); \ | |
885 | CYSetProperty(context, typed, cyt__s, CYMakeType(context, *this), kJSPropertyAttributeDontEnum); \ | |
886 | return typed; \ | |
887 | } | |
888 | ||
889 | #define CYFromFFI_2(Type_) \ | |
890 | template <> \ | |
891 | JSValueRef Primitive<Type_>::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { \ | |
892 | return CYCastJSValue(context, *reinterpret_cast<Type_ *>(data)); \ | |
893 | } | |
894 | ||
895 | CYFromFFI_(wchar_t) | |
896 | CYFromFFI_(float) | |
897 | CYFromFFI_(double) | |
898 | CYFromFFI_(long double) | |
899 | ||
900 | CYFromFFI_2(signed char) | |
901 | CYFromFFI_2(signed int) | |
902 | CYFromFFI_(signed long int) | |
903 | CYFromFFI_(signed long long int) | |
904 | CYFromFFI_2(signed short int) | |
905 | ||
906 | CYFromFFI_2(unsigned char) | |
907 | CYFromFFI_2(unsigned int) | |
908 | CYFromFFI_(unsigned long int) | |
909 | CYFromFFI_(unsigned long long int) | |
910 | CYFromFFI_2(unsigned short int) | |
911 | ||
912 | #ifdef __SIZEOF_INT128__ | |
913 | CYFromFFI_(signed __int128) | |
914 | CYFromFFI_(unsigned __int128) | |
915 | #endif | |
916 | ||
917 | template <> | |
918 | JSValueRef Primitive<bool>::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { | |
919 | return CYCastJSValue(context, *reinterpret_cast<bool *>(data)); | |
920 | } | |
921 | ||
922 | template <> | |
923 | JSValueRef Primitive<char>::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { | |
924 | uint16_t string(uint8_t(*reinterpret_cast<char *>(data))); | |
925 | JSValueRef value(CYCastJSValue(context, CYJSString(CYUTF16String(&string, 1)))); | |
926 | JSObjectRef typed(_jsccall(JSObjectCallAsConstructor, context, CYGetCachedObject(context, CYJSString("String")), 1, &value)); | |
927 | CYSetProperty(context, typed, cyt_s, CYMakeType(context, sig::Primitive<char>()), kJSPropertyAttributeDontEnum); | |
928 | CYSetPrototype(context, typed, CYGetCachedValue(context, CYJSString("Character_prototype"))); | |
929 | return typed; | |
930 | } | |
931 | ||
932 | JSValueRef Void::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { | |
933 | return CYJSUndefined(context); | |
934 | } | |
935 | ||
936 | JSValueRef Unknown::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { | |
937 | _assert(false); | |
938 | } | |
939 | ||
940 | JSValueRef String::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { | |
941 | if (char *value = *reinterpret_cast<char **>(data)) | |
942 | return CYPrivate<CString>::Make(context, value, context, owner); | |
943 | return CYJSNull(context); | |
944 | } | |
945 | ||
946 | JSValueRef Bits::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { | |
947 | _assert(false); | |
948 | } | |
949 | ||
950 | JSValueRef Pointer::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { | |
951 | if (void *value = *reinterpret_cast<void **>(data)) | |
952 | return CYMakePointer(context, value, type, NULL, owner); | |
953 | return CYJSNull(context); | |
954 | } | |
955 | ||
956 | JSValueRef Array::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { | |
957 | return CYPrivate<CArray>::Make(context, data, size, type, ffi->elements[0], context, owner); | |
958 | } | |
959 | ||
960 | JSValueRef Enum::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { | |
961 | return type.FromFFI(context, ffi, data, initialize, owner); | |
962 | } | |
963 | ||
964 | JSValueRef Aggregate::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { | |
965 | _assert(!overlap); | |
966 | _assert(signature.count != _not(size_t)); | |
967 | return CYPrivate<Struct_privateData>::Make(context, data, *this, ffi, context, owner); | |
968 | } | |
969 | ||
970 | JSValueRef Function::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { | |
971 | return CYMakeFunctor(context, reinterpret_cast<void (*)()>(data), variadic, signature); | |
972 | } | |
973 | ||
974 | } | |
975 | ||
976 | void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg) { | |
977 | Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg)); | |
978 | ||
979 | JSContextRef context(internal->function_); | |
980 | ||
981 | size_t count(internal->cif_.nargs); | |
982 | JSValueRef values[count]; | |
983 | ||
984 | for (size_t index(0); index != count; ++index) | |
985 | values[index] = internal->signature_.elements[1 + index].type->FromFFI(context, internal->cif_.arg_types[index], arguments[index]); | |
986 | ||
987 | JSValueRef value(internal->adapter_(context, count, values, internal->function_)); | |
988 | internal->signature_.elements[0].type->PoolFFI(NULL, context, internal->cif_.rtype, result, value); | |
989 | } | |
990 | ||
991 | static JSValueRef FunctionAdapter_(JSContextRef context, size_t count, JSValueRef values[], JSObjectRef function) { | |
992 | return CYCallAsFunction(context, function, NULL, count, values); | |
993 | } | |
994 | ||
995 | #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__)) | |
996 | static void CYFreeFunctor(void *data) { | |
997 | ffi_closure_free(data); | |
998 | } | |
999 | #else | |
1000 | static void CYFreeFunctor(void *data) { | |
1001 | _syscall(munmap(data, sizeof(ffi_closure))); | |
1002 | } | |
1003 | #endif | |
1004 | ||
1005 | Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)) { | |
1006 | // XXX: in case of exceptions this will leak | |
1007 | Closure_privateData *internal(new Closure_privateData(context, function, adapter, signature)); | |
1008 | ||
1009 | #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__)) | |
1010 | void *executable; | |
1011 | ffi_closure *writable(reinterpret_cast<ffi_closure *>(ffi_closure_alloc(sizeof(ffi_closure), &executable))); | |
1012 | ||
1013 | ffi_status status(ffi_prep_closure_loc(writable, &internal->cif_, &CYExecuteClosure, internal, executable)); | |
1014 | _assert(status == FFI_OK); | |
1015 | ||
1016 | internal->pool_->atexit(&CYFreeFunctor, writable); | |
1017 | internal->value_ = reinterpret_cast<void (*)()>(executable); | |
1018 | #else | |
1019 | ffi_closure *closure((ffi_closure *) _syscall(mmap( | |
1020 | NULL, sizeof(ffi_closure), | |
1021 | PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, | |
1022 | -1, 0 | |
1023 | ))); | |
1024 | ||
1025 | ffi_status status(ffi_prep_closure(closure, &internal->cif_, &CYExecuteClosure, internal)); | |
1026 | _assert(status == FFI_OK); | |
1027 | ||
1028 | _syscall(mprotect(closure, sizeof(*closure), PROT_READ | PROT_EXEC)); | |
1029 | ||
1030 | internal->pool_->atexit(&CYFreeFunctor, closure); | |
1031 | internal->value_ = reinterpret_cast<void (*)()>(closure); | |
1032 | #endif | |
1033 | ||
1034 | return internal; | |
1035 | } | |
1036 | ||
1037 | static JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const sig::Signature &signature) { | |
1038 | Closure_privateData *internal(CYMakeFunctor_(context, function, signature, &FunctionAdapter_)); | |
1039 | JSObjectRef object(JSObjectMake(context, cy::Functor::Class_, internal)); | |
1040 | // XXX: see above notes about needing to leak | |
1041 | JSValueProtect(CYGetJSContext(context), object); | |
1042 | return object; | |
1043 | } | |
1044 | ||
1045 | JSValueRef CYGetCachedValue(JSContextRef context, JSStringRef name) { | |
1046 | return CYGetProperty(context, CYCastJSObject(context, CYGetProperty(context, CYGetGlobalObject(context), cy_s)), name); | |
1047 | } | |
1048 | ||
1049 | JSObjectRef CYGetCachedObject(JSContextRef context, JSStringRef name) { | |
1050 | return CYCastJSObject(context, CYGetCachedValue(context, name)); | |
1051 | } | |
1052 | ||
1053 | static JSValueRef CYMakeFunctor(JSContextRef context, JSValueRef value, bool variadic, const sig::Signature &signature) { | |
1054 | JSObjectRef Function(CYGetCachedObject(context, CYJSString("Function"))); | |
1055 | ||
1056 | bool function(_jsccall(JSValueIsInstanceOfConstructor, context, value, Function)); | |
1057 | if (function) { | |
1058 | JSObjectRef function(CYCastJSObject(context, value)); | |
1059 | return CYMakeFunctor(context, function, signature); | |
1060 | } else { | |
1061 | void (*function)()(CYCastPointer<void (*)()>(context, value)); | |
1062 | return CYMakeFunctor(context, function, variadic, signature); | |
1063 | } | |
1064 | } | |
1065 | ||
1066 | static JSValueRef CString_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1067 | CYPool pool; | |
1068 | CString *internal(reinterpret_cast<CString *>(JSObjectGetPrivate(object))); | |
1069 | ||
1070 | ssize_t offset; | |
1071 | if (JSStringIsEqualToUTF8CString(property, "$cyi")) | |
1072 | offset = 0; | |
1073 | else if (!CYGetOffset(pool, context, property, offset)) | |
1074 | return NULL; | |
1075 | ||
1076 | sig::Primitive<char> type; | |
1077 | return type.FromFFI(context, type.GetFFI(pool), internal->value_ + offset, false, NULL); | |
1078 | } CYCatch(NULL) } | |
1079 | ||
1080 | static bool CString_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { | |
1081 | CYPool pool; | |
1082 | CString *internal(reinterpret_cast<CString *>(JSObjectGetPrivate(object))); | |
1083 | ||
1084 | ssize_t offset; | |
1085 | if (JSStringIsEqualToUTF8CString(property, "$cyi")) | |
1086 | offset = 0; | |
1087 | else if (!CYGetOffset(pool, context, property, offset)) | |
1088 | return false; | |
1089 | ||
1090 | sig::Primitive<char> type; | |
1091 | type.PoolFFI(NULL, context, type.GetFFI(pool), internal->value_ + offset, value); | |
1092 | return true; | |
1093 | } CYCatch(false) } | |
1094 | ||
1095 | static bool Index_(CYPool &pool, JSContextRef context, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) { | |
1096 | Type_privateData *typical(internal->type_); | |
1097 | sig::Aggregate *type(static_cast<sig::Aggregate *>(typical->type_)); | |
1098 | if (type == NULL) | |
1099 | return false; | |
1100 | ||
1101 | const char *name(CYPoolCString(pool, context, property)); | |
1102 | size_t length(strlen(name)); | |
1103 | double number(CYCastDouble(name, length)); | |
1104 | ||
1105 | size_t count(type->signature.count); | |
1106 | ||
1107 | if (std::isnan(number)) { | |
1108 | if (property == NULL) | |
1109 | return false; | |
1110 | ||
1111 | sig::Element *elements(type->signature.elements); | |
1112 | ||
1113 | for (size_t local(0); local != count; ++local) { | |
1114 | sig::Element *element(&elements[local]); | |
1115 | if (element->name != NULL && strcmp(name, element->name) == 0) { | |
1116 | index = local; | |
1117 | goto base; | |
1118 | } | |
1119 | } | |
1120 | ||
1121 | return false; | |
1122 | } else { | |
1123 | index = static_cast<ssize_t>(number); | |
1124 | if (index != number || index < 0 || static_cast<size_t>(index) >= count) | |
1125 | return false; | |
1126 | } | |
1127 | ||
1128 | base: | |
1129 | ffi_type **elements(typical->GetFFI()->elements); | |
1130 | ||
1131 | size_t offset(0); | |
1132 | for (ssize_t local(0); local != index; ++local) { | |
1133 | offset += elements[local]->size; | |
1134 | CYAlign(offset, elements[local + 1]->alignment); | |
1135 | } | |
1136 | ||
1137 | base = reinterpret_cast<uint8_t *>(internal->value_) + offset; | |
1138 | return true; | |
1139 | } | |
1140 | ||
1141 | static void *Offset_(CYPool &pool, JSContextRef context, JSStringRef property, void *data, ffi_type *ffi) { | |
1142 | ssize_t offset; | |
1143 | if (JSStringIsEqualToUTF8CString(property, "$cyi")) | |
1144 | offset = 0; | |
1145 | else if (!CYGetOffset(pool, context, property, offset)) | |
1146 | return NULL; | |
1147 | return reinterpret_cast<uint8_t *>(data) + ffi->size * offset; | |
1148 | } | |
1149 | ||
1150 | static JSValueRef Offset_getProperty(CYPool &pool, JSContextRef context, JSStringRef property, void *data, Type_privateData *typical, JSObjectRef owner) { | |
1151 | ffi_type *ffi(typical->GetFFI()); | |
1152 | void *base(Offset_(pool, context, property, data, ffi)); | |
1153 | if (base == NULL) | |
1154 | return NULL; | |
1155 | return typical->type_->FromFFI(context, ffi, base, false, owner); | |
1156 | } | |
1157 | ||
1158 | static bool Offset_setProperty(CYPool &pool, JSContextRef context, JSStringRef property, void *data, Type_privateData *typical, JSValueRef value) { | |
1159 | ffi_type *ffi(typical->GetFFI()); | |
1160 | void *base(Offset_(pool, context, property, data, ffi)); | |
1161 | if (base == NULL) | |
1162 | return false; | |
1163 | ||
1164 | typical->type_->PoolFFI(NULL, context, ffi, base, value); | |
1165 | return true; | |
1166 | } | |
1167 | ||
1168 | static JSValueRef CArray_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1169 | CYPool pool; | |
1170 | CArray *internal(reinterpret_cast<CArray *>(JSObjectGetPrivate(object))); | |
1171 | if (JSStringIsEqual(property, length_s)) | |
1172 | return CYCastJSValue(context, internal->length_); | |
1173 | Type_privateData *typical(internal->type_); | |
1174 | JSObjectRef owner(internal->owner_ ?: object); | |
1175 | return Offset_getProperty(pool, context, property, internal->value_, typical, owner); | |
1176 | } CYCatch(NULL) } | |
1177 | ||
1178 | static bool CArray_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { | |
1179 | CYPool pool; | |
1180 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
1181 | Type_privateData *typical(internal->type_); | |
1182 | return Offset_setProperty(pool, context, property, internal->value_, typical, value); | |
1183 | } CYCatch(false) } | |
1184 | ||
1185 | static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1186 | CYPool pool; | |
1187 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
1188 | ||
1189 | Type_privateData *typical(internal->type_); | |
1190 | ||
1191 | if (sig::Function *function = dynamic_cast<sig::Function *>(typical->type_)) { | |
1192 | if (!JSStringIsEqualToUTF8CString(property, "$cyi")) | |
1193 | return NULL; | |
1194 | return CYMakeFunctor(context, reinterpret_cast<void (*)()>(internal->value_), function->variadic, function->signature); | |
1195 | } | |
1196 | ||
1197 | JSObjectRef owner(internal->owner_ ?: object); | |
1198 | return Offset_getProperty(pool, context, property, internal->value_, typical, owner); | |
1199 | } CYCatch(NULL) } | |
1200 | ||
1201 | static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { | |
1202 | CYPool pool; | |
1203 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
1204 | Type_privateData *typical(internal->type_); | |
1205 | return Offset_setProperty(pool, context, property, internal->value_, typical, value); | |
1206 | } CYCatch(false) } | |
1207 | ||
1208 | static JSValueRef Struct_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1209 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(_this))); | |
1210 | Type_privateData *typical(internal->type_); | |
1211 | return CYMakePointer(context, internal->value_, *typical->type_, typical->ffi_, _this); | |
1212 | } CYCatch(NULL) } | |
1213 | ||
1214 | static JSValueRef Struct_getProperty_$cyt(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1215 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
1216 | return CYMakeType(context, *internal->type_->type_); | |
1217 | } CYCatch(NULL) } | |
1218 | ||
1219 | static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1220 | CYPool pool; | |
1221 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
1222 | Type_privateData *typical(internal->type_); | |
1223 | sig::Aggregate *type(static_cast<sig::Aggregate *>(typical->type_)); | |
1224 | ||
1225 | ssize_t index; | |
1226 | uint8_t *base; | |
1227 | ||
1228 | if (!Index_(pool, context, internal, property, index, base)) | |
1229 | return NULL; | |
1230 | ||
1231 | JSObjectRef owner(internal->owner_ ?: object); | |
1232 | ||
1233 | return type->signature.elements[index].type->FromFFI(context, typical->GetFFI()->elements[index], base, false, owner); | |
1234 | } CYCatch(NULL) } | |
1235 | ||
1236 | static bool Struct_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { | |
1237 | CYPool pool; | |
1238 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
1239 | Type_privateData *typical(internal->type_); | |
1240 | sig::Aggregate *type(static_cast<sig::Aggregate *>(typical->type_)); | |
1241 | ||
1242 | ssize_t index; | |
1243 | uint8_t *base; | |
1244 | ||
1245 | if (!Index_(pool, context, internal, property, index, base)) | |
1246 | return false; | |
1247 | ||
1248 | type->signature.elements[index].type->PoolFFI(NULL, context, typical->GetFFI()->elements[index], base, value); | |
1249 | return true; | |
1250 | } CYCatch(false) } | |
1251 | ||
1252 | static void Struct_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
1253 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
1254 | Type_privateData *typical(internal->type_); | |
1255 | sig::Aggregate *type(static_cast<sig::Aggregate *>(typical->type_)); | |
1256 | ||
1257 | if (type == NULL) | |
1258 | return; | |
1259 | ||
1260 | size_t count(type->signature.count); | |
1261 | sig::Element *elements(type->signature.elements); | |
1262 | ||
1263 | char number[32]; | |
1264 | ||
1265 | for (size_t index(0); index != count; ++index) { | |
1266 | const char *name; | |
1267 | name = elements[index].name; | |
1268 | ||
1269 | if (name == NULL) { | |
1270 | sprintf(number, "%zu", index); | |
1271 | name = number; | |
1272 | } | |
1273 | ||
1274 | JSPropertyNameAccumulatorAddName(names, CYJSString(name)); | |
1275 | } | |
1276 | } | |
1277 | ||
1278 | static sig::Void Void_; | |
1279 | static sig::Pointer PointerToVoid_(Void_); | |
1280 | ||
1281 | static sig::Type *CYGetType(CYPool &pool, JSContextRef context, JSValueRef value) { | |
1282 | if (JSValueIsNull(context, value)) | |
1283 | return &PointerToVoid_; | |
1284 | JSObjectRef object(CYCastJSObject(context, value)); | |
1285 | JSValueRef check(CYGetProperty(context, object, cyt_s)); | |
1286 | if (JSValueIsUndefined(context, check)) | |
1287 | CYThrow("could not infer type of argument '%s'", CYPoolCString(pool, context, value)); | |
1288 | JSObjectRef type(CYCastJSObject(context, check)); | |
1289 | _assert(JSValueIsObjectOfClass(context, type, CYPrivate<Type_privateData>::Class_)); | |
1290 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(type))); | |
1291 | return internal->type_; | |
1292 | } | |
1293 | ||
1294 | void CYCallFunction(CYPool &pool, JSContextRef context, ffi_cif *cif, void (*function)(), void *value, void **values) { | |
1295 | ffi_call(cif, function, value, values); | |
1296 | } | |
1297 | ||
1298 | JSValueRef CYCallFunction(CYPool &pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, bool variadic, const sig::Signature &signature, ffi_cif *cif, void (*function)()) { | |
1299 | size_t have(setups + count); | |
1300 | size_t need(signature.count - 1); | |
1301 | ||
1302 | if (have < need) | |
1303 | throw CYJSError(context, "insufficient number of arguments to ffi function"); | |
1304 | ||
1305 | ffi_cif corrected; | |
1306 | sig::Element *elements(signature.elements); | |
1307 | ||
1308 | if (have > need) { | |
1309 | if (!variadic) | |
1310 | throw CYJSError(context, "exorbitant number of arguments to ffi function"); | |
1311 | ||
1312 | elements = new (pool) sig::Element[have + 1]; | |
1313 | memcpy(elements, signature.elements, sizeof(sig::Element) * (need + 1)); | |
1314 | ||
1315 | for (size_t index(need); index != have; ++index) { | |
1316 | sig::Element &element(elements[index + 1]); | |
1317 | element.name = NULL; | |
1318 | element.offset = _not(size_t); | |
1319 | element.type = CYGetType(pool, context, arguments[index - setups]); | |
1320 | } | |
1321 | ||
1322 | sig::Signature extended; | |
1323 | extended.elements = elements; | |
1324 | extended.count = have + 1; | |
1325 | sig::sig_ffi_cif(pool, signature.count, extended, &corrected); | |
1326 | cif = &corrected; | |
1327 | } | |
1328 | ||
1329 | void *values[have]; | |
1330 | memcpy(values, setup, sizeof(void *) * setups); | |
1331 | ||
1332 | for (size_t index(setups); index != have; ++index) { | |
1333 | sig::Element &element(elements[index + 1]); | |
1334 | ffi_type *ffi(cif->arg_types[index]); | |
1335 | values[index] = pool.malloc<uint8_t>(ffi->size, ffi->alignment); | |
1336 | element.type->PoolFFI(&pool, context, ffi, values[index], arguments[index - setups]); | |
1337 | } | |
1338 | ||
1339 | CYBuffer buffer(context); | |
1340 | uint8_t *value(buffer->malloc<uint8_t>(std::max<size_t>(cif->rtype->size, sizeof(ffi_arg)), std::max<size_t>(cif->rtype->alignment, alignof(ffi_arg)))); | |
1341 | ||
1342 | void (*call)(CYPool &, JSContextRef, ffi_cif *, void (*)(), void *, void **) = &CYCallFunction; | |
1343 | // XXX: this only supports one hook, but it is a bad idea anyway | |
1344 | for (CYHook *hook : GetHooks()) | |
1345 | if (hook->CallFunction != NULL) | |
1346 | call = hook->CallFunction; | |
1347 | ||
1348 | call(pool, context, cif, function, value, values); | |
1349 | return signature.elements[0].type->FromFFI(context, cif->rtype, value, initialize, buffer); | |
1350 | } | |
1351 | ||
1352 | static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1353 | CYPool pool; | |
1354 | cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(object))); | |
1355 | return CYCallFunction(pool, context, 0, NULL, count, arguments, false, internal->variadic_, internal->signature_, &internal->cif_, internal->value_); | |
1356 | } CYCatch(NULL) } | |
1357 | ||
1358 | static JSValueRef Pointer_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1359 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
1360 | if (dynamic_cast<sig::Function *>(internal->type_->type_) == NULL) | |
1361 | throw CYJSError(context, "cannot call a pointer to non-function"); | |
1362 | JSObjectRef functor(CYCastJSObject(context, CYGetProperty(context, object, cyi_s))); | |
1363 | return CYCallAsFunction(context, functor, _this, count, arguments); | |
1364 | } CYCatch(NULL) } | |
1365 | ||
1366 | JSObjectRef CYMakeType(JSContextRef context, const sig::Type &type) { | |
1367 | return CYPrivate<Type_privateData>::Make(context, type); | |
1368 | } | |
1369 | ||
1370 | extern "C" bool CYBridgeHash(CYPool &pool, CYUTF8String name, const char *&code, unsigned &flags) { | |
1371 | sqlite3_stmt *statement; | |
1372 | ||
1373 | _sqlcall(sqlite3_prepare(database_, | |
1374 | "select " | |
1375 | "\"cache\".\"code\", " | |
1376 | "\"cache\".\"flags\" " | |
1377 | "from \"cache\" " | |
1378 | "where" | |
1379 | " \"cache\".\"system\" & " CY_SYSTEM " == " CY_SYSTEM " and" | |
1380 | " \"cache\".\"name\" = ?" | |
1381 | " limit 1" | |
1382 | , -1, &statement, NULL)); | |
1383 | ||
1384 | _sqlcall(sqlite3_bind_text(statement, 1, name.data, name.size, SQLITE_STATIC)); | |
1385 | ||
1386 | bool success; | |
1387 | if (_sqlcall(sqlite3_step(statement)) == SQLITE_DONE) | |
1388 | success = false; | |
1389 | else { | |
1390 | success = true; | |
1391 | code = sqlite3_column_pooled(pool, statement, 0); | |
1392 | flags = sqlite3_column_int(statement, 1); | |
1393 | } | |
1394 | ||
1395 | _sqlcall(sqlite3_finalize(statement)); | |
1396 | return success; | |
1397 | } | |
1398 | ||
1399 | static bool All_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { | |
1400 | if (JSStringIsEqualToUTF8CString(property, "errno")) | |
1401 | return true; | |
1402 | ||
1403 | JSObjectRef global(CYGetGlobalObject(context)); | |
1404 | JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript")))); | |
1405 | JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls")))); | |
1406 | ||
1407 | for (size_t i(0), count(CYArrayLength(context, alls)); i != count; ++i) | |
1408 | if (JSObjectRef space = CYCastJSObject(context, CYArrayGet(context, alls, count - i - 1))) | |
1409 | if (CYHasProperty(context, space, property)) | |
1410 | return true; | |
1411 | ||
1412 | CYPool pool; | |
1413 | const char *code; | |
1414 | unsigned flags; | |
1415 | if (CYBridgeHash(pool, CYPoolUTF8String(pool, context, property), code, flags)) | |
1416 | return true; | |
1417 | ||
1418 | return false; | |
1419 | } | |
1420 | ||
1421 | static JSValueRef All_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1422 | if (JSStringIsEqualToUTF8CString(property, "errno")) | |
1423 | return CYCastJSValue(context, errno); | |
1424 | ||
1425 | JSObjectRef global(CYGetGlobalObject(context)); | |
1426 | JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript")))); | |
1427 | JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls")))); | |
1428 | ||
1429 | for (size_t i(0), count(CYArrayLength(context, alls)); i != count; ++i) | |
1430 | if (JSObjectRef space = CYCastJSObject(context, CYArrayGet(context, alls, count - i - 1))) | |
1431 | if (JSValueRef value = CYGetProperty(context, space, property)) | |
1432 | if (!JSValueIsUndefined(context, value)) | |
1433 | return value; | |
1434 | ||
1435 | CYPool pool; | |
1436 | const char *code; | |
1437 | unsigned flags; | |
1438 | if (CYBridgeHash(pool, CYPoolUTF8String(pool, context, property), code, flags)) { | |
1439 | CYUTF8String parsed; | |
1440 | ||
1441 | try { | |
1442 | parsed = CYPoolCode(pool, code); | |
1443 | } catch (const CYException &error) { | |
1444 | CYThrow("%s", pool.strcat("error caching ", CYPoolCString(pool, context, property), ": ", error.PoolCString(pool), NULL)); | |
1445 | } | |
1446 | ||
1447 | JSObjectRef cache(CYGetCachedObject(context, CYJSString("cache"))); | |
1448 | ||
1449 | JSObjectRef stub; | |
1450 | if (flags == CYBridgeType) { | |
1451 | stub = CYMakeType(context, sig::Void()); | |
1452 | CYSetProperty(context, cache, property, stub); | |
1453 | } else | |
1454 | stub = NULL; | |
1455 | ||
1456 | JSValueRef value(_jsccall(JSEvaluateScript, context, CYJSString(parsed), NULL, NULL, 0)); | |
1457 | ||
1458 | switch (flags) { | |
1459 | case CYBridgeVoid: { | |
1460 | } break; | |
1461 | ||
1462 | case CYBridgeHold: { | |
1463 | CYSetProperty(context, cache, property, value); | |
1464 | } break; | |
1465 | ||
1466 | case CYBridgeType: { | |
1467 | JSObjectRef swap(CYCastJSObject(context, value)); | |
1468 | void *source(JSObjectGetPrivate(swap)); | |
1469 | _assert(source != NULL); | |
1470 | void *target(JSObjectGetPrivate(stub)); | |
1471 | _assert(JSObjectSetPrivate(swap, target)); | |
1472 | _assert(JSObjectSetPrivate(stub, source)); | |
1473 | value = stub; | |
1474 | } break; | |
1475 | } | |
1476 | ||
1477 | return value; | |
1478 | } | |
1479 | ||
1480 | return NULL; | |
1481 | } CYCatch(NULL) } | |
1482 | ||
1483 | static JSValueRef All_complete_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1484 | _assert(count == 1 || count == 2); | |
1485 | CYPool pool; | |
1486 | CYUTF8String prefix(CYPoolUTF8String(pool, context, CYJSString(context, arguments[0]))); | |
1487 | ||
1488 | JSObjectRef array(NULL); | |
1489 | ||
1490 | { | |
1491 | CYArrayBuilder<1024> values(context, array); | |
1492 | ||
1493 | sqlite3_stmt *statement; | |
1494 | ||
1495 | if (prefix.size == 0) | |
1496 | _sqlcall(sqlite3_prepare(database_, | |
1497 | "select " | |
1498 | "\"cache\".\"name\" " | |
1499 | "from \"cache\" " | |
1500 | "where" | |
1501 | " \"cache\".\"system\" & " CY_SYSTEM " == " CY_SYSTEM | |
1502 | , -1, &statement, NULL)); | |
1503 | else { | |
1504 | _sqlcall(sqlite3_prepare(database_, | |
1505 | "select " | |
1506 | "\"cache\".\"name\" " | |
1507 | "from \"cache\" " | |
1508 | "where" | |
1509 | " \"cache\".\"name\" >= ? and \"cache\".\"name\" < ? and " | |
1510 | " \"cache\".\"system\" & " CY_SYSTEM " == " CY_SYSTEM | |
1511 | , -1, &statement, NULL)); | |
1512 | ||
1513 | _sqlcall(sqlite3_bind_text(statement, 1, prefix.data, prefix.size, SQLITE_STATIC)); | |
1514 | ||
1515 | char *after(pool.strndup(prefix.data, prefix.size)); | |
1516 | ++after[prefix.size - 1]; | |
1517 | _sqlcall(sqlite3_bind_text(statement, 2, after, prefix.size, SQLITE_STATIC)); | |
1518 | } | |
1519 | ||
1520 | while (_sqlcall(sqlite3_step(statement)) != SQLITE_DONE) | |
1521 | values(CYCastJSValue(context, CYJSString(sqlite3_column_string(statement, 0)))); | |
1522 | ||
1523 | _sqlcall(sqlite3_finalize(statement)); | |
1524 | } | |
1525 | ||
1526 | return array; | |
1527 | } CYCatch(NULL) } | |
1528 | ||
1529 | static void All_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
1530 | JSObjectRef global(CYGetGlobalObject(context)); | |
1531 | JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript")))); | |
1532 | JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls")))); | |
1533 | ||
1534 | for (size_t i(0), count(CYArrayLength(context, alls)); i != count; ++i) | |
1535 | if (JSObjectRef space = CYCastJSObject(context, CYArrayGet(context, alls, count - i - 1))) { | |
1536 | JSPropertyNameArrayRef subset(JSObjectCopyPropertyNames(context, space)); | |
1537 | for (size_t index(0), count(JSPropertyNameArrayGetCount(subset)); index != count; ++index) | |
1538 | JSPropertyNameAccumulatorAddName(names, JSPropertyNameArrayGetNameAtIndex(subset, index)); | |
1539 | JSPropertyNameArrayRelease(subset); | |
1540 | } | |
1541 | } | |
1542 | ||
1543 | static JSObjectRef CArray_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1544 | _assert(false); | |
1545 | } CYCatchObject() } | |
1546 | ||
1547 | static JSObjectRef CString_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1548 | _assert(false); | |
1549 | } CYCatchObject() } | |
1550 | ||
1551 | static JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1552 | _assert(false); | |
1553 | } CYCatchObject() } | |
1554 | ||
1555 | static JSObjectRef Type_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1556 | CYPool pool; | |
1557 | ||
1558 | if (false) { | |
1559 | } else if (count == 1) { | |
1560 | switch (JSValueGetType(context, arguments[0])) { | |
1561 | case kJSTypeString: { | |
1562 | const char *encoding(CYPoolCString(pool, context, arguments[0])); | |
1563 | sig::Signature signature; | |
1564 | sig::Parse(pool, &signature, encoding, &Structor_); | |
1565 | return CYMakeType(context, *signature.elements[0].type); | |
1566 | } break; | |
1567 | ||
1568 | case kJSTypeObject: { | |
1569 | // XXX: accept a set of enum constants and /guess/ at their size | |
1570 | _assert(false); | |
1571 | } break; | |
1572 | ||
1573 | default: | |
1574 | throw CYJSError(context, "incorrect kind of argument to new Type"); | |
1575 | } | |
1576 | } else if (count == 2) { | |
1577 | JSObjectRef types(CYCastJSObject(context, arguments[0])); | |
1578 | size_t count(CYArrayLength(context, types)); | |
1579 | ||
1580 | JSObjectRef names(CYCastJSObject(context, arguments[1])); | |
1581 | ||
1582 | sig::Aggregate type(false); | |
1583 | type.signature.elements = new(pool) sig::Element[count]; | |
1584 | type.signature.count = count; | |
1585 | ||
1586 | for (size_t i(0); i != count; ++i) { | |
1587 | sig::Element &element(type.signature.elements[i]); | |
1588 | element.offset = _not(size_t); | |
1589 | ||
1590 | JSValueRef name(CYArrayGet(context, names, i)); | |
1591 | if (JSValueIsUndefined(context, name)) | |
1592 | element.name = NULL; | |
1593 | else | |
1594 | element.name = CYPoolCString(pool, context, name); | |
1595 | ||
1596 | JSObjectRef object(CYCastJSObject(context, CYArrayGet(context, types, i))); | |
1597 | _assert(JSValueIsObjectOfClass(context, object, CYPrivate<Type_privateData>::Class_)); | |
1598 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); | |
1599 | element.type = internal->type_; | |
1600 | _assert(element.type != NULL); | |
1601 | } | |
1602 | ||
1603 | return CYMakeType(context, type); | |
1604 | } else { | |
1605 | throw CYJSError(context, "incorrect number of arguments to Type constructor"); | |
1606 | } | |
1607 | } CYCatchObject() } | |
1608 | ||
1609 | static JSValueRef Type_callAsFunction_$With(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], sig::Callable &type, JSValueRef *exception) { CYTry { | |
1610 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1611 | ||
1612 | CYPool pool; | |
1613 | ||
1614 | type.signature.elements = new(pool) sig::Element[1 + count]; | |
1615 | type.signature.count = 1 + count; | |
1616 | ||
1617 | type.signature.elements[0].name = NULL; | |
1618 | type.signature.elements[0].type = internal->type_; | |
1619 | type.signature.elements[0].offset = _not(size_t); | |
1620 | ||
1621 | for (size_t i(0); i != count; ++i) { | |
1622 | sig::Element &element(type.signature.elements[i + 1]); | |
1623 | element.name = NULL; | |
1624 | element.offset = _not(size_t); | |
1625 | ||
1626 | JSObjectRef object(CYCastJSObject(context, arguments[i])); | |
1627 | _assert(JSValueIsObjectOfClass(context, object, CYPrivate<Type_privateData>::Class_)); | |
1628 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); | |
1629 | ||
1630 | element.type = internal->type_; | |
1631 | } | |
1632 | ||
1633 | return CYMakeType(context, type); | |
1634 | } CYCatch(NULL) } | |
1635 | ||
1636 | static JSValueRef Type_callAsFunction_arrayOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1637 | if (count != 1) | |
1638 | throw CYJSError(context, "incorrect number of arguments to Type.arrayOf"); | |
1639 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1640 | ||
1641 | CYPool pool; | |
1642 | size_t index(CYGetIndex(pool, context, CYJSString(context, arguments[0]))); | |
1643 | if (index == _not(size_t)) | |
1644 | throw CYJSError(context, "invalid array size used with Type.arrayOf"); | |
1645 | ||
1646 | sig::Array type(*internal->type_, index); | |
1647 | return CYMakeType(context, type); | |
1648 | } CYCatch(NULL) } | |
1649 | ||
1650 | static JSValueRef Type_callAsFunction_blockWith(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
1651 | #ifdef CY_OBJECTIVEC | |
1652 | sig::Block type; | |
1653 | return Type_callAsFunction_$With(context, object, _this, count, arguments, type, exception); | |
1654 | #else | |
1655 | _assert(false); | |
1656 | #endif | |
1657 | } | |
1658 | ||
1659 | static JSValueRef Type_callAsFunction_constant(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1660 | if (count != 0) | |
1661 | throw CYJSError(context, "incorrect number of arguments to Type.constant"); | |
1662 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1663 | ||
1664 | CYPool pool; | |
1665 | sig::Type *type(internal->type_->Copy(pool)); | |
1666 | type->flags |= JOC_TYPE_CONST; | |
1667 | return CYMakeType(context, *type); | |
1668 | } CYCatch(NULL) } | |
1669 | ||
1670 | static JSValueRef Type_callAsFunction_enumFor(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1671 | if (count != 1) | |
1672 | throw CYJSError(context, "incorrect number of arguments to Type.enumFor"); | |
1673 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1674 | ||
1675 | CYPool pool; | |
1676 | ||
1677 | JSObjectRef constants(CYCastJSObject(context, arguments[0])); | |
1678 | ||
1679 | // XXX: this is, sadly, going to leak | |
1680 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context, constants)); | |
1681 | ||
1682 | size_t count(JSPropertyNameArrayGetCount(names)); | |
1683 | ||
1684 | sig::Enum type(*internal->type_, count); | |
1685 | type.constants = new(pool) sig::Constant[count]; | |
1686 | ||
1687 | for (size_t index(0); index != count; ++index) { | |
1688 | JSStringRef name(JSPropertyNameArrayGetNameAtIndex(names, index)); | |
1689 | JSValueRef value(CYGetProperty(context, constants, name)); | |
1690 | _assert(JSValueGetType(context, value) == kJSTypeNumber); | |
1691 | CYUTF8String string(CYPoolUTF8String(pool, context, name)); | |
1692 | type.constants[index].name = string.data; | |
1693 | type.constants[index].value = CYCastDouble(context, value); | |
1694 | } | |
1695 | ||
1696 | JSPropertyNameArrayRelease(names); | |
1697 | ||
1698 | return CYMakeType(context, type); | |
1699 | } CYCatch(NULL) } | |
1700 | ||
1701 | static JSValueRef Type_callAsFunction_functionWith(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
1702 | bool variadic(count != 0 && JSValueIsNull(context, arguments[count - 1])); | |
1703 | sig::Function type(variadic); | |
1704 | return Type_callAsFunction_$With(context, object, _this, variadic ? count - 1 : count, arguments, type, exception); | |
1705 | } | |
1706 | ||
1707 | static JSValueRef Type_callAsFunction_pointerTo(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1708 | if (count != 0) | |
1709 | throw CYJSError(context, "incorrect number of arguments to Type.pointerTo"); | |
1710 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1711 | ||
1712 | if (dynamic_cast<sig::Primitive<char> *>(internal->type_) != NULL) | |
1713 | return CYMakeType(context, sig::String((internal->type_->flags & JOC_TYPE_CONST) != 0)); | |
1714 | else | |
1715 | return CYMakeType(context, sig::Pointer(*internal->type_)); | |
1716 | } CYCatch(NULL) } | |
1717 | ||
1718 | static JSValueRef Type_callAsFunction_withName(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1719 | if (count != 1) | |
1720 | throw CYJSError(context, "incorrect number of arguments to Type.withName"); | |
1721 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1722 | ||
1723 | CYPool pool; | |
1724 | return CYMakeType(context, *internal->type_->Copy(pool, CYPoolCString(pool, context, arguments[0]))); | |
1725 | } CYCatch(NULL) } | |
1726 | ||
1727 | static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1728 | if (count != 1) | |
1729 | throw CYJSError(context, "incorrect number of arguments to type cast function"); | |
1730 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); | |
1731 | ||
1732 | if (sig::Function *function = dynamic_cast<sig::Function *>(internal->type_)) | |
1733 | return CYMakeFunctor(context, arguments[0], function->variadic, function->signature); | |
1734 | ||
1735 | CYBuffer buffer(context); | |
1736 | ||
1737 | sig::Type *type(internal->type_); | |
1738 | ffi_type *ffi(internal->GetFFI()); | |
1739 | ||
1740 | void *data; | |
1741 | if (_this == NULL || CYIsStrictEqual(context, _this, CYGetGlobalObject(context))) | |
1742 | data = buffer->malloc<void>(ffi->size, ffi->alignment); | |
1743 | else { | |
1744 | CYSetProperty(context, buffer, CYJSString("$cyo"), _this, kJSPropertyAttributeDontEnum); | |
1745 | data = CYCastPointer<void *>(context, _this); | |
1746 | } | |
1747 | ||
1748 | type->PoolFFI(buffer, context, ffi, data, arguments[0]); | |
1749 | JSValueRef value(type->FromFFI(context, ffi, data, false, buffer)); | |
1750 | return value; | |
1751 | } CYCatch(NULL) } | |
1752 | ||
1753 | static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1754 | if (count > 1) | |
1755 | throw CYJSError(context, "incorrect number of arguments to Type allocator"); | |
1756 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); | |
1757 | ||
1758 | JSObjectRef pointer(CYMakePointer(context, NULL, *internal->type_, NULL, NULL)); | |
1759 | Pointer *value(reinterpret_cast<Pointer *>(JSObjectGetPrivate(pointer))); | |
1760 | ||
1761 | sig::Type *type(internal->type_); | |
1762 | ffi_type *ffi(internal->GetFFI()); | |
1763 | value->value_ = value->pool_->malloc<void>(ffi->size, ffi->alignment); | |
1764 | ||
1765 | if (count == 0) | |
1766 | memset(value->value_, 0, ffi->size); | |
1767 | else | |
1768 | type->PoolFFI(value->pool_, context, ffi, value->value_, arguments[0]); | |
1769 | ||
1770 | return pointer; | |
1771 | } CYCatchObject() } | |
1772 | ||
1773 | // XXX: I don't even think the user should be allowed to do this | |
1774 | static JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1775 | if (count != 2) | |
1776 | throw CYJSError(context, "incorrect number of arguments to Functor constructor"); | |
1777 | CYPool pool; | |
1778 | const char *encoding(CYPoolCString(pool, context, arguments[1])); | |
1779 | sig::Signature signature; | |
1780 | sig::Parse(pool, &signature, encoding, &Structor_); | |
1781 | // XXX: this can try to return null, and I guess then it just fails | |
1782 | return CYCastJSObject(context, CYMakeFunctor(context, arguments[0], false, signature)); | |
1783 | } CYCatchObject() } | |
1784 | ||
1785 | static JSValueRef CArray_callAsFunction_toPointer(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1786 | CArray *internal(reinterpret_cast<CArray *>(JSObjectGetPrivate(_this))); | |
1787 | JSObjectRef owner(internal->owner_ ?: object); | |
1788 | return CYMakePointer(context, internal->value_, *internal->type_->type_, NULL, owner); | |
1789 | } CYCatch(NULL) } | |
1790 | ||
1791 | static JSValueRef CString_callAsFunction_toPointer(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1792 | CString *internal(reinterpret_cast<CString *>(JSObjectGetPrivate(_this))); | |
1793 | JSObjectRef owner(internal->owner_ ?: object); | |
1794 | return CYMakePointer(context, internal->value_, sig::Primitive<char>(), NULL, owner); | |
1795 | } CYCatch(NULL) } | |
1796 | ||
1797 | static JSValueRef Functor_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1798 | CYPool pool; | |
1799 | cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(_this))); | |
1800 | ||
1801 | sig::Function type(internal->variadic_); | |
1802 | sig::Copy(pool, type.signature, internal->signature_); | |
1803 | ||
1804 | return CYMakePointer(context, reinterpret_cast<void *>(internal->value_), type, NULL, NULL); | |
1805 | } CYCatch(NULL) } | |
1806 | ||
1807 | static JSValueRef Pointer_callAsFunction_toPointer(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1808 | return _this; | |
1809 | } CYCatch(NULL) } | |
1810 | ||
1811 | static JSValueRef CArray_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1812 | CArray *internal(reinterpret_cast<CArray *>(JSObjectGetPrivate(_this))); | |
1813 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_)); | |
1814 | } CYCatch(NULL) } | |
1815 | ||
1816 | static JSValueRef Pointer_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1817 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(_this))); | |
1818 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_)); | |
1819 | } CYCatch(NULL) } | |
1820 | ||
1821 | static JSValueRef Functor_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1822 | cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(_this))); | |
1823 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_)); | |
1824 | } CYCatch(NULL) } | |
1825 | ||
1826 | static JSValueRef Functor_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1827 | cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(_this))); | |
1828 | uint8_t *value(reinterpret_cast<uint8_t *>(internal->value_)); | |
1829 | _assert(value != NULL); | |
1830 | ||
1831 | CYLocalPool pool; | |
1832 | ||
1833 | sig::Function function(internal->variadic_); | |
1834 | sig::Copy(pool, function.signature, internal->signature_); | |
1835 | ||
1836 | CYPropertyName *name(internal->GetName(pool)); | |
1837 | auto typed(CYDecodeType(pool, &function)); | |
1838 | ||
1839 | std::ostringstream str; | |
1840 | CYOptions options; | |
1841 | CYOutput output(*str.rdbuf(), options); | |
1842 | output.pretty_ = true; | |
1843 | (new(pool) CYExternalExpression(new(pool) CYString("C"), typed, name))->Output(output, CYNoFlags); | |
1844 | return CYCastJSValue(context, CYJSString(str.str())); | |
1845 | } CYCatch(NULL) } | |
1846 | ||
1847 | CYPropertyName *cy::Functor::GetName(CYPool &pool) const { | |
1848 | Dl_info info; | |
1849 | if (dladdr(reinterpret_cast<void *>(value_), &info) == 0 || strcmp(info.dli_sname, "<redacted>") == 0) | |
1850 | return new(pool) CYNumber(reinterpret_cast<uintptr_t>(value_)); | |
1851 | ||
1852 | std::ostringstream str; | |
1853 | str << info.dli_sname; | |
1854 | off_t offset(reinterpret_cast<uint8_t *>(value_) - reinterpret_cast<uint8_t *>(info.dli_saddr)); | |
1855 | if (offset != 0) | |
1856 | str << "+0x" << std::hex << offset; | |
1857 | return new(pool) CYString(pool.strdup(str.str().c_str())); | |
1858 | } | |
1859 | ||
1860 | static JSValueRef Pointer_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1861 | std::set<void *> *objects(CYCastObjects(context, _this, count, arguments)); | |
1862 | ||
1863 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(_this))); | |
1864 | ||
1865 | try { | |
1866 | JSValueRef value(CYGetProperty(context, _this, cyi_s)); | |
1867 | if (!JSValueIsUndefined(context, value)) { | |
1868 | CYPool pool; | |
1869 | return CYCastJSValue(context, pool.strcat("&", CYPoolCCYON(pool, context, value, objects), NULL)); | |
1870 | } | |
1871 | } catch (const CYException &e) { | |
1872 | // XXX: it might be interesting to include this error | |
1873 | } | |
1874 | ||
1875 | CYLocalPool pool; | |
1876 | std::ostringstream str; | |
1877 | ||
1878 | sig::Pointer type(*internal->type_->type_); | |
1879 | ||
1880 | CYOptions options; | |
1881 | CYOutput output(*str.rdbuf(), options); | |
1882 | (new(pool) CYTypeExpression(CYDecodeType(pool, &type)))->Output(output, CYNoFlags); | |
1883 | ||
1884 | str << "(" << internal->value_ << ")"; | |
1885 | std::string value(str.str()); | |
1886 | return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size()))); | |
1887 | } CYCatch(NULL) } | |
1888 | ||
1889 | static JSValueRef CString_getProperty_length(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1890 | CString *internal(reinterpret_cast<CString *>(JSObjectGetPrivate(object))); | |
1891 | return CYCastJSValue(context, strlen(internal->value_)); | |
1892 | } CYCatch(NULL) } | |
1893 | ||
1894 | static JSValueRef CString_getProperty_$cyt(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1895 | return CYMakeType(context, sig::String(true)); | |
1896 | } CYCatch(NULL) } | |
1897 | ||
1898 | static JSValueRef CArray_getProperty_$cyt(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1899 | CArray *internal(reinterpret_cast<CArray *>(JSObjectGetPrivate(object))); | |
1900 | sig::Array type(*internal->type_->type_, internal->length_); | |
1901 | return CYMakeType(context, type); | |
1902 | } CYCatch(NULL) } | |
1903 | ||
1904 | static JSValueRef Pointer_getProperty_$cyt(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1905 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
1906 | sig::Pointer type(*internal->type_->type_); | |
1907 | return CYMakeType(context, type); | |
1908 | } CYCatch(NULL) } | |
1909 | ||
1910 | static JSValueRef CString_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1911 | CString *internal(reinterpret_cast<CString *>(JSObjectGetPrivate(_this))); | |
1912 | const char *string(internal->value_); | |
1913 | std::ostringstream str; | |
1914 | if (string == NULL) | |
1915 | str << "NULL"; | |
1916 | else { | |
1917 | str << "&"; | |
1918 | CYStringify(str, string, strlen(string), CYStringifyModeNative); | |
1919 | } | |
1920 | std::string value(str.str()); | |
1921 | return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size()))); | |
1922 | } CYCatch(NULL) } | |
1923 | ||
1924 | static JSValueRef CString_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1925 | CString *internal(reinterpret_cast<CString *>(JSObjectGetPrivate(_this))); | |
1926 | return CYCastJSValue(context, internal->value_); | |
1927 | } CYCatch(NULL) } | |
1928 | ||
1929 | static JSValueRef Functor_getProperty_$cyt(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1930 | cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(object))); | |
1931 | CYPool pool; | |
1932 | sig::Function type(internal->variadic_); | |
1933 | sig::Copy(pool, type.signature, internal->signature_); | |
1934 | return CYMakeType(context, type); | |
1935 | } CYCatch(NULL) } | |
1936 | ||
1937 | static JSValueRef Type_getProperty_alignment(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1938 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); | |
1939 | return CYCastJSValue(context, internal->GetFFI()->alignment); | |
1940 | } CYCatch(NULL) } | |
1941 | ||
1942 | static JSValueRef Type_getProperty_name(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1943 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); | |
1944 | return CYCastJSValue(context, internal->type_->GetName()); | |
1945 | } CYCatch(NULL) } | |
1946 | ||
1947 | static JSValueRef Type_getProperty_size(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
1948 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); | |
1949 | return CYCastJSValue(context, internal->GetFFI()->size); | |
1950 | } CYCatch(NULL) } | |
1951 | ||
1952 | static JSValueRef Type_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1953 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1954 | CYPool pool; | |
1955 | const char *type(sig::Unparse(pool, internal->type_)); | |
1956 | return CYCastJSValue(context, CYJSString(type)); | |
1957 | } CYCatch(NULL) } | |
1958 | ||
1959 | static JSValueRef Type_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1960 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1961 | CYLocalPool pool; | |
1962 | std::stringbuf out; | |
1963 | CYOptions options; | |
1964 | CYOutput output(out, options); | |
1965 | output.pretty_ = true; | |
1966 | (new(pool) CYTypeExpression(CYDecodeType(pool, internal->type_->Copy(pool, ""))))->Output(output, CYNoFlags); | |
1967 | return CYCastJSValue(context, CYJSString(out.str().c_str())); | |
1968 | } CYCatch(NULL) } | |
1969 | ||
1970 | static JSStaticFunction All_staticFunctions[2] = { | |
1971 | {"cy$complete", &All_complete_callAsFunction, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1972 | {NULL, NULL, 0} | |
1973 | }; | |
1974 | ||
1975 | static JSStaticFunction CArray_staticFunctions[3] = { | |
1976 | {"toPointer", &CArray_callAsFunction_toPointer, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1977 | {"valueOf", &CArray_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1978 | {NULL, NULL, 0} | |
1979 | }; | |
1980 | ||
1981 | static JSStaticValue CArray_staticValues[2] = { | |
1982 | {"$cyt", &CArray_getProperty_$cyt, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1983 | {NULL, NULL, NULL, 0} | |
1984 | }; | |
1985 | ||
1986 | static JSStaticFunction CString_staticFunctions[5] = { | |
1987 | {"toCYON", &CString_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1988 | {"toPointer", &CString_callAsFunction_toPointer, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1989 | {"toString", &CString_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1990 | {"valueOf", &CString_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1991 | {NULL, NULL, 0} | |
1992 | }; | |
1993 | ||
1994 | static JSStaticValue CString_staticValues[3] = { | |
1995 | {"length", &CString_getProperty_length, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1996 | {"$cyt", &CString_getProperty_$cyt, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1997 | {NULL, NULL, NULL, 0} | |
1998 | }; | |
1999 | ||
2000 | static JSStaticFunction Pointer_staticFunctions[4] = { | |
2001 | {"toCYON", &Pointer_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2002 | {"toPointer", &Pointer_callAsFunction_toPointer, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2003 | {"valueOf", &Pointer_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2004 | {NULL, NULL, 0} | |
2005 | }; | |
2006 | ||
2007 | static JSStaticValue Pointer_staticValues[2] = { | |
2008 | {"$cyt", &Pointer_getProperty_$cyt, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2009 | {NULL, NULL, NULL, 0} | |
2010 | }; | |
2011 | ||
2012 | static JSStaticFunction Struct_staticFunctions[2] = { | |
2013 | {"$cya", &Struct_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2014 | {NULL, NULL, 0} | |
2015 | }; | |
2016 | ||
2017 | static JSStaticValue Struct_staticValues[2] = { | |
2018 | {"$cyt", &Struct_getProperty_$cyt, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2019 | {NULL, NULL, NULL, 0} | |
2020 | }; | |
2021 | ||
2022 | static JSStaticFunction Functor_staticFunctions[5] = { | |
2023 | {"$cya", &Functor_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2024 | {"toCYON", &Functor_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2025 | {"toPointer", &Functor_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2026 | {"valueOf", &Functor_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2027 | {NULL, NULL, 0} | |
2028 | }; | |
2029 | ||
2030 | static JSStaticValue Functor_staticValues[2] = { | |
2031 | {"$cyt", &Functor_getProperty_$cyt, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2032 | {NULL, NULL, NULL, 0} | |
2033 | }; | |
2034 | ||
2035 | static JSStaticValue Type_staticValues[4] = { | |
2036 | {"alignment", &Type_getProperty_alignment, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2037 | {"name", &Type_getProperty_name, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2038 | {"size", &Type_getProperty_size, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2039 | {NULL, NULL, NULL, 0} | |
2040 | }; | |
2041 | ||
2042 | static JSStaticFunction Type_staticFunctions[10] = { | |
2043 | {"arrayOf", &Type_callAsFunction_arrayOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2044 | {"blockWith", &Type_callAsFunction_blockWith, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2045 | {"constant", &Type_callAsFunction_constant, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2046 | {"enumFor", &Type_callAsFunction_enumFor, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2047 | {"functionWith", &Type_callAsFunction_functionWith, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2048 | {"pointerTo", &Type_callAsFunction_pointerTo, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2049 | {"withName", &Type_callAsFunction_withName, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2050 | {"toCYON", &Type_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2051 | {"toString", &Type_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
2052 | {NULL, NULL, 0} | |
2053 | }; | |
2054 | ||
2055 | _visible void CYSetArgs(const char *argv0, const char *script, int argc, const char *argv[]) { | |
2056 | JSContextRef context(CYGetJSContext()); | |
2057 | JSValueRef args[argc + 2]; | |
2058 | for (int i(0); i != argc; ++i) | |
2059 | args[i + 2] = CYCastJSValue(context, argv[i]); | |
2060 | ||
2061 | size_t offset; | |
2062 | if (script == NULL) | |
2063 | offset = 1; | |
2064 | else { | |
2065 | offset = 0; | |
2066 | args[1] = CYCastJSValue(context, CYJSString(script)); | |
2067 | } | |
2068 | ||
2069 | args[offset] = CYCastJSValue(context, CYJSString(argv0)); | |
2070 | ||
2071 | CYSetProperty(context, CYGetCachedObject(context, CYJSString("System")), CYJSString("args"), CYObjectMakeArray(context, argc, args + 2)); | |
2072 | CYSetProperty(context, CYGetCachedObject(context, CYJSString("process")), CYJSString("argv"), CYObjectMakeArray(context, argc + 2 - offset, args + offset)); | |
2073 | } | |
2074 | ||
2075 | JSObjectRef CYGetGlobalObject(JSContextRef context) { | |
2076 | return JSContextGetGlobalObject(context); | |
2077 | } | |
2078 | ||
2079 | // XXX: this is neither exceptin safe nor even terribly sane | |
2080 | class ExecutionHandle { | |
2081 | private: | |
2082 | JSContextRef context_; | |
2083 | std::vector<void *> handles_; | |
2084 | ||
2085 | public: | |
2086 | ExecutionHandle(JSContextRef context) : | |
2087 | context_(context) | |
2088 | { | |
2089 | handles_.resize(GetHooks().size()); | |
2090 | for (size_t i(0); i != GetHooks().size(); ++i) { | |
2091 | CYHook *hook(GetHooks()[i]); | |
2092 | if (hook->ExecuteStart != NULL) | |
2093 | handles_[i] = (*hook->ExecuteStart)(context_); | |
2094 | else | |
2095 | handles_[i] = NULL; | |
2096 | } | |
2097 | } | |
2098 | ||
2099 | ~ExecutionHandle() { | |
2100 | for (size_t i(GetHooks().size()); i != 0; --i) { | |
2101 | CYHook *hook(GetHooks()[i-1]); | |
2102 | if (hook->ExecuteEnd != NULL) | |
2103 | (*hook->ExecuteEnd)(context_, handles_[i-1]); | |
2104 | } | |
2105 | } | |
2106 | }; | |
2107 | ||
2108 | #ifndef __ANDROID__ | |
2109 | static volatile bool cancel_; | |
2110 | ||
2111 | static bool CYShouldTerminate(JSContextRef context, void *arg) { | |
2112 | return cancel_; | |
2113 | } | |
2114 | #endif | |
2115 | ||
2116 | _visible const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) { | |
2117 | ExecutionHandle handle(context); | |
2118 | ||
2119 | #ifndef __ANDROID__ | |
2120 | cancel_ = false; | |
2121 | if (&JSContextGroupSetExecutionTimeLimit != NULL) | |
2122 | JSContextGroupSetExecutionTimeLimit(JSContextGetGroup(context), 0.5, &CYShouldTerminate, NULL); | |
2123 | #endif | |
2124 | ||
2125 | try { | |
2126 | JSValueRef result(_jsccall(JSEvaluateScript, context, CYJSString(code), NULL, NULL, 0)); | |
2127 | if (JSValueIsUndefined(context, result)) | |
2128 | return NULL; | |
2129 | ||
2130 | std::set<void *> objects; | |
2131 | const char *json(_jsccall(CYPoolCCYON, pool, context, result, objects)); | |
2132 | CYSetProperty(context, CYGetGlobalObject(context), Result_, result); | |
2133 | ||
2134 | return json; | |
2135 | } catch (const CYException &error) { | |
2136 | return pool.strcat("throw ", error.PoolCString(pool), NULL); | |
2137 | } | |
2138 | } | |
2139 | ||
2140 | #ifndef __ANDROID__ | |
2141 | _visible void CYCancel() { | |
2142 | cancel_ = true; | |
2143 | } | |
2144 | #endif | |
2145 | ||
2146 | const char *CYPoolLibraryPath(CYPool &pool); | |
2147 | ||
2148 | static bool initialized_ = false; | |
2149 | ||
2150 | void CYInitializeDynamic() { | |
2151 | if (!initialized_) | |
2152 | initialized_ = true; | |
2153 | else return; | |
2154 | ||
2155 | CYPool pool; | |
2156 | const char *db(pool.strcat(CYPoolLibraryPath(pool), "/libcycript.db", NULL)); | |
2157 | _sqlcall(sqlite3_open_v2(db, &database_, SQLITE_OPEN_READONLY, NULL)); | |
2158 | ||
2159 | JSObjectMakeArray$ = reinterpret_cast<JSObjectRef (*)(JSContextRef, size_t, const JSValueRef[], JSValueRef *)>(dlsym(RTLD_DEFAULT, "JSObjectMakeArray")); | |
2160 | JSSynchronousGarbageCollectForDebugging$ = reinterpret_cast<void (*)(JSContextRef)>(dlsym(RTLD_DEFAULT, "JSSynchronousGarbageCollectForDebugging")); | |
2161 | ||
2162 | JSClassDefinition definition; | |
2163 | ||
2164 | definition = kJSClassDefinitionEmpty; | |
2165 | definition.attributes = kJSClassAttributeNoAutomaticPrototype; | |
2166 | definition.className = "All"; | |
2167 | definition.staticFunctions = All_staticFunctions; | |
2168 | definition.hasProperty = &All_hasProperty; | |
2169 | definition.getProperty = &All_getProperty; | |
2170 | definition.getPropertyNames = &All_getPropertyNames; | |
2171 | All_ = JSClassCreate(&definition); | |
2172 | ||
2173 | definition = kJSClassDefinitionEmpty; | |
2174 | definition.attributes = kJSClassAttributeNoAutomaticPrototype; | |
2175 | definition.className = "Context"; | |
2176 | definition.finalize = &CYFinalize; | |
2177 | CYPrivate<Context>::Class_ = JSClassCreate(&definition); | |
2178 | ||
2179 | definition = kJSClassDefinitionEmpty; | |
2180 | definition.className = "CArray"; | |
2181 | definition.staticFunctions = CArray_staticFunctions; | |
2182 | definition.staticValues = CArray_staticValues; | |
2183 | definition.getProperty = &CArray_getProperty; | |
2184 | definition.setProperty = &CArray_setProperty; | |
2185 | definition.finalize = &CYFinalize; | |
2186 | CYPrivate<CArray>::Class_ = JSClassCreate(&definition); | |
2187 | ||
2188 | definition = kJSClassDefinitionEmpty; | |
2189 | definition.className = "CString"; | |
2190 | definition.staticFunctions = CString_staticFunctions; | |
2191 | definition.staticValues = CString_staticValues; | |
2192 | definition.getProperty = &CString_getProperty; | |
2193 | definition.setProperty = &CString_setProperty; | |
2194 | definition.finalize = &CYFinalize; | |
2195 | CYPrivate<CString>::Class_ = JSClassCreate(&definition); | |
2196 | ||
2197 | definition = kJSClassDefinitionEmpty; | |
2198 | definition.className = "Functor"; | |
2199 | definition.staticFunctions = Functor_staticFunctions; | |
2200 | definition.staticValues = Functor_staticValues; | |
2201 | definition.callAsFunction = &Functor_callAsFunction; | |
2202 | definition.finalize = &CYFinalize; | |
2203 | cy::Functor::Class_ = JSClassCreate(&definition); | |
2204 | ||
2205 | definition = kJSClassDefinitionEmpty; | |
2206 | definition.className = "Pointer"; | |
2207 | definition.staticFunctions = Pointer_staticFunctions; | |
2208 | definition.staticValues = Pointer_staticValues; | |
2209 | definition.callAsFunction = &Pointer_callAsFunction; | |
2210 | definition.getProperty = &Pointer_getProperty; | |
2211 | definition.setProperty = &Pointer_setProperty; | |
2212 | definition.finalize = &CYFinalize; | |
2213 | CYPrivate<Pointer>::Class_ = JSClassCreate(&definition); | |
2214 | ||
2215 | definition = kJSClassDefinitionEmpty; | |
2216 | definition.className = "Root"; | |
2217 | definition.finalize = &CYFinalize; | |
2218 | CYPrivate<CYRoot>::Class_ = JSClassCreate(&definition); | |
2219 | ||
2220 | definition = kJSClassDefinitionEmpty; | |
2221 | definition.className = "Struct"; | |
2222 | definition.staticFunctions = Struct_staticFunctions; | |
2223 | definition.staticValues = Struct_staticValues; | |
2224 | definition.getProperty = &Struct_getProperty; | |
2225 | definition.setProperty = &Struct_setProperty; | |
2226 | definition.getPropertyNames = &Struct_getPropertyNames; | |
2227 | definition.finalize = &CYFinalize; | |
2228 | CYPrivate<Struct_privateData>::Class_ = JSClassCreate(&definition); | |
2229 | ||
2230 | definition = kJSClassDefinitionEmpty; | |
2231 | definition.className = "Type"; | |
2232 | definition.staticValues = Type_staticValues; | |
2233 | definition.staticFunctions = Type_staticFunctions; | |
2234 | definition.callAsFunction = &Type_callAsFunction; | |
2235 | definition.callAsConstructor = &Type_callAsConstructor; | |
2236 | definition.finalize = &CYFinalize; | |
2237 | CYPrivate<Type_privateData>::Class_ = JSClassCreate(&definition); | |
2238 | ||
2239 | definition = kJSClassDefinitionEmpty; | |
2240 | definition.className = "Global"; | |
2241 | //definition.getProperty = &Global_getProperty; | |
2242 | Global_ = JSClassCreate(&definition); | |
2243 | ||
2244 | Array_s = JSStringCreateWithUTF8CString("Array"); | |
2245 | constructor_s = JSStringCreateWithUTF8CString("constructor"); | |
2246 | cy_s = JSStringCreateWithUTF8CString("$cy"); | |
2247 | cyi_s = JSStringCreateWithUTF8CString("$cyi"); | |
2248 | cyt_s = JSStringCreateWithUTF8CString("$cyt"); | |
2249 | cyt__s = JSStringCreateWithUTF8CString("$cyt_"); | |
2250 | length_s = JSStringCreateWithUTF8CString("length"); | |
2251 | message_s = JSStringCreateWithUTF8CString("message"); | |
2252 | name_s = JSStringCreateWithUTF8CString("name"); | |
2253 | pop_s = JSStringCreateWithUTF8CString("pop"); | |
2254 | prototype_s = JSStringCreateWithUTF8CString("prototype"); | |
2255 | push_s = JSStringCreateWithUTF8CString("push"); | |
2256 | splice_s = JSStringCreateWithUTF8CString("splice"); | |
2257 | toCYON_s = JSStringCreateWithUTF8CString("toCYON"); | |
2258 | toJSON_s = JSStringCreateWithUTF8CString("toJSON"); | |
2259 | toPointer_s = JSStringCreateWithUTF8CString("toPointer"); | |
2260 | toString_s = JSStringCreateWithUTF8CString("toString"); | |
2261 | weak_s = JSStringCreateWithUTF8CString("weak"); | |
2262 | ||
2263 | Result_ = JSStringCreateWithUTF8CString("_"); | |
2264 | ||
2265 | for (CYHook *hook : GetHooks()) | |
2266 | if (hook->Initialize != NULL) | |
2267 | (*hook->Initialize)(); | |
2268 | } | |
2269 | ||
2270 | void CYThrow(JSContextRef context, JSValueRef value) { | |
2271 | if (value != NULL) | |
2272 | throw CYJSError(context, value); | |
2273 | } | |
2274 | ||
2275 | const char *CYJSError::PoolCString(CYPool &pool) const { | |
2276 | std::set<void *> objects; | |
2277 | // XXX: this used to be CYPoolCString | |
2278 | return CYPoolCCYON(pool, context_, value_, objects); | |
2279 | } | |
2280 | ||
2281 | JSValueRef CYJSError::CastJSValue(JSContextRef context, const char *name) const { | |
2282 | // XXX: what if the context is different? or the name? I dunno. ("epic" :/) | |
2283 | return value_; | |
2284 | } | |
2285 | ||
2286 | JSValueRef CYCastJSError(JSContextRef context, const char *name, const char *message) { | |
2287 | JSObjectRef Error(CYGetCachedObject(context, CYJSString(name))); | |
2288 | JSValueRef arguments[1] = {CYCastJSValue(context, message)}; | |
2289 | return _jsccall(JSObjectCallAsConstructor, context, Error, 1, arguments); | |
2290 | } | |
2291 | ||
2292 | JSValueRef CYPoolError::CastJSValue(JSContextRef context, const char *name) const { | |
2293 | return CYCastJSError(context, name, message_); | |
2294 | } | |
2295 | ||
2296 | CYJSError::CYJSError(JSContextRef context, const char *format, ...) { | |
2297 | _assert(context != NULL); | |
2298 | ||
2299 | CYPool pool; | |
2300 | ||
2301 | va_list args; | |
2302 | va_start(args, format); | |
2303 | // XXX: there might be a beter way to think about this | |
2304 | const char *message(pool.vsprintf(64, format, args)); | |
2305 | va_end(args); | |
2306 | ||
2307 | value_ = CYCastJSError(context, "Error", message); | |
2308 | } | |
2309 | ||
2310 | JSGlobalContextRef CYGetJSContext(JSContextRef context) { | |
2311 | return reinterpret_cast<Context *>(JSObjectGetPrivate(CYCastJSObject(context, CYGetProperty(context, CYGetGlobalObject(context), cy_s))))->context_; | |
2312 | } | |
2313 | ||
2314 | #ifdef __ANDROID__ | |
2315 | char *CYPoolLibraryPath_(CYPool &pool) { | |
2316 | FILE *maps(fopen("/proc/self/maps", "r")); | |
2317 | struct F { FILE *f; F(FILE *f) : f(f) {} | |
2318 | ~F() { fclose(f); } } f(maps); | |
2319 | ||
2320 | size_t function(reinterpret_cast<size_t>(&CYPoolLibraryPath)); | |
2321 | ||
2322 | for (;;) { | |
2323 | size_t start; size_t end; char flags[8]; unsigned long long offset; | |
2324 | int major; int minor; unsigned long long inode; char file[1024]; | |
2325 | int count(fscanf(maps, "%zx-%zx %7s %llx %x:%x %llu%[ ]%1024[^\n]\n", | |
2326 | &start, &end, flags, &offset, &major, &minor, &inode, file, file)); | |
2327 | if (count < 8) break; else if (start <= function && function < end) | |
2328 | return pool.strdup(file); | |
2329 | } | |
2330 | ||
2331 | _assert(false); | |
2332 | } | |
2333 | #else | |
2334 | char *CYPoolLibraryPath_(CYPool &pool) { | |
2335 | Dl_info addr; | |
2336 | _assert(dladdr(reinterpret_cast<void *>(&CYPoolLibraryPath), &addr) != 0); | |
2337 | return pool.strdup(addr.dli_fname); | |
2338 | } | |
2339 | #endif | |
2340 | ||
2341 | const char *CYPoolLibraryPath(CYPool &pool) { | |
2342 | char *lib(CYPoolLibraryPath_(pool)); | |
2343 | ||
2344 | char *slash(strrchr(lib, '/')); | |
2345 | if (slash == NULL) | |
2346 | return "."; | |
2347 | *slash = '\0'; | |
2348 | ||
2349 | slash = strrchr(lib, '/'); | |
2350 | if (slash != NULL) { | |
2351 | if (strcmp(slash, "/.libs") == 0) | |
2352 | *slash = '\0'; | |
2353 | } else if (strcmp(lib, ".libs") == 0) | |
2354 | return "."; | |
2355 | ||
2356 | return lib; | |
2357 | } | |
2358 | ||
2359 | static JSValueRef require_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
2360 | _assert(count == 1); | |
2361 | CYPool pool; | |
2362 | ||
2363 | CYUTF8String name(CYPoolUTF8String(pool, context, CYJSString(context, arguments[0]))); | |
2364 | if (memchr(name.data, '/', name.size) == NULL && ( | |
2365 | #ifdef __APPLE__ | |
2366 | dlopen(pool.strcat("/System/Library/Frameworks/", name.data, ".framework/", name.data, NULL), RTLD_LAZY | RTLD_GLOBAL) != NULL || | |
2367 | dlopen(pool.strcat("/System/Library/PrivateFrameworks/", name.data, ".framework/", name.data, NULL), RTLD_LAZY | RTLD_GLOBAL) != NULL || | |
2368 | #endif | |
2369 | false)) | |
2370 | return CYJSUndefined(context); | |
2371 | ||
2372 | CYJSString path; | |
2373 | CYUTF8String code; | |
2374 | ||
2375 | sqlite3_stmt *statement; | |
2376 | ||
2377 | _sqlcall(sqlite3_prepare(database_, | |
2378 | "select " | |
2379 | "\"module\".\"code\", " | |
2380 | "\"module\".\"flags\" " | |
2381 | "from \"module\" " | |
2382 | "where" | |
2383 | " \"module\".\"name\" = ?" | |
2384 | " limit 1" | |
2385 | , -1, &statement, NULL)); | |
2386 | ||
2387 | _sqlcall(sqlite3_bind_text(statement, 1, name.data, name.size, SQLITE_STATIC)); | |
2388 | ||
2389 | if (_sqlcall(sqlite3_step(statement)) != SQLITE_DONE) { | |
2390 | code.data = static_cast<const char *>(sqlite3_column_blob(statement, 0)); | |
2391 | code.size = sqlite3_column_bytes(statement, 0); | |
2392 | path = CYJSString(name); | |
2393 | code = CYPoolUTF8String(pool, code); | |
2394 | } else { | |
2395 | JSObjectRef resolve(CYCastJSObject(context, CYGetProperty(context, object, CYJSString("resolve")))); | |
2396 | path = CYJSString(context, CYCallAsFunction(context, resolve, NULL, 1, arguments)); | |
2397 | } | |
2398 | ||
2399 | _sqlcall(sqlite3_finalize(statement)); | |
2400 | ||
2401 | CYJSString property("exports"); | |
2402 | ||
2403 | JSObjectRef modules(CYGetCachedObject(context, CYJSString("modules"))); | |
2404 | JSValueRef cache(CYGetProperty(context, modules, path)); | |
2405 | ||
2406 | JSValueRef result; | |
2407 | if (!JSValueIsUndefined(context, cache)) { | |
2408 | JSObjectRef module(CYCastJSObject(context, cache)); | |
2409 | result = CYGetProperty(context, module, property); | |
2410 | } else { | |
2411 | if (code.data == NULL) { | |
2412 | code = CYPoolFileUTF8String(pool, CYPoolCString(pool, context, path)); | |
2413 | _assert(code.data != NULL); | |
2414 | } | |
2415 | ||
2416 | size_t length(name.size); | |
2417 | if (length >= 5 && strncmp(name.data + length - 5, ".json", 5) == 0) { | |
2418 | JSObjectRef JSON(CYGetCachedObject(context, CYJSString("JSON"))); | |
2419 | JSObjectRef parse(CYCastJSObject(context, CYGetProperty(context, JSON, CYJSString("parse")))); | |
2420 | JSValueRef arguments[1] = { CYCastJSValue(context, CYJSString(code)) }; | |
2421 | result = CYCallAsFunction(context, parse, JSON, 1, arguments); | |
2422 | } else { | |
2423 | JSObjectRef module(JSObjectMake(context, NULL, NULL)); | |
2424 | CYSetProperty(context, modules, path, module); | |
2425 | ||
2426 | JSObjectRef exports(JSObjectMake(context, NULL, NULL)); | |
2427 | CYSetProperty(context, module, property, exports); | |
2428 | ||
2429 | std::stringstream wrap; | |
2430 | wrap << "(function (exports, require, module, __filename) { " << code << "\n});"; | |
2431 | code = CYPoolCode(pool, *wrap.rdbuf()); | |
2432 | ||
2433 | JSValueRef value(_jsccall(JSEvaluateScript, context, CYJSString(code), NULL, NULL, 0)); | |
2434 | JSObjectRef function(CYCastJSObject(context, value)); | |
2435 | ||
2436 | JSValueRef arguments[4] = { exports, object, module, CYCastJSValue(context, path) }; | |
2437 | CYCallAsFunction(context, function, NULL, 4, arguments); | |
2438 | result = CYGetProperty(context, module, property); | |
2439 | } | |
2440 | } | |
2441 | ||
2442 | return result; | |
2443 | } CYCatch(NULL) } | |
2444 | ||
2445 | static bool CYRunScript(JSGlobalContextRef context, const char *path) { | |
2446 | CYPool pool; | |
2447 | CYUTF8String code(CYPoolFileUTF8String(pool, pool.strcat(CYPoolLibraryPath(pool), path, NULL))); | |
2448 | if (code.data == NULL) | |
2449 | return false; | |
2450 | ||
2451 | code = CYPoolCode(pool, code); | |
2452 | _jsccall(JSEvaluateScript, context, CYJSString(code), NULL, NULL, 0); | |
2453 | return true; | |
2454 | } | |
2455 | ||
2456 | extern "C" void CYDestroyWeak(JSWeakObjectMapRef weak, void *data) { | |
2457 | } | |
2458 | ||
2459 | extern "C" void CYSetupContext(JSGlobalContextRef context) { | |
2460 | CYInitializeDynamic(); | |
2461 | ||
2462 | JSObjectRef global(CYGetGlobalObject(context)); | |
2463 | ||
2464 | JSObjectRef cy(CYPrivate<Context>::Make(context, context)); | |
2465 | CYSetProperty(context, global, cy_s, cy, kJSPropertyAttributeDontEnum); | |
2466 | ||
2467 | /* Cache Globals {{{ */ | |
2468 | JSObjectRef Array(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array")))); | |
2469 | CYSetProperty(context, cy, CYJSString("Array"), Array); | |
2470 | ||
2471 | JSObjectRef Array_prototype(CYCastJSObject(context, CYGetProperty(context, Array, prototype_s))); | |
2472 | CYSetProperty(context, cy, CYJSString("Array_prototype"), Array_prototype); | |
2473 | ||
2474 | JSObjectRef Boolean(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Boolean")))); | |
2475 | CYSetProperty(context, cy, CYJSString("Boolean"), Boolean); | |
2476 | ||
2477 | JSObjectRef Boolean_prototype(CYCastJSObject(context, CYGetProperty(context, Boolean, prototype_s))); | |
2478 | CYSetProperty(context, cy, CYJSString("Boolean_prototype"), Boolean_prototype); | |
2479 | ||
2480 | JSObjectRef Error(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Error")))); | |
2481 | CYSetProperty(context, cy, CYJSString("Error"), Error); | |
2482 | ||
2483 | JSObjectRef Function(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function")))); | |
2484 | CYSetProperty(context, cy, CYJSString("Function"), Function); | |
2485 | ||
2486 | JSObjectRef Function_prototype(CYCastJSObject(context, CYGetProperty(context, Function, prototype_s))); | |
2487 | CYSetProperty(context, cy, CYJSString("Function_prototype"), Function_prototype); | |
2488 | ||
2489 | JSObjectRef JSON(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("JSON")))); | |
2490 | CYSetProperty(context, cy, CYJSString("JSON"), JSON); | |
2491 | ||
2492 | JSObjectRef Number(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Number")))); | |
2493 | CYSetProperty(context, cy, CYJSString("Number"), Number); | |
2494 | ||
2495 | JSObjectRef Number_prototype(CYCastJSObject(context, CYGetProperty(context, Number, prototype_s))); | |
2496 | CYSetProperty(context, cy, CYJSString("Number_prototype"), Number_prototype); | |
2497 | ||
2498 | JSObjectRef Object(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Object")))); | |
2499 | CYSetProperty(context, cy, CYJSString("Object"), Object); | |
2500 | ||
2501 | JSObjectRef Object_prototype(CYCastJSObject(context, CYGetProperty(context, Object, prototype_s))); | |
2502 | CYSetProperty(context, cy, CYJSString("Object_prototype"), Object_prototype); | |
2503 | ||
2504 | JSObjectRef String(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("String")))); | |
2505 | CYSetProperty(context, cy, CYJSString("String"), String); | |
2506 | ||
2507 | JSObjectRef String_prototype(CYCastJSObject(context, CYGetProperty(context, String, prototype_s))); | |
2508 | CYSetProperty(context, cy, CYJSString("String_prototype"), String_prototype); | |
2509 | ||
2510 | JSObjectRef SyntaxError(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("SyntaxError")))); | |
2511 | CYSetProperty(context, cy, CYJSString("SyntaxError"), SyntaxError); | |
2512 | /* }}} */ | |
2513 | ||
2514 | CYSetProperty(context, Array_prototype, toCYON_s, &Array_callAsFunction_toCYON, kJSPropertyAttributeDontEnum); | |
2515 | CYSetProperty(context, String_prototype, toCYON_s, &String_callAsFunction_toCYON, kJSPropertyAttributeDontEnum); | |
2516 | ||
2517 | JSObjectRef cycript(JSObjectMake(context, NULL, NULL)); | |
2518 | CYSetProperty(context, global, CYJSString("Cycript"), cycript); | |
2519 | CYSetProperty(context, cycript, CYJSString("compile"), &Cycript_compile_callAsFunction); | |
2520 | CYSetProperty(context, cycript, CYJSString("gc"), &Cycript_gc_callAsFunction); | |
2521 | ||
2522 | JSObjectRef CArray(JSObjectMakeConstructor(context, CYPrivate<::CArray>::Class_, &CArray_new)); | |
2523 | CYSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, CArray, prototype_s)), Array_prototype); | |
2524 | CYSetProperty(context, cycript, CYJSString("CArray"), CArray); | |
2525 | ||
2526 | JSObjectRef CString(JSObjectMakeConstructor(context, CYPrivate<::CString>::Class_, &CString_new)); | |
2527 | CYSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, CString, prototype_s)), String_prototype); | |
2528 | CYSetProperty(context, cycript, CYJSString("CString"), CString); | |
2529 | ||
2530 | JSObjectRef Functor(JSObjectMakeConstructor(context, cy::Functor::Class_, &Functor_new)); | |
2531 | JSObjectRef Functor_prototype(CYCastJSObject(context, CYGetProperty(context, Functor, prototype_s))); | |
2532 | CYSetPrototype(context, Functor_prototype, Function_prototype); | |
2533 | CYSetProperty(context, cy, CYJSString("Functor_prototype"), Functor_prototype); | |
2534 | CYSetProperty(context, cycript, CYJSString("Functor"), Functor); | |
2535 | ||
2536 | CYSetProperty(context, cycript, CYJSString("Pointer"), JSObjectMakeConstructor(context, CYPrivate<Pointer>::Class_, &Pointer_new)); | |
2537 | ||
2538 | JSObjectRef Type(JSObjectMakeConstructor(context, CYPrivate<Type_privateData>::Class_, &Type_new)); | |
2539 | JSObjectRef Type_prototype(CYCastJSObject(context, CYGetProperty(context, Type, prototype_s))); | |
2540 | CYSetPrototype(context, Type_prototype, Function_prototype); | |
2541 | CYSetProperty(context, cy, CYJSString("Type_prototype"), Type_prototype); | |
2542 | CYSetProperty(context, cycript, CYJSString("Type"), Type); | |
2543 | ||
2544 | JSObjectRef Character_prototype(JSObjectMake(context, NULL, NULL)); | |
2545 | CYSetPrototype(context, Character_prototype, String_prototype); | |
2546 | CYSetProperty(context, cy, CYJSString("Character_prototype"), Character_prototype); | |
2547 | CYSetProperty(context, Character_prototype, CYJSString("valueOf"), _jsccall(JSEvaluateScript, context, CYJSString("(function(){return this.charCodeAt(0);})"), NULL, NULL, 0)); | |
2548 | ||
2549 | JSObjectRef modules(JSObjectMake(context, NULL, NULL)); | |
2550 | CYSetProperty(context, cy, CYJSString("modules"), modules); | |
2551 | ||
2552 | JSObjectRef all(JSObjectMake(context, All_, NULL)); | |
2553 | CYSetProperty(context, cycript, CYJSString("all"), all); | |
2554 | ||
2555 | JSObjectRef cache(JSObjectMake(context, NULL, NULL)); | |
2556 | CYSetProperty(context, cy, CYJSString("cache"), cache); | |
2557 | CYSetPrototype(context, cache, all); | |
2558 | ||
2559 | JSObjectRef alls(_jsccall(JSObjectCallAsConstructor, context, Array, 0, NULL)); | |
2560 | CYSetProperty(context, cycript, CYJSString("alls"), alls); | |
2561 | ||
2562 | if (true) { | |
2563 | JSObjectRef last(NULL), curr(global); | |
2564 | ||
2565 | goto next; for (JSValueRef next;;) { | |
2566 | if (JSValueIsNull(context, next)) | |
2567 | break; | |
2568 | last = curr; | |
2569 | curr = CYCastJSObject(context, next); | |
2570 | next: | |
2571 | next = JSObjectGetPrototype(context, curr); | |
2572 | } | |
2573 | ||
2574 | CYSetPrototype(context, last, cache); | |
2575 | } | |
2576 | ||
2577 | #ifdef __APPLE__ | |
2578 | if (&JSWeakObjectMapCreate != NULL) { | |
2579 | JSWeakObjectMapRef weak(JSWeakObjectMapCreate(context, NULL, &CYDestroyWeak)); | |
2580 | CYSetProperty(context, cy, weak_s, CYCastJSValue(context, reinterpret_cast<uintptr_t>(weak))); | |
2581 | } | |
2582 | #endif | |
2583 | ||
2584 | CYSetProperty(context, String_prototype, cyt_s, CYMakeType(context, sig::String(true)), kJSPropertyAttributeDontEnum); | |
2585 | ||
2586 | CYSetProperty(context, cache, CYJSString("dlerror"), CYMakeFunctor(context, "dlerror", "*"), kJSPropertyAttributeDontEnum); | |
2587 | CYSetProperty(context, cache, CYJSString("RTLD_DEFAULT"), CYCastJSValue(context, reinterpret_cast<intptr_t>(RTLD_DEFAULT)), kJSPropertyAttributeDontEnum); | |
2588 | CYSetProperty(context, cache, CYJSString("dlsym"), CYMakeFunctor(context, "dlsym", "^v^v*"), kJSPropertyAttributeDontEnum); | |
2589 | ||
2590 | CYSetProperty(context, cache, CYJSString("NULL"), CYJSNull(context), kJSPropertyAttributeDontEnum); | |
2591 | ||
2592 | CYSetProperty(context, cache, CYJSString("bool"), CYMakeType(context, sig::Primitive<bool>()), kJSPropertyAttributeDontEnum); | |
2593 | CYSetProperty(context, cache, CYJSString("char"), CYMakeType(context, sig::Primitive<char>()), kJSPropertyAttributeDontEnum); | |
2594 | CYSetProperty(context, cache, CYJSString("schar"), CYMakeType(context, sig::Primitive<signed char>()), kJSPropertyAttributeDontEnum); | |
2595 | CYSetProperty(context, cache, CYJSString("uchar"), CYMakeType(context, sig::Primitive<unsigned char>()), kJSPropertyAttributeDontEnum); | |
2596 | ||
2597 | CYSetProperty(context, cache, CYJSString("short"), CYMakeType(context, sig::Primitive<short>()), kJSPropertyAttributeDontEnum); | |
2598 | CYSetProperty(context, cache, CYJSString("int"), CYMakeType(context, sig::Primitive<int>()), kJSPropertyAttributeDontEnum); | |
2599 | CYSetProperty(context, cache, CYJSString("long"), CYMakeType(context, sig::Primitive<long>()), kJSPropertyAttributeDontEnum); | |
2600 | CYSetProperty(context, cache, CYJSString("longlong"), CYMakeType(context, sig::Primitive<long long>()), kJSPropertyAttributeDontEnum); | |
2601 | ||
2602 | CYSetProperty(context, cache, CYJSString("ushort"), CYMakeType(context, sig::Primitive<unsigned short>()), kJSPropertyAttributeDontEnum); | |
2603 | CYSetProperty(context, cache, CYJSString("uint"), CYMakeType(context, sig::Primitive<unsigned int>()), kJSPropertyAttributeDontEnum); | |
2604 | CYSetProperty(context, cache, CYJSString("ulong"), CYMakeType(context, sig::Primitive<unsigned long>()), kJSPropertyAttributeDontEnum); | |
2605 | CYSetProperty(context, cache, CYJSString("ulonglong"), CYMakeType(context, sig::Primitive<unsigned long long>()), kJSPropertyAttributeDontEnum); | |
2606 | ||
2607 | #ifdef __SIZEOF_INT128__ | |
2608 | CYSetProperty(context, cache, CYJSString("int128"), CYMakeType(context, sig::Primitive<__int128>()), kJSPropertyAttributeDontEnum); | |
2609 | CYSetProperty(context, cache, CYJSString("uint128"), CYMakeType(context, sig::Primitive<unsigned __int128>()), kJSPropertyAttributeDontEnum); | |
2610 | #endif | |
2611 | ||
2612 | CYSetProperty(context, cache, CYJSString("float"), CYMakeType(context, sig::Primitive<float>()), kJSPropertyAttributeDontEnum); | |
2613 | CYSetProperty(context, cache, CYJSString("double"), CYMakeType(context, sig::Primitive<double>()), kJSPropertyAttributeDontEnum); | |
2614 | CYSetProperty(context, cache, CYJSString("longdouble"), CYMakeType(context, sig::Primitive<long double>()), kJSPropertyAttributeDontEnum); | |
2615 | ||
2616 | CYSetProperty(context, global, CYJSString("require"), &require_callAsFunction, kJSPropertyAttributeDontEnum); | |
2617 | ||
2618 | JSObjectRef System(JSObjectMake(context, NULL, NULL)); | |
2619 | CYSetProperty(context, all, CYJSString("system"), System); | |
2620 | System = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("system"))); | |
2621 | CYSetProperty(context, cy, CYJSString("System"), System); | |
2622 | ||
2623 | JSObjectRef process(JSObjectMake(context, NULL, NULL)); | |
2624 | CYSetProperty(context, global, CYJSString("process"), process); | |
2625 | CYSetProperty(context, cy, CYJSString("process"), process); | |
2626 | ||
2627 | CYSetProperty(context, System, CYJSString("args"), CYJSNull(context)); | |
2628 | CYSetProperty(context, System, CYJSString("print"), &System_print); | |
2629 | ||
2630 | CYSetProperty(context, global, CYJSString("global"), global); | |
2631 | CYSetProperty(context, global, CYJSString("print"), &Global_print); | |
2632 | ||
2633 | for (CYHook *hook : GetHooks()) | |
2634 | if (hook->SetupContext != NULL) | |
2635 | (*hook->SetupContext)(context); | |
2636 | ||
2637 | CYArrayPush(context, alls, cycript); | |
2638 | ||
2639 | CYRunScript(context, "/libcycript.cy"); | |
2640 | } | |
2641 | ||
2642 | static JSGlobalContextRef context_; | |
2643 | ||
2644 | _visible JSGlobalContextRef CYGetJSContext() { | |
2645 | CYInitializeDynamic(); | |
2646 | ||
2647 | if (context_ == NULL) { | |
2648 | context_ = JSGlobalContextCreate(Global_); | |
2649 | CYSetupContext(context_); | |
2650 | } | |
2651 | ||
2652 | return context_; | |
2653 | } | |
2654 | ||
2655 | _visible void CYDestroyContext() { | |
2656 | if (context_ == NULL) | |
2657 | return; | |
2658 | JSGlobalContextRelease(context_); | |
2659 | context_ = NULL; | |
2660 | } |