Skip to content

Commit

Permalink
Added class property wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
Hadeweka committed Jun 5, 2023
1 parent 6065731 commit 029b427
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 41 deletions.
82 changes: 41 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,53 +148,53 @@ NOTE: This version will only work with mruby 3.2.0 and above.

#### Features

* [X] Added direct bindings to `Regex` from Crystal (`Regexp` in Ruby)
* [X] Added option to use a separate Ruby interpreter
* [X] Added safeguard to catch non-fatal Crystal exceptions and raise them as Ruby errors
* [X] Added option to transform procs into bytecode
* [X] Added function to disable external program execution
* [X] Added interpreter depth counter
* [X] Updated to mruby 3.2.0
* [X] Updated to MRI 3.0.5
* [X] Added backtrace function for mruby
* [X] Added option to fully protect Crystal values from the Ruby GC
* [ ] Added standalone wrappers for class properties
* Added direct bindings to `Regex` from Crystal (`Regexp` in Ruby)
* Added option to use a separate Ruby interpreter
* Added safeguard to catch non-fatal Crystal exceptions and raise them as Ruby errors
* Added option to transform procs into bytecode
* Added function to disable external program execution
* Added interpreter depth counter
* Updated to mruby 3.2.0
* Updated to MRI 3.0.5
* Added backtrace function for mruby
* Added option to fully protect Crystal values from the Ruby GC
* Added standalone wrappers for class properties

#### Usability

* [X] Updated mruby config file to use `libucrt` instead of `msvcrt`
* [X] Discarded old and problematic Regex mruby gem
* [X] Made arguments for `Anyolite.call_rb_method_of_object` optional
* [X] Added automatic conversion from Ruby class names to class objects in method callers
* [X] Improved testing script
* [X] Added check for `RbValue` and `RbRef` to some macro methods
* [X] Added ability to pass Ruby blocks via Crystal to Ruby function calls
* [X] Added some internal methods to mruby to handle fibers
* [X] Added some exception checking methods
* [X] Added methods to check reference table size
* [X] Removed now optional `Dir` gem from default build config
* [X] Removed need for `use_general_object_format_chars` flag for MRI
* [X] Added global option to use keyword args for optional arguments only
* Updated mruby config file to use `libucrt` instead of `msvcrt`
* Discarded old and problematic Regex mruby gem
* Made arguments for `Anyolite.call_rb_method_of_object` optional
* Added automatic conversion from Ruby class names to class objects in method callers
* Improved testing script
* Added check for `RbValue` and `RbRef` to some macro methods
* Added ability to pass Ruby blocks via Crystal to Ruby function calls
* Added some internal methods to mruby to handle fibers
* Added some exception checking methods
* Added methods to check reference table size
* Removed now optional `Dir` gem from default build config
* Removed need for `use_general_object_format_chars` flag for MRI
* Added global option to use keyword args for optional arguments only

#### Bugfixes

* [X] Fixed error when running `shards install` on Windows
* [X] Fixed compilation warning messages for Windows
* [X] Fixed problems with Regexes due to PCRE conflicts
* [X] Fixed problems with Anyolite on Crystal 1.5.1
* [X] Unspecified arguments now always correctly throw warnings instead of confusing errors
* [X] Fixed compiletime error when casting to `Char`
* [X] Fixed errors when passing certain name arguments to Macros for calling Ruby functions
* [X] Fixed `Anyolite.call_rb_method_of_object` argument `args` not being optional
* [X] Fixed linker error due to typo in mruby bindings for block functions
* [X] Fixed crash when casting Ruby numbers into objects in some cases
* [X] Fixed script lines not printing exceptions
* [X] Fixed memory leak when calling Ruby scripts and script lines
* [X] Updated tests to account for reworked `inspect` method for enums
* [X] Fixed errors when building Anyolite in a path with spaces
* [X] Fixed problems on Linux when `LD` is set, but `gcc` should compile
* [X] Fixed Crystal functions for accessing global variables in mruby
* [X] Fixed Anyolite to allow enums with types different than `Int32`
* Fixed error when running `shards install` on Windows
* Fixed compilation warning messages for Windows
* Fixed problems with Regexes due to PCRE conflicts
* Fixed problems with Anyolite on Crystal 1.5.1
* Unspecified arguments now always correctly throw warnings instead of confusing errors
* Fixed compiletime error when casting to `Char`
* Fixed errors when passing certain name arguments to Macros for calling Ruby functions
* Fixed `Anyolite.call_rb_method_of_object` argument `args` not being optional
* Fixed linker error due to typo in mruby bindings for block functions
* Fixed crash when casting Ruby numbers into objects in some cases
* Fixed script lines not printing exceptions
* Fixed memory leak when calling Ruby scripts and script lines
* Updated tests to account for reworked `inspect` method for enums
* Fixed errors when building Anyolite in a path with spaces
* Fixed problems on Linux when `LD` is set, but `gcc` should compile
* Fixed Crystal functions for accessing global variables in mruby
* Fixed Anyolite to allow enums with types different than `Int32`

### Later releases

Expand Down
5 changes: 5 additions & 0 deletions examples/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,11 @@ def self.class_method_in_ruby(str, int)
end
end

TestFramework.check(test_no: 63, should_be: 268) do
TestModule.some_class_property += 10
TestModule.some_class_property
end

final_time = Time.now

puts "Tests done."
Expand Down
41 changes: 41 additions & 0 deletions src/Main.cr
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,47 @@ module Anyolite
Anyolite.wrap_setter({{rb_interpreter}}, {{crystal_class}}, {{name + "="}}, {{proc}}, {{proc_arg}}, operator: {{operator_setter}}, context: {{context}})
end

# Wraps a class setter into mruby.
#
# The class setter *proc* (without the `=`) of the Crystal `Class` *crystal_class* will be integrated into the `RbInterpreter` *rb_interpreter*,
# with the argument *proc_arg* as its respective `Class`.
#
# Its new name will be *name*.
#
# The value *operator* will append the specified `String`
# to the final name and *context* can give the function a `Path` for resolving types correctly.
macro wrap_class_setter(rb_interpreter, crystal_class, name, proc, proc_arg, operator = "=", context = nil)
{% options = {:context => context} %}
Anyolite::Macro.wrap_module_function_with_args({{rb_interpreter}}, {{crystal_class}}, {{name}}, {{proc}}, {{proc_arg}}, operator: {{operator}}, options: {{options}})
end

# Wraps a class getter into mruby.
#
# The class getter *proc* of the Crystal `Class` *crystal_class* will be integrated into the `RbInterpreter` *rb_interpreter*.
#
# Its new name will be *name*.
#
# The value *operator* will append the specified `String`
# to the final name and *context* can give the function a `Path` for resolving types correctly.
macro wrap_class_getter(rb_interpreter, crystal_class, name, proc, operator = "", context = nil)
{% options = {:context => context} %}
Anyolite::Macro.wrap_module_function_with_args({{rb_interpreter}}, {{crystal_class}}, {{name}}, {{proc}}, operator: {{operator}}, options: {{options}})
end

# Wraps a class property into mruby.
#
# The class property *proc* of the Crystal `Class` *crystal_class* will be integrated into the `RbInterpreter` *rb_interpreter*,
# with the argument *proc_arg* as its respective `Class`.
#
# Its new name will be *name*.
#
# The values *operator_getter* and *operator_setter* will append the specified `String`
# to the final names and *context* can give the function a `Path` for resolving types correctly.
macro wrap_class_property(rb_interpreter, crystal_class, name, proc, proc_arg, operator_getter = "", operator_setter = "=", context = nil)
Anyolite.wrap_class_getter({{rb_interpreter}}, {{crystal_class}}, {{name}}, {{proc}}, operator: {{operator_getter}}, context: {{context}})
Anyolite.wrap_class_setter({{rb_interpreter}}, {{crystal_class}}, {{name + "="}}, {{proc}}, {{proc_arg}}, operator: {{operator_setter}}, context: {{context}})
end

# Wraps a constant value under a module into mruby.
#
# The value *crystal_value* will be integrated into the `RbInterpreter` *rb_interpreter*,
Expand Down
3 changes: 3 additions & 0 deletions test.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ require "./anyolite.cr"
{% end %}

module SomeModule
class_property some_class_property : Int32 = 258

def self.test_method(int : Int32, str : String)
[str, int]
end
Expand Down Expand Up @@ -555,6 +557,7 @@ end
macro load_test_module
Anyolite.wrap_module(rb, SomeModule, "TestModule")
Anyolite.wrap_module_function_with_keywords(rb, SomeModule, "test_method", SomeModule.test_method, [int : Int32 = 19, str : String])
Anyolite.wrap_class_property(rb, SomeModule, "some_class_property", SomeModule.some_class_property, Int32)
Anyolite.wrap_constant(rb, SomeModule, "SOME_CONSTANT", "Smile! 😊")
Anyolite.wrap(rb, SomeModule::Bla, under: SomeModule, verbose: true)
Anyolite.wrap(rb, SomeModule::TestStruct, under: SomeModule, verbose: true)
Expand Down

0 comments on commit 029b427

Please sign in to comment.