es6-shim icon indicating copy to clipboard operation
es6-shim copied to clipboard

Array methods should respect the realm of the `this`.

Open jdalton opened this issue 9 years ago • 9 comments

For example see step 9 of this https://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.prototype.filter

var i = document.createElement('iframe');
i.name = 'iframe';
i.style.display = 'none';
document.body.insertBefore(i, document.body.firstChild);

var f = frames.iframe;
f.document.write('<script><\/script>');
f.document.close();

var otherArray = f.Array(1,2,3);
var sliced = Array.prototype.slice.call(otherArray, 2);

console.log(sliced instanceof Array);  // should be `false`
console.log(sliced instanceof f.Array); // should be `true`

jdalton avatar Oct 07 '14 00:10 jdalton

Is there any way to test this without an iframe (in node)? The spec specifies "exotic Array object" only - is an Array from another iframe exotic?

ljharb avatar Oct 07 '14 09:10 ljharb

Realms are a red herring. Subclasses are the more interesting case where consulting this.constructor matters.

domenic avatar Oct 07 '14 13:10 domenic

Is there any way to test this without an iframe (in node)?

Yap, you can use require('vm').

jdalton avatar Oct 07 '14 15:10 jdalton

There's the perf side to consider; wrapping all those methods would be hella bad for that. Maybe this is one that es6-shim opts out of and just documents the lack of support.

jdalton avatar Oct 07 '14 16:10 jdalton

Indeed, that's one of my main concerns. At the least, we can ensure that our actual shims do the right thing, even if we choose not to detect and override every existing Array method :-)

ljharb avatar Oct 07 '14 17:10 ljharb

Array.prototype.map.call(document.querySelectorAll("*"), function (e) {
  return e.tagName;
});

am i right, that this code should throw error (because NodeList is not constructable) according to spec?

Yaffle avatar Oct 31 '14 03:10 Yaffle

@Yaffle yes, I believe so.

domenic avatar Oct 31 '14 04:10 domenic

seems, this was discussed here: https://esdiscuss.org/topic/array-prototype-slice-web-compat-issue#content-17 OK, i am not using Array.prototype.map for NodeLists anyway, so I am fine with it.

Yaffle avatar Oct 31 '14 04:10 Yaffle

Wow, that's a shame, that's a really common existing pattern.

ljharb avatar Oct 31 '14 04:10 ljharb