Code Monkey home page Code Monkey logo

lua_reg's Introduction

lua_reg

C++ register function to lua

Copyright (c) 2013, Chen Yu. All rights reserved.

Use of this source code is governed by a BSD-style license that can be found in the License file.

一个lua与C++交互粘合层,支持C++函数导出到lua、C++调用lua函数、C++引用lua对象,等等。

设计目标:接口简单、不依赖第三方库

std::allocator<char> std_allocator;
luareg::state_t state(std_allocator);

try
{
	test_t t;

	// free function & class method
	luareg::module(state, "cpp")
		<< lua::def("test0", &test0)
		<< lua::def("test1", &test1)
		<< lua::def("test2", &test2)
		<< lua::def("test3", &test3)
		<< lua::def("test4", &test4)
		<< lua::def("test5", &test5)
		<< lua::def("test6", &t, &test_t::test6);

	luareg::module(state, "cpp")
		<< lua::def("test7", &test7)
		<< lua::def("test8", &test8)
		<< lua::def("test9", &test9)
		<< lua::def("test10", &test10)
		<< lua::def("test11", &t, &test_t::test11)
		<< lua::def("test12", &test12)
		<< lua::def("test13", &test13)
		<< lua::def("test14", &test14)
		<< lua::def("test15", &test15)
		<< lua::def("test16", &test16)
		<< lua::def("test17", &test17)
		<< lua::def("test18", &test18)
		//<< lua::def("test19", [](int n, const std::string &msg)->int{ return n; })
		<< lua::def("test20", &test20);

	base_t base;
	luareg::module(state)
		<< lua::def("test21", &test21)
		<< lua::def("base_print", &base, &base_t::print);

	// object method

	luareg::module(state, "cpp")
		[
			luareg::class_t<foo_t>(state, "foo_t")
			<< luareg::constructor<int>()
			<< luareg::destructor()
			<< luareg::def("add", &foo_t::add)
			<< luareg::def("get", &foo_t::get)
			<< luareg::def("get_pointer", &foo_t::get_pointer)
			<< luareg::def("get_base", &foo_t::get_base)
		]
		<< lua::def("test21", &test21);

	lua::execute(state, "test.lua");
}
catch (const luareg::parameter_error_t &e)
{
	std::cout << e.what() << std::endl;
	e.dump(std::cout);
}
catch(const luareg::fatal_error_t &e)
{
	std::cout << e.what() << std::endl;
	e.dump(std::cout);
}

try
{
	lua::execute(state, "test2.lua");
	std::pair<int, std::string> n = lua::call(state, "test_call", 1, "haha", 10.2, false);

	auto val = std::make_pair("test abc", 10.2);
	lua::call(state, "test_call2", 1, "haha", val);
}
catch(const luareg::fatal_error_t &e)
{
	std::cout << e.what() << std::endl;
	e.dump(std::cout);
}

lua_reg's People

Contributors

chenyu2202863 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

lua_reg's Issues

调用原生函数失败

直接调用
string.format("%d", 11)
提示

test.lua:9: attempt to index a nil value (global 'string')
lua stack:
C: __index
test.lua(9): ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.