teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.02k stars 101 forks source link

Support luajit native bit library instead of bit32 #723

Open creationix opened 8 months ago

creationix commented 8 months ago

I would love to be able to use teal with luajit based runtimes. As you know, luajit has a nice bit library built-in that is the same or very similar as bit32. I need a way to configure luajit as the runtime that tweaks the generated code.

hishamhm commented 6 months ago

Hi! Thanks for looking at Teal :) @fperrad has also asked for this recently, and even sent a PR... At the time, I declined having this behavior be automatic, and wrote down my reasoning: https://github.com/teal-language/tl/pull/686

I'm not 100% opposed to making this into a compiler option (cautiously saying this while being afraid of opening Pandora's Box) but I still want Teal's default behavior to be as consistent as possible on every supported Lua version (within some pragmatic limits).

fperrad commented 6 months ago

At this time, with an unmodified tl, I just patch the generated files with a simple command sed -e 's/bit32/bit/g;'.

cattokomo commented 1 month ago

Maybe add bit checking along with bit32 checking in generated file, like this.

local bit32 = bit32; if not bit32 then local p, m = pcall(require, 'bit32'); if p then bit32 = m elseif p, m = pcall(require, 'bit'); if p then bit32 = m end end end; --...
creationix commented 3 weeks ago

I understand the fear of opening Pandora's box, but I'd really like to be able to have something like teal for luajit based runtimes. Lua is just too slow for the types of projects I work on and the main drawback of luajit is the lack of proper types in the tooling. Native teal support for luvit and openresty apps would be amazing!

Creating a competing teal-like project that targets luajit seems silly and a waste of effort. I hope there is a way to support luajit properly without causing undue burden on this project.

hishamhm commented 3 weeks ago

@creationix Thanks again for the feedback. I think a way out of this conundrum (and similar ones in general) will be to introduce compiler flags not only at the CLI level, but at the module-file level as special comments, like https://wiki.haskell.org/Language_Pragmas (related: https://github.com/seanbaxter/circle/blob/master/new-circle/README.md#versioning-with-feature-directives )

hishamhm commented 3 weeks ago

see https://github.com/teal-language/tl/discussions/751