Import the Go function into LUA for LUA invocation

Source: Internet
Author: User
Tags lua
This is a creation in Article, where the information may have evolved or changed.

There is a need to use the go language to implement some basic modules, using LUA to implement basic logic, so there is the need to invoke the Go function in Lua.

There is very little data on go, but the go can embed the C language, making this implementation feasible.

Finally, the full userdata in Lua was used to implement this thing. The functions in Go are all encapsulated into UserData for Lua to invoke. This is possible, since UserData can be set in metatable,metatable can be set in the original method __call, then the following call: Func (1, 3) can become Func.metatable.__call (func, 1, 3) , we can implement this requirement by implementing a meta-method in C, loading a specific metatable, and setting this metatable to the metatable of the function UserData.

All calls in the Lua script will be called to the C code part of Go, in C function, this UserData parse, determine to a go function, then in go to call the Go language part, then lua-"Go" bridge has been opened.


So, we can define a structure in go

Type luago_state struct {    .... Basepart    gofunctions map[int]interface{}    baseseq int}

Some of the basic LUA operations should have been implemented in Basepart, such as creating something, and I'm not going to post it. Baseseq is the ordinal of the exported gofunction, which is used to determine the callback's go function. The Gofunctions records the corresponding gofunction of an ordinal number.

Then, we need to record the corresponding luago_state in the lua_state in the lua_state.

Turn on Gofunction Registration support func (this *luago_state) luago_openinvokecompenent () {//Initialize export Mapc.luago_open (this.handle, unsafe. Pointer (this)) This.gofunctions = Make (map[int]interface{}) This.baseseq = 1}
So we're going to press the luago_state in the C code.


The next step is to register a go function, and each go function corresponds to a sequence within a luago_state, then make a record in the map, and then register it in the C code.

Press-in Gofunctionfunc (this *luago_state) luago_pushgofunction (_name string, _func luago_function) int {funcseq: = This.luago_getgofunctionseq () this.gofunctions[funcseq] = _func//registered in Lua cfuncname: = c.cstring (_name) C.luago_ Pushgofunction (This.handle, Cfuncname, C.int (FUNCSEQ)) C.free (unsafe. Pointer (Cfuncname)) return FUNCSEQ}

In C code, the idea is to create a userdata,userdata value for this go function is its ordinal number, and then set the above MetaTable, recorded in the Lua_state global variables. The original method in C is called when a function call is made in Lua.


Meta-method static int Luago_metamethod_call (struct lua_state* L) {//stack UserData, Parameters...if (Lua_isuserdata (L, 1)) {// Check validint* Puserdata = (int*) lual_checkudata (L, 1, g_szgometatable); if (0! = puserdata) {int ngofunctionseq = *pUserDa ta;//get the golua_statevoid* pluagostate = Luago_getgoluastate (L);//call go callbackreturn (int) Luago_call ( Pluagostate, ngofunctionseq);} Else{lual_argcheck (L, Puserdata! = 0, 1, "gofunction expected");}} Luago_error (L, "trying to call a Non-callable object"); return 0;}
This is also very simple, take the userdata sequence out, call the go callback function, in go through this sequence to find the corresponding function, call this function.

Related Article

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.