Skip to content

Commit

Permalink
build asm inside nix shell, heck copyright sign
Browse files Browse the repository at this point in the history
  • Loading branch information
onsails committed Jul 18, 2021
1 parent 69ca747 commit 396c8fa
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/target
**/*.rs.bk
Cargo.lock

/test-wasm/build
/test-wasm/node_modules
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ impl WasmerEnv for Env {
}

fn main() -> Result<(), Box<dyn Error>> {
let wasm_bytes = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/get-string.wasm"));
let wasm_bytes = include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/test-wasm/build/optimized.wasm"
));
let store = Store::default();
let module = Module::new(&store, wasm_bytes)?;

Expand Down Expand Up @@ -56,7 +59,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let memory = instance.exports.get_memory("memory").expect("get memory");
let string = str_ptr.read(memory)?;

assert_eq!(string, "TheString");
assert_eq!(string, "TheString ©");

Ok(())
}
Expand Down
6 changes: 6 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
buildInputs = with pkgs; [
rust-bin.stable.latest.default
libiconv
nodejs-14_x
];

shellHook = ''
cd test-wasm
npm run asbuild
'';
};

}
Expand Down
Binary file removed get-string.wasm
Binary file not shown.
20 changes: 20 additions & 0 deletions test-wasm/asconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"targets": {
"debug": {
"binaryFile": "build/untouched.wasm",
"textFile": "build/untouched.wat",
"sourceMap": true,
"debug": true
},
"release": {
"binaryFile": "build/optimized.wasm",
"textFile": "build/optimized.wat",
"sourceMap": true,
"optimizeLevel": 3,
"shrinkLevel": 0,
"converge": false,
"noAssert": false
}
},
"options": {}
}
2 changes: 1 addition & 1 deletion get-string.ts → test-wasm/assembly/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function getString(): string {
return "TheString";
return "TheString ©";
}
6 changes: 6 additions & 0 deletions test-wasm/assembly/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "assemblyscript/std/assembly.json",
"include": [
"./**/*.ts"
]
}
5 changes: 5 additions & 0 deletions test-wasm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const fs = require("fs");
const loader = require("@assemblyscript/loader");
const imports = { /* imports go here */ };
const wasmModule = loader.instantiateSync(fs.readFileSync(__dirname + "/build/optimized.wasm"), imports);
module.exports = wasmModule.exports;
35 changes: 35 additions & 0 deletions test-wasm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions test-wasm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "test-wasm",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "node tests",
"asbuild:untouched": "asc assembly/index.ts --target debug",
"asbuild:optimized": "asc assembly/index.ts --target release",
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized"
},
"author": "",
"license": "ISC",
"dependencies": {
"@assemblyscript/loader": "^0.19.7"
},
"devDependencies": {
"assemblyscript": "^0.19.7"
}
}
4 changes: 4 additions & 0 deletions test-wasm/tests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const assert = require("assert");
const myModule = require("..");
assert.strictEqual(myModule.add(1, 2), 3);
console.log("ok");
7 changes: 5 additions & 2 deletions tests/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ impl WasmerEnv for Env {

#[test]
fn read_strings() -> Result<(), Box<dyn Error>> {
let wasm_bytes = include_bytes!("../get-string.wasm");
let wasm_bytes = include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/test-wasm/build/optimized.wasm"
));
let store = Store::default();
let module = Module::new(&store, wasm_bytes)?;

Expand All @@ -46,7 +49,7 @@ fn read_strings() -> Result<(), Box<dyn Error>> {
let str_ptr = get_string.call()?;
let string = str_ptr.read(memory)?;

assert_eq!(string, "TheString");
assert_eq!(string, "TheString ©");

Ok(())
}
Expand Down

0 comments on commit 396c8fa

Please sign in to comment.