summaryrefslogtreecommitdiffstats
path: root/js/src/frontend/PrivateOpEmitter.cpp
blob: c10463eb56a72603d68372c1a6e40d17c556dab2 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* -*- 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/. */

#include "frontend/PrivateOpEmitter.h"

#include "frontend/BytecodeEmitter.h"
#include "frontend/NameOpEmitter.h"
#include "vm/Opcodes.h"
#include "vm/ThrowMsgKind.h"  // ThrowMsgKind

using namespace js;
using namespace js::frontend;

PrivateOpEmitter::PrivateOpEmitter(BytecodeEmitter* bce, Kind kind,
                                   TaggedParserAtomIndex name)
    : bce_(bce), kind_(kind), name_(name) {
  MOZ_ASSERT(kind_ != Kind::Delete);
}

bool PrivateOpEmitter::init() {
  // Static analysis needs us to initialise this to something, so use Dynamic()
  NameLocation loc = NameLocation::Dynamic();
  bce_->lookupPrivate(name_, loc, brandLoc_);
  loc_ = mozilla::Some(loc);
  return true;
}

bool PrivateOpEmitter::emitLoad(TaggedParserAtomIndex name,
                                const NameLocation& loc) {
  NameOpEmitter noe(bce_, name, loc, NameOpEmitter::Kind::Get);
  return noe.emitGet();
}

bool PrivateOpEmitter::emitLoadPrivateBrand() {
  return emitLoad(TaggedParserAtomIndex::WellKnown::dot_privateBrand_(),
                  *brandLoc_);
}

bool PrivateOpEmitter::emitBrandCheck() {
  MOZ_ASSERT(state_ == State::Reference);

  if (isBrandCheck()) {
    // Emit a CheckPrivateField CheckRhs; note: The message is irrelvant here,
    // it will never be thrown, so DoubleInit was chosen arbitrarily.
    if (!bce_->emitCheckPrivateField(ThrowCondition::OnlyCheckRhs,
                                     ThrowMsgKind::PrivateDoubleInit)) {
      //            [stack] OBJ KEY BBOOL
      return false;
    }

    return true;
  }

  //                [stack] OBJ KEY
  if (isFieldInit()) {
    if (!bce_->emitCheckPrivateField(ThrowCondition::ThrowHas,
                                     ThrowMsgKind::PrivateDoubleInit)) {
      //            [stack] OBJ KEY false
      return false;
    }
  } else {
    bool assigning =
        isSimpleAssignment() || isCompoundAssignment() || isIncDec();
    if (!bce_->emitCheckPrivateField(ThrowCondition::ThrowHasNot,
                                     assigning
                                         ? ThrowMsgKind::MissingPrivateOnSet
                                         : ThrowMsgKind::MissingPrivateOnGet)) {
      //            [stack] OBJ KEY true
      return false;
    }
  }

  return true;
}

bool PrivateOpEmitter::emitReference() {
  MOZ_ASSERT(state_ == State::Start);

  if (!init()) {
    return false;
  }

  if (brandLoc_) {
    if (!emitLoadPrivateBrand()) {
      //            [stack] OBJ BRAND
      return false;
    }
  } else {
    if (!emitLoad(name_, loc_.ref())) {
      //            [stack] OBJ NAME
      return false;
    }
  }
#ifdef DEBUG
  state_ = State::Reference;
#endif
  return true;
}

bool PrivateOpEmitter::skipReference() {
  MOZ_ASSERT(state_ == State::Start);

  if (!init()) {
    return false;
  }

#ifdef DEBUG
  state_ = State::Reference;
#endif
  return true;
}

bool PrivateOpEmitter::emitGet() {
  MOZ_ASSERT(state_ == State::Reference);

  //                [stack] OBJ NAME

  if (brandLoc_) {
    // Note that the decision of what we leave on the stack depends on kind_,
    // not loc_->bindingKind().  We can't emit code for a call just because this
    // private member is a method. `obj.#method` is allowed without a call,
    // just fetching the function object (it's useful in code like
    // `obj.#method.bind(...)`). Even if the user says `obj.#method += 7`, we
    // emit honest bytecode for the brand check, method load, and addition, and
    // throw the error later. This preserves stack nuses/ndefs balance.
    if (!emitBrandCheck()) {
      //            [stack] OBJ BRAND true
      return false;
    }

    if (isCompoundAssignment()) {
      if (!bce_->emit1(JSOp::Pop)) {
        //          [stack] OBJ BRAND
        return false;
      }
    } else if (isCall()) {
      if (!bce_->emitPopN(2)) {
        //          [stack] OBJ
        return false;
      }
    } else {
      if (!bce_->emitPopN(3)) {
        //          [stack]
        return false;
      }
    }

    if (!emitLoad(name_, loc_.ref())) {
      //            [stack] OBJ BRAND METHOD  # if isCompoundAssignment
      //            [stack] OBJ METHOD        # if call
      //            [stack] METHOD            # otherwise
      return false;
    }
  } else {
    if (isCall()) {
      if (!bce_->emitDupAt(1)) {
        //          [stack] OBJ NAME OBJ
        return false;
      }
      if (!bce_->emit1(JSOp::Swap)) {
        //          [stack] OBJ OBJ NAME
        return false;
      }
    }
    //              [stack] OBJ? OBJ NAME
    if (!emitBrandCheck()) {
      //            [stack] OBJ? OBJ NAME true
      return false;
    }
    if (!bce_->emit1(JSOp::Pop)) {
      //            [stack] OBJ? OBJ NAME
      return false;
    }

    if (isCompoundAssignment()) {
      if (!bce_->emit1(JSOp::Dup2)) {
        //          [stack] OBJ NAME OBJ NAME
        return false;
      }
    }

    if (!bce_->emitElemOpBase(JSOp::GetElem)) {
      //            [stack] OBJ NAME VALUE  # if isCompoundAssignment
      //            [stack] OBJ METHOD      # if Call
      //            [stack] VALUE           # otherwise
      return false;
    }
  }

  if (isCall()) {
    if (!bce_->emit1(JSOp::Swap)) {
      //            [stack] METHOD OBJ
      return false;
    }
  }

  //                [stack] OBJ NAME VALUE  # if isCompoundAssignment
  //                [stack] METHOD OBJ      # if call
  //                [stack] VALUE           # otherwise

#ifdef DEBUG
  state_ = State::Get;
#endif
  return true;
}

bool PrivateOpEmitter::emitGetForCallOrNew() { return emitGet(); }

bool PrivateOpEmitter::emitAssignment() {
  MOZ_ASSERT(isSimpleAssignment() || isFieldInit() || isCompoundAssignment());
  MOZ_ASSERT_IF(!isCompoundAssignment(), state_ == State::Reference);
  MOZ_ASSERT_IF(isCompoundAssignment(), state_ == State::Get);

  //                [stack] OBJ KEY RHS

  if (brandLoc_) {
    if (!bce_->emit2(JSOp::ThrowMsg,
                     uint8_t(ThrowMsgKind::AssignToPrivateMethod))) {
      return false;
    }

    // Balance the expression stack.
    if (!bce_->emitPopN(2)) {
      //            [stack] OBJ
      return false;
    }
  } else {
    // Emit a brand check. If this is compound assignment, emitGet() already
    // emitted a check for this object and key. There's no point checking
    // again--a private field can't be removed from an object.
    if (!isCompoundAssignment()) {
      if (!bce_->emitUnpickN(2)) {
        //          [stack] RHS OBJ KEY
        return false;
      }
      if (!emitBrandCheck()) {
        //          [stack] RHS OBJ KEY BOOL
        return false;
      }
      if (!bce_->emit1(JSOp::Pop)) {
        //          [stack] RHS OBJ KEY
        return false;
      }
      if (!bce_->emitPickN(2)) {
        //          [stack] OBJ KEY RHS
        return false;
      }
    }

    JSOp setOp = isFieldInit() ? JSOp::InitElem : JSOp::StrictSetElem;
    if (!bce_->emitElemOpBase(setOp)) {
      //            [stack] RHS
      return false;
    }
  }

#ifdef DEBUG
  state_ = State::Assignment;
#endif
  return true;
}

bool PrivateOpEmitter::emitIncDec(ValueUsage valueUsage) {
  MOZ_ASSERT(state_ == State::Reference);
  MOZ_ASSERT(isIncDec());
  //                [stack] OBJ NAME

  if (!bce_->emitDupAt(1, 2)) {
    //              [stack] OBJ NAME OBJ NAME
    return false;
  }

  if (!emitGet()) {
    //              [stack] OBJ NAME VALUE
    return false;
  }

  MOZ_ASSERT(state_ == State::Get);

  JSOp incOp = isInc() ? JSOp::Inc : JSOp::Dec;

  if (!bce_->emit1(JSOp::ToNumeric)) {
    //              [stack] OBJ NAME N
    return false;
  }
  if (isPostIncDec() && valueUsage == ValueUsage::WantValue) {
    //              [stack] OBJ NAME N
    if (!bce_->emit1(JSOp::Dup)) {
      //            [stack] OBJ NAME N N
      return false;
    }
    if (!bce_->emit2(JSOp::Unpick, 3)) {
      //            [stack] N OBJ NAME N
      return false;
    }
  }
  if (!bce_->emit1(incOp)) {
    //              [stack] N? OBJ NAME N+1
    return false;
  }

  if (brandLoc_) {
    if (!bce_->emit2(JSOp::ThrowMsg,
                     uint8_t(ThrowMsgKind::AssignToPrivateMethod))) {
      return false;
    }

    // Balance the expression stack.
    if (!bce_->emitPopN(2)) {
      //            [stack] N? N+1
      return false;
    }
  } else {
    if (!bce_->emitElemOpBase(JSOp::StrictSetElem)) {
      //            [stack] N? N+1
      return false;
    }
  }

  if (isPostIncDec() && valueUsage == ValueUsage::WantValue) {
    if (!bce_->emit1(JSOp::Pop)) {
      //            [stack] N
      return false;
    }
  }

  return true;
}