v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
option-utils.h
Go to the documentation of this file.
1// Copyright 2021 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_OBJECTS_OPTION_UTILS_H_
6#define V8_OBJECTS_OPTION_UTILS_H_
7
11#include "src/objects/string.h"
12
13namespace v8 {
14namespace internal {
15
16// ecma402/#sec-getoptionsobject and temporal/#sec-getoptionsobject
17V8_WARN_UNUSED_RESULT MaybeDirectHandle<JSReceiver> GetOptionsObject(
18 Isolate* isolate, DirectHandle<Object> options, const char* method_name);
19
20// ecma402/#sec-coerceoptionstoobject
21V8_WARN_UNUSED_RESULT MaybeDirectHandle<JSReceiver> CoerceOptionsToObject(
22 Isolate* isolate, DirectHandle<Object> options, const char* method_name);
23
24// ECMA402 9.2.10. GetOption( options, property, type, values, fallback)
25// ecma402/#sec-getoption and temporal/#sec-getoption
26//
27// This is specialized for the case when type is string.
28//
29// Instead of passing undefined for the values argument as the spec
30// defines, pass in an empty vector.
31//
32// Returns true if options object has the property and stores the
33// result in value. Returns false if the value is not found. The
34// caller is required to use fallback value appropriately in this
35// case.
36//
37// method_name is a string denoting the method the call from; used when
38// printing the error message.
40 Isolate* isolate, DirectHandle<JSReceiver> options, const char* property,
41 const std::vector<const char*>& values, const char* method_name,
42 std::unique_ptr<char[]>* result);
43
44// A helper template to get string from option into a enum.
45// The enum in the enum_values is the corresponding value to the strings
46// in the str_values. If the option does not contains name,
47// default_value will be return.
48template <typename T>
50 Isolate* isolate, DirectHandle<JSReceiver> options, const char* name,
51 const char* method_name, const std::vector<const char*>& str_values,
52 const std::vector<T>& enum_values, T default_value) {
53 DCHECK_EQ(str_values.size(), enum_values.size());
54 std::unique_ptr<char[]> cstr;
55 Maybe<bool> found =
56 GetStringOption(isolate, options, name, str_values, method_name, &cstr);
57 MAYBE_RETURN(found, Nothing<T>());
58 if (found.FromJust()) {
59 DCHECK_NOT_NULL(cstr.get());
60 for (size_t i = 0; i < str_values.size(); i++) {
61 if (strcmp(cstr.get(), str_values[i]) == 0) {
62 return Just(enum_values[i]);
63 }
64 }
66 }
67 return Just(default_value);
68}
69
70// A helper template to get string from option into a enum.
71// The enum in the enum_values is the corresponding value to the strings
72// in the str_values. If the option does not contains name,
73// default_value will be return.
74template <typename T>
76 Isolate* isolate, DirectHandle<JSReceiver> options, const char* property,
77 const char* method, const std::vector<const char*>& str_values,
78 const std::vector<T>& enum_values, T true_value, T false_value,
79 T fallback_value) {
80 DCHECK_EQ(str_values.size(), enum_values.size());
81 Factory* factory = isolate->factory();
82 DirectHandle<String> property_str =
83 factory->NewStringFromAsciiChecked(property);
84
85 // 1. Let value be ? Get(options, property).
88 isolate, value,
89 Object::GetPropertyOrElement(isolate, options, property_str),
90 Nothing<T>());
91 // 2. If value is undefined, then return fallback.
92 if (IsUndefined(*value, isolate)) {
93 return Just(fallback_value);
94 }
95 // 3. If value is true, then return trueValue.
96 if (IsTrue(*value, isolate)) {
97 return Just(true_value);
98 }
99 // 4. Let valueBoolean be ToBoolean(value).
100 bool valueBoolean = Object::BooleanValue(*value, isolate);
101 // 5. If valueBoolean is false, then return valueBoolean.
102 if (!valueBoolean) {
103 return Just(false_value);
104 }
105
106 DirectHandle<String> value_str;
107 // 6. Let value be ? ToString(value).
109 isolate, value_str, Object::ToString(isolate, value), Nothing<T>());
110 // 7. If value is *"true"* or *"false"*, return _fallback_.
111 if (String::Equals(isolate, value_str, factory->true_string()) ||
112 String::Equals(isolate, value_str, factory->false_string())) {
113 return Just(fallback_value);
114 }
115 // 8. If values does not contain an element equal to _value_, throw a
116 // *RangeError* exception.
117 // 9. Return value.
118 value_str = String::Flatten(isolate, value_str);
119 {
121 const String::FlatContent& flat = value_str->GetFlatContent(no_gc);
122 int32_t length = value_str->length();
123 for (size_t i = 0; i < str_values.size(); i++) {
124 if (static_cast<int32_t>(strlen(str_values.at(i))) == length) {
125 if (flat.IsOneByte()) {
126 if (CompareCharsEqual(str_values.at(i),
127 flat.ToOneByteVector().begin(), length)) {
128 return Just(enum_values[i]);
129 }
130 } else {
131 if (CompareCharsEqual(str_values.at(i), flat.ToUC16Vector().begin(),
132 length)) {
133 return Just(enum_values[i]);
134 }
135 }
136 }
137 }
138 } // end of no_gc
140 isolate,
141 NewRangeError(MessageTemplate::kValueOutOfRange, value,
142 factory->NewStringFromAsciiChecked(method), property_str),
143 Nothing<T>());
144}
145
146// ECMA402 9.2.10. GetOption( options, property, type, values, fallback)
147// ecma402/#sec-getoption
148//
149// This is specialized for the case when type is boolean.
150//
151// Returns true if options object has the property and stores the
152// result in value. Returns false if the value is not found. The
153// caller is required to use fallback value appropriately in this
154// case.
155//
156// method_name is a string denoting the method it called from; used when
157// printing the error message.
159 Isolate* isolate, DirectHandle<JSReceiver> options, const char* property,
160 const char* method_name, bool* result);
161
163 Isolate* isolate, DirectHandle<JSReceiver> options,
164 DirectHandle<String> property, int min, int max, int fallback);
165
166// #sec-getoption while type is "number"
168 Isolate* isolate, DirectHandle<JSReceiver> options,
169 DirectHandle<String> property, double default_value);
170
171// ecma402/#sec-defaultnumberoption
173 Isolate* isolate, DirectHandle<Object> value, int min, int max,
174 int fallback, DirectHandle<String> property);
175
176} // namespace internal
177} // namespace v8
178#endif // V8_OBJECTS_OPTION_UTILS_H_
V8_INLINE T FromJust() const &
Definition v8-maybe.h:64
constexpr T * begin() const
Definition vector.h:96
Handle< String > NewStringFromAsciiChecked(const char *str, AllocationType allocation=AllocationType::kYoung)
static V8_WARN_UNUSED_RESULT HandleType< String >::MaybeType ToString(Isolate *isolate, HandleType< T > input)
static V8_WARN_UNUSED_RESULT MaybeHandle< Object > GetPropertyOrElement(Isolate *isolate, DirectHandle< JSAny > object, DirectHandle< Name > name)
static V8_EXPORT_PRIVATE bool BooleanValue(Tagged< Object > obj, IsolateT *isolate)
base::Vector< const uint8_t > ToOneByteVector() const
Definition string.h:139
base::Vector< const base::uc16 > ToUC16Vector() const
Definition string.h:145
static V8_INLINE HandleType< String > Flatten(Isolate *isolate, HandleType< T > string, AllocationType allocation=AllocationType::kYoung)
bool Equals(Tagged< String > other) const
Definition string-inl.h:535
#define THROW_NEW_ERROR_RETURN_VALUE(isolate, call, value)
Definition isolate.h:300
#define ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, dst, call, value)
Definition isolate.h:276
#define MAYBE_RETURN(call, value)
Definition isolate.h:408
ZoneVector< RpoNumber > & result
Maybe< double > GetNumberOptionAsDouble(Isolate *isolate, DirectHandle< JSReceiver > options, DirectHandle< String > property, double default_value)
Maybe< int > DefaultNumberOption(Isolate *isolate, DirectHandle< Object > value, int min, int max, int fallback, DirectHandle< String > property)
MaybeDirectHandle< JSReceiver > GetOptionsObject(Isolate *isolate, DirectHandle< Object > options, const char *method_name)
bool CompareCharsEqual(const lchar *lhs, const rchar *rhs, size_t chars)
Definition utils.h:509
V8_WARN_UNUSED_RESULT Maybe< bool > GetBoolOption(Isolate *isolate, DirectHandle< JSReceiver > options, const char *property, const char *method_name, bool *result)
Maybe< int > GetNumberOption(Isolate *isolate, DirectHandle< JSReceiver > options, DirectHandle< String > property, int min, int max, int fallback)
return value
Definition map-inl.h:893
MaybeDirectHandle< JSReceiver > CoerceOptionsToObject(Isolate *isolate, DirectHandle< Object > options, const char *method_name)
static V8_WARN_UNUSED_RESULT Maybe< T > GetStringOrBooleanOption(Isolate *isolate, DirectHandle< JSReceiver > options, const char *property, const char *method, const std::vector< const char * > &str_values, const std::vector< T > &enum_values, T true_value, T false_value, T fallback_value)
Maybe< bool > GetStringOption(Isolate *isolate, DirectHandle< JSReceiver > options, const char *property, const std::vector< const char * > &values, const char *method_name, std::unique_ptr< char[]> *result)
Maybe< T > Nothing()
Definition v8-maybe.h:112
Maybe< T > Just(const T &t)
Definition v8-maybe.h:117
#define DCHECK_NOT_NULL(val)
Definition logging.h:492
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define V8_EXPORT_PRIVATE
Definition macros.h:460
Symbol method
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:671