V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
factory-inl.h
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 #ifndef V8_HEAP_FACTORY_INL_H_
6 #define V8_HEAP_FACTORY_INL_H_
7 
8 #include "src/heap/factory.h"
9 
10 // Clients of this interface shouldn't depend on lots of heap internals.
11 // Do not include anything from src/heap here!
12 #include "src/handles-inl.h"
13 #include "src/objects-inl.h"
14 #include "src/string-hasher.h"
15 
16 namespace v8 {
17 namespace internal {
18 
19 // TODO(jkummerow): Drop std::remove_pointer after the migration to ObjectPtr.
20 #define ROOT_ACCESSOR(Type, name, CamelName) \
21  Handle<std::remove_pointer<Type>::type> Factory::name() { \
22  return Handle<std::remove_pointer<Type>::type>(bit_cast<Address*>( \
23  &isolate()->roots_table()[RootIndex::k##CamelName])); \
24  }
25 ROOT_LIST(ROOT_ACCESSOR)
26 #undef ROOT_ACCESSOR
27 
28 Handle<String> Factory::InternalizeString(Handle<String> string) {
29  if (string->IsInternalizedString()) return string;
30  return StringTable::LookupString(isolate(), string);
31 }
32 
33 Handle<Name> Factory::InternalizeName(Handle<Name> name) {
34  if (name->IsUniqueName()) return name;
35  return StringTable::LookupString(isolate(), Handle<String>::cast(name));
36 }
37 
38 Handle<String> Factory::NewSubString(Handle<String> str, int begin, int end) {
39  if (begin == 0 && end == str->length()) return str;
40  return NewProperSubString(str, begin, end);
41 }
42 
43 Handle<Object> Factory::NewNumberFromSize(size_t value,
44  PretenureFlag pretenure) {
45  // We can't use Smi::IsValid() here because that operates on a signed
46  // intptr_t, and casting from size_t could create a bogus sign bit.
47  if (value <= static_cast<size_t>(Smi::kMaxValue)) {
48  return Handle<Object>(Smi::FromIntptr(static_cast<intptr_t>(value)),
49  isolate());
50  }
51  return NewNumber(static_cast<double>(value), pretenure);
52 }
53 
54 Handle<Object> Factory::NewNumberFromInt64(int64_t value,
55  PretenureFlag pretenure) {
56  if (value <= std::numeric_limits<int32_t>::max() &&
57  value >= std::numeric_limits<int32_t>::min() &&
58  Smi::IsValid(static_cast<int32_t>(value))) {
59  return Handle<Object>(Smi::FromInt(static_cast<int32_t>(value)), isolate());
60  }
61  return NewNumber(static_cast<double>(value), pretenure);
62 }
63 
64 Handle<HeapNumber> Factory::NewHeapNumber(double value,
65  PretenureFlag pretenure) {
66  Handle<HeapNumber> heap_number = NewHeapNumber(pretenure);
67  heap_number->set_value(value);
68  return heap_number;
69 }
70 
71 Handle<MutableHeapNumber> Factory::NewMutableHeapNumber(
72  double value, PretenureFlag pretenure) {
73  Handle<MutableHeapNumber> number = NewMutableHeapNumber(pretenure);
74  number->set_value(value);
75  return number;
76 }
77 
78 Handle<HeapNumber> Factory::NewHeapNumberFromBits(uint64_t bits,
79  PretenureFlag pretenure) {
80  Handle<HeapNumber> heap_number = NewHeapNumber(pretenure);
81  heap_number->set_value_as_bits(bits);
82  return heap_number;
83 }
84 
85 Handle<MutableHeapNumber> Factory::NewMutableHeapNumberFromBits(
86  uint64_t bits, PretenureFlag pretenure) {
87  Handle<MutableHeapNumber> number = NewMutableHeapNumber(pretenure);
88  number->set_value_as_bits(bits);
89  return number;
90 }
91 
92 Handle<MutableHeapNumber> Factory::NewMutableHeapNumberWithHoleNaN(
93  PretenureFlag pretenure) {
94  return NewMutableHeapNumberFromBits(kHoleNanInt64, pretenure);
95 }
96 
97 Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements,
98  ElementsKind elements_kind,
99  PretenureFlag pretenure) {
100  return NewJSArrayWithElements(elements, elements_kind, elements->length(),
101  pretenure);
102 }
103 
104 Handle<Object> Factory::NewURIError() {
105  return NewError(isolate()->uri_error_function(),
106  MessageTemplate::kURIMalformed);
107 }
108 
109 Handle<String> Factory::Uint32ToString(uint32_t value, bool check_cache) {
110  Handle<String> result;
111  int32_t int32v = static_cast<int32_t>(value);
112  if (int32v >= 0 && Smi::IsValid(int32v)) {
113  result = NumberToString(Smi::FromInt(int32v), check_cache);
114  } else {
115  result = NumberToString(NewNumberFromUint(value), check_cache);
116  }
117 
118  if (result->length() <= String::kMaxArrayIndexSize &&
119  result->hash_field() == String::kEmptyHashField) {
120  uint32_t field = StringHasher::MakeArrayIndexHash(value, result->length());
121  result->set_hash_field(field);
122  }
123  return result;
124 }
125 
126 } // namespace internal
127 } // namespace v8
128 
129 #endif // V8_HEAP_FACTORY_INL_H_
Definition: libplatform.h:13