跟着 Lua 5.1 官方参考文档学习 Lua (1)

1 – Introduction

Lua is an extension programming language designed to support general procedural programming with data description facilities. It also offers good support for object-oriented programming, functional programming, and data-driven programming. Lua is intended to be used as a powerful, light-weight scripting language for any program that needs one. Lua is implemented as a library, written in clean C (that is, in the common subset of ANSI C and C++).

例子:安装LuaJIT

LuaJIT is a Just-In-Time Compiler (JIT) for the Lua programming language.

官方页面:https://luajit.org/luajit.html

安装步骤

git clone https://github.com/LuaJIT/LuaJIT.git
make 
make install

安装后的头文件路径:/usr/local/include/luajit-2.1/

ls -lh /usr/local/include/luajit-2.1/
total 44K
-rw-r--r-- 1 root root 5.9K Dec 24 12:27 lauxlib.h
-rw-r--r-- 1 root root 4.5K Dec 24 12:27 luaconf.h
-rw-r--r-- 1 root root  13K Dec 24 12:27 lua.h
-rw-r--r-- 1 root root  135 Dec 24 12:27 lua.hpp
-rw-r--r-- 1 root root 3.0K Dec 24 12:27 luajit.h
-rw-r--r-- 1 root root 1.2K Dec 24 12:27 lualib.h

安装后的库文件路径:/usr/local/lib/libluajit-5.1.so

root@ubuntu:~/luajit# ls -lh /usr/local/lib/libluajit-5.1.so
lrwxrwxrwx 1 root root 31 Dec 24 12:27 /usr/local/lib/libluajit-5.1.so -> libluajit-5.1.so.2.1.1736781742

Being an extension language, Lua has no notion of a “main” program: it only works embedded in a host client, called the embedding program or simply the host.

This host program can invoke functions to execute a piece of Lua code, can write and read Lua variables, and can register C functions to be called by Lua code.

例子:执行 Lua 代码并读取 Lua 变量

#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

int main() {
   
    lua_State *L = luaL_newstate();  // 创建一个新的 Lua 状态
    luaL_openlibs(L);  // 打开 Lua 标准库

    // 执行 Lua 代码定义一个变量
    const char *lua_code = "x = 42";
    if (luaL_dostring(L, lua_code) != LUA_OK) {
   
        printf("Error: %s\n", lua_tostring(L, -1));
        return 1;
    }

    // 获取 Lua 中的变量 x
    lua_getglobal(L, "x");  // 将 Lua 全局变量 x 的值压入栈

    // 检查栈顶的数据类型并获取其值
    if (lua_isnumber(L, -1)) {
   
        double x = lua_tonumber(L, -1);
        printf("Value of x: %f\n", x);  // 输出 x 的值
    }

    lua_pop(L, 1);  // 弹出栈顶的 x 变量

    lua_close(L);  // 关闭 Lua 状态
    return 0;
}

编译程序

gcc -o test test.c -I/usr/local/include/luajit-2.1 -lluajit-5.1

输出

Value of x: 42.000000

例子:在 Lua 中修改宿主程序的变量

#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

int main() {
   
    lua_State *L = luaL_newstate();  // 创建 Lua 状态
    luaL_openlibs(L);  // 打开 Lua 标准库

    // 定义一个变量
    int value = 10;
    
    // 将 C 变量传递给 Lua
    lua_pushnumber(L, value);  // 将 value 压栈
    lua_setglobal(L, "value");  // 将栈顶的值设置为 Lua 中的全局变量 value

    // 在 Lua 中修改变量
    if (luaL_dostring(L, "value = value * 2") != LUA_OK) {
   
        printf("Error: %s\n", lua_tostring(L, -1));
        return 1;
    }

    // 获取 Lua 中修改后的变量值
    lua_getglobal(L, "value");
    if (lua_isnumber(L, -1)) {
   
        value = lua_tonumber(L, -1);
        printf("Modified value: %d\n", value);  // 输出修改后的值
    }

    lua_close(L);  // 关闭 Lua 状态
    return 0;
}

输出

Modified value: 20

例子:从 Lua 中调用 C 函数

#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

// 定义一个 C 函数
int add_numbers(lua_State *L) {
   
    // 获取 Lua 中传入的参数
    int a = luaL_checknumber(L, 1);
    int b = luaL_checknumber(L, 2);
    
    // 将返回值压栈
    lua_pushnumber(L, a + b);
    return 1;  // 返回 1 个值
}

int main() {
   
    lua_State *L = luaL_newstate();  // 创建 Lua 状态
    luaL_openlibs(L);  // 打开 Lua 标准库

    // 注册 C 函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值