[lua] Fix closure on string method causing runtime error#12676
Open
jdonaldson wants to merge 1 commit intoHaxeFoundation:developmentfrom
Open
[lua] Fix closure on string method causing runtime error#12676jdonaldson wants to merge 1 commit intoHaxeFoundation:developmentfrom
jdonaldson wants to merge 1 commit intoHaxeFoundation:developmentfrom
Conversation
Member
|
I generally think this should be fixed in the run-time because whatever problem can occur with properly typed code can also occur when accessing it through |
21910e9 to
0db2d52
Compare
…ion#8068) Two fixes for extracting string methods as closures (e.g. f.charAt): 1. Generator: when creating a closure on a String-typed expression, look up the method from String.prototype instead of from the native Lua string, which doesn't have Haxe methods on its metatable. 2. Runtime: guard _hx_bind against non-table values (strings, numbers) by skipping the closure cache (rawset crashes on primitives) and returning a fresh bound closure. Covers Dynamic/untyped access.
0db2d52 to
cc1ef26
Compare
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
Fixes #8068 on the Lua target.
Extracting a method from a string as a closure (
var o = {charAt: f.charAt}) crashes at runtime because:Native Lua strings don't have Haxe methods on their metatable. The generator emits
_hx_bind(f, f.charAt), butf.charAtresolves tonilthrough Lua's default string metatable. Fixed by looking up the method fromString.prototypeinstead when the expression is String-typed._hx_bindcrashes on non-table primitives. It callsrawset(o, '_hx__closures', {})which fails on strings. Fixed by detecting non-table types and returning a fresh bound closure without caching. This covers theDynamic/untyped case where the generator can't know the type at compile time.Note: the JS target has the same
$bindissue (tries to sethx__closures__on a string primitive). This PR only addresses Lua.Test plan
Issue8068— extractscharAtfrom a string, calls it through an anonymous object