Mercurial > hg > openjdk > jdk9 > nashorn
changeset 895:2a4a7831f274
8047035: (function() "hello")() crashes in Lexer with jdk9
Reviewed-by: lagergren, hannesw, attila
author | sundar |
---|---|
date | Tue, 17 Jun 2014 15:55:39 +0530 |
parents | b9a5c20a37b9 |
children | 1a9340351629 |
files | src/jdk/nashorn/internal/parser/Parser.java src/jdk/nashorn/internal/parser/Token.java src/jdk/nashorn/internal/runtime/RecompilableScriptFunctionData.java test/script/basic/JDK-8047035.js test/script/basic/JDK-8047035.js.EXPECTED |
diffstat | 5 files changed, 65 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/src/jdk/nashorn/internal/parser/Parser.java Tue Jun 17 11:37:03 2014 +0200 +++ b/src/jdk/nashorn/internal/parser/Parser.java Tue Jun 17 15:55:39 2014 +0530 @@ -680,7 +680,7 @@ */ private FunctionNode program(final String scriptName, final boolean allowPropertyFunction) { // Make a pseudo-token for the script holding its start and length. - final long functionToken = Token.toDesc(FUNCTION, getProgramStartPosition(token), source.getLength()); + final long functionToken = Token.toDesc(FUNCTION, Token.descPosition(Token.withDelimiter(token)), source.getLength()); final int functionLine = line; // Set up the script to append elements. @@ -711,20 +711,6 @@ } /** - * Returns the start position of the program based on its first token. Normally returns the position of the token - * itself, except in case of string tokens which report their position past their opening delimiter and thus need - * to have one subtracted from their position. - * @param firstToken the first token of the program - * @return the start position of the program - */ - private static int getProgramStartPosition(final long firstToken) { - final int start = Token.descPosition(firstToken); - switch(Token.descType(firstToken)) { - case STRING: case ESCSTRING: case EXECSTRING: return start - 1; - default: return start; - } - } - /** * Directive value or null if statement is not a directive. * * @param stmt Statement to be checked
--- a/src/jdk/nashorn/internal/parser/Token.java Tue Jun 17 11:37:03 2014 +0200 +++ b/src/jdk/nashorn/internal/parser/Token.java Tue Jun 17 15:55:39 2014 +0530 @@ -61,6 +61,28 @@ } /** + * Normally returns the token itself, except in case of string tokens + * which report their position past their opening delimiter and thus + * need to have position and length adjusted. + * + * @param token Token descriptor. + * @return same or adjusted token. + */ + public static long withDelimiter(final long token) { + final TokenType tokenType = Token.descType(token); + switch(tokenType) { + case STRING: case ESCSTRING: case EXECSTRING: { + final int start = Token.descPosition(token) - 1; + final int len = Token.descLength(token) + 2; + return toDesc(tokenType, start, len); + } + default: { + return token; + } + } + } + + /** * Extract token length from a token descriptor. * @param token Token descriptor. * @return Length of the token.
--- a/src/jdk/nashorn/internal/runtime/RecompilableScriptFunctionData.java Tue Jun 17 11:37:03 2014 +0200 +++ b/src/jdk/nashorn/internal/runtime/RecompilableScriptFunctionData.java Tue Jun 17 15:55:39 2014 +0530 @@ -294,7 +294,7 @@ private static long tokenFor(final FunctionNode fn) { final int position = Token.descPosition(fn.getFirstToken()); - final long lastToken = fn.getLastToken(); + final long lastToken = Token.withDelimiter(fn.getLastToken()); // EOL uses length field to store the line number final int length = Token.descPosition(lastToken) - position + (Token.descType(lastToken) == TokenType.EOL ? 0 : Token.descLength(lastToken));
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/script/basic/JDK-8047035.js Tue Jun 17 15:55:39 2014 +0530 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2014, 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-8047035: (function() "hello")() crashes in Lexer with jdk9 + * + * @test + * @run + */ + +// should not print ")" at the end +print(function() "hello"); +print(function() ''); + +// The following should not crash inside lexer +print((function() '')()); +print((function() "hello")());