Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dot dot dot usage in loadstring/file #89

Open
coldino opened this issue Dec 15, 2018 · 7 comments
Open

Dot dot dot usage in loadstring/file #89

coldino opened this issue Dec 15, 2018 · 7 comments

Comments

@coldino
Copy link

coldino commented Dec 15, 2018

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 (a0fdcc6)

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
@neolithos
Copy link
Owner

neolithos commented Dec 16, 2018

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

@coldino
Copy link
Author

coldino commented Dec 16, 2018

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.

@neolithos
Copy link
Owner

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

@neolithos
Copy link
Owner

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.

@coldino
Copy link
Author

coldino commented Dec 17, 2018

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.

@neolithos
Copy link
Owner

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);

@coldino
Copy link
Author

coldino commented Dec 18, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants