UNPKG

js-precision

Version:

一个基于 Rust + WebAssembly 的高精度计算库,封装了 [`rust_decimal`](https://crates.io/crates/rust_decimal) 实现,支持链式调用、科学计数法和精度控制,适用于前端金融、加密等高精度场景。

221 lines (191 loc) 6.23 kB
let wasm; export function __wbg_set_wasm(val) { wasm = val; } const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder; let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true }); cachedTextDecoder.decode(); let cachedUint8ArrayMemory0 = null; function getUint8ArrayMemory0() { if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); } return cachedUint8ArrayMemory0; } function getStringFromWasm0(ptr, len) { ptr = ptr >>> 0; return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); } let WASM_VECTOR_LEN = 0; const lTextEncoder = typeof TextEncoder === 'undefined' ? (0, module.require)('util').TextEncoder : TextEncoder; let cachedTextEncoder = new lTextEncoder('utf-8'); const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' ? function (arg, view) { return cachedTextEncoder.encodeInto(arg, view); } : function (arg, view) { const buf = cachedTextEncoder.encode(arg); view.set(buf); return { read: arg.length, written: buf.length }; }); function passStringToWasm0(arg, malloc, realloc) { if (realloc === undefined) { const buf = cachedTextEncoder.encode(arg); const ptr = malloc(buf.length, 1) >>> 0; getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf); WASM_VECTOR_LEN = buf.length; return ptr; } let len = arg.length; let ptr = malloc(len, 1) >>> 0; const mem = getUint8ArrayMemory0(); let offset = 0; for (; offset < len; offset++) { const code = arg.charCodeAt(offset); if (code > 0x7F) break; mem[ptr + offset] = code; } if (offset !== len) { if (offset !== 0) { arg = arg.slice(offset); } ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len); const ret = encodeString(arg, view); offset += ret.written; ptr = realloc(ptr, len, offset, 1) >>> 0; } WASM_VECTOR_LEN = offset; return ptr; } function isLikeNone(x) { return x === undefined || x === null; } const PreciseFinalization = (typeof FinalizationRegistry === 'undefined') ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry(ptr => wasm.__wbg_precise_free(ptr >>> 0, 1)); export class Precise { static __wrap(ptr) { ptr = ptr >>> 0; const obj = Object.create(Precise.prototype); obj.__wbg_ptr = ptr; PreciseFinalization.register(obj, obj.__wbg_ptr, obj); return obj; } __destroy_into_raw() { const ptr = this.__wbg_ptr; this.__wbg_ptr = 0; PreciseFinalization.unregister(this); return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_precise_free(ptr, 0); } /** * @param {string} input */ constructor(input) { const ptr0 = passStringToWasm0(input, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.precise_new(ptr0, len0); this.__wbg_ptr = ret >>> 0; PreciseFinalization.register(this, this.__wbg_ptr, this); return this; } /** * @param {number} n * @returns {Precise} */ static fromNumber(n) { const ret = wasm.precise_fromNumber(n); return Precise.__wrap(ret); } /** * @param {number} dp * @returns {Precise} */ with_precision(dp) { const ret = wasm.precise_with_precision(this.__wbg_ptr, dp); return Precise.__wrap(ret); } /** * @param {string} other * @returns {Precise} */ add(other) { const ptr0 = passStringToWasm0(other, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.precise_add(this.__wbg_ptr, ptr0, len0); return Precise.__wrap(ret); } /** * @param {string} other * @returns {Precise} */ sub(other) { const ptr0 = passStringToWasm0(other, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.precise_sub(this.__wbg_ptr, ptr0, len0); return Precise.__wrap(ret); } /** * @param {string} other * @returns {Precise} */ mul(other) { const ptr0 = passStringToWasm0(other, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.precise_mul(this.__wbg_ptr, ptr0, len0); return Precise.__wrap(ret); } /** * @param {string} other * @returns {Precise} */ div(other) { const ptr0 = passStringToWasm0(other, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); const len0 = WASM_VECTOR_LEN; const ret = wasm.precise_div(this.__wbg_ptr, ptr0, len0); return Precise.__wrap(ret); } /** * @param {number | null} [digits] * @returns {string} */ to_fixed(digits) { let deferred1_0; let deferred1_1; try { const ret = wasm.precise_to_fixed(this.__wbg_ptr, isLikeNone(digits) ? 0x100000001 : (digits) >>> 0); deferred1_0 = ret[0]; deferred1_1 = ret[1]; return getStringFromWasm0(ret[0], ret[1]); } finally { wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); } } /** * @returns {number} */ to_number() { const ret = wasm.precise_to_number(this.__wbg_ptr); return ret; } } export function __wbindgen_init_externref_table() { const table = wasm.__wbindgen_export_0; const offset = table.grow(4); table.set(0, undefined); table.set(offset + 0, undefined); table.set(offset + 1, null); table.set(offset + 2, true); table.set(offset + 3, false); ; }; export function __wbindgen_throw(arg0, arg1) { throw new Error(getStringFromWasm0(arg0, arg1)); };