Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/anuoua/minejs
用ts实现的js解释器
https://github.com/anuoua/minejs
Last synced: about 2 months ago
JSON representation
用ts实现的js解释器
- Host: GitHub
- URL: https://github.com/anuoua/minejs
- Owner: anuoua
- Created: 2019-07-19T11:18:56.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-07T13:08:47.000Z (over 1 year ago)
- Last Synced: 2024-06-07T23:34:05.361Z (4 months ago)
- Language: TypeScript
- Homepage:
- Size: 970 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# mine-jsjs
一个ts实现的js解释器
## 安装
```bash
$ npm install mine-jsjs
```## 使用方法
```javascript
import { run } from 'mine-jsjs'run(code, options)
```直接跑代码
```javascript
import { run } from 'mine-jsjs'
run(`console.log("hello world!")`)
// hello world!
```直接跑ast节点
```javascript
import { run } from 'mine-jsjs'
run(
{
"type": "Program",
"start": 0,
"end": 26,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 26,
"expression": {
"type": "CallExpression",
"start": 0,
"end": 26,
"callee": {
"type": "MemberExpression",
"start": 0,
"end": 11,
"object": {
"type": "Identifier",
"start": 0,
"end": 7,
"name": "console"
},
"property": {
"type": "Identifier",
"start": 8,
"end": 11,
"name": "log"
},
"computed": false
},
"arguments": [
{
"type": "Literal",
"start": 12,
"end": 25,
"value": "hello world!",
"raw": "'hello world!'"
}
]
}
}
],
"sourceType": "module"
}
)// hello world!
```参数选项
`options.injectObj`: 通过这个对象可以将外部变量注入到内部代码
例子:
```javascript
import { run } from 'mine-jsjs'
const str = 'hello'
run(`console.log(hello + ' world')`, { hello: str })
// hello world
````options.module`: 按照module的方式运行code,运行后会返回内部代码export的变量
例子:
```javascript
import { run } from 'mine-jsjs'
run(`
exports.hello = 'hello'
`)
// { hello: 'hello' }
```