-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathworker.js
More file actions
90 lines (83 loc) · 2.89 KB
/
Copy pathworker.js
File metadata and controls
90 lines (83 loc) · 2.89 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const Module = require('./dist/camaro')
let cachedInstance
function callWasmBinding(methodName, ...args) {
if (!cachedInstance) throw new Error('camaro is not initialized yet.')
return cachedInstance[methodName](...args)
}
function asUint8View(input) {
if (input instanceof Uint8Array) return input
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(input)) {
return new Uint8Array(input.buffer, input.byteOffset, input.byteLength)
}
if (input instanceof ArrayBuffer) return new Uint8Array(input)
return null
}
function withMallocUtf8(u8View, wasmCall) {
const M = cachedInstance
const n = u8View.byteLength
if (n === 0) return wasmCall(0, 0)
const need = n + 1
if (!withMallocUtf8.scratch || withMallocUtf8.cap < need) {
if (withMallocUtf8.scratch) M._free(withMallocUtf8.scratch)
withMallocUtf8.cap = Math.max(need, withMallocUtf8.cap * 2 || 65536)
withMallocUtf8.scratch = M._malloc(withMallocUtf8.cap)
if (!withMallocUtf8.scratch) throw new Error('camaro WASM heap allocation failed')
}
M.HEAPU8.set(u8View.subarray(0, n), withMallocUtf8.scratch)
M.HEAPU8[withMallocUtf8.scratch + n] = 0
return wasmCall(withMallocUtf8.scratch, n)
}
withMallocUtf8.scratch = 0
withMallocUtf8.cap = 0
// Non-MODULARIZE emscripten exports the Module object; wasm init is async.
const ready = new Promise((resolve) => {
const finish = () => {
cachedInstance = Module
resolve()
}
if (Module.calledRun) {
finish()
} else {
const prev = Module.onRuntimeInitialized
Module.onRuntimeInitialized = function () {
if (typeof prev === 'function') prev()
finish()
}
}
})
function runTask({ fn, args }) {
if (fn === 'transform') {
const [xml, tmplStr] = args
const u8 = asUint8View(xml)
if (u8 !== null)
return withMallocUtf8(u8, (ptr, len) =>
callWasmBinding('transformFromUtf8', ptr, len, tmplStr))
return callWasmBinding(fn, xml, tmplStr)
}
if (fn === 'toJson') {
const [xml] = args
const u8 = asUint8View(xml)
if (u8 !== null)
return withMallocUtf8(u8, (ptr, len) =>
callWasmBinding('toJsonFromUtf8', ptr, len))
return callWasmBinding(fn, xml)
}
if (fn === 'prettyPrint') {
const [xml, opts] = args
const u8 = asUint8View(xml)
if (u8 !== null)
return withMallocUtf8(u8, (ptr, len) =>
callWasmBinding('prettyPrintFromUtf8', ptr, len, opts))
return callWasmBinding(fn, xml, opts)
}
return callWasmBinding(fn, ...args)
}
module.exports = async (task) => {
await ready
return runTask(task)
}
module.exports.whenReady = () => ready
module.exports.runSync = (task) => {
if (!cachedInstance) throw new Error('camaro is not initialized yet.')
return runTask(task)
}