]>
Commit | Line | Data |
---|---|---|
b3378a02 | 1 | /* Cycript - Optimizing JavaScript Compiler/Runtime |
c15969fd | 2 | * Copyright (C) 2009-2013 Jay Freeman (saurik) |
9cad30fa JF |
3 | */ |
4 | ||
c15969fd | 5 | /* GNU General Public License, Version 3 {{{ */ |
9cad30fa | 6 | /* |
c15969fd JF |
7 | * Cycript is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published | |
9 | * by the Free Software Foundation, either version 3 of the License, | |
10 | * or (at your option) any later version. | |
9cad30fa | 11 | * |
c15969fd JF |
12 | * Cycript is distributed in the hope that it will be useful, but |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
9cad30fa | 16 | * |
c15969fd | 17 | * You should have received a copy of the GNU General Public License |
b3378a02 JF |
18 | * along with Cycript. If not, see <http://www.gnu.org/licenses/>. |
19 | **/ | |
9cad30fa JF |
20 | /* }}} */ |
21 | ||
9cad30fa JF |
22 | #include "Internal.hpp" |
23 | ||
24 | #include <dlfcn.h> | |
9cad30fa JF |
25 | |
26 | #include "cycript.hpp" | |
27 | ||
28 | #include "sig/parse.hpp" | |
29 | #include "sig/ffi_type.hpp" | |
30 | ||
31 | #include "Pooling.hpp" | |
2f51d6ab | 32 | #include "Execute.hpp" |
9cad30fa JF |
33 | |
34 | #include <sys/mman.h> | |
35 | ||
36 | #include <iostream> | |
9cad30fa JF |
37 | #include <set> |
38 | #include <map> | |
39 | #include <iomanip> | |
40 | #include <sstream> | |
41 | #include <cmath> | |
42 | ||
43 | #include "Parser.hpp" | |
9cad30fa JF |
44 | |
45 | #include "Error.hpp" | |
46 | #include "JavaScript.hpp" | |
47 | #include "String.hpp" | |
48 | ||
9cad30fa JF |
49 | struct CYHooks *hooks_; |
50 | ||
51 | /* JavaScript Properties {{{ */ | |
52 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index) { | |
53 | JSValueRef exception(NULL); | |
54 | JSValueRef value(JSObjectGetPropertyAtIndex(context, object, index, &exception)); | |
55 | CYThrow(context, exception); | |
56 | return value; | |
57 | } | |
58 | ||
59 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name) { | |
60 | JSValueRef exception(NULL); | |
61 | JSValueRef value(JSObjectGetProperty(context, object, name, &exception)); | |
62 | CYThrow(context, exception); | |
63 | return value; | |
64 | } | |
65 | ||
66 | void CYSetProperty(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value) { | |
67 | JSValueRef exception(NULL); | |
68 | JSObjectSetPropertyAtIndex(context, object, index, value, &exception); | |
69 | CYThrow(context, exception); | |
70 | } | |
71 | ||
72 | void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value, JSPropertyAttributes attributes) { | |
73 | JSValueRef exception(NULL); | |
74 | JSObjectSetProperty(context, object, name, value, attributes, &exception); | |
75 | CYThrow(context, exception); | |
76 | } | |
77 | ||
78 | void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef (*callback)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *), JSPropertyAttributes attributes) { | |
79 | CYSetProperty(context, object, name, JSObjectMakeFunctionWithCallback(context, name, callback), attributes); | |
80 | } | |
81 | /* }}} */ | |
82 | /* JavaScript Strings {{{ */ | |
83 | JSStringRef CYCopyJSString(const char *value) { | |
84 | return value == NULL ? NULL : JSStringCreateWithUTF8CString(value); | |
85 | } | |
86 | ||
87 | JSStringRef CYCopyJSString(JSStringRef value) { | |
88 | return value == NULL ? NULL : JSStringRetain(value); | |
89 | } | |
90 | ||
91 | JSStringRef CYCopyJSString(CYUTF8String value) { | |
92 | // XXX: this is very wrong; it needs to convert to UTF16 and then create from there | |
93 | return CYCopyJSString(value.data); | |
94 | } | |
95 | ||
96 | JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value) { | |
97 | if (JSValueIsNull(context, value)) | |
98 | return NULL; | |
99 | JSValueRef exception(NULL); | |
100 | JSStringRef string(JSValueToStringCopy(context, value, &exception)); | |
101 | CYThrow(context, exception); | |
102 | return string; | |
103 | } | |
104 | ||
105 | static CYUTF16String CYCastUTF16String(JSStringRef value) { | |
106 | return CYUTF16String(JSStringGetCharactersPtr(value), JSStringGetLength(value)); | |
107 | } | |
108 | ||
b799113b | 109 | CYUTF8String CYPoolUTF8String(CYPool &pool, JSContextRef context, JSStringRef value) { |
9cad30fa JF |
110 | return CYPoolUTF8String(pool, CYCastUTF16String(value)); |
111 | } | |
112 | ||
b799113b | 113 | const char *CYPoolCString(CYPool &pool, JSContextRef context, JSStringRef value) { |
9cad30fa JF |
114 | CYUTF8String utf8(CYPoolUTF8String(pool, context, value)); |
115 | _assert(memchr(utf8.data, '\0', utf8.size) == NULL); | |
116 | return utf8.data; | |
117 | } | |
118 | ||
b799113b | 119 | const char *CYPoolCString(CYPool &pool, JSContextRef context, JSValueRef value) { |
9cad30fa JF |
120 | return JSValueIsNull(context, value) ? NULL : CYPoolCString(pool, context, CYJSString(context, value)); |
121 | } | |
122 | /* }}} */ | |
123 | /* Index Offsets {{{ */ | |
b799113b | 124 | size_t CYGetIndex(CYPool &pool, JSContextRef context, JSStringRef value) { |
9cad30fa JF |
125 | return CYGetIndex(CYPoolUTF8String(pool, context, value)); |
126 | } | |
127 | /* }}} */ | |
128 | ||
129 | static JSClassRef All_; | |
130 | static JSClassRef Context_; | |
3f9ae37c | 131 | JSClassRef Functor_; |
9cad30fa JF |
132 | static JSClassRef Global_; |
133 | static JSClassRef Pointer_; | |
134 | static JSClassRef Struct_; | |
135 | ||
136 | JSStringRef Array_s; | |
137 | JSStringRef cy_s; | |
138 | JSStringRef length_s; | |
139 | JSStringRef message_s; | |
140 | JSStringRef name_s; | |
141 | JSStringRef pop_s; | |
142 | JSStringRef prototype_s; | |
143 | JSStringRef push_s; | |
144 | JSStringRef splice_s; | |
145 | JSStringRef toCYON_s; | |
146 | JSStringRef toJSON_s; | |
20ded97a | 147 | JSStringRef toPointer_s; |
4cb8aa43 | 148 | JSStringRef toString_s; |
9cad30fa JF |
149 | |
150 | static JSStringRef Result_; | |
151 | ||
9cad30fa | 152 | void CYFinalize(JSObjectRef object) { |
1850a470 | 153 | CYData *internal(reinterpret_cast<CYData *>(JSObjectGetPrivate(object))); |
b799113b | 154 | _assert(internal->count_ != _not(unsigned)); |
1850a470 JF |
155 | if (--internal->count_ == 0) |
156 | delete internal; | |
9cad30fa JF |
157 | } |
158 | ||
b799113b | 159 | void Structor_(CYPool &pool, sig::Type *&type) { |
9cad30fa JF |
160 | if ( |
161 | type->primitive == sig::pointer_P && | |
162 | type->data.data.type != NULL && | |
163 | type->data.data.type->primitive == sig::struct_P && | |
1648ddb9 | 164 | type->data.data.type->name != NULL && |
9cad30fa JF |
165 | strcmp(type->data.data.type->name, "_objc_class") == 0 |
166 | ) { | |
167 | type->primitive = sig::typename_P; | |
168 | type->data.data.type = NULL; | |
169 | return; | |
170 | } | |
171 | ||
172 | if (type->primitive != sig::struct_P || type->name == NULL) | |
173 | return; | |
174 | ||
2f51d6ab JF |
175 | size_t length(strlen(type->name)); |
176 | char keyed[length + 2]; | |
177 | memcpy(keyed + 1, type->name, length + 1); | |
178 | ||
179 | static const char *modes = "34"; | |
180 | for (size_t i(0); i != 2; ++i) { | |
181 | char mode(modes[i]); | |
182 | keyed[0] = mode; | |
183 | ||
184 | if (CYBridgeEntry *entry = CYBridgeHash(keyed, length + 1)) | |
185 | switch (mode) { | |
186 | case '3': | |
187 | sig::Parse(pool, &type->data.signature, entry->value_, &Structor_); | |
188 | break; | |
189 | ||
190 | case '4': { | |
191 | sig::Signature signature; | |
192 | sig::Parse(pool, &signature, entry->value_, &Structor_); | |
193 | type = signature.elements[0].type; | |
194 | } break; | |
195 | } | |
9cad30fa JF |
196 | } |
197 | } | |
198 | ||
199 | JSClassRef Type_privateData::Class_; | |
200 | ||
201 | struct Context : | |
202 | CYData | |
203 | { | |
204 | JSGlobalContextRef context_; | |
205 | ||
206 | Context(JSGlobalContextRef context) : | |
207 | context_(context) | |
208 | { | |
209 | } | |
210 | }; | |
211 | ||
212 | struct Pointer : | |
213 | CYOwned | |
214 | { | |
215 | Type_privateData *type_; | |
216 | size_t length_; | |
217 | ||
218 | Pointer(void *value, JSContextRef context, JSObjectRef owner, size_t length, sig::Type *type) : | |
219 | CYOwned(value, context, owner), | |
b799113b | 220 | type_(new(*pool_) Type_privateData(type)), |
9cad30fa JF |
221 | length_(length) |
222 | { | |
223 | } | |
224 | }; | |
225 | ||
226 | struct Struct_privateData : | |
227 | CYOwned | |
228 | { | |
229 | Type_privateData *type_; | |
230 | ||
231 | Struct_privateData(JSContextRef context, JSObjectRef owner) : | |
232 | CYOwned(NULL, context, owner) | |
233 | { | |
234 | } | |
235 | }; | |
236 | ||
14ec9e00 | 237 | typedef std::map<const char *, Type_privateData *, CYCStringLess> TypeMap; |
9cad30fa JF |
238 | static TypeMap Types_; |
239 | ||
240 | JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef owner) { | |
241 | Struct_privateData *internal(new Struct_privateData(context, owner)); | |
b799113b | 242 | CYPool &pool(*internal->pool_); |
9cad30fa JF |
243 | Type_privateData *typical(new(pool) Type_privateData(type, ffi)); |
244 | internal->type_ = typical; | |
245 | ||
246 | if (owner != NULL) | |
247 | internal->value_ = data; | |
248 | else { | |
249 | size_t size(typical->GetFFI()->size); | |
0cbeddf8 | 250 | void *copy(internal->pool_->malloc<void>(size)); |
9cad30fa JF |
251 | memcpy(copy, data, size); |
252 | internal->value_ = copy; | |
253 | } | |
254 | ||
255 | return JSObjectMake(context, Struct_, internal); | |
256 | } | |
257 | ||
9a98069f JF |
258 | static void *CYCastSymbol(const char *name) { |
259 | return dlsym(RTLD_DEFAULT, name); | |
260 | } | |
261 | ||
9cad30fa JF |
262 | JSValueRef CYCastJSValue(JSContextRef context, bool value) { |
263 | return JSValueMakeBoolean(context, value); | |
264 | } | |
265 | ||
266 | JSValueRef CYCastJSValue(JSContextRef context, double value) { | |
267 | return JSValueMakeNumber(context, value); | |
268 | } | |
269 | ||
270 | #define CYCastJSValue_(Type_) \ | |
271 | JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \ | |
272 | return JSValueMakeNumber(context, static_cast<double>(value)); \ | |
273 | } | |
274 | ||
275 | CYCastJSValue_(int) | |
276 | CYCastJSValue_(unsigned int) | |
277 | CYCastJSValue_(long int) | |
278 | CYCastJSValue_(long unsigned int) | |
279 | CYCastJSValue_(long long int) | |
280 | CYCastJSValue_(long long unsigned int) | |
281 | ||
282 | JSValueRef CYJSUndefined(JSContextRef context) { | |
283 | return JSValueMakeUndefined(context); | |
284 | } | |
285 | ||
286 | double CYCastDouble(JSContextRef context, JSValueRef value) { | |
287 | JSValueRef exception(NULL); | |
288 | double number(JSValueToNumber(context, value, &exception)); | |
289 | CYThrow(context, exception); | |
290 | return number; | |
291 | } | |
292 | ||
293 | bool CYCastBool(JSContextRef context, JSValueRef value) { | |
294 | return JSValueToBoolean(context, value); | |
295 | } | |
296 | ||
297 | JSValueRef CYJSNull(JSContextRef context) { | |
298 | return JSValueMakeNull(context); | |
299 | } | |
300 | ||
301 | JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value) { | |
302 | return value == NULL ? CYJSNull(context) : JSValueMakeString(context, value); | |
303 | } | |
304 | ||
305 | JSValueRef CYCastJSValue(JSContextRef context, const char *value) { | |
306 | return CYCastJSValue(context, CYJSString(value)); | |
307 | } | |
308 | ||
309 | JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) { | |
310 | JSValueRef exception(NULL); | |
311 | JSObjectRef object(JSValueToObject(context, value, &exception)); | |
312 | CYThrow(context, exception); | |
313 | return object; | |
314 | } | |
315 | ||
316 | JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, const JSValueRef arguments[]) { | |
317 | JSValueRef exception(NULL); | |
318 | JSValueRef value(JSObjectCallAsFunction(context, function, _this, count, arguments, &exception)); | |
319 | CYThrow(context, exception); | |
320 | return value; | |
321 | } | |
322 | ||
323 | bool CYIsCallable(JSContextRef context, JSValueRef value) { | |
324 | return value != NULL && JSValueIsObject(context, value) && JSObjectIsFunction(context, (JSObjectRef) value); | |
325 | } | |
326 | ||
327 | static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
328 | if (count == 0) | |
329 | printf("\n"); | |
330 | else { | |
331 | CYPool pool; | |
332 | printf("%s\n", CYPoolCString(pool, context, arguments[0])); | |
333 | } | |
334 | ||
335 | return CYJSUndefined(context); | |
55c6d6ab | 336 | } CYCatch(NULL) } |
9cad30fa JF |
337 | |
338 | static size_t Nonce_(0); | |
339 | ||
340 | static JSValueRef $cyq(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
341 | CYPool pool; | |
0cbeddf8 | 342 | const char *name(pool.strcat(CYPoolCString(pool, context, arguments[0]), pool.itoa(Nonce_++), NULL)); |
9cad30fa JF |
343 | return CYCastJSValue(context, name); |
344 | } | |
345 | ||
346 | static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
347 | JSGarbageCollect(context); | |
348 | return CYJSUndefined(context); | |
349 | } | |
350 | ||
b799113b | 351 | const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value, JSValueRef *exception) { CYTry { |
9cad30fa JF |
352 | switch (JSType type = JSValueGetType(context, value)) { |
353 | case kJSTypeUndefined: | |
354 | return "undefined"; | |
355 | case kJSTypeNull: | |
356 | return "null"; | |
357 | case kJSTypeBoolean: | |
358 | return CYCastBool(context, value) ? "true" : "false"; | |
359 | ||
360 | case kJSTypeNumber: { | |
361 | std::ostringstream str; | |
362 | CYNumerify(str, CYCastDouble(context, value)); | |
363 | std::string value(str.str()); | |
b799113b | 364 | return pool.strmemdup(value.c_str(), value.size()); |
9cad30fa JF |
365 | } break; |
366 | ||
367 | case kJSTypeString: { | |
368 | std::ostringstream str; | |
369 | CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, value))); | |
370 | CYStringify(str, string.data, string.size); | |
371 | std::string value(str.str()); | |
b799113b | 372 | return pool.strmemdup(value.c_str(), value.size()); |
9cad30fa JF |
373 | } break; |
374 | ||
375 | case kJSTypeObject: | |
376 | return CYPoolCCYON(pool, context, (JSObjectRef) value); | |
377 | default: | |
378 | throw CYJSError(context, "JSValueGetType() == 0x%x", type); | |
379 | } | |
55c6d6ab | 380 | } CYCatch(NULL) } |
9cad30fa | 381 | |
b799113b | 382 | const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value) { |
9cad30fa JF |
383 | JSValueRef exception(NULL); |
384 | const char *cyon(CYPoolCCYON(pool, context, value, &exception)); | |
385 | CYThrow(context, exception); | |
386 | return cyon; | |
387 | } | |
388 | ||
b799113b | 389 | const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object) { |
9cad30fa JF |
390 | JSValueRef toCYON(CYGetProperty(context, object, toCYON_s)); |
391 | if (CYIsCallable(context, toCYON)) { | |
392 | JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toCYON, object, 0, NULL)); | |
393 | _assert(value != NULL); | |
394 | return CYPoolCString(pool, context, value); | |
395 | } | |
396 | ||
397 | JSValueRef toJSON(CYGetProperty(context, object, toJSON_s)); | |
398 | if (CYIsCallable(context, toJSON)) { | |
399 | JSValueRef arguments[1] = {CYCastJSValue(context, CYJSString(""))}; | |
400 | JSValueRef exception(NULL); | |
401 | const char *cyon(CYPoolCCYON(pool, context, CYCallAsFunction(context, (JSObjectRef) toJSON, object, 1, arguments), &exception)); | |
402 | CYThrow(context, exception); | |
403 | return cyon; | |
404 | } | |
405 | ||
005b2e9f JF |
406 | if (JSObjectIsFunction(context, object)) { |
407 | JSValueRef toString(CYGetProperty(context, object, toString_s)); | |
408 | if (CYIsCallable(context, toString)) { | |
409 | JSValueRef arguments[1] = {CYCastJSValue(context, CYJSString(""))}; | |
410 | JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toString, object, 1, arguments)); | |
411 | _assert(value != NULL); | |
412 | return CYPoolCString(pool, context, value); | |
413 | } | |
414 | } | |
415 | ||
9cad30fa JF |
416 | std::ostringstream str; |
417 | ||
418 | str << '{'; | |
419 | ||
420 | // XXX: this is, sadly, going to leak | |
421 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context, object)); | |
422 | ||
423 | bool comma(false); | |
424 | ||
425 | for (size_t index(0), count(JSPropertyNameArrayGetCount(names)); index != count; ++index) { | |
426 | JSStringRef name(JSPropertyNameArrayGetNameAtIndex(names, index)); | |
427 | JSValueRef value(CYGetProperty(context, object, name)); | |
428 | ||
429 | if (comma) | |
430 | str << ','; | |
431 | else | |
432 | comma = true; | |
433 | ||
434 | CYUTF8String string(CYPoolUTF8String(pool, context, name)); | |
435 | if (CYIsKey(string)) | |
436 | str << string.data; | |
437 | else | |
438 | CYStringify(str, string.data, string.size); | |
439 | ||
440 | str << ':' << CYPoolCCYON(pool, context, value); | |
441 | } | |
442 | ||
443 | str << '}'; | |
444 | ||
445 | JSPropertyNameArrayRelease(names); | |
446 | ||
447 | std::string string(str.str()); | |
b799113b | 448 | return pool.strmemdup(string.c_str(), string.size()); |
9cad30fa JF |
449 | } |
450 | ||
451 | static JSValueRef Array_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
452 | CYPool pool; | |
453 | std::ostringstream str; | |
454 | ||
455 | str << '['; | |
456 | ||
457 | JSValueRef length(CYGetProperty(context, _this, length_s)); | |
458 | bool comma(false); | |
459 | ||
460 | for (size_t index(0), count(CYCastDouble(context, length)); index != count; ++index) { | |
461 | JSValueRef value(CYGetProperty(context, _this, index)); | |
462 | ||
463 | if (comma) | |
464 | str << ','; | |
465 | else | |
466 | comma = true; | |
467 | ||
468 | if (!JSValueIsUndefined(context, value)) | |
469 | str << CYPoolCCYON(pool, context, value); | |
470 | else { | |
471 | str << ','; | |
472 | comma = false; | |
473 | } | |
474 | } | |
475 | ||
476 | str << ']'; | |
477 | ||
478 | std::string value(str.str()); | |
479 | return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size()))); | |
55c6d6ab | 480 | } CYCatch(NULL) } |
9cad30fa | 481 | |
4cb8aa43 JF |
482 | static JSValueRef String_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
483 | CYPool pool; | |
484 | std::ostringstream str; | |
485 | ||
486 | CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, _this))); | |
487 | CYStringify(str, string.data, string.size); | |
488 | ||
489 | std::string value(str.str()); | |
490 | return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size()))); | |
55c6d6ab | 491 | } CYCatch(NULL) } |
4cb8aa43 | 492 | |
9cad30fa JF |
493 | JSObjectRef CYMakePointer(JSContextRef context, void *pointer, size_t length, sig::Type *type, ffi_type *ffi, JSObjectRef owner) { |
494 | Pointer *internal(new Pointer(pointer, context, owner, length, type)); | |
495 | return JSObjectMake(context, Pointer_, internal); | |
496 | } | |
497 | ||
9a98069f JF |
498 | static JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) { |
499 | return JSObjectMake(context, Functor_, new cy::Functor(type, function)); | |
500 | } | |
1850a470 | 501 | |
9a98069f JF |
502 | static JSObjectRef CYMakeFunctor(JSContextRef context, const char *symbol, const char *type, void **cache) { |
503 | cy::Functor *internal; | |
504 | if (*cache != NULL) | |
1850a470 | 505 | internal = reinterpret_cast<cy::Functor *>(*cache); |
9a98069f JF |
506 | else { |
507 | void (*function)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol))); | |
508 | if (function == NULL) | |
509 | return NULL; | |
1850a470 | 510 | |
9a98069f JF |
511 | internal = new cy::Functor(type, function); |
512 | *cache = internal; | |
1850a470 JF |
513 | } |
514 | ||
9a98069f | 515 | ++internal->count_; |
9cad30fa JF |
516 | return JSObjectMake(context, Functor_, internal); |
517 | } | |
518 | ||
b799113b | 519 | static bool CYGetOffset(CYPool &pool, JSContextRef context, JSStringRef value, ssize_t &index) { |
9cad30fa JF |
520 | return CYGetOffset(CYPoolCString(pool, context, value), index); |
521 | } | |
522 | ||
523 | void *CYCastPointer_(JSContextRef context, JSValueRef value) { | |
524 | switch (JSValueGetType(context, value)) { | |
525 | case kJSTypeNull: | |
526 | return NULL; | |
20ded97a JF |
527 | case kJSTypeObject: { |
528 | JSObjectRef object((JSObjectRef) value); | |
9cad30fa | 529 | if (JSValueIsObjectOfClass(context, value, Pointer_)) { |
20ded97a | 530 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); |
9cad30fa | 531 | return internal->value_; |
20ded97a JF |
532 | } |
533 | JSValueRef toPointer(CYGetProperty(context, object, toPointer_s)); | |
534 | if (CYIsCallable(context, toPointer)) { | |
535 | JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toPointer, object, 0, NULL)); | |
536 | _assert(value != NULL); | |
537 | return CYCastPointer_(context, value); | |
538 | } | |
539 | } default: | |
9cad30fa JF |
540 | double number(CYCastDouble(context, value)); |
541 | if (std::isnan(number)) | |
542 | throw CYJSError(context, "cannot convert value to pointer"); | |
543 | return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number))); | |
544 | } | |
545 | } | |
546 | ||
b799113b | 547 | void CYPoolFFI(CYPool *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { |
9cad30fa JF |
548 | switch (type->primitive) { |
549 | case sig::boolean_P: | |
550 | *reinterpret_cast<bool *>(data) = JSValueToBoolean(context, value); | |
551 | break; | |
552 | ||
553 | #define CYPoolFFI_(primitive, native) \ | |
554 | case sig::primitive ## _P: \ | |
555 | *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \ | |
556 | break; | |
557 | ||
558 | CYPoolFFI_(uchar, unsigned char) | |
559 | CYPoolFFI_(char, char) | |
560 | CYPoolFFI_(ushort, unsigned short) | |
561 | CYPoolFFI_(short, short) | |
562 | CYPoolFFI_(ulong, unsigned long) | |
563 | CYPoolFFI_(long, long) | |
564 | CYPoolFFI_(uint, unsigned int) | |
565 | CYPoolFFI_(int, int) | |
566 | CYPoolFFI_(ulonglong, unsigned long long) | |
567 | CYPoolFFI_(longlong, long long) | |
568 | CYPoolFFI_(float, float) | |
569 | CYPoolFFI_(double, double) | |
570 | ||
571 | case sig::array_P: { | |
572 | uint8_t *base(reinterpret_cast<uint8_t *>(data)); | |
573 | JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL); | |
574 | for (size_t index(0); index != type->data.data.size; ++index) { | |
575 | ffi_type *field(ffi->elements[index]); | |
576 | ||
577 | JSValueRef rhs; | |
578 | if (aggregate == NULL) | |
579 | rhs = value; | |
580 | else { | |
581 | rhs = CYGetProperty(context, aggregate, index); | |
582 | if (JSValueIsUndefined(context, rhs)) | |
583 | throw CYJSError(context, "unable to extract array value"); | |
584 | } | |
585 | ||
586 | CYPoolFFI(pool, context, type->data.data.type, field, base, rhs); | |
587 | // XXX: alignment? | |
588 | base += field->size; | |
589 | } | |
590 | } break; | |
591 | ||
592 | case sig::pointer_P: | |
593 | *reinterpret_cast<void **>(data) = CYCastPointer<void *>(context, value); | |
594 | break; | |
595 | ||
596 | case sig::string_P: | |
b799113b JF |
597 | _assert(pool != NULL); |
598 | *reinterpret_cast<const char **>(data) = CYPoolCString(*pool, context, value); | |
9cad30fa JF |
599 | break; |
600 | ||
601 | case sig::struct_P: { | |
602 | uint8_t *base(reinterpret_cast<uint8_t *>(data)); | |
603 | JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL); | |
604 | for (size_t index(0); index != type->data.signature.count; ++index) { | |
605 | sig::Element *element(&type->data.signature.elements[index]); | |
606 | ffi_type *field(ffi->elements[index]); | |
607 | ||
608 | JSValueRef rhs; | |
609 | if (aggregate == NULL) | |
610 | rhs = value; | |
611 | else { | |
612 | rhs = CYGetProperty(context, aggregate, index); | |
613 | if (JSValueIsUndefined(context, rhs)) { | |
614 | if (element->name != NULL) | |
615 | rhs = CYGetProperty(context, aggregate, CYJSString(element->name)); | |
616 | else | |
617 | goto undefined; | |
618 | if (JSValueIsUndefined(context, rhs)) undefined: | |
619 | throw CYJSError(context, "unable to extract structure value"); | |
620 | } | |
621 | } | |
622 | ||
623 | CYPoolFFI(pool, context, element->type, field, base, rhs); | |
624 | // XXX: alignment? | |
625 | base += field->size; | |
626 | } | |
627 | } break; | |
628 | ||
629 | case sig::void_P: | |
630 | break; | |
631 | ||
632 | default: | |
633 | if (hooks_ != NULL && hooks_->PoolFFI != NULL) | |
634 | if ((*hooks_->PoolFFI)(pool, context, type, ffi, data, value)) | |
635 | return; | |
636 | ||
637 | CYThrow("unimplemented signature code: '%c''\n", type->primitive); | |
638 | } | |
639 | } | |
640 | ||
641 | JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) { | |
642 | switch (type->primitive) { | |
643 | case sig::boolean_P: | |
644 | return CYCastJSValue(context, *reinterpret_cast<bool *>(data)); | |
645 | ||
646 | #define CYFromFFI_(primitive, native) \ | |
647 | case sig::primitive ## _P: \ | |
648 | return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \ | |
649 | ||
650 | CYFromFFI_(uchar, unsigned char) | |
651 | CYFromFFI_(char, char) | |
652 | CYFromFFI_(ushort, unsigned short) | |
653 | CYFromFFI_(short, short) | |
654 | CYFromFFI_(ulong, unsigned long) | |
655 | CYFromFFI_(long, long) | |
656 | CYFromFFI_(uint, unsigned int) | |
657 | CYFromFFI_(int, int) | |
658 | CYFromFFI_(ulonglong, unsigned long long) | |
659 | CYFromFFI_(longlong, long long) | |
660 | CYFromFFI_(float, float) | |
661 | CYFromFFI_(double, double) | |
662 | ||
663 | case sig::array_P: | |
664 | if (void *pointer = data) | |
665 | return CYMakePointer(context, pointer, type->data.data.size, type->data.data.type, NULL, owner); | |
666 | else goto null; | |
667 | ||
668 | case sig::pointer_P: | |
669 | if (void *pointer = *reinterpret_cast<void **>(data)) | |
670 | return CYMakePointer(context, pointer, _not(size_t), type->data.data.type, NULL, owner); | |
671 | else goto null; | |
672 | ||
673 | case sig::string_P: | |
674 | if (char *utf8 = *reinterpret_cast<char **>(data)) | |
675 | return CYCastJSValue(context, utf8); | |
676 | else goto null; | |
677 | ||
678 | case sig::struct_P: | |
679 | return CYMakeStruct(context, data, type, ffi, owner); | |
680 | case sig::void_P: | |
681 | return CYJSUndefined(context); | |
682 | ||
683 | null: | |
684 | return CYJSNull(context); | |
685 | default: | |
686 | if (hooks_ != NULL && hooks_->FromFFI != NULL) | |
687 | if (JSValueRef value = (*hooks_->FromFFI)(context, type, ffi, data, initialize, owner)) | |
688 | return value; | |
689 | ||
690 | CYThrow("unimplemented signature code: '%c''\n", type->primitive); | |
691 | } | |
692 | } | |
693 | ||
9ec7dd18 | 694 | void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)) { |
9cad30fa JF |
695 | Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg)); |
696 | ||
697 | JSContextRef context(internal->context_); | |
698 | ||
699 | size_t count(internal->cif_.nargs); | |
700 | JSValueRef values[count]; | |
701 | ||
702 | for (size_t index(0); index != count; ++index) | |
703 | values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]); | |
704 | ||
9ec7dd18 | 705 | JSValueRef value(adapter(context, count, values, internal->function_)); |
9cad30fa JF |
706 | CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value); |
707 | } | |
708 | ||
9ec7dd18 JF |
709 | static JSValueRef FunctionAdapter_(JSContextRef context, size_t count, JSValueRef values[], JSObjectRef function) { |
710 | return CYCallAsFunction(context, function, NULL, count, values); | |
711 | } | |
712 | ||
713 | static void FunctionClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) { | |
714 | CYExecuteClosure(cif, result, arguments, arg, &FunctionAdapter_); | |
715 | } | |
716 | ||
9cad30fa JF |
717 | Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const char *type, void (*callback)(ffi_cif *, void *, void **, void *)) { |
718 | // XXX: in case of exceptions this will leak | |
719 | // XXX: in point of fact, this may /need/ to leak :( | |
720 | Closure_privateData *internal(new Closure_privateData(context, function, type)); | |
721 | ||
c5bce670 JF |
722 | #if defined(__APPLE__) && defined(__arm__) |
723 | void *executable; | |
724 | ffi_closure *writable(reinterpret_cast<ffi_closure *>(ffi_closure_alloc(sizeof(ffi_closure), &executable))); | |
725 | ||
726 | ffi_status status(ffi_prep_closure_loc(writable, &internal->cif_, callback, internal, executable)); | |
727 | _assert(status == FFI_OK); | |
728 | ||
729 | internal->value_ = executable; | |
730 | #else | |
9cad30fa JF |
731 | ffi_closure *closure((ffi_closure *) _syscall(mmap( |
732 | NULL, sizeof(ffi_closure), | |
733 | PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, | |
734 | -1, 0 | |
735 | ))); | |
736 | ||
737 | ffi_status status(ffi_prep_closure(closure, &internal->cif_, callback, internal)); | |
738 | _assert(status == FFI_OK); | |
739 | ||
740 | _syscall(mprotect(closure, sizeof(*closure), PROT_READ | PROT_EXEC)); | |
741 | ||
742 | internal->value_ = closure; | |
c5bce670 | 743 | #endif |
9cad30fa JF |
744 | |
745 | return internal; | |
746 | } | |
747 | ||
748 | static JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) { | |
749 | Closure_privateData *internal(CYMakeFunctor_(context, function, type, &FunctionClosure_)); | |
750 | JSObjectRef object(JSObjectMake(context, Functor_, internal)); | |
751 | // XXX: see above notes about needing to leak | |
752 | JSValueProtect(CYGetJSContext(context), object); | |
753 | return object; | |
754 | } | |
755 | ||
756 | JSObjectRef CYGetCachedObject(JSContextRef context, JSStringRef name) { | |
757 | return CYCastJSObject(context, CYGetProperty(context, CYCastJSObject(context, CYGetProperty(context, CYGetGlobalObject(context), cy_s)), name)); | |
758 | } | |
759 | ||
760 | static JSObjectRef CYMakeFunctor(JSContextRef context, JSValueRef value, const char *type) { | |
761 | JSObjectRef Function(CYGetCachedObject(context, CYJSString("Function"))); | |
762 | ||
763 | JSValueRef exception(NULL); | |
764 | bool function(JSValueIsInstanceOfConstructor(context, value, Function, &exception)); | |
765 | CYThrow(context, exception); | |
766 | ||
767 | if (function) { | |
768 | JSObjectRef function(CYCastJSObject(context, value)); | |
769 | return CYMakeFunctor(context, function, type); | |
770 | } else { | |
771 | void (*function)()(CYCastPointer<void (*)()>(context, value)); | |
772 | return CYMakeFunctor(context, function, type); | |
773 | } | |
774 | } | |
775 | ||
b799113b | 776 | static bool Index_(CYPool &pool, JSContextRef context, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) { |
9cad30fa JF |
777 | Type_privateData *typical(internal->type_); |
778 | sig::Type *type(typical->type_); | |
779 | if (type == NULL) | |
780 | return false; | |
781 | ||
782 | const char *name(CYPoolCString(pool, context, property)); | |
783 | size_t length(strlen(name)); | |
784 | double number(CYCastDouble(name, length)); | |
785 | ||
786 | size_t count(type->data.signature.count); | |
787 | ||
788 | if (std::isnan(number)) { | |
789 | if (property == NULL) | |
790 | return false; | |
791 | ||
792 | sig::Element *elements(type->data.signature.elements); | |
793 | ||
794 | for (size_t local(0); local != count; ++local) { | |
795 | sig::Element *element(&elements[local]); | |
796 | if (element->name != NULL && strcmp(name, element->name) == 0) { | |
797 | index = local; | |
798 | goto base; | |
799 | } | |
800 | } | |
801 | ||
802 | return false; | |
803 | } else { | |
804 | index = static_cast<ssize_t>(number); | |
805 | if (index != number || index < 0 || static_cast<size_t>(index) >= count) | |
806 | return false; | |
807 | } | |
808 | ||
809 | base: | |
810 | ffi_type **elements(typical->GetFFI()->elements); | |
811 | ||
812 | base = reinterpret_cast<uint8_t *>(internal->value_); | |
813 | for (ssize_t local(0); local != index; ++local) | |
814 | base += elements[local]->size; | |
815 | ||
816 | return true; | |
817 | } | |
818 | ||
819 | static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
820 | CYPool pool; | |
821 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
822 | ||
823 | if (JSStringIsEqual(property, length_s)) | |
824 | return internal->length_ == _not(size_t) ? CYJSUndefined(context) : CYCastJSValue(context, internal->length_); | |
825 | ||
826 | Type_privateData *typical(internal->type_); | |
827 | ||
828 | if (typical->type_ == NULL) | |
829 | return NULL; | |
830 | ||
831 | ssize_t offset; | |
832 | if (JSStringIsEqualToUTF8CString(property, "$cyi")) | |
833 | offset = 0; | |
834 | else if (!CYGetOffset(pool, context, property, offset)) | |
835 | return NULL; | |
836 | ||
837 | ffi_type *ffi(typical->GetFFI()); | |
838 | ||
839 | uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_)); | |
840 | base += ffi->size * offset; | |
841 | ||
842 | JSObjectRef owner(internal->GetOwner() ?: object); | |
843 | return CYFromFFI(context, typical->type_, ffi, base, false, owner); | |
55c6d6ab | 844 | } CYCatch(NULL) } |
9cad30fa JF |
845 | |
846 | static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { | |
847 | CYPool pool; | |
848 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object))); | |
849 | Type_privateData *typical(internal->type_); | |
850 | ||
851 | if (typical->type_ == NULL) | |
55c6d6ab | 852 | return false; |
9cad30fa JF |
853 | |
854 | ssize_t offset; | |
855 | if (JSStringIsEqualToUTF8CString(property, "$cyi")) | |
856 | offset = 0; | |
857 | else if (!CYGetOffset(pool, context, property, offset)) | |
55c6d6ab | 858 | return false; |
9cad30fa JF |
859 | |
860 | ffi_type *ffi(typical->GetFFI()); | |
861 | ||
862 | uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_)); | |
863 | base += ffi->size * offset; | |
864 | ||
865 | CYPoolFFI(NULL, context, typical->type_, ffi, base, value); | |
866 | return true; | |
55c6d6ab | 867 | } CYCatch(false) } |
9cad30fa JF |
868 | |
869 | static JSValueRef Struct_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
870 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(_this))); | |
871 | Type_privateData *typical(internal->type_); | |
872 | return CYMakePointer(context, internal->value_, _not(size_t), typical->type_, typical->ffi_, _this); | |
873 | } | |
874 | ||
875 | static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
876 | CYPool pool; | |
877 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
878 | Type_privateData *typical(internal->type_); | |
879 | ||
880 | ssize_t index; | |
881 | uint8_t *base; | |
882 | ||
883 | if (!Index_(pool, context, internal, property, index, base)) | |
884 | return NULL; | |
885 | ||
886 | JSObjectRef owner(internal->GetOwner() ?: object); | |
887 | ||
888 | return CYFromFFI(context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, false, owner); | |
55c6d6ab | 889 | } CYCatch(NULL) } |
9cad30fa JF |
890 | |
891 | static bool Struct_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { | |
892 | CYPool pool; | |
893 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
894 | Type_privateData *typical(internal->type_); | |
895 | ||
896 | ssize_t index; | |
897 | uint8_t *base; | |
898 | ||
899 | if (!Index_(pool, context, internal, property, index, base)) | |
900 | return false; | |
901 | ||
902 | CYPoolFFI(NULL, context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, value); | |
903 | return true; | |
55c6d6ab | 904 | } CYCatch(false) } |
9cad30fa JF |
905 | |
906 | static void Struct_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { | |
907 | Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object))); | |
908 | Type_privateData *typical(internal->type_); | |
909 | sig::Type *type(typical->type_); | |
910 | ||
911 | if (type == NULL) | |
912 | return; | |
913 | ||
914 | size_t count(type->data.signature.count); | |
915 | sig::Element *elements(type->data.signature.elements); | |
916 | ||
917 | char number[32]; | |
918 | ||
919 | for (size_t index(0); index != count; ++index) { | |
920 | const char *name; | |
921 | name = elements[index].name; | |
922 | ||
923 | if (name == NULL) { | |
924 | sprintf(number, "%zu", index); | |
925 | name = number; | |
926 | } | |
927 | ||
928 | JSPropertyNameAccumulatorAddName(names, CYJSString(name)); | |
929 | } | |
930 | } | |
931 | ||
b799113b | 932 | JSValueRef CYCallFunction(CYPool &pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) { CYTry { |
9cad30fa JF |
933 | if (setups + count != signature->count - 1) |
934 | throw CYJSError(context, "incorrect number of arguments to ffi function"); | |
935 | ||
936 | size_t size(setups + count); | |
937 | void *values[size]; | |
938 | memcpy(values, setup, sizeof(void *) * setups); | |
939 | ||
940 | for (size_t index(setups); index != size; ++index) { | |
941 | sig::Element *element(&signature->elements[index + 1]); | |
942 | ffi_type *ffi(cif->arg_types[index]); | |
943 | // XXX: alignment? | |
944 | values[index] = new(pool) uint8_t[ffi->size]; | |
b799113b | 945 | CYPoolFFI(&pool, context, element->type, ffi, values[index], arguments[index - setups]); |
9cad30fa JF |
946 | } |
947 | ||
948 | uint8_t value[cif->rtype->size]; | |
949 | ||
950 | if (hooks_ != NULL && hooks_->CallFunction != NULL) | |
951 | (*hooks_->CallFunction)(context, cif, function, value, values); | |
952 | else | |
953 | ffi_call(cif, function, value, values); | |
954 | ||
955 | return CYFromFFI(context, signature->elements[0].type, cif->rtype, value, initialize); | |
55c6d6ab | 956 | } CYCatch(NULL) } |
9cad30fa JF |
957 | |
958 | static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
959 | CYPool pool; | |
960 | cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(object))); | |
961 | return CYCallFunction(pool, context, 0, NULL, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue()); | |
962 | } | |
963 | ||
8a23fb2e | 964 | JSObjectRef CYMakeType(JSContextRef context, const char *type) { |
9cad30fa JF |
965 | Type_privateData *internal(new Type_privateData(type)); |
966 | return JSObjectMake(context, Type_privateData::Class_, internal); | |
967 | } | |
968 | ||
8a23fb2e | 969 | JSObjectRef CYMakeType(JSContextRef context, sig::Type *type) { |
9cad30fa JF |
970 | Type_privateData *internal(new Type_privateData(type)); |
971 | return JSObjectMake(context, Type_privateData::Class_, internal); | |
972 | } | |
973 | ||
9cad30fa JF |
974 | static JSValueRef All_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { |
975 | JSObjectRef global(CYGetGlobalObject(context)); | |
976 | JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript")))); | |
26ef7a82 JF |
977 | JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls")))); |
978 | ||
979 | for (size_t i(0), count(CYArrayLength(context, alls)); i != count; ++i) | |
980 | if (JSObjectRef space = CYCastJSObject(context, CYArrayGet(context, alls, count - i - 1))) | |
981 | if (JSValueRef value = CYGetProperty(context, space, property)) | |
982 | if (!JSValueIsUndefined(context, value)) | |
983 | return value; | |
9cad30fa JF |
984 | |
985 | CYPool pool; | |
986 | CYUTF8String name(CYPoolUTF8String(pool, context, property)); | |
987 | ||
2f51d6ab JF |
988 | size_t length(name.size); |
989 | char keyed[length + 2]; | |
990 | memcpy(keyed + 1, name.data, length + 1); | |
991 | ||
992 | static const char *modes = "0124"; | |
993 | for (size_t i(0); i != 4; ++i) { | |
994 | char mode(modes[i]); | |
995 | keyed[0] = mode; | |
996 | ||
997 | if (CYBridgeEntry *entry = CYBridgeHash(keyed, length + 1)) | |
998 | switch (mode) { | |
999 | case '0': | |
1000 | return JSEvaluateScript(CYGetJSContext(context), CYJSString(entry->value_), NULL, NULL, 0, NULL); | |
1001 | ||
1002 | case '1': | |
9a98069f | 1003 | return CYMakeFunctor(context, name.data, entry->value_, &entry->cache_); |
2f51d6ab JF |
1004 | |
1005 | case '2': | |
1006 | if (void *symbol = CYCastSymbol(name.data)) { | |
1007 | // XXX: this is horrendously inefficient | |
1008 | sig::Signature signature; | |
1009 | sig::Parse(pool, &signature, entry->value_, &Structor_); | |
1010 | ffi_cif cif; | |
1011 | sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); | |
1012 | return CYFromFFI(context, signature.elements[0].type, cif.rtype, symbol); | |
1013 | } else return NULL; | |
1014 | ||
1015 | // XXX: implement case 3 | |
1016 | case '4': | |
1017 | return CYMakeType(context, entry->value_); | |
1018 | } | |
9cad30fa JF |
1019 | } |
1020 | ||
2f51d6ab | 1021 | return NULL; |
55c6d6ab | 1022 | } CYCatch(NULL) } |
9cad30fa | 1023 | |
26ef7a82 JF |
1024 | static void All_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { |
1025 | JSObjectRef global(CYGetGlobalObject(context)); | |
1026 | JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript")))); | |
1027 | JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls")))); | |
1028 | ||
1029 | for (size_t i(0), count(CYArrayLength(context, alls)); i != count; ++i) | |
1030 | if (JSObjectRef space = CYCastJSObject(context, CYArrayGet(context, alls, count - i - 1))) { | |
1031 | JSPropertyNameArrayRef subset(JSObjectCopyPropertyNames(context, space)); | |
1032 | for (size_t index(0), count(JSPropertyNameArrayGetCount(subset)); index != count; ++index) | |
1033 | JSPropertyNameAccumulatorAddName(names, JSPropertyNameArrayGetNameAtIndex(subset, index)); | |
1034 | JSPropertyNameArrayRelease(subset); | |
1035 | } | |
1036 | } | |
1037 | ||
9cad30fa JF |
1038 | static JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
1039 | if (count != 2) | |
5d422750 | 1040 | throw CYJSError(context, "incorrect number of arguments to Pointer constructor"); |
9cad30fa JF |
1041 | |
1042 | CYPool pool; | |
1043 | ||
1044 | void *value(CYCastPointer<void *>(context, arguments[0])); | |
1045 | const char *type(CYPoolCString(pool, context, arguments[1])); | |
1046 | ||
1047 | sig::Signature signature; | |
1048 | sig::Parse(pool, &signature, type, &Structor_); | |
1049 | ||
1050 | return CYMakePointer(context, value, _not(size_t), signature.elements[0].type, NULL, NULL); | |
55c6d6ab | 1051 | } CYCatch(NULL) } |
9cad30fa JF |
1052 | |
1053 | static JSObjectRef Type_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1054 | if (count != 1) | |
1055 | throw CYJSError(context, "incorrect number of arguments to Type constructor"); | |
1056 | CYPool pool; | |
1057 | const char *type(CYPoolCString(pool, context, arguments[0])); | |
1058 | return CYMakeType(context, type); | |
55c6d6ab | 1059 | } CYCatch(NULL) } |
9cad30fa | 1060 | |
85e90410 JF |
1061 | static JSValueRef Type_callAsFunction_arrayOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
1062 | if (count != 1) | |
1063 | throw CYJSError(context, "incorrect number of arguments to Type.arrayOf"); | |
1064 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1065 | ||
1066 | CYPool pool; | |
1067 | size_t index(CYGetIndex(pool, context, CYJSString(context, arguments[0]))); | |
1068 | if (index == _not(size_t)) | |
1069 | throw CYJSError(context, "invalid array size used with Type.arrayOf"); | |
9cad30fa JF |
1070 | |
1071 | sig::Type type; | |
85e90410 JF |
1072 | type.name = NULL; |
1073 | type.flags = 0; | |
9cad30fa | 1074 | |
85e90410 JF |
1075 | type.primitive = sig::array_P; |
1076 | type.data.data.type = internal->type_; | |
1077 | type.data.data.size = index; | |
1078 | ||
1079 | return CYMakeType(context, &type); | |
55c6d6ab | 1080 | } CYCatch(NULL) } |
9cad30fa | 1081 | |
fbc17268 JF |
1082 | static JSValueRef Type_callAsFunction_constant(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
1083 | if (count != 0) | |
1084 | throw CYJSError(context, "incorrect number of arguments to Type.constant"); | |
1085 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1086 | ||
1087 | sig::Type type(*internal->type_); | |
1088 | type.flags |= JOC_TYPE_CONST; | |
1089 | return CYMakeType(context, &type); | |
55c6d6ab | 1090 | } CYCatch(NULL) } |
fbc17268 | 1091 | |
85e90410 JF |
1092 | static JSValueRef Type_callAsFunction_pointerTo(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
1093 | if (count != 0) | |
1094 | throw CYJSError(context, "incorrect number of arguments to Type.pointerTo"); | |
1095 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1096 | ||
1097 | sig::Type type; | |
9cad30fa JF |
1098 | type.name = NULL; |
1099 | type.flags = 0; | |
1100 | ||
85e90410 | 1101 | type.primitive = sig::pointer_P; |
9cad30fa | 1102 | type.data.data.type = internal->type_; |
85e90410 | 1103 | type.data.data.size = 0; |
9cad30fa JF |
1104 | |
1105 | return CYMakeType(context, &type); | |
55c6d6ab | 1106 | } CYCatch(NULL) } |
9cad30fa | 1107 | |
21d5f610 JF |
1108 | static JSValueRef Type_callAsFunction_withName(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
1109 | if (count != 1) | |
1110 | throw CYJSError(context, "incorrect number of arguments to Type.withName"); | |
1111 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1112 | ||
1113 | CYPool pool; | |
1114 | const char *name(CYPoolCString(pool, context, arguments[0])); | |
1115 | ||
1116 | sig::Type type(*internal->type_); | |
1117 | type.name = name; | |
1118 | return CYMakeType(context, &type); | |
55c6d6ab | 1119 | } CYCatch(NULL) } |
21d5f610 | 1120 | |
9cad30fa | 1121 | static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
9cad30fa JF |
1122 | if (count != 1) |
1123 | throw CYJSError(context, "incorrect number of arguments to type cast function"); | |
1c3dd2c3 JF |
1124 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); |
1125 | ||
9cad30fa JF |
1126 | sig::Type *type(internal->type_); |
1127 | ffi_type *ffi(internal->GetFFI()); | |
1128 | // XXX: alignment? | |
1129 | uint8_t value[ffi->size]; | |
1130 | CYPool pool; | |
b799113b | 1131 | CYPoolFFI(&pool, context, type, ffi, value, arguments[0]); |
9cad30fa | 1132 | return CYFromFFI(context, type, ffi, value); |
55c6d6ab | 1133 | } CYCatch(NULL) } |
9cad30fa JF |
1134 | |
1135 | static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1136 | if (count != 0) | |
767e8255 | 1137 | throw CYJSError(context, "incorrect number of arguments to Type allocator"); |
9cad30fa JF |
1138 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); |
1139 | ||
1140 | sig::Type *type(internal->type_); | |
1141 | size_t length; | |
1142 | ||
1143 | if (type->primitive != sig::array_P) | |
1144 | length = _not(size_t); | |
1145 | else { | |
1146 | length = type->data.data.size; | |
1147 | type = type->data.data.type; | |
1148 | } | |
1149 | ||
1150 | void *value(malloc(internal->GetFFI()->size)); | |
1151 | return CYMakePointer(context, value, length, type, NULL, NULL); | |
55c6d6ab | 1152 | } CYCatch(NULL) } |
9cad30fa JF |
1153 | |
1154 | static JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1155 | if (count != 2) | |
1156 | throw CYJSError(context, "incorrect number of arguments to Functor constructor"); | |
1157 | CYPool pool; | |
1158 | const char *type(CYPoolCString(pool, context, arguments[1])); | |
1159 | return CYMakeFunctor(context, arguments[0], type); | |
55c6d6ab | 1160 | } CYCatch(NULL) } |
9cad30fa JF |
1161 | |
1162 | static JSValueRef CYValue_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1163 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this))); | |
1164 | return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_)); | |
55c6d6ab | 1165 | } CYCatch(NULL) } |
9cad30fa JF |
1166 | |
1167 | static JSValueRef CYValue_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
1168 | return CYValue_callAsFunction_valueOf(context, object, _this, count, arguments, exception); | |
1169 | } | |
1170 | ||
1171 | static JSValueRef CYValue_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1172 | CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this))); | |
1173 | char string[32]; | |
1174 | sprintf(string, "%p", internal->value_); | |
1175 | return CYCastJSValue(context, string); | |
55c6d6ab | 1176 | } CYCatch(NULL) } |
9cad30fa JF |
1177 | |
1178 | static JSValueRef Pointer_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1179 | Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(_this))); | |
1180 | if (internal->length_ != _not(size_t)) { | |
3b1534c0 | 1181 | JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array_prototype"))); |
9cad30fa JF |
1182 | JSObjectRef toCYON(CYCastJSObject(context, CYGetProperty(context, Array, toCYON_s))); |
1183 | return CYCallAsFunction(context, toCYON, _this, count, arguments); | |
1184 | } else { | |
1185 | char string[32]; | |
1186 | sprintf(string, "%p", internal->value_); | |
1187 | return CYCastJSValue(context, string); | |
1188 | } | |
55c6d6ab | 1189 | } CYCatch(NULL) } |
9cad30fa | 1190 | |
74e8c566 JF |
1191 | static JSValueRef Type_getProperty_alignment(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
1192 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); | |
1193 | return CYCastJSValue(context, internal->GetFFI()->alignment); | |
1194 | } | |
1195 | ||
21d5f610 JF |
1196 | static JSValueRef Type_getProperty_name(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
1197 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); | |
1198 | return CYCastJSValue(context, internal->type_->name); | |
1199 | } | |
1200 | ||
74e8c566 JF |
1201 | static JSValueRef Type_getProperty_size(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
1202 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object))); | |
1203 | return CYCastJSValue(context, internal->GetFFI()->size); | |
1204 | } | |
1205 | ||
9cad30fa JF |
1206 | static JSValueRef Type_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
1207 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1208 | CYPool pool; | |
1209 | const char *type(sig::Unparse(pool, internal->type_)); | |
1210 | return CYCastJSValue(context, CYJSString(type)); | |
55c6d6ab | 1211 | } CYCatch(NULL) } |
9cad30fa JF |
1212 | |
1213 | static JSValueRef Type_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1214 | Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this))); | |
1215 | CYPool pool; | |
1216 | const char *type(sig::Unparse(pool, internal->type_)); | |
4a8523e5 JF |
1217 | std::ostringstream str; |
1218 | CYStringify(str, type, strlen(type)); | |
0cbeddf8 | 1219 | char *cyon(pool.strcat("new Type(", str.str().c_str(), ")", NULL)); |
9cad30fa | 1220 | return CYCastJSValue(context, CYJSString(cyon)); |
55c6d6ab | 1221 | } CYCatch(NULL) } |
9cad30fa JF |
1222 | |
1223 | static JSValueRef Type_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
1224 | return Type_callAsFunction_toString(context, object, _this, count, arguments, exception); | |
1225 | } | |
1226 | ||
1227 | static JSStaticFunction Pointer_staticFunctions[4] = { | |
1228 | {"toCYON", &Pointer_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1229 | {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1230 | {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1231 | {NULL, NULL, 0} | |
1232 | }; | |
1233 | ||
1234 | static JSStaticFunction Struct_staticFunctions[2] = { | |
1235 | {"$cya", &Struct_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1236 | {NULL, NULL, 0} | |
1237 | }; | |
1238 | ||
1239 | static JSStaticFunction Functor_staticFunctions[4] = { | |
1240 | {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1241 | {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1242 | {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1243 | {NULL, NULL, 0} | |
1244 | }; | |
1245 | ||
1246 | namespace cy { | |
1247 | JSStaticFunction const * const Functor::StaticFunctions = Functor_staticFunctions; | |
1248 | } | |
1249 | ||
21d5f610 | 1250 | static JSStaticValue Type_staticValues[4] = { |
74e8c566 | 1251 | {"alignment", &Type_getProperty_alignment, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
21d5f610 | 1252 | {"name", &Type_getProperty_name, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
74e8c566 JF |
1253 | {"size", &Type_getProperty_size, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
1254 | {NULL, NULL, NULL, 0} | |
1255 | }; | |
1256 | ||
21d5f610 | 1257 | static JSStaticFunction Type_staticFunctions[8] = { |
85e90410 | 1258 | {"arrayOf", &Type_callAsFunction_arrayOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
fbc17268 | 1259 | {"constant", &Type_callAsFunction_constant, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
85e90410 | 1260 | {"pointerTo", &Type_callAsFunction_pointerTo, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
21d5f610 | 1261 | {"withName", &Type_callAsFunction_withName, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
9cad30fa JF |
1262 | {"toCYON", &Type_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
1263 | {"toJSON", &Type_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1264 | {"toString", &Type_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1265 | {NULL, NULL, 0} | |
1266 | }; | |
1267 | ||
1268 | static JSObjectRef (*JSObjectMakeArray$)(JSContextRef, size_t, const JSValueRef[], JSValueRef *); | |
1269 | ||
1270 | void CYSetArgs(int argc, const char *argv[]) { | |
1271 | JSContextRef context(CYGetJSContext()); | |
1272 | JSValueRef args[argc]; | |
1273 | for (int i(0); i != argc; ++i) | |
1274 | args[i] = CYCastJSValue(context, argv[i]); | |
1275 | ||
1276 | JSObjectRef array; | |
1277 | if (JSObjectMakeArray$ != NULL) { | |
1278 | JSValueRef exception(NULL); | |
1279 | array = (*JSObjectMakeArray$)(context, argc, args, &exception); | |
1280 | CYThrow(context, exception); | |
1281 | } else { | |
1282 | JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array"))); | |
1283 | JSValueRef value(CYCallAsFunction(context, Array, NULL, argc, args)); | |
1284 | array = CYCastJSObject(context, value); | |
1285 | } | |
1286 | ||
1287 | JSObjectRef System(CYGetCachedObject(context, CYJSString("System"))); | |
1288 | CYSetProperty(context, System, CYJSString("args"), array); | |
1289 | } | |
1290 | ||
1291 | JSObjectRef CYGetGlobalObject(JSContextRef context) { | |
1292 | return JSContextGetGlobalObject(context); | |
1293 | } | |
1294 | ||
b799113b | 1295 | const char *CYExecute(CYPool &pool, CYUTF8String code) { |
9cad30fa JF |
1296 | JSContextRef context(CYGetJSContext()); |
1297 | JSValueRef exception(NULL), result; | |
1298 | ||
1299 | void *handle; | |
1300 | if (hooks_ != NULL && hooks_->ExecuteStart != NULL) | |
1301 | handle = (*hooks_->ExecuteStart)(context); | |
1302 | else | |
1303 | handle = NULL; | |
1304 | ||
1305 | const char *json; | |
1306 | ||
1307 | try { | |
1308 | result = JSEvaluateScript(context, CYJSString(code), NULL, NULL, 0, &exception); | |
1309 | } catch (const char *error) { | |
1310 | return error; | |
1311 | } | |
1312 | ||
9cc84a5a JF |
1313 | if (exception != NULL) error: |
1314 | return CYPoolCString(pool, context, CYJSString(context, exception)); | |
9cad30fa JF |
1315 | |
1316 | if (JSValueIsUndefined(context, result)) | |
1317 | return NULL; | |
1318 | ||
1319 | try { | |
1320 | json = CYPoolCCYON(pool, context, result, &exception); | |
1321 | } catch (const char *error) { | |
1322 | return error; | |
1323 | } | |
1324 | ||
1325 | if (exception != NULL) | |
1326 | goto error; | |
1327 | ||
1328 | CYSetProperty(context, CYGetGlobalObject(context), Result_, result); | |
1329 | ||
1330 | if (hooks_ != NULL && hooks_->ExecuteEnd != NULL) | |
1331 | (*hooks_->ExecuteEnd)(context, handle); | |
1332 | return json; | |
1333 | } | |
1334 | ||
1335 | extern "C" void CydgetSetupContext(JSGlobalContextRef context) { | |
1336 | CYSetupContext(context); | |
1337 | } | |
1338 | ||
09eee478 JF |
1339 | static bool initialized_ = false; |
1340 | ||
9cad30fa | 1341 | void CYInitializeDynamic() { |
09eee478 JF |
1342 | if (!initialized_) |
1343 | initialized_ = true; | |
1344 | else return; | |
1345 | ||
9cad30fa JF |
1346 | JSObjectMakeArray$ = reinterpret_cast<JSObjectRef (*)(JSContextRef, size_t, const JSValueRef[], JSValueRef *)>(dlsym(RTLD_DEFAULT, "JSObjectMakeArray")); |
1347 | ||
1348 | JSClassDefinition definition; | |
1349 | ||
1350 | definition = kJSClassDefinitionEmpty; | |
1351 | definition.className = "All"; | |
1352 | definition.getProperty = &All_getProperty; | |
26ef7a82 | 1353 | definition.getPropertyNames = &All_getPropertyNames; |
9cad30fa JF |
1354 | All_ = JSClassCreate(&definition); |
1355 | ||
1356 | definition = kJSClassDefinitionEmpty; | |
1357 | definition.className = "Context"; | |
1358 | definition.finalize = &CYFinalize; | |
1359 | Context_ = JSClassCreate(&definition); | |
1360 | ||
1361 | definition = kJSClassDefinitionEmpty; | |
1362 | definition.className = "Functor"; | |
1363 | definition.staticFunctions = cy::Functor::StaticFunctions; | |
1364 | definition.callAsFunction = &Functor_callAsFunction; | |
1365 | definition.finalize = &CYFinalize; | |
1366 | Functor_ = JSClassCreate(&definition); | |
1367 | ||
1368 | definition = kJSClassDefinitionEmpty; | |
1369 | definition.className = "Pointer"; | |
1370 | definition.staticFunctions = Pointer_staticFunctions; | |
1371 | definition.getProperty = &Pointer_getProperty; | |
1372 | definition.setProperty = &Pointer_setProperty; | |
1373 | definition.finalize = &CYFinalize; | |
1374 | Pointer_ = JSClassCreate(&definition); | |
1375 | ||
1376 | definition = kJSClassDefinitionEmpty; | |
1377 | definition.className = "Struct"; | |
1378 | definition.staticFunctions = Struct_staticFunctions; | |
1379 | definition.getProperty = &Struct_getProperty; | |
1380 | definition.setProperty = &Struct_setProperty; | |
1381 | definition.getPropertyNames = &Struct_getPropertyNames; | |
1382 | definition.finalize = &CYFinalize; | |
1383 | Struct_ = JSClassCreate(&definition); | |
1384 | ||
1385 | definition = kJSClassDefinitionEmpty; | |
1386 | definition.className = "Type"; | |
74e8c566 | 1387 | definition.staticValues = Type_staticValues; |
9cad30fa | 1388 | definition.staticFunctions = Type_staticFunctions; |
9cad30fa JF |
1389 | definition.callAsFunction = &Type_callAsFunction; |
1390 | definition.callAsConstructor = &Type_callAsConstructor; | |
1391 | definition.finalize = &CYFinalize; | |
1392 | Type_privateData::Class_ = JSClassCreate(&definition); | |
1393 | ||
1394 | definition = kJSClassDefinitionEmpty; | |
56a66df3 | 1395 | definition.className = "Global"; |
9cad30fa JF |
1396 | //definition.getProperty = &Global_getProperty; |
1397 | Global_ = JSClassCreate(&definition); | |
1398 | ||
1399 | Array_s = JSStringCreateWithUTF8CString("Array"); | |
1400 | cy_s = JSStringCreateWithUTF8CString("$cy"); | |
1401 | length_s = JSStringCreateWithUTF8CString("length"); | |
1402 | message_s = JSStringCreateWithUTF8CString("message"); | |
1403 | name_s = JSStringCreateWithUTF8CString("name"); | |
1404 | pop_s = JSStringCreateWithUTF8CString("pop"); | |
1405 | prototype_s = JSStringCreateWithUTF8CString("prototype"); | |
1406 | push_s = JSStringCreateWithUTF8CString("push"); | |
1407 | splice_s = JSStringCreateWithUTF8CString("splice"); | |
1408 | toCYON_s = JSStringCreateWithUTF8CString("toCYON"); | |
1409 | toJSON_s = JSStringCreateWithUTF8CString("toJSON"); | |
20ded97a | 1410 | toPointer_s = JSStringCreateWithUTF8CString("toPointer"); |
4cb8aa43 | 1411 | toString_s = JSStringCreateWithUTF8CString("toString"); |
9cad30fa JF |
1412 | |
1413 | Result_ = JSStringCreateWithUTF8CString("_"); | |
1414 | ||
1415 | if (hooks_ != NULL && hooks_->Initialize != NULL) | |
1416 | (*hooks_->Initialize)(); | |
1417 | } | |
1418 | ||
1419 | void CYThrow(JSContextRef context, JSValueRef value) { | |
1420 | if (value != NULL) | |
1421 | throw CYJSError(context, value); | |
1422 | } | |
1423 | ||
b799113b | 1424 | const char *CYJSError::PoolCString(CYPool &pool) const { |
9cad30fa JF |
1425 | // XXX: this used to be CYPoolCString |
1426 | return CYPoolCCYON(pool, context_, value_); | |
1427 | } | |
1428 | ||
1429 | JSValueRef CYJSError::CastJSValue(JSContextRef context) const { | |
1430 | // XXX: what if the context is different? | |
1431 | return value_; | |
1432 | } | |
1433 | ||
1434 | JSValueRef CYCastJSError(JSContextRef context, const char *message) { | |
1435 | JSObjectRef Error(CYGetCachedObject(context, CYJSString("Error"))); | |
1436 | ||
1437 | JSValueRef arguments[1] = {CYCastJSValue(context, message)}; | |
1438 | ||
1439 | JSValueRef exception(NULL); | |
1440 | JSValueRef value(JSObjectCallAsConstructor(context, Error, 1, arguments, &exception)); | |
1441 | CYThrow(context, exception); | |
1442 | ||
1443 | return value; | |
1444 | } | |
1445 | ||
1446 | JSValueRef CYPoolError::CastJSValue(JSContextRef context) const { | |
1447 | return CYCastJSError(context, message_); | |
1448 | } | |
1449 | ||
1450 | CYJSError::CYJSError(JSContextRef context, const char *format, ...) { | |
1451 | _assert(context != NULL); | |
1452 | ||
1453 | CYPool pool; | |
1454 | ||
1455 | va_list args; | |
1456 | va_start(args, format); | |
0cbeddf8 JF |
1457 | // XXX: there might be a beter way to think about this |
1458 | const char *message(pool.vsprintf(64, format, args)); | |
9cad30fa JF |
1459 | va_end(args); |
1460 | ||
1461 | value_ = CYCastJSError(context, message); | |
1462 | } | |
1463 | ||
1464 | JSGlobalContextRef CYGetJSContext(JSContextRef context) { | |
1465 | return reinterpret_cast<Context *>(JSObjectGetPrivate(CYCastJSObject(context, CYGetProperty(context, CYGetGlobalObject(context), cy_s))))->context_; | |
1466 | } | |
1467 | ||
1468 | extern "C" void CYSetupContext(JSGlobalContextRef context) { | |
26ef7a82 JF |
1469 | JSValueRef exception(NULL); |
1470 | ||
9cad30fa JF |
1471 | CYInitializeDynamic(); |
1472 | ||
1473 | JSObjectRef global(CYGetGlobalObject(context)); | |
1474 | ||
1475 | JSObjectRef cy(JSObjectMake(context, Context_, new Context(context))); | |
1476 | CYSetProperty(context, global, cy_s, cy, kJSPropertyAttributeDontEnum); | |
1477 | ||
1478 | /* Cache Globals {{{ */ | |
1479 | JSObjectRef Array(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array")))); | |
1480 | CYSetProperty(context, cy, CYJSString("Array"), Array); | |
1481 | ||
1482 | JSObjectRef Array_prototype(CYCastJSObject(context, CYGetProperty(context, Array, prototype_s))); | |
1483 | CYSetProperty(context, cy, CYJSString("Array_prototype"), Array_prototype); | |
1484 | ||
1485 | JSObjectRef Error(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Error")))); | |
1486 | CYSetProperty(context, cy, CYJSString("Error"), Error); | |
1487 | ||
1488 | JSObjectRef Function(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function")))); | |
1489 | CYSetProperty(context, cy, CYJSString("Function"), Function); | |
1490 | ||
1491 | JSObjectRef Function_prototype(CYCastJSObject(context, CYGetProperty(context, Function, prototype_s))); | |
1492 | CYSetProperty(context, cy, CYJSString("Function_prototype"), Function_prototype); | |
1493 | ||
1494 | JSObjectRef Object(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Object")))); | |
1495 | CYSetProperty(context, cy, CYJSString("Object"), Object); | |
1496 | ||
1497 | JSObjectRef Object_prototype(CYCastJSObject(context, CYGetProperty(context, Object, prototype_s))); | |
1498 | CYSetProperty(context, cy, CYJSString("Object_prototype"), Object_prototype); | |
1499 | ||
1500 | JSObjectRef String(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("String")))); | |
1501 | CYSetProperty(context, cy, CYJSString("String"), String); | |
4cb8aa43 JF |
1502 | |
1503 | JSObjectRef String_prototype(CYCastJSObject(context, CYGetProperty(context, String, prototype_s))); | |
1504 | CYSetProperty(context, cy, CYJSString("String_prototype"), String_prototype); | |
9cad30fa JF |
1505 | /* }}} */ |
1506 | ||
1507 | CYSetProperty(context, Array_prototype, toCYON_s, &Array_callAsFunction_toCYON, kJSPropertyAttributeDontEnum); | |
4cb8aa43 | 1508 | CYSetProperty(context, String_prototype, toCYON_s, &String_callAsFunction_toCYON, kJSPropertyAttributeDontEnum); |
9cad30fa JF |
1509 | |
1510 | JSObjectRef cycript(JSObjectMake(context, NULL, NULL)); | |
1511 | CYSetProperty(context, global, CYJSString("Cycript"), cycript); | |
1512 | CYSetProperty(context, cycript, CYJSString("gc"), &Cycript_gc_callAsFunction); | |
1513 | ||
1514 | JSObjectRef Functor(JSObjectMakeConstructor(context, Functor_, &Functor_new)); | |
1515 | JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Functor, prototype_s)), Function_prototype); | |
1516 | CYSetProperty(context, cycript, CYJSString("Functor"), Functor); | |
1517 | ||
1518 | CYSetProperty(context, cycript, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new)); | |
b63701b2 | 1519 | CYSetProperty(context, cycript, CYJSString("Type"), JSObjectMakeConstructor(context, Type_privateData::Class_, &Type_new)); |
9cad30fa JF |
1520 | |
1521 | JSObjectRef all(JSObjectMake(context, All_, NULL)); | |
1522 | CYSetProperty(context, cycript, CYJSString("all"), all); | |
1523 | ||
26ef7a82 JF |
1524 | JSObjectRef alls(JSObjectCallAsConstructor(context, Array, 0, NULL, &exception)); |
1525 | CYThrow(context, exception); | |
1526 | CYSetProperty(context, cycript, CYJSString("alls"), alls); | |
1527 | ||
56a66df3 JF |
1528 | if (true) { |
1529 | JSObjectRef last(NULL), curr(global); | |
9cad30fa | 1530 | |
56a66df3 JF |
1531 | goto next; for (JSValueRef next;;) { |
1532 | if (JSValueIsNull(context, next)) | |
1533 | break; | |
1534 | last = curr; | |
1535 | curr = CYCastJSObject(context, next); | |
1536 | next: | |
1537 | next = JSObjectGetPrototype(context, curr); | |
1538 | } | |
9cad30fa | 1539 | |
56a66df3 JF |
1540 | JSObjectSetPrototype(context, last, all); |
1541 | } | |
9cad30fa | 1542 | |
56a66df3 | 1543 | CYSetProperty(context, global, CYJSString("$cyq"), &$cyq, kJSPropertyAttributeDontEnum); |
9cad30fa JF |
1544 | |
1545 | JSObjectRef System(JSObjectMake(context, NULL, NULL)); | |
cdc80ff2 | 1546 | CYSetProperty(context, cy, CYJSString("System"), System); |
9cad30fa JF |
1547 | |
1548 | CYSetProperty(context, global, CYJSString("system"), System); | |
1549 | CYSetProperty(context, System, CYJSString("args"), CYJSNull(context)); | |
1550 | //CYSetProperty(context, System, CYJSString("global"), global); | |
1551 | CYSetProperty(context, System, CYJSString("print"), &System_print); | |
1552 | ||
a621ae76 JF |
1553 | if (CYBridgeEntry *entry = CYBridgeHash("1dlerror", 8)) |
1554 | entry->cache_ = new cy::Functor(entry->value_, reinterpret_cast<void (*)()>(&dlerror)); | |
1555 | ||
9cad30fa JF |
1556 | if (hooks_ != NULL && hooks_->SetupContext != NULL) |
1557 | (*hooks_->SetupContext)(context); | |
26ef7a82 JF |
1558 | |
1559 | CYArrayPush(context, alls, cycript); | |
9cad30fa JF |
1560 | } |
1561 | ||
1562 | JSGlobalContextRef CYGetJSContext() { | |
1563 | CYInitializeDynamic(); | |
1564 | ||
1565 | static JSGlobalContextRef context_; | |
1566 | ||
1567 | if (context_ == NULL) { | |
1568 | context_ = JSGlobalContextCreate(Global_); | |
1569 | CYSetupContext(context_); | |
1570 | } | |
1571 | ||
1572 | return context_; | |
1573 | } |