summaryrefslogtreecommitdiffstats
path: root/js/src/frontend/PrivateOpEmitter.h
blob: 558541b05c36b3c0d2e179733a54332f166c5263 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * 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/. */

#ifndef frontend_PrivateOpEmitter_h
#define frontend_PrivateOpEmitter_h

#include "mozilla/Attributes.h"
#include "mozilla/Maybe.h"

#include <stddef.h>

#include "frontend/NameAnalysisTypes.h"  // NameLocation
#include "frontend/ParserAtom.h"         // TaggedParserAtomIndex

namespace js {
namespace frontend {

struct BytecodeEmitter;
enum class ValueUsage;

// Class for emitting bytecode for operations on private members of objects.
//
// Usage is similar to PropOpEmitter, but the name of the private member must
// be passed to the constructor; prepare*() methods aren't necessary; and
// `delete obj.#member` and `super.#member` aren't supported because both are
// SyntaxErrors.
//
// Usage: (error checking is omitted for simplicity)
//
//   `obj.#member;`
//     PrivateOpEmitter xoe(this,
//                          privateName,
//                          PrivateOpEmitter::Kind::Get);
//     emit(obj);
//     xoe.emitReference();
//     xoe.emitGet();
//
//   `obj.#member();`
//     PrivateOpEmitter xoe(this,
//                          privateName,
//                          PrivateOpEmitter::Kind::Call);
//     emit(obj);
//     xoe.emitReference();
//     xoe.emitGet();
//     emit_call_here();
//
//   `new obj.#member();`
//     The same, but use PrivateOpEmitter::Kind::Get.
//
//   `obj.#field++;`
//     PrivateOpEmitter xoe(this,
//                          privateName,
//                          PrivateOpEmitter::Kind::PostIncrement);
//     emit(obj);
//     xoe.emitReference();
//     xoe.emitIncDec();
//
//   `obj.#field = value;`
//     PrivateOpEmitter xoe(this,
//                          privateName,
//                          PrivateOpEmitter::Kind::SimpleAssignment);
//     emit(obj);
//     xoe.emitReference();
//     emit(value);
//     xoe.emitAssignment();
//
//   `obj.#field += value;`
//     PrivateOpEmitter xoe(this,
//                          privateName,
//                          PrivateOpEmitter::Kind::CompoundAssignment);
//     emit(obj);
//     xoe.emitReference();
//     emit(JSOp::Dup2);
//     xoe.emitGet();
//     emit(value);
//     emit_add_op_here();
//     xoe.emitAssignment();
//
class MOZ_STACK_CLASS PrivateOpEmitter {
 public:
  enum class Kind {
    Get,
    Call,
    Delete,
    PostIncrement,
    PreIncrement,
    PostDecrement,
    PreDecrement,
    SimpleAssignment,
    PropInit,
    CompoundAssignment,
    ErgonomicBrandCheck,
  };

 private:
  BytecodeEmitter* bce_;

  Kind kind_;

  // Name of the private member, e.g. "#field".
  TaggedParserAtomIndex name_;

  // Location of the slot containing the private name symbol; or, for a
  // non-static private method, the slot containing the method.
  mozilla::Maybe<NameLocation> loc_;

  // For non-static private method accesses, the location of the relevant
  // `.privateBrand` binding. Otherwise, `Nothing`.
  mozilla::Maybe<NameLocation> brandLoc_{};

#ifdef DEBUG
  // The state of this emitter.
  //
  //            emitReference
  // +-------+  skipReference  +-----------+
  // | Start |---------------->| Reference |
  // +-------+                 +-----+-----+
  //                                 |
  //     +---------------------------+
  //     |
  //     |
  //     | [Get]
  //     |   emitGet
  //     | [Call] [Get]                  [CompoundAssignment]
  //     |   emitGetForCallOrNew +-----+   emitAssignment
  //     +---------------------->| Get |-------------------+
  //     |                       +-----+                   |
  //     | [PostIncrement]                                 |
  //     | [PreIncrement]                                  |
  //     | [PostDecrement]                                 |
  //     | [PreDecrement]                                  |
  //     |   emitIncDec                                    |
  //     +------------------------------------------------>+
  //     |                                                 |
  //     | [SimpleAssignment]                              |
  //     | [PropInit]                                      V
  //     |   emitAssignment                          +------------+
  //     +------------------------------------------>| Assignment |
  //                                                 +------------+
  enum class State {
    // The initial state.
    Start,

    // After calling emitReference or skipReference.
    Reference,

    // After calling emitGet.
    Get,

    // After calling emitAssignment or emitIncDec.
    Assignment,
  };
  State state_ = State::Start;
#endif

 public:
  PrivateOpEmitter(BytecodeEmitter* bce, Kind kind, TaggedParserAtomIndex name);

 private:
  [[nodiscard]] bool isCall() const { return kind_ == Kind::Call; }

  [[nodiscard]] bool isSimpleAssignment() const {
    return kind_ == Kind::SimpleAssignment;
  }

  [[nodiscard]] bool isFieldInit() const { return kind_ == Kind::PropInit; }

  [[nodiscard]] bool isBrandCheck() const {
    return kind_ == Kind::ErgonomicBrandCheck;
  }

  [[nodiscard]] bool isCompoundAssignment() const {
    return kind_ == Kind::CompoundAssignment;
  }

  [[nodiscard]] bool isIncDec() const {
    return isPostIncDec() || isPreIncDec();
  }

  [[nodiscard]] bool isPostIncDec() const {
    return kind_ == Kind::PostIncrement || kind_ == Kind::PostDecrement;
  }

  [[nodiscard]] bool isPreIncDec() const {
    return kind_ == Kind::PreIncrement || kind_ == Kind::PreDecrement;
  }

  [[nodiscard]] bool isInc() const {
    return kind_ == Kind::PostIncrement || kind_ == Kind::PreIncrement;
  }

  [[nodiscard]] bool init();

  // Emit a GetAliasedLexical or similar instruction.
  [[nodiscard]] bool emitLoad(TaggedParserAtomIndex name,
                              const NameLocation& loc);

  [[nodiscard]] bool emitLoadPrivateBrand();

 public:
  // Emit bytecode to check for the presence/absence of a private field/brand.
  //
  // Given OBJ KEY on the stack, where KEY is a private name symbol, the
  // emitted code will throw if OBJ does not have the given KEY.
  //
  // If `isFieldInit()`, the check is reversed: the code will throw if OBJ
  // already has the KEY.
  //
  // If `isBrandCheck()`, the check verifies RHS is an object (throwing if not).
  //
  // The bytecode leaves OBJ KEY BOOL on the stack. Caller is responsible for
  // consuming or popping it.
  [[nodiscard]] bool emitBrandCheck();

  [[nodiscard]] bool emitReference();
  [[nodiscard]] bool skipReference();
  [[nodiscard]] bool emitGet();
  [[nodiscard]] bool emitGetForCallOrNew();
  [[nodiscard]] bool emitAssignment();
  [[nodiscard]] bool emitIncDec(ValueUsage valueUsage);

  [[nodiscard]] size_t numReferenceSlots() { return 2; }
};

} /* namespace frontend */
} /* namespace js */

#endif /* frontend_PrivateOpEmitter_h */