Giter Site home page Giter Site logo

luah's Introduction

                                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.

luah's People

Contributors

resty-daze avatar

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.