|
40 | 40 | "node_modules"
|
41 | 41 | )
|
42 | 42 | )
|
| 43 | +evalOpts = { 'filename': __file__, 'fromPythonFrame': True } |
43 | 44 |
|
44 | 45 | # Add some python functions to the global python object for code in this file to use.
|
45 |
| -globalThis = pm.eval("globalThis;") |
46 |
| -pm.eval("globalThis.python = { pythonMonkey: {}, stdout: {}, stderr: {} }") |
| 46 | +globalThis = pm.eval("globalThis;", evalOpts) |
| 47 | +pm.eval("globalThis.python = { pythonMonkey: {}, stdout: {}, stderr: {} }", evalOpts); |
47 | 48 | globalThis.pmEval = pm.eval
|
48 | 49 | globalThis.python.pythonMonkey.dir = os.path.dirname(__file__)
|
49 | 50 | #globalThis.python.pythonMonkey.version = pm.__version__
|
|
59 | 60 | globalThis.python.exec = exec
|
60 | 61 | globalThis.python.getenv = os.getenv
|
61 | 62 | globalThis.python.paths = ':'.join(sys.path)
|
62 |
| -pm.eval("python.paths = python.paths.split(':');"); # fix when pm supports arrays |
| 63 | +pm.eval("python.paths = python.paths.split(':');", evalOpts); # fix when pm supports arrays |
63 | 64 |
|
64 | 65 | globalThis.python.exit = pm.eval("""'use strict';
|
65 | 66 | (exit) => function pythonExitWrapper(exitCode) {
|
66 | 67 | if (typeof exitCode === 'number')
|
67 | 68 | exitCode = BigInt(Math.floor(exitCode));
|
68 | 69 | exit(exitCode);
|
69 | 70 | }
|
70 |
| -""")(sys.exit); |
| 71 | +""", evalOpts)(sys.exit); |
71 | 72 |
|
72 | 73 | # bootstrap is effectively a scoping object which keeps us from polluting the global JS scope.
|
73 | 74 | # The idea is that we hold a reference to the bootstrap object in Python-load, for use by the
|
|
78 | 79 |
|
79 | 80 | const bootstrap = {
|
80 | 81 | modules: {
|
81 |
| - vm: { runInContext: eval }, |
| 82 | + vm: {}, |
82 | 83 | 'ctx-module': {},
|
83 | 84 | },
|
84 | 85 | }
|
|
92 | 93 | throw new Error('module not found: ' + mid);
|
93 | 94 | }
|
94 | 95 |
|
95 |
| -bootstrap.modules.vm.runInContext_broken = function runInContext(code, _unused_contextifiedObject, options) |
| 96 | +bootstrap.modules.vm.runInContext = function runInContext(code, _unused_contextifiedObject, options) |
96 | 97 | {
|
97 | 98 | var evalOptions = {};
|
98 | 99 |
|
|
169 | 170 | globalThis.bootstrap = bootstrap;
|
170 | 171 |
|
171 | 172 | return bootstrap;
|
172 |
| -})(globalThis.python)""") |
| 173 | +})(globalThis.python)""", evalOpts) |
173 | 174 |
|
174 | 175 | def statSync_inner(filename: str) -> Union[Dict[str, int], bool]:
|
175 | 176 | """
|
@@ -208,15 +209,18 @@ def existsSync(filename: str) -> bool:
|
208 | 209 | # require and exports symbols injected from the bootstrap object above. Current PythonMonkey bugs
|
209 | 210 | # prevent us from injecting names properly so they are stolen from trail left behind in the global
|
210 | 211 | # scope until that can be fixed.
|
| 212 | +# |
| 213 | +# lineno should be -5 but jsapi 102 uses unsigned line numbers, so we take the newlines out of the |
| 214 | +# wrapper prologue to make stack traces line up. |
211 | 215 | with open(node_modules + "/ctx-module/ctx-module.js", "r") as ctxModuleSource:
|
212 | 216 | initCtxModule = pm.eval("""'use strict';
|
213 | 217 | (function moduleWrapper_forCtxModule(broken_require, broken_exports)
|
214 | 218 | {
|
215 | 219 | const require = bootstrap.require;
|
216 | 220 | const exports = bootstrap.modules['ctx-module'];
|
217 |
| -""" + ctxModuleSource.read() + """ |
| 221 | +""".replace("\n", " ") + "\n" + ctxModuleSource.read() + """ |
218 | 222 | })
|
219 |
| -"""); |
| 223 | +""", { 'filename': node_modules + "/ctx-module/ctx-module.js", 'lineno': 0 }); |
220 | 224 | #broken initCtxModule(bootstrap.require, bootstrap.modules['ctx-module'].exports)
|
221 | 225 | initCtxModule();
|
222 | 226 |
|
@@ -298,7 +302,7 @@ def _createRequireInner(*args):
|
298 | 302 | module.require.path.splice(module.require.path.length, 0, ...(extraPaths.split(':')));
|
299 | 303 |
|
300 | 304 | return module.require;
|
301 |
| -})""")(*args) |
| 305 | +})""", evalOpts)(*args) |
302 | 306 |
|
303 | 307 | def createRequire(filename, extraPaths: Union[List[str], Literal[False]] = False, isMain = False):
|
304 | 308 | """
|
@@ -331,7 +335,7 @@ def runProgramModule(filename, argv, extraPaths=[]):
|
331 | 335 | globalThis.__filename = fullFilename;
|
332 | 336 | globalThis.__dirname = os.path.dirname(fullFilename);
|
333 | 337 | with open(fullFilename, encoding="utf-8", mode="r") as mainModuleSource:
|
334 |
| - pm.eval(mainModuleSource.read()) |
| 338 | + pm.eval(mainModuleSource.read(), {'filename': fullFilename}) |
335 | 339 |
|
336 | 340 | def require(moduleIdentifier: str):
|
337 | 341 | # Retrieve the caller’s filename from the call stack
|
|
0 commit comments