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

推荐订阅源

博客园 - 【当耐特】
Help Net Security
Help Net Security
P
Proofpoint News Feed
J
Java Code Geeks
爱范儿
爱范儿
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
Google DeepMind News
Google DeepMind News
H
Help Net Security
G
Google Developers Blog
Jina AI
Jina AI
Vercel News
Vercel News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
Lohrmann on Cybersecurity
S
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
Security Archives - TechRepublic
Security Archives - TechRepublic
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic
GbyAI
GbyAI
B
Blog
O
OpenAI News
博客园_首页
Cisco Talos Blog
Cisco Talos Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
M
MIT News - Artificial intelligence
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
Webroot Blog
Webroot Blog
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
C
Cisco Blogs
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
AI
AI
W
WeLiveSecurity
aimingoo的专栏
aimingoo的专栏
The Register - Security
The Register - Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale

Lua

用 Lua 写 Android 应用? AutoLua 做到了 - V2EX Android 上直接用 Lua 调 Java API 写脚本——AutoLua 技术分享 - V2EX 使用 Android Lua Helper 在 VSCODE 中调试安卓 Lua 应用 - V2EX skynet 怎么给 mysql drive 增加对 caching_sha2_password 的支持 - V2EX 奇怪的小问题 - V2EX Lua 代码上的优化,相互学习一下 - V2EX openresty + lua 这个地方怎么写 - V2EX 求一份 lua 入门教程 - V2EX 飞书 + Lua 实现企业级组织架构登录认证 - V2EX 为什么游戏架构要用事件来驱动? - V2EX vscode 上写 lua 有显示注释的插件吗? - V2EX lua 为什么要使用~=当作不等于运算符? - V2EX 请问现在 lua 混淆用什么工具,求常年 lua 的老哥指点 - V2EX lua 的前景如何? - V2EX core_framework - 基于 libev 的轻量级 lua 网络开发框架 如何构建伪视频流服务? 低延时低采样, 基于 OpenResty , HTTP, 内网... TP-Link 官方使用的是开源固件? 表达过去、现在与将来:之将来(1) - V2EX 有个问题, lua 平时能用来做做什么 - V2EX ngx_lua 遇到的一个小坑 - V2EX Lua IDE 各位 V 友,用 lua 作为开发语言用什么 IDE 可以方便代码提示以及调试呢 - V2EX Lua 怎么实现 php strstr() 函数的功能 - V2EX 使用 Sol2 来进行 Lua 绑定 - V2EX luarocks 使用上的一些小技巧 - V2EX 正在用 lua 写一个 nginx 防盗链的拓展,遇到了一个问题。 - V2EX 9k-18k 宁波 经验 3-5 年 本科及以上 全职 职位诱惑 : 年终奖 出国旅游 顶级办公设备及环境 - V2EX 关于 Lua 的标准库 - V2EX Torch - V2EX 一个在 MediaWiki 里面加入 Lua 脚本的插件 - V2EX ngx_lua_reqstatus 实时监控 Nginx 域名 qps 的 lua 拓展 - V2EX 首页搜索到 14 个 python,现在应该是 15 个了,为神马没有 lua 一席之地 - V2EX 学习 ngx_lua - V2EX 用 Lua 写 Telegram 的 Bot - V2EX Lua 资料收集整理 - V2EX 云风翻译了 Lua 5.3 的手册 - V2EX 我对 Lua coroutine 的理解,希望有人能指点下有哪些更适合的使用场景或高阶用法~ - V2EX Lua 5.3 - V2EX 为什么 Lua 本身不包含像 endswith() 之类的特别常用的字符串函数呢? - V2EX 为 Atom 写了一个 linter-luacheck,希望大家喜欢 - V2EX lua 如何最快速度入门 - V2EX 学习 Lua,请问各位有什么书推荐? - V2EX lua 很牛啊,有人在服务端用 ta 么? - V2EX LÖVE - V2EX Scripting Nginx with Lua - V2EX 关于 openssl aes-256-cbc 的一些参数 - V2EX LuaJIT 2.1 PPA - V2EX 谁会Luci的cbi模块?怎样实现重启服务? - V2EX lua有没有3des加密解密的模块? - V2EX 关于学习 Lua 有什么好书推荐么? - V2EX lua好像有种简化的语法,叫啥来着 - V2EX
有关 Lua 调用 C++ 编译动态库程序 - V2EX
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()