A detailed explanation of the functions involved in compiling and executing code in Lua _lua

Source: Internet
Author: User
Tags function prototype lua

It can be said that Lua is called an interpreted language because there are functions such as load, because such a function allows Lua to execute dynamically generated code. The following is a detailed analysis of these functions.

Load function

The Load function prototype is as follows:

Copy Code code as follows:

Load (chunk [, chunkname [, mode [, Env]]]

The function loads a chunk, and returns a function if there are no errors. If the value of the incoming chunk is a string, the string is loaded; If the value of the incoming chunk is a function, then the function must return a string, and load will call the function until the function returns an empty string or nil. Load will connect the return result of each function as a chunk to load.

If there is no syntax error, load returns the chunk for the function, otherwise nil and the corresponding error message are returned. If the returned function has a upvalue, the value of the first Upvalue is set to the passed-in parameter env, and if no value is passed in, the value of the first upvalue is the global environment. The returned function Upvalue will not be shared by any other functions.

The parameter chunkname is used as the chunk name for the error message for debugging. If chunk is a string, the Chunkname default value is the value of the variable chunk, otherwise = (load). Parameter mode determines whether the value passed to the chunk is text or binary data. If "B", the value passed to chunk must be binary data (for example, you can use String.dump (foo) to get binary data for the corresponding function, then pass to load), if it is "T" and must be text data, if it is "BT", then file or binary data can be. The default value is "BT".

LUA does not detect the stability of binary data, so malicious binary data can cause LUA interpreter crash.

LoadFile function

The function prototype is as follows:

Copy Code code as follows:

LoadFile ([filename [, mode [, Env]])

This function is similar to load, but is loaded from the active chunk in file filename or standard input (if filename is empty).
Dofile function
The function prototype is as follows:

Copy Code code as follows:

Dofile ([filename])

Reads the contents of the file filename and executes it as a LUA chunk. If no parameters are passed in, the execution content is read from standard input. The return value is the same as the value returned after chunk execution. If an error occurs, Dofile passes the error message to its caller. In other words, Dofile is executed in unprotected mode. Note that this function differs from loadfile,loadfile simply returns a function and does not execute it, and when an error occurs, LoadFile returns an error message that is not passed to its caller. In summary, LoadFile loads the LUA code block from a file, but does not run the code, just compiles the code, and then returns the results of the compilation from a function. Dofile, in addition to compiling code, also runs the returned results.
The Lua-level compiler-related functions discussed above, corresponding to the C API related functions are: Lua_load, Lual_loadfilex, Lual_loadfile, and Lual_dofile, which are very similar to the corresponding LUA layer functions.

LoadString function

The function prototype is as follows:

Copy Code code as follows:

LoadString (string [, Chunkname])

This function is similar to load, which is equivalent to passing a string as a parameter to the load function. In order to load and run a string, you can write these:

Copy Code code as follows:

ASSERT (LoadString (s)) ()

Note that the function was deleted in Lua5.2 because it could be completely replaced by load.

The above is the full content of this article, I hope that the mastery of Lua can help.

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.