Skip to content

Commit

Permalink
fix: adapt node module
Browse files Browse the repository at this point in the history
  • Loading branch information
mnater committed Jun 12, 2021
1 parent cd28f40 commit f5137ea
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 123 deletions.
19 changes: 12 additions & 7 deletions Hyphenopoly.js
Original file line number Diff line number Diff line change
Expand Up @@ -874,13 +874,18 @@
wa.instantiateStreaming &&
(response.headers.get("Content-Type") === "application/wasm")
) {
return wa.instantiateStreaming(r2, {
"hyphenEngine": {
"log": (value) => {
return console.log(value);
}
}
});
return wa.instantiateStreaming(r2);

/*
* Debug
* return wa.instantiateStreaming(r2, {
* "hyphenEngine": {
* "log": (value) => {
* return console.log(value);
* }
* }
* });
*/
}
return r2.arrayBuffer().then((ab) => {
return wa.instantiate(ab);
Expand Down
32 changes: 12 additions & 20 deletions hyphenopoly.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,8 @@ function prepareLanguagesObj(
* @param {function} hyphenateFunc hyphenateFunction
* @returns {function} hyphenateFunction with closured environment
*/
function encloseHyphenateFunction(baseData, hyphenateFunc) {
const heapBuffer = baseData.wasmMem.buffer;
const wordStore = new Uint16Array(heapBuffer, baseData.wo, 64);
function encloseHyphenateFunction(buf, hyphenateFunc) {
const wordStore = new Uint16Array(buf, 0, 64);

/**
* The hyphenateFunction that encloses the env above
Expand All @@ -242,16 +241,16 @@ function encloseHyphenateFunction(baseData, hyphenateFunc) {
*/
return ((word, hyphencc, leftmin, rightmin) => {
wordStore.set([
95,
46,
...[...word].map((c) => {
return c.charCodeAt(0);
}),
95,
46,
0
]);
const len = hyphenateFunc(leftmin, rightmin, hyphencc);
if (len > 0) {
word = decode(new Uint16Array(heapBuffer, baseData.hw, len));
word = decode(new Uint16Array(buf, 0, len));
}
return word;
});
Expand Down Expand Up @@ -292,26 +291,19 @@ function instantiateWasmEngine(lang) {
*/
function handleWasm(inst) {
const exp = inst.exports;
const baseData = {
/* eslint-disable multiline-ternary */
"hw": (WebAssembly.Global) ? exp.hwo.value : exp.hwo,
"lm": (WebAssembly.Global) ? exp.lmi.value : exp.lmi,
"rm": (WebAssembly.Global) ? exp.rmi.value : exp.rmi,
"wasmMem": exp.mem,
"wo": (WebAssembly.Global) ? exp.uwo.value : exp.uwo
/* eslint-enable multiline-ternary */
};
let alphalen = exp.conv();
let alphalen = exp.init();
alphalen = registerSubstitutions(alphalen, exp);
prepareLanguagesObj(
lang,
encloseHyphenateFunction(
baseData,
exp.mem.buffer,
exp.hyphenate
),
decode(new Uint16Array(exp.mem.buffer, 1026, alphalen - 1)),
baseData.lm,
baseData.rm
decode(new Uint16Array(exp.mem.buffer, 1280, alphalen)),
/* eslint-disable multiline-ternary */
(WebAssembly.Global) ? exp.lmi.value : exp.lmi,
(WebAssembly.Global) ? exp.rmi.value : exp.rmi
/* eslint-enable multiline-ternary */
);
}
if (H.c.sync) {
Expand Down
2 changes: 1 addition & 1 deletion src/hyphenEngine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare function log(arg0: i32): void;
// Debug: declare function log(arg0: i32): void;

let alphabetOffset:i32 = 0;
let bitmapOffset:i32 = 0;
Expand Down
138 changes: 43 additions & 95 deletions test/wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ t.test("load module", async(t) => {
t.test("check filesize", async(t) => {
return t.equal(
hyphenEngine.buffer.byteLength,
91989,
78907,
"update when de.wasm changes"
);
});
Expand All @@ -36,79 +36,59 @@ t.test("load module", async(t) => {
t.test("check instance keys", async(t) => {
return t.same(
Object.keys(result.instance.exports),
["uwo", "hwo", "lmi", "rmi", "subst", "conv", "hyphenate", "mem"]
["lmi", "rmi", "init", "subst", "hyphenate", "mem"]
);
});
t.test("build trie", async(t) => {
const ret = result.instance.exports.conv();
return t.equal(ret, 32, "length of alphabet");
const ret = result.instance.exports.init();
return t.equal(ret, 31, "length of alphabet");
});
t.test("check translate table", async(t) => {
const heapBuffer = result.instance.exports.mem.buffer;
const translateTableCC = new Uint16Array(heapBuffer, 0, 256);
const translateTableID = new Uint8Array(heapBuffer, 512, 256);
const translateTableCC = new Uint16Array(heapBuffer, 256, 256);
const translateTableID = new Uint8Array(heapBuffer, 768, 256);
return t.same(
[
translateTableID[translateTableCC.indexOf(95)],
translateTableID[translateTableCC.indexOf(46)],
translateTableID[translateTableCC.indexOf(97)],
translateTableID[translateTableCC.indexOf(98)],
translateTableID[translateTableCC.indexOf(65)]
],
[1, 2, 3, 2]
[0, 1, 2, 1]
);
});
t.test("hyphenate standard word", async(t) => {
const heapBuffer = result.instance.exports.mem.buffer;
const unhyphenatedWordOffset = (WebAssembly.Global)
? result.instance.exports.uwo.value
: result.instance.exports.uwo;
const hyphenatedWordOffset = (WebAssembly.Global)
? result.instance.exports.hwo.value
: result.instance.exports.hwo;
const unhyphenatedWordStore = new Uint16Array(
heapBuffer,
unhyphenatedWordOffset,
64
);
unhyphenatedWordStore.set([
95,
const wordStore = new Uint16Array(heapBuffer, 0, 64);
wordStore.set([
46,
...[..."Silbentrennung"].map((c) => {
return c.charCodeAt(0);
}),
95,
46,
0
]);
const len = result.instance.exports.hyphenate(2, 2, 8226);
t.test("check length of hwo", async(t) => {
t.test("check length of hyphenated word", async(t) => {
return t.equal(len, 17);
});
t.test("check hyphenated word", async(t) => {
const hw = decode(
new Uint16Array(heapBuffer, hyphenatedWordOffset, len)
new Uint16Array(heapBuffer, 0, len)
);
return t.equal(hw, "Sil•ben•tren•nung");
});
});

t.test("hyphenate word with unknown char (no collision)", async(t) => {
const heapBuffer = result.instance.exports.mem.buffer;
const unhyphenatedWordOffset = (WebAssembly.Global)
? result.instance.exports.uwo.value
: result.instance.exports.uwo;
const hyphenatedWordOffset = (WebAssembly.Global)
? result.instance.exports.hwo.value
: result.instance.exports.hwo;
const unhyphenatedWordStore = new Uint16Array(
heapBuffer,
unhyphenatedWordOffset,
64
);
unhyphenatedWordStore.set([
95,
const wordStore = new Uint16Array(heapBuffer, 0, 64);
wordStore.set([
46,
...[..."Jalapeños"].map((c) => {
return c.charCodeAt(0);
}),
95,
46,
0
]);
const len = result.instance.exports.hyphenate(2, 2, 8226);
Expand All @@ -117,31 +97,21 @@ t.test("load module", async(t) => {
});
t.test("check hyphenated word", async(t) => {
const hw = decode(
new Uint16Array(heapBuffer, hyphenatedWordOffset, len)
new Uint16Array(heapBuffer, 0, len)
);
return t.equal(hw, "");
});
});

t.test("hyphenate word with unknown char (collision)", async(t) => {
const heapBuffer = result.instance.exports.mem.buffer;
const unhyphenatedWordOffset = (WebAssembly.Global)
? result.instance.exports.uwo.value
: result.instance.exports.uwo;
const hyphenatedWordOffset = (WebAssembly.Global)
? result.instance.exports.hwo.value
: result.instance.exports.hwo;
const unhyphenatedWordStore = new Uint16Array(
heapBuffer,
unhyphenatedWordOffset,
64
);
unhyphenatedWordStore.set([
95,
const wordStore = new Uint16Array(heapBuffer, 0, 64);
wordStore.set([
46,
...[..."Test\u0563test"].map((c) => {
return c.charCodeAt(0);
}),
95,
46,
0
]);
const len = result.instance.exports.hyphenate(2, 2, 8226);
Expand All @@ -150,7 +120,7 @@ t.test("load module", async(t) => {
});
t.test("check hyphenated word", async(t) => {
const hw = decode(
new Uint16Array(heapBuffer, hyphenatedWordOffset, len)
new Uint16Array(heapBuffer, 0, len)
);
return t.equal(hw, "");
});
Expand All @@ -159,41 +129,30 @@ t.test("load module", async(t) => {
t.test("Add char substitution (no collision)", async(t) => {
const ret = result.instance.exports.subst(241, 209, 110);
t.test("check new alphabet length", async(t) => {
return t.equal(ret, 33);
return t.equal(ret, 32);
});
t.test("check translate table with char substitution", async(t) => {
const heapBuffer = result.instance.exports.mem.buffer;
const translateTableCC = new Uint16Array(heapBuffer, 0, 256);
const translateTableID = new Uint8Array(heapBuffer, 512, 256);
const translateTableCC = new Uint16Array(heapBuffer, 256, 256);
const translateTableID = new Uint8Array(heapBuffer, 768, 256);
return t.same(
[
translateTableID[translateTableCC.indexOf(241)],
translateTableID[translateTableCC.indexOf(209)],
translateTableID[translateTableCC.indexOf(110)]
],
[15, 15, 15]
[14, 14, 14]
);
});
t.test("hyphenate word with substituted char", async(t) => {
const exp = result.instance.exports;
const heapBuffer = exp.mem.buffer;
const unhyphenatedWordOffset = (WebAssembly.Global)
? result.instance.exports.uwo.value
: result.instance.exports.uwo;
const hyphenatedWordOffset = (WebAssembly.Global)
? result.instance.exports.hwo.value
: result.instance.exports.hwo;
const unhyphenatedWordStore = new Uint16Array(
heapBuffer,
unhyphenatedWordOffset,
64
);
unhyphenatedWordStore.set([
95,
const heapBuffer = result.instance.exports.mem.buffer;
const wordStore = new Uint16Array(heapBuffer, 0, 64);
wordStore.set([
46,
...[..."Jalapeños"].map((c) => {
return c.charCodeAt(0);
}),
95,
46,
0
]);
const len = result.instance.exports.hyphenate(2, 2, 8226);
Expand All @@ -202,7 +161,7 @@ t.test("load module", async(t) => {
});
t.test("check hyphenated word", async(t) => {
const hw = decode(
new Uint16Array(heapBuffer, hyphenatedWordOffset, len)
new Uint16Array(heapBuffer, 0, len)
);
return t.equal(hw, "Jala•pe•ños");
});
Expand All @@ -211,39 +170,28 @@ t.test("load module", async(t) => {
t.test("Add char substitution (collision)", async(t) => {
const ret = result.instance.exports.subst(1086, 1054, 111);
t.test("check new alphabet length", async(t) => {
return t.equal(ret, 34);
return t.equal(ret, 33);
});
t.test("check translate table with char substitution", async(t) => {
const heapBuffer = result.instance.exports.mem.buffer;
const collisionsTable = new Uint16Array(heapBuffer, 768, 128);
const collisionsTable = new Uint16Array(heapBuffer, 1024, 128);
return t.same(
[
collisionsTable[collisionsTable.indexOf(1086) + 1],
collisionsTable[collisionsTable.indexOf(1054) + 1]
],
[16, 16]
[15, 15]
);
});
t.test("hyphenate word with substituted char", async(t) => {
const exp = result.instance.exports;
const heapBuffer = exp.mem.buffer;
const unhyphenatedWordOffset = (WebAssembly.Global)
? result.instance.exports.uwo.value
: result.instance.exports.uwo;
const hyphenatedWordOffset = (WebAssembly.Global)
? result.instance.exports.hwo.value
: result.instance.exports.hwo;
const unhyphenatedWordStore = new Uint16Array(
heapBuffer,
unhyphenatedWordOffset,
64
);
unhyphenatedWordStore.set([
95,
const heapBuffer = result.instance.exports.mem.buffer;
const wordStore = new Uint16Array(heapBuffer, 0, 64);
wordStore.set([
46,
...[..."Glasn\u043Est"].map((c) => {
return c.charCodeAt(0);
}),
95,
46,
0
]);
const len = result.instance.exports.hyphenate(2, 2, 8226);
Expand All @@ -252,7 +200,7 @@ t.test("load module", async(t) => {
});
t.test("check hyphenated word", async(t) => {
const hw = decode(
new Uint16Array(heapBuffer, hyphenatedWordOffset, len)
new Uint16Array(heapBuffer, 0, len)
);
return t.equal(hw, "Glas•n\u043Est");
});
Expand Down

0 comments on commit f5137ea

Please sign in to comment.