forked from brython-dev/brython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_bridge.js
167 lines (152 loc) · 5.76 KB
/
node_bridge.js
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
/*
Author: Billy Earney
Date: 04/19/2013
License: MIT
Description: This file can work as a "bridge" between nodejs and brython
so that client side brython code can be executed on the server side.
Will brython replace Cython one day? Only time will tell.
:)
*/
const fs = require('fs');
const path = require('path')
const brythonSrcPath = path.join(__dirname, '..', 'www', 'src', 'brython.js')
document = {};
document.getElementsByTagName = () => [{src: ''}];
window = {};
window.location = {href: ''};
window.navigator = {}
window.confirm = () => true;
window.console = console;
document.$py_src = {}
document.$debug = 0
self = {};
__BRYTHON__ = {}
__BRYTHON__.$js_module_path = {}
__BRYTHON__.$js_module_alias = {}
__BRYTHON__.$py_module_path = {}
__BRYTHON__.$py_module_alias = {}
__BRYTHON__.$py_next_hash = -Math.pow(2, 53)
__BRYTHON__.exception_stack = []
__BRYTHON__.scope = {}
__BRYTHON__.modules = {}
// Read and eval library
jscode = fs.readFileSync(brythonSrcPath, 'utf8');
eval(jscode);
function $import_hooks(mod_name, path, from_stdlib) {
var search_path = ['./www/src/libs', './www/src/Lib'];
var ext = ['.js', '.py'];
var mods = [mod_name, mod_name + '/__init__'];
for (var i = 0, _len_i = search_path.length; i < _len_i; i++) {
for (var j = 0, _len_j = ext.length; j < _len_j; j++) {
for (var k = 0, _len_k = mods.length; k < _len_k; k++) {
var path = search_path[i] + '/' + mods[k] + ext[j]
try {
var module_contents = fs.readFileSync(path, 'utf8')
if (ext[j] === '.js') {
__BRYTHON__.$js_module_path[mod_name] = path
} else {
__BRYTHON__.$py_module_path[mod_name] = path
}
__BRYTHON__.imported[mod_name] = path
return module_contents
} catch (err) {
}
}
}
}
console.log("error time!");
var res = Error()
res.name = 'NotFoundError'
res.message = "No module named '" + mod_name + "'"
throw res
}
$compile_python = function (module_contents, module) {
var root = __BRYTHON__.py2js(module_contents, module)
var body = root.children
root.children = []
// use the module pattern : module name returns the results of an anonymous function
var mod_node = new $Node('expression')
//if(names!==undefined){alias='$module'}
new $NodeJSCtx(mod_node, '$module=(function()')
root.insert(0, mod_node)
mod_node.children = body
// search for module-level names : functions, classes and variables
var mod_names = []
for (var i = 0, _len_i = mod_node.children.length; i < _len_i; i++) {
var node = mod_node.children[i]
// use function get_ctx()
// because attribute 'context' is renamed by make_dist...
var ctx = node.get_ctx().tree[0]
if (ctx.type === 'def' || ctx.type === 'class') {
if (mod_names.indexOf(ctx.name) === -1) {
mod_names.push(ctx.name)
}
} else if (ctx.type === 'from') {
for (var j = 0, _len_j = ctx.names.length; j < _len_j; j++) {
var name = ctx.names[j];
if (name === '*') {
// just pass, we don't want to include '*'
} else if (ctx.aliases[name] !== undefined) {
if (mod_names.indexOf(ctx.aliases[name]) === -1) {
mod_names.push(ctx.aliases[name])
}
} else {
if (mod_names.indexOf(ctx.names[j]) === -1) {
mod_names.push(ctx.names[j])
}
}
}
} else if (ctx.type === 'assign') {
var left = ctx.tree[0]
if (left.type === 'expr' && left.tree[0].type === 'id' && left.tree[0].tree.length === 0) {
var id_name = left.tree[0].value
if (mod_names.indexOf(id_name) === -1) {
mod_names.push(id_name)
}
}
}
}
// create the object that will be returned when the anonymous function is run
var ret_code = 'return {'
for (var i = 0, _len_i = mod_names.length; i < _len_i; i++) {
ret_code += mod_names[i] + ':' + mod_names[i] + ','
}
ret_code += '__getattr__:function(attr){return this[attr]},'
ret_code += '__setattr__:function(attr,value){this[attr]=value}'
ret_code += '}'
var ret_node = new $Node('expression')
new $NodeJSCtx(ret_node, ret_code)
mod_node.add(ret_node)
// add parenthesis for anonymous function execution
var ex_node = new $Node('expression')
new $NodeJSCtx(ex_node, ')()')
root.add(ex_node)
try {
var js = root.to_js()
return js;
} catch (err) {
eval('throw ' + err.name + '(err.message)')
}
return undefined;
}
function execute_python_script(filename) {
_py_src = fs.readFileSync(filename, 'utf8')
__BRYTHON__.$py_module_path['__main__'] = './'
var js = __BRYTHON__.python_to_js(_py_src, '__main_soln__')
eval(js);
}
__BRYTHON__.$js_module_path = __BRYTHON__.$js_module_path || {}
__BRYTHON__.$js_module_alias = __BRYTHON__.$js_module_alias || {}
__BRYTHON__.$py_module_path = __BRYTHON__.$py_module_path || {}
__BRYTHON__.$py_module_alias = __BRYTHON__.$py_module_alias || {}
__BRYTHON__.exception_stack = __BRYTHON__.exception_stack || []
__BRYTHON__.scope = __BRYTHON__.scope || {}
__BRYTHON__.imported = __BRYTHON__.imported || {}
__BRYTHON__.modules = __BRYTHON__.modules || {}
__BRYTHON__.compile_python = $compile_python
__BRYTHON__.import_hooks = $import_hooks
__BRYTHON__.debug = 0
__BRYTHON__.$options = {}
__BRYTHON__.$options.debug = 0
var filename = process.argv[2];
execute_python_script(filename)