{"id":26674190,"url":"https://github.com/dorin131/inline-webassembly","last_synced_at":"2025-04-12T06:53:48.105Z","repository":{"id":36469249,"uuid":"226374113","full_name":"dorin131/inline-webassembly","owner":"dorin131","description":"Inline WebAssembly (text format) into JS","archived":false,"fork":false,"pushed_at":"2023-01-07T12:33:03.000Z","size":228,"stargazers_count":24,"open_issues_count":6,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-12T06:53:43.535Z","etag":null,"topics":["inline-webassembly","javascript","webassembly"],"latest_commit_sha":null,"homepage":null,"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/dorin131.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":"2019-12-06T17:02:56.000Z","updated_at":"2024-03-05T03:06:19.000Z","dependencies_parsed_at":"2023-01-17T01:46:45.535Z","dependency_job_id":null,"html_url":"https://github.com/dorin131/inline-webassembly","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dorin131%2Finline-webassembly","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dorin131%2Finline-webassembly/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dorin131%2Finline-webassembly/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dorin131%2Finline-webassembly/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dorin131","download_url":"https://codeload.github.com/dorin131/inline-webassembly/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248530591,"owners_count":21119595,"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":["inline-webassembly","javascript","webassembly"],"created_at":"2025-03-26T02:17:46.368Z","updated_at":"2025-04-12T06:53:48.083Z","avatar_url":"https://github.com/dorin131.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# inline-webassembly\nEasiest way to write WebAssembly\n\n[![npm version](https://badge.fury.io/js/inline-webassembly.svg)](https://badge.fury.io/js/inline-webassembly)\n\n\n## Installation\n\n```shell\n$ npm install --save inline-webassembly\n```\n\n## Usage\n\n```js\nconst iw = require('inline-webassembly');\n// or\nimport * as iw from 'inline-webassembly';\n```\n\n## Loading in browser\nAssuming that your JS file is named `main.js`\n```shell\n$ npm i -g browserify\n$ browserify main.js -o bundle.js\n```\nthen\n```html\n\u003cscript src=\"bundle.js\"\u003e\u003c/script\u003e\n```\n\n## Syntax highlight (VS Code)\nTo have WebAssembly syntax highlighting in VS Code, use extension: \u003ca href=\"https://marketplace.visualstudio.com/items?itemName=dorin131.vscode-inline-webassembly\u0026ssr=false#overview\"\u003eInline WebAssembly Syntax Highlight\u003c/a\u003e\n\n\u003cimg src=\"./images/code.png\" /\u003e\n\n## API\n\n### WasmModule\n\nThe constructor is going to return an instance of *WasmModule* which is an extension of the original WasmModule returned by the underlying \u003ca href=\"https://www.npmjs.com/package/wabt\"\u003ewabt\u003c/a\u003e package.\n\nAdditional helper methods:\n\n **readString**(index: `number`, length?: `number`): `string`\n Returns a string, provided an index, which should be a pointer in the module memory. \n\n **createString**(string: `string`, memoryLocation?: `number`): `number`\n Creates a string in the module memory and returns a pointer to it.\n\n## Getting started\n\n### Add two numbers\n\n```js\nconst iw = require('inline-webassembly');\n\niw(`\n  (module\n    (func (export \"add\") (param $n1 i32) (param $n2 i32) (result i32)\n      get_local $n1\n      get_local $n2\n      i32.add))`\n).then((wasmModule) =\u003e {\n  const sum = wasmModule.add(44, 99);\n  console.log(`Sum = ${sum}`); // 143\n});\n```\n\n## Other examples\n\n### Read a string from memory\n\n```js\nconst iw = require('inline-webassembly');\n\niw(`\n  (module\n    (memory (export \"memory\") 1)\n    (func (export \"hello\") (result i32)\n      i32.const 16\n    )\n    (data (i32.const 16)\n      \"Hello World\"\n    )\n  )`\n).then((wasmModule) =\u003e {\n  const stringPointer = wasmModule.hello(44, 99);\n  const string = wasmModule.readString(stringPointer)\n  console.log(`Result = ${string}`); // Hello World\n});\n```\n\n### Call a JS function from WebAssembly\n\n```js\nconst iw = require('inline-webassembly');\n\nconst sayHey = function() {\n  console.log('Hey!')\n}\n\niw(`\n  (module\n    (import \"env\" \"sayHey\" (func $sayHey))\n    (func (export \"hello\")\n      (call $sayHey)\n    )\n  )`, { env: { sayHey }}\n).then((wasmModule) =\u003e {\n  wasmModule.hello(); // Hey!\n});\n```\n\n### Reverse a string\n\n```js\nconst iw = require('inline-webassembly');\n\niw(`\n  (module\n    (memory $0 1)\n    (export \"memory\" (memory $0))\n    ;; declaring and exporting a function named \"reverse\"\n    ;; it takes two arguments, the pointer to a string and its length\n    ;; and it returns a 32 bit integer which is going to be the pointer\n    ;; to the reversed string\n    (func (export \"reverse\") (param $sref i32) (param $slen i32) (result i32)\n\n      ;; declaring new variable to store result pointer\n      (local $result i32)\n\n      ;; seclaring iterator variable\n      (local $iterator i32)\n\n      ;; write pointer\n      (local $write_to i32)\n\n      ;; setting $result = $sref + $slen + 1\n      (set_local $result\n        ;; adding 1\n        (i32.add\n          ;; adding the string pointer with its length\n          (i32.add\n            (get_local $sref)\n            (get_local $slen)\n          )\n          (i32.const 1)\n        )\n      )\n      \n      ;; setting iterator to 0, for the following loop\n      (set_local $iterator\n        (i32.const 0)  \n      )\n\n      ;; we'll start writing to the start of the result\n      (set_local $write_to\n        (get_local $result)  \n      )\n        \n      (block\n        (loop\n          \n          ;; store one character from original string to resulting string\n          (i32.store\n            (get_local $write_to)\n            ;; load 1 byte and sign-extend i8 to i32\n            (i32.load8_s\n              (i32.sub\n                (i32.sub\n                  (i32.add\n                    (get_local $sref)\n                    (get_local $slen)\n                  )\n                  (get_local $iterator)\n                )\n                (i32.const 1)\n              )\n            )  \n          )\n\n          ;; increment position to write to on next loop iteration\n          (set_local $write_to\n            (i32.add\n              (get_local $write_to)\n              (i32.const 1)  \n            )  \n          )\n\n          ;; increment iterator by 1 for every loop iteration\n          (set_local $iterator\n            (i32.add\n              (get_local $iterator)\n              (i32.const 1)  \n            )  \n          )\n          \n          ;; break loop if iterator reaches string length\n          (br_if 1\n            (i32.ge_s\n              (get_local $iterator)\n              (get_local $slen)\n            )\n          )\n          \n          ;; repeat loop\n          (br 0)\n        )\n      )\n\n      ;; returning result which contains pointer to the reversed string\n      (get_local $result)\n    )\n  )`\n).then((wasmModule) =\u003e {\n  const stringToReverse = wasmModule.createString('Dorin');\n  const resultPointer = wasmModule.reverse(stringToReverse, 5);\n  const resultString = wasmModule.readString(resultPointer);\n  console.log(`Result = ${resultString}`);\n});\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdorin131%2Finline-webassembly","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdorin131%2Finline-webassembly","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdorin131%2Finline-webassembly/lists"}