Tidy up with some renames (#347)

* Tidy up with some renames

* Adjust
This commit is contained in:
Jaco
2022-05-12 08:33:42 +02:00
committed by GitHub
parent 614c88e6c3
commit 29ecaa46c2
14 changed files with 44 additions and 38 deletions

View File

@@ -7,10 +7,12 @@ import type { BridgeBase, InitFn, InitPromise, WasmBaseInstance } from './types'
import { stringToU8a, u8aToString } from '@polkadot/util';
import { Wbg } from './wbg';
export class Bridge<C extends WasmBaseInstance> implements BridgeBase<C> {
#cachegetInt32: Int32Array | null;
#cachegetUint8: Uint8Array | null;
#defaultInit: (wbg: WebAssembly.ModuleImports) => InitPromise<C>;
#createWasm: InitFn<C>;
#heap: unknown[];
#heapNext: number;
#wasm: C | null;
@@ -19,8 +21,8 @@ export class Bridge<C extends WasmBaseInstance> implements BridgeBase<C> {
#wbg: WebAssembly.ModuleImports;
#type: 'asm' | 'wasm' | 'none';
constructor (createWbg: (bridge: Bridge<C>) => WebAssembly.ModuleImports, createWasm: InitFn<C>) {
this.#defaultInit = createWasm;
constructor (createWasm: InitFn<C>) {
this.#createWasm = createWasm;
this.#cachegetInt32 = null;
this.#cachegetUint8 = null;
this.#heap = new Array(32)
@@ -31,7 +33,7 @@ export class Bridge<C extends WasmBaseInstance> implements BridgeBase<C> {
this.#wasm = null;
this.#wasmError = null;
this.#wasmPromise = null;
this.#wbg = createWbg(this);
this.#wbg = { ...new Wbg(this) };
}
get error (): string | null {
@@ -50,9 +52,9 @@ export class Bridge<C extends WasmBaseInstance> implements BridgeBase<C> {
return this.#wasm;
}
async init (override?: InitFn<C>): Promise<C | null> {
if (!this.#wasmPromise || override) {
this.#wasmPromise = (override || this.#defaultInit)(this.#wbg);
async init (createWasm?: InitFn<C>): Promise<C | null> {
if (!this.#wasmPromise || createWasm) {
this.#wasmPromise = (createWasm || this.#createWasm)(this.#wbg);
}
const { error, type, wasm } = await this.#wasmPromise;

View File

@@ -5,7 +5,7 @@ import type { InitFn, InitPromise, InitResult, WasmBaseInstance } from './types'
import { assert } from '@polkadot/util';
export function initWasm <C extends WasmBaseInstance> (root: string, wasmBytes: null | Uint8Array, asmFn: null | ((wbg: WebAssembly.ModuleImports) => C)): InitFn<C> {
export function createWasmFn <C extends WasmBaseInstance> (root: string, wasmBytes: null | Uint8Array, asmFn: null | ((wbg: WebAssembly.ModuleImports) => C)): InitFn<C> {
return async (wbg: WebAssembly.ModuleImports): InitPromise<C> => {
const result: InitResult<C> = {
error: null,

View File

@@ -11,12 +11,12 @@ export declare interface InitResult<C extends WasmBaseInstance> {
export type InitPromise <C extends WasmBaseInstance> = Promise<InitResult<C>>;
export type InitFn <C extends WasmBaseInstance> = (wgb: WebAssembly.ModuleImports) => InitPromise<C>;
export type InitFn <C extends WasmBaseInstance> = (wbg: WebAssembly.ModuleImports) => InitPromise<C>;
export interface BridgeBase<C extends WasmBaseInstance> extends InitResult<C> {
wbg: WebAssembly.ModuleImports;
init (initOverride?: InitFn<C>): Promise<C | null>;
init (createWasm?: InitFn<C>): Promise<C | null>;
getObject (idx: number): unknown;
dropObject (idx: number): void;
takeObject (idx: number): unknown;

View File

@@ -61,3 +61,7 @@ export class Wbg<C extends WasmBaseInstance> {
this.#bridge.takeObject(idx);
};
}
export function initWbg <C extends WasmBaseInstance> (bridge: BridgeBase<C>): Wbg<C> {
return new Wbg(bridge);
}

View File

@@ -4,9 +4,9 @@
import type { InitFn } from '@polkadot/wasm-bridge/types';
import type { WasmCryptoInstance } from './types';
import { initWasm } from '@polkadot/wasm-bridge';
import { createWasmFn } from '@polkadot/wasm-bridge';
import { asmJsInit } from '@polkadot/wasm-crypto-asmjs';
export { packageInfo } from './packageInfo';
export const init: InitFn<WasmCryptoInstance> = initWasm('crypto', null, asmJsInit);
export const createWasm: InitFn<WasmCryptoInstance> = createWasmFn('crypto', null, asmJsInit);

View File

@@ -4,10 +4,10 @@
import type { InitFn } from '@polkadot/wasm-bridge/types';
import type { WasmCryptoInstance } from './types';
import { initWasm } from '@polkadot/wasm-bridge';
import { createWasmFn } from '@polkadot/wasm-bridge';
import { asmJsInit } from '@polkadot/wasm-crypto-asmjs';
import { wasmBytes } from '@polkadot/wasm-crypto-wasm';
export { packageInfo } from './packageInfo';
export const init: InitFn<WasmCryptoInstance> = initWasm('crypto', wasmBytes, asmJsInit);
export const createWasm: InitFn<WasmCryptoInstance> = createWasmFn('crypto', wasmBytes, asmJsInit);

View File

@@ -4,8 +4,8 @@
import type { InitFn } from '@polkadot/wasm-bridge/types';
import type { WasmCryptoInstance } from './types';
import { initWasm } from '@polkadot/wasm-bridge';
import { createWasmFn } from '@polkadot/wasm-bridge';
export { packageInfo } from './packageInfo';
export const init: InitFn<WasmCryptoInstance> = initWasm('crypto', null, null);
export const createWasm: InitFn<WasmCryptoInstance> = createWasmFn('crypto', null, null);

View File

@@ -4,9 +4,9 @@
import type { InitFn } from '@polkadot/wasm-bridge/types';
import type { WasmCryptoInstance } from './types';
import { initWasm } from '@polkadot/wasm-bridge';
import { createWasmFn } from '@polkadot/wasm-bridge';
import { wasmBytes } from '@polkadot/wasm-crypto-wasm';
export { packageInfo } from './packageInfo';
export const init: InitFn<WasmCryptoInstance> = initWasm('crypto', wasmBytes, null);
export const createWasm: InitFn<WasmCryptoInstance> = createWasmFn('crypto', wasmBytes, null);

View File

@@ -5,7 +5,7 @@ import type { WasmCryptoInstance } from '@polkadot/wasm-crypto-init/types';
import { assert } from '@polkadot/util';
import { bridge, initBase } from './init';
import { bridge, initBridge } from './init';
export { packageInfo } from './packageInfo';
export { bridge };
@@ -221,7 +221,7 @@ export function isReady (): boolean {
export async function waitReady (): Promise<boolean> {
try {
const wasm = await initBase();
const wasm = await initBridge();
return !!wasm;
} catch {

View File

@@ -1,14 +1,14 @@
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { InitPromise } from '@polkadot/wasm-bridge/types';
import type { InitFn } from '@polkadot/wasm-bridge/types';
import type { WasmCryptoInstance } from '@polkadot/wasm-crypto-init/types';
import { Bridge, Wbg } from '@polkadot/wasm-bridge';
import { init } from '@polkadot/wasm-crypto-init';
import { Bridge } from '@polkadot/wasm-bridge';
import { createWasm } from '@polkadot/wasm-crypto-init';
export const bridge = new Bridge<WasmCryptoInstance>((b) => ({ ...new Wbg(b) }), init);
export const bridge = new Bridge<WasmCryptoInstance>(createWasm);
export async function initBase (override?: (wbg: WebAssembly.ModuleImports) => InitPromise<WasmCryptoInstance>): Promise<WasmCryptoInstance | null> {
return bridge.init(override);
export async function initBridge (createWasm?: InitFn<WasmCryptoInstance>): Promise<WasmCryptoInstance | null> {
return bridge.init(createWasm);
}

View File

@@ -1,12 +1,12 @@
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { init } from '@polkadot/wasm-crypto-init/none';
import { createWasm } from '@polkadot/wasm-crypto-init/none';
import { initBase } from './init';
import { initBridge } from './init';
export async function initWasm (): Promise<void> {
await initBase(init);
await initBridge(createWasm);
}
initWasm().catch(undefined);

View File

@@ -1,12 +1,12 @@
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { init } from '@polkadot/wasm-crypto-init/asm';
import { createWasm } from '@polkadot/wasm-crypto-init/asm';
import { initBase } from './init';
import { initBridge } from './init';
export async function initWasm (): Promise<void> {
await initBase(init);
await initBridge(createWasm);
}
initWasm().catch(undefined);

View File

@@ -1,12 +1,12 @@
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { init } from '@polkadot/wasm-crypto-init/wasm';
import { createWasm } from '@polkadot/wasm-crypto-init/wasm';
import { initBase } from './init';
import { initBridge } from './init';
export async function initWasm (): Promise<void> {
await initBase(init);
await initBridge(createWasm);
}
initWasm().catch(undefined);

View File

@@ -1,12 +1,12 @@
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { init } from '@polkadot/wasm-crypto-init/both';
import { createWasm } from '@polkadot/wasm-crypto-init/both';
import { initBase } from './init';
import { initBridge } from './init';
export async function initWasm (): Promise<void> {
await initBase(init);
await initBridge(createWasm);
}
initWasm().catch(undefined);