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