bug icon indicating copy to clipboard operation
bug copied to clipboard

ScriptException.getLineNumber returns an inaccurate value

Open lare96 opened this issue 7 years ago • 3 comments

Scala version: 2.12.6 Java version: 8

I have a series of scripts that are evaluated with the Scala ScriptEngine like so

for (Script script : plugin.getScripts()) {
    try {
        engine.eval(script.getContents());
    } catch (ScriptException e) {
        throw new ScriptInterpretException(script, e);
    }
    ...
}

When an exception is thrown, the line number that it returns is inaccurate. For example, take this script

import io.luna.game.event.impl.ServerLaunchEvent
import io.luna.game.model.mob.Player

private val TICK_INTERVAL = 1500 // 15 mins

private val MESSAGES = Vector(
  "Luna is a Runescape private server for the #317 protocol.",
  "Luna can be found on GitHub under luna-rs/luna",
  "Change these messages in /plugins/world/announcements/announcements.scala",
  "Any bugs found using Luna should be reported to the GitHub page."
)

private val FILTER = (plr: Player) => plr.rights <= RIGHTS_ADMIN


on[ServerLaunchEvent] { msg =>
  world.scheduleForever(TICK_INTERVAL, error here!!!) { // error, line 21 
    world.players.
      filter(FILTER).
      foreach { _.sendMessage(pick(MESSAGES)) }
  }
}

getLineNumber should return 21 but instead it returns 221. In this instance, you could subtract 200 to get the line number but normally the value is unpredictable (ranges between 150-300). Could this be because the line number counter isn't being reset after each script evaluation? If so, is this the intended behavior?

lare96 avatar Oct 15 '18 23:10 lare96

To test if the counter isn't being reset I'll make my own line counter that records the amount of lines of each successful evaluated script. Then I'll just subtract the getLineNumber value by my counter and if the result is the correct line number then there's the problem.

lare96 avatar Oct 16 '18 00:10 lare96

My original thought about why this function doesn't work has been ruled out. Not sure where to go from here

lare96 avatar Oct 16 '18 00:10 lare96

I think the underlying REPL tries to use the length of the header to compute offsets, but maybe something is different in scripted usage. You can supply -Xprint:parser to see what is actually submitted for compilation. (Scripted.apply takes settings.)

som-snytt avatar Oct 19 '18 18:10 som-snytt