Sat, 25 Jun 2016 20:03:39 +0100
Merge
1.1 --- a/src/jdk/nashorn/api/scripting/NashornScriptEngine.java Tue Jun 21 10:15:20 2016 -0700 1.2 +++ b/src/jdk/nashorn/api/scripting/NashornScriptEngine.java Sat Jun 25 20:03:39 2016 +0100 1.3 @@ -319,6 +319,9 @@ 1.4 // Create new global instance mirror and associate with the Bindings. 1.5 final ScriptObjectMirror mirror = createGlobalMirror(); 1.6 bindings.put(NASHORN_GLOBAL, mirror); 1.7 + // Since we created this global explicitly for the non-default script context we set the 1.8 + // current script context in global permanently so that invokes work as expected. See JDK-8150219 1.9 + mirror.getHomeGlobal().setInitScriptContext(ctxt); 1.10 return mirror.getHomeGlobal(); 1.11 } 1.12
2.1 --- a/src/jdk/nashorn/internal/objects/Global.java Tue Jun 21 10:15:20 2016 -0700 2.2 +++ b/src/jdk/nashorn/internal/objects/Global.java Sat Jun 25 20:03:39 2016 +0100 2.3 @@ -928,6 +928,8 @@ 2.4 private ThreadLocal<ScriptContext> scontext; 2.5 // current ScriptEngine associated - can be null. 2.6 private ScriptEngine engine; 2.7 + // initial ScriptContext - usually null and only used for special case 2.8 + private volatile ScriptContext initscontext; 2.9 2.10 // ES6 global lexical scope. 2.11 private final LexicalScope lexicalScope; 2.12 @@ -953,9 +955,22 @@ 2.13 return scontext.get(); 2.14 } 2.15 2.16 + /** 2.17 + * Set the initial script context 2.18 + * @param ctxt initial script context 2.19 + */ 2.20 + public void setInitScriptContext(final ScriptContext ctxt) { 2.21 + this.initscontext = ctxt; 2.22 + } 2.23 + 2.24 private ScriptContext currentContext() { 2.25 final ScriptContext sc = scontext != null? scontext.get() : null; 2.26 - return (sc != null)? sc : (engine != null? engine.getContext() : null); 2.27 + if (sc != null) { 2.28 + return sc; 2.29 + } else if (initscontext != null) { 2.30 + return initscontext; 2.31 + } 2.32 + return engine != null? engine.getContext() : null; 2.33 } 2.34 2.35 @Override
3.1 --- a/src/jdk/nashorn/internal/runtime/regexp/RegExpScanner.java Tue Jun 21 10:15:20 2016 -0700 3.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/RegExpScanner.java Sat Jun 25 20:03:39 2016 +0100 3.3 @@ -80,8 +80,17 @@ 3.4 this.negLookaheadLevel = negLookaheadLevel; 3.5 } 3.6 3.7 - boolean isContained(final int group, final int level) { 3.8 - return group == this.negLookaheadGroup && level >= this.negLookaheadLevel; 3.9 + /** 3.10 + * Returns true if this Capture can be referenced from the position specified by the 3.11 + * group and level parameters. This is the case if either the group is not within 3.12 + * a negative lookahead, or the position of the referrer is in the same negative lookahead. 3.13 + * 3.14 + * @param group current negative lookahead group 3.15 + * @param level current negative lokahead level 3.16 + * @return true if this capture group can be referenced from the given position 3.17 + */ 3.18 + boolean canBeReferencedFrom(final int group, final int level) { 3.19 + return this.negLookaheadLevel == 0 || (group == this.negLookaheadGroup && level >= this.negLookaheadLevel); 3.20 } 3.21 3.22 } 3.23 @@ -671,8 +680,9 @@ 3.24 3.25 } else if (decimalValue <= caps.size()) { 3.26 // Captures inside a negative lookahead are undefined when referenced from the outside. 3.27 - if (!caps.get(decimalValue - 1).isContained(negLookaheadGroup, negLookaheadLevel)) { 3.28 - // Reference to capture in negative lookahead, omit from output buffer. 3.29 + final Capture capture = caps.get(decimalValue - 1); 3.30 + if (!capture.canBeReferencedFrom(negLookaheadGroup, negLookaheadLevel)) { 3.31 + // Outside reference to capture in negative lookahead, omit from output buffer. 3.32 sb.setLength(sb.length() - 1); 3.33 } else { 3.34 // Append backreference to output buffer.
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/script/basic/JDK-8137240.js Sat Jun 25 20:03:39 2016 +0100 4.3 @@ -0,0 +1,44 @@ 4.4 +/* 4.5 + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.23 + * or visit www.oracle.com if you need additional information or have any 4.24 + * questions. 4.25 + */ 4.26 + 4.27 +/** 4.28 + * JDK-8137240: Negative lookahead in RegEx breaks backreference 4.29 + * 4.30 + * @test 4.31 + * @run 4.32 + */ 4.33 + 4.34 + 4.35 +Assert.assertEquals('aa'.replace(/(a)(?!b)\1/gm, 'c'), 'c'); 4.36 + 4.37 +var result = 'aa'.match(/(a)(?!b)\1/); 4.38 +Assert.assertTrue(result.length === 2); 4.39 +Assert.assertTrue(result[0] === 'aa'); 4.40 +Assert.assertTrue(result[1] === 'a'); 4.41 + 4.42 +result = 'aa'.match(/(a)(?!(b))\2(a)/); 4.43 +Assert.assertTrue(result.length === 4); 4.44 +Assert.assertTrue(result[0] === 'aa'); 4.45 +Assert.assertTrue(result[1] === 'a'); 4.46 +Assert.assertTrue(result[2] === undefined); 4.47 +Assert.assertTrue(result[3] === 'a');
5.1 --- a/test/src/jdk/nashorn/api/scripting/test/ScopeTest.java Tue Jun 21 10:15:20 2016 -0700 5.2 +++ b/test/src/jdk/nashorn/api/scripting/test/ScopeTest.java Sat Jun 25 20:03:39 2016 +0100 5.3 @@ -30,6 +30,8 @@ 5.4 import static org.testng.Assert.assertTrue; 5.5 import static org.testng.Assert.fail; 5.6 import javax.script.Bindings; 5.7 +import javax.script.Compilable; 5.8 +import javax.script.CompiledScript; 5.9 import javax.script.Invocable; 5.10 import javax.script.ScriptContext; 5.11 import javax.script.ScriptEngine; 5.12 @@ -911,4 +913,27 @@ 5.13 Object value = ((Invocable)engine).invokeFunction("newfunc"); 5.14 assertTrue(((Number)value).intValue() == 42); 5.15 } 5.16 + 5.17 + // @bug 8150219 ReferenceError in 1.8.0_72 5.18 + // When we create a Global for a non-default ScriptContext that needs one keep the 5.19 + // ScriptContext associated with the Global so that invoke methods work as expected. 5.20 + @Test 5.21 + public void invokeFunctionWithCustomScriptContextTest() throws Exception { 5.22 + final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); 5.23 + 5.24 + // create an engine and a ScriptContext, but don't set it as default 5.25 + ScriptContext scriptContext = new SimpleScriptContext(); 5.26 + 5.27 + // Set some value in the context 5.28 + scriptContext.setAttribute("myString", "foo", ScriptContext.ENGINE_SCOPE); 5.29 + 5.30 + // Evaluate script with custom context and get back a function 5.31 + final String script = "function (c) { return myString.indexOf(c); }"; 5.32 + CompiledScript compiledScript = ((Compilable)engine).compile(script); 5.33 + Object func = compiledScript.eval(scriptContext); 5.34 + 5.35 + // Invoked function should be able to see context it was evaluated with 5.36 + Object result = ((Invocable) engine).invokeMethod(func, "call", func, "o", null); 5.37 + assertTrue(((Number)result).intValue() == 1); 5.38 + } 5.39 }