tdweb:0.2.42: restore asmjs support

GitOrigin-RevId: 6ab170756d6774d37b98c2d95323555d31fa3d76
This commit is contained in:
Arseny Smirnov 2019-04-23 20:21:54 +03:00
parent 58aafd9b39
commit e6b3f9ea98
3 changed files with 54 additions and 38 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@arseny30/tdweb", "name": "@arseny30/tdweb",
"version": "0.2.41", "version": "0.2.42",
"description": "Javascript interface for TDLib (telegram library)", "description": "Javascript interface for TDLib (telegram library)",
"main": "dist/tdweb.js", "main": "dist/tdweb.js",
"files": [ "files": [
@ -8,7 +8,7 @@
], ],
"scripts": { "scripts": {
"precommit": "lint-staged", "precommit": "lint-staged",
"build": "node --max_old_space_size=8192 node_modules/.bin/webpack ", "build": "webpack",
"start": "webpack-dev-server --open" "start": "webpack-dev-server --open"
}, },
"keywords": [ "keywords": [

View File

@ -7,10 +7,8 @@ import {
import td_wasm_release from './prebuilt/release/td_wasm.wasm'; import td_wasm_release from './prebuilt/release/td_wasm.wasm';
// Uncomment for asmjs support // 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 tdlibVersion = 6;
const localForageDrivers = [localforage.INDEXEDDB, localforage.LOCALSTORAGE, 'memoryDriver']; const localForageDrivers = [localforage.INDEXEDDB, localforage.LOCALSTORAGE, 'memoryDriver'];
@ -64,6 +62,7 @@ async function initLocalForage() {
} }
async function loadTdLibWasm(onFS) { async function loadTdLibWasm(onFS) {
console.log('loadTdLibWasm');
let Module = await import('./prebuilt/release/td_wasm.js'); let Module = await import('./prebuilt/release/td_wasm.js');
log.info('got td_wasm.js'); log.info('got td_wasm.js');
let td_wasm = td_wasm_release; let td_wasm = td_wasm_release;
@ -95,37 +94,56 @@ async function loadTdLibWasm(onFS) {
} }
// Uncomment for asmjs support // Uncomment for asmjs support
//async function loadTdLibAsmjs() { async function loadTdLibAsmjs(onFS) {
//let Module = await import('./prebuilt/release/td_asmjs.js'); console.log('loadTdLibAsmjs');
//console.log('got td_wasm.js'); let Module = await import('./prebuilt/release/td_asmjs.js');
//let fromFile = 'td_asmjs.js.mem'; console.log('got td_asm.js');
//let toFile = td_asmjs_mem_release; let fromFile = 'td_asmjs.js.mem';
//let TdModule = new Promise((resolve, reject) => let toFile = td_asmjs_mem_release;
//Module({ let module = Module({
//onRuntimeInitialized: () => { onRuntimeInitialized: () => {
//console.log('runtime intialized'); console.log('runtime intialized');
//}, },
//locateFile: name => { locateFile: name => {
//if (name === fromFile) { if (name === fromFile) {
//return toFile; return toFile;
//} }
//return name; return name;
//}, },
//ENVIROMENT: 'WORKER' ENVIROMENT: 'WORKER'
//}).then(m => { });
//delete m.then; onFS(module.FS);
//resolve(m); let TdModule = new Promise((resolve, reject) =>
//}) module.then(m => {
//); delete m.then;
resolve(m);
})
);
//return TdModule; return TdModule;
//} }
async function loadTdLib(mode, onFS) { async function loadTdLib(mode, onFS) {
// Uncomment for asmjs support const wasmSupported = (() => {
//if (mode === 'asmjs') { try {
//return loadTdLibAsmjs(); 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); return loadTdLibWasm(onFS);
} }
@ -422,9 +440,6 @@ class TdClient {
options = options || {}; options = options || {};
let mode = 'wasm'; let mode = 'wasm';
if (browser && (browser.name === 'chrome' || browser.name === 'safari')) {
mode = 'asmjs';
}
mode = options.mode || mode; mode = options.mode || mode;
var self = this; var self = this;
@ -521,7 +536,7 @@ class TdClient {
name: 'ignore_background_updates', name: 'ignore_background_updates',
value: { value: {
'@type': 'optionValueBoolean', '@type': 'optionValueBoolean',
value: !this.noDb value: this.noDb
} }
}); });

View File

@ -16,11 +16,12 @@ module.exports = {
contentBase: './dist' contentBase: './dist'
}, },
plugins: [ plugins: [
new HtmlWebpackPlugin(), // new HtmlWebpackPlugin(),
new CleanWebpackPlugin(['dist'], {}) new CleanWebpackPlugin(['dist'], {})
//, new UglifyJSPlugin() //, new UglifyJSPlugin()
], ],
module: { module: {
noParse: /td_asmjs\.js$/,
rules: [ rules: [
{ {
test: /\.(js|jsx)$/, test: /\.(js|jsx)$/,