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

Support Ruby file loading, class extraction and method call #70

Closed
ihrwein opened this issue Mar 27, 2016 · 9 comments
Closed

Support Ruby file loading, class extraction and method call #70

ihrwein opened this issue Mar 27, 2016 · 9 comments
Assignees

Comments

@ihrwein
Copy link

ihrwein commented Mar 27, 2016

First of all, thanks for this library! I was stunned by the elegancy of your crate :)

I'm in a great need of an embedded language in a Rust project. I wanted to use Python, then I read
your post in reddit and I got here.

I need to load a pure Ruby module with a class, create an instance from the class (with parameters), then call a method on the instance several times. I found an interesting wiki page which shows the steps
what I want to accomplish (just in C). I didn't find Rust bindings for some methods (e.g. mrb_class_get_under). Do you want to support this use-case?

Thanks!

@dragostis
Copy link
Member

Surely! My reasoning for now was that anyone would use Ruby's reflection API and call it dybamically either with run() or with call() if wanting to have more granularity.

But of course, for added elegance and performance, some wrappers would be nice. Did you take a short look over the API? Is there any particular way you see this implemented?

I'm guessing that you need a few functions defined on the mruby instance.

@dragostis
Copy link
Member

The example shouldn't be so hard to implement. What you would need would be some module handling functions (like get_module and get_class, get_class_under). Once you get the class from mruby, you should receive a Value that you can call new on with class.call("new", vec![args]).

@dragostis
Copy link
Member

I was thinking a while ago about adding a Class type to mrusty that would actually inherit from Value, but right now I see little use for it apart from being able to invoke new instead of call("new", ...) on a Value.

@dragostis
Copy link
Member

I've looked into it a bit more and I came to the conclusion that we need a class structure:

struct Class {
    mruby: MrubyType,
    class: MrClass
}

The following methods need to be added:

  • Class::to_str
  • Class::to_value - converts Class to Value (cannot fail)
  • Class::is_module
  • Class::new - call constructor
  • MrubyType::def_class - should return a Class
  • MrubyType::* - where * means get_module, get_class, ... , def_module, ...
  • Value::class - returns Class of the Value
  • Value::to_class - returns Result<Class, MrubyError> if Value is of type MRB_TT_CLASS or MRB_TT_MODULE
  • remove Value::type_name

I don't think we should make any distinction in Rust between modules and classes, since they can be swapped safely as far as I know.

Let me know if you want to try a PR with this. If not, I'll try to implement it during the following days.

@ihrwein
Copy link
Author

ihrwein commented Mar 27, 2016

Thanks for your quick and detailed answer!

I'll start the implementation right now, but I don't have any mruby experience so I might ask questions :)
(I also sufffer Internet connection problems - only 32kbps, that's why my answer was so late...)

@dragostis
Copy link
Member

Most of the stuff shouldn't be so hard. You can find most of the relevant RClass functions here.

Class::to_str should be implemented with mrb_class_name. And Value::to_class and Class::to_value will need C implementations similar to what you'll find in mrb_ext.c with the mrb_class_ptr macro.

The rest should be pretty easy and similar to what is already implemented. If you need anything else, I usually answer pretty fast.

@dragostis
Copy link
Member

@ihrwein Did you encounter any issues? If you need an implementation for this soon, I can try finishing it.

@ihrwein
Copy link
Author

ihrwein commented Apr 15, 2016

Sorry for my late response. I managed to implement to_str() but otherwise I did nothing. The priority of the feature I worked on was lowered so I couldn't contribute the way I intended to do.

I saw that you began to work on this feature. I still want to use mruby for one of my pet projects so I'll use the new functionality, just cannot make it myself.

Thanks for your work!

@dragostis
Copy link
Member

@ihrwein No prob. 😄 I actually needed part of this feature for Anima, so I went ahead to implement it. I'm going to work on #73 then launch 0.4.0 which will work with the newest Rust beta. (1.9.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants