| 378 | |
| 379 | if (len(args) > 0): |
| 380 | async def runJS(): |
| 381 | hasUncaughtException = False |
| 382 | loop = asyncio.get_running_loop() |
| 383 | |
| 384 | def exceptionHandler(loop, context): |
| 385 | "See https://docs.python.org/3.11/library/asyncio-eventloop.html#error-handling-api" |
| 386 | error = context["exception"] |
| 387 | try: |
| 388 | globalInitModule.uncaughtExceptionHandler(error) |
| 389 | except SystemExit: # the "exception" raised by `sys.exit()` call |
| 390 | pass |
| 391 | finally: |
| 392 | pm.stop() # unblock `await pm.wait()` to gracefully exit the program |
| 393 | nonlocal hasUncaughtException |
| 394 | hasUncaughtException = True |
| 395 | loop.set_exception_handler(exceptionHandler) |
| 396 | |
| 397 | def cleanupExit(code=0): |
| 398 | pm.stop() |
| 399 | realExit(code) |
| 400 | realExit = globalThis.python.exit |
| 401 | globalThis.python.exit = cleanupExit |
| 402 | |
| 403 | try: |
| 404 | globalInitModule.patchGlobalRequire() |
| 405 | pm.runProgramModule(args[0], args, requirePath) |
| 406 | await pm.wait() # blocks until all asynchronous calls finish |
| 407 | if hasUncaughtException: |
| 408 | sys.exit(1) |
| 409 | except Exception as error: |
| 410 | print(error, file=sys.stderr) |
| 411 | sys.exit(1) |
| 412 | try: |
| 413 | asyncio.run(runJS()) |
| 414 | except KeyboardInterrupt: |