Thu, 29 Aug 2013 16:34:31 -0700
Merge
1.1 --- a/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/ConstructorGenerator.java Thu Aug 29 09:42:13 2013 -0700 1.2 +++ b/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/ConstructorGenerator.java Thu Aug 29 16:34:31 2013 -0700 1.3 @@ -27,7 +27,6 @@ 1.4 1.5 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_FINAL; 1.6 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PUBLIC; 1.7 -import static jdk.internal.org.objectweb.asm.Opcodes.ACC_SUPER; 1.8 import static jdk.internal.org.objectweb.asm.Opcodes.H_INVOKESTATIC; 1.9 import static jdk.internal.org.objectweb.asm.Opcodes.V1_7; 1.10 import static jdk.nashorn.internal.tools.nasgen.StringConstants.CONSTRUCTOR_SUFFIX;
2.1 --- a/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/ScriptClassInstrumentor.java Thu Aug 29 09:42:13 2013 -0700 2.2 +++ b/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/ScriptClassInstrumentor.java Thu Aug 29 16:34:31 2013 -0700 2.3 @@ -27,7 +27,6 @@ 2.4 2.5 import static jdk.internal.org.objectweb.asm.Opcodes.ALOAD; 2.6 import static jdk.internal.org.objectweb.asm.Opcodes.DUP; 2.7 -import static jdk.internal.org.objectweb.asm.Opcodes.GETSTATIC; 2.8 import static jdk.internal.org.objectweb.asm.Opcodes.INVOKESPECIAL; 2.9 import static jdk.internal.org.objectweb.asm.Opcodes.INVOKESTATIC; 2.10 import static jdk.internal.org.objectweb.asm.Opcodes.NEW;
3.1 --- a/docs/DEVELOPER_README Thu Aug 29 09:42:13 2013 -0700 3.2 +++ b/docs/DEVELOPER_README Thu Aug 29 16:34:31 2013 -0700 3.3 @@ -16,8 +16,9 @@ 3.4 SYSTEM PROPERTY: -Dnashorn.args=<string> 3.5 3.6 This property takes as its value a space separated list of Nashorn 3.7 -command line options that should be passed to Nashorn. This might be useful 3.8 -in environments where it is hard to tell how a nashorn.jar is launched. 3.9 +command line options that should be passed to Nashorn. This might be 3.10 +useful in environments where it is hard to tell how a nashorn.jar is 3.11 +launched. 3.12 3.13 Example: 3.14 3.15 @@ -43,6 +44,10 @@ 3.16 3.17 SYSTEM PROPERTY: -Dnashorn.compiler.intarithmetic 3.18 3.19 +(and integer arithmetic in general) 3.20 + 3.21 +<currently disabled - this is being refactored for update releases> 3.22 + 3.23 Arithmetic operations in Nashorn (except bitwise ones) typically 3.24 coerce the operands to doubles (as per the JavaScript spec). To switch 3.25 this off and remain in integer mode, for example for "var x = a&b; var 3.26 @@ -65,13 +70,382 @@ 3.27 does not overflow. Getting access to a JVM intrinsic that does branch 3.28 on overflow would probably alleviate this. 3.29 3.30 -There is also a problem with this optimistic approach if the symbol 3.31 -happens to reside in a local variable slot in the bytecode, as those 3.32 -are strongly typed. Then we would need to split large sections of 3.33 -control flow, so this is probably not the right way to go, while range 3.34 -analysis is. There is a large difference between integer bytecode 3.35 -without overflow checks and double bytecode. The former is 3.36 -significantly faster. 3.37 +The future: 3.38 + 3.39 +We are transitioning to an optimistic type system that uses int 3.40 +arithmetic everywhere until proven wrong. The problem here is mostly 3.41 +catch an overflow exception and rolling back the state to a new method 3.42 +with less optimistic assumptions for an operation at a particular 3.43 +program point. This will most likely not be in the Java 8.0 release 3.44 +but likely end up in an update release 3.45 + 3.46 +For Java 8, several java.lang.Math methods like addExact, subExact and 3.47 +mulExact are available to help us. Experiments intrinsifying these 3.48 +show a lot of promise, and we have devised a system that basically 3.49 +does on stack replacement with exceptions in bytecode to revert 3.50 +erroneous assumptions. An explanation of how this works and what we 3.51 +are doing can be found here: 3.52 +http://www.slideshare.net/lagergren/lagergren-jvmls2013final 3.53 + 3.54 +Experiments with this show significant ~x2-3 performance increases on 3.55 +pretty much everything, provided that optimistic assumptions don't 3.56 +fail much. It will affect warmup time negatively, depending on how 3.57 +many erroneous too optimistic assumptions are placed in the code at 3.58 +compile time. We don't think this will be much of an issue. 3.59 + 3.60 +For example for a small benchmark that repeatedly executes this 3.61 +method taken from the Crypto Octane benchmark 3.62 + 3.63 +function am3(i,x,w,j,c,n) { 3.64 + var this_array = this.array; 3.65 + var w_array = w.array; 3.66 + var xl = x&0x3fff, xh = x>>14; 3.67 + while(--n >= 0) { 3.68 + var l = this_array[i]&0x3fff; 3.69 + var h = this_array[i++]>>14; 3.70 + var m = xh*l+h*xl; 3.71 + l = xl*l+((m&0x3fff)<<14)+w_array[j]+c; 3.72 + c = (l>>28)+(m>>14)+xh*h; 3.73 + w_array[j++] = l&0xfffffff; 3.74 + } 3.75 + 3.76 + return c; 3.77 +} 3.78 + 3.79 +The performance increase more than doubles. We are also working hard 3.80 +with the code generation team in the Java Virtual Machine to fix 3.81 +things that are lacking in invokedynamic performance, which is another 3.82 +area where a lot of ongoing performance work takes place 3.83 + 3.84 +"Pessimistic" bytecode for am3, guaranteed to be semantically correct: 3.85 + 3.86 +// access flags 0x9 3.87 + public static am3(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; 3.88 + L0 3.89 + LINENUMBER 12 L0 3.90 + ALOAD 0 3.91 + INVOKEDYNAMIC dyn:getProp|getElem|getMethod:array(Ljava/lang/Object;)Ljava/lang/Object; [ 3.92 + // handle kind 0x6 : INVOKESTATIC 3.93 + jdk/nashorn/internal/runtime/linker/Bootstrap.bootstrap((Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;I)Ljava/lang/invoke/CallSite;) 3.94 + // arguments: 3.95 + 0 3.96 + ] 3.97 + ASTORE 8 3.98 + L1 3.99 + LINENUMBER 13 L1 3.100 + ALOAD 3 3.101 + INVOKEDYNAMIC dyn:getProp|getElem|getMethod:array(Ljava/lang/Object;)Ljava/lang/Object; [ 3.102 + // handle kind 0x6 : INVOKESTATIC 3.103 + jdk/nashorn/internal/runtime/linker/Bootstrap.bootstrap((Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;I)Ljava/lang/invoke/CallSite;) 3.104 + // arguments: 3.105 + 0 3.106 + ] 3.107 + ASTORE 9 3.108 + L2 3.109 + LINENUMBER 14 L2 3.110 + ALOAD 2 3.111 + INVOKESTATIC jdk/nashorn/internal/runtime/JSType.toInt32 (Ljava/lang/Object;)I 3.112 + SIPUSH 16383 3.113 + IAND 3.114 + ISTORE 10 3.115 + ALOAD 2 3.116 + INVOKESTATIC jdk/nashorn/internal/runtime/JSType.toInt32 (Ljava/lang/Object;)I 3.117 + BIPUSH 14 3.118 + ISHR 3.119 + ISTORE 11 3.120 + L3 3.121 + LINENUMBER 15 L3 3.122 + GOTO L4 3.123 + L5 3.124 + LINENUMBER 16 L5 3.125 + FRAME FULL [java/lang/Object java/lang/Object java/lang/Object java/lang/Object java/lang/Object java/lang/Object java/lang/Double T java/lang/Object java/lang/Object I I] [] 3.126 + ALOAD 8 3.127 + ALOAD 1 3.128 + INVOKEDYNAMIC dyn:getElem|getProp|getMethod(Ljava/lang/Object;Ljava/lang/Object;)I [ 3.129 + // handle kind 0x6 : INVOKESTATIC 3.130 + jdk/nashorn/internal/runtime/linker/Bootstrap.bootstrap((Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;I)Ljava/lang/invoke/CallSite;) 3.131 + // arguments: 3.132 + 0 3.133 + ] 3.134 + SIPUSH 16383 3.135 + IAND 3.136 + INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer; 3.137 + ASTORE 12 3.138 + L6 3.139 + LINENUMBER 17 L6 3.140 + ALOAD 8 3.141 + ALOAD 1 3.142 + INVOKESTATIC jdk/nashorn/internal/runtime/JSType.toNumber (Ljava/lang/Object;)D 3.143 + DUP2 3.144 + DCONST_1 3.145 + DADD 3.146 + INVOKESTATIC java/lang/Double.valueOf (D)Ljava/lang/Double; 3.147 + ASTORE 1 3.148 + INVOKEDYNAMIC dyn:getElem|getProp|getMethod(Ljava/lang/Object;D)I [ 3.149 + // handle kind 0x6 : INVOKESTATIC 3.150 + jdk/nashorn/internal/runtime/linker/Bootstrap.bootstrap((Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;I)Ljava/lang/invoke/CallSite;) 3.151 + // arguments: 3.152 + 0 3.153 + ] 3.154 + BIPUSH 14 3.155 + ISHR 3.156 + ISTORE 13 3.157 + L7 3.158 + LINENUMBER 18 L7 3.159 + ILOAD 11 3.160 + I2D 3.161 + ALOAD 12 3.162 + INVOKESTATIC jdk/nashorn/internal/runtime/JSType.toNumber (Ljava/lang/Object;)D 3.163 + DMUL 3.164 + ILOAD 13 3.165 + I2D 3.166 + ILOAD 10 3.167 + I2D 3.168 + DMUL 3.169 + DADD 3.170 + DSTORE 14 3.171 + L8 3.172 + LINENUMBER 19 L8 3.173 + ILOAD 10 3.174 + I2D 3.175 + ALOAD 12 3.176 + INVOKESTATIC jdk/nashorn/internal/runtime/JSType.toNumber (Ljava/lang/Object;)D 3.177 + DMUL 3.178 + DLOAD 14 3.179 + INVOKESTATIC jdk/nashorn/internal/runtime/JSType.toInt32 (D)I 3.180 + SIPUSH 16383 3.181 + IAND 3.182 + BIPUSH 14 3.183 + ISHL 3.184 + I2D 3.185 + DADD 3.186 + ALOAD 9 3.187 + ALOAD 4 3.188 + INVOKEDYNAMIC dyn:getElem|getProp|getMethod(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; [ 3.189 + // handle kind 0x6 : INVOKESTATIC 3.190 + jdk/nashorn/internal/runtime/linker/Bootstrap.bootstrap((Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;I)Ljava/lang/invoke/CallSite;) 3.191 + // arguments: 3.192 + 0 3.193 + ] 3.194 + INVOKEDYNAMIC ADD:ODO_D(DLjava/lang/Object;)Ljava/lang/Object; [ 3.195 + // handle kind 0x6 : INVOKESTATIC 3.196 + jdk/nashorn/internal/runtime/linker/Bootstrap.runtimeBootstrap((Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;) 3.197 + // arguments: none 3.198 + ] 3.199 + ALOAD 5 3.200 + INVOKEDYNAMIC ADD:OOO_I(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; [ 3.201 + // handle kind 0x6 : INVOKESTATIC 3.202 + jdk/nashorn/internal/runtime/linker/Bootstrap.runtimeBootstrap((Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;) 3.203 + // arguments: none 3.204 + ] 3.205 + ASTORE 12 3.206 + L9 3.207 + LINENUMBER 20 L9 3.208 + ALOAD 12 3.209 + INVOKESTATIC jdk/nashorn/internal/runtime/JSType.toInt32 (Ljava/lang/Object;)I 3.210 + BIPUSH 28 3.211 + ISHR 3.212 + I2D 3.213 + DLOAD 14 3.214 + INVOKESTATIC jdk/nashorn/internal/runtime/JSType.toInt32 (D)I 3.215 + BIPUSH 14 3.216 + ISHR 3.217 + I2D 3.218 + DADD 3.219 + ILOAD 11 3.220 + I2D 3.221 + ILOAD 13 3.222 + I2D 3.223 + DMUL 3.224 + DADD 3.225 + INVOKESTATIC java/lang/Double.valueOf (D)Ljava/lang/Double; 3.226 + ASTORE 5 3.227 + L10 3.228 + LINENUMBER 21 L10 3.229 + ALOAD 9 3.230 + ALOAD 4 3.231 + INVOKESTATIC jdk/nashorn/internal/runtime/JSType.toNumber (Ljava/lang/Object;)D 3.232 + DUP2 3.233 + DCONST_1 3.234 + DADD 3.235 + INVOKESTATIC java/lang/Double.valueOf (D)Ljava/lang/Double; 3.236 + ASTORE 4 3.237 + ALOAD 12 3.238 + INVOKESTATIC jdk/nashorn/internal/runtime/JSType.toInt32 (Ljava/lang/Object;)I 3.239 + LDC 268435455 3.240 + IAND 3.241 + INVOKEDYNAMIC dyn:setElem|setProp(Ljava/lang/Object;DI)V [ 3.242 + // handle kind 0x6 : INVOKESTATIC 3.243 + jdk/nashorn/internal/runtime/linker/Bootstrap.bootstrap((Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;I)Ljava/lang/invoke/CallSite;) 3.244 + // arguments: 3.245 + 0 3.246 + ] 3.247 + L4 3.248 + FRAME FULL [java/lang/Object java/lang/Object java/lang/Object java/lang/Object java/lang/Object java/lang/Object java/lang/Object T java/lang/Object java/lang/Object I I] [] 3.249 + ALOAD 6 3.250 + INVOKESTATIC jdk/nashorn/internal/runtime/JSType.toNumber (Ljava/lang/Object;)D 3.251 + LDC -1.0 3.252 + DADD 3.253 + DUP2 3.254 + INVOKESTATIC java/lang/Double.valueOf (D)Ljava/lang/Double; 3.255 + ASTORE 6 3.256 + DCONST_0 3.257 + DCMPL 3.258 + IFGE L5 3.259 + L11 3.260 + LINENUMBER 24 L11 3.261 + ALOAD 5 3.262 + ARETURN 3.263 + 3.264 +"Optimistic" bytecode that requires invalidation on e.g overflow. Factor 3.265 +x2-3 speedup: 3.266 + 3.267 +public static am3(Ljava/lang/Object;IILjava/lang/Object;III)I 3.268 + L0 3.269 + LINENUMBER 12 L0 3.270 + ALOAD 0 3.271 + INVOKEDYNAMIC dyn:getProp|getElem|getMethod:array(Ljava/lang/Object;)Ljava/lang/Object; [ 3.272 + // handle kind 0x6 : INVOKESTATIC 3.273 + jdk/nashorn/internal/runtime/linker/Bootstrap.bootstrap((Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;I)Ljava/lang/invoke/CallSite;) 3.274 + // arguments: 3.275 + 0 3.276 + ] 3.277 + ASTORE 8 3.278 + L1 3.279 + LINENUMBER 13 L1 3.280 + ALOAD 3 3.281 + INVOKEDYNAMIC dyn:getProp|getElem|getMethod:array(Ljava/lang/Object;)Ljava/lang/Object; [ 3.282 + // handle kind 0x6 : INVOKESTATIC 3.283 + jdk/nashorn/internal/runtime/linker/Bootstrap.bootstrap((Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;I)Ljava/lang/invoke/CallSite;) 3.284 + // arguments: 3.285 + 0 3.286 + ] 3.287 + ASTORE 9 3.288 + L2 3.289 + LINENUMBER 14 L2 3.290 + ILOAD 2 3.291 + SIPUSH 16383 3.292 + IAND 3.293 + ISTORE 10 3.294 + ILOAD 2 3.295 + BIPUSH 14 3.296 + ISHR 3.297 + ISTORE 11 3.298 + L3 3.299 + LINENUMBER 15 L3 3.300 + GOTO L4 3.301 + L5 3.302 + LINENUMBER 16 L5 3.303 + FRAME FULL [java/lang/Object I I java/lang/Object I I I T java/lang/Object java/lang/Object I I] [] 3.304 + ALOAD 8 3.305 + ILOAD 1 3.306 + INVOKEDYNAMIC dyn:getElem|getProp|getMethod(Ljava/lang/Object;I)I [ 3.307 + // handle kind 0x6 : INVOKESTATIC 3.308 + jdk/nashorn/internal/runtime/linker/Bootstrap.bootstrap((Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;I)Ljava/lang/invoke/CallSite;) 3.309 + // arguments: 3.310 + 0 3.311 + ] 3.312 + SIPUSH 16383 3.313 + IAND 3.314 + ISTORE 12 3.315 + L6 3.316 + LINENUMBER 17 L6 3.317 + ALOAD 8 3.318 + ILOAD 1 3.319 + DUP 3.320 + ICONST_1 3.321 + IADD 3.322 + ISTORE 1 3.323 + INVOKEDYNAMIC dyn:getElem|getProp|getMethod(Ljava/lang/Object;I)I [ 3.324 + // handle kind 0x6 : INVOKESTATIC 3.325 + jdk/nashorn/internal/runtime/linker/Bootstrap.bootstrap((Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;I)Ljava/lang/invoke/CallSite;) 3.326 + // arguments: 3.327 + 0 3.328 + ] 3.329 + BIPUSH 14 3.330 + ISHR 3.331 + ISTORE 13 3.332 + L7 3.333 + LINENUMBER 18 L7 3.334 + ILOAD 11 3.335 + ILOAD 12 3.336 + BIPUSH 8 3.337 + INVOKESTATIC jdk/nashorn/internal/runtime/JSType.mulExact (III)I 3.338 + ILOAD 13 3.339 + ILOAD 10 3.340 + BIPUSH 9 3.341 + INVOKESTATIC jdk/nashorn/internal/runtime/JSType.mulExact (III)I 3.342 + IADD 3.343 + ISTORE 14 3.344 + L8 3.345 + LINENUMBER 19 L8 3.346 + ILOAD 10 3.347 + ILOAD 12 3.348 + BIPUSH 11 3.349 + INVOKESTATIC jdk/nashorn/internal/runtime/JSType.mulExact (III)I 3.350 + ILOAD 14 3.351 + SIPUSH 16383 3.352 + IAND 3.353 + BIPUSH 14 3.354 + ISHL 3.355 + IADD 3.356 + ALOAD 9 3.357 + ILOAD 4 3.358 + INVOKEDYNAMIC dyn:getElem|getProp|getMethod(Ljava/lang/Object;I)I [ 3.359 + // handle kind 0x6 : INVOKESTATIC 3.360 + jdk/nashorn/internal/runtime/linker/Bootstrap.bootstrap((Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;I)Ljava/lang/invoke/CallSite;) 3.361 + // arguments: 3.362 + 0 3.363 + ] 3.364 + IADD 3.365 + ILOAD 5 3.366 + IADD 3.367 + ISTORE 12 3.368 + L9 3.369 + LINENUMBER 20 L9 3.370 + ILOAD 12 3.371 + BIPUSH 28 3.372 + ISHR 3.373 + ILOAD 14 3.374 + BIPUSH 14 3.375 + ISHR 3.376 + IADD 3.377 + ILOAD 11 3.378 + ILOAD 13 3.379 + BIPUSH 21 3.380 + INVOKESTATIC jdk/nashorn/internal/runtime/JSType.mulExact (III)I 3.381 + IADD 3.382 + ISTORE 5 3.383 + L10 3.384 + LINENUMBER 21 L10 3.385 + ALOAD 9 3.386 + ILOAD 4 3.387 + DUP 3.388 + ICONST_1 3.389 + IADD 3.390 + ISTORE 4 3.391 + ILOAD 12 3.392 + LDC 268435455 3.393 + IAND 3.394 + INVOKEDYNAMIC dyn:setElem|setProp(Ljava/lang/Object;II)V [ 3.395 + // handle kind 0x6 : INVOKESTATIC 3.396 + jdk/nashorn/internal/runtime/linker/Bootstrap.bootstrap((Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;I)Ljava/lang/invoke/CallSite;) 3.397 + // arguments: 3.398 + 0 3.399 + ] 3.400 + L4 3.401 + FRAME SAME 3.402 + ILOAD 6 3.403 + ICONST_M1 3.404 + IADD 3.405 + DUP 3.406 + ISTORE 6 3.407 + ICONST_0 3.408 + IF_ICMPGE L5 3.409 + L11 3.410 + LINENUMBER 24 L11 3.411 + ILOAD 5 3.412 + IRETURN 3.413 3.414 3.415 SYSTEM PROPERTY: -Dnashorn.codegen.debug, -Dnashorn.codegen.debug.trace=<x> 3.416 @@ -167,7 +541,7 @@ 3.417 of byte code (try it e.g. on all the variants of am3 in the Octane 3.418 benchmark crypto.js). Thus, this needs to be lazy 3.419 3.420 -3) Possibly optimistic callsite writes, something on the form 3.421 +3) Optimistic callsite writes, something on the form 3.422 3.423 x = y; //x is a field known to be a primitive. y is only an object as 3.424 far as we can tell 3.425 @@ -189,6 +563,12 @@ 3.426 We still have to deal with objects vs primitives for local bytecode 3.427 slots, possibly through code copying and versioning. 3.428 3.429 +The Future: 3.430 + 3.431 +We expect the usefulness of dual fields to increase significantly 3.432 +after the optimistic type system described in the section on 3.433 +integer arithmetic above is implemented. 3.434 + 3.435 3.436 SYSTEM PROPERTY: -Dnashorn.compiler.symbol.trace=[<x>[,*]], 3.437 -Dnashorn.compiler.symbol.stacktrace=[<x>[,*]] 3.438 @@ -211,7 +591,7 @@ 3.439 semantics. 3.440 3.441 3.442 -SYSTEM PROPERTY: nashorn.lexer.xmlliterals 3.443 +SYSTEM PROPERTY: -Dnashorn.lexer.xmlliterals 3.444 3.445 If this property it set, it means that the Lexer should attempt to 3.446 parse XML literals, which would otherwise generate syntax 3.447 @@ -222,7 +602,7 @@ 3.448 the IR. 3.449 3.450 3.451 -SYSTEM_PROPERTY: nashorn.debug 3.452 +SYSTEM_PROPERTY: -Dnashorn.debug 3.453 3.454 If this property is set to true, Nashorn runs in Debug mode. Debug 3.455 mode is slightly slower, as for example statistics counters are enabled 3.456 @@ -269,8 +649,8 @@ 3.457 object layout being invalidated. 3.458 3.459 3.460 -SYSTEM PROPERTY: nashorn.methodhandles.debug, 3.461 -nashorn.methodhandles.debug=create 3.462 +SYSTEM PROPERTY: -Dnashorn.methodhandles.debug, 3.463 +-Dnashorn.methodhandles.debug=create 3.464 3.465 If this property is enabled, each MethodHandle related call that uses 3.466 the java.lang.invoke package gets its MethodHandle intercepted and an 3.467 @@ -286,7 +666,7 @@ 3.468 rather than at runtime usage. 3.469 3.470 3.471 -SYSTEM PROPERTY: nashorn.methodhandles.debug.stacktrace 3.472 +SYSTEM PROPERTY: -Dnashorn.methodhandles.debug.stacktrace 3.473 3.474 This does the same as nashorn.methodhandles.debug, but when enabled 3.475 also dumps the stack trace for every instrumented method handle 3.476 @@ -297,14 +677,13 @@ 3.477 description of this option 3.478 3.479 3.480 -SYSTEM PROPERTY: nashorn.scriptfunction.specialization.disable 3.481 +SYSTEM PROPERTY: -Dnashorn.scriptfunction.specialization.disable 3.482 3.483 There are several "fast path" implementations of constructors and 3.484 functions in the NativeObject classes that, in their original form, 3.485 take a variable amount of arguments. Said functions are also declared 3.486 to take Object parameters in their original form, as this is what the 3.487 JavaScript specification mandates. 3.488 - 3.489 However, we often know quite a lot more at a callsite of one of these 3.490 functions. For example, Math.min is called with a fixed number (2) of 3.491 integer arguments. The overhead of boxing these ints to Objects and 3.492 @@ -331,7 +710,7 @@ 3.493 just call the generic one. 3.494 3.495 3.496 -SYSTEM PROPERTY: nashorn.tcs.miss.samplePercent=<x> 3.497 +SYSTEM PROPERTY: -Dnashorn.tcs.miss.samplePercent=<x> 3.498 3.499 When running with the trace callsite option (-tcs), Nashorn will count 3.500 and instrument any callsite misses that require relinking. As the 3.501 @@ -341,7 +720,7 @@ 3.502 default value. 3.503 3.504 3.505 -SYSTEM_PROPERTY: nashorn.profilefile=<filename> 3.506 +SYSTEM_PROPERTY: -Dnashorn.profilefile=<filename> 3.507 3.508 When running with the profile callsite options (-pcs), Nashorn will 3.509 dump profiling data for all callsites to stderr as a shutdown hook. To 3.510 @@ -349,15 +728,35 @@ 3.511 this system property. 3.512 3.513 3.514 -SYSTEM_PROPERTY: nashorn.regexp.impl=[jdk|joni] 3.515 +SYSTEM_PROPERTY: -Dnashorn.regexp.impl=[jdk|joni] 3.516 3.517 This property defines the regular expression engine to be used by 3.518 -Nashorn. The default implementation is "jdk" which is based on the 3.519 +Nashorn. Set this flag to "jdk" to get an implementation based on the 3.520 JDK's java.util.regex package. Set this property to "joni" to install 3.521 an implementation based on Joni, the regular expression engine used by 3.522 -the JRuby project. 3.523 +the JRuby project. The default value for this flag is "joni" 3.524 3.525 3.526 +SYSTEM PROPERTY: -Dnashorn.time 3.527 + 3.528 +This enables timers for various phases of script compilation. The timers 3.529 +will be dumped when the Nashorn process exits. We see a percentage value 3.530 +of how much time was spent not executing bytecode (i.e. compilation and 3.531 +internal tasks) at the end of the report. 3.532 + 3.533 +Here is an example: 3.534 + 3.535 +[JavaScript Parsing] 61 ms 3.536 +[Constant Folding] 11 ms 3.537 +[Control Flow Lowering] 26 ms 3.538 +[Type Attribution] 81 ms 3.539 +[Range Analysis] 0 ms 3.540 +[Code Splitting] 29 ms 3.541 +[Type Finalization] 19 ms 3.542 +[Bytecode Generation] 189 ms 3.543 +[Code Installation] 7 ms 3.544 +Total runtime: 508 ms (Non-runtime: 423 ms [83%]) 3.545 + 3.546 =============== 3.547 2. The loggers. 3.548 =============== 3.549 @@ -442,6 +841,9 @@ 3.550 The --log=codegen option is equivalent to setting the system variable 3.551 "nashorn.codegen.debug" to true. 3.552 3.553 +* fold 3.554 + 3.555 +Shows constant folding taking place before lowering 3.556 3.557 * lower 3.558 3.559 @@ -484,3 +886,160 @@ 3.560 etc. It will also show the internal representation of respective field 3.561 (Object in the normal case, unless running with the dual field 3.562 representation) 3.563 + 3.564 + 3.565 +======================= 3.566 +3. Undocumented options 3.567 +======================= 3.568 + 3.569 +Here follows a short description of undocumented options for Nashorn. 3.570 +To see a list of all undocumented options, use the (undocumented) flag 3.571 +"-xhelp". 3.572 + 3.573 +i.e. jjs -xhelp or java -jar nashorn.jar -xhelp 3.574 + 3.575 +Undocumented options are not guaranteed to work, run correctly or be 3.576 +bug free. They are experimental and for internal or debugging use. 3.577 +They are also subject to change without notice. 3.578 + 3.579 +In practice, though, all options below not explicitly documented as 3.580 +EXPERIMENTAL can be relied upon, for example --dump-on-error is useful 3.581 +for any JavaScript/Nashorn developer, but there is no guarantee. 3.582 + 3.583 +A short summary follows: 3.584 + 3.585 + -D (-Dname=value. Set a system property. This option can be repeated.) 3.586 + 3.587 + -ccs, --class-cache-size (Size of the Class cache size per global scope.) 3.588 + 3.589 + -cp, -classpath (-cp path. Specify where to find user class files.) 3.590 + 3.591 + -co, --compile-only (Compile script without running. Exit after compilation) 3.592 + param: [true|false] default: false 3.593 + 3.594 + -d, --dump-debug-dir (specify a destination directory to dump class files. 3.595 + This must be combined with the --compile-only option to work) 3.596 + param: <path> 3.597 + 3.598 + --debug-lines (Generate line number table in .class files.) 3.599 + param: [true|false] default: true 3.600 + 3.601 + --debug-locals (Generate local variable table in .class files.) 3.602 + param: [true|false] default: false 3.603 + 3.604 + -doe, -dump-on-error (Dump a stack trace on errors.) 3.605 + param: [true|false] default: false 3.606 + 3.607 + --early-lvalue-error (invalid lvalue expressions should be reported as early errors.) 3.608 + param: [true|false] default: true 3.609 + 3.610 + --empty-statements (Preserve empty statements in AST.) 3.611 + param: [true|false] default: false 3.612 + 3.613 + -fv, -fullversion (Print full version info of Nashorn.) 3.614 + param: [true|false] default: false 3.615 + 3.616 + --function-statement-error (Report an error when function declaration is used as a statement.) 3.617 + param: [true|false] default: false 3.618 + 3.619 + --function-statement-warning (Warn when function declaration is used as a statement.) 3.620 + param: [true|false] default: false 3.621 + 3.622 + -fx (Launch script as an fx application.) 3.623 + param: [true|false] default: false 3.624 + 3.625 + --global-per-engine (Use single Global instance per script engine instance.) 3.626 + param: [true|false] default: false 3.627 + 3.628 + -h, -help (Print help for command line flags.) 3.629 + param: [true|false] default: false 3.630 + 3.631 + --lazy-compilation (EXPERIMENTAL: Use lazy code generation strategies - do not compile 3.632 + the entire script at once.) 3.633 + param: [true|false] default: false 3.634 + 3.635 + --loader-per-compile (Create a new class loader per compile.) 3.636 + param: [true|false] default: true 3.637 + 3.638 + -l, --locale (Set Locale for script execution.) 3.639 + param: <locale> default: en-US 3.640 + 3.641 + --log (Enable logging of a given level for a given number of sub systems. 3.642 + [for example: --log=fields:finest,codegen:info]) 3.643 + param: <module:level>,* 3.644 + 3.645 + -nj, --no-java (No Java support) 3.646 + param: [true|false] default: false 3.647 + 3.648 + -nse, --no-syntax-extensions (No non-standard syntax extensions) 3.649 + param: [true|false] default: false 3.650 + 3.651 + -nta, --no-typed-arrays (No Typed arrays support) 3.652 + param: [true|false] default: false 3.653 + 3.654 + --parse-only (Parse without compiling.) 3.655 + param: [true|false] default: false 3.656 + 3.657 + --print-ast (Print abstract syntax tree.) 3.658 + param: [true|false] default: false 3.659 + 3.660 + --print-code (Print bytecode.) 3.661 + param: [true|false] default: false 3.662 + 3.663 + --print-lower-ast (Print lowered abstract syntax tree.) 3.664 + param: [true|false] default: false 3.665 + 3.666 + --print-lower-parse (Print the parse tree after lowering.) 3.667 + param: [true|false] default: false 3.668 + 3.669 + --print-mem-usage (Print memory usage of IR after each compile stage.) 3.670 + param: [true|false] default: false 3.671 + 3.672 + --print-no-newline (Print function will not print new line char.) 3.673 + param: [true|false] default: false 3.674 + 3.675 + --print-parse (Print the parse tree.) 3.676 + param: [true|false] default: false 3.677 + 3.678 + --print-symbols (Print the symbol table.) 3.679 + param: [true|false] default: false 3.680 + 3.681 + -pcs, --profile-callsites (Dump callsite profile data.) 3.682 + param: [true|false] default: false 3.683 + 3.684 + --range-analysis (EXPERIMENTAL: Do range analysis using known compile time types, 3.685 + and try to narrow number types) 3.686 + param: [true|false] default: false 3.687 + 3.688 + -scripting (Enable scripting features.) 3.689 + param: [true|false] default: false 3.690 + 3.691 + --specialize-calls (EXPERIMENTAL: Specialize all or a set of method according 3.692 + to callsite parameter types) 3.693 + param: [=function_1,...,function_n] 3.694 + 3.695 + --stderr (Redirect stderr to a filename or to another tty, e.g. stdout) 3.696 + param: <output console> 3.697 + 3.698 + --stdout (Redirect stdout to a filename or to another tty, e.g. stderr) 3.699 + param: <output console> 3.700 + 3.701 + -strict (Run scripts in strict mode.) 3.702 + param: [true|false] default: false 3.703 + 3.704 + -t, -timezone (Set timezone for script execution.) 3.705 + param: <timezone> default: Europe/Stockholm 3.706 + 3.707 + -tcs, --trace-callsites (Enable callsite trace mode. Options are: miss [trace callsite misses] 3.708 + enterexit [trace callsite enter/exit], objects [print object properties]) 3.709 + param: [=[option,]*] 3.710 + 3.711 + --verify-code (Verify byte code before running.) 3.712 + param: [true|false] default: false 3.713 + 3.714 + -v, -version (Print version info of Nashorn.) 3.715 + param: [true|false] default: false 3.716 + 3.717 + -xhelp (Print extended help for command line flags.) 3.718 + param: [true|false] default: false 3.719 +
4.1 --- a/src/jdk/internal/dynalink/ChainedCallSite.java Thu Aug 29 09:42:13 2013 -0700 4.2 +++ b/src/jdk/internal/dynalink/ChainedCallSite.java Thu Aug 29 16:34:31 2013 -0700 4.3 @@ -121,7 +121,6 @@ 4.4 * to change the value. If your override returns a value less than 1, the code will break. 4.5 * @return the maximum number of method handles in the chain. 4.6 */ 4.7 - @SuppressWarnings("static-method") 4.8 protected int getMaxChainLength() { 4.9 return 8; 4.10 }
5.1 --- a/src/jdk/internal/dynalink/DefaultBootstrapper.java Thu Aug 29 09:42:13 2013 -0700 5.2 +++ b/src/jdk/internal/dynalink/DefaultBootstrapper.java Thu Aug 29 16:34:31 2013 -0700 5.3 @@ -133,7 +133,7 @@ 5.4 * @param type the method signature at the call site 5.5 * @return a new {@link MonomorphicCallSite} linked with the default dynamic linker. 5.6 */ 5.7 - public static CallSite publicBootstrap(@SuppressWarnings("unused") MethodHandles.Lookup caller, String name, MethodType type) { 5.8 + public static CallSite publicBootstrap(MethodHandles.Lookup caller, String name, MethodType type) { 5.9 return bootstrapInternal(MethodHandles.publicLookup(), name, type); 5.10 } 5.11
6.1 --- a/src/jdk/internal/dynalink/beans/AbstractJavaLinker.java Thu Aug 29 09:42:13 2013 -0700 6.2 +++ b/src/jdk/internal/dynalink/beans/AbstractJavaLinker.java Thu Aug 29 16:34:31 2013 -0700 6.3 @@ -97,6 +97,7 @@ 6.4 import java.util.HashMap; 6.5 import java.util.List; 6.6 import java.util.Map; 6.7 + 6.8 import jdk.internal.dynalink.CallSiteDescriptor; 6.9 import jdk.internal.dynalink.beans.GuardedInvocationComponent.ValidationType; 6.10 import jdk.internal.dynalink.linker.GuardedInvocation;
7.1 --- a/src/jdk/internal/dynalink/beans/OverloadedDynamicMethod.java Thu Aug 29 09:42:13 2013 -0700 7.2 +++ b/src/jdk/internal/dynalink/beans/OverloadedDynamicMethod.java Thu Aug 29 16:34:31 2013 -0700 7.3 @@ -148,6 +148,7 @@ 7.4 } 7.5 } 7.6 7.7 + @SuppressWarnings("fallthrough") 7.8 @Override 7.9 public MethodHandle getInvocation(final CallSiteDescriptor callSiteDescriptor, final LinkerServices linkerServices) { 7.10 final MethodType callSiteType = callSiteDescriptor.getMethodType();
8.1 --- a/src/jdk/nashorn/api/scripting/NashornScriptEngine.java Thu Aug 29 09:42:13 2013 -0700 8.2 +++ b/src/jdk/nashorn/api/scripting/NashornScriptEngine.java Thu Aug 29 16:34:31 2013 -0700 8.3 @@ -55,6 +55,7 @@ 8.4 import javax.script.ScriptEngine; 8.5 import javax.script.ScriptEngineFactory; 8.6 import javax.script.ScriptException; 8.7 +import javax.script.SimpleBindings; 8.8 import jdk.nashorn.internal.runtime.Context; 8.9 import jdk.nashorn.internal.runtime.ErrorManager; 8.10 import jdk.nashorn.internal.runtime.GlobalObject; 8.11 @@ -74,6 +75,12 @@ 8.12 */ 8.13 8.14 public final class NashornScriptEngine extends AbstractScriptEngine implements Compilable, Invocable { 8.15 + /** 8.16 + * Key used to associate Nashorn global object mirror with arbitrary Bindings instance. 8.17 + */ 8.18 + public static final String NASHORN_GLOBAL = "nashorn.global"; 8.19 + 8.20 + // commonly used access control context objects 8.21 private static AccessControlContext createPermAccCtxt(final String permName) { 8.22 final Permissions perms = new Permissions(); 8.23 perms.add(new RuntimePermission(permName)); 8.24 @@ -83,16 +90,23 @@ 8.25 private static final AccessControlContext CREATE_CONTEXT_ACC_CTXT = createPermAccCtxt(Context.NASHORN_CREATE_CONTEXT); 8.26 private static final AccessControlContext CREATE_GLOBAL_ACC_CTXT = createPermAccCtxt(Context.NASHORN_CREATE_GLOBAL); 8.27 8.28 + // the factory that created this engine 8.29 private final ScriptEngineFactory factory; 8.30 + // underlying nashorn Context - 1:1 with engine instance 8.31 private final Context nashornContext; 8.32 + // do we want to share single Nashorn global instance across ENGINE_SCOPEs? 8.33 + private final boolean _global_per_engine; 8.34 + // This is the initial default Nashorn global object. 8.35 + // This is used as "shared" global if above option is true. 8.36 private final ScriptObject global; 8.37 - // initialized bit late to be made 'final'. Property object for "context" 8.38 - // property of global object 8.39 - private Property contextProperty; 8.40 + // initialized bit late to be made 'final'. 8.41 + // Property object for "context" property of global object. 8.42 + private volatile Property contextProperty; 8.43 8.44 // default options passed to Nashorn Options object 8.45 private static final String[] DEFAULT_OPTIONS = new String[] { "-scripting", "-doe" }; 8.46 8.47 + // Nashorn script engine error message management 8.48 private static final String MESSAGES_RESOURCE = "jdk.nashorn.api.scripting.resources.Messages"; 8.49 8.50 private static final ResourceBundle MESSAGES_BUNDLE; 8.51 @@ -100,6 +114,7 @@ 8.52 MESSAGES_BUNDLE = ResourceBundle.getBundle(MESSAGES_RESOURCE, Locale.getDefault()); 8.53 } 8.54 8.55 + // helper to get Nashorn script engine error message 8.56 private static String getMessage(final String msgId, final String... args) { 8.57 try { 8.58 return new MessageFormat(MESSAGES_BUNDLE.getString(msgId)).format(args); 8.59 @@ -108,6 +123,31 @@ 8.60 } 8.61 } 8.62 8.63 + // load engine.js and return content as a char[] 8.64 + @SuppressWarnings("resource") 8.65 + private static char[] loadEngineJSSource() { 8.66 + final String script = "resources/engine.js"; 8.67 + try { 8.68 + final InputStream is = AccessController.doPrivileged( 8.69 + new PrivilegedExceptionAction<InputStream>() { 8.70 + @Override 8.71 + public InputStream run() throws Exception { 8.72 + final URL url = NashornScriptEngine.class.getResource(script); 8.73 + return url.openStream(); 8.74 + } 8.75 + }); 8.76 + return Source.readFully(new InputStreamReader(is)); 8.77 + } catch (final PrivilegedActionException | IOException e) { 8.78 + if (Context.DEBUG) { 8.79 + e.printStackTrace(); 8.80 + } 8.81 + throw new RuntimeException(e); 8.82 + } 8.83 + } 8.84 + 8.85 + // Source object for engine.js 8.86 + private static final Source ENGINE_SCRIPT_SRC = new Source(NashornException.ENGINE_SCRIPT_SOURCE_NAME, loadEngineJSSource()); 8.87 + 8.88 NashornScriptEngine(final NashornScriptEngineFactory factory, final ClassLoader appLoader) { 8.89 this(factory, DEFAULT_OPTIONS, appLoader); 8.90 } 8.91 @@ -134,20 +174,13 @@ 8.92 } 8.93 }, CREATE_CONTEXT_ACC_CTXT); 8.94 8.95 + // cache this option that is used often 8.96 + this._global_per_engine = nashornContext.getEnv()._global_per_engine; 8.97 + 8.98 // create new global object 8.99 - this.global = createNashornGlobal(); 8.100 - // set the default engine scope for the default context 8.101 + this.global = createNashornGlobal(context); 8.102 + // set the default ENGINE_SCOPE object for the default context 8.103 context.setBindings(new ScriptObjectMirror(global, global), ScriptContext.ENGINE_SCOPE); 8.104 - 8.105 - // evaluate engine initial script 8.106 - try { 8.107 - evalEngineScript(); 8.108 - } catch (final ScriptException e) { 8.109 - if (Context.DEBUG) { 8.110 - e.printStackTrace(); 8.111 - } 8.112 - throw new RuntimeException(e); 8.113 - } 8.114 } 8.115 8.116 @Override 8.117 @@ -176,8 +209,12 @@ 8.118 8.119 @Override 8.120 public Bindings createBindings() { 8.121 - final ScriptObject newGlobal = createNashornGlobal(); 8.122 - return new ScriptObjectMirror(newGlobal, newGlobal); 8.123 + if (_global_per_engine) { 8.124 + // just create normal SimpleBindings. 8.125 + // We use same 'global' for all Bindings. 8.126 + return new SimpleBindings(); 8.127 + } 8.128 + return createGlobalMirror(null); 8.129 } 8.130 8.131 // Compilable methods 8.132 @@ -213,6 +250,48 @@ 8.133 return invokeImpl(thiz, name, args); 8.134 } 8.135 8.136 + @Override 8.137 + public <T> T getInterface(final Class<T> clazz) { 8.138 + return getInterfaceInner(null, clazz); 8.139 + } 8.140 + 8.141 + @Override 8.142 + public <T> T getInterface(final Object thiz, final Class<T> clazz) { 8.143 + if (thiz == null) { 8.144 + throw new IllegalArgumentException(getMessage("thiz.cannot.be.null")); 8.145 + } 8.146 + return getInterfaceInner(thiz, clazz); 8.147 + } 8.148 + 8.149 + // These are called from the "engine.js" script 8.150 + 8.151 + /** 8.152 + * This hook is used to search js global variables exposed from Java code. 8.153 + * 8.154 + * @param self 'this' passed from the script 8.155 + * @param ctxt current ScriptContext in which name is searched 8.156 + * @param name name of the variable searched 8.157 + * @return the value of the named variable 8.158 + */ 8.159 + public Object __noSuchProperty__(final Object self, final ScriptContext ctxt, final String name) { 8.160 + if (ctxt != null) { 8.161 + final int scope = ctxt.getAttributesScope(name); 8.162 + final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt); 8.163 + if (scope != -1) { 8.164 + return ScriptObjectMirror.unwrap(ctxt.getAttribute(name, scope), ctxtGlobal); 8.165 + } 8.166 + 8.167 + if (self == UNDEFINED) { 8.168 + // scope access and so throw ReferenceError 8.169 + throw referenceError(ctxtGlobal, "not.defined", name); 8.170 + } 8.171 + } 8.172 + 8.173 + return UNDEFINED; 8.174 + } 8.175 + 8.176 + // Implementation only below this point 8.177 + 8.178 private <T> T getInterfaceInner(final Object thiz, final Class<T> clazz) { 8.179 if (clazz == null || !clazz.isInterface()) { 8.180 throw new IllegalArgumentException(getMessage("interface.class.expected")); 8.181 @@ -280,58 +359,56 @@ 8.182 } 8.183 } 8.184 8.185 - @Override 8.186 - public <T> T getInterface(final Class<T> clazz) { 8.187 - return getInterfaceInner(null, clazz); 8.188 + // Retrieve nashorn Global object for a given ScriptContext object 8.189 + private ScriptObject getNashornGlobalFrom(final ScriptContext ctxt) { 8.190 + if (_global_per_engine) { 8.191 + // shared single global object for all ENGINE_SCOPE Bindings 8.192 + return global; 8.193 + } 8.194 + 8.195 + final Bindings bindings = ctxt.getBindings(ScriptContext.ENGINE_SCOPE); 8.196 + // is this Nashorn's own Bindings implementation? 8.197 + if (bindings instanceof ScriptObjectMirror) { 8.198 + final ScriptObject sobj = globalFromMirror((ScriptObjectMirror)bindings); 8.199 + if (sobj != null) { 8.200 + return sobj; 8.201 + } 8.202 + } 8.203 + 8.204 + // Arbitrary user Bindings implementation. Look for NASHORN_GLOBAL in it! 8.205 + Object scope = bindings.get(NASHORN_GLOBAL); 8.206 + if (scope instanceof ScriptObjectMirror) { 8.207 + final ScriptObject sobj = globalFromMirror((ScriptObjectMirror)scope); 8.208 + if (sobj != null) { 8.209 + return sobj; 8.210 + } 8.211 + } 8.212 + 8.213 + // We didn't find associated nashorn global mirror in the Bindings given! 8.214 + // Create new global instance mirror and associate with the Bindings. 8.215 + final ScriptObjectMirror mirror = createGlobalMirror(ctxt); 8.216 + bindings.put(NASHORN_GLOBAL, mirror); 8.217 + return mirror.getScriptObject(); 8.218 } 8.219 8.220 - @Override 8.221 - public <T> T getInterface(final Object thiz, final Class<T> clazz) { 8.222 - if (thiz == null) { 8.223 - throw new IllegalArgumentException(getMessage("thiz.cannot.be.null")); 8.224 + // Retrieve nashorn Global object from a given ScriptObjectMirror 8.225 + private ScriptObject globalFromMirror(final ScriptObjectMirror mirror) { 8.226 + ScriptObject sobj = mirror.getScriptObject(); 8.227 + if (sobj instanceof GlobalObject && sobj.isOfContext(nashornContext)) { 8.228 + return sobj; 8.229 } 8.230 - return getInterfaceInner(thiz, clazz); 8.231 + 8.232 + return null; 8.233 } 8.234 8.235 - // These are called from the "engine.js" script 8.236 - 8.237 - /** 8.238 - * This hook is used to search js global variables exposed from Java code. 8.239 - * 8.240 - * @param self 'this' passed from the script 8.241 - * @param ctxt current ScriptContext in which name is searched 8.242 - * @param name name of the variable searched 8.243 - * @return the value of the named variable 8.244 - */ 8.245 - public Object __noSuchProperty__(final Object self, final ScriptContext ctxt, final String name) { 8.246 - final int scope = ctxt.getAttributesScope(name); 8.247 - final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt); 8.248 - if (scope != -1) { 8.249 - return ScriptObjectMirror.unwrap(ctxt.getAttribute(name, scope), ctxtGlobal); 8.250 - } 8.251 - 8.252 - if (self == UNDEFINED) { 8.253 - // scope access and so throw ReferenceError 8.254 - throw referenceError(ctxtGlobal, "not.defined", name); 8.255 - } 8.256 - 8.257 - return UNDEFINED; 8.258 + // Create a new ScriptObjectMirror wrapping a newly created Nashorn Global object 8.259 + private ScriptObjectMirror createGlobalMirror(final ScriptContext ctxt) { 8.260 + final ScriptObject newGlobal = createNashornGlobal(ctxt); 8.261 + return new ScriptObjectMirror(newGlobal, newGlobal); 8.262 } 8.263 8.264 - private ScriptObject getNashornGlobalFrom(final ScriptContext ctxt) { 8.265 - final Bindings bindings = ctxt.getBindings(ScriptContext.ENGINE_SCOPE); 8.266 - if (bindings instanceof ScriptObjectMirror) { 8.267 - ScriptObject sobj = ((ScriptObjectMirror)bindings).getScriptObject(); 8.268 - if (sobj instanceof GlobalObject) { 8.269 - return sobj; 8.270 - } 8.271 - } 8.272 - 8.273 - // didn't find global object from context given - return the engine-wide global 8.274 - return global; 8.275 - } 8.276 - 8.277 - private ScriptObject createNashornGlobal() { 8.278 + // Create a new Nashorn Global object 8.279 + private ScriptObject createNashornGlobal(final ScriptContext ctxt) { 8.280 final ScriptObject newGlobal = AccessController.doPrivileged(new PrivilegedAction<ScriptObject>() { 8.281 @Override 8.282 public ScriptObject run() { 8.283 @@ -352,7 +429,7 @@ 8.284 // current ScriptContext exposed as "context" 8.285 // "context" is non-writable from script - but script engine still 8.286 // needs to set it and so save the context Property object 8.287 - contextProperty = newGlobal.addOwnProperty("context", NON_ENUMERABLE_CONSTANT, UNDEFINED); 8.288 + contextProperty = newGlobal.addOwnProperty("context", NON_ENUMERABLE_CONSTANT, null); 8.289 // current ScriptEngine instance exposed as "engine". We added @SuppressWarnings("LeakingThisInConstructor") as 8.290 // NetBeans identifies this assignment as such a leak - this is a false positive as we're setting this property 8.291 // in the Global of a Context we just created - both the Context and the Global were just created and can not be 8.292 @@ -362,38 +439,17 @@ 8.293 newGlobal.addOwnProperty("arguments", Property.NOT_ENUMERABLE, UNDEFINED); 8.294 // file name default is null 8.295 newGlobal.addOwnProperty(ScriptEngine.FILENAME, Property.NOT_ENUMERABLE, null); 8.296 + // evaluate engine.js initialization script this new global object 8.297 + try { 8.298 + evalImpl(compileImpl(ENGINE_SCRIPT_SRC, newGlobal), ctxt, newGlobal); 8.299 + } catch (final ScriptException exp) { 8.300 + throw new RuntimeException(exp); 8.301 + } 8.302 return newGlobal; 8.303 } 8.304 8.305 - private void evalEngineScript() throws ScriptException { 8.306 - final String script = "resources/engine.js"; 8.307 - final String name = NashornException.ENGINE_SCRIPT_SOURCE_NAME; 8.308 - try { 8.309 - final InputStream is = AccessController.doPrivileged( 8.310 - new PrivilegedExceptionAction<InputStream>() { 8.311 - @Override 8.312 - public InputStream run() throws Exception { 8.313 - final URL url = NashornScriptEngine.class.getResource(script); 8.314 - return url.openStream(); 8.315 - } 8.316 - }); 8.317 - put(ScriptEngine.FILENAME, name); 8.318 - try (final InputStreamReader isr = new InputStreamReader(is)) { 8.319 - eval(isr); 8.320 - } 8.321 - } catch (final PrivilegedActionException | IOException e) { 8.322 - if (Context.DEBUG) { 8.323 - e.printStackTrace(); 8.324 - } 8.325 - throw new ScriptException(e); 8.326 - } finally { 8.327 - put(ScriptEngine.FILENAME, null); 8.328 - } 8.329 - } 8.330 - 8.331 - // scripts should see "context" and "engine" as variables 8.332 - private void setContextVariables(final ScriptContext ctxt) { 8.333 - final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt); 8.334 + // scripts should see "context" and "engine" as variables in the given global object 8.335 + private void setContextVariables(final ScriptObject ctxtGlobal, final ScriptContext ctxt) { 8.336 // set "context" global variable via contextProperty - because this 8.337 // property is non-writable 8.338 contextProperty.setObjectValue(ctxtGlobal, ctxtGlobal, ctxt, false); 8.339 @@ -402,8 +458,10 @@ 8.340 args = ScriptRuntime.EMPTY_ARRAY; 8.341 } 8.342 // if no arguments passed, expose it 8.343 - args = ((GlobalObject)ctxtGlobal).wrapAsObject(args); 8.344 - ctxtGlobal.set("arguments", args, false); 8.345 + if (! (args instanceof ScriptObject)) { 8.346 + args = ((GlobalObject)ctxtGlobal).wrapAsObject(args); 8.347 + ctxtGlobal.set("arguments", args, false); 8.348 + } 8.349 } 8.350 8.351 private Object invokeImpl(final Object selfObject, final String name, final Object... args) throws ScriptException, NoSuchMethodException { 8.352 @@ -456,18 +514,24 @@ 8.353 } 8.354 8.355 private Object evalImpl(final ScriptFunction script, final ScriptContext ctxt) throws ScriptException { 8.356 + return evalImpl(script, ctxt, getNashornGlobalFrom(ctxt)); 8.357 + } 8.358 + 8.359 + private Object evalImpl(final ScriptFunction script, final ScriptContext ctxt, final ScriptObject ctxtGlobal) throws ScriptException { 8.360 if (script == null) { 8.361 return null; 8.362 } 8.363 final ScriptObject oldGlobal = Context.getGlobal(); 8.364 - final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt); 8.365 final boolean globalChanged = (oldGlobal != ctxtGlobal); 8.366 try { 8.367 if (globalChanged) { 8.368 Context.setGlobal(ctxtGlobal); 8.369 } 8.370 8.371 - setContextVariables(ctxt); 8.372 + // set ScriptContext variables if ctxt is non-null 8.373 + if (ctxt != null) { 8.374 + setContextVariables(ctxtGlobal, ctxt); 8.375 + } 8.376 return ScriptObjectMirror.translateUndefined(ScriptObjectMirror.wrap(ScriptRuntime.apply(script, ctxtGlobal), ctxtGlobal)); 8.377 } catch (final Exception e) { 8.378 throwAsScriptException(e); 8.379 @@ -517,15 +581,18 @@ 8.380 } 8.381 8.382 private ScriptFunction compileImpl(final Source source, final ScriptContext ctxt) throws ScriptException { 8.383 + return compileImpl(source, getNashornGlobalFrom(ctxt)); 8.384 + } 8.385 + 8.386 + private ScriptFunction compileImpl(final Source source, final ScriptObject newGlobal) throws ScriptException { 8.387 final ScriptObject oldGlobal = Context.getGlobal(); 8.388 - final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt); 8.389 - final boolean globalChanged = (oldGlobal != ctxtGlobal); 8.390 + final boolean globalChanged = (oldGlobal != newGlobal); 8.391 try { 8.392 if (globalChanged) { 8.393 - Context.setGlobal(ctxtGlobal); 8.394 + Context.setGlobal(newGlobal); 8.395 } 8.396 8.397 - return nashornContext.compileScript(source, ctxtGlobal); 8.398 + return nashornContext.compileScript(source, newGlobal); 8.399 } catch (final Exception e) { 8.400 throwAsScriptException(e); 8.401 throw new AssertionError("should not reach here");
9.1 --- a/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java Thu Aug 29 09:42:13 2013 -0700 9.2 +++ b/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java Thu Aug 29 16:34:31 2013 -0700 9.3 @@ -99,12 +99,14 @@ 9.4 } 9.5 9.6 final Object val = functionName == null? sobj : sobj.get(functionName); 9.7 - if (! (val instanceof ScriptFunction)) { 9.8 - throw new NoSuchMethodException("No such function " + ((functionName != null)? functionName : "")); 9.9 + if (val instanceof ScriptFunction) { 9.10 + final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args; 9.11 + return wrap(ScriptRuntime.checkAndApply((ScriptFunction)val, sobj, unwrapArray(modArgs, global)), global); 9.12 + } else if (val instanceof ScriptObjectMirror && ((ScriptObjectMirror)val).isFunction()) { 9.13 + return ((ScriptObjectMirror)val).call(null, args); 9.14 } 9.15 9.16 - final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args; 9.17 - return wrap(ScriptRuntime.checkAndApply((ScriptFunction)val, sobj, unwrapArray(modArgs, global)), global); 9.18 + throw new NoSuchMethodException("No such function " + ((functionName != null)? functionName : "")); 9.19 } catch (final RuntimeException | Error e) { 9.20 throw e; 9.21 } catch (final Throwable t) { 9.22 @@ -127,12 +129,14 @@ 9.23 } 9.24 9.25 final Object val = functionName == null? sobj : sobj.get(functionName); 9.26 - if (! (val instanceof ScriptFunction)) { 9.27 - throw new RuntimeException("not a constructor " + ((functionName != null)? functionName : "")); 9.28 + if (val instanceof ScriptFunction) { 9.29 + final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args; 9.30 + return wrap(ScriptRuntime.checkAndConstruct((ScriptFunction)val, unwrapArray(modArgs, global)), global); 9.31 + } else if (val instanceof ScriptObjectMirror && ((ScriptObjectMirror)val).isFunction()) { 9.32 + return ((ScriptObjectMirror)val).newObject(null, args); 9.33 } 9.34 9.35 - final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args; 9.36 - return wrap(ScriptRuntime.checkAndConstruct((ScriptFunction)val, unwrapArray(modArgs, global)), global); 9.37 + throw new RuntimeException("not a constructor " + ((functionName != null)? functionName : "")); 9.38 } catch (final RuntimeException | Error e) { 9.39 throw e; 9.40 } catch (final Throwable t) { 9.41 @@ -374,6 +378,28 @@ 9.42 } 9.43 9.44 /** 9.45 + * Set the __proto__ of this object. 9.46 + * @param proto new proto for this object 9.47 + */ 9.48 + public void setProto(final Object proto) { 9.49 + inGlobal(new Callable<Void>() { 9.50 + @Override public Void call() { 9.51 + sobj.setProtoCheck(unwrap(proto, global)); 9.52 + return null; 9.53 + } 9.54 + }); 9.55 + } 9.56 + 9.57 + /** 9.58 + * ECMA [[Class]] property 9.59 + * 9.60 + * @return ECMA [[Class]] property value of this object 9.61 + */ 9.62 + public String getClassName() { 9.63 + return sobj.getClassName(); 9.64 + } 9.65 + 9.66 + /** 9.67 * ECMA 8.12.1 [[GetOwnProperty]] (P) 9.68 * 9.69 * @param key property key
10.1 --- a/src/jdk/nashorn/api/scripting/resources/engine.js Thu Aug 29 09:42:13 2013 -0700 10.2 +++ b/src/jdk/nashorn/api/scripting/resources/engine.js Thu Aug 29 16:34:31 2013 -0700 10.3 @@ -23,10 +23,9 @@ 10.4 10.5 /** 10.6 * This script file is executed by script engine at the construction 10.7 - * of the engine. The functions here assume global variables "context" 10.8 - * of type javax.script.ScriptContext and "engine" of the type 10.9 + * of the every new Global object. The functions here assume global variables 10.10 + * "context" of type javax.script.ScriptContext and "engine" of the type 10.11 * jdk.nashorn.api.scripting.NashornScriptEngine. 10.12 - * 10.13 **/ 10.14 10.15 Object.defineProperty(this, "__noSuchProperty__", { 10.16 @@ -40,7 +39,7 @@ 10.17 }); 10.18 10.19 function print() { 10.20 - var writer = context.getWriter(); 10.21 + var writer = context != null? context.writer : engine.context.writer; 10.22 if (! (writer instanceof java.io.PrintWriter)) { 10.23 writer = new java.io.PrintWriter(writer); 10.24 }
11.1 --- a/src/jdk/nashorn/internal/codegen/Attr.java Thu Aug 29 09:42:13 2013 -0700 11.2 +++ b/src/jdk/nashorn/internal/codegen/Attr.java Thu Aug 29 16:34:31 2013 -0700 11.3 @@ -543,8 +543,6 @@ 11.4 public Node leaveIdentNode(final IdentNode identNode) { 11.5 final String name = identNode.getName(); 11.6 11.7 - start(identNode); 11.8 - 11.9 if (identNode.isPropertyName()) { 11.10 // assign a pseudo symbol to property name 11.11 final Symbol pseudoSymbol = pseudoSymbol(name); 11.12 @@ -1850,9 +1848,10 @@ 11.13 append("] "). 11.14 append(printNode ? node.toString() : ""). 11.15 append(" in '"). 11.16 - append(lc.getCurrentFunction().getName()); 11.17 + append(lc.getCurrentFunction().getName()). 11.18 + append('\''); 11.19 11.20 - if(node instanceof Expression) { 11.21 + if (node instanceof Expression) { 11.22 final Symbol symbol = ((Expression)node).getSymbol(); 11.23 if (symbol == null) { 11.24 sb.append(" <NO SYMBOL>");
12.1 --- a/src/jdk/nashorn/internal/codegen/CompilationPhase.java Thu Aug 29 09:42:13 2013 -0700 12.2 +++ b/src/jdk/nashorn/internal/codegen/CompilationPhase.java Thu Aug 29 16:34:31 2013 -0700 12.3 @@ -414,30 +414,34 @@ 12.4 compiler.getCodeInstaller().verify(bytecode); 12.5 } 12.6 12.7 - // should code be dumped to disk - only valid in compile_only 12.8 - // mode? 12.9 + // should code be dumped to disk - only valid in compile_only mode? 12.10 if (env._dest_dir != null && env._compile_only) { 12.11 final String fileName = className.replace('.', File.separatorChar) + ".class"; 12.12 - final int index = fileName.lastIndexOf(File.separatorChar); 12.13 + final int index = fileName.lastIndexOf(File.separatorChar); 12.14 12.15 + final File dir; 12.16 if (index != -1) { 12.17 - final File dir = new File(fileName.substring(0, index)); 12.18 - try { 12.19 - if (!dir.exists() && !dir.mkdirs()) { 12.20 - throw new IOException(dir.toString()); 12.21 - } 12.22 - final File file = new File(env._dest_dir, fileName); 12.23 - try (final FileOutputStream fos = new FileOutputStream(file)) { 12.24 - fos.write(bytecode); 12.25 - } 12.26 - } catch (final IOException e) { 12.27 - Compiler.LOG.warning("Skipping class dump for ", 12.28 - className, 12.29 - ": ", 12.30 - ECMAErrors.getMessage( 12.31 - "io.error.cant.write", 12.32 - dir.toString())); 12.33 + dir = new File(env._dest_dir, fileName.substring(0, index)); 12.34 + } else { 12.35 + dir = new File(env._dest_dir); 12.36 + } 12.37 + 12.38 + try { 12.39 + if (!dir.exists() && !dir.mkdirs()) { 12.40 + throw new IOException(dir.toString()); 12.41 } 12.42 + final File file = new File(env._dest_dir, fileName); 12.43 + try (final FileOutputStream fos = new FileOutputStream(file)) { 12.44 + fos.write(bytecode); 12.45 + } 12.46 + Compiler.LOG.info("Wrote class to '" + file.getAbsolutePath() + '\''); 12.47 + } catch (final IOException e) { 12.48 + Compiler.LOG.warning("Skipping class dump for ", 12.49 + className, 12.50 + ": ", 12.51 + ECMAErrors.getMessage( 12.52 + "io.error.cant.write", 12.53 + dir.toString())); 12.54 } 12.55 } 12.56 }
13.1 --- a/src/jdk/nashorn/internal/objects/NativeArray.java Thu Aug 29 09:42:13 2013 -0700 13.2 +++ b/src/jdk/nashorn/internal/objects/NativeArray.java Thu Aug 29 16:34:31 2013 -0700 13.3 @@ -40,6 +40,7 @@ 13.4 import java.util.Iterator; 13.5 import java.util.List; 13.6 import java.util.concurrent.Callable; 13.7 + 13.8 import jdk.nashorn.api.scripting.ScriptObjectMirror; 13.9 import jdk.nashorn.internal.objects.annotations.Attribute; 13.10 import jdk.nashorn.internal.objects.annotations.Constructor; 13.11 @@ -632,6 +633,7 @@ 13.12 return new NativeArray(list.toArray()); 13.13 } 13.14 13.15 + @SuppressWarnings("null") 13.16 private static void concatToList(final ArrayList<Object> list, final Object obj) { 13.17 final boolean isScriptArray = isArray(obj); 13.18 final boolean isScriptObject = isScriptArray || obj instanceof ScriptObject;
14.1 --- a/src/jdk/nashorn/internal/objects/NativeArrayBuffer.java Thu Aug 29 09:42:13 2013 -0700 14.2 +++ b/src/jdk/nashorn/internal/objects/NativeArrayBuffer.java Thu Aug 29 16:34:31 2013 -0700 14.3 @@ -73,6 +73,11 @@ 14.4 this(Arrays.copyOfRange(other.buffer, begin, end)); 14.5 } 14.6 14.7 + @Override 14.8 + public String getClassName() { 14.9 + return "ArrayBuffer"; 14.10 + } 14.11 + 14.12 @Getter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE) 14.13 public static Object byteLength(final Object self) { 14.14 return ((NativeArrayBuffer)self).buffer.length;
15.1 --- a/src/jdk/nashorn/internal/objects/NativeFloat32Array.java Thu Aug 29 09:42:13 2013 -0700 15.2 +++ b/src/jdk/nashorn/internal/objects/NativeFloat32Array.java Thu Aug 29 16:34:31 2013 -0700 15.3 @@ -145,6 +145,11 @@ 15.4 } 15.5 15.6 @Override 15.7 + public String getClassName() { 15.8 + return "Float32Array"; 15.9 + } 15.10 + 15.11 + @Override 15.12 protected Factory factory() { 15.13 return FACTORY; 15.14 }
16.1 --- a/src/jdk/nashorn/internal/objects/NativeFloat64Array.java Thu Aug 29 09:42:13 2013 -0700 16.2 +++ b/src/jdk/nashorn/internal/objects/NativeFloat64Array.java Thu Aug 29 16:34:31 2013 -0700 16.3 @@ -155,6 +155,11 @@ 16.4 } 16.5 16.6 @Override 16.7 + public String getClassName() { 16.8 + return "Float64Array"; 16.9 + } 16.10 + 16.11 + @Override 16.12 protected Factory factory() { 16.13 return FACTORY; 16.14 }
17.1 --- a/src/jdk/nashorn/internal/objects/NativeInt16Array.java Thu Aug 29 09:42:13 2013 -0700 17.2 +++ b/src/jdk/nashorn/internal/objects/NativeInt16Array.java Thu Aug 29 16:34:31 2013 -0700 17.3 @@ -109,6 +109,11 @@ 17.4 } 17.5 17.6 @Override 17.7 + public String getClassName() { 17.8 + return "Int16Array"; 17.9 + } 17.10 + 17.11 + @Override 17.12 protected Factory factory() { 17.13 return FACTORY; 17.14 }
18.1 --- a/src/jdk/nashorn/internal/objects/NativeInt32Array.java Thu Aug 29 09:42:13 2013 -0700 18.2 +++ b/src/jdk/nashorn/internal/objects/NativeInt32Array.java Thu Aug 29 16:34:31 2013 -0700 18.3 @@ -112,6 +112,11 @@ 18.4 } 18.5 18.6 @Override 18.7 + public String getClassName() { 18.8 + return "Int32Array"; 18.9 + } 18.10 + 18.11 + @Override 18.12 protected Factory factory() { 18.13 return FACTORY; 18.14 }
19.1 --- a/src/jdk/nashorn/internal/objects/NativeInt8Array.java Thu Aug 29 09:42:13 2013 -0700 19.2 +++ b/src/jdk/nashorn/internal/objects/NativeInt8Array.java Thu Aug 29 16:34:31 2013 -0700 19.3 @@ -102,6 +102,11 @@ 19.4 } 19.5 19.6 @Override 19.7 + public String getClassName() { 19.8 + return "Int8Array"; 19.9 + } 19.10 + 19.11 + @Override 19.12 protected Factory factory() { 19.13 return FACTORY; 19.14 }
20.1 --- a/src/jdk/nashorn/internal/objects/NativeJava.java Thu Aug 29 09:42:13 2013 -0700 20.2 +++ b/src/jdk/nashorn/internal/objects/NativeJava.java Thu Aug 29 16:34:31 2013 -0700 20.3 @@ -32,7 +32,6 @@ 20.4 import java.util.Collection; 20.5 import java.util.Deque; 20.6 import java.util.List; 20.7 - 20.8 import jdk.internal.dynalink.beans.StaticClass; 20.9 import jdk.internal.dynalink.support.TypeUtilities; 20.10 import jdk.nashorn.internal.objects.annotations.Attribute; 20.11 @@ -44,6 +43,7 @@ 20.12 import jdk.nashorn.internal.runtime.ListAdapter; 20.13 import jdk.nashorn.internal.runtime.PropertyMap; 20.14 import jdk.nashorn.internal.runtime.ScriptObject; 20.15 +import jdk.nashorn.internal.runtime.linker.Bootstrap; 20.16 import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory; 20.17 20.18 /** 20.19 @@ -539,4 +539,25 @@ 20.20 } 20.21 return JavaAdapterFactory.getAdapterClassFor(stypes, classOverrides); 20.22 } 20.23 + 20.24 + /** 20.25 + * When given an object created using {@code Java.extend()} or equivalent mechanism (that is, any JavaScript-to-Java 20.26 + * adapter), returns an object that can be used to invoke superclass methods on that object. E.g.: 20.27 + * <pre> 20.28 + * var cw = new FilterWriterAdapter(sw) { 20.29 + * write: function(s, off, len) { 20.30 + * s = capitalize(s, off, len) 20.31 + * cw_super.write(s, 0, s.length()) 20.32 + * } 20.33 + * } 20.34 + * var cw_super = Java.super(cw) 20.35 + * </pre> 20.36 + * @param self the {@code Java} object itself - not used. 20.37 + * @param adapter the original Java adapter instance for which the super adapter is created. 20.38 + * @return a super adapter for the original adapter 20.39 + */ 20.40 + @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR, name="super") 20.41 + public static Object _super(final Object self, final Object adapter) { 20.42 + return Bootstrap.createSuperAdapter(adapter); 20.43 + } 20.44 }
21.1 --- a/src/jdk/nashorn/internal/objects/NativeObject.java Thu Aug 29 09:42:13 2013 -0700 21.2 +++ b/src/jdk/nashorn/internal/objects/NativeObject.java Thu Aug 29 16:34:31 2013 -0700 21.3 @@ -125,6 +125,28 @@ 21.4 } 21.5 21.6 /** 21.7 + * Nashorn extension: Object.setPrototypeOf ( O, proto ) 21.8 + * Also found in ES6 draft specification. 21.9 + * 21.10 + * @param self self reference 21.11 + * @param obj object to set prototype for 21.12 + * @param proto prototype object to be used 21.13 + * @return object whose prototype is set 21.14 + */ 21.15 + @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) 21.16 + public static Object setPrototypeOf(final Object self, final Object obj, final Object proto) { 21.17 + if (obj instanceof ScriptObject) { 21.18 + ((ScriptObject)obj).setProtoCheck(proto); 21.19 + return obj; 21.20 + } else if (obj instanceof ScriptObjectMirror) { 21.21 + ((ScriptObjectMirror)obj).setProto(proto); 21.22 + return obj; 21.23 + } 21.24 + 21.25 + throw notAnObject(obj); 21.26 + } 21.27 + 21.28 + /** 21.29 * ECMA 15.2.3.3 Object.getOwnPropertyDescriptor ( O, P ) 21.30 * 21.31 * @param self self reference 21.32 @@ -184,7 +206,7 @@ 21.33 // FIXME: should we create a proper object with correct number of 21.34 // properties? 21.35 final ScriptObject newObj = Global.newEmptyInstance(); 21.36 - newObj.setProtoCheck(proto); 21.37 + newObj.setProto((ScriptObject)proto); 21.38 if (props != UNDEFINED) { 21.39 NativeObject.defineProperties(self, newObj, props); 21.40 } 21.41 @@ -647,15 +669,43 @@ 21.42 21.43 final List<AccessorProperty> properties = new ArrayList<>(propertyNames.size() + methodNames.size()); 21.44 for(final String methodName: methodNames) { 21.45 - properties.add(AccessorProperty.create(methodName, Property.NOT_WRITABLE, 21.46 - getBoundBeanMethodGetter(source, getBeanOperation(linker, "dyn:getMethod:" + methodName, getterType, source)), 21.47 - null)); 21.48 + final MethodHandle method; 21.49 + try { 21.50 + method = getBeanOperation(linker, "dyn:getMethod:" + methodName, getterType, source); 21.51 + } catch(final IllegalAccessError e) { 21.52 + // Presumably, this was a caller sensitive method. Ignore it and carry on. 21.53 + continue; 21.54 + } 21.55 + properties.add(AccessorProperty.create(methodName, Property.NOT_WRITABLE, getBoundBeanMethodGetter(source, 21.56 + method), null)); 21.57 } 21.58 for(final String propertyName: propertyNames) { 21.59 + MethodHandle getter; 21.60 + if(readablePropertyNames.contains(propertyName)) { 21.61 + try { 21.62 + getter = getBeanOperation(linker, "dyn:getProp:" + propertyName, getterType, source); 21.63 + } catch(final IllegalAccessError e) { 21.64 + // Presumably, this was a caller sensitive method. Ignore it and carry on. 21.65 + getter = Lookup.EMPTY_GETTER; 21.66 + } 21.67 + } else { 21.68 + getter = Lookup.EMPTY_GETTER; 21.69 + } 21.70 final boolean isWritable = writablePropertyNames.contains(propertyName); 21.71 - properties.add(AccessorProperty.create(propertyName, isWritable ? 0 : Property.NOT_WRITABLE, 21.72 - readablePropertyNames.contains(propertyName) ? getBeanOperation(linker, "dyn:getProp:" + propertyName, getterType, source) : Lookup.EMPTY_GETTER, 21.73 - isWritable ? getBeanOperation(linker, "dyn:setProp:" + propertyName, setterType, source) : Lookup.EMPTY_SETTER)); 21.74 + MethodHandle setter; 21.75 + if(isWritable) { 21.76 + try { 21.77 + setter = getBeanOperation(linker, "dyn:setProp:" + propertyName, setterType, source); 21.78 + } catch(final IllegalAccessError e) { 21.79 + // Presumably, this was a caller sensitive method. Ignore it and carry on. 21.80 + setter = Lookup.EMPTY_SETTER; 21.81 + } 21.82 + } else { 21.83 + setter = Lookup.EMPTY_SETTER; 21.84 + } 21.85 + if(getter != Lookup.EMPTY_GETTER || setter != Lookup.EMPTY_SETTER) { 21.86 + properties.add(AccessorProperty.create(propertyName, isWritable ? 0 : Property.NOT_WRITABLE, getter, setter)); 21.87 + } 21.88 } 21.89 21.90 targetObj.addBoundProperties(source, properties.toArray(new AccessorProperty[properties.size()]));
22.1 --- a/src/jdk/nashorn/internal/objects/NativeRegExp.java Thu Aug 29 09:42:13 2013 -0700 22.2 +++ b/src/jdk/nashorn/internal/objects/NativeRegExp.java Thu Aug 29 16:34:31 2013 -0700 22.3 @@ -65,10 +65,9 @@ 22.4 private RegExp regexp; 22.5 22.6 // Reference to global object needed to support static RegExp properties 22.7 - private Global globalObject; 22.8 + private final Global globalObject; 22.9 22.10 // initialized by nasgen 22.11 - @SuppressWarnings("unused") 22.12 private static PropertyMap $nasgenmap$; 22.13 22.14 static PropertyMap getInitialMap() {
23.1 --- a/src/jdk/nashorn/internal/objects/NativeString.java Thu Aug 29 09:42:13 2013 -0700 23.2 +++ b/src/jdk/nashorn/internal/objects/NativeString.java Thu Aug 29 16:34:31 2013 -0700 23.3 @@ -1058,9 +1058,8 @@ 23.4 public static Object trim(final Object self) { 23.5 23.6 final String str = checkObjectToString(self); 23.7 - final int len = str.length(); 23.8 int start = 0; 23.9 - int end = len - 1; 23.10 + int end = str.length() - 1; 23.11 23.12 while (start <= end && ScriptRuntime.isJSWhitespace(str.charAt(start))) { 23.13 start++; 23.14 @@ -1069,7 +1068,45 @@ 23.15 end--; 23.16 } 23.17 23.18 - return start == 0 && end + 1 == len ? str : str.substring(start, end + 1); 23.19 + return str.substring(start, end + 1); 23.20 + } 23.21 + 23.22 + /** 23.23 + * Nashorn extension: String.prototype.trimLeft ( ) 23.24 + * @param self self reference 23.25 + * @return string trimmed left from whitespace 23.26 + */ 23.27 + @Function(attributes = Attribute.NOT_ENUMERABLE) 23.28 + public static Object trimLeft(final Object self) { 23.29 + 23.30 + final String str = checkObjectToString(self); 23.31 + int start = 0; 23.32 + int end = str.length() - 1; 23.33 + 23.34 + while (start <= end && ScriptRuntime.isJSWhitespace(str.charAt(start))) { 23.35 + start++; 23.36 + } 23.37 + 23.38 + return str.substring(start, end + 1); 23.39 + } 23.40 + 23.41 + /** 23.42 + * Nashorn extension: String.prototype.trimRight ( ) 23.43 + * @param self self reference 23.44 + * @return string trimmed right from whitespace 23.45 + */ 23.46 + @Function(attributes = Attribute.NOT_ENUMERABLE) 23.47 + public static Object trimRight(final Object self) { 23.48 + 23.49 + final String str = checkObjectToString(self); 23.50 + int start = 0; 23.51 + int end = str.length() - 1; 23.52 + 23.53 + while (end >= start && ScriptRuntime.isJSWhitespace(str.charAt(end))) { 23.54 + end--; 23.55 + } 23.56 + 23.57 + return str.substring(start, end + 1); 23.58 } 23.59 23.60 private static Object newObj(final Object self, final CharSequence str) {
24.1 --- a/src/jdk/nashorn/internal/objects/NativeUint16Array.java Thu Aug 29 09:42:13 2013 -0700 24.2 +++ b/src/jdk/nashorn/internal/objects/NativeUint16Array.java Thu Aug 29 16:34:31 2013 -0700 24.3 @@ -108,6 +108,11 @@ 24.4 } 24.5 24.6 @Override 24.7 + public String getClassName() { 24.8 + return "Uint16Array"; 24.9 + } 24.10 + 24.11 + @Override 24.12 protected Factory factory() { 24.13 return FACTORY; 24.14 }
25.1 --- a/src/jdk/nashorn/internal/objects/NativeUint32Array.java Thu Aug 29 09:42:13 2013 -0700 25.2 +++ b/src/jdk/nashorn/internal/objects/NativeUint32Array.java Thu Aug 29 16:34:31 2013 -0700 25.3 @@ -127,6 +127,11 @@ 25.4 } 25.5 25.6 @Override 25.7 + public String getClassName() { 25.8 + return "Uint32Array"; 25.9 + } 25.10 + 25.11 + @Override 25.12 protected Factory factory() { 25.13 return FACTORY; 25.14 }
26.1 --- a/src/jdk/nashorn/internal/objects/NativeUint8Array.java Thu Aug 29 09:42:13 2013 -0700 26.2 +++ b/src/jdk/nashorn/internal/objects/NativeUint8Array.java Thu Aug 29 16:34:31 2013 -0700 26.3 @@ -101,6 +101,11 @@ 26.4 } 26.5 26.6 @Override 26.7 + public String getClassName() { 26.8 + return "Uint8Array"; 26.9 + } 26.10 + 26.11 + @Override 26.12 protected Factory factory() { 26.13 return FACTORY; 26.14 }
27.1 --- a/src/jdk/nashorn/internal/objects/NativeUint8ClampedArray.java Thu Aug 29 09:42:13 2013 -0700 27.2 +++ b/src/jdk/nashorn/internal/objects/NativeUint8ClampedArray.java Thu Aug 29 16:34:31 2013 -0700 27.3 @@ -118,6 +118,11 @@ 27.4 } 27.5 27.6 @Override 27.7 + public String getClassName() { 27.8 + return "Uint8ClampedArray"; 27.9 + } 27.10 + 27.11 + @Override 27.12 protected Factory factory() { 27.13 return FACTORY; 27.14 }
28.1 --- a/src/jdk/nashorn/internal/parser/TokenType.java Thu Aug 29 09:42:13 2013 -0700 28.2 +++ b/src/jdk/nashorn/internal/parser/TokenType.java Thu Aug 29 16:34:31 2013 -0700 28.3 @@ -284,7 +284,7 @@ 28.4 28.5 @Override 28.6 public String toString() { 28.7 - return name; 28.8 + return getNameOrType(); 28.9 } 28.10 28.11 static {
29.1 --- a/src/jdk/nashorn/internal/runtime/Context.java Thu Aug 29 09:42:13 2013 -0700 29.2 +++ b/src/jdk/nashorn/internal/runtime/Context.java Thu Aug 29 16:34:31 2013 -0700 29.3 @@ -91,6 +91,11 @@ 29.4 */ 29.5 public static final String NASHORN_JAVA_REFLECTION = "nashorn.JavaReflection"; 29.6 29.7 + /* Force DebuggerSupport to be loaded. */ 29.8 + static { 29.9 + DebuggerSupport.FORCELOAD = true; 29.10 + } 29.11 + 29.12 /** 29.13 * ContextCodeInstaller that has the privilege of installing classes in the Context. 29.14 * Can only be instantiated from inside the context and is opaque to other classes 29.15 @@ -889,7 +894,6 @@ 29.16 return script; 29.17 } 29.18 29.19 - @SuppressWarnings("static-method") 29.20 private ScriptLoader createNewLoader() { 29.21 return AccessController.doPrivileged( 29.22 new PrivilegedAction<ScriptLoader>() {
30.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 30.2 +++ b/src/jdk/nashorn/internal/runtime/DebuggerSupport.java Thu Aug 29 16:34:31 2013 -0700 30.3 @@ -0,0 +1,330 @@ 30.4 +/* 30.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 30.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 30.7 + * 30.8 + * This code is free software; you can redistribute it and/or modify it 30.9 + * under the terms of the GNU General License version 2 only, as 30.10 + * published by the Free Software Foundation. Oracle designates this 30.11 + * particular file as subject to the "Classpath" exception as provided 30.12 + * by Oracle in the LICENSE file that accompanied this code. 30.13 + * 30.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 30.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 30.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General License 30.17 + * version 2 for more details (a copy is included in the LICENSE file that 30.18 + * accompanied this code). 30.19 + * 30.20 + * You should have received a copy of the GNU General License version 30.21 + * 2 along with this work; if not, write to the Free Software Foundation, 30.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 30.23 + * 30.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 30.25 + * or visit www.oracle.com if you need additional information or have any 30.26 + * questions. 30.27 + */ 30.28 + 30.29 +package jdk.nashorn.internal.runtime; 30.30 + 30.31 +import java.util.HashSet; 30.32 +import java.util.Set; 30.33 + 30.34 +/** 30.35 + * This class provides support for external debuggers. Its primary purpose is 30.36 + * is to simplify the debugger tasks and provide better performance. 30.37 + */ 30.38 +final class DebuggerSupport { 30.39 + /** 30.40 + * Hook to force the loading of the DebuggerSupport class so that it is 30.41 + * available to external debuggers. 30.42 + */ 30.43 + static boolean FORCELOAD = true; 30.44 + 30.45 + static { 30.46 + /** 30.47 + * Hook to force the loading of the DebuggerValueDesc class so that it is 30.48 + * available to external debuggers. 30.49 + */ 30.50 + @SuppressWarnings("unused") 30.51 + DebuggerValueDesc forceLoad = new DebuggerValueDesc(null, false, null, null); 30.52 + } 30.53 + 30.54 + /** This class is used to send a bulk description of a value. */ 30.55 + static class DebuggerValueDesc { 30.56 + /** Property key (or index) or field name. */ 30.57 + final String key; 30.58 + 30.59 + /** If the value is expandable. */ 30.60 + final boolean expandable; 30.61 + 30.62 + /** Property or field value as object. */ 30.63 + final Object valueAsObject; 30.64 + 30.65 + /** Property or field value as string. */ 30.66 + final String valueAsString; 30.67 + 30.68 + DebuggerValueDesc(final String key, final boolean expandable, final Object valueAsObject, final String valueAsString) { 30.69 + this.key = key; 30.70 + this.expandable = expandable; 30.71 + this.valueAsObject = valueAsObject; 30.72 + this.valueAsString = valueAsString; 30.73 + } 30.74 + } 30.75 + 30.76 + /** 30.77 + * Return the current context global. 30.78 + * @return context global. 30.79 + */ 30.80 + static Object getGlobal() { 30.81 + return Context.getGlobalTrusted(); 30.82 + } 30.83 + 30.84 + /** 30.85 + * Call eval on the current global. 30.86 + * @param scope Scope to use. 30.87 + * @param self Receiver to use. 30.88 + * @param string String to evaluate. 30.89 + * @param returnException true if exceptions are to be returned. 30.90 + * @return Result of eval as string, or, an exception or null depending on returnException. 30.91 + */ 30.92 + static Object eval(final ScriptObject scope, final Object self, final String string, final boolean returnException) { 30.93 + final ScriptObject global = Context.getGlobalTrusted(); 30.94 + final ScriptObject initialScope = scope != null ? scope : global; 30.95 + final Object callThis = self != null ? self : global; 30.96 + final Context context = global.getContext(); 30.97 + 30.98 + try { 30.99 + return context.eval(initialScope, string, callThis, ScriptRuntime.UNDEFINED, false); 30.100 + } catch (Throwable ex) { 30.101 + return returnException ? ex : null; 30.102 + } 30.103 + } 30.104 + 30.105 + /** 30.106 + * This method returns a bulk description of an object's properties. 30.107 + * @param object Script object to be displayed by the debugger. 30.108 + * @param all true if to include non-enumerable values. 30.109 + * @return An array of DebuggerValueDesc. 30.110 + */ 30.111 + static DebuggerValueDesc[] valueInfos(final Object object, final boolean all) { 30.112 + assert object instanceof ScriptObject; 30.113 + return getDebuggerValueDescs((ScriptObject)object, all, new HashSet<>()); 30.114 + } 30.115 + 30.116 + /** 30.117 + * This method returns a debugger description of the value. 30.118 + * @param name Name of value (property name). 30.119 + * @param value Data value. 30.120 + * @param all true if to include non-enumerable values. 30.121 + * @return A DebuggerValueDesc. 30.122 + */ 30.123 + static DebuggerValueDesc valueInfo(final String name, final Object value, final boolean all) { 30.124 + return valueInfo(name, value, all, new HashSet<>()); 30.125 + } 30.126 + 30.127 + /** 30.128 + * This method returns a debugger description of the value. 30.129 + * @param name Name of value (property name). 30.130 + * @param value Data value. 30.131 + * @param all true if to include non-enumerable values. 30.132 + * @param duplicates Duplication set to avoid cycles. 30.133 + * @return A DebuggerValueDesc. 30.134 + */ 30.135 + private static DebuggerValueDesc valueInfo(final String name, final Object value, final boolean all, final Set<Object> duplicates) { 30.136 + if (value instanceof ScriptObject && !(value instanceof ScriptFunction)) { 30.137 + final ScriptObject object = (ScriptObject)value; 30.138 + return new DebuggerValueDesc(name, !object.isEmpty(), value, objectAsString(object, all, duplicates)); 30.139 + } 30.140 + return new DebuggerValueDesc(name, false, value, valueAsString(value)); 30.141 + } 30.142 + 30.143 + /** 30.144 + * Generate the descriptions for an object's properties. 30.145 + * @param object Object to introspect. 30.146 + * @param all true if to include non-enumerable values. 30.147 + * @param duplicates Duplication set to avoid cycles. 30.148 + * @return An array of DebuggerValueDesc. 30.149 + */ 30.150 + private static DebuggerValueDesc[] getDebuggerValueDescs(final ScriptObject object, final boolean all, final Set<Object> duplicates) { 30.151 + if (duplicates.contains(object)) { 30.152 + return null; 30.153 + } 30.154 + 30.155 + duplicates.add(object); 30.156 + 30.157 + final String[] keys = object.getOwnKeys(all); 30.158 + final DebuggerValueDesc[] descs = new DebuggerValueDesc[keys.length]; 30.159 + 30.160 + for (int i = 0; i < keys.length; i++) { 30.161 + final String key = keys[i]; 30.162 + descs[i] = valueInfo(key, object.get(key), all, duplicates); 30.163 + } 30.164 + 30.165 + duplicates.remove(object); 30.166 + 30.167 + return descs; 30.168 + } 30.169 + 30.170 + /** 30.171 + * Generate a string representation of a Script object. 30.172 + * @param object Script object to represent. 30.173 + * @param all true if to include non-enumerable values. 30.174 + * @param duplicates Duplication set to avoid cycles. 30.175 + * @return String representation. 30.176 + */ 30.177 + private static String objectAsString(final ScriptObject object, final boolean all, final Set<Object> duplicates) { 30.178 + final StringBuilder sb = new StringBuilder(); 30.179 + 30.180 + if (ScriptObject.isArray(object)) { 30.181 + sb.append('['); 30.182 + final long length = object.getLong("length"); 30.183 + 30.184 + for (long i = 0; i < length; i++) { 30.185 + if (object.has(i)) { 30.186 + final Object valueAsObject = object.get(i); 30.187 + final boolean isUndefined = JSType.of(valueAsObject) == JSType.UNDEFINED; 30.188 + 30.189 + if (isUndefined) { 30.190 + if (i != 0) { 30.191 + sb.append(","); 30.192 + } 30.193 + } else { 30.194 + if (i != 0) { 30.195 + sb.append(", "); 30.196 + } 30.197 + 30.198 + if (valueAsObject instanceof ScriptObject && !(valueAsObject instanceof ScriptFunction)) { 30.199 + final String objectString = objectAsString((ScriptObject)valueAsObject, all, duplicates); 30.200 + sb.append(objectString != null ? objectString : "{...}"); 30.201 + } else { 30.202 + sb.append(valueAsString(valueAsObject)); 30.203 + } 30.204 + } 30.205 + } else { 30.206 + if (i != 0) { 30.207 + sb.append(','); 30.208 + } 30.209 + } 30.210 + } 30.211 + 30.212 + sb.append(']'); 30.213 + } else { 30.214 + sb.append('{'); 30.215 + final DebuggerValueDesc[] descs = getDebuggerValueDescs(object, all, duplicates); 30.216 + 30.217 + if (descs != null) { 30.218 + for (int i = 0; i < descs.length; i++) { 30.219 + if (i != 0) { 30.220 + sb.append(", "); 30.221 + } 30.222 + 30.223 + final String valueAsString = descs[i].valueAsString; 30.224 + sb.append(descs[i].key); 30.225 + sb.append(": "); 30.226 + sb.append(valueAsString); 30.227 + } 30.228 + } 30.229 + 30.230 + sb.append('}'); 30.231 + } 30.232 + 30.233 + return sb.toString(); 30.234 + } 30.235 + 30.236 + /** 30.237 + * This method returns a string representation of a value. 30.238 + * @param value Arbitrary value to be displayed by the debugger. 30.239 + * @return A string representation of the value or an array of DebuggerValueDesc. 30.240 + */ 30.241 + private static String valueAsString(final Object value) { 30.242 + final JSType type = JSType.of(value); 30.243 + 30.244 + switch (type) { 30.245 + case BOOLEAN: 30.246 + return value.toString(); 30.247 + 30.248 + case STRING: 30.249 + return escape((String)value); 30.250 + 30.251 + case NUMBER: 30.252 + return JSType.toString(((Number)value).doubleValue()); 30.253 + 30.254 + case NULL: 30.255 + return "null"; 30.256 + 30.257 + case UNDEFINED: 30.258 + return "undefined"; 30.259 + 30.260 + case OBJECT: 30.261 + return ScriptRuntime.safeToString(value); 30.262 + 30.263 + case FUNCTION: 30.264 + if (value instanceof ScriptFunction) { 30.265 + return ((ScriptFunction)value).toSource(); 30.266 + } 30.267 + return value.toString(); 30.268 + 30.269 + default: 30.270 + return value.toString(); 30.271 + } 30.272 + } 30.273 + 30.274 + /** 30.275 + * Escape a string into a form that can be parsed by JavaScript. 30.276 + * @param value String to be escaped. 30.277 + * @return Escaped string. 30.278 + */ 30.279 + private static String escape(final String value) { 30.280 + final StringBuilder sb = new StringBuilder(); 30.281 + 30.282 + sb.append("\""); 30.283 + 30.284 + for (final char ch : value.toCharArray()) { 30.285 + switch (ch) { 30.286 + case '\\': 30.287 + sb.append("\\\\"); 30.288 + break; 30.289 + case '"': 30.290 + sb.append("\\\""); 30.291 + break; 30.292 + case '\'': 30.293 + sb.append("\\\'"); 30.294 + break; 30.295 + case '\b': 30.296 + sb.append("\\b"); 30.297 + break; 30.298 + case '\f': 30.299 + sb.append("\\f"); 30.300 + break; 30.301 + case '\n': 30.302 + sb.append("\\n"); 30.303 + break; 30.304 + case '\r': 30.305 + sb.append("\\r"); 30.306 + break; 30.307 + case '\t': 30.308 + sb.append("\\t"); 30.309 + break; 30.310 + default: 30.311 + if (ch < ' ' || ch >= 0xFF) { 30.312 + sb.append("\\u"); 30.313 + 30.314 + final String hex = Integer.toHexString(ch); 30.315 + for (int i = hex.length(); i < 4; i++) { 30.316 + sb.append('0'); 30.317 + } 30.318 + sb.append(hex); 30.319 + } else { 30.320 + sb.append(ch); 30.321 + } 30.322 + 30.323 + break; 30.324 + } 30.325 + } 30.326 + 30.327 + sb.append("\""); 30.328 + 30.329 + return sb.toString(); 30.330 + } 30.331 +} 30.332 + 30.333 +
31.1 --- a/src/jdk/nashorn/internal/runtime/JSType.java Thu Aug 29 09:42:13 2013 -0700 31.2 +++ b/src/jdk/nashorn/internal/runtime/JSType.java Thu Aug 29 16:34:31 2013 -0700 31.3 @@ -30,11 +30,10 @@ 31.4 31.5 import java.lang.invoke.MethodHandles; 31.6 import java.util.Locale; 31.7 -import jdk.internal.dynalink.beans.BeansLinker; 31.8 import jdk.internal.dynalink.beans.StaticClass; 31.9 -import jdk.nashorn.api.scripting.ScriptObjectMirror; 31.10 import jdk.nashorn.internal.codegen.CompilerConstants.Call; 31.11 import jdk.nashorn.internal.parser.Lexer; 31.12 +import jdk.nashorn.internal.runtime.linker.Bootstrap; 31.13 31.14 /** 31.15 * Representation for ECMAScript types - this maps directly to the ECMA script standard 31.16 @@ -148,22 +147,10 @@ 31.17 return JSType.STRING; 31.18 } 31.19 31.20 - if (obj instanceof ScriptObject) { 31.21 - return (obj instanceof ScriptFunction) ? JSType.FUNCTION : JSType.OBJECT; 31.22 - } 31.23 - 31.24 - if (obj instanceof StaticClass) { 31.25 + if (Bootstrap.isCallable(obj)) { 31.26 return JSType.FUNCTION; 31.27 } 31.28 31.29 - if (BeansLinker.isDynamicMethod(obj)) { 31.30 - return JSType.FUNCTION; 31.31 - } 31.32 - 31.33 - if (obj instanceof ScriptObjectMirror) { 31.34 - return ((ScriptObjectMirror)obj).isFunction()? JSType.FUNCTION : JSType.OBJECT; 31.35 - } 31.36 - 31.37 return JSType.OBJECT; 31.38 } 31.39
32.1 --- a/src/jdk/nashorn/internal/runtime/PropertyListener.java Thu Aug 29 09:42:13 2013 -0700 32.2 +++ b/src/jdk/nashorn/internal/runtime/PropertyListener.java Thu Aug 29 16:34:31 2013 -0700 32.3 @@ -54,4 +54,13 @@ 32.4 * 32.5 */ 32.6 public void propertyModified(ScriptObject object, Property oldProp, Property newProp); 32.7 + 32.8 + /** 32.9 + * Given object's __proto__ has changed. 32.10 + * 32.11 + * @param object object whose __proto__ has changed. 32.12 + * @param oldProto old __proto__ 32.13 + * @param newProto new __proto__ 32.14 + */ 32.15 + public void protoChanged(ScriptObject object, ScriptObject oldProto, ScriptObject newProto); 32.16 }
33.1 --- a/src/jdk/nashorn/internal/runtime/PropertyListenerManager.java Thu Aug 29 09:42:13 2013 -0700 33.2 +++ b/src/jdk/nashorn/internal/runtime/PropertyListenerManager.java Thu Aug 29 16:34:31 2013 -0700 33.3 @@ -140,6 +140,21 @@ 33.4 } 33.5 } 33.6 33.7 + /** 33.8 + * This method can be called to notify __proto__ modification to this object's listeners. 33.9 + * 33.10 + * @param object The ScriptObject whose __proto__ was changed. 33.11 + * @param oldProto old __proto__ 33.12 + * @param newProto new __proto__ 33.13 + */ 33.14 + protected synchronized final void notifyProtoChanged(final ScriptObject object, final ScriptObject oldProto, final ScriptObject newProto) { 33.15 + if (listeners != null) { 33.16 + for (PropertyListener listener : listeners.keySet()) { 33.17 + listener.protoChanged(object, oldProto, newProto); 33.18 + } 33.19 + } 33.20 + } 33.21 + 33.22 // PropertyListener methods 33.23 33.24 @Override 33.25 @@ -156,4 +171,9 @@ 33.26 public final void propertyModified(final ScriptObject object, final Property oldProp, final Property newProp) { 33.27 notifyPropertyModified(object, oldProp, newProp); 33.28 } 33.29 + 33.30 + @Override 33.31 + public final void protoChanged(final ScriptObject object, final ScriptObject oldProto, final ScriptObject newProto) { 33.32 + notifyProtoChanged(object, oldProto, newProto); 33.33 + } 33.34 }
34.1 --- a/src/jdk/nashorn/internal/runtime/PropertyMap.java Thu Aug 29 09:42:13 2013 -0700 34.2 +++ b/src/jdk/nashorn/internal/runtime/PropertyMap.java Thu Aug 29 16:34:31 2013 -0700 34.3 @@ -230,7 +230,7 @@ 34.4 } 34.5 34.6 /** 34.7 - * Indicate that a prototype property hash changed. 34.8 + * Indicate that a prototype property has changed. 34.9 * 34.10 * @param property {@link Property} to invalidate. 34.11 */ 34.12 @@ -251,6 +251,18 @@ 34.13 } 34.14 34.15 /** 34.16 + * Indicate that proto itself has changed in hierachy somewhere. 34.17 + */ 34.18 + private void invalidateAllProtoGetSwitchPoints() { 34.19 + assert !isShared() : "proto invalidation on a shared PropertyMap"; 34.20 + 34.21 + if (protoGetSwitches != null) { 34.22 + final Collection<SwitchPoint> sws = protoGetSwitches.values(); 34.23 + SwitchPoint.invalidateAll(sws.toArray(new SwitchPoint[sws.size()])); 34.24 + } 34.25 + } 34.26 + 34.27 + /** 34.28 * Add a property to the map, re-binding its getters and setters, 34.29 * if available, to a given receiver. This is typically the global scope. See 34.30 * {@link ScriptObject#addBoundProperties(ScriptObject)} 34.31 @@ -878,6 +890,15 @@ 34.32 invalidateProtoGetSwitchPoint(oldProp); 34.33 } 34.34 34.35 + @Override 34.36 + public void protoChanged(final ScriptObject object, final ScriptObject oldProto, final ScriptObject newProto) { 34.37 + // We may walk and invalidate SwitchPoints for properties inherited 34.38 + // from 'object' or it's old proto chain. But, it may not be worth it. 34.39 + // For example, a new proto may have a user defined getter/setter for 34.40 + // a data property down the chain. So, invalidating all is better. 34.41 + invalidateAllProtoGetSwitchPoints(); 34.42 + } 34.43 + 34.44 /* 34.45 * Debugging and statistics. 34.46 */
35.1 --- a/src/jdk/nashorn/internal/runtime/ScriptEnvironment.java Thu Aug 29 09:42:13 2013 -0700 35.2 +++ b/src/jdk/nashorn/internal/runtime/ScriptEnvironment.java Thu Aug 29 16:34:31 2013 -0700 35.3 @@ -86,6 +86,9 @@ 35.4 /** Launch using as fx application */ 35.5 public final boolean _fx; 35.6 35.7 + /** Use single Global instance per jsr223 engine instance. */ 35.8 + public final boolean _global_per_engine; 35.9 + 35.10 /** 35.11 * Behavior when encountering a function declaration in a lexical context where only statements are acceptable 35.12 * (function declarations are source elements, but not statements). 35.13 @@ -128,9 +131,6 @@ 35.14 /** Do not support typed arrays. */ 35.15 public final boolean _no_typed_arrays; 35.16 35.17 - /** Package to which generated class files are added */ 35.18 - public final String _package; 35.19 - 35.20 /** Only parse the source code, do not compile */ 35.21 public final boolean _parse_only; 35.22 35.23 @@ -211,12 +211,12 @@ 35.24 _function_statement = FunctionStatementBehavior.ACCEPT; 35.25 } 35.26 _fx = options.getBoolean("fx"); 35.27 + _global_per_engine = options.getBoolean("global.per.engine"); 35.28 _lazy_compilation = options.getBoolean("lazy.compilation"); 35.29 _loader_per_compile = options.getBoolean("loader.per.compile"); 35.30 _no_java = options.getBoolean("no.java"); 35.31 _no_syntax_extensions = options.getBoolean("no.syntax.extensions"); 35.32 _no_typed_arrays = options.getBoolean("no.typed.arrays"); 35.33 - _package = options.getString("package"); 35.34 _parse_only = options.getBoolean("parse.only"); 35.35 _print_ast = options.getBoolean("print.ast"); 35.36 _print_lower_ast = options.getBoolean("print.lower.ast");
36.1 --- a/src/jdk/nashorn/internal/runtime/ScriptObject.java Thu Aug 29 09:42:13 2013 -0700 36.2 +++ b/src/jdk/nashorn/internal/runtime/ScriptObject.java Thu Aug 29 16:34:31 2013 -0700 36.3 @@ -52,6 +52,7 @@ 36.4 import java.util.List; 36.5 import java.util.Map; 36.6 import java.util.Set; 36.7 + 36.8 import jdk.internal.dynalink.CallSiteDescriptor; 36.9 import jdk.internal.dynalink.linker.GuardedInvocation; 36.10 import jdk.internal.dynalink.linker.LinkRequest; 36.11 @@ -1008,10 +1009,6 @@ 36.12 return getMap().findProperty(key); 36.13 } 36.14 36.15 - static String convertKey(final Object key) { 36.16 - return (key instanceof String) ? (String)key : JSType.toString(key); 36.17 - } 36.18 - 36.19 /** 36.20 * Overridden by {@link jdk.nashorn.internal.objects.NativeArguments} class (internal use.) 36.21 * Used for argument access in a vararg function using parameter name. 36.22 @@ -1129,6 +1126,9 @@ 36.23 proto = newProto; 36.24 36.25 if (isPrototype()) { 36.26 + // tell listeners that my __proto__ has been changed 36.27 + notifyProtoChanged(this, oldProto, newProto); 36.28 + 36.29 if (oldProto != null) { 36.30 oldProto.removePropertyListener(this); 36.31 } 36.32 @@ -1144,7 +1144,19 @@ 36.33 * @param newProto Prototype to set. 36.34 */ 36.35 public final void setProtoCheck(final Object newProto) { 36.36 + if (!isExtensible()) { 36.37 + throw typeError("__proto__.set.non.extensible", ScriptRuntime.safeToString(this)); 36.38 + } 36.39 + 36.40 if (newProto == null || newProto instanceof ScriptObject) { 36.41 + // check for circularity 36.42 + ScriptObject p = (ScriptObject)newProto; 36.43 + while (p != null) { 36.44 + if (p == this) { 36.45 + throw typeError("circular.__proto__.set", ScriptRuntime.safeToString(this)); 36.46 + } 36.47 + p = p.getProto(); 36.48 + } 36.49 setProto((ScriptObject)newProto); 36.50 } else { 36.51 final ScriptObject global = Context.getGlobalTrusted(); 36.52 @@ -2006,6 +2018,7 @@ 36.53 * @param request the link request 36.54 * @return GuardedInvocation to be invoked at call site. 36.55 */ 36.56 + @SuppressWarnings("null") 36.57 public GuardedInvocation noSuchProperty(final CallSiteDescriptor desc, final LinkRequest request) { 36.58 final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND); 36.59 final FindProperty find = findProperty(NO_SUCH_PROPERTY_NAME, true); 36.60 @@ -2359,7 +2372,7 @@ 36.61 return array.getInt(index); 36.62 } 36.63 36.64 - return getInt(index, convertKey(key)); 36.65 + return getInt(index, JSType.toString(key)); 36.66 } 36.67 36.68 @Override 36.69 @@ -2371,7 +2384,7 @@ 36.70 return array.getInt(index); 36.71 } 36.72 36.73 - return getInt(index, convertKey(key)); 36.74 + return getInt(index, JSType.toString(key)); 36.75 } 36.76 36.77 @Override 36.78 @@ -2383,7 +2396,7 @@ 36.79 return array.getInt(index); 36.80 } 36.81 36.82 - return getInt(index, convertKey(key)); 36.83 + return getInt(index, JSType.toString(key)); 36.84 } 36.85 36.86 @Override 36.87 @@ -2394,7 +2407,7 @@ 36.88 return array.getInt(key); 36.89 } 36.90 36.91 - return getInt(key, convertKey(key)); 36.92 + return getInt(key, JSType.toString(key)); 36.93 } 36.94 36.95 private long getLong(final int index, final String key) { 36.96 @@ -2436,7 +2449,7 @@ 36.97 return array.getLong(index); 36.98 } 36.99 36.100 - return getLong(index, convertKey(key)); 36.101 + return getLong(index, JSType.toString(key)); 36.102 } 36.103 36.104 @Override 36.105 @@ -2448,7 +2461,7 @@ 36.106 return array.getLong(index); 36.107 } 36.108 36.109 - return getLong(index, convertKey(key)); 36.110 + return getLong(index, JSType.toString(key)); 36.111 } 36.112 36.113 @Override 36.114 @@ -2460,7 +2473,7 @@ 36.115 return array.getLong(index); 36.116 } 36.117 36.118 - return getLong(index, convertKey(key)); 36.119 + return getLong(index, JSType.toString(key)); 36.120 } 36.121 36.122 @Override 36.123 @@ -2471,7 +2484,7 @@ 36.124 return array.getLong(key); 36.125 } 36.126 36.127 - return getLong(key, convertKey(key)); 36.128 + return getLong(key, JSType.toString(key)); 36.129 } 36.130 36.131 private double getDouble(final int index, final String key) { 36.132 @@ -2513,7 +2526,7 @@ 36.133 return array.getDouble(index); 36.134 } 36.135 36.136 - return getDouble(index, convertKey(key)); 36.137 + return getDouble(index, JSType.toString(key)); 36.138 } 36.139 36.140 @Override 36.141 @@ -2525,7 +2538,7 @@ 36.142 return array.getDouble(index); 36.143 } 36.144 36.145 - return getDouble(index, convertKey(key)); 36.146 + return getDouble(index, JSType.toString(key)); 36.147 } 36.148 36.149 @Override 36.150 @@ -2537,7 +2550,7 @@ 36.151 return array.getDouble(index); 36.152 } 36.153 36.154 - return getDouble(index, convertKey(key)); 36.155 + return getDouble(index, JSType.toString(key)); 36.156 } 36.157 36.158 @Override 36.159 @@ -2548,7 +2561,7 @@ 36.160 return array.getDouble(key); 36.161 } 36.162 36.163 - return getDouble(key, convertKey(key)); 36.164 + return getDouble(key, JSType.toString(key)); 36.165 } 36.166 36.167 private Object get(final int index, final String key) { 36.168 @@ -2590,7 +2603,7 @@ 36.169 return array.getObject(index); 36.170 } 36.171 36.172 - return get(index, convertKey(key)); 36.173 + return get(index, JSType.toString(key)); 36.174 } 36.175 36.176 @Override 36.177 @@ -2602,7 +2615,7 @@ 36.178 return array.getObject(index); 36.179 } 36.180 36.181 - return get(index, convertKey(key)); 36.182 + return get(index, JSType.toString(key)); 36.183 } 36.184 36.185 @Override 36.186 @@ -2614,7 +2627,7 @@ 36.187 return array.getObject(index); 36.188 } 36.189 36.190 - return get(index, convertKey(key)); 36.191 + return get(index, JSType.toString(key)); 36.192 } 36.193 36.194 @Override 36.195 @@ -2625,7 +2638,7 @@ 36.196 return array.getObject(key); 36.197 } 36.198 36.199 - return get(key, convertKey(key)); 36.200 + return get(key, JSType.toString(key)); 36.201 } 36.202 36.203 /** 36.204 @@ -2640,7 +2653,7 @@ 36.205 final long longIndex = index & JSType.MAX_UINT; 36.206 36.207 if (!getArray().has(index)) { 36.208 - final String key = convertKey(longIndex); 36.209 + final String key = JSType.toString(longIndex); 36.210 final FindProperty find = findProperty(key, true); 36.211 36.212 if (find != null) { 36.213 @@ -2786,7 +2799,7 @@ 36.214 return; 36.215 } 36.216 36.217 - final String propName = convertKey(key); 36.218 + final String propName = JSType.toString(key); 36.219 final FindProperty find = findProperty(propName, true); 36.220 36.221 setObject(find, strict, propName, value); 36.222 @@ -3008,7 +3021,7 @@ 36.223 } 36.224 } 36.225 36.226 - final FindProperty find = findProperty(convertKey(key), true); 36.227 + final FindProperty find = findProperty(JSType.toString(key), true); 36.228 36.229 return find != null; 36.230 } 36.231 @@ -3025,7 +3038,7 @@ 36.232 } 36.233 } 36.234 36.235 - final FindProperty find = findProperty(convertKey(key), true); 36.236 + final FindProperty find = findProperty(JSType.toString(key), true); 36.237 36.238 return find != null; 36.239 } 36.240 @@ -3042,7 +3055,7 @@ 36.241 } 36.242 } 36.243 36.244 - final FindProperty find = findProperty(convertKey(key), true); 36.245 + final FindProperty find = findProperty(JSType.toString(key), true); 36.246 36.247 return find != null; 36.248 } 36.249 @@ -3059,7 +3072,7 @@ 36.250 } 36.251 } 36.252 36.253 - final FindProperty find = findProperty(convertKey(key), true); 36.254 + final FindProperty find = findProperty(JSType.toString(key), true); 36.255 36.256 return find != null; 36.257 } 36.258 @@ -3072,7 +3085,7 @@ 36.259 return true; 36.260 } 36.261 36.262 - final FindProperty find = findProperty(convertKey(key), false); 36.263 + final FindProperty find = findProperty(JSType.toString(key), false); 36.264 36.265 return find != null; 36.266 } 36.267 @@ -3085,7 +3098,7 @@ 36.268 return true; 36.269 } 36.270 36.271 - final FindProperty find = findProperty(convertKey(key), false); 36.272 + final FindProperty find = findProperty(JSType.toString(key), false); 36.273 36.274 return find != null; 36.275 } 36.276 @@ -3098,7 +3111,7 @@ 36.277 return true; 36.278 } 36.279 36.280 - final FindProperty find = findProperty(convertKey(key), false); 36.281 + final FindProperty find = findProperty(JSType.toString(key), false); 36.282 36.283 return find != null; 36.284 } 36.285 @@ -3111,7 +3124,7 @@ 36.286 return true; 36.287 } 36.288 36.289 - final FindProperty find = findProperty(convertKey(key), false); 36.290 + final FindProperty find = findProperty(JSType.toString(key), false); 36.291 36.292 return find != null; 36.293 } 36.294 @@ -3181,7 +3194,7 @@ 36.295 } 36.296 36.297 private boolean deleteObject(final Object key, final boolean strict) { 36.298 - final String propName = convertKey(key); 36.299 + final String propName = JSType.toString(key); 36.300 final FindProperty find = findProperty(propName, false); 36.301 36.302 if (find == null) {
37.1 --- a/src/jdk/nashorn/internal/runtime/ScriptRuntime.java Thu Aug 29 09:42:13 2013 -0700 37.2 +++ b/src/jdk/nashorn/internal/runtime/ScriptRuntime.java Thu Aug 29 16:34:31 2013 -0700 37.3 @@ -37,7 +37,9 @@ 37.4 import java.lang.reflect.Array; 37.5 import java.util.Collections; 37.6 import java.util.Iterator; 37.7 +import java.util.List; 37.8 import java.util.Locale; 37.9 +import java.util.Map; 37.10 import java.util.NoSuchElementException; 37.11 import java.util.Objects; 37.12 import jdk.internal.dynalink.beans.StaticClass; 37.13 @@ -188,6 +190,8 @@ 37.14 case FUNCTION: 37.15 if (self instanceof ScriptObject) { 37.16 className = ((ScriptObject)self).getClassName(); 37.17 + } else if (self instanceof ScriptObjectMirror) { 37.18 + className = ((ScriptObjectMirror)self).getClassName(); 37.19 } else { 37.20 className = self.getClass().getName(); 37.21 } 37.22 @@ -221,49 +225,71 @@ 37.23 } 37.24 37.25 /** 37.26 - * Used to determine property iterator used in for in. 37.27 - * @param obj Object to iterate on. 37.28 - * @return Iterator. 37.29 + * Returns an iterator over property identifiers used in the {@code for...in} statement. Note that the ECMAScript 37.30 + * 5.1 specification, chapter 12.6.4. uses the terminology "property names", which seems to imply that the property 37.31 + * identifiers are expected to be strings, but this is not actually spelled out anywhere, and Nashorn will in some 37.32 + * cases deviate from this. Namely, we guarantee to always return an iterator over {@link String} values for any 37.33 + * built-in JavaScript object. We will however return an iterator over {@link Integer} objects for native Java 37.34 + * arrays and {@link List} objects, as well as arbitrary objects representing keys of a {@link Map}. Therefore, the 37.35 + * expression {@code typeof i} within a {@code for(i in obj)} statement can return something other than 37.36 + * {@code string} when iterating over native Java arrays, {@code List}, and {@code Map} objects. 37.37 + * @param obj object to iterate on. 37.38 + * @return iterator over the object's property names. 37.39 */ 37.40 - public static Iterator<String> toPropertyIterator(final Object obj) { 37.41 + public static Iterator<?> toPropertyIterator(final Object obj) { 37.42 if (obj instanceof ScriptObject) { 37.43 return ((ScriptObject)obj).propertyIterator(); 37.44 } 37.45 37.46 if (obj != null && obj.getClass().isArray()) { 37.47 - final int length = Array.getLength(obj); 37.48 - 37.49 - return new Iterator<String>() { 37.50 - private int index = 0; 37.51 - 37.52 - @Override 37.53 - public boolean hasNext() { 37.54 - return index < length; 37.55 - } 37.56 - 37.57 - @Override 37.58 - public String next() { 37.59 - return "" + index++; //TODO numeric property iterator? 37.60 - } 37.61 - 37.62 - @Override 37.63 - public void remove() { 37.64 - throw new UnsupportedOperationException(); 37.65 - } 37.66 - }; 37.67 + return new RangeIterator(Array.getLength(obj)); 37.68 } 37.69 37.70 if (obj instanceof ScriptObjectMirror) { 37.71 return ((ScriptObjectMirror)obj).keySet().iterator(); 37.72 } 37.73 37.74 + if (obj instanceof List) { 37.75 + return new RangeIterator(((List<?>)obj).size()); 37.76 + } 37.77 + 37.78 + if (obj instanceof Map) { 37.79 + return ((Map<?,?>)obj).keySet().iterator(); 37.80 + } 37.81 + 37.82 return Collections.emptyIterator(); 37.83 } 37.84 37.85 + private static final class RangeIterator implements Iterator<Integer> { 37.86 + private final int length; 37.87 + private int index; 37.88 + 37.89 + RangeIterator(int length) { 37.90 + this.length = length; 37.91 + } 37.92 + 37.93 + @Override 37.94 + public boolean hasNext() { 37.95 + return index < length; 37.96 + } 37.97 + 37.98 + @Override 37.99 + public Integer next() { 37.100 + return index++; 37.101 + } 37.102 + 37.103 + @Override 37.104 + public void remove() { 37.105 + throw new UnsupportedOperationException(); 37.106 + } 37.107 + } 37.108 + 37.109 /** 37.110 - * Used to determine property value iterator used in for each in. 37.111 - * @param obj Object to iterate on. 37.112 - * @return Iterator. 37.113 + * Returns an iterator over property values used in the {@code for each...in} statement. Aside from built-in JS 37.114 + * objects, it also operates on Java arrays, any {@link Iterable}, as well as on {@link Map} objects, iterating over 37.115 + * map values. 37.116 + * @param obj object to iterate on. 37.117 + * @return iterator over the object's property values. 37.118 */ 37.119 public static Iterator<?> toValueIterator(final Object obj) { 37.120 if (obj instanceof ScriptObject) { 37.121 @@ -301,6 +327,10 @@ 37.122 return ((ScriptObjectMirror)obj).values().iterator(); 37.123 } 37.124 37.125 + if (obj instanceof Map) { 37.126 + return ((Map<?,?>)obj).values().iterator(); 37.127 + } 37.128 + 37.129 if (obj instanceof Iterable) { 37.130 return ((Iterable<?>)obj).iterator(); 37.131 }
38.1 --- a/src/jdk/nashorn/internal/runtime/WithObject.java Thu Aug 29 09:42:13 2013 -0700 38.2 +++ b/src/jdk/nashorn/internal/runtime/WithObject.java Thu Aug 29 16:34:31 2013 -0700 38.3 @@ -73,7 +73,7 @@ 38.4 public boolean delete(final Object key, final boolean strict) { 38.5 if (expression instanceof ScriptObject) { 38.6 final ScriptObject self = (ScriptObject)expression; 38.7 - final String propName = ScriptObject.convertKey(key); 38.8 + final String propName = JSType.toString(key); 38.9 38.10 final FindProperty find = self.findProperty(propName, true); 38.11
39.1 --- a/src/jdk/nashorn/internal/runtime/linker/Bootstrap.java Thu Aug 29 09:42:13 2013 -0700 39.2 +++ b/src/jdk/nashorn/internal/runtime/linker/Bootstrap.java Thu Aug 29 16:34:31 2013 -0700 39.3 @@ -36,6 +36,7 @@ 39.4 import jdk.internal.dynalink.DynamicLinker; 39.5 import jdk.internal.dynalink.DynamicLinkerFactory; 39.6 import jdk.internal.dynalink.beans.BeansLinker; 39.7 +import jdk.internal.dynalink.beans.StaticClass; 39.8 import jdk.internal.dynalink.linker.GuardedInvocation; 39.9 import jdk.internal.dynalink.linker.LinkerServices; 39.10 import jdk.nashorn.api.scripting.ScriptObjectMirror; 39.11 @@ -61,7 +62,7 @@ 39.12 static { 39.13 final DynamicLinkerFactory factory = new DynamicLinkerFactory(); 39.14 factory.setPrioritizedLinkers(new NashornLinker(), new NashornPrimitiveLinker(), new NashornStaticClassLinker(), 39.15 - new BoundDynamicMethodLinker(), new JSObjectLinker(), new ReflectionCheckLinker()); 39.16 + new BoundDynamicMethodLinker(), new JavaSuperAdapterLinker(), new JSObjectLinker(), new ReflectionCheckLinker()); 39.17 factory.setFallbackLinkers(new BeansLinker(), new NashornBottomLinker()); 39.18 factory.setSyncOnRelink(true); 39.19 final int relinkThreshold = Options.getIntProperty("nashorn.unstable.relink.threshold", -1); 39.20 @@ -88,7 +89,8 @@ 39.21 return obj instanceof ScriptFunction || 39.22 ((obj instanceof ScriptObjectMirror) && ((ScriptObjectMirror)obj).isFunction()) || 39.23 isDynamicMethod(obj) || 39.24 - isFunctionalInterfaceObject(obj); 39.25 + isFunctionalInterfaceObject(obj) || 39.26 + obj instanceof StaticClass; 39.27 } 39.28 39.29 /** 39.30 @@ -262,6 +264,16 @@ 39.31 } 39.32 39.33 /** 39.34 + * Creates a super-adapter for an adapter, that is, an adapter to the adapter that allows invocation of superclass 39.35 + * methods on it. 39.36 + * @param adapter the original adapter 39.37 + * @return a new adapter that can be used to invoke super methods on the original adapter. 39.38 + */ 39.39 + public static Object createSuperAdapter(final Object adapter) { 39.40 + return new JavaSuperAdapter(adapter); 39.41 + } 39.42 + 39.43 + /** 39.44 * If the given class is a reflection-specific class (anything in {@code java.lang.reflect} and 39.45 * {@code java.lang.invoke} package, as well a {@link Class} and any subclass of {@link ClassLoader}) and there is 39.46 * a security manager in the system, then it checks the {@code nashorn.JavaReflection} {@code RuntimePermission}.
40.1 --- a/src/jdk/nashorn/internal/runtime/linker/BoundDynamicMethodLinker.java Thu Aug 29 09:42:13 2013 -0700 40.2 +++ b/src/jdk/nashorn/internal/runtime/linker/BoundDynamicMethodLinker.java Thu Aug 29 16:34:31 2013 -0700 40.3 @@ -67,11 +67,12 @@ 40.4 // BeansLinker. 40.5 final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor(); 40.6 final MethodType type = descriptor.getMethodType(); 40.7 + final Class<?> dynamicMethodClass = dynamicMethod.getClass(); 40.8 final CallSiteDescriptor newDescriptor = descriptor.changeMethodType( 40.9 - type.changeParameterType(0, dynamicMethod.getClass()).changeParameterType(1, boundThis.getClass())); 40.10 + type.changeParameterType(0, dynamicMethodClass).changeParameterType(1, boundThis.getClass())); 40.11 40.12 // Delegate to BeansLinker 40.13 - final GuardedInvocation inv = BeansLinker.getLinkerForClass(dynamicMethod.getClass()).getGuardedInvocation( 40.14 + final GuardedInvocation inv = BeansLinker.getLinkerForClass(dynamicMethodClass).getGuardedInvocation( 40.15 linkRequest.replaceArguments(newDescriptor, args), linkerServices); 40.16 if(inv == null) { 40.17 return null;
41.1 --- a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterBytecodeGenerator.java Thu Aug 29 09:42:13 2013 -0700 41.2 +++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterBytecodeGenerator.java Thu Aug 29 16:34:31 2013 -0700 41.3 @@ -173,6 +173,9 @@ 41.4 private static final String CLASS_INIT = "<clinit>"; 41.5 private static final String STATIC_GLOBAL_FIELD_NAME = "staticGlobal"; 41.6 41.7 + // Method name prefix for invoking super-methods 41.8 + static final String SUPER_PREFIX = "super$"; 41.9 + 41.10 /** 41.11 * Collection of methods we never override: Object.clone(), Object.finalize(). 41.12 */ 41.13 @@ -240,6 +243,7 @@ 41.14 } 41.15 generateConstructors(); 41.16 generateMethods(); 41.17 + generateSuperMethods(); 41.18 // } 41.19 cw.visitEnd(); 41.20 } 41.21 @@ -507,6 +511,10 @@ 41.22 41.23 private static void endInitMethod(final InstructionAdapter mv) { 41.24 mv.visitInsn(RETURN); 41.25 + endMethod(mv); 41.26 + } 41.27 + 41.28 + private static void endMethod(final InstructionAdapter mv) { 41.29 mv.visitMaxs(0, 0); 41.30 mv.visitEnd(); 41.31 } 41.32 @@ -603,13 +611,8 @@ 41.33 */ 41.34 private void generateMethod(final MethodInfo mi) { 41.35 final Method method = mi.method; 41.36 - final int mod = method.getModifiers(); 41.37 - final int access = ACC_PUBLIC | (method.isVarArgs() ? ACC_VARARGS : 0); 41.38 final Class<?>[] exceptions = method.getExceptionTypes(); 41.39 - final String[] exceptionNames = new String[exceptions.length]; 41.40 - for (int i = 0; i < exceptions.length; ++i) { 41.41 - exceptionNames[i] = Type.getInternalName(exceptions[i]); 41.42 - } 41.43 + final String[] exceptionNames = getExceptionNames(exceptions); 41.44 final MethodType type = mi.type; 41.45 final String methodDesc = type.toMethodDescriptorString(); 41.46 final String name = mi.getName(); 41.47 @@ -617,14 +620,8 @@ 41.48 final Type asmType = Type.getMethodType(methodDesc); 41.49 final Type[] asmArgTypes = asmType.getArgumentTypes(); 41.50 41.51 - // Determine the first index for a local variable 41.52 - int nextLocalVar = 1; // this 41.53 - for(final Type t: asmArgTypes) { 41.54 - nextLocalVar += t.getSize(); 41.55 - } 41.56 - 41.57 - final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(access, name, methodDesc, null, 41.58 - exceptionNames)); 41.59 + final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(getAccessModifiers(method), name, 41.60 + methodDesc, null, exceptionNames)); 41.61 mv.visitCode(); 41.62 41.63 final Label instanceHandleDefined = new Label(); 41.64 @@ -646,7 +643,7 @@ 41.65 } 41.66 41.67 // No handle is available, fall back to default behavior 41.68 - if(Modifier.isAbstract(mod)) { 41.69 + if(Modifier.isAbstract(method.getModifiers())) { 41.70 // If the super method is abstract, throw an exception 41.71 mv.anew(UNSUPPORTED_OPERATION_TYPE); 41.72 mv.dup(); 41.73 @@ -654,14 +651,7 @@ 41.74 mv.athrow(); 41.75 } else { 41.76 // If the super method is not abstract, delegate to it. 41.77 - mv.visitVarInsn(ALOAD, 0); 41.78 - int nextParam = 1; 41.79 - for(final Type t: asmArgTypes) { 41.80 - mv.load(nextParam, t); 41.81 - nextParam += t.getSize(); 41.82 - } 41.83 - mv.invokespecial(superClassName, name, methodDesc); 41.84 - mv.areturn(asmReturnType); 41.85 + emitSuperCall(mv, name, methodDesc); 41.86 } 41.87 41.88 final Label setupGlobal = new Label(); 41.89 @@ -685,6 +675,12 @@ 41.90 // stack: [creatingGlobal, someHandle] 41.91 mv.visitLabel(setupGlobal); 41.92 41.93 + // Determine the first index for a local variable 41.94 + int nextLocalVar = 1; // "this" is at 0 41.95 + for(final Type t: asmArgTypes) { 41.96 + nextLocalVar += t.getSize(); 41.97 + } 41.98 + // Set our local variable indices 41.99 final int currentGlobalVar = nextLocalVar++; 41.100 final int globalsDifferVar = nextLocalVar++; 41.101 41.102 @@ -775,8 +771,7 @@ 41.103 } 41.104 mv.visitTryCatchBlock(tryBlockStart, tryBlockEnd, throwableHandler, THROWABLE_TYPE_NAME); 41.105 } 41.106 - mv.visitMaxs(0, 0); 41.107 - mv.visitEnd(); 41.108 + endMethod(mv); 41.109 } 41.110 41.111 /** 41.112 @@ -817,6 +812,53 @@ 41.113 return false; 41.114 } 41.115 41.116 + private void generateSuperMethods() { 41.117 + for(final MethodInfo mi: methodInfos) { 41.118 + if(!Modifier.isAbstract(mi.method.getModifiers())) { 41.119 + generateSuperMethod(mi); 41.120 + } 41.121 + } 41.122 + } 41.123 + 41.124 + private void generateSuperMethod(MethodInfo mi) { 41.125 + final Method method = mi.method; 41.126 + 41.127 + final String methodDesc = mi.type.toMethodDescriptorString(); 41.128 + final String name = mi.getName(); 41.129 + 41.130 + final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(getAccessModifiers(method), 41.131 + SUPER_PREFIX + name, methodDesc, null, getExceptionNames(method.getExceptionTypes()))); 41.132 + mv.visitCode(); 41.133 + 41.134 + emitSuperCall(mv, name, methodDesc); 41.135 + 41.136 + endMethod(mv); 41.137 + } 41.138 + 41.139 + private void emitSuperCall(final InstructionAdapter mv, final String name, final String methodDesc) { 41.140 + mv.visitVarInsn(ALOAD, 0); 41.141 + int nextParam = 1; 41.142 + final Type methodType = Type.getMethodType(methodDesc); 41.143 + for(final Type t: methodType.getArgumentTypes()) { 41.144 + mv.load(nextParam, t); 41.145 + nextParam += t.getSize(); 41.146 + } 41.147 + mv.invokespecial(superClassName, name, methodDesc); 41.148 + mv.areturn(methodType.getReturnType()); 41.149 + } 41.150 + 41.151 + private static String[] getExceptionNames(final Class<?>[] exceptions) { 41.152 + final String[] exceptionNames = new String[exceptions.length]; 41.153 + for (int i = 0; i < exceptions.length; ++i) { 41.154 + exceptionNames[i] = Type.getInternalName(exceptions[i]); 41.155 + } 41.156 + return exceptionNames; 41.157 + } 41.158 + 41.159 + private static int getAccessModifiers(final Method method) { 41.160 + return ACC_PUBLIC | (method.isVarArgs() ? ACC_VARARGS : 0); 41.161 + } 41.162 + 41.163 /** 41.164 * Gathers methods that can be implemented or overridden from the specified type into this factory's 41.165 * {@link #methodInfos} set. It will add all non-final, non-static methods that are either public or protected from
42.1 --- a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterClassLoader.java Thu Aug 29 09:42:13 2013 -0700 42.2 +++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterClassLoader.java Thu Aug 29 16:34:31 2013 -0700 42.3 @@ -34,7 +34,6 @@ 42.4 import java.security.PrivilegedAction; 42.5 import java.security.ProtectionDomain; 42.6 import java.security.SecureClassLoader; 42.7 - 42.8 import jdk.internal.dynalink.beans.StaticClass; 42.9 42.10 /**
43.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 43.2 +++ b/src/jdk/nashorn/internal/runtime/linker/JavaSuperAdapter.java Thu Aug 29 16:34:31 2013 -0700 43.3 @@ -0,0 +1,44 @@ 43.4 +/* 43.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 43.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 43.7 + * 43.8 + * This code is free software; you can redistribute it and/or modify it 43.9 + * under the terms of the GNU General Public License version 2 only, as 43.10 + * published by the Free Software Foundation. Oracle designates this 43.11 + * particular file as subject to the "Classpath" exception as provided 43.12 + * by Oracle in the LICENSE file that accompanied this code. 43.13 + * 43.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 43.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 43.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 43.17 + * version 2 for more details (a copy is included in the LICENSE file that 43.18 + * accompanied this code). 43.19 + * 43.20 + * You should have received a copy of the GNU General Public License version 43.21 + * 2 along with this work; if not, write to the Free Software Foundation, 43.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 43.23 + * 43.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 43.25 + * or visit www.oracle.com if you need additional information or have any 43.26 + * questions. 43.27 + */ 43.28 + 43.29 +package jdk.nashorn.internal.runtime.linker; 43.30 + 43.31 +/** 43.32 + * Represents a an adapter for invoking superclass methods on an adapter instance generated by 43.33 + * {@link JavaAdapterBytecodeGenerator}. Note that objects of this class are just wrappers around the adapter instances, 43.34 + * without any behavior. All the behavior is defined in the {@code JavaSuperAdapterLinker}. 43.35 + */ 43.36 +class JavaSuperAdapter { 43.37 + private final Object adapter; 43.38 + 43.39 + JavaSuperAdapter(final Object adapter) { 43.40 + adapter.getClass(); // NPE check 43.41 + this.adapter = adapter; 43.42 + } 43.43 + 43.44 + public Object getAdapter() { 43.45 + return adapter; 43.46 + } 43.47 +}
44.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 44.2 +++ b/src/jdk/nashorn/internal/runtime/linker/JavaSuperAdapterLinker.java Thu Aug 29 16:34:31 2013 -0700 44.3 @@ -0,0 +1,180 @@ 44.4 +/* 44.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 44.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 44.7 + * 44.8 + * This code is free software; you can redistribute it and/or modify it 44.9 + * under the terms of the GNU General Public License version 2 only, as 44.10 + * published by the Free Software Foundation. Oracle designates this 44.11 + * particular file as subject to the "Classpath" exception as provided 44.12 + * by Oracle in the LICENSE file that accompanied this code. 44.13 + * 44.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 44.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 44.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 44.17 + * version 2 for more details (a copy is included in the LICENSE file that 44.18 + * accompanied this code). 44.19 + * 44.20 + * You should have received a copy of the GNU General Public License version 44.21 + * 2 along with this work; if not, write to the Free Software Foundation, 44.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 44.23 + * 44.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 44.25 + * or visit www.oracle.com if you need additional information or have any 44.26 + * questions. 44.27 + */ 44.28 + 44.29 +package jdk.nashorn.internal.runtime.linker; 44.30 + 44.31 +import static jdk.nashorn.internal.lookup.Lookup.EMPTY_GETTER; 44.32 +import static jdk.nashorn.internal.runtime.linker.JavaAdapterBytecodeGenerator.SUPER_PREFIX; 44.33 + 44.34 +import java.lang.invoke.MethodHandle; 44.35 +import java.lang.invoke.MethodHandles; 44.36 +import java.lang.invoke.MethodType; 44.37 +import jdk.internal.dynalink.CallSiteDescriptor; 44.38 +import jdk.internal.dynalink.beans.BeansLinker; 44.39 +import jdk.internal.dynalink.linker.GuardedInvocation; 44.40 +import jdk.internal.dynalink.linker.LinkRequest; 44.41 +import jdk.internal.dynalink.linker.LinkerServices; 44.42 +import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker; 44.43 +import jdk.internal.dynalink.support.CallSiteDescriptorFactory; 44.44 +import jdk.internal.dynalink.support.Lookup; 44.45 +import jdk.nashorn.internal.runtime.ScriptRuntime; 44.46 + 44.47 +/** 44.48 + * A linker for instances of {@link JavaSuperAdapter}. Only links {@code getMethod} calls, by forwarding them to the 44.49 + * bean linker for the adapter class and prepending {@code super$} to method names. 44.50 + * 44.51 + */ 44.52 +final class JavaSuperAdapterLinker implements TypeBasedGuardingDynamicLinker { 44.53 + private static final String GET_METHOD = "getMethod"; 44.54 + private static final String DYN_GET_METHOD = "dyn:" + GET_METHOD; 44.55 + private static final String DYN_GET_METHOD_FIXED = DYN_GET_METHOD + ":" + SUPER_PREFIX; 44.56 + 44.57 + private static final MethodHandle ADD_PREFIX_TO_METHOD_NAME; 44.58 + private static final MethodHandle BIND_DYNAMIC_METHOD; 44.59 + private static final MethodHandle GET_ADAPTER; 44.60 + private static final MethodHandle IS_ADAPTER_OF_CLASS; 44.61 + 44.62 + static { 44.63 + final Lookup lookup = new Lookup(MethodHandles.lookup()); 44.64 + ADD_PREFIX_TO_METHOD_NAME = lookup.findOwnStatic("addPrefixToMethodName", Object.class, Object.class); 44.65 + BIND_DYNAMIC_METHOD = lookup.findOwnStatic("bindDynamicMethod", Object.class, Object.class, Object.class); 44.66 + GET_ADAPTER = lookup.findVirtual(JavaSuperAdapter.class, "getAdapter", MethodType.methodType(Object.class)); 44.67 + IS_ADAPTER_OF_CLASS = lookup.findOwnStatic("isAdapterOfClass", boolean.class, Class.class, Object.class); 44.68 + } 44.69 + 44.70 + @Override 44.71 + public boolean canLinkType(final Class<?> type) { 44.72 + return type == JavaSuperAdapter.class; 44.73 + } 44.74 + 44.75 + @Override 44.76 + public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) 44.77 + throws Exception { 44.78 + final Object objSuperAdapter = linkRequest.getReceiver(); 44.79 + if(!(objSuperAdapter instanceof JavaSuperAdapter)) { 44.80 + return null; 44.81 + } 44.82 + 44.83 + final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor(); 44.84 + if(!CallSiteDescriptorFactory.tokenizeOperators(descriptor).contains(GET_METHOD)) { 44.85 + // We only handle getMethod 44.86 + return null; 44.87 + } 44.88 + 44.89 + final Object adapter = ((JavaSuperAdapter)objSuperAdapter).getAdapter(); 44.90 + 44.91 + // Replace argument (javaSuperAdapter, ...) => (adapter, ...) when delegating to BeansLinker 44.92 + final Object[] args = linkRequest.getArguments(); 44.93 + args[0] = adapter; 44.94 + 44.95 + // Use R(T0, ...) => R(adapter.class, ...) call site type when delegating to BeansLinker. 44.96 + final MethodType type = descriptor.getMethodType(); 44.97 + final Class<?> adapterClass = adapter.getClass(); 44.98 + final boolean hasFixedName = descriptor.getNameTokenCount() > 2; 44.99 + final String opName = hasFixedName ? (DYN_GET_METHOD_FIXED + descriptor.getNameToken( 44.100 + CallSiteDescriptor.NAME_OPERAND)) : DYN_GET_METHOD; 44.101 + 44.102 + final CallSiteDescriptor newDescriptor = NashornCallSiteDescriptor.get(descriptor.getLookup(), opName, 44.103 + type.changeParameterType(0, adapterClass), 0); 44.104 + 44.105 + // Delegate to BeansLinker 44.106 + final GuardedInvocation guardedInv = BeansLinker.getLinkerForClass(adapterClass).getGuardedInvocation( 44.107 + linkRequest.replaceArguments(newDescriptor, args), linkerServices); 44.108 + 44.109 + final MethodHandle guard = IS_ADAPTER_OF_CLASS.bindTo(adapterClass); 44.110 + if(guardedInv == null) { 44.111 + // Short circuit the lookup here for non-existent methods by linking an empty getter. If we just returned 44.112 + // null instead, BeansLinker would find final methods on the JavaSuperAdapter instead: getClass() and 44.113 + // wait(). 44.114 + return new GuardedInvocation(MethodHandles.dropArguments(EMPTY_GETTER, 1,type.parameterList().subList(1, 44.115 + type.parameterCount())), guard).asType(descriptor); 44.116 + } 44.117 + 44.118 + final MethodHandle invocation = guardedInv.getInvocation(); 44.119 + final MethodType invType = invocation.type(); 44.120 + // For invocation typed R(T0, ...) create a dynamic method binder of type R(R, T0) 44.121 + final MethodHandle typedBinder = BIND_DYNAMIC_METHOD.asType(MethodType.methodType(invType.returnType(), 44.122 + invType.returnType(), invType.parameterType(0))); 44.123 + // For invocation typed R(T0, T1, ...) create a dynamic method binder of type R(R, T0, T1, ...) 44.124 + final MethodHandle droppingBinder = MethodHandles.dropArguments(typedBinder, 2, 44.125 + invType.parameterList().subList(1, invType.parameterCount())); 44.126 + // Finally, fold the invocation into the binder to produce a method handle that will bind every returned 44.127 + // DynamicMethod object from dyn:getMethod calls to the actual receiver 44.128 + // R(R(T0, T1, ...), T0, T1, ...) 44.129 + final MethodHandle bindingInvocation = MethodHandles.foldArguments(droppingBinder, invocation); 44.130 + 44.131 + final MethodHandle typedGetAdapter = asFilterType(GET_ADAPTER, 0, invType, type); 44.132 + final MethodHandle adaptedInvocation; 44.133 + if(hasFixedName) { 44.134 + adaptedInvocation = MethodHandles.filterArguments(bindingInvocation, 0, typedGetAdapter); 44.135 + } else { 44.136 + // Add a filter that'll prepend "super$" to each name passed to the variable-name "dyn:getMethod". 44.137 + final MethodHandle typedAddPrefix = asFilterType(ADD_PREFIX_TO_METHOD_NAME, 1, invType, type); 44.138 + adaptedInvocation = MethodHandles.filterArguments(bindingInvocation, 0, typedGetAdapter, typedAddPrefix); 44.139 + } 44.140 + 44.141 + return guardedInv.replaceMethods(adaptedInvocation, guard).asType(descriptor); 44.142 + } 44.143 + 44.144 + /** 44.145 + * Adapts the type of a method handle used as a filter in a position from a source method type to a target method type. 44.146 + * @param filter the filter method handle 44.147 + * @param pos the position in the argument list that it's filtering 44.148 + * @param targetType the target method type for filtering 44.149 + * @param sourceType the source method type for filtering 44.150 + * @return a type adapted filter 44.151 + */ 44.152 + private static MethodHandle asFilterType(final MethodHandle filter, int pos, MethodType targetType, MethodType sourceType) { 44.153 + return filter.asType(MethodType.methodType(targetType.parameterType(pos), sourceType.parameterType(pos))); 44.154 + } 44.155 + 44.156 + @SuppressWarnings("unused") 44.157 + private static Object addPrefixToMethodName(final Object name) { 44.158 + return SUPER_PREFIX.concat(String.valueOf(name)); 44.159 + } 44.160 + 44.161 + /** 44.162 + * Used to transform the return value of getMethod; transform a {@code DynamicMethod} into a 44.163 + * {@code BoundDynamicMethod} while also accounting for the possibility of a non-existent method. 44.164 + * @param dynamicMethod the dynamic method to bind 44.165 + * @param boundThis the adapter underlying a super adapter, to which the dynamic method is bound. 44.166 + * @return a dynamic method bound to the adapter instance. 44.167 + */ 44.168 + @SuppressWarnings("unused") 44.169 + private static Object bindDynamicMethod(final Object dynamicMethod, final Object boundThis) { 44.170 + return dynamicMethod == null ? ScriptRuntime.UNDEFINED : Bootstrap.bindDynamicMethod(dynamicMethod, boundThis); 44.171 + } 44.172 + 44.173 + /** 44.174 + * Used as the guard of linkages, as the receiver is not guaranteed to be a JavaSuperAdapter. 44.175 + * @param clazz the class the receiver's adapter is tested against. 44.176 + * @param obj receiver 44.177 + * @return true if the receiver is a super adapter, and its underlying adapter is of the specified class 44.178 + */ 44.179 + @SuppressWarnings("unused") 44.180 + private static boolean isAdapterOfClass(Class<?> clazz, Object obj) { 44.181 + return obj instanceof JavaSuperAdapter && clazz == (((JavaSuperAdapter)obj).getAdapter()).getClass(); 44.182 + } 44.183 +}
45.1 --- a/src/jdk/nashorn/internal/runtime/regexp/RegExp.java Thu Aug 29 09:42:13 2013 -0700 45.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/RegExp.java Thu Aug 29 16:34:31 2013 -0700 45.3 @@ -60,7 +60,7 @@ 45.4 * @param flags the flags string 45.5 */ 45.6 protected RegExp(final String source, final String flags) { 45.7 - this.source = source; 45.8 + this.source = source.length() == 0 ? "(?:)" : source; 45.9 for (int i = 0; i < flags.length(); i++) { 45.10 final char ch = flags.charAt(i); 45.11 switch (ch) {
46.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/ArrayCompiler.java Thu Aug 29 09:42:13 2013 -0700 46.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/ArrayCompiler.java Thu Aug 29 16:34:31 2013 -0700 46.3 @@ -39,7 +39,6 @@ 46.4 import jdk.nashorn.internal.runtime.regexp.joni.constants.OPCode; 46.5 import jdk.nashorn.internal.runtime.regexp.joni.constants.OPSize; 46.6 import jdk.nashorn.internal.runtime.regexp.joni.constants.TargetInfo; 46.7 -import jdk.nashorn.internal.runtime.regexp.joni.encoding.CharacterType; 46.8 46.9 final class ArrayCompiler extends Compiler { 46.10 private int[] code; 46.11 @@ -345,6 +344,7 @@ 46.12 46.13 private static final int QUANTIFIER_EXPAND_LIMIT_SIZE = 50; // was 50 46.14 46.15 + @SuppressWarnings("unused") 46.16 private static boolean cknOn(int ckn) { 46.17 return ckn > 0; 46.18 } 46.19 @@ -879,6 +879,7 @@ 46.20 } 46.21 } 46.22 46.23 + @SuppressWarnings("unused") 46.24 private void addStateCheckNum(int num) { 46.25 addInt(num); 46.26 } 46.27 @@ -887,6 +888,7 @@ 46.28 addInt(addr); 46.29 } 46.30 46.31 + @SuppressWarnings("unused") 46.32 private void addAbsAddr(int addr) { 46.33 addInt(addr); 46.34 }
47.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/BitSet.java Thu Aug 29 09:42:13 2013 -0700 47.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/BitSet.java Thu Aug 29 16:34:31 2013 -0700 47.3 @@ -29,6 +29,7 @@ 47.4 final int[] bits = new int[BITSET_SIZE]; 47.5 47.6 private static final int BITS_TO_STRING_WRAP = 4; 47.7 + @Override 47.8 public String toString() { 47.9 StringBuilder buffer = new StringBuilder(); 47.10 buffer.append("BitSet");
48.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/ByteCodeMachine.java Thu Aug 29 09:42:13 2013 -0700 48.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/ByteCodeMachine.java Thu Aug 29 16:34:31 2013 -0700 48.3 @@ -26,7 +26,6 @@ 48.4 import static jdk.nashorn.internal.runtime.regexp.joni.Option.isNotBol; 48.5 import static jdk.nashorn.internal.runtime.regexp.joni.Option.isNotEol; 48.6 import static jdk.nashorn.internal.runtime.regexp.joni.Option.isPosixRegion; 48.7 -import static jdk.nashorn.internal.runtime.regexp.joni.EncodingHelper.isCrnl; 48.8 import static jdk.nashorn.internal.runtime.regexp.joni.EncodingHelper.isNewLine; 48.9 48.10 import jdk.nashorn.internal.runtime.regexp.joni.ast.CClassNode; 48.11 @@ -96,6 +95,7 @@ 48.12 } 48.13 } 48.14 48.15 + @Override 48.16 protected final int matchAt(int range, int sstart, int sprev) { 48.17 this.range = range; 48.18 this.sstart = sstart; 48.19 @@ -499,7 +499,7 @@ 48.20 48.21 private void opAnyChar() { 48.22 if (s >= range) {opFail(); return;} 48.23 - if (chars[s] == EncodingHelper.NEW_LINE) {opFail(); return;} 48.24 + if (isNewLine(chars[s])) {opFail(); return;} 48.25 s++; 48.26 sprev = sbegin; // break; 48.27 } 48.28 @@ -537,7 +537,7 @@ 48.29 while (s < range) { 48.30 char b = chars[s]; 48.31 if (c == b) pushAlt(ip + 1, s, sprev); 48.32 - if (b == EncodingHelper.NEW_LINE) {opFail(); return;} 48.33 + if (isNewLine(b)) {opFail(); return;} 48.34 sprev = s; 48.35 s++; 48.36 } 48.37 @@ -616,7 +616,7 @@ 48.38 if (s == str) { 48.39 if (isNotBol(msaOptions)) opFail(); 48.40 return; 48.41 - } else if (EncodingHelper.isNewLine(chars, sprev, end) && s != end) { 48.42 + } else if (isNewLine(chars, sprev, end) && s != end) { 48.43 return; 48.44 } 48.45 opFail(); 48.46 @@ -625,7 +625,7 @@ 48.47 private void opEndLine() { 48.48 if (s == end) { 48.49 if (Config.USE_NEWLINE_AT_END_OF_STRING_HAS_EMPTY_LINE) { 48.50 - if (str == end || !EncodingHelper.isNewLine(chars, sprev, end)) { 48.51 + if (str == end || !isNewLine(chars, sprev, end)) { 48.52 if (isNotEol(msaOptions)) opFail(); 48.53 } 48.54 return; 48.55 @@ -633,7 +633,7 @@ 48.56 if (isNotEol(msaOptions)) opFail(); 48.57 return; 48.58 } 48.59 - } else if (isNewLine(chars, s, end) || (Config.USE_CRNL_AS_LINE_TERMINATOR && isCrnl(chars, s, end))) { 48.60 + } else if (isNewLine(chars, s, end)) { 48.61 return; 48.62 } 48.63 opFail(); 48.64 @@ -652,9 +652,6 @@ 48.65 } 48.66 } else if (isNewLine(chars, s, end) && s + 1 == end) { 48.67 return; 48.68 - } else if (Config.USE_CRNL_AS_LINE_TERMINATOR && isCrnl(chars, s, end)) { 48.69 - int ss = s + 2; 48.70 - if (ss == end) return; 48.71 } 48.72 opFail(); 48.73 } 48.74 @@ -731,8 +728,6 @@ 48.75 // STRING_CMP 48.76 while(n-- > 0) if (chars[pstart++] != chars[s++]) {opFail(); return;} 48.77 48.78 - int len; 48.79 - 48.80 // beyond string check 48.81 if (sprev < range) { 48.82 while (sprev + 1 < s) sprev++; 48.83 @@ -768,7 +763,6 @@ 48.84 if (!stringCmpIC(regex.caseFoldFlag, pstart, this, n, end)) {opFail(); return;} 48.85 s = value; 48.86 48.87 - int len; 48.88 // if (sprev < chars.length) 48.89 while (sprev + 1 < s) sprev++; 48.90 } 48.91 @@ -796,8 +790,6 @@ 48.92 48.93 s = swork; 48.94 48.95 - int len; 48.96 - 48.97 // beyond string check 48.98 if (sprev < range) { 48.99 while (sprev + 1 < s) sprev++; 48.100 @@ -829,7 +821,6 @@ 48.101 if (!stringCmpIC(regex.caseFoldFlag, pstart, this, n, end)) continue loop; // STRING_CMP_VALUE_IC 48.102 s = value; 48.103 48.104 - int len; 48.105 // if (sprev < chars.length) 48.106 while (sprev + 1 < s) sprev++; 48.107 48.108 @@ -902,7 +893,6 @@ 48.109 48.110 sprev = s; 48.111 if (backrefMatchAtNestedLevel(ic != 0, regex.caseFoldFlag, level, tlen, ip)) { // (s) and (end) implicit 48.112 - int len; 48.113 while (sprev + 1 < s) sprev++; 48.114 ip += tlen; // * SIZE_MEMNUM 48.115 } else {
49.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/CodeRangeBuffer.java Thu Aug 29 09:42:13 2013 -0700 49.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/CodeRangeBuffer.java Thu Aug 29 16:34:31 2013 -0700 49.3 @@ -58,6 +58,7 @@ 49.4 used = orig.used; 49.5 } 49.6 49.7 + @Override 49.8 public String toString() { 49.9 StringBuilder buf = new StringBuilder(); 49.10 buf.append("CodeRange");
50.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/Config.java Thu Aug 29 09:42:13 2013 -0700 50.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/Config.java Thu Aug 29 16:34:31 2013 -0700 50.3 @@ -29,7 +29,6 @@ 50.4 final int INTERNAL_ENC_CASE_FOLD_MULTI_CHAR = (1<<30); 50.5 final int ENC_CASE_FOLD_MIN = INTERNAL_ENC_CASE_FOLD_MULTI_CHAR; 50.6 final int ENC_CASE_FOLD_DEFAULT = ENC_CASE_FOLD_MIN; 50.7 - final boolean USE_CRNL_AS_LINE_TERMINATOR = false; 50.8 50.9 final boolean USE_MONOMANIAC_CHECK_CAPTURES_IN_ENDLESS_REPEAT = true; /* /(?:()|())*\2/ */ 50.10 final boolean USE_NEWLINE_AT_END_OF_STRING_HAS_EMPTY_LINE = true; /* /\n$/ =~ "\n" */
51.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/EncodingHelper.java Thu Aug 29 09:42:13 2013 -0700 51.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/EncodingHelper.java Thu Aug 29 16:34:31 2013 -0700 51.3 @@ -24,10 +24,12 @@ 51.4 51.5 import java.util.Arrays; 51.6 51.7 -public class EncodingHelper { 51.8 +public final class EncodingHelper { 51.9 51.10 - public final static char NEW_LINE = 0xa; 51.11 - public final static char RETURN = 0xd; 51.12 + final static int NEW_LINE = 0x000a; 51.13 + final static int RETURN = 0x000d; 51.14 + final static int LINE_SEPARATOR = 0x2028; 51.15 + final static int PARAGRAPH_SEPARATOR = 0x2029; 51.16 51.17 final static char[] EMPTYCHARS = new char[0]; 51.18 final static int[][] codeRanges = new int[15][]; 51.19 @@ -64,15 +66,11 @@ 51.20 } 51.21 51.22 public static boolean isNewLine(int code) { 51.23 - return code == NEW_LINE; 51.24 + return code == NEW_LINE || code == RETURN || code == LINE_SEPARATOR || code == PARAGRAPH_SEPARATOR; 51.25 } 51.26 51.27 public static boolean isNewLine(char[] chars, int p, int end) { 51.28 - return p < end && chars[p] == NEW_LINE; 51.29 - } 51.30 - 51.31 - public static boolean isCrnl(char[] chars, int p, int end) { 51.32 - return p + 1 < end && chars[p] == RETURN && chars[p + 1] == NEW_LINE; 51.33 + return p < end && isNewLine(chars[p]); 51.34 } 51.35 51.36 // Encoding.prevCharHead 51.37 @@ -194,7 +192,7 @@ 51.38 int type; 51.39 switch (ctype) { 51.40 case CharacterType.NEWLINE: 51.41 - return code == EncodingHelper.NEW_LINE; 51.42 + return isNewLine(code); 51.43 case CharacterType.ALPHA: 51.44 return (1 << Character.getType(code) & CharacterType.ALPHA_MASK) != 0; 51.45 case CharacterType.BLANK:
52.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/Lexer.java Thu Aug 29 09:42:13 2013 -0700 52.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/Lexer.java Thu Aug 29 16:34:31 2013 -0700 52.3 @@ -139,6 +139,7 @@ 52.4 } 52.5 } 52.6 52.7 + @SuppressWarnings("fallthrough") 52.8 /* \M-, \C-, \c, or \... */ 52.9 private int fetchEscapedValue() { 52.10 if (!left()) { 52.11 @@ -731,7 +732,7 @@ 52.12 if (syntax.opLineAnchor()) fetchTokenFor_anchor(isSingleline(env.option) ? AnchorType.BEGIN_BUF : AnchorType.BEGIN_LINE); 52.13 break; 52.14 case '$': 52.15 - if (syntax.opLineAnchor()) fetchTokenFor_anchor(isSingleline(env.option) ? AnchorType.SEMI_END_BUF : AnchorType.END_LINE); 52.16 + if (syntax.opLineAnchor()) fetchTokenFor_anchor(isSingleline(env.option) ? AnchorType.END_BUF : AnchorType.END_LINE); 52.17 break; 52.18 case '[': 52.19 if (syntax.opBracketCC()) token.type = TokenType.CC_CC_OPEN;
53.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/Matcher.java Thu Aug 29 09:42:13 2013 -0700 53.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/Matcher.java Thu Aug 29 16:34:31 2013 -0700 53.3 @@ -141,7 +141,7 @@ 53.4 continue retry; 53.5 } 53.6 } 53.7 - } else if (!EncodingHelper.isNewLine(chars, p, end) && (!Config.USE_CRNL_AS_LINE_TERMINATOR || !EncodingHelper.isCrnl(chars, p, end))) { 53.8 + } else if (!EncodingHelper.isNewLine(chars, p, end)) { 53.9 //if () break; 53.10 // goto retry_gate; 53.11 pprev = p; 53.12 @@ -226,7 +226,7 @@ 53.13 continue retry; 53.14 } 53.15 } 53.16 - } else if (!EncodingHelper.isNewLine(chars, p, end) && (!Config.USE_CRNL_AS_LINE_TERMINATOR || !EncodingHelper.isCrnl(chars, p, end))) { 53.17 + } else if (!EncodingHelper.isNewLine(chars, p, end)) { 53.18 p = EncodingHelper.prevCharHead(adjrange, p); 53.19 if (p == -1) return false; 53.20 continue retry; 53.21 @@ -330,12 +330,6 @@ 53.22 maxSemiEnd = end; 53.23 if (EncodingHelper.isNewLine(chars, preEnd, end)) { 53.24 minSemiEnd = preEnd; 53.25 - if (Config.USE_CRNL_AS_LINE_TERMINATOR) { 53.26 - preEnd = EncodingHelper.stepBack(str, preEnd, 1); 53.27 - if (preEnd != -1 && EncodingHelper.isCrnl(chars, preEnd, end)) { 53.28 - minSemiEnd = preEnd; 53.29 - } 53.30 - } 53.31 if (minSemiEnd > str && start <= minSemiEnd) { 53.32 // !goto end_buf;! 53.33 if (endBuf(start, range, minSemiEnd, maxSemiEnd)) return -1; // mismatch_no_msa;
54.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/Parser.java Thu Aug 29 09:42:13 2013 -0700 54.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/Parser.java Thu Aug 29 16:34:31 2013 -0700 54.3 @@ -297,8 +297,6 @@ 54.4 throw new SyntaxException(ERR_END_PATTERN_IN_GROUP); 54.5 } 54.6 54.7 - boolean listCapture = false; 54.8 - 54.9 fetch(); 54.10 switch(c) { 54.11 case ':': /* (?:...) grouping only */
55.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/Region.java Thu Aug 29 09:42:13 2013 -0700 55.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/Region.java Thu Aug 29 16:34:31 2013 -0700 55.3 @@ -32,6 +32,7 @@ 55.4 this.end = new int[num]; 55.5 } 55.6 55.7 + @Override 55.8 public String toString() { 55.9 StringBuilder sb = new StringBuilder(); 55.10 sb.append("Region: \n");
56.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/ScannerSupport.java Thu Aug 29 09:42:13 2013 -0700 56.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/ScannerSupport.java Thu Aug 29 16:34:31 2013 -0700 56.3 @@ -21,9 +21,6 @@ 56.4 56.5 import jdk.nashorn.internal.runtime.regexp.joni.encoding.IntHolder; 56.6 import jdk.nashorn.internal.runtime.regexp.joni.exception.ErrorMessages; 56.7 -import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException; 56.8 -import jdk.nashorn.internal.runtime.regexp.joni.exception.SyntaxException; 56.9 -import jdk.nashorn.internal.runtime.regexp.joni.exception.ValueException; 56.10 56.11 abstract class ScannerSupport extends IntHolder implements ErrorMessages { 56.12
57.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/SearchAlgorithm.java Thu Aug 29 09:42:13 2013 -0700 57.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/SearchAlgorithm.java Thu Aug 29 16:34:31 2013 -0700 57.3 @@ -28,14 +28,17 @@ 57.4 57.5 public static final SearchAlgorithm NONE = new SearchAlgorithm() { 57.6 57.7 + @Override 57.8 public final String getName() { 57.9 return "NONE"; 57.10 } 57.11 57.12 + @Override 57.13 public final int search(Regex regex, char[] text, int textP, int textEnd, int textRange) { 57.14 return textP; 57.15 } 57.16 57.17 + @Override 57.18 public final int searchBackward(Regex regex, char[] text, int textP, int adjustText, int textEnd, int textStart, int s_, int range_) { 57.19 return textP; 57.20 } 57.21 @@ -44,10 +47,12 @@ 57.22 57.23 public static final SearchAlgorithm SLOW = new SearchAlgorithm() { 57.24 57.25 + @Override 57.26 public final String getName() { 57.27 return "EXACT"; 57.28 } 57.29 57.30 + @Override 57.31 public final int search(Regex regex, char[] text, int textP, int textEnd, int textRange) { 57.32 char[] target = regex.exact; 57.33 int targetP = regex.exactP; 57.34 @@ -78,6 +83,7 @@ 57.35 return -1; 57.36 } 57.37 57.38 + @Override 57.39 public final int searchBackward(Regex regex, char[] text, int textP, int adjustText, int textEnd, int textStart, int s_, int range_) { 57.40 char[] target = regex.exact; 57.41 int targetP = regex.exactP; 57.42 @@ -114,10 +120,12 @@ 57.43 this.caseFoldFlag = regex.caseFoldFlag; 57.44 } 57.45 57.46 + @Override 57.47 public final String getName() { 57.48 return "EXACT_IC"; 57.49 } 57.50 57.51 + @Override 57.52 public final int search(Regex regex, char[] text, int textP, int textEnd, int textRange) { 57.53 char[] target = regex.exact; 57.54 int targetP = regex.exactP; 57.55 @@ -136,6 +144,7 @@ 57.56 return -1; 57.57 } 57.58 57.59 + @Override 57.60 public final int searchBackward(Regex regex, char[] text, int textP, int adjustText, int textEnd, int textStart, int s_, int range_) { 57.61 char[] target = regex.exact; 57.62 int targetP = regex.exactP; 57.63 @@ -163,14 +172,16 @@ 57.64 } 57.65 return true; 57.66 } 57.67 - }; 57.68 + } 57.69 57.70 public static final SearchAlgorithm BM = new SearchAlgorithm() { 57.71 57.72 + @Override 57.73 public final String getName() { 57.74 return "EXACT_BM"; 57.75 } 57.76 57.77 + @Override 57.78 public final int search(Regex regex, char[] text, int textP, int textEnd, int textRange) { 57.79 char[] target = regex.exact; 57.80 int targetP = regex.exactP; 57.81 @@ -212,6 +223,7 @@ 57.82 57.83 private static final int BM_BACKWARD_SEARCH_LENGTH_THRESHOLD = 100; 57.84 57.85 + @Override 57.86 public final int searchBackward(Regex regex, char[] text, int textP, int adjustText, int textEnd, int textStart, int s_, int range_) { 57.87 char[] target = regex.exact; 57.88 int targetP = regex.exactP; 57.89 @@ -263,10 +275,12 @@ 57.90 57.91 public static final SearchAlgorithm MAP = new SearchAlgorithm() { 57.92 57.93 + @Override 57.94 public final String getName() { 57.95 return "MAP"; 57.96 } 57.97 57.98 + @Override 57.99 public final int search(Regex regex, char[] text, int textP, int textEnd, int textRange) { 57.100 byte[] map = regex.map; 57.101 int s = textP; 57.102 @@ -278,6 +292,7 @@ 57.103 return -1; 57.104 } 57.105 57.106 + @Override 57.107 public final int searchBackward(Regex regex, char[] text, int textP, int adjustText, int textEnd, int textStart, int s_, int range_) { 57.108 byte[] map = regex.map; 57.109 int s = textStart;
58.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/StackMachine.java Thu Aug 29 09:42:13 2013 -0700 58.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/StackMachine.java Thu Aug 29 16:34:31 2013 -0700 58.3 @@ -458,7 +458,7 @@ 58.4 isNull = 0; 58.5 break; 58.6 } else if (endp != s) { 58.7 - isNull = -1;; /* empty, but position changed */ 58.8 + isNull = -1; /* empty, but position changed */ 58.9 } 58.10 } 58.11 k++;
59.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/WarnCallback.java Thu Aug 29 09:42:13 2013 -0700 59.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/WarnCallback.java Thu Aug 29 16:34:31 2013 -0700 59.3 @@ -24,6 +24,7 @@ 59.4 */ 59.5 public interface WarnCallback { 59.6 WarnCallback DEFAULT = new WarnCallback() { 59.7 + @Override 59.8 public void warn(String message) { 59.9 System.err.println(message); 59.10 }
60.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/ast/EncloseNode.java Thu Aug 29 09:42:13 2013 -0700 60.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/ast/EncloseNode.java Thu Aug 29 16:34:31 2013 -0700 60.3 @@ -19,7 +19,6 @@ 60.4 */ 60.5 package jdk.nashorn.internal.runtime.regexp.joni.ast; 60.6 60.7 -import jdk.nashorn.internal.runtime.regexp.joni.Config; 60.8 import jdk.nashorn.internal.runtime.regexp.joni.Option; 60.9 import jdk.nashorn.internal.runtime.regexp.joni.constants.EncloseType; 60.10
61.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/ast/Node.java Thu Aug 29 09:42:13 2013 -0700 61.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/ast/Node.java Thu Aug 29 16:34:31 2013 -0700 61.3 @@ -35,7 +35,7 @@ 61.4 } 61.5 61.6 protected void setChild(Node tgt){} // default definition 61.7 - protected Node getChild(){return null;}; // default definition 61.8 + protected Node getChild(){return null;} // default definition 61.9 61.10 public void swap(Node with) { 61.11 Node tmp; 61.12 @@ -74,6 +74,7 @@ 61.13 return getName() + ":0x" + Integer.toHexString(System.identityHashCode(this)); 61.14 } 61.15 61.16 + @Override 61.17 public final String toString() { 61.18 StringBuilder s = new StringBuilder(); 61.19 s.append("<" + getAddressName() + " (" + (parent == null ? "NULL" : parent.getAddressName()) + ")>");
62.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/ast/QuantifierNode.java Thu Aug 29 09:42:13 2013 -0700 62.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/ast/QuantifierNode.java Thu Aug 29 16:34:31 2013 -0700 62.3 @@ -223,6 +223,7 @@ 62.4 other.target = null; // remove target from reduced quantifier 62.5 } 62.6 62.7 + @SuppressWarnings("fallthrough") 62.8 public int setQuantifier(Node tgt, boolean group, ScanEnvironment env, char[] chars, int p, int end) { 62.9 if (lower == 1 && upper == 1) return 1; 62.10
63.1 --- a/src/jdk/nashorn/internal/runtime/regexp/joni/exception/ErrorMessages.java Thu Aug 29 09:42:13 2013 -0700 63.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/joni/exception/ErrorMessages.java Thu Aug 29 16:34:31 2013 -0700 63.3 @@ -19,8 +19,6 @@ 63.4 */ 63.5 package jdk.nashorn.internal.runtime.regexp.joni.exception; 63.6 63.7 -import jdk.nashorn.internal.runtime.regexp.joni.Config; 63.8 - 63.9 public interface ErrorMessages { 63.10 63.11 /* from jcodings */
64.1 --- a/src/jdk/nashorn/internal/runtime/resources/Messages.properties Thu Aug 29 09:42:13 2013 -0700 64.2 +++ b/src/jdk/nashorn/internal/runtime/resources/Messages.properties Thu Aug 29 16:34:31 2013 -0700 64.3 @@ -94,6 +94,8 @@ 64.4 type.error.cant.redefine.property=Cannot redefine property "{0}" of {1} 64.5 type.error.property.not.writable="{0}" is not a writable property of {1} 64.6 type.error.object.non.extensible=Cannot add new property "{0}" to non-extensible {1} 64.7 +type.error.__proto__.set.non.extensible=Cannot set __proto__ of non-extensible {0} 64.8 +type.error.circular.__proto__.set=Cannot create__proto__ cycle for {0} 64.9 64.10 # miscellaneous 64.11 type.error.regex.cant.supply.flags=Cannot supply flags when constructing one RegExp from another
65.1 --- a/src/jdk/nashorn/internal/runtime/resources/Options.properties Thu Aug 29 09:42:13 2013 -0700 65.2 +++ b/src/jdk/nashorn/internal/runtime/resources/Options.properties Thu Aug 29 16:34:31 2013 -0700 65.3 @@ -157,6 +157,14 @@ 65.4 default=false \ 65.5 } 65.6 65.7 +nashorn.option.global.per.engine = { \ 65.8 + name="--global-per-engine", \ 65.9 + desc="Use single Global instance per script engine instance.", \ 65.10 + is_undocumented=true, \ 65.11 + type=Boolean, \ 65.12 + default=false \ 65.13 +} 65.14 + 65.15 nashorn.option.log = { \ 65.16 name="--log", \ 65.17 is_undocumented=true, \ 65.18 @@ -216,15 +224,6 @@ 65.19 default=false \ 65.20 } 65.21 65.22 -nashorn.option.package = { \ 65.23 - name="--package", \ 65.24 - is_undocumented=true, \ 65.25 - desc="Package to which generated .class files are added.", \ 65.26 - params="<package>", \ 65.27 - type=String, \ 65.28 - default="" \ 65.29 -} 65.30 - 65.31 nashorn.option.parse.only = { \ 65.32 name="--parse-only", \ 65.33 is_undocumented=true, \ 65.34 @@ -289,7 +288,7 @@ 65.35 nashorn.option.range.analysis = { \ 65.36 name="--range-analysis", \ 65.37 is_undocumented=true, \ 65.38 - desc="Do range analysis using known compile time types, and try to narrow number types" \ 65.39 + desc="EXPERIMENTAL: Do range analysis using known compile time types, and try to narrow number types" \ 65.40 } 65.41 65.42 nashorn.option.D = { \ 65.43 @@ -308,12 +307,12 @@ 65.44 desc="Enable scripting features." \ 65.45 } 65.46 65.47 -nashorn.option.specialize.calls = { \ 65.48 - name="--specialize-calls", \ 65.49 - is_undocumented=true, \ 65.50 - type=String, \ 65.51 - params="[=function_1,...,function_n]", \ 65.52 - desc="Specialize all or a set of method according to callsite parameter types" \ 65.53 +nashorn.option.specialize.calls = { \ 65.54 + name="--specialize-calls", \ 65.55 + is_undocumented=true, \ 65.56 + type=String, \ 65.57 + params="[=function_1,...,function_n]", \ 65.58 + desc="EXPERIMENTAL: Specialize all or a set of method according to callsite parameter types" \ 65.59 } 65.60 65.61 nashorn.option.stdout = { \
66.1 --- a/src/jdk/nashorn/internal/runtime/resources/mozilla_compat.js Thu Aug 29 09:42:13 2013 -0700 66.2 +++ b/src/jdk/nashorn/internal/runtime/resources/mozilla_compat.js Thu Aug 29 16:34:31 2013 -0700 66.3 @@ -144,7 +144,7 @@ 66.4 return Object.getPrototypeOf(this); 66.5 }, 66.6 set: function(x) { 66.7 - throw new TypeError("__proto__ set not supported"); 66.8 + Object.setPrototypeOf(this, x); 66.9 } 66.10 }); 66.11
67.1 --- a/src/jdk/nashorn/tools/Shell.java Thu Aug 29 09:42:13 2013 -0700 67.2 +++ b/src/jdk/nashorn/tools/Shell.java Thu Aug 29 16:34:31 2013 -0700 67.3 @@ -445,7 +445,7 @@ 67.4 continue; 67.5 } 67.6 67.7 - if (res != null && res != ScriptRuntime.UNDEFINED) { 67.8 + if (res != ScriptRuntime.UNDEFINED) { 67.9 err.println(JSType.toString(res)); 67.10 } 67.11 }
68.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 68.2 +++ b/test/script/basic/JDK-8019987.js Thu Aug 29 16:34:31 2013 -0700 68.3 @@ -0,0 +1,171 @@ 68.4 +/* 68.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 68.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 68.7 + * 68.8 + * This code is free software; you can redistribute it and/or modify it 68.9 + * under the terms of the GNU General Public License version 2 only, as 68.10 + * published by the Free Software Foundation. 68.11 + * 68.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 68.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 68.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 68.15 + * version 2 for more details (a copy is included in the LICENSE file that 68.16 + * accompanied this code). 68.17 + * 68.18 + * You should have received a copy of the GNU General Public License version 68.19 + * 2 along with this work; if not, write to the Free Software Foundation, 68.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 68.21 + * 68.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 68.23 + * or visit www.oracle.com if you need additional information or have any 68.24 + * questions. 68.25 + */ 68.26 + 68.27 +/** 68.28 + * JDK-8019987: String trimRight and trimLeft could be defined. 68.29 + * 68.30 + * @test 68.31 + * @run 68.32 + */ 68.33 + 68.34 +var TESTSTRING = "abcde"; 68.35 + 68.36 +var SPACES = " "; 68.37 +var TESTSTRING_LEFT_SPACES = SPACES + TESTSTRING; 68.38 +var TESTSTRING_RIGHT_SPACES = TESTSTRING + SPACES; 68.39 +var TESTSTRING_BOTH_SPACES = SPACES + TESTSTRING + SPACES; 68.40 +var TESTSTRING_MIDDLE_SPACES = TESTSTRING + SPACES + TESTSTRING; 68.41 + 68.42 +var WHITESPACE = 68.43 + " \t" + // space and tab 68.44 + "\n\r" + // newline and return 68.45 + "\u2028" + // line separator 68.46 + "\u2029" + // paragraph separator 68.47 + "\u000b" + // tabulation line 68.48 + "\u000c" + // ff (ctrl-l) 68.49 + "\u00a0" + // Latin-1 space 68.50 + "\u1680" + // Ogham space mark 68.51 + "\u180e" + // separator, Mongolian vowel 68.52 + "\u2000" + // en quad 68.53 + "\u2001" + // em quad 68.54 + "\u2002" + // en space 68.55 + "\u2003" + // em space 68.56 + "\u2004" + // three-per-em space 68.57 + "\u2005" + // four-per-em space 68.58 + "\u2006" + // six-per-em space 68.59 + "\u2007" + // figure space 68.60 + "\u2008" + // punctuation space 68.61 + "\u2009" + // thin space 68.62 + "\u200a" + // hair space 68.63 + "\u202f" + // narrow no-break space 68.64 + "\u205f" + // medium mathematical space 68.65 + "\u3000" + // ideographic space 68.66 + "\ufeff"; // byte order mark 68.67 +var TESTSTRING_LEFT_WHITESPACE = WHITESPACE + TESTSTRING; 68.68 +var TESTSTRING_RIGHT_WHITESPACE = TESTSTRING + WHITESPACE; 68.69 +var TESTSTRING_BOTH_WHITESPACE = WHITESPACE + TESTSTRING + WHITESPACE; 68.70 +var TESTSTRING_MIDDLE_WHITESPACE = TESTSTRING + WHITESPACE + TESTSTRING; 68.71 + 68.72 +function escape(string) { 68.73 + var sb = new java.lang.StringBuilder(); 68.74 + sb.append("\""); 68.75 + 68.76 + for (var i = 0; i < string.length; i++) { 68.77 + var ch = string.charAt(i); 68.78 + 68.79 + switch (ch) { 68.80 + case '\\': 68.81 + sb.append("\\\\"); 68.82 + break; 68.83 + case '"': 68.84 + sb.append("\\\""); 68.85 + break; 68.86 + case '\'': 68.87 + sb.append("\\\'"); 68.88 + break; 68.89 + case '\b': 68.90 + sb.append("\\b"); 68.91 + break; 68.92 + case '\f': 68.93 + sb.append("\\f"); 68.94 + break; 68.95 + case '\n': 68.96 + sb.append("\\n"); 68.97 + break; 68.98 + case '\r': 68.99 + sb.append("\\r"); 68.100 + break; 68.101 + case '\t': 68.102 + sb.append("\\t"); 68.103 + break; 68.104 + default: 68.105 + var code = string.charCodeAt(i); 68.106 + 68.107 + if (code < 0x20 || code >= 0xFF) { 68.108 + sb.append("\\u"); 68.109 + 68.110 + var hex = java.lang.Integer.toHexString(code); 68.111 + for (var i = hex.length; i < 4; i++) { 68.112 + sb.append('0'); 68.113 + } 68.114 + sb.append(hex); 68.115 + } else { 68.116 + sb.append(ch); 68.117 + } 68.118 + 68.119 + break; 68.120 + } 68.121 + } 68.122 + 68.123 + sb.append("\""); 68.124 + 68.125 + return sb.toString(); 68.126 +} 68.127 + 68.128 +var count = 0; 68.129 +function test(expected, trimmed) { 68.130 + count++; 68.131 + if (trimmed != expected) { 68.132 + print(count + ": Expected: " + escape(expected) + ", found: " + escape(trimmed)); 68.133 + } 68.134 +} 68.135 + 68.136 +test("", SPACES.trim()); 68.137 +test("", SPACES.trimLeft()); 68.138 +test("", SPACES.trimRight()); 68.139 + 68.140 +test(TESTSTRING, TESTSTRING_LEFT_SPACES.trim()); 68.141 +test(TESTSTRING, TESTSTRING_LEFT_SPACES.trimLeft()); 68.142 +test(TESTSTRING_LEFT_SPACES, TESTSTRING_LEFT_SPACES.trimRight()); 68.143 + 68.144 +test(TESTSTRING, TESTSTRING_RIGHT_SPACES.trim()); 68.145 +test(TESTSTRING_RIGHT_SPACES, TESTSTRING_RIGHT_SPACES.trimLeft()); 68.146 +test(TESTSTRING, TESTSTRING_RIGHT_SPACES.trimRight()); 68.147 + 68.148 +test(TESTSTRING, TESTSTRING_BOTH_SPACES.trim()); 68.149 +test(TESTSTRING_RIGHT_SPACES, TESTSTRING_BOTH_SPACES.trimLeft()); 68.150 +test(TESTSTRING_LEFT_SPACES, TESTSTRING_BOTH_SPACES.trimRight()); 68.151 + 68.152 +test(TESTSTRING_MIDDLE_SPACES, TESTSTRING_MIDDLE_SPACES.trim()); 68.153 +test(TESTSTRING_MIDDLE_SPACES, TESTSTRING_MIDDLE_SPACES.trimLeft()); 68.154 +test(TESTSTRING_MIDDLE_SPACES, TESTSTRING_MIDDLE_SPACES.trimRight()); 68.155 + 68.156 +test("", WHITESPACE.trim()); 68.157 +test("", WHITESPACE.trimLeft()); 68.158 +test("", WHITESPACE.trimRight()); 68.159 + 68.160 +test(TESTSTRING, TESTSTRING_LEFT_WHITESPACE.trim()); 68.161 +test(TESTSTRING, TESTSTRING_LEFT_WHITESPACE.trimLeft()); 68.162 +test(TESTSTRING_LEFT_WHITESPACE, TESTSTRING_LEFT_WHITESPACE.trimRight()); 68.163 + 68.164 +test(TESTSTRING, TESTSTRING_RIGHT_WHITESPACE.trim()); 68.165 +test(TESTSTRING_RIGHT_WHITESPACE, TESTSTRING_RIGHT_WHITESPACE.trimLeft()); 68.166 +test(TESTSTRING, TESTSTRING_RIGHT_WHITESPACE.trimRight()); 68.167 + 68.168 +test(TESTSTRING, TESTSTRING_BOTH_WHITESPACE.trim()); 68.169 +test(TESTSTRING_RIGHT_WHITESPACE, TESTSTRING_BOTH_WHITESPACE.trimLeft()); 68.170 +test(TESTSTRING_LEFT_WHITESPACE, TESTSTRING_BOTH_WHITESPACE.trimRight()); 68.171 + 68.172 +test(TESTSTRING_MIDDLE_WHITESPACE, TESTSTRING_MIDDLE_WHITESPACE.trim()); 68.173 +test(TESTSTRING_MIDDLE_WHITESPACE, TESTSTRING_MIDDLE_WHITESPACE.trimLeft()); 68.174 +test(TESTSTRING_MIDDLE_WHITESPACE, TESTSTRING_MIDDLE_WHITESPACE.trimRight());
69.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 69.2 +++ b/test/script/basic/JDK-8022903.js Thu Aug 29 16:34:31 2013 -0700 69.3 @@ -0,0 +1,55 @@ 69.4 +/* 69.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 69.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 69.7 + * 69.8 + * This code is free software; you can redistribute it and/or modify it 69.9 + * under the terms of the GNU General Public License version 2 only, as 69.10 + * published by the Free Software Foundation. 69.11 + * 69.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 69.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 69.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 69.15 + * version 2 for more details (a copy is included in the LICENSE file that 69.16 + * accompanied this code). 69.17 + * 69.18 + * You should have received a copy of the GNU General Public License version 69.19 + * 2 along with this work; if not, write to the Free Software Foundation, 69.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 69.21 + * 69.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 69.23 + * or visit www.oracle.com if you need additional information or have any 69.24 + * questions. 69.25 + */ 69.26 + 69.27 +/** 69.28 + * JDK-8022903: Enhance for-in and for-each for Lists and Maps 69.29 + * 69.30 + * @test 69.31 + * @run 69.32 + */ 69.33 + 69.34 +var colors = new java.util.ArrayList() 69.35 +colors.add("red") 69.36 +colors.add("purple") 69.37 +colors.add("pink") 69.38 + 69.39 +for(var index in colors) { 69.40 + print("colors[" + index + "]=" + colors[index]) 69.41 +} 69.42 + 69.43 +for each(var color in colors) { 69.44 + print(color) 69.45 +} 69.46 + 69.47 +var capitals = new java.util.LinkedHashMap() 69.48 +capitals.Sweden = "Stockholm" 69.49 +capitals.Hungary = "Budapet" 69.50 +capitals.Croatia = "Zagreb" 69.51 + 69.52 +for(var key in capitals) { 69.53 + print("capital of " + key + " is " + capitals[key]) 69.54 +} 69.55 + 69.56 +for each(var capital in capitals) { 69.57 + print(capital) 69.58 +}
70.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 70.2 +++ b/test/script/basic/JDK-8022903.js.EXPECTED Thu Aug 29 16:34:31 2013 -0700 70.3 @@ -0,0 +1,12 @@ 70.4 +colors[0]=red 70.5 +colors[1]=purple 70.6 +colors[2]=pink 70.7 +red 70.8 +purple 70.9 +pink 70.10 +capital of Sweden is Stockholm 70.11 +capital of Hungary is Budapet 70.12 +capital of Croatia is Zagreb 70.13 +Stockholm 70.14 +Budapet 70.15 +Zagreb
71.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 71.2 +++ b/test/script/basic/JDK-8023368.js Thu Aug 29 16:34:31 2013 -0700 71.3 @@ -0,0 +1,73 @@ 71.4 +/* 71.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 71.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 71.7 + * 71.8 + * This code is free software; you can redistribute it and/or modify it 71.9 + * under the terms of the GNU General Public License version 2 only, as 71.10 + * published by the Free Software Foundation. 71.11 + * 71.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 71.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 71.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 71.15 + * version 2 for more details (a copy is included in the LICENSE file that 71.16 + * accompanied this code). 71.17 + * 71.18 + * You should have received a copy of the GNU General Public License version 71.19 + * 2 along with this work; if not, write to the Free Software Foundation, 71.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 71.21 + * 71.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 71.23 + * or visit www.oracle.com if you need additional information or have any 71.24 + * questions. 71.25 + */ 71.26 + 71.27 +/** 71.28 + * JDK-8023368: Instance __proto__ property should exist and be writable. 71.29 + * 71.30 + * @test 71.31 + * @run 71.32 + */ 71.33 + 71.34 +load("nashorn:mozilla_compat.js"); 71.35 + 71.36 +// function to force same callsites 71.37 +function check(obj) { 71.38 + print(obj.func()); 71.39 + print(obj.x); 71.40 + print(obj.toString()); 71.41 +} 71.42 + 71.43 +function Func() { 71.44 +} 71.45 + 71.46 +Func.prototype.func = function() { 71.47 + return "Func.prototype.func"; 71.48 +} 71.49 + 71.50 +Func.prototype.x = "hello"; 71.51 + 71.52 +var obj = new Func(); 71.53 +var obj2 = Object.create(obj); 71.54 + 71.55 +// check direct and indirect __proto__ change 71.56 +check(obj); 71.57 +check(obj2); 71.58 +obj.__proto__ = { 71.59 + func: function() { 71.60 + return "obj.__proto__.func @ " + __LINE__; 71.61 + }, 71.62 + x: 344 71.63 +}; 71.64 + 71.65 +check(obj); 71.66 +check(obj2); 71.67 + 71.68 +// check indirect (1 and 2 levels) __proto__ function change 71.69 +obj.__proto__.__proto__ = { 71.70 + toString: function() { 71.71 + return "new object.toString"; 71.72 + } 71.73 +}; 71.74 + 71.75 +check(obj); 71.76 +check(obj2);
72.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 72.2 +++ b/test/script/basic/JDK-8023368.js.EXPECTED Thu Aug 29 16:34:31 2013 -0700 72.3 @@ -0,0 +1,18 @@ 72.4 +Func.prototype.func 72.5 +hello 72.6 +[object Object] 72.7 +Func.prototype.func 72.8 +hello 72.9 +[object Object] 72.10 +obj.__proto__.func @ 57 72.11 +344 72.12 +[object Object] 72.13 +obj.__proto__.func @ 57 72.14 +344 72.15 +[object Object] 72.16 +obj.__proto__.func @ 57 72.17 +344 72.18 +new object.toString 72.19 +obj.__proto__.func @ 57 72.20 +344 72.21 +new object.toString
73.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 73.2 +++ b/test/script/basic/JDK-8023368_2.js Thu Aug 29 16:34:31 2013 -0700 73.3 @@ -0,0 +1,73 @@ 73.4 +/* 73.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 73.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 73.7 + * 73.8 + * This code is free software; you can redistribute it and/or modify it 73.9 + * under the terms of the GNU General Public License version 2 only, as 73.10 + * published by the Free Software Foundation. 73.11 + * 73.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 73.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 73.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 73.15 + * version 2 for more details (a copy is included in the LICENSE file that 73.16 + * accompanied this code). 73.17 + * 73.18 + * You should have received a copy of the GNU General Public License version 73.19 + * 2 along with this work; if not, write to the Free Software Foundation, 73.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 73.21 + * 73.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 73.23 + * or visit www.oracle.com if you need additional information or have any 73.24 + * questions. 73.25 + */ 73.26 + 73.27 +/** 73.28 + * JDK-8023368: Instance __proto__ property should exist and be writable. 73.29 + * 73.30 + * @test 73.31 + * @run 73.32 + */ 73.33 + 73.34 +// check Object.setPrototypeOf extension rather than using __proto__ 73.35 + 73.36 +// function to force same callsites 73.37 +function check(obj) { 73.38 + print(obj.func()); 73.39 + print(obj.x); 73.40 + print(obj.toString()); 73.41 +} 73.42 + 73.43 +function Func() { 73.44 +} 73.45 + 73.46 +Func.prototype.func = function() { 73.47 + return "Func.prototype.func"; 73.48 +} 73.49 + 73.50 +Func.prototype.x = "hello"; 73.51 + 73.52 +var obj = new Func(); 73.53 +var obj2 = Object.create(obj); 73.54 + 73.55 +// check direct and indirect __proto__ change 73.56 +check(obj); 73.57 +check(obj2); 73.58 +Object.setPrototypeOf(obj, { 73.59 + func: function() { 73.60 + return "obj.__proto__.func @ " + __LINE__; 73.61 + }, 73.62 + x: 344 73.63 +}); 73.64 + 73.65 +check(obj); 73.66 +check(obj2); 73.67 + 73.68 +// check indirect (1 and 2 levels) __proto__ function change 73.69 +Object.setPrototypeOf(Object.getPrototypeOf(obj), { 73.70 + toString: function() { 73.71 + return "new object.toString"; 73.72 + } 73.73 +}); 73.74 + 73.75 +check(obj); 73.76 +check(obj2);
74.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 74.2 +++ b/test/script/basic/JDK-8023368_2.js.EXPECTED Thu Aug 29 16:34:31 2013 -0700 74.3 @@ -0,0 +1,18 @@ 74.4 +Func.prototype.func 74.5 +hello 74.6 +[object Object] 74.7 +Func.prototype.func 74.8 +hello 74.9 +[object Object] 74.10 +obj.__proto__.func @ 57 74.11 +344 74.12 +[object Object] 74.13 +obj.__proto__.func @ 57 74.14 +344 74.15 +[object Object] 74.16 +obj.__proto__.func @ 57 74.17 +344 74.18 +new object.toString 74.19 +obj.__proto__.func @ 57 74.20 +344 74.21 +new object.toString
75.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 75.2 +++ b/test/script/basic/JDK-8023373.js Thu Aug 29 16:34:31 2013 -0700 75.3 @@ -0,0 +1,84 @@ 75.4 +/* 75.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 75.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 75.7 + * 75.8 + * This code is free software; you can redistribute it and/or modify it 75.9 + * under the terms of the GNU General Public License version 2 only, as 75.10 + * published by the Free Software Foundation. 75.11 + * 75.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 75.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 75.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 75.15 + * version 2 for more details (a copy is included in the LICENSE file that 75.16 + * accompanied this code). 75.17 + * 75.18 + * You should have received a copy of the GNU General Public License version 75.19 + * 2 along with this work; if not, write to the Free Software Foundation, 75.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 75.21 + * 75.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 75.23 + * or visit www.oracle.com if you need additional information or have any 75.24 + * questions. 75.25 + */ 75.26 + 75.27 +/** 75.28 + * JDK-8023373: allow super invocation for adapters 75.29 + * 75.30 + * @test 75.31 + * @run 75.32 + */ 75.33 + 75.34 +var CharArray = Java.type("char[]") 75.35 +var jString = Java.type("java.lang.String") 75.36 +var Character = Java.type("java.lang.Character") 75.37 + 75.38 +function capitalize(s) { 75.39 + if(s instanceof CharArray) { 75.40 + return new jString(s).toUpperCase() 75.41 + } 75.42 + if(s instanceof jString) { 75.43 + return s.toUpperCase() 75.44 + } 75.45 + return Character.toUpperCase(s) // must be int 75.46 +} 75.47 + 75.48 +var sw = new (Java.type("java.io.StringWriter")) 75.49 + 75.50 +var FilterWriterAdapter = Java.extend(Java.type("java.io.FilterWriter")) 75.51 + 75.52 +var cw = new FilterWriterAdapter(sw) { 75.53 + write: function(s, off, len) { 75.54 + s = capitalize(s) 75.55 + // Must handle overloads by arity 75.56 + if(off === undefined) { 75.57 + cw.super$write(s, 0, s.length()) 75.58 + } else if (typeof s === "string") { 75.59 + cw.super$write(s, off, len) 75.60 + } 75.61 + } 75.62 +} 75.63 + 75.64 +cw.write("abcd") 75.65 +cw.write("e".charAt(0)) 75.66 +cw.write("fgh".toCharArray()) 75.67 +cw.write("**ijk**", 2, 3) 75.68 +cw.write("***lmno**".toCharArray(), 3, 4) 75.69 +cw.flush() 75.70 +print(sw) 75.71 + 75.72 +// Can invoke super for Object methods 75.73 +print("cw has super hashCode(): " + (typeof cw.super$hashCode === "function")) 75.74 +print("cw has super equals(): " + (typeof cw.super$equals === "function")) 75.75 +// Can't invoke super for final methods 75.76 +print("cw has no super getClass(): " + (typeof cw.super$getClass === "undefined")) 75.77 +print("cw has no super wait(): " + (typeof cw.super$wait === "undefined")) 75.78 + 75.79 +var r = new (Java.type("java.lang.Runnable"))(function() {}) 75.80 +// Can't invoke super for abstract methods 75.81 +print("r has no super run(): " + (typeof r.super$run === "undefined")) 75.82 +// Interfaces can also invoke super Object methods 75.83 +print("r has super hashCode(): " + (typeof r.super$hashCode === "function")) 75.84 +print("r has super equals(): " + (typeof r.super$equals === "function")) 75.85 +// But still can't invoke final methods 75.86 +print("r has no super getClass(): " + (typeof r.super$getClass === "undefined")) 75.87 +print("r has no super wait(): " + (typeof r.super$wait === "undefined"))
76.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 76.2 +++ b/test/script/basic/JDK-8023373.js.EXPECTED Thu Aug 29 16:34:31 2013 -0700 76.3 @@ -0,0 +1,10 @@ 76.4 +ABCDEFGHIJKLMNO 76.5 +cw has super hashCode(): true 76.6 +cw has super equals(): true 76.7 +cw has no super getClass(): true 76.8 +cw has no super wait(): true 76.9 +r has no super run(): true 76.10 +r has super hashCode(): true 76.11 +r has super equals(): true 76.12 +r has no super getClass(): true 76.13 +r has no super wait(): true
77.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 77.2 +++ b/test/script/basic/JDK-8023531.js Thu Aug 29 16:34:31 2013 -0700 77.3 @@ -0,0 +1,106 @@ 77.4 +/* 77.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 77.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 77.7 + * 77.8 + * This code is free software; you can redistribute it and/or modify it 77.9 + * under the terms of the GNU General Public License version 2 only, as 77.10 + * published by the Free Software Foundation. 77.11 + * 77.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 77.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 77.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 77.15 + * version 2 for more details (a copy is included in the LICENSE file that 77.16 + * accompanied this code). 77.17 + * 77.18 + * You should have received a copy of the GNU General Public License version 77.19 + * 2 along with this work; if not, write to the Free Software Foundation, 77.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 77.21 + * 77.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 77.23 + * or visit www.oracle.com if you need additional information or have any 77.24 + * questions. 77.25 + */ 77.26 + 77.27 +/** 77.28 + * JDK-8023531: new RegExp('').toString() should return '/(?:)/' 77.29 + * 77.30 + * @test 77.31 + * @run 77.32 + */ 77.33 + 77.34 +if (new RegExp("").toString() !== "/(?:)/") { 77.35 + throw new Error(); 77.36 +} else if (!(new RegExp("").test(""))) { 77.37 + throw new Error(); 77.38 +} 77.39 + 77.40 +if (new RegExp("", "g").toString() !== "/(?:)/g") { 77.41 + throw new Error(); 77.42 +} else if (!(new RegExp("", "g").test(""))) { 77.43 + throw new Error(); 77.44 +} 77.45 + 77.46 +if (new RegExp("", "i").toString() !== "/(?:)/i") { 77.47 + throw new Error(); 77.48 +} else if (!(new RegExp("", "i").test(""))) { 77.49 + throw new Error(); 77.50 +} 77.51 + 77.52 +if (new RegExp("", "m").toString() !== "/(?:)/m") { 77.53 + throw new Error(); 77.54 +} else if (!(new RegExp("", "m").test(""))) { 77.55 + throw new Error(); 77.56 +} 77.57 + 77.58 +if (RegExp("").toString() !== "/(?:)/") { 77.59 + throw new Error(); 77.60 +} else if (!RegExp("").test("")) { 77.61 + throw new Error(); 77.62 +} 77.63 + 77.64 +if (RegExp("", "g").toString() !== "/(?:)/g") { 77.65 + throw new Error(); 77.66 +} else if (!RegExp("", "g").test("")) { 77.67 + throw new Error(); 77.68 +} 77.69 + 77.70 +if (RegExp("", "i").toString() !== "/(?:)/i") { 77.71 + throw new Error(); 77.72 +} else if (!RegExp("", "i").test("")) { 77.73 + throw new Error(); 77.74 +} 77.75 + 77.76 +if (RegExp("", "m").toString() !== "/(?:)/m") { 77.77 + throw new Error(); 77.78 +} else if (!RegExp("", "m").test("")) { 77.79 + throw new Error(); 77.80 +} 77.81 + 77.82 +var re = /abc/; 77.83 +re.compile(""); 77.84 +if (re.toString() !== "/(?:)/") { 77.85 + throw new Error(); 77.86 +} else if (!re.test("")) { 77.87 + throw new Error(); 77.88 +} 77.89 + 77.90 +re.compile("", "g"); 77.91 +if (re.toString() !== "/(?:)/g") { 77.92 + throw new Error(); 77.93 +} else if (!re.test("")) { 77.94 + throw new Error(); 77.95 +} 77.96 + 77.97 +re.compile("", "i"); 77.98 +if (re.toString() !== "/(?:)/i") { 77.99 + throw new Error(); 77.100 +} else if (!re.test("")) { 77.101 + throw new Error(); 77.102 +} 77.103 + 77.104 +re.compile("", "m"); 77.105 +if (re.toString() !== "/(?:)/m") { 77.106 + throw new Error(); 77.107 +} else if (!re.test("")) { 77.108 + throw new Error(); 77.109 +}
78.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 78.2 +++ b/test/script/basic/JDK-8023551.js Thu Aug 29 16:34:31 2013 -0700 78.3 @@ -0,0 +1,42 @@ 78.4 +/* 78.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 78.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 78.7 + * 78.8 + * This code is free software; you can redistribute it and/or modify it 78.9 + * under the terms of the GNU General Public License version 2 only, as 78.10 + * published by the Free Software Foundation. 78.11 + * 78.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 78.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 78.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 78.15 + * version 2 for more details (a copy is included in the LICENSE file that 78.16 + * accompanied this code). 78.17 + * 78.18 + * You should have received a copy of the GNU General Public License version 78.19 + * 2 along with this work; if not, write to the Free Software Foundation, 78.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 78.21 + * 78.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 78.23 + * or visit www.oracle.com if you need additional information or have any 78.24 + * questions. 78.25 + */ 78.26 + 78.27 +/** 78.28 + * JDK-8023551: Mirror functions can not be invoked using invokeMethod, invokeFunction 78.29 + * 78.30 + * @test 78.31 + * @run 78.32 + */ 78.33 + 78.34 +var m = new javax.script.ScriptEngineManager(); 78.35 +var e = m.getEngineByName("nashorn"); 78.36 + 78.37 +function func(x) { 78.38 + print("func: " + x); 78.39 +} 78.40 + 78.41 +e.put("func", func); 78.42 +e.invokeFunction("func", "hello"); 78.43 + 78.44 +var obj = e.eval("({ foo: func })"); 78.45 +e.invokeMethod(obj, "foo", "world");
79.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 79.2 +++ b/test/script/basic/JDK-8023551.js.EXPECTED Thu Aug 29 16:34:31 2013 -0700 79.3 @@ -0,0 +1,2 @@ 79.4 +func: hello 79.5 +func: world
80.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 80.2 +++ b/test/script/basic/JDK-8023630.js Thu Aug 29 16:34:31 2013 -0700 80.3 @@ -0,0 +1,94 @@ 80.4 +/* 80.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 80.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 80.7 + * 80.8 + * This code is free software; you can redistribute it and/or modify it 80.9 + * under the terms of the GNU General Public License version 2 only, as 80.10 + * published by the Free Software Foundation. 80.11 + * 80.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 80.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 80.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 80.15 + * version 2 for more details (a copy is included in the LICENSE file that 80.16 + * accompanied this code). 80.17 + * 80.18 + * You should have received a copy of the GNU General Public License version 80.19 + * 2 along with this work; if not, write to the Free Software Foundation, 80.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 80.21 + * 80.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 80.23 + * or visit www.oracle.com if you need additional information or have any 80.24 + * questions. 80.25 + */ 80.26 + 80.27 +/** 80.28 + * JDK-8023630: Implement Java.super() as the preferred way to call super methods 80.29 + * 80.30 + * @test 80.31 + * @run 80.32 + */ 80.33 + 80.34 +var CharArray = Java.type("char[]") 80.35 +var jString = Java.type("java.lang.String") 80.36 +var Character = Java.type("java.lang.Character") 80.37 + 80.38 +function capitalize(s) { 80.39 + if(s instanceof CharArray) { 80.40 + return new jString(s).toUpperCase() 80.41 + } 80.42 + if(s instanceof jString) { 80.43 + return s.toUpperCase() 80.44 + } 80.45 + return Character.toUpperCase(s) // must be int 80.46 +} 80.47 + 80.48 +var sw = new (Java.type("java.io.StringWriter")) 80.49 + 80.50 +var FilterWriterAdapter = Java.extend(Java.type("java.io.FilterWriter")) 80.51 + 80.52 +var cw = new FilterWriterAdapter(sw) { 80.53 + write: function(s, off, len) { 80.54 + s = capitalize(s) 80.55 + // Must handle overloads by arity 80.56 + if(off === undefined) { 80.57 + cw_super.write(s, 0, s.length()) 80.58 + } else if (typeof s === "string") { 80.59 + cw_super.write(s, off, len) 80.60 + } 80.61 + } 80.62 +} 80.63 +var cw_super = Java.super(cw) 80.64 + 80.65 +cw.write("abcd") 80.66 +cw.write("e".charAt(0)) 80.67 +cw.write("fgh".toCharArray()) 80.68 +cw.write("**ijk**", 2, 3) 80.69 +cw.write("***lmno**".toCharArray(), 3, 4) 80.70 +cw.flush() 80.71 +print(sw) 80.72 + 80.73 +// Can invoke super for Object methods 80.74 +print("cw_super has hashCode(): " + (typeof cw_super.hashCode === "function")) 80.75 +print("cw_super has super equals(): " + (typeof cw_super.equals === "function")) 80.76 +// Can't invoke super for final methods 80.77 +print("cw_super has no getClass(): " + (typeof cw_super.getClass === "undefined")) 80.78 +print("cw_super has no wait(): " + (typeof cw_super.wait === "undefined")) 80.79 + 80.80 +var r = new (Java.type("java.lang.Runnable"))(function() {}) 80.81 +var r_super = Java.super(r) 80.82 + 80.83 +// Can't invoke super for abstract methods 80.84 +print("r_super has no run(): " + (typeof r_super.run === "undefined")) 80.85 +// Interfaces can also invoke super Object methods 80.86 +print("r_super has hashCode(): " + (typeof r_super.hashCode === "function")) 80.87 +print("r_super has equals(): " + (typeof r_super.equals === "function")) 80.88 +// But still can't invoke final methods 80.89 +print("r_super has no getClass(): " + (typeof r_super.getClass === "undefined")) 80.90 +print("r_super has no wait(): " + (typeof r_super.wait === "undefined")) 80.91 + 80.92 +var name = "write" 80.93 +print("cw_super can access write through [] getter: " + (typeof cw_super[name] === "function")) 80.94 +var name = "hashCode" 80.95 +print("cw_super can access hashCode through [] getter: " + (typeof cw_super[name] === "function")) 80.96 +var name = "getClass" 80.97 +print("cw_super can not access getClass through [] getter: " + (typeof cw_super[name] === "undefined"))
81.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 81.2 +++ b/test/script/basic/JDK-8023630.js.EXPECTED Thu Aug 29 16:34:31 2013 -0700 81.3 @@ -0,0 +1,13 @@ 81.4 +ABCDEFGHIJKLMNO 81.5 +cw_super has hashCode(): true 81.6 +cw_super has super equals(): true 81.7 +cw_super has no getClass(): true 81.8 +cw_super has no wait(): true 81.9 +r_super has no run(): true 81.10 +r_super has hashCode(): true 81.11 +r_super has equals(): true 81.12 +r_super has no getClass(): true 81.13 +r_super has no wait(): true 81.14 +cw_super can access write through [] getter: true 81.15 +cw_super can access hashCode through [] getter: true 81.16 +cw_super can not access getClass through [] getter: true
82.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 82.2 +++ b/test/script/basic/JDK-8023650.js Thu Aug 29 16:34:31 2013 -0700 82.3 @@ -0,0 +1,109 @@ 82.4 +/* 82.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 82.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 82.7 + * 82.8 + * This code is free software; you can redistribute it and/or modify it 82.9 + * under the terms of the GNU General Public License version 2 only, as 82.10 + * published by the Free Software Foundation. 82.11 + * 82.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 82.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 82.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 82.15 + * version 2 for more details (a copy is included in the LICENSE file that 82.16 + * accompanied this code). 82.17 + * 82.18 + * You should have received a copy of the GNU General Public License version 82.19 + * 2 along with this work; if not, write to the Free Software Foundation, 82.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 82.21 + * 82.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 82.23 + * or visit www.oracle.com if you need additional information or have any 82.24 + * questions. 82.25 + */ 82.26 + 82.27 +/** 82.28 + * JDK-8023650: Regexp m flag does not recognize CRNL or CR 82.29 + * 82.30 + * @test 82.31 + * @run 82.32 + */ 82.33 + 82.34 +if (!/^Connection: close$/m.test('\r\n\r\nConnection: close\r\n\r\n')) { 82.35 + throw new Error(); 82.36 +} 82.37 + 82.38 +if (!/^Connection: close$/m.test('\n\nConnection: close\n\n')) { 82.39 + throw new Error(); 82.40 +} 82.41 + 82.42 +if (!/^Connection: close$/m.test('\r\rConnection: close\r\r')) { 82.43 + throw new Error(); 82.44 +} 82.45 + 82.46 +if (!/^Connection: close$/m.test('\u2028\u2028Connection: close\u2028\u2028')) { 82.47 + throw new Error(); 82.48 +} 82.49 + 82.50 +if (!/^Connection: close$/m.test('\u2029\u2029Connection: close\u2029\u2029')) { 82.51 + throw new Error(); 82.52 +} 82.53 + 82.54 +var result = /a(.*)/.exec("a\r"); 82.55 +if (!result || result[0] != 'a' || result[1] != '') { 82.56 + throw new Error(); 82.57 +} 82.58 + 82.59 +result = /a(.*)/m.exec("a\r"); 82.60 +if (!result || result[0] != 'a' || result[1] != '') { 82.61 + throw new Error(); 82.62 +} 82.63 + 82.64 +result = /a(.*)/.exec("a\n"); 82.65 +if (!result || result[0] != 'a' || result[1] != '') { 82.66 + throw new Error(); 82.67 +} 82.68 + 82.69 +result = /a(.*)/m.exec("a\n"); 82.70 +if (!result || result[0] != 'a' || result[1] != '') { 82.71 + throw new Error(); 82.72 +} 82.73 + 82.74 +result = /a(.*)/.exec("a\r\n"); 82.75 +if (!result || result[0] != 'a' || result[1] != '') { 82.76 + throw new Error(); 82.77 +} 82.78 + 82.79 +result = /a(.*)/m.exec("a\r\n"); 82.80 +if (!result || result[0] != 'a' || result[1] != '') { 82.81 + throw new Error(); 82.82 +} 82.83 + 82.84 +result = /a(.*)/.exec("a\u2028"); 82.85 +if (!result || result[0] != 'a' || result[1] != '') { 82.86 + throw new Error(); 82.87 +} 82.88 + 82.89 +result = /a(.*)/m.exec("a\u2029"); 82.90 +if (!result || result[0] != 'a' || result[1] != '') { 82.91 + throw new Error(); 82.92 +} 82.93 + 82.94 +if (/a$/.test("a\n")) { 82.95 + throw new Error(); 82.96 +} 82.97 + 82.98 +if (/a$/.test("a\r")) { 82.99 + throw new Error(); 82.100 +} 82.101 + 82.102 +if (/a$/.test("a\r\n")) { 82.103 + throw new Error(); 82.104 +} 82.105 + 82.106 +if (/a$/.test("a\u2028")) { 82.107 + throw new Error(); 82.108 +} 82.109 + 82.110 +if (/a$/.test("a\u2029")) { 82.111 + throw new Error(); 82.112 +}
83.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 83.2 +++ b/test/script/basic/JDK-8023780.js Thu Aug 29 16:34:31 2013 -0700 83.3 @@ -0,0 +1,38 @@ 83.4 +/* 83.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 83.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 83.7 + * 83.8 + * This code is free software; you can redistribute it and/or modify it 83.9 + * under the terms of the GNU General Public License version 2 only, as 83.10 + * published by the Free Software Foundation. 83.11 + * 83.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 83.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 83.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 83.15 + * version 2 for more details (a copy is included in the LICENSE file that 83.16 + * accompanied this code). 83.17 + * 83.18 + * You should have received a copy of the GNU General Public License version 83.19 + * 2 along with this work; if not, write to the Free Software Foundation, 83.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 83.21 + * 83.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 83.23 + * or visit www.oracle.com if you need additional information or have any 83.24 + * questions. 83.25 + */ 83.26 + 83.27 +/** 83.28 + * JDK-8023780: Gracefully handle @CS methods while binding bean properties 83.29 + * 83.30 + * @test 83.31 + * @run 83.32 + */ 83.33 + 83.34 +var obj = {} 83.35 +Object.bindProperties(obj, java.lang.Thread.currentThread()); 83.36 + 83.37 +print(typeof obj.getName === "function") 83.38 +print(typeof obj.getCurrentContext === "undefined") 83.39 + 83.40 +print(Object.hasOwnProperty("name")) 83.41 +print(!Object.hasOwnProperty("currentContext"))
84.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 84.2 +++ b/test/script/basic/JDK-8023780.js.EXPECTED Thu Aug 29 16:34:31 2013 -0700 84.3 @@ -0,0 +1,4 @@ 84.4 +true 84.5 +true 84.6 +true 84.7 +true
85.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 85.2 +++ b/test/script/basic/JDK-8023784.js Thu Aug 29 16:34:31 2013 -0700 85.3 @@ -0,0 +1,66 @@ 85.4 +/* 85.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 85.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 85.7 + * 85.8 + * This code is free software; you can redistribute it and/or modify it 85.9 + * under the terms of the GNU General Public License version 2 only, as 85.10 + * published by the Free Software Foundation. 85.11 + * 85.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 85.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 85.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 85.15 + * version 2 for more details (a copy is included in the LICENSE file that 85.16 + * accompanied this code). 85.17 + * 85.18 + * You should have received a copy of the GNU General Public License version 85.19 + * 2 along with this work; if not, write to the Free Software Foundation, 85.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 85.21 + * 85.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 85.23 + * or visit www.oracle.com if you need additional information or have any 85.24 + * questions. 85.25 + */ 85.26 + 85.27 +/** 85.28 + * JDK-8023784: Object.prototype.toString should contain the class name for all instances 85.29 + * 85.30 + * @test 85.31 + * @run 85.32 + */ 85.33 + 85.34 +// two parts to this bug -- typed array don't have proper [[Class]] property 85.35 + 85.36 +print(Object.prototype.toString.call(new ArrayBuffer(1))); 85.37 +print(Object.prototype.toString.call(new Int8Array(1))); 85.38 +print(Object.prototype.toString.call(new Int16Array(1))); 85.39 +print(Object.prototype.toString.call(new Int32Array(1))); 85.40 +print(Object.prototype.toString.call(new Uint8Array(1))); 85.41 +print(Object.prototype.toString.call(new Uint8ClampedArray(1))); 85.42 +print(Object.prototype.toString.call(new Uint16Array(1))); 85.43 +print(Object.prototype.toString.call(new Uint32Array(1))); 85.44 +print(Object.prototype.toString.call(new Float32Array(1))); 85.45 +print(Object.prototype.toString.call(new Float64Array(1))); 85.46 + 85.47 +// second part is that Object.prototype.toString does not handle mirror 85.48 +// in the manner expected. 85.49 + 85.50 +var global = loadWithNewGlobal({ 85.51 + name: "test", 85.52 + script: "this" 85.53 +}); 85.54 + 85.55 +print(Object.prototype.toString.call(new global.Object())); 85.56 +print(Object.prototype.toString.call(new global.Array())); 85.57 +print(Object.prototype.toString.call(new global.RegExp())); 85.58 +print(Object.prototype.toString.call(new global.Error("error!"))); 85.59 +print(Object.prototype.toString.call(global.Object)); 85.60 +print(Object.prototype.toString.call(new global.ArrayBuffer(1))); 85.61 +print(Object.prototype.toString.call(new global.Int8Array(1))); 85.62 +print(Object.prototype.toString.call(new global.Int16Array(1))); 85.63 +print(Object.prototype.toString.call(new global.Int32Array(1))); 85.64 +print(Object.prototype.toString.call(new global.Uint8Array(1))); 85.65 +print(Object.prototype.toString.call(new global.Uint8ClampedArray(1))); 85.66 +print(Object.prototype.toString.call(new global.Uint16Array(1))); 85.67 +print(Object.prototype.toString.call(new global.Uint32Array(1))); 85.68 +print(Object.prototype.toString.call(new global.Float32Array(1))); 85.69 +print(Object.prototype.toString.call(new global.Float64Array(1)));
86.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 86.2 +++ b/test/script/basic/JDK-8023784.js.EXPECTED Thu Aug 29 16:34:31 2013 -0700 86.3 @@ -0,0 +1,25 @@ 86.4 +[object ArrayBuffer] 86.5 +[object Int8Array] 86.6 +[object Int16Array] 86.7 +[object Int32Array] 86.8 +[object Uint8Array] 86.9 +[object Uint8ClampedArray] 86.10 +[object Uint16Array] 86.11 +[object Uint32Array] 86.12 +[object Float32Array] 86.13 +[object Float64Array] 86.14 +[object Object] 86.15 +[object Array] 86.16 +[object RegExp] 86.17 +[object Error] 86.18 +[object Function] 86.19 +[object ArrayBuffer] 86.20 +[object Int8Array] 86.21 +[object Int16Array] 86.22 +[object Int32Array] 86.23 +[object Uint8Array] 86.24 +[object Uint8ClampedArray] 86.25 +[object Uint16Array] 86.26 +[object Uint32Array] 86.27 +[object Float32Array] 86.28 +[object Float64Array]
87.1 --- a/test/script/basic/NASHORN-377.js.EXPECTED Thu Aug 29 09:42:13 2013 -0700 87.2 +++ b/test/script/basic/NASHORN-377.js.EXPECTED Thu Aug 29 16:34:31 2013 -0700 87.3 @@ -1,5 +1,5 @@ 87.4 8 8 true undefined 87.5 -[object Object] [object Object] [object Object] 87.6 +[object ArrayBuffer] [object ArrayBuffer] [object Int8Array] 87.7 0 8 8 1 87.8 0 8 8 1 87.9 0 8 8 1
88.1 --- a/test/script/basic/NASHORN-397.js Thu Aug 29 09:42:13 2013 -0700 88.2 +++ b/test/script/basic/NASHORN-397.js Thu Aug 29 16:34:31 2013 -0700 88.3 @@ -35,7 +35,10 @@ 88.4 fail("typeof(5).x is not 'number'"); 88.5 } 88.6 88.7 -if (typeof (java.lang.System.out) != 'object') { 88.8 +// It is function because PrintStream implements Closeable, which is 88.9 +// marked with @FunctionalInterface. Yes, this means calling a stream 88.10 +// like "stream()" closes it. 88.11 +if (typeof (java.lang.System.out) != 'function') { 88.12 fail("typeof java.lang.System.out is not 'object'"); 88.13 } 88.14
89.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 89.2 +++ b/test/script/basic/circular_proto.js Thu Aug 29 16:34:31 2013 -0700 89.3 @@ -0,0 +1,46 @@ 89.4 +/* 89.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 89.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 89.7 + * 89.8 + * This code is free software; you can redistribute it and/or modify it 89.9 + * under the terms of the GNU General Public License version 2 only, as 89.10 + * published by the Free Software Foundation. 89.11 + * 89.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 89.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 89.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 89.15 + * version 2 for more details (a copy is included in the LICENSE file that 89.16 + * accompanied this code). 89.17 + * 89.18 + * You should have received a copy of the GNU General Public License version 89.19 + * 2 along with this work; if not, write to the Free Software Foundation, 89.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 89.21 + * 89.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 89.23 + * or visit www.oracle.com if you need additional information or have any 89.24 + * questions. 89.25 + */ 89.26 + 89.27 +/** 89.28 + * JDK-8023368: Instance __proto__ property should exist and be writable. 89.29 + * 89.30 + * @test 89.31 + * @run 89.32 + */ 89.33 + 89.34 +// check that we cannot create __proto__ cycle 89.35 +load("nashorn:mozilla_compat.js"); 89.36 + 89.37 +var obj = {}; 89.38 +var obj2 = Object.create(obj); 89.39 + 89.40 +// attempt to create __proto__ cycle 89.41 +try { 89.42 + obj.__proto__ = obj2; 89.43 + fail("Should have thrown TypeError"); 89.44 +} catch (e) { 89.45 + if (! (e instanceof TypeError)) { 89.46 + fail("Expected TypeError, got " + e); 89.47 + } 89.48 + print(e); 89.49 +}
90.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 90.2 +++ b/test/script/basic/circular_proto.js.EXPECTED Thu Aug 29 16:34:31 2013 -0700 90.3 @@ -0,0 +1,1 @@ 90.4 +TypeError: Cannot create__proto__ cycle for [object Object]
91.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 91.2 +++ b/test/script/basic/mirror_proto_assign.js Thu Aug 29 16:34:31 2013 -0700 91.3 @@ -0,0 +1,52 @@ 91.4 +/* 91.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 91.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 91.7 + * 91.8 + * This code is free software; you can redistribute it and/or modify it 91.9 + * under the terms of the GNU General Public License version 2 only, as 91.10 + * published by the Free Software Foundation. 91.11 + * 91.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 91.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 91.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 91.15 + * version 2 for more details (a copy is included in the LICENSE file that 91.16 + * accompanied this code). 91.17 + * 91.18 + * You should have received a copy of the GNU General Public License version 91.19 + * 2 along with this work; if not, write to the Free Software Foundation, 91.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 91.21 + * 91.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 91.23 + * or visit www.oracle.com if you need additional information or have any 91.24 + * questions. 91.25 + */ 91.26 + 91.27 +/** 91.28 + * JDK-8023368: Instance __proto__ property should exist and be writable. 91.29 + * 91.30 + * @test 91.31 + * @run 91.32 + */ 91.33 + 91.34 +// check that Object.setPrototypeOf works for mirror objects as well. 91.35 + 91.36 +var global = loadWithNewGlobal({ 91.37 + name: "test", 91.38 + script: "var obj = {}; this" 91.39 +}); 91.40 + 91.41 +var proto = global.eval("({ foo: 323 })"); 91.42 + 91.43 +Object.setPrototypeOf(global.obj, proto); 91.44 + 91.45 +function func(obj) { 91.46 + // check proto inherited value 91.47 + print("obj.foo = " + obj.foo); 91.48 +} 91.49 + 91.50 +func(global.obj); 91.51 + 91.52 +var newProto = global.eval("({ foo: 'hello' })"); 91.53 +Object.setPrototypeOf(global.obj, newProto); 91.54 + 91.55 +func(global.obj);
92.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 92.2 +++ b/test/script/basic/mirror_proto_assign.js.EXPECTED Thu Aug 29 16:34:31 2013 -0700 92.3 @@ -0,0 +1,2 @@ 92.4 +obj.foo = 323 92.5 +obj.foo = hello
93.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 93.2 +++ b/test/script/basic/nonextensible_proto_assign.js Thu Aug 29 16:34:31 2013 -0700 93.3 @@ -0,0 +1,44 @@ 93.4 +/* 93.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 93.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 93.7 + * 93.8 + * This code is free software; you can redistribute it and/or modify it 93.9 + * under the terms of the GNU General Public License version 2 only, as 93.10 + * published by the Free Software Foundation. 93.11 + * 93.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 93.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 93.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 93.15 + * version 2 for more details (a copy is included in the LICENSE file that 93.16 + * accompanied this code). 93.17 + * 93.18 + * You should have received a copy of the GNU General Public License version 93.19 + * 2 along with this work; if not, write to the Free Software Foundation, 93.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 93.21 + * 93.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 93.23 + * or visit www.oracle.com if you need additional information or have any 93.24 + * questions. 93.25 + */ 93.26 + 93.27 +/** 93.28 + * JDK-8023368: Instance __proto__ property should exist and be writable. 93.29 + * 93.30 + * @test 93.31 + * @run 93.32 + */ 93.33 + 93.34 +load("nashorn:mozilla_compat.js") 93.35 + 93.36 +// check that we cannot assign to __proto__ of a non-extensible object 93.37 +try { 93.38 + var obj = {} 93.39 + Object.preventExtensions(obj); 93.40 + obj.__proto__ = { }; 93.41 + fail("Should have thrown TypeError"); 93.42 +} catch (e) { 93.43 + if (! (e instanceof TypeError)) { 93.44 + fail("Expected TypeError, got " + e); 93.45 + } 93.46 + print(e); 93.47 +}
94.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 94.2 +++ b/test/script/basic/nonextensible_proto_assign.js.EXPECTED Thu Aug 29 16:34:31 2013 -0700 94.3 @@ -0,0 +1,1 @@ 94.4 +TypeError: Cannot set __proto__ of non-extensible [object Object]
95.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 95.2 +++ b/test/src/jdk/nashorn/api/scripting/InvocableTest.java Thu Aug 29 16:34:31 2013 -0700 95.3 @@ -0,0 +1,525 @@ 95.4 +/* 95.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 95.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 95.7 + * 95.8 + * This code is free software; you can redistribute it and/or modify it 95.9 + * under the terms of the GNU General Public License version 2 only, as 95.10 + * published by the Free Software Foundation. Oracle designates this 95.11 + * particular file as subject to the "Classpath" exception as provided 95.12 + * by Oracle in the LICENSE file that accompanied this code. 95.13 + * 95.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 95.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 95.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 95.17 + * version 2 for more details (a copy is included in the LICENSE file that 95.18 + * accompanied this code). 95.19 + * 95.20 + * You should have received a copy of the GNU General Public License version 95.21 + * 2 along with this work; if not, write to the Free Software Foundation, 95.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 95.23 + * 95.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 95.25 + * or visit www.oracle.com if you need additional information or have any 95.26 + * questions. 95.27 + */ 95.28 + 95.29 +package jdk.nashorn.api.scripting; 95.30 + 95.31 +import java.util.Objects; 95.32 +import javax.script.Invocable; 95.33 +import javax.script.ScriptContext; 95.34 +import javax.script.ScriptEngine; 95.35 +import javax.script.ScriptEngineManager; 95.36 +import javax.script.ScriptException; 95.37 +import javax.script.SimpleScriptContext; 95.38 +import org.testng.Assert; 95.39 +import static org.testng.Assert.assertEquals; 95.40 +import static org.testng.Assert.fail; 95.41 +import org.testng.annotations.Test; 95.42 + 95.43 +/** 95.44 + * Tests for javax.script.Invocable implementation of nashorn. 95.45 + */ 95.46 +public class InvocableTest { 95.47 + 95.48 + private void log(String msg) { 95.49 + org.testng.Reporter.log(msg, true); 95.50 + } 95.51 + 95.52 + @Test 95.53 + public void invokeMethodTest() { 95.54 + final ScriptEngineManager m = new ScriptEngineManager(); 95.55 + final ScriptEngine e = m.getEngineByName("nashorn"); 95.56 + 95.57 + try { 95.58 + e.eval("var Example = function() { this.hello = function() { return 'Hello World!'; };}; myExample = new Example();"); 95.59 + final Object obj = e.get("myExample"); 95.60 + final Object res = ((Invocable) e).invokeMethod(obj, "hello"); 95.61 + assertEquals(res, "Hello World!"); 95.62 + } catch (final Exception exp) { 95.63 + exp.printStackTrace(); 95.64 + fail(exp.getMessage()); 95.65 + } 95.66 + } 95.67 + 95.68 + @Test 95.69 + /** 95.70 + * Check that we can call invokeMethod on an object that we got by 95.71 + * evaluating script with different Context set. 95.72 + */ 95.73 + public void invokeMethodDifferentContextTest() { 95.74 + ScriptEngineManager m = new ScriptEngineManager(); 95.75 + ScriptEngine e = m.getEngineByName("nashorn"); 95.76 + 95.77 + try { 95.78 + // define an object with method on it 95.79 + Object obj = e.eval("({ hello: function() { return 'Hello World!'; } })"); 95.80 + 95.81 + final ScriptContext ctxt = new SimpleScriptContext(); 95.82 + ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); 95.83 + e.setContext(ctxt); 95.84 + 95.85 + // invoke 'func' on obj - but with current script context changed 95.86 + final Object res = ((Invocable) e).invokeMethod(obj, "hello"); 95.87 + assertEquals(res, "Hello World!"); 95.88 + } catch (final Exception exp) { 95.89 + exp.printStackTrace(); 95.90 + fail(exp.getMessage()); 95.91 + } 95.92 + } 95.93 + 95.94 + @Test 95.95 + /** 95.96 + * Check that invokeMethod throws NPE on null method name. 95.97 + */ 95.98 + public void invokeMethodNullNameTest() { 95.99 + final ScriptEngineManager m = new ScriptEngineManager(); 95.100 + final ScriptEngine e = m.getEngineByName("nashorn"); 95.101 + 95.102 + try { 95.103 + final Object obj = e.eval("({})"); 95.104 + final Object res = ((Invocable) e).invokeMethod(obj, null); 95.105 + fail("should have thrown NPE"); 95.106 + } catch (final Exception exp) { 95.107 + if (!(exp instanceof NullPointerException)) { 95.108 + exp.printStackTrace(); 95.109 + fail(exp.getMessage()); 95.110 + } 95.111 + } 95.112 + } 95.113 + 95.114 + @Test 95.115 + /** 95.116 + * Check that invokeMethod throws NoSuchMethodException on missing method. 95.117 + */ 95.118 + public void invokeMethodMissingTest() { 95.119 + final ScriptEngineManager m = new ScriptEngineManager(); 95.120 + final ScriptEngine e = m.getEngineByName("nashorn"); 95.121 + 95.122 + try { 95.123 + final Object obj = e.eval("({})"); 95.124 + final Object res = ((Invocable) e).invokeMethod(obj, "nonExistentMethod"); 95.125 + fail("should have thrown NoSuchMethodException"); 95.126 + } catch (final Exception exp) { 95.127 + if (!(exp instanceof NoSuchMethodException)) { 95.128 + exp.printStackTrace(); 95.129 + fail(exp.getMessage()); 95.130 + } 95.131 + } 95.132 + } 95.133 + 95.134 + @Test 95.135 + /** 95.136 + * Check that calling method on non-script object 'thiz' results in 95.137 + * IllegalArgumentException. 95.138 + */ 95.139 + public void invokeMethodNonScriptObjectThizTest() { 95.140 + final ScriptEngineManager m = new ScriptEngineManager(); 95.141 + final ScriptEngine e = m.getEngineByName("nashorn"); 95.142 + 95.143 + try { 95.144 + ((Invocable) e).invokeMethod(new Object(), "toString"); 95.145 + fail("should have thrown IllegalArgumentException"); 95.146 + } catch (final Exception exp) { 95.147 + if (!(exp instanceof IllegalArgumentException)) { 95.148 + exp.printStackTrace(); 95.149 + fail(exp.getMessage()); 95.150 + } 95.151 + } 95.152 + } 95.153 + 95.154 + @Test 95.155 + /** 95.156 + * Check that calling method on null 'thiz' results in 95.157 + * IllegalArgumentException. 95.158 + */ 95.159 + public void invokeMethodNullThizTest() { 95.160 + final ScriptEngineManager m = new ScriptEngineManager(); 95.161 + final ScriptEngine e = m.getEngineByName("nashorn"); 95.162 + 95.163 + try { 95.164 + ((Invocable) e).invokeMethod(null, "toString"); 95.165 + fail("should have thrown IllegalArgumentException"); 95.166 + } catch (final Exception exp) { 95.167 + if (!(exp instanceof IllegalArgumentException)) { 95.168 + exp.printStackTrace(); 95.169 + fail(exp.getMessage()); 95.170 + } 95.171 + } 95.172 + } 95.173 + 95.174 + @Test 95.175 + /** 95.176 + * Check that calling method on mirror created by another engine results in 95.177 + * IllegalArgumentException. 95.178 + */ 95.179 + public void invokeMethodMixEnginesTest() { 95.180 + final ScriptEngineManager m = new ScriptEngineManager(); 95.181 + final ScriptEngine engine1 = m.getEngineByName("nashorn"); 95.182 + final ScriptEngine engine2 = m.getEngineByName("nashorn"); 95.183 + 95.184 + try { 95.185 + Object obj = engine1.eval("({ run: function() {} })"); 95.186 + // pass object from engine1 to engine2 as 'thiz' for invokeMethod 95.187 + ((Invocable) engine2).invokeMethod(obj, "run"); 95.188 + fail("should have thrown IllegalArgumentException"); 95.189 + } catch (final Exception exp) { 95.190 + if (!(exp instanceof IllegalArgumentException)) { 95.191 + exp.printStackTrace(); 95.192 + fail(exp.getMessage()); 95.193 + } 95.194 + } 95.195 + } 95.196 + 95.197 + @Test 95.198 + public void getInterfaceTest() { 95.199 + final ScriptEngineManager m = new ScriptEngineManager(); 95.200 + final ScriptEngine e = m.getEngineByName("nashorn"); 95.201 + final Invocable inv = (Invocable) e; 95.202 + 95.203 + // try to get interface from global functions 95.204 + try { 95.205 + e.eval("function run() { print('run'); };"); 95.206 + final Runnable runnable = inv.getInterface(Runnable.class); 95.207 + runnable.run(); 95.208 + } catch (final Exception exp) { 95.209 + exp.printStackTrace(); 95.210 + fail(exp.getMessage()); 95.211 + } 95.212 + 95.213 + // try interface on specific script object 95.214 + try { 95.215 + e.eval("var obj = { run: function() { print('run from obj'); } };"); 95.216 + Object obj = e.get("obj"); 95.217 + final Runnable runnable = inv.getInterface(obj, Runnable.class); 95.218 + runnable.run(); 95.219 + } catch (final Exception exp) { 95.220 + exp.printStackTrace(); 95.221 + fail(exp.getMessage()); 95.222 + } 95.223 + } 95.224 + 95.225 + public interface Foo { 95.226 + 95.227 + public void bar(); 95.228 + } 95.229 + 95.230 + public interface Foo2 extends Foo { 95.231 + 95.232 + public void bar2(); 95.233 + } 95.234 + 95.235 + @Test 95.236 + public void getInterfaceMissingTest() { 95.237 + final ScriptEngineManager manager = new ScriptEngineManager(); 95.238 + final ScriptEngine engine = manager.getEngineByName("nashorn"); 95.239 + 95.240 + // don't define any function. 95.241 + try { 95.242 + engine.eval(""); 95.243 + } catch (final Exception exp) { 95.244 + exp.printStackTrace(); 95.245 + fail(exp.getMessage()); 95.246 + } 95.247 + 95.248 + Runnable runnable = ((Invocable) engine).getInterface(Runnable.class); 95.249 + if (runnable != null) { 95.250 + fail("runnable is not null!"); 95.251 + } 95.252 + 95.253 + // now define "run" 95.254 + try { 95.255 + engine.eval("function run() { print('this is run function'); }"); 95.256 + } catch (final Exception exp) { 95.257 + exp.printStackTrace(); 95.258 + fail(exp.getMessage()); 95.259 + } 95.260 + runnable = ((Invocable) engine).getInterface(Runnable.class); 95.261 + // should not return null now! 95.262 + runnable.run(); 95.263 + 95.264 + // define only one method of "Foo2" 95.265 + try { 95.266 + engine.eval("function bar() { print('bar function'); }"); 95.267 + } catch (final Exception exp) { 95.268 + exp.printStackTrace(); 95.269 + fail(exp.getMessage()); 95.270 + } 95.271 + 95.272 + Foo2 foo2 = ((Invocable) engine).getInterface(Foo2.class); 95.273 + if (foo2 != null) { 95.274 + throw new RuntimeException("foo2 is not null!"); 95.275 + } 95.276 + 95.277 + // now define other method of "Foo2" 95.278 + try { 95.279 + engine.eval("function bar2() { print('bar2 function'); }"); 95.280 + } catch (final Exception exp) { 95.281 + exp.printStackTrace(); 95.282 + fail(exp.getMessage()); 95.283 + } 95.284 + foo2 = ((Invocable) engine).getInterface(Foo2.class); 95.285 + foo2.bar(); 95.286 + foo2.bar2(); 95.287 + } 95.288 + 95.289 + @Test 95.290 + /** 95.291 + * Try passing non-interface Class object for interface implementation. 95.292 + */ 95.293 + public void getNonInterfaceGetInterfaceTest() { 95.294 + final ScriptEngineManager manager = new ScriptEngineManager(); 95.295 + final ScriptEngine engine = manager.getEngineByName("nashorn"); 95.296 + try { 95.297 + log(Objects.toString(((Invocable) engine).getInterface(Object.class))); 95.298 + fail("Should have thrown IllegalArgumentException"); 95.299 + } catch (final Exception exp) { 95.300 + if (!(exp instanceof IllegalArgumentException)) { 95.301 + fail("IllegalArgumentException expected, got " + exp); 95.302 + } 95.303 + } 95.304 + } 95.305 + 95.306 + @Test 95.307 + /** 95.308 + * Check that we can get interface out of a script object even after 95.309 + * switching to use different ScriptContext. 95.310 + */ 95.311 + public void getInterfaceDifferentContext() { 95.312 + ScriptEngineManager m = new ScriptEngineManager(); 95.313 + ScriptEngine e = m.getEngineByName("nashorn"); 95.314 + try { 95.315 + Object obj = e.eval("({ run: function() { } })"); 95.316 + 95.317 + // change script context 95.318 + ScriptContext ctxt = new SimpleScriptContext(); 95.319 + ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); 95.320 + e.setContext(ctxt); 95.321 + 95.322 + Runnable r = ((Invocable) e).getInterface(obj, Runnable.class); 95.323 + r.run(); 95.324 + } catch (final Exception exp) { 95.325 + exp.printStackTrace(); 95.326 + fail(exp.getMessage()); 95.327 + } 95.328 + } 95.329 + 95.330 + @Test 95.331 + /** 95.332 + * Check that getInterface on non-script object 'thiz' results in 95.333 + * IllegalArgumentException. 95.334 + */ 95.335 + public void getInterfaceNonScriptObjectThizTest() { 95.336 + final ScriptEngineManager m = new ScriptEngineManager(); 95.337 + final ScriptEngine e = m.getEngineByName("nashorn"); 95.338 + 95.339 + try { 95.340 + ((Invocable) e).getInterface(new Object(), Runnable.class); 95.341 + fail("should have thrown IllegalArgumentException"); 95.342 + } catch (final Exception exp) { 95.343 + if (!(exp instanceof IllegalArgumentException)) { 95.344 + exp.printStackTrace(); 95.345 + fail(exp.getMessage()); 95.346 + } 95.347 + } 95.348 + } 95.349 + 95.350 + @Test 95.351 + /** 95.352 + * Check that getInterface on null 'thiz' results in 95.353 + * IllegalArgumentException. 95.354 + */ 95.355 + public void getInterfaceNullThizTest() { 95.356 + final ScriptEngineManager m = new ScriptEngineManager(); 95.357 + final ScriptEngine e = m.getEngineByName("nashorn"); 95.358 + 95.359 + try { 95.360 + ((Invocable) e).getInterface(null, Runnable.class); 95.361 + fail("should have thrown IllegalArgumentException"); 95.362 + } catch (final Exception exp) { 95.363 + if (!(exp instanceof IllegalArgumentException)) { 95.364 + exp.printStackTrace(); 95.365 + fail(exp.getMessage()); 95.366 + } 95.367 + } 95.368 + } 95.369 + 95.370 + @Test 95.371 + /** 95.372 + * Check that calling getInterface on mirror created by another engine 95.373 + * results in IllegalArgumentException. 95.374 + */ 95.375 + public void getInterfaceMixEnginesTest() { 95.376 + final ScriptEngineManager m = new ScriptEngineManager(); 95.377 + final ScriptEngine engine1 = m.getEngineByName("nashorn"); 95.378 + final ScriptEngine engine2 = m.getEngineByName("nashorn"); 95.379 + 95.380 + try { 95.381 + Object obj = engine1.eval("({ run: function() {} })"); 95.382 + // pass object from engine1 to engine2 as 'thiz' for getInterface 95.383 + ((Invocable) engine2).getInterface(obj, Runnable.class); 95.384 + fail("should have thrown IllegalArgumentException"); 95.385 + } catch (final Exception exp) { 95.386 + if (!(exp instanceof IllegalArgumentException)) { 95.387 + exp.printStackTrace(); 95.388 + fail(exp.getMessage()); 95.389 + } 95.390 + } 95.391 + } 95.392 + 95.393 + @Test 95.394 + /** 95.395 + * check that null function name results in NPE. 95.396 + */ 95.397 + public void invokeFunctionNullNameTest() { 95.398 + final ScriptEngineManager m = new ScriptEngineManager(); 95.399 + final ScriptEngine e = m.getEngineByName("nashorn"); 95.400 + 95.401 + try { 95.402 + final Object res = ((Invocable) e).invokeFunction(null); 95.403 + fail("should have thrown NPE"); 95.404 + } catch (final Exception exp) { 95.405 + if (!(exp instanceof NullPointerException)) { 95.406 + exp.printStackTrace(); 95.407 + fail(exp.getMessage()); 95.408 + } 95.409 + } 95.410 + } 95.411 + 95.412 + @Test 95.413 + /** 95.414 + * Check that attempt to call missing function results in 95.415 + * NoSuchMethodException. 95.416 + */ 95.417 + public void invokeFunctionMissingTest() { 95.418 + final ScriptEngineManager m = new ScriptEngineManager(); 95.419 + final ScriptEngine e = m.getEngineByName("nashorn"); 95.420 + 95.421 + try { 95.422 + final Object res = ((Invocable) e).invokeFunction("NonExistentFunc"); 95.423 + fail("should have thrown NoSuchMethodException"); 95.424 + } catch (final Exception exp) { 95.425 + if (!(exp instanceof NoSuchMethodException)) { 95.426 + exp.printStackTrace(); 95.427 + fail(exp.getMessage()); 95.428 + } 95.429 + } 95.430 + } 95.431 + 95.432 + @Test 95.433 + /** 95.434 + * Check that invokeFunction calls functions only from current context's 95.435 + * Bindings. 95.436 + */ 95.437 + public void invokeFunctionDifferentContextTest() { 95.438 + ScriptEngineManager m = new ScriptEngineManager(); 95.439 + ScriptEngine e = m.getEngineByName("nashorn"); 95.440 + 95.441 + try { 95.442 + // define an object with method on it 95.443 + Object obj = e.eval("function hello() { return 'Hello World!'; }"); 95.444 + final ScriptContext ctxt = new SimpleScriptContext(); 95.445 + ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); 95.446 + // change engine's current context 95.447 + e.setContext(ctxt); 95.448 + 95.449 + ((Invocable) e).invokeFunction("hello"); // no 'hello' in new context! 95.450 + fail("should have thrown NoSuchMethodException"); 95.451 + } catch (final Exception exp) { 95.452 + if (!(exp instanceof NoSuchMethodException)) { 95.453 + exp.printStackTrace(); 95.454 + fail(exp.getMessage()); 95.455 + } 95.456 + } 95.457 + } 95.458 + 95.459 + @Test 95.460 + public void invokeFunctionExceptionTest() { 95.461 + final ScriptEngineManager m = new ScriptEngineManager(); 95.462 + final ScriptEngine e = m.getEngineByName("nashorn"); 95.463 + try { 95.464 + e.eval("function func() { throw new TypeError(); }"); 95.465 + } catch (final Throwable t) { 95.466 + t.printStackTrace(); 95.467 + fail(t.getMessage()); 95.468 + } 95.469 + 95.470 + try { 95.471 + ((Invocable) e).invokeFunction("func"); 95.472 + fail("should have thrown exception"); 95.473 + } catch (final ScriptException se) { 95.474 + // ECMA TypeError property wrapped as a ScriptException 95.475 + log("got " + se + " as expected"); 95.476 + } catch (final Throwable t) { 95.477 + t.printStackTrace(); 95.478 + fail(t.getMessage()); 95.479 + } 95.480 + } 95.481 + 95.482 + @Test 95.483 + public void invokeMethodExceptionTest() { 95.484 + final ScriptEngineManager m = new ScriptEngineManager(); 95.485 + final ScriptEngine e = m.getEngineByName("nashorn"); 95.486 + try { 95.487 + e.eval("var sobj = {}; sobj.foo = function func() { throw new TypeError(); }"); 95.488 + } catch (final Throwable t) { 95.489 + t.printStackTrace(); 95.490 + fail(t.getMessage()); 95.491 + } 95.492 + 95.493 + try { 95.494 + final Object sobj = e.get("sobj"); 95.495 + ((Invocable) e).invokeMethod(sobj, "foo"); 95.496 + fail("should have thrown exception"); 95.497 + } catch (final ScriptException se) { 95.498 + // ECMA TypeError property wrapped as a ScriptException 95.499 + log("got " + se + " as expected"); 95.500 + } catch (final Throwable t) { 95.501 + t.printStackTrace(); 95.502 + fail(t.getMessage()); 95.503 + } 95.504 + } 95.505 + 95.506 + @Test 95.507 + /** 95.508 + * Tests whether invocation of a JavaScript method through a variable arity 95.509 + * Java method will pass the vararg array. Both non-vararg and vararg 95.510 + * JavaScript methods are tested. 95.511 + * 95.512 + * @throws ScriptException 95.513 + */ 95.514 + public void variableArityInterfaceTest() throws ScriptException { 95.515 + final ScriptEngineManager m = new ScriptEngineManager(); 95.516 + final ScriptEngine e = m.getEngineByName("nashorn"); 95.517 + e.eval( 95.518 + "function test1(i, strings) {" 95.519 + + " return 'i == ' + i + ', strings instanceof java.lang.String[] == ' + (strings instanceof Java.type('java.lang.String[]')) + ', strings == ' + java.util.Arrays.toString(strings)" 95.520 + + "}" 95.521 + + "function test2() {" 95.522 + + " return 'arguments[0] == ' + arguments[0] + ', arguments[1] instanceof java.lang.String[] == ' + (arguments[1] instanceof Java.type('java.lang.String[]')) + ', arguments[1] == ' + java.util.Arrays.toString(arguments[1])" 95.523 + + "}"); 95.524 + final VariableArityTestInterface itf = ((Invocable) e).getInterface(VariableArityTestInterface.class); 95.525 + Assert.assertEquals(itf.test1(42, "a", "b"), "i == 42, strings instanceof java.lang.String[] == true, strings == [a, b]"); 95.526 + Assert.assertEquals(itf.test2(44, "c", "d", "e"), "arguments[0] == 44, arguments[1] instanceof java.lang.String[] == true, arguments[1] == [c, d, e]"); 95.527 + } 95.528 +}
96.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 96.2 +++ b/test/src/jdk/nashorn/api/scripting/ScopeTest.java Thu Aug 29 16:34:31 2013 -0700 96.3 @@ -0,0 +1,248 @@ 96.4 +/* 96.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 96.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 96.7 + * 96.8 + * This code is free software; you can redistribute it and/or modify it 96.9 + * under the terms of the GNU General Public License version 2 only, as 96.10 + * published by the Free Software Foundation. Oracle designates this 96.11 + * particular file as subject to the "Classpath" exception as provided 96.12 + * by Oracle in the LICENSE file that accompanied this code. 96.13 + * 96.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 96.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 96.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 96.17 + * version 2 for more details (a copy is included in the LICENSE file that 96.18 + * accompanied this code). 96.19 + * 96.20 + * You should have received a copy of the GNU General Public License version 96.21 + * 2 along with this work; if not, write to the Free Software Foundation, 96.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 96.23 + * 96.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 96.25 + * or visit www.oracle.com if you need additional information or have any 96.26 + * questions. 96.27 + */ 96.28 +package jdk.nashorn.api.scripting; 96.29 + 96.30 +import javax.script.Bindings; 96.31 +import javax.script.ScriptContext; 96.32 +import javax.script.ScriptEngine; 96.33 +import javax.script.ScriptEngineManager; 96.34 +import javax.script.ScriptException; 96.35 +import javax.script.SimpleBindings; 96.36 +import javax.script.SimpleScriptContext; 96.37 +import org.testng.Assert; 96.38 +import static org.testng.Assert.assertEquals; 96.39 +import static org.testng.Assert.assertNotNull; 96.40 +import static org.testng.Assert.assertTrue; 96.41 +import static org.testng.Assert.fail; 96.42 +import org.testng.annotations.Test; 96.43 + 96.44 +/** 96.45 + * Tests for jsr223 Bindings "scope" (engine, global scopes) 96.46 + */ 96.47 +public class ScopeTest { 96.48 + 96.49 + @Test 96.50 + public void createBindingsTest() { 96.51 + final ScriptEngineManager m = new ScriptEngineManager(); 96.52 + final ScriptEngine e = m.getEngineByName("nashorn"); 96.53 + Bindings b = e.createBindings(); 96.54 + b.put("foo", 42.0); 96.55 + Object res = null; 96.56 + try { 96.57 + res = e.eval("foo == 42.0", b); 96.58 + } catch (final ScriptException | NullPointerException se) { 96.59 + se.printStackTrace(); 96.60 + fail(se.getMessage()); 96.61 + } 96.62 + 96.63 + assertEquals(res, Boolean.TRUE); 96.64 + } 96.65 + 96.66 + @Test 96.67 + public void engineScopeTest() { 96.68 + final ScriptEngineManager m = new ScriptEngineManager(); 96.69 + final ScriptEngine e = m.getEngineByName("nashorn"); 96.70 + Bindings engineScope = e.getBindings(ScriptContext.ENGINE_SCOPE); 96.71 + 96.72 + // check few ECMA standard built-in global properties 96.73 + assertNotNull(engineScope.get("Object")); 96.74 + assertNotNull(engineScope.get("TypeError")); 96.75 + assertNotNull(engineScope.get("eval")); 96.76 + 96.77 + // can access via ScriptEngine.get as well 96.78 + assertNotNull(e.get("Object")); 96.79 + assertNotNull(e.get("TypeError")); 96.80 + assertNotNull(e.get("eval")); 96.81 + 96.82 + // Access by either way should return same object 96.83 + assertEquals(engineScope.get("Array"), e.get("Array")); 96.84 + assertEquals(engineScope.get("EvalError"), e.get("EvalError")); 96.85 + assertEquals(engineScope.get("undefined"), e.get("undefined")); 96.86 + 96.87 + // try exposing a new variable from scope 96.88 + engineScope.put("myVar", "foo"); 96.89 + try { 96.90 + assertEquals(e.eval("myVar"), "foo"); 96.91 + } catch (final ScriptException se) { 96.92 + se.printStackTrace(); 96.93 + fail(se.getMessage()); 96.94 + } 96.95 + 96.96 + // update "myVar" in script an check the value from scope 96.97 + try { 96.98 + e.eval("myVar = 'nashorn';"); 96.99 + } catch (final ScriptException se) { 96.100 + se.printStackTrace(); 96.101 + fail(se.getMessage()); 96.102 + } 96.103 + 96.104 + // now check modified value from scope and engine 96.105 + assertEquals(engineScope.get("myVar"), "nashorn"); 96.106 + assertEquals(e.get("myVar"), "nashorn"); 96.107 + } 96.108 + 96.109 + @Test 96.110 + public void multiGlobalTest() { 96.111 + final ScriptEngineManager m = new ScriptEngineManager(); 96.112 + final ScriptEngine e = m.getEngineByName("nashorn"); 96.113 + final Bindings b = e.createBindings(); 96.114 + final ScriptContext newCtxt = new SimpleScriptContext(); 96.115 + newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 96.116 + 96.117 + try { 96.118 + Object obj1 = e.eval("Object"); 96.119 + Object obj2 = e.eval("Object", newCtxt); 96.120 + Assert.assertNotEquals(obj1, obj2); 96.121 + Assert.assertNotNull(obj1); 96.122 + Assert.assertNotNull(obj2); 96.123 + Assert.assertEquals(obj1.toString(), obj2.toString()); 96.124 + 96.125 + e.eval("x = 'hello'"); 96.126 + e.eval("x = 'world'", newCtxt); 96.127 + Object x1 = e.getContext().getAttribute("x"); 96.128 + Object x2 = newCtxt.getAttribute("x"); 96.129 + Assert.assertNotEquals(x1, x2); 96.130 + Assert.assertEquals(x1, "hello"); 96.131 + Assert.assertEquals(x2, "world"); 96.132 + 96.133 + x1 = e.eval("x"); 96.134 + x2 = e.eval("x", newCtxt); 96.135 + Assert.assertNotEquals(x1, x2); 96.136 + Assert.assertEquals(x1, "hello"); 96.137 + Assert.assertEquals(x2, "world"); 96.138 + 96.139 + final ScriptContext origCtxt = e.getContext(); 96.140 + e.setContext(newCtxt); 96.141 + e.eval("y = new Object()"); 96.142 + e.eval("y = new Object()", origCtxt); 96.143 + 96.144 + Object y1 = origCtxt.getAttribute("y"); 96.145 + Object y2 = newCtxt.getAttribute("y"); 96.146 + Assert.assertNotEquals(y1, y2); 96.147 + Assert.assertNotEquals(e.eval("y"), e.eval("y", origCtxt)); 96.148 + Assert.assertEquals("[object Object]", y1.toString()); 96.149 + Assert.assertEquals("[object Object]", y2.toString()); 96.150 + } catch (final ScriptException se) { 96.151 + se.printStackTrace(); 96.152 + fail(se.getMessage()); 96.153 + } 96.154 + } 96.155 + 96.156 + @Test 96.157 + public void userEngineScopeBindingsTest() throws ScriptException { 96.158 + final ScriptEngineManager m = new ScriptEngineManager(); 96.159 + final ScriptEngine e = m.getEngineByName("nashorn"); 96.160 + e.eval("function func() {}"); 96.161 + 96.162 + final ScriptContext newContext = new SimpleScriptContext(); 96.163 + newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); 96.164 + // we are using a new bindings - so it should have 'func' defined 96.165 + Object value = e.eval("typeof func", newContext); 96.166 + assertTrue(value.equals("undefined")); 96.167 + } 96.168 + 96.169 + @Test 96.170 + public void userEngineScopeBindingsNoLeakTest() throws ScriptException { 96.171 + final ScriptEngineManager m = new ScriptEngineManager(); 96.172 + final ScriptEngine e = m.getEngineByName("nashorn"); 96.173 + final ScriptContext newContext = new SimpleScriptContext(); 96.174 + newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); 96.175 + e.eval("function foo() {}", newContext); 96.176 + 96.177 + // in the default context's ENGINE_SCOPE, 'foo' shouldn't exist 96.178 + assertTrue(e.eval("typeof foo").equals("undefined")); 96.179 + } 96.180 + 96.181 + @Test 96.182 + public void userEngineScopeBindingsRetentionTest() throws ScriptException { 96.183 + final ScriptEngineManager m = new ScriptEngineManager(); 96.184 + final ScriptEngine e = m.getEngineByName("nashorn"); 96.185 + final ScriptContext newContext = new SimpleScriptContext(); 96.186 + newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); 96.187 + e.eval("function foo() {}", newContext); 96.188 + 96.189 + // definition retained with user's ENGINE_SCOPE Binding 96.190 + assertTrue(e.eval("typeof foo", newContext).equals("function")); 96.191 + 96.192 + final Bindings oldBindings = newContext.getBindings(ScriptContext.ENGINE_SCOPE); 96.193 + // but not in another ENGINE_SCOPE binding 96.194 + newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); 96.195 + assertTrue(e.eval("typeof foo", newContext).equals("undefined")); 96.196 + 96.197 + // restore ENGINE_SCOPE and check again 96.198 + newContext.setBindings(oldBindings, ScriptContext.ENGINE_SCOPE); 96.199 + assertTrue(e.eval("typeof foo", newContext).equals("function")); 96.200 + } 96.201 + 96.202 + @Test 96.203 + // check that engine.js definitions are visible in all new global instances 96.204 + public void checkBuiltinsInNewBindingsTest() throws ScriptException { 96.205 + final ScriptEngineManager m = new ScriptEngineManager(); 96.206 + final ScriptEngine e = m.getEngineByName("nashorn"); 96.207 + 96.208 + // check default global instance has engine.js definitions 96.209 + final Bindings g = (Bindings) e.eval("this"); 96.210 + Object value = g.get("__noSuchProperty__"); 96.211 + assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); 96.212 + value = g.get("print"); 96.213 + assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); 96.214 + 96.215 + // check new global instance created has engine.js definitions 96.216 + Bindings b = e.createBindings(); 96.217 + value = b.get("__noSuchProperty__"); 96.218 + assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); 96.219 + value = b.get("print"); 96.220 + assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); 96.221 + 96.222 + // put a mapping into GLOBAL_SCOPE 96.223 + final Bindings globalScope = e.getContext().getBindings(ScriptContext.GLOBAL_SCOPE); 96.224 + globalScope.put("x", "hello"); 96.225 + 96.226 + // GLOBAL_SCOPE mapping should be visible from default ScriptContext eval 96.227 + assertTrue(e.eval("x").equals("hello")); 96.228 + 96.229 + final ScriptContext ctx = new SimpleScriptContext(); 96.230 + ctx.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE); 96.231 + ctx.setBindings(b, ScriptContext.ENGINE_SCOPE); 96.232 + 96.233 + // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval 96.234 + assertTrue(e.eval("x", ctx).equals("hello")); 96.235 + 96.236 + // try some arbitray Bindings for ENGINE_SCOPE 96.237 + Bindings sb = new SimpleBindings(); 96.238 + ctx.setBindings(sb, ScriptContext.ENGINE_SCOPE); 96.239 + 96.240 + // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval 96.241 + assertTrue(e.eval("x", ctx).equals("hello")); 96.242 + 96.243 + // engine.js builtins are still defined even with arbitrary Bindings 96.244 + assertTrue(e.eval("typeof print", ctx).equals("function")); 96.245 + assertTrue(e.eval("typeof __noSuchProperty__", ctx).equals("function")); 96.246 + 96.247 + // ENGINE_SCOPE definition should 'hide' GLOBAL_SCOPE definition 96.248 + sb.put("x", "newX"); 96.249 + assertTrue(e.eval("x", ctx).equals("newX")); 96.250 + } 96.251 +}
97.1 --- a/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java Thu Aug 29 09:42:13 2013 -0700 97.2 +++ b/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java Thu Aug 29 16:34:31 2013 -0700 97.3 @@ -26,29 +26,21 @@ 97.4 package jdk.nashorn.api.scripting; 97.5 97.6 import static org.testng.Assert.assertEquals; 97.7 -import static org.testng.Assert.assertFalse; 97.8 import static org.testng.Assert.assertNotNull; 97.9 import static org.testng.Assert.assertTrue; 97.10 import static org.testng.Assert.fail; 97.11 97.12 +import java.io.PrintWriter; 97.13 import java.io.StringReader; 97.14 import java.io.StringWriter; 97.15 import java.lang.reflect.Method; 97.16 -import java.util.HashMap; 97.17 -import java.util.Map; 97.18 -import java.util.Objects; 97.19 import java.util.concurrent.Callable; 97.20 -import javax.script.Bindings; 97.21 import javax.script.Compilable; 97.22 import javax.script.CompiledScript; 97.23 -import javax.script.Invocable; 97.24 -import javax.script.ScriptContext; 97.25 import javax.script.ScriptEngine; 97.26 import javax.script.ScriptEngineFactory; 97.27 import javax.script.ScriptEngineManager; 97.28 import javax.script.ScriptException; 97.29 -import javax.script.SimpleScriptContext; 97.30 -import org.testng.Assert; 97.31 import org.testng.annotations.Test; 97.32 97.33 /** 97.34 @@ -239,214 +231,6 @@ 97.35 } 97.36 97.37 @Test 97.38 - public void createBindingsTest() { 97.39 - final ScriptEngineManager m = new ScriptEngineManager(); 97.40 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.41 - Bindings b = e.createBindings(); 97.42 - b.put("foo", 42.0); 97.43 - Object res = null; 97.44 - try { 97.45 - res = e.eval("foo == 42.0", b); 97.46 - } catch (final ScriptException | NullPointerException se) { 97.47 - se.printStackTrace(); 97.48 - fail(se.getMessage()); 97.49 - } 97.50 - 97.51 - assertEquals(res, Boolean.TRUE); 97.52 - } 97.53 - 97.54 - @Test 97.55 - public void getInterfaceTest() { 97.56 - final ScriptEngineManager m = new ScriptEngineManager(); 97.57 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.58 - final Invocable inv = (Invocable)e; 97.59 - 97.60 - // try to get interface from global functions 97.61 - try { 97.62 - e.eval("function run() { print('run'); };"); 97.63 - final Runnable runnable = inv.getInterface(Runnable.class); 97.64 - runnable.run(); 97.65 - } catch (final Exception exp) { 97.66 - exp.printStackTrace(); 97.67 - fail(exp.getMessage()); 97.68 - } 97.69 - 97.70 - // try interface on specific script object 97.71 - try { 97.72 - e.eval("var obj = { run: function() { print('run from obj'); } };"); 97.73 - Object obj = e.get("obj"); 97.74 - final Runnable runnable = inv.getInterface(obj, Runnable.class); 97.75 - runnable.run(); 97.76 - } catch (final Exception exp) { 97.77 - exp.printStackTrace(); 97.78 - fail(exp.getMessage()); 97.79 - } 97.80 - } 97.81 - 97.82 - public interface Foo { 97.83 - public void bar(); 97.84 - } 97.85 - 97.86 - public interface Foo2 extends Foo { 97.87 - public void bar2(); 97.88 - } 97.89 - 97.90 - @Test 97.91 - public void getInterfaceMissingTest() { 97.92 - final ScriptEngineManager manager = new ScriptEngineManager(); 97.93 - final ScriptEngine engine = manager.getEngineByName("nashorn"); 97.94 - 97.95 - // don't define any function. 97.96 - try { 97.97 - engine.eval(""); 97.98 - } catch (final Exception exp) { 97.99 - exp.printStackTrace(); 97.100 - fail(exp.getMessage()); 97.101 - } 97.102 - 97.103 - Runnable runnable = ((Invocable)engine).getInterface(Runnable.class); 97.104 - if (runnable != null) { 97.105 - fail("runnable is not null!"); 97.106 - } 97.107 - 97.108 - // now define "run" 97.109 - try { 97.110 - engine.eval("function run() { print('this is run function'); }"); 97.111 - } catch (final Exception exp) { 97.112 - exp.printStackTrace(); 97.113 - fail(exp.getMessage()); 97.114 - } 97.115 - runnable = ((Invocable)engine).getInterface(Runnable.class); 97.116 - // should not return null now! 97.117 - runnable.run(); 97.118 - 97.119 - // define only one method of "Foo2" 97.120 - try { 97.121 - engine.eval("function bar() { print('bar function'); }"); 97.122 - } catch (final Exception exp) { 97.123 - exp.printStackTrace(); 97.124 - fail(exp.getMessage()); 97.125 - } 97.126 - 97.127 - Foo2 foo2 = ((Invocable)engine).getInterface(Foo2.class); 97.128 - if (foo2 != null) { 97.129 - throw new RuntimeException("foo2 is not null!"); 97.130 - } 97.131 - 97.132 - // now define other method of "Foo2" 97.133 - try { 97.134 - engine.eval("function bar2() { print('bar2 function'); }"); 97.135 - } catch (final Exception exp) { 97.136 - exp.printStackTrace(); 97.137 - fail(exp.getMessage()); 97.138 - } 97.139 - foo2 = ((Invocable)engine).getInterface(Foo2.class); 97.140 - foo2.bar(); 97.141 - foo2.bar2(); 97.142 - } 97.143 - 97.144 - @Test 97.145 - /** 97.146 - * Try passing non-interface Class object for interface implementation. 97.147 - */ 97.148 - public void getNonInterfaceGetInterfaceTest() { 97.149 - final ScriptEngineManager manager = new ScriptEngineManager(); 97.150 - final ScriptEngine engine = manager.getEngineByName("nashorn"); 97.151 - try { 97.152 - log(Objects.toString(((Invocable)engine).getInterface(Object.class))); 97.153 - fail("Should have thrown IllegalArgumentException"); 97.154 - } catch (final Exception exp) { 97.155 - if (! (exp instanceof IllegalArgumentException)) { 97.156 - fail("IllegalArgumentException expected, got " + exp); 97.157 - } 97.158 - } 97.159 - } 97.160 - 97.161 - @Test 97.162 - /** 97.163 - * Check that we can get interface out of a script object even after 97.164 - * switching to use different ScriptContext. 97.165 - */ 97.166 - public void getInterfaceDifferentContext() { 97.167 - ScriptEngineManager m = new ScriptEngineManager(); 97.168 - ScriptEngine e = m.getEngineByName("nashorn"); 97.169 - try { 97.170 - Object obj = e.eval("({ run: function() { } })"); 97.171 - 97.172 - // change script context 97.173 - ScriptContext ctxt = new SimpleScriptContext(); 97.174 - ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); 97.175 - e.setContext(ctxt); 97.176 - 97.177 - Runnable r = ((Invocable)e).getInterface(obj, Runnable.class); 97.178 - r.run(); 97.179 - }catch (final Exception exp) { 97.180 - exp.printStackTrace(); 97.181 - fail(exp.getMessage()); 97.182 - } 97.183 - } 97.184 - 97.185 - @Test 97.186 - /** 97.187 - * Check that getInterface on non-script object 'thiz' results in IllegalArgumentException. 97.188 - */ 97.189 - public void getInterfaceNonScriptObjectThizTest() { 97.190 - final ScriptEngineManager m = new ScriptEngineManager(); 97.191 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.192 - 97.193 - try { 97.194 - ((Invocable)e).getInterface(new Object(), Runnable.class); 97.195 - fail("should have thrown IllegalArgumentException"); 97.196 - } catch (final Exception exp) { 97.197 - if (! (exp instanceof IllegalArgumentException)) { 97.198 - exp.printStackTrace(); 97.199 - fail(exp.getMessage()); 97.200 - } 97.201 - } 97.202 - } 97.203 - 97.204 - @Test 97.205 - /** 97.206 - * Check that getInterface on null 'thiz' results in IllegalArgumentException. 97.207 - */ 97.208 - public void getInterfaceNullThizTest() { 97.209 - final ScriptEngineManager m = new ScriptEngineManager(); 97.210 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.211 - 97.212 - try { 97.213 - ((Invocable)e).getInterface(null, Runnable.class); 97.214 - fail("should have thrown IllegalArgumentException"); 97.215 - } catch (final Exception exp) { 97.216 - if (! (exp instanceof IllegalArgumentException)) { 97.217 - exp.printStackTrace(); 97.218 - fail(exp.getMessage()); 97.219 - } 97.220 - } 97.221 - } 97.222 - 97.223 - @Test 97.224 - /** 97.225 - * Check that calling getInterface on mirror created by another engine results in IllegalArgumentException. 97.226 - */ 97.227 - public void getInterfaceMixEnginesTest() { 97.228 - final ScriptEngineManager m = new ScriptEngineManager(); 97.229 - final ScriptEngine engine1 = m.getEngineByName("nashorn"); 97.230 - final ScriptEngine engine2 = m.getEngineByName("nashorn"); 97.231 - 97.232 - try { 97.233 - Object obj = engine1.eval("({ run: function() {} })"); 97.234 - // pass object from engine1 to engine2 as 'thiz' for getInterface 97.235 - ((Invocable)engine2).getInterface(obj, Runnable.class); 97.236 - fail("should have thrown IllegalArgumentException"); 97.237 - } catch (final Exception exp) { 97.238 - if (! (exp instanceof IllegalArgumentException)) { 97.239 - exp.printStackTrace(); 97.240 - fail(exp.getMessage()); 97.241 - } 97.242 - } 97.243 - } 97.244 - 97.245 - @Test 97.246 public void accessGlobalTest() { 97.247 final ScriptEngineManager m = new ScriptEngineManager(); 97.248 final ScriptEngine e = m.getEngineByName("nashorn"); 97.249 @@ -617,90 +401,7 @@ 97.250 exp.printStackTrace(); 97.251 fail(exp.getMessage()); 97.252 } 97.253 - // dos2unix - fix line endings if running on windows 97.254 - assertEquals(sw.toString().replaceAll("\r", ""), "hello world\n"); 97.255 - } 97.256 - 97.257 - @SuppressWarnings("unchecked") 97.258 - @Test 97.259 - public void reflectionTest() throws ScriptException { 97.260 - final ScriptEngineManager m = new ScriptEngineManager(); 97.261 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.262 - 97.263 - e.eval("var obj = { x: 344, y: 'nashorn' }"); 97.264 - 97.265 - int count = 0; 97.266 - Map<Object, Object> map = (Map<Object, Object>)e.get("obj"); 97.267 - assertFalse(map.isEmpty()); 97.268 - assertTrue(map.keySet().contains("x")); 97.269 - assertTrue(map.containsKey("x")); 97.270 - assertTrue(map.values().contains("nashorn")); 97.271 - assertTrue(map.containsValue("nashorn")); 97.272 - for (final Map.Entry<?, ?> ex : map.entrySet()) { 97.273 - final Object key = ex.getKey(); 97.274 - if (key.equals("x")) { 97.275 - assertTrue(344 == ((Number)ex.getValue()).doubleValue()); 97.276 - count++; 97.277 - } else if (key.equals("y")) { 97.278 - assertEquals(ex.getValue(), "nashorn"); 97.279 - count++; 97.280 - } 97.281 - } 97.282 - assertEquals(2, count); 97.283 - assertEquals(2, map.size()); 97.284 - 97.285 - // add property 97.286 - map.put("z", "hello"); 97.287 - assertEquals(e.eval("obj.z"), "hello"); 97.288 - assertEquals(map.get("z"), "hello"); 97.289 - assertTrue(map.keySet().contains("z")); 97.290 - assertTrue(map.containsKey("z")); 97.291 - assertTrue(map.values().contains("hello")); 97.292 - assertTrue(map.containsValue("hello")); 97.293 - assertEquals(map.size(), 3); 97.294 - 97.295 - final Map<Object, Object> newMap = new HashMap<>(); 97.296 - newMap.put("foo", 23.0); 97.297 - newMap.put("bar", true); 97.298 - map.putAll(newMap); 97.299 - 97.300 - assertEquals(e.eval("obj.foo"), 23.0); 97.301 - assertEquals(e.eval("obj.bar"), true); 97.302 - 97.303 - // remove using map method 97.304 - map.remove("foo"); 97.305 - assertEquals(e.eval("typeof obj.foo"), "undefined"); 97.306 - 97.307 - count = 0; 97.308 - e.eval("var arr = [ true, 'hello' ]"); 97.309 - map = (Map<Object, Object>)e.get("arr"); 97.310 - assertFalse(map.isEmpty()); 97.311 - assertTrue(map.containsKey("length")); 97.312 - assertTrue(map.containsValue("hello")); 97.313 - for (final Map.Entry<?, ?> ex : map.entrySet()) { 97.314 - final Object key = ex.getKey(); 97.315 - if (key.equals("0")) { 97.316 - assertEquals(ex.getValue(), Boolean.TRUE); 97.317 - count++; 97.318 - } else if (key.equals("1")) { 97.319 - assertEquals(ex.getValue(), "hello"); 97.320 - count++; 97.321 - } 97.322 - } 97.323 - assertEquals(count, 2); 97.324 - assertEquals(map.size(), 2); 97.325 - 97.326 - // add element 97.327 - map.put("2", "world"); 97.328 - assertEquals(map.get("2"), "world"); 97.329 - assertEquals(map.size(), 3); 97.330 - 97.331 - // remove all 97.332 - map.clear(); 97.333 - assertTrue(map.isEmpty()); 97.334 - assertEquals(e.eval("typeof arr[0]"), "undefined"); 97.335 - assertEquals(e.eval("typeof arr[1]"), "undefined"); 97.336 - assertEquals(e.eval("typeof arr[2]"), "undefined"); 97.337 + assertEquals(sw.toString(), println("hello world")); 97.338 } 97.339 97.340 @Test 97.341 @@ -715,150 +416,6 @@ 97.342 fail(exp.getMessage()); 97.343 } 97.344 } 97.345 - 97.346 - @Test 97.347 - public void invokeMethodTest() { 97.348 - final ScriptEngineManager m = new ScriptEngineManager(); 97.349 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.350 - 97.351 - try { 97.352 - e.eval("var Example = function() { this.hello = function() { return 'Hello World!'; };}; myExample = new Example();"); 97.353 - final Object obj = e.get("myExample"); 97.354 - final Object res = ((Invocable)e).invokeMethod(obj, "hello"); 97.355 - assertEquals(res, "Hello World!"); 97.356 - } catch (final Exception exp) { 97.357 - exp.printStackTrace(); 97.358 - fail(exp.getMessage()); 97.359 - } 97.360 - } 97.361 - 97.362 - @Test 97.363 - /** 97.364 - * Check that we can call invokeMethod on an object that we got by evaluating 97.365 - * script with different Context set. 97.366 - */ 97.367 - public void invokeMethodDifferentContextTest() { 97.368 - ScriptEngineManager m = new ScriptEngineManager(); 97.369 - ScriptEngine e = m.getEngineByName("nashorn"); 97.370 - 97.371 - try { 97.372 - // define an object with method on it 97.373 - Object obj = e.eval("({ hello: function() { return 'Hello World!'; } })"); 97.374 - 97.375 - final ScriptContext ctxt = new SimpleScriptContext(); 97.376 - ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); 97.377 - e.setContext(ctxt); 97.378 - 97.379 - // invoke 'func' on obj - but with current script context changed 97.380 - final Object res = ((Invocable)e).invokeMethod(obj, "hello"); 97.381 - assertEquals(res, "Hello World!"); 97.382 - } catch (final Exception exp) { 97.383 - exp.printStackTrace(); 97.384 - fail(exp.getMessage()); 97.385 - } 97.386 - } 97.387 - 97.388 - @Test 97.389 - /** 97.390 - * Check that invokeMethod throws NPE on null method name. 97.391 - */ 97.392 - public void invokeMethodNullNameTest() { 97.393 - final ScriptEngineManager m = new ScriptEngineManager(); 97.394 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.395 - 97.396 - try { 97.397 - final Object obj = e.eval("({})"); 97.398 - final Object res = ((Invocable)e).invokeMethod(obj, null); 97.399 - fail("should have thrown NPE"); 97.400 - } catch (final Exception exp) { 97.401 - if (! (exp instanceof NullPointerException)) { 97.402 - exp.printStackTrace(); 97.403 - fail(exp.getMessage()); 97.404 - } 97.405 - } 97.406 - } 97.407 - 97.408 - @Test 97.409 - /** 97.410 - * Check that invokeMethod throws NoSuchMethodException on missing method. 97.411 - */ 97.412 - public void invokeMethodMissingTest() { 97.413 - final ScriptEngineManager m = new ScriptEngineManager(); 97.414 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.415 - 97.416 - try { 97.417 - final Object obj = e.eval("({})"); 97.418 - final Object res = ((Invocable)e).invokeMethod(obj, "nonExistentMethod"); 97.419 - fail("should have thrown NoSuchMethodException"); 97.420 - } catch (final Exception exp) { 97.421 - if (! (exp instanceof NoSuchMethodException)) { 97.422 - exp.printStackTrace(); 97.423 - fail(exp.getMessage()); 97.424 - } 97.425 - } 97.426 - } 97.427 - 97.428 - @Test 97.429 - /** 97.430 - * Check that calling method on non-script object 'thiz' results in IllegalArgumentException. 97.431 - */ 97.432 - public void invokeMethodNonScriptObjectThizTest() { 97.433 - final ScriptEngineManager m = new ScriptEngineManager(); 97.434 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.435 - 97.436 - try { 97.437 - ((Invocable)e).invokeMethod(new Object(), "toString"); 97.438 - fail("should have thrown IllegalArgumentException"); 97.439 - } catch (final Exception exp) { 97.440 - if (! (exp instanceof IllegalArgumentException)) { 97.441 - exp.printStackTrace(); 97.442 - fail(exp.getMessage()); 97.443 - } 97.444 - } 97.445 - } 97.446 - 97.447 - @Test 97.448 - /** 97.449 - * Check that calling method on null 'thiz' results in IllegalArgumentException. 97.450 - */ 97.451 - public void invokeMethodNullThizTest() { 97.452 - final ScriptEngineManager m = new ScriptEngineManager(); 97.453 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.454 - 97.455 - try { 97.456 - ((Invocable)e).invokeMethod(null, "toString"); 97.457 - fail("should have thrown IllegalArgumentException"); 97.458 - } catch (final Exception exp) { 97.459 - if (! (exp instanceof IllegalArgumentException)) { 97.460 - exp.printStackTrace(); 97.461 - fail(exp.getMessage()); 97.462 - } 97.463 - } 97.464 - } 97.465 - 97.466 - 97.467 - @Test 97.468 - /** 97.469 - * Check that calling method on mirror created by another engine results in IllegalArgumentException. 97.470 - */ 97.471 - public void invokeMethodMixEnginesTest() { 97.472 - final ScriptEngineManager m = new ScriptEngineManager(); 97.473 - final ScriptEngine engine1 = m.getEngineByName("nashorn"); 97.474 - final ScriptEngine engine2 = m.getEngineByName("nashorn"); 97.475 - 97.476 - try { 97.477 - Object obj = engine1.eval("({ run: function() {} })"); 97.478 - // pass object from engine1 to engine2 as 'thiz' for invokeMethod 97.479 - ((Invocable)engine2).invokeMethod(obj, "run"); 97.480 - fail("should have thrown IllegalArgumentException"); 97.481 - } catch (final Exception exp) { 97.482 - if (! (exp instanceof IllegalArgumentException)) { 97.483 - exp.printStackTrace(); 97.484 - fail(exp.getMessage()); 97.485 - } 97.486 - } 97.487 - } 97.488 - 97.489 @Test 97.490 public void noEnumerablePropertiesTest() { 97.491 final ScriptEngineManager m = new ScriptEngineManager(); 97.492 @@ -920,308 +477,6 @@ 97.493 } 97.494 97.495 @Test 97.496 - public void jsobjectTest() { 97.497 - final ScriptEngineManager m = new ScriptEngineManager(); 97.498 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.499 - try { 97.500 - e.eval("var obj = { '1': 'world', func: function() { return this.bar; }, bar: 'hello' }"); 97.501 - JSObject obj = (JSObject) e.get("obj"); 97.502 - 97.503 - // try basic get on existing properties 97.504 - if (! obj.getMember("bar").equals("hello")) { 97.505 - fail("obj.bar != 'hello'"); 97.506 - } 97.507 - 97.508 - if (! obj.getSlot(1).equals("world")) { 97.509 - fail("obj[1] != 'world'"); 97.510 - } 97.511 - 97.512 - if (! obj.call("func", new Object[0]).equals("hello")) { 97.513 - fail("obj.call('func') != 'hello'"); 97.514 - } 97.515 - 97.516 - // try setting properties 97.517 - obj.setMember("bar", "new-bar"); 97.518 - obj.setSlot(1, "new-element-1"); 97.519 - if (! obj.getMember("bar").equals("new-bar")) { 97.520 - fail("obj.bar != 'new-bar'"); 97.521 - } 97.522 - 97.523 - if (! obj.getSlot(1).equals("new-element-1")) { 97.524 - fail("obj[1] != 'new-element-1'"); 97.525 - } 97.526 - 97.527 - // try adding properties 97.528 - obj.setMember("prop", "prop-value"); 97.529 - obj.setSlot(12, "element-12"); 97.530 - if (! obj.getMember("prop").equals("prop-value")) { 97.531 - fail("obj.prop != 'prop-value'"); 97.532 - } 97.533 - 97.534 - if (! obj.getSlot(12).equals("element-12")) { 97.535 - fail("obj[12] != 'element-12'"); 97.536 - } 97.537 - 97.538 - // delete properties 97.539 - obj.removeMember("prop"); 97.540 - if ("prop-value".equals(obj.getMember("prop"))) { 97.541 - fail("obj.prop is not deleted!"); 97.542 - } 97.543 - 97.544 - // Simple eval tests 97.545 - assertEquals(obj.eval("typeof Object"), "function"); 97.546 - assertEquals(obj.eval("'nashorn'.substring(3)"), "horn"); 97.547 - } catch (final Exception exp) { 97.548 - exp.printStackTrace(); 97.549 - fail(exp.getMessage()); 97.550 - } 97.551 - } 97.552 - 97.553 - @Test 97.554 - /** 97.555 - * check that null function name results in NPE. 97.556 - */ 97.557 - public void invokeFunctionNullNameTest() { 97.558 - final ScriptEngineManager m = new ScriptEngineManager(); 97.559 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.560 - 97.561 - try { 97.562 - final Object res = ((Invocable)e).invokeFunction(null); 97.563 - fail("should have thrown NPE"); 97.564 - } catch (final Exception exp) { 97.565 - if (! (exp instanceof NullPointerException)) { 97.566 - exp.printStackTrace(); 97.567 - fail(exp.getMessage()); 97.568 - } 97.569 - } 97.570 - } 97.571 - 97.572 - @Test 97.573 - /** 97.574 - * Check that attempt to call missing function results in NoSuchMethodException. 97.575 - */ 97.576 - public void invokeFunctionMissingTest() { 97.577 - final ScriptEngineManager m = new ScriptEngineManager(); 97.578 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.579 - 97.580 - try { 97.581 - final Object res = ((Invocable)e).invokeFunction("NonExistentFunc"); 97.582 - fail("should have thrown NoSuchMethodException"); 97.583 - } catch (final Exception exp) { 97.584 - if (! (exp instanceof NoSuchMethodException)) { 97.585 - exp.printStackTrace(); 97.586 - fail(exp.getMessage()); 97.587 - } 97.588 - } 97.589 - } 97.590 - 97.591 - @Test 97.592 - /** 97.593 - * Check that invokeFunction calls functions only from current context's Bindings. 97.594 - */ 97.595 - public void invokeFunctionDifferentContextTest() { 97.596 - ScriptEngineManager m = new ScriptEngineManager(); 97.597 - ScriptEngine e = m.getEngineByName("nashorn"); 97.598 - 97.599 - try { 97.600 - // define an object with method on it 97.601 - Object obj = e.eval("function hello() { return 'Hello World!'; }"); 97.602 - final ScriptContext ctxt = new SimpleScriptContext(); 97.603 - ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); 97.604 - // change engine's current context 97.605 - e.setContext(ctxt); 97.606 - 97.607 - ((Invocable)e).invokeFunction("hello"); // no 'hello' in new context! 97.608 - fail("should have thrown NoSuchMethodException"); 97.609 - } catch (final Exception exp) { 97.610 - if (! (exp instanceof NoSuchMethodException)) { 97.611 - exp.printStackTrace(); 97.612 - fail(exp.getMessage()); 97.613 - } 97.614 - } 97.615 - } 97.616 - 97.617 - @Test 97.618 - public void invokeFunctionExceptionTest() { 97.619 - final ScriptEngineManager m = new ScriptEngineManager(); 97.620 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.621 - try { 97.622 - e.eval("function func() { throw new TypeError(); }"); 97.623 - } catch (final Throwable t) { 97.624 - t.printStackTrace(); 97.625 - fail(t.getMessage()); 97.626 - } 97.627 - 97.628 - try { 97.629 - ((Invocable)e).invokeFunction("func"); 97.630 - fail("should have thrown exception"); 97.631 - } catch (final ScriptException se) { 97.632 - // ECMA TypeError property wrapped as a ScriptException 97.633 - log("got " + se + " as expected"); 97.634 - } catch (final Throwable t) { 97.635 - t.printStackTrace(); 97.636 - fail(t.getMessage()); 97.637 - } 97.638 - } 97.639 - 97.640 - @Test 97.641 - public void invokeMethodExceptionTest() { 97.642 - final ScriptEngineManager m = new ScriptEngineManager(); 97.643 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.644 - try { 97.645 - e.eval("var sobj = {}; sobj.foo = function func() { throw new TypeError(); }"); 97.646 - } catch (final Throwable t) { 97.647 - t.printStackTrace(); 97.648 - fail(t.getMessage()); 97.649 - } 97.650 - 97.651 - try { 97.652 - final Object sobj = e.get("sobj"); 97.653 - ((Invocable)e).invokeMethod(sobj, "foo"); 97.654 - fail("should have thrown exception"); 97.655 - } catch (final ScriptException se) { 97.656 - // ECMA TypeError property wrapped as a ScriptException 97.657 - log("got " + se + " as expected"); 97.658 - } catch (final Throwable t) { 97.659 - t.printStackTrace(); 97.660 - fail(t.getMessage()); 97.661 - } 97.662 - } 97.663 - 97.664 - @Test 97.665 - public void scriptObjectMirrorToStringTest() { 97.666 - final ScriptEngineManager m = new ScriptEngineManager(); 97.667 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.668 - try { 97.669 - Object obj = e.eval("new TypeError('wrong type')"); 97.670 - assertEquals(obj.toString(), "TypeError: wrong type", "toString returns wrong value"); 97.671 - } catch (final Throwable t) { 97.672 - t.printStackTrace(); 97.673 - fail(t.getMessage()); 97.674 - } 97.675 - 97.676 - try { 97.677 - Object obj = e.eval("function func() { print('hello'); }"); 97.678 - assertEquals(obj.toString(), "function func() { print('hello'); }", "toString returns wrong value"); 97.679 - } catch (final Throwable t) { 97.680 - t.printStackTrace(); 97.681 - fail(t.getMessage()); 97.682 - } 97.683 - } 97.684 - 97.685 - @Test 97.686 - public void engineScopeTest() { 97.687 - final ScriptEngineManager m = new ScriptEngineManager(); 97.688 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.689 - Bindings engineScope = e.getBindings(ScriptContext.ENGINE_SCOPE); 97.690 - 97.691 - // check few ECMA standard built-in global properties 97.692 - assertNotNull(engineScope.get("Object")); 97.693 - assertNotNull(engineScope.get("TypeError")); 97.694 - assertNotNull(engineScope.get("eval")); 97.695 - 97.696 - // can access via ScriptEngine.get as well 97.697 - assertNotNull(e.get("Object")); 97.698 - assertNotNull(e.get("TypeError")); 97.699 - assertNotNull(e.get("eval")); 97.700 - 97.701 - // Access by either way should return same object 97.702 - assertEquals(engineScope.get("Array"), e.get("Array")); 97.703 - assertEquals(engineScope.get("EvalError"), e.get("EvalError")); 97.704 - assertEquals(engineScope.get("undefined"), e.get("undefined")); 97.705 - 97.706 - // try exposing a new variable from scope 97.707 - engineScope.put("myVar", "foo"); 97.708 - try { 97.709 - assertEquals(e.eval("myVar"), "foo"); 97.710 - } catch (final ScriptException se) { 97.711 - se.printStackTrace(); 97.712 - fail(se.getMessage()); 97.713 - } 97.714 - 97.715 - // update "myVar" in script an check the value from scope 97.716 - try { 97.717 - e.eval("myVar = 'nashorn';"); 97.718 - } catch (final ScriptException se) { 97.719 - se.printStackTrace(); 97.720 - fail(se.getMessage()); 97.721 - } 97.722 - 97.723 - // now check modified value from scope and engine 97.724 - assertEquals(engineScope.get("myVar"), "nashorn"); 97.725 - assertEquals(e.get("myVar"), "nashorn"); 97.726 - } 97.727 - 97.728 - @Test 97.729 - public void multiGlobalTest() { 97.730 - final ScriptEngineManager m = new ScriptEngineManager(); 97.731 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.732 - final Bindings b = e.createBindings(); 97.733 - final ScriptContext newCtxt = new SimpleScriptContext(); 97.734 - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 97.735 - 97.736 - try { 97.737 - Object obj1 = e.eval("Object"); 97.738 - Object obj2 = e.eval("Object", newCtxt); 97.739 - Assert.assertNotEquals(obj1, obj2); 97.740 - Assert.assertNotNull(obj1); 97.741 - Assert.assertNotNull(obj2); 97.742 - Assert.assertEquals(obj1.toString(), obj2.toString()); 97.743 - 97.744 - e.eval("x = 'hello'"); 97.745 - e.eval("x = 'world'", newCtxt); 97.746 - Object x1 = e.getContext().getAttribute("x"); 97.747 - Object x2 = newCtxt.getAttribute("x"); 97.748 - Assert.assertNotEquals(x1, x2); 97.749 - Assert.assertEquals(x1, "hello"); 97.750 - Assert.assertEquals(x2, "world"); 97.751 - 97.752 - x1 = e.eval("x"); 97.753 - x2 = e.eval("x", newCtxt); 97.754 - Assert.assertNotEquals(x1, x2); 97.755 - Assert.assertEquals(x1, "hello"); 97.756 - Assert.assertEquals(x2, "world"); 97.757 - 97.758 - final ScriptContext origCtxt = e.getContext(); 97.759 - e.setContext(newCtxt); 97.760 - e.eval("y = new Object()"); 97.761 - e.eval("y = new Object()", origCtxt); 97.762 - 97.763 - Object y1 = origCtxt.getAttribute("y"); 97.764 - Object y2 = newCtxt.getAttribute("y"); 97.765 - Assert.assertNotEquals(y1, y2); 97.766 - Assert.assertNotEquals(e.eval("y"), e.eval("y", origCtxt)); 97.767 - Assert.assertEquals("[object Object]", y1.toString()); 97.768 - Assert.assertEquals("[object Object]", y2.toString()); 97.769 - } catch (final ScriptException se) { 97.770 - se.printStackTrace(); 97.771 - fail(se.getMessage()); 97.772 - } 97.773 - } 97.774 - 97.775 - @Test 97.776 - /** 97.777 - * Tests whether invocation of a JavaScript method through a variable arity Java method will pass the vararg array. 97.778 - * Both non-vararg and vararg JavaScript methods are tested. 97.779 - * @throws ScriptException 97.780 - */ 97.781 - public void variableArityInterfaceTest() throws ScriptException { 97.782 - final ScriptEngineManager m = new ScriptEngineManager(); 97.783 - final ScriptEngine e = m.getEngineByName("nashorn"); 97.784 - e.eval( 97.785 - "function test1(i, strings) {" + 97.786 - " return 'i == ' + i + ', strings instanceof java.lang.String[] == ' + (strings instanceof Java.type('java.lang.String[]')) + ', strings == ' + java.util.Arrays.toString(strings)" + 97.787 - "}" + 97.788 - "function test2() {" + 97.789 - " return 'arguments[0] == ' + arguments[0] + ', arguments[1] instanceof java.lang.String[] == ' + (arguments[1] instanceof Java.type('java.lang.String[]')) + ', arguments[1] == ' + java.util.Arrays.toString(arguments[1])" + 97.790 - "}" 97.791 - ); 97.792 - final VariableArityTestInterface itf = ((Invocable)e).getInterface(VariableArityTestInterface.class); 97.793 - Assert.assertEquals(itf.test1(42, "a", "b"), "i == 42, strings instanceof java.lang.String[] == true, strings == [a, b]"); 97.794 - Assert.assertEquals(itf.test2(44, "c", "d", "e"), "arguments[0] == 44, arguments[1] instanceof java.lang.String[] == true, arguments[1] == [c, d, e]"); 97.795 - } 97.796 - 97.797 - @Test 97.798 // check that print function prints arg followed by newline char 97.799 public void printTest() { 97.800 final ScriptEngineManager m = new ScriptEngineManager(); 97.801 @@ -1235,8 +490,7 @@ 97.802 fail(t.getMessage()); 97.803 } 97.804 97.805 - // dos2unix - fix line endings if running on windows 97.806 - assertEquals(sw.toString().replaceAll("\r", ""), "hello\n"); 97.807 + assertEquals(sw.toString(), println("hello")); 97.808 } 97.809 97.810 @Test 97.811 @@ -1253,7 +507,14 @@ 97.812 fail(t.getMessage()); 97.813 } 97.814 97.815 - // dos2unix - fix line endings if running on windows 97.816 - assertEquals(sw.toString().replaceAll("\r", ""), "34 true hello\n"); 97.817 + assertEquals(sw.toString(), println("34 true hello")); 97.818 + } 97.819 + 97.820 + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); 97.821 + 97.822 + // Returns String that would be the result of calling PrintWriter.println 97.823 + // of the given String. (This is to handle platform specific newline). 97.824 + private static String println(final String str) { 97.825 + return str + LINE_SEPARATOR; 97.826 } 97.827 }
98.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 98.2 +++ b/test/src/jdk/nashorn/api/scripting/ScriptObjectMirrorTest.java Thu Aug 29 16:34:31 2013 -0700 98.3 @@ -0,0 +1,230 @@ 98.4 +/* 98.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 98.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 98.7 + * 98.8 + * This code is free software; you can redistribute it and/or modify it 98.9 + * under the terms of the GNU General Public License version 2 only, as 98.10 + * published by the Free Software Foundation. Oracle designates this 98.11 + * particular file as subject to the "Classpath" exception as provided 98.12 + * by Oracle in the LICENSE file that accompanied this code. 98.13 + * 98.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 98.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 98.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 98.17 + * version 2 for more details (a copy is included in the LICENSE file that 98.18 + * accompanied this code). 98.19 + * 98.20 + * You should have received a copy of the GNU General Public License version 98.21 + * 2 along with this work; if not, write to the Free Software Foundation, 98.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 98.23 + * 98.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 98.25 + * or visit www.oracle.com if you need additional information or have any 98.26 + * questions. 98.27 + */ 98.28 + 98.29 +package jdk.nashorn.api.scripting; 98.30 + 98.31 +import java.util.HashMap; 98.32 +import java.util.Map; 98.33 +import javax.script.ScriptEngine; 98.34 +import javax.script.ScriptEngineManager; 98.35 +import javax.script.ScriptException; 98.36 +import static org.testng.Assert.assertEquals; 98.37 +import static org.testng.Assert.assertFalse; 98.38 +import static org.testng.Assert.assertTrue; 98.39 +import static org.testng.Assert.fail; 98.40 +import org.testng.annotations.Test; 98.41 + 98.42 +/** 98.43 + * Tests to check jdk.nashorn.api.scripting.ScriptObjectMirror API. 98.44 + */ 98.45 +public class ScriptObjectMirrorTest { 98.46 + 98.47 + @SuppressWarnings("unchecked") 98.48 + @Test 98.49 + public void reflectionTest() throws ScriptException { 98.50 + final ScriptEngineManager m = new ScriptEngineManager(); 98.51 + final ScriptEngine e = m.getEngineByName("nashorn"); 98.52 + 98.53 + e.eval("var obj = { x: 344, y: 'nashorn' }"); 98.54 + 98.55 + int count = 0; 98.56 + Map<Object, Object> map = (Map<Object, Object>) e.get("obj"); 98.57 + assertFalse(map.isEmpty()); 98.58 + assertTrue(map.keySet().contains("x")); 98.59 + assertTrue(map.containsKey("x")); 98.60 + assertTrue(map.values().contains("nashorn")); 98.61 + assertTrue(map.containsValue("nashorn")); 98.62 + for (final Map.Entry<?, ?> ex : map.entrySet()) { 98.63 + final Object key = ex.getKey(); 98.64 + if (key.equals("x")) { 98.65 + assertTrue(344 == ((Number) ex.getValue()).doubleValue()); 98.66 + count++; 98.67 + } else if (key.equals("y")) { 98.68 + assertEquals(ex.getValue(), "nashorn"); 98.69 + count++; 98.70 + } 98.71 + } 98.72 + assertEquals(2, count); 98.73 + assertEquals(2, map.size()); 98.74 + 98.75 + // add property 98.76 + map.put("z", "hello"); 98.77 + assertEquals(e.eval("obj.z"), "hello"); 98.78 + assertEquals(map.get("z"), "hello"); 98.79 + assertTrue(map.keySet().contains("z")); 98.80 + assertTrue(map.containsKey("z")); 98.81 + assertTrue(map.values().contains("hello")); 98.82 + assertTrue(map.containsValue("hello")); 98.83 + assertEquals(map.size(), 3); 98.84 + 98.85 + final Map<Object, Object> newMap = new HashMap<>(); 98.86 + newMap.put("foo", 23.0); 98.87 + newMap.put("bar", true); 98.88 + map.putAll(newMap); 98.89 + 98.90 + assertEquals(e.eval("obj.foo"), 23.0); 98.91 + assertEquals(e.eval("obj.bar"), true); 98.92 + 98.93 + // remove using map method 98.94 + map.remove("foo"); 98.95 + assertEquals(e.eval("typeof obj.foo"), "undefined"); 98.96 + 98.97 + count = 0; 98.98 + e.eval("var arr = [ true, 'hello' ]"); 98.99 + map = (Map<Object, Object>) e.get("arr"); 98.100 + assertFalse(map.isEmpty()); 98.101 + assertTrue(map.containsKey("length")); 98.102 + assertTrue(map.containsValue("hello")); 98.103 + for (final Map.Entry<?, ?> ex : map.entrySet()) { 98.104 + final Object key = ex.getKey(); 98.105 + if (key.equals("0")) { 98.106 + assertEquals(ex.getValue(), Boolean.TRUE); 98.107 + count++; 98.108 + } else if (key.equals("1")) { 98.109 + assertEquals(ex.getValue(), "hello"); 98.110 + count++; 98.111 + } 98.112 + } 98.113 + assertEquals(count, 2); 98.114 + assertEquals(map.size(), 2); 98.115 + 98.116 + // add element 98.117 + map.put("2", "world"); 98.118 + assertEquals(map.get("2"), "world"); 98.119 + assertEquals(map.size(), 3); 98.120 + 98.121 + // remove all 98.122 + map.clear(); 98.123 + assertTrue(map.isEmpty()); 98.124 + assertEquals(e.eval("typeof arr[0]"), "undefined"); 98.125 + assertEquals(e.eval("typeof arr[1]"), "undefined"); 98.126 + assertEquals(e.eval("typeof arr[2]"), "undefined"); 98.127 + } 98.128 + 98.129 + @Test 98.130 + public void jsobjectTest() { 98.131 + final ScriptEngineManager m = new ScriptEngineManager(); 98.132 + final ScriptEngine e = m.getEngineByName("nashorn"); 98.133 + try { 98.134 + e.eval("var obj = { '1': 'world', func: function() { return this.bar; }, bar: 'hello' }"); 98.135 + JSObject obj = (JSObject) e.get("obj"); 98.136 + 98.137 + // try basic get on existing properties 98.138 + if (!obj.getMember("bar").equals("hello")) { 98.139 + fail("obj.bar != 'hello'"); 98.140 + } 98.141 + 98.142 + if (!obj.getSlot(1).equals("world")) { 98.143 + fail("obj[1] != 'world'"); 98.144 + } 98.145 + 98.146 + if (!obj.call("func", new Object[0]).equals("hello")) { 98.147 + fail("obj.call('func') != 'hello'"); 98.148 + } 98.149 + 98.150 + // try setting properties 98.151 + obj.setMember("bar", "new-bar"); 98.152 + obj.setSlot(1, "new-element-1"); 98.153 + if (!obj.getMember("bar").equals("new-bar")) { 98.154 + fail("obj.bar != 'new-bar'"); 98.155 + } 98.156 + 98.157 + if (!obj.getSlot(1).equals("new-element-1")) { 98.158 + fail("obj[1] != 'new-element-1'"); 98.159 + } 98.160 + 98.161 + // try adding properties 98.162 + obj.setMember("prop", "prop-value"); 98.163 + obj.setSlot(12, "element-12"); 98.164 + if (!obj.getMember("prop").equals("prop-value")) { 98.165 + fail("obj.prop != 'prop-value'"); 98.166 + } 98.167 + 98.168 + if (!obj.getSlot(12).equals("element-12")) { 98.169 + fail("obj[12] != 'element-12'"); 98.170 + } 98.171 + 98.172 + // delete properties 98.173 + obj.removeMember("prop"); 98.174 + if ("prop-value".equals(obj.getMember("prop"))) { 98.175 + fail("obj.prop is not deleted!"); 98.176 + } 98.177 + 98.178 + // Simple eval tests 98.179 + assertEquals(obj.eval("typeof Object"), "function"); 98.180 + assertEquals(obj.eval("'nashorn'.substring(3)"), "horn"); 98.181 + } catch (final Exception exp) { 98.182 + exp.printStackTrace(); 98.183 + fail(exp.getMessage()); 98.184 + } 98.185 + } 98.186 + 98.187 + @Test 98.188 + public void scriptObjectMirrorToStringTest() { 98.189 + final ScriptEngineManager m = new ScriptEngineManager(); 98.190 + final ScriptEngine e = m.getEngineByName("nashorn"); 98.191 + try { 98.192 + Object obj = e.eval("new TypeError('wrong type')"); 98.193 + assertEquals(obj.toString(), "TypeError: wrong type", "toString returns wrong value"); 98.194 + } catch (final Throwable t) { 98.195 + t.printStackTrace(); 98.196 + fail(t.getMessage()); 98.197 + } 98.198 + 98.199 + try { 98.200 + Object obj = e.eval("function func() { print('hello'); }"); 98.201 + assertEquals(obj.toString(), "function func() { print('hello'); }", "toString returns wrong value"); 98.202 + } catch (final Throwable t) { 98.203 + t.printStackTrace(); 98.204 + fail(t.getMessage()); 98.205 + } 98.206 + } 98.207 + 98.208 + @Test 98.209 + public void mirrorNewObjectGlobalFunctionTest() throws ScriptException { 98.210 + final ScriptEngineManager m = new ScriptEngineManager(); 98.211 + final ScriptEngine e = m.getEngineByName("nashorn"); 98.212 + final ScriptEngine e2 = m.getEngineByName("nashorn"); 98.213 + 98.214 + e.eval("function func() {}"); 98.215 + e2.put("foo", e.get("func")); 98.216 + final Object e2global = e2.eval("this"); 98.217 + final Object newObj = ((ScriptObjectMirror) e2global).newObject("foo"); 98.218 + assertTrue(newObj instanceof ScriptObjectMirror); 98.219 + } 98.220 + 98.221 + @Test 98.222 + public void mirrorNewObjectInstanceFunctionTest() throws ScriptException { 98.223 + final ScriptEngineManager m = new ScriptEngineManager(); 98.224 + final ScriptEngine e = m.getEngineByName("nashorn"); 98.225 + final ScriptEngine e2 = m.getEngineByName("nashorn"); 98.226 + 98.227 + e.eval("function func() {}"); 98.228 + e2.put("func", e.get("func")); 98.229 + final Object e2obj = e2.eval("({ foo: func })"); 98.230 + final Object newObj = ((ScriptObjectMirror) e2obj).newObject("foo"); 98.231 + assertTrue(newObj instanceof ScriptObjectMirror); 98.232 + } 98.233 +}
99.1 --- a/test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java Thu Aug 29 09:42:13 2013 -0700 99.2 +++ b/test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java Thu Aug 29 16:34:31 2013 -0700 99.3 @@ -32,7 +32,10 @@ 99.4 import javax.script.ScriptEngine; 99.5 import javax.script.ScriptEngineFactory; 99.6 import javax.script.ScriptEngineManager; 99.7 +import javax.script.ScriptContext; 99.8 import javax.script.ScriptException; 99.9 +import javax.script.SimpleBindings; 99.10 +import javax.script.SimpleScriptContext; 99.11 import jdk.nashorn.api.scripting.NashornScriptEngineFactory; 99.12 import org.testng.annotations.Test; 99.13 99.14 @@ -196,4 +199,25 @@ 99.15 } 99.16 fail("Cannot find nashorn factory!"); 99.17 } 99.18 + 99.19 + @Test 99.20 + public void globalPerEngineTest() throws ScriptException { 99.21 + final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 99.22 + final String[] options = new String[] { "--global-per-engine" }; 99.23 + final ScriptEngine e = fac.getScriptEngine(options); 99.24 + 99.25 + e.eval("function foo() {}"); 99.26 + 99.27 + final ScriptContext newCtx = new SimpleScriptContext(); 99.28 + newCtx.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); 99.29 + 99.30 + // all global definitions shared and so 'foo' should be 99.31 + // visible in new Bindings as well. 99.32 + assertTrue(e.eval("typeof foo", newCtx).equals("function")); 99.33 + 99.34 + e.eval("function bar() {}", newCtx); 99.35 + 99.36 + // bar should be visible in default context 99.37 + assertTrue(e.eval("typeof bar").equals("function")); 99.38 + } 99.39 }
100.1 --- a/tools/fxshell/jdk/nashorn/tools/FXShell.java Thu Aug 29 09:42:13 2013 -0700 100.2 +++ b/tools/fxshell/jdk/nashorn/tools/FXShell.java Thu Aug 29 16:34:31 2013 -0700 100.3 @@ -32,13 +32,9 @@ 100.4 import java.util.ArrayList; 100.5 import java.util.List; 100.6 import java.util.Map; 100.7 -import java.util.Set; 100.8 -import java.util.logging.Level; 100.9 -import java.util.logging.Logger; 100.10 import javafx.application.Application; 100.11 import javafx.stage.Stage; 100.12 import javax.script.Invocable; 100.13 -import javax.script.ScriptContext; 100.14 import javax.script.ScriptEngine; 100.15 import javax.script.ScriptEngineFactory; 100.16 import javax.script.ScriptEngineManager; 100.17 @@ -180,6 +176,7 @@ 100.18 * 100.19 * @return Last evaluation result (discarded.) 100.20 */ 100.21 + @SuppressWarnings("resource") 100.22 private Object load(String path) { 100.23 try { 100.24 FileInputStream file = new FileInputStream(path);