UNPKG

piyin-wasm

Version:

convert Chinese character to pinyin with WebAssembly!

136 lines (113 loc) 3.66 kB
import * as wasm from './piyin_wasm_bg.wasm'; /** */ export function greet() { wasm.greet(); } let WASM_VECTOR_LEN = 0; let cachedTextEncoder = new TextEncoder('utf-8'); let cachegetUint8Memory = null; function getUint8Memory() { if (cachegetUint8Memory === null || cachegetUint8Memory.buffer !== wasm.memory.buffer) { cachegetUint8Memory = new Uint8Array(wasm.memory.buffer); } return cachegetUint8Memory; } let passStringToWasm; if (typeof cachedTextEncoder.encodeInto === 'function') { passStringToWasm = function(arg) { let size = arg.length; let ptr = wasm.__wbindgen_malloc(size); let offset = 0; { const mem = getUint8Memory(); for (; offset < arg.length; offset++) { const code = arg.charCodeAt(offset); if (code > 0x7F) break; mem[ptr + offset] = code; } } if (offset !== arg.length) { arg = arg.slice(offset); ptr = wasm.__wbindgen_realloc(ptr, size, size = offset + arg.length * 3); const view = getUint8Memory().subarray(ptr + offset, ptr + size); const ret = cachedTextEncoder.encodeInto(arg, view); offset += ret.written; } WASM_VECTOR_LEN = offset; return ptr; }; } else { passStringToWasm = function(arg) { let size = arg.length; let ptr = wasm.__wbindgen_malloc(size); let offset = 0; { const mem = getUint8Memory(); for (; offset < arg.length; offset++) { const code = arg.charCodeAt(offset); if (code > 0x7F) break; mem[ptr + offset] = code; } } if (offset !== arg.length) { const buf = cachedTextEncoder.encode(arg.slice(offset)); ptr = wasm.__wbindgen_realloc(ptr, size, size = offset + buf.length); getUint8Memory().set(buf, ptr + offset); offset += buf.length; } WASM_VECTOR_LEN = offset; return ptr; }; } let cachegetInt32Memory = null; function getInt32Memory() { if (cachegetInt32Memory === null || cachegetInt32Memory.buffer !== wasm.memory.buffer) { cachegetInt32Memory = new Int32Array(wasm.memory.buffer); } return cachegetInt32Memory; } let cachedTextDecoder = new TextDecoder('utf-8'); function getStringFromWasm(ptr, len) { return cachedTextDecoder.decode(getUint8Memory().subarray(ptr, ptr + len)); } /** */ export class Pinyin { static __wrap(ptr) { const obj = Object.create(Pinyin.prototype); obj.ptr = ptr; return obj; } free() { const ptr = this.ptr; this.ptr = 0; wasm.__wbg_pinyin_free(ptr); } /** * @returns {Pinyin} */ static new() { const ret = wasm.pinyin_new(); return Pinyin.__wrap(ret); } /** * @param {string} han * @param {number} style * @returns {string} */ covert(han, style) { const retptr = 8; const ret = wasm.pinyin_covert(retptr, this.ptr, passStringToWasm(han), WASM_VECTOR_LEN, style); const memi32 = getInt32Memory(); const v0 = getStringFromWasm(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1]).slice(); wasm.__wbindgen_free(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1] * 1); return v0; } } export const __wbg_alert_c0e31539759c688a = function(arg0, arg1) { alert(getStringFromWasm(arg0, arg1)); }; export const __wbindgen_throw = function(arg0, arg1) { throw new Error(getStringFromWasm(arg0, arg1)); };