Created
April 1, 2011 13:49
-
-
Save malkia/898171 to your computer and use it in GitHub Desktop.
The goal is to wrap the ffi symbols and new ones in one table
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
local ffi = require( "ffi" ) | |
local function wraplib( lib, options ) | |
local function LOG(...) | |
if options.verbose then | |
print(...) | |
end | |
end | |
return setmetatable ( | |
{}, | |
{ | |
__index = | |
function( t, k ) | |
local v = rawget( t, k ) | |
if v ~= nil then | |
LOG( "__index: returning from table <" .. tostring(t) .. ">\n" .. | |
" the symbol " .. tostring(k) .. " = " .. tostring(v) ) | |
return v | |
end | |
local ok, v = xpcall(function() return lib[k] end, function() end) | |
if ok then | |
LOG( "__index: importing from library <" .. tostring(lib) .. ">\n" .. | |
" into table <" .. tostring(t) .. ">\n" .. | |
" the symbol " .. tostring(k) .. " = " .. tostring(v) .. "\n" ) | |
rawset( t, k, v ); | |
return v | |
end | |
LOG( "__index: can't find in table <" .. tostring(t) .. ">\n" .. | |
" the symbol " .. tostring(k) .. "\n" ) | |
error( "symbol " .. tostring( k ) .. " does not exist" ) | |
end, | |
__newindex = | |
function( t, k, nv ) | |
local ok, v = xpcall(function() return lib[k] end, function() end) | |
if ok then | |
error("symbol " .. tostring(k) .. " already exist, imported with value " .. tostring(v)) | |
end | |
local v = rawget( t, k ) | |
if v ~= nil then | |
error("symbol " .. tostring(k) .. " already exist, created with value " .. tostring(v)) | |
end | |
LOG( "__newindex: adding to table <" .. tostring(t) .. ">\n" .. | |
" the symbol " .. tostring(k) .. " = " .. tostring(nv) .. "\n" ) | |
rawset( t, k, nv ) | |
end | |
} | |
) | |
end | |
print("\n") | |
local lib = wraplib( ffi.load( "curl" ), { verbose = true } ) | |
ffi.cdef[[ | |
typedef void* CURL; | |
CURL* curl_easy_init(); | |
]] | |
ffi.cdef[[ | |
void curl_easy_cleanup( CURL* ); | |
]] | |
print("1") | |
for k,v in pairs(lib) do print(k,v) end | |
print("2") | |
local curl = lib.curl_easy_init() | |
print("3") | |
for k,v in pairs(lib) do print(k,v) end | |
print("4") | |
print( lib["curl_easy_init"] ) | |
print( lib["curl_easy_init"] ) | |
print( lib["curl_easy_init"] ) | |
function lib.curl_new_function( string ) | |
print( string ) | |
end | |
--lib.curl_new_function( "test" ) | |
--print( lib.curl_new_function ) | |
function lib.curl_easy_cleanup1( curl ) | |
print( "This might work" ) | |
end | |
lib.curl_easy_cleanup(curl) | |
ffi.cdef[[ | |
void curl_easy_cleanup( CURL* ); | |
]] | |
function lib.curl_easy_cleanup( curl ) | |
print( "This should not work" ) | |
end | |
lib.curl_easy_cleanup( curl ) | |
lib.curl_easy_cleanup = function( curl ) | |
print( "This should not work 1" ) | |
end | |
lib.curl_easy_cleanup( curl ) | |
lib.curl_easy_cleanup = function( curl ) | |
print( "This should not work 2" ) | |
end | |
lib.curl_easy_cleanup( curl ) | |
local mt = getmetatable(lib) | |
for k,v in pairs(mt) do print(k,v) end | |
for k,v in pairs(lib) do print(k,v) end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment