Skip to content

Commit 64bcb22

Browse files
neildharfacebook-github-bot
authored andcommitted
Use GC::makeA in PrimitiveBox
Summary: Use new GC::makeA method for PrimitiveBox types. Reviewed By: dulinriley Differential Revision: D23808426 fbshipit-source-id: f32623b12ae50aee1ed16667f41ac02e7ca1ef56
1 parent 31dc389 commit 64bcb22

2 files changed

Lines changed: 54 additions & 61 deletions

File tree

include/hermes/VM/PrimitiveBox.h

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ namespace vm {
1515

1616
/// A container object for primitive HermesValues.
1717
class PrimitiveBox : public JSObject {
18+
friend GC;
19+
1820
public:
1921
using Super = JSObject;
2022

@@ -61,6 +63,8 @@ class PrimitiveBox : public JSObject {
6163

6264
/// String object.
6365
class JSString final : public PrimitiveBox {
66+
friend GC;
67+
6468
public:
6569
using Super = PrimitiveBox;
6670

@@ -105,8 +109,8 @@ class JSString final : public PrimitiveBox {
105109
}
106110

107111
protected:
108-
JSString(Runtime *runtime, JSObject *parent, HiddenClass *clazz)
109-
: PrimitiveBox(runtime, &vt.base, parent, clazz) {
112+
JSString(Runtime *runtime, Handle<JSObject> parent, Handle<HiddenClass> clazz)
113+
: PrimitiveBox(runtime, &vt.base, *parent, *clazz) {
110114
flags_.indexedStorage = true;
111115
flags_.fastIndexProperties = true;
112116
}
@@ -161,6 +165,7 @@ class JSString final : public PrimitiveBox {
161165
/// See ES6 21.1.5.3 for Properties of String Iterator Instances.
162166
class JSStringIterator : public JSObject {
163167
using Super = JSObject;
168+
friend GC;
164169

165170
friend void StringIteratorBuildMeta(
166171
const GCCell *cell,
@@ -192,11 +197,11 @@ class JSStringIterator : public JSObject {
192197

193198
JSStringIterator(
194199
Runtime *runtime,
195-
JSObject *parent,
196-
HiddenClass *clazz,
197-
StringPrimitive *iteratedString)
198-
: JSObject(runtime, &vt.base, parent, clazz),
199-
iteratedString_(runtime, iteratedString, &runtime->getHeap()) {}
200+
Handle<JSObject> parent,
201+
Handle<HiddenClass> clazz,
202+
Handle<StringPrimitive> iteratedString)
203+
: JSObject(runtime, &vt.base, *parent, *clazz),
204+
iteratedString_(runtime, *iteratedString, &runtime->getHeap()) {}
200205

201206
private:
202207
/// [[IteratedString]]
@@ -209,6 +214,8 @@ class JSStringIterator : public JSObject {
209214

210215
/// Number object.
211216
class JSNumber final : public PrimitiveBox {
217+
friend GC;
218+
212219
public:
213220
static ObjectVTable vt;
214221

@@ -230,12 +237,14 @@ class JSNumber final : public PrimitiveBox {
230237
}
231238

232239
protected:
233-
JSNumber(Runtime *runtime, JSObject *parent, HiddenClass *clazz)
234-
: PrimitiveBox(runtime, &vt.base, parent, clazz) {}
240+
JSNumber(Runtime *runtime, Handle<JSObject> parent, Handle<HiddenClass> clazz)
241+
: PrimitiveBox(runtime, &vt.base, *parent, *clazz) {}
235242
};
236243

237244
/// Boolean object.
238245
class JSBoolean final : public PrimitiveBox {
246+
friend GC;
247+
239248
public:
240249
static ObjectVTable vt;
241250

@@ -257,12 +266,17 @@ class JSBoolean final : public PrimitiveBox {
257266
}
258267

259268
protected:
260-
JSBoolean(Runtime *runtime, JSObject *parent, HiddenClass *clazz)
261-
: PrimitiveBox(runtime, &vt.base, parent, clazz) {}
269+
JSBoolean(
270+
Runtime *runtime,
271+
Handle<JSObject> parent,
272+
Handle<HiddenClass> clazz)
273+
: PrimitiveBox(runtime, &vt.base, *parent, *clazz) {}
262274
};
263275

264276
/// Symbol object.
265277
class JSSymbol final : public PrimitiveBox {
278+
friend GC;
279+
266280
public:
267281
static ObjectVTable vt;
268282

@@ -294,8 +308,8 @@ class JSSymbol final : public PrimitiveBox {
294308
friend void SymbolObjectDeserialize(Deserializer &d, CellKind kind);
295309
#endif
296310

297-
JSSymbol(Runtime *runtime, JSObject *parent, HiddenClass *clazz)
298-
: PrimitiveBox(runtime, &vt.base, parent, clazz) {}
311+
JSSymbol(Runtime *runtime, Handle<JSObject> parent, Handle<HiddenClass> clazz)
312+
: PrimitiveBox(runtime, &vt.base, *parent, *clazz) {}
299313
};
300314

301315
} // namespace vm

lib/VM/PrimitiveBox.cpp

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ void StringObjectSerialize(Serializer &s, const GCCell *cell) {
4949

5050
void StringObjectDeserialize(Deserializer &d, CellKind kind) {
5151
assert(kind == CellKind::StringObjectKind && "Expected StringObject");
52-
void *mem = d.getRuntime()->alloc(cellSize<JSString>());
53-
auto *cell = new (mem) JSString(d, &JSString::vt.base);
54-
52+
auto *cell = d.getRuntime()->makeAFixed<JSString>(d, &JSString::vt.base);
5553
d.endObject(cell);
5654
}
5755
#endif
@@ -60,13 +58,11 @@ CallResult<Handle<JSString>> JSString::create(
6058
Runtime *runtime,
6159
Handle<StringPrimitive> value,
6260
Handle<JSObject> parentHandle) {
63-
JSObjectAlloc<JSString> mem{runtime};
64-
auto selfHandle = mem.initToHandle(new (mem) JSString(
65-
runtime,
66-
*parentHandle,
67-
runtime->getHiddenClassForPrototypeRaw(
68-
*parentHandle,
69-
numOverlapSlots<JSString>() + ANONYMOUS_PROPERTY_SLOTS)));
61+
auto clazzHandle = runtime->getHiddenClassForPrototype(
62+
*parentHandle, numOverlapSlots<JSString>() + ANONYMOUS_PROPERTY_SLOTS);
63+
auto obj = runtime->makeAFixed<JSString>(runtime, parentHandle, clazzHandle);
64+
65+
auto selfHandle = JSObjectInit::initToHandle(runtime, obj);
7066

7167
JSObject::setInternalProperty(
7268
*selfHandle,
@@ -233,8 +229,7 @@ void StringIteratorSerialize(Serializer &s, const GCCell *cell) {
233229

234230
void StringIteratorDeserialize(Deserializer &d, CellKind kind) {
235231
assert(kind == CellKind::StringIteratorKind && "Expected StringIterator");
236-
void *mem = d.getRuntime()->alloc(cellSize<JSStringIterator>());
237-
auto *cell = new (mem) JSStringIterator(d);
232+
auto *cell = d.getRuntime()->makeAFixed<JSStringIterator>(d);
238233
d.endObject(cell);
239234
}
240235
#endif
@@ -244,15 +239,11 @@ PseudoHandle<JSStringIterator> JSStringIterator::create(
244239
Runtime *runtime,
245240
Handle<StringPrimitive> string) {
246241
auto proto = Handle<JSObject>::vmcast(&runtime->stringIteratorPrototype);
247-
248-
JSObjectAlloc<JSStringIterator> mem{runtime};
249-
return mem.initToPseudoHandle(new (mem) JSStringIterator(
250-
runtime,
251-
*proto,
252-
runtime->getHiddenClassForPrototypeRaw(
253-
*proto,
254-
numOverlapSlots<JSStringIterator>() + ANONYMOUS_PROPERTY_SLOTS),
255-
*string));
242+
auto clazzHandle = runtime->getHiddenClassForPrototype(
243+
*proto, numOverlapSlots<JSStringIterator>() + ANONYMOUS_PROPERTY_SLOTS);
244+
auto obj = runtime->makeAFixed<JSStringIterator>(
245+
runtime, proto, clazzHandle, string);
246+
return JSObjectInit::initToPseudoHandle(runtime, obj);
256247
}
257248

258249
/// ES6.0 21.1.5.2.1 %StringIteratorPrototype%.next ( ) 4-14
@@ -346,8 +337,7 @@ void NumberObjectSerialize(Serializer &s, const GCCell *cell) {
346337

347338
void NumberObjectDeserialize(Deserializer &d, CellKind kind) {
348339
assert(kind == CellKind::NumberObjectKind && "Expected NumberObject");
349-
void *mem = d.getRuntime()->alloc(cellSize<JSNumber>());
350-
auto *cell = new (mem) JSNumber(d, &JSNumber::vt.base);
340+
auto *cell = d.getRuntime()->makeAFixed<JSNumber>(d, &JSNumber::vt.base);
351341
d.endObject(cell);
352342
}
353343
#endif
@@ -356,13 +346,10 @@ PseudoHandle<JSNumber> JSNumber::create(
356346
Runtime *runtime,
357347
double value,
358348
Handle<JSObject> parentHandle) {
359-
JSObjectAlloc<JSNumber> mem{runtime};
360-
auto self = mem.initToPseudoHandle(new (mem) JSNumber(
361-
runtime,
362-
*parentHandle,
363-
runtime->getHiddenClassForPrototypeRaw(
364-
*parentHandle,
365-
numOverlapSlots<JSNumber>() + ANONYMOUS_PROPERTY_SLOTS)));
349+
auto clazzHandle = runtime->getHiddenClassForPrototype(
350+
*parentHandle, numOverlapSlots<JSNumber>() + ANONYMOUS_PROPERTY_SLOTS);
351+
auto obj = runtime->makeAFixed<JSNumber>(runtime, parentHandle, clazzHandle);
352+
auto self = JSObjectInit::initToPseudoHandle(runtime, obj);
366353

367354
JSObject::setInternalProperty(
368355
self.get(),
@@ -403,21 +390,17 @@ void BooleanObjectSerialize(Serializer &s, const GCCell *cell) {
403390

404391
void BooleanObjectDeserialize(Deserializer &d, CellKind kind) {
405392
assert(kind == CellKind::BooleanObjectKind && "Expected BooleanObject");
406-
void *mem = d.getRuntime()->alloc(cellSize<JSBoolean>());
407-
auto *cell = new (mem) JSBoolean(d, &JSBoolean::vt.base);
393+
auto *cell = d.getRuntime()->makeAFixed<JSBoolean>(d, &JSBoolean::vt.base);
408394
d.endObject(cell);
409395
}
410396
#endif
411397

412398
PseudoHandle<JSBoolean>
413399
JSBoolean::create(Runtime *runtime, bool value, Handle<JSObject> parentHandle) {
414-
JSObjectAlloc<JSBoolean> mem{runtime};
415-
auto self = mem.initToPseudoHandle(new (mem) JSBoolean(
416-
runtime,
417-
*parentHandle,
418-
runtime->getHiddenClassForPrototypeRaw(
419-
*parentHandle,
420-
numOverlapSlots<JSBoolean>() + ANONYMOUS_PROPERTY_SLOTS)));
400+
auto clazzHandle = runtime->getHiddenClassForPrototype(
401+
*parentHandle, numOverlapSlots<JSBoolean>() + ANONYMOUS_PROPERTY_SLOTS);
402+
auto obj = runtime->makeAFixed<JSBoolean>(runtime, parentHandle, clazzHandle);
403+
auto self = JSObjectInit::initToPseudoHandle(runtime, obj);
421404

422405
JSObject::setInternalProperty(
423406
self.get(),
@@ -456,8 +439,7 @@ void SymbolObjectSerialize(Serializer &s, const GCCell *cell) {
456439

457440
void SymbolObjectDeserialize(Deserializer &d, CellKind kind) {
458441
assert(kind == CellKind::SymbolObjectKind && "Expected SymbolObject");
459-
void *mem = d.getRuntime()->alloc(cellSize<JSSymbol>());
460-
auto *cell = new (mem) JSSymbol(d);
442+
auto *cell = d.getRuntime()->makeAFixed<JSSymbol>(d);
461443
d.endObject(cell);
462444
}
463445
#endif
@@ -466,13 +448,10 @@ PseudoHandle<JSSymbol> JSSymbol::create(
466448
Runtime *runtime,
467449
SymbolID value,
468450
Handle<JSObject> parentHandle) {
469-
JSObjectAlloc<JSSymbol> mem{runtime};
470-
auto self = mem.initToPseudoHandle(new (mem) JSSymbol(
471-
runtime,
472-
*parentHandle,
473-
runtime->getHiddenClassForPrototypeRaw(
474-
*parentHandle,
475-
numOverlapSlots<JSSymbol>() + ANONYMOUS_PROPERTY_SLOTS)));
451+
auto clazzHandle = runtime->getHiddenClassForPrototype(
452+
*parentHandle, numOverlapSlots<JSSymbol>() + ANONYMOUS_PROPERTY_SLOTS);
453+
auto *obj = runtime->makeAFixed<JSSymbol>(runtime, parentHandle, clazzHandle);
454+
auto self = JSObjectInit::initToPseudoHandle(runtime, obj);
476455

477456
JSObject::setInternalProperty(
478457
self.get(),

0 commit comments

Comments
 (0)