Skip to content

Commit

Permalink
fix #11: add text conversion polyfills for Edge
Browse files Browse the repository at this point in the history
  • Loading branch information
hlorenzi committed Jun 23, 2019
1 parent 182bea6 commit f64ea0e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ Usage: customasm [options] <asm-file-1> ... <asm-file-N>
Options:
-f, --format FORMAT The format of the output file. Possible formats:
binary, binstr, hexstr, bindump, hexdump,
mif, intelhex, deccomma, hexcomma, decc, hexc,
logisim8, logisim16
mif, intelhex, deccomma, hexcomma, decc, hexc,
logisim8, logisim16
-o, --output FILE The name of the output file.
-p, --print Print output to stdout instead of writing to a file.
-q, --quiet Suppress progress reports.
Expand Down
Binary file modified web/customasm.gc.wasm
Binary file not shown.
66 changes: 63 additions & 3 deletions web/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function assemble()

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


Expand All @@ -130,7 +130,7 @@ function makeRustString(str)
//console.log("makeRustString")
//console.log(str)

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

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

//console.log(bytes)

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

g_wasm.instance.exports.wasm_string_drop(ptr)
}


// From https://github.com/google/closure-library/blob/e877b1eac410c0d842bcda118689759512e0e26f/closure/goog/crypt/crypt.js#L115
function stringToUtf8ByteArray(str)
{
let out = [], p = 0
for (let i = 0; i < str.length; i++) {
let c = str.charCodeAt(i)
if (c < 128) {
out[p++] = c
} else if (c < 2048) {
out[p++] = (c >> 6) | 192
out[p++] = (c & 63) | 128
} else if (
((c & 0xFC00) == 0xD800) && (i + 1) < str.length &&
((str.charCodeAt(i + 1) & 0xFC00) == 0xDC00)) {
// Surrogate Pair
c = 0x10000 + ((c & 0x03FF) << 10) + (str.charCodeAt(++i) & 0x03FF)
out[p++] = (c >> 18) | 240
out[p++] = ((c >> 12) & 63) | 128
out[p++] = ((c >> 6) & 63) | 128
out[p++] = (c & 63) | 128
} else {
out[p++] = (c >> 12) | 224
out[p++] = ((c >> 6) & 63) | 128
out[p++] = (c & 63) | 128
}
}
return out
}


// From https://github.com/google/closure-library/blob/e877b1eac410c0d842bcda118689759512e0e26f/closure/goog/crypt/crypt.js#L149
function utf8ByteArrayToString(bytes)
{
let out = [], pos = 0, c = 0
while (pos < bytes.length) {
let c1 = bytes[pos++]
if (c1 < 128) {
out[c++] = String.fromCharCode(c1)
} else if (c1 > 191 && c1 < 224) {
let c2 = bytes[pos++]
out[c++] = String.fromCharCode((c1 & 31) << 6 | c2 & 63)
} else if (c1 > 239 && c1 < 365) {
// Surrogate Pair
let c2 = bytes[pos++]
let c3 = bytes[pos++]
let c4 = bytes[pos++]
let u = ((c1 & 7) << 18 | (c2 & 63) << 12 | (c3 & 63) << 6 | c4 & 63) - 0x10000
out[c++] = String.fromCharCode(0xD800 + (u >> 10))
out[c++] = String.fromCharCode(0xDC00 + (u & 1023))
} else {
let c2 = bytes[pos++]
let c3 = bytes[pos++]
out[c++] =
String.fromCharCode((c1 & 15) << 12 | (c2 & 63) << 6 | c3 & 63)
}
}
return out.join('')
}

0 comments on commit f64ea0e

Please sign in to comment.