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