OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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/builtins/builtins.h" | 5 #include "src/builtins/builtins.h" |
6 | 6 |
7 #include "src/accessors.h" | |
7 #include "src/api-arguments.h" | 8 #include "src/api-arguments.h" |
8 #include "src/api-natives.h" | 9 #include "src/api-natives.h" |
9 #include "src/api.h" | 10 #include "src/api.h" |
10 #include "src/base/ieee754.h" | 11 #include "src/base/ieee754.h" |
11 #include "src/base/once.h" | 12 #include "src/base/once.h" |
12 #include "src/bootstrapper.h" | 13 #include "src/bootstrapper.h" |
13 #include "src/code-factory.h" | 14 #include "src/code-factory.h" |
14 #include "src/code-stub-assembler.h" | 15 #include "src/code-stub-assembler.h" |
15 #include "src/dateparser-inl.h" | 16 #include "src/dateparser-inl.h" |
16 #include "src/elements.h" | 17 #include "src/elements.h" |
(...skipping 4589 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4606 // static | 4607 // static |
4607 void Builtins::Generate_DatePrototypeGetUTCMonth(MacroAssembler* masm) { | 4608 void Builtins::Generate_DatePrototypeGetUTCMonth(MacroAssembler* masm) { |
4608 Generate_DatePrototype_GetField(masm, JSDate::kMonthUTC); | 4609 Generate_DatePrototype_GetField(masm, JSDate::kMonthUTC); |
4609 } | 4610 } |
4610 | 4611 |
4611 // static | 4612 // static |
4612 void Builtins::Generate_DatePrototypeGetUTCSeconds(MacroAssembler* masm) { | 4613 void Builtins::Generate_DatePrototypeGetUTCSeconds(MacroAssembler* masm) { |
4613 Generate_DatePrototype_GetField(masm, JSDate::kSecondUTC); | 4614 Generate_DatePrototype_GetField(masm, JSDate::kSecondUTC); |
4614 } | 4615 } |
4615 | 4616 |
4617 // ES6 section 19.5.1.1 Error ( message ) | |
4618 BUILTIN(ErrorConstructor) { | |
4619 HandleScope scope(isolate); | |
4620 | |
4621 // 1. If NewTarget is undefined, let newTarget be the active function object, | |
4622 // else let newTarget be NewTarget. | |
4623 | |
4624 Handle<JSFunction> target = args.target<JSFunction>(); | |
4625 Handle<JSReceiver> new_target; | |
4626 if (args.new_target()->IsJSReceiver()) { | |
4627 new_target = Handle<JSReceiver>::cast(args.new_target()); | |
4628 } else { | |
4629 new_target = target; | |
4630 } | |
4631 | |
4632 // 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%ErrorPrototype%", | |
4633 // « [[ErrorData]] »). | |
4634 Handle<JSObject> err; | |
4635 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, err, | |
4636 JSObject::New(target, new_target)); | |
4637 | |
4638 // 3. If message is not undefined, then | |
4639 // a. Let msg be ? ToString(message). | |
4640 // b. Let msgDesc be the PropertyDescriptor{[[Value]]: msg, [[Writable]]: | |
4641 // true, [[Enumerable]]: false, [[Configurable]]: true}. | |
4642 // c. Perform ! DefinePropertyOrThrow(O, "message", msgDesc). | |
4643 // 4. Return O. | |
4644 | |
4645 Handle<Object> msg = args.atOrUndefined(isolate, 1); | |
4646 if (!msg->IsUndefined(isolate)) { | |
4647 Handle<String> msg_string; | |
4648 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, msg_string, | |
4649 Object::ToString(isolate, msg)); | |
4650 RETURN_FAILURE_ON_EXCEPTION( | |
4651 isolate, | |
4652 JSObject::SetOwnPropertyIgnoreAttributes( | |
4653 err, isolate->factory()->message_string(), msg_string, DONT_ENUM)); | |
4654 } | |
4655 | |
4656 // Capture the stack trace unless we're setting up. | |
4657 if (!isolate->bootstrapper()->IsActive()) { | |
4658 // Optionally capture a more detailed stack trace for the message. | |
4659 RETURN_FAILURE_ON_EXCEPTION(isolate, | |
4660 isolate->CaptureAndSetDetailedStackTrace(err)); | |
4661 // Capture a simple stack trace for the stack property. | |
4662 RETURN_FAILURE_ON_EXCEPTION(isolate, | |
4663 isolate->CaptureAndSetSimpleStackTrace(err)); | |
4664 } | |
4665 | |
4666 return *err; | |
4667 } | |
4668 | |
4669 // static | |
4670 BUILTIN(ErrorCaptureStackTrace) { | |
4671 HandleScope scope(isolate); | |
4672 Handle<Object> object_obj = args.atOrUndefined(isolate, 1); | |
4673 if (!object_obj->IsJSObject()) { | |
4674 THROW_NEW_ERROR_RETURN_FAILURE( | |
4675 isolate, NewTypeError(MessageTemplate::kInvalidArgument, object_obj)); | |
4676 } | |
4677 Handle<JSObject> object = Handle<JSObject>::cast(object_obj); | |
4678 Handle<Object> caller = args.atOrUndefined(isolate, 2); | |
4679 | |
4680 // TODO(jgruber): Eagerly format the stack trace and remove accessors.h | |
4681 // include. | |
4682 | |
4683 // Check if the stack property is read-only. | |
4684 | |
4685 bool is_extensible = true; | |
4686 if (!JSObject::IsExtensible(object)) { | |
4687 is_extensible = false; | |
4688 } | |
4689 | |
4690 PropertyDescriptor desc; | |
4691 Maybe<bool> owned = JSReceiver::GetOwnPropertyDescriptor( | |
4692 isolate, object, isolate->factory()->stack_string(), &desc); | |
4693 if (owned.FromMaybe(false)) { | |
4694 if (!desc.configurable() || !desc.writable()) { | |
4695 is_extensible = false; | |
4696 } | |
4697 } | |
4698 | |
4699 if (!is_extensible) { | |
4700 THROW_NEW_ERROR_RETURN_FAILURE( | |
4701 isolate, NewTypeError(MessageTemplate::kDefineDisallowed, | |
4702 isolate->factory()->stack_string(), object)); | |
4703 } | |
4704 | |
4705 // Add stack accessors to the given object | |
4706 | |
4707 Handle<Map> map(object->map()); | |
4708 PropertyAttributes attribs = DONT_ENUM; | |
4709 Handle<AccessorInfo> error_stack = | |
4710 Accessors::ErrorStackInfo(isolate, attribs); | |
4711 { | |
4712 AccessorConstantDescriptor d(Handle<Name>(Name::cast(error_stack->name())), | |
4713 error_stack, attribs); | |
4714 Handle<Map> new_map = Map::CopyInsertDescriptor(map, &d, INSERT_TRANSITION); | |
4715 JSObject::MigrateToMap(object, new_map, 1); | |
4716 } | |
4717 | |
4718 // Collect the stack trace. | |
4719 | |
4720 RETURN_FAILURE_ON_EXCEPTION(isolate, | |
4721 isolate->CaptureAndSetDetailedStackTrace(object)); | |
4722 RETURN_FAILURE_ON_EXCEPTION( | |
4723 isolate, isolate->CaptureAndSetSimpleStackTrace(object, caller)); | |
4724 | |
4725 return *isolate->factory()->undefined_value(); | |
4726 } | |
4727 namespace { | |
Yang
2016/07/15 13:10:11
new line above this namespace
jgruber
2016/07/18 13:06:11
Done.
| |
4728 | |
4729 MaybeHandle<String> GetStringPropertyOrDefault(Isolate* isolate, | |
4730 Handle<JSReceiver> recv, | |
4731 Handle<String> key, | |
4732 Handle<String> default_str) { | |
4733 Handle<Object> obj; | |
4734 ASSIGN_RETURN_ON_EXCEPTION(isolate, obj, JSObject::GetProperty(recv, key), | |
4735 String); | |
4736 | |
4737 Handle<String> str; | |
4738 if (obj->IsUndefined(isolate)) { | |
4739 str = default_str; | |
4740 } else { | |
4741 ASSIGN_RETURN_ON_EXCEPTION(isolate, str, Object::ToString(isolate, obj), | |
4742 String); | |
4743 } | |
4744 | |
4745 return str; | |
4746 } | |
4747 | |
4748 } // namespace | |
4749 | |
4750 // ES6 section 19.5.3.4 Error.prototype.toString ( ) | |
4751 BUILTIN(ErrorPrototypeToString) { | |
4752 HandleScope scope(isolate); | |
4753 | |
4754 // 1. Let O be the this value. | |
4755 // 2. If Type(O) is not Object, throw a TypeError exception. | |
4756 CHECK_RECEIVER(JSReceiver, receiver, "Error.prototype.toString"); | |
4757 | |
4758 // 3. Let name be ? Get(O, "name"). | |
4759 // 4. If name is undefined, let name be "Error"; otherwise let name be | |
4760 // ? ToString(name). | |
4761 Handle<String> name_key = isolate->factory()->name_string(); | |
4762 Handle<String> name_default = isolate->factory()->Error_string(); | |
4763 Handle<String> name; | |
4764 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
4765 isolate, name, | |
4766 GetStringPropertyOrDefault(isolate, receiver, name_key, name_default)); | |
4767 | |
4768 // 5. Let msg be ? Get(O, "message"). | |
4769 // 6. If msg is undefined, let msg be the empty String; otherwise let msg be | |
4770 // ? ToString(msg). | |
4771 Handle<String> msg_key = isolate->factory()->message_string(); | |
4772 Handle<String> msg_default = isolate->factory()->empty_string(); | |
4773 Handle<String> msg; | |
4774 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
4775 isolate, msg, | |
4776 GetStringPropertyOrDefault(isolate, receiver, msg_key, msg_default)); | |
4777 | |
4778 // 7. If name is the empty String, return msg. | |
4779 // 8. If msg is the empty String, return name. | |
4780 if (name->length() == 0) return *msg; | |
4781 if (msg->length() == 0) return *name; | |
4782 | |
4783 // 9. Return the result of concatenating name, the code unit 0x003A (COLON), | |
4784 // the code unit 0x0020 (SPACE), and msg. | |
4785 IncrementalStringBuilder builder(isolate); | |
4786 builder.AppendString(name); | |
4787 builder.AppendCString(": "); | |
4788 builder.AppendString(msg); | |
4789 RETURN_RESULT_OR_FAILURE(isolate, builder.Finish()); | |
4790 } | |
4791 | |
4616 namespace { | 4792 namespace { |
4617 | 4793 |
4618 // ES6 section 19.2.1.1.1 CreateDynamicFunction | 4794 // ES6 section 19.2.1.1.1 CreateDynamicFunction |
4619 MaybeHandle<JSFunction> CreateDynamicFunction(Isolate* isolate, | 4795 MaybeHandle<JSFunction> CreateDynamicFunction(Isolate* isolate, |
4620 BuiltinArguments args, | 4796 BuiltinArguments args, |
4621 const char* token) { | 4797 const char* token) { |
4622 // Compute number of arguments, ignoring the receiver. | 4798 // Compute number of arguments, ignoring the receiver. |
4623 DCHECK_LE(1, args.length()); | 4799 DCHECK_LE(1, args.length()); |
4624 int const argc = args.length() - 1; | 4800 int const argc = args.length() - 1; |
4625 | 4801 |
(...skipping 2199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
6825 #define DEFINE_BUILTIN_ACCESSOR(Name, ...) \ | 7001 #define DEFINE_BUILTIN_ACCESSOR(Name, ...) \ |
6826 Handle<Code> Builtins::Name() { \ | 7002 Handle<Code> Builtins::Name() { \ |
6827 Code** code_address = reinterpret_cast<Code**>(builtin_address(k##Name)); \ | 7003 Code** code_address = reinterpret_cast<Code**>(builtin_address(k##Name)); \ |
6828 return Handle<Code>(code_address); \ | 7004 return Handle<Code>(code_address); \ |
6829 } | 7005 } |
6830 BUILTIN_LIST_ALL(DEFINE_BUILTIN_ACCESSOR) | 7006 BUILTIN_LIST_ALL(DEFINE_BUILTIN_ACCESSOR) |
6831 #undef DEFINE_BUILTIN_ACCESSOR | 7007 #undef DEFINE_BUILTIN_ACCESSOR |
6832 | 7008 |
6833 } // namespace internal | 7009 } // namespace internal |
6834 } // namespace v8 | 7010 } // namespace v8 |
OLD | NEW |