Lua C API

Lua 5.4 Reference Manual - contents官方网站

void lua_call(lua_State *L, int nargs, int nresults);
void lua_callK(lua_State *L, int nargs, int nresults, lua_KContext ctx, lua_KFunction k);// allows the called function to yield


typedef int (*lua_CFunction)(lua_State *L);//自定义C函数,用于操作Lua中的变量或函数。

int lua_checkstack(lua_State *L, int n);//Ensures that the stack has space for at least n extra elements.

int lua_compare(lua_State *L, int index1, int index2, int op);


void lua_createtable(lua_State *L, int narr, int nrec);
//Parameter narr is a hint for how many elements the table will have as a sequence;
//Parameter nrec is a hint for how many other elements the table will have.


int lua_getfield(lua_State *L, int index, const char *k);
//Pushes onto the stack the value t[k], where t is the value at the given index;

int lua_getglobal(lua_State *L, const char *name);
//Pushes onto the stack the value of the global name.Returns the type of that value.

int lua_geti(lua_State *L, int index, lua_Integer i);
//Pushes onto the stack the value t[i], where t is the value at the given index.

int lua_getmetatable(lua_State *L, int index);
//If the value at the given index has a metatable, the function pushes that metatable onto the stack and returns 1.


int lua_gettable(lua_State *L, int index);
Pushes onto the stack the value t[k], where t is the value at the given index and k is the value on the top of the stack.

int lua_gettop(lua_State *L);
//Returns the index of the top element in the stack.

int lua_iscfunction(lua_State *L, int index)
int lua_isfunction(lua_State *L, int index)

int lua_isnumber(lua_State *L, int index)
Returns 1 if the value at the given index is a number or a string convertible to a number.
int lua_isstring(lua_State *L, int index)
Returns 1 if the value at the given index is a string or a number.


typedef ... lua_KContext;


lua_State *lua_newstate(lua_Alloc f, void *ud);
//Creates a new independent state and returns its main thread.

void lua_newtable(lua_State *L);
//Creates a new empty table and pushes it onto the stack. It is equivalent to lua_createtable(L, 0, 0).


int lua_next(lua_State *L, int index);
//Pops a key from the stack, and pushes a key-value pair from the table at the given index.


int lua_pcall(lua_State *L, int nargs, int nresults, int msgh);
//Calls a function(or a callable object) in protected mode.
//Like lua_call, lua_pcall always removes the function and its arguments from the stack.

int lua_pcallk(lua_State *L, int nargs, int nresults, int msgh, lua_KContext ctx, lua_KFunction k);

void lua_pop(lua_State *L, int n);
void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n);
//Pushes a new C closure onto the stack.The parameter n tells how many upvalues this function will have.
//When a C function in created, it is possible to associate some values with it, the so called upvalues; these upvalues are the accessible to the function whenever it is called. This association is called a C closure.
//When n is zero, this function creates a light C function, which is just a pointer to the C function.

void lua_pushcfunction(lua_State *L, lua_CFunction f);
//Pushes a C function onto the stack.This function is equivalent to lua_pushcclosure with no upvalues.

void lua_pushglobaltable(lua_State *L)
//Pushes the global environment onto the stack.

void lua_pushlightuserdata(lua_State *L, void *P);
//Pushes a light userdata onto the stack.
//Userdata represent C values in Lua. A light userdata represents a pointer.

void lua_pushvalue(lua_State *L, int index)
//Pushes a copy of the element at the given index onto the stack.

void lua_rawset(lua_State *L, int index)
//Similar to lua_settable, but does a raw access. the value at index must be a table.
void lua_rawget(lua_State *L, int index)
//Similar to lua_gettable, but does a raw access. The value at index must be a table.

int lua_rawgetp(lua_State *L, int index, const void *P);
//Pushes onto the stack the value t[k], where t is the table at the given index and k is the pointer p represented as a light userdata. Returns the type of the pushed value.

void lua_register(lua_State *L, const char *name, lua_CFunction f);
//Sets the C function f as the new value of global name.It is defined as a macro:
//#define lua_register(L, n, f) (lua_pushcfunction(L,f), lua_setglobal(L, n))

void lua_remove(lua_State *L, int index)
//Removes the element at the given valid index, shifting down the elements above this index to fill the gap.

void lua_setfield(lua_State *L, int index, const char *k)
//Does the equivalent to t[k] = v, where t is the value at the given index and v is the value on the top of the stack.

void lua_setglobal(lua_State *L, const char *name)
//Pops a value from the stack and sets it as the new value of global name.

int lua_setiuservalue(lua_State *L, int index, int n)
//Pops a value from the stack and sets it as the new n-th user value associated to the full userdata at the given index. Returns 0 if the userdata does not have that value.

int lua_setmetatable(lua_State *L, int index)
//Pops a table or nil from the stack and sets that value as the new metatable for the value at the given index.(nil means no metatable)

void lua_settable(lua_State *L, int index);
//Does the equivalent to t[k] = v, where t is the value at the given index, v is the value on the top of the stack, and k is the value just below the top.

void lua_settop(lua_State *L, int index);

//转换函数
int lua_toboolean(lua_State *L, int index);
//Converts the Lua value at the given index to a C boolean value (0 or 1)

lua_CFunction lua_tocfunction(lua_State *L, int index);
//Converts a value at the given index to a C function. That value must be a C function.

const void* lua_topointer(lua_State *L, int index)
//Converts the value at the given index to a generic C pointer(void*). The value can be a userdata, a table, a thread, a string, or a function; otherwise returns NULL.

void* lua_touserdata(lua_State *L, int index)
//If the value at the given index is a full userdata, returns its memory-block address. If the value is a light userdata, returns its value(a pointer). otherwise returns NULL.

int lua_upvalueindex(int i);
//Returns the pseudo-index that represents the i-th upvalue of the running function. i must be in the range [1,256]

void lua_toclose(lua_State *L, int index);

void

Unlua、Slua均是腾讯开源的UE lua框架。

// Tencent is pleased to support the open source community by making UnLua available.
// 
// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the MIT License (the "License"); 
// you may not use this file except in compliance with the License. You may obtain a copy of the License at
//
// http://opensource.org/licenses/MIT
//
// Unless required by applicable law or agreed to in writing, 
// software distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
// See the License for the specific language governing permissions and limitations under the License.

#include "UELib.h"
#include "Binding.h"
#include "Registries/ClassRegistry.h"
#include "LuaCore.h"
#include "LuaDynamicBinding.h"
#include "LuaEnv.h"
#include "Registries/EnumRegistry.h"

static const char* REGISTRY_KEY = "UnLua_UELib";
static const char* NAMESPACE_NAME = "UE";

static int UE_Index(lua_State* L)
{
    const int32 Type = lua_type(L, 2);
    if (Type != LUA_TSTRING)
        return 0;

    const char* Name = lua_tostring(L, 2);
    const auto Exported = UnLua::FindExportedNonReflectedClass(Name);
    if (Exported)
    {
        Exported->Register(L);
        lua_rawget(L, 1);
        return 1;
    }

    const char Prefix = Name[0];
    const auto& Env = UnLua::FLuaEnv::FindEnvChecked(L);
    if (Prefix == 'U' || Prefix == 'A' || Prefix == 'F')
    {
        const auto ReflectedType = UnLua::FClassRegistry::LoadReflectedType(Name + 1);
        if (!ReflectedType)
            return 0;

        if (ReflectedType->IsNative())
        {
            if (auto Struct = Cast<UStruct>(ReflectedType))
                Env.GetClassRegistry()->Register(Struct);
        }
        else
        {
            UE_LOG(LogUnLua, Warning, TEXT("attempt to load a blueprint type %s with UE namespace, use UE.UClass.Load or UE.UObject.Load instead."), UTF8_TO_TCHAR(Name));
            return 0;
        }
    }
    else if (Prefix == 'E')
    {
        const auto ReflectedType = UnLua::FClassRegistry::LoadReflectedType(Name);
        if (!ReflectedType)
            return 0;

        if (ReflectedType->IsNative())
        {
            if (auto Enum = Cast<UEnum>(ReflectedType))
                Env.GetEnumRegistry()->Register(Enum);
        }
        else
        {
            UE_LOG(LogUnLua, Warning, TEXT("attempt to load a blueprint enum %s with UE namespace, use UE.UObject.Load instead."), UTF8_TO_TCHAR(Name));
            return 0;
        }
    }

    lua_rawget(L, 1);
    return 1;
}

extern int32 UObject_Load(lua_State *L);
extern int32 UClass_Load(lua_State *L);
static int32 Global_NewObject(lua_State *L)
{
    int32 NumParams = lua_gettop(L);
    if (NumParams < 1)
    {
        UNLUA_LOGERROR(L, LogUnLua, Log, TEXT("%s: Invalid parameters!"), ANSI_TO_TCHAR(__FUNCTION__));
        return 0;
    }

    UClass *Class = Cast<UClass>(UnLua::GetUObject(L, 1));
    if (!Class)
    {
        UNLUA_LOGERROR(L, LogUnLua, Log, TEXT("%s: Invalid class!"), ANSI_TO_TCHAR(__FUNCTION__));
        return 0;
    }

    UObject* Outer = UnLua::GetUObject(L, 2);
    if (!Outer)
        Outer = GetTransientPackage();

    FName Name = NumParams > 2 ? FName(lua_tostring(L, 3)) : NAME_None;
    //EObjectFlags Flags = NumParams > 3 ? EObjectFlags(lua_tointeger(L, 4)) : RF_NoFlags;

    {
        const char *ModuleName = NumParams > 3 ? lua_tostring(L, 4) : nullptr;
        int32 TableRef = LUA_NOREF;
        if (NumParams > 4 && lua_type(L, 5) == LUA_TTABLE)
        {
            lua_pushvalue(L, 5);
            TableRef = luaL_ref(L, LUA_REGISTRYINDEX);
        }
        FScopedLuaDynamicBinding Binding(L, Class, UTF8_TO_TCHAR(ModuleName), TableRef);
#if ENGINE_MAJOR_VERSION <= 4 && ENGINE_MINOR_VERSION < 26
        UObject* Object = StaticConstructObject_Internal(Class, Outer, Name);
#else
        FStaticConstructObjectParameters ObjParams(Class);
        ObjParams.Outer = Outer;
        ObjParams.Name = Name;
        UObject* Object = StaticConstructObject_Internal(ObjParams);
#endif
        if (Object)
        {
            UnLua::PushUObject(L, Object);
        }
        else
        {
            UNLUA_LOGERROR(L, LogUnLua, Log, TEXT("%s: Failed to new object for class %s!"), ANSI_TO_TCHAR(__FUNCTION__), *Class->GetName());
            return 0;
        }
    }

    return 1;
}

static constexpr luaL_Reg UE_Functions[] = {
    {"LoadObject", UObject_Load},
    {"LoadClass", UClass_Load},
    {"NewObject", Global_NewObject},
    {NULL, NULL}
};

int UnLua::UELib::Open(lua_State* L)
{
    lua_newtable(L);
    lua_pushstring(L, "__index");
    lua_pushcfunction(L, UE_Index);
    lua_rawset(L, -3);

    lua_pushvalue(L, -1);
    lua_setmetatable(L, -2);

    lua_pushvalue(L, -1);
    lua_pushstring(L, REGISTRY_KEY);
    lua_rawset(L, LUA_REGISTRYINDEX);

    luaL_setfuncs(L, UE_Functions, 0);
    lua_setglobal(L, NAMESPACE_NAME);

    // global access for legacy support
    lua_getglobal(L, LUA_GNAME);
    luaL_setfuncs(L, UE_Functions, 0);
    lua_pop(L, 1);

#if WITH_UE4_NAMESPACE == 1
    // 兼容UE4访问
    lua_getglobal(L, NAMESPACE_NAME);
    lua_setglobal(L, "UE4");
#elif WITH_UE4_NAMESPACE == 0
    // 兼容无UE4全局访问
    lua_getglobal(L, LUA_GNAME);
    lua_newtable(L);
    lua_pushstring(L, "__index");
    lua_getglobal(L, NAMESPACE_NAME);
    lua_rawset(L, -3);
    lua_setmetatable(L, -2);
#endif

    return 1;
}

void UnLua::UELib::SetTableForClass(lua_State* L, const char* Name)
{
    lua_getglobal(L, NAMESPACE_NAME);
    lua_pushstring(L, Name);
    lua_pushvalue(L, -3);
    lua_rawset(L, -3);
    lua_pop(L, 1);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/753288.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

cartographer从入门到精通(一):cartographer介绍

一、cartographer重要文档 有关cartographer的资料有2个比较重要的网站&#xff0c;我们的介绍也是基于这两个网站&#xff0c;其中会加入自己的一些理解&#xff0c;后续也有一些对代码的修改&#xff0c;来实现我们想完善的功能。 1-Cartographer 2-Cartographer ROS 第1个…

如何使用飞书快捷指令无感记账,ios版

总结 很多人无法长期坚持记账&#xff0c;主要是每次消费需要打开手机软件&#xff0c;一系列繁琐的操作&#xff0c;导致过程中可能就忘了。 今天给大家带来飞书自动记账。 演示视频 点击查看&#xff1a;https://www.douyin.com/video/7312857946382241063 安装 下载快捷…

【java计算机毕设】网上商城系统java MySQL ssm vue html maven项目设计代码源码+文档PPT

1项目功能 2项目介绍 系统功能&#xff1a; 网上商城系统包括管理员、用户俩种角色。 管理员功能包括个人中心模块用于修改个人信息和密码、管理员管理、基础数据管理、论坛管理、商品管理、浏览记录管理、公告信息管理、用户管理、轮播图信息。 用户功能包括个人中心模块用于…

【Pyspark-驯化】spark中高效保存数据到hive表中:stored as PARQUET

【Pyspark-驯化】spark中高效保存数据到hive表中&#xff1a;stored as PARQUET 本次修炼方法请往下查看 &#x1f308; 欢迎莅临我的个人主页 &#x1f448;这里是我工作、学习、实践 IT领域、真诚分享 踩坑集合&#xff0c;智慧小天地&#xff01; &#x1f387; 免费获取相关…

python数据分析与可视化一

公共部分 # 引入数据分析工具 Pandas import pandas as pd # 引入数据可视化工具 Matplotlib import matplotlib.pyplot as plt # 引入数据可视化工具 Seaborn (基于matplotlib) import seaborn as sns # 解决输出时的列名对齐问题 pd.set_option(display.unicode.east_…

短视频利器 ffmpeg (2)

ffmpeg 官网这样写到 Converting video and audio has never been so easy. 如何轻松简单的使用&#xff1a; 1、下载 官网&#xff1a;http://www.ffmpeg.org 安装参考文档&#xff1a; https://blog.csdn.net/qq_36765018/article/details/139067654 2、安装 # 启用RPM …

华强盛网络变压器外部电路如何接线

图一是 华强盛 Hqst 网络变压器工厂19926430038 华强盛电子导读&#xff1a; 网络变压器的外部电路接线通常依赖于其设计和用途。一般来说&#xff0c;网络变压器有多个端口&#xff0c;每个端口可能用于不同的连接或功能。以下是一些可能的接线方式&#xff1a; 1. **主电源…

自研网关架构设计

网关项目 1. 了解网关网关横向对比为什么自研网关 2. 架构设计技术栈技术要点异步化设计使用缓存缓冲合理使用串行化吞吐量为王合适的工作线程 架构图 1. 了解网关 概念 访问数据、业务逻辑或功能的 “前门”负责处理接受和处理调用过程中的所有任务 类型 RESTful APl 使用…

核方法总结(三)———核主成分(kernel PCA)学习笔记

一、核主成分 1.1 和PCA的区别 PCA &#xff08;主成分分析&#xff09;对应一个线性高斯模型&#xff08;参考书的第二章&#xff09;&#xff0c;其基本假设是数据由一个符合正态分布的隐变量通过一个线性映射得到&#xff0c;因此可很好描述符合高斯分布的数据。然而在很多实…

深入分析 Android BroadcastReceiver (七)

文章目录 深入分析 Android BroadcastReceiver (七)1. 高级应用场景1.1 示例&#xff1a;动态权限请求1.2 示例&#xff1a;应用内通知更新 2. 安全性与性能优化2.1 示例&#xff1a;设置权限防止广播攻击2.2 示例&#xff1a;使用 LocalBroadcastManager2.3 示例&#xff1a;在…

零成本打造精品宣传册

​随着互联网的发展&#xff0c;企业和个人对宣传册的需求日益增长&#xff0c;然而&#xff0c;高质量的宣传册制作往往需要不菲的成本。那么&#xff0c;如何零成本打造精品宣传册呢&#xff1f; 一、明确定位和目标群体 在制作宣传册之前&#xff0c;首先要明确其定位和目标…

关于怎么将wireshark抓包视频流转为视频播放出来

0.安装wireshark 安装PotPlayer 1.将以下两个插件放入 C:\Program Files\Wireshark\plugins 目录中 2.筛选视频流数据包&#xff0c;右键Decode As… 改为RTP 或者 右键->follow&#xff08;追踪流&#xff09;->UDP stream 然后叉掉弹窗 3.选择菜单Edit->Prefe…

职责链让树状分支更严谨更易读更易维护

业务场景 传统方式就不列举了 职责链解决 Chain 类 class Chain {fn: Function;successor: any;constructor(fn: Function) {this.fn fn;this.successor null;}setNextSuccessor(successor: any) {return (this.successor successor);}passRequest() {var ret this.fn.a…

微信公众号扫码授权登录

【微信扫登录】原理说明 1、准备工作&#xff1a;注册开放微信公众号。获得此账号的AppID和AppSecret。 2、发起授权登录&#xff1a;通过授权链接或者扫码授权二维码的方式&#xff0c;获取登录code&#xff0c;通过code获取access_token。 3、成功获取access_token即代表登…

[CTF]-PWN:mips反汇编工具,ida插件retdec的安装

IDA是没有办法直接按F5来反汇编mips的汇编的&#xff0c;而较为复杂的函数直接看汇编不太现实&#xff0c;所以只能借用插件来反汇编 先配置环境&#xff0c;下载python3.4以上的版本&#xff0c;并将其加入到环境变量中 下载retdec 地址&#xff1a;Release v1.0-ida80 ava…

常见Web认证方式对比

认证是一个在用户或者设备在访问一个受限的系统时&#xff0c;鉴定用户凭据的过程&#xff0c;即确认“你是谁”的问题。最常见的认证用户的方式是通过用户名和密码的形式进行校验&#xff0c;目前存在多种校验方式&#xff0c;本文将对其进行一个简单的对比&#xff0c;使得大…

今天起,全球所有Mac用户可免费安装桌面版ChatGPT

在 macOS 上&#xff0c;用户在安装新的 ChatGPT 应用程序后&#xff0c;使用 Option Space 的键盘组合即可快速调用 ChatGPT。 刚刚&#xff0c;OpenAI 宣布推出适用于 macOS 的应用程序。 虽然 Mac 应用程序尚未在 Mac App Store 中提供&#xff0c;但用户可以直接从 Open…

Lean4Game 开发教程 | 数学形式化

引言 Lean 是一门用于形式化证明的编程语言&#xff0c;它允许严格证明数学定理和验证软件代码的正确性。 本篇介绍 Lean 游戏的编写和发布方式。这类游戏不仅利于对 Lean 本身的学习&#xff0c;对学科知识的理解&#xff0c;还能推动数学圈内人对 Lean 的接触学习。 Lean4…

elementUI搭建使用过程

前言 Element&#xff0c;一套为开发者、设计师和产品经理准备的基于 Vue 2.0 的桌面端组 件库 安装 ElementUI 在终端命令行输入 npm i element-ui -S 在 main.js 中写入以下内容&#xff1a; import ElementUI from element-ui ; import element-ui/lib/theme-chalk/i…

0-30 VDC 稳压电源,电流控制 0.002-3 A

怎么运行的 首先&#xff0c;有一个次级绕组额定值为 24 V/3 A 的降压电源变压器&#xff0c;连接在电路输入点的引脚 1 和 2 上。&#xff08;电源输出的质量将直接影响与变压器的质量成正比&#xff09;。变压器次级绕组的交流电压经四个二极管D1-D4组成的电桥整流。桥输出端…