2022-02-22 13:12:10 +00:00
|
|
|
// Copyright 2022 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2022-08-06 07:56:26 +00:00
|
|
|
// Flags: --experimental-web-snapshots --allow-natives-syntax --verify-heap
|
2022-02-22 13:12:10 +00:00
|
|
|
|
|
|
|
const external_1 = {external: 1};
|
|
|
|
const external_2 = {external: 2};
|
|
|
|
const object = {
|
|
|
|
a: [1,2],
|
|
|
|
b: external_1,
|
|
|
|
c: [external_1, external_2],
|
|
|
|
d: { d_a: external_2 }
|
|
|
|
};
|
|
|
|
|
|
|
|
(function testNoExternals() {
|
2022-04-20 13:37:05 +00:00
|
|
|
const snapshot = WebSnapshot.serialize(object);
|
|
|
|
const deserialized = WebSnapshot.deserialize(snapshot);
|
2022-02-22 13:12:10 +00:00
|
|
|
%HeapObjectVerify(deserialized);
|
2022-09-07 11:13:42 +00:00
|
|
|
assertEquals(object, deserialized);
|
|
|
|
assertEquals(external_1, deserialized.b);
|
|
|
|
assertNotSame(external_1, deserialized.b);
|
|
|
|
assertEquals(external_2, deserialized.d.d_a);
|
|
|
|
assertNotSame(external_2, deserialized.d.d_a);
|
2022-02-22 13:12:10 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function testOneExternals() {
|
2022-09-07 11:13:42 +00:00
|
|
|
const externals = [external_1];
|
2022-04-20 13:37:05 +00:00
|
|
|
const snapshot = WebSnapshot.serialize(object, externals);
|
2022-02-22 13:12:10 +00:00
|
|
|
const replaced_externals = [{replacement:1}]
|
2022-04-20 13:37:05 +00:00
|
|
|
const deserialized = WebSnapshot.deserialize(snapshot, replaced_externals);
|
2022-02-22 13:12:10 +00:00
|
|
|
%HeapObjectVerify(deserialized);
|
2022-09-07 11:13:42 +00:00
|
|
|
assertEquals(object.a, deserialized.a);
|
|
|
|
assertSame(replaced_externals[0], deserialized.b);
|
|
|
|
assertArrayEquals([replaced_externals[0], external_2], deserialized.c);
|
|
|
|
assertSame(replaced_externals[0], deserialized.c[0]);
|
|
|
|
assertNotSame(external_2, deserialized.c[1]);
|
|
|
|
assertEquals(external_2, deserialized.d.d_a);
|
|
|
|
assertNotSame(external_2, deserialized.d.d_a);
|
2022-02-22 13:12:10 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function testTwoExternals() {
|
|
|
|
const externals = [external_1, external_2];
|
2022-04-20 13:37:05 +00:00
|
|
|
const snapshot = WebSnapshot.serialize(object, externals);
|
2022-02-22 13:12:10 +00:00
|
|
|
const replaced_externals = [{replacement:1}, {replacement:2}]
|
2022-04-20 13:37:05 +00:00
|
|
|
const deserialized = WebSnapshot.deserialize(snapshot, replaced_externals);
|
2022-02-22 13:12:10 +00:00
|
|
|
%HeapObjectVerify(deserialized);
|
2022-09-07 11:13:42 +00:00
|
|
|
assertEquals(object.a, deserialized.a);
|
2022-02-22 13:12:10 +00:00
|
|
|
assertSame(deserialized.b, replaced_externals[0]);
|
2022-09-07 11:13:42 +00:00
|
|
|
assertArrayEquals(replaced_externals, deserialized.c);
|
|
|
|
assertSame(replaced_externals[0], deserialized.c[0]);
|
|
|
|
assertSame(replaced_externals[1], deserialized.c[1]);
|
|
|
|
assertSame(replaced_externals[1], deserialized.d.d_a);
|
2022-02-22 13:12:10 +00:00
|
|
|
})();
|
2022-02-24 10:35:02 +00:00
|
|
|
|
|
|
|
(function testApiObject() {
|
|
|
|
const api_object = new d8.dom.Div();
|
|
|
|
const source_1 = [{}, api_object];
|
2022-04-20 13:37:05 +00:00
|
|
|
assertThrows(() => WebSnapshot.serialize(source_1));
|
2022-02-24 10:35:02 +00:00
|
|
|
|
|
|
|
let externals = [external_1]
|
|
|
|
const source_2 = [{}, external_1, api_object, api_object];
|
2022-04-20 13:37:05 +00:00
|
|
|
const snapshot_2 = WebSnapshot.serialize(source_2, externals);
|
2022-02-24 10:35:02 +00:00
|
|
|
%HeapObjectVerify(externals);
|
|
|
|
// Check that the unhandled api object is added to the externals.
|
2022-09-07 11:13:42 +00:00
|
|
|
assertArrayEquals([external_1, api_object], externals);
|
2022-02-24 10:35:02 +00:00
|
|
|
|
2022-04-20 13:37:05 +00:00
|
|
|
assertThrows(() => WebSnapshot.deserialize(snapshot_2));
|
|
|
|
assertThrows(() => WebSnapshot.deserialize(snapshot_2, []));
|
|
|
|
assertThrows(() => WebSnapshot.deserialize(snapshot_2, [external_1]));
|
2022-02-24 10:35:02 +00:00
|
|
|
|
2022-04-20 13:37:05 +00:00
|
|
|
const result_2 = WebSnapshot.deserialize(snapshot_2, [external_1, api_object]);
|
2022-02-24 10:35:02 +00:00
|
|
|
%HeapObjectVerify(externals);
|
|
|
|
%HeapObjectVerify(result_2);
|
2022-09-07 11:13:42 +00:00
|
|
|
assertArrayEquals(source_2, result_2);
|
|
|
|
assertNotSame(source_2[0], result_2[0]);
|
|
|
|
assertSame(external_1, result_2[1]);
|
|
|
|
assertSame(api_object, result_2[2]);
|
|
|
|
assertSame(api_object, result_2[3]);
|
2022-02-24 10:35:02 +00:00
|
|
|
})();
|