-
Notifications
You must be signed in to change notification settings - Fork 866
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
Double/Integer in javascript body #350
Comments
ECMAScript spec only governs effects/behavior observable inside of the ECMAScript code. As far as I know, most ES implementations use an optimistic assumption that all numbers are regular 32-bit int-s, until they have to fallback to doubles. I assume that in this case the difference you get is a results of similar optimization done by Rhino.
I'm not sure I understand what do you mean here, but first, what is the problem you're trying to solve? If you want to always have doubles instead of int-s in the generated code, I don't think it's possible; even in the ES code some operations only work with integers. |
The optimization level may have an effect on this -- there are certain integer optimizations that only take affect at optimization level 9. |
Sorry for late response. Use case I'm trying to apply is pretty complicated. But let me simplify it. So let us assume that we have a simple java class that is receiving map of arguments passed from javascript code (as map) and relying on parameter types it's launching correct handler. I have written simple java class that is simple receiving map of arguments and printing types to console. It will look like:
Now I have created three test cases and only in one everything is working fine.
As a result I'm receiving:
And in my opinion it's correct behavior as all "primitive" types in javascript are float with double precision.
Here as a result we are receiving:
In my opinion "arg1" should also be treated as Double and it's an incorrect behavior.
Here we are receiving same result as in case 2.
I would expect javascript engine at least to behave the same in all cases. However I think in all cases it should always return java.lang.Double for "primitive" number type. |
Here I was thinking about similar approach that we can find in java. I know that for javascript we are not able to define type for numbers but maybe rhino is allowing for something like:
Thanks for that hint. I've checked all optimization levels from -1 to 9. And for level -1 I'm receiving always Double from javascript (just as in case 1 presented above). Unfortunately I cannot afford lower runtime performance with interpretive mode turn on. I really feel that generating compiled bytecode classes for selected javascript makes difference.
|
Thanks for the examples! First of all, I think your second and third cases are essentially the same: we're creating an object and passing it to the Now, the type conversion of arguments that are passed to Java methods from JavaScript seems to be done via call to
I'm not sure I agree. The cases are different after all, even if that's not obvious. It may be possible to achieve consistency by making |
I'm working with rhino library:
To operate on numbers in javascript. Here I have a sample code:
And what suprised me is the fact that rhino is returning Integer, as according to ECMAScript specification all "primitive" javascript Number types should be float with double precision. What's even more interesting is the fact that changing script to:
String script = "var x = 1.0; x;";
Will return a double. So results are not consistent if no decimal values are provided.
And I think it's all due to implementation. Problem seems to be connected with class org.mozilla.javascript.optimizer.Codegen and method
pushNumberAsObject(ClassFileWriter cfw, double num)
For 0.0, 1.0, -1.0 we are using some predefined statics. For other we are creating static constants with type defined by method
String getStaticConstantWrapperType(double num)
And this method for number 2.0 will always return "Ljava/lang/Integer" object. Is it correct behavior? What's the reason for keeping such strange behavior?
And if it is something that cannot be resolved, can I somehow give a bytecode node additional number property?
I also verified this issue for latest released library 1.7-R5 and 1.7.7.2 but still I'm getting same behavior.
The text was updated successfully, but these errors were encountered: