summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/actions/expressions.js
blob: 383f191506e6523df6ea42821b992c9293c18ba8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* 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/>. */

import {
  getExpression,
  getExpressions,
  getSelectedSource,
  getSelectedScopeMappings,
  getSelectedFrameBindings,
  getIsPaused,
  getSelectedFrame,
  getCurrentThread,
  isMapScopesEnabled,
} from "../selectors/index";
import { PROMISE } from "./utils/middleware/promise";
import { wrapExpression } from "../utils/expressions";
import { features } from "../utils/prefs";

/**
 * Add expression for debugger to watch
 *
 * @param {string} input
 */
export function addExpression(input) {
  return async ({ dispatch, getState }) => {
    if (!input) {
      return null;
    }

    // If the expression already exists, only update its evaluation
    let expression = getExpression(getState(), input);
    if (!expression) {
      // This will only display the expression input,
      // evaluateExpression will update its value.
      dispatch({ type: "ADD_EXPRESSION", input });

      expression = getExpression(getState(), input);
      // When there is an expression error, we won't store the expression
      if (!expression) {
        return null;
      }
    }

    return dispatch(evaluateExpression(expression));
  };
}

export function autocomplete(input, cursor) {
  return async ({ dispatch, getState, client }) => {
    if (!input) {
      return;
    }
    const thread = getCurrentThread(getState());
    const selectedFrame = getSelectedFrame(getState(), thread);
    const result = await client.autocomplete(input, cursor, selectedFrame?.id);
    // Pass both selectedFrame and thread in case selectedFrame is null
    dispatch({ type: "AUTOCOMPLETE", selectedFrame, thread, input, result });
  };
}

export function clearAutocomplete() {
  return { type: "CLEAR_AUTOCOMPLETE" };
}

export function updateExpression(input, expression) {
  return async ({ dispatch }) => {
    if (!input) {
      return;
    }

    dispatch({
      type: "UPDATE_EXPRESSION",
      expression,
      input,
    });

    await dispatch(evaluateExpressionsForCurrentContext());
  };
}

/**
 *
 * @param {object} expression
 * @param {string} expression.input
 */
export function deleteExpression(expression) {
  return {
    type: "DELETE_EXPRESSION",
    input: expression.input,
  };
}

export function evaluateExpressionsForCurrentContext() {
  return async ({ getState, dispatch }) => {
    const thread = getCurrentThread(getState());
    const selectedFrame = getSelectedFrame(getState(), thread);
    await dispatch(evaluateExpressions(selectedFrame));
  };
}

/**
 * Update all the expressions by querying the server for updated values.
 *
 * @param {object} selectedFrame
 *        If defined, will evaluate the expression against this given frame,
 *        otherwise it will use the global scope of the thread.
 */
export function evaluateExpressions(selectedFrame) {
  return async function ({ dispatch, getState, client }) {
    const expressions = getExpressions(getState());
    const inputs = expressions.map(({ input }) => input);
    // Fallback to global scope of the current thread when selectedFrame is null
    const thread = selectedFrame?.thread || getCurrentThread(getState());
    const results = await client.evaluateExpressions(inputs, {
      // We will only have a specific frame when passing a Selected frame context.
      frameId: selectedFrame?.id,
      threadId: thread,
    });
    // Pass both selectedFrame and thread in case selectedFrame is null
    dispatch({
      type: "EVALUATE_EXPRESSIONS",

      selectedFrame,
      // As `selectedFrame` can be null, pass `thread` to help
      // the reducer know what is the related thread of this action.
      thread,

      inputs,
      results,
    });
  };
}

function evaluateExpression(expression) {
  return async function (thunkArgs) {
    let { input } = expression;
    if (!input) {
      console.warn("Expressions should not be empty");
      return null;
    }

    const { dispatch, getState, client } = thunkArgs;
    const thread = getCurrentThread(getState());
    const selectedFrame = getSelectedFrame(getState(), thread);

    const selectedSource = getSelectedSource(getState());
    // Only map when we are paused and if the currently selected source is original,
    // and the paused location is also original.
    if (
      selectedFrame &&
      selectedSource &&
      selectedFrame.location.source.isOriginal &&
      selectedSource.isOriginal
    ) {
      const mapResult = await getMappedExpression(
        input,
        selectedFrame.thread,
        thunkArgs
      );
      if (mapResult) {
        input = mapResult.expression;
      }
    }

    // Pass both selectedFrame and thread in case selectedFrame is null
    return dispatch({
      type: "EVALUATE_EXPRESSION",
      selectedFrame,
      // When we aren't passing a frame, we have to pass a thread to the pause reducer
      thread: selectedFrame ? null : thread,
      input: expression.input,
      [PROMISE]: client.evaluate(wrapExpression(input), {
        // When evaluating against the global scope (when not paused)
        // frameId will be null here.
        frameId: selectedFrame?.id,
      }),
    });
  };
}

/**
 * Gets information about original variable names from the source map
 * and replaces all possible generated names.
 */
export function getMappedExpression(expression, thread, thunkArgs) {
  const { getState, parserWorker } = thunkArgs;
  const mappings = getSelectedScopeMappings(getState(), thread);
  const bindings = getSelectedFrameBindings(getState(), thread);

  // We bail early if we do not need to map the expression. This is important
  // because mapping an expression can be slow if the parserWorker
  // worker is busy doing other work.
  //
  // 1. there are no mappings - we do not need to map original expressions
  // 2. does not contain `await` - we do not need to map top level awaits
  // 3. does not contain `=` - we do not need to map assignments
  const shouldMapScopes = isMapScopesEnabled(getState()) && mappings;
  if (!shouldMapScopes && !expression.match(/(await|=)/)) {
    return null;
  }

  return parserWorker.mapExpression(
    expression,
    mappings,
    bindings || [],
    features.mapExpressionBindings && getIsPaused(getState(), thread),
    features.mapAwaitExpression
  );
}