Skip to content

Instantly share code, notes, and snippets.

@vascoosx
Created October 22, 2019 11:10
Show Gist options
  • Select an option

  • Save vascoosx/38a68b8a320e3589abc3e37a56350d87 to your computer and use it in GitHub Desktop.

Select an option

Save vascoosx/38a68b8a320e3589abc3e37a56350d87 to your computer and use it in GitHub Desktop.
Nim/Ruby FFI
# frozen_string_literal: true
require 'ffi'
# ffi test with code compiled from nim
module FfiStringTest
extend FFI::Library
ffi_lib 'c'
ffi_lib './libsample.so'
attach_function :nimlog, [:pointer], :int
end
strptrs = %w[one two].map { |s| FFI::MemoryPointer.from_string(s) }
strarray = FFI::MemoryPointer.new(:pointer, strptrs.length)
strptrs.each_with_index do |p, i|
strarray[i].put_pointer(0, p)
end
FfiStringTest.nimlog(strarray)
# frozen_string_literal: true
source 'https://rubygems.org'
gem 'ffi'
proc log(x: array[2, cstring]): int {.exportc: "nimlog"} =
for s in x:
write(stdout, s)
write(stdout, "\n")
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment