Skip to content

Commit

Permalink
Fix lua String with no-inline (#11057)
Browse files Browse the repository at this point in the history
* Hide lua String inlines

* Patch string metatable

* Fix both inline modes

* Okay, real fix time
  • Loading branch information
RblSb authored Mar 30, 2023
1 parent 2f60b84 commit 341371d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/generators/genlua.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,9 @@ and gen_value ctx e =
gen_expr ctx e
| TMeta (_,e1) ->
gen_value ctx e1
| TCall ({eexpr = (TField (e, (FInstance _ as s)))}, el) when (is_string_expr e) ->
spr ctx ("String.prototype." ^ (field_name s));
gen_paren_arguments ctx (e :: el)
| TCall (e,el) ->
gen_call ctx e el
| TReturn _
Expand Down
14 changes: 7 additions & 7 deletions std/lua/_std/String.hx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class String {
public inline function toLowerCase():String
return BaseString.lower(this);

public inline function indexOf(str:String, ?startIndex:Int):Int {
public function indexOf(str:String, ?startIndex:Int):Int {
if (startIndex == null)
startIndex = 1;
else
Expand All @@ -86,7 +86,7 @@ class String {
return startIndex > length ? length : startIndex;
}

public inline function lastIndexOf(str:String, ?startIndex:Int):Int {
public function lastIndexOf(str:String, ?startIndex:Int):Int {
var ret = -1;
if (startIndex == null)
startIndex = length;
Expand All @@ -99,11 +99,11 @@ class String {
return ret;
}

public inline function split(delimiter:String):Array<String> {
var idx = 1;
public function split(delimiter:String):Array<String> {
var idx:Null<Int> = 1;
var ret = [];
while (idx != null) {
var newidx = 0;
var newidx:Null<Int> = 0;
if (delimiter.length > 0) {
newidx = BaseString.find(this, delimiter, idx, true).begin;
} else if (idx >= this.length) {
Expand All @@ -128,7 +128,7 @@ class String {
return this;
}

public inline function substring(startIndex:Int, ?endIndex:Int):String {
public function substring(startIndex:Int, ?endIndex:Int):String {
if (endIndex == null)
endIndex = this.length;
if (endIndex < 0)
Expand All @@ -151,7 +151,7 @@ class String {
return BaseString.byte(this, index + 1);
}

public inline function substr(pos:Int, ?len:Int):String {
public function substr(pos:Int, ?len:Int):String {
if (len == null || len > pos + this.length)
len = this.length;
else if (len < 0)
Expand Down
21 changes: 21 additions & 0 deletions tests/misc/lua/projects/Issue9530/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using StringTools;

@:nullSafety(Strict)
class Main {
static var f = "field";
static function main() {
final sclass = new String("foo");
final scl = sclass.toUpperCase();
trace(scl);
final f2 = f.toUpperCase();
trace(f2);
final str = "str".toUpperCase();
trace(str);
final isS = "sss".startsWith("s");
trace(isS);
// test internal static call
final i = "foo".indexOf("");
trace(i);
trace(("dyn" : Dynamic).toUpperCase());
}
}
4 changes: 4 additions & 0 deletions tests/misc/lua/projects/Issue9530/compile.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-main Main
-lua bin/test.lua
--cmd lua bin/test.lua
-D no-inline
6 changes: 6 additions & 0 deletions tests/misc/lua/projects/Issue9530/compile.hxml.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Main.hx:9: FOO
Main.hx:11: FIELD
Main.hx:13: STR
Main.hx:15: true
Main.hx:18: 0
Main.hx:19: DYN
3 changes: 2 additions & 1 deletion tests/unit/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{"label": "JavaScript", "args": ["compile-js.hxml", "-cmd", "node -e \"require('./bin/unit.js').unit.TestMain.main()\""]},
{"label": "JVM", "args": ["compile-jvm-only.hxml", "-cmd", "java -jar bin/unit.jar"]},
{"label": "Interp", "args": ["compile-macro.hxml"]},
{"label": "Lua", "args": ["compile-lua.hxml", "-cmd", "lua bin/unit.lua"]},
],
"[haxe]": {
"editor.formatOnSave": true,
Expand All @@ -16,4 +17,4 @@
"arguments": ["-v"]
},
"haxe.enableCompilationServer": false
}
}

0 comments on commit 341371d

Please sign in to comment.