Surprising Nashorn behavior for String methods
Esben Andreasen
esben at esbena.dk
Sun Oct 16 20:50:58 UTC 2016
Hi
I am not sure if this is a bug or feature of the Nashorn engine. But
it looks like some String-prototype methods are missing, yet the
methods are somehow present when used in the right way.
Minimal example: calling `startsWith` by using `call`:
```
1 $ jjs -v
2 jjs> String.prototype.startsWith.call('abc', 'ab')
3 <shell>:1 TypeError: Cannot read property "call" from undefined
```
Expected behavior:
Not a type error, for multiple reasons:
1. Other JavaScript engines do not throw a type error.
2. The equivalent code `"abc".startsWith("ab")` does not throw a type error.
Further investigation:
```
1 $ jjs -v
2 nashorn 1.8.0_101
3 jjs> "abc".startsWith('ab')
4 true
5 jjs> "abc".startsWith
6 [jdk.internal.dynalink.beans.OverloadedDynamicMethod
boolean java.lang.String.startsWith(String,int)
boolean java.lang.String.startsWith(String)
]
7 jjs> "abc".startsWith.call("abc", "ab")
8 <shell>:1 TypeError: "abc".startsWith.call is not a function
9 jjs> typeof String.prototype.startsWith
10 undefined
11 jjs> String.prototype.split.call("abc", "b")
12 a,c
```
Interpretation:
It looks like `startsWith` is not present on the String-prototype, yet
it is present on the String-primitives. Other String-prototype methods
(`split`) are however present.
Further notes:
This also applies to `String.prototype.endsWith`.
Related bug/feature: primitive values are not wrapped properly:
```
1 $ jjs -v
2 jjs> typeof Object("abc").startsWith
3 undefined
```
More information about the nashorn-dev
mailing list