forked from cryptogarageinc/cfd-js-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfdjs_wasm_json.js
45 lines (40 loc) · 1.39 KB
/
cfdjs_wasm_json.js
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
const cfdjsWasm = require('./dist/cfdjs_wasm.js');
const cfdjsWasmJsonApi = require('./cfdjs_wasm_jsonapi.js');
cfdjsWasm['preInit'] = function() {};
const wrappedModule = {};
cfdjsWasm['onRuntimeInitialized'] = async () => {
const funcNameResult = await cfdjsWasmJsonApi.ccallCfd(cfdjsWasm,
cfdjsWasm._cfdjsGetJsonApiNames, 'string', [], []);
if (funcNameResult.indexOf('Error:') >= 0) {
throw new cfdjsWasmJsonApi.CfdError(
`cfdjsGetJsonApiNames internal error. ${funcNameResult}`);
}
const funcList = funcNameResult.split(',');
// register function list
funcList.forEach((requestName) => {
const hook = async function(...args) {
if (args.length > 1) {
throw new cfdjsWasmJsonApi.CfdError('ERROR: Invalid argument passed:' +
`func=[${requestName}], args=[${args}]`);
}
let arg = '';
if (typeof args === 'undefined') {
arg = '';
} else if (typeof args === 'string') {
arg = args;
} else if (args) {
arg = args[0];
}
return await cfdjsWasmJsonApi.callJsonApi(cfdjsWasm, requestName, arg);
};
Object.defineProperty(wrappedModule, requestName, {
value: hook,
enumerable: true,
});
});
if ('onRuntimeInitialized' in wrappedModule) {
wrappedModule.onRuntimeInitialized();
}
};
module.exports = wrappedModule;
module.exports.CfdError = cfdjsWasmJsonApi.CfdError;