Files
react-native-devtools-frontend/front_end/object_ui/JavaScriptREPL.js
T
Simon Zünd 149d99e7f5 Revert "Reland "Remove top-level await pre-processing""
This reverts commit 1e3659a463.

Reason for revert: Preparatory revert in case the corresponding
V8 CL gets reverted as well.

Original change's description:
> Reland "Remove top-level await pre-processing"
> 
> This is a reland of d4092e36af
> 
> The CL was speculatively reverted due to a deterministic bot failure
> (https://ci.chromium.org/p/chromium/builders/ci/Deterministic%20Linux/25924)
> 
> The bot has since turned green without the revert beeing rolled into chromium.
> Relanding this CL, to potentially fix the current red tree.
> 
> Original change's description:
> > Remove top-level await pre-processing
> >
> > This CL removes the wrapping of code in async arrow functions, should
> > the code contain 'await'. Support for top-level is implemented as part
> > of V8's native REPL mode.
> >
> > R=petermarshall@chromium.org
> >
> > Bug: chromium:1021921
> > Change-Id: I0a601dd9a469a108f39d2ea3abcf8700726a1d8e
> > Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/1928927
> > Commit-Queue: Simon Zünd <szuend@chromium.org>
> > Reviewed-by: Peter Marshall <petermarshall@chromium.org>
> > Reviewed-by: Mathias Bynens <mathias@chromium.org>
> 
> TBR: petermarshall@chromium.org
> Bug: chromium:1021921
> Change-Id: If7be71cabc6a41c5433636132d9c9f222162747f
> Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/1947524
> Reviewed-by: Simon Zünd <szuend@chromium.org>
> Commit-Queue: Simon Zünd <szuend@chromium.org>

TBR=petermarshall@chromium.org,mathias@chromium.org,szuend@chromium.org

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: chromium:1021921
Change-Id: I6643c1ed8336a96362be06b4c60d57cb12f760f9
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/1950304
Reviewed-by: Simon Zünd <szuend@chromium.org>
Commit-Queue: Simon Zünd <szuend@chromium.org>
2019-12-05 07:33:40 +00:00

124 lines
4.0 KiB
JavaScript

// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
export default class JavaScriptREPL {
/**
* @param {string} code
* @return {string}
*/
static wrapObjectLiteral(code) {
// Only parenthesize what appears to be an object literal.
if (!(/^\s*\{/.test(code) && /\}\s*$/.test(code))) {
return code;
}
const parse = (async () => 0).constructor;
try {
// Check if the code can be interpreted as an expression.
parse('return ' + code + ';');
// No syntax error! Does it work parenthesized?
const wrappedCode = '(' + code + ')';
parse(wrappedCode);
return wrappedCode;
} catch (e) {
return code;
}
}
/**
* @param {string} text
* @return {!Promise<!{text: string, preprocessed: boolean}>}
*/
static async preprocessExpression(text) {
text = JavaScriptREPL.wrapObjectLiteral(text);
let preprocessed = false;
if (text.indexOf('await') !== -1) {
const preprocessedText = await Formatter.formatterWorkerPool().preprocessTopLevelAwaitExpressions(text);
preprocessed = !!preprocessedText;
text = preprocessedText || text;
}
return {text, preprocessed};
}
/**
* @param {string} text
* @param {boolean} throwOnSideEffect
* @param {number=} timeout
* @param {boolean=} allowErrors
* @param {string=} objectGroup
* @return {!Promise<!{preview: !DocumentFragment, result: ?SDK.RuntimeModel.EvaluationResult}>}
*/
static async evaluateAndBuildPreview(text, throwOnSideEffect, timeout, allowErrors, objectGroup) {
const executionContext = UI.context.flavor(SDK.ExecutionContext);
const isTextLong = text.length > ObjectUI.JavaScriptREPL._MaxLengthForEvaluation;
if (!text || !executionContext || (throwOnSideEffect && isTextLong)) {
return {preview: createDocumentFragment(), result: null};
}
const wrappedResult = await JavaScriptREPL.preprocessExpression(text);
const options = {
expression: wrappedResult.text,
generatePreview: true,
includeCommandLineAPI: true,
throwOnSideEffect: throwOnSideEffect,
timeout: timeout,
objectGroup: objectGroup,
disableBreaks: true
};
const result = await executionContext.evaluate(
options, false /* userGesture */, wrappedResult.preprocessed /* awaitPromise */);
const preview = JavaScriptREPL._buildEvaluationPreview(result, allowErrors);
return {preview, result};
}
/**
* @param {!SDK.RuntimeModel.EvaluationResult} result
* @param {boolean=} allowErrors
* @return {!DocumentFragment}
*/
static _buildEvaluationPreview(result, allowErrors) {
const fragment = createDocumentFragment();
if (result.error) {
return fragment;
}
if (result.exceptionDetails && result.exceptionDetails.exception && result.exceptionDetails.exception.description) {
const exception = result.exceptionDetails.exception.description;
if (exception.startsWith('TypeError: ') || allowErrors) {
fragment.createChild('span').textContent = result.exceptionDetails.text + ' ' + exception;
}
return fragment;
}
const formatter = new ObjectUI.RemoteObjectPreviewFormatter();
const {preview, type, subtype, description} = result.object;
if (preview && type === 'object' && subtype !== 'node') {
formatter.appendObjectPreview(fragment, preview, false /* isEntry */);
} else {
const nonObjectPreview = formatter.renderPropertyPreview(type, subtype, description.trimEndWithMaxLength(400));
fragment.appendChild(nonObjectPreview);
}
return fragment;
}
}
/**
* @const
* @type {number}
*/
export const _MaxLengthForEvaluation = 2000;
/* Legacy exported object */
self.ObjectUI = self.ObjectUI || {};
/* Legacy exported object */
ObjectUI = ObjectUI || {};
/** @constructor */
ObjectUI.JavaScriptREPL = JavaScriptREPL;
ObjectUI.JavaScriptREPL._MaxLengthForEvaluation = _MaxLengthForEvaluation;