inlove2010 / luah Goto Github PK
View Code? Open in Web Editor NEWThis project forked from resty-daze/luah
a simple lua c++ helper library
License: MIT License
This project forked from resty-daze/luah
a simple lua c++ helper library
License: MIT License
README ====== Table of Contents ================= 1 LuaH 1.1 What is LuaH? 1.2 Tutor 1.2.1 Import a function to Lua 1.2.2 Call a lua function 1.2.3 Import a class to Lua 1 LuaH ~~~~~~~ 1.1 What is LuaH? ================== When using lua in C++, the lua's native api is well designed, but import functions to lua become a repetitive work. So I wrote this library to help me to do this work. Compare to other bind librarys, it keeps very lightweight. I'm trying to keep the API simple. 1.2 Tutor ========== 1.2.1 Import a function to Lua ------------------------------- in C++: You can import a function to lua as follow: // function: int function_to_export(int x) { return x + 1; } // import: // ... luah::add_func(L, "my_func", function_to_export); // ... then, use it in lua: print(my_func(3)) You can also import a function to a table: // following code import the function_to_export to table lua_newtable(L); luah::add_func_table(L, -1, "my_func", function_to_export); lua_setglobal(L, "my_table"); 1.2.2 Call a lua function -------------------------- if we have a lua function: function double(x) return x * 2 end then we can call it in C++: int ret = luah::call_func<int>(L, "double", 6); 1.2.3 Import a class to Lua ---------------------------- To make import a class to lua, it can be do as follow: // a example class class MyClass { public: MyClass(int x, int y) : x_(x), y_(y) {} int get_sum() { return x_ + y_; } void set_x(int x) { x_ = x; } void set_y(int y) { y_ = y; } private: int x_, y_; }; // import code: luah::add_class<MyClass>("myclass")[luah::ctor<int, int>()] << luah::method("get_sum", &MyClass::get_sum) << luah::method("set_x", &MyClass::set_x) << luah::method("set_y", &MyClass::set_y); then use in lua: a = myclass(2, 3) print(a.get_sum()) a.set_x(4) print(a.get_sum()) hint: - the [luah::ctor<int, int>] is the to set the construct function of the class, it means new T(int, int) will be called to create a new instance, if you just want to call new T(), this part could be ignored.
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.