简体   繁体   中英

Lua loadstring Function fail

Given this code:

local fruit = {}
fruit.name = "Bramley"
loadstring("fruit.pips = '2'")
fruit.skinc = 'Red'
print(fruit)

Why aren't the pips added to the table: table

'fruit'{
  'name'='Bramley',
  'skinc'='Red'
}

loadstring() (or load() in Lua 5.2 or higher) returns a function, you have to run that function to execute the code. Like this:

fruit = {}
fruit.name = "Bramley"
loadstring("fruit.pips = '2'")()

Note that fruit has to be global, or an error would be generated because the environment of the returned function of loadstring is the global environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM