diff --git a/.gitignore b/.gitignore index 5377b7e..e484d5a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ build/ utility/mruby_build_config.rb.lock third_party/ docs/ -examples/bytecode_test.mrb \ No newline at end of file +examples/bytecode_test.mrb +.vscode/ \ No newline at end of file diff --git a/README.md b/README.md index 33e92b5..6db21df 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,7 @@ The term 'anyoli' means 'green' in the Maasai language, thus naming 'anyolite'. * * [X] Keyword argument handling * * [X] Block argument handling * * [X] Storage of Ruby objects +* * [X] Access to variables * * [ ] Porting of all MRI functions to the abstraction layer * * [ ] Bytecode compilation (might not be possible) * * [ ] Tests diff --git a/glue/mri/data_helper.c b/glue/mri/data_helper.c index 298656e..6963f41 100644 --- a/glue/mri/data_helper.c +++ b/glue/mri/data_helper.c @@ -222,3 +222,38 @@ extern VALUE rb_funcall_argv_with_block_helper(void *rb, VALUE value, ID name, i } +extern VALUE rb_iv_get_helper(void* rb, VALUE obj, ID sym) { + + rb_ivar_get(obj, sym); + +} + +extern void rb_iv_set_helper(void* rb, VALUE obj, ID sym, VALUE value) { + + rb_ivar_set(obj, sym, value); + +} + +extern VALUE rb_cv_get_helper(void* rb, VALUE mod, ID sym) { + + rb_cvar_get(mod, sym); + +} + +extern void rb_cv_set_helper(void* rb, VALUE mod, ID sym, VALUE value) { + + rb_cvar_set(mod, sym, value); + +} + +extern VALUE rb_gv_get_helper(void* rb, const char* name) { + + rb_gv_get(name); + +} + +extern void rb_gv_set_helper(void* rb, const char* name, VALUE value) { + + rb_gv_set(name, value); + +} \ No newline at end of file diff --git a/src/Main.cr b/src/Main.cr index 4808b2c..22fda14 100644 --- a/src/Main.cr +++ b/src/Main.cr @@ -400,9 +400,8 @@ module Anyolite # If needed, *context* can be set to a `Path` in order to specify *cast_to*. macro get_gv(name, cast_to = nil, context = nil) %rb = Anyolite::RbRefTable.get_current_interpreter - %name = Anyolite::RbCore.convert_to_rb_sym(%rb, {{name}}.to_s) - %result = Anyolite::RbCore.rb_gv_get(%rb, %name) + %result = Anyolite::RbCore.rb_gv_get(%rb, {{name}}.to_s) {% if cast_to %} Anyolite::Macro.convert_from_ruby_to_crystal(%rb.to_unsafe, %result, {{cast_to}}, context: {{context}}) @@ -421,10 +420,9 @@ module Anyolite # If needed, *context* can be set to a `Path` in order to specify *cast_to*. macro set_gv(name, value) %rb = Anyolite::RbRefTable.get_current_interpreter - %name = Anyolite::RbCore.convert_to_rb_sym(%rb, {{name}}.to_s) %value = Anyolite::RbCast.return_value(%rb.to_unsafe, {{value}}) - Anyolite::RbCore.rb_gv_set(%rb, %name, %value) + Anyolite::RbCore.rb_gv_set(%rb, {{name}}.to_s, %value) end # Wraps a Crystal class directly into an mruby class. diff --git a/src/implementations/mri/RbCore.cr b/src/implementations/mri/RbCore.cr index 0fa029f..04c42cf 100644 --- a/src/implementations/mri/RbCore.cr +++ b/src/implementations/mri/RbCore.cr @@ -195,14 +195,14 @@ module Anyolite fun rb_respond_to = rb_respond_to_helper(rb : State*, obj : RbValue, name : RbSymbol) : RbBool - # fun rb_iv_set = mrb_iv_set(rb : State*, obj : RbValue, sym : RbSymbol, value : RbValue) : Void - # fun rb_iv_get = mrb_iv_get(rb : State*, obj : RbValue, sym : RbSymbol) : RbValue + fun rb_iv_set = rb_iv_set_helper(rb : State*, obj : RbValue, sym : RbSymbol, value : RbValue) : Void + fun rb_iv_get = rb_iv_get_helper(rb : State*, obj : RbValue, sym : RbSymbol) : RbValue - # fun rb_cv_set = mrb_cv_set(rb : State*, mod : RbValue, sym : RbSymbol, value : RbValue) : Void - # fun rb_cv_get = mrb_cv_get(rb : State*, mod : RbValue, sym : RbSymbol) : RbValue + fun rb_cv_set = rb_cv_set_helper(rb : State*, mod : RbValue, sym : RbSymbol, value : RbValue) : Void + fun rb_cv_get = rb_cv_get_helper(rb : State*, mod : RbValue, sym : RbSymbol) : RbValue - # fun rb_gv_set = mrb_gv_set(rb : State*, sym : RbSymbol, value : RbValue) : Void - # fun rb_gv_get = mrb_gv_get(rb : State*, sym : RbSymbol) : RbValue + fun rb_gv_set = rb_gv_set_helper(rb : State*, name : LibC::Char*, value : RbValue) : Void + fun rb_gv_get = rb_gv_get_helper(rb : State*, name : LibC::Char*) : RbValue fun load_script_from_file(rb : State*, filename : LibC::Char*) : Void # fun execute_script_line(rb : State*, str : LibC::Char*) : RbValue diff --git a/src/implementations/mruby/RbCore.cr b/src/implementations/mruby/RbCore.cr index efe1d80..a5c4a27 100644 --- a/src/implementations/mruby/RbCore.cr +++ b/src/implementations/mruby/RbCore.cr @@ -195,8 +195,11 @@ module Anyolite fun rb_cv_set = mrb_cv_set(rb : State*, mod : RbValue, sym : RbSymbol, value : RbValue) : Void fun rb_cv_get = mrb_cv_get(rb : State*, mod : RbValue, sym : RbSymbol) : RbValue - fun rb_gv_set = mrb_gv_set(rb : State*, sym : RbSymbol, value : RbValue) : Void - fun rb_gv_get = mrb_gv_get(rb : State*, sym : RbSymbol) : RbValue + # NOTE: These are differing due to global variable methods by ID being private in MRI + # NOTE: You should not use globals that often, anyway + + fun rb_gv_set = mrb_gv_set(rb : State*, name : LibC::Char*, value : RbValue) : Void + fun rb_gv_get = mrb_gv_get(rb : State*, name : LibC::Char*) : RbValue fun load_script_from_file(rb : State*, filename : LibC::Char*) : RbValue fun execute_script_line(rb : State*, str : LibC::Char*) : RbValue diff --git a/test.cr b/test.cr index 11c2b94..30d9bcb 100644 --- a/test.cr +++ b/test.cr @@ -400,8 +400,6 @@ module SomeModule result end - {% unless flag?(:anyolite_implementation_ruby_3) %} - def set_instance_variable_to_int(name : String, value : Int) Anyolite.set_iv(self, name, value) end @@ -410,8 +408,6 @@ module SomeModule Anyolite.get_iv(self, name, cast_to: Int?) end - {% end %} - def ref_test(str : String, ref : Anyolite::RbRef) converted_arg = Anyolite.cast_to_crystal(ref, Int32?) "#{str} and a reference with #{ref.value} (which is #{converted_arg}) were given."