summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2023-05-26 01:01:44 -0400
committerrealtradam <[email protected]>2023-05-26 01:01:44 -0400
commit85f3e9ed33b3e3cab7e011bae3b1ac99fb4520a7 (patch)
tree5f6819bcd93a10c66eb3daf0bd90ec88f194bbb2
parent0e689b72d7fc7afa64508a99948527009712d189 (diff)
downloadmruby-playground-85f3e9ed33b3e3cab7e011bae3b1ac99fb4520a7.tar.gz
mruby-playground-85f3e9ed33b3e3cab7e011bae3b1ac99fb4520a7.zip
some progress
-rw-r--r--build_config.rb4
-rw-r--r--mrb_gems/arguments_and_return_example/autogen.rb20
-rw-r--r--mrb_gems/arguments_and_return_example/src/arguments_and_return_example.c47
-rw-r--r--mrb_gems/basic_example/autogen.rb15
-rw-r--r--mrb_gems/basic_example/src/basic_example.c32
-rw-r--r--mrb_gems/basic_struct_example/autogen.rb39
-rw-r--r--mrb_gems/basic_struct_example/src/basic_struct_example.c177
m---------mruby0
-rw-r--r--test_suite.rb2
9 files changed, 153 insertions, 183 deletions
diff --git a/build_config.rb b/build_config.rb
index 91a6087..9d2b0a2 100644
--- a/build_config.rb
+++ b/build_config.rb
@@ -3,8 +3,8 @@ MRuby::Build.new do |conf|
conf.toolchain
# Our example gems, uncomment to include in build
- #conf.gem './mrb_gems/basic_example'
- #conf.gem './mrb_gems/arguments_and_return_example'
+ conf.gem './mrb_gems/basic_example'
+ conf.gem './mrb_gems/arguments_and_return_example'
#conf.gem './mrb_gems/keyword_arguments_example'
conf.gem './mrb_gems/basic_struct_example'
diff --git a/mrb_gems/arguments_and_return_example/autogen.rb b/mrb_gems/arguments_and_return_example/autogen.rb
new file mode 100644
index 0000000..17ad5bd
--- /dev/null
+++ b/mrb_gems/arguments_and_return_example/autogen.rb
@@ -0,0 +1,20 @@
+require "FelBind"
+
+mgem = FelBind::BindGem.new(gem_name: "arguments_and_return_example")
+
+mgem.add_class("ArgumentsAndReturnExample")
+
+mgem.add_function(class_name: "ArgumentsAndReturnExample", function_name: "multiply_numbers") do |func|
+ func.get_args do |args|
+ args.int "first_input"
+ args.int "second_input"
+ end
+
+ func.return_call do |rc|
+ rc.type = "int"
+ rc.val = "#{func.arg("first_input")} * #{func.arg("second_input")}"
+ end
+end
+
+File.write("src/arguments_and_return_example.c", mgem.build)
+
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
index 72e9ace..cba5a96 100644
--- 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
@@ -1,44 +1,15 @@
#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
- );
+felbind_multiply_numbers(mrb_state *mrb, mrb_value self){
+mrb_int felflame_var_first_input;
+mrb_int felflame_var_second_input;
+mrb_get_args(mrb, "ii", &felflame_var_first_input, &felflame_var_second_input);
- // multiply the values and then convert the result
- // into a mruby fixnum
- return mrb_fixnum_value(first_input * second_input);
+return mrb_fixnum_value(felflame_var_first_input * felflame_var_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) {
-
+void mrb_arguments_and_return_example_gem_init(mrb_state* mrb) {
+struct RClass *ArgumentsAndReturnExample_class = mrb_define_module(mrb, "ArgumentsAndReturnExample");
+mrb_define_class_method(mrb, ArgumentsAndReturnExample_class, "multiply_numbers", felbind_multiply_numbers, MRB_ARGS_REQ(2));
}
+void mrb_arguments_and_return_example_gem_final(mrb_state* mrb) {}
diff --git a/mrb_gems/basic_example/autogen.rb b/mrb_gems/basic_example/autogen.rb
new file mode 100644
index 0000000..0640254
--- /dev/null
+++ b/mrb_gems/basic_example/autogen.rb
@@ -0,0 +1,15 @@
+require "FelBind"
+
+mgem = FelBind::BindGem.new(gem_name: "basic_example")
+
+mgem.add_class("BasicExample")
+
+mgem.add_function(class_name: "BasicExample", function_name: "say_hello") do |func|
+ func.content = "printf(\"Hello World\\n\");"
+ func.return_call do |rc|
+ rc.type = "nil"
+ end
+end
+
+File.write("src/basic_example.c", mgem.build)
+
diff --git a/mrb_gems/basic_example/src/basic_example.c b/mrb_gems/basic_example/src/basic_example.c
index 908acd2..f039f80 100644
--- a/mrb_gems/basic_example/src/basic_example.c
+++ b/mrb_gems/basic_example/src/basic_example.c
@@ -1,30 +1,12 @@
#include <mruby.h>
#include <stdio.h>
-
-// defining the function to be later bound to a ruby method
static mrb_value
-hello_world(mrb_state *mrb, mrb_value self)
-{
- printf("Hello World\n");
-
- return mrb_nil_value(); // return null
+felbind_say_hello(mrb_state *mrb, mrb_value self){
+printf("Hello World\n");
+return mrb_nil_value();
}
-
-// gem initializer
-void
-mrb_basic_example_gem_init(mrb_state* mrb) {
- struct RClass *basic_example_class = mrb_define_module(mrb, "BasicExample");
- mrb_define_class_method(
- mrb, // Mruby VM state
- basic_example_class, // Class we bind method to
- "say_hello", // Name of method
- hello_world, // Function we are binding as a method
- MRB_ARGS_NONE() // How many arguments are optional/required
- );
-}
-
-// gem finalizer
-void
-mrb_basic_example_gem_final(mrb_state* mrb) {
-
+void mrb_basic_example_gem_init(mrb_state* mrb) {
+struct RClass *BasicExample_class = mrb_define_module(mrb, "BasicExample");
+mrb_define_class_method(mrb, BasicExample_class, "say_hello", felbind_say_hello, MRB_ARGS_NONE());
}
+void mrb_basic_example_gem_final(mrb_state* mrb) {}
diff --git a/mrb_gems/basic_struct_example/autogen.rb b/mrb_gems/basic_struct_example/autogen.rb
new file mode 100644
index 0000000..5db0cb8
--- /dev/null
+++ b/mrb_gems/basic_struct_example/autogen.rb
@@ -0,0 +1,39 @@
+require "FelBind"
+
+mgem = FelBind::BindGem.new(gem_name: "basic_struct_example")
+
+mgem.add_class("Color")
+
+mgem.add_struct(class_name: "Color", cstruct_name: "Color") do |struct|
+ struct.initializer = true
+ struct.member(
+ name: "r",
+ ctype: "char",
+ rtype: "int",
+ accessor: true
+ )
+ struct.member(
+ name: "g",
+ ctype: "char",
+ rtype: "int",
+ accessor: true
+ )
+ struct.member(
+ name: "b",
+ ctype: "char",
+ rtype: "int",
+ accessor: true
+ )
+end
+
+result = "typedef struct Color
+{
+ char r;
+ char g;
+ char b;
+} Color;\n"
+
+result += mgem.build
+
+File.write("src/basic_struct_example.c", result)
+
diff --git a/mrb_gems/basic_struct_example/src/basic_struct_example.c b/mrb_gems/basic_struct_example/src/basic_struct_example.c
index ec304a1..9ced661 100644
--- a/mrb_gems/basic_struct_example/src/basic_struct_example.c
+++ b/mrb_gems/basic_struct_example/src/basic_struct_example.c
@@ -1,129 +1,72 @@
-#include <mruby.h>
-#include <mruby/data.h>
-#include <mruby/class.h>
-#include <mruby/compile.h>
-#include <stdio.h>
-
-// a regular C struct
typedef struct Color
{
char r;
char g;
char b;
} Color;
-// we will later be building an interface for it
-
-// A struct that informs the Ruby VM what function to call
-// when garbage collecting our struct wrapper. Here nothing
-// special needs to be done so the default "mrb_free" function
-// is used.
-// Ending the struct name with 'mrb_struct' is a
-// convention I like to use for these mruby 'internal'
-// structs
-static const struct mrb_data_type Color_mrb_struct = {
- "Color", // name of the class in ruby
- mrb_free // the function we want it to call when garbage collected
- // ^ you can customize this, but remember to always call mrb_free
- // ^ within whatever customization you make
-};
-
-static mrb_value
-Color_initialize(mrb_state* mrb, mrb_value self) {
- //Rectangle *wrapped_value = (Rectangle *) mrb_malloc(mrb, sizeof(Rectangle));
- //MyCustomStruct *wrapped_value = (MyCustomStruct *)mrb_malloc(mrb, sizeof(MyCustomStruct));
-
- Color *wrapped_value = (Color *)DATA_PTR(self);
- if(wrapped_value) { mrb_free(mrb, wrapped_value); }
- mrb_data_init(self, NULL, &Color_mrb_struct);
- wrapped_value = (Color *)mrb_malloc(mrb, sizeof(Color));
-
- mrb_int parameter_r; // the value that will be recieved from the function call parameter
- mrb_int parameter_g; // the value that will be recieved from the function call parameter
- mrb_int parameter_b; // the value that will be recieved from the function call parameter
-
- mrb_get_args(mrb, "iii", &parameter_r, &parameter_g, &parameter_b); // getting the value of the param and storing it.
- wrapped_value->r = (char)parameter_r;
- wrapped_value->g = (char)parameter_g;
- wrapped_value->b = (char)parameter_b;
- //mrb_data_init(self, wrapped_value, &mrb_Rectangle_struct);
- mrb_data_init(self, wrapped_value, &Color_mrb_struct);
+#include <mruby.h>
+#include <mruby/data.h>
+#include <mruby/class.h>
+#include <mruby/compile.h>
+#include <stdio.h>
+static const struct mrb_data_type felbind_struct_Color = { "Color", mrb_free };
+static mrb_value felbind_struct_init_Color(mrb_state* mrb, mrb_value self) {
+ Color *felbind_struct_wrapped_Color = (Color *)DATA_PTR(self);
+ if(felbind_struct_wrapped_Color) { mrb_free(mrb, felbind_struct_wrapped_Color); }
+ mrb_data_init(self, NULL, &felbind_struct_Color);
+ felbind_struct_wrapped_Color = (Color *)mrb_malloc(mrb, sizeof(Color));
+ mrb_int felbind_param_r;
+ mrb_int felbind_param_g;
+ mrb_int felbind_param_b;
+ mrb_get_args(mrb, "iii", &felbind_param_r, &felbind_param_g, &felbind_param_b);
+ felbind_struct_wrapped_Color->r = (char)felbind_param_r;
+ felbind_struct_wrapped_Color->g = (char)felbind_param_g;
+ felbind_struct_wrapped_Color->b = (char)felbind_param_b;
+ mrb_data_init(self, felbind_struct_wrapped_Color, &felbind_struct_Color);
return self;
}
-
-// defining the function to be later bound to a ruby method
-static mrb_value
-Color_r_getter(mrb_state *mrb, mrb_value self)
-{
- // Unwrap the Ruby object into a C pointer
- struct Color *my_struct = DATA_GET_PTR(
- mrb, // The Ruby VM
- self, // The Ruby object holding the struct
- &Color_mrb_struct, // Address to the custom struct tag we declared above
- Color // The struct tag we declared at the top
- );
-
- // Get the x value from the struct and convert it into a Ruby object. Return it
- return mrb_fixnum_value(my_struct->r);
+static mrb_value felbind_getter_Color_r(mrb_state *mrb, mrb_value self) {
+ struct Color *felbind_struct_get = DATA_GET_PTR(mrb, self, &felbind_struct_Color, Color);
+ return mrb_fixnum_value(felbind_struct_get->r);
+}
+static mrb_value felbind_setter_Color_r(mrb_state *mrb, mrb_value self) {
+ mrb_int felbind_param_r;
+ mrb_get_args(mrb, "i", &felbind_param_r);
+ struct Color *felbind_struct_set = DATA_GET_PTR(mrb, self, &felbind_struct_Color, Color);
+ felbind_struct_set->r = (char)felbind_param_r;
+ return mrb_fixnum_value(felbind_struct_set->r);
+}
+static mrb_value felbind_getter_Color_g(mrb_state *mrb, mrb_value self) {
+ struct Color *felbind_struct_get = DATA_GET_PTR(mrb, self, &felbind_struct_Color, Color);
+ return mrb_fixnum_value(felbind_struct_get->g);
+}
+static mrb_value felbind_setter_Color_g(mrb_state *mrb, mrb_value self) {
+ mrb_int felbind_param_g;
+ mrb_get_args(mrb, "i", &felbind_param_g);
+ struct Color *felbind_struct_set = DATA_GET_PTR(mrb, self, &felbind_struct_Color, Color);
+ felbind_struct_set->g = (char)felbind_param_g;
+ return mrb_fixnum_value(felbind_struct_set->g);
}
-
-static mrb_value
-Color_r_setter(mrb_state* mrb, mrb_value self) {
- mrb_int parameter_r; // the value that will be recieved from the function call parameter
-
- mrb_get_args(mrb, "i", &parameter_r); // getting the value of the param and storing it.
-
- // Unwrap the Ruby object into a C pointer
- // same as the function defined above does
- Color *my_struct = DATA_GET_PTR(mrb, self, &Color_mrb_struct, Color);
-
- // Set the value in the struct to the new one
- // note: we do not need to rerwap the struct or anything as we
- // are using a pointer to change the values inside the wrapped
- // struct.
- my_struct->r = (char)parameter_r;
-
- // Ruby convention is to return the value we set so we do that
- return mrb_fixnum_value((char)parameter_r);
+static mrb_value felbind_getter_Color_b(mrb_state *mrb, mrb_value self) {
+ struct Color *felbind_struct_get = DATA_GET_PTR(mrb, self, &felbind_struct_Color, Color);
+ return mrb_fixnum_value(felbind_struct_get->b);
}
-
-// gem initializer
-void
-mrb_basic_struct_example_gem_init(mrb_state* mrb) {
- struct RClass *basic_struct_example_class = mrb_define_module(mrb, "BasicStructExample");
- struct RClass *color_class = mrb_define_class_under(
- mrb,
- basic_struct_example_class,
- "Color",
- mrb->object_class
- );
- // This macro MUST be called and it must be after the class is defined
- MRB_SET_INSTANCE_TT(color_class, MRB_TT_DATA);
-
- mrb_define_method(
- mrb, // Mruby VM state
- color_class, // Class we bind method to
- "initialize", // Name of method
- Color_initialize, // Function we are binding as a method
- MRB_ARGS_ANY() // How many arguments are optional/required
- );
- mrb_define_method(
- mrb, // Mruby VM state
- color_class, // Class we bind method to
- "r", // Name of method
- Color_r_getter, // Function we are binding as a method
- MRB_ARGS_NONE() // How many arguments are optional/required
- );
- mrb_define_method(
- mrb, // Mruby VM state
- color_class, // Class we bind method to
- "r=", // Name of method
- Color_r_setter, // Function we are binding as a method
- MRB_ARGS_ANY() // How many arguments are optional/required
- );
+static mrb_value felbind_setter_Color_b(mrb_state *mrb, mrb_value self) {
+ mrb_int felbind_param_b;
+ mrb_get_args(mrb, "i", &felbind_param_b);
+ struct Color *felbind_struct_set = DATA_GET_PTR(mrb, self, &felbind_struct_Color, Color);
+ felbind_struct_set->b = (char)felbind_param_b;
+ return mrb_fixnum_value(felbind_struct_set->b);
}
-
-// gem finalizer
-void
-mrb_basic_struct_example_gem_final(mrb_state* mrb) {
-
+void mrb_basic_struct_example_gem_init(mrb_state* mrb) {
+ struct RClass *Color_class = mrb_define_module(mrb, "Color");
+ MRB_SET_INSTANCE_TT(Color_class, MRB_TT_DATA);
+ mrb_define_method(mrb, Color_class, "initialize", felbind_struct_init_Color, MRB_ARGS_ANY());
+ mrb_define_method(mrb, Color_class, "r", felbind_getter_Color_r, MRB_ARGS_NONE());
+ mrb_define_method(mrb, Color_class, "r=", felbind_setter_Color_r, MRB_ARGS_ANY());
+ mrb_define_method(mrb, Color_class, "g", felbind_getter_Color_g, MRB_ARGS_NONE());
+ mrb_define_method(mrb, Color_class, "g=", felbind_setter_Color_g, MRB_ARGS_ANY());
+ mrb_define_method(mrb, Color_class, "b", felbind_getter_Color_b, MRB_ARGS_NONE());
+ mrb_define_method(mrb, Color_class, "b=", felbind_setter_Color_b, MRB_ARGS_ANY());
}
+void mrb_basic_struct_example_gem_final(mrb_state* mrb) {}
diff --git a/mruby b/mruby
-Subproject 78141d17f17e4e543300607efdc54ace88ae1d3
+Subproject a099ca44f4d165cf89575c21d78abff062d6767
diff --git a/test_suite.rb b/test_suite.rb
index 470f7b7..a31297e 100644
--- a/test_suite.rb
+++ b/test_suite.rb
@@ -32,7 +32,7 @@ if Object.const_defined? 'BasicStructExample'
puts " - Basic Struct Example - "
puts "Making a new rgb struct with:"
puts " r: 10, g: 20, b: 30"
- color = BasicStructExample::Color.new(10,20,30)
+ color = Color.new(10,20,30)
puts "Red is #{color.r}"
puts "Set red to 60: #{color.r = 60}"
puts "Red is #{color.r}"