Html5 target problem - substr/charAt is not a function

Hello masters! My html5 target were successfully built before I’ve tried to use some simple String functions (substr and charAt). Now in my browser I’m getting an error that substr is not a function. Where is the problem may be? Api says that those functions are available on Java platform…

substr and charAt are both available for the js target.

http://try.haxe.org/#96221

Does this code works for you? (with and without openfl)

What version of haxe do you have?

I use Haxe 3.2.0… and recently I’ve updated OpenFL to version 3.3.8, maybe that somehow caused the problem…

Ok, now I see, it doesn’t work only at a specific place of the code.
Here is my code and it works on other targets:

    var str:String = "";
    
    for (i in 0...sd.collisionGroup.length) {
        str = sd.collisionGroup[i];
        
        if (str.substr(0, 1) == "0") {
            collisionGroup[i] = Utils.parseBinary(str);
        }else{
            collisionGroup[i] = Std.parseInt(str);
        }
    }

but on html5 I’m getting error TypeError: s.substr is not a function

Here is problem code:

static function substr( s : String, pos : Int, ?len : Int ) : String {
    if( pos != null && pos != 0 && len != null && len < 0 ) return "";
    if( len == null ) len = s.length;
    if( pos < 0 ){
        pos = s.length + pos;
        if( pos < 0 ) pos = 0;
    }else if( len < 0 ){
        len = s.length + len - pos;
    }

    return (untyped s).substr(pos, len); //---------- ERROR HERE
}

Ok, the problem strangely solved.

Here is my new code that works:

    var str:String = "";
    
    for (i in 0...sd.collisionGroup.length) {
        str = sd.collisionGroup[i].toString();
        
        if (str.substr(0, 1) == "0") {
            collisionGroup[i] = Utils.parseBinary(str);
        }else{
            collisionGroup[i] = Std.parseInt(str);
        }
    }

The strange thing is that I didn’t get any type errors, and collisionGroup were declared as
public var collisionGroup:Array"String" = [“1”, “1”, “1”];
with a strict type. And also there are no problems on other targets… so it’s a mystery of compilation…
Case closed)

Perhaps its because you used untyped?

Not exactly, my str was declared with a type String and sd.collisionGroup also a String, but after a compilation, html5 marked str as untyped…
I suppose to sd.collisionGroup were assigned Integer values, but why compiler didn’t give me a type error is a mystery…

I mean, what if this is just return s.substr(pos, len);?

That’s not my code, it’s what I got from already compiled JavaScript (Chrome developer tools). If you look above there is two examples with my code and compiled Java… Sorry if I explained it in a such confused way…

The untyped is from the std https://github.com/HaxeFoundation/haxe/blob/development/std/js/_std/HxOverrides.hx#L69-L88
and it’s probably required to avoid an infinite loop call.

@Monz You should open an issue on the haxe bug tracker https://github.com/HaxeFoundation/haxe/issues