Some c API for Lua used in the luars232 library

Source: Internet
Author: User
Tags lua

The code is not posted, here is just to comb the first two of the missing things, as read the code record it.

1. header file

#include <lauxlib.h><lua.h>

All API functions and related types and constants is declared in the header file "Lua.h".

The Auxiliary library provides several convenient functions to interface C with Lua. While the basic API provides the primitive functions for all interactions between C and Lua, the Auxiliary library provide s higher-level functions for some common tasks. All functions from the auxiliary library is defined in header file Lauxlib.h and has a prefix lual_.

2, void *lua_newuserdata (Lua_state *l, size_t size).

First we need to know what is called UserData? Well, I understand that all the data types in C, there is only one name UserData in Lua. In fact, this is just a piece of memory. If you remember Lightuserdata, you should remember that this is a pointer.

Lua_newuserdata This call is to request a chunk size of memory, and then return a pointer to the UserData (void type), the pointer to the first address of the space, and push a userdata type element onto the heap.

LUA does not understand userdata, and cannot operate on its contents. So, using UserData, we also need to add the relevant operation method, in addition, new out of the UserData, but also need to add a GC meta-method for Lua to retract the release.

As to when UserData will be retrieved by GC ... Well, I've been looking for a long time without a definitive answer, or a credible answer, and I don't want to go through the code for the moment, but some say it is this: the GC assumes that all UserData are required to end, and then starts to find out if the root object it points to is valid (as to how a valid method is temporarily unknown), If it is not valid, reclaim the UserData space.

Chase the call case here:

1) We'll come out in L. A size is a pointer-type space, and its value is an address returned by the DLL, which is brought back by the pointer p to this address, so there is a UserData type element at the top of the heap. There is no need for us to Pushinteger to Lua.

2) Set the metatable for this element.

3) METATABLE.__GC () is implemented by invoking the operation in the DLL. Call the method inside the DLL directly, free (p). This completes the GC.

3, int lual_optint (lua_state *l, int narg, int d)

Check the Narg, if it is a number, returns an int of type, and if it is nil or not, then return parameter d. I don't want to know what this is for.

4, No 4 has incredibly, well, hastily put this wrapper to see again.

Some c API for Lua used in the luars232 library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.