19258b6bc66e09368ada54001f619d53b4fc976d5ager@chromium.org// Copyright 2008 the V8 project authors. All rights reserved.
29a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Redistribution and use in source and binary forms, with or without
39a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// modification, are permitted provided that the following conditions are
49a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// met:
59a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//
69a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//     * Redistributions of source code must retain the above copyright
79a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       notice, this list of conditions and the following disclaimer.
89a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//     * Redistributions in binary form must reproduce the above
99a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       copyright notice, this list of conditions and the following
109a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       disclaimer in the documentation and/or other materials provided
119a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       with the distribution.
129a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//     * Neither the name of Google Inc. nor the names of its
139a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       contributors may be used to endorse or promote products derived
149a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//       from this software without specific prior written permission.
159a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com//
169a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
179a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
189a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
199a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
209a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
219a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
229a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
239a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
249a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
259a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
269a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
279a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
289a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Flags: --expose-debug-as debug
299a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Get the Debug object exposed from the debug context global object.
309a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comDebug = debug.Debug
319a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
329a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Simple object.
339a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comvar a = {};
349a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
359a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Create mirror for the object.
369a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comvar mirror = debug.MakeMirror(a);
379a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
389a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Initially one reference from the global object.
399a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(1, mirror.referencedBy().length);
409a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(1, mirror.referencedBy(0).length);
419a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(1, mirror.referencedBy(1).length);
429a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(1, mirror.referencedBy(10).length);
439a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
449a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Add some more references from simple objects and arrays.
459a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comvar b = {}
469a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comb.a = a;
479a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(2, mirror.referencedBy().length);
489a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comvar c = {}
499a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comc.a = a;
509a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comc.aa = a;
519a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comc.aaa = a;
529a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(3, mirror.referencedBy().length);
539a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comfunction d(){};
549a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comd.a = a
559a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(4, mirror.referencedBy().length);
569a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.come = [a,b,c,d];
579a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(5, mirror.referencedBy().length);
589a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
599a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
609a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Simple closure.
619a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comfunction closure_simple(p) {
629a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  return function() { p = null; };
639a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
649a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
659a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// This adds a reference (function context).
669a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comf = closure_simple(a);
679a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(6, mirror.referencedBy().length);
689a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// This clears the reference (in function context).
699a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comf()
709a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(5, mirror.referencedBy().length);
719a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
729a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Use closure with eval - creates arguments array.
739a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comfunction closure_eval(p, s) {
749a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  if (s) {
759a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    eval(s);
769a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  }
779a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  return function e(s) { eval(s); };
789a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
799a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
809a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// This adds a references (function context).
819a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comg = closure_eval(a);
829a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(6, mirror.referencedBy().length);
839a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
849a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Dynamically create a variable. This should create a context extension.
859a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comh = closure_eval(null, "var x_");
869a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(6, mirror.referencedBy().length);
879a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Adds a reference when set.
889a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comh("x_ = a");
899a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comvar x = mirror.referencedBy();
90245aa859d34fd516161c48ef4c69d38d9b889284iposva@chromium.orgassertEquals(7, mirror.referencedBy().length);
919a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Removes a reference when cleared.
929a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comh("x_ = null");
939a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(6, mirror.referencedBy().length);
949a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
959a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Check circular references.
969a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comx = {}
979a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.commirror = debug.MakeMirror(x);
989a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(1, mirror.referencedBy().length);
999a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comx.x = x;
1009a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(2, mirror.referencedBy().length);
1019a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comx = null;
1029a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(0, mirror.referencedBy().length);
1039a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
1049a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Check many references.
1059a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comy = {}
1069a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.commirror = debug.MakeMirror(y);
1079a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comrefs = [];
1089a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comfor (var i = 0; i < 200; i++) {
1099a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  refs[i] = {'y': y};
1109a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
1119a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comy = null;
1129a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comassertEquals(200, mirror.referencedBy().length);
113