Lua C# Integration

The FFI library allows calling external C functions and using C data structures from pure Lua code. The FFI library largely obviates the need to write tedious manual Lua/C bindings in C. No need to learn a separate binding language — it parses plain C declarations! These can be cut-n-pasted from C header files or reference manuals.

Download ULua

More than 300 binary packages updated daily from Luarocks

For Windows (7 onward), OSX (10.5 onward) and Linux (2009 onward) in x86 and x64 variants

No compiler required, no external dependencies, portable, powered by LuaJIT


Unzip the downloaded file into an arbitrary folder. This will create a single sub-folder named ulua which contains the ULua distribution:


The lua executable starts LuaJIT, the Lua manual and the LuaJIT manual document its usage:

By default LuaJIT runs in 32-bit mode, to run it in 64-bit mode set the environment variable BIT=64:

On Linux, if LuaJIT 32-bit is executed on a distribution lacking the 32-bit C runtime, this will result in a meaningless error such as luajit: command not found. Either launch LuaJIT in 64-bit mode or install the required Linux libraries. OSX 10.15 (Catalina) similarly does not support 32-bit applications any longer, launching the 32-bit version of LuaJIT will result in the error message luajit: Bad CPU type in executable. Use the 64-bit version of LuaJIT on OSX 10.15 onward.

Lua C# Integration Tool


The upkg executable provides a convenient way of managing ULua:

ULua features LuaRocks integration: new and updated rocks are automatically tracked, compiled and made available in ULua. More than 300 of such packages are currently available for installation, see LuaRocks Modules.

Lua C# Integration

ULua also features a number of Native packages, i.e. packages that are developed directly for ULua according to its package manager specifications.

ZeroBrane Studio Integration

Lua C Integration

It's possible to configure ZeroBrane Studio to use ULua, in which case ULua's installed packages become available inside of ZeroBrane and ZeroBrane itself runs on ULua's binaries.

First of all (this is only needed to support ZeroBrane's Lua debuger feature) make sure that mobdebug is installed:

To configure ZeroBrane to use ULua set the path of ZeroBrane's Lua (5.1) interpreter in the IDE (Edit -> Preferences -> Settings: User -> user.lua:) to ULua's executable:

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 24. An Overview of the C API

Lua is an embedded language.That means that Lua is not a stand-alone package,but a library that can be linked with other applicationsso as to incorporate Lua facilities into these applications.

You may be wondering:If Lua is not a stand-alone program,how come we have been using Lua stand alone through the whole book?The solution to this puzzle is the Lua interpreter(the executable lua).This interpreter is a tiny application(with less than five hundred lines of code)that uses the Lua library to implement the stand-alone interpreter.This program handles the interface with the user,taking her files and strings to feed them to the Lua library,which does the bulk of the work (such as actually running Lua code).

This ability to be used as a library to extend an applicationis what makes Lua an extension language.At the same time, a program that uses Lua can register newfunctions in the Lua environment;such functions are implemented in C (or another language)and can add facilities that cannot bewritten directly in Lua.This is what makes Lua an extensible language.

These two views of Lua(as an extension language and as an extensible language)correspond to two kinds of interaction between C and Lua.In the first kind, C has the control and Lua is the library.The C code in this kind of interactionis what we call application code.In the second kind, Lua has the control and C is the library.Here, the C code is called library code.Both application code and library code usethe same API to communicate with Lua,the so called C API.

The C API is the set of functions that allow C codeto interact with Lua.It comprises functions to read and write Lua global variables,to call Lua functions,to run pieces of Lua code,to register C functions so that they can later be called by Lua code,and so on.(Throughout this text, the term 'function' actually means'function or macro'.The API implements several facilities as macros.)

The C API follows the C modus operandi,which is quite different from Lua.When programming in C,we must care about type checking (and type errors),error recovery, memory-allocation errors,and several other sources of complexity.Most functions in the API do not check the correctness of their arguments;it is your responsibility to make sure that the argumentsare valid before calling a function.If you make mistakes,you can get a 'segmentation fault' error or something similar,instead of a well-behaved error message.Moreover, the API emphasizes flexibility and simplicity,sometimes at the cost of ease of use.Common tasks may involve several API calls.This may be boring,but it gives you full control over all details,such as error handling, buffer sizes, and the like.

As its title says,the goal of this chapter is to give an overview of what isinvolved when you use Lua from C.Do not bother understanding all the details of what is going on now.Later we will fill in the details.Nevertheless,do not forget that you can find more details about specific functionsin the Lua reference manual.Moreover, you can find several examples of the use of the API in theLua distribution itself.The Lua stand-alone interpreter (lua.c)provides examples of application code,while the standard libraries (lmathlib.c, lstrlib.c, etc.)provide examples of library code.

From now on, we are wearing a C programmers' hat.When we talk about 'you', we mean you when programming in C,or you impersonated by the C code you write.

A major component in the communication between Lua and C is anomnipresent virtual stack.Almost all API calls operate on values on this stack.All data exchange from Lua to C and from C to Lua occursthrough this stack.Moreover, you can use the stack to keep intermediateresults too.The stack helps to solve two impedance mismatches between Lua and C:The first is caused by Lua being garbage collected,whereas C requires explicit deallocation;the second results from the shock between dynamic typing in Luaversus the static typing of C.We will discuss the stack in more detail in Section 24.2.

Lua C# Integration Example

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