sjbarag / brs

An interpreter for the BrightScript language that runs on non-Roku platforms.
MIT License
113 stars 43 forks source link

bug(interp): brs doesn't automatically unbox parameters and return values #369

Open sjbarag opened 4 years ago

sjbarag commented 4 years ago

In #365 and #360 we realized that RBI automatically boxes function parameters and return values. As it turns out, RBI also unboxes function parameters and return values to match a function's declared signature. Consider this example:

sub main()
    boxed = createObject("roString")
    boxed.setString("lorem ipsum")
    print "type(boxed) = " type(boxed) " | boxed = " boxed

    result = unboxing(boxed)
    print "type(unboxed) = " type(unboxed) " | unboxed = " unboxed
end sub

sub unboxing(s as string) as string
    print "type(s) = " type(s) " | s = " s
    return s
end sub

Inside of the unboxing function, s is always a primitive string and never an instance of roString. Similarly, the return value of unboxing is also always a primitive string and never an instance of roString.

brs currently doesn't handle this demotion properly, and throws type mismatch errors at runtime 😢

lvcabral commented 3 years ago

Another scenario with this same unboxing issue can be reproduced this way: brs> a = CreateObject("roInt",1) brs> ? a+1 REPL(1,2-5): Attempting to add non-homogeneous values. left: Object right: Integer

In case of a comparison is even worse, as it does not return an error so it silently return "false" if you compare a "roInt" with value 1 and the constant 1 like this:

brs> a = CreateObject("roInt",1) brs> ? a 1 brs> ? a=1 false