Files
react-native-devtools-frontend/front_end/object_ui/JavaScriptREPL.js
T
Erik Luo 157299825a DevTools: generalize preview without side effect builder
Introduces JavaScriptREPL, a utility that includes
- Logic used by EagerEval for building side-effect-free previews
- Console prompt's preprocessing logic
  (object literal wrapping and top level await)

Bug: 849875
Change-Id: Ie8146435fced9ebf440b43c225d25ef145416247
Reviewed-on: https://chromium-review.googlesource.com/1145911
Reviewed-by: Dmitry Gozman <dgozman@chromium.org>
Commit-Queue: Erik Luo <luoe@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#577341}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: f8ffab5d79243cb7de978d17669e8decdb9141df
2018-07-23 23:48:44 +00:00

104 lines
3.4 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.
ObjectUI.JavaScriptREPL = class {
/**
* @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 = ObjectUI.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
* @return {!Promise<!{preview: !DocumentFragment, result: ?SDK.RuntimeModel.EvaluationResult}>}
*/
static async evaluateAndBuildPreview(text, throwOnSideEffect, timeout) {
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 ObjectUI.JavaScriptREPL.preprocessExpression(text);
const options = {
expression: wrappedResult.text,
generatePreview: true,
includeCommandLineAPI: true,
throwOnSideEffect: throwOnSideEffect,
timeout: timeout
};
const result = await executionContext.evaluate(
options, false /* userGesture */, wrappedResult.preprocessed /* awaitPromise */);
const preview = ObjectUI.JavaScriptREPL._buildEvaluationPreview(result);
return {preview, result};
}
/**
* @param {!SDK.RuntimeModel.EvaluationResult} result
* @return {!DocumentFragment}
*/
static _buildEvaluationPreview(result) {
const fragment = createDocumentFragment();
if (result.error)
return fragment;
if (result.exceptionDetails) {
const exception = result.exceptionDetails.exception.description;
if (exception.startsWith('TypeError: '))
fragment.createChild('span').textContent = 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.trimEnd(400));
fragment.appendChild(nonObjectPreview);
}
return fragment;
}
};
/**
* @const
* @type {number}
*/
ObjectUI.JavaScriptREPL._MaxLengthForEvaluation = 2000;