summaryrefslogtreecommitdiffhomepage
path: root/templates.rb
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2022-03-07 18:33:20 -0500
committerrealtradam <[email protected]>2022-03-07 18:33:20 -0500
commit9d440b24a50b54b1708123d77679477b14c4b3c0 (patch)
treec75ddb2d8d1289a4768274e93822f862e0be44bf /templates.rb
parent779d9f17adf189b45c6be2a4b3d0959019c5ad03 (diff)
downloadFelBind-9d440b24a50b54b1708123d77679477b14c4b3c0.tar.gz
FelBind-9d440b24a50b54b1708123d77679477b14c4b3c0.zip
added some struct wrapping methods
Diffstat (limited to 'templates.rb')
-rw-r--r--templates.rb67
1 files changed, 64 insertions, 3 deletions
diff --git a/templates.rb b/templates.rb
index 6f753af..4378681 100644
--- a/templates.rb
+++ b/templates.rb
@@ -14,12 +14,31 @@ mrb_mruby_#{gem_name}_gem_final(mrb_state* mrb) {
}
end
+ def non_struct_types
+ @non_struct_types ||= ['bool', 'int', 'float', 'double', 'float', 'const char *', 'unsigned int', 'void']
+ end
+
+ def init_module(module_name)
+ "struct RClass *#{module_name.downcase}_module = mrb_define_module(mrb, \"#{module_name}\");"
+ end
+
def init_module_function(module_name, function_name, mrb_function_name, mrb_args)
%{
mrb_define_module_function(mrb, #{module_name}, "#{function_name}", mrb_#{mrb_function_name}, #{mrb_args});
}
end
+ # define under needs the C name, not the ruby name which may be confusing
+ def init_class(class_name, define_under, is_struct_wrapper = true)
+ %{
+struct RClass *#{class_name.downcase}_class = mrb_define_class_under(mrb, #{define_under}, \"#{class_name}\", mrb->object_class);"#{
+ if is_struct_wrapper
+ "\nMRB_SET_INSTANCE_TT(#{class_name.downcase}_class, MRB_TT_DATA);"
+ end
+ }
+ }
+ end
+
def function(function_name, body)
%{
static mrb_value
@@ -58,10 +77,20 @@ if (mrb_undef_p(kw_values[#{kwarg_iter}])) {
}
end
+
def unwrap_struct(var_name, target, mrb_type, type)
%{#{var_name} = DATA_GET_PTR(mrb, #{target}, &#{mrb_type}, #{type})}
end
+ def wrap_struct(var_name, target, mrb_type, type)
+ %{
+#{var_name} = (#{type} *)DATA_PTR(#{target})
+if(#{var_name}) #{'{'} mrb_free(mrb, #{var_name}); #{'}'}
+mrb_data_init(#{target}, NULL, &#{mrb_type});
+#{var_name} = (#{type} *)mrb_malloc(mrb, sizeof(#{type}));
+ }
+ end
+
def define_module(module_name)
%{struct RClass *#{module_name.downcase} = mrb_define_module(mrb, "#{module_name}");
}
@@ -93,7 +122,8 @@ if (mrb_undef_p(kw_values[#{kwarg_iter}])) {
end
end
- # make a function named like a ruby one would
+ # convert a C function name to be
+ # formatted like a Ruby method name
def rubify_func_name(function)
func = function.underscore
if func.start_with? 'is_'
@@ -102,7 +132,7 @@ if (mrb_undef_p(kw_values[#{kwarg_iter}])) {
func.delete_prefix('get_')
end
- # generate a return
+ # generate a return of a ruby bound C function
def return_format(function, params)
func_rpart = function.rpartition(' ')
func_datatype = func_rpart.first
@@ -112,7 +142,7 @@ if (mrb_undef_p(kw_values[#{kwarg_iter}])) {
if params.first == 'void'
result = "#{func_name}();\nreturn mrb_nil_value();"
else
- result = "#{func_name}(" #);\nreturn mrb_nil_value();"
+ result = "#{func_name}("
result += params.first.rpartition(' ').last
params.drop(1).each do |param|
@@ -133,6 +163,37 @@ if (mrb_undef_p(kw_values[#{kwarg_iter}])) {
result
end
+ # wrapping an existing struct to be used by ruby
+ def init_struct_wrapper(struct, free_body = nil)
+ %{
+ #{"void mrb_helper_#{struct}_free(mrb_state*, void*);" if free_body}
+
+static const struct mrb_data_type mrb_#{struct}_struct = {
+"#{struct}",
+#{
+ if free_body
+ "mrb_helper_#{struct}_free"
+ else
+ "mrb_free"
+ end
+}
+ };
+ #{
+ if free_body
+
+ %{
+ void
+ mrb_helper_#{struct}_free(mrb_state* mrb, void*ptr) {
+#{struct} *struct_data = (#{struct}*)ptr;
+#{free_body}
+mrb_free(mrb, ptr);
+ }
+ }
+ end
+ }
+ }
+ end
+
end
end