Skip to content

Commit f64ea0e

Browse files
committed
fix #11: add text conversion polyfills for Edge
1 parent 182bea6 commit f64ea0e

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ Usage: customasm [options] <asm-file-1> ... <asm-file-N>
2828
Options:
2929
-f, --format FORMAT The format of the output file. Possible formats:
3030
binary, binstr, hexstr, bindump, hexdump,
31-
mif, intelhex, deccomma, hexcomma, decc, hexc,
32-
logisim8, logisim16
33-
31+
mif, intelhex, deccomma, hexcomma, decc, hexc,
32+
logisim8, logisim16
33+
3434
-o, --output FILE The name of the output file.
3535
-p, --print Print output to stdout instead of writing to a file.
3636
-q, --quiet Suppress progress reports.

web/customasm.gc.wasm

21.5 KB
Binary file not shown.

web/main.js

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function assemble()
121121

122122
let divText = document.getElementById("divOutputText")
123123
divText.innerHTML = output
124-
divText.style.whiteSpace = (isError || format <= 1) ? "pre" : "no-wrap"
124+
divText.style.whiteSpace = "no-wrap"
125125
}
126126

127127

@@ -130,7 +130,7 @@ function makeRustString(str)
130130
//console.log("makeRustString")
131131
//console.log(str)
132132

133-
let bytes = new TextEncoder("utf-8").encode(str)
133+
let bytes = window.TextEncoder ? new TextEncoder("utf-8").encode(str) : stringToUtf8ByteArray(str)
134134
//console.log(bytes)
135135

136136
let ptr = g_wasm.instance.exports.wasm_string_new(bytes.length)
@@ -157,7 +157,7 @@ function readRustString(ptr)
157157

158158
//console.log(bytes)
159159

160-
let str = new TextDecoder("utf-8").decode(new Uint8Array(bytes))
160+
let str = window.TextDecoder ? new TextDecoder("utf-8").decode(new Uint8Array(bytes)) : utf8ByteArrayToString(bytes)
161161
//console.log(str)
162162
return str
163163
}
@@ -169,4 +169,64 @@ function dropRustString(ptr)
169169
//console.log(ptr)
170170

171171
g_wasm.instance.exports.wasm_string_drop(ptr)
172+
}
173+
174+
175+
// From https://github.com/google/closure-library/blob/e877b1eac410c0d842bcda118689759512e0e26f/closure/goog/crypt/crypt.js#L115
176+
function stringToUtf8ByteArray(str)
177+
{
178+
let out = [], p = 0
179+
for (let i = 0; i < str.length; i++) {
180+
let c = str.charCodeAt(i)
181+
if (c < 128) {
182+
out[p++] = c
183+
} else if (c < 2048) {
184+
out[p++] = (c >> 6) | 192
185+
out[p++] = (c & 63) | 128
186+
} else if (
187+
((c & 0xFC00) == 0xD800) && (i + 1) < str.length &&
188+
((str.charCodeAt(i + 1) & 0xFC00) == 0xDC00)) {
189+
// Surrogate Pair
190+
c = 0x10000 + ((c & 0x03FF) << 10) + (str.charCodeAt(++i) & 0x03FF)
191+
out[p++] = (c >> 18) | 240
192+
out[p++] = ((c >> 12) & 63) | 128
193+
out[p++] = ((c >> 6) & 63) | 128
194+
out[p++] = (c & 63) | 128
195+
} else {
196+
out[p++] = (c >> 12) | 224
197+
out[p++] = ((c >> 6) & 63) | 128
198+
out[p++] = (c & 63) | 128
199+
}
200+
}
201+
return out
202+
}
203+
204+
205+
// From https://github.com/google/closure-library/blob/e877b1eac410c0d842bcda118689759512e0e26f/closure/goog/crypt/crypt.js#L149
206+
function utf8ByteArrayToString(bytes)
207+
{
208+
let out = [], pos = 0, c = 0
209+
while (pos < bytes.length) {
210+
let c1 = bytes[pos++]
211+
if (c1 < 128) {
212+
out[c++] = String.fromCharCode(c1)
213+
} else if (c1 > 191 && c1 < 224) {
214+
let c2 = bytes[pos++]
215+
out[c++] = String.fromCharCode((c1 & 31) << 6 | c2 & 63)
216+
} else if (c1 > 239 && c1 < 365) {
217+
// Surrogate Pair
218+
let c2 = bytes[pos++]
219+
let c3 = bytes[pos++]
220+
let c4 = bytes[pos++]
221+
let u = ((c1 & 7) << 18 | (c2 & 63) << 12 | (c3 & 63) << 6 | c4 & 63) - 0x10000
222+
out[c++] = String.fromCharCode(0xD800 + (u >> 10))
223+
out[c++] = String.fromCharCode(0xDC00 + (u & 1023))
224+
} else {
225+
let c2 = bytes[pos++]
226+
let c3 = bytes[pos++]
227+
out[c++] =
228+
String.fromCharCode((c1 & 15) << 12 | (c2 & 63) << 6 | c3 & 63)
229+
}
230+
}
231+
return out.join('')
172232
}

0 commit comments

Comments
 (0)