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

Fix captureJSCallUsage in the face of Error polyfills. #1218

Merged
merged 6 commits into from
Dec 21, 2016
Merged
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
5 changes: 5 additions & 0 deletions lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ <h2>Do better web tester page</h2>
<a href="./doesnotexist" target="_blank">internal link is ok</a>
</template>

<!-- Some websites overwrite the original Error object. The captureJSCallUsage function
relies on the native Error object and prepareStackTrace from V8. When overwriting the stack
property the e.stack inside the gatherer won't be in the correct format
https://github.com/GoogleChrome/lighthouse/issues/1194 -->
<script>window.Error = function(error) { this.stack = 'stacktrace'; };</script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will you add a comment about why we're doing this and maybe referencing #1194 for future reference?

<script>
function stampTemplate(id, location) {
const template = document.querySelector('#' + id);
Expand Down
18 changes: 14 additions & 4 deletions lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,15 @@ class Driver {
});
}

/**
* Cache native functions/objects inside window
* so we are sure polyfills do not overwrite the native implementations
*/
cacheNatives() {
return this.evaluateScriptOnLoad(`window.__nativePromise = Promise;
window.__nativeError = Error;`);
}

/**
* Keeps track of calls to a JS function and returns a list of {url, line, col}
* of the usage. Should be called before page load (in beforePass).
Expand Down Expand Up @@ -715,16 +724,17 @@ class Driver {
* @return {function(...*): *} A wrapper around the original function.
*/
function captureJSCallUsage(funcRef, set) {
const __nativeError = window.__nativeError || Error;
const originalFunc = funcRef;
const originalPrepareStackTrace = Error.prepareStackTrace;
const originalPrepareStackTrace = __nativeError.prepareStackTrace;

return function() {
// Note: this function runs in the context of the page that is being audited.

const args = [...arguments]; // callee's arguments.

// See v8's Stack Trace API https://github.com/v8/v8/wiki/Stack-Trace-API#customizing-stack-traces
Error.prepareStackTrace = function(error, structStackTrace) {
__nativeError.prepareStackTrace = function(error, structStackTrace) {
// First frame is the function we injected (the one that just threw).
// Second, is the actual callsite of the funcRef we're after.
const callFrame = structStackTrace[1];
Expand All @@ -747,12 +757,12 @@ function captureJSCallUsage(funcRef, set) {
// would be unique.
return {url, args, line, col, isEval}; // return value is e.stack
};
const e = new Error(`__called ${funcRef.name}__`);
const e = new __nativeError(`__called ${funcRef.name}__`);
set.add(JSON.stringify(e.stack));

// Restore prepareStackTrace so future errors use v8's formatter and not
// our custom one.
Error.prepareStackTrace = originalPrepareStackTrace;
__nativeError.prepareStackTrace = originalPrepareStackTrace;

// eslint-disable-next-line no-invalid-this
return originalFunc.apply(this, arguments);
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/gather/gather-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class GatherRunner {
return driver.assertNoSameOriginServiceWorkerClients(options.url)
.then(_ => driver.beginEmulation(options.flags))
.then(_ => driver.enableRuntimeEvents())
.then(_ => driver.evaluateScriptOnLoad('window.__nativePromise = Promise;'))
.then(_ => driver.cacheNatives())
.then(_ => driver.cleanAndDisableBrowserCaches())
.then(_ => driver.clearDataForOrigin(options.url));
}
Expand Down
3 changes: 3 additions & 0 deletions lighthouse-core/test/gather/fake-driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ module.exports = {
},
cleanAndDisableBrowserCaches() {},
clearDataForOrigin() {},
cacheNatives() {
return Promise.resolve();
},
beginTrace() {
return Promise.resolve();
},
Expand Down
3 changes: 3 additions & 0 deletions lighthouse-core/test/gather/gather-runner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ function getMockedEmulationDriver(emulationFn, netThrottleFn, cpuThrottleFn) {
assertNoSameOriginServiceWorkerClients() {
return Promise.resolve();
}
cacheNatives() {
return Promise.resolve();
}
cleanAndDisableBrowserCaches() {}
clearDataForOrigin() {}
};
Expand Down