diff --git a/dist/host/sandbox.js b/dist/host/sandbox.js index 78cf1e6..6a5e085 100644 --- a/dist/host/sandbox.js +++ b/dist/host/sandbox.js @@ -103,9 +103,11 @@ class Sandbox { } async execute({ code, timeout, globals, context, }) { this.start(); - const result = await this.initialize({ timeout }); - if (result.error) { - return result; + if(!this.initialized) { + const result = await this.initialize({ timeout }); + if (result.error) { + return result; + } } return new Promise((resolve) => { this.queue.push({ @@ -117,7 +119,6 @@ class Sandbox { context: context ?? {}, output: [], callback: (res) => { - this.initialized = false; resolve(res); }, }); diff --git a/dist/sandbox/runtime.js b/dist/sandbox/runtime.js index 0c333dd..48e1352 100644 --- a/dist/sandbox/runtime.js +++ b/dist/sandbox/runtime.js @@ -32,8 +32,16 @@ environment.define = (name) => { }; environment.defineAsync = (name) => { environment[name] = (...args) => { - const callback = args.pop(); - return environment.dispatch(name, args, callback); + if (typeof args[args.length-1] == 'function') { // water-fall function + let callback = args.pop(); + return environment.dispatch(name, args, callback); + }else { + return new Promise(async(resolve, reject) => { // standard await function + environment.dispatch(name, args, function(value) { + resolve(value); + }) + }) + } }; }; const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; diff --git a/vanesca-tests/fix1.js b/vanesca-tests/fix1.js new file mode 100644 index 0000000..6a19b49 --- /dev/null +++ b/vanesca-tests/fix1.js @@ -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 }) +})() \ No newline at end of file diff --git a/vanesca-tests/fix2.js b/vanesca-tests/fix2.js new file mode 100644 index 0000000..546cd24 --- /dev/null +++ b/vanesca-tests/fix2.js @@ -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) + ) + } +})() \ No newline at end of file diff --git a/vanesca-tests/functions1.js b/vanesca-tests/functions1.js new file mode 100644 index 0000000..d9951fd --- /dev/null +++ b/vanesca-tests/functions1.js @@ -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) +}) \ No newline at end of file