Skip to content

Commit

Permalink
Fixed float conversion for keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
Hadeweka committed Apr 2, 2021
1 parent ea629a0 commit 3339c57
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 27 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,15 @@ The term 'anyoli' means 'green' in the Maasai language, thus naming 'anyolite'.

* [X] Explicitly exclude pointers
* [X] Explicitly exclude procs
* [X] Added recompilation options for the Rakefile

#### Bugfixes

* [X] Fixed exception for class methods with empty regular argument list
* [X] Allow operator methods for class and module methods
* [X] Fixed path resolution for types starting with `::`
* [ ] Resolve generics as arguments for generics properly
* [X] Resolve generics as arguments for generics properly
* [X] Fix broken floats in mruby

### Version 0.9.0

Expand Down
2 changes: 2 additions & 0 deletions Rakefile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def determine_compiler
end

task :build_shard => [:load_config, :install_mruby, :build_mruby, :build_glue]
task :recompile_glue => [:load_config, :build_glue]
task :recompile => [:load_config, :build_mruby, :build_glue]

GLUE_FILES = ["return_functions", "data_helper", "script_helper", "error_helper"]

Expand Down
10 changes: 6 additions & 4 deletions examples/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
a.overload_test(arg: 3.0 / 5.0)
a.overload_test(arg: b)
a.overload_test(arg: TestModule::Test::TestEnum::Four)
a.overload_test(arg: TestModule::Test::GTIntInt.new)
a.overload_test(arg: TestModule::Test::GTIntInt.new(u: 1, v: 3))
a.overload_test

a.nilable_test(arg: 123)
Expand All @@ -71,8 +71,10 @@
puts "This function returns an enum: #{a.returns_an_enum.value}"
puts "Either an int or a string: #{a.returns_something_random.inspect}"

TestModule::Test::GTIntFloat.new.test(u: 3, v: 5.5)
TestModule::Test::GTIntInt.new.test(u: 3, v: 9)
TestModule::Test::GTIntFloat.new(u: 1, v: 0.4).test(u1: 3, v1: 5.5)
TestModule::Test::GTIntInt.new(u: 7, v: 10).test(u1: 3, v1: 9)

TestModule::Test::GTIntFloat.new(u: 1, v: 10.0).compare(other: TestModule::Test::GTIntFloat.new(u: 2, v: 5.0))

puts "Results of complicated method: #{a.complicated_method(11, 0.111, 0.1, "Hello", arg_opt_2: 1)}"
puts "Results of complicated method: #{a.complicated_method(22, 0.222, 0.2, "Hello")}"
Expand All @@ -84,7 +86,7 @@
puts "Results of complicated method: #{a.complicated_method(88, 0.888, 0.8, TestModule::Test::TestEnum::Four)}"
puts "Results of complicated method: #{a.complicated_method(99, 0.999, 0.9, arg_opt_2: 5)}"
puts "Results of complicated method: #{a.complicated_method(100, 0.000, 1.0)}"
puts "Results of complicated method: #{a.complicated_method(0, 0.0, 0.0, TestModule::Test::GTIntInt.new)}"
puts "Results of complicated method: #{a.complicated_method(0, 0.0, 0.0, TestModule::Test::GTIntInt.new(u: 1, v: 1))}"

a.happy😀emoji😀test😀😀😀(1234567)

Expand Down
4 changes: 2 additions & 2 deletions glue/return_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extern mrb_value get_string_value(mrb_state* mrb, char* value) {

extern int check_mrb_fixnum(mrb_value value) {

return mrb_fixnum_p(value);
return mrb_integer_p(value);

}

Expand Down Expand Up @@ -102,7 +102,7 @@ extern int check_mrb_data(mrb_value value) {

extern mrb_int get_mrb_fixnum(mrb_value value) {

return mrb_fixnum(value);
return mrb_integer(value);

}

Expand Down
16 changes: 9 additions & 7 deletions src/MrbInternal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ lib MrbInternal
type RClass = Void
type File = Void

alias MrbFloat = LibC::Float
alias MrbFloat = LibC::Double
alias MrbInt = Int64
alias MrbBool = UInt8
alias MrbSymbol = UInt32

union MrbValueUnion
value_float : MrbFloat
value_pointer : Void*
value_int : MrbInt
value_sym : MrbSymbol
end
{% if flag?(:anyolite_use_legacy_mruby_2_1_2) %}
union MrbValueUnion
value_float : MrbFloat
value_pointer : Void*
value_int : MrbInt
value_sym : MrbSymbol
end
{% end %}

{% if flag?(:anyolite_use_legacy_mruby_2_1_2) %}
enum MrbVType
Expand Down
32 changes: 26 additions & 6 deletions src/MrbMacro.cr
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,36 @@ module MrbMacro
{% type_var_names.each_with_index { |element, index| result = index if element == arg_type.type } %}
MrbMacro.convert_keyword_arg({{mrb}}, {{arg}}, {{type_vars[result]}}, context: {{context}}, debug_information: {{debug_information}})
{% elsif type_var_names %}
{% replacement_arg_type = arg_type.stringify.gsub(/([\(\s\:])([A-Z]+)([\),\s])/, "\\1\#\\3") %}
{% magical_regex = /([\(\s\:])([A-Z]+)([\),\s])/ %}
{% replacement_arg_type = arg_type.type.stringify.gsub(magical_regex, "\\1\#\\2\#\\3") %}

{% for type_var_name, index in type_var_names %}
{% puts "NOW: #{replacement_arg_type}, SPLIT = #{replacement_arg_type.split("\#")}" %}
{% split_types = replacement_arg_type.split("\#")[0..-2] %}
{% split_types = replacement_arg_type.split("\#") %}
{% odd = true %}
{% final_split_types = [] of ASTNode %}
{% for split_type, split_type_index in split_types %}
# TODO
{% if !odd %}
{% result = nil %}
{% type_var_names.each_with_index { |element, index| result = index if element.stringify == split_type } %}
{% if result %}
{% final_split_types.push(type_vars[result].stringify) %}
{% else %}
{% final_split_types.push(split_type) %}
{% end %}
{% else %}
{% final_split_types.push(split_type) %}
{% end %}
{% odd = !odd %}
{% end %}
{% end %}
MrbMacro.convert_keyword_arg({{mrb}}, {{arg}}, {{replacement_arg_type.id}}, context: {{context}}, debug_information: {{debug_information}})

{% if arg_type.value %}
{% final_type_def = "#{arg_type.var} : #{final_split_types.join("").id} = #{arg_type.value}" %}
{% else %}
{% final_type_def = "#{arg_type.var} : #{final_split_types.join("").id}" %}
{% end %}

MrbMacro.convert_keyword_arg({{mrb}}, {{arg}}, {{final_type_def.id}}, context: {{context}}, debug_information: {{debug_information}})
{% elsif arg_type.is_a?(Call) %}
{% raise "Received Call #{arg_type} instead of TypeDeclaration or TypeNode" %}
{% elsif arg_type.is_a?(TypeDeclaration) %}
Expand Down Expand Up @@ -741,7 +762,6 @@ module MrbMacro
{% regular_arg_array = [regular_args] %}
{% end %}

{% puts "RESOLVING #{crystal_class} -> #{crystal_class.resolve.type_vars}" if !crystal_class.resolve.type_vars.empty? %}
{% type_vars = crystal_class.resolve.type_vars %}
{% type_var_names_annotation = crystal_class.resolve.annotation(MrbWrap::SpecifyGenericTypes) %}
{% type_var_names = type_var_names_annotation ? type_var_names_annotation[0] : nil %}
Expand Down
20 changes: 14 additions & 6 deletions test.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,22 @@ module SomeModule

@[MrbWrap::SpecifyGenericTypes([U, V])]
struct GenericTest(U, V)
def test(u : U, v : V)
puts "u is #{u} and has class #{U}, v is #{v} and has class #{V}."

property u : U
property v : V

def initialize(u : U, v : V)
@u = u
@v = v
end

# TODO: Currently, this will not work
# def compare(other : GenericTest(U, V))
# puts "This has #{self.u} and #{self.v}, the other has #{other.u} and #{other.v}."
# end
def test(u1 : U, v1 : V)
puts "u1 is #{u1} and has class #{U}, v1 is #{v1} and has class #{V}."
end

def compare(other : GenericTest(U, V))
puts "This has #{@u} and #{@v}, the other has #{other.u} and #{other.v}."
end
end

alias GTIntFloat = GenericTest(Int32, Float32)
Expand Down
2 changes: 1 addition & 1 deletion utility/mruby_build_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
conf.gem :mgem => 'dir'
conf.gem :mgem => 'regexp-pcre'

conf.cc.flags << '-DMRB_USE_FLOAT -DMRB_UTF8_STRING -DMRB_INT64'
conf.cc.flags << '-DMRB_UTF8_STRING -DMRB_INT64'

conf.build_dir = ENV["MRUBY_BUILD_DIR"] || raise("MRUBY_BUILD_DIR undefined!")

Expand Down

0 comments on commit 3339c57

Please sign in to comment.