PythonMonkey   v1.1.1 (dev)
Loading...
Searching...
No Matches
global-init.js File Reference

Functions

 for (let mid in require.cache) delete require.cache[mid]
 
Object assign (python, originalPython)
 
python on ('error', function unhandledError(error) { if(python.listenerCount('error') > 1) return;if(python.listenerCount('error')===0||python.listeners('error')[0]===unhandledError) python.emit('unhandledException', error);})
 

Variables

use strict
 
 const { EventEmitter } = require('events')
 
const originalPython = globalThis.python
 
const python = globalThis.python = new EventEmitter('python')
 
exports patchGlobalRequire
 
exports initReplLibs
 
exports uncaughtExceptionHandler
 
exports unhandledRejectionHandler
 

Detailed Description

Set up global scope which is used to run either program or REPL code in the pmjs script runner.

Author
Wes Garland, wes@d.nosp@m.istr.nosp@m.ibuti.nosp@m.ve.n.nosp@m.etwor.nosp@m.k
Date
June 2023

Function Documentation

◆ assign()

Object assign ( python  ,
originalPython   
)

◆ for()

for ( let mid in require.  cache)

◆ on()

python on ( 'error'  ,
function unhandledError(error) { if(python.listenerCount('error') > 1) return;if(python.listenerCount('error')===0||python.listeners('error')[0]===unhandledError) python.emit('unhandledException', error);}   
)

Variable Documentation

◆ const

const { EventEmitter } = require('events')

◆ initReplLibs

exports initReplLibs
Initial value:
= function pmjs$$initReplLibs()
{
globalThis.util = require('util');
globalThis.events = require('events');
}
require('core-js/actual/dom-exception')

◆ originalPython

const originalPython = globalThis.python

◆ patchGlobalRequire

exports patchGlobalRequire
Initial value:
= function pmjs$$patchGlobalRequire()
{
globalThis.require = require;
}

runProgramModule wants to include the require.cache from the pre-program loads (e.g. via -r or -e), but due to current bugs in PythonMonkey, we can't access the cache property of require because it is a JS function wrapped in a Python function wrapper exposed to script as a native function.

This patch swaps in a descended version of require(), which has the same require.cache, but that has side effects in terms of local module id resolution, so this patch happens only right before we want to fire up the program module.

◆ python

const python = globalThis.python = new EventEmitter('python')

◆ strict

use strict

◆ uncaughtExceptionHandler

exports uncaughtExceptionHandler
Initial value:
= function globalInit$$uncaughtExceptionHandler(error)
{
if (python._events && python._events['uncaughtException'])
python.emit('uncaughtException', error);
else
{
console.error('Uncaught', error);
python.exit(1);
}
}
const python
Definition global-init.js:23
var declare console
Definition global.d.ts:46

Temporary API until we get EventEmitters working. Replace this export with a custom handler.

◆ unhandledRejectionHandler

exports unhandledRejectionHandler
Initial value:
= function globalInit$$unhandledRejectionHandler(error)
{
if (python._events && python._events['uncaughtRejection'])
python.emit('unhandledRejection', error);
else
{
console.error(error);
python.exit(1);
}
}

Temporary API until we get EventEmitters working. Replace this export with a custom handler.