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