Fields as private

This commit is contained in:
Adria Navarro 2024-02-06 17:06:19 +01:00
parent 599860b558
commit 1ee20b0464
1 changed files with 13 additions and 14 deletions

View File

@ -37,10 +37,10 @@ class ScriptRunner {
} }
class IsolatedVM { class IsolatedVM {
isolate: ivm.Isolate #isolate: ivm.Isolate
vm: ivm.Context #vm: ivm.Context
#jail: ivm.Reference #jail: ivm.Reference
script: ivm.Module = undefined! #script: ivm.Module = undefined!
#bsonModule?: ivm.Module #bsonModule?: ivm.Module
constructor({ constructor({
@ -50,23 +50,22 @@ class IsolatedVM {
memoryLimit: number memoryLimit: number
parseBson: boolean parseBson: boolean
}) { }) {
this.isolate = new ivm.Isolate({ memoryLimit }) this.#isolate = new ivm.Isolate({ memoryLimit })
this.vm = this.isolate.createContextSync() this.#vm = this.#isolate.createContextSync()
this.#jail = this.vm.global this.#jail = this.#vm.global
this.#jail.setSync("global", this.#jail.derefInto()) this.#jail.setSync("global", this.#jail.derefInto())
// this.#parseBson = parseBson
if (parseBson) { if (parseBson) {
const bsonSource = loadBundle(BundleType.BSON) const bsonSource = loadBundle(BundleType.BSON)
this.#bsonModule = this.isolate.compileModuleSync(bsonSource) this.#bsonModule = this.#isolate.compileModuleSync(bsonSource)
this.#bsonModule.instantiateSync(this.vm, specifier => { this.#bsonModule.instantiateSync(this.#vm, specifier => {
throw new Error(`No imports allowed. Required: ${specifier}`) throw new Error(`No imports allowed. Required: ${specifier}`)
}) })
} }
} }
getValue(key: string) { getValue(key: string) {
const ref = this.vm.global.getSync(key, { reference: true }) const ref = this.#vm.global.getSync(key, { reference: true })
const result = ref.copySync() const result = ref.copySync()
ref.release() ref.release()
return result return result
@ -82,12 +81,12 @@ class IsolatedVM {
if (this.#bsonModule) { if (this.#bsonModule) {
code = `import {deserialize} from "compiled_module";${code}` code = `import {deserialize} from "compiled_module";${code}`
} }
this.script = this.isolate.compileModuleSync(code) this.#script = this.#isolate.compileModuleSync(code)
} }
runScript() { runScript() {
if (this.#bsonModule) { if (this.#bsonModule) {
this.script.instantiateSync(this.vm, specifier => { this.#script.instantiateSync(this.#vm, specifier => {
if (specifier === "compiled_module") { if (specifier === "compiled_module") {
return this.#bsonModule! return this.#bsonModule!
} }
@ -97,14 +96,14 @@ class IsolatedVM {
} }
let result let result
this.vm.global.setSync( this.#vm.global.setSync(
"cb", "cb",
new ivm.Callback((value: any) => { new ivm.Callback((value: any) => {
result = value result = value
}) })
) )
this.script.evaluateSync({ timeout: JS_TIMEOUT_MS }) this.#script.evaluateSync({ timeout: JS_TIMEOUT_MS })
return result return result
} }