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-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.nashorn.internal.runtime; |
|
27 |
||
28 |
import static jdk.nashorn.internal.codegen.Compiler.SCRIPTS_PACKAGE; |
|
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 | 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 | 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 | 38 |
|
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 | 41 |
*/ |
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 | 45 |
|
36517 | 46 |
private final Module structuresModule; |
47 |
||
16147 | 48 |
/** |
49 |
* Constructor. |
|
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 | 53 |
|
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 | 56 |
|
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 | 60 |
|
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 | 75 |
} |
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 | 102 |
} |
103 |
||
38806
f5be54c0a7c7
8158131: Nashorn should not use jdk.internal.module.Modules API
sundar
parents:
36517
diff
changeset
|
104 |
Module getModule() { |
36517 | 105 |
return structuresModule; |
106 |
} |
|
107 |
||
16147 | 108 |
@Override |
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 | 114 |
} |
115 |
return super.findClass(name); |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* Generate a layout class. |
|
120 |
* @param name Name of class. |
|
121 |
* @param descriptor Layout descriptor. |
|
122 |
* @return Generated class. |
|
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 | 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 | 128 |
return defineClass(name, code, 0, code.length, new ProtectionDomain(null, getPermissions(null))); |
16147 | 129 |
} |
130 |
} |