Skip to content
A safe eval library based on WebAssembly and Duktape/QuickJS.
C JavaScript Python C++ Makefile Objective-C
Branch: master
Clone or download
Latest commit c0e0f33 Jan 4, 2020
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
duktape init Jan 3, 2020
example
quickjs test build of quickjs Jan 4, 2020
src build two target Jan 4, 2020
test
.gitignore
.npmignore add npmignore Jan 3, 2020
LICENSE Create LICENSE Jan 3, 2020
README.md update readme & entry Jan 4, 2020
duktapeEval.js
index.js update readme & entry Jan 4, 2020
package.json
quickjsEval.js build two target Jan 4, 2020
rollup.config.js
yarn.lock init Jan 3, 2020

README.md

wasm-jseval

A safe eval library based on WebAssembly and Duktape/QuickJS.

Duktape Demo | Source code of Duktape Demo

Usage

In node:

const { duktapeEval, quickjsEval } = require('wasm-jseval')
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
})
quickjsEval.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://unpkg.com/wasm-jseval/duktapeEval.js"></script>
<!-- <script src="https://unpkg.com/wasm-jseval/quickjsEval.js"></script> -->
<script>
	// or quickjsEval
	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 duktapeEval instance.

quickjsEval.getInstance(): Promise<Instance>

Returns a Promise to resolve the quickjsEval 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?

duktapeEval can run ES5 syntax and some ES6, ES7 capabilities. quickjsEval can run almost complete feature set of ES10.

Why two version?

duktapeEval is smaller, but less feature. quickjsEval has a more complete JavaScript support, but it result in bigger size.

How can I pass data to it?

JSON.stringify is your friend. newFunction is a good choice too.

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?

duktapeEval is about 348kB, and gzipped version is 154kB. quickjsEval is about 484kB, and gzipped version is 225kB.

You can’t perform that action at this time.