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

Issue 2686 #2688

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ import {
PropertyPrototype,
IndexSignature,
File,
mangleInternalName
mangleInternalName,
DebugLocation
} from "./program";

import {
Expand Down Expand Up @@ -9830,8 +9831,7 @@ export class Compiler extends DiagnosticEmitter {
let targetFunction = this.currentFlow.targetFunction;
let source = range.source;
if (source.debugInfoIndex < 0) source.debugInfoIndex = this.module.addDebugInfoFile(source.normalizedPath);
range.debugInfoRef = expr;
targetFunction.debugLocations.push(range);
targetFunction.debugLocations.push(new DebugLocation(expr, range));
}

/** Checks whether a particular function signature is supported. */
Expand Down
1 change: 0 additions & 1 deletion src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export const enum DiagnosticCategory {
export class Range {

source!: Source;
debugInfoRef: usize = 0;

constructor(public start: i32, public end: i32) {}

Expand Down
23 changes: 16 additions & 7 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ import {
Module,
FunctionRef,
MemorySegment,
getFunctionName
getFunctionName,
ExpressionRef
} from "./module";

import {
Expand Down Expand Up @@ -421,6 +422,13 @@ export namespace OperatorKind {
return OperatorKind.Invalid;
}
}
/** DebugLocation for debug info generation */
export class DebugLocation {
/** Constructs a new Debug Location*/
constructor(
public expressionRef: ExpressionRef,
public range: Range) {}
}

/** Represents an AssemblyScript program. */
export class Program extends DiagnosticEmitter {
Expand Down Expand Up @@ -3745,7 +3753,7 @@ export class Function extends TypedElement {
/** Default control flow. */
flow!: Flow;
/** Remembered debug locations. */
debugLocations: Range[] = [];
debugLocations: DebugLocation[] = [];
/** Function reference, if compiled. */
ref: FunctionRef = 0;
/** Varargs stub for calling with omitted arguments. */
Expand Down Expand Up @@ -3912,13 +3920,14 @@ export class Function extends TypedElement {

addDebugInfo(module: Module, ref: FunctionRef): void {
if (this.program.options.sourceMap) {
let debugLocations = this.debugLocations;
for (let i = 0, k = debugLocations.length; i < k; ++i) {
let range = debugLocations[i];
let source = range.source;
const debugLocations = this.debugLocations;
for (let i = 0; i < debugLocations.length; ++i) {
const debugLocation = debugLocations[i];
const range = debugLocation.range;
const source = range.source;
module.setDebugLocation(
ref,
range.debugInfoRef,
debugLocation.expressionRef,
source.debugInfoIndex,
source.lineAt(range.start),
source.columnAt() - 1 // source maps are 0-based
Expand Down
40 changes: 39 additions & 1 deletion tests/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { coreCount, threadCount } from "../util/cpu.js";
import { diff } from "../util/text.js";
import { Rtrace } from "../lib/rtrace/index.js";
import asc from "../dist/asc.js";
import {exec} from "child_process";

const dirname = path.dirname(fileURLToPath(import.meta.url));
const require = createRequire(import.meta.url);
Expand Down Expand Up @@ -143,7 +144,10 @@ async function runTest(basename) {
// Makes sure to reset the environment after
function prepareResult(code, message = null) {
if (v8_no_flags) v8.setFlagsFromString(v8_no_flags);
if (!args.createBinary) fs.unlink(path.join(basedir, basename + ".debug.wasm"), err => { /* nop */ });
if (!args.createBinary) {
fs.unlink(path.join(basedir, basename + ".debug.wasm"), err => { /* nop */ });
fs.unlink(path.join(basedir, basename + ".debug.wasm.map"), err => { /* nop */ });
}
return { code, message };
}

Expand Down Expand Up @@ -253,6 +257,40 @@ async function runTest(basename) {
}
compareFixture.end(SUCCESS);
}

// compare fixture source map
const sourcemapCompareFixture = section("compare fixture source map");
const debugWasmPath = path.join(basedir, basename + ".debug.wasm");
const debugSourceMapPath = path.join(basedir, basename + ".debug.wasm.map");
const sourceMapFixturePath = path.join(basedir, basename + ".debug.sourcemap.wat");

if (fs.existsSync(debugSourceMapPath) && fs.existsSync(sourceMapFixturePath)) {
const sourceMapCmd = `node node_modules/.bin/wasm-opt ${debugWasmPath} -ism ${debugSourceMapPath} --print`;
exec(sourceMapCmd, (err, stdout, stderr) => {
const actual = stdout.toString().replace(/\r\n/g, "\n");
if (args.create) {
fs.writeFileSync(sourceMapFixturePath, actual, { encoding: "utf8" });
console.log(" " + stdoutColors.yellow("Created fixture"));
sourcemapCompareFixture.end(SKIPPED);
} else {
const expected = fs.readFileSync(sourceMapFixturePath, { encoding: "utf8" }).replace(/\r\n/g, "\n");
if (args.noDiff) {
if (expected != actual) {
compareFixture.end(FAILURE);
return prepareResult(FAILURE, "fixture mismatch");
}
} else {
let diffs = diff(basename + ".debug.sourcemap.wat", expected, actual);
if (diffs !== null) {
console.log(diffs);
compareFixture.end(FAILURE);
return prepareResult(FAILURE, "fixture mismatch");
}
}
compareFixture.end(SUCCESS);
}
});
}
}

stdout.length = 0;
Expand Down
Loading