neolua icon indicating copy to clipboard operation
neolua copied to clipboard

Dot dot dot usage in loadstring/file

Open coldino opened this issue 6 years ago • 7 comments

I can't find in the Lua docs if this usage of ... inside loadstring or loadfile is valid or standardised, but I do see it working on LuaJit.

NeoLua Version: Latest commit (a0fdcc68d0dbb847296322e761e4adeee5db0859)

Example to reproduce:

> data = {}
> fn = loadstring("d = ...; d.test = 1")
Error: Can not call nil value.

Expected behaviour (from LuaJit):

> data = {}
> fn = loadstring("d = ...; d.test = 1")
> fn(data)
> print(data.test)
1

coldino avatar Dec 15 '18 14:12 coldino

loadstring is not an official function any more.

To get this to work, I need to change a lot of stuff down to the parser. But it is possible to create this default argument (for load).

Btw: I use as reference: https://www.lua.org/cgi-bin/demo

neolithos avatar Dec 16 '18 11:12 neolithos

I'm happy to do work for this, if you can provide more information.

My requirement is to read from a file - loadstring was just used to demo the issue, but I see it has been replaced by simply load.

coldino avatar Dec 16 '18 12:12 coldino

The compile funktion has no arguments, the null needs to get an array of objects.

neolithos avatar Dec 16 '18 13:12 neolithos

The offset point for all changes is in LuaGlobal.cs:333. The third parameter of CompileChunk need the change. First I will checkout the differents between dochunk and load. May you can provide me some detailed information, examples.

neolithos avatar Dec 17 '18 09:12 neolithos

I'll do what I can. The reproduction I gave is an example of many generated files in the project I'm working on. The files in question look like:

local data = ...
data.points = { } -- with lots of data

There are also cases like this:

local skills, mod, flag, skill = ...
skills["Blah"] = { } -- with lots of data

These files are loaded using the function:

function LoadModule(fileName, ...)
	if not fileName:match("%.lua") then
		fileName = fileName .. ".lua"
	end
	local func, err = loadfile(fileName)
	if func then
		return func(...)
	else
		error("LoadModule() error loading '"..fileName.."': "..err)
	end
end

The Lua docs are somewhat vague on this and state: Vararg expressions, denoted by three dots ('...'), can only be used when directly inside a vararg function; they are explained in §3.4.11. ...but, this file is being treated as a function so I assume that means this is a valid use.

coldino avatar Dec 17 '18 11:12 coldino

It is vague indeed, and this is not the only thing.

I changed it, know that your code should run?

local fn = load('local a, b = ...; return a, b;');
return fn(23,42);

neolithos avatar Dec 17 '18 17:12 neolithos

From a quick check this does appear to help - thank you. I'll test further tomorrow.

coldino avatar Dec 18 '18 01:12 coldino