Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Ivan Enderlin Hywan

🏠
Working from home
View GitHub Profile
@Hywan
Hywan / MemoryExample.java
Created May 12, 2020
Reading WebAssembly memory from Java
View MemoryExample.java
import org.wasmer.Instance;
import org.wasmer.Memory;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Paths;
class MemoryExample {
public static void main(String[] args) throws IOException {
@Hywan
Hywan / memory.rs
Created May 12, 2020
A Rust program that compiles to WebAssembly
View memory.rs
#[no_mangle]
pub extern fn return_hello() -> *const u8 {
b"Hello, World!\0".as_ptr()
}
@Hywan
Hywan / SimpleExample.java
Created May 12, 2020
WebAssembly exporte function as regular Java function
View SimpleExample.java
import org.wasmer.Instance;
import org.wasmer.exports.Function;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
class SimpleExample {
public static void main(String[] args) throws IOException {
// Read the WebAssembly bytes.
@Hywan
Hywan / SimpleExample.java
Last active May 12, 2020
Execute a WebAssembly module from Java
View SimpleExample.java
import org.wasmer.Instance;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
class SimpleExample {
public static void main(String[] args) throws IOException {
// Read the WebAssembly bytes.
byte[] bytes = Files.readAllBytes(Paths.get("simple.wasm"));
@Hywan
Hywan / simple.rs
Created May 12, 2020
Simple Rust program that will compile to WebAssembly to be executed in Java
View simple.rs
#[no_mangle]
pub extern fn sum(x: i32, y: i32) -> i32 {
x + y
}
@Hywan
Hywan / lib.rs
Last active Oct 11, 2019
Safely and Dynamically distribute arguments on function
View lib.rs
use std::{convert::TryFrom, fmt::Debug};
pub trait WitValue
where
Self: Sized + Debug + Copy,
{
}
impl WitValue for i32 {}
impl WitValue for f32 {}
@Hywan
Hywan / inspect.sql
Created Aug 28, 2019
Introspect a WebAssembly module with Postgres
View inspect.sql
-- Select all WebAssembly instances.
SELECT * FROM wasm.instances;
-- id | wasm_file
-- --------------------------------------+-------------------------------
-- 426e17af-c32f-5027-ad73-239e5450dd91 | /absolute/path/to/simple.wasm
-- (1 row)
-- Select all exported functions for a specific instance.
SELECT
@Hywan
Hywan / simple.sql
Created Aug 28, 2019
Postgres WebAssembly usage
View simple.sql
-- 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)
@Hywan
Hywan / simple.rs
Created Aug 28, 2019
Simple Rust program that compiles to WebAssembly
View simple.rs
#[no_mangle]
pub extern fn sum(x: i32, y: i32) -> i32 {
x + y
}
@Hywan
Hywan / install.sh
Created Aug 28, 2019
How to install postgres-ext-wasm
View install.sh
$ # 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