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.
Use the Dumpbin tool like this: Dumpbin /EXPORTS. Using Lua from a C#.Net project. Create a new.Net console application. In the example I use a console app, because Lua will then be able to output to the stdout of the console window. I suggest you make a static class to wrap the Lua dll. Register the C functions you want lua to use Call a lua function from within C, either by a call to luaLdofile, or by calling a lua function directly The lua function that you called from C can access the C.
|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 (
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.
Lua Use Classes
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.
Use Lua Or Co
|Copyright © 2003–2004 Roberto Ierusalimschy. All rights reserved.|