Skip to content

Instantly share code, notes, and snippets.

View Hywan's full-sized avatar
🦀
Oxidising every bit

Ivan Enderlin Hywan

🦀
Oxidising every bit
View GitHub Profile
@Hywan
Hywan / simple.rs
Last active April 3, 2019 07:46
Wasm examples, Rust part
#[no_mangle]
pub extern fn sum(x: i32, y: i32) -> i32 {
x + y
}
@Hywan
Hywan / simple.rs
Created May 28, 2019 13:52
Go Wasm example, simple.rs
#[no_mangle]
pub extern fn sum(x: i32, y: i32) -> i32 {
x + y
}
@Hywan
Hywan / simple.go
Last active May 28, 2019 13:57
Go Wasm example, simple.go
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")
@Hywan
Hywan / import.rs
Last active May 28, 2019 14:03
Go Wasm example, import.rs
extern {
fn sum(x: i32, y: i32) -> i32;
}
#[no_mangle]
pub extern fn add1(x: i32, y: i32) -> i32 {
unsafe { sum(x, y) } + 1
}
@Hywan
Hywan / import.go
Last active May 28, 2019 14:13
Go Wasm example, import.go
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 (
@Hywan
Hywan / memory.rs
Created May 28, 2019 14:39
Go Wasm example, memory.rs
#[no_mangle]
pub extern fn return_hello() -> *const u8 {
b"Hello, World!\0".as_ptr()
}
@Hywan
Hywan / memory.go
Last active May 29, 2019 10:07
Go Wasm example, memory.go
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()
@Hywan
Hywan / install.sh
Created August 28, 2019 14:15
How to install postgres-ext-wasm
$ # 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
@Hywan
Hywan / simple.rs
Created August 28, 2019 14:27
Simple Rust program that compiles to WebAssembly
#[no_mangle]
pub extern fn sum(x: i32, y: i32) -> i32 {
x + y
}
@Hywan
Hywan / simple.sql
Created August 28, 2019 14:29
Postgres WebAssembly usage
-- New instance of the `simple.wasm` WebAssembly module.
SELECT wasm_new_instance('/absolute/path/to/simple.wasm', 'ns');
-- Call a WebAssembly exported function!
SELECT ns_sum(1, 2);
-- ns_sum
-- --------
-- 3
-- (1 row)