test_tearoffs.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. const Cc = Components.classes;
  5. const Ci = Components.interfaces;
  6. function run_test() {
  7. // Load the component manifest containing our test interface implementations.
  8. Components.manager.autoRegister(do_get_file('../components/js/xpctest.manifest'));
  9. // Shortcut the interfaces we're using.
  10. var ifs = {
  11. a: Ci['nsIXPCTestInterfaceA'],
  12. b: Ci['nsIXPCTestInterfaceB'],
  13. c: Ci['nsIXPCTestInterfaceC']
  14. };
  15. // Shortcut the class we're instantiating. This implements all three interfaces.
  16. var cls = Cc["@mozilla.org/js/xpc/test/js/TestInterfaceAll;1"];
  17. // Run through the logic a few times.
  18. for (i = 0; i < 2; ++i)
  19. play_with_tearoffs(ifs, cls);
  20. }
  21. function play_with_tearoffs(ifs, cls) {
  22. // Allocate a bunch of objects, QI-ed to B.
  23. var instances = [];
  24. for (var i = 0; i < 300; ++i)
  25. instances.push(cls.createInstance(ifs.b));
  26. // Nothing to collect.
  27. gc();
  28. // QI them to A.
  29. instances.forEach(function(v, i, a) { v.QueryInterface(ifs.a); });
  30. // QI them to C.
  31. instances.forEach(function(v, i, a) { v.QueryInterface(ifs.c); });
  32. // Check
  33. do_check_true('name' in instances[10], 'Have the prop from A/B');
  34. do_check_true('someInteger' in instances[10], 'Have the prop from C');
  35. // Grab tearoff reflections for a and b.
  36. var aTearOffs = instances.map(function(v, i, a) { return v.nsIXPCTestInterfaceA; } );
  37. var bTearOffs = instances.map(function(v, i, a) { return v.nsIXPCTestInterfaceB; } );
  38. // Check
  39. do_check_true('name' in aTearOffs[1], 'Have the prop from A');
  40. do_check_true(!('someInteger' in aTearOffs[1]), 'Dont have the prop from C');
  41. // Nothing to collect.
  42. gc();
  43. // Null out every even instance pointer.
  44. for (var i = 0; i < instances.length; ++i)
  45. if (i % 2 == 0)
  46. instances[i] = null;
  47. // Nothing to collect, since we still have the A and B tearoff reflections.
  48. gc();
  49. // Null out A tearoff reflections that are a multiple of 3.
  50. for (var i = 0; i < aTearOffs.length; ++i)
  51. if (i % 3 == 0)
  52. aTearOffs[i] = null;
  53. // Nothing to collect, since we still have the B tearoff reflections.
  54. gc();
  55. // Null out B tearoff reflections that are a multiple of 5.
  56. for (var i = 0; i < bTearOffs.length; ++i)
  57. if (i % 5 == 0)
  58. bTearOffs[i] = null;
  59. // This should collect every 30th object (indices that are multiples of 2, 3, and 5).
  60. gc();
  61. // Kill the b tearoffs entirely.
  62. bTearOffs = 0;
  63. // Collect more.
  64. gc();
  65. // Get C tearoffs.
  66. var cTearOffs = instances.map(function(v, i, a) { return v ? v.nsIXPCTestInterfaceC : null; } );
  67. // Check.
  68. do_check_true(!('name' in cTearOffs[1]), 'Dont have the prop from A');
  69. do_check_true('someInteger' in cTearOffs[1], 'have the prop from C');
  70. // Null out the a tearoffs.
  71. aTearOffs = null;
  72. // Collect all even indices.
  73. gc();
  74. // Collect all indices.
  75. instances = null;
  76. gc();
  77. // Give ourselves a pat on the back. :-)
  78. do_check_true(true, "Got all the way through without crashing!");
  79. }