Skip to content

Add sharedMemory option to allow threads with shared memory #247

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

Merged
merged 4 commits into from
May 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -27,13 +27,12 @@ jobs:
id: DEVELOPMENT-SNAPSHOT-2024-05-25-a-wasm32-unknown-wasi
download-url: "https://github.com/swiftwasm/swift/releases/download/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-05-25-a/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-05-25-a-wasm32-unknown-wasi.artifactbundle.zip"
wasi-backend: Node
# TODO: Enable this once we support threads in JavaScriptKit
# - os: ubuntu-22.04
# toolchain: DEVELOPMENT-SNAPSHOT-2024-05-01-a
# swift-sdk:
# id: DEVELOPMENT-SNAPSHOT-2024-05-25-a-wasm32-unknown-wasip1-threads
# download-url: "https://github.com/swiftwasm/swift/releases/download/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-05-25-a/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-05-25-a-wasm32-unknown-wasip1-threads.artifactbundle.zip"
# wasi-backend: Node
- os: ubuntu-22.04
toolchain: DEVELOPMENT-SNAPSHOT-2024-05-01-a
swift-sdk:
id: DEVELOPMENT-SNAPSHOT-2024-05-25-a-wasm32-unknown-wasip1-threads
download-url: "https://github.com/swiftwasm/swift/releases/download/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-05-25-a/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-05-25-a-wasm32-unknown-wasip1-threads.artifactbundle.zip"
wasi-backend: Node

runs-on: ${{ matrix.entry.os }}
env:
39 changes: 34 additions & 5 deletions IntegrationTests/lib.js
Original file line number Diff line number Diff line change
@@ -2,11 +2,12 @@ import { SwiftRuntime } from "javascript-kit-swift"
import { WASI as NodeWASI } from "wasi"
import { WASI as MicroWASI, useAll } from "uwasi"
import * as fs from "fs/promises"
import path from "path";

const WASI = {
MicroWASI: ({ programName }) => {
const wasi = new MicroWASI({
args: [programName],
args: [path.basename(programName)],
env: {},
features: [useAll()],
})
@@ -21,7 +22,7 @@ const WASI = {
},
Node: ({ programName }) => {
const wasi = new NodeWASI({
args: [programName],
args: [path.basename(programName)],
env: {},
preopens: {
"/": "./",
@@ -51,21 +52,49 @@ const selectWASIBackend = () => {
return WASI.Node;
};

function isUsingSharedMemory(module) {
const imports = WebAssembly.Module.imports(module);
for (const entry of imports) {
if (entry.module === "env" && entry.name === "memory" && entry.kind == "memory") {
return true;
}
}
return false;
}

export const startWasiTask = async (wasmPath, wasiConstructor = selectWASIBackend()) => {
const swift = new SwiftRuntime();
// Fetch our Wasm File
const wasmBinary = await fs.readFile(wasmPath);
const wasi = wasiConstructor({ programName: wasmPath });

// Instantiate the WebAssembly file
let { instance } = await WebAssembly.instantiate(wasmBinary, {
const module = await WebAssembly.compile(wasmBinary);

const importObject = {
wasi_snapshot_preview1: wasi.wasiImport,
javascript_kit: swift.importObjects(),
benchmark_helper: {
noop: () => {},
noop_with_int: (_) => {},
}
});
};

if (isUsingSharedMemory(module)) {
importObject["env"] = {
// We don't have JS API to get memory descriptor of imported memory
// at this moment, so we assume 256 pages (16MB) memory is enough
// large for initial memory size.
memory: new WebAssembly.Memory({ initial: 256, maximum: 16384, shared: true }),
};
importObject["wasi"] = {
"thread-spawn": () => {
throw new Error("thread-spawn not implemented");
}
}
}

// Instantiate the WebAssembly file
const instance = await WebAssembly.instantiate(module, importObject);

swift.setInstance(instance);
// Start the WebAssembly WASI instance!
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ test:
unittest:
@echo Running unit tests
swift build --build-tests -Xswiftc -Xclang-linker -Xswiftc -mexec-model=reactor -Xlinker --export-if-defined=main -Xlinker --export-if-defined=__main_argc_argv --static-swift-stdlib -Xswiftc -static-stdlib $(SWIFT_BUILD_FLAGS)
node --experimental-wasi-unstable-preview1 scripts/test-harness.mjs ./.build/wasm32-unknown-wasi/debug/JavaScriptKitPackageTests.wasm
node --experimental-wasi-unstable-preview1 scripts/test-harness.mjs ./.build/debug/JavaScriptKitPackageTests.wasm

.PHONY: benchmark_setup
benchmark_setup:
580 changes: 300 additions & 280 deletions Runtime/src/index.ts

Large diffs are not rendered by default.

196 changes: 105 additions & 91 deletions Sources/JavaScriptKit/Runtime/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

196 changes: 105 additions & 91 deletions Sources/JavaScriptKit/Runtime/index.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.