简体   繁体   English

是否可以通过 luajit ffi 使用 c++ 命名空间中的函数?

[英]Is it possible to use functions from c++ namespaces with luajit ffi?

I've got a lot of c++ code which contains a lot of functions and classes in namespaces (boost, for example).我有很多 c++ 代码,其中包含命名空间中的很多函数和类(例如,boost)。
Now I'm trying to embed LuaJiT2 as script engine, but I cannot find anything about calling functions and using other stuff from namespaces.现在我正在尝试将 LuaJiT2 作为脚本引擎嵌入,但我找不到任何关于调用函数和使用命名空间中的其他内容的信息。
So, Is it possible to pass the functions from c++ namespaces to LuaJIT with the FFI?那么,是否可以使用 FFI 将 c++ 命名空间中的函数传递给 LuaJIT?

You may use the standard Lua API to expose namespace-scope functions, as well as class static functions, to Lua. You may use the standard Lua API to expose namespace-scope functions, as well as class static functions, to Lua. This is done exactly as you would with the regular Lua interpreter, since LuaJIT is drop-in compatible with it.这与使用常规 Lua 解释器完全一样,因为 LuaJIT 与其兼容。

But you can't use FFI, because FFI is based on a C-based parsing of the header files.但是您不能使用 FFI,因为 FFI 是基于对 header 文件的基于 C 的解析。 And you're using C++ syntax.而且您正在使用 C++ 语法。 FFI is not the only way to use LuaJIT; FFI 不是使用 LuaJIT 的唯一方式; it's just one that is based on C.它只是基于 C 的一个。

Any of the C++-specific binding APIs that use Lua (Luabind, SWIG, etc) should work just fine with LuaJIT as well.任何使用 Lua(Luabind、SWIG 等)的特定于 C++ 的绑定 API 都应该可以与 LuaJIT 一起正常工作。

It is possible to use different name mangling other than C.可以使用除 C 之外的其他名称修饰。 The reason why its not "common" is because the C++ name mangling is very compiler/platform specific: http://lua-users.org/lists/lua-l/2011-07/msg00502.html The reason why its not "common" is because the C++ name mangling is very compiler/platform specific: http://lua-users.org/lists/lua-l/2011-07/msg00502.html

So this sort of declaration is valid:所以这种声明是有效的:

ffi.cdef[[
void Test1_Method1(void) asm("_ZN5Test17Method1Ev");
]]

And you can then call Test1_Method1.然后您可以调用 Test1_Method1。 Mike Pall has done an amazing job with luajit. Mike Pall 在 luajit 方面做得非常出色。 So many great features.这么多很棒的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM