• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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/objects/template-objects.h"
6 
7 #include "src/heap/factory.h"
8 #include "src/isolate.h"
9 #include "src/objects-inl.h"
10 #include "src/property-descriptor.h"
11 
12 namespace v8 {
13 namespace internal {
14 
15 // static
CreateTemplateObject(Isolate * isolate,Handle<TemplateObjectDescription> description)16 Handle<JSArray> TemplateObjectDescription::CreateTemplateObject(
17     Isolate* isolate, Handle<TemplateObjectDescription> description) {
18   // Create the raw object from the {raw_strings}.
19   Handle<FixedArray> raw_strings(description->raw_strings(), isolate);
20   Handle<JSArray> raw_object = isolate->factory()->NewJSArrayWithElements(
21       raw_strings, PACKED_ELEMENTS, raw_strings->length(), TENURED);
22 
23   // Create the template object from the {cooked_strings}.
24   Handle<FixedArray> cooked_strings(description->cooked_strings(), isolate);
25   Handle<JSArray> template_object = isolate->factory()->NewJSArrayWithElements(
26       cooked_strings, PACKED_ELEMENTS, cooked_strings->length(), TENURED);
27 
28   // Freeze the {raw_object}.
29   JSObject::SetIntegrityLevel(raw_object, FROZEN, kThrowOnError).ToChecked();
30 
31   // Install a "raw" data property for {raw_object} on {template_object}.
32   PropertyDescriptor raw_desc;
33   raw_desc.set_value(raw_object);
34   raw_desc.set_configurable(false);
35   raw_desc.set_enumerable(false);
36   raw_desc.set_writable(false);
37   JSArray::DefineOwnProperty(isolate, template_object,
38                              isolate->factory()->raw_string(), &raw_desc,
39                              kThrowOnError)
40       .ToChecked();
41 
42   // Freeze the {template_object} as well.
43   JSObject::SetIntegrityLevel(template_object, FROZEN, kThrowOnError)
44       .ToChecked();
45 
46   return template_object;
47 }
48 
49 }  // namespace internal
50 }  // namespace v8
51