Skip to content

Commit 0011ca3

Browse files
Tzvetan Mikovfacebook-github-bot
authored andcommitted
Start replacing GCScope with Locals in JSLib
Summary: This diff only adds locals, doesn't remove GCScope yet. Reviewed By: avp Differential Revision: D76012420
1 parent d8a9898 commit 0011ca3

29 files changed

+1598
-822
lines changed

lib/VM/JSLib/ArrayBuffer.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ using std::min;
2626
/// @name Implementation
2727
/// @{
2828

29-
Handle<NativeConstructor> createArrayBufferConstructor(Runtime &runtime) {
29+
HermesValue createArrayBufferConstructor(Runtime &runtime) {
3030
auto arrayBufferPrototype =
3131
Handle<JSObject>::vmcast(&runtime.arrayBufferPrototype);
3232
auto cons = defineSystemConstructor(
@@ -73,7 +73,7 @@ Handle<NativeConstructor> createArrayBufferConstructor(Runtime &runtime) {
7373
arrayBufferIsView,
7474
1);
7575

76-
return cons;
76+
return cons.getHermesValue();
7777
}
7878

7979
CallResult<HermesValue> arrayBufferConstructor(void *, Runtime &runtime) {
@@ -168,6 +168,11 @@ CallResult<HermesValue> arrayBufferPrototypeSlice(void *, Runtime &runtime) {
168168
"Called ArrayBuffer.prototype.slice on a non-ArrayBuffer");
169169
}
170170

171+
struct : public Locals {
172+
PinnedValue<JSArrayBuffer> newBuf;
173+
} lv;
174+
LocalsRAII lraii(runtime, &lv);
175+
171176
// 5. Let len be the value of O’s [[ArrayBufferByteLength]] internal slot.
172177
double len = self->size();
173178
// 6. Let relativeStart be ToIntegerOrInfinity(start).
@@ -209,10 +214,12 @@ CallResult<HermesValue> arrayBufferPrototypeSlice(void *, Runtime &runtime) {
209214
// 15. Let new be Construct(ctor, «newLen»).
210215
// 16. ReturnIfAbrupt(new).
211216

212-
auto newBuf = runtime.makeHandle(JSArrayBuffer::create(
213-
runtime, Handle<JSObject>::vmcast(&runtime.arrayBufferPrototype)));
217+
lv.newBuf.castAndSetHermesValue<JSArrayBuffer>(
218+
JSArrayBuffer::create(
219+
runtime, Handle<JSObject>::vmcast(&runtime.arrayBufferPrototype))
220+
.getHermesValue());
214221

215-
if (JSArrayBuffer::createDataBlock(runtime, newBuf, newLen_int) ==
222+
if (JSArrayBuffer::createDataBlock(runtime, lv.newBuf, newLen_int) ==
216223
ExecutionStatus::EXCEPTION) {
217224
return ExecutionStatus::EXCEPTION;
218225
}
@@ -224,17 +231,17 @@ CallResult<HermesValue> arrayBufferPrototypeSlice(void *, Runtime &runtime) {
224231
// slot < newLen, throw a TypeError exception.
225232
// 21. NOTE: Side-effects of the above steps may have detached O.
226233
// 22. If IsDetachedBuffer(O) is true, throw a TypeError exception.
227-
if (!self->attached() || !newBuf->attached()) {
234+
if (!self->attached() || !lv.newBuf->attached()) {
228235
return runtime.raiseTypeError("Cannot split with detached ArrayBuffers");
229236
}
230237

231238
// 23. Let fromBuf be the value of O’s [[ArrayBufferData]] internal slot.
232239
// 24. Let toBuf be the value of new’s [[ArrayBufferData]] internal slot.
233240
// 25. Perform CopyDataBlockBytes(toBuf, 0, fromBuf, first, newLen).
234241
JSArrayBuffer::copyDataBlockBytes(
235-
runtime, *newBuf, 0, *self, first_int, newLen_int);
242+
runtime, *lv.newBuf, 0, *self, first_int, newLen_int);
236243
// 26. Return new.
237-
return newBuf.getHermesValue();
244+
return lv.newBuf.getHermesValue();
238245
}
239246
/// @}
240247
} // namespace vm

lib/VM/JSLib/AsyncFunction.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,30 @@ CallResult<HermesValue> asyncFunctionConstructor(void *, Runtime &runtime) {
2121
return createDynamicFunction(runtime, args, DynamicFunctionKind::Async);
2222
}
2323

24-
Handle<NativeConstructor> createAsyncFunctionConstructor(Runtime &runtime) {
24+
HermesValue createAsyncFunctionConstructor(Runtime &runtime) {
2525
auto proto = Handle<JSObject>::vmcast(&runtime.asyncFunctionPrototype);
2626

2727
/// 26.7.2 Properties of the AsyncFunction Constructor
2828
/// has a [[Prototype]] internal slot whose value is %Function%.
29-
auto cons = runtime.makeHandle(NativeConstructor::create(
29+
auto consRes = NativeConstructor::create(
3030
runtime,
3131
Handle<JSObject>::vmcast(&runtime.functionConstructor),
3232
nullptr,
3333
asyncFunctionConstructor,
34-
1));
34+
1);
35+
36+
struct : public Locals {
37+
PinnedValue<NativeConstructor> cons;
38+
} lv;
39+
LocalsRAII lraii(runtime, &lv);
40+
41+
lv.cons.castAndSetHermesValue<NativeConstructor>(consRes.getHermesValue());
3542

3643
/// has a "name" property whose value is "AsyncFunction".
3744
/// 26.7.2.1 AsyncFunction.length
3845
/// 26.7.2.2 AsyncFunction.prototype
3946
auto st = Callable::defineNameLengthAndPrototype(
40-
cons,
47+
lv.cons,
4148
runtime,
4249
Predefined::getSymbolID(Predefined::AsyncFunction),
4350
1,
@@ -59,7 +66,7 @@ Handle<NativeConstructor> createAsyncFunctionConstructor(Runtime &runtime) {
5966
runtime,
6067
proto,
6168
Predefined::getSymbolID(Predefined::constructor),
62-
cons,
69+
lv.cons,
6370
dpf);
6471

6572
/// 26.7.3.2 AsyncFunction.prototype [ @@toStringTag ]
@@ -71,7 +78,7 @@ Handle<NativeConstructor> createAsyncFunctionConstructor(Runtime &runtime) {
7178
runtime.getPredefinedStringHandle(Predefined::AsyncFunction),
7279
dpf);
7380

74-
return cons;
81+
return lv.cons.getHermesValue();
7582
}
7683

7784
} // namespace vm

lib/VM/JSLib/Base64.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ CallResult<HermesValue> btoa(void *, Runtime &runtime) {
2525
return ExecutionStatus::EXCEPTION;
2626
}
2727

28-
auto string = runtime.makeHandle(std::move(*res));
28+
struct : public Locals {
29+
PinnedValue<StringPrimitive> string;
30+
} lv;
31+
LocalsRAII lraii(runtime, &lv);
32+
lv.string = std::move(*res);
2933

3034
// Figure out the expected encoded length
31-
uint64_t expectedLength = ((string->getStringLength() + 2) / 3) * 4;
35+
uint64_t expectedLength = ((lv.string->getStringLength() + 2) / 3) * 4;
3236
bool overflow = expectedLength > std::numeric_limits<uint32_t>::max();
3337
if (overflow) {
3438
return runtime.raiseError("String length to convert to base64 is too long");
@@ -40,9 +44,9 @@ CallResult<HermesValue> btoa(void *, Runtime &runtime) {
4044
return ExecutionStatus::EXCEPTION;
4145
}
4246

43-
bool success = string->isASCII()
44-
? base64Encode(string->getStringRef<char>(), *builder)
45-
: base64Encode(string->getStringRef<char16_t>(), *builder);
47+
bool success = lv.string->isASCII()
48+
? base64Encode(lv.string->getStringRef<char>(), *builder)
49+
: base64Encode(lv.string->getStringRef<char16_t>(), *builder);
4650
if (!success) {
4751
return runtime.raiseError(
4852
"Found invalid character when converting to base64");
@@ -61,11 +65,15 @@ CallResult<HermesValue> atob(void *, Runtime &runtime) {
6165
return ExecutionStatus::EXCEPTION;
6266
}
6367

64-
auto string = runtime.makeHandle(std::move(*res));
68+
struct : public Locals {
69+
PinnedValue<StringPrimitive> string;
70+
} lv;
71+
LocalsRAII lraii(runtime, &lv);
72+
lv.string = std::move(*res);
6573

66-
OptValue<uint32_t> expectedLength = string->isASCII()
67-
? base64DecodeOutputLength(string->getStringRef<char>())
68-
: base64DecodeOutputLength(string->getStringRef<char16_t>());
74+
OptValue<uint32_t> expectedLength = lv.string->isASCII()
75+
? base64DecodeOutputLength(lv.string->getStringRef<char>())
76+
: base64DecodeOutputLength(lv.string->getStringRef<char16_t>());
6977
if (!expectedLength) {
7078
return runtime.raiseError("Not a valid base64 encoded string length");
7179
}
@@ -75,9 +83,9 @@ CallResult<HermesValue> atob(void *, Runtime &runtime) {
7583
return ExecutionStatus::EXCEPTION;
7684
}
7785

78-
bool success = string->isASCII()
79-
? base64Decode(string->getStringRef<char>(), *builder)
80-
: base64Decode(string->getStringRef<char16_t>(), *builder);
86+
bool success = lv.string->isASCII()
87+
? base64Decode(lv.string->getStringRef<char>(), *builder)
88+
: base64Decode(lv.string->getStringRef<char16_t>(), *builder);
8189
if (!success) {
8290
return runtime.raiseError(
8391
"Found invalid character when decoding base64 string");

lib/VM/JSLib/BigInt.cpp

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,13 @@ CallResult<HermesValue> bigintConstructor(void *, Runtime &runtime) {
9292
return runtime.raiseTypeError("BigInt is not a constructor");
9393
}
9494

95-
auto hArg0 = runtime.makeHandle(args.getArg(0));
96-
auto prim = toPrimitive_RJS(runtime, hArg0, PreferredType::NUMBER);
95+
struct : public Locals {
96+
PinnedValue<> hArg0;
97+
} lv;
98+
LocalsRAII lraii(runtime, &lv);
99+
100+
lv.hArg0 = args.getArg(0);
101+
auto prim = toPrimitive_RJS(runtime, lv.hArg0, PreferredType::NUMBER);
97102
if (LLVM_UNLIKELY(prim == ExecutionStatus::EXCEPTION)) {
98103
return ExecutionStatus::EXCEPTION;
99104
}
@@ -102,7 +107,7 @@ CallResult<HermesValue> bigintConstructor(void *, Runtime &runtime) {
102107
return numberToBigInt(runtime, prim->getNumber());
103108
}
104109

105-
return toBigInt_RJS(runtime, hArg0);
110+
return toBigInt_RJS(runtime, lv.hArg0);
106111
}
107112

108113
CallResult<HermesValue> bigintPrototypeToLocaleString(
@@ -114,9 +119,15 @@ CallResult<HermesValue> bigintPrototypeToLocaleString(
114119
return ExecutionStatus::EXCEPTION;
115120
}
116121

122+
struct : public Locals {
123+
PinnedValue<> bigintHandle;
124+
} lv;
125+
LocalsRAII lraii(runtime, &lv);
126+
117127
// Call toString, as JSC does.
118128
// TODO(T120187933): Format string according to locale.
119-
auto res = toString_RJS(runtime, runtime.makeHandle(*bigint));
129+
lv.bigintHandle = *bigint;
130+
auto res = toString_RJS(runtime, lv.bigintHandle);
120131
if (LLVM_UNLIKELY(res == ExecutionStatus::EXCEPTION)) {
121132
return ExecutionStatus::EXCEPTION;
122133
}
@@ -133,7 +144,12 @@ CallResult<HermesValue> bigintPrototypeToString(void *, Runtime &runtime) {
133144
return ExecutionStatus::EXCEPTION;
134145
}
135146

136-
Handle<BigIntPrimitive> xHandle = runtime.makeHandle(x->getBigInt());
147+
struct : public Locals {
148+
PinnedValue<BigIntPrimitive> xHandle;
149+
} lv;
150+
LocalsRAII lraii(runtime, &lv);
151+
152+
lv.xHandle.castAndSetHermesValue<BigIntPrimitive>(*x);
137153

138154
// 2. If radix is undefined, let radixMV be 10.
139155
uint32_t radixMV = 10;
@@ -157,7 +173,8 @@ CallResult<HermesValue> bigintPrototypeToString(void *, Runtime &runtime) {
157173

158174
// 5. If radixMV = 10, return ! ToString(x).
159175
// 6. Return the String representation of x.
160-
return BigIntPrimitive::toString(runtime, xHandle, radixMV);
176+
return BigIntPrimitive::toString(
177+
runtime, Handle<BigIntPrimitive>{lv.xHandle}, radixMV);
161178
}
162179

163180
CallResult<HermesValue> bigintPrototypeValueOf(void *, Runtime &runtime) {
@@ -181,8 +198,14 @@ CallResult<HermesValue> bigintTruncate(void *ctx, Runtime &runtime) {
181198
return ExecutionStatus::EXCEPTION;
182199
}
183200

201+
struct : public Locals {
202+
PinnedValue<BigIntPrimitive> bigintHandle;
203+
} lv;
204+
LocalsRAII lraii(runtime, &lv);
205+
206+
lv.bigintHandle.castAndSetHermesValue<BigIntPrimitive>(*bigint);
184207
auto op = reinterpret_cast<TruncateOp>(ctx);
185-
return (*op)(runtime, bits, runtime.makeHandle(bigint->getBigInt()));
208+
return (*op)(runtime, bits, lv.bigintHandle);
186209
}
187210

188211
} // namespace vm

lib/VM/JSLib/Date.cpp

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,12 @@ CallResult<HermesValue> dateConstructor_RJS(void *, Runtime &runtime) {
422422
StringPrimitive::create(runtime, str));
423423
}
424424

425+
struct : public Locals {
426+
PinnedValue<> primitiveValue;
427+
PinnedValue<JSObject> selfParent;
428+
} lv;
429+
LocalsRAII lraii(runtime, &lv);
430+
425431
uint32_t argCount = args.getArgCount();
426432
double finalDate;
427433
if (argCount == 0) {
@@ -439,16 +445,16 @@ CallResult<HermesValue> dateConstructor_RJS(void *, Runtime &runtime) {
439445
if (res == ExecutionStatus::EXCEPTION) {
440446
return ExecutionStatus::EXCEPTION;
441447
}
442-
auto v = runtime.makeHandle(res.getValue());
448+
lv.primitiveValue = res.getValue();
443449

444-
if (v->isString()) {
450+
if (lv.primitiveValue->isString()) {
445451
// Call the String -> Date parsing function.
446452
finalDate = timeClip(parseDate(
447453
StringPrimitive::createStringView(
448-
runtime, Handle<StringPrimitive>::vmcast(v)),
454+
runtime, Handle<StringPrimitive>::vmcast(&lv.primitiveValue)),
449455
runtime.getJSLibStorage()->localTimeOffsetCache));
450456
} else {
451-
auto numRes = toNumber_RJS(runtime, v);
457+
auto numRes = toNumber_RJS(runtime, lv.primitiveValue);
452458
if (numRes == ExecutionStatus::EXCEPTION) {
453459
return ExecutionStatus::EXCEPTION;
454460
}
@@ -475,10 +481,6 @@ CallResult<HermesValue> dateConstructor_RJS(void *, Runtime &runtime) {
475481
return JSDate::create(runtime, finalDate, runtime.datePrototype)
476482
.getHermesValue();
477483
}
478-
struct : public Locals {
479-
PinnedValue<JSObject> selfParent;
480-
} lv;
481-
LocalsRAII lraii(runtime, &lv);
482484
CallResult<PseudoHandle<JSObject>> thisParentRes =
483485
NativeConstructor::parentForNewThis_RJS(
484486
runtime,
@@ -493,13 +495,18 @@ CallResult<HermesValue> dateConstructor_RJS(void *, Runtime &runtime) {
493495

494496
CallResult<HermesValue> dateParse_RJS(void *, Runtime &runtime) {
495497
NativeArgs args = runtime.getCurrentFrame().getNativeArgs();
498+
struct : public Locals {
499+
PinnedValue<StringPrimitive> stringValue;
500+
} lv;
501+
LocalsRAII lraii(runtime, &lv);
502+
496503
auto res = toString_RJS(runtime, args.getArgHandle(0));
497504
if (res == ExecutionStatus::EXCEPTION) {
498505
return ExecutionStatus::EXCEPTION;
499506
}
507+
lv.stringValue = std::move(*res);
500508
return HermesValue::encodeTrustedNumberValue(parseDate(
501-
StringPrimitive::createStringView(
502-
runtime, runtime.makeHandle(std::move(*res))),
509+
StringPrimitive::createStringView(runtime, lv.stringValue),
503510
runtime.getJSLibStorage()->localTimeOffsetCache));
504511
}
505512

@@ -1058,13 +1065,19 @@ CallResult<HermesValue> datePrototypeSetYear_RJS(void *ctx, Runtime &runtime) {
10581065

10591066
CallResult<HermesValue> datePrototypeToJSON_RJS(void *ctx, Runtime &runtime) {
10601067
NativeArgs args = runtime.getCurrentFrame().getNativeArgs();
1068+
struct : public Locals {
1069+
PinnedValue<JSObject> O;
1070+
PinnedValue<> propValue;
1071+
} lv;
1072+
LocalsRAII lraii(runtime, &lv);
1073+
10611074
auto selfHandle = args.getThisHandle();
10621075
auto objRes = toObject(runtime, selfHandle);
10631076
if (objRes == ExecutionStatus::EXCEPTION) {
10641077
return ExecutionStatus::EXCEPTION;
10651078
}
1066-
auto O = runtime.makeHandle<JSObject>(objRes.getValue());
1067-
auto tvRes = toPrimitive_RJS(runtime, O, PreferredType::NUMBER);
1079+
lv.O = vmcast<JSObject>(objRes.getValue());
1080+
auto tvRes = toPrimitive_RJS(runtime, lv.O, PreferredType::NUMBER);
10681081
if (tvRes == ExecutionStatus::EXCEPTION) {
10691082
return ExecutionStatus::EXCEPTION;
10701083
}
@@ -1073,17 +1086,17 @@ CallResult<HermesValue> datePrototypeToJSON_RJS(void *ctx, Runtime &runtime) {
10731086
return HermesValue::encodeNullValue();
10741087
}
10751088
auto propRes = JSObject::getNamed_RJS(
1076-
O, runtime, Predefined::getSymbolID(Predefined::toISOString));
1089+
lv.O, runtime, Predefined::getSymbolID(Predefined::toISOString));
10771090
if (LLVM_UNLIKELY(propRes == ExecutionStatus::EXCEPTION)) {
10781091
return ExecutionStatus::EXCEPTION;
10791092
}
1080-
Handle<Callable> toISO =
1081-
Handle<Callable>::dyn_vmcast(runtime.makeHandle(std::move(*propRes)));
1093+
lv.propValue = std::move(*propRes);
1094+
auto toISO = Handle<Callable>::dyn_vmcast(Handle<>{lv.propValue});
10821095
if (!toISO.get()) {
10831096
return runtime.raiseTypeError(
10841097
"toISOString is not callable in Date.prototype.toJSON()");
10851098
}
1086-
return Callable::executeCall0(toISO, runtime, O).toCallResultHermesValue();
1099+
return Callable::executeCall0(toISO, runtime, lv.O).toCallResultHermesValue();
10871100
}
10881101

10891102
CallResult<HermesValue> datePrototypeSymbolToPrimitive(

0 commit comments

Comments
 (0)