summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/utils/watchpoint-map.js
blob: 7e4e3ee54c93fe1aa47427d2438029a1b3581349 (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
/* 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/. */

"use strict";

class WatchpointMap {
  constructor(threadActor) {
    this.threadActor = threadActor;
    this._watchpoints = new Map();
  }

  _setWatchpoint(objActor, data) {
    const { property, label, watchpointType } = data;
    const obj = objActor.rawValue();

    const desc = objActor.obj.getOwnPropertyDescriptor(property);

    if (this.has(obj, property) || desc.set || desc.get || !desc.configurable) {
      return null;
    }

    function getValue() {
      return typeof desc.value === "object" && desc.value
        ? desc.value.unsafeDereference()
        : desc.value;
    }

    function setValue(v) {
      desc.value = objActor.obj.makeDebuggeeValue(v);
    }

    const maybeHandlePause = type => {
      const frame = this.threadActor.dbg.getNewestFrame();

      if (
        this.threadActor.shouldSkipAnyBreakpoint ||
        !this.threadActor.hasMoved(frame, type) ||
        this.threadActor.sourcesManager.isFrameBlackBoxed(frame)
      ) {
        return;
      }

      this.threadActor._pauseAndRespond(frame, {
        type,
        message: label,
      });
    };

    if (watchpointType === "get") {
      objActor.obj.defineProperty(property, {
        configurable: desc.configurable,
        enumerable: desc.enumerable,
        set: objActor.obj.makeDebuggeeValue(v => {
          setValue(v);
        }),
        get: objActor.obj.makeDebuggeeValue(() => {
          maybeHandlePause("getWatchpoint");
          return getValue();
        }),
      });
    }

    if (watchpointType === "set") {
      objActor.obj.defineProperty(property, {
        configurable: desc.configurable,
        enumerable: desc.enumerable,
        set: objActor.obj.makeDebuggeeValue(v => {
          maybeHandlePause("setWatchpoint");
          setValue(v);
        }),
        get: objActor.obj.makeDebuggeeValue(() => {
          return getValue();
        }),
      });
    }

    if (watchpointType === "getorset") {
      objActor.obj.defineProperty(property, {
        configurable: desc.configurable,
        enumerable: desc.enumerable,
        set: objActor.obj.makeDebuggeeValue(v => {
          maybeHandlePause("setWatchpoint");
          setValue(v);
        }),
        get: objActor.obj.makeDebuggeeValue(() => {
          maybeHandlePause("getWatchpoint");
          return getValue();
        }),
      });
    }

    return desc;
  }

  add(objActor, data) {
    // Get the object's description before calling setWatchpoint,
    // otherwise we'll get the modified property descriptor instead
    const desc = this._setWatchpoint(objActor, data);
    if (!desc) {
      return;
    }

    const objWatchpoints =
      this._watchpoints.get(objActor.rawValue()) || new Map();

    objWatchpoints.set(data.property, { ...data, desc });
    this._watchpoints.set(objActor.rawValue(), objWatchpoints);
  }

  has(obj, property) {
    const objWatchpoints = this._watchpoints.get(obj);
    return objWatchpoints && objWatchpoints.has(property);
  }

  get(obj, property) {
    const objWatchpoints = this._watchpoints.get(obj);
    return objWatchpoints && objWatchpoints.get(property);
  }

  remove(objActor, property) {
    const obj = objActor.rawValue();

    // This should remove watchpoints on all of the object's properties if
    // a property isn't passed in as an argument
    if (!property) {
      for (const objProperty in obj) {
        this.remove(objActor, objProperty);
      }
    }

    if (!this.has(obj, property)) {
      return;
    }

    const objWatchpoints = this._watchpoints.get(obj);
    const { desc } = objWatchpoints.get(property);

    objWatchpoints.delete(property);
    this._watchpoints.set(obj, objWatchpoints);

    // We should stop keeping track of an object if it no longer
    // has a watchpoint
    if (objWatchpoints.size == 0) {
      this._watchpoints.delete(obj);
    }

    objActor.obj.defineProperty(property, desc);
  }

  removeAll(objActor) {
    const objWatchpoints = this._watchpoints.get(objActor.rawValue());
    if (!objWatchpoints) {
      return;
    }

    for (const objProperty in objWatchpoints) {
      this.remove(objActor, objProperty);
    }
  }
}

exports.WatchpointMap = WatchpointMap;