builtin_map_test.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package goja
  2. import "testing"
  3. func TestMapEvilIterator(t *testing.T) {
  4. const SCRIPT = `
  5. 'use strict';
  6. var o = {};
  7. function Iter(value) {
  8. this.value = value;
  9. this.idx = 0;
  10. }
  11. Iter.prototype.next = function() {
  12. var idx = this.idx;
  13. if (idx === 0) {
  14. this.idx++;
  15. return this.value;
  16. }
  17. return {done: true};
  18. }
  19. o[Symbol.iterator] = function() {
  20. return new Iter({});
  21. }
  22. assert.throws(TypeError, function() {
  23. new Map(o);
  24. });
  25. o[Symbol.iterator] = function() {
  26. return new Iter({value: []});
  27. }
  28. function t(prefix) {
  29. var m = new Map(o);
  30. assert.sameValue(1, m.size, prefix+": m.size");
  31. assert.sameValue(true, m.has(undefined), prefix+": m.has(undefined)");
  32. assert.sameValue(undefined, m.get(undefined), prefix+": m.get(undefined)");
  33. }
  34. t("standard adder");
  35. var count = 0;
  36. var origSet = Map.prototype.set;
  37. Map.prototype.set = function() {
  38. count++;
  39. origSet.apply(this, arguments);
  40. }
  41. t("custom adder");
  42. assert.sameValue(1, count, "count");
  43. undefined;
  44. `
  45. testScript1(TESTLIB+SCRIPT, _undefined, t)
  46. }