{"id":13494708,"url":"https://github.com/WebAssembly/wasm-c-api","last_synced_at":"2025-03-28T14:31:38.386Z","repository":{"id":33594500,"uuid":"135301410","full_name":"WebAssembly/wasm-c-api","owner":"WebAssembly","description":"Wasm C API prototype","archived":false,"fork":false,"pushed_at":"2025-01-17T09:01:32.000Z","size":578,"stargazers_count":565,"open_issues_count":59,"forks_count":80,"subscribers_count":60,"default_branch":"main","last_synced_at":"2025-03-24T02:13:08.778Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WebAssembly.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-05-29T13:36:29.000Z","updated_at":"2025-03-16T04:57:59.000Z","dependencies_parsed_at":"2025-01-26T00:02:38.848Z","dependency_job_id":"fc738acb-471e-480c-a8df-235719cdfef9","html_url":"https://github.com/WebAssembly/wasm-c-api","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/WebAssembly%2Fwasm-c-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebAssembly%2Fwasm-c-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebAssembly%2Fwasm-c-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebAssembly%2Fwasm-c-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WebAssembly","download_url":"https://codeload.github.com/WebAssembly/wasm-c-api/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246046040,"owners_count":20714891,"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":[],"created_at":"2024-07-31T19:01:27.422Z","updated_at":"2025-03-28T14:31:37.923Z","avatar_url":"https://github.com/WebAssembly.png","language":"C++","readme":"# WebAssembly C and C++ API \n\nWork in progress! No docs yet.\n\n\n### Design Goals\n\n* Provide a \"black box\" API for embedding a Wasm engine in other C/C++ applications.\n\n  * Be completely agnostic to VM specifics.\n\n  * Non-goal: \"white box\" interoperability with embedder (such as combined GC instead of mere finalisation) -- *much* more difficult to achieve.\n\n* Allow creation of bindings for other languages through typical C foreign function interfaces.\n\n  * Support a plain C API.\n\n  * Stick to mostly manual memory management of interface objects.\n\n* Avoid language features that raise barrier to use.\n\n  * E.g., no exceptions or post-C++11 features in C++ API.\n\n  * E.g., no passing of structs by-value or post-C99 features in C API.\n\n* Achieve link-time compatibility between different implementations.\n\n  * All implementation-dependent API classes are abstract and can be instantiated through factory methods only.\n\n\n### Interfaces\n\n* C++ API:\n\n  * See `include/wasm.hh` for interface.\n\n  * See `example/*.cc` for example usages.\n\n* C API:\n\n  * See `include/wasm.h` for interface.\n\n  * See `example/*.c` for example usages.\n\nSome random explanations:\n\n* The VM must be initialised by creating an instance of an *engine* (`wasm::Engine`/`wasm_engine_t`) and is shut down by deleting it. Such an instance may only be created once per process.\n\n* All runtime objects are tied to a specific *store* (`wasm::Store`/`wasm_store_t`). Multiple stores can be created, but their objects cannot interact. Every store and its objects must only be accessed in a single thread.\n\n* To exchange module objects between threads, create a *shared* module (`wasm::Shared\u003cModule\u003e`/`wasm_shared_module_t`). Other objects cannot be shared in current Wasm.\n\n* *Vector* structures (`wasm::vec\u003cX\u003e`/`wasm_x_vec_t`) are lightweight abstractions of a pair of a plain array and its length. The C++ API does not use `std::vector` because that does not support adopting pre-existing arrays.\n\n* *References* point to runtime objects, but may involve internal indirections, which may or may not be cached. Thus, pointer equality on `Ref*` or subclasses cannot be used to compare identity of the underlying objects (`Ref::eq` may be added later). However, `nullptr`/`NULL` uniquely represents null references.\n\n* The API already encompasses current proposals like [multiple return values](https://github.com/WebAssembly/multi-value/blob/master/proposals/multi-value/Overview.md) and [reference types](https://github.com/WebAssembly/reference-types/blob/master/proposals/reference-types/Overview.md), but not yet [threads](https://github.com/WebAssembly/threads/blob/master/proposals/threads/Overview.md).\n\n\n### Prototype Implementation\n\n* This repo contains a prototype implementation based on V8 is in `src`.\n\n  * Note that this requires adding a module to V8, so it patches V8's build file.\n\n* The C API is implemented on top of the C++ API.\n\n* See `Makefile` for build recipe. Canonical steps to run examples:\n\n  1. `make v8-checkout`\n  2. `make v8`\n  3. `make all`\n\n\n#### Limitations\n\nV8 implementation:\n\n* Currently requires patching V8 by adding a module.\n\n* Host functions (`Func::make`) create a JavaScript function internally, since V8 cannot handle raw C imports yet.\n\n* As a consequence, does not support multiple results in external calls or host functions.\n\n* Host functions and host globals are created through auxiliary modules constructed on the fly, to work around limitations in JS API.\n\n* `Shared\u003cModule\u003e` is currently implemented via serialisation, since V8 does not currently have direct support for cross-isolate sharing.\n\n\n### Other Implementations\n\nCurrently, known implementations of this API are included in\n\n* V8 natively (both C and C++)\n* Wabt (only C?)\n* Wasmtime (only C?)\n* [Wasmer](https://github.com/wasmerio/wasmer/tree/master/lib/c-api) (only C, C++ coming soon)\n\n\n### TODO\n\nPossible API tweaks:\n\n  * Add `Ref::eq` (or better, a subclass `EqRef::eq`) for reference equality?\n\n  * Add a way to return error messages from `Module::make` and `Module::validate`.\n\n  * Use `restrict` in C API?\n\n  * Find a way to perform C callbacks through C++ without extra wrapper?\n\n  * Add iterators to `vec` class?\n","funding_links":[],"categories":["C++","\u003ca name=\"wamr\"\u003e\u003c/a\u003e[WAMR](https://github.com/bytecodealliance/wasm-micro-runtime) \u003csup\u003e[top⇈](#contents)\u003c/sup\u003e"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FWebAssembly%2Fwasm-c-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FWebAssembly%2Fwasm-c-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FWebAssembly%2Fwasm-c-api/lists"}