Created
October 22, 2019 11:10
-
-
Save vascoosx/38a68b8a320e3589abc3e37a56350d87 to your computer and use it in GitHub Desktop.
Nim/Ruby FFI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| source 'https://rubygems.org' | |
| gem 'ffi' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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