neolithos / neolua

A Lua implementation for the Dynamic Language Runtime (DLR).
https://neolua.codeplex.com/
Apache License 2.0
471 stars 76 forks source link

Dot dot dot usage in loadstring/file #89

Open coldino opened 5 years ago

coldino commented 5 years ago

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
neolithos commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

neolithos commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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