Debian Codegraph @

js/src/vm/Interpreter-inl.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef vm_Interpreter_inl_h
8 #define vm_Interpreter_inl_h
9
10 #include "vm/Interpreter.h"
11
12 #include "jslibmath.h"
13 #include "jsmath.h"
14 #include "jsnum.h"
15
16 #include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_*
17 #include "util/CheckedArithmetic.h"
18 #include "vm/BigIntType.h"
19 #include "vm/BytecodeUtil.h" // JSDVG_SEARCH_STACK
20 #include "vm/JSAtomUtils.h" // AtomizeString
21 #include "vm/Realm.h"
22 #include "vm/StaticStrings.h"
23 #include "vm/ThrowMsgKind.h"
24
25 #include "vm/GlobalObject-inl.h"
26 #include "vm/JSAtomUtils-inl.h" // PrimitiveValueToId, TypeName
27 #include "vm/JSContext-inl.h"
28 #include "vm/JSObject-inl.h"
29 #include "vm/NativeObject-inl.h"
30 #include "vm/ObjectOperations-inl.h"
31 #include "vm/StringType-inl.h"
32
33 namespace js {
34
35 /*
36 * Per ES6, lexical declarations may not be accessed in any fashion until they
37 * are initialized (i.e., until the actual declaring statement is
38 * executed). The various LEXICAL opcodes need to check if the slot is an
39 * uninitialized let declaration, represented by the magic value
40 * JS_UNINITIALIZED_LEXICAL.
41 */
42 static inline bool IsUninitializedLexical(const Value& val) {
43 // Use whyMagic here because JS_OPTIMIZED_OUT could flow into here.
44 return val.isMagic() && val.whyMagic() == JS_UNINITIALIZED_LEXICAL;
45 }
46
47 static inline bool IsUninitializedLexicalSlot(HandleObject obj,
48 const PropertyResult& prop) {
49 MOZ_ASSERT(prop.isFound());
50 if (obj->is<WithEnvironmentObject>()) {
51 return false;
52 }
53
54 // Proxy hooks may return a non-native property.
55 if (prop.isNonNativeProperty()) {
56 return false;
57 }
58
59 PropertyInfo propInfo = prop.propertyInfo();
60 if (!propInfo.isDataProperty()) {
61 return false;
62 }
63
64 return IsUninitializedLexical(
65 obj->as<NativeObject>().getSlot(propInfo.slot()));
66 }
67
68 static inline bool CheckUninitializedLexical(JSContext* cx,
69 Handle<PropertyName*> name,
70 HandleValue val) {
71 if (IsUninitializedLexical(val)) {
72 ReportRuntimeLexicalError(cx, JSMSG_UNINITIALIZED_LEXICAL, name);
73 return false;
74 }
75 return true;
76 }
77
78 enum class GetNameMode { Normal, TypeOf };
79
80 template <GetNameMode mode>
81 inline bool FetchName(JSContext* cx, HandleObject receiver, HandleObject holder,
82 Handle<PropertyName*> name, const PropertyResult& prop,
83 MutableHandleValue vp) {
84 if (prop.isNotFound()) {
85 switch (mode) {
86 case GetNameMode::Normal:
87 ReportIsNotDefined(cx, name);
88 return false;
89 case GetNameMode::TypeOf:
90 vp.setUndefined();
91 return true;
92 }
93 }
94
95 /* Take the slow path if shape was not found in a native object. */
96 if (!receiver->is<NativeObject>() || !holder->is<NativeObject>() ||
97 (receiver->is<WithEnvironmentObject>() &&
98 receiver->as<WithEnvironmentObject>().supportUnscopables())) {
99 Rooted<jsid> id(cx, NameToId(name));
100 if (!GetProperty(cx, receiver, receiver, id, vp)) {
101 return false;
102 }
103 } else {
104 PropertyInfo propInfo = prop.propertyInfo();
105 if (propInfo.isDataProperty()) {
106 /* Fast path for Object instance properties. */
107 vp.set(holder->as<NativeObject>().getSlot(propInfo.slot()));
108 } else {
109 // Unwrap 'with' environments for reasons given in
110 // GetNameBoundInEnvironment.
111 RootedObject normalized(cx, MaybeUnwrapWithEnvironment(receiver));
112 RootedId id(cx, NameToId(name));
113 if (!NativeGetExistingProperty(cx, normalized, holder.as<NativeObject>(),
114 id, propInfo, vp)) {
115 return false;
116 }
117 }
118 }
119
120 // We do our own explicit checking for |this|
121 if (name == cx->names().dot_this_) {
122 return true;
123 }
124
125 // NAME operations are the slow paths already, so unconditionally check
126 // for uninitialized lets.
127 return CheckUninitializedLexical(cx, name, vp);
128 }
129
130 inline bool FetchNameNoGC(NativeObject* pobj, PropertyResult prop, Value* vp) {
131 if (prop.isNotFound()) {
132 return false;
133 }
134
135 PropertyInfo propInfo = prop.propertyInfo();
136 if (!propInfo.isDataProperty()) {
137 return false;
138 }
139
140 *vp = pobj->getSlot(propInfo.slot());
141 return !IsUninitializedLexical(*vp);
142 }
143
144 template <js::GetNameMode mode>
145 inline bool GetEnvironmentName(JSContext* cx, HandleObject envChain,
146 Handle<PropertyName*> name,
147 MutableHandleValue vp) {
148 {
149 PropertyResult prop;
150 NativeObject* pobj = nullptr;
151 if (LookupNameNoGC(cx, name, envChain, &pobj, &prop)) {
152 if (FetchNameNoGC(pobj, prop, vp.address())) {
153 return true;
154 }
155 }
156 }
157
158 PropertyResult prop;
159 RootedObject obj(cx), pobj(cx);
160 if (!LookupName(cx, name, envChain, &obj, &pobj, &prop)) {
161 return false;
162 }
163
164 return FetchName<mode>(cx, obj, pobj, name, prop, vp);
165 }
166
167 inline bool HasOwnProperty(JSContext* cx, HandleValue val, HandleValue idValue,
168 bool* result) {
169 // As an optimization, provide a fast path when rooting is not necessary and
170 // we can safely retrieve the object's shape.
171 jsid id;
172 if (val.isObject() && idValue.isPrimitive() &&
173 PrimitiveValueToId<NoGC>(cx, idValue, &id)) {
174 JSObject* obj = &val.toObject();
175 PropertyResult prop;
176 if (obj->is<NativeObject>() &&
177 NativeLookupOwnProperty<NoGC>(cx, &obj->as<NativeObject>(), id,
178 &prop)) {
179 *result = prop.isFound();
180 return true;
181 }
182 }
183
184 // Step 1.
185 RootedId key(cx);
186 if (!ToPropertyKey(cx, idValue, &key)) {
187 return false;
188 }
189
190 // Step 2.
191 RootedObject obj(cx, ToObject(cx, val));
192 if (!obj) {
193 return false;
194 }
195
196 // Step 3.
197 return HasOwnProperty(cx, obj, key, result);
198 }
199
200 inline bool GetIntrinsicOperation(JSContext* cx, HandleScript script,
201 jsbytecode* pc, MutableHandleValue vp) {
202 Rooted<PropertyName*> name(cx, script->getName(pc));
203 return GlobalObject::getIntrinsicValue(cx, cx->global(), name, vp);
204 }
205
206 inline bool SetIntrinsicOperation(JSContext* cx, JSScript* script,
207 jsbytecode* pc, HandleValue val) {
208 Rooted<PropertyName*> name(cx, script->getName(pc));
209 return GlobalObject::setIntrinsicValue(cx, cx->global(), name, val);
210 }
211
212 inline bool SetNameOperation(JSContext* cx, JSScript* script, jsbytecode* pc,
213 HandleObject env, HandleValue val) {
214 MOZ_ASSERT(JSOp(*pc) == JSOp::SetName || JSOp(*pc) == JSOp::StrictSetName ||
215 JSOp(*pc) == JSOp::SetGName || JSOp(*pc) == JSOp::StrictSetGName);
216 MOZ_ASSERT_IF(
217 JSOp(*pc) == JSOp::SetGName || JSOp(*pc) == JSOp::StrictSetGName,
218 !script->hasNonSyntacticScope());
219 MOZ_ASSERT_IF(
220 JSOp(*pc) == JSOp::SetGName || JSOp(*pc) == JSOp::StrictSetGName,
221 env == cx->global() || env == &cx->global()->lexicalEnvironment() ||
222 env->is<RuntimeLexicalErrorObject>());
223
224 bool strict =
225 JSOp(*pc) == JSOp::StrictSetName || JSOp(*pc) == JSOp::StrictSetGName;
226 Rooted<PropertyName*> name(cx, script->getName(pc));
227
228 // In strict mode, assigning to an undeclared global variable is an
229 // error. To detect this, we call NativeSetProperty directly and pass
230 // Unqualified. It stores the error, if any, in |result|.
231 bool ok;
232 ObjectOpResult result;
233 RootedId id(cx, NameToId(name));
234 RootedValue receiver(cx, ObjectValue(*env));
235 if (env->isUnqualifiedVarObj()) {
236 Rooted<NativeObject*> varobj(cx);
237 if (env->is<DebugEnvironmentProxy>()) {
238 varobj =
239 &env->as<DebugEnvironmentProxy>().environment().as<NativeObject>();
240 } else {
241 varobj = &env->as<NativeObject>();
242 }
243 MOZ_ASSERT(!varobj->getOpsSetProperty());
244 ok = NativeSetProperty<Unqualified>(cx, varobj, id, val, receiver, result);
245 } else {
246 ok = SetProperty(cx, env, id, val, receiver, result);
247 }
248 return ok && result.checkStrictModeError(cx, env, id, strict);
249 }
250
251 inline void InitGlobalLexicalOperation(
252 JSContext* cx, ExtensibleLexicalEnvironmentObject* lexicalEnv,
253 JSScript* script, jsbytecode* pc, HandleValue value) {
254 MOZ_ASSERT_IF(!script->hasNonSyntacticScope(),
255 lexicalEnv == &cx->global()->lexicalEnvironment());
256 MOZ_ASSERT(JSOp(*pc) == JSOp::InitGLexical);
257
258 mozilla::Maybe<PropertyInfo> prop =
259 lexicalEnv->lookup(cx, script->getName(pc));
260 MOZ_ASSERT(prop.isSome());
261 MOZ_ASSERT(IsUninitializedLexical(lexicalEnv->getSlot(prop->slot())));
262
263 lexicalEnv->setSlot(prop->slot(), value);
264 }
265
266 inline bool InitPropertyOperation(JSContext* cx, jsbytecode* pc,
267 HandleObject obj, Handle<PropertyName*> name,
268 HandleValue rhs) {
269 unsigned propAttrs = GetInitDataPropAttrs(JSOp(*pc));
270 return DefineDataProperty(cx, obj, name, rhs, propAttrs);
271 }
272
273 static MOZ_ALWAYS_INLINE bool NegOperation(JSContext* cx,
274 MutableHandleValue val,
275 MutableHandleValue res) {
276 /*
277 * When the operand is int jsval, INT32_FITS_IN_JSVAL(i) implies
278 * INT32_FITS_IN_JSVAL(-i) unless i is 0 or INT32_MIN when the
279 * results, -0.0 or INT32_MAX + 1, are double values.
280 */
281 int32_t i;
282 if (val.isInt32() && (i = val.toInt32()) != 0 && i != INT32_MIN) {
283 res.setInt32(-i);
284 return true;
285 }
286
287 if (!ToNumeric(cx, val)) {
288 return false;
289 }
290
291 if (val.isBigInt()) {
292 return BigInt::negValue(cx, val, res);
293 }
294
295 res.setNumber(-val.toNumber());
296 return true;
297 }
298
299 static MOZ_ALWAYS_INLINE bool IncOperation(JSContext* cx, HandleValue val,
300 MutableHandleValue res) {
301 int32_t i;
302 if (val.isInt32() && (i = val.toInt32()) != INT32_MAX) {
303 res.setInt32(i + 1);
304 return true;
305 }
306
307 if (val.isNumber()) {
308 res.setNumber(val.toNumber() + 1);
309 return true;
310 }
311
312 MOZ_ASSERT(val.isBigInt(), "+1 only callable on result of JSOp::ToNumeric");
313 return BigInt::incValue(cx, val, res);
314 }
315
316 static MOZ_ALWAYS_INLINE bool DecOperation(JSContext* cx, HandleValue val,
317 MutableHandleValue res) {
318 int32_t i;
319 if (val.isInt32() && (i = val.toInt32()) != INT32_MIN) {
320 res.setInt32(i - 1);
321 return true;
322 }
323
324 if (val.isNumber()) {
325 res.setNumber(val.toNumber() - 1);
326 return true;
327 }
328
329 MOZ_ASSERT(val.isBigInt(), "-1 only callable on result of JSOp::ToNumeric");
330 return BigInt::decValue(cx, val, res);
331 }
332
333 static MOZ_ALWAYS_INLINE bool ToPropertyKeyOperation(JSContext* cx,
334 HandleValue idval,
335 MutableHandleValue res) {
336 if (idval.isInt32()) {
337 res.set(idval);
338 return true;
339 }
340
341 RootedId id(cx);
342 if (!ToPropertyKey(cx, idval, &id)) {
343 return false;
344 }
345
346 res.set(IdToValue(id));
347 return true;
348 }
349
350 static MOZ_ALWAYS_INLINE bool GetObjectElementOperation(
351 JSContext* cx, JSOp op, JS::HandleObject obj, JS::HandleValue receiver,
352 HandleValue key, MutableHandleValue res) {
353 MOZ_ASSERT(op == JSOp::GetElem || op == JSOp::GetElemSuper);
354 MOZ_ASSERT_IF(op == JSOp::GetElem, obj == &receiver.toObject());
355
356 do {
357 uint32_t index;
358 if (IsDefinitelyIndex(key, &index)) {
359 if (GetElementNoGC(cx, obj, receiver, index, res.address())) {
360 break;
361 }
362
363 if (!GetElement(cx, obj, receiver, index, res)) {
364 return false;
365 }
366 break;
367 }
368
369 if (key.isString()) {
370 JSAtom* name = AtomizeString(cx, key.toString());
371 if (!name) {
372 return false;
373 }
374 if (name->isIndex(&index)) {
375 if (GetElementNoGC(cx, obj, receiver, index, res.address())) {
376 break;
377 }
378 } else {
379 if (GetPropertyNoGC(cx, obj, receiver, name->asPropertyName(),
380 res.address())) {
381 break;
382 }
383 }
384 }
385
386 RootedId id(cx);
387 if (!ToPropertyKey(cx, key, &id)) {
388 return false;
389 }
390 if (!GetProperty(cx, obj, receiver, id, res)) {
391 return false;
392 }
393 } while (false);
394
395 cx->debugOnlyCheck(res);
396 return true;
397 }
398
399 static MOZ_ALWAYS_INLINE bool GetPrimitiveElementOperation(
400 JSContext* cx, JS::HandleValue receiver, int receiverIndex, HandleValue key,
401 MutableHandleValue res) {
402 // FIXME: Bug 1234324 We shouldn't be boxing here.
403 RootedObject boxed(
404 cx, ToObjectFromStackForPropertyAccess(cx, receiver, receiverIndex, key));
405 if (!boxed) {
406 return false;
407 }
408
409 do {
410 uint32_t index;
411 if (IsDefinitelyIndex(key, &index)) {
412 if (GetElementNoGC(cx, boxed, receiver, index, res.address())) {
413 break;
414 }
415
416 if (!GetElement(cx, boxed, receiver, index, res)) {
417 return false;
418 }
419 break;
420 }
421
422 if (key.isString()) {
423 JSAtom* name = AtomizeString(cx, key.toString());
424 if (!name) {
425 return false;
426 }
427 if (name->isIndex(&index)) {
428 if (GetElementNoGC(cx, boxed, receiver, index, res.address())) {
429 break;
430 }
431 } else {
432 if (GetPropertyNoGC(cx, boxed, receiver, name->asPropertyName(),
433 res.address())) {
434 break;
435 }
436 }
437 }
438
439 RootedId id(cx);
440 if (!ToPropertyKey(cx, key, &id)) {
441 return false;
442 }
443 if (!GetProperty(cx, boxed, receiver, id, res)) {
444 return false;
445 }
446 } while (false);
447
448 cx->debugOnlyCheck(res);
449 return true;
450 }
451
452 static MOZ_ALWAYS_INLINE bool GetElementOperationWithStackIndex(
453 JSContext* cx, HandleValue lref, int lrefIndex, HandleValue rref,
454 MutableHandleValue res) {
455 uint32_t index;
456 if (lref.isString() && IsDefinitelyIndex(rref, &index)) {
457 JSString* str = lref.toString();
458 if (index < str->length()) {
459 str = cx->staticStrings().getUnitStringForElement(cx, str, index);
460 if (!str) {
461 return false;
462 }
463 res.setString(str);
464 return true;
465 }
466 }
467
468 if (lref.isPrimitive()) {
469 return GetPrimitiveElementOperation(cx, lref, lrefIndex, rref, res);
470 }
471
472 RootedObject obj(cx, &lref.toObject());
473 return GetObjectElementOperation(cx, JSOp::GetElem, obj, lref, rref, res);
474 }
475
476 // Wrapper for callVM from JIT.
477 static MOZ_ALWAYS_INLINE bool GetElementOperation(JSContext* cx,
478 HandleValue lref,
479 HandleValue rref,
480 MutableHandleValue res) {
481 return GetElementOperationWithStackIndex(cx, lref, JSDVG_SEARCH_STACK, rref,
482 res);
483 }
484
485 static MOZ_ALWAYS_INLINE JSString* TypeOfOperation(const Value& v,
486 JSRuntime* rt) {
487 JSType type = js::TypeOfValue(v);
488 return TypeName(type, *rt->commonNames);
489 }
490
491 static MOZ_ALWAYS_INLINE bool InitElemOperation(JSContext* cx, jsbytecode* pc,
492 HandleObject obj,
493 HandleValue idval,
494 HandleValue val) {
495 MOZ_ASSERT(!val.isMagic(JS_ELEMENTS_HOLE));
496
497 RootedId id(cx);
498 if (!ToPropertyKey(cx, idval, &id)) {
499 return false;
500 }
501
502 unsigned flags = GetInitDataPropAttrs(JSOp(*pc));
503 return DefineDataProperty(cx, obj, id, val, flags);
504 }
505
506 static MOZ_ALWAYS_INLINE bool CheckPrivateFieldOperation(JSContext* cx,
507 jsbytecode* pc,
508 HandleValue val,
509 HandleValue idval,
510 bool* result) {
511 MOZ_ASSERT(idval.isSymbol());
512 MOZ_ASSERT(idval.toSymbol()->isPrivateName());
513
514 // Result had better not be a nullptr.
515 MOZ_ASSERT(result);
516
517 ThrowCondition condition;
518 ThrowMsgKind msgKind;
519 GetCheckPrivateFieldOperands(pc, &condition, &msgKind);
520
521 // When we are using OnlyCheckRhs, we are implementing PrivateInExpr
522 // This requires we throw if the rhs is not an object;
523 //
524 // The InlineCache for CheckPrivateField already checks for a
525 // non-object rhs and refuses to attach in that circumstance.
526 if (condition == ThrowCondition::OnlyCheckRhs) {
527 if (!val.isObject()) {
528 ReportInNotObjectError(cx, idval, val);
529 return false;
530 }
531 }
532
533 // Invoke the HostEnsureCanAddPrivateElement ( O ) host hook here
534 // if the code is attempting to attach a new private element (which
535 // corresponds to the ThrowHas Throw Condition).
536 if (condition == ThrowCondition::ThrowHas) {
537 if (JS::EnsureCanAddPrivateElementOp op =
538 cx->runtime()->canAddPrivateElement) {
539 if (!op(cx, val)) {
540 return false;
541 }
542 }
543 }
544
545 if (!HasOwnProperty(cx, val, idval, result)) {
546 return false;
547 }
548
549 if (!CheckPrivateFieldWillThrow(condition, *result)) {
550 return true;
551 }
552
553 // Throw!
554 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
555 ThrowMsgKindToErrNum(msgKind));
556 return false;
557 }
558
559 static inline JS::Symbol* NewPrivateName(JSContext* cx, Handle<JSAtom*> name) {
560 return JS::Symbol::new_(cx, JS::SymbolCode::PrivateNameSymbol, name);
561 }
562
563 inline bool InitElemIncOperation(JSContext* cx, Handle<ArrayObject*> arr,
564 uint32_t index, HandleValue val) {
565 if (index == INT32_MAX) {
566 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
567 JSMSG_SPREAD_TOO_LARGE);
568 return false;
569 }
570
571 // If val is a hole, do not call DefineDataElement.
572 if (val.isMagic(JS_ELEMENTS_HOLE)) {
573 // Always call SetLengthProperty even if this is not the last element
574 // initialiser, because this may be followed by a SpreadElement loop,
575 // which will not set the array length if nothing is spread.
576 return SetLengthProperty(cx, arr, index + 1);
577 }
578
579 return DefineDataElement(cx, arr, index, val, JSPROP_ENUMERATE);
580 }
581
582 inline JSFunction* ReportIfNotFunction(
583 JSContext* cx, HandleValue v, MaybeConstruct construct = NO_CONSTRUCT) {
584 if (v.isObject() && v.toObject().is<JSFunction>()) {
585 return &v.toObject().as<JSFunction>();
586 }
587
588 ReportIsNotFunction(cx, v, -1, construct);
589 return nullptr;
590 }
591
592 static inline JSObject* SuperFunOperation(JSObject* callee) {
593 MOZ_ASSERT(callee->as<JSFunction>().isClassConstructor());
594 MOZ_ASSERT(
595 callee->as<JSFunction>().baseScript()->isDerivedClassConstructor());
596
597 return callee->as<JSFunction>().staticPrototype();
598 }
599
600 static inline JSObject* HomeObjectSuperBase(JSObject* homeObj) {
601 MOZ_ASSERT(homeObj->is<PlainObject>() || homeObj->is<JSFunction>());
602
603 return homeObj->staticPrototype();
604 }
605
606 static MOZ_ALWAYS_INLINE bool AddOperation(JSContext* cx,
607 MutableHandleValue lhs,
608 MutableHandleValue rhs,
609 MutableHandleValue res) {
610 if (lhs.isInt32() && rhs.isInt32()) {
611 int32_t l = lhs.toInt32(), r = rhs.toInt32();
612 int32_t t;
613 if (MOZ_LIKELY(SafeAdd(l, r, &t))) {
614 res.setInt32(t);
615 return true;
616 }
617 }
618
619 if (!ToPrimitive(cx, lhs)) {
620 return false;
621 }
622 if (!ToPrimitive(cx, rhs)) {
623 return false;
624 }
625
626 bool lIsString = lhs.isString();
627 bool rIsString = rhs.isString();
628 if (lIsString || rIsString) {
629 JSString* lstr;
630 if (lIsString) {
631 lstr = lhs.toString();
632 } else {
633 lstr = ToString<CanGC>(cx, lhs);
634 if (!lstr) {
635 return false;
636 }
637 }
638
639 JSString* rstr;
640 if (rIsString) {
641 rstr = rhs.toString();
642 } else {
643 // Save/restore lstr in case of GC activity under ToString.
644 lhs.setString(lstr);
645 rstr = ToString<CanGC>(cx, rhs);
646 if (!rstr) {
647 return false;
648 }
649 lstr = lhs.toString();
650 }
651 JSString* str = ConcatStrings<NoGC>(cx, lstr, rstr);
652 if (!str) {
653 RootedString nlstr(cx, lstr), nrstr(cx, rstr);
654 str = ConcatStrings<CanGC>(cx, nlstr, nrstr);
655 if (!str) {
656 return false;
657 }
658 }
659 res.setString(str);
660 return true;
661 }
662
663 if (!ToNumeric(cx, lhs) || !ToNumeric(cx, rhs)) {
664 return false;
665 }
666
667 if (lhs.isBigInt() || rhs.isBigInt()) {
668 return BigInt::addValue(cx, lhs, rhs, res);
669 }
670
671 res.setNumber(lhs.toNumber() + rhs.toNumber());
672 return true;
673 }
674
675 static MOZ_ALWAYS_INLINE bool SubOperation(JSContext* cx,
676 MutableHandleValue lhs,
677 MutableHandleValue rhs,
678 MutableHandleValue res) {
679 if (!ToNumeric(cx, lhs) || !ToNumeric(cx, rhs)) {
680 return false;
681 }
682
683 if (lhs.isBigInt() || rhs.isBigInt()) {
684 return BigInt::subValue(cx, lhs, rhs, res);
685 }
686
687 res.setNumber(lhs.toNumber() - rhs.toNumber());
688 return true;
689 }
690
691 static MOZ_ALWAYS_INLINE bool MulOperation(JSContext* cx,
692 MutableHandleValue lhs,
693 MutableHandleValue rhs,
694 MutableHandleValue res) {
695 if (!ToNumeric(cx, lhs) || !ToNumeric(cx, rhs)) {
696 return false;
697 }
698
699 if (lhs.isBigInt() || rhs.isBigInt()) {
700 return BigInt::mulValue(cx, lhs, rhs, res);
701 }
702
703 res.setNumber(lhs.toNumber() * rhs.toNumber());
704 return true;
705 }
706
707 static MOZ_ALWAYS_INLINE bool DivOperation(JSContext* cx,
708 MutableHandleValue lhs,
709 MutableHandleValue rhs,
710 MutableHandleValue res) {
711 if (!ToNumeric(cx, lhs) || !ToNumeric(cx, rhs)) {
712 return false;
713 }
714
715 if (lhs.isBigInt() || rhs.isBigInt()) {
716 return BigInt::divValue(cx, lhs, rhs, res);
717 }
718
719 res.setNumber(NumberDiv(lhs.toNumber(), rhs.toNumber()));
720 return true;
721 }
722
723 static MOZ_ALWAYS_INLINE bool ModOperation(JSContext* cx,
724 MutableHandleValue lhs,
725 MutableHandleValue rhs,
726 MutableHandleValue res) {
727 int32_t l, r;
728 if (lhs.isInt32() && rhs.isInt32() && (l = lhs.toInt32()) >= 0 &&
729 (r = rhs.toInt32()) > 0) {
730 int32_t mod = l % r;
731 res.setInt32(mod);
732 return true;
733 }
734
735 if (!ToNumeric(cx, lhs) || !ToNumeric(cx, rhs)) {
736 return false;
737 }
738
739 if (lhs.isBigInt() || rhs.isBigInt()) {
740 return BigInt::modValue(cx, lhs, rhs, res);
741 }
742
743 res.setNumber(NumberMod(lhs.toNumber(), rhs.toNumber()));
744 return true;
745 }
746
747 static MOZ_ALWAYS_INLINE bool PowOperation(JSContext* cx,
748 MutableHandleValue lhs,
749 MutableHandleValue rhs,
750 MutableHandleValue res) {
751 if (!ToNumeric(cx, lhs) || !ToNumeric(cx, rhs)) {
752 return false;
753 }
754
755 if (lhs.isBigInt() || rhs.isBigInt()) {
756 return BigInt::powValue(cx, lhs, rhs, res);
757 }
758
759 res.setNumber(ecmaPow(lhs.toNumber(), rhs.toNumber()));
760 return true;
761 }
762
763 static MOZ_ALWAYS_INLINE bool BitNotOperation(JSContext* cx,
764 MutableHandleValue in,
765 MutableHandleValue out) {
766 if (!ToInt32OrBigInt(cx, in)) {
767 return false;
768 }
769
770 if (in.isBigInt()) {
771 return BigInt::bitNotValue(cx, in, out);
772 }
773
774 out.setInt32(~in.toInt32());
775 return true;
776 }
777
778 static MOZ_ALWAYS_INLINE bool BitXorOperation(JSContext* cx,
779 MutableHandleValue lhs,
780 MutableHandleValue rhs,
781 MutableHandleValue out) {
782 if (!ToInt32OrBigInt(cx, lhs) || !ToInt32OrBigInt(cx, rhs)) {
783 return false;
784 }
785
786 if (lhs.isBigInt() || rhs.isBigInt()) {
787 return BigInt::bitXorValue(cx, lhs, rhs, out);
788 }
789
790 out.setInt32(lhs.toInt32() ^ rhs.toInt32());
791 return true;
792 }
793
794 static MOZ_ALWAYS_INLINE bool BitOrOperation(JSContext* cx,
795 MutableHandleValue lhs,
796 MutableHandleValue rhs,
797 MutableHandleValue out) {
798 if (!ToInt32OrBigInt(cx, lhs) || !ToInt32OrBigInt(cx, rhs)) {
799 return false;
800 }
801
802 if (lhs.isBigInt() || rhs.isBigInt()) {
803 return BigInt::bitOrValue(cx, lhs, rhs, out);
804 }
805
806 out.setInt32(lhs.toInt32() | rhs.toInt32());
807 return true;
808 }
809
810 static MOZ_ALWAYS_INLINE bool BitAndOperation(JSContext* cx,
811 MutableHandleValue lhs,
812 MutableHandleValue rhs,
813 MutableHandleValue out) {
814 if (!ToInt32OrBigInt(cx, lhs) || !ToInt32OrBigInt(cx, rhs)) {
815 return false;
816 }
817
818 if (lhs.isBigInt() || rhs.isBigInt()) {
819 return BigInt::bitAndValue(cx, lhs, rhs, out);
820 }
821
822 out.setInt32(lhs.toInt32() & rhs.toInt32());
823 return true;
824 }
825
826 static MOZ_ALWAYS_INLINE bool BitLshOperation(JSContext* cx,
827 MutableHandleValue lhs,
828 MutableHandleValue rhs,
829 MutableHandleValue out) {
830 if (!ToInt32OrBigInt(cx, lhs) || !ToInt32OrBigInt(cx, rhs)) {
831 return false;
832 }
833
834 if (lhs.isBigInt() || rhs.isBigInt()) {
835 return BigInt::lshValue(cx, lhs, rhs, out);
836 }
837
838 // Signed left-shift is undefined on overflow, so |lhs << (rhs & 31)| won't
839 // work. Instead, convert to unsigned space (where overflow is treated
840 // modularly), perform the operation there, then convert back.
841 uint32_t left = static_cast<uint32_t>(lhs.toInt32());
842 uint8_t right = rhs.toInt32() & 31;
843 out.setInt32(mozilla::WrapToSigned(left << right));
844 return true;
845 }
846
847 static MOZ_ALWAYS_INLINE bool BitRshOperation(JSContext* cx,
848 MutableHandleValue lhs,
849 MutableHandleValue rhs,
850 MutableHandleValue out) {
851 if (!ToInt32OrBigInt(cx, lhs) || !ToInt32OrBigInt(cx, rhs)) {
852 return false;
853 }
854
855 if (lhs.isBigInt() || rhs.isBigInt()) {
856 return BigInt::rshValue(cx, lhs, rhs, out);
857 }
858
859 out.setInt32(lhs.toInt32() >> (rhs.toInt32() & 31));
860 return true;
861 }
862
863 static MOZ_ALWAYS_INLINE bool UrshOperation(JSContext* cx,
864 MutableHandleValue lhs,
865 MutableHandleValue rhs,
866 MutableHandleValue out) {
867 if (!ToNumeric(cx, lhs) || !ToNumeric(cx, rhs)) {
868 return false;
869 }
870
871 if (lhs.isBigInt() || rhs.isBigInt()) {
872 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
873 JSMSG_BIGINT_TO_NUMBER);
874 return false;
875 }
876
877 uint32_t left;
878 int32_t right;
879 if (!ToUint32(cx, lhs, &left) || !ToInt32(cx, rhs, &right)) {
880 return false;
881 }
882 left >>= right & 31;
883 out.setNumber(uint32_t(left));
884 return true;
885 }
886
887 static MOZ_ALWAYS_INLINE void InitElemArrayOperation(JSContext* cx,
888 jsbytecode* pc,
889 Handle<ArrayObject*> arr,
890 HandleValue val) {
891 MOZ_ASSERT(JSOp(*pc) == JSOp::InitElemArray);
892
893 // The dense elements must have been initialized up to this index. The JIT
894 // implementation also depends on this.
895 uint32_t index = GET_UINT32(pc);
896 MOZ_ASSERT(index < arr->getDenseCapacity());
897 MOZ_ASSERT(index == arr->getDenseInitializedLength());
898
899 // Bump the initialized length even for hole values to ensure the
900 // index == initLength invariant holds for later InitElemArray ops.
901 arr->setDenseInitializedLength(index + 1);
902
903 if (val.isMagic(JS_ELEMENTS_HOLE)) {
904 arr->initDenseElementHole(index);
905 } else {
906 arr->initDenseElement(index, val);
907 }
908 }
909
910 /*
911 * As an optimization, the interpreter creates a handful of reserved rooted
912 * variables at the beginning, thus inserting them into the Rooted list once
913 * upon entry. ReservedRooted "borrows" a reserved Rooted variable and uses it
914 * within a local scope, resetting the value to nullptr (or the appropriate
915 * equivalent for T) at scope end. This avoids inserting/removing the Rooted
916 * from the rooter list, while preventing stale values from being kept alive
917 * unnecessarily.
918 */
919 template <typename T>
920 class ReservedRooted : public RootedOperations<T, ReservedRooted<T>> {
921 MutableHandle<T> savedRoot;
922
923 public:
924 ReservedRooted(MutableHandle<T> root, const T& ptr) : savedRoot(root) {
925 root.set(ptr);
926 }
927
928 explicit ReservedRooted(MutableHandle<T> root) : savedRoot(root) { clear(); }
929
930 ~ReservedRooted() { clear(); }
931
932 void clear() { savedRoot.set(JS::SafelyInitialized<T>::create()); }
933 void set(const T& p) { savedRoot.set(p); }
934 operator Handle<T>() { return savedRoot; }
935 MutableHandle<T> operator&() { return savedRoot; }
936
937 DECLARE_NONPOINTER_ACCESSOR_METHODS(savedRoot.get())
938 DECLARE_NONPOINTER_MUTABLE_ACCESSOR_METHODS(savedRoot.get())
939 DECLARE_POINTER_CONSTREF_OPS(T)
940 DECLARE_POINTER_ASSIGN_OPS(ReservedRooted, T)
941 };
942
943 } /* namespace js */
944
945 #endif /* vm_Interpreter_inl_h */