forked from gython/Gython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-js
More file actions
195 lines (165 loc) · 6.08 KB
/
python-js
File metadata and controls
195 lines (165 loc) · 6.08 KB
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
// python-js module for nodejs //
var fs = require('fs');
var path = require('path');
var requirejs = require('requirejs');
var pyjs = require('./pythonjs.js');
var empythoned = require('./empythoned.js');
var Python = empythoned.Python;
var buffer = [];
var _on_stderr = function(chr) {
if (chr == null || chr == 0 || chr == 10) {
console.log( buffer.join('') );
buffer.length = 0;
} else {
buffer.push( String.fromCharCode(chr) );
}
}
// https://github.com/kripken/emscripten/wiki/Filesystem-API
empythoned.FS.createLazyFile(".", "python_to_pythonjs.py", "./python_to_pythonjs.py",
true,false
);
empythoned.FS.createLazyFile(".", "pythonjs.py", "./pythonjs.py",
true,false
);
empythoned.FS.createLazyFile(".", "ministdlib.py", "./ministdlib.py",
true,false
);
empythoned.FS.createLazyFile(".", "typedpython.py", "./typedpython.py",
true,false
);
empythoned.FS.createLazyFile(".", "pythonjs_to_dart.py", "./pythonjs_to_dart.py",
true,false
);
empythoned.FS.createLazyFile(".", "pythonjs_to_coffee.py", "./pythonjs_to_coffee.py",
true,false
);
empythoned.FS.createLazyFile(".", "pythonjs_to_lua.py", "./pythonjs_to_lua.py",
true,false
);
empythoned.FS.createLazyFile(".", "python_to_visjs.py", "./python_to_visjs.py",
true,false
);
empythoned.FS.createLazyFile(".", "code_writer.py", "./code_writer.py",
true,false
);
empythoned.FS.createLazyFile(".", "inline_function.py", "./inline_function.py",
true,false
);
empythoned.FS.createLazyFile(".", "ast_utils.py", "./ast_utils.py",
true,false
);
empythoned.FS.createLazyFile(".", "pythonjs.js", "./pythonjs.js",
true,false
);
empythoned.FS.createFolder(".","fakelibs",true,true);
empythoned.FS.createLazyFile("./fakelibs", "tornado.py", "./fakelibs/tornado.py",
true,false
);
empythoned.FS.createLazyFile("./fakelibs", "os.py", "./fakelibs/os.py",
true,false
);
empythoned.FS.createLazyFile("./fakelibs", "sys.py", "./fakelibs/sys.py",
true,false
);
empythoned.FS.createLazyFile("./fakelibs", "tempfile.py", "./fakelibs/tempfile.py",
true,false
);
empythoned.FS.createLazyFile("./fakelibs", "subprocess.py", "./fakelibs/subprocess.py",
true,false
);
Python.initialize(
null, // stdin
null, // stdout
_on_stderr // stderr
);
Python.eval('from python_to_pythonjs import main as to_pythonjs');
Python.eval('from pythonjs import main as _to_javascript');
//Python.eval('from pythonjs_to_dart import main as _to_dart');
//Python.eval('from pythonjs_to_coffee import main as _to_coffee');
//Python.eval('from pythonjs_to_lua import main as _to_lua');
Python.eval('from python_to_visjs import main as to_visjs');
Python.eval('def to_javascript_module(src, runtime=True): return _to_javascript(to_pythonjs(src), requirejs=True, insert_runtime=runtime)');
Python.eval('def to_javascript(src, runtime=False): return _to_javascript(to_pythonjs(src), requirejs=False, insert_runtime=runtime)');
Python.eval('def to_dart(src): from pythonjs_to_dart import main as _to_dart; return _to_dart(to_pythonjs(src, dart=True))');
Python.eval('def to_coffee(src): from pythonjs_to_coffee import main as _to_coffee; return _to_coffee(to_pythonjs(src, coffee=True))');
Python.eval('def to_lua(src): from pythonjs_to_lua import main as _to_lua; return _to_lua(to_pythonjs(src, lua=True))');
function clean_code( code ) {
code = code.substring(1, code.length-1); // strip quotes
code = code.split('\\n').join('\n');
code = code.split('\\t').join('\t');
code = code.split("\\'").join("\'");
code = code.split('\\"').join('\"');
return code;
}
// data is written to a file to avoid any problems with escaping string quotes
// note: `FS.createDataFile` is part of the old Emscripten 1.0 API
exports.translator = {
to_javascript_module : function(data, runtime) {
empythoned.FS.createDataFile( "/sandbox", "temp", data, true, true );
if (runtime == true || runtime === undefined) {
var code = Python.eval('to_javascript_module(open("/sandbox/temp","r").read(), runtime=True)');
} else {
var code = Python.eval('to_javascript_module(open("/sandbox/temp","r").read(), runtime=False)');
}
if (code !== null && code !== undefined) {
return clean_code( code );
}
},
to_javascript : function(data, runtime) {
empythoned.FS.createDataFile( "/sandbox", "temp", data, true, true );
if (runtime == true) {
var code = Python.eval('to_javascript(open("/sandbox/temp","r").read(), runtime=True)');
} else {
var code = Python.eval('to_javascript(open("/sandbox/temp","r").read(), runtime=False)');
}
if (code !== null && code !== undefined) {
return clean_code( code );
}
},
to_dart : function(data) {
empythoned.FS.createDataFile( "/sandbox", "temp", data, true, true );
var code = Python.eval('to_dart(open("/sandbox/temp","r").read())');
if (code !== null && code !== undefined) {
return clean_code( code );
}
},
to_coffee : function(data) {
empythoned.FS.createDataFile( "/sandbox", "temp", data, true, true );
var code = Python.eval('to_coffee(open("/sandbox/temp","r").read())');
if (code !== null && code !== undefined) {
return clean_code( code );
}
},
to_lua : function(data) {
empythoned.FS.createDataFile( "/sandbox", "temp", data, true, true );
var code = Python.eval('to_lua(open("/sandbox/temp","r").read())');
if (code !== null && code !== undefined) {
return clean_code( code );
}
},
to_visjs : function(data) {
empythoned.FS.createDataFile( "/sandbox", "temp", data, true, true );
var code = Python.eval('to_visjs(open("/sandbox/temp","r").read())');
if (code !== null && code !== undefined) {
return clean_code( code );
}
}
}
var runtime = {};
var basepath = path.dirname( __filename );
Object.defineProperty(
runtime,
'javascript',
{ enumerable:true, get : function() {return fs.readFileSync( basepath+'/pythonjs.js', {'encoding':'utf8'} )} }
);
Object.defineProperty(
runtime,
'dart',
{ enumerable:true, get : function() {return fs.readFileSync( basepath+'/runtime/dart_builtins.py', {'encoding':'utf8'} )} }
);
Object.defineProperty(
runtime,
'lua',
{ enumerable:true, get : function() {return fs.readFileSync( basepath+'/runtime/lua_builtins.py', {'encoding':'utf8'} )} }
);
exports.runtime = runtime;