Fix indexOf on C++ and C#

This commit is contained in:
2022-03-28 17:38:11 -06:00
parent 482020bf4f
commit f74124888f
2 changed files with 8 additions and 6 deletions

View File

@@ -1126,12 +1126,15 @@ class Macros {
var funcName = if (last) "lastIndexOf" else "indexOf"; var funcName = if (last) "lastIndexOf" else "indexOf";
wholeExp.checkNumArgs(2, 3, '($funcName <list or string> <element or substring> <?startingIndex>)'); wholeExp.checkNumArgs(2, 3, '($funcName <list or string> <element or substring> <?startingIndex>)');
var b = wholeExp.expBuilder(); var b = wholeExp.expBuilder();
return b.callSymbol("case", [ var cases = [
b.callField(funcName, exps.shift(), exps), b.callField(funcName, exps.shift(), exps),
b.callSymbol("-1", [b.symbol("haxe.ds.Option.None")]), b.callSymbol("-1", [b.symbol("haxe.ds.Option.None")]),
b.callSymbol("other", [b.callSymbol("haxe.ds.Option.Some", [b.symbol("other")])]), b.callSymbol("other", [b.callSymbol("haxe.ds.Option.Some", [b.symbol("other")])]),
b.callSymbol("null", [b.callSymbol("throw", [b.str("Haxe indexOf is broken")])]) ];
]); if (!(Context.defined('cs') || Context.defined('cpp'))) {
cases.push(b.callSymbol("null", [b.callSymbol("throw", [b.str("Haxe indexOf is broken")])]));
}
return b.callSymbol("case", cases);
} }
macros["indexOf"] = indexOfMacro.bind(false); macros["indexOf"] = indexOfMacro.bind(false);
macros["lastIndexOf"] = indexOfMacro.bind(true); macros["lastIndexOf"] = indexOfMacro.bind(true);

View File

@@ -306,10 +306,9 @@ class SpecialForms {
var exp = k.withoutListWrapping().convert(args[0]); var exp = k.withoutListWrapping().convert(args[0]);
// On C#, value types (specifically Float and Int) cannot be null, so they cannot be compared with null. // On C# and C++, value types (specifically Float and Int) cannot be null, so they cannot be compared with null.
// Therefore a null case doesn't need to be added--and will cause a compile failure if it is. // Therefore a null case doesn't need to be added--and will cause a compile failure if it is.
// TODO also c++? var canCompareNull = if (Context.defined('cs') || Context.defined('cpp')) {
var canCompareNull = if (Context.defined('cs')) {
// TODO can locals from let bindings and localVar be gathered and passed to this? Would be difficult and maybe require a separate stack in KissState for each (begin) conversion // TODO can locals from let bindings and localVar be gathered and passed to this? Would be difficult and maybe require a separate stack in KissState for each (begin) conversion
switch (exp.typeof()) { switch (exp.typeof()) {
case Success(TAbstract(ref, [])) if (["Int", "Float"].indexOf(ref.get().name) != -1): case Success(TAbstract(ref, [])) if (["Int", "Float"].indexOf(ref.get().name) != -1):