HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.2k stars 657 forks source link

Add @:luaNotLocal compiler metadata and lua_no_xpcall compiler flag for Lua backend #10309

Open ghost opened 3 years ago

ghost commented 3 years ago

Hi everyone, Thanks for making Haxe! :)

I wish if Haxe's Lua backend would introduce/add these 2 because really good...

  1. @luaNotLocal can be used to disable generating local variables and just let them global, The purpose of this is to let some game engines/tools like Scrupp works because it does not allow game loop table to be local variable...

  2. lua_no_xpcall can be helpful in case xpcall does not work properly on Lua interpreter dev working with, In addition this can help making Haxe externs for Lua homebrews much easier, As MicroLua (3DS/NDS Lua homebrew) does not work with xpcall and to get it working i was forced to edit last line of code to call main function with no xpcall usage, The same even goes to Amulet i think so...

Sorry if this issue not helpful, I ❤️ Haxe and i wish if i can get stuff working with no headache of editing generated code!

Thanks for reading and awaiting replies...

jdonaldson commented 3 years ago

Very cool! I didn't know about these micro frameworks.

  1. For managing local/global scenarios, I'm a little wary of overriding the scoping of a variable for any reason... it can very quickly clobber references in other scopes, and the likelihood of this happening increases as the complexity of the app increases. Is there possibly a way you could handle this with low level __lua__ calls instead in a very isolated fashion?
  2. xpcall is a vital mechanism for capturing exceptions. If it's not available on a given platform, you might just alias it to a dummy pass through function.

It would be helpful to also see some problematic code from your end, perhaps I can be more specific with suggestions.

ghost commented 3 years ago
  1. Is there possibly a way you could handle this with low level __lua__ calls instead in a very isolated fashion?

I wish but i would something like this in Haxe to get working

package;

import hxscrupp.Scrupp;
import lua.Table;

class Main {
    public static function main() {
        Scrupp.init("Scrupp", 640, 480, 32, false);

        @:expose("main")
        var main: lua.Table<String, haxe.Constraints.Function> = Table.create({
            render: function(dt: Float) {
              trace(dt);
            }
        });
    }
}

Unfortunately, Haxe generates main variable (Not the function) as local main and that's what in my mind, I can do something like this and it's working :)

package;

import hxscrupp.Scrupp;

class Main {
    public static function main() {
        Scrupp.init("Scrupp", 640, 480, 32, false);

        function render_func(dt: Float) {
          trace(dt);
        }

        untyped __lua__("
          main = {
            render = render_func
          }
        ");
    }
}

But i somehow feel that this not good practice/idea i see, But it's last hope if gonna count on it...

  1. xpcall is a vital mechanism for capturing exceptions. If it's not available on a given platform, you might just alias it to a dummy pass through function.

Well all engines i mentioned has xpcall support but the problem is in the exception detection due to how Haxe generates Lua code, To be honest i wish if there is build option that allows to embed code depending on target, For example MicroLua triggers errors when using xpcall maybe because of being Lua modified...

Awaiting replies!