This is a minimalistic web assembly tool for creating modules from arrays of bytes. This helps you develop much more closer to how web assembly apps exist at a low level.
If you check this project out and just want to get the dependencies to setup:
make setup
Go into any example directory:
terminal
make
make serve
Then open a browser to http://localhost:9999
Check the console for output!
If you are using this as a npm module
npm install wasmly
If all you are doing is writing a single exported "main" function that takes in inputs, returns an output, and exposes "memory". Try this to save boiler plate:
```javascript var fs = require('fs'); let {makeSimple,int,I32,I32_CONST,END} = require("wasmly");
// main() -> i32
app = makeSimple([],[I32],[
vec([]), // no local variables
I32_CONST, int(42), // return 42
END
])
fs.writeFileSync('out.wasm',Buffer.from(app)) ```
If you are trying to create the most simplest module from scratch. This has no functions
```javascript var fs = require('fs'); let {flatten,MAGICNUMBER,VERSION1} = require("wasmly");
app = [ MAGICNUMBER, VERSION1, ]
// this is just a nested array of bytes: // [[[0, 97, 115, 109]][[1, 0, 0, 0]]]
fs.writeFileSync('out.wasm',Buffer.from(flatten(app))) ``` Not very useful!
main()
From Scratch```javascript ... // main() -> i32 { return 42 } mainfunctionsignature = [FUNC,vec([]),vec([I32])] // function signature returns 42 mainfunctioncode = bytevec([ vec([]), // no local variables [I32_CONST, int(42)], // return 42 END ])
//lets make memory at least 2 pages and at most 10 pages long memory = [LIMITMINMAX,uint(2),uint(10)]
// put it all together as a module app = [ MAGICNUMBER, VERSION1, [SECTIONTYPE,bytevec(vec([mainfunctionsignature]))], [SECTIONFUNCTION,bytevec(vec([int(0)]))], [SECTIONMEMORY,bytevec(vec([memory]))], [SECTIONEXPORT,bytevec(vec([ [str("main"),DESCFUNCTION,0], [str("memory"),DESCMEMORY,0] ]))], [SECTIONCODE,bytevec(vec([mainfunction_code]))] ] ... ```
Let's make a very simple memory allocator.
```javascript ... // malloc(length:i32) -> i32 { ... } mallocfunctionsignature = [FUNC,vec([I32]),vec([I32])] // function signature returns 42 mallocfunctioncode = bytevec([ vec([ [1, I32] // currentheap:i32 ]), // currentheap = global.heap GLOBALGET, 0, LOCALSET, 1, // memorycurrentheap = length GLOBALGET, 0, LOCALGET, 0, I32STORE, 0, 0, // global.heap = currentheap + 1 + length LOCALGET, 1, I32CONST, 1, I32ADD, LOCALGET, 0, I32ADD, GLOBALSET, 0, // return currentheap + 1 LOCALGET, 1, I32CONST, 5, I32_ADD, END ])
// create a heap global set to zero heapglobal = [I32,MUTABLE,I32CONST, int(0),END]
//lets make memory at least 2 pages and at most 10 pages long memory = [LIMITMINMAX,uint(2),uint(10)]
// put it all together as a module app = [ MAGICNUMBER, VERSION1, [SECTIONTYPE,bytevec(vec([mallocfunctionsignature]))], [SECTIONFUNCTION,bytevec(vec([0]))], [SECTIONMEMORY,bytevec(vec([memory]))], [SECTIONGLOBAL,bytevec(vec([heapglobal]))], [SECTIONEXPORT,bytevec(vec([ [str("malloc"),DESCFUNCTION,0], [str("memory"),DESCMEMORY,0], [str("heap"),DESCGLOBAL,0] ]))], [SECTIONCODE,bytevec(vec([mallocfunctioncode]))] ] ... ```