Skip to content

Mister-Hope/es-parser

Repository files navigation

es-parser

Author: Mr.Hope

一个 110KB 大小的 JS 解释器/运行器

A 110KB size JS parser and runner.

Import

TypeScript

import * as esParser from "es-parser";

JavaScript

const esParser = require("es-parser");

API

  • run:
run(codeString: string, globalMap: GlobalMap)

interface GlobalMap{
  [prop: globalIdentifier]: value
}

Demo

esParser.run("console.log('hello world')");

esParser.run(
  `
const progress = wx.downloadFile({
  url: 'https://www.baidu.com/img/bd_logo1.png',
  success: res => {
    wx.hideLoading();
    if (res.statusCode === 200) console.log(success, res.tempFilePath);
    else wx.showToast({ title: '下载失败' });
  });
  },
  fail: failMsg => {
    wx.hideLoading();
    wx.showToast({ title: '下载失败' });
  }
});

progress.onProgressUpdate(res => {
  wx.showLoading({ title: \`下载中\${Math.round(res.progress)}%\` });
});
`,
  { wx }
);
// this will pass the global variable `wx` to the parser

Known Bugs

None, but package is still in test.

Notes

  • You should always access isFinite, isNaN through Number.

TODO

  • add Stack Track