node/deps/v8/test/mjsunit/proto-accessor-not-accessible.js
Michaël Zasso a7cbf19a82
deps: update V8 to 9.1.269.36
PR-URL: https://github.com/nodejs/node/pull/38273
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Mary Marchini <oss@mmarchini.me>
2021-06-10 11:10:13 +02:00

44 lines
1.6 KiB
JavaScript

// Copyright 2021 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.
// Accessors for __proto__ are defined in Object.prototype (spec:
// https://tc39.es/ecma262/#sec-object.prototype.__proto__ ). If
// Object.prototype is not in the prototype chain of an object, the accessors
// are not accessible. In particular, __proto__ is treated as a normal property
// and the special meaning (that getting __proto__ would return the prototype
// and setting __proto__ would change the prototype) is lost.
function testObjectWithNullProto(object) {
assertNull(Object.getPrototypeOf(object));
// The __proto__ getter is not accessible.
assertEquals(undefined, object.__proto__);
// The __proto__ setter is not accessible. Setting __proto__ will create a
// normal property called __proto__ and not change the prototype.
object.__proto__ = {};
assertNull(Object.getPrototypeOf(object));
// Object.setPrototypeOf can still be used for really setting the prototype.
const proto1 = {};
Object.setPrototypeOf(object, proto1);
// Now the accessors are accessible again.
assertEquals(proto1, object.__proto__);
const proto2 = {};
object.__proto__ = proto2;
assertEquals(proto2, object.__proto__);
}
(function TestObjectCreatedWithObjectCreate() {
testObjectWithNullProto(Object.create(null));
})();
(function TestProtoSetToNullAfterCreation() {
let object_with_null_proto = {};
object_with_null_proto.__proto__ = null;
testObjectWithNullProto(Object.create(null));
})();