8066407: Function with same body not reparsed after SyntaxError

Mon, 27 Apr 2015 12:27:33 +0200

author
hannesw
date
Mon, 27 Apr 2015 12:27:33 +0200
changeset 1336
5ed57fe26f13
parent 1335
411652a014ff
child 1337
248dc4f11e5b

8066407: Function with same body not reparsed after SyntaxError
Reviewed-by: attila, lagergren

src/jdk/nashorn/internal/runtime/regexp/RegExpFactory.java file | annotate | diff | comparison | revisions
test/script/basic/JDK-8066407.js file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/internal/runtime/regexp/RegExpFactory.java	Thu Apr 23 16:05:40 2015 -0700
     1.2 +++ b/src/jdk/nashorn/internal/runtime/regexp/RegExpFactory.java	Mon Apr 27 12:27:33 2015 +0200
     1.3 @@ -26,7 +26,7 @@
     1.4  package jdk.nashorn.internal.runtime.regexp;
     1.5  
     1.6  import java.util.Collections;
     1.7 -import java.util.Set;
     1.8 +import java.util.Map;
     1.9  import java.util.WeakHashMap;
    1.10  import jdk.nashorn.internal.runtime.ParserException;
    1.11  import jdk.nashorn.internal.runtime.options.Options;
    1.12 @@ -45,11 +45,10 @@
    1.13      /** Weak cache of already validated regexps - when reparsing, we don't, for example
    1.14       *  need to recompile (reverify) all regexps that have previously been parsed by this
    1.15       *  RegExpFactory in a previous compilation. This saves significant time in e.g. avatar
    1.16 -     *  startup */
    1.17 -    private static final Set<String> VALID_CACHE_SET =
    1.18 -            Collections.newSetFromMap(
    1.19 -                    Collections.synchronizedMap(
    1.20 -                            new WeakHashMap<String, Boolean>()));
    1.21 +     *  startup
    1.22 +     */
    1.23 +    private static final Map<String, RegExp> REGEXP_CACHE =
    1.24 +            Collections.synchronizedMap(new WeakHashMap<String, RegExp>());
    1.25  
    1.26      static {
    1.27          final String impl = Options.getStringProperty("nashorn.regexp.impl", JONI);
    1.28 @@ -87,7 +86,13 @@
    1.29       * @throws ParserException if invalid source or flags
    1.30       */
    1.31      public static RegExp create(final String pattern, final String flags) {
    1.32 -        return instance.compile(pattern,  flags);
    1.33 +        final String key = pattern + "/" + flags;
    1.34 +        RegExp regexp = REGEXP_CACHE.get(key);
    1.35 +        if (regexp == null) {
    1.36 +            regexp = instance.compile(pattern,  flags);
    1.37 +            REGEXP_CACHE.put(key, regexp);
    1.38 +        }
    1.39 +        return regexp;
    1.40      }
    1.41  
    1.42      /**
    1.43 @@ -98,11 +103,8 @@
    1.44       *
    1.45       * @throws ParserException if invalid source or flags
    1.46       */
    1.47 -    // @SuppressWarnings({"unused"})
    1.48      public static void validate(final String pattern, final String flags) throws ParserException {
    1.49 -        if (VALID_CACHE_SET.add(pattern + flags)) {
    1.50 -            instance.compile(pattern, flags);
    1.51 -        }
    1.52 +        create(pattern, flags);
    1.53      }
    1.54  
    1.55      /**
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/script/basic/JDK-8066407.js	Mon Apr 27 12:27:33 2015 +0200
     2.3 @@ -0,0 +1,48 @@
     2.4 +/*
     2.5 + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + * 
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + * 
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + * 
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + * 
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/**
    2.28 + * JDK-8066407: Function with same body not reparsed after SyntaxError
    2.29 + *
    2.30 + * @test
    2.31 + * @run
    2.32 + */
    2.33 +
    2.34 +function testFunction(body) {
    2.35 +    try {
    2.36 +        Function(body);
    2.37 +        Assert.fail("Should have thrown");
    2.38 +    } catch (e) {
    2.39 +        Assert.assertEquals(e.name, "SyntaxError");
    2.40 +        count++;
    2.41 +    }
    2.42 +
    2.43 +}
    2.44 +
    2.45 +var count = 0;
    2.46 +
    2.47 +testFunction("/a/r");
    2.48 +testFunction("/a/r");
    2.49 +testFunction("/a/r");
    2.50 +
    2.51 +Assert.assertTrue(count === 3);

mercurial