-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathemscripten.py
1253 lines (1128 loc) · 53.2 KB
/
emscripten.py
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
import inspect
import ast
import os
from rpython.javascript import json
from rpython.rtyper.lltypesystem import lltype, rffi
from rpython.translator.tool.cbuild import ExternalCompilationInfo
from rpython.rlib.entrypoint import entrypoint_highlevel
from rpython.rlib.rstring import replace
JSON = json
decompile = None
tempfile = None
imp = None
try:
import ast_decompiler
import tempfile
import random
import imp
def decompile(code, original, lineno):
if original.endswith('.pyc'): original = original[0:-1]
object_id = hex(id(code)) + repr(random.random())[2:]
code = ast_decompiler.decompile(code)
fd, path = tempfile.mkstemp(dir=os.getenv('PYPY_USESSION_DIR'))
file = open(path, 'w')
file.write('\n'.join([line + ' #File ' + original + ':' + str(lineno) for line in code.splitlines()]))
file.seek(0)
file.close()
#name = path.split('/')[-1]
module = imp.load_source(object_id, path)
return module
except:
pass
info = ExternalCompilationInfo(includes=['emscripten.h'])
run_script_string = rffi.llexternal('emscripten_run_script_string', [rffi.CCHARP], rffi.CCHARP, compilation_info=info)
run_script = rffi.llexternal('emscripten_run_script', [rffi.CCHARP], lltype.Void, compilation_info=info)
em_js = """
#include <emscripten.h>
/*EM_JS(const char*, run_safe_string, (const char* str), {
var global = typeof rpyGlobalArg !== "undefined" ? rpyGlobalArg : this;
});*/
EM_JS(const char*, run_safe_json, (const char* json, const char* variable), {
var global = typeof rpyGlobalArg !== "undefined" ? rpyGlobalArg : this;
variable = UTF8ToString(variable);
var object = JSON.parse(UTF8ToString(json));
object = global.deserialize_rpython_json(object);
try {
global[variable] = object;
}
catch (error) {
console.error('Trying to set variable ' + variable);
console.error(error);
throw error;
}
var type;
if (object === null) type = 'null';
else if (Array.isArray(object)) type = 'array';
else type = typeof object;
var lengthBytes = lengthBytesUTF8(type) + 1;
var stringOnWasmHeap = _malloc(lengthBytes);
stringToUTF8(type, stringOnWasmHeap, lengthBytes);
return stringOnWasmHeap;
});
EM_JS(const char*, run_safe_get, (const char* variable, const char* key, const char* new_variable), {
var global = typeof rpyGlobalArg !== "undefined" ? rpyGlobalArg : this;
key = UTF8ToString(key);
variable = UTF8ToString(variable);
new_variable = UTF8ToString(new_variable);
var object;
try {
object = global[variable][key];
}
catch (error) {
console.error('Trying to get variable ' + variable + ' and ' + key);
console.error(error);
throw error;
}
if (typeof object === 'function' && (!object.prototype || Object.getOwnPropertyNames(object.prototype).length === 1)) object = object.bind(global[variable]);
global[new_variable] = object;
var type;
if (object === null) type = 'null';
else if (Array.isArray(object)) type = 'array';
else type = typeof object;
var lengthBytes = lengthBytesUTF8(type) + 1;
var stringOnWasmHeap = _malloc(lengthBytes);
stringToUTF8(type, stringOnWasmHeap, lengthBytes);
return stringOnWasmHeap;
});
EM_JS(void, run_safe_set, (const char* variable, const char* key, const char* value), {
var global = typeof rpyGlobalArg !== "undefined" ? rpyGlobalArg : this;
key = UTF8ToString(key);
variable = UTF8ToString(variable);
value = global.deserialize_rpython_json(JSON.parse(UTF8ToString(value)));
try {
global[variable][key] = value;
}
catch (error) {
console.error('Trying to set variable ' + variable + ' and ' + key);
console.error(error);
throw error;
}
});
EM_JS(void, run_safe_del, (const char* variable, const char* key), {
var global = typeof rpyGlobalArg !== "undefined" ? rpyGlobalArg : this;
key = UTF8ToString(key);
variable = UTF8ToString(variable);
try {
delete global[variable][key];
}
catch (error) {
console.error('Trying to delete variable ' + variable + ' and ' + key);
console.error(error);
throw error;
}
});
EM_JS(const char*, run_safe_call, (const char* variable, const char* args, const char* new_variable), {
var global = typeof rpyGlobalArg !== "undefined" ? rpyGlobalArg : this;
variable = UTF8ToString(variable);
args = JSON.parse(UTF8ToString(args));
new_variable = UTF8ToString(new_variable);
deserialize_rpython_json(args);
var call;
try {
call = global[variable];
}
catch (error) {
console.error('Trying to get variable ' + variable);
console.error(error);
throw error;
}
var object;
try {
object = call(...args);
}
catch (error) {
console.error('Trying to call variable ' + variable);
console.error(error);
throw error;
}
global[new_variable] = object;
var type;
if (object === null) type = 'null';
else if (Array.isArray(object)) type = 'array';
else type = typeof object;
var lengthBytes = lengthBytesUTF8(type) + 1;
var stringOnWasmHeap = _malloc(lengthBytes);
stringToUTF8(type, stringOnWasmHeap, lengthBytes);
return stringOnWasmHeap;
});
EM_JS(const char*, run_safe_new, (const char* variable, const char* args, const char* new_variable), {
var global = typeof rpyGlobalArg !== "undefined" ? rpyGlobalArg : this;
variable = UTF8ToString(variable);
args = JSON.parse(UTF8ToString(args));
new_variable = UTF8ToString(new_variable);
deserialize_rpython_json(args);
var constructor;
try {
constructor = global[variable];
}
catch (error) {
console.error('Trying to get variable ' + variable);
console.error(error);
throw error;
}
var object;
try {
object = new constructor(...args);
}
catch (error) {
console.error('Trying to instantiate variable ' + variable);
console.error(error);
throw error;
}
global[new_variable] = object;
var type;
if (object === null) type = 'null';
else if (Array.isArray(object)) type = 'array';
else type = typeof object;
var lengthBytes = lengthBytesUTF8(type) + 1;
var stringOnWasmHeap = _malloc(lengthBytes);
stringToUTF8(type, stringOnWasmHeap, lengthBytes);
return stringOnWasmHeap;
});
EM_JS(void, run_safe_promise, (const char* parent_promise_id, const char* promise_id, const char* variables), {
var global = typeof rpyGlobalArg !== "undefined" ? rpyGlobalArg : this;
var args = [UTF8ToString(parent_promise_id), UTF8ToString(promise_id)]; //.map(function (string) {return allocate.length === 2 ? allocate(intArrayFromString(string), ALLOC_NORMAL) : allocate(intArrayFromString(string), 'i8', ALLOC_NORMAL)});
variables = JSON.parse(UTF8ToString(variables));
Promise.all(variables.map(async function (variable) {
var promise;
try {
promise = global[variable];
}
catch (error) {
console.error('Trying to get variable ' + variable);
console.error(error);
throw error;
}
var object = await promise;
if (object && object.then) object.rpython_resolved = true;
global[variable] = object;
})).then(function () {
//Module.asm.onresolve(...args);
Module.ccall('onresolve', 'null', ['string', 'string'], args);
}) //.catch(function (error) {console.error(error) /*|| throw error*/});
});
EM_JS(const char*, run_safe_type_update, (const char* variable), {
var global = typeof rpyGlobalArg !== "undefined" ? rpyGlobalArg : this;
variable = UTF8ToString(variable);
var object;
try {
object = global[variable];
}
catch (error) {
console.error('Trying to get variable ' + variable);
console.error(error);
throw error;
}
var type;
if (object === null) type = 'null';
else if (Array.isArray(object)) type = 'array';
else type = typeof object;
var lengthBytes = lengthBytesUTF8(type) + 1;
var stringOnWasmHeap = _malloc(lengthBytes);
stringToUTF8(type, stringOnWasmHeap, lengthBytes);
return stringOnWasmHeap;
});
EM_JS(const char*, create_function, (const char* id, const char* new_variable, const char* function_info), {
var global = typeof rpyGlobalArg !== "undefined" ? rpyGlobalArg : this;
var index = parseInt(UTF8ToString(id));
new_variable = UTF8ToString(new_variable);
function_info = UTF8ToString(function_info);
var new_object = {};
new_object[function_info] = (function (...args) {
if (!global.rpyfunction_call_args) global.rpyfunction_call_args = {};
var variable = 'rpyfunction_call_args';
global.rpyfunction_call_args[index] = args;
args = [variable, index.toString()]; //.map(function (string) {return allocate.length === 2 ? allocate(intArrayFromString(string), ALLOC_NORMAL) : allocate(intArrayFromString(string), 'i8', ALLOC_NORMAL)});
//Module.asm.onfunctioncall(...args);
Module.ccall('onfunctioncall', 'null', ['string', 'string'], args);
delete global.rpyfunction_call_args[index];
var result = global[global['rpyfunction_call_' + index]];
delete global['rpyfunction_call_' + index];
return result;
});
//object[function_info].rpython_info = function_info;
var object = (function (...args) {
try {
return new_object[function_info](...args);
}
catch (error) {
console.error('Trying to call ' + function_info);
throw error;
}
});
global[new_variable] = object;
var type;
if (object === null) type = 'null';
else if (Array.isArray(object)) type = 'array';
else type = typeof object;
var lengthBytes = lengthBytesUTF8(type) + 1;
var stringOnWasmHeap = _malloc(lengthBytes);
stringToUTF8(type, stringOnWasmHeap, lengthBytes);
return stringOnWasmHeap;
});
EM_JS(const char*, create_method, (const char* id, const char* method_id, const char* new_variable), {
var global = typeof rpyGlobalArg !== "undefined" ? rpyGlobalArg : this;
var index = parseInt(UTF8ToString(id));
new_variable = UTF8ToString(new_variable);
method_id = UTF8ToString(method_id);
var object = (function (...args) {
if (!global.rpymethod_call_args) global.rpymethod_call_args = {};
var variable = 'rpymethod_call_args';
global.rpymethod_call_args[index] = args;
args = [variable, index.toString()]; //.map(function (string) {return allocate.length === 2 ? allocate(intArrayFromString(string), ALLOC_NORMAL) : allocate(intArrayFromString(string), 'i8', ALLOC_NORMAL)});
//Module.asm['onmethodcall' + method_id](...args);
Module.ccall('onmethodcall' + method_id, 'null', ['string', 'string'], args);
delete global.rpymethod_call_args[index];
var result = global[global['rpymethod_call_' + index]];
delete global['rpymethod_call_' + index];
return result;
});
global[new_variable] = object;
var type;
if (object === null) type = 'null';
else if (Array.isArray(object)) type = 'array';
else type = typeof object;
var lengthBytes = lengthBytesUTF8(type) + 1;
var stringOnWasmHeap = _malloc(lengthBytes);
stringToUTF8(type, stringOnWasmHeap, lengthBytes);
return stringOnWasmHeap;
});
EM_JS(const char*, create_js_closure, (const char* func, const char* args, const char* new_variable), {
var global = typeof rpyGlobalArg !== "undefined" ? rpyGlobalArg : this;
new_variable = UTF8ToString(new_variable);
args = JSON.parse(UTF8ToString(args));
global.deserialize_rpython_json(args);
func = global.deserialize_rpython_json(UTF8ToString(func));
var object = (function (...new_args) {
return func(...args, ...new_args);
});
global[new_variable] = object;
var type;
if (object === null) type = 'null';
else if (Array.isArray(object)) type = 'array';
else type = typeof object;
var lengthBytes = lengthBytesUTF8(type) + 1;
var stringOnWasmHeap = _malloc(lengthBytes);
stringToUTF8(type, stringOnWasmHeap, lengthBytes);
return stringOnWasmHeap;
});
EM_JS(const char*, get_string, (const char* variable), {
var global = typeof rpyGlobalArg !== "undefined" ? rpyGlobalArg : this;
variable = UTF8ToString(variable);
var string = global[variable];
var result;
if (typeof string === 'string') result = string;
else if (string && string.toString) result = string.toString();
else result = String(string);
var lengthBytes = lengthBytesUTF8(result) + 1;
var stringOnWasmHeap = _malloc(lengthBytes);
stringToUTF8(result, stringOnWasmHeap, lengthBytes);
return stringOnWasmHeap;
});
EM_JS(const char*, get_integer, (const char* variable), {
var global = typeof rpyGlobalArg !== "undefined" ? rpyGlobalArg : this;
variable = UTF8ToString(variable);
var integer = parseInt(global[variable]);
if (isNaN(integer)) {
throw new Error(global[variable] + ' is not a number');
}
var result = integer.toString();
var lengthBytes = lengthBytesUTF8(result) + 1;
var stringOnWasmHeap = _malloc(lengthBytes);
stringToUTF8(result, stringOnWasmHeap, lengthBytes);
return stringOnWasmHeap;
});
EM_JS(const char*, get_float, (const char* variable), {
var global = typeof rpyGlobalArg !== "undefined" ? rpyGlobalArg : this;
variable = UTF8ToString(variable);
var float = parseFloat(global[variable]);
if (isNaN(float)) {
throw new Error(global[variable] + ' is not a number');
}
var result = float.toString();
var lengthBytes = lengthBytesUTF8(result) + 1;
var stringOnWasmHeap = _malloc(lengthBytes);
stringToUTF8(result, stringOnWasmHeap, lengthBytes);
return stringOnWasmHeap;
});
EM_JS(const char*, get_boolean, (const char* variable), {
var global = typeof rpyGlobalArg !== "undefined" ? rpyGlobalArg : this;
var result = global[variable];
if (typeof result !== 'boolean') result = !!result;
result = JSON.stringify(result);
var lengthBytes = lengthBytesUTF8(result) + 1;
var stringOnWasmHeap = _malloc(lengthBytes);
stringToUTF8(result, stringOnWasmHeap, lengthBytes);
return stringOnWasmHeap;
});
EM_JS(const char*, run_unsafe_code, (const char* code), {
code = UTF8ToString(code);
var global = typeof rpyGlobalArg !== "undefined" ? rpyGlobalArg : this;
if (!Module.wasmMemory) Module.wasmMemory = wasmMemory;
var eval_function;
try {
eval_function = eval(code);
}
catch (error) {
console.error('Trying to build eval code: ' + code);
throw error;
}
var result;
try {
result = String(eval_function(Module, global));
}
catch (error) {
console.error('Trying to execute eval code: ' + code);
throw error;
}
var lengthBytes = lengthBytesUTF8(result) + 1;
var stringOnWasmHeap = _malloc(lengthBytes);
stringToUTF8(result, stringOnWasmHeap, lengthBytes);
return stringOnWasmHeap;
});
"""
def rffi_1(function, void=False):
def wrapper(arg1, skip_gc=False):
if not skip_gc and globals.collector_id is None: run_garbage_collector()
pointer = function(rffi.str2charp(arg1))
if void: return
result = rffi.charp2str(pointer)
lltype.free(pointer, flavor='raw')
return result
return wrapper
def rffi_2(function, void=False):
def wrapper(arg1, arg2, skip_gc=False):
if not skip_gc and globals.collector_id is None: run_garbage_collector()
pointer = function(rffi.str2charp(arg1), rffi.str2charp(arg2))
if void: return
result = rffi.charp2str(pointer)
lltype.free(pointer, flavor='raw')
return result
return wrapper
def rffi_3(function, void=False):
def wrapper(arg1, arg2, arg3, skip_gc=False):
if not skip_gc and globals.collector_id is None: run_garbage_collector()
pointer = function(rffi.str2charp(arg1), rffi.str2charp(arg2), rffi.str2charp(arg3))
if void: return
result = rffi.charp2str(pointer)
lltype.free(pointer, flavor='raw')
return result
return wrapper
info = ExternalCompilationInfo(separate_module_sources=[em_js], includes=['src/em_js_api.h'])
#run_safe_string = rffi.llexternal('run_safe_string', [rffi.CCHARP], rffi.CCHARP, compilation_info=info)
run_safe_json = rffi_2(rffi.llexternal('run_safe_json', [rffi.CCHARP, rffi.CCHARP], rffi.CCHARP, compilation_info=info))
run_safe_get = rffi_3(rffi.llexternal('run_safe_get', [rffi.CCHARP, rffi.CCHARP, rffi.CCHARP], rffi.CCHARP, compilation_info=info))
run_safe_set = rffi_3(rffi.llexternal('run_safe_set', [rffi.CCHARP, rffi.CCHARP, rffi.CCHARP], lltype.Void, compilation_info=info), void=True)
run_safe_del = rffi_2(rffi.llexternal('run_safe_del', [rffi.CCHARP, rffi.CCHARP], lltype.Void, compilation_info=info), void=True)
run_safe_call = rffi_3(rffi.llexternal('run_safe_call', [rffi.CCHARP, rffi.CCHARP, rffi.CCHARP], rffi.CCHARP, compilation_info=info))
run_safe_new = rffi_3(rffi.llexternal('run_safe_new', [rffi.CCHARP, rffi.CCHARP, rffi.CCHARP], rffi.CCHARP, compilation_info=info))
run_safe_promise = rffi_3(rffi.llexternal('run_safe_promise', [rffi.CCHARP, rffi.CCHARP, rffi.CCHARP], lltype.Void, compilation_info=info), void=True)
run_safe_type_update = rffi_1(rffi.llexternal('run_safe_type_update', [rffi.CCHARP], rffi.CCHARP, compilation_info=info))
run_unsafe_code = rffi_1(rffi.llexternal('run_unsafe_code', [rffi.CCHARP], rffi.CCHARP, compilation_info=info))
create_function = rffi_3(rffi.llexternal('create_function', [rffi.CCHARP, rffi.CCHARP, rffi.CCHARP], rffi.CCHARP, compilation_info=info))
create_method = rffi_3(rffi.llexternal('create_method', [rffi.CCHARP, rffi.CCHARP, rffi.CCHARP], rffi.CCHARP, compilation_info=info))
create_js_closure = rffi_3(rffi.llexternal('create_js_closure', [rffi.CCHARP, rffi.CCHARP, rffi.CCHARP], rffi.CCHARP, compilation_info=info))
get_string = rffi_1(rffi.llexternal('get_string', [rffi.CCHARP], rffi.CCHARP, compilation_info=info))
get_integer = rffi_1(rffi.llexternal('get_integer', [rffi.CCHARP], rffi.CCHARP, compilation_info=info))
get_float = rffi_1(rffi.llexternal('get_float', [rffi.CCHARP], rffi.CCHARP, compilation_info=info))
get_boolean = rffi_1(rffi.llexternal('get_boolean', [rffi.CCHARP], rffi.CCHARP, compilation_info=info))
def run_javascript(code, returns=False, skip_gc=False):
if not skip_gc and globals.collector_id is None: run_garbage_collector()
code = '(function(Module, global) {' + code + '})'
return run_unsafe_code(code)
"""if returns:
pointer = run_script_string(rffi.str2charp(code))
result = rffi.charp2str(pointer)
lltype.free(pointer, flavor='raw')
return result
run_script(rffi.str2charp(code))
return None"""
def resolve_next_event(parent_id, child_id): return
#Extra JSON Types for interfacing, it is recommended to use JSON.from* APIs instead of creating fat pointers from these Object.from* APIs. This APIs is used to mix primitive types to be used on Javascript-side
def rpyobject(function):
def wrapper(value):
return Object(function(value), safe_json=True)
return wrapper
@rpyobject
def toString(value):
#if value is None: return 'null'
return json.fromString(value)
toStr = toString
@rpyobject
def toInt(value):
#return '%s' % (value)
return json.fromInteger(value)
toInteger = toInt
@rpyobject
def toFloat(value):
#return repr(value)
return json.fromFloat(value)
@rpyobject
def toBoolean(value):
#return 'true' if value == True else 'false' if value == False else 'null'
return json.fromBoolean(value)
toBool = toBoolean
@rpyobject
def toList(value):
return json.fromList(value)
@rpyobject
def toDict(value):
return json.fromDict(value)
functions = {}
function_template = '''
(function (...args) {
if (!global.rpyfunction_call_args) global.rpyfunction_call_args = {};
var index = %s;
var variable = 'global.rpyfunction_call_args[' + index + ']';
global.rpyfunction_call_args[index] = args;
args = [variable, index.toString()]; //.map(function (string) {return allocate.length === 2 ? allocate(intArrayFromString(string), ALLOC_NORMAL) : allocate(intArrayFromString(string), 'i8', ALLOC_NORMAL)});
//Module.asm.onfunctioncall(...args);
Module.ccall('onnfunctioncall', 'null', ['string', 'string'], args);
delete global.rpyfunction_call_args[index];
var result = global[global['rpyfunction_call_' + index]];
delete global['rpyfunction_call_' + index];
return result;
});
'''
def toFunction(function, keep=False):
#if globals.functions_cache is None:
# globals.functions_cache = {}
#if function in globals.functions_cache: return globals.functions_cache[function]
#index = globals.functions
#functions[index] = function
#functions.append(function)
#globals.functions += 1
object = Object(str(decorated_functions[function]), safe_function=True, safe_function_info='Function %s in module %s' % (function.function_name, function.function_module)) #function_template % (index))
if keep: object.keep()
#globals.functions_cache[function] = object
return object
def fromFunction(function=None, keep=False):
if function is None: return 'RPYJSON:null:RPYJSON'
return toFunction(function, keep=keep).toRef()
@entrypoint_highlevel(key='main', c_name='onfunctioncall', argtypes=[rffi.CCHARP, rffi.CCHARP])
def onfunctioncall(*arguments):
pointers = list(arguments)
variable, function_id = [rffi.charp2str(pointer) for pointer in pointers]
for pointer in pointers: lltype.free(pointer, flavor='raw')
args = Object.get(variable, function_id).toArray()
#id = int(function_id)
#return
function = functions[int(function_id)]
result = function.function[0](args=[arg for arg in args]) #if function in decorated_functions else function([arg for arg in args])
run_safe_set('global', 'rpyfunction_call_' + function_id, ('"%s"' % result.variable) if result is not None else 'null', skip_gc=True)
#run_javascript('global.rpyfunction_call_' + function_id + ((' = "%s"' % result.variable) if result is not None else ' = null'), skip_gc=True)
#globals.collector_id = None
decorated_functions = {}
#functions_id = {}
class Function:
function = (None,)
cache = {'count': 0}
def __init__(self, function):
self.cache['count'] += 1
count = self.cache['count']
self.function = (function,)
decorated_functions[self] = count
functions[count] = self
self.function_name = function.__name__
self.function_module = function.__module__
#TODO make this callable without Object.fromFunction
def javascript_function(function=None, asynchronous=False, name=None, count=None): #Spread list of Object to each of the argument
if not function and asynchronous:
def wrapper(function):
return javascript_function(function=asynchronous_function(function), name=function.__name__, count=function.func_code.co_argcount, asynchronous=True)
return wrapper
if function is None: raise Exception("Where is the function?")
if name is None: name = function.__name__
if count is None: count = function.func_code.co_argcount
args = ', '.join('args[%s]' % index for index in range(count))
arg_names = ', '.join('rpyarg%s=None' % (index + 1) for index in range(count))
namespace = {'rpython_decorated_function': function, 'RPYObject': Object}
indent = '\n' + (' ' * 4)
#code = 'def ' + name + '(args=None' + (', ' if count else "") + arg_names + '):
code = 'def ' + name + '(' + arg_names + (', ' if count else "") + 'args=None):'
def return_if_synchronous(): return 'return ' if not asynchronous else ""
if count:
code += indent + 'if args is None: ' + return_if_synchronous() + 'rpython_decorated_function(' + ', '.join('rpyarg%s or RPYObject("null")' % (index + 1) for index in range(count)) + ')'
if asynchronous: code += indent + 'if args is None: return'
code += indent + 'if args is not None and len(args) < ' + str(count) + ': ' + return_if_synchronous() + 'rpython_decorated_function(' + ', '.join('args[%s] if len(args) >= %s else RPYObject("null")' % (index, index + 1) for index in range(count)) + ')'
if asynchronous:
code += indent + 'if args is not None and len(args) < ' + str(count) + ': return'
code += indent + 'assert args is not None and len(args) >= ' + str(count)
code += indent + return_if_synchronous() + 'rpython_decorated_function(' + args + ')'
exec(code, namespace)
function = namespace[name]
#decorated_functions.append(function)
#return function
return Function(function)
args = javascript_function
function = args
snapshot = open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'utils/snapshot_memory.js'), 'r').read()
@Function
def garbage_collector(args):
if globals.collector_id is None: return
garbage = globals.garbage
for variable in garbage: garbage[variable].free()
if globals.pendingAsync is None or not len(globals.pendingAsync):
if globals.snapshot is None:
globals.snapshot = Object(snapshot).keep()
globals.setTimeout(globals.snapshot.toRef()) #, JSON.fromInteger(0))
globals.collector_id = None
def run_garbage_collector():
globals.collector_id = ''
if globals.garbage is None:
globals.garbage = {}
#if globals.collector_function is None:
globals.collector_function = json.fromFunction(garbage_collector)
if globals.setTimeout is None:
globals.setTimeout = Object('Promise.resolve()')['then'].keep().toFunction()
setTimeout = globals.setTimeout
timeout = setTimeout(globals.collector_function) #, json.fromInteger(0))
globals.collector_id = timeout.toString()
method_template = '''
(function (...args) {
if (!global.rpymethod_call_args) global.rpymethod_call_args = {};
var index = %s;
var variable = 'global.rpymethod_call_args[' + index + ']';
global.rpymethod_call_args[index] = args;
args = [variable, index.toString()]; //.map(function (string) {return allocate.length === 2 ? allocate(intArrayFromString(string), ALLOC_NORMAL) : allocate(intArrayFromString(string), 'i8', ALLOC_NORMAL)});
Module.ccall('onmethodcall%s', 'null', ['string', 'string'], args);
delete global.rpymethod_call_args[index];
var result = global[global['rpymethod_call_' + index]];
delete global['rpymethod_call_' + index];
return result;
});
'''
def fromMethod():
methods = {}
globals.method_callers += 1
method_callers = globals.method_callers
@entrypoint_highlevel(key='main', c_name='onmethodcall' + str(globals.method_callers), argtypes=[rffi.CCHARP, rffi.CCHARP])
def onmethodcall(*arguments):
pointers = list(arguments)
variable, method_id = [rffi.charp2str(pointer) for pointer in pointers]
for pointer in pointers: lltype.free(pointer, flavor='raw')
args = Object.get(variable, method_id).toArray()
method = methods[int(method_id)]
result = method(args=[arg for arg in args])
run_safe_set('global', 'rpymethod_call_' + method_id, ('"%s"' % result.variable) if result is not None else 'null')
#run_javascript('global.rpymethod_call_' + method_id + ((' = "%s"' % result.variable) if result is not None else ' = null'))
onmethodcall.__name__ = 'onmethodcall' + str(globals.method_callers)
#class Cache:
# methods = None
#cache = Cache()
def Method(method, keep=False):
if method is None: return 'RPYJSON:null:RPYJSON'
#if cache.methods is None:
# cache.methods = {}
#if method in cache.methods: return cache.methods[method]
#globals.methods += 1
methods[method_callers] = method
object = Object(str(method_callers), safe_method=method_callers) #method_template % (globals.methods, globals.method_callers))
if keep: object.keep()
#cache.methods[method] = object.toRef()
return object.toRef() #cache.methods[method]
return Method
def Method(count=0):
if not count: return fromMethod()
return (fromMethod() for index in range(count))
def method(function, asynchronous=False, name=None, count=None): #Spread list of Object to each of the argument
if asynchronous:
def wrapper(function):
return method(asynchronous_function(function), name=function.__name__, count=function.func_code.co_argcount)
return wrapper
if name is None: name = function.__name__
if count is None: count = function.func_code.co_argcount
count -= 1
args = ', '.join(['self'] + ['args[%s]' % index for index in range(count)])
arg_names = ', '.join(['self'] + ['rpyarg%s=None' % (index + 1) for index in range(count)] + ['args=None'])
namespace = {'rpython_decorated_function': function, 'RPYObject': Object}
indent = '\n' + (' ' * 4)
code = 'def ' + name + '(' + arg_names + '):'
#if count:
code += indent + 'if args is None: return rpython_decorated_function(' + ', '.join(['self'] + ['rpyarg%s or RPYObject("null")' % (index + 1) for index in range(count)]) + ')'
code += indent + 'if args is not None and len(args) < ' + str(count) + ': return rpython_decorated_function(' + ', '.join(['self'] + ['args[%s] if len(args) >= %s else RPYObject("null")' % (index, index + 1) for index in range(count)]) + ')'
code += indent + 'assert args is not None and len(args) >= ' + str(count)
code += indent + 'return rpython_decorated_function(' + args + ')'
exec(code, namespace)
function = namespace[name]
#decorated_methods.append(function)
return function
class String:
def __init__(self, value):
self.value = value
def replace(self, search, substitute):
self.value = replace(self.value, search, substitute)
return self
def format(self, *strings):
index = 0
for string in list(strings):
self.value = replace(self.value, '{%s}' % index, string)
index += 1
return self
class Globals:
promises = 0
objects = 0
functions = 0
functions_cache = None
methods = 0
methods_cache = None
method_callers = 0
garbage = None
collector_id = None
collector_function = None
setTimeout = None
snapshot = None
pendingAsync = None
def __init__(self):
self.resolve_next_event = resolve_next_event
globals = Globals()
class Array:
def __init__(self, object):
self.object = object
def __iter__(self):
object = self.object['length']
if object.type != 'number': return iter([])
length = object.toInteger()
objects = []
for index in range(length):
objects += [self.object[str(index)]]
return iter(objects)
class Error:
def __init__(self, message):
run_script(rffi.str2charp('throw new Error(`%s`)' % message))
def create_closure(function, *objects):
object = Object(JSON.fromFunction(function), safe_closure_args=[object.toRef() for object in list(objects)])
return object
class Object:
id = -1
resolved = True
keep_from_gc = False
fromString = staticmethod(toString)
fromStr = staticmethod(toStr)
fromInteger = staticmethod(toInteger)
fromInt = staticmethod(toInt)
fromFloat = staticmethod(toFloat)
fromBoolean = staticmethod(toBoolean)
fromBool = staticmethod(toBool)
fromList = staticmethod(toList)
fromDict = staticmethod(toDict)
fromFunction = staticmethod(toFunction)
createClosure = staticmethod(create_closure)
def __init__(self, code, bind='', prestart='', safe_json=False, safe_get="", safe_call="", safe_new=str(), safe_function=False, safe_function_info=str(), safe_method=0, safe_closure_args=None):
self.id = globals.objects
globals.objects += 1
self.code = code
self.variable = 'rpython_object_' + str(self.id)
if safe_json:
self.type = run_safe_json(json.parse_rpy_json(code), self.variable)
elif safe_get:
self.type = run_safe_get(code, safe_get, self.variable)
elif safe_call:
self.type = run_safe_call(safe_call, code, self.variable)
elif safe_new:
self.type = run_safe_new(safe_new, code, self.variable)
elif safe_function:
self.type = create_function(code, self.variable, safe_function_info)
elif safe_method:
self.type = create_method(code, str(safe_method), self.variable)
elif safe_closure_args is not None:
self.type = create_js_closure(code, json.parse_rpy_json(json.fromList(safe_closure_args)), self.variable)
else:
self.type = run_javascript(String("""
{3}
global.{0} = {1}
var object = global.{0};
{2}
global.{0} = object;
if (global.{0} === null) return 'null';
if (Array.isArray(global.{0})) return 'array';
return typeof global.{0};
""").format(self.variable, code, bind, prestart).value, returns=True)
globals.garbage[self.variable] = self
@staticmethod
def get(*args):
keys = list(args)
object = Object('global', safe_get=keys.pop(0))
for key in keys:
object = object[key]
return object
def new(self, *args):
json_args = '[' + ', '.join([json.parse_rpy_json(arg) for arg in list(args)]) + ']'
return Object(json_args, safe_new=self.variable)
def call(self, *args):
#if not args: return Object(String('call()').replace('{0}', self.variable).value, prestart='var call = global.' + self.variable)
json_args = '[' + ', '.join([json.parse_rpy_json(arg) for arg in list(args)]) + ']'
return Object(json_args, safe_call=self.variable)
#return Object(String('call(...[{1}])').replace('{0}', self.variable).replace('{1}', json_args).value, prestart='var call = global.' + self.variable)
def free(self):
if self.keep_from_gc: return
#run_javascript('delete global.' + self.variable)
run_safe_del('global', self.variable)
del globals.garbage[self.variable]
def keep(self):
self.keep_from_gc = True
return self
def release(self):
self.keep_from_gc = False
return self
def __iter__(self):
keys = Object('Object.keys(global.%s)' % (self.variable))
length = keys['length'].toInteger()
objects = []
for index in range(length):
objects += [keys[str(index)].toString()]
return iter(objects)
def __getitem__(self, key):
return Object(self.variable, safe_get=key) #, bind="object = typeof object != 'function' || object.prototype ? object : object.bind(global." + self.variable + ')')
def __setitem__(self, key, value):
run_safe_set(self.variable, key, json.parse_rpy_json(value))
#run_javascript(('global.%s["%s"] = ' % (self.variable, key)) + json.parse_rpy_json(value))
return
def toString(self):
return get_string(self.variable)
#if self.type == 'string': return run_javascript('return global.%s' % self.variable, returns=True)
#return run_javascript(String('return global.{0} && global.{0}.toString ? global.{0}.toString() : String(global.{0})').format(self.variable).value, returns=True)
def toStr(self): return self.toString()
def toInteger(self):
return int(get_integer(self.variable))
#integer = 0
#if self.type == 'number': integer = int(run_javascript('return JSON.stringify(global.%s)' % self.variable, returns=True))
#else: integer = int(run_javascript(String('var integer = parseInt(global.{0}); if (!isNaN(integer)) return integer; console.log(global.{0}); throw new Error("Not a number")').format(self.variable).value, returns=True))
#return integer
def toInt(self): return self.toInteger()
def toFloat(self):
return float(get_float(self.variable))
#number = 0
#if self.type == 'number': number = float(run_javascript('return JSON.stringify(global.%s)' % self.variable, returns=True))
#else: number = float(run_javascript(String('var float = parseFloat(global.{0}); if (!isNaN(float)) return float; console.log(global.{0}); throw new Error("Not a number")').format(self.variable).value, returns=True))
#return number
def toBoolean(self):
self._update()
if self.type == 'boolean':
return True if 'true' == self.toString() else False
#return True if 'true' == get_boolean(self.variable) else False
elif self.type in ['array', 'object']: return True
elif self.type == 'string': return self['length'].toBoolean()
elif self.type == 'number': return self.toInteger() != 0
elif self.type in ['null', 'undefined']: return False
return True
def toBool(self): return self.toBoolean()
def toArray(self): #This is basically iter but returns the object just like for of
return Array(self)
def toFunction(self):
return self.call
def toReference(self):
return 'RPYJSOBJECT:' + self.variable + ':RPYJSOBJECT'
def toRef(self): return self.toReference()
#def toDict(self): TODO
#def toList(self): TODO
def log(self):
run_javascript('console.log(global.%s)' % (self.variable))
return self
def wait(self, awaits, native_awaits, promise_id, parent_id):
self.resolved = True if self.type in ['null', 'undefined'] or self['then'].type != 'function' else False #False
awaits.append(self)
return self
def _update(self):
self.type = run_safe_type_update(self.variable)
#self.type = run_javascript(String("if (global.{0} === null) {return 'null'} else if (Array.isArray(global.{0})) {return 'array'} else return typeof global.{0}").replace('{0}', self.variable).value, returns=True)
self.resolved = True if self.type in ['null', 'undefined'] or self['then'].type != 'function' else False if self['rpython_resolved'].type != 'boolean' else True
class Wait:
variable = ''
parent_id = -1
promise_id = -1
def __init__(self):
self.object = {'resolved': False}
handler_template = """
def resolve_next_event(parent_id, child_id):
if False: return
"""
def get_variables_name(variables):
return ', '.join(variables)
def get_variables_cache(variables):
return ', '.join(['rpython_promise.var_' + variable for variable in variables])
dummy_tuple = (None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None)
def asynchronous(function):
def keep_object(object):
if isinstance(object, Object): object.keep()
return object
original_file = function.__globals__.get('__file__', "")
original_function = function
function_globals = function.__globals__
class Waitable(Wait):
rpython_promise = None
native_map = None
native_index = -1
native_values = None
native_values_count = 0
function_name = function.__name__
function_module = function.__module__
def wait(self, awaits, native_awaits, promise_id, parent_id):
self.promise_id = promise_id
self.parent_id = parent_id
native_awaits.append(self.object)
return (self, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None,)
class Promise:
step = 1
#last = -1
id = -1
promises = {}
count = 0
parent = None
wait = None
next_called = False
function_name = function.__name__
function_module = function.__module__
def __init__(self, function, last):
self.awaits = []
self.native_awaits = []
#self.promises = {}
self.function = function
self.last = last