Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Wasmer::XxxArray #15

Merged
merged 3 commits into from
Jun 17, 2019
Merged

Enhance Wasmer::XxxArray #15

merged 3 commits into from
Jun 17, 2019

Conversation

irxground
Copy link
Contributor

ref: #14
Now Wasmer::XxxArray has each method and include Enumerable module.

The Enumerable mixin provides collection classes with several traversal and searching methods, and with the ability to sort. The class must provide a method each, which yields successive members of the collection.
https://docs.ruby-lang.org/en/2.6.0/Enumerable.html

let view = self.memory.view::<$wasm_type>();

let mut offset = self.offset;
while offset < view.len() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can write:

for nth in self.offset..view.len() {
    let value = view[nth].get() as i64;
    VM::yield_object(Integer::from(value))
}

It's more Rust idiomatic, and it avoids misusing the stop condition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed.

@Hywan
Copy link
Contributor

Hywan commented Jun 14, 2019

Should we support <=>, so that max, min and sort can be used? Just in order to get a full implementation of Enumerable. Or… maybe integers are automatically supported, and then, <=> isn't required?

@Hywan
Copy link
Contributor

Hywan commented Jun 14, 2019

bors try

bors bot added a commit that referenced this pull request Jun 14, 2019
@Hywan Hywan self-assigned this Jun 14, 2019
@Hywan Hywan added 🎉 enhancement New feature or request 📦 component-extension About the Ruby extension written in Rust 🧪 tests I love tests labels Jun 14, 2019
@bors
Copy link
Contributor

bors bot commented Jun 14, 2019

try

Build succeeded

@irxground
Copy link
Contributor Author

irxground commented Jun 14, 2019

Since the type of elements such as Int8Array is always an integer, sort works well.

If we implement the method <=>, the following code will work, but I think it is unnecessary.

mem1 = instance.memory.int8_view
mem2 = another_instance.memory.int8_view
[mem1, mem2].sort!

Note: Ruby's sort returns a sorted array. sort! sorts itself.

@Hywan
Copy link
Contributor

Hywan commented Jun 17, 2019

I agree sort isn't very useful here. Let's see if there is a need!

@Hywan
Copy link
Contributor

Hywan commented Jun 17, 2019

bors r+

bors bot added a commit that referenced this pull request Jun 17, 2019
15: Enhance `Wasmer::XxxArray` r=Hywan a=irxground

ref: #14
Now `Wasmer::XxxArray` has `each` method and include `Enumerable` module.

> The Enumerable mixin provides collection classes with several traversal and searching methods, and with the ability to sort. The class must provide a method each, which yields successive members of the collection.
https://docs.ruby-lang.org/en/2.6.0/Enumerable.html


Co-authored-by: irxground <irxnjhtchlnrw@gmail.com>
@Hywan
Copy link
Contributor

Hywan commented Jun 17, 2019

Thanks for the great PR!

@bors
Copy link
Contributor

bors bot commented Jun 17, 2019

Build succeeded

@bors bors bot merged commit 16d85f3 into wasmerio:master Jun 17, 2019
@irxground irxground deleted the impl_each branch June 17, 2019 07:49
@dnsco
Copy link
Contributor

dnsco commented Jun 17, 2019

This is awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 component-extension About the Ruby extension written in Rust 🎉 enhancement New feature or request 🧪 tests I love tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants