OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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/api-natives.h" | 5 #include "src/api-natives.h" |
6 #include "src/isolate-inl.h" | 6 #include "src/isolate-inl.h" |
7 | 7 |
8 namespace v8 { | 8 namespace v8 { |
9 namespace internal { | 9 namespace internal { |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 // Transform getter or setter into something DefineAccessor can handle. | 13 MaybeHandle<JSObject> InstantiateObject(Isolate* isolate, |
14 Handle<Object> InstantiateAccessorComponent(Isolate* isolate, | 14 Handle<ObjectTemplateInfo> data); |
15 Handle<Object> component) { | 15 |
16 if (component->IsUndefined()) return isolate->factory()->undefined_value(); | 16 |
17 Handle<FunctionTemplateInfo> info = | 17 MaybeHandle<JSFunction> InstantiateFunction(Isolate* isolate, |
18 Handle<FunctionTemplateInfo>::cast(component); | 18 Handle<FunctionTemplateInfo> data, |
19 // TODO(dcarney): instantiate directly. | 19 Handle<Name> name = Handle<Name>()); |
20 return Utils::OpenHandle(*Utils::ToLocal(info)->GetFunction()); | 20 |
| 21 |
| 22 MaybeHandle<Object> Instantiate(Isolate* isolate, Handle<Object> data, |
| 23 Handle<Name> name = Handle<Name>()) { |
| 24 if (data->IsFunctionTemplateInfo()) { |
| 25 return InstantiateFunction(isolate, |
| 26 Handle<FunctionTemplateInfo>::cast(data), name); |
| 27 } else if (data->IsObjectTemplateInfo()) { |
| 28 return InstantiateObject(isolate, Handle<ObjectTemplateInfo>::cast(data)); |
| 29 } else { |
| 30 return data; |
| 31 } |
21 } | 32 } |
22 | 33 |
23 | 34 |
24 MaybeHandle<Object> DefineApiAccessorProperty( | 35 MaybeHandle<Object> DefineAccessorProperty( |
25 Isolate* isolate, Handle<JSObject> object, Handle<Name> name, | 36 Isolate* isolate, Handle<JSObject> object, Handle<Name> name, |
26 Handle<Object> getter, Handle<Object> setter, Smi* attribute) { | 37 Handle<Object> getter, Handle<Object> setter, Smi* attributes) { |
27 DCHECK(PropertyDetails::AttributesField::is_valid( | 38 DCHECK(PropertyDetails::AttributesField::is_valid( |
28 static_cast<PropertyAttributes>(attribute->value()))); | 39 static_cast<PropertyAttributes>(attributes->value()))); |
29 RETURN_ON_EXCEPTION( | 40 if (!getter->IsUndefined()) { |
30 isolate, JSObject::DefineAccessor( | 41 ASSIGN_RETURN_ON_EXCEPTION( |
31 object, name, InstantiateAccessorComponent(isolate, getter), | 42 isolate, getter, |
32 InstantiateAccessorComponent(isolate, setter), | 43 InstantiateFunction(isolate, |
33 static_cast<PropertyAttributes>(attribute->value())), | 44 Handle<FunctionTemplateInfo>::cast(getter)), |
34 Object); | 45 Object); |
| 46 } |
| 47 if (!setter->IsUndefined()) { |
| 48 ASSIGN_RETURN_ON_EXCEPTION( |
| 49 isolate, setter, |
| 50 InstantiateFunction(isolate, |
| 51 Handle<FunctionTemplateInfo>::cast(setter)), |
| 52 Object); |
| 53 } |
| 54 RETURN_ON_EXCEPTION(isolate, |
| 55 JSObject::DefineAccessor( |
| 56 object, name, getter, setter, |
| 57 static_cast<PropertyAttributes>(attributes->value())), |
| 58 Object); |
35 return object; | 59 return object; |
36 } | 60 } |
37 | 61 |
38 | 62 |
39 MaybeHandle<Object> AddPropertyForTemplate(Isolate* isolate, | 63 MaybeHandle<Object> DefineDataProperty(Isolate* isolate, |
40 Handle<JSObject> object, | 64 Handle<JSObject> object, |
41 Handle<Object> key, | 65 Handle<Name> key, |
42 Handle<Object> value, | 66 Handle<Object> prop_data, |
43 Smi* unchecked_attributes) { | 67 Smi* unchecked_attributes) { |
44 DCHECK((unchecked_attributes->value() & | 68 DCHECK((unchecked_attributes->value() & |
45 ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); | 69 ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
46 // Compute attributes. | 70 // Compute attributes. |
47 PropertyAttributes attributes = | 71 PropertyAttributes attributes = |
48 static_cast<PropertyAttributes>(unchecked_attributes->value()); | 72 static_cast<PropertyAttributes>(unchecked_attributes->value()); |
49 | 73 |
| 74 Handle<Object> value; |
| 75 ASSIGN_RETURN_ON_EXCEPTION(isolate, value, |
| 76 Instantiate(isolate, prop_data, key), Object); |
| 77 |
50 #ifdef DEBUG | 78 #ifdef DEBUG |
51 bool duplicate; | 79 bool duplicate; |
52 if (key->IsName()) { | 80 if (key->IsName()) { |
53 LookupIterator it(object, Handle<Name>::cast(key), | 81 LookupIterator it(object, Handle<Name>::cast(key), |
54 LookupIterator::OWN_SKIP_INTERCEPTOR); | 82 LookupIterator::OWN_SKIP_INTERCEPTOR); |
55 Maybe<PropertyAttributes> maybe = JSReceiver::GetPropertyAttributes(&it); | 83 Maybe<PropertyAttributes> maybe = JSReceiver::GetPropertyAttributes(&it); |
56 DCHECK(maybe.has_value); | 84 DCHECK(maybe.has_value); |
57 duplicate = it.IsFound(); | 85 duplicate = it.IsFound(); |
58 } else { | 86 } else { |
59 uint32_t index = 0; | 87 uint32_t index = 0; |
(...skipping 28 matching lines...) Expand all Loading... |
88 | 116 |
89 void EnableAccessChecks(Isolate* isolate, Handle<JSObject> object) { | 117 void EnableAccessChecks(Isolate* isolate, Handle<JSObject> object) { |
90 Handle<Map> old_map(object->map()); | 118 Handle<Map> old_map(object->map()); |
91 // Copy map so it won't interfere constructor's initial map. | 119 // Copy map so it won't interfere constructor's initial map. |
92 Handle<Map> new_map = Map::Copy(old_map, "EnableAccessChecks"); | 120 Handle<Map> new_map = Map::Copy(old_map, "EnableAccessChecks"); |
93 new_map->set_is_access_check_needed(true); | 121 new_map->set_is_access_check_needed(true); |
94 JSObject::MigrateToMap(object, new_map); | 122 JSObject::MigrateToMap(object, new_map); |
95 } | 123 } |
96 | 124 |
97 | 125 |
98 MaybeHandle<JSObject> InstantiateObject(Isolate* isolate, | |
99 Handle<ObjectTemplateInfo> data); | |
100 | |
101 | |
102 MaybeHandle<JSFunction> InstantiateFunction(Isolate* isolate, | |
103 Handle<FunctionTemplateInfo> data, | |
104 Handle<Name> name = Handle<Name>()); | |
105 | |
106 | |
107 MaybeHandle<Object> Instantiate(Isolate* isolate, Handle<Object> data, | |
108 Handle<Name> name = Handle<Name>()) { | |
109 if (data->IsFunctionTemplateInfo()) { | |
110 return InstantiateFunction(isolate, | |
111 Handle<FunctionTemplateInfo>::cast(data), name); | |
112 } else if (data->IsObjectTemplateInfo()) { | |
113 return InstantiateObject(isolate, Handle<ObjectTemplateInfo>::cast(data)); | |
114 } else { | |
115 // TODO(dcarney): CHECK data is JSObject or Primitive. | |
116 return data; | |
117 } | |
118 } | |
119 | |
120 | |
121 class AccessCheckDisableScope { | 126 class AccessCheckDisableScope { |
122 public: | 127 public: |
123 AccessCheckDisableScope(Isolate* isolate, Handle<JSObject> obj) | 128 AccessCheckDisableScope(Isolate* isolate, Handle<JSObject> obj) |
124 : isolate_(isolate), | 129 : isolate_(isolate), |
125 disabled_(obj->map()->is_access_check_needed()), | 130 disabled_(obj->map()->is_access_check_needed()), |
126 obj_(obj) { | 131 obj_(obj) { |
127 if (disabled_) { | 132 if (disabled_) { |
128 DisableAccessChecks(isolate_, obj_); | 133 DisableAccessChecks(isolate_, obj_); |
129 } | 134 } |
130 } | 135 } |
(...skipping 19 matching lines...) Expand all Loading... |
150 if (properties.length() == 0) return obj; | 155 if (properties.length() == 0) return obj; |
151 HandleScope scope(isolate); | 156 HandleScope scope(isolate); |
152 // Disable access checks while instantiating the object. | 157 // Disable access checks while instantiating the object. |
153 AccessCheckDisableScope access_check_scope(isolate, obj); | 158 AccessCheckDisableScope access_check_scope(isolate, obj); |
154 for (int i = 0; i < properties.length();) { | 159 for (int i = 0; i < properties.length();) { |
155 int length = Smi::cast(properties.get(i))->value(); | 160 int length = Smi::cast(properties.get(i))->value(); |
156 if (length == 3) { | 161 if (length == 3) { |
157 auto name = handle(Name::cast(properties.get(i + 1)), isolate); | 162 auto name = handle(Name::cast(properties.get(i + 1)), isolate); |
158 auto prop_data = handle(properties.get(i + 2), isolate); | 163 auto prop_data = handle(properties.get(i + 2), isolate); |
159 auto attributes = Smi::cast(properties.get(i + 3)); | 164 auto attributes = Smi::cast(properties.get(i + 3)); |
160 Handle<Object> value; | 165 RETURN_ON_EXCEPTION(isolate, DefineDataProperty(isolate, obj, name, |
161 ASSIGN_RETURN_ON_EXCEPTION( | 166 prop_data, attributes), |
162 isolate, value, Instantiate(isolate, prop_data, name), JSObject); | |
163 RETURN_ON_EXCEPTION(isolate, AddPropertyForTemplate(isolate, obj, name, | |
164 value, attributes), | |
165 JSObject); | 167 JSObject); |
166 } else { | 168 } else { |
167 DCHECK(length == 4 || length == 5); | 169 DCHECK(length == 4); |
168 // TODO(verwaest): The 5th value used to be access_control. Remove once | |
169 // the bindings are updated. | |
170 auto name = handle(Name::cast(properties.get(i + 1)), isolate); | 170 auto name = handle(Name::cast(properties.get(i + 1)), isolate); |
171 auto getter = handle(properties.get(i + 2), isolate); | 171 auto getter = handle(properties.get(i + 2), isolate); |
172 auto setter = handle(properties.get(i + 3), isolate); | 172 auto setter = handle(properties.get(i + 3), isolate); |
173 auto attributes = Smi::cast(properties.get(i + 4)); | 173 auto attributes = Smi::cast(properties.get(i + 4)); |
174 RETURN_ON_EXCEPTION(isolate, | 174 RETURN_ON_EXCEPTION(isolate, |
175 DefineApiAccessorProperty(isolate, obj, name, getter, | 175 DefineAccessorProperty(isolate, obj, name, getter, |
176 setter, attributes), | 176 setter, attributes), |
177 JSObject); | 177 JSObject); |
178 } | 178 } |
179 i += length + 1; | 179 i += length + 1; |
180 } | 180 } |
181 return obj; | 181 return obj; |
182 } | 182 } |
183 | 183 |
184 | 184 |
185 MaybeHandle<JSObject> InstantiateObject(Isolate* isolate, | 185 MaybeHandle<JSObject> InstantiateObject(Isolate* isolate, |
186 Handle<ObjectTemplateInfo> data) { | 186 Handle<ObjectTemplateInfo> data) { |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 } else { | 306 } else { |
307 isolate_->clear_pending_message(); | 307 isolate_->clear_pending_message(); |
308 } | 308 } |
309 } | 309 } |
310 | 310 |
311 private: | 311 private: |
312 Isolate* isolate_; | 312 Isolate* isolate_; |
313 SaveContext save_context_; | 313 SaveContext save_context_; |
314 }; | 314 }; |
315 | 315 |
| 316 |
| 317 void AddPropertyToPropertyList(Isolate* isolate, Handle<TemplateInfo> templ, |
| 318 int length, Handle<Object>* data) { |
| 319 auto list = handle(templ->property_list(), isolate); |
| 320 if (list->IsUndefined()) { |
| 321 list = NeanderArray(isolate).value(); |
| 322 templ->set_property_list(*list); |
| 323 } |
| 324 NeanderArray array(list); |
| 325 array.add(isolate, isolate->factory()->NewNumberFromInt(length)); |
| 326 for (int i = 0; i < length; i++) { |
| 327 Handle<Object> value = |
| 328 data[i].is_null() |
| 329 ? Handle<Object>::cast(isolate->factory()->undefined_value()) |
| 330 : data[i]; |
| 331 array.add(isolate, value); |
| 332 } |
| 333 } |
| 334 |
316 } // namespace | 335 } // namespace |
317 | 336 |
318 | 337 |
319 MaybeHandle<JSFunction> ApiNatives::InstantiateFunction( | 338 MaybeHandle<JSFunction> ApiNatives::InstantiateFunction( |
320 Handle<FunctionTemplateInfo> data) { | 339 Handle<FunctionTemplateInfo> data) { |
321 Isolate* isolate = data->GetIsolate(); | 340 Isolate* isolate = data->GetIsolate(); |
322 InvokeScope invoke_scope(isolate); | 341 InvokeScope invoke_scope(isolate); |
323 return ::v8::internal::InstantiateFunction(isolate, data); | 342 return ::v8::internal::InstantiateFunction(isolate, data); |
324 } | 343 } |
325 | 344 |
(...skipping 15 matching lines...) Expand all Loading... |
341 InvokeScope invoke_scope(isolate); | 360 InvokeScope invoke_scope(isolate); |
342 Handle<ObjectTemplateInfo> instance_template( | 361 Handle<ObjectTemplateInfo> instance_template( |
343 ObjectTemplateInfo::cast(desc->instance_template()), isolate); | 362 ObjectTemplateInfo::cast(desc->instance_template()), isolate); |
344 RETURN_ON_EXCEPTION(isolate, ::v8::internal::ConfigureInstance( | 363 RETURN_ON_EXCEPTION(isolate, ::v8::internal::ConfigureInstance( |
345 isolate, instance, instance_template), | 364 isolate, instance, instance_template), |
346 FunctionTemplateInfo); | 365 FunctionTemplateInfo); |
347 return desc; | 366 return desc; |
348 } | 367 } |
349 | 368 |
350 | 369 |
| 370 void ApiNatives::AddDataProperty(Isolate* isolate, Handle<TemplateInfo> info, |
| 371 Handle<Name> name, Handle<Object> value, |
| 372 PropertyAttributes attributes) { |
| 373 const int kSize = 3; |
| 374 DCHECK(Smi::IsValid(static_cast<int>(attributes))); |
| 375 auto attribute_handle = |
| 376 handle(Smi::FromInt(static_cast<int>(attributes)), isolate); |
| 377 Handle<Object> data[kSize] = {name, value, attribute_handle}; |
| 378 AddPropertyToPropertyList(isolate, info, kSize, data); |
| 379 } |
| 380 |
| 381 |
| 382 void ApiNatives::AddAccessorProperty(Isolate* isolate, |
| 383 Handle<TemplateInfo> info, |
| 384 Handle<Name> name, |
| 385 Handle<FunctionTemplateInfo> getter, |
| 386 Handle<FunctionTemplateInfo> setter, |
| 387 PropertyAttributes attributes) { |
| 388 const int kSize = 4; |
| 389 DCHECK(Smi::IsValid(static_cast<int>(attributes))); |
| 390 auto attribute_handle = |
| 391 handle(Smi::FromInt(static_cast<int>(attributes)), isolate); |
| 392 Handle<Object> data[kSize] = {name, getter, setter, attribute_handle}; |
| 393 AddPropertyToPropertyList(isolate, info, kSize, data); |
| 394 } |
| 395 |
| 396 |
| 397 void ApiNatives::AddNativeDataProperty(Isolate* isolate, |
| 398 Handle<TemplateInfo> info, |
| 399 Handle<AccessorInfo> property) { |
| 400 auto list = handle(info->property_accessors(), isolate); |
| 401 if (list->IsUndefined()) { |
| 402 list = NeanderArray(isolate).value(); |
| 403 info->set_property_accessors(*list); |
| 404 } |
| 405 NeanderArray array(list); |
| 406 array.add(isolate, property); |
| 407 } |
| 408 |
| 409 |
351 Handle<JSFunction> ApiNatives::CreateApiFunction( | 410 Handle<JSFunction> ApiNatives::CreateApiFunction( |
352 Isolate* isolate, Handle<FunctionTemplateInfo> obj, | 411 Isolate* isolate, Handle<FunctionTemplateInfo> obj, |
353 Handle<Object> prototype, ApiInstanceType instance_type) { | 412 Handle<Object> prototype, ApiInstanceType instance_type) { |
354 Handle<Code> code = isolate->builtins()->HandleApiCall(); | 413 Handle<Code> code = isolate->builtins()->HandleApiCall(); |
355 Handle<Code> construct_stub = isolate->builtins()->JSConstructStubApi(); | 414 Handle<Code> construct_stub = isolate->builtins()->JSConstructStubApi(); |
356 | 415 |
357 obj->set_instantiated(true); | 416 obj->set_instantiated(true); |
358 Handle<JSFunction> result; | 417 Handle<JSFunction> result; |
359 if (obj->remove_prototype()) { | 418 if (obj->remove_prototype()) { |
360 result = isolate->factory()->NewFunctionWithoutPrototype( | 419 result = isolate->factory()->NewFunctionWithoutPrototype( |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 Handle<AccessorInfo> accessor(AccessorInfo::cast(array->get(i))); | 579 Handle<AccessorInfo> accessor(AccessorInfo::cast(array->get(i))); |
521 JSObject::SetAccessor(result, accessor).Assert(); | 580 JSObject::SetAccessor(result, accessor).Assert(); |
522 } | 581 } |
523 | 582 |
524 DCHECK(result->shared()->IsApiFunction()); | 583 DCHECK(result->shared()->IsApiFunction()); |
525 return result; | 584 return result; |
526 } | 585 } |
527 | 586 |
528 } // namespace internal | 587 } // namespace internal |
529 } // namespace v8 | 588 } // namespace v8 |
OLD | NEW |