From e6b3f9ea98943017f74485b1f28234833ae42e13 Mon Sep 17 00:00:00 2001 From: Arseny Smirnov Date: Tue, 23 Apr 2019 20:21:54 +0300 Subject: [PATCH] tdweb:0.2.42: restore asmjs support GitOrigin-RevId: 6ab170756d6774d37b98c2d95323555d31fa3d76 --- example/web/tdweb/package.json | 4 +- example/web/tdweb/src/worker.js | 85 +++++++++++++++++------------ example/web/tdweb/webpack.config.js | 3 +- 3 files changed, 54 insertions(+), 38 deletions(-) diff --git a/example/web/tdweb/package.json b/example/web/tdweb/package.json index d2205eb04..7d51ea19f 100644 --- a/example/web/tdweb/package.json +++ b/example/web/tdweb/package.json @@ -1,6 +1,6 @@ { "name": "@arseny30/tdweb", - "version": "0.2.41", + "version": "0.2.42", "description": "Javascript interface for TDLib (telegram library)", "main": "dist/tdweb.js", "files": [ @@ -8,7 +8,7 @@ ], "scripts": { "precommit": "lint-staged", - "build": "node --max_old_space_size=8192 node_modules/.bin/webpack ", + "build": "webpack", "start": "webpack-dev-server --open" }, "keywords": [ diff --git a/example/web/tdweb/src/worker.js b/example/web/tdweb/src/worker.js index 638d64662..6be881e40 100644 --- a/example/web/tdweb/src/worker.js +++ b/example/web/tdweb/src/worker.js @@ -7,10 +7,8 @@ import { import td_wasm_release from './prebuilt/release/td_wasm.wasm'; // Uncomment for asmjs support -//import td_asmjs_mem_release from './prebuilt/release/td_asmjs.js.mem'; +import td_asmjs_mem_release from './prebuilt/release/td_asmjs.js.mem'; -import { detect } from 'detect-browser'; -const browser = detect(); const tdlibVersion = 6; const localForageDrivers = [localforage.INDEXEDDB, localforage.LOCALSTORAGE, 'memoryDriver']; @@ -64,6 +62,7 @@ async function initLocalForage() { } async function loadTdLibWasm(onFS) { + console.log('loadTdLibWasm'); let Module = await import('./prebuilt/release/td_wasm.js'); log.info('got td_wasm.js'); let td_wasm = td_wasm_release; @@ -95,37 +94,56 @@ async function loadTdLibWasm(onFS) { } // Uncomment for asmjs support -//async function loadTdLibAsmjs() { - //let Module = await import('./prebuilt/release/td_asmjs.js'); - //console.log('got td_wasm.js'); - //let fromFile = 'td_asmjs.js.mem'; - //let toFile = td_asmjs_mem_release; - //let TdModule = new Promise((resolve, reject) => - //Module({ - //onRuntimeInitialized: () => { - //console.log('runtime intialized'); - //}, - //locateFile: name => { - //if (name === fromFile) { - //return toFile; - //} - //return name; - //}, - //ENVIROMENT: 'WORKER' - //}).then(m => { - //delete m.then; - //resolve(m); - //}) - //); +async function loadTdLibAsmjs(onFS) { + console.log('loadTdLibAsmjs'); + let Module = await import('./prebuilt/release/td_asmjs.js'); + console.log('got td_asm.js'); + let fromFile = 'td_asmjs.js.mem'; + let toFile = td_asmjs_mem_release; + let module = Module({ + onRuntimeInitialized: () => { + console.log('runtime intialized'); + }, + locateFile: name => { + if (name === fromFile) { + return toFile; + } + return name; + }, + ENVIROMENT: 'WORKER' + }); + onFS(module.FS); + let TdModule = new Promise((resolve, reject) => + module.then(m => { + delete m.then; + resolve(m); + }) + ); - //return TdModule; -//} + return TdModule; +} async function loadTdLib(mode, onFS) { -// Uncomment for asmjs support - //if (mode === 'asmjs') { - //return loadTdLibAsmjs(); - //} + const wasmSupported = (() => { + try { + if (typeof WebAssembly === "object" + && typeof WebAssembly.instantiate === "function") { + const module = new WebAssembly.Module(Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00)); + if (module instanceof WebAssembly.Module) + return new WebAssembly.Instance(module) instanceof WebAssembly.Instance; + } + } catch (e) { + } + return false; + })(); + if (!wasmSupported) { + log.warning("WebAssembly is not supported, trying to use asmjs"); + mode = 'asmjs'; + } + + if (mode === 'asmjs') { + return loadTdLibAsmjs(onFS); + } return loadTdLibWasm(onFS); } @@ -422,9 +440,6 @@ class TdClient { options = options || {}; let mode = 'wasm'; - if (browser && (browser.name === 'chrome' || browser.name === 'safari')) { - mode = 'asmjs'; - } mode = options.mode || mode; var self = this; @@ -521,7 +536,7 @@ class TdClient { name: 'ignore_background_updates', value: { '@type': 'optionValueBoolean', - value: !this.noDb + value: this.noDb } }); diff --git a/example/web/tdweb/webpack.config.js b/example/web/tdweb/webpack.config.js index 01817d441..708f074e5 100644 --- a/example/web/tdweb/webpack.config.js +++ b/example/web/tdweb/webpack.config.js @@ -16,11 +16,12 @@ module.exports = { contentBase: './dist' }, plugins: [ - new HtmlWebpackPlugin(), + // new HtmlWebpackPlugin(), new CleanWebpackPlugin(['dist'], {}) //, new UglifyJSPlugin() ], module: { + noParse: /td_asmjs\.js$/, rules: [ { test: /\.(js|jsx)$/,