1// Copyright 2015 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5// Flags: --strong-mode --allow-natives-syntax 6// Flags: --harmony-destructuring-bind 7 8'use strict'; 9 10(function WeakObjectLiterals() { 11 function assertWeakObject(x) { 12 assertFalse(%IsStrong(x)); 13 assertSame(Object.prototype, Object.getPrototypeOf(x)); 14 } 15 assertWeakObject({}); 16 assertWeakObject({a: 0, b: 0}); 17 assertWeakObject({a: [], b: {}}); 18 assertWeakObject({a: [], b: {}}.b); 19 assertWeakObject({a: {b: {c: {}}}}.a); 20 assertWeakObject({a: {b: {c: {}}}}.a.b); 21 assertWeakObject({a: {b: {c: {}}}}.a.b.c); 22 assertWeakObject([[1], {}, [[3]]][1]); 23 assertWeakObject({f: function(){}}); 24 assertWeakObject( 25 Realm.eval(Realm.current(), "({f: function(){}})")); 26})(); 27 28(function StrongObjectLiterals() { 29 'use strong'; 30 function assertStrongObject(x) { 31 assertTrue(%IsStrong(x)); 32 assertSame(Object.prototype, Object.getPrototypeOf(x)); 33 } 34 assertStrongObject({}); 35 assertStrongObject({a: 0, b: 0}); 36 assertStrongObject({a: [], b: {}}); 37 assertStrongObject({a: [], b: {}}.b); 38 assertStrongObject({a: {b: {c: {}}}}.a); 39 assertStrongObject({a: {b: {c: {}}}}.a.b); 40 assertStrongObject({a: {b: {c: {}}}}.a.b.c); 41 // Maps for literals with too many properties are not cached. 42 assertStrongObject({ 43 x001: 0, x002: 0, x003: 0, x004: 0, x005: 0, 44 x006: 0, x007: 0, x008: 0, x009: 0, x010: 0, 45 x011: 0, x012: 0, x013: 0, x014: 0, x015: 0, 46 x016: 0, x017: 0, x018: 0, x019: 0, x020: 0, 47 x021: 0, x022: 0, x023: 0, x024: 0, x025: 0, 48 x026: 0, x027: 0, x028: 0, x029: 0, x030: 0, 49 x031: 0, x032: 0, x033: 0, x034: 0, x035: 0, 50 x036: 0, x037: 0, x038: 0, x039: 0, x040: 0, 51 x041: 0, x042: 0, x043: 0, x044: 0, x045: 0, 52 x046: 0, x047: 0, x048: 0, x049: 0, x050: 0, 53 x051: 0, x052: 0, x053: 0, x054: 0, x055: 0, 54 x056: 0, x057: 0, x058: 0, x059: 0, x060: 0, 55 x061: 0, x062: 0, x063: 0, x064: 0, x065: 0, 56 x066: 0, x067: 0, x068: 0, x069: 0, x070: 0, 57 x071: 0, x072: 0, x073: 0, x074: 0, x075: 0, 58 x076: 0, x077: 0, x078: 0, x079: 0, x080: 0, 59 x081: 0, x082: 0, x083: 0, x084: 0, x085: 0, 60 x086: 0, x087: 0, x088: 0, x089: 0, x090: 0, 61 x091: 0, x092: 0, x093: 0, x094: 0, x095: 0, 62 x096: 0, x097: 0, x098: 0, x099: 0, x100: 0, 63 x101: 0, x102: 0, x103: 0, x104: 0, x105: 0, 64 x106: 0, x107: 0, x108: 0, x109: 0, x110: 0, 65 x111: 0, x112: 0, x113: 0, x114: 0, x115: 0, 66 x116: 0, x117: 0, x118: 0, x119: 0, x120: 0, 67 x121: 0, x122: 0, x123: 0, x124: 0, x125: 0, 68 x126: 0, x127: 0, x128: 0, x129: 0, x130: 0, 69 x131: 0, x132: 0, x133: 0, x134: 0, x135: 0, 70 x136: 0, x137: 0, x138: 0, x139: 0, x140: 0, 71 x141: 0, x142: 0, x143: 0, x144: 0, x145: 0, 72 x146: 0, x147: 0, x148: 0, x149: 0, x150: 0, 73 x151: 0, x152: 0, x153: 0, x154: 0, x155: 0, 74 x156: 0, x157: 0, x158: 0, x159: 0, x160: 0, 75 x161: 0, x162: 0, x163: 0, x164: 0, x165: 0, 76 x166: 0, x167: 0, x168: 0, x169: 0, x170: 0, 77 x171: 0, x172: 0, x173: 0, x174: 0, x175: 0, 78 x176: 0, x177: 0, x178: 0, x179: 0, x180: 0, 79 x181: 0, x182: 0, x183: 0, x184: 0, x185: 0, 80 x186: 0, x187: 0, x188: 0, x189: 0, x190: 0, 81 x191: 0, x192: 0, x193: 0, x194: 0, x195: 0, 82 x196: 0, x197: 0, x198: 0, x199: 0, x200: 0, 83 }); 84 assertStrongObject([[1], {}, [[3]]][1]); 85 assertStrongObject({[Date() + ""]: 0, [Symbol()]: 0}); 86 assertStrongObject({m() { super.m() }}); 87 assertTrue(%IsStrong({__proto__: {}, get a() {}, set b(x) {}})); 88 // Object literals with constant functions are treated specially, 89 // but currently only on the toplevel (using Realm.eval to emulate that). 90 assertStrongObject({f: function(){}}); 91 assertStrongObject( 92 Realm.eval(Realm.current(), "'use strong'; ({f: function(){}})")); 93})(); 94 95(function WeakArrayLiterals(...args) { 96 function assertWeakArray(x) { 97 assertFalse(%IsStrong(x)); 98 assertSame(Array.prototype, Object.getPrototypeOf(x)); 99 } 100 let [...r] = []; 101 assertWeakArray(args); 102 assertWeakArray(r); 103 assertWeakArray([]); 104 assertWeakArray([1, 2, 3]); 105 assertWeakArray([1, 2, ...[3, 4], 5]); 106 assertWeakArray([[[]]]); 107 assertWeakArray([[1], {}, [[3]]]); 108 assertWeakArray([[1], {}, [[3]]][0]); 109 assertWeakArray([[1], {}, [[3]]][2]); 110 assertWeakArray([[1], {}, [[3]]][2][0]); 111 assertWeakArray({a: [], b: {}}.a); 112})(); 113 114(function StrongArrayLiterals() { 115 'use strong'; 116 function assertStrongArray(x) { 117 assertTrue(%IsStrong(x)); 118 assertSame(Array.prototype, Object.getPrototypeOf(x)); 119 } 120 let [...r] = []; 121 assertStrongArray((function(...a) { return a; })()); 122 assertStrongArray(r); 123 assertStrongArray([]); 124 assertStrongArray([1, 2, 3]); 125 assertStrongArray([1, 2, ...[3, 4], 5]); 126 assertStrongArray([[[]]]); 127 assertStrongArray([[1], {}, [[3]]]); 128 assertStrongArray([[1], {}, [[3]]][0]); 129 assertStrongArray([[1], {}, [[3]]][2]); 130 assertStrongArray([[1], {}, [[3]]][2][0]); 131 assertStrongArray({a: [], b: {}}.a); 132})(); 133 134(function WeakFunctionLiterals() { 135 function assertWeakFunction(x) { 136 assertFalse(%IsStrong(x)); 137 assertFalse(%IsStrong(x.prototype)); 138 assertSame(Function.prototype, Object.getPrototypeOf(x)); 139 } 140 function f() {} 141 assertWeakFunction(f); 142 assertWeakFunction(function(){}); 143 assertWeakFunction(function f(){}); 144 assertWeakFunction(() => {}); 145 assertWeakFunction(x => x); 146 assertWeakFunction({m(){}}.m); 147 assertWeakFunction(Object.getOwnPropertyDescriptor( 148 {get a(){}}, 'a').get); 149 assertWeakFunction(Object.getOwnPropertyDescriptor( 150 {set a(x){}}, 'a').set); 151 assertWeakFunction((class {static m(){}}).m); 152 assertWeakFunction(Object.getOwnPropertyDescriptor( 153 class {static get a(){}}, 'a').get); 154 assertWeakFunction(Object.getOwnPropertyDescriptor( 155 class {static set a(x){}}, 'a').set); 156 assertWeakFunction((new class {m(){}}).m); 157 assertWeakFunction(Object.getOwnPropertyDescriptor( 158 (class {get a(){}}).prototype, 'a').get); 159 assertWeakFunction(Object.getOwnPropertyDescriptor( 160 (class {set a(x){}}).prototype, 'a').set); 161})(); 162 163(function StrongFunctionLiterals() { 164 'use strong'; 165 function assertStrongFunction(x) { 166 assertTrue(%IsStrong(x)); 167 assertFalse('prototype' in x); 168 assertSame(Function.prototype, Object.getPrototypeOf(x)); 169 } 170 function f() {} 171 assertStrongFunction(f); 172 assertStrongFunction(function(){}); 173 assertStrongFunction(function f(){}); 174 assertStrongFunction(() => {}); 175 assertStrongFunction(x => x); 176 assertStrongFunction({m(){}}.m); 177 assertStrongFunction(Object.getOwnPropertyDescriptor( 178 {get a(){}}, 'a').get); 179 assertStrongFunction(Object.getOwnPropertyDescriptor( 180 {set a(x){}}, 'a').set); 181 assertStrongFunction((class {static m(){}}).m); 182 assertStrongFunction(Object.getOwnPropertyDescriptor( 183 class {static get a(){}}, 'a').get); 184 assertStrongFunction(Object.getOwnPropertyDescriptor( 185 class {static set a(x){}}, 'a').set); 186 assertStrongFunction((new class {m(){}}).m); 187 assertStrongFunction(Object.getOwnPropertyDescriptor( 188 (class {get a(){}}).prototype, 'a').get); 189 assertStrongFunction(Object.getOwnPropertyDescriptor( 190 (class {set a(x){}}).prototype, 'a').set); 191})(); 192 193(function SelfStrongFunctionLiterals() { 194 function assertStrongFunction(x) { 195 assertTrue(%IsStrong(x)); 196 assertFalse('prototype' in x); 197 assertSame(Function.prototype, Object.getPrototypeOf(x)); 198 } 199 function f() {'use strong'} 200 assertStrongFunction(f); 201 assertStrongFunction(function(){'use strong'}); 202 assertStrongFunction(function f(){'use strong'}); 203 assertStrongFunction(() => {'use strong'}); 204 assertStrongFunction(x => {'use strong'}); 205 assertStrongFunction({m(){'use strong'}}.m); 206 assertStrongFunction(Object.getOwnPropertyDescriptor( 207 {get a(){'use strong'}}, 'a').get); 208 assertStrongFunction(Object.getOwnPropertyDescriptor( 209 {set a(x){'use strong'}}, 'a').set); 210 assertStrongFunction((class {static m(){'use strong'}}).m); 211 assertStrongFunction(Object.getOwnPropertyDescriptor( 212 class {static get a(){'use strong'}}, 'a').get); 213 assertStrongFunction(Object.getOwnPropertyDescriptor( 214 class {static set a(x){'use strong'}}, 'a').set); 215 assertStrongFunction((new class {m(){'use strong'}}).m); 216 assertStrongFunction(Object.getOwnPropertyDescriptor( 217 (class {get a(){'use strong'}}).prototype, 'a').get); 218 assertStrongFunction(Object.getOwnPropertyDescriptor( 219 (class {set a(x){'use strong'}}).prototype, 'a').set); 220})(); 221 222let GeneratorPrototype = (function*(){}).__proto__; 223 224(function WeakGeneratorLiterals() { 225 function assertWeakGenerator(x) { 226 assertFalse(%IsStrong(x)); 227 assertFalse(%IsStrong(x.prototype)); 228 assertSame(GeneratorPrototype, Object.getPrototypeOf(x)); 229 assertFalse(%IsStrong(x())); 230 } 231 function* g() {} 232 assertWeakGenerator(g); 233 assertWeakGenerator(function*(){}); 234 assertWeakGenerator(function* g(){}); 235 assertWeakGenerator({*m(){}}.m); 236 assertWeakGenerator((class {static *m(){}}).m); 237 assertWeakGenerator((new class {*m(){}}).m); 238})(); 239 240(function StrongGeneratorLiterals() { 241 'use strong'; 242 function assertStrongGenerator(x) { 243 assertTrue(%IsStrong(x)); 244 // TODO(rossberg): strongify generator prototypes 245 // assertTrue(%IsStrong(x.prototype)); 246 assertSame(GeneratorPrototype, Object.getPrototypeOf(x)); 247 // TODO(rossberg): strongify generator instances 248 // assertTrue(%IsStrong(x())); 249 } 250 function* g() {} 251 assertStrongGenerator(g); 252 assertStrongGenerator(function*(){}); 253 assertStrongGenerator(function* g(){}); 254 assertStrongGenerator({*m(){}}.m); 255 assertStrongGenerator((class {static *m(){}}).m); 256 assertStrongGenerator((new class {*m(){}}).m); 257})(); 258 259(function SelfStrongGeneratorLiterals() { 260 function assertStrongGenerator(x) { 261 assertTrue(%IsStrong(x)); 262 // TODO(rossberg): strongify generator prototypes 263 // assertTrue(%IsStrong(x.prototype)); 264 assertSame(GeneratorPrototype, Object.getPrototypeOf(x)); 265 // TODO(rossberg): strongify generator instances 266 // assertTrue(%IsStrong(x())); 267 } 268 function* g() {'use strong'} 269 assertStrongGenerator(g); 270 assertStrongGenerator(function*(){'use strong'}); 271 assertStrongGenerator(function* g(){'use strong'}); 272 assertStrongGenerator({*m(){'use strong'}}.m); 273 assertStrongGenerator((class {static *m(){'use strong'}}).m); 274 assertStrongGenerator((new class {*m(){'use strong'}}).m); 275})(); 276 277(function WeakRegExpLiterals() { 278 function assertWeakRegExp(x) { 279 assertFalse(%IsStrong(x)); 280 } 281 assertWeakRegExp(/abc/); 282})(); 283 284(function StrongRegExpLiterals() { 285 'use strong'; 286 function assertStrongRegExp(x) { 287 // TODO(rossberg): strongify regexps 288 // assertTrue(%IsStrong(x)); 289 } 290 assertStrongRegExp(/abc/); 291})(); 292