Skip to content

Commit

Permalink
Serialize/Deserialize JSRegExp.
Browse files Browse the repository at this point in the history
Summary: Serialize/deserialize JSRegExp.

Reviewed By: avp

Differential Revision: D16663531

fbshipit-source-id: a4b715ee288b576093ba54dbdf4b080b15fa059d
  • Loading branch information
Lun-Liu authored and facebook-github-bot committed Sep 3, 2019
1 parent a21d0c5 commit 3e4b223
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
13 changes: 13 additions & 0 deletions include/hermes/VM/Deserializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ class Deserializer {
offset_ += size;
}

/// Read serialize data and return as an ArrayRef<T>. Read in total \p size
/// elements of type T and increase the offset accordingly.
template <typename T>
ArrayRef<T> readArrayRef(size_t size) {
assert(offset_ + size >= offset_ && "Read overflowed");
assert(
buffer_->getBufferStart() + offset_ + size < buffer_->getBufferEnd() &&
"Deserialize read out of range");
ArrayRef<T> res((const T *)buffer_->getBufferStart() + offset_, size);
offset_ += size * sizeof(T);
return res;
}

template <typename T>
T readInt() {
static_assert(std::is_integral<T>::value, "T must be integral for readInt");
Expand Down
7 changes: 7 additions & 0 deletions include/hermes/VM/JSRegExp.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ class JSRegExp final : public JSObject {
uint32_t searchStartOffset);

private:
#ifdef HERMESVM_SERIALIZE
explicit JSRegExp(Deserializer &d);

friend void RegExpSerialize(Serializer &s, const GCCell *cell);
friend void RegExpDeserialize(Deserializer &d, CellKind kind);
#endif

JSRegExp(Runtime *runtime, JSObject *parent, HiddenClass *clazz)
: JSObject(runtime, &vt.base, parent, clazz) {}

Expand Down
21 changes: 18 additions & 3 deletions lib/VM/JSRegExp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,28 @@ void RegExpBuildMeta(const GCCell *cell, Metadata::Builder &mb) {
}

#ifdef HERMESVM_SERIALIZE
JSRegExp::JSRegExp(Deserializer &d) : JSObject(d, &vt.base) {
size_t size = d.readInt<size_t>();
bytecode_ = d.readArrayRef<uint8_t>(size);
d.readData(&flagBits_, sizeof(flagBits_));
}

void RegExpSerialize(Serializer &s, const GCCell *cell) {
LLVM_DEBUG(llvm::dbgs() << "Serialize function not implemented for RegExp\n");
auto *self = vmcast<const JSRegExp>(cell);
JSObject::serializeObjectImpl(s, cell);
s.writeInt<size_t>(self->bytecode_.size());
s.writeData(self->bytecode_.begin(), self->bytecode_.size());
s.writeData(&self->flagBits_, sizeof(self->flagBits_));

s.endObject(cell);
}

void RegExpDeserialize(Deserializer &d, CellKind kind) {
LLVM_DEBUG(
llvm::dbgs() << "Deserialize function not implemented for RegExp\n");
assert(kind == CellKind::RegExpKind && "Expected RegExp");
void *mem = d.getRuntime()->alloc</*fixedSize*/ true, HasFinalizer::Yes>(
sizeof(JSRegExp));
auto *cell = new (mem) JSRegExp(d);
d.endObject(cell);
}
#endif

Expand Down

0 comments on commit 3e4b223

Please sign in to comment.