58 static PropertyMap getInitialMap() {
59 return map$;
60 }
61
62 static PropertyMap getInitialAnonymousMap() {
63 return AnonymousFunction.getInitialMap();
64 }
65
66 static PropertyMap getInitialStrictMap() {
67 return strictmodemap$;
68 }
69
70 static PropertyMap getInitialBoundMap() {
71 return boundfunctionmap$;
72 }
73
74 // Marker object for lazily initialized prototype object
75 private static final Object LAZY_PROTOTYPE = new Object();
76
77 private ScriptFunctionImpl(final String name, final MethodHandle invokeHandle, final MethodHandle[] specs, final Global global) {
78 super(name, invokeHandle, getInitialMap(), null, specs, false, true, true);
79 init(global);
80 }
81
82 /**
83 * Constructor called by Nasgen generated code, no membercount, use the default map.
84 * Creates builtin functions only.
85 *
86 * @param name name of function
87 * @param invokeHandle handle for invocation
88 * @param specs specialized versions of this method, if available, null otherwise
89 */
90 ScriptFunctionImpl(final String name, final MethodHandle invokeHandle, final MethodHandle[] specs) {
91 this(name, invokeHandle, specs, Global.instance());
92 }
93
94 private ScriptFunctionImpl(final String name, final MethodHandle invokeHandle, final PropertyMap map, final MethodHandle[] specs, final Global global) {
95 super(name, invokeHandle, map.addAll(getInitialMap()), null, specs, false, true, true);
96 init(global);
97 }
98
99 /**
100 * Constructor called by Nasgen generated code, no membercount, use the map passed as argument.
101 * Creates builtin functions only.
102 *
103 * @param name name of function
104 * @param invokeHandle handle for invocation
105 * @param map initial property map
106 * @param specs specialized versions of this method, if available, null otherwise
107 */
108 ScriptFunctionImpl(final String name, final MethodHandle invokeHandle, final PropertyMap map, final MethodHandle[] specs) {
109 this(name, invokeHandle, map, specs, Global.instance());
110 }
111
112 private ScriptFunctionImpl(final String name, final MethodHandle methodHandle, final ScriptObject scope, final MethodHandle[] specs, final boolean isStrict, final boolean isBuiltin, final boolean isConstructor, final Global global) {
113 super(name, methodHandle, getMap(global, isStrict), scope, specs, isStrict, isBuiltin, isConstructor);
114 init(global);
115 }
116
117 /**
118 * Constructor called by Global.newScriptFunction (runtime).
119 *
120 * @param name name of function
121 * @param methodHandle handle for invocation
122 * @param scope scope object
123 * @param specs specialized versions of this method, if available, null otherwise
124 * @param isStrict are we in strict mode
125 * @param isBuiltin is this a built-in function
126 * @param isConstructor can the function be used as a constructor (most can; some built-ins are restricted).
127 */
128 ScriptFunctionImpl(final String name, final MethodHandle methodHandle, final ScriptObject scope, final MethodHandle[] specs, final boolean isStrict, final boolean isBuiltin, final boolean isConstructor) {
129 this(name, methodHandle, scope, specs, isStrict, isBuiltin, isConstructor, Global.instance());
130 }
131
132 private ScriptFunctionImpl(final RecompilableScriptFunctionData data, final ScriptObject scope, final Global global) {
133 super(data, getMap(global, data.isStrict()), scope);
134 init(global);
135 }
136
137 /**
138 * Constructor called by (compiler) generated code for {@link ScriptObject}s.
139 *
140 * @param data static function data
141 * @param scope scope object
142 */
143 public ScriptFunctionImpl(final RecompilableScriptFunctionData data, final ScriptObject scope) {
144 this(data, scope, Global.instance());
145 }
146
147 /**
148 * Only invoked internally from {@link BoundScriptFunctionImpl} constructor.
149 * @param data the script function data for the bound function.
156
157 static {
158 final ArrayList<Property> properties = new ArrayList<>(3);
159 properties.add(AccessorProperty.create("prototype", Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE, G$PROTOTYPE, S$PROTOTYPE));
160 properties.add(AccessorProperty.create("length", Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE | Property.NOT_WRITABLE, G$LENGTH, null));
161 properties.add(AccessorProperty.create("name", Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE | Property.NOT_WRITABLE, G$NAME, null));
162 map$ = PropertyMap.newMap(properties);
163 strictmodemap$ = createStrictModeMap(map$);
164 boundfunctionmap$ = createBoundFunctionMap(strictmodemap$);
165 }
166
167 private static PropertyMap createStrictModeMap(final PropertyMap map) {
168 final int flags = Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE;
169 PropertyMap newMap = map;
170 // Need to add properties directly to map since slots are assigned speculatively by newUserAccessors.
171 newMap = newMap.addPropertyNoHistory(map.newUserAccessors("arguments", flags));
172 newMap = newMap.addPropertyNoHistory(map.newUserAccessors("caller", flags));
173 return newMap;
174 }
175
176 // Choose the map based on strict mode!
177 private static PropertyMap getMap(final Global global, final boolean strict) {
178 return strict ? getInitialStrictMap() : getInitialMap();
179 }
180
181 private static PropertyMap createBoundFunctionMap(final PropertyMap strictModeMap) {
182 // Bound function map is same as strict function map, but additionally lacks the "prototype" property, see
183 // ECMAScript 5.1 section 15.3.4.5
184 return strictModeMap.deleteProperty(strictModeMap.findProperty("prototype"));
185 }
186
187 // Instance of this class is used as global anonymous function which
188 // serves as Function.prototype object.
189 private static class AnonymousFunction extends ScriptFunctionImpl {
190 private static final PropertyMap anonmap$ = PropertyMap.newMap();
191
192 static PropertyMap getInitialMap() {
193 return anonmap$;
194 }
195
196 AnonymousFunction(final Global global) {
197 super("", GlobalFunctions.ANONYMOUS, getInitialAnonymousMap(), null);
198 }
199 }
200
201 static ScriptFunctionImpl newAnonymousFunction(final Global global) {
202 return new AnonymousFunction(global);
203 }
204
205 /**
206 * Factory method for non-constructor built-in functions
207 *
208 * @param name function name
209 * @param methodHandle handle for invocation
210 * @param specs specialized versions of function if available, null otherwise
211 * @return new ScriptFunction
212 */
213 static ScriptFunction makeFunction(final String name, final MethodHandle methodHandle, final MethodHandle[] specs) {
214 final ScriptFunctionImpl func = new ScriptFunctionImpl(name, methodHandle, null, specs, false, true, false);
215 func.setPrototype(UNDEFINED);
216 // Non-constructor built-in functions do not have "prototype" property
217 func.deleteOwnProperty(func.getMap().findProperty("prototype"));
218
219 return func;
220 }
221
222 /**
223 * Factory method for non-constructor built-in functions
224 *
225 * @param name function name
226 * @param methodHandle handle for invocation
227 * @return new ScriptFunction
228 */
229 static ScriptFunction makeFunction(final String name, final MethodHandle methodHandle) {
230 return makeFunction(name, methodHandle, null);
231 }
232
233 @Override
234 public ScriptFunction makeSynchronizedFunction(final Object sync) {
|
58 static PropertyMap getInitialMap() {
59 return map$;
60 }
61
62 static PropertyMap getInitialAnonymousMap() {
63 return AnonymousFunction.getInitialMap();
64 }
65
66 static PropertyMap getInitialStrictMap() {
67 return strictmodemap$;
68 }
69
70 static PropertyMap getInitialBoundMap() {
71 return boundfunctionmap$;
72 }
73
74 // Marker object for lazily initialized prototype object
75 private static final Object LAZY_PROTOTYPE = new Object();
76
77 private ScriptFunctionImpl(final String name, final MethodHandle invokeHandle, final MethodHandle[] specs, final Global global) {
78 super(name, invokeHandle, getInitialMap(), null, specs, ScriptFunctionData.IS_BUILTIN_CONSTRUCTOR);
79 init(global);
80 }
81
82 /**
83 * Constructor called by Nasgen generated code, no membercount, use the default map.
84 * Creates builtin functions only.
85 *
86 * @param name name of function
87 * @param invokeHandle handle for invocation
88 * @param specs specialized versions of this method, if available, null otherwise
89 */
90 ScriptFunctionImpl(final String name, final MethodHandle invokeHandle, final MethodHandle[] specs) {
91 this(name, invokeHandle, specs, Global.instance());
92 }
93
94 private ScriptFunctionImpl(final String name, final MethodHandle invokeHandle, final PropertyMap map, final MethodHandle[] specs, final Global global) {
95 super(name, invokeHandle, map.addAll(getInitialMap()), null, specs, ScriptFunctionData.IS_BUILTIN_CONSTRUCTOR);
96 init(global);
97 }
98
99 /**
100 * Constructor called by Nasgen generated code, no membercount, use the map passed as argument.
101 * Creates builtin functions only.
102 *
103 * @param name name of function
104 * @param invokeHandle handle for invocation
105 * @param map initial property map
106 * @param specs specialized versions of this method, if available, null otherwise
107 */
108 ScriptFunctionImpl(final String name, final MethodHandle invokeHandle, final PropertyMap map, final MethodHandle[] specs) {
109 this(name, invokeHandle, map, specs, Global.instance());
110 }
111
112 private ScriptFunctionImpl(final String name, final MethodHandle methodHandle, final ScriptObject scope, final MethodHandle[] specs, final int flags, final Global global) {
113 super(name, methodHandle, getMap(global, isStrict(flags)), scope, specs, flags);
114 init(global);
115 }
116
117 /**
118 * Constructor called by Global.newScriptFunction (runtime).
119 *
120 * @param name name of function
121 * @param methodHandle handle for invocation
122 * @param scope scope object
123 * @param specs specialized versions of this method, if available, null otherwise
124 * @param flags {@link ScriptFunctionData} flags
125 */
126 ScriptFunctionImpl(final String name, final MethodHandle methodHandle, final ScriptObject scope, final MethodHandle[] specs, final int flags) {
127 this(name, methodHandle, scope, specs, flags, Global.instance());
128 }
129
130 private ScriptFunctionImpl(final RecompilableScriptFunctionData data, final ScriptObject scope, final Global global) {
131 super(data, getMap(global, data.isStrict()), scope);
132 init(global);
133 }
134
135 /**
136 * Constructor called by (compiler) generated code for {@link ScriptObject}s.
137 *
138 * @param data static function data
139 * @param scope scope object
140 */
141 public ScriptFunctionImpl(final RecompilableScriptFunctionData data, final ScriptObject scope) {
142 this(data, scope, Global.instance());
143 }
144
145 /**
146 * Only invoked internally from {@link BoundScriptFunctionImpl} constructor.
147 * @param data the script function data for the bound function.
154
155 static {
156 final ArrayList<Property> properties = new ArrayList<>(3);
157 properties.add(AccessorProperty.create("prototype", Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE, G$PROTOTYPE, S$PROTOTYPE));
158 properties.add(AccessorProperty.create("length", Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE | Property.NOT_WRITABLE, G$LENGTH, null));
159 properties.add(AccessorProperty.create("name", Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE | Property.NOT_WRITABLE, G$NAME, null));
160 map$ = PropertyMap.newMap(properties);
161 strictmodemap$ = createStrictModeMap(map$);
162 boundfunctionmap$ = createBoundFunctionMap(strictmodemap$);
163 }
164
165 private static PropertyMap createStrictModeMap(final PropertyMap map) {
166 final int flags = Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE;
167 PropertyMap newMap = map;
168 // Need to add properties directly to map since slots are assigned speculatively by newUserAccessors.
169 newMap = newMap.addPropertyNoHistory(map.newUserAccessors("arguments", flags));
170 newMap = newMap.addPropertyNoHistory(map.newUserAccessors("caller", flags));
171 return newMap;
172 }
173
174 private static boolean isStrict(final int flags) {
175 return (flags & ScriptFunctionData.IS_STRICT) != 0;
176 }
177
178 // Choose the map based on strict mode!
179 private static PropertyMap getMap(final Global global, final boolean strict) {
180 return strict ? getInitialStrictMap() : getInitialMap();
181 }
182
183 private static PropertyMap createBoundFunctionMap(final PropertyMap strictModeMap) {
184 // Bound function map is same as strict function map, but additionally lacks the "prototype" property, see
185 // ECMAScript 5.1 section 15.3.4.5
186 return strictModeMap.deleteProperty(strictModeMap.findProperty("prototype"));
187 }
188
189 // Instance of this class is used as global anonymous function which
190 // serves as Function.prototype object.
191 private static class AnonymousFunction extends ScriptFunctionImpl {
192 private static final PropertyMap anonmap$ = PropertyMap.newMap();
193
194 static PropertyMap getInitialMap() {
195 return anonmap$;
196 }
197
198 AnonymousFunction(final Global global) {
199 super("", GlobalFunctions.ANONYMOUS, getInitialAnonymousMap(), null);
200 }
201 }
202
203 static ScriptFunctionImpl newAnonymousFunction(final Global global) {
204 return new AnonymousFunction(global);
205 }
206
207 /**
208 * Factory method for non-constructor built-in functions
209 *
210 * @param name function name
211 * @param methodHandle handle for invocation
212 * @param specs specialized versions of function if available, null otherwise
213 * @return new ScriptFunction
214 */
215 static ScriptFunction makeFunction(final String name, final MethodHandle methodHandle, final MethodHandle[] specs) {
216 final ScriptFunctionImpl func = new ScriptFunctionImpl(name, methodHandle, null, specs, ScriptFunctionData.IS_BUILTIN);
217 func.setPrototype(UNDEFINED);
218 // Non-constructor built-in functions do not have "prototype" property
219 func.deleteOwnProperty(func.getMap().findProperty("prototype"));
220
221 return func;
222 }
223
224 /**
225 * Factory method for non-constructor built-in functions
226 *
227 * @param name function name
228 * @param methodHandle handle for invocation
229 * @return new ScriptFunction
230 */
231 static ScriptFunction makeFunction(final String name, final MethodHandle methodHandle) {
232 return makeFunction(name, methodHandle, null);
233 }
234
235 @Override
236 public ScriptFunction makeSynchronizedFunction(final Object sync) {
|