crypto icon indicating copy to clipboard operation
crypto copied to clipboard

Method chain calls causes Interpreter to trigger the methods multiple times

Open lvcabral opened this issue 6 years ago • 1 comments

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()

lvcabral avatar Nov 12 '19 18:11 lvcabral

Thanks for the report! I confirmed this was 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.

strattonbrazil avatar Jan 31 '20 05:01 strattonbrazil