Created
October 18, 2016 16:32
-
-
Save elct9620/f971ab0818570bf66d66048f3f1b4bdd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "mruby.h" | |
#include "mruby/data.h" | |
#include "mruby/compile.h" | |
#include "mruby/string.h" | |
#include "mruby/class.h" | |
class Application { | |
public: | |
inline void say() { | |
std::cout << "Hello World"; | |
}; | |
}; | |
static struct mrb_data_type ApplicationType = {"Application", mrb_free}; | |
static mrb_value appInitializer(mrb_state* mrb, mrb_value self) { | |
Application* application; | |
application = (Application*) DATA_PTR(self); | |
if(application != NULL) { | |
delete application; | |
} | |
DATA_TYPE(self) = &ApplicationType; | |
DATA_PTR(self) = NULL; | |
application = (Application*) mrb_alloca(mrb, sizeof(Application)); | |
DATA_PTR(self) = application; | |
return self; | |
} | |
static mrb_value appSay(mrb_state*, mrb_value self) { | |
Application* application = (Application*) DATA_PTR(self); | |
application->say(); | |
return self; | |
} | |
int main(int, char**) { | |
mrb_state* mrb = mrb_open(); | |
// mruby body | |
RClass* appClass = mrb_define_class(mrb, "Application", mrb->object_class); | |
mrb_define_method(mrb, appClass, "initialize", appInitializer, MRB_ARGS_NONE()); | |
mrb_define_method(mrb, appClass, "say", appSay, MRB_ARGS_NONE()); | |
mrb_load_string(mrb, "app = Application.new.say"); | |
if(mrb->exc) { | |
mrb_value exception = mrb_obj_value(mrb->exc); | |
std::cout << mrb_string_value_ptr(mrb, exception); | |
} | |
mrb_close(mrb); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment