-
Notifications
You must be signed in to change notification settings - Fork 161
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
Is go-wasmer can handle C codes? #77
Comments
If your C code is compiled to WebAssembly, yes, it will work! |
Yea, we can compile C code to wasm. Is there any examples, go-wasmer handles C code? |
So far, a useful tool is https://emscripten.org/. More to come in few days :-). |
Using emscripten we can compile. but that can't be use in go-wasmer. I see go-wasmer is working on WASI. Waiting for WASI support |
Few days later We've released |
This looks good to me. But my feature request is to handle wasm bytecode(from C/C++ code) in go module. This wasienv is usable in only in wasmer if I'm not wrong |
wasienv helps to compile a C/C++ program to Wasm (with WASI support), and then you can use this Wasm module anywhere, incl. this project. |
Thanks will try with go module. |
Nothing I'm aware of. What do you want to know? |
i'm trying to run C code in go-wasmer. C code compiled in wasm using WASI
main.go
This is endup error
If I remove
How can I call C/C++ code in go-wasmer ? |
Considering #92 is merged (it still a draft, it's blocked by wasmerio/wasmer#1028 and wasmerio/wasmer#1030, they are all landing very soon). Let's consider the following C++ program: #include <stdio.h>
extern "C" {
int32_t sum(int32_t, int32_t) __attribute__((used));
int32_t sum(int32_t x, int32_t y) {
return x + y;
}
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("Hello, WASI!\n");
} else {
printf("Hello, %s!\n", argv[1]);
}
} You can compile it with: $ wasicc wasi_c.cpp -o wasi_c Then from Go: module, err := wasm.Compile(getBytes("wasi_c.wasm"))
assert.NoError(t, err)
wasiVersion := wasm.WasiGetVersion(module)
assert.Equal(t, wasiVersion, wasm.Snapshot0)
importObject := wasm.NewDefaultWasiImportObjectForVersion(wasiVersion)
instance, err := module.InstantiateWithImportObject(importObject)
assert.NoError(t, err)
defer instance.Close()
// `_starts` calls the `main` function. It prints `Hello, WASI!`.
start, exists := instance.Exports["_start"]
assert.Equal(t, true, exists)
_, err = start()
assert.NoError(t, err)
// `sum` is a regular exported function.
sum, exists := instance.Exports["sum"]
assert.Equal(t, true, exists)
output, err := sum(1, 2)
assert.NoError(t, err)
assert.Equal(t, wasm.I32(3), output) As soon as the PR mentionned above are merged, we have to wait for a new Wasmer release, and we will good to go! Hope it helps. |
92: feat(wasi) Support `WasiGetVersion` & other `*ForVersion` WASI API r=Hywan a=Hywan Fix #90. Fix #77.⚠️ Depends on wasmerio/wasmer#1028, wasmerio/wasmer#1029, and wasmerio/wasmer#1030. They must be merged before merging this PR! Marking this PR as a draft to be sure (the shared libraries must be updated too). This patch updates `bridge.go` and `wasmer.h` to support the new `wasmer_wasi_generate_import_object_for_version` and `wasmer_wasi_get_version` functions. After that, this patch updates `wasi.go` to implement the new `NewDefaultWasiImportObjectForVersion`, `NewWasiImportObjectForVersion` and the `WasiGetVersion` functions. In addition to that, we create a new `WasiVersion` type. Finally, this patch updates the tests to test this new API. And it works like a charm 👌. Co-authored-by: Ivan Enderlin <[email protected]>
92: feat(wasi) Support `WasiGetVersion` & other `*ForVersion` WASI API r=Hywan a=Hywan Fix #90. Fix #77.⚠️ Depends on wasmerio/wasmer#1028, wasmerio/wasmer#1029, and wasmerio/wasmer#1030. They must be merged before merging this PR! Marking this PR as a draft to be sure (the shared libraries must be updated too). This patch updates `bridge.go` and `wasmer.h` to support the new `wasmer_wasi_generate_import_object_for_version` and `wasmer_wasi_get_version` functions. After that, this patch updates `wasi.go` to implement the new `NewDefaultWasiImportObjectForVersion`, `NewWasiImportObjectForVersion` and the `WasiGetVersion` functions. In addition to that, we create a new `WasiVersion` type. Finally, this patch updates the tests to test this new API. And it works like a charm 👌. Co-authored-by: Ivan Enderlin <[email protected]>
Using go-wasmer we were able to handle Rust code by calling extern function.
can we do same for C codes?
is it compatible now?
If yes. How can we call C code as an extern function?
The text was updated successfully, but these errors were encountered: