-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
45 lines (32 loc) · 843 Bytes
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require 'ffi'
class ULongPtr < FFI::Struct
layout :value, :ulong
end
class UCharPtr < FFI::Struct
layout :value, :char
end
class Point < FFI::Struct
layout :x, :uchar,
:y, :uchar
end
module PointsLib
extend FFI::Library
ffi_lib "zig-out/lib/libpoints.so"
attach_function :generatePoints, [ULongPtr, UCharPtr], :bool
attach_function :freePoints, [:ulong, :uchar], :void
end
class Points
def self.coordinates
addr_ptr = ULongPtr.new
len_ptr = UCharPtr.new
PointsLib.generatePoints(addr_ptr, len_ptr) or raise("Error in Zig")
addr, len = addr_ptr[:value], len_ptr[:value]
points_ptr = FFI::Pointer.new(Point.size, addr)
coords = (0...len).map do |i|
point = Point.new(points_ptr[i])
[point[:x], point[:y]]
end
PointsLib.freePoints addr, len
coords
end
end