Skip to content

Commit

Permalink
clean up trie construction
Browse files Browse the repository at this point in the history
  • Loading branch information
chearon committed Sep 7, 2023
1 parent 2696be5 commit df2b516
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 30 deletions.
2 changes: 0 additions & 2 deletions gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ function writeTrie(filename, varname, trie) {
#include <stdint.h>
__attribute__((used))
uint32_t ${varname}[] = {${buffer.join(', ')}};
__attribute__((used))
uint32_t ${varname}_len = ${buffer.length};
`);
}

Expand Down
4 changes: 1 addition & 3 deletions gen/emoji-trie.cc

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions gen/grapheme-break-trie.cc

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions gen/line-break-trie.cc

Large diffs are not rendered by default.

Binary file modified overflow.wasm
Binary file not shown.
10 changes: 5 additions & 5 deletions src/grapheme-break.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// All code based on foliojs/grapheme-breaker at time of writing
import UnicodeTrie from './unicode-trie.js';
import {createTrie} from './unicode-trie.js';
import wasm from './wasm.js';

const heapu32 = new Uint32Array(wasm.instance.exports.memory.buffer);
const len = heapu32[wasm.instance.exports.grapheme_break_trie_len.value >> 2];
// I don't know why the pointer value is stored directly in the .value here.
// It must be an emscripten weirdness, so watch out in the future
const ptr = wasm.instance.exports.grapheme_break_trie.value >> 2;
const trie = new UnicodeTrie(heapu32.subarray(ptr, ptr + len));
const trie = createTrie(
wasm.instance.exports.memory.buffer,
wasm.instance.exports.grapheme_break_trie.value
);

// Gets a code point from a UTF-16 string
// handling surrogate pairs appropriately
Expand Down
10 changes: 5 additions & 5 deletions src/itemize.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import UnicodeTrie from './unicode-trie.js';
import {createTrie} from './unicode-trie.js';
import wasm from './wasm.js';

const heapu32 = new Uint32Array(wasm.instance.exports.memory.buffer);
const len = heapu32[wasm.instance.exports.emoji_trie_len.value >> 2];
// I don't know why the pointer value is stored directly in the .value here.
// It must be an emscripten weirdness, so watch out in the future
const ptr = wasm.instance.exports.emoji_trie.value >> 2;
const emojiTrie = new UnicodeTrie(heapu32.subarray(ptr, ptr + len));
const emojiTrie = createTrie(
wasm.instance.exports.memory.buffer,
wasm.instance.exports.emoji_trie.value
);

const {
// SheenBidi
Expand Down
10 changes: 5 additions & 5 deletions src/line-break.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// All code based on foliojs/linebreak at time of writing
import UnicodeTrie from './unicode-trie.js';
import {createTrie} from './unicode-trie.js';
import wasm from './wasm.js';

const heapu32 = new Uint32Array(wasm.instance.exports.memory.buffer);
const len = heapu32[wasm.instance.exports.line_break_trie_len.value >> 2];
// I don't know why the pointer value is stored directly in the .value here.
// It must be an emscripten weirdness, so watch out in the future
const ptr = wasm.instance.exports.line_break_trie.value >> 2;
const trie = new UnicodeTrie(heapu32.subarray(ptr, ptr + len));
const trie = createTrie(
wasm.instance.exports.memory.buffer,
wasm.instance.exports.line_break_trie.value
);

const DI_BRK = 0; // Direct break opportunity
const IN_BRK = 1; // Indirect break opportunity
Expand Down
3 changes: 2 additions & 1 deletion src/unicode-trie-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -816,9 +816,10 @@ export default class UnicodeTrieBuilder {

// calculate the sizes of, and allocate, the index and data arrays
const indexLength = allIndexesLength + this.dataLength;
const data = new Uint32Array(indexLength + 2);
const data = new Uint32Array(indexLength + 3);
let destIdx = 0;

data[destIdx++] = indexLength + 3;
data[destIdx++] = this.highStart;
data[destIdx++] = this.errorValue;

Expand Down
12 changes: 9 additions & 3 deletions src/unicode-trie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ export default class UnicodeTrie {
data: Uint32Array;

constructor(data: Uint32Array) {
this.highStart = data[0];
this.errorValue = data[1];
this.data = data.subarray(2);
this.highStart = data[1];
this.errorValue = data[2];
this.data = data.subarray(3);
}

get(codePoint: number) {
Expand Down Expand Up @@ -138,3 +138,9 @@ export default class UnicodeTrie {
}
}

export function createTrie(memory: ArrayBuffer, ptr: number) {
const ptr32 = ptr >> 2;
const heapu32 = new Uint32Array(memory);
const len = heapu32[ptr32];
return new UnicodeTrie(heapu32.subarray(ptr32, ptr32 + len));
}

0 comments on commit df2b516

Please sign in to comment.