Class: RubyHToGo::ArgumentDefinition

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
TypeHelper
Defined in:
_tools/ruby_h_to_go/lib/ruby_h_to_go/argument_definition.rb

Overview

Proxy class for generating argument in go function

Constant Summary collapse

C_NAME_TO_GO_NAME =
{
  # These are reserved in Go
  "var"   => "v",
  "func"  => "fun",
  "range" => "r",
  "type"  => "t",

  # Can't use "_" as a value
  "_"     => "arg",
}.freeze

Instance Method Summary collapse

Methods included from TypeHelper

#cast_to_cgo_type, #ruby_c_type_to_go_type

Constructor Details

#initialize(definition:) ⇒ ArgumentDefinition

Returns a new instance of ArgumentDefinition.

Parameters:

  • definition (RubyHeaderParser::ArgumentDefinition)


13
14
15
# File '_tools/ruby_h_to_go/lib/ruby_h_to_go/argument_definition.rb', line 13

def initialize(definition:)
  @definition = definition
end

Instance Method Details

#cast_to_cgoString

Returns:

  • (String)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File '_tools/ruby_h_to_go/lib/ruby_h_to_go/argument_definition.rb', line 41

def cast_to_cgo
  case pointer
  when :array
    return "toCArray[#{ruby_c_type_to_go_type}, #{cast_to_cgo_type}](#{go_name})"
  when :ref_array
    return "toCArray[*#{ruby_c_type_to_go_type}, *#{cast_to_cgo_type}](#{go_name})"
  when :sref
    return go_name if type == "void" && length == 2

    return "(#{"*" * length}#{cast_to_cgo_type})(unsafe.Pointer(#{go_name}))"
  when :in_ref
    return "(*#{cast_to_cgo_type})(#{go_name})"
  end

  "#{cast_to_cgo_type}(#{go_name})"
end

#generate_go_arguments(char_var_count:, chars_var_count:) ⇒ Array<String, Array<String>, Array<String>>

Returns - casted_go_arg [String] - before_call_function_lines [Array<String>] - after_call_function_lines [Array<String>].

Parameters:

  • char_var_count (Integer)
  • chars_var_count (Integer)

Returns:

  • (Array<String, Array<String>, Array<String>>)
    • casted_go_arg [String]

    • before_call_function_lines [Array<String>]

    • after_call_function_lines [Array<String>]



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File '_tools/ruby_h_to_go/lib/ruby_h_to_go/argument_definition.rb', line 65

def generate_go_arguments(char_var_count:, chars_var_count:)
  case pointer
  when :ref
    case type
    when "char"
      generate_go_arguments_for_char_pointer(char_var_count)

    when "void"
      # c_arg is pointer
      [go_name, [], []]

    else
      c_var_name = "c#{GoUtil.snake_to_camel(go_name)}"

      before_call_function_line = "var #{c_var_name} #{cast_to_cgo_type}"
      after_call_function_line = "*#{go_name} = #{ruby_c_type_to_go_type(pos: :arg)}(#{c_var_name})"

      ["&#{c_var_name}", [before_call_function_line], [after_call_function_line]]
    end

  when :function
    ["toCFunctionPointer(#{go_name})", [], []]

  when :str_array
    generate_go_arguments_for_str_array(chars_var_count)

  else
    [cast_to_cgo, [], []]
  end
end

#go_function_argString

Returns:

  • (String)


36
37
38
# File '_tools/ruby_h_to_go/lib/ruby_h_to_go/argument_definition.rb', line 36

def go_function_arg
  "#{go_name} #{ruby_c_type_to_go_type(pointer:, pointer_length: length, pos: :arg)}"
end

#go_nameString

Returns Variable name available in Go.

Returns:

  • (String)

    Variable name available in Go



29
30
31
32
33
# File '_tools/ruby_h_to_go/lib/ruby_h_to_go/argument_definition.rb', line 29

def go_name
  return C_NAME_TO_GO_NAME[name] if C_NAME_TO_GO_NAME[name]

  name
end