rokucommunity / brs

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

`brs` doesn't automatically unbox parameters and return values #18

Closed lvcabral closed 6 months ago

lvcabral commented 1 year ago

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

    unboxed = 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 1 year ago

Another scenario with this same unboxing issue can be reproduced this way:

a = CreateObject("roInt",1)
? a+1

causes an error:

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:

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