rokucommunity / brs

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

Method chain calls causes Interpreter to trigger the methods multiple times #9

Closed lvcabral closed 10 months ago

lvcabral commented 12 months ago

In the line below, the method TotalSeconds is called 4 times (to check add a console.log in the implementations): ? CreateObject("roTimeSpan").TotalSeconds().ToStr().Trim()

User @strattonbrazil also reported:

This is also happening for similar chained calls such as:

" 3 ".trim().toInt()

where trim() is called twice. I also found this has always behaved this way and went unnoticed since most of the chained functions had little to no side effects and only their values were used in testing. I also confirmed that this seems specific to function chaining and other cases like 1 + 2 + 3 have the appropriate number of addition operations.

Using the example above, it appears the trim() call is evaluated once in Interpreter.visitDottedGet() and evaluated in Interpreter.visitCall(). While this has some performance concerns, this can certainly introduce more problems when more functions with side effects are introduced. I'll start looking at some options. I'm guessing the Interpreter.visitCall() is the appropriate place to do this evaluation and Interpreter.visitDottedGet() needs to be updated.

lvcabral commented 10 months ago

image

lvcabral commented 10 months ago

Addressed by #22