From 00049a842a41d81f67a78d46965040f9d121df27 Mon Sep 17 00:00:00 2001 From: Arseny Smirnov Date: Fri, 9 Oct 2020 19:40:13 +0300 Subject: [PATCH] tdweb: temporary fix to access Module.FS before promise is completed GitOrigin-RevId: 32af58ee1994919c75d50f8a2da2643a20bf166c --- CMakeLists.txt | 1 + example/web/tdweb/src/index.js | 9 +++++++-- example/web/tdweb/src/worker.js | 26 ++++++++++++++++++++------ post.js | 1 + 4 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 post.js diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f2ea6362..86cdcabcd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -113,6 +113,7 @@ if (EMSCRIPTEN) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s WASM=1") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s WASM=1") endif() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --post-js ${CMAKE_CURRENT_SOURCE_DIR}/post.js") endif() if (NOT OPENSSL_FOUND) diff --git a/example/web/tdweb/src/index.js b/example/web/tdweb/src/index.js index 1c38f154c..e8a5d16cc 100644 --- a/example/web/tdweb/src/index.js +++ b/example/web/tdweb/src/index.js @@ -169,7 +169,7 @@ class TdClient { 'receive from worker: ', JSON.parse( JSON.stringify(response, (key, value) => { - if (key === 'arr' || key == 'data') { + if (key === 'arr' || key === 'data') { return undefined; } return value; @@ -233,7 +233,12 @@ class TdClient { } for (const key in response) { const field = response[key]; - if (field && typeof field === 'object' && key != 'data' && key != 'arr') { + if ( + field && + typeof field === 'object' && + key !== 'data' && + key !== 'arr' + ) { response[key] = this.prepareResponse(field); } } diff --git a/example/web/tdweb/src/worker.js b/example/web/tdweb/src/worker.js index 42fb181e1..ad6c32e0a 100644 --- a/example/web/tdweb/src/worker.js +++ b/example/web/tdweb/src/worker.js @@ -70,7 +70,7 @@ async function loadTdlibWasm(onFS, wasmUrl) { if (wasmUrl) { td_wasm = wasmUrl; } - const module = await createTdwebModule({ + let module = createTdwebModule({ onRuntimeInitialized: () => { log.info('runtime intialized'); }, @@ -85,8 +85,11 @@ async function loadTdlibWasm(onFS, wasmUrl) { }, ENVIROMENT: 'WORKER' }); + onFS(module.FS); // hack + log.info('Wait module'); + module = await module; log.info('Got module', module); - onFS(module.FS); + //onFS(module.FS); return module; } @@ -97,7 +100,7 @@ async function loadTdlibAsmjs(onFS) { console.log('got td_asm.js', createTdwebModule); const fromFile = 'td_asmjs.js.mem'; const toFile = td_asmjs_mem_release; - const module = await createTdwebModule({ + let module = createTdwebModule({ onRuntimeInitialized: () => { console.log('runtime intialized'); }, @@ -109,7 +112,11 @@ async function loadTdlibAsmjs(onFS) { }, ENVIROMENT: 'WORKER' }); - onFS(module.FS); + onFS(module.FS); // hack + log.info('Wait module'); + module = await module; + log.info('Got module', module); + //onFS(module.FS); return module; } @@ -608,7 +615,10 @@ class TdClient { log.info('got TdModule'); this.td_functions = { td_create: this.TdModule.cwrap('td_emscripten_create', 'number', []), - td_send: this.TdModule.cwrap('td_emscripten_send', null, ['number', 'string']), + td_send: this.TdModule.cwrap('td_emscripten_send', null, [ + 'number', + 'string' + ]), td_execute: this.TdModule.cwrap('td_emscripten_execute', 'string', [ 'string' ]), @@ -621,7 +631,11 @@ class TdClient { }) ); }, - td_get_timeout: this.TdModule.cwrap('td_emscripten_get_timeout', 'number', []) + td_get_timeout: this.TdModule.cwrap( + 'td_emscripten_get_timeout', + 'number', + [] + ) }; //this.onFS(this.TdModule.FS); this.FS = this.TdModule.FS; diff --git a/post.js b/post.js new file mode 100644 index 000000000..531af4df1 --- /dev/null +++ b/post.js @@ -0,0 +1 @@ +createTdwebModule.ready.FS = Module.FS;