stevekchiu / lua-intf Goto Github PK
View Code? Open in Web Editor NEWA binding between C++11 and Lua language
License: MIT License
A binding between C++11 and Lua language
License: MIT License
I would like to patch LuaIntf to support user aligned types. At the moment, the alignment of types is ignored as a result of the way placement new works. User aligned types are common in games where SIMD instructions are useful. I worked around this for a while by patching LuaIntf to allocate user aligned types directly via new, and just store their pointers in the user data. However, I think it would probably be better to use something like alignof to dynamically allocate extra memory and offset it so as to avoid the additional memory allocation.
hey Steve,
first of all I would like to say that lua-intf compiles fine without modification, I must have been doing something dumb, this was something I reported 2 weeks ago.
Getting more familiar with it, I was wondering about this issue:
I can do this:
shared_ptr<lc::Layer> getLayer() const {
return shared_ptr<lc::Layer>();
}
but I cannot do this:
shared_ptr<const lc::Layer> getLayer() const {
return shared_ptr<lc::Layer>();
}
I work with a lot of immutable so I would like to have these const correct.
Again, i wish I could propose a patch, but my knowledge on template classes in c++ is rather limited.
Hi,
Subjects states it all :)
Are function argument modifiers possible? This to reduce the number of names to create objects.
Any idea how this may be possible?
It appears addVariable always results in a copy of the variable being created before it is pushed on the stack. This is problematic for class types for two reasons: it results in extra work and memory, and it does not support class types which are missing copy constructors.
I'll take a stab at changing the behavior for class types, but I wanted to call it to your attention first in case I am potentially misreading the code or misusing the API.
When export function with the type "signed char",it'll trigger static_assert(std::is_class::value,
"type is not class, need template specialization");
example:void f(signed char i) {}
typeid(char).name() and typeid(signed char).name() are not the same.
patch:
luaType.h
LUA_USING_VALUE_TYPE(char)
+LUA_USING_VALUE_TYPE_EXT(signed char, lua_Integer)
LUA_USING_VALUE_TYPE_EXT(unsigned char, lua_Integer)
Hi, when I try to compile even this simple code:
namespace LuaIntf
{
LUA_USING_SHARED_PTR_TYPE(std::shared_ptr)
}
class A {
};
std::shared_ptr<A> returnA() {
return std::shared_ptr<A>(new A);
}
std::shared_ptr<const A> returnConstA() {
return std::shared_ptr<const A>(new A);
}
void do_the_bind(lua_State* L) {
LuaBinding(L).beginClass<A>("A")
.endClass();
LuaBinding(L).beginModule("utils")
.addFunction("returnA", &returnA)
.addFunction("returnConstA", &returnConstA)
.endModule();
}
The compiler give me this error:
../../Dependencies/Include/lua-intf/impl/LuaType.h: In instantiation of 'struct LuaIntf::LuaTypeMappingExistsstd::shared_ptr':
../../Dependencies/Include/lua-intf/impl/LuaType.h:68:16: required from 'struct LuaIntf::LuaTypestd::shared_ptr'
../../Dependencies/Include/lua-intf/impl/CppInvoke.h:80:45: required from 'static int LuaIntf::CppInvokeMethod<FN, R, P>::push(lua_State_, const FN&, std::tuple<Args2 ...>&) [with FN = std::shared_ptr ()(); R = std::shared_ptr; P = {}; lua_State = lua_State]'
../../Dependencies/Include/lua-intf/impl/CppBindModule.h:124:96: required from 'static int LuaIntf::CppBindMethodBase<CHK, FN, IARG, R, P>::call(lua_State_) [with int CHK = 0; FN = std::shared_ptr ()(); int IARG = 1; R = std::shared_ptr; P = {}; lua_State = lua_State]'
../../Dependencies/Include/lua-intf/impl/CppBindModule.h:447:61: required from 'LuaIntf::CppBindModule& LuaIntf::CppBindModule::addFunction(const char, const FN&) [with FN = std::shared_ptr (_)()]'
..\Source\Core../Lua/aeLuaIO.hpp:62:42: required from here
../../Dependencies/Include/lua-intf/impl/LuaType.h:62:63: error: invalid use of incomplete type 'struct LuaIntf::LuaTypeMappingstd::shared_ptr<A, void>'
: decltype(LuaTypeExists(std::declval<LuaTypeMapping>())) {};
^
../../Dependencies/Include/lua-intf/impl/LuaType.h:42:8: error: declaration of 'struct LuaIntf::LuaTypeMappingstd::shared_ptr<A, void>'
struct LuaTypeMapping;
^
../../Dependencies/Include/lua-intf/impl/LuaType.h: In instantiation of 'struct LuaIntf::LuaTypeMappingExists<std::shared_ptr >':
../../Dependencies/Include/lua-intf/impl/LuaType.h:68:16: required from 'struct LuaIntf::LuaType<std::shared_ptr >'
../../Dependencies/Include/lua-intf/impl/CppInvoke.h:80:45: required from 'static int LuaIntf::CppInvokeMethod<FN, R, P>::push(lua_State_, const FN&, std::tuple<Args2 ...>&) [with FN = std::shared_ptr ()(); R = std::shared_ptr; P = {}; lua_State = lua_State]'
../../Dependencies/Include/lua-intf/impl/CppBindModule.h:124:96: required from 'static int LuaIntf::CppBindMethodBase<CHK, FN, IARG, R, P>::call(lua_State_) [with int CHK = 0; FN = std::shared_ptr ()(); int IARG = 1; R = std::shared_ptr; P = {}; lua_State = lua_State]'
../../Dependencies/Include/lua-intf/impl/CppBindModule.h:447:61: required from 'LuaIntf::CppBindModule& LuaIntf::CppBindModule::addFunction(const char, const FN&) [with FN = std::shared_ptr (_)()]'
..\Source\Core../Lua/aeLuaIO.hpp:63:52: required from here
../../Dependencies/Include/lua-intf/impl/LuaType.h:62:63: error: invalid use of incomplete type 'struct LuaIntf::LuaTypeMapping<std::shared_ptr, void>'
: decltype(LuaTypeExists(std::declval<LuaTypeMapping>())) {};
^
../../Dependencies/Include/lua-intf/impl/LuaType.h:42:8: error: declaration of 'struct LuaIntf::LuaTypeMapping<std::shared_ptr, void>'
struct LuaTypeMapping;
I'm using TDM-GCC 4.9.2 64 bit under Windows 7.
Thank you for help and for the amazing work you are doing!
Hi
and thanks for a great library!
What is the easiest way to pass a pointer to an object from c++ to lua? The low level method seems to be by using lua_pushlightuserdata() but I'm guessing lua-intf has a nicer solution? Could you give a practical example?
Thanks,
Mikko
I know the "C++11" means that this project do not support Visual Studio.
But Visual Studio "14" (current CTP4) is much better now.
It's only need a little change to work with VS14 .
Will you accept It?^_^
I'll post the linux/gcc,mac/clang compatibility of this chane later. :)
patch:
CppArg.h
-typedef typename Traits::Type Type;
-typedef typename Traits::ValueType ValueType;
-typedef typename Traits::HolderType HolderType;
+typedef Traits::Type Type;
+typedef Traits::ValueType ValueType;
+typedef Traits::HolderType HolderType;
Hi,
First of all, very great library and lot of neat features, i would like to use into my game engine which has scene graph with component based system, i would like to know how would i be able to has scripting components using lua and attach to node, would be possible to extend c++ class and call lua function for methods, ie => initialize, update, draw etc.
any advice?
I found something similar with LuaBridge (http://www.executionunit.com/blog/2014/03/09/integrating-lua-and-my-c-game-engine/)
Thanks
Hey Steve,
Any plans to support const variables? For example, I would like to export const globals with addVariable if possible, but I'm also happy writing a addProperty + lambda. However, I'm less clear how I would expose a const field on a class, since I haven't checked if you have access to the instance in the lambda.
Thanks again,
Kris
LuaIntf::LuaContext lua_context;
lua_context.doString("my_variable=25");
LuaIntf::LuaRef globals(lua_context.state(), "_G");
for(auto& e : globals) {
std::string key = e.keystd::string();
}
I get a core dump on the for() line. It dumps in LuaTableIterator::next()
I"m using Lua 5.2.3 on Windows 7 and a slightly older (six months?) version of lua-intf that I hacked a little to work with VS2013. It's entirely possible that I broke lua-intf, but I have been using it successfully.
Thanks
Hi,
I would like to bind to a class method that accepts std function as parameter, how can I achieve this?
LuaGlue has support for this, https://github.com/Tomasu/LuaGlue
https://github.com/Tomasu/LuaGlue/blob/master/include/LuaGlue/StackTemplates/StdFunction.h
Thanks
I have my script:
MyComponent = class()
function MyComponent:initialize(gameObject)
self.parent = gameObject
end
Then from code
LuaRef typeTable(_lua, "MyComponent");
if (!typeTable.isTable())
{
ALIMER_ERROR("Could not find lua table %s", "MyComponent");
}
auto factory = Lua::eval<LuaRef>(_lua, "MyComponent");
SceneNode* parent = GetParent();
LuaRef instance = factory.call<LuaRef>(parent ); // This cannot be done
This can be done in luabridge.
I'm stuck with a problem with CppBindClass::setConstant but after several hours I haven't solved it yet.
CPP:
.beginModule("myModule")
.addConstant("myConst", 10)
.endModule()
.beginClass<myClass>("myClass")
.addConstant("myConst", 10)
.endClass()
Lua:
print(myModule.myConst) -- prints 10 correctly
print(myClass.myConst) -- Problem here: prints 'nil' value
print(myClass.___getters.myConst) -- prints 10 correctly
TDM GCC 4.9.2 64 bit under WIndows 7 with LuaJIT 2.0.3
Unfortunately it seems that I'm unable to solve this myself... any advice?
Thank you again very much
Hi,
Is possible to compile this using CLang + Visual Studio 2013?
http://sourceforge.net/projects/clangonwin/files/MsvcBuild/3.7/
EDIT: is Possible, but is needed add some options, read this!
http://lists.cs.uiuc.edu/pipermail/llvmdev/2014-August/075373.html
Hi Steve,
Was just wondering if there was at some point a VS2013 compatible version of this library? The current version makes use of VS2014+ features and it breaks my build.
Thanks
-Matt
I know this is possible to be done from the Lua side, but is there any straightforward way to do this from LuaIntf? If not, do you have any suggestions?
Compiling with GCC 4.9.1, I get loads of errors because CppBindClass.h needs the memory header, but if it is directly included in the offending file it seems to be setting the included objects/methods in the LuaIntf namespace.
(It works if the memory header is included in the LuaIntf.h file, but that's a bit ugly.)
Modification of variables/calls to functions get routed to the C++ object, but when the C++ object is destroyed (e.g. falling out of scope) the Lua variable remains valid and the variables are still valid.
Hi,
Some code has ref pointer support therefore addRef and release method are used for reference tracing, in this case class destructor is private.
Hello SteveKChiu,
I really enjoy Your work and I have got a small enhancement.
It would be nice if You add cast mentioned in title, like this: operator lua_State* () const { return L; }
in LuaContext class. Doing it this way integrates LuaContext with for example LuaRef.
Keep it up ๐
Hey Steve,
This was the other issue that I mentioned. Since merging your tweaks to my original CppArgHolder change, I can no longer compile code uses addFunction to expose a method with non-const reference arguments.
Here is an example of some code that now fails to compile:
.addFunction("addRenderable", &wv::BaseSceneRenderer::addRenderable, LUA_ARGS(wv::Renderable &))
The error message I get is:
Binding of reference to type 'wv::Renderable' to a value of type 'const wv::Renderable' drops qualifiers.
It's not clear to me where exactly the code is trying to cast to a non-const type since CppArgOutput<traits, false>
takes a const typename Traits::ValueType& v
. It's also not clear to me what you changed that could have possibly broke this.
In any case, I created this change set to fix (workaround?) the issue:
krisr@1dd8683
Curious if you have any thoughts and have experienced the same issue?
Thanks,
Kris
If a class override the operator new(this is normal when using some kind of memory pool),when export the Constructor, it'll triggeran error:
'operator new' : function does not take 2 arguments
To be sure to use the global allocation function,the placement new should explicit use ::new.
patch:
CppInvoke.h
static T* call(void* mem, TUPLE& args)
{
-return new (mem) T(std::get(args).value()...);
+return ::new (mem) T(std::get(args).value()...);
}
Most of developers out uses VS 2013 or even VS 2012, lua-intf is great library but I cannot use it as it does not have
VS 2013 support, some stuff can be tweaked using the _MSC_VER definition.
Thanks
I have idea.
Can you add threads to Lua?
1 file - 1 thread or something other with multithread support..
This is only proposal. ;)
use std::bind error with the auto return type on VS14 or Clang.
gcc not tested.
bool f(int i, std::string file) {return true;}
int ii=1;
....
std::function<bool (std::string)> f1 = std::bind(f, ii);
.addFunction("f1", f1) //ok
.addFunction("f11", std::bind(f, ii)) //error!!
.addFunction("f2", [ii](std::string s) {
return f(ii,s);
}) //ok
error occured here: decltype(&FN::operator())
I don't know how to fix it.
Hey Steve,
I'm using LUA_USING_SHARED_PTR_TYPE to expose my own shared ptr class, however, I was surprised to find that the default SharedPtr wrapper type dereferences the shared_ptr on construction. I can see why this wouldn't be a big deal for standard shared ptrs, but it doesn't play well with custom shared ptrs that do work to dereference the ptr for the first time.
For example, there is a common pattern in Game engines to define a SharedPtr like interface for Resource Handles. The Resource Handle interface will typically only load the resource when it is first dereferenced. By dereferencing the handle early you may be forcing resources to be loaded that are never actually used. Furthermore, in my case, resources can be reloaded, and their pointers change in the process so the current LuaIntf SharedPtr impl is a no go - though I'm not opposed to trying to change my own code to reuse the same pointer if possible.
I can see three ways of fixing this:
My preference is to do 1 or 2, but only if you agree. Otherwise, it makes more sense to do 3 so I don't have to maintain my own fork ;).
Thanks,
Kris
Is it possible to create a class in Lua that inherits a native class that has been bound to Lua via Lua-intf?
If it's possible, it would be helpful if you could provide a small Lua module (e.g. luaintf.lua
) along with the library which people could use to create classes derived from the bound classes in Lua. Something like this (inspired by middleclass):
local luaintf = require('luaintf') -- The module
local DerivedClass = class('DerivedClass', BaseClass) -- BaseClass is native C++ class bound via lua-intf
-- Implement the constructor
function DerivedClass:construct(foo)
BaseClass.construct(self, foo)
-- Do stuff
end
-- Add methods
function DerivedClass:hello()
print("Hello, world!")
end
-- Override methods
function DerivedClass:foo(bar)
BaseClass.foo(self, bar)
-- Do stuff
end
return DerivedClass
local DerivedClass = require('DerivedClass')
local test = DerivedClass("blah")
test:hello() -- Prints "Hello, world"
test:foo()
Sample classes:
class Base {};
class Derived : public Base {
public:
void foo();
};
// Their binding code
LuaIntf::LuaBinding(L).beginClass<Base>("Base")
.endClass();
LuaIntf::LuaBinding(L).beginExtendClass<Derived, Base>("Derived")
.addFunction("foo", &Derived::foo)
.endClass();
Let's say I have an object of class Derived
, but the pointer that keeps track of it is of type Base
:
Derived *a = new Derived();
// ...
Base *b = a;
I notice that if I push b
to lua, I will not be able to call foo
inside the script:
Lua::setGlobal(L, "b", b);
b:foo() -- This fails
Is it possible to get it to work? (I think it's duck typing)
Thank you very much. :)
I'm using the "os" Lua table, it has functions like "getenv". I want to add a "setenv" function to it but I'm having trouble getting the syntax correct.
I tried
LuaBinding(lua_context.state()).beginModule("os")
.addFunction("setenv", &lua_setenv)
.endModule();
which results in error: bad argument #-1 (table expected, got nil)
I poked around in LuaRef.h, guessing that there would be something there along the lines of the example
LuaRef(L, "os").addFunction("setenv", &lua_setenv);
but I didn't see anything like that- there were some createFunction methods, but those didn't seem to be the right thing.
I did try just qualifying the function name, like
LuaIntf::LuaBinding(lua_context.state()).addFunction("os.setenv", &lua_setenv);
but that results in
[string "..."]:7: attempt to call field 'setenv' (a nil value)
when I try to use it in a lua script
Any assistance would be appreciated.
Thanks for a really cool framework!
It appears that lua-intf attempts to copy reference argument types which results in compilation errors when passing types that do not have copy constructors, and inefficiencies otherwise:
template <typename Traits, bool IsDefault>
struct CppArgTraitsInput <Traits, true, false, IsDefault>
{
static int get(lua_State* L, int index, typename Traits::ValueType& r)
{
r = LuaType<typename Traits::Type>::get(L, index);
return 1;
}
};
I'm going to dig into this further and see how difficult it would be to handle reference types without copying them. Let me know if you have any suggestions!
I cannot compile your library using visual studio 2013 as it has no support for constexpr, could you help me out?
http://msdn.microsoft.com/en-us/library/vstudio/hh567368.aspx
UPDATE:
Actually, this may have just been a compiler bug (apple llvm). I cannot reproduce it after moving back to the original code. It could just be that the compiler had cached a bad version and was not rebuilding it properly. It's not the first time I've seen xcode do that. I'll let you know if I see it again.
Hey Steve,
After upgrading to the latest version, I'm having a couple issues. The most serious of which is that one of my functions is returning the wrong value when I explicitly state the parameters via LUA_ARGS() macro.
For example, when exposing this function:
Pass& Material::createPass(const ShaderProgramHandle& shader_program)
This correctly returns a Lua object with __type=class<wv::Pass>
.addFunction(
"createPass", &wv::Material::createPass)
But, this incorrectly returns a Lua object with __type=class<wv::ShaderProgram>
.addFunction(
"createPass", &wv::Material::createPass, LUA_ARGS(const ShaderProgramHandle& shader))
Even stranger, wv::ShaderProgram is the type contained in the "Smart Pointer" that is wv::ShaderProgramHandle.
I can work around this by not using LUA_ARGS in functions where they are unnecessary, but I still need to add them in other places like the constructors. Perhaps the argument is being misinterpreted as a output argument due to some of your recent changes?
The other issue I am having may be related. After your updates to my introduction of PasserType (which is now named HolderType), I can no longer pass non-const reference types around due to a type error. I had to work around this by modifying HolderType such that value is always const qualified and the actual held value is mutable. I was hoping to find a better way but didn't have time yet.
Kris
I have a class
enum HorizontalAlignment
{
HorizontalAlignment_Left,
HorizontalAlignment_Center,
HorizontalAlignment_Right,
HorizontalAlignment_Stretch
};
HorizontalAlignment GetHorizontalAlignment() const { return _horizontalAlignment; }
void SetHorizontalAlignment(HorizontalAlignment value) { _horizontalAlignment = value; }
When I do
.addProperty("horizontalAlignment",
&UIElement::GetHorizontalAlignment,
&UIElement::SetHorizontalAlignment)
Does not work.
CMakeFiles/TiC.dir/src/main.cpp.o: In function `LuaState::newState()':
/home/theaifam5/Documents/CLion Projects/TiC/src/ScriptEngine/Lua/LuaState.h: undefined reference to `luaL_newstate()'
CMakeFiles/TiC.dir/src/main.cpp.o: In function `LuaState::version() const':
/home/theaifam5/Documents/CLion Projects/TiC/src/ScriptEngine/Lua/LuaState.h: undefined reference to `lua_version(lua_State*)'
CMakeFiles/TiC.dir/src/main.cpp.o: In function `LuaState::openLibs() const':
/home/theaifam5/Documents/CLion Projects/TiC/src/ScriptEngine/Lua/LuaState.h: undefined reference to `luaL_openlibs(lua_State*)'
if i use a extern, work normally.
I have arch linux and Lua 5.2.
problem with precompiled Lua from repository?
Hello again,
I'm getting a compiler error when registering function with return type std::shared_ptr<const Class>
. Here's the test code:
#include "../lua-intf/LuaIntf/LuaIntf.h"
#include <cstdio>
#include <memory>
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
using namespace LuaIntf;
namespace LuaIntf
{
LUA_USING_SHARED_PTR_TYPE(std::shared_ptr)
}
//------
class A {
public:
};
//------
std::shared_ptr<A> returnA() {
return std::shared_ptr<A>(new A);
}
std::shared_ptr<const A> returnConstA() {
return std::shared_ptr<const A>(new A);
}
//------
int main(int argc, char **argv) {
LuaState L = LuaState::newState();
LuaBinding(L).beginClass<A>("A")
.endClass();
LuaBinding(L).beginModule("utils")
.addFunction("returnA", &returnA)
.addFunction("returnConstA", &returnConstA)
.endModule();
printf("Hello world!\n");
return 0;
}
Here's the compiler error which I received:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -std=c++11 -I../lua-5.2.3/src -L../lua-5.2.3/src -llua -o test1 test1.cpp
In file included from test1.cpp:1:
In file included from ./../lua-intf/LuaIntf/LuaIntf.h:41:
./../lua-intf/LuaIntf/impl/CppObject.h:284:11: error: no matching constructor for initialization of 'LuaIntf::CppObject'
: CppObject(&*sp)
^ ~~~~
./../lua-intf/LuaIntf/impl/CppObject.h:319:19: note: in instantiation of member function 'LuaIntf::CppObjectSharedPtr<std::__1::shared_ptr<const A>,
A>::CppObjectSharedPtr' requested here
new (mem) CppObjectSharedPtr<SP, T>(sp);
^
./../lua-intf/LuaIntf/impl/CppObject.h:422:40: note: in instantiation of member function 'LuaIntf::CppObjectSharedPtr<std::__1::shared_ptr<const A>, A>::pushToStack'
requested here
CppObjectSharedPtr<SP, T>::pushToStack(L, sp, is_const);
^
./../lua-intf/LuaIntf/impl/CppObject.h:454:62: note: in instantiation of member function 'LuaIntf::LuaCppObjectFactory<std::__1::shared_ptr<const A>, A, true,
true>::push' requested here
LuaCppObjectFactory<T, ObjectType, IsShared, IsRef>::push(L, t, IsConst);
^
./../lua-intf/LuaIntf/impl/CppInvoke.h:62:21: note: in instantiation of member function 'LuaIntf::LuaCppObject<std::__1::shared_ptr<const A>, false, false>::push'
requested here
LuaType<R>::push(L, call(func, args));
^
./../lua-intf/LuaIntf/impl/CppBindModule.h:102:79: note: in instantiation of member function 'LuaIntf::CppInvokeMethod<std::__1::shared_ptr<const A> (*)(),
std::__1::shared_ptr<const A>>::push' requested here
int n = CppInvokeMethod<FN, R, typename CppArg<P>::ValueType...>::push(L, fn, args);
^
./../lua-intf/LuaIntf/impl/CppBindModule.h:333:71: note: in instantiation of member function 'LuaIntf::CppBindMethodBase<0, std::__1::shared_ptr<const A> (*)(), 1,
std::__1::shared_ptr<const A>>::call' requested here
m_meta.rawset(name, LuaRef::createFunction(state(), &CppProc::call, CppProc::function(proc)));
^
test1.cpp:44:4: note: in instantiation of function template specialization 'LuaIntf::CppBindModule::addFunction<std::__1::shared_ptr<const A> (*)()>' requested here
.addFunction("returnConstA", &returnConstA)
^
./../lua-intf/LuaIntf/impl/CppObject.h:117:14: note: candidate constructor not viable: no known conversion from 'const A *' to 'void *' for 1st argument
explicit CppObject(void* ptr)
^
./../lua-intf/LuaIntf/impl/CppObject.h:142:5: note: candidate constructor not viable: no known conversion from 'const A *' to 'const LuaIntf::CppObject' for 1st
argument
CppObject(const CppObject&) = delete;
^
1 error generated.
My compiler is clang++:
$ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ --version
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
Hello Steve, I was testing today your library. I like it, but what I don't understand is, is it possible to bind C++ classes with empty default constructors ?
I tried doing this:
LuaIntf::LuaBinding(L).beginClass<Block>("Block")
.addConstructor(LUA_ARGS(void))
.addConstructor(LUA_ARGS(std::string))
.addFunction("printName", &Block::printName)
.endClass();
Where I have this very simple class definition for testing:
class Block {
public:
Block() {
printf("DEFAULT CONSTRUCTOR!\n");
_name = "Jantteri";
};
Block(std::string name) : _name(name) {};
~Block() = default;
void printName() {
printf("BLOCK HERE, My name is %s\n", _name.c_str());
printf("Check it yo! Before you wreck it yo!\n");
}
private:
std::string _name;
};
And I call this in the Lua side:
function callBlokk()
print("CALLBLOKK");
b = Block("PETTERI");
b:printName();
c = Block();
c:printName();
end
When the object 'c' gets constructed, the default constructor is not called for some reason. Does it set the parameter to "" ? How should I define or bind this ? Can't see any empty constructors in the documentation. It doesn't even create the 'c' object. 'b' works perfectly fine.
Also, what about using default values in the constructor ? Doesn't seem to work. Like this:
Block(std::string name = "BLOCK") : _name(name) {};
This just gives this template error:
clang++ -Os -Wall -stdlib=libc++ -std=c++11 -I/usr/local/include -I/opt/local/include -c -o testgame.o testgame.cpp
In file included from testgame.cpp:11:
In file included from ./LuaIntf/LuaIntf.h:40:
./LuaIntf/impl/CppInvoke.h:172:28: error: call to constructor of 'Block' is ambiguous
return ::new (mem) T(std::get<INDEX>(args).value()...);
^
./LuaIntf/impl/CppInvoke.h:186:80: note: in instantiation of member function
'LuaIntf::CppDispatchClassConstructor<Block, std::__1::tuple<>, 0>::call' requested here
return CppDispatchClassConstructor<T, std::tuple<P...>, sizeof...(P)>::call(mem, args);
^
./LuaIntf/impl/CppObject.h:181:45: note: in instantiation of member function
'LuaIntf::CppInvokeClassConstructor<Block>::call' requested here
CppInvokeClassConstructor<T, P...>::call(v->objectPtr(), args);
^
./LuaIntf/impl/CppBindClass.h:41:32: note: in instantiation of function template specialization
'LuaIntf::CppObjectValue<Block>::pushToStack<>' requested here
CppObjectValue<T>::pushToStack(L, args, false);
^
./LuaIntf/impl/CppBindClass.h:647:71: note: in instantiation of member function 'LuaIntf::CppBindClassConstructor<Block,
Block, LuaIntf::_arg (*)()>::call' requested here
m_meta.rawset("__call", &CppBindClassConstructor<T, T, ARGS>::call);
^
testgame.cpp:256:4: note: in instantiation of function template specialization 'LuaIntf::CppBindClass<Block,
LuaIntf::CppBindModule>::addConstructor<LuaIntf::_arg (*)()>' requested here
.addConstructor(LUA_ARGS(void))
^
testgame.cpp:133:3: note: candidate constructor
Block(std::string name = "BLOCK") : _name(name) {};
^
testgame.cpp:134:3: note: candidate constructor
Block() {
^
1 error generated.
Can you help me ? Bit of a Lua newbie here! Thank you!
I have a bound function:
.addStaticFunction("createPerspective",
&Camera::CreatePerspective,
LUA_ARGS(_def<float, 3.14f>, _def<float, 1.33333333333333f>, _def<float, 0.2f>, _def<float, 100000.0f>))
.endClass().
What I get is:
Error 1 error C2993: 'float' : illegal type for non-type template parameter 'DEF'.
Hey Steve,
This library is coming along really nicely. I especially like the clean use of C++ 11 features. I was curious if you have any plans to support operators and/or function overloads?
Kris
Hi,
I would like to bind to instance function with wrapper method:
void MyWrapper_Method(ClassInstance* instance, int test)
{
}
.addWrapperFunction("test", &MyWrapper_Method)
I have done this into CppBindClass.h
template <typename FN>
CppBindClass<T>& addWrapperFunction(const char* name, const FN& proc)
{
static_assert(!std::is_function<FN>::value,
"function pointer is needed, please prepend & to function name");
using CppProc = CppBindMethod<FN>;
setMemberFunction(name, LuaRef::createFunction(state(), &CppProc::call, CppProc::function(proc)), true);
return *this;
}
Thanks
||=== Build: Debug CLANG x86_64 in login-server_sql (compiler: LLVM Clang Compiler) ===|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|92|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|98|error: unknown type name 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|98|error: use of undeclared identifier 'lua_pushunsigned'; did you mean 'lua_pushinteger'?|
../src/3rdparty/LuaJIT-2.0.3/src/lua.h|162|note: 'lua_pushinteger' declared here|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|103|error: use of undeclared identifier 'luaL_checkunsigned'; did you mean 'luaL_checkinteger'?|
../src/3rdparty/LuaJIT-2.0.3/src/lauxlib.h|44|note: 'luaL_checkinteger' declared here|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|108|error: unknown type name 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|108|error: use of undeclared identifier 'luaL_optunsigned'; did you mean 'luaL_optinteger'?|
../src/3rdparty/LuaJIT-2.0.3/src/lauxlib.h|45|note: 'luaL_optinteger' declared here|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|92|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|98|error: unknown type name 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|98|error: use of undeclared identifier 'lua_pushunsigned'; did you mean 'lua_pushinteger'?|
../src/3rdparty/LuaJIT-2.0.3/src/lua.h|162|note: 'lua_pushinteger' declared here|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|103|error: use of undeclared identifier 'luaL_checkunsigned'; did you mean 'luaL_checkinteger'?|
../src/3rdparty/LuaJIT-2.0.3/src/lauxlib.h|44|note: 'luaL_checkinteger' declared here|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|108|error: unknown type name 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|108|error: use of undeclared identifier 'luaL_optunsigned'; did you mean 'luaL_optinteger'?|
../src/3rdparty/LuaJIT-2.0.3/src/lauxlib.h|45|note: 'luaL_optinteger' declared here|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|360|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|347|note: expanded from macro 'LUA_USING_VALUE_TYPE_EXT'|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|360|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|348|note: expanded from macro 'LUA_USING_VALUE_TYPE_EXT'|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|360|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|349|note: expanded from macro 'LUA_USING_VALUE_TYPE_EXT'|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|362|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|347|note: expanded from macro 'LUA_USING_VALUE_TYPE_EXT'|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|362|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|348|note: expanded from macro 'LUA_USING_VALUE_TYPE_EXT'|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|362|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|349|note: expanded from macro 'LUA_USING_VALUE_TYPE_EXT'|
../src/3rdparty/lua-intf/LuaIntf/LuaState.h|190|error: use of undeclared identifier 'luaL_len'; did you mean 'luaL_ref'?|
../src/3rdparty/LuaJIT-2.0.3/src/lauxlib.h|61|note: 'luaL_ref' declared here|
../src/3rdparty/lua-intf/LuaIntf/LuaState.h|471|error: unknown type name 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/LuaState.h|474|error: unknown type name 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/LuaState.h|477|error: unknown type name 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/LuaState.h|477|error: unknown type name 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/LuaState.h|541|error: unknown type name 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/LuaState.h|360|error: use of undeclared identifier 'lua_version'|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|360|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|347|note: expanded from macro 'LUA_USING_VALUE_TYPE_EXT'|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|360|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|348|note: expanded from macro 'LUA_USING_VALUE_TYPE_EXT'|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|360|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|349|note: expanded from macro 'LUA_USING_VALUE_TYPE_EXT'|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|362|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|347|note: expanded from macro 'LUA_USING_VALUE_TYPE_EXT'|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|362|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|348|note: expanded from macro 'LUA_USING_VALUE_TYPE_EXT'|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|362|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|349|note: expanded from macro 'LUA_USING_VALUE_TYPE_EXT'|
../src/3rdparty/lua-intf/LuaIntf/LuaState.h|190|error: use of undeclared identifier 'luaL_len'; did you mean 'luaL_ref'?|
../src/3rdparty/LuaJIT-2.0.3/src/lauxlib.h|61|note: 'luaL_ref' declared here|
../src/3rdparty/lua-intf/LuaIntf/LuaState.h|471|error: unknown type name 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/LuaState.h|474|error: unknown type name 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/LuaState.h|477|error: unknown type name 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/LuaState.h|477|error: unknown type name 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/LuaState.h|541|error: unknown type name 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/LuaState.h|360|error: use of undeclared identifier 'lua_version'|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|92|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|98|error: unknown type name 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|98|error: use of undeclared identifier 'lua_pushunsigned'; did you mean 'lua_pushinteger'?|
../src/3rdparty/LuaJIT-2.0.3/src/lua.h|162|note: 'lua_pushinteger' declared here|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|103|error: use of undeclared identifier 'luaL_checkunsigned'; did you mean 'luaL_checkinteger'?|
../src/3rdparty/LuaJIT-2.0.3/src/lauxlib.h|44|note: 'luaL_checkinteger' declared here|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|108|error: unknown type name 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|108|error: use of undeclared identifier 'luaL_optunsigned'; did you mean 'luaL_optinteger'?|
../src/3rdparty/LuaJIT-2.0.3/src/lauxlib.h|45|note: 'luaL_optinteger' declared here|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|360|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|347|note: expanded from macro 'LUA_USING_VALUE_TYPE_EXT'|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|360|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|348|note: expanded from macro 'LUA_USING_VALUE_TYPE_EXT'|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|360|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|349|note: expanded from macro 'LUA_USING_VALUE_TYPE_EXT'|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|362|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|347|note: expanded from macro 'LUA_USING_VALUE_TYPE_EXT'|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|362|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|348|note: expanded from macro 'LUA_USING_VALUE_TYPE_EXT'|
../src/3rdparty/lua-intf/LuaIntf/impl/LuaType.h|362|error: use of undeclared identifier 'lua_Unsigned'; did you mean 'unsigned'?|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build finished: 50 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
I discovered this issue a few weeks ago. I think it should be mentioned here. I'm using lua-intf for an iOS application (it should be the same for OSX too), and initially the project failed to compile due to a parse issue:
.../lua-intf/LuaIntf/impl/LuaType.h:30:5: error: expected '}'
Nil = LUA_TNIL,
^
.
.
.
.../usr/include/objc/objc.h:80:16: note: expanded from macro 'Nil'
# define Nil nullptr
.
.
.
.../lua-intf/LuaIntf/impl/LuaType.h:28:1: note: to match this '{'
{
^
This is because there is a macro Nil
defined in usr/include/objc/objc.h:
#ifndef Nil
# if __has_feature(cxx_nullptr)
# define Nil nullptr
# else
# define Nil __DARWIN_NULL
# endif
#endif
I fixed it by renaming LuaTypeID::Nil
to LuaTypeID::Null
.
Allow constant binding like (https://github.com/Tomasu/LuaGlue).
addConstants( { { "ONE", 1 }, { "TWO", 2.0 }, { "THREE", "three" } } )
or
addConstant("ONE", 1)
Hey,
I am getting the below compile error.
What could be the cause of it?
I did add the lib as headers only, but also tried compiling as cpp.
Ries
In file included from ./lua-intf/LuaIntf.h:27: In file included from ./lua-intf/LuaIntf/LuaIntf.h:32: In file included from ./lua-intf/LuaIntf/LuaContext.h:32: ./lua-intf/LuaIntf/LuaRef.h:1098:36: warning: unused parameter 'L' [-Wunused-parameter] static void pushArg(lua_State* L) ^ In file included from cad/lualibrecadbridge.cpp:6: In file included from ./lua-intf/LuaIntf.h:27: In file included from ./lua-intf/LuaIntf/LuaIntf.h:40: .. .. ./lua-intf/LuaIntf/impl/CppInvoke.h:236:32: warning: unused parameter 'L' [-Wunused-parameter] static int push(lua_State* L, T* t, const FN& func, std::tuple& args) ^ cad/lualibrecadbridge.cpp:26:25: error: type name requires a specifier or qualifier .addConstructor(LUA_ARGS(_opt)) ^ ./lua-intf/LuaIntf/impl/CppArg.h:49:35: note: expanded from macro 'LUA_ARGS' #define LUA_ARGS(...) static_cast(nullptr) ^ ./lua-intf/LuaIntf/impl/CppArg.h:48:28: note: expanded from macro 'LUA_ARGS_TYPE' #define LUA_ARGS_TYPE(...) _arg(*)(__VA_ARGS__) ^ cad/lualibrecadbridge.cpp:26:25: error: expected '>' ./lua-intf/LuaIntf/impl/CppArg.h:49:35: note: expanded from macro 'LUA_ARGS' #define LUA_ARGS(...) static_cast(nullptr) ^ ./lua-intf/LuaIntf/impl/CppArg.h:48:28: note: expanded from macro 'LUA_ARGS_TYPE' #define LUA_ARGS_TYPE(...) _arg(*)(__VA_ARGS__) ^ cad/lualibrecadbridge.cpp:26:25: note: to match this '<' ./lua-intf/LuaIntf/impl/CppArg.h:49:34: note: expanded from macro 'LUA_ARGS' #define LUA_ARGS(...) static_cast(nullptr) ^ 7 warnings and 2 errors generated. make: *** [generated/obj/lualibrecadbridge.o] Error 1
Hi,
I have problem, how to pass array (from LUA) to C++ function?
And how to get array size and items?
LUA:
srututuArray({0xDA8208, 0x13A8, 0x4, 0x0018})
Lua compiled with LUA_USE_APICHECK.
Code belows:
LuaIntf::LuaState state = LuaIntf::LuaState::newState();
state.doString("AA = {1,2,3, sub= function () return 0 end}"));
LuaIntf::LuaRef tableAA(script::g_engine->state,"AA");
while (true)
{
LuaIntf::LuaRef sub = tableAA["sub"]; // Crashes in just serveral loop iterations
//LuaIntf::LuaRef sub = tableAA.get<LuaIntf::LuaRef>("sub"); // Works well
}
causes overflow of lua stack.
1.When need to know whether a LuaRef instance is have meaningful data,we should use
if(inst.isValid() && inst.type() != LuaTypeID::NIL) xxx(inst);
maybe
if(inst) xxx(inst);
is more useful.
code change:
class LuaRef
{
...
operator bool() const {
return isValid() && type() != LuaTypeID::NIL;
}
...
}
2.When pass a LuaRef instance using default constructor as param,the function call will crash.
LuaRef fun(L,"test");
LuaRef a;
fun(a); //crash because there's no lua_State* in a
template <>
struct LuaValueType {
...
static void push(lua_State* L, const LuaRef& r)
{
if (r) //!!need operator bool()
r.pushToStack();
else
Lua::push(L, nullptr);
}
...
}
This will also fix the return of default constructor.
LuaIntf::LuaRef TTT() {
return LuaIntf::LuaRef();
}
Export this function to lua,and call it in lua will cause crash.
A constructor to create a nil will also helps.sothing like
LuaRef inst(L); //this return a nil
But considered using LuaRef as a member of Class
class A{
public:
LuaRef a;
}
but we do not want to pass luaState* in A's constructor(or the luaState* are not available until some function called),we just want "a" to keep nil.
The solution as the code above is better.
The usage is from my project use luabind before. luabind::object() means nil,do not need lua_State*.
If there's something already considered in LuaInf and already have a normal way to achieve my goal,please let me know,thanks.
In file LuaCompat.h
extern "C"
{
}
this should be
extern "C"
..
maybe your test project include lua.hpp before include LuaInf.h
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.