Skip to content

Commit 07ce9e4

Browse files
committed
Merge remote-tracking branch 'upstream/prealpha-dev' into nk/improve-workflow
2 parents f0c1fcf + 2fcf33d commit 07ce9e4

32 files changed

Lines changed: 1230 additions & 212 deletions

File tree

packages/cli/src/lib/defaults/build-images/wasm/rust/Dockerfile.mustache

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM rustlang/rust:nightly-slim as base
1+
FROM rust:1.60.0 as base
22

33
# Install the wasm32 rust build target
44
RUN rustup target add wasm32-unknown-unknown
@@ -7,7 +7,7 @@ WORKDIR /build-deps
77

88
# Install curl
99
RUN apt-get update
10-
RUN apt-get -y install curl
10+
RUN apt-get -y install curl clang llvm build-essential
1111

1212
# Install wasm-opt
1313
RUN curl -L https://github.com/WebAssembly/binaryen/releases/download/version_101/binaryen-version_101-x86_64-linux.tar.gz | tar -xz \
@@ -16,10 +16,13 @@ RUN curl -L https://github.com/WebAssembly/binaryen/releases/download/version_10
1616
&& rm -rf binary-version_101
1717

1818
# Install the toml-cli
19-
RUN cargo install toml-cli
19+
RUN cargo install -f toml-cli
2020

2121
# Install wasm-snip
22-
RUN cargo install wasm-snip
22+
RUN cargo install -f wasm-snip
23+
24+
# Install wasm-bindgen
25+
RUN cargo install -f wasm-bindgen-cli
2326

2427
{{#polywrap_linked_packages.length}}
2528
WORKDIR /linked-packages
@@ -55,38 +58,50 @@ RUN PACKAGE_NAME={{name}}; \
5558
{{/polywrap_module}}
5659
true
5760
{{/polywrap_linked_packages}}
61+
5862
{{/polywrap_linked_packages.length}}
5963

60-
# Remove any Cargo.lock files
6164
{{#polywrap_module}}
65+
# Remove any Cargo.lock files
6266
RUN rm -rf {{dir}}/Cargo.lock
6367

6468
# Ensure the Wasm module is configured to use imported memory
6569
ENV RUSTFLAGS="-C link-arg=-z -C link-arg=stack-size=65536 -C link-arg=--import-memory"
6670

67-
# Cleanup an artifact left by the toml CLI program ("[]" -> [])
68-
RUN sed -i 's/"\[\]"/\[\]/g' ./{{dir}}/Cargo.toml
69-
7071
# Ensure the module at {{dir}} has the crate-type = ["cdylib"]
71-
RUN toml set ./{{dir}}/Cargo.toml lib.crate-type ["cdylib","rlib"] > ./{{dir}}/Cargo-local.toml && \
72+
RUN toml set ./{{dir}}/Cargo.toml lib.crate-type ["cdylib"] > ./{{dir}}/Cargo-local.toml && \
7273
rm -rf ./{{dir}}/Cargo.toml && \
7374
mv ./{{dir}}/Cargo-local.toml ./{{dir}}/Cargo.toml && \
7475
true
7576

76-
# Clean up artifacts left by the toml CLI program ("["cdylib", "rlib"]" -> ["cdylib", "rlib"])
77-
RUN sed -i 's/"\[cdylib,rlib\]"/\["cdylib","rlib"\]/g' ./{{dir}}/Cargo.toml
77+
# Clean up artifacts left by the toml CLI program ("["cdylib"]" -> ["cdylib"])
78+
RUN sed -i 's/"\[cdylib\]"/\["cdylib"\]/g' ./{{dir}}/Cargo.toml
7879

79-
# Build the module at {{dir}}
80-
RUN cargo +nightly build --manifest-path ./{{dir}}/Cargo.toml \
81-
--target wasm32-unknown-unknown --release
80+
# Ensure the package name = "module"
81+
RUN toml set ./{{dir}}/Cargo.toml package.name "module" > ./{{dir}}/Cargo-local.toml && \
82+
rm -rf ./{{dir}}/Cargo.toml && \
83+
mv ./{{dir}}/Cargo-local.toml ./{{dir}}/Cargo.toml && \
84+
true
8285

8386
# Make the build directory
8487
RUN rm -rf ./build
8588
RUN mkdir ./build
8689

90+
# Build the module at {{dir}}
91+
RUN cargo build --manifest-path ./{{dir}}/Cargo.toml \
92+
--target wasm32-unknown-unknown --release
93+
94+
# Enable the "WASM_INTERFACE_TYPES" feature, which will remove the __wbindgen_throw import.
95+
# See: https://github.com/rustwasm/wasm-bindgen/blob/7f4663b70bd492278bf0e7bba4eeddb3d840c868/crates/cli-support/src/lib.rs#L397-L403
96+
ENV WASM_INTERFACE_TYPES=1
97+
98+
# Run wasm-bindgen over the module, replacing all placeholder __wbindgen_... imports
99+
RUN wasm-bindgen ./{{dir}}/target/wasm32-unknown-unknown/release/module.wasm --out-dir ./build --out-name bg_module.wasm
100+
101+
RUN wasm-snip ./build/bg_module.wasm -o ./build/snipped_module.wasm && \
102+
rm -rf ./build/bg_module.wasm
103+
87104
# Use wasm-opt to perform the "asyncify" post-processing step over all modules
88-
RUN WASM_MODULE=$(ls ./{{dir}}/target/wasm32-unknown-unknown/release/*.wasm); \
89-
wasm-snip $WASM_MODULE -o ./build/snipped_{{name}}.wasm && \
90-
wasm-opt --asyncify -Os ./build/snipped_{{name}}.wasm -o ./build/{{name}}.wasm && \
91-
rm -rf ./build/snipped_{{name}}.wasm
105+
RUN wasm-opt --asyncify -Os ./build/snipped_module.wasm -o ./build/module.wasm && \
106+
rm -rf ./build/snipped_module.wasm
92107
{{/polywrap_module}}

packages/js/asyncify/src/AsyncWasmInstance.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ export class AsyncWasmInstance {
4444

4545
private constructor() {}
4646

47-
public static createMemory(config: { module: ArrayBuffer }): WasmMemory {
48-
const bytecode = new Uint8Array(config.module);
49-
47+
public static createMemory(config: { module: Uint8Array }): WasmMemory {
5048
// extract the initial memory page size, as it will
5149
// throw an error if the imported page size differs:
5250
// https://chromium.googlesource.com/v8/v8/+/644556e6ed0e6e4fac2dfabb441439820ec59813/src/wasm/module-instantiate.cc#924
@@ -73,7 +71,7 @@ export class AsyncWasmInstance {
7371
// 0x__,
7472
]);
7573

76-
const sigIdx = indexOfArray(bytecode, envMemoryImportSignature);
74+
const sigIdx = indexOfArray(config.module, envMemoryImportSignature);
7775

7876
if (sigIdx < 0) {
7977
throw Error(
@@ -86,7 +84,7 @@ export class AsyncWasmInstance {
8684

8785
// Extract the initial memory page-range size
8886
const memoryInitalLimits =
89-
bytecode[sigIdx + envMemoryImportSignature.length + 1];
87+
config.module[sigIdx + envMemoryImportSignature.length + 1];
9088

9189
if (memoryInitalLimits === undefined) {
9290
throw Error(
@@ -98,7 +96,7 @@ export class AsyncWasmInstance {
9896
}
9997

10098
public static async createInstance(config: {
101-
module: ArrayBuffer;
99+
module: Uint8Array;
102100
imports: WasmImports;
103101
requiredExports?: readonly string[];
104102
}): Promise<AsyncWasmInstance> {

packages/js/client/src/PolywrapClient.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
InterfaceImplementations,
1919
InvokeOptions,
2020
InvokeResult,
21+
InvokerOptions,
2122
PluginRegistration,
2223
QueryOptions,
2324
QueryResult,
@@ -47,6 +48,8 @@ import {
4748
JobRunner,
4849
PluginPackage,
4950
RunOptions,
51+
msgpackEncode,
52+
msgpackDecode,
5053
} from "@polywrap/core-js";
5154
import { Tracer } from "@polywrap/tracing-js";
5255

@@ -197,7 +200,7 @@ export class PolywrapClient implements Client {
197200
public async getFile<TUri extends Uri | string>(
198201
uri: TUri,
199202
options: GetFileOptions
200-
): Promise<string | ArrayBuffer> {
203+
): Promise<string | Uint8Array> {
201204
const wrapper = await this._loadWrapper(this._toUri(uri), options);
202205
const client = contextualizeClient(this, options.contextId);
203206
return await wrapper.getFile(options, client);
@@ -309,14 +312,14 @@ export class PolywrapClient implements Client {
309312

310313
@Tracer.traceMethod("PolywrapClient: invoke")
311314
public async invoke<TData = unknown, TUri extends Uri | string = string>(
312-
options: InvokeOptions<TUri, PolywrapClientConfig>
315+
options: InvokerOptions<TUri, PolywrapClientConfig>
313316
): Promise<InvokeResult<TData>> {
314317
const { contextId, shouldClearContext } = this._setContext(
315318
options.contextId,
316319
options.config
317320
);
318321

319-
let result: InvokeResult<TData>;
322+
let error: Error | undefined;
320323

321324
try {
322325
const typedOptions: InvokeOptions<Uri> = {
@@ -327,18 +330,39 @@ export class PolywrapClient implements Client {
327330

328331
const wrapper = await this._loadWrapper(typedOptions.uri, { contextId });
329332

330-
result = (await wrapper.invoke(
333+
const invocableResult = await wrapper.invoke(
331334
typedOptions,
332335
contextualizeClient(this, contextId)
333-
)) as TData;
334-
} catch (error) {
335-
result = { error };
336+
);
337+
338+
if (invocableResult.data !== undefined) {
339+
if (options.encodeResult && !invocableResult.encoded) {
340+
return {
341+
// TODO: if options.encodeResult, fix return type to Uint8Array
342+
data: (msgpackEncode(invocableResult.data) as unknown) as TData,
343+
};
344+
} else if (invocableResult.encoded && !options.encodeResult) {
345+
return {
346+
// TODO: if result.encoded, fix return type to Uint8Array
347+
data: msgpackDecode(invocableResult.data as Uint8Array) as TData,
348+
};
349+
} else {
350+
return {
351+
data: invocableResult.data as TData,
352+
};
353+
}
354+
} else {
355+
error = invocableResult.error;
356+
}
357+
} catch (e) {
358+
error = e;
336359
}
337360

338361
if (shouldClearContext) {
339362
this._clearContext(contextId);
340363
}
341-
return result;
364+
365+
return { error };
342366
}
343367

344368
@Tracer.traceMethod("PolywrapClient: run")

packages/js/client/src/__tests__/core/interface-impls.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe("interface-impls", () => {
8383
{
8484
uri: implementation4Uri,
8585
plugin: {
86-
factory: () => ({} as PluginModule),
86+
factory: () => ({} as PluginModule<{}>),
8787
manifest: {
8888
schema: "",
8989
implements: [],
@@ -145,7 +145,7 @@ describe("interface-impls", () => {
145145
{
146146
uri: interface1Uri,
147147
plugin: {
148-
factory: () => ({} as PluginModule),
148+
factory: () => ({} as PluginModule<{}>),
149149
manifest: {
150150
schema: "",
151151
implements: [],
@@ -155,7 +155,7 @@ describe("interface-impls", () => {
155155
{
156156
uri: interface2Uri,
157157
plugin: {
158-
factory: () => ({} as PluginModule),
158+
factory: () => ({} as PluginModule<{}>),
159159
manifest: {
160160
schema: "",
161161
implements: [],
@@ -197,7 +197,7 @@ describe("interface-impls", () => {
197197
{
198198
uri: interfaceUri,
199199
plugin: {
200-
factory: () => ({} as PluginModule),
200+
factory: () => ({} as PluginModule<{}>),
201201
manifest: {
202202
schema: "",
203203
implements: [],
@@ -295,7 +295,7 @@ describe("interface-impls", () => {
295295
{
296296
uri: implementation1Uri,
297297
plugin: {
298-
factory: () => ({} as PluginModule),
298+
factory: () => ({} as PluginModule<{}>),
299299
manifest: {
300300
schema: "",
301301
implements: [new Uri(interfaceUri)],
@@ -330,7 +330,7 @@ describe("interface-impls", () => {
330330
{
331331
uri: implementation1Uri,
332332
plugin: {
333-
factory: () => ({} as PluginModule),
333+
factory: () => ({} as PluginModule<{}>),
334334
manifest: {
335335
schema: "",
336336
implements: [],

packages/js/client/src/__tests__/core/resolveUri.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ describe("resolveUri", () => {
190190
uri: pluginUri.uri,
191191
plugin: {
192192
factory: () => {
193-
return ({} as unknown) as PluginModule;
193+
return ({} as unknown) as PluginModule<{}>;
194194
},
195195
manifest: {
196196
schema: "",

packages/js/client/src/__tests__/core/wasm-wrapper.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ describe("wasm-wrapper", () => {
113113
networkNameOrChainId: "testnet",
114114
},
115115
},
116-
noDecode: true,
116+
encodeResult: true,
117117
});
118118

119119
expect(result.error).toBeFalsy();
120120
expect(result.data).toBeTruthy();
121-
expect(result.data instanceof ArrayBuffer).toBeTruthy();
122-
expect(msgpackDecode(result.data as ArrayBuffer)).toContain("0x");
121+
expect(result.data instanceof Uint8Array).toBeTruthy();
122+
expect(msgpackDecode(result.data as Uint8Array)).toContain("0x");
123123
});
124124

125125
it("should invoke wrapper with custom redirects", async () => {
@@ -291,9 +291,9 @@ describe("wasm-wrapper", () => {
291291
): Int!
292292
`);
293293

294-
const fileBuffer: ArrayBuffer = (await client.getFile(wrapperUri, {
294+
const fileBuffer: Uint8Array = (await client.getFile(wrapperUri, {
295295
path: manifest.schema!,
296-
})) as ArrayBuffer;
296+
})) as Uint8Array;
297297
const decoder = new TextDecoder("utf8");
298298
const text = decoder.decode(fileBuffer);
299299
expect(text).toContain(`getData(

0 commit comments

Comments
 (0)