|
import { registry } from './interpreters.js'; |
|
|
|
const beforeRun = 'BeforeRun'; |
|
const afterRun = 'AfterRun'; |
|
|
|
export const code = [ |
|
`code${beforeRun}`, |
|
`code${beforeRun}Async`, |
|
`code${afterRun}`, |
|
`code${afterRun}Async`, |
|
]; |
|
|
|
export const js = [ |
|
'onWorker', |
|
'onReady', |
|
`on${beforeRun}`, |
|
`on${beforeRun}Async`, |
|
`on${afterRun}`, |
|
`on${afterRun}Async`, |
|
]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function patch(resolved, interpreter) { |
|
const { run, runAsync } = registry.get(this.type); |
|
return { |
|
...resolved, |
|
run: run.bind(this, interpreter), |
|
runAsync: runAsync.bind(this, interpreter) |
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const polluteJS = (module, resolved, ref, isAsync, before, after) => { |
|
if (before || after) { |
|
const patched = patch.bind(module, resolved); |
|
const name = isAsync ? 'runAsync' : 'run'; |
|
const method = module[name]; |
|
module[name] = isAsync ? |
|
async function (interpreter, code, ...args) { |
|
if (before) await before.call(this, patched(interpreter), ref); |
|
const result = await method.call( |
|
this, |
|
interpreter, |
|
code, |
|
...args |
|
); |
|
if (after) await after.call(this, patched(interpreter), ref); |
|
return result; |
|
} : |
|
function (interpreter, code, ...args) { |
|
if (before) before.call(this, patched(interpreter), ref); |
|
const result = method.call(this, interpreter, code, ...args); |
|
if (after) after.call(this, patched(interpreter), ref); |
|
return result; |
|
} |
|
; |
|
} |
|
}; |
|
|
|
|