Skip to content

Commit

Permalink
Implement Wasmer::XxxArray#each
Browse files Browse the repository at this point in the history
  • Loading branch information
irxground committed Jun 13, 2019
1 parent baa8363 commit eeaf9f7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ pub extern "C" fn Init_wasmer() {

// Declare the `[]` (get) method.
itself.def("[]", memory::view::$mod_name::ruby_memory_view_get);

// Declare the `each` method.
itself.def("each", memory::view::$mod_name::ruby_memory_view_each);
});
};
}
Expand Down
19 changes: 18 additions & 1 deletion src/memory/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ macro_rules! memory_view {
use lazy_static::lazy_static;
use rutie::{
class, methods, wrappable_struct, AnyException, Exception, Fixnum, Integer,
NilClass, Object,
NilClass, Object, VM,
};
use std::{mem::size_of, rc::Rc};
use wasmer_runtime as runtime;
Expand Down Expand Up @@ -71,6 +71,17 @@ macro_rules! memory_view {
Ok(view[offset + index].get())
}
}

pub fn each(&self) {
let view = self.memory.view::<$wasm_type>();

let mut offset = self.offset;
while offset < view.len() {
let value = view[offset].get();
VM::yield_object(Integer::from(value as i64));
offset += 1;
}
}
}

wrappable_struct!(MemoryView, MemoryViewWrapper, MEMORY_VIEW_WRAPPER);
Expand Down Expand Up @@ -117,6 +128,12 @@ macro_rules! memory_view {
))
})
}

fn ruby_memory_view_each() -> RubyMemoryView {
let memory_view = _itself.get_data(&*MEMORY_VIEW_WRAPPER);
memory_view.each();
_itself
}
);
}
};
Expand Down
12 changes: 4 additions & 8 deletions tests/memory_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,13 @@ def test_hello_world
nth = 0
string = ""

while true
char = memory[nth]

if 0 == char
break
end

string += char.chr
memory.each do |char|
break if 0 == char
string << char.chr
nth += 1
end

assert_equal 13, nth
assert_equal "Hello, World!", string
end

Expand Down

0 comments on commit eeaf9f7

Please sign in to comment.