Files
sdk/tests/web/native/super_call_test.dart
Nicholas Shahan c2a879e41d [tests] Cleanup .__proto__ use in native tests
The existing usage will no longer work as expected once the security
measures to prevent native Object prototype pollution are enabled.

The recommended replacements are `Object.getPrototypeOf()` and
`Object.setPrototypeOf()`.

Change-Id: I0617233bd31ccd2afc9f94c527eecb31c5f1b340
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/330047
Reviewed-by: Srujan Gaddam <srujzs@google.com>
Commit-Queue: Nicholas Shahan <nshahan@google.com>
2023-10-11 20:37:57 +00:00

89 lines
2.0 KiB
Dart

// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "native_testing.dart";
// Test to see if resolving a hidden native class's method interferes with
// subsequent resolving the subclass's method. This might happen if the
// superclass caches the method in the prototype, so shadowing the dispatcher
// stored on Object.prototype.
@Native("A")
class A {
foo() => 'A.foo ${bar()}';
bar() => 'A.bar';
}
@Native("B")
class B extends A {
bar() => 'B.bar';
}
@Native("C")
class C extends B {
foo() => 'C.foo; super.foo = ${super.foo()}';
bar() => 'C.bar';
}
@Native("D")
class D extends C {
bar() => 'D.bar';
}
makeA() native;
makeB() native;
makeC() native;
makeD() native;
void setup() {
JS('', r"""
(function(){
// This code is inside 'setup' and so not accessible from the global scope.
function inherits(child, parent) {
if (Object.getPrototypeOf(child.prototype)) {
Object.setPrototypeOf(child.prototype, parent.prototype);
} else {
function tmp() {};
tmp.prototype = parent.prototype;
child.prototype = new tmp();
child.prototype.constructor = child;
}
}
function A(){}
function B(){}
inherits(B, A);
function C(){}
inherits(C, B);
function D(){}
inherits(D, C);
self.makeA = function(){return new A()};
self.makeB = function(){return new B()};
self.makeC = function(){return new C()};
self.makeD = function(){return new D()};
self.nativeConstructor(A);
self.nativeConstructor(B);
self.nativeConstructor(C);
self.nativeConstructor(D);
})()""");
applyTestExtensions(['A', 'B', 'C', 'D']);
}
main() {
nativeTesting();
setup();
var a = makeA();
var b = makeB();
var c = makeC();
var d = makeD();
Expect.equals('A.foo A.bar', a.foo());
Expect.equals('A.foo B.bar', b.foo());
Expect.equals('C.foo; super.foo = A.foo C.bar', c.foo());
Expect.equals('C.foo; super.foo = A.foo D.bar', d.foo());
}