Skip to content

Commit f72168e

Browse files
Merge branch 'main' into caleb/fix/this
2 parents 703acad + 41a1d1d commit f72168e

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

python/pythonmonkey/cli/pmjs.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
}
146146
""", evalOpts);
147147

148-
def repl():
148+
async def repl():
149149
"""
150150
Start a REPL to evaluate JavaScript code in the extra-module environment. Multi-line statements and
151151
readline history are supported. ^C support is sketchy. Exit the REPL with ^D or ".quit".
@@ -177,7 +177,7 @@ def quit():
177177
"""
178178
Quit the REPL. Repl saved by atexit handler.
179179
"""
180-
sys.exit(0)
180+
globalThis.python.exit(); # need for python.exit.code in require.py
181181

182182
def sigint_handler(signum, frame):
183183
"""
@@ -234,6 +234,7 @@ def sigint_handler(signum, frame):
234234
#
235235
while got_sigint < 2:
236236
try:
237+
await asyncio.sleep(0)
237238
inner_loop = False
238239
if (statement == ""):
239240
statement = input('> ')[readline_skip_chars:]
@@ -258,6 +259,7 @@ def sigint_handler(signum, frame):
258259
# SIGINT is received, so we have to patch things up so that the next-entered line is
259260
# treated as the input at the top of the loop.
260261
while (got_sigint == 0):
262+
await asyncio.sleep(0)
261263
inner_loop = True
262264
lineBuffer = input('... ')
263265
more = lineBuffer[readline_skip_chars:]
@@ -361,8 +363,13 @@ async def runJS():
361363
await pm.wait() # blocks until all asynchronous calls finish
362364
asyncio.run(runJS())
363365
elif (enterRepl or forceRepl):
364-
globalInitModule.initReplLibs()
365-
repl()
366+
async def runREPL():
367+
globalInitModule.initReplLibs()
368+
await repl()
369+
await pm.wait()
370+
asyncio.run(runREPL())
371+
372+
globalThis.python.exit(); # need for python.exit.code in require.py
366373

367374
if __name__ == "__main__":
368375
main()

python/pythonmonkey/require.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,14 @@
7070

7171
globalThis.python.exit = pm.eval("""'use strict';
7272
(exit) => function pythonExitWrapper(exitCode) {
73+
if (typeof exitCode === 'undefined')
74+
exitCode = pythonExitWrapper.code;
75+
if (typeof exitCode == 'undefined')
76+
exitCode = 0n;
7377
if (typeof exitCode === 'number')
7478
exitCode = BigInt(Math.floor(exitCode));
79+
if (typeof exitCode !== 'bigint')
80+
exitCode = 1n;
7581
exit(exitCode);
7682
}
7783
""", evalOpts)(sys.exit);

tests/js/set-timeout.simple

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @file set-timeout.simple
3+
* Simple test which ensures that setTimeout() fires, and that it fires at about the right
4+
* time.
5+
* @author Wes Garland, [email protected]
6+
* @date March 2024
7+
*/
8+
9+
const time = python.eval('__import__("time").time');
10+
const start = time();
11+
12+
/**
13+
* - must fire later than 100ms
14+
* - must fire before 10s
15+
* - 10s is well before 100s but very CI-load-tolerant; the idea is not to check for accurancy, but
16+
* rather ensure we haven't mixed up seconds and milliseconds somewhere.
17+
*/
18+
function check100ms()
19+
{
20+
end = time();
21+
22+
if (end - start < 0.1)
23+
throw new Error('timer fired too soon');
24+
if (end - start > 10)
25+
throw new Error('timer fired too late');
26+
27+
console.log('done - timer fired after ', (end-start) * 1000, 'ms');
28+
python.exit.code = 0;
29+
}
30+
31+
python.exit.code = 2;
32+
setTimeout(check100ms, 100);

0 commit comments

Comments
 (0)