Compilation of lua learning

compile

  1. lua is an interpreted language
  2. But lua allows source code to be compiled into an intermediate form before running it
  3. The main characteristic that distinguishes interpreted languages ​​is not whether they can be compiled
    1. Whether the compiler is part of the language runtime library
    2. Is it capable of executing dynamically generated code

loadfile function

  1. The dofile function is a built-in operation for running blocks of lua code

  2. dofile just does the auxiliary work of loadfile

  3. loadfile will load lua code blocks from a file

  4. but will not run the code, just compile the code

  5. Then return the compiled result as a function

  6. dofile throws an error

  7. loadfile just throws error values, but doesn't handle errors

function dofile(filename)
    -- assert return error value
    local f = assert(loadfile(filename))
    return f()
end
  1. When an error occurs, loadfile will return nil and an error message, which can be customized
  2. When you need to run a file multiple times, you only need to call loadfile once, and call its return result multiple times, that is, the function.
  3. The overhead of dofile is much larger than that of loadfile, because loadfile only compiles the file once

loadstring function

  1. read code from a string
  2. also returns a function
  3. Inexpensive because it compiles once every time loadstring is called
  4. And the way of writing function is only compiled once when compiling for the program block
i = 0
f = loadstring("i = i + 1") -- equal to f = function() i = i + 1 end
f()
print(i) -- 1
f()
print(i) -- 2

-- dostring Finish loading and running the code
assert(loadstring(s))() -- Grammatical errors "attempt to call a nil value"
  1. loadstring compiles without lexical domains
  2. loading only compiles strings in the global environment, not the local environment
i = 32
local i = 0
f = loadstring("i = i + 1; print(i)")
g = function() i = i + 1; print(i) end
f() -- 33 using global variables
g() -- 1 using local variables
  1. Can execute external code
do
print("enter you expression:")
local l = io.read()
local func = assert(loadstring("return '" .. l .. "'"))
print("the value of your expression is " .. func())
end

do
print("enter function to be plotted(with variable 'x'):")
local l = io.read()
local f = assert(loadstring("return " .. l))
for i = 1, 20 do
    x = i
    print(x .. ":" .. string.rep("*", f()))
end
end
  1. loadfile and loadstring , there is a real primitive function load

  2. loadfile and loadstring read blocks from file and string respectively

  3. load takes a "reader function" and calls it internally to get the block

  4. The reader function can return a block several times, and load will call it repeatedly until it returns nil (indicating the end of the block)

  5. load is only used when the block is not in the file, or if the block is too large to fit in memory

  6. lua treats all independent program blocks as the body of an anonymous function that also has variable-length arguments

  7. Like other functions, local variables can be declared in a block

loadstring("a = 1") -- equal to function(...) a = 1 end
f = loadstring("local a = 10; print(a + 10)")
f() -- 20
  1. Rewrite the read input example to avoid using the global variable x
print("enter function to be plotted (with variable 'x'):")
local l = io.read()
local f = assert(loadstring("local x = ...; return " .. l))
for i = 1, 20 do
    print(string.rep("*", f(i)))
end
  1. The load function does not throw an error. When an error occurs, load will return nil with an error message
print(loadstring("a a"))
-- nil [string "a a":1 '=' expected nead 'a']

  1. loadfile and loadstring have no side effects
  2. They just compile the block into an intermediate representation and return the result as an anonymous function.
  3. instead of loading a block, or defining a function within it
  4. A function definition is an assignment operation that is done at runtime.
-- write a lua file, named foo
function foo(x)
	print(x)
end

-- exist cmd middle lua input in the interpreter
f = loadfile("you store foo file path")
print(foo()) -- nil
f() -- define function
foo("test") -- test
  1. Execute some operation of external code
  2. Report any errors when processing loader blocks
  3. If the code is not trusted, it needs to be executed in a protected environment (i.e. the aforementioned "sandbox")

Tags: lua

Posted by wsantos on Wed, 01 Jun 2022 08:56:32 +0530