michael@0: // Any copyright is dedicated to the Public Domain. michael@0: // http://creativecommons.org/licenses/publicdomain/ michael@0: // Contributor: Jim Blandy michael@0: michael@0: if (typeof findReferences == "function") { michael@0: function C() {} michael@0: var o = new C; michael@0: o.x = {}; // via ordinary property michael@0: o[42] = {}; // via numeric property michael@0: o[123456789] = {}; // via ridiculous numeric property michael@0: o.myself = o; // self-references should be reported michael@0: o.alsoMyself = o; // multiple self-references should all be reported michael@0: michael@0: assertEq(referencesVia(o, 'type; type_proto', C.prototype), true); michael@0: assertEq(referencesVia(o, 'shape; base; parent', this), true); michael@0: assertEq(referencesVia(o, 'x', o.x), true); michael@0: assertEq(referencesVia(o, 'objectElements[42]', o[42]), true); michael@0: assertEq(referencesVia(o, '123456789', o[123456789]), true); michael@0: assertEq(referencesVia(o, 'myself', o), true); michael@0: assertEq(referencesVia(o, 'alsoMyself', o), true); michael@0: michael@0: function g() { return 42; } michael@0: function s(v) { } michael@0: var p = Object.defineProperty({}, 'a', { get:g, set:s }); michael@0: assertEq(referencesVia(p, 'shape; base; getter', g), true); michael@0: assertEq(referencesVia(p, 'shape; base; setter', s), true); michael@0: michael@0: // If there are multiple objects with the same shape referring to a getter michael@0: // or setter, findReferences should get all of them, even though the shape michael@0: // gets 'marked' the first time we visit it. michael@0: var q = Object.defineProperty({}, 'a', { get:g, set:s }); michael@0: assertEq(referencesVia(p, 'shape; base; getter', g), true); michael@0: assertEq(referencesVia(q, 'shape; base; getter', g), true); michael@0: michael@0: // If we extend each object's shape chain, both should still be able to michael@0: // reach the getter, even though the two shapes are each traversed twice. michael@0: p.b = 9; michael@0: q.b = 9; michael@0: assertEq(referencesVia(p, 'shape; parent; base; getter', g), true); michael@0: assertEq(referencesVia(q, 'shape; parent; base; getter', g), true); michael@0: michael@0: // These are really just ordinary own property references. michael@0: assertEq(referencesVia(C, 'prototype', Object.getPrototypeOf(o)), true); michael@0: assertEq(referencesVia(Object.getPrototypeOf(o), 'constructor', C), true); michael@0: michael@0: // Dense arrays should work, too. michael@0: a = []; michael@0: a[1] = o; michael@0: assertEq(referencesVia(a, 'objectElements[1]', o), true); michael@0: michael@0: reportCompare(true, true); michael@0: } else { michael@0: reportCompare(true, true, "test skipped: findReferences is not a function"); michael@0: }