本文整理汇总了C++中kaguya::State类的典型用法代码示例。如果您正苦于以下问题:C++ State类的具体用法?C++ State怎么用?C++ State使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了State类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: KAGUYA_TEST_FUNCTION_DEF
KAGUYA_TEST_FUNCTION_DEF(compare_null_ptr)(kaguya::State& state)
{
{
kaguya::LuaRef nullref = state.newRef(nullptr);
TEST_CHECK(nullref.typeTest<std::nullptr_t>());
TEST_CHECK(nullref.weakTypeTest<std::nullptr_t>());
TEST_CHECK(nullref == nullptr);
void* ptr = nullref.get<std::nullptr_t>();
TEST_CHECK(!ptr);
}
{
kaguya::LuaRef ref = state.newRef(1);
TEST_CHECK(!(ref.typeTest<std::nullptr_t>()));
TEST_CHECK(!(ref.weakTypeTest<std::nullptr_t>()));
TEST_CHECK(ref != nullptr);
bool catch_except = false;
try
{
ref.get<std::nullptr_t>();
}
catch (const kaguya::LuaTypeMismatch&)
{
catch_except = true;
}
TEST_CHECK(catch_except);
}
}
示例2: KAGUYA_TEST_FUNCTION_DEF
KAGUYA_TEST_FUNCTION_DEF(load_string)(kaguya::State& state)
{
kaguya::LuaRef luafun = state.loadstring("assert(11 == 11);return true");
TEST_CHECK(luafun() != false);
state.setErrorHandler(ignore_error_fun);
kaguya::LuaRef errorref = state.loadstring("function() e");//syntax error
TEST_CHECK(!errorref);
}
示例3: KAGUYA_TEST_FUNCTION_DEF
KAGUYA_TEST_FUNCTION_DEF(luaL_requiref_test)(kaguya::State& s)
{
using namespace kaguya;
luaL_requiref(s.state(),"mylib", lua_mylibf,false);
kaguya::LuaStackRef ref(s.state(), -1,true);
TEST_EQUAL(ref["value"],111);
TEST_CHECK(s("assert(mylib == nil)"));
}
示例4: KAGUYA_TEST_FUNCTION_DEF
KAGUYA_TEST_FUNCTION_DEF(object_take_copy)(kaguya::State& state)
{
state["Base"].setClass(kaguya::ClassMetatable<Base>()
.addConstructor()
.addProperty("a", &Base::a)
);
TEST_CHECK(state("fn = function(object,value) object.a = value end"));
{
Base base;
base.a = 232;
state["obj"] = base;
TEST_CHECK(state("assert(obj.a==232)"));
TEST_CHECK(state("obj.a = 2"));
TEST_CHECK(state("assert(obj.a==2)"));
TEST_EQUAL(base.a, 232);
state.garbageCollect();
TEST_CHECK(state("assert(obj.a==2)"));
state["fn"](base, 54);
TEST_EQUAL(base.a, 232);
}
{//
Base basesrc;
Base& base = basesrc;
base.a = 232;
state["obj"] = base;
TEST_CHECK(state("assert(obj.a==232)"));
TEST_CHECK(state("obj.a = 2"));
TEST_CHECK(state("assert(obj.a==2)"));
TEST_EQUAL(base.a, 232);
state.garbageCollect();
TEST_CHECK(state("assert(obj.a==2)"));
state["fn"](base, 54);
TEST_EQUAL(base.a, 232);
}
{//
Base basesrc;
const Base& base = basesrc;
basesrc.a = 232;
state["obj"] = base;
TEST_CHECK(state("assert(obj.a==232)"));
TEST_CHECK(state("obj.a = 2"));
TEST_CHECK(state("assert(obj.a==2)"));
TEST_EQUAL(base.a, 232);
state.garbageCollect();
TEST_CHECK(state("assert(obj.a==2)"));
state["fn"](base, 54);
TEST_EQUAL(base.a, 232);
}
}
示例5: KAGUYA_TEST_FUNCTION_DEF
KAGUYA_TEST_FUNCTION_DEF(defaultarguments)(kaguya::State& state)
{
KAGUYA_FUNCTION_OVERLOADS(defargfn_wrapper, defargfn,0,3)
state["defargfn"] = kaguya::function(defargfn_wrapper);
state.dostring("assert(defargfn() == 6)");
state.dostring("assert(defargfn(6) == 12)");
state.dostring("assert(defargfn(6,5) == 30)");
state.dostring("assert(defargfn(2,2,2) == 8)");
}
示例6: KAGUYA_TEST_FUNCTION_DEF
KAGUYA_TEST_FUNCTION_DEF(defaultarguments)(kaguya::State& state)
{
state.setErrorHandler(error_fun);
state["defargfn"] = kaguya::function(defargfn_wrapper());
state["defargfn2"] = kaguya::function(defargfn_wrapper2());
state.dostring("defargfn('abc')");
last_error_message.find("[OPT]int,[OPT]int,[OPT]int");
state.dostring("defargfn2('abc')");
last_error_message.find("int,int,[OPT]int");
}
示例7: KAGUYA_TEST_FUNCTION_DEF
KAGUYA_TEST_FUNCTION_DEF(defaultarguments)(kaguya::State& state)
{
state.setErrorHandler(error_fun);
state["defargfn"] = kaguya::function(defargfn_wrapper());
state["defargfn2"] = kaguya::function(defargfn_wrapper2());
std::string intname = kaguya::util::pretty_name(typeid(int));
if (intname == "int")
{
state.dostring("defargfn('abc')");
TEST_CHECK_M(last_error_message.find("[OPT]int,[OPT]int,[OPT]int") != std::string::npos, last_error_message);
state.dostring("defargfn2('abc',3)");
TEST_CHECK_M(last_error_message.find("int,int,[OPT]int") != std::string::npos, last_error_message);
}
}
示例8: Register
void LuaInterfaceBase::Register(kaguya::State& state)
{
state["RTTRBase"].setClass(kaguya::ClassMetatable<LuaInterfaceBase>()
.addMemberFunction("Log", &LuaInterfaceBase::Log)
.addMemberFunction("IsHost", &LuaInterfaceBase::IsHost)
.addMemberFunction("GetLocalPlayerIdx", &LuaInterfaceBase::GetLocalPlayerIdx)
.addMemberFunction("MsgBox", &LuaInterfaceBase::MsgBox)
.addMemberFunction("MsgBox", &LuaInterfaceBase::MsgBox2)
.addMemberFunction("MsgBoxEx", &LuaInterfaceBase::MsgBoxEx)
.addMemberFunction("MsgBoxEx", &LuaInterfaceBase::MsgBoxEx2)
);
state.setErrorHandler(ErrorHandler);
}
示例9: KAGUYA_TEST_FUNCTION_DEF
KAGUYA_TEST_FUNCTION_DEF(int_constructor)(kaguya::State& state)
{
state.openlib("test_bind", &luaopen_test_bind);
TEST_CHECK(state("value = assert(test_bind.TestClass.new(32))"));
TEST_CHECK(state("assert(value:getInt() == 32)"));
TEST_CHECK(state("assert(test_bind.squared(3) == 9)"));
TEST_CHECK(state("assert(test_bind.submodule.squared(6) == 36)"));
TEST_CHECK(state("assert(test_bind.submodule.submodule.squared3(6) == 36)"));
TEST_CHECK(state("assert(test_bind.x == 1)"))
TEST_CHECK(state("assert(test_bind.ENUM_TEST_TYPE.ENUM_TEST_A == 2)"));
TEST_CHECK(state("assert(test_bind.ENUM_TEST_TYPE.ENUM_TEST_B == 4)"));
}
示例10: KAGUYA_TEST_FUNCTION_DEF
KAGUYA_TEST_FUNCTION_DEF(native_function_call_test)(kaguya::State& state)
{
using namespace kaguya::nativefunction;
Foo foo;
state.newRef(6).push();
state.newRef(9).push();
state.newRef(2).push();
call(state.state(), &free_standing_function);
lua_settop(state.state(), 0);
state.newRef(&foo).push();
state.newRef("Bar").push();
call(state.state(), &Foo::setBar);
#if KAGUYA_USE_CPP11
state.newRef(&foo).push();
state.newRef(9).push();
call(state.state(), [](Foo* foo, int b) {
foo->setBar("fromlambda");
});
TEST_EQUAL(foo.bar, "fromlambda");
std::string capture("capture");
call(state.state(), [=](Foo* foo, int b) {
foo->setBar(capture + "lambda");
});
TEST_EQUAL(foo.bar, "capturelambda");
#endif
lua_settop(state.state(), 0);
}