summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/object/symbol-iterator.js
blob: 5982de2acaf4e70b3e76cc0ddef344dabc34cdf6 (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
/* 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";

const protocol = require("devtools/shared/protocol");
const { symbolIteratorSpec } = require("devtools/shared/specs/symbol-iterator");
const DevToolsUtils = require("devtools/shared/DevToolsUtils");

/**
 * Creates an actor to iterate over an object's symbols.
 *
 * @param objectActor ObjectActor
 *        The object actor.
 */
const SymbolIteratorActor = protocol.ActorClassWithSpec(symbolIteratorSpec, {
  initialize(objectActor, conn) {
    protocol.Actor.prototype.initialize.call(this, conn);

    let symbols = [];
    if (DevToolsUtils.isSafeDebuggerObject(objectActor.obj)) {
      try {
        symbols = objectActor.obj.getOwnPropertySymbols();
      } catch (err) {
        // The above can throw when the debuggee does not subsume the object's
        // compartment, or for some WrappedNatives like Cu.Sandbox.
      }
    }

    this.iterator = {
      size: symbols.length,
      symbolDescription(index) {
        const symbol = symbols[index];
        return {
          name: symbol.toString(),
          descriptor: objectActor._propertyDescriptor(symbol),
        };
      },
    };
  },

  form() {
    return {
      type: this.typeName,
      actor: this.actorID,
      count: this.iterator.size,
    };
  },

  slice({ start, count }) {
    const ownSymbols = [];
    for (let i = start, m = start + count; i < m; i++) {
      ownSymbols.push(this.iterator.symbolDescription(i));
    }
    return {
      ownSymbols,
    };
  },

  all() {
    return this.slice({ start: 0, count: this.iterator.size });
  },
});

exports.SymbolIteratorActor = SymbolIteratorActor;