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