scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

ScriptException.getLineNumber returns an inaccurate value #11209

Open lare96 opened 5 years ago

lare96 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

som-snytt commented 5 years ago

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