Skip to content

Commit 23cdae6

Browse files
fbmal7facebook-github-bot
authored andcommitted
Store Runtime fields as PinnedValue
Summary: Migrate all the PinnedHermesValues in `RuntimeHermesValueFields.def` to PinnedValue. Use the correct type on each field. `RootAcceptor` is made to be a `friend` of `PinnedValue` so that it can take in a PV but internally simply cast it to a PHV and use that `accept` overload. Reviewed By: neildhar Differential Revision: D59254376 fbshipit-source-id: 4db3175a00cfe6efbd956b01a440d054b132c6b0
1 parent 0544d21 commit 23cdae6

35 files changed

+282
-342
lines changed

include/hermes/VM/Handle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ template <typename T = HermesValue>
168168
class PinnedValue : private PinnedHermesValue {
169169
template <class U>
170170
friend class Handle;
171+
friend struct RootAcceptor;
171172

172173
using traits_type = HermesValueTraits<T>;
173174
using value_type = typename traits_type::value_type;

include/hermes/VM/JSArray.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ class JSArray final : public ArrayImpl {
325325
return createNoAllocPropStorage(
326326
runtime,
327327
prototypeHandle,
328-
*prototypeHandle == runtime.arrayPrototype.getObject()
329-
? Handle<HiddenClass>::vmcast(&runtime.arrayClass)
328+
*prototypeHandle == *runtime.arrayPrototype
329+
? runtime.arrayClass
330330
: createClass(runtime, prototypeHandle),
331331
capacity,
332332
length);

include/hermes/VM/Runtime.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ class Runtime : public RuntimeBase, public HandleRootOwner {
293293
runtimeModuleFlags,
294294
sourceURL,
295295
environment,
296-
Handle<>(&global_));
296+
global_);
297297
}
298298

299299
ExecutionStatus loadSegment(
@@ -559,7 +559,7 @@ class Runtime : public RuntimeBase, public HandleRootOwner {
559559

560560
/// \return `thrownValue`.
561561
HermesValue getThrownValue() const {
562-
return thrownValue_;
562+
return *thrownValue_;
563563
}
564564

565565
/// Set `thrownValue` to the specified value \p value, `returnValue` to
@@ -760,7 +760,7 @@ class Runtime : public RuntimeBase, public HandleRootOwner {
760760

761761
/// @}
762762

763-
#define RUNTIME_HV_FIELD(name) PinnedHermesValue name{};
763+
#define RUNTIME_HV_FIELD(name, type) PinnedValue<type> name{};
764764
#include "hermes/VM/RuntimeHermesValueFields.def"
765765
#undef RUNTIME_HV_FIELD
766766

include/hermes/VM/RuntimeHermesValueFields.def

Lines changed: 65 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -9,95 +9,96 @@
99
#error "RUNTIME_HV_FIELD must be defined"
1010
#endif
1111

12-
RUNTIME_HV_FIELD(objectPrototype)
13-
RUNTIME_HV_FIELD(errorConstructor)
12+
RUNTIME_HV_FIELD(objectPrototype, JSObject)
13+
RUNTIME_HV_FIELD(errorConstructor, NativeConstructor)
1414

1515
/// JSError.prototype, and prototype of all native error types.
16-
#define ALL_ERROR_TYPE(name) RUNTIME_HV_FIELD(name##Prototype)
16+
#define ALL_ERROR_TYPE(name) RUNTIME_HV_FIELD(name##Prototype, JSObject)
1717
#include "hermes/VM/NativeErrorTypes.def"
1818
#undef ALL_ERROR_TYPE
19-
RUNTIME_HV_FIELD(functionPrototype)
20-
RUNTIME_HV_FIELD(functionConstructor)
19+
RUNTIME_HV_FIELD(functionPrototype, NativeFunction)
20+
RUNTIME_HV_FIELD(functionConstructor, NativeConstructor)
2121

22-
RUNTIME_HV_FIELD(stringPrototype)
23-
RUNTIME_HV_FIELD(bigintPrototype)
24-
RUNTIME_HV_FIELD(numberPrototype)
25-
RUNTIME_HV_FIELD(booleanPrototype)
26-
RUNTIME_HV_FIELD(symbolPrototype)
27-
RUNTIME_HV_FIELD(datePrototype)
28-
RUNTIME_HV_FIELD(arrayPrototype)
29-
RUNTIME_HV_FIELD(fastArrayPrototype)
22+
RUNTIME_HV_FIELD(stringPrototype, JSString)
23+
RUNTIME_HV_FIELD(bigintPrototype, JSObject)
24+
RUNTIME_HV_FIELD(numberPrototype, JSNumber)
25+
RUNTIME_HV_FIELD(booleanPrototype, JSBoolean)
26+
RUNTIME_HV_FIELD(symbolPrototype, JSObject)
27+
RUNTIME_HV_FIELD(datePrototype, JSObject)
28+
RUNTIME_HV_FIELD(arrayPrototype, JSArray)
29+
RUNTIME_HV_FIELD(fastArrayPrototype, JSObject)
3030

31-
RUNTIME_HV_FIELD(arrayBufferPrototype)
32-
RUNTIME_HV_FIELD(dataViewPrototype)
33-
RUNTIME_HV_FIELD(typedArrayBasePrototype)
31+
RUNTIME_HV_FIELD(arrayBufferPrototype, JSObject)
32+
RUNTIME_HV_FIELD(dataViewPrototype, JSObject)
33+
RUNTIME_HV_FIELD(typedArrayBasePrototype, JSObject)
3434

3535
/// %TypedArray%.prototype and constructor for each typed array.
36-
#define TYPED_ARRAY(name, type) \
37-
RUNTIME_HV_FIELD(name##ArrayPrototype) \
38-
RUNTIME_HV_FIELD(name##ArrayConstructor)
36+
#define TYPED_ARRAY(name, type) \
37+
RUNTIME_HV_FIELD(name##ArrayPrototype, JSObject) \
38+
RUNTIME_HV_FIELD(name##ArrayConstructor, NativeConstructor)
3939
#include "hermes/VM/TypedArrays.def"
4040
#undef TYPED_ARRAY
4141

42-
RUNTIME_HV_FIELD(setPrototype)
43-
RUNTIME_HV_FIELD(setPrototypeAdd)
44-
RUNTIME_HV_FIELD(setPrototypeValues)
45-
RUNTIME_HV_FIELD(setIteratorPrototype)
46-
RUNTIME_HV_FIELD(mapPrototype)
47-
RUNTIME_HV_FIELD(mapPrototypeSet)
48-
RUNTIME_HV_FIELD(mapPrototypeEntries)
49-
RUNTIME_HV_FIELD(mapIteratorPrototype)
50-
RUNTIME_HV_FIELD(weakMapPrototype)
51-
RUNTIME_HV_FIELD(weakSetPrototype)
52-
RUNTIME_HV_FIELD(weakRefPrototype)
53-
RUNTIME_HV_FIELD(regExpPrototype)
54-
RUNTIME_HV_FIELD(typedArrayBaseConstructor)
42+
RUNTIME_HV_FIELD(setPrototype, JSObject)
43+
RUNTIME_HV_FIELD(setPrototypeAdd, NativeFunction)
44+
RUNTIME_HV_FIELD(setPrototypeValues, NativeFunction)
45+
RUNTIME_HV_FIELD(setIteratorPrototype, JSObject)
46+
RUNTIME_HV_FIELD(mapPrototype, JSObject)
47+
RUNTIME_HV_FIELD(mapPrototypeSet, NativeFunction)
48+
RUNTIME_HV_FIELD(mapPrototypeEntries, NativeFunction)
49+
RUNTIME_HV_FIELD(mapIteratorPrototype, JSObject)
50+
RUNTIME_HV_FIELD(weakMapPrototype, JSObject)
51+
RUNTIME_HV_FIELD(weakSetPrototype, JSObject)
52+
RUNTIME_HV_FIELD(weakRefPrototype, JSObject)
53+
RUNTIME_HV_FIELD(regExpPrototype, JSObject)
54+
RUNTIME_HV_FIELD(typedArrayBaseConstructor, NativeConstructor)
5555

56-
RUNTIME_HV_FIELD(regExpLastInput)
57-
RUNTIME_HV_FIELD(regExpLastRegExp)
56+
RUNTIME_HV_FIELD(regExpLastInput, StringPrimitive)
57+
RUNTIME_HV_FIELD(regExpLastRegExp, JSRegExp)
5858

59-
RUNTIME_HV_FIELD(throwTypeErrorAccessor)
60-
RUNTIME_HV_FIELD(arrayClass)
61-
RUNTIME_HV_FIELD(fastArrayClass)
62-
RUNTIME_HV_FIELD(regExpMatchClass)
59+
RUNTIME_HV_FIELD(throwTypeErrorAccessor, PropertyAccessor)
60+
RUNTIME_HV_FIELD(arrayClass, HiddenClass)
61+
RUNTIME_HV_FIELD(fastArrayClass, HiddenClass)
62+
RUNTIME_HV_FIELD(regExpMatchClass, HiddenClass)
6363

64-
RUNTIME_HV_FIELD(iteratorPrototype)
65-
RUNTIME_HV_FIELD(arrayIteratorPrototype)
66-
RUNTIME_HV_FIELD(arrayPrototypeValues)
67-
RUNTIME_HV_FIELD(asyncFunctionPrototype)
68-
RUNTIME_HV_FIELD(stringIteratorPrototype)
69-
RUNTIME_HV_FIELD(regExpStringIteratorPrototype)
70-
RUNTIME_HV_FIELD(generatorPrototype)
71-
RUNTIME_HV_FIELD(generatorFunctionPrototype)
72-
RUNTIME_HV_FIELD(parseIntFunction)
73-
RUNTIME_HV_FIELD(parseFloatFunction)
74-
RUNTIME_HV_FIELD(requireFunction)
75-
RUNTIME_HV_FIELD(jsErrorStackAccessor)
76-
RUNTIME_HV_FIELD(callSitePrototype)
77-
RUNTIME_HV_FIELD(textEncoderPrototype)
64+
RUNTIME_HV_FIELD(iteratorPrototype, JSObject)
65+
RUNTIME_HV_FIELD(arrayIteratorPrototype, JSObject)
66+
RUNTIME_HV_FIELD(arrayPrototypeValues, NativeFunction)
67+
RUNTIME_HV_FIELD(asyncFunctionPrototype, JSObject)
68+
RUNTIME_HV_FIELD(stringIteratorPrototype, JSObject)
69+
RUNTIME_HV_FIELD(regExpStringIteratorPrototype, JSObject)
70+
RUNTIME_HV_FIELD(generatorPrototype, JSObject)
71+
RUNTIME_HV_FIELD(generatorFunctionPrototype, JSObject)
72+
RUNTIME_HV_FIELD(parseIntFunction, NativeFunction)
73+
RUNTIME_HV_FIELD(parseFloatFunction, NativeFunction)
74+
RUNTIME_HV_FIELD(requireFunction, NativeFunction)
75+
RUNTIME_HV_FIELD(jsErrorStackAccessor, PropertyAccessor)
76+
RUNTIME_HV_FIELD(callSitePrototype, JSObject)
77+
RUNTIME_HV_FIELD(textEncoderPrototype, JSObject)
7878

7979
// TODO: for Serialization/Deserialization after global object initialization
8080
// we record specialCodeBlockDomain_ and create runtimemodule later need to
8181
// revisit this in stage 2
82-
RUNTIME_HV_FIELD(specialCodeBlockDomain_)
82+
RUNTIME_HV_FIELD(specialCodeBlockDomain_, Domain)
8383

84-
RUNTIME_HV_FIELD(global_)
85-
RUNTIME_HV_FIELD(thrownValue_)
86-
RUNTIME_HV_FIELD(keptObjects_)
84+
RUNTIME_HV_FIELD(global_, JSObject)
85+
RUNTIME_HV_FIELD(thrownValue_, HermesValue)
86+
// TODO(T194880545) change to OrderedHashMap
87+
RUNTIME_HV_FIELD(keptObjects_, HermesValue)
8788
#ifdef HERMES_ENABLE_DEBUGGER
88-
RUNTIME_HV_FIELD(debuggerInternalObject_)
89+
RUNTIME_HV_FIELD(debuggerInternalObject_, JSObject)
8990
#endif // HERMES_ENABLE_DEBUGGER
9091

9192
#ifdef HERMES_ENABLE_INTL
9293
// TODO T65916424: move these out of Runtime
93-
RUNTIME_HV_FIELD(intlCollator)
94-
RUNTIME_HV_FIELD(intlCollatorPrototype)
95-
RUNTIME_HV_FIELD(intlDateTimeFormat)
96-
RUNTIME_HV_FIELD(intlDateTimeFormatPrototype)
97-
RUNTIME_HV_FIELD(intlNumberFormat)
98-
RUNTIME_HV_FIELD(intlNumberFormatPrototype)
94+
RUNTIME_HV_FIELD(intlCollator, NativeConstructor)
95+
RUNTIME_HV_FIELD(intlCollatorPrototype, JSObject)
96+
RUNTIME_HV_FIELD(intlDateTimeFormat, NativeConstructor)
97+
RUNTIME_HV_FIELD(intlDateTimeFormatPrototype, JSObject)
98+
RUNTIME_HV_FIELD(intlNumberFormat, NativeConstructor)
99+
RUNTIME_HV_FIELD(intlNumberFormatPrototype, JSObject)
99100
#endif
100101

101-
RUNTIME_HV_FIELD(promiseRejectionTrackingHook_)
102+
RUNTIME_HV_FIELD(promiseRejectionTrackingHook_, HermesValue)
102103

103104
#undef RUNTIME_HV_FIELD

include/hermes/VM/SlotAcceptor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#ifndef HERMES_VM_SLOTACCEPTOR_H
99
#define HERMES_VM_SLOTACCEPTOR_H
1010

11+
#include "hermes/VM/Handle.h"
1112
#include "hermes/VM/HermesValue.h"
1213
#include "hermes/VM/SmallHermesValue.h"
1314
#include "hermes/VM/SymbolID.h"
@@ -58,6 +59,10 @@ struct RootAcceptor : public RootSectionAcceptor {
5859
/// Same as the above, but allows the HermesValue to store a nullptr value.
5960
virtual void acceptNullable(PinnedHermesValue &hv) = 0;
6061
virtual void accept(const RootSymbolID &sym) = 0;
62+
template <typename T>
63+
void acceptNullablePV(PinnedValue<T> &pv) {
64+
acceptNullable(pv);
65+
}
6166

6267
/// When we want to call an acceptor on "raw" root pointers of
6368
/// some JSObject subtype T, this method does the necessary

lib/VM/Callable.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -918,16 +918,14 @@ Handle<NativeJSFunction> NativeJSFunction::create(
918918

919919
/// \return the inferred parent of a Callable based on its \p kind.
920920
static Handle<JSObject> inferredParent(Runtime &runtime, FuncKind kind) {
921-
PinnedHermesValue *parent;
922921
if (kind == FuncKind::Generator) {
923-
parent = &runtime.generatorFunctionPrototype;
922+
return runtime.generatorFunctionPrototype;
924923
} else if (kind == FuncKind::Async) {
925-
parent = &runtime.asyncFunctionPrototype;
924+
return runtime.asyncFunctionPrototype;
926925
} else {
927926
assert(kind == FuncKind::Normal && "Unsupported function kind");
928-
parent = &runtime.functionPrototype;
927+
return runtime.functionPrototype;
929928
}
930-
return Handle<JSObject>::vmcast(parent);
931929
}
932930

933931
Handle<NativeJSFunction> NativeJSFunction::createWithInferredParent(

lib/VM/FastArray.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ CallResult<Handle<FastArray>> FastArray::create(
9292
runtime,
9393
runtime.makeAFixed<FastArray>(
9494
runtime,
95-
Handle<JSObject>::vmcast(&runtime.fastArrayPrototype),
95+
runtime.fastArrayPrototype,
9696
Handle<HiddenClass>::vmcast(&runtime.fastArrayClass),
9797
GCPointerBase::NoBarriers()));
9898

lib/VM/Interpreter-slowpaths.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ ExecutionStatus Interpreter::caseIteratorBegin(
119119
}
120120
PseudoHandle<> slotValue = std::move(*slotValueRes);
121121
if (LLVM_LIKELY(
122-
slotValue->getRaw() == runtime.arrayPrototypeValues.getRaw())) {
122+
slotValue->getRaw() ==
123+
runtime.arrayPrototypeValues.getHermesValue().getRaw())) {
123124
O1REG(IteratorBegin) = HermesValue::encodeTrustedNumberValue(0);
124125
return ExecutionStatus::RETURNED;
125126
}

lib/VM/Interpreter.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,7 @@ static CallResult<HiddenClass *> getHiddenClassForBuffer(
516516
LocalsRAII lraii(runtime, &lv);
517517

518518
lv.clazz = *runtime.getHiddenClassForPrototype(
519-
vmcast<JSObject>(runtime.objectPrototype),
520-
JSObject::numOverlapSlots<JSObject>());
519+
*runtime.objectPrototype, JSObject::numOverlapSlots<JSObject>());
521520

522521
ShapeTableEntry shapeInfo = curCodeBlock->getRuntimeModule()
523522
->getBytecode()
@@ -1518,7 +1517,7 @@ CallResult<HermesValue> Interpreter::interpretFunction(
15181517
O1REG(CoerceThisNS) = O2REG(CoerceThisNS);
15191518
} else if (
15201519
O2REG(CoerceThisNS).isNull() || O2REG(CoerceThisNS).isUndefined()) {
1521-
O1REG(CoerceThisNS) = runtime.global_;
1520+
O1REG(CoerceThisNS) = runtime.global_.getHermesValue();
15221521
} else {
15231522
tmpHandle = O2REG(CoerceThisNS);
15241523
nextIP = NEXTINST(CoerceThisNS);
@@ -1533,7 +1532,7 @@ CallResult<HermesValue> Interpreter::interpretFunction(
15331532
} else if (
15341533
FRAME.getThisArgRef().isNull() ||
15351534
FRAME.getThisArgRef().isUndefined()) {
1536-
O1REG(LoadThisNS) = runtime.global_;
1535+
O1REG(LoadThisNS) = runtime.global_.getHermesValue();
15371536
} else {
15381537
tmpHandle = FRAME.getThisArgRef();
15391538
nextIP = NEXTINST(LoadThisNS);
@@ -1771,11 +1770,11 @@ CallResult<HermesValue> Interpreter::interpretFunction(
17711770
}
17721771

17731772
CASE(Catch) {
1774-
assert(!runtime.thrownValue_.isEmpty() && "Invalid thrown value");
1773+
assert(!runtime.thrownValue_->isEmpty() && "Invalid thrown value");
17751774
assert(
1776-
!isUncatchableError(runtime.thrownValue_) &&
1775+
!isUncatchableError(*runtime.thrownValue_) &&
17771776
"Uncatchable thrown value was caught");
1778-
O1REG(Catch) = runtime.thrownValue_;
1777+
O1REG(Catch) = *runtime.thrownValue_;
17791778
runtime.clearThrownValue();
17801779
#ifdef HERMES_ENABLE_DEBUGGER
17811780
// Signal to the debugger that we're done unwinding an exception,
@@ -1790,7 +1789,7 @@ CallResult<HermesValue> Interpreter::interpretFunction(
17901789
runtime.thrownValue_ = O1REG(Throw);
17911790
SLOW_DEBUG(
17921791
dbgs() << "Exception thrown: "
1793-
<< DumpHermesValue(runtime.thrownValue_) << "\n");
1792+
<< DumpHermesValue(*runtime.thrownValue_) << "\n");
17941793
goto exception;
17951794
}
17961795

@@ -2103,7 +2102,7 @@ CallResult<HermesValue> Interpreter::interpretFunction(
21032102
}
21042103

21052104
CASE(GetGlobalObject) {
2106-
O1REG(GetGlobalObject) = runtime.global_;
2105+
O1REG(GetGlobalObject) = runtime.global_.getHermesValue();
21072106
ip = NEXTINST(GetGlobalObject);
21082107
DISPATCH;
21092108
}
@@ -3002,9 +3001,9 @@ CallResult<HermesValue> Interpreter::interpretFunction(
30023001
Callable::newObject(
30033002
Handle<Callable>::vmcast(&O3REG(CreateThis)),
30043003
runtime,
3005-
Handle<JSObject>::vmcast(
3006-
O2REG(CreateThis).isObject() ? &O2REG(CreateThis)
3007-
: &runtime.objectPrototype)));
3004+
O2REG(CreateThis).isObject()
3005+
? Handle<JSObject>::vmcast(&O2REG(CreateThis))
3006+
: runtime.objectPrototype));
30083007
if (LLVM_UNLIKELY(res == ExecutionStatus::EXCEPTION)) {
30093008
goto exception;
30103009
}
@@ -3480,7 +3479,7 @@ CallResult<HermesValue> Interpreter::interpretFunction(
34803479
Runtime::getEmptyValue()));
34813480
if (LLVM_UNLIKELY(res == ExecutionStatus::EXCEPTION)) {
34823481
if (ip->iIteratorClose.op2 &&
3483-
!isUncatchableError(runtime.thrownValue_)) {
3482+
!isUncatchableError(*runtime.thrownValue_)) {
34843483
// Ignore inner exception.
34853484
runtime.clearThrownValue();
34863485
} else {
@@ -3525,26 +3524,29 @@ CallResult<HermesValue> Interpreter::interpretFunction(
35253524
exception:
35263525
UPDATE_OPCODE_TIME_SPENT;
35273526
assert(
3528-
!runtime.thrownValue_.isEmpty() &&
3527+
!runtime.thrownValue_->isEmpty() &&
35293528
"thrownValue unavailable at exception");
35303529

35313530
bool catchable = true;
35323531
// If this is an Error object that was thrown internally, it didn't have
35333532
// access to the current codeblock and IP, so collect the stack trace here.
3534-
if (auto *jsError = dyn_vmcast<JSError>(runtime.thrownValue_)) {
3533+
if (auto *jsError = dyn_vmcast<JSError>(*runtime.thrownValue_)) {
35353534
catchable = jsError->catchable();
35363535
if (!jsError->getStackTrace()) {
3537-
// Temporarily clear the thrown value for following operations.
3538-
CAPTURE_IP_ASSIGN(
3539-
auto errorHandle,
3540-
runtime.makeHandle(vmcast<JSError>(runtime.thrownValue_)));
3536+
// Temporarily clear the thrown value for following operations. Before
3537+
// we clear it, save the value in tmpHandle.
3538+
tmpHandle = *runtime.thrownValue_;
35413539
runtime.clearThrownValue();
35423540

35433541
CAPTURE_IP(JSError::recordStackTrace(
3544-
errorHandle, runtime, false, curCodeBlock, ip));
3542+
Handle<JSError>::vmcast(tmpHandle),
3543+
runtime,
3544+
false,
3545+
curCodeBlock,
3546+
ip));
35453547

35463548
// Restore the thrown value.
3547-
runtime.setThrownValue(errorHandle.getHermesValue());
3549+
runtime.setThrownValue(*tmpHandle);
35483550
}
35493551
}
35503552

lib/VM/JSArray.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,7 @@ CallResult<Handle<Arguments>> Arguments::create(
436436
P::length,
437437
runtime.makeHandle(HermesValue::encodeTrustedNumberValue(length)));
438438

439-
DEFINE_PROP(
440-
selfHandle, P::SymbolIterator, Handle<>(&runtime.arrayPrototypeValues));
439+
DEFINE_PROP(selfHandle, P::SymbolIterator, runtime.arrayPrototypeValues);
441440

442441
if (strictMode) {
443442
// Define .callee and .caller properties: throw always in strict mode

0 commit comments

Comments
 (0)