We'd previously checked Function.caller, but that isn't available in
strict mode (to which we're switching our codebase).
Is there any other way to detect that a custom iterator was called via
Iterator()? We'd like our custom iterators to behave the same as
standard object iterators, including returning [key, value] pairs when
Iterator() is used.
-myk
Andreas
> _______________________________________________
> dev-tech-js-engine mailing list
> dev-tech-...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-tech-js-engine
Iterator = (function(Iterator){return function Iterator(o){if (o
instanceof Whatever) { /* special Iterator() behavior */} else {/*
normal behavior */}}})(Iterator)
The purpose of passing Iterator as an argument here is to allow the
"normal behavior" path to invoke the Iterator function being
overridden.
> _______________________________________________
> dev-tech-js-engine mailing list
> dev-tech-...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-tech-js-engine
>
Here are the first few lines of a stack trace (Error().stack) from
Firefox 3.6:
Error()@:0
()@file:///mnt/hgfs/myk/Projects/jsdk/packages/jetpack-core/lib/securable-module.js
-> resource://jetpack-core-jetpack-core-lib/securable-module.js ->
resource://jetpack-core-jetpack-core-tests/test-iterator.js:7
Iterator([object Object])@:0
([object
Object])@file:///mnt/hgfs/myk/Projects/jsdk/packages/jetpack-core/lib/securable-module.js
-> resource://jetpack-core-jetpack-core-lib/securable-module.js ->
resource://jetpack-core-jetpack-core-tests/test-iterator.js:13
Here are the first few lines of one from a Firefox 4 trunk build:
()@resource://jetpack-core-jetpack-core-lib/securable-module.js ->
resource://jetpack-core-jetpack-core-lib/securable-module.js ->
resource://jetpack-core-jetpack-core-tests/test-iterator.js:7
([object
Object])@resource://jetpack-core-jetpack-core-lib/securable-module.js ->
resource://jetpack-core-jetpack-core-lib/securable-module.js ->
resource://jetpack-core-jetpack-core-tests/test-iterator.js:13
-myk
Thanks for the suggestion! It's what I've done (over in bug 597004) via
a custom Iterator function that passes an extra parameter to custom
iterator methods to specify that the method is being called via
Iterator, i.e. something like:
(function(DefaultIterator) {
return function Iterator(obj, keysOnly) {
if ("__iterator__" in obj && !keysOnly)
return obj.__iterator__.call(obj, false, true);
return DefaultIterator(obj, keysOnly);
};
})(Iterator);
Objects with custom iterator methods that want to support Iterator then
do something like:
this.__iterator__ = function (keysOnly, keysVals) {
for each (let [key, val] in customSet)
yield keysVals ? [key, val] : keysOnly ? key : val;
};
Perhaps it would be useful to add such a second parameter to the native
Iterator function.
-myk