OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/runtime/runtime-utils.h" | 5 #include "src/runtime/runtime-utils.h" |
6 | 6 |
7 #include "src/accessors.h" | 7 #include "src/accessors.h" |
8 #include "src/arguments.h" | 8 #include "src/arguments.h" |
9 #include "src/ast/scopeinfo.h" | 9 #include "src/ast/scopeinfo.h" |
10 #include "src/ast/scopes.h" | 10 #include "src/ast/scopes.h" |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 attr = static_cast<PropertyAttributes>(old_attributes | READ_ONLY); | 181 attr = static_cast<PropertyAttributes>(old_attributes | READ_ONLY); |
182 } | 182 } |
183 } | 183 } |
184 | 184 |
185 RETURN_FAILURE_ON_EXCEPTION( | 185 RETURN_FAILURE_ON_EXCEPTION( |
186 isolate, JSObject::DefineOwnPropertyIgnoreAttributes(&it, value, attr)); | 186 isolate, JSObject::DefineOwnPropertyIgnoreAttributes(&it, value, attr)); |
187 | 187 |
188 return *value; | 188 return *value; |
189 } | 189 } |
190 | 190 |
191 | |
192 namespace { | 191 namespace { |
193 | 192 |
194 Object* DeclareLookupSlot(Isolate* isolate, Handle<String> name, | 193 Object* DeclareEvalHelper(Isolate* isolate, Handle<String> name, |
195 Handle<Object> initial_value, | 194 Handle<Object> value) { |
196 PropertyAttributes attr) { | 195 // Declarations are always made in a function, native, or script context, or |
197 // Declarations are always made in a function, eval or script context, or | 196 // a declaration block scope. Since this is called from eval, the context |
198 // a declaration block scope. | 197 // passed is the context of the caller, which may be some nested context and |
199 // In the case of eval code, the context passed is the context of the caller, | 198 // not the declaration context. |
200 // which may be some nested context and not the declaration context. | |
201 Handle<Context> context_arg(isolate->context(), isolate); | 199 Handle<Context> context_arg(isolate->context(), isolate); |
202 Handle<Context> context(context_arg->declaration_context(), isolate); | 200 Handle<Context> context(context_arg->declaration_context(), isolate); |
203 | 201 |
204 // TODO(verwaest): Unify the encoding indicating "var" with DeclareGlobals. | 202 DCHECK(context->IsFunctionContext() || context->IsNativeContext() || |
205 bool is_var = *initial_value == NULL; | 203 context->IsScriptContext() || |
206 bool is_function = initial_value->IsJSFunction(); | 204 (context->IsBlockContext() && context->has_extension())); |
207 DCHECK_EQ(1, BoolToInt(is_var) + BoolToInt(is_function)); | 205 |
| 206 bool is_function = value->IsJSFunction(); |
| 207 bool is_var = !is_function; |
| 208 DCHECK(!is_var || value->IsUndefined(isolate)); |
208 | 209 |
209 int index; | 210 int index; |
210 PropertyAttributes attributes; | 211 PropertyAttributes attributes; |
211 BindingFlags binding_flags; | 212 BindingFlags binding_flags; |
212 | 213 |
213 if ((attr & EVAL_DECLARED) != 0) { | 214 // Check for a conflict with a lexically scoped variable |
214 // Check for a conflict with a lexically scoped variable | 215 context_arg->Lookup(name, LEXICAL_TEST, &index, &attributes, &binding_flags); |
215 context_arg->Lookup(name, LEXICAL_TEST, &index, &attributes, | 216 if (attributes != ABSENT && binding_flags == BINDING_CHECK_INITIALIZED) { |
216 &binding_flags); | 217 return ThrowRedeclarationError(isolate, name); |
217 if (attributes != ABSENT && binding_flags == BINDING_CHECK_INITIALIZED) { | |
218 return ThrowRedeclarationError(isolate, name); | |
219 } | |
220 attr = static_cast<PropertyAttributes>(attr & ~EVAL_DECLARED); | |
221 } | 218 } |
222 | 219 |
223 Handle<Object> holder = context->Lookup(name, DONT_FOLLOW_CHAINS, &index, | 220 Handle<Object> holder = context->Lookup(name, DONT_FOLLOW_CHAINS, &index, |
224 &attributes, &binding_flags); | 221 &attributes, &binding_flags); |
225 if (holder.is_null()) { | 222 DCHECK(!isolate->has_pending_exception()); |
226 // In case of JSProxy, an exception might have been thrown. | |
227 if (isolate->has_pending_exception()) return isolate->heap()->exception(); | |
228 } | |
229 | 223 |
230 Handle<JSObject> object; | 224 Handle<JSObject> object; |
231 Handle<Object> value = | |
232 is_function ? initial_value | |
233 : Handle<Object>::cast(isolate->factory()->undefined_value()); | |
234 | 225 |
235 // TODO(verwaest): This case should probably not be covered by this function, | |
236 // but by DeclareGlobals instead. | |
237 if (attributes != ABSENT && holder->IsJSGlobalObject()) { | 226 if (attributes != ABSENT && holder->IsJSGlobalObject()) { |
238 return DeclareGlobals(isolate, Handle<JSGlobalObject>::cast(holder), name, | 227 return DeclareGlobals(isolate, Handle<JSGlobalObject>::cast(holder), name, |
239 value, attr, is_var, is_function); | 228 value, NONE, is_var, is_function); |
240 } | 229 } |
241 if (context_arg->extension()->IsJSGlobalObject()) { | 230 if (context_arg->extension()->IsJSGlobalObject()) { |
242 Handle<JSGlobalObject> global( | 231 Handle<JSGlobalObject> global( |
243 JSGlobalObject::cast(context_arg->extension()), isolate); | 232 JSGlobalObject::cast(context_arg->extension()), isolate); |
244 return DeclareGlobals(isolate, global, name, value, attr, is_var, | 233 return DeclareGlobals(isolate, global, name, value, NONE, is_var, |
245 is_function); | 234 is_function); |
246 } else if (context->IsScriptContext()) { | 235 } else if (context->IsScriptContext()) { |
247 DCHECK(context->global_object()->IsJSGlobalObject()); | 236 DCHECK(context->global_object()->IsJSGlobalObject()); |
248 Handle<JSGlobalObject> global( | 237 Handle<JSGlobalObject> global( |
249 JSGlobalObject::cast(context->global_object()), isolate); | 238 JSGlobalObject::cast(context->global_object()), isolate); |
250 return DeclareGlobals(isolate, global, name, value, attr, is_var, | 239 return DeclareGlobals(isolate, global, name, value, NONE, is_var, |
251 is_function); | 240 is_function); |
252 } | 241 } |
253 | 242 |
254 if (attributes != ABSENT) { | 243 if (attributes != ABSENT) { |
255 // The name was declared before; check for conflicting re-declarations. | 244 DCHECK_EQ(NONE, attributes); |
256 if ((attributes & READ_ONLY) != 0) { | |
257 return ThrowRedeclarationError(isolate, name); | |
258 } | |
259 | 245 |
260 // Skip var re-declarations. | 246 // Skip var re-declarations. |
261 if (is_var) return isolate->heap()->undefined_value(); | 247 if (is_var) return isolate->heap()->undefined_value(); |
262 | 248 |
263 DCHECK(is_function); | 249 DCHECK(is_function); |
264 if (index != Context::kNotFound) { | 250 if (index != Context::kNotFound) { |
265 DCHECK(holder.is_identical_to(context)); | 251 DCHECK(holder.is_identical_to(context)); |
266 context->set(index, *initial_value); | 252 context->set(index, *value); |
267 return isolate->heap()->undefined_value(); | 253 return isolate->heap()->undefined_value(); |
268 } | 254 } |
269 | 255 |
270 object = Handle<JSObject>::cast(holder); | 256 object = Handle<JSObject>::cast(holder); |
271 | 257 |
272 } else if (context->has_extension()) { | 258 } else if (context->has_extension()) { |
273 // Sloppy varblock contexts might not have an extension object yet, | 259 // Sloppy varblock contexts might not have an extension object yet, |
274 // in which case their extension is a ScopeInfo. | 260 // in which case their extension is a ScopeInfo. |
275 if (context->extension()->IsScopeInfo()) { | 261 if (context->extension()->IsScopeInfo()) { |
276 DCHECK(context->IsBlockContext()); | 262 DCHECK(context->IsBlockContext()); |
277 object = isolate->factory()->NewJSObject( | 263 object = isolate->factory()->NewJSObject( |
278 isolate->context_extension_function()); | 264 isolate->context_extension_function()); |
279 Handle<HeapObject> extension = | 265 Handle<HeapObject> extension = |
280 isolate->factory()->NewSloppyBlockWithEvalContextExtension( | 266 isolate->factory()->NewSloppyBlockWithEvalContextExtension( |
281 handle(context->scope_info()), object); | 267 handle(context->scope_info()), object); |
282 context->set_extension(*extension); | 268 context->set_extension(*extension); |
283 } else { | 269 } else { |
284 object = handle(context->extension_object(), isolate); | 270 object = handle(context->extension_object(), isolate); |
285 } | 271 } |
286 DCHECK(object->IsJSContextExtensionObject() || object->IsJSGlobalObject()); | 272 DCHECK(object->IsJSContextExtensionObject() || object->IsJSGlobalObject()); |
287 } else { | 273 } else { |
288 DCHECK(context->IsFunctionContext()); | 274 DCHECK(context->IsFunctionContext()); |
289 object = | 275 object = |
290 isolate->factory()->NewJSObject(isolate->context_extension_function()); | 276 isolate->factory()->NewJSObject(isolate->context_extension_function()); |
291 context->set_extension(*object); | 277 context->set_extension(*object); |
292 } | 278 } |
293 | 279 |
294 RETURN_FAILURE_ON_EXCEPTION(isolate, JSObject::SetOwnPropertyIgnoreAttributes( | 280 RETURN_FAILURE_ON_EXCEPTION(isolate, JSObject::SetOwnPropertyIgnoreAttributes( |
295 object, name, value, attr)); | 281 object, name, value, NONE)); |
296 | 282 |
297 return isolate->heap()->undefined_value(); | 283 return isolate->heap()->undefined_value(); |
298 } | 284 } |
299 | 285 |
300 } // namespace | 286 } // namespace |
301 | 287 |
302 | 288 RUNTIME_FUNCTION(Runtime_DeclareEvalFunction) { |
303 RUNTIME_FUNCTION(Runtime_DeclareLookupSlot) { | |
304 HandleScope scope(isolate); | 289 HandleScope scope(isolate); |
305 DCHECK_EQ(3, args.length()); | 290 DCHECK_EQ(2, args.length()); |
306 CONVERT_ARG_HANDLE_CHECKED(String, name, 0); | 291 CONVERT_ARG_HANDLE_CHECKED(String, name, 0); |
307 CONVERT_ARG_HANDLE_CHECKED(Object, initial_value, 1); | 292 CONVERT_ARG_HANDLE_CHECKED(Object, value, 1); |
308 CONVERT_ARG_HANDLE_CHECKED(Smi, property_attributes, 2); | 293 return DeclareEvalHelper(isolate, name, value); |
309 | |
310 PropertyAttributes attributes = | |
311 static_cast<PropertyAttributes>(property_attributes->value()); | |
312 return DeclareLookupSlot(isolate, name, initial_value, attributes); | |
313 } | 294 } |
314 | 295 |
| 296 RUNTIME_FUNCTION(Runtime_DeclareEvalVar) { |
| 297 HandleScope scope(isolate); |
| 298 DCHECK_EQ(1, args.length()); |
| 299 CONVERT_ARG_HANDLE_CHECKED(String, name, 0); |
| 300 return DeclareEvalHelper(isolate, name, |
| 301 isolate->factory()->undefined_value()); |
| 302 } |
315 | 303 |
316 namespace { | 304 namespace { |
317 | 305 |
318 // Find the arguments of the JavaScript function invocation that called | 306 // Find the arguments of the JavaScript function invocation that called |
319 // into C++ code. Collect these in a newly allocated array of handles. | 307 // into C++ code. Collect these in a newly allocated array of handles. |
320 base::SmartArrayPointer<Handle<Object>> GetCallerArguments(Isolate* isolate, | 308 base::SmartArrayPointer<Handle<Object>> GetCallerArguments(Isolate* isolate, |
321 int* total_argc) { | 309 int* total_argc) { |
322 // Find frame containing arguments passed to the caller. | 310 // Find frame containing arguments passed to the caller. |
323 JavaScriptFrameIterator it(isolate); | 311 JavaScriptFrameIterator it(isolate); |
324 JavaScriptFrame* frame = it.frame(); | 312 JavaScriptFrame* frame = it.frame(); |
(...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1002 RUNTIME_FUNCTION(Runtime_StoreLookupSlot_Strict) { | 990 RUNTIME_FUNCTION(Runtime_StoreLookupSlot_Strict) { |
1003 HandleScope scope(isolate); | 991 HandleScope scope(isolate); |
1004 DCHECK_EQ(2, args.length()); | 992 DCHECK_EQ(2, args.length()); |
1005 CONVERT_ARG_HANDLE_CHECKED(String, name, 0); | 993 CONVERT_ARG_HANDLE_CHECKED(String, name, 0); |
1006 CONVERT_ARG_HANDLE_CHECKED(Object, value, 1); | 994 CONVERT_ARG_HANDLE_CHECKED(Object, value, 1); |
1007 RETURN_RESULT_OR_FAILURE(isolate, StoreLookupSlot(name, value, STRICT)); | 995 RETURN_RESULT_OR_FAILURE(isolate, StoreLookupSlot(name, value, STRICT)); |
1008 } | 996 } |
1009 | 997 |
1010 } // namespace internal | 998 } // namespace internal |
1011 } // namespace v8 | 999 } // namespace v8 |
OLD | NEW |