Skip to content

Commit

Permalink
Created a test, to showcase the bug-fix of Runtime
Browse files Browse the repository at this point in the history
Run with "node fix1.js"
  • Loading branch information
vanesca88 committed Feb 1, 2023
1 parent cdc2a65 commit 8ab61de
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
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 })
})()
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)
})

0 comments on commit 8ab61de

Please sign in to comment.