This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # Build the shared library. | |
$ just build | |
$ # Install the extension in the Postgres tree. | |
$ just install | |
$ # Activate the extension. | |
$ echo 'CREATE EXTENSION wasm;' | \ | |
psql -h $host -d $database |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bytes, _ := wasm.ReadBytes("memory.wasm") | |
instance, _ := wasm.NewInstance(bytes) | |
defer instance.Close() | |
// Calls the `return_hello` exported function. | |
// This function returns a pointer to a string. | |
result, _ := instance.Exports["return_hello"]() | |
// Gets the pointer value as an integer. | |
pointer := result.ToI32() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[no_mangle] | |
pub extern fn return_hello() -> *const u8 { | |
b"Hello, World!\0".as_ptr() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// // 1️⃣ Declare the `sum` function signature (see cgo). | |
// | |
// #include <stdlib.h> | |
// | |
// extern int32_t sum(void *context, int32_t x, int32_t y); | |
import "C" | |
import ( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern { | |
fn sum(x: i32, y: i32) -> i32; | |
} | |
#[no_mangle] | |
pub extern fn add1(x: i32, y: i32) -> i32 { | |
unsafe { sum(x, y) } + 1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
wasm "github.com/wasmerio/go-ext-wasm/wasmer" | |
) | |
func main() { | |
// Reads the WebAssembly module as bytes. | |
bytes, _ := wasm.ReadBytes("simple.wasm") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[no_mangle] | |
pub extern fn sum(x: i32, y: i32) -> i32 { | |
x + y | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[no_mangle] | |
pub extern fn sum(x: i32, y: i32) -> i32 { | |
x + y | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$instance = new Wasm\Instance('simple.wasm'); | |
$result = $instance->sum(1, 2); | |
var_dump($result); // int(3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters