v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
option-utils.cc
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
6
9
10namespace v8 {
11namespace internal {
12
13// ecma402/#sec-getoptionsobject
16 const char* method_name) {
17 // 1. If options is undefined, then
18 if (IsUndefined(*options, isolate)) {
19 // a. Return ! ObjectCreate(null).
20 return isolate->factory()->NewJSObjectWithNullProto();
21 }
22 // 2. If Type(options) is Object, then
23 if (IsJSReceiver(*options)) {
24 // a. Return options.
25 return Cast<JSReceiver>(options);
26 }
27 // 3. Throw a TypeError exception.
28 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kInvalidArgument));
29}
30
31// ecma402/#sec-coerceoptionstoobject
33 Isolate* isolate, DirectHandle<Object> options, const char* method_name) {
34 // 1. If options is undefined, then
35 if (IsUndefined(*options, isolate)) {
36 // a. Return ! ObjectCreate(null).
37 return isolate->factory()->NewJSObjectWithNullProto();
38 }
39 // 2. Return ? ToObject(options).
40 ASSIGN_RETURN_ON_EXCEPTION(isolate, options,
41 Object::ToObject(isolate, options, method_name));
42 return Cast<JSReceiver>(options);
43}
44
46 const char* property,
47 const std::vector<const char*>& values,
48 const char* method_name,
49 std::unique_ptr<char[]>* result) {
50 DirectHandle<String> property_str =
51 isolate->factory()->NewStringFromAsciiChecked(property);
52
53 // 1. Let value be ? Get(options, property).
56 isolate, value,
57 Object::GetPropertyOrElement(isolate, options, property_str),
59
60 if (IsUndefined(*value, isolate)) {
61 return Just(false);
62 }
63
64 // 2. c. Let value be ? ToString(value).
65 DirectHandle<String> value_str;
67 isolate, value_str, Object::ToString(isolate, value), Nothing<bool>());
68 std::unique_ptr<char[]> value_cstr = value_str->ToCString();
69
70 // 2. d. if values is not undefined, then
71 if (!values.empty()) {
72 // 2. d. i. If values does not contain an element equal to value,
73 // throw a RangeError exception.
74 for (size_t i = 0; i < values.size(); i++) {
75 if (strcmp(values.at(i), value_cstr.get()) == 0) {
76 // 2. e. return value
77 *result = std::move(value_cstr);
78 return Just(true);
79 }
80 }
81
82 DirectHandle<String> method_str =
83 isolate->factory()->NewStringFromAsciiChecked(method_name);
85 isolate,
86 NewRangeError(MessageTemplate::kValueOutOfRange, value, method_str,
87 property_str),
89 }
90
91 // 2. e. return value
92 *result = std::move(value_cstr);
93 return Just(true);
94}
95
97 Isolate* isolate, DirectHandle<JSReceiver> options, const char* property,
98 const char* method_name, bool* result) {
99 DirectHandle<String> property_str =
100 isolate->factory()->NewStringFromAsciiChecked(property);
101
102 // 1. Let value be ? Get(options, property).
105 isolate, value,
106 Object::GetPropertyOrElement(isolate, options, property_str),
107 Nothing<bool>());
108
109 // 2. If value is not undefined, then
110 if (!IsUndefined(*value, isolate)) {
111 // 2. b. i. Let value be ToBoolean(value).
112 *result = Object::BooleanValue(*value, isolate);
113
114 // 2. e. return value
115 return Just(true);
116 }
117
118 return Just(false);
119}
120
121// ecma402/#sec-defaultnumberoption
123 int min, int max, int fallback,
124 DirectHandle<String> property) {
125 // 2. Else, return fallback.
126 if (IsUndefined(*value)) return Just(fallback);
127
128 // 1. If value is not undefined, then
129 // a. Let value be ? ToNumber(value).
130 DirectHandle<Number> value_num;
132 isolate, value_num, Object::ToNumber(isolate, value), Nothing<int>());
133 DCHECK(IsNumber(*value_num));
134
135 // b. If value is NaN or less than minimum or greater than maximum, throw a
136 // RangeError exception.
137 if (IsNaN(*value_num) || Object::NumberValue(*value_num) < min ||
138 Object::NumberValue(*value_num) > max) {
140 isolate,
141 NewRangeError(MessageTemplate::kPropertyValueOutOfRange, property),
142 Nothing<int>());
143 }
144
145 // The max and min arguments are integers and the above check makes
146 // sure that we are within the integer range making this double to
147 // int conversion safe.
148 //
149 // c. Return floor(value).
150 return Just(FastD2I(floor(Object::NumberValue(*value_num))));
151}
152
153// ecma402/#sec-getnumberoption
155 DirectHandle<String> property, int min, int max,
156 int fallback) {
157 // 1. Let value be ? Get(options, property).
160 isolate, value, JSReceiver::GetProperty(isolate, options, property),
161 Nothing<int>());
162
163 // Return ? DefaultNumberOption(value, minimum, maximum, fallback).
164 return DefaultNumberOption(isolate, value, min, max, fallback, property);
165}
166
167// #sec-getoption while type is "number"
170 DirectHandle<String> property,
171 double default_value) {
172 // 1. Let value be ? Get(options, property).
175 isolate, value, JSReceiver::GetProperty(isolate, options, property),
177 // 2. If value is undefined, then
178 if (IsUndefined(*value)) {
179 // b. Return default.
180 return Just(default_value);
181 }
182 // 4. Else if type is "number", then
183 // a. Set value to ? ToNumber(value).
184 DirectHandle<Number> value_num;
186 isolate, value_num, Object::ToNumber(isolate, value), Nothing<double>());
187 // b. If value is NaN, throw a RangeError exception.
188 if (IsNaN(*value_num)) {
190 isolate,
191 NewRangeError(MessageTemplate::kPropertyValueOutOfRange, property),
193 }
194
195 // 7. Return value.
196 return Just(Object::NumberValue(*value_num));
197}
198
199} // namespace internal
200} // namespace v8
static V8_WARN_UNUSED_RESULT MaybeHandle< Object > GetProperty(Isolate *isolate, DirectHandle< JSReceiver > receiver, const char *key)
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_WARN_UNUSED_RESULT HandleType< Number >::MaybeType ToNumber(Isolate *isolate, HandleType< T > input)
static V8_EXPORT_PRIVATE bool BooleanValue(Tagged< Object > obj, IsolateT *isolate)
static V8_WARN_UNUSED_RESULT HandleType< JSReceiver >::MaybeType ToObject(Isolate *isolate, HandleType< T > object, const char *method_name=nullptr)
static double NumberValue(Tagged< Number > obj)
#define THROW_NEW_ERROR_RETURN_VALUE(isolate, call, value)
Definition isolate.h:300
#define ASSIGN_RETURN_ON_EXCEPTION(isolate, dst, call)
Definition isolate.h:291
#define THROW_NEW_ERROR(isolate, call)
Definition isolate.h:307
#define ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, dst, call, value)
Definition isolate.h:276
ZoneVector< RpoNumber > & result
bool IsNaN(Tagged< Object > obj)
bool IsNumber(Tagged< Object > obj)
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)
int FastD2I(double x)
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)
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)
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
Maybe< T > Nothing()
Definition v8-maybe.h:112
Maybe< T > Just(const T &t)
Definition v8-maybe.h:117
#define DCHECK(condition)
Definition logging.h:482
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:671