{"id":13726974,"url":"https://github.com/aspkg/ecmassembly","last_synced_at":"2025-05-07T22:30:43.497Z","repository":{"id":52449830,"uuid":"346227321","full_name":"aspkg/ecmassembly","owner":"aspkg","description":"(Some) JavaScript APIs brought to AssemblyScript.","archived":false,"fork":false,"pushed_at":"2022-06-12T00:45:55.000Z","size":93,"stargazers_count":32,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-26T04:33:55.778Z","etag":null,"topics":["assemblyscript","ecmascript","javascript","typescript","wasm","webassembly","webdev","webdevelopment"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aspkg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-10T04:08:57.000Z","updated_at":"2025-01-21T17:20:06.000Z","dependencies_parsed_at":"2022-08-18T23:50:12.143Z","dependency_job_id":null,"html_url":"https://github.com/aspkg/ecmassembly","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aspkg%2Fecmassembly","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aspkg%2Fecmassembly/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aspkg%2Fecmassembly/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aspkg%2Fecmassembly/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aspkg","download_url":"https://codeload.github.com/aspkg/ecmassembly/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252965122,"owners_count":21832827,"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":["assemblyscript","ecmascript","javascript","typescript","wasm","webassembly","webdev","webdevelopment"],"created_at":"2024-08-03T01:03:33.940Z","updated_at":"2025-05-07T22:30:42.076Z","avatar_url":"https://github.com/aspkg.png","language":"TypeScript","readme":"# ECMAssembly\n\nSpec'd and/or common JavaScript APIs brought to AssemblyScript.\n\n\u003e The name is a play on words:\n\u003e `ECMAScript -\u003e AssemblyScript -\u003e ECMAssembly`\n\nNamely, this provides APIs that require task scheduling from the host\nenvironment's (JavaScript's) event loop; APIs such as `setTimeout`, `Promise`,\netc. AssemblyScript's stdlib currently only provides APIs that can be\nimplemented entirely on their own in Wasm without scheduling (Wasm does not yet\nprovide any APIs for scheduling) therefore without the need for bindings.\n\n\u003e **Note**\n\u003e For DOM APIs, see [`asdom`](https://github.com/lume/asdom).\n\n# Usage\n\nFirst:\n\n```sh\nnpm install ecmassembly @assemblyscript/loader\n```\n\n\u003e **Note** \u003e `@assemblyscript/loader` is needed, and your program will need to be manually\n\u003e loaded with the [loader API](https://github.com/AssemblyScript/assemblyscript/tree/main/lib/loader), and not with AssemblyScript's new auto-bindings\n\u003e in 0.20+, for now.\n\nOn the JavaScript side pass required glue code to the Wasm module via imports:\n\n```js\nimport {ECMAssembly} from 'ecmassembly/index.js'\nimport ASLoader from '@assemblyscript/loader'\n\nconst es = new ECMAssembly()\n\nconst imports = {\n\t...es.wasmImports,\n\t/*...All your own imports...*/\n}\n\nASLoader.instantiateStreaming(fetch('path/to/module.wasm'), imports).then(wasmModule =\u003e {\n\t// After the Wasm module is created, you need to pass the exports back to the lib:\n\tes.wasmExports = wasmModule.exports\n\n\t// Then finally, run anything from the module that depends on setTimeout, Promise, etc:\n\twasmModule.exports.runMyApp()\n})\n```\n\nFor example, see the `example/`'s [Wasm entrypoint](./example/index.js).\n\nIn your AssemblyScript project's `asconfig.json`, make sure the globals are included in `entries`, along with your own entry point:\n\n```json\n{\n\t\"entries\": [\n\t\t\"./node_modules/ecmassembly/assembly/PromiseActions\",\n\t\t\"./node_modules/ecmassembly/assembly/globals.ts\",\n\t\t\"./path/to/your-entry-point.ts\"\n\t]\n}\n```\n\nFor example, see the `example/`'s [asconfig.json](./example/asconfig.json).\n\nIn your code you can now use the available APIs similar to in regular\nJavaScript, for example here is what `Promise` currently looks like:\n\n```ts\nlet actions: PromiseActions\u003cboolean\u003e | null = null\n\nexport function runMyApp() {\n\tconst promise = new Promise\u003cboolean\u003e(_actions =\u003e {\n\t\t// Temporary hack while AS does not yet support closures (no closing\n\t\t// over variable except those that are at the top-level of the module).\n\t\tactions = _actions\n\n\t\t// resolve after 1 second\n\t\tsetTimeout(() =\u003e {\n\t\t\tactions!.resolve(true)\n\t\t}, 1000)\n\t})\n\n\tpromise.then(result =\u003e {\n\t\t// this runs one second later, and `result` will be `true` here\n\t})\n}\n```\n\n\u003e **Note**\n\u003e AssemblyScript does not support closures for non-top-level variables yet, so\n\u003e a `Promise` constructor's executor function receives an object with `resolve` and\n\u003e `reject` methods instead of two separate `resolve` and `reject` args, for the\n\u003e time being, but this will be updated in the future once closures are supported.\n\nHere's what `requestAnimationFrame` looks like:\n\n```ts\n// This is out here because there is no closure support for non-top-level variables yet.\nlet loop: (time: number) =\u003e void = (t: number) =\u003e {}\n\nexport function runMyApp() {\n\t// Make an infinite game loop:\n\n\tloop = (time: number) =\u003e {\n\t\t// ... render something based on the current elapsed time ...\n\n\t\trequestAnimationFrame(loop)\n\t}\n\n\trequestAnimationFrame(loop)\n}\n```\n\nFor example, see `example/`'s [`index-wasm.ts`](./example/assembly/index-wasm.ts) where it exercises all the APIs.\n\nFinally, make sure when you compile your AS code you pass `--exportTable --exportRuntime` to the `asc` CLI. For example:\n\n```sh\nasc --target release --exportTable --exportRuntime\n```\n\n# Portable mode\n\nIf you plan to make your code compile to Wasm using AssemblyScript (with `asc`) _and_ plain JS using TypeScript (f.e. with `tsc`), then you should make a JS entry point that imports your AS entry point, like so:\n\n```js\n// First import JS helpers\nimport 'assemblyscript/std/portable/index'\nimport type {} from 'ecmassembly/assembly/PromiseActions-js' // placeholder type for JS\n\n// Then import AS code so that it works in the JS target\nexport * from './path/to/your-entry-point.js'\n```\n\nThen you will need to write some conditional branching in order to handle the\ndifference between Wasm and JS runtimes when it comes to using `Promise`. The above `Promise` example would need to be updated like so:\n\n```ts\nlet actions: PromiseActions\u003cboolean\u003e | null = null\n\nexport function runMyApp() {\n\tconst promise = new Promise\u003cboolean\u003e((resolve, reject) =\u003e {\n\t\t// Temporary hack while AS does not yet support closures (no closing\n\t\t// over variable except those that are at the top-level of the module).\n\t\t// @ts-expect-error action object is for Wasm only\n\t\tactions = resolve\n\n\t\t// resolve after 1 second\n\t\tsetTimeout(() =\u003e {\n\t\t\tif (ASC_TARGET == 0) resolve(time)\n\t\t\telse actions!.resolve(true)\n\t\t}, 1000)\n\t})\n\n\tpromise.then(result =\u003e {\n\t\t// this runs one second later, and `result` will be `true` here\n\t})\n}\n```\n\nWhere `ASC_TARGET == 0` means we're in a JS environment, otherwise the code will use the actions object when compiled to Wasm (`ASC_TARGET != 0`).\n\n\u003e **Note**\n\u003e When closures for non-top-level variables arrive in AssemblyScript, this\n\u003e conditional checking will not be needed, and the `Promise` API will work exactly\n\u003e the same way in either environment.\n\n# APIs so far\n\n- [x] `requestAnimationFrame`/`cancelAnimationFrame`\n- [x] `setTimeout`/`clearTimeout`\n- [x] `setInterval`/`clearInterval`\n- [x] `Promise` (rudimentary initial implementation, still lacking things like proper promise chaining and static methods, etc)\n  - [ ] Complete the API, make it more to spec.\n  - [ ] Remove `PromiseActions` after closure support.\n- [ ] `queueMicrotask`\n","funding_links":[],"categories":["Packages","TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faspkg%2Fecmassembly","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faspkg%2Fecmassembly","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faspkg%2Fecmassembly/lists"}