Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: V8 Iterators

$
0
0
Hello JohnGeeB,

Patching ClearScript may not be necessary. Consider this code:
engine.Script.host = new HostFunctions();
engine.AddHostType(typeof(IEnumerable));
engine.Execute(@"
    host.constructor.prototype[Symbol.iterator] = function () {
        var enumerable = host.asType(IEnumerable, this);
        if (host.isNull(enumerable))
            throw new TypeError('the host object is not enumerable');
        var enumerator = enumerable.GetEnumerator();
        return {
            next: function () {
                if (enumerator.MoveNext())
                    return { done: false, value: enumerator.Current };
                return { done: true };
            }
        };
    }
");
With this setup, all exposed .NET objects that implement IEnumerable support the ES6 iteration protocols.

Good luck!

Viewing all articles
Browse latest Browse all 2297

Latest Images





Latest Images