Line data Source code
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/builtins/builtins-utils-gen.h"
6 : #include "src/builtins/builtins.h"
7 : #include "src/code-stub-assembler.h"
8 :
9 : namespace v8 {
10 : namespace internal {
11 :
12 : // -----------------------------------------------------------------------------
13 : // ES6 section 19.1 Object Objects
14 :
15 : typedef compiler::Node Node;
16 :
17 : class ObjectBuiltinsAssembler : public CodeStubAssembler {
18 : public:
19 : explicit ObjectBuiltinsAssembler(compiler::CodeAssemblerState* state)
20 344 : : CodeStubAssembler(state) {}
21 :
22 : protected:
23 : void IsString(Node* object, Label* if_string, Label* if_notstring);
24 : void ReturnToStringFormat(Node* context, Node* string);
25 : };
26 :
27 43 : void ObjectBuiltinsAssembler::IsString(Node* object, Label* if_string,
28 : Label* if_notstring) {
29 43 : Label if_notsmi(this);
30 43 : Branch(TaggedIsSmi(object), if_notstring, &if_notsmi);
31 :
32 43 : BIND(&if_notsmi);
33 : {
34 43 : Node* instance_type = LoadInstanceType(object);
35 :
36 43 : Branch(IsStringInstanceType(instance_type), if_string, if_notstring);
37 43 : }
38 43 : }
39 :
40 86 : void ObjectBuiltinsAssembler::ReturnToStringFormat(Node* context,
41 : Node* string) {
42 172 : Node* lhs = HeapConstant(factory()->NewStringFromStaticChars("[object "));
43 172 : Node* rhs = HeapConstant(factory()->NewStringFromStaticChars("]"));
44 :
45 : Callable callable =
46 86 : CodeFactory::StringAdd(isolate(), STRING_ADD_CHECK_NONE, NOT_TENURED);
47 :
48 : Return(CallStub(callable, context, CallStub(callable, context, lhs, string),
49 86 : rhs));
50 86 : }
51 :
52 172 : TF_BUILTIN(ObjectHasOwnProperty, ObjectBuiltinsAssembler) {
53 : Node* object = Parameter(Descriptor::kReceiver);
54 : Node* key = Parameter(Descriptor::kKey);
55 : Node* context = Parameter(Descriptor::kContext);
56 :
57 43 : Label call_runtime(this), return_true(this), return_false(this);
58 :
59 : // Smi receivers do not have own properties.
60 43 : Label if_objectisnotsmi(this);
61 43 : Branch(TaggedIsSmi(object), &return_false, &if_objectisnotsmi);
62 43 : BIND(&if_objectisnotsmi);
63 :
64 43 : Node* map = LoadMap(object);
65 43 : Node* instance_type = LoadMapInstanceType(map);
66 :
67 : {
68 43 : VARIABLE(var_index, MachineType::PointerRepresentation());
69 86 : VARIABLE(var_unique, MachineRepresentation::kTagged);
70 :
71 43 : Label if_index(this), if_unique_name(this), if_notunique_name(this);
72 : TryToName(key, &if_index, &var_index, &if_unique_name, &var_unique,
73 43 : &call_runtime, &if_notunique_name);
74 :
75 43 : BIND(&if_unique_name);
76 : TryHasOwnProperty(object, map, instance_type, var_unique.value(),
77 43 : &return_true, &return_false, &call_runtime);
78 :
79 43 : BIND(&if_index);
80 : {
81 : // Handle negative keys in the runtime.
82 : GotoIf(IntPtrLessThan(var_index.value(), IntPtrConstant(0)),
83 43 : &call_runtime);
84 : TryLookupElement(object, map, instance_type, var_index.value(),
85 : &return_true, &return_false, &return_false,
86 43 : &call_runtime);
87 : }
88 :
89 43 : BIND(&if_notunique_name);
90 : {
91 : // If the string was not found in the string table, then no object can
92 : // have a property with that name, so return |false|.
93 : TryInternalizeString(key, &if_index, &var_index, &if_unique_name,
94 43 : &var_unique, &return_false, &call_runtime);
95 43 : }
96 : }
97 43 : BIND(&return_true);
98 43 : Return(BooleanConstant(true));
99 :
100 43 : BIND(&return_false);
101 43 : Return(BooleanConstant(false));
102 :
103 43 : BIND(&call_runtime);
104 86 : Return(CallRuntime(Runtime::kObjectHasOwnProperty, context, object, key));
105 43 : }
106 :
107 : // ES6 #sec-object.prototype.tostring
108 215 : TF_BUILTIN(ObjectProtoToString, ObjectBuiltinsAssembler) {
109 43 : Label return_undefined(this, Label::kDeferred),
110 43 : return_null(this, Label::kDeferred),
111 43 : return_arguments(this, Label::kDeferred), return_array(this),
112 43 : return_api(this, Label::kDeferred), return_object(this),
113 43 : return_regexp(this), return_function(this), return_error(this),
114 43 : return_date(this), return_jsvalue(this),
115 43 : return_jsproxy(this, Label::kDeferred);
116 :
117 43 : Label if_isproxy(this, Label::kDeferred);
118 :
119 43 : Label checkstringtag(this);
120 43 : Label if_tostringtag(this), if_notostringtag(this);
121 :
122 : Node* receiver = Parameter(Descriptor::kReceiver);
123 : Node* context = Parameter(Descriptor::kContext);
124 :
125 43 : GotoIf(WordEqual(receiver, UndefinedConstant()), &return_undefined);
126 :
127 43 : GotoIf(WordEqual(receiver, NullConstant()), &return_null);
128 :
129 43 : Callable to_object = CodeFactory::ToObject(isolate());
130 43 : receiver = CallStub(to_object, context, receiver);
131 :
132 43 : Node* receiver_instance_type = LoadInstanceType(receiver);
133 :
134 : // for proxies, check IsArray before getting @@toStringTag
135 86 : VARIABLE(var_proxy_is_array, MachineRepresentation::kTagged);
136 43 : var_proxy_is_array.Bind(BooleanConstant(false));
137 :
138 : Branch(Word32Equal(receiver_instance_type, Int32Constant(JS_PROXY_TYPE)),
139 43 : &if_isproxy, &checkstringtag);
140 :
141 43 : BIND(&if_isproxy);
142 : {
143 : // This can throw
144 : var_proxy_is_array.Bind(
145 43 : CallRuntime(Runtime::kArrayIsArray, context, receiver));
146 43 : Goto(&checkstringtag);
147 : }
148 :
149 43 : BIND(&checkstringtag);
150 : {
151 : Node* to_string_tag_symbol =
152 86 : HeapConstant(isolate()->factory()->to_string_tag_symbol());
153 :
154 43 : GetPropertyStub stub(isolate());
155 : Callable get_property =
156 43 : Callable(stub.GetCode(), stub.GetCallInterfaceDescriptor());
157 : Node* to_string_tag_value =
158 43 : CallStub(get_property, context, receiver, to_string_tag_symbol);
159 :
160 43 : IsString(to_string_tag_value, &if_tostringtag, &if_notostringtag);
161 :
162 43 : BIND(&if_tostringtag);
163 43 : ReturnToStringFormat(context, to_string_tag_value);
164 : }
165 43 : BIND(&if_notostringtag);
166 : {
167 : size_t const kNumCases = 11;
168 : Label* case_labels[kNumCases];
169 : int32_t case_values[kNumCases];
170 43 : case_labels[0] = &return_api;
171 43 : case_values[0] = JS_API_OBJECT_TYPE;
172 43 : case_labels[1] = &return_api;
173 43 : case_values[1] = JS_SPECIAL_API_OBJECT_TYPE;
174 43 : case_labels[2] = &return_arguments;
175 43 : case_values[2] = JS_ARGUMENTS_TYPE;
176 43 : case_labels[3] = &return_array;
177 43 : case_values[3] = JS_ARRAY_TYPE;
178 43 : case_labels[4] = &return_function;
179 43 : case_values[4] = JS_BOUND_FUNCTION_TYPE;
180 43 : case_labels[5] = &return_function;
181 43 : case_values[5] = JS_FUNCTION_TYPE;
182 43 : case_labels[6] = &return_error;
183 43 : case_values[6] = JS_ERROR_TYPE;
184 43 : case_labels[7] = &return_date;
185 43 : case_values[7] = JS_DATE_TYPE;
186 43 : case_labels[8] = &return_regexp;
187 43 : case_values[8] = JS_REGEXP_TYPE;
188 43 : case_labels[9] = &return_jsvalue;
189 43 : case_values[9] = JS_VALUE_TYPE;
190 43 : case_labels[10] = &return_jsproxy;
191 43 : case_values[10] = JS_PROXY_TYPE;
192 :
193 : Switch(receiver_instance_type, &return_object, case_values, case_labels,
194 43 : arraysize(case_values));
195 :
196 43 : BIND(&return_undefined);
197 86 : Return(HeapConstant(isolate()->factory()->undefined_to_string()));
198 :
199 43 : BIND(&return_null);
200 86 : Return(HeapConstant(isolate()->factory()->null_to_string()));
201 :
202 43 : BIND(&return_arguments);
203 86 : Return(HeapConstant(isolate()->factory()->arguments_to_string()));
204 :
205 43 : BIND(&return_array);
206 86 : Return(HeapConstant(isolate()->factory()->array_to_string()));
207 :
208 43 : BIND(&return_function);
209 86 : Return(HeapConstant(isolate()->factory()->function_to_string()));
210 :
211 43 : BIND(&return_error);
212 86 : Return(HeapConstant(isolate()->factory()->error_to_string()));
213 :
214 43 : BIND(&return_date);
215 86 : Return(HeapConstant(isolate()->factory()->date_to_string()));
216 :
217 43 : BIND(&return_regexp);
218 86 : Return(HeapConstant(isolate()->factory()->regexp_to_string()));
219 :
220 43 : BIND(&return_api);
221 : {
222 43 : Node* class_name = CallRuntime(Runtime::kClassOf, context, receiver);
223 43 : ReturnToStringFormat(context, class_name);
224 : }
225 :
226 43 : BIND(&return_jsvalue);
227 : {
228 43 : Label return_boolean(this), return_number(this), return_string(this);
229 :
230 43 : Node* value = LoadJSValueValue(receiver);
231 43 : GotoIf(TaggedIsSmi(value), &return_number);
232 43 : Node* instance_type = LoadInstanceType(value);
233 :
234 43 : GotoIf(IsStringInstanceType(instance_type), &return_string);
235 : GotoIf(Word32Equal(instance_type, Int32Constant(HEAP_NUMBER_TYPE)),
236 43 : &return_number);
237 : GotoIf(Word32Equal(instance_type, Int32Constant(ODDBALL_TYPE)),
238 43 : &return_boolean);
239 :
240 : CSA_ASSERT(this, Word32Equal(instance_type, Int32Constant(SYMBOL_TYPE)));
241 43 : Goto(&return_object);
242 :
243 43 : BIND(&return_string);
244 86 : Return(HeapConstant(isolate()->factory()->string_to_string()));
245 :
246 43 : BIND(&return_number);
247 86 : Return(HeapConstant(isolate()->factory()->number_to_string()));
248 :
249 43 : BIND(&return_boolean);
250 129 : Return(HeapConstant(isolate()->factory()->boolean_to_string()));
251 : }
252 :
253 43 : BIND(&return_jsproxy);
254 : {
255 : GotoIf(WordEqual(var_proxy_is_array.value(), BooleanConstant(true)),
256 43 : &return_array);
257 :
258 43 : Node* map = LoadMap(receiver);
259 :
260 : // Return object if the proxy {receiver} is not callable.
261 43 : Branch(IsCallableMap(map), &return_function, &return_object);
262 : }
263 :
264 : // Default
265 43 : BIND(&return_object);
266 86 : Return(HeapConstant(isolate()->factory()->object_to_string()));
267 43 : }
268 43 : }
269 :
270 : // ES6 #sec-object.prototype.valueof
271 129 : TF_BUILTIN(ObjectPrototypeValueOf, CodeStubAssembler) {
272 : Node* receiver = Parameter(Descriptor::kReceiver);
273 : Node* context = Parameter(Descriptor::kContext);
274 :
275 43 : Callable to_object = CodeFactory::ToObject(isolate());
276 43 : receiver = CallStub(to_object, context, receiver);
277 :
278 43 : Return(receiver);
279 43 : }
280 :
281 : // ES #sec-object.create
282 172 : TF_BUILTIN(ObjectCreate, ObjectBuiltinsAssembler) {
283 : Node* prototype = Parameter(Descriptor::kPrototype);
284 : Node* properties = Parameter(Descriptor::kProperties);
285 : Node* context = Parameter(Descriptor::kContext);
286 :
287 43 : Label call_runtime(this, Label::kDeferred), prototype_valid(this),
288 43 : no_properties(this);
289 : {
290 43 : Comment("Argument 1 check: prototype");
291 43 : GotoIf(WordEqual(prototype, NullConstant()), &prototype_valid);
292 43 : BranchIfJSReceiver(prototype, &prototype_valid, &call_runtime);
293 : }
294 :
295 43 : BIND(&prototype_valid);
296 : {
297 43 : Comment("Argument 2 check: properties");
298 : // Check that we have a simple object
299 43 : GotoIf(TaggedIsSmi(properties), &call_runtime);
300 : // Undefined implies no properties.
301 43 : GotoIf(WordEqual(properties, UndefinedConstant()), &no_properties);
302 43 : Node* properties_map = LoadMap(properties);
303 43 : GotoIf(IsSpecialReceiverMap(properties_map), &call_runtime);
304 : // Stay on the fast path only if there are no elements.
305 : GotoIfNot(WordEqual(LoadElements(properties),
306 : LoadRoot(Heap::kEmptyFixedArrayRootIndex)),
307 43 : &call_runtime);
308 : // Handle dictionary objects or fast objects with properties in runtime.
309 43 : Node* bit_field3 = LoadMapBitField3(properties_map);
310 43 : GotoIf(IsSetWord32<Map::DictionaryMap>(bit_field3), &call_runtime);
311 : Branch(IsSetWord32<Map::NumberOfOwnDescriptorsBits>(bit_field3),
312 43 : &call_runtime, &no_properties);
313 : }
314 :
315 : // Create a new object with the given prototype.
316 43 : BIND(&no_properties);
317 : {
318 43 : VARIABLE(map, MachineRepresentation::kTagged);
319 86 : VARIABLE(properties, MachineRepresentation::kTagged);
320 43 : Label non_null_proto(this), instantiate_map(this), good(this);
321 :
322 43 : Branch(WordEqual(prototype, NullConstant()), &good, &non_null_proto);
323 :
324 43 : BIND(&good);
325 : {
326 : map.Bind(LoadContextElement(
327 43 : context, Context::SLOW_OBJECT_WITH_NULL_PROTOTYPE_MAP));
328 43 : properties.Bind(AllocateNameDictionary(NameDictionary::kInitialCapacity));
329 43 : Goto(&instantiate_map);
330 : }
331 :
332 43 : BIND(&non_null_proto);
333 : {
334 43 : properties.Bind(EmptyFixedArrayConstant());
335 : Node* object_function =
336 43 : LoadContextElement(context, Context::OBJECT_FUNCTION_INDEX);
337 : Node* object_function_map = LoadObjectField(
338 43 : object_function, JSFunction::kPrototypeOrInitialMapOffset);
339 43 : map.Bind(object_function_map);
340 : GotoIf(WordEqual(prototype, LoadMapPrototype(map.value())),
341 43 : &instantiate_map);
342 : // Try loading the prototype info.
343 : Node* prototype_info =
344 43 : LoadMapPrototypeInfo(LoadMap(prototype), &call_runtime);
345 43 : Comment("Load ObjectCreateMap from PrototypeInfo");
346 : Node* weak_cell =
347 43 : LoadObjectField(prototype_info, PrototypeInfo::kObjectCreateMap);
348 43 : GotoIf(WordEqual(weak_cell, UndefinedConstant()), &call_runtime);
349 43 : map.Bind(LoadWeakCellValue(weak_cell, &call_runtime));
350 43 : Goto(&instantiate_map);
351 : }
352 :
353 43 : BIND(&instantiate_map);
354 : {
355 43 : Node* instance = AllocateJSObjectFromMap(map.value(), properties.value());
356 43 : Return(instance);
357 43 : }
358 : }
359 :
360 43 : BIND(&call_runtime);
361 : {
362 43 : Return(CallRuntime(Runtime::kObjectCreate, context, prototype, properties));
363 43 : }
364 43 : }
365 :
366 172 : TF_BUILTIN(CreateIterResultObject, ObjectBuiltinsAssembler) {
367 : Node* const value = Parameter(Descriptor::kValue);
368 : Node* const done = Parameter(Descriptor::kDone);
369 : Node* const context = Parameter(Descriptor::kContext);
370 :
371 43 : Node* const native_context = LoadNativeContext(context);
372 : Node* const map =
373 43 : LoadContextElement(native_context, Context::ITERATOR_RESULT_MAP_INDEX);
374 :
375 43 : Node* const result = AllocateJSObjectFromMap(map);
376 :
377 43 : StoreObjectFieldNoWriteBarrier(result, JSIteratorResult::kValueOffset, value);
378 43 : StoreObjectFieldNoWriteBarrier(result, JSIteratorResult::kDoneOffset, done);
379 :
380 43 : Return(result);
381 43 : }
382 :
383 172 : TF_BUILTIN(HasProperty, ObjectBuiltinsAssembler) {
384 : Node* key = Parameter(Descriptor::kKey);
385 : Node* object = Parameter(Descriptor::kObject);
386 : Node* context = Parameter(Descriptor::kContext);
387 :
388 43 : Return(HasProperty(object, key, context, Runtime::kHasProperty));
389 43 : }
390 :
391 172 : TF_BUILTIN(InstanceOf, ObjectBuiltinsAssembler) {
392 : Node* object = Parameter(Descriptor::kLeft);
393 : Node* callable = Parameter(Descriptor::kRight);
394 : Node* context = Parameter(Descriptor::kContext);
395 :
396 43 : Return(InstanceOf(object, callable, context));
397 43 : }
398 :
399 : // ES6 section 7.3.19 OrdinaryHasInstance ( C, O )
400 172 : TF_BUILTIN(OrdinaryHasInstance, ObjectBuiltinsAssembler) {
401 : Node* constructor = Parameter(Descriptor::kLeft);
402 : Node* object = Parameter(Descriptor::kRight);
403 : Node* context = Parameter(Descriptor::kContext);
404 :
405 43 : Return(OrdinaryHasInstance(context, constructor, object));
406 43 : }
407 :
408 172 : TF_BUILTIN(GetSuperConstructor, ObjectBuiltinsAssembler) {
409 : Node* object = Parameter(Descriptor::kObject);
410 : Node* context = Parameter(Descriptor::kContext);
411 :
412 43 : Return(GetSuperConstructor(object, context));
413 43 : }
414 :
415 : } // namespace internal
416 : } // namespace v8
|