Skip to content

Commit b971be0

Browse files
authored
fix(core): inline .wasm (#243)
Signed-off-by: 0x009922 <[email protected]>
1 parent c01ce40 commit b971be0

File tree

8 files changed

+31
-84
lines changed

8 files changed

+31
-84
lines changed

crypto-wasm/Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crypto-wasm/Cargo.toml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ edition = "2021"
77
[lib]
88
crate-type = ["cdylib", "rlib"]
99

10-
[features]
11-
default = ["console_error_panic_hook"]
12-
1310
[dependencies]
1411
# iroha_crypto = { path = "../.iroha/crates/iroha_crypto", default-features = false, features = ["rand"] }
1512
iroha_crypto = { git = "https://github.com/hyperledger-iroha/iroha.git", rev = "daa2d50fbe288aed1231c6f45c853698435477d6", default-features = false, features = ["rand"] }
@@ -21,18 +18,16 @@ serde = { version = "1", default-features = false, features = ["derive"] }
2118
serde-wasm-bindgen = "0.6"
2219
hex = { version = "0.4", default-features = false, features = ["alloc"] }
2320

24-
# The `console_error_panic_hook` crate provides better debugging of panics by
25-
# logging them with `console.error`. This is great for development, but requires
26-
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
27-
# code size when deploying.
28-
console_error_panic_hook = { version = "0.1", optional = true }
29-
3021
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
3122
# compared to the default allocator's ~10K. It is slower than the default
3223
# allocator, however.
3324
#
3425
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
35-
wee_alloc = { version = "0.4.5", optional = true }
26+
wee_alloc = { version = "0.4.5" }
27+
28+
[profile.release]
29+
opt-level = "z"
30+
lto = true
3631

3732
[dev-dependencies]
3833
wasm-bindgen-test = "0.3.13"

crypto-wasm/src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
extern crate alloc;
77

8+
extern crate wee_alloc;
9+
10+
// Use `wee_alloc` as the global allocator.
11+
#[global_allocator]
12+
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
13+
814
mod utils;
915

1016
use alloc::string::ToString;
@@ -327,8 +333,3 @@ impl TryFrom<Result<(), iroha_crypto::error::Error>> for VerifyResultJs {
327333
Ok(Self { obj: js_value })
328334
}
329335
}
330-
331-
#[wasm_bindgen(start)]
332-
pub fn main_js() {
333-
utils::set_panic_hook();
334-
}

crypto-wasm/src/utils.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,6 @@ use core::fmt::Display;
44

55
use wasm_bindgen::prelude::*;
66

7-
pub fn set_panic_hook() {
8-
// When the `console_error_panic_hook` feature is enabled, we can call the
9-
// `set_panic_hook` function at least once during initialization, and then
10-
// we will get better error messages if our code ever panics.
11-
//
12-
// For more details see
13-
// https://github.com/rustwasm/console_error_panic_hook#readme
14-
#[cfg(feature = "console_error_panic_hook")]
15-
console_error_panic_hook::set_once();
16-
}
17-
187
pub trait JsErrorResultExt<T> {
198
fn wrap_js_error(self) -> Result<T, JsError>;
209
}

etc/task-prep-crypto-wasm.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ const PREP_DIR = resolveFromRoot('prep/crypto-wasm')
1212
console.log(PROJECT_DIR)
1313
const CRATE_NAME = 'iroha_crypto_wasm'
1414

15+
const GENERATED_FILES = new Set([`.d.ts`, '.internal.js', '.js'].map((x) => `${CRATE_NAME}${x}`))
16+
1517
async function buildWasm() {
1618
const outDir = PREP_DIR
1719
console.log(' ' + colors.yellow(`empty ${pathRel(PREP_DIR)}`))
1820
await emptyDir(PREP_DIR)
1921

2022
$.logStep(`Building with @deno/wasmbuild...`)
21-
await $`deno run -A jsr:@deno/wasmbuild --out ${outDir}`.cwd(PROJECT_DIR)
23+
await $`deno run -A jsr:@deno/wasmbuild@0.19.1 --out ${outDir} --inline`.cwd(PROJECT_DIR)
2224
console.log(` ${colors.yellow(`write ${pathRel(outDir)}`)}`)
2325
}
2426

@@ -29,27 +31,39 @@ async function checkBuildReady() {
2931
assert(i.isFile)
3032
files.add(i.name)
3133
}
32-
assertEquals(files, new Set([`.d.ts`, '.internal.js', '.js', '.wasm'].map((x) => `${CRATE_NAME}${x}`)))
34+
assertEquals(files, GENERATED_FILES)
3335
return true
3436
} catch (err) {
3537
$.logWarn('Error whiule checking build artifacts:', err)
3638
return false
3739
}
3840
}
3941

42+
function patchWasmJsWrapCode(code: string): string {
43+
return code.replace(/(__wbg_set_wasm\(wasm.exports\))/, '$1\nwasm.exports.__wbindgen_start()')
44+
}
45+
46+
async function patchWasmJsWrap(file: string) {
47+
const content = await Deno.readTextFile(file)
48+
const patched = patchWasmJsWrapCode(content)
49+
await Deno.writeTextFile(file, patched)
50+
}
51+
4052
async function copyOutputs() {
4153
const targetDir = resolveFromRoot('packages/core/crypto/wasm')
42-
const files = [`${CRATE_NAME}.d.ts`, `${CRATE_NAME}.wasm`, `${CRATE_NAME}.internal.js`]
4354

4455
console.log(' ' + colors.yellow(`empty ${pathRel(targetDir)}`))
4556
await emptyDir(targetDir)
4657

47-
for (const i of files) {
58+
for (const i of GENERATED_FILES) {
4859
const src = resolveFromRoot(PREP_DIR, i)
4960
const dest = path.join(targetDir, i)
5061
console.log(` ${colors.yellow(`write ${pathRel(dest)}`)}`)
5162
await copy(src, dest)
5263
}
64+
65+
// https://github.com/denoland/wasmbuild/issues/156
66+
await patchWasmJsWrap(path.join(targetDir, `${CRATE_NAME}.js`))
5367
}
5468

5569
const args = parseArgs(Deno.args, {

packages/core/crypto/mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import { enumCodec, GenCodec, structCodec } from '../codec.ts'
5858
import * as scale from '@scale-codec/core'
5959
import { assert } from '@std/assert/assert'
6060
import { BytesVec } from '../data-model/primitives.ts'
61-
import * as wasm from './wasm.js'
61+
import * as wasm from './wasm/iroha_crypto_wasm.js'
6262

6363
export { Bytes }
6464
export type VerifyResult = wasm.VerifyResult

packages/core/crypto/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { decodeHex, encodeHex } from '@std/encoding/hex'
2-
import type { Bytes as WasmBytes } from './wasm.js'
2+
import type { Bytes as WasmBytes } from './wasm/iroha_crypto_wasm.js'
33

44
/**
55
* Helper to work with binary data passed into the WASM

packages/core/crypto/wasm.js

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)