-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjs.py
155 lines (135 loc) · 4.98 KB
/
js.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
import json
import js
def js_exec(code, *args):
code = format(code, *args)
result = js.exec('formatString((() => {\n' + code + '\n})());')
return json.loads(result)
def js_print(value):
js.exec('console.log(%s)' % (json.dumps(str(value))))
py_print = print
def format(string, *args):
for index, arg in enumerate(args):
string = string.replace('{%s}' % (index), str(arg))
return string
def wait(promise):
while not promise._resolved:
js.sleep(250)
result = js_exec('''
const object = {0};
if (object && object.constructor !== Promise) return true;
''', promise._name)
if result: promise.resolve(JS(promise._name))
return promise._value
class JSPromise():
def __init__(self, name):
self._name = name
self._resolved = False
self._value = False
def resolve(self, value):
self._resolved = True
self._value = value
return value
def wait(self):
return wait(self)
class JSObject():
def __init__(self, name):
self._name = name
def __len__(self):
if self._name:
return 1
else:
return 0
def __dir__(self):
if not self._name: return []
return js_exec('''
return Object.keys({0}));
''', self._name)
def __getattr__(self, key):
name = self._name + '.' + key if self._name else key
result = js_exec('''
let object = {0};
if (object && object.constructor === Promise) {
object.then((object) => global.mpjscache['{0}'] = object);
return 'mpjspromiseobject:{0}';
}
try {
if (object && [Array, Object, Number, String, Boolean, Function, AsyncFunction].indexOf(object.constructor) < 0) throw Error('Not JSON Serializable');
if (object && object.constructor === Object) for (let key in Object.keys(object)) {
if (object.indexOf(key) < 0) throw Error('Not a JSON');
}
else if (object && (object.constructor === Function || object.constructor === AsyncFunction)) {
delete global.mpjscache['{0}'];
return 'mpjsfunctionobject:{0}';
}
return object;
}
catch(error) {
return 'mpjsjavascriptobject:{0}';
}
''', name)
if type(result) != str: return result
types = {'promise': JSPromise, 'function': JSFunction, 'javascript': JSObject}
for object_type in types:
if 'mpjs' + object_type + 'object' in result:
result = types[object_type](result.split(':')[1])
break
return result
def __setattr__(self, key, value):
if not self._name: return
value = json.dumps(value)
object_name = self._name + '.' + key
js_exec('''
{0} = {1};
''', object_name, value)
def JSFunction(name):
short_name = name.split('.')[-1]
def function(*args):
args = json.dumps(list(args))
cache_name = 'global.mpjscache' + ('.' + name if '.' not in name else json.dumps([name]))
result = js_exec('''
let object;
if (global.mpjscache['{0}']) {
object = global.mpjscache['{0}'](...{1});
}
else if ({0}) {
object = {0}(...{1});
}
global.mpjscache['{0}'] = object;
if (object && object.constructor === Promise) {
object.then((object) => global.mpjscache['{0}'] = object);
return 'mpjspromiseobject';
}
try {
if (object && [Array, Object, Number, String, Boolean, Function, AsyncFunction].indexOf(object.constructor) < 0) throw Error('Not JSON Serializable');
if (object && object.constructor === Object) for (let key in Object.keys(object)) {
if (object.indexOf(key) < 0) throw Error('Not a JSON');
}
else if (object && (object.constructor === Function || object.constructor === AsyncFunction)) {
return 'mpjsfunctionobject';
}
return object;
}
catch(error) {
return 'mpjsjavascriptobject';
}
''', name, args)
if type(result) != str: return result
types = {'promise': JSPromise, 'function': JSFunction, 'javascript': JSObject}
for object_type in types:
if 'mpjs' + object_type + 'object' in result:
result = types[object_type](cache_name)
break
return result
return function
def JS(variable):
empty = JSObject('')
return getattr(empty, variable)
#require = JS('require')
#result = require('fs').readFileSync('./test.js').toString()
#print(result)
#New API promise.wait(), now works without skipping event loop using emscripten_sleep, requires emterpreter
#require = JS('require')
#response = require('node-fetch')('https://github.com/').wait()
#print(response)
#result = response.text().wait()
#print(result)