Skip to content

Commit

Permalink
Fixed sandbox.js
Browse files Browse the repository at this point in the history
There was a bug that didn't allow you to run execute multiple times, especially after something like a delay. A flag was being reset. Being able to run .execute multiple times is part of the charm.

Includes a test.
Run with "node fix2.js"
  • Loading branch information
vanesca88 committed Feb 1, 2023
1 parent 8ab61de commit fdd3549
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
9 changes: 5 additions & 4 deletions dist/host/sandbox.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions vanesca-tests/fix2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

process.stdout.write("\u001b[2J\u001b[00H") // clear screen
const { Sandbox, SandboxCluster } = require('../v8-sandbox')
let sandbox = new Sandbox({ memory: 1000 })

let code1 = `
let t = 1
console.log(t) // works
`; // always end with semicolon

let code2 = ` // repeated executes STACK and should keep their values
t++
console.log(t)
`;

(async () => {

const RUN = 4
console.log('Test no.' + RUN)

// test 1 (runs in sync) (works)
if(RUN === 1) {
sandbox.execute({ code: code1 })
sandbox.execute({ code: code2 })
}

//////////////////////////////////////////

// test 2 (runs first in async/second sync) (doesn't work, even though it should!)
if(RUN === 2) {
await sandbox.execute({ code: code1 })
sandbox.execute({ code: code2 })
}

//////////////////////////////////////////

// test 3 (run in async) (doesn't work, even though it should!)
if(RUN === 3) {
await sandbox.execute({ code: code1 })
await sandbox.execute({ code: code2 })
}

//////////////////////////////////////////

// test 4 (promise result also doesn't work, even though it should!)
if(RUN === 4) {
sandbox.execute({ code: code1 }).then(() =>
// doesn't work, even though it should!
// because of a flag being reset in sandbox.js code
// this is fixed now.
setTimeout(async() => {
sandbox.execute({ code: code2 }) // should display '2' (after fix)
}, 2000)
)
}
})()

0 comments on commit fdd3549

Please sign in to comment.