{"id":13666970,"url":"https://github.com/bplok20010/eval5","last_synced_at":"2025-04-26T15:32:02.040Z","repository":{"id":37706460,"uuid":"230759395","full_name":"bplok20010/eval5","owner":"bplok20010","description":"A JavaScript interpreter written in TypeScript - Support ES5","archived":false,"fork":false,"pushed_at":"2024-04-09T06:17:11.000Z","size":1675,"stargazers_count":656,"open_issues_count":0,"forks_count":97,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-04-19T09:32:13.720Z","etag":null,"topics":["eval5","interpreter","javascript-interpreter","js-interpreter","sandbox","typescript-javascript","vm","wechat-mini"],"latest_commit_sha":null,"homepage":"https://bplok20010.github.io/eval5/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bplok20010.png","metadata":{"files":{"readme":"README-en_US.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-29T14:12:27.000Z","updated_at":"2024-06-18T13:40:35.258Z","dependencies_parsed_at":"2024-06-18T13:39:49.763Z","dependency_job_id":"fb3be2bc-30c4-4348-b2ac-2b6dc88d9e8e","html_url":"https://github.com/bplok20010/eval5","commit_stats":{"total_commits":143,"total_committers":3,"mean_commits":"47.666666666666664","dds":0.2727272727272727,"last_synced_commit":"a3007801c64ed29487993b8c42740082c666d281"},"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bplok20010%2Feval5","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bplok20010%2Feval5/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bplok20010%2Feval5/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bplok20010%2Feval5/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bplok20010","download_url":"https://codeload.github.com/bplok20010/eval5/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224039039,"owners_count":17245530,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eval5","interpreter","javascript-interpreter","js-interpreter","sandbox","typescript-javascript","vm","wechat-mini"],"created_at":"2024-08-02T07:00:20.501Z","updated_at":"2024-11-11T02:30:46.839Z","avatar_url":"https://github.com/bplok20010.png","language":"TypeScript","readme":"# eval5\n\n[中文](./README.md) | English\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\nA JavaScript interpreter written in TypeScript, Supports full ES5 syntax.\n\n\u003e Support JavaScript running environment such as browser, node.js, WeChat Mini Program, etc\n\n[Try it out](https://bplok20010.github.io/eval5/)\n\n[Examples](https://bplok20010.github.io/eval5/examples.html)\n\n## You may not need it unless\n\n-   Need to execute code in the browser with a sandbox environment\n-   Controlling execution time\n-   JavaScript runtime environment that does not support `eval` and `Function`. for example: WeChat Mini Program [demo](https://github.com/bplok20010/eval5-wx-demo)\n-   Be interested or Be curious\n\n## ECMAScript version supported\n\nES5\n\n## Install\n\n```\nnpm install --save eval5\n```\n\n## Usage\n\n[![Edit eval5](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/eval5-zxndl?fontsize=14\u0026hidenavigation=1\u0026theme=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## Options\n\n```ts\ninterface Options {\n\t// default: 0 not limited\n\ttimeout?: number;\n\t// readonly\n\trootContext?: {} | null;\n\tglobalContextInFunction?: any;\n}\n```\n\nExample\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\ncurrent version\n\n**`global`**\n\ndefault: `{}`\n\nglobal context\n\n```js\nInterpreter.global = window;\nconst interpreter = new Interpreter();\ninterpreter.evaluate('alert(\"hello eval5\")');\n```\n\n**`globalContextInFunction`**\n\ndefault: `undefined`\n\n`eval5` does not support `use strict` mode, but the default value of `this` in function calls is `undefined`, you can set this property as the default.\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(ctx);\ninterpreter.evaluate(`\nthis; // ctx\nfunction func(){\n    return this; // window\n}\nfunc();\n`);\n```\n\nLet's take a look at the following example\n\n\u003e **Note: Illegal invocation**\n\u003e\n\u003e that's why `globalContextInFunction` is set to `undefined`\n\n```\nimport { Interpreter } from \"Interpreter\";\n\nInterpreter.globalContextInFunction = {};\n\nconst ctx = {alert: alert};\n\nconst interpreter = new Interpreter(ctx);\n\ninterpreter.evaluate(`\n// alert.call({}, 'Hello eval5')\n// Illegal invocation\nalert('Hello eval5');\n`);\n```\n\n**`constructor(context = Interpreter.global, options?: Options )`**\n\n---\n\n## Instance methods\n\n**`evaluate(code: string): any`**\n\nexecutes string code and returns the value of the last expression\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\nalias of `evaluate`\n\n**`getExecutionTime(): number`**\n\nget the last execution time\n\n**`setExecTimeout(timeout: number = 0): void`**\n\nset the timeout for each execution\n\n**`getOptions(): Readonly\u003cOptions\u003e`**\n\nget interpreter options\n\n## evaluate(code: string, ctx?: {}, options?: Options)\n\nexecutes string code and returns the value of the last expression\n\n\u003e note: a new interpreter is created with every execution\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\nuse `Interpreter.global` as the default context, `Interpreter.globalContextInFunction` also\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\nsee [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## Related\n\n-   [evaljs](https://github.com/marten-de-vries/evaljs)\n-   [closure-interpreter](https://github.com/int3/closure-interpreter)\n","funding_links":[],"categories":["javascript interpreters","TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbplok20010%2Feval5","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbplok20010%2Feval5","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbplok20010%2Feval5/lists"}