Skip to content

Instantly share code, notes, and snippets.

@Minalien
Created December 4, 2014 18:28
Show Gist options
  • Save Minalien/dcb83fc4e74604882029 to your computer and use it in GitHub Desktop.
Save Minalien/dcb83fc4e74604882029 to your computer and use it in GitHub Desktop.
class MyClass
def initialize(val)
@my_val = val
end
def print_val
puts "My value is #{@my_val}"
end
def my_val
@my_val
end
end
fun myclass_new(v : Int32) : Void*
inst = Pointer(MyClass).malloc(1)
inst[0] = MyClass.new v
inst as Pointer(Void)
end
fun myclass_print(instance : Void*)
inst = instance as Pointer(MyClass)
inst[0].print_val
end
local ffi = require("ffi")
ffi.cdef[[
void* myclass_new(int val);
void myclass_print(void* instance);
]]
local MyClass = {}
MyClass.new = function(val)
local inst = {}
inst.__native_instance = ffi.C.myclass_new(val)
inst.print_val = function(self)
ffi.C.myclass_print(self.__native_instance)
end
return inst
end
return MyClass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment