From 8ab61defddb3fdae66e93b3ee6e91ff494ff0951 Mon Sep 17 00:00:00 2001 From: feliz Date: Wed, 1 Feb 2023 23:46:35 +0100 Subject: [PATCH] Created a test, to showcase the bug-fix of Runtime Run with "node fix1.js" --- vanesca-tests/fix1.js | 28 ++++++++++++++++++++++++++++ vanesca-tests/functions1.js | 10 ++++++++++ 2 files changed, 38 insertions(+) create mode 100644 vanesca-tests/fix1.js create mode 100644 vanesca-tests/functions1.js 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/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