Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invokeFunction does not preserve numeric global variable types #3710

Closed
mtakikawa opened this issue Mar 4, 2016 · 1 comment
Closed

invokeFunction does not preserve numeric global variable types #3710

mtakikawa opened this issue Mar 4, 2016 · 1 comment

Comments

@mtakikawa
Copy link

Environment

  • JRuby version: jruby 9.0.5.0 (2.2.3) 2016-01-26 7bee00d Java HotSpot(TM) 64-Bit Server VM 25.20-b23 on 1.8.0_20-b26 +jit [darwin-x86_64], but the same bug appeared in older JRuby's too.
  • OS: Does not matter (at least OSX 10.11.3 and Oracle Linux 7)

Expected Behavior and Actual Behavior

We expect all global variable values are preserved between multiple invocations of invokeFunction from Java. However, when a numeric global variable is initialized by a Fixnum, it will always become a Fixnum even if we assign a Float to it. Because of that, the assigned values will lose fractional values.

Here is an example:

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class JRubyTest {
  public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByExtension("rb");
    Invocable invocableEngine = (Invocable)engine;

    engine.eval(
      "$var1 = 0\n" +
      "$var2 = 0.0\n" +
      "def fn\n" +
      "  puts \"before updating: var1=#{$var1} var2=#{$var2}\"\n" +
      "  $var1 += 0.2; $var2 += 0.2\n" +
      "  puts \"after updating: var1=#{$var1} var2=#{$var2}\"\n" +
      "end\n");

    invocableEngine.invokeFunction("fn");
    invocableEngine.invokeFunction("fn");
  }

and the output:

before updating: var1=0 var2=0.0
after updating: var1=0.2 var2=0.2
before updating: var1=0 var2=0.2
after updating: var1=0.2 var2=0.4

As you can see, var2 (which is initialized by a Float 0.0) is updated correctly, but var1 (which is initialized by a Fixnum 0) will lose its fractional value at each iteration, which is wrong.

@headius
Copy link
Member

headius commented Apr 12, 2019

This appears to have been fixed at some point. We'll say 9.2.7 since that's the latest release, but it probably was fixed earlier.

@headius headius closed this as completed Apr 12, 2019
@headius headius added this to the JRuby 9.2.7.0 milestone Apr 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants