|
'use strict'; |
|
const { registry } = require('./interpreters.js'); |
|
|
|
const beforeRun = 'BeforeRun'; |
|
const afterRun = 'AfterRun'; |
|
|
|
const code = [ |
|
`code${beforeRun}`, |
|
`code${beforeRun}Async`, |
|
`code${afterRun}`, |
|
`code${afterRun}Async`, |
|
]; |
|
exports.code = code; |
|
|
|
const js = [ |
|
'onWorker', |
|
'onReady', |
|
`on${beforeRun}`, |
|
`on${beforeRun}Async`, |
|
`on${afterRun}`, |
|
`on${afterRun}Async`, |
|
]; |
|
exports.js = js; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function patch(resolved, interpreter) { |
|
const { run, runAsync } = registry.get(this.type); |
|
return { |
|
...resolved, |
|
run: run.bind(this, interpreter), |
|
runAsync: runAsync.bind(this, interpreter) |
|
}; |
|
} |
|
exports.patch = patch |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
} |
|
; |
|
} |
|
}; |
|
exports.polluteJS = polluteJS; |
|
|
|
|