blob: 1a82c3c07ba980d7b0aaa0f2b3f1cbf07bc5d19a [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005#include "src/objects.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007#include <cmath>
8#include <iomanip>
9#include <sstream>
Steve Blocka7e24c12009-10-30 11:49:00 +000010
Ben Murdochda12d292016-06-02 14:46:10 +010011#include "src/objects-inl.h"
12
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013#include "src/accessors.h"
14#include "src/allocation-site-scopes.h"
15#include "src/api.h"
Ben Murdochda12d292016-06-02 14:46:10 +010016#include "src/api-arguments.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010017#include "src/api-natives.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018#include "src/base/bits.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019#include "src/base/utils/random-number-generator.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000020#include "src/bootstrapper.h"
21#include "src/code-stubs.h"
22#include "src/codegen.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000023#include "src/compilation-dependencies.h"
24#include "src/compiler.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025#include "src/date.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000026#include "src/debug/debug.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027#include "src/deoptimizer.h"
28#include "src/elements.h"
29#include "src/execution.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000030#include "src/field-index-inl.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010031#include "src/field-index.h"
32#include "src/field-type.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000033#include "src/full-codegen/full-codegen.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000034#include "src/ic/ic.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035#include "src/identity-map.h"
Ben Murdochda12d292016-06-02 14:46:10 +010036#include "src/interpreter/bytecode-array-iterator.h"
37#include "src/interpreter/interpreter.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010038#include "src/interpreter/source-position-table.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039#include "src/isolate-inl.h"
Ben Murdochda12d292016-06-02 14:46:10 +010040#include "src/keys.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041#include "src/list.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042#include "src/log.h"
43#include "src/lookup.h"
44#include "src/macro-assembler.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000045#include "src/messages.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000046#include "src/objects-body-descriptors-inl.h"
47#include "src/profiler/cpu-profiler.h"
48#include "src/property-descriptor.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049#include "src/prototype.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000050#include "src/regexp/jsregexp.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051#include "src/safepoint-table.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000052#include "src/string-builder.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053#include "src/string-search.h"
54#include "src/string-stream.h"
55#include "src/utils.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000056#include "src/zone.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000057
58#ifdef ENABLE_DISASSEMBLER
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059#include "src/disasm.h"
60#include "src/disassembler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000061#endif
62
Steve Blocka7e24c12009-10-30 11:49:00 +000063namespace v8 {
64namespace internal {
65
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066std::ostream& operator<<(std::ostream& os, InstanceType instance_type) {
67 switch (instance_type) {
68#define WRITE_TYPE(TYPE) \
69 case TYPE: \
70 return os << #TYPE;
71 INSTANCE_TYPE_LIST(WRITE_TYPE)
72#undef WRITE_TYPE
73 }
74 UNREACHABLE();
75 return os << "UNKNOWN"; // Keep the compiler happy.
76}
77
Ben Murdoch097c5b22016-05-18 11:27:45 +010078Handle<FieldType> Object::OptimalType(Isolate* isolate,
79 Representation representation) {
80 if (representation.IsNone()) return FieldType::None(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000081 if (FLAG_track_field_types) {
82 if (representation.IsHeapObject() && IsHeapObject()) {
83 // We can track only JavaScript objects with stable maps.
84 Handle<Map> map(HeapObject::cast(this)->map(), isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000085 if (map->is_stable() && map->IsJSReceiverMap()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010086 return FieldType::Class(map, isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000087 }
88 }
89 }
Ben Murdoch097c5b22016-05-18 11:27:45 +010090 return FieldType::Any(isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010091}
92
Steve Blocka7e24c12009-10-30 11:49:00 +000093
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094MaybeHandle<JSReceiver> Object::ToObject(Isolate* isolate,
95 Handle<Object> object,
96 Handle<Context> native_context) {
97 if (object->IsJSReceiver()) return Handle<JSReceiver>::cast(object);
98 Handle<JSFunction> constructor;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099 if (object->IsSmi()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100 constructor = handle(native_context->number_function(), isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000102 int constructor_function_index =
103 Handle<HeapObject>::cast(object)->map()->GetConstructorFunctionIndex();
104 if (constructor_function_index == Map::kNoConstructorFunctionIndex) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100105 THROW_NEW_ERROR(isolate,
106 NewTypeError(MessageTemplate::kUndefinedOrNullToObject),
107 JSReceiver);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000108 }
109 constructor = handle(
110 JSFunction::cast(native_context->get(constructor_function_index)),
111 isolate);
John Reck59135872010-11-02 12:39:01 -0700112 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113 Handle<JSObject> result = isolate->factory()->NewJSObject(constructor);
114 Handle<JSValue>::cast(result)->set_value(*object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000115 return result;
116}
117
Ben Murdochda12d292016-06-02 14:46:10 +0100118// ES6 section 9.2.1.2, OrdinaryCallBindThis for sloppy callee.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119// static
Ben Murdochda12d292016-06-02 14:46:10 +0100120MaybeHandle<JSReceiver> Object::ConvertReceiver(Isolate* isolate,
121 Handle<Object> object) {
122 if (object->IsJSReceiver()) return Handle<JSReceiver>::cast(object);
123 if (*object == isolate->heap()->null_value() ||
124 *object == isolate->heap()->undefined_value()) {
125 return handle(isolate->global_proxy(), isolate);
126 }
127 return Object::ToObject(isolate, object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000128}
129
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000130// static
131MaybeHandle<Object> Object::ToNumber(Handle<Object> input) {
132 while (true) {
133 if (input->IsNumber()) {
134 return input;
135 }
136 if (input->IsString()) {
137 return String::ToNumber(Handle<String>::cast(input));
138 }
139 if (input->IsOddball()) {
140 return Oddball::ToNumber(Handle<Oddball>::cast(input));
141 }
142 Isolate* const isolate = Handle<HeapObject>::cast(input)->GetIsolate();
143 if (input->IsSymbol()) {
144 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSymbolToNumber),
145 Object);
146 }
147 if (input->IsSimd128Value()) {
148 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSimdToNumber),
149 Object);
150 }
151 ASSIGN_RETURN_ON_EXCEPTION(
152 isolate, input, JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(input),
153 ToPrimitiveHint::kNumber),
154 Object);
155 }
156}
157
158
159// static
160MaybeHandle<Object> Object::ToInteger(Isolate* isolate, Handle<Object> input) {
161 ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object);
162 return isolate->factory()->NewNumber(DoubleToInteger(input->Number()));
163}
164
165
166// static
167MaybeHandle<Object> Object::ToInt32(Isolate* isolate, Handle<Object> input) {
168 ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object);
169 return isolate->factory()->NewNumberFromInt(DoubleToInt32(input->Number()));
170}
171
172
173// static
174MaybeHandle<Object> Object::ToUint32(Isolate* isolate, Handle<Object> input) {
175 ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object);
176 return isolate->factory()->NewNumberFromUint(DoubleToUint32(input->Number()));
177}
178
179
180// static
Ben Murdochda12d292016-06-02 14:46:10 +0100181MaybeHandle<Name> Object::ConvertToName(Isolate* isolate,
182 Handle<Object> input) {
183 ASSIGN_RETURN_ON_EXCEPTION(
184 isolate, input, Object::ToPrimitive(input, ToPrimitiveHint::kString),
185 Name);
186 if (input->IsName()) return Handle<Name>::cast(input);
187 return ToString(isolate, input);
188}
189
190// static
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000191MaybeHandle<String> Object::ToString(Isolate* isolate, Handle<Object> input) {
192 while (true) {
193 if (input->IsString()) {
194 return Handle<String>::cast(input);
195 }
196 if (input->IsOddball()) {
197 return handle(Handle<Oddball>::cast(input)->to_string(), isolate);
198 }
199 if (input->IsNumber()) {
200 return isolate->factory()->NumberToString(input);
201 }
202 if (input->IsSymbol()) {
203 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSymbolToString),
204 String);
205 }
206 if (input->IsSimd128Value()) {
207 return Simd128Value::ToString(Handle<Simd128Value>::cast(input));
208 }
209 ASSIGN_RETURN_ON_EXCEPTION(
210 isolate, input, JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(input),
211 ToPrimitiveHint::kString),
212 String);
213 }
214}
215
216
217// static
218MaybeHandle<Object> Object::ToLength(Isolate* isolate, Handle<Object> input) {
219 ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object);
220 double len = DoubleToInteger(input->Number());
221 if (len <= 0.0) {
222 len = 0.0;
223 } else if (len >= kMaxSafeInteger) {
224 len = kMaxSafeInteger;
225 }
226 return isolate->factory()->NewNumber(len);
227}
228
229
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230bool Object::BooleanValue() {
231 if (IsBoolean()) return IsTrue();
232 if (IsSmi()) return Smi::cast(this)->value() != 0;
233 if (IsUndefined() || IsNull()) return false;
Ben Murdochda12d292016-06-02 14:46:10 +0100234 if (IsUndetectable()) return false; // Undetectable object is false.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000235 if (IsString()) return String::cast(this)->length() != 0;
236 if (IsHeapNumber()) return HeapNumber::cast(this)->HeapNumberBooleanValue();
237 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000238}
239
240
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000241namespace {
242
243// TODO(bmeurer): Maybe we should introduce a marker interface Number,
244// where we put all these methods at some point?
245ComparisonResult NumberCompare(double x, double y) {
246 if (std::isnan(x) || std::isnan(y)) {
247 return ComparisonResult::kUndefined;
248 } else if (x < y) {
249 return ComparisonResult::kLessThan;
250 } else if (x > y) {
251 return ComparisonResult::kGreaterThan;
252 } else {
253 return ComparisonResult::kEqual;
Steve Blocka7e24c12009-10-30 11:49:00 +0000254 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000255}
256
257
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000258bool NumberEquals(double x, double y) {
259 // Must check explicitly for NaN's on Windows, but -0 works fine.
260 if (std::isnan(x)) return false;
261 if (std::isnan(y)) return false;
262 return x == y;
263}
264
265
266bool NumberEquals(const Object* x, const Object* y) {
267 return NumberEquals(x->Number(), y->Number());
268}
269
270
271bool NumberEquals(Handle<Object> x, Handle<Object> y) {
272 return NumberEquals(*x, *y);
273}
274
275} // namespace
276
277
278// static
Ben Murdoch097c5b22016-05-18 11:27:45 +0100279Maybe<ComparisonResult> Object::Compare(Handle<Object> x, Handle<Object> y) {
280 // ES6 section 7.2.11 Abstract Relational Comparison step 3 and 4.
281 if (!Object::ToPrimitive(x, ToPrimitiveHint::kNumber).ToHandle(&x) ||
282 !Object::ToPrimitive(y, ToPrimitiveHint::kNumber).ToHandle(&y)) {
283 return Nothing<ComparisonResult>();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000284 }
285 if (x->IsString() && y->IsString()) {
286 // ES6 section 7.2.11 Abstract Relational Comparison step 5.
287 return Just(
288 String::Compare(Handle<String>::cast(x), Handle<String>::cast(y)));
289 }
290 // ES6 section 7.2.11 Abstract Relational Comparison step 6.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100291 if (!Object::ToNumber(x).ToHandle(&x) || !Object::ToNumber(y).ToHandle(&y)) {
292 return Nothing<ComparisonResult>();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000293 }
294 return Just(NumberCompare(x->Number(), y->Number()));
295}
296
297
298// static
299Maybe<bool> Object::Equals(Handle<Object> x, Handle<Object> y) {
Ben Murdochda12d292016-06-02 14:46:10 +0100300 // This is the generic version of Abstract Equality Comparison; a version in
301 // JavaScript land is available in the EqualStub and NotEqualStub. Whenever
302 // you change something functionality wise in here, remember to update the
303 // TurboFan code stubs as well.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000304 while (true) {
305 if (x->IsNumber()) {
306 if (y->IsNumber()) {
307 return Just(NumberEquals(x, y));
308 } else if (y->IsBoolean()) {
309 return Just(NumberEquals(*x, Handle<Oddball>::cast(y)->to_number()));
310 } else if (y->IsString()) {
311 return Just(NumberEquals(x, String::ToNumber(Handle<String>::cast(y))));
Ben Murdochda12d292016-06-02 14:46:10 +0100312 } else if (y->IsJSReceiver()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000313 if (!JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(y))
314 .ToHandle(&y)) {
315 return Nothing<bool>();
316 }
317 } else {
318 return Just(false);
319 }
320 } else if (x->IsString()) {
321 if (y->IsString()) {
322 return Just(
323 String::Equals(Handle<String>::cast(x), Handle<String>::cast(y)));
324 } else if (y->IsNumber()) {
325 x = String::ToNumber(Handle<String>::cast(x));
326 return Just(NumberEquals(x, y));
327 } else if (y->IsBoolean()) {
328 x = String::ToNumber(Handle<String>::cast(x));
329 return Just(NumberEquals(*x, Handle<Oddball>::cast(y)->to_number()));
Ben Murdochda12d292016-06-02 14:46:10 +0100330 } else if (y->IsJSReceiver()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000331 if (!JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(y))
332 .ToHandle(&y)) {
333 return Nothing<bool>();
334 }
335 } else {
336 return Just(false);
337 }
338 } else if (x->IsBoolean()) {
339 if (y->IsOddball()) {
340 return Just(x.is_identical_to(y));
341 } else if (y->IsNumber()) {
342 return Just(NumberEquals(Handle<Oddball>::cast(x)->to_number(), *y));
343 } else if (y->IsString()) {
344 y = String::ToNumber(Handle<String>::cast(y));
345 return Just(NumberEquals(Handle<Oddball>::cast(x)->to_number(), *y));
Ben Murdochda12d292016-06-02 14:46:10 +0100346 } else if (y->IsJSReceiver()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000347 if (!JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(y))
348 .ToHandle(&y)) {
349 return Nothing<bool>();
350 }
351 x = Oddball::ToNumber(Handle<Oddball>::cast(x));
352 } else {
353 return Just(false);
354 }
355 } else if (x->IsSymbol()) {
356 if (y->IsSymbol()) {
357 return Just(x.is_identical_to(y));
Ben Murdochda12d292016-06-02 14:46:10 +0100358 } else if (y->IsJSReceiver()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000359 if (!JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(y))
360 .ToHandle(&y)) {
361 return Nothing<bool>();
362 }
363 } else {
364 return Just(false);
365 }
366 } else if (x->IsSimd128Value()) {
367 if (y->IsSimd128Value()) {
368 return Just(Simd128Value::Equals(Handle<Simd128Value>::cast(x),
369 Handle<Simd128Value>::cast(y)));
Ben Murdochda12d292016-06-02 14:46:10 +0100370 } else if (y->IsJSReceiver()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000371 if (!JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(y))
372 .ToHandle(&y)) {
373 return Nothing<bool>();
374 }
375 } else {
376 return Just(false);
377 }
Ben Murdochda12d292016-06-02 14:46:10 +0100378 } else if (x->IsJSReceiver()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000379 if (y->IsJSReceiver()) {
380 return Just(x.is_identical_to(y));
Ben Murdochda12d292016-06-02 14:46:10 +0100381 } else if (y->IsUndetectable()) {
382 return Just(x->IsUndetectable());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000383 } else if (y->IsBoolean()) {
384 y = Oddball::ToNumber(Handle<Oddball>::cast(y));
385 } else if (!JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(x))
386 .ToHandle(&x)) {
387 return Nothing<bool>();
388 }
389 } else {
Ben Murdochda12d292016-06-02 14:46:10 +0100390 return Just(x->IsUndetectable() && y->IsUndetectable());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000391 }
392 }
393}
394
395
396bool Object::StrictEquals(Object* that) {
397 if (this->IsNumber()) {
398 if (!that->IsNumber()) return false;
399 return NumberEquals(this, that);
400 } else if (this->IsString()) {
401 if (!that->IsString()) return false;
402 return String::cast(this)->Equals(String::cast(that));
403 } else if (this->IsSimd128Value()) {
404 if (!that->IsSimd128Value()) return false;
405 return Simd128Value::cast(this)->Equals(Simd128Value::cast(that));
406 }
407 return this == that;
408}
409
410
411// static
412Handle<String> Object::TypeOf(Isolate* isolate, Handle<Object> object) {
413 if (object->IsNumber()) return isolate->factory()->number_string();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100414 if (object->IsOddball()) return handle(Oddball::cast(*object)->type_of());
Ben Murdochda12d292016-06-02 14:46:10 +0100415 if (object->IsUndetectable()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000416 return isolate->factory()->undefined_string();
417 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000418 if (object->IsString()) return isolate->factory()->string_string();
419 if (object->IsSymbol()) return isolate->factory()->symbol_string();
420 if (object->IsString()) return isolate->factory()->string_string();
421#define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \
422 if (object->Is##Type()) return isolate->factory()->type##_string();
423 SIMD128_TYPES(SIMD128_TYPE)
424#undef SIMD128_TYPE
425 if (object->IsCallable()) return isolate->factory()->function_string();
426 return isolate->factory()->object_string();
427}
428
429
430// static
431MaybeHandle<Object> Object::Multiply(Isolate* isolate, Handle<Object> lhs,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100432 Handle<Object> rhs) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000433 if (!lhs->IsNumber() || !rhs->IsNumber()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000434 ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object);
435 ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object);
436 }
437 return isolate->factory()->NewNumber(lhs->Number() * rhs->Number());
438}
439
440
441// static
442MaybeHandle<Object> Object::Divide(Isolate* isolate, Handle<Object> lhs,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100443 Handle<Object> rhs) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000444 if (!lhs->IsNumber() || !rhs->IsNumber()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000445 ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object);
446 ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object);
447 }
448 return isolate->factory()->NewNumber(lhs->Number() / rhs->Number());
449}
450
451
452// static
453MaybeHandle<Object> Object::Modulus(Isolate* isolate, Handle<Object> lhs,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100454 Handle<Object> rhs) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000455 if (!lhs->IsNumber() || !rhs->IsNumber()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000456 ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object);
457 ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object);
458 }
459 return isolate->factory()->NewNumber(modulo(lhs->Number(), rhs->Number()));
460}
461
462
463// static
464MaybeHandle<Object> Object::Add(Isolate* isolate, Handle<Object> lhs,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100465 Handle<Object> rhs) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000466 if (lhs->IsNumber() && rhs->IsNumber()) {
467 return isolate->factory()->NewNumber(lhs->Number() + rhs->Number());
468 } else if (lhs->IsString() && rhs->IsString()) {
469 return isolate->factory()->NewConsString(Handle<String>::cast(lhs),
470 Handle<String>::cast(rhs));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000471 }
472 ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToPrimitive(lhs), Object);
473 ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToPrimitive(rhs), Object);
474 if (lhs->IsString() || rhs->IsString()) {
475 ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToString(isolate, rhs),
476 Object);
477 ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToString(isolate, lhs),
478 Object);
479 return isolate->factory()->NewConsString(Handle<String>::cast(lhs),
480 Handle<String>::cast(rhs));
481 }
482 ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object);
483 ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object);
484 return isolate->factory()->NewNumber(lhs->Number() + rhs->Number());
485}
486
487
488// static
489MaybeHandle<Object> Object::Subtract(Isolate* isolate, Handle<Object> lhs,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100490 Handle<Object> rhs) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000491 if (!lhs->IsNumber() || !rhs->IsNumber()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000492 ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object);
493 ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object);
494 }
495 return isolate->factory()->NewNumber(lhs->Number() - rhs->Number());
496}
497
498
499// static
500MaybeHandle<Object> Object::ShiftLeft(Isolate* isolate, Handle<Object> lhs,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100501 Handle<Object> rhs) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000502 if (!lhs->IsNumber() || !rhs->IsNumber()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000503 ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object);
504 ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object);
505 }
506 return isolate->factory()->NewNumberFromInt(NumberToInt32(*lhs)
507 << (NumberToUint32(*rhs) & 0x1F));
508}
509
510
511// static
512MaybeHandle<Object> Object::ShiftRight(Isolate* isolate, Handle<Object> lhs,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100513 Handle<Object> rhs) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000514 if (!lhs->IsNumber() || !rhs->IsNumber()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000515 ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object);
516 ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object);
517 }
518 return isolate->factory()->NewNumberFromInt(NumberToInt32(*lhs) >>
519 (NumberToUint32(*rhs) & 0x1F));
520}
521
522
523// static
524MaybeHandle<Object> Object::ShiftRightLogical(Isolate* isolate,
525 Handle<Object> lhs,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100526 Handle<Object> rhs) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000527 if (!lhs->IsNumber() || !rhs->IsNumber()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000528 ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object);
529 ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object);
530 }
531 return isolate->factory()->NewNumberFromUint(NumberToUint32(*lhs) >>
532 (NumberToUint32(*rhs) & 0x1F));
533}
534
535
536// static
537MaybeHandle<Object> Object::BitwiseAnd(Isolate* isolate, Handle<Object> lhs,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100538 Handle<Object> rhs) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000539 if (!lhs->IsNumber() || !rhs->IsNumber()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000540 ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object);
541 ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object);
542 }
543 return isolate->factory()->NewNumberFromInt(NumberToInt32(*lhs) &
544 NumberToInt32(*rhs));
545}
546
547
548// static
549MaybeHandle<Object> Object::BitwiseOr(Isolate* isolate, Handle<Object> lhs,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100550 Handle<Object> rhs) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000551 if (!lhs->IsNumber() || !rhs->IsNumber()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000552 ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object);
553 ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object);
554 }
555 return isolate->factory()->NewNumberFromInt(NumberToInt32(*lhs) |
556 NumberToInt32(*rhs));
557}
558
559
560// static
561MaybeHandle<Object> Object::BitwiseXor(Isolate* isolate, Handle<Object> lhs,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100562 Handle<Object> rhs) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000563 if (!lhs->IsNumber() || !rhs->IsNumber()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000564 ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object);
565 ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object);
566 }
567 return isolate->factory()->NewNumberFromInt(NumberToInt32(*lhs) ^
568 NumberToInt32(*rhs));
569}
570
571
572Maybe<bool> Object::IsArray(Handle<Object> object) {
573 if (object->IsJSArray()) return Just(true);
574 if (object->IsJSProxy()) {
575 Handle<JSProxy> proxy = Handle<JSProxy>::cast(object);
576 Isolate* isolate = proxy->GetIsolate();
577 if (proxy->IsRevoked()) {
578 isolate->Throw(*isolate->factory()->NewTypeError(
579 MessageTemplate::kProxyRevoked,
580 isolate->factory()->NewStringFromAsciiChecked("IsArray")));
581 return Nothing<bool>();
582 }
583 return Object::IsArray(handle(proxy->target(), isolate));
584 }
585 return Just(false);
586}
587
588
589bool Object::IsPromise(Handle<Object> object) {
590 if (!object->IsJSObject()) return false;
591 auto js_object = Handle<JSObject>::cast(object);
592 // Promises can't have access checks.
593 if (js_object->map()->is_access_check_needed()) return false;
594 auto isolate = js_object->GetIsolate();
595 // TODO(dcarney): this should just be read from the symbol registry so as not
596 // to be context dependent.
597 auto key = isolate->factory()->promise_status_symbol();
598 // Shouldn't be possible to throw here.
599 return JSObject::HasRealNamedProperty(js_object, key).FromJust();
600}
601
602
603// static
604MaybeHandle<Object> Object::GetMethod(Handle<JSReceiver> receiver,
605 Handle<Name> name) {
606 Handle<Object> func;
607 Isolate* isolate = receiver->GetIsolate();
608 ASSIGN_RETURN_ON_EXCEPTION(isolate, func,
609 JSReceiver::GetProperty(receiver, name), Object);
610 if (func->IsNull() || func->IsUndefined()) {
611 return isolate->factory()->undefined_value();
612 }
613 if (!func->IsCallable()) {
614 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kPropertyNotFunction,
615 func, name, receiver),
616 Object);
617 }
618 return func;
619}
620
621
622// static
623MaybeHandle<FixedArray> Object::CreateListFromArrayLike(
624 Isolate* isolate, Handle<Object> object, ElementTypes element_types) {
625 // 1. ReturnIfAbrupt(object).
626 // 2. (default elementTypes -- not applicable.)
627 // 3. If Type(obj) is not Object, throw a TypeError exception.
628 if (!object->IsJSReceiver()) {
629 THROW_NEW_ERROR(isolate,
630 NewTypeError(MessageTemplate::kCalledOnNonObject,
631 isolate->factory()->NewStringFromAsciiChecked(
632 "CreateListFromArrayLike")),
633 FixedArray);
634 }
635 // 4. Let len be ? ToLength(? Get(obj, "length")).
Ben Murdochda12d292016-06-02 14:46:10 +0100636 Handle<JSReceiver> receiver = Handle<JSReceiver>::cast(object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000637 Handle<Object> raw_length_obj;
638 ASSIGN_RETURN_ON_EXCEPTION(
639 isolate, raw_length_obj,
Ben Murdochda12d292016-06-02 14:46:10 +0100640 JSReceiver::GetProperty(receiver, isolate->factory()->length_string()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000641 FixedArray);
642 Handle<Object> raw_length_number;
643 ASSIGN_RETURN_ON_EXCEPTION(isolate, raw_length_number,
644 Object::ToLength(isolate, raw_length_obj),
645 FixedArray);
646 uint32_t len;
647 if (!raw_length_number->ToUint32(&len) ||
648 len > static_cast<uint32_t>(FixedArray::kMaxLength)) {
649 THROW_NEW_ERROR(isolate,
650 NewRangeError(MessageTemplate::kInvalidArrayLength),
651 FixedArray);
652 }
653 // 5. Let list be an empty List.
654 Handle<FixedArray> list = isolate->factory()->NewFixedArray(len);
655 // 6. Let index be 0.
656 // 7. Repeat while index < len:
657 for (uint32_t index = 0; index < len; ++index) {
658 // 7a. Let indexName be ToString(index).
659 // 7b. Let next be ? Get(obj, indexName).
660 Handle<Object> next;
Ben Murdochda12d292016-06-02 14:46:10 +0100661 ASSIGN_RETURN_ON_EXCEPTION(isolate, next,
662 JSReceiver::GetElement(isolate, receiver, index),
663 FixedArray);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000664 switch (element_types) {
665 case ElementTypes::kAll:
666 // Nothing to do.
667 break;
668 case ElementTypes::kStringAndSymbol: {
669 // 7c. If Type(next) is not an element of elementTypes, throw a
670 // TypeError exception.
671 if (!next->IsName()) {
672 THROW_NEW_ERROR(isolate,
673 NewTypeError(MessageTemplate::kNotPropertyName, next),
674 FixedArray);
675 }
676 // 7d. Append next as the last element of list.
677 // Internalize on the fly so we can use pointer identity later.
678 next = isolate->factory()->InternalizeName(Handle<Name>::cast(next));
679 break;
680 }
681 }
682 list->set(index, *next);
683 // 7e. Set index to index + 1. (See loop header.)
684 }
685 // 8. Return list.
686 return list;
687}
688
689
690// static
691Maybe<bool> JSReceiver::HasProperty(LookupIterator* it) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000692 for (; it->IsFound(); it->Next()) {
693 switch (it->state()) {
694 case LookupIterator::NOT_FOUND:
695 case LookupIterator::TRANSITION:
696 UNREACHABLE();
697 case LookupIterator::JSPROXY:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000698 return JSProxy::HasProperty(it->isolate(), it->GetHolder<JSProxy>(),
699 it->GetName());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000700 case LookupIterator::INTERCEPTOR: {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000701 Maybe<PropertyAttributes> result =
702 JSObject::GetPropertyAttributesWithInterceptor(it);
Ben Murdochda12d292016-06-02 14:46:10 +0100703 if (result.IsNothing()) return Nothing<bool>();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000704 if (result.FromJust() != ABSENT) return Just(true);
705 break;
706 }
707 case LookupIterator::ACCESS_CHECK: {
708 if (it->HasAccess()) break;
709 Maybe<PropertyAttributes> result =
710 JSObject::GetPropertyAttributesWithFailedAccessCheck(it);
Ben Murdochda12d292016-06-02 14:46:10 +0100711 if (result.IsNothing()) return Nothing<bool>();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000712 return Just(result.FromJust() != ABSENT);
713 }
714 case LookupIterator::INTEGER_INDEXED_EXOTIC:
715 // TypedArray out-of-bounds access.
716 return Just(false);
717 case LookupIterator::ACCESSOR:
718 case LookupIterator::DATA:
719 return Just(true);
720 }
721 }
722 return Just(false);
723}
724
725
726// static
Ben Murdoch097c5b22016-05-18 11:27:45 +0100727MaybeHandle<Object> Object::GetProperty(LookupIterator* it) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000728 for (; it->IsFound(); it->Next()) {
729 switch (it->state()) {
730 case LookupIterator::NOT_FOUND:
731 case LookupIterator::TRANSITION:
732 UNREACHABLE();
Ben Murdochda12d292016-06-02 14:46:10 +0100733 case LookupIterator::JSPROXY: {
734 bool was_found;
735 MaybeHandle<Object> result =
736 JSProxy::GetProperty(it->isolate(), it->GetHolder<JSProxy>(),
737 it->GetName(), it->GetReceiver(), &was_found);
738 if (!was_found) it->NotFound();
739 return result;
740 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000741 case LookupIterator::INTERCEPTOR: {
742 bool done;
743 Handle<Object> result;
744 ASSIGN_RETURN_ON_EXCEPTION(
745 it->isolate(), result,
746 JSObject::GetPropertyWithInterceptor(it, &done), Object);
747 if (done) return result;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000748 break;
749 }
750 case LookupIterator::ACCESS_CHECK:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000751 if (it->HasAccess()) break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000752 return JSObject::GetPropertyWithFailedAccessCheck(it);
753 case LookupIterator::ACCESSOR:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100754 return GetPropertyWithAccessor(it);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000755 case LookupIterator::INTEGER_INDEXED_EXOTIC:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100756 return ReadAbsentProperty(it);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000757 case LookupIterator::DATA:
758 return it->GetDataValue();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100759 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000760 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100761 return ReadAbsentProperty(it);
Steve Blocka7e24c12009-10-30 11:49:00 +0000762}
763
764
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000765#define STACK_CHECK(result_value) \
766 do { \
767 StackLimitCheck stack_check(isolate); \
768 if (stack_check.HasOverflowed()) { \
769 isolate->Throw(*isolate->factory()->NewRangeError( \
770 MessageTemplate::kStackOverflow)); \
771 return result_value; \
772 } \
773 } while (false)
774
775
776// static
777MaybeHandle<Object> JSProxy::GetProperty(Isolate* isolate,
778 Handle<JSProxy> proxy,
779 Handle<Name> name,
Ben Murdochda12d292016-06-02 14:46:10 +0100780 Handle<Object> receiver,
781 bool* was_found) {
782 *was_found = true;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000783 if (receiver->IsJSGlobalObject()) {
784 THROW_NEW_ERROR(
785 isolate,
786 NewTypeError(MessageTemplate::kReadGlobalReferenceThroughProxy, name),
787 Object);
788 }
789
790 DCHECK(!name->IsPrivate());
791 STACK_CHECK(MaybeHandle<Object>());
792 Handle<Name> trap_name = isolate->factory()->get_string();
793 // 1. Assert: IsPropertyKey(P) is true.
794 // 2. Let handler be the value of the [[ProxyHandler]] internal slot of O.
795 Handle<Object> handler(proxy->handler(), isolate);
796 // 3. If handler is null, throw a TypeError exception.
797 // 4. Assert: Type(handler) is Object.
798 if (proxy->IsRevoked()) {
799 THROW_NEW_ERROR(isolate,
800 NewTypeError(MessageTemplate::kProxyRevoked, trap_name),
801 Object);
802 }
803 // 5. Let target be the value of the [[ProxyTarget]] internal slot of O.
804 Handle<JSReceiver> target(proxy->target(), isolate);
805 // 6. Let trap be ? GetMethod(handler, "get").
806 Handle<Object> trap;
807 ASSIGN_RETURN_ON_EXCEPTION(
808 isolate, trap,
809 Object::GetMethod(Handle<JSReceiver>::cast(handler), trap_name), Object);
810 // 7. If trap is undefined, then
811 if (trap->IsUndefined()) {
812 // 7.a Return target.[[Get]](P, Receiver).
813 LookupIterator it =
814 LookupIterator::PropertyOrElement(isolate, receiver, name, target);
Ben Murdochda12d292016-06-02 14:46:10 +0100815 MaybeHandle<Object> result = Object::GetProperty(&it);
816 *was_found = it.IsFound();
817 return result;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000818 }
819 // 8. Let trapResult be ? Call(trap, handler, «target, P, Receiver»).
820 Handle<Object> trap_result;
821 Handle<Object> args[] = {target, name, receiver};
822 ASSIGN_RETURN_ON_EXCEPTION(
823 isolate, trap_result,
824 Execution::Call(isolate, trap, handler, arraysize(args), args), Object);
825 // 9. Let targetDesc be ? target.[[GetOwnProperty]](P).
826 PropertyDescriptor target_desc;
827 Maybe<bool> target_found =
828 JSReceiver::GetOwnPropertyDescriptor(isolate, target, name, &target_desc);
829 MAYBE_RETURN_NULL(target_found);
830 // 10. If targetDesc is not undefined, then
831 if (target_found.FromJust()) {
832 // 10.a. If IsDataDescriptor(targetDesc) and targetDesc.[[Configurable]] is
833 // false and targetDesc.[[Writable]] is false, then
834 // 10.a.i. If SameValue(trapResult, targetDesc.[[Value]]) is false,
835 // throw a TypeError exception.
836 bool inconsistent = PropertyDescriptor::IsDataDescriptor(&target_desc) &&
837 !target_desc.configurable() &&
838 !target_desc.writable() &&
839 !trap_result->SameValue(*target_desc.value());
840 if (inconsistent) {
841 THROW_NEW_ERROR(
842 isolate, NewTypeError(MessageTemplate::kProxyGetNonConfigurableData,
843 name, target_desc.value(), trap_result),
844 Object);
845 }
846 // 10.b. If IsAccessorDescriptor(targetDesc) and targetDesc.[[Configurable]]
847 // is false and targetDesc.[[Get]] is undefined, then
848 // 10.b.i. If trapResult is not undefined, throw a TypeError exception.
849 inconsistent = PropertyDescriptor::IsAccessorDescriptor(&target_desc) &&
850 !target_desc.configurable() &&
851 target_desc.get()->IsUndefined() &&
852 !trap_result->IsUndefined();
853 if (inconsistent) {
854 THROW_NEW_ERROR(
855 isolate,
856 NewTypeError(MessageTemplate::kProxyGetNonConfigurableAccessor, name,
857 trap_result),
858 Object);
859 }
860 }
861 // 11. Return trap_result
862 return trap_result;
863}
864
865
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000866Handle<Object> JSReceiver::GetDataProperty(LookupIterator* it) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000867 for (; it->IsFound(); it->Next()) {
868 switch (it->state()) {
869 case LookupIterator::INTERCEPTOR:
870 case LookupIterator::NOT_FOUND:
871 case LookupIterator::TRANSITION:
872 UNREACHABLE();
873 case LookupIterator::ACCESS_CHECK:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000874 // Support calling this method without an active context, but refuse
875 // access to access-checked objects in that case.
876 if (it->isolate()->context() != nullptr && it->HasAccess()) continue;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000877 // Fall through.
878 case LookupIterator::JSPROXY:
879 it->NotFound();
880 return it->isolate()->factory()->undefined_value();
881 case LookupIterator::ACCESSOR:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100882 // TODO(verwaest): For now this doesn't call into AccessorInfo, since
883 // clients don't need it. Update once relevant.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000884 it->NotFound();
885 return it->isolate()->factory()->undefined_value();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000886 case LookupIterator::INTEGER_INDEXED_EXOTIC:
887 return it->isolate()->factory()->undefined_value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000888 case LookupIterator::DATA:
889 return it->GetDataValue();
890 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000891 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000892 return it->isolate()->factory()->undefined_value();
893}
Steve Blocka7e24c12009-10-30 11:49:00 +0000894
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000895
896bool Object::ToInt32(int32_t* value) {
897 if (IsSmi()) {
898 *value = Smi::cast(this)->value();
899 return true;
900 }
901 if (IsHeapNumber()) {
902 double num = HeapNumber::cast(this)->value();
903 if (FastI2D(FastD2I(num)) == num) {
904 *value = FastD2I(num);
905 return true;
906 }
907 }
908 return false;
909}
910
911
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000912bool FunctionTemplateInfo::IsTemplateFor(Object* object) {
913 if (!object->IsHeapObject()) return false;
914 return IsTemplateFor(HeapObject::cast(object)->map());
915}
916
917
918bool FunctionTemplateInfo::IsTemplateFor(Map* map) {
919 // There is a constraint on the object; check.
920 if (!map->IsJSObjectMap()) return false;
921 // Fetch the constructor function of the object.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000922 Object* cons_obj = map->GetConstructor();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000923 if (!cons_obj->IsJSFunction()) return false;
924 JSFunction* fun = JSFunction::cast(cons_obj);
925 // Iterate through the chain of inheriting function templates to
926 // see if the required one occurs.
927 for (Object* type = fun->shared()->function_data();
928 type->IsFunctionTemplateInfo();
929 type = FunctionTemplateInfo::cast(type)->parent_template()) {
930 if (type == this) return true;
931 }
932 // Didn't find the required type in the inheritance chain.
933 return false;
934}
935
936
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000937// TODO(dcarney): CallOptimization duplicates this logic, merge.
938Object* FunctionTemplateInfo::GetCompatibleReceiver(Isolate* isolate,
939 Object* receiver) {
940 // API calls are only supported with JSObject receivers.
941 if (!receiver->IsJSObject()) return isolate->heap()->null_value();
942 Object* recv_type = this->signature();
943 // No signature, return holder.
944 if (recv_type->IsUndefined()) return receiver;
945 FunctionTemplateInfo* signature = FunctionTemplateInfo::cast(recv_type);
946 // Check the receiver.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100947 for (PrototypeIterator iter(isolate, JSObject::cast(receiver),
948 PrototypeIterator::START_AT_RECEIVER,
949 PrototypeIterator::END_AT_NON_HIDDEN);
950 !iter.IsAtEnd(); iter.Advance()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000951 if (signature->IsTemplateFor(iter.GetCurrent())) return iter.GetCurrent();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000952 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000953 return isolate->heap()->null_value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000954}
955
956
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000957// static
958MaybeHandle<JSObject> JSObject::New(Handle<JSFunction> constructor,
959 Handle<JSReceiver> new_target,
960 Handle<AllocationSite> site) {
961 // If called through new, new.target can be:
962 // - a subclass of constructor,
963 // - a proxy wrapper around constructor, or
964 // - the constructor itself.
965 // If called through Reflect.construct, it's guaranteed to be a constructor.
966 Isolate* const isolate = constructor->GetIsolate();
967 DCHECK(constructor->IsConstructor());
968 DCHECK(new_target->IsConstructor());
969 DCHECK(!constructor->has_initial_map() ||
970 constructor->initial_map()->instance_type() != JS_FUNCTION_TYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000971
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000972 Handle<Map> initial_map;
973 ASSIGN_RETURN_ON_EXCEPTION(
974 isolate, initial_map,
975 JSFunction::GetDerivedMap(isolate, constructor, new_target), JSObject);
976 Handle<JSObject> result =
977 isolate->factory()->NewJSObjectFromMap(initial_map, NOT_TENURED, site);
978 isolate->counters()->constructed_objects()->Increment();
979 isolate->counters()->constructed_objects_runtime()->Increment();
980 return result;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000981}
982
Ben Murdochda12d292016-06-02 14:46:10 +0100983void JSObject::EnsureWritableFastElements(Handle<JSObject> object) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100984 DCHECK(object->HasFastSmiOrObjectElements() ||
985 object->HasFastStringWrapperElements());
Ben Murdochda12d292016-06-02 14:46:10 +0100986 FixedArray* raw_elems = FixedArray::cast(object->elements());
987 Heap* heap = object->GetHeap();
988 if (raw_elems->map() != heap->fixed_cow_array_map()) return;
989 Isolate* isolate = heap->isolate();
990 Handle<FixedArray> elems(raw_elems, isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000991 Handle<FixedArray> writable_elems = isolate->factory()->CopyFixedArrayWithMap(
992 elems, isolate->factory()->fixed_array_map());
993 object->set_elements(*writable_elems);
994 isolate->counters()->cow_arrays_converted()->Increment();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000995}
996
997
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000998// ES6 9.5.1
999// static
1000MaybeHandle<Object> JSProxy::GetPrototype(Handle<JSProxy> proxy) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001001 Isolate* isolate = proxy->GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001002 Handle<String> trap_name = isolate->factory()->getPrototypeOf_string();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001003
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001004 STACK_CHECK(MaybeHandle<Object>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001005
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001006 // 1. Let handler be the value of the [[ProxyHandler]] internal slot.
1007 // 2. If handler is null, throw a TypeError exception.
1008 // 3. Assert: Type(handler) is Object.
1009 // 4. Let target be the value of the [[ProxyTarget]] internal slot.
1010 if (proxy->IsRevoked()) {
1011 THROW_NEW_ERROR(isolate,
1012 NewTypeError(MessageTemplate::kProxyRevoked, trap_name),
1013 Object);
1014 }
1015 Handle<JSReceiver> target(proxy->target(), isolate);
1016 Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate);
1017
1018 // 5. Let trap be ? GetMethod(handler, "getPrototypeOf").
1019 Handle<Object> trap;
1020 ASSIGN_RETURN_ON_EXCEPTION(isolate, trap, GetMethod(handler, trap_name),
1021 Object);
1022 // 6. If trap is undefined, then return target.[[GetPrototypeOf]]().
1023 if (trap->IsUndefined()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001024 return JSReceiver::GetPrototype(isolate, target);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001025 }
1026 // 7. Let handlerProto be ? Call(trap, handler, «target»).
1027 Handle<Object> argv[] = {target};
1028 Handle<Object> handler_proto;
1029 ASSIGN_RETURN_ON_EXCEPTION(
1030 isolate, handler_proto,
1031 Execution::Call(isolate, trap, handler, arraysize(argv), argv), Object);
1032 // 8. If Type(handlerProto) is neither Object nor Null, throw a TypeError.
1033 if (!(handler_proto->IsJSReceiver() || handler_proto->IsNull())) {
1034 THROW_NEW_ERROR(isolate,
1035 NewTypeError(MessageTemplate::kProxyGetPrototypeOfInvalid),
1036 Object);
1037 }
1038 // 9. Let extensibleTarget be ? IsExtensible(target).
1039 Maybe<bool> is_extensible = JSReceiver::IsExtensible(target);
1040 MAYBE_RETURN_NULL(is_extensible);
1041 // 10. If extensibleTarget is true, return handlerProto.
1042 if (is_extensible.FromJust()) return handler_proto;
1043 // 11. Let targetProto be ? target.[[GetPrototypeOf]]().
1044 Handle<Object> target_proto;
1045 ASSIGN_RETURN_ON_EXCEPTION(isolate, target_proto,
Ben Murdoch097c5b22016-05-18 11:27:45 +01001046 JSReceiver::GetPrototype(isolate, target), Object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001047 // 12. If SameValue(handlerProto, targetProto) is false, throw a TypeError.
1048 if (!handler_proto->SameValue(*target_proto)) {
1049 THROW_NEW_ERROR(
1050 isolate,
1051 NewTypeError(MessageTemplate::kProxyGetPrototypeOfNonExtensible),
1052 Object);
1053 }
1054 // 13. Return handlerProto.
1055 return handler_proto;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001056}
1057
Ben Murdoch097c5b22016-05-18 11:27:45 +01001058MaybeHandle<Object> Object::GetPropertyWithAccessor(LookupIterator* it) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001059 Isolate* isolate = it->isolate();
1060 Handle<Object> structure = it->GetAccessors();
1061 Handle<Object> receiver = it->GetReceiver();
1062
1063 // We should never get here to initialize a const with the hole value since a
1064 // const declaration would conflict with the getter.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001065 DCHECK(!structure->IsForeign());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001066
1067 // API style callbacks.
Steve Blocka7e24c12009-10-30 11:49:00 +00001068 if (structure->IsAccessorInfo()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001069 Handle<JSObject> holder = it->GetHolder<JSObject>();
1070 Handle<Name> name = it->GetName();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001071 Handle<AccessorInfo> info = Handle<AccessorInfo>::cast(structure);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001072 if (!info->IsCompatibleReceiver(*receiver)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001073 THROW_NEW_ERROR(isolate,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001074 NewTypeError(MessageTemplate::kIncompatibleMethodReceiver,
1075 name, receiver),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001076 Object);
Steve Blocka7e24c12009-10-30 11:49:00 +00001077 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001078
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001079 v8::AccessorNameGetterCallback call_fun =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001080 v8::ToCData<v8::AccessorNameGetterCallback>(info->getter());
1081 if (call_fun == nullptr) return isolate->factory()->undefined_value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001082
Ben Murdochda12d292016-06-02 14:46:10 +01001083 if (info->is_sloppy() && !receiver->IsJSReceiver()) {
1084 ASSIGN_RETURN_ON_EXCEPTION(isolate, receiver,
1085 Object::ConvertReceiver(isolate, receiver),
1086 Object);
1087 }
1088
Ben Murdoch097c5b22016-05-18 11:27:45 +01001089 PropertyCallbackArguments args(isolate, info->data(), *receiver, *holder,
1090 Object::DONT_THROW);
Ben Murdochda12d292016-06-02 14:46:10 +01001091 Handle<Object> result = args.Call(call_fun, name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001092 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
Ben Murdochda12d292016-06-02 14:46:10 +01001093 if (result.is_null()) return ReadAbsentProperty(isolate, receiver, name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001094 // Rebox handle before return.
Ben Murdochda12d292016-06-02 14:46:10 +01001095 return handle(*result, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001096 }
1097
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001098 // Regular accessor.
1099 Handle<Object> getter(AccessorPair::cast(*structure)->getter(), isolate);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001100 if (getter->IsFunctionTemplateInfo()) {
1101 auto result = Builtins::InvokeApiFunction(
1102 Handle<FunctionTemplateInfo>::cast(getter), receiver, 0, nullptr);
1103 if (isolate->has_pending_exception()) {
1104 return MaybeHandle<Object>();
1105 }
1106 Handle<Object> return_value;
1107 if (result.ToHandle(&return_value)) {
1108 return_value->VerifyApiCallResultType();
1109 return handle(*return_value, isolate);
1110 }
1111 } else if (getter->IsCallable()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001112 // TODO(rossberg): nicer would be to cast to some JSCallable here...
1113 return Object::GetPropertyWithDefinedGetter(
1114 receiver, Handle<JSReceiver>::cast(getter));
1115 }
1116 // Getter is not a function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001117 return ReadAbsentProperty(isolate, receiver, it->GetName());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001118}
1119
1120
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001121bool AccessorInfo::IsCompatibleReceiverMap(Isolate* isolate,
1122 Handle<AccessorInfo> info,
1123 Handle<Map> map) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001124 if (!info->HasExpectedReceiverType()) return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001125 if (!map->IsJSObjectMap()) return false;
1126 return FunctionTemplateInfo::cast(info->expected_receiver_type())
1127 ->IsTemplateFor(*map);
1128}
1129
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001130Maybe<bool> Object::SetPropertyWithAccessor(LookupIterator* it,
1131 Handle<Object> value,
1132 ShouldThrow should_throw) {
1133 Isolate* isolate = it->isolate();
1134 Handle<Object> structure = it->GetAccessors();
1135 Handle<Object> receiver = it->GetReceiver();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001136
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001137 // We should never get here to initialize a const with the hole value since a
1138 // const declaration would conflict with the setter.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001139 DCHECK(!structure->IsForeign());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001140
1141 // API style callbacks.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001142 if (structure->IsAccessorInfo()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001143 Handle<JSObject> holder = it->GetHolder<JSObject>();
1144 Handle<Name> name = it->GetName();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001145 Handle<AccessorInfo> info = Handle<AccessorInfo>::cast(structure);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001146 if (!info->IsCompatibleReceiver(*receiver)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001147 isolate->Throw(*isolate->factory()->NewTypeError(
1148 MessageTemplate::kIncompatibleMethodReceiver, name, receiver));
1149 return Nothing<bool>();
Steve Blocka7e24c12009-10-30 11:49:00 +00001150 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001151
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001152 v8::AccessorNameSetterCallback call_fun =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001153 v8::ToCData<v8::AccessorNameSetterCallback>(info->setter());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001154 // TODO(verwaest): We should not get here anymore once all AccessorInfos are
1155 // marked as special_data_property. They cannot both be writable and not
1156 // have a setter.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001157 if (call_fun == nullptr) return Just(true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001158
Ben Murdochda12d292016-06-02 14:46:10 +01001159 if (info->is_sloppy() && !receiver->IsJSReceiver()) {
1160 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
1161 isolate, receiver, Object::ConvertReceiver(isolate, receiver),
1162 Nothing<bool>());
1163 }
1164
Ben Murdoch097c5b22016-05-18 11:27:45 +01001165 PropertyCallbackArguments args(isolate, info->data(), *receiver, *holder,
1166 should_throw);
Ben Murdochda12d292016-06-02 14:46:10 +01001167 args.Call(call_fun, name, value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001168 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>());
1169 return Just(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001170 }
1171
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001172 // Regular accessor.
1173 Handle<Object> setter(AccessorPair::cast(*structure)->setter(), isolate);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001174 if (setter->IsFunctionTemplateInfo()) {
1175 Handle<Object> argv[] = {value};
1176 auto result =
1177 Builtins::InvokeApiFunction(Handle<FunctionTemplateInfo>::cast(setter),
1178 receiver, arraysize(argv), argv);
1179 if (isolate->has_pending_exception()) {
1180 return Nothing<bool>();
1181 }
1182 return Just(true);
1183 } else if (setter->IsCallable()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001184 // TODO(rossberg): nicer would be to cast to some JSCallable here...
1185 return SetPropertyWithDefinedSetter(
1186 receiver, Handle<JSReceiver>::cast(setter), value, should_throw);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001187 }
1188
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001189 RETURN_FAILURE(isolate, should_throw,
1190 NewTypeError(MessageTemplate::kNoSetterInCallback,
1191 it->GetName(), it->GetHolder<JSObject>()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001192}
1193
1194
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001195MaybeHandle<Object> Object::GetPropertyWithDefinedGetter(
1196 Handle<Object> receiver,
1197 Handle<JSReceiver> getter) {
1198 Isolate* isolate = getter->GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001199
1200 // Platforms with simulators like arm/arm64 expose a funny issue. If the
1201 // simulator has a separate JS stack pointer from the C++ stack pointer, it
1202 // can miss C++ stack overflows in the stack guard at the start of JavaScript
1203 // functions. It would be very expensive to check the C++ stack pointer at
1204 // that location. The best solution seems to be to break the impasse by
1205 // adding checks at possible recursion points. What's more, we don't put
1206 // this stack check behind the USE_SIMULATOR define in order to keep
1207 // behavior the same between hardware and simulators.
1208 StackLimitCheck check(isolate);
1209 if (check.JsHasOverflowed()) {
1210 isolate->StackOverflow();
1211 return MaybeHandle<Object>();
Steve Blocka7e24c12009-10-30 11:49:00 +00001212 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001213
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001214 return Execution::Call(isolate, getter, receiver, 0, NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +00001215}
1216
1217
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001218Maybe<bool> Object::SetPropertyWithDefinedSetter(Handle<Object> receiver,
1219 Handle<JSReceiver> setter,
1220 Handle<Object> value,
1221 ShouldThrow should_throw) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001222 Isolate* isolate = setter->GetIsolate();
1223
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001224 Handle<Object> argv[] = { value };
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001225 RETURN_ON_EXCEPTION_VALUE(isolate, Execution::Call(isolate, setter, receiver,
1226 arraysize(argv), argv),
1227 Nothing<bool>());
1228 return Just(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001229}
1230
1231
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001232// static
1233bool Object::IsErrorObject(Isolate* isolate, Handle<Object> object) {
1234 if (!object->IsJSObject()) return false;
1235 // Use stack_trace_symbol as proxy for [[ErrorData]].
1236 Handle<Name> symbol = isolate->factory()->stack_trace_symbol();
1237 Maybe<bool> has_stack_trace =
1238 JSReceiver::HasOwnProperty(Handle<JSReceiver>::cast(object), symbol);
1239 DCHECK(!has_stack_trace.IsNothing());
1240 return has_stack_trace.FromJust();
1241}
1242
1243
1244// static
1245bool JSObject::AllCanRead(LookupIterator* it) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001246 // Skip current iteration, it's in state ACCESS_CHECK or INTERCEPTOR, both of
1247 // which have already been checked.
1248 DCHECK(it->state() == LookupIterator::ACCESS_CHECK ||
1249 it->state() == LookupIterator::INTERCEPTOR);
1250 for (it->Next(); it->IsFound(); it->Next()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001251 if (it->state() == LookupIterator::ACCESSOR) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001252 auto accessors = it->GetAccessors();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001253 if (accessors->IsAccessorInfo()) {
1254 if (AccessorInfo::cast(*accessors)->all_can_read()) return true;
1255 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001256 } else if (it->state() == LookupIterator::INTERCEPTOR) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001257 if (it->GetInterceptor()->all_can_read()) return true;
1258 } else if (it->state() == LookupIterator::JSPROXY) {
1259 // Stop lookupiterating. And no, AllCanNotRead.
1260 return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001261 }
1262 }
1263 return false;
1264}
1265
1266
1267MaybeHandle<Object> JSObject::GetPropertyWithFailedAccessCheck(
1268 LookupIterator* it) {
1269 Handle<JSObject> checked = it->GetHolder<JSObject>();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001270 while (AllCanRead(it)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001271 if (it->state() == LookupIterator::ACCESSOR) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001272 return GetPropertyWithAccessor(it);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001273 }
1274 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001275 bool done;
1276 Handle<Object> result;
1277 ASSIGN_RETURN_ON_EXCEPTION(it->isolate(), result,
1278 GetPropertyWithInterceptor(it, &done), Object);
1279 if (done) return result;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001280 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001281
1282 // Cross-Origin [[Get]] of Well-Known Symbols does not throw, and returns
1283 // undefined.
1284 Handle<Name> name = it->GetName();
1285 if (name->IsSymbol() && Symbol::cast(*name)->is_well_known_symbol()) {
1286 return it->factory()->undefined_value();
1287 }
1288
1289 it->isolate()->ReportFailedAccessCheck(checked);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001290 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(it->isolate(), Object);
1291 return it->factory()->undefined_value();
1292}
1293
1294
1295Maybe<PropertyAttributes> JSObject::GetPropertyAttributesWithFailedAccessCheck(
1296 LookupIterator* it) {
1297 Handle<JSObject> checked = it->GetHolder<JSObject>();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001298 while (AllCanRead(it)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001299 if (it->state() == LookupIterator::ACCESSOR) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001300 return Just(it->property_attributes());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001301 }
1302 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001303 auto result = GetPropertyAttributesWithInterceptor(it);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001304 if (it->isolate()->has_scheduled_exception()) break;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001305 if (result.IsJust() && result.FromJust() != ABSENT) return result;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001306 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001307 it->isolate()->ReportFailedAccessCheck(checked);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001308 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(it->isolate(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001309 Nothing<PropertyAttributes>());
1310 return Just(ABSENT);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001311}
1312
1313
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001314// static
1315bool JSObject::AllCanWrite(LookupIterator* it) {
1316 for (; it->IsFound() && it->state() != LookupIterator::JSPROXY; it->Next()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001317 if (it->state() == LookupIterator::ACCESSOR) {
1318 Handle<Object> accessors = it->GetAccessors();
1319 if (accessors->IsAccessorInfo()) {
1320 if (AccessorInfo::cast(*accessors)->all_can_write()) return true;
1321 }
1322 }
1323 }
1324 return false;
1325}
1326
1327
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001328Maybe<bool> JSObject::SetPropertyWithFailedAccessCheck(
1329 LookupIterator* it, Handle<Object> value, ShouldThrow should_throw) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001330 Handle<JSObject> checked = it->GetHolder<JSObject>();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001331 if (AllCanWrite(it)) {
1332 return SetPropertyWithAccessor(it, value, should_throw);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001333 }
1334
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001335 it->isolate()->ReportFailedAccessCheck(checked);
1336 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(it->isolate(), Nothing<bool>());
1337 return Just(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001338}
1339
1340
1341void JSObject::SetNormalizedProperty(Handle<JSObject> object,
1342 Handle<Name> name,
1343 Handle<Object> value,
1344 PropertyDetails details) {
1345 DCHECK(!object->HasFastProperties());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001346 if (!name->IsUniqueName()) {
1347 name = object->GetIsolate()->factory()->InternalizeString(
1348 Handle<String>::cast(name));
1349 }
1350
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001351 if (object->IsJSGlobalObject()) {
1352 Handle<GlobalDictionary> property_dictionary(object->global_dictionary());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001353
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001354 int entry = property_dictionary->FindEntry(name);
1355 if (entry == GlobalDictionary::kNotFound) {
1356 auto cell = object->GetIsolate()->factory()->NewPropertyCell();
1357 cell->set_value(*value);
1358 auto cell_type = value->IsUndefined() ? PropertyCellType::kUndefined
1359 : PropertyCellType::kConstant;
1360 details = details.set_cell_type(cell_type);
1361 value = cell;
1362 property_dictionary =
1363 GlobalDictionary::Add(property_dictionary, name, value, details);
1364 object->set_properties(*property_dictionary);
Steve Blocka7e24c12009-10-30 11:49:00 +00001365 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001366 PropertyCell::UpdateCell(property_dictionary, entry, value, details);
1367 }
1368 } else {
1369 Handle<NameDictionary> property_dictionary(object->property_dictionary());
1370
1371 int entry = property_dictionary->FindEntry(name);
1372 if (entry == NameDictionary::kNotFound) {
1373 property_dictionary =
1374 NameDictionary::Add(property_dictionary, name, value, details);
1375 object->set_properties(*property_dictionary);
1376 } else {
1377 PropertyDetails original_details = property_dictionary->DetailsAt(entry);
1378 int enumeration_index = original_details.dictionary_index();
1379 DCHECK(enumeration_index > 0);
1380 details = details.set_index(enumeration_index);
1381 property_dictionary->SetEntry(entry, name, value, details);
Steve Blocka7e24c12009-10-30 11:49:00 +00001382 }
1383 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001384}
1385
Ben Murdoch097c5b22016-05-18 11:27:45 +01001386Maybe<bool> JSReceiver::HasInPrototypeChain(Isolate* isolate,
1387 Handle<JSReceiver> object,
1388 Handle<Object> proto) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001389 PrototypeIterator iter(isolate, object, PrototypeIterator::START_AT_RECEIVER);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001390 while (true) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001391 if (!iter.AdvanceFollowingProxies()) return Nothing<bool>();
1392 if (iter.IsAtEnd()) return Just(false);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001393 if (PrototypeIterator::GetCurrent(iter).is_identical_to(proto)) {
1394 return Just(true);
1395 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001396 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001397}
1398
1399
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001400Map* Object::GetRootMap(Isolate* isolate) {
1401 DisallowHeapAllocation no_alloc;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001402 if (IsSmi()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001403 Context* native_context = isolate->context()->native_context();
1404 return native_context->number_function()->initial_map();
Ben Murdoch8b112d22011-06-08 16:22:53 +01001405 }
1406
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001407 // The object is either a number, a string, a symbol, a boolean, a SIMD value,
Ben Murdoch257744e2011-11-30 15:57:28 +00001408 // a real JS object, or a Harmony proxy.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001409 HeapObject* heap_object = HeapObject::cast(this);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001410 if (heap_object->IsJSReceiver()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001411 return heap_object->map();
Ben Murdoch8b112d22011-06-08 16:22:53 +01001412 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001413 int constructor_function_index =
1414 heap_object->map()->GetConstructorFunctionIndex();
1415 if (constructor_function_index != Map::kNoConstructorFunctionIndex) {
1416 Context* native_context = isolate->context()->native_context();
1417 JSFunction* constructor_function =
1418 JSFunction::cast(native_context->get(constructor_function_index));
1419 return constructor_function->initial_map();
Steve Blocka7e24c12009-10-30 11:49:00 +00001420 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001421 return isolate->heap()->null_value()->map();
Steve Blocka7e24c12009-10-30 11:49:00 +00001422}
1423
1424
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001425Object* Object::GetHash() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001426 Object* hash = GetSimpleHash();
1427 if (hash->IsSmi()) return hash;
1428
Ben Murdochda12d292016-06-02 14:46:10 +01001429 DisallowHeapAllocation no_gc;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001430 DCHECK(IsJSReceiver());
Ben Murdochda12d292016-06-02 14:46:10 +01001431 JSReceiver* receiver = JSReceiver::cast(this);
1432 Isolate* isolate = receiver->GetIsolate();
1433 return *JSReceiver::GetIdentityHash(isolate, handle(receiver, isolate));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001434}
1435
1436
1437Object* Object::GetSimpleHash() {
1438 // The object is either a Smi, a HeapNumber, a name, an odd-ball,
1439 // a SIMD value type, a real JS object, or a Harmony proxy.
1440 if (IsSmi()) {
1441 uint32_t hash = ComputeIntegerHash(Smi::cast(this)->value(), kZeroHashSeed);
1442 return Smi::FromInt(hash & Smi::kMaxValue);
1443 }
1444 if (IsHeapNumber()) {
1445 double num = HeapNumber::cast(this)->value();
1446 if (std::isnan(num)) return Smi::FromInt(Smi::kMaxValue);
1447 if (i::IsMinusZero(num)) num = 0;
1448 if (IsSmiDouble(num)) {
1449 return Smi::FromInt(FastD2I(num))->GetHash();
1450 }
1451 uint32_t hash = ComputeLongHash(double_to_uint64(num));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001452 return Smi::FromInt(hash & Smi::kMaxValue);
1453 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001454 if (IsName()) {
1455 uint32_t hash = Name::cast(this)->Hash();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001456 return Smi::FromInt(hash);
1457 }
1458 if (IsOddball()) {
1459 uint32_t hash = Oddball::cast(this)->to_string()->Hash();
1460 return Smi::FromInt(hash);
1461 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001462 if (IsSimd128Value()) {
1463 uint32_t hash = Simd128Value::cast(this)->Hash();
1464 return Smi::FromInt(hash & Smi::kMaxValue);
1465 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001466 DCHECK(IsJSReceiver());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001467 JSReceiver* receiver = JSReceiver::cast(this);
1468 return receiver->GetHeap()->undefined_value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001469}
1470
1471
1472Handle<Smi> Object::GetOrCreateHash(Isolate* isolate, Handle<Object> object) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001473 Handle<Object> hash(object->GetSimpleHash(), isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001474 if (hash->IsSmi()) return Handle<Smi>::cast(hash);
1475
1476 DCHECK(object->IsJSReceiver());
1477 return JSReceiver::GetOrCreateIdentityHash(Handle<JSReceiver>::cast(object));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001478}
1479
1480
1481bool Object::SameValue(Object* other) {
1482 if (other == this) return true;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001483
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001484 // The object is either a number, a name, an odd-ball,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001485 // a real JS object, or a Harmony proxy.
1486 if (IsNumber() && other->IsNumber()) {
1487 double this_value = Number();
1488 double other_value = other->Number();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001489 // SameValue(NaN, NaN) is true.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001490 if (this_value != other_value) {
1491 return std::isnan(this_value) && std::isnan(other_value);
1492 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001493 // SameValue(0.0, -0.0) is false.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001494 return (std::signbit(this_value) == std::signbit(other_value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001495 }
1496 if (IsString() && other->IsString()) {
1497 return String::cast(this)->Equals(String::cast(other));
1498 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001499 if (IsFloat32x4() && other->IsFloat32x4()) {
1500 Float32x4* a = Float32x4::cast(this);
1501 Float32x4* b = Float32x4::cast(other);
1502 for (int i = 0; i < 4; i++) {
1503 float x = a->get_lane(i);
1504 float y = b->get_lane(i);
1505 // Implements the ES5 SameValue operation for floating point types.
1506 // http://www.ecma-international.org/ecma-262/6.0/#sec-samevalue
1507 if (x != y && !(std::isnan(x) && std::isnan(y))) return false;
1508 if (std::signbit(x) != std::signbit(y)) return false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001509 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001510 return true;
1511 } else if (IsSimd128Value() && other->IsSimd128Value()) {
1512 Simd128Value* a = Simd128Value::cast(this);
1513 Simd128Value* b = Simd128Value::cast(other);
1514 return a->map() == b->map() && a->BitwiseEquals(b);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001515 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001516 return false;
1517}
1518
1519
1520bool Object::SameValueZero(Object* other) {
1521 if (other == this) return true;
1522
1523 // The object is either a number, a name, an odd-ball,
1524 // a real JS object, or a Harmony proxy.
1525 if (IsNumber() && other->IsNumber()) {
1526 double this_value = Number();
1527 double other_value = other->Number();
1528 // +0 == -0 is true
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001529 return this_value == other_value ||
1530 (std::isnan(this_value) && std::isnan(other_value));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001531 }
1532 if (IsString() && other->IsString()) {
1533 return String::cast(this)->Equals(String::cast(other));
1534 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001535 if (IsFloat32x4() && other->IsFloat32x4()) {
1536 Float32x4* a = Float32x4::cast(this);
1537 Float32x4* b = Float32x4::cast(other);
1538 for (int i = 0; i < 4; i++) {
1539 float x = a->get_lane(i);
1540 float y = b->get_lane(i);
1541 // Implements the ES6 SameValueZero operation for floating point types.
1542 // http://www.ecma-international.org/ecma-262/6.0/#sec-samevaluezero
1543 if (x != y && !(std::isnan(x) && std::isnan(y))) return false;
1544 // SameValueZero doesn't distinguish between 0 and -0.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001545 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001546 return true;
1547 } else if (IsSimd128Value() && other->IsSimd128Value()) {
1548 Simd128Value* a = Simd128Value::cast(this);
1549 Simd128Value* b = Simd128Value::cast(other);
1550 return a->map() == b->map() && a->BitwiseEquals(b);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001551 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001552 return false;
1553}
1554
1555
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001556MaybeHandle<Object> Object::ArraySpeciesConstructor(
1557 Isolate* isolate, Handle<Object> original_array) {
1558 Handle<Context> native_context = isolate->native_context();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001559 Handle<Object> default_species = isolate->array_function();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001560 if (!FLAG_harmony_species) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001561 return default_species;
1562 }
1563 if (original_array->IsJSArray() &&
Ben Murdochda12d292016-06-02 14:46:10 +01001564 Handle<JSArray>::cast(original_array)->HasArrayPrototype(isolate) &&
Ben Murdoch097c5b22016-05-18 11:27:45 +01001565 isolate->IsArraySpeciesLookupChainIntact()) {
1566 return default_species;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001567 }
1568 Handle<Object> constructor = isolate->factory()->undefined_value();
1569 Maybe<bool> is_array = Object::IsArray(original_array);
1570 MAYBE_RETURN_NULL(is_array);
1571 if (is_array.FromJust()) {
1572 ASSIGN_RETURN_ON_EXCEPTION(
1573 isolate, constructor,
1574 Object::GetProperty(original_array,
1575 isolate->factory()->constructor_string()),
1576 Object);
1577 if (constructor->IsConstructor()) {
1578 Handle<Context> constructor_context;
1579 ASSIGN_RETURN_ON_EXCEPTION(
1580 isolate, constructor_context,
1581 JSReceiver::GetFunctionRealm(Handle<JSReceiver>::cast(constructor)),
1582 Object);
1583 if (*constructor_context != *native_context &&
1584 *constructor == constructor_context->array_function()) {
1585 constructor = isolate->factory()->undefined_value();
1586 }
1587 }
1588 if (constructor->IsJSReceiver()) {
1589 ASSIGN_RETURN_ON_EXCEPTION(
1590 isolate, constructor,
Ben Murdochda12d292016-06-02 14:46:10 +01001591 JSReceiver::GetProperty(Handle<JSReceiver>::cast(constructor),
1592 isolate->factory()->species_symbol()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001593 Object);
1594 if (constructor->IsNull()) {
1595 constructor = isolate->factory()->undefined_value();
1596 }
1597 }
1598 }
1599 if (constructor->IsUndefined()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001600 return default_species;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001601 } else {
1602 if (!constructor->IsConstructor()) {
1603 THROW_NEW_ERROR(isolate,
1604 NewTypeError(MessageTemplate::kSpeciesNotConstructor),
1605 Object);
1606 }
1607 return constructor;
1608 }
1609}
1610
1611
Ben Murdochb0fe1622011-05-05 13:52:32 +01001612void Object::ShortPrint(FILE* out) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001613 OFStream os(out);
1614 os << Brief(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001615}
1616
1617
1618void Object::ShortPrint(StringStream* accumulator) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001619 std::ostringstream os;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001620 os << Brief(this);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001621 accumulator->Add(os.str().c_str());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001622}
1623
1624
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001625void Object::ShortPrint(std::ostream& os) { os << Brief(this); }
1626
1627
1628std::ostream& operator<<(std::ostream& os, const Brief& v) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001629 if (v.value->IsSmi()) {
1630 Smi::cast(v.value)->SmiPrint(os);
Steve Blocka7e24c12009-10-30 11:49:00 +00001631 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001632 // TODO(svenpanne) Const-correct HeapObjectShortPrint!
1633 HeapObject* obj = const_cast<HeapObject*>(HeapObject::cast(v.value));
1634 obj->HeapObjectShortPrint(os);
Steve Blocka7e24c12009-10-30 11:49:00 +00001635 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001636 return os;
Steve Blocka7e24c12009-10-30 11:49:00 +00001637}
1638
1639
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001640void Smi::SmiPrint(std::ostream& os) const { // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001641 os << value();
Steve Blocka7e24c12009-10-30 11:49:00 +00001642}
1643
1644
Steve Blocka7e24c12009-10-30 11:49:00 +00001645// Should a word be prefixed by 'a' or 'an' in order to read naturally in
1646// English? Returns false for non-ASCII or words that don't start with
1647// a capital letter. The a/an rule follows pronunciation in English.
1648// We don't use the BBC's overcorrect "an historic occasion" though if
1649// you speak a dialect you may well say "an 'istoric occasion".
1650static bool AnWord(String* str) {
1651 if (str->length() == 0) return false; // A nothing.
1652 int c0 = str->Get(0);
1653 int c1 = str->length() > 1 ? str->Get(1) : 0;
1654 if (c0 == 'U') {
1655 if (c1 > 'Z') {
1656 return true; // An Umpire, but a UTF8String, a U.
1657 }
1658 } else if (c0 == 'A' || c0 == 'E' || c0 == 'I' || c0 == 'O') {
1659 return true; // An Ape, an ABCBook.
1660 } else if ((c1 == 0 || (c1 >= 'A' && c1 <= 'Z')) &&
1661 (c0 == 'F' || c0 == 'H' || c0 == 'M' || c0 == 'N' || c0 == 'R' ||
1662 c0 == 'S' || c0 == 'X')) {
1663 return true; // An MP3File, an M.
1664 }
1665 return false;
1666}
1667
1668
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001669Handle<String> String::SlowFlatten(Handle<ConsString> cons,
1670 PretenureFlag pretenure) {
1671 DCHECK(AllowHeapAllocation::IsAllowed());
1672 DCHECK(cons->second()->length() != 0);
1673 Isolate* isolate = cons->GetIsolate();
1674 int length = cons->length();
1675 PretenureFlag tenure = isolate->heap()->InNewSpace(*cons) ? pretenure
1676 : TENURED;
1677 Handle<SeqString> result;
1678 if (cons->IsOneByteRepresentation()) {
1679 Handle<SeqOneByteString> flat = isolate->factory()->NewRawOneByteString(
1680 length, tenure).ToHandleChecked();
1681 DisallowHeapAllocation no_gc;
1682 WriteToFlat(*cons, flat->GetChars(), 0, length);
1683 result = flat;
1684 } else {
1685 Handle<SeqTwoByteString> flat = isolate->factory()->NewRawTwoByteString(
1686 length, tenure).ToHandleChecked();
1687 DisallowHeapAllocation no_gc;
1688 WriteToFlat(*cons, flat->GetChars(), 0, length);
1689 result = flat;
Steve Blocka7e24c12009-10-30 11:49:00 +00001690 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001691 cons->set_first(*result);
1692 cons->set_second(isolate->heap()->empty_string());
1693 DCHECK(result->IsFlat());
1694 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00001695}
1696
1697
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001698
Steve Blocka7e24c12009-10-30 11:49:00 +00001699bool String::MakeExternal(v8::String::ExternalStringResource* resource) {
Steve Block8defd9f2010-07-08 12:39:36 +01001700 // Externalizing twice leaks the external resource, so it's
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001701 // prohibited by the API.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001702 DCHECK(!this->IsExternalString());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001703 DCHECK(!resource->IsCompressible());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001704#ifdef ENABLE_SLOW_DCHECKS
Steve Block3ce2e202009-11-05 08:53:23 +00001705 if (FLAG_enable_slow_asserts) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001706 // Assert that the resource and the string are equivalent.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001707 DCHECK(static_cast<size_t>(this->length()) == resource->length());
Kristian Monsen25f61362010-05-21 11:50:48 +01001708 ScopedVector<uc16> smart_chars(this->length());
1709 String::WriteToFlat(this, smart_chars.start(), 0, this->length());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001710 DCHECK(memcmp(smart_chars.start(),
Steve Blocka7e24c12009-10-30 11:49:00 +00001711 resource->data(),
Kristian Monsen25f61362010-05-21 11:50:48 +01001712 resource->length() * sizeof(smart_chars[0])) == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001713 }
1714#endif // DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001715 int size = this->Size(); // Byte size of the original string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001716 // Abort if size does not allow in-place conversion.
1717 if (size < ExternalString::kShortSize) return false;
1718 Heap* heap = GetHeap();
1719 bool is_one_byte = this->IsOneByteRepresentation();
1720 bool is_internalized = this->IsInternalizedString();
Steve Blocka7e24c12009-10-30 11:49:00 +00001721
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001722 // Morph the string to an external string by replacing the map and
1723 // reinitializing the fields. This won't work if the space the existing
1724 // string occupies is too small for a regular external string.
1725 // Instead, we resort to a short external string instead, omitting
1726 // the field caching the address of the backing store. When we encounter
1727 // short external strings in generated code, we need to bailout to runtime.
1728 Map* new_map;
1729 if (size < ExternalString::kSize) {
1730 new_map = is_internalized
1731 ? (is_one_byte
1732 ? heap->short_external_internalized_string_with_one_byte_data_map()
1733 : heap->short_external_internalized_string_map())
1734 : (is_one_byte ? heap->short_external_string_with_one_byte_data_map()
1735 : heap->short_external_string_map());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001736 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001737 new_map = is_internalized
1738 ? (is_one_byte
1739 ? heap->external_internalized_string_with_one_byte_data_map()
1740 : heap->external_internalized_string_map())
1741 : (is_one_byte ? heap->external_string_with_one_byte_data_map()
1742 : heap->external_string_map());
Ben Murdoch85b71792012-04-11 18:30:58 +01001743 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001744
1745 // Byte size of the external String object.
1746 int new_size = this->SizeFromMap(new_map);
Ben Murdochda12d292016-06-02 14:46:10 +01001747 heap->CreateFillerObjectAt(this->address() + new_size, size - new_size,
1748 ClearRecordedSlots::kNo);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001749
1750 // We are storing the new map using release store after creating a filler for
1751 // the left-over space to avoid races with the sweeper thread.
1752 this->synchronized_set_map(new_map);
1753
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001754 ExternalTwoByteString* self = ExternalTwoByteString::cast(this);
1755 self->set_resource(resource);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001756 if (is_internalized) self->Hash(); // Force regeneration of the hash value.
Steve Blocka7e24c12009-10-30 11:49:00 +00001757
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001758 heap->AdjustLiveBytes(this, new_size - size, Heap::CONCURRENT_TO_SWEEPER);
Steve Blocka7e24c12009-10-30 11:49:00 +00001759 return true;
1760}
1761
1762
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001763bool String::MakeExternal(v8::String::ExternalOneByteStringResource* resource) {
1764 // Externalizing twice leaks the external resource, so it's
1765 // prohibited by the API.
1766 DCHECK(!this->IsExternalString());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001767 DCHECK(!resource->IsCompressible());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001768#ifdef ENABLE_SLOW_DCHECKS
Steve Block3ce2e202009-11-05 08:53:23 +00001769 if (FLAG_enable_slow_asserts) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001770 // Assert that the resource and the string are equivalent.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001771 DCHECK(static_cast<size_t>(this->length()) == resource->length());
1772 if (this->IsTwoByteRepresentation()) {
1773 ScopedVector<uint16_t> smart_chars(this->length());
1774 String::WriteToFlat(this, smart_chars.start(), 0, this->length());
1775 DCHECK(String::IsOneByte(smart_chars.start(), this->length()));
1776 }
Kristian Monsen25f61362010-05-21 11:50:48 +01001777 ScopedVector<char> smart_chars(this->length());
1778 String::WriteToFlat(this, smart_chars.start(), 0, this->length());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001779 DCHECK(memcmp(smart_chars.start(),
Steve Blocka7e24c12009-10-30 11:49:00 +00001780 resource->data(),
Kristian Monsen25f61362010-05-21 11:50:48 +01001781 resource->length() * sizeof(smart_chars[0])) == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001782 }
1783#endif // DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001784 int size = this->Size(); // Byte size of the original string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001785 // Abort if size does not allow in-place conversion.
1786 if (size < ExternalString::kShortSize) return false;
1787 Heap* heap = GetHeap();
1788 bool is_internalized = this->IsInternalizedString();
Steve Blocka7e24c12009-10-30 11:49:00 +00001789
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001790 // Morph the string to an external string by replacing the map and
1791 // reinitializing the fields. This won't work if the space the existing
1792 // string occupies is too small for a regular external string.
1793 // Instead, we resort to a short external string instead, omitting
1794 // the field caching the address of the backing store. When we encounter
1795 // short external strings in generated code, we need to bailout to runtime.
1796 Map* new_map;
1797 if (size < ExternalString::kSize) {
1798 new_map = is_internalized
1799 ? heap->short_external_one_byte_internalized_string_map()
1800 : heap->short_external_one_byte_string_map();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001801 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001802 new_map = is_internalized
1803 ? heap->external_one_byte_internalized_string_map()
1804 : heap->external_one_byte_string_map();
Ben Murdoch85b71792012-04-11 18:30:58 +01001805 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001806
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001807 // Byte size of the external String object.
1808 int new_size = this->SizeFromMap(new_map);
Ben Murdochda12d292016-06-02 14:46:10 +01001809 heap->CreateFillerObjectAt(this->address() + new_size, size - new_size,
1810 ClearRecordedSlots::kNo);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001811
1812 // We are storing the new map using release store after creating a filler for
1813 // the left-over space to avoid races with the sweeper thread.
1814 this->synchronized_set_map(new_map);
1815
1816 ExternalOneByteString* self = ExternalOneByteString::cast(this);
1817 self->set_resource(resource);
1818 if (is_internalized) self->Hash(); // Force regeneration of the hash value.
1819
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001820 heap->AdjustLiveBytes(this, new_size - size, Heap::CONCURRENT_TO_SWEEPER);
Steve Blocka7e24c12009-10-30 11:49:00 +00001821 return true;
1822}
1823
1824
1825void String::StringShortPrint(StringStream* accumulator) {
1826 int len = length();
Steve Blockd0582a62009-12-15 09:54:21 +00001827 if (len > kMaxShortPrintLength) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001828 accumulator->Add("<Very long string[%u]>", len);
1829 return;
1830 }
1831
1832 if (!LooksValid()) {
1833 accumulator->Add("<Invalid String>");
1834 return;
1835 }
1836
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001837 StringCharacterStream stream(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001838
1839 bool truncated = false;
1840 if (len > kMaxShortPrintLength) {
1841 len = kMaxShortPrintLength;
1842 truncated = true;
1843 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001844 bool one_byte = true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001845 for (int i = 0; i < len; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001846 uint16_t c = stream.GetNext();
Steve Blocka7e24c12009-10-30 11:49:00 +00001847
1848 if (c < 32 || c >= 127) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001849 one_byte = false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001850 }
1851 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001852 stream.Reset(this);
1853 if (one_byte) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001854 accumulator->Add("<String[%u]: ", length());
1855 for (int i = 0; i < len; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001856 accumulator->Put(static_cast<char>(stream.GetNext()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001857 }
1858 accumulator->Put('>');
1859 } else {
1860 // Backslash indicates that the string contains control
1861 // characters and that backslashes are therefore escaped.
1862 accumulator->Add("<String[%u]\\: ", length());
1863 for (int i = 0; i < len; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001864 uint16_t c = stream.GetNext();
Steve Blocka7e24c12009-10-30 11:49:00 +00001865 if (c == '\n') {
1866 accumulator->Add("\\n");
1867 } else if (c == '\r') {
1868 accumulator->Add("\\r");
1869 } else if (c == '\\') {
1870 accumulator->Add("\\\\");
1871 } else if (c < 32 || c > 126) {
1872 accumulator->Add("\\x%02x", c);
1873 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001874 accumulator->Put(static_cast<char>(c));
Steve Blocka7e24c12009-10-30 11:49:00 +00001875 }
1876 }
1877 if (truncated) {
1878 accumulator->Put('.');
1879 accumulator->Put('.');
1880 accumulator->Put('.');
1881 }
1882 accumulator->Put('>');
1883 }
1884 return;
1885}
1886
1887
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001888void String::PrintUC16(std::ostream& os, int start, int end) { // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001889 if (end < 0) end = length();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001890 StringCharacterStream stream(this, start);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001891 for (int i = start; i < end && stream.HasMore(); i++) {
1892 os << AsUC16(stream.GetNext());
1893 }
1894}
1895
1896
Steve Blocka7e24c12009-10-30 11:49:00 +00001897void JSObject::JSObjectShortPrint(StringStream* accumulator) {
1898 switch (map()->instance_type()) {
1899 case JS_ARRAY_TYPE: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001900 double length = JSArray::cast(this)->length()->IsUndefined()
1901 ? 0
1902 : JSArray::cast(this)->length()->Number();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001903 accumulator->Add("<JS Array[%u]>", static_cast<uint32_t>(length));
Steve Blocka7e24c12009-10-30 11:49:00 +00001904 break;
1905 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001906 case JS_BOUND_FUNCTION_TYPE: {
1907 JSBoundFunction* bound_function = JSBoundFunction::cast(this);
1908 Object* name = bound_function->name();
1909 accumulator->Add("<JS BoundFunction");
1910 if (name->IsString()) {
1911 String* str = String::cast(name);
1912 if (str->length() > 0) {
1913 accumulator->Add(" ");
1914 accumulator->Put(str);
1915 }
1916 }
1917 accumulator->Add(
1918 " (BoundTargetFunction %p)>",
1919 reinterpret_cast<void*>(bound_function->bound_target_function()));
1920 break;
1921 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001922 case JS_WEAK_MAP_TYPE: {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001923 accumulator->Add("<JS WeakMap>");
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001924 break;
1925 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001926 case JS_WEAK_SET_TYPE: {
1927 accumulator->Add("<JS WeakSet>");
1928 break;
1929 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001930 case JS_REGEXP_TYPE: {
1931 accumulator->Add("<JS RegExp>");
1932 break;
1933 }
1934 case JS_FUNCTION_TYPE: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001935 JSFunction* function = JSFunction::cast(this);
1936 Object* fun_name = function->shared()->DebugName();
Steve Blocka7e24c12009-10-30 11:49:00 +00001937 bool printed = false;
1938 if (fun_name->IsString()) {
1939 String* str = String::cast(fun_name);
1940 if (str->length() > 0) {
1941 accumulator->Add("<JS Function ");
1942 accumulator->Put(str);
Steve Blocka7e24c12009-10-30 11:49:00 +00001943 printed = true;
1944 }
1945 }
1946 if (!printed) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001947 accumulator->Add("<JS Function");
Steve Blocka7e24c12009-10-30 11:49:00 +00001948 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001949 accumulator->Add(" (SharedFunctionInfo %p)",
1950 reinterpret_cast<void*>(function->shared()));
1951 accumulator->Put('>');
1952 break;
1953 }
1954 case JS_GENERATOR_OBJECT_TYPE: {
1955 accumulator->Add("<JS Generator>");
1956 break;
1957 }
1958 case JS_MODULE_TYPE: {
1959 accumulator->Add("<JS Module>");
Steve Blocka7e24c12009-10-30 11:49:00 +00001960 break;
1961 }
1962 // All other JSObjects are rather similar to each other (JSObject,
Ben Murdochda12d292016-06-02 14:46:10 +01001963 // JSGlobalProxy, JSGlobalObject, JSUndetectable, JSValue).
Steve Blocka7e24c12009-10-30 11:49:00 +00001964 default: {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001965 Map* map_of_this = map();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001966 Heap* heap = GetHeap();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001967 Object* constructor = map_of_this->GetConstructor();
Steve Blocka7e24c12009-10-30 11:49:00 +00001968 bool printed = false;
1969 if (constructor->IsHeapObject() &&
Steve Block44f0eee2011-05-26 01:26:41 +01001970 !heap->Contains(HeapObject::cast(constructor))) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001971 accumulator->Add("!!!INVALID CONSTRUCTOR!!!");
1972 } else {
1973 bool global_object = IsJSGlobalProxy();
1974 if (constructor->IsJSFunction()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001975 if (!heap->Contains(JSFunction::cast(constructor)->shared())) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001976 accumulator->Add("!!!INVALID SHARED ON CONSTRUCTOR!!!");
1977 } else {
1978 Object* constructor_name =
1979 JSFunction::cast(constructor)->shared()->name();
1980 if (constructor_name->IsString()) {
1981 String* str = String::cast(constructor_name);
1982 if (str->length() > 0) {
1983 bool vowel = AnWord(str);
1984 accumulator->Add("<%sa%s ",
1985 global_object ? "Global Object: " : "",
1986 vowel ? "n" : "");
1987 accumulator->Put(str);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001988 accumulator->Add(" with %smap %p",
1989 map_of_this->is_deprecated() ? "deprecated " : "",
1990 map_of_this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001991 printed = true;
1992 }
1993 }
1994 }
1995 }
1996 if (!printed) {
1997 accumulator->Add("<JS %sObject", global_object ? "Global " : "");
1998 }
1999 }
2000 if (IsJSValue()) {
2001 accumulator->Add(" value = ");
2002 JSValue::cast(this)->value()->ShortPrint(accumulator);
2003 }
2004 accumulator->Put('>');
2005 break;
2006 }
2007 }
2008}
2009
2010
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002011void JSObject::PrintElementsTransition(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002012 FILE* file, Handle<JSObject> object,
2013 ElementsKind from_kind, Handle<FixedArrayBase> from_elements,
2014 ElementsKind to_kind, Handle<FixedArrayBase> to_elements) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002015 if (from_kind != to_kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002016 OFStream os(file);
2017 os << "elements transition [" << ElementsKindToString(from_kind) << " -> "
2018 << ElementsKindToString(to_kind) << "] in ";
2019 JavaScriptFrame::PrintTop(object->GetIsolate(), file, false, true);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002020 PrintF(file, " for ");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002021 object->ShortPrint(file);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002022 PrintF(file, " from ");
2023 from_elements->ShortPrint(file);
2024 PrintF(file, " to ");
2025 to_elements->ShortPrint(file);
2026 PrintF(file, "\n");
2027 }
2028}
2029
2030
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002031// static
2032MaybeHandle<JSFunction> Map::GetConstructorFunction(
2033 Handle<Map> map, Handle<Context> native_context) {
2034 if (map->IsPrimitiveMap()) {
2035 int const constructor_function_index = map->GetConstructorFunctionIndex();
2036 if (constructor_function_index != kNoConstructorFunctionIndex) {
2037 return handle(
2038 JSFunction::cast(native_context->get(constructor_function_index)));
2039 }
2040 }
2041 return MaybeHandle<JSFunction>();
2042}
2043
2044
2045void Map::PrintReconfiguration(FILE* file, int modify_index, PropertyKind kind,
2046 PropertyAttributes attributes) {
2047 OFStream os(file);
2048 os << "[reconfiguring]";
2049 Name* name = instance_descriptors()->GetKey(modify_index);
2050 if (name->IsString()) {
2051 String::cast(name)->PrintOn(file);
2052 } else {
2053 os << "{symbol " << static_cast<void*>(name) << "}";
2054 }
2055 os << ": " << (kind == kData ? "kData" : "ACCESSORS") << ", attrs: ";
2056 os << attributes << " [";
2057 JavaScriptFrame::PrintTop(GetIsolate(), file, false, true);
2058 os << "]\n";
2059}
2060
Ben Murdoch097c5b22016-05-18 11:27:45 +01002061void Map::PrintGeneralization(
2062 FILE* file, const char* reason, int modify_index, int split,
2063 int descriptors, bool constant_to_field, Representation old_representation,
2064 Representation new_representation, MaybeHandle<FieldType> old_field_type,
2065 MaybeHandle<Object> old_value, MaybeHandle<FieldType> new_field_type,
2066 MaybeHandle<Object> new_value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002067 OFStream os(file);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002068 os << "[generalizing]";
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002069 Name* name = instance_descriptors()->GetKey(modify_index);
2070 if (name->IsString()) {
2071 String::cast(name)->PrintOn(file);
2072 } else {
2073 os << "{symbol " << static_cast<void*>(name) << "}";
2074 }
2075 os << ":";
2076 if (constant_to_field) {
2077 os << "c";
2078 } else {
2079 os << old_representation.Mnemonic() << "{";
Ben Murdoch097c5b22016-05-18 11:27:45 +01002080 if (old_field_type.is_null()) {
2081 os << Brief(*(old_value.ToHandleChecked()));
2082 } else {
2083 old_field_type.ToHandleChecked()->PrintTo(os);
2084 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002085 os << "}";
2086 }
2087 os << "->" << new_representation.Mnemonic() << "{";
Ben Murdoch097c5b22016-05-18 11:27:45 +01002088 if (new_field_type.is_null()) {
2089 os << Brief(*(new_value.ToHandleChecked()));
2090 } else {
2091 new_field_type.ToHandleChecked()->PrintTo(os);
2092 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002093 os << "} (";
2094 if (strlen(reason) > 0) {
2095 os << reason;
2096 } else {
2097 os << "+" << (descriptors - split) << " maps";
2098 }
2099 os << ") [";
2100 JavaScriptFrame::PrintTop(GetIsolate(), file, false, true);
2101 os << "]\n";
2102}
2103
2104
2105void JSObject::PrintInstanceMigration(FILE* file,
2106 Map* original_map,
2107 Map* new_map) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002108 PrintF(file, "[migrating]");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002109 DescriptorArray* o = original_map->instance_descriptors();
2110 DescriptorArray* n = new_map->instance_descriptors();
2111 for (int i = 0; i < original_map->NumberOfOwnDescriptors(); i++) {
2112 Representation o_r = o->GetDetails(i).representation();
2113 Representation n_r = n->GetDetails(i).representation();
2114 if (!o_r.Equals(n_r)) {
2115 String::cast(o->GetKey(i))->PrintOn(file);
2116 PrintF(file, ":%s->%s ", o_r.Mnemonic(), n_r.Mnemonic());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002117 } else if (o->GetDetails(i).type() == DATA_CONSTANT &&
2118 n->GetDetails(i).type() == DATA) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002119 Name* name = o->GetKey(i);
2120 if (name->IsString()) {
2121 String::cast(name)->PrintOn(file);
2122 } else {
2123 PrintF(file, "{symbol %p}", static_cast<void*>(name));
2124 }
2125 PrintF(file, " ");
2126 }
2127 }
2128 PrintF(file, "\n");
2129}
2130
2131
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002132void HeapObject::HeapObjectShortPrint(std::ostream& os) { // NOLINT
Steve Block44f0eee2011-05-26 01:26:41 +01002133 Heap* heap = GetHeap();
2134 if (!heap->Contains(this)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002135 os << "!!!INVALID POINTER!!!";
Steve Blocka7e24c12009-10-30 11:49:00 +00002136 return;
2137 }
Steve Block44f0eee2011-05-26 01:26:41 +01002138 if (!heap->Contains(map())) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002139 os << "!!!INVALID MAP!!!";
Steve Blocka7e24c12009-10-30 11:49:00 +00002140 return;
2141 }
2142
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002143 os << this << " ";
Steve Blocka7e24c12009-10-30 11:49:00 +00002144
2145 if (IsString()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002146 HeapStringAllocator allocator;
2147 StringStream accumulator(&allocator);
2148 String::cast(this)->StringShortPrint(&accumulator);
2149 os << accumulator.ToCString().get();
Steve Blocka7e24c12009-10-30 11:49:00 +00002150 return;
2151 }
2152 if (IsJSObject()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002153 HeapStringAllocator allocator;
2154 StringStream accumulator(&allocator);
2155 JSObject::cast(this)->JSObjectShortPrint(&accumulator);
2156 os << accumulator.ToCString().get();
Steve Blocka7e24c12009-10-30 11:49:00 +00002157 return;
2158 }
2159 switch (map()->instance_type()) {
2160 case MAP_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002161 os << "<Map(" << ElementsKindToString(Map::cast(this)->elements_kind())
2162 << ")>";
Steve Blocka7e24c12009-10-30 11:49:00 +00002163 break;
2164 case FIXED_ARRAY_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002165 os << "<FixedArray[" << FixedArray::cast(this)->length() << "]>";
Steve Blocka7e24c12009-10-30 11:49:00 +00002166 break;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002167 case FIXED_DOUBLE_ARRAY_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002168 os << "<FixedDoubleArray[" << FixedDoubleArray::cast(this)->length()
2169 << "]>";
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002170 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00002171 case BYTE_ARRAY_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002172 os << "<ByteArray[" << ByteArray::cast(this)->length() << "]>";
Steve Blocka7e24c12009-10-30 11:49:00 +00002173 break;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002174 case BYTECODE_ARRAY_TYPE:
2175 os << "<BytecodeArray[" << BytecodeArray::cast(this)->length() << "]>";
2176 break;
2177 case TRANSITION_ARRAY_TYPE:
2178 os << "<TransitionArray[" << TransitionArray::cast(this)->length()
2179 << "]>";
2180 break;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002181 case FREE_SPACE_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002182 os << "<FreeSpace[" << FreeSpace::cast(this)->size() << "]>";
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002183 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002184#define TYPED_ARRAY_SHORT_PRINT(Type, type, TYPE, ctype, size) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002185 case FIXED_##TYPE##_ARRAY_TYPE: \
2186 os << "<Fixed" #Type "Array[" << Fixed##Type##Array::cast(this)->length() \
2187 << "]>"; \
2188 break;
2189
2190 TYPED_ARRAYS(TYPED_ARRAY_SHORT_PRINT)
2191#undef TYPED_ARRAY_SHORT_PRINT
2192
2193 case SHARED_FUNCTION_INFO_TYPE: {
2194 SharedFunctionInfo* shared = SharedFunctionInfo::cast(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002195 base::SmartArrayPointer<char> debug_name =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002196 shared->DebugName()->ToCString();
2197 if (debug_name[0] != 0) {
2198 os << "<SharedFunctionInfo " << debug_name.get() << ">";
2199 } else {
2200 os << "<SharedFunctionInfo>";
2201 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002202 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002203 }
Steve Block1e0659c2011-05-24 12:43:12 +01002204 case JS_MESSAGE_OBJECT_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002205 os << "<JSMessageObject>";
Steve Block1e0659c2011-05-24 12:43:12 +01002206 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00002207#define MAKE_STRUCT_CASE(NAME, Name, name) \
2208 case NAME##_TYPE: \
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002209 os << "<" #Name ">"; \
Steve Blocka7e24c12009-10-30 11:49:00 +00002210 break;
2211 STRUCT_LIST(MAKE_STRUCT_CASE)
2212#undef MAKE_STRUCT_CASE
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002213 case CODE_TYPE: {
2214 Code* code = Code::cast(this);
2215 os << "<Code: " << Code::Kind2String(code->kind()) << ">";
Steve Blocka7e24c12009-10-30 11:49:00 +00002216 break;
2217 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002218 case ODDBALL_TYPE: {
2219 if (IsUndefined()) {
2220 os << "<undefined>";
2221 } else if (IsTheHole()) {
2222 os << "<the hole>";
2223 } else if (IsNull()) {
2224 os << "<null>";
2225 } else if (IsTrue()) {
2226 os << "<true>";
2227 } else if (IsFalse()) {
2228 os << "<false>";
2229 } else {
2230 os << "<Odd Oddball>";
2231 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002232 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002233 }
2234 case SYMBOL_TYPE: {
2235 Symbol* symbol = Symbol::cast(this);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002236 symbol->SymbolShortPrint(os);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002237 break;
2238 }
2239 case HEAP_NUMBER_TYPE: {
2240 os << "<Number: ";
2241 HeapNumber::cast(this)->HeapNumberPrint(os);
2242 os << ">";
2243 break;
2244 }
2245 case MUTABLE_HEAP_NUMBER_TYPE: {
2246 os << "<MutableNumber: ";
2247 HeapNumber::cast(this)->HeapNumberPrint(os);
2248 os << '>';
2249 break;
2250 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002251 case SIMD128_VALUE_TYPE: {
2252#define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \
2253 if (Is##Type()) { \
2254 os << "<" #Type ">"; \
2255 break; \
2256 }
2257 SIMD128_TYPES(SIMD128_TYPE)
2258#undef SIMD128_TYPE
2259 UNREACHABLE();
2260 break;
2261 }
Ben Murdoch589d6972011-11-30 16:04:58 +00002262 case JS_PROXY_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002263 os << "<JSProxy>";
Ben Murdoch589d6972011-11-30 16:04:58 +00002264 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002265 case FOREIGN_TYPE:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002266 os << "<Foreign>";
Steve Blocka7e24c12009-10-30 11:49:00 +00002267 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002268 case CELL_TYPE: {
2269 os << "Cell for ";
2270 HeapStringAllocator allocator;
2271 StringStream accumulator(&allocator);
2272 Cell::cast(this)->value()->ShortPrint(&accumulator);
2273 os << accumulator.ToCString().get();
Steve Blocka7e24c12009-10-30 11:49:00 +00002274 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002275 }
2276 case PROPERTY_CELL_TYPE: {
2277 os << "PropertyCell for ";
2278 HeapStringAllocator allocator;
2279 StringStream accumulator(&allocator);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002280 PropertyCell* cell = PropertyCell::cast(this);
2281 cell->value()->ShortPrint(&accumulator);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002282 os << accumulator.ToCString().get();
2283 break;
2284 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002285 case WEAK_CELL_TYPE: {
2286 os << "WeakCell for ";
2287 HeapStringAllocator allocator;
2288 StringStream accumulator(&allocator);
2289 WeakCell::cast(this)->value()->ShortPrint(&accumulator);
2290 os << accumulator.ToCString().get();
2291 break;
2292 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002293 default:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002294 os << "<Other heap object (" << map()->instance_type() << ")>";
Steve Blocka7e24c12009-10-30 11:49:00 +00002295 break;
2296 }
2297}
2298
2299
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002300void HeapObject::Iterate(ObjectVisitor* v) { IterateFast<ObjectVisitor>(v); }
2301
2302
2303void HeapObject::IterateBody(ObjectVisitor* v) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002304 Map* m = map();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002305 IterateBodyFast<ObjectVisitor>(m->instance_type(), SizeFromMap(m), v);
Steve Blocka7e24c12009-10-30 11:49:00 +00002306}
2307
2308
2309void HeapObject::IterateBody(InstanceType type, int object_size,
2310 ObjectVisitor* v) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002311 IterateBodyFast<ObjectVisitor>(type, object_size, v);
2312}
2313
2314
2315struct CallIsValidSlot {
2316 template <typename BodyDescriptor>
2317 static bool apply(HeapObject* obj, int offset, int) {
2318 return BodyDescriptor::IsValidSlot(obj, offset);
Steve Blocka7e24c12009-10-30 11:49:00 +00002319 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002320};
Steve Blocka7e24c12009-10-30 11:49:00 +00002321
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002322
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002323bool HeapObject::IsValidSlot(int offset) {
2324 DCHECK_NE(0, offset);
2325 return BodyDescriptorApply<CallIsValidSlot, bool>(map()->instance_type(),
2326 this, offset, 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00002327}
2328
2329
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002330bool HeapNumber::HeapNumberBooleanValue() {
2331 return DoubleToBoolean(value());
Steve Blocka7e24c12009-10-30 11:49:00 +00002332}
2333
2334
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002335void HeapNumber::HeapNumberPrint(std::ostream& os) { // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002336 os << value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002337}
2338
2339
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002340#define FIELD_ADDR_CONST(p, offset) \
2341 (reinterpret_cast<const byte*>(p) + offset - kHeapObjectTag)
2342
2343#define READ_INT32_FIELD(p, offset) \
2344 (*reinterpret_cast<const int32_t*>(FIELD_ADDR_CONST(p, offset)))
2345
2346#define READ_INT64_FIELD(p, offset) \
2347 (*reinterpret_cast<const int64_t*>(FIELD_ADDR_CONST(p, offset)))
2348
2349#define READ_BYTE_FIELD(p, offset) \
2350 (*reinterpret_cast<const byte*>(FIELD_ADDR_CONST(p, offset)))
2351
2352
2353// static
2354Handle<String> Simd128Value::ToString(Handle<Simd128Value> input) {
2355#define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \
2356 if (input->Is##Type()) return Type::ToString(Handle<Type>::cast(input));
2357 SIMD128_TYPES(SIMD128_TYPE)
2358#undef SIMD128_TYPE
2359 UNREACHABLE();
2360 return Handle<String>::null();
2361}
2362
2363
2364// static
2365Handle<String> Float32x4::ToString(Handle<Float32x4> input) {
2366 Isolate* const isolate = input->GetIsolate();
2367 char arr[100];
2368 Vector<char> buffer(arr, arraysize(arr));
2369 std::ostringstream os;
2370 os << "SIMD.Float32x4("
2371 << std::string(DoubleToCString(input->get_lane(0), buffer)) << ", "
2372 << std::string(DoubleToCString(input->get_lane(1), buffer)) << ", "
2373 << std::string(DoubleToCString(input->get_lane(2), buffer)) << ", "
2374 << std::string(DoubleToCString(input->get_lane(3), buffer)) << ")";
2375 return isolate->factory()->NewStringFromAsciiChecked(os.str().c_str());
2376}
2377
2378
2379#define SIMD128_BOOL_TO_STRING(Type, lane_count) \
2380 Handle<String> Type::ToString(Handle<Type> input) { \
2381 Isolate* const isolate = input->GetIsolate(); \
2382 std::ostringstream os; \
2383 os << "SIMD." #Type "("; \
2384 os << (input->get_lane(0) ? "true" : "false"); \
2385 for (int i = 1; i < lane_count; i++) { \
2386 os << ", " << (input->get_lane(i) ? "true" : "false"); \
2387 } \
2388 os << ")"; \
2389 return isolate->factory()->NewStringFromAsciiChecked(os.str().c_str()); \
2390 }
2391SIMD128_BOOL_TO_STRING(Bool32x4, 4)
2392SIMD128_BOOL_TO_STRING(Bool16x8, 8)
2393SIMD128_BOOL_TO_STRING(Bool8x16, 16)
2394#undef SIMD128_BOOL_TO_STRING
2395
2396
2397#define SIMD128_INT_TO_STRING(Type, lane_count) \
2398 Handle<String> Type::ToString(Handle<Type> input) { \
2399 Isolate* const isolate = input->GetIsolate(); \
2400 char arr[100]; \
2401 Vector<char> buffer(arr, arraysize(arr)); \
2402 std::ostringstream os; \
2403 os << "SIMD." #Type "("; \
2404 os << IntToCString(input->get_lane(0), buffer); \
2405 for (int i = 1; i < lane_count; i++) { \
2406 os << ", " << IntToCString(input->get_lane(i), buffer); \
2407 } \
2408 os << ")"; \
2409 return isolate->factory()->NewStringFromAsciiChecked(os.str().c_str()); \
2410 }
2411SIMD128_INT_TO_STRING(Int32x4, 4)
2412SIMD128_INT_TO_STRING(Uint32x4, 4)
2413SIMD128_INT_TO_STRING(Int16x8, 8)
2414SIMD128_INT_TO_STRING(Uint16x8, 8)
2415SIMD128_INT_TO_STRING(Int8x16, 16)
2416SIMD128_INT_TO_STRING(Uint8x16, 16)
2417#undef SIMD128_INT_TO_STRING
2418
2419
2420bool Simd128Value::BitwiseEquals(const Simd128Value* other) const {
2421 return READ_INT64_FIELD(this, kValueOffset) ==
2422 READ_INT64_FIELD(other, kValueOffset) &&
2423 READ_INT64_FIELD(this, kValueOffset + kInt64Size) ==
2424 READ_INT64_FIELD(other, kValueOffset + kInt64Size);
2425}
2426
2427
2428uint32_t Simd128Value::Hash() const {
2429 uint32_t seed = v8::internal::kZeroHashSeed;
2430 uint32_t hash;
2431 hash = ComputeIntegerHash(READ_INT32_FIELD(this, kValueOffset), seed);
2432 hash = ComputeIntegerHash(
2433 READ_INT32_FIELD(this, kValueOffset + 1 * kInt32Size), hash * 31);
2434 hash = ComputeIntegerHash(
2435 READ_INT32_FIELD(this, kValueOffset + 2 * kInt32Size), hash * 31);
2436 hash = ComputeIntegerHash(
2437 READ_INT32_FIELD(this, kValueOffset + 3 * kInt32Size), hash * 31);
2438 return hash;
2439}
2440
2441
2442void Simd128Value::CopyBits(void* destination) const {
2443 memcpy(destination, &READ_BYTE_FIELD(this, kValueOffset), kSimd128Size);
2444}
2445
2446
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002447String* JSReceiver::class_name() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002448 if (IsFunction()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002449 return GetHeap()->Function_string();
Steve Blocka7e24c12009-10-30 11:49:00 +00002450 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002451 Object* maybe_constructor = map()->GetConstructor();
2452 if (maybe_constructor->IsJSFunction()) {
2453 JSFunction* constructor = JSFunction::cast(maybe_constructor);
Steve Blocka7e24c12009-10-30 11:49:00 +00002454 return String::cast(constructor->shared()->instance_class_name());
2455 }
2456 // If the constructor is not present, return "Object".
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002457 return GetHeap()->Object_string();
Steve Blocka7e24c12009-10-30 11:49:00 +00002458}
2459
2460
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002461MaybeHandle<String> JSReceiver::BuiltinStringTag(Handle<JSReceiver> object) {
2462 Maybe<bool> is_array = Object::IsArray(object);
2463 MAYBE_RETURN(is_array, MaybeHandle<String>());
2464 Isolate* const isolate = object->GetIsolate();
2465 if (is_array.FromJust()) {
2466 return isolate->factory()->Array_string();
Steve Blocka7e24c12009-10-30 11:49:00 +00002467 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002468 // TODO(adamk): According to ES2015, we should return "Function" when
2469 // object has a [[Call]] internal method (corresponds to IsCallable).
2470 // But this is well cemented in layout tests and might cause webbreakage.
2471 // if (object->IsCallable()) {
2472 // return isolate->factory()->Function_string();
2473 // }
2474 // TODO(adamk): class_name() is expensive, replace with instance type
2475 // checks where possible.
2476 return handle(object->class_name(), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002477}
2478
2479
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002480// static
2481Handle<String> JSReceiver::GetConstructorName(Handle<JSReceiver> receiver) {
2482 Isolate* isolate = receiver->GetIsolate();
2483
2484 // If the object was instantiated simply with base == new.target, the
2485 // constructor on the map provides the most accurate name.
2486 // Don't provide the info for prototypes, since their constructors are
2487 // reclaimed and replaced by Object in OptimizeAsPrototype.
2488 if (!receiver->IsJSProxy() && receiver->map()->new_target_is_base() &&
2489 !receiver->map()->is_prototype_map()) {
2490 Object* maybe_constructor = receiver->map()->GetConstructor();
2491 if (maybe_constructor->IsJSFunction()) {
2492 JSFunction* constructor = JSFunction::cast(maybe_constructor);
2493 String* name = String::cast(constructor->shared()->name());
2494 if (name->length() == 0) name = constructor->shared()->inferred_name();
2495 if (name->length() != 0 &&
2496 !name->Equals(isolate->heap()->Object_string())) {
2497 return handle(name, isolate);
2498 }
2499 }
2500 }
2501
Ben Murdochda12d292016-06-02 14:46:10 +01002502 Handle<Object> maybe_tag = JSReceiver::GetDataProperty(
2503 receiver, isolate->factory()->to_string_tag_symbol());
2504 if (maybe_tag->IsString()) return Handle<String>::cast(maybe_tag);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002505
2506 PrototypeIterator iter(isolate, receiver);
2507 if (iter.IsAtEnd()) return handle(receiver->class_name());
2508 Handle<JSReceiver> start = PrototypeIterator::GetCurrent<JSReceiver>(iter);
2509 LookupIterator it(receiver, isolate->factory()->constructor_string(), start,
2510 LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
2511 Handle<Object> maybe_constructor = JSReceiver::GetDataProperty(&it);
2512 Handle<String> result = isolate->factory()->Object_string();
2513 if (maybe_constructor->IsJSFunction()) {
2514 JSFunction* constructor = JSFunction::cast(*maybe_constructor);
2515 String* name = String::cast(constructor->shared()->name());
2516 if (name->length() == 0) name = constructor->shared()->inferred_name();
2517 if (name->length() > 0) result = handle(name, isolate);
2518 }
2519
2520 return result.is_identical_to(isolate->factory()->Object_string())
2521 ? handle(receiver->class_name())
2522 : result;
2523}
2524
2525
2526Context* JSReceiver::GetCreationContext() {
Ben Murdoch097c5b22016-05-18 11:27:45 +01002527 JSReceiver* receiver = this;
2528 while (receiver->IsJSBoundFunction()) {
2529 receiver = JSBoundFunction::cast(receiver)->bound_target_function();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002530 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01002531 Object* constructor = receiver->map()->GetConstructor();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002532 JSFunction* function;
2533 if (constructor->IsJSFunction()) {
2534 function = JSFunction::cast(constructor);
2535 } else {
2536 // Functions have null as a constructor,
2537 // but any JSFunction knows its context immediately.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002538 CHECK(receiver->IsJSFunction());
2539 function = JSFunction::cast(receiver);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002540 }
2541
2542 return function->context()->native_context();
2543}
2544
Ben Murdoch097c5b22016-05-18 11:27:45 +01002545static Handle<Object> WrapType(Handle<FieldType> type) {
2546 if (type->IsClass()) return Map::WeakCellForMap(type->AsClass());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002547 return type;
Steve Blocka7e24c12009-10-30 11:49:00 +00002548}
2549
Ben Murdoch097c5b22016-05-18 11:27:45 +01002550MaybeHandle<Map> Map::CopyWithField(Handle<Map> map, Handle<Name> name,
2551 Handle<FieldType> type,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002552 PropertyAttributes attributes,
2553 Representation representation,
2554 TransitionFlag flag) {
2555 DCHECK(DescriptorArray::kNotFound ==
2556 map->instance_descriptors()->Search(
2557 *name, map->NumberOfOwnDescriptors()));
Ben Murdoch8b112d22011-06-08 16:22:53 +01002558
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002559 // Ensure the descriptor array does not get too big.
2560 if (map->NumberOfOwnDescriptors() >= kMaxNumberOfDescriptors) {
2561 return MaybeHandle<Map>();
Steve Blocka7e24c12009-10-30 11:49:00 +00002562 }
2563
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002564 Isolate* isolate = map->GetIsolate();
2565
Steve Blocka7e24c12009-10-30 11:49:00 +00002566 // Compute the new index for new field.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002567 int index = map->NextFreePropertyIndex();
Steve Blocka7e24c12009-10-30 11:49:00 +00002568
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002569 if (map->instance_type() == JS_CONTEXT_EXTENSION_OBJECT_TYPE) {
2570 representation = Representation::Tagged();
Ben Murdoch097c5b22016-05-18 11:27:45 +01002571 type = FieldType::Any(isolate);
John Reck59135872010-11-02 12:39:01 -07002572 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002573
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002574 Handle<Object> wrapped_type(WrapType(type));
2575
2576 DataDescriptor new_field_desc(name, index, wrapped_type, attributes,
2577 representation);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002578 Handle<Map> new_map = Map::CopyAddDescriptor(map, &new_field_desc, flag);
2579 int unused_property_fields = new_map->unused_property_fields() - 1;
2580 if (unused_property_fields < 0) {
2581 unused_property_fields += JSObject::kFieldsAdded;
John Reck59135872010-11-02 12:39:01 -07002582 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002583 new_map->set_unused_property_fields(unused_property_fields);
2584 return new_map;
Steve Blocka7e24c12009-10-30 11:49:00 +00002585}
2586
2587
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002588MaybeHandle<Map> Map::CopyWithConstant(Handle<Map> map,
2589 Handle<Name> name,
2590 Handle<Object> constant,
2591 PropertyAttributes attributes,
2592 TransitionFlag flag) {
2593 // Ensure the descriptor array does not get too big.
2594 if (map->NumberOfOwnDescriptors() >= kMaxNumberOfDescriptors) {
2595 return MaybeHandle<Map>();
John Reck59135872010-11-02 12:39:01 -07002596 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002597
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002598 // Allocate new instance descriptors with (name, constant) added.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002599 DataConstantDescriptor new_constant_desc(name, constant, attributes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002600 return Map::CopyAddDescriptor(map, &new_constant_desc, flag);
Steve Blocka7e24c12009-10-30 11:49:00 +00002601}
2602
2603
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002604void JSObject::AddSlowProperty(Handle<JSObject> object,
2605 Handle<Name> name,
2606 Handle<Object> value,
2607 PropertyAttributes attributes) {
2608 DCHECK(!object->HasFastProperties());
2609 Isolate* isolate = object->GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002610 if (object->IsJSGlobalObject()) {
2611 Handle<GlobalDictionary> dict(object->global_dictionary());
2612 PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell);
Steve Blocka7e24c12009-10-30 11:49:00 +00002613 int entry = dict->FindEntry(name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002614 // If there's a cell there, just invalidate and set the property.
2615 if (entry != GlobalDictionary::kNotFound) {
2616 PropertyCell::UpdateCell(dict, entry, value, details);
2617 // TODO(ishell): move this to UpdateCell.
2618 // Need to adjust the details.
Steve Blocka7e24c12009-10-30 11:49:00 +00002619 int index = dict->NextEnumerationIndex();
Steve Blocka7e24c12009-10-30 11:49:00 +00002620 dict->SetNextEnumerationIndex(index + 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002621 PropertyCell* cell = PropertyCell::cast(dict->ValueAt(entry));
2622 details = cell->property_details().set_index(index);
2623 cell->set_property_details(details);
2624
2625 } else {
2626 auto cell = isolate->factory()->NewPropertyCell();
2627 cell->set_value(*value);
2628 auto cell_type = value->IsUndefined() ? PropertyCellType::kUndefined
2629 : PropertyCellType::kConstant;
2630 details = details.set_cell_type(cell_type);
2631 value = cell;
2632
2633 Handle<GlobalDictionary> result =
2634 GlobalDictionary::Add(dict, name, value, details);
2635 if (*dict != *result) object->set_properties(*result);
Steve Blocka7e24c12009-10-30 11:49:00 +00002636 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002637 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002638 Handle<NameDictionary> dict(object->property_dictionary());
2639 PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell);
2640 Handle<NameDictionary> result =
2641 NameDictionary::Add(dict, name, value, details);
2642 if (*dict != *result) object->set_properties(*result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002643 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002644}
2645
2646
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002647MaybeHandle<Object> JSObject::EnqueueChangeRecord(Handle<JSObject> object,
2648 const char* type_str,
2649 Handle<Name> name,
2650 Handle<Object> old_value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002651 DCHECK(!object->IsJSGlobalProxy());
2652 DCHECK(!object->IsJSGlobalObject());
2653 Isolate* isolate = object->GetIsolate();
2654 HandleScope scope(isolate);
2655 Handle<String> type = isolate->factory()->InternalizeUtf8String(type_str);
2656 Handle<Object> args[] = { type, object, name, old_value };
2657 int argc = name.is_null() ? 2 : old_value->IsTheHole() ? 3 : 4;
2658
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002659 return Execution::Call(isolate,
2660 Handle<JSFunction>(isolate->observers_notify_change()),
2661 isolate->factory()->undefined_value(), argc, args);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002662}
2663
2664
2665const char* Representation::Mnemonic() const {
2666 switch (kind_) {
2667 case kNone: return "v";
2668 case kTagged: return "t";
2669 case kSmi: return "s";
2670 case kDouble: return "d";
2671 case kInteger32: return "i";
2672 case kHeapObject: return "h";
2673 case kExternal: return "x";
2674 default:
2675 UNREACHABLE();
2676 return NULL;
2677 }
2678}
2679
2680
2681bool Map::InstancesNeedRewriting(Map* target, int target_number_of_fields,
2682 int target_inobject, int target_unused,
2683 int* old_number_of_fields) {
2684 // If fields were added (or removed), rewrite the instance.
2685 *old_number_of_fields = NumberOfFields();
2686 DCHECK(target_number_of_fields >= *old_number_of_fields);
2687 if (target_number_of_fields != *old_number_of_fields) return true;
2688
2689 // If smi descriptors were replaced by double descriptors, rewrite.
2690 DescriptorArray* old_desc = instance_descriptors();
2691 DescriptorArray* new_desc = target->instance_descriptors();
2692 int limit = NumberOfOwnDescriptors();
2693 for (int i = 0; i < limit; i++) {
2694 if (new_desc->GetDetails(i).representation().IsDouble() !=
2695 old_desc->GetDetails(i).representation().IsDouble()) {
2696 return true;
Steve Block44f0eee2011-05-26 01:26:41 +01002697 }
Steve Block8defd9f2010-07-08 12:39:36 +01002698 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002699
2700 // If no fields were added, and no inobject properties were removed, setting
2701 // the map is sufficient.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002702 if (target_inobject == GetInObjectProperties()) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002703 // In-object slack tracking may have reduced the object size of the new map.
2704 // In that case, succeed if all existing fields were inobject, and they still
2705 // fit within the new inobject size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002706 DCHECK(target_inobject < GetInObjectProperties());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002707 if (target_number_of_fields <= target_inobject) {
2708 DCHECK(target_number_of_fields + target_unused == target_inobject);
2709 return false;
2710 }
2711 // Otherwise, properties will need to be moved to the backing store.
2712 return true;
2713}
2714
2715
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002716// static
2717void JSObject::UpdatePrototypeUserRegistration(Handle<Map> old_map,
2718 Handle<Map> new_map,
2719 Isolate* isolate) {
2720 if (!FLAG_track_prototype_users) return;
2721 if (!old_map->is_prototype_map()) return;
2722 DCHECK(new_map->is_prototype_map());
2723 bool was_registered = JSObject::UnregisterPrototypeUser(old_map, isolate);
2724 new_map->set_prototype_info(old_map->prototype_info());
2725 old_map->set_prototype_info(Smi::FromInt(0));
2726 if (FLAG_trace_prototype_users) {
2727 PrintF("Moving prototype_info %p from map %p to map %p.\n",
2728 reinterpret_cast<void*>(new_map->prototype_info()),
2729 reinterpret_cast<void*>(*old_map),
2730 reinterpret_cast<void*>(*new_map));
2731 }
2732 if (was_registered) {
2733 if (new_map->prototype_info()->IsPrototypeInfo()) {
2734 // The new map isn't registered with its prototype yet; reflect this fact
2735 // in the PrototypeInfo it just inherited from the old map.
2736 PrototypeInfo::cast(new_map->prototype_info())
2737 ->set_registry_slot(PrototypeInfo::UNREGISTERED);
2738 }
2739 JSObject::LazyRegisterPrototypeUser(new_map, isolate);
2740 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002741}
2742
Ben Murdoch097c5b22016-05-18 11:27:45 +01002743namespace {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002744// To migrate a fast instance to a fast map:
2745// - First check whether the instance needs to be rewritten. If not, simply
2746// change the map.
2747// - Otherwise, allocate a fixed array large enough to hold all fields, in
2748// addition to unused space.
2749// - Copy all existing properties in, in the following order: backing store
2750// properties, unused fields, inobject properties.
2751// - If all allocation succeeded, commit the state atomically:
2752// * Copy inobject properties from the backing store back into the object.
2753// * Trim the difference in instance size of the object. This also cleanly
2754// frees inobject properties that moved to the backing store.
2755// * If there are properties left in the backing store, trim of the space used
2756// to temporarily store the inobject properties.
2757// * If there are properties left in the backing store, install the backing
2758// store.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002759void MigrateFastToFast(Handle<JSObject> object, Handle<Map> new_map) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002760 Isolate* isolate = object->GetIsolate();
2761 Handle<Map> old_map(object->map());
Ben Murdoch097c5b22016-05-18 11:27:45 +01002762 // In case of a regular transition.
2763 if (new_map->GetBackPointer() == *old_map) {
2764 // If the map does not add named properties, simply set the map.
2765 if (old_map->NumberOfOwnDescriptors() ==
2766 new_map->NumberOfOwnDescriptors()) {
2767 object->synchronized_set_map(*new_map);
2768 return;
2769 }
2770
2771 PropertyDetails details = new_map->GetLastDescriptorDetails();
2772 // Either new_map adds an kDescriptor property, or a kField property for
2773 // which there is still space, and which does not require a mutable double
2774 // box (an out-of-object double).
2775 if (details.location() == kDescriptor ||
2776 (old_map->unused_property_fields() > 0 &&
2777 ((FLAG_unbox_double_fields && object->properties()->length() == 0) ||
2778 !details.representation().IsDouble()))) {
2779 object->synchronized_set_map(*new_map);
2780 return;
2781 }
2782
2783 // If there is still space in the object, we need to allocate a mutable
2784 // double box.
2785 if (old_map->unused_property_fields() > 0) {
2786 FieldIndex index =
2787 FieldIndex::ForDescriptor(*new_map, new_map->LastAdded());
2788 DCHECK(details.representation().IsDouble());
2789 DCHECK(!new_map->IsUnboxedDoubleField(index));
2790 Handle<Object> value = isolate->factory()->NewHeapNumber(0, MUTABLE);
2791 object->RawFastPropertyAtPut(index, *value);
2792 object->synchronized_set_map(*new_map);
2793 return;
2794 }
2795
2796 // This migration is a transition from a map that has run out of property
2797 // space. Extend the backing store.
2798 int grow_by = new_map->unused_property_fields() + 1;
2799 Handle<FixedArray> old_storage = handle(object->properties(), isolate);
2800 Handle<FixedArray> new_storage =
2801 isolate->factory()->CopyFixedArrayAndGrow(old_storage, grow_by);
2802
2803 // Properly initialize newly added property.
2804 Handle<Object> value;
2805 if (details.representation().IsDouble()) {
2806 value = isolate->factory()->NewHeapNumber(0, MUTABLE);
2807 } else {
2808 value = isolate->factory()->uninitialized_value();
2809 }
2810 DCHECK_EQ(DATA, details.type());
2811 int target_index = details.field_index() - new_map->GetInObjectProperties();
2812 DCHECK(target_index >= 0); // Must be a backing store index.
2813 new_storage->set(target_index, *value);
2814
2815 // From here on we cannot fail and we shouldn't GC anymore.
2816 DisallowHeapAllocation no_allocation;
2817
2818 // Set the new property value and do the map transition.
2819 object->set_properties(*new_storage);
2820 object->synchronized_set_map(*new_map);
2821 return;
2822 }
2823
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002824 int old_number_of_fields;
2825 int number_of_fields = new_map->NumberOfFields();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002826 int inobject = new_map->GetInObjectProperties();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002827 int unused = new_map->unused_property_fields();
2828
2829 // Nothing to do if no functions were converted to fields and no smis were
2830 // converted to doubles.
2831 if (!old_map->InstancesNeedRewriting(*new_map, number_of_fields, inobject,
2832 unused, &old_number_of_fields)) {
2833 object->synchronized_set_map(*new_map);
2834 return;
2835 }
2836
2837 int total_size = number_of_fields + unused;
2838 int external = total_size - inobject;
2839
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002840 Handle<FixedArray> array = isolate->factory()->NewFixedArray(total_size);
2841
2842 Handle<DescriptorArray> old_descriptors(old_map->instance_descriptors());
2843 Handle<DescriptorArray> new_descriptors(new_map->instance_descriptors());
2844 int old_nof = old_map->NumberOfOwnDescriptors();
2845 int new_nof = new_map->NumberOfOwnDescriptors();
2846
2847 // This method only supports generalizing instances to at least the same
2848 // number of properties.
2849 DCHECK(old_nof <= new_nof);
2850
2851 for (int i = 0; i < old_nof; i++) {
2852 PropertyDetails details = new_descriptors->GetDetails(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002853 if (details.type() != DATA) continue;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002854 PropertyDetails old_details = old_descriptors->GetDetails(i);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002855 Representation old_representation = old_details.representation();
2856 Representation representation = details.representation();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002857 Handle<Object> value;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002858 if (old_details.type() == ACCESSOR_CONSTANT) {
2859 // In case of kAccessor -> kData property reconfiguration, the property
2860 // must already be prepared for data or certain type.
2861 DCHECK(!details.representation().IsNone());
2862 if (details.representation().IsDouble()) {
2863 value = isolate->factory()->NewHeapNumber(0, MUTABLE);
2864 } else {
2865 value = isolate->factory()->uninitialized_value();
2866 }
2867 } else if (old_details.type() == DATA_CONSTANT) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002868 value = handle(old_descriptors->GetValue(i), isolate);
2869 DCHECK(!old_representation.IsDouble() && !representation.IsDouble());
2870 } else {
2871 FieldIndex index = FieldIndex::ForDescriptor(*old_map, i);
2872 if (object->IsUnboxedDoubleField(index)) {
2873 double old = object->RawFastDoublePropertyAt(index);
2874 value = isolate->factory()->NewHeapNumber(
2875 old, representation.IsDouble() ? MUTABLE : IMMUTABLE);
2876
2877 } else {
2878 value = handle(object->RawFastPropertyAt(index), isolate);
2879 if (!old_representation.IsDouble() && representation.IsDouble()) {
2880 if (old_representation.IsNone()) {
2881 value = handle(Smi::FromInt(0), isolate);
2882 }
2883 value = Object::NewStorageFor(isolate, value, representation);
2884 } else if (old_representation.IsDouble() &&
2885 !representation.IsDouble()) {
2886 value = Object::WrapForRead(isolate, value, old_representation);
2887 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002888 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002889 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002890 DCHECK(!(representation.IsDouble() && value->IsSmi()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002891 int target_index = new_descriptors->GetFieldIndex(i) - inobject;
2892 if (target_index < 0) target_index += total_size;
2893 array->set(target_index, *value);
2894 }
2895
2896 for (int i = old_nof; i < new_nof; i++) {
2897 PropertyDetails details = new_descriptors->GetDetails(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002898 if (details.type() != DATA) continue;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002899 Handle<Object> value;
2900 if (details.representation().IsDouble()) {
2901 value = isolate->factory()->NewHeapNumber(0, MUTABLE);
2902 } else {
2903 value = isolate->factory()->uninitialized_value();
2904 }
2905 int target_index = new_descriptors->GetFieldIndex(i) - inobject;
2906 if (target_index < 0) target_index += total_size;
2907 array->set(target_index, *value);
2908 }
2909
2910 // From here on we cannot fail and we shouldn't GC anymore.
2911 DisallowHeapAllocation no_allocation;
2912
Ben Murdoch097c5b22016-05-18 11:27:45 +01002913 Heap* heap = isolate->heap();
2914
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002915 // Copy (real) inobject properties. If necessary, stop at number_of_fields to
2916 // avoid overwriting |one_pointer_filler_map|.
2917 int limit = Min(inobject, number_of_fields);
2918 for (int i = 0; i < limit; i++) {
2919 FieldIndex index = FieldIndex::ForPropertyIndex(*new_map, i);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002920 Object* value = array->get(external + i);
2921 // Can't use JSObject::FastPropertyAtPut() because proper map was not set
2922 // yet.
2923 if (new_map->IsUnboxedDoubleField(index)) {
2924 DCHECK(value->IsMutableHeapNumber());
2925 object->RawFastDoublePropertyAtPut(index,
2926 HeapNumber::cast(value)->value());
Ben Murdoch097c5b22016-05-18 11:27:45 +01002927 if (i < old_number_of_fields && !old_map->IsUnboxedDoubleField(index)) {
2928 // Transition from tagged to untagged slot.
2929 heap->ClearRecordedSlot(*object,
2930 HeapObject::RawField(*object, index.offset()));
2931 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002932 } else {
2933 object->RawFastPropertyAtPut(index, value);
2934 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002935 }
2936
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002937
2938 // If there are properties in the new backing store, trim it to the correct
2939 // size and install the backing store into the object.
2940 if (external > 0) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002941 heap->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>(*array, inobject);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002942 object->set_properties(*array);
2943 }
2944
2945 // Create filler object past the new instance size.
2946 int new_instance_size = new_map->instance_size();
2947 int instance_size_delta = old_map->instance_size() - new_instance_size;
2948 DCHECK(instance_size_delta >= 0);
2949
2950 if (instance_size_delta > 0) {
2951 Address address = object->address();
Ben Murdochda12d292016-06-02 14:46:10 +01002952 heap->CreateFillerObjectAt(address + new_instance_size, instance_size_delta,
2953 ClearRecordedSlots::kYes);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002954 heap->AdjustLiveBytes(*object, -instance_size_delta,
2955 Heap::CONCURRENT_TO_SWEEPER);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002956 }
2957
2958 // We are storing the new map using release store after creating a filler for
2959 // the left-over space to avoid races with the sweeper thread.
2960 object->synchronized_set_map(*new_map);
2961}
2962
Ben Murdoch097c5b22016-05-18 11:27:45 +01002963void MigrateFastToSlow(Handle<JSObject> object, Handle<Map> new_map,
2964 int expected_additional_properties) {
2965 // The global object is always normalized.
2966 DCHECK(!object->IsJSGlobalObject());
2967 // JSGlobalProxy must never be normalized
2968 DCHECK(!object->IsJSGlobalProxy());
2969
2970 Isolate* isolate = object->GetIsolate();
2971 HandleScope scope(isolate);
2972 Handle<Map> map(object->map());
2973
2974 // Allocate new content.
2975 int real_size = map->NumberOfOwnDescriptors();
2976 int property_count = real_size;
2977 if (expected_additional_properties > 0) {
2978 property_count += expected_additional_properties;
2979 } else {
2980 property_count += 2; // Make space for two more properties.
2981 }
2982 Handle<NameDictionary> dictionary =
2983 NameDictionary::New(isolate, property_count);
2984
2985 Handle<DescriptorArray> descs(map->instance_descriptors());
2986 for (int i = 0; i < real_size; i++) {
2987 PropertyDetails details = descs->GetDetails(i);
2988 Handle<Name> key(descs->GetKey(i));
2989 switch (details.type()) {
2990 case DATA_CONSTANT: {
2991 Handle<Object> value(descs->GetConstant(i), isolate);
2992 PropertyDetails d(details.attributes(), DATA, i + 1,
2993 PropertyCellType::kNoCell);
2994 dictionary = NameDictionary::Add(dictionary, key, value, d);
2995 break;
2996 }
2997 case DATA: {
2998 FieldIndex index = FieldIndex::ForDescriptor(*map, i);
2999 Handle<Object> value;
3000 if (object->IsUnboxedDoubleField(index)) {
3001 double old_value = object->RawFastDoublePropertyAt(index);
3002 value = isolate->factory()->NewHeapNumber(old_value);
3003 } else {
3004 value = handle(object->RawFastPropertyAt(index), isolate);
3005 if (details.representation().IsDouble()) {
3006 DCHECK(value->IsMutableHeapNumber());
3007 Handle<HeapNumber> old = Handle<HeapNumber>::cast(value);
3008 value = isolate->factory()->NewHeapNumber(old->value());
3009 }
3010 }
3011 PropertyDetails d(details.attributes(), DATA, i + 1,
3012 PropertyCellType::kNoCell);
3013 dictionary = NameDictionary::Add(dictionary, key, value, d);
3014 break;
3015 }
3016 case ACCESSOR: {
3017 FieldIndex index = FieldIndex::ForDescriptor(*map, i);
3018 Handle<Object> value(object->RawFastPropertyAt(index), isolate);
3019 PropertyDetails d(details.attributes(), ACCESSOR_CONSTANT, i + 1,
3020 PropertyCellType::kNoCell);
3021 dictionary = NameDictionary::Add(dictionary, key, value, d);
3022 break;
3023 }
3024 case ACCESSOR_CONSTANT: {
3025 Handle<Object> value(descs->GetCallbacksObject(i), isolate);
3026 PropertyDetails d(details.attributes(), ACCESSOR_CONSTANT, i + 1,
3027 PropertyCellType::kNoCell);
3028 dictionary = NameDictionary::Add(dictionary, key, value, d);
3029 break;
3030 }
3031 }
3032 }
3033
3034 // Copy the next enumeration index from instance descriptor.
3035 dictionary->SetNextEnumerationIndex(real_size + 1);
3036
3037 // From here on we cannot fail and we shouldn't GC anymore.
3038 DisallowHeapAllocation no_allocation;
3039
3040 // Resize the object in the heap if necessary.
3041 int new_instance_size = new_map->instance_size();
3042 int instance_size_delta = map->instance_size() - new_instance_size;
3043 DCHECK(instance_size_delta >= 0);
3044
3045 if (instance_size_delta > 0) {
3046 Heap* heap = isolate->heap();
3047 heap->CreateFillerObjectAt(object->address() + new_instance_size,
Ben Murdochda12d292016-06-02 14:46:10 +01003048 instance_size_delta, ClearRecordedSlots::kYes);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003049 heap->AdjustLiveBytes(*object, -instance_size_delta,
3050 Heap::CONCURRENT_TO_SWEEPER);
3051 }
3052
3053 // We are storing the new map using release store after creating a filler for
3054 // the left-over space to avoid races with the sweeper thread.
3055 object->synchronized_set_map(*new_map);
3056
3057 object->set_properties(*dictionary);
3058
3059 // Ensure that in-object space of slow-mode object does not contain random
3060 // garbage.
3061 int inobject_properties = new_map->GetInObjectProperties();
3062 for (int i = 0; i < inobject_properties; i++) {
3063 FieldIndex index = FieldIndex::ForPropertyIndex(*new_map, i);
3064 object->RawFastPropertyAtPut(index, Smi::FromInt(0));
3065 }
3066
3067 isolate->counters()->props_to_dictionary()->Increment();
3068
3069#ifdef DEBUG
3070 if (FLAG_trace_normalization) {
3071 OFStream os(stdout);
3072 os << "Object properties have been normalized:\n";
3073 object->Print(os);
3074 }
3075#endif
3076}
3077
3078} // namespace
3079
3080void JSObject::MigrateToMap(Handle<JSObject> object, Handle<Map> new_map,
3081 int expected_additional_properties) {
3082 if (object->map() == *new_map) return;
3083 Handle<Map> old_map(object->map());
3084 if (old_map->is_prototype_map()) {
3085 // If this object is a prototype (the callee will check), invalidate any
3086 // prototype chains involving it.
3087 InvalidatePrototypeChains(object->map());
3088
3089 // If the map was registered with its prototype before, ensure that it
3090 // registers with its new prototype now. This preserves the invariant that
3091 // when a map on a prototype chain is registered with its prototype, then
3092 // all prototypes further up the chain are also registered with their
3093 // respective prototypes.
3094 UpdatePrototypeUserRegistration(old_map, new_map, new_map->GetIsolate());
3095 }
3096
3097 if (old_map->is_dictionary_map()) {
3098 // For slow-to-fast migrations JSObject::MigrateSlowToFast()
3099 // must be used instead.
3100 CHECK(new_map->is_dictionary_map());
3101
3102 // Slow-to-slow migration is trivial.
3103 object->set_map(*new_map);
3104 } else if (!new_map->is_dictionary_map()) {
3105 MigrateFastToFast(object, new_map);
3106 if (old_map->is_prototype_map()) {
3107 DCHECK(!old_map->is_stable());
3108 DCHECK(new_map->is_stable());
3109 // Clear out the old descriptor array to avoid problems to sharing
3110 // the descriptor array without using an explicit.
3111 old_map->InitializeDescriptors(
3112 old_map->GetHeap()->empty_descriptor_array(),
3113 LayoutDescriptor::FastPointerLayout());
3114 // Ensure that no transition was inserted for prototype migrations.
3115 DCHECK_EQ(
3116 0, TransitionArray::NumberOfTransitions(old_map->raw_transitions()));
3117 DCHECK(new_map->GetBackPointer()->IsUndefined());
3118 }
3119 } else {
3120 MigrateFastToSlow(object, new_map, expected_additional_properties);
3121 }
3122
3123 // Careful: Don't allocate here!
3124 // For some callers of this method, |object| might be in an inconsistent
3125 // state now: the new map might have a new elements_kind, but the object's
3126 // elements pointer hasn't been updated yet. Callers will fix this, but in
3127 // the meantime, (indirectly) calling JSObjectVerify() must be avoided.
3128 // When adding code here, add a DisallowHeapAllocation too.
3129}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003130
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003131int Map::NumberOfFields() {
3132 DescriptorArray* descriptors = instance_descriptors();
3133 int result = 0;
3134 for (int i = 0; i < NumberOfOwnDescriptors(); i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003135 if (descriptors->GetDetails(i).location() == kField) result++;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003136 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003137 return result;
3138}
3139
3140
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003141Handle<Map> Map::CopyGeneralizeAllRepresentations(
3142 Handle<Map> map, int modify_index, StoreMode store_mode, PropertyKind kind,
3143 PropertyAttributes attributes, const char* reason) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003144 Isolate* isolate = map->GetIsolate();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003145 Handle<DescriptorArray> old_descriptors(map->instance_descriptors(), isolate);
3146 int number_of_own_descriptors = map->NumberOfOwnDescriptors();
3147 Handle<DescriptorArray> descriptors =
3148 DescriptorArray::CopyUpTo(old_descriptors, number_of_own_descriptors);
Steve Blocka7e24c12009-10-30 11:49:00 +00003149
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003150 for (int i = 0; i < number_of_own_descriptors; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003151 descriptors->SetRepresentation(i, Representation::Tagged());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003152 if (descriptors->GetDetails(i).type() == DATA) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003153 descriptors->SetValue(i, FieldType::Any());
John Reck59135872010-11-02 12:39:01 -07003154 }
3155 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003156
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003157 Handle<LayoutDescriptor> new_layout_descriptor(
3158 LayoutDescriptor::FastPointerLayout(), isolate);
3159 Handle<Map> new_map = CopyReplaceDescriptors(
3160 map, descriptors, new_layout_descriptor, OMIT_TRANSITION,
3161 MaybeHandle<Name>(), reason, SPECIAL_TRANSITION);
3162
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003163 // Unless the instance is being migrated, ensure that modify_index is a field.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003164 if (modify_index >= 0) {
3165 PropertyDetails details = descriptors->GetDetails(modify_index);
3166 if (store_mode == FORCE_FIELD &&
3167 (details.type() != DATA || details.attributes() != attributes)) {
3168 int field_index = details.type() == DATA ? details.field_index()
3169 : new_map->NumberOfFields();
3170 DataDescriptor d(handle(descriptors->GetKey(modify_index), isolate),
3171 field_index, attributes, Representation::Tagged());
3172 descriptors->Replace(modify_index, &d);
3173 if (details.type() != DATA) {
3174 int unused_property_fields = new_map->unused_property_fields() - 1;
3175 if (unused_property_fields < 0) {
3176 unused_property_fields += JSObject::kFieldsAdded;
3177 }
3178 new_map->set_unused_property_fields(unused_property_fields);
John Reck59135872010-11-02 12:39:01 -07003179 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003180 } else {
3181 DCHECK(details.attributes() == attributes);
John Reck59135872010-11-02 12:39:01 -07003182 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003183
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003184 if (FLAG_trace_generalization) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003185 MaybeHandle<FieldType> field_type = FieldType::None(isolate);
3186 if (details.type() == DATA) {
3187 field_type = handle(
3188 map->instance_descriptors()->GetFieldType(modify_index), isolate);
3189 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003190 map->PrintGeneralization(
3191 stdout, reason, modify_index, new_map->NumberOfOwnDescriptors(),
3192 new_map->NumberOfOwnDescriptors(),
3193 details.type() == DATA_CONSTANT && store_mode == FORCE_FIELD,
3194 details.representation(), Representation::Tagged(), field_type,
Ben Murdoch097c5b22016-05-18 11:27:45 +01003195 MaybeHandle<Object>(), FieldType::Any(isolate),
3196 MaybeHandle<Object>());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003197 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003198 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003199 return new_map;
Steve Blocka7e24c12009-10-30 11:49:00 +00003200}
3201
3202
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003203void Map::DeprecateTransitionTree() {
3204 if (is_deprecated()) return;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003205 Object* transitions = raw_transitions();
3206 int num_transitions = TransitionArray::NumberOfTransitions(transitions);
3207 for (int i = 0; i < num_transitions; ++i) {
3208 TransitionArray::GetTarget(transitions, i)->DeprecateTransitionTree();
Steve Blocka7e24c12009-10-30 11:49:00 +00003209 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003210 deprecate();
3211 dependent_code()->DeoptimizeDependentCodeGroup(
3212 GetIsolate(), DependentCode::kTransitionGroup);
3213 NotifyLeafMapLayoutChange();
Steve Blocka7e24c12009-10-30 11:49:00 +00003214}
3215
3216
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003217static inline bool EqualImmutableValues(Object* obj1, Object* obj2) {
3218 if (obj1 == obj2) return true; // Valid for both kData and kAccessor kinds.
3219 // TODO(ishell): compare AccessorPairs.
3220 return false;
3221}
Steve Blocka7e24c12009-10-30 11:49:00 +00003222
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003223
3224// Installs |new_descriptors| over the current instance_descriptors to ensure
3225// proper sharing of descriptor arrays.
3226void Map::ReplaceDescriptors(DescriptorArray* new_descriptors,
3227 LayoutDescriptor* new_layout_descriptor) {
3228 // Don't overwrite the empty descriptor array or initial map's descriptors.
3229 if (NumberOfOwnDescriptors() == 0 || GetBackPointer()->IsUndefined()) {
3230 return;
3231 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003232
3233 DescriptorArray* to_replace = instance_descriptors();
Ben Murdochda12d292016-06-02 14:46:10 +01003234 GetHeap()->incremental_marking()->IterateBlackObject(to_replace);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003235 Map* current = this;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003236 while (current->instance_descriptors() == to_replace) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003237 Object* next = current->GetBackPointer();
3238 if (next->IsUndefined()) break; // Stop overwriting at initial map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003239 current->SetEnumLength(kInvalidEnumCacheSentinel);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003240 current->UpdateDescriptors(new_descriptors, new_layout_descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003241 current = Map::cast(next);
3242 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003243 set_owns_descriptors(false);
3244}
3245
3246
3247Map* Map::FindRootMap() {
3248 Map* result = this;
3249 while (true) {
3250 Object* back = result->GetBackPointer();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003251 if (back->IsUndefined()) {
3252 // Initial map always owns descriptors and doesn't have unused entries
3253 // in the descriptor array.
3254 DCHECK(result->owns_descriptors());
3255 DCHECK_EQ(result->NumberOfOwnDescriptors(),
3256 result->instance_descriptors()->number_of_descriptors());
3257 return result;
3258 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003259 result = Map::cast(back);
3260 }
3261}
3262
3263
3264Map* Map::FindLastMatchMap(int verbatim,
3265 int length,
3266 DescriptorArray* descriptors) {
3267 DisallowHeapAllocation no_allocation;
3268
3269 // This can only be called on roots of transition trees.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003270 DCHECK_EQ(verbatim, NumberOfOwnDescriptors());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003271
3272 Map* current = this;
3273
3274 for (int i = verbatim; i < length; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003275 Name* name = descriptors->GetKey(i);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003276 PropertyDetails details = descriptors->GetDetails(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003277 Map* next = TransitionArray::SearchTransition(current, details.kind(), name,
3278 details.attributes());
3279 if (next == NULL) break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003280 DescriptorArray* next_descriptors = next->instance_descriptors();
3281
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003282 PropertyDetails next_details = next_descriptors->GetDetails(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003283 DCHECK_EQ(details.kind(), next_details.kind());
3284 DCHECK_EQ(details.attributes(), next_details.attributes());
3285 if (details.location() != next_details.location()) break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003286 if (!details.representation().Equals(next_details.representation())) break;
Steve Blocka7e24c12009-10-30 11:49:00 +00003287
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003288 if (next_details.location() == kField) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003289 FieldType* next_field_type = next_descriptors->GetFieldType(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003290 if (!descriptors->GetFieldType(i)->NowIs(next_field_type)) {
3291 break;
3292 }
3293 } else {
3294 if (!EqualImmutableValues(descriptors->GetValue(i),
3295 next_descriptors->GetValue(i))) {
3296 break;
3297 }
3298 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003299 current = next;
3300 }
3301 return current;
Steve Blocka7e24c12009-10-30 11:49:00 +00003302}
3303
3304
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003305Map* Map::FindFieldOwner(int descriptor) {
3306 DisallowHeapAllocation no_allocation;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003307 DCHECK_EQ(DATA, instance_descriptors()->GetDetails(descriptor).type());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003308 Map* result = this;
3309 while (true) {
3310 Object* back = result->GetBackPointer();
3311 if (back->IsUndefined()) break;
3312 Map* parent = Map::cast(back);
3313 if (parent->NumberOfOwnDescriptors() <= descriptor) break;
3314 result = parent;
Steve Blocka7e24c12009-10-30 11:49:00 +00003315 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003316 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00003317}
3318
3319
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003320void Map::UpdateFieldType(int descriptor, Handle<Name> name,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003321 Representation new_representation,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003322 Handle<Object> new_wrapped_type) {
3323 DCHECK(new_wrapped_type->IsSmi() || new_wrapped_type->IsWeakCell());
Ben Murdochda12d292016-06-02 14:46:10 +01003324 // We store raw pointers in the queue, so no allocations are allowed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003325 DisallowHeapAllocation no_allocation;
3326 PropertyDetails details = instance_descriptors()->GetDetails(descriptor);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003327 if (details.type() != DATA) return;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003328
Ben Murdochda12d292016-06-02 14:46:10 +01003329 Zone zone(GetIsolate()->allocator());
3330 ZoneQueue<Map*> backlog(&zone);
3331 backlog.push(this);
3332
3333 while (!backlog.empty()) {
3334 Map* current = backlog.front();
3335 backlog.pop();
3336
3337 Object* transitions = current->raw_transitions();
3338 int num_transitions = TransitionArray::NumberOfTransitions(transitions);
3339 for (int i = 0; i < num_transitions; ++i) {
3340 Map* target = TransitionArray::GetTarget(transitions, i);
3341 backlog.push(target);
3342 }
3343 DescriptorArray* descriptors = current->instance_descriptors();
3344 PropertyDetails details = descriptors->GetDetails(descriptor);
3345
3346 // It is allowed to change representation here only from None to something.
3347 DCHECK(details.representation().Equals(new_representation) ||
3348 details.representation().IsNone());
3349
3350 // Skip if already updated the shared descriptor.
3351 if (descriptors->GetValue(descriptor) != *new_wrapped_type) {
3352 DataDescriptor d(name, descriptors->GetFieldIndex(descriptor),
3353 new_wrapped_type, details.attributes(),
3354 new_representation);
3355 descriptors->Replace(descriptor, &d);
3356 }
3357 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003358}
3359
Ben Murdoch097c5b22016-05-18 11:27:45 +01003360bool FieldTypeIsCleared(Representation rep, FieldType* type) {
3361 return type->IsNone() && rep.IsHeapObject();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003362}
3363
3364
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003365// static
Ben Murdoch097c5b22016-05-18 11:27:45 +01003366Handle<FieldType> Map::GeneralizeFieldType(Representation rep1,
3367 Handle<FieldType> type1,
3368 Representation rep2,
3369 Handle<FieldType> type2,
3370 Isolate* isolate) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003371 // Cleared field types need special treatment. They represent lost knowledge,
3372 // so we must be conservative, so their generalization with any other type
3373 // is "Any".
3374 if (FieldTypeIsCleared(rep1, *type1) || FieldTypeIsCleared(rep2, *type2)) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003375 return FieldType::Any(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003376 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003377 if (type1->NowIs(type2)) return type2;
3378 if (type2->NowIs(type1)) return type1;
Ben Murdoch097c5b22016-05-18 11:27:45 +01003379 return FieldType::Any(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003380}
3381
3382
3383// static
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003384void Map::GeneralizeFieldType(Handle<Map> map, int modify_index,
3385 Representation new_representation,
Ben Murdoch097c5b22016-05-18 11:27:45 +01003386 Handle<FieldType> new_field_type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003387 Isolate* isolate = map->GetIsolate();
3388
3389 // Check if we actually need to generalize the field type at all.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003390 Handle<DescriptorArray> old_descriptors(map->instance_descriptors(), isolate);
3391 Representation old_representation =
3392 old_descriptors->GetDetails(modify_index).representation();
Ben Murdoch097c5b22016-05-18 11:27:45 +01003393 Handle<FieldType> old_field_type(old_descriptors->GetFieldType(modify_index),
3394 isolate);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003395
3396 if (old_representation.Equals(new_representation) &&
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003397 !FieldTypeIsCleared(new_representation, *new_field_type) &&
3398 // Checking old_field_type for being cleared is not necessary because
3399 // the NowIs check below would fail anyway in that case.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003400 new_field_type->NowIs(old_field_type)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003401 DCHECK(Map::GeneralizeFieldType(old_representation, old_field_type,
3402 new_representation, new_field_type, isolate)
3403 ->NowIs(old_field_type));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003404 return;
3405 }
3406
3407 // Determine the field owner.
3408 Handle<Map> field_owner(map->FindFieldOwner(modify_index), isolate);
3409 Handle<DescriptorArray> descriptors(
3410 field_owner->instance_descriptors(), isolate);
3411 DCHECK_EQ(*old_field_type, descriptors->GetFieldType(modify_index));
3412
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003413 new_field_type =
3414 Map::GeneralizeFieldType(old_representation, old_field_type,
3415 new_representation, new_field_type, isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003416
3417 PropertyDetails details = descriptors->GetDetails(modify_index);
3418 Handle<Name> name(descriptors->GetKey(modify_index));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003419
3420 Handle<Object> wrapped_type(WrapType(new_field_type));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003421 field_owner->UpdateFieldType(modify_index, name, new_representation,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003422 wrapped_type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003423 field_owner->dependent_code()->DeoptimizeDependentCodeGroup(
3424 isolate, DependentCode::kFieldTypeGroup);
3425
3426 if (FLAG_trace_generalization) {
3427 map->PrintGeneralization(
Ben Murdoch097c5b22016-05-18 11:27:45 +01003428 stdout, "field type generalization", modify_index,
3429 map->NumberOfOwnDescriptors(), map->NumberOfOwnDescriptors(), false,
3430 details.representation(), details.representation(), old_field_type,
3431 MaybeHandle<Object>(), new_field_type, MaybeHandle<Object>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003432 }
3433}
3434
Ben Murdoch097c5b22016-05-18 11:27:45 +01003435static inline Handle<FieldType> GetFieldType(
3436 Isolate* isolate, Handle<DescriptorArray> descriptors, int descriptor,
3437 PropertyLocation location, Representation representation) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003438#ifdef DEBUG
3439 PropertyDetails details = descriptors->GetDetails(descriptor);
3440 DCHECK_EQ(kData, details.kind());
3441 DCHECK_EQ(details.location(), location);
3442#endif
3443 if (location == kField) {
3444 return handle(descriptors->GetFieldType(descriptor), isolate);
3445 } else {
3446 return descriptors->GetValue(descriptor)
3447 ->OptimalType(isolate, representation);
3448 }
3449}
3450
3451
3452// Reconfigures property at |modify_index| with |new_kind|, |new_attributes|,
3453// |store_mode| and/or |new_representation|/|new_field_type|.
3454// If |modify_index| is negative then no properties are reconfigured but the
3455// map is migrated to the up-to-date non-deprecated state.
3456//
3457// This method rewrites or completes the transition tree to reflect the new
3458// change. To avoid high degrees over polymorphism, and to stabilize quickly,
3459// on every rewrite the new type is deduced by merging the current type with
3460// any potential new (partial) version of the type in the transition tree.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003461// To do this, on each rewrite:
3462// - Search the root of the transition tree using FindRootMap.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003463// - Find |target_map|, the newest matching version of this map using the
3464// virtually "enhanced" |old_map|'s descriptor array (i.e. whose entry at
3465// |modify_index| is considered to be of |new_kind| and having
3466// |new_attributes|) to walk the transition tree.
3467// - Merge/generalize the "enhanced" descriptor array of the |old_map| and
3468// descriptor array of the |target_map|.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003469// - Generalize the |modify_index| descriptor using |new_representation| and
3470// |new_field_type|.
3471// - Walk the tree again starting from the root towards |target_map|. Stop at
3472// |split_map|, the first map who's descriptor array does not match the merged
3473// descriptor array.
3474// - If |target_map| == |split_map|, |target_map| is in the expected state.
3475// Return it.
3476// - Otherwise, invalidate the outdated transition target from |target_map|, and
3477// replace its transition tree with a new branch for the updated descriptors.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003478Handle<Map> Map::ReconfigureProperty(Handle<Map> old_map, int modify_index,
3479 PropertyKind new_kind,
3480 PropertyAttributes new_attributes,
3481 Representation new_representation,
Ben Murdoch097c5b22016-05-18 11:27:45 +01003482 Handle<FieldType> new_field_type,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003483 StoreMode store_mode) {
3484 DCHECK_NE(kAccessor, new_kind); // TODO(ishell): not supported yet.
3485 DCHECK(store_mode != FORCE_FIELD || modify_index >= 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003486 Isolate* isolate = old_map->GetIsolate();
3487
3488 Handle<DescriptorArray> old_descriptors(
3489 old_map->instance_descriptors(), isolate);
3490 int old_nof = old_map->NumberOfOwnDescriptors();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003491
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003492 // If it's just a representation generalization case (i.e. property kind and
3493 // attributes stays unchanged) it's fine to transition from None to anything
3494 // but double without any modification to the object, because the default
3495 // uninitialized value for representation None can be overwritten by both
3496 // smi and tagged values. Doubles, however, would require a box allocation.
3497 if (modify_index >= 0 && !new_representation.IsNone() &&
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003498 !new_representation.IsDouble()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003499 PropertyDetails old_details = old_descriptors->GetDetails(modify_index);
3500 Representation old_representation = old_details.representation();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003501
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003502 if (old_representation.IsNone()) {
3503 DCHECK_EQ(new_kind, old_details.kind());
3504 DCHECK_EQ(new_attributes, old_details.attributes());
3505 DCHECK_EQ(DATA, old_details.type());
3506 if (FLAG_trace_generalization) {
3507 old_map->PrintGeneralization(
3508 stdout, "uninitialized field", modify_index,
3509 old_map->NumberOfOwnDescriptors(),
3510 old_map->NumberOfOwnDescriptors(), false, old_representation,
Ben Murdoch097c5b22016-05-18 11:27:45 +01003511 new_representation,
3512 handle(old_descriptors->GetFieldType(modify_index), isolate),
3513 MaybeHandle<Object>(), new_field_type, MaybeHandle<Object>());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003514 }
3515 Handle<Map> field_owner(old_map->FindFieldOwner(modify_index), isolate);
3516
3517 GeneralizeFieldType(field_owner, modify_index, new_representation,
3518 new_field_type);
3519 DCHECK(old_descriptors->GetDetails(modify_index)
3520 .representation()
3521 .Equals(new_representation));
3522 DCHECK(
3523 old_descriptors->GetFieldType(modify_index)->NowIs(new_field_type));
3524 return old_map;
3525 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003526 }
3527
3528 // Check the state of the root map.
3529 Handle<Map> root_map(old_map->FindRootMap(), isolate);
3530 if (!old_map->EquivalentToForTransition(*root_map)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003531 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003532 new_kind, new_attributes,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003533 "GenAll_NotEquivalent");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003534 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003535
3536 ElementsKind from_kind = root_map->elements_kind();
3537 ElementsKind to_kind = old_map->elements_kind();
3538 // TODO(ishell): Add a test for SLOW_SLOPPY_ARGUMENTS_ELEMENTS.
3539 if (from_kind != to_kind && to_kind != DICTIONARY_ELEMENTS &&
3540 to_kind != SLOW_SLOPPY_ARGUMENTS_ELEMENTS &&
3541 !(IsTransitionableFastElementsKind(from_kind) &&
3542 IsMoreGeneralElementsKindTransition(from_kind, to_kind))) {
3543 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
3544 new_kind, new_attributes,
3545 "GenAll_InvalidElementsTransition");
3546 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003547 int root_nof = root_map->NumberOfOwnDescriptors();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003548 if (modify_index >= 0 && modify_index < root_nof) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003549 PropertyDetails old_details = old_descriptors->GetDetails(modify_index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003550 if (old_details.kind() != new_kind ||
3551 old_details.attributes() != new_attributes) {
3552 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
3553 new_kind, new_attributes,
3554 "GenAll_RootModification1");
3555 }
3556 if ((old_details.type() != DATA && store_mode == FORCE_FIELD) ||
3557 (old_details.type() == DATA &&
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003558 (!new_field_type->NowIs(old_descriptors->GetFieldType(modify_index)) ||
3559 !new_representation.fits_into(old_details.representation())))) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003560 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003561 new_kind, new_attributes,
3562 "GenAll_RootModification2");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003563 }
3564 }
3565
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003566 // From here on, use the map with correct elements kind as root map.
3567 if (from_kind != to_kind) {
3568 root_map = Map::AsElementsKind(root_map, to_kind);
3569 }
3570
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003571 Handle<Map> target_map = root_map;
3572 for (int i = root_nof; i < old_nof; ++i) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003573 PropertyDetails old_details = old_descriptors->GetDetails(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003574 PropertyKind next_kind;
3575 PropertyLocation next_location;
3576 PropertyAttributes next_attributes;
3577 Representation next_representation;
3578 bool property_kind_reconfiguration = false;
3579
3580 if (modify_index == i) {
3581 DCHECK_EQ(FORCE_FIELD, store_mode);
3582 property_kind_reconfiguration = old_details.kind() != new_kind;
3583
3584 next_kind = new_kind;
3585 next_location = kField;
3586 next_attributes = new_attributes;
3587 // If property kind is not reconfigured merge the result with
3588 // representation/field type from the old descriptor.
3589 next_representation = new_representation;
3590 if (!property_kind_reconfiguration) {
3591 next_representation =
3592 next_representation.generalize(old_details.representation());
3593 }
3594
3595 } else {
3596 next_kind = old_details.kind();
3597 next_location = old_details.location();
3598 next_attributes = old_details.attributes();
3599 next_representation = old_details.representation();
3600 }
3601 Map* transition = TransitionArray::SearchTransition(
3602 *target_map, next_kind, old_descriptors->GetKey(i), next_attributes);
3603 if (transition == NULL) break;
3604 Handle<Map> tmp_map(transition, isolate);
3605
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003606 Handle<DescriptorArray> tmp_descriptors = handle(
3607 tmp_map->instance_descriptors(), isolate);
3608
3609 // Check if target map is incompatible.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003610 PropertyDetails tmp_details = tmp_descriptors->GetDetails(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003611 DCHECK_EQ(next_kind, tmp_details.kind());
3612 DCHECK_EQ(next_attributes, tmp_details.attributes());
3613 if (next_kind == kAccessor &&
3614 !EqualImmutableValues(old_descriptors->GetValue(i),
3615 tmp_descriptors->GetValue(i))) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003616 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003617 new_kind, new_attributes,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003618 "GenAll_Incompatible");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003619 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003620 if (next_location == kField && tmp_details.location() == kDescriptor) break;
3621
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003622 Representation tmp_representation = tmp_details.representation();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003623 if (!next_representation.fits_into(tmp_representation)) break;
3624
3625 PropertyLocation old_location = old_details.location();
3626 PropertyLocation tmp_location = tmp_details.location();
3627 if (tmp_location == kField) {
3628 if (next_kind == kData) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003629 Handle<FieldType> next_field_type;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003630 if (modify_index == i) {
3631 next_field_type = new_field_type;
3632 if (!property_kind_reconfiguration) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003633 Handle<FieldType> old_field_type =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003634 GetFieldType(isolate, old_descriptors, i,
3635 old_details.location(), tmp_representation);
3636 Representation old_representation = old_details.representation();
3637 next_field_type = GeneralizeFieldType(
3638 old_representation, old_field_type, new_representation,
3639 next_field_type, isolate);
3640 }
3641 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003642 Handle<FieldType> old_field_type =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003643 GetFieldType(isolate, old_descriptors, i, old_details.location(),
3644 tmp_representation);
3645 next_field_type = old_field_type;
3646 }
3647 GeneralizeFieldType(tmp_map, i, tmp_representation, next_field_type);
3648 }
3649 } else if (old_location == kField ||
3650 !EqualImmutableValues(old_descriptors->GetValue(i),
3651 tmp_descriptors->GetValue(i))) {
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01003652 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00003653 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003654 DCHECK(!tmp_map->is_deprecated());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003655 target_map = tmp_map;
Steve Blocka7e24c12009-10-30 11:49:00 +00003656 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003657
3658 // Directly change the map if the target map is more general.
3659 Handle<DescriptorArray> target_descriptors(
3660 target_map->instance_descriptors(), isolate);
3661 int target_nof = target_map->NumberOfOwnDescriptors();
3662 if (target_nof == old_nof &&
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003663 (store_mode != FORCE_FIELD ||
3664 (modify_index >= 0 &&
3665 target_descriptors->GetDetails(modify_index).location() == kField))) {
3666#ifdef DEBUG
3667 if (modify_index >= 0) {
3668 PropertyDetails details = target_descriptors->GetDetails(modify_index);
3669 DCHECK_EQ(new_kind, details.kind());
3670 DCHECK_EQ(new_attributes, details.attributes());
3671 DCHECK(new_representation.fits_into(details.representation()));
3672 DCHECK(details.location() != kField ||
3673 new_field_type->NowIs(
3674 target_descriptors->GetFieldType(modify_index)));
3675 }
3676#endif
3677 if (*target_map != *old_map) {
3678 old_map->NotifyLeafMapLayoutChange();
3679 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003680 return target_map;
3681 }
3682
3683 // Find the last compatible target map in the transition tree.
3684 for (int i = target_nof; i < old_nof; ++i) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003685 PropertyDetails old_details = old_descriptors->GetDetails(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003686 PropertyKind next_kind;
3687 PropertyAttributes next_attributes;
3688 if (modify_index == i) {
3689 next_kind = new_kind;
3690 next_attributes = new_attributes;
3691 } else {
3692 next_kind = old_details.kind();
3693 next_attributes = old_details.attributes();
3694 }
3695 Map* transition = TransitionArray::SearchTransition(
3696 *target_map, next_kind, old_descriptors->GetKey(i), next_attributes);
3697 if (transition == NULL) break;
3698 Handle<Map> tmp_map(transition, isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003699 Handle<DescriptorArray> tmp_descriptors(
3700 tmp_map->instance_descriptors(), isolate);
3701
3702 // Check if target map is compatible.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003703#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003704 PropertyDetails tmp_details = tmp_descriptors->GetDetails(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003705 DCHECK_EQ(next_kind, tmp_details.kind());
3706 DCHECK_EQ(next_attributes, tmp_details.attributes());
3707#endif
3708 if (next_kind == kAccessor &&
3709 !EqualImmutableValues(old_descriptors->GetValue(i),
3710 tmp_descriptors->GetValue(i))) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003711 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003712 new_kind, new_attributes,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003713 "GenAll_Incompatible");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003714 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003715 DCHECK(!tmp_map->is_deprecated());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003716 target_map = tmp_map;
3717 }
3718 target_nof = target_map->NumberOfOwnDescriptors();
3719 target_descriptors = handle(target_map->instance_descriptors(), isolate);
3720
3721 // Allocate a new descriptor array large enough to hold the required
3722 // descriptors, with minimally the exact same size as the old descriptor
3723 // array.
3724 int new_slack = Max(
3725 old_nof, old_descriptors->number_of_descriptors()) - old_nof;
3726 Handle<DescriptorArray> new_descriptors = DescriptorArray::Allocate(
3727 isolate, old_nof, new_slack);
3728 DCHECK(new_descriptors->length() > target_descriptors->length() ||
3729 new_descriptors->NumberOfSlackDescriptors() > 0 ||
3730 new_descriptors->number_of_descriptors() ==
3731 old_descriptors->number_of_descriptors());
3732 DCHECK(new_descriptors->number_of_descriptors() == old_nof);
3733
3734 // 0 -> |root_nof|
3735 int current_offset = 0;
3736 for (int i = 0; i < root_nof; ++i) {
3737 PropertyDetails old_details = old_descriptors->GetDetails(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003738 if (old_details.location() == kField) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003739 current_offset += old_details.field_width_in_words();
3740 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003741 Descriptor d(handle(old_descriptors->GetKey(i), isolate),
3742 handle(old_descriptors->GetValue(i), isolate),
3743 old_details);
3744 new_descriptors->Set(i, &d);
3745 }
3746
3747 // |root_nof| -> |target_nof|
3748 for (int i = root_nof; i < target_nof; ++i) {
3749 Handle<Name> target_key(target_descriptors->GetKey(i), isolate);
3750 PropertyDetails old_details = old_descriptors->GetDetails(i);
3751 PropertyDetails target_details = target_descriptors->GetDetails(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003752
3753 PropertyKind next_kind;
3754 PropertyAttributes next_attributes;
3755 PropertyLocation next_location;
3756 Representation next_representation;
3757 bool property_kind_reconfiguration = false;
3758
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003759 if (modify_index == i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003760 DCHECK_EQ(FORCE_FIELD, store_mode);
3761 property_kind_reconfiguration = old_details.kind() != new_kind;
3762
3763 next_kind = new_kind;
3764 next_attributes = new_attributes;
3765 next_location = kField;
3766
3767 // Merge new representation/field type with ones from the target
3768 // descriptor. If property kind is not reconfigured merge the result with
3769 // representation/field type from the old descriptor.
3770 next_representation =
3771 new_representation.generalize(target_details.representation());
3772 if (!property_kind_reconfiguration) {
3773 next_representation =
3774 next_representation.generalize(old_details.representation());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003775 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003776 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003777 // Merge old_descriptor and target_descriptor entries.
3778 DCHECK_EQ(target_details.kind(), old_details.kind());
3779 next_kind = target_details.kind();
3780 next_attributes = target_details.attributes();
3781 next_location =
3782 old_details.location() == kField ||
3783 target_details.location() == kField ||
3784 !EqualImmutableValues(target_descriptors->GetValue(i),
3785 old_descriptors->GetValue(i))
3786 ? kField
3787 : kDescriptor;
3788
3789 next_representation = old_details.representation().generalize(
3790 target_details.representation());
3791 }
3792 DCHECK_EQ(next_kind, target_details.kind());
3793 DCHECK_EQ(next_attributes, target_details.attributes());
3794
3795 if (next_location == kField) {
3796 if (next_kind == kData) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003797 Handle<FieldType> target_field_type =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003798 GetFieldType(isolate, target_descriptors, i,
3799 target_details.location(), next_representation);
3800
Ben Murdoch097c5b22016-05-18 11:27:45 +01003801 Handle<FieldType> next_field_type;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003802 if (modify_index == i) {
3803 next_field_type = GeneralizeFieldType(
3804 target_details.representation(), target_field_type,
3805 new_representation, new_field_type, isolate);
3806 if (!property_kind_reconfiguration) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003807 Handle<FieldType> old_field_type =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003808 GetFieldType(isolate, old_descriptors, i,
3809 old_details.location(), next_representation);
3810 next_field_type = GeneralizeFieldType(
3811 old_details.representation(), old_field_type,
3812 next_representation, next_field_type, isolate);
3813 }
3814 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003815 Handle<FieldType> old_field_type =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003816 GetFieldType(isolate, old_descriptors, i, old_details.location(),
3817 next_representation);
3818 next_field_type = GeneralizeFieldType(
3819 old_details.representation(), old_field_type, next_representation,
3820 target_field_type, isolate);
3821 }
3822 Handle<Object> wrapped_type(WrapType(next_field_type));
3823 DataDescriptor d(target_key, current_offset, wrapped_type,
3824 next_attributes, next_representation);
3825 current_offset += d.GetDetails().field_width_in_words();
3826 new_descriptors->Set(i, &d);
3827 } else {
3828 UNIMPLEMENTED(); // TODO(ishell): implement.
3829 }
3830 } else {
3831 PropertyDetails details(next_attributes, next_kind, next_location,
3832 next_representation);
3833 Descriptor d(target_key, handle(target_descriptors->GetValue(i), isolate),
3834 details);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003835 new_descriptors->Set(i, &d);
3836 }
3837 }
3838
3839 // |target_nof| -> |old_nof|
3840 for (int i = target_nof; i < old_nof; ++i) {
3841 PropertyDetails old_details = old_descriptors->GetDetails(i);
3842 Handle<Name> old_key(old_descriptors->GetKey(i), isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003843
3844 // Merge old_descriptor entry and modified details together.
3845 PropertyKind next_kind;
3846 PropertyAttributes next_attributes;
3847 PropertyLocation next_location;
3848 Representation next_representation;
3849 bool property_kind_reconfiguration = false;
3850
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003851 if (modify_index == i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003852 DCHECK_EQ(FORCE_FIELD, store_mode);
3853 // In case of property kind reconfiguration it is not necessary to
3854 // take into account representation/field type of the old descriptor.
3855 property_kind_reconfiguration = old_details.kind() != new_kind;
3856
3857 next_kind = new_kind;
3858 next_attributes = new_attributes;
3859 next_location = kField;
3860 next_representation = new_representation;
3861 if (!property_kind_reconfiguration) {
3862 next_representation =
3863 next_representation.generalize(old_details.representation());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003864 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003865 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003866 next_kind = old_details.kind();
3867 next_attributes = old_details.attributes();
3868 next_location = old_details.location();
3869 next_representation = old_details.representation();
3870 }
3871
3872 if (next_location == kField) {
3873 if (next_kind == kData) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003874 Handle<FieldType> next_field_type;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003875 if (modify_index == i) {
3876 next_field_type = new_field_type;
3877 if (!property_kind_reconfiguration) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003878 Handle<FieldType> old_field_type =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003879 GetFieldType(isolate, old_descriptors, i,
3880 old_details.location(), next_representation);
3881 next_field_type = GeneralizeFieldType(
3882 old_details.representation(), old_field_type,
3883 next_representation, next_field_type, isolate);
3884 }
3885 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003886 Handle<FieldType> old_field_type =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003887 GetFieldType(isolate, old_descriptors, i, old_details.location(),
3888 next_representation);
3889 next_field_type = old_field_type;
3890 }
3891
3892 Handle<Object> wrapped_type(WrapType(next_field_type));
3893
3894 DataDescriptor d(old_key, current_offset, wrapped_type, next_attributes,
3895 next_representation);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003896 current_offset += d.GetDetails().field_width_in_words();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003897 new_descriptors->Set(i, &d);
3898 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003899 UNIMPLEMENTED(); // TODO(ishell): implement.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003900 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003901 } else {
3902 PropertyDetails details(next_attributes, next_kind, next_location,
3903 next_representation);
3904 Descriptor d(old_key, handle(old_descriptors->GetValue(i), isolate),
3905 details);
3906 new_descriptors->Set(i, &d);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003907 }
3908 }
3909
3910 new_descriptors->Sort();
3911
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003912 DCHECK(store_mode != FORCE_FIELD ||
3913 new_descriptors->GetDetails(modify_index).location() == kField);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003914
3915 Handle<Map> split_map(root_map->FindLastMatchMap(
3916 root_nof, old_nof, *new_descriptors), isolate);
3917 int split_nof = split_map->NumberOfOwnDescriptors();
3918 DCHECK_NE(old_nof, split_nof);
3919
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003920 PropertyKind split_kind;
3921 PropertyAttributes split_attributes;
3922 if (modify_index == split_nof) {
3923 split_kind = new_kind;
3924 split_attributes = new_attributes;
3925 } else {
3926 PropertyDetails split_prop_details = old_descriptors->GetDetails(split_nof);
3927 split_kind = split_prop_details.kind();
3928 split_attributes = split_prop_details.attributes();
3929 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003930
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003931 // Invalidate a transition target at |key|.
3932 Map* maybe_transition = TransitionArray::SearchTransition(
3933 *split_map, split_kind, old_descriptors->GetKey(split_nof),
3934 split_attributes);
3935 if (maybe_transition != NULL) {
3936 maybe_transition->DeprecateTransitionTree();
3937 }
3938
3939 // If |maybe_transition| is not NULL then the transition array already
3940 // contains entry for given descriptor. This means that the transition
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003941 // could be inserted regardless of whether transitions array is full or not.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003942 if (maybe_transition == NULL &&
3943 !TransitionArray::CanHaveMoreTransitions(split_map)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003944 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003945 new_kind, new_attributes,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003946 "GenAll_CantHaveMoreTransitions");
3947 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003948
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003949 old_map->NotifyLeafMapLayoutChange();
3950
3951 if (FLAG_trace_generalization && modify_index >= 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003952 PropertyDetails old_details = old_descriptors->GetDetails(modify_index);
3953 PropertyDetails new_details = new_descriptors->GetDetails(modify_index);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003954 MaybeHandle<FieldType> old_field_type;
3955 MaybeHandle<FieldType> new_field_type;
3956 MaybeHandle<Object> old_value;
3957 MaybeHandle<Object> new_value;
3958 if (old_details.type() == DATA) {
3959 old_field_type =
3960 handle(old_descriptors->GetFieldType(modify_index), isolate);
3961 } else {
3962 old_value = handle(old_descriptors->GetValue(modify_index), isolate);
3963 }
3964 if (new_details.type() == DATA) {
3965 new_field_type =
3966 handle(new_descriptors->GetFieldType(modify_index), isolate);
3967 } else {
3968 new_value = handle(new_descriptors->GetValue(modify_index), isolate);
3969 }
3970
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003971 old_map->PrintGeneralization(
3972 stdout, "", modify_index, split_nof, old_nof,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003973 old_details.location() == kDescriptor && store_mode == FORCE_FIELD,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003974 old_details.representation(), new_details.representation(),
Ben Murdoch097c5b22016-05-18 11:27:45 +01003975 old_field_type, old_value, new_field_type, new_value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003976 }
3977
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003978 Handle<LayoutDescriptor> new_layout_descriptor =
3979 LayoutDescriptor::New(split_map, new_descriptors, old_nof);
3980
3981 Handle<Map> new_map =
3982 AddMissingTransitions(split_map, new_descriptors, new_layout_descriptor);
3983
3984 // Deprecated part of the transition tree is no longer reachable, so replace
3985 // current instance descriptors in the "survived" part of the tree with
3986 // the new descriptors to maintain descriptors sharing invariant.
3987 split_map->ReplaceDescriptors(*new_descriptors, *new_layout_descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003988 return new_map;
Steve Blocka7e24c12009-10-30 11:49:00 +00003989}
3990
3991
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003992// Generalize the representation of all DATA descriptors.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003993Handle<Map> Map::GeneralizeAllFieldRepresentations(
3994 Handle<Map> map) {
3995 Handle<DescriptorArray> descriptors(map->instance_descriptors());
3996 for (int i = 0; i < map->NumberOfOwnDescriptors(); ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003997 PropertyDetails details = descriptors->GetDetails(i);
3998 if (details.type() == DATA) {
3999 map = ReconfigureProperty(map, i, kData, details.attributes(),
4000 Representation::Tagged(),
Ben Murdoch097c5b22016-05-18 11:27:45 +01004001 FieldType::Any(map->GetIsolate()), FORCE_FIELD);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004002 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004003 }
4004 return map;
4005}
4006
4007
4008// static
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004009MaybeHandle<Map> Map::TryUpdate(Handle<Map> old_map) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004010 DisallowHeapAllocation no_allocation;
4011 DisallowDeoptimization no_deoptimization(old_map->GetIsolate());
4012
4013 if (!old_map->is_deprecated()) return old_map;
4014
4015 // Check the state of the root map.
4016 Map* root_map = old_map->FindRootMap();
4017 if (!old_map->EquivalentToForTransition(root_map)) return MaybeHandle<Map>();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004018
4019 ElementsKind from_kind = root_map->elements_kind();
4020 ElementsKind to_kind = old_map->elements_kind();
4021 if (from_kind != to_kind) {
4022 // Try to follow existing elements kind transitions.
4023 root_map = root_map->LookupElementsTransitionMap(to_kind);
4024 if (root_map == NULL) return MaybeHandle<Map>();
4025 // From here on, use the map with correct elements kind as root map.
4026 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004027 int root_nof = root_map->NumberOfOwnDescriptors();
4028
4029 int old_nof = old_map->NumberOfOwnDescriptors();
4030 DescriptorArray* old_descriptors = old_map->instance_descriptors();
4031
4032 Map* new_map = root_map;
4033 for (int i = root_nof; i < old_nof; ++i) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004034 PropertyDetails old_details = old_descriptors->GetDetails(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004035 Map* transition = TransitionArray::SearchTransition(
4036 new_map, old_details.kind(), old_descriptors->GetKey(i),
4037 old_details.attributes());
4038 if (transition == NULL) return MaybeHandle<Map>();
4039 new_map = transition;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004040 DescriptorArray* new_descriptors = new_map->instance_descriptors();
4041
4042 PropertyDetails new_details = new_descriptors->GetDetails(i);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004043 DCHECK_EQ(old_details.kind(), new_details.kind());
4044 DCHECK_EQ(old_details.attributes(), new_details.attributes());
4045 if (!old_details.representation().fits_into(new_details.representation())) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004046 return MaybeHandle<Map>();
4047 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004048 switch (new_details.type()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004049 case DATA: {
Ben Murdoch097c5b22016-05-18 11:27:45 +01004050 FieldType* new_type = new_descriptors->GetFieldType(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004051 // Cleared field types need special treatment. They represent lost
4052 // knowledge, so we must first generalize the new_type to "Any".
4053 if (FieldTypeIsCleared(new_details.representation(), new_type)) {
4054 return MaybeHandle<Map>();
4055 }
4056 PropertyType old_property_type = old_details.type();
4057 if (old_property_type == DATA) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01004058 FieldType* old_type = old_descriptors->GetFieldType(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004059 if (FieldTypeIsCleared(old_details.representation(), old_type) ||
4060 !old_type->NowIs(new_type)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004061 return MaybeHandle<Map>();
4062 }
4063 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004064 DCHECK(old_property_type == DATA_CONSTANT);
4065 Object* old_value = old_descriptors->GetValue(i);
4066 if (!new_type->NowContains(old_value)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004067 return MaybeHandle<Map>();
4068 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004069 }
4070 break;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004071 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004072 case ACCESSOR: {
4073#ifdef DEBUG
Ben Murdoch097c5b22016-05-18 11:27:45 +01004074 FieldType* new_type = new_descriptors->GetFieldType(i);
4075 DCHECK(new_type->IsAny());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004076#endif
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004077 break;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004078 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004079
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004080 case DATA_CONSTANT:
4081 case ACCESSOR_CONSTANT: {
4082 Object* old_value = old_descriptors->GetValue(i);
4083 Object* new_value = new_descriptors->GetValue(i);
4084 if (old_details.location() == kField || old_value != new_value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004085 return MaybeHandle<Map>();
4086 }
4087 break;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004088 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004089 }
4090 }
4091 if (new_map->NumberOfOwnDescriptors() != old_nof) return MaybeHandle<Map>();
4092 return handle(new_map);
4093}
4094
4095
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004096// static
4097Handle<Map> Map::Update(Handle<Map> map) {
4098 if (!map->is_deprecated()) return map;
4099 return ReconfigureProperty(map, -1, kData, NONE, Representation::None(),
Ben Murdoch097c5b22016-05-18 11:27:45 +01004100 FieldType::None(map->GetIsolate()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004101 ALLOW_IN_DESCRIPTOR);
4102}
4103
4104
4105Maybe<bool> JSObject::SetPropertyWithInterceptor(LookupIterator* it,
Ben Murdoch097c5b22016-05-18 11:27:45 +01004106 ShouldThrow should_throw,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004107 Handle<Object> value) {
4108 Isolate* isolate = it->isolate();
4109 // Make sure that the top context does not change when doing callbacks or
4110 // interceptor calls.
4111 AssertNoContextChange ncc(isolate);
4112
4113 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
4114 Handle<InterceptorInfo> interceptor(it->GetInterceptor());
4115 if (interceptor->setter()->IsUndefined()) return Just(false);
4116
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004117 Handle<JSObject> holder = it->GetHolder<JSObject>();
Ben Murdochda12d292016-06-02 14:46:10 +01004118 bool result;
4119 Handle<Object> receiver = it->GetReceiver();
4120 if (!receiver->IsJSReceiver()) {
4121 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, receiver,
4122 Object::ConvertReceiver(isolate, receiver),
4123 Nothing<bool>());
4124 }
4125 PropertyCallbackArguments args(isolate, interceptor->data(), *receiver,
4126 *holder, should_throw);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004127
4128 if (it->IsElement()) {
4129 uint32_t index = it->index();
4130 v8::IndexedPropertySetterCallback setter =
4131 v8::ToCData<v8::IndexedPropertySetterCallback>(interceptor->setter());
Ben Murdochda12d292016-06-02 14:46:10 +01004132 // TODO(neis): In the future, we may want to actually return the
4133 // interceptor's result, which then should be a boolean.
4134 result = !args.Call(setter, index, value).is_null();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004135 } else {
4136 Handle<Name> name = it->name();
4137 DCHECK(!name->IsPrivate());
4138
4139 if (name->IsSymbol() && !interceptor->can_intercept_symbols()) {
4140 return Just(false);
4141 }
4142
4143 v8::GenericNamedPropertySetterCallback setter =
4144 v8::ToCData<v8::GenericNamedPropertySetterCallback>(
4145 interceptor->setter());
Ben Murdochda12d292016-06-02 14:46:10 +01004146 result = !args.Call(setter, name, value).is_null();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004147 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004148
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004149 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(it->isolate(), Nothing<bool>());
Ben Murdochda12d292016-06-02 14:46:10 +01004150 return Just(result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004151}
4152
4153
4154MaybeHandle<Object> Object::SetProperty(Handle<Object> object,
4155 Handle<Name> name, Handle<Object> value,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004156 LanguageMode language_mode,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004157 StoreFromKeyed store_mode) {
4158 LookupIterator it(object, name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004159 MAYBE_RETURN_NULL(SetProperty(&it, value, language_mode, store_mode));
4160 return value;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004161}
4162
4163
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004164Maybe<bool> Object::SetPropertyInternal(LookupIterator* it,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004165 Handle<Object> value,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004166 LanguageMode language_mode,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004167 StoreFromKeyed store_mode,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004168 bool* found) {
Ben Murdochda12d292016-06-02 14:46:10 +01004169 DCHECK(it->IsFound());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004170 ShouldThrow should_throw =
4171 is_sloppy(language_mode) ? DONT_THROW : THROW_ON_ERROR;
4172
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004173 // Make sure that the top context does not change when doing callbacks or
4174 // interceptor calls.
4175 AssertNoContextChange ncc(it->isolate());
4176
Ben Murdochda12d292016-06-02 14:46:10 +01004177 do {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004178 switch (it->state()) {
4179 case LookupIterator::NOT_FOUND:
4180 UNREACHABLE();
4181
4182 case LookupIterator::ACCESS_CHECK:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004183 if (it->HasAccess()) break;
4184 // Check whether it makes sense to reuse the lookup iterator. Here it
4185 // might still call into setters up the prototype chain.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004186 return JSObject::SetPropertyWithFailedAccessCheck(it, value,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004187 should_throw);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004188
4189 case LookupIterator::JSPROXY:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004190 return JSProxy::SetProperty(it->GetHolder<JSProxy>(), it->GetName(),
4191 value, it->GetReceiver(), language_mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004192
4193 case LookupIterator::INTERCEPTOR:
4194 if (it->HolderIsReceiverOrHiddenPrototype()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01004195 Maybe<bool> result =
4196 JSObject::SetPropertyWithInterceptor(it, should_throw, value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004197 if (result.IsNothing() || result.FromJust()) return result;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004198 } else {
4199 Maybe<PropertyAttributes> maybe_attributes =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004200 JSObject::GetPropertyAttributesWithInterceptor(it);
4201 if (!maybe_attributes.IsJust()) return Nothing<bool>();
Ben Murdochda12d292016-06-02 14:46:10 +01004202 if (maybe_attributes.FromJust() == ABSENT) break;
4203 if ((maybe_attributes.FromJust() & READ_ONLY) != 0) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004204 return WriteToReadOnlyProperty(it, value, should_throw);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004205 }
Ben Murdochda12d292016-06-02 14:46:10 +01004206 *found = false;
4207 return Nothing<bool>();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004208 }
4209 break;
4210
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004211 case LookupIterator::ACCESSOR: {
4212 if (it->IsReadOnly()) {
4213 return WriteToReadOnlyProperty(it, value, should_throw);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004214 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004215 Handle<Object> accessors = it->GetAccessors();
4216 if (accessors->IsAccessorInfo() &&
4217 !it->HolderIsReceiverOrHiddenPrototype() &&
4218 AccessorInfo::cast(*accessors)->is_special_data_property()) {
Ben Murdochda12d292016-06-02 14:46:10 +01004219 *found = false;
4220 return Nothing<bool>();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004221 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004222 return SetPropertyWithAccessor(it, value, should_throw);
4223 }
4224 case LookupIterator::INTEGER_INDEXED_EXOTIC:
Ben Murdochda12d292016-06-02 14:46:10 +01004225 // TODO(verwaest): We should throw an exception if holder is receiver.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004226 return Just(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004227
4228 case LookupIterator::DATA:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004229 if (it->IsReadOnly()) {
4230 return WriteToReadOnlyProperty(it, value, should_throw);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004231 }
4232 if (it->HolderIsReceiverOrHiddenPrototype()) {
4233 return SetDataProperty(it, value);
4234 }
Ben Murdochda12d292016-06-02 14:46:10 +01004235 // Fall through.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004236 case LookupIterator::TRANSITION:
Ben Murdochda12d292016-06-02 14:46:10 +01004237 *found = false;
4238 return Nothing<bool>();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004239 }
Ben Murdochda12d292016-06-02 14:46:10 +01004240 it->Next();
4241 } while (it->IsFound());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004242
4243 *found = false;
4244 return Nothing<bool>();
4245}
4246
4247
4248Maybe<bool> Object::SetProperty(LookupIterator* it, Handle<Object> value,
4249 LanguageMode language_mode,
4250 StoreFromKeyed store_mode) {
Ben Murdochda12d292016-06-02 14:46:10 +01004251 it->UpdateProtector();
4252 if (it->IsFound()) {
4253 bool found = true;
4254 Maybe<bool> result =
4255 SetPropertyInternal(it, value, language_mode, store_mode, &found);
4256 if (found) return result;
4257 }
4258
4259 // If the receiver is the JSGlobalObject, the store was contextual. In case
4260 // the property did not exist yet on the global object itself, we have to
4261 // throw a reference error in strict mode. In sloppy mode, we continue.
4262 if (is_strict(language_mode) && it->GetReceiver()->IsJSGlobalObject()) {
4263 it->isolate()->Throw(*it->isolate()->factory()->NewReferenceError(
4264 MessageTemplate::kNotDefined, it->name()));
4265 return Nothing<bool>();
4266 }
4267
Ben Murdoch097c5b22016-05-18 11:27:45 +01004268 ShouldThrow should_throw =
4269 is_sloppy(language_mode) ? DONT_THROW : THROW_ON_ERROR;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004270 return AddDataProperty(it, value, NONE, should_throw, store_mode);
4271}
4272
4273
4274Maybe<bool> Object::SetSuperProperty(LookupIterator* it, Handle<Object> value,
4275 LanguageMode language_mode,
4276 StoreFromKeyed store_mode) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004277 Isolate* isolate = it->isolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004278
Ben Murdochda12d292016-06-02 14:46:10 +01004279 it->UpdateProtector();
4280 if (it->IsFound()) {
4281 bool found = true;
4282 Maybe<bool> result =
4283 SetPropertyInternal(it, value, language_mode, store_mode, &found);
4284 if (found) return result;
4285 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004286
4287 // The property either doesn't exist on the holder or exists there as a data
4288 // property.
4289
Ben Murdoch097c5b22016-05-18 11:27:45 +01004290 ShouldThrow should_throw =
4291 is_sloppy(language_mode) ? DONT_THROW : THROW_ON_ERROR;
4292
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004293 if (!it->GetReceiver()->IsJSReceiver()) {
4294 return WriteToReadOnlyProperty(it, value, should_throw);
4295 }
4296 Handle<JSReceiver> receiver = Handle<JSReceiver>::cast(it->GetReceiver());
4297
Ben Murdochda12d292016-06-02 14:46:10 +01004298 LookupIterator::Configuration c = LookupIterator::HIDDEN;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004299 LookupIterator own_lookup =
4300 it->IsElement() ? LookupIterator(isolate, receiver, it->index(), c)
4301 : LookupIterator(receiver, it->name(), c);
4302
4303 for (; own_lookup.IsFound(); own_lookup.Next()) {
4304 switch (own_lookup.state()) {
4305 case LookupIterator::ACCESS_CHECK:
4306 if (!own_lookup.HasAccess()) {
4307 return JSObject::SetPropertyWithFailedAccessCheck(&own_lookup, value,
4308 should_throw);
4309 }
4310 break;
4311
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004312 case LookupIterator::ACCESSOR:
Ben Murdoch097c5b22016-05-18 11:27:45 +01004313 if (own_lookup.GetAccessors()->IsAccessorInfo()) {
4314 if (own_lookup.IsReadOnly()) {
4315 return WriteToReadOnlyProperty(&own_lookup, value, should_throw);
4316 }
4317 return JSObject::SetPropertyWithAccessor(&own_lookup, value,
4318 should_throw);
4319 }
4320 // Fall through.
4321 case LookupIterator::INTEGER_INDEXED_EXOTIC:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004322 return RedefineIncompatibleProperty(isolate, it->GetName(), value,
4323 should_throw);
4324
4325 case LookupIterator::DATA: {
Ben Murdoch097c5b22016-05-18 11:27:45 +01004326 if (own_lookup.IsReadOnly()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004327 return WriteToReadOnlyProperty(&own_lookup, value, should_throw);
4328 }
4329 return SetDataProperty(&own_lookup, value);
4330 }
4331
4332 case LookupIterator::INTERCEPTOR:
4333 case LookupIterator::JSPROXY: {
4334 PropertyDescriptor desc;
4335 Maybe<bool> owned =
4336 JSReceiver::GetOwnPropertyDescriptor(&own_lookup, &desc);
4337 MAYBE_RETURN(owned, Nothing<bool>());
4338 if (!owned.FromJust()) {
4339 return JSReceiver::CreateDataProperty(&own_lookup, value,
4340 should_throw);
4341 }
4342 if (PropertyDescriptor::IsAccessorDescriptor(&desc) ||
4343 !desc.writable()) {
4344 return RedefineIncompatibleProperty(isolate, it->GetName(), value,
4345 should_throw);
4346 }
4347
4348 PropertyDescriptor value_desc;
4349 value_desc.set_value(value);
4350 return JSReceiver::DefineOwnProperty(isolate, receiver, it->GetName(),
4351 &value_desc, should_throw);
4352 }
4353
4354 case LookupIterator::NOT_FOUND:
4355 case LookupIterator::TRANSITION:
4356 UNREACHABLE();
4357 }
4358 }
4359
Ben Murdochda12d292016-06-02 14:46:10 +01004360 return AddDataProperty(&own_lookup, value, NONE, should_throw, store_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004361}
4362
Ben Murdoch097c5b22016-05-18 11:27:45 +01004363MaybeHandle<Object> Object::ReadAbsentProperty(LookupIterator* it) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004364 return it->isolate()->factory()->undefined_value();
4365}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004366
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004367MaybeHandle<Object> Object::ReadAbsentProperty(Isolate* isolate,
4368 Handle<Object> receiver,
Ben Murdoch097c5b22016-05-18 11:27:45 +01004369 Handle<Object> name) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004370 return isolate->factory()->undefined_value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004371}
4372
4373
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004374Maybe<bool> Object::CannotCreateProperty(Isolate* isolate,
4375 Handle<Object> receiver,
4376 Handle<Object> name,
4377 Handle<Object> value,
4378 ShouldThrow should_throw) {
4379 RETURN_FAILURE(
4380 isolate, should_throw,
4381 NewTypeError(MessageTemplate::kStrictCannotCreateProperty, name,
4382 Object::TypeOf(isolate, receiver), receiver));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004383}
4384
4385
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004386Maybe<bool> Object::WriteToReadOnlyProperty(LookupIterator* it,
4387 Handle<Object> value,
4388 ShouldThrow should_throw) {
4389 return WriteToReadOnlyProperty(it->isolate(), it->GetReceiver(),
4390 it->GetName(), value, should_throw);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004391}
4392
4393
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004394Maybe<bool> Object::WriteToReadOnlyProperty(Isolate* isolate,
4395 Handle<Object> receiver,
4396 Handle<Object> name,
4397 Handle<Object> value,
4398 ShouldThrow should_throw) {
4399 RETURN_FAILURE(isolate, should_throw,
4400 NewTypeError(MessageTemplate::kStrictReadOnlyProperty, name,
4401 Object::TypeOf(isolate, receiver), receiver));
4402}
4403
4404
4405Maybe<bool> Object::RedefineIncompatibleProperty(Isolate* isolate,
4406 Handle<Object> name,
4407 Handle<Object> value,
4408 ShouldThrow should_throw) {
4409 RETURN_FAILURE(isolate, should_throw,
4410 NewTypeError(MessageTemplate::kRedefineDisallowed, name));
4411}
4412
4413
4414Maybe<bool> Object::SetDataProperty(LookupIterator* it, Handle<Object> value) {
4415 // Proxies are handled elsewhere. Other non-JSObjects cannot have own
4416 // properties.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004417 Handle<JSObject> receiver = Handle<JSObject>::cast(it->GetReceiver());
4418
4419 // Store on the holder which may be hidden behind the receiver.
4420 DCHECK(it->HolderIsReceiverOrHiddenPrototype());
4421
4422 // Old value for the observation change record.
4423 // Fetch before transforming the object since the encoding may become
4424 // incompatible with what's cached in |it|.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004425 bool is_observed = receiver->map()->is_observed() &&
Ben Murdoch097c5b22016-05-18 11:27:45 +01004426 (it->IsElement() || !it->name()->IsPrivate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004427 MaybeHandle<Object> maybe_old;
4428 if (is_observed) maybe_old = it->GetDataValue();
4429
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004430 Handle<Object> to_assign = value;
4431 // Convert the incoming value to a number for storing into typed arrays.
4432 if (it->IsElement() && receiver->HasFixedTypedArrayElements()) {
4433 if (!value->IsNumber() && !value->IsUndefined()) {
4434 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
4435 it->isolate(), to_assign, Object::ToNumber(value), Nothing<bool>());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004436 // We have to recheck the length. However, it can only change if the
4437 // underlying buffer was neutered, so just check that.
4438 if (Handle<JSArrayBufferView>::cast(receiver)->WasNeutered()) {
4439 return Just(true);
4440 // TODO(neis): According to the spec, this should throw a TypeError.
4441 }
4442 }
4443 }
4444
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004445 // Possibly migrate to the most up-to-date map that will be able to store
4446 // |value| under it->name().
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004447 it->PrepareForDataProperty(to_assign);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004448
4449 // Write the property value.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004450 it->WriteDataValue(to_assign);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004451
4452 // Send the change record if there are observers.
4453 if (is_observed && !value->SameValue(*maybe_old.ToHandleChecked())) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004454 RETURN_ON_EXCEPTION_VALUE(
4455 it->isolate(),
4456 JSObject::EnqueueChangeRecord(receiver, "update", it->GetName(),
4457 maybe_old.ToHandleChecked()),
4458 Nothing<bool>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004459 }
4460
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004461#if VERIFY_HEAP
4462 if (FLAG_verify_heap) {
4463 receiver->JSObjectVerify();
4464 }
4465#endif
4466 return Just(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004467}
4468
4469
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004470MUST_USE_RESULT static MaybeHandle<Object> BeginPerformSplice(
4471 Handle<JSArray> object) {
4472 Isolate* isolate = object->GetIsolate();
4473 HandleScope scope(isolate);
4474 Handle<Object> args[] = {object};
4475
4476 return Execution::Call(
4477 isolate, Handle<JSFunction>(isolate->observers_begin_perform_splice()),
4478 isolate->factory()->undefined_value(), arraysize(args), args);
4479}
4480
4481
4482MUST_USE_RESULT static MaybeHandle<Object> EndPerformSplice(
4483 Handle<JSArray> object) {
4484 Isolate* isolate = object->GetIsolate();
4485 HandleScope scope(isolate);
4486 Handle<Object> args[] = {object};
4487
4488 return Execution::Call(
4489 isolate, Handle<JSFunction>(isolate->observers_end_perform_splice()),
4490 isolate->factory()->undefined_value(), arraysize(args), args);
4491}
4492
4493
4494MUST_USE_RESULT static MaybeHandle<Object> EnqueueSpliceRecord(
4495 Handle<JSArray> object, uint32_t index, Handle<JSArray> deleted,
4496 uint32_t add_count) {
4497 Isolate* isolate = object->GetIsolate();
4498 HandleScope scope(isolate);
4499 Handle<Object> index_object = isolate->factory()->NewNumberFromUint(index);
4500 Handle<Object> add_count_object =
4501 isolate->factory()->NewNumberFromUint(add_count);
4502
4503 Handle<Object> args[] = {object, index_object, deleted, add_count_object};
4504
4505 return Execution::Call(
4506 isolate, Handle<JSFunction>(isolate->observers_enqueue_splice()),
4507 isolate->factory()->undefined_value(), arraysize(args), args);
4508}
4509
4510
4511Maybe<bool> Object::AddDataProperty(LookupIterator* it, Handle<Object> value,
4512 PropertyAttributes attributes,
4513 ShouldThrow should_throw,
4514 StoreFromKeyed store_mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004515 if (!it->GetReceiver()->IsJSObject()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01004516 if (it->GetReceiver()->IsJSProxy() && it->GetName()->IsPrivate()) {
4517 RETURN_FAILURE(it->isolate(), should_throw,
4518 NewTypeError(MessageTemplate::kProxyPrivate));
4519 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004520 return CannotCreateProperty(it->isolate(), it->GetReceiver(), it->GetName(),
4521 value, should_throw);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004522 }
4523
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004524 DCHECK_NE(LookupIterator::INTEGER_INDEXED_EXOTIC, it->state());
4525
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004526 Handle<JSObject> receiver = it->GetStoreTarget();
4527
4528 // If the receiver is a JSGlobalProxy, store on the prototype (JSGlobalObject)
4529 // instead. If the prototype is Null, the proxy is detached.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004530 if (receiver->IsJSGlobalProxy()) return Just(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004531
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004532 Isolate* isolate = it->isolate();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004533
Ben Murdoch097c5b22016-05-18 11:27:45 +01004534 if (it->ExtendingNonExtensible(receiver)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004535 RETURN_FAILURE(
4536 isolate, should_throw,
4537 NewTypeError(MessageTemplate::kObjectNotExtensible, it->GetName()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004538 }
4539
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004540 if (it->IsElement()) {
4541 if (receiver->IsJSArray()) {
4542 Handle<JSArray> array = Handle<JSArray>::cast(receiver);
4543 if (JSArray::WouldChangeReadOnlyLength(array, it->index())) {
4544 RETURN_FAILURE(array->GetIsolate(), should_throw,
4545 NewTypeError(MessageTemplate::kStrictReadOnlyProperty,
4546 isolate->factory()->length_string(),
4547 Object::TypeOf(isolate, array), array));
4548 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004549
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004550 if (FLAG_trace_external_array_abuse &&
4551 array->HasFixedTypedArrayElements()) {
4552 CheckArrayAbuse(array, "typed elements write", it->index(), true);
4553 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004554
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004555 if (FLAG_trace_js_array_abuse && !array->HasFixedTypedArrayElements()) {
4556 CheckArrayAbuse(array, "elements write", it->index(), false);
Steve Blocka7e24c12009-10-30 11:49:00 +00004557 }
4558 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004559
4560 Maybe<bool> result = JSObject::AddDataElement(receiver, it->index(), value,
4561 attributes, should_throw);
4562 JSObject::ValidateElements(receiver);
4563 return result;
4564 } else {
4565 // Migrate to the most up-to-date map that will be able to store |value|
4566 // under it->name() with |attributes|.
Ben Murdoch097c5b22016-05-18 11:27:45 +01004567 it->PrepareTransitionToDataProperty(receiver, value, attributes,
4568 store_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004569 DCHECK_EQ(LookupIterator::TRANSITION, it->state());
Ben Murdoch097c5b22016-05-18 11:27:45 +01004570 it->ApplyTransitionToDataProperty(receiver);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004571
4572 // TODO(verwaest): Encapsulate dictionary handling better.
4573 if (receiver->map()->is_dictionary_map()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004574 // TODO(dcarney): just populate TransitionPropertyCell here?
4575 JSObject::AddSlowProperty(receiver, it->name(), value, attributes);
4576 } else {
4577 // Write the property value.
4578 it->WriteDataValue(value);
4579 }
4580
4581 // Send the change record if there are observers.
Ben Murdoch097c5b22016-05-18 11:27:45 +01004582 if (receiver->map()->is_observed() && !it->name()->IsPrivate()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004583 RETURN_ON_EXCEPTION_VALUE(isolate, JSObject::EnqueueChangeRecord(
4584 receiver, "add", it->name(),
4585 it->factory()->the_hole_value()),
4586 Nothing<bool>());
4587 }
4588#if VERIFY_HEAP
4589 if (FLAG_verify_heap) {
4590 receiver->JSObjectVerify();
4591 }
4592#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004593 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004594
4595 return Just(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00004596}
4597
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004598
4599void Map::EnsureDescriptorSlack(Handle<Map> map, int slack) {
4600 // Only supports adding slack to owned descriptors.
4601 DCHECK(map->owns_descriptors());
4602
4603 Handle<DescriptorArray> descriptors(map->instance_descriptors());
4604 int old_size = map->NumberOfOwnDescriptors();
4605 if (slack <= descriptors->NumberOfSlackDescriptors()) return;
4606
4607 Handle<DescriptorArray> new_descriptors = DescriptorArray::CopyUpTo(
4608 descriptors, old_size, slack);
4609
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004610 DisallowHeapAllocation no_allocation;
4611 // The descriptors are still the same, so keep the layout descriptor.
4612 LayoutDescriptor* layout_descriptor = map->GetLayoutDescriptor();
4613
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004614 if (old_size == 0) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004615 map->UpdateDescriptors(*new_descriptors, layout_descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004616 return;
4617 }
4618
4619 // If the source descriptors had an enum cache we copy it. This ensures
4620 // that the maps to which we push the new descriptor array back can rely
4621 // on a cache always being available once it is set. If the map has more
4622 // enumerated descriptors than available in the original cache, the cache
4623 // will be lazily replaced by the extended cache when needed.
4624 if (descriptors->HasEnumCache()) {
4625 new_descriptors->CopyEnumCacheFrom(*descriptors);
4626 }
4627
4628 // Replace descriptors by new_descriptors in all maps that share it.
Ben Murdochda12d292016-06-02 14:46:10 +01004629 map->GetHeap()->incremental_marking()->IterateBlackObject(*descriptors);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004630
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004631 Map* current = *map;
4632 while (current->instance_descriptors() == *descriptors) {
4633 Object* next = current->GetBackPointer();
4634 if (next->IsUndefined()) break; // Stop overwriting at initial map.
4635 current->UpdateDescriptors(*new_descriptors, layout_descriptor);
4636 current = Map::cast(next);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004637 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004638 map->UpdateDescriptors(*new_descriptors, layout_descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004639}
4640
4641
4642template<class T>
4643static int AppendUniqueCallbacks(NeanderArray* callbacks,
4644 Handle<typename T::Array> array,
4645 int valid_descriptors) {
4646 int nof_callbacks = callbacks->length();
4647
4648 Isolate* isolate = array->GetIsolate();
4649 // Ensure the keys are unique names before writing them into the
4650 // instance descriptor. Since it may cause a GC, it has to be done before we
4651 // temporarily put the heap in an invalid state while appending descriptors.
4652 for (int i = 0; i < nof_callbacks; ++i) {
4653 Handle<AccessorInfo> entry(AccessorInfo::cast(callbacks->get(i)));
4654 if (entry->name()->IsUniqueName()) continue;
4655 Handle<String> key =
4656 isolate->factory()->InternalizeString(
4657 Handle<String>(String::cast(entry->name())));
4658 entry->set_name(*key);
4659 }
4660
4661 // Fill in new callback descriptors. Process the callbacks from
4662 // back to front so that the last callback with a given name takes
4663 // precedence over previously added callbacks with that name.
4664 for (int i = nof_callbacks - 1; i >= 0; i--) {
4665 Handle<AccessorInfo> entry(AccessorInfo::cast(callbacks->get(i)));
4666 Handle<Name> key(Name::cast(entry->name()));
4667 // Check if a descriptor with this name already exists before writing.
4668 if (!T::Contains(key, entry, valid_descriptors, array)) {
4669 T::Insert(key, entry, valid_descriptors, array);
4670 valid_descriptors++;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004671 }
4672 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004673
4674 return valid_descriptors;
4675}
4676
4677struct DescriptorArrayAppender {
4678 typedef DescriptorArray Array;
4679 static bool Contains(Handle<Name> key,
4680 Handle<AccessorInfo> entry,
4681 int valid_descriptors,
4682 Handle<DescriptorArray> array) {
4683 DisallowHeapAllocation no_gc;
4684 return array->Search(*key, valid_descriptors) != DescriptorArray::kNotFound;
4685 }
4686 static void Insert(Handle<Name> key,
4687 Handle<AccessorInfo> entry,
4688 int valid_descriptors,
4689 Handle<DescriptorArray> array) {
4690 DisallowHeapAllocation no_gc;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004691 AccessorConstantDescriptor desc(key, entry, entry->property_attributes());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004692 array->Append(&desc);
4693 }
4694};
4695
4696
4697struct FixedArrayAppender {
4698 typedef FixedArray Array;
4699 static bool Contains(Handle<Name> key,
4700 Handle<AccessorInfo> entry,
4701 int valid_descriptors,
4702 Handle<FixedArray> array) {
4703 for (int i = 0; i < valid_descriptors; i++) {
4704 if (*key == AccessorInfo::cast(array->get(i))->name()) return true;
4705 }
4706 return false;
4707 }
4708 static void Insert(Handle<Name> key,
4709 Handle<AccessorInfo> entry,
4710 int valid_descriptors,
4711 Handle<FixedArray> array) {
4712 DisallowHeapAllocation no_gc;
4713 array->set(valid_descriptors, *entry);
4714 }
4715};
4716
4717
4718void Map::AppendCallbackDescriptors(Handle<Map> map,
4719 Handle<Object> descriptors) {
4720 int nof = map->NumberOfOwnDescriptors();
4721 Handle<DescriptorArray> array(map->instance_descriptors());
4722 NeanderArray callbacks(descriptors);
4723 DCHECK(array->NumberOfSlackDescriptors() >= callbacks.length());
4724 nof = AppendUniqueCallbacks<DescriptorArrayAppender>(&callbacks, array, nof);
4725 map->SetNumberOfOwnDescriptors(nof);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004726}
4727
Steve Blocka7e24c12009-10-30 11:49:00 +00004728
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004729int AccessorInfo::AppendUnique(Handle<Object> descriptors,
4730 Handle<FixedArray> array,
4731 int valid_descriptors) {
4732 NeanderArray callbacks(descriptors);
4733 DCHECK(array->length() >= callbacks.length() + valid_descriptors);
4734 return AppendUniqueCallbacks<FixedArrayAppender>(&callbacks,
4735 array,
4736 valid_descriptors);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004737}
4738
4739
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004740static bool ContainsMap(MapHandleList* maps, Map* map) {
4741 DCHECK_NOT_NULL(map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004742 for (int i = 0; i < maps->length(); ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004743 if (!maps->at(i).is_null() && *maps->at(i) == map) return true;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004744 }
4745 return false;
4746}
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004747
Ben Murdoch85b71792012-04-11 18:30:58 +01004748
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004749Handle<Map> Map::FindTransitionedMap(Handle<Map> map,
4750 MapHandleList* candidates) {
4751 ElementsKind kind = map->elements_kind();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004752 bool packed = IsFastPackedElementsKind(kind);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004753
4754 Map* transition = nullptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004755 if (IsTransitionableFastElementsKind(kind)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004756 for (Map* current = map->ElementsTransitionMap();
4757 current != nullptr && current->has_fast_elements();
4758 current = current->ElementsTransitionMap()) {
4759 if (ContainsMap(candidates, current) &&
4760 (packed || !IsFastPackedElementsKind(current->elements_kind()))) {
4761 transition = current;
4762 packed = packed && IsFastPackedElementsKind(current->elements_kind());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004763 }
Ben Murdoch85b71792012-04-11 18:30:58 +01004764 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004765 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004766 return transition == nullptr ? Handle<Map>() : handle(transition);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004767}
Ben Murdoch85b71792012-04-11 18:30:58 +01004768
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004769
4770static Map* FindClosestElementsTransition(Map* map, ElementsKind to_kind) {
4771 Map* current_map = map;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004772
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004773 ElementsKind kind = map->elements_kind();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004774 while (kind != to_kind) {
4775 Map* next_map = current_map->ElementsTransitionMap();
4776 if (next_map == nullptr) return current_map;
4777 kind = next_map->elements_kind();
4778 current_map = next_map;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004779 }
4780
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004781 DCHECK_EQ(to_kind, current_map->elements_kind());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004782 return current_map;
4783}
4784
4785
4786Map* Map::LookupElementsTransitionMap(ElementsKind to_kind) {
4787 Map* to_map = FindClosestElementsTransition(this, to_kind);
4788 if (to_map->elements_kind() == to_kind) return to_map;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004789 return nullptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004790}
4791
4792
4793bool Map::IsMapInArrayPrototypeChain() {
4794 Isolate* isolate = GetIsolate();
4795 if (isolate->initial_array_prototype()->map() == this) {
4796 return true;
4797 }
4798
4799 if (isolate->initial_object_prototype()->map() == this) {
4800 return true;
4801 }
4802
4803 return false;
4804}
4805
4806
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004807Handle<WeakCell> Map::WeakCellForMap(Handle<Map> map) {
4808 Isolate* isolate = map->GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004809 if (map->weak_cell_cache()->IsWeakCell()) {
4810 return Handle<WeakCell>(WeakCell::cast(map->weak_cell_cache()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004811 }
4812 Handle<WeakCell> weak_cell = isolate->factory()->NewWeakCell(map);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004813 map->set_weak_cell_cache(*weak_cell);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004814 return weak_cell;
4815}
4816
4817
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004818static Handle<Map> AddMissingElementsTransitions(Handle<Map> map,
4819 ElementsKind to_kind) {
4820 DCHECK(IsTransitionElementsKind(map->elements_kind()));
4821
4822 Handle<Map> current_map = map;
4823
4824 ElementsKind kind = map->elements_kind();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004825 TransitionFlag flag;
4826 if (map->is_prototype_map()) {
4827 flag = OMIT_TRANSITION;
4828 } else {
4829 flag = INSERT_TRANSITION;
4830 if (IsFastElementsKind(kind)) {
4831 while (kind != to_kind && !IsTerminalElementsKind(kind)) {
4832 kind = GetNextTransitionElementsKind(kind);
4833 current_map = Map::CopyAsElementsKind(current_map, kind, flag);
4834 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004835 }
4836 }
4837
4838 // In case we are exiting the fast elements kind system, just add the map in
4839 // the end.
4840 if (kind != to_kind) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004841 current_map = Map::CopyAsElementsKind(current_map, to_kind, flag);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004842 }
4843
4844 DCHECK(current_map->elements_kind() == to_kind);
4845 return current_map;
4846}
4847
4848
4849Handle<Map> Map::TransitionElementsTo(Handle<Map> map,
4850 ElementsKind to_kind) {
4851 ElementsKind from_kind = map->elements_kind();
4852 if (from_kind == to_kind) return map;
4853
4854 Isolate* isolate = map->GetIsolate();
4855 Context* native_context = isolate->context()->native_context();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004856 if (from_kind == FAST_SLOPPY_ARGUMENTS_ELEMENTS) {
4857 if (*map == native_context->fast_aliased_arguments_map()) {
4858 DCHECK_EQ(SLOW_SLOPPY_ARGUMENTS_ELEMENTS, to_kind);
4859 return handle(native_context->slow_aliased_arguments_map());
4860 }
4861 } else if (from_kind == SLOW_SLOPPY_ARGUMENTS_ELEMENTS) {
4862 if (*map == native_context->slow_aliased_arguments_map()) {
4863 DCHECK_EQ(FAST_SLOPPY_ARGUMENTS_ELEMENTS, to_kind);
4864 return handle(native_context->fast_aliased_arguments_map());
4865 }
4866 } else if (IsFastElementsKind(from_kind) && IsFastElementsKind(to_kind)) {
4867 // Reuse map transitions for JSArrays.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004868 DisallowHeapAllocation no_gc;
Ben Murdochda12d292016-06-02 14:46:10 +01004869 if (native_context->get(Context::ArrayMapIndex(from_kind)) == *map) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004870 Object* maybe_transitioned_map =
Ben Murdochda12d292016-06-02 14:46:10 +01004871 native_context->get(Context::ArrayMapIndex(to_kind));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004872 if (maybe_transitioned_map->IsMap()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004873 return handle(Map::cast(maybe_transitioned_map), isolate);
Ben Murdoch85b71792012-04-11 18:30:58 +01004874 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004875 }
4876 }
4877
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004878 DCHECK(!map->IsUndefined());
4879 // Check if we can go back in the elements kind transition chain.
4880 if (IsHoleyElementsKind(from_kind) &&
4881 to_kind == GetPackedElementsKind(from_kind) &&
4882 map->GetBackPointer()->IsMap() &&
4883 Map::cast(map->GetBackPointer())->elements_kind() == to_kind) {
4884 return handle(Map::cast(map->GetBackPointer()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004885 }
4886
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004887 bool allow_store_transition = IsTransitionElementsKind(from_kind);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004888 // Only store fast element maps in ascending generality.
4889 if (IsFastElementsKind(to_kind)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004890 allow_store_transition =
4891 allow_store_transition && IsTransitionableFastElementsKind(from_kind) &&
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004892 IsMoreGeneralElementsKindTransition(from_kind, to_kind);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004893 }
4894
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004895 if (!allow_store_transition) {
4896 return Map::CopyAsElementsKind(map, to_kind, OMIT_TRANSITION);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004897 }
4898
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004899 return Map::AsElementsKind(map, to_kind);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004900}
4901
4902
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004903// static
4904Handle<Map> Map::AsElementsKind(Handle<Map> map, ElementsKind kind) {
4905 Handle<Map> closest_map(FindClosestElementsTransition(*map, kind));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004906
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004907 if (closest_map->elements_kind() == kind) {
4908 return closest_map;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004909 }
4910
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004911 return AddMissingElementsTransitions(closest_map, kind);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004912}
4913
4914
4915Handle<Map> JSObject::GetElementsTransitionMap(Handle<JSObject> object,
4916 ElementsKind to_kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004917 Handle<Map> map(object->map());
4918 return Map::TransitionElementsTo(map, to_kind);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004919}
4920
4921
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004922void JSProxy::Revoke(Handle<JSProxy> proxy) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004923 Isolate* isolate = proxy->GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004924 if (!proxy->IsRevoked()) proxy->set_handler(isolate->heap()->null_value());
4925 DCHECK(proxy->IsRevoked());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004926}
4927
4928
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004929Maybe<bool> JSProxy::HasProperty(Isolate* isolate, Handle<JSProxy> proxy,
4930 Handle<Name> name) {
4931 DCHECK(!name->IsPrivate());
4932 STACK_CHECK(Nothing<bool>());
4933 // 1. (Assert)
4934 // 2. Let handler be the value of the [[ProxyHandler]] internal slot of O.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004935 Handle<Object> handler(proxy->handler(), isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004936 // 3. If handler is null, throw a TypeError exception.
4937 // 4. Assert: Type(handler) is Object.
4938 if (proxy->IsRevoked()) {
4939 isolate->Throw(*isolate->factory()->NewTypeError(
4940 MessageTemplate::kProxyRevoked, isolate->factory()->has_string()));
4941 return Nothing<bool>();
4942 }
4943 // 5. Let target be the value of the [[ProxyTarget]] internal slot of O.
4944 Handle<JSReceiver> target(proxy->target(), isolate);
4945 // 6. Let trap be ? GetMethod(handler, "has").
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004946 Handle<Object> trap;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004947 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
4948 isolate, trap, Object::GetMethod(Handle<JSReceiver>::cast(handler),
4949 isolate->factory()->has_string()),
4950 Nothing<bool>());
4951 // 7. If trap is undefined, then
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004952 if (trap->IsUndefined()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004953 // 7a. Return target.[[HasProperty]](P).
4954 return JSReceiver::HasProperty(target, name);
4955 }
4956 // 8. Let booleanTrapResult be ToBoolean(? Call(trap, handler, «target, P»)).
4957 Handle<Object> trap_result_obj;
4958 Handle<Object> args[] = {target, name};
4959 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
4960 isolate, trap_result_obj,
4961 Execution::Call(isolate, trap, handler, arraysize(args), args),
4962 Nothing<bool>());
4963 bool boolean_trap_result = trap_result_obj->BooleanValue();
4964 // 9. If booleanTrapResult is false, then:
4965 if (!boolean_trap_result) {
4966 // 9a. Let targetDesc be ? target.[[GetOwnProperty]](P).
4967 PropertyDescriptor target_desc;
4968 Maybe<bool> target_found = JSReceiver::GetOwnPropertyDescriptor(
4969 isolate, target, name, &target_desc);
4970 MAYBE_RETURN(target_found, Nothing<bool>());
4971 // 9b. If targetDesc is not undefined, then:
4972 if (target_found.FromJust()) {
4973 // 9b i. If targetDesc.[[Configurable]] is false, throw a TypeError
4974 // exception.
4975 if (!target_desc.configurable()) {
4976 isolate->Throw(*isolate->factory()->NewTypeError(
4977 MessageTemplate::kProxyHasNonConfigurable, name));
4978 return Nothing<bool>();
4979 }
4980 // 9b ii. Let extensibleTarget be ? IsExtensible(target).
4981 Maybe<bool> extensible_target = JSReceiver::IsExtensible(target);
4982 MAYBE_RETURN(extensible_target, Nothing<bool>());
4983 // 9b iii. If extensibleTarget is false, throw a TypeError exception.
4984 if (!extensible_target.FromJust()) {
4985 isolate->Throw(*isolate->factory()->NewTypeError(
4986 MessageTemplate::kProxyHasNonExtensible, name));
4987 return Nothing<bool>();
4988 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004989 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004990 }
4991 // 10. Return booleanTrapResult.
4992 return Just(boolean_trap_result);
4993}
4994
4995
4996Maybe<bool> JSProxy::SetProperty(Handle<JSProxy> proxy, Handle<Name> name,
4997 Handle<Object> value, Handle<Object> receiver,
4998 LanguageMode language_mode) {
4999 DCHECK(!name->IsPrivate());
5000 Isolate* isolate = proxy->GetIsolate();
5001 STACK_CHECK(Nothing<bool>());
5002 Factory* factory = isolate->factory();
5003 Handle<String> trap_name = factory->set_string();
5004 ShouldThrow should_throw =
5005 is_sloppy(language_mode) ? DONT_THROW : THROW_ON_ERROR;
5006
5007 if (proxy->IsRevoked()) {
5008 isolate->Throw(
5009 *factory->NewTypeError(MessageTemplate::kProxyRevoked, trap_name));
5010 return Nothing<bool>();
5011 }
5012 Handle<JSReceiver> target(proxy->target(), isolate);
5013 Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate);
5014
5015 Handle<Object> trap;
5016 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
5017 isolate, trap, Object::GetMethod(handler, trap_name), Nothing<bool>());
5018 if (trap->IsUndefined()) {
5019 LookupIterator it =
5020 LookupIterator::PropertyOrElement(isolate, receiver, name, target);
5021 return Object::SetSuperProperty(&it, value, language_mode,
5022 Object::MAY_BE_STORE_FROM_KEYED);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005023 }
5024
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005025 Handle<Object> trap_result;
5026 Handle<Object> args[] = {target, name, value, receiver};
5027 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
5028 isolate, trap_result,
5029 Execution::Call(isolate, trap, handler, arraysize(args), args),
5030 Nothing<bool>());
5031 if (!trap_result->BooleanValue()) {
5032 RETURN_FAILURE(isolate, should_throw,
5033 NewTypeError(MessageTemplate::kProxyTrapReturnedFalsishFor,
5034 trap_name, name));
5035 }
5036
5037 // Enforce the invariant.
5038 PropertyDescriptor target_desc;
5039 Maybe<bool> owned =
5040 JSReceiver::GetOwnPropertyDescriptor(isolate, target, name, &target_desc);
5041 MAYBE_RETURN(owned, Nothing<bool>());
5042 if (owned.FromJust()) {
5043 bool inconsistent = PropertyDescriptor::IsDataDescriptor(&target_desc) &&
5044 !target_desc.configurable() &&
5045 !target_desc.writable() &&
5046 !value->SameValue(*target_desc.value());
5047 if (inconsistent) {
5048 isolate->Throw(*isolate->factory()->NewTypeError(
5049 MessageTemplate::kProxySetFrozenData, name));
5050 return Nothing<bool>();
5051 }
5052 inconsistent = PropertyDescriptor::IsAccessorDescriptor(&target_desc) &&
5053 !target_desc.configurable() &&
5054 target_desc.set()->IsUndefined();
5055 if (inconsistent) {
5056 isolate->Throw(*isolate->factory()->NewTypeError(
5057 MessageTemplate::kProxySetFrozenAccessor, name));
5058 return Nothing<bool>();
5059 }
5060 }
5061 return Just(true);
5062}
5063
5064
5065Maybe<bool> JSProxy::DeletePropertyOrElement(Handle<JSProxy> proxy,
5066 Handle<Name> name,
5067 LanguageMode language_mode) {
5068 DCHECK(!name->IsPrivate());
5069 ShouldThrow should_throw =
5070 is_sloppy(language_mode) ? DONT_THROW : THROW_ON_ERROR;
5071 Isolate* isolate = proxy->GetIsolate();
5072 STACK_CHECK(Nothing<bool>());
5073 Factory* factory = isolate->factory();
5074 Handle<String> trap_name = factory->deleteProperty_string();
5075
5076 if (proxy->IsRevoked()) {
5077 isolate->Throw(
5078 *factory->NewTypeError(MessageTemplate::kProxyRevoked, trap_name));
5079 return Nothing<bool>();
5080 }
5081 Handle<JSReceiver> target(proxy->target(), isolate);
5082 Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate);
5083
5084 Handle<Object> trap;
5085 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
5086 isolate, trap, Object::GetMethod(handler, trap_name), Nothing<bool>());
5087 if (trap->IsUndefined()) {
5088 return JSReceiver::DeletePropertyOrElement(target, name, language_mode);
5089 }
5090
5091 Handle<Object> trap_result;
5092 Handle<Object> args[] = {target, name};
5093 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
5094 isolate, trap_result,
5095 Execution::Call(isolate, trap, handler, arraysize(args), args),
5096 Nothing<bool>());
5097 if (!trap_result->BooleanValue()) {
5098 RETURN_FAILURE(isolate, should_throw,
5099 NewTypeError(MessageTemplate::kProxyTrapReturnedFalsishFor,
5100 trap_name, name));
5101 }
5102
5103 // Enforce the invariant.
5104 PropertyDescriptor target_desc;
5105 Maybe<bool> owned =
5106 JSReceiver::GetOwnPropertyDescriptor(isolate, target, name, &target_desc);
5107 MAYBE_RETURN(owned, Nothing<bool>());
5108 if (owned.FromJust() && !target_desc.configurable()) {
5109 isolate->Throw(*factory->NewTypeError(
5110 MessageTemplate::kProxyDeletePropertyNonConfigurable, name));
5111 return Nothing<bool>();
5112 }
5113 return Just(true);
5114}
5115
5116
5117// static
5118MaybeHandle<JSProxy> JSProxy::New(Isolate* isolate, Handle<Object> target,
5119 Handle<Object> handler) {
5120 if (!target->IsJSReceiver()) {
5121 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kProxyNonObject),
5122 JSProxy);
5123 }
5124 if (target->IsJSProxy() && JSProxy::cast(*target)->IsRevoked()) {
5125 THROW_NEW_ERROR(isolate,
5126 NewTypeError(MessageTemplate::kProxyHandlerOrTargetRevoked),
5127 JSProxy);
5128 }
5129 if (!handler->IsJSReceiver()) {
5130 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kProxyNonObject),
5131 JSProxy);
5132 }
5133 if (handler->IsJSProxy() && JSProxy::cast(*handler)->IsRevoked()) {
5134 THROW_NEW_ERROR(isolate,
5135 NewTypeError(MessageTemplate::kProxyHandlerOrTargetRevoked),
5136 JSProxy);
5137 }
5138 return isolate->factory()->NewJSProxy(Handle<JSReceiver>::cast(target),
5139 Handle<JSReceiver>::cast(handler));
5140}
5141
5142
5143// static
5144MaybeHandle<Context> JSProxy::GetFunctionRealm(Handle<JSProxy> proxy) {
5145 DCHECK(proxy->map()->is_constructor());
5146 if (proxy->IsRevoked()) {
5147 THROW_NEW_ERROR(proxy->GetIsolate(),
5148 NewTypeError(MessageTemplate::kProxyRevoked), Context);
5149 }
5150 Handle<JSReceiver> target(JSReceiver::cast(proxy->target()));
5151 return JSReceiver::GetFunctionRealm(target);
5152}
5153
5154
5155// static
5156MaybeHandle<Context> JSBoundFunction::GetFunctionRealm(
5157 Handle<JSBoundFunction> function) {
5158 DCHECK(function->map()->is_constructor());
5159 return JSReceiver::GetFunctionRealm(
5160 handle(function->bound_target_function()));
5161}
5162
5163
5164// static
5165Handle<Context> JSFunction::GetFunctionRealm(Handle<JSFunction> function) {
5166 DCHECK(function->map()->is_constructor());
5167 return handle(function->context()->native_context());
5168}
5169
5170
5171// static
5172MaybeHandle<Context> JSObject::GetFunctionRealm(Handle<JSObject> object) {
5173 DCHECK(object->map()->is_constructor());
5174 DCHECK(!object->IsJSFunction());
5175 return handle(object->GetCreationContext());
5176}
5177
5178
5179// static
5180MaybeHandle<Context> JSReceiver::GetFunctionRealm(Handle<JSReceiver> receiver) {
5181 if (receiver->IsJSProxy()) {
5182 return JSProxy::GetFunctionRealm(Handle<JSProxy>::cast(receiver));
5183 }
5184
5185 if (receiver->IsJSFunction()) {
5186 return JSFunction::GetFunctionRealm(Handle<JSFunction>::cast(receiver));
5187 }
5188
5189 if (receiver->IsJSBoundFunction()) {
5190 return JSBoundFunction::GetFunctionRealm(
5191 Handle<JSBoundFunction>::cast(receiver));
5192 }
5193
5194 return JSObject::GetFunctionRealm(Handle<JSObject>::cast(receiver));
5195}
5196
5197
5198Maybe<PropertyAttributes> JSProxy::GetPropertyAttributes(LookupIterator* it) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005199 PropertyDescriptor desc;
5200 Maybe<bool> found = JSProxy::GetOwnPropertyDescriptor(
Ben Murdochda12d292016-06-02 14:46:10 +01005201 it->isolate(), it->GetHolder<JSProxy>(), it->GetName(), &desc);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005202 MAYBE_RETURN(found, Nothing<PropertyAttributes>());
5203 if (!found.FromJust()) return Just(ABSENT);
5204 return Just(desc.ToAttributes());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005205}
5206
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005207
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005208void JSObject::AllocateStorageForMap(Handle<JSObject> object, Handle<Map> map) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005209 DCHECK(object->map()->GetInObjectProperties() ==
5210 map->GetInObjectProperties());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005211 ElementsKind obj_kind = object->map()->elements_kind();
5212 ElementsKind map_kind = map->elements_kind();
5213 if (map_kind != obj_kind) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005214 ElementsKind to_kind = GetMoreGeneralElementsKind(map_kind, obj_kind);
5215 if (IsDictionaryElementsKind(obj_kind)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005216 to_kind = obj_kind;
John Reck59135872010-11-02 12:39:01 -07005217 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005218 if (IsDictionaryElementsKind(to_kind)) {
5219 NormalizeElements(object);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005220 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005221 TransitionElementsKind(object, to_kind);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005222 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005223 map = Map::AsElementsKind(map, to_kind);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005224 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005225 JSObject::MigrateToMap(object, map);
5226}
5227
5228
5229void JSObject::MigrateInstance(Handle<JSObject> object) {
5230 Handle<Map> original_map(object->map());
5231 Handle<Map> map = Map::Update(original_map);
5232 map->set_migration_target(true);
5233 MigrateToMap(object, map);
5234 if (FLAG_trace_migration) {
5235 object->PrintInstanceMigration(stdout, *original_map, *map);
5236 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005237#if VERIFY_HEAP
5238 if (FLAG_verify_heap) {
5239 object->JSObjectVerify();
5240 }
5241#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005242}
5243
5244
5245// static
5246bool JSObject::TryMigrateInstance(Handle<JSObject> object) {
5247 Isolate* isolate = object->GetIsolate();
5248 DisallowDeoptimization no_deoptimization(isolate);
5249 Handle<Map> original_map(object->map(), isolate);
5250 Handle<Map> new_map;
5251 if (!Map::TryUpdate(original_map).ToHandle(&new_map)) {
5252 return false;
5253 }
5254 JSObject::MigrateToMap(object, new_map);
5255 if (FLAG_trace_migration) {
5256 object->PrintInstanceMigration(stdout, *original_map, object->map());
5257 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005258#if VERIFY_HEAP
5259 if (FLAG_verify_heap) {
5260 object->JSObjectVerify();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005261 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005262#endif
5263 return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005264}
5265
5266
5267void JSObject::AddProperty(Handle<JSObject> object, Handle<Name> name,
5268 Handle<Object> value,
5269 PropertyAttributes attributes) {
Ben Murdochda12d292016-06-02 14:46:10 +01005270 LookupIterator it(object, name, object, LookupIterator::OWN_SKIP_INTERCEPTOR);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005271 CHECK_NE(LookupIterator::ACCESS_CHECK, it.state());
5272#ifdef DEBUG
5273 uint32_t index;
5274 DCHECK(!object->IsJSProxy());
5275 DCHECK(!name->AsArrayIndex(&index));
5276 Maybe<PropertyAttributes> maybe = GetPropertyAttributes(&it);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005277 DCHECK(maybe.IsJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005278 DCHECK(!it.IsFound());
Ben Murdoch097c5b22016-05-18 11:27:45 +01005279 DCHECK(object->map()->is_extensible() || name->IsPrivate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005280#endif
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005281 CHECK(AddDataProperty(&it, value, attributes, THROW_ON_ERROR,
5282 CERTAINLY_NOT_STORE_FROM_KEYED)
5283 .IsJust());
5284}
5285
5286
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005287// Reconfigures a property to a data property with attributes, even if it is not
5288// reconfigurable.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005289// Requires a LookupIterator that does not look at the prototype chain beyond
5290// hidden prototypes.
5291MaybeHandle<Object> JSObject::DefineOwnPropertyIgnoreAttributes(
5292 LookupIterator* it, Handle<Object> value, PropertyAttributes attributes,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005293 AccessorInfoHandling handling) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005294 MAYBE_RETURN_NULL(DefineOwnPropertyIgnoreAttributes(
5295 it, value, attributes, THROW_ON_ERROR, handling));
5296 return value;
5297}
5298
5299
5300Maybe<bool> JSObject::DefineOwnPropertyIgnoreAttributes(
5301 LookupIterator* it, Handle<Object> value, PropertyAttributes attributes,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005302 ShouldThrow should_throw, AccessorInfoHandling handling) {
5303 it->UpdateProtector();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005304 Handle<JSObject> object = Handle<JSObject>::cast(it->GetReceiver());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005305 bool is_observed = object->map()->is_observed() &&
Ben Murdoch097c5b22016-05-18 11:27:45 +01005306 (it->IsElement() || !it->name()->IsPrivate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005307
5308 for (; it->IsFound(); it->Next()) {
5309 switch (it->state()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005310 case LookupIterator::JSPROXY:
5311 case LookupIterator::NOT_FOUND:
5312 case LookupIterator::TRANSITION:
5313 UNREACHABLE();
5314
5315 case LookupIterator::ACCESS_CHECK:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005316 if (!it->HasAccess()) {
5317 it->isolate()->ReportFailedAccessCheck(it->GetHolder<JSObject>());
5318 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(it->isolate(), Nothing<bool>());
5319 return Just(true);
5320 }
5321 break;
5322
5323 // If there's an interceptor, try to store the property with the
5324 // interceptor.
5325 // In case of success, the attributes will have been reset to the default
5326 // attributes of the interceptor, rather than the incoming attributes.
5327 //
5328 // TODO(verwaest): JSProxy afterwards verify the attributes that the
5329 // JSProxy claims it has, and verifies that they are compatible. If not,
5330 // they throw. Here we should do the same.
5331 case LookupIterator::INTERCEPTOR:
5332 if (handling == DONT_FORCE_FIELD) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01005333 Maybe<bool> result =
5334 JSObject::SetPropertyWithInterceptor(it, should_throw, value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005335 if (result.IsNothing() || result.FromJust()) return result;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005336 }
5337 break;
5338
5339 case LookupIterator::ACCESSOR: {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005340 Handle<Object> accessors = it->GetAccessors();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005341
Ben Murdoch097c5b22016-05-18 11:27:45 +01005342 // Special handling for AccessorInfo, which behaves like a data
5343 // property.
5344 if (accessors->IsAccessorInfo() && handling == DONT_FORCE_FIELD) {
5345 PropertyAttributes current_attributes = it->property_attributes();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005346 // Ensure the context isn't changed after calling into accessors.
5347 AssertNoContextChange ncc(it->isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005348
Ben Murdoch097c5b22016-05-18 11:27:45 +01005349 // Update the attributes before calling the setter. The setter may
5350 // later change the shape of the property.
5351 if (current_attributes != attributes) {
5352 it->TransitionToAccessorPair(accessors, attributes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005353 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005354
Ben Murdoch097c5b22016-05-18 11:27:45 +01005355 Maybe<bool> result =
5356 JSObject::SetPropertyWithAccessor(it, value, should_throw);
5357
5358 if (current_attributes == attributes || result.IsNothing()) {
5359 return result;
5360 }
5361
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005362 } else {
5363 it->ReconfigureDataProperty(value, attributes);
5364 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005365
5366 if (is_observed) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005367 RETURN_ON_EXCEPTION_VALUE(
5368 it->isolate(),
5369 EnqueueChangeRecord(object, "reconfigure", it->GetName(),
5370 it->factory()->the_hole_value()),
5371 Nothing<bool>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005372 }
5373
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005374 return Just(true);
Iain Merrick75681382010-08-19 15:07:18 +01005375 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005376 case LookupIterator::INTEGER_INDEXED_EXOTIC:
5377 return RedefineIncompatibleProperty(it->isolate(), it->GetName(), value,
5378 should_throw);
Steve Blocka7e24c12009-10-30 11:49:00 +00005379
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005380 case LookupIterator::DATA: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005381 // Regular property update if the attributes match.
Ben Murdoch097c5b22016-05-18 11:27:45 +01005382 if (it->property_attributes() == attributes) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005383 return SetDataProperty(it, value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005384 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005385
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005386 // Special case: properties of typed arrays cannot be reconfigured to
5387 // non-writable nor to non-enumerable.
5388 if (it->IsElement() && object->HasFixedTypedArrayElements()) {
5389 return RedefineIncompatibleProperty(it->isolate(), it->GetName(),
5390 value, should_throw);
5391 }
5392
5393 // Reconfigure the data property if the attributes mismatch.
Ben Murdochda12d292016-06-02 14:46:10 +01005394 Handle<Object> old_value = it->factory()->the_hole_value();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005395 if (is_observed) old_value = it->GetDataValue();
5396
5397 it->ReconfigureDataProperty(value, attributes);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005398
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005399 if (is_observed) {
5400 if (old_value->SameValue(*value)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005401 old_value = it->factory()->the_hole_value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005402 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005403 RETURN_ON_EXCEPTION_VALUE(
5404 it->isolate(), EnqueueChangeRecord(object, "reconfigure",
5405 it->GetName(), old_value),
5406 Nothing<bool>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005407 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005408 return Just(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00005409 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005410 }
5411 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005412
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005413 return AddDataProperty(it, value, attributes, should_throw,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005414 CERTAINLY_NOT_STORE_FROM_KEYED);
Steve Blocka7e24c12009-10-30 11:49:00 +00005415}
5416
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005417MaybeHandle<Object> JSObject::SetOwnPropertyIgnoreAttributes(
5418 Handle<JSObject> object, Handle<Name> name, Handle<Object> value,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005419 PropertyAttributes attributes) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005420 DCHECK(!value->IsTheHole());
Ben Murdochda12d292016-06-02 14:46:10 +01005421 LookupIterator it(object, name, object, LookupIterator::OWN);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005422 return DefineOwnPropertyIgnoreAttributes(&it, value, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00005423}
5424
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005425MaybeHandle<Object> JSObject::SetOwnElementIgnoreAttributes(
5426 Handle<JSObject> object, uint32_t index, Handle<Object> value,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005427 PropertyAttributes attributes) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005428 Isolate* isolate = object->GetIsolate();
Ben Murdochda12d292016-06-02 14:46:10 +01005429 LookupIterator it(isolate, object, index, object, LookupIterator::OWN);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005430 return DefineOwnPropertyIgnoreAttributes(&it, value, attributes);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005431}
5432
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005433MaybeHandle<Object> JSObject::DefinePropertyOrElementIgnoreAttributes(
5434 Handle<JSObject> object, Handle<Name> name, Handle<Object> value,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005435 PropertyAttributes attributes) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005436 Isolate* isolate = object->GetIsolate();
Ben Murdochda12d292016-06-02 14:46:10 +01005437 LookupIterator it = LookupIterator::PropertyOrElement(
5438 isolate, object, name, object, LookupIterator::OWN);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005439 return DefineOwnPropertyIgnoreAttributes(&it, value, attributes);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005440}
5441
5442
5443Maybe<PropertyAttributes> JSObject::GetPropertyAttributesWithInterceptor(
5444 LookupIterator* it) {
5445 Isolate* isolate = it->isolate();
5446 // Make sure that the top context does not change when doing
5447 // callbacks or interceptor calls.
5448 AssertNoContextChange ncc(isolate);
5449 HandleScope scope(isolate);
5450
5451 Handle<JSObject> holder = it->GetHolder<JSObject>();
5452 Handle<InterceptorInfo> interceptor(it->GetInterceptor());
5453 if (!it->IsElement() && it->name()->IsSymbol() &&
5454 !interceptor->can_intercept_symbols()) {
5455 return Just(ABSENT);
Steve Blocka7e24c12009-10-30 11:49:00 +00005456 }
Ben Murdochda12d292016-06-02 14:46:10 +01005457 Handle<Object> receiver = it->GetReceiver();
5458 if (!receiver->IsJSReceiver()) {
5459 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, receiver,
5460 Object::ConvertReceiver(isolate, receiver),
5461 Nothing<PropertyAttributes>());
5462 }
5463 PropertyCallbackArguments args(isolate, interceptor->data(), *receiver,
5464 *holder, Object::DONT_THROW);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005465 if (!interceptor->query()->IsUndefined()) {
Ben Murdochda12d292016-06-02 14:46:10 +01005466 Handle<Object> result;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005467 if (it->IsElement()) {
5468 uint32_t index = it->index();
5469 v8::IndexedPropertyQueryCallback query =
5470 v8::ToCData<v8::IndexedPropertyQueryCallback>(interceptor->query());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005471 result = args.Call(query, index);
5472 } else {
5473 Handle<Name> name = it->name();
5474 DCHECK(!name->IsPrivate());
5475 v8::GenericNamedPropertyQueryCallback query =
5476 v8::ToCData<v8::GenericNamedPropertyQueryCallback>(
5477 interceptor->query());
Ben Murdochda12d292016-06-02 14:46:10 +01005478 result = args.Call(query, name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005479 }
Ben Murdochda12d292016-06-02 14:46:10 +01005480 if (!result.is_null()) {
5481 int32_t value;
5482 CHECK(result->ToInt32(&value));
5483 return Just(static_cast<PropertyAttributes>(value));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005484 }
5485 } else if (!interceptor->getter()->IsUndefined()) {
5486 // TODO(verwaest): Use GetPropertyWithInterceptor?
Ben Murdochda12d292016-06-02 14:46:10 +01005487 Handle<Object> result;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005488 if (it->IsElement()) {
5489 uint32_t index = it->index();
5490 v8::IndexedPropertyGetterCallback getter =
5491 v8::ToCData<v8::IndexedPropertyGetterCallback>(interceptor->getter());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005492 result = args.Call(getter, index);
5493 } else {
5494 Handle<Name> name = it->name();
5495 DCHECK(!name->IsPrivate());
5496 v8::GenericNamedPropertyGetterCallback getter =
5497 v8::ToCData<v8::GenericNamedPropertyGetterCallback>(
5498 interceptor->getter());
Ben Murdochda12d292016-06-02 14:46:10 +01005499 result = args.Call(getter, name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005500 }
Ben Murdochda12d292016-06-02 14:46:10 +01005501 if (!result.is_null()) return Just(DONT_ENUM);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005502 }
5503
5504 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<PropertyAttributes>());
5505 return Just(ABSENT);
Steve Blocka7e24c12009-10-30 11:49:00 +00005506}
5507
5508
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005509Maybe<PropertyAttributes> JSReceiver::GetPropertyAttributes(
5510 LookupIterator* it) {
5511 for (; it->IsFound(); it->Next()) {
5512 switch (it->state()) {
5513 case LookupIterator::NOT_FOUND:
5514 case LookupIterator::TRANSITION:
5515 UNREACHABLE();
5516 case LookupIterator::JSPROXY:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005517 return JSProxy::GetPropertyAttributes(it);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005518 case LookupIterator::INTERCEPTOR: {
5519 Maybe<PropertyAttributes> result =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005520 JSObject::GetPropertyAttributesWithInterceptor(it);
5521 if (!result.IsJust()) return result;
5522 if (result.FromJust() != ABSENT) return result;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005523 break;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005524 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005525 case LookupIterator::ACCESS_CHECK:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005526 if (it->HasAccess()) break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005527 return JSObject::GetPropertyAttributesWithFailedAccessCheck(it);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005528 case LookupIterator::INTEGER_INDEXED_EXOTIC:
5529 return Just(ABSENT);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005530 case LookupIterator::ACCESSOR:
5531 case LookupIterator::DATA:
Ben Murdoch097c5b22016-05-18 11:27:45 +01005532 return Just(it->property_attributes());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005533 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005534 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005535 return Just(ABSENT);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005536}
5537
5538
5539Handle<NormalizedMapCache> NormalizedMapCache::New(Isolate* isolate) {
5540 Handle<FixedArray> array(
5541 isolate->factory()->NewFixedArray(kEntries, TENURED));
5542 return Handle<NormalizedMapCache>::cast(array);
5543}
5544
5545
5546MaybeHandle<Map> NormalizedMapCache::Get(Handle<Map> fast_map,
5547 PropertyNormalizationMode mode) {
5548 DisallowHeapAllocation no_gc;
5549 Object* value = FixedArray::get(GetIndex(fast_map));
5550 if (!value->IsMap() ||
5551 !Map::cast(value)->EquivalentToForNormalization(*fast_map, mode)) {
5552 return MaybeHandle<Map>();
5553 }
5554 return handle(Map::cast(value));
5555}
5556
5557
5558void NormalizedMapCache::Set(Handle<Map> fast_map,
5559 Handle<Map> normalized_map) {
5560 DisallowHeapAllocation no_gc;
5561 DCHECK(normalized_map->is_dictionary_map());
5562 FixedArray::set(GetIndex(fast_map), *normalized_map);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005563}
5564
5565
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005566void NormalizedMapCache::Clear() {
5567 int entries = length();
5568 for (int i = 0; i != entries; i++) {
5569 set_undefined(i);
5570 }
5571}
5572
5573
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005574void HeapObject::UpdateMapCodeCache(Handle<HeapObject> object,
5575 Handle<Name> name,
5576 Handle<Code> code) {
5577 Handle<Map> map(object->map());
5578 Map::UpdateCodeCache(map, name, code);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005579}
5580
5581
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005582void JSObject::NormalizeProperties(Handle<JSObject> object,
5583 PropertyNormalizationMode mode,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005584 int expected_additional_properties,
5585 const char* reason) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005586 if (!object->HasFastProperties()) return;
5587
5588 Handle<Map> map(object->map());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005589 Handle<Map> new_map = Map::Normalize(map, mode, reason);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005590
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005591 MigrateToMap(object, new_map, expected_additional_properties);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005592}
5593
5594
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005595void JSObject::MigrateSlowToFast(Handle<JSObject> object,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005596 int unused_property_fields,
5597 const char* reason) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005598 if (object->HasFastProperties()) return;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005599 DCHECK(!object->IsJSGlobalObject());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005600 Isolate* isolate = object->GetIsolate();
5601 Factory* factory = isolate->factory();
5602 Handle<NameDictionary> dictionary(object->property_dictionary());
5603
5604 // Make sure we preserve dictionary representation if there are too many
5605 // descriptors.
5606 int number_of_elements = dictionary->NumberOfElements();
5607 if (number_of_elements > kMaxNumberOfDescriptors) return;
5608
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005609 Handle<FixedArray> iteration_order;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005610 if (number_of_elements != dictionary->NextEnumerationIndex()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005611 iteration_order =
5612 NameDictionary::DoGenerateNewEnumerationIndices(dictionary);
5613 } else {
5614 iteration_order = NameDictionary::BuildIterationIndicesArray(dictionary);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005615 }
5616
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005617 int instance_descriptor_length = iteration_order->length();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005618 int number_of_fields = 0;
5619
5620 // Compute the length of the instance descriptor.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005621 for (int i = 0; i < instance_descriptor_length; i++) {
5622 int index = Smi::cast(iteration_order->get(i))->value();
5623 DCHECK(dictionary->IsKey(dictionary->KeyAt(index)));
5624
5625 Object* value = dictionary->ValueAt(index);
5626 PropertyType type = dictionary->DetailsAt(index).type();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005627 if (type == DATA && !value->IsJSFunction()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005628 number_of_fields += 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005629 }
5630 }
5631
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005632 Handle<Map> old_map(object->map(), isolate);
5633
5634 int inobject_props = old_map->GetInObjectProperties();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005635
5636 // Allocate new map.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005637 Handle<Map> new_map = Map::CopyDropDescriptors(old_map);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005638 new_map->set_dictionary_map(false);
5639
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005640 UpdatePrototypeUserRegistration(old_map, new_map, isolate);
5641
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005642#if TRACE_MAPS
5643 if (FLAG_trace_maps) {
5644 PrintF("[TraceMaps: SlowToFast from= %p to= %p reason= %s ]\n",
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005645 reinterpret_cast<void*>(*old_map), reinterpret_cast<void*>(*new_map),
5646 reason);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005647 }
5648#endif
5649
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005650 if (instance_descriptor_length == 0) {
5651 DisallowHeapAllocation no_gc;
5652 DCHECK_LE(unused_property_fields, inobject_props);
5653 // Transform the object.
5654 new_map->set_unused_property_fields(inobject_props);
5655 object->synchronized_set_map(*new_map);
5656 object->set_properties(isolate->heap()->empty_fixed_array());
5657 // Check that it really works.
5658 DCHECK(object->HasFastProperties());
5659 return;
5660 }
5661
5662 // Allocate the instance descriptor.
5663 Handle<DescriptorArray> descriptors = DescriptorArray::Allocate(
Ben Murdoch097c5b22016-05-18 11:27:45 +01005664 isolate, instance_descriptor_length, 0, TENURED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005665
5666 int number_of_allocated_fields =
5667 number_of_fields + unused_property_fields - inobject_props;
5668 if (number_of_allocated_fields < 0) {
5669 // There is enough inobject space for all fields (including unused).
5670 number_of_allocated_fields = 0;
5671 unused_property_fields = inobject_props - number_of_fields;
5672 }
5673
5674 // Allocate the fixed array for the fields.
5675 Handle<FixedArray> fields = factory->NewFixedArray(
5676 number_of_allocated_fields);
5677
5678 // Fill in the instance descriptor and the fields.
5679 int current_offset = 0;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005680 for (int i = 0; i < instance_descriptor_length; i++) {
5681 int index = Smi::cast(iteration_order->get(i))->value();
5682 Object* k = dictionary->KeyAt(index);
5683 DCHECK(dictionary->IsKey(k));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005684 // Dictionary keys are internalized upon insertion.
5685 // TODO(jkummerow): Turn this into a DCHECK if it's not hit in the wild.
5686 CHECK(k->IsUniqueName());
5687 Handle<Name> key(Name::cast(k), isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005688
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005689 Object* value = dictionary->ValueAt(index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005690
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005691 PropertyDetails details = dictionary->DetailsAt(index);
5692 int enumeration_index = details.dictionary_index();
5693 PropertyType type = details.type();
5694
5695 if (value->IsJSFunction()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005696 DataConstantDescriptor d(key, handle(value, isolate),
5697 details.attributes());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005698 descriptors->Set(enumeration_index - 1, &d);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005699 } else if (type == DATA) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005700 if (current_offset < inobject_props) {
5701 object->InObjectPropertyAtPut(current_offset, value,
5702 UPDATE_WRITE_BARRIER);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005703 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005704 int offset = current_offset - inobject_props;
5705 fields->set(offset, value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005706 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005707 DataDescriptor d(key, current_offset, details.attributes(),
5708 // TODO(verwaest): value->OptimalRepresentation();
5709 Representation::Tagged());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005710 current_offset += d.GetDetails().field_width_in_words();
5711 descriptors->Set(enumeration_index - 1, &d);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005712 } else if (type == ACCESSOR_CONSTANT) {
5713 AccessorConstantDescriptor d(key, handle(value, isolate),
5714 details.attributes());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005715 descriptors->Set(enumeration_index - 1, &d);
5716 } else {
5717 UNREACHABLE();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005718 }
5719 }
5720 DCHECK(current_offset == number_of_fields);
5721
5722 descriptors->Sort();
5723
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005724 Handle<LayoutDescriptor> layout_descriptor = LayoutDescriptor::New(
5725 new_map, descriptors, descriptors->number_of_descriptors());
5726
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005727 DisallowHeapAllocation no_gc;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005728 new_map->InitializeDescriptors(*descriptors, *layout_descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005729 new_map->set_unused_property_fields(unused_property_fields);
5730
5731 // Transform the object.
5732 object->synchronized_set_map(*new_map);
5733
5734 object->set_properties(*fields);
5735 DCHECK(object->IsJSObject());
5736
5737 // Check that it really works.
5738 DCHECK(object->HasFastProperties());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005739}
5740
5741
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005742void JSObject::ResetElements(Handle<JSObject> object) {
5743 Isolate* isolate = object->GetIsolate();
5744 CHECK(object->map() != isolate->heap()->sloppy_arguments_elements_map());
5745 if (object->map()->has_dictionary_elements()) {
5746 Handle<SeededNumberDictionary> new_elements =
5747 SeededNumberDictionary::New(isolate, 0);
5748 object->set_elements(*new_elements);
5749 } else {
5750 object->set_elements(object->map()->GetInitialElements());
5751 }
5752}
5753
5754
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005755void JSObject::RequireSlowElements(SeededNumberDictionary* dictionary) {
5756 if (dictionary->requires_slow_elements()) return;
5757 dictionary->set_requires_slow_elements();
5758 // TODO(verwaest): Remove this hack.
5759 if (map()->is_prototype_map()) {
5760 TypeFeedbackVector::ClearAllKeyedStoreICs(GetIsolate());
5761 }
5762}
5763
5764
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005765Handle<SeededNumberDictionary> JSObject::NormalizeElements(
5766 Handle<JSObject> object) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005767 DCHECK(!object->HasFixedTypedArrayElements());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005768 Isolate* isolate = object->GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005769 bool is_arguments = object->HasSloppyArgumentsElements();
Ben Murdochda12d292016-06-02 14:46:10 +01005770 {
5771 DisallowHeapAllocation no_gc;
5772 FixedArrayBase* elements = object->elements();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005773
Ben Murdochda12d292016-06-02 14:46:10 +01005774 if (is_arguments) {
5775 FixedArray* parameter_map = FixedArray::cast(elements);
5776 elements = FixedArrayBase::cast(parameter_map->get(1));
5777 }
5778
5779 if (elements->IsDictionary()) {
5780 return handle(SeededNumberDictionary::cast(elements), isolate);
5781 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005782 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005783
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005784 DCHECK(object->HasFastSmiOrObjectElements() ||
5785 object->HasFastDoubleElements() ||
Ben Murdoch097c5b22016-05-18 11:27:45 +01005786 object->HasFastArgumentsElements() ||
5787 object->HasFastStringWrapperElements());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005788
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005789 Handle<SeededNumberDictionary> dictionary =
Ben Murdochda12d292016-06-02 14:46:10 +01005790 object->GetElementsAccessor()->Normalize(object);
Steve Blocka7e24c12009-10-30 11:49:00 +00005791
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005792 // Switch to using the dictionary as the backing storage for elements.
Ben Murdoch097c5b22016-05-18 11:27:45 +01005793 ElementsKind target_kind = is_arguments
5794 ? SLOW_SLOPPY_ARGUMENTS_ELEMENTS
5795 : object->HasFastStringWrapperElements()
5796 ? SLOW_STRING_WRAPPER_ELEMENTS
5797 : DICTIONARY_ELEMENTS;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005798 Handle<Map> new_map = JSObject::GetElementsTransitionMap(object, target_kind);
5799 // Set the new map first to satify the elements type assert in set_elements().
5800 JSObject::MigrateToMap(object, new_map);
5801
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005802 if (is_arguments) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005803 FixedArray::cast(object->elements())->set(1, *dictionary);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005804 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005805 object->set_elements(*dictionary);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005806 }
5807
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005808 isolate->counters()->elements_to_dictionary()->Increment();
Steve Blocka7e24c12009-10-30 11:49:00 +00005809
5810#ifdef DEBUG
5811 if (FLAG_trace_normalization) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005812 OFStream os(stdout);
5813 os << "Object elements have been normalized:\n";
5814 object->Print(os);
Steve Blocka7e24c12009-10-30 11:49:00 +00005815 }
5816#endif
5817
Ben Murdoch097c5b22016-05-18 11:27:45 +01005818 DCHECK(object->HasDictionaryElements() ||
5819 object->HasSlowArgumentsElements() ||
5820 object->HasSlowStringWrapperElements());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005821 return dictionary;
Steve Blocka7e24c12009-10-30 11:49:00 +00005822}
5823
5824
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005825static Smi* GenerateIdentityHash(Isolate* isolate) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005826 int hash_value;
5827 int attempts = 0;
5828 do {
5829 // Generate a random 32-bit hash value but limit range to fit
5830 // within a smi.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005831 hash_value = isolate->random_number_generator()->NextInt() & Smi::kMaxValue;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005832 attempts++;
5833 } while (hash_value == 0 && attempts < 30);
5834 hash_value = hash_value != 0 ? hash_value : 1; // never return 0
5835
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005836 return Smi::FromInt(hash_value);
5837}
5838
5839
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005840template<typename ProxyType>
5841static Handle<Smi> GetOrCreateIdentityHashHelper(Handle<ProxyType> proxy) {
5842 Isolate* isolate = proxy->GetIsolate();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005843
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005844 Handle<Object> maybe_hash(proxy->hash(), isolate);
5845 if (maybe_hash->IsSmi()) return Handle<Smi>::cast(maybe_hash);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005846
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005847 Handle<Smi> hash(GenerateIdentityHash(isolate), isolate);
5848 proxy->set_hash(*hash);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005849 return hash;
5850}
5851
Ben Murdochda12d292016-06-02 14:46:10 +01005852// static
5853Handle<Object> JSObject::GetIdentityHash(Isolate* isolate,
5854 Handle<JSObject> object) {
5855 if (object->IsJSGlobalProxy()) {
5856 return handle(JSGlobalProxy::cast(*object)->hash(), isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005857 }
Ben Murdochda12d292016-06-02 14:46:10 +01005858 Handle<Name> hash_code_symbol = isolate->factory()->hash_code_symbol();
5859 return JSReceiver::GetDataProperty(object, hash_code_symbol);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005860}
5861
Ben Murdochda12d292016-06-02 14:46:10 +01005862// static
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005863Handle<Smi> JSObject::GetOrCreateIdentityHash(Handle<JSObject> object) {
5864 if (object->IsJSGlobalProxy()) {
5865 return GetOrCreateIdentityHashHelper(Handle<JSGlobalProxy>::cast(object));
5866 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005867 Isolate* isolate = object->GetIsolate();
5868
Ben Murdochda12d292016-06-02 14:46:10 +01005869 Handle<Name> hash_code_symbol = isolate->factory()->hash_code_symbol();
5870 LookupIterator it(object, hash_code_symbol, object, LookupIterator::OWN);
5871 if (it.IsFound()) {
5872 DCHECK_EQ(LookupIterator::DATA, it.state());
5873 Handle<Object> maybe_hash = it.GetDataValue();
5874 if (maybe_hash->IsSmi()) return Handle<Smi>::cast(maybe_hash);
5875 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005876
5877 Handle<Smi> hash(GenerateIdentityHash(isolate), isolate);
Ben Murdochda12d292016-06-02 14:46:10 +01005878 CHECK(AddDataProperty(&it, hash, NONE, THROW_ON_ERROR,
5879 CERTAINLY_NOT_STORE_FROM_KEYED)
5880 .IsJust());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005881 return hash;
5882}
5883
Ben Murdochda12d292016-06-02 14:46:10 +01005884// static
5885Handle<Object> JSProxy::GetIdentityHash(Isolate* isolate,
5886 Handle<JSProxy> proxy) {
5887 return handle(proxy->hash(), isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005888}
5889
5890
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005891Handle<Smi> JSProxy::GetOrCreateIdentityHash(Handle<JSProxy> proxy) {
5892 return GetOrCreateIdentityHashHelper(proxy);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005893}
5894
5895
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005896Object* JSObject::GetHiddenProperty(Handle<Name> key) {
5897 DisallowHeapAllocation no_gc;
5898 DCHECK(key->IsUniqueName());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005899 if (IsJSGlobalProxy()) {
5900 // For a proxy, use the prototype as target object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005901 PrototypeIterator iter(GetIsolate(), this);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005902 // If the proxy is detached, return undefined.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005903 if (iter.IsAtEnd()) return GetHeap()->the_hole_value();
5904 DCHECK(iter.GetCurrent()->IsJSGlobalObject());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005905 return iter.GetCurrent<JSObject>()->GetHiddenProperty(key);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005906 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005907 DCHECK(!IsJSGlobalProxy());
5908 Object* inline_value = GetHiddenPropertiesHashTable();
5909
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005910 if (inline_value->IsUndefined()) return GetHeap()->the_hole_value();
5911
5912 ObjectHashTable* hashtable = ObjectHashTable::cast(inline_value);
5913 Object* entry = hashtable->Lookup(key);
5914 return entry;
5915}
5916
5917
5918Handle<Object> JSObject::SetHiddenProperty(Handle<JSObject> object,
5919 Handle<Name> key,
5920 Handle<Object> value) {
5921 Isolate* isolate = object->GetIsolate();
5922
5923 DCHECK(key->IsUniqueName());
5924 if (object->IsJSGlobalProxy()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005925 // For a proxy, use the prototype as target object.
5926 PrototypeIterator iter(isolate, object);
5927 // If the proxy is detached, return undefined.
5928 if (iter.IsAtEnd()) return isolate->factory()->undefined_value();
5929 DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005930 return SetHiddenProperty(PrototypeIterator::GetCurrent<JSObject>(iter), key,
5931 value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005932 }
5933 DCHECK(!object->IsJSGlobalProxy());
5934
5935 Handle<Object> inline_value(object->GetHiddenPropertiesHashTable(), isolate);
5936
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005937 Handle<ObjectHashTable> hashtable =
5938 GetOrCreateHiddenPropertiesHashtable(object);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005939
5940 // If it was found, check if the key is already in the dictionary.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005941 Handle<ObjectHashTable> new_table = ObjectHashTable::Put(hashtable, key,
5942 value);
5943 if (*new_table != *hashtable) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005944 // If adding the key expanded the dictionary (i.e., Add returned a new
5945 // dictionary), store it back to the object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005946 SetHiddenPropertiesHashTable(object, new_table);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005947 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005948
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005949 // Return this to mark success.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005950 return object;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005951}
5952
5953
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005954void JSObject::DeleteHiddenProperty(Handle<JSObject> object, Handle<Name> key) {
5955 Isolate* isolate = object->GetIsolate();
5956 DCHECK(key->IsUniqueName());
5957
5958 if (object->IsJSGlobalProxy()) {
5959 PrototypeIterator iter(isolate, object);
5960 if (iter.IsAtEnd()) return;
5961 DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005962 return DeleteHiddenProperty(PrototypeIterator::GetCurrent<JSObject>(iter),
5963 key);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005964 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005965
5966 Object* inline_value = object->GetHiddenPropertiesHashTable();
5967
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005968 if (inline_value->IsUndefined()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005969
5970 Handle<ObjectHashTable> hashtable(ObjectHashTable::cast(inline_value));
5971 bool was_present = false;
5972 ObjectHashTable::Remove(hashtable, key, &was_present);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005973}
5974
5975
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005976bool JSObject::HasHiddenProperties(Handle<JSObject> object) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01005977 Isolate* isolate = object->GetIsolate();
5978 Handle<Symbol> hidden = isolate->factory()->hidden_properties_symbol();
Ben Murdochda12d292016-06-02 14:46:10 +01005979 LookupIterator it(object, hidden, object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005980 Maybe<PropertyAttributes> maybe = GetPropertyAttributes(&it);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005981 // Cannot get an exception since the hidden_properties_symbol isn't exposed to
5982 // JS.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005983 DCHECK(maybe.IsJust());
5984 return maybe.FromJust() != ABSENT;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005985}
5986
5987
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005988Object* JSObject::GetHiddenPropertiesHashTable() {
5989 DCHECK(!IsJSGlobalProxy());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005990 if (HasFastProperties()) {
5991 // If the object has fast properties, check whether the first slot
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005992 // in the descriptor array matches the hidden string. Since the
5993 // hidden strings hash code is zero (and no other name has hash
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005994 // code zero) it will always occupy the first entry if present.
5995 DescriptorArray* descriptors = this->map()->instance_descriptors();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005996 if (descriptors->number_of_descriptors() > 0) {
5997 int sorted_index = descriptors->GetSortedKeyIndex(0);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005998 if (descriptors->GetKey(sorted_index) ==
5999 GetHeap()->hidden_properties_symbol() &&
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006000 sorted_index < map()->NumberOfOwnDescriptors()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006001 DCHECK(descriptors->GetType(sorted_index) == DATA);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006002 DCHECK(descriptors->GetDetails(sorted_index).representation().
6003 IsCompatibleForLoad(Representation::Tagged()));
6004 FieldIndex index = FieldIndex::ForDescriptor(this->map(),
6005 sorted_index);
6006 return this->RawFastPropertyAt(index);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006007 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006008 return GetHeap()->undefined_value();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006009 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006010 } else {
6011 return GetHeap()->undefined_value();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006012 }
6013 } else {
Ben Murdochda12d292016-06-02 14:46:10 +01006014 Isolate* isolate = GetIsolate();
6015 Handle<Symbol> hidden = isolate->factory()->hidden_properties_symbol();
6016 Handle<JSObject> receiver(this, isolate);
6017 LookupIterator it(receiver, hidden, receiver);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006018 // Access check is always skipped for the hidden string anyways.
6019 return *GetDataProperty(&it);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006020 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006021}
6022
6023Handle<ObjectHashTable> JSObject::GetOrCreateHiddenPropertiesHashtable(
6024 Handle<JSObject> object) {
6025 Isolate* isolate = object->GetIsolate();
6026
6027 static const int kInitialCapacity = 4;
6028 Handle<Object> inline_value(object->GetHiddenPropertiesHashTable(), isolate);
6029 if (inline_value->IsHashTable()) {
6030 return Handle<ObjectHashTable>::cast(inline_value);
6031 }
6032
6033 Handle<ObjectHashTable> hashtable = ObjectHashTable::New(
6034 isolate, kInitialCapacity, USE_CUSTOM_MINIMUM_CAPACITY);
6035
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006036 DCHECK(inline_value->IsUndefined());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006037 SetHiddenPropertiesHashTable(object, hashtable);
6038 return hashtable;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006039}
6040
6041
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006042Handle<Object> JSObject::SetHiddenPropertiesHashTable(Handle<JSObject> object,
6043 Handle<Object> value) {
6044 DCHECK(!object->IsJSGlobalProxy());
6045 Isolate* isolate = object->GetIsolate();
Ben Murdoch097c5b22016-05-18 11:27:45 +01006046 Handle<Symbol> name = isolate->factory()->hidden_properties_symbol();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006047 SetOwnPropertyIgnoreAttributes(object, name, value, DONT_ENUM).Assert();
6048 return object;
6049}
6050
6051
Ben Murdoch097c5b22016-05-18 11:27:45 +01006052Maybe<bool> JSObject::DeletePropertyWithInterceptor(LookupIterator* it,
6053 ShouldThrow should_throw) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006054 Isolate* isolate = it->isolate();
6055 // Make sure that the top context does not change when doing callbacks or
6056 // interceptor calls.
6057 AssertNoContextChange ncc(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006058
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006059 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
6060 Handle<InterceptorInfo> interceptor(it->GetInterceptor());
6061 if (interceptor->deleter()->IsUndefined()) return Nothing<bool>();
6062
6063 Handle<JSObject> holder = it->GetHolder<JSObject>();
Ben Murdochda12d292016-06-02 14:46:10 +01006064 Handle<Object> receiver = it->GetReceiver();
6065 if (!receiver->IsJSReceiver()) {
6066 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, receiver,
6067 Object::ConvertReceiver(isolate, receiver),
6068 Nothing<bool>());
6069 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006070
Ben Murdochda12d292016-06-02 14:46:10 +01006071 PropertyCallbackArguments args(isolate, interceptor->data(), *receiver,
6072 *holder, should_throw);
6073 Handle<Object> result;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006074 if (it->IsElement()) {
6075 uint32_t index = it->index();
6076 v8::IndexedPropertyDeleterCallback deleter =
6077 v8::ToCData<v8::IndexedPropertyDeleterCallback>(interceptor->deleter());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006078 result = args.Call(deleter, index);
6079 } else if (it->name()->IsSymbol() && !interceptor->can_intercept_symbols()) {
6080 return Nothing<bool>();
6081 } else {
6082 Handle<Name> name = it->name();
6083 DCHECK(!name->IsPrivate());
6084 v8::GenericNamedPropertyDeleterCallback deleter =
6085 v8::ToCData<v8::GenericNamedPropertyDeleterCallback>(
6086 interceptor->deleter());
Ben Murdochda12d292016-06-02 14:46:10 +01006087 result = args.Call(deleter, name);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006088 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006089
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006090 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>());
Ben Murdochda12d292016-06-02 14:46:10 +01006091 if (result.is_null()) return Nothing<bool>();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006092
6093 DCHECK(result->IsBoolean());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006094 // Rebox CustomArguments::kReturnValueOffset before returning.
Ben Murdochda12d292016-06-02 14:46:10 +01006095 return Just(result->IsTrue());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006096}
6097
6098
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006099void JSReceiver::DeleteNormalizedProperty(Handle<JSReceiver> object,
6100 Handle<Name> name, int entry) {
6101 DCHECK(!object->HasFastProperties());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006102 Isolate* isolate = object->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006103
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006104 if (object->IsJSGlobalObject()) {
6105 // If we have a global object, invalidate the cell and swap in a new one.
6106 Handle<GlobalDictionary> dictionary(
6107 JSObject::cast(*object)->global_dictionary());
6108 DCHECK_NE(GlobalDictionary::kNotFound, entry);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006109
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006110 auto cell = PropertyCell::InvalidateEntry(dictionary, entry);
6111 cell->set_value(isolate->heap()->the_hole_value());
6112 // TODO(ishell): InvalidateForDelete
6113 cell->set_property_details(
6114 cell->property_details().set_cell_type(PropertyCellType::kInvalidated));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006115 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006116 Handle<NameDictionary> dictionary(object->property_dictionary());
6117 DCHECK_NE(NameDictionary::kNotFound, entry);
Steve Blocka7e24c12009-10-30 11:49:00 +00006118
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006119 NameDictionary::DeleteProperty(dictionary, entry);
6120 Handle<NameDictionary> new_properties =
6121 NameDictionary::Shrink(dictionary, name);
6122 object->set_properties(*new_properties);
Steve Blocka7e24c12009-10-30 11:49:00 +00006123 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006124}
6125
6126
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006127Maybe<bool> JSReceiver::DeleteProperty(LookupIterator* it,
6128 LanguageMode language_mode) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01006129 it->UpdateProtector();
6130
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006131 Isolate* isolate = it->isolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00006132
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006133 if (it->state() == LookupIterator::JSPROXY) {
6134 return JSProxy::DeletePropertyOrElement(it->GetHolder<JSProxy>(),
6135 it->GetName(), language_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00006136 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006137
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006138 if (it->GetReceiver()->IsJSProxy()) {
6139 if (it->state() != LookupIterator::NOT_FOUND) {
6140 DCHECK_EQ(LookupIterator::DATA, it->state());
Ben Murdoch097c5b22016-05-18 11:27:45 +01006141 DCHECK(it->name()->IsPrivate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006142 it->Delete();
6143 }
6144 return Just(true);
6145 }
6146 Handle<JSObject> receiver = Handle<JSObject>::cast(it->GetReceiver());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006147
Ben Murdoch097c5b22016-05-18 11:27:45 +01006148 bool is_observed = receiver->map()->is_observed() &&
6149 (it->IsElement() || !it->name()->IsPrivate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006150
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006151 Handle<Object> old_value = it->factory()->the_hole_value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006152
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006153 for (; it->IsFound(); it->Next()) {
6154 switch (it->state()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006155 case LookupIterator::JSPROXY:
6156 case LookupIterator::NOT_FOUND:
6157 case LookupIterator::TRANSITION:
6158 UNREACHABLE();
6159 case LookupIterator::ACCESS_CHECK:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006160 if (it->HasAccess()) break;
6161 isolate->ReportFailedAccessCheck(it->GetHolder<JSObject>());
6162 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>());
6163 return Just(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006164 case LookupIterator::INTERCEPTOR: {
Ben Murdoch097c5b22016-05-18 11:27:45 +01006165 ShouldThrow should_throw =
6166 is_sloppy(language_mode) ? DONT_THROW : THROW_ON_ERROR;
6167 Maybe<bool> result =
6168 JSObject::DeletePropertyWithInterceptor(it, should_throw);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006169 // An exception was thrown in the interceptor. Propagate.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006170 if (isolate->has_pending_exception()) return Nothing<bool>();
6171 // Delete with interceptor succeeded. Return result.
6172 // TODO(neis): In strict mode, we should probably throw if the
6173 // interceptor returns false.
6174 if (result.IsJust()) return result;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006175 break;
6176 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006177 case LookupIterator::INTEGER_INDEXED_EXOTIC:
6178 return Just(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006179 case LookupIterator::DATA:
6180 if (is_observed) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006181 old_value = it->GetDataValue();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006182 }
6183 // Fall through.
6184 case LookupIterator::ACCESSOR: {
Ben Murdochda12d292016-06-02 14:46:10 +01006185 if (!it->IsConfigurable()) {
6186 // Fail if the property is not configurable.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006187 if (is_strict(language_mode)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006188 isolate->Throw(*isolate->factory()->NewTypeError(
Ben Murdochda12d292016-06-02 14:46:10 +01006189 MessageTemplate::kStrictDeleteProperty, it->GetName(),
6190 receiver));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006191 return Nothing<bool>();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006192 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006193 return Just(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006194 }
6195
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006196 it->Delete();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006197
6198 if (is_observed) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006199 RETURN_ON_EXCEPTION_VALUE(
6200 isolate, JSObject::EnqueueChangeRecord(receiver, "delete",
6201 it->GetName(), old_value),
6202 Nothing<bool>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006203 }
6204
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006205 return Just(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006206 }
6207 }
6208 }
6209
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006210 return Just(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00006211}
6212
6213
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006214Maybe<bool> JSReceiver::DeleteElement(Handle<JSReceiver> object, uint32_t index,
6215 LanguageMode language_mode) {
Ben Murdochda12d292016-06-02 14:46:10 +01006216 LookupIterator it(object->GetIsolate(), object, index, object,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006217 LookupIterator::HIDDEN);
6218 return DeleteProperty(&it, language_mode);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006219}
6220
6221
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006222Maybe<bool> JSReceiver::DeleteProperty(Handle<JSReceiver> object,
6223 Handle<Name> name,
6224 LanguageMode language_mode) {
Ben Murdochda12d292016-06-02 14:46:10 +01006225 LookupIterator it(object, name, object, LookupIterator::HIDDEN);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006226 return DeleteProperty(&it, language_mode);
6227}
6228
6229
6230Maybe<bool> JSReceiver::DeletePropertyOrElement(Handle<JSReceiver> object,
6231 Handle<Name> name,
6232 LanguageMode language_mode) {
6233 LookupIterator it = LookupIterator::PropertyOrElement(
Ben Murdochda12d292016-06-02 14:46:10 +01006234 name->GetIsolate(), object, name, object, LookupIterator::HIDDEN);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006235 return DeleteProperty(&it, language_mode);
6236}
6237
6238
6239// ES6 7.1.14
6240MaybeHandle<Object> ToPropertyKey(Isolate* isolate, Handle<Object> value) {
6241 // 1. Let key be ToPrimitive(argument, hint String).
6242 MaybeHandle<Object> maybe_key =
6243 Object::ToPrimitive(value, ToPrimitiveHint::kString);
6244 // 2. ReturnIfAbrupt(key).
6245 Handle<Object> key;
6246 if (!maybe_key.ToHandle(&key)) return key;
6247 // 3. If Type(key) is Symbol, then return key.
6248 if (key->IsSymbol()) return key;
6249 // 4. Return ToString(key).
6250 // Extending spec'ed behavior, we'd be happy to return an element index.
6251 if (key->IsSmi()) return key;
6252 if (key->IsHeapNumber()) {
6253 uint32_t uint_value;
6254 if (value->ToArrayLength(&uint_value) &&
6255 uint_value <= static_cast<uint32_t>(Smi::kMaxValue)) {
6256 return handle(Smi::FromInt(static_cast<int>(uint_value)), isolate);
6257 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006258 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006259 return Object::ToString(isolate, key);
6260}
6261
6262
6263// ES6 19.1.2.4
6264// static
6265Object* JSReceiver::DefineProperty(Isolate* isolate, Handle<Object> object,
6266 Handle<Object> key,
6267 Handle<Object> attributes) {
6268 // 1. If Type(O) is not Object, throw a TypeError exception.
6269 if (!object->IsJSReceiver()) {
6270 Handle<String> fun_name =
6271 isolate->factory()->InternalizeUtf8String("Object.defineProperty");
6272 THROW_NEW_ERROR_RETURN_FAILURE(
6273 isolate, NewTypeError(MessageTemplate::kCalledOnNonObject, fun_name));
6274 }
6275 // 2. Let key be ToPropertyKey(P).
6276 // 3. ReturnIfAbrupt(key).
6277 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, key, ToPropertyKey(isolate, key));
6278 // 4. Let desc be ToPropertyDescriptor(Attributes).
6279 // 5. ReturnIfAbrupt(desc).
6280 PropertyDescriptor desc;
6281 if (!PropertyDescriptor::ToPropertyDescriptor(isolate, attributes, &desc)) {
6282 return isolate->heap()->exception();
6283 }
6284 // 6. Let success be DefinePropertyOrThrow(O,key, desc).
6285 Maybe<bool> success = DefineOwnProperty(
6286 isolate, Handle<JSReceiver>::cast(object), key, &desc, THROW_ON_ERROR);
6287 // 7. ReturnIfAbrupt(success).
6288 MAYBE_RETURN(success, isolate->heap()->exception());
6289 CHECK(success.FromJust());
6290 // 8. Return O.
6291 return *object;
6292}
6293
6294
6295// ES6 19.1.2.3.1
6296// static
6297MaybeHandle<Object> JSReceiver::DefineProperties(Isolate* isolate,
6298 Handle<Object> object,
6299 Handle<Object> properties) {
6300 // 1. If Type(O) is not Object, throw a TypeError exception.
6301 if (!object->IsJSReceiver()) {
6302 Handle<String> fun_name =
6303 isolate->factory()->InternalizeUtf8String("Object.defineProperties");
6304 THROW_NEW_ERROR(isolate,
6305 NewTypeError(MessageTemplate::kCalledOnNonObject, fun_name),
6306 Object);
6307 }
6308 // 2. Let props be ToObject(Properties).
6309 // 3. ReturnIfAbrupt(props).
6310 Handle<JSReceiver> props;
6311 if (!Object::ToObject(isolate, properties).ToHandle(&props)) {
6312 THROW_NEW_ERROR(isolate,
6313 NewTypeError(MessageTemplate::kUndefinedOrNullToObject),
6314 Object);
6315 }
6316 // 4. Let keys be props.[[OwnPropertyKeys]]().
6317 // 5. ReturnIfAbrupt(keys).
6318 Handle<FixedArray> keys;
6319 ASSIGN_RETURN_ON_EXCEPTION(
Ben Murdoch097c5b22016-05-18 11:27:45 +01006320 isolate, keys, JSReceiver::GetKeys(props, OWN_ONLY, ALL_PROPERTIES),
6321 Object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006322 // 6. Let descriptors be an empty List.
6323 int capacity = keys->length();
6324 std::vector<PropertyDescriptor> descriptors(capacity);
6325 size_t descriptors_index = 0;
6326 // 7. Repeat for each element nextKey of keys in List order,
6327 for (int i = 0; i < keys->length(); ++i) {
6328 Handle<Object> next_key(keys->get(i), isolate);
6329 // 7a. Let propDesc be props.[[GetOwnProperty]](nextKey).
6330 // 7b. ReturnIfAbrupt(propDesc).
6331 bool success = false;
6332 LookupIterator it = LookupIterator::PropertyOrElement(
6333 isolate, props, next_key, &success, LookupIterator::HIDDEN);
6334 DCHECK(success);
6335 Maybe<PropertyAttributes> maybe = JSReceiver::GetPropertyAttributes(&it);
6336 if (!maybe.IsJust()) return MaybeHandle<Object>();
6337 PropertyAttributes attrs = maybe.FromJust();
6338 // 7c. If propDesc is not undefined and propDesc.[[Enumerable]] is true:
6339 if (attrs == ABSENT) continue;
6340 if (attrs & DONT_ENUM) continue;
6341 // 7c i. Let descObj be Get(props, nextKey).
6342 // 7c ii. ReturnIfAbrupt(descObj).
6343 Handle<Object> desc_obj;
6344 ASSIGN_RETURN_ON_EXCEPTION(isolate, desc_obj, Object::GetProperty(&it),
6345 Object);
6346 // 7c iii. Let desc be ToPropertyDescriptor(descObj).
6347 success = PropertyDescriptor::ToPropertyDescriptor(
6348 isolate, desc_obj, &descriptors[descriptors_index]);
6349 // 7c iv. ReturnIfAbrupt(desc).
6350 if (!success) return MaybeHandle<Object>();
6351 // 7c v. Append the pair (a two element List) consisting of nextKey and
6352 // desc to the end of descriptors.
6353 descriptors[descriptors_index].set_name(next_key);
6354 descriptors_index++;
6355 }
6356 // 8. For each pair from descriptors in list order,
6357 for (size_t i = 0; i < descriptors_index; ++i) {
6358 PropertyDescriptor* desc = &descriptors[i];
6359 // 8a. Let P be the first element of pair.
6360 // 8b. Let desc be the second element of pair.
6361 // 8c. Let status be DefinePropertyOrThrow(O, P, desc).
6362 Maybe<bool> status =
6363 DefineOwnProperty(isolate, Handle<JSReceiver>::cast(object),
6364 desc->name(), desc, THROW_ON_ERROR);
6365 // 8d. ReturnIfAbrupt(status).
6366 if (!status.IsJust()) return MaybeHandle<Object>();
6367 CHECK(status.FromJust());
6368 }
6369 // 9. Return o.
6370 return object;
6371}
6372
6373
6374// static
6375Maybe<bool> JSReceiver::DefineOwnProperty(Isolate* isolate,
6376 Handle<JSReceiver> object,
6377 Handle<Object> key,
6378 PropertyDescriptor* desc,
6379 ShouldThrow should_throw) {
6380 if (object->IsJSArray()) {
6381 return JSArray::DefineOwnProperty(isolate, Handle<JSArray>::cast(object),
6382 key, desc, should_throw);
6383 }
6384 if (object->IsJSProxy()) {
6385 return JSProxy::DefineOwnProperty(isolate, Handle<JSProxy>::cast(object),
6386 key, desc, should_throw);
6387 }
6388 // TODO(jkummerow): Support Modules (ES6 9.4.6.6)
6389
6390 // OrdinaryDefineOwnProperty, by virtue of calling
6391 // DefineOwnPropertyIgnoreAttributes, can handle arguments (ES6 9.4.4.2)
6392 // and IntegerIndexedExotics (ES6 9.4.5.3), with one exception:
6393 // TODO(jkummerow): Setting an indexed accessor on a typed array should throw.
6394 return OrdinaryDefineOwnProperty(isolate, Handle<JSObject>::cast(object), key,
6395 desc, should_throw);
6396}
6397
6398
6399// static
6400Maybe<bool> JSReceiver::OrdinaryDefineOwnProperty(Isolate* isolate,
6401 Handle<JSObject> object,
6402 Handle<Object> key,
6403 PropertyDescriptor* desc,
6404 ShouldThrow should_throw) {
6405 bool success = false;
6406 DCHECK(key->IsName() || key->IsNumber()); // |key| is a PropertyKey...
6407 LookupIterator it = LookupIterator::PropertyOrElement(
6408 isolate, object, key, &success, LookupIterator::HIDDEN);
6409 DCHECK(success); // ...so creating a LookupIterator can't fail.
6410
6411 // Deal with access checks first.
6412 if (it.state() == LookupIterator::ACCESS_CHECK) {
6413 if (!it.HasAccess()) {
6414 isolate->ReportFailedAccessCheck(it.GetHolder<JSObject>());
6415 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>());
6416 return Just(true);
6417 }
6418 it.Next();
6419 }
6420
6421 return OrdinaryDefineOwnProperty(&it, desc, should_throw);
6422}
6423
6424
6425// ES6 9.1.6.1
6426// static
6427Maybe<bool> JSReceiver::OrdinaryDefineOwnProperty(LookupIterator* it,
6428 PropertyDescriptor* desc,
6429 ShouldThrow should_throw) {
6430 Isolate* isolate = it->isolate();
6431 // 1. Let current be O.[[GetOwnProperty]](P).
6432 // 2. ReturnIfAbrupt(current).
6433 PropertyDescriptor current;
6434 MAYBE_RETURN(GetOwnPropertyDescriptor(it, &current), Nothing<bool>());
6435
6436 // TODO(jkummerow/verwaest): It would be nice if we didn't have to reset
6437 // the iterator every time. Currently, the reasons why we need it are:
6438 // - handle interceptors correctly
6439 // - handle accessors correctly (which might change the holder's map)
6440 it->Restart();
6441 // 3. Let extensible be the value of the [[Extensible]] internal slot of O.
6442 Handle<JSObject> object = Handle<JSObject>::cast(it->GetReceiver());
6443 bool extensible = JSObject::IsExtensible(object);
6444
6445 return ValidateAndApplyPropertyDescriptor(isolate, it, extensible, desc,
6446 &current, should_throw);
6447}
6448
6449
6450// ES6 9.1.6.2
6451// static
6452Maybe<bool> JSReceiver::IsCompatiblePropertyDescriptor(
6453 Isolate* isolate, bool extensible, PropertyDescriptor* desc,
6454 PropertyDescriptor* current, Handle<Name> property_name,
6455 ShouldThrow should_throw) {
6456 // 1. Return ValidateAndApplyPropertyDescriptor(undefined, undefined,
6457 // Extensible, Desc, Current).
6458 return ValidateAndApplyPropertyDescriptor(
6459 isolate, NULL, extensible, desc, current, should_throw, property_name);
6460}
6461
6462
6463// ES6 9.1.6.3
6464// static
6465Maybe<bool> JSReceiver::ValidateAndApplyPropertyDescriptor(
6466 Isolate* isolate, LookupIterator* it, bool extensible,
6467 PropertyDescriptor* desc, PropertyDescriptor* current,
6468 ShouldThrow should_throw, Handle<Name> property_name) {
6469 // We either need a LookupIterator, or a property name.
6470 DCHECK((it == NULL) != property_name.is_null());
6471 Handle<JSObject> object;
6472 if (it != NULL) object = Handle<JSObject>::cast(it->GetReceiver());
6473 bool desc_is_data_descriptor = PropertyDescriptor::IsDataDescriptor(desc);
6474 bool desc_is_accessor_descriptor =
6475 PropertyDescriptor::IsAccessorDescriptor(desc);
6476 bool desc_is_generic_descriptor =
6477 PropertyDescriptor::IsGenericDescriptor(desc);
6478 // 1. (Assert)
6479 // 2. If current is undefined, then
6480 if (current->is_empty()) {
6481 // 2a. If extensible is false, return false.
6482 if (!extensible) {
6483 RETURN_FAILURE(isolate, should_throw,
6484 NewTypeError(MessageTemplate::kDefineDisallowed,
6485 it != NULL ? it->GetName() : property_name));
6486 }
6487 // 2c. If IsGenericDescriptor(Desc) or IsDataDescriptor(Desc) is true, then:
6488 // (This is equivalent to !IsAccessorDescriptor(desc).)
6489 DCHECK((desc_is_generic_descriptor || desc_is_data_descriptor) ==
6490 !desc_is_accessor_descriptor);
6491 if (!desc_is_accessor_descriptor) {
6492 // 2c i. If O is not undefined, create an own data property named P of
6493 // object O whose [[Value]], [[Writable]], [[Enumerable]] and
6494 // [[Configurable]] attribute values are described by Desc. If the value
6495 // of an attribute field of Desc is absent, the attribute of the newly
6496 // created property is set to its default value.
6497 if (it != NULL) {
6498 if (!desc->has_writable()) desc->set_writable(false);
6499 if (!desc->has_enumerable()) desc->set_enumerable(false);
6500 if (!desc->has_configurable()) desc->set_configurable(false);
6501 Handle<Object> value(
6502 desc->has_value()
6503 ? desc->value()
6504 : Handle<Object>::cast(isolate->factory()->undefined_value()));
6505 MaybeHandle<Object> result =
Ben Murdoch097c5b22016-05-18 11:27:45 +01006506 JSObject::DefineOwnPropertyIgnoreAttributes(it, value,
6507 desc->ToAttributes());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006508 if (result.is_null()) return Nothing<bool>();
6509 }
6510 } else {
6511 // 2d. Else Desc must be an accessor Property Descriptor,
6512 DCHECK(desc_is_accessor_descriptor);
6513 // 2d i. If O is not undefined, create an own accessor property named P
6514 // of object O whose [[Get]], [[Set]], [[Enumerable]] and
6515 // [[Configurable]] attribute values are described by Desc. If the value
6516 // of an attribute field of Desc is absent, the attribute of the newly
6517 // created property is set to its default value.
6518 if (it != NULL) {
6519 if (!desc->has_enumerable()) desc->set_enumerable(false);
6520 if (!desc->has_configurable()) desc->set_configurable(false);
6521 Handle<Object> getter(
6522 desc->has_get()
6523 ? desc->get()
6524 : Handle<Object>::cast(isolate->factory()->null_value()));
6525 Handle<Object> setter(
6526 desc->has_set()
6527 ? desc->set()
6528 : Handle<Object>::cast(isolate->factory()->null_value()));
6529 MaybeHandle<Object> result =
6530 JSObject::DefineAccessor(it, getter, setter, desc->ToAttributes());
6531 if (result.is_null()) return Nothing<bool>();
6532 }
6533 }
6534 // 2e. Return true.
6535 return Just(true);
6536 }
6537 // 3. Return true, if every field in Desc is absent.
6538 // 4. Return true, if every field in Desc also occurs in current and the
6539 // value of every field in Desc is the same value as the corresponding field
6540 // in current when compared using the SameValue algorithm.
6541 if ((!desc->has_enumerable() ||
6542 desc->enumerable() == current->enumerable()) &&
6543 (!desc->has_configurable() ||
6544 desc->configurable() == current->configurable()) &&
6545 (!desc->has_value() ||
6546 (current->has_value() && current->value()->SameValue(*desc->value()))) &&
6547 (!desc->has_writable() ||
6548 (current->has_writable() && current->writable() == desc->writable())) &&
6549 (!desc->has_get() ||
6550 (current->has_get() && current->get()->SameValue(*desc->get()))) &&
6551 (!desc->has_set() ||
6552 (current->has_set() && current->set()->SameValue(*desc->set())))) {
6553 return Just(true);
6554 }
6555 // 5. If the [[Configurable]] field of current is false, then
6556 if (!current->configurable()) {
6557 // 5a. Return false, if the [[Configurable]] field of Desc is true.
6558 if (desc->has_configurable() && desc->configurable()) {
6559 RETURN_FAILURE(isolate, should_throw,
6560 NewTypeError(MessageTemplate::kRedefineDisallowed,
6561 it != NULL ? it->GetName() : property_name));
6562 }
6563 // 5b. Return false, if the [[Enumerable]] field of Desc is present and the
6564 // [[Enumerable]] fields of current and Desc are the Boolean negation of
6565 // each other.
6566 if (desc->has_enumerable() && desc->enumerable() != current->enumerable()) {
6567 RETURN_FAILURE(isolate, should_throw,
6568 NewTypeError(MessageTemplate::kRedefineDisallowed,
6569 it != NULL ? it->GetName() : property_name));
6570 }
6571 }
6572
6573 bool current_is_data_descriptor =
6574 PropertyDescriptor::IsDataDescriptor(current);
6575 // 6. If IsGenericDescriptor(Desc) is true, no further validation is required.
6576 if (desc_is_generic_descriptor) {
6577 // Nothing to see here.
6578
6579 // 7. Else if IsDataDescriptor(current) and IsDataDescriptor(Desc) have
6580 // different results, then:
6581 } else if (current_is_data_descriptor != desc_is_data_descriptor) {
6582 // 7a. Return false, if the [[Configurable]] field of current is false.
6583 if (!current->configurable()) {
6584 RETURN_FAILURE(isolate, should_throw,
6585 NewTypeError(MessageTemplate::kRedefineDisallowed,
6586 it != NULL ? it->GetName() : property_name));
6587 }
6588 // 7b. If IsDataDescriptor(current) is true, then:
6589 if (current_is_data_descriptor) {
6590 // 7b i. If O is not undefined, convert the property named P of object O
6591 // from a data property to an accessor property. Preserve the existing
6592 // values of the converted property's [[Configurable]] and [[Enumerable]]
6593 // attributes and set the rest of the property's attributes to their
6594 // default values.
6595 // --> Folded into step 10.
6596 } else {
6597 // 7c i. If O is not undefined, convert the property named P of object O
6598 // from an accessor property to a data property. Preserve the existing
6599 // values of the converted property’s [[Configurable]] and [[Enumerable]]
6600 // attributes and set the rest of the property’s attributes to their
6601 // default values.
6602 // --> Folded into step 10.
6603 }
6604
6605 // 8. Else if IsDataDescriptor(current) and IsDataDescriptor(Desc) are both
6606 // true, then:
6607 } else if (current_is_data_descriptor && desc_is_data_descriptor) {
6608 // 8a. If the [[Configurable]] field of current is false, then:
6609 if (!current->configurable()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006610 // 8a i. Return false, if the [[Writable]] field of current is false and
6611 // the [[Writable]] field of Desc is true.
6612 if (!current->writable() && desc->has_writable() && desc->writable()) {
6613 RETURN_FAILURE(
6614 isolate, should_throw,
6615 NewTypeError(MessageTemplate::kRedefineDisallowed,
6616 it != NULL ? it->GetName() : property_name));
6617 }
6618 // 8a ii. If the [[Writable]] field of current is false, then:
6619 if (!current->writable()) {
6620 // 8a ii 1. Return false, if the [[Value]] field of Desc is present and
6621 // SameValue(Desc.[[Value]], current.[[Value]]) is false.
6622 if (desc->has_value() && !desc->value()->SameValue(*current->value())) {
6623 RETURN_FAILURE(
6624 isolate, should_throw,
6625 NewTypeError(MessageTemplate::kRedefineDisallowed,
6626 it != NULL ? it->GetName() : property_name));
6627 }
6628 }
6629 }
6630 } else {
6631 // 9. Else IsAccessorDescriptor(current) and IsAccessorDescriptor(Desc)
6632 // are both true,
6633 DCHECK(PropertyDescriptor::IsAccessorDescriptor(current) &&
6634 desc_is_accessor_descriptor);
6635 // 9a. If the [[Configurable]] field of current is false, then:
6636 if (!current->configurable()) {
6637 // 9a i. Return false, if the [[Set]] field of Desc is present and
6638 // SameValue(Desc.[[Set]], current.[[Set]]) is false.
6639 if (desc->has_set() && !desc->set()->SameValue(*current->set())) {
6640 RETURN_FAILURE(
6641 isolate, should_throw,
6642 NewTypeError(MessageTemplate::kRedefineDisallowed,
6643 it != NULL ? it->GetName() : property_name));
6644 }
6645 // 9a ii. Return false, if the [[Get]] field of Desc is present and
6646 // SameValue(Desc.[[Get]], current.[[Get]]) is false.
6647 if (desc->has_get() && !desc->get()->SameValue(*current->get())) {
6648 RETURN_FAILURE(
6649 isolate, should_throw,
6650 NewTypeError(MessageTemplate::kRedefineDisallowed,
6651 it != NULL ? it->GetName() : property_name));
6652 }
6653 }
6654 }
6655
6656 // 10. If O is not undefined, then:
6657 if (it != NULL) {
6658 // 10a. For each field of Desc that is present, set the corresponding
6659 // attribute of the property named P of object O to the value of the field.
6660 PropertyAttributes attrs = NONE;
6661
6662 if (desc->has_enumerable()) {
6663 attrs = static_cast<PropertyAttributes>(
6664 attrs | (desc->enumerable() ? NONE : DONT_ENUM));
6665 } else {
6666 attrs = static_cast<PropertyAttributes>(
6667 attrs | (current->enumerable() ? NONE : DONT_ENUM));
6668 }
6669 if (desc->has_configurable()) {
6670 attrs = static_cast<PropertyAttributes>(
6671 attrs | (desc->configurable() ? NONE : DONT_DELETE));
6672 } else {
6673 attrs = static_cast<PropertyAttributes>(
6674 attrs | (current->configurable() ? NONE : DONT_DELETE));
6675 }
6676 if (desc_is_data_descriptor ||
6677 (desc_is_generic_descriptor && current_is_data_descriptor)) {
6678 if (desc->has_writable()) {
6679 attrs = static_cast<PropertyAttributes>(
6680 attrs | (desc->writable() ? NONE : READ_ONLY));
6681 } else {
6682 attrs = static_cast<PropertyAttributes>(
6683 attrs | (current->writable() ? NONE : READ_ONLY));
6684 }
6685 Handle<Object> value(
6686 desc->has_value() ? desc->value()
6687 : current->has_value()
6688 ? current->value()
6689 : Handle<Object>::cast(
6690 isolate->factory()->undefined_value()));
Ben Murdoch097c5b22016-05-18 11:27:45 +01006691 MaybeHandle<Object> result =
6692 JSObject::DefineOwnPropertyIgnoreAttributes(it, value, attrs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006693 if (result.is_null()) return Nothing<bool>();
6694 } else {
6695 DCHECK(desc_is_accessor_descriptor ||
6696 (desc_is_generic_descriptor &&
6697 PropertyDescriptor::IsAccessorDescriptor(current)));
6698 Handle<Object> getter(
6699 desc->has_get()
6700 ? desc->get()
6701 : current->has_get()
6702 ? current->get()
6703 : Handle<Object>::cast(isolate->factory()->null_value()));
6704 Handle<Object> setter(
6705 desc->has_set()
6706 ? desc->set()
6707 : current->has_set()
6708 ? current->set()
6709 : Handle<Object>::cast(isolate->factory()->null_value()));
6710 MaybeHandle<Object> result =
6711 JSObject::DefineAccessor(it, getter, setter, attrs);
6712 if (result.is_null()) return Nothing<bool>();
6713 }
6714 }
6715
6716 // 11. Return true.
6717 return Just(true);
6718}
6719
6720
6721// static
6722Maybe<bool> JSReceiver::CreateDataProperty(LookupIterator* it,
6723 Handle<Object> value,
6724 ShouldThrow should_throw) {
6725 DCHECK(!it->check_prototype_chain());
6726 Handle<JSReceiver> receiver = Handle<JSReceiver>::cast(it->GetReceiver());
6727 Isolate* isolate = receiver->GetIsolate();
6728
6729 if (receiver->IsJSObject()) {
Ben Murdochda12d292016-06-02 14:46:10 +01006730 return JSObject::CreateDataProperty(it, value, should_throw); // Shortcut.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006731 }
6732
6733 PropertyDescriptor new_desc;
6734 new_desc.set_value(value);
6735 new_desc.set_writable(true);
6736 new_desc.set_enumerable(true);
6737 new_desc.set_configurable(true);
6738
6739 return JSReceiver::DefineOwnProperty(isolate, receiver, it->GetName(),
6740 &new_desc, should_throw);
6741}
6742
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006743Maybe<bool> JSObject::CreateDataProperty(LookupIterator* it,
Ben Murdochda12d292016-06-02 14:46:10 +01006744 Handle<Object> value,
6745 ShouldThrow should_throw) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006746 DCHECK(it->GetReceiver()->IsJSObject());
6747 MAYBE_RETURN(JSReceiver::GetPropertyAttributes(it), Nothing<bool>());
Ben Murdochda12d292016-06-02 14:46:10 +01006748 Handle<JSReceiver> receiver = Handle<JSReceiver>::cast(it->GetReceiver());
6749 Isolate* isolate = receiver->GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006750
6751 if (it->IsFound()) {
Ben Murdochda12d292016-06-02 14:46:10 +01006752 Maybe<PropertyAttributes> attributes = GetPropertyAttributes(it);
6753 MAYBE_RETURN(attributes, Nothing<bool>());
6754 if ((attributes.FromJust() & DONT_DELETE) != 0) {
6755 RETURN_FAILURE(
6756 isolate, should_throw,
6757 NewTypeError(MessageTemplate::kRedefineDisallowed, it->GetName()));
6758 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006759 } else {
Ben Murdochda12d292016-06-02 14:46:10 +01006760 if (!JSObject::IsExtensible(Handle<JSObject>::cast(it->GetReceiver()))) {
6761 RETURN_FAILURE(
6762 isolate, should_throw,
6763 NewTypeError(MessageTemplate::kDefineDisallowed, it->GetName()));
6764 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006765 }
6766
Ben Murdoch097c5b22016-05-18 11:27:45 +01006767 RETURN_ON_EXCEPTION_VALUE(it->isolate(),
6768 DefineOwnPropertyIgnoreAttributes(it, value, NONE),
6769 Nothing<bool>());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006770
6771 return Just(true);
6772}
6773
6774
6775// TODO(jkummerow): Consider unification with FastAsArrayLength() in
6776// accessors.cc.
6777bool PropertyKeyToArrayLength(Handle<Object> value, uint32_t* length) {
6778 DCHECK(value->IsNumber() || value->IsName());
6779 if (value->ToArrayLength(length)) return true;
6780 if (value->IsString()) return String::cast(*value)->AsArrayIndex(length);
6781 return false;
6782}
6783
6784
6785bool PropertyKeyToArrayIndex(Handle<Object> index_obj, uint32_t* output) {
6786 return PropertyKeyToArrayLength(index_obj, output) && *output != kMaxUInt32;
6787}
6788
6789
6790// ES6 9.4.2.1
6791// static
6792Maybe<bool> JSArray::DefineOwnProperty(Isolate* isolate, Handle<JSArray> o,
6793 Handle<Object> name,
6794 PropertyDescriptor* desc,
6795 ShouldThrow should_throw) {
6796 // 1. Assert: IsPropertyKey(P) is true. ("P" is |name|.)
6797 // 2. If P is "length", then:
6798 // TODO(jkummerow): Check if we need slow string comparison.
6799 if (*name == isolate->heap()->length_string()) {
6800 // 2a. Return ArraySetLength(A, Desc).
6801 return ArraySetLength(isolate, o, desc, should_throw);
6802 }
6803 // 3. Else if P is an array index, then:
6804 uint32_t index = 0;
6805 if (PropertyKeyToArrayIndex(name, &index)) {
6806 // 3a. Let oldLenDesc be OrdinaryGetOwnProperty(A, "length").
6807 PropertyDescriptor old_len_desc;
6808 Maybe<bool> success = GetOwnPropertyDescriptor(
6809 isolate, o, isolate->factory()->length_string(), &old_len_desc);
6810 // 3b. (Assert)
6811 DCHECK(success.FromJust());
6812 USE(success);
6813 // 3c. Let oldLen be oldLenDesc.[[Value]].
6814 uint32_t old_len = 0;
6815 CHECK(old_len_desc.value()->ToArrayLength(&old_len));
6816 // 3d. Let index be ToUint32(P).
6817 // (Already done above.)
6818 // 3e. (Assert)
6819 // 3f. If index >= oldLen and oldLenDesc.[[Writable]] is false,
6820 // return false.
6821 if (index >= old_len && old_len_desc.has_writable() &&
6822 !old_len_desc.writable()) {
6823 RETURN_FAILURE(isolate, should_throw,
6824 NewTypeError(MessageTemplate::kDefineDisallowed, name));
6825 }
6826 // 3g. Let succeeded be OrdinaryDefineOwnProperty(A, P, Desc).
6827 Maybe<bool> succeeded =
6828 OrdinaryDefineOwnProperty(isolate, o, name, desc, should_throw);
6829 // 3h. Assert: succeeded is not an abrupt completion.
6830 // In our case, if should_throw == THROW_ON_ERROR, it can be!
6831 // 3i. If succeeded is false, return false.
6832 if (succeeded.IsNothing() || !succeeded.FromJust()) return succeeded;
6833 // 3j. If index >= oldLen, then:
6834 if (index >= old_len) {
6835 // 3j i. Set oldLenDesc.[[Value]] to index + 1.
6836 old_len_desc.set_value(isolate->factory()->NewNumberFromUint(index + 1));
6837 // 3j ii. Let succeeded be
6838 // OrdinaryDefineOwnProperty(A, "length", oldLenDesc).
6839 succeeded = OrdinaryDefineOwnProperty(isolate, o,
6840 isolate->factory()->length_string(),
6841 &old_len_desc, should_throw);
6842 // 3j iii. Assert: succeeded is true.
6843 DCHECK(succeeded.FromJust());
6844 USE(succeeded);
6845 }
6846 // 3k. Return true.
6847 return Just(true);
6848 }
6849
6850 // 4. Return OrdinaryDefineOwnProperty(A, P, Desc).
6851 return OrdinaryDefineOwnProperty(isolate, o, name, desc, should_throw);
6852}
6853
6854
6855// Part of ES6 9.4.2.4 ArraySetLength.
6856// static
6857bool JSArray::AnythingToArrayLength(Isolate* isolate,
6858 Handle<Object> length_object,
6859 uint32_t* output) {
6860 // Fast path: check numbers and strings that can be converted directly
6861 // and unobservably.
6862 if (length_object->ToArrayLength(output)) return true;
6863 if (length_object->IsString() &&
6864 Handle<String>::cast(length_object)->AsArrayIndex(output)) {
6865 return true;
6866 }
6867 // Slow path: follow steps in ES6 9.4.2.4 "ArraySetLength".
6868 // 3. Let newLen be ToUint32(Desc.[[Value]]).
6869 Handle<Object> uint32_v;
6870 if (!Object::ToUint32(isolate, length_object).ToHandle(&uint32_v)) {
6871 // 4. ReturnIfAbrupt(newLen).
6872 return false;
6873 }
6874 // 5. Let numberLen be ToNumber(Desc.[[Value]]).
6875 Handle<Object> number_v;
6876 if (!Object::ToNumber(length_object).ToHandle(&number_v)) {
6877 // 6. ReturnIfAbrupt(newLen).
6878 return false;
6879 }
6880 // 7. If newLen != numberLen, throw a RangeError exception.
6881 if (uint32_v->Number() != number_v->Number()) {
6882 Handle<Object> exception =
6883 isolate->factory()->NewRangeError(MessageTemplate::kInvalidArrayLength);
6884 isolate->Throw(*exception);
6885 return false;
6886 }
6887 CHECK(uint32_v->ToArrayLength(output));
6888 return true;
6889}
6890
6891
6892// ES6 9.4.2.4
6893// static
6894Maybe<bool> JSArray::ArraySetLength(Isolate* isolate, Handle<JSArray> a,
6895 PropertyDescriptor* desc,
6896 ShouldThrow should_throw) {
6897 // 1. If the [[Value]] field of Desc is absent, then
6898 if (!desc->has_value()) {
6899 // 1a. Return OrdinaryDefineOwnProperty(A, "length", Desc).
6900 return OrdinaryDefineOwnProperty(
6901 isolate, a, isolate->factory()->length_string(), desc, should_throw);
6902 }
6903 // 2. Let newLenDesc be a copy of Desc.
6904 // (Actual copying is not necessary.)
6905 PropertyDescriptor* new_len_desc = desc;
6906 // 3. - 7. Convert Desc.[[Value]] to newLen.
6907 uint32_t new_len = 0;
6908 if (!AnythingToArrayLength(isolate, desc->value(), &new_len)) {
6909 DCHECK(isolate->has_pending_exception());
6910 return Nothing<bool>();
6911 }
6912 // 8. Set newLenDesc.[[Value]] to newLen.
6913 // (Done below, if needed.)
6914 // 9. Let oldLenDesc be OrdinaryGetOwnProperty(A, "length").
6915 PropertyDescriptor old_len_desc;
6916 Maybe<bool> success = GetOwnPropertyDescriptor(
6917 isolate, a, isolate->factory()->length_string(), &old_len_desc);
6918 // 10. (Assert)
6919 DCHECK(success.FromJust());
6920 USE(success);
6921 // 11. Let oldLen be oldLenDesc.[[Value]].
6922 uint32_t old_len = 0;
6923 CHECK(old_len_desc.value()->ToArrayLength(&old_len));
6924 // 12. If newLen >= oldLen, then
6925 if (new_len >= old_len) {
6926 // 8. Set newLenDesc.[[Value]] to newLen.
6927 // 12a. Return OrdinaryDefineOwnProperty(A, "length", newLenDesc).
6928 new_len_desc->set_value(isolate->factory()->NewNumberFromUint(new_len));
6929 return OrdinaryDefineOwnProperty(isolate, a,
6930 isolate->factory()->length_string(),
6931 new_len_desc, should_throw);
6932 }
6933 // 13. If oldLenDesc.[[Writable]] is false, return false.
6934 if (!old_len_desc.writable()) {
6935 RETURN_FAILURE(isolate, should_throw,
6936 NewTypeError(MessageTemplate::kRedefineDisallowed,
6937 isolate->factory()->length_string()));
6938 }
6939 // 14. If newLenDesc.[[Writable]] is absent or has the value true,
6940 // let newWritable be true.
6941 bool new_writable = false;
6942 if (!new_len_desc->has_writable() || new_len_desc->writable()) {
6943 new_writable = true;
6944 } else {
6945 // 15. Else,
6946 // 15a. Need to defer setting the [[Writable]] attribute to false in case
6947 // any elements cannot be deleted.
6948 // 15b. Let newWritable be false. (It's initialized as "false" anyway.)
6949 // 15c. Set newLenDesc.[[Writable]] to true.
6950 // (Not needed.)
6951 }
6952 // Most of steps 16 through 19 is implemented by JSArray::SetLength.
6953 if (JSArray::ObservableSetLength(a, new_len).is_null()) {
6954 DCHECK(isolate->has_pending_exception());
6955 return Nothing<bool>();
6956 }
6957 // Steps 19d-ii, 20.
6958 if (!new_writable) {
6959 PropertyDescriptor readonly;
6960 readonly.set_writable(false);
6961 Maybe<bool> success = OrdinaryDefineOwnProperty(
6962 isolate, a, isolate->factory()->length_string(), &readonly,
6963 should_throw);
6964 DCHECK(success.FromJust());
6965 USE(success);
6966 }
6967 uint32_t actual_new_len = 0;
6968 CHECK(a->length()->ToArrayLength(&actual_new_len));
6969 // Steps 19d-v, 21. Return false if there were non-deletable elements.
6970 bool result = actual_new_len == new_len;
6971 if (!result) {
6972 RETURN_FAILURE(
6973 isolate, should_throw,
6974 NewTypeError(MessageTemplate::kStrictDeleteProperty,
6975 isolate->factory()->NewNumberFromUint(actual_new_len - 1),
6976 a));
6977 }
6978 return Just(result);
6979}
6980
6981
6982// ES6 9.5.6
6983// static
6984Maybe<bool> JSProxy::DefineOwnProperty(Isolate* isolate, Handle<JSProxy> proxy,
6985 Handle<Object> key,
6986 PropertyDescriptor* desc,
6987 ShouldThrow should_throw) {
6988 STACK_CHECK(Nothing<bool>());
6989 if (key->IsSymbol() && Handle<Symbol>::cast(key)->IsPrivate()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01006990 return SetPrivateProperty(isolate, proxy, Handle<Symbol>::cast(key), desc,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006991 should_throw);
6992 }
6993 Handle<String> trap_name = isolate->factory()->defineProperty_string();
6994 // 1. Assert: IsPropertyKey(P) is true.
6995 DCHECK(key->IsName() || key->IsNumber());
6996 // 2. Let handler be the value of the [[ProxyHandler]] internal slot of O.
6997 Handle<Object> handler(proxy->handler(), isolate);
6998 // 3. If handler is null, throw a TypeError exception.
6999 // 4. Assert: Type(handler) is Object.
7000 if (proxy->IsRevoked()) {
7001 isolate->Throw(*isolate->factory()->NewTypeError(
7002 MessageTemplate::kProxyRevoked, trap_name));
7003 return Nothing<bool>();
7004 }
7005 // 5. Let target be the value of the [[ProxyTarget]] internal slot of O.
7006 Handle<JSReceiver> target(proxy->target(), isolate);
7007 // 6. Let trap be ? GetMethod(handler, "defineProperty").
7008 Handle<Object> trap;
7009 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
7010 isolate, trap,
7011 Object::GetMethod(Handle<JSReceiver>::cast(handler), trap_name),
7012 Nothing<bool>());
7013 // 7. If trap is undefined, then:
7014 if (trap->IsUndefined()) {
7015 // 7a. Return target.[[DefineOwnProperty]](P, Desc).
7016 return JSReceiver::DefineOwnProperty(isolate, target, key, desc,
7017 should_throw);
7018 }
7019 // 8. Let descObj be FromPropertyDescriptor(Desc).
7020 Handle<Object> desc_obj = desc->ToObject(isolate);
7021 // 9. Let booleanTrapResult be
7022 // ToBoolean(? Call(trap, handler, «target, P, descObj»)).
7023 Handle<Name> property_name =
7024 key->IsName()
7025 ? Handle<Name>::cast(key)
7026 : Handle<Name>::cast(isolate->factory()->NumberToString(key));
7027 // Do not leak private property names.
7028 DCHECK(!property_name->IsPrivate());
7029 Handle<Object> trap_result_obj;
7030 Handle<Object> args[] = {target, property_name, desc_obj};
7031 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
7032 isolate, trap_result_obj,
7033 Execution::Call(isolate, trap, handler, arraysize(args), args),
7034 Nothing<bool>());
7035 // 10. If booleanTrapResult is false, return false.
7036 if (!trap_result_obj->BooleanValue()) {
7037 RETURN_FAILURE(isolate, should_throw,
7038 NewTypeError(MessageTemplate::kProxyTrapReturnedFalsishFor,
7039 trap_name, property_name));
7040 }
7041 // 11. Let targetDesc be ? target.[[GetOwnProperty]](P).
7042 PropertyDescriptor target_desc;
7043 Maybe<bool> target_found =
7044 JSReceiver::GetOwnPropertyDescriptor(isolate, target, key, &target_desc);
7045 MAYBE_RETURN(target_found, Nothing<bool>());
7046 // 12. Let extensibleTarget be ? IsExtensible(target).
7047 Maybe<bool> maybe_extensible = JSReceiver::IsExtensible(target);
7048 MAYBE_RETURN(maybe_extensible, Nothing<bool>());
7049 bool extensible_target = maybe_extensible.FromJust();
7050 // 13. If Desc has a [[Configurable]] field and if Desc.[[Configurable]]
7051 // is false, then:
7052 // 13a. Let settingConfigFalse be true.
7053 // 14. Else let settingConfigFalse be false.
7054 bool setting_config_false = desc->has_configurable() && !desc->configurable();
7055 // 15. If targetDesc is undefined, then
7056 if (!target_found.FromJust()) {
7057 // 15a. If extensibleTarget is false, throw a TypeError exception.
7058 if (!extensible_target) {
7059 isolate->Throw(*isolate->factory()->NewTypeError(
7060 MessageTemplate::kProxyDefinePropertyNonExtensible, property_name));
7061 return Nothing<bool>();
7062 }
7063 // 15b. If settingConfigFalse is true, throw a TypeError exception.
7064 if (setting_config_false) {
7065 isolate->Throw(*isolate->factory()->NewTypeError(
7066 MessageTemplate::kProxyDefinePropertyNonConfigurable, property_name));
7067 return Nothing<bool>();
7068 }
7069 } else {
7070 // 16. Else targetDesc is not undefined,
7071 // 16a. If IsCompatiblePropertyDescriptor(extensibleTarget, Desc,
7072 // targetDesc) is false, throw a TypeError exception.
7073 Maybe<bool> valid =
7074 IsCompatiblePropertyDescriptor(isolate, extensible_target, desc,
7075 &target_desc, property_name, DONT_THROW);
7076 MAYBE_RETURN(valid, Nothing<bool>());
7077 if (!valid.FromJust()) {
7078 isolate->Throw(*isolate->factory()->NewTypeError(
7079 MessageTemplate::kProxyDefinePropertyIncompatible, property_name));
7080 return Nothing<bool>();
7081 }
7082 // 16b. If settingConfigFalse is true and targetDesc.[[Configurable]] is
7083 // true, throw a TypeError exception.
7084 if (setting_config_false && target_desc.configurable()) {
7085 isolate->Throw(*isolate->factory()->NewTypeError(
7086 MessageTemplate::kProxyDefinePropertyNonConfigurable, property_name));
7087 return Nothing<bool>();
7088 }
7089 }
7090 // 17. Return true.
7091 return Just(true);
7092}
7093
7094
7095// static
Ben Murdoch097c5b22016-05-18 11:27:45 +01007096Maybe<bool> JSProxy::SetPrivateProperty(Isolate* isolate, Handle<JSProxy> proxy,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007097 Handle<Symbol> private_name,
7098 PropertyDescriptor* desc,
7099 ShouldThrow should_throw) {
7100 // Despite the generic name, this can only add private data properties.
7101 if (!PropertyDescriptor::IsDataDescriptor(desc) ||
7102 desc->ToAttributes() != DONT_ENUM) {
7103 RETURN_FAILURE(isolate, should_throw,
7104 NewTypeError(MessageTemplate::kProxyPrivate));
7105 }
7106 DCHECK(proxy->map()->is_dictionary_map());
7107 Handle<Object> value =
7108 desc->has_value()
7109 ? desc->value()
7110 : Handle<Object>::cast(isolate->factory()->undefined_value());
7111
Ben Murdochda12d292016-06-02 14:46:10 +01007112 LookupIterator it(proxy, private_name, proxy);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007113
7114 if (it.IsFound()) {
7115 DCHECK_EQ(LookupIterator::DATA, it.state());
Ben Murdoch097c5b22016-05-18 11:27:45 +01007116 DCHECK_EQ(DONT_ENUM, it.property_attributes());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007117 it.WriteDataValue(value);
7118 return Just(true);
7119 }
7120
7121 Handle<NameDictionary> dict(proxy->property_dictionary());
7122 PropertyDetails details(DONT_ENUM, DATA, 0, PropertyCellType::kNoCell);
7123 Handle<NameDictionary> result =
7124 NameDictionary::Add(dict, private_name, value, details);
7125 if (!dict.is_identical_to(result)) proxy->set_properties(*result);
7126 return Just(true);
7127}
7128
7129
7130// static
7131Maybe<bool> JSReceiver::GetOwnPropertyDescriptor(Isolate* isolate,
7132 Handle<JSReceiver> object,
7133 Handle<Object> key,
7134 PropertyDescriptor* desc) {
7135 bool success = false;
7136 DCHECK(key->IsName() || key->IsNumber()); // |key| is a PropertyKey...
7137 LookupIterator it = LookupIterator::PropertyOrElement(
7138 isolate, object, key, &success, LookupIterator::HIDDEN);
7139 DCHECK(success); // ...so creating a LookupIterator can't fail.
7140 return GetOwnPropertyDescriptor(&it, desc);
7141}
7142
7143
7144// ES6 9.1.5.1
7145// Returns true on success, false if the property didn't exist, nothing if
7146// an exception was thrown.
7147// static
7148Maybe<bool> JSReceiver::GetOwnPropertyDescriptor(LookupIterator* it,
7149 PropertyDescriptor* desc) {
7150 Isolate* isolate = it->isolate();
7151 // "Virtual" dispatch.
7152 if (it->IsFound() && it->GetHolder<JSReceiver>()->IsJSProxy()) {
7153 return JSProxy::GetOwnPropertyDescriptor(isolate, it->GetHolder<JSProxy>(),
7154 it->GetName(), desc);
7155 }
7156
7157 // 1. (Assert)
7158 // 2. If O does not have an own property with key P, return undefined.
7159 Maybe<PropertyAttributes> maybe = JSObject::GetPropertyAttributes(it);
7160 MAYBE_RETURN(maybe, Nothing<bool>());
7161 PropertyAttributes attrs = maybe.FromJust();
7162 if (attrs == ABSENT) return Just(false);
7163 DCHECK(!isolate->has_pending_exception());
7164
7165 // 3. Let D be a newly created Property Descriptor with no fields.
7166 DCHECK(desc->is_empty());
7167 // 4. Let X be O's own property whose key is P.
7168 // 5. If X is a data property, then
7169 bool is_accessor_pair = it->state() == LookupIterator::ACCESSOR &&
7170 it->GetAccessors()->IsAccessorPair();
7171 if (!is_accessor_pair) {
7172 // 5a. Set D.[[Value]] to the value of X's [[Value]] attribute.
7173 Handle<Object> value;
Ben Murdochda12d292016-06-02 14:46:10 +01007174 if (!Object::GetProperty(it).ToHandle(&value)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007175 DCHECK(isolate->has_pending_exception());
7176 return Nothing<bool>();
7177 }
7178 desc->set_value(value);
7179 // 5b. Set D.[[Writable]] to the value of X's [[Writable]] attribute
7180 desc->set_writable((attrs & READ_ONLY) == 0);
7181 } else {
7182 // 6. Else X is an accessor property, so
7183 Handle<AccessorPair> accessors =
7184 Handle<AccessorPair>::cast(it->GetAccessors());
7185 // 6a. Set D.[[Get]] to the value of X's [[Get]] attribute.
Ben Murdoch097c5b22016-05-18 11:27:45 +01007186 desc->set_get(AccessorPair::GetComponent(accessors, ACCESSOR_GETTER));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007187 // 6b. Set D.[[Set]] to the value of X's [[Set]] attribute.
Ben Murdoch097c5b22016-05-18 11:27:45 +01007188 desc->set_set(AccessorPair::GetComponent(accessors, ACCESSOR_SETTER));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007189 }
7190
7191 // 7. Set D.[[Enumerable]] to the value of X's [[Enumerable]] attribute.
7192 desc->set_enumerable((attrs & DONT_ENUM) == 0);
7193 // 8. Set D.[[Configurable]] to the value of X's [[Configurable]] attribute.
7194 desc->set_configurable((attrs & DONT_DELETE) == 0);
7195 // 9. Return D.
7196 DCHECK(PropertyDescriptor::IsAccessorDescriptor(desc) !=
7197 PropertyDescriptor::IsDataDescriptor(desc));
7198 return Just(true);
7199}
7200
7201
7202// ES6 9.5.5
7203// static
7204Maybe<bool> JSProxy::GetOwnPropertyDescriptor(Isolate* isolate,
7205 Handle<JSProxy> proxy,
7206 Handle<Name> name,
7207 PropertyDescriptor* desc) {
7208 DCHECK(!name->IsPrivate());
7209 STACK_CHECK(Nothing<bool>());
7210
7211 Handle<String> trap_name =
7212 isolate->factory()->getOwnPropertyDescriptor_string();
7213 // 1. (Assert)
7214 // 2. Let handler be the value of the [[ProxyHandler]] internal slot of O.
7215 Handle<Object> handler(proxy->handler(), isolate);
7216 // 3. If handler is null, throw a TypeError exception.
7217 // 4. Assert: Type(handler) is Object.
7218 if (proxy->IsRevoked()) {
7219 isolate->Throw(*isolate->factory()->NewTypeError(
7220 MessageTemplate::kProxyRevoked, trap_name));
7221 return Nothing<bool>();
7222 }
7223 // 5. Let target be the value of the [[ProxyTarget]] internal slot of O.
7224 Handle<JSReceiver> target(proxy->target(), isolate);
7225 // 6. Let trap be ? GetMethod(handler, "getOwnPropertyDescriptor").
7226 Handle<Object> trap;
7227 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
7228 isolate, trap,
7229 Object::GetMethod(Handle<JSReceiver>::cast(handler), trap_name),
7230 Nothing<bool>());
7231 // 7. If trap is undefined, then
7232 if (trap->IsUndefined()) {
7233 // 7a. Return target.[[GetOwnProperty]](P).
7234 return JSReceiver::GetOwnPropertyDescriptor(isolate, target, name, desc);
7235 }
7236 // 8. Let trapResultObj be ? Call(trap, handler, «target, P»).
7237 Handle<Object> trap_result_obj;
7238 Handle<Object> args[] = {target, name};
7239 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
7240 isolate, trap_result_obj,
7241 Execution::Call(isolate, trap, handler, arraysize(args), args),
7242 Nothing<bool>());
7243 // 9. If Type(trapResultObj) is neither Object nor Undefined, throw a
7244 // TypeError exception.
7245 if (!trap_result_obj->IsJSReceiver() && !trap_result_obj->IsUndefined()) {
7246 isolate->Throw(*isolate->factory()->NewTypeError(
7247 MessageTemplate::kProxyGetOwnPropertyDescriptorInvalid, name));
7248 return Nothing<bool>();
7249 }
7250 // 10. Let targetDesc be ? target.[[GetOwnProperty]](P).
7251 PropertyDescriptor target_desc;
7252 Maybe<bool> found =
7253 JSReceiver::GetOwnPropertyDescriptor(isolate, target, name, &target_desc);
7254 MAYBE_RETURN(found, Nothing<bool>());
7255 // 11. If trapResultObj is undefined, then
7256 if (trap_result_obj->IsUndefined()) {
7257 // 11a. If targetDesc is undefined, return undefined.
7258 if (!found.FromJust()) return Just(false);
7259 // 11b. If targetDesc.[[Configurable]] is false, throw a TypeError
7260 // exception.
7261 if (!target_desc.configurable()) {
7262 isolate->Throw(*isolate->factory()->NewTypeError(
7263 MessageTemplate::kProxyGetOwnPropertyDescriptorUndefined, name));
7264 return Nothing<bool>();
7265 }
7266 // 11c. Let extensibleTarget be ? IsExtensible(target).
7267 Maybe<bool> extensible_target = JSReceiver::IsExtensible(target);
7268 MAYBE_RETURN(extensible_target, Nothing<bool>());
7269 // 11d. (Assert)
7270 // 11e. If extensibleTarget is false, throw a TypeError exception.
7271 if (!extensible_target.FromJust()) {
7272 isolate->Throw(*isolate->factory()->NewTypeError(
7273 MessageTemplate::kProxyGetOwnPropertyDescriptorNonExtensible, name));
7274 return Nothing<bool>();
7275 }
7276 // 11f. Return undefined.
7277 return Just(false);
7278 }
7279 // 12. Let extensibleTarget be ? IsExtensible(target).
7280 Maybe<bool> extensible_target = JSReceiver::IsExtensible(target);
7281 MAYBE_RETURN(extensible_target, Nothing<bool>());
7282 // 13. Let resultDesc be ? ToPropertyDescriptor(trapResultObj).
7283 if (!PropertyDescriptor::ToPropertyDescriptor(isolate, trap_result_obj,
7284 desc)) {
7285 DCHECK(isolate->has_pending_exception());
7286 return Nothing<bool>();
7287 }
7288 // 14. Call CompletePropertyDescriptor(resultDesc).
7289 PropertyDescriptor::CompletePropertyDescriptor(isolate, desc);
7290 // 15. Let valid be IsCompatiblePropertyDescriptor (extensibleTarget,
7291 // resultDesc, targetDesc).
7292 Maybe<bool> valid =
7293 IsCompatiblePropertyDescriptor(isolate, extensible_target.FromJust(),
7294 desc, &target_desc, name, DONT_THROW);
7295 MAYBE_RETURN(valid, Nothing<bool>());
7296 // 16. If valid is false, throw a TypeError exception.
7297 if (!valid.FromJust()) {
7298 isolate->Throw(*isolate->factory()->NewTypeError(
7299 MessageTemplate::kProxyGetOwnPropertyDescriptorIncompatible, name));
7300 return Nothing<bool>();
7301 }
7302 // 17. If resultDesc.[[Configurable]] is false, then
7303 if (!desc->configurable()) {
7304 // 17a. If targetDesc is undefined or targetDesc.[[Configurable]] is true:
7305 if (target_desc.is_empty() || target_desc.configurable()) {
7306 // 17a i. Throw a TypeError exception.
7307 isolate->Throw(*isolate->factory()->NewTypeError(
7308 MessageTemplate::kProxyGetOwnPropertyDescriptorNonConfigurable,
7309 name));
7310 return Nothing<bool>();
7311 }
7312 }
7313 // 18. Return resultDesc.
7314 return Just(true);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007315}
7316
7317
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007318bool JSObject::ReferencesObjectFromElements(FixedArray* elements,
7319 ElementsKind kind,
7320 Object* object) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01007321 if (IsFastObjectElementsKind(kind) || kind == FAST_STRING_WRAPPER_ELEMENTS) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007322 int length = IsJSArray()
7323 ? Smi::cast(JSArray::cast(this)->length())->value()
7324 : elements->length();
7325 for (int i = 0; i < length; ++i) {
7326 Object* element = elements->get(i);
7327 if (!element->IsTheHole() && element == object) return true;
7328 }
7329 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01007330 DCHECK(kind == DICTIONARY_ELEMENTS || kind == SLOW_STRING_WRAPPER_ELEMENTS);
Ben Murdochc7cc0282012-03-05 14:35:55 +00007331 Object* key =
7332 SeededNumberDictionary::cast(elements)->SlowReverseLookup(object);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007333 if (!key->IsUndefined()) return true;
7334 }
7335 return false;
7336}
7337
7338
Steve Blocka7e24c12009-10-30 11:49:00 +00007339// Check whether this object references another object.
7340bool JSObject::ReferencesObject(Object* obj) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01007341 Map* map_of_this = map();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007342 Heap* heap = GetHeap();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007343 DisallowHeapAllocation no_allocation;
Steve Blocka7e24c12009-10-30 11:49:00 +00007344
7345 // Is the object the constructor for this object?
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007346 if (map_of_this->GetConstructor() == obj) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007347 return true;
7348 }
7349
7350 // Is the object the prototype for this object?
Ben Murdoch8b112d22011-06-08 16:22:53 +01007351 if (map_of_this->prototype() == obj) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007352 return true;
7353 }
7354
7355 // Check if the object is among the named properties.
7356 Object* key = SlowReverseLookup(obj);
Steve Block44f0eee2011-05-26 01:26:41 +01007357 if (!key->IsUndefined()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007358 return true;
7359 }
7360
7361 // Check if the object is among the indexed properties.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007362 ElementsKind kind = GetElementsKind();
7363 switch (kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007364 // Raw pixels and external arrays do not reference other
7365 // objects.
7366#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007367 case TYPE##_ELEMENTS: \
Steve Blocka7e24c12009-10-30 11:49:00 +00007368 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007369
7370 TYPED_ARRAYS(TYPED_ARRAY_CASE)
7371#undef TYPED_ARRAY_CASE
7372
7373 case FAST_DOUBLE_ELEMENTS:
7374 case FAST_HOLEY_DOUBLE_ELEMENTS:
7375 break;
7376 case FAST_SMI_ELEMENTS:
7377 case FAST_HOLEY_SMI_ELEMENTS:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007378 break;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007379 case FAST_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007380 case FAST_HOLEY_ELEMENTS:
Ben Murdoch097c5b22016-05-18 11:27:45 +01007381 case DICTIONARY_ELEMENTS:
7382 case FAST_STRING_WRAPPER_ELEMENTS:
7383 case SLOW_STRING_WRAPPER_ELEMENTS: {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007384 FixedArray* elements = FixedArray::cast(this->elements());
7385 if (ReferencesObjectFromElements(elements, kind, obj)) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00007386 break;
7387 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007388 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
7389 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007390 FixedArray* parameter_map = FixedArray::cast(elements());
7391 // Check the mapped parameters.
7392 int length = parameter_map->length();
7393 for (int i = 2; i < length; ++i) {
7394 Object* value = parameter_map->get(i);
7395 if (!value->IsTheHole() && value == obj) return true;
7396 }
7397 // Check the arguments.
7398 FixedArray* arguments = FixedArray::cast(parameter_map->get(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007399 kind = arguments->IsDictionary() ? DICTIONARY_ELEMENTS :
7400 FAST_HOLEY_ELEMENTS;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007401 if (ReferencesObjectFromElements(arguments, kind, obj)) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00007402 break;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007403 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01007404 case NO_ELEMENTS:
7405 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00007406 }
7407
Steve Block6ded16b2010-05-10 14:33:55 +01007408 // For functions check the context.
7409 if (IsJSFunction()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007410 // Get the constructor function for arguments array.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007411 Map* arguments_map =
7412 heap->isolate()->context()->native_context()->sloppy_arguments_map();
Steve Blocka7e24c12009-10-30 11:49:00 +00007413 JSFunction* arguments_function =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007414 JSFunction::cast(arguments_map->GetConstructor());
Steve Blocka7e24c12009-10-30 11:49:00 +00007415
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007416 // Get the context and don't check if it is the native context.
Steve Blocka7e24c12009-10-30 11:49:00 +00007417 JSFunction* f = JSFunction::cast(this);
7418 Context* context = f->context();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007419 if (context->IsNativeContext()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007420 return false;
7421 }
7422
7423 // Check the non-special context slots.
7424 for (int i = Context::MIN_CONTEXT_SLOTS; i < context->length(); i++) {
7425 // Only check JS objects.
7426 if (context->get(i)->IsJSObject()) {
7427 JSObject* ctxobj = JSObject::cast(context->get(i));
7428 // If it is an arguments array check the content.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007429 if (ctxobj->map()->GetConstructor() == arguments_function) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007430 if (ctxobj->ReferencesObject(obj)) {
7431 return true;
7432 }
7433 } else if (ctxobj == obj) {
7434 return true;
7435 }
7436 }
7437 }
7438
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007439 // Check the context extension (if any) if it can have references.
7440 if (context->has_extension() && !context->IsCatchContext()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007441 // With harmony scoping, a JSFunction may have a script context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007442 // TODO(mvstanton): walk into the ScopeInfo.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007443 if (context->IsScriptContext()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007444 return false;
7445 }
7446
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007447 return context->extension_object()->ReferencesObject(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00007448 }
7449 }
7450
7451 // No references to object.
7452 return false;
7453}
7454
7455
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007456Maybe<bool> JSReceiver::SetIntegrityLevel(Handle<JSReceiver> receiver,
7457 IntegrityLevel level,
7458 ShouldThrow should_throw) {
7459 DCHECK(level == SEALED || level == FROZEN);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007460
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007461 if (receiver->IsJSObject()) {
7462 Handle<JSObject> object = Handle<JSObject>::cast(receiver);
7463 if (!object->HasSloppyArgumentsElements() &&
Ben Murdochda12d292016-06-02 14:46:10 +01007464 !object->map()->is_observed()) { // Fast path.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007465 if (level == SEALED) {
7466 return JSObject::PreventExtensionsWithTransition<SEALED>(object,
7467 should_throw);
7468 } else {
7469 return JSObject::PreventExtensionsWithTransition<FROZEN>(object,
7470 should_throw);
7471 }
7472 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007473 }
7474
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007475 Isolate* isolate = receiver->GetIsolate();
7476
7477 MAYBE_RETURN(JSReceiver::PreventExtensions(receiver, should_throw),
7478 Nothing<bool>());
7479
7480 Handle<FixedArray> keys;
7481 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
7482 isolate, keys, JSReceiver::OwnPropertyKeys(receiver), Nothing<bool>());
7483
7484 PropertyDescriptor no_conf;
7485 no_conf.set_configurable(false);
7486
7487 PropertyDescriptor no_conf_no_write;
7488 no_conf_no_write.set_configurable(false);
7489 no_conf_no_write.set_writable(false);
7490
7491 if (level == SEALED) {
7492 for (int i = 0; i < keys->length(); ++i) {
7493 Handle<Object> key(keys->get(i), isolate);
7494 MAYBE_RETURN(
7495 DefineOwnProperty(isolate, receiver, key, &no_conf, THROW_ON_ERROR),
7496 Nothing<bool>());
7497 }
7498 return Just(true);
7499 }
7500
7501 for (int i = 0; i < keys->length(); ++i) {
7502 Handle<Object> key(keys->get(i), isolate);
7503 PropertyDescriptor current_desc;
7504 Maybe<bool> owned = JSReceiver::GetOwnPropertyDescriptor(
7505 isolate, receiver, key, &current_desc);
7506 MAYBE_RETURN(owned, Nothing<bool>());
7507 if (owned.FromJust()) {
7508 PropertyDescriptor desc =
7509 PropertyDescriptor::IsAccessorDescriptor(&current_desc)
7510 ? no_conf
7511 : no_conf_no_write;
7512 MAYBE_RETURN(
7513 DefineOwnProperty(isolate, receiver, key, &desc, THROW_ON_ERROR),
7514 Nothing<bool>());
7515 }
7516 }
7517 return Just(true);
7518}
7519
7520
7521Maybe<bool> JSReceiver::TestIntegrityLevel(Handle<JSReceiver> object,
7522 IntegrityLevel level) {
7523 DCHECK(level == SEALED || level == FROZEN);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007524 Isolate* isolate = object->GetIsolate();
7525
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007526 Maybe<bool> extensible = JSReceiver::IsExtensible(object);
7527 MAYBE_RETURN(extensible, Nothing<bool>());
7528 if (extensible.FromJust()) return Just(false);
7529
7530 Handle<FixedArray> keys;
7531 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
7532 isolate, keys, JSReceiver::OwnPropertyKeys(object), Nothing<bool>());
7533
7534 for (int i = 0; i < keys->length(); ++i) {
7535 Handle<Object> key(keys->get(i), isolate);
7536 PropertyDescriptor current_desc;
7537 Maybe<bool> owned = JSReceiver::GetOwnPropertyDescriptor(
7538 isolate, object, key, &current_desc);
7539 MAYBE_RETURN(owned, Nothing<bool>());
7540 if (owned.FromJust()) {
7541 if (current_desc.configurable()) return Just(false);
7542 if (level == FROZEN &&
7543 PropertyDescriptor::IsDataDescriptor(&current_desc) &&
7544 current_desc.writable()) {
7545 return Just(false);
7546 }
7547 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007548 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007549 return Just(true);
7550}
7551
7552
7553Maybe<bool> JSReceiver::PreventExtensions(Handle<JSReceiver> object,
7554 ShouldThrow should_throw) {
7555 if (object->IsJSProxy()) {
7556 return JSProxy::PreventExtensions(Handle<JSProxy>::cast(object),
7557 should_throw);
7558 }
7559 DCHECK(object->IsJSObject());
7560 return JSObject::PreventExtensions(Handle<JSObject>::cast(object),
7561 should_throw);
7562}
7563
7564
7565Maybe<bool> JSProxy::PreventExtensions(Handle<JSProxy> proxy,
7566 ShouldThrow should_throw) {
7567 Isolate* isolate = proxy->GetIsolate();
7568 STACK_CHECK(Nothing<bool>());
7569 Factory* factory = isolate->factory();
7570 Handle<String> trap_name = factory->preventExtensions_string();
7571
7572 if (proxy->IsRevoked()) {
7573 isolate->Throw(
7574 *factory->NewTypeError(MessageTemplate::kProxyRevoked, trap_name));
7575 return Nothing<bool>();
7576 }
7577 Handle<JSReceiver> target(proxy->target(), isolate);
7578 Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate);
7579
7580 Handle<Object> trap;
7581 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
7582 isolate, trap, Object::GetMethod(handler, trap_name), Nothing<bool>());
7583 if (trap->IsUndefined()) {
7584 return JSReceiver::PreventExtensions(target, should_throw);
7585 }
7586
7587 Handle<Object> trap_result;
7588 Handle<Object> args[] = {target};
7589 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
7590 isolate, trap_result,
7591 Execution::Call(isolate, trap, handler, arraysize(args), args),
7592 Nothing<bool>());
7593 if (!trap_result->BooleanValue()) {
7594 RETURN_FAILURE(
7595 isolate, should_throw,
7596 NewTypeError(MessageTemplate::kProxyTrapReturnedFalsish, trap_name));
7597 }
7598
7599 // Enforce the invariant.
7600 Maybe<bool> target_result = JSReceiver::IsExtensible(target);
7601 MAYBE_RETURN(target_result, Nothing<bool>());
7602 if (target_result.FromJust()) {
7603 isolate->Throw(*factory->NewTypeError(
7604 MessageTemplate::kProxyPreventExtensionsExtensible));
7605 return Nothing<bool>();
7606 }
7607 return Just(true);
7608}
7609
7610
7611Maybe<bool> JSObject::PreventExtensions(Handle<JSObject> object,
7612 ShouldThrow should_throw) {
7613 Isolate* isolate = object->GetIsolate();
7614
7615 if (!object->HasSloppyArgumentsElements() && !object->map()->is_observed()) {
7616 return PreventExtensionsWithTransition<NONE>(object, should_throw);
7617 }
7618
7619 if (object->IsAccessCheckNeeded() &&
7620 !isolate->MayAccess(handle(isolate->context()), object)) {
7621 isolate->ReportFailedAccessCheck(object);
7622 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>());
7623 RETURN_FAILURE(isolate, should_throw,
7624 NewTypeError(MessageTemplate::kNoAccess));
7625 }
7626
7627 if (!object->map()->is_extensible()) return Just(true);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007628
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007629 if (object->IsJSGlobalProxy()) {
7630 PrototypeIterator iter(isolate, object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007631 if (iter.IsAtEnd()) return Just(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007632 DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007633 return PreventExtensions(PrototypeIterator::GetCurrent<JSObject>(iter),
7634 should_throw);
Steve Block1e0659c2011-05-24 12:43:12 +01007635 }
7636
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007637 if (!object->HasFixedTypedArrayElements()) {
7638 // If there are fast elements we normalize.
7639 Handle<SeededNumberDictionary> dictionary = NormalizeElements(object);
7640 DCHECK(object->HasDictionaryElements() ||
7641 object->HasSlowArgumentsElements());
7642
7643 // Make sure that we never go back to fast case.
7644 object->RequireSlowElements(*dictionary);
Ben Murdoch257744e2011-11-30 15:57:28 +00007645 }
7646
Steve Block8defd9f2010-07-08 12:39:36 +01007647 // Do a map transition, other objects with this map may still
7648 // be extensible.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007649 // TODO(adamk): Extend the NormalizedMapCache to handle non-extensible maps.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007650 Handle<Map> new_map = Map::Copy(handle(object->map()), "PreventExtensions");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007651
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007652 new_map->set_is_extensible(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007653 JSObject::MigrateToMap(object, new_map);
7654 DCHECK(!object->map()->is_extensible());
7655
7656 if (object->map()->is_observed()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007657 RETURN_ON_EXCEPTION_VALUE(
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007658 isolate,
7659 EnqueueChangeRecord(object, "preventExtensions", Handle<Name>(),
7660 isolate->factory()->the_hole_value()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007661 Nothing<bool>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007662 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007663 return Just(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007664}
7665
7666
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007667Maybe<bool> JSReceiver::IsExtensible(Handle<JSReceiver> object) {
7668 if (object->IsJSProxy()) {
7669 return JSProxy::IsExtensible(Handle<JSProxy>::cast(object));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007670 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007671 return Just(JSObject::IsExtensible(Handle<JSObject>::cast(object)));
7672}
7673
7674
7675Maybe<bool> JSProxy::IsExtensible(Handle<JSProxy> proxy) {
7676 Isolate* isolate = proxy->GetIsolate();
7677 STACK_CHECK(Nothing<bool>());
7678 Factory* factory = isolate->factory();
7679 Handle<String> trap_name = factory->isExtensible_string();
7680
7681 if (proxy->IsRevoked()) {
7682 isolate->Throw(
7683 *factory->NewTypeError(MessageTemplate::kProxyRevoked, trap_name));
7684 return Nothing<bool>();
7685 }
7686 Handle<JSReceiver> target(proxy->target(), isolate);
7687 Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate);
7688
7689 Handle<Object> trap;
7690 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
7691 isolate, trap, Object::GetMethod(handler, trap_name), Nothing<bool>());
7692 if (trap->IsUndefined()) {
7693 return JSReceiver::IsExtensible(target);
7694 }
7695
7696 Handle<Object> trap_result;
7697 Handle<Object> args[] = {target};
7698 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
7699 isolate, trap_result,
7700 Execution::Call(isolate, trap, handler, arraysize(args), args),
7701 Nothing<bool>());
7702
7703 // Enforce the invariant.
7704 Maybe<bool> target_result = JSReceiver::IsExtensible(target);
7705 MAYBE_RETURN(target_result, Nothing<bool>());
7706 if (target_result.FromJust() != trap_result->BooleanValue()) {
7707 isolate->Throw(
7708 *factory->NewTypeError(MessageTemplate::kProxyIsExtensibleInconsistent,
7709 factory->ToBoolean(target_result.FromJust())));
7710 return Nothing<bool>();
7711 }
7712 return target_result;
7713}
7714
7715
7716bool JSObject::IsExtensible(Handle<JSObject> object) {
7717 Isolate* isolate = object->GetIsolate();
7718 if (object->IsAccessCheckNeeded() &&
7719 !isolate->MayAccess(handle(isolate->context()), object)) {
7720 return true;
7721 }
7722 if (object->IsJSGlobalProxy()) {
7723 PrototypeIterator iter(isolate, *object);
7724 if (iter.IsAtEnd()) return false;
7725 DCHECK(iter.GetCurrent()->IsJSGlobalObject());
7726 return iter.GetCurrent<JSObject>()->map()->is_extensible();
7727 }
7728 return object->map()->is_extensible();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007729}
7730
7731
7732template <typename Dictionary>
7733static void ApplyAttributesToDictionary(Dictionary* dictionary,
7734 const PropertyAttributes attributes) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007735 int capacity = dictionary->Capacity();
7736 for (int i = 0; i < capacity; i++) {
7737 Object* k = dictionary->KeyAt(i);
7738 if (dictionary->IsKey(k) &&
7739 !(k->IsSymbol() && Symbol::cast(k)->is_private())) {
7740 PropertyDetails details = dictionary->DetailsAt(i);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007741 int attrs = attributes;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007742 // READ_ONLY is an invalid attribute for JS setters/getters.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007743 if ((attributes & READ_ONLY) && details.type() == ACCESSOR_CONSTANT) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007744 Object* v = dictionary->ValueAt(i);
7745 if (v->IsPropertyCell()) v = PropertyCell::cast(v)->value();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007746 if (v->IsAccessorPair()) attrs &= ~READ_ONLY;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007747 }
7748 details = details.CopyAddAttributes(
7749 static_cast<PropertyAttributes>(attrs));
7750 dictionary->DetailsAtPut(i, details);
7751 }
7752 }
7753}
7754
7755
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007756template <PropertyAttributes attrs>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007757Maybe<bool> JSObject::PreventExtensionsWithTransition(
7758 Handle<JSObject> object, ShouldThrow should_throw) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007759 STATIC_ASSERT(attrs == NONE || attrs == SEALED || attrs == FROZEN);
7760
7761 // Sealing/freezing sloppy arguments should be handled elsewhere.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007762 DCHECK(!object->HasSloppyArgumentsElements());
7763 DCHECK(!object->map()->is_observed());
7764
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007765 Isolate* isolate = object->GetIsolate();
7766 if (object->IsAccessCheckNeeded() &&
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007767 !isolate->MayAccess(handle(isolate->context()), object)) {
7768 isolate->ReportFailedAccessCheck(object);
7769 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>());
7770 RETURN_FAILURE(isolate, should_throw,
7771 NewTypeError(MessageTemplate::kNoAccess));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007772 }
7773
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007774 if (attrs == NONE && !object->map()->is_extensible()) return Just(true);
7775
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007776 if (object->IsJSGlobalProxy()) {
7777 PrototypeIterator iter(isolate, object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007778 if (iter.IsAtEnd()) return Just(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007779 DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007780 return PreventExtensionsWithTransition<attrs>(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007781 PrototypeIterator::GetCurrent<JSObject>(iter), should_throw);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007782 }
7783
7784 Handle<SeededNumberDictionary> new_element_dictionary;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007785 if (!object->HasFixedTypedArrayElements() &&
Ben Murdoch097c5b22016-05-18 11:27:45 +01007786 !object->HasDictionaryElements() &&
7787 !object->HasSlowStringWrapperElements()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007788 int length =
7789 object->IsJSArray()
7790 ? Smi::cast(Handle<JSArray>::cast(object)->length())->value()
7791 : object->elements()->length();
7792 new_element_dictionary =
7793 length == 0 ? isolate->factory()->empty_slow_element_dictionary()
Ben Murdochda12d292016-06-02 14:46:10 +01007794 : object->GetElementsAccessor()->Normalize(object);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007795 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007796
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007797 Handle<Symbol> transition_marker;
7798 if (attrs == NONE) {
7799 transition_marker = isolate->factory()->nonextensible_symbol();
7800 } else if (attrs == SEALED) {
7801 transition_marker = isolate->factory()->sealed_symbol();
7802 } else {
7803 DCHECK(attrs == FROZEN);
7804 transition_marker = isolate->factory()->frozen_symbol();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007805 }
7806
7807 Handle<Map> old_map(object->map(), isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007808 Map* transition =
7809 TransitionArray::SearchSpecial(*old_map, *transition_marker);
7810 if (transition != NULL) {
7811 Handle<Map> transition_map(transition, isolate);
7812 DCHECK(transition_map->has_dictionary_elements() ||
Ben Murdoch097c5b22016-05-18 11:27:45 +01007813 transition_map->has_fixed_typed_array_elements() ||
7814 transition_map->elements_kind() == SLOW_STRING_WRAPPER_ELEMENTS);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007815 DCHECK(!transition_map->is_extensible());
7816 JSObject::MigrateToMap(object, transition_map);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007817 } else if (TransitionArray::CanHaveMoreTransitions(old_map)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007818 // Create a new descriptor array with the appropriate property attributes
7819 Handle<Map> new_map = Map::CopyForPreventExtensions(
7820 old_map, attrs, transition_marker, "CopyForPreventExtensions");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007821 JSObject::MigrateToMap(object, new_map);
7822 } else {
7823 DCHECK(old_map->is_dictionary_map() || !old_map->is_prototype_map());
7824 // Slow path: need to normalize properties for safety
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007825 NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0,
7826 "SlowPreventExtensions");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007827
7828 // Create a new map, since other objects with this map may be extensible.
7829 // TODO(adamk): Extend the NormalizedMapCache to handle non-extensible maps.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007830 Handle<Map> new_map =
7831 Map::Copy(handle(object->map()), "SlowCopyForPreventExtensions");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007832 new_map->set_is_extensible(false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007833 if (!new_element_dictionary.is_null()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01007834 ElementsKind new_kind =
7835 IsStringWrapperElementsKind(old_map->elements_kind())
7836 ? SLOW_STRING_WRAPPER_ELEMENTS
7837 : DICTIONARY_ELEMENTS;
7838 new_map->set_elements_kind(new_kind);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007839 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007840 JSObject::MigrateToMap(object, new_map);
7841
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007842 if (attrs != NONE) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007843 if (object->IsJSGlobalObject()) {
7844 ApplyAttributesToDictionary(object->global_dictionary(), attrs);
7845 } else {
7846 ApplyAttributesToDictionary(object->property_dictionary(), attrs);
7847 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007848 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007849 }
7850
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007851 // Both seal and preventExtensions always go through without modifications to
7852 // typed array elements. Freeze works only if there are no actual elements.
7853 if (object->HasFixedTypedArrayElements()) {
7854 if (attrs == FROZEN &&
7855 JSArrayBufferView::cast(*object)->byte_length()->Number() > 0) {
7856 isolate->Throw(*isolate->factory()->NewTypeError(
7857 MessageTemplate::kCannotFreezeArrayBufferView));
7858 return Nothing<bool>();
7859 }
7860 return Just(true);
7861 }
7862
Ben Murdoch097c5b22016-05-18 11:27:45 +01007863 DCHECK(object->map()->has_dictionary_elements() ||
7864 object->map()->elements_kind() == SLOW_STRING_WRAPPER_ELEMENTS);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007865 if (!new_element_dictionary.is_null()) {
7866 object->set_elements(*new_element_dictionary);
7867 }
7868
7869 if (object->elements() != isolate->heap()->empty_slow_element_dictionary()) {
7870 SeededNumberDictionary* dictionary = object->element_dictionary();
7871 // Make sure we never go back to the fast case
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007872 object->RequireSlowElements(dictionary);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007873 if (attrs != NONE) {
7874 ApplyAttributesToDictionary(dictionary, attrs);
7875 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007876 }
7877
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007878 return Just(true);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007879}
7880
7881
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007882void JSObject::SetObserved(Handle<JSObject> object) {
7883 DCHECK(!object->IsJSGlobalProxy());
7884 DCHECK(!object->IsJSGlobalObject());
7885 Isolate* isolate = object->GetIsolate();
7886 Handle<Map> new_map;
7887 Handle<Map> old_map(object->map(), isolate);
7888 DCHECK(!old_map->is_observed());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007889 Map* transition = TransitionArray::SearchSpecial(
7890 *old_map, isolate->heap()->observed_symbol());
7891 if (transition != NULL) {
7892 new_map = handle(transition, isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007893 DCHECK(new_map->is_observed());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007894 } else if (TransitionArray::CanHaveMoreTransitions(old_map)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007895 new_map = Map::CopyForObserved(old_map);
7896 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007897 new_map = Map::Copy(old_map, "SlowObserved");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007898 new_map->set_is_observed();
7899 }
7900 JSObject::MigrateToMap(object, new_map);
7901}
7902
7903
7904Handle<Object> JSObject::FastPropertyAt(Handle<JSObject> object,
7905 Representation representation,
7906 FieldIndex index) {
7907 Isolate* isolate = object->GetIsolate();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007908 if (object->IsUnboxedDoubleField(index)) {
7909 double value = object->RawFastDoublePropertyAt(index);
7910 return isolate->factory()->NewHeapNumber(value);
7911 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007912 Handle<Object> raw_value(object->RawFastPropertyAt(index), isolate);
7913 return Object::WrapForRead(isolate, raw_value, representation);
7914}
7915
Ben Murdochda12d292016-06-02 14:46:10 +01007916template <class ContextObject>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007917class JSObjectWalkVisitor {
7918 public:
7919 JSObjectWalkVisitor(ContextObject* site_context, bool copying,
7920 JSObject::DeepCopyHints hints)
7921 : site_context_(site_context),
7922 copying_(copying),
7923 hints_(hints) {}
7924
7925 MUST_USE_RESULT MaybeHandle<JSObject> StructureWalk(Handle<JSObject> object);
7926
7927 protected:
7928 MUST_USE_RESULT inline MaybeHandle<JSObject> VisitElementOrProperty(
7929 Handle<JSObject> object,
7930 Handle<JSObject> value) {
7931 Handle<AllocationSite> current_site = site_context()->EnterNewScope();
7932 MaybeHandle<JSObject> copy_of_value = StructureWalk(value);
7933 site_context()->ExitScope(current_site, value);
7934 return copy_of_value;
7935 }
7936
7937 inline ContextObject* site_context() { return site_context_; }
7938 inline Isolate* isolate() { return site_context()->isolate(); }
7939
7940 inline bool copying() const { return copying_; }
7941
7942 private:
7943 ContextObject* site_context_;
7944 const bool copying_;
7945 const JSObject::DeepCopyHints hints_;
7946};
7947
Ben Murdochda12d292016-06-02 14:46:10 +01007948template <class ContextObject>
7949MaybeHandle<JSObject> JSObjectWalkVisitor<ContextObject>::StructureWalk(
7950 Handle<JSObject> object) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007951 Isolate* isolate = this->isolate();
7952 bool copying = this->copying();
7953 bool shallow = hints_ == JSObject::kObjectIsShallow;
7954
7955 if (!shallow) {
7956 StackLimitCheck check(isolate);
7957
7958 if (check.HasOverflowed()) {
7959 isolate->StackOverflow();
7960 return MaybeHandle<JSObject>();
7961 }
7962 }
7963
7964 if (object->map()->is_deprecated()) {
7965 JSObject::MigrateInstance(object);
7966 }
7967
7968 Handle<JSObject> copy;
7969 if (copying) {
Ben Murdochda12d292016-06-02 14:46:10 +01007970 // JSFunction objects are not allowed to be in normal boilerplates at all.
7971 DCHECK(!object->IsJSFunction());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007972 Handle<AllocationSite> site_to_pass;
7973 if (site_context()->ShouldCreateMemento(object)) {
7974 site_to_pass = site_context()->current();
7975 }
7976 copy = isolate->factory()->CopyJSObjectWithAllocationSite(
7977 object, site_to_pass);
7978 } else {
7979 copy = object;
7980 }
7981
7982 DCHECK(copying || copy.is_identical_to(object));
7983
7984 ElementsKind kind = copy->GetElementsKind();
7985 if (copying && IsFastSmiOrObjectElementsKind(kind) &&
7986 FixedArray::cast(copy->elements())->map() ==
7987 isolate->heap()->fixed_cow_array_map()) {
7988 isolate->counters()->cow_arrays_created_runtime()->Increment();
7989 }
7990
7991 if (!shallow) {
7992 HandleScope scope(isolate);
7993
7994 // Deep copy own properties.
7995 if (copy->HasFastProperties()) {
7996 Handle<DescriptorArray> descriptors(copy->map()->instance_descriptors());
7997 int limit = copy->map()->NumberOfOwnDescriptors();
7998 for (int i = 0; i < limit; i++) {
7999 PropertyDetails details = descriptors->GetDetails(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008000 if (details.type() != DATA) continue;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008001 FieldIndex index = FieldIndex::ForDescriptor(copy->map(), i);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008002 if (object->IsUnboxedDoubleField(index)) {
8003 if (copying) {
8004 double value = object->RawFastDoublePropertyAt(index);
8005 copy->RawFastDoublePropertyAtPut(index, value);
8006 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008007 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008008 Handle<Object> value(object->RawFastPropertyAt(index), isolate);
8009 if (value->IsJSObject()) {
8010 ASSIGN_RETURN_ON_EXCEPTION(
8011 isolate, value,
8012 VisitElementOrProperty(copy, Handle<JSObject>::cast(value)),
8013 JSObject);
8014 if (copying) {
8015 copy->FastPropertyAtPut(index, *value);
8016 }
8017 } else {
8018 if (copying) {
8019 Representation representation = details.representation();
8020 value = Object::NewStorageFor(isolate, value, representation);
8021 copy->FastPropertyAtPut(index, *value);
8022 }
8023 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008024 }
8025 }
8026 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008027 // Only deep copy fields from the object literal expression.
8028 // In particular, don't try to copy the length attribute of
8029 // an array.
8030 PropertyFilter filter = static_cast<PropertyFilter>(
8031 ONLY_WRITABLE | ONLY_ENUMERABLE | ONLY_CONFIGURABLE);
Ben Murdoch097c5b22016-05-18 11:27:45 +01008032 KeyAccumulator accumulator(isolate, OWN_ONLY, filter);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008033 accumulator.NextPrototype();
8034 copy->CollectOwnPropertyNames(&accumulator, filter);
8035 Handle<FixedArray> names = accumulator.GetKeys();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008036 for (int i = 0; i < names->length(); i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008037 DCHECK(names->get(i)->IsName());
8038 Handle<Name> name(Name::cast(names->get(i)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008039 Handle<Object> value =
Ben Murdochda12d292016-06-02 14:46:10 +01008040 JSObject::GetProperty(copy, name).ToHandleChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008041 if (value->IsJSObject()) {
8042 Handle<JSObject> result;
8043 ASSIGN_RETURN_ON_EXCEPTION(
8044 isolate, result,
8045 VisitElementOrProperty(copy, Handle<JSObject>::cast(value)),
8046 JSObject);
8047 if (copying) {
8048 // Creating object copy for literals. No strict mode needed.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008049 JSObject::SetProperty(copy, name, result, SLOPPY).Assert();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008050 }
8051 }
8052 }
8053 }
8054
8055 // Deep copy own elements.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008056 switch (kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008057 case FAST_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008058 case FAST_HOLEY_ELEMENTS: {
8059 Handle<FixedArray> elements(FixedArray::cast(copy->elements()));
8060 if (elements->map() == isolate->heap()->fixed_cow_array_map()) {
8061#ifdef DEBUG
8062 for (int i = 0; i < elements->length(); i++) {
8063 DCHECK(!elements->get(i)->IsJSObject());
8064 }
8065#endif
8066 } else {
8067 for (int i = 0; i < elements->length(); i++) {
8068 Handle<Object> value(elements->get(i), isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008069 if (value->IsJSObject()) {
8070 Handle<JSObject> result;
8071 ASSIGN_RETURN_ON_EXCEPTION(
8072 isolate, result,
8073 VisitElementOrProperty(copy, Handle<JSObject>::cast(value)),
8074 JSObject);
8075 if (copying) {
8076 elements->set(i, *result);
8077 }
8078 }
8079 }
8080 }
8081 break;
8082 }
8083 case DICTIONARY_ELEMENTS: {
8084 Handle<SeededNumberDictionary> element_dictionary(
8085 copy->element_dictionary());
8086 int capacity = element_dictionary->Capacity();
8087 for (int i = 0; i < capacity; i++) {
8088 Object* k = element_dictionary->KeyAt(i);
8089 if (element_dictionary->IsKey(k)) {
8090 Handle<Object> value(element_dictionary->ValueAt(i), isolate);
8091 if (value->IsJSObject()) {
8092 Handle<JSObject> result;
8093 ASSIGN_RETURN_ON_EXCEPTION(
8094 isolate, result,
8095 VisitElementOrProperty(copy, Handle<JSObject>::cast(value)),
8096 JSObject);
8097 if (copying) {
8098 element_dictionary->ValueAtPut(i, *result);
8099 }
8100 }
8101 }
8102 }
8103 break;
8104 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008105 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
8106 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008107 UNIMPLEMENTED();
8108 break;
Ben Murdoch097c5b22016-05-18 11:27:45 +01008109 case FAST_STRING_WRAPPER_ELEMENTS:
8110 case SLOW_STRING_WRAPPER_ELEMENTS:
8111 UNREACHABLE();
8112 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008113
8114#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008115 case TYPE##_ELEMENTS: \
8116
8117 TYPED_ARRAYS(TYPED_ARRAY_CASE)
8118#undef TYPED_ARRAY_CASE
Ben Murdoch097c5b22016-05-18 11:27:45 +01008119 // Typed elements cannot be created using an object literal.
8120 UNREACHABLE();
8121 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008122
Ben Murdoch097c5b22016-05-18 11:27:45 +01008123 case FAST_SMI_ELEMENTS:
8124 case FAST_HOLEY_SMI_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008125 case FAST_DOUBLE_ELEMENTS:
8126 case FAST_HOLEY_DOUBLE_ELEMENTS:
Ben Murdoch097c5b22016-05-18 11:27:45 +01008127 case NO_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008128 // No contained objects, nothing to do.
8129 break;
8130 }
8131 }
8132
8133 return copy;
8134}
8135
8136
8137MaybeHandle<JSObject> JSObject::DeepWalk(
8138 Handle<JSObject> object,
8139 AllocationSiteCreationContext* site_context) {
Ben Murdochda12d292016-06-02 14:46:10 +01008140 JSObjectWalkVisitor<AllocationSiteCreationContext> v(site_context, false,
8141 kNoHints);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008142 MaybeHandle<JSObject> result = v.StructureWalk(object);
8143 Handle<JSObject> for_assert;
8144 DCHECK(!result.ToHandle(&for_assert) || for_assert.is_identical_to(object));
8145 return result;
8146}
8147
8148
8149MaybeHandle<JSObject> JSObject::DeepCopy(
8150 Handle<JSObject> object,
8151 AllocationSiteUsageContext* site_context,
8152 DeepCopyHints hints) {
Ben Murdochda12d292016-06-02 14:46:10 +01008153 JSObjectWalkVisitor<AllocationSiteUsageContext> v(site_context, true, hints);
Ben Murdoch097c5b22016-05-18 11:27:45 +01008154 MaybeHandle<JSObject> copy = v.StructureWalk(object);
8155 Handle<JSObject> for_assert;
8156 DCHECK(!copy.ToHandle(&for_assert) || !for_assert.is_identical_to(object));
8157 return copy;
8158}
Steve Block8defd9f2010-07-08 12:39:36 +01008159
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008160// static
8161MaybeHandle<Object> JSReceiver::ToPrimitive(Handle<JSReceiver> receiver,
8162 ToPrimitiveHint hint) {
8163 Isolate* const isolate = receiver->GetIsolate();
8164 Handle<Object> exotic_to_prim;
8165 ASSIGN_RETURN_ON_EXCEPTION(
8166 isolate, exotic_to_prim,
8167 GetMethod(receiver, isolate->factory()->to_primitive_symbol()), Object);
8168 if (!exotic_to_prim->IsUndefined()) {
8169 Handle<Object> hint_string;
8170 switch (hint) {
8171 case ToPrimitiveHint::kDefault:
8172 hint_string = isolate->factory()->default_string();
8173 break;
8174 case ToPrimitiveHint::kNumber:
8175 hint_string = isolate->factory()->number_string();
8176 break;
8177 case ToPrimitiveHint::kString:
8178 hint_string = isolate->factory()->string_string();
8179 break;
8180 }
8181 Handle<Object> result;
8182 ASSIGN_RETURN_ON_EXCEPTION(
8183 isolate, result,
8184 Execution::Call(isolate, exotic_to_prim, receiver, 1, &hint_string),
8185 Object);
8186 if (result->IsPrimitive()) return result;
8187 THROW_NEW_ERROR(isolate,
8188 NewTypeError(MessageTemplate::kCannotConvertToPrimitive),
8189 Object);
8190 }
8191 return OrdinaryToPrimitive(receiver, (hint == ToPrimitiveHint::kString)
8192 ? OrdinaryToPrimitiveHint::kString
8193 : OrdinaryToPrimitiveHint::kNumber);
8194}
8195
8196
8197// static
8198MaybeHandle<Object> JSReceiver::OrdinaryToPrimitive(
8199 Handle<JSReceiver> receiver, OrdinaryToPrimitiveHint hint) {
8200 Isolate* const isolate = receiver->GetIsolate();
8201 Handle<String> method_names[2];
8202 switch (hint) {
8203 case OrdinaryToPrimitiveHint::kNumber:
8204 method_names[0] = isolate->factory()->valueOf_string();
8205 method_names[1] = isolate->factory()->toString_string();
8206 break;
8207 case OrdinaryToPrimitiveHint::kString:
8208 method_names[0] = isolate->factory()->toString_string();
8209 method_names[1] = isolate->factory()->valueOf_string();
8210 break;
8211 }
8212 for (Handle<String> name : method_names) {
8213 Handle<Object> method;
8214 ASSIGN_RETURN_ON_EXCEPTION(isolate, method,
8215 JSReceiver::GetProperty(receiver, name), Object);
8216 if (method->IsCallable()) {
8217 Handle<Object> result;
8218 ASSIGN_RETURN_ON_EXCEPTION(
8219 isolate, result, Execution::Call(isolate, method, receiver, 0, NULL),
8220 Object);
8221 if (result->IsPrimitive()) return result;
8222 }
8223 }
8224 THROW_NEW_ERROR(isolate,
8225 NewTypeError(MessageTemplate::kCannotConvertToPrimitive),
8226 Object);
8227}
8228
8229
8230// TODO(cbruni/jkummerow): Consider moving this into elements.cc.
Ben Murdochda12d292016-06-02 14:46:10 +01008231bool JSObject::HasEnumerableElements() {
8232 // TODO(cbruni): cleanup
8233 JSObject* object = this;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008234 switch (object->GetElementsKind()) {
8235 case FAST_SMI_ELEMENTS:
8236 case FAST_ELEMENTS:
8237 case FAST_DOUBLE_ELEMENTS: {
8238 int length = object->IsJSArray()
8239 ? Smi::cast(JSArray::cast(object)->length())->value()
8240 : object->elements()->length();
8241 return length > 0;
8242 }
8243 case FAST_HOLEY_SMI_ELEMENTS:
8244 case FAST_HOLEY_ELEMENTS: {
8245 FixedArray* elements = FixedArray::cast(object->elements());
8246 int length = object->IsJSArray()
8247 ? Smi::cast(JSArray::cast(object)->length())->value()
8248 : elements->length();
8249 for (int i = 0; i < length; i++) {
8250 if (!elements->is_the_hole(i)) return true;
8251 }
8252 return false;
8253 }
8254 case FAST_HOLEY_DOUBLE_ELEMENTS: {
8255 int length = object->IsJSArray()
8256 ? Smi::cast(JSArray::cast(object)->length())->value()
8257 : object->elements()->length();
8258 // Zero-length arrays would use the empty FixedArray...
8259 if (length == 0) return false;
8260 // ...so only cast to FixedDoubleArray otherwise.
8261 FixedDoubleArray* elements = FixedDoubleArray::cast(object->elements());
8262 for (int i = 0; i < length; i++) {
8263 if (!elements->is_the_hole(i)) return true;
8264 }
8265 return false;
8266 }
8267#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
8268 case TYPE##_ELEMENTS:
8269
8270 TYPED_ARRAYS(TYPED_ARRAY_CASE)
8271#undef TYPED_ARRAY_CASE
8272 {
8273 int length = object->elements()->length();
8274 return length > 0;
8275 }
8276 case DICTIONARY_ELEMENTS: {
8277 SeededNumberDictionary* elements =
8278 SeededNumberDictionary::cast(object->elements());
8279 return elements->NumberOfElementsFilterAttributes(ONLY_ENUMERABLE) > 0;
8280 }
8281 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
8282 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
8283 // We're approximating non-empty arguments objects here.
8284 return true;
Ben Murdoch097c5b22016-05-18 11:27:45 +01008285 case FAST_STRING_WRAPPER_ELEMENTS:
8286 case SLOW_STRING_WRAPPER_ELEMENTS:
8287 if (String::cast(JSValue::cast(object)->value())->length() > 0) {
8288 return true;
8289 }
8290 return object->elements()->length() > 0;
8291 case NO_ELEMENTS:
8292 return false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008293 }
8294 UNREACHABLE();
8295 return true;
8296}
8297
Steve Blocka7e24c12009-10-30 11:49:00 +00008298// Tests for the fast common case for property enumeration:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008299// - This object and all prototypes has an enum cache (which means that
8300// it is no proxy, has no interceptors and needs no access checks).
Steve Blockd0582a62009-12-15 09:54:21 +00008301// - This object has no elements.
8302// - No prototype has enumerable properties/elements.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008303bool JSReceiver::IsSimpleEnum() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008304 for (PrototypeIterator iter(GetIsolate(), this,
8305 PrototypeIterator::START_AT_RECEIVER);
8306 !iter.IsAtEnd(); iter.Advance()) {
8307 if (!iter.GetCurrent()->IsJSObject()) return false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008308 JSObject* current = iter.GetCurrent<JSObject>();
8309 int enum_length = current->map()->EnumLength();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008310 if (enum_length == kInvalidEnumCacheSentinel) return false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008311 if (current->IsAccessCheckNeeded()) return false;
8312 DCHECK(!current->HasNamedInterceptor());
8313 DCHECK(!current->HasIndexedInterceptor());
Ben Murdochda12d292016-06-02 14:46:10 +01008314 if (current->HasEnumerableElements()) return false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008315 if (current != this && enum_length != 0) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00008316 }
8317 return true;
8318}
8319
8320
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008321int Map::NumberOfDescribedProperties(DescriptorFlag which,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008322 PropertyFilter filter) {
Steve Blocka7e24c12009-10-30 11:49:00 +00008323 int result = 0;
8324 DescriptorArray* descs = instance_descriptors();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008325 int limit = which == ALL_DESCRIPTORS
8326 ? descs->number_of_descriptors()
8327 : NumberOfOwnDescriptors();
8328 for (int i = 0; i < limit; i++) {
8329 if ((descs->GetDetails(i).attributes() & filter) == 0 &&
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008330 !descs->GetKey(i)->FilterKey(filter)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008331 result++;
8332 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008333 }
8334 return result;
8335}
8336
8337
Steve Blocka7e24c12009-10-30 11:49:00 +00008338int Map::NextFreePropertyIndex() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008339 int free_index = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008340 int number_of_own_descriptors = NumberOfOwnDescriptors();
Steve Blocka7e24c12009-10-30 11:49:00 +00008341 DescriptorArray* descs = instance_descriptors();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008342 for (int i = 0; i < number_of_own_descriptors; i++) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008343 PropertyDetails details = descs->GetDetails(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008344 if (details.location() == kField) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008345 int candidate = details.field_index() + details.field_width_in_words();
8346 if (candidate > free_index) free_index = candidate;
Steve Blocka7e24c12009-10-30 11:49:00 +00008347 }
8348 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008349 return free_index;
Steve Blocka7e24c12009-10-30 11:49:00 +00008350}
8351
8352
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008353static bool ContainsOnlyValidKeys(Handle<FixedArray> array) {
8354 int len = array->length();
8355 for (int i = 0; i < len; i++) {
8356 Object* e = array->get(i);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008357 if (!(e->IsName() || e->IsNumber())) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008358 }
8359 return true;
8360}
8361
8362
8363static Handle<FixedArray> ReduceFixedArrayTo(
8364 Handle<FixedArray> array, int length) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01008365 DCHECK_LE(length, array->length());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008366 if (array->length() == length) return array;
Ben Murdoch097c5b22016-05-18 11:27:45 +01008367 return array->GetIsolate()->factory()->CopyFixedArrayUpTo(array, length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008368}
8369
Ben Murdoch097c5b22016-05-18 11:27:45 +01008370bool Map::OnlyHasSimpleProperties() {
8371 // Wrapped string elements aren't explicitly stored in the elements backing
8372 // store, but are loaded indirectly from the underlying string.
8373 return !IsStringWrapperElementsKind(elements_kind()) &&
Ben Murdochda12d292016-06-02 14:46:10 +01008374 instance_type() > LAST_SPECIAL_RECEIVER_TYPE &&
8375 !has_hidden_prototype() && !is_dictionary_map();
Ben Murdoch097c5b22016-05-18 11:27:45 +01008376}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008377
Ben Murdochda12d292016-06-02 14:46:10 +01008378// static
8379Handle<FixedArray> JSObject::GetFastEnumPropertyKeys(Isolate* isolate,
8380 Handle<JSObject> object) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008381 Handle<Map> map(object->map());
Ben Murdoch097c5b22016-05-18 11:27:45 +01008382 bool cache_enum_length = map->OnlyHasSimpleProperties();
8383
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008384 Handle<DescriptorArray> descs =
8385 Handle<DescriptorArray>(map->instance_descriptors(), isolate);
8386 int own_property_count = map->EnumLength();
8387 // If the enum length of the given map is set to kInvalidEnumCache, this
8388 // means that the map itself has never used the present enum cache. The
8389 // first step to using the cache is to set the enum length of the map by
8390 // counting the number of own descriptors that are ENUMERABLE_STRINGS.
8391 if (own_property_count == kInvalidEnumCacheSentinel) {
8392 own_property_count =
8393 map->NumberOfDescribedProperties(OWN_DESCRIPTORS, ENUMERABLE_STRINGS);
8394 } else {
8395 DCHECK(
8396 own_property_count ==
8397 map->NumberOfDescribedProperties(OWN_DESCRIPTORS, ENUMERABLE_STRINGS));
8398 }
8399
8400 if (descs->HasEnumCache()) {
8401 Handle<FixedArray> keys(descs->GetEnumCache(), isolate);
8402 // In case the number of properties required in the enum are actually
8403 // present, we can reuse the enum cache. Otherwise, this means that the
8404 // enum cache was generated for a previous (smaller) version of the
8405 // Descriptor Array. In that case we regenerate the enum cache.
8406 if (own_property_count <= keys->length()) {
8407 isolate->counters()->enum_cache_hits()->Increment();
8408 if (cache_enum_length) map->SetEnumLength(own_property_count);
8409 return ReduceFixedArrayTo(keys, own_property_count);
8410 }
8411 }
8412
8413 if (descs->IsEmpty()) {
8414 isolate->counters()->enum_cache_hits()->Increment();
8415 if (cache_enum_length) map->SetEnumLength(0);
8416 return isolate->factory()->empty_fixed_array();
8417 }
8418
8419 isolate->counters()->enum_cache_misses()->Increment();
8420
8421 Handle<FixedArray> storage =
8422 isolate->factory()->NewFixedArray(own_property_count);
8423 Handle<FixedArray> indices =
8424 isolate->factory()->NewFixedArray(own_property_count);
8425
8426 int size = map->NumberOfOwnDescriptors();
8427 int index = 0;
8428
8429 for (int i = 0; i < size; i++) {
8430 PropertyDetails details = descs->GetDetails(i);
Ben Murdochda12d292016-06-02 14:46:10 +01008431 if (details.IsDontEnum()) continue;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008432 Object* key = descs->GetKey(i);
Ben Murdochda12d292016-06-02 14:46:10 +01008433 if (key->IsSymbol()) continue;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008434 storage->set(index, key);
8435 if (!indices.is_null()) {
8436 if (details.type() != DATA) {
8437 indices = Handle<FixedArray>();
8438 } else {
8439 FieldIndex field_index = FieldIndex::ForDescriptor(*map, i);
8440 int load_by_field_index = field_index.GetLoadByFieldIndex();
8441 indices->set(index, Smi::FromInt(load_by_field_index));
8442 }
8443 }
8444 index++;
8445 }
8446 DCHECK(index == storage->length());
8447
8448 DescriptorArray::SetEnumCache(descs, isolate, storage, indices);
8449 if (cache_enum_length) {
8450 map->SetEnumLength(own_property_count);
8451 }
8452 return storage;
8453}
8454
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008455
Ben Murdoch097c5b22016-05-18 11:27:45 +01008456Handle<FixedArray> JSObject::GetEnumPropertyKeys(Handle<JSObject> object) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008457 Isolate* isolate = object->GetIsolate();
8458 if (object->HasFastProperties()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01008459 return GetFastEnumPropertyKeys(isolate, object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008460 } else if (object->IsJSGlobalObject()) {
8461 Handle<GlobalDictionary> dictionary(object->global_dictionary());
8462 int length = dictionary->NumberOfEnumElements();
8463 if (length == 0) {
Ben Murdochda12d292016-06-02 14:46:10 +01008464 return isolate->factory()->empty_fixed_array();
Steve Blocka7e24c12009-10-30 11:49:00 +00008465 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008466 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(length);
8467 dictionary->CopyEnumKeysTo(*storage);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008468 return storage;
8469 } else {
8470 Handle<NameDictionary> dictionary(object->property_dictionary());
8471 int length = dictionary->NumberOfEnumElements();
8472 if (length == 0) {
Ben Murdochda12d292016-06-02 14:46:10 +01008473 return isolate->factory()->empty_fixed_array();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008474 }
8475 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(length);
8476 dictionary->CopyEnumKeysTo(*storage);
8477 return storage;
Steve Blocka7e24c12009-10-30 11:49:00 +00008478 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008479}
8480
8481
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008482enum IndexedOrNamed { kIndexed, kNamed };
Steve Blocka7e24c12009-10-30 11:49:00 +00008483
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008484
8485// Returns |true| on success, |nothing| on exception.
8486template <class Callback, IndexedOrNamed type>
8487static Maybe<bool> GetKeysFromInterceptor(Isolate* isolate,
8488 Handle<JSReceiver> receiver,
8489 Handle<JSObject> object,
8490 PropertyFilter filter,
8491 KeyAccumulator* accumulator) {
8492 if (type == kIndexed) {
8493 if (!object->HasIndexedInterceptor()) return Just(true);
8494 } else {
8495 if (!object->HasNamedInterceptor()) return Just(true);
8496 }
8497 Handle<InterceptorInfo> interceptor(type == kIndexed
8498 ? object->GetIndexedInterceptor()
8499 : object->GetNamedInterceptor(),
8500 isolate);
8501 if ((filter & ONLY_ALL_CAN_READ) && !interceptor->all_can_read()) {
8502 return Just(true);
8503 }
8504 PropertyCallbackArguments args(isolate, interceptor->data(), *receiver,
Ben Murdoch097c5b22016-05-18 11:27:45 +01008505 *object, Object::DONT_THROW);
Ben Murdochda12d292016-06-02 14:46:10 +01008506 Handle<JSObject> result;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008507 if (!interceptor->enumerator()->IsUndefined()) {
8508 Callback enum_fun = v8::ToCData<Callback>(interceptor->enumerator());
8509 const char* log_tag = type == kIndexed ? "interceptor-indexed-enum"
8510 : "interceptor-named-enum";
8511 LOG(isolate, ApiObjectAccess(log_tag, *object));
8512 result = args.Call(enum_fun);
8513 }
8514 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>());
Ben Murdochda12d292016-06-02 14:46:10 +01008515 if (result.is_null()) return Just(true);
8516 DCHECK(result->IsJSArray() || result->HasSloppyArgumentsElements());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008517 // The accumulator takes care of string/symbol filtering.
8518 if (type == kIndexed) {
Ben Murdochda12d292016-06-02 14:46:10 +01008519 accumulator->AddElementKeysFromInterceptor(result);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008520 } else {
Ben Murdochda12d292016-06-02 14:46:10 +01008521 accumulator->AddKeys(result, DO_NOT_CONVERT);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008522 }
8523 return Just(true);
8524}
8525
8526
8527// Returns |true| on success, |false| if prototype walking should be stopped,
8528// |nothing| if an exception was thrown.
8529static Maybe<bool> GetKeysFromJSObject(Isolate* isolate,
8530 Handle<JSReceiver> receiver,
8531 Handle<JSObject> object,
8532 PropertyFilter* filter,
Ben Murdoch097c5b22016-05-18 11:27:45 +01008533 KeyCollectionType type,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008534 KeyAccumulator* accumulator) {
8535 accumulator->NextPrototype();
8536 // Check access rights if required.
8537 if (object->IsAccessCheckNeeded() &&
8538 !isolate->MayAccess(handle(isolate->context()), object)) {
8539 // The cross-origin spec says that [[Enumerate]] shall return an empty
8540 // iterator when it doesn't have access...
Ben Murdoch097c5b22016-05-18 11:27:45 +01008541 if (type == INCLUDE_PROTOS) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008542 return Just(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008543 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008544 // ...whereas [[OwnPropertyKeys]] shall return whitelisted properties.
Ben Murdoch097c5b22016-05-18 11:27:45 +01008545 DCHECK_EQ(OWN_ONLY, type);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008546 *filter = static_cast<PropertyFilter>(*filter | ONLY_ALL_CAN_READ);
8547 }
Steve Block44f0eee2011-05-26 01:26:41 +01008548
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008549 JSObject::CollectOwnElementKeys(object, accumulator, *filter);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008550
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008551 // Add the element keys from the interceptor.
8552 Maybe<bool> success =
8553 GetKeysFromInterceptor<v8::IndexedPropertyEnumeratorCallback, kIndexed>(
8554 isolate, receiver, object, *filter, accumulator);
8555 MAYBE_RETURN(success, Nothing<bool>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008556
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008557 if (*filter == ENUMERABLE_STRINGS) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01008558 Handle<FixedArray> enum_keys = JSObject::GetEnumPropertyKeys(object);
8559 accumulator->AddKeys(enum_keys, DO_NOT_CONVERT);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008560 } else {
8561 object->CollectOwnPropertyNames(accumulator, *filter);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008562 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008563
8564 // Add the property keys from the interceptor.
8565 success = GetKeysFromInterceptor<v8::GenericNamedPropertyEnumeratorCallback,
8566 kNamed>(isolate, receiver, object, *filter,
8567 accumulator);
8568 MAYBE_RETURN(success, Nothing<bool>());
8569 return Just(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00008570}
8571
8572
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008573// Helper function for JSReceiver::GetKeys() below. Can be called recursively.
8574// Returns |true| or |nothing|.
8575static Maybe<bool> GetKeys_Internal(Isolate* isolate,
8576 Handle<JSReceiver> receiver,
8577 Handle<JSReceiver> object,
Ben Murdoch097c5b22016-05-18 11:27:45 +01008578 KeyCollectionType type,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008579 PropertyFilter filter,
8580 KeyAccumulator* accumulator) {
Ben Murdochda12d292016-06-02 14:46:10 +01008581 // Proxies have no hidden prototype and we should not trigger the
8582 // [[GetPrototypeOf]] trap on the last iteration when using
8583 // AdvanceFollowingProxies.
8584 if (type == OWN_ONLY && object->IsJSProxy()) {
8585 MAYBE_RETURN(JSProxy::OwnPropertyKeys(isolate, receiver,
8586 Handle<JSProxy>::cast(object), filter,
8587 accumulator),
8588 Nothing<bool>());
8589 return Just(true);
8590 }
8591
Ben Murdoch097c5b22016-05-18 11:27:45 +01008592 PrototypeIterator::WhereToEnd end = type == OWN_ONLY
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008593 ? PrototypeIterator::END_AT_NON_HIDDEN
8594 : PrototypeIterator::END_AT_NULL;
8595 for (PrototypeIterator iter(isolate, object,
Ben Murdoch097c5b22016-05-18 11:27:45 +01008596 PrototypeIterator::START_AT_RECEIVER, end);
Ben Murdochda12d292016-06-02 14:46:10 +01008597 !iter.IsAtEnd();) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008598 Handle<JSReceiver> current =
8599 PrototypeIterator::GetCurrent<JSReceiver>(iter);
8600 Maybe<bool> result = Just(false); // Dummy initialization.
8601 if (current->IsJSProxy()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01008602 result = JSProxy::OwnPropertyKeys(isolate, receiver,
8603 Handle<JSProxy>::cast(current), filter,
8604 accumulator);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008605 } else {
8606 DCHECK(current->IsJSObject());
8607 result = GetKeysFromJSObject(isolate, receiver,
8608 Handle<JSObject>::cast(current), &filter,
8609 type, accumulator);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008610 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008611 MAYBE_RETURN(result, Nothing<bool>());
8612 if (!result.FromJust()) break; // |false| means "stop iterating".
Ben Murdochda12d292016-06-02 14:46:10 +01008613 // Iterate through proxies but ignore access checks for the ALL_CAN_READ
8614 // case on API objects for OWN_ONLY keys handlede in GgetKeysFromJSObject.
8615 if (!iter.AdvanceFollowingProxiesIgnoringAccessChecks()) {
8616 return Nothing<bool>();
8617 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008618 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008619 return Just(true);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008620}
8621
8622
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008623// ES6 9.5.12
8624// Returns |true| on success, |nothing| in case of exception.
8625// static
8626Maybe<bool> JSProxy::OwnPropertyKeys(Isolate* isolate,
8627 Handle<JSReceiver> receiver,
8628 Handle<JSProxy> proxy,
8629 PropertyFilter filter,
8630 KeyAccumulator* accumulator) {
8631 STACK_CHECK(Nothing<bool>());
8632 // 1. Let handler be the value of the [[ProxyHandler]] internal slot of O.
8633 Handle<Object> handler(proxy->handler(), isolate);
8634 // 2. If handler is null, throw a TypeError exception.
8635 // 3. Assert: Type(handler) is Object.
8636 if (proxy->IsRevoked()) {
8637 isolate->Throw(*isolate->factory()->NewTypeError(
8638 MessageTemplate::kProxyRevoked, isolate->factory()->ownKeys_string()));
8639 return Nothing<bool>();
8640 }
8641 // 4. Let target be the value of the [[ProxyTarget]] internal slot of O.
8642 Handle<JSReceiver> target(proxy->target(), isolate);
8643 // 5. Let trap be ? GetMethod(handler, "ownKeys").
8644 Handle<Object> trap;
8645 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
8646 isolate, trap, Object::GetMethod(Handle<JSReceiver>::cast(handler),
8647 isolate->factory()->ownKeys_string()),
8648 Nothing<bool>());
8649 // 6. If trap is undefined, then
8650 if (trap->IsUndefined()) {
8651 // 6a. Return target.[[OwnPropertyKeys]]().
8652 return GetKeys_Internal(isolate, receiver, target, OWN_ONLY, filter,
8653 accumulator);
8654 }
8655 // 7. Let trapResultArray be Call(trap, handler, «target»).
8656 Handle<Object> trap_result_array;
8657 Handle<Object> args[] = {target};
8658 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
8659 isolate, trap_result_array,
8660 Execution::Call(isolate, trap, handler, arraysize(args), args),
8661 Nothing<bool>());
8662 // 8. Let trapResult be ? CreateListFromArrayLike(trapResultArray,
8663 // «String, Symbol»).
8664 Handle<FixedArray> trap_result;
8665 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
8666 isolate, trap_result,
8667 Object::CreateListFromArrayLike(isolate, trap_result_array,
8668 ElementTypes::kStringAndSymbol),
8669 Nothing<bool>());
8670 // 9. Let extensibleTarget be ? IsExtensible(target).
8671 Maybe<bool> maybe_extensible = JSReceiver::IsExtensible(target);
8672 MAYBE_RETURN(maybe_extensible, Nothing<bool>());
8673 bool extensible_target = maybe_extensible.FromJust();
8674 // 10. Let targetKeys be ? target.[[OwnPropertyKeys]]().
8675 Handle<FixedArray> target_keys;
8676 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, target_keys,
8677 JSReceiver::OwnPropertyKeys(target),
8678 Nothing<bool>());
8679 // 11. (Assert)
8680 // 12. Let targetConfigurableKeys be an empty List.
8681 // To save memory, we're re-using target_keys and will modify it in-place.
8682 Handle<FixedArray> target_configurable_keys = target_keys;
8683 // 13. Let targetNonconfigurableKeys be an empty List.
8684 Handle<FixedArray> target_nonconfigurable_keys =
8685 isolate->factory()->NewFixedArray(target_keys->length());
8686 int nonconfigurable_keys_length = 0;
8687 // 14. Repeat, for each element key of targetKeys:
8688 for (int i = 0; i < target_keys->length(); ++i) {
8689 // 14a. Let desc be ? target.[[GetOwnProperty]](key).
8690 PropertyDescriptor desc;
8691 Maybe<bool> found = JSReceiver::GetOwnPropertyDescriptor(
8692 isolate, target, handle(target_keys->get(i), isolate), &desc);
8693 MAYBE_RETURN(found, Nothing<bool>());
8694 // 14b. If desc is not undefined and desc.[[Configurable]] is false, then
8695 if (found.FromJust() && !desc.configurable()) {
8696 // 14b i. Append key as an element of targetNonconfigurableKeys.
8697 target_nonconfigurable_keys->set(nonconfigurable_keys_length,
8698 target_keys->get(i));
8699 nonconfigurable_keys_length++;
8700 // The key was moved, null it out in the original list.
8701 target_keys->set(i, Smi::FromInt(0));
8702 } else {
8703 // 14c. Else,
8704 // 14c i. Append key as an element of targetConfigurableKeys.
8705 // (No-op, just keep it in |target_keys|.)
Steve Blocka7e24c12009-10-30 11:49:00 +00008706 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008707 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008708 accumulator->NextPrototype(); // Prepare for accumulating keys.
8709 // 15. If extensibleTarget is true and targetNonconfigurableKeys is empty,
8710 // then:
8711 if (extensible_target && nonconfigurable_keys_length == 0) {
8712 // 15a. Return trapResult.
8713 return accumulator->AddKeysFromProxy(proxy, trap_result);
8714 }
8715 // 16. Let uncheckedResultKeys be a new List which is a copy of trapResult.
Ben Murdochda12d292016-06-02 14:46:10 +01008716 Zone set_zone(isolate->allocator());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008717 const int kPresent = 1;
8718 const int kGone = 0;
8719 IdentityMap<int> unchecked_result_keys(isolate->heap(), &set_zone);
Ben Murdoch097c5b22016-05-18 11:27:45 +01008720 int unchecked_result_keys_size = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008721 for (int i = 0; i < trap_result->length(); ++i) {
8722 DCHECK(trap_result->get(i)->IsUniqueName());
Ben Murdoch097c5b22016-05-18 11:27:45 +01008723 Object* key = trap_result->get(i);
8724 int* entry = unchecked_result_keys.Get(key);
8725 if (*entry != kPresent) {
8726 *entry = kPresent;
8727 unchecked_result_keys_size++;
8728 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008729 }
8730 // 17. Repeat, for each key that is an element of targetNonconfigurableKeys:
8731 for (int i = 0; i < nonconfigurable_keys_length; ++i) {
8732 Object* key = target_nonconfigurable_keys->get(i);
8733 // 17a. If key is not an element of uncheckedResultKeys, throw a
8734 // TypeError exception.
8735 int* found = unchecked_result_keys.Find(key);
8736 if (found == nullptr || *found == kGone) {
8737 isolate->Throw(*isolate->factory()->NewTypeError(
8738 MessageTemplate::kProxyOwnKeysMissing, handle(key, isolate)));
8739 return Nothing<bool>();
8740 }
8741 // 17b. Remove key from uncheckedResultKeys.
8742 *found = kGone;
8743 unchecked_result_keys_size--;
8744 }
8745 // 18. If extensibleTarget is true, return trapResult.
8746 if (extensible_target) {
8747 return accumulator->AddKeysFromProxy(proxy, trap_result);
8748 }
8749 // 19. Repeat, for each key that is an element of targetConfigurableKeys:
8750 for (int i = 0; i < target_configurable_keys->length(); ++i) {
8751 Object* key = target_configurable_keys->get(i);
8752 if (key->IsSmi()) continue; // Zapped entry, was nonconfigurable.
8753 // 19a. If key is not an element of uncheckedResultKeys, throw a
8754 // TypeError exception.
8755 int* found = unchecked_result_keys.Find(key);
8756 if (found == nullptr || *found == kGone) {
8757 isolate->Throw(*isolate->factory()->NewTypeError(
8758 MessageTemplate::kProxyOwnKeysMissing, handle(key, isolate)));
8759 return Nothing<bool>();
8760 }
8761 // 19b. Remove key from uncheckedResultKeys.
8762 *found = kGone;
8763 unchecked_result_keys_size--;
8764 }
8765 // 20. If uncheckedResultKeys is not empty, throw a TypeError exception.
8766 if (unchecked_result_keys_size != 0) {
8767 DCHECK_GT(unchecked_result_keys_size, 0);
8768 isolate->Throw(*isolate->factory()->NewTypeError(
8769 MessageTemplate::kProxyOwnKeysNonExtensible));
8770 return Nothing<bool>();
8771 }
8772 // 21. Return trapResult.
8773 return accumulator->AddKeysFromProxy(proxy, trap_result);
8774}
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008775
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008776MaybeHandle<FixedArray> JSReceiver::GetKeys(Handle<JSReceiver> object,
8777 KeyCollectionType type,
8778 PropertyFilter filter,
Ben Murdochda12d292016-06-02 14:46:10 +01008779 GetKeysConversion keys_conversion,
8780 bool filter_proxy_keys) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008781 USE(ContainsOnlyValidKeys);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008782 Isolate* isolate = object->GetIsolate();
Ben Murdoch097c5b22016-05-18 11:27:45 +01008783 KeyAccumulator accumulator(isolate, type, filter);
Ben Murdochda12d292016-06-02 14:46:10 +01008784 accumulator.set_filter_proxy_keys(filter_proxy_keys);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008785 MAYBE_RETURN(
8786 GetKeys_Internal(isolate, object, object, type, filter, &accumulator),
8787 MaybeHandle<FixedArray>());
8788 Handle<FixedArray> keys = accumulator.GetKeys(keys_conversion);
8789 DCHECK(ContainsOnlyValidKeys(keys));
8790 return keys;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008791}
8792
Ben Murdochda12d292016-06-02 14:46:10 +01008793MUST_USE_RESULT Maybe<bool> FastGetOwnValuesOrEntries(
8794 Isolate* isolate, Handle<JSReceiver> receiver, bool get_entries,
8795 Handle<FixedArray>* result) {
8796 Handle<Map> map(JSReceiver::cast(*receiver)->map(), isolate);
8797
8798 if (!map->IsJSObjectMap()) return Just(false);
8799 if (!map->OnlyHasSimpleProperties()) return Just(false);
8800
8801 Handle<JSObject> object(JSObject::cast(*receiver));
8802
8803 Handle<DescriptorArray> descriptors(map->instance_descriptors(), isolate);
8804 int number_of_own_descriptors = map->NumberOfOwnDescriptors();
8805 int number_of_own_elements =
8806 object->GetElementsAccessor()->GetCapacity(*object, object->elements());
8807 Handle<FixedArray> values_or_entries = isolate->factory()->NewFixedArray(
8808 number_of_own_descriptors + number_of_own_elements);
8809 int count = 0;
8810
8811 if (object->elements() != isolate->heap()->empty_fixed_array()) {
8812 MAYBE_RETURN(object->GetElementsAccessor()->CollectValuesOrEntries(
8813 isolate, object, values_or_entries, get_entries, &count,
8814 ENUMERABLE_STRINGS),
8815 Nothing<bool>());
8816 }
8817
8818 bool stable = object->map() == *map;
8819
8820 for (int index = 0; index < number_of_own_descriptors; index++) {
8821 Handle<Name> next_key(descriptors->GetKey(index), isolate);
8822 if (!next_key->IsString()) continue;
8823 Handle<Object> prop_value;
8824
8825 // Directly decode from the descriptor array if |from| did not change shape.
8826 if (stable) {
8827 PropertyDetails details = descriptors->GetDetails(index);
8828 if (!details.IsEnumerable()) continue;
8829 if (details.kind() == kData) {
8830 if (details.location() == kDescriptor) {
8831 prop_value = handle(descriptors->GetValue(index), isolate);
8832 } else {
8833 Representation representation = details.representation();
8834 FieldIndex field_index = FieldIndex::ForDescriptor(*map, index);
8835 prop_value =
8836 JSObject::FastPropertyAt(object, representation, field_index);
8837 }
8838 } else {
8839 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
8840 isolate, prop_value, JSReceiver::GetProperty(object, next_key),
8841 Nothing<bool>());
8842 stable = object->map() == *map;
8843 }
8844 } else {
8845 // If the map did change, do a slower lookup. We are still guaranteed that
8846 // the object has a simple shape, and that the key is a name.
8847 LookupIterator it(object, next_key, LookupIterator::OWN_SKIP_INTERCEPTOR);
8848 if (!it.IsFound()) continue;
8849 DCHECK(it.state() == LookupIterator::DATA ||
8850 it.state() == LookupIterator::ACCESSOR);
8851 if (!it.IsEnumerable()) continue;
8852 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
8853 isolate, prop_value, Object::GetProperty(&it), Nothing<bool>());
8854 }
8855
8856 if (get_entries) {
8857 prop_value = MakeEntryPair(isolate, next_key, prop_value);
8858 }
8859
8860 values_or_entries->set(count, *prop_value);
8861 count++;
8862 }
8863
8864 if (count < values_or_entries->length()) values_or_entries->Shrink(count);
8865 *result = values_or_entries;
8866 return Just(true);
8867}
8868
Ben Murdoch097c5b22016-05-18 11:27:45 +01008869MaybeHandle<FixedArray> GetOwnValuesOrEntries(Isolate* isolate,
8870 Handle<JSReceiver> object,
8871 PropertyFilter filter,
8872 bool get_entries) {
Ben Murdochda12d292016-06-02 14:46:10 +01008873 Handle<FixedArray> values_or_entries;
8874 if (filter == ENUMERABLE_STRINGS) {
8875 Maybe<bool> fast_values_or_entries = FastGetOwnValuesOrEntries(
8876 isolate, object, get_entries, &values_or_entries);
8877 if (fast_values_or_entries.IsNothing()) return MaybeHandle<FixedArray>();
8878 if (fast_values_or_entries.FromJust()) return values_or_entries;
8879 }
8880
Ben Murdoch097c5b22016-05-18 11:27:45 +01008881 PropertyFilter key_filter =
8882 static_cast<PropertyFilter>(filter & ~ONLY_ENUMERABLE);
8883 KeyAccumulator accumulator(isolate, OWN_ONLY, key_filter);
8884 MAYBE_RETURN(GetKeys_Internal(isolate, object, object, OWN_ONLY, key_filter,
8885 &accumulator),
8886 MaybeHandle<FixedArray>());
8887 Handle<FixedArray> keys = accumulator.GetKeys(CONVERT_TO_STRING);
8888 DCHECK(ContainsOnlyValidKeys(keys));
8889
Ben Murdochda12d292016-06-02 14:46:10 +01008890 values_or_entries = isolate->factory()->NewFixedArray(keys->length());
Ben Murdoch097c5b22016-05-18 11:27:45 +01008891 int length = 0;
8892
8893 for (int i = 0; i < keys->length(); ++i) {
8894 Handle<Name> key = Handle<Name>::cast(handle(keys->get(i), isolate));
8895
8896 if (filter & ONLY_ENUMERABLE) {
8897 PropertyDescriptor descriptor;
8898 Maybe<bool> did_get_descriptor = JSReceiver::GetOwnPropertyDescriptor(
8899 isolate, object, key, &descriptor);
8900 MAYBE_RETURN(did_get_descriptor, MaybeHandle<FixedArray>());
8901 if (!did_get_descriptor.FromJust() || !descriptor.enumerable()) continue;
8902 }
8903
8904 Handle<Object> value;
8905 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
8906 isolate, value, JSReceiver::GetPropertyOrElement(object, key),
8907 MaybeHandle<FixedArray>());
8908
8909 if (get_entries) {
8910 Handle<FixedArray> entry_storage =
8911 isolate->factory()->NewUninitializedFixedArray(2);
8912 entry_storage->set(0, *key);
8913 entry_storage->set(1, *value);
8914 value = isolate->factory()->NewJSArrayWithElements(entry_storage,
8915 FAST_ELEMENTS, 2);
8916 }
8917
8918 values_or_entries->set(length, *value);
8919 length++;
8920 }
8921 if (length < values_or_entries->length()) values_or_entries->Shrink(length);
8922 return values_or_entries;
8923}
8924
8925MaybeHandle<FixedArray> JSReceiver::GetOwnValues(Handle<JSReceiver> object,
8926 PropertyFilter filter) {
8927 return GetOwnValuesOrEntries(object->GetIsolate(), object, filter, false);
8928}
8929
8930MaybeHandle<FixedArray> JSReceiver::GetOwnEntries(Handle<JSReceiver> object,
8931 PropertyFilter filter) {
8932 return GetOwnValuesOrEntries(object->GetIsolate(), object, filter, true);
8933}
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008934
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008935bool Map::DictionaryElementsInPrototypeChainOnly() {
8936 if (IsDictionaryElementsKind(elements_kind())) {
8937 return false;
8938 }
8939
8940 for (PrototypeIterator iter(this); !iter.IsAtEnd(); iter.Advance()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008941 // Be conservative, don't walk into proxies.
8942 if (iter.GetCurrent()->IsJSProxy()) return true;
8943 // String wrappers have non-configurable, non-writable elements.
8944 if (iter.GetCurrent()->IsStringWrapper()) return true;
8945 JSObject* current = iter.GetCurrent<JSObject>();
8946
8947 if (current->HasDictionaryElements() &&
8948 current->element_dictionary()->requires_slow_elements()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008949 return true;
8950 }
8951
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008952 if (current->HasSlowArgumentsElements()) {
8953 FixedArray* parameter_map = FixedArray::cast(current->elements());
8954 Object* arguments = parameter_map->get(1);
8955 if (SeededNumberDictionary::cast(arguments)->requires_slow_elements()) {
8956 return true;
8957 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008958 }
8959 }
8960
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008961 return false;
Leon Clarkef7060e22010-06-03 12:02:55 +01008962}
8963
8964
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008965MaybeHandle<Object> JSObject::DefineAccessor(Handle<JSObject> object,
8966 Handle<Name> name,
8967 Handle<Object> getter,
8968 Handle<Object> setter,
8969 PropertyAttributes attributes) {
8970 Isolate* isolate = object->GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008971
8972 LookupIterator it = LookupIterator::PropertyOrElement(
8973 isolate, object, name, LookupIterator::HIDDEN_SKIP_INTERCEPTOR);
8974 return DefineAccessor(&it, getter, setter, attributes);
8975}
8976
8977
8978MaybeHandle<Object> JSObject::DefineAccessor(LookupIterator* it,
8979 Handle<Object> getter,
8980 Handle<Object> setter,
8981 PropertyAttributes attributes) {
8982 Isolate* isolate = it->isolate();
8983
Ben Murdochda12d292016-06-02 14:46:10 +01008984 it->UpdateProtector();
8985
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008986 if (it->state() == LookupIterator::ACCESS_CHECK) {
8987 if (!it->HasAccess()) {
8988 isolate->ReportFailedAccessCheck(it->GetHolder<JSObject>());
8989 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
8990 return isolate->factory()->undefined_value();
8991 }
8992 it->Next();
Steve Blocka7e24c12009-10-30 11:49:00 +00008993 }
8994
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008995 Handle<JSObject> object = Handle<JSObject>::cast(it->GetReceiver());
8996 // Ignore accessors on typed arrays.
8997 if (it->IsElement() && object->HasFixedTypedArrayElements()) {
8998 return it->factory()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00008999 }
9000
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009001 Handle<Object> old_value = isolate->factory()->the_hole_value();
9002 bool is_observed = object->map()->is_observed() &&
Ben Murdoch097c5b22016-05-18 11:27:45 +01009003 (it->IsElement() || !it->name()->IsPrivate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009004 bool preexists = false;
9005 if (is_observed) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009006 CHECK(GetPropertyAttributes(it).IsJust());
9007 preexists = it->IsFound();
9008 if (preexists && (it->state() == LookupIterator::DATA ||
9009 it->GetAccessors()->IsAccessorInfo())) {
Ben Murdochda12d292016-06-02 14:46:10 +01009010 old_value = Object::GetProperty(it).ToHandleChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009011 }
9012 }
9013
Ben Murdoch097c5b22016-05-18 11:27:45 +01009014 DCHECK(getter->IsCallable() || getter->IsUndefined() || getter->IsNull() ||
9015 getter->IsFunctionTemplateInfo());
9016 DCHECK(setter->IsCallable() || setter->IsUndefined() || setter->IsNull() ||
9017 getter->IsFunctionTemplateInfo());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009018 // At least one of the accessors needs to be a new value.
9019 DCHECK(!getter->IsNull() || !setter->IsNull());
9020 if (!getter->IsNull()) {
9021 it->TransitionToAccessorProperty(ACCESSOR_GETTER, getter, attributes);
9022 }
9023 if (!setter->IsNull()) {
9024 it->TransitionToAccessorProperty(ACCESSOR_SETTER, setter, attributes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009025 }
9026
9027 if (is_observed) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009028 // Make sure the top context isn't changed.
9029 AssertNoContextChange ncc(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009030 const char* type = preexists ? "reconfigure" : "add";
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009031 RETURN_ON_EXCEPTION(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009032 isolate, EnqueueChangeRecord(object, type, it->GetName(), old_value),
9033 Object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009034 }
9035
9036 return isolate->factory()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00009037}
9038
9039
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009040MaybeHandle<Object> JSObject::SetAccessor(Handle<JSObject> object,
9041 Handle<AccessorInfo> info) {
9042 Isolate* isolate = object->GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009043 Handle<Name> name(Name::cast(info->name()), isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009044
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009045 LookupIterator it = LookupIterator::PropertyOrElement(
9046 isolate, object, name, LookupIterator::HIDDEN_SKIP_INTERCEPTOR);
Leon Clarkef7060e22010-06-03 12:02:55 +01009047
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009048 // Duplicate ACCESS_CHECK outside of GetPropertyAttributes for the case that
9049 // the FailedAccessCheckCallbackFunction doesn't throw an exception.
9050 //
9051 // TODO(verwaest): Force throw an exception if the callback doesn't, so we can
9052 // remove reliance on default return values.
9053 if (it.state() == LookupIterator::ACCESS_CHECK) {
9054 if (!it.HasAccess()) {
9055 isolate->ReportFailedAccessCheck(object);
9056 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
9057 return it.factory()->undefined_value();
Leon Clarkef7060e22010-06-03 12:02:55 +01009058 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009059 it.Next();
Leon Clarkef7060e22010-06-03 12:02:55 +01009060 }
9061
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009062 // Ignore accessors on typed arrays.
9063 if (it.IsElement() && object->HasFixedTypedArrayElements()) {
9064 return it.factory()->undefined_value();
9065 }
9066
9067 CHECK(GetPropertyAttributes(&it).IsJust());
9068
9069 // ES5 forbids turning a property into an accessor if it's not
9070 // configurable. See 8.6.1 (Table 5).
9071 if (it.IsFound() && !it.IsConfigurable()) {
9072 return it.factory()->undefined_value();
9073 }
9074
9075 it.TransitionToAccessorPair(info, info->property_attributes());
9076
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009077 return object;
Leon Clarkef7060e22010-06-03 12:02:55 +01009078}
9079
9080
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009081MaybeHandle<Object> JSObject::GetAccessor(Handle<JSObject> object,
9082 Handle<Name> name,
9083 AccessorComponent component) {
9084 Isolate* isolate = object->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01009085
Steve Blocka7e24c12009-10-30 11:49:00 +00009086 // Make sure that the top context does not change when doing callbacks or
9087 // interceptor calls.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009088 AssertNoContextChange ncc(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00009089
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009090 LookupIterator it = LookupIterator::PropertyOrElement(
9091 isolate, object, name, LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
9092
9093 for (; it.IsFound(); it.Next()) {
9094 switch (it.state()) {
9095 case LookupIterator::INTERCEPTOR:
9096 case LookupIterator::NOT_FOUND:
9097 case LookupIterator::TRANSITION:
9098 UNREACHABLE();
9099
9100 case LookupIterator::ACCESS_CHECK:
9101 if (it.HasAccess()) continue;
9102 isolate->ReportFailedAccessCheck(it.GetHolder<JSObject>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009103 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
9104 return isolate->factory()->undefined_value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009105
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009106 case LookupIterator::JSPROXY:
9107 return isolate->factory()->undefined_value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009108
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009109 case LookupIterator::INTEGER_INDEXED_EXOTIC:
9110 return isolate->factory()->undefined_value();
9111 case LookupIterator::DATA:
9112 continue;
9113 case LookupIterator::ACCESSOR: {
9114 Handle<Object> maybe_pair = it.GetAccessors();
9115 if (maybe_pair->IsAccessorPair()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01009116 return AccessorPair::GetComponent(
9117 Handle<AccessorPair>::cast(maybe_pair), component);
Steve Blocka7e24c12009-10-30 11:49:00 +00009118 }
9119 }
9120 }
9121 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009122
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009123 return isolate->factory()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00009124}
9125
9126
9127Object* JSObject::SlowReverseLookup(Object* value) {
9128 if (HasFastProperties()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009129 int number_of_own_descriptors = map()->NumberOfOwnDescriptors();
Steve Blocka7e24c12009-10-30 11:49:00 +00009130 DescriptorArray* descs = map()->instance_descriptors();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009131 bool value_is_number = value->IsNumber();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009132 for (int i = 0; i < number_of_own_descriptors; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009133 if (descs->GetType(i) == DATA) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009134 FieldIndex field_index = FieldIndex::ForDescriptor(map(), i);
9135 if (IsUnboxedDoubleField(field_index)) {
9136 if (value_is_number) {
9137 double property = RawFastDoublePropertyAt(field_index);
9138 if (property == value->Number()) {
9139 return descs->GetKey(i);
9140 }
9141 }
9142 } else {
9143 Object* property = RawFastPropertyAt(field_index);
9144 if (field_index.is_double()) {
9145 DCHECK(property->IsMutableHeapNumber());
9146 if (value_is_number && property->Number() == value->Number()) {
9147 return descs->GetKey(i);
9148 }
9149 } else if (property == value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009150 return descs->GetKey(i);
9151 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009152 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009153 } else if (descs->GetType(i) == DATA_CONSTANT) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009154 if (descs->GetConstant(i) == value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009155 return descs->GetKey(i);
9156 }
9157 }
9158 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01009159 return GetHeap()->undefined_value();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009160 } else if (IsJSGlobalObject()) {
9161 return global_dictionary()->SlowReverseLookup(value);
Steve Blocka7e24c12009-10-30 11:49:00 +00009162 } else {
9163 return property_dictionary()->SlowReverseLookup(value);
9164 }
9165}
9166
9167
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009168Handle<Map> Map::RawCopy(Handle<Map> map, int instance_size) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009169 Isolate* isolate = map->GetIsolate();
9170 Handle<Map> result =
9171 isolate->factory()->NewMap(map->instance_type(), instance_size);
9172 Handle<Object> prototype(map->prototype(), isolate);
9173 Map::SetPrototype(result, prototype);
9174 result->set_constructor_or_backpointer(map->GetConstructor());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009175 result->set_bit_field(map->bit_field());
9176 result->set_bit_field2(map->bit_field2());
9177 int new_bit_field3 = map->bit_field3();
9178 new_bit_field3 = OwnsDescriptors::update(new_bit_field3, true);
9179 new_bit_field3 = NumberOfOwnDescriptorsBits::update(new_bit_field3, 0);
9180 new_bit_field3 = EnumLengthBits::update(new_bit_field3,
9181 kInvalidEnumCacheSentinel);
9182 new_bit_field3 = Deprecated::update(new_bit_field3, false);
9183 if (!map->is_dictionary_map()) {
9184 new_bit_field3 = IsUnstable::update(new_bit_field3, false);
John Reck59135872010-11-02 12:39:01 -07009185 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009186 result->set_bit_field3(new_bit_field3);
Steve Blocka7e24c12009-10-30 11:49:00 +00009187 return result;
9188}
9189
9190
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009191Handle<Map> Map::Normalize(Handle<Map> fast_map, PropertyNormalizationMode mode,
9192 const char* reason) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009193 DCHECK(!fast_map->is_dictionary_map());
9194
9195 Isolate* isolate = fast_map->GetIsolate();
9196 Handle<Object> maybe_cache(isolate->native_context()->normalized_map_cache(),
9197 isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009198 bool use_cache = !fast_map->is_prototype_map() && !maybe_cache->IsUndefined();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009199 Handle<NormalizedMapCache> cache;
9200 if (use_cache) cache = Handle<NormalizedMapCache>::cast(maybe_cache);
9201
9202 Handle<Map> new_map;
9203 if (use_cache && cache->Get(fast_map, mode).ToHandle(&new_map)) {
9204#ifdef VERIFY_HEAP
9205 if (FLAG_verify_heap) new_map->DictionaryMapVerify();
9206#endif
9207#ifdef ENABLE_SLOW_DCHECKS
9208 if (FLAG_enable_slow_asserts) {
9209 // The cached map should match newly created normalized map bit-by-bit,
9210 // except for the code cache, which can contain some ics which can be
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009211 // applied to the shared map, dependent code and weak cell cache.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009212 Handle<Map> fresh = Map::CopyNormalized(fast_map, mode);
9213
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009214 if (new_map->is_prototype_map()) {
9215 // For prototype maps, the PrototypeInfo is not copied.
9216 DCHECK(memcmp(fresh->address(), new_map->address(),
9217 kTransitionsOrPrototypeInfoOffset) == 0);
9218 DCHECK(fresh->raw_transitions() == Smi::FromInt(0));
9219 STATIC_ASSERT(kDescriptorsOffset ==
9220 kTransitionsOrPrototypeInfoOffset + kPointerSize);
9221 DCHECK(memcmp(HeapObject::RawField(*fresh, kDescriptorsOffset),
9222 HeapObject::RawField(*new_map, kDescriptorsOffset),
9223 kCodeCacheOffset - kDescriptorsOffset) == 0);
9224 } else {
9225 DCHECK(memcmp(fresh->address(), new_map->address(),
9226 Map::kCodeCacheOffset) == 0);
9227 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009228 STATIC_ASSERT(Map::kDependentCodeOffset ==
9229 Map::kCodeCacheOffset + kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009230 STATIC_ASSERT(Map::kWeakCellCacheOffset ==
9231 Map::kDependentCodeOffset + kPointerSize);
9232 int offset = Map::kWeakCellCacheOffset + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009233 DCHECK(memcmp(fresh->address() + offset,
9234 new_map->address() + offset,
9235 Map::kSize - offset) == 0);
9236 }
9237#endif
9238 } else {
9239 new_map = Map::CopyNormalized(fast_map, mode);
9240 if (use_cache) {
9241 cache->Set(fast_map, new_map);
Ben Murdoch097c5b22016-05-18 11:27:45 +01009242 isolate->counters()->maps_normalized()->Increment();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009243 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009244#if TRACE_MAPS
9245 if (FLAG_trace_maps) {
9246 PrintF("[TraceMaps: Normalize from= %p to= %p reason= %s ]\n",
9247 reinterpret_cast<void*>(*fast_map),
9248 reinterpret_cast<void*>(*new_map), reason);
9249 }
9250#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009251 }
9252 fast_map->NotifyLeafMapLayoutChange();
9253 return new_map;
9254}
9255
9256
9257Handle<Map> Map::CopyNormalized(Handle<Map> map,
9258 PropertyNormalizationMode mode) {
9259 int new_instance_size = map->instance_size();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01009260 if (mode == CLEAR_INOBJECT_PROPERTIES) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009261 new_instance_size -= map->GetInObjectProperties() * kPointerSize;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01009262 }
9263
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009264 Handle<Map> result = RawCopy(map, new_instance_size);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01009265
9266 if (mode != CLEAR_INOBJECT_PROPERTIES) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009267 result->SetInObjectProperties(map->GetInObjectProperties());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01009268 }
9269
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009270 result->set_dictionary_map(true);
9271 result->set_migration_target(false);
Ben Murdoch097c5b22016-05-18 11:27:45 +01009272 result->set_construction_counter(kNoSlackTracking);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01009273
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009274#ifdef VERIFY_HEAP
9275 if (FLAG_verify_heap) result->DictionaryMapVerify();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01009276#endif
9277
9278 return result;
9279}
9280
9281
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009282Handle<Map> Map::CopyInitialMap(Handle<Map> map, int instance_size,
9283 int in_object_properties,
9284 int unused_property_fields) {
9285#ifdef DEBUG
9286 Isolate* isolate = map->GetIsolate();
Ben Murdochda12d292016-06-02 14:46:10 +01009287 // Strict function maps have Function as a constructor but the
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009288 // Function's initial map is a sloppy function map. Same holds for
9289 // GeneratorFunction and its initial map.
9290 Object* constructor = map->GetConstructor();
9291 DCHECK(constructor->IsJSFunction());
9292 DCHECK(*map == JSFunction::cast(constructor)->initial_map() ||
9293 *map == *isolate->strict_function_map() ||
Ben Murdochda12d292016-06-02 14:46:10 +01009294 *map == *isolate->strict_generator_function_map());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009295#endif
9296 // Initial maps must always own their descriptors and it's descriptor array
9297 // does not contain descriptors that do not belong to the map.
9298 DCHECK(map->owns_descriptors());
9299 DCHECK_EQ(map->NumberOfOwnDescriptors(),
9300 map->instance_descriptors()->number_of_descriptors());
9301
9302 Handle<Map> result = RawCopy(map, instance_size);
9303
9304 // Please note instance_type and instance_size are set when allocated.
9305 result->SetInObjectProperties(in_object_properties);
9306 result->set_unused_property_fields(unused_property_fields);
9307
9308 int number_of_own_descriptors = map->NumberOfOwnDescriptors();
9309 if (number_of_own_descriptors > 0) {
9310 // The copy will use the same descriptors array.
9311 result->UpdateDescriptors(map->instance_descriptors(),
9312 map->GetLayoutDescriptor());
9313 result->SetNumberOfOwnDescriptors(number_of_own_descriptors);
9314
9315 DCHECK_EQ(result->NumberOfFields(),
9316 in_object_properties - unused_property_fields);
9317 }
9318
9319 return result;
9320}
9321
9322
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009323Handle<Map> Map::CopyDropDescriptors(Handle<Map> map) {
9324 Handle<Map> result = RawCopy(map, map->instance_size());
9325
9326 // Please note instance_type and instance_size are set when allocated.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009327 if (map->IsJSObjectMap()) {
9328 result->SetInObjectProperties(map->GetInObjectProperties());
9329 result->set_unused_property_fields(map->unused_property_fields());
9330 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009331 result->ClearCodeCache(map->GetHeap());
9332 map->NotifyLeafMapLayoutChange();
9333 return result;
9334}
9335
9336
9337Handle<Map> Map::ShareDescriptor(Handle<Map> map,
9338 Handle<DescriptorArray> descriptors,
9339 Descriptor* descriptor) {
9340 // Sanity check. This path is only to be taken if the map owns its descriptor
9341 // array, implying that its NumberOfOwnDescriptors equals the number of
9342 // descriptors in the descriptor array.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009343 DCHECK_EQ(map->NumberOfOwnDescriptors(),
9344 map->instance_descriptors()->number_of_descriptors());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009345
9346 Handle<Map> result = CopyDropDescriptors(map);
9347 Handle<Name> name = descriptor->GetKey();
9348
9349 // Ensure there's space for the new descriptor in the shared descriptor array.
9350 if (descriptors->NumberOfSlackDescriptors() == 0) {
9351 int old_size = descriptors->number_of_descriptors();
9352 if (old_size == 0) {
9353 descriptors = DescriptorArray::Allocate(map->GetIsolate(), 0, 1);
9354 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009355 int slack = SlackForArraySize(old_size, kMaxNumberOfDescriptors);
9356 EnsureDescriptorSlack(map, slack);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009357 descriptors = handle(map->instance_descriptors());
9358 }
John Reck59135872010-11-02 12:39:01 -07009359 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009360
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009361 Handle<LayoutDescriptor> layout_descriptor =
9362 FLAG_unbox_double_fields
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009363 ? LayoutDescriptor::ShareAppend(map, descriptor->GetDetails())
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009364 : handle(LayoutDescriptor::FastPointerLayout(), map->GetIsolate());
9365
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009366 {
9367 DisallowHeapAllocation no_gc;
9368 descriptors->Append(descriptor);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009369 result->InitializeDescriptors(*descriptors, *layout_descriptor);
John Reck59135872010-11-02 12:39:01 -07009370 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009371
9372 DCHECK(result->NumberOfOwnDescriptors() == map->NumberOfOwnDescriptors() + 1);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009373 ConnectTransition(map, result, name, SIMPLE_PROPERTY_TRANSITION);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009374
9375 return result;
9376}
9377
9378
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009379#if TRACE_MAPS
9380
9381// static
9382void Map::TraceTransition(const char* what, Map* from, Map* to, Name* name) {
9383 if (FLAG_trace_maps) {
9384 PrintF("[TraceMaps: %s from= %p to= %p name= ", what,
9385 reinterpret_cast<void*>(from), reinterpret_cast<void*>(to));
9386 name->NameShortPrint();
9387 PrintF(" ]\n");
9388 }
9389}
9390
9391
9392// static
9393void Map::TraceAllTransitions(Map* map) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009394 Object* transitions = map->raw_transitions();
9395 int num_transitions = TransitionArray::NumberOfTransitions(transitions);
9396 for (int i = -0; i < num_transitions; ++i) {
9397 Map* target = TransitionArray::GetTarget(transitions, i);
9398 Name* key = TransitionArray::GetKey(transitions, i);
9399 Map::TraceTransition("Transition", map, target, key);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009400 Map::TraceAllTransitions(target);
9401 }
9402}
9403
9404#endif // TRACE_MAPS
9405
9406
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009407void Map::ConnectTransition(Handle<Map> parent, Handle<Map> child,
9408 Handle<Name> name, SimpleTransitionFlag flag) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009409 if (!parent->GetBackPointer()->IsUndefined()) {
9410 parent->set_owns_descriptors(false);
9411 } else {
9412 // |parent| is initial map and it must keep the ownership, there must be no
9413 // descriptors in the descriptors array that do not belong to the map.
9414 DCHECK(parent->owns_descriptors());
9415 DCHECK_EQ(parent->NumberOfOwnDescriptors(),
9416 parent->instance_descriptors()->number_of_descriptors());
9417 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009418 if (parent->is_prototype_map()) {
9419 DCHECK(child->is_prototype_map());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009420#if TRACE_MAPS
9421 Map::TraceTransition("NoTransition", *parent, *child, *name);
9422#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009423 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009424 TransitionArray::Insert(parent, name, child, flag);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009425#if TRACE_MAPS
9426 Map::TraceTransition("Transition", *parent, *child, *name);
9427#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009428 }
9429}
9430
9431
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009432Handle<Map> Map::CopyReplaceDescriptors(
9433 Handle<Map> map, Handle<DescriptorArray> descriptors,
9434 Handle<LayoutDescriptor> layout_descriptor, TransitionFlag flag,
9435 MaybeHandle<Name> maybe_name, const char* reason,
9436 SimpleTransitionFlag simple_flag) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009437 DCHECK(descriptors->IsSortedNoDuplicates());
9438
9439 Handle<Map> result = CopyDropDescriptors(map);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009440
9441 if (!map->is_prototype_map()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009442 if (flag == INSERT_TRANSITION &&
9443 TransitionArray::CanHaveMoreTransitions(map)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009444 result->InitializeDescriptors(*descriptors, *layout_descriptor);
9445
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009446 Handle<Name> name;
9447 CHECK(maybe_name.ToHandle(&name));
9448 ConnectTransition(map, result, name, simple_flag);
9449 } else {
9450 int length = descriptors->number_of_descriptors();
9451 for (int i = 0; i < length; i++) {
9452 descriptors->SetRepresentation(i, Representation::Tagged());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009453 if (descriptors->GetDetails(i).type() == DATA) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01009454 descriptors->SetValue(i, FieldType::Any());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009455 }
9456 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009457 result->InitializeDescriptors(*descriptors,
9458 LayoutDescriptor::FastPointerLayout());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009459 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009460 } else {
9461 result->InitializeDescriptors(*descriptors, *layout_descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009462 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009463#if TRACE_MAPS
9464 if (FLAG_trace_maps &&
9465 // Mirror conditions above that did not call ConnectTransition().
9466 (map->is_prototype_map() ||
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009467 !(flag == INSERT_TRANSITION &&
9468 TransitionArray::CanHaveMoreTransitions(map)))) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009469 PrintF("[TraceMaps: ReplaceDescriptors from= %p to= %p reason= %s ]\n",
9470 reinterpret_cast<void*>(*map), reinterpret_cast<void*>(*result),
9471 reason);
9472 }
9473#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009474
9475 return result;
9476}
9477
9478
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009479// Creates transition tree starting from |split_map| and adding all descriptors
9480// starting from descriptor with index |split_map|.NumberOfOwnDescriptors().
9481// The way how it is done is tricky because of GC and special descriptors
9482// marking logic.
9483Handle<Map> Map::AddMissingTransitions(
9484 Handle<Map> split_map, Handle<DescriptorArray> descriptors,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009485 Handle<LayoutDescriptor> full_layout_descriptor) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009486 DCHECK(descriptors->IsSortedNoDuplicates());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009487 int split_nof = split_map->NumberOfOwnDescriptors();
9488 int nof_descriptors = descriptors->number_of_descriptors();
9489 DCHECK_LT(split_nof, nof_descriptors);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009490
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009491 // Start with creating last map which will own full descriptors array.
9492 // This is necessary to guarantee that GC will mark the whole descriptor
9493 // array if any of the allocations happening below fail.
9494 // Number of unused properties is temporarily incorrect and the layout
9495 // descriptor could unnecessarily be in slow mode but we will fix after
9496 // all the other intermediate maps are created.
9497 Handle<Map> last_map = CopyDropDescriptors(split_map);
9498 last_map->InitializeDescriptors(*descriptors, *full_layout_descriptor);
9499 last_map->set_unused_property_fields(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009500
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009501 // During creation of intermediate maps we violate descriptors sharing
9502 // invariant since the last map is not yet connected to the transition tree
9503 // we create here. But it is safe because GC never trims map's descriptors
9504 // if there are no dead transitions from that map and this is exactly the
9505 // case for all the intermediate maps we create here.
9506 Handle<Map> map = split_map;
9507 for (int i = split_nof; i < nof_descriptors - 1; ++i) {
9508 Handle<Map> new_map = CopyDropDescriptors(map);
9509 InstallDescriptors(map, new_map, i, descriptors, full_layout_descriptor);
9510 map = new_map;
9511 }
9512 map->NotifyLeafMapLayoutChange();
9513 InstallDescriptors(map, last_map, nof_descriptors - 1, descriptors,
9514 full_layout_descriptor);
9515 return last_map;
9516}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009517
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009518
9519// Since this method is used to rewrite an existing transition tree, it can
9520// always insert transitions without checking.
9521void Map::InstallDescriptors(Handle<Map> parent, Handle<Map> child,
9522 int new_descriptor,
9523 Handle<DescriptorArray> descriptors,
9524 Handle<LayoutDescriptor> full_layout_descriptor) {
9525 DCHECK(descriptors->IsSortedNoDuplicates());
9526
9527 child->set_instance_descriptors(*descriptors);
9528 child->SetNumberOfOwnDescriptors(new_descriptor + 1);
9529
9530 int unused_property_fields = parent->unused_property_fields();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009531 PropertyDetails details = descriptors->GetDetails(new_descriptor);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009532 if (details.location() == kField) {
9533 unused_property_fields = parent->unused_property_fields() - 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009534 if (unused_property_fields < 0) {
9535 unused_property_fields += JSObject::kFieldsAdded;
9536 }
9537 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009538 child->set_unused_property_fields(unused_property_fields);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009539
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009540 if (FLAG_unbox_double_fields) {
9541 Handle<LayoutDescriptor> layout_descriptor =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009542 LayoutDescriptor::AppendIfFastOrUseFull(parent, details,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009543 full_layout_descriptor);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009544 child->set_layout_descriptor(*layout_descriptor);
9545#ifdef VERIFY_HEAP
9546 // TODO(ishell): remove these checks from VERIFY_HEAP mode.
9547 if (FLAG_verify_heap) {
9548 CHECK(child->layout_descriptor()->IsConsistentWithMap(*child));
9549 }
9550#else
9551 SLOW_DCHECK(child->layout_descriptor()->IsConsistentWithMap(*child));
9552#endif
9553 child->set_visitor_id(Heap::GetStaticVisitorIdForMap(*child));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009554 }
9555
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009556 Handle<Name> name = handle(descriptors->GetKey(new_descriptor));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009557 ConnectTransition(parent, child, name, SIMPLE_PROPERTY_TRANSITION);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009558}
9559
9560
9561Handle<Map> Map::CopyAsElementsKind(Handle<Map> map, ElementsKind kind,
9562 TransitionFlag flag) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009563 Map* maybe_elements_transition_map = NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009564 if (flag == INSERT_TRANSITION) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009565 maybe_elements_transition_map = map->ElementsTransitionMap();
9566 DCHECK(maybe_elements_transition_map == NULL ||
9567 (maybe_elements_transition_map->elements_kind() ==
9568 DICTIONARY_ELEMENTS &&
9569 kind == DICTIONARY_ELEMENTS));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009570 DCHECK(!IsFastElementsKind(kind) ||
9571 IsMoreGeneralElementsKindTransition(map->elements_kind(), kind));
9572 DCHECK(kind != map->elements_kind());
9573 }
9574
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009575 bool insert_transition = flag == INSERT_TRANSITION &&
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009576 TransitionArray::CanHaveMoreTransitions(map) &&
9577 maybe_elements_transition_map == NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009578
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009579 if (insert_transition) {
9580 Handle<Map> new_map = CopyForTransition(map, "CopyAsElementsKind");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009581 new_map->set_elements_kind(kind);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009582
9583 Isolate* isolate = map->GetIsolate();
9584 Handle<Name> name = isolate->factory()->elements_transition_symbol();
9585 ConnectTransition(map, new_map, name, SPECIAL_TRANSITION);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009586 return new_map;
9587 }
9588
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009589 // Create a new free-floating map only if we are not allowed to store it.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009590 Handle<Map> new_map = Copy(map, "CopyAsElementsKind");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009591 new_map->set_elements_kind(kind);
Steve Block8defd9f2010-07-08 12:39:36 +01009592 return new_map;
Steve Blocka7e24c12009-10-30 11:49:00 +00009593}
9594
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009595
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009596Handle<Map> Map::AsLanguageMode(Handle<Map> initial_map,
9597 LanguageMode language_mode, FunctionKind kind) {
9598 DCHECK_EQ(JS_FUNCTION_TYPE, initial_map->instance_type());
9599 // Initial map for sloppy mode function is stored in the function
Ben Murdochda12d292016-06-02 14:46:10 +01009600 // constructor. Initial maps for strict mode are cached as special transitions
9601 // using |strict_function_transition_symbol| as a key.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009602 if (language_mode == SLOPPY) return initial_map;
9603 Isolate* isolate = initial_map->GetIsolate();
9604 Factory* factory = isolate->factory();
9605 Handle<Symbol> transition_symbol;
9606
9607 int map_index = Context::FunctionMapIndex(language_mode, kind);
9608 Handle<Map> function_map(
9609 Map::cast(isolate->native_context()->get(map_index)));
9610
9611 STATIC_ASSERT(LANGUAGE_END == 3);
9612 switch (language_mode) {
9613 case STRICT:
9614 transition_symbol = factory->strict_function_transition_symbol();
9615 break;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009616 default:
9617 UNREACHABLE();
9618 break;
9619 }
9620 Map* maybe_transition =
9621 TransitionArray::SearchSpecial(*initial_map, *transition_symbol);
9622 if (maybe_transition != NULL) {
9623 return handle(maybe_transition, isolate);
9624 }
9625 initial_map->NotifyLeafMapLayoutChange();
9626
9627 // Create new map taking descriptors from the |function_map| and all
9628 // the other details from the |initial_map|.
9629 Handle<Map> map =
9630 Map::CopyInitialMap(function_map, initial_map->instance_size(),
9631 initial_map->GetInObjectProperties(),
9632 initial_map->unused_property_fields());
9633 map->SetConstructor(initial_map->GetConstructor());
9634 map->set_prototype(initial_map->prototype());
9635
9636 if (TransitionArray::CanHaveMoreTransitions(initial_map)) {
9637 Map::ConnectTransition(initial_map, map, transition_symbol,
9638 SPECIAL_TRANSITION);
9639 }
9640 return map;
9641}
9642
9643
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009644Handle<Map> Map::CopyForObserved(Handle<Map> map) {
9645 DCHECK(!map->is_observed());
9646
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009647 Isolate* isolate = map->GetIsolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009648
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009649 bool insert_transition =
9650 TransitionArray::CanHaveMoreTransitions(map) && !map->is_prototype_map();
9651
9652 if (insert_transition) {
9653 Handle<Map> new_map = CopyForTransition(map, "CopyForObserved");
9654 new_map->set_is_observed();
9655
9656 Handle<Name> name = isolate->factory()->observed_symbol();
9657 ConnectTransition(map, new_map, name, SPECIAL_TRANSITION);
9658 return new_map;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009659 }
9660
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009661 // Create a new free-floating map only if we are not allowed to store it.
9662 Handle<Map> new_map = Map::Copy(map, "CopyForObserved");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009663 new_map->set_is_observed();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009664 return new_map;
9665}
9666
9667
9668Handle<Map> Map::CopyForTransition(Handle<Map> map, const char* reason) {
9669 DCHECK(!map->is_prototype_map());
9670 Handle<Map> new_map = CopyDropDescriptors(map);
9671
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009672 if (map->owns_descriptors()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009673 // In case the map owned its own descriptors, share the descriptors and
9674 // transfer ownership to the new map.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009675 // The properties did not change, so reuse descriptors.
9676 new_map->InitializeDescriptors(map->instance_descriptors(),
9677 map->GetLayoutDescriptor());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009678 } else {
9679 // In case the map did not own its own descriptors, a split is forced by
9680 // copying the map; creating a new descriptor array cell.
9681 Handle<DescriptorArray> descriptors(map->instance_descriptors());
9682 int number_of_own_descriptors = map->NumberOfOwnDescriptors();
9683 Handle<DescriptorArray> new_descriptors =
9684 DescriptorArray::CopyUpTo(descriptors, number_of_own_descriptors);
9685 Handle<LayoutDescriptor> new_layout_descriptor(map->GetLayoutDescriptor(),
9686 map->GetIsolate());
9687 new_map->InitializeDescriptors(*new_descriptors, *new_layout_descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009688 }
9689
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009690#if TRACE_MAPS
9691 if (FLAG_trace_maps) {
9692 PrintF("[TraceMaps: CopyForTransition from= %p to= %p reason= %s ]\n",
9693 reinterpret_cast<void*>(*map), reinterpret_cast<void*>(*new_map),
9694 reason);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009695 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009696#endif
9697
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009698 return new_map;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009699}
Steve Blocka7e24c12009-10-30 11:49:00 +00009700
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009701
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009702Handle<Map> Map::Copy(Handle<Map> map, const char* reason) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009703 Handle<DescriptorArray> descriptors(map->instance_descriptors());
9704 int number_of_own_descriptors = map->NumberOfOwnDescriptors();
9705 Handle<DescriptorArray> new_descriptors =
9706 DescriptorArray::CopyUpTo(descriptors, number_of_own_descriptors);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009707 Handle<LayoutDescriptor> new_layout_descriptor(map->GetLayoutDescriptor(),
9708 map->GetIsolate());
9709 return CopyReplaceDescriptors(map, new_descriptors, new_layout_descriptor,
9710 OMIT_TRANSITION, MaybeHandle<Name>(), reason,
9711 SPECIAL_TRANSITION);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009712}
9713
9714
9715Handle<Map> Map::Create(Isolate* isolate, int inobject_properties) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009716 Handle<Map> copy =
9717 Copy(handle(isolate->object_function()->initial_map()), "MapCreate");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009718
9719 // Check that we do not overflow the instance size when adding the extra
9720 // inobject properties. If the instance size overflows, we allocate as many
9721 // properties as we can as inobject properties.
9722 int max_extra_properties =
9723 (JSObject::kMaxInstanceSize - JSObject::kHeaderSize) >> kPointerSizeLog2;
9724
9725 if (inobject_properties > max_extra_properties) {
9726 inobject_properties = max_extra_properties;
9727 }
9728
9729 int new_instance_size =
9730 JSObject::kHeaderSize + kPointerSize * inobject_properties;
9731
9732 // Adjust the map with the extra inobject properties.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009733 copy->SetInObjectProperties(inobject_properties);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009734 copy->set_unused_property_fields(inobject_properties);
9735 copy->set_instance_size(new_instance_size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009736 copy->set_visitor_id(Heap::GetStaticVisitorIdForMap(*copy));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009737 return copy;
9738}
9739
9740
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009741Handle<Map> Map::CopyForPreventExtensions(Handle<Map> map,
9742 PropertyAttributes attrs_to_add,
9743 Handle<Symbol> transition_marker,
9744 const char* reason) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009745 int num_descriptors = map->NumberOfOwnDescriptors();
9746 Isolate* isolate = map->GetIsolate();
9747 Handle<DescriptorArray> new_desc = DescriptorArray::CopyUpToAddAttributes(
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009748 handle(map->instance_descriptors(), isolate), num_descriptors,
9749 attrs_to_add);
9750 Handle<LayoutDescriptor> new_layout_descriptor(map->GetLayoutDescriptor(),
9751 isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009752 Handle<Map> new_map = CopyReplaceDescriptors(
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009753 map, new_desc, new_layout_descriptor, INSERT_TRANSITION,
9754 transition_marker, reason, SPECIAL_TRANSITION);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009755 new_map->set_is_extensible(false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009756 if (!IsFixedTypedArrayElementsKind(map->elements_kind())) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01009757 ElementsKind new_kind = IsStringWrapperElementsKind(map->elements_kind())
9758 ? SLOW_STRING_WRAPPER_ELEMENTS
9759 : DICTIONARY_ELEMENTS;
9760 new_map->set_elements_kind(new_kind);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009761 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009762 return new_map;
9763}
9764
Ben Murdoch097c5b22016-05-18 11:27:45 +01009765FieldType* DescriptorArray::GetFieldType(int descriptor_number) {
9766 DCHECK(GetDetails(descriptor_number).location() == kField);
9767 Object* value = GetValue(descriptor_number);
9768 if (value->IsWeakCell()) {
9769 if (WeakCell::cast(value)->cleared()) return FieldType::None();
9770 value = WeakCell::cast(value)->value();
9771 }
9772 return FieldType::cast(value);
9773}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009774
Ben Murdoch097c5b22016-05-18 11:27:45 +01009775namespace {
9776
9777bool CanHoldValue(DescriptorArray* descriptors, int descriptor, Object* value) {
9778 PropertyDetails details = descriptors->GetDetails(descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009779 switch (details.type()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009780 case DATA:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009781 return value->FitsRepresentation(details.representation()) &&
Ben Murdoch097c5b22016-05-18 11:27:45 +01009782 descriptors->GetFieldType(descriptor)->NowContains(value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009783
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009784 case DATA_CONSTANT:
Ben Murdoch097c5b22016-05-18 11:27:45 +01009785 DCHECK(descriptors->GetConstant(descriptor) != value ||
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009786 value->FitsRepresentation(details.representation()));
Ben Murdoch097c5b22016-05-18 11:27:45 +01009787 return descriptors->GetConstant(descriptor) == value;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009788
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009789 case ACCESSOR:
9790 case ACCESSOR_CONSTANT:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009791 return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009792 }
9793
9794 UNREACHABLE();
9795 return false;
9796}
9797
Ben Murdoch097c5b22016-05-18 11:27:45 +01009798Handle<Map> UpdateDescriptorForValue(Handle<Map> map, int descriptor,
9799 Handle<Object> value) {
9800 if (CanHoldValue(map->instance_descriptors(), descriptor, *value)) return map;
9801
9802 Isolate* isolate = map->GetIsolate();
9803 PropertyAttributes attributes =
9804 map->instance_descriptors()->GetDetails(descriptor).attributes();
9805 Representation representation = value->OptimalRepresentation();
9806 Handle<FieldType> type = value->OptimalType(isolate, representation);
9807
9808 return Map::ReconfigureProperty(map, descriptor, kData, attributes,
9809 representation, type, FORCE_FIELD);
9810}
9811
9812} // namespace
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009813
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009814// static
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009815Handle<Map> Map::PrepareForDataProperty(Handle<Map> map, int descriptor,
9816 Handle<Object> value) {
9817 // Dictionaries can store any property value.
Ben Murdoch097c5b22016-05-18 11:27:45 +01009818 DCHECK(!map->is_dictionary_map());
9819 // Update to the newest map before storing the property.
9820 return UpdateDescriptorForValue(Update(map), descriptor, value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009821}
9822
9823
9824Handle<Map> Map::TransitionToDataProperty(Handle<Map> map, Handle<Name> name,
9825 Handle<Object> value,
9826 PropertyAttributes attributes,
9827 StoreFromKeyed store_mode) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01009828 DCHECK(name->IsUniqueName());
9829 DCHECK(!map->is_dictionary_map());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009830
9831 // Migrate to the newest map before storing the property.
9832 map = Update(map);
9833
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009834 Map* maybe_transition =
9835 TransitionArray::SearchTransition(*map, kData, *name, attributes);
9836 if (maybe_transition != NULL) {
9837 Handle<Map> transition(maybe_transition);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009838 int descriptor = transition->LastAdded();
9839
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009840 DCHECK_EQ(attributes, transition->instance_descriptors()
9841 ->GetDetails(descriptor)
9842 .attributes());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009843
Ben Murdoch097c5b22016-05-18 11:27:45 +01009844 return UpdateDescriptorForValue(transition, descriptor, value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009845 }
9846
9847 TransitionFlag flag = INSERT_TRANSITION;
9848 MaybeHandle<Map> maybe_map;
9849 if (value->IsJSFunction()) {
9850 maybe_map = Map::CopyWithConstant(map, name, value, attributes, flag);
9851 } else if (!map->TooManyFastProperties(store_mode)) {
9852 Isolate* isolate = name->GetIsolate();
9853 Representation representation = value->OptimalRepresentation();
Ben Murdoch097c5b22016-05-18 11:27:45 +01009854 Handle<FieldType> type = value->OptimalType(isolate, representation);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009855 maybe_map =
9856 Map::CopyWithField(map, name, type, attributes, representation, flag);
9857 }
9858
9859 Handle<Map> result;
9860 if (!maybe_map.ToHandle(&result)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009861#if TRACE_MAPS
9862 if (FLAG_trace_maps) {
9863 Vector<char> name_buffer = Vector<char>::New(100);
9864 name->NameShortPrint(name_buffer);
9865 Vector<char> buffer = Vector<char>::New(128);
9866 SNPrintF(buffer, "TooManyFastProperties %s", name_buffer.start());
9867 return Map::Normalize(map, CLEAR_INOBJECT_PROPERTIES, buffer.start());
9868 }
9869#endif
9870 return Map::Normalize(map, CLEAR_INOBJECT_PROPERTIES,
9871 "TooManyFastProperties");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009872 }
9873
9874 return result;
9875}
9876
9877
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009878Handle<Map> Map::ReconfigureExistingProperty(Handle<Map> map, int descriptor,
9879 PropertyKind kind,
9880 PropertyAttributes attributes) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009881 // Dictionaries have to be reconfigured in-place.
9882 DCHECK(!map->is_dictionary_map());
9883
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009884 if (!map->GetBackPointer()->IsMap()) {
9885 // There is no benefit from reconstructing transition tree for maps without
9886 // back pointers.
9887 return CopyGeneralizeAllRepresentations(
9888 map, descriptor, FORCE_FIELD, kind, attributes,
9889 "GenAll_AttributesMismatchProtoMap");
9890 }
9891
9892 if (FLAG_trace_generalization) {
9893 map->PrintReconfiguration(stdout, descriptor, kind, attributes);
9894 }
9895
9896 Isolate* isolate = map->GetIsolate();
9897 Handle<Map> new_map = ReconfigureProperty(
9898 map, descriptor, kind, attributes, Representation::None(),
Ben Murdoch097c5b22016-05-18 11:27:45 +01009899 FieldType::None(isolate), FORCE_FIELD);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009900 return new_map;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009901}
9902
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009903Handle<Map> Map::TransitionToAccessorProperty(Handle<Map> map,
Ben Murdochda12d292016-06-02 14:46:10 +01009904 Handle<Name> name, int descriptor,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009905 AccessorComponent component,
9906 Handle<Object> accessor,
9907 PropertyAttributes attributes) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01009908 DCHECK(name->IsUniqueName());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009909 Isolate* isolate = name->GetIsolate();
9910
9911 // Dictionary maps can always have additional data properties.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009912 if (map->is_dictionary_map()) return map;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009913
9914 // Migrate to the newest map before transitioning to the new property.
9915 map = Update(map);
9916
9917 PropertyNormalizationMode mode = map->is_prototype_map()
9918 ? KEEP_INOBJECT_PROPERTIES
9919 : CLEAR_INOBJECT_PROPERTIES;
9920
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009921 Map* maybe_transition =
9922 TransitionArray::SearchTransition(*map, kAccessor, *name, attributes);
9923 if (maybe_transition != NULL) {
9924 Handle<Map> transition(maybe_transition, isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009925 DescriptorArray* descriptors = transition->instance_descriptors();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009926 int descriptor = transition->LastAdded();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009927 DCHECK(descriptors->GetKey(descriptor)->Equals(*name));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009928
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009929 DCHECK_EQ(kAccessor, descriptors->GetDetails(descriptor).kind());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009930 DCHECK_EQ(attributes, descriptors->GetDetails(descriptor).attributes());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009931
9932 Handle<Object> maybe_pair(descriptors->GetValue(descriptor), isolate);
9933 if (!maybe_pair->IsAccessorPair()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009934 return Map::Normalize(map, mode, "TransitionToAccessorFromNonPair");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009935 }
9936
9937 Handle<AccessorPair> pair = Handle<AccessorPair>::cast(maybe_pair);
9938 if (pair->get(component) != *accessor) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009939 return Map::Normalize(map, mode, "TransitionToDifferentAccessor");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009940 }
9941
9942 return transition;
9943 }
9944
9945 Handle<AccessorPair> pair;
9946 DescriptorArray* old_descriptors = map->instance_descriptors();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009947 if (descriptor != DescriptorArray::kNotFound) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009948 if (descriptor != map->LastAdded()) {
9949 return Map::Normalize(map, mode, "AccessorsOverwritingNonLast");
9950 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009951 PropertyDetails old_details = old_descriptors->GetDetails(descriptor);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009952 if (old_details.type() != ACCESSOR_CONSTANT) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009953 return Map::Normalize(map, mode, "AccessorsOverwritingNonAccessors");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009954 }
9955
9956 if (old_details.attributes() != attributes) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009957 return Map::Normalize(map, mode, "AccessorsWithAttributes");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009958 }
9959
9960 Handle<Object> maybe_pair(old_descriptors->GetValue(descriptor), isolate);
9961 if (!maybe_pair->IsAccessorPair()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009962 return Map::Normalize(map, mode, "AccessorsOverwritingNonPair");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009963 }
9964
9965 Object* current = Handle<AccessorPair>::cast(maybe_pair)->get(component);
9966 if (current == *accessor) return map;
9967
9968 if (!current->IsTheHole()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009969 return Map::Normalize(map, mode, "AccessorsOverwritingAccessors");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009970 }
9971
9972 pair = AccessorPair::Copy(Handle<AccessorPair>::cast(maybe_pair));
9973 } else if (map->NumberOfOwnDescriptors() >= kMaxNumberOfDescriptors ||
9974 map->TooManyFastProperties(CERTAINLY_NOT_STORE_FROM_KEYED)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009975 return Map::Normalize(map, CLEAR_INOBJECT_PROPERTIES, "TooManyAccessors");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009976 } else {
9977 pair = isolate->factory()->NewAccessorPair();
9978 }
9979
9980 pair->set(component, *accessor);
9981 TransitionFlag flag = INSERT_TRANSITION;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009982 AccessorConstantDescriptor new_desc(name, pair, attributes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009983 return Map::CopyInsertDescriptor(map, &new_desc, flag);
9984}
9985
9986
9987Handle<Map> Map::CopyAddDescriptor(Handle<Map> map,
9988 Descriptor* descriptor,
9989 TransitionFlag flag) {
9990 Handle<DescriptorArray> descriptors(map->instance_descriptors());
9991
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009992 // Share descriptors only if map owns descriptors and it not an initial map.
9993 if (flag == INSERT_TRANSITION && map->owns_descriptors() &&
9994 !map->GetBackPointer()->IsUndefined() &&
9995 TransitionArray::CanHaveMoreTransitions(map)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009996 return ShareDescriptor(map, descriptors, descriptor);
9997 }
9998
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009999 int nof = map->NumberOfOwnDescriptors();
10000 Handle<DescriptorArray> new_descriptors =
10001 DescriptorArray::CopyUpTo(descriptors, nof, 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010002 new_descriptors->Append(descriptor);
10003
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010004 Handle<LayoutDescriptor> new_layout_descriptor =
10005 FLAG_unbox_double_fields
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010006 ? LayoutDescriptor::New(map, new_descriptors, nof + 1)
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010007 : handle(LayoutDescriptor::FastPointerLayout(), map->GetIsolate());
10008
10009 return CopyReplaceDescriptors(map, new_descriptors, new_layout_descriptor,
10010 flag, descriptor->GetKey(), "CopyAddDescriptor",
10011 SIMPLE_PROPERTY_TRANSITION);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010012}
10013
10014
10015Handle<Map> Map::CopyInsertDescriptor(Handle<Map> map,
10016 Descriptor* descriptor,
10017 TransitionFlag flag) {
10018 Handle<DescriptorArray> old_descriptors(map->instance_descriptors());
10019
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010020 // We replace the key if it is already present.
Ben Murdoch097c5b22016-05-18 11:27:45 +010010021 int index = old_descriptors->SearchWithCache(map->GetIsolate(),
10022 *descriptor->GetKey(), *map);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010023 if (index != DescriptorArray::kNotFound) {
10024 return CopyReplaceDescriptor(map, old_descriptors, descriptor, index, flag);
10025 }
10026 return CopyAddDescriptor(map, descriptor, flag);
10027}
10028
10029
10030Handle<DescriptorArray> DescriptorArray::CopyUpTo(
10031 Handle<DescriptorArray> desc,
10032 int enumeration_index,
10033 int slack) {
10034 return DescriptorArray::CopyUpToAddAttributes(
10035 desc, enumeration_index, NONE, slack);
10036}
10037
10038
10039Handle<DescriptorArray> DescriptorArray::CopyUpToAddAttributes(
10040 Handle<DescriptorArray> desc,
10041 int enumeration_index,
10042 PropertyAttributes attributes,
10043 int slack) {
10044 if (enumeration_index + slack == 0) {
10045 return desc->GetIsolate()->factory()->empty_descriptor_array();
10046 }
10047
10048 int size = enumeration_index;
10049
10050 Handle<DescriptorArray> descriptors =
10051 DescriptorArray::Allocate(desc->GetIsolate(), size, slack);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010052
10053 if (attributes != NONE) {
10054 for (int i = 0; i < size; ++i) {
10055 Object* value = desc->GetValue(i);
10056 Name* key = desc->GetKey(i);
10057 PropertyDetails details = desc->GetDetails(i);
10058 // Bulk attribute changes never affect private properties.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010059 if (!key->IsPrivate()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010060 int mask = DONT_DELETE | DONT_ENUM;
10061 // READ_ONLY is an invalid attribute for JS setters/getters.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010062 if (details.type() != ACCESSOR_CONSTANT || !value->IsAccessorPair()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010063 mask |= READ_ONLY;
10064 }
10065 details = details.CopyAddAttributes(
10066 static_cast<PropertyAttributes>(attributes & mask));
10067 }
10068 Descriptor inner_desc(
10069 handle(key), handle(value, desc->GetIsolate()), details);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010070 descriptors->SetDescriptor(i, &inner_desc);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010071 }
10072 } else {
10073 for (int i = 0; i < size; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010074 descriptors->CopyFrom(i, *desc);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010075 }
10076 }
10077
10078 if (desc->number_of_descriptors() != enumeration_index) descriptors->Sort();
10079
10080 return descriptors;
10081}
10082
10083
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010084bool DescriptorArray::IsEqualUpTo(DescriptorArray* desc, int nof_descriptors) {
10085 for (int i = 0; i < nof_descriptors; i++) {
10086 if (GetKey(i) != desc->GetKey(i) || GetValue(i) != desc->GetValue(i)) {
10087 return false;
10088 }
10089 PropertyDetails details = GetDetails(i);
10090 PropertyDetails other_details = desc->GetDetails(i);
10091 if (details.type() != other_details.type() ||
10092 !details.representation().Equals(other_details.representation())) {
10093 return false;
10094 }
10095 }
10096 return true;
10097}
10098
10099
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010100Handle<Map> Map::CopyReplaceDescriptor(Handle<Map> map,
10101 Handle<DescriptorArray> descriptors,
10102 Descriptor* descriptor,
10103 int insertion_index,
10104 TransitionFlag flag) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010105 Handle<Name> key = descriptor->GetKey();
10106 DCHECK(*key == descriptors->GetKey(insertion_index));
10107
10108 Handle<DescriptorArray> new_descriptors = DescriptorArray::CopyUpTo(
10109 descriptors, map->NumberOfOwnDescriptors());
10110
10111 new_descriptors->Replace(insertion_index, descriptor);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010112 Handle<LayoutDescriptor> new_layout_descriptor = LayoutDescriptor::New(
10113 map, new_descriptors, new_descriptors->number_of_descriptors());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010114
10115 SimpleTransitionFlag simple_flag =
10116 (insertion_index == descriptors->number_of_descriptors() - 1)
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010117 ? SIMPLE_PROPERTY_TRANSITION
10118 : PROPERTY_TRANSITION;
10119 return CopyReplaceDescriptors(map, new_descriptors, new_layout_descriptor,
10120 flag, key, "CopyReplaceDescriptor",
10121 simple_flag);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010122}
10123
10124
10125void Map::UpdateCodeCache(Handle<Map> map,
10126 Handle<Name> name,
10127 Handle<Code> code) {
10128 Isolate* isolate = map->GetIsolate();
10129 HandleScope scope(isolate);
10130 // Allocate the code cache if not present.
10131 if (map->code_cache()->IsFixedArray()) {
10132 Handle<Object> result = isolate->factory()->NewCodeCache();
10133 map->set_code_cache(*result);
Steve Block6ded16b2010-05-10 14:33:55 +010010134 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010135
Steve Block6ded16b2010-05-10 14:33:55 +010010136 // Update the code cache.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010137 Handle<CodeCache> code_cache(CodeCache::cast(map->code_cache()), isolate);
10138 CodeCache::Update(code_cache, name, code);
Steve Block6ded16b2010-05-10 14:33:55 +010010139}
10140
10141
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010142Object* Map::FindInCodeCache(Name* name, Code::Flags flags) {
Steve Block6ded16b2010-05-10 14:33:55 +010010143 // Do a lookup if a code cache exists.
10144 if (!code_cache()->IsFixedArray()) {
10145 return CodeCache::cast(code_cache())->Lookup(name, flags);
10146 } else {
Steve Block44f0eee2011-05-26 01:26:41 +010010147 return GetHeap()->undefined_value();
Steve Block6ded16b2010-05-10 14:33:55 +010010148 }
10149}
10150
10151
10152int Map::IndexInCodeCache(Object* name, Code* code) {
10153 // Get the internal index if a code cache exists.
10154 if (!code_cache()->IsFixedArray()) {
10155 return CodeCache::cast(code_cache())->GetIndex(name, code);
10156 }
10157 return -1;
10158}
10159
10160
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010161void Map::RemoveFromCodeCache(Name* name, Code* code, int index) {
Steve Block6ded16b2010-05-10 14:33:55 +010010162 // No GC is supposed to happen between a call to IndexInCodeCache and
10163 // RemoveFromCodeCache so the code cache must be there.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010164 DCHECK(!code_cache()->IsFixedArray());
Steve Block6ded16b2010-05-10 14:33:55 +010010165 CodeCache::cast(code_cache())->RemoveByIndex(name, code, index);
10166}
10167
10168
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010169void CodeCache::Update(
10170 Handle<CodeCache> code_cache, Handle<Name> name, Handle<Code> code) {
Steve Block6ded16b2010-05-10 14:33:55 +010010171 // The number of monomorphic stubs for normal load/store/call IC's can grow to
10172 // a large number and therefore they need to go into a hash table. They are
10173 // used to load global properties from cells.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010174 if (code->type() == Code::NORMAL) {
Steve Block6ded16b2010-05-10 14:33:55 +010010175 // Make sure that a hash table is allocated for the normal load code cache.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010176 if (code_cache->normal_type_cache()->IsUndefined()) {
10177 Handle<Object> result =
10178 CodeCacheHashTable::New(code_cache->GetIsolate(),
10179 CodeCacheHashTable::kInitialSize);
10180 code_cache->set_normal_type_cache(*result);
Steve Block6ded16b2010-05-10 14:33:55 +010010181 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010182 UpdateNormalTypeCache(code_cache, name, code);
Steve Block6ded16b2010-05-10 14:33:55 +010010183 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010184 DCHECK(code_cache->default_cache()->IsFixedArray());
10185 UpdateDefaultCache(code_cache, name, code);
Steve Block6ded16b2010-05-10 14:33:55 +010010186 }
10187}
10188
10189
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010190void CodeCache::UpdateDefaultCache(
10191 Handle<CodeCache> code_cache, Handle<Name> name, Handle<Code> code) {
Ben Murdochda12d292016-06-02 14:46:10 +010010192 Isolate* isolate = code_cache->GetIsolate();
Steve Block6ded16b2010-05-10 14:33:55 +010010193 // When updating the default code cache we disregard the type encoded in the
Steve Blocka7e24c12009-10-30 11:49:00 +000010194 // flags. This allows call constant stubs to overwrite call field
10195 // stubs, etc.
10196 Code::Flags flags = Code::RemoveTypeFromFlags(code->flags());
10197
10198 // First check whether we can update existing code cache without
10199 // extending it.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010200 Handle<FixedArray> cache = handle(code_cache->default_cache());
Steve Blocka7e24c12009-10-30 11:49:00 +000010201 int length = cache->length();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010202 {
10203 DisallowHeapAllocation no_alloc;
10204 int deleted_index = -1;
Ben Murdochda12d292016-06-02 14:46:10 +010010205 Object* null = isolate->heap()->null_value();
10206 Object* undefined = isolate->heap()->undefined_value();
10207 DCHECK(name->IsUniqueName());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010208 for (int i = 0; i < length; i += kCodeCacheEntrySize) {
10209 Object* key = cache->get(i);
Ben Murdochda12d292016-06-02 14:46:10 +010010210 if (key == null) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010211 if (deleted_index < 0) deleted_index = i;
10212 continue;
10213 }
Ben Murdochda12d292016-06-02 14:46:10 +010010214 if (key == undefined) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010215 if (deleted_index >= 0) i = deleted_index;
10216 cache->set(i + kCodeCacheEntryNameOffset, *name);
10217 cache->set(i + kCodeCacheEntryCodeOffset, *code);
10218 return;
10219 }
Ben Murdochda12d292016-06-02 14:46:10 +010010220 DCHECK(key->IsUniqueName());
10221 if (*name == key) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010222 Code::Flags found =
10223 Code::cast(cache->get(i + kCodeCacheEntryCodeOffset))->flags();
10224 if (Code::RemoveTypeFromFlags(found) == flags) {
10225 cache->set(i + kCodeCacheEntryCodeOffset, *code);
10226 return;
10227 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010228 }
10229 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010230
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010231 // Reached the end of the code cache. If there were deleted
10232 // elements, reuse the space for the first of them.
10233 if (deleted_index >= 0) {
10234 cache->set(deleted_index + kCodeCacheEntryNameOffset, *name);
10235 cache->set(deleted_index + kCodeCacheEntryCodeOffset, *code);
10236 return;
10237 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010238 }
10239
Steve Block6ded16b2010-05-10 14:33:55 +010010240 // Extend the code cache with some new entries (at least one). Must be a
10241 // multiple of the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010242 int new_length = length + (length >> 1) + kCodeCacheEntrySize;
Steve Block6ded16b2010-05-10 14:33:55 +010010243 new_length = new_length - new_length % kCodeCacheEntrySize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010244 DCHECK((new_length % kCodeCacheEntrySize) == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010245 cache = isolate->factory()->CopyFixedArrayAndGrow(cache, new_length - length);
Steve Blocka7e24c12009-10-30 11:49:00 +000010246
10247 // Add the (name, code) pair to the new cache.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010248 cache->set(length + kCodeCacheEntryNameOffset, *name);
10249 cache->set(length + kCodeCacheEntryCodeOffset, *code);
10250 code_cache->set_default_cache(*cache);
Steve Blocka7e24c12009-10-30 11:49:00 +000010251}
10252
10253
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010254void CodeCache::UpdateNormalTypeCache(
10255 Handle<CodeCache> code_cache, Handle<Name> name, Handle<Code> code) {
Steve Block6ded16b2010-05-10 14:33:55 +010010256 // Adding a new entry can cause a new cache to be allocated.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010257 Handle<CodeCacheHashTable> cache(
10258 CodeCacheHashTable::cast(code_cache->normal_type_cache()));
10259 Handle<Object> new_cache = CodeCacheHashTable::Put(cache, name, code);
10260 code_cache->set_normal_type_cache(*new_cache);
Steve Block6ded16b2010-05-10 14:33:55 +010010261}
10262
10263
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010264Object* CodeCache::Lookup(Name* name, Code::Flags flags) {
10265 Object* result = LookupDefaultCache(name, Code::RemoveTypeFromFlags(flags));
10266 if (result->IsCode()) {
10267 if (Code::cast(result)->flags() == flags) return result;
10268 return GetHeap()->undefined_value();
Steve Block6ded16b2010-05-10 14:33:55 +010010269 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010270 return LookupNormalTypeCache(name, flags);
Steve Block6ded16b2010-05-10 14:33:55 +010010271}
10272
10273
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010274Object* CodeCache::LookupDefaultCache(Name* name, Code::Flags flags) {
Steve Block6ded16b2010-05-10 14:33:55 +010010275 FixedArray* cache = default_cache();
Ben Murdochda12d292016-06-02 14:46:10 +010010276 Heap* heap = GetHeap();
10277 Object* null = heap->null_value();
10278 Object* undefined = heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000010279 int length = cache->length();
Ben Murdochda12d292016-06-02 14:46:10 +010010280 DCHECK(name->IsUniqueName());
Steve Block6ded16b2010-05-10 14:33:55 +010010281 for (int i = 0; i < length; i += kCodeCacheEntrySize) {
10282 Object* key = cache->get(i + kCodeCacheEntryNameOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +000010283 // Skip deleted elements.
Ben Murdochda12d292016-06-02 14:46:10 +010010284 if (key == null) continue;
10285 if (key == undefined) return key;
10286 DCHECK(key->IsUniqueName());
10287 if (name == key) {
Steve Block6ded16b2010-05-10 14:33:55 +010010288 Code* code = Code::cast(cache->get(i + kCodeCacheEntryCodeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010289 if (Code::RemoveTypeFromFlags(code->flags()) == flags) {
Steve Block6ded16b2010-05-10 14:33:55 +010010290 return code;
10291 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010292 }
10293 }
Ben Murdoch8b112d22011-06-08 16:22:53 +010010294 return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000010295}
10296
10297
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010298Object* CodeCache::LookupNormalTypeCache(Name* name, Code::Flags flags) {
Steve Block6ded16b2010-05-10 14:33:55 +010010299 if (!normal_type_cache()->IsUndefined()) {
10300 CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache());
10301 return cache->Lookup(name, flags);
10302 } else {
Steve Block44f0eee2011-05-26 01:26:41 +010010303 return GetHeap()->undefined_value();
Steve Block6ded16b2010-05-10 14:33:55 +010010304 }
10305}
10306
10307
10308int CodeCache::GetIndex(Object* name, Code* code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010309 if (code->type() == Code::NORMAL) {
Steve Block6ded16b2010-05-10 14:33:55 +010010310 if (normal_type_cache()->IsUndefined()) return -1;
10311 CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010312 return cache->GetIndex(Name::cast(name), code->flags());
Steve Block6ded16b2010-05-10 14:33:55 +010010313 }
10314
10315 FixedArray* array = default_cache();
Steve Blocka7e24c12009-10-30 11:49:00 +000010316 int len = array->length();
Steve Block6ded16b2010-05-10 14:33:55 +010010317 for (int i = 0; i < len; i += kCodeCacheEntrySize) {
10318 if (array->get(i + kCodeCacheEntryCodeOffset) == code) return i + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000010319 }
10320 return -1;
10321}
10322
10323
Steve Block6ded16b2010-05-10 14:33:55 +010010324void CodeCache::RemoveByIndex(Object* name, Code* code, int index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010325 if (code->type() == Code::NORMAL) {
10326 DCHECK(!normal_type_cache()->IsUndefined());
Steve Block6ded16b2010-05-10 14:33:55 +010010327 CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010328 DCHECK(cache->GetIndex(Name::cast(name), code->flags()) == index);
Steve Block6ded16b2010-05-10 14:33:55 +010010329 cache->RemoveByIndex(index);
10330 } else {
10331 FixedArray* array = default_cache();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010332 DCHECK(array->length() >= index && array->get(index)->IsCode());
Steve Block6ded16b2010-05-10 14:33:55 +010010333 // Use null instead of undefined for deleted elements to distinguish
10334 // deleted elements from unused elements. This distinction is used
10335 // when looking up in the cache and when updating the cache.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010336 DCHECK_EQ(1, kCodeCacheEntryCodeOffset - kCodeCacheEntryNameOffset);
Steve Block6ded16b2010-05-10 14:33:55 +010010337 array->set_null(index - 1); // Name.
10338 array->set_null(index); // Code.
10339 }
10340}
10341
10342
10343// The key in the code cache hash table consists of the property name and the
10344// code object. The actual match is on the name and the code flags. If a key
10345// is created using the flags and not a code object it can only be used for
10346// lookup not to create a new entry.
10347class CodeCacheHashTableKey : public HashTableKey {
10348 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010349 CodeCacheHashTableKey(Handle<Name> name, Code::Flags flags)
10350 : name_(name), flags_(flags), code_() { }
Steve Block6ded16b2010-05-10 14:33:55 +010010351
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010352 CodeCacheHashTableKey(Handle<Name> name, Handle<Code> code)
10353 : name_(name), flags_(code->flags()), code_(code) { }
Steve Block6ded16b2010-05-10 14:33:55 +010010354
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010355 bool IsMatch(Object* other) override {
Steve Block6ded16b2010-05-10 14:33:55 +010010356 if (!other->IsFixedArray()) return false;
10357 FixedArray* pair = FixedArray::cast(other);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010358 Name* name = Name::cast(pair->get(0));
Steve Block6ded16b2010-05-10 14:33:55 +010010359 Code::Flags flags = Code::cast(pair->get(1))->flags();
10360 if (flags != flags_) {
10361 return false;
10362 }
10363 return name_->Equals(name);
10364 }
10365
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010366 static uint32_t NameFlagsHashHelper(Name* name, Code::Flags flags) {
Steve Block6ded16b2010-05-10 14:33:55 +010010367 return name->Hash() ^ flags;
10368 }
10369
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010370 uint32_t Hash() override { return NameFlagsHashHelper(*name_, flags_); }
Steve Block6ded16b2010-05-10 14:33:55 +010010371
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010372 uint32_t HashForObject(Object* obj) override {
Steve Block6ded16b2010-05-10 14:33:55 +010010373 FixedArray* pair = FixedArray::cast(obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010374 Name* name = Name::cast(pair->get(0));
Steve Block6ded16b2010-05-10 14:33:55 +010010375 Code* code = Code::cast(pair->get(1));
10376 return NameFlagsHashHelper(name, code->flags());
10377 }
10378
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010379 MUST_USE_RESULT Handle<Object> AsHandle(Isolate* isolate) override {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010380 Handle<Code> code = code_.ToHandleChecked();
10381 Handle<FixedArray> pair = isolate->factory()->NewFixedArray(2);
10382 pair->set(0, *name_);
10383 pair->set(1, *code);
Steve Block6ded16b2010-05-10 14:33:55 +010010384 return pair;
10385 }
10386
10387 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010388 Handle<Name> name_;
Steve Block6ded16b2010-05-10 14:33:55 +010010389 Code::Flags flags_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010390 // TODO(jkummerow): We should be able to get by without this.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010391 MaybeHandle<Code> code_;
Steve Block6ded16b2010-05-10 14:33:55 +010010392};
10393
10394
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010395Object* CodeCacheHashTable::Lookup(Name* name, Code::Flags flags) {
10396 DisallowHeapAllocation no_alloc;
10397 CodeCacheHashTableKey key(handle(name), flags);
Steve Block6ded16b2010-05-10 14:33:55 +010010398 int entry = FindEntry(&key);
Steve Block44f0eee2011-05-26 01:26:41 +010010399 if (entry == kNotFound) return GetHeap()->undefined_value();
Steve Block6ded16b2010-05-10 14:33:55 +010010400 return get(EntryToIndex(entry) + 1);
10401}
10402
10403
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010404Handle<CodeCacheHashTable> CodeCacheHashTable::Put(
10405 Handle<CodeCacheHashTable> cache, Handle<Name> name, Handle<Code> code) {
Steve Block6ded16b2010-05-10 14:33:55 +010010406 CodeCacheHashTableKey key(name, code);
Steve Block6ded16b2010-05-10 14:33:55 +010010407
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010408 Handle<CodeCacheHashTable> new_cache = EnsureCapacity(cache, 1, &key);
Steve Block6ded16b2010-05-10 14:33:55 +010010409
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010410 int entry = new_cache->FindInsertionEntry(key.Hash());
10411 Handle<Object> k = key.AsHandle(cache->GetIsolate());
Steve Block6ded16b2010-05-10 14:33:55 +010010412
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010413 new_cache->set(EntryToIndex(entry), *k);
10414 new_cache->set(EntryToIndex(entry) + 1, *code);
10415 new_cache->ElementAdded();
10416 return new_cache;
Steve Block6ded16b2010-05-10 14:33:55 +010010417}
10418
10419
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010420int CodeCacheHashTable::GetIndex(Name* name, Code::Flags flags) {
10421 DisallowHeapAllocation no_alloc;
10422 CodeCacheHashTableKey key(handle(name), flags);
Steve Block6ded16b2010-05-10 14:33:55 +010010423 int entry = FindEntry(&key);
10424 return (entry == kNotFound) ? -1 : entry;
10425}
10426
10427
10428void CodeCacheHashTable::RemoveByIndex(int index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010429 DCHECK(index >= 0);
Steve Block44f0eee2011-05-26 01:26:41 +010010430 Heap* heap = GetHeap();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010431 set(EntryToIndex(index), heap->the_hole_value());
10432 set(EntryToIndex(index) + 1, heap->the_hole_value());
Steve Block6ded16b2010-05-10 14:33:55 +010010433 ElementRemoved();
Steve Blocka7e24c12009-10-30 11:49:00 +000010434}
10435
10436
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010437void PolymorphicCodeCache::Update(Handle<PolymorphicCodeCache> code_cache,
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010438 MapHandleList* maps,
10439 Code::Flags flags,
10440 Handle<Code> code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010441 Isolate* isolate = code_cache->GetIsolate();
10442 if (code_cache->cache()->IsUndefined()) {
10443 Handle<PolymorphicCodeCacheHashTable> result =
10444 PolymorphicCodeCacheHashTable::New(
10445 isolate,
10446 PolymorphicCodeCacheHashTable::kInitialSize);
10447 code_cache->set_cache(*result);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010448 } else {
10449 // This entry shouldn't be contained in the cache yet.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010450 DCHECK(PolymorphicCodeCacheHashTable::cast(code_cache->cache())
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010451 ->Lookup(maps, flags)->IsUndefined());
10452 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010453 Handle<PolymorphicCodeCacheHashTable> hash_table =
10454 handle(PolymorphicCodeCacheHashTable::cast(code_cache->cache()));
10455 Handle<PolymorphicCodeCacheHashTable> new_cache =
10456 PolymorphicCodeCacheHashTable::Put(hash_table, maps, flags, code);
10457 code_cache->set_cache(*new_cache);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010458}
10459
10460
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010461Handle<Object> PolymorphicCodeCache::Lookup(MapHandleList* maps,
10462 Code::Flags flags) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010463 if (!cache()->IsUndefined()) {
10464 PolymorphicCodeCacheHashTable* hash_table =
10465 PolymorphicCodeCacheHashTable::cast(cache());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010466 return Handle<Object>(hash_table->Lookup(maps, flags), GetIsolate());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010467 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010468 return GetIsolate()->factory()->undefined_value();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010469 }
10470}
10471
10472
10473// Despite their name, object of this class are not stored in the actual
10474// hash table; instead they're temporarily used for lookups. It is therefore
10475// safe to have a weak (non-owning) pointer to a MapList as a member field.
10476class PolymorphicCodeCacheHashTableKey : public HashTableKey {
10477 public:
10478 // Callers must ensure that |maps| outlives the newly constructed object.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010479 PolymorphicCodeCacheHashTableKey(MapHandleList* maps, int code_flags)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010480 : maps_(maps),
10481 code_flags_(code_flags) {}
10482
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010483 bool IsMatch(Object* other) override {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010484 MapHandleList other_maps(kDefaultListAllocationSize);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010485 int other_flags;
10486 FromObject(other, &other_flags, &other_maps);
10487 if (code_flags_ != other_flags) return false;
10488 if (maps_->length() != other_maps.length()) return false;
10489 // Compare just the hashes first because it's faster.
10490 int this_hash = MapsHashHelper(maps_, code_flags_);
10491 int other_hash = MapsHashHelper(&other_maps, other_flags);
10492 if (this_hash != other_hash) return false;
10493
10494 // Full comparison: for each map in maps_, look for an equivalent map in
10495 // other_maps. This implementation is slow, but probably good enough for
10496 // now because the lists are short (<= 4 elements currently).
10497 for (int i = 0; i < maps_->length(); ++i) {
10498 bool match_found = false;
10499 for (int j = 0; j < other_maps.length(); ++j) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010500 if (*(maps_->at(i)) == *(other_maps.at(j))) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010501 match_found = true;
10502 break;
10503 }
10504 }
10505 if (!match_found) return false;
10506 }
10507 return true;
10508 }
10509
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010510 static uint32_t MapsHashHelper(MapHandleList* maps, int code_flags) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010511 uint32_t hash = code_flags;
10512 for (int i = 0; i < maps->length(); ++i) {
10513 hash ^= maps->at(i)->Hash();
10514 }
10515 return hash;
10516 }
10517
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010518 uint32_t Hash() override { return MapsHashHelper(maps_, code_flags_); }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010519
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010520 uint32_t HashForObject(Object* obj) override {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010521 MapHandleList other_maps(kDefaultListAllocationSize);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010522 int other_flags;
10523 FromObject(obj, &other_flags, &other_maps);
10524 return MapsHashHelper(&other_maps, other_flags);
10525 }
10526
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010527 MUST_USE_RESULT Handle<Object> AsHandle(Isolate* isolate) override {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010528 // The maps in |maps_| must be copied to a newly allocated FixedArray,
10529 // both because the referenced MapList is short-lived, and because C++
10530 // objects can't be stored in the heap anyway.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010531 Handle<FixedArray> list =
10532 isolate->factory()->NewUninitializedFixedArray(maps_->length() + 1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010533 list->set(0, Smi::FromInt(code_flags_));
10534 for (int i = 0; i < maps_->length(); ++i) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010535 list->set(i + 1, *maps_->at(i));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010536 }
10537 return list;
10538 }
10539
10540 private:
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010541 static MapHandleList* FromObject(Object* obj,
10542 int* code_flags,
10543 MapHandleList* maps) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010544 FixedArray* list = FixedArray::cast(obj);
10545 maps->Rewind(0);
10546 *code_flags = Smi::cast(list->get(0))->value();
10547 for (int i = 1; i < list->length(); ++i) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010548 maps->Add(Handle<Map>(Map::cast(list->get(i))));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010549 }
10550 return maps;
10551 }
10552
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010553 MapHandleList* maps_; // weak.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010554 int code_flags_;
10555 static const int kDefaultListAllocationSize = kMaxKeyedPolymorphism + 1;
10556};
10557
10558
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010559Object* PolymorphicCodeCacheHashTable::Lookup(MapHandleList* maps,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010560 int code_kind) {
10561 DisallowHeapAllocation no_alloc;
10562 PolymorphicCodeCacheHashTableKey key(maps, code_kind);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010563 int entry = FindEntry(&key);
10564 if (entry == kNotFound) return GetHeap()->undefined_value();
10565 return get(EntryToIndex(entry) + 1);
10566}
10567
10568
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010569Handle<PolymorphicCodeCacheHashTable> PolymorphicCodeCacheHashTable::Put(
10570 Handle<PolymorphicCodeCacheHashTable> hash_table,
10571 MapHandleList* maps,
10572 int code_kind,
10573 Handle<Code> code) {
10574 PolymorphicCodeCacheHashTableKey key(maps, code_kind);
10575 Handle<PolymorphicCodeCacheHashTable> cache =
10576 EnsureCapacity(hash_table, 1, &key);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010577 int entry = cache->FindInsertionEntry(key.Hash());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010578
10579 Handle<Object> obj = key.AsHandle(hash_table->GetIsolate());
10580 cache->set(EntryToIndex(entry), *obj);
10581 cache->set(EntryToIndex(entry) + 1, *code);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010582 cache->ElementAdded();
10583 return cache;
10584}
10585
10586
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010587void FixedArray::Shrink(int new_length) {
10588 DCHECK(0 <= new_length && new_length <= length());
10589 if (new_length < length()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010590 GetHeap()->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010591 this, length() - new_length);
10592 }
10593}
10594
10595
Steve Blocka7e24c12009-10-30 11:49:00 +000010596void FixedArray::CopyTo(int pos, FixedArray* dest, int dest_pos, int len) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010597 DisallowHeapAllocation no_gc;
Leon Clarke4515c472010-02-03 11:58:03 +000010598 WriteBarrierMode mode = dest->GetWriteBarrierMode(no_gc);
Steve Blocka7e24c12009-10-30 11:49:00 +000010599 for (int index = 0; index < len; index++) {
10600 dest->set(dest_pos+index, get(pos+index), mode);
10601 }
10602}
10603
10604
10605#ifdef DEBUG
10606bool FixedArray::IsEqualTo(FixedArray* other) {
10607 if (length() != other->length()) return false;
10608 for (int i = 0 ; i < length(); ++i) {
10609 if (get(i) != other->get(i)) return false;
10610 }
10611 return true;
10612}
10613#endif
10614
10615
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010616// static
10617void WeakFixedArray::Set(Handle<WeakFixedArray> array, int index,
10618 Handle<HeapObject> value) {
10619 DCHECK(array->IsEmptySlot(index)); // Don't overwrite anything.
10620 Handle<WeakCell> cell =
10621 value->IsMap() ? Map::WeakCellForMap(Handle<Map>::cast(value))
10622 : array->GetIsolate()->factory()->NewWeakCell(value);
10623 Handle<FixedArray>::cast(array)->set(index + kFirstIndex, *cell);
10624 if (FLAG_trace_weak_arrays) {
10625 PrintF("[WeakFixedArray: storing at index %d ]\n", index);
10626 }
10627 array->set_last_used_index(index);
10628}
10629
10630
10631// static
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010632Handle<WeakFixedArray> WeakFixedArray::Add(Handle<Object> maybe_array,
10633 Handle<HeapObject> value,
10634 int* assigned_index) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010635 Handle<WeakFixedArray> array =
10636 (maybe_array.is_null() || !maybe_array->IsWeakFixedArray())
10637 ? Allocate(value->GetIsolate(), 1, Handle<WeakFixedArray>::null())
10638 : Handle<WeakFixedArray>::cast(maybe_array);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010639 // Try to store the new entry if there's room. Optimize for consecutive
10640 // accesses.
10641 int first_index = array->last_used_index();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010642 int length = array->Length();
10643 if (length > 0) {
10644 for (int i = first_index;;) {
10645 if (array->IsEmptySlot((i))) {
10646 WeakFixedArray::Set(array, i, value);
10647 if (assigned_index != NULL) *assigned_index = i;
10648 return array;
10649 }
10650 if (FLAG_trace_weak_arrays) {
10651 PrintF("[WeakFixedArray: searching for free slot]\n");
10652 }
10653 i = (i + 1) % length;
10654 if (i == first_index) break;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010655 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010656 }
10657
10658 // No usable slot found, grow the array.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010659 int new_length = length == 0 ? 1 : length + (length >> 1) + 4;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010660 Handle<WeakFixedArray> new_array =
10661 Allocate(array->GetIsolate(), new_length, array);
10662 if (FLAG_trace_weak_arrays) {
10663 PrintF("[WeakFixedArray: growing to size %d ]\n", new_length);
10664 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010665 WeakFixedArray::Set(new_array, length, value);
10666 if (assigned_index != NULL) *assigned_index = length;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010667 return new_array;
10668}
10669
10670
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010671template <class CompactionCallback>
10672void WeakFixedArray::Compact() {
10673 FixedArray* array = FixedArray::cast(this);
10674 int new_length = kFirstIndex;
10675 for (int i = kFirstIndex; i < array->length(); i++) {
10676 Object* element = array->get(i);
10677 if (element->IsSmi()) continue;
10678 if (WeakCell::cast(element)->cleared()) continue;
10679 Object* value = WeakCell::cast(element)->value();
10680 CompactionCallback::Callback(value, i - kFirstIndex,
10681 new_length - kFirstIndex);
10682 array->set(new_length++, element);
10683 }
10684 array->Shrink(new_length);
10685 set_last_used_index(0);
10686}
10687
10688
10689void WeakFixedArray::Iterator::Reset(Object* maybe_array) {
10690 if (maybe_array->IsWeakFixedArray()) {
10691 list_ = WeakFixedArray::cast(maybe_array);
10692 index_ = 0;
10693#ifdef DEBUG
10694 last_used_index_ = list_->last_used_index();
10695#endif // DEBUG
10696 }
10697}
10698
10699
10700void JSObject::PrototypeRegistryCompactionCallback::Callback(Object* value,
10701 int old_index,
10702 int new_index) {
10703 DCHECK(value->IsMap() && Map::cast(value)->is_prototype_map());
10704 Map* map = Map::cast(value);
10705 DCHECK(map->prototype_info()->IsPrototypeInfo());
10706 PrototypeInfo* proto_info = PrototypeInfo::cast(map->prototype_info());
10707 DCHECK_EQ(old_index, proto_info->registry_slot());
10708 proto_info->set_registry_slot(new_index);
10709}
10710
10711
10712template void WeakFixedArray::Compact<WeakFixedArray::NullCallback>();
10713template void
10714WeakFixedArray::Compact<JSObject::PrototypeRegistryCompactionCallback>();
10715
10716
10717bool WeakFixedArray::Remove(Handle<HeapObject> value) {
10718 if (Length() == 0) return false;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010719 // Optimize for the most recently added element to be removed again.
10720 int first_index = last_used_index();
10721 for (int i = first_index;;) {
10722 if (Get(i) == *value) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010723 Clear(i);
10724 // Users of WeakFixedArray should make sure that there are no duplicates.
10725 return true;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010726 }
10727 i = (i + 1) % Length();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010728 if (i == first_index) return false;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010729 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010730 UNREACHABLE();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010731}
10732
10733
10734// static
10735Handle<WeakFixedArray> WeakFixedArray::Allocate(
10736 Isolate* isolate, int size, Handle<WeakFixedArray> initialize_from) {
10737 DCHECK(0 <= size);
10738 Handle<FixedArray> result =
10739 isolate->factory()->NewUninitializedFixedArray(size + kFirstIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010740 int index = 0;
10741 if (!initialize_from.is_null()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010742 DCHECK(initialize_from->Length() <= size);
10743 Handle<FixedArray> raw_source = Handle<FixedArray>::cast(initialize_from);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010744 // Copy the entries without compacting, since the PrototypeInfo relies on
10745 // the index of the entries not to change.
10746 while (index < raw_source->length()) {
10747 result->set(index, raw_source->get(index));
10748 index++;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010749 }
10750 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010751 while (index < result->length()) {
10752 result->set(index, Smi::FromInt(0));
10753 index++;
10754 }
10755 return Handle<WeakFixedArray>::cast(result);
10756}
10757
10758
10759Handle<ArrayList> ArrayList::Add(Handle<ArrayList> array, Handle<Object> obj,
10760 AddMode mode) {
10761 int length = array->Length();
10762 array = EnsureSpace(array, length + 1);
10763 if (mode == kReloadLengthAfterAllocation) {
10764 DCHECK(array->Length() <= length);
10765 length = array->Length();
10766 }
10767 array->Set(length, *obj);
10768 array->SetLength(length + 1);
10769 return array;
10770}
10771
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010772Handle<ArrayList> ArrayList::Add(Handle<ArrayList> array, Handle<Object> obj1,
10773 Handle<Object> obj2, AddMode mode) {
10774 int length = array->Length();
10775 array = EnsureSpace(array, length + 2);
10776 if (mode == kReloadLengthAfterAllocation) {
10777 length = array->Length();
10778 }
10779 array->Set(length, *obj1);
10780 array->Set(length + 1, *obj2);
10781 array->SetLength(length + 2);
10782 return array;
10783}
10784
10785
10786bool ArrayList::IsFull() {
10787 int capacity = length();
10788 return kFirstIndex + Length() == capacity;
10789}
10790
10791
10792Handle<ArrayList> ArrayList::EnsureSpace(Handle<ArrayList> array, int length) {
10793 int capacity = array->length();
10794 bool empty = (capacity == 0);
10795 if (capacity < kFirstIndex + length) {
10796 Isolate* isolate = array->GetIsolate();
10797 int new_capacity = kFirstIndex + length;
10798 new_capacity = new_capacity + Max(new_capacity / 2, 2);
10799 int grow_by = new_capacity - capacity;
10800 array = Handle<ArrayList>::cast(
10801 isolate->factory()->CopyFixedArrayAndGrow(array, grow_by));
10802 if (empty) array->SetLength(0);
10803 }
10804 return array;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010805}
10806
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010807Handle<DescriptorArray> DescriptorArray::Allocate(Isolate* isolate,
10808 int number_of_descriptors,
Ben Murdoch097c5b22016-05-18 11:27:45 +010010809 int slack,
10810 PretenureFlag pretenure) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010811 DCHECK(0 <= number_of_descriptors);
10812 Factory* factory = isolate->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +000010813 // Do not use DescriptorArray::cast on incomplete object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010814 int size = number_of_descriptors + slack;
10815 if (size == 0) return factory->empty_descriptor_array();
10816 // Allocate the array of keys.
Ben Murdoch097c5b22016-05-18 11:27:45 +010010817 Handle<FixedArray> result =
10818 factory->NewFixedArray(LengthFor(size), pretenure);
Steve Blocka7e24c12009-10-30 11:49:00 +000010819
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010820 result->set(kDescriptorLengthIndex, Smi::FromInt(number_of_descriptors));
10821 result->set(kEnumCacheIndex, Smi::FromInt(0));
10822 return Handle<DescriptorArray>::cast(result);
10823}
10824
10825
10826void DescriptorArray::ClearEnumCache() {
10827 set(kEnumCacheIndex, Smi::FromInt(0));
10828}
10829
10830
10831void DescriptorArray::Replace(int index, Descriptor* descriptor) {
10832 descriptor->SetSortedKeyIndex(GetSortedKeyIndex(index));
10833 Set(index, descriptor);
Steve Blocka7e24c12009-10-30 11:49:00 +000010834}
10835
10836
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010837// static
10838void DescriptorArray::SetEnumCache(Handle<DescriptorArray> descriptors,
10839 Isolate* isolate,
10840 Handle<FixedArray> new_cache,
10841 Handle<FixedArray> new_index_cache) {
10842 DCHECK(!descriptors->IsEmpty());
10843 FixedArray* bridge_storage;
10844 bool needs_new_enum_cache = !descriptors->HasEnumCache();
10845 if (needs_new_enum_cache) {
10846 bridge_storage = *isolate->factory()->NewFixedArray(
10847 DescriptorArray::kEnumCacheBridgeLength);
10848 } else {
10849 bridge_storage = FixedArray::cast(descriptors->get(kEnumCacheIndex));
10850 }
10851 bridge_storage->set(kEnumCacheBridgeCacheIndex, *new_cache);
10852 bridge_storage->set(kEnumCacheBridgeIndicesCacheIndex,
10853 new_index_cache.is_null() ? Object::cast(Smi::FromInt(0))
10854 : *new_index_cache);
10855 if (needs_new_enum_cache) {
10856 descriptors->set(kEnumCacheIndex, bridge_storage);
10857 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010858}
10859
10860
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010861void DescriptorArray::CopyFrom(int index, DescriptorArray* src) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010862 Object* value = src->GetValue(index);
10863 PropertyDetails details = src->GetDetails(index);
10864 Descriptor desc(handle(src->GetKey(index)),
10865 handle(value, src->GetIsolate()),
10866 details);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010867 SetDescriptor(index, &desc);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010868}
10869
10870
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010871void DescriptorArray::Sort() {
Steve Blocka7e24c12009-10-30 11:49:00 +000010872 // In-place heap sort.
10873 int len = number_of_descriptors();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010874 // Reset sorting since the descriptor array might contain invalid pointers.
10875 for (int i = 0; i < len; ++i) SetSortedKey(i, i);
Steve Blocka7e24c12009-10-30 11:49:00 +000010876 // Bottom-up max-heap construction.
Steve Block6ded16b2010-05-10 14:33:55 +010010877 // Index of the last node with children
10878 const int max_parent_index = (len / 2) - 1;
10879 for (int i = max_parent_index; i >= 0; --i) {
10880 int parent_index = i;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010881 const uint32_t parent_hash = GetSortedKey(i)->Hash();
Steve Block6ded16b2010-05-10 14:33:55 +010010882 while (parent_index <= max_parent_index) {
10883 int child_index = 2 * parent_index + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010884 uint32_t child_hash = GetSortedKey(child_index)->Hash();
Steve Block6ded16b2010-05-10 14:33:55 +010010885 if (child_index + 1 < len) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010886 uint32_t right_child_hash = GetSortedKey(child_index + 1)->Hash();
Steve Block6ded16b2010-05-10 14:33:55 +010010887 if (right_child_hash > child_hash) {
10888 child_index++;
10889 child_hash = right_child_hash;
10890 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010891 }
Steve Block6ded16b2010-05-10 14:33:55 +010010892 if (child_hash <= parent_hash) break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010893 SwapSortedKeys(parent_index, child_index);
Steve Block6ded16b2010-05-10 14:33:55 +010010894 // Now element at child_index could be < its children.
10895 parent_index = child_index; // parent_hash remains correct.
Steve Blocka7e24c12009-10-30 11:49:00 +000010896 }
10897 }
10898
10899 // Extract elements and create sorted array.
10900 for (int i = len - 1; i > 0; --i) {
10901 // Put max element at the back of the array.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010902 SwapSortedKeys(0, i);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010903 // Shift down the new top element.
Steve Blocka7e24c12009-10-30 11:49:00 +000010904 int parent_index = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010905 const uint32_t parent_hash = GetSortedKey(parent_index)->Hash();
Steve Block6ded16b2010-05-10 14:33:55 +010010906 const int max_parent_index = (i / 2) - 1;
10907 while (parent_index <= max_parent_index) {
10908 int child_index = parent_index * 2 + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010909 uint32_t child_hash = GetSortedKey(child_index)->Hash();
Steve Block6ded16b2010-05-10 14:33:55 +010010910 if (child_index + 1 < i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010911 uint32_t right_child_hash = GetSortedKey(child_index + 1)->Hash();
Steve Block6ded16b2010-05-10 14:33:55 +010010912 if (right_child_hash > child_hash) {
10913 child_index++;
10914 child_hash = right_child_hash;
10915 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010916 }
Steve Block6ded16b2010-05-10 14:33:55 +010010917 if (child_hash <= parent_hash) break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010918 SwapSortedKeys(parent_index, child_index);
Steve Block6ded16b2010-05-10 14:33:55 +010010919 parent_index = child_index;
Steve Blocka7e24c12009-10-30 11:49:00 +000010920 }
10921 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010922 DCHECK(IsSortedNoDuplicates());
Kristian Monsen0d5e1162010-09-30 15:31:59 +010010923}
Steve Blocka7e24c12009-10-30 11:49:00 +000010924
Kristian Monsen0d5e1162010-09-30 15:31:59 +010010925
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010926Handle<AccessorPair> AccessorPair::Copy(Handle<AccessorPair> pair) {
10927 Handle<AccessorPair> copy = pair->GetIsolate()->factory()->NewAccessorPair();
10928 copy->set_getter(pair->getter());
10929 copy->set_setter(pair->setter());
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010930 return copy;
10931}
10932
Ben Murdoch097c5b22016-05-18 11:27:45 +010010933Handle<Object> AccessorPair::GetComponent(Handle<AccessorPair> accessor_pair,
10934 AccessorComponent component) {
10935 Object* accessor = accessor_pair->get(component);
10936 if (accessor->IsFunctionTemplateInfo()) {
10937 return ApiNatives::InstantiateFunction(
10938 handle(FunctionTemplateInfo::cast(accessor)))
10939 .ToHandleChecked();
10940 }
10941 Isolate* isolate = accessor_pair->GetIsolate();
10942 if (accessor->IsTheHole()) {
10943 return isolate->factory()->undefined_value();
10944 }
10945 return handle(accessor, isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010946}
10947
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010948Handle<DeoptimizationInputData> DeoptimizationInputData::New(
10949 Isolate* isolate, int deopt_entry_count, PretenureFlag pretenure) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010950 return Handle<DeoptimizationInputData>::cast(
10951 isolate->factory()->NewFixedArray(LengthFor(deopt_entry_count),
10952 pretenure));
Ben Murdochb0fe1622011-05-05 13:52:32 +010010953}
10954
10955
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010956Handle<DeoptimizationOutputData> DeoptimizationOutputData::New(
10957 Isolate* isolate,
10958 int number_of_deopt_points,
10959 PretenureFlag pretenure) {
10960 Handle<FixedArray> result;
10961 if (number_of_deopt_points == 0) {
10962 result = isolate->factory()->empty_fixed_array();
10963 } else {
10964 result = isolate->factory()->NewFixedArray(
10965 LengthOfFixedArray(number_of_deopt_points), pretenure);
10966 }
10967 return Handle<DeoptimizationOutputData>::cast(result);
Ben Murdochb0fe1622011-05-05 13:52:32 +010010968}
10969
10970
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010971// static
10972Handle<LiteralsArray> LiteralsArray::New(Isolate* isolate,
10973 Handle<TypeFeedbackVector> vector,
10974 int number_of_literals,
10975 PretenureFlag pretenure) {
10976 Handle<FixedArray> literals = isolate->factory()->NewFixedArray(
10977 number_of_literals + kFirstLiteralIndex, pretenure);
10978 Handle<LiteralsArray> casted_literals = Handle<LiteralsArray>::cast(literals);
10979 casted_literals->set_feedback_vector(*vector);
10980 return casted_literals;
10981}
10982
Ben Murdoch097c5b22016-05-18 11:27:45 +010010983int HandlerTable::LookupRange(int pc_offset, int* data_out,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010984 CatchPrediction* prediction_out) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010010985 int innermost_handler = -1;
10986#ifdef DEBUG
10987 // Assuming that ranges are well nested, we don't need to track the innermost
10988 // offsets. This is just to verify that the table is actually well nested.
10989 int innermost_start = std::numeric_limits<int>::min();
10990 int innermost_end = std::numeric_limits<int>::max();
10991#endif
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010992 for (int i = 0; i < length(); i += kRangeEntrySize) {
10993 int start_offset = Smi::cast(get(i + kRangeStartIndex))->value();
10994 int end_offset = Smi::cast(get(i + kRangeEndIndex))->value();
10995 int handler_field = Smi::cast(get(i + kRangeHandlerIndex))->value();
10996 int handler_offset = HandlerOffsetField::decode(handler_field);
10997 CatchPrediction prediction = HandlerPredictionField::decode(handler_field);
Ben Murdoch097c5b22016-05-18 11:27:45 +010010998 int handler_data = Smi::cast(get(i + kRangeDataIndex))->value();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010999 if (pc_offset > start_offset && pc_offset <= end_offset) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010011000 DCHECK_GE(start_offset, innermost_start);
11001 DCHECK_LT(end_offset, innermost_end);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011002 innermost_handler = handler_offset;
Ben Murdoch097c5b22016-05-18 11:27:45 +010011003#ifdef DEBUG
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011004 innermost_start = start_offset;
Ben Murdoch097c5b22016-05-18 11:27:45 +010011005 innermost_end = end_offset;
11006#endif
11007 if (data_out) *data_out = handler_data;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011008 if (prediction_out) *prediction_out = prediction;
11009 }
11010 }
11011 return innermost_handler;
11012}
11013
11014
11015// TODO(turbofan): Make sure table is sorted and use binary search.
11016int HandlerTable::LookupReturn(int pc_offset, CatchPrediction* prediction_out) {
11017 for (int i = 0; i < length(); i += kReturnEntrySize) {
11018 int return_offset = Smi::cast(get(i + kReturnOffsetIndex))->value();
11019 int handler_field = Smi::cast(get(i + kReturnHandlerIndex))->value();
11020 if (pc_offset == return_offset) {
11021 if (prediction_out) {
11022 *prediction_out = HandlerPredictionField::decode(handler_field);
11023 }
11024 return HandlerOffsetField::decode(handler_field);
11025 }
11026 }
11027 return -1;
11028}
11029
11030
Steve Blocka7e24c12009-10-30 11:49:00 +000011031#ifdef DEBUG
11032bool DescriptorArray::IsEqualTo(DescriptorArray* other) {
11033 if (IsEmpty()) return other->IsEmpty();
11034 if (other->IsEmpty()) return false;
11035 if (length() != other->length()) return false;
11036 for (int i = 0; i < length(); ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011037 if (get(i) != other->get(i)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +000011038 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011039 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +000011040}
11041#endif
11042
11043
Steve Blocka7e24c12009-10-30 11:49:00 +000011044bool String::LooksValid() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011045 if (!GetIsolate()->heap()->Contains(this)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +000011046 return true;
11047}
11048
11049
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011050// static
11051MaybeHandle<String> Name::ToFunctionName(Handle<Name> name) {
11052 if (name->IsString()) return Handle<String>::cast(name);
11053 // ES6 section 9.2.11 SetFunctionName, step 4.
11054 Isolate* const isolate = name->GetIsolate();
11055 Handle<Object> description(Handle<Symbol>::cast(name)->name(), isolate);
11056 if (description->IsUndefined()) return isolate->factory()->empty_string();
11057 IncrementalStringBuilder builder(isolate);
11058 builder.AppendCharacter('[');
11059 builder.AppendString(Handle<String>::cast(description));
11060 builder.AppendCharacter(']');
11061 return builder.Finish();
11062}
11063
11064
11065namespace {
11066
11067bool AreDigits(const uint8_t* s, int from, int to) {
11068 for (int i = from; i < to; i++) {
11069 if (s[i] < '0' || s[i] > '9') return false;
11070 }
11071
11072 return true;
11073}
11074
11075
11076int ParseDecimalInteger(const uint8_t* s, int from, int to) {
11077 DCHECK(to - from < 10); // Overflow is not possible.
11078 DCHECK(from < to);
11079 int d = s[from] - '0';
11080
11081 for (int i = from + 1; i < to; i++) {
11082 d = 10 * d + (s[i] - '0');
11083 }
11084
11085 return d;
11086}
11087
11088} // namespace
11089
11090
11091// static
11092Handle<Object> String::ToNumber(Handle<String> subject) {
11093 Isolate* const isolate = subject->GetIsolate();
11094
11095 // Flatten {subject} string first.
11096 subject = String::Flatten(subject);
11097
11098 // Fast array index case.
11099 uint32_t index;
11100 if (subject->AsArrayIndex(&index)) {
11101 return isolate->factory()->NewNumberFromUint(index);
11102 }
11103
11104 // Fast case: short integer or some sorts of junk values.
11105 if (subject->IsSeqOneByteString()) {
11106 int len = subject->length();
11107 if (len == 0) return handle(Smi::FromInt(0), isolate);
11108
11109 DisallowHeapAllocation no_gc;
11110 uint8_t const* data = Handle<SeqOneByteString>::cast(subject)->GetChars();
11111 bool minus = (data[0] == '-');
11112 int start_pos = (minus ? 1 : 0);
11113
11114 if (start_pos == len) {
11115 return isolate->factory()->nan_value();
11116 } else if (data[start_pos] > '9') {
11117 // Fast check for a junk value. A valid string may start from a
11118 // whitespace, a sign ('+' or '-'), the decimal point, a decimal digit
11119 // or the 'I' character ('Infinity'). All of that have codes not greater
11120 // than '9' except 'I' and &nbsp;.
11121 if (data[start_pos] != 'I' && data[start_pos] != 0xa0) {
11122 return isolate->factory()->nan_value();
11123 }
11124 } else if (len - start_pos < 10 && AreDigits(data, start_pos, len)) {
11125 // The maximal/minimal smi has 10 digits. If the string has less digits
11126 // we know it will fit into the smi-data type.
11127 int d = ParseDecimalInteger(data, start_pos, len);
11128 if (minus) {
11129 if (d == 0) return isolate->factory()->minus_zero_value();
11130 d = -d;
11131 } else if (!subject->HasHashCode() && len <= String::kMaxArrayIndexSize &&
11132 (len == 1 || data[0] != '0')) {
11133 // String hash is not calculated yet but all the data are present.
11134 // Update the hash field to speed up sequential convertions.
11135 uint32_t hash = StringHasher::MakeArrayIndexHash(d, len);
11136#ifdef DEBUG
11137 subject->Hash(); // Force hash calculation.
11138 DCHECK_EQ(static_cast<int>(subject->hash_field()),
11139 static_cast<int>(hash));
11140#endif
11141 subject->set_hash_field(hash);
11142 }
11143 return handle(Smi::FromInt(d), isolate);
11144 }
11145 }
11146
11147 // Slower case.
11148 int flags = ALLOW_HEX | ALLOW_OCTAL | ALLOW_BINARY;
11149 return isolate->factory()->NewNumber(
11150 StringToDouble(isolate->unicode_cache(), subject, flags));
11151}
11152
11153
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011154String::FlatContent String::GetFlatContent() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011155 DCHECK(!AllowHeapAllocation::IsAllowed());
Steve Blocka7e24c12009-10-30 11:49:00 +000011156 int length = this->length();
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011157 StringShape shape(this);
Steve Blocka7e24c12009-10-30 11:49:00 +000011158 String* string = this;
Steve Blocka7e24c12009-10-30 11:49:00 +000011159 int offset = 0;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011160 if (shape.representation_tag() == kConsStringTag) {
Steve Blocka7e24c12009-10-30 11:49:00 +000011161 ConsString* cons = ConsString::cast(string);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011162 if (cons->second()->length() != 0) {
11163 return FlatContent();
11164 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011165 string = cons->first();
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011166 shape = StringShape(string);
Steve Blocka7e24c12009-10-30 11:49:00 +000011167 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011168 if (shape.representation_tag() == kSlicedStringTag) {
11169 SlicedString* slice = SlicedString::cast(string);
11170 offset = slice->offset();
11171 string = slice->parent();
11172 shape = StringShape(string);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011173 DCHECK(shape.representation_tag() != kConsStringTag &&
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011174 shape.representation_tag() != kSlicedStringTag);
Steve Blocka7e24c12009-10-30 11:49:00 +000011175 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011176 if (shape.encoding_tag() == kOneByteStringTag) {
11177 const uint8_t* start;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011178 if (shape.representation_tag() == kSeqStringTag) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011179 start = SeqOneByteString::cast(string)->GetChars();
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011180 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011181 start = ExternalOneByteString::cast(string)->GetChars();
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011182 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011183 return FlatContent(start + offset, length);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011184 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011185 DCHECK(shape.encoding_tag() == kTwoByteStringTag);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011186 const uc16* start;
11187 if (shape.representation_tag() == kSeqStringTag) {
11188 start = SeqTwoByteString::cast(string)->GetChars();
11189 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011190 start = ExternalTwoByteString::cast(string)->GetChars();
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011191 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011192 return FlatContent(start + offset, length);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011193 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011194}
11195
11196
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011197base::SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls,
11198 RobustnessFlag robust_flag,
11199 int offset, int length,
11200 int* length_return) {
Steve Blocka7e24c12009-10-30 11:49:00 +000011201 if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011202 return base::SmartArrayPointer<char>(NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +000011203 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011204 // Negative length means the to the end of the string.
11205 if (length < 0) length = kMaxInt - offset;
11206
11207 // Compute the size of the UTF-8 string. Start at the specified offset.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011208 StringCharacterStream stream(this, offset);
Steve Blocka7e24c12009-10-30 11:49:00 +000011209 int character_position = offset;
11210 int utf8_bytes = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011211 int last = unibrow::Utf16::kNoPreviousCharacter;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011212 while (stream.HasMore() && character_position++ < offset + length) {
11213 uint16_t character = stream.GetNext();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011214 utf8_bytes += unibrow::Utf8::Length(character, last);
11215 last = character;
Steve Blocka7e24c12009-10-30 11:49:00 +000011216 }
11217
11218 if (length_return) {
11219 *length_return = utf8_bytes;
11220 }
11221
11222 char* result = NewArray<char>(utf8_bytes + 1);
11223
11224 // Convert the UTF-16 string to a UTF-8 buffer. Start at the specified offset.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011225 stream.Reset(this, offset);
Steve Blocka7e24c12009-10-30 11:49:00 +000011226 character_position = offset;
11227 int utf8_byte_position = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011228 last = unibrow::Utf16::kNoPreviousCharacter;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011229 while (stream.HasMore() && character_position++ < offset + length) {
11230 uint16_t character = stream.GetNext();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011231 if (allow_nulls == DISALLOW_NULLS && character == 0) {
11232 character = ' ';
Steve Blocka7e24c12009-10-30 11:49:00 +000011233 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011234 utf8_byte_position +=
11235 unibrow::Utf8::Encode(result + utf8_byte_position, character, last);
11236 last = character;
Steve Blocka7e24c12009-10-30 11:49:00 +000011237 }
11238 result[utf8_byte_position] = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011239 return base::SmartArrayPointer<char>(result);
Steve Blocka7e24c12009-10-30 11:49:00 +000011240}
11241
11242
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011243base::SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls,
11244 RobustnessFlag robust_flag,
11245 int* length_return) {
Steve Blocka7e24c12009-10-30 11:49:00 +000011246 return ToCString(allow_nulls, robust_flag, 0, -1, length_return);
11247}
11248
11249
Steve Blocka7e24c12009-10-30 11:49:00 +000011250const uc16* String::GetTwoByteData(unsigned start) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011251 DCHECK(!IsOneByteRepresentationUnderneath());
Steve Blocka7e24c12009-10-30 11:49:00 +000011252 switch (StringShape(this).representation_tag()) {
11253 case kSeqStringTag:
11254 return SeqTwoByteString::cast(this)->SeqTwoByteStringGetData(start);
11255 case kExternalStringTag:
11256 return ExternalTwoByteString::cast(this)->
11257 ExternalTwoByteStringGetData(start);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011258 case kSlicedStringTag: {
11259 SlicedString* slice = SlicedString::cast(this);
11260 return slice->parent()->GetTwoByteData(start + slice->offset());
11261 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011262 case kConsStringTag:
11263 UNREACHABLE();
11264 return NULL;
11265 }
11266 UNREACHABLE();
11267 return NULL;
11268}
11269
11270
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011271base::SmartArrayPointer<uc16> String::ToWideCString(
11272 RobustnessFlag robust_flag) {
Steve Blocka7e24c12009-10-30 11:49:00 +000011273 if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011274 return base::SmartArrayPointer<uc16>();
Steve Blocka7e24c12009-10-30 11:49:00 +000011275 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011276 StringCharacterStream stream(this);
Steve Blocka7e24c12009-10-30 11:49:00 +000011277
11278 uc16* result = NewArray<uc16>(length() + 1);
11279
11280 int i = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011281 while (stream.HasMore()) {
11282 uint16_t character = stream.GetNext();
Steve Blocka7e24c12009-10-30 11:49:00 +000011283 result[i++] = character;
11284 }
11285 result[i] = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011286 return base::SmartArrayPointer<uc16>(result);
Steve Blocka7e24c12009-10-30 11:49:00 +000011287}
11288
11289
11290const uc16* SeqTwoByteString::SeqTwoByteStringGetData(unsigned start) {
11291 return reinterpret_cast<uc16*>(
11292 reinterpret_cast<char*>(this) - kHeapObjectTag + kHeaderSize) + start;
11293}
11294
11295
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011296void Relocatable::PostGarbageCollectionProcessing(Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +010011297 Relocatable* current = isolate->relocatable_top();
Steve Blocka7e24c12009-10-30 11:49:00 +000011298 while (current != NULL) {
11299 current->PostGarbageCollection();
11300 current = current->prev_;
11301 }
11302}
11303
11304
11305// Reserve space for statics needing saving and restoring.
11306int Relocatable::ArchiveSpacePerThread() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011307 return sizeof(Relocatable*); // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +000011308}
11309
11310
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011311// Archive statics that are thread-local.
Ben Murdoch257744e2011-11-30 15:57:28 +000011312char* Relocatable::ArchiveState(Isolate* isolate, char* to) {
Steve Block44f0eee2011-05-26 01:26:41 +010011313 *reinterpret_cast<Relocatable**>(to) = isolate->relocatable_top();
11314 isolate->set_relocatable_top(NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +000011315 return to + ArchiveSpacePerThread();
11316}
11317
11318
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011319// Restore statics that are thread-local.
Ben Murdoch257744e2011-11-30 15:57:28 +000011320char* Relocatable::RestoreState(Isolate* isolate, char* from) {
Steve Block44f0eee2011-05-26 01:26:41 +010011321 isolate->set_relocatable_top(*reinterpret_cast<Relocatable**>(from));
Steve Blocka7e24c12009-10-30 11:49:00 +000011322 return from + ArchiveSpacePerThread();
11323}
11324
11325
11326char* Relocatable::Iterate(ObjectVisitor* v, char* thread_storage) {
11327 Relocatable* top = *reinterpret_cast<Relocatable**>(thread_storage);
11328 Iterate(v, top);
11329 return thread_storage + ArchiveSpacePerThread();
11330}
11331
11332
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011333void Relocatable::Iterate(Isolate* isolate, ObjectVisitor* v) {
Steve Block44f0eee2011-05-26 01:26:41 +010011334 Iterate(v, isolate->relocatable_top());
Steve Blocka7e24c12009-10-30 11:49:00 +000011335}
11336
11337
11338void Relocatable::Iterate(ObjectVisitor* v, Relocatable* top) {
11339 Relocatable* current = top;
11340 while (current != NULL) {
11341 current->IterateInstance(v);
11342 current = current->prev_;
11343 }
11344}
11345
11346
Steve Block44f0eee2011-05-26 01:26:41 +010011347FlatStringReader::FlatStringReader(Isolate* isolate, Handle<String> str)
11348 : Relocatable(isolate),
11349 str_(str.location()),
Steve Blocka7e24c12009-10-30 11:49:00 +000011350 length_(str->length()) {
11351 PostGarbageCollection();
11352}
11353
11354
Steve Block44f0eee2011-05-26 01:26:41 +010011355FlatStringReader::FlatStringReader(Isolate* isolate, Vector<const char> input)
11356 : Relocatable(isolate),
11357 str_(0),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011358 is_one_byte_(true),
Steve Blocka7e24c12009-10-30 11:49:00 +000011359 length_(input.length()),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011360 start_(input.start()) {}
Steve Blocka7e24c12009-10-30 11:49:00 +000011361
11362
11363void FlatStringReader::PostGarbageCollection() {
11364 if (str_ == NULL) return;
11365 Handle<String> str(str_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011366 DCHECK(str->IsFlat());
11367 DisallowHeapAllocation no_gc;
11368 // This does not actually prevent the vector from being relocated later.
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011369 String::FlatContent content = str->GetFlatContent();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011370 DCHECK(content.IsFlat());
11371 is_one_byte_ = content.IsOneByte();
11372 if (is_one_byte_) {
11373 start_ = content.ToOneByteVector().start();
Steve Blocka7e24c12009-10-30 11:49:00 +000011374 } else {
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011375 start_ = content.ToUC16Vector().start();
Steve Blocka7e24c12009-10-30 11:49:00 +000011376 }
11377}
11378
11379
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011380void ConsStringIterator::Initialize(ConsString* cons_string, int offset) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011381 DCHECK(cons_string != NULL);
11382 root_ = cons_string;
11383 consumed_ = offset;
11384 // Force stack blown condition to trigger restart.
11385 depth_ = 1;
11386 maximum_depth_ = kStackSize + depth_;
11387 DCHECK(StackBlown());
Steve Blocka7e24c12009-10-30 11:49:00 +000011388}
11389
11390
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011391String* ConsStringIterator::Continue(int* offset_out) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011392 DCHECK(depth_ != 0);
11393 DCHECK_EQ(0, *offset_out);
11394 bool blew_stack = StackBlown();
11395 String* string = NULL;
11396 // Get the next leaf if there is one.
11397 if (!blew_stack) string = NextLeaf(&blew_stack);
11398 // Restart search from root.
11399 if (blew_stack) {
11400 DCHECK(string == NULL);
11401 string = Search(offset_out);
Steve Blocka7e24c12009-10-30 11:49:00 +000011402 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011403 // Ensure future calls return null immediately.
11404 if (string == NULL) Reset(NULL);
11405 return string;
Steve Blocka7e24c12009-10-30 11:49:00 +000011406}
11407
11408
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011409String* ConsStringIterator::Search(int* offset_out) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011410 ConsString* cons_string = root_;
11411 // Reset the stack, pushing the root string.
11412 depth_ = 1;
11413 maximum_depth_ = 1;
11414 frames_[0] = cons_string;
11415 const int consumed = consumed_;
11416 int offset = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +000011417 while (true) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011418 // Loop until the string is found which contains the target offset.
11419 String* string = cons_string->first();
11420 int length = string->length();
11421 int32_t type;
11422 if (consumed < offset + length) {
11423 // Target offset is in the left branch.
11424 // Keep going if we're still in a ConString.
11425 type = string->map()->instance_type();
11426 if ((type & kStringRepresentationMask) == kConsStringTag) {
11427 cons_string = ConsString::cast(string);
11428 PushLeft(cons_string);
Steve Blocka7e24c12009-10-30 11:49:00 +000011429 continue;
Steve Blocka7e24c12009-10-30 11:49:00 +000011430 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011431 // Tell the stack we're done descending.
11432 AdjustMaximumDepth();
Steve Blocka7e24c12009-10-30 11:49:00 +000011433 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011434 // Descend right.
11435 // Update progress through the string.
11436 offset += length;
11437 // Keep going if we're still in a ConString.
11438 string = cons_string->second();
11439 type = string->map()->instance_type();
11440 if ((type & kStringRepresentationMask) == kConsStringTag) {
11441 cons_string = ConsString::cast(string);
11442 PushRight(cons_string);
11443 continue;
Steve Blocka7e24c12009-10-30 11:49:00 +000011444 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011445 // Need this to be updated for the current string.
11446 length = string->length();
11447 // Account for the possibility of an empty right leaf.
11448 // This happens only if we have asked for an offset outside the string.
11449 if (length == 0) {
11450 // Reset so future operations will return null immediately.
11451 Reset(NULL);
11452 return NULL;
11453 }
11454 // Tell the stack we're done descending.
11455 AdjustMaximumDepth();
11456 // Pop stack so next iteration is in correct place.
11457 Pop();
11458 }
11459 DCHECK(length != 0);
11460 // Adjust return values and exit.
11461 consumed_ = offset + length;
11462 *offset_out = consumed - offset;
11463 return string;
11464 }
11465 UNREACHABLE();
11466 return NULL;
11467}
11468
11469
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011470String* ConsStringIterator::NextLeaf(bool* blew_stack) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011471 while (true) {
11472 // Tree traversal complete.
11473 if (depth_ == 0) {
11474 *blew_stack = false;
11475 return NULL;
11476 }
11477 // We've lost track of higher nodes.
11478 if (StackBlown()) {
11479 *blew_stack = true;
11480 return NULL;
11481 }
11482 // Go right.
11483 ConsString* cons_string = frames_[OffsetForDepth(depth_ - 1)];
11484 String* string = cons_string->second();
11485 int32_t type = string->map()->instance_type();
11486 if ((type & kStringRepresentationMask) != kConsStringTag) {
11487 // Pop stack so next iteration is in correct place.
11488 Pop();
11489 int length = string->length();
11490 // Could be a flattened ConsString.
11491 if (length == 0) continue;
11492 consumed_ += length;
11493 return string;
11494 }
11495 cons_string = ConsString::cast(string);
11496 PushRight(cons_string);
11497 // Need to traverse all the way left.
11498 while (true) {
11499 // Continue left.
11500 string = cons_string->first();
11501 type = string->map()->instance_type();
11502 if ((type & kStringRepresentationMask) != kConsStringTag) {
11503 AdjustMaximumDepth();
11504 int length = string->length();
11505 DCHECK(length != 0);
11506 consumed_ += length;
11507 return string;
11508 }
11509 cons_string = ConsString::cast(string);
11510 PushLeft(cons_string);
Steve Blocka7e24c12009-10-30 11:49:00 +000011511 }
11512 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011513 UNREACHABLE();
11514 return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +000011515}
11516
11517
Steve Blocka7e24c12009-10-30 11:49:00 +000011518uint16_t ConsString::ConsStringGet(int index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011519 DCHECK(index >= 0 && index < this->length());
Steve Blocka7e24c12009-10-30 11:49:00 +000011520
11521 // Check for a flattened cons string
11522 if (second()->length() == 0) {
11523 String* left = first();
11524 return left->Get(index);
11525 }
11526
11527 String* string = String::cast(this);
11528
11529 while (true) {
11530 if (StringShape(string).IsCons()) {
11531 ConsString* cons_string = ConsString::cast(string);
11532 String* left = cons_string->first();
11533 if (left->length() > index) {
11534 string = left;
11535 } else {
11536 index -= left->length();
11537 string = cons_string->second();
11538 }
11539 } else {
11540 return string->Get(index);
11541 }
11542 }
11543
11544 UNREACHABLE();
11545 return 0;
11546}
11547
11548
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011549uint16_t SlicedString::SlicedStringGet(int index) {
11550 return parent()->Get(offset() + index);
11551}
11552
11553
Steve Blocka7e24c12009-10-30 11:49:00 +000011554template <typename sinkchar>
11555void String::WriteToFlat(String* src,
11556 sinkchar* sink,
11557 int f,
11558 int t) {
11559 String* source = src;
11560 int from = f;
11561 int to = t;
11562 while (true) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011563 DCHECK(0 <= from && from <= to && to <= source->length());
Steve Blocka7e24c12009-10-30 11:49:00 +000011564 switch (StringShape(source).full_representation_tag()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011565 case kOneByteStringTag | kExternalStringTag: {
11566 CopyChars(sink, ExternalOneByteString::cast(source)->GetChars() + from,
Steve Blocka7e24c12009-10-30 11:49:00 +000011567 to - from);
11568 return;
11569 }
11570 case kTwoByteStringTag | kExternalStringTag: {
11571 const uc16* data =
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011572 ExternalTwoByteString::cast(source)->GetChars();
Steve Blocka7e24c12009-10-30 11:49:00 +000011573 CopyChars(sink,
11574 data + from,
11575 to - from);
11576 return;
11577 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011578 case kOneByteStringTag | kSeqStringTag: {
Steve Blocka7e24c12009-10-30 11:49:00 +000011579 CopyChars(sink,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011580 SeqOneByteString::cast(source)->GetChars() + from,
Steve Blocka7e24c12009-10-30 11:49:00 +000011581 to - from);
11582 return;
11583 }
11584 case kTwoByteStringTag | kSeqStringTag: {
11585 CopyChars(sink,
11586 SeqTwoByteString::cast(source)->GetChars() + from,
11587 to - from);
11588 return;
11589 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011590 case kOneByteStringTag | kConsStringTag:
Steve Blocka7e24c12009-10-30 11:49:00 +000011591 case kTwoByteStringTag | kConsStringTag: {
11592 ConsString* cons_string = ConsString::cast(source);
11593 String* first = cons_string->first();
11594 int boundary = first->length();
11595 if (to - boundary >= boundary - from) {
11596 // Right hand side is longer. Recurse over left.
11597 if (from < boundary) {
11598 WriteToFlat(first, sink, from, boundary);
Ben Murdochda12d292016-06-02 14:46:10 +010011599 if (from == 0 && cons_string->second() == first) {
11600 CopyChars(sink + boundary, sink, boundary);
11601 return;
11602 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011603 sink += boundary - from;
11604 from = 0;
11605 } else {
11606 from -= boundary;
11607 }
11608 to -= boundary;
11609 source = cons_string->second();
11610 } else {
11611 // Left hand side is longer. Recurse over right.
11612 if (to > boundary) {
11613 String* second = cons_string->second();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011614 // When repeatedly appending to a string, we get a cons string that
11615 // is unbalanced to the left, a list, essentially. We inline the
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011616 // common case of sequential one-byte right child.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011617 if (to - boundary == 1) {
11618 sink[boundary - from] = static_cast<sinkchar>(second->Get(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011619 } else if (second->IsSeqOneByteString()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011620 CopyChars(sink + boundary - from,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011621 SeqOneByteString::cast(second)->GetChars(),
Steve Blocka7e24c12009-10-30 11:49:00 +000011622 to - boundary);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011623 } else {
11624 WriteToFlat(second,
11625 sink + boundary - from,
11626 0,
11627 to - boundary);
11628 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011629 to = boundary;
11630 }
11631 source = first;
11632 }
11633 break;
11634 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011635 case kOneByteStringTag | kSlicedStringTag:
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011636 case kTwoByteStringTag | kSlicedStringTag: {
11637 SlicedString* slice = SlicedString::cast(source);
11638 unsigned offset = slice->offset();
11639 WriteToFlat(slice->parent(), sink, from + offset, to + offset);
11640 return;
11641 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011642 }
11643 }
11644}
11645
11646
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011647
11648template <typename SourceChar>
11649static void CalculateLineEndsImpl(Isolate* isolate,
11650 List<int>* line_ends,
11651 Vector<const SourceChar> src,
11652 bool include_ending_line) {
11653 const int src_len = src.length();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011654 UnicodeCache* cache = isolate->unicode_cache();
11655 for (int i = 0; i < src_len - 1; i++) {
11656 SourceChar current = src[i];
11657 SourceChar next = src[i + 1];
11658 if (cache->IsLineTerminatorSequence(current, next)) line_ends->Add(i);
11659 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011660
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011661 if (src_len > 0 && cache->IsLineTerminatorSequence(src[src_len - 1], 0)) {
11662 line_ends->Add(src_len - 1);
11663 }
11664 if (include_ending_line) {
11665 // Include one character beyond the end of script. The rewriter uses that
11666 // position for the implicit return statement.
11667 line_ends->Add(src_len);
Steve Blocka7e24c12009-10-30 11:49:00 +000011668 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011669}
11670
11671
11672Handle<FixedArray> String::CalculateLineEnds(Handle<String> src,
11673 bool include_ending_line) {
11674 src = Flatten(src);
11675 // Rough estimate of line count based on a roughly estimated average
11676 // length of (unpacked) code.
11677 int line_count_estimate = src->length() >> 4;
11678 List<int> line_ends(line_count_estimate);
11679 Isolate* isolate = src->GetIsolate();
11680 { DisallowHeapAllocation no_allocation; // ensure vectors stay valid.
11681 // Dispatch on type of strings.
11682 String::FlatContent content = src->GetFlatContent();
11683 DCHECK(content.IsFlat());
11684 if (content.IsOneByte()) {
11685 CalculateLineEndsImpl(isolate,
11686 &line_ends,
11687 content.ToOneByteVector(),
11688 include_ending_line);
11689 } else {
11690 CalculateLineEndsImpl(isolate,
11691 &line_ends,
11692 content.ToUC16Vector(),
11693 include_ending_line);
11694 }
11695 }
11696 int line_count = line_ends.length();
11697 Handle<FixedArray> array = isolate->factory()->NewFixedArray(line_count);
11698 for (int i = 0; i < line_count; i++) {
11699 array->set(i, Smi::FromInt(line_ends[i]));
11700 }
11701 return array;
Steve Blocka7e24c12009-10-30 11:49:00 +000011702}
11703
11704
11705// Compares the contents of two strings by reading and comparing
11706// int-sized blocks of characters.
11707template <typename Char>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011708static inline bool CompareRawStringContents(const Char* const a,
11709 const Char* const b,
11710 int length) {
11711 return CompareChars(a, b, length) == 0;
11712}
11713
11714
11715template<typename Chars1, typename Chars2>
11716class RawStringComparator : public AllStatic {
11717 public:
11718 static inline bool compare(const Chars1* a, const Chars2* b, int len) {
11719 DCHECK(sizeof(Chars1) != sizeof(Chars2));
11720 for (int i = 0; i < len; i++) {
11721 if (a[i] != b[i]) {
Steve Blocka7e24c12009-10-30 11:49:00 +000011722 return false;
11723 }
11724 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011725 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +000011726 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011727};
Steve Blocka7e24c12009-10-30 11:49:00 +000011728
11729
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011730template<>
11731class RawStringComparator<uint16_t, uint16_t> {
11732 public:
11733 static inline bool compare(const uint16_t* a, const uint16_t* b, int len) {
11734 return CompareRawStringContents(a, b, len);
Steve Blocka7e24c12009-10-30 11:49:00 +000011735 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011736};
11737
11738
11739template<>
11740class RawStringComparator<uint8_t, uint8_t> {
11741 public:
11742 static inline bool compare(const uint8_t* a, const uint8_t* b, int len) {
11743 return CompareRawStringContents(a, b, len);
11744 }
11745};
11746
11747
11748class StringComparator {
11749 class State {
11750 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011751 State() : is_one_byte_(true), length_(0), buffer8_(NULL) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011752
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011753 void Init(String* string) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011754 ConsString* cons_string = String::VisitFlat(this, string);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011755 iter_.Reset(cons_string);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011756 if (cons_string != NULL) {
11757 int offset;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011758 string = iter_.Next(&offset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011759 String::VisitFlat(this, string, offset);
11760 }
11761 }
11762
11763 inline void VisitOneByteString(const uint8_t* chars, int length) {
11764 is_one_byte_ = true;
11765 buffer8_ = chars;
11766 length_ = length;
11767 }
11768
11769 inline void VisitTwoByteString(const uint16_t* chars, int length) {
11770 is_one_byte_ = false;
11771 buffer16_ = chars;
11772 length_ = length;
11773 }
11774
11775 void Advance(int consumed) {
11776 DCHECK(consumed <= length_);
11777 // Still in buffer.
11778 if (length_ != consumed) {
11779 if (is_one_byte_) {
11780 buffer8_ += consumed;
11781 } else {
11782 buffer16_ += consumed;
11783 }
11784 length_ -= consumed;
11785 return;
11786 }
11787 // Advance state.
11788 int offset;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011789 String* next = iter_.Next(&offset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011790 DCHECK_EQ(0, offset);
11791 DCHECK(next != NULL);
11792 String::VisitFlat(this, next);
11793 }
11794
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011795 ConsStringIterator iter_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011796 bool is_one_byte_;
11797 int length_;
11798 union {
11799 const uint8_t* buffer8_;
11800 const uint16_t* buffer16_;
11801 };
11802
11803 private:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011804 DISALLOW_COPY_AND_ASSIGN(State);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011805 };
11806
11807 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011808 inline StringComparator() {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011809
11810 template<typename Chars1, typename Chars2>
11811 static inline bool Equals(State* state_1, State* state_2, int to_check) {
11812 const Chars1* a = reinterpret_cast<const Chars1*>(state_1->buffer8_);
11813 const Chars2* b = reinterpret_cast<const Chars2*>(state_2->buffer8_);
11814 return RawStringComparator<Chars1, Chars2>::compare(a, b, to_check);
11815 }
11816
11817 bool Equals(String* string_1, String* string_2) {
11818 int length = string_1->length();
11819 state_1_.Init(string_1);
11820 state_2_.Init(string_2);
11821 while (true) {
11822 int to_check = Min(state_1_.length_, state_2_.length_);
11823 DCHECK(to_check > 0 && to_check <= length);
11824 bool is_equal;
11825 if (state_1_.is_one_byte_) {
11826 if (state_2_.is_one_byte_) {
11827 is_equal = Equals<uint8_t, uint8_t>(&state_1_, &state_2_, to_check);
11828 } else {
11829 is_equal = Equals<uint8_t, uint16_t>(&state_1_, &state_2_, to_check);
11830 }
11831 } else {
11832 if (state_2_.is_one_byte_) {
11833 is_equal = Equals<uint16_t, uint8_t>(&state_1_, &state_2_, to_check);
11834 } else {
11835 is_equal = Equals<uint16_t, uint16_t>(&state_1_, &state_2_, to_check);
11836 }
11837 }
11838 // Looping done.
11839 if (!is_equal) return false;
11840 length -= to_check;
11841 // Exit condition. Strings are equal.
11842 if (length == 0) return true;
11843 state_1_.Advance(to_check);
11844 state_2_.Advance(to_check);
11845 }
11846 }
11847
11848 private:
11849 State state_1_;
11850 State state_2_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011851
11852 DISALLOW_COPY_AND_ASSIGN(StringComparator);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011853};
Steve Blocka7e24c12009-10-30 11:49:00 +000011854
11855
Steve Blocka7e24c12009-10-30 11:49:00 +000011856bool String::SlowEquals(String* other) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011857 DisallowHeapAllocation no_gc;
Steve Blocka7e24c12009-10-30 11:49:00 +000011858 // Fast check: negative check with lengths.
11859 int len = length();
11860 if (len != other->length()) return false;
11861 if (len == 0) return true;
11862
11863 // Fast check: if hash code is computed for both strings
11864 // a fast negative check can be performed.
11865 if (HasHashCode() && other->HasHashCode()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011866#ifdef ENABLE_SLOW_DCHECKS
Ben Murdochc7cc0282012-03-05 14:35:55 +000011867 if (FLAG_enable_slow_asserts) {
11868 if (Hash() != other->Hash()) {
11869 bool found_difference = false;
11870 for (int i = 0; i < len; i++) {
11871 if (Get(i) != other->Get(i)) {
11872 found_difference = true;
11873 break;
11874 }
11875 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011876 DCHECK(found_difference);
Ben Murdochc7cc0282012-03-05 14:35:55 +000011877 }
11878 }
11879#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000011880 if (Hash() != other->Hash()) return false;
11881 }
11882
Leon Clarkef7060e22010-06-03 12:02:55 +010011883 // We know the strings are both non-empty. Compare the first chars
11884 // before we try to flatten the strings.
11885 if (this->Get(0) != other->Get(0)) return false;
11886
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011887 if (IsSeqOneByteString() && other->IsSeqOneByteString()) {
11888 const uint8_t* str1 = SeqOneByteString::cast(this)->GetChars();
11889 const uint8_t* str2 = SeqOneByteString::cast(other)->GetChars();
11890 return CompareRawStringContents(str1, str2, len);
Steve Blocka7e24c12009-10-30 11:49:00 +000011891 }
11892
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011893 StringComparator comparator;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011894 return comparator.Equals(this, other);
11895}
11896
11897
11898bool String::SlowEquals(Handle<String> one, Handle<String> two) {
11899 // Fast check: negative check with lengths.
11900 int one_length = one->length();
11901 if (one_length != two->length()) return false;
11902 if (one_length == 0) return true;
11903
11904 // Fast check: if hash code is computed for both strings
11905 // a fast negative check can be performed.
11906 if (one->HasHashCode() && two->HasHashCode()) {
11907#ifdef ENABLE_SLOW_DCHECKS
11908 if (FLAG_enable_slow_asserts) {
11909 if (one->Hash() != two->Hash()) {
11910 bool found_difference = false;
11911 for (int i = 0; i < one_length; i++) {
11912 if (one->Get(i) != two->Get(i)) {
11913 found_difference = true;
11914 break;
11915 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011916 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011917 DCHECK(found_difference);
Steve Blocka7e24c12009-10-30 11:49:00 +000011918 }
11919 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011920#endif
11921 if (one->Hash() != two->Hash()) return false;
11922 }
11923
11924 // We know the strings are both non-empty. Compare the first chars
11925 // before we try to flatten the strings.
11926 if (one->Get(0) != two->Get(0)) return false;
11927
11928 one = String::Flatten(one);
11929 two = String::Flatten(two);
11930
11931 DisallowHeapAllocation no_gc;
11932 String::FlatContent flat1 = one->GetFlatContent();
11933 String::FlatContent flat2 = two->GetFlatContent();
11934
11935 if (flat1.IsOneByte() && flat2.IsOneByte()) {
11936 return CompareRawStringContents(flat1.ToOneByteVector().start(),
11937 flat2.ToOneByteVector().start(),
11938 one_length);
Steve Blocka7e24c12009-10-30 11:49:00 +000011939 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011940 for (int i = 0; i < one_length; i++) {
11941 if (flat1.Get(i) != flat2.Get(i)) return false;
11942 }
11943 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +000011944 }
11945}
11946
11947
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011948// static
11949ComparisonResult String::Compare(Handle<String> x, Handle<String> y) {
11950 // A few fast case tests before we flatten.
11951 if (x.is_identical_to(y)) {
11952 return ComparisonResult::kEqual;
11953 } else if (y->length() == 0) {
11954 return x->length() == 0 ? ComparisonResult::kEqual
11955 : ComparisonResult::kGreaterThan;
11956 } else if (x->length() == 0) {
11957 return ComparisonResult::kLessThan;
Steve Blocka7e24c12009-10-30 11:49:00 +000011958 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011959
11960 int const d = x->Get(0) - y->Get(0);
11961 if (d < 0) {
11962 return ComparisonResult::kLessThan;
11963 } else if (d > 0) {
11964 return ComparisonResult::kGreaterThan;
11965 }
11966
11967 // Slow case.
11968 x = String::Flatten(x);
11969 y = String::Flatten(y);
11970
11971 DisallowHeapAllocation no_gc;
11972 ComparisonResult result = ComparisonResult::kEqual;
11973 int prefix_length = x->length();
11974 if (y->length() < prefix_length) {
11975 prefix_length = y->length();
11976 result = ComparisonResult::kGreaterThan;
11977 } else if (y->length() > prefix_length) {
11978 result = ComparisonResult::kLessThan;
11979 }
11980 int r;
11981 String::FlatContent x_content = x->GetFlatContent();
11982 String::FlatContent y_content = y->GetFlatContent();
11983 if (x_content.IsOneByte()) {
11984 Vector<const uint8_t> x_chars = x_content.ToOneByteVector();
11985 if (y_content.IsOneByte()) {
11986 Vector<const uint8_t> y_chars = y_content.ToOneByteVector();
11987 r = CompareChars(x_chars.start(), y_chars.start(), prefix_length);
11988 } else {
11989 Vector<const uc16> y_chars = y_content.ToUC16Vector();
11990 r = CompareChars(x_chars.start(), y_chars.start(), prefix_length);
11991 }
11992 } else {
11993 Vector<const uc16> x_chars = x_content.ToUC16Vector();
11994 if (y_content.IsOneByte()) {
11995 Vector<const uint8_t> y_chars = y_content.ToOneByteVector();
11996 r = CompareChars(x_chars.start(), y_chars.start(), prefix_length);
11997 } else {
11998 Vector<const uc16> y_chars = y_content.ToUC16Vector();
11999 r = CompareChars(x_chars.start(), y_chars.start(), prefix_length);
12000 }
12001 }
12002 if (r < 0) {
12003 result = ComparisonResult::kLessThan;
12004 } else if (r > 0) {
12005 result = ComparisonResult::kGreaterThan;
12006 }
12007 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +000012008}
12009
12010
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012011bool String::IsUtf8EqualTo(Vector<const char> str, bool allow_prefix_match) {
Steve Blocka7e24c12009-10-30 11:49:00 +000012012 int slen = length();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012013 // Can't check exact length equality, but we can check bounds.
12014 int str_len = str.length();
12015 if (!allow_prefix_match &&
12016 (str_len < slen ||
12017 str_len > slen*static_cast<int>(unibrow::Utf8::kMaxEncodedSize))) {
12018 return false;
12019 }
Steve Blocka7e24c12009-10-30 11:49:00 +000012020 int i;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012021 size_t remaining_in_str = static_cast<size_t>(str_len);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012022 const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(str.start());
12023 for (i = 0; i < slen && remaining_in_str > 0; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012024 size_t cursor = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012025 uint32_t r = unibrow::Utf8::ValueOf(utf8_data, remaining_in_str, &cursor);
12026 DCHECK(cursor > 0 && cursor <= remaining_in_str);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012027 if (r > unibrow::Utf16::kMaxNonSurrogateCharCode) {
12028 if (i > slen - 1) return false;
12029 if (Get(i++) != unibrow::Utf16::LeadSurrogate(r)) return false;
12030 if (Get(i) != unibrow::Utf16::TrailSurrogate(r)) return false;
12031 } else {
12032 if (Get(i) != r) return false;
12033 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012034 utf8_data += cursor;
12035 remaining_in_str -= cursor;
Steve Blocka7e24c12009-10-30 11:49:00 +000012036 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012037 return (allow_prefix_match || i == slen) && remaining_in_str == 0;
Steve Blocka7e24c12009-10-30 11:49:00 +000012038}
12039
12040
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012041bool String::IsOneByteEqualTo(Vector<const uint8_t> str) {
Steve Block9fac8402011-05-12 15:51:54 +010012042 int slen = length();
12043 if (str.length() != slen) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012044 DisallowHeapAllocation no_gc;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000012045 FlatContent content = GetFlatContent();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012046 if (content.IsOneByte()) {
12047 return CompareChars(content.ToOneByteVector().start(),
Ben Murdoch69a99ed2011-11-30 16:03:39 +000012048 str.start(), slen) == 0;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000012049 }
12050 for (int i = 0; i < slen; i++) {
12051 if (Get(i) != static_cast<uint16_t>(str[i])) return false;
Steve Block9fac8402011-05-12 15:51:54 +010012052 }
12053 return true;
12054}
12055
12056
12057bool String::IsTwoByteEqualTo(Vector<const uc16> str) {
12058 int slen = length();
12059 if (str.length() != slen) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012060 DisallowHeapAllocation no_gc;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000012061 FlatContent content = GetFlatContent();
12062 if (content.IsTwoByte()) {
12063 return CompareChars(content.ToUC16Vector().start(), str.start(), slen) == 0;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000012064 }
Steve Block9fac8402011-05-12 15:51:54 +010012065 for (int i = 0; i < slen; i++) {
12066 if (Get(i) != str[i]) return false;
12067 }
12068 return true;
12069}
12070
12071
Steve Blocka7e24c12009-10-30 11:49:00 +000012072uint32_t String::ComputeAndSetHash() {
12073 // Should only be called if hash code has not yet been computed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012074 DCHECK(!HasHashCode());
Steve Blocka7e24c12009-10-30 11:49:00 +000012075
12076 // Store the hash code in the object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012077 uint32_t field = IteratingStringHasher::Hash(this, GetHeap()->HashSeed());
Steve Blockd0582a62009-12-15 09:54:21 +000012078 set_hash_field(field);
Steve Blocka7e24c12009-10-30 11:49:00 +000012079
12080 // Check the hash code is there.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012081 DCHECK(HasHashCode());
Steve Blocka7e24c12009-10-30 11:49:00 +000012082 uint32_t result = field >> kHashShift;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012083 DCHECK(result != 0); // Ensure that the hash value of 0 is never computed.
Steve Blocka7e24c12009-10-30 11:49:00 +000012084 return result;
12085}
12086
12087
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012088bool String::ComputeArrayIndex(uint32_t* index) {
12089 int length = this->length();
Steve Blocka7e24c12009-10-30 11:49:00 +000012090 if (length == 0 || length > kMaxArrayIndexSize) return false;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012091 StringCharacterStream stream(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012092 return StringToArrayIndex(&stream, index);
Steve Blocka7e24c12009-10-30 11:49:00 +000012093}
12094
12095
12096bool String::SlowAsArrayIndex(uint32_t* index) {
12097 if (length() <= kMaxCachedArrayIndexLength) {
12098 Hash(); // force computation of hash code
Steve Blockd0582a62009-12-15 09:54:21 +000012099 uint32_t field = hash_field();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010012100 if ((field & kIsNotArrayIndexMask) != 0) return false;
Steve Blockd0582a62009-12-15 09:54:21 +000012101 // Isolate the array index form the full hash field.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012102 *index = ArrayIndexValueBits::decode(field);
Steve Blocka7e24c12009-10-30 11:49:00 +000012103 return true;
12104 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012105 return ComputeArrayIndex(index);
Steve Blocka7e24c12009-10-30 11:49:00 +000012106 }
12107}
12108
12109
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012110Handle<String> SeqString::Truncate(Handle<SeqString> string, int new_length) {
12111 int new_size, old_size;
12112 int old_length = string->length();
12113 if (old_length <= new_length) return string;
12114
12115 if (string->IsSeqOneByteString()) {
12116 old_size = SeqOneByteString::SizeFor(old_length);
12117 new_size = SeqOneByteString::SizeFor(new_length);
12118 } else {
12119 DCHECK(string->IsSeqTwoByteString());
12120 old_size = SeqTwoByteString::SizeFor(old_length);
12121 new_size = SeqTwoByteString::SizeFor(new_length);
12122 }
12123
12124 int delta = old_size - new_size;
12125
12126 Address start_of_string = string->address();
12127 DCHECK_OBJECT_ALIGNED(start_of_string);
12128 DCHECK_OBJECT_ALIGNED(start_of_string + new_size);
12129
12130 Heap* heap = string->GetHeap();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012131 // Sizes are pointer size aligned, so that we can use filler objects
12132 // that are a multiple of pointer size.
Ben Murdochda12d292016-06-02 14:46:10 +010012133 heap->CreateFillerObjectAt(start_of_string + new_size, delta,
12134 ClearRecordedSlots::kNo);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012135 heap->AdjustLiveBytes(*string, -delta, Heap::CONCURRENT_TO_SWEEPER);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012136
12137 // We are storing the new length using release store after creating a filler
12138 // for the left-over space to avoid races with the sweeper thread.
12139 string->synchronized_set_length(new_length);
12140
12141 if (new_length == 0) return heap->isolate()->factory()->empty_string();
12142 return string;
12143}
12144
12145
Iain Merrick9ac36c92010-09-13 15:29:50 +010012146uint32_t StringHasher::MakeArrayIndexHash(uint32_t value, int length) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +010012147 // For array indexes mix the length into the hash as an array index could
12148 // be zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012149 DCHECK(length > 0);
12150 DCHECK(length <= String::kMaxArrayIndexSize);
12151 DCHECK(TenToThe(String::kMaxCachedArrayIndexLength) <
Kristian Monsen80d68ea2010-09-08 11:05:35 +010012152 (1 << String::kArrayIndexValueBits));
Iain Merrick9ac36c92010-09-13 15:29:50 +010012153
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012154 value <<= String::ArrayIndexValueBits::kShift;
12155 value |= length << String::ArrayIndexLengthBits::kShift;
Iain Merrick9ac36c92010-09-13 15:29:50 +010012156
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012157 DCHECK((value & String::kIsNotArrayIndexMask) == 0);
12158 DCHECK((length > String::kMaxCachedArrayIndexLength) ||
Iain Merrick9ac36c92010-09-13 15:29:50 +010012159 (value & String::kContainsCachedArrayIndexMask) == 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +010012160 return value;
Steve Blocka7e24c12009-10-30 11:49:00 +000012161}
12162
12163
12164uint32_t StringHasher::GetHashField() {
Steve Blockd0582a62009-12-15 09:54:21 +000012165 if (length_ <= String::kMaxHashCalcLength) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012166 if (is_array_index_) {
12167 return MakeArrayIndexHash(array_index_, length_);
Steve Blocka7e24c12009-10-30 11:49:00 +000012168 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012169 return (GetHashCore(raw_running_hash_) << String::kHashShift) |
12170 String::kIsNotArrayIndexMask;
Steve Blocka7e24c12009-10-30 11:49:00 +000012171 } else {
Kristian Monsen80d68ea2010-09-08 11:05:35 +010012172 return (length_ << String::kHashShift) | String::kIsNotArrayIndexMask;
Steve Blocka7e24c12009-10-30 11:49:00 +000012173 }
12174}
12175
12176
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012177uint32_t StringHasher::ComputeUtf8Hash(Vector<const char> chars,
12178 uint32_t seed,
12179 int* utf16_length_out) {
12180 int vector_length = chars.length();
12181 // Handle some edge cases
12182 if (vector_length <= 1) {
12183 DCHECK(vector_length == 0 ||
12184 static_cast<uint8_t>(chars.start()[0]) <=
12185 unibrow::Utf8::kMaxOneByteChar);
12186 *utf16_length_out = vector_length;
12187 return HashSequentialString(chars.start(), vector_length, seed);
Steve Blocka7e24c12009-10-30 11:49:00 +000012188 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012189 // Start with a fake length which won't affect computation.
12190 // It will be updated later.
12191 StringHasher hasher(String::kMaxArrayIndexSize, seed);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012192 size_t remaining = static_cast<size_t>(vector_length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012193 const uint8_t* stream = reinterpret_cast<const uint8_t*>(chars.start());
12194 int utf16_length = 0;
12195 bool is_index = true;
12196 DCHECK(hasher.is_array_index_);
12197 while (remaining > 0) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012198 size_t consumed = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012199 uint32_t c = unibrow::Utf8::ValueOf(stream, remaining, &consumed);
12200 DCHECK(consumed > 0 && consumed <= remaining);
12201 stream += consumed;
12202 remaining -= consumed;
12203 bool is_two_characters = c > unibrow::Utf16::kMaxNonSurrogateCharCode;
12204 utf16_length += is_two_characters ? 2 : 1;
12205 // No need to keep hashing. But we do need to calculate utf16_length.
12206 if (utf16_length > String::kMaxHashCalcLength) continue;
12207 if (is_two_characters) {
12208 uint16_t c1 = unibrow::Utf16::LeadSurrogate(c);
12209 uint16_t c2 = unibrow::Utf16::TrailSurrogate(c);
12210 hasher.AddCharacter(c1);
12211 hasher.AddCharacter(c2);
12212 if (is_index) is_index = hasher.UpdateIndex(c1);
12213 if (is_index) is_index = hasher.UpdateIndex(c2);
12214 } else {
12215 hasher.AddCharacter(c);
12216 if (is_index) is_index = hasher.UpdateIndex(c);
12217 }
Steve Blocka7e24c12009-10-30 11:49:00 +000012218 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012219 *utf16_length_out = static_cast<int>(utf16_length);
12220 // Must set length here so that hash computation is correct.
12221 hasher.length_ = utf16_length;
Steve Blocka7e24c12009-10-30 11:49:00 +000012222 return hasher.GetHashField();
12223}
12224
12225
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012226void IteratingStringHasher::VisitConsString(ConsString* cons_string) {
12227 // Run small ConsStrings through ConsStringIterator.
12228 if (cons_string->length() < 64) {
12229 ConsStringIterator iter(cons_string);
12230 int offset;
12231 String* string;
12232 while (nullptr != (string = iter.Next(&offset))) {
12233 DCHECK_EQ(0, offset);
12234 String::VisitFlat(this, string, 0);
12235 }
12236 return;
12237 }
12238 // Slow case.
12239 const int max_length = String::kMaxHashCalcLength;
12240 int length = std::min(cons_string->length(), max_length);
12241 if (cons_string->HasOnlyOneByteChars()) {
12242 uint8_t* buffer = new uint8_t[length];
12243 String::WriteToFlat(cons_string, buffer, 0, length);
12244 AddCharacters(buffer, length);
12245 delete[] buffer;
12246 } else {
12247 uint16_t* buffer = new uint16_t[length];
12248 String::WriteToFlat(cons_string, buffer, 0, length);
12249 AddCharacters(buffer, length);
12250 delete[] buffer;
12251 }
12252}
12253
12254
Steve Blocka7e24c12009-10-30 11:49:00 +000012255void String::PrintOn(FILE* file) {
12256 int length = this->length();
12257 for (int i = 0; i < length; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012258 PrintF(file, "%c", Get(i));
Steve Blocka7e24c12009-10-30 11:49:00 +000012259 }
12260}
12261
12262
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000012263int Map::Hash() {
12264 // For performance reasons we only hash the 3 most variable fields of a map:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012265 // constructor, prototype and bit_field2. For predictability reasons we
12266 // use objects' offsets in respective pages for hashing instead of raw
12267 // addresses.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000012268
12269 // Shift away the tag.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012270 int hash = ObjectAddressForHashing(GetConstructor()) >> 2;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000012271
12272 // XOR-ing the prototype and constructor directly yields too many zero bits
12273 // when the two pointers are close (which is fairly common).
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012274 // To avoid this we shift the prototype bits relatively to the constructor.
12275 hash ^= ObjectAddressForHashing(prototype()) << (32 - kPageSizeBits);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000012276
12277 return hash ^ (hash >> 16) ^ bit_field2();
12278}
12279
12280
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012281namespace {
12282
12283bool CheckEquivalent(Map* first, Map* second) {
12284 return first->GetConstructor() == second->GetConstructor() &&
12285 first->prototype() == second->prototype() &&
12286 first->instance_type() == second->instance_type() &&
12287 first->bit_field() == second->bit_field() &&
12288 first->is_extensible() == second->is_extensible() &&
Ben Murdoch097c5b22016-05-18 11:27:45 +010012289 first->new_target_is_base() == second->new_target_is_base() &&
12290 first->has_hidden_prototype() == second->has_hidden_prototype();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012291}
12292
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012293} // namespace
12294
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012295
12296bool Map::EquivalentToForTransition(Map* other) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012297 if (!CheckEquivalent(this, other)) return false;
12298 if (instance_type() == JS_FUNCTION_TYPE) {
12299 // JSFunctions require more checks to ensure that sloppy function is
12300 // not equvalent to strict function.
12301 int nof = Min(NumberOfOwnDescriptors(), other->NumberOfOwnDescriptors());
12302 return instance_descriptors()->IsEqualUpTo(other->instance_descriptors(),
12303 nof);
12304 }
12305 return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012306}
12307
12308
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000012309bool Map::EquivalentToForNormalization(Map* other,
12310 PropertyNormalizationMode mode) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012311 int properties =
12312 mode == CLEAR_INOBJECT_PROPERTIES ? 0 : other->GetInObjectProperties();
12313 return CheckEquivalent(this, other) && bit_field2() == other->bit_field2() &&
12314 GetInObjectProperties() == properties;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012315}
12316
12317
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012318bool JSFunction::Inlines(SharedFunctionInfo* candidate) {
12319 DisallowHeapAllocation no_gc;
12320 if (shared() == candidate) return true;
12321 if (code()->kind() != Code::OPTIMIZED_FUNCTION) return false;
12322 DeoptimizationInputData* const data =
12323 DeoptimizationInputData::cast(code()->deoptimization_data());
12324 if (data->length() == 0) return false;
12325 FixedArray* const literals = data->LiteralArray();
12326 int const inlined_count = data->InlinedFunctionCount()->value();
12327 for (int i = 0; i < inlined_count; ++i) {
12328 if (SharedFunctionInfo::cast(literals->get(i)) == candidate) {
12329 return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012330 }
12331 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012332 return false;
Steve Block791712a2010-08-27 10:21:07 +010012333}
12334
12335
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012336void JSFunction::MarkForOptimization() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012337 Isolate* isolate = GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012338 // Do not optimize if function contains break points.
12339 if (shared()->HasDebugInfo()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012340 DCHECK(!IsOptimized());
12341 DCHECK(shared()->allows_lazy_compilation() ||
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012342 !shared()->optimization_disabled());
12343 DCHECK(!shared()->HasDebugInfo());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012344 set_code_no_write_barrier(
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012345 isolate->builtins()->builtin(Builtins::kCompileOptimized));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012346 // No write barrier required, since the builtin is part of the root set.
Ben Murdochb0fe1622011-05-05 13:52:32 +010012347}
12348
12349
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012350void JSFunction::AttemptConcurrentOptimization() {
12351 Isolate* isolate = GetIsolate();
12352 if (!isolate->concurrent_recompilation_enabled() ||
12353 isolate->bootstrapper()->IsActive()) {
12354 MarkForOptimization();
12355 return;
12356 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012357 DCHECK(!IsInOptimizationQueue());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012358 DCHECK(!IsOptimized());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012359 DCHECK(shared()->allows_lazy_compilation() ||
12360 !shared()->optimization_disabled());
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012361 DCHECK(isolate->concurrent_recompilation_enabled());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012362 if (FLAG_trace_concurrent_recompilation) {
12363 PrintF(" ** Marking ");
12364 ShortPrint();
12365 PrintF(" for concurrent recompilation.\n");
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012366 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012367 set_code_no_write_barrier(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012368 isolate->builtins()->builtin(Builtins::kCompileOptimizedConcurrent));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012369 // No write barrier required, since the builtin is part of the root set.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012370}
12371
12372
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012373void SharedFunctionInfo::AddSharedCodeToOptimizedCodeMap(
12374 Handle<SharedFunctionInfo> shared, Handle<Code> code) {
12375 Isolate* isolate = shared->GetIsolate();
12376 if (isolate->serializer_enabled()) return;
12377 DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
12378 // Empty code maps are unsupported.
12379 if (!shared->OptimizedCodeMapIsCleared()) {
12380 Handle<WeakCell> cell = isolate->factory()->NewWeakCell(code);
12381 // A collection may have occured and cleared the optimized code map in the
12382 // allocation above.
12383 if (!shared->OptimizedCodeMapIsCleared()) {
12384 shared->optimized_code_map()->set(kSharedCodeIndex, *cell);
12385 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012386 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012387}
12388
Ben Murdochda12d292016-06-02 14:46:10 +010012389// static
12390void SharedFunctionInfo::AddToOptimizedCodeMap(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012391 Handle<SharedFunctionInfo> shared, Handle<Context> native_context,
Ben Murdochda12d292016-06-02 14:46:10 +010012392 MaybeHandle<Code> code, Handle<LiteralsArray> literals,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012393 BailoutId osr_ast_id) {
12394 Isolate* isolate = shared->GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012395 if (isolate->serializer_enabled()) return;
Ben Murdochda12d292016-06-02 14:46:10 +010012396 DCHECK(code.is_null() ||
12397 code.ToHandleChecked()->kind() == Code::OPTIMIZED_FUNCTION);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012398 DCHECK(native_context->IsNativeContext());
12399 STATIC_ASSERT(kEntryLength == 4);
12400 Handle<FixedArray> new_code_map;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012401 int entry;
12402
12403 if (shared->OptimizedCodeMapIsCleared()) {
12404 new_code_map = isolate->factory()->NewFixedArray(kInitialLength, TENURED);
12405 new_code_map->set(kSharedCodeIndex, *isolate->factory()->empty_weak_cell(),
12406 SKIP_WRITE_BARRIER);
12407 entry = kEntriesStart;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012408 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012409 Handle<FixedArray> old_code_map(shared->optimized_code_map(), isolate);
12410 entry = shared->SearchOptimizedCodeMapEntry(*native_context, osr_ast_id);
12411 if (entry > kSharedCodeIndex) {
Ben Murdochda12d292016-06-02 14:46:10 +010012412 // Just set the code and literals of the entry.
12413 if (!code.is_null()) {
12414 Handle<WeakCell> code_cell =
12415 isolate->factory()->NewWeakCell(code.ToHandleChecked());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012416 old_code_map->set(entry + kCachedCodeOffset, *code_cell);
12417 }
12418 Handle<WeakCell> literals_cell =
12419 isolate->factory()->NewWeakCell(literals);
12420 old_code_map->set(entry + kLiteralsOffset, *literals_cell);
12421 return;
12422 }
12423
12424 // Can we reuse an entry?
12425 DCHECK(entry < kEntriesStart);
12426 int length = old_code_map->length();
12427 for (int i = kEntriesStart; i < length; i += kEntryLength) {
12428 if (WeakCell::cast(old_code_map->get(i + kContextOffset))->cleared()) {
12429 new_code_map = old_code_map;
12430 entry = i;
12431 break;
12432 }
12433 }
12434
12435 if (entry < kEntriesStart) {
12436 // Copy old optimized code map and append one new entry.
12437 new_code_map = isolate->factory()->CopyFixedArrayAndGrow(
12438 old_code_map, kEntryLength, TENURED);
12439 // TODO(mstarzinger): Temporary workaround. The allocation above might
12440 // have flushed the optimized code map and the copy we created is full of
12441 // holes. For now we just give up on adding the entry and pretend it got
12442 // flushed.
12443 if (shared->OptimizedCodeMapIsCleared()) return;
12444 entry = old_code_map->length();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012445 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012446 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012447
Ben Murdochda12d292016-06-02 14:46:10 +010012448 Handle<WeakCell> code_cell =
12449 code.is_null() ? isolate->factory()->empty_weak_cell()
12450 : isolate->factory()->NewWeakCell(code.ToHandleChecked());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012451 Handle<WeakCell> literals_cell = isolate->factory()->NewWeakCell(literals);
12452 WeakCell* context_cell = native_context->self_weak_cell();
12453
12454 new_code_map->set(entry + kContextOffset, context_cell);
12455 new_code_map->set(entry + kCachedCodeOffset, *code_cell);
12456 new_code_map->set(entry + kLiteralsOffset, *literals_cell);
12457 new_code_map->set(entry + kOsrAstIdOffset, Smi::FromInt(osr_ast_id.ToInt()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012458
12459#ifdef DEBUG
12460 for (int i = kEntriesStart; i < new_code_map->length(); i += kEntryLength) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012461 WeakCell* cell = WeakCell::cast(new_code_map->get(i + kContextOffset));
12462 DCHECK(cell->cleared() || cell->value()->IsNativeContext());
12463 cell = WeakCell::cast(new_code_map->get(i + kCachedCodeOffset));
12464 DCHECK(cell->cleared() ||
12465 (cell->value()->IsCode() &&
12466 Code::cast(cell->value())->kind() == Code::OPTIMIZED_FUNCTION));
12467 cell = WeakCell::cast(new_code_map->get(i + kLiteralsOffset));
12468 DCHECK(cell->cleared() || cell->value()->IsFixedArray());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012469 DCHECK(new_code_map->get(i + kOsrAstIdOffset)->IsSmi());
12470 }
12471#endif
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012472
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012473 FixedArray* old_code_map = shared->optimized_code_map();
12474 if (old_code_map != *new_code_map) {
12475 shared->set_optimized_code_map(*new_code_map);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012476 }
Ben Murdochb0fe1622011-05-05 13:52:32 +010012477}
12478
12479
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012480void SharedFunctionInfo::ClearOptimizedCodeMap() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012481 FixedArray* cleared_map = GetHeap()->cleared_optimized_code_map();
12482 set_optimized_code_map(cleared_map, SKIP_WRITE_BARRIER);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012483}
12484
12485
12486void SharedFunctionInfo::EvictFromOptimizedCodeMap(Code* optimized_code,
12487 const char* reason) {
12488 DisallowHeapAllocation no_gc;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012489 if (OptimizedCodeMapIsCleared()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012490
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012491 Heap* heap = GetHeap();
12492 FixedArray* code_map = optimized_code_map();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012493 int dst = kEntriesStart;
12494 int length = code_map->length();
12495 for (int src = kEntriesStart; src < length; src += kEntryLength) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012496 DCHECK(WeakCell::cast(code_map->get(src))->cleared() ||
12497 WeakCell::cast(code_map->get(src))->value()->IsNativeContext());
12498 if (WeakCell::cast(code_map->get(src + kCachedCodeOffset))->value() ==
12499 optimized_code) {
12500 BailoutId osr(Smi::cast(code_map->get(src + kOsrAstIdOffset))->value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012501 if (FLAG_trace_opt) {
12502 PrintF("[evicting entry from optimizing code map (%s) for ", reason);
12503 ShortPrint();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012504 if (osr.IsNone()) {
12505 PrintF("]\n");
12506 } else {
12507 PrintF(" (osr ast id %d)]\n", osr.ToInt());
12508 }
12509 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012510 if (!osr.IsNone()) {
12511 // Evict the src entry by not copying it to the dst entry.
12512 continue;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012513 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012514 // In case of non-OSR entry just clear the code in order to proceed
12515 // sharing literals.
12516 code_map->set(src + kCachedCodeOffset, heap->empty_weak_cell(),
12517 SKIP_WRITE_BARRIER);
12518 }
12519
12520 // Keep the src entry by copying it to the dst entry.
12521 if (dst != src) {
12522 code_map->set(dst + kContextOffset, code_map->get(src + kContextOffset));
12523 code_map->set(dst + kCachedCodeOffset,
12524 code_map->get(src + kCachedCodeOffset));
12525 code_map->set(dst + kLiteralsOffset,
12526 code_map->get(src + kLiteralsOffset));
12527 code_map->set(dst + kOsrAstIdOffset,
12528 code_map->get(src + kOsrAstIdOffset));
12529 }
12530 dst += kEntryLength;
12531 }
12532 if (WeakCell::cast(code_map->get(kSharedCodeIndex))->value() ==
12533 optimized_code) {
12534 // Evict context-independent code as well.
12535 code_map->set(kSharedCodeIndex, heap->empty_weak_cell(),
12536 SKIP_WRITE_BARRIER);
12537 if (FLAG_trace_opt) {
12538 PrintF("[evicting entry from optimizing code map (%s) for ", reason);
12539 ShortPrint();
12540 PrintF(" (context-independent code)]\n");
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012541 }
12542 }
12543 if (dst != length) {
12544 // Always trim even when array is cleared because of heap verifier.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012545 heap->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>(code_map,
12546 length - dst);
12547 if (code_map->length() == kEntriesStart &&
12548 WeakCell::cast(code_map->get(kSharedCodeIndex))->cleared()) {
12549 ClearOptimizedCodeMap();
12550 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012551 }
12552}
12553
12554
12555void SharedFunctionInfo::TrimOptimizedCodeMap(int shrink_by) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012556 FixedArray* code_map = optimized_code_map();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012557 DCHECK(shrink_by % kEntryLength == 0);
12558 DCHECK(shrink_by <= code_map->length() - kEntriesStart);
12559 // Always trim even when array is cleared because of heap verifier.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012560 GetHeap()->RightTrimFixedArray<Heap::SEQUENTIAL_TO_SWEEPER>(code_map,
12561 shrink_by);
12562 if (code_map->length() == kEntriesStart &&
12563 WeakCell::cast(code_map->get(kSharedCodeIndex))->cleared()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012564 ClearOptimizedCodeMap();
12565 }
12566}
12567
12568
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012569static void GetMinInobjectSlack(Map* map, void* data) {
12570 int slack = map->unused_property_fields();
12571 if (*reinterpret_cast<int*>(data) > slack) {
12572 *reinterpret_cast<int*>(data) = slack;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012573 }
12574}
12575
12576
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012577static void ShrinkInstanceSize(Map* map, void* data) {
12578 int slack = *reinterpret_cast<int*>(data);
12579 map->SetInObjectProperties(map->GetInObjectProperties() - slack);
12580 map->set_unused_property_fields(map->unused_property_fields() - slack);
12581 map->set_instance_size(map->instance_size() - slack * kPointerSize);
Ben Murdoch097c5b22016-05-18 11:27:45 +010012582 map->set_construction_counter(Map::kNoSlackTracking);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012583
12584 // Visitor id might depend on the instance size, recalculate it.
12585 map->set_visitor_id(Heap::GetStaticVisitorIdForMap(map));
12586}
12587
Ben Murdoch097c5b22016-05-18 11:27:45 +010012588static void StopSlackTracking(Map* map, void* data) {
12589 map->set_construction_counter(Map::kNoSlackTracking);
12590}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012591
12592void Map::CompleteInobjectSlackTracking() {
12593 // Has to be an initial map.
12594 DCHECK(GetBackPointer()->IsUndefined());
12595
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012596 int slack = unused_property_fields();
12597 TransitionArray::TraverseTransitionTree(this, &GetMinInobjectSlack, &slack);
12598 if (slack != 0) {
12599 // Resize the initial map and all maps in its transition tree.
12600 TransitionArray::TraverseTransitionTree(this, &ShrinkInstanceSize, &slack);
Ben Murdoch097c5b22016-05-18 11:27:45 +010012601 } else {
12602 TransitionArray::TraverseTransitionTree(this, &StopSlackTracking, nullptr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012603 }
12604}
12605
12606
12607static bool PrototypeBenefitsFromNormalization(Handle<JSObject> object) {
12608 DisallowHeapAllocation no_gc;
12609 if (!object->HasFastProperties()) return false;
12610 Map* map = object->map();
12611 if (map->is_prototype_map()) return false;
12612 DescriptorArray* descriptors = map->instance_descriptors();
12613 for (int i = 0; i < map->NumberOfOwnDescriptors(); i++) {
12614 PropertyDetails details = descriptors->GetDetails(i);
12615 if (details.location() == kDescriptor) continue;
12616 if (details.representation().IsHeapObject() ||
12617 details.representation().IsTagged()) {
12618 FieldIndex index = FieldIndex::ForDescriptor(map, i);
12619 if (object->RawFastPropertyAt(index)->IsJSFunction()) return true;
12620 }
12621 }
12622 return false;
12623}
12624
12625
12626// static
12627void JSObject::OptimizeAsPrototype(Handle<JSObject> object,
12628 PrototypeOptimizationMode mode) {
12629 if (object->IsJSGlobalObject()) return;
12630 if (mode == FAST_PROTOTYPE && PrototypeBenefitsFromNormalization(object)) {
12631 // First normalize to ensure all JSFunctions are DATA_CONSTANT.
12632 JSObject::NormalizeProperties(object, KEEP_INOBJECT_PROPERTIES, 0,
12633 "NormalizeAsPrototype");
12634 }
12635 Handle<Map> previous_map(object->map());
12636 if (!object->HasFastProperties()) {
12637 JSObject::MigrateSlowToFast(object, 0, "OptimizeAsPrototype");
12638 }
12639 if (!object->map()->is_prototype_map()) {
12640 if (object->map() == *previous_map) {
12641 Handle<Map> new_map = Map::Copy(handle(object->map()), "CopyAsPrototype");
12642 JSObject::MigrateToMap(object, new_map);
12643 }
12644 object->map()->set_is_prototype_map(true);
12645
12646 // Replace the pointer to the exact constructor with the Object function
12647 // from the same context if undetectable from JS. This is to avoid keeping
12648 // memory alive unnecessarily.
12649 Object* maybe_constructor = object->map()->GetConstructor();
12650 if (maybe_constructor->IsJSFunction()) {
12651 JSFunction* constructor = JSFunction::cast(maybe_constructor);
12652 Isolate* isolate = object->GetIsolate();
12653 if (!constructor->shared()->IsApiFunction() &&
12654 object->class_name() == isolate->heap()->Object_string()) {
12655 Context* context = constructor->context()->native_context();
12656 JSFunction* object_function = context->object_function();
12657 object->map()->SetConstructor(object_function);
12658 }
12659 }
12660 }
12661}
12662
12663
12664// static
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012665void JSObject::ReoptimizeIfPrototype(Handle<JSObject> object) {
12666 if (!object->map()->is_prototype_map()) return;
12667 OptimizeAsPrototype(object, FAST_PROTOTYPE);
12668}
12669
12670
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012671// static
12672void JSObject::LazyRegisterPrototypeUser(Handle<Map> user, Isolate* isolate) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012673 DCHECK(FLAG_track_prototype_users);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012674 // Contract: In line with InvalidatePrototypeChains()'s requirements,
12675 // leaf maps don't need to register as users, only prototypes do.
12676 DCHECK(user->is_prototype_map());
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012677
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012678 Handle<Map> current_user = user;
12679 Handle<PrototypeInfo> current_user_info =
12680 Map::GetOrCreatePrototypeInfo(user, isolate);
12681 for (PrototypeIterator iter(user); !iter.IsAtEnd(); iter.Advance()) {
12682 // Walk up the prototype chain as far as links haven't been registered yet.
12683 if (current_user_info->registry_slot() != PrototypeInfo::UNREGISTERED) {
12684 break;
12685 }
12686 Handle<Object> maybe_proto = PrototypeIterator::GetCurrent(iter);
12687 // Proxies on the prototype chain are not supported. They make it
12688 // impossible to make any assumptions about the prototype chain anyway.
12689 if (maybe_proto->IsJSProxy()) return;
12690 Handle<JSObject> proto = Handle<JSObject>::cast(maybe_proto);
12691 Handle<PrototypeInfo> proto_info =
12692 Map::GetOrCreatePrototypeInfo(proto, isolate);
12693 Handle<Object> maybe_registry(proto_info->prototype_users(), isolate);
12694 int slot = 0;
12695 Handle<WeakFixedArray> new_array =
12696 WeakFixedArray::Add(maybe_registry, current_user, &slot);
12697 current_user_info->set_registry_slot(slot);
12698 if (!maybe_registry.is_identical_to(new_array)) {
12699 proto_info->set_prototype_users(*new_array);
12700 }
12701 if (FLAG_trace_prototype_users) {
12702 PrintF("Registering %p as a user of prototype %p (map=%p).\n",
12703 reinterpret_cast<void*>(*current_user),
12704 reinterpret_cast<void*>(*proto),
12705 reinterpret_cast<void*>(proto->map()));
12706 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012707
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012708 current_user = handle(proto->map(), isolate);
12709 current_user_info = proto_info;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012710 }
12711}
12712
12713
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012714// Can be called regardless of whether |user| was actually registered with
12715// |prototype|. Returns true when there was a registration.
12716// static
12717bool JSObject::UnregisterPrototypeUser(Handle<Map> user, Isolate* isolate) {
12718 DCHECK(user->is_prototype_map());
12719 // If it doesn't have a PrototypeInfo, it was never registered.
12720 if (!user->prototype_info()->IsPrototypeInfo()) return false;
12721 // If it had no prototype before, see if it had users that might expect
12722 // registration.
12723 if (!user->prototype()->IsJSObject()) {
12724 Object* users =
12725 PrototypeInfo::cast(user->prototype_info())->prototype_users();
12726 return users->IsWeakFixedArray();
12727 }
12728 Handle<JSObject> prototype(JSObject::cast(user->prototype()), isolate);
12729 Handle<PrototypeInfo> user_info =
12730 Map::GetOrCreatePrototypeInfo(user, isolate);
12731 int slot = user_info->registry_slot();
12732 if (slot == PrototypeInfo::UNREGISTERED) return false;
12733 DCHECK(prototype->map()->is_prototype_map());
12734 Object* maybe_proto_info = prototype->map()->prototype_info();
12735 // User knows its registry slot, prototype info and user registry must exist.
12736 DCHECK(maybe_proto_info->IsPrototypeInfo());
12737 Handle<PrototypeInfo> proto_info(PrototypeInfo::cast(maybe_proto_info),
12738 isolate);
12739 Object* maybe_registry = proto_info->prototype_users();
12740 DCHECK(maybe_registry->IsWeakFixedArray());
12741 DCHECK(WeakFixedArray::cast(maybe_registry)->Get(slot) == *user);
12742 WeakFixedArray::cast(maybe_registry)->Clear(slot);
12743 if (FLAG_trace_prototype_users) {
12744 PrintF("Unregistering %p as a user of prototype %p.\n",
12745 reinterpret_cast<void*>(*user), reinterpret_cast<void*>(*prototype));
12746 }
12747 return true;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012748}
12749
12750
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012751static void InvalidatePrototypeChainsInternal(Map* map) {
12752 if (!map->is_prototype_map()) return;
12753 if (FLAG_trace_prototype_users) {
12754 PrintF("Invalidating prototype map %p 's cell\n",
12755 reinterpret_cast<void*>(map));
12756 }
12757 Object* maybe_proto_info = map->prototype_info();
12758 if (!maybe_proto_info->IsPrototypeInfo()) return;
12759 PrototypeInfo* proto_info = PrototypeInfo::cast(maybe_proto_info);
12760 Object* maybe_cell = proto_info->validity_cell();
12761 if (maybe_cell->IsCell()) {
12762 // Just set the value; the cell will be replaced lazily.
12763 Cell* cell = Cell::cast(maybe_cell);
12764 cell->set_value(Smi::FromInt(Map::kPrototypeChainInvalid));
12765 }
12766
12767 WeakFixedArray::Iterator iterator(proto_info->prototype_users());
12768 // For now, only maps register themselves as users.
12769 Map* user;
12770 while ((user = iterator.Next<Map>())) {
12771 // Walk the prototype chain (backwards, towards leaf objects) if necessary.
12772 InvalidatePrototypeChainsInternal(user);
12773 }
12774}
12775
12776
12777// static
12778void JSObject::InvalidatePrototypeChains(Map* map) {
12779 if (!FLAG_eliminate_prototype_chain_checks) return;
12780 DisallowHeapAllocation no_gc;
12781 InvalidatePrototypeChainsInternal(map);
12782}
12783
12784
12785// static
12786Handle<PrototypeInfo> Map::GetOrCreatePrototypeInfo(Handle<JSObject> prototype,
12787 Isolate* isolate) {
12788 Object* maybe_proto_info = prototype->map()->prototype_info();
12789 if (maybe_proto_info->IsPrototypeInfo()) {
12790 return handle(PrototypeInfo::cast(maybe_proto_info), isolate);
12791 }
12792 Handle<PrototypeInfo> proto_info = isolate->factory()->NewPrototypeInfo();
12793 prototype->map()->set_prototype_info(*proto_info);
12794 return proto_info;
12795}
12796
12797
12798// static
12799Handle<PrototypeInfo> Map::GetOrCreatePrototypeInfo(Handle<Map> prototype_map,
12800 Isolate* isolate) {
12801 Object* maybe_proto_info = prototype_map->prototype_info();
12802 if (maybe_proto_info->IsPrototypeInfo()) {
12803 return handle(PrototypeInfo::cast(maybe_proto_info), isolate);
12804 }
12805 Handle<PrototypeInfo> proto_info = isolate->factory()->NewPrototypeInfo();
12806 prototype_map->set_prototype_info(*proto_info);
12807 return proto_info;
12808}
12809
12810
12811// static
12812Handle<Cell> Map::GetOrCreatePrototypeChainValidityCell(Handle<Map> map,
12813 Isolate* isolate) {
12814 Handle<Object> maybe_prototype(map->prototype(), isolate);
12815 if (!maybe_prototype->IsJSObject()) return Handle<Cell>::null();
12816 Handle<JSObject> prototype = Handle<JSObject>::cast(maybe_prototype);
12817 // Ensure the prototype is registered with its own prototypes so its cell
12818 // will be invalidated when necessary.
12819 JSObject::LazyRegisterPrototypeUser(handle(prototype->map(), isolate),
12820 isolate);
12821 Handle<PrototypeInfo> proto_info =
12822 GetOrCreatePrototypeInfo(prototype, isolate);
12823 Object* maybe_cell = proto_info->validity_cell();
12824 // Return existing cell if it's still valid.
12825 if (maybe_cell->IsCell()) {
12826 Handle<Cell> cell(Cell::cast(maybe_cell), isolate);
12827 if (cell->value() == Smi::FromInt(Map::kPrototypeChainValid)) {
12828 return cell;
12829 }
12830 }
12831 // Otherwise create a new cell.
12832 Handle<Cell> cell = isolate->factory()->NewCell(
12833 handle(Smi::FromInt(Map::kPrototypeChainValid), isolate));
12834 proto_info->set_validity_cell(*cell);
12835 return cell;
12836}
12837
12838
12839// static
12840void Map::SetPrototype(Handle<Map> map, Handle<Object> prototype,
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012841 PrototypeOptimizationMode proto_mode) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010012842 bool is_hidden = false;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012843 if (prototype->IsJSObject()) {
12844 Handle<JSObject> prototype_jsobj = Handle<JSObject>::cast(prototype);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012845 JSObject::OptimizeAsPrototype(prototype_jsobj, proto_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +010012846
12847 Object* maybe_constructor = prototype_jsobj->map()->GetConstructor();
12848 if (maybe_constructor->IsJSFunction()) {
12849 JSFunction* constructor = JSFunction::cast(maybe_constructor);
12850 Object* data = constructor->shared()->function_data();
12851 is_hidden = (data->IsFunctionTemplateInfo() &&
12852 FunctionTemplateInfo::cast(data)->hidden_prototype()) ||
12853 prototype->IsJSGlobalObject();
12854 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012855 }
Ben Murdoch097c5b22016-05-18 11:27:45 +010012856 map->set_has_hidden_prototype(is_hidden);
12857
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012858 WriteBarrierMode wb_mode =
12859 prototype->IsNull() ? SKIP_WRITE_BARRIER : UPDATE_WRITE_BARRIER;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012860 map->set_prototype(*prototype, wb_mode);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012861}
12862
12863
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012864Handle<Object> CacheInitialJSArrayMaps(
12865 Handle<Context> native_context, Handle<Map> initial_map) {
12866 // Replace all of the cached initial array maps in the native context with
12867 // the appropriate transitioned elements kind maps.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012868 Handle<Map> current_map = initial_map;
12869 ElementsKind kind = current_map->elements_kind();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012870 DCHECK_EQ(GetInitialFastElementsKind(), kind);
Ben Murdochda12d292016-06-02 14:46:10 +010012871 native_context->set(Context::ArrayMapIndex(kind), *current_map);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012872 for (int i = GetSequenceIndexFromFastElementsKind(kind) + 1;
12873 i < kFastElementsKindCount; ++i) {
12874 Handle<Map> new_map;
12875 ElementsKind next_kind = GetFastElementsKindFromSequenceIndex(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012876 if (Map* maybe_elements_transition = current_map->ElementsTransitionMap()) {
12877 new_map = handle(maybe_elements_transition);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012878 } else {
12879 new_map = Map::CopyAsElementsKind(
12880 current_map, next_kind, INSERT_TRANSITION);
12881 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012882 DCHECK_EQ(next_kind, new_map->elements_kind());
Ben Murdochda12d292016-06-02 14:46:10 +010012883 native_context->set(Context::ArrayMapIndex(next_kind), *new_map);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012884 current_map = new_map;
12885 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012886 return initial_map;
12887}
12888
12889
12890void JSFunction::SetInstancePrototype(Handle<JSFunction> function,
12891 Handle<Object> value) {
12892 Isolate* isolate = function->GetIsolate();
12893
12894 DCHECK(value->IsJSReceiver());
12895
12896 // Now some logic for the maps of the objects that are created by using this
12897 // function as a constructor.
12898 if (function->has_initial_map()) {
12899 // If the function has allocated the initial map replace it with a
12900 // copy containing the new prototype. Also complete any in-object
12901 // slack tracking that is in progress at this point because it is
12902 // still tracking the old copy.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012903 function->CompleteInobjectSlackTrackingIfActive();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012904
12905 Handle<Map> initial_map(function->initial_map(), isolate);
12906
12907 if (!initial_map->GetIsolate()->bootstrapper()->IsActive() &&
12908 initial_map->instance_type() == JS_OBJECT_TYPE) {
12909 // Put the value in the initial map field until an initial map is needed.
12910 // At that point, a new initial map is created and the prototype is put
12911 // into the initial map where it belongs.
12912 function->set_prototype_or_initial_map(*value);
12913 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012914 Handle<Map> new_map = Map::Copy(initial_map, "SetInstancePrototype");
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012915 JSFunction::SetInitialMap(function, new_map, value);
12916
12917 // If the function is used as the global Array function, cache the
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012918 // updated initial maps (and transitioned versions) in the native context.
12919 Handle<Context> native_context(function->context()->native_context(),
12920 isolate);
12921 Handle<Object> array_function(
12922 native_context->get(Context::ARRAY_FUNCTION_INDEX), isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012923 if (array_function->IsJSFunction() &&
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012924 *function == JSFunction::cast(*array_function)) {
12925 CacheInitialJSArrayMaps(native_context, new_map);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012926 }
12927 }
12928
12929 // Deoptimize all code that embeds the previous initial map.
12930 initial_map->dependent_code()->DeoptimizeDependentCodeGroup(
12931 isolate, DependentCode::kInitialMapChangedGroup);
Steve Blocka7e24c12009-10-30 11:49:00 +000012932 } else {
12933 // Put the value in the initial map field until an initial map is
12934 // needed. At that point, a new initial map is created and the
12935 // prototype is put into the initial map where it belongs.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012936 function->set_prototype_or_initial_map(*value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012937 if (value->IsJSObject()) {
12938 // Optimize as prototype to detach it from its transition tree.
12939 JSObject::OptimizeAsPrototype(Handle<JSObject>::cast(value),
12940 FAST_PROTOTYPE);
12941 }
Steve Blocka7e24c12009-10-30 11:49:00 +000012942 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012943 isolate->heap()->ClearInstanceofCache();
Steve Blocka7e24c12009-10-30 11:49:00 +000012944}
12945
12946
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012947void JSFunction::SetPrototype(Handle<JSFunction> function,
12948 Handle<Object> value) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010012949 DCHECK(function->IsConstructor() ||
12950 IsGeneratorFunction(function->shared()->kind()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012951 Handle<Object> construct_prototype = value;
Steve Blocka7e24c12009-10-30 11:49:00 +000012952
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012953 // If the value is not a JSReceiver, store the value in the map's
Steve Blocka7e24c12009-10-30 11:49:00 +000012954 // constructor field so it can be accessed. Also, set the prototype
12955 // used for constructing objects to the original object prototype.
12956 // See ECMA-262 13.2.2.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012957 if (!value->IsJSReceiver()) {
Steve Blocka7e24c12009-10-30 11:49:00 +000012958 // Copy the map so this does not affect unrelated functions.
12959 // Remove map transitions because they point to maps with a
12960 // different prototype.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012961 Handle<Map> new_map = Map::Copy(handle(function->map()), "SetPrototype");
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012962
12963 JSObject::MigrateToMap(function, new_map);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012964 new_map->SetConstructor(*value);
Ben Murdoch8b112d22011-06-08 16:22:53 +010012965 new_map->set_non_instance_prototype(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012966 Isolate* isolate = new_map->GetIsolate();
12967 construct_prototype = handle(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012968 function->context()->native_context()->initial_object_prototype(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012969 isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +000012970 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012971 function->map()->set_non_instance_prototype(false);
Steve Blocka7e24c12009-10-30 11:49:00 +000012972 }
12973
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012974 return SetInstancePrototype(function, construct_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +000012975}
12976
12977
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012978bool JSFunction::RemovePrototype() {
12979 Context* native_context = context()->native_context();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012980 Map* no_prototype_map =
12981 is_strict(shared()->language_mode())
12982 ? native_context->strict_function_without_prototype_map()
12983 : native_context->sloppy_function_without_prototype_map();
Steve Block44f0eee2011-05-26 01:26:41 +010012984
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012985 if (map() == no_prototype_map) return true;
12986
12987#ifdef DEBUG
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012988 if (map() != (is_strict(shared()->language_mode())
12989 ? native_context->strict_function_map()
12990 : native_context->sloppy_function_map())) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012991 return false;
Ben Murdoche0cee9b2011-05-25 10:26:03 +010012992 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012993#endif
Steve Block44f0eee2011-05-26 01:26:41 +010012994
12995 set_map(no_prototype_map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012996 set_prototype_or_initial_map(no_prototype_map->GetHeap()->the_hole_value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012997 return true;
Steve Block6ded16b2010-05-10 14:33:55 +010012998}
12999
13000
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013001void JSFunction::SetInitialMap(Handle<JSFunction> function, Handle<Map> map,
13002 Handle<Object> prototype) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013003 if (map->prototype() != *prototype) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013004 Map::SetPrototype(map, prototype, FAST_PROTOTYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013005 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013006 function->set_prototype_or_initial_map(*map);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013007 map->SetConstructor(*function);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013008#if TRACE_MAPS
13009 if (FLAG_trace_maps) {
13010 PrintF("[TraceMaps: InitialMap map= %p SFI= %d_%s ]\n",
13011 reinterpret_cast<void*>(*map), function->shared()->unique_id(),
13012 function->shared()->DebugName()->ToCString().get());
13013 }
13014#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013015}
13016
13017
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013018#ifdef DEBUG
13019namespace {
13020
13021bool CanSubclassHaveInobjectProperties(InstanceType instance_type) {
13022 switch (instance_type) {
13023 case JS_OBJECT_TYPE:
13024 case JS_CONTEXT_EXTENSION_OBJECT_TYPE:
13025 case JS_GENERATOR_OBJECT_TYPE:
13026 case JS_MODULE_TYPE:
13027 case JS_VALUE_TYPE:
13028 case JS_DATE_TYPE:
13029 case JS_ARRAY_TYPE:
13030 case JS_MESSAGE_OBJECT_TYPE:
13031 case JS_ARRAY_BUFFER_TYPE:
13032 case JS_TYPED_ARRAY_TYPE:
13033 case JS_DATA_VIEW_TYPE:
13034 case JS_SET_TYPE:
13035 case JS_MAP_TYPE:
13036 case JS_SET_ITERATOR_TYPE:
13037 case JS_MAP_ITERATOR_TYPE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013038 case JS_WEAK_MAP_TYPE:
13039 case JS_WEAK_SET_TYPE:
13040 case JS_PROMISE_TYPE:
13041 case JS_REGEXP_TYPE:
13042 case JS_FUNCTION_TYPE:
13043 return true;
13044
13045 case JS_BOUND_FUNCTION_TYPE:
13046 case JS_PROXY_TYPE:
13047 case JS_GLOBAL_PROXY_TYPE:
13048 case JS_GLOBAL_OBJECT_TYPE:
13049 case FIXED_ARRAY_TYPE:
13050 case FIXED_DOUBLE_ARRAY_TYPE:
13051 case ODDBALL_TYPE:
13052 case FOREIGN_TYPE:
13053 case MAP_TYPE:
13054 case CODE_TYPE:
13055 case CELL_TYPE:
13056 case PROPERTY_CELL_TYPE:
13057 case WEAK_CELL_TYPE:
13058 case SYMBOL_TYPE:
13059 case BYTECODE_ARRAY_TYPE:
13060 case HEAP_NUMBER_TYPE:
13061 case MUTABLE_HEAP_NUMBER_TYPE:
13062 case SIMD128_VALUE_TYPE:
13063 case FILLER_TYPE:
13064 case BYTE_ARRAY_TYPE:
13065 case FREE_SPACE_TYPE:
13066 case SHARED_FUNCTION_INFO_TYPE:
13067
13068#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
13069 case FIXED_##TYPE##_ARRAY_TYPE:
13070#undef TYPED_ARRAY_CASE
13071
13072#define MAKE_STRUCT_CASE(NAME, Name, name) case NAME##_TYPE:
13073 STRUCT_LIST(MAKE_STRUCT_CASE)
13074#undef MAKE_STRUCT_CASE
13075 // We must not end up here for these instance types at all.
13076 UNREACHABLE();
13077 // Fall through.
13078 default:
13079 return false;
13080 }
13081}
13082
13083} // namespace
13084#endif
13085
13086
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013087void JSFunction::EnsureHasInitialMap(Handle<JSFunction> function) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013088 DCHECK(function->IsConstructor() || function->shared()->is_generator());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013089 if (function->has_initial_map()) return;
13090 Isolate* isolate = function->GetIsolate();
13091
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013092 // The constructor should be compiled for the optimization hints to be
13093 // available.
Ben Murdochda12d292016-06-02 14:46:10 +010013094 Compiler::Compile(function, Compiler::CLEAR_EXCEPTION);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013095
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013096 // First create a new map with the size and number of in-object properties
13097 // suggested by the function.
13098 InstanceType instance_type;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013099 if (function->shared()->is_generator()) {
13100 instance_type = JS_GENERATOR_OBJECT_TYPE;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013101 } else {
13102 instance_type = JS_OBJECT_TYPE;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013103 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013104 int instance_size;
13105 int in_object_properties;
13106 function->CalculateInstanceSize(instance_type, 0, &instance_size,
13107 &in_object_properties);
13108
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013109 Handle<Map> map = isolate->factory()->NewMap(instance_type, instance_size);
13110
13111 // Fetch or allocate prototype.
13112 Handle<Object> prototype;
13113 if (function->has_instance_prototype()) {
13114 prototype = handle(function->instance_prototype(), isolate);
13115 } else {
13116 prototype = isolate->factory()->NewFunctionPrototype(function);
13117 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013118 map->SetInObjectProperties(in_object_properties);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013119 map->set_unused_property_fields(in_object_properties);
13120 DCHECK(map->has_fast_object_elements());
13121
13122 // Finally link initial map and constructor function.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013123 DCHECK(prototype->IsJSReceiver());
13124 JSFunction::SetInitialMap(function, map, prototype);
13125 map->StartInobjectSlackTracking();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013126}
13127
13128
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013129// static
13130MaybeHandle<Map> JSFunction::GetDerivedMap(Isolate* isolate,
13131 Handle<JSFunction> constructor,
13132 Handle<JSReceiver> new_target) {
13133 EnsureHasInitialMap(constructor);
13134
13135 Handle<Map> constructor_initial_map(constructor->initial_map(), isolate);
13136 if (*new_target == *constructor) return constructor_initial_map;
13137
13138 // Fast case, new.target is a subclass of constructor. The map is cacheable
13139 // (and may already have been cached). new.target.prototype is guaranteed to
13140 // be a JSReceiver.
13141 if (new_target->IsJSFunction()) {
13142 Handle<JSFunction> function = Handle<JSFunction>::cast(new_target);
13143
13144 // Check that |function|'s initial map still in sync with the |constructor|,
13145 // otherwise we must create a new initial map for |function|.
13146 if (function->has_initial_map() &&
13147 function->initial_map()->GetConstructor() == *constructor) {
13148 return handle(function->initial_map(), isolate);
13149 }
13150
13151 // Create a new map with the size and number of in-object properties
13152 // suggested by |function|.
13153
13154 // Link initial map and constructor function if the new.target is actually a
13155 // subclass constructor.
13156 if (IsSubclassConstructor(function->shared()->kind())) {
13157 Handle<Object> prototype(function->instance_prototype(), isolate);
13158 InstanceType instance_type = constructor_initial_map->instance_type();
13159 DCHECK(CanSubclassHaveInobjectProperties(instance_type));
13160 int internal_fields =
13161 JSObject::GetInternalFieldCount(*constructor_initial_map);
13162 int pre_allocated = constructor_initial_map->GetInObjectProperties() -
13163 constructor_initial_map->unused_property_fields();
13164 int instance_size;
13165 int in_object_properties;
13166 function->CalculateInstanceSizeForDerivedClass(
13167 instance_type, internal_fields, &instance_size,
13168 &in_object_properties);
13169
13170 int unused_property_fields = in_object_properties - pre_allocated;
13171 Handle<Map> map =
13172 Map::CopyInitialMap(constructor_initial_map, instance_size,
13173 in_object_properties, unused_property_fields);
13174 map->set_new_target_is_base(false);
13175
13176 JSFunction::SetInitialMap(function, map, prototype);
13177 map->SetConstructor(*constructor);
Ben Murdoch097c5b22016-05-18 11:27:45 +010013178 map->set_construction_counter(Map::kNoSlackTracking);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013179 map->StartInobjectSlackTracking();
13180 return map;
13181 }
13182 }
13183
13184 // Slow path, new.target is either a proxy or can't cache the map.
13185 // new.target.prototype is not guaranteed to be a JSReceiver, and may need to
13186 // fall back to the intrinsicDefaultProto.
13187 Handle<Object> prototype;
13188 if (new_target->IsJSFunction()) {
13189 Handle<JSFunction> function = Handle<JSFunction>::cast(new_target);
13190 // Make sure the new.target.prototype is cached.
13191 EnsureHasInitialMap(function);
13192 prototype = handle(function->prototype(), isolate);
13193 } else {
13194 Handle<String> prototype_string = isolate->factory()->prototype_string();
13195 ASSIGN_RETURN_ON_EXCEPTION(
13196 isolate, prototype,
13197 JSReceiver::GetProperty(new_target, prototype_string), Map);
13198 // The above prototype lookup might change the constructor and its
13199 // prototype, hence we have to reload the initial map.
13200 EnsureHasInitialMap(constructor);
13201 constructor_initial_map = handle(constructor->initial_map(), isolate);
13202 }
13203
13204 // If prototype is not a JSReceiver, fetch the intrinsicDefaultProto from the
13205 // correct realm. Rather than directly fetching the .prototype, we fetch the
13206 // constructor that points to the .prototype. This relies on
13207 // constructor.prototype being FROZEN for those constructors.
13208 if (!prototype->IsJSReceiver()) {
13209 Handle<Context> context;
13210 ASSIGN_RETURN_ON_EXCEPTION(isolate, context,
13211 JSReceiver::GetFunctionRealm(new_target), Map);
13212 DCHECK(context->IsNativeContext());
13213 Handle<Object> maybe_index = JSReceiver::GetDataProperty(
13214 constructor, isolate->factory()->native_context_index_symbol());
13215 int index = maybe_index->IsSmi() ? Smi::cast(*maybe_index)->value()
13216 : Context::OBJECT_FUNCTION_INDEX;
13217 Handle<JSFunction> realm_constructor(JSFunction::cast(context->get(index)));
13218 prototype = handle(realm_constructor->prototype(), isolate);
13219 }
13220
13221 Handle<Map> map = Map::CopyInitialMap(constructor_initial_map);
13222 map->set_new_target_is_base(false);
13223 DCHECK(prototype->IsJSReceiver());
13224 if (map->prototype() != *prototype) {
13225 Map::SetPrototype(map, prototype, FAST_PROTOTYPE);
13226 }
13227 map->SetConstructor(*constructor);
13228 return map;
Steve Blocka7e24c12009-10-30 11:49:00 +000013229}
13230
13231
Ben Murdochb0fe1622011-05-05 13:52:32 +010013232void JSFunction::PrintName(FILE* out) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013233 base::SmartArrayPointer<char> name = shared()->DebugName()->ToCString();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013234 PrintF(out, "%s", name.get());
Ben Murdochb0fe1622011-05-05 13:52:32 +010013235}
13236
13237
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013238Handle<String> JSFunction::GetName(Handle<JSFunction> function) {
13239 Isolate* isolate = function->GetIsolate();
13240 Handle<Object> name =
13241 JSReceiver::GetDataProperty(function, isolate->factory()->name_string());
13242 if (name->IsString()) return Handle<String>::cast(name);
13243 return handle(function->shared()->DebugName(), isolate);
13244}
13245
13246
13247Handle<String> JSFunction::GetDebugName(Handle<JSFunction> function) {
13248 Isolate* isolate = function->GetIsolate();
13249 Handle<Object> name = JSReceiver::GetDataProperty(
13250 function, isolate->factory()->display_name_string());
13251 if (name->IsString()) return Handle<String>::cast(name);
13252 return JSFunction::GetName(function);
13253}
13254
Ben Murdoch097c5b22016-05-18 11:27:45 +010013255void JSFunction::SetName(Handle<JSFunction> function, Handle<Name> name,
13256 Handle<String> prefix) {
13257 Isolate* isolate = function->GetIsolate();
13258 Handle<String> function_name = Name::ToFunctionName(name).ToHandleChecked();
13259 if (prefix->length() > 0) {
13260 IncrementalStringBuilder builder(isolate);
13261 builder.AppendString(prefix);
13262 builder.AppendCharacter(' ');
13263 builder.AppendString(function_name);
13264 function_name = builder.Finish().ToHandleChecked();
13265 }
13266 JSObject::DefinePropertyOrElementIgnoreAttributes(
13267 function, isolate->factory()->name_string(), function_name,
13268 static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY))
13269 .ToHandleChecked();
13270}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013271
13272namespace {
13273
13274char const kNativeCodeSource[] = "function () { [native code] }";
13275
13276
13277Handle<String> NativeCodeFunctionSourceString(
13278 Handle<SharedFunctionInfo> shared_info) {
13279 Isolate* const isolate = shared_info->GetIsolate();
13280 if (shared_info->name()->IsString()) {
13281 IncrementalStringBuilder builder(isolate);
13282 builder.AppendCString("function ");
13283 builder.AppendString(handle(String::cast(shared_info->name()), isolate));
13284 builder.AppendCString("() { [native code] }");
13285 return builder.Finish().ToHandleChecked();
13286 }
13287 return isolate->factory()->NewStringFromAsciiChecked(kNativeCodeSource);
13288}
13289
13290} // namespace
13291
13292
13293// static
13294Handle<String> JSBoundFunction::ToString(Handle<JSBoundFunction> function) {
13295 Isolate* const isolate = function->GetIsolate();
13296 return isolate->factory()->NewStringFromAsciiChecked(kNativeCodeSource);
13297}
13298
Ben Murdochda12d292016-06-02 14:46:10 +010013299// static
13300MaybeHandle<String> JSBoundFunction::GetName(Isolate* isolate,
13301 Handle<JSBoundFunction> function) {
13302 Handle<String> prefix = isolate->factory()->bound__string();
13303 if (!function->bound_target_function()->IsJSFunction()) return prefix;
13304 Handle<JSFunction> target(JSFunction::cast(function->bound_target_function()),
13305 isolate);
13306 Handle<Object> target_name = JSFunction::GetName(target);
13307 if (!target_name->IsString()) return prefix;
13308 Factory* factory = isolate->factory();
13309 return factory->NewConsString(prefix, Handle<String>::cast(target_name));
13310}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013311
13312// static
13313Handle<String> JSFunction::ToString(Handle<JSFunction> function) {
13314 Isolate* const isolate = function->GetIsolate();
13315 Handle<SharedFunctionInfo> shared_info(function->shared(), isolate);
13316
13317 // Check if {function} should hide its source code.
13318 if (!shared_info->script()->IsScript() ||
13319 Script::cast(shared_info->script())->hide_source()) {
13320 return NativeCodeFunctionSourceString(shared_info);
13321 }
13322
13323 // Check if we should print {function} as a class.
13324 Handle<Object> class_start_position = JSReceiver::GetDataProperty(
13325 function, isolate->factory()->class_start_position_symbol());
13326 if (class_start_position->IsSmi()) {
13327 Handle<Object> class_end_position = JSReceiver::GetDataProperty(
13328 function, isolate->factory()->class_end_position_symbol());
13329 Handle<String> script_source(
13330 String::cast(Script::cast(shared_info->script())->source()), isolate);
13331 return isolate->factory()->NewSubString(
13332 script_source, Handle<Smi>::cast(class_start_position)->value(),
13333 Handle<Smi>::cast(class_end_position)->value());
13334 }
13335
13336 // Check if we have source code for the {function}.
13337 if (!shared_info->HasSourceCode()) {
13338 return NativeCodeFunctionSourceString(shared_info);
13339 }
13340
13341 IncrementalStringBuilder builder(isolate);
13342 if (!shared_info->is_arrow()) {
13343 if (shared_info->is_concise_method()) {
13344 if (shared_info->is_generator()) builder.AppendCharacter('*');
13345 } else {
13346 if (shared_info->is_generator()) {
13347 builder.AppendCString("function* ");
13348 } else {
13349 builder.AppendCString("function ");
13350 }
13351 }
13352 if (shared_info->name_should_print_as_anonymous()) {
13353 builder.AppendCString("anonymous");
Ben Murdoch097c5b22016-05-18 11:27:45 +010013354 } else if (!shared_info->is_anonymous_expression()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013355 builder.AppendString(handle(String::cast(shared_info->name()), isolate));
13356 }
13357 }
13358 builder.AppendString(Handle<String>::cast(shared_info->GetSourceCode()));
13359 return builder.Finish().ToHandleChecked();
13360}
13361
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013362void Oddball::Initialize(Isolate* isolate, Handle<Oddball> oddball,
13363 const char* to_string, Handle<Object> to_number,
Ben Murdochda12d292016-06-02 14:46:10 +010013364 bool to_boolean, const char* type_of, byte kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013365 Handle<String> internalized_to_string =
13366 isolate->factory()->InternalizeUtf8String(to_string);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013367 Handle<String> internalized_type_of =
13368 isolate->factory()->InternalizeUtf8String(type_of);
Ben Murdochda12d292016-06-02 14:46:10 +010013369 oddball->set_to_boolean(isolate->heap()->ToBoolean(to_boolean));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013370 oddball->set_to_number(*to_number);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013371 oddball->set_to_string(*internalized_to_string);
13372 oddball->set_type_of(*internalized_type_of);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013373 oddball->set_kind(kind);
13374}
13375
13376
13377void Script::InitLineEnds(Handle<Script> script) {
13378 if (!script->line_ends()->IsUndefined()) return;
13379
13380 Isolate* isolate = script->GetIsolate();
13381
13382 if (!script->source()->IsString()) {
13383 DCHECK(script->source()->IsUndefined());
13384 Handle<FixedArray> empty = isolate->factory()->NewFixedArray(0);
13385 script->set_line_ends(*empty);
13386 DCHECK(script->line_ends()->IsFixedArray());
13387 return;
13388 }
13389
13390 Handle<String> src(String::cast(script->source()), isolate);
13391
13392 Handle<FixedArray> array = String::CalculateLineEnds(src, true);
13393
13394 if (*array != isolate->heap()->empty_fixed_array()) {
13395 array->set_map(isolate->heap()->fixed_cow_array_map());
13396 }
13397
13398 script->set_line_ends(*array);
13399 DCHECK(script->line_ends()->IsFixedArray());
13400}
13401
13402
13403int Script::GetColumnNumber(Handle<Script> script, int code_pos) {
13404 int line_number = GetLineNumber(script, code_pos);
13405 if (line_number == -1) return -1;
13406
13407 DisallowHeapAllocation no_allocation;
13408 FixedArray* line_ends_array = FixedArray::cast(script->line_ends());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013409 line_number = line_number - script->line_offset();
13410 if (line_number == 0) return code_pos + script->column_offset();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013411 int prev_line_end_pos =
13412 Smi::cast(line_ends_array->get(line_number - 1))->value();
13413 return code_pos - (prev_line_end_pos + 1);
13414}
13415
13416
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013417int Script::GetLineNumberWithArray(int code_pos) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013418 DisallowHeapAllocation no_allocation;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013419 DCHECK(line_ends()->IsFixedArray());
13420 FixedArray* line_ends_array = FixedArray::cast(line_ends());
13421 int line_ends_len = line_ends_array->length();
13422 if (line_ends_len == 0) return -1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013423
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013424 if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) {
13425 return line_offset();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013426 }
13427
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013428 int left = 0;
13429 int right = line_ends_len;
13430 while (int half = (right - left) / 2) {
13431 if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) {
13432 right -= half;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013433 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013434 left += half;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013435 }
13436 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013437 return right + line_offset();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013438}
13439
13440
13441int Script::GetLineNumber(Handle<Script> script, int code_pos) {
13442 InitLineEnds(script);
13443 return script->GetLineNumberWithArray(code_pos);
13444}
13445
13446
13447int Script::GetLineNumber(int code_pos) {
13448 DisallowHeapAllocation no_allocation;
13449 if (!line_ends()->IsUndefined()) return GetLineNumberWithArray(code_pos);
13450
13451 // Slow mode: we do not have line_ends. We have to iterate through source.
13452 if (!source()->IsString()) return -1;
13453
13454 String* source_string = String::cast(source());
13455 int line = 0;
13456 int len = source_string->length();
13457 for (int pos = 0; pos < len; pos++) {
13458 if (pos == code_pos) break;
13459 if (source_string->Get(pos) == '\n') line++;
13460 }
13461 return line;
13462}
13463
13464
13465Handle<Object> Script::GetNameOrSourceURL(Handle<Script> script) {
13466 Isolate* isolate = script->GetIsolate();
13467 Handle<String> name_or_source_url_key =
13468 isolate->factory()->InternalizeOneByteString(
13469 STATIC_CHAR_VECTOR("nameOrSourceURL"));
13470 Handle<JSObject> script_wrapper = Script::GetWrapper(script);
Ben Murdochda12d292016-06-02 14:46:10 +010013471 Handle<Object> property =
13472 JSReceiver::GetProperty(script_wrapper, name_or_source_url_key)
13473 .ToHandleChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013474 DCHECK(property->IsJSFunction());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013475 Handle<Object> result;
13476 // Do not check against pending exception, since this function may be called
13477 // when an exception has already been pending.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013478 if (!Execution::TryCall(isolate, property, script_wrapper, 0, NULL)
13479 .ToHandle(&result)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013480 return isolate->factory()->undefined_value();
13481 }
13482 return result;
13483}
13484
13485
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013486Handle<JSObject> Script::GetWrapper(Handle<Script> script) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013487 Isolate* isolate = script->GetIsolate();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013488 if (!script->wrapper()->IsUndefined()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013489 DCHECK(script->wrapper()->IsWeakCell());
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013490 Handle<WeakCell> cell(WeakCell::cast(script->wrapper()));
13491 if (!cell->cleared()) {
13492 // Return a handle for the existing script wrapper from the cache.
13493 return handle(JSObject::cast(cell->value()));
13494 }
13495 // If we found an empty WeakCell, that means the script wrapper was
13496 // GCed. We are not notified directly of that, so we decrement here
13497 // so that we at least don't count double for any given script.
13498 isolate->counters()->script_wrappers()->Decrement();
13499 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013500 // Construct a new script wrapper.
13501 isolate->counters()->script_wrappers()->Increment();
13502 Handle<JSFunction> constructor = isolate->script_function();
13503 Handle<JSValue> result =
13504 Handle<JSValue>::cast(isolate->factory()->NewJSObject(constructor));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013505 result->set_value(*script);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013506 Handle<WeakCell> cell = isolate->factory()->NewWeakCell(result);
13507 script->set_wrapper(*cell);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013508 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +000013509}
13510
13511
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013512MaybeHandle<SharedFunctionInfo> Script::FindSharedFunctionInfo(
13513 FunctionLiteral* fun) {
13514 WeakFixedArray::Iterator iterator(shared_function_infos());
13515 SharedFunctionInfo* shared;
13516 while ((shared = iterator.Next<SharedFunctionInfo>())) {
13517 if (fun->function_token_position() == shared->function_token_position() &&
13518 fun->start_position() == shared->start_position()) {
13519 return Handle<SharedFunctionInfo>(shared);
13520 }
13521 }
13522 return MaybeHandle<SharedFunctionInfo>();
13523}
13524
13525
13526Script::Iterator::Iterator(Isolate* isolate)
13527 : iterator_(isolate->heap()->script_list()) {}
13528
13529
13530Script* Script::Iterator::Next() { return iterator_.Next<Script>(); }
13531
13532
13533SharedFunctionInfo::Iterator::Iterator(Isolate* isolate)
13534 : script_iterator_(isolate),
13535 sfi_iterator_(isolate->heap()->noscript_shared_function_infos()) {}
13536
13537
13538bool SharedFunctionInfo::Iterator::NextScript() {
13539 Script* script = script_iterator_.Next();
13540 if (script == NULL) return false;
13541 sfi_iterator_.Reset(script->shared_function_infos());
13542 return true;
13543}
13544
13545
13546SharedFunctionInfo* SharedFunctionInfo::Iterator::Next() {
13547 do {
13548 SharedFunctionInfo* next = sfi_iterator_.Next<SharedFunctionInfo>();
13549 if (next != NULL) return next;
13550 } while (NextScript());
13551 return NULL;
13552}
13553
13554
13555void SharedFunctionInfo::SetScript(Handle<SharedFunctionInfo> shared,
13556 Handle<Object> script_object) {
13557 if (shared->script() == *script_object) return;
13558 Isolate* isolate = shared->GetIsolate();
13559
13560 // Add shared function info to new script's list. If a collection occurs,
13561 // the shared function info may be temporarily in two lists.
13562 // This is okay because the gc-time processing of these lists can tolerate
13563 // duplicates.
13564 Handle<Object> list;
13565 if (script_object->IsScript()) {
13566 Handle<Script> script = Handle<Script>::cast(script_object);
13567 list = handle(script->shared_function_infos(), isolate);
13568 } else {
13569 list = isolate->factory()->noscript_shared_function_infos();
13570 }
13571
13572#ifdef DEBUG
Ben Murdochda12d292016-06-02 14:46:10 +010013573 if (FLAG_enable_slow_asserts) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013574 WeakFixedArray::Iterator iterator(*list);
13575 SharedFunctionInfo* next;
13576 while ((next = iterator.Next<SharedFunctionInfo>())) {
13577 DCHECK_NE(next, *shared);
13578 }
13579 }
13580#endif // DEBUG
13581 list = WeakFixedArray::Add(list, shared);
13582
13583 if (script_object->IsScript()) {
13584 Handle<Script> script = Handle<Script>::cast(script_object);
13585 script->set_shared_function_infos(*list);
13586 } else {
13587 isolate->heap()->SetRootNoScriptSharedFunctionInfos(*list);
13588 }
13589
13590 // Remove shared function info from old script's list.
13591 if (shared->script()->IsScript()) {
13592 Script* old_script = Script::cast(shared->script());
13593 if (old_script->shared_function_infos()->IsWeakFixedArray()) {
13594 WeakFixedArray* list =
13595 WeakFixedArray::cast(old_script->shared_function_infos());
13596 list->Remove(shared);
13597 }
13598 } else {
13599 // Remove shared function info from root array.
13600 Object* list = isolate->heap()->noscript_shared_function_infos();
13601 CHECK(WeakFixedArray::cast(list)->Remove(shared));
13602 }
13603
13604 // Finally set new script.
13605 shared->set_script(*script_object);
13606}
13607
13608
Ben Murdochf87a2032010-10-22 12:50:53 +010013609String* SharedFunctionInfo::DebugName() {
13610 Object* n = name();
13611 if (!n->IsString() || String::cast(n)->length() == 0) return inferred_name();
13612 return String::cast(n);
13613}
13614
Ben Murdochda12d292016-06-02 14:46:10 +010013615// The filter is a pattern that matches function names in this way:
13616// "*" all; the default
13617// "-" all but the top-level function
13618// "-name" all but the function "name"
13619// "" only the top-level function
13620// "name" only the function "name"
13621// "name*" only functions starting with "name"
13622// "~" none; the tilde is not an identifier
13623bool SharedFunctionInfo::PassesFilter(const char* raw_filter) {
13624 if (*raw_filter == '*') return true;
13625 String* name = DebugName();
13626 Vector<const char> filter = CStrVector(raw_filter);
13627 if (filter.length() == 0) return name->length() == 0;
13628 if (filter[0] == '-') {
13629 // Negative filter.
13630 if (filter.length() == 1) {
13631 return (name->length() != 0);
13632 } else if (name->IsUtf8EqualTo(filter.SubVector(1, filter.length()))) {
13633 return false;
13634 }
13635 if (filter[filter.length() - 1] == '*' &&
13636 name->IsUtf8EqualTo(filter.SubVector(1, filter.length() - 1), true)) {
13637 return false;
13638 }
13639 return true;
13640
13641 } else if (name->IsUtf8EqualTo(filter)) {
13642 return true;
13643 }
13644 if (filter[filter.length() - 1] == '*' &&
13645 name->IsUtf8EqualTo(filter.SubVector(0, filter.length() - 1), true)) {
13646 return true;
13647 }
13648 return false;
13649}
Ben Murdochf87a2032010-10-22 12:50:53 +010013650
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013651bool SharedFunctionInfo::HasSourceCode() const {
Steve Blocka7e24c12009-10-30 11:49:00 +000013652 return !script()->IsUndefined() &&
Iain Merrick75681382010-08-19 15:07:18 +010013653 !reinterpret_cast<Script*>(script())->source()->IsUndefined();
Steve Blocka7e24c12009-10-30 11:49:00 +000013654}
13655
13656
Ben Murdoch3ef787d2012-04-12 10:51:47 +010013657Handle<Object> SharedFunctionInfo::GetSourceCode() {
13658 if (!HasSourceCode()) return GetIsolate()->factory()->undefined_value();
13659 Handle<String> source(String::cast(Script::cast(script())->source()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013660 return GetIsolate()->factory()->NewSubString(
13661 source, start_position(), end_position());
13662}
13663
13664
13665bool SharedFunctionInfo::IsInlineable() {
13666 // Check that the function has a script associated with it.
13667 if (!script()->IsScript()) return false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013668 return !optimization_disabled();
Steve Blocka7e24c12009-10-30 11:49:00 +000013669}
13670
13671
Ben Murdochb0fe1622011-05-05 13:52:32 +010013672int SharedFunctionInfo::SourceSize() {
13673 return end_position() - start_position();
13674}
13675
Ben Murdochda12d292016-06-02 14:46:10 +010013676void JSFunction::CalculateInstanceSizeHelper(InstanceType instance_type,
13677 int requested_internal_fields,
13678 int requested_in_object_properties,
13679 int* instance_size,
13680 int* in_object_properties) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013681 int header_size = JSObject::GetHeaderSize(instance_type);
13682 DCHECK_LE(requested_internal_fields,
13683 (JSObject::kMaxInstanceSize - header_size) >> kPointerSizeLog2);
13684 *instance_size =
13685 Min(header_size +
13686 ((requested_internal_fields + requested_in_object_properties)
13687 << kPointerSizeLog2),
13688 JSObject::kMaxInstanceSize);
13689 *in_object_properties = ((*instance_size - header_size) >> kPointerSizeLog2) -
13690 requested_internal_fields;
13691}
13692
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013693
13694void JSFunction::CalculateInstanceSize(InstanceType instance_type,
13695 int requested_internal_fields,
13696 int* instance_size,
13697 int* in_object_properties) {
13698 CalculateInstanceSizeHelper(instance_type, requested_internal_fields,
13699 shared()->expected_nof_properties(),
13700 instance_size, in_object_properties);
Steve Blocka7e24c12009-10-30 11:49:00 +000013701}
13702
13703
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013704void JSFunction::CalculateInstanceSizeForDerivedClass(
13705 InstanceType instance_type, int requested_internal_fields,
13706 int* instance_size, int* in_object_properties) {
13707 Isolate* isolate = GetIsolate();
13708 int expected_nof_properties = 0;
13709 for (PrototypeIterator iter(isolate, this,
13710 PrototypeIterator::START_AT_RECEIVER);
13711 !iter.IsAtEnd(); iter.Advance()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010013712 JSReceiver* current = iter.GetCurrent<JSReceiver>();
13713 if (!current->IsJSFunction()) break;
13714 JSFunction* func = JSFunction::cast(current);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013715 SharedFunctionInfo* shared = func->shared();
13716 expected_nof_properties += shared->expected_nof_properties();
13717 if (!IsSubclassConstructor(shared->kind())) {
13718 break;
13719 }
13720 }
13721 CalculateInstanceSizeHelper(instance_type, requested_internal_fields,
13722 expected_nof_properties, instance_size,
13723 in_object_properties);
Steve Blocka7e24c12009-10-30 11:49:00 +000013724}
13725
13726
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013727// Output the source code without any allocation in the heap.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013728std::ostream& operator<<(std::ostream& os, const SourceCodeOf& v) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013729 const SharedFunctionInfo* s = v.value;
Steve Blocka7e24c12009-10-30 11:49:00 +000013730 // For some native functions there is no source.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013731 if (!s->HasSourceCode()) return os << "<No Source>";
Steve Blocka7e24c12009-10-30 11:49:00 +000013732
Steve Blockd0582a62009-12-15 09:54:21 +000013733 // Get the source for the script which this function came from.
Steve Blocka7e24c12009-10-30 11:49:00 +000013734 // Don't use String::cast because we don't want more assertion errors while
13735 // we are already creating a stack dump.
13736 String* script_source =
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013737 reinterpret_cast<String*>(Script::cast(s->script())->source());
Steve Blocka7e24c12009-10-30 11:49:00 +000013738
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013739 if (!script_source->LooksValid()) return os << "<Invalid Source>";
Steve Blocka7e24c12009-10-30 11:49:00 +000013740
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013741 if (!s->is_toplevel()) {
13742 os << "function ";
13743 Object* name = s->name();
Steve Blocka7e24c12009-10-30 11:49:00 +000013744 if (name->IsString() && String::cast(name)->length() > 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013745 String::cast(name)->PrintUC16(os);
Steve Blocka7e24c12009-10-30 11:49:00 +000013746 }
13747 }
13748
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013749 int len = s->end_position() - s->start_position();
13750 if (len <= v.max_length || v.max_length < 0) {
13751 script_source->PrintUC16(os, s->start_position(), s->end_position());
13752 return os;
Ben Murdochb0fe1622011-05-05 13:52:32 +010013753 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013754 script_source->PrintUC16(os, s->start_position(),
13755 s->start_position() + v.max_length);
13756 return os << "...\n";
Steve Blocka7e24c12009-10-30 11:49:00 +000013757 }
13758}
13759
13760
Ben Murdochb0fe1622011-05-05 13:52:32 +010013761static bool IsCodeEquivalent(Code* code, Code* recompiled) {
13762 if (code->instruction_size() != recompiled->instruction_size()) return false;
13763 ByteArray* code_relocation = code->relocation_info();
13764 ByteArray* recompiled_relocation = recompiled->relocation_info();
13765 int length = code_relocation->length();
13766 if (length != recompiled_relocation->length()) return false;
13767 int compare = memcmp(code_relocation->GetDataStartAddress(),
13768 recompiled_relocation->GetDataStartAddress(),
13769 length);
13770 return compare == 0;
13771}
13772
13773
13774void SharedFunctionInfo::EnableDeoptimizationSupport(Code* recompiled) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013775 DCHECK(!has_deoptimization_support());
13776 DisallowHeapAllocation no_allocation;
Ben Murdochb0fe1622011-05-05 13:52:32 +010013777 Code* code = this->code();
13778 if (IsCodeEquivalent(code, recompiled)) {
13779 // Copy the deoptimization data from the recompiled code.
13780 code->set_deoptimization_data(recompiled->deoptimization_data());
13781 code->set_has_deoptimization_support(true);
13782 } else {
13783 // TODO(3025757): In case the recompiled isn't equivalent to the
13784 // old code, we have to replace it. We should try to avoid this
13785 // altogether because it flushes valuable type feedback by
13786 // effectively resetting all IC state.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013787 ReplaceCode(recompiled);
Ben Murdochb0fe1622011-05-05 13:52:32 +010013788 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013789 DCHECK(has_deoptimization_support());
Ben Murdochb0fe1622011-05-05 13:52:32 +010013790}
13791
13792
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013793void SharedFunctionInfo::DisableOptimization(BailoutReason reason) {
Ben Murdoch257744e2011-11-30 15:57:28 +000013794 // Disable optimization for the shared function info and mark the
13795 // code as non-optimizable. The marker on the shared function info
13796 // is there because we flush non-optimized code thereby loosing the
13797 // non-optimizable information for the code. When the code is
13798 // regenerated and set on the shared function info it is marked as
13799 // non-optimizable if optimization is disabled for the shared
13800 // function info.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013801 DCHECK(reason != kNoReason);
Ben Murdoch257744e2011-11-30 15:57:28 +000013802 set_optimization_disabled(true);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013803 set_disable_optimization_reason(reason);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013804 // Code should be the lazy compilation stub or else unoptimized.
Ben Murdochda12d292016-06-02 14:46:10 +010013805 DCHECK(abstract_code()->kind() == AbstractCode::FUNCTION ||
13806 abstract_code()->kind() == AbstractCode::INTERPRETED_FUNCTION ||
13807 abstract_code()->kind() == AbstractCode::BUILTIN);
13808 PROFILE(GetIsolate(), CodeDisableOptEvent(abstract_code(), this));
Ben Murdoch257744e2011-11-30 15:57:28 +000013809 if (FLAG_trace_opt) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013810 PrintF("[disabled optimization for ");
13811 ShortPrint();
13812 PrintF(", reason: %s]\n", GetBailoutReason(reason));
Ben Murdoch257744e2011-11-30 15:57:28 +000013813 }
13814}
13815
13816
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013817void SharedFunctionInfo::InitFromFunctionLiteral(
13818 Handle<SharedFunctionInfo> shared_info, FunctionLiteral* lit) {
13819 shared_info->set_length(lit->scope()->default_function_length());
13820 shared_info->set_internal_formal_parameter_count(lit->parameter_count());
13821 shared_info->set_function_token_position(lit->function_token_position());
13822 shared_info->set_start_position(lit->start_position());
13823 shared_info->set_end_position(lit->end_position());
Ben Murdoch097c5b22016-05-18 11:27:45 +010013824 shared_info->set_is_declaration(lit->is_declaration());
13825 shared_info->set_is_named_expression(lit->is_named_expression());
13826 shared_info->set_is_anonymous_expression(lit->is_anonymous_expression());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013827 shared_info->set_inferred_name(*lit->inferred_name());
13828 shared_info->set_allows_lazy_compilation(lit->AllowsLazyCompilation());
13829 shared_info->set_allows_lazy_compilation_without_context(
13830 lit->AllowsLazyCompilationWithoutContext());
13831 shared_info->set_language_mode(lit->language_mode());
13832 shared_info->set_uses_arguments(lit->scope()->arguments() != NULL);
13833 shared_info->set_has_duplicate_parameters(lit->has_duplicate_parameters());
13834 shared_info->set_ast_node_count(lit->ast_node_count());
13835 shared_info->set_is_function(lit->is_function());
13836 if (lit->dont_optimize_reason() != kNoReason) {
13837 shared_info->DisableOptimization(lit->dont_optimize_reason());
13838 }
13839 shared_info->set_dont_crankshaft(lit->flags() &
13840 AstProperties::kDontCrankshaft);
13841 shared_info->set_kind(lit->kind());
13842 if (!IsConstructable(lit->kind(), lit->language_mode())) {
13843 shared_info->set_construct_stub(
13844 *shared_info->GetIsolate()->builtins()->ConstructedNonConstructable());
13845 }
13846 shared_info->set_needs_home_object(lit->scope()->NeedsHomeObject());
13847 shared_info->set_asm_function(lit->scope()->asm_function());
13848}
13849
13850
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013851bool SharedFunctionInfo::VerifyBailoutId(BailoutId id) {
13852 DCHECK(!id.IsNone());
Ben Murdochb0fe1622011-05-05 13:52:32 +010013853 Code* unoptimized = code();
13854 DeoptimizationOutputData* data =
13855 DeoptimizationOutputData::cast(unoptimized->deoptimization_data());
13856 unsigned ignore = Deoptimizer::GetOutputInfo(data, id, this);
13857 USE(ignore);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013858 return true; // Return true if there was no DCHECK.
Ben Murdochb0fe1622011-05-05 13:52:32 +010013859}
13860
13861
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013862void Map::StartInobjectSlackTracking() {
13863 DCHECK(!IsInobjectSlackTrackingInProgress());
Ben Murdoch3ef787d2012-04-12 10:51:47 +010013864
Kristian Monsen0d5e1162010-09-30 15:31:59 +010013865 // No tracking during the snapshot construction phase.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013866 Isolate* isolate = GetIsolate();
13867 if (isolate->serializer_enabled()) return;
Kristian Monsen0d5e1162010-09-30 15:31:59 +010013868
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013869 if (unused_property_fields() == 0) return;
Kristian Monsen0d5e1162010-09-30 15:31:59 +010013870
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013871 set_construction_counter(Map::kSlackTrackingCounterStart);
Kristian Monsen0d5e1162010-09-30 15:31:59 +010013872}
13873
13874
Ben Murdoch8f9999f2012-04-23 10:39:17 +010013875void SharedFunctionInfo::ResetForNewContext(int new_ic_age) {
13876 code()->ClearInlineCaches();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013877 // If we clear ICs, we need to clear the type feedback vector too, since
13878 // CallICs are synced with a feedback vector slot.
13879 ClearTypeFeedbackInfo();
Ben Murdoch8f9999f2012-04-23 10:39:17 +010013880 set_ic_age(new_ic_age);
13881 if (code()->kind() == Code::FUNCTION) {
13882 code()->set_profiler_ticks(0);
13883 if (optimization_disabled() &&
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013884 opt_count() >= FLAG_max_opt_count) {
Ben Murdoch8f9999f2012-04-23 10:39:17 +010013885 // Re-enable optimizations if they were disabled due to opt_count limit.
13886 set_optimization_disabled(false);
Ben Murdoch8f9999f2012-04-23 10:39:17 +010013887 }
13888 set_opt_count(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013889 set_deopt_count(0);
Ben Murdoch8f9999f2012-04-23 10:39:17 +010013890 }
13891}
13892
13893
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013894int SharedFunctionInfo::SearchOptimizedCodeMapEntry(Context* native_context,
13895 BailoutId osr_ast_id) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013896 DisallowHeapAllocation no_gc;
13897 DCHECK(native_context->IsNativeContext());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013898 if (!OptimizedCodeMapIsCleared()) {
13899 FixedArray* optimized_code_map = this->optimized_code_map();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013900 int length = optimized_code_map->length();
13901 Smi* osr_ast_id_smi = Smi::FromInt(osr_ast_id.ToInt());
13902 for (int i = kEntriesStart; i < length; i += kEntryLength) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013903 if (WeakCell::cast(optimized_code_map->get(i + kContextOffset))
13904 ->value() == native_context &&
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013905 optimized_code_map->get(i + kOsrAstIdOffset) == osr_ast_id_smi) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013906 return i;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013907 }
13908 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013909 Object* shared_code =
13910 WeakCell::cast(optimized_code_map->get(kSharedCodeIndex))->value();
13911 if (shared_code->IsCode() && osr_ast_id.IsNone()) {
13912 return kSharedCodeIndex;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013913 }
13914 }
13915 return -1;
Ben Murdoch8f9999f2012-04-23 10:39:17 +010013916}
13917
13918
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013919CodeAndLiterals SharedFunctionInfo::SearchOptimizedCodeMap(
13920 Context* native_context, BailoutId osr_ast_id) {
13921 CodeAndLiterals result = {nullptr, nullptr};
13922 int entry = SearchOptimizedCodeMapEntry(native_context, osr_ast_id);
13923 if (entry != kNotFound) {
13924 FixedArray* code_map = optimized_code_map();
13925 if (entry == kSharedCodeIndex) {
13926 // We know the weak cell isn't cleared because we made sure of it in
13927 // SearchOptimizedCodeMapEntry and performed no allocations since that
13928 // call.
13929 result = {
13930 Code::cast(WeakCell::cast(code_map->get(kSharedCodeIndex))->value()),
13931 nullptr};
13932 } else {
13933 DCHECK_LE(entry + kEntryLength, code_map->length());
13934 WeakCell* cell = WeakCell::cast(code_map->get(entry + kCachedCodeOffset));
13935 WeakCell* literals_cell =
13936 WeakCell::cast(code_map->get(entry + kLiteralsOffset));
13937
13938 result = {cell->cleared() ? nullptr : Code::cast(cell->value()),
13939 literals_cell->cleared()
13940 ? nullptr
13941 : LiteralsArray::cast(literals_cell->value())};
13942 }
13943 }
13944 if (FLAG_trace_opt && !OptimizedCodeMapIsCleared() &&
13945 result.code == nullptr) {
13946 PrintF("[didn't find optimized code in optimized code map for ");
13947 ShortPrint();
13948 PrintF("]\n");
13949 }
13950 return result;
13951}
13952
13953
Ben Murdoch3ef787d2012-04-12 10:51:47 +010013954#define DECLARE_TAG(ignore1, name, ignore2) name,
13955const char* const VisitorSynchronization::kTags[
13956 VisitorSynchronization::kNumberOfSyncTags] = {
13957 VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG)
13958};
13959#undef DECLARE_TAG
13960
13961
13962#define DECLARE_TAG(ignore1, ignore2, name) name,
13963const char* const VisitorSynchronization::kTagNames[
13964 VisitorSynchronization::kNumberOfSyncTags] = {
13965 VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG)
13966};
13967#undef DECLARE_TAG
13968
13969
Steve Blocka7e24c12009-10-30 11:49:00 +000013970void ObjectVisitor::VisitCodeTarget(RelocInfo* rinfo) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013971 DCHECK(RelocInfo::IsCodeTarget(rinfo->rmode()));
Steve Blocka7e24c12009-10-30 11:49:00 +000013972 Object* target = Code::GetCodeFromTargetAddress(rinfo->target_address());
13973 Object* old_target = target;
13974 VisitPointer(&target);
13975 CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target.
13976}
13977
13978
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013979void ObjectVisitor::VisitCodeAgeSequence(RelocInfo* rinfo) {
13980 DCHECK(RelocInfo::IsCodeAgeSequence(rinfo->rmode()));
13981 Object* stub = rinfo->code_age_stub();
13982 if (stub) {
13983 VisitPointer(&stub);
13984 }
13985}
13986
13987
Steve Block791712a2010-08-27 10:21:07 +010013988void ObjectVisitor::VisitCodeEntry(Address entry_address) {
13989 Object* code = Code::GetObjectFromEntryAddress(entry_address);
13990 Object* old_code = code;
13991 VisitPointer(&code);
13992 if (code != old_code) {
13993 Memory::Address_at(entry_address) = reinterpret_cast<Code*>(code)->entry();
13994 }
13995}
13996
13997
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013998void ObjectVisitor::VisitCell(RelocInfo* rinfo) {
13999 DCHECK(rinfo->rmode() == RelocInfo::CELL);
Ben Murdochb0fe1622011-05-05 13:52:32 +010014000 Object* cell = rinfo->target_cell();
14001 Object* old_cell = cell;
14002 VisitPointer(&cell);
14003 if (cell != old_cell) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014004 rinfo->set_target_cell(reinterpret_cast<Cell*>(cell));
Ben Murdochb0fe1622011-05-05 13:52:32 +010014005 }
14006}
14007
14008
Steve Blocka7e24c12009-10-30 11:49:00 +000014009void ObjectVisitor::VisitDebugTarget(RelocInfo* rinfo) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014010 DCHECK(RelocInfo::IsDebugBreakSlot(rinfo->rmode()) &&
14011 rinfo->IsPatchedDebugBreakSlotSequence());
14012 Object* target = Code::GetCodeFromTargetAddress(rinfo->debug_call_address());
Steve Blocka7e24c12009-10-30 11:49:00 +000014013 Object* old_target = target;
14014 VisitPointer(&target);
14015 CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target.
14016}
14017
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014018
Ben Murdoch3ef787d2012-04-12 10:51:47 +010014019void ObjectVisitor::VisitEmbeddedPointer(RelocInfo* rinfo) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014020 DCHECK(rinfo->rmode() == RelocInfo::EMBEDDED_OBJECT);
14021 Object* p = rinfo->target_object();
14022 VisitPointer(&p);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010014023}
14024
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014025
Ben Murdoch3ef787d2012-04-12 10:51:47 +010014026void ObjectVisitor::VisitExternalReference(RelocInfo* rinfo) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014027 Address p = rinfo->target_external_reference();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014028 VisitExternalReference(&p);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010014029}
14030
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014031
Ben Murdochb0fe1622011-05-05 13:52:32 +010014032void Code::InvalidateRelocation() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014033 InvalidateEmbeddedObjects();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010014034 set_relocation_info(GetHeap()->empty_byte_array());
Ben Murdochb0fe1622011-05-05 13:52:32 +010014035}
14036
14037
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014038void Code::InvalidateEmbeddedObjects() {
14039 Object* undefined = GetHeap()->undefined_value();
14040 Cell* undefined_cell = GetHeap()->undefined_cell();
14041 int mode_mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
14042 RelocInfo::ModeMask(RelocInfo::CELL);
14043 for (RelocIterator it(this, mode_mask); !it.done(); it.next()) {
14044 RelocInfo::Mode mode = it.rinfo()->rmode();
14045 if (mode == RelocInfo::EMBEDDED_OBJECT) {
14046 it.rinfo()->set_target_object(undefined, SKIP_WRITE_BARRIER);
14047 } else if (mode == RelocInfo::CELL) {
14048 it.rinfo()->set_target_cell(undefined_cell, SKIP_WRITE_BARRIER);
14049 }
14050 }
14051}
14052
14053
Steve Blockd0582a62009-12-15 09:54:21 +000014054void Code::Relocate(intptr_t delta) {
Steve Blocka7e24c12009-10-30 11:49:00 +000014055 for (RelocIterator it(this, RelocInfo::kApplyMask); !it.done(); it.next()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014056 it.rinfo()->apply(delta);
Steve Blocka7e24c12009-10-30 11:49:00 +000014057 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014058 Assembler::FlushICache(GetIsolate(), instruction_start(), instruction_size());
Steve Blocka7e24c12009-10-30 11:49:00 +000014059}
14060
14061
14062void Code::CopyFrom(const CodeDesc& desc) {
14063 // copy code
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014064 CopyBytes(instruction_start(), desc.buffer,
14065 static_cast<size_t>(desc.instr_size));
Steve Blocka7e24c12009-10-30 11:49:00 +000014066
Steve Blocka7e24c12009-10-30 11:49:00 +000014067 // copy reloc info
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014068 CopyBytes(relocation_start(),
14069 desc.buffer + desc.buffer_size - desc.reloc_size,
14070 static_cast<size_t>(desc.reloc_size));
Steve Blocka7e24c12009-10-30 11:49:00 +000014071
14072 // unbox handles and relocate
Steve Block3ce2e202009-11-05 08:53:23 +000014073 intptr_t delta = instruction_start() - desc.buffer;
Steve Blocka7e24c12009-10-30 11:49:00 +000014074 int mode_mask = RelocInfo::kCodeTargetMask |
14075 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014076 RelocInfo::ModeMask(RelocInfo::CELL) |
14077 RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY) |
Steve Blocka7e24c12009-10-30 11:49:00 +000014078 RelocInfo::kApplyMask;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014079 // Needed to find target_object and runtime_entry on X64
14080 Assembler* origin = desc.origin;
14081 AllowDeferredHandleDereference embedding_raw_address;
Steve Blocka7e24c12009-10-30 11:49:00 +000014082 for (RelocIterator it(this, mode_mask); !it.done(); it.next()) {
14083 RelocInfo::Mode mode = it.rinfo()->rmode();
14084 if (mode == RelocInfo::EMBEDDED_OBJECT) {
Steve Block3ce2e202009-11-05 08:53:23 +000014085 Handle<Object> p = it.rinfo()->target_object_handle(origin);
Ben Murdochda12d292016-06-02 14:46:10 +010014086 it.rinfo()->set_target_object(*p, UPDATE_WRITE_BARRIER,
14087 SKIP_ICACHE_FLUSH);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014088 } else if (mode == RelocInfo::CELL) {
14089 Handle<Cell> cell = it.rinfo()->target_cell_handle();
Ben Murdochda12d292016-06-02 14:46:10 +010014090 it.rinfo()->set_target_cell(*cell, UPDATE_WRITE_BARRIER,
14091 SKIP_ICACHE_FLUSH);
Steve Blocka7e24c12009-10-30 11:49:00 +000014092 } else if (RelocInfo::IsCodeTarget(mode)) {
14093 // rewrite code handles in inline cache targets to direct
14094 // pointers to the first instruction in the code object
Steve Block3ce2e202009-11-05 08:53:23 +000014095 Handle<Object> p = it.rinfo()->target_object_handle(origin);
Steve Blocka7e24c12009-10-30 11:49:00 +000014096 Code* code = Code::cast(*p);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010014097 it.rinfo()->set_target_address(code->instruction_start(),
Ben Murdochda12d292016-06-02 14:46:10 +010014098 UPDATE_WRITE_BARRIER, SKIP_ICACHE_FLUSH);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014099 } else if (RelocInfo::IsRuntimeEntry(mode)) {
14100 Address p = it.rinfo()->target_runtime_entry(origin);
Ben Murdochda12d292016-06-02 14:46:10 +010014101 it.rinfo()->set_target_runtime_entry(p, UPDATE_WRITE_BARRIER,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014102 SKIP_ICACHE_FLUSH);
14103 } else if (mode == RelocInfo::CODE_AGE_SEQUENCE) {
14104 Handle<Object> p = it.rinfo()->code_age_stub_handle(origin);
14105 Code* code = Code::cast(*p);
14106 it.rinfo()->set_code_age_stub(code, SKIP_ICACHE_FLUSH);
Steve Blocka7e24c12009-10-30 11:49:00 +000014107 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014108 it.rinfo()->apply(delta);
Steve Blocka7e24c12009-10-30 11:49:00 +000014109 }
14110 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014111 Assembler::FlushICache(GetIsolate(), instruction_start(), instruction_size());
Steve Blocka7e24c12009-10-30 11:49:00 +000014112}
14113
Ben Murdoch097c5b22016-05-18 11:27:45 +010014114// Locate the source position which is closest to the code offset. This is
14115// using the source position information embedded in the relocation info.
Steve Blocka7e24c12009-10-30 11:49:00 +000014116// The position returned is relative to the beginning of the script where the
14117// source for this function is found.
Ben Murdoch097c5b22016-05-18 11:27:45 +010014118int Code::SourcePosition(int code_offset) {
14119 Address pc = instruction_start() + code_offset;
Steve Blocka7e24c12009-10-30 11:49:00 +000014120 int distance = kMaxInt;
14121 int position = RelocInfo::kNoPosition; // Initially no position found.
14122 // Run through all the relocation info to find the best matching source
14123 // position. All the code needs to be considered as the sequence of the
14124 // instructions in the code does not necessarily follow the same order as the
14125 // source.
14126 RelocIterator it(this, RelocInfo::kPositionMask);
14127 while (!it.done()) {
14128 // Only look at positions after the current pc.
14129 if (it.rinfo()->pc() < pc) {
14130 // Get position and distance.
Steve Blockd0582a62009-12-15 09:54:21 +000014131
14132 int dist = static_cast<int>(pc - it.rinfo()->pc());
14133 int pos = static_cast<int>(it.rinfo()->data());
Steve Blocka7e24c12009-10-30 11:49:00 +000014134 // If this position is closer than the current candidate or if it has the
14135 // same distance as the current candidate and the position is higher then
14136 // this position is the new candidate.
14137 if ((dist < distance) ||
14138 (dist == distance && pos > position)) {
14139 position = pos;
14140 distance = dist;
14141 }
14142 }
14143 it.next();
14144 }
Ben Murdochda12d292016-06-02 14:46:10 +010014145 DCHECK(kind() == FUNCTION || (is_optimized_code() && is_turbofanned()) ||
14146 is_wasm_code() || position == RelocInfo::kNoPosition);
Steve Blocka7e24c12009-10-30 11:49:00 +000014147 return position;
14148}
14149
14150
14151// Same as Code::SourcePosition above except it only looks for statement
14152// positions.
Ben Murdoch097c5b22016-05-18 11:27:45 +010014153int Code::SourceStatementPosition(int code_offset) {
Steve Blocka7e24c12009-10-30 11:49:00 +000014154 // First find the position as close as possible using all position
14155 // information.
Ben Murdoch097c5b22016-05-18 11:27:45 +010014156 int position = SourcePosition(code_offset);
Steve Blocka7e24c12009-10-30 11:49:00 +000014157 // Now find the closest statement position before the position.
14158 int statement_position = 0;
14159 RelocIterator it(this, RelocInfo::kPositionMask);
14160 while (!it.done()) {
14161 if (RelocInfo::IsStatementPosition(it.rinfo()->rmode())) {
Steve Blockd0582a62009-12-15 09:54:21 +000014162 int p = static_cast<int>(it.rinfo()->data());
Steve Blocka7e24c12009-10-30 11:49:00 +000014163 if (statement_position < p && p <= position) {
14164 statement_position = p;
14165 }
14166 }
14167 it.next();
14168 }
14169 return statement_position;
14170}
14171
14172
Ben Murdochb8e0da22011-05-16 14:20:40 +010014173SafepointEntry Code::GetSafepointEntry(Address pc) {
Ben Murdochb0fe1622011-05-05 13:52:32 +010014174 SafepointTable table(this);
Ben Murdochb8e0da22011-05-16 14:20:40 +010014175 return table.FindEntry(pc);
Ben Murdochb0fe1622011-05-05 13:52:32 +010014176}
14177
14178
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014179Object* Code::FindNthObject(int n, Map* match_map) {
14180 DCHECK(is_inline_cache_stub());
14181 DisallowHeapAllocation no_allocation;
Ben Murdochb0fe1622011-05-05 13:52:32 +010014182 int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
14183 for (RelocIterator it(this, mask); !it.done(); it.next()) {
14184 RelocInfo* info = it.rinfo();
14185 Object* object = info->target_object();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014186 if (object->IsWeakCell()) object = WeakCell::cast(object)->value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014187 if (object->IsHeapObject()) {
14188 if (HeapObject::cast(object)->map() == match_map) {
14189 if (--n == 0) return object;
14190 }
14191 }
14192 }
14193 return NULL;
14194}
14195
14196
14197AllocationSite* Code::FindFirstAllocationSite() {
14198 Object* result = FindNthObject(1, GetHeap()->allocation_site_map());
14199 return (result != NULL) ? AllocationSite::cast(result) : NULL;
14200}
14201
14202
14203Map* Code::FindFirstMap() {
14204 Object* result = FindNthObject(1, GetHeap()->meta_map());
14205 return (result != NULL) ? Map::cast(result) : NULL;
14206}
14207
14208
14209void Code::FindAndReplace(const FindAndReplacePattern& pattern) {
14210 DCHECK(is_inline_cache_stub() || is_handler());
14211 DisallowHeapAllocation no_allocation;
14212 int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
14213 STATIC_ASSERT(FindAndReplacePattern::kMaxCount < 32);
14214 int current_pattern = 0;
14215 for (RelocIterator it(this, mask); !it.done(); it.next()) {
14216 RelocInfo* info = it.rinfo();
14217 Object* object = info->target_object();
14218 if (object->IsHeapObject()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014219 if (object->IsWeakCell()) {
14220 object = HeapObject::cast(WeakCell::cast(object)->value());
14221 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014222 Map* map = HeapObject::cast(object)->map();
14223 if (map == *pattern.find_[current_pattern]) {
14224 info->set_target_object(*pattern.replace_[current_pattern]);
14225 if (++current_pattern == pattern.count_) return;
14226 }
14227 }
14228 }
14229 UNREACHABLE();
14230}
14231
14232
14233void Code::FindAllMaps(MapHandleList* maps) {
14234 DCHECK(is_inline_cache_stub());
14235 DisallowHeapAllocation no_allocation;
14236 int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
14237 for (RelocIterator it(this, mask); !it.done(); it.next()) {
14238 RelocInfo* info = it.rinfo();
14239 Object* object = info->target_object();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014240 if (object->IsWeakCell()) object = WeakCell::cast(object)->value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014241 if (object->IsMap()) maps->Add(handle(Map::cast(object)));
14242 }
14243}
14244
14245
14246Code* Code::FindFirstHandler() {
14247 DCHECK(is_inline_cache_stub());
14248 DisallowHeapAllocation no_allocation;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014249 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
14250 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
14251 bool skip_next_handler = false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014252 for (RelocIterator it(this, mask); !it.done(); it.next()) {
14253 RelocInfo* info = it.rinfo();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014254 if (info->rmode() == RelocInfo::EMBEDDED_OBJECT) {
14255 Object* obj = info->target_object();
14256 skip_next_handler |= obj->IsWeakCell() && WeakCell::cast(obj)->cleared();
14257 } else {
14258 Code* code = Code::GetCodeFromTargetAddress(info->target_address());
14259 if (code->kind() == Code::HANDLER) {
14260 if (!skip_next_handler) return code;
14261 skip_next_handler = false;
14262 }
14263 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014264 }
14265 return NULL;
14266}
14267
14268
14269bool Code::FindHandlers(CodeHandleList* code_list, int length) {
14270 DCHECK(is_inline_cache_stub());
14271 DisallowHeapAllocation no_allocation;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014272 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
14273 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
14274 bool skip_next_handler = false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014275 int i = 0;
14276 for (RelocIterator it(this, mask); !it.done(); it.next()) {
14277 if (i == length) return true;
14278 RelocInfo* info = it.rinfo();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014279 if (info->rmode() == RelocInfo::EMBEDDED_OBJECT) {
14280 Object* obj = info->target_object();
14281 skip_next_handler |= obj->IsWeakCell() && WeakCell::cast(obj)->cleared();
14282 } else {
14283 Code* code = Code::GetCodeFromTargetAddress(info->target_address());
14284 // IC stubs with handlers never contain non-handler code objects before
14285 // handler targets.
14286 if (code->kind() != Code::HANDLER) break;
14287 if (!skip_next_handler) {
14288 code_list->Add(Handle<Code>(code));
14289 i++;
14290 }
14291 skip_next_handler = false;
14292 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014293 }
14294 return i == length;
14295}
14296
14297
14298MaybeHandle<Code> Code::FindHandlerForMap(Map* map) {
14299 DCHECK(is_inline_cache_stub());
14300 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
14301 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
14302 bool return_next = false;
14303 for (RelocIterator it(this, mask); !it.done(); it.next()) {
14304 RelocInfo* info = it.rinfo();
14305 if (info->rmode() == RelocInfo::EMBEDDED_OBJECT) {
14306 Object* object = info->target_object();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014307 if (object->IsWeakCell()) object = WeakCell::cast(object)->value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014308 if (object == map) return_next = true;
14309 } else if (return_next) {
14310 Code* code = Code::GetCodeFromTargetAddress(info->target_address());
14311 DCHECK(code->kind() == Code::HANDLER);
14312 return handle(code);
14313 }
14314 }
14315 return MaybeHandle<Code>();
14316}
14317
14318
14319Name* Code::FindFirstName() {
14320 DCHECK(is_inline_cache_stub());
14321 DisallowHeapAllocation no_allocation;
14322 int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
14323 for (RelocIterator it(this, mask); !it.done(); it.next()) {
14324 RelocInfo* info = it.rinfo();
14325 Object* object = info->target_object();
14326 if (object->IsName()) return Name::cast(object);
Ben Murdochb0fe1622011-05-05 13:52:32 +010014327 }
14328 return NULL;
14329}
14330
14331
Ben Murdoch8f9999f2012-04-23 10:39:17 +010014332void Code::ClearInlineCaches() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014333 ClearInlineCaches(NULL);
14334}
14335
14336
14337void Code::ClearInlineCaches(Code::Kind kind) {
14338 ClearInlineCaches(&kind);
14339}
14340
14341
14342void Code::ClearInlineCaches(Code::Kind* kind) {
Ben Murdoch8f9999f2012-04-23 10:39:17 +010014343 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014344 RelocInfo::ModeMask(RelocInfo::CODE_TARGET_WITH_ID);
Ben Murdoch8f9999f2012-04-23 10:39:17 +010014345 for (RelocIterator it(this, mask); !it.done(); it.next()) {
14346 RelocInfo* info = it.rinfo();
14347 Code* target(Code::GetCodeFromTargetAddress(info->target_address()));
14348 if (target->is_inline_cache_stub()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014349 if (kind == NULL || *kind == target->kind()) {
14350 IC::Clear(this->GetIsolate(), info->pc(),
14351 info->host()->constant_pool());
14352 }
Ben Murdoch8f9999f2012-04-23 10:39:17 +010014353 }
14354 }
14355}
14356
Ben Murdoch097c5b22016-05-18 11:27:45 +010014357int AbstractCode::SourcePosition(int offset) {
14358 return IsBytecodeArray() ? GetBytecodeArray()->SourcePosition(offset)
14359 : GetCode()->SourcePosition(offset);
14360}
14361
14362int AbstractCode::SourceStatementPosition(int offset) {
14363 return IsBytecodeArray() ? GetBytecodeArray()->SourceStatementPosition(offset)
14364 : GetCode()->SourceStatementPosition(offset);
14365}
Ben Murdoch8f9999f2012-04-23 10:39:17 +010014366
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014367void SharedFunctionInfo::ClearTypeFeedbackInfo() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014368 feedback_vector()->ClearSlots(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014369}
14370
14371
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014372void SharedFunctionInfo::ClearTypeFeedbackInfoAtGCTime() {
14373 feedback_vector()->ClearSlotsAtGCTime(this);
14374}
14375
14376
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014377BailoutId Code::TranslatePcOffsetToAstId(uint32_t pc_offset) {
14378 DisallowHeapAllocation no_gc;
14379 DCHECK(kind() == FUNCTION);
14380 BackEdgeTable back_edges(this, &no_gc);
14381 for (uint32_t i = 0; i < back_edges.length(); i++) {
14382 if (back_edges.pc_offset(i) == pc_offset) return back_edges.ast_id(i);
14383 }
14384 return BailoutId::None();
14385}
14386
14387
14388uint32_t Code::TranslateAstIdToPcOffset(BailoutId ast_id) {
14389 DisallowHeapAllocation no_gc;
14390 DCHECK(kind() == FUNCTION);
14391 BackEdgeTable back_edges(this, &no_gc);
14392 for (uint32_t i = 0; i < back_edges.length(); i++) {
14393 if (back_edges.ast_id(i) == ast_id) return back_edges.pc_offset(i);
14394 }
14395 UNREACHABLE(); // We expect to find the back edge.
14396 return 0;
14397}
14398
14399
14400void Code::MakeCodeAgeSequenceYoung(byte* sequence, Isolate* isolate) {
14401 PatchPlatformCodeAge(isolate, sequence, kNoAgeCodeAge, NO_MARKING_PARITY);
14402}
14403
14404
14405void Code::MarkCodeAsExecuted(byte* sequence, Isolate* isolate) {
14406 PatchPlatformCodeAge(isolate, sequence, kExecutedOnceCodeAge,
14407 NO_MARKING_PARITY);
14408}
14409
14410
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014411// NextAge defines the Code::Age state transitions during a GC cycle.
14412static Code::Age NextAge(Code::Age age) {
14413 switch (age) {
14414 case Code::kNotExecutedCodeAge: // Keep, until we've been executed.
14415 case Code::kToBeExecutedOnceCodeAge: // Keep, until we've been executed.
14416 case Code::kLastCodeAge: // Clamp at last Code::Age value.
14417 return age;
14418 case Code::kExecutedOnceCodeAge:
14419 // Pre-age code that has only been executed once.
14420 return static_cast<Code::Age>(Code::kPreAgedCodeAge + 1);
14421 default:
14422 return static_cast<Code::Age>(age + 1); // Default case: Increase age.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014423 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014424}
14425
14426
14427// IsOldAge defines the collection criteria for a Code object.
14428static bool IsOldAge(Code::Age age) {
14429 return age >= Code::kIsOldCodeAge || age == Code::kNotExecutedCodeAge;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014430}
14431
14432
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014433void Code::MakeYoung(Isolate* isolate) {
14434 byte* sequence = FindCodeAgeSequence();
14435 if (sequence != NULL) MakeCodeAgeSequenceYoung(sequence, isolate);
14436}
14437
Ben Murdochda12d292016-06-02 14:46:10 +010014438void Code::PreAge(Isolate* isolate) {
14439 byte* sequence = FindCodeAgeSequence();
14440 if (sequence != NULL) {
14441 PatchPlatformCodeAge(isolate, sequence, kPreAgedCodeAge, NO_MARKING_PARITY);
14442 }
14443}
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014444
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014445void Code::MarkToBeExecutedOnce(Isolate* isolate) {
14446 byte* sequence = FindCodeAgeSequence();
14447 if (sequence != NULL) {
14448 PatchPlatformCodeAge(isolate, sequence, kToBeExecutedOnceCodeAge,
14449 NO_MARKING_PARITY);
14450 }
14451}
14452
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014453void Code::MakeOlder(MarkingParity current_parity) {
14454 byte* sequence = FindCodeAgeSequence();
14455 if (sequence != NULL) {
14456 Age age;
14457 MarkingParity code_parity;
14458 Isolate* isolate = GetIsolate();
14459 GetCodeAgeAndParity(isolate, sequence, &age, &code_parity);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014460 Age next_age = NextAge(age);
14461 if (age != next_age && code_parity != current_parity) {
14462 PatchPlatformCodeAge(isolate, sequence, next_age, current_parity);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014463 }
14464 }
14465}
14466
14467
14468bool Code::IsOld() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014469 return IsOldAge(GetAge());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014470}
14471
14472
14473byte* Code::FindCodeAgeSequence() {
14474 return FLAG_age_code &&
14475 prologue_offset() != Code::kPrologueOffsetNotSet &&
14476 (kind() == OPTIMIZED_FUNCTION ||
14477 (kind() == FUNCTION && !has_debug_break_slots()))
14478 ? instruction_start() + prologue_offset()
14479 : NULL;
14480}
14481
14482
14483Code::Age Code::GetAge() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014484 byte* sequence = FindCodeAgeSequence();
14485 if (sequence == NULL) {
14486 return kNoAgeCodeAge;
14487 }
14488 Age age;
14489 MarkingParity parity;
14490 GetCodeAgeAndParity(GetIsolate(), sequence, &age, &parity);
14491 return age;
14492}
14493
14494
14495void Code::GetCodeAgeAndParity(Code* code, Age* age,
14496 MarkingParity* parity) {
14497 Isolate* isolate = code->GetIsolate();
14498 Builtins* builtins = isolate->builtins();
14499 Code* stub = NULL;
14500#define HANDLE_CODE_AGE(AGE) \
14501 stub = *builtins->Make##AGE##CodeYoungAgainEvenMarking(); \
14502 if (code == stub) { \
14503 *age = k##AGE##CodeAge; \
14504 *parity = EVEN_MARKING_PARITY; \
14505 return; \
14506 } \
14507 stub = *builtins->Make##AGE##CodeYoungAgainOddMarking(); \
14508 if (code == stub) { \
14509 *age = k##AGE##CodeAge; \
14510 *parity = ODD_MARKING_PARITY; \
14511 return; \
14512 }
14513 CODE_AGE_LIST(HANDLE_CODE_AGE)
14514#undef HANDLE_CODE_AGE
14515 stub = *builtins->MarkCodeAsExecutedOnce();
14516 if (code == stub) {
14517 *age = kNotExecutedCodeAge;
14518 *parity = NO_MARKING_PARITY;
14519 return;
14520 }
14521 stub = *builtins->MarkCodeAsExecutedTwice();
14522 if (code == stub) {
14523 *age = kExecutedOnceCodeAge;
14524 *parity = NO_MARKING_PARITY;
14525 return;
14526 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014527 stub = *builtins->MarkCodeAsToBeExecutedOnce();
14528 if (code == stub) {
14529 *age = kToBeExecutedOnceCodeAge;
14530 *parity = NO_MARKING_PARITY;
14531 return;
14532 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014533 UNREACHABLE();
14534}
14535
14536
14537Code* Code::GetCodeAgeStub(Isolate* isolate, Age age, MarkingParity parity) {
14538 Builtins* builtins = isolate->builtins();
14539 switch (age) {
14540#define HANDLE_CODE_AGE(AGE) \
14541 case k##AGE##CodeAge: { \
14542 Code* stub = parity == EVEN_MARKING_PARITY \
14543 ? *builtins->Make##AGE##CodeYoungAgainEvenMarking() \
14544 : *builtins->Make##AGE##CodeYoungAgainOddMarking(); \
14545 return stub; \
14546 }
14547 CODE_AGE_LIST(HANDLE_CODE_AGE)
14548#undef HANDLE_CODE_AGE
14549 case kNotExecutedCodeAge: {
14550 DCHECK(parity == NO_MARKING_PARITY);
14551 return *builtins->MarkCodeAsExecutedOnce();
14552 }
14553 case kExecutedOnceCodeAge: {
14554 DCHECK(parity == NO_MARKING_PARITY);
14555 return *builtins->MarkCodeAsExecutedTwice();
14556 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014557 case kToBeExecutedOnceCodeAge: {
14558 DCHECK(parity == NO_MARKING_PARITY);
14559 return *builtins->MarkCodeAsToBeExecutedOnce();
14560 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014561 default:
14562 UNREACHABLE();
14563 break;
14564 }
14565 return NULL;
14566}
14567
14568
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014569void Code::PrintDeoptLocation(FILE* out, Address pc) {
14570 Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(this, pc);
14571 class SourcePosition pos = info.position;
14572 if (info.deopt_reason != Deoptimizer::kNoReason || !pos.IsUnknown()) {
14573 if (FLAG_hydrogen_track_positions) {
14574 PrintF(out, " ;;; deoptimize at %d_%d: %s\n",
14575 pos.inlining_id(), pos.position(),
14576 Deoptimizer::GetDeoptReason(info.deopt_reason));
14577 } else {
14578 PrintF(out, " ;;; deoptimize at %d: %s\n", pos.raw(),
14579 Deoptimizer::GetDeoptReason(info.deopt_reason));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014580 }
14581 }
14582}
14583
14584
14585bool Code::CanDeoptAt(Address pc) {
14586 DeoptimizationInputData* deopt_data =
14587 DeoptimizationInputData::cast(deoptimization_data());
14588 Address code_start_address = instruction_start();
14589 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
14590 if (deopt_data->Pc(i)->value() == -1) continue;
14591 Address address = code_start_address + deopt_data->Pc(i)->value();
Ben Murdoch097c5b22016-05-18 11:27:45 +010014592 if (address == pc && deopt_data->AstId(i) != BailoutId::None()) {
14593 return true;
14594 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014595 }
14596 return false;
14597}
14598
14599
14600// Identify kind of code.
14601const char* Code::Kind2String(Kind kind) {
14602 switch (kind) {
14603#define CASE(name) case name: return #name;
14604 CODE_KIND_LIST(CASE)
14605#undef CASE
14606 case NUMBER_OF_KINDS: break;
14607 }
14608 UNREACHABLE();
14609 return NULL;
14610}
14611
14612
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014613Handle<WeakCell> Code::WeakCellFor(Handle<Code> code) {
14614 DCHECK(code->kind() == OPTIMIZED_FUNCTION);
14615 WeakCell* raw_cell = code->CachedWeakCell();
14616 if (raw_cell != NULL) return Handle<WeakCell>(raw_cell);
14617 Handle<WeakCell> cell = code->GetIsolate()->factory()->NewWeakCell(code);
14618 DeoptimizationInputData::cast(code->deoptimization_data())
14619 ->SetWeakCellCache(*cell);
14620 return cell;
14621}
14622
14623
14624WeakCell* Code::CachedWeakCell() {
14625 DCHECK(kind() == OPTIMIZED_FUNCTION);
14626 Object* weak_cell_cache =
14627 DeoptimizationInputData::cast(deoptimization_data())->WeakCellCache();
14628 if (weak_cell_cache->IsWeakCell()) {
14629 DCHECK(this == WeakCell::cast(weak_cell_cache)->value());
14630 return WeakCell::cast(weak_cell_cache);
14631 }
14632 return NULL;
14633}
14634
14635
Steve Blocka7e24c12009-10-30 11:49:00 +000014636#ifdef ENABLE_DISASSEMBLER
Ben Murdochb0fe1622011-05-05 13:52:32 +010014637
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014638void DeoptimizationInputData::DeoptimizationInputDataPrint(
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014639 std::ostream& os) { // NOLINT
Ben Murdochb0fe1622011-05-05 13:52:32 +010014640 disasm::NameConverter converter;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014641 int const inlined_function_count = InlinedFunctionCount()->value();
14642 os << "Inlined functions (count = " << inlined_function_count << ")\n";
14643 for (int id = 0; id < inlined_function_count; ++id) {
14644 Object* info = LiteralArray()->get(id);
14645 os << " " << Brief(SharedFunctionInfo::cast(info)) << "\n";
14646 }
14647 os << "\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +010014648 int deopt_count = DeoptCount();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014649 os << "Deoptimization Input Data (deopt points = " << deopt_count << ")\n";
14650 if (0 != deopt_count) {
14651 os << " index ast id argc pc";
14652 if (FLAG_print_code_verbose) os << " commands";
14653 os << "\n";
14654 }
Ben Murdochb0fe1622011-05-05 13:52:32 +010014655 for (int i = 0; i < deopt_count; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014656 os << std::setw(6) << i << " " << std::setw(6) << AstId(i).ToInt() << " "
14657 << std::setw(6) << ArgumentsStackHeight(i)->value() << " "
14658 << std::setw(6) << Pc(i)->value();
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014659
14660 if (!FLAG_print_code_verbose) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014661 os << "\n";
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014662 continue;
14663 }
14664 // Print details of the frame translation.
Ben Murdochb0fe1622011-05-05 13:52:32 +010014665 int translation_index = TranslationIndex(i)->value();
14666 TranslationIterator iterator(TranslationByteArray(), translation_index);
14667 Translation::Opcode opcode =
14668 static_cast<Translation::Opcode>(iterator.Next());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014669 DCHECK(Translation::BEGIN == opcode);
Ben Murdochb0fe1622011-05-05 13:52:32 +010014670 int frame_count = iterator.Next();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010014671 int jsframe_count = iterator.Next();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014672 os << " " << Translation::StringFor(opcode)
14673 << " {frame count=" << frame_count
14674 << ", js frame count=" << jsframe_count << "}\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +010014675
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014676 while (iterator.HasNext() &&
14677 Translation::BEGIN !=
14678 (opcode = static_cast<Translation::Opcode>(iterator.Next()))) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014679 os << std::setw(31) << " " << Translation::StringFor(opcode) << " ";
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014680
14681 switch (opcode) {
14682 case Translation::BEGIN:
14683 UNREACHABLE();
14684 break;
14685
Ben Murdoch3ef787d2012-04-12 10:51:47 +010014686 case Translation::JS_FRAME: {
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014687 int ast_id = iterator.Next();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014688 int shared_info_id = iterator.Next();
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014689 unsigned height = iterator.Next();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014690 Object* shared_info = LiteralArray()->get(shared_info_id);
14691 os << "{ast_id=" << ast_id << ", function="
14692 << Brief(SharedFunctionInfo::cast(shared_info)->DebugName())
14693 << ", height=" << height << "}";
14694 break;
14695 }
14696
14697 case Translation::INTERPRETED_FRAME: {
14698 int bytecode_offset = iterator.Next();
14699 int shared_info_id = iterator.Next();
14700 unsigned height = iterator.Next();
14701 Object* shared_info = LiteralArray()->get(shared_info_id);
14702 os << "{bytecode_offset=" << bytecode_offset << ", function="
14703 << Brief(SharedFunctionInfo::cast(shared_info)->DebugName())
14704 << ", height=" << height << "}";
14705 break;
14706 }
14707
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014708 case Translation::COMPILED_STUB_FRAME: {
14709 Code::Kind stub_kind = static_cast<Code::Kind>(iterator.Next());
14710 os << "{kind=" << stub_kind << "}";
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014711 break;
14712 }
14713
Ben Murdoch3ef787d2012-04-12 10:51:47 +010014714 case Translation::ARGUMENTS_ADAPTOR_FRAME:
14715 case Translation::CONSTRUCT_STUB_FRAME: {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014716 int shared_info_id = iterator.Next();
14717 Object* shared_info = LiteralArray()->get(shared_info_id);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010014718 unsigned height = iterator.Next();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014719 os << "{function="
14720 << Brief(SharedFunctionInfo::cast(shared_info)->DebugName())
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014721 << ", height=" << height << "}";
Ben Murdoch3ef787d2012-04-12 10:51:47 +010014722 break;
14723 }
14724
Ben Murdochda12d292016-06-02 14:46:10 +010014725 case Translation::TAIL_CALLER_FRAME: {
14726 int shared_info_id = iterator.Next();
14727 Object* shared_info = LiteralArray()->get(shared_info_id);
14728 os << "{function="
14729 << Brief(SharedFunctionInfo::cast(shared_info)->DebugName())
14730 << "}";
14731 break;
14732 }
14733
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014734 case Translation::GETTER_STUB_FRAME:
14735 case Translation::SETTER_STUB_FRAME: {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014736 int shared_info_id = iterator.Next();
14737 Object* shared_info = LiteralArray()->get(shared_info_id);
14738 os << "{function=" << Brief(SharedFunctionInfo::cast(shared_info)
14739 ->DebugName()) << "}";
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014740 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014741 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014742
14743 case Translation::REGISTER: {
14744 int reg_code = iterator.Next();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014745 os << "{input=" << converter.NameOfCPURegister(reg_code) << "}";
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014746 break;
14747 }
14748
14749 case Translation::INT32_REGISTER: {
14750 int reg_code = iterator.Next();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014751 os << "{input=" << converter.NameOfCPURegister(reg_code) << "}";
14752 break;
14753 }
14754
14755 case Translation::UINT32_REGISTER: {
14756 int reg_code = iterator.Next();
14757 os << "{input=" << converter.NameOfCPURegister(reg_code)
14758 << " (unsigned)}";
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014759 break;
14760 }
14761
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014762 case Translation::BOOL_REGISTER: {
14763 int reg_code = iterator.Next();
14764 os << "{input=" << converter.NameOfCPURegister(reg_code)
14765 << " (bool)}";
14766 break;
14767 }
14768
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014769 case Translation::DOUBLE_REGISTER: {
14770 int reg_code = iterator.Next();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014771 os << "{input=" << DoubleRegister::from_code(reg_code).ToString()
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014772 << "}";
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014773 break;
14774 }
14775
14776 case Translation::STACK_SLOT: {
14777 int input_slot_index = iterator.Next();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014778 os << "{input=" << input_slot_index << "}";
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014779 break;
14780 }
14781
14782 case Translation::INT32_STACK_SLOT: {
14783 int input_slot_index = iterator.Next();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014784 os << "{input=" << input_slot_index << "}";
14785 break;
14786 }
14787
14788 case Translation::UINT32_STACK_SLOT: {
14789 int input_slot_index = iterator.Next();
14790 os << "{input=" << input_slot_index << " (unsigned)}";
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014791 break;
14792 }
14793
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014794 case Translation::BOOL_STACK_SLOT: {
14795 int input_slot_index = iterator.Next();
14796 os << "{input=" << input_slot_index << " (bool)}";
14797 break;
14798 }
14799
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014800 case Translation::DOUBLE_STACK_SLOT: {
14801 int input_slot_index = iterator.Next();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014802 os << "{input=" << input_slot_index << "}";
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014803 break;
14804 }
14805
14806 case Translation::LITERAL: {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014807 int literal_index = iterator.Next();
14808 Object* literal_value = LiteralArray()->get(literal_index);
14809 os << "{literal_id=" << literal_index << " (" << Brief(literal_value)
14810 << ")}";
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014811 break;
14812 }
14813
14814 case Translation::DUPLICATED_OBJECT: {
14815 int object_index = iterator.Next();
14816 os << "{object_index=" << object_index << "}";
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014817 break;
14818 }
14819
14820 case Translation::ARGUMENTS_OBJECT:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014821 case Translation::CAPTURED_OBJECT: {
14822 int args_length = iterator.Next();
14823 os << "{length=" << args_length << "}";
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014824 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014825 }
Ben Murdochb0fe1622011-05-05 13:52:32 +010014826 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014827 os << "\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +010014828 }
Ben Murdochb0fe1622011-05-05 13:52:32 +010014829 }
14830}
14831
14832
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014833void DeoptimizationOutputData::DeoptimizationOutputDataPrint(
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014834 std::ostream& os) { // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014835 os << "Deoptimization Output Data (deopt points = " << this->DeoptPoints()
14836 << ")\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +010014837 if (this->DeoptPoints() == 0) return;
14838
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014839 os << "ast id pc state\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +010014840 for (int i = 0; i < this->DeoptPoints(); i++) {
14841 int pc_and_state = this->PcAndState(i)->value();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014842 os << std::setw(6) << this->AstId(i).ToInt() << " " << std::setw(8)
14843 << FullCodeGenerator::PcField::decode(pc_and_state) << " "
14844 << FullCodeGenerator::State2String(
14845 FullCodeGenerator::StateField::decode(pc_and_state)) << "\n";
14846 }
14847}
14848
14849
14850void HandlerTable::HandlerTableRangePrint(std::ostream& os) {
14851 os << " from to hdlr\n";
14852 for (int i = 0; i < length(); i += kRangeEntrySize) {
14853 int pc_start = Smi::cast(get(i + kRangeStartIndex))->value();
14854 int pc_end = Smi::cast(get(i + kRangeEndIndex))->value();
14855 int handler_field = Smi::cast(get(i + kRangeHandlerIndex))->value();
14856 int handler_offset = HandlerOffsetField::decode(handler_field);
14857 CatchPrediction prediction = HandlerPredictionField::decode(handler_field);
Ben Murdoch097c5b22016-05-18 11:27:45 +010014858 int data = Smi::cast(get(i + kRangeDataIndex))->value();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014859 os << " (" << std::setw(4) << pc_start << "," << std::setw(4) << pc_end
14860 << ") -> " << std::setw(4) << handler_offset
Ben Murdoch097c5b22016-05-18 11:27:45 +010014861 << " (prediction=" << prediction << ", data=" << data << ")\n";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014862 }
14863}
14864
14865
14866void HandlerTable::HandlerTableReturnPrint(std::ostream& os) {
14867 os << " off hdlr (c)\n";
14868 for (int i = 0; i < length(); i += kReturnEntrySize) {
14869 int pc_offset = Smi::cast(get(i + kReturnOffsetIndex))->value();
14870 int handler_field = Smi::cast(get(i + kReturnHandlerIndex))->value();
14871 int handler_offset = HandlerOffsetField::decode(handler_field);
14872 CatchPrediction prediction = HandlerPredictionField::decode(handler_field);
14873 os << " " << std::setw(4) << pc_offset << " -> " << std::setw(4)
14874 << handler_offset << " (prediction=" << prediction << ")\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +010014875 }
14876}
14877
Ben Murdochb0fe1622011-05-05 13:52:32 +010014878
Steve Blocka7e24c12009-10-30 11:49:00 +000014879const char* Code::ICState2String(InlineCacheState state) {
14880 switch (state) {
14881 case UNINITIALIZED: return "UNINITIALIZED";
14882 case PREMONOMORPHIC: return "PREMONOMORPHIC";
14883 case MONOMORPHIC: return "MONOMORPHIC";
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014884 case PROTOTYPE_FAILURE:
14885 return "PROTOTYPE_FAILURE";
14886 case POLYMORPHIC: return "POLYMORPHIC";
Steve Blocka7e24c12009-10-30 11:49:00 +000014887 case MEGAMORPHIC: return "MEGAMORPHIC";
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014888 case GENERIC: return "GENERIC";
14889 case DEBUG_STUB: return "DEBUG_STUB";
Steve Blocka7e24c12009-10-30 11:49:00 +000014890 }
14891 UNREACHABLE();
14892 return NULL;
14893}
14894
14895
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014896const char* Code::StubType2String(StubType type) {
Steve Blocka7e24c12009-10-30 11:49:00 +000014897 switch (type) {
14898 case NORMAL: return "NORMAL";
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014899 case FAST: return "FAST";
Steve Blocka7e24c12009-10-30 11:49:00 +000014900 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +010014901 UNREACHABLE(); // keep the compiler happy
Steve Blocka7e24c12009-10-30 11:49:00 +000014902 return NULL;
14903}
14904
Ben Murdochb0fe1622011-05-05 13:52:32 +010014905
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014906void Code::PrintExtraICState(std::ostream& os, // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014907 Kind kind, ExtraICState extra) {
14908 os << "extra_ic_state = ";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014909 if ((kind == STORE_IC || kind == KEYED_STORE_IC) &&
14910 is_strict(static_cast<LanguageMode>(extra))) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014911 os << "STRICT\n";
Steve Block1e0659c2011-05-24 12:43:12 +010014912 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014913 os << extra << "\n";
Steve Block1e0659c2011-05-24 12:43:12 +010014914 }
14915}
14916
14917
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014918void Code::Disassemble(const char* name, std::ostream& os) { // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014919 os << "kind = " << Kind2String(kind()) << "\n";
14920 if (IsCodeStubOrIC()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014921 const char* n = CodeStub::MajorName(CodeStub::GetMajorKey(this));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014922 os << "major_key = " << (n == NULL ? "null" : n) << "\n";
14923 }
Steve Blocka7e24c12009-10-30 11:49:00 +000014924 if (is_inline_cache_stub()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014925 os << "ic_state = " << ICState2String(ic_state()) << "\n";
14926 PrintExtraICState(os, kind(), extra_ic_state());
Steve Blocka7e24c12009-10-30 11:49:00 +000014927 if (ic_state() == MONOMORPHIC) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014928 os << "type = " << StubType2String(type()) << "\n";
Steve Blocka7e24c12009-10-30 11:49:00 +000014929 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014930 if (is_compare_ic_stub()) {
14931 DCHECK(CodeStub::GetMajorKey(this) == CodeStub::CompareIC);
14932 CompareICStub stub(stub_key(), GetIsolate());
14933 os << "compare_state = " << CompareICState::GetStateName(stub.left())
14934 << "*" << CompareICState::GetStateName(stub.right()) << " -> "
14935 << CompareICState::GetStateName(stub.state()) << "\n";
14936 os << "compare_operation = " << Token::Name(stub.op()) << "\n";
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000014937 }
Steve Blocka7e24c12009-10-30 11:49:00 +000014938 }
Ben Murdochda12d292016-06-02 14:46:10 +010014939 if ((name != nullptr) && (name[0] != '\0')) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014940 os << "name = " << name << "\n";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014941 } else if (kind() == BUILTIN) {
14942 name = GetIsolate()->builtins()->Lookup(instruction_start());
Ben Murdochda12d292016-06-02 14:46:10 +010014943 if (name != nullptr) {
14944 os << "name = " << name << "\n";
14945 }
14946 } else if (kind() == BYTECODE_HANDLER) {
14947 name = GetIsolate()->interpreter()->LookupNameOfBytecodeHandler(this);
14948 if (name != nullptr) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014949 os << "name = " << name << "\n";
14950 }
Ben Murdochb0fe1622011-05-05 13:52:32 +010014951 }
14952 if (kind() == OPTIMIZED_FUNCTION) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014953 os << "stack_slots = " << stack_slots() << "\n";
Steve Blocka7e24c12009-10-30 11:49:00 +000014954 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014955 os << "compiler = " << (is_turbofanned()
14956 ? "turbofan"
14957 : is_crankshafted() ? "crankshaft"
14958 : kind() == Code::FUNCTION
14959 ? "full-codegen"
14960 : "unknown") << "\n";
Steve Blocka7e24c12009-10-30 11:49:00 +000014961
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014962 os << "Instructions (size = " << instruction_size() << ")\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014963 {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014964 Isolate* isolate = GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014965 int size = instruction_size();
14966 int safepoint_offset =
14967 is_crankshafted() ? static_cast<int>(safepoint_table_offset()) : size;
14968 int back_edge_offset = (kind() == Code::FUNCTION)
14969 ? static_cast<int>(back_edge_table_offset())
14970 : size;
14971 int constant_pool_offset = FLAG_enable_embedded_constant_pool
14972 ? this->constant_pool_offset()
14973 : size;
14974
14975 // Stop before reaching any embedded tables
14976 int code_size = Min(safepoint_offset, back_edge_offset);
14977 code_size = Min(code_size, constant_pool_offset);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014978 byte* begin = instruction_start();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014979 byte* end = begin + code_size;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014980 Disassembler::Decode(isolate, &os, begin, end, this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014981
14982 if (constant_pool_offset < size) {
14983 int constant_pool_size = size - constant_pool_offset;
14984 DCHECK((constant_pool_size & kPointerAlignmentMask) == 0);
14985 os << "\nConstant Pool (size = " << constant_pool_size << ")\n";
14986 Vector<char> buf = Vector<char>::New(50);
14987 intptr_t* ptr = reinterpret_cast<intptr_t*>(begin + constant_pool_offset);
14988 for (int i = 0; i < constant_pool_size; i += kPointerSize, ptr++) {
14989 SNPrintF(buf, "%4d %08" V8PRIxPTR, i, *ptr);
14990 os << static_cast<const void*>(ptr) << " " << buf.start() << "\n";
14991 }
14992 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014993 }
14994 os << "\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +010014995
Ben Murdochb0fe1622011-05-05 13:52:32 +010014996 if (kind() == FUNCTION) {
14997 DeoptimizationOutputData* data =
14998 DeoptimizationOutputData::cast(this->deoptimization_data());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014999 data->DeoptimizationOutputDataPrint(os);
Ben Murdochb0fe1622011-05-05 13:52:32 +010015000 } else if (kind() == OPTIMIZED_FUNCTION) {
15001 DeoptimizationInputData* data =
15002 DeoptimizationInputData::cast(this->deoptimization_data());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015003 data->DeoptimizationInputDataPrint(os);
Ben Murdochb0fe1622011-05-05 13:52:32 +010015004 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015005 os << "\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +010015006
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015007 if (is_crankshafted()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +010015008 SafepointTable table(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015009 os << "Safepoints (size = " << table.size() << ")\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +010015010 for (unsigned i = 0; i < table.length(); i++) {
15011 unsigned pc_offset = table.GetPcOffset(i);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040015012 os << static_cast<const void*>(instruction_start() + pc_offset) << " ";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015013 os << std::setw(4) << pc_offset << " ";
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015014 table.PrintEntry(i, os);
15015 os << " (sp -> fp) ";
Ben Murdochb8e0da22011-05-16 14:20:40 +010015016 SafepointEntry entry = table.GetEntry(i);
15017 if (entry.deoptimization_index() != Safepoint::kNoDeoptimizationIndex) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015018 os << std::setw(6) << entry.deoptimization_index();
Ben Murdochb0fe1622011-05-05 13:52:32 +010015019 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015020 os << "<none>";
Ben Murdochb0fe1622011-05-05 13:52:32 +010015021 }
Ben Murdochb8e0da22011-05-16 14:20:40 +010015022 if (entry.argument_count() > 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015023 os << " argc: " << entry.argument_count();
Ben Murdochb8e0da22011-05-16 14:20:40 +010015024 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015025 os << "\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +010015026 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015027 os << "\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +010015028 } else if (kind() == FUNCTION) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015029 unsigned offset = back_edge_table_offset();
15030 // If there is no back edge table, the "table start" will be at or after
Ben Murdochb0fe1622011-05-05 13:52:32 +010015031 // (due to alignment) the end of the instruction stream.
15032 if (static_cast<int>(offset) < instruction_size()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015033 DisallowHeapAllocation no_gc;
15034 BackEdgeTable back_edges(this, &no_gc);
15035
15036 os << "Back edges (size = " << back_edges.length() << ")\n";
15037 os << "ast_id pc_offset loop_depth\n";
15038
15039 for (uint32_t i = 0; i < back_edges.length(); i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015040 os << std::setw(6) << back_edges.ast_id(i).ToInt() << " "
15041 << std::setw(9) << back_edges.pc_offset(i) << " " << std::setw(10)
15042 << back_edges.loop_depth(i) << "\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +010015043 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015044
15045 os << "\n";
Ben Murdochb0fe1622011-05-05 13:52:32 +010015046 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015047#ifdef OBJECT_PRINT
15048 if (!type_feedback_info()->IsUndefined()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015049 TypeFeedbackInfo::cast(type_feedback_info())->TypeFeedbackInfoPrint(os);
15050 os << "\n";
15051 }
15052#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +010015053 }
Steve Blocka7e24c12009-10-30 11:49:00 +000015054
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015055 if (handler_table()->length() > 0) {
15056 os << "Handler Table (size = " << handler_table()->Size() << ")\n";
15057 if (kind() == FUNCTION) {
15058 HandlerTable::cast(handler_table())->HandlerTableRangePrint(os);
15059 } else if (kind() == OPTIMIZED_FUNCTION) {
15060 HandlerTable::cast(handler_table())->HandlerTableReturnPrint(os);
15061 }
15062 os << "\n";
15063 }
15064
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015065 os << "RelocInfo (size = " << relocation_size() << ")\n";
15066 for (RelocIterator it(this); !it.done(); it.next()) {
15067 it.rinfo()->Print(GetIsolate(), os);
15068 }
15069 os << "\n";
Steve Blocka7e24c12009-10-30 11:49:00 +000015070}
15071#endif // ENABLE_DISASSEMBLER
15072
Ben Murdoch097c5b22016-05-18 11:27:45 +010015073int BytecodeArray::SourcePosition(int offset) {
15074 int last_position = 0;
Ben Murdochda12d292016-06-02 14:46:10 +010015075 for (interpreter::SourcePositionTableIterator iterator(
15076 source_position_table());
Ben Murdoch097c5b22016-05-18 11:27:45 +010015077 !iterator.done() && iterator.bytecode_offset() <= offset;
15078 iterator.Advance()) {
15079 last_position = iterator.source_position();
15080 }
15081 return last_position;
15082}
15083
15084int BytecodeArray::SourceStatementPosition(int offset) {
15085 // First find the position as close as possible using all position
15086 // information.
15087 int position = SourcePosition(offset);
15088 // Now find the closest statement position before the position.
15089 int statement_position = 0;
Ben Murdochda12d292016-06-02 14:46:10 +010015090 interpreter::SourcePositionTableIterator iterator(source_position_table());
Ben Murdoch097c5b22016-05-18 11:27:45 +010015091 while (!iterator.done()) {
15092 if (iterator.is_statement()) {
15093 int p = iterator.source_position();
15094 if (statement_position < p && p <= position) {
15095 statement_position = p;
15096 }
15097 }
15098 iterator.Advance();
15099 }
15100 return statement_position;
15101}
Steve Blocka7e24c12009-10-30 11:49:00 +000015102
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015103void BytecodeArray::Disassemble(std::ostream& os) {
15104 os << "Parameter count " << parameter_count() << "\n";
15105 os << "Frame size " << frame_size() << "\n";
15106 Vector<char> buf = Vector<char>::New(50);
Steve Block8defd9f2010-07-08 12:39:36 +010015107
Ben Murdochda12d292016-06-02 14:46:10 +010015108 const uint8_t* base_address = GetFirstBytecodeAddress();
15109 interpreter::SourcePositionTableIterator source_positions(
15110 source_position_table());
Ben Murdoch097c5b22016-05-18 11:27:45 +010015111
Ben Murdochda12d292016-06-02 14:46:10 +010015112 interpreter::BytecodeArrayIterator iterator(handle(this));
15113 while (!iterator.done()) {
15114 if (!source_positions.done() &&
15115 iterator.current_offset() == source_positions.bytecode_offset()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010015116 os << std::setw(5) << source_positions.source_position();
15117 os << (source_positions.is_statement() ? " S> " : " E> ");
15118 source_positions.Advance();
15119 } else {
15120 os << " ";
15121 }
Ben Murdochda12d292016-06-02 14:46:10 +010015122 const uint8_t* current_address = base_address + iterator.current_offset();
15123 SNPrintF(buf, "%p", current_address);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015124 os << buf.start() << " : ";
Ben Murdochda12d292016-06-02 14:46:10 +010015125 interpreter::Bytecodes::Decode(os, current_address, parameter_count());
15126 if (interpreter::Bytecodes::IsJump(iterator.current_bytecode())) {
15127 SNPrintF(buf, " (%p)", base_address + iterator.GetJumpTargetOffset());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015128 os << buf.start();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015129 }
Ben Murdoch097c5b22016-05-18 11:27:45 +010015130 os << std::endl;
Ben Murdochda12d292016-06-02 14:46:10 +010015131 iterator.Advance();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010015132 }
15133
Ben Murdoch097c5b22016-05-18 11:27:45 +010015134 if (constant_pool()->length() > 0) {
15135 os << "Constant pool (size = " << constant_pool()->length() << ")\n";
15136 constant_pool()->Print();
15137 }
15138
15139#ifdef ENABLE_DISASSEMBLER
15140 if (handler_table()->length() > 0) {
15141 os << "Handler Table (size = " << handler_table()->Size() << ")\n";
15142 HandlerTable::cast(handler_table())->HandlerTableRangePrint(os);
15143 }
15144#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000015145}
15146
Ben Murdoch097c5b22016-05-18 11:27:45 +010015147void BytecodeArray::CopyBytecodesTo(BytecodeArray* to) {
15148 BytecodeArray* from = this;
15149 DCHECK_EQ(from->length(), to->length());
15150 CopyBytes(to->GetFirstBytecodeAddress(), from->GetFirstBytecodeAddress(),
15151 from->length());
15152}
Steve Blocka7e24c12009-10-30 11:49:00 +000015153
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015154// static
15155void JSArray::Initialize(Handle<JSArray> array, int capacity, int length) {
15156 DCHECK(capacity >= 0);
15157 array->GetIsolate()->factory()->NewJSArrayStorage(
15158 array, length, capacity, INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE);
15159}
15160
15161
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015162// Returns false if the passed-in index is marked non-configurable, which will
15163// cause the truncation operation to halt, and thus no further old values need
15164// be collected.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015165static bool GetOldValue(Isolate* isolate,
15166 Handle<JSObject> object,
15167 uint32_t index,
15168 List<Handle<Object> >* old_values,
15169 List<uint32_t>* indices) {
Ben Murdochda12d292016-06-02 14:46:10 +010015170 LookupIterator it(isolate, object, index, object, LookupIterator::HIDDEN);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015171 CHECK(JSReceiver::GetPropertyAttributes(&it).IsJust());
15172 DCHECK(it.IsFound());
15173 if (!it.IsConfigurable()) return false;
15174 Handle<Object> value =
15175 it.state() == LookupIterator::ACCESSOR
15176 ? Handle<Object>::cast(isolate->factory()->the_hole_value())
15177 : JSReceiver::GetDataProperty(&it);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015178 old_values->Add(value);
15179 indices->Add(index);
15180 return true;
15181}
15182
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015183
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015184void JSArray::SetLength(Handle<JSArray> array, uint32_t new_length) {
Steve Block3ce2e202009-11-05 08:53:23 +000015185 // We should never end in here with a pixel or external array.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015186 DCHECK(array->AllowsSetLength());
15187 if (array->SetLengthWouldNormalize(new_length)) {
15188 JSObject::NormalizeElements(array);
15189 }
15190 array->GetElementsAccessor()->SetLength(array, new_length);
15191}
15192
15193
15194MaybeHandle<Object> JSArray::ObservableSetLength(Handle<JSArray> array,
15195 uint32_t new_length) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015196 if (!array->map()->is_observed()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015197 SetLength(array, new_length);
15198 return array;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015199 }
15200
15201 Isolate* isolate = array->GetIsolate();
15202 List<uint32_t> indices;
15203 List<Handle<Object> > old_values;
15204 Handle<Object> old_length_handle(array->length(), isolate);
15205 uint32_t old_length = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015206 CHECK(old_length_handle->ToArrayLength(&old_length));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015207
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015208 int num_elements = array->NumberOfOwnElements(ALL_PROPERTIES);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015209 if (num_elements > 0) {
15210 if (old_length == static_cast<uint32_t>(num_elements)) {
15211 // Simple case for arrays without holes.
15212 for (uint32_t i = old_length - 1; i + 1 > new_length; --i) {
15213 if (!GetOldValue(isolate, array, i, &old_values, &indices)) break;
15214 }
15215 } else {
15216 // For sparse arrays, only iterate over existing elements.
15217 // TODO(rafaelw): For fast, sparse arrays, we can avoid iterating over
15218 // the to-be-removed indices twice.
15219 Handle<FixedArray> keys = isolate->factory()->NewFixedArray(num_elements);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015220 array->GetOwnElementKeys(*keys, ALL_PROPERTIES);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015221 while (num_elements-- > 0) {
15222 uint32_t index = NumberToUint32(keys->get(num_elements));
15223 if (index < new_length) break;
15224 if (!GetOldValue(isolate, array, index, &old_values, &indices)) break;
15225 }
15226 }
15227 }
15228
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015229 SetLength(array, new_length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015230
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015231 CHECK(array->length()->ToArrayLength(&new_length));
15232 if (old_length == new_length) return array;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015233
Emily Bernierd0a1eb72015-03-24 16:35:39 -040015234 RETURN_ON_EXCEPTION(isolate, BeginPerformSplice(array), Object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015235
15236 for (int i = 0; i < indices.length(); ++i) {
15237 // For deletions where the property was an accessor, old_values[i]
15238 // will be the hole, which instructs EnqueueChangeRecord to elide
15239 // the "oldValue" property.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040015240 RETURN_ON_EXCEPTION(
15241 isolate,
15242 JSObject::EnqueueChangeRecord(
15243 array, "delete", isolate->factory()->Uint32ToString(indices[i]),
15244 old_values[i]),
15245 Object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015246 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015247
Emily Bernierd0a1eb72015-03-24 16:35:39 -040015248 RETURN_ON_EXCEPTION(isolate,
15249 JSObject::EnqueueChangeRecord(
15250 array, "update", isolate->factory()->length_string(),
15251 old_length_handle),
15252 Object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015253
Emily Bernierd0a1eb72015-03-24 16:35:39 -040015254 RETURN_ON_EXCEPTION(isolate, EndPerformSplice(array), Object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015255
15256 uint32_t index = Min(old_length, new_length);
15257 uint32_t add_count = new_length > old_length ? new_length - old_length : 0;
15258 uint32_t delete_count = new_length < old_length ? old_length - new_length : 0;
15259 Handle<JSArray> deleted = isolate->factory()->NewJSArray(0);
15260 if (delete_count > 0) {
15261 for (int i = indices.length() - 1; i >= 0; i--) {
15262 // Skip deletions where the property was an accessor, leaving holes
15263 // in the array of old values.
15264 if (old_values[i]->IsTheHole()) continue;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015265 JSObject::AddDataElement(deleted, indices[i] - index, old_values[i], NONE)
15266 .Assert();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015267 }
15268
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015269 JSArray::SetLength(deleted, delete_count);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015270 }
15271
Emily Bernierd0a1eb72015-03-24 16:35:39 -040015272 RETURN_ON_EXCEPTION(
15273 isolate, EnqueueSpliceRecord(array, index, deleted, add_count), Object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015274
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015275 return array;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015276}
15277
15278
15279// static
15280void Map::AddDependentCode(Handle<Map> map,
15281 DependentCode::DependencyGroup group,
15282 Handle<Code> code) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015283 Handle<WeakCell> cell = Code::WeakCellFor(code);
15284 Handle<DependentCode> codes = DependentCode::InsertWeakCode(
15285 Handle<DependentCode>(map->dependent_code()), group, cell);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015286 if (*codes != map->dependent_code()) map->set_dependent_code(*codes);
15287}
15288
15289
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015290Handle<DependentCode> DependentCode::InsertCompilationDependencies(
15291 Handle<DependentCode> entries, DependencyGroup group,
15292 Handle<Foreign> info) {
15293 return Insert(entries, group, info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015294}
15295
15296
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015297Handle<DependentCode> DependentCode::InsertWeakCode(
15298 Handle<DependentCode> entries, DependencyGroup group,
15299 Handle<WeakCell> code_cell) {
15300 return Insert(entries, group, code_cell);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015301}
15302
15303
15304Handle<DependentCode> DependentCode::Insert(Handle<DependentCode> entries,
15305 DependencyGroup group,
15306 Handle<Object> object) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015307 if (entries->length() == 0 || entries->group() > group) {
15308 // There is no such group.
15309 return DependentCode::New(group, object, entries);
15310 }
15311 if (entries->group() < group) {
15312 // The group comes later in the list.
15313 Handle<DependentCode> old_next(entries->next_link());
15314 Handle<DependentCode> new_next = Insert(old_next, group, object);
15315 if (!old_next.is_identical_to(new_next)) {
15316 entries->set_next_link(*new_next);
15317 }
15318 return entries;
15319 }
15320 DCHECK_EQ(group, entries->group());
15321 int count = entries->count();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015322 // Check for existing entry to avoid duplicates.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015323 for (int i = 0; i < count; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015324 if (entries->object_at(i) == *object) return entries;
15325 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015326 if (entries->length() < kCodesStartIndex + count + 1) {
15327 entries = EnsureSpace(entries);
15328 // Count could have changed, reload it.
15329 count = entries->count();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015330 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015331 entries->set_object_at(count, *object);
15332 entries->set_count(count + 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015333 return entries;
15334}
15335
15336
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015337Handle<DependentCode> DependentCode::New(DependencyGroup group,
15338 Handle<Object> object,
15339 Handle<DependentCode> next) {
15340 Isolate* isolate = next->GetIsolate();
15341 Handle<DependentCode> result = Handle<DependentCode>::cast(
15342 isolate->factory()->NewFixedArray(kCodesStartIndex + 1, TENURED));
15343 result->set_next_link(*next);
15344 result->set_flags(GroupField::encode(group) | CountField::encode(1));
15345 result->set_object_at(0, *object);
15346 return result;
15347}
15348
15349
15350Handle<DependentCode> DependentCode::EnsureSpace(
15351 Handle<DependentCode> entries) {
15352 if (entries->Compact()) return entries;
15353 Isolate* isolate = entries->GetIsolate();
15354 int capacity = kCodesStartIndex + DependentCode::Grow(entries->count());
15355 int grow_by = capacity - entries->length();
15356 return Handle<DependentCode>::cast(
15357 isolate->factory()->CopyFixedArrayAndGrow(entries, grow_by, TENURED));
15358}
15359
15360
15361bool DependentCode::Compact() {
15362 int old_count = count();
15363 int new_count = 0;
15364 for (int i = 0; i < old_count; i++) {
15365 Object* obj = object_at(i);
15366 if (!obj->IsWeakCell() || !WeakCell::cast(obj)->cleared()) {
15367 if (i != new_count) {
15368 copy(i, new_count);
15369 }
15370 new_count++;
15371 }
15372 }
15373 set_count(new_count);
15374 for (int i = new_count; i < old_count; i++) {
15375 clear_at(i);
15376 }
15377 return new_count < old_count;
15378}
15379
15380
15381void DependentCode::UpdateToFinishedCode(DependencyGroup group, Foreign* info,
15382 WeakCell* code_cell) {
15383 if (this->length() == 0 || this->group() > group) {
15384 // There is no such group.
15385 return;
15386 }
15387 if (this->group() < group) {
15388 // The group comes later in the list.
15389 next_link()->UpdateToFinishedCode(group, info, code_cell);
15390 return;
15391 }
15392 DCHECK_EQ(group, this->group());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015393 DisallowHeapAllocation no_gc;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015394 int count = this->count();
15395 for (int i = 0; i < count; i++) {
15396 if (object_at(i) == info) {
15397 set_object_at(i, code_cell);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015398 break;
15399 }
15400 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000015401#ifdef DEBUG
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015402 for (int i = 0; i < count; i++) {
15403 DCHECK(object_at(i) != info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015404 }
15405#endif
15406}
15407
15408
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015409void DependentCode::RemoveCompilationDependencies(
15410 DependentCode::DependencyGroup group, Foreign* info) {
15411 if (this->length() == 0 || this->group() > group) {
15412 // There is no such group.
15413 return;
15414 }
15415 if (this->group() < group) {
15416 // The group comes later in the list.
15417 next_link()->RemoveCompilationDependencies(group, info);
15418 return;
15419 }
15420 DCHECK_EQ(group, this->group());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015421 DisallowHeapAllocation no_allocation;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015422 int old_count = count();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015423 // Find compilation info wrapper.
15424 int info_pos = -1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015425 for (int i = 0; i < old_count; i++) {
15426 if (object_at(i) == info) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015427 info_pos = i;
15428 break;
15429 }
15430 }
15431 if (info_pos == -1) return; // Not found.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015432 // Use the last code to fill the gap.
15433 if (info_pos < old_count - 1) {
15434 copy(old_count - 1, info_pos);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015435 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015436 clear_at(old_count - 1);
15437 set_count(old_count - 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015438
15439#ifdef DEBUG
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015440 for (int i = 0; i < old_count - 1; i++) {
15441 DCHECK(object_at(i) != info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015442 }
15443#endif
15444}
15445
15446
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015447bool DependentCode::Contains(DependencyGroup group, WeakCell* code_cell) {
15448 if (this->length() == 0 || this->group() > group) {
15449 // There is no such group.
15450 return false;
15451 }
15452 if (this->group() < group) {
15453 // The group comes later in the list.
15454 return next_link()->Contains(group, code_cell);
15455 }
15456 DCHECK_EQ(group, this->group());
15457 int count = this->count();
15458 for (int i = 0; i < count; i++) {
15459 if (object_at(i) == code_cell) return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015460 }
15461 return false;
15462}
15463
15464
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015465bool DependentCode::IsEmpty(DependencyGroup group) {
15466 if (this->length() == 0 || this->group() > group) {
15467 // There is no such group.
15468 return true;
15469 }
15470 if (this->group() < group) {
15471 // The group comes later in the list.
15472 return next_link()->IsEmpty(group);
15473 }
15474 DCHECK_EQ(group, this->group());
15475 return count() == 0;
15476}
15477
15478
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015479bool DependentCode::MarkCodeForDeoptimization(
15480 Isolate* isolate,
15481 DependentCode::DependencyGroup group) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015482 if (this->length() == 0 || this->group() > group) {
15483 // There is no such group.
15484 return false;
15485 }
15486 if (this->group() < group) {
15487 // The group comes later in the list.
15488 return next_link()->MarkCodeForDeoptimization(isolate, group);
15489 }
15490 DCHECK_EQ(group, this->group());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015491 DisallowHeapAllocation no_allocation_scope;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015492 // Mark all the code that needs to be deoptimized.
15493 bool marked = false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015494 bool invalidate_embedded_objects = group == kWeakCodeGroup;
15495 int count = this->count();
15496 for (int i = 0; i < count; i++) {
15497 Object* obj = object_at(i);
15498 if (obj->IsWeakCell()) {
15499 WeakCell* cell = WeakCell::cast(obj);
15500 if (cell->cleared()) continue;
15501 Code* code = Code::cast(cell->value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015502 if (!code->marked_for_deoptimization()) {
15503 SetMarkedForDeoptimization(code, group);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015504 if (invalidate_embedded_objects) {
15505 code->InvalidateEmbeddedObjects();
15506 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015507 marked = true;
15508 }
15509 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015510 DCHECK(obj->IsForeign());
15511 CompilationDependencies* info =
15512 reinterpret_cast<CompilationDependencies*>(
15513 Foreign::cast(obj)->foreign_address());
15514 info->Abort();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015515 }
15516 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015517 for (int i = 0; i < count; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015518 clear_at(i);
15519 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015520 set_count(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015521 return marked;
15522}
15523
15524
15525void DependentCode::DeoptimizeDependentCodeGroup(
15526 Isolate* isolate,
15527 DependentCode::DependencyGroup group) {
15528 DCHECK(AllowCodeDependencyChange::IsAllowed());
15529 DisallowHeapAllocation no_allocation_scope;
15530 bool marked = MarkCodeForDeoptimization(isolate, group);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015531 if (marked) Deoptimizer::DeoptimizeMarkedCode(isolate);
15532}
15533
15534
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015535void DependentCode::SetMarkedForDeoptimization(Code* code,
15536 DependencyGroup group) {
15537 code->set_marked_for_deoptimization(true);
15538 if (FLAG_trace_deopt &&
15539 (code->deoptimization_data() != code->GetHeap()->empty_fixed_array())) {
15540 DeoptimizationInputData* deopt_data =
15541 DeoptimizationInputData::cast(code->deoptimization_data());
15542 CodeTracer::Scope scope(code->GetHeap()->isolate()->GetCodeTracer());
15543 PrintF(scope.file(), "[marking dependent code 0x%08" V8PRIxPTR
15544 " (opt #%d) for deoptimization, reason: %s]\n",
15545 reinterpret_cast<intptr_t>(code),
15546 deopt_data->OptimizationId()->value(), DependencyGroupName(group));
15547 }
15548}
15549
15550
15551const char* DependentCode::DependencyGroupName(DependencyGroup group) {
15552 switch (group) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015553 case kWeakCodeGroup:
15554 return "weak-code";
15555 case kTransitionGroup:
15556 return "transition";
15557 case kPrototypeCheckGroup:
15558 return "prototype-check";
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015559 case kPropertyCellChangedGroup:
15560 return "property-cell-changed";
15561 case kFieldTypeGroup:
15562 return "field-type";
15563 case kInitialMapChangedGroup:
15564 return "initial-map-changed";
15565 case kAllocationSiteTenuringChangedGroup:
15566 return "allocation-site-tenuring-changed";
15567 case kAllocationSiteTransitionChangedGroup:
15568 return "allocation-site-transition-changed";
15569 }
15570 UNREACHABLE();
15571 return "?";
15572}
15573
15574
15575Handle<Map> Map::TransitionToPrototype(Handle<Map> map,
Emily Bernierd0a1eb72015-03-24 16:35:39 -040015576 Handle<Object> prototype,
15577 PrototypeOptimizationMode mode) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015578 Handle<Map> new_map = TransitionArray::GetPrototypeTransition(map, prototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015579 if (new_map.is_null()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040015580 new_map = Copy(map, "TransitionToPrototype");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015581 TransitionArray::PutPrototypeTransition(map, prototype, new_map);
15582 Map::SetPrototype(new_map, prototype, mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015583 }
15584 return new_map;
15585}
15586
15587
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015588Maybe<bool> JSReceiver::SetPrototype(Handle<JSReceiver> object,
15589 Handle<Object> value, bool from_javascript,
15590 ShouldThrow should_throw) {
15591 if (object->IsJSProxy()) {
15592 return JSProxy::SetPrototype(Handle<JSProxy>::cast(object), value,
15593 from_javascript, should_throw);
15594 }
15595 return JSObject::SetPrototype(Handle<JSObject>::cast(object), value,
15596 from_javascript, should_throw);
15597}
15598
15599
15600// ES6: 9.5.2 [[SetPrototypeOf]] (V)
15601// static
15602Maybe<bool> JSProxy::SetPrototype(Handle<JSProxy> proxy, Handle<Object> value,
15603 bool from_javascript,
15604 ShouldThrow should_throw) {
15605 Isolate* isolate = proxy->GetIsolate();
15606 STACK_CHECK(Nothing<bool>());
15607 Handle<Name> trap_name = isolate->factory()->setPrototypeOf_string();
15608 // 1. Assert: Either Type(V) is Object or Type(V) is Null.
15609 DCHECK(value->IsJSReceiver() || value->IsNull());
15610 // 2. Let handler be the value of the [[ProxyHandler]] internal slot of O.
15611 Handle<Object> handler(proxy->handler(), isolate);
15612 // 3. If handler is null, throw a TypeError exception.
15613 // 4. Assert: Type(handler) is Object.
15614 if (proxy->IsRevoked()) {
15615 isolate->Throw(*isolate->factory()->NewTypeError(
15616 MessageTemplate::kProxyRevoked, trap_name));
15617 return Nothing<bool>();
15618 }
15619 // 5. Let target be the value of the [[ProxyTarget]] internal slot.
15620 Handle<JSReceiver> target(proxy->target(), isolate);
15621 // 6. Let trap be ? GetMethod(handler, "getPrototypeOf").
15622 Handle<Object> trap;
15623 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
15624 isolate, trap,
15625 Object::GetMethod(Handle<JSReceiver>::cast(handler), trap_name),
15626 Nothing<bool>());
15627 // 7. If trap is undefined, then return target.[[SetPrototypeOf]]().
15628 if (trap->IsUndefined()) {
15629 return JSReceiver::SetPrototype(target, value, from_javascript,
15630 should_throw);
15631 }
15632 // 8. Let booleanTrapResult be ToBoolean(? Call(trap, handler, «target, V»)).
15633 Handle<Object> argv[] = {target, value};
15634 Handle<Object> trap_result;
15635 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
15636 isolate, trap_result,
15637 Execution::Call(isolate, trap, handler, arraysize(argv), argv),
15638 Nothing<bool>());
15639 bool bool_trap_result = trap_result->BooleanValue();
Ben Murdoch097c5b22016-05-18 11:27:45 +010015640 // 9. If booleanTrapResult is false, return false.
15641 if (!bool_trap_result) {
15642 RETURN_FAILURE(
15643 isolate, should_throw,
15644 NewTypeError(MessageTemplate::kProxyTrapReturnedFalsish, trap_name));
15645 }
15646 // 10. Let extensibleTarget be ? IsExtensible(target).
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015647 Maybe<bool> is_extensible = JSReceiver::IsExtensible(target);
15648 if (is_extensible.IsNothing()) return Nothing<bool>();
Ben Murdoch097c5b22016-05-18 11:27:45 +010015649 // 11. If extensibleTarget is true, return true.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015650 if (is_extensible.FromJust()) {
15651 if (bool_trap_result) return Just(true);
15652 RETURN_FAILURE(
15653 isolate, should_throw,
15654 NewTypeError(MessageTemplate::kProxyTrapReturnedFalsish, trap_name));
15655 }
Ben Murdoch097c5b22016-05-18 11:27:45 +010015656 // 12. Let targetProto be ? target.[[GetPrototypeOf]]().
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015657 Handle<Object> target_proto;
15658 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, target_proto,
Ben Murdoch097c5b22016-05-18 11:27:45 +010015659 JSReceiver::GetPrototype(isolate, target),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015660 Nothing<bool>());
Ben Murdoch097c5b22016-05-18 11:27:45 +010015661 // 13. If SameValue(V, targetProto) is false, throw a TypeError exception.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015662 if (bool_trap_result && !value->SameValue(*target_proto)) {
15663 isolate->Throw(*isolate->factory()->NewTypeError(
15664 MessageTemplate::kProxySetPrototypeOfNonExtensible));
15665 return Nothing<bool>();
15666 }
Ben Murdoch097c5b22016-05-18 11:27:45 +010015667 // 14. Return true.
15668 return Just(true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015669}
15670
15671
15672Maybe<bool> JSObject::SetPrototype(Handle<JSObject> object,
15673 Handle<Object> value, bool from_javascript,
15674 ShouldThrow should_throw) {
15675 Isolate* isolate = object->GetIsolate();
15676
15677 const bool observed = from_javascript && object->map()->is_observed();
15678 Handle<Object> old_value;
15679 if (observed) {
15680 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, old_value,
Ben Murdoch097c5b22016-05-18 11:27:45 +010015681 JSReceiver::GetPrototype(isolate, object),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015682 Nothing<bool>());
15683 }
15684
15685 Maybe<bool> result =
15686 SetPrototypeUnobserved(object, value, from_javascript, should_throw);
15687 MAYBE_RETURN(result, Nothing<bool>());
15688
15689 if (result.FromJust() && observed) {
15690 Handle<Object> new_value;
15691 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, new_value,
Ben Murdoch097c5b22016-05-18 11:27:45 +010015692 JSReceiver::GetPrototype(isolate, object),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015693 Nothing<bool>());
15694 if (!new_value->SameValue(*old_value)) {
15695 RETURN_ON_EXCEPTION_VALUE(
15696 isolate, JSObject::EnqueueChangeRecord(
15697 object, "setPrototype",
15698 isolate->factory()->proto_string(), old_value),
15699 Nothing<bool>());
15700 }
15701 }
15702
15703 return result;
15704}
15705
15706
15707Maybe<bool> JSObject::SetPrototypeUnobserved(Handle<JSObject> object,
15708 Handle<Object> value,
15709 bool from_javascript,
15710 ShouldThrow should_throw) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015711#ifdef DEBUG
15712 int size = object->Size();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000015713#endif
15714
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015715 Isolate* isolate = object->GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015716
15717 if (from_javascript) {
15718 if (object->IsAccessCheckNeeded() &&
15719 !isolate->MayAccess(handle(isolate->context()), object)) {
15720 isolate->ReportFailedAccessCheck(object);
15721 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>());
15722 RETURN_FAILURE(isolate, should_throw,
15723 NewTypeError(MessageTemplate::kNoAccess));
15724 }
15725 } else {
15726 DCHECK(!object->IsAccessCheckNeeded());
15727 }
15728
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015729 Heap* heap = isolate->heap();
Andrei Popescu402d9372010-02-26 13:31:12 +000015730 // Silently ignore the change if value is not a JSObject or null.
15731 // SpiderMonkey behaves this way.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015732 if (!value->IsJSReceiver() && !value->IsNull()) return Just(true);
15733
15734 bool dictionary_elements_in_chain =
15735 object->map()->DictionaryElementsInPrototypeChainOnly();
15736
15737 bool all_extensible = object->map()->is_extensible();
15738 Handle<JSObject> real_receiver = object;
15739 if (from_javascript) {
15740 // Find the first object in the chain whose prototype object is not
15741 // hidden.
Ben Murdoch097c5b22016-05-18 11:27:45 +010015742 PrototypeIterator iter(isolate, real_receiver,
15743 PrototypeIterator::START_AT_PROTOTYPE,
15744 PrototypeIterator::END_AT_NON_HIDDEN);
15745 while (!iter.IsAtEnd()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015746 // Casting to JSObject is fine because hidden prototypes are never
15747 // JSProxies.
15748 real_receiver = PrototypeIterator::GetCurrent<JSObject>(iter);
15749 iter.Advance();
15750 all_extensible = all_extensible && real_receiver->map()->is_extensible();
15751 }
15752 }
15753 Handle<Map> map(real_receiver->map());
15754
15755 // Nothing to do if prototype is already set.
15756 if (map->prototype() == *value) return Just(true);
Andrei Popescu402d9372010-02-26 13:31:12 +000015757
Ben Murdoch8b112d22011-06-08 16:22:53 +010015758 // From 8.6.2 Object Internal Methods
15759 // ...
15760 // In addition, if [[Extensible]] is false the value of the [[Class]] and
15761 // [[Prototype]] internal properties of the object may not be modified.
15762 // ...
15763 // Implementation specific extensions that modify [[Class]], [[Prototype]]
15764 // or [[Extensible]] must not violate the invariants defined in the preceding
15765 // paragraph.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015766 if (!all_extensible) {
15767 RETURN_FAILURE(isolate, should_throw,
15768 NewTypeError(MessageTemplate::kNonExtensibleProto, object));
Ben Murdoch8b112d22011-06-08 16:22:53 +010015769 }
15770
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015771 // Before we can set the prototype we need to be sure prototype cycles are
15772 // prevented. It is sufficient to validate that the receiver is not in the
15773 // new prototype chain.
Ben Murdoch097c5b22016-05-18 11:27:45 +010015774 if (value->IsJSReceiver()) {
15775 for (PrototypeIterator iter(isolate, JSReceiver::cast(*value),
15776 PrototypeIterator::START_AT_RECEIVER);
15777 !iter.IsAtEnd(); iter.Advance()) {
15778 if (iter.GetCurrent<JSReceiver>() == *object) {
15779 // Cycle detected.
15780 RETURN_FAILURE(isolate, should_throw,
15781 NewTypeError(MessageTemplate::kCyclicProto));
15782 }
Andrei Popescu402d9372010-02-26 13:31:12 +000015783 }
15784 }
15785
15786 // Set the new prototype of the object.
Steve Block053d10c2011-06-13 19:13:29 +010015787
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015788 isolate->UpdateArrayProtectorOnSetPrototype(real_receiver);
Steve Block053d10c2011-06-13 19:13:29 +010015789
Emily Bernierd0a1eb72015-03-24 16:35:39 -040015790 PrototypeOptimizationMode mode =
15791 from_javascript ? REGULAR_PROTOTYPE : FAST_PROTOTYPE;
15792 Handle<Map> new_map = Map::TransitionToPrototype(map, value, mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015793 DCHECK(new_map->prototype() == *value);
15794 JSObject::MigrateToMap(real_receiver, new_map);
15795
Emily Bernierd0a1eb72015-03-24 16:35:39 -040015796 if (from_javascript && !dictionary_elements_in_chain &&
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015797 new_map->DictionaryElementsInPrototypeChainOnly()) {
15798 // If the prototype chain didn't previously have element callbacks, then
15799 // KeyedStoreICs need to be cleared to ensure any that involve this
15800 // map go generic.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015801 TypeFeedbackVector::ClearAllKeyedStoreICs(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015802 }
Andrei Popescu402d9372010-02-26 13:31:12 +000015803
Steve Block44f0eee2011-05-26 01:26:41 +010015804 heap->ClearInstanceofCache();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015805 DCHECK(size == object->Size());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015806 return Just(true);
Andrei Popescu402d9372010-02-26 13:31:12 +000015807}
15808
15809
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015810void JSObject::EnsureCanContainElements(Handle<JSObject> object,
15811 Arguments* args,
15812 uint32_t first_arg,
15813 uint32_t arg_count,
15814 EnsureElementsMode mode) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010015815 // Elements in |Arguments| are ordered backwards (because they're on the
15816 // stack), but the method that's called here iterates over them in forward
15817 // direction.
15818 return EnsureCanContainElements(
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015819 object, args->arguments() - first_arg - (arg_count - 1), arg_count, mode);
Ben Murdoch592a9fc2012-03-05 11:04:45 +000015820}
15821
15822
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015823ElementsAccessor* JSObject::GetElementsAccessor() {
15824 return ElementsAccessor::ForKind(GetElementsKind());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015825}
15826
15827
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015828void JSObject::ValidateElements(Handle<JSObject> object) {
15829#ifdef ENABLE_SLOW_DCHECKS
15830 if (FLAG_enable_slow_asserts) {
15831 ElementsAccessor* accessor = object->GetElementsAccessor();
15832 accessor->Validate(object);
Steve Blocka7e24c12009-10-30 11:49:00 +000015833 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015834#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000015835}
15836
15837
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015838static bool ShouldConvertToSlowElements(JSObject* object, uint32_t capacity,
15839 uint32_t index,
15840 uint32_t* new_capacity) {
15841 STATIC_ASSERT(JSObject::kMaxUncheckedOldFastElementsLength <=
15842 JSObject::kMaxUncheckedFastElementsLength);
15843 if (index < capacity) {
15844 *new_capacity = capacity;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000015845 return false;
15846 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015847 if (index - capacity >= JSObject::kMaxGap) return true;
15848 *new_capacity = JSObject::NewElementsCapacity(index + 1);
15849 DCHECK_LT(index, *new_capacity);
15850 if (*new_capacity <= JSObject::kMaxUncheckedOldFastElementsLength ||
15851 (*new_capacity <= JSObject::kMaxUncheckedFastElementsLength &&
15852 object->GetHeap()->InNewSpace(object))) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000015853 return false;
15854 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015855 // If the fast-case backing storage takes up roughly three times as
15856 // much space (in machine words) as a dictionary backing storage
15857 // would, the object should have slow elements.
15858 int used_elements = object->GetFastElementsUsage();
15859 int dictionary_size = SeededNumberDictionary::ComputeCapacity(used_elements) *
15860 SeededNumberDictionary::kEntrySize;
15861 return 3 * static_cast<uint32_t>(dictionary_size) <= *new_capacity;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000015862}
15863
15864
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015865bool JSObject::WouldConvertToSlowElements(uint32_t index) {
15866 if (HasFastElements()) {
15867 Handle<FixedArrayBase> backing_store(FixedArrayBase::cast(elements()));
15868 uint32_t capacity = static_cast<uint32_t>(backing_store->length());
15869 uint32_t new_capacity;
15870 return ShouldConvertToSlowElements(this, capacity, index, &new_capacity);
15871 }
15872 return false;
15873}
15874
15875
15876static ElementsKind BestFittingFastElementsKind(JSObject* object) {
15877 if (object->HasSloppyArgumentsElements()) {
15878 return FAST_SLOPPY_ARGUMENTS_ELEMENTS;
15879 }
Ben Murdoch097c5b22016-05-18 11:27:45 +010015880 if (object->HasStringWrapperElements()) {
15881 return FAST_STRING_WRAPPER_ELEMENTS;
15882 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015883 DCHECK(object->HasDictionaryElements());
15884 SeededNumberDictionary* dictionary = object->element_dictionary();
15885 ElementsKind kind = FAST_HOLEY_SMI_ELEMENTS;
15886 for (int i = 0; i < dictionary->Capacity(); i++) {
15887 Object* key = dictionary->KeyAt(i);
15888 if (key->IsNumber()) {
15889 Object* value = dictionary->ValueAt(i);
15890 if (!value->IsNumber()) return FAST_HOLEY_ELEMENTS;
15891 if (!value->IsSmi()) {
15892 if (!FLAG_unbox_double_arrays) return FAST_HOLEY_ELEMENTS;
15893 kind = FAST_HOLEY_DOUBLE_ELEMENTS;
15894 }
15895 }
15896 }
15897 return kind;
15898}
15899
15900
15901static bool ShouldConvertToFastElements(JSObject* object,
15902 SeededNumberDictionary* dictionary,
15903 uint32_t index,
15904 uint32_t* new_capacity) {
15905 // If properties with non-standard attributes or accessors were added, we
15906 // cannot go back to fast elements.
15907 if (dictionary->requires_slow_elements()) return false;
15908
15909 // Adding a property with this index will require slow elements.
15910 if (index >= static_cast<uint32_t>(Smi::kMaxValue)) return false;
15911
15912 if (object->IsJSArray()) {
15913 Object* length = JSArray::cast(object)->length();
15914 if (!length->IsSmi()) return false;
15915 *new_capacity = static_cast<uint32_t>(Smi::cast(length)->value());
15916 } else {
15917 *new_capacity = dictionary->max_number_key() + 1;
15918 }
15919 *new_capacity = Max(index + 1, *new_capacity);
15920
15921 uint32_t dictionary_size = static_cast<uint32_t>(dictionary->Capacity()) *
15922 SeededNumberDictionary::kEntrySize;
15923
15924 // Turn fast if the dictionary only saves 50% space.
15925 return 2 * dictionary_size >= *new_capacity;
15926}
15927
15928
15929// static
15930MaybeHandle<Object> JSObject::AddDataElement(Handle<JSObject> object,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015931 uint32_t index,
15932 Handle<Object> value,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015933 PropertyAttributes attributes) {
15934 MAYBE_RETURN_NULL(
15935 AddDataElement(object, index, value, attributes, THROW_ON_ERROR));
Ben Murdoch3ef787d2012-04-12 10:51:47 +010015936 return value;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000015937}
15938
15939
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015940// static
15941Maybe<bool> JSObject::AddDataElement(Handle<JSObject> object, uint32_t index,
15942 Handle<Object> value,
15943 PropertyAttributes attributes,
15944 ShouldThrow should_throw) {
15945 DCHECK(object->map()->is_extensible());
15946
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015947 Isolate* isolate = object->GetIsolate();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000015948
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015949 uint32_t old_length = 0;
15950 uint32_t new_capacity = 0;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000015951
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015952 Handle<Object> old_length_handle;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015953 if (object->IsJSArray()) {
15954 CHECK(JSArray::cast(*object)->length()->ToArrayLength(&old_length));
15955 if (object->map()->is_observed()) {
15956 old_length_handle = handle(JSArray::cast(*object)->length(), isolate);
15957 }
15958 }
15959
15960 ElementsKind kind = object->GetElementsKind();
15961 FixedArrayBase* elements = object->elements();
15962 ElementsKind dictionary_kind = DICTIONARY_ELEMENTS;
15963 if (IsSloppyArgumentsElements(kind)) {
15964 elements = FixedArrayBase::cast(FixedArray::cast(elements)->get(1));
15965 dictionary_kind = SLOW_SLOPPY_ARGUMENTS_ELEMENTS;
Ben Murdoch097c5b22016-05-18 11:27:45 +010015966 } else if (IsStringWrapperElementsKind(kind)) {
15967 dictionary_kind = SLOW_STRING_WRAPPER_ELEMENTS;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015968 }
15969
15970 if (attributes != NONE) {
15971 kind = dictionary_kind;
15972 } else if (elements->IsSeededNumberDictionary()) {
15973 kind = ShouldConvertToFastElements(*object,
15974 SeededNumberDictionary::cast(elements),
15975 index, &new_capacity)
15976 ? BestFittingFastElementsKind(*object)
15977 : dictionary_kind; // Overwrite in case of arguments.
15978 } else if (ShouldConvertToSlowElements(
15979 *object, static_cast<uint32_t>(elements->length()), index,
15980 &new_capacity)) {
15981 kind = dictionary_kind;
15982 }
15983
15984 ElementsKind to = value->OptimalElementsKind();
15985 if (IsHoleyElementsKind(kind) || !object->IsJSArray() || index > old_length) {
15986 to = GetHoleyElementsKind(to);
15987 kind = GetHoleyElementsKind(kind);
15988 }
15989 to = GetMoreGeneralElementsKind(kind, to);
15990 ElementsAccessor* accessor = ElementsAccessor::ForKind(to);
15991 accessor->Add(object, index, value, attributes, new_capacity);
15992
15993 uint32_t new_length = old_length;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015994 Handle<Object> new_length_handle;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015995 if (object->IsJSArray() && index >= old_length) {
15996 new_length = index + 1;
15997 new_length_handle = isolate->factory()->NewNumberFromUint(new_length);
15998 JSArray::cast(*object)->set_length(*new_length_handle);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015999 }
16000
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016001 if (!old_length_handle.is_null() && new_length != old_length) {
16002 // |old_length_handle| is kept null above unless the object is observed.
16003 DCHECK(object->map()->is_observed());
16004 Handle<JSArray> array = Handle<JSArray>::cast(object);
16005 Handle<String> name = isolate->factory()->Uint32ToString(index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016006
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016007 RETURN_ON_EXCEPTION_VALUE(isolate, BeginPerformSplice(array),
16008 Nothing<bool>());
16009 RETURN_ON_EXCEPTION_VALUE(
16010 isolate, EnqueueChangeRecord(array, "add", name,
16011 isolate->factory()->the_hole_value()),
16012 Nothing<bool>());
16013 RETURN_ON_EXCEPTION_VALUE(
16014 isolate, EnqueueChangeRecord(array, "update",
16015 isolate->factory()->length_string(),
16016 old_length_handle),
16017 Nothing<bool>());
16018 RETURN_ON_EXCEPTION_VALUE(isolate, EndPerformSplice(array),
16019 Nothing<bool>());
16020 Handle<JSArray> deleted = isolate->factory()->NewJSArray(0);
16021 RETURN_ON_EXCEPTION_VALUE(isolate,
16022 EnqueueSpliceRecord(array, old_length, deleted,
16023 new_length - old_length),
16024 Nothing<bool>());
16025 } else if (object->map()->is_observed()) {
16026 Handle<String> name = isolate->factory()->Uint32ToString(index);
16027 RETURN_ON_EXCEPTION_VALUE(
16028 isolate, EnqueueChangeRecord(object, "add", name,
16029 isolate->factory()->the_hole_value()),
16030 Nothing<bool>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016031 }
16032
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016033 return Just(true);
Steve Blocka7e24c12009-10-30 11:49:00 +000016034}
16035
16036
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016037bool JSArray::SetLengthWouldNormalize(uint32_t new_length) {
16038 if (!HasFastElements()) return false;
16039 uint32_t capacity = static_cast<uint32_t>(elements()->length());
16040 uint32_t new_capacity;
16041 return JSArray::SetLengthWouldNormalize(GetHeap(), new_length) &&
16042 ShouldConvertToSlowElements(this, capacity, new_length - 1,
16043 &new_capacity);
Steve Blocka7e24c12009-10-30 11:49:00 +000016044}
16045
16046
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016047const double AllocationSite::kPretenureRatio = 0.85;
16048
16049
16050void AllocationSite::ResetPretenureDecision() {
16051 set_pretenure_decision(kUndecided);
16052 set_memento_found_count(0);
16053 set_memento_create_count(0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010016054}
16055
16056
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016057PretenureFlag AllocationSite::GetPretenureMode() {
16058 PretenureDecision mode = pretenure_decision();
16059 // Zombie objects "decide" to be untenured.
16060 return mode == kTenure ? TENURED : NOT_TENURED;
16061}
Ben Murdoch3ef787d2012-04-12 10:51:47 +010016062
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016063
16064bool AllocationSite::IsNestedSite() {
16065 DCHECK(FLAG_trace_track_allocation_sites);
16066 Object* current = GetHeap()->allocation_sites_list();
16067 while (current->IsAllocationSite()) {
16068 AllocationSite* current_site = AllocationSite::cast(current);
16069 if (current_site->nested_site() == this) {
16070 return true;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010016071 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016072 current = current_site->weak_next();
16073 }
16074 return false;
16075}
16076
16077
16078void AllocationSite::DigestTransitionFeedback(Handle<AllocationSite> site,
16079 ElementsKind to_kind) {
16080 Isolate* isolate = site->GetIsolate();
16081
16082 if (site->SitePointsToLiteral() && site->transition_info()->IsJSArray()) {
16083 Handle<JSArray> transition_info =
16084 handle(JSArray::cast(site->transition_info()));
16085 ElementsKind kind = transition_info->GetElementsKind();
16086 // if kind is holey ensure that to_kind is as well.
16087 if (IsHoleyElementsKind(kind)) {
16088 to_kind = GetHoleyElementsKind(to_kind);
16089 }
16090 if (IsMoreGeneralElementsKindTransition(kind, to_kind)) {
16091 // If the array is huge, it's not likely to be defined in a local
16092 // function, so we shouldn't make new instances of it very often.
16093 uint32_t length = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016094 CHECK(transition_info->length()->ToArrayLength(&length));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016095 if (length <= kMaximumArrayBytesToPretransition) {
16096 if (FLAG_trace_track_allocation_sites) {
16097 bool is_nested = site->IsNestedSite();
16098 PrintF(
16099 "AllocationSite: JSArray %p boilerplate %s updated %s->%s\n",
16100 reinterpret_cast<void*>(*site),
16101 is_nested ? "(nested)" : "",
16102 ElementsKindToString(kind),
16103 ElementsKindToString(to_kind));
16104 }
16105 JSObject::TransitionElementsKind(transition_info, to_kind);
16106 site->dependent_code()->DeoptimizeDependentCodeGroup(
16107 isolate, DependentCode::kAllocationSiteTransitionChangedGroup);
16108 }
16109 }
16110 } else {
16111 ElementsKind kind = site->GetElementsKind();
16112 // if kind is holey ensure that to_kind is as well.
16113 if (IsHoleyElementsKind(kind)) {
16114 to_kind = GetHoleyElementsKind(to_kind);
16115 }
16116 if (IsMoreGeneralElementsKindTransition(kind, to_kind)) {
16117 if (FLAG_trace_track_allocation_sites) {
16118 PrintF("AllocationSite: JSArray %p site updated %s->%s\n",
16119 reinterpret_cast<void*>(*site),
16120 ElementsKindToString(kind),
16121 ElementsKindToString(to_kind));
16122 }
16123 site->SetElementsKind(to_kind);
16124 site->dependent_code()->DeoptimizeDependentCodeGroup(
16125 isolate, DependentCode::kAllocationSiteTransitionChangedGroup);
16126 }
16127 }
16128}
16129
16130
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016131const char* AllocationSite::PretenureDecisionName(PretenureDecision decision) {
16132 switch (decision) {
16133 case kUndecided: return "undecided";
16134 case kDontTenure: return "don't tenure";
16135 case kMaybeTenure: return "maybe tenure";
16136 case kTenure: return "tenure";
16137 case kZombie: return "zombie";
16138 default: UNREACHABLE();
16139 }
16140 return NULL;
16141}
16142
16143
16144void JSObject::UpdateAllocationSite(Handle<JSObject> object,
16145 ElementsKind to_kind) {
16146 if (!object->IsJSArray()) return;
16147
16148 Heap* heap = object->GetHeap();
16149 if (!heap->InNewSpace(*object)) return;
16150
16151 Handle<AllocationSite> site;
16152 {
16153 DisallowHeapAllocation no_allocation;
16154
Ben Murdoch097c5b22016-05-18 11:27:45 +010016155 AllocationMemento* memento =
16156 heap->FindAllocationMemento<Heap::kForRuntime>(*object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016157 if (memento == NULL) return;
16158
16159 // Walk through to the Allocation Site
16160 site = handle(memento->GetAllocationSite());
16161 }
16162 AllocationSite::DigestTransitionFeedback(site, to_kind);
16163}
16164
16165
16166void JSObject::TransitionElementsKind(Handle<JSObject> object,
16167 ElementsKind to_kind) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016168 ElementsKind from_kind = object->GetElementsKind();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016169
16170 if (IsFastHoleyElementsKind(from_kind)) {
16171 to_kind = GetHoleyElementsKind(to_kind);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010016172 }
16173
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016174 if (from_kind == to_kind) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016175
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016176 // This method should never be called for any other case.
16177 DCHECK(IsFastElementsKind(from_kind));
16178 DCHECK(IsFastElementsKind(to_kind));
16179 DCHECK_NE(TERMINAL_FAST_ELEMENTS_KIND, from_kind);
16180
16181 UpdateAllocationSite(object, to_kind);
16182 if (object->elements() == object->GetHeap()->empty_fixed_array() ||
16183 IsFastDoubleElementsKind(from_kind) ==
16184 IsFastDoubleElementsKind(to_kind)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016185 // No change is needed to the elements() buffer, the transition
16186 // only requires a map change.
16187 Handle<Map> new_map = GetElementsTransitionMap(object, to_kind);
16188 MigrateToMap(object, new_map);
16189 if (FLAG_trace_elements_transitions) {
16190 Handle<FixedArrayBase> elms(object->elements());
16191 PrintElementsTransition(stdout, object, from_kind, elms, to_kind, elms);
16192 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016193 } else {
16194 DCHECK((IsFastSmiElementsKind(from_kind) &&
16195 IsFastDoubleElementsKind(to_kind)) ||
16196 (IsFastDoubleElementsKind(from_kind) &&
16197 IsFastObjectElementsKind(to_kind)));
16198 uint32_t c = static_cast<uint32_t>(object->elements()->length());
16199 ElementsAccessor::ForKind(to_kind)->GrowCapacityAndConvert(object, c);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016200 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +010016201}
16202
16203
16204// static
16205bool Map::IsValidElementsTransition(ElementsKind from_kind,
16206 ElementsKind to_kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016207 // Transitions can't go backwards.
16208 if (!IsMoreGeneralElementsKindTransition(from_kind, to_kind)) {
16209 return false;
16210 }
16211
16212 // Transitions from HOLEY -> PACKED are not allowed.
16213 return !IsFastHoleyElementsKind(from_kind) ||
16214 IsFastHoleyElementsKind(to_kind);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010016215}
16216
16217
Emily Bernierd0a1eb72015-03-24 16:35:39 -040016218bool JSArray::HasReadOnlyLength(Handle<JSArray> array) {
Ben Murdochda12d292016-06-02 14:46:10 +010016219 Map* map = array->map();
16220 // Fast path: "length" is the first fast property of arrays. Since it's not
16221 // configurable, it's guaranteed to be the first in the descriptor array.
16222 if (!map->is_dictionary_map()) {
16223 DCHECK(map->instance_descriptors()->GetKey(0) ==
16224 array->GetHeap()->length_string());
16225 return map->instance_descriptors()->GetDetails(0).IsReadOnly();
16226 }
16227
16228 Isolate* isolate = array->GetIsolate();
16229 LookupIterator it(array, isolate->factory()->length_string(), array,
Emily Bernierd0a1eb72015-03-24 16:35:39 -040016230 LookupIterator::OWN_SKIP_INTERCEPTOR);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040016231 CHECK_EQ(LookupIterator::ACCESSOR, it.state());
16232 return it.IsReadOnly();
16233}
16234
16235
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016236bool JSArray::WouldChangeReadOnlyLength(Handle<JSArray> array,
16237 uint32_t index) {
16238 uint32_t length = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016239 CHECK(array->length()->ToArrayLength(&length));
Emily Bernierd0a1eb72015-03-24 16:35:39 -040016240 if (length <= index) return HasReadOnlyLength(array);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016241 return false;
16242}
16243
16244
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016245template <typename BackingStore>
16246static int FastHoleyElementsUsage(JSObject* object, BackingStore* store) {
16247 int limit = object->IsJSArray()
16248 ? Smi::cast(JSArray::cast(object)->length())->value()
16249 : store->length();
Ben Murdoch69a99ed2011-11-30 16:03:39 +000016250 int used = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016251 for (int i = 0; i < limit; ++i) {
16252 if (!store->is_the_hole(i)) ++used;
16253 }
16254 return used;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000016255}
16256
16257
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016258int JSObject::GetFastElementsUsage() {
16259 FixedArrayBase* store = elements();
Steve Blocka7e24c12009-10-30 11:49:00 +000016260 switch (GetElementsKind()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016261 case FAST_SMI_ELEMENTS:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016262 case FAST_DOUBLE_ELEMENTS:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000016263 case FAST_ELEMENTS:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016264 return IsJSArray() ? Smi::cast(JSArray::cast(this)->length())->value()
16265 : store->length();
16266 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
16267 store = FixedArray::cast(FixedArray::cast(store)->get(1));
16268 // Fall through.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016269 case FAST_HOLEY_SMI_ELEMENTS:
16270 case FAST_HOLEY_ELEMENTS:
Ben Murdoch097c5b22016-05-18 11:27:45 +010016271 case FAST_STRING_WRAPPER_ELEMENTS:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016272 return FastHoleyElementsUsage(this, FixedArray::cast(store));
16273 case FAST_HOLEY_DOUBLE_ELEMENTS:
16274 if (elements()->length() == 0) return 0;
16275 return FastHoleyElementsUsage(this, FixedDoubleArray::cast(store));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016276
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016277 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
Ben Murdoch097c5b22016-05-18 11:27:45 +010016278 case SLOW_STRING_WRAPPER_ELEMENTS:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016279 case DICTIONARY_ELEMENTS:
Ben Murdoch097c5b22016-05-18 11:27:45 +010016280 case NO_ELEMENTS:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016281#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016282 case TYPE##_ELEMENTS: \
16283
16284 TYPED_ARRAYS(TYPED_ARRAY_CASE)
16285#undef TYPED_ARRAY_CASE
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016286 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +000016287 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016288 return 0;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000016289}
16290
16291
Steve Blocka7e24c12009-10-30 11:49:00 +000016292// Certain compilers request function template instantiation when they
16293// see the definition of the other template functions in the
16294// class. This requires us to have the template functions put
16295// together, so even though this function belongs in objects-debug.cc,
16296// we keep it here instead to satisfy certain compilers.
Ben Murdochb0fe1622011-05-05 13:52:32 +010016297#ifdef OBJECT_PRINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016298template <typename Derived, typename Shape, typename Key>
Emily Bernierd0a1eb72015-03-24 16:35:39 -040016299void Dictionary<Derived, Shape, Key>::Print(std::ostream& os) { // NOLINT
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016300 int capacity = this->Capacity();
Steve Blocka7e24c12009-10-30 11:49:00 +000016301 for (int i = 0; i < capacity; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016302 Object* k = this->KeyAt(i);
16303 if (this->IsKey(k)) {
16304 os << "\n ";
Steve Blocka7e24c12009-10-30 11:49:00 +000016305 if (k->IsString()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016306 String::cast(k)->StringPrint(os);
Steve Blocka7e24c12009-10-30 11:49:00 +000016307 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016308 os << Brief(k);
Steve Blocka7e24c12009-10-30 11:49:00 +000016309 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016310 os << ": " << Brief(this->ValueAt(i)) << " " << this->DetailsAt(i);
Steve Blocka7e24c12009-10-30 11:49:00 +000016311 }
16312 }
16313}
16314#endif
16315
16316
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016317template<typename Derived, typename Shape, typename Key>
16318void Dictionary<Derived, Shape, Key>::CopyValuesTo(FixedArray* elements) {
Steve Blocka7e24c12009-10-30 11:49:00 +000016319 int pos = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016320 int capacity = this->Capacity();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016321 DisallowHeapAllocation no_gc;
Leon Clarke4515c472010-02-03 11:58:03 +000016322 WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc);
Steve Blocka7e24c12009-10-30 11:49:00 +000016323 for (int i = 0; i < capacity; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016324 Object* k = this->KeyAt(i);
16325 if (this->IsKey(k)) {
16326 elements->set(pos++, this->ValueAt(i), mode);
Steve Blocka7e24c12009-10-30 11:49:00 +000016327 }
16328 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016329 DCHECK(pos == elements->length());
Steve Blocka7e24c12009-10-30 11:49:00 +000016330}
16331
16332
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016333MaybeHandle<Object> JSObject::GetPropertyWithInterceptor(LookupIterator* it,
16334 bool* done) {
16335 *done = false;
16336 Isolate* isolate = it->isolate();
16337 // Make sure that the top context does not change when doing callbacks or
16338 // interceptor calls.
16339 AssertNoContextChange ncc(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +000016340
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016341 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
16342 Handle<InterceptorInfo> interceptor = it->GetInterceptor();
16343 if (interceptor->getter()->IsUndefined()) {
16344 return isolate->factory()->undefined_value();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040016345 }
16346
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016347 Handle<JSObject> holder = it->GetHolder<JSObject>();
Ben Murdochda12d292016-06-02 14:46:10 +010016348 Handle<Object> result;
16349 Handle<Object> receiver = it->GetReceiver();
16350 if (!receiver->IsJSReceiver()) {
16351 ASSIGN_RETURN_ON_EXCEPTION(
16352 isolate, receiver, Object::ConvertReceiver(isolate, receiver), Object);
16353 }
16354 PropertyCallbackArguments args(isolate, interceptor->data(), *receiver,
16355 *holder, Object::DONT_THROW);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016356
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016357 if (it->IsElement()) {
16358 uint32_t index = it->index();
16359 v8::IndexedPropertyGetterCallback getter =
16360 v8::ToCData<v8::IndexedPropertyGetterCallback>(interceptor->getter());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016361 result = args.Call(getter, index);
16362 } else {
16363 Handle<Name> name = it->name();
16364 DCHECK(!name->IsPrivate());
16365
16366 if (name->IsSymbol() && !interceptor->can_intercept_symbols()) {
16367 return isolate->factory()->undefined_value();
16368 }
16369
16370 v8::GenericNamedPropertyGetterCallback getter =
16371 v8::ToCData<v8::GenericNamedPropertyGetterCallback>(
16372 interceptor->getter());
Ben Murdochda12d292016-06-02 14:46:10 +010016373 result = args.Call(getter, name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016374 }
16375
16376 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
Ben Murdochda12d292016-06-02 14:46:10 +010016377 if (result.is_null()) return isolate->factory()->undefined_value();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016378 *done = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016379 // Rebox handle before return
Ben Murdochda12d292016-06-02 14:46:10 +010016380 return handle(*result, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +000016381}
16382
16383
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016384Maybe<bool> JSObject::HasRealNamedProperty(Handle<JSObject> object,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016385 Handle<Name> name) {
16386 LookupIterator it = LookupIterator::PropertyOrElement(
16387 name->GetIsolate(), object, name, LookupIterator::OWN_SKIP_INTERCEPTOR);
16388 return HasProperty(&it);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016389}
16390
16391
16392Maybe<bool> JSObject::HasRealElementProperty(Handle<JSObject> object,
16393 uint32_t index) {
16394 Isolate* isolate = object->GetIsolate();
Ben Murdochda12d292016-06-02 14:46:10 +010016395 LookupIterator it(isolate, object, index, object,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016396 LookupIterator::OWN_SKIP_INTERCEPTOR);
16397 return HasProperty(&it);
Steve Blocka7e24c12009-10-30 11:49:00 +000016398}
16399
16400
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016401Maybe<bool> JSObject::HasRealNamedCallbackProperty(Handle<JSObject> object,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016402 Handle<Name> name) {
16403 LookupIterator it = LookupIterator::PropertyOrElement(
16404 name->GetIsolate(), object, name, LookupIterator::OWN_SKIP_INTERCEPTOR);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016405 Maybe<PropertyAttributes> maybe_result = GetPropertyAttributes(&it);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016406 return maybe_result.IsJust() ? Just(it.state() == LookupIterator::ACCESSOR)
16407 : Nothing<bool>();
Steve Blocka7e24c12009-10-30 11:49:00 +000016408}
16409
16410
16411void FixedArray::SwapPairs(FixedArray* numbers, int i, int j) {
16412 Object* temp = get(i);
16413 set(i, get(j));
16414 set(j, temp);
16415 if (this != numbers) {
16416 temp = numbers->get(i);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010016417 numbers->set(i, Smi::cast(numbers->get(j)));
16418 numbers->set(j, Smi::cast(temp));
Steve Blocka7e24c12009-10-30 11:49:00 +000016419 }
16420}
16421
16422
16423static void InsertionSortPairs(FixedArray* content,
16424 FixedArray* numbers,
16425 int len) {
16426 for (int i = 1; i < len; i++) {
16427 int j = i;
16428 while (j > 0 &&
16429 (NumberToUint32(numbers->get(j - 1)) >
16430 NumberToUint32(numbers->get(j)))) {
16431 content->SwapPairs(numbers, j - 1, j);
16432 j--;
16433 }
16434 }
16435}
16436
16437
16438void HeapSortPairs(FixedArray* content, FixedArray* numbers, int len) {
16439 // In-place heap sort.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016440 DCHECK(content->length() == numbers->length());
Steve Blocka7e24c12009-10-30 11:49:00 +000016441
16442 // Bottom-up max-heap construction.
16443 for (int i = 1; i < len; ++i) {
16444 int child_index = i;
16445 while (child_index > 0) {
16446 int parent_index = ((child_index + 1) >> 1) - 1;
16447 uint32_t parent_value = NumberToUint32(numbers->get(parent_index));
16448 uint32_t child_value = NumberToUint32(numbers->get(child_index));
16449 if (parent_value < child_value) {
16450 content->SwapPairs(numbers, parent_index, child_index);
16451 } else {
16452 break;
16453 }
16454 child_index = parent_index;
16455 }
16456 }
16457
16458 // Extract elements and create sorted array.
16459 for (int i = len - 1; i > 0; --i) {
16460 // Put max element at the back of the array.
16461 content->SwapPairs(numbers, 0, i);
16462 // Sift down the new top element.
16463 int parent_index = 0;
16464 while (true) {
16465 int child_index = ((parent_index + 1) << 1) - 1;
16466 if (child_index >= i) break;
16467 uint32_t child1_value = NumberToUint32(numbers->get(child_index));
16468 uint32_t child2_value = NumberToUint32(numbers->get(child_index + 1));
16469 uint32_t parent_value = NumberToUint32(numbers->get(parent_index));
16470 if (child_index + 1 >= i || child1_value > child2_value) {
16471 if (parent_value > child1_value) break;
16472 content->SwapPairs(numbers, parent_index, child_index);
16473 parent_index = child_index;
16474 } else {
16475 if (parent_value > child2_value) break;
16476 content->SwapPairs(numbers, parent_index, child_index + 1);
16477 parent_index = child_index + 1;
16478 }
16479 }
16480 }
16481}
16482
16483
16484// Sort this array and the numbers as pairs wrt. the (distinct) numbers.
16485void FixedArray::SortPairs(FixedArray* numbers, uint32_t len) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016486 DCHECK(this->length() == numbers->length());
Steve Blocka7e24c12009-10-30 11:49:00 +000016487 // For small arrays, simply use insertion sort.
16488 if (len <= 10) {
16489 InsertionSortPairs(this, numbers, len);
16490 return;
16491 }
16492 // Check the range of indices.
16493 uint32_t min_index = NumberToUint32(numbers->get(0));
16494 uint32_t max_index = min_index;
16495 uint32_t i;
16496 for (i = 1; i < len; i++) {
16497 if (NumberToUint32(numbers->get(i)) < min_index) {
16498 min_index = NumberToUint32(numbers->get(i));
16499 } else if (NumberToUint32(numbers->get(i)) > max_index) {
16500 max_index = NumberToUint32(numbers->get(i));
16501 }
16502 }
16503 if (max_index - min_index + 1 == len) {
16504 // Indices form a contiguous range, unless there are duplicates.
16505 // Do an in-place linear time sort assuming distinct numbers, but
16506 // avoid hanging in case they are not.
16507 for (i = 0; i < len; i++) {
16508 uint32_t p;
16509 uint32_t j = 0;
16510 // While the current element at i is not at its correct position p,
16511 // swap the elements at these two positions.
16512 while ((p = NumberToUint32(numbers->get(i)) - min_index) != i &&
16513 j++ < len) {
16514 SwapPairs(numbers, i, p);
16515 }
16516 }
16517 } else {
16518 HeapSortPairs(this, numbers, len);
16519 return;
16520 }
16521}
16522
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016523void JSObject::CollectOwnPropertyNames(KeyAccumulator* keys,
16524 PropertyFilter filter) {
Steve Blocka7e24c12009-10-30 11:49:00 +000016525 if (HasFastProperties()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016526 int real_size = map()->NumberOfOwnDescriptors();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016527 Handle<DescriptorArray> descs(map()->instance_descriptors());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016528 for (int i = 0; i < real_size; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016529 PropertyDetails details = descs->GetDetails(i);
16530 if ((details.attributes() & filter) != 0) continue;
16531 if (filter & ONLY_ALL_CAN_READ) {
16532 if (details.kind() != kAccessor) continue;
16533 Object* accessors = descs->GetValue(i);
16534 if (!accessors->IsAccessorInfo()) continue;
16535 if (!AccessorInfo::cast(accessors)->all_can_read()) continue;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016536 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016537 Name* key = descs->GetKey(i);
16538 if (key->FilterKey(filter)) continue;
Ben Murdoch097c5b22016-05-18 11:27:45 +010016539 keys->AddKey(key, DO_NOT_CONVERT);
Steve Blocka7e24c12009-10-30 11:49:00 +000016540 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016541 } else if (IsJSGlobalObject()) {
16542 GlobalDictionary::CollectKeysTo(handle(global_dictionary()), keys, filter);
Steve Blocka7e24c12009-10-30 11:49:00 +000016543 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016544 NameDictionary::CollectKeysTo(handle(property_dictionary()), keys, filter);
Steve Blocka7e24c12009-10-30 11:49:00 +000016545 }
16546}
16547
16548
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016549int JSObject::NumberOfOwnElements(PropertyFilter filter) {
16550 // Fast case for objects with no elements.
16551 if (!IsJSValue() && HasFastElements()) {
16552 uint32_t length =
16553 IsJSArray()
16554 ? static_cast<uint32_t>(
16555 Smi::cast(JSArray::cast(this)->length())->value())
16556 : static_cast<uint32_t>(FixedArrayBase::cast(elements())->length());
16557 if (length == 0) return 0;
16558 }
16559 // Compute the number of enumerable elements.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016560 return GetOwnElementKeys(NULL, filter);
Steve Blocka7e24c12009-10-30 11:49:00 +000016561}
16562
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016563void JSObject::CollectOwnElementKeys(Handle<JSObject> object,
16564 KeyAccumulator* keys,
16565 PropertyFilter filter) {
16566 if (filter & SKIP_STRINGS) return;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016567 ElementsAccessor* accessor = object->GetElementsAccessor();
16568 accessor->CollectElementIndices(object, keys, kMaxUInt32, filter, 0);
Steve Blocka7e24c12009-10-30 11:49:00 +000016569}
16570
16571
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016572int JSObject::GetOwnElementKeys(FixedArray* storage, PropertyFilter filter) {
Steve Blocka7e24c12009-10-30 11:49:00 +000016573 int counter = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016574
16575 // If this is a String wrapper, add the string indices first,
16576 // as they're guaranteed to precede the elements in numerical order
16577 // and ascending order is required by ECMA-262, 6th, 9.1.12.
16578 if (IsJSValue()) {
16579 Object* val = JSValue::cast(this)->value();
16580 if (val->IsString()) {
16581 String* str = String::cast(val);
16582 if (storage) {
16583 for (int i = 0; i < str->length(); i++) {
16584 storage->set(counter + i, Smi::FromInt(i));
16585 }
16586 }
16587 counter += str->length();
16588 }
16589 }
16590
Steve Blocka7e24c12009-10-30 11:49:00 +000016591 switch (GetElementsKind()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016592 case FAST_SMI_ELEMENTS:
16593 case FAST_ELEMENTS:
16594 case FAST_HOLEY_SMI_ELEMENTS:
Ben Murdoch097c5b22016-05-18 11:27:45 +010016595 case FAST_HOLEY_ELEMENTS:
16596 case FAST_STRING_WRAPPER_ELEMENTS: {
Steve Blocka7e24c12009-10-30 11:49:00 +000016597 int length = IsJSArray() ?
16598 Smi::cast(JSArray::cast(this)->length())->value() :
16599 FixedArray::cast(elements())->length();
16600 for (int i = 0; i < length; i++) {
16601 if (!FixedArray::cast(elements())->get(i)->IsTheHole()) {
16602 if (storage != NULL) {
Leon Clarke4515c472010-02-03 11:58:03 +000016603 storage->set(counter, Smi::FromInt(i));
Steve Blocka7e24c12009-10-30 11:49:00 +000016604 }
16605 counter++;
16606 }
16607 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016608 DCHECK(!storage || storage->length() >= counter);
Steve Blocka7e24c12009-10-30 11:49:00 +000016609 break;
16610 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016611 case FAST_DOUBLE_ELEMENTS:
16612 case FAST_HOLEY_DOUBLE_ELEMENTS: {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000016613 int length = IsJSArray() ?
16614 Smi::cast(JSArray::cast(this)->length())->value() :
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016615 FixedArrayBase::cast(elements())->length();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000016616 for (int i = 0; i < length; i++) {
16617 if (!FixedDoubleArray::cast(elements())->is_the_hole(i)) {
16618 if (storage != NULL) {
16619 storage->set(counter, Smi::FromInt(i));
16620 }
16621 counter++;
16622 }
16623 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016624 DCHECK(!storage || storage->length() >= counter);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000016625 break;
16626 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016627
16628#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016629 case TYPE##_ELEMENTS: \
16630
16631 TYPED_ARRAYS(TYPED_ARRAY_CASE)
16632#undef TYPED_ARRAY_CASE
16633 {
16634 int length = FixedArrayBase::cast(elements())->length();
Steve Blocka7e24c12009-10-30 11:49:00 +000016635 while (counter < length) {
16636 if (storage != NULL) {
Leon Clarke4515c472010-02-03 11:58:03 +000016637 storage->set(counter, Smi::FromInt(counter));
Steve Blocka7e24c12009-10-30 11:49:00 +000016638 }
16639 counter++;
16640 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016641 DCHECK(!storage || storage->length() >= counter);
Steve Blocka7e24c12009-10-30 11:49:00 +000016642 break;
16643 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016644
Ben Murdoch097c5b22016-05-18 11:27:45 +010016645 case DICTIONARY_ELEMENTS:
16646 case SLOW_STRING_WRAPPER_ELEMENTS: {
Steve Blocka7e24c12009-10-30 11:49:00 +000016647 if (storage != NULL) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016648 element_dictionary()->CopyKeysTo(storage, counter, filter,
Ben Murdochc7cc0282012-03-05 14:35:55 +000016649 SeededNumberDictionary::SORTED);
Steve Blocka7e24c12009-10-30 11:49:00 +000016650 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000016651 counter += element_dictionary()->NumberOfElementsFilterAttributes(filter);
Steve Blocka7e24c12009-10-30 11:49:00 +000016652 break;
16653 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016654 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
16655 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000016656 FixedArray* parameter_map = FixedArray::cast(elements());
16657 int mapped_length = parameter_map->length() - 2;
16658 FixedArray* arguments = FixedArray::cast(parameter_map->get(1));
16659 if (arguments->IsDictionary()) {
16660 // Copy the keys from arguments first, because Dictionary::CopyKeysTo
16661 // will insert in storage starting at index 0.
Ben Murdochc7cc0282012-03-05 14:35:55 +000016662 SeededNumberDictionary* dictionary =
16663 SeededNumberDictionary::cast(arguments);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000016664 if (storage != NULL) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016665 dictionary->CopyKeysTo(storage, counter, filter,
16666 SeededNumberDictionary::UNSORTED);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000016667 }
16668 counter += dictionary->NumberOfElementsFilterAttributes(filter);
16669 for (int i = 0; i < mapped_length; ++i) {
16670 if (!parameter_map->get(i + 2)->IsTheHole()) {
16671 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
16672 ++counter;
16673 }
16674 }
16675 if (storage != NULL) storage->SortPairs(storage, counter);
16676
16677 } else {
16678 int backing_length = arguments->length();
16679 int i = 0;
16680 for (; i < mapped_length; ++i) {
16681 if (!parameter_map->get(i + 2)->IsTheHole()) {
16682 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
16683 ++counter;
16684 } else if (i < backing_length && !arguments->get(i)->IsTheHole()) {
16685 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
16686 ++counter;
16687 }
16688 }
16689 for (; i < backing_length; ++i) {
16690 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
16691 ++counter;
16692 }
16693 }
Steve Blocka7e24c12009-10-30 11:49:00 +000016694 break;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000016695 }
Ben Murdoch097c5b22016-05-18 11:27:45 +010016696 case NO_ELEMENTS:
16697 break;
Steve Blocka7e24c12009-10-30 11:49:00 +000016698 }
16699
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016700 DCHECK(!storage || storage->length() == counter);
Steve Blocka7e24c12009-10-30 11:49:00 +000016701 return counter;
16702}
16703
16704
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016705MaybeHandle<String> Object::ObjectProtoToString(Isolate* isolate,
16706 Handle<Object> object) {
16707 if (object->IsUndefined()) return isolate->factory()->undefined_to_string();
16708 if (object->IsNull()) return isolate->factory()->null_to_string();
16709
Ben Murdoch097c5b22016-05-18 11:27:45 +010016710 Handle<JSReceiver> receiver =
16711 Object::ToObject(isolate, object).ToHandleChecked();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016712
16713 Handle<String> tag;
Ben Murdochda12d292016-06-02 14:46:10 +010016714 Handle<Object> to_string_tag;
16715 ASSIGN_RETURN_ON_EXCEPTION(
16716 isolate, to_string_tag,
16717 JSReceiver::GetProperty(receiver,
16718 isolate->factory()->to_string_tag_symbol()),
16719 String);
16720 if (to_string_tag->IsString()) {
16721 tag = Handle<String>::cast(to_string_tag);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016722 }
16723
16724 if (tag.is_null()) {
16725 ASSIGN_RETURN_ON_EXCEPTION(isolate, tag,
16726 JSReceiver::BuiltinStringTag(receiver), String);
16727 }
16728
16729 IncrementalStringBuilder builder(isolate);
16730 builder.AppendCString("[object ");
16731 builder.AppendString(tag);
16732 builder.AppendCharacter(']');
16733 return builder.Finish();
Steve Blocka7e24c12009-10-30 11:49:00 +000016734}
16735
16736
Emily Bernierd0a1eb72015-03-24 16:35:39 -040016737const char* Symbol::PrivateSymbolToName() const {
16738 Heap* heap = GetIsolate()->heap();
16739#define SYMBOL_CHECK_AND_PRINT(name) \
16740 if (this == heap->name()) return #name;
16741 PRIVATE_SYMBOL_LIST(SYMBOL_CHECK_AND_PRINT)
16742#undef SYMBOL_CHECK_AND_PRINT
16743 return "UNKNOWN";
16744}
16745
16746
16747void Symbol::SymbolShortPrint(std::ostream& os) {
16748 os << "<Symbol: " << Hash();
16749 if (!name()->IsUndefined()) {
16750 os << " ";
16751 HeapStringAllocator allocator;
16752 StringStream accumulator(&allocator);
16753 String::cast(name())->StringShortPrint(&accumulator);
16754 os << accumulator.ToCString().get();
16755 } else {
16756 os << " (" << PrivateSymbolToName() << ")";
16757 }
16758 os << ">";
16759}
16760
16761
Steve Blocka7e24c12009-10-30 11:49:00 +000016762// StringSharedKeys are used as keys in the eval cache.
16763class StringSharedKey : public HashTableKey {
16764 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016765 StringSharedKey(Handle<String> source, Handle<SharedFunctionInfo> shared,
16766 LanguageMode language_mode, int scope_position)
Steve Block1e0659c2011-05-24 12:43:12 +010016767 : source_(source),
16768 shared_(shared),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016769 language_mode_(language_mode),
16770 scope_position_(scope_position) {}
Steve Blocka7e24c12009-10-30 11:49:00 +000016771
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016772 bool IsMatch(Object* other) override {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016773 DisallowHeapAllocation no_allocation;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040016774 if (!other->IsFixedArray()) {
16775 if (!other->IsNumber()) return false;
16776 uint32_t other_hash = static_cast<uint32_t>(other->Number());
16777 return Hash() == other_hash;
16778 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +010016779 FixedArray* other_array = FixedArray::cast(other);
16780 SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016781 if (shared != *shared_) return false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016782 int language_unchecked = Smi::cast(other_array->get(2))->value();
16783 DCHECK(is_valid_language_mode(language_unchecked));
16784 LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked);
16785 if (language_mode != language_mode_) return false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010016786 int scope_position = Smi::cast(other_array->get(3))->value();
16787 if (scope_position != scope_position_) return false;
16788 String* source = String::cast(other_array->get(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016789 return source->Equals(*source_);
Steve Blocka7e24c12009-10-30 11:49:00 +000016790 }
16791
16792 static uint32_t StringSharedHashHelper(String* source,
Steve Block1e0659c2011-05-24 12:43:12 +010016793 SharedFunctionInfo* shared,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016794 LanguageMode language_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +010016795 int scope_position) {
Steve Blocka7e24c12009-10-30 11:49:00 +000016796 uint32_t hash = source->Hash();
16797 if (shared->HasSourceCode()) {
16798 // Instead of using the SharedFunctionInfo pointer in the hash
16799 // code computation, we use a combination of the hash of the
Ben Murdoch3ef787d2012-04-12 10:51:47 +010016800 // script source code and the start position of the calling scope.
16801 // We do this to ensure that the cache entries can survive garbage
Steve Blocka7e24c12009-10-30 11:49:00 +000016802 // collection.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016803 Script* script(Script::cast(shared->script()));
Steve Blocka7e24c12009-10-30 11:49:00 +000016804 hash ^= String::cast(script->source())->Hash();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016805 STATIC_ASSERT(LANGUAGE_END == 3);
16806 if (is_strict(language_mode)) hash ^= 0x8000;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010016807 hash += scope_position;
Steve Blocka7e24c12009-10-30 11:49:00 +000016808 }
16809 return hash;
16810 }
16811
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016812 uint32_t Hash() override {
16813 return StringSharedHashHelper(*source_, *shared_, language_mode_,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016814 scope_position_);
Steve Blocka7e24c12009-10-30 11:49:00 +000016815 }
16816
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016817 uint32_t HashForObject(Object* obj) override {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016818 DisallowHeapAllocation no_allocation;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040016819 if (obj->IsNumber()) {
16820 return static_cast<uint32_t>(obj->Number());
16821 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +010016822 FixedArray* other_array = FixedArray::cast(obj);
16823 SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0));
16824 String* source = String::cast(other_array->get(1));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016825 int language_unchecked = Smi::cast(other_array->get(2))->value();
16826 DCHECK(is_valid_language_mode(language_unchecked));
16827 LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010016828 int scope_position = Smi::cast(other_array->get(3))->value();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016829 return StringSharedHashHelper(source, shared, language_mode,
16830 scope_position);
Steve Blocka7e24c12009-10-30 11:49:00 +000016831 }
16832
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016833
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016834 Handle<Object> AsHandle(Isolate* isolate) override {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016835 Handle<FixedArray> array = isolate->factory()->NewFixedArray(4);
16836 array->set(0, *shared_);
16837 array->set(1, *source_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016838 array->set(2, Smi::FromInt(language_mode_));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016839 array->set(3, Smi::FromInt(scope_position_));
16840 return array;
Steve Blocka7e24c12009-10-30 11:49:00 +000016841 }
16842
16843 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016844 Handle<String> source_;
16845 Handle<SharedFunctionInfo> shared_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016846 LanguageMode language_mode_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010016847 int scope_position_;
Steve Blocka7e24c12009-10-30 11:49:00 +000016848};
16849
16850
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016851namespace {
16852
16853JSRegExp::Flags RegExpFlagsFromString(Handle<String> flags, bool* success) {
16854 JSRegExp::Flags value = JSRegExp::kNone;
16855 int length = flags->length();
16856 // A longer flags string cannot be valid.
16857 if (length > 5) return JSRegExp::Flags(0);
16858 for (int i = 0; i < length; i++) {
16859 JSRegExp::Flag flag = JSRegExp::kNone;
16860 switch (flags->Get(i)) {
16861 case 'g':
16862 flag = JSRegExp::kGlobal;
16863 break;
16864 case 'i':
16865 flag = JSRegExp::kIgnoreCase;
16866 break;
16867 case 'm':
16868 flag = JSRegExp::kMultiline;
16869 break;
16870 case 'u':
16871 if (!FLAG_harmony_unicode_regexps) return JSRegExp::Flags(0);
16872 flag = JSRegExp::kUnicode;
16873 break;
16874 case 'y':
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016875 flag = JSRegExp::kSticky;
16876 break;
16877 default:
16878 return JSRegExp::Flags(0);
16879 }
16880 // Duplicate flag.
16881 if (value & flag) return JSRegExp::Flags(0);
16882 value |= flag;
16883 }
16884 *success = true;
16885 return value;
16886}
16887
16888} // namespace
16889
16890
16891// static
16892MaybeHandle<JSRegExp> JSRegExp::New(Handle<String> pattern, Flags flags) {
16893 Isolate* isolate = pattern->GetIsolate();
16894 Handle<JSFunction> constructor = isolate->regexp_function();
16895 Handle<JSRegExp> regexp =
16896 Handle<JSRegExp>::cast(isolate->factory()->NewJSObject(constructor));
16897
16898 return JSRegExp::Initialize(regexp, pattern, flags);
16899}
16900
16901
16902// static
16903MaybeHandle<JSRegExp> JSRegExp::New(Handle<String> pattern,
16904 Handle<String> flags_string) {
16905 Isolate* isolate = pattern->GetIsolate();
16906 bool success = false;
16907 Flags flags = RegExpFlagsFromString(flags_string, &success);
16908 if (!success) {
16909 THROW_NEW_ERROR(
16910 isolate,
16911 NewSyntaxError(MessageTemplate::kInvalidRegExpFlags, flags_string),
16912 JSRegExp);
16913 }
16914 return New(pattern, flags);
16915}
16916
16917
16918// static
16919Handle<JSRegExp> JSRegExp::Copy(Handle<JSRegExp> regexp) {
16920 Isolate* const isolate = regexp->GetIsolate();
16921 return Handle<JSRegExp>::cast(isolate->factory()->CopyJSObject(regexp));
16922}
16923
16924
16925template <typename Char>
16926inline int CountRequiredEscapes(Handle<String> source) {
16927 DisallowHeapAllocation no_gc;
16928 int escapes = 0;
16929 Vector<const Char> src = source->GetCharVector<Char>();
16930 for (int i = 0; i < src.length(); i++) {
16931 if (src[i] == '/' && (i == 0 || src[i - 1] != '\\')) escapes++;
16932 }
16933 return escapes;
16934}
16935
16936
16937template <typename Char, typename StringType>
16938inline Handle<StringType> WriteEscapedRegExpSource(Handle<String> source,
16939 Handle<StringType> result) {
16940 DisallowHeapAllocation no_gc;
16941 Vector<const Char> src = source->GetCharVector<Char>();
16942 Vector<Char> dst(result->GetChars(), result->length());
16943 int s = 0;
16944 int d = 0;
16945 while (s < src.length()) {
16946 if (src[s] == '/' && (s == 0 || src[s - 1] != '\\')) dst[d++] = '\\';
16947 dst[d++] = src[s++];
16948 }
16949 DCHECK_EQ(result->length(), d);
16950 return result;
16951}
16952
16953
16954MaybeHandle<String> EscapeRegExpSource(Isolate* isolate,
16955 Handle<String> source) {
16956 String::Flatten(source);
16957 if (source->length() == 0) return isolate->factory()->query_colon_string();
16958 bool one_byte = source->IsOneByteRepresentationUnderneath();
16959 int escapes = one_byte ? CountRequiredEscapes<uint8_t>(source)
16960 : CountRequiredEscapes<uc16>(source);
16961 if (escapes == 0) return source;
16962 int length = source->length() + escapes;
16963 if (one_byte) {
16964 Handle<SeqOneByteString> result;
16965 ASSIGN_RETURN_ON_EXCEPTION(isolate, result,
16966 isolate->factory()->NewRawOneByteString(length),
16967 String);
16968 return WriteEscapedRegExpSource<uint8_t>(source, result);
16969 } else {
16970 Handle<SeqTwoByteString> result;
16971 ASSIGN_RETURN_ON_EXCEPTION(isolate, result,
16972 isolate->factory()->NewRawTwoByteString(length),
16973 String);
16974 return WriteEscapedRegExpSource<uc16>(source, result);
16975 }
16976}
16977
16978
16979// static
16980MaybeHandle<JSRegExp> JSRegExp::Initialize(Handle<JSRegExp> regexp,
16981 Handle<String> source,
16982 Handle<String> flags_string) {
16983 Isolate* isolate = source->GetIsolate();
16984 bool success = false;
16985 Flags flags = RegExpFlagsFromString(flags_string, &success);
16986 if (!success) {
16987 THROW_NEW_ERROR(
16988 isolate,
16989 NewSyntaxError(MessageTemplate::kInvalidRegExpFlags, flags_string),
16990 JSRegExp);
16991 }
16992 return Initialize(regexp, source, flags);
16993}
16994
16995
16996// static
16997MaybeHandle<JSRegExp> JSRegExp::Initialize(Handle<JSRegExp> regexp,
16998 Handle<String> source, Flags flags) {
16999 Isolate* isolate = regexp->GetIsolate();
17000 Factory* factory = isolate->factory();
17001 // If source is the empty string we set it to "(?:)" instead as
17002 // suggested by ECMA-262, 5th, section 15.10.4.1.
17003 if (source->length() == 0) source = factory->query_colon_string();
17004
17005 Handle<String> escaped_source;
17006 ASSIGN_RETURN_ON_EXCEPTION(isolate, escaped_source,
17007 EscapeRegExpSource(isolate, source), JSRegExp);
17008
17009 regexp->set_source(*escaped_source);
17010 regexp->set_flags(Smi::FromInt(flags));
17011
17012 Map* map = regexp->map();
17013 Object* constructor = map->GetConstructor();
17014 if (constructor->IsJSFunction() &&
17015 JSFunction::cast(constructor)->initial_map() == map) {
17016 // If we still have the original map, set in-object properties directly.
17017 regexp->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex,
17018 Smi::FromInt(0), SKIP_WRITE_BARRIER);
17019 } else {
17020 // Map has changed, so use generic, but slower, method.
17021 PropertyAttributes writable =
17022 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
17023 JSObject::SetOwnPropertyIgnoreAttributes(
17024 regexp, factory->last_index_string(),
17025 Handle<Smi>(Smi::FromInt(0), isolate), writable)
17026 .Check();
17027 }
17028
17029 RETURN_ON_EXCEPTION(isolate, RegExpImpl::Compile(regexp, source, flags),
17030 JSRegExp);
17031
17032 return regexp;
17033}
17034
17035
Steve Blocka7e24c12009-10-30 11:49:00 +000017036// RegExpKey carries the source and flags of a regular expression as key.
17037class RegExpKey : public HashTableKey {
17038 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017039 RegExpKey(Handle<String> string, JSRegExp::Flags flags)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017040 : string_(string), flags_(Smi::FromInt(flags)) {}
Steve Blocka7e24c12009-10-30 11:49:00 +000017041
Steve Block3ce2e202009-11-05 08:53:23 +000017042 // Rather than storing the key in the hash table, a pointer to the
17043 // stored value is stored where the key should be. IsMatch then
17044 // compares the search key to the found object, rather than comparing
17045 // a key to a key.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017046 bool IsMatch(Object* obj) override {
Steve Blocka7e24c12009-10-30 11:49:00 +000017047 FixedArray* val = FixedArray::cast(obj);
17048 return string_->Equals(String::cast(val->get(JSRegExp::kSourceIndex)))
17049 && (flags_ == val->get(JSRegExp::kFlagsIndex));
17050 }
17051
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017052 uint32_t Hash() override { return RegExpHash(*string_, flags_); }
Steve Blocka7e24c12009-10-30 11:49:00 +000017053
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017054 Handle<Object> AsHandle(Isolate* isolate) override {
Steve Blocka7e24c12009-10-30 11:49:00 +000017055 // Plain hash maps, which is where regexp keys are used, don't
17056 // use this function.
17057 UNREACHABLE();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017058 return MaybeHandle<Object>().ToHandleChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +000017059 }
17060
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017061 uint32_t HashForObject(Object* obj) override {
Steve Blocka7e24c12009-10-30 11:49:00 +000017062 FixedArray* val = FixedArray::cast(obj);
17063 return RegExpHash(String::cast(val->get(JSRegExp::kSourceIndex)),
17064 Smi::cast(val->get(JSRegExp::kFlagsIndex)));
17065 }
17066
17067 static uint32_t RegExpHash(String* string, Smi* flags) {
17068 return string->Hash() + flags->value();
17069 }
17070
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017071 Handle<String> string_;
Steve Blocka7e24c12009-10-30 11:49:00 +000017072 Smi* flags_;
17073};
17074
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017075
17076Handle<Object> OneByteStringKey::AsHandle(Isolate* isolate) {
17077 if (hash_field_ == 0) Hash();
17078 return isolate->factory()->NewOneByteInternalizedString(string_, hash_field_);
17079}
17080
17081
17082Handle<Object> TwoByteStringKey::AsHandle(Isolate* isolate) {
17083 if (hash_field_ == 0) Hash();
17084 return isolate->factory()->NewTwoByteInternalizedString(string_, hash_field_);
17085}
17086
17087
17088Handle<Object> SeqOneByteSubStringKey::AsHandle(Isolate* isolate) {
17089 if (hash_field_ == 0) Hash();
17090 return isolate->factory()->NewOneByteInternalizedSubString(
17091 string_, from_, length_, hash_field_);
17092}
17093
17094
17095bool SeqOneByteSubStringKey::IsMatch(Object* string) {
17096 Vector<const uint8_t> chars(string_->GetChars() + from_, length_);
17097 return String::cast(string)->IsOneByteEqualTo(chars);
17098}
17099
17100
17101// InternalizedStringKey carries a string/internalized-string object as key.
17102class InternalizedStringKey : public HashTableKey {
Steve Blocka7e24c12009-10-30 11:49:00 +000017103 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017104 explicit InternalizedStringKey(Handle<String> string)
Ben Murdochda12d292016-06-02 14:46:10 +010017105 : string_(String::Flatten(string)) {}
Steve Blocka7e24c12009-10-30 11:49:00 +000017106
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017107 bool IsMatch(Object* string) override {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017108 return String::cast(string)->Equals(*string_);
Steve Blocka7e24c12009-10-30 11:49:00 +000017109 }
17110
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017111 uint32_t Hash() override { return string_->Hash(); }
Steve Blocka7e24c12009-10-30 11:49:00 +000017112
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017113 uint32_t HashForObject(Object* other) override {
Steve Blocka7e24c12009-10-30 11:49:00 +000017114 return String::cast(other)->Hash();
17115 }
17116
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017117 Handle<Object> AsHandle(Isolate* isolate) override {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017118 // Internalize the string if possible.
17119 MaybeHandle<Map> maybe_map =
17120 isolate->factory()->InternalizedStringMapForString(string_);
17121 Handle<Map> map;
17122 if (maybe_map.ToHandle(&map)) {
17123 string_->set_map_no_write_barrier(*map);
17124 DCHECK(string_->IsInternalizedString());
Steve Blocka7e24c12009-10-30 11:49:00 +000017125 return string_;
17126 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017127 // Otherwise allocate a new internalized string.
17128 return isolate->factory()->NewInternalizedStringImpl(
17129 string_, string_->length(), string_->hash_field());
Steve Blocka7e24c12009-10-30 11:49:00 +000017130 }
17131
17132 static uint32_t StringHash(Object* obj) {
17133 return String::cast(obj)->Hash();
17134 }
17135
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017136 Handle<String> string_;
Steve Blocka7e24c12009-10-30 11:49:00 +000017137};
17138
17139
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017140template<typename Derived, typename Shape, typename Key>
17141void HashTable<Derived, Shape, Key>::IteratePrefix(ObjectVisitor* v) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017142 BodyDescriptorBase::IteratePointers(this, 0, kElementsStartOffset, v);
Steve Blocka7e24c12009-10-30 11:49:00 +000017143}
17144
17145
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017146template<typename Derived, typename Shape, typename Key>
17147void HashTable<Derived, Shape, Key>::IterateElements(ObjectVisitor* v) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017148 BodyDescriptorBase::IteratePointers(this, kElementsStartOffset,
17149 kHeaderSize + length() * kPointerSize, v);
Steve Blocka7e24c12009-10-30 11:49:00 +000017150}
17151
17152
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017153template<typename Derived, typename Shape, typename Key>
17154Handle<Derived> HashTable<Derived, Shape, Key>::New(
17155 Isolate* isolate,
17156 int at_least_space_for,
17157 MinimumCapacity capacity_option,
17158 PretenureFlag pretenure) {
17159 DCHECK(0 <= at_least_space_for);
17160 DCHECK(!capacity_option || base::bits::IsPowerOfTwo32(at_least_space_for));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017161
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017162 int capacity = (capacity_option == USE_CUSTOM_MINIMUM_CAPACITY)
17163 ? at_least_space_for
17164 : ComputeCapacity(at_least_space_for);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000017165 if (capacity > HashTable::kMaxCapacity) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017166 v8::internal::Heap::FatalProcessOutOfMemory("invalid table size", true);
Leon Clarkee46be812010-01-19 14:06:41 +000017167 }
17168
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017169 Factory* factory = isolate->factory();
17170 int length = EntryToIndex(capacity);
17171 Handle<FixedArray> array = factory->NewFixedArray(length, pretenure);
17172 array->set_map_no_write_barrier(*factory->hash_table_map());
17173 Handle<Derived> table = Handle<Derived>::cast(array);
17174
17175 table->SetNumberOfElements(0);
17176 table->SetNumberOfDeletedElements(0);
17177 table->SetCapacity(capacity);
17178 return table;
Steve Blocka7e24c12009-10-30 11:49:00 +000017179}
17180
17181
Leon Clarkee46be812010-01-19 14:06:41 +000017182// Find entry for key otherwise return kNotFound.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017183template <typename Derived, typename Shape>
17184int NameDictionaryBase<Derived, Shape>::FindEntry(Handle<Name> key) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017185 if (!key->IsUniqueName()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017186 return DerivedDictionary::FindEntry(key);
Ben Murdoch3bec4d22010-07-22 14:51:16 +010017187 }
17188
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017189 // Optimized for unique names. Knowledge of the key type allows:
17190 // 1. Move the check if the key is unique out of the loop.
17191 // 2. Avoid comparing hash codes in unique-to-unique comparison.
17192 // 3. Detect a case when a dictionary key is not unique but the key is.
17193 // In case of positive result the dictionary key may be replaced by the
17194 // internalized string with minimal performance penalty. It gives a chance
17195 // to perform further lookups in code stubs (and significant performance
17196 // boost a certain style of code).
Ben Murdoch3bec4d22010-07-22 14:51:16 +010017197
17198 // EnsureCapacity will guarantee the hash table is never full.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017199 uint32_t capacity = this->Capacity();
17200 uint32_t entry = Derived::FirstProbe(key->Hash(), capacity);
Ben Murdoch3bec4d22010-07-22 14:51:16 +010017201 uint32_t count = 1;
17202
17203 while (true) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017204 int index = Derived::EntryToIndex(entry);
17205 Object* element = this->get(index);
Ben Murdoch3bec4d22010-07-22 14:51:16 +010017206 if (element->IsUndefined()) break; // Empty entry.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017207 if (*key == element) return entry;
17208 if (!element->IsUniqueName() &&
Ben Murdoch3ef787d2012-04-12 10:51:47 +010017209 !element->IsTheHole() &&
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017210 Name::cast(element)->Equals(*key)) {
17211 // Replace a key that is a non-internalized string by the equivalent
17212 // internalized string for faster further lookups.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017213 this->set(index, *key);
Ben Murdoch3bec4d22010-07-22 14:51:16 +010017214 return entry;
17215 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017216 DCHECK(element->IsTheHole() || !Name::cast(element)->Equals(*key));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017217 entry = Derived::NextProbe(entry, count++, capacity);
Ben Murdoch3bec4d22010-07-22 14:51:16 +010017218 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017219 return Derived::kNotFound;
Ben Murdoch3bec4d22010-07-22 14:51:16 +010017220}
17221
17222
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017223template<typename Derived, typename Shape, typename Key>
17224void HashTable<Derived, Shape, Key>::Rehash(
17225 Handle<Derived> new_table,
17226 Key key) {
17227 DCHECK(NumberOfElements() < new_table->Capacity());
Ben Murdoch3ef787d2012-04-12 10:51:47 +010017228
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017229 DisallowHeapAllocation no_gc;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000017230 WriteBarrierMode mode = new_table->GetWriteBarrierMode(no_gc);
17231
17232 // Copy prefix to new array.
17233 for (int i = kPrefixStartIndex;
17234 i < kPrefixStartIndex + Shape::kPrefixSize;
17235 i++) {
17236 new_table->set(i, get(i), mode);
17237 }
17238
17239 // Rehash the elements.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017240 int capacity = this->Capacity();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000017241 for (int i = 0; i < capacity; i++) {
17242 uint32_t from_index = EntryToIndex(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017243 Object* k = this->get(from_index);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000017244 if (IsKey(k)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017245 uint32_t hash = this->HashForObject(key, k);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000017246 uint32_t insertion_index =
17247 EntryToIndex(new_table->FindInsertionEntry(hash));
17248 for (int j = 0; j < Shape::kEntrySize; j++) {
17249 new_table->set(insertion_index + j, get(from_index + j), mode);
17250 }
17251 }
17252 }
17253 new_table->SetNumberOfElements(NumberOfElements());
17254 new_table->SetNumberOfDeletedElements(0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000017255}
17256
17257
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017258template<typename Derived, typename Shape, typename Key>
17259uint32_t HashTable<Derived, Shape, Key>::EntryForProbe(
17260 Key key,
17261 Object* k,
17262 int probe,
17263 uint32_t expected) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017264 uint32_t hash = this->HashForObject(key, k);
17265 uint32_t capacity = this->Capacity();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017266 uint32_t entry = FirstProbe(hash, capacity);
17267 for (int i = 1; i < probe; i++) {
17268 if (entry == expected) return expected;
17269 entry = NextProbe(entry, i, capacity);
17270 }
17271 return entry;
17272}
17273
17274
17275template<typename Derived, typename Shape, typename Key>
17276void HashTable<Derived, Shape, Key>::Swap(uint32_t entry1,
17277 uint32_t entry2,
17278 WriteBarrierMode mode) {
17279 int index1 = EntryToIndex(entry1);
17280 int index2 = EntryToIndex(entry2);
17281 Object* temp[Shape::kEntrySize];
17282 for (int j = 0; j < Shape::kEntrySize; j++) {
17283 temp[j] = get(index1 + j);
17284 }
17285 for (int j = 0; j < Shape::kEntrySize; j++) {
17286 set(index1 + j, get(index2 + j), mode);
17287 }
17288 for (int j = 0; j < Shape::kEntrySize; j++) {
17289 set(index2 + j, temp[j], mode);
17290 }
17291}
17292
17293
17294template<typename Derived, typename Shape, typename Key>
17295void HashTable<Derived, Shape, Key>::Rehash(Key key) {
17296 DisallowHeapAllocation no_gc;
17297 WriteBarrierMode mode = GetWriteBarrierMode(no_gc);
17298 uint32_t capacity = Capacity();
17299 bool done = false;
17300 for (int probe = 1; !done; probe++) {
17301 // All elements at entries given by one of the first _probe_ probes
17302 // are placed correctly. Other elements might need to be moved.
17303 done = true;
17304 for (uint32_t current = 0; current < capacity; current++) {
17305 Object* current_key = get(EntryToIndex(current));
17306 if (IsKey(current_key)) {
17307 uint32_t target = EntryForProbe(key, current_key, probe, current);
17308 if (current == target) continue;
17309 Object* target_key = get(EntryToIndex(target));
17310 if (!IsKey(target_key) ||
17311 EntryForProbe(key, target_key, probe, target) != target) {
17312 // Put the current element into the correct position.
17313 Swap(current, target, mode);
17314 // The other element will be processed on the next iteration.
17315 current--;
17316 } else {
17317 // The place for the current element is occupied. Leave the element
17318 // for the next probe.
17319 done = false;
17320 }
17321 }
17322 }
17323 }
Ben Murdoch097c5b22016-05-18 11:27:45 +010017324 // Wipe deleted entries.
17325 Heap* heap = GetHeap();
17326 Object* the_hole = heap->the_hole_value();
17327 Object* undefined = heap->undefined_value();
17328 for (uint32_t current = 0; current < capacity; current++) {
17329 if (get(EntryToIndex(current)) == the_hole) {
17330 set(EntryToIndex(current), undefined);
17331 }
17332 }
17333 SetNumberOfDeletedElements(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017334}
17335
17336
17337template<typename Derived, typename Shape, typename Key>
17338Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity(
17339 Handle<Derived> table,
17340 int n,
17341 Key key,
17342 PretenureFlag pretenure) {
17343 Isolate* isolate = table->GetIsolate();
17344 int capacity = table->Capacity();
17345 int nof = table->NumberOfElements() + n;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017346
17347 if (table->HasSufficientCapacity(n)) return table;
Steve Blocka7e24c12009-10-30 11:49:00 +000017348
Steve Block6ded16b2010-05-10 14:33:55 +010017349 const int kMinCapacityForPretenure = 256;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017350 bool should_pretenure = pretenure == TENURED ||
17351 ((capacity > kMinCapacityForPretenure) &&
17352 !isolate->heap()->InNewSpace(*table));
17353 Handle<Derived> new_table = HashTable::New(
17354 isolate,
17355 nof * 2,
17356 USE_DEFAULT_MINIMUM_CAPACITY,
17357 should_pretenure ? TENURED : NOT_TENURED);
Leon Clarke4515c472010-02-03 11:58:03 +000017358
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017359 table->Rehash(new_table, key);
17360 return new_table;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000017361}
Steve Blocka7e24c12009-10-30 11:49:00 +000017362
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000017363
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017364template <typename Derived, typename Shape, typename Key>
17365bool HashTable<Derived, Shape, Key>::HasSufficientCapacity(int n) {
17366 int capacity = Capacity();
17367 int nof = NumberOfElements() + n;
17368 int nod = NumberOfDeletedElements();
17369 // Return true if:
17370 // 50% is still free after adding n elements and
17371 // at most 50% of the free elements are deleted elements.
17372 if (nod <= (capacity - nof) >> 1) {
17373 int needed_free = nof >> 1;
17374 if (nof + needed_free <= capacity) return true;
17375 }
17376 return false;
17377}
17378
17379
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017380template<typename Derived, typename Shape, typename Key>
17381Handle<Derived> HashTable<Derived, Shape, Key>::Shrink(Handle<Derived> table,
17382 Key key) {
17383 int capacity = table->Capacity();
17384 int nof = table->NumberOfElements();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000017385
17386 // Shrink to fit the number of elements if only a quarter of the
17387 // capacity is filled with elements.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017388 if (nof > (capacity >> 2)) return table;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000017389 // Allocate a new dictionary with room for at least the current
17390 // number of elements. The allocation method will make sure that
17391 // there is extra room in the dictionary for additions. Don't go
17392 // lower than room for 16 elements.
17393 int at_least_room_for = nof;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017394 if (at_least_room_for < 16) return table;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000017395
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017396 Isolate* isolate = table->GetIsolate();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000017397 const int kMinCapacityForPretenure = 256;
17398 bool pretenure =
17399 (at_least_room_for > kMinCapacityForPretenure) &&
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017400 !isolate->heap()->InNewSpace(*table);
17401 Handle<Derived> new_table = HashTable::New(
17402 isolate,
17403 at_least_room_for,
17404 USE_DEFAULT_MINIMUM_CAPACITY,
17405 pretenure ? TENURED : NOT_TENURED);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000017406
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017407 table->Rehash(new_table, key);
17408 return new_table;
Steve Blocka7e24c12009-10-30 11:49:00 +000017409}
17410
17411
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017412template<typename Derived, typename Shape, typename Key>
17413uint32_t HashTable<Derived, Shape, Key>::FindInsertionEntry(uint32_t hash) {
Steve Blocka7e24c12009-10-30 11:49:00 +000017414 uint32_t capacity = Capacity();
Leon Clarkee46be812010-01-19 14:06:41 +000017415 uint32_t entry = FirstProbe(hash, capacity);
17416 uint32_t count = 1;
17417 // EnsureCapacity will guarantee the hash table is never full.
17418 while (true) {
17419 Object* element = KeyAt(entry);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010017420 if (element->IsUndefined() || element->IsTheHole()) break;
Leon Clarkee46be812010-01-19 14:06:41 +000017421 entry = NextProbe(entry, count++, capacity);
Steve Blocka7e24c12009-10-30 11:49:00 +000017422 }
Steve Blocka7e24c12009-10-30 11:49:00 +000017423 return entry;
17424}
17425
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017426
Steve Blocka7e24c12009-10-30 11:49:00 +000017427// Force instantiation of template instances class.
17428// Please note this list is compiler dependent.
17429
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017430template class HashTable<StringTable, StringTableShape, HashTableKey*>;
Steve Blocka7e24c12009-10-30 11:49:00 +000017431
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017432template class HashTable<CompilationCacheTable,
17433 CompilationCacheShape,
17434 HashTableKey*>;
Steve Blocka7e24c12009-10-30 11:49:00 +000017435
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017436template class HashTable<ObjectHashTable,
17437 ObjectHashTableShape,
17438 Handle<Object> >;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010017439
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017440template class HashTable<WeakHashTable, WeakHashTableShape<2>, Handle<Object> >;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000017441
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017442template class Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >;
Steve Blocka7e24c12009-10-30 11:49:00 +000017443
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017444template class Dictionary<GlobalDictionary, GlobalDictionaryShape,
17445 Handle<Name> >;
17446
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017447template class Dictionary<SeededNumberDictionary,
17448 SeededNumberDictionaryShape,
17449 uint32_t>;
Steve Blocka7e24c12009-10-30 11:49:00 +000017450
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017451template class Dictionary<UnseededNumberDictionary,
17452 UnseededNumberDictionaryShape,
17453 uint32_t>;
Ben Murdochc7cc0282012-03-05 14:35:55 +000017454
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017455template Handle<SeededNumberDictionary>
17456Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
17457 New(Isolate*, int at_least_space_for, PretenureFlag pretenure);
Ben Murdochc7cc0282012-03-05 14:35:55 +000017458
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017459template Handle<UnseededNumberDictionary>
17460Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>::
17461 New(Isolate*, int at_least_space_for, PretenureFlag pretenure);
Steve Blocka7e24c12009-10-30 11:49:00 +000017462
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017463template Handle<NameDictionary>
17464Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::
17465 New(Isolate*, int n, PretenureFlag pretenure);
Steve Blocka7e24c12009-10-30 11:49:00 +000017466
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017467template Handle<GlobalDictionary>
17468Dictionary<GlobalDictionary, GlobalDictionaryShape, Handle<Name> >::New(
17469 Isolate*, int n, PretenureFlag pretenure);
17470
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017471template Handle<SeededNumberDictionary>
17472Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
17473 AtPut(Handle<SeededNumberDictionary>, uint32_t, Handle<Object>);
Steve Blocka7e24c12009-10-30 11:49:00 +000017474
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017475template Handle<UnseededNumberDictionary>
17476Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>::
17477 AtPut(Handle<UnseededNumberDictionary>, uint32_t, Handle<Object>);
Ben Murdochc7cc0282012-03-05 14:35:55 +000017478
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017479template Object*
17480Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
Ben Murdoch3ef787d2012-04-12 10:51:47 +010017481 SlowReverseLookup(Object* value);
17482
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017483template Object*
17484Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::
Ben Murdochc7cc0282012-03-05 14:35:55 +000017485 SlowReverseLookup(Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +000017486
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017487template Handle<Object>
17488Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::DeleteProperty(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017489 Handle<NameDictionary>, int);
Steve Blocka7e24c12009-10-30 11:49:00 +000017490
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017491template Handle<Object>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017492Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape,
17493 uint32_t>::DeleteProperty(Handle<SeededNumberDictionary>, int);
Steve Blocka7e24c12009-10-30 11:49:00 +000017494
Ben Murdochda12d292016-06-02 14:46:10 +010017495template Handle<Object>
17496Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape,
17497 uint32_t>::DeleteProperty(Handle<UnseededNumberDictionary>, int);
17498
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017499template Handle<NameDictionary>
17500HashTable<NameDictionary, NameDictionaryShape, Handle<Name> >::
17501 New(Isolate*, int, MinimumCapacity, PretenureFlag);
Steve Blocka7e24c12009-10-30 11:49:00 +000017502
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017503template Handle<NameDictionary>
17504HashTable<NameDictionary, NameDictionaryShape, Handle<Name> >::
17505 Shrink(Handle<NameDictionary>, Handle<Name>);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000017506
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017507template Handle<SeededNumberDictionary>
17508HashTable<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
17509 Shrink(Handle<SeededNumberDictionary>, uint32_t);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000017510
Ben Murdochda12d292016-06-02 14:46:10 +010017511template Handle<UnseededNumberDictionary>
17512 HashTable<UnseededNumberDictionary, UnseededNumberDictionaryShape,
17513 uint32_t>::Shrink(Handle<UnseededNumberDictionary>, uint32_t);
17514
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017515template Handle<NameDictionary>
17516Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::Add(
17517 Handle<NameDictionary>, Handle<Name>, Handle<Object>, PropertyDetails);
Steve Blocka7e24c12009-10-30 11:49:00 +000017518
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017519template Handle<GlobalDictionary>
17520 Dictionary<GlobalDictionary, GlobalDictionaryShape, Handle<Name> >::Add(
17521 Handle<GlobalDictionary>, Handle<Name>, Handle<Object>,
17522 PropertyDetails);
17523
Emily Bernierd0a1eb72015-03-24 16:35:39 -040017524template Handle<FixedArray> Dictionary<
17525 NameDictionary, NameDictionaryShape,
17526 Handle<Name> >::BuildIterationIndicesArray(Handle<NameDictionary>);
17527
17528template Handle<FixedArray> Dictionary<
17529 NameDictionary, NameDictionaryShape,
17530 Handle<Name> >::GenerateNewEnumerationIndices(Handle<NameDictionary>);
Ben Murdochc7cc0282012-03-05 14:35:55 +000017531
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017532template Handle<SeededNumberDictionary>
17533Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
17534 Add(Handle<SeededNumberDictionary>,
17535 uint32_t,
17536 Handle<Object>,
17537 PropertyDetails);
Steve Blocka7e24c12009-10-30 11:49:00 +000017538
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017539template Handle<UnseededNumberDictionary>
17540Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>::
17541 Add(Handle<UnseededNumberDictionary>,
17542 uint32_t,
17543 Handle<Object>,
17544 PropertyDetails);
Steve Blocka7e24c12009-10-30 11:49:00 +000017545
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017546template Handle<SeededNumberDictionary>
17547Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
17548 EnsureCapacity(Handle<SeededNumberDictionary>, int, uint32_t);
Ben Murdochc7cc0282012-03-05 14:35:55 +000017549
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017550template Handle<UnseededNumberDictionary>
17551Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>::
17552 EnsureCapacity(Handle<UnseededNumberDictionary>, int, uint32_t);
Steve Blocka7e24c12009-10-30 11:49:00 +000017553
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017554template void Dictionary<NameDictionary, NameDictionaryShape,
17555 Handle<Name> >::SetRequiresCopyOnCapacityChange();
17556
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017557template Handle<NameDictionary>
17558Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::
17559 EnsureCapacity(Handle<NameDictionary>, int, Handle<Name>);
Steve Blocka7e24c12009-10-30 11:49:00 +000017560
Emily Bernierd0a1eb72015-03-24 16:35:39 -040017561template int HashTable<SeededNumberDictionary, SeededNumberDictionaryShape,
17562 uint32_t>::FindEntry(uint32_t);
Leon Clarkee46be812010-01-19 14:06:41 +000017563
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017564template int NameDictionaryBase<NameDictionary, NameDictionaryShape>::FindEntry(
17565 Handle<Name>);
17566
Leon Clarkee46be812010-01-19 14:06:41 +000017567
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017568Handle<Object> JSObject::PrepareSlowElementsForSort(
17569 Handle<JSObject> object, uint32_t limit) {
17570 DCHECK(object->HasDictionaryElements());
17571 Isolate* isolate = object->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +000017572 // Must stay in dictionary mode, either because of requires_slow_elements,
17573 // or because we are not going to sort (and therefore compact) all of the
17574 // elements.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017575 Handle<SeededNumberDictionary> dict(object->element_dictionary(), isolate);
17576 Handle<SeededNumberDictionary> new_dict =
17577 SeededNumberDictionary::New(isolate, dict->NumberOfElements());
Steve Blocka7e24c12009-10-30 11:49:00 +000017578
17579 uint32_t pos = 0;
17580 uint32_t undefs = 0;
Steve Block6ded16b2010-05-10 14:33:55 +010017581 int capacity = dict->Capacity();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017582 Handle<Smi> bailout(Smi::FromInt(-1), isolate);
17583 // Entry to the new dictionary does not cause it to grow, as we have
17584 // allocated one that is large enough for all entries.
17585 DisallowHeapAllocation no_gc;
Steve Blocka7e24c12009-10-30 11:49:00 +000017586 for (int i = 0; i < capacity; i++) {
17587 Object* k = dict->KeyAt(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017588 if (!dict->IsKey(k)) continue;
17589
17590 DCHECK(k->IsNumber());
17591 DCHECK(!k->IsSmi() || Smi::cast(k)->value() >= 0);
17592 DCHECK(!k->IsHeapNumber() || HeapNumber::cast(k)->value() >= 0);
17593 DCHECK(!k->IsHeapNumber() || HeapNumber::cast(k)->value() <= kMaxUInt32);
17594
17595 HandleScope scope(isolate);
17596 Handle<Object> value(dict->ValueAt(i), isolate);
17597 PropertyDetails details = dict->DetailsAt(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017598 if (details.type() == ACCESSOR_CONSTANT || details.IsReadOnly()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017599 // Bail out and do the sorting of undefineds and array holes in JS.
17600 // Also bail out if the element is not supposed to be moved.
17601 return bailout;
17602 }
17603
17604 uint32_t key = NumberToUint32(k);
17605 if (key < limit) {
17606 if (value->IsUndefined()) {
17607 undefs++;
17608 } else if (pos > static_cast<uint32_t>(Smi::kMaxValue)) {
17609 // Adding an entry with the key beyond smi-range requires
17610 // allocation. Bailout.
17611 return bailout;
Steve Blocka7e24c12009-10-30 11:49:00 +000017612 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017613 Handle<Object> result = SeededNumberDictionary::AddNumberEntry(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017614 new_dict, pos, value, details, object->map()->is_prototype_map());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017615 DCHECK(result.is_identical_to(new_dict));
17616 USE(result);
17617 pos++;
Steve Blocka7e24c12009-10-30 11:49:00 +000017618 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017619 } else if (key > static_cast<uint32_t>(Smi::kMaxValue)) {
17620 // Adding an entry with the key beyond smi-range requires
17621 // allocation. Bailout.
17622 return bailout;
17623 } else {
17624 Handle<Object> result = SeededNumberDictionary::AddNumberEntry(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017625 new_dict, key, value, details, object->map()->is_prototype_map());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017626 DCHECK(result.is_identical_to(new_dict));
17627 USE(result);
Steve Blocka7e24c12009-10-30 11:49:00 +000017628 }
17629 }
17630
17631 uint32_t result = pos;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017632 PropertyDetails no_details = PropertyDetails::Empty();
Steve Blocka7e24c12009-10-30 11:49:00 +000017633 while (undefs > 0) {
Steve Block1e0659c2011-05-24 12:43:12 +010017634 if (pos > static_cast<uint32_t>(Smi::kMaxValue)) {
17635 // Adding an entry with the key beyond smi-range requires
17636 // allocation. Bailout.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017637 return bailout;
Steve Block1e0659c2011-05-24 12:43:12 +010017638 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017639 HandleScope scope(isolate);
17640 Handle<Object> result = SeededNumberDictionary::AddNumberEntry(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017641 new_dict, pos, isolate->factory()->undefined_value(), no_details,
17642 object->map()->is_prototype_map());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017643 DCHECK(result.is_identical_to(new_dict));
17644 USE(result);
Steve Blocka7e24c12009-10-30 11:49:00 +000017645 pos++;
17646 undefs--;
17647 }
17648
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017649 object->set_elements(*new_dict);
Steve Blocka7e24c12009-10-30 11:49:00 +000017650
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017651 AllowHeapAllocation allocate_return_value;
17652 return isolate->factory()->NewNumberFromUint(result);
Steve Blocka7e24c12009-10-30 11:49:00 +000017653}
17654
17655
17656// Collects all defined (non-hole) and non-undefined (array) elements at
17657// the start of the elements array.
17658// If the object is in dictionary mode, it is converted to fast elements
17659// mode.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017660Handle<Object> JSObject::PrepareElementsForSort(Handle<JSObject> object,
17661 uint32_t limit) {
17662 Isolate* isolate = object->GetIsolate();
17663 if (object->HasSloppyArgumentsElements() ||
17664 object->map()->is_observed()) {
17665 return handle(Smi::FromInt(-1), isolate);
17666 }
Ben Murdoch8b112d22011-06-08 16:22:53 +010017667
Ben Murdoch097c5b22016-05-18 11:27:45 +010017668 if (object->HasStringWrapperElements()) {
17669 int len = String::cast(Handle<JSValue>::cast(object)->value())->length();
17670 return handle(Smi::FromInt(len), isolate);
17671 }
17672
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017673 if (object->HasDictionaryElements()) {
Steve Blocka7e24c12009-10-30 11:49:00 +000017674 // Convert to fast elements containing only the existing properties.
17675 // Ordering is irrelevant, since we are going to sort anyway.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017676 Handle<SeededNumberDictionary> dict(object->element_dictionary());
17677 if (object->IsJSArray() || dict->requires_slow_elements() ||
Steve Blocka7e24c12009-10-30 11:49:00 +000017678 dict->max_number_key() >= limit) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017679 return JSObject::PrepareSlowElementsForSort(object, limit);
Steve Blocka7e24c12009-10-30 11:49:00 +000017680 }
17681 // Convert to fast elements.
17682
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017683 Handle<Map> new_map =
17684 JSObject::GetElementsTransitionMap(object, FAST_HOLEY_ELEMENTS);
Steve Block8defd9f2010-07-08 12:39:36 +010017685
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017686 PretenureFlag tenure = isolate->heap()->InNewSpace(*object) ?
17687 NOT_TENURED: TENURED;
17688 Handle<FixedArray> fast_elements =
17689 isolate->factory()->NewFixedArray(dict->NumberOfElements(), tenure);
17690 dict->CopyValuesTo(*fast_elements);
17691 JSObject::ValidateElements(object);
Steve Block8defd9f2010-07-08 12:39:36 +010017692
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017693 JSObject::SetMapAndElements(object, new_map, fast_elements);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017694 } else if (object->HasFixedTypedArrayElements()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017695 // Typed arrays cannot have holes or undefined elements.
17696 return handle(Smi::FromInt(
17697 FixedArrayBase::cast(object->elements())->length()), isolate);
17698 } else if (!object->HasFastDoubleElements()) {
17699 EnsureWritableFastElements(object);
Steve Blocka7e24c12009-10-30 11:49:00 +000017700 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017701 DCHECK(object->HasFastSmiOrObjectElements() ||
17702 object->HasFastDoubleElements());
Steve Blocka7e24c12009-10-30 11:49:00 +000017703
17704 // Collect holes at the end, undefined before that and the rest at the
17705 // start, and return the number of non-hole, non-undefined values.
17706
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017707 Handle<FixedArrayBase> elements_base(object->elements());
Ben Murdoch69a99ed2011-11-30 16:03:39 +000017708 uint32_t elements_length = static_cast<uint32_t>(elements_base->length());
Steve Blocka7e24c12009-10-30 11:49:00 +000017709 if (limit > elements_length) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017710 limit = elements_length;
Steve Blocka7e24c12009-10-30 11:49:00 +000017711 }
17712 if (limit == 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017713 return handle(Smi::FromInt(0), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +000017714 }
17715
Ben Murdoch69a99ed2011-11-30 16:03:39 +000017716 uint32_t result = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017717 if (elements_base->map() == isolate->heap()->fixed_double_array_map()) {
17718 FixedDoubleArray* elements = FixedDoubleArray::cast(*elements_base);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000017719 // Split elements into defined and the_hole, in that order.
17720 unsigned int holes = limit;
17721 // Assume most arrays contain no holes and undefined values, so minimize the
17722 // number of stores of non-undefined, non-the-hole values.
17723 for (unsigned int i = 0; i < holes; i++) {
17724 if (elements->is_the_hole(i)) {
17725 holes--;
17726 } else {
17727 continue;
17728 }
17729 // Position i needs to be filled.
17730 while (holes > i) {
17731 if (elements->is_the_hole(holes)) {
17732 holes--;
17733 } else {
17734 elements->set(i, elements->get_scalar(holes));
17735 break;
17736 }
17737 }
Steve Blocka7e24c12009-10-30 11:49:00 +000017738 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +000017739 result = holes;
17740 while (holes < limit) {
17741 elements->set_the_hole(holes);
17742 holes++;
17743 }
17744 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017745 FixedArray* elements = FixedArray::cast(*elements_base);
17746 DisallowHeapAllocation no_gc;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000017747
17748 // Split elements into defined, undefined and the_hole, in that order. Only
17749 // count locations for undefined and the hole, and fill them afterwards.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017750 WriteBarrierMode write_barrier = elements->GetWriteBarrierMode(no_gc);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000017751 unsigned int undefs = limit;
17752 unsigned int holes = limit;
17753 // Assume most arrays contain no holes and undefined values, so minimize the
17754 // number of stores of non-undefined, non-the-hole values.
17755 for (unsigned int i = 0; i < undefs; i++) {
17756 Object* current = elements->get(i);
Steve Blocka7e24c12009-10-30 11:49:00 +000017757 if (current->IsTheHole()) {
17758 holes--;
17759 undefs--;
17760 } else if (current->IsUndefined()) {
17761 undefs--;
17762 } else {
Ben Murdoch69a99ed2011-11-30 16:03:39 +000017763 continue;
17764 }
17765 // Position i needs to be filled.
17766 while (undefs > i) {
17767 current = elements->get(undefs);
17768 if (current->IsTheHole()) {
17769 holes--;
17770 undefs--;
17771 } else if (current->IsUndefined()) {
17772 undefs--;
17773 } else {
17774 elements->set(i, current, write_barrier);
17775 break;
17776 }
Steve Blocka7e24c12009-10-30 11:49:00 +000017777 }
17778 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +000017779 result = undefs;
17780 while (undefs < holes) {
17781 elements->set_undefined(undefs);
17782 undefs++;
17783 }
17784 while (holes < limit) {
17785 elements->set_the_hole(holes);
17786 holes++;
17787 }
Steve Blocka7e24c12009-10-30 11:49:00 +000017788 }
17789
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017790 return isolate->factory()->NewNumberFromUint(result);
Steve Blocka7e24c12009-10-30 11:49:00 +000017791}
17792
17793
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017794ExternalArrayType JSTypedArray::type() {
17795 switch (elements()->map()->instance_type()) {
17796#define INSTANCE_TYPE_TO_ARRAY_TYPE(Type, type, TYPE, ctype, size) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017797 case FIXED_##TYPE##_ARRAY_TYPE: \
17798 return kExternal##Type##Array;
17799
17800 TYPED_ARRAYS(INSTANCE_TYPE_TO_ARRAY_TYPE)
17801#undef INSTANCE_TYPE_TO_ARRAY_TYPE
17802
17803 default:
17804 UNREACHABLE();
17805 return static_cast<ExternalArrayType>(-1);
17806 }
17807}
17808
17809
17810size_t JSTypedArray::element_size() {
17811 switch (elements()->map()->instance_type()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017812#define INSTANCE_TYPE_TO_ELEMENT_SIZE(Type, type, TYPE, ctype, size) \
17813 case FIXED_##TYPE##_ARRAY_TYPE: \
17814 return size;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017815
17816 TYPED_ARRAYS(INSTANCE_TYPE_TO_ELEMENT_SIZE)
17817#undef INSTANCE_TYPE_TO_ELEMENT_SIZE
17818
17819 default:
17820 UNREACHABLE();
17821 return 0;
17822 }
17823}
17824
17825
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017826void JSGlobalObject::InvalidatePropertyCell(Handle<JSGlobalObject> global,
17827 Handle<Name> name) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040017828 DCHECK(!global->HasFastProperties());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017829 auto dictionary = handle(global->global_dictionary());
17830 int entry = dictionary->FindEntry(name);
17831 if (entry == GlobalDictionary::kNotFound) return;
17832 PropertyCell::InvalidateEntry(dictionary, entry);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040017833}
17834
17835
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017836// TODO(ishell): rename to EnsureEmptyPropertyCell or something.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017837Handle<PropertyCell> JSGlobalObject::EnsurePropertyCell(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017838 Handle<JSGlobalObject> global, Handle<Name> name) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017839 DCHECK(!global->HasFastProperties());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017840 auto dictionary = handle(global->global_dictionary());
17841 int entry = dictionary->FindEntry(name);
17842 Handle<PropertyCell> cell;
17843 if (entry != GlobalDictionary::kNotFound) {
17844 // This call should be idempotent.
17845 DCHECK(dictionary->ValueAt(entry)->IsPropertyCell());
17846 cell = handle(PropertyCell::cast(dictionary->ValueAt(entry)));
17847 DCHECK(cell->property_details().cell_type() ==
17848 PropertyCellType::kUninitialized ||
17849 cell->property_details().cell_type() ==
17850 PropertyCellType::kInvalidated);
17851 DCHECK(cell->value()->IsTheHole());
Steve Blocka7e24c12009-10-30 11:49:00 +000017852 return cell;
Steve Blocka7e24c12009-10-30 11:49:00 +000017853 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017854 Isolate* isolate = global->GetIsolate();
17855 cell = isolate->factory()->NewPropertyCell();
17856 PropertyDetails details(NONE, DATA, 0, PropertyCellType::kUninitialized);
17857 dictionary = GlobalDictionary::Add(dictionary, name, cell, details);
17858 global->set_properties(*dictionary);
17859 return cell;
Steve Blocka7e24c12009-10-30 11:49:00 +000017860}
17861
17862
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017863// This class is used for looking up two character strings in the string table.
Steve Blockd0582a62009-12-15 09:54:21 +000017864// If we don't have a hit we don't want to waste much time so we unroll the
17865// string hash calculation loop here for speed. Doesn't work if the two
17866// characters form a decimal integer, since such strings have a different hash
17867// algorithm.
17868class TwoCharHashTableKey : public HashTableKey {
17869 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017870 TwoCharHashTableKey(uint16_t c1, uint16_t c2, uint32_t seed)
Steve Blockd0582a62009-12-15 09:54:21 +000017871 : c1_(c1), c2_(c2) {
17872 // Char 1.
Ben Murdochc7cc0282012-03-05 14:35:55 +000017873 uint32_t hash = seed;
17874 hash += c1;
17875 hash += hash << 10;
Steve Blockd0582a62009-12-15 09:54:21 +000017876 hash ^= hash >> 6;
17877 // Char 2.
17878 hash += c2;
17879 hash += hash << 10;
17880 hash ^= hash >> 6;
17881 // GetHash.
17882 hash += hash << 3;
17883 hash ^= hash >> 11;
17884 hash += hash << 15;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017885 if ((hash & String::kHashBitMask) == 0) hash = StringHasher::kZeroHash;
17886 hash_ = hash;
Steve Blockd0582a62009-12-15 09:54:21 +000017887#ifdef DEBUG
Steve Blockd0582a62009-12-15 09:54:21 +000017888 // If this assert fails then we failed to reproduce the two-character
17889 // version of the string hashing algorithm above. One reason could be
17890 // that we were passed two digits as characters, since the hash
17891 // algorithm is different in that case.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017892 uint16_t chars[2] = {c1, c2};
17893 uint32_t check_hash = StringHasher::HashSequentialString(chars, 2, seed);
17894 hash = (hash << String::kHashShift) | String::kIsNotArrayIndexMask;
17895 DCHECK_EQ(static_cast<int32_t>(hash), static_cast<int32_t>(check_hash));
Steve Blockd0582a62009-12-15 09:54:21 +000017896#endif
Steve Blockd0582a62009-12-15 09:54:21 +000017897 }
17898
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017899 bool IsMatch(Object* o) override {
Steve Blockd0582a62009-12-15 09:54:21 +000017900 if (!o->IsString()) return false;
17901 String* other = String::cast(o);
17902 if (other->length() != 2) return false;
17903 if (other->Get(0) != c1_) return false;
17904 return other->Get(1) == c2_;
17905 }
17906
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017907 uint32_t Hash() override { return hash_; }
17908 uint32_t HashForObject(Object* key) override {
Steve Blockd0582a62009-12-15 09:54:21 +000017909 if (!key->IsString()) return 0;
17910 return String::cast(key)->Hash();
17911 }
17912
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017913 Handle<Object> AsHandle(Isolate* isolate) override {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017914 // The TwoCharHashTableKey is only used for looking in the string
Steve Blockd0582a62009-12-15 09:54:21 +000017915 // table, not for adding to it.
17916 UNREACHABLE();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017917 return MaybeHandle<Object>().ToHandleChecked();
Steve Blockd0582a62009-12-15 09:54:21 +000017918 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000017919
Steve Blockd0582a62009-12-15 09:54:21 +000017920 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017921 uint16_t c1_;
17922 uint16_t c2_;
Steve Blockd0582a62009-12-15 09:54:21 +000017923 uint32_t hash_;
17924};
17925
17926
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017927MaybeHandle<String> StringTable::InternalizeStringIfExists(
17928 Isolate* isolate,
17929 Handle<String> string) {
17930 if (string->IsInternalizedString()) {
17931 return string;
17932 }
17933 return LookupStringIfExists(isolate, string);
17934}
17935
17936
17937MaybeHandle<String> StringTable::LookupStringIfExists(
17938 Isolate* isolate,
17939 Handle<String> string) {
17940 Handle<StringTable> string_table = isolate->factory()->string_table();
17941 InternalizedStringKey key(string);
17942 int entry = string_table->FindEntry(&key);
Steve Blocka7e24c12009-10-30 11:49:00 +000017943 if (entry == kNotFound) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017944 return MaybeHandle<String>();
Steve Blocka7e24c12009-10-30 11:49:00 +000017945 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017946 Handle<String> result(String::cast(string_table->KeyAt(entry)), isolate);
17947 DCHECK(StringShape(*result).IsInternalized());
17948 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +000017949 }
17950}
17951
17952
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017953MaybeHandle<String> StringTable::LookupTwoCharsStringIfExists(
17954 Isolate* isolate,
17955 uint16_t c1,
17956 uint16_t c2) {
17957 Handle<StringTable> string_table = isolate->factory()->string_table();
17958 TwoCharHashTableKey key(c1, c2, isolate->heap()->HashSeed());
17959 int entry = string_table->FindEntry(&key);
Steve Blockd0582a62009-12-15 09:54:21 +000017960 if (entry == kNotFound) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017961 return MaybeHandle<String>();
Steve Blockd0582a62009-12-15 09:54:21 +000017962 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017963 Handle<String> result(String::cast(string_table->KeyAt(entry)), isolate);
17964 DCHECK(StringShape(*result).IsInternalized());
17965 return result;
Steve Blockd0582a62009-12-15 09:54:21 +000017966 }
17967}
17968
17969
Emily Bernierd0a1eb72015-03-24 16:35:39 -040017970void StringTable::EnsureCapacityForDeserialization(Isolate* isolate,
17971 int expected) {
17972 Handle<StringTable> table = isolate->factory()->string_table();
17973 // We need a key instance for the virtual hash function.
Ben Murdochda12d292016-06-02 14:46:10 +010017974 InternalizedStringKey dummy_key(isolate->factory()->empty_string());
Emily Bernierd0a1eb72015-03-24 16:35:39 -040017975 table = StringTable::EnsureCapacity(table, expected, &dummy_key);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017976 isolate->heap()->SetRootStringTable(*table);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040017977}
17978
17979
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017980Handle<String> StringTable::LookupString(Isolate* isolate,
17981 Handle<String> string) {
Ben Murdochda12d292016-06-02 14:46:10 +010017982 if (string->IsConsString() && string->IsFlat()) {
17983 string = String::Flatten(string);
17984 if (string->IsInternalizedString()) return string;
17985 }
17986
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017987 InternalizedStringKey key(string);
Ben Murdochda12d292016-06-02 14:46:10 +010017988 Handle<String> result = LookupKey(isolate, &key);
17989
17990 if (string->IsConsString()) {
17991 Handle<ConsString> cons = Handle<ConsString>::cast(string);
17992 cons->set_first(*result);
17993 cons->set_second(isolate->heap()->empty_string());
17994 }
17995 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +000017996}
17997
17998
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017999Handle<String> StringTable::LookupKey(Isolate* isolate, HashTableKey* key) {
18000 Handle<StringTable> table = isolate->factory()->string_table();
18001 int entry = table->FindEntry(key);
Steve Block9fac8402011-05-12 15:51:54 +010018002
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018003 // String already in table.
Steve Blocka7e24c12009-10-30 11:49:00 +000018004 if (entry != kNotFound) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018005 return handle(String::cast(table->KeyAt(entry)), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +000018006 }
18007
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018008 // Adding new string. Grow table if needed.
18009 table = StringTable::EnsureCapacity(table, 1, key);
Steve Blocka7e24c12009-10-30 11:49:00 +000018010
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018011 // Create string object.
18012 Handle<Object> string = key->AsHandle(isolate);
18013 // There must be no attempts to internalize strings that could throw
18014 // InvalidStringLength error.
18015 CHECK(!string.is_null());
Steve Blocka7e24c12009-10-30 11:49:00 +000018016
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018017 // Add the new string and return it along with the string table.
Steve Blocka7e24c12009-10-30 11:49:00 +000018018 entry = table->FindInsertionEntry(key->Hash());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018019 table->set(EntryToIndex(entry), *string);
Steve Blocka7e24c12009-10-30 11:49:00 +000018020 table->ElementAdded();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018021
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018022 isolate->heap()->SetRootStringTable(*table);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018023 return Handle<String>::cast(string);
Steve Blocka7e24c12009-10-30 11:49:00 +000018024}
18025
18026
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018027String* StringTable::LookupKeyIfExists(Isolate* isolate, HashTableKey* key) {
18028 Handle<StringTable> table = isolate->factory()->string_table();
18029 int entry = table->FindEntry(key);
18030 if (entry != kNotFound) return String::cast(table->KeyAt(entry));
18031 return NULL;
18032}
18033
Ben Murdochda12d292016-06-02 14:46:10 +010018034Handle<StringSet> StringSet::New(Isolate* isolate) {
18035 return HashTable::New(isolate, 0);
18036}
18037
18038Handle<StringSet> StringSet::Add(Handle<StringSet> stringset,
18039 Handle<String> name) {
18040 if (!stringset->Has(name)) {
18041 stringset = EnsureCapacity(stringset, 1, *name);
18042 uint32_t hash = StringSetShape::Hash(*name);
18043 int entry = stringset->FindInsertionEntry(hash);
18044 stringset->set(EntryToIndex(entry), *name);
18045 stringset->ElementAdded();
18046 }
18047 return stringset;
18048}
18049
18050bool StringSet::Has(Handle<String> name) {
18051 return FindEntry(*name) != kNotFound;
18052}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018053
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018054Handle<Object> CompilationCacheTable::Lookup(Handle<String> src,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018055 Handle<Context> context,
18056 LanguageMode language_mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018057 Isolate* isolate = GetIsolate();
18058 Handle<SharedFunctionInfo> shared(context->closure()->shared());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018059 StringSharedKey key(src, shared, language_mode, RelocInfo::kNoPosition);
Steve Blocka7e24c12009-10-30 11:49:00 +000018060 int entry = FindEntry(&key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018061 if (entry == kNotFound) return isolate->factory()->undefined_value();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018062 int index = EntryToIndex(entry);
18063 if (!get(index)->IsFixedArray()) return isolate->factory()->undefined_value();
18064 return Handle<Object>(get(index + 1), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +000018065}
18066
18067
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018068Handle<Object> CompilationCacheTable::LookupEval(
18069 Handle<String> src, Handle<SharedFunctionInfo> outer_info,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018070 LanguageMode language_mode, int scope_position) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018071 Isolate* isolate = GetIsolate();
18072 // Cache key is the tuple (source, outer shared function info, scope position)
18073 // to unambiguously identify the context chain the cached eval code assumes.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018074 StringSharedKey key(src, outer_info, language_mode, scope_position);
Steve Blocka7e24c12009-10-30 11:49:00 +000018075 int entry = FindEntry(&key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018076 if (entry == kNotFound) return isolate->factory()->undefined_value();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018077 int index = EntryToIndex(entry);
18078 if (!get(index)->IsFixedArray()) return isolate->factory()->undefined_value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018079 return Handle<Object>(get(EntryToIndex(entry) + 1), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +000018080}
18081
18082
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018083Handle<Object> CompilationCacheTable::LookupRegExp(Handle<String> src,
18084 JSRegExp::Flags flags) {
18085 Isolate* isolate = GetIsolate();
18086 DisallowHeapAllocation no_allocation;
Steve Blocka7e24c12009-10-30 11:49:00 +000018087 RegExpKey key(src, flags);
18088 int entry = FindEntry(&key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018089 if (entry == kNotFound) return isolate->factory()->undefined_value();
18090 return Handle<Object>(get(EntryToIndex(entry) + 1), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +000018091}
18092
18093
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018094Handle<CompilationCacheTable> CompilationCacheTable::Put(
18095 Handle<CompilationCacheTable> cache, Handle<String> src,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018096 Handle<Context> context, LanguageMode language_mode, Handle<Object> value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018097 Isolate* isolate = cache->GetIsolate();
18098 Handle<SharedFunctionInfo> shared(context->closure()->shared());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018099 StringSharedKey key(src, shared, language_mode, RelocInfo::kNoPosition);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018100 {
18101 Handle<Object> k = key.AsHandle(isolate);
18102 DisallowHeapAllocation no_allocation_scope;
18103 int entry = cache->FindEntry(&key);
18104 if (entry != kNotFound) {
18105 cache->set(EntryToIndex(entry), *k);
18106 cache->set(EntryToIndex(entry) + 1, *value);
18107 return cache;
18108 }
18109 }
18110
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018111 cache = EnsureCapacity(cache, 1, &key);
Steve Blocka7e24c12009-10-30 11:49:00 +000018112 int entry = cache->FindInsertionEntry(key.Hash());
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018113 Handle<Object> k =
18114 isolate->factory()->NewNumber(static_cast<double>(key.Hash()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018115 cache->set(EntryToIndex(entry), *k);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018116 cache->set(EntryToIndex(entry) + 1, Smi::FromInt(kHashGenerations));
Steve Blocka7e24c12009-10-30 11:49:00 +000018117 cache->ElementAdded();
18118 return cache;
18119}
18120
18121
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018122Handle<CompilationCacheTable> CompilationCacheTable::PutEval(
18123 Handle<CompilationCacheTable> cache, Handle<String> src,
18124 Handle<SharedFunctionInfo> outer_info, Handle<SharedFunctionInfo> value,
18125 int scope_position) {
18126 Isolate* isolate = cache->GetIsolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018127 StringSharedKey key(src, outer_info, value->language_mode(), scope_position);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018128 {
18129 Handle<Object> k = key.AsHandle(isolate);
18130 DisallowHeapAllocation no_allocation_scope;
18131 int entry = cache->FindEntry(&key);
18132 if (entry != kNotFound) {
18133 cache->set(EntryToIndex(entry), *k);
18134 cache->set(EntryToIndex(entry) + 1, *value);
18135 return cache;
18136 }
18137 }
18138
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018139 cache = EnsureCapacity(cache, 1, &key);
Steve Blocka7e24c12009-10-30 11:49:00 +000018140 int entry = cache->FindInsertionEntry(key.Hash());
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018141 Handle<Object> k =
18142 isolate->factory()->NewNumber(static_cast<double>(key.Hash()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018143 cache->set(EntryToIndex(entry), *k);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018144 cache->set(EntryToIndex(entry) + 1, Smi::FromInt(kHashGenerations));
Steve Blocka7e24c12009-10-30 11:49:00 +000018145 cache->ElementAdded();
18146 return cache;
18147}
18148
18149
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018150Handle<CompilationCacheTable> CompilationCacheTable::PutRegExp(
18151 Handle<CompilationCacheTable> cache, Handle<String> src,
18152 JSRegExp::Flags flags, Handle<FixedArray> value) {
Steve Blocka7e24c12009-10-30 11:49:00 +000018153 RegExpKey key(src, flags);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018154 cache = EnsureCapacity(cache, 1, &key);
Steve Blocka7e24c12009-10-30 11:49:00 +000018155 int entry = cache->FindInsertionEntry(key.Hash());
Steve Block3ce2e202009-11-05 08:53:23 +000018156 // We store the value in the key slot, and compare the search key
18157 // to the stored value with a custon IsMatch function during lookups.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018158 cache->set(EntryToIndex(entry), *value);
18159 cache->set(EntryToIndex(entry) + 1, *value);
Steve Blocka7e24c12009-10-30 11:49:00 +000018160 cache->ElementAdded();
18161 return cache;
18162}
18163
18164
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018165void CompilationCacheTable::Age() {
18166 DisallowHeapAllocation no_allocation;
18167 Object* the_hole_value = GetHeap()->the_hole_value();
18168 for (int entry = 0, size = Capacity(); entry < size; entry++) {
18169 int entry_index = EntryToIndex(entry);
18170 int value_index = entry_index + 1;
18171
18172 if (get(entry_index)->IsNumber()) {
18173 Smi* count = Smi::cast(get(value_index));
18174 count = Smi::FromInt(count->value() - 1);
18175 if (count->value() == 0) {
18176 NoWriteBarrierSet(this, entry_index, the_hole_value);
18177 NoWriteBarrierSet(this, value_index, the_hole_value);
18178 ElementRemoved();
18179 } else {
18180 NoWriteBarrierSet(this, value_index, count);
18181 }
18182 } else if (get(entry_index)->IsFixedArray()) {
18183 SharedFunctionInfo* info = SharedFunctionInfo::cast(get(value_index));
18184 if (info->code()->kind() != Code::FUNCTION || info->code()->IsOld()) {
18185 NoWriteBarrierSet(this, entry_index, the_hole_value);
18186 NoWriteBarrierSet(this, value_index, the_hole_value);
18187 ElementRemoved();
18188 }
18189 }
18190 }
18191}
18192
18193
Ben Murdochb0fe1622011-05-05 13:52:32 +010018194void CompilationCacheTable::Remove(Object* value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018195 DisallowHeapAllocation no_allocation;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010018196 Object* the_hole_value = GetHeap()->the_hole_value();
Ben Murdochb0fe1622011-05-05 13:52:32 +010018197 for (int entry = 0, size = Capacity(); entry < size; entry++) {
18198 int entry_index = EntryToIndex(entry);
18199 int value_index = entry_index + 1;
18200 if (get(value_index) == value) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010018201 NoWriteBarrierSet(this, entry_index, the_hole_value);
18202 NoWriteBarrierSet(this, value_index, the_hole_value);
Ben Murdochb0fe1622011-05-05 13:52:32 +010018203 ElementRemoved();
18204 }
18205 }
18206 return;
18207}
18208
18209
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018210template<typename Derived, typename Shape, typename Key>
18211Handle<Derived> Dictionary<Derived, Shape, Key>::New(
18212 Isolate* isolate,
18213 int at_least_space_for,
18214 PretenureFlag pretenure) {
18215 DCHECK(0 <= at_least_space_for);
18216 Handle<Derived> dict = DerivedHashTable::New(isolate,
18217 at_least_space_for,
18218 USE_DEFAULT_MINIMUM_CAPACITY,
18219 pretenure);
18220
John Reck59135872010-11-02 12:39:01 -070018221 // Initialize the next enumeration index.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018222 dict->SetNextEnumerationIndex(PropertyDetails::kInitialIndex);
18223 return dict;
Steve Blocka7e24c12009-10-30 11:49:00 +000018224}
18225
18226
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018227template <typename Derived, typename Shape, typename Key>
18228Handle<FixedArray> Dictionary<Derived, Shape, Key>::BuildIterationIndicesArray(
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018229 Handle<Derived> dictionary) {
18230 Factory* factory = dictionary->GetIsolate()->factory();
18231 int length = dictionary->NumberOfElements();
Steve Blocka7e24c12009-10-30 11:49:00 +000018232
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018233 Handle<FixedArray> iteration_order = factory->NewFixedArray(length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018234 Handle<FixedArray> enumeration_order = factory->NewFixedArray(length);
Steve Blocka7e24c12009-10-30 11:49:00 +000018235
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018236 // Fill both the iteration order array and the enumeration order array
18237 // with property details.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018238 int capacity = dictionary->Capacity();
Steve Blocka7e24c12009-10-30 11:49:00 +000018239 int pos = 0;
18240 for (int i = 0; i < capacity; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018241 if (dictionary->IsKey(dictionary->KeyAt(i))) {
18242 int index = dictionary->DetailsAt(i).dictionary_index();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018243 iteration_order->set(pos, Smi::FromInt(i));
18244 enumeration_order->set(pos, Smi::FromInt(index));
18245 pos++;
Steve Blocka7e24c12009-10-30 11:49:00 +000018246 }
18247 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018248 DCHECK(pos == length);
Steve Blocka7e24c12009-10-30 11:49:00 +000018249
18250 // Sort the arrays wrt. enumeration order.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018251 iteration_order->SortPairs(*enumeration_order, enumeration_order->length());
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018252 return iteration_order;
18253}
Steve Blocka7e24c12009-10-30 11:49:00 +000018254
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018255
18256template <typename Derived, typename Shape, typename Key>
18257Handle<FixedArray>
18258Dictionary<Derived, Shape, Key>::GenerateNewEnumerationIndices(
18259 Handle<Derived> dictionary) {
18260 int length = dictionary->NumberOfElements();
18261
18262 Handle<FixedArray> iteration_order = BuildIterationIndicesArray(dictionary);
18263 DCHECK(iteration_order->length() == length);
18264
18265 // Iterate over the dictionary using the enumeration order and update
18266 // the dictionary with new enumeration indices.
Steve Blocka7e24c12009-10-30 11:49:00 +000018267 for (int i = 0; i < length; i++) {
18268 int index = Smi::cast(iteration_order->get(i))->value();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018269 DCHECK(dictionary->IsKey(dictionary->KeyAt(index)));
Steve Blocka7e24c12009-10-30 11:49:00 +000018270
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018271 int enum_index = PropertyDetails::kInitialIndex + i;
18272
18273 PropertyDetails details = dictionary->DetailsAt(index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018274 PropertyDetails new_details = details.set_index(enum_index);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018275 dictionary->DetailsAtPut(index, new_details);
Steve Blocka7e24c12009-10-30 11:49:00 +000018276 }
18277
18278 // Set the next enumeration index.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018279 dictionary->SetNextEnumerationIndex(PropertyDetails::kInitialIndex+length);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018280 return iteration_order;
Steve Blocka7e24c12009-10-30 11:49:00 +000018281}
18282
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018283
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018284template <typename Derived, typename Shape, typename Key>
18285void Dictionary<Derived, Shape, Key>::SetRequiresCopyOnCapacityChange() {
18286 DCHECK_EQ(0, DerivedHashTable::NumberOfElements());
18287 DCHECK_EQ(0, DerivedHashTable::NumberOfDeletedElements());
18288 // Make sure that HashTable::EnsureCapacity will create a copy.
18289 DerivedHashTable::SetNumberOfDeletedElements(DerivedHashTable::Capacity());
18290 DCHECK(!DerivedHashTable::HasSufficientCapacity(1));
18291}
18292
18293
18294template <typename Derived, typename Shape, typename Key>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018295Handle<Derived> Dictionary<Derived, Shape, Key>::EnsureCapacity(
18296 Handle<Derived> dictionary, int n, Key key) {
Steve Blocka7e24c12009-10-30 11:49:00 +000018297 // Check whether there are enough enumeration indices to add n elements.
18298 if (Shape::kIsEnumerable &&
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018299 !PropertyDetails::IsValidIndex(dictionary->NextEnumerationIndex() + n)) {
Steve Blocka7e24c12009-10-30 11:49:00 +000018300 // If not, we generate new indices for the properties.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018301 GenerateNewEnumerationIndices(dictionary);
Steve Blocka7e24c12009-10-30 11:49:00 +000018302 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018303 return DerivedHashTable::EnsureCapacity(dictionary, n, key);
Steve Blocka7e24c12009-10-30 11:49:00 +000018304}
18305
18306
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018307template <typename Derived, typename Shape, typename Key>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018308Handle<Object> Dictionary<Derived, Shape, Key>::DeleteProperty(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018309 Handle<Derived> dictionary, int entry) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018310 Factory* factory = dictionary->GetIsolate()->factory();
18311 PropertyDetails details = dictionary->DetailsAt(entry);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018312 if (!details.IsConfigurable()) return factory->false_value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018313
18314 dictionary->SetEntry(
18315 entry, factory->the_hole_value(), factory->the_hole_value());
18316 dictionary->ElementRemoved();
18317 return factory->true_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000018318}
18319
18320
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018321template<typename Derived, typename Shape, typename Key>
18322Handle<Derived> Dictionary<Derived, Shape, Key>::AtPut(
18323 Handle<Derived> dictionary, Key key, Handle<Object> value) {
18324 int entry = dictionary->FindEntry(key);
Steve Blocka7e24c12009-10-30 11:49:00 +000018325
18326 // If the entry is present set the value;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018327 if (entry != Dictionary::kNotFound) {
18328 dictionary->ValueAtPut(entry, *value);
18329 return dictionary;
Steve Blocka7e24c12009-10-30 11:49:00 +000018330 }
18331
18332 // Check whether the dictionary should be extended.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018333 dictionary = EnsureCapacity(dictionary, 1, key);
18334#ifdef DEBUG
18335 USE(Shape::AsHandle(dictionary->GetIsolate(), key));
18336#endif
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018337 PropertyDetails details = PropertyDetails::Empty();
Steve Blocka7e24c12009-10-30 11:49:00 +000018338
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018339 AddEntry(dictionary, key, value, details, dictionary->Hash(key));
18340 return dictionary;
Steve Blocka7e24c12009-10-30 11:49:00 +000018341}
18342
18343
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018344template<typename Derived, typename Shape, typename Key>
18345Handle<Derived> Dictionary<Derived, Shape, Key>::Add(
18346 Handle<Derived> dictionary,
18347 Key key,
18348 Handle<Object> value,
18349 PropertyDetails details) {
Steve Blocka7e24c12009-10-30 11:49:00 +000018350 // Valdate key is absent.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018351 SLOW_DCHECK((dictionary->FindEntry(key) == Dictionary::kNotFound));
Steve Blocka7e24c12009-10-30 11:49:00 +000018352 // Check whether the dictionary should be extended.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018353 dictionary = EnsureCapacity(dictionary, 1, key);
Ben Murdochc7cc0282012-03-05 14:35:55 +000018354
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018355 AddEntry(dictionary, key, value, details, dictionary->Hash(key));
18356 return dictionary;
Steve Blocka7e24c12009-10-30 11:49:00 +000018357}
18358
18359
18360// Add a key, value pair to the dictionary.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018361template<typename Derived, typename Shape, typename Key>
18362void Dictionary<Derived, Shape, Key>::AddEntry(
18363 Handle<Derived> dictionary,
18364 Key key,
18365 Handle<Object> value,
18366 PropertyDetails details,
18367 uint32_t hash) {
Steve Blocka7e24c12009-10-30 11:49:00 +000018368 // Compute the key object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018369 Handle<Object> k = Shape::AsHandle(dictionary->GetIsolate(), key);
Steve Blocka7e24c12009-10-30 11:49:00 +000018370
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018371 uint32_t entry = dictionary->FindInsertionEntry(hash);
Steve Blocka7e24c12009-10-30 11:49:00 +000018372 // Insert element at empty or deleted entry
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018373 if (details.dictionary_index() == 0 && Shape::kIsEnumerable) {
Steve Blocka7e24c12009-10-30 11:49:00 +000018374 // Assign an enumeration index to the property and update
18375 // SetNextEnumerationIndex.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018376 int index = dictionary->NextEnumerationIndex();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018377 details = details.set_index(index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018378 dictionary->SetNextEnumerationIndex(index + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +000018379 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018380 dictionary->SetEntry(entry, k, value, details);
18381 DCHECK((dictionary->KeyAt(entry)->IsNumber() ||
18382 dictionary->KeyAt(entry)->IsName()));
18383 dictionary->ElementAdded();
Steve Blocka7e24c12009-10-30 11:49:00 +000018384}
18385
Ben Murdochda12d292016-06-02 14:46:10 +010018386bool SeededNumberDictionary::HasComplexElements() {
18387 if (!requires_slow_elements()) return false;
18388 int capacity = this->Capacity();
18389 for (int i = 0; i < capacity; i++) {
18390 Object* k = this->KeyAt(i);
18391 if (this->IsKey(k)) {
18392 DCHECK(!IsDeleted(i));
18393 PropertyDetails details = this->DetailsAt(i);
18394 if (details.type() == ACCESSOR_CONSTANT) return true;
18395 PropertyAttributes attr = details.attributes();
18396 if (attr & ALL_ATTRIBUTES_MASK) return true;
18397 }
18398 }
18399 return false;
18400}
Steve Blocka7e24c12009-10-30 11:49:00 +000018401
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018402void SeededNumberDictionary::UpdateMaxNumberKey(uint32_t key,
18403 bool used_as_prototype) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018404 DisallowHeapAllocation no_allocation;
Steve Blocka7e24c12009-10-30 11:49:00 +000018405 // If the dictionary requires slow elements an element has already
18406 // been added at a high index.
18407 if (requires_slow_elements()) return;
18408 // Check if this index is high enough that we should require slow
18409 // elements.
18410 if (key > kRequiresSlowElementsLimit) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018411 if (used_as_prototype) {
18412 // TODO(verwaest): Remove this hack.
18413 TypeFeedbackVector::ClearAllKeyedStoreICs(GetIsolate());
18414 }
Steve Blocka7e24c12009-10-30 11:49:00 +000018415 set_requires_slow_elements();
18416 return;
18417 }
18418 // Update max key value.
18419 Object* max_index_object = get(kMaxNumberKeyIndex);
18420 if (!max_index_object->IsSmi() || max_number_key() < key) {
18421 FixedArray::set(kMaxNumberKeyIndex,
Leon Clarke4515c472010-02-03 11:58:03 +000018422 Smi::FromInt(key << kRequiresSlowElementsTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +000018423 }
18424}
18425
18426
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018427Handle<SeededNumberDictionary> SeededNumberDictionary::AddNumberEntry(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018428 Handle<SeededNumberDictionary> dictionary, uint32_t key,
18429 Handle<Object> value, PropertyDetails details, bool used_as_prototype) {
18430 dictionary->UpdateMaxNumberKey(key, used_as_prototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018431 SLOW_DCHECK(dictionary->FindEntry(key) == kNotFound);
18432 return Add(dictionary, key, value, details);
Steve Blocka7e24c12009-10-30 11:49:00 +000018433}
18434
18435
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018436Handle<UnseededNumberDictionary> UnseededNumberDictionary::AddNumberEntry(
18437 Handle<UnseededNumberDictionary> dictionary,
18438 uint32_t key,
18439 Handle<Object> value) {
18440 SLOW_DCHECK(dictionary->FindEntry(key) == kNotFound);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018441 return Add(dictionary, key, value, PropertyDetails::Empty());
Ben Murdochc7cc0282012-03-05 14:35:55 +000018442}
18443
18444
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018445Handle<SeededNumberDictionary> SeededNumberDictionary::AtNumberPut(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018446 Handle<SeededNumberDictionary> dictionary, uint32_t key,
18447 Handle<Object> value, bool used_as_prototype) {
18448 dictionary->UpdateMaxNumberKey(key, used_as_prototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018449 return AtPut(dictionary, key, value);
Steve Blocka7e24c12009-10-30 11:49:00 +000018450}
18451
18452
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018453Handle<UnseededNumberDictionary> UnseededNumberDictionary::AtNumberPut(
18454 Handle<UnseededNumberDictionary> dictionary,
18455 uint32_t key,
18456 Handle<Object> value) {
18457 return AtPut(dictionary, key, value);
Ben Murdochc7cc0282012-03-05 14:35:55 +000018458}
18459
18460
Ben Murdoch3ef787d2012-04-12 10:51:47 +010018461Handle<SeededNumberDictionary> SeededNumberDictionary::Set(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018462 Handle<SeededNumberDictionary> dictionary, uint32_t key,
18463 Handle<Object> value, PropertyDetails details, bool used_as_prototype) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018464 int entry = dictionary->FindEntry(key);
18465 if (entry == kNotFound) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018466 return AddNumberEntry(dictionary, key, value, details, used_as_prototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018467 }
18468 // Preserve enumeration index.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018469 details = details.set_index(dictionary->DetailsAt(entry).dictionary_index());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018470 Handle<Object> object_key =
18471 SeededNumberDictionaryShape::AsHandle(dictionary->GetIsolate(), key);
18472 dictionary->SetEntry(entry, object_key, value, details);
18473 return dictionary;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010018474}
18475
18476
18477Handle<UnseededNumberDictionary> UnseededNumberDictionary::Set(
18478 Handle<UnseededNumberDictionary> dictionary,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018479 uint32_t key,
Ben Murdoch3ef787d2012-04-12 10:51:47 +010018480 Handle<Object> value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018481 int entry = dictionary->FindEntry(key);
18482 if (entry == kNotFound) return AddNumberEntry(dictionary, key, value);
18483 Handle<Object> object_key =
18484 UnseededNumberDictionaryShape::AsHandle(dictionary->GetIsolate(), key);
18485 dictionary->SetEntry(entry, object_key, value);
18486 return dictionary;
Ben Murdochc7cc0282012-03-05 14:35:55 +000018487}
18488
18489
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018490template <typename Derived, typename Shape, typename Key>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018491int Dictionary<Derived, Shape, Key>::NumberOfElementsFilterAttributes(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018492 PropertyFilter filter) {
18493 int capacity = this->Capacity();
Steve Blocka7e24c12009-10-30 11:49:00 +000018494 int result = 0;
18495 for (int i = 0; i < capacity; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018496 Object* k = this->KeyAt(i);
18497 if (this->IsKey(k) && !k->FilterKey(filter)) {
18498 if (this->IsDeleted(i)) continue;
18499 PropertyDetails details = this->DetailsAt(i);
Steve Blocka7e24c12009-10-30 11:49:00 +000018500 PropertyAttributes attr = details.attributes();
18501 if ((attr & filter) == 0) result++;
18502 }
18503 }
18504 return result;
18505}
18506
18507
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018508template <typename Dictionary>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018509struct EnumIndexComparator {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018510 explicit EnumIndexComparator(Dictionary* dict) : dict(dict) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018511 bool operator() (Smi* a, Smi* b) {
18512 PropertyDetails da(dict->DetailsAt(a->value()));
18513 PropertyDetails db(dict->DetailsAt(b->value()));
18514 return da.dictionary_index() < db.dictionary_index();
18515 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018516 Dictionary* dict;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018517};
18518
18519
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018520template <typename Derived, typename Shape, typename Key>
18521void Dictionary<Derived, Shape, Key>::CopyEnumKeysTo(FixedArray* storage) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018522 int length = storage->length();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018523 int capacity = this->Capacity();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018524 int properties = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +000018525 for (int i = 0; i < capacity; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018526 Object* k = this->KeyAt(i);
18527 if (this->IsKey(k) && !k->IsSymbol()) {
18528 PropertyDetails details = this->DetailsAt(i);
18529 if (details.IsDontEnum() || this->IsDeleted(i)) continue;
18530 storage->set(properties, Smi::FromInt(i));
18531 properties++;
18532 if (properties == length) break;
18533 }
Steve Blocka7e24c12009-10-30 11:49:00 +000018534 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018535 CHECK_EQ(length, properties);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018536 EnumIndexComparator<Derived> cmp(static_cast<Derived*>(this));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018537 Smi** start = reinterpret_cast<Smi**>(storage->GetFirstElementAddress());
18538 std::sort(start, start + length, cmp);
18539 for (int i = 0; i < length; i++) {
18540 int index = Smi::cast(storage->get(i))->value();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018541 storage->set(i, this->KeyAt(index));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018542 }
Steve Blocka7e24c12009-10-30 11:49:00 +000018543}
18544
18545
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018546template <typename Derived, typename Shape, typename Key>
18547int Dictionary<Derived, Shape, Key>::CopyKeysTo(
18548 FixedArray* storage, int index, PropertyFilter filter,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018549 typename Dictionary<Derived, Shape, Key>::SortMode sort_mode) {
18550 DCHECK(storage->length() >= NumberOfElementsFilterAttributes(filter));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018551 int start_index = index;
18552 int capacity = this->Capacity();
Steve Blocka7e24c12009-10-30 11:49:00 +000018553 for (int i = 0; i < capacity; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018554 Object* k = this->KeyAt(i);
18555 if (!this->IsKey(k) || k->FilterKey(filter)) continue;
18556 if (this->IsDeleted(i)) continue;
18557 PropertyDetails details = this->DetailsAt(i);
18558 PropertyAttributes attr = details.attributes();
18559 if ((attr & filter) != 0) continue;
18560 storage->set(index++, k);
Steve Blocka7e24c12009-10-30 11:49:00 +000018561 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018562 if (sort_mode == Dictionary::SORTED) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000018563 storage->SortPairs(storage, index);
18564 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018565 DCHECK(storage->length() >= index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018566 return index - start_index;
18567}
18568
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018569template <typename Derived, typename Shape, typename Key>
18570void Dictionary<Derived, Shape, Key>::CollectKeysTo(
18571 Handle<Dictionary<Derived, Shape, Key> > dictionary, KeyAccumulator* keys,
18572 PropertyFilter filter) {
18573 int capacity = dictionary->Capacity();
18574 Handle<FixedArray> array =
18575 keys->isolate()->factory()->NewFixedArray(dictionary->NumberOfElements());
18576 int array_size = 0;
18577
18578 {
18579 DisallowHeapAllocation no_gc;
18580 Dictionary<Derived, Shape, Key>* raw_dict = *dictionary;
18581 for (int i = 0; i < capacity; i++) {
18582 Object* k = raw_dict->KeyAt(i);
18583 if (!raw_dict->IsKey(k) || k->FilterKey(filter)) continue;
18584 if (raw_dict->IsDeleted(i)) continue;
18585 PropertyDetails details = raw_dict->DetailsAt(i);
18586 if ((details.attributes() & filter) != 0) continue;
18587 if (filter & ONLY_ALL_CAN_READ) {
18588 if (details.kind() != kAccessor) continue;
18589 Object* accessors = raw_dict->ValueAt(i);
18590 if (accessors->IsPropertyCell()) {
18591 accessors = PropertyCell::cast(accessors)->value();
18592 }
18593 if (!accessors->IsAccessorInfo()) continue;
18594 if (!AccessorInfo::cast(accessors)->all_can_read()) continue;
18595 }
18596 array->set(array_size++, Smi::FromInt(i));
18597 }
18598
18599 EnumIndexComparator<Derived> cmp(static_cast<Derived*>(raw_dict));
18600 Smi** start = reinterpret_cast<Smi**>(array->GetFirstElementAddress());
18601 std::sort(start, start + array_size, cmp);
18602 }
18603
18604 for (int i = 0; i < array_size; i++) {
18605 int index = Smi::cast(array->get(i))->value();
Ben Murdoch097c5b22016-05-18 11:27:45 +010018606 keys->AddKey(dictionary->KeyAt(index), DO_NOT_CONVERT);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018607 }
Steve Blocka7e24c12009-10-30 11:49:00 +000018608}
18609
18610
18611// Backwards lookup (slow).
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018612template<typename Derived, typename Shape, typename Key>
18613Object* Dictionary<Derived, Shape, Key>::SlowReverseLookup(Object* value) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018614 int capacity = this->Capacity();
Steve Blocka7e24c12009-10-30 11:49:00 +000018615 for (int i = 0; i < capacity; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018616 Object* k = this->KeyAt(i);
18617 if (this->IsKey(k)) {
18618 Object* e = this->ValueAt(i);
18619 // TODO(dcarney): this should be templatized.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018620 if (e->IsPropertyCell()) {
18621 e = PropertyCell::cast(e)->value();
Steve Blocka7e24c12009-10-30 11:49:00 +000018622 }
18623 if (e == value) return k;
18624 }
18625 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018626 Heap* heap = Dictionary::GetHeap();
Steve Block44f0eee2011-05-26 01:26:41 +010018627 return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000018628}
18629
18630
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018631Object* ObjectHashTable::Lookup(Isolate* isolate, Handle<Object> key,
18632 int32_t hash) {
18633 DisallowHeapAllocation no_gc;
18634 DCHECK(IsKey(*key));
18635
18636 int entry = FindEntry(isolate, key, hash);
18637 if (entry == kNotFound) return isolate->heap()->the_hole_value();
18638 return get(EntryToIndex(entry) + 1);
18639}
18640
18641
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018642Object* ObjectHashTable::Lookup(Handle<Object> key) {
18643 DisallowHeapAllocation no_gc;
18644 DCHECK(IsKey(*key));
Ben Murdoch3ef787d2012-04-12 10:51:47 +010018645
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018646 Isolate* isolate = GetIsolate();
18647
Ben Murdoch69a99ed2011-11-30 16:03:39 +000018648 // If the object does not have an identity hash, it was never used as a key.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018649 Object* hash = key->GetHash();
18650 if (hash->IsUndefined()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018651 return isolate->heap()->the_hole_value();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010018652 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018653 return Lookup(isolate, key, Smi::cast(hash)->value());
18654}
18655
18656
18657Object* ObjectHashTable::Lookup(Handle<Object> key, int32_t hash) {
18658 return Lookup(GetIsolate(), key, hash);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000018659}
18660
18661
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018662Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table,
18663 Handle<Object> key,
18664 Handle<Object> value) {
18665 DCHECK(table->IsKey(*key));
18666 DCHECK(!value->IsTheHole());
18667
18668 Isolate* isolate = table->GetIsolate();
Ben Murdoch69a99ed2011-11-30 16:03:39 +000018669 // Make sure the key object has an identity hash code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018670 int32_t hash = Object::GetOrCreateHash(isolate, key)->value();
Ben Murdoch69a99ed2011-11-30 16:03:39 +000018671
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018672 return Put(table, key, value, hash);
18673}
18674
18675
18676Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table,
18677 Handle<Object> key,
18678 Handle<Object> value,
18679 int32_t hash) {
18680 DCHECK(table->IsKey(*key));
18681 DCHECK(!value->IsTheHole());
18682
18683 Isolate* isolate = table->GetIsolate();
18684
18685 int entry = table->FindEntry(isolate, key, hash);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000018686
18687 // Key is already in table, just overwrite value.
18688 if (entry != kNotFound) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018689 table->set(EntryToIndex(entry) + 1, *value);
18690 return table;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000018691 }
18692
Ben Murdoch097c5b22016-05-18 11:27:45 +010018693 // Rehash if more than 25% of the entries are deleted entries.
18694 // TODO(jochen): Consider to shrink the fixed array in place.
18695 if ((table->NumberOfDeletedElements() << 1) > table->NumberOfElements()) {
18696 table->Rehash(isolate->factory()->undefined_value());
18697 }
18698
Ben Murdoch69a99ed2011-11-30 16:03:39 +000018699 // Check whether the hash table should be extended.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018700 table = EnsureCapacity(table, 1, key);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018701 table->AddEntry(table->FindInsertionEntry(hash), *key, *value);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000018702 return table;
18703}
18704
18705
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018706Handle<ObjectHashTable> ObjectHashTable::Remove(Handle<ObjectHashTable> table,
18707 Handle<Object> key,
18708 bool* was_present) {
18709 DCHECK(table->IsKey(*key));
18710
18711 Object* hash = key->GetHash();
18712 if (hash->IsUndefined()) {
18713 *was_present = false;
18714 return table;
18715 }
18716
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018717 return Remove(table, key, was_present, Smi::cast(hash)->value());
18718}
18719
18720
18721Handle<ObjectHashTable> ObjectHashTable::Remove(Handle<ObjectHashTable> table,
18722 Handle<Object> key,
18723 bool* was_present,
18724 int32_t hash) {
18725 DCHECK(table->IsKey(*key));
18726
18727 int entry = table->FindEntry(table->GetIsolate(), key, hash);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018728 if (entry == kNotFound) {
18729 *was_present = false;
18730 return table;
18731 }
18732
18733 *was_present = true;
18734 table->RemoveEntry(entry);
18735 return Shrink(table, key);
18736}
18737
18738
Ben Murdoch3ef787d2012-04-12 10:51:47 +010018739void ObjectHashTable::AddEntry(int entry, Object* key, Object* value) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +000018740 set(EntryToIndex(entry), key);
18741 set(EntryToIndex(entry) + 1, value);
18742 ElementAdded();
18743}
18744
18745
Ben Murdoch3ef787d2012-04-12 10:51:47 +010018746void ObjectHashTable::RemoveEntry(int entry) {
18747 set_the_hole(EntryToIndex(entry));
18748 set_the_hole(EntryToIndex(entry) + 1);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000018749 ElementRemoved();
18750}
18751
18752
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018753Object* WeakHashTable::Lookup(Handle<HeapObject> key) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018754 DisallowHeapAllocation no_gc;
18755 DCHECK(IsKey(*key));
18756 int entry = FindEntry(key);
18757 if (entry == kNotFound) return GetHeap()->the_hole_value();
18758 return get(EntryToValueIndex(entry));
18759}
18760
18761
18762Handle<WeakHashTable> WeakHashTable::Put(Handle<WeakHashTable> table,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018763 Handle<HeapObject> key,
18764 Handle<HeapObject> value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018765 DCHECK(table->IsKey(*key));
18766 int entry = table->FindEntry(key);
18767 // Key is already in table, just overwrite value.
18768 if (entry != kNotFound) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018769 table->set(EntryToValueIndex(entry), *value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018770 return table;
18771 }
18772
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018773 Handle<WeakCell> key_cell = key->GetIsolate()->factory()->NewWeakCell(key);
18774
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018775 // Check whether the hash table should be extended.
18776 table = EnsureCapacity(table, 1, key, TENURED);
18777
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018778 table->AddEntry(table->FindInsertionEntry(table->Hash(key)), key_cell, value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018779 return table;
18780}
18781
18782
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018783void WeakHashTable::AddEntry(int entry, Handle<WeakCell> key_cell,
18784 Handle<HeapObject> value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018785 DisallowHeapAllocation no_allocation;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018786 set(EntryToIndex(entry), *key_cell);
18787 set(EntryToValueIndex(entry), *value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018788 ElementAdded();
18789}
18790
18791
18792template<class Derived, class Iterator, int entrysize>
18793Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Allocate(
18794 Isolate* isolate, int capacity, PretenureFlag pretenure) {
18795 // Capacity must be a power of two, since we depend on being able
18796 // to divide and multiple by 2 (kLoadFactor) to derive capacity
18797 // from number of buckets. If we decide to change kLoadFactor
18798 // to something other than 2, capacity should be stored as another
18799 // field of this object.
18800 capacity = base::bits::RoundUpToPowerOfTwo32(Max(kMinCapacity, capacity));
18801 if (capacity > kMaxCapacity) {
18802 v8::internal::Heap::FatalProcessOutOfMemory("invalid table size", true);
18803 }
18804 int num_buckets = capacity / kLoadFactor;
18805 Handle<FixedArray> backing_store = isolate->factory()->NewFixedArray(
18806 kHashTableStartIndex + num_buckets + (capacity * kEntrySize), pretenure);
18807 backing_store->set_map_no_write_barrier(
18808 isolate->heap()->ordered_hash_table_map());
18809 Handle<Derived> table = Handle<Derived>::cast(backing_store);
18810 for (int i = 0; i < num_buckets; ++i) {
18811 table->set(kHashTableStartIndex + i, Smi::FromInt(kNotFound));
18812 }
18813 table->SetNumberOfBuckets(num_buckets);
18814 table->SetNumberOfElements(0);
18815 table->SetNumberOfDeletedElements(0);
18816 return table;
18817}
18818
18819
18820template<class Derived, class Iterator, int entrysize>
18821Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::EnsureGrowable(
18822 Handle<Derived> table) {
18823 DCHECK(!table->IsObsolete());
18824
18825 int nof = table->NumberOfElements();
18826 int nod = table->NumberOfDeletedElements();
18827 int capacity = table->Capacity();
18828 if ((nof + nod) < capacity) return table;
18829 // Don't need to grow if we can simply clear out deleted entries instead.
18830 // Note that we can't compact in place, though, so we always allocate
18831 // a new table.
18832 return Rehash(table, (nod < (capacity >> 1)) ? capacity << 1 : capacity);
18833}
18834
18835
18836template<class Derived, class Iterator, int entrysize>
18837Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Shrink(
18838 Handle<Derived> table) {
18839 DCHECK(!table->IsObsolete());
18840
18841 int nof = table->NumberOfElements();
18842 int capacity = table->Capacity();
18843 if (nof >= (capacity >> 2)) return table;
18844 return Rehash(table, capacity / 2);
18845}
18846
18847
18848template<class Derived, class Iterator, int entrysize>
18849Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Clear(
18850 Handle<Derived> table) {
18851 DCHECK(!table->IsObsolete());
18852
18853 Handle<Derived> new_table =
18854 Allocate(table->GetIsolate(),
18855 kMinCapacity,
18856 table->GetHeap()->InNewSpace(*table) ? NOT_TENURED : TENURED);
18857
18858 table->SetNextTable(*new_table);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018859 table->SetNumberOfDeletedElements(kClearedTableSentinel);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018860
18861 return new_table;
18862}
18863
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018864template <class Derived, class Iterator, int entrysize>
18865bool OrderedHashTable<Derived, Iterator, entrysize>::HasKey(
18866 Handle<Derived> table, Handle<Object> key) {
18867 int entry = table->KeyToFirstEntry(*key);
18868 // Walk the chain in the bucket to find the key.
18869 while (entry != kNotFound) {
18870 Object* candidate_key = table->KeyAt(entry);
18871 if (candidate_key->SameValueZero(*key)) return true;
18872 entry = table->NextChainEntry(entry);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018873 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018874 return false;
18875}
18876
18877
18878Handle<OrderedHashSet> OrderedHashSet::Add(Handle<OrderedHashSet> table,
18879 Handle<Object> key) {
18880 int hash = Object::GetOrCreateHash(table->GetIsolate(), key)->value();
18881 int entry = table->HashToEntry(hash);
18882 // Walk the chain of the bucket and try finding the key.
18883 while (entry != kNotFound) {
18884 Object* candidate_key = table->KeyAt(entry);
18885 // Do not add if we have the key already
18886 if (candidate_key->SameValueZero(*key)) return table;
18887 entry = table->NextChainEntry(entry);
18888 }
18889
18890 table = OrderedHashSet::EnsureGrowable(table);
18891 // Read the existing bucket values.
18892 int bucket = table->HashToBucket(hash);
18893 int previous_entry = table->HashToEntry(hash);
18894 int nof = table->NumberOfElements();
18895 // Insert a new entry at the end,
18896 int new_entry = nof + table->NumberOfDeletedElements();
18897 int new_index = table->EntryToIndex(new_entry);
18898 table->set(new_index, *key);
18899 table->set(new_index + kChainOffset, Smi::FromInt(previous_entry));
18900 // and point the bucket to the new entry.
18901 table->set(kHashTableStartIndex + bucket, Smi::FromInt(new_entry));
18902 table->SetNumberOfElements(nof + 1);
18903 return table;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018904}
18905
18906
18907template<class Derived, class Iterator, int entrysize>
18908Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Rehash(
18909 Handle<Derived> table, int new_capacity) {
18910 DCHECK(!table->IsObsolete());
18911
18912 Handle<Derived> new_table =
18913 Allocate(table->GetIsolate(),
18914 new_capacity,
18915 table->GetHeap()->InNewSpace(*table) ? NOT_TENURED : TENURED);
18916 int nof = table->NumberOfElements();
18917 int nod = table->NumberOfDeletedElements();
18918 int new_buckets = new_table->NumberOfBuckets();
18919 int new_entry = 0;
18920 int removed_holes_index = 0;
18921
18922 for (int old_entry = 0; old_entry < (nof + nod); ++old_entry) {
18923 Object* key = table->KeyAt(old_entry);
18924 if (key->IsTheHole()) {
18925 table->SetRemovedIndexAt(removed_holes_index++, old_entry);
18926 continue;
18927 }
18928
18929 Object* hash = key->GetHash();
18930 int bucket = Smi::cast(hash)->value() & (new_buckets - 1);
18931 Object* chain_entry = new_table->get(kHashTableStartIndex + bucket);
18932 new_table->set(kHashTableStartIndex + bucket, Smi::FromInt(new_entry));
18933 int new_index = new_table->EntryToIndex(new_entry);
18934 int old_index = table->EntryToIndex(old_entry);
18935 for (int i = 0; i < entrysize; ++i) {
18936 Object* value = table->get(old_index + i);
18937 new_table->set(new_index + i, value);
18938 }
18939 new_table->set(new_index + kChainOffset, chain_entry);
18940 ++new_entry;
18941 }
18942
18943 DCHECK_EQ(nod, removed_holes_index);
18944
18945 new_table->SetNumberOfElements(nof);
18946 table->SetNextTable(*new_table);
18947
18948 return new_table;
18949}
18950
18951
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018952template Handle<OrderedHashSet>
18953OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Allocate(
18954 Isolate* isolate, int capacity, PretenureFlag pretenure);
18955
18956template Handle<OrderedHashSet>
18957OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::EnsureGrowable(
18958 Handle<OrderedHashSet> table);
18959
18960template Handle<OrderedHashSet>
18961OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Shrink(
18962 Handle<OrderedHashSet> table);
18963
18964template Handle<OrderedHashSet>
18965OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Clear(
18966 Handle<OrderedHashSet> table);
18967
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018968template bool OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::HasKey(
18969 Handle<OrderedHashSet> table, Handle<Object> key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018970
18971
18972template Handle<OrderedHashMap>
18973OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Allocate(
18974 Isolate* isolate, int capacity, PretenureFlag pretenure);
18975
18976template Handle<OrderedHashMap>
18977OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::EnsureGrowable(
18978 Handle<OrderedHashMap> table);
18979
18980template Handle<OrderedHashMap>
18981OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Shrink(
18982 Handle<OrderedHashMap> table);
18983
18984template Handle<OrderedHashMap>
18985OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Clear(
18986 Handle<OrderedHashMap> table);
18987
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018988template bool OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::HasKey(
18989 Handle<OrderedHashMap> table, Handle<Object> key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018990
18991
18992template<class Derived, class TableType>
18993void OrderedHashTableIterator<Derived, TableType>::Transition() {
18994 DisallowHeapAllocation no_allocation;
18995 TableType* table = TableType::cast(this->table());
18996 if (!table->IsObsolete()) return;
18997
18998 int index = Smi::cast(this->index())->value();
18999 while (table->IsObsolete()) {
19000 TableType* next_table = table->NextTable();
19001
19002 if (index > 0) {
19003 int nod = table->NumberOfDeletedElements();
19004
Emily Bernierd0a1eb72015-03-24 16:35:39 -040019005 if (nod == TableType::kClearedTableSentinel) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019006 index = 0;
19007 } else {
19008 int old_index = index;
19009 for (int i = 0; i < nod; ++i) {
19010 int removed_index = table->RemovedIndexAt(i);
19011 if (removed_index >= old_index) break;
19012 --index;
19013 }
19014 }
19015 }
19016
19017 table = next_table;
19018 }
19019
19020 set_table(table);
19021 set_index(Smi::FromInt(index));
19022}
19023
19024
19025template<class Derived, class TableType>
19026bool OrderedHashTableIterator<Derived, TableType>::HasMore() {
19027 DisallowHeapAllocation no_allocation;
19028 if (this->table()->IsUndefined()) return false;
19029
19030 Transition();
19031
19032 TableType* table = TableType::cast(this->table());
19033 int index = Smi::cast(this->index())->value();
19034 int used_capacity = table->UsedCapacity();
19035
19036 while (index < used_capacity && table->KeyAt(index)->IsTheHole()) {
19037 index++;
19038 }
19039
19040 set_index(Smi::FromInt(index));
19041
19042 if (index < used_capacity) return true;
19043
19044 set_table(GetHeap()->undefined_value());
19045 return false;
19046}
19047
19048
19049template<class Derived, class TableType>
19050Smi* OrderedHashTableIterator<Derived, TableType>::Next(JSArray* value_array) {
19051 DisallowHeapAllocation no_allocation;
19052 if (HasMore()) {
19053 FixedArray* array = FixedArray::cast(value_array->elements());
19054 static_cast<Derived*>(this)->PopulateValueArray(array);
19055 MoveNext();
19056 return Smi::cast(kind());
19057 }
19058 return Smi::FromInt(0);
19059}
19060
19061
19062template Smi*
19063OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::Next(
19064 JSArray* value_array);
19065
19066template bool
19067OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::HasMore();
19068
19069template void
19070OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::MoveNext();
19071
19072template Object*
19073OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::CurrentKey();
19074
19075template void
19076OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::Transition();
19077
19078
19079template Smi*
19080OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::Next(
19081 JSArray* value_array);
19082
19083template bool
19084OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::HasMore();
19085
19086template void
19087OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::MoveNext();
19088
19089template Object*
19090OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::CurrentKey();
19091
19092template void
19093OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::Transition();
19094
19095
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019096void JSSet::Initialize(Handle<JSSet> set, Isolate* isolate) {
19097 Handle<OrderedHashSet> table = isolate->factory()->NewOrderedHashSet();
19098 set->set_table(*table);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019099}
19100
19101
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019102void JSSet::Clear(Handle<JSSet> set) {
19103 Handle<OrderedHashSet> table(OrderedHashSet::cast(set->table()));
19104 table = OrderedHashSet::Clear(table);
19105 set->set_table(*table);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019106}
19107
19108
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019109void JSMap::Initialize(Handle<JSMap> map, Isolate* isolate) {
19110 Handle<OrderedHashMap> table = isolate->factory()->NewOrderedHashMap();
19111 map->set_table(*table);
19112}
19113
19114
19115void JSMap::Clear(Handle<JSMap> map) {
19116 Handle<OrderedHashMap> table(OrderedHashMap::cast(map->table()));
19117 table = OrderedHashMap::Clear(table);
19118 map->set_table(*table);
19119}
19120
19121
19122void JSWeakCollection::Initialize(Handle<JSWeakCollection> weak_collection,
19123 Isolate* isolate) {
19124 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 0);
19125 weak_collection->set_table(*table);
19126}
19127
19128
19129void JSWeakCollection::Set(Handle<JSWeakCollection> weak_collection,
19130 Handle<Object> key, Handle<Object> value,
19131 int32_t hash) {
19132 DCHECK(key->IsJSReceiver() || key->IsSymbol());
19133 Handle<ObjectHashTable> table(
19134 ObjectHashTable::cast(weak_collection->table()));
19135 DCHECK(table->IsKey(*key));
19136 Handle<ObjectHashTable> new_table =
19137 ObjectHashTable::Put(table, key, value, hash);
19138 weak_collection->set_table(*new_table);
19139 if (*table != *new_table) {
19140 // Zap the old table since we didn't record slots for its elements.
19141 table->FillWithHoles(0, table->length());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019142 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019143}
19144
19145
19146bool JSWeakCollection::Delete(Handle<JSWeakCollection> weak_collection,
19147 Handle<Object> key, int32_t hash) {
19148 DCHECK(key->IsJSReceiver() || key->IsSymbol());
19149 Handle<ObjectHashTable> table(
19150 ObjectHashTable::cast(weak_collection->table()));
19151 DCHECK(table->IsKey(*key));
19152 bool was_present = false;
19153 Handle<ObjectHashTable> new_table =
19154 ObjectHashTable::Remove(table, key, &was_present, hash);
19155 weak_collection->set_table(*new_table);
19156 if (*table != *new_table) {
19157 // Zap the old table since we didn't record slots for its elements.
19158 table->FillWithHoles(0, table->length());
19159 }
19160 return was_present;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019161}
19162
Ben Murdoch097c5b22016-05-18 11:27:45 +010019163// Check if there is a break point at this code offset.
19164bool DebugInfo::HasBreakPoint(int code_offset) {
19165 // Get the break point info object for this code offset.
19166 Object* break_point_info = GetBreakPointInfo(code_offset);
Steve Blocka7e24c12009-10-30 11:49:00 +000019167
19168 // If there is no break point info object or no break points in the break
Ben Murdoch097c5b22016-05-18 11:27:45 +010019169 // point info object there is no break point at this code offset.
Steve Blocka7e24c12009-10-30 11:49:00 +000019170 if (break_point_info->IsUndefined()) return false;
19171 return BreakPointInfo::cast(break_point_info)->GetBreakPointCount() > 0;
19172}
19173
Ben Murdoch097c5b22016-05-18 11:27:45 +010019174// Get the break point info object for this code offset.
19175Object* DebugInfo::GetBreakPointInfo(int code_offset) {
19176 // Find the index of the break point info object for this code offset.
19177 int index = GetBreakPointInfoIndex(code_offset);
Steve Blocka7e24c12009-10-30 11:49:00 +000019178
19179 // Return the break point info object if any.
Ben Murdoch8b112d22011-06-08 16:22:53 +010019180 if (index == kNoBreakPointInfo) return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000019181 return BreakPointInfo::cast(break_points()->get(index));
19182}
19183
Ben Murdoch097c5b22016-05-18 11:27:45 +010019184// Clear a break point at the specified code offset.
19185void DebugInfo::ClearBreakPoint(Handle<DebugInfo> debug_info, int code_offset,
Steve Blocka7e24c12009-10-30 11:49:00 +000019186 Handle<Object> break_point_object) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010019187 Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_offset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019188 debug_info->GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +000019189 if (break_point_info->IsUndefined()) return;
19190 BreakPointInfo::ClearBreakPoint(
19191 Handle<BreakPointInfo>::cast(break_point_info),
19192 break_point_object);
19193}
19194
Ben Murdoch097c5b22016-05-18 11:27:45 +010019195void DebugInfo::SetBreakPoint(Handle<DebugInfo> debug_info, int code_offset,
19196 int source_position, int statement_position,
Steve Blocka7e24c12009-10-30 11:49:00 +000019197 Handle<Object> break_point_object) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019198 Isolate* isolate = debug_info->GetIsolate();
Ben Murdoch097c5b22016-05-18 11:27:45 +010019199 Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_offset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019200 isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +000019201 if (!break_point_info->IsUndefined()) {
19202 BreakPointInfo::SetBreakPoint(
19203 Handle<BreakPointInfo>::cast(break_point_info),
19204 break_point_object);
19205 return;
19206 }
19207
Ben Murdoch097c5b22016-05-18 11:27:45 +010019208 // Adding a new break point for a code offset which did not have any
Steve Blocka7e24c12009-10-30 11:49:00 +000019209 // break points before. Try to find a free slot.
19210 int index = kNoBreakPointInfo;
19211 for (int i = 0; i < debug_info->break_points()->length(); i++) {
19212 if (debug_info->break_points()->get(i)->IsUndefined()) {
19213 index = i;
19214 break;
19215 }
19216 }
19217 if (index == kNoBreakPointInfo) {
19218 // No free slot - extend break point info array.
19219 Handle<FixedArray> old_break_points =
19220 Handle<FixedArray>(FixedArray::cast(debug_info->break_points()));
Steve Blocka7e24c12009-10-30 11:49:00 +000019221 Handle<FixedArray> new_break_points =
Steve Block44f0eee2011-05-26 01:26:41 +010019222 isolate->factory()->NewFixedArray(
19223 old_break_points->length() +
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019224 DebugInfo::kEstimatedNofBreakPointsInFunction);
Kristian Monsen0d5e1162010-09-30 15:31:59 +010019225
19226 debug_info->set_break_points(*new_break_points);
Steve Blocka7e24c12009-10-30 11:49:00 +000019227 for (int i = 0; i < old_break_points->length(); i++) {
19228 new_break_points->set(i, old_break_points->get(i));
19229 }
19230 index = old_break_points->length();
19231 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019232 DCHECK(index != kNoBreakPointInfo);
Steve Blocka7e24c12009-10-30 11:49:00 +000019233
19234 // Allocate new BreakPointInfo object and set the break point.
Steve Block44f0eee2011-05-26 01:26:41 +010019235 Handle<BreakPointInfo> new_break_point_info = Handle<BreakPointInfo>::cast(
19236 isolate->factory()->NewStruct(BREAK_POINT_INFO_TYPE));
Ben Murdoch097c5b22016-05-18 11:27:45 +010019237 new_break_point_info->set_code_offset(code_offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019238 new_break_point_info->set_source_position(source_position);
19239 new_break_point_info->set_statement_position(statement_position);
Steve Block44f0eee2011-05-26 01:26:41 +010019240 new_break_point_info->set_break_point_objects(
19241 isolate->heap()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +000019242 BreakPointInfo::SetBreakPoint(new_break_point_info, break_point_object);
19243 debug_info->break_points()->set(index, *new_break_point_info);
19244}
19245
Ben Murdoch097c5b22016-05-18 11:27:45 +010019246// Get the break point objects for a code offset.
19247Handle<Object> DebugInfo::GetBreakPointObjects(int code_offset) {
19248 Object* break_point_info = GetBreakPointInfo(code_offset);
Steve Blocka7e24c12009-10-30 11:49:00 +000019249 if (break_point_info->IsUndefined()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019250 return GetIsolate()->factory()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000019251 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019252 return Handle<Object>(
19253 BreakPointInfo::cast(break_point_info)->break_point_objects(),
19254 GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +000019255}
19256
19257
19258// Get the total number of break points.
19259int DebugInfo::GetBreakPointCount() {
19260 if (break_points()->IsUndefined()) return 0;
19261 int count = 0;
19262 for (int i = 0; i < break_points()->length(); i++) {
19263 if (!break_points()->get(i)->IsUndefined()) {
19264 BreakPointInfo* break_point_info =
19265 BreakPointInfo::cast(break_points()->get(i));
19266 count += break_point_info->GetBreakPointCount();
19267 }
19268 }
19269 return count;
19270}
19271
19272
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019273Handle<Object> DebugInfo::FindBreakPointInfo(
19274 Handle<DebugInfo> debug_info, Handle<Object> break_point_object) {
19275 Isolate* isolate = debug_info->GetIsolate();
19276 if (!debug_info->break_points()->IsUndefined()) {
19277 for (int i = 0; i < debug_info->break_points()->length(); i++) {
19278 if (!debug_info->break_points()->get(i)->IsUndefined()) {
19279 Handle<BreakPointInfo> break_point_info = Handle<BreakPointInfo>(
19280 BreakPointInfo::cast(debug_info->break_points()->get(i)), isolate);
19281 if (BreakPointInfo::HasBreakPointObject(break_point_info,
19282 break_point_object)) {
19283 return break_point_info;
19284 }
Steve Blocka7e24c12009-10-30 11:49:00 +000019285 }
19286 }
19287 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019288 return isolate->factory()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000019289}
19290
19291
19292// Find the index of the break point info object for the specified code
19293// position.
Ben Murdoch097c5b22016-05-18 11:27:45 +010019294int DebugInfo::GetBreakPointInfoIndex(int code_offset) {
Steve Blocka7e24c12009-10-30 11:49:00 +000019295 if (break_points()->IsUndefined()) return kNoBreakPointInfo;
19296 for (int i = 0; i < break_points()->length(); i++) {
19297 if (!break_points()->get(i)->IsUndefined()) {
19298 BreakPointInfo* break_point_info =
19299 BreakPointInfo::cast(break_points()->get(i));
Ben Murdoch097c5b22016-05-18 11:27:45 +010019300 if (break_point_info->code_offset() == code_offset) {
Steve Blocka7e24c12009-10-30 11:49:00 +000019301 return i;
19302 }
19303 }
19304 }
19305 return kNoBreakPointInfo;
19306}
19307
19308
19309// Remove the specified break point object.
19310void BreakPointInfo::ClearBreakPoint(Handle<BreakPointInfo> break_point_info,
19311 Handle<Object> break_point_object) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019312 Isolate* isolate = break_point_info->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +000019313 // If there are no break points just ignore.
19314 if (break_point_info->break_point_objects()->IsUndefined()) return;
19315 // If there is a single break point clear it if it is the same.
19316 if (!break_point_info->break_point_objects()->IsFixedArray()) {
19317 if (break_point_info->break_point_objects() == *break_point_object) {
Steve Block44f0eee2011-05-26 01:26:41 +010019318 break_point_info->set_break_point_objects(
19319 isolate->heap()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +000019320 }
19321 return;
19322 }
19323 // If there are multiple break points shrink the array
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019324 DCHECK(break_point_info->break_point_objects()->IsFixedArray());
Steve Blocka7e24c12009-10-30 11:49:00 +000019325 Handle<FixedArray> old_array =
19326 Handle<FixedArray>(
19327 FixedArray::cast(break_point_info->break_point_objects()));
19328 Handle<FixedArray> new_array =
Steve Block44f0eee2011-05-26 01:26:41 +010019329 isolate->factory()->NewFixedArray(old_array->length() - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +000019330 int found_count = 0;
19331 for (int i = 0; i < old_array->length(); i++) {
19332 if (old_array->get(i) == *break_point_object) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019333 DCHECK(found_count == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +000019334 found_count++;
19335 } else {
19336 new_array->set(i - found_count, old_array->get(i));
19337 }
19338 }
19339 // If the break point was found in the list change it.
19340 if (found_count > 0) break_point_info->set_break_point_objects(*new_array);
19341}
19342
19343
19344// Add the specified break point object.
19345void BreakPointInfo::SetBreakPoint(Handle<BreakPointInfo> break_point_info,
19346 Handle<Object> break_point_object) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019347 Isolate* isolate = break_point_info->GetIsolate();
19348
Steve Blocka7e24c12009-10-30 11:49:00 +000019349 // If there was no break point objects before just set it.
19350 if (break_point_info->break_point_objects()->IsUndefined()) {
19351 break_point_info->set_break_point_objects(*break_point_object);
19352 return;
19353 }
19354 // If the break point object is the same as before just ignore.
19355 if (break_point_info->break_point_objects() == *break_point_object) return;
19356 // If there was one break point object before replace with array.
19357 if (!break_point_info->break_point_objects()->IsFixedArray()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019358 Handle<FixedArray> array = isolate->factory()->NewFixedArray(2);
Steve Blocka7e24c12009-10-30 11:49:00 +000019359 array->set(0, break_point_info->break_point_objects());
19360 array->set(1, *break_point_object);
19361 break_point_info->set_break_point_objects(*array);
19362 return;
19363 }
19364 // If there was more than one break point before extend array.
19365 Handle<FixedArray> old_array =
19366 Handle<FixedArray>(
19367 FixedArray::cast(break_point_info->break_point_objects()));
19368 Handle<FixedArray> new_array =
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019369 isolate->factory()->NewFixedArray(old_array->length() + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +000019370 for (int i = 0; i < old_array->length(); i++) {
19371 // If the break point was there before just ignore.
19372 if (old_array->get(i) == *break_point_object) return;
19373 new_array->set(i, old_array->get(i));
19374 }
19375 // Add the new break point.
19376 new_array->set(old_array->length(), *break_point_object);
19377 break_point_info->set_break_point_objects(*new_array);
19378}
19379
19380
19381bool BreakPointInfo::HasBreakPointObject(
19382 Handle<BreakPointInfo> break_point_info,
19383 Handle<Object> break_point_object) {
19384 // No break point.
19385 if (break_point_info->break_point_objects()->IsUndefined()) return false;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000019386 // Single break point.
Steve Blocka7e24c12009-10-30 11:49:00 +000019387 if (!break_point_info->break_point_objects()->IsFixedArray()) {
19388 return break_point_info->break_point_objects() == *break_point_object;
19389 }
19390 // Multiple break points.
19391 FixedArray* array = FixedArray::cast(break_point_info->break_point_objects());
19392 for (int i = 0; i < array->length(); i++) {
19393 if (array->get(i) == *break_point_object) {
19394 return true;
19395 }
19396 }
19397 return false;
19398}
19399
19400
19401// Get the number of break points.
19402int BreakPointInfo::GetBreakPointCount() {
19403 // No break point.
19404 if (break_point_objects()->IsUndefined()) return 0;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000019405 // Single break point.
Steve Blocka7e24c12009-10-30 11:49:00 +000019406 if (!break_point_objects()->IsFixedArray()) return 1;
19407 // Multiple break points.
19408 return FixedArray::cast(break_point_objects())->length();
19409}
Steve Blocka7e24c12009-10-30 11:49:00 +000019410
19411
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019412// static
19413MaybeHandle<JSDate> JSDate::New(Handle<JSFunction> constructor,
19414 Handle<JSReceiver> new_target, double tv) {
19415 Isolate* const isolate = constructor->GetIsolate();
19416 Handle<JSObject> result;
19417 ASSIGN_RETURN_ON_EXCEPTION(isolate, result,
19418 JSObject::New(constructor, new_target), JSDate);
19419 if (-DateCache::kMaxTimeInMs <= tv && tv <= DateCache::kMaxTimeInMs) {
19420 tv = DoubleToInteger(tv) + 0.0;
19421 } else {
19422 tv = std::numeric_limits<double>::quiet_NaN();
19423 }
19424 Handle<Object> value = isolate->factory()->NewNumber(tv);
19425 Handle<JSDate>::cast(result)->SetValue(*value, std::isnan(tv));
19426 return Handle<JSDate>::cast(result);
19427}
19428
19429
19430// static
19431double JSDate::CurrentTimeValue(Isolate* isolate) {
19432 if (FLAG_log_timer_events || FLAG_prof_cpp) LOG(isolate, CurrentTimeEvent());
19433
19434 // According to ECMA-262, section 15.9.1, page 117, the precision of
19435 // the number in a Date object representing a particular instant in
19436 // time is milliseconds. Therefore, we floor the result of getting
19437 // the OS time.
19438 return Floor(FLAG_verify_predictable
19439 ? isolate->heap()->MonotonicallyIncreasingTimeInMs()
19440 : base::OS::TimeCurrentMillis());
19441}
19442
19443
19444// static
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019445Object* JSDate::GetField(Object* object, Smi* index) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010019446 return JSDate::cast(object)->DoGetField(
19447 static_cast<FieldIndex>(index->value()));
19448}
19449
19450
19451Object* JSDate::DoGetField(FieldIndex index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019452 DCHECK(index != kDateValue);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010019453
19454 DateCache* date_cache = GetIsolate()->date_cache();
19455
19456 if (index < kFirstUncachedField) {
19457 Object* stamp = cache_stamp();
19458 if (stamp != date_cache->stamp() && stamp->IsSmi()) {
19459 // Since the stamp is not NaN, the value is also not NaN.
19460 int64_t local_time_ms =
19461 date_cache->ToLocal(static_cast<int64_t>(value()->Number()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019462 SetCachedFields(local_time_ms, date_cache);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010019463 }
19464 switch (index) {
19465 case kYear: return year();
19466 case kMonth: return month();
19467 case kDay: return day();
19468 case kWeekday: return weekday();
19469 case kHour: return hour();
19470 case kMinute: return min();
19471 case kSecond: return sec();
19472 default: UNREACHABLE();
19473 }
19474 }
19475
19476 if (index >= kFirstUTCField) {
19477 return GetUTCField(index, value()->Number(), date_cache);
19478 }
19479
19480 double time = value()->Number();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019481 if (std::isnan(time)) return GetIsolate()->heap()->nan_value();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010019482
19483 int64_t local_time_ms = date_cache->ToLocal(static_cast<int64_t>(time));
19484 int days = DateCache::DaysFromTime(local_time_ms);
19485
19486 if (index == kDays) return Smi::FromInt(days);
19487
19488 int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days);
19489 if (index == kMillisecond) return Smi::FromInt(time_in_day_ms % 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019490 DCHECK(index == kTimeInDay);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010019491 return Smi::FromInt(time_in_day_ms);
19492}
19493
19494
19495Object* JSDate::GetUTCField(FieldIndex index,
19496 double value,
19497 DateCache* date_cache) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019498 DCHECK(index >= kFirstUTCField);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010019499
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019500 if (std::isnan(value)) return GetIsolate()->heap()->nan_value();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010019501
19502 int64_t time_ms = static_cast<int64_t>(value);
19503
19504 if (index == kTimezoneOffset) {
19505 return Smi::FromInt(date_cache->TimezoneOffset(time_ms));
19506 }
19507
19508 int days = DateCache::DaysFromTime(time_ms);
19509
19510 if (index == kWeekdayUTC) return Smi::FromInt(date_cache->Weekday(days));
19511
19512 if (index <= kDayUTC) {
19513 int year, month, day;
19514 date_cache->YearMonthDayFromDays(days, &year, &month, &day);
19515 if (index == kYearUTC) return Smi::FromInt(year);
19516 if (index == kMonthUTC) return Smi::FromInt(month);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019517 DCHECK(index == kDayUTC);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010019518 return Smi::FromInt(day);
19519 }
19520
19521 int time_in_day_ms = DateCache::TimeInDay(time_ms, days);
19522 switch (index) {
19523 case kHourUTC: return Smi::FromInt(time_in_day_ms / (60 * 60 * 1000));
19524 case kMinuteUTC: return Smi::FromInt((time_in_day_ms / (60 * 1000)) % 60);
19525 case kSecondUTC: return Smi::FromInt((time_in_day_ms / 1000) % 60);
19526 case kMillisecondUTC: return Smi::FromInt(time_in_day_ms % 1000);
19527 case kDaysUTC: return Smi::FromInt(days);
19528 case kTimeInDayUTC: return Smi::FromInt(time_in_day_ms);
19529 default: UNREACHABLE();
19530 }
19531
19532 UNREACHABLE();
19533 return NULL;
19534}
19535
19536
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019537// static
19538Handle<Object> JSDate::SetValue(Handle<JSDate> date, double v) {
19539 Isolate* const isolate = date->GetIsolate();
19540 Handle<Object> value = isolate->factory()->NewNumber(v);
19541 bool value_is_nan = std::isnan(v);
19542 date->SetValue(*value, value_is_nan);
19543 return value;
19544}
19545
19546
Ben Murdoch3ef787d2012-04-12 10:51:47 +010019547void JSDate::SetValue(Object* value, bool is_value_nan) {
19548 set_value(value);
19549 if (is_value_nan) {
19550 HeapNumber* nan = GetIsolate()->heap()->nan_value();
19551 set_cache_stamp(nan, SKIP_WRITE_BARRIER);
19552 set_year(nan, SKIP_WRITE_BARRIER);
19553 set_month(nan, SKIP_WRITE_BARRIER);
19554 set_day(nan, SKIP_WRITE_BARRIER);
19555 set_hour(nan, SKIP_WRITE_BARRIER);
19556 set_min(nan, SKIP_WRITE_BARRIER);
19557 set_sec(nan, SKIP_WRITE_BARRIER);
19558 set_weekday(nan, SKIP_WRITE_BARRIER);
19559 } else {
19560 set_cache_stamp(Smi::FromInt(DateCache::kInvalidStamp), SKIP_WRITE_BARRIER);
19561 }
19562}
19563
19564
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019565// static
19566MaybeHandle<Object> JSDate::ToPrimitive(Handle<JSReceiver> receiver,
19567 Handle<Object> hint) {
19568 Isolate* const isolate = receiver->GetIsolate();
19569 if (hint->IsString()) {
19570 Handle<String> hint_string = Handle<String>::cast(hint);
19571 if (hint_string->Equals(isolate->heap()->number_string())) {
19572 return JSReceiver::OrdinaryToPrimitive(receiver,
19573 OrdinaryToPrimitiveHint::kNumber);
19574 }
19575 if (hint_string->Equals(isolate->heap()->default_string()) ||
19576 hint_string->Equals(isolate->heap()->string_string())) {
19577 return JSReceiver::OrdinaryToPrimitive(receiver,
19578 OrdinaryToPrimitiveHint::kString);
19579 }
19580 }
19581 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kInvalidHint, hint),
19582 Object);
19583}
19584
19585
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019586void JSDate::SetCachedFields(int64_t local_time_ms, DateCache* date_cache) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010019587 int days = DateCache::DaysFromTime(local_time_ms);
19588 int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days);
19589 int year, month, day;
19590 date_cache->YearMonthDayFromDays(days, &year, &month, &day);
19591 int weekday = date_cache->Weekday(days);
19592 int hour = time_in_day_ms / (60 * 60 * 1000);
19593 int min = (time_in_day_ms / (60 * 1000)) % 60;
19594 int sec = (time_in_day_ms / 1000) % 60;
19595 set_cache_stamp(date_cache->stamp());
19596 set_year(Smi::FromInt(year), SKIP_WRITE_BARRIER);
19597 set_month(Smi::FromInt(month), SKIP_WRITE_BARRIER);
19598 set_day(Smi::FromInt(day), SKIP_WRITE_BARRIER);
19599 set_weekday(Smi::FromInt(weekday), SKIP_WRITE_BARRIER);
19600 set_hour(Smi::FromInt(hour), SKIP_WRITE_BARRIER);
19601 set_min(Smi::FromInt(min), SKIP_WRITE_BARRIER);
19602 set_sec(Smi::FromInt(sec), SKIP_WRITE_BARRIER);
19603}
19604
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019605
19606void JSArrayBuffer::Neuter() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040019607 CHECK(is_neuterable());
19608 CHECK(is_external());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019609 set_backing_store(NULL);
19610 set_byte_length(Smi::FromInt(0));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019611 set_was_neutered(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019612}
19613
19614
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019615void JSArrayBuffer::Setup(Handle<JSArrayBuffer> array_buffer, Isolate* isolate,
19616 bool is_external, void* data, size_t allocated_length,
19617 SharedFlag shared) {
19618 DCHECK(array_buffer->GetInternalFieldCount() ==
19619 v8::ArrayBuffer::kInternalFieldCount);
19620 for (int i = 0; i < v8::ArrayBuffer::kInternalFieldCount; i++) {
19621 array_buffer->SetInternalField(i, Smi::FromInt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019622 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019623 array_buffer->set_bit_field(0);
19624 array_buffer->set_is_external(is_external);
19625 array_buffer->set_is_neuterable(shared == SharedFlag::kNotShared);
19626 array_buffer->set_is_shared(shared == SharedFlag::kShared);
19627
19628 Handle<Object> byte_length =
19629 isolate->factory()->NewNumberFromSize(allocated_length);
19630 CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber());
19631 array_buffer->set_byte_length(*byte_length);
19632 // Initialize backing store at last to avoid handling of |JSArrayBuffers| that
19633 // are currently being constructed in the |ArrayBufferTracker|. The
19634 // registration method below handles the case of registering a buffer that has
19635 // already been promoted.
19636 array_buffer->set_backing_store(data);
19637
19638 if (data && !is_external) {
19639 isolate->heap()->RegisterNewArrayBuffer(*array_buffer);
19640 }
19641}
19642
19643
19644bool JSArrayBuffer::SetupAllocatingData(Handle<JSArrayBuffer> array_buffer,
19645 Isolate* isolate,
19646 size_t allocated_length,
19647 bool initialize, SharedFlag shared) {
19648 void* data;
19649 CHECK(isolate->array_buffer_allocator() != NULL);
19650 // Prevent creating array buffers when serializing.
19651 DCHECK(!isolate->serializer_enabled());
19652 if (allocated_length != 0) {
19653 if (initialize) {
19654 data = isolate->array_buffer_allocator()->Allocate(allocated_length);
19655 } else {
19656 data = isolate->array_buffer_allocator()->AllocateUninitialized(
19657 allocated_length);
19658 }
19659 if (data == NULL) return false;
19660 } else {
19661 data = NULL;
19662 }
19663
19664 JSArrayBuffer::Setup(array_buffer, isolate, false, data, allocated_length,
19665 shared);
19666 return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019667}
19668
19669
19670Handle<JSArrayBuffer> JSTypedArray::MaterializeArrayBuffer(
19671 Handle<JSTypedArray> typed_array) {
19672
19673 Handle<Map> map(typed_array->map());
19674 Isolate* isolate = typed_array->GetIsolate();
19675
19676 DCHECK(IsFixedTypedArrayElementsKind(map->elements_kind()));
19677
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019678 Handle<FixedTypedArrayBase> fixed_typed_array(
19679 FixedTypedArrayBase::cast(typed_array->elements()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019680
19681 Handle<JSArrayBuffer> buffer(JSArrayBuffer::cast(typed_array->buffer()),
19682 isolate);
19683 void* backing_store =
19684 isolate->array_buffer_allocator()->AllocateUninitialized(
19685 fixed_typed_array->DataSize());
19686 buffer->set_is_external(false);
19687 DCHECK(buffer->byte_length()->IsSmi() ||
19688 buffer->byte_length()->IsHeapNumber());
19689 DCHECK(NumberToInt32(buffer->byte_length()) == fixed_typed_array->DataSize());
19690 // Initialize backing store at last to avoid handling of |JSArrayBuffers| that
19691 // are currently being constructed in the |ArrayBufferTracker|. The
19692 // registration method below handles the case of registering a buffer that has
19693 // already been promoted.
19694 buffer->set_backing_store(backing_store);
19695 isolate->heap()->RegisterNewArrayBuffer(*buffer);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019696 memcpy(buffer->backing_store(),
19697 fixed_typed_array->DataPtr(),
19698 fixed_typed_array->DataSize());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019699 Handle<FixedTypedArrayBase> new_elements =
19700 isolate->factory()->NewFixedTypedArrayWithExternalPointer(
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019701 fixed_typed_array->length(), typed_array->type(),
19702 static_cast<uint8_t*>(buffer->backing_store()));
19703
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019704 typed_array->set_elements(*new_elements);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019705
19706 return buffer;
19707}
19708
19709
19710Handle<JSArrayBuffer> JSTypedArray::GetBuffer() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019711 Handle<JSArrayBuffer> array_buffer(JSArrayBuffer::cast(buffer()),
19712 GetIsolate());
19713 if (array_buffer->was_neutered() ||
19714 array_buffer->backing_store() != nullptr) {
19715 return array_buffer;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019716 }
19717 Handle<JSTypedArray> self(this);
19718 return MaterializeArrayBuffer(self);
19719}
19720
19721
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019722Handle<PropertyCell> PropertyCell::InvalidateEntry(
19723 Handle<GlobalDictionary> dictionary, int entry) {
19724 Isolate* isolate = dictionary->GetIsolate();
19725 // Swap with a copy.
19726 DCHECK(dictionary->ValueAt(entry)->IsPropertyCell());
19727 Handle<PropertyCell> cell(PropertyCell::cast(dictionary->ValueAt(entry)));
19728 auto new_cell = isolate->factory()->NewPropertyCell();
19729 new_cell->set_value(cell->value());
19730 dictionary->ValueAtPut(entry, *new_cell);
19731 bool is_the_hole = cell->value()->IsTheHole();
19732 // Cell is officially mutable henceforth.
19733 PropertyDetails details = cell->property_details();
19734 details = details.set_cell_type(is_the_hole ? PropertyCellType::kInvalidated
19735 : PropertyCellType::kMutable);
19736 new_cell->set_property_details(details);
19737 // Old cell is ready for invalidation.
19738 if (is_the_hole) {
19739 cell->set_value(isolate->heap()->undefined_value());
19740 } else {
19741 cell->set_value(isolate->heap()->the_hole_value());
19742 }
19743 details = details.set_cell_type(PropertyCellType::kInvalidated);
19744 cell->set_property_details(details);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019745 cell->dependent_code()->DeoptimizeDependentCodeGroup(
19746 isolate, DependentCode::kPropertyCellChangedGroup);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019747 return new_cell;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019748}
19749
19750
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019751PropertyCellConstantType PropertyCell::GetConstantType() {
19752 if (value()->IsSmi()) return PropertyCellConstantType::kSmi;
19753 return PropertyCellConstantType::kStableMap;
19754}
19755
19756
19757static bool RemainsConstantType(Handle<PropertyCell> cell,
19758 Handle<Object> value) {
19759 // TODO(dcarney): double->smi and smi->double transition from kConstant
19760 if (cell->value()->IsSmi() && value->IsSmi()) {
19761 return true;
19762 } else if (cell->value()->IsHeapObject() && value->IsHeapObject()) {
19763 return HeapObject::cast(cell->value())->map() ==
19764 HeapObject::cast(*value)->map() &&
19765 HeapObject::cast(*value)->map()->is_stable();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040019766 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019767 return false;
19768}
19769
19770
19771PropertyCellType PropertyCell::UpdatedType(Handle<PropertyCell> cell,
19772 Handle<Object> value,
19773 PropertyDetails details) {
19774 PropertyCellType type = details.cell_type();
19775 DCHECK(!value->IsTheHole());
19776 if (cell->value()->IsTheHole()) {
19777 switch (type) {
19778 // Only allow a cell to transition once into constant state.
19779 case PropertyCellType::kUninitialized:
19780 if (value->IsUndefined()) return PropertyCellType::kUndefined;
19781 return PropertyCellType::kConstant;
19782 case PropertyCellType::kInvalidated:
19783 return PropertyCellType::kMutable;
19784 default:
19785 UNREACHABLE();
19786 return PropertyCellType::kMutable;
19787 }
19788 }
19789 switch (type) {
19790 case PropertyCellType::kUndefined:
19791 return PropertyCellType::kConstant;
19792 case PropertyCellType::kConstant:
19793 if (*value == cell->value()) return PropertyCellType::kConstant;
19794 // Fall through.
19795 case PropertyCellType::kConstantType:
19796 if (RemainsConstantType(cell, value)) {
19797 return PropertyCellType::kConstantType;
19798 }
19799 // Fall through.
19800 case PropertyCellType::kMutable:
19801 return PropertyCellType::kMutable;
19802 }
19803 UNREACHABLE();
19804 return PropertyCellType::kMutable;
19805}
19806
19807
19808void PropertyCell::UpdateCell(Handle<GlobalDictionary> dictionary, int entry,
19809 Handle<Object> value, PropertyDetails details) {
19810 DCHECK(!value->IsTheHole());
19811 DCHECK(dictionary->ValueAt(entry)->IsPropertyCell());
19812 Handle<PropertyCell> cell(PropertyCell::cast(dictionary->ValueAt(entry)));
19813 const PropertyDetails original_details = cell->property_details();
19814 // Data accesses could be cached in ics or optimized code.
19815 bool invalidate =
19816 original_details.kind() == kData && details.kind() == kAccessor;
19817 int index = original_details.dictionary_index();
19818 PropertyCellType old_type = original_details.cell_type();
19819 // Preserve the enumeration index unless the property was deleted or never
19820 // initialized.
19821 if (cell->value()->IsTheHole()) {
19822 index = dictionary->NextEnumerationIndex();
19823 dictionary->SetNextEnumerationIndex(index + 1);
19824 // Negative lookup cells must be invalidated.
19825 invalidate = true;
19826 }
19827 DCHECK(index > 0);
19828 details = details.set_index(index);
19829
19830 PropertyCellType new_type = UpdatedType(cell, value, original_details);
19831 if (invalidate) cell = PropertyCell::InvalidateEntry(dictionary, entry);
19832
19833 // Install new property details and cell value.
19834 details = details.set_cell_type(new_type);
19835 cell->set_property_details(details);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019836 cell->set_value(*value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019837
19838 // Deopt when transitioning from a constant type.
19839 if (!invalidate && (old_type != new_type ||
19840 original_details.IsReadOnly() != details.IsReadOnly())) {
19841 Isolate* isolate = dictionary->GetIsolate();
19842 cell->dependent_code()->DeoptimizeDependentCodeGroup(
19843 isolate, DependentCode::kPropertyCellChangedGroup);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019844 }
19845}
19846
19847
19848// static
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019849void PropertyCell::SetValueWithInvalidation(Handle<PropertyCell> cell,
19850 Handle<Object> new_value) {
19851 if (cell->value() != *new_value) {
19852 cell->set_value(*new_value);
19853 Isolate* isolate = cell->GetIsolate();
19854 cell->dependent_code()->DeoptimizeDependentCodeGroup(
19855 isolate, DependentCode::kPropertyCellChangedGroup);
19856 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019857}
19858
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019859} // namespace internal
19860} // namespace v8