Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to wasm-bindgen #474

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 0 additions & 23 deletions lib/read-wasm-browser.js

This file was deleted.

27 changes: 0 additions & 27 deletions lib/read-wasm.js

This file was deleted.

1 change: 0 additions & 1 deletion lib/source-map-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const util = require("./util");
const binarySearch = require("./binary-search");
const ArraySet = require("./array-set").ArraySet;
const base64VLQ = require("./base64-vlq"); // eslint-disable-line no-unused-vars
const readWasm = require("../lib/read-wasm");
const wasm = require("./wasm");

const INTERNAL = Symbol("smcInternal");
Expand Down
199 changes: 78 additions & 121 deletions lib/wasm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
const readWasm = require("../lib/read-wasm");
let cachedWasm = null;

module.exports = async function wasm() {
if (cachedWasm) {
return cachedWasm;
}

let wasmExports;

// Once we support Es Modules is Workers in Firefox,
// we can migrate all modules from CommonJS to ES Modules, especially the current module
// and import the wasm mjs from the top level and no longar have to be async here!
//
// => This mean that we will no longer require to be async in this library, at least on nodejs
const wasm = await import("../wasm-mappings/source-map-mappings-wasm-api/source_map_mappings_wasm_api.mjs");
if (true /* IS NODE */) {
const path = require('path').join(__dirname, '../wasm-mappings/source-map-mappings-wasm-api/source_map_mappings_wasm_api_bg.wasm');
const bytes = require('fs').readFileSync(path);
const wasmModule = new WebAssembly.Module(bytes);
wasmExports = wasm.initSync(wasmModule);
} else { /* IS BROWSER */
wasmExports = await wasm.default("../wasm-mappings/source-map-mappings-wasm-api/source_map_mappings_wasm_api.wasm");
}

cachedWasm = {
exports: wasmExports,
withMappingCallback,
};
return cachedWasm;
};

/**
* Provide the JIT with a nice shape / hidden class.
Expand All @@ -13,126 +42,54 @@ function Mapping() {
this.name = null;
}

let cachedWasm = null;
const callbackStack = [];

module.exports = function wasm() {
if (cachedWasm) {
return cachedWasm;
function withMappingCallback(mappingCallback, f) {
callbackStack.push(mappingCallback);
try {
f();
} finally {
callbackStack.pop();
}
}

const callbackStack = [];

cachedWasm = readWasm()
.then(buffer => {
return WebAssembly.instantiate(buffer, {
env: {
mapping_callback(
generatedLine,
generatedColumn,

hasLastGeneratedColumn,
lastGeneratedColumn,

hasOriginal,
source,
originalLine,
originalColumn,

hasName,
name
) {
const mapping = new Mapping();
// JS uses 1-based line numbers, wasm uses 0-based.
mapping.generatedLine = generatedLine + 1;
mapping.generatedColumn = generatedColumn;

if (hasLastGeneratedColumn) {
// JS uses inclusive last generated column, wasm uses exclusive.
mapping.lastGeneratedColumn = lastGeneratedColumn - 1;
}

if (hasOriginal) {
mapping.source = source;
// JS uses 1-based line numbers, wasm uses 0-based.
mapping.originalLine = originalLine + 1;
mapping.originalColumn = originalColumn;

if (hasName) {
mapping.name = name;
}
}

callbackStack[callbackStack.length - 1](mapping);
},

start_all_generated_locations_for() {
console.time("all_generated_locations_for");
},
end_all_generated_locations_for() {
console.timeEnd("all_generated_locations_for");
},

start_compute_column_spans() {
console.time("compute_column_spans");
},
end_compute_column_spans() {
console.timeEnd("compute_column_spans");
},

start_generated_location_for() {
console.time("generated_location_for");
},
end_generated_location_for() {
console.timeEnd("generated_location_for");
},

start_original_location_for() {
console.time("original_location_for");
},
end_original_location_for() {
console.timeEnd("original_location_for");
},

start_parse_mappings() {
console.time("parse_mappings");
},
end_parse_mappings() {
console.timeEnd("parse_mappings");
},

start_sort_by_generated_location() {
console.time("sort_by_generated_location");
},
end_sort_by_generated_location() {
console.timeEnd("sort_by_generated_location");
},

start_sort_by_original_location() {
console.time("sort_by_original_location");
},
end_sort_by_original_location() {
console.timeEnd("sort_by_original_location");
},
},
});
})
.then(Wasm => {
return {
exports: Wasm.instance.exports,
withMappingCallback: (mappingCallback, f) => {
callbackStack.push(mappingCallback);
try {
f();
} finally {
callbackStack.pop();
}
},
};
})
.then(null, e => {
cachedWasm = null;
throw e;
});

return cachedWasm;
};
// Expose this method to WASM/Rust
module.exports.mapping_callback =
(
generatedLine,
generatedColumn,

hasLastGeneratedColumn,
lastGeneratedColumn,

hasOriginal,
source,
originalLine,
originalColumn,

hasName,
name
) => {
const mapping = new Mapping();
// JS uses 1-based line numbers, wasm uses 0-based.
mapping.generatedLine = generatedLine + 1;
mapping.generatedColumn = generatedColumn;

if (hasLastGeneratedColumn) {
// JS uses inclusive last generated column, wasm uses exclusive.
mapping.lastGeneratedColumn = lastGeneratedColumn - 1;
}

if (hasOriginal) {
mapping.source = source;
// JS uses 1-based line numbers, wasm uses 0-based.
mapping.originalLine = originalLine + 1;
mapping.originalColumn = originalColumn;

if (hasName) {
mapping.name = name;
}
}

callbackStack[callbackStack.length - 1](mapping);
};
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
"main": "./source-map.js",
"types": "./source-map.d.ts",
"browser": {
"./lib/url.js": "./lib/url-browser.js",
"./lib/read-wasm.js": "./lib/read-wasm-browser.js"
"./lib/url.js": "./lib/url-browser.js"
},
"files": [
"source-map.js",
Expand Down
3 changes: 2 additions & 1 deletion wasm-mappings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ repository = "https://github.com/mozilla/source-map"
version = "0.5.0"

[dependencies]
rand = "0.4.1"
rand = "0.8.5"
getrandom = { version = "0.2.8", features = ["js"] }
vlq = "0.5.1"

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions wasm-mappings/source-map-mappings-wasm-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ version = "0.5.0"

[dependencies]
source-map-mappings = { version = "0.5.0", path = ".." }
wasm-bindgen = { version ="0.2.83" }

[features]
profiling = []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

cargo build --target=wasm32-unknown-unknown
wasm-bindgen target/wasm32-unknown-unknown/debug/source_map_mappings_wasm_api.wasm --out-dir . --target web --no-typescript
mv source_map_mappings_wasm_api.js source_map_mappings_wasm_api.mjs