-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (40 loc) · 1.05 KB
/
index.js
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
const fs = require('fs')
async function setupWasi(fileName) {
// Read the wasm file.
const buf = fs.readFileSync(fileName)
// Instantiate the wasm module.
const res = await WebAssembly.instantiate(buf)
return res
}
async function main() {
// Setup the WASI instance.
const wasi = await setupWasi('./strwidth.wasm')
// Get the functions exported from the WebAssembly
const {
strWidth,
setAmbw,
malloc,
memory
} = wasi.instance.exports
let patternPtr = malloc(1024)
const bytes = new Uint8Array(memory.buffer)
const changePattern = (pattern) => {
let buf = Buffer.from(pattern, 'utf8')
let len = buf.length
if (len > 1024) throw new Error('pattern too long')
bytes.set(buf, patternPtr)
bytes[patternPtr + len] = 0
}
const getWidth = text => {
changePattern(text)
let width = strWidth(patternPtr)
console.log(`${text}: ${width}`)
}
setAmbw(1)
getWidth('你好啊')
getWidth('fo o 这个')
getWidth('\t')
getWidth('\x1b')
getWidth('1\t2\t')
}
main().then(() => console.log('Done'))