class-object-frozen.js 2.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2015 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: --strong-mode

"use strict";

function getClass() {
  class Foo {
    static get bar() { return 0 }
12
    get bar() { return 0 }
13 14 15 16 17
  }
  return Foo;
}

function getClassExpr() {
18
  return (class { static get bar() { return 0 } get bar() { return 0 } });
19 20 21 22 23 24
}

function getClassStrong() {
  "use strong";
  class Foo {
    static get bar() { return 0 }
25
    get bar() { return 0 }
26 27 28 29 30 31
  }
  return Foo;
}

function getClassExprStrong() {
  "use strong";
32
  return (class { static get bar() { return 0 } get bar() { return 0 } });
33 34 35 36 37 38 39 40 41 42 43
}

function addProperty(o) {
  o.baz = 1;
}

function convertPropertyToData(o) {
  assertTrue(o.hasOwnProperty("bar"));
  Object.defineProperty(o, "bar", { value: 1 });
}

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
function testWeakClass(classFunc) {
  assertDoesNotThrow(function(){addProperty(classFunc())});
  assertDoesNotThrow(function(){addProperty(classFunc().prototype)});
  assertDoesNotThrow(function(){convertPropertyToData(classFunc())});
  assertDoesNotThrow(function(){convertPropertyToData(classFunc().prototype)});
}

function testStrongClass(classFunc) {
  assertThrows(function(){addProperty(classFunc())}, TypeError);
  assertThrows(function(){addProperty(classFunc().prototype)}, TypeError);
  assertThrows(function(){convertPropertyToData(classFunc())}, TypeError);
  assertThrows(function(){convertPropertyToData(classFunc().prototype)},
               TypeError);
}

testWeakClass(getClass);
testWeakClass(getClassExpr);
61

62 63
testStrongClass(getClassStrong);
testStrongClass(getClassExprStrong);
64 65 66 67 68

// Check strong classes don't freeze their parents.
(function() {
  let parent = getClass();

69 70 71 72 73 74 75
  let classFunc = function() {
    "use strong";
    class Foo extends parent {
      static get bar() { return 0 }
      get bar() { return 0 }
    }
    return Foo;
76 77
  }

78
  testStrongClass(classFunc);
79 80 81
  assertDoesNotThrow(function(){addProperty(parent)});
  assertDoesNotThrow(function(){convertPropertyToData(parent)});
})();
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

// Check strong classes don't freeze their children.
(function() {
  let parent = getClassStrong();

  let classFunc = function() {
    class Foo extends parent {
      static get bar() { return 0 }
      get bar() { return 0 }
    }
    return Foo;
  }

  assertThrows(function(){addProperty(parent)}, TypeError);
  assertThrows(function(){convertPropertyToData(parent)}, TypeError);
  testWeakClass(classFunc);
})();