{"_id":"l7eval5","_rev":"4-41e5d003158c3653bfd879814aa45e32","name":"l7eval5","dist-tags":{"latest":"0.0.3"},"versions":{"0.0.1":{"name":"l7eval5","version":"0.0.1","main":"./dist/cjs/index.js","module":"./dist/esm/index.js","types":"./lib/index.d.ts","scripts":{"start":"tsc -w","build":"run-s clear build:* bundle:dev bundle:prod bundle:banner","build:lib":"tsc","build:cjs":"babel lib --config-file=./babel.config.js  --out-dir dist/cjs","build:esm":"babel lib --config-file=./babel.config.esm.js  --out-dir dist/esm","clear":"rimraf lib dist umd","bundle:dev":"packez bundle ./src/index.ts -d umd -c","bundle:prod":"packez bundle ./src/index.ts -d umd --state prod","bundle:banner":"node scripts/banner.js","prepublishOnly":"run-s test build","test":"jest"},"keywords":["interpreter","js-interpreter","eval","Function","vm","eval5"],"license":"MIT","devDependencies":{"@babel/cli":"^7.8.4","@types/fs-extra":"^8.1.0","@types/jest":"^24.0.25","babel-plugin-search-and-replace":"^1.0.1","babel-preset-packez":"^1.0.0","fs-extra":"^8.1.0","jest":"^24.9.0","npm-run-all":"^4.1.5","packez":"^3.1.0","rimraf":"^3.0.0"},"dependencies":{"@babel/runtime":"^7.8.4","@types/acorn":"^4.0.5","@types/estree":"0.0.41","acorn":"^7.1.0"},"browserslist":[">=0.25%","not dead","not op_mini all","not Android 4.4.3-4.4.4","not ios_saf < 10","not Chrome < 50","firefox ESR"],"gitHead":"a6f1c8c1020a347ee56e210e364426d87628c351","description":"中文 | [English](./README-en_US.md)","_id":"l7eval5@0.0.1","_nodeVersion":"14.16.1","_npmVersion":"7.20.1","dist":{"integrity":"sha512-knqZOm9vyjevkzA/zWN09Oz8HHuurzc7nQtS3+i+k2/iAVY5UyZvoc/5eVXzhO2sUrvufZWvq93l0N9cqAzcIw==","shasum":"11f906534e1469ccbfcdb8430c93ebebf230c994","tarball":"https://registry.npmjs.org/l7eval5/-/l7eval5-0.0.1.tgz","fileCount":40,"unpackedSize":1075872,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhCjbICRA9TVsSAnZWagAAiFUP/jlJLVjI5wXn7z2kepUs\nqGrmRxybZGNvS/ni38vCSnO+P3UlMPucmsQjdVAw4gfTDyh64visF8FYiSSB\npTV0nH2ld9v7tT/GzxAWI1JVRotTgsqvGdyDVu9Ijz/Z8Z+DbeoJID8pVshR\n9Wq2YCGhJ8iyg6gBSNdcGdEbkPlYl2l58bvQ1aiGeWZyrQztQ2ZspNM7TSUJ\njTL5PdwOCw7Ku/MoNVDyKivcpT7FCHreXDP1qqsS+wdGBxS8pAO4jEXrTgUU\nyAXntLzzxGvwbIMkQUr9J7YH3S4EqKTuVygmj8yAXsMfyd/kmPBzp6tsmnnf\nMoZ0ZG+zd8WR4QgCeAho3raUHTSKQQxtIlxA0ij4WiyvVbnk5PLS7MYLw9RB\nFNBSZ/hntbcAU9gmyC5UzUceHBDeGWIs1KyI2z0W4TyrO7Z8OiMmNMRyfHG0\neO0oVIX/sIQUApa59kuwOkBjKoMy5rba9qgkOCWqLjLwvHactchd12JaJA9C\nGZfEFOCqIgN0KmE3JSZ+Z2m+I7tRpQMbbBWkcosbBt1e+nITxXyT+B3NZjE/\nNpQoige3yn3CjRwFr4CBxgkWUIVmR0oqaWcEUng0g90XuxHo60Yzof+6IjfV\nbvoM2iJiWZ/or+ss+sXXjhGbEhRJED6qmPhH0/rAEdn2XcXdE1kaSkvw4txT\ndYR+\r\n=GRm7\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCNMRyDnyqFd/kI5hCafj2TWZN+y2SoZ+ubrQDymrcA3wIgZvRocP/JgWeKam0LEezUh9yQbs/ytGcZGWMSbJ+fqEk="}]},"_npmUser":{"name":"yiqianyao","email":"2912401452@qq.com"},"directories":{},"maintainers":[{"name":"yiqianyao","email":"2912401452@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/l7eval5_0.0.1_1628059335905_0.553140317299144"},"_hasShrinkwrap":false},"0.0.2":{"name":"l7eval5","version":"0.0.2","main":"./dist/cjs/index.js","module":"./dist/esm/index.js","types":"./lib/index.d.ts","scripts":{"start":"tsc -w","build":"run-s clear build:* bundle:dev bundle:prod bundle:banner","build:lib":"tsc","build:cjs":"babel lib --config-file=./babel.config.js  --out-dir dist/cjs","build:esm":"babel lib --config-file=./babel.config.esm.js  --out-dir dist/esm","clear":"rimraf lib dist umd","bundle:dev":"packez bundle ./src/index.ts -d umd -c","bundle:prod":"packez bundle ./src/index.ts -d umd --state prod","bundle:banner":"node scripts/banner.js","prepublishOnly":"run-s test build","test":"jest"},"keywords":["interpreter","js-interpreter","eval","Function","vm","eval5"],"license":"MIT","devDependencies":{"@babel/cli":"^7.8.4","@types/fs-extra":"^8.1.0","@types/jest":"^24.0.25","babel-plugin-search-and-replace":"^1.0.1","babel-preset-packez":"^1.0.0","fs-extra":"^8.1.0","jest":"^24.9.0","npm-run-all":"^4.1.5","packez":"^3.1.0","rimraf":"^3.0.0"},"dependencies":{"@babel/runtime":"^7.8.4","@types/acorn":"^4.0.5","@types/estree":"0.0.41","acorn":"^7.1.0"},"browserslist":[">=0.25%","not dead","not op_mini all","not Android 4.4.3-4.4.4","not ios_saf < 10","not Chrome < 50","firefox ESR"],"gitHead":"a6f1c8c1020a347ee56e210e364426d87628c351","description":"中文 | [English](./README-en_US.md)","_id":"l7eval5@0.0.2","_nodeVersion":"14.16.1","_npmVersion":"7.20.1","dist":{"integrity":"sha512-klGYkcnMhQUQPgQqq3gU+Mgv3Z3ncRWieqPCKL0IJhoOfAlbyyxGIn+WLcE1nWx3I7dkJflnVAK2U6vxrhtrGA==","shasum":"71529ebe9d6ec4791600bbdefdcc874e02c1e1b4","tarball":"https://registry.npmjs.org/l7eval5/-/l7eval5-0.0.2.tgz","fileCount":40,"unpackedSize":1076052,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhE3RACRA9TVsSAnZWagAAQ3cP/0I5C/f/aWf07eNseDdP\npj88iTQ7TEAOM/1cw1m1kk0ii7oY7u2HWzS0pioYrLRZYHIGEbtwWD6MNOs/\ng5ZIDusaZdD0xCphFDzNwmZRsA1mB4wvv5qwPq0v5yRTHYLzufqqRa4eaA2Z\nVAd+4TmSfuqtv03/JbiIN0Np20t2xvA/LV9u9FAkpEmS++tpYHZ+M47ojhej\nxxInZQiHVLC9zOXhMR26RoZZYnCXwWcA0IiJmuHdgljmibVrjgBGYAALC+Zl\ntTdh8aB4JITRwxXKiRxlWHUYeU722FKs8vhYfhlAaVMqLmhE2zDzvrAf5Ilh\ne6ktmDPuMmZuR8rxOG+zX/o4YoSc5DmdG1HZ8yTd/LbsaT3XGHXzUTJlUGBX\nVa7FvW0fI3Y5Evtmy2yunsHH+iuD9rX2CpWYBSoQSBFldGcXmp6VOujf0QdA\n4P6ulD/UwWqx/Y/MQ1Byh6IBdIMT5x9CYCWV6vVjWaORF8psYsHxacc9IwRJ\ncTtAmOzV6NL7qbZi+dLy506BUGDJevDDWMiNLL1MtdrgJo9GI1OnPzWpgE3k\nDjuQWN6w+Yz366GYszZegV4dmLCb/9glmkrh5j1uBKlkvA/eoL0ij6kBwxZE\nBaCN5dF8bqu5v2ReiUwwF8YTM3NsLE5dCdJ7r4JvDi4yAIzH2O7BUmEdzDwj\nsHbE\r\n=RymG\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIEn5a1EvHYhm4xguaPK601lnym5JyziViBbLs86XYl+sAiEA8gEqgOkW53xXYLJcI5jv4HchwtSQUrS7TZ5HZE6pavU="}]},"_npmUser":{"name":"yiqianyao","email":"2912401452@qq.com"},"directories":{},"maintainers":[{"name":"yiqianyao","email":"2912401452@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/l7eval5_0.0.2_1628664896642_0.28304478931322086"},"_hasShrinkwrap":false},"0.0.3":{"name":"l7eval5","version":"0.0.3","main":"./dist/cjs/index.js","module":"./dist/esm/index.js","types":"./lib/index.d.ts","scripts":{"start":"tsc -w","build":"run-s clear build:* bundle:dev bundle:prod bundle:banner","build:lib":"tsc","build:cjs":"babel lib --config-file=./babel.config.js  --out-dir dist/cjs","build:esm":"babel lib --config-file=./babel.config.esm.js  --out-dir dist/esm","clear":"rimraf lib dist umd","bundle:dev":"packez bundle ./src/index.ts -d umd -c","bundle:prod":"packez bundle ./src/index.ts -d umd --state prod","bundle:banner":"node scripts/banner.js","prepublishOnly":"run-s test build","test":"jest"},"keywords":["interpreter","js-interpreter","eval","Function","vm","eval5"],"license":"MIT","devDependencies":{"@babel/cli":"^7.8.4","@types/fs-extra":"^8.1.0","@types/jest":"^24.0.25","babel-plugin-search-and-replace":"^1.0.1","babel-preset-packez":"^1.0.0","fs-extra":"^8.1.0","jest":"^24.9.0","npm-run-all":"^4.1.5","packez":"^3.1.0","rimraf":"^3.0.0"},"dependencies":{"@babel/runtime":"^7.8.4","@types/acorn":"^4.0.5","@types/estree":"0.0.41","acorn":"^7.1.0"},"browserslist":[">=0.25%","not dead","not op_mini all","not Android 4.4.3-4.4.4","not ios_saf < 10","not Chrome < 50","firefox ESR"],"gitHead":"a6f1c8c1020a347ee56e210e364426d87628c351","description":"中文 | [English](./README-en_US.md)","_id":"l7eval5@0.0.3","_nodeVersion":"14.16.1","_npmVersion":"8.1.0","dist":{"integrity":"sha512-xnn9x/T0zawTM1L9DASmRXVMb5fTCib83FtGZQcn5ToM1lAo4dutNOK2JAC+jd3mEMWa9MMq188dyoQcqG2WOg==","shasum":"fac75ffdf2dd25de021fb102509196f586779bcb","tarball":"https://registry.npmjs.org/l7eval5/-/l7eval5-0.0.3.tgz","fileCount":40,"unpackedSize":1076241,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJh2xdPCRA9TVsSAnZWagAAkWEP/3GWUZsGZm0ztHRbvv6u\nj6DhhxXXdPkQHyIRhBevrNnu4UMYVNAEQdUzca52/oK0jaNOyj4cGQVziAdU\nmQJNnO9pkbeEK9ezkA/+neGHroJr2QdgsyAAU+klqTO6KlB/eGG82e9ValMZ\n3CzuxuByb1Clsh2UK4YImTfrYE4r/1IONPBh+MF8zTqcEN8P2aowhzt8BQ5h\n42sMWuPqfFHACvKXy8/yR4HpiHD4orNssoUPWzjatBzmXqfa7xSzA3sOuDMj\nCawieX76XIrZ3ZtC0ZM1o3KsXgLXq3Uo228Otot1fCYNo3whpMeDP5cfhP1B\n9EfnWm7s2yyZvRqKltosdkSHNU2Ov+G2r/p+t6nUHtJyOYJb03iCOCK2z1Z+\nZVj+8UyuVcMAD6Ppc+3t3ZQ/SjuVKXjk1hPY9kOosw+ticb3TTyd8oHhn0/i\n1SJJfHP3h0A0FNyXqz0Ai3WwC4wVvaZhoJZ279agiuHC/gCckCVOjsXTB1eO\nair0YcoarEAAZaxhT8S0oCKu7SIQoRmAaHiiX9xxU39H+B9QA9hUfJd30+Af\nFoECLul/KidDVLaowTIP8EC2aHMQ4guRubq0JOpCIK/8O9tU21B8yaiKsn1N\nFQtR0alXDQZx02xhrkGZIhVCVtGCb40ZMZqsWCmeTqm01cdDW1HhXAUG8Yn+\nzEgX\r\n=mBqM\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCspQjOX8xU4yDvt5E9KoBDP0+DyP35VJ8bsy9DZlJo0AIgchRUCbwxfY3XTnxLLqneAIoj/c5NE+pdagsZFw9f9Xs="}]},"_npmUser":{"name":"yiqianyao","email":"2912401452@qq.com"},"directories":{},"maintainers":[{"name":"yiqianyao","email":"2912401452@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/l7eval5_0.0.3_1636462221250_0.9667103587512551"},"_hasShrinkwrap":false}},"time":{"created":"2021-08-04T06:42:15.904Z","0.0.1":"2021-08-04T06:42:16.133Z","modified":"2022-05-07T14:10:52.052Z","0.0.2":"2021-08-11T06:54:56.786Z","0.0.3":"2021-11-09T12:50:21.410Z"},"maintainers":[{"name":"yiqianyao","email":"2912401452@qq.com"}],"description":"中文 | [English](./README-en_US.md)","keywords":["interpreter","js-interpreter","eval","Function","vm","eval5"],"license":"MIT","readme":"# eval5\n\n中文 | [English](./README-en_US.md)\n\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/bplok20010/eval5/blob/master/LICENSE)\n[![npm](https://img.shields.io/npm/v/eval5)](https://www.npmjs.com/package/eval5)\n[![npm bundle size](https://img.shields.io/bundlephobia/min/eval5)](https://raw.githubusercontent.com/bplok20010/eval5/master/umd/eval5.min.js)\n\n基于 TypeScript 编写的 JavaScript 解释器，支持完整 ES5 语法\n\n**支持浏览器、node.js、小程序等 JavaScript 运行环境**\n\n[在线体验](https://bplok20010.github.io/eval5/)\n\n[更多示例](https://bplok20010.github.io/eval5/examples.html)\n\n## 使用场景\n\n-   浏览器环境中需要使用沙盒环境执行 JavaScript 脚本\n-   控制执行时长\n-   不支持`eval` `Function`的 JavaScript 运行环境：如 微信小程序 [demo](https://github.com/bplok20010/eval5-wx-demo) [we-script](https://github.com/bplok20010/we-script) [taro-script](https://github.com/bplok20010/taro-script)\n-   研究/学习用\n\n## 支持 ECMAScript 版本\n\nES5\n\n## 安装\n\n```\nnpm install --save eval5\n```\n\n## 使用\n\n[![Edit eval5](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/eval5-zxndl?fontsize=14&hidenavigation=1&theme=dark)\n\n```javascript\nimport { Interpreter } from \"eval5\";\n\nconst interpreter = new Interpreter(window, {\n\ttimeout: 1000,\n});\n\nlet result;\n\ntry {\n\tresult = interpreter.evaluate(\"1+1\");\n\tconsole.log(result);\n\n\tinterpreter.evaluate(\"var a=100\");\n\tinterpreter.evaluate(\"var b=200\");\n\tresult = interpreter.evaluate(\"a+b\");\n\n\tconsole.log(result);\n} catch (e) {\n\tconsole.log(e);\n}\n```\n\n## 参数\n\n```ts\ninterface Options {\n\t// 默认为：0，不限制\n\ttimeout?: number;\n\t// 根作用域，只读\n\trootContext?: {} | null;\n\tglobalContextInFunction?: any;\n}\n```\n\n示例\n\n```\nimport { Interpreter } from \"eval5\";\n\nconst ctx = {};\nconst interpreter = new Interpreter(ctx, {\n    rootContext: window,\n\ttimeout: 1000,\n});\n\ninterpreter.evaluate(`\n    a = 100;\n    console.log(a); // 100\n`);\n\nwindow.a;//undefined\n\n```\n\n## Interpreter\n\n**`version`**\n\n当前版本\n\n**`global`**\n\n默认值: `{}`\n\n设置默认的全局作用域\n\n```js\nInterpreter.global = window;\nconst interpreter = new Interpreter();\ninterpreter.evaluate('alert(\"hello eval5\")');\n```\n\n**`globalContextInFunction`**\n\n默认值: `undefined`\n\n`eval5` 不支持 `use strict` 严格模式, 在非严格下的函数中`this`默认指向的是全局作用域，但在`eval5`中是`undefined`， 可通过`globalContextInFunction`来设置默认指向。\n\n```js\nimport { Interpreter } from \"Interpreter\";\n\nconst ctx = {};\nconst interpreter = new Interpreter(ctx);\ninterpreter.evaluate(`\nthis; // ctx\nfunction func(){\n    return this; // undefined\n}\nfunc();\n`);\n```\n\n```js\nimport { Interpreter } from \"Interpreter\";\n\nInterpreter.globalContextInFunction = window;\nconst ctx = {};\nconst interpreter = new Interpreter({});\ninterpreter.evaluate(`\nthis; // ctx\nfunction func(){\n    return this; // window\n}\nfunc();\n`);\n```\n\n原因，示例代码：\n\n> **注意: alert异常**\n\n```\nimport { Interpreter } from \"Interpreter\";\n\nInterpreter.globalContextInFunction = {};\n\nconst ctx = {alert: alert};\n\nconst interpreter = new Interpreter(ctx);\n\ninterpreter.evaluate(`\n// throw Illegal invocation\nalert('Hello eval5'); // 同 alert.call({}, 'Hello eval5')\n`);\n```\n\n**`constructor(context = Interpreter.global, options?: Options )`**\n\n构造函数\n\n## Interpreter 的实例方法\n\n**`evaluate(code: string): any`**\n\n执行给定的字符串代码，并返回最后一个表达式的值\n\n```js\nimport { Interpreter } from \"Interpreter\";\n\nconst interpreter = new Interpreter(window);\n\nconst result = interpreter.evaluate(`\nvar a = 100;\nvar b = 200;\n\na+b;\n\n`);\n\nconsole.log(result); // 300\n```\n\n**`appendCode(code: string): any`**\n\n`evaluate`的别名\n\n**`getExecutionTime(): number`**\n\n获取上一次调用`evaluate`的执行时长\n\n**`setExecTimeout(timeout: number = 0): void`**\n\n设置执行时长\n\n**`getOptions(): Readonly<Options>`**\n\n获取解释器参数\n\n---\n\n## evaluate(code: string, ctx?: {}, options?: Options)\n\n执行给定的字符串代码，并返回最后一个表达式的值\n\n> 注: 该函数每次执行都会创建一个新的解释器\n\n```js\nimport { evaluate } from \"eval5\";\n\nevaluate(\n\t`\nvar a = 100;\nvar b = 100;\nconsole.log(a+b);\n`,\n\t{ console: console }\n); // 200\n\nevaluate(`\n    a;\n`); // a is not defined\n```\n\n## Function\n\n该函数会将`Interpreter.global` `Interpreter.globalContextInFunction`当作默认值并创建新的解释器\n\n```js\nimport { Function } from \"eval5\";\n\nconst func = new Function(\"a\", \"b\", \"return a+b;\");\nconsole.log(func(100, 200)); // 300\n```\n\n## vm\n\n查看 [vm](https://nodejs.org/dist/latest-v13.x/docs/api/vm.html)\n\n-   vm.createContext\n-   vm.compileFunction\n-   vm.runInContext\n-   vm.runInNewContext\n-   vm.Script\n\n## License\n\nMIT\n\n## 相关\n\n-   [evaljs](https://github.com/marten-de-vries/evaljs)\n-   [closure-interpreter](https://github.com/int3/closure-interpreter)\n","readmeFilename":"README.md"}