Lua Ffi How To Run A C Function

Benchmark of LuaJIT FFI callbacks from C into Lua

Lua has a very simple C API. You can run Lua code and access lua objects from C. Similarly, you can access C functions and libraries from Lua. In this post, we’ll primarily look at how to expose C. Lua C Table Iteration. The bug is in luanext(L, -2) line, because -2 refers to stack top minus one, which happens here to be the last argument to print. Use luanext(L, i) instead. Upd: Lua stack indexes are subject to float when moving code around at development stage, so the general advice is to. One Way to Call a Lua Function from C. Now that we have the Lua code to draw mazes, let’s see how to write a C program that can run the Lua file. As examples of why this is useful, you could run a Lua script from Objective-C in an iOS app, or run a cross-platform Lua script from platform-specific C or C code. See full list on luajit.org.

callback-bench.lua
local ffi =require('ffi')
ffi.cdef[[
typedefvoid (*cb)(void);
voidcall(int n, void (*)(void));
voidloop(int n);
voidfunc(void);
]]
local callback = ffi.load('./callback.so')
local timeit =require('timeit')
localfunctionlfunc() end
print('C into C', timeit(function(n)
callback.call(n, callback.func)
end))
print('Lua into C', timeit(function(n)
for i =1, n do callback.func() end
end))
print('C into Lua', timeit(function(n)
callback.call(n, lfunc)
end))
print('Lua into Lua', timeit(function(n)
for i =1, n dolfunc() end
end))
print('C empty loop', timeit(function(n)
callback.loop(n)
end))
print('Lua empty loop', timeit(function(n)
for i =1, n doend
end))
callback.c
Lua
voidcall(int n, void (*f)(void)) {
for (int i = 0; i < n; i++) {
f();
}
}
voidloop(int n) {
for (int i = 0; i < n; i++) {
/* prevent compiler optimizations from skipping loop entirely */
__asm__('');
}
}
voidfunc(void) {
}
timeit.lua
--- call a function with a repeat count argument.
-- Takes a single argument, which is a function which in turn takes one argument,
-- a repeat count. The function is called with increasingly large repeat counts
-- until it takes at least a certain amount of time to run, and is then called four
-- more times with the same repeat count, with the minimum elapsed time recorded.
-- Modeled loosely on Python's timeit, except the function passed in is responsible
-- for doing the actual repetition.
returnfunction(func)
local reps =10000
local elapsed
repeat
reps = reps *10
local start =os.clock()
func(reps)
elapsed =os.clock() - start
until elapsed >0.05or reps >=1e9
for i =1, 4do
local start =os.clock()
func(reps)
elapsed =math.min(elapsed, os.clock() - start)
end
return ('%7.3f nsec/call'):format(elapsed / reps *1e9)
end
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

See Full List On Luajit.org

This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.
The fourth edition targets Lua 5.3 and is available at Amazon and other bookstores.
By buying the book, you also help to support the Lua project.

Programming in Lua
Part IV. The C APIChapter 26. Calling C from Lua
Ktap FFI Proposal - cs.cmu.edu

LuaJIT FFI Callbacks (was Re: Calling Lua Functions From C ...

One of the basic means for extending Luais for the application to registernew C functions into Lua.

Lua Ffi How To Run A C Function Diagram

When we say that Lua can call C functions,this does not mean that Lua can call any C function.(There are packages that allow Lua to call any C function,but they are neither portable nor robust.)As we saw previously,when C calls a Lua function,it must follow a simple protocol to pass the arguments andto get the results.Similarly,for a C function to be called from Lua,it must follow a protocolto get its arguments and to return its results.Moreover, for a C function to be called from Lua,we must register it, that is,we must give its address to Lua in an appropriate way.

When Lua calls a C function,it uses the same kind of stack that C uses to call Lua.The C function gets its arguments from the stackand pushes the results on the stack.To distinguish the results from other values on the stack,the function returns (in C) the number of results itis leaving on the stack.An important concept here is that the stack is not a global structure;each function has its own private local stack.When Lua calls a C function,the first argument will always be at index 1 of this local stack.Even when a C function calls Lua code that calls thesame (or another) C function again,each of these invocations sees only its own private stack,with its first argument at index 1.

See Full List On Chsasank.github.io

Copyright © 2003–2004 Roberto Ierusalimschy. All rights reserved.