public-static-class-fields.js 7 KB
Newer Older
1 2 3 4
// 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.

5 6
"use strict";

7 8 9 10 11 12
{
  class C {
    static a;
  }

  assertEquals(undefined, C.a);
13 14 15 16 17
  let descriptor = Object.getOwnPropertyDescriptor(C, 'a');
  assertTrue(C.hasOwnProperty('a'));
  assertTrue(descriptor.writable);
  assertTrue(descriptor.enumerable);
  assertTrue(descriptor.configurable);
18 19 20 21 22 23 24 25 26

  let c = new C;
  assertEquals(undefined, c.a);
}

{
  let x = 'a';
  class C {
    static a;
27
    static hasOwnProperty = function() { return 1; }
28 29 30 31 32 33 34
    static b = x;
    static c = 1;
  }

  assertEquals(undefined, C.a);
  assertEquals('a', C.b);
  assertEquals(1, C.c);
35
  assertEquals(1, C.hasOwnProperty());
36 37 38 39 40 41 42

  let c = new C;
  assertEquals(undefined, c.a);
  assertEquals(undefined, c.b);
  assertEquals(undefined, c.c);
}

43 44 45 46 47 48 49 50 51
{
  assertThrows(() => {
    class C {
      static x = Object.freeze(this);
      static c = 42;
    }
  }, TypeError);
}

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
{
  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);
}

{
  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);
}

{
147
  let d = function() { return new.target; }
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  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);
}


223 224 225 226
{
  let C  = class {
    static c;
  };
227

228 229
  assertEquals("C", C.name);
}
230

231 232 233 234 235 236 237
{
  class C {
    static c = new C;
  }

  assertTrue(C.c instanceof C);
}
238

239 240 241
(function test() {
  function makeC() {
    var x = 1;
242

243 244 245 246
    return class {
      static a = () => () => x;
    }
  }
247

248 249 250 251
  let C = makeC();
  let f = C.a();
  assertEquals(1, f());
})()
252

253
{
254
  let c = "c";
255 256 257 258 259
  class C {
    static ["a"] = 1;
    static ["b"];
    static [c];
  }
260

261 262
  assertEquals(1, C.a);
  assertEquals(undefined, C.b);
263
  assertEquals(undefined, C[c]);
264
}
265 266

{
267 268
  let log = [];
  function run(i) {
269 270 271 272 273
    log.push(i);
    return i;
  }

  class C {
274 275 276 277 278
    static [run(1)] = run(6);
    static [run(2)] = run(7);
    [run(3)]() { run(9);}
    static [run(4)] = run(8);
    static [run(5)]() { throw new Error('should not execute');};
279 280
  }

281
  let c = new C;
282 283 284 285 286 287 288 289 290 291
  c[3]();
  assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], log);
}



function x() {

  // This tests lazy parsing.
  return function() {
292 293
    let log = [];
    function run(i) {
294 295 296 297 298
      log.push(i);
      return i;
    }

    class C {
299 300 301 302 303
      static [run(1)] = run(6);
      static [run(2)] = run(7);
      [run(3)]() { run(9);}
      static [run(4)] = run(8);
      static [run(5)]() { throw new Error('should not execute');};
304 305
    }

306
    let c = new C;
307 308 309 310
    c[3]();
    assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], log);
  }
}
311
x()();
312

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
{
  let log = [];
  function run(i) {
    log.push(i);
    return i;
  }

  class C {
    [run(1)] = run(7);
    [run(2)] = run(8);
    [run(3)]() { run(9);}
    static [run(4)] = run(6);
    [run(5)]() { throw new Error('should not execute');};
  }

  let c = new C;
  c[3]();
  assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], log);
}

function y() {
  // This tests lazy parsing.
  return function() {
    let log = [];
    function run(i) {
      log.push(i);
      return i;
    }

    class C {
      [run(1)] = run(7);
      [run(2)] = run(8);
      [run(3)]() { run(9);}
      static [run(4)] = run(6);
      [run(5)]() { throw new Error('should not execute');};
    }

    let c = new C;
    c[3]();
    assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], log);
  }
}
y()();

357 358 359 360 361 362 363 364 365
{
  class C {}
  class D {
    static [C];
  }

  assertThrows(() => { class X { static [X] } });
  assertEquals(undefined, D[C]);
}
366 367 368 369 370 371 372 373 374 375 376 377

{
  function t() {
    return class {
      static ['x'] = 2;
    }
  }

  let klass = t();
  let obj = new klass;
  assertEquals(2, klass.x);
}
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

{
  let x = 'a';
  class C {
    a;
    b = x;
    c = 1;
    hasOwnProperty() { return 1;}
    static [x] = 2;
    static b = 3;
    static d;
  }

  assertEquals(2, C.a);
  assertEquals(3, C.b);
  assertEquals(undefined, C.d);
  assertEquals(undefined, C.c);

  let c = new C;
  assertEquals(undefined, c.a);
  assertEquals('a', c.b);
  assertEquals(1, c.c);
  assertEquals(undefined, c.d);
  assertEquals(1, c.hasOwnProperty());
}

{
  function t() {
    return class {
      ['x'] = 1;
      static ['x'] = 2;
    }
  }

  let klass = t();
  let obj = new klass;
  assertEquals(1, obj.x);
  assertEquals(2, klass.x);
}
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457


{
  class X {
    static p = function() { return arguments[0]; }
  }

  assertEquals(1, X.p(1));
}

{
  class X {
    static t = () => {
      function p() { return arguments[0]; };
      return p;
    }
  }

  let p = X.t();
  assertEquals(1, p(1));
}

{
  class X {
    static t = () => {
      function p() { return eval("arguments[0]"); };
      return p;
    }
  }

  let p = X.t();
  assertEquals(1, p(1));
}

{
  class X {
    static p = eval("(function() { return arguments[0]; })(1)");
  }

  assertEquals(1, X.p);
}
458 459 460 461 462 463 464 465 466 467 468 469 470

{
  let p = { z: class { static y = this.name } }
  assertEquals(p.z.y, 'z');

  let q = { ["z"]: class { static y = this.name } }
  assertEquals(q.z.y, 'z');

  const C = class {
    static x = this.name;
  }
  assertEquals(C.x, 'C');
}