Introduction to Luaplus: Useful lua for C + + extensions

Source: Internet
Author: User
Tags lua svn zip

Luaplus is the LUA C + + enhancement, which means that Luaplus itself is enhanced by the LUA source. It is a better choice to cooperate with C + +.

The current version of Luaplus is: Luaplus for Lua 5.01 Distribution build 1080 (February 28, 2004). You can go to the http://luaplus.org/site to download:

Source Code (HTTP://WWHIZ.COM/LUAPLUS/LUAPLUS50_BUILD1081.ZIP)

Target Code (HTTP://WWHIZ.COM/LUAPLUS/LUAPLUS50_BUILD1081_WIN32BINARIES.ZIP)

Introduction Luaplus: Easy LUA for C + + extended mu Maple Blog

[Since the LUA kernel was upgraded to 5.1, Luaplus also followed the upgrade.) The latest luaplus can be retrieved via SVN, address svn://svn.luaplus.org/luaplus/work51, and Luaplus's kernel based on 5.0 is still being updated and can be retrieved via SVN, address svn:// Svn.luaplus.org/root/luaplus/dev]

I'll show you how to use luaplus and how to make it more convenient for luaplus to work seamlessly with C + + classes.

1. Call Lua Scripts

// 创建Lua解释器:
LuaStateOwner state;

// 执行Lua脚本:
state->DoString("print('Hello World\\n')");
// 载入Lua脚本文件并执行:
state->DoFile("C:\\test.lua");
// 载入编译后的Lua脚本文件并执行:
state->DoFile("C:\\test.luac");

2. Call each other with LUA scripts

// 为Lua脚本设置变量
state->GetGlobals().SetNumber("myvalue", 123456);
// 获得Lua变量的值
int myvalue = state->GetGlobal("myvalue").GetInteger();

// 调用Lua函数
LuaFunction<int> luaPrint = state->GetGlobal("print");
luaPrint("Hello World\n");

// 让Lua调用C语言函数
int add(int a, int b){ return a+b;}
state->GetGlobals().RegisterDirect("add", add);
state->DoString("print(add(3,4))");

// 让Lua调用C++类成员函数
class Test{public: int add(int a, int b){return a+b;}};
Test test;
state->GetGlobals().RegisterDirect("add", test, &Test::add);
state->DoString("print(add(3,4))");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.