惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
量子位
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
B
Blog
U
Unit 42
C
Check Point Blog
I
InfoQ
aimingoo的专栏
aimingoo的专栏
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 【当耐特】
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
云风的 BLOG
云风的 BLOG
宝玉的分享
宝玉的分享
爱范儿
爱范儿

Lua

用 Lua 写 Android 应用? AutoLua 做到了 Android 上直接用 Lua 调 Java API 写脚本——AutoLua 技术分享 使用 Android Lua Helper 在 VSCODE 中调试安卓 Lua 应用 skynet 怎么给 mysql drive 增加对 caching_sha2_password 的支持 奇怪的小问题 Lua 代码上的优化,相互学习一下 openresty + lua 这个地方怎么写 求一份 lua 入门教程 飞书 + Lua 实现企业级组织架构登录认证 为什么游戏架构要用事件来驱动? vscode 上写 lua 有显示注释的插件吗? lua 为什么要使用~=当作不等于运算符? 请问现在 lua 混淆用什么工具,求常年 lua 的老哥指点 lua 的前景如何? core_framework - 基于 libev 的轻量级 lua 网络开发框架 如何构建伪视频流服务? 低延时低采样, 基于 OpenResty , HTTP, 内网... TP-Link 官方使用的是开源固件? 表达过去、现在与将来:之将来(1) 有个问题, lua 平时能用来做做什么 ngx_lua 遇到的一个小坑 Lua IDE 各位 V 友,用 lua 作为开发语言用什么 IDE 可以方便代码提示以及调试呢 Lua 怎么实现 php strstr() 函数的功能 使用 Sol2 来进行 Lua 绑定 luarocks 使用上的一些小技巧 正在用 lua 写一个 nginx 防盗链的拓展,遇到了一个问题。 9k-18k 宁波 经验 3-5 年 本科及以上 全职 职位诱惑 : 年终奖 出国旅游 顶级办公设备及环境 关于 Lua 的标准库 Torch 一个在 MediaWiki 里面加入 Lua 脚本的插件 ngx_lua_reqstatus 实时监控 Nginx 域名 qps 的 lua 拓展
有关 Lua 调用 C++ 编译动态库程序
kanhongj · 2022-01-06 · via Lua

主要目标

希望能够将 cpp 文件编译成动态库, 以 lua 作为主要运行逻辑(main 函数)

希望解决的问题

  1. 刚入门 Lua 不太明白,若编译了一个 cpp 文件里包含了一个 Worker 对象,Lua 中如何调用生成这个对象并根据公有函数对私有变量进行操作。
  2. 查找到的一些示例代码,大多是将 main 逻辑放在 cpp 文件里,生成元表作为全局变量传入 Lua 脚本进行操作。但是将 Lua 作为程序运行入口,C++ 库作为辅助函数,又该如何操作。
  3. Lua 调试工具各位 Lua 大佬能否介绍一下,或者推荐一些调试方法,lua 的 debug 调试使用我还是有点懵。

自身尝试代码

稍微有点多,感谢耐心观看

classstudy.h

#pragma once
extern "C" {
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"
}

#include <string>

class Worker{
    public:
        Worker();
        ~Worker();

        int SetName(std::string &name);
        int SetAge(int &age);
        int SetHight(float &hight);

        std::string GetName();
        int GetAge();
        float GetHight();
    private:
        std::string name;
        int age;
        float hight;
};

extern "C" int luaopen_cstudy(lua_State *L);

classstudy.cpp

#include "classstudy.h"

#include <iostream>


/*
Class Worker 的函数定义省略
*/

static int CreateNewWorker(lua_State *L){
    //ligth_userdata
    // int n = luaL_checkany(L, 1);
    Worker **w1 = (Worker**)lua_newuserdata(L, sizeof(Worker*));
    *w1 = new Worker();
    if(luaL_getmetatable(L, "WorkerClass") == false){
        printf("getmetatable nil\n");
    }
    lua_setmetatable(L, -2);
    return 1;
}

static int SetWorkerName(lua_State *L){

    luaL_checktype(L, -1, LUA_TSTRING);
    std::string var_name = lua_tostring(L, -1);
    printf("%s\n", var_name.c_str());

    Worker **w1 = (Worker**)lua_newuserdata(L, sizeof(Worker*));
    luaL_argcheck(L, w1 != NULL, 1, "invalid user data");
    (*w1)->SetName(var_name);

    return 0;
}

static int SetWorkerAge(lua_State *L){

	luaL_checktype(L, -1, LUA_TNUMBER);
    int var_age = lua_tointeger(L, -1);
    Worker **w1 = (Worker**)lua_newuserdata(L, sizeof(Worker*));
    luaL_argcheck(L, w1 != NULL, 1, "invalid user data");
    (*w1)->SetAge(var_age);
    
    return 0;
}

static int SetWorkerHight(lua_State *L){

    luaL_checktype(L, -1, LUA_TNUMBER);
    float var_hight = lua_tonumber(L, -1);
    Worker **w1 = (Worker**)lua_newuserdata(L, sizeof(Worker*));
    luaL_argcheck(L, w1 != NULL, 1, "invalid user data");
    (*w1)->SetHight(var_hight);

    return 0;
}

static int GetWorkerInfo(lua_State *L){

    Worker **w1 = (Worker**)lua_newuserdata(L, sizeof(Worker*));
    luaL_argcheck(L, w1 != NULL, 1, "invalid user data");
    printf("Name: %s\n", ((*w1)->GetName()).c_str());
    printf("Age: %d\n", (*w1)->GetAge());
    printf("Hight: %f\n", (*w1)->GetHight());
    return 0;
}

static int DestoryInfo(lua_State* L)
{
    // 释放对象
    delete *(Worker**)lua_topointer(L, 1);
    return 0;
}

const static luaL_Reg mylib[] = {
    // {"NewWorker", CreateNewWorker},
    {"SetName", SetWorkerName},
    {"SetAge", SetWorkerAge},
    {"SetHight", SetWorkerHight},
    {"PrintInfo", GetWorkerInfo},
    {NULL,NULL}
};

int luaopen_cstudy(lua_State *L){
    
    // C\++对象     = 私有数据 + 类(公共数据 + 公共方法)  
    // Lua Table  = 私有数据 + 元表(元数据 + 元函数)

    //  luaL_newlib(L, mylib);
    lua_pushcfunction(L, CreateNewWorker);    // 注册用于创建类的全局函数
    lua_setglobal(L,  "CWorker");
    luaL_newmetatable(L, "WorkerClass");
    
    // 设置自身
    lua_pushstring(L, "__gc");
    lua_pushcfunction(L, DestoryInfo);
    lua_settable(L, -3);
    
    // 设置元表
    lua_pushstring(L, "__index"); // 设置元表为自己
    lua_pushvalue(L, -2);
    lua_settable(L, -3);

    lua_pushstring(L, "SetName");
    lua_pushcfunction(L, SetWorkerName);
    lua_settable(L, -3);

    lua_pushstring(L, "SetAge");
    lua_pushcfunction(L, SetWorkerAge);
    lua_settable(L, -3);

    lua_pushstring(L, "SetHight");
    lua_pushcfunction(L, SetWorkerHight);
    lua_settable(L, -3);

    lua_pushstring(L, "PrintInfo");
    lua_pushcfunction(L, GetWorkerInfo);
    lua_settable(L, -3);
    // lua_pop(L, 1);

    return 1;
}

classstudy.lua

local csl = require("cstudy")
local Worker = CWorker()
print(debug.getregistry())

Worker.SetName("hello")
Worker.PrintInfo()
-- csl.SetAge(23)
-- csl:SetHight(180.0)
-- csl:PrintInfo()