-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.ts
48 lines (37 loc) · 1.6 KB
/
index.ts
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
// Copyright 2017-2018 @polkadot/client-wasm authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { Config } from '@polkadot/client/types';
import { RuntimeInterface } from '@polkadot/client-runtime/types';
import { Logger } from '@polkadot/util/types';
import { WasmInstanceExports } from '../types';
import defaults from '../defaults';
import createEnv from './env';
import createExports from './exports';
import createMemory from './memory';
type Options = {
config: Config,
l: Logger
};
function instrument <T> (name: string, elapsed: Array<string>, fn: () => T): T {
const start = Date.now();
const result = fn();
elapsed.push(`${name}=${Date.now() - start}ms`);
return result;
}
export default function wasm ({ config: { wasm: { heapSize = defaults.HEAP_SIZE_KB } }, l }: Options, runtime: RuntimeInterface, chainCode: Uint8Array, chainProxy: Uint8Array): WasmInstanceExports {
const elapsed: string[] = [];
const env = instrument('runtime', elapsed, (): WasmInstanceExports =>
createEnv(runtime, createMemory(0, 0))
);
const chain = instrument('chain', elapsed, (): WasmInstanceExports =>
createExports(chainCode, { env })
);
const instance = instrument('proxy', elapsed, (): WasmInstanceExports =>
createExports(chainProxy, { proxy: chain }, createMemory(0, 0))
);
const offset = chain.memory.grow(Math.ceil(heapSize / 64));
runtime.environment.heap.setWasmMemory(chain.memory, offset * 64 * 1024);
l.debug(() => `WASM created ${elapsed.join(', ')}`);
return instance;
}