src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/StructureLoader.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 44555 nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/StructureLoader.java@3a7475e3da26
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.internal.runtime;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
import static jdk.nashorn.internal.codegen.Compiler.SCRIPTS_PACKAGE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
import static jdk.nashorn.internal.codegen.Compiler.binaryName;
29834
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    30
import static jdk.nashorn.internal.codegen.CompilerConstants.JS_OBJECT_DUAL_FIELD_PREFIX;
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    31
import static jdk.nashorn.internal.codegen.CompilerConstants.JS_OBJECT_SINGLE_FIELD_PREFIX;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
38806
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    33
import java.lang.module.ModuleDescriptor;
43741
818fdb79ae11 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42380
diff changeset
    34
import java.lang.module.ModuleDescriptor.Modifier;
16522
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16240
diff changeset
    35
import java.security.ProtectionDomain;
43741
818fdb79ae11 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42380
diff changeset
    36
import java.util.Set;
16240
e1468b33e201 8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents: 16234
diff changeset
    37
import jdk.nashorn.internal.codegen.ObjectClassGenerator;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
/**
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 17513
diff changeset
    40
 * Responsible for on the fly construction of structure classes.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
final class StructureLoader extends NashornLoader {
29834
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    43
    private static final String SINGLE_FIELD_PREFIX = binaryName(SCRIPTS_PACKAGE) + '.' + JS_OBJECT_SINGLE_FIELD_PREFIX.symbolName();
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    44
    private static final String DUAL_FIELD_PREFIX   = binaryName(SCRIPTS_PACKAGE) + '.' + JS_OBJECT_DUAL_FIELD_PREFIX.symbolName();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
36517
41a1c20eb619 8142968: Module System implementation
alanb
parents: 29834
diff changeset
    46
    private final Module structuresModule;
41a1c20eb619 8142968: Module System implementation
alanb
parents: 29834
diff changeset
    47
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
     * Constructor.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
     */
19895
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 18851
diff changeset
    51
    StructureLoader(final ClassLoader parent) {
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 18851
diff changeset
    52
        super(parent);
36517
41a1c20eb619 8142968: Module System implementation
alanb
parents: 29834
diff changeset
    53
41a1c20eb619 8142968: Module System implementation
alanb
parents: 29834
diff changeset
    54
        // new structures module, it's exports, read edges
38806
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    55
        structuresModule = createModule("jdk.scripting.nashorn.structures");
36517
41a1c20eb619 8142968: Module System implementation
alanb
parents: 29834
diff changeset
    56
41a1c20eb619 8142968: Module System implementation
alanb
parents: 29834
diff changeset
    57
        // specific exports from nashorn to the structures module
38806
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    58
        NASHORN_MODULE.addExports(SCRIPTS_PKG, structuresModule);
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    59
        NASHORN_MODULE.addExports(RUNTIME_PKG, structuresModule);
36517
41a1c20eb619 8142968: Module System implementation
alanb
parents: 29834
diff changeset
    60
41a1c20eb619 8142968: Module System implementation
alanb
parents: 29834
diff changeset
    61
        // nashorn has to read fields from classes of the new module
38806
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    62
        NASHORN_MODULE.addReads(structuresModule);
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    63
    }
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    64
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    65
    private Module createModule(final String moduleName) {
43741
818fdb79ae11 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42380
diff changeset
    66
        final ModuleDescriptor descriptor =
818fdb79ae11 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42380
diff changeset
    67
            ModuleDescriptor.newModule(moduleName, Set.of(Modifier.SYNTHETIC))
818fdb79ae11 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42380
diff changeset
    68
                            .requires(NASHORN_MODULE.getName())
818fdb79ae11 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42380
diff changeset
    69
                            .packages(Set.of(SCRIPTS_PKG))
818fdb79ae11 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42380
diff changeset
    70
                            .build();
38806
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    71
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    72
        final Module mod = Context.createModuleTrusted(descriptor, this);
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    73
        loadModuleManipulator();
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
    74
        return mod;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
29834
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    77
    /**
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    78
     * Returns true if the class name represents a structure object with dual primitive/object fields.
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    79
     * @param name a class name
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    80
     * @return true if a dual field structure class
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    81
     */
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    82
    private static boolean isDualFieldStructure(final String name) {
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    83
        return name.startsWith(DUAL_FIELD_PREFIX);
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    84
    }
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    85
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    86
    /**
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    87
     * Returns true if the class name represents a structure object with single object-only fields.
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    88
     * @param name a class name
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    89
     * @return true if a single field structure class
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    90
     */
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    91
    static boolean isSingleFieldStructure(final String name) {
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    92
        return name.startsWith(SINGLE_FIELD_PREFIX);
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    93
    }
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    94
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    95
    /**
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    96
     * Returns true if the class name represents a Nashorn structure object.
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    97
     * @param name a class name
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    98
     * @return true if a structure class
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
    99
     */
19895
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 18851
diff changeset
   100
    static boolean isStructureClass(final String name) {
29834
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
   101
        return isDualFieldStructure(name) || isSingleFieldStructure(name);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
38806
f5be54c0a7c7 8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents: 36517
diff changeset
   104
    Module getModule() {
36517
41a1c20eb619 8142968: Module System implementation
alanb
parents: 29834
diff changeset
   105
        return structuresModule;
41a1c20eb619 8142968: Module System implementation
alanb
parents: 29834
diff changeset
   106
    }
41a1c20eb619 8142968: Module System implementation
alanb
parents: 29834
diff changeset
   107
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
    protected Class<?> findClass(final String name) throws ClassNotFoundException {
29834
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
   110
        if (isDualFieldStructure(name)) {
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
   111
            return generateClass(name, name.substring(DUAL_FIELD_PREFIX.length()), true);
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
   112
        } else if (isSingleFieldStructure(name)) {
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
   113
            return generateClass(name, name.substring(SINGLE_FIELD_PREFIX.length()), false);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   114
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
        return super.findClass(name);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
     * Generate a layout class.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
     * @param name       Name of class.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
     * @param descriptor Layout descriptor.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
     * @return Generated class.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   123
     */
29834
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
   124
    private Class<?> generateClass(final String name, final String descriptor, final boolean dualFields) {
19895
965b12eb322e 8024619: JDBC java.sql.DriverManager is not usable from JS script
sundar
parents: 18851
diff changeset
   125
        final Context context = Context.getContextTrusted();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
29834
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
   127
        final byte[] code = new ObjectClassGenerator(context, dualFields).generate(descriptor);
16522
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16240
diff changeset
   128
        return defineClass(name, code, 0, code.length, new ProtectionDomain(null, getPermissions(null)));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
}