{"id":42707919,"url":"https://github.com/taowen/define-function","last_synced_at":"2026-01-29T14:50:05.755Z","repository":{"id":47353947,"uuid":"465536602","full_name":"taowen/define-function","owner":"taowen","description":"quick.js based eval","archived":false,"fork":false,"pushed_at":"2023-04-24T14:53:48.000Z","size":112,"stargazers_count":43,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-09-23T23:11:38.775Z","etag":null,"topics":["eval","javascript","plugins","quickjs","wasm"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/taowen.png","metadata":{"files":{"readme":"README.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}},"created_at":"2022-03-03T02:14:43.000Z","updated_at":"2025-04-15T19:27:40.000Z","dependencies_parsed_at":"2022-08-21T13:10:50.961Z","dependency_job_id":null,"html_url":"https://github.com/taowen/define-function","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"purl":"pkg:github/taowen/define-function","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taowen%2Fdefine-function","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taowen%2Fdefine-function/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taowen%2Fdefine-function/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taowen%2Fdefine-function/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/taowen","download_url":"https://codeload.github.com/taowen/define-function/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taowen%2Fdefine-function/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28880007,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-29T10:31:27.438Z","status":"ssl_error","status_checked_at":"2026-01-29T10:31:01.017Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["eval","javascript","plugins","quickjs","wasm"],"created_at":"2026-01-29T14:50:04.441Z","updated_at":"2026-01-29T14:50:05.749Z","avatar_url":"https://github.com/taowen.png","language":"JavaScript","readme":"# About\n\n[quick.js](https://bellard.org/quickjs/) based sandbox\n\n```\nnpm install define-function\n```\n\nworks in any WebAssembly environment\n\n* node\n* browser\n* wechat miniprogram\n\n# Usage\n\ndefine a function dynamically with javascript source code\n\n```js\nconst def = require('define-function')\nconst f = await def(`\n    return 'hello';\n`)\nf() // 'hello'\n```\n\nfunction can have argument\n\n```js\nconst def = require('define-function')\nconst f = await def(`\n    const [hello, world] = arguments;\n    return hello + ' ' + world;\n`)\nf('hello', 'world') // 'hello world'\n```\n\nargument can be function\n\n```js\nconst def = require('define-function')\nconst f = await def(`\n    const [print] = arguments;\n    print('hello')\n`)\nf((msg) =\u003e {\n    console.log(msg)\n}) // 'hello'\n```\n\nargument can be async function\n\n```js\nconst def = require('define-function')\nconst f = await def(`\n    const [print, sleep] = arguments;\n    (async() =\u003e {\n        print('hello')\n        await sleep(1000);\n        print('world')\n    })();\n`)\nf(\n    msg =\u003e console.log(msg),\n    milliseconds =\u003e new Promise(resolve =\u003e setTimeout(resolve, milliseconds))\n) \n// hello\n// world\n```\n\ncan return promise back to host\n\n```js\nconst def = require('define-function')\nconst f = await def(`\n    const [print, sleep] = arguments;\n    return (async() =\u003e {\n        print('hello')\n        await sleep(1000);\n        print('world')\n    })();\n`)\nawait f(\n    msg =\u003e console.log(msg),\n    milliseconds =\u003e new Promise(resolve =\u003e setTimeout(resolve, milliseconds))\n)\nconsole.log('done')\n// hello\n// world\n// done\n```\n\nshare context between multiple invocations\n\n```js\nconst { context } = require('define-function')\nconst ctx = await context()\nconst f = await ctx.def(`\n    global.counter = (global.counter || 0)+1;\n    return counter; // counter can be referenced globally\n`)\nf() // 1\nf() // 2\nf() // 3\nctx.dispose()\n```\n\ninject value and callbacks into global\n\n```js\nconst { context } = require('define-function')\nconst ctx = await context({ global: { \n    console,\n    anwerOfEverything() {\n        return 42;\n    }\n} }) // inject console and anwerOfEverything to global\nconst f = await ctx.def(`\n    console.log(anwerOfEverything());\n`)\nf() // 42\nctx.dispose();\n```\n\nimport and export es module\n\n```js\nconst { context } = require('define-function')\nconst ctx = await context({ \n    loadModuleContent(moduleName) {\n        if (moduleName !== 'xxx') {\n            throw new Error('expect xxx');\n        }\n        return `export function sayHello() { return 'hello' }`\n    }\n});\nconst { hello } = await ctx.load(`\n    import { sayHello } from 'xxx';\n    export const hello = sayHello();\n`)\nconsole.log(hello) // hello\nctx.dispose();\n```\n\n# Limit\n\n* function argument does not support Set/Map/Class or anything that can not survive JSON.parse(JSON.stringify), except the argument is a function\n* function return value does not support Set/Map/Class or anything that can not survive JSON.parse(JSON.stringify), except promise object\n* JSON.stringify and JSON.parse takes time, so the arguments and return value should be as small as possible for best performance\n\n# Similar projects\n\n* https://github.com/justjake/quickjs-emscripten/\n* https://github.com/maple3142/wasm-jseval\n\ndefine-function has a simpler API and support async/await\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaowen%2Fdefine-function","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftaowen%2Fdefine-function","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaowen%2Fdefine-function/lists"}