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 / sunrise.js
Created January 7, 2022 11:40
rust-sunrise implemented in Javascript
const SECONDS_IN_A_DAY = 86400.;
const UNIX_EPOCH_JULIAN_DAY = 2440587.5;
function unix_to_julian(timestamp) {
return timestamp / SECONDS_IN_A_DAY + UNIX_EPOCH_JULIAN_DAY;
}
function julian_to_unix(day) {
return ((day - UNIX_EPOCH_JULIAN_DAY) * SECONDS_IN_A_DAY);
}
@Hywan
Hywan / MemoryExample.java
Created May 12, 2020 09:40
Reading WebAssembly memory from 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 09:36
A Rust program that compiles to WebAssembly
#[no_mangle]
pub extern fn return_hello() -> *const u8 {
b"Hello, World!\0".as_ptr()
}
@Hywan
Hywan / SimpleExample.java
Created May 12, 2020 09:31
WebAssembly exporte function as regular Java function
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 09:23
Execute a WebAssembly module from 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 09:17
Simple Rust program that will compile to WebAssembly to be executed in Java
#[no_mangle]
pub extern fn sum(x: i32, y: i32) -> i32 {
x + y
}
@Hywan
Hywan / lib.rs
Last active October 11, 2019 13:17
Safely and Dynamically distribute arguments on function
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 August 28, 2019 14:36
Introspect a WebAssembly module with Postgres
-- 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 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)
@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
}