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