Communication process decomposition in Lua and C + + _lua

Source: Internet
Author: User
Tags lua

The online tutorial on Lua does not seem to have flooded, and recently just learned to use LUA in Cocos2d-x, of course, I am a tutorial fanatic, I will share My learning experience ~
(Narrator: I poof ~! Every time you write something I will spit, you are not tired? = =)

This is the first lesson, first let Lua and C + + to know, by the way let them go shopping for a little hand or something ...
(Narrator: ...) Blow, continue to blow)

1. Lua's stacks and global tables

Let's briefly explain the LUA stack and global table, which you should be familiar with, mainly to communicate with C + + and Lua, yes, they don't know each other, they can only communicate through the stack, just like writing letters.
(Narrator: They don't use micro-letters?!) Micro-letter ~! I don't know? )

What is the global table for LUA? Can be imagined as a map hash table structure, for example LUA has a variable:

Copy Code code as follows:
name = "Hello"

The global table then holds the corresponding relationship between "name" and "Hello", and Lua can find hello in the global table by name. It's supposed to be like this ~
(Narrator: should = =! )

2. The first communication between LUA and C + +

Now to design a scenario where C + + sees Lua at a JavaScript developer conference, C + + is deeply attracted by Lua.
(Narrator: JavaScript Convention ... That for Mao is lua in the lecture ~! )

Let's take a look at what this beautiful Lua lady looks like:

Copy Code code as follows:
--Hello.lua file
myname = "Beauty Girl"

OK, a simple and beautiful Miss Lua.
Then, C + + wants to know what Lua is called, so they have to communicate. To look at the communication process:

Note the red number, which represents the order of communication:
1 C + + wants to get the value of the LUA myname string, so it puts myname on the LUA stack (the top of the stack) so that LUA can see
2 Lua gets the myname from the stack (top), when the top of the stack becomes empty again
3 Lua takes this myname to the LUA global table lookup myname corresponding string
4 Global Table Returns a string "Beauty Girl"
5 Lua puts the "Beauty Girl" string on the stack (top of stack)
6 C + + can get "beauty Girl" from the LUA stack, the name of this beautiful Miss Lua.
The world is so beautiful, it is so simple.
(Narrator: OK, this time do not spit, really simple ... )

But, (narrator: I knew ~! I knew it wasn't that simple! This is just the simplest case, in fact, the various C + + and LUA operations are much more complicated than that, but the rationale is the same.
Well, while the narrator hasn't spit, we're going to end the first chapter and hopefully we can help.
(Narrator: It's like I like to grab a scene like this, am I?) = = Wait ~! example? What about demo?)

Oh, demo will be introduced in the next chapter.
(Narrator: Hanging appetite ...) Definitely in the hanging appetite ... You think I'm going to look forward to it, my soul is light ... The heart is very urticant ~! )

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.