added hello-world wasm example and npmPackage nix derivation

This commit is contained in:
Sebastian Moser
2024-05-23 00:24:09 +02:00
parent a16c2f9221
commit 7944fd3f7f
4 changed files with 69 additions and 6 deletions

View File

@@ -21,23 +21,56 @@
let let
pkgs = nixpkgs.legacyPackages.${system}; pkgs = nixpkgs.legacyPackages.${system};
wasmToolchain = fenix.packages.${system}.combine [ wasmToolchain = fenix.packages.${system}.combine [
fenix.packages.${system}.targets.wasm32-unknown-unknown.stable.toolchain fenix.packages.${system}.targets.wasm32-unknown-unknown.latest.toolchain
fenix.packages.${system}.complete.toolchain fenix.packages.${system}.latest.toolchain
]; ];
osToolchain = fenix.packages.${system}.complete.toolchain; osToolchain = fenix.packages.${system}.latest.toolchain;
wasmCrane = crane.lib.${system}.overrideToolchain wasmToolchain; wasmCrane = crane.lib.${system}.overrideToolchain wasmToolchain;
osCrane = crane.lib.${system}.overrideToolchain osToolchain; osCrane = crane.lib.${system}.overrideToolchain osToolchain;
wasmArtifacts = wasmCrane.buildDepsOnly ({
src = self;
doCheck = false; # tests does not work in wasm
CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
});
in { in {
############################## PACKAGES ############################## ############################## PACKAGES ##############################
packages.default = osCrane.buildPackage { packages.default = osCrane.buildPackage {
src = "${self}"; src = "${self}";
cargoExtraArgs = "--bin mize --features os-binary";
}; };
packages = { packages = rec {
wasm = wasmCrane.buildPackage { wasm = wasmCrane.buildPackage {
src = "${self}"; src = "${self}";
CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
doCheck = false;
}; };
# Thanks to:
# - https://github.com/dl-solarity/hardhat-diamond-tools/blob/e5b8bc9624fbf89eda83a8ce4f6b6672ea83f550/flake.nix#L103
# - https://github.com/ipetkov/crane/issues/362#issuecomment-1683220603
npmPackage = wasmCrane.mkCargoDerivation {
name = "mize-npm-package";
# i can't use CARGO_BUILD_TARGET to set the target
# because then the cargo install run by wasm-pack tries to build the wasm-bindgen-cli for a wasm target...
cargoExtraArgs = "--target wasm32-unknown-unknown --features wasm-target";
src = self;
cargoArtifacts = wasmArtifacts;
doCheck = false;
buildPhaseCargoCommand = ''
mkdir -p $out/pkg
HOME=$(mktemp -d fake-homeXXXX) wasm-pack build --out-dir $out/pkg --scope=c2vi -- --features wasm-target
'';
buildInputs = with pkgs; [ wasm-bindgen-cli binaryen wasm-pack wasmToolchain ];
};
}; };
############################## DEV SHELLS ############################## ############################## DEV SHELLS ##############################
@@ -58,6 +91,10 @@ in {
]; ];
}; };
}); }) // {
############################## SOME GLOBAL OUTPUTS ##############################
inherit inputs;
};
} }

View File

@@ -3,6 +3,8 @@
static PROTO_VERSION: u8 = 1; static PROTO_VERSION: u8 = 1;
// the core part houses the code that can run on any platform
/*
pub mod core { pub mod core {
pub mod memstore; pub mod memstore;
pub mod instance; pub mod instance;
@@ -14,7 +16,12 @@ pub mod core {
} }
pub use core::*; pub use core::*;
// */
pub mod platform {
#[cfg(feature = "wasm-target")]
pub mod wasm;
}

19
src/platform/wasm/mod.rs Normal file
View File

@@ -0,0 +1,19 @@
mod utils;
use wasm_bindgen::prelude::*;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet() {
alert("Hello, wasm-game-of-life!");
}

View File