-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathempythoned-translator-webworker.js
More file actions
59 lines (50 loc) · 1.61 KB
/
empythoned-translator-webworker.js
File metadata and controls
59 lines (50 loc) · 1.61 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
(function () {
self.console = {
log: function () {}
};
self.prompt = function () {
return 'Input not supported in demo';
};
var ready = false;
importScripts('empythoned.js');
// https://github.com/kripken/emscripten/wiki/Filesystem-API
FS.createLazyFile(".", "python_to_pythonjs.py", "./python_to_pythonjs.py",
true,false
);
FS.createLazyFile(".", "pythonjs.py", "./pythonjs.py",
true,false
);
FS.createLazyFile(".", "ministdlib.py", "./ministdlib.py",
true,false
);
var buffer = [];
var on_stderr = function(chr) {
if (chr == null || chr == 0 || chr == 10) {
postMessage( {'error':buffer.join('')} );
buffer.length = 0;
} else {
buffer.push( String.fromCharCode(chr) );
}
}
Python.initialize(
null, // stdin
null, // stdout
on_stderr // stderr
);
var res = Python.eval('from python_to_pythonjs import main as to_pythonjs');
var res = Python.eval('from pythonjs import main as to_javascript');
var res = Python.eval('def translate_to_javascript(src): return to_javascript(to_pythonjs(src))');
ready = true;
var on_message = function (e) {
if (ready != true) throw Error('Empythoned not ready');
// e.data is written to a file to avoid any problems with escaping string quotes
// note: emscripten 1.0 api
FS.createDataFile( "/sandbox", "temp", e.data, true, true );
var result = Python.eval('translate_to_javascript(open("/sandbox/temp","r").read())');
if (result !== null && result !== undefined) {
postMessage( {'code':result} ); // javascript to eval
}
//FS.deleteFile( "/sandbox/temp" );
};
addEventListener('message', on_message, false);
})();