Skip to content

Instantly share code, notes, and snippets.

@imolorhe
Created August 7, 2019 22:03
Show Gist options
  • Save imolorhe/a4ad3b76d8d0b522a3b6c620442f7d2a to your computer and use it in GitHub Desktop.
Save imolorhe/a4ad3b76d8d0b522a3b6c620442f7d2a to your computer and use it in GitHub Desktop.

https://github.com/Siubaak/sval

import Sval from 'sval'

// Sval options
const options = {
  // ECMA Version of the code (5 | 6 | 7 | 8 | 9 | 10 | 2015 | 2016 | 2017 | 2018 | 2019)
  ecmaVer: 9,
  // Whether the code runs in a sandbox
  sandBox: true,
}

// Create a interpreter
const interpreter = new Sval(options)

// Add global modules in interpreter
interpreter.import('importWhatYouNeed', 'AllKindsOfStuffs')
// Or interpreter.import({ importWhatYouNeed: 'AllKindsOfStuffs' })
console.log('Sval Version: ' + Sval.version);
const interpreter = new Sval()
interpreter.import({
getItem: n => new Promise(resolve => setTimeout(resolve, 1500, n))
})
interpreter.run(`
const program = async() => {
for (const i of [1, 2, 3]) {
console.log(await getItem(i))
}
return await getItem(45);
};
exports.end = program();
`)
console.log('End: ' + interpreter.exports.end)
interpreter.exports.end.then(res => console.log('result:', res));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment