14-07-2021

  1. Lua Variadic Arguments
  2. Lua Pass Arguments To Cmd
  3. Passing A Lua Table As An Argument To C/C++ - Engines And ...
  4. Lua Pass Arguments To Script

This tutorial will show you how to define a funcion in a Lua script and call it from your C or C program. We will cover passing arguments, returning values, and deal with global variables. Your first function in Lua. Defining functions in Lua is very simple. Start with the word 'function', followed by the function name, and a list of arguments. See full list on lucasklassmann.com. The first step is to define the function. All C or C functions that will be called from Lua will be called using a pointer of this type: typedef int (.luaCFunction) (luaState.L); In other words, functions must have a Lua interpreter as the only argument and return only an integer. Since a Lua interpreter is used for the argument, the.

Lua Variadic Arguments

In this short tutorial I'll show how to run Lua programs from C and C++ and howto expose functions to them. It's easy!

Update: The code in this post has been updated for Lua 5.2.4. I haven'tchecked if the Lua 5.3 C API is backwards-compatible with 5.2. All the codehere is available on GitHub.

The first program will just create a Lua state object and exit. It will be ahybrid between C and C++. Since the two languages must include different files,we need to discern between them by checking for the existence of the__cplusplus macro.

Notice that I'm being explicit about which version of Lua I'm using in thecode. If you trust that the Lua developers care about compatibility, you canjust #include <lua.hpp> and so on directly.

The purpose of the program is just to make sure that we can compile, link andrun it without errors.

You need to let the compiler know where it can find the include files and theLua shared library. The include files are usually located in/usr/local/include and the library files in /usr/local/lib. Search yoursystem directories if needed. To compile the above program, pass thedirectories with -I and -L, respectively.

You may swap out g++ with llvm-g++, or just c++, depending on yourcompiler. If you're using a C compiler, use gcc or llvm-gcc — butremember to rename the file to first.c.

Now try to run the program to make sure it doesn't segfault:

This one worked just fine.

Executing Lua programs from a host

The next step is to execute Lua programs from your C or C++ code. We'll createthe Lua state object as above, load a file from disk and execute it.

Put this into runlua.cpp or runlua.c:

You can reuse the compilation arguments from above:

Lua

or

Running Lua programs

Let's test this with some Lua programs. The first one prints the Lua versionand exits.

You may want to double-check that it works by running lua hello.lua. It maynot be important for this trivial program, but can become important when youtry more advanced ones.

Now try it with runlua:

You can even run bytecode-compiled programs:

We should also check that the error handling works. Put some garbage in a filecalled error.lua, for example

Running it produces

Calling C functions from Lua

It gets very interesting when Lua programs call back to your C or C++functions. We'll create a function called howdy that prints its inputarguments and returns the integer 123.

To be on the safe side, we'll declare C linkage for the function in the C++version of the program. This has to do with name mangling,but in this case, it really doesn't matter: Lua just receives a pointer to afunction, and that's that. But if you start using dynamic loading of sharedlibraries through dlopen and dlsym, this will be an issue. So let's do itcorrect from the start.

Copy the above program into a file called callback.cpp and add the howdyfunction.

We have to pass the address of this function to Lua along with a name. Put thefollowing line somewhere between the call to lua_newstate andluaL_loadfile:

Create a test program called callback.lua

Compile and test it

I told you it was easy!

What next?

Read the Lua C APIReference. You've learned enough now to get going with it. Did you see mynote about clearing the stack in howdy? You may want to investigate that.

Pass

Find out how to integrate Lua closures with your C functions.

If you want to hide or catch console output from Lua, you need to figure thatout as well. I once did it by trapping io.write(); I copied its code fromlualib.c and changed io_write to point to my own function. There isprobably a better way to do it, though. Doing so is useful for things like gameprogramming.

Lua Pass Arguments To Cmd

Use RAIIor smart pointers to manage resources like lua_State.

I also strongly recommend to try out LuaJIT.Calling into your functions there is even easier, using LuaJIT's foreignfunction library. I'll write a blog post on how todo that as well. In short, just create ordinary C functions, compile as ashared library, copy their signatures into pure Lua source code and hook themup with LuaJIT's FFIlibrary.

LuaJIT runs between 10-20 and up to 135 times faster than interpreted Lua, soit's definitely worth it.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
  • Subject: Re: Passing Arguments to a Lua Script
  • From: Rena <hyperhacker@...>
  • Date: Wed, 17 Aug 2016 17:23:02 -0400

On Aug 17, 2016 4:37 PM, 'Leinen, Rick' <RLeinen@leviton.com> wrote:
>
> Thanks for the help you have given me in the past.
>
>
>
> I have what I assume will be an easy question to answer.
>
>
>
> I have successfully been able to call a Lua function with arguments defined within a Lua script from C. I am wondering whether you can pass arguments from C when you call the script. If so, how do I assign the arguments to variables within the script?
>
>
>
> Thanks,
>
>
>
> Rick
>
>
>
>
>
>

Whatever method you use to load the script should leave a function on the stack, which you call using lua_call or similar. As described in the manual for those methods, to pass arguments to the function, you push them to the stack (after the function is already there), and tell lua_call how many arguments you pushed.

The called script receives the arguments via the ... operator:

C:
luaL_loadstring(L, 'your Lua code here');
lua_pushinteger(L, 2);
lua_pushinteger(L, 5);
lua_pushinteger(L, 8);
lua_call(L, 3, 0); //3 args, 0 return values

Lua:
local x, y, z = ...
assert(x 2)
assert(y 5)
assert(z 8)

(if fewer than 3 arguments were passed - even zero - the remaining items are set to nil. If more than 3 are passed, the extras are discarded. Use select('#', ...) to check how many arguments were passed.)

Passing A Lua Table As An Argument To C/C++ - Engines And ...

  • Follow-Ups:
    • RE: Passing Arguments to a Lua Script, Leinen, Rick
  • References:
    • Passing Arguments to a Lua Script, Leinen, Rick

Lua Pass Arguments To Script

  • Prev by Date:Re: C API - lua_next traversal of 'array' table
  • Next by Date:RE: Passing Arguments to a Lua Script
  • Previous by thread:Passing Arguments to a Lua Script
  • Next by thread:RE: Passing Arguments to a Lua Script
  • Index(es):