Skip to content

Commit b21c288

Browse files
committed
Added a package script to wrap generated wasm in js code.
1 parent a7d6852 commit b21c288

File tree

5 files changed

+87
-12
lines changed

5 files changed

+87
-12
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
target
2-
pkg
2+
pkg
3+
.venv
4+
5+
bot_code.js

bot_template.js.j2

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var wasm_data = "{{wasm_encoded}}";
2+
3+
{{generated_js}}
4+
5+
function asciiToBinary(str) {
6+
if (typeof atob === "function") {
7+
// this works in the browser
8+
return atob(str);
9+
} else {
10+
// this works in node
11+
return new Buffer(str, "base64").toString("binary");
12+
}
13+
}
14+
15+
function decode(encoded) {
16+
var binaryString = asciiToBinary(encoded);
17+
var bytes = new Uint8Array(binaryString.length);
18+
for (var i = 0; i < binaryString.length; i++) {
19+
bytes[i] = binaryString.charCodeAt(i);
20+
}
21+
return bytes.buffer;
22+
}
23+
24+
let wasm_module;
25+
__wbg_init(decode(wasm_data)).then(result => wasm_module = result);

build_and_package.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import base64
2+
import os
3+
import sys
4+
import shutil
5+
import subprocess
6+
from pathlib import Path
7+
8+
from jinja2 import Template
9+
10+
if len(sys.argv) < 2:
11+
print("USAGE: python build_and_package.py [project_folder]")
12+
sys.exit(1)
13+
14+
project = Path(sys.argv[1])
15+
os.chdir(project)
16+
17+
target_folder = Path("./pkg/")
18+
19+
# first do a little cleanup
20+
shutil.rmtree(target_folder)
21+
22+
# Build the js (has exports, tho, so we'll do some sketchy substitution)
23+
subprocess.run(["wasm-pack", "build", "--release", "--target", "web"])
24+
25+
potential_wasm_files = list(target_folder.glob("*_bg.wasm"))
26+
assert len(potential_wasm_files) == 1
27+
28+
wasm_file = potential_wasm_files[0]
29+
with open(wasm_file, "rb") as f:
30+
wasm_data = f.read()
31+
32+
wasm_encoded = base64.b64encode(wasm_data).decode("utf-8")
33+
34+
potential_js_files = list(target_folder.glob("*.js"))
35+
assert len(potential_js_files) == 1
36+
37+
js_file = potential_js_files[0]
38+
with open(js_file) as f:
39+
js_lines = f.read().splitlines()
40+
41+
for i in range(len(js_lines)):
42+
line = js_lines[i]
43+
if line.strip().startswith("export function"):
44+
js_lines[i] = line.replace("export ", "")
45+
elif line.strip().startswith("export"):
46+
js_lines[i] = "//delete this"
47+
js_lines = filter(lambda s: s != "//delete this", js_lines)
48+
49+
os.chdir("..")
50+
with open("bot_template.js.j2") as f:
51+
template = Template(f.read())
52+
53+
rendered = template.render(wasm_encoded=wasm_encoded, generated_js="\n".join(js_lines))
54+
with open("bot_code.js", "w") as f:
55+
f.write(rendered)

gaussian-bots/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ opt-level = 'z'
1515
[dependencies]
1616
js-sys = "0.3.67"
1717
wasm-bindgen = "0.2.90"
18+
19+
[package.metadata.wasm-pack.profile.release.wasm-bindgen]
20+
omit-default-module-path = true

gaussian-bots/file_base64.py

-11
This file was deleted.

0 commit comments

Comments
 (0)