0.1.1 • Published 4 years ago

duktape-eval v0.1.1

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

duktape-eval

A safe eval library based on WebAssembly build of Duktape.

Demo | Source code of Demo

Usage

In node:

const { getInstance } = require('duktape-eval')
getInstance().then(mod => {
	console.log(mod.eval('1+1')) // 2
	const add = mod.newFunction(['a', 'b'], 'return a+b')
	console.log(add(1, 2)) // 3
})

In browser:

<script src="https://cdn.jsdelivr.net/gh/maple3142/duktape-eval/duktapeEval.js"></script>
<script>
	duktapeEval.getInstance().then(mod => {
		console.log(mod.eval('1+1')) // 2
		const add = mod.newFunction(['a', 'b'], 'return a+b')
		console.log(add(1, 2)) // 3
	})
</script>

API

duktapeEval.getInstance(): Promise<Instance>

Returns a Promise to resolve the module instance.

Instance

Instance#eval(code: string): any

Evaluate JavaScript string in Duktape engine, and return a value.

Instance#newFunction(argnames: string[], body: string): (...any) => any

Create a function like new Function to be called afterward.

Q&A

What can it runs?

Whatever Duktape can run. Basically ES5 syntax and some ES6, ES7 capabilities.

How can I pass data to it?

JSON.stringify is your friend.

How can I return data from it?

Just like normal eval, for example var a={};a.prop=1;a will return { prop: 1 }. But keep in mind, only JSON serializable data can be returned.

How big is this?

Script size: 348kB Gzipped size: 154kB

0.1.1

4 years ago

0.1.0

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago