Skip to content

Commit

Permalink
feat: update wasm pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
erwanvivien committed Oct 9, 2023
1 parent 0dd45f4 commit 06f23fd
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 88 deletions.
18 changes: 11 additions & 7 deletions pkg/fast_qr.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function qr(content: string): Uint8Array;
*/
export function qr_svg(content: string, options: SvgOptions): string;
/**
* Different possible Shapes to represent modules in a QRCode
* Different possible Shapes to represent modules in a [`crate::QRCode`]
*/
export enum Shape {
/**
Expand Down Expand Up @@ -128,7 +128,6 @@ export class SvgOptions {
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;

export interface InitOutput {
readonly memory: WebAssembly.Memory;
readonly qr: (a: number, b: number, c: number) => void;
readonly __wbg_svgoptions_free: (a: number) => void;
readonly svgoptions_shape: (a: number, b: number) => number;
Expand All @@ -142,10 +141,13 @@ export interface InitOutput {
readonly svgoptions_image_position: (a: number, b: number, c: number) => number;
readonly svgoptions_new: () => number;
readonly qr_svg: (a: number, b: number, c: number, d: number) => void;
readonly memory: WebAssembly.Memory;
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
readonly __wbindgen_malloc: (a: number) => number;
readonly __wbindgen_realloc: (a: number, b: number, c: number) => number;
readonly __wbindgen_free: (a: number, b: number) => void;
readonly __wbindgen_malloc: (a: number, b: number) => number;
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
readonly __wbindgen_thread_destroy: (a: number, b: number) => void;
readonly __wbindgen_start: () => void;
}

export type SyncInitInput = BufferSource | WebAssembly.Module;
Expand All @@ -154,17 +156,19 @@ export type SyncInitInput = BufferSource | WebAssembly.Module;
* a precompiled `WebAssembly.Module`.
*
* @param {SyncInitInput} module
* @param {WebAssembly.Memory} maybe_memory
*
* @returns {InitOutput}
*/
export function initSync(module: SyncInitInput): InitOutput;
export function initSync(module: SyncInitInput, maybe_memory?: WebAssembly.Memory): InitOutput;

/**
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
* for everything else, calls `WebAssembly.instantiate` directly.
*
* @param {InitInput | Promise<InitInput>} module_or_path
* @param {WebAssembly.Memory} maybe_memory
*
* @returns {Promise<InitOutput>}
*/
export default function init (module_or_path?: InitInput | Promise<InitInput>): Promise<InitOutput>;
export default function __wbg_init (module_or_path?: InitInput | Promise<InitInput>, maybe_memory?: WebAssembly.Memory): Promise<InitOutput>;
104 changes: 55 additions & 49 deletions pkg/fast_qr.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,48 @@
let wasm;

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

cachedTextDecoder.decode();
if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); };

let cachedUint8Memory0 = new Uint8Array();
let cachedUint8Memory0 = null;

function getUint8Memory0() {
if (cachedUint8Memory0.byteLength === 0) {
if (cachedUint8Memory0 === null || cachedUint8Memory0.buffer !== wasm.memory.buffer) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8Memory0;
}

function getStringFromWasm0(ptr, len) {
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8Memory0().slice(ptr, ptr + len));
}

let WASM_VECTOR_LEN = 0;

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

const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
? function (arg, view) {
return cachedTextEncoder.encodeInto(arg, view);
}
: function (arg, view) {
const encodeString = function (arg, view) {
const buf = cachedTextEncoder.encode(arg);
view.set(buf);
return {
read: arg.length,
written: buf.length
};
});
};

function passStringToWasm0(arg, malloc, realloc) {

if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length);
const ptr = malloc(buf.length, 1) >>> 0;
getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
WASM_VECTOR_LEN = buf.length;
return ptr;
}

let len = arg.length;
let ptr = malloc(len);
let ptr = malloc(len, 1) >>> 0;

const mem = getUint8Memory0();

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

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

let cachedInt32Memory0 = new Int32Array();
let cachedInt32Memory0 = null;

function getInt32Memory0() {
if (cachedInt32Memory0.byteLength === 0) {
if (cachedInt32Memory0 === null || cachedInt32Memory0.buffer !== wasm.memory.buffer) {
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachedInt32Memory0;
}

function getArrayU8FromWasm0(ptr, len) {
ptr = ptr >>> 0;
return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
}
/**
Expand All @@ -97,25 +95,25 @@ export function qr(content) {
wasm.qr(retptr, ptr0, len0);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v1 = getArrayU8FromWasm0(r0, r1).slice();
var v2 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
return v1;
return v2;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}

let cachedFloat64Memory0 = new Float64Array();
let cachedFloat64Memory0 = null;

function getFloat64Memory0() {
if (cachedFloat64Memory0.byteLength === 0) {
if (cachedFloat64Memory0 === null || cachedFloat64Memory0.buffer !== wasm.memory.buffer) {
cachedFloat64Memory0 = new Float64Array(wasm.memory.buffer);
}
return cachedFloat64Memory0;
}

function passArrayF64ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 8);
const ptr = malloc(arg.length * 8, 8) >>> 0;
getFloat64Memory0().set(arg, ptr / 8);
WASM_VECTOR_LEN = arg.length;
return ptr;
Expand All @@ -134,25 +132,28 @@ function _assertClass(instance, klass) {
* @returns {string}
*/
export function qr_svg(content, options) {
let deferred3_0;
let deferred3_1;
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
_assertClass(options, SvgOptions);
var ptr1 = options.ptr;
options.ptr = 0;
var ptr1 = options.__destroy_into_raw();
wasm.qr_svg(retptr, ptr0, len0, ptr1);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
deferred3_0 = r0;
deferred3_1 = r1;
return getStringFromWasm0(r0, r1);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
wasm.__wbindgen_free(r0, r1);
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
}
}

/**
* Different possible Shapes to represent modules in a QRCode
* Different possible Shapes to represent modules in a [`crate::QRCode`]
*/
export const Shape = Object.freeze({
/**
Expand Down Expand Up @@ -201,15 +202,16 @@ RoundedSquare:2,"2":"RoundedSquare", });
export class SvgOptions {

static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(SvgOptions.prototype);
obj.ptr = ptr;
obj.__wbg_ptr = ptr;

return obj;
}

__destroy_into_raw() {
const ptr = this.ptr;
this.ptr = 0;
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;

return ptr;
}
Expand Down Expand Up @@ -328,7 +330,7 @@ export class SvgOptions {
}
}

async function load(module, imports) {
async function __wbg_load(module, imports) {
if (typeof Response === 'function' && module instanceof Response) {
if (typeof WebAssembly.instantiateStreaming === 'function') {
try {
Expand Down Expand Up @@ -359,7 +361,7 @@ async function load(module, imports) {
}
}

function getImports() {
function __wbg_get_imports() {
const imports = {};
imports.wbg = {};
imports.wbg.__wbindgen_throw = function(arg0, arg1) {
Expand All @@ -369,51 +371,55 @@ function getImports() {
return imports;
}

function initMemory(imports, maybe_memory) {

function __wbg_init_memory(imports, maybe_memory) {
imports.wbg.memory = maybe_memory || new WebAssembly.Memory({initial:18,maximum:65536,shared:true});
}

function finalizeInit(instance, module) {
function __wbg_finalize_init(instance, module) {
wasm = instance.exports;
init.__wbindgen_wasm_module = module;
cachedFloat64Memory0 = new Float64Array();
cachedInt32Memory0 = new Int32Array();
cachedUint8Memory0 = new Uint8Array();

__wbg_init.__wbindgen_wasm_module = module;
cachedFloat64Memory0 = null;
cachedInt32Memory0 = null;
cachedUint8Memory0 = null;

wasm.__wbindgen_start();
return wasm;
}

function initSync(module) {
const imports = getImports();
function initSync(module, maybe_memory) {
if (wasm !== undefined) return wasm;

initMemory(imports);
const imports = __wbg_get_imports();

__wbg_init_memory(imports, maybe_memory);

if (!(module instanceof WebAssembly.Module)) {
module = new WebAssembly.Module(module);
}

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

return finalizeInit(instance, module);
return __wbg_finalize_init(instance, module);
}

async function init(input) {
async function __wbg_init(input, maybe_memory) {
if (wasm !== undefined) return wasm;

if (typeof input === 'undefined') {
input = new URL('fast_qr_bg.wasm', import.meta.url);
}
const imports = getImports();
const imports = __wbg_get_imports();

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

initMemory(imports);
__wbg_init_memory(imports, maybe_memory);

const { instance, module } = await load(await input, imports);
const { instance, module } = await __wbg_load(await input, imports);

return finalizeInit(instance, module);
return __wbg_finalize_init(instance, module);
}

export { initSync }
export default init;
export default __wbg_init;
10 changes: 6 additions & 4 deletions pkg/fast_qr_bg.wasm.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* tslint:disable */
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function qr(a: number, b: number, c: number): void;
export function __wbg_svgoptions_free(a: number): void;
export function svgoptions_shape(a: number, b: number): number;
Expand All @@ -14,7 +13,10 @@ export function svgoptions_image_size(a: number, b: number, c: number): number;
export function svgoptions_image_position(a: number, b: number, c: number): number;
export function svgoptions_new(): number;
export function qr_svg(a: number, b: number, c: number, d: number): void;
export const memory: WebAssembly.Memory;
export function __wbindgen_add_to_stack_pointer(a: number): number;
export function __wbindgen_malloc(a: number): number;
export function __wbindgen_realloc(a: number, b: number, c: number): number;
export function __wbindgen_free(a: number, b: number): void;
export function __wbindgen_malloc(a: number, b: number): number;
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number;
export function __wbindgen_free(a: number, b: number, c: number): void;
export function __wbindgen_thread_destroy(a: number, b: number): void;
export function __wbindgen_start(): void;
23 changes: 23 additions & 0 deletions wasm-pack.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
$CARGO_MODE="--release"
$TARGET_PATH="release"
$BUILD_STD_FEATURES="panic_immediate_abort"

Write-Host "Building with cargo mode: ${CARGO_MODE}"

$OUTPUT_DIR="pkg"

cargo +nightly build ${CARGO_MODE} `
--target wasm32-unknown-unknown `
-Z "build-std=std,panic_abort" `
-Z "build-std-features=${BUILD_STD_FEATURES}" `
--features svg,wasm-bindgen

& wasm-bindgen --out-dir "${OUTPUT_DIR}" `
--web target/wasm32-unknown-unknown/${TARGET_PATH}/fast_qr.wasm

& wasm-opt `
-O2 `
"${OUTPUT_DIR}/fast_qr_bg.wasm" `
-o "${OUTPUT_DIR}/fast_qr_bg.wasm"

& Write-Host "Done!"
Loading

0 comments on commit 06f23fd

Please sign in to comment.