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