2022-01-26 14:31:59 +00:00
|
|
|
// Copyright 2022 the V8 project authors. All rights reserved.
|
2021-05-26 10:43:21 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-11-22 14:22:34 +00:00
|
|
|
// Flags: --experimental-d8-web-snapshot-api --allow-natives-syntax
|
|
|
|
|
2022-03-10 12:09:05 +00:00
|
|
|
'use strict';
|
2021-05-26 10:43:21 +00:00
|
|
|
|
2022-03-10 12:09:05 +00:00
|
|
|
d8.file.execute('test/mjsunit/web-snapshot/web-snapshot-helpers.js');
|
2021-05-26 10:43:21 +00:00
|
|
|
|
2021-06-10 08:44:57 +00:00
|
|
|
(function TestObjectReferencingObject() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.foo = {
|
2021-07-23 16:50:52 +00:00
|
|
|
bar: { baz: 11525 }
|
2021-06-10 08:44:57 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
assertEquals(11525, foo.bar.baz);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestContextReferencingObject() {
|
|
|
|
function createObjects() {
|
|
|
|
function outer() {
|
2021-07-23 16:50:52 +00:00
|
|
|
let o = { value: 11525 };
|
2021-06-10 08:44:57 +00:00
|
|
|
function inner() { return o; }
|
|
|
|
return inner;
|
|
|
|
}
|
|
|
|
globalThis.foo = {
|
|
|
|
func: outer()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
assertEquals(11525, foo.func().value);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestCircularObjectReference() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.foo = {
|
|
|
|
bar: {}
|
|
|
|
};
|
|
|
|
globalThis.foo.bar.circular = globalThis.foo;
|
|
|
|
}
|
|
|
|
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
assertSame(foo, foo.bar.circular);
|
|
|
|
})();
|
2021-06-12 08:39:26 +00:00
|
|
|
|
|
|
|
(function TestArray() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.foo = {
|
|
|
|
array: [5, 6, 7]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
assertEquals([5, 6, 7], foo.array);
|
|
|
|
})();
|
|
|
|
|
2022-05-02 16:21:53 +00:00
|
|
|
(function TestHoleySmiElementsArray() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.foo = [1,,2];
|
|
|
|
}
|
|
|
|
const {foo} = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
assertEquals([1,,2], foo);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestHoleyElementsArray() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.foo = [1,,"123"];
|
|
|
|
}
|
|
|
|
const {foo} = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
assertEquals([1,,"123"], foo);
|
|
|
|
})();
|
|
|
|
|
2022-05-10 11:15:00 +00:00
|
|
|
(function TestDictionaryElementsArray() {
|
|
|
|
function createObjects() {
|
|
|
|
const array = [];
|
|
|
|
// Add a large index to force dictionary elements.
|
|
|
|
array[2 ** 30] = 10;
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
array[i * 101] = i;
|
|
|
|
}
|
|
|
|
globalThis.foo = array;
|
|
|
|
}
|
|
|
|
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
assertTrue(%HasDictionaryElements(foo));
|
|
|
|
assertEquals(2 ** 30 + 1, foo.length);
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
assertEquals(i,foo[i * 101]);
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestDictionaryElementsArrayContainingArray() {
|
|
|
|
function createObjects() {
|
|
|
|
const array = [];
|
|
|
|
// Add a large index to force dictionary elements.
|
|
|
|
array[2 ** 30] = 10;
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
array[i * 101] = [i];
|
|
|
|
}
|
|
|
|
globalThis.foo = array;
|
|
|
|
}
|
|
|
|
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
assertTrue(%HasDictionaryElements(foo));
|
|
|
|
assertEquals(2 ** 30 + 1, foo.length);
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
assertEquals([i],foo[i * 101]);
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestDictionaryElementsArrayContainingObject() {
|
|
|
|
function createObjects() {
|
|
|
|
const array = [];
|
|
|
|
// Add a large index to force dictionary elements.
|
|
|
|
array[2 ** 30] = 10;
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
array[i * 101] = {i:i};
|
|
|
|
}
|
|
|
|
globalThis.foo = array;
|
|
|
|
}
|
|
|
|
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
assertTrue(%HasDictionaryElements(foo));
|
|
|
|
assertEquals(2 ** 30 + 1, foo.length);
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
assertEquals({i:i}, foo[i * 101]);
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestDictionaryElementsArrayContainingFunction() {
|
|
|
|
function createObjects() {
|
|
|
|
const array = [];
|
|
|
|
// Add a large index to force dictionary elements.
|
|
|
|
array[2 ** 30] = 10;
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
array[i * 101] = function() { return i; };
|
|
|
|
}
|
|
|
|
globalThis.foo = array;
|
|
|
|
}
|
|
|
|
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
assertTrue(%HasDictionaryElements(foo));
|
|
|
|
assertEquals(2 ** 30 + 1, foo.length);
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
assertEquals(i, foo[i * 101]());
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestDictionaryElementsArrayContainingString() {
|
|
|
|
function createObjects() {
|
|
|
|
const array = [];
|
|
|
|
// Add a large index to force dictionary elements.
|
|
|
|
array[2 ** 30] = 10;
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
array[i * 101] = `${i}`;
|
|
|
|
}
|
|
|
|
globalThis.foo = array;
|
|
|
|
}
|
|
|
|
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
assertTrue(%HasDictionaryElements(foo));
|
|
|
|
assertEquals(2 ** 30 + 1, foo.length);
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
assertEquals(`${i}`, foo[i * 101]);
|
|
|
|
}
|
|
|
|
})();
|
2022-05-02 16:21:53 +00:00
|
|
|
|
2021-11-09 12:32:36 +00:00
|
|
|
(function TestEmptyArray() {
|
2021-06-12 08:39:26 +00:00
|
|
|
function createObjects() {
|
|
|
|
globalThis.foo = {
|
|
|
|
array: []
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
assertEquals(0, foo.array.length);
|
|
|
|
assertEquals([], foo.array);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestArrayContainingArray() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.foo = {
|
|
|
|
array: [[2, 3], [4, 5]]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
assertEquals([[2, 3], [4, 5]], foo.array);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestArrayContainingObject() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.foo = {
|
2021-07-23 16:50:52 +00:00
|
|
|
array: [{ a: 1 }, { b: 2 }]
|
2021-06-12 08:39:26 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
assertEquals(1, foo.array[0].a);
|
|
|
|
assertEquals(2, foo.array[1].b);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestArrayContainingFunction() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.foo = {
|
2021-07-23 16:50:52 +00:00
|
|
|
array: [function () { return 5; }]
|
2021-06-12 08:39:26 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
assertEquals(5, foo.array[0]());
|
|
|
|
})();
|
|
|
|
|
2022-03-07 08:42:08 +00:00
|
|
|
(function TestInPlaceStringsInArray() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.foo = {
|
|
|
|
array: ['foo', 'bar', 'baz']
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
// We cannot test that the strings are really in-place; that's covered by
|
|
|
|
// cctests.
|
|
|
|
assertEquals('foobarbaz', foo.array.join(''));
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestRepeatedInPlaceStringsInArray() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.foo = {
|
|
|
|
array: ['foo', 'bar', 'foo']
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
// We cannot test that the strings are really in-place; that's covered by
|
|
|
|
// cctests.
|
|
|
|
assertEquals('foobarfoo', foo.array.join(''));
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestInPlaceStringsInObject() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.foo = {a: 'foo', b: 'bar', c: 'baz'};
|
|
|
|
}
|
|
|
|
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
// We cannot test that the strings are really in-place; that's covered by
|
|
|
|
// cctests.
|
|
|
|
assertEquals('foobarbaz', foo.a + foo.b + foo.c);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestRepeatedInPlaceStringsInObject() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.foo = {a: 'foo', b: 'bar', c: 'foo'};
|
|
|
|
}
|
|
|
|
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
// We cannot test that the strings are really in-place; that's covered by
|
|
|
|
// cctests.
|
|
|
|
assertEquals('foobarfoo', foo.a + foo.b + foo.c);
|
|
|
|
})();
|
|
|
|
|
2021-06-12 08:39:26 +00:00
|
|
|
(function TestContextReferencingArray() {
|
|
|
|
function createObjects() {
|
|
|
|
function outer() {
|
|
|
|
let o = [11525];
|
|
|
|
function inner() { return o; }
|
|
|
|
return inner;
|
|
|
|
}
|
|
|
|
globalThis.foo = {
|
|
|
|
func: outer()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const { foo } = takeAndUseWebSnapshot(createObjects, ['foo']);
|
|
|
|
assertEquals(11525, foo.func()[0]);
|
|
|
|
})();
|
2021-07-23 16:50:52 +00:00
|
|
|
|
|
|
|
(function TestEmptyClass() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.Foo = class Foo { };
|
|
|
|
}
|
|
|
|
const { Foo } = takeAndUseWebSnapshot(createObjects, ['Foo']);
|
|
|
|
const x = new Foo();
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestClassWithConstructor() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.Foo = class {
|
|
|
|
constructor() {
|
|
|
|
this.n = 42;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const { Foo } = takeAndUseWebSnapshot(createObjects, ['Foo']);
|
|
|
|
const x = new Foo(2);
|
|
|
|
assertEquals(42, x.n);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestClassWithMethods() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.Foo = class {
|
|
|
|
f() { return 7; };
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const { Foo } = takeAndUseWebSnapshot(createObjects, ['Foo']);
|
|
|
|
const x = new Foo();
|
|
|
|
assertEquals(7, x.f());
|
|
|
|
})();
|
|
|
|
|
|
|
|
(async function TestClassWithAsyncMethods() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.Foo = class {
|
|
|
|
async g() { return 6; };
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const { Foo } = takeAndUseWebSnapshot(createObjects, ['Foo']);
|
|
|
|
const x = new Foo();
|
|
|
|
assertEquals(6, await x.g());
|
|
|
|
})();
|
2021-11-22 12:31:02 +00:00
|
|
|
|
|
|
|
(function TwoExportedObjects() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.one = {x: 1};
|
|
|
|
globalThis.two = {x: 2};
|
|
|
|
}
|
|
|
|
const { one, two } = takeAndUseWebSnapshot(createObjects, ['one', 'two']);
|
|
|
|
assertEquals(1, one.x);
|
|
|
|
assertEquals(2, two.x);
|
|
|
|
})();
|
2021-11-22 14:22:34 +00:00
|
|
|
|
|
|
|
(function TestOptimizingFunctionFromSnapshot() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.f = function(a, b) { return a + b; }
|
|
|
|
}
|
|
|
|
const { f } = takeAndUseWebSnapshot(createObjects, ['f']);
|
|
|
|
%PrepareFunctionForOptimization(f);
|
|
|
|
assertEquals(3, f(1, 2));
|
|
|
|
%OptimizeFunctionOnNextCall(f);
|
|
|
|
assertEquals(4, f(1, 3));
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestOptimizingConstructorFromSnapshot() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.C = class {
|
|
|
|
constructor(a, b) {
|
|
|
|
this.x = a + b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const { C } = takeAndUseWebSnapshot(createObjects, ['C']);
|
|
|
|
%PrepareFunctionForOptimization(C);
|
|
|
|
assertEquals(3, new C(1, 2).x);
|
|
|
|
%OptimizeFunctionOnNextCall(C);
|
|
|
|
assertEquals(4, new C(1, 3).x);
|
|
|
|
})();
|
2021-12-06 13:30:40 +00:00
|
|
|
|
|
|
|
(function TestFunctionPrototype() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.F = function(p1, p2) {
|
|
|
|
this.x = p1 + p2;
|
|
|
|
}
|
|
|
|
globalThis.F.prototype.m = function(p1, p2) {
|
|
|
|
return this.x + p1 + p2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const { F } = takeAndUseWebSnapshot(createObjects, ['F']);
|
|
|
|
const o = new F(1, 2);
|
|
|
|
assertEquals(3, o.x);
|
|
|
|
assertEquals(10, o.m(3, 4));
|
|
|
|
})();
|
2021-12-16 13:55:06 +00:00
|
|
|
|
|
|
|
(function TestFunctionPrototypeBecomesProto() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.F = function() {}
|
|
|
|
globalThis.F.prototype.x = 100;
|
|
|
|
}
|
|
|
|
const { F } = takeAndUseWebSnapshot(createObjects, ['F']);
|
|
|
|
const o = new F();
|
|
|
|
assertEquals(100, Object.getPrototypeOf(o).x);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestFunctionCtorCallsFunctionInPrototype() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.F = function() {
|
|
|
|
this.fooCalled = false;
|
|
|
|
this.foo();
|
|
|
|
}
|
|
|
|
globalThis.F.prototype.foo = function() { this.fooCalled = true; };
|
|
|
|
}
|
|
|
|
const { F } = takeAndUseWebSnapshot(createObjects, ['F']);
|
|
|
|
const o = new F();
|
|
|
|
assertTrue(o.fooCalled);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestFunctionPrototypeConnectedToObjectPrototype() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.F = function() {}
|
|
|
|
}
|
|
|
|
const { F } = takeAndUseWebSnapshot(createObjects, ['F']);
|
|
|
|
const o = new F();
|
|
|
|
assertEquals(Object.prototype,
|
|
|
|
Object.getPrototypeOf(Object.getPrototypeOf(o)));
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestFunctionInheritance() {
|
|
|
|
function createObjects() {
|
|
|
|
globalThis.Super = function() {}
|
|
|
|
globalThis.Super.prototype.superfunc = function() { return 'superfunc'; };
|
|
|
|
globalThis.Sub = function() {}
|
|
|
|
globalThis.Sub.prototype = Object.create(Super.prototype);
|
|
|
|
globalThis.Sub.prototype.subfunc = function() { return 'subfunc'; };
|
|
|
|
}
|
|
|
|
const { Sub } = takeAndUseWebSnapshot(createObjects, ['Sub']);
|
|
|
|
const o = new Sub();
|
|
|
|
assertEquals('superfunc', o.superfunc());
|
|
|
|
assertEquals('subfunc', o.subfunc());
|
|
|
|
})();
|