Support for..of / for..in syntax
Hello,
I'm trying to compile a simple piece of code that uses the for... in or for... of iterable syntax, but I keep getting the following error:
ERROR TS1005: ';' expected.
for (var i:i32 in foo)
~~~
in main.ts(13,13)
Code sample here. It's happening using the latest AS code.
For this feature at first needs implement Iterators. @dcodeIO Could we use symbols for now?
Still hoping that iterators can be implemented without allocating dynamic memory, but haven't found a solution yet. If no such solution exists, well, you know, GC.
Why are iterators needed to have basic support for for..of?
I mean compared to say transforming:
for (a of arr) {
doSomethingWith(a);
}
to something like:
for (let i = 0, len = arr.length; i < len; i++) {
let a = arr[i];
doSomethingWith(a);
}
As for for..in in this case I assume it's possible to hold i32/i64 pointer to map entry functioning as "iterator".
As I understand WebAssembly supports 64-bit ints in 32-bit runtime. This means you can implement some basic value types as tagged pointers, see: https://en.wikipedia.org/wiki/Tagged_pointer https://www.mikeash.com/pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html
This likely should work well for iterators, as I don't see why would they need more state than is fitting into 64-bit.
The thought process so far was indeed to use a 64-bit value to represent an iterator. The information to encode into such a value would be 1) the index into the respective collection the iterator is at and 2) a reference back to the collection to enable calling it.next. This wouldn't work for an eventual Wasm64 unfortunately, so my conclusion was to wait for multi value support, making an iterator effectively a pair of two values.
wasm64 is very low priority as I know and may be even never appear. So may be make sense pack iterator to u64 something like:
| 64 | 63 - 32 | 31-0 |
|---|---|---|
| done-bit | value | iter-pointer |
Implementing something while ignoring 64-bit support for now is of course an option. Needs 29 bits for the reference back to the iterated object (can use the lower 3 bits for something else, i.e. state like done, because of 8 byte alignment) plus a 32-bit index. Also needs being able to use any type for this to implement Iterator#next for example, similar to what's planned for I32#toString.
Seems TypeScript handles it in the way as https://github.com/AssemblyScript/assemblyscript/issues/166#issuecomment-445686899 suggested. Any movement on this?
Hey. Just experimenting with AssemblyScript, trying to make a AS version of Conways Game of Life (https://github.com/KieranP/Game-Of-Life-Implementations).
Was unable to get a very simple for ... of ... from JS/TypeScript that currently doesn't work in AssemblyScript, unless I'm missing something?
const DIRECTIONS = [
[-1, 1], [0, 1], [1, 1], // above
[-1, 0], [1, 0], // sides
[-1, -1], [0, -1], [1, -1] // below
]
for (const [x, y] of DIRECTIONS) {
// SNIP
}
Yes. It is not implemented yet.