7 #include "src/accessors.h" 8 #include "src/arguments-inl.h" 9 #include "src/ast/scopes.h" 10 #include "src/bootstrapper.h" 11 #include "src/counters.h" 12 #include "src/deoptimizer.h" 13 #include "src/frames-inl.h" 14 #include "src/isolate-inl.h" 15 #include "src/message-template.h" 16 #include "src/objects/heap-object-inl.h" 17 #include "src/objects/module-inl.h" 18 #include "src/objects/smi.h" 19 #include "src/runtime/runtime-utils.h" 24 RUNTIME_FUNCTION(Runtime_ThrowConstAssignError) {
25 HandleScope scope(isolate);
26 THROW_NEW_ERROR_RETURN_FAILURE(isolate,
27 NewTypeError(MessageTemplate::kConstAssign));
32 enum class RedeclarationType { kSyntaxError = 0, kTypeError = 1 };
34 Object* ThrowRedeclarationError(Isolate* isolate, Handle<String> name,
35 RedeclarationType redeclaration_type) {
36 HandleScope scope(isolate);
37 if (redeclaration_type == RedeclarationType::kSyntaxError) {
38 THROW_NEW_ERROR_RETURN_FAILURE(
39 isolate, NewSyntaxError(MessageTemplate::kVarRedeclaration, name));
41 THROW_NEW_ERROR_RETURN_FAILURE(
42 isolate, NewTypeError(MessageTemplate::kVarRedeclaration, name));
48 Object* DeclareGlobal(
49 Isolate* isolate, Handle<JSGlobalObject> global, Handle<String> name,
50 Handle<Object> value, PropertyAttributes attr,
bool is_var,
51 bool is_function_declaration, RedeclarationType redeclaration_type,
52 Handle<FeedbackVector> feedback_vector = Handle<FeedbackVector>(),
53 FeedbackSlot slot = FeedbackSlot::Invalid()) {
54 Handle<ScriptContextTable> script_contexts(
55 global->native_context()->script_context_table(), isolate);
56 ScriptContextTable::LookupResult lookup;
57 if (ScriptContextTable::Lookup(isolate, script_contexts, name, &lookup) &&
58 IsLexicalVariableMode(lookup.mode)) {
62 return ThrowRedeclarationError(isolate, name,
63 RedeclarationType::kSyntaxError);
67 LookupIterator::Configuration lookup_config(
68 LookupIterator::Configuration::OWN_SKIP_INTERCEPTOR);
69 if (is_function_declaration) {
72 lookup_config = LookupIterator::Configuration::OWN;
74 LookupIterator it(global, name, global, lookup_config);
75 Maybe<PropertyAttributes> maybe = JSReceiver::GetPropertyAttributes(&it);
76 if (maybe.IsNothing())
return ReadOnlyRoots(isolate).exception();
79 PropertyAttributes old_attributes = maybe.FromJust();
83 if (is_var)
return ReadOnlyRoots(isolate).undefined_value();
85 DCHECK(is_function_declaration);
86 if ((old_attributes & DONT_DELETE) != 0) {
89 DCHECK_EQ(attr & READ_ONLY, 0);
93 if (old_attributes & READ_ONLY || old_attributes & DONT_ENUM ||
94 (it.state() == LookupIterator::ACCESSOR)) {
99 return ThrowRedeclarationError(isolate, name, redeclaration_type);
102 attr = old_attributes;
111 if (it.state() == LookupIterator::ACCESSOR) it.Delete();
114 if (is_function_declaration) {
119 RETURN_FAILURE_ON_EXCEPTION(
120 isolate, JSObject::DefineOwnPropertyIgnoreAttributes(&it, value, attr));
122 if (!feedback_vector.is_null() &&
123 it.state() != LookupIterator::State::INTERCEPTOR) {
124 DCHECK_EQ(*global, *it.GetHolder<Object>());
127 if (!global->HasNamedInterceptor() ||
128 global->GetNamedInterceptor()->non_masking()) {
129 FeedbackNexus nexus(feedback_vector, slot);
130 nexus.ConfigurePropertyCellMode(it.GetPropertyCell());
133 return ReadOnlyRoots(isolate).undefined_value();
136 Object* DeclareGlobals(Isolate* isolate, Handle<FixedArray> declarations,
137 int flags, Handle<FeedbackVector> feedback_vector) {
138 HandleScope scope(isolate);
139 Handle<JSGlobalObject> global(isolate->global_object());
140 Handle<Context> context(isolate->context(), isolate);
143 int length = declarations->length();
144 FOR_WITH_HANDLE_SCOPE(isolate,
int,
i = 0,
i,
i < length,
i += 4, {
145 Handle<String> name(String::cast(declarations->get(
i)), isolate);
146 FeedbackSlot slot(Smi::ToInt(declarations->get(
i + 1)));
147 Handle<Object> possibly_feedback_cell_slot(declarations->get(
i + 2),
149 Handle<Object> initial_value(declarations->get(
i + 3), isolate);
151 bool is_var = initial_value->IsUndefined(isolate);
152 bool is_function = initial_value->IsSharedFunctionInfo();
153 DCHECK_EQ(1, BoolToInt(is_var) + BoolToInt(is_function));
155 Handle<Object> value;
160 Handle<FeedbackCell> feedback_cell =
161 isolate->factory()->no_feedback_cell();
162 if (!feedback_vector.is_null()) {
163 DCHECK(possibly_feedback_cell_slot->IsSmi());
164 FeedbackSlot feedback_cells_slot(
165 Smi::ToInt(*possibly_feedback_cell_slot));
166 feedback_cell = Handle<FeedbackCell>(
167 FeedbackCell::cast(feedback_vector->Get(feedback_cells_slot)
168 ->GetHeapObjectAssumeStrong()),
172 Handle<SharedFunctionInfo> shared =
173 Handle<SharedFunctionInfo>::cast(initial_value);
174 Handle<JSFunction>
function =
175 isolate->factory()->NewFunctionFromSharedFunctionInfo(
176 shared, context, feedback_cell, TENURED);
179 value = isolate->factory()->undefined_value();
184 bool is_native = DeclareGlobalsNativeFlag::decode(flags);
185 bool is_eval = DeclareGlobalsEvalFlag::decode(flags);
187 if (is_function && is_native) attr |= READ_ONLY;
188 if (!is_eval) attr |= DONT_DELETE;
192 Object* result = DeclareGlobal(
193 isolate, global, name, value, static_cast<PropertyAttributes>(attr),
194 is_var, is_function, RedeclarationType::kSyntaxError, feedback_vector,
196 if (isolate->has_pending_exception())
return result;
199 return ReadOnlyRoots(isolate).undefined_value();
204 RUNTIME_FUNCTION(Runtime_DeclareGlobals) {
205 HandleScope scope(isolate);
206 DCHECK_EQ(3, args.length());
208 CONVERT_ARG_HANDLE_CHECKED(FixedArray, declarations, 0);
209 CONVERT_SMI_ARG_CHECKED(flags, 1);
210 CONVERT_ARG_HANDLE_CHECKED(JSFunction, closure, 2);
212 Handle<FeedbackVector> feedback_vector = Handle<FeedbackVector>();
213 if (closure->has_feedback_vector()) {
215 Handle<FeedbackVector>(closure->feedback_vector(), isolate);
217 return DeclareGlobals(isolate, declarations, flags, feedback_vector);
222 Object* DeclareEvalHelper(Isolate* isolate, Handle<String> name,
223 Handle<Object> value) {
228 Handle<Context> context_arg(isolate->context(), isolate);
229 Handle<Context> context(context_arg->declaration_context(), isolate);
231 DCHECK(context->IsFunctionContext() || context->IsNativeContext() ||
232 context->IsScriptContext() || context->IsEvalContext() ||
233 (context->IsBlockContext() &&
234 context->scope_info()->is_declaration_scope()));
236 bool is_function = value->IsJSFunction();
237 bool is_var = !is_function;
238 DCHECK(!is_var || value->IsUndefined(isolate));
241 PropertyAttributes attributes;
242 InitializationFlag init_flag;
246 const ContextLookupFlags lookup_flags =
static_cast<ContextLookupFlags
>(
247 FOLLOW_CONTEXT_CHAIN | STOP_AT_DECLARATION_SCOPE | SKIP_WITH_CONTEXT);
248 context_arg->Lookup(name, lookup_flags, &index, &attributes, &init_flag,
250 if (attributes != ABSENT && IsLexicalVariableMode(mode)) {
256 return ThrowRedeclarationError(isolate, name,
257 RedeclarationType::kSyntaxError);
260 Handle<Object> holder = context->Lookup(name, DONT_FOLLOW_CHAINS, &index,
261 &attributes, &init_flag, &mode);
262 DCHECK(holder.is_null() || !holder->IsModule());
263 DCHECK(!isolate->has_pending_exception());
265 Handle<JSObject> object;
267 if (attributes != ABSENT && holder->IsJSGlobalObject()) {
270 return DeclareGlobal(isolate, Handle<JSGlobalObject>::cast(holder), name,
271 value, NONE, is_var, is_function,
272 RedeclarationType::kTypeError);
274 if (context_arg->extension()->IsJSGlobalObject()) {
275 Handle<JSGlobalObject> global(
276 JSGlobalObject::cast(context_arg->extension()), isolate);
277 return DeclareGlobal(isolate, global, name, value, NONE, is_var,
278 is_function, RedeclarationType::kTypeError);
279 }
else if (context->IsScriptContext()) {
280 DCHECK(context->global_object()->IsJSGlobalObject());
281 Handle<JSGlobalObject> global(
282 JSGlobalObject::cast(context->global_object()), isolate);
283 return DeclareGlobal(isolate, global, name, value, NONE, is_var,
284 is_function, RedeclarationType::kTypeError);
287 if (attributes != ABSENT) {
288 DCHECK_EQ(NONE, attributes);
291 if (is_var)
return ReadOnlyRoots(isolate).undefined_value();
294 if (index != Context::kNotFound) {
295 DCHECK(holder.is_identical_to(context));
296 context->set(index, *value);
297 return ReadOnlyRoots(isolate).undefined_value();
300 object = Handle<JSObject>::cast(holder);
302 }
else if (context->has_extension()) {
303 object = handle(context->extension_object(), isolate);
304 DCHECK(object->IsJSContextExtensionObject() ||
object->IsJSGlobalObject());
309 DCHECK((context->IsBlockContext() &&
310 context->scope_info()->is_declaration_scope()) ||
311 context->IsFunctionContext());
313 isolate->factory()->NewJSObject(isolate->context_extension_function());
315 context->set_extension(*
object);
318 RETURN_FAILURE_ON_EXCEPTION(isolate, JSObject::SetOwnPropertyIgnoreAttributes(
319 object, name, value, NONE));
321 return ReadOnlyRoots(isolate).undefined_value();
326 RUNTIME_FUNCTION(Runtime_DeclareEvalFunction) {
327 HandleScope scope(isolate);
328 DCHECK_EQ(2, args.length());
329 CONVERT_ARG_HANDLE_CHECKED(String, name, 0);
330 CONVERT_ARG_HANDLE_CHECKED(Object, value, 1);
331 return DeclareEvalHelper(isolate, name, value);
334 RUNTIME_FUNCTION(Runtime_DeclareEvalVar) {
335 HandleScope scope(isolate);
336 DCHECK_EQ(1, args.length());
337 CONVERT_ARG_HANDLE_CHECKED(String, name, 0);
338 return DeclareEvalHelper(isolate, name,
339 isolate->factory()->undefined_value());
346 std::unique_ptr<Handle<Object>[]> GetCallerArguments(Isolate* isolate,
349 JavaScriptFrameIterator it(isolate);
350 JavaScriptFrame* frame = it.frame();
351 std::vector<SharedFunctionInfo*> functions;
352 frame->GetFunctions(&functions);
353 if (functions.size() > 1) {
354 int inlined_jsframe_index =
static_cast<int>(functions.size()) - 1;
355 TranslatedState translated_values(frame);
356 translated_values.Prepare(frame->fp());
358 int argument_count = 0;
359 TranslatedFrame* translated_frame =
360 translated_values.GetArgumentsInfoFromJSFrameIndex(
361 inlined_jsframe_index, &argument_count);
362 TranslatedFrame::iterator iter = translated_frame->begin();
371 *total_argc = argument_count;
372 std::unique_ptr<Handle<Object>[]> param_data(
373 NewArray<Handle<Object>>(*total_argc));
374 bool should_deoptimize =
false;
375 for (
int i = 0;
i < argument_count;
i++) {
378 should_deoptimize = should_deoptimize || iter->IsMaterializedObject();
379 Handle<Object> value = iter->GetValue();
380 param_data[
i] = value;
384 if (should_deoptimize) {
385 translated_values.StoreMaterializedValuesAndDeopt(frame);
390 if (it.frame()->has_adapted_arguments()) {
391 it.AdvanceOneFrame();
392 DCHECK(it.frame()->is_arguments_adaptor());
395 int args_count = frame->ComputeParametersCount();
397 *total_argc = args_count;
398 std::unique_ptr<Handle<Object>[]> param_data(
399 NewArray<Handle<Object>>(*total_argc));
400 for (
int i = 0;
i < args_count;
i++) {
401 Handle<Object> val = Handle<Object>(frame->GetParameter(
i), isolate);
408 template <
typename T>
409 Handle<JSObject> NewSloppyArguments(Isolate* isolate, Handle<JSFunction> callee,
410 T parameters,
int argument_count) {
411 CHECK(!IsDerivedConstructor(callee->shared()->kind()));
412 DCHECK(callee->shared()->has_simple_parameters());
413 Handle<JSObject> result =
414 isolate->factory()->NewArgumentsObject(callee, argument_count);
417 int parameter_count = callee->shared()->internal_formal_parameter_count();
418 if (argument_count > 0) {
419 if (parameter_count > 0) {
420 int mapped_count = Min(argument_count, parameter_count);
421 Handle<FixedArray> parameter_map =
422 isolate->factory()->NewFixedArray(mapped_count + 2, NOT_TENURED);
423 parameter_map->set_map(
424 ReadOnlyRoots(isolate).sloppy_arguments_elements_map());
425 result->set_map(isolate->native_context()->fast_aliased_arguments_map());
426 result->set_elements(*parameter_map);
430 Handle<Context> context(isolate->context(), isolate);
431 Handle<FixedArray> arguments =
432 isolate->factory()->NewFixedArray(argument_count, NOT_TENURED);
433 parameter_map->set(0, *context);
434 parameter_map->set(1, *arguments);
437 int index = argument_count - 1;
438 while (index >= mapped_count) {
441 arguments->set(index, parameters[index]);
445 Handle<ScopeInfo> scope_info(callee->shared()->scope_info(), isolate);
449 for (
int i = 0;
i < mapped_count;
i++) {
450 arguments->set(
i, parameters[
i]);
451 parameter_map->set_the_hole(
i + 2);
456 for (
int i = 0;
i < scope_info->ContextLocalCount();
i++) {
457 if (!scope_info->ContextLocalIsParameter(
i))
continue;
458 int parameter = scope_info->ContextLocalParameterNumber(
i);
459 if (parameter >= mapped_count)
continue;
460 arguments->set_the_hole(parameter);
461 Smi slot = Smi::FromInt(Context::MIN_CONTEXT_SLOTS +
i);
462 parameter_map->set(parameter + 2, slot);
467 Handle<FixedArray> elements =
468 isolate->factory()->NewFixedArray(argument_count, NOT_TENURED);
469 result->set_elements(*elements);
470 for (
int i = 0;
i < argument_count; ++
i) {
471 elements->set(
i, parameters[
i]);
478 class HandleArguments {
480 explicit HandleArguments(Handle<Object>* array) : array_(array) {}
481 Object* operator[](
int index) {
return *array_[index]; }
484 Handle<Object>* array_;
487 class ParameterArguments {
489 explicit ParameterArguments(Address parameters) : parameters_(parameters) {}
490 Object* operator[](
int index) {
491 return *ObjectSlot(parameters_ - (index + 1) * kPointerSize);
501 RUNTIME_FUNCTION(Runtime_NewSloppyArguments_Generic) {
502 HandleScope scope(isolate);
503 DCHECK_EQ(1, args.length());
504 CONVERT_ARG_HANDLE_CHECKED(JSFunction, callee, 0);
507 int argument_count = 0;
508 std::unique_ptr<Handle<Object>[]> arguments =
509 GetCallerArguments(isolate, &argument_count);
510 HandleArguments argument_getter(arguments.get());
511 return *NewSloppyArguments(isolate, callee, argument_getter, argument_count);
515 RUNTIME_FUNCTION(Runtime_NewStrictArguments) {
516 HandleScope scope(isolate);
517 DCHECK_EQ(1, args.length());
518 CONVERT_ARG_HANDLE_CHECKED(JSFunction, callee, 0);
521 int argument_count = 0;
522 std::unique_ptr<Handle<Object>[]> arguments =
523 GetCallerArguments(isolate, &argument_count);
524 Handle<JSObject> result =
525 isolate->factory()->NewArgumentsObject(callee, argument_count);
526 if (argument_count) {
527 Handle<FixedArray> array =
528 isolate->factory()->NewUninitializedFixedArray(argument_count);
529 DisallowHeapAllocation no_gc;
530 WriteBarrierMode mode = array->GetWriteBarrierMode(no_gc);
531 for (
int i = 0;
i < argument_count;
i++) {
532 array->set(
i, *arguments[
i], mode);
534 result->set_elements(*array);
540 RUNTIME_FUNCTION(Runtime_NewRestParameter) {
541 HandleScope scope(isolate);
542 DCHECK_EQ(1, args.length());
543 CONVERT_ARG_HANDLE_CHECKED(JSFunction, callee, 0)
544 int start_index = callee->shared()->internal_formal_parameter_count();
547 int argument_count = 0;
548 std::unique_ptr<Handle<Object>[]> arguments =
549 GetCallerArguments(isolate, &argument_count);
550 int num_elements =
std::max(0, argument_count - start_index);
551 Handle<JSObject> result = isolate->factory()->NewJSArray(
552 PACKED_ELEMENTS, num_elements, num_elements,
553 DONT_INITIALIZE_ARRAY_ELEMENTS);
555 DisallowHeapAllocation no_gc;
556 FixedArray elements = FixedArray::cast(result->elements());
557 WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc);
558 for (
int i = 0;
i < num_elements;
i++) {
559 elements->set(
i, *arguments[
i + start_index], mode);
566 RUNTIME_FUNCTION(Runtime_NewSloppyArguments) {
567 HandleScope scope(isolate);
568 DCHECK_EQ(1, args.length());
569 CONVERT_ARG_HANDLE_CHECKED(JSFunction, callee, 0);
570 StackFrameIterator iterator(isolate);
574 DCHECK(iterator.frame()->type() == StackFrame::STUB);
578 JavaScriptFrame* function_frame = JavaScriptFrame::cast(iterator.frame());
579 DCHECK(function_frame->is_java_script());
580 int argc = function_frame->ComputeParametersCount();
581 Address fp = function_frame->fp();
582 if (function_frame->has_adapted_arguments()) {
584 ArgumentsAdaptorFrame* adaptor_frame =
585 ArgumentsAdaptorFrame::cast(iterator.frame());
586 argc = adaptor_frame->ComputeParametersCount();
587 fp = adaptor_frame->fp();
591 fp + argc * kPointerSize + StandardFrameConstants::kCallerSPOffset;
592 ParameterArguments argument_getter(parameters);
593 return *NewSloppyArguments(isolate, callee, argument_getter, argc);
596 RUNTIME_FUNCTION(Runtime_NewArgumentsElements) {
597 HandleScope scope(isolate);
598 DCHECK_EQ(3, args.length());
601 DCHECK(args[0].IsSmi());
602 ObjectSlot frame(args[0]->ptr());
603 CONVERT_SMI_ARG_CHECKED(length, 1);
604 CONVERT_SMI_ARG_CHECKED(mapped_count, 2);
605 Handle<FixedArray> result =
606 isolate->factory()->NewUninitializedFixedArray(length);
607 int const offset = length + 1;
608 DisallowHeapAllocation no_gc;
609 WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc);
610 int number_of_holes = Min(mapped_count, length);
611 for (
int index = 0; index < number_of_holes; ++index) {
612 result->set_the_hole(isolate, index);
614 for (
int index = number_of_holes; index < length; ++index) {
615 result->set(index, *(frame + (offset - index)), mode);
620 RUNTIME_FUNCTION(Runtime_NewClosure) {
621 HandleScope scope(isolate);
622 DCHECK_EQ(2, args.length());
623 CONVERT_ARG_HANDLE_CHECKED(SharedFunctionInfo, shared, 0);
624 CONVERT_ARG_HANDLE_CHECKED(FeedbackCell, feedback_cell, 1);
625 Handle<Context> context(isolate->context(), isolate);
626 Handle<JSFunction>
function =
627 isolate->factory()->NewFunctionFromSharedFunctionInfo(
628 shared, context, feedback_cell, NOT_TENURED);
632 RUNTIME_FUNCTION(Runtime_NewClosure_Tenured) {
633 HandleScope scope(isolate);
634 DCHECK_EQ(2, args.length());
635 CONVERT_ARG_HANDLE_CHECKED(SharedFunctionInfo, shared, 0);
636 CONVERT_ARG_HANDLE_CHECKED(FeedbackCell, feedback_cell, 1);
637 Handle<Context> context(isolate->context(), isolate);
640 Handle<JSFunction>
function =
641 isolate->factory()->NewFunctionFromSharedFunctionInfo(
642 shared, context, feedback_cell, TENURED);
646 static Object* FindNameClash(Isolate* isolate, Handle<ScopeInfo> scope_info,
647 Handle<JSGlobalObject> global_object,
648 Handle<ScriptContextTable> script_context) {
649 for (
int var = 0; var < scope_info->ContextLocalCount(); var++) {
650 Handle<String> name(scope_info->ContextLocalName(var), isolate);
651 VariableMode mode = scope_info->ContextLocalMode(var);
652 ScriptContextTable::LookupResult lookup;
653 if (ScriptContextTable::Lookup(isolate, script_context, name, &lookup)) {
654 if (IsLexicalVariableMode(mode) || IsLexicalVariableMode(lookup.mode)) {
658 return ThrowRedeclarationError(isolate, name,
659 RedeclarationType::kSyntaxError);
663 if (IsLexicalVariableMode(mode)) {
664 LookupIterator it(global_object, name, global_object,
665 LookupIterator::OWN_SKIP_INTERCEPTOR);
666 Maybe<PropertyAttributes> maybe = JSReceiver::GetPropertyAttributes(&it);
667 if (maybe.IsNothing())
return ReadOnlyRoots(isolate).exception();
668 if ((maybe.FromJust() & DONT_DELETE) != 0) {
674 return ThrowRedeclarationError(isolate, name,
675 RedeclarationType::kSyntaxError);
678 JSGlobalObject::InvalidatePropertyCell(global_object, name);
681 return ReadOnlyRoots(isolate).undefined_value();
685 RUNTIME_FUNCTION(Runtime_NewScriptContext) {
686 HandleScope scope(isolate);
687 DCHECK_EQ(1, args.length());
689 CONVERT_ARG_HANDLE_CHECKED(ScopeInfo, scope_info, 0);
690 Handle<NativeContext> native_context(NativeContext::cast(isolate->context()),
692 Handle<JSGlobalObject> global_object(native_context->global_object(),
694 Handle<ScriptContextTable> script_context_table(
695 native_context->script_context_table(), isolate);
697 Object* name_clash_result =
698 FindNameClash(isolate, scope_info, global_object, script_context_table);
699 if (isolate->has_pending_exception())
return name_clash_result;
702 DCHECK(!isolate->bootstrapper()->IsActive());
704 Handle<Context> result =
705 isolate->factory()->NewScriptContext(native_context, scope_info);
707 Handle<ScriptContextTable> new_script_context_table =
708 ScriptContextTable::Extend(script_context_table, result);
709 native_context->set_script_context_table(*new_script_context_table);
713 RUNTIME_FUNCTION(Runtime_NewFunctionContext) {
714 HandleScope scope(isolate);
715 DCHECK_EQ(1, args.length());
717 CONVERT_ARG_HANDLE_CHECKED(ScopeInfo, scope_info, 0);
719 Handle<Context> outer(isolate->context(), isolate);
720 return *isolate->factory()->NewFunctionContext(outer, scope_info);
723 RUNTIME_FUNCTION(Runtime_PushWithContext) {
724 HandleScope scope(isolate);
725 DCHECK_EQ(2, args.length());
726 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, extension_object, 0);
727 CONVERT_ARG_HANDLE_CHECKED(ScopeInfo, scope_info, 1);
728 Handle<Context> current(isolate->context(), isolate);
729 Handle<Context> context =
730 isolate->factory()->NewWithContext(current, scope_info, extension_object);
731 isolate->set_context(*context);
735 RUNTIME_FUNCTION(Runtime_PushModuleContext) {
736 HandleScope scope(isolate);
737 DCHECK_EQ(2, args.length());
738 CONVERT_ARG_HANDLE_CHECKED(Module, module, 0);
739 CONVERT_ARG_HANDLE_CHECKED(ScopeInfo, scope_info, 1);
741 Handle<NativeContext> outer(NativeContext::cast(isolate->context()), isolate);
742 Handle<Context> context =
743 isolate->factory()->NewModuleContext(module, outer, scope_info);
744 isolate->set_context(*context);
748 RUNTIME_FUNCTION(Runtime_PushCatchContext) {
749 HandleScope scope(isolate);
750 DCHECK_EQ(2, args.length());
751 CONVERT_ARG_HANDLE_CHECKED(Object, thrown_object, 0);
752 CONVERT_ARG_HANDLE_CHECKED(ScopeInfo, scope_info, 1);
753 Handle<Context> current(isolate->context(), isolate);
754 Handle<Context> context =
755 isolate->factory()->NewCatchContext(current, scope_info, thrown_object);
756 isolate->set_context(*context);
761 RUNTIME_FUNCTION(Runtime_PushBlockContext) {
762 HandleScope scope(isolate);
763 DCHECK_EQ(1, args.length());
764 CONVERT_ARG_HANDLE_CHECKED(ScopeInfo, scope_info, 0);
765 Handle<Context> current(isolate->context(), isolate);
766 Handle<Context> context =
767 isolate->factory()->NewBlockContext(current, scope_info);
768 isolate->set_context(*context);
773 RUNTIME_FUNCTION(Runtime_DeleteLookupSlot) {
774 HandleScope scope(isolate);
775 DCHECK_EQ(1, args.length());
776 CONVERT_ARG_HANDLE_CHECKED(String, name, 0);
779 PropertyAttributes attributes;
780 InitializationFlag flag;
782 Handle<Object> holder = isolate->context()->Lookup(
783 name, FOLLOW_CHAINS, &index, &attributes, &flag, &mode);
786 if (holder.is_null()) {
788 if (isolate->has_pending_exception())
789 return ReadOnlyRoots(isolate).exception();
790 return ReadOnlyRoots(isolate).true_value();
795 if (holder->IsContext() || holder->IsModule()) {
796 return ReadOnlyRoots(isolate).false_value();
802 Handle<JSReceiver>
object = Handle<JSReceiver>::cast(holder);
803 Maybe<bool> result = JSReceiver::DeleteProperty(
object, name);
804 MAYBE_RETURN(result, ReadOnlyRoots(isolate).exception());
805 return isolate->heap()->ToBoolean(result.FromJust());
811 MaybeHandle<Object> LoadLookupSlot(Isolate* isolate, Handle<String> name,
812 ShouldThrow should_throw,
813 Handle<Object>* receiver_return =
nullptr) {
815 PropertyAttributes attributes;
816 InitializationFlag flag;
818 Handle<Object> holder = isolate->context()->Lookup(
819 name, FOLLOW_CHAINS, &index, &attributes, &flag, &mode);
820 if (isolate->has_pending_exception())
return MaybeHandle<Object>();
822 if (!holder.is_null() && holder->IsModule()) {
823 Handle<Object> receiver = isolate->factory()->undefined_value();
824 if (receiver_return) *receiver_return = receiver;
825 return Module::LoadVariable(isolate, Handle<Module>::cast(holder), index);
827 if (index != Context::kNotFound) {
828 DCHECK(holder->IsContext());
831 Handle<Object> receiver = isolate->factory()->undefined_value();
832 Handle<Object> value = handle(Context::cast(*holder)->get(index), isolate);
834 if (flag == kNeedsInitialization && value->IsTheHole(isolate)) {
835 THROW_NEW_ERROR(isolate,
836 NewReferenceError(MessageTemplate::kNotDefined, name),
839 DCHECK(!value->IsTheHole(isolate));
840 if (receiver_return) *receiver_return = receiver;
847 if (!holder.is_null()) {
850 Handle<Object> value;
851 ASSIGN_RETURN_ON_EXCEPTION(
852 isolate, value, Object::GetProperty(isolate, holder, name), Object);
853 if (receiver_return) {
855 (holder->IsJSGlobalObject() || holder->IsJSContextExtensionObject())
856 ? Handle<Object>::cast(isolate->factory()->undefined_value())
862 if (should_throw == kThrowOnError) {
865 isolate, NewReferenceError(MessageTemplate::kNotDefined, name), Object);
869 if (receiver_return) *receiver_return = isolate->factory()->undefined_value();
870 return isolate->factory()->undefined_value();
876 RUNTIME_FUNCTION(Runtime_LoadLookupSlot) {
877 HandleScope scope(isolate);
878 DCHECK_EQ(1, args.length());
879 CONVERT_ARG_HANDLE_CHECKED(String, name, 0);
880 RETURN_RESULT_OR_FAILURE(isolate,
881 LoadLookupSlot(isolate, name, kThrowOnError));
885 RUNTIME_FUNCTION(Runtime_LoadLookupSlotInsideTypeof) {
886 HandleScope scope(isolate);
887 DCHECK_EQ(1, args.length());
888 CONVERT_ARG_HANDLE_CHECKED(String, name, 0);
889 RETURN_RESULT_OR_FAILURE(isolate, LoadLookupSlot(isolate, name, kDontThrow));
893 RUNTIME_FUNCTION_RETURN_PAIR(Runtime_LoadLookupSlotForCall) {
894 HandleScope scope(isolate);
895 DCHECK_EQ(1, args.length());
896 DCHECK(args[0]->IsString());
897 Handle<String> name = args.at<String>(0);
898 Handle<Object> value;
899 Handle<Object> receiver;
900 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
901 isolate, value, LoadLookupSlot(isolate, name, kThrowOnError, &receiver),
902 MakePair(ReadOnlyRoots(isolate).exception(),
nullptr));
903 return MakePair(*value, *receiver);
909 MaybeHandle<Object> StoreLookupSlot(
910 Isolate* isolate, Handle<String> name, Handle<Object> value,
911 LanguageMode language_mode,
912 ContextLookupFlags context_lookup_flags = FOLLOW_CHAINS) {
913 Handle<Context> context(isolate->context(), isolate);
916 PropertyAttributes attributes;
917 InitializationFlag flag;
919 bool is_sloppy_function_name;
920 Handle<Object> holder =
921 context->Lookup(name, context_lookup_flags, &index, &attributes, &flag,
922 &mode, &is_sloppy_function_name);
923 if (holder.is_null()) {
925 if (isolate->has_pending_exception())
return MaybeHandle<Object>();
926 }
else if (holder->IsModule()) {
927 if ((attributes & READ_ONLY) == 0) {
928 Module::StoreVariable(Handle<Module>::cast(holder), index, value);
931 isolate, NewTypeError(MessageTemplate::kConstAssign, name), Object);
936 if (index != Context::kNotFound) {
937 if (flag == kNeedsInitialization &&
938 Handle<Context>::cast(holder)->
get(index)->IsTheHole(isolate)) {
939 THROW_NEW_ERROR(isolate,
940 NewReferenceError(MessageTemplate::kNotDefined, name),
943 if ((attributes & READ_ONLY) == 0) {
944 Handle<Context>::cast(holder)->set(index, *value);
945 }
else if (!is_sloppy_function_name || is_strict(language_mode)) {
947 isolate, NewTypeError(MessageTemplate::kConstAssign, name), Object);
955 Handle<JSReceiver> object;
956 if (attributes != ABSENT) {
958 object = Handle<JSReceiver>::cast(holder);
959 }
else if (is_strict(language_mode)) {
962 isolate, NewReferenceError(MessageTemplate::kNotDefined, name), Object);
965 object = handle(context->global_object(), isolate);
968 ASSIGN_RETURN_ON_EXCEPTION(
970 Object::SetProperty(isolate,
object, name, value, language_mode), Object);
977 RUNTIME_FUNCTION(Runtime_StoreLookupSlot_Sloppy) {
978 HandleScope scope(isolate);
979 DCHECK_EQ(2, args.length());
980 CONVERT_ARG_HANDLE_CHECKED(String, name, 0);
981 CONVERT_ARG_HANDLE_CHECKED(Object, value, 1);
982 RETURN_RESULT_OR_FAILURE(
983 isolate, StoreLookupSlot(isolate, name, value, LanguageMode::kSloppy));
989 RUNTIME_FUNCTION(Runtime_StoreLookupSlot_SloppyHoisting) {
990 HandleScope scope(isolate);
991 DCHECK_EQ(2, args.length());
992 CONVERT_ARG_HANDLE_CHECKED(String, name, 0);
993 CONVERT_ARG_HANDLE_CHECKED(Object, value, 1);
994 const ContextLookupFlags lookup_flags =
static_cast<ContextLookupFlags
>(
995 FOLLOW_CONTEXT_CHAIN | STOP_AT_DECLARATION_SCOPE | SKIP_WITH_CONTEXT);
996 RETURN_RESULT_OR_FAILURE(
997 isolate, StoreLookupSlot(isolate, name, value, LanguageMode::kSloppy,
1001 RUNTIME_FUNCTION(Runtime_StoreLookupSlot_Strict) {
1002 HandleScope scope(isolate);
1003 DCHECK_EQ(2, args.length());
1004 CONVERT_ARG_HANDLE_CHECKED(String, name, 0);
1005 CONVERT_ARG_HANDLE_CHECKED(Object, value, 1);
1006 RETURN_RESULT_OR_FAILURE(
1007 isolate, StoreLookupSlot(isolate, name, value, LanguageMode::kStrict));