sjbarag / brs

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

Issue #416 toString() method not printing string fields with quotes #640

Closed ystarangl closed 3 years ago

ystarangl commented 3 years ago

Fixes #416

It's my first PR here -- please be gentle with me. 😅

OK, so Roku doesn't add quotes to a string only in 1 case: if we're printing a simple string variable:

stringVar = "Test string"
? "String variable:"
? stringVar

Output:

String variable:
Test string

I all other cases, Roku adds quotes. Whether it's an array, AA, list, or field of a node:

arrayWithString = ["Test string"]
? "Array with string:"
? arrayWithString
?

assocArrayWithString = {
    entry: "Test string"
}
? "AA with string:"
? assocArrayWithString
?

listWithString = CreateObject("roList")
listWithString.addTail("Test string")
? "List with string:"
? listWithString
? "type("Chr(34)"Test string"Chr(34)"): "type("Test string")
? "type(listWithString[0]) (!): "type(listWithString[0])
?

nodeWithString = CreateObject("roSGNode", "Node")
nodeWithString.addFields({ stringField: "Test string" })
? "Node with string:"
? nodeWithString

Output:

Array with string:
<Component: roArray> =
[
    "Test string"
]

AA with string:
<Component: roAssociativeArray> =
{
    entry: "Test string"
}

List with string:
<Component: roList> =
(
    "Test string"
)
type("Test string"): String
type(listWithString[0]) (!): roString

Node with string:
<Component: roSGNode:Node> =
{
    change: <Component: roAssociativeArray>
    focusable: false
    focusedChild: <Component: roInvalid>
    id: ""
    stringfield: "Test string"
}

What this PR changes:

  1. Tests have been updated with the fixed output of the strings.
  2. BrsString toString(...) function output now depends on parent existence. toString(...) gets parent only in case this string has any kind of container (array, AA, node etc.). This perfectly correspond to Roku's behavior.

I've also made roString pass parent to its internal BrsString. List boxes strings into roStrings, so this is needed for proper string print.

ystarangl commented 3 years ago

@Vasya-M roString works as expected -- same as unboxed string.