Module: RubyHToGo::GoUtil

Defined in:
_tools/ruby_h_to_go/lib/ruby_h_to_go/go_util.rb

Overview

helper methods for generating go code

Constant Summary collapse

C_TYPE_TO_GO_TYPE =
{
  "RUBY_DATA_FUNC"     => "unsafe.Pointer",
  "long long"          => "Longlong",
  "rb_alloc_func_t"    => "unsafe.Pointer",
  "unsigned char"      => "Uchar",
  "unsigned int"       => "uint",
  "unsigned long"      => "uint",
  "unsigned long long" => "Ulonglong",
  "unsigned short"     => "Ushort",
}.freeze
C_TYPE_TO_CGO_TYPE =
{
  "RUBY_DATA_FUNC"       => "toCFunctionPointer",
  "long long"            => "C.longlong",
  "rb_io_wait_readwrite" => "C.enum_rb_io_wait_readwrite",
  "ruby_value_type"      => "C.enum_ruby_value_type",
  "unsigned char"        => "C.uchar",
  "unsigned int"         => "C.uint",
  "unsigned long"        => "C.ulong",
  "unsigned long long"   => "C.ulonglong",
  "unsigned short"       => "C.ushort",
  "st_hash_type"         => "C.struct_st_hash_type",
  "timespec"             => "C.struct_timespec",
  "timeval"              => "C.struct_timeval",
}.freeze

Class Method Summary collapse

Class Method Details

.cast_to_cgo_type(typename) ⇒ String

Cast C type to cgo type. (Used in wrapper function)

Parameters:

  • typename (String)

Returns:

  • (String)


90
91
92
93
94
# File '_tools/ruby_h_to_go/lib/ruby_h_to_go/go_util.rb', line 90

def self.cast_to_cgo_type(typename)
  return C_TYPE_TO_CGO_TYPE[typename] if C_TYPE_TO_CGO_TYPE[typename]

  "C.#{typename}"
end

.generate_initial_go_file(go_file_path) ⇒ Object

Generate initial go file whether not exists

Parameters:

  • go_file_path (String)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File '_tools/ruby_h_to_go/lib/ruby_h_to_go/go_util.rb', line 16

def self.generate_initial_go_file(go_file_path)
  return if File.exist?(go_file_path)

  File.binwrite(go_file_path, <<~GO)
    // THE AUTOGENERATED LICENSE. ALL THE RIGHTS ARE RESERVED BY ROBOTS.

    // WARNING: This file has automatically been generated
    // Code generated by ruby_h_to_go. DO NOT EDIT.

    package ruby

    /*
    #include "ruby.h"
    */
    import "C"

    import (
      "unsafe"
    )

  GO
end

.ruby_c_type_to_go_type(typename, pos: nil, pointer: nil, pointer_length: 0) ⇒ String

Convert C type to Go type. (used in wrapper function args and return type etc)

Parameters:

  • typename (String)
  • pos (Symbol, nil) (defaults to: nil)

    :arg, :typeref, :return

  • pointer (Symbol, nil) (defaults to: nil)

    pointer hint (:ref, :array, :ref_array, :function, :sref, :str_array, :in_ref, :raw)

  • pointer_length (Integer) (defaults to: 0)

Returns:

  • (String)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File '_tools/ruby_h_to_go/lib/ruby_h_to_go/go_util.rb', line 56

def self.ruby_c_type_to_go_type(typename, pos: nil, pointer: nil, pointer_length: 0)
  return ruby_pointer_c_type_to_go_type(typename, pos:, pointer:, pointer_length:) if pointer

  return C_TYPE_TO_GO_TYPE[typename] if C_TYPE_TO_GO_TYPE[typename]

  case typename
  when /^[A-Z]+$/, "int"
    # e.g. VALUE
    return typename
  when "void"
    return "unsafe.Pointer" if pointer == :ref && type == :typeref
  end

  snake_to_camel(typename)
end

.snake_to_camel(str) ⇒ String

Parameters:

  • str (String)

Returns:

  • (String)


8
9
10
11
12
# File '_tools/ruby_h_to_go/lib/ruby_h_to_go/go_util.rb', line 8

def self.snake_to_camel(str)
  return str if %w[VALUE ID].include?(str)

  str.split("_").map(&:capitalize).join.gsub(/(?<=\d)([a-z])/) { _1.upcase } # rubocop:disable Style/SymbolProc
end