Mercurial > hg > openjdk > jdk8u > nashorn
changeset 1834:29f97057e4e1 jdk8u112-b02
Merge
author | robm |
---|---|
date | Sat, 25 Jun 2016 20:03:39 +0100 |
parents | 8dad9af70d3e (current diff) d95a6070758d (diff) |
children | a569f39e9c01 |
files | |
diffstat | 5 files changed, 102 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/src/jdk/nashorn/api/scripting/NashornScriptEngine.java Tue Jun 21 10:15:20 2016 -0700 +++ b/src/jdk/nashorn/api/scripting/NashornScriptEngine.java Sat Jun 25 20:03:39 2016 +0100 @@ -319,6 +319,9 @@ // Create new global instance mirror and associate with the Bindings. final ScriptObjectMirror mirror = createGlobalMirror(); bindings.put(NASHORN_GLOBAL, mirror); + // Since we created this global explicitly for the non-default script context we set the + // current script context in global permanently so that invokes work as expected. See JDK-8150219 + mirror.getHomeGlobal().setInitScriptContext(ctxt); return mirror.getHomeGlobal(); }
--- a/src/jdk/nashorn/internal/objects/Global.java Tue Jun 21 10:15:20 2016 -0700 +++ b/src/jdk/nashorn/internal/objects/Global.java Sat Jun 25 20:03:39 2016 +0100 @@ -928,6 +928,8 @@ private ThreadLocal<ScriptContext> scontext; // current ScriptEngine associated - can be null. private ScriptEngine engine; + // initial ScriptContext - usually null and only used for special case + private volatile ScriptContext initscontext; // ES6 global lexical scope. private final LexicalScope lexicalScope; @@ -953,9 +955,22 @@ return scontext.get(); } + /** + * Set the initial script context + * @param ctxt initial script context + */ + public void setInitScriptContext(final ScriptContext ctxt) { + this.initscontext = ctxt; + } + private ScriptContext currentContext() { final ScriptContext sc = scontext != null? scontext.get() : null; - return (sc != null)? sc : (engine != null? engine.getContext() : null); + if (sc != null) { + return sc; + } else if (initscontext != null) { + return initscontext; + } + return engine != null? engine.getContext() : null; } @Override
--- a/src/jdk/nashorn/internal/runtime/regexp/RegExpScanner.java Tue Jun 21 10:15:20 2016 -0700 +++ b/src/jdk/nashorn/internal/runtime/regexp/RegExpScanner.java Sat Jun 25 20:03:39 2016 +0100 @@ -80,8 +80,17 @@ this.negLookaheadLevel = negLookaheadLevel; } - boolean isContained(final int group, final int level) { - return group == this.negLookaheadGroup && level >= this.negLookaheadLevel; + /** + * Returns true if this Capture can be referenced from the position specified by the + * group and level parameters. This is the case if either the group is not within + * a negative lookahead, or the position of the referrer is in the same negative lookahead. + * + * @param group current negative lookahead group + * @param level current negative lokahead level + * @return true if this capture group can be referenced from the given position + */ + boolean canBeReferencedFrom(final int group, final int level) { + return this.negLookaheadLevel == 0 || (group == this.negLookaheadGroup && level >= this.negLookaheadLevel); } } @@ -671,8 +680,9 @@ } else if (decimalValue <= caps.size()) { // Captures inside a negative lookahead are undefined when referenced from the outside. - if (!caps.get(decimalValue - 1).isContained(negLookaheadGroup, negLookaheadLevel)) { - // Reference to capture in negative lookahead, omit from output buffer. + final Capture capture = caps.get(decimalValue - 1); + if (!capture.canBeReferencedFrom(negLookaheadGroup, negLookaheadLevel)) { + // Outside reference to capture in negative lookahead, omit from output buffer. sb.setLength(sb.length() - 1); } else { // Append backreference to output buffer.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/script/basic/JDK-8137240.js Sat Jun 25 20:03:39 2016 +0100 @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * JDK-8137240: Negative lookahead in RegEx breaks backreference + * + * @test + * @run + */ + + +Assert.assertEquals('aa'.replace(/(a)(?!b)\1/gm, 'c'), 'c'); + +var result = 'aa'.match(/(a)(?!b)\1/); +Assert.assertTrue(result.length === 2); +Assert.assertTrue(result[0] === 'aa'); +Assert.assertTrue(result[1] === 'a'); + +result = 'aa'.match(/(a)(?!(b))\2(a)/); +Assert.assertTrue(result.length === 4); +Assert.assertTrue(result[0] === 'aa'); +Assert.assertTrue(result[1] === 'a'); +Assert.assertTrue(result[2] === undefined); +Assert.assertTrue(result[3] === 'a');
--- a/test/src/jdk/nashorn/api/scripting/test/ScopeTest.java Tue Jun 21 10:15:20 2016 -0700 +++ b/test/src/jdk/nashorn/api/scripting/test/ScopeTest.java Sat Jun 25 20:03:39 2016 +0100 @@ -30,6 +30,8 @@ import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; import javax.script.Bindings; +import javax.script.Compilable; +import javax.script.CompiledScript; import javax.script.Invocable; import javax.script.ScriptContext; import javax.script.ScriptEngine; @@ -911,4 +913,27 @@ Object value = ((Invocable)engine).invokeFunction("newfunc"); assertTrue(((Number)value).intValue() == 42); } + + // @bug 8150219 ReferenceError in 1.8.0_72 + // When we create a Global for a non-default ScriptContext that needs one keep the + // ScriptContext associated with the Global so that invoke methods work as expected. + @Test + public void invokeFunctionWithCustomScriptContextTest() throws Exception { + final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); + + // create an engine and a ScriptContext, but don't set it as default + ScriptContext scriptContext = new SimpleScriptContext(); + + // Set some value in the context + scriptContext.setAttribute("myString", "foo", ScriptContext.ENGINE_SCOPE); + + // Evaluate script with custom context and get back a function + final String script = "function (c) { return myString.indexOf(c); }"; + CompiledScript compiledScript = ((Compilable)engine).compile(script); + Object func = compiledScript.eval(scriptContext); + + // Invoked function should be able to see context it was evaluated with + Object result = ((Invocable) engine).invokeMethod(func, "call", func, "o", null); + assertTrue(((Number)result).intValue() == 1); + } }