049844a1c2
This patch implements the runtime semantics of static public class fields. Adds a new InitializeClassFieldsStatement AST node that contains all the static class fields and their initializers. ClassLiteral is now desugared to be included in a do-exp that calls an initializer function which contains this new AST node. Bug: v8:5367 Change-Id: I3574e4c685f1c039de42521c122e24f8d28e5d6c Reviewed-on: https://chromium-review.googlesource.com/714817 Reviewed-by: Adam Klein <adamk@chromium.org> Reviewed-by: Ross McIlroy <rmcilroy@chromium.org> Reviewed-by: Mythri Alle <mythria@chromium.org> Commit-Queue: Sathya Gunasekaran <gsathya@chromium.org> Cr-Commit-Position: refs/heads/master@{#48835}
249 lines
3.5 KiB
JavaScript
249 lines
3.5 KiB
JavaScript
// Copyright 2017 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.
|
|
|
|
// Flags: --harmony-class-fields
|
|
|
|
{
|
|
class C {
|
|
static a;
|
|
}
|
|
|
|
assertEquals(undefined, C.a);
|
|
|
|
let c = new C;
|
|
assertEquals(undefined, c.a);
|
|
}
|
|
|
|
{
|
|
let x = 'a';
|
|
class C {
|
|
static a;
|
|
static b = x;
|
|
static c = 1;
|
|
}
|
|
|
|
assertEquals(undefined, C.a);
|
|
assertEquals('a', C.b);
|
|
assertEquals(1, C.c);
|
|
|
|
let c = new C;
|
|
assertEquals(undefined, c.a);
|
|
assertEquals(undefined, c.b);
|
|
assertEquals(undefined, c.c);
|
|
}
|
|
|
|
{
|
|
class C {
|
|
static c = this;
|
|
static d = () => this;
|
|
}
|
|
|
|
assertEquals(C, C.c);
|
|
assertEquals(C, C.d());
|
|
|
|
let c = new C;
|
|
assertEquals(undefined, c.c);
|
|
assertEquals(undefined, c.d);
|
|
}
|
|
|
|
{
|
|
this.c = 1;
|
|
class C {
|
|
static c = this.c;
|
|
}
|
|
|
|
assertEquals(undefined, C.c);
|
|
|
|
let c = new C;
|
|
assertEquals(undefined, c.c);
|
|
}
|
|
|
|
{
|
|
class C {
|
|
static c = 1;
|
|
static d = this.c;
|
|
}
|
|
|
|
assertEquals(1, C.c);
|
|
assertEquals(1, C.d);
|
|
|
|
let c = new C;
|
|
assertEquals(undefined, c.c);
|
|
assertEquals(undefined, c.d);
|
|
}
|
|
|
|
{
|
|
class C {
|
|
static b = 1;
|
|
static c = () => this.b;
|
|
}
|
|
|
|
assertEquals(1, C.b);
|
|
assertEquals(1, C.c());
|
|
|
|
let c = new C;
|
|
assertEquals(undefined, c.c);
|
|
}
|
|
|
|
{
|
|
let x = 'a';
|
|
class C {
|
|
static b = 1;
|
|
static c = () => this.b;
|
|
static e = () => x;
|
|
}
|
|
|
|
assertEquals(1, C.b);
|
|
assertEquals('a', C.e());
|
|
|
|
let a = {b : 2 };
|
|
assertEquals(1, C.c.call(a));
|
|
|
|
let c = new C;
|
|
assertEquals(undefined, c.b);
|
|
assertEquals(undefined, c.c);
|
|
}
|
|
|
|
{
|
|
let x = 'a';
|
|
class C {
|
|
static c = 1;
|
|
static d = function() { return this.c; };
|
|
static e = function() { return x; };
|
|
}
|
|
|
|
assertEquals(1, C.c);
|
|
assertEquals(1, C.d());
|
|
assertEquals('a', C.e());
|
|
|
|
C.c = 2;
|
|
assertEquals(2, C.d());
|
|
|
|
let a = {c : 3 };
|
|
assertEquals(3, C.d.call(a));
|
|
|
|
assertThrows(C.d.bind(undefined));
|
|
|
|
let c = new C;
|
|
assertEquals(undefined, c.c);
|
|
assertEquals(undefined, c.d);
|
|
assertEquals(undefined, c.e);
|
|
}
|
|
|
|
{
|
|
class C {
|
|
static c = function() { return 1 };
|
|
}
|
|
|
|
assertEquals('c', C.c.name);
|
|
}
|
|
|
|
{
|
|
d = function() { return new.target; }
|
|
class C {
|
|
static c = d;
|
|
}
|
|
|
|
assertEquals(undefined, C.c());
|
|
assertEquals(new d, new C.c());
|
|
}
|
|
|
|
{
|
|
class C {
|
|
static c = () => new.target;
|
|
}
|
|
|
|
assertEquals(undefined, C.c());
|
|
}
|
|
|
|
{
|
|
class C {
|
|
static c = () => {
|
|
let b;
|
|
class A {
|
|
constructor() {
|
|
b = new.target;
|
|
}
|
|
};
|
|
new A;
|
|
assertEquals(A, b);
|
|
}
|
|
}
|
|
|
|
C.c();
|
|
}
|
|
|
|
{
|
|
class C {
|
|
static c = new.target;
|
|
}
|
|
|
|
assertEquals(undefined, C.c);
|
|
}
|
|
|
|
{
|
|
class B {
|
|
static d = 1;
|
|
static b = () => this.d;
|
|
}
|
|
|
|
class C extends B {
|
|
static c = super.d;
|
|
static d = () => super.d;
|
|
static e = () => super.b();
|
|
}
|
|
|
|
assertEquals(1, C.c);
|
|
assertEquals(1, C.d());
|
|
assertEquals(1, C.e());
|
|
}
|
|
|
|
{
|
|
let foo = undefined;
|
|
class B {
|
|
static set d(x) {
|
|
foo = x;
|
|
}
|
|
}
|
|
|
|
class C extends B {
|
|
static d = 2;
|
|
}
|
|
|
|
assertEquals(undefined, foo);
|
|
assertEquals(2, C.d);
|
|
}
|
|
|
|
|
|
|
|
// {
|
|
// let C = class {
|
|
// static c;
|
|
// };
|
|
|
|
// assertEquals("C", C.name);
|
|
// }
|
|
|
|
// {
|
|
// class C {
|
|
// static c = new C;
|
|
// }
|
|
|
|
// assertTrue(c instanceof C);
|
|
// }
|
|
|
|
// (function test() {
|
|
// function makeC() {
|
|
// var x = 1;
|
|
|
|
// return class {
|
|
// static a = () => () => x;
|
|
// }
|
|
// }
|
|
|
|
// let C = makeC();
|
|
// let f = C.a();
|
|
// assertEquals(1, f());
|
|
// })()
|