OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/v8.h" |
| 6 |
| 7 #include "src/arguments.h" |
| 8 #include "src/bootstrapper.h" |
| 9 #include "src/runtime/runtime.h" |
| 10 #include "src/runtime/runtime-utils.h" |
| 11 |
| 12 namespace v8 { |
| 13 namespace internal { |
| 14 |
| 15 RUNTIME_FUNCTION(Runtime_CreateApiFunction) { |
| 16 HandleScope scope(isolate); |
| 17 DCHECK(args.length() == 2); |
| 18 CONVERT_ARG_HANDLE_CHECKED(FunctionTemplateInfo, data, 0); |
| 19 CONVERT_ARG_HANDLE_CHECKED(Object, prototype, 1); |
| 20 return *isolate->factory()->CreateApiFunction(data, prototype); |
| 21 } |
| 22 |
| 23 |
| 24 RUNTIME_FUNCTION(Runtime_IsTemplate) { |
| 25 SealHandleScope shs(isolate); |
| 26 DCHECK(args.length() == 1); |
| 27 CONVERT_ARG_HANDLE_CHECKED(Object, arg, 0); |
| 28 bool result = arg->IsObjectTemplateInfo() || arg->IsFunctionTemplateInfo(); |
| 29 return isolate->heap()->ToBoolean(result); |
| 30 } |
| 31 |
| 32 |
| 33 RUNTIME_FUNCTION(Runtime_GetTemplateField) { |
| 34 SealHandleScope shs(isolate); |
| 35 DCHECK(args.length() == 2); |
| 36 CONVERT_ARG_CHECKED(HeapObject, templ, 0); |
| 37 CONVERT_SMI_ARG_CHECKED(index, 1); |
| 38 int offset = index * kPointerSize + HeapObject::kHeaderSize; |
| 39 InstanceType type = templ->map()->instance_type(); |
| 40 RUNTIME_ASSERT(type == FUNCTION_TEMPLATE_INFO_TYPE || |
| 41 type == OBJECT_TEMPLATE_INFO_TYPE); |
| 42 RUNTIME_ASSERT(offset > 0); |
| 43 if (type == FUNCTION_TEMPLATE_INFO_TYPE) { |
| 44 RUNTIME_ASSERT(offset < FunctionTemplateInfo::kSize); |
| 45 } else { |
| 46 RUNTIME_ASSERT(offset < ObjectTemplateInfo::kSize); |
| 47 } |
| 48 return *HeapObject::RawField(templ, offset); |
| 49 } |
| 50 |
| 51 |
| 52 // Transform getter or setter into something DefineAccessor can handle. |
| 53 static Handle<Object> InstantiateAccessorComponent(Isolate* isolate, |
| 54 Handle<Object> component) { |
| 55 if (component->IsUndefined()) return isolate->factory()->undefined_value(); |
| 56 Handle<FunctionTemplateInfo> info = |
| 57 Handle<FunctionTemplateInfo>::cast(component); |
| 58 return Utils::OpenHandle(*Utils::ToLocal(info)->GetFunction()); |
| 59 } |
| 60 |
| 61 |
| 62 RUNTIME_FUNCTION(Runtime_DefineApiAccessorProperty) { |
| 63 HandleScope scope(isolate); |
| 64 DCHECK(args.length() == 5); |
| 65 CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); |
| 66 CONVERT_ARG_HANDLE_CHECKED(Name, name, 1); |
| 67 CONVERT_ARG_HANDLE_CHECKED(Object, getter, 2); |
| 68 CONVERT_ARG_HANDLE_CHECKED(Object, setter, 3); |
| 69 CONVERT_SMI_ARG_CHECKED(attribute, 4); |
| 70 RUNTIME_ASSERT(getter->IsUndefined() || getter->IsFunctionTemplateInfo()); |
| 71 RUNTIME_ASSERT(setter->IsUndefined() || setter->IsFunctionTemplateInfo()); |
| 72 RUNTIME_ASSERT(PropertyDetails::AttributesField::is_valid( |
| 73 static_cast<PropertyAttributes>(attribute))); |
| 74 RETURN_FAILURE_ON_EXCEPTION( |
| 75 isolate, JSObject::DefineAccessor( |
| 76 object, name, InstantiateAccessorComponent(isolate, getter), |
| 77 InstantiateAccessorComponent(isolate, setter), |
| 78 static_cast<PropertyAttributes>(attribute))); |
| 79 return isolate->heap()->undefined_value(); |
| 80 } |
| 81 |
| 82 |
| 83 RUNTIME_FUNCTION(Runtime_AddPropertyForTemplate) { |
| 84 HandleScope scope(isolate); |
| 85 RUNTIME_ASSERT(args.length() == 4); |
| 86 |
| 87 CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); |
| 88 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
| 89 CONVERT_ARG_HANDLE_CHECKED(Object, value, 2); |
| 90 CONVERT_SMI_ARG_CHECKED(unchecked_attributes, 3); |
| 91 RUNTIME_ASSERT( |
| 92 (unchecked_attributes & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 93 // Compute attributes. |
| 94 PropertyAttributes attributes = |
| 95 static_cast<PropertyAttributes>(unchecked_attributes); |
| 96 |
| 97 #ifdef DEBUG |
| 98 bool duplicate; |
| 99 if (key->IsName()) { |
| 100 LookupIterator it(object, Handle<Name>::cast(key), |
| 101 LookupIterator::OWN_SKIP_INTERCEPTOR); |
| 102 Maybe<PropertyAttributes> maybe = JSReceiver::GetPropertyAttributes(&it); |
| 103 DCHECK(maybe.has_value); |
| 104 duplicate = it.IsFound(); |
| 105 } else { |
| 106 uint32_t index = 0; |
| 107 RUNTIME_ASSERT(key->ToArrayIndex(&index)); |
| 108 Maybe<bool> maybe = JSReceiver::HasOwnElement(object, index); |
| 109 if (!maybe.has_value) return isolate->heap()->exception(); |
| 110 duplicate = maybe.value; |
| 111 } |
| 112 if (duplicate) { |
| 113 Handle<Object> args[1] = {key}; |
| 114 THROW_NEW_ERROR_RETURN_FAILURE( |
| 115 isolate, |
| 116 NewTypeError("duplicate_template_property", HandleVector(args, 1))); |
| 117 } |
| 118 #endif |
| 119 |
| 120 Handle<Object> result; |
| 121 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 122 isolate, result, |
| 123 Runtime::DefineObjectProperty(object, key, value, attributes)); |
| 124 return *result; |
| 125 } |
| 126 } |
| 127 } // namespace v8::internal |
OLD | NEW |