HaxeFoundation / HaxeFoundation/haxe

[Feature Request / Lua] Define global classes/functions

Open
#6,517 4 comments 0 reactions 1 assignee Claimed by @jdonaldson View on GitHub
enhancement platform-lua
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

In my project I use Haxe Lua for a mod in Minecraft called OpenComputers. This mod adds programmable computers with the Lua language. They have limited native API, so for instance `io` and `print` does not exists.

You can create your own implementation of this, but it is not possible to register these with Haxe. Currently I did a workaround with `@:expose` and `Compiler.includeFile`:

```lua
local _hx_exports = _hx_exports or {}
_G.io = _G.io or setmetatable({}, {__index = function (t, k)
if _hx_exports.io then
return _hx_exports.io[k]
else
error('io is not implemented')
end
end})
_G.print = _G.print or (function(...)
if _hx_exports.print then
_hx_exports.print(...)
else
error('print is not implemented')
end
end)
```

However, this is very "hacky". For every new global I have to create a wrapper for the function or metatable for objects.

Now was my request to be able to register the classes and functions globally, so for instance `@:exposeGlobal(name, override)` so I can use them in both Haxe (for `trace`, `Io.open`) and in Lua.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.