{"id":13423628,"url":"https://github.com/dirkschumacher/wasmr","last_synced_at":"2025-03-15T17:32:03.649Z","repository":{"id":46321749,"uuid":"202800439","full_name":"dirkschumacher/wasmr","owner":"dirkschumacher","description":"Execute WebAssembly from R using wasmer","archived":true,"fork":false,"pushed_at":"2020-03-11T20:37:06.000Z","size":91,"stargazers_count":75,"open_issues_count":2,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-10T15:11:31.572Z","etag":null,"topics":["prototype","r","wasmer","webassembly"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dirkschumacher.png","metadata":{"files":{"readme":"README.Rmd","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-08-16T21:19:04.000Z","updated_at":"2024-06-22T16:37:27.000Z","dependencies_parsed_at":"2022-09-01T23:20:32.858Z","dependency_job_id":null,"html_url":"https://github.com/dirkschumacher/wasmr","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirkschumacher%2Fwasmr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirkschumacher%2Fwasmr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirkschumacher%2Fwasmr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirkschumacher%2Fwasmr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dirkschumacher","download_url":"https://codeload.github.com/dirkschumacher/wasmr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243767072,"owners_count":20344867,"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":["prototype","r","wasmer","webassembly"],"created_at":"2024-07-31T00:00:39.216Z","updated_at":"2025-03-15T17:32:03.167Z","avatar_url":"https://github.com/dirkschumacher.png","language":"C++","funding_links":[],"categories":["C++"],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n\n# WebAssembly in R using wasmer\n\n\u003c!-- badges: start --\u003e\n[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)\n[![Travis build status](https://travis-ci.org/dirkschumacher/wasmr.svg?branch=master)](https://travis-ci.org/dirkschumacher/wasmr)\n\u003c!-- badges: end --\u003e\n\nThe goal of `wasmr` is to run [WebAssembly](https://developer.mozilla.org/en-US/docs/WebAssembly/Concepts) code from R using the [wasmer](https://wasmer.io/) runtime.\n\nThis is a early version with probably bugs, missing features and not the best code quality. But it already works and I am happy to hear feedback. The goal is that this package evolves into something stable.\n\nThe package is mainly written in C++ using the C API of the `wasmer` runtime which is written in `rust`. You therefore need a rust compiler to install the package.\n\n## Installation\n\n``` r\nremotes::install_github(\"dirkschumacher/wasmr\")\n```\n\nAlso make sure to have a very recent rust compiler. The best way to install the rust toolchain is to use [rustup](https://rustup.rs/).\n\n## Example\n\n```{r example}\nlibrary(wasmr)\n```\n\n```{r}\nf \u003c- system.file(\"examples/sum.wasm\", package = \"wasmr\")\ninstance \u003c- instantiate(f)\ninstance$exports$sum(10, 20)\n```\n\n```{r}\nf \u003c- system.file(\"examples/hello.wasm\", package = \"wasmr\")\ninstance \u003c- instantiate(f)\nmemory_pointer \u003c- instance$exports$hello()\nhi \u003c- instance$memory$get_memory_view(memory_pointer)\nrawToChar(hi$get(1:11))\n```\n\n```{r}\nf \u003c- system.file(\"examples/fib.wasm\", package = \"wasmr\")\ninstance \u003c- instantiate(f)\ninstance$exports$fib(20)\n\nfib \u003c- function(n) {\n  if (n \u003c 2) return(n)\n  fib(n - 1) + fib(n - 2)\n}\n\nmicrobenchmark::microbenchmark(\n  instance$exports$fib(20),\n  fib(20)\n)\n```\n\n## Memory\n\n```{r}\nf \u003c- system.file(\"examples/fib.wasm\", package = \"wasmr\")\ninstance \u003c- instantiate(f)\ninstance$memory$get_memory_length()\n\n# grow the number of pages\ninstance$memory$grow(1)\ninstance$memory$get_memory_length()\n```\n\n```{r}\nf \u003c- system.file(\"examples/hello.wasm\", package = \"wasmr\")\ninstance \u003c- instantiate(f)\nmemory_pointer \u003c- instance$exports$hello()\nmemory \u003c- instance$memory$get_memory_view(memory_pointer)\nmemory$get(1:11)\nrawToChar(memory$get(1:11))\n\n# you can also write to the internal memory\nmemory$set(5:6, charToRaw(\"o_\"))\nrawToChar(memory$get(1:11))\n```\n\n\n### Imports\n\n```{r}\nset.seed(42)\nimports \u003c- list(\n  env = list(\n    add = typed_function(\n      function(a, b) {\n        # use R's RNG to add a random number to the result\n        a + b * as.integer(runif(1) * 100)\n      },\n      param_types = c(\"I32\", \"I32\"),\n      return_type = \"I32\"\n    )\n  )\n)\nf \u003c- system.file(\"examples/sum_import.wasm\", package = \"wasmr\")\ninstance \u003c- instantiate(f, imports)\n# sum: add(a, b) + 42\ninstance$exports$sum(1, 5)\n```\n\n## Caveats and limitations\n\n* It is not feature complete and does not support everything that `wasm` supports\n* Imported functions can only have integer parameters\n* No globals\n* There is hardly any documentation except for the examples\n* `I32/I64` are mapped to `IntegerVector` and `F32/F64` to `NumericVector`. Currently no way to differentiate.\n* WIP\n\n## Inspiration and References\n\n* [wasmer Python](https://github.com/wasmerio/python-ext-wasm) - haven't tried it but looked at the API/examples\n* [wasmer](https://github.com/wasmerio/wasmer) - especially the C api and the tests give some good examples.\n* @jeroen 's [rust template](https://github.com/r-rust/hellorust)\n\n## Contribute\n\nWhile this is work in progress, the best way to contribute is to test the package and write/comment on issues. Before sending a PR it would great to post an issue first to discuss.\n\n## License\n\nMIT just like wasmer.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdirkschumacher%2Fwasmr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdirkschumacher%2Fwasmr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdirkschumacher%2Fwasmr/lists"}