Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
Bug 961194 - Pull out stack utils in loader to prevent circular depen…
Browse files Browse the repository at this point in the history
…dencies in SDK
  • Loading branch information
jsantell committed Feb 6, 2014
1 parent fdafd7b commit 160be1d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 38 deletions.
8 changes: 4 additions & 4 deletions lib/sdk/console/traceback.js
Expand Up @@ -9,10 +9,10 @@ module.metadata = {
};

const { Cc, Ci, components } = require("chrome");
const { parseStack, sourceURI } = require("toolkit/loader");
const { parseStack, parseURI } = require("../../toolkit/stack-utils");
const { readURISync } = require("../net/url");

exports.sourceURI = sourceURI
exports.sourceURI = parseURI;

function safeGetFileLine(path, line) {
try {
Expand All @@ -31,7 +31,7 @@ function nsIStackFramesToJSON(frame) {
while (frame) {
if (frame.filename) {
stack.unshift({
fileName: sourceURI(frame.filename),
fileName: parseURI(frame.filename),
lineNumber: frame.lineNumber,
name: frame.name
});
Expand All @@ -48,7 +48,7 @@ var fromException = exports.fromException = function fromException(e) {
if (e.stack && e.stack.length)
return parseStack(e.stack);
if (e.fileName && typeof(e.lineNumber == "number"))
return [{fileName: sourceURI(e.fileName),
return [{fileName: parseURI(e.fileName),
lineNumber: e.lineNumber,
name: null}];
return [];
Expand Down
2 changes: 1 addition & 1 deletion lib/sdk/test/harness.js
Expand Up @@ -9,7 +9,7 @@ module.metadata = {

const { Cc, Ci, Cu } = require("chrome");
const { Loader } = require('./loader');
const { serializeStack, parseStack } = require("toolkit/loader");
const { parseStack, serializeStack } = require("../../toolkit/stack-utils");
const { setTimeout } = require('../timers');
const { PlainTextConsole } = require("../console/plain-text");
const { when: unload } = require("../system/unload");
Expand Down
40 changes: 7 additions & 33 deletions lib/toolkit/loader.js
Expand Up @@ -42,6 +42,8 @@ const { NetUtil } = Cu.import("resource://gre/modules/NetUtil.jsm", {});
const { Reflect } = Cu.import("resource://gre/modules/reflect.jsm", {});
const { console } = Cu.import("resource://gre/modules/devtools/Console.jsm");
const { join: pathJoin, normalize, dirname } = Cu.import("resource://gre/modules/osfile/ospath_unix.jsm");
const toolkitURI = module.uri.replace(/loader\.js$/, "");
const { parseStack, serializeStack, parseURI } = Cu.import(toolkitURI + "/stack-utils.js", {}).stackUtils;

// Define some shortcuts.
const bind = Function.call.bind(Function.bind);
Expand Down Expand Up @@ -126,41 +128,13 @@ const override = iced(function override(target, source) {
});
exports.override = override;

function sourceURI(uri) { return String(uri).split(" -> ").pop(); }
exports.sourceURI = iced(sourceURI);
// Export stack utils
exports.parseStack = iced(parseStack);
exports.serializeStack= iced(serializeStack);
exports.sourceURI = iced(parseURI);

function isntLoaderFrame(frame) { return frame.fileName !== module.uri }

var parseStack = iced(function parseStack(stack) {
let lines = String(stack).split("\n");
return lines.reduce(function(frames, line) {
if (line) {
let atIndex = line.indexOf("@");
let columnIndex = line.lastIndexOf(":");
let fileName = sourceURI(line.slice(atIndex + 1, columnIndex));
let lineNumber = parseInt(line.slice(columnIndex + 1));
let name = line.slice(0, atIndex).split("(").shift();
frames.unshift({
fileName: fileName,
name: name,
lineNumber: lineNumber
});
}
return frames;
}, []);
})
exports.parseStack = parseStack

var serializeStack = iced(function serializeStack(frames) {
return frames.reduce(function(stack, frame) {
return frame.name + "@" +
frame.fileName + ":" +
frame.lineNumber + "\n" +
stack;
}, "");
})
exports.serializeStack = serializeStack

function readURI(uri) {
let stream = NetUtil.newChannel(uri, 'UTF-8', null).open();
let count = stream.available();
Expand Down Expand Up @@ -302,7 +276,7 @@ const load = iced(function load(loader, module) {
let stack = error.stack || Error().stack;
let frames = parseStack(stack).filter(isntLoaderFrame);
let toString = String(error);
let file = sourceURI(fileName);
let file = parseURI(fileName);

// Note that `String(error)` where error is from subscript loader does
// not puts `:` after `"Error"` unlike regular errors thrown by JS code.
Expand Down
67 changes: 67 additions & 0 deletions lib/toolkit/stack-utils.js
@@ -0,0 +1,67 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

;(function(id, factory) { // Module boilerplate :(
if (typeof(define) === "function") { // RequireJS
define(factory);
} else if (typeof(require) === "function") { // CommonJS
factory.call(this, require, exports, module);
} else if (~String(this).indexOf("BackstagePass")) { // JSM
this[factory.name] = {};
factory(function require(uri) {
var imports = {};
this["Components"].utils.import(uri, imports);
return imports;
}, this[factory.name], { uri: __URI__, id: id });
this.EXPORTED_SYMBOLS = [factory.name];
} else if (~String(this).indexOf("Sandbox")) { // Sandbox
factory(function require(uri) {}, this, { uri: __URI__, id: id });
} else { // Browser or alike
var globals = this
factory(function require(id) {
return globals[id];
}, (globals[id] = {}), { uri: document.location.href + "#" + id, id: id });
}
}).call(this, "stackUtils", function stackUtils (require, exports, module) {

"use strict";

module.metadata = {
"stability": "unstable"
};

function parseURI(uri) { return String(uri).split(" -> ").pop(); }
exports.parseURI = parseURI;

function parseStack(stack) {
let lines = String(stack).split("\n");
return lines.reduce(function(frames, line) {
if (line) {
let atIndex = line.indexOf("@");
let columnIndex = line.lastIndexOf(":");
let fileName = parseURI(line.slice(atIndex + 1, columnIndex));
let lineNumber = parseInt(line.slice(columnIndex + 1));
let name = line.slice(0, atIndex).split("(").shift();
frames.unshift({
fileName: fileName,
name: name,
lineNumber: lineNumber
});
}
return frames;
}, []);
}
exports.parseStack = parseStack;

function serializeStack(frames) {
return frames.reduce(function(stack, frame) {
return frame.name + "@" +
frame.fileName + ":" +
frame.lineNumber + "\n" +
stack;
}, "");
}
exports.serializeStack = serializeStack;

});

0 comments on commit 160be1d

Please sign in to comment.