Why does Lua loadstring() not work on the demo site?

Viewed 4544

I am trying to test out the Lua loadstring function.

This example is taken direction from the Lua documentation:

f = loadstring("i = i + 1")

However, when I try to run it at:

https://www.lua.org/cgi-bin/demo

I get the following error:

input:1: attempt to call a nil value (global 'loadstring')
2 Answers

loadstring is not available in Lua past version 5.1. The demo you linked is running Lua 5.3. You can prove this by running the program print(_VERSION).

In Lua 5.2 an later your code should use load, here is an example program you can run that loads a chunk from a string:

local f = load("return 2 + 2")
print(f())

It is Lua 5.4 and load is there. Look...

tdump=[[for k,v in pairs(_G) do print(k,v) end]]
load(tdump)()

I think the Lua 5.4 CGI is cutted down of functions because of security reasons. Look...

tdump=[[for k,v in pairs(_G.io) do print(k,v) end]]
load(tdump)()

...there is only: write()

Related