-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExecute.cpp
2648 lines (2131 loc) · 107 KB
/
Execute.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Cycript - The Truly Universal Scripting Language
* Copyright (C) 2009-2016 Jay Freeman (saurik)
*/
/* GNU Affero General Public License, Version 3 {{{ */
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */
#include "cycript.hpp"
#include <iostream>
#include <set>
#include <map>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <dlfcn.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sqlite3.h>
#include "sig/parse.hpp"
#include "sig/ffi_type.hpp"
#include "Bridge.hpp"
#include "Code.hpp"
#include "Decode.hpp"
#include "Error.hpp"
#include "Execute.hpp"
#include "Internal.hpp"
#include "JavaScript.hpp"
#include "Pooling.hpp"
#include "String.hpp"
const char *sqlite3_column_string(sqlite3_stmt *stmt, int n) {
return reinterpret_cast<const char *>(sqlite3_column_text(stmt, n));
}
char *sqlite3_column_pooled(CYPool &pool, sqlite3_stmt *stmt, int n) {
if (const char *value = sqlite3_column_string(stmt, n))
return pool.strdup(value);
else return NULL;
}
static std::vector<CYHook *> &GetHooks() {
static std::vector<CYHook *> hooks;
return hooks;
}
CYRegisterHook::CYRegisterHook(CYHook *hook) {
GetHooks().push_back(hook);
}
/* JavaScript Properties {{{ */
bool CYHasProperty(JSContextRef context, JSObjectRef object, JSStringRef name) {
return JSObjectHasProperty(context, object, name);
}
JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index) {
return _jsccall(JSObjectGetPropertyAtIndex, context, object, index);
}
JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name) {
return _jsccall(JSObjectGetProperty, context, object, name);
}
void CYSetProperty(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value) {
_jsccall(JSObjectSetPropertyAtIndex, context, object, index, value);
}
void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value, JSPropertyAttributes attributes) {
_jsccall(JSObjectSetProperty, context, object, name, value, attributes);
}
void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef (*callback)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *), JSPropertyAttributes attributes) {
CYSetProperty(context, object, name, JSObjectMakeFunctionWithCallback(context, name, callback), attributes);
}
JSObjectRef CYGetPrototype(JSContextRef context, JSObjectRef object) {
return CYCastJSObject(context, JSObjectGetPrototype(context, object));
}
void CYSetPrototype(JSContextRef context, JSObjectRef object, JSValueRef value) {
_assert(!JSValueIsUndefined(context, value));
JSObjectSetPrototype(context, object, value);
_assert(CYIsStrictEqual(context, JSObjectGetPrototype(context, object), value));
}
/* }}} */
/* JavaScript Strings {{{ */
JSStringRef CYCopyJSString(const char *value) {
return value == NULL ? NULL : JSStringCreateWithUTF8CString(value);
}
JSStringRef CYCopyJSString(JSStringRef value) {
return value == NULL ? NULL : JSStringRetain(value);
}
JSStringRef CYCopyJSString(CYUTF8String value) {
if (memchr(value.data, '\0', value.size) != NULL) {
CYPool pool;
return CYCopyJSString(CYPoolUTF16String(pool, value));
} else if (value.data[value.size] != '\0') {
CYPool pool;
return CYCopyJSString(pool.strmemdup(value.data, value.size));
} else {
return CYCopyJSString(value.data);
}
}
JSStringRef CYCopyJSString(const std::string &value) {
return CYCopyJSString(CYUTF8String(value.c_str(), value.size()));
}
JSStringRef CYCopyJSString(CYUTF16String value) {
return JSStringCreateWithCharacters(value.data, value.size);
}
JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value) {
if (JSValueIsNull(context, value))
return NULL;
return _jsccall(JSValueToStringCopy, context, value);
}
CYUTF16String CYCastUTF16String(JSStringRef value) {
return CYUTF16String(JSStringGetCharactersPtr(value), JSStringGetLength(value));
}
CYUTF8String CYPoolUTF8String(CYPool &pool, JSContextRef context, JSStringRef value) {
return CYPoolUTF8String(pool, CYCastUTF16String(value));
}
const char *CYPoolCString(CYPool &pool, JSContextRef context, JSStringRef value) {
CYUTF8String utf8(CYPoolUTF8String(pool, context, value));
_assert(memchr(utf8.data, '\0', utf8.size) == NULL);
return utf8.data;
}
const char *CYPoolCString(CYPool &pool, JSContextRef context, JSValueRef value) {
return JSValueIsNull(context, value) ? NULL : CYPoolCString(pool, context, CYJSString(context, value));
}
/* }}} */
/* Index Offsets {{{ */
size_t CYGetIndex(CYPool &pool, JSContextRef context, JSStringRef value) {
return CYGetIndex(CYPoolUTF8String(pool, context, value));
}
/* }}} */
static JSObjectRef (*JSObjectMakeArray$)(JSContextRef, size_t, const JSValueRef[], JSValueRef *);
JSObjectRef CYObjectMakeArray(JSContextRef context, size_t length, const JSValueRef values[]) {
if (JSObjectMakeArray$ != NULL)
return _jsccall(*JSObjectMakeArray$, context, length, values);
JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array")));
bool wat(length == 1 && JSValueGetType(context, values[0]) == kJSTypeNumber);
JSValueRef value(CYCallAsFunction(context, Array, NULL, wat ? 0 : length, values));
JSObjectRef object(CYCastJSObject(context, value));
if (wat) CYArrayPush(context, object, 1, values);
return object;
}
static JSClassRef All_;
JSClassRef cy::Functor::Class_;
static JSClassRef Global_;
JSStringRef Array_s;
JSStringRef constructor_s;
JSStringRef cy_s;
JSStringRef cyi_s;
JSStringRef cyt_s;
JSStringRef cyt__s;
JSStringRef length_s;
JSStringRef message_s;
JSStringRef name_s;
JSStringRef pop_s;
JSStringRef prototype_s;
JSStringRef push_s;
JSStringRef splice_s;
JSStringRef toCYON_s;
JSStringRef toJSON_s;
JSStringRef toPointer_s;
JSStringRef toString_s;
JSStringRef weak_s;
static sqlite3 *database_;
static JSStringRef Result_;
void CYFinalize(JSObjectRef object) {
CYData *internal(reinterpret_cast<CYData *>(JSObjectGetPrivate(object)));
if (internal == NULL)
return;
_assert(internal->count_ != _not(unsigned));
if (--internal->count_ == 0)
delete internal;
}
sig::Type *Structor_(CYPool &pool, sig::Aggregate *aggregate) {
//_assert(false);
return aggregate;
}
struct Context :
CYRoot
{
JSGlobalContextRef context_;
Context(JSGlobalContextRef context) :
context_(context)
{
}
};
struct CArray :
CYRoot
{
void *value_;
CYProtect owner_;
Type_privateData *type_;
size_t length_;
CArray(void *value, size_t length, const sig::Type &type, ffi_type *ffi, JSContextRef context, JSObjectRef owner) :
value_(value),
owner_(context, owner),
type_(new(*pool_) Type_privateData(type, ffi)),
length_(length)
{
}
};
struct CString :
CYRoot
{
char *value_;
CYProtect owner_;
CString(char *value, JSContextRef context, JSObjectRef owner) :
value_(value),
owner_(context, owner)
{
}
};
struct Pointer :
CYRoot
{
void *value_;
CYProtect owner_;
Type_privateData *type_;
Pointer(void *value, const sig::Type &type, JSContextRef context, JSObjectRef owner) :
value_(value),
owner_(context, owner),
type_(new(*pool_) Type_privateData(type))
{
}
Pointer(void *value, const char *encoding, JSContextRef context, JSObjectRef owner) :
value_(value),
owner_(context, owner),
type_(new(*pool_) Type_privateData(encoding))
{
}
};
struct Struct_privateData :
CYRoot
{
void *value_;
CYProtect owner_;
Type_privateData *type_;
Struct_privateData(void *value, const sig::Type &type, ffi_type *ffi, JSContextRef context, JSObjectRef owner) :
value_(value),
owner_(context, owner),
type_(new(*pool_) Type_privateData(type, ffi))
{
if (owner == NULL) {
size_t size(ffi->size);
void *copy(pool_->malloc<void>(size, ffi->alignment));
memcpy(copy, value_, size);
value_ = copy;
}
}
};
static void *CYCastSymbol(const char *name) {
for (CYHook *hook : GetHooks())
if (hook->CastSymbol != NULL)
if (void *value = (*hook->CastSymbol)(name))
return value;
return dlsym(RTLD_DEFAULT, name);
}
JSValueRef CYCastJSValue(JSContextRef context, bool value) {
return JSValueMakeBoolean(context, value);
}
JSValueRef CYCastJSValue(JSContextRef context, double value) {
return JSValueMakeNumber(context, value);
}
#define CYCastJSValue_(Type_) \
JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
_assert(static_cast<Type_>(static_cast<double>(value)) == value); \
return JSValueMakeNumber(context, static_cast<double>(value)); \
}
CYCastJSValue_(long double)
CYCastJSValue_(signed short int)
CYCastJSValue_(unsigned short int)
CYCastJSValue_(signed int)
CYCastJSValue_(unsigned int)
CYCastJSValue_(signed long int)
CYCastJSValue_(unsigned long int)
CYCastJSValue_(signed long long int)
CYCastJSValue_(unsigned long long int)
#ifdef __SIZEOF_INT128__
CYCastJSValue_(signed __int128)
CYCastJSValue_(unsigned __int128)
#endif
JSValueRef CYJSUndefined(JSContextRef context) {
return JSValueMakeUndefined(context);
}
double CYCastDouble(JSContextRef context, JSValueRef value) {
return _jsccall(JSValueToNumber, context, value);
}
bool CYCastBool(JSContextRef context, JSValueRef value) {
return JSValueToBoolean(context, value);
}
JSValueRef CYJSNull(JSContextRef context) {
return JSValueMakeNull(context);
}
JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value) {
return value == NULL ? CYJSNull(context) : JSValueMakeString(context, value);
}
JSValueRef CYCastJSValue(JSContextRef context, const char *value) {
return CYCastJSValue(context, CYJSString(value));
}
JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) {
return _jsccall(JSValueToObject, context, value);
}
JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, const JSValueRef arguments[]) {
return _jsccall(JSObjectCallAsFunction, context, function, _this, count, arguments);
}
bool CYIsCallable(JSContextRef context, JSValueRef value) {
return value != NULL && JSValueIsObject(context, value) && JSObjectIsFunction(context, (JSObjectRef) value);
}
bool CYIsEqual(JSContextRef context, JSValueRef lhs, JSValueRef rhs) {
return _jsccall(JSValueIsEqual, context, lhs, rhs);
}
bool CYIsStrictEqual(JSContextRef context, JSValueRef lhs, JSValueRef rhs) {
return JSValueIsStrictEqual(context, lhs, rhs);
}
size_t CYArrayLength(JSContextRef context, JSObjectRef array) {
return CYCastDouble(context, CYGetProperty(context, array, length_s));
}
JSValueRef CYArrayGet(JSContextRef context, JSObjectRef array, size_t index) {
return _jsccall(JSObjectGetPropertyAtIndex, context, array, index);
}
void CYArrayPush(JSContextRef context, JSObjectRef array, size_t length, const JSValueRef arguments[]) {
JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array_prototype")));
_jsccall(JSObjectCallAsFunction, context, CYCastJSObject(context, CYGetProperty(context, Array, push_s)), array, length, arguments);
}
void CYArrayPush(JSContextRef context, JSObjectRef array, JSValueRef value) {
return CYArrayPush(context, array, 1, &value);
}
static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
FILE *file(stdout);
if (count == 0)
fputc('\n', file);
else {
CYPool pool;
CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, arguments[0])));
fwrite(string.data, string.size, 1, file);
}
fflush(file);
return CYJSUndefined(context);
} CYCatch(NULL) }
static JSValueRef Global_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
FILE *file(stdout);
CYPool pool;
for (size_t i(0); i != count; ++i) {
if (i != 0)
fputc(' ', file);
CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, arguments[i])));
fwrite(string.data, string.size, 1, file);
}
fputc('\n', file);
fflush(file);
return CYJSUndefined(context);
} CYCatch(NULL) }
static void (*JSSynchronousGarbageCollectForDebugging$)(JSContextRef);
_visible void CYGarbageCollect(JSContextRef context) {
(JSSynchronousGarbageCollectForDebugging$ ?: &JSGarbageCollect)(context);
}
static JSValueRef Cycript_compile_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
CYPool pool;
CYUTF8String before(CYPoolUTF8String(pool, context, CYJSString(context, arguments[0])));
CYUTF8String after(CYPoolCode(pool, before));
return CYCastJSValue(context, CYJSString(after));
} CYCatch_(NULL, "SyntaxError") }
static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
CYGarbageCollect(context);
return CYJSUndefined(context);
} CYCatch(NULL) }
const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value, std::set<void *> &objects, JSValueRef *exception) { CYTry {
switch (JSType type = JSValueGetType(context, value)) {
case kJSTypeUndefined:
return "undefined";
case kJSTypeNull:
return "null";
case kJSTypeBoolean:
return CYCastBool(context, value) ? "true" : "false";
case kJSTypeNumber: {
std::ostringstream str;
CYNumerify(str, CYCastDouble(context, value));
std::string value(str.str());
return pool.strmemdup(value.c_str(), value.size());
} break;
case kJSTypeString: {
std::ostringstream str;
CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, value)));
CYStringify(str, string.data, string.size, CYStringifyModeCycript);
std::string value(str.str());
return pool.strmemdup(value.c_str(), value.size());
} break;
case kJSTypeObject:
return CYPoolCCYON(pool, context, (JSObjectRef) value, objects);
default:
throw CYJSError(context, "JSValueGetType() == 0x%x", type);
}
} CYCatch(NULL) }
const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value, std::set<void *> &objects) {
return _jsccall(CYPoolCCYON, pool, context, value, objects);
}
const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value, std::set<void *> *objects) {
if (objects != NULL)
return CYPoolCCYON(pool, context, value, *objects);
else {
std::set<void *> objects;
return CYPoolCCYON(pool, context, value, objects);
}
}
const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object, std::set<void *> &objects) {
JSValueRef toCYON(CYGetProperty(context, object, toCYON_s));
if (CYIsCallable(context, toCYON)) {
// XXX: this needs to be abstracted behind some kind of function
JSValueRef arguments[1] = {CYCastJSValue(context, reinterpret_cast<uintptr_t>(&objects))};
JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toCYON, object, 1, arguments));
_assert(value != NULL);
return CYPoolCString(pool, context, value);
}
JSValueRef toJSON(CYGetProperty(context, object, toJSON_s));
if (CYIsCallable(context, toJSON)) {
JSValueRef arguments[1] = {CYCastJSValue(context, CYJSString(""))};
return _jsccall(CYPoolCCYON, pool, context, CYCallAsFunction(context, (JSObjectRef) toJSON, object, 1, arguments), objects);
}
if (JSObjectIsFunction(context, object)) {
JSValueRef toString(CYGetProperty(context, object, toString_s));
if (CYIsCallable(context, toString)) {
JSValueRef arguments[1] = {CYCastJSValue(context, CYJSString(""))};
JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toString, object, 1, arguments));
_assert(value != NULL);
return CYPoolCString(pool, context, value);
}
}
_assert(objects.insert(object).second);
std::ostringstream str;
JSValueRef value(CYGetProperty(context, object, constructor_s));
if (JSValueIsObject(context, value)) {
JSObjectRef constructor(CYCastJSObject(context, value));
JSValueRef theory(CYGetProperty(context, constructor, prototype_s));
JSValueRef practice(JSObjectGetPrototype(context, object));
if (CYIsStrictEqual(context, theory, practice)) {
JSValueRef name(CYGetProperty(context, constructor, name_s));
if (!JSValueIsUndefined(context, name)) {
auto utf8(CYPoolUTF8String(pool, context, CYJSString(context, name)));
if (utf8 != "Object")
str << "new" << ' ' << utf8;
}
}
}
str << '{';
// XXX: this is, sadly, going to leak
JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context, object));
bool comma(false);
for (size_t index(0), count(JSPropertyNameArrayGetCount(names)); index != count; ++index) {
if (comma)
str << ',';
else
comma = true;
JSStringRef name(JSPropertyNameArrayGetNameAtIndex(names, index));
CYUTF8String string(CYPoolUTF8String(pool, context, name));
if (CYIsKey(string))
str << string.data;
else
CYStringify(str, string.data, string.size, CYStringifyModeLegacy);
str << ':';
try {
JSValueRef value(CYGetProperty(context, object, name));
str << CYPoolCCYON(pool, context, value, objects);
} catch (const CYException &error) {
str << "@error";
}
}
JSPropertyNameArrayRelease(names);
str << '}';
std::string string(str.str());
return pool.strmemdup(string.c_str(), string.size());
}
std::set<void *> *CYCastObjects(JSContextRef context, JSObjectRef _this, size_t count, const JSValueRef arguments[]) {
if (count == 0)
return NULL;
return CYCastPointer<std::set<void *> *>(context, arguments[0]);
}
static JSValueRef Array_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
std::set<void *> *objects(CYCastObjects(context, _this, count, arguments));
// XXX: this is horribly inefficient
std::set<void *> backup;
if (objects == NULL)
objects = &backup;
CYPool pool;
std::ostringstream str;
str << '[';
JSValueRef length(CYGetProperty(context, _this, length_s));
bool comma(false);
for (size_t index(0), count(CYCastDouble(context, length)); index != count; ++index) {
if (comma)
str << ',';
else
comma = true;
try {
JSValueRef value(CYGetProperty(context, _this, index));
if (!JSValueIsUndefined(context, value))
str << CYPoolCCYON(pool, context, value, *objects);
else {
str << ',';
comma = false;
}
} catch (const CYException &error) {
str << "@error";
}
}
str << ']';
std::string value(str.str());
return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size())));
} CYCatch(NULL) }
static JSValueRef String_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
CYPool pool;
std::ostringstream str;
CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, _this)));
CYStringify(str, string.data, string.size, CYStringifyModeCycript);
std::string value(str.str());
return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size())));
} CYCatch(NULL) }
JSObjectRef CYMakePointer(JSContextRef context, void *pointer, const sig::Type &type, ffi_type *ffi, JSObjectRef owner) {
return CYPrivate<Pointer>::Make(context, pointer, type, context, owner);
}
static JSValueRef CYMakeFunctor(JSContextRef context, void (*function)(), bool variadic, const sig::Signature &signature) {
if (function == NULL)
return CYJSNull(context);
return JSObjectMake(context, cy::Functor::Class_, new cy::Functor(function, variadic, signature));
}
// XXX: remove this, as it is really stupid
static JSObjectRef CYMakeFunctor(JSContextRef context, const char *symbol, const char *encoding) {
void (*function)()(reinterpret_cast<void (*)()>(CYCastSymbol(symbol)));
if (function == NULL)
return NULL;
cy::Functor *internal(new cy::Functor(function, encoding));
++internal->count_;
return JSObjectMake(context, cy::Functor::Class_, internal);
}
bool CYGetOffset(CYPool &pool, JSContextRef context, JSStringRef value, ssize_t &index) {
return CYGetOffset(CYPoolCString(pool, context, value), index);
}
// XXX: this is a horrible back added for CFType
void *CYCastPointerEx_(JSContextRef context, JSObjectRef value) {
JSObjectRef object((JSObjectRef) value);
if (JSValueIsObjectOfClass(context, value, CYPrivate<Pointer>::Class_)) {
Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
return internal->value_;
}
JSValueRef toPointer(CYGetProperty(context, object, toPointer_s));
if (CYIsCallable(context, toPointer)) {
JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toPointer, object, 0, NULL));
_assert(value != NULL);
return CYCastPointer_(context, value);
}
return NULL;
}
void *CYCastPointer_(JSContextRef context, JSValueRef value, bool *guess) {
if (value == NULL)
return NULL;
else switch (JSValueGetType(context, value)) {
case kJSTypeNull:
return NULL;
case kJSTypeObject: {
JSObjectRef object((JSObjectRef) value);
if (JSValueIsObjectOfClass(context, value, CYPrivate<Pointer>::Class_)) {
Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
return internal->value_;
}
JSValueRef toPointer(CYGetProperty(context, object, toPointer_s));
if (CYIsCallable(context, toPointer)) {
JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toPointer, object, 0, NULL));
_assert(value != NULL);
return CYCastPointer_(context, value, guess);
}
} default:
if (guess != NULL)
*guess = true;
case kJSTypeNumber:
double number(CYCastDouble(context, value));
if (!std::isnan(number))
return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number)));
if (guess == NULL)
throw CYJSError(context, "cannot convert value to pointer");
else {
*guess = true;
return NULL;
}
}
}
static JSValueRef FunctionAdapter_(JSContextRef context, size_t count, JSValueRef values[], JSObjectRef function);
namespace sig {
// XXX: this is somehow not quite a template :/
template <>
void Primitive<bool>::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const {
*reinterpret_cast<bool *>(data) = JSValueToBoolean(context, value);
}
#define CYPoolFFI_(Type_) \
template <> \
void Primitive<Type_>::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const { \
*reinterpret_cast<Type_ *>(data) = CYCastDouble(context, value); \
}
CYPoolFFI_(wchar_t)
CYPoolFFI_(float)
CYPoolFFI_(double)
CYPoolFFI_(long double)
CYPoolFFI_(signed char)
CYPoolFFI_(signed int)
CYPoolFFI_(signed long int)
CYPoolFFI_(signed long long int)
CYPoolFFI_(signed short int)
CYPoolFFI_(unsigned char)
CYPoolFFI_(unsigned int)
CYPoolFFI_(unsigned long int)
CYPoolFFI_(unsigned long long int)
CYPoolFFI_(unsigned short int)
#ifdef __SIZEOF_INT128__
CYPoolFFI_(signed __int128)
CYPoolFFI_(unsigned __int128)
#endif
template <>
void Primitive<char>::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const {
if (JSValueGetType(context, value) != kJSTypeString)
*reinterpret_cast<char *>(data) = CYCastDouble(context, value);
else {
CYJSString script(context, value);
auto string(CYCastUTF16String(script));
_assert(string.size == 1);
_assert((string.data[0] & 0xff) == string.data[0]);
*reinterpret_cast<char *>(data) = string.data[0];
}
}
void Void::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const {
_assert(JSValueIsUndefined(context, value));
}
void Unknown::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const {
_assert(false);
}
void String::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const {
bool guess(false);
*reinterpret_cast<const char **>(data) = CYCastPointer<const char *>(context, value, &guess);
if (guess && pool != NULL)
*reinterpret_cast<const char **>(data) = CYPoolCString(*pool, context, value);
}
void Bits::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const {
_assert(false);
}
static void CYArrayCopy(CYPool *pool, JSContextRef context, uint8_t *base, size_t length, const sig::Type &type, ffi_type *ffi, JSObjectRef object) {
for (size_t index(0); index != length; ++index) {
JSValueRef rhs(CYGetProperty(context, object, index));
if (JSValueIsUndefined(context, rhs))
throw CYJSError(context, "unable to extract array value");
type.PoolFFI(pool, context, ffi, base, rhs);
base += ffi->size;
}
}
void Pointer::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const {
bool guess(false);
*reinterpret_cast<void **>(data) = CYCastPointer<void *>(context, value, &guess);
if (!guess || pool == NULL || !JSValueIsObject(context, value))
return;
JSObjectRef object(CYCastJSObject(context, value));
if (sig::Function *function = dynamic_cast<sig::Function *>(&type)) {
_assert(!function->variadic);
auto internal(CYMakeFunctor_(context, object, function->signature, &FunctionAdapter_));
// XXX: see notes in Library.cpp about needing to leak
*reinterpret_cast<void (**)()>(data) = internal->value_;
} else if (CYHasProperty(context, object, length_s)) {
size_t length(CYArrayLength(context, object));
ffi_type *element(type.GetFFI(*pool));
size_t size(element->size * length);
uint8_t *base(pool->malloc<uint8_t>(size, element->alignment));
CYArrayCopy(pool, context, base, length, type, element, object);
*reinterpret_cast<void **>(data) = base;
}
}
void Array::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const {
if (size == 0)
return;
uint8_t *base(reinterpret_cast<uint8_t *>(data));
CYArrayCopy(pool, context, base, size, type, ffi->elements[0], CYCastJSObject(context, value));
}
void Enum::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const {
return type.PoolFFI(pool, context, ffi, data, value);
}
void Aggregate::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const {
_assert(!overlap);
_assert(signature.count != _not(size_t));
size_t offset(0);
uint8_t *base(reinterpret_cast<uint8_t *>(data));
JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL);
for (size_t index(0); index != signature.count; ++index) {
sig::Element *element(&signature.elements[index]);
ffi_type *field(ffi->elements[index]);
JSValueRef rhs;
if (aggregate == NULL)
rhs = value;
else {
rhs = CYGetProperty(context, aggregate, index);
if (JSValueIsUndefined(context, rhs)) {
if (element->name != NULL)
rhs = CYGetProperty(context, aggregate, CYJSString(element->name));
else
goto undefined;
if (JSValueIsUndefined(context, rhs)) undefined:
throw CYJSError(context, "unable to extract structure value");
}
}
element->type->PoolFFI(pool, context, field, base + offset, rhs);
offset += field->size;
CYAlign(offset, field->alignment);
}
}
void Function::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const {
_assert(false);
}
// XXX: this code is getting worse, not better :/
#define CYFromFFI_(Type_) \
template <> \
JSValueRef Primitive<Type_>::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { \
JSValueRef value(CYCastJSValue(context, *reinterpret_cast<Type_ *>(data))); \
JSObjectRef typed(_jsccall(JSObjectCallAsConstructor, context, CYGetCachedObject(context, CYJSString("Number")), 1, &value)); \
CYSetProperty(context, typed, cyt__s, CYMakeType(context, *this), kJSPropertyAttributeDontEnum); \
return typed; \
}
#define CYFromFFI_2(Type_) \
template <> \
JSValueRef Primitive<Type_>::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { \
return CYCastJSValue(context, *reinterpret_cast<Type_ *>(data)); \
}
CYFromFFI_(wchar_t)
CYFromFFI_(float)
CYFromFFI_(double)
CYFromFFI_(long double)
CYFromFFI_2(signed char)
CYFromFFI_2(signed int)
CYFromFFI_(signed long int)
CYFromFFI_(signed long long int)
CYFromFFI_2(signed short int)
CYFromFFI_2(unsigned char)
CYFromFFI_2(unsigned int)
CYFromFFI_(unsigned long int)
CYFromFFI_(unsigned long long int)
CYFromFFI_2(unsigned short int)
#ifdef __SIZEOF_INT128__
CYFromFFI_(signed __int128)
CYFromFFI_(unsigned __int128)
#endif
template <>
JSValueRef Primitive<bool>::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
return CYCastJSValue(context, *reinterpret_cast<bool *>(data));
}
template <>
JSValueRef Primitive<char>::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
uint16_t string(uint8_t(*reinterpret_cast<char *>(data)));
JSValueRef value(CYCastJSValue(context, CYJSString(CYUTF16String(&string, 1))));
JSObjectRef typed(_jsccall(JSObjectCallAsConstructor, context, CYGetCachedObject(context, CYJSString("String")), 1, &value));
CYSetProperty(context, typed, cyt_s, CYMakeType(context, sig::Primitive<char>()), kJSPropertyAttributeDontEnum);
CYSetPrototype(context, typed, CYGetCachedValue(context, CYJSString("Character_prototype")));
return typed;
}
JSValueRef Void::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
return CYJSUndefined(context);
}
JSValueRef Unknown::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
_assert(false);
}
JSValueRef String::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
if (char *value = *reinterpret_cast<char **>(data))
return CYPrivate<CString>::Make(context, value, context, owner);
return CYJSNull(context);
}
JSValueRef Bits::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
_assert(false);
}
JSValueRef Pointer::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
if (void *value = *reinterpret_cast<void **>(data))
return CYMakePointer(context, value, type, NULL, owner);
return CYJSNull(context);
}
JSValueRef Array::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
return CYPrivate<CArray>::Make(context, data, size, type, ffi->elements[0], context, owner);
}
JSValueRef Enum::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
return type.FromFFI(context, ffi, data, initialize, owner);
}
JSValueRef Aggregate::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
_assert(!overlap);
_assert(signature.count != _not(size_t));
return CYPrivate<Struct_privateData>::Make(context, data, *this, ffi, context, owner);
}
JSValueRef Function::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
return CYMakeFunctor(context, reinterpret_cast<void (*)()>(data), variadic, signature);
}
}
void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg) {
Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg));
JSContextRef context(internal->function_);
size_t count(internal->cif_.nargs);
JSValueRef values[count];
for (size_t index(0); index != count; ++index)
values[index] = internal->signature_.elements[1 + index].type->FromFFI(context, internal->cif_.arg_types[index], arguments[index]);
JSValueRef value(internal->adapter_(context, count, values, internal->function_));
internal->signature_.elements[0].type->PoolFFI(NULL, context, internal->cif_.rtype, result, value);
}
static JSValueRef FunctionAdapter_(JSContextRef context, size_t count, JSValueRef values[], JSObjectRef function) {
return CYCallAsFunction(context, function, NULL, count, values);
}
#if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
static void CYFreeFunctor(void *data) {
ffi_closure_free(data);
}
#else
static void CYFreeFunctor(void *data) {
_syscall(munmap(data, sizeof(ffi_closure)));
}
#endif
Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)) {
// XXX: in case of exceptions this will leak
Closure_privateData *internal(new Closure_privateData(context, function, adapter, signature));
#if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
void *executable;
ffi_closure *writable(reinterpret_cast<ffi_closure *>(ffi_closure_alloc(sizeof(ffi_closure), &executable)));