Commit 08ce6e5c authored by Vicky Kontoura's avatar Vicky Kontoura Committed by V8 LUCI CQ

[web snapshot] Support top-level non-objects

Currently, the serializer and deserializer assume that all top-level
declarations to be serialized will be objects.

This CL removes this assumption.

Bug: v8:11525, v8:11706
Change-Id: I5acf5e7a3b73aba5ffc5b1d5eb9cb51b3804a4af
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2945178Reviewed-by: 's avatarCamillo Bruni <cbruni@chromium.org>
Reviewed-by: 's avatarMarja Hölttä <marja@chromium.org>
Commit-Queue: Camillo Bruni <cbruni@chromium.org>
Commit-Queue: Vicky Kontoura <vkont@google.com>
Cr-Commit-Position: refs/heads/master@{#75020}
parent 376eb802
...@@ -412,17 +412,22 @@ void WebSnapshotSerializer::SerializePendingObject(Handle<JSObject> object) { ...@@ -412,17 +412,22 @@ void WebSnapshotSerializer::SerializePendingObject(Handle<JSObject> object) {
// Format (serialized export): // Format (serialized export):
// - String id (export name) // - String id (export name)
// - Object id (exported object) // - Serialized value (export value)
void WebSnapshotSerializer::SerializeExport(Handle<JSObject> object, void WebSnapshotSerializer::SerializeExport(Handle<JSObject> object,
Handle<String> export_name) { Handle<String> export_name) {
// TODO(v8:11525): Support exporting functions.
++export_count_; ++export_count_;
uint32_t string_id = 0; uint32_t string_id = 0;
SerializeString(export_name, string_id); SerializeString(export_name, string_id);
uint32_t object_id = 0;
SerializeObject(object, object_id);
export_serializer_.WriteUint32(string_id); export_serializer_.WriteUint32(string_id);
export_serializer_.WriteUint32(object_id); if (object->IsJSPrimitiveWrapper()) {
Handle<JSPrimitiveWrapper> wrapper =
Handle<JSPrimitiveWrapper>::cast(object);
Handle<Object> export_value =
handle(JSPrimitiveWrapper::cast(*wrapper).value(), isolate_);
WriteValue(export_value, export_serializer_);
} else {
WriteValue(object, export_serializer_);
}
} }
// Format (serialized value): // Format (serialized value):
...@@ -901,12 +906,9 @@ void WebSnapshotDeserializer::DeserializeExports() { ...@@ -901,12 +906,9 @@ void WebSnapshotDeserializer::DeserializeExports() {
} }
for (uint32_t i = 0; i < count; ++i) { for (uint32_t i = 0; i < count; ++i) {
Handle<String> export_name = ReadString(true); Handle<String> export_name = ReadString(true);
uint32_t object_id = 0; Handle<Object> export_value;
if (!deserializer_->ReadUint32(&object_id) || object_id >= object_count_) { Representation representation;
Throw("Web snapshot: Malformed export"); ReadValue(export_value, representation);
return;
}
Handle<Object> exported_object = handle(objects_->get(object_id), isolate_);
// Check for the correctness of the snapshot (thus far) before producing // Check for the correctness of the snapshot (thus far) before producing
// something observable. TODO(v8:11525): Strictly speaking, we should // something observable. TODO(v8:11525): Strictly speaking, we should
...@@ -917,7 +919,7 @@ void WebSnapshotDeserializer::DeserializeExports() { ...@@ -917,7 +919,7 @@ void WebSnapshotDeserializer::DeserializeExports() {
} }
auto result = Object::SetProperty(isolate_, isolate_->global_object(), auto result = Object::SetProperty(isolate_, isolate_->global_object(),
export_name, exported_object); export_name, export_value);
if (result.is_null()) { if (result.is_null()) {
Throw("Web snapshot: Setting global property failed"); Throw("Web snapshot: Setting global property failed");
return; return;
......
...@@ -68,6 +68,16 @@ function takeAndUseWebSnapshot(createObjects, exports) { ...@@ -68,6 +68,16 @@ function takeAndUseWebSnapshot(createObjects, exports) {
assertEquals(Number.NEGATIVE_INFINITY, foo.f); assertEquals(Number.NEGATIVE_INFINITY, foo.f);
})(); })();
(function TestTopLevelNumbers() {
function createObjects() {
globalThis.a = 6;
globalThis.b = -7;
}
const { a, b } = takeAndUseWebSnapshot(createObjects, ['a', 'b']);
assertEquals(6, a);
assertEquals(-7, b);
})();
(function TestOddballs() { (function TestOddballs() {
function createObjects() { function createObjects() {
globalThis.foo = { globalThis.foo = {
...@@ -84,6 +94,16 @@ function takeAndUseWebSnapshot(createObjects, exports) { ...@@ -84,6 +94,16 @@ function takeAndUseWebSnapshot(createObjects, exports) {
assertEquals(undefined, foo.d); assertEquals(undefined, foo.d);
})(); })();
(function TestTopLevelOddballs() {
function createObjects() {
globalThis.a = true;
globalThis.b = false;
}
const { a, b } = takeAndUseWebSnapshot(createObjects, ['a', 'b']);
assertTrue(a);
assertFalse(b);
})();
(function TestFunction() { (function TestFunction() {
function createObjects() { function createObjects() {
globalThis.foo = { globalThis.foo = {
...@@ -128,6 +148,18 @@ function takeAndUseWebSnapshot(createObjects, exports) { ...@@ -128,6 +148,18 @@ function takeAndUseWebSnapshot(createObjects, exports) {
assertEquals('snapshot', foo.key()); assertEquals('snapshot', foo.key());
})(); })();
(function TestTopLevelFunctionWithContext() {
function createObjects() {
globalThis.foo = (function () {
let result = 'bar';
function inner() { return result; }
return inner;
})();
}
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
assertEquals('bar', foo());
})();
(function TestRegExp() { (function TestRegExp() {
function createObjects() { function createObjects() {
globalThis.foo = { globalThis.foo = {
...@@ -151,3 +183,13 @@ function takeAndUseWebSnapshot(createObjects, exports) { ...@@ -151,3 +183,13 @@ function takeAndUseWebSnapshot(createObjects, exports) {
assertTrue(foo.re.test('abc')); assertTrue(foo.re.test('abc'));
assertFalse(foo.re.test('ac')); assertFalse(foo.re.test('ac'));
})(); })();
(function TestTopLevelRegExp() {
function createObjects() {
globalThis.re = /ab+c/gi;
}
const { re } = takeAndUseWebSnapshot(createObjects, ['re']);
assertEquals('/ab+c/gi', re.toString());
assertTrue(re.test('aBc'));
assertFalse(re.test('ac'));
})();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment