From 38a98f1846a5c714d50babcce29650b3a828b983 Mon Sep 17 00:00:00 2001 From: spacemeowx2 Date: Mon, 11 Jan 2021 23:11:24 +0800 Subject: [PATCH] add init_blflash --- libblflash/Cargo.toml | 3 +++ libblflash/index.js | 6 +++--- libblflash/src/lib.rs | 7 ++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libblflash/Cargo.toml b/libblflash/Cargo.toml index 629df9a..91d54d7 100644 --- a/libblflash/Cargo.toml +++ b/libblflash/Cargo.toml @@ -14,3 +14,6 @@ wasm-bindgen = { version = "0.2", features = ["serde-serialize"] } wasm-bindgen-futures = "0.4" console_error_panic_hook = "0.1" wasm-logger = "0.2" + +[package.metadata.wasm-pack.profile.release] +wasm-opt = ["-Oz", "--enable-mutable-globals"] diff --git a/libblflash/index.js b/libblflash/index.js index 2fa5eed..dea6cbb 100644 --- a/libblflash/index.js +++ b/libblflash/index.js @@ -1,4 +1,4 @@ -import init, { dump, flash, FS } from './pkg/libblflash.js' +import init, { init_blflash, dump, flash, FS } from './pkg/libblflash.js' async function onDump() { try { @@ -11,14 +11,13 @@ async function onDump() { end: 0x100000, }) console.log('done') - debugger const result = FS.read_file('output.bin') console.log(result) } catch(e) { console.error('error during Dump', e) } } -async function onFlash(event) { +async function onFlash() { try { const file = document.getElementById('file').files[0] const content = await new Response(file).arrayBuffer() @@ -39,6 +38,7 @@ async function main() { console.log('load wasm') await init() console.log('wasm loaded') + init_blflash() document.getElementById('dump').addEventListener('click', onDump) document.getElementById('flash').addEventListener('click', onFlash) } diff --git a/libblflash/src/lib.rs b/libblflash/src/lib.rs index 0e94a1c..def64d1 100644 --- a/libblflash/src/lib.rs +++ b/libblflash/src/lib.rs @@ -3,15 +3,14 @@ use wasm_bindgen::prelude::*; use std::path::PathBuf; mod utils; -fn init() { +#[wasm_bindgen] +pub fn init_blflash() { utils::set_panic_hook(); wasm_logger::init(wasm_logger::Config::new(log::Level::Trace)); } #[wasm_bindgen] pub async fn flash(opt: JsValue) -> Result<(), JsValue> { - init(); - let opt: FlashOpt = opt.into_serde().map_err(|e| e.to_string())?; blflash::flash(opt).await.map_err(|e| e.to_string())?; Ok(()) @@ -19,8 +18,6 @@ pub async fn flash(opt: JsValue) -> Result<(), JsValue> { #[wasm_bindgen] pub async fn dump(opt: JsValue) -> Result<(), JsValue> { - init(); - let opt: DumpOpt = opt.into_serde().map_err(|e| e.to_string())?; blflash::dump(opt).await.map_err(|e| e.to_string())?; Ok(())