PHP WebShell

Текущая директория: /usr/lib/node_modules/bitgo/node_modules/@polkadot/wasm-bridge

Просмотр файла: bridge.js

import { stringToU8a, u8aToString } from '@polkadot/util';
import { Wbg } from './wbg.js';
/**
 * @name Bridge
 * @description
 * Creates a bridge between the JS and WASM environments.
 *
 * For any bridge it is passed an function which is then called internally at the
 * time of initialization. This affectively implements the layer between WASM and
 * the native environment, providing all the plumbing needed for the Wbg classes.
 */
export class Bridge {
    #createWasm;
    #heap;
    #wbg;
    #cachegetInt32;
    #cachegetUint8;
    #heapNext;
    #wasm;
    #wasmError;
    #wasmPromise;
    #type;
    constructor(createWasm) {
        this.#createWasm = createWasm;
        this.#cachegetInt32 = null;
        this.#cachegetUint8 = null;
        this.#heap = new Array(32)
            .fill(undefined)
            .concat(undefined, null, true, false);
        this.#heapNext = this.#heap.length;
        this.#type = 'none';
        this.#wasm = null;
        this.#wasmError = null;
        this.#wasmPromise = null;
        this.#wbg = { ...new Wbg(this) };
    }
    /** @description Returns the init error */
    get error() {
        return this.#wasmError;
    }
    /** @description Returns the init type */
    get type() {
        return this.#type;
    }
    /** @description Returns the created wasm interface */
    get wasm() {
        return this.#wasm;
    }
    /** @description Performs the wasm initialization */
    async init(createWasm) {
        if (!this.#wasmPromise || createWasm) {
            this.#wasmPromise = (createWasm || this.#createWasm)(this.#wbg);
        }
        const { error, type, wasm } = await this.#wasmPromise;
        this.#type = type;
        this.#wasm = wasm;
        this.#wasmError = error;
        return this.#wasm;
    }
    /**
     * @internal
     * @description Gets an object from the heap
     */
    getObject(idx) {
        return this.#heap[idx];
    }
    /**
     * @internal
     * @description Removes an object from the heap
     */
    dropObject(idx) {
        if (idx < 36) {
            return;
        }
        this.#heap[idx] = this.#heapNext;
        this.#heapNext = idx;
    }
    /**
     * @internal
     * @description Retrieves and removes an object to the heap
     */
    takeObject(idx) {
        const ret = this.getObject(idx);
        this.dropObject(idx);
        return ret;
    }
    /**
     * @internal
     * @description Adds an object to the heap
     */
    addObject(obj) {
        if (this.#heapNext === this.#heap.length) {
            this.#heap.push(this.#heap.length + 1);
        }
        const idx = this.#heapNext;
        this.#heapNext = this.#heap[idx];
        this.#heap[idx] = obj;
        return idx;
    }
    /**
     * @internal
     * @description Retrieve an Int32 in the WASM interface
     */
    getInt32() {
        if (this.#cachegetInt32 === null || this.#cachegetInt32.buffer !== this.#wasm.memory.buffer) {
            this.#cachegetInt32 = new Int32Array(this.#wasm.memory.buffer);
        }
        return this.#cachegetInt32;
    }
    /**
     * @internal
     * @description Retrieve an Uint8Array in the WASM interface
     */
    getUint8() {
        if (this.#cachegetUint8 === null || this.#cachegetUint8.buffer !== this.#wasm.memory.buffer) {
            this.#cachegetUint8 = new Uint8Array(this.#wasm.memory.buffer);
        }
        return this.#cachegetUint8;
    }
    /**
     * @internal
     * @description Retrieves an Uint8Array in the WASM interface
     */
    getU8a(ptr, len) {
        return this.getUint8().subarray(ptr / 1, ptr / 1 + len);
    }
    /**
     * @internal
     * @description Retrieves a string in the WASM interface
     */
    getString(ptr, len) {
        return u8aToString(this.getU8a(ptr, len));
    }
    /**
     * @internal
     * @description Allocates an Uint8Array in the WASM interface
     */
    allocU8a(arg) {
        const ptr = this.#wasm.__wbindgen_malloc(arg.length * 1);
        this.getUint8().set(arg, ptr / 1);
        return [ptr, arg.length];
    }
    /**
     * @internal
     * @description Allocates a string in the WASM interface
     */
    allocString(arg) {
        return this.allocU8a(stringToU8a(arg));
    }
    /**
     * @internal
     * @description Retrieves an Uint8Array from the WASM interface
     */
    resultU8a() {
        const r0 = this.getInt32()[8 / 4 + 0];
        const r1 = this.getInt32()[8 / 4 + 1];
        const ret = this.getU8a(r0, r1).slice();
        this.#wasm.__wbindgen_free(r0, r1 * 1);
        return ret;
    }
    /**
     * @internal
     * @description Retrieve a string from the WASM interface
     */
    resultString() {
        return u8aToString(this.resultU8a());
    }
}

Выполнить команду


Для локальной разработки. Не используйте в интернете!