Browse Source

Initialize BNG memory

rexim 5 years ago
parent
commit
655aec6625
5 changed files with 43 additions and 3 deletions
  1. 4 1
      2020-09-04/Makefile
  2. 11 2
      2020-09-04/README.md
  3. 9 0
      2020-09-04/bng.html
  4. 12 0
      2020-09-04/bng.js
  5. 7 0
      2020-09-04/bng.wat

+ 4 - 1
2020-09-04/Makefile

@@ -1,7 +1,7 @@
 BNGVIEWER_CFLAGS=$(shell pkg-config --cflags sdl2)
 BNGVIEWER_CFLAGS=$(shell pkg-config --cflags sdl2)
 BNGVIEWER_LIBS=$(shell pkg-config --libs sdl2)
 BNGVIEWER_LIBS=$(shell pkg-config --libs sdl2)
 
 
-all: prime prime.wasm png2bng tsodinw.bng bngviewer
+all: prime prime.wasm png2bng tsodinw.bng bngviewer bng.wasm
 
 
 prime: prime.c
 prime: prime.c
 	$(CC) -O3 -o prime prime.c
 	$(CC) -O3 -o prime prime.c
@@ -17,3 +17,6 @@ tsodinw.bng: png2bng tsodinw.png
 
 
 bngviewer: bngviewer.c bng.h
 bngviewer: bngviewer.c bng.h
 	$(CC) $(BNGVIEWER_CFLAGS) -O3 -o bngviewer bngviewer.c $(BNGVIEWER_LIBS)
 	$(CC) $(BNGVIEWER_CFLAGS) -O3 -o bngviewer bngviewer.c $(BNGVIEWER_LIBS)
+
+bng.wasm: bng.wat
+	wat2wasm bng.wat

+ 11 - 2
2020-09-04/README.md

@@ -34,8 +34,17 @@
     - [x] [Implement png2bng](./png2bng.c)
     - [x] [Implement png2bng](./png2bng.c)
     - [x] [Implement bngviewer](./bngviewer.c)
     - [x] [Implement bngviewer](./bngviewer.c)
     - [ ] Implement bng support in WASM
     - [ ] Implement bng support in WASM
-      - [ ] fetch("tsodinw.bng")
-      - [ ] Put the fetched file into WASM memory
+      - [x] fetch("tsodinw.bng"):
+        ```js
+          fetch("./tsodinw.bng").then((x) => console.log(x.arrayBuffer()))
+        ```
+      - [x] Put the fetched file into WASM memory
+        ```js
+          let bngFile = await fetch("tsodinw.bng");
+          let fileData = await bngFile.arrayBuffer();
+          let memory = new WebAssembly.Memory({initial: 10, maximum: 10});
+          new Uint8Array(memory.buffer).set(new Uint8Array(fileData));
+        ```
       - [ ] Call a WASM function that turns the file into Image Data
       - [ ] Call a WASM function that turns the file into Image Data
       - [ ] Take out the Image Data from WASM memory and display it
       - [ ] Take out the Image Data from WASM memory and display it
     - [ ] Add more interesting features to bng to test the support
     - [ ] Add more interesting features to bng to test the support

+ 9 - 0
2020-09-04/bng.html

@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <title>The First Website that support BNG natively Pog</title>
+  </head>
+  <body>
+    <script src='./bng.js'></script>
+  </body>
+</html>

+ 12 - 0
2020-09-04/bng.js

@@ -0,0 +1,12 @@
+async function bng() {
+    let bngFile = await fetch("tsodinw.bng");
+    let fileData = await bngFile.arrayBuffer();
+    let memory = new WebAssembly.Memory({initial: 10, maximum: 10});
+    new Uint8Array(memory.buffer).set(new Uint8Array(fileData));
+    let bngProgram = await WebAssembly.instantiateStreaming(
+        fetch("bng.wasm"),
+        { js: { mem: memory, print: arg => console.log(arg) } });
+    bngProgram.instance.exports.bng();
+}
+
+bng().catch((e) => console.log(e));

+ 7 - 0
2020-09-04/bng.wat

@@ -0,0 +1,7 @@
+(module
+ (memory (import "js" "mem") 1)
+ (func $print (import "js" "print") (param i32))
+ (func (export "bng")
+       (call $print (i32.load (i32.const 0)))
+       (call $print (i32.load (i32.const 4)))
+       (call $print (i32.load (i32.const 8)))))