Skip to content

Commit 7f663db

Browse files
committed
Fix compatibility on Webpack and add register module for py files loader
1 parent 90e7656 commit 7f663db

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

index.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ if (typeof window === 'undefined' && typeof importScripts === 'undefined') {
2222
pyjs = require('fs').readFileSync(__dirname + '/js.py').toString();
2323
}
2424
else {
25-
pyjs = require('!raw-loader!./js.py');
25+
window.global = window;
26+
pyjs = require('!raw-loader!./js.py').default;
2627
}
2728

2829
function wait_exist(fn) {
@@ -45,15 +46,17 @@ if (browser) {
4546

4647
global.AsyncFunction = (async () => {}).constructor;
4748

48-
let python_browser;
49+
let initiated;
50+
let pyjs_initiated;
4951

5052
module.exports = (async () => {
5153
await wait_exist(() => global.mp_js_init);
5254
const methods = {}
53-
methods.init = global.mp_js_init;
55+
const init = global.mp_js_init;
56+
methods.init = (stack) => !initiated && (initiated = !init(stack));
5457
const do_str = global.mp_js_do_str;
5558
global._mp_js_do_str = do_str;
56-
methods.do_str = async (code) => {
59+
methods.do_str = async (code, module, initiated) => {
5760
const codes = code.split('\n');
5861
let spaces = '';
5962
for (let line of codes) {
@@ -66,6 +69,18 @@ module.exports = (async () => {
6669
}
6770
break;
6871
}
72+
if (module) {
73+
if (!spaces) {
74+
code = code.replace(/^/gm, ' ');
75+
spaces = ' ';
76+
}
77+
code = 'def mpjs_module():\n' + code;
78+
code += '\n' + spaces + 'import sys';
79+
code += '\n' + spaces + `sys.modules['${module}'] = type('${module}', (), globals())()`;
80+
code += '\nmpjs_module()';
81+
if (!initiated) return code;
82+
spaces = '';
83+
}
6984
if (spaces) {
7085
let index_split = 0;
7186
const new_code = [];
@@ -77,23 +92,28 @@ module.exports = (async () => {
7792
}
7893
stdout_text = '';
7994
stdout_ready = false;
80-
code += "\ntry: py_print('mpjsendline')\nexcept: print('mpjsendline')";
95+
code += "\nprint('mpjsendline')";
8196
if (global.promiseWaitInterval) await wait_exist(() => !global.promiseWaitInterval);
8297
do_str(code);
8398
await wait_exist(() => stdout_ready);
8499
return stdout_text;
85100
}
86101
methods.init_python = async (stack) => {
102+
if (pyjs_initiated) return;
87103
methods.init(stack);
88-
if (!python_browser) {
104+
if (!pyjs_initiated) {
89105
await methods.do_str(`
90106
import json
91107
isbrowser = json.loads('${browser}')
92108
`);
93-
python_browser = true;
109+
pyjs_initiated = true;
94110
}
95111
return await methods.do_str(pyjs);
96112
}
113+
methods.register_module = async (module, code) {
114+
if (!pyjs_initiated) return pyjs += '\n' + await methods.do_str(code, module, false);
115+
return await methods.do_str(code, module, true);
116+
}
97117
global.mp_js_do_str = methods.do_str;
98118
methods.init_repl = global.mp_js_init_repl;
99119
methods.process_char = global.mp_js_process_char;

js.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ def js_print(value):
1010
js.exec('console.log(%s)' % (json.dumps(str(value))))
1111

1212
py_print = print
13-
print = js_print
1413

1514
def format(string, *args):
1615
for index, arg in enumerate(args):

0 commit comments

Comments
 (0)