Skip to content

Commit cee3fb0

Browse files
Merge branch 'philippe/pyTypeFactory-cleanup' into philippe/full-js-and-python-stacks
2 parents 993da0e + b52383c commit cee3fb0

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
@@ -146,7 +146,7 @@
146146
}
147147
""", evalOpts);
148148

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

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

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

python/pythonmonkey/require.py

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

7272
globalThis.python.exit = pm.eval("""'use strict';
7373
(exit) => function pythonExitWrapper(exitCode) {
74+
if (typeof exitCode === 'undefined')
75+
exitCode = pythonExitWrapper.code;
76+
if (typeof exitCode == 'undefined')
77+
exitCode = 0n;
7478
if (typeof exitCode === 'number')
7579
exitCode = BigInt(Math.floor(exitCode));
80+
if (typeof exitCode !== 'bigint')
81+
exitCode = 1n;
7682
exit(exitCode);
7783
}
7884
""", 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)