Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Experimental WASM Extensions #849

Merged
merged 56 commits into from
Dec 21, 2023
Merged

feat: Experimental WASM Extensions #849

merged 56 commits into from
Dec 21, 2023

Conversation

jwerle
Copy link
Member

@jwerle jwerle commented Dec 14, 2023

This PR introduces support for WASM C/C++ extensions that support a subset of libc and the Socket Runtime Native Extension API.

Targeting wasm32 extensions is experimental as the ABI and APIs may be incomplete or subject to change

WASM extensions can be configured like:

[meta]
name = "my-wasm-extension"
type = "extension"

[extension]
target = "wasm32"
sources[] = extension.cc

Implemented like:

#include <socket/extension.h>
#include <string.h>

static void onEcho (
  sapi_context_t* context,
  sapi_ipc_message_t* message,
  const sapi_ipc_router_t* router
) {
  auto result = sapi_ipc_result_create(context, message);
  auto value = sapi_ipc_mesage_get(message, "value");
  auto data = sapi_json_string_create(context, value);
  sapi_ipc_result_set_json_data(result, sapi_json_any(data));
  sapi_ipc_reply(result);
}

static bool initialize (sapi_context_t* context, const void* data) {
  sapi_ipc_router_map(context, "my-wasm-extension.echo", onEcho, NULL);
  return true;
}

SOCKET_RUNTIME_REGISTER_EXTENSION("my-wasm-extension", initialize);

Consumed like this:

import extension from 'socket:extension'

const wasm = await extension.load('my-wasm-extension')
const result = await wasm.binding.echo({ value: 'hello' })
console.log(result.data) // 'hello'

@jwerle jwerle added javascript An issue, discussion, or task related to the runtime javascript api An issue, task, or discussion related to public runtime APIs extension An issue, task, or discussion related to native extensions runtime An issue, task, or discussion related to the runtime core wasm An issue, task, or discussion related to WebAssembly (WASM) labels Dec 14, 2023
@jwerle jwerle self-assigned this Dec 14, 2023
@jwerle jwerle marked this pull request as ready for review December 19, 2023 15:43
Copy link
Member

@chicoxyzzy chicoxyzzy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

macOS CI is failing with

error: unable to create target: 'No available targets are compatible with triple "wasm32"'

@jwerle jwerle changed the title feat: WASM Extensions feat: Experimental WASM Extensions Dec 21, 2023
@jwerle jwerle added the experimental An issue, discussion, or pull request related to experimental features label Dec 21, 2023
@jwerle jwerle merged commit cae9100 into master Dec 21, 2023
6 of 10 checks passed
@jwerle jwerle deleted the wasm-extensions branch December 21, 2023 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api An issue, task, or discussion related to public runtime APIs experimental An issue, discussion, or pull request related to experimental features extension An issue, task, or discussion related to native extensions javascript An issue, discussion, or task related to the runtime javascript runtime An issue, task, or discussion related to the runtime core wasm An issue, task, or discussion related to WebAssembly (WASM)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants