Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Runtime.js to not always remove last argument #32

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

12 changes: 10 additions & 2 deletions dist/sandbox/runtime.js

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

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

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

let code = `
async function test() {

// always works! (water-fall/callback embedded)
addNumbers(1, 2, function(result) {
if(result.error) console.log('error1:', result.error)
if(result.value) console.log('value1:', result.value)
})

// only works with the runtime.js fix (standard await)
// otherwise the last argument is removed, because it expects a callback (embedded)
let {error, value} = await addNumbers(1, 2)
if(error) console.log('error2:', error)
if(value) console.log('value2:', value)
}
test()
`; // always end with semicolon

(async () => {

// test 1 (runs in async)
await sandbox.execute({ code })
})()
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)
)
}
})()
10 changes: 10 additions & 0 deletions vanesca-tests/functions1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

// Create or require your own functions and libraries (more safe)

// Async (water-fall method / callback)
defineAsync('addNumbers', ([ value1, value2 ], { respond, callback }) => {
respond() // always required!
setTimeout(() => {
callback({error: null, value: value1 + value2}) // send to sandbox
}, 1000) // 1 sec delay (for testing)
})