| 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 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 BoolToInt(is_var) + BoolToInt(is_const) + BoolToInt(is_function)); | 230 BoolToInt(is_var) + BoolToInt(is_const) + BoolToInt(is_function)); |
| 231 | 231 |
| 232 int index; | 232 int index; |
| 233 PropertyAttributes attributes; | 233 PropertyAttributes attributes; |
| 234 BindingFlags binding_flags; | 234 BindingFlags binding_flags; |
| 235 | 235 |
| 236 if ((attr & EVAL_DECLARED) != 0) { | 236 if ((attr & EVAL_DECLARED) != 0) { |
| 237 // Check for a conflict with a lexically scoped variable | 237 // Check for a conflict with a lexically scoped variable |
| 238 context_arg->Lookup(name, LEXICAL_TEST, &index, &attributes, | 238 context_arg->Lookup(name, LEXICAL_TEST, &index, &attributes, |
| 239 &binding_flags); | 239 &binding_flags); |
| 240 if (attributes != ABSENT && | 240 if (attributes != ABSENT && binding_flags == BINDING_CHECK_INITIALIZED) { |
| 241 (binding_flags == MUTABLE_CHECK_INITIALIZED || | |
| 242 binding_flags == IMMUTABLE_CHECK_INITIALIZED || | |
| 243 binding_flags == IMMUTABLE_CHECK_INITIALIZED_HARMONY)) { | |
| 244 return ThrowRedeclarationError(isolate, name); | 241 return ThrowRedeclarationError(isolate, name); |
| 245 } | 242 } |
| 246 attr = static_cast<PropertyAttributes>(attr & ~EVAL_DECLARED); | 243 attr = static_cast<PropertyAttributes>(attr & ~EVAL_DECLARED); |
| 247 } | 244 } |
| 248 | 245 |
| 249 Handle<Object> holder = context->Lookup(name, DONT_FOLLOW_CHAINS, &index, | 246 Handle<Object> holder = context->Lookup(name, DONT_FOLLOW_CHAINS, &index, |
| 250 &attributes, &binding_flags); | 247 &attributes, &binding_flags); |
| 251 if (holder.is_null()) { | 248 if (holder.is_null()) { |
| 252 // In case of JSProxy, an exception might have been thrown. | 249 // In case of JSProxy, an exception might have been thrown. |
| 253 if (isolate->has_pending_exception()) return isolate->heap()->exception(); | 250 if (isolate->has_pending_exception()) return isolate->heap()->exception(); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 CONVERT_ARG_HANDLE_CHECKED(String, name, 0); | 329 CONVERT_ARG_HANDLE_CHECKED(String, name, 0); |
| 333 CONVERT_ARG_HANDLE_CHECKED(Object, initial_value, 1); | 330 CONVERT_ARG_HANDLE_CHECKED(Object, initial_value, 1); |
| 334 CONVERT_ARG_HANDLE_CHECKED(Smi, property_attributes, 2); | 331 CONVERT_ARG_HANDLE_CHECKED(Smi, property_attributes, 2); |
| 335 | 332 |
| 336 PropertyAttributes attributes = | 333 PropertyAttributes attributes = |
| 337 static_cast<PropertyAttributes>(property_attributes->value()); | 334 static_cast<PropertyAttributes>(property_attributes->value()); |
| 338 return DeclareLookupSlot(isolate, name, initial_value, attributes); | 335 return DeclareLookupSlot(isolate, name, initial_value, attributes); |
| 339 } | 336 } |
| 340 | 337 |
| 341 | 338 |
| 342 RUNTIME_FUNCTION(Runtime_InitializeLegacyConstLookupSlot) { | |
| 343 HandleScope scope(isolate); | |
| 344 DCHECK(args.length() == 3); | |
| 345 | |
| 346 CONVERT_ARG_HANDLE_CHECKED(Object, value, 0); | |
| 347 DCHECK(!value->IsTheHole()); | |
| 348 // Initializations are always done in a function or native context. | |
| 349 CONVERT_ARG_HANDLE_CHECKED(Context, context_arg, 1); | |
| 350 Handle<Context> context(context_arg->declaration_context()); | |
| 351 CONVERT_ARG_HANDLE_CHECKED(String, name, 2); | |
| 352 | |
| 353 int index; | |
| 354 PropertyAttributes attributes; | |
| 355 ContextLookupFlags flags = DONT_FOLLOW_CHAINS; | |
| 356 BindingFlags binding_flags; | |
| 357 Handle<Object> holder = | |
| 358 context->Lookup(name, flags, &index, &attributes, &binding_flags); | |
| 359 if (holder.is_null()) { | |
| 360 // In case of JSProxy, an exception might have been thrown. | |
| 361 if (isolate->has_pending_exception()) return isolate->heap()->exception(); | |
| 362 } | |
| 363 | |
| 364 if (index != Context::kNotFound) { | |
| 365 DCHECK(holder->IsContext()); | |
| 366 // Property was found in a context. Perform the assignment if the constant | |
| 367 // was uninitialized. | |
| 368 Handle<Context> context = Handle<Context>::cast(holder); | |
| 369 DCHECK((attributes & READ_ONLY) != 0); | |
| 370 if (context->get(index)->IsTheHole()) context->set(index, *value); | |
| 371 return *value; | |
| 372 } | |
| 373 | |
| 374 PropertyAttributes attr = | |
| 375 static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY); | |
| 376 | |
| 377 // Strict mode handling not needed (legacy const is disallowed in strict | |
| 378 // mode). | |
| 379 | |
| 380 // The declared const was configurable, and may have been deleted in the | |
| 381 // meanwhile. If so, re-introduce the variable in the context extension. | |
| 382 if (attributes == ABSENT) { | |
| 383 Handle<Context> declaration_context(context_arg->declaration_context()); | |
| 384 if (declaration_context->IsScriptContext()) { | |
| 385 holder = handle(declaration_context->global_object(), isolate); | |
| 386 } else { | |
| 387 holder = handle(declaration_context->extension_object(), isolate); | |
| 388 DCHECK(!holder.is_null()); | |
| 389 } | |
| 390 CHECK(holder->IsJSObject()); | |
| 391 } else { | |
| 392 // For JSContextExtensionObjects, the initializer can be run multiple times | |
| 393 // if in a for loop: for (var i = 0; i < 2; i++) { const x = i; }. Only the | |
| 394 // first assignment should go through. For JSGlobalObjects, additionally any | |
| 395 // code can run in between that modifies the declared property. | |
| 396 DCHECK(holder->IsJSGlobalObject() || holder->IsJSContextExtensionObject()); | |
| 397 | |
| 398 LookupIterator it(holder, name, Handle<JSReceiver>::cast(holder), | |
| 399 LookupIterator::HIDDEN_SKIP_INTERCEPTOR); | |
| 400 Maybe<PropertyAttributes> maybe = JSReceiver::GetPropertyAttributes(&it); | |
| 401 if (!maybe.IsJust()) return isolate->heap()->exception(); | |
| 402 PropertyAttributes old_attributes = maybe.FromJust(); | |
| 403 | |
| 404 // Ignore if we can't reconfigure the value. | |
| 405 if ((old_attributes & DONT_DELETE) != 0) { | |
| 406 if ((old_attributes & READ_ONLY) != 0 || | |
| 407 it.state() == LookupIterator::ACCESSOR) { | |
| 408 return *value; | |
| 409 } | |
| 410 attr = static_cast<PropertyAttributes>(old_attributes | READ_ONLY); | |
| 411 } | |
| 412 } | |
| 413 | |
| 414 RETURN_FAILURE_ON_EXCEPTION( | |
| 415 isolate, JSObject::SetOwnPropertyIgnoreAttributes( | |
| 416 Handle<JSObject>::cast(holder), name, value, attr)); | |
| 417 | |
| 418 return *value; | |
| 419 } | |
| 420 | |
| 421 | |
| 422 namespace { | 339 namespace { |
| 423 | 340 |
| 424 // Find the arguments of the JavaScript function invocation that called | 341 // Find the arguments of the JavaScript function invocation that called |
| 425 // into C++ code. Collect these in a newly allocated array of handles. | 342 // into C++ code. Collect these in a newly allocated array of handles. |
| 426 base::SmartArrayPointer<Handle<Object>> GetCallerArguments(Isolate* isolate, | 343 base::SmartArrayPointer<Handle<Object>> GetCallerArguments(Isolate* isolate, |
| 427 int* total_argc) { | 344 int* total_argc) { |
| 428 // Find frame containing arguments passed to the caller. | 345 // Find frame containing arguments passed to the caller. |
| 429 JavaScriptFrameIterator it(isolate); | 346 JavaScriptFrameIterator it(isolate); |
| 430 JavaScriptFrame* frame = it.frame(); | 347 JavaScriptFrame* frame = it.frame(); |
| 431 List<JSFunction*> functions(2); | 348 List<JSFunction*> functions(2); |
| (...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 868 Handle<JSModule> module(context->module()); | 785 Handle<JSModule> module(context->module()); |
| 869 | 786 |
| 870 for (int j = 0; j < description->length(); ++j) { | 787 for (int j = 0; j < description->length(); ++j) { |
| 871 Handle<String> name(description->name(j)); | 788 Handle<String> name(description->name(j)); |
| 872 VariableMode mode = description->mode(j); | 789 VariableMode mode = description->mode(j); |
| 873 int index = description->index(j); | 790 int index = description->index(j); |
| 874 switch (mode) { | 791 switch (mode) { |
| 875 case VAR: | 792 case VAR: |
| 876 case LET: | 793 case LET: |
| 877 case CONST: | 794 case CONST: |
| 878 case CONST_LEGACY: | 795 case CONST_LEGACY: { |
| 879 case IMPORT: { | |
| 880 PropertyAttributes attr = | 796 PropertyAttributes attr = |
| 881 IsImmutableVariableMode(mode) ? FROZEN : SEALED; | 797 IsImmutableVariableMode(mode) ? FROZEN : SEALED; |
| 882 Handle<AccessorInfo> info = | 798 Handle<AccessorInfo> info = |
| 883 Accessors::MakeModuleExport(name, index, attr); | 799 Accessors::MakeModuleExport(name, index, attr); |
| 884 Handle<Object> result = | 800 Handle<Object> result = |
| 885 JSObject::SetAccessor(module, info).ToHandleChecked(); | 801 JSObject::SetAccessor(module, info).ToHandleChecked(); |
| 886 DCHECK(!result->IsUndefined()); | 802 DCHECK(!result->IsUndefined()); |
| 887 USE(result); | 803 USE(result); |
| 888 break; | 804 break; |
| 889 } | 805 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 954 if (isolate->has_pending_exception()) return MaybeHandle<Object>(); | 870 if (isolate->has_pending_exception()) return MaybeHandle<Object>(); |
| 955 | 871 |
| 956 if (index != Context::kNotFound) { | 872 if (index != Context::kNotFound) { |
| 957 DCHECK(holder->IsContext()); | 873 DCHECK(holder->IsContext()); |
| 958 // If the "property" we were looking for is a local variable, the | 874 // If the "property" we were looking for is a local variable, the |
| 959 // receiver is the global object; see ECMA-262, 3rd., 10.1.6 and 10.2.3. | 875 // receiver is the global object; see ECMA-262, 3rd., 10.1.6 and 10.2.3. |
| 960 Handle<Object> receiver = isolate->factory()->undefined_value(); | 876 Handle<Object> receiver = isolate->factory()->undefined_value(); |
| 961 Handle<Object> value = handle(Context::cast(*holder)->get(index), isolate); | 877 Handle<Object> value = handle(Context::cast(*holder)->get(index), isolate); |
| 962 // Check for uninitialized bindings. | 878 // Check for uninitialized bindings. |
| 963 switch (flags) { | 879 switch (flags) { |
| 964 case MUTABLE_CHECK_INITIALIZED: | 880 case BINDING_CHECK_INITIALIZED: |
| 965 case IMMUTABLE_CHECK_INITIALIZED_HARMONY: | |
| 966 if (value->IsTheHole()) { | 881 if (value->IsTheHole()) { |
| 967 THROW_NEW_ERROR(isolate, | 882 THROW_NEW_ERROR(isolate, |
| 968 NewReferenceError(MessageTemplate::kNotDefined, name), | 883 NewReferenceError(MessageTemplate::kNotDefined, name), |
| 969 Object); | 884 Object); |
| 970 } | 885 } |
| 971 // FALLTHROUGH | 886 // FALLTHROUGH |
| 972 case IMMUTABLE_CHECK_INITIALIZED: | 887 case BINDING_IS_INITIALIZED: |
| 973 if (value->IsTheHole()) { | |
| 974 DCHECK(attributes & READ_ONLY); | |
| 975 value = isolate->factory()->undefined_value(); | |
| 976 } | |
| 977 // FALLTHROUGH | |
| 978 case MUTABLE_IS_INITIALIZED: | |
| 979 case IMMUTABLE_IS_INITIALIZED: | |
| 980 case IMMUTABLE_IS_INITIALIZED_HARMONY: | |
| 981 DCHECK(!value->IsTheHole()); | 888 DCHECK(!value->IsTheHole()); |
| 982 if (receiver_return) *receiver_return = receiver; | 889 if (receiver_return) *receiver_return = receiver; |
| 983 return value; | 890 return value; |
| 984 case MISSING_BINDING: | 891 case MISSING_BINDING: |
| 985 break; | 892 break; |
| 986 } | 893 } |
| 987 UNREACHABLE(); | 894 UNREACHABLE(); |
| 988 } | 895 } |
| 989 | 896 |
| 990 // Otherwise, if the slot was found the holder is a context extension | 897 // Otherwise, if the slot was found the holder is a context extension |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1068 BindingFlags flags; | 975 BindingFlags flags; |
| 1069 Handle<Object> holder = | 976 Handle<Object> holder = |
| 1070 context->Lookup(name, FOLLOW_CHAINS, &index, &attributes, &flags); | 977 context->Lookup(name, FOLLOW_CHAINS, &index, &attributes, &flags); |
| 1071 if (holder.is_null()) { | 978 if (holder.is_null()) { |
| 1072 // In case of JSProxy, an exception might have been thrown. | 979 // In case of JSProxy, an exception might have been thrown. |
| 1073 if (isolate->has_pending_exception()) return MaybeHandle<Object>(); | 980 if (isolate->has_pending_exception()) return MaybeHandle<Object>(); |
| 1074 } | 981 } |
| 1075 | 982 |
| 1076 // The property was found in a context slot. | 983 // The property was found in a context slot. |
| 1077 if (index != Context::kNotFound) { | 984 if (index != Context::kNotFound) { |
| 1078 if ((flags == MUTABLE_CHECK_INITIALIZED || | 985 if (flags == BINDING_CHECK_INITIALIZED && |
| 1079 flags == IMMUTABLE_CHECK_INITIALIZED_HARMONY) && | |
| 1080 Handle<Context>::cast(holder)->is_the_hole(index)) { | 986 Handle<Context>::cast(holder)->is_the_hole(index)) { |
| 1081 THROW_NEW_ERROR(isolate, | 987 THROW_NEW_ERROR(isolate, |
| 1082 NewReferenceError(MessageTemplate::kNotDefined, name), | 988 NewReferenceError(MessageTemplate::kNotDefined, name), |
| 1083 Object); | 989 Object); |
| 1084 } | 990 } |
| 1085 if ((attributes & READ_ONLY) == 0) { | 991 if ((attributes & READ_ONLY) == 0) { |
| 1086 Handle<Context>::cast(holder)->set(index, *value); | 992 Handle<Context>::cast(holder)->set(index, *value); |
| 1087 } else if (is_strict(language_mode)) { | 993 } else if (is_strict(language_mode)) { |
| 1088 // Setting read only property in strict mode. | 994 // Setting read only property in strict mode. |
| 1089 THROW_NEW_ERROR(isolate, | 995 THROW_NEW_ERROR(isolate, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1134 DCHECK_EQ(2, args.length()); | 1040 DCHECK_EQ(2, args.length()); |
| 1135 CONVERT_ARG_HANDLE_CHECKED(String, name, 0); | 1041 CONVERT_ARG_HANDLE_CHECKED(String, name, 0); |
| 1136 CONVERT_ARG_HANDLE_CHECKED(Object, value, 1); | 1042 CONVERT_ARG_HANDLE_CHECKED(Object, value, 1); |
| 1137 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, value, | 1043 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, value, |
| 1138 StoreLookupSlot(name, value, STRICT)); | 1044 StoreLookupSlot(name, value, STRICT)); |
| 1139 return *value; | 1045 return *value; |
| 1140 } | 1046 } |
| 1141 | 1047 |
| 1142 } // namespace internal | 1048 } // namespace internal |
| 1143 } // namespace v8 | 1049 } // namespace v8 |
| OLD | NEW |