@@ -22,7 +22,8 @@ if (typeof window === 'undefined' && typeof importScripts === 'undefined') {
22
22
pyjs = require ( 'fs' ) . readFileSync ( __dirname + '/js.py' ) . toString ( ) ;
23
23
}
24
24
else {
25
- pyjs = require ( '!raw-loader!./js.py' ) ;
25
+ window . global = window ;
26
+ pyjs = require ( '!raw-loader!./js.py' ) . default ;
26
27
}
27
28
28
29
function wait_exist ( fn ) {
@@ -45,15 +46,17 @@ if (browser) {
45
46
46
47
global . AsyncFunction = ( async ( ) => { } ) . constructor ;
47
48
48
- let python_browser ;
49
+ let initiated ;
50
+ let pyjs_initiated ;
49
51
50
52
module . exports = ( async ( ) => {
51
53
await wait_exist ( ( ) => global . mp_js_init ) ;
52
54
const methods = { }
53
- methods . init = global . mp_js_init ;
55
+ const init = global . mp_js_init ;
56
+ methods . init = ( stack ) => ! initiated && ( initiated = ! init ( stack ) ) ;
54
57
const do_str = global . mp_js_do_str ;
55
58
global . _mp_js_do_str = do_str ;
56
- methods . do_str = async ( code ) => {
59
+ methods . do_str = async ( code , module , initiated ) => {
57
60
const codes = code . split ( '\n' ) ;
58
61
let spaces = '' ;
59
62
for ( let line of codes ) {
@@ -66,6 +69,18 @@ module.exports = (async () => {
66
69
}
67
70
break ;
68
71
}
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
+ }
69
84
if ( spaces ) {
70
85
let index_split = 0 ;
71
86
const new_code = [ ] ;
@@ -77,23 +92,28 @@ module.exports = (async () => {
77
92
}
78
93
stdout_text = '' ;
79
94
stdout_ready = false ;
80
- code += "\ntry: py_print('mpjsendline')\nexcept: print ('mpjsendline')" ;
95
+ code += "\nprint ('mpjsendline')" ;
81
96
if ( global . promiseWaitInterval ) await wait_exist ( ( ) => ! global . promiseWaitInterval ) ;
82
97
do_str ( code ) ;
83
98
await wait_exist ( ( ) => stdout_ready ) ;
84
99
return stdout_text ;
85
100
}
86
101
methods . init_python = async ( stack ) => {
102
+ if ( pyjs_initiated ) return ;
87
103
methods . init ( stack ) ;
88
- if ( ! python_browser ) {
104
+ if ( ! pyjs_initiated ) {
89
105
await methods . do_str ( `
90
106
import json
91
107
isbrowser = json.loads('${ browser } ')
92
108
` ) ;
93
- python_browser = true ;
109
+ pyjs_initiated = true ;
94
110
}
95
111
return await methods . do_str ( pyjs ) ;
96
112
}
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
+ }
97
117
global . mp_js_do_str = methods . do_str ;
98
118
methods . init_repl = global . mp_js_init_repl ;
99
119
methods . process_char = global . mp_js_process_char ;
0 commit comments