Skip to content

Commit 06f23fd

Browse files
committed
feat: update wasm pkg
1 parent 0dd45f4 commit 06f23fd

File tree

5 files changed

+114
-88
lines changed

5 files changed

+114
-88
lines changed

pkg/fast_qr.d.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function qr(content: string): Uint8Array;
1414
*/
1515
export function qr_svg(content: string, options: SvgOptions): string;
1616
/**
17-
* Different possible Shapes to represent modules in a QRCode
17+
* Different possible Shapes to represent modules in a [`crate::QRCode`]
1818
*/
1919
export enum Shape {
2020
/**
@@ -128,7 +128,6 @@ export class SvgOptions {
128128
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
129129

130130
export interface InitOutput {
131-
readonly memory: WebAssembly.Memory;
132131
readonly qr: (a: number, b: number, c: number) => void;
133132
readonly __wbg_svgoptions_free: (a: number) => void;
134133
readonly svgoptions_shape: (a: number, b: number) => number;
@@ -142,10 +141,13 @@ export interface InitOutput {
142141
readonly svgoptions_image_position: (a: number, b: number, c: number) => number;
143142
readonly svgoptions_new: () => number;
144143
readonly qr_svg: (a: number, b: number, c: number, d: number) => void;
144+
readonly memory: WebAssembly.Memory;
145145
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
146-
readonly __wbindgen_malloc: (a: number) => number;
147-
readonly __wbindgen_realloc: (a: number, b: number, c: number) => number;
148-
readonly __wbindgen_free: (a: number, b: number) => void;
146+
readonly __wbindgen_malloc: (a: number, b: number) => number;
147+
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
148+
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
149+
readonly __wbindgen_thread_destroy: (a: number, b: number) => void;
150+
readonly __wbindgen_start: () => void;
149151
}
150152

151153
export type SyncInitInput = BufferSource | WebAssembly.Module;
@@ -154,17 +156,19 @@ export type SyncInitInput = BufferSource | WebAssembly.Module;
154156
* a precompiled `WebAssembly.Module`.
155157
*
156158
* @param {SyncInitInput} module
159+
* @param {WebAssembly.Memory} maybe_memory
157160
*
158161
* @returns {InitOutput}
159162
*/
160-
export function initSync(module: SyncInitInput): InitOutput;
163+
export function initSync(module: SyncInitInput, maybe_memory?: WebAssembly.Memory): InitOutput;
161164

162165
/**
163166
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
164167
* for everything else, calls `WebAssembly.instantiate` directly.
165168
*
166169
* @param {InitInput | Promise<InitInput>} module_or_path
170+
* @param {WebAssembly.Memory} maybe_memory
167171
*
168172
* @returns {Promise<InitOutput>}
169173
*/
170-
export default function init (module_or_path?: InitInput | Promise<InitInput>): Promise<InitOutput>;
174+
export default function __wbg_init (module_or_path?: InitInput | Promise<InitInput>, maybe_memory?: WebAssembly.Memory): Promise<InitOutput>;

pkg/fast_qr.js

+55-49
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,48 @@
11
let wasm;
22

3-
const cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
3+
const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } );
44

5-
cachedTextDecoder.decode();
5+
if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); };
66

7-
let cachedUint8Memory0 = new Uint8Array();
7+
let cachedUint8Memory0 = null;
88

99
function getUint8Memory0() {
10-
if (cachedUint8Memory0.byteLength === 0) {
10+
if (cachedUint8Memory0 === null || cachedUint8Memory0.buffer !== wasm.memory.buffer) {
1111
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
1212
}
1313
return cachedUint8Memory0;
1414
}
1515

1616
function getStringFromWasm0(ptr, len) {
17-
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
17+
ptr = ptr >>> 0;
18+
return cachedTextDecoder.decode(getUint8Memory0().slice(ptr, ptr + len));
1819
}
1920

2021
let WASM_VECTOR_LEN = 0;
2122

22-
const cachedTextEncoder = new TextEncoder('utf-8');
23+
const cachedTextEncoder = (typeof TextEncoder !== 'undefined' ? new TextEncoder('utf-8') : { encode: () => { throw Error('TextEncoder not available') } } );
2324

24-
const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
25-
? function (arg, view) {
26-
return cachedTextEncoder.encodeInto(arg, view);
27-
}
28-
: function (arg, view) {
25+
const encodeString = function (arg, view) {
2926
const buf = cachedTextEncoder.encode(arg);
3027
view.set(buf);
3128
return {
3229
read: arg.length,
3330
written: buf.length
3431
};
35-
});
32+
};
3633

3734
function passStringToWasm0(arg, malloc, realloc) {
3835

3936
if (realloc === undefined) {
4037
const buf = cachedTextEncoder.encode(arg);
41-
const ptr = malloc(buf.length);
38+
const ptr = malloc(buf.length, 1) >>> 0;
4239
getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
4340
WASM_VECTOR_LEN = buf.length;
4441
return ptr;
4542
}
4643

4744
let len = arg.length;
48-
let ptr = malloc(len);
45+
let ptr = malloc(len, 1) >>> 0;
4946

5047
const mem = getUint8Memory0();
5148

@@ -61,7 +58,7 @@ function passStringToWasm0(arg, malloc, realloc) {
6158
if (offset !== 0) {
6259
arg = arg.slice(offset);
6360
}
64-
ptr = realloc(ptr, len, len = offset + arg.length * 3);
61+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
6562
const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
6663
const ret = encodeString(arg, view);
6764

@@ -72,16 +69,17 @@ function passStringToWasm0(arg, malloc, realloc) {
7269
return ptr;
7370
}
7471

75-
let cachedInt32Memory0 = new Int32Array();
72+
let cachedInt32Memory0 = null;
7673

7774
function getInt32Memory0() {
78-
if (cachedInt32Memory0.byteLength === 0) {
75+
if (cachedInt32Memory0 === null || cachedInt32Memory0.buffer !== wasm.memory.buffer) {
7976
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
8077
}
8178
return cachedInt32Memory0;
8279
}
8380

8481
function getArrayU8FromWasm0(ptr, len) {
82+
ptr = ptr >>> 0;
8583
return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
8684
}
8785
/**
@@ -97,25 +95,25 @@ export function qr(content) {
9795
wasm.qr(retptr, ptr0, len0);
9896
var r0 = getInt32Memory0()[retptr / 4 + 0];
9997
var r1 = getInt32Memory0()[retptr / 4 + 1];
100-
var v1 = getArrayU8FromWasm0(r0, r1).slice();
98+
var v2 = getArrayU8FromWasm0(r0, r1).slice();
10199
wasm.__wbindgen_free(r0, r1 * 1);
102-
return v1;
100+
return v2;
103101
} finally {
104102
wasm.__wbindgen_add_to_stack_pointer(16);
105103
}
106104
}
107105

108-
let cachedFloat64Memory0 = new Float64Array();
106+
let cachedFloat64Memory0 = null;
109107

110108
function getFloat64Memory0() {
111-
if (cachedFloat64Memory0.byteLength === 0) {
109+
if (cachedFloat64Memory0 === null || cachedFloat64Memory0.buffer !== wasm.memory.buffer) {
112110
cachedFloat64Memory0 = new Float64Array(wasm.memory.buffer);
113111
}
114112
return cachedFloat64Memory0;
115113
}
116114

117115
function passArrayF64ToWasm0(arg, malloc) {
118-
const ptr = malloc(arg.length * 8);
116+
const ptr = malloc(arg.length * 8, 8) >>> 0;
119117
getFloat64Memory0().set(arg, ptr / 8);
120118
WASM_VECTOR_LEN = arg.length;
121119
return ptr;
@@ -134,25 +132,28 @@ function _assertClass(instance, klass) {
134132
* @returns {string}
135133
*/
136134
export function qr_svg(content, options) {
135+
let deferred3_0;
136+
let deferred3_1;
137137
try {
138138
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
139139
const ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
140140
const len0 = WASM_VECTOR_LEN;
141141
_assertClass(options, SvgOptions);
142-
var ptr1 = options.ptr;
143-
options.ptr = 0;
142+
var ptr1 = options.__destroy_into_raw();
144143
wasm.qr_svg(retptr, ptr0, len0, ptr1);
145144
var r0 = getInt32Memory0()[retptr / 4 + 0];
146145
var r1 = getInt32Memory0()[retptr / 4 + 1];
146+
deferred3_0 = r0;
147+
deferred3_1 = r1;
147148
return getStringFromWasm0(r0, r1);
148149
} finally {
149150
wasm.__wbindgen_add_to_stack_pointer(16);
150-
wasm.__wbindgen_free(r0, r1);
151+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
151152
}
152153
}
153154

154155
/**
155-
* Different possible Shapes to represent modules in a QRCode
156+
* Different possible Shapes to represent modules in a [`crate::QRCode`]
156157
*/
157158
export const Shape = Object.freeze({
158159
/**
@@ -201,15 +202,16 @@ RoundedSquare:2,"2":"RoundedSquare", });
201202
export class SvgOptions {
202203

203204
static __wrap(ptr) {
205+
ptr = ptr >>> 0;
204206
const obj = Object.create(SvgOptions.prototype);
205-
obj.ptr = ptr;
207+
obj.__wbg_ptr = ptr;
206208

207209
return obj;
208210
}
209211

210212
__destroy_into_raw() {
211-
const ptr = this.ptr;
212-
this.ptr = 0;
213+
const ptr = this.__wbg_ptr;
214+
this.__wbg_ptr = 0;
213215

214216
return ptr;
215217
}
@@ -328,7 +330,7 @@ export class SvgOptions {
328330
}
329331
}
330332

331-
async function load(module, imports) {
333+
async function __wbg_load(module, imports) {
332334
if (typeof Response === 'function' && module instanceof Response) {
333335
if (typeof WebAssembly.instantiateStreaming === 'function') {
334336
try {
@@ -359,7 +361,7 @@ async function load(module, imports) {
359361
}
360362
}
361363

362-
function getImports() {
364+
function __wbg_get_imports() {
363365
const imports = {};
364366
imports.wbg = {};
365367
imports.wbg.__wbindgen_throw = function(arg0, arg1) {
@@ -369,51 +371,55 @@ function getImports() {
369371
return imports;
370372
}
371373

372-
function initMemory(imports, maybe_memory) {
373-
374+
function __wbg_init_memory(imports, maybe_memory) {
375+
imports.wbg.memory = maybe_memory || new WebAssembly.Memory({initial:18,maximum:65536,shared:true});
374376
}
375377

376-
function finalizeInit(instance, module) {
378+
function __wbg_finalize_init(instance, module) {
377379
wasm = instance.exports;
378-
init.__wbindgen_wasm_module = module;
379-
cachedFloat64Memory0 = new Float64Array();
380-
cachedInt32Memory0 = new Int32Array();
381-
cachedUint8Memory0 = new Uint8Array();
382-
380+
__wbg_init.__wbindgen_wasm_module = module;
381+
cachedFloat64Memory0 = null;
382+
cachedInt32Memory0 = null;
383+
cachedUint8Memory0 = null;
383384

385+
wasm.__wbindgen_start();
384386
return wasm;
385387
}
386388

387-
function initSync(module) {
388-
const imports = getImports();
389+
function initSync(module, maybe_memory) {
390+
if (wasm !== undefined) return wasm;
389391

390-
initMemory(imports);
392+
const imports = __wbg_get_imports();
393+
394+
__wbg_init_memory(imports, maybe_memory);
391395

392396
if (!(module instanceof WebAssembly.Module)) {
393397
module = new WebAssembly.Module(module);
394398
}
395399

396400
const instance = new WebAssembly.Instance(module, imports);
397401

398-
return finalizeInit(instance, module);
402+
return __wbg_finalize_init(instance, module);
399403
}
400404

401-
async function init(input) {
405+
async function __wbg_init(input, maybe_memory) {
406+
if (wasm !== undefined) return wasm;
407+
402408
if (typeof input === 'undefined') {
403409
input = new URL('fast_qr_bg.wasm', import.meta.url);
404410
}
405-
const imports = getImports();
411+
const imports = __wbg_get_imports();
406412

407413
if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
408414
input = fetch(input);
409415
}
410416

411-
initMemory(imports);
417+
__wbg_init_memory(imports, maybe_memory);
412418

413-
const { instance, module } = await load(await input, imports);
419+
const { instance, module } = await __wbg_load(await input, imports);
414420

415-
return finalizeInit(instance, module);
421+
return __wbg_finalize_init(instance, module);
416422
}
417423

418424
export { initSync }
419-
export default init;
425+
export default __wbg_init;

pkg/fast_qr_bg.wasm.d.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* tslint:disable */
22
/* eslint-disable */
3-
export const memory: WebAssembly.Memory;
43
export function qr(a: number, b: number, c: number): void;
54
export function __wbg_svgoptions_free(a: number): void;
65
export function svgoptions_shape(a: number, b: number): number;
@@ -14,7 +13,10 @@ export function svgoptions_image_size(a: number, b: number, c: number): number;
1413
export function svgoptions_image_position(a: number, b: number, c: number): number;
1514
export function svgoptions_new(): number;
1615
export function qr_svg(a: number, b: number, c: number, d: number): void;
16+
export const memory: WebAssembly.Memory;
1717
export function __wbindgen_add_to_stack_pointer(a: number): number;
18-
export function __wbindgen_malloc(a: number): number;
19-
export function __wbindgen_realloc(a: number, b: number, c: number): number;
20-
export function __wbindgen_free(a: number, b: number): void;
18+
export function __wbindgen_malloc(a: number, b: number): number;
19+
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number;
20+
export function __wbindgen_free(a: number, b: number, c: number): void;
21+
export function __wbindgen_thread_destroy(a: number, b: number): void;
22+
export function __wbindgen_start(): void;

wasm-pack.ps1

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
$CARGO_MODE="--release"
2+
$TARGET_PATH="release"
3+
$BUILD_STD_FEATURES="panic_immediate_abort"
4+
5+
Write-Host "Building with cargo mode: ${CARGO_MODE}"
6+
7+
$OUTPUT_DIR="pkg"
8+
9+
cargo +nightly build ${CARGO_MODE} `
10+
--target wasm32-unknown-unknown `
11+
-Z "build-std=std,panic_abort" `
12+
-Z "build-std-features=${BUILD_STD_FEATURES}" `
13+
--features svg,wasm-bindgen
14+
15+
& wasm-bindgen --out-dir "${OUTPUT_DIR}" `
16+
--web target/wasm32-unknown-unknown/${TARGET_PATH}/fast_qr.wasm
17+
18+
& wasm-opt `
19+
-O2 `
20+
"${OUTPUT_DIR}/fast_qr_bg.wasm" `
21+
-o "${OUTPUT_DIR}/fast_qr_bg.wasm"
22+
23+
& Write-Host "Done!"

0 commit comments

Comments
 (0)