<commit_before>require 'ffi'

module Awesome
  extend FFI::Library
  ffi_lib 'awesomelib/awesome.so'
  class GoSlice < FFI::Struct
    layout :data,  :pointer,
           :len,   :long_long,
           :cap,   :long_long
  end
  class GoString < FFI::Struct
    layout :p,     :pointer,
           :len,   :long_long
  end

  attach_function :Add, [:long_long, :long_long], :long_long
  attach_function :Cosine, [:double], :double
  attach_function :Sort, [GoSlice.by_value], :void
end

print "awesome.Add(12, 99) = ",  Awesome.Add(12, 99), "\n"
print "awesome.Cosine(1) = ", Awesome.Cosine(1), "\n"

# call Sort
# First prepare data mapping for
# C typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
nums = [92,101,3,44,7]
ptr = FFI::MemoryPointer.new :long_long, nums.size
ptr.write_array_of_long_long  nums

slice = Awesome::GoSlice.new
slice[:data] = ptr
slice[:len] = nums.size
slice[:cap] = nums.size
Awesome.Sort(slice)
sorted = slice[:data].read_array_of_long_long nums.size
print "awesome.Sort(", nums, ") = ", sorted, "\n"<commit_msg>Add awesome.Log fn call<commit_after>require 'ffi'

# Module to define shared lib
module Awesome
  extend FFI::Library
  
  ffi_lib 'awesomelib/awesome.so'
  
  class GoSlice < FFI::Struct
    layout :data,  :pointer,
           :len,   :long_long,
           :cap,   :long_long
  end

  class GoString < FFI::Struct
    layout :p,     :pointer,
           :len,   :long_long
  end

  # foreign function definitions
  attach_function :Add, [:long_long, :long_long], :long_long
  attach_function :Cosine, [:double], :double
  attach_function :Sort, [GoSlice.by_value], :void
  attach_function :Log, [GoString.by_value], :int
end

# Call Add
print "awesome.Add(12, 99) = ",  Awesome.Add(12, 99), "\n"

# Call Cosine
print "awesome.Cosine(1) = ", Awesome.Cosine(1), "\n"

# call Sort
# Prepare type mapping for
# C typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
# Create poninter to array
nums = [92,101,3,44,7]
ptr = FFI::MemoryPointer.new :long_long, nums.size
ptr.write_array_of_long_long  nums
# Create new GoSlice object to map to struct
slice = Awesome::GoSlice.new
slice[:data] = ptr
slice[:len] = nums.size
slice[:cap] = nums.size
Awesome.Sort(slice)
# retrieve sorted data
sorted = slice[:data].read_array_of_long_long nums.size
print "awesome.Sort(", nums, ") = ", sorted, "\n"

# Call Log
# Prepare type mapping for 
# typedef struct { const char *p; GoInt n; } GoString;
msg = "Hello Ruby!"
str = Awesome::GoString.new
str[:p] = FFI::MemoryPointer.from_string(msg) 
str[:len] = msg.size
print "logid ", Awesome.Log(str), "\n"
{'fraction_non_alphanumeric': 0.14055299539170507, 'fraction_numerical': 0.014592933947772658, 'mean_word_length': 4.0, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6141652', 'n_tokens_mistral': 994, 'n_tokens_neox': 956, 'n_words': 247}