Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maple3142/wasm-jseval
A safe eval library based on WebAssembly and Duktape/QuickJS.
https://github.com/maple3142/wasm-jseval
duktape eval webassembly
Last synced: 2 months ago
JSON representation
A safe eval library based on WebAssembly and Duktape/QuickJS.
- Host: GitHub
- URL: https://github.com/maple3142/wasm-jseval
- Owner: maple3142
- License: mit
- Created: 2020-01-03T14:58:13.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-01-04T03:36:22.000Z (almost 5 years ago)
- Last Synced: 2024-05-08T12:33:15.449Z (5 months ago)
- Topics: duktape, eval, webassembly
- Language: C
- Homepage:
- Size: 4.13 MB
- Stars: 224
- Watchers: 5
- Forks: 8
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-list - wasm-jseval
README
# wasm-jseval
A safe **eval** library based on WebAssembly and [Duktape](https://duktape.org/)/[QuickJS](https://bellard.org/quickjs/).
[Duktape Demo](https://gh.maple3142.net/maple3142/duktape-eval/ae198189baf244ff062901475e8877637d265df3/example/example.html) | [Source code of Duktape Demo](https://github.com/maple3142/duktape-eval/blob/master/example/example.html)
## Usage
In node:
```js
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:
```html
// 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
})```
## API
### `duktapeEval.getInstance(): Promise`
Returns a Promise to resolve the duktapeEval instance.
### `quickjsEval.getInstance(): Promise`
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.