diff options
| -rw-r--r-- | build_config.rb | 1 | ||||
| -rw-r--r-- | mrb_gems/arguments_and_return_example/LICENSE | 22 | ||||
| -rw-r--r-- | mrb_gems/arguments_and_return_example/README.md | 2 | ||||
| -rw-r--r-- | mrb_gems/arguments_and_return_example/mrbgem.rake | 4 | ||||
| -rw-r--r-- | mrb_gems/arguments_and_return_example/src/arguments_and_return_example.c | 44 | ||||
| -rw-r--r-- | test_suite.rb | 10 |
6 files changed, 83 insertions, 0 deletions
diff --git a/build_config.rb b/build_config.rb index a4d6ddb..a7c77ff 100644 --- a/build_config.rb +++ b/build_config.rb @@ -4,6 +4,7 @@ MRuby::Build.new do |conf| # Use mrbgems conf.gem './mrb_gems/basic_example' + conf.gem './mrb_gems/arguments_and_return_example' # conf.gem 'examples/mrbgems/ruby_extension_example' # conf.gem 'examples/mrbgems/c_extension_example' do |g| # g.cc.flags << '-g' # append cflags in this gem diff --git a/mrb_gems/arguments_and_return_example/LICENSE b/mrb_gems/arguments_and_return_example/LICENSE new file mode 100644 index 0000000..a632bdf --- /dev/null +++ b/mrb_gems/arguments_and_return_example/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2023 Tradam + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/mrb_gems/arguments_and_return_example/README.md b/mrb_gems/arguments_and_return_example/README.md new file mode 100644 index 0000000..64fb41e --- /dev/null +++ b/mrb_gems/arguments_and_return_example/README.md @@ -0,0 +1,2 @@ +# ArgumentsAndReturnExample +An mruby gem created by Tradam using mruby_gem_scaffolding. diff --git a/mrb_gems/arguments_and_return_example/mrbgem.rake b/mrb_gems/arguments_and_return_example/mrbgem.rake new file mode 100644 index 0000000..f666af5 --- /dev/null +++ b/mrb_gems/arguments_and_return_example/mrbgem.rake @@ -0,0 +1,4 @@ +MRuby::Gem::Specification.new('arguments_and_return_example') do |spec| + spec.license = 'MIT' + spec.author = 'Tradam' +end diff --git a/mrb_gems/arguments_and_return_example/src/arguments_and_return_example.c b/mrb_gems/arguments_and_return_example/src/arguments_and_return_example.c new file mode 100644 index 0000000..72e9ace --- /dev/null +++ b/mrb_gems/arguments_and_return_example/src/arguments_and_return_example.c @@ -0,0 +1,44 @@ +#include <mruby.h> +#include <stdio.h> + +// defining the function to be later bound to a ruby method +static mrb_value +multiply_numbers(mrb_state *mrb, mrb_value self) +{ + // we need to store the method arguments we recieve + // because of that we initialize them here + mrb_int first_input; + mrb_int second_input; + + // We need to call a function to recieve the method arguments + mrb_get_args( + mrb, // Ruby VM state + "ii", // Tell the function we expect 2 integers + &first_input, &second_input // Pass addresses of where we want the values to be stored into + ); + + // multiply the values and then convert the result + // into a mruby fixnum + return mrb_fixnum_value(first_input * second_input); +} + +// gem initializer +void +mrb_arguments_and_return_example_gem_init(mrb_state* mrb) { + struct RClass *arguments_and_return_example_class = mrb_define_module(mrb, "ArgumentsAndReturnExample"); + + // (!) signifies a change from the last example + mrb_define_module_function( + mrb, // Mruby VM state + arguments_and_return_example_class, // Class we bind method to + "multiply_numbers", // Name of method + multiply_numbers, // Function we are binding as a method + MRB_ARGS_REQ(2) // (!) 2 arguments are required + ); +} + +// gem finalizer +void +mrb_arguments_and_return_example_gem_final(mrb_state* mrb) { + +} diff --git a/test_suite.rb b/test_suite.rb index b0a6b36..7ddc1eb 100644 --- a/test_suite.rb +++ b/test_suite.rb @@ -1,4 +1,14 @@ +puts "Beginning Test Suite..." +puts + +puts " - Basic Example - " BasicExample.say_hello BasicExample.say_goodbye +puts + +puts " - Multiply Numbers Return Example - " +print "3 * 10 = " +puts ArgumentsAndReturnExample.multiply_numbers(3, 10) +puts puts "Reached end of test suite." |
