feat: expose Crystal procs as Lua global functions#44
Merged
Conversation
Adds `Stack#function(name, proc)` so Crystal callbacks can be called from Lua. Argument and return types are taken from the proc's signature; closures, method pointers, and proc literals all work. Closes #15.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #38 and #15
Adds
Stack#function(name, proc)so Crystal callbacks can be invoked from Lua. Argument and return types come from the proc's signature, so values flow naturally between the two languages.Closes #15.
Design
Proc(*Args, R) forall Args, Rso types are picked up at compile time without macros at the call site.LuaCallable::LuaConvert(T).convertfor Lua→Crystal coercion andpushclosure(viaset_global) for closure-data upvalues — so closures over locals just work.Nilreturn type → no Lua return value; otherwise the result is pushed and1is returned to Lua.ArgumentErrorif Lua passes too few args (silentnilsubstitution was the easiest footgun).Test plan
crystal spec— 144 examples, 0 failurescrystal run examples/crystal_callback.cr— outputs7.0,counter = 3,Hi, Lua,requests:5:true (30.0)crystal tool formatcleanFollow-ups (out of scope)
LuaCallable._lua_callinsrc/lua/callable.cr— both do the same arg-extract / call / push pattern. Worth extracting into a shared macro once the surface settles.open_libs+set_error_handleron every Lua→Crystal callback: same pre-existing pattern is incallable.crat four call sites; a dedicated lightweightStackview would fix all of them at once.LuaCallable::LuaConverttoLua::Convertso it's not reached throughLuaCallable's namespace.