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

[Lua] 0-indexed arrays #39

Open
Eiyeron opened this issue Sep 22, 2017 · 5 comments
Open

[Lua] 0-indexed arrays #39

Eiyeron opened this issue Sep 22, 2017 · 5 comments
Labels

Comments

@Eiyeron
Copy link
Contributor

Eiyeron commented Sep 22, 2017

Hello.

I tried to use the lib in a simple Lua project but I quickly went against a design issue with the current API : Lua's table indexes start at 1, whereas almost the rest of the world, including Haxe, starts at 0. So a classic Haxe array will be a bit weird to access from Lua as it might induce code such as manually setting the 0th element instead of directly using an array.

This is almost an edge case, so I was wondering if wrapping or conditionnal compilation would allow us to offer to lua users a module easily useable without a weird interface.

@boronine
Copy link
Member

Under what circumstances would one need to "access a classic Haxe array from Lua"? Our Haxe-generated Lua code uses native Lua tables, doesn't it?

@Eiyeron
Copy link
Contributor Author

Eiyeron commented Sep 22, 2017

import lua.Table;

@:expose
class Tests {
    public function new()
    {

    }
    // Classic return, that's how we do in hsluv
    public static function returnArray():Array<String>
    {
        return ["Hello", "World"];
    }

    // Using the lua.Table function instead
    public static function returnTable():Table<Int, String>
    {
        var table = Table.create(["Hello", "World"]);
        
        return table;
    }
}
-- Small file to test the exported lib
print("--- Start Tests ---")
-- The Haxe file is here compiled into test.lua
local test = require("test")

print(test.Tests.returnArray()[1])
print(test.Tests.returnTable()[1])

Yields

--- Start Tests ---
World
Hello

@Eiyeron
Copy link
Contributor Author

Eiyeron commented Sep 22, 2017

Note, the compiled function explicitely set the 0th element when just returning an Haxe array:

_hx_exports["Tests"] = Tests
Tests.returnArray = function() 
  do return _hx_tab_array({[0]="Hello", "World" }, 2) end;
end
Tests.returnTable = function() 
  local table = ({"Hello","World"});
  do return table end;
end

@boronine
Copy link
Member

Ok I understand now. I assumed you were referring to https://github.com/hsluv/hsluv-lua code when you ran into the limitation. The Lua code there is a cleaned up version of the Haxe output. What you're suggesting makes sense, but I'm not sure if it's worthwhile considering that Haxe-generated Lua is very messy anyways and needs manual cleanup.

The underlying problem here is using the array/list type to represent colors. The proper type for these values is a tuple/struct/class/"product type" but all of those tend to generate a lot of boilerplate when compiled with Haxe.

@Eiyeron
Copy link
Contributor Author

Eiyeron commented Sep 24, 2017

Well, I didn't check hsluv-lua's codebase, I just referred to Haxe's output but yeah, if it have a cleaner api, I guess it'd be interesting to tell the users to avoid exporting to Lua as the other repo is more accessible.

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

No branches or pull requests

2 participants