-
-
Notifications
You must be signed in to change notification settings - Fork 12.5k
/
wasmtime.rb
171 lines (149 loc) · 6.34 KB
/
wasmtime.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
class Wasmtime < Formula
desc "Standalone JIT-style runtime for WebAssembly, using Cranelift"
homepage "https://wasmtime.dev/"
url "https://github.com/bytecodealliance/wasmtime.git",
tag: "v14.0.1",
revision: "402d3da91f7b7f1bdc25a1b9a90c81fc95cbe367"
license "Apache-2.0" => { with: "LLVM-exception" }
head "https://github.com/bytecodealliance/wasmtime.git", branch: "main"
livecheck do
url :stable
strategy :github_latest
end
bottle do
sha256 cellar: :any, arm64_sonoma: "31775e765e8da9ed80451cf242a1990145a527e71de7ea4df35d198daefbfb59"
sha256 cellar: :any, arm64_ventura: "e21b23c486dd82153ab544526839f40fce66e7b2f914bf8ddce92b010b6ebc9b"
sha256 cellar: :any, arm64_monterey: "e8fc57510f8898bad048cb9a73677aa7bb36066325b97f85b8da304ff331192f"
sha256 cellar: :any, sonoma: "e16162a60cc82b19d3de32510a3c1486e8a1a48610c61b3ef0fd581e90204b7d"
sha256 cellar: :any, ventura: "289e78efea2fa88dea5c9920a227e6c09f9dbc58ed9fc3aff57c96141bada01e"
sha256 cellar: :any, monterey: "158daf5898f8aeda842c6f1030f3a4b26d3adeccbd150ebfff8416bd06a0d664"
sha256 cellar: :any_skip_relocation, x86_64_linux: "aef2b3fa6e7e591681b610e8b716a3623b780420a00277aa7dacb27450614096"
end
depends_on "rust" => :build
def install
system "cargo", "install", *std_cargo_args
system "cargo", "build", "--locked", "--lib", "--manifest-path", "crates/c-api/Cargo.toml", "--release"
cp "crates/c-api/wasm-c-api/include/wasm.h", "crates/c-api/include/"
lib.install shared_library("target/release/libwasmtime")
include.install "crates/c-api/wasm-c-api/include/wasm.h"
include.install Dir["crates/c-api/include/*"]
end
test do
wasm = ["0061736d0100000001070160027f7f017f030201000707010373756d00000a09010700200020016a0b"].pack("H*")
(testpath/"sum.wasm").write(wasm)
assert_equal "3\n",
shell_output("#{bin}/wasmtime --invoke sum #{testpath/"sum.wasm"} 1 2")
(testpath/"hello.wat").write <<~EOS
(module
(func $hello (import "" "hello"))
(func (export "run") (call $hello))
)
EOS
# Example from https://docs.wasmtime.dev/examples-c-hello-world.html to test C library API,
# with comments removed for brevity
(testpath/"hello.c").write <<~EOS
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <wasm.h>
#include <wasmtime.h>
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
static wasm_trap_t* hello_callback(
void *env,
wasmtime_caller_t *caller,
const wasmtime_val_t *args,
size_t nargs,
wasmtime_val_t *results,
size_t nresults
) {
printf("Calling back...\\n");
printf("> Hello World!\\n");
return NULL;
}
int main() {
int ret = 0;
printf("Initializing...\\n");
wasm_engine_t *engine = wasm_engine_new();
assert(engine != NULL);
wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
assert(store != NULL);
wasmtime_context_t *context = wasmtime_store_context(store);
FILE* file = fopen("./hello.wat", "r");
assert(file != NULL);
fseek(file, 0L, SEEK_END);
size_t file_size = ftell(file);
fseek(file, 0L, SEEK_SET);
wasm_byte_vec_t wat;
wasm_byte_vec_new_uninitialized(&wat, file_size);
assert(fread(wat.data, file_size, 1, file) == 1);
fclose(file);
wasm_byte_vec_t wasm;
wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
if (error != NULL)
exit_with_error("failed to parse wat", error, NULL);
wasm_byte_vec_delete(&wat);
printf("Compiling module...\\n");
wasmtime_module_t *module = NULL;
error = wasmtime_module_new(engine, (uint8_t*) wasm.data, wasm.size, &module);
wasm_byte_vec_delete(&wasm);
if (error != NULL)
exit_with_error("failed to compile module", error, NULL);
printf("Creating callback...\\n");
wasm_functype_t *hello_ty = wasm_functype_new_0_0();
wasmtime_func_t hello;
wasmtime_func_new(context, hello_ty, hello_callback, NULL, NULL, &hello);
printf("Instantiating module...\\n");
wasm_trap_t *trap = NULL;
wasmtime_instance_t instance;
wasmtime_extern_t import;
import.kind = WASMTIME_EXTERN_FUNC;
import.of.func = hello;
error = wasmtime_instance_new(context, module, &import, 1, &instance, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to instantiate", error, trap);
printf("Extracting export...\\n");
wasmtime_extern_t run;
bool ok = wasmtime_instance_export_get(context, &instance, "run", 3, &run);
assert(ok);
assert(run.kind == WASMTIME_EXTERN_FUNC);
printf("Calling export...\\n");
error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call function", error, trap);
printf("All finished!\\n");
ret = 0;
wasmtime_module_delete(module);
wasmtime_store_delete(store);
wasm_engine_delete(engine);
return ret;
}
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
fprintf(stderr, "error: %s\\n", message);
wasm_byte_vec_t error_message;
if (error != NULL) {
wasmtime_error_message(error, &error_message);
wasmtime_error_delete(error);
} else {
wasm_trap_message(trap, &error_message);
wasm_trap_delete(trap);
}
fprintf(stderr, "%.*s\\n", (int) error_message.size, error_message.data);
wasm_byte_vec_delete(&error_message);
exit(1);
}
EOS
system ENV.cc, "hello.c", "-I#{include}", "-L#{lib}", "-lwasmtime", "-o", "hello"
expected = <<~EOS
Initializing...
Compiling module...
Creating callback...
Instantiating module...
Extracting export...
Calling export...
Calling back...
> Hello World!
All finished!
EOS
assert_equal expected, shell_output("./hello")
end
end