-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwasm_to_java.py
More file actions
79 lines (63 loc) · 2.67 KB
/
Copy pathwasm_to_java.py
File metadata and controls
79 lines (63 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import base64
import gzip
from typing import Generator
chars_per_line = 80 - len(' "" +')
def split_every_n(s: str, n: int) -> list[str]:
return [s[i:i+n] for i in range(0, len(s), n)]
with open("../safe-url-paths-rust/target/wasm32-unknown-unknown/release/safe_url_paths_rust.wasm", "rb") as file:
wasm_bytes = file.read()
wasm_bytes_gzipped = gzip.compress(wasm_bytes, 9)
wasm_bytes_gzipped_base64 = base64.b64encode(wasm_bytes_gzipped).decode("utf-8")
wasm_bytes_gzipped_base64_lines = split_every_n(wasm_bytes_gzipped_base64, chars_per_line)
print("WASM size ", len(wasm_bytes))
print("gzipped WASM size: ", len(wasm_bytes_gzipped))
print("Base64'd gzipped WASM size:", len(wasm_bytes_gzipped_base64))
chars_per_line = 80 - len(' "" +')
encoded_lines = wasm_bytes_gzipped_base64_lines
# - Put together the output
out_lines = [
"package io.github.fearnoeval.minimalchicory;",
"",
"import java.io.ByteArrayInputStream;",
"import java.io.ByteArrayOutputStream;",
"import java.io.IOException;",
"import java.util.Base64;",
"import java.util.zip.GZIPInputStream;",
"",
"class GeneratedWasmBlob {",
" private static final String WASM_BYTES_GZIPPED_BASE64 =",
]
# - Add all of the encoded lines
for encoded_line in encoded_lines:
out_lines.append(f' "{encoded_line}" +')
# - Finish up, adding:
# - A finish to the string concatenation
# - Cleaner output vs. cleaner code? In this case, former wins, IMO
# - A closing curly brace for the class declaration and a newlines since it's
# the end of the file
out_lines = out_lines + [
' "";',
"",
" public static final byte[] WASM_BYTES = generateBytes();",
"",
" private static final byte[] generateBytes() {",
" final var wasmBytesGzipped = Base64.getDecoder().decode(WASM_BYTES_GZIPPED_BASE64);",
" final var bais = new ByteArrayInputStream(wasmBytesGzipped);",
" final var wasmBytesBaos = new ByteArrayOutputStream();",
"",
" try (var wasmBytesGzippedInputStream = new GZIPInputStream(bais)) {",
" final var buffer = new byte[4096];",
" int len;",
" while ((len = wasmBytesGzippedInputStream.read(buffer)) != -1) {",
" wasmBytesBaos.write(buffer, 0, len);",
" }",
" } catch (final IOException e) {",
" throw new RuntimeException(e);",
" }",
"",
" return wasmBytesBaos.toByteArray();",
" }",
"}\n",
]
with open("src/main/java/io/github/fearnoeval/minimalchicory/GeneratedWasmBlob.java", "w") as out_file:
out_file.write("\n".join(out_lines))