From: Brendan Hansen Date: Sat, 17 Apr 2021 22:27:13 +0000 (-0500) Subject: fixed list iterator destoying list X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=5577a9d70fc28adc8d586c5549c1a86ff53636f8;p=onyx.git fixed list iterator destoying list --- diff --git a/core/list.onyx b/core/list.onyx index 81c14727..d4bd0658 100644 --- a/core/list.onyx +++ b/core/list.onyx @@ -72,20 +72,31 @@ contains :: (list: ^List($T), x: T) -> bool { } get_iterator :: (list: ^List($T)) -> Iterator(T) { - iterator_impl :: ($T: type_expr, data: rawptr) -> (T, bool) { - elem_ptr := cast(^^ListElem(T)) data; - elem := *elem_ptr; + iterator_next :: ($T: type_expr, data: rawptr) -> (T, bool) { + list_iter := cast(^ListIterator(T)) data; use package core.intrinsics.onyx { __zero_value } - if elem == null do return __zero_value(T), false; + if list_iter.current == null do return __zero_value(T), false; - *elem_ptr = elem.next; - return elem.data, true; + defer list_iter.current = list_iter.current.next; + return list_iter.current.data, true; } + iterator_close :: (data: rawptr) { + cfree(data); + } + + ListIterator :: struct (T: type_expr) { + current: ^ListElem(T); + } + + list_iterator := new(#type ListIterator(T)); + list_iterator.current = list.first; + return .{ - data = ^list.first, - next = #solidify iterator_impl { T = T }, + data = list_iterator, + next = #solidify iterator_next { T = T }, + close = iterator_close, }; } diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 582f6aed..0975b55a 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -6,9 +6,8 @@ // by the WASM outputter. // [x] remove the need to have "allocate_exprs" on blocks and in functions. This will // be easy once the above is done. -// [ ] there should be a better way to emit pending deferred statements because there +// [x] there should be a better way to emit pending deferred statements because there // is some code duplication between emit_return and emit_structured_jump. -// // [ ] Change the calling convention so it is easier to use from both JS and in the compiler.