oops, actually pass arguments to emscripten

This commit is contained in:
Florian Dold 2019-08-16 00:15:19 +02:00
parent 0cab39f2b6
commit cc4eeec031
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
2 changed files with 55 additions and 44 deletions

View File

@ -1,6 +1,6 @@
{
"name": "taler-wallet",
"version": "0.0.3",
"version": "0.0.4",
"description": "",
"main": "dist/node/index.js",
"repository": {

View File

@ -24,7 +24,9 @@ import fs = require("fs");
*/
export class SynchronousCryptoWorker {
private cachedEmscEnvironment: EmscEnvironment | undefined = undefined;
private cachedEmscEnvironmentPromise: Promise<EmscEnvironment> | undefined = undefined;
private cachedEmscEnvironmentPromise:
| Promise<EmscEnvironment>
| undefined = undefined;
/**
* Function to be called when we receive a message from the worker thread.
@ -64,6 +66,28 @@ export class SynchronousCryptoWorker {
return this.cachedEmscEnvironmentPromise;
}
const binaryPath =
__dirname + "/../../../emscripten/taler-emscripten-lib.wasm";
console.log("reading from", binaryPath);
const wasmBinary = new Uint8Array(fs.readFileSync(binaryPath));
let lib: any;
return new Promise((resolve, reject) => {
// Arguments passed to the emscripten prelude
const libArgs = {
wasmBinary,
onRuntimeInitialized: () => {
if (!lib) {
console.error("fatal emscripten initialization error");
return;
}
this.cachedEmscEnvironmentPromise = undefined;
this.cachedEmscEnvironment = new EmscEnvironment(lib);
resolve(this.cachedEmscEnvironment);
},
};
// Make sure that TypeScript doesn't try
// to check the taler-emscripten-lib.
const indirectRequire = require;
@ -83,7 +107,7 @@ export class SynchronousCryptoWorker {
const libFn = indirectRequire(
"../../../emscripten/taler-emscripten-lib.js",
);
const lib = libFn();
const lib = libFn(libArgs);
// @ts-ignore
g.importScripts = savedImportScripts;
@ -96,20 +120,7 @@ export class SynchronousCryptoWorker {
"sanity check failed: taler-emscripten lib does not have 'ccall'",
);
}
const binaryPath = __dirname + "/../../../emscripten/taler-emscripten-lib.wasm";
console.log("reading from", binaryPath);
const wasmBinary = new Uint8Array(fs.readFileSync(binaryPath));
this.cachedEmscEnvironmentPromise = new Promise((resolve, reject) => {
lib.wasmBinary = wasmBinary;
lib.onRuntimeInitialized = () => {
this.cachedEmscEnvironmentPromise = undefined;
this.cachedEmscEnvironment = new EmscEnvironment(lib);
resolve(this.cachedEmscEnvironment);
};
});
return this.cachedEmscEnvironmentPromise;
}
private dispatchMessage(msg: any) {
@ -157,7 +168,7 @@ export class SynchronousCryptoWorker {
return;
}
this.handleRequest(operation, id, args).catch((e) => {
this.handleRequest(operation, id, args).catch(e => {
console.error("Error while handling crypto request:", e);
});
}