Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1 | // Copyright 2015 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5 | #include "src/objects.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7 | #include <cmath> |
| 8 | #include <iomanip> |
| 9 | #include <sstream> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11 | #include "src/accessors.h" |
| 12 | #include "src/allocation-site-scopes.h" |
| 13 | #include "src/api.h" |
| 14 | #include "src/arguments.h" |
| 15 | #include "src/base/bits.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16 | #include "src/base/utils/random-number-generator.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17 | #include "src/bootstrapper.h" |
| 18 | #include "src/code-stubs.h" |
| 19 | #include "src/codegen.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 20 | #include "src/compilation-dependencies.h" |
| 21 | #include "src/compiler.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 22 | #include "src/date.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 23 | #include "src/debug/debug.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 24 | #include "src/deoptimizer.h" |
| 25 | #include "src/elements.h" |
| 26 | #include "src/execution.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 27 | #include "src/field-index.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 28 | #include "src/field-index-inl.h" |
| 29 | #include "src/full-codegen/full-codegen.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 30 | #include "src/ic/ic.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 31 | #include "src/identity-map.h" |
| 32 | #include "src/interpreter/bytecodes.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 33 | #include "src/isolate-inl.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 34 | #include "src/key-accumulator.h" |
| 35 | #include "src/list.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 36 | #include "src/log.h" |
| 37 | #include "src/lookup.h" |
| 38 | #include "src/macro-assembler.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 39 | #include "src/messages.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 40 | #include "src/objects-inl.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 41 | #include "src/objects-body-descriptors-inl.h" |
| 42 | #include "src/profiler/cpu-profiler.h" |
| 43 | #include "src/property-descriptor.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 44 | #include "src/prototype.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 45 | #include "src/regexp/jsregexp.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 46 | #include "src/safepoint-table.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 47 | #include "src/string-builder.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 48 | #include "src/string-search.h" |
| 49 | #include "src/string-stream.h" |
| 50 | #include "src/utils.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 51 | #include "src/zone.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 52 | |
| 53 | #ifdef ENABLE_DISASSEMBLER |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 54 | #include "src/disasm.h" |
| 55 | #include "src/disassembler.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 56 | #endif |
| 57 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 58 | namespace v8 { |
| 59 | namespace internal { |
| 60 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 61 | std::ostream& operator<<(std::ostream& os, InstanceType instance_type) { |
| 62 | switch (instance_type) { |
| 63 | #define WRITE_TYPE(TYPE) \ |
| 64 | case TYPE: \ |
| 65 | return os << #TYPE; |
| 66 | INSTANCE_TYPE_LIST(WRITE_TYPE) |
| 67 | #undef WRITE_TYPE |
| 68 | } |
| 69 | UNREACHABLE(); |
| 70 | return os << "UNKNOWN"; // Keep the compiler happy. |
| 71 | } |
| 72 | |
| 73 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 74 | Handle<HeapType> Object::OptimalType(Isolate* isolate, |
| 75 | Representation representation) { |
| 76 | if (representation.IsNone()) return HeapType::None(isolate); |
| 77 | if (FLAG_track_field_types) { |
| 78 | if (representation.IsHeapObject() && IsHeapObject()) { |
| 79 | // We can track only JavaScript objects with stable maps. |
| 80 | Handle<Map> map(HeapObject::cast(this)->map(), isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 81 | if (map->is_stable() && map->IsJSReceiverMap()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 82 | return HeapType::Class(map, isolate); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | return HeapType::Any(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 89 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 90 | MaybeHandle<JSReceiver> Object::ToObject(Isolate* isolate, |
| 91 | Handle<Object> object, |
| 92 | Handle<Context> native_context) { |
| 93 | if (object->IsJSReceiver()) return Handle<JSReceiver>::cast(object); |
| 94 | Handle<JSFunction> constructor; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 95 | if (object->IsSmi()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 96 | constructor = handle(native_context->number_function(), isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 97 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 98 | int constructor_function_index = |
| 99 | Handle<HeapObject>::cast(object)->map()->GetConstructorFunctionIndex(); |
| 100 | if (constructor_function_index == Map::kNoConstructorFunctionIndex) { |
| 101 | return MaybeHandle<JSReceiver>(); |
| 102 | } |
| 103 | constructor = handle( |
| 104 | JSFunction::cast(native_context->get(constructor_function_index)), |
| 105 | isolate); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 106 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 107 | Handle<JSObject> result = isolate->factory()->NewJSObject(constructor); |
| 108 | Handle<JSValue>::cast(result)->set_value(*object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 109 | return result; |
| 110 | } |
| 111 | |
| 112 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 113 | // static |
| 114 | MaybeHandle<Name> Object::ToName(Isolate* isolate, Handle<Object> input) { |
| 115 | ASSIGN_RETURN_ON_EXCEPTION( |
| 116 | isolate, input, Object::ToPrimitive(input, ToPrimitiveHint::kString), |
| 117 | Name); |
| 118 | if (input->IsName()) return Handle<Name>::cast(input); |
| 119 | return ToString(isolate, input); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | // static |
| 124 | MaybeHandle<Object> Object::ToNumber(Handle<Object> input) { |
| 125 | while (true) { |
| 126 | if (input->IsNumber()) { |
| 127 | return input; |
| 128 | } |
| 129 | if (input->IsString()) { |
| 130 | return String::ToNumber(Handle<String>::cast(input)); |
| 131 | } |
| 132 | if (input->IsOddball()) { |
| 133 | return Oddball::ToNumber(Handle<Oddball>::cast(input)); |
| 134 | } |
| 135 | Isolate* const isolate = Handle<HeapObject>::cast(input)->GetIsolate(); |
| 136 | if (input->IsSymbol()) { |
| 137 | THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSymbolToNumber), |
| 138 | Object); |
| 139 | } |
| 140 | if (input->IsSimd128Value()) { |
| 141 | THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSimdToNumber), |
| 142 | Object); |
| 143 | } |
| 144 | ASSIGN_RETURN_ON_EXCEPTION( |
| 145 | isolate, input, JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(input), |
| 146 | ToPrimitiveHint::kNumber), |
| 147 | Object); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | |
| 152 | // static |
| 153 | MaybeHandle<Object> Object::ToInteger(Isolate* isolate, Handle<Object> input) { |
| 154 | ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object); |
| 155 | return isolate->factory()->NewNumber(DoubleToInteger(input->Number())); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | // static |
| 160 | MaybeHandle<Object> Object::ToInt32(Isolate* isolate, Handle<Object> input) { |
| 161 | ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object); |
| 162 | return isolate->factory()->NewNumberFromInt(DoubleToInt32(input->Number())); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | // static |
| 167 | MaybeHandle<Object> Object::ToUint32(Isolate* isolate, Handle<Object> input) { |
| 168 | ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object); |
| 169 | return isolate->factory()->NewNumberFromUint(DoubleToUint32(input->Number())); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | // static |
| 174 | MaybeHandle<String> Object::ToString(Isolate* isolate, Handle<Object> input) { |
| 175 | while (true) { |
| 176 | if (input->IsString()) { |
| 177 | return Handle<String>::cast(input); |
| 178 | } |
| 179 | if (input->IsOddball()) { |
| 180 | return handle(Handle<Oddball>::cast(input)->to_string(), isolate); |
| 181 | } |
| 182 | if (input->IsNumber()) { |
| 183 | return isolate->factory()->NumberToString(input); |
| 184 | } |
| 185 | if (input->IsSymbol()) { |
| 186 | THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSymbolToString), |
| 187 | String); |
| 188 | } |
| 189 | if (input->IsSimd128Value()) { |
| 190 | return Simd128Value::ToString(Handle<Simd128Value>::cast(input)); |
| 191 | } |
| 192 | ASSIGN_RETURN_ON_EXCEPTION( |
| 193 | isolate, input, JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(input), |
| 194 | ToPrimitiveHint::kString), |
| 195 | String); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | |
| 200 | // static |
| 201 | MaybeHandle<Object> Object::ToLength(Isolate* isolate, Handle<Object> input) { |
| 202 | ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object); |
| 203 | double len = DoubleToInteger(input->Number()); |
| 204 | if (len <= 0.0) { |
| 205 | len = 0.0; |
| 206 | } else if (len >= kMaxSafeInteger) { |
| 207 | len = kMaxSafeInteger; |
| 208 | } |
| 209 | return isolate->factory()->NewNumber(len); |
| 210 | } |
| 211 | |
| 212 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 213 | bool Object::BooleanValue() { |
| 214 | if (IsBoolean()) return IsTrue(); |
| 215 | if (IsSmi()) return Smi::cast(this)->value() != 0; |
| 216 | if (IsUndefined() || IsNull()) return false; |
| 217 | if (IsUndetectableObject()) return false; // Undetectable object is false. |
| 218 | if (IsString()) return String::cast(this)->length() != 0; |
| 219 | if (IsHeapNumber()) return HeapNumber::cast(this)->HeapNumberBooleanValue(); |
| 220 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 224 | namespace { |
| 225 | |
| 226 | // TODO(bmeurer): Maybe we should introduce a marker interface Number, |
| 227 | // where we put all these methods at some point? |
| 228 | ComparisonResult NumberCompare(double x, double y) { |
| 229 | if (std::isnan(x) || std::isnan(y)) { |
| 230 | return ComparisonResult::kUndefined; |
| 231 | } else if (x < y) { |
| 232 | return ComparisonResult::kLessThan; |
| 233 | } else if (x > y) { |
| 234 | return ComparisonResult::kGreaterThan; |
| 235 | } else { |
| 236 | return ComparisonResult::kEqual; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 237 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 241 | bool NumberEquals(double x, double y) { |
| 242 | // Must check explicitly for NaN's on Windows, but -0 works fine. |
| 243 | if (std::isnan(x)) return false; |
| 244 | if (std::isnan(y)) return false; |
| 245 | return x == y; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | bool NumberEquals(const Object* x, const Object* y) { |
| 250 | return NumberEquals(x->Number(), y->Number()); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | bool NumberEquals(Handle<Object> x, Handle<Object> y) { |
| 255 | return NumberEquals(*x, *y); |
| 256 | } |
| 257 | |
| 258 | } // namespace |
| 259 | |
| 260 | |
| 261 | // static |
| 262 | Maybe<ComparisonResult> Object::Compare(Handle<Object> x, Handle<Object> y, |
| 263 | Strength strength) { |
| 264 | if (!is_strong(strength)) { |
| 265 | // ES6 section 7.2.11 Abstract Relational Comparison step 3 and 4. |
| 266 | if (!Object::ToPrimitive(x, ToPrimitiveHint::kNumber).ToHandle(&x) || |
| 267 | !Object::ToPrimitive(y, ToPrimitiveHint::kNumber).ToHandle(&y)) { |
| 268 | return Nothing<ComparisonResult>(); |
| 269 | } |
| 270 | } |
| 271 | if (x->IsString() && y->IsString()) { |
| 272 | // ES6 section 7.2.11 Abstract Relational Comparison step 5. |
| 273 | return Just( |
| 274 | String::Compare(Handle<String>::cast(x), Handle<String>::cast(y))); |
| 275 | } |
| 276 | // ES6 section 7.2.11 Abstract Relational Comparison step 6. |
| 277 | if (!is_strong(strength)) { |
| 278 | if (!Object::ToNumber(x).ToHandle(&x) || |
| 279 | !Object::ToNumber(y).ToHandle(&y)) { |
| 280 | return Nothing<ComparisonResult>(); |
| 281 | } |
| 282 | } else { |
| 283 | if (!x->IsNumber()) { |
| 284 | Isolate* const isolate = Handle<HeapObject>::cast(x)->GetIsolate(); |
| 285 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 286 | MessageTemplate::kStrongImplicitConversion)); |
| 287 | return Nothing<ComparisonResult>(); |
| 288 | } else if (!y->IsNumber()) { |
| 289 | Isolate* const isolate = Handle<HeapObject>::cast(y)->GetIsolate(); |
| 290 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 291 | MessageTemplate::kStrongImplicitConversion)); |
| 292 | return Nothing<ComparisonResult>(); |
| 293 | } |
| 294 | } |
| 295 | return Just(NumberCompare(x->Number(), y->Number())); |
| 296 | } |
| 297 | |
| 298 | |
| 299 | // static |
| 300 | Maybe<bool> Object::Equals(Handle<Object> x, Handle<Object> y) { |
| 301 | while (true) { |
| 302 | if (x->IsNumber()) { |
| 303 | if (y->IsNumber()) { |
| 304 | return Just(NumberEquals(x, y)); |
| 305 | } else if (y->IsBoolean()) { |
| 306 | return Just(NumberEquals(*x, Handle<Oddball>::cast(y)->to_number())); |
| 307 | } else if (y->IsString()) { |
| 308 | return Just(NumberEquals(x, String::ToNumber(Handle<String>::cast(y)))); |
| 309 | } else if (y->IsJSReceiver() && !y->IsUndetectableObject()) { |
| 310 | if (!JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(y)) |
| 311 | .ToHandle(&y)) { |
| 312 | return Nothing<bool>(); |
| 313 | } |
| 314 | } else { |
| 315 | return Just(false); |
| 316 | } |
| 317 | } else if (x->IsString()) { |
| 318 | if (y->IsString()) { |
| 319 | return Just( |
| 320 | String::Equals(Handle<String>::cast(x), Handle<String>::cast(y))); |
| 321 | } else if (y->IsNumber()) { |
| 322 | x = String::ToNumber(Handle<String>::cast(x)); |
| 323 | return Just(NumberEquals(x, y)); |
| 324 | } else if (y->IsBoolean()) { |
| 325 | x = String::ToNumber(Handle<String>::cast(x)); |
| 326 | return Just(NumberEquals(*x, Handle<Oddball>::cast(y)->to_number())); |
| 327 | } else if (y->IsJSReceiver() && !y->IsUndetectableObject()) { |
| 328 | if (!JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(y)) |
| 329 | .ToHandle(&y)) { |
| 330 | return Nothing<bool>(); |
| 331 | } |
| 332 | } else { |
| 333 | return Just(false); |
| 334 | } |
| 335 | } else if (x->IsBoolean()) { |
| 336 | if (y->IsOddball()) { |
| 337 | return Just(x.is_identical_to(y)); |
| 338 | } else if (y->IsNumber()) { |
| 339 | return Just(NumberEquals(Handle<Oddball>::cast(x)->to_number(), *y)); |
| 340 | } else if (y->IsString()) { |
| 341 | y = String::ToNumber(Handle<String>::cast(y)); |
| 342 | return Just(NumberEquals(Handle<Oddball>::cast(x)->to_number(), *y)); |
| 343 | } else if (y->IsJSReceiver() && !y->IsUndetectableObject()) { |
| 344 | if (!JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(y)) |
| 345 | .ToHandle(&y)) { |
| 346 | return Nothing<bool>(); |
| 347 | } |
| 348 | x = Oddball::ToNumber(Handle<Oddball>::cast(x)); |
| 349 | } else { |
| 350 | return Just(false); |
| 351 | } |
| 352 | } else if (x->IsSymbol()) { |
| 353 | if (y->IsSymbol()) { |
| 354 | return Just(x.is_identical_to(y)); |
| 355 | } else if (y->IsJSReceiver() && !y->IsUndetectableObject()) { |
| 356 | if (!JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(y)) |
| 357 | .ToHandle(&y)) { |
| 358 | return Nothing<bool>(); |
| 359 | } |
| 360 | } else { |
| 361 | return Just(false); |
| 362 | } |
| 363 | } else if (x->IsSimd128Value()) { |
| 364 | if (y->IsSimd128Value()) { |
| 365 | return Just(Simd128Value::Equals(Handle<Simd128Value>::cast(x), |
| 366 | Handle<Simd128Value>::cast(y))); |
| 367 | } else if (y->IsJSReceiver() && !y->IsUndetectableObject()) { |
| 368 | if (!JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(y)) |
| 369 | .ToHandle(&y)) { |
| 370 | return Nothing<bool>(); |
| 371 | } |
| 372 | } else { |
| 373 | return Just(false); |
| 374 | } |
| 375 | } else if (x->IsJSReceiver() && !x->IsUndetectableObject()) { |
| 376 | if (y->IsJSReceiver()) { |
| 377 | return Just(x.is_identical_to(y)); |
| 378 | } else if (y->IsNull() || y->IsUndefined()) { |
| 379 | return Just(false); |
| 380 | } else if (y->IsBoolean()) { |
| 381 | y = Oddball::ToNumber(Handle<Oddball>::cast(y)); |
| 382 | } else if (!JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(x)) |
| 383 | .ToHandle(&x)) { |
| 384 | return Nothing<bool>(); |
| 385 | } |
| 386 | } else { |
| 387 | return Just( |
| 388 | (x->IsNull() || x->IsUndefined() || x->IsUndetectableObject()) && |
| 389 | (y->IsNull() || y->IsUndefined() || y->IsUndetectableObject())); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | |
| 395 | bool Object::StrictEquals(Object* that) { |
| 396 | if (this->IsNumber()) { |
| 397 | if (!that->IsNumber()) return false; |
| 398 | return NumberEquals(this, that); |
| 399 | } else if (this->IsString()) { |
| 400 | if (!that->IsString()) return false; |
| 401 | return String::cast(this)->Equals(String::cast(that)); |
| 402 | } else if (this->IsSimd128Value()) { |
| 403 | if (!that->IsSimd128Value()) return false; |
| 404 | return Simd128Value::cast(this)->Equals(Simd128Value::cast(that)); |
| 405 | } |
| 406 | return this == that; |
| 407 | } |
| 408 | |
| 409 | |
| 410 | // static |
| 411 | Handle<String> Object::TypeOf(Isolate* isolate, Handle<Object> object) { |
| 412 | if (object->IsNumber()) return isolate->factory()->number_string(); |
| 413 | if (object->IsUndefined() || object->IsUndetectableObject()) { |
| 414 | return isolate->factory()->undefined_string(); |
| 415 | } |
| 416 | if (object->IsBoolean()) return isolate->factory()->boolean_string(); |
| 417 | if (object->IsString()) return isolate->factory()->string_string(); |
| 418 | if (object->IsSymbol()) return isolate->factory()->symbol_string(); |
| 419 | if (object->IsString()) return isolate->factory()->string_string(); |
| 420 | #define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \ |
| 421 | if (object->Is##Type()) return isolate->factory()->type##_string(); |
| 422 | SIMD128_TYPES(SIMD128_TYPE) |
| 423 | #undef SIMD128_TYPE |
| 424 | if (object->IsCallable()) return isolate->factory()->function_string(); |
| 425 | return isolate->factory()->object_string(); |
| 426 | } |
| 427 | |
| 428 | |
| 429 | // static |
| 430 | MaybeHandle<Object> Object::Multiply(Isolate* isolate, Handle<Object> lhs, |
| 431 | Handle<Object> rhs, Strength strength) { |
| 432 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
| 433 | if (is_strong(strength)) { |
| 434 | THROW_NEW_ERROR(isolate, |
| 435 | NewTypeError(MessageTemplate::kStrongImplicitConversion), |
| 436 | Object); |
| 437 | } |
| 438 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 439 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 440 | } |
| 441 | return isolate->factory()->NewNumber(lhs->Number() * rhs->Number()); |
| 442 | } |
| 443 | |
| 444 | |
| 445 | // static |
| 446 | MaybeHandle<Object> Object::Divide(Isolate* isolate, Handle<Object> lhs, |
| 447 | Handle<Object> rhs, Strength strength) { |
| 448 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
| 449 | if (is_strong(strength)) { |
| 450 | THROW_NEW_ERROR(isolate, |
| 451 | NewTypeError(MessageTemplate::kStrongImplicitConversion), |
| 452 | Object); |
| 453 | } |
| 454 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 455 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 456 | } |
| 457 | return isolate->factory()->NewNumber(lhs->Number() / rhs->Number()); |
| 458 | } |
| 459 | |
| 460 | |
| 461 | // static |
| 462 | MaybeHandle<Object> Object::Modulus(Isolate* isolate, Handle<Object> lhs, |
| 463 | Handle<Object> rhs, Strength strength) { |
| 464 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
| 465 | if (is_strong(strength)) { |
| 466 | THROW_NEW_ERROR(isolate, |
| 467 | NewTypeError(MessageTemplate::kStrongImplicitConversion), |
| 468 | Object); |
| 469 | } |
| 470 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 471 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 472 | } |
| 473 | return isolate->factory()->NewNumber(modulo(lhs->Number(), rhs->Number())); |
| 474 | } |
| 475 | |
| 476 | |
| 477 | // static |
| 478 | MaybeHandle<Object> Object::Add(Isolate* isolate, Handle<Object> lhs, |
| 479 | Handle<Object> rhs, Strength strength) { |
| 480 | if (lhs->IsNumber() && rhs->IsNumber()) { |
| 481 | return isolate->factory()->NewNumber(lhs->Number() + rhs->Number()); |
| 482 | } else if (lhs->IsString() && rhs->IsString()) { |
| 483 | return isolate->factory()->NewConsString(Handle<String>::cast(lhs), |
| 484 | Handle<String>::cast(rhs)); |
| 485 | } else if (is_strong(strength)) { |
| 486 | THROW_NEW_ERROR(isolate, |
| 487 | NewTypeError(MessageTemplate::kStrongImplicitConversion), |
| 488 | Object); |
| 489 | } |
| 490 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToPrimitive(lhs), Object); |
| 491 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToPrimitive(rhs), Object); |
| 492 | if (lhs->IsString() || rhs->IsString()) { |
| 493 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToString(isolate, rhs), |
| 494 | Object); |
| 495 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToString(isolate, lhs), |
| 496 | Object); |
| 497 | return isolate->factory()->NewConsString(Handle<String>::cast(lhs), |
| 498 | Handle<String>::cast(rhs)); |
| 499 | } |
| 500 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 501 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 502 | return isolate->factory()->NewNumber(lhs->Number() + rhs->Number()); |
| 503 | } |
| 504 | |
| 505 | |
| 506 | // static |
| 507 | MaybeHandle<Object> Object::Subtract(Isolate* isolate, Handle<Object> lhs, |
| 508 | Handle<Object> rhs, Strength strength) { |
| 509 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
| 510 | if (is_strong(strength)) { |
| 511 | THROW_NEW_ERROR(isolate, |
| 512 | NewTypeError(MessageTemplate::kStrongImplicitConversion), |
| 513 | Object); |
| 514 | } |
| 515 | 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()->NewNumber(lhs->Number() - rhs->Number()); |
| 519 | } |
| 520 | |
| 521 | |
| 522 | // static |
| 523 | MaybeHandle<Object> Object::ShiftLeft(Isolate* isolate, Handle<Object> lhs, |
| 524 | Handle<Object> rhs, Strength strength) { |
| 525 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
| 526 | if (is_strong(strength)) { |
| 527 | THROW_NEW_ERROR(isolate, |
| 528 | NewTypeError(MessageTemplate::kStrongImplicitConversion), |
| 529 | Object); |
| 530 | } |
| 531 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 532 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 533 | } |
| 534 | return isolate->factory()->NewNumberFromInt(NumberToInt32(*lhs) |
| 535 | << (NumberToUint32(*rhs) & 0x1F)); |
| 536 | } |
| 537 | |
| 538 | |
| 539 | // static |
| 540 | MaybeHandle<Object> Object::ShiftRight(Isolate* isolate, Handle<Object> lhs, |
| 541 | Handle<Object> rhs, Strength strength) { |
| 542 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
| 543 | if (is_strong(strength)) { |
| 544 | THROW_NEW_ERROR(isolate, |
| 545 | NewTypeError(MessageTemplate::kStrongImplicitConversion), |
| 546 | Object); |
| 547 | } |
| 548 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 549 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 550 | } |
| 551 | return isolate->factory()->NewNumberFromInt(NumberToInt32(*lhs) >> |
| 552 | (NumberToUint32(*rhs) & 0x1F)); |
| 553 | } |
| 554 | |
| 555 | |
| 556 | // static |
| 557 | MaybeHandle<Object> Object::ShiftRightLogical(Isolate* isolate, |
| 558 | Handle<Object> lhs, |
| 559 | Handle<Object> rhs, |
| 560 | Strength strength) { |
| 561 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
| 562 | if (is_strong(strength)) { |
| 563 | THROW_NEW_ERROR(isolate, |
| 564 | NewTypeError(MessageTemplate::kStrongImplicitConversion), |
| 565 | Object); |
| 566 | } |
| 567 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 568 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 569 | } |
| 570 | return isolate->factory()->NewNumberFromUint(NumberToUint32(*lhs) >> |
| 571 | (NumberToUint32(*rhs) & 0x1F)); |
| 572 | } |
| 573 | |
| 574 | |
| 575 | // static |
| 576 | MaybeHandle<Object> Object::BitwiseAnd(Isolate* isolate, Handle<Object> lhs, |
| 577 | Handle<Object> rhs, Strength strength) { |
| 578 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
| 579 | if (is_strong(strength)) { |
| 580 | THROW_NEW_ERROR(isolate, |
| 581 | NewTypeError(MessageTemplate::kStrongImplicitConversion), |
| 582 | Object); |
| 583 | } |
| 584 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 585 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 586 | } |
| 587 | return isolate->factory()->NewNumberFromInt(NumberToInt32(*lhs) & |
| 588 | NumberToInt32(*rhs)); |
| 589 | } |
| 590 | |
| 591 | |
| 592 | // static |
| 593 | MaybeHandle<Object> Object::BitwiseOr(Isolate* isolate, Handle<Object> lhs, |
| 594 | Handle<Object> rhs, Strength strength) { |
| 595 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
| 596 | if (is_strong(strength)) { |
| 597 | THROW_NEW_ERROR(isolate, |
| 598 | NewTypeError(MessageTemplate::kStrongImplicitConversion), |
| 599 | Object); |
| 600 | } |
| 601 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 602 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 603 | } |
| 604 | return isolate->factory()->NewNumberFromInt(NumberToInt32(*lhs) | |
| 605 | NumberToInt32(*rhs)); |
| 606 | } |
| 607 | |
| 608 | |
| 609 | // static |
| 610 | MaybeHandle<Object> Object::BitwiseXor(Isolate* isolate, Handle<Object> lhs, |
| 611 | Handle<Object> rhs, Strength strength) { |
| 612 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
| 613 | if (is_strong(strength)) { |
| 614 | THROW_NEW_ERROR(isolate, |
| 615 | NewTypeError(MessageTemplate::kStrongImplicitConversion), |
| 616 | Object); |
| 617 | } |
| 618 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 619 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 620 | } |
| 621 | return isolate->factory()->NewNumberFromInt(NumberToInt32(*lhs) ^ |
| 622 | NumberToInt32(*rhs)); |
| 623 | } |
| 624 | |
| 625 | |
| 626 | Maybe<bool> Object::IsArray(Handle<Object> object) { |
| 627 | if (object->IsJSArray()) return Just(true); |
| 628 | if (object->IsJSProxy()) { |
| 629 | Handle<JSProxy> proxy = Handle<JSProxy>::cast(object); |
| 630 | Isolate* isolate = proxy->GetIsolate(); |
| 631 | if (proxy->IsRevoked()) { |
| 632 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 633 | MessageTemplate::kProxyRevoked, |
| 634 | isolate->factory()->NewStringFromAsciiChecked("IsArray"))); |
| 635 | return Nothing<bool>(); |
| 636 | } |
| 637 | return Object::IsArray(handle(proxy->target(), isolate)); |
| 638 | } |
| 639 | return Just(false); |
| 640 | } |
| 641 | |
| 642 | |
| 643 | bool Object::IsPromise(Handle<Object> object) { |
| 644 | if (!object->IsJSObject()) return false; |
| 645 | auto js_object = Handle<JSObject>::cast(object); |
| 646 | // Promises can't have access checks. |
| 647 | if (js_object->map()->is_access_check_needed()) return false; |
| 648 | auto isolate = js_object->GetIsolate(); |
| 649 | // TODO(dcarney): this should just be read from the symbol registry so as not |
| 650 | // to be context dependent. |
| 651 | auto key = isolate->factory()->promise_status_symbol(); |
| 652 | // Shouldn't be possible to throw here. |
| 653 | return JSObject::HasRealNamedProperty(js_object, key).FromJust(); |
| 654 | } |
| 655 | |
| 656 | |
| 657 | // static |
| 658 | MaybeHandle<Object> Object::GetMethod(Handle<JSReceiver> receiver, |
| 659 | Handle<Name> name) { |
| 660 | Handle<Object> func; |
| 661 | Isolate* isolate = receiver->GetIsolate(); |
| 662 | ASSIGN_RETURN_ON_EXCEPTION(isolate, func, |
| 663 | JSReceiver::GetProperty(receiver, name), Object); |
| 664 | if (func->IsNull() || func->IsUndefined()) { |
| 665 | return isolate->factory()->undefined_value(); |
| 666 | } |
| 667 | if (!func->IsCallable()) { |
| 668 | THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kPropertyNotFunction, |
| 669 | func, name, receiver), |
| 670 | Object); |
| 671 | } |
| 672 | return func; |
| 673 | } |
| 674 | |
| 675 | |
| 676 | // static |
| 677 | MaybeHandle<FixedArray> Object::CreateListFromArrayLike( |
| 678 | Isolate* isolate, Handle<Object> object, ElementTypes element_types) { |
| 679 | // 1. ReturnIfAbrupt(object). |
| 680 | // 2. (default elementTypes -- not applicable.) |
| 681 | // 3. If Type(obj) is not Object, throw a TypeError exception. |
| 682 | if (!object->IsJSReceiver()) { |
| 683 | THROW_NEW_ERROR(isolate, |
| 684 | NewTypeError(MessageTemplate::kCalledOnNonObject, |
| 685 | isolate->factory()->NewStringFromAsciiChecked( |
| 686 | "CreateListFromArrayLike")), |
| 687 | FixedArray); |
| 688 | } |
| 689 | // 4. Let len be ? ToLength(? Get(obj, "length")). |
| 690 | Handle<Object> raw_length_obj; |
| 691 | ASSIGN_RETURN_ON_EXCEPTION( |
| 692 | isolate, raw_length_obj, |
| 693 | JSReceiver::GetProperty(object, isolate->factory()->length_string()), |
| 694 | FixedArray); |
| 695 | Handle<Object> raw_length_number; |
| 696 | ASSIGN_RETURN_ON_EXCEPTION(isolate, raw_length_number, |
| 697 | Object::ToLength(isolate, raw_length_obj), |
| 698 | FixedArray); |
| 699 | uint32_t len; |
| 700 | if (!raw_length_number->ToUint32(&len) || |
| 701 | len > static_cast<uint32_t>(FixedArray::kMaxLength)) { |
| 702 | THROW_NEW_ERROR(isolate, |
| 703 | NewRangeError(MessageTemplate::kInvalidArrayLength), |
| 704 | FixedArray); |
| 705 | } |
| 706 | // 5. Let list be an empty List. |
| 707 | Handle<FixedArray> list = isolate->factory()->NewFixedArray(len); |
| 708 | // 6. Let index be 0. |
| 709 | // 7. Repeat while index < len: |
| 710 | for (uint32_t index = 0; index < len; ++index) { |
| 711 | // 7a. Let indexName be ToString(index). |
| 712 | // 7b. Let next be ? Get(obj, indexName). |
| 713 | Handle<Object> next; |
| 714 | ASSIGN_RETURN_ON_EXCEPTION( |
| 715 | isolate, next, Object::GetElement(isolate, object, index), FixedArray); |
| 716 | switch (element_types) { |
| 717 | case ElementTypes::kAll: |
| 718 | // Nothing to do. |
| 719 | break; |
| 720 | case ElementTypes::kStringAndSymbol: { |
| 721 | // 7c. If Type(next) is not an element of elementTypes, throw a |
| 722 | // TypeError exception. |
| 723 | if (!next->IsName()) { |
| 724 | THROW_NEW_ERROR(isolate, |
| 725 | NewTypeError(MessageTemplate::kNotPropertyName, next), |
| 726 | FixedArray); |
| 727 | } |
| 728 | // 7d. Append next as the last element of list. |
| 729 | // Internalize on the fly so we can use pointer identity later. |
| 730 | next = isolate->factory()->InternalizeName(Handle<Name>::cast(next)); |
| 731 | break; |
| 732 | } |
| 733 | } |
| 734 | list->set(index, *next); |
| 735 | // 7e. Set index to index + 1. (See loop header.) |
| 736 | } |
| 737 | // 8. Return list. |
| 738 | return list; |
| 739 | } |
| 740 | |
| 741 | |
| 742 | // static |
| 743 | Maybe<bool> JSReceiver::HasProperty(LookupIterator* it) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 744 | for (; it->IsFound(); it->Next()) { |
| 745 | switch (it->state()) { |
| 746 | case LookupIterator::NOT_FOUND: |
| 747 | case LookupIterator::TRANSITION: |
| 748 | UNREACHABLE(); |
| 749 | case LookupIterator::JSPROXY: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 750 | // Call the "has" trap on proxies. |
| 751 | return JSProxy::HasProperty(it->isolate(), it->GetHolder<JSProxy>(), |
| 752 | it->GetName()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 753 | case LookupIterator::INTERCEPTOR: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 754 | Maybe<PropertyAttributes> result = |
| 755 | JSObject::GetPropertyAttributesWithInterceptor(it); |
| 756 | if (!result.IsJust()) return Nothing<bool>(); |
| 757 | if (result.FromJust() != ABSENT) return Just(true); |
| 758 | break; |
| 759 | } |
| 760 | case LookupIterator::ACCESS_CHECK: { |
| 761 | if (it->HasAccess()) break; |
| 762 | Maybe<PropertyAttributes> result = |
| 763 | JSObject::GetPropertyAttributesWithFailedAccessCheck(it); |
| 764 | if (!result.IsJust()) return Nothing<bool>(); |
| 765 | return Just(result.FromJust() != ABSENT); |
| 766 | } |
| 767 | case LookupIterator::INTEGER_INDEXED_EXOTIC: |
| 768 | // TypedArray out-of-bounds access. |
| 769 | return Just(false); |
| 770 | case LookupIterator::ACCESSOR: |
| 771 | case LookupIterator::DATA: |
| 772 | return Just(true); |
| 773 | } |
| 774 | } |
| 775 | return Just(false); |
| 776 | } |
| 777 | |
| 778 | |
| 779 | // static |
| 780 | MaybeHandle<Object> Object::GetProperty(LookupIterator* it, |
| 781 | LanguageMode language_mode) { |
| 782 | for (; it->IsFound(); it->Next()) { |
| 783 | switch (it->state()) { |
| 784 | case LookupIterator::NOT_FOUND: |
| 785 | case LookupIterator::TRANSITION: |
| 786 | UNREACHABLE(); |
| 787 | case LookupIterator::JSPROXY: |
| 788 | return JSProxy::GetProperty(it->isolate(), it->GetHolder<JSProxy>(), |
| 789 | it->GetName(), it->GetReceiver(), |
| 790 | language_mode); |
| 791 | case LookupIterator::INTERCEPTOR: { |
| 792 | bool done; |
| 793 | Handle<Object> result; |
| 794 | ASSIGN_RETURN_ON_EXCEPTION( |
| 795 | it->isolate(), result, |
| 796 | JSObject::GetPropertyWithInterceptor(it, &done), Object); |
| 797 | if (done) return result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 798 | break; |
| 799 | } |
| 800 | case LookupIterator::ACCESS_CHECK: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 801 | if (it->HasAccess()) break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 802 | return JSObject::GetPropertyWithFailedAccessCheck(it); |
| 803 | case LookupIterator::ACCESSOR: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 804 | return GetPropertyWithAccessor(it, language_mode); |
| 805 | case LookupIterator::INTEGER_INDEXED_EXOTIC: |
| 806 | return ReadAbsentProperty(it, language_mode); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 807 | case LookupIterator::DATA: |
| 808 | return it->GetDataValue(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 809 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 810 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 811 | return ReadAbsentProperty(it, language_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 815 | #define STACK_CHECK(result_value) \ |
| 816 | do { \ |
| 817 | StackLimitCheck stack_check(isolate); \ |
| 818 | if (stack_check.HasOverflowed()) { \ |
| 819 | isolate->Throw(*isolate->factory()->NewRangeError( \ |
| 820 | MessageTemplate::kStackOverflow)); \ |
| 821 | return result_value; \ |
| 822 | } \ |
| 823 | } while (false) |
| 824 | |
| 825 | |
| 826 | // static |
| 827 | MaybeHandle<Object> JSProxy::GetProperty(Isolate* isolate, |
| 828 | Handle<JSProxy> proxy, |
| 829 | Handle<Name> name, |
| 830 | Handle<Object> receiver, |
| 831 | LanguageMode language_mode) { |
| 832 | if (receiver->IsJSGlobalObject()) { |
| 833 | THROW_NEW_ERROR( |
| 834 | isolate, |
| 835 | NewTypeError(MessageTemplate::kReadGlobalReferenceThroughProxy, name), |
| 836 | Object); |
| 837 | } |
| 838 | |
| 839 | DCHECK(!name->IsPrivate()); |
| 840 | STACK_CHECK(MaybeHandle<Object>()); |
| 841 | Handle<Name> trap_name = isolate->factory()->get_string(); |
| 842 | // 1. Assert: IsPropertyKey(P) is true. |
| 843 | // 2. Let handler be the value of the [[ProxyHandler]] internal slot of O. |
| 844 | Handle<Object> handler(proxy->handler(), isolate); |
| 845 | // 3. If handler is null, throw a TypeError exception. |
| 846 | // 4. Assert: Type(handler) is Object. |
| 847 | if (proxy->IsRevoked()) { |
| 848 | THROW_NEW_ERROR(isolate, |
| 849 | NewTypeError(MessageTemplate::kProxyRevoked, trap_name), |
| 850 | Object); |
| 851 | } |
| 852 | // 5. Let target be the value of the [[ProxyTarget]] internal slot of O. |
| 853 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 854 | // 6. Let trap be ? GetMethod(handler, "get"). |
| 855 | Handle<Object> trap; |
| 856 | ASSIGN_RETURN_ON_EXCEPTION( |
| 857 | isolate, trap, |
| 858 | Object::GetMethod(Handle<JSReceiver>::cast(handler), trap_name), Object); |
| 859 | // 7. If trap is undefined, then |
| 860 | if (trap->IsUndefined()) { |
| 861 | // 7.a Return target.[[Get]](P, Receiver). |
| 862 | LookupIterator it = |
| 863 | LookupIterator::PropertyOrElement(isolate, receiver, name, target); |
| 864 | return Object::GetProperty(&it, language_mode); |
| 865 | } |
| 866 | // 8. Let trapResult be ? Call(trap, handler, «target, P, Receiver»). |
| 867 | Handle<Object> trap_result; |
| 868 | Handle<Object> args[] = {target, name, receiver}; |
| 869 | ASSIGN_RETURN_ON_EXCEPTION( |
| 870 | isolate, trap_result, |
| 871 | Execution::Call(isolate, trap, handler, arraysize(args), args), Object); |
| 872 | // 9. Let targetDesc be ? target.[[GetOwnProperty]](P). |
| 873 | PropertyDescriptor target_desc; |
| 874 | Maybe<bool> target_found = |
| 875 | JSReceiver::GetOwnPropertyDescriptor(isolate, target, name, &target_desc); |
| 876 | MAYBE_RETURN_NULL(target_found); |
| 877 | // 10. If targetDesc is not undefined, then |
| 878 | if (target_found.FromJust()) { |
| 879 | // 10.a. If IsDataDescriptor(targetDesc) and targetDesc.[[Configurable]] is |
| 880 | // false and targetDesc.[[Writable]] is false, then |
| 881 | // 10.a.i. If SameValue(trapResult, targetDesc.[[Value]]) is false, |
| 882 | // throw a TypeError exception. |
| 883 | bool inconsistent = PropertyDescriptor::IsDataDescriptor(&target_desc) && |
| 884 | !target_desc.configurable() && |
| 885 | !target_desc.writable() && |
| 886 | !trap_result->SameValue(*target_desc.value()); |
| 887 | if (inconsistent) { |
| 888 | THROW_NEW_ERROR( |
| 889 | isolate, NewTypeError(MessageTemplate::kProxyGetNonConfigurableData, |
| 890 | name, target_desc.value(), trap_result), |
| 891 | Object); |
| 892 | } |
| 893 | // 10.b. If IsAccessorDescriptor(targetDesc) and targetDesc.[[Configurable]] |
| 894 | // is false and targetDesc.[[Get]] is undefined, then |
| 895 | // 10.b.i. If trapResult is not undefined, throw a TypeError exception. |
| 896 | inconsistent = PropertyDescriptor::IsAccessorDescriptor(&target_desc) && |
| 897 | !target_desc.configurable() && |
| 898 | target_desc.get()->IsUndefined() && |
| 899 | !trap_result->IsUndefined(); |
| 900 | if (inconsistent) { |
| 901 | THROW_NEW_ERROR( |
| 902 | isolate, |
| 903 | NewTypeError(MessageTemplate::kProxyGetNonConfigurableAccessor, name, |
| 904 | trap_result), |
| 905 | Object); |
| 906 | } |
| 907 | } |
| 908 | // 11. Return trap_result |
| 909 | return trap_result; |
| 910 | } |
| 911 | |
| 912 | |
| 913 | Handle<Object> JSReceiver::GetDataProperty(Handle<JSReceiver> object, |
| 914 | Handle<Name> name) { |
| 915 | LookupIterator it(object, name, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 916 | LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR); |
| 917 | return GetDataProperty(&it); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 921 | Handle<Object> JSReceiver::GetDataProperty(LookupIterator* it) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 922 | for (; it->IsFound(); it->Next()) { |
| 923 | switch (it->state()) { |
| 924 | case LookupIterator::INTERCEPTOR: |
| 925 | case LookupIterator::NOT_FOUND: |
| 926 | case LookupIterator::TRANSITION: |
| 927 | UNREACHABLE(); |
| 928 | case LookupIterator::ACCESS_CHECK: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 929 | // Support calling this method without an active context, but refuse |
| 930 | // access to access-checked objects in that case. |
| 931 | if (it->isolate()->context() != nullptr && it->HasAccess()) continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 932 | // Fall through. |
| 933 | case LookupIterator::JSPROXY: |
| 934 | it->NotFound(); |
| 935 | return it->isolate()->factory()->undefined_value(); |
| 936 | case LookupIterator::ACCESSOR: |
| 937 | // TODO(verwaest): For now this doesn't call into |
| 938 | // ExecutableAccessorInfo, since clients don't need it. Update once |
| 939 | // relevant. |
| 940 | it->NotFound(); |
| 941 | return it->isolate()->factory()->undefined_value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 942 | case LookupIterator::INTEGER_INDEXED_EXOTIC: |
| 943 | return it->isolate()->factory()->undefined_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 944 | case LookupIterator::DATA: |
| 945 | return it->GetDataValue(); |
| 946 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 947 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 948 | return it->isolate()->factory()->undefined_value(); |
| 949 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 950 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 951 | |
| 952 | bool Object::ToInt32(int32_t* value) { |
| 953 | if (IsSmi()) { |
| 954 | *value = Smi::cast(this)->value(); |
| 955 | return true; |
| 956 | } |
| 957 | if (IsHeapNumber()) { |
| 958 | double num = HeapNumber::cast(this)->value(); |
| 959 | if (FastI2D(FastD2I(num)) == num) { |
| 960 | *value = FastD2I(num); |
| 961 | return true; |
| 962 | } |
| 963 | } |
| 964 | return false; |
| 965 | } |
| 966 | |
| 967 | |
| 968 | bool Object::ToUint32(uint32_t* value) { |
| 969 | if (IsSmi()) { |
| 970 | int num = Smi::cast(this)->value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 971 | if (num < 0) return false; |
| 972 | *value = static_cast<uint32_t>(num); |
| 973 | return true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 974 | } |
| 975 | if (IsHeapNumber()) { |
| 976 | double num = HeapNumber::cast(this)->value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 977 | if (num < 0) return false; |
| 978 | uint32_t uint_value = FastD2UI(num); |
| 979 | if (FastUI2D(uint_value) == num) { |
| 980 | *value = uint_value; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 981 | return true; |
| 982 | } |
| 983 | } |
| 984 | return false; |
| 985 | } |
| 986 | |
| 987 | |
| 988 | bool FunctionTemplateInfo::IsTemplateFor(Object* object) { |
| 989 | if (!object->IsHeapObject()) return false; |
| 990 | return IsTemplateFor(HeapObject::cast(object)->map()); |
| 991 | } |
| 992 | |
| 993 | |
| 994 | bool FunctionTemplateInfo::IsTemplateFor(Map* map) { |
| 995 | // There is a constraint on the object; check. |
| 996 | if (!map->IsJSObjectMap()) return false; |
| 997 | // Fetch the constructor function of the object. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 998 | Object* cons_obj = map->GetConstructor(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 999 | if (!cons_obj->IsJSFunction()) return false; |
| 1000 | JSFunction* fun = JSFunction::cast(cons_obj); |
| 1001 | // Iterate through the chain of inheriting function templates to |
| 1002 | // see if the required one occurs. |
| 1003 | for (Object* type = fun->shared()->function_data(); |
| 1004 | type->IsFunctionTemplateInfo(); |
| 1005 | type = FunctionTemplateInfo::cast(type)->parent_template()) { |
| 1006 | if (type == this) return true; |
| 1007 | } |
| 1008 | // Didn't find the required type in the inheritance chain. |
| 1009 | return false; |
| 1010 | } |
| 1011 | |
| 1012 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1013 | // TODO(dcarney): CallOptimization duplicates this logic, merge. |
| 1014 | Object* FunctionTemplateInfo::GetCompatibleReceiver(Isolate* isolate, |
| 1015 | Object* receiver) { |
| 1016 | // API calls are only supported with JSObject receivers. |
| 1017 | if (!receiver->IsJSObject()) return isolate->heap()->null_value(); |
| 1018 | Object* recv_type = this->signature(); |
| 1019 | // No signature, return holder. |
| 1020 | if (recv_type->IsUndefined()) return receiver; |
| 1021 | FunctionTemplateInfo* signature = FunctionTemplateInfo::cast(recv_type); |
| 1022 | // Check the receiver. |
| 1023 | for (PrototypeIterator iter(isolate, receiver, |
| 1024 | PrototypeIterator::START_AT_RECEIVER); |
| 1025 | !iter.IsAtEnd(PrototypeIterator::END_AT_NON_HIDDEN); iter.Advance()) { |
| 1026 | if (signature->IsTemplateFor(iter.GetCurrent())) return iter.GetCurrent(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1027 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1028 | return isolate->heap()->null_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1032 | // static |
| 1033 | MaybeHandle<JSObject> JSObject::New(Handle<JSFunction> constructor, |
| 1034 | Handle<JSReceiver> new_target, |
| 1035 | Handle<AllocationSite> site) { |
| 1036 | // If called through new, new.target can be: |
| 1037 | // - a subclass of constructor, |
| 1038 | // - a proxy wrapper around constructor, or |
| 1039 | // - the constructor itself. |
| 1040 | // If called through Reflect.construct, it's guaranteed to be a constructor. |
| 1041 | Isolate* const isolate = constructor->GetIsolate(); |
| 1042 | DCHECK(constructor->IsConstructor()); |
| 1043 | DCHECK(new_target->IsConstructor()); |
| 1044 | DCHECK(!constructor->has_initial_map() || |
| 1045 | constructor->initial_map()->instance_type() != JS_FUNCTION_TYPE); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1046 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1047 | Handle<Map> initial_map; |
| 1048 | ASSIGN_RETURN_ON_EXCEPTION( |
| 1049 | isolate, initial_map, |
| 1050 | JSFunction::GetDerivedMap(isolate, constructor, new_target), JSObject); |
| 1051 | Handle<JSObject> result = |
| 1052 | isolate->factory()->NewJSObjectFromMap(initial_map, NOT_TENURED, site); |
| 1053 | isolate->counters()->constructed_objects()->Increment(); |
| 1054 | isolate->counters()->constructed_objects_runtime()->Increment(); |
| 1055 | return result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | |
| 1059 | Handle<FixedArray> JSObject::EnsureWritableFastElements( |
| 1060 | Handle<JSObject> object) { |
| 1061 | DCHECK(object->HasFastSmiOrObjectElements()); |
| 1062 | Isolate* isolate = object->GetIsolate(); |
| 1063 | Handle<FixedArray> elems(FixedArray::cast(object->elements()), isolate); |
| 1064 | if (elems->map() != isolate->heap()->fixed_cow_array_map()) return elems; |
| 1065 | Handle<FixedArray> writable_elems = isolate->factory()->CopyFixedArrayWithMap( |
| 1066 | elems, isolate->factory()->fixed_array_map()); |
| 1067 | object->set_elements(*writable_elems); |
| 1068 | isolate->counters()->cow_arrays_converted()->Increment(); |
| 1069 | return writable_elems; |
| 1070 | } |
| 1071 | |
| 1072 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1073 | // ES6 9.5.1 |
| 1074 | // static |
| 1075 | MaybeHandle<Object> JSProxy::GetPrototype(Handle<JSProxy> proxy) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1076 | Isolate* isolate = proxy->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1077 | Handle<String> trap_name = isolate->factory()->getPrototypeOf_string(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1078 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1079 | STACK_CHECK(MaybeHandle<Object>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1080 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1081 | // 1. Let handler be the value of the [[ProxyHandler]] internal slot. |
| 1082 | // 2. If handler is null, throw a TypeError exception. |
| 1083 | // 3. Assert: Type(handler) is Object. |
| 1084 | // 4. Let target be the value of the [[ProxyTarget]] internal slot. |
| 1085 | if (proxy->IsRevoked()) { |
| 1086 | THROW_NEW_ERROR(isolate, |
| 1087 | NewTypeError(MessageTemplate::kProxyRevoked, trap_name), |
| 1088 | Object); |
| 1089 | } |
| 1090 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 1091 | Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate); |
| 1092 | |
| 1093 | // 5. Let trap be ? GetMethod(handler, "getPrototypeOf"). |
| 1094 | Handle<Object> trap; |
| 1095 | ASSIGN_RETURN_ON_EXCEPTION(isolate, trap, GetMethod(handler, trap_name), |
| 1096 | Object); |
| 1097 | // 6. If trap is undefined, then return target.[[GetPrototypeOf]](). |
| 1098 | if (trap->IsUndefined()) { |
| 1099 | return Object::GetPrototype(isolate, target); |
| 1100 | } |
| 1101 | // 7. Let handlerProto be ? Call(trap, handler, «target»). |
| 1102 | Handle<Object> argv[] = {target}; |
| 1103 | Handle<Object> handler_proto; |
| 1104 | ASSIGN_RETURN_ON_EXCEPTION( |
| 1105 | isolate, handler_proto, |
| 1106 | Execution::Call(isolate, trap, handler, arraysize(argv), argv), Object); |
| 1107 | // 8. If Type(handlerProto) is neither Object nor Null, throw a TypeError. |
| 1108 | if (!(handler_proto->IsJSReceiver() || handler_proto->IsNull())) { |
| 1109 | THROW_NEW_ERROR(isolate, |
| 1110 | NewTypeError(MessageTemplate::kProxyGetPrototypeOfInvalid), |
| 1111 | Object); |
| 1112 | } |
| 1113 | // 9. Let extensibleTarget be ? IsExtensible(target). |
| 1114 | Maybe<bool> is_extensible = JSReceiver::IsExtensible(target); |
| 1115 | MAYBE_RETURN_NULL(is_extensible); |
| 1116 | // 10. If extensibleTarget is true, return handlerProto. |
| 1117 | if (is_extensible.FromJust()) return handler_proto; |
| 1118 | // 11. Let targetProto be ? target.[[GetPrototypeOf]](). |
| 1119 | Handle<Object> target_proto; |
| 1120 | ASSIGN_RETURN_ON_EXCEPTION(isolate, target_proto, |
| 1121 | Object::GetPrototype(isolate, target), Object); |
| 1122 | // 12. If SameValue(handlerProto, targetProto) is false, throw a TypeError. |
| 1123 | if (!handler_proto->SameValue(*target_proto)) { |
| 1124 | THROW_NEW_ERROR( |
| 1125 | isolate, |
| 1126 | NewTypeError(MessageTemplate::kProxyGetPrototypeOfNonExtensible), |
| 1127 | Object); |
| 1128 | } |
| 1129 | // 13. Return handlerProto. |
| 1130 | return handler_proto; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1134 | MaybeHandle<Object> Object::GetPropertyWithAccessor( |
| 1135 | LookupIterator* it, LanguageMode language_mode) { |
| 1136 | Isolate* isolate = it->isolate(); |
| 1137 | Handle<Object> structure = it->GetAccessors(); |
| 1138 | Handle<Object> receiver = it->GetReceiver(); |
| 1139 | |
| 1140 | // We should never get here to initialize a const with the hole value since a |
| 1141 | // const declaration would conflict with the getter. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1142 | DCHECK(!structure->IsForeign()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1143 | |
| 1144 | // API style callbacks. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1145 | if (structure->IsAccessorInfo()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1146 | Handle<JSObject> holder = it->GetHolder<JSObject>(); |
| 1147 | Handle<Name> name = it->GetName(); |
| 1148 | Handle<ExecutableAccessorInfo> info = |
| 1149 | Handle<ExecutableAccessorInfo>::cast(structure); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1150 | if (!info->IsCompatibleReceiver(*receiver)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1151 | THROW_NEW_ERROR(isolate, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1152 | NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, |
| 1153 | name, receiver), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1154 | Object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1155 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1156 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1157 | v8::AccessorNameGetterCallback call_fun = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1158 | v8::ToCData<v8::AccessorNameGetterCallback>(info->getter()); |
| 1159 | if (call_fun == nullptr) return isolate->factory()->undefined_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1160 | |
| 1161 | LOG(isolate, ApiNamedPropertyAccess("load", *holder, *name)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1162 | PropertyCallbackArguments args(isolate, info->data(), *receiver, *holder); |
| 1163 | v8::Local<v8::Value> result = args.Call(call_fun, v8::Utils::ToLocal(name)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1164 | RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1165 | if (result.IsEmpty()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1166 | return ReadAbsentProperty(isolate, receiver, name, language_mode); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1167 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1168 | Handle<Object> return_value = v8::Utils::OpenHandle(*result); |
| 1169 | return_value->VerifyApiCallResultType(); |
| 1170 | // Rebox handle before return. |
| 1171 | return handle(*return_value, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1174 | // Regular accessor. |
| 1175 | Handle<Object> getter(AccessorPair::cast(*structure)->getter(), isolate); |
| 1176 | if (getter->IsCallable()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1177 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
| 1178 | return Object::GetPropertyWithDefinedGetter( |
| 1179 | receiver, Handle<JSReceiver>::cast(getter)); |
| 1180 | } |
| 1181 | // Getter is not a function. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1182 | return ReadAbsentProperty(isolate, receiver, it->GetName(), language_mode); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1186 | bool AccessorInfo::IsCompatibleReceiverMap(Isolate* isolate, |
| 1187 | Handle<AccessorInfo> info, |
| 1188 | Handle<Map> map) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1189 | if (!info->HasExpectedReceiverType()) return true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1190 | if (!map->IsJSObjectMap()) return false; |
| 1191 | return FunctionTemplateInfo::cast(info->expected_receiver_type()) |
| 1192 | ->IsTemplateFor(*map); |
| 1193 | } |
| 1194 | |
| 1195 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1196 | Maybe<bool> Object::SetPropertyWithAccessor(LookupIterator* it, |
| 1197 | Handle<Object> value, |
| 1198 | ShouldThrow should_throw) { |
| 1199 | Isolate* isolate = it->isolate(); |
| 1200 | Handle<Object> structure = it->GetAccessors(); |
| 1201 | Handle<Object> receiver = it->GetReceiver(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1202 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1203 | // We should never get here to initialize a const with the hole value since a |
| 1204 | // const declaration would conflict with the setter. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1205 | DCHECK(!structure->IsForeign()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1206 | |
| 1207 | // API style callbacks. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1208 | if (structure->IsExecutableAccessorInfo()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1209 | Handle<JSObject> holder = it->GetHolder<JSObject>(); |
| 1210 | Handle<Name> name = it->GetName(); |
| 1211 | Handle<ExecutableAccessorInfo> info = |
| 1212 | Handle<ExecutableAccessorInfo>::cast(structure); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1213 | if (!info->IsCompatibleReceiver(*receiver)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1214 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 1215 | MessageTemplate::kIncompatibleMethodReceiver, name, receiver)); |
| 1216 | return Nothing<bool>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1217 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1218 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1219 | v8::AccessorNameSetterCallback call_fun = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1220 | v8::ToCData<v8::AccessorNameSetterCallback>(info->setter()); |
| 1221 | if (call_fun == nullptr) return Just(true); |
| 1222 | // TODO(verwaest): Shouldn't this case be unreachable (at least in the |
| 1223 | // long run?) Should we have ExecutableAccessorPairs with missing setter |
| 1224 | // that are "writable"? If they aren't writable, shouldn't we have bailed |
| 1225 | // out already earlier? |
| 1226 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1227 | LOG(isolate, ApiNamedPropertyAccess("store", *holder, *name)); |
| 1228 | PropertyCallbackArguments args(isolate, info->data(), *receiver, *holder); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1229 | args.Call(call_fun, v8::Utils::ToLocal(name), v8::Utils::ToLocal(value)); |
| 1230 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); |
| 1231 | return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1234 | // Regular accessor. |
| 1235 | Handle<Object> setter(AccessorPair::cast(*structure)->setter(), isolate); |
| 1236 | if (setter->IsCallable()) { |
| 1237 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
| 1238 | return SetPropertyWithDefinedSetter( |
| 1239 | receiver, Handle<JSReceiver>::cast(setter), value, should_throw); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1242 | RETURN_FAILURE(isolate, should_throw, |
| 1243 | NewTypeError(MessageTemplate::kNoSetterInCallback, |
| 1244 | it->GetName(), it->GetHolder<JSObject>())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1248 | MaybeHandle<Object> Object::GetPropertyWithDefinedGetter( |
| 1249 | Handle<Object> receiver, |
| 1250 | Handle<JSReceiver> getter) { |
| 1251 | Isolate* isolate = getter->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1252 | |
| 1253 | // Platforms with simulators like arm/arm64 expose a funny issue. If the |
| 1254 | // simulator has a separate JS stack pointer from the C++ stack pointer, it |
| 1255 | // can miss C++ stack overflows in the stack guard at the start of JavaScript |
| 1256 | // functions. It would be very expensive to check the C++ stack pointer at |
| 1257 | // that location. The best solution seems to be to break the impasse by |
| 1258 | // adding checks at possible recursion points. What's more, we don't put |
| 1259 | // this stack check behind the USE_SIMULATOR define in order to keep |
| 1260 | // behavior the same between hardware and simulators. |
| 1261 | StackLimitCheck check(isolate); |
| 1262 | if (check.JsHasOverflowed()) { |
| 1263 | isolate->StackOverflow(); |
| 1264 | return MaybeHandle<Object>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1265 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1266 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1267 | return Execution::Call(isolate, getter, receiver, 0, NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1271 | Maybe<bool> Object::SetPropertyWithDefinedSetter(Handle<Object> receiver, |
| 1272 | Handle<JSReceiver> setter, |
| 1273 | Handle<Object> value, |
| 1274 | ShouldThrow should_throw) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1275 | Isolate* isolate = setter->GetIsolate(); |
| 1276 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1277 | Handle<Object> argv[] = { value }; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1278 | RETURN_ON_EXCEPTION_VALUE(isolate, Execution::Call(isolate, setter, receiver, |
| 1279 | arraysize(argv), argv), |
| 1280 | Nothing<bool>()); |
| 1281 | return Just(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1285 | // static |
| 1286 | bool Object::IsErrorObject(Isolate* isolate, Handle<Object> object) { |
| 1287 | if (!object->IsJSObject()) return false; |
| 1288 | // Use stack_trace_symbol as proxy for [[ErrorData]]. |
| 1289 | Handle<Name> symbol = isolate->factory()->stack_trace_symbol(); |
| 1290 | Maybe<bool> has_stack_trace = |
| 1291 | JSReceiver::HasOwnProperty(Handle<JSReceiver>::cast(object), symbol); |
| 1292 | DCHECK(!has_stack_trace.IsNothing()); |
| 1293 | return has_stack_trace.FromJust(); |
| 1294 | } |
| 1295 | |
| 1296 | |
| 1297 | // static |
| 1298 | bool JSObject::AllCanRead(LookupIterator* it) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1299 | // Skip current iteration, it's in state ACCESS_CHECK or INTERCEPTOR, both of |
| 1300 | // which have already been checked. |
| 1301 | DCHECK(it->state() == LookupIterator::ACCESS_CHECK || |
| 1302 | it->state() == LookupIterator::INTERCEPTOR); |
| 1303 | for (it->Next(); it->IsFound(); it->Next()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1304 | if (it->state() == LookupIterator::ACCESSOR) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1305 | auto accessors = it->GetAccessors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1306 | if (accessors->IsAccessorInfo()) { |
| 1307 | if (AccessorInfo::cast(*accessors)->all_can_read()) return true; |
| 1308 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1309 | } else if (it->state() == LookupIterator::INTERCEPTOR) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1310 | if (it->GetInterceptor()->all_can_read()) return true; |
| 1311 | } else if (it->state() == LookupIterator::JSPROXY) { |
| 1312 | // Stop lookupiterating. And no, AllCanNotRead. |
| 1313 | return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1314 | } |
| 1315 | } |
| 1316 | return false; |
| 1317 | } |
| 1318 | |
| 1319 | |
| 1320 | MaybeHandle<Object> JSObject::GetPropertyWithFailedAccessCheck( |
| 1321 | LookupIterator* it) { |
| 1322 | Handle<JSObject> checked = it->GetHolder<JSObject>(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1323 | while (AllCanRead(it)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1324 | if (it->state() == LookupIterator::ACCESSOR) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1325 | return GetPropertyWithAccessor(it, SLOPPY); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1326 | } |
| 1327 | DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1328 | bool done; |
| 1329 | Handle<Object> result; |
| 1330 | ASSIGN_RETURN_ON_EXCEPTION(it->isolate(), result, |
| 1331 | GetPropertyWithInterceptor(it, &done), Object); |
| 1332 | if (done) return result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1333 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1334 | |
| 1335 | // Cross-Origin [[Get]] of Well-Known Symbols does not throw, and returns |
| 1336 | // undefined. |
| 1337 | Handle<Name> name = it->GetName(); |
| 1338 | if (name->IsSymbol() && Symbol::cast(*name)->is_well_known_symbol()) { |
| 1339 | return it->factory()->undefined_value(); |
| 1340 | } |
| 1341 | |
| 1342 | it->isolate()->ReportFailedAccessCheck(checked); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1343 | RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(it->isolate(), Object); |
| 1344 | return it->factory()->undefined_value(); |
| 1345 | } |
| 1346 | |
| 1347 | |
| 1348 | Maybe<PropertyAttributes> JSObject::GetPropertyAttributesWithFailedAccessCheck( |
| 1349 | LookupIterator* it) { |
| 1350 | Handle<JSObject> checked = it->GetHolder<JSObject>(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1351 | while (AllCanRead(it)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1352 | if (it->state() == LookupIterator::ACCESSOR) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1353 | return Just(it->property_details().attributes()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1354 | } |
| 1355 | DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1356 | auto result = GetPropertyAttributesWithInterceptor(it); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1357 | if (it->isolate()->has_scheduled_exception()) break; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1358 | if (result.IsJust() && result.FromJust() != ABSENT) return result; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1359 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1360 | it->isolate()->ReportFailedAccessCheck(checked); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1361 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(it->isolate(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1362 | Nothing<PropertyAttributes>()); |
| 1363 | return Just(ABSENT); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1367 | // static |
| 1368 | bool JSObject::AllCanWrite(LookupIterator* it) { |
| 1369 | for (; it->IsFound() && it->state() != LookupIterator::JSPROXY; it->Next()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1370 | if (it->state() == LookupIterator::ACCESSOR) { |
| 1371 | Handle<Object> accessors = it->GetAccessors(); |
| 1372 | if (accessors->IsAccessorInfo()) { |
| 1373 | if (AccessorInfo::cast(*accessors)->all_can_write()) return true; |
| 1374 | } |
| 1375 | } |
| 1376 | } |
| 1377 | return false; |
| 1378 | } |
| 1379 | |
| 1380 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1381 | Maybe<bool> JSObject::SetPropertyWithFailedAccessCheck( |
| 1382 | LookupIterator* it, Handle<Object> value, ShouldThrow should_throw) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1383 | Handle<JSObject> checked = it->GetHolder<JSObject>(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1384 | if (AllCanWrite(it)) { |
| 1385 | return SetPropertyWithAccessor(it, value, should_throw); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1388 | it->isolate()->ReportFailedAccessCheck(checked); |
| 1389 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(it->isolate(), Nothing<bool>()); |
| 1390 | return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | |
| 1394 | void JSObject::SetNormalizedProperty(Handle<JSObject> object, |
| 1395 | Handle<Name> name, |
| 1396 | Handle<Object> value, |
| 1397 | PropertyDetails details) { |
| 1398 | DCHECK(!object->HasFastProperties()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1399 | if (!name->IsUniqueName()) { |
| 1400 | name = object->GetIsolate()->factory()->InternalizeString( |
| 1401 | Handle<String>::cast(name)); |
| 1402 | } |
| 1403 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1404 | if (object->IsJSGlobalObject()) { |
| 1405 | Handle<GlobalDictionary> property_dictionary(object->global_dictionary()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1406 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1407 | int entry = property_dictionary->FindEntry(name); |
| 1408 | if (entry == GlobalDictionary::kNotFound) { |
| 1409 | auto cell = object->GetIsolate()->factory()->NewPropertyCell(); |
| 1410 | cell->set_value(*value); |
| 1411 | auto cell_type = value->IsUndefined() ? PropertyCellType::kUndefined |
| 1412 | : PropertyCellType::kConstant; |
| 1413 | details = details.set_cell_type(cell_type); |
| 1414 | value = cell; |
| 1415 | property_dictionary = |
| 1416 | GlobalDictionary::Add(property_dictionary, name, value, details); |
| 1417 | object->set_properties(*property_dictionary); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1418 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1419 | PropertyCell::UpdateCell(property_dictionary, entry, value, details); |
| 1420 | } |
| 1421 | } else { |
| 1422 | Handle<NameDictionary> property_dictionary(object->property_dictionary()); |
| 1423 | |
| 1424 | int entry = property_dictionary->FindEntry(name); |
| 1425 | if (entry == NameDictionary::kNotFound) { |
| 1426 | property_dictionary = |
| 1427 | NameDictionary::Add(property_dictionary, name, value, details); |
| 1428 | object->set_properties(*property_dictionary); |
| 1429 | } else { |
| 1430 | PropertyDetails original_details = property_dictionary->DetailsAt(entry); |
| 1431 | int enumeration_index = original_details.dictionary_index(); |
| 1432 | DCHECK(enumeration_index > 0); |
| 1433 | details = details.set_index(enumeration_index); |
| 1434 | property_dictionary->SetEntry(entry, name, value, details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1435 | } |
| 1436 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1440 | Maybe<bool> Object::HasInPrototypeChain(Isolate* isolate, Handle<Object> object, |
| 1441 | Handle<Object> proto) { |
| 1442 | PrototypeIterator iter(isolate, object, PrototypeIterator::START_AT_RECEIVER); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1443 | while (true) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1444 | if (!iter.AdvanceFollowingProxies()) return Nothing<bool>(); |
| 1445 | if (iter.IsAtEnd()) return Just(false); |
| 1446 | if (iter.IsAtEnd(proto)) return Just(true); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1447 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1451 | Map* Object::GetRootMap(Isolate* isolate) { |
| 1452 | DisallowHeapAllocation no_alloc; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1453 | if (IsSmi()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1454 | Context* native_context = isolate->context()->native_context(); |
| 1455 | return native_context->number_function()->initial_map(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1456 | } |
| 1457 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1458 | // The object is either a number, a string, a symbol, a boolean, a SIMD value, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1459 | // a real JS object, or a Harmony proxy. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1460 | HeapObject* heap_object = HeapObject::cast(this); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1461 | if (heap_object->IsJSReceiver()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1462 | return heap_object->map(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1463 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1464 | int constructor_function_index = |
| 1465 | heap_object->map()->GetConstructorFunctionIndex(); |
| 1466 | if (constructor_function_index != Map::kNoConstructorFunctionIndex) { |
| 1467 | Context* native_context = isolate->context()->native_context(); |
| 1468 | JSFunction* constructor_function = |
| 1469 | JSFunction::cast(native_context->get(constructor_function_index)); |
| 1470 | return constructor_function->initial_map(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1471 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1472 | return isolate->heap()->null_value()->map(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1476 | Object* Object::GetHash() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1477 | Object* hash = GetSimpleHash(); |
| 1478 | if (hash->IsSmi()) return hash; |
| 1479 | |
| 1480 | DCHECK(IsJSReceiver()); |
| 1481 | return JSReceiver::cast(this)->GetIdentityHash(); |
| 1482 | } |
| 1483 | |
| 1484 | |
| 1485 | Object* Object::GetSimpleHash() { |
| 1486 | // The object is either a Smi, a HeapNumber, a name, an odd-ball, |
| 1487 | // a SIMD value type, a real JS object, or a Harmony proxy. |
| 1488 | if (IsSmi()) { |
| 1489 | uint32_t hash = ComputeIntegerHash(Smi::cast(this)->value(), kZeroHashSeed); |
| 1490 | return Smi::FromInt(hash & Smi::kMaxValue); |
| 1491 | } |
| 1492 | if (IsHeapNumber()) { |
| 1493 | double num = HeapNumber::cast(this)->value(); |
| 1494 | if (std::isnan(num)) return Smi::FromInt(Smi::kMaxValue); |
| 1495 | if (i::IsMinusZero(num)) num = 0; |
| 1496 | if (IsSmiDouble(num)) { |
| 1497 | return Smi::FromInt(FastD2I(num))->GetHash(); |
| 1498 | } |
| 1499 | uint32_t hash = ComputeLongHash(double_to_uint64(num)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1500 | return Smi::FromInt(hash & Smi::kMaxValue); |
| 1501 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1502 | if (IsName()) { |
| 1503 | uint32_t hash = Name::cast(this)->Hash(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1504 | return Smi::FromInt(hash); |
| 1505 | } |
| 1506 | if (IsOddball()) { |
| 1507 | uint32_t hash = Oddball::cast(this)->to_string()->Hash(); |
| 1508 | return Smi::FromInt(hash); |
| 1509 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1510 | if (IsSimd128Value()) { |
| 1511 | uint32_t hash = Simd128Value::cast(this)->Hash(); |
| 1512 | return Smi::FromInt(hash & Smi::kMaxValue); |
| 1513 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1514 | DCHECK(IsJSReceiver()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1515 | JSReceiver* receiver = JSReceiver::cast(this); |
| 1516 | return receiver->GetHeap()->undefined_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | |
| 1520 | Handle<Smi> Object::GetOrCreateHash(Isolate* isolate, Handle<Object> object) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1521 | Handle<Object> hash(object->GetSimpleHash(), isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1522 | if (hash->IsSmi()) return Handle<Smi>::cast(hash); |
| 1523 | |
| 1524 | DCHECK(object->IsJSReceiver()); |
| 1525 | return JSReceiver::GetOrCreateIdentityHash(Handle<JSReceiver>::cast(object)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | |
| 1529 | bool Object::SameValue(Object* other) { |
| 1530 | if (other == this) return true; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1531 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1532 | // The object is either a number, a name, an odd-ball, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1533 | // a real JS object, or a Harmony proxy. |
| 1534 | if (IsNumber() && other->IsNumber()) { |
| 1535 | double this_value = Number(); |
| 1536 | double other_value = other->Number(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1537 | // SameValue(NaN, NaN) is true. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1538 | if (this_value != other_value) { |
| 1539 | return std::isnan(this_value) && std::isnan(other_value); |
| 1540 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1541 | // SameValue(0.0, -0.0) is false. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1542 | return (std::signbit(this_value) == std::signbit(other_value)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1543 | } |
| 1544 | if (IsString() && other->IsString()) { |
| 1545 | return String::cast(this)->Equals(String::cast(other)); |
| 1546 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1547 | if (IsSimd128Value() && other->IsSimd128Value()) { |
| 1548 | if (IsFloat32x4() && other->IsFloat32x4()) { |
| 1549 | Float32x4* a = Float32x4::cast(this); |
| 1550 | Float32x4* b = Float32x4::cast(other); |
| 1551 | for (int i = 0; i < 4; i++) { |
| 1552 | float x = a->get_lane(i); |
| 1553 | float y = b->get_lane(i); |
| 1554 | // Implements the ES5 SameValue operation for floating point types. |
| 1555 | // http://www.ecma-international.org/ecma-262/6.0/#sec-samevalue |
| 1556 | if (x != y && !(std::isnan(x) && std::isnan(y))) return false; |
| 1557 | if (std::signbit(x) != std::signbit(y)) return false; |
| 1558 | } |
| 1559 | return true; |
| 1560 | } else { |
| 1561 | Simd128Value* a = Simd128Value::cast(this); |
| 1562 | Simd128Value* b = Simd128Value::cast(other); |
| 1563 | return a->map()->instance_type() == b->map()->instance_type() && |
| 1564 | a->BitwiseEquals(b); |
| 1565 | } |
| 1566 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1567 | return false; |
| 1568 | } |
| 1569 | |
| 1570 | |
| 1571 | bool Object::SameValueZero(Object* other) { |
| 1572 | if (other == this) return true; |
| 1573 | |
| 1574 | // The object is either a number, a name, an odd-ball, |
| 1575 | // a real JS object, or a Harmony proxy. |
| 1576 | if (IsNumber() && other->IsNumber()) { |
| 1577 | double this_value = Number(); |
| 1578 | double other_value = other->Number(); |
| 1579 | // +0 == -0 is true |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1580 | return this_value == other_value || |
| 1581 | (std::isnan(this_value) && std::isnan(other_value)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1582 | } |
| 1583 | if (IsString() && other->IsString()) { |
| 1584 | return String::cast(this)->Equals(String::cast(other)); |
| 1585 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1586 | if (IsSimd128Value() && other->IsSimd128Value()) { |
| 1587 | if (IsFloat32x4() && other->IsFloat32x4()) { |
| 1588 | Float32x4* a = Float32x4::cast(this); |
| 1589 | Float32x4* b = Float32x4::cast(other); |
| 1590 | for (int i = 0; i < 4; i++) { |
| 1591 | float x = a->get_lane(i); |
| 1592 | float y = b->get_lane(i); |
| 1593 | // Implements the ES6 SameValueZero operation for floating point types. |
| 1594 | // http://www.ecma-international.org/ecma-262/6.0/#sec-samevaluezero |
| 1595 | if (x != y && !(std::isnan(x) && std::isnan(y))) return false; |
| 1596 | // SameValueZero doesn't distinguish between 0 and -0. |
| 1597 | } |
| 1598 | return true; |
| 1599 | } else { |
| 1600 | Simd128Value* a = Simd128Value::cast(this); |
| 1601 | Simd128Value* b = Simd128Value::cast(other); |
| 1602 | return a->map()->instance_type() == b->map()->instance_type() && |
| 1603 | a->BitwiseEquals(b); |
| 1604 | } |
| 1605 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1606 | return false; |
| 1607 | } |
| 1608 | |
| 1609 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1610 | MaybeHandle<Object> Object::ArraySpeciesConstructor( |
| 1611 | Isolate* isolate, Handle<Object> original_array) { |
| 1612 | Handle<Context> native_context = isolate->native_context(); |
| 1613 | if (!FLAG_harmony_species) { |
| 1614 | return Handle<Object>(native_context->array_function(), isolate); |
| 1615 | } |
| 1616 | Handle<Object> constructor = isolate->factory()->undefined_value(); |
| 1617 | Maybe<bool> is_array = Object::IsArray(original_array); |
| 1618 | MAYBE_RETURN_NULL(is_array); |
| 1619 | if (is_array.FromJust()) { |
| 1620 | ASSIGN_RETURN_ON_EXCEPTION( |
| 1621 | isolate, constructor, |
| 1622 | Object::GetProperty(original_array, |
| 1623 | isolate->factory()->constructor_string()), |
| 1624 | Object); |
| 1625 | if (constructor->IsConstructor()) { |
| 1626 | Handle<Context> constructor_context; |
| 1627 | ASSIGN_RETURN_ON_EXCEPTION( |
| 1628 | isolate, constructor_context, |
| 1629 | JSReceiver::GetFunctionRealm(Handle<JSReceiver>::cast(constructor)), |
| 1630 | Object); |
| 1631 | if (*constructor_context != *native_context && |
| 1632 | *constructor == constructor_context->array_function()) { |
| 1633 | constructor = isolate->factory()->undefined_value(); |
| 1634 | } |
| 1635 | } |
| 1636 | if (constructor->IsJSReceiver()) { |
| 1637 | ASSIGN_RETURN_ON_EXCEPTION( |
| 1638 | isolate, constructor, |
| 1639 | Object::GetProperty(constructor, |
| 1640 | isolate->factory()->species_symbol()), |
| 1641 | Object); |
| 1642 | if (constructor->IsNull()) { |
| 1643 | constructor = isolate->factory()->undefined_value(); |
| 1644 | } |
| 1645 | } |
| 1646 | } |
| 1647 | if (constructor->IsUndefined()) { |
| 1648 | return Handle<Object>(native_context->array_function(), isolate); |
| 1649 | } else { |
| 1650 | if (!constructor->IsConstructor()) { |
| 1651 | THROW_NEW_ERROR(isolate, |
| 1652 | NewTypeError(MessageTemplate::kSpeciesNotConstructor), |
| 1653 | Object); |
| 1654 | } |
| 1655 | return constructor; |
| 1656 | } |
| 1657 | } |
| 1658 | |
| 1659 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1660 | void Object::ShortPrint(FILE* out) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1661 | OFStream os(out); |
| 1662 | os << Brief(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | |
| 1666 | void Object::ShortPrint(StringStream* accumulator) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1667 | std::ostringstream os; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1668 | os << Brief(this); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1669 | accumulator->Add(os.str().c_str()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1673 | void Object::ShortPrint(std::ostream& os) { os << Brief(this); } |
| 1674 | |
| 1675 | |
| 1676 | std::ostream& operator<<(std::ostream& os, const Brief& v) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1677 | if (v.value->IsSmi()) { |
| 1678 | Smi::cast(v.value)->SmiPrint(os); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1679 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1680 | // TODO(svenpanne) Const-correct HeapObjectShortPrint! |
| 1681 | HeapObject* obj = const_cast<HeapObject*>(HeapObject::cast(v.value)); |
| 1682 | obj->HeapObjectShortPrint(os); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1683 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1684 | return os; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1688 | void Smi::SmiPrint(std::ostream& os) const { // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1689 | os << value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1693 | // Should a word be prefixed by 'a' or 'an' in order to read naturally in |
| 1694 | // English? Returns false for non-ASCII or words that don't start with |
| 1695 | // a capital letter. The a/an rule follows pronunciation in English. |
| 1696 | // We don't use the BBC's overcorrect "an historic occasion" though if |
| 1697 | // you speak a dialect you may well say "an 'istoric occasion". |
| 1698 | static bool AnWord(String* str) { |
| 1699 | if (str->length() == 0) return false; // A nothing. |
| 1700 | int c0 = str->Get(0); |
| 1701 | int c1 = str->length() > 1 ? str->Get(1) : 0; |
| 1702 | if (c0 == 'U') { |
| 1703 | if (c1 > 'Z') { |
| 1704 | return true; // An Umpire, but a UTF8String, a U. |
| 1705 | } |
| 1706 | } else if (c0 == 'A' || c0 == 'E' || c0 == 'I' || c0 == 'O') { |
| 1707 | return true; // An Ape, an ABCBook. |
| 1708 | } else if ((c1 == 0 || (c1 >= 'A' && c1 <= 'Z')) && |
| 1709 | (c0 == 'F' || c0 == 'H' || c0 == 'M' || c0 == 'N' || c0 == 'R' || |
| 1710 | c0 == 'S' || c0 == 'X')) { |
| 1711 | return true; // An MP3File, an M. |
| 1712 | } |
| 1713 | return false; |
| 1714 | } |
| 1715 | |
| 1716 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1717 | Handle<String> String::SlowFlatten(Handle<ConsString> cons, |
| 1718 | PretenureFlag pretenure) { |
| 1719 | DCHECK(AllowHeapAllocation::IsAllowed()); |
| 1720 | DCHECK(cons->second()->length() != 0); |
| 1721 | Isolate* isolate = cons->GetIsolate(); |
| 1722 | int length = cons->length(); |
| 1723 | PretenureFlag tenure = isolate->heap()->InNewSpace(*cons) ? pretenure |
| 1724 | : TENURED; |
| 1725 | Handle<SeqString> result; |
| 1726 | if (cons->IsOneByteRepresentation()) { |
| 1727 | Handle<SeqOneByteString> flat = isolate->factory()->NewRawOneByteString( |
| 1728 | length, tenure).ToHandleChecked(); |
| 1729 | DisallowHeapAllocation no_gc; |
| 1730 | WriteToFlat(*cons, flat->GetChars(), 0, length); |
| 1731 | result = flat; |
| 1732 | } else { |
| 1733 | Handle<SeqTwoByteString> flat = isolate->factory()->NewRawTwoByteString( |
| 1734 | length, tenure).ToHandleChecked(); |
| 1735 | DisallowHeapAllocation no_gc; |
| 1736 | WriteToFlat(*cons, flat->GetChars(), 0, length); |
| 1737 | result = flat; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1738 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1739 | cons->set_first(*result); |
| 1740 | cons->set_second(isolate->heap()->empty_string()); |
| 1741 | DCHECK(result->IsFlat()); |
| 1742 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1746 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1747 | bool String::MakeExternal(v8::String::ExternalStringResource* resource) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1748 | // Externalizing twice leaks the external resource, so it's |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1749 | // prohibited by the API. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1750 | DCHECK(!this->IsExternalString()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1751 | DCHECK(!resource->IsCompressible()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1752 | #ifdef ENABLE_SLOW_DCHECKS |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1753 | if (FLAG_enable_slow_asserts) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1754 | // Assert that the resource and the string are equivalent. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1755 | DCHECK(static_cast<size_t>(this->length()) == resource->length()); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1756 | ScopedVector<uc16> smart_chars(this->length()); |
| 1757 | String::WriteToFlat(this, smart_chars.start(), 0, this->length()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1758 | DCHECK(memcmp(smart_chars.start(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1759 | resource->data(), |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1760 | resource->length() * sizeof(smart_chars[0])) == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1761 | } |
| 1762 | #endif // DEBUG |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1763 | int size = this->Size(); // Byte size of the original string. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1764 | // Abort if size does not allow in-place conversion. |
| 1765 | if (size < ExternalString::kShortSize) return false; |
| 1766 | Heap* heap = GetHeap(); |
| 1767 | bool is_one_byte = this->IsOneByteRepresentation(); |
| 1768 | bool is_internalized = this->IsInternalizedString(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1769 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1770 | // Morph the string to an external string by replacing the map and |
| 1771 | // reinitializing the fields. This won't work if the space the existing |
| 1772 | // string occupies is too small for a regular external string. |
| 1773 | // Instead, we resort to a short external string instead, omitting |
| 1774 | // the field caching the address of the backing store. When we encounter |
| 1775 | // short external strings in generated code, we need to bailout to runtime. |
| 1776 | Map* new_map; |
| 1777 | if (size < ExternalString::kSize) { |
| 1778 | new_map = is_internalized |
| 1779 | ? (is_one_byte |
| 1780 | ? heap->short_external_internalized_string_with_one_byte_data_map() |
| 1781 | : heap->short_external_internalized_string_map()) |
| 1782 | : (is_one_byte ? heap->short_external_string_with_one_byte_data_map() |
| 1783 | : heap->short_external_string_map()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1784 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1785 | new_map = is_internalized |
| 1786 | ? (is_one_byte |
| 1787 | ? heap->external_internalized_string_with_one_byte_data_map() |
| 1788 | : heap->external_internalized_string_map()) |
| 1789 | : (is_one_byte ? heap->external_string_with_one_byte_data_map() |
| 1790 | : heap->external_string_map()); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 1791 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1792 | |
| 1793 | // Byte size of the external String object. |
| 1794 | int new_size = this->SizeFromMap(new_map); |
| 1795 | heap->CreateFillerObjectAt(this->address() + new_size, size - new_size); |
| 1796 | |
| 1797 | // We are storing the new map using release store after creating a filler for |
| 1798 | // the left-over space to avoid races with the sweeper thread. |
| 1799 | this->synchronized_set_map(new_map); |
| 1800 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1801 | ExternalTwoByteString* self = ExternalTwoByteString::cast(this); |
| 1802 | self->set_resource(resource); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1803 | if (is_internalized) self->Hash(); // Force regeneration of the hash value. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1804 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1805 | heap->AdjustLiveBytes(this, new_size - size, Heap::CONCURRENT_TO_SWEEPER); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1806 | return true; |
| 1807 | } |
| 1808 | |
| 1809 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1810 | bool String::MakeExternal(v8::String::ExternalOneByteStringResource* resource) { |
| 1811 | // Externalizing twice leaks the external resource, so it's |
| 1812 | // prohibited by the API. |
| 1813 | DCHECK(!this->IsExternalString()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1814 | DCHECK(!resource->IsCompressible()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1815 | #ifdef ENABLE_SLOW_DCHECKS |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1816 | if (FLAG_enable_slow_asserts) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1817 | // Assert that the resource and the string are equivalent. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1818 | DCHECK(static_cast<size_t>(this->length()) == resource->length()); |
| 1819 | if (this->IsTwoByteRepresentation()) { |
| 1820 | ScopedVector<uint16_t> smart_chars(this->length()); |
| 1821 | String::WriteToFlat(this, smart_chars.start(), 0, this->length()); |
| 1822 | DCHECK(String::IsOneByte(smart_chars.start(), this->length())); |
| 1823 | } |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1824 | ScopedVector<char> smart_chars(this->length()); |
| 1825 | String::WriteToFlat(this, smart_chars.start(), 0, this->length()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1826 | DCHECK(memcmp(smart_chars.start(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1827 | resource->data(), |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1828 | resource->length() * sizeof(smart_chars[0])) == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1829 | } |
| 1830 | #endif // DEBUG |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1831 | int size = this->Size(); // Byte size of the original string. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1832 | // Abort if size does not allow in-place conversion. |
| 1833 | if (size < ExternalString::kShortSize) return false; |
| 1834 | Heap* heap = GetHeap(); |
| 1835 | bool is_internalized = this->IsInternalizedString(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1836 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1837 | // Morph the string to an external string by replacing the map and |
| 1838 | // reinitializing the fields. This won't work if the space the existing |
| 1839 | // string occupies is too small for a regular external string. |
| 1840 | // Instead, we resort to a short external string instead, omitting |
| 1841 | // the field caching the address of the backing store. When we encounter |
| 1842 | // short external strings in generated code, we need to bailout to runtime. |
| 1843 | Map* new_map; |
| 1844 | if (size < ExternalString::kSize) { |
| 1845 | new_map = is_internalized |
| 1846 | ? heap->short_external_one_byte_internalized_string_map() |
| 1847 | : heap->short_external_one_byte_string_map(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1848 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1849 | new_map = is_internalized |
| 1850 | ? heap->external_one_byte_internalized_string_map() |
| 1851 | : heap->external_one_byte_string_map(); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 1852 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1853 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1854 | // Byte size of the external String object. |
| 1855 | int new_size = this->SizeFromMap(new_map); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1856 | heap->CreateFillerObjectAt(this->address() + new_size, size - new_size); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1857 | |
| 1858 | // We are storing the new map using release store after creating a filler for |
| 1859 | // the left-over space to avoid races with the sweeper thread. |
| 1860 | this->synchronized_set_map(new_map); |
| 1861 | |
| 1862 | ExternalOneByteString* self = ExternalOneByteString::cast(this); |
| 1863 | self->set_resource(resource); |
| 1864 | if (is_internalized) self->Hash(); // Force regeneration of the hash value. |
| 1865 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1866 | heap->AdjustLiveBytes(this, new_size - size, Heap::CONCURRENT_TO_SWEEPER); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1867 | return true; |
| 1868 | } |
| 1869 | |
| 1870 | |
| 1871 | void String::StringShortPrint(StringStream* accumulator) { |
| 1872 | int len = length(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1873 | if (len > kMaxShortPrintLength) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1874 | accumulator->Add("<Very long string[%u]>", len); |
| 1875 | return; |
| 1876 | } |
| 1877 | |
| 1878 | if (!LooksValid()) { |
| 1879 | accumulator->Add("<Invalid String>"); |
| 1880 | return; |
| 1881 | } |
| 1882 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1883 | StringCharacterStream stream(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1884 | |
| 1885 | bool truncated = false; |
| 1886 | if (len > kMaxShortPrintLength) { |
| 1887 | len = kMaxShortPrintLength; |
| 1888 | truncated = true; |
| 1889 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1890 | bool one_byte = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1891 | for (int i = 0; i < len; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1892 | uint16_t c = stream.GetNext(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1893 | |
| 1894 | if (c < 32 || c >= 127) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1895 | one_byte = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1896 | } |
| 1897 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1898 | stream.Reset(this); |
| 1899 | if (one_byte) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1900 | accumulator->Add("<String[%u]: ", length()); |
| 1901 | for (int i = 0; i < len; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1902 | accumulator->Put(static_cast<char>(stream.GetNext())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1903 | } |
| 1904 | accumulator->Put('>'); |
| 1905 | } else { |
| 1906 | // Backslash indicates that the string contains control |
| 1907 | // characters and that backslashes are therefore escaped. |
| 1908 | accumulator->Add("<String[%u]\\: ", length()); |
| 1909 | for (int i = 0; i < len; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1910 | uint16_t c = stream.GetNext(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1911 | if (c == '\n') { |
| 1912 | accumulator->Add("\\n"); |
| 1913 | } else if (c == '\r') { |
| 1914 | accumulator->Add("\\r"); |
| 1915 | } else if (c == '\\') { |
| 1916 | accumulator->Add("\\\\"); |
| 1917 | } else if (c < 32 || c > 126) { |
| 1918 | accumulator->Add("\\x%02x", c); |
| 1919 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1920 | accumulator->Put(static_cast<char>(c)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1921 | } |
| 1922 | } |
| 1923 | if (truncated) { |
| 1924 | accumulator->Put('.'); |
| 1925 | accumulator->Put('.'); |
| 1926 | accumulator->Put('.'); |
| 1927 | } |
| 1928 | accumulator->Put('>'); |
| 1929 | } |
| 1930 | return; |
| 1931 | } |
| 1932 | |
| 1933 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1934 | void String::PrintUC16(std::ostream& os, int start, int end) { // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1935 | if (end < 0) end = length(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1936 | StringCharacterStream stream(this, start); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1937 | for (int i = start; i < end && stream.HasMore(); i++) { |
| 1938 | os << AsUC16(stream.GetNext()); |
| 1939 | } |
| 1940 | } |
| 1941 | |
| 1942 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1943 | void JSObject::JSObjectShortPrint(StringStream* accumulator) { |
| 1944 | switch (map()->instance_type()) { |
| 1945 | case JS_ARRAY_TYPE: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1946 | double length = JSArray::cast(this)->length()->IsUndefined() |
| 1947 | ? 0 |
| 1948 | : JSArray::cast(this)->length()->Number(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1949 | accumulator->Add("<JS Array[%u]>", static_cast<uint32_t>(length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1950 | break; |
| 1951 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 1952 | case JS_BOUND_FUNCTION_TYPE: { |
| 1953 | JSBoundFunction* bound_function = JSBoundFunction::cast(this); |
| 1954 | Object* name = bound_function->name(); |
| 1955 | accumulator->Add("<JS BoundFunction"); |
| 1956 | if (name->IsString()) { |
| 1957 | String* str = String::cast(name); |
| 1958 | if (str->length() > 0) { |
| 1959 | accumulator->Add(" "); |
| 1960 | accumulator->Put(str); |
| 1961 | } |
| 1962 | } |
| 1963 | accumulator->Add( |
| 1964 | " (BoundTargetFunction %p)>", |
| 1965 | reinterpret_cast<void*>(bound_function->bound_target_function())); |
| 1966 | break; |
| 1967 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1968 | case JS_WEAK_MAP_TYPE: { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1969 | accumulator->Add("<JS WeakMap>"); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1970 | break; |
| 1971 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1972 | case JS_WEAK_SET_TYPE: { |
| 1973 | accumulator->Add("<JS WeakSet>"); |
| 1974 | break; |
| 1975 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1976 | case JS_REGEXP_TYPE: { |
| 1977 | accumulator->Add("<JS RegExp>"); |
| 1978 | break; |
| 1979 | } |
| 1980 | case JS_FUNCTION_TYPE: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1981 | JSFunction* function = JSFunction::cast(this); |
| 1982 | Object* fun_name = function->shared()->DebugName(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1983 | bool printed = false; |
| 1984 | if (fun_name->IsString()) { |
| 1985 | String* str = String::cast(fun_name); |
| 1986 | if (str->length() > 0) { |
| 1987 | accumulator->Add("<JS Function "); |
| 1988 | accumulator->Put(str); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1989 | printed = true; |
| 1990 | } |
| 1991 | } |
| 1992 | if (!printed) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1993 | accumulator->Add("<JS Function"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1994 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1995 | accumulator->Add(" (SharedFunctionInfo %p)", |
| 1996 | reinterpret_cast<void*>(function->shared())); |
| 1997 | accumulator->Put('>'); |
| 1998 | break; |
| 1999 | } |
| 2000 | case JS_GENERATOR_OBJECT_TYPE: { |
| 2001 | accumulator->Add("<JS Generator>"); |
| 2002 | break; |
| 2003 | } |
| 2004 | case JS_MODULE_TYPE: { |
| 2005 | accumulator->Add("<JS Module>"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2006 | break; |
| 2007 | } |
| 2008 | // All other JSObjects are rather similar to each other (JSObject, |
| 2009 | // JSGlobalProxy, JSGlobalObject, JSUndetectableObject, JSValue). |
| 2010 | default: { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2011 | Map* map_of_this = map(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2012 | Heap* heap = GetHeap(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2013 | Object* constructor = map_of_this->GetConstructor(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2014 | bool printed = false; |
| 2015 | if (constructor->IsHeapObject() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2016 | !heap->Contains(HeapObject::cast(constructor))) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2017 | accumulator->Add("!!!INVALID CONSTRUCTOR!!!"); |
| 2018 | } else { |
| 2019 | bool global_object = IsJSGlobalProxy(); |
| 2020 | if (constructor->IsJSFunction()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2021 | if (!heap->Contains(JSFunction::cast(constructor)->shared())) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2022 | accumulator->Add("!!!INVALID SHARED ON CONSTRUCTOR!!!"); |
| 2023 | } else { |
| 2024 | Object* constructor_name = |
| 2025 | JSFunction::cast(constructor)->shared()->name(); |
| 2026 | if (constructor_name->IsString()) { |
| 2027 | String* str = String::cast(constructor_name); |
| 2028 | if (str->length() > 0) { |
| 2029 | bool vowel = AnWord(str); |
| 2030 | accumulator->Add("<%sa%s ", |
| 2031 | global_object ? "Global Object: " : "", |
| 2032 | vowel ? "n" : ""); |
| 2033 | accumulator->Put(str); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2034 | accumulator->Add(" with %smap %p", |
| 2035 | map_of_this->is_deprecated() ? "deprecated " : "", |
| 2036 | map_of_this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2037 | printed = true; |
| 2038 | } |
| 2039 | } |
| 2040 | } |
| 2041 | } |
| 2042 | if (!printed) { |
| 2043 | accumulator->Add("<JS %sObject", global_object ? "Global " : ""); |
| 2044 | } |
| 2045 | } |
| 2046 | if (IsJSValue()) { |
| 2047 | accumulator->Add(" value = "); |
| 2048 | JSValue::cast(this)->value()->ShortPrint(accumulator); |
| 2049 | } |
| 2050 | accumulator->Put('>'); |
| 2051 | break; |
| 2052 | } |
| 2053 | } |
| 2054 | } |
| 2055 | |
| 2056 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2057 | void JSObject::PrintElementsTransition( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2058 | FILE* file, Handle<JSObject> object, |
| 2059 | ElementsKind from_kind, Handle<FixedArrayBase> from_elements, |
| 2060 | ElementsKind to_kind, Handle<FixedArrayBase> to_elements) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2061 | if (from_kind != to_kind) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2062 | OFStream os(file); |
| 2063 | os << "elements transition [" << ElementsKindToString(from_kind) << " -> " |
| 2064 | << ElementsKindToString(to_kind) << "] in "; |
| 2065 | JavaScriptFrame::PrintTop(object->GetIsolate(), file, false, true); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2066 | PrintF(file, " for "); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2067 | object->ShortPrint(file); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2068 | PrintF(file, " from "); |
| 2069 | from_elements->ShortPrint(file); |
| 2070 | PrintF(file, " to "); |
| 2071 | to_elements->ShortPrint(file); |
| 2072 | PrintF(file, "\n"); |
| 2073 | } |
| 2074 | } |
| 2075 | |
| 2076 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2077 | // static |
| 2078 | MaybeHandle<JSFunction> Map::GetConstructorFunction( |
| 2079 | Handle<Map> map, Handle<Context> native_context) { |
| 2080 | if (map->IsPrimitiveMap()) { |
| 2081 | int const constructor_function_index = map->GetConstructorFunctionIndex(); |
| 2082 | if (constructor_function_index != kNoConstructorFunctionIndex) { |
| 2083 | return handle( |
| 2084 | JSFunction::cast(native_context->get(constructor_function_index))); |
| 2085 | } |
| 2086 | } |
| 2087 | return MaybeHandle<JSFunction>(); |
| 2088 | } |
| 2089 | |
| 2090 | |
| 2091 | void Map::PrintReconfiguration(FILE* file, int modify_index, PropertyKind kind, |
| 2092 | PropertyAttributes attributes) { |
| 2093 | OFStream os(file); |
| 2094 | os << "[reconfiguring]"; |
| 2095 | Name* name = instance_descriptors()->GetKey(modify_index); |
| 2096 | if (name->IsString()) { |
| 2097 | String::cast(name)->PrintOn(file); |
| 2098 | } else { |
| 2099 | os << "{symbol " << static_cast<void*>(name) << "}"; |
| 2100 | } |
| 2101 | os << ": " << (kind == kData ? "kData" : "ACCESSORS") << ", attrs: "; |
| 2102 | os << attributes << " ["; |
| 2103 | JavaScriptFrame::PrintTop(GetIsolate(), file, false, true); |
| 2104 | os << "]\n"; |
| 2105 | } |
| 2106 | |
| 2107 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2108 | void Map::PrintGeneralization(FILE* file, |
| 2109 | const char* reason, |
| 2110 | int modify_index, |
| 2111 | int split, |
| 2112 | int descriptors, |
| 2113 | bool constant_to_field, |
| 2114 | Representation old_representation, |
| 2115 | Representation new_representation, |
| 2116 | HeapType* old_field_type, |
| 2117 | HeapType* new_field_type) { |
| 2118 | OFStream os(file); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2119 | os << "[generalizing]"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2120 | Name* name = instance_descriptors()->GetKey(modify_index); |
| 2121 | if (name->IsString()) { |
| 2122 | String::cast(name)->PrintOn(file); |
| 2123 | } else { |
| 2124 | os << "{symbol " << static_cast<void*>(name) << "}"; |
| 2125 | } |
| 2126 | os << ":"; |
| 2127 | if (constant_to_field) { |
| 2128 | os << "c"; |
| 2129 | } else { |
| 2130 | os << old_representation.Mnemonic() << "{"; |
| 2131 | old_field_type->PrintTo(os, HeapType::SEMANTIC_DIM); |
| 2132 | os << "}"; |
| 2133 | } |
| 2134 | os << "->" << new_representation.Mnemonic() << "{"; |
| 2135 | new_field_type->PrintTo(os, HeapType::SEMANTIC_DIM); |
| 2136 | os << "} ("; |
| 2137 | if (strlen(reason) > 0) { |
| 2138 | os << reason; |
| 2139 | } else { |
| 2140 | os << "+" << (descriptors - split) << " maps"; |
| 2141 | } |
| 2142 | os << ") ["; |
| 2143 | JavaScriptFrame::PrintTop(GetIsolate(), file, false, true); |
| 2144 | os << "]\n"; |
| 2145 | } |
| 2146 | |
| 2147 | |
| 2148 | void JSObject::PrintInstanceMigration(FILE* file, |
| 2149 | Map* original_map, |
| 2150 | Map* new_map) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2151 | PrintF(file, "[migrating]"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2152 | DescriptorArray* o = original_map->instance_descriptors(); |
| 2153 | DescriptorArray* n = new_map->instance_descriptors(); |
| 2154 | for (int i = 0; i < original_map->NumberOfOwnDescriptors(); i++) { |
| 2155 | Representation o_r = o->GetDetails(i).representation(); |
| 2156 | Representation n_r = n->GetDetails(i).representation(); |
| 2157 | if (!o_r.Equals(n_r)) { |
| 2158 | String::cast(o->GetKey(i))->PrintOn(file); |
| 2159 | PrintF(file, ":%s->%s ", o_r.Mnemonic(), n_r.Mnemonic()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2160 | } else if (o->GetDetails(i).type() == DATA_CONSTANT && |
| 2161 | n->GetDetails(i).type() == DATA) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2162 | Name* name = o->GetKey(i); |
| 2163 | if (name->IsString()) { |
| 2164 | String::cast(name)->PrintOn(file); |
| 2165 | } else { |
| 2166 | PrintF(file, "{symbol %p}", static_cast<void*>(name)); |
| 2167 | } |
| 2168 | PrintF(file, " "); |
| 2169 | } |
| 2170 | } |
| 2171 | PrintF(file, "\n"); |
| 2172 | } |
| 2173 | |
| 2174 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2175 | void HeapObject::HeapObjectShortPrint(std::ostream& os) { // NOLINT |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2176 | Heap* heap = GetHeap(); |
| 2177 | if (!heap->Contains(this)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2178 | os << "!!!INVALID POINTER!!!"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2179 | return; |
| 2180 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2181 | if (!heap->Contains(map())) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2182 | os << "!!!INVALID MAP!!!"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2183 | return; |
| 2184 | } |
| 2185 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2186 | os << this << " "; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2187 | |
| 2188 | if (IsString()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2189 | HeapStringAllocator allocator; |
| 2190 | StringStream accumulator(&allocator); |
| 2191 | String::cast(this)->StringShortPrint(&accumulator); |
| 2192 | os << accumulator.ToCString().get(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2193 | return; |
| 2194 | } |
| 2195 | if (IsJSObject()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2196 | HeapStringAllocator allocator; |
| 2197 | StringStream accumulator(&allocator); |
| 2198 | JSObject::cast(this)->JSObjectShortPrint(&accumulator); |
| 2199 | os << accumulator.ToCString().get(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2200 | return; |
| 2201 | } |
| 2202 | switch (map()->instance_type()) { |
| 2203 | case MAP_TYPE: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2204 | os << "<Map(" << ElementsKindToString(Map::cast(this)->elements_kind()) |
| 2205 | << ")>"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2206 | break; |
| 2207 | case FIXED_ARRAY_TYPE: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2208 | os << "<FixedArray[" << FixedArray::cast(this)->length() << "]>"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2209 | break; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2210 | case FIXED_DOUBLE_ARRAY_TYPE: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2211 | os << "<FixedDoubleArray[" << FixedDoubleArray::cast(this)->length() |
| 2212 | << "]>"; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2213 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2214 | case BYTE_ARRAY_TYPE: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2215 | os << "<ByteArray[" << ByteArray::cast(this)->length() << "]>"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2216 | break; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2217 | case BYTECODE_ARRAY_TYPE: |
| 2218 | os << "<BytecodeArray[" << BytecodeArray::cast(this)->length() << "]>"; |
| 2219 | break; |
| 2220 | case TRANSITION_ARRAY_TYPE: |
| 2221 | os << "<TransitionArray[" << TransitionArray::cast(this)->length() |
| 2222 | << "]>"; |
| 2223 | break; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2224 | case FREE_SPACE_TYPE: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2225 | os << "<FreeSpace[" << FreeSpace::cast(this)->size() << "]>"; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2226 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2227 | #define TYPED_ARRAY_SHORT_PRINT(Type, type, TYPE, ctype, size) \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2228 | case FIXED_##TYPE##_ARRAY_TYPE: \ |
| 2229 | os << "<Fixed" #Type "Array[" << Fixed##Type##Array::cast(this)->length() \ |
| 2230 | << "]>"; \ |
| 2231 | break; |
| 2232 | |
| 2233 | TYPED_ARRAYS(TYPED_ARRAY_SHORT_PRINT) |
| 2234 | #undef TYPED_ARRAY_SHORT_PRINT |
| 2235 | |
| 2236 | case SHARED_FUNCTION_INFO_TYPE: { |
| 2237 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(this); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2238 | base::SmartArrayPointer<char> debug_name = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2239 | shared->DebugName()->ToCString(); |
| 2240 | if (debug_name[0] != 0) { |
| 2241 | os << "<SharedFunctionInfo " << debug_name.get() << ">"; |
| 2242 | } else { |
| 2243 | os << "<SharedFunctionInfo>"; |
| 2244 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2245 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2246 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2247 | case JS_MESSAGE_OBJECT_TYPE: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2248 | os << "<JSMessageObject>"; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2249 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2250 | #define MAKE_STRUCT_CASE(NAME, Name, name) \ |
| 2251 | case NAME##_TYPE: \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2252 | os << "<" #Name ">"; \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2253 | break; |
| 2254 | STRUCT_LIST(MAKE_STRUCT_CASE) |
| 2255 | #undef MAKE_STRUCT_CASE |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2256 | case CODE_TYPE: { |
| 2257 | Code* code = Code::cast(this); |
| 2258 | os << "<Code: " << Code::Kind2String(code->kind()) << ">"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2259 | break; |
| 2260 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2261 | case ODDBALL_TYPE: { |
| 2262 | if (IsUndefined()) { |
| 2263 | os << "<undefined>"; |
| 2264 | } else if (IsTheHole()) { |
| 2265 | os << "<the hole>"; |
| 2266 | } else if (IsNull()) { |
| 2267 | os << "<null>"; |
| 2268 | } else if (IsTrue()) { |
| 2269 | os << "<true>"; |
| 2270 | } else if (IsFalse()) { |
| 2271 | os << "<false>"; |
| 2272 | } else { |
| 2273 | os << "<Odd Oddball>"; |
| 2274 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2275 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2276 | } |
| 2277 | case SYMBOL_TYPE: { |
| 2278 | Symbol* symbol = Symbol::cast(this); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2279 | symbol->SymbolShortPrint(os); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2280 | break; |
| 2281 | } |
| 2282 | case HEAP_NUMBER_TYPE: { |
| 2283 | os << "<Number: "; |
| 2284 | HeapNumber::cast(this)->HeapNumberPrint(os); |
| 2285 | os << ">"; |
| 2286 | break; |
| 2287 | } |
| 2288 | case MUTABLE_HEAP_NUMBER_TYPE: { |
| 2289 | os << "<MutableNumber: "; |
| 2290 | HeapNumber::cast(this)->HeapNumberPrint(os); |
| 2291 | os << '>'; |
| 2292 | break; |
| 2293 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2294 | case SIMD128_VALUE_TYPE: { |
| 2295 | #define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \ |
| 2296 | if (Is##Type()) { \ |
| 2297 | os << "<" #Type ">"; \ |
| 2298 | break; \ |
| 2299 | } |
| 2300 | SIMD128_TYPES(SIMD128_TYPE) |
| 2301 | #undef SIMD128_TYPE |
| 2302 | UNREACHABLE(); |
| 2303 | break; |
| 2304 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2305 | case JS_PROXY_TYPE: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2306 | os << "<JSProxy>"; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2307 | break; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2308 | case FOREIGN_TYPE: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2309 | os << "<Foreign>"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2310 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2311 | case CELL_TYPE: { |
| 2312 | os << "Cell for "; |
| 2313 | HeapStringAllocator allocator; |
| 2314 | StringStream accumulator(&allocator); |
| 2315 | Cell::cast(this)->value()->ShortPrint(&accumulator); |
| 2316 | os << accumulator.ToCString().get(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2317 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2318 | } |
| 2319 | case PROPERTY_CELL_TYPE: { |
| 2320 | os << "PropertyCell for "; |
| 2321 | HeapStringAllocator allocator; |
| 2322 | StringStream accumulator(&allocator); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2323 | PropertyCell* cell = PropertyCell::cast(this); |
| 2324 | cell->value()->ShortPrint(&accumulator); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2325 | os << accumulator.ToCString().get(); |
| 2326 | break; |
| 2327 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2328 | case WEAK_CELL_TYPE: { |
| 2329 | os << "WeakCell for "; |
| 2330 | HeapStringAllocator allocator; |
| 2331 | StringStream accumulator(&allocator); |
| 2332 | WeakCell::cast(this)->value()->ShortPrint(&accumulator); |
| 2333 | os << accumulator.ToCString().get(); |
| 2334 | break; |
| 2335 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2336 | default: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2337 | os << "<Other heap object (" << map()->instance_type() << ")>"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2338 | break; |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2343 | void HeapObject::Iterate(ObjectVisitor* v) { IterateFast<ObjectVisitor>(v); } |
| 2344 | |
| 2345 | |
| 2346 | void HeapObject::IterateBody(ObjectVisitor* v) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2347 | Map* m = map(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2348 | IterateBodyFast<ObjectVisitor>(m->instance_type(), SizeFromMap(m), v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | |
| 2352 | void HeapObject::IterateBody(InstanceType type, int object_size, |
| 2353 | ObjectVisitor* v) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2354 | IterateBodyFast<ObjectVisitor>(type, object_size, v); |
| 2355 | } |
| 2356 | |
| 2357 | |
| 2358 | struct CallIsValidSlot { |
| 2359 | template <typename BodyDescriptor> |
| 2360 | static bool apply(HeapObject* obj, int offset, int) { |
| 2361 | return BodyDescriptor::IsValidSlot(obj, offset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2362 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2363 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2364 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2365 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2366 | bool HeapObject::IsValidSlot(int offset) { |
| 2367 | DCHECK_NE(0, offset); |
| 2368 | return BodyDescriptorApply<CallIsValidSlot, bool>(map()->instance_type(), |
| 2369 | this, offset, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2373 | bool HeapNumber::HeapNumberBooleanValue() { |
| 2374 | return DoubleToBoolean(value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2378 | void HeapNumber::HeapNumberPrint(std::ostream& os) { // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2379 | os << value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
| 2382 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2383 | #define FIELD_ADDR_CONST(p, offset) \ |
| 2384 | (reinterpret_cast<const byte*>(p) + offset - kHeapObjectTag) |
| 2385 | |
| 2386 | #define READ_INT32_FIELD(p, offset) \ |
| 2387 | (*reinterpret_cast<const int32_t*>(FIELD_ADDR_CONST(p, offset))) |
| 2388 | |
| 2389 | #define READ_INT64_FIELD(p, offset) \ |
| 2390 | (*reinterpret_cast<const int64_t*>(FIELD_ADDR_CONST(p, offset))) |
| 2391 | |
| 2392 | #define READ_BYTE_FIELD(p, offset) \ |
| 2393 | (*reinterpret_cast<const byte*>(FIELD_ADDR_CONST(p, offset))) |
| 2394 | |
| 2395 | |
| 2396 | // static |
| 2397 | Handle<String> Simd128Value::ToString(Handle<Simd128Value> input) { |
| 2398 | #define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \ |
| 2399 | if (input->Is##Type()) return Type::ToString(Handle<Type>::cast(input)); |
| 2400 | SIMD128_TYPES(SIMD128_TYPE) |
| 2401 | #undef SIMD128_TYPE |
| 2402 | UNREACHABLE(); |
| 2403 | return Handle<String>::null(); |
| 2404 | } |
| 2405 | |
| 2406 | |
| 2407 | // static |
| 2408 | Handle<String> Float32x4::ToString(Handle<Float32x4> input) { |
| 2409 | Isolate* const isolate = input->GetIsolate(); |
| 2410 | char arr[100]; |
| 2411 | Vector<char> buffer(arr, arraysize(arr)); |
| 2412 | std::ostringstream os; |
| 2413 | os << "SIMD.Float32x4(" |
| 2414 | << std::string(DoubleToCString(input->get_lane(0), buffer)) << ", " |
| 2415 | << std::string(DoubleToCString(input->get_lane(1), buffer)) << ", " |
| 2416 | << std::string(DoubleToCString(input->get_lane(2), buffer)) << ", " |
| 2417 | << std::string(DoubleToCString(input->get_lane(3), buffer)) << ")"; |
| 2418 | return isolate->factory()->NewStringFromAsciiChecked(os.str().c_str()); |
| 2419 | } |
| 2420 | |
| 2421 | |
| 2422 | #define SIMD128_BOOL_TO_STRING(Type, lane_count) \ |
| 2423 | Handle<String> Type::ToString(Handle<Type> input) { \ |
| 2424 | Isolate* const isolate = input->GetIsolate(); \ |
| 2425 | std::ostringstream os; \ |
| 2426 | os << "SIMD." #Type "("; \ |
| 2427 | os << (input->get_lane(0) ? "true" : "false"); \ |
| 2428 | for (int i = 1; i < lane_count; i++) { \ |
| 2429 | os << ", " << (input->get_lane(i) ? "true" : "false"); \ |
| 2430 | } \ |
| 2431 | os << ")"; \ |
| 2432 | return isolate->factory()->NewStringFromAsciiChecked(os.str().c_str()); \ |
| 2433 | } |
| 2434 | SIMD128_BOOL_TO_STRING(Bool32x4, 4) |
| 2435 | SIMD128_BOOL_TO_STRING(Bool16x8, 8) |
| 2436 | SIMD128_BOOL_TO_STRING(Bool8x16, 16) |
| 2437 | #undef SIMD128_BOOL_TO_STRING |
| 2438 | |
| 2439 | |
| 2440 | #define SIMD128_INT_TO_STRING(Type, lane_count) \ |
| 2441 | Handle<String> Type::ToString(Handle<Type> input) { \ |
| 2442 | Isolate* const isolate = input->GetIsolate(); \ |
| 2443 | char arr[100]; \ |
| 2444 | Vector<char> buffer(arr, arraysize(arr)); \ |
| 2445 | std::ostringstream os; \ |
| 2446 | os << "SIMD." #Type "("; \ |
| 2447 | os << IntToCString(input->get_lane(0), buffer); \ |
| 2448 | for (int i = 1; i < lane_count; i++) { \ |
| 2449 | os << ", " << IntToCString(input->get_lane(i), buffer); \ |
| 2450 | } \ |
| 2451 | os << ")"; \ |
| 2452 | return isolate->factory()->NewStringFromAsciiChecked(os.str().c_str()); \ |
| 2453 | } |
| 2454 | SIMD128_INT_TO_STRING(Int32x4, 4) |
| 2455 | SIMD128_INT_TO_STRING(Uint32x4, 4) |
| 2456 | SIMD128_INT_TO_STRING(Int16x8, 8) |
| 2457 | SIMD128_INT_TO_STRING(Uint16x8, 8) |
| 2458 | SIMD128_INT_TO_STRING(Int8x16, 16) |
| 2459 | SIMD128_INT_TO_STRING(Uint8x16, 16) |
| 2460 | #undef SIMD128_INT_TO_STRING |
| 2461 | |
| 2462 | |
| 2463 | bool Simd128Value::BitwiseEquals(const Simd128Value* other) const { |
| 2464 | return READ_INT64_FIELD(this, kValueOffset) == |
| 2465 | READ_INT64_FIELD(other, kValueOffset) && |
| 2466 | READ_INT64_FIELD(this, kValueOffset + kInt64Size) == |
| 2467 | READ_INT64_FIELD(other, kValueOffset + kInt64Size); |
| 2468 | } |
| 2469 | |
| 2470 | |
| 2471 | uint32_t Simd128Value::Hash() const { |
| 2472 | uint32_t seed = v8::internal::kZeroHashSeed; |
| 2473 | uint32_t hash; |
| 2474 | hash = ComputeIntegerHash(READ_INT32_FIELD(this, kValueOffset), seed); |
| 2475 | hash = ComputeIntegerHash( |
| 2476 | READ_INT32_FIELD(this, kValueOffset + 1 * kInt32Size), hash * 31); |
| 2477 | hash = ComputeIntegerHash( |
| 2478 | READ_INT32_FIELD(this, kValueOffset + 2 * kInt32Size), hash * 31); |
| 2479 | hash = ComputeIntegerHash( |
| 2480 | READ_INT32_FIELD(this, kValueOffset + 3 * kInt32Size), hash * 31); |
| 2481 | return hash; |
| 2482 | } |
| 2483 | |
| 2484 | |
| 2485 | void Simd128Value::CopyBits(void* destination) const { |
| 2486 | memcpy(destination, &READ_BYTE_FIELD(this, kValueOffset), kSimd128Size); |
| 2487 | } |
| 2488 | |
| 2489 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2490 | String* JSReceiver::class_name() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2491 | if (IsFunction()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2492 | return GetHeap()->Function_string(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2493 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2494 | Object* maybe_constructor = map()->GetConstructor(); |
| 2495 | if (maybe_constructor->IsJSFunction()) { |
| 2496 | JSFunction* constructor = JSFunction::cast(maybe_constructor); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2497 | return String::cast(constructor->shared()->instance_class_name()); |
| 2498 | } |
| 2499 | // If the constructor is not present, return "Object". |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2500 | return GetHeap()->Object_string(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2501 | } |
| 2502 | |
| 2503 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2504 | MaybeHandle<String> JSReceiver::BuiltinStringTag(Handle<JSReceiver> object) { |
| 2505 | Maybe<bool> is_array = Object::IsArray(object); |
| 2506 | MAYBE_RETURN(is_array, MaybeHandle<String>()); |
| 2507 | Isolate* const isolate = object->GetIsolate(); |
| 2508 | if (is_array.FromJust()) { |
| 2509 | return isolate->factory()->Array_string(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2510 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2511 | // TODO(adamk): According to ES2015, we should return "Function" when |
| 2512 | // object has a [[Call]] internal method (corresponds to IsCallable). |
| 2513 | // But this is well cemented in layout tests and might cause webbreakage. |
| 2514 | // if (object->IsCallable()) { |
| 2515 | // return isolate->factory()->Function_string(); |
| 2516 | // } |
| 2517 | // TODO(adamk): class_name() is expensive, replace with instance type |
| 2518 | // checks where possible. |
| 2519 | return handle(object->class_name(), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
| 2522 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2523 | // static |
| 2524 | Handle<String> JSReceiver::GetConstructorName(Handle<JSReceiver> receiver) { |
| 2525 | Isolate* isolate = receiver->GetIsolate(); |
| 2526 | |
| 2527 | // If the object was instantiated simply with base == new.target, the |
| 2528 | // constructor on the map provides the most accurate name. |
| 2529 | // Don't provide the info for prototypes, since their constructors are |
| 2530 | // reclaimed and replaced by Object in OptimizeAsPrototype. |
| 2531 | if (!receiver->IsJSProxy() && receiver->map()->new_target_is_base() && |
| 2532 | !receiver->map()->is_prototype_map()) { |
| 2533 | Object* maybe_constructor = receiver->map()->GetConstructor(); |
| 2534 | if (maybe_constructor->IsJSFunction()) { |
| 2535 | JSFunction* constructor = JSFunction::cast(maybe_constructor); |
| 2536 | String* name = String::cast(constructor->shared()->name()); |
| 2537 | if (name->length() == 0) name = constructor->shared()->inferred_name(); |
| 2538 | if (name->length() != 0 && |
| 2539 | !name->Equals(isolate->heap()->Object_string())) { |
| 2540 | return handle(name, isolate); |
| 2541 | } |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | if (FLAG_harmony_tostring) { |
| 2546 | Handle<Object> maybe_tag = JSReceiver::GetDataProperty( |
| 2547 | receiver, isolate->factory()->to_string_tag_symbol()); |
| 2548 | if (maybe_tag->IsString()) return Handle<String>::cast(maybe_tag); |
| 2549 | } |
| 2550 | |
| 2551 | PrototypeIterator iter(isolate, receiver); |
| 2552 | if (iter.IsAtEnd()) return handle(receiver->class_name()); |
| 2553 | Handle<JSReceiver> start = PrototypeIterator::GetCurrent<JSReceiver>(iter); |
| 2554 | LookupIterator it(receiver, isolate->factory()->constructor_string(), start, |
| 2555 | LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR); |
| 2556 | Handle<Object> maybe_constructor = JSReceiver::GetDataProperty(&it); |
| 2557 | Handle<String> result = isolate->factory()->Object_string(); |
| 2558 | if (maybe_constructor->IsJSFunction()) { |
| 2559 | JSFunction* constructor = JSFunction::cast(*maybe_constructor); |
| 2560 | String* name = String::cast(constructor->shared()->name()); |
| 2561 | if (name->length() == 0) name = constructor->shared()->inferred_name(); |
| 2562 | if (name->length() > 0) result = handle(name, isolate); |
| 2563 | } |
| 2564 | |
| 2565 | return result.is_identical_to(isolate->factory()->Object_string()) |
| 2566 | ? handle(receiver->class_name()) |
| 2567 | : result; |
| 2568 | } |
| 2569 | |
| 2570 | |
| 2571 | Context* JSReceiver::GetCreationContext() { |
| 2572 | if (IsJSBoundFunction()) { |
| 2573 | return JSBoundFunction::cast(this)->creation_context(); |
| 2574 | } |
| 2575 | Object* constructor = map()->GetConstructor(); |
| 2576 | JSFunction* function; |
| 2577 | if (constructor->IsJSFunction()) { |
| 2578 | function = JSFunction::cast(constructor); |
| 2579 | } else { |
| 2580 | // Functions have null as a constructor, |
| 2581 | // but any JSFunction knows its context immediately. |
| 2582 | CHECK(IsJSFunction()); |
| 2583 | function = JSFunction::cast(this); |
| 2584 | } |
| 2585 | |
| 2586 | return function->context()->native_context(); |
| 2587 | } |
| 2588 | |
| 2589 | |
| 2590 | static Handle<Object> WrapType(Handle<HeapType> type) { |
| 2591 | if (type->IsClass()) return Map::WeakCellForMap(type->AsClass()->Map()); |
| 2592 | return type; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2596 | MaybeHandle<Map> Map::CopyWithField(Handle<Map> map, |
| 2597 | Handle<Name> name, |
| 2598 | Handle<HeapType> type, |
| 2599 | PropertyAttributes attributes, |
| 2600 | Representation representation, |
| 2601 | TransitionFlag flag) { |
| 2602 | DCHECK(DescriptorArray::kNotFound == |
| 2603 | map->instance_descriptors()->Search( |
| 2604 | *name, map->NumberOfOwnDescriptors())); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2605 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2606 | // Ensure the descriptor array does not get too big. |
| 2607 | if (map->NumberOfOwnDescriptors() >= kMaxNumberOfDescriptors) { |
| 2608 | return MaybeHandle<Map>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2611 | Isolate* isolate = map->GetIsolate(); |
| 2612 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2613 | // Compute the new index for new field. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2614 | int index = map->NextFreePropertyIndex(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2615 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2616 | if (map->instance_type() == JS_CONTEXT_EXTENSION_OBJECT_TYPE) { |
| 2617 | representation = Representation::Tagged(); |
| 2618 | type = HeapType::Any(isolate); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2619 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2620 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2621 | Handle<Object> wrapped_type(WrapType(type)); |
| 2622 | |
| 2623 | DataDescriptor new_field_desc(name, index, wrapped_type, attributes, |
| 2624 | representation); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2625 | Handle<Map> new_map = Map::CopyAddDescriptor(map, &new_field_desc, flag); |
| 2626 | int unused_property_fields = new_map->unused_property_fields() - 1; |
| 2627 | if (unused_property_fields < 0) { |
| 2628 | unused_property_fields += JSObject::kFieldsAdded; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2629 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2630 | new_map->set_unused_property_fields(unused_property_fields); |
| 2631 | return new_map; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2635 | MaybeHandle<Map> Map::CopyWithConstant(Handle<Map> map, |
| 2636 | Handle<Name> name, |
| 2637 | Handle<Object> constant, |
| 2638 | PropertyAttributes attributes, |
| 2639 | TransitionFlag flag) { |
| 2640 | // Ensure the descriptor array does not get too big. |
| 2641 | if (map->NumberOfOwnDescriptors() >= kMaxNumberOfDescriptors) { |
| 2642 | return MaybeHandle<Map>(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2643 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2644 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2645 | // Allocate new instance descriptors with (name, constant) added. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2646 | DataConstantDescriptor new_constant_desc(name, constant, attributes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2647 | return Map::CopyAddDescriptor(map, &new_constant_desc, flag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2651 | void JSObject::AddSlowProperty(Handle<JSObject> object, |
| 2652 | Handle<Name> name, |
| 2653 | Handle<Object> value, |
| 2654 | PropertyAttributes attributes) { |
| 2655 | DCHECK(!object->HasFastProperties()); |
| 2656 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2657 | if (object->IsJSGlobalObject()) { |
| 2658 | Handle<GlobalDictionary> dict(object->global_dictionary()); |
| 2659 | PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2660 | int entry = dict->FindEntry(name); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2661 | // If there's a cell there, just invalidate and set the property. |
| 2662 | if (entry != GlobalDictionary::kNotFound) { |
| 2663 | PropertyCell::UpdateCell(dict, entry, value, details); |
| 2664 | // TODO(ishell): move this to UpdateCell. |
| 2665 | // Need to adjust the details. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2666 | int index = dict->NextEnumerationIndex(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2667 | dict->SetNextEnumerationIndex(index + 1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2668 | PropertyCell* cell = PropertyCell::cast(dict->ValueAt(entry)); |
| 2669 | details = cell->property_details().set_index(index); |
| 2670 | cell->set_property_details(details); |
| 2671 | |
| 2672 | } else { |
| 2673 | auto cell = isolate->factory()->NewPropertyCell(); |
| 2674 | cell->set_value(*value); |
| 2675 | auto cell_type = value->IsUndefined() ? PropertyCellType::kUndefined |
| 2676 | : PropertyCellType::kConstant; |
| 2677 | details = details.set_cell_type(cell_type); |
| 2678 | value = cell; |
| 2679 | |
| 2680 | Handle<GlobalDictionary> result = |
| 2681 | GlobalDictionary::Add(dict, name, value, details); |
| 2682 | if (*dict != *result) object->set_properties(*result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2683 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2684 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2685 | Handle<NameDictionary> dict(object->property_dictionary()); |
| 2686 | PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell); |
| 2687 | Handle<NameDictionary> result = |
| 2688 | NameDictionary::Add(dict, name, value, details); |
| 2689 | if (*dict != *result) object->set_properties(*result); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2690 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2691 | } |
| 2692 | |
| 2693 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2694 | MaybeHandle<Object> JSObject::EnqueueChangeRecord(Handle<JSObject> object, |
| 2695 | const char* type_str, |
| 2696 | Handle<Name> name, |
| 2697 | Handle<Object> old_value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2698 | DCHECK(!object->IsJSGlobalProxy()); |
| 2699 | DCHECK(!object->IsJSGlobalObject()); |
| 2700 | Isolate* isolate = object->GetIsolate(); |
| 2701 | HandleScope scope(isolate); |
| 2702 | Handle<String> type = isolate->factory()->InternalizeUtf8String(type_str); |
| 2703 | Handle<Object> args[] = { type, object, name, old_value }; |
| 2704 | int argc = name.is_null() ? 2 : old_value->IsTheHole() ? 3 : 4; |
| 2705 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2706 | return Execution::Call(isolate, |
| 2707 | Handle<JSFunction>(isolate->observers_notify_change()), |
| 2708 | isolate->factory()->undefined_value(), argc, args); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
| 2711 | |
| 2712 | const char* Representation::Mnemonic() const { |
| 2713 | switch (kind_) { |
| 2714 | case kNone: return "v"; |
| 2715 | case kTagged: return "t"; |
| 2716 | case kSmi: return "s"; |
| 2717 | case kDouble: return "d"; |
| 2718 | case kInteger32: return "i"; |
| 2719 | case kHeapObject: return "h"; |
| 2720 | case kExternal: return "x"; |
| 2721 | default: |
| 2722 | UNREACHABLE(); |
| 2723 | return NULL; |
| 2724 | } |
| 2725 | } |
| 2726 | |
| 2727 | |
| 2728 | bool Map::InstancesNeedRewriting(Map* target, int target_number_of_fields, |
| 2729 | int target_inobject, int target_unused, |
| 2730 | int* old_number_of_fields) { |
| 2731 | // If fields were added (or removed), rewrite the instance. |
| 2732 | *old_number_of_fields = NumberOfFields(); |
| 2733 | DCHECK(target_number_of_fields >= *old_number_of_fields); |
| 2734 | if (target_number_of_fields != *old_number_of_fields) return true; |
| 2735 | |
| 2736 | // If smi descriptors were replaced by double descriptors, rewrite. |
| 2737 | DescriptorArray* old_desc = instance_descriptors(); |
| 2738 | DescriptorArray* new_desc = target->instance_descriptors(); |
| 2739 | int limit = NumberOfOwnDescriptors(); |
| 2740 | for (int i = 0; i < limit; i++) { |
| 2741 | if (new_desc->GetDetails(i).representation().IsDouble() != |
| 2742 | old_desc->GetDetails(i).representation().IsDouble()) { |
| 2743 | return true; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2744 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2745 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2746 | |
| 2747 | // If no fields were added, and no inobject properties were removed, setting |
| 2748 | // the map is sufficient. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2749 | if (target_inobject == GetInObjectProperties()) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2750 | // In-object slack tracking may have reduced the object size of the new map. |
| 2751 | // In that case, succeed if all existing fields were inobject, and they still |
| 2752 | // fit within the new inobject size. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2753 | DCHECK(target_inobject < GetInObjectProperties()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2754 | if (target_number_of_fields <= target_inobject) { |
| 2755 | DCHECK(target_number_of_fields + target_unused == target_inobject); |
| 2756 | return false; |
| 2757 | } |
| 2758 | // Otherwise, properties will need to be moved to the backing store. |
| 2759 | return true; |
| 2760 | } |
| 2761 | |
| 2762 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2763 | // static |
| 2764 | void JSObject::UpdatePrototypeUserRegistration(Handle<Map> old_map, |
| 2765 | Handle<Map> new_map, |
| 2766 | Isolate* isolate) { |
| 2767 | if (!FLAG_track_prototype_users) return; |
| 2768 | if (!old_map->is_prototype_map()) return; |
| 2769 | DCHECK(new_map->is_prototype_map()); |
| 2770 | bool was_registered = JSObject::UnregisterPrototypeUser(old_map, isolate); |
| 2771 | new_map->set_prototype_info(old_map->prototype_info()); |
| 2772 | old_map->set_prototype_info(Smi::FromInt(0)); |
| 2773 | if (FLAG_trace_prototype_users) { |
| 2774 | PrintF("Moving prototype_info %p from map %p to map %p.\n", |
| 2775 | reinterpret_cast<void*>(new_map->prototype_info()), |
| 2776 | reinterpret_cast<void*>(*old_map), |
| 2777 | reinterpret_cast<void*>(*new_map)); |
| 2778 | } |
| 2779 | if (was_registered) { |
| 2780 | if (new_map->prototype_info()->IsPrototypeInfo()) { |
| 2781 | // The new map isn't registered with its prototype yet; reflect this fact |
| 2782 | // in the PrototypeInfo it just inherited from the old map. |
| 2783 | PrototypeInfo::cast(new_map->prototype_info()) |
| 2784 | ->set_registry_slot(PrototypeInfo::UNREGISTERED); |
| 2785 | } |
| 2786 | JSObject::LazyRegisterPrototypeUser(new_map, isolate); |
| 2787 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
| 2790 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2791 | void JSObject::MigrateToMap(Handle<JSObject> object, Handle<Map> new_map, |
| 2792 | int expected_additional_properties) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2793 | if (object->map() == *new_map) return; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2794 | // If this object is a prototype (the callee will check), invalidate any |
| 2795 | // prototype chains involving it. |
| 2796 | InvalidatePrototypeChains(object->map()); |
| 2797 | Handle<Map> old_map(object->map()); |
| 2798 | |
| 2799 | // If the map was registered with its prototype before, ensure that it |
| 2800 | // registers with its new prototype now. This preserves the invariant that |
| 2801 | // when a map on a prototype chain is registered with its prototype, then |
| 2802 | // all prototypes further up the chain are also registered with their |
| 2803 | // respective prototypes. |
| 2804 | UpdatePrototypeUserRegistration(old_map, new_map, new_map->GetIsolate()); |
| 2805 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2806 | if (object->HasFastProperties()) { |
| 2807 | if (!new_map->is_dictionary_map()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2808 | MigrateFastToFast(object, new_map); |
| 2809 | if (old_map->is_prototype_map()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2810 | DCHECK(!old_map->is_stable()); |
| 2811 | DCHECK(new_map->is_stable()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2812 | // Clear out the old descriptor array to avoid problems to sharing |
| 2813 | // the descriptor array without using an explicit. |
| 2814 | old_map->InitializeDescriptors( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2815 | old_map->GetHeap()->empty_descriptor_array(), |
| 2816 | LayoutDescriptor::FastPointerLayout()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2817 | // Ensure that no transition was inserted for prototype migrations. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2818 | DCHECK_EQ(0, TransitionArray::NumberOfTransitions( |
| 2819 | old_map->raw_transitions())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2820 | DCHECK(new_map->GetBackPointer()->IsUndefined()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2821 | } |
| 2822 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2823 | MigrateFastToSlow(object, new_map, expected_additional_properties); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2824 | } |
| 2825 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2826 | // For slow-to-fast migrations JSObject::MigrateSlowToFast() |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2827 | // must be used instead. |
| 2828 | CHECK(new_map->is_dictionary_map()); |
| 2829 | |
| 2830 | // Slow-to-slow migration is trivial. |
| 2831 | object->set_map(*new_map); |
| 2832 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2833 | |
| 2834 | // Careful: Don't allocate here! |
| 2835 | // For some callers of this method, |object| might be in an inconsistent |
| 2836 | // state now: the new map might have a new elements_kind, but the object's |
| 2837 | // elements pointer hasn't been updated yet. Callers will fix this, but in |
| 2838 | // the meantime, (indirectly) calling JSObjectVerify() must be avoided. |
| 2839 | // When adding code here, add a DisallowHeapAllocation too. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
| 2842 | |
| 2843 | // To migrate a fast instance to a fast map: |
| 2844 | // - First check whether the instance needs to be rewritten. If not, simply |
| 2845 | // change the map. |
| 2846 | // - Otherwise, allocate a fixed array large enough to hold all fields, in |
| 2847 | // addition to unused space. |
| 2848 | // - Copy all existing properties in, in the following order: backing store |
| 2849 | // properties, unused fields, inobject properties. |
| 2850 | // - If all allocation succeeded, commit the state atomically: |
| 2851 | // * Copy inobject properties from the backing store back into the object. |
| 2852 | // * Trim the difference in instance size of the object. This also cleanly |
| 2853 | // frees inobject properties that moved to the backing store. |
| 2854 | // * If there are properties left in the backing store, trim of the space used |
| 2855 | // to temporarily store the inobject properties. |
| 2856 | // * If there are properties left in the backing store, install the backing |
| 2857 | // store. |
| 2858 | void JSObject::MigrateFastToFast(Handle<JSObject> object, Handle<Map> new_map) { |
| 2859 | Isolate* isolate = object->GetIsolate(); |
| 2860 | Handle<Map> old_map(object->map()); |
| 2861 | int old_number_of_fields; |
| 2862 | int number_of_fields = new_map->NumberOfFields(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2863 | int inobject = new_map->GetInObjectProperties(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2864 | int unused = new_map->unused_property_fields(); |
| 2865 | |
| 2866 | // Nothing to do if no functions were converted to fields and no smis were |
| 2867 | // converted to doubles. |
| 2868 | if (!old_map->InstancesNeedRewriting(*new_map, number_of_fields, inobject, |
| 2869 | unused, &old_number_of_fields)) { |
| 2870 | object->synchronized_set_map(*new_map); |
| 2871 | return; |
| 2872 | } |
| 2873 | |
| 2874 | int total_size = number_of_fields + unused; |
| 2875 | int external = total_size - inobject; |
| 2876 | |
| 2877 | if (number_of_fields != old_number_of_fields && |
| 2878 | new_map->GetBackPointer() == *old_map) { |
| 2879 | PropertyDetails details = new_map->GetLastDescriptorDetails(); |
| 2880 | |
| 2881 | if (old_map->unused_property_fields() > 0) { |
| 2882 | if (details.representation().IsDouble()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2883 | FieldIndex index = |
| 2884 | FieldIndex::ForDescriptor(*new_map, new_map->LastAdded()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2885 | if (new_map->IsUnboxedDoubleField(index)) { |
| 2886 | object->RawFastDoublePropertyAtPut(index, 0); |
| 2887 | } else { |
| 2888 | Handle<Object> value = isolate->factory()->NewHeapNumber(0, MUTABLE); |
| 2889 | object->RawFastPropertyAtPut(index, *value); |
| 2890 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2891 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2892 | object->synchronized_set_map(*new_map); |
| 2893 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2894 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2895 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2896 | DCHECK(number_of_fields == old_number_of_fields + 1); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2897 | // This migration is a transition from a map that has run out of property |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2898 | // space. Therefore it could be done by extending the backing store. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2899 | int grow_by = external - object->properties()->length(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2900 | Handle<FixedArray> old_storage = handle(object->properties(), isolate); |
| 2901 | Handle<FixedArray> new_storage = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2902 | isolate->factory()->CopyFixedArrayAndGrow(old_storage, grow_by); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2903 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2904 | // Properly initialize newly added property. |
| 2905 | Handle<Object> value; |
| 2906 | if (details.representation().IsDouble()) { |
| 2907 | value = isolate->factory()->NewHeapNumber(0, MUTABLE); |
| 2908 | } else { |
| 2909 | value = isolate->factory()->uninitialized_value(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2910 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2911 | DCHECK(details.type() == DATA); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2912 | int target_index = details.field_index() - inobject; |
| 2913 | DCHECK(target_index >= 0); // Must be a backing store index. |
| 2914 | new_storage->set(target_index, *value); |
| 2915 | |
| 2916 | // From here on we cannot fail and we shouldn't GC anymore. |
| 2917 | DisallowHeapAllocation no_allocation; |
| 2918 | |
| 2919 | // Set the new property value and do the map transition. |
| 2920 | object->set_properties(*new_storage); |
| 2921 | object->synchronized_set_map(*new_map); |
| 2922 | return; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2923 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2924 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(total_size); |
| 2925 | |
| 2926 | Handle<DescriptorArray> old_descriptors(old_map->instance_descriptors()); |
| 2927 | Handle<DescriptorArray> new_descriptors(new_map->instance_descriptors()); |
| 2928 | int old_nof = old_map->NumberOfOwnDescriptors(); |
| 2929 | int new_nof = new_map->NumberOfOwnDescriptors(); |
| 2930 | |
| 2931 | // This method only supports generalizing instances to at least the same |
| 2932 | // number of properties. |
| 2933 | DCHECK(old_nof <= new_nof); |
| 2934 | |
| 2935 | for (int i = 0; i < old_nof; i++) { |
| 2936 | PropertyDetails details = new_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2937 | if (details.type() != DATA) continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2938 | PropertyDetails old_details = old_descriptors->GetDetails(i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2939 | Representation old_representation = old_details.representation(); |
| 2940 | Representation representation = details.representation(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2941 | Handle<Object> value; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2942 | if (old_details.type() == ACCESSOR_CONSTANT) { |
| 2943 | // In case of kAccessor -> kData property reconfiguration, the property |
| 2944 | // must already be prepared for data or certain type. |
| 2945 | DCHECK(!details.representation().IsNone()); |
| 2946 | if (details.representation().IsDouble()) { |
| 2947 | value = isolate->factory()->NewHeapNumber(0, MUTABLE); |
| 2948 | } else { |
| 2949 | value = isolate->factory()->uninitialized_value(); |
| 2950 | } |
| 2951 | } else if (old_details.type() == DATA_CONSTANT) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2952 | value = handle(old_descriptors->GetValue(i), isolate); |
| 2953 | DCHECK(!old_representation.IsDouble() && !representation.IsDouble()); |
| 2954 | } else { |
| 2955 | FieldIndex index = FieldIndex::ForDescriptor(*old_map, i); |
| 2956 | if (object->IsUnboxedDoubleField(index)) { |
| 2957 | double old = object->RawFastDoublePropertyAt(index); |
| 2958 | value = isolate->factory()->NewHeapNumber( |
| 2959 | old, representation.IsDouble() ? MUTABLE : IMMUTABLE); |
| 2960 | |
| 2961 | } else { |
| 2962 | value = handle(object->RawFastPropertyAt(index), isolate); |
| 2963 | if (!old_representation.IsDouble() && representation.IsDouble()) { |
| 2964 | if (old_representation.IsNone()) { |
| 2965 | value = handle(Smi::FromInt(0), isolate); |
| 2966 | } |
| 2967 | value = Object::NewStorageFor(isolate, value, representation); |
| 2968 | } else if (old_representation.IsDouble() && |
| 2969 | !representation.IsDouble()) { |
| 2970 | value = Object::WrapForRead(isolate, value, old_representation); |
| 2971 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2972 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2973 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2974 | DCHECK(!(representation.IsDouble() && value->IsSmi())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2975 | int target_index = new_descriptors->GetFieldIndex(i) - inobject; |
| 2976 | if (target_index < 0) target_index += total_size; |
| 2977 | array->set(target_index, *value); |
| 2978 | } |
| 2979 | |
| 2980 | for (int i = old_nof; i < new_nof; i++) { |
| 2981 | PropertyDetails details = new_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 2982 | if (details.type() != DATA) continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2983 | Handle<Object> value; |
| 2984 | if (details.representation().IsDouble()) { |
| 2985 | value = isolate->factory()->NewHeapNumber(0, MUTABLE); |
| 2986 | } else { |
| 2987 | value = isolate->factory()->uninitialized_value(); |
| 2988 | } |
| 2989 | int target_index = new_descriptors->GetFieldIndex(i) - inobject; |
| 2990 | if (target_index < 0) target_index += total_size; |
| 2991 | array->set(target_index, *value); |
| 2992 | } |
| 2993 | |
| 2994 | // From here on we cannot fail and we shouldn't GC anymore. |
| 2995 | DisallowHeapAllocation no_allocation; |
| 2996 | |
| 2997 | // Copy (real) inobject properties. If necessary, stop at number_of_fields to |
| 2998 | // avoid overwriting |one_pointer_filler_map|. |
| 2999 | int limit = Min(inobject, number_of_fields); |
| 3000 | for (int i = 0; i < limit; i++) { |
| 3001 | FieldIndex index = FieldIndex::ForPropertyIndex(*new_map, i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3002 | Object* value = array->get(external + i); |
| 3003 | // Can't use JSObject::FastPropertyAtPut() because proper map was not set |
| 3004 | // yet. |
| 3005 | if (new_map->IsUnboxedDoubleField(index)) { |
| 3006 | DCHECK(value->IsMutableHeapNumber()); |
| 3007 | object->RawFastDoublePropertyAtPut(index, |
| 3008 | HeapNumber::cast(value)->value()); |
| 3009 | } else { |
| 3010 | object->RawFastPropertyAtPut(index, value); |
| 3011 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
| 3014 | Heap* heap = isolate->heap(); |
| 3015 | |
| 3016 | // If there are properties in the new backing store, trim it to the correct |
| 3017 | // size and install the backing store into the object. |
| 3018 | if (external > 0) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3019 | heap->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>(*array, inobject); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3020 | object->set_properties(*array); |
| 3021 | } |
| 3022 | |
| 3023 | // Create filler object past the new instance size. |
| 3024 | int new_instance_size = new_map->instance_size(); |
| 3025 | int instance_size_delta = old_map->instance_size() - new_instance_size; |
| 3026 | DCHECK(instance_size_delta >= 0); |
| 3027 | |
| 3028 | if (instance_size_delta > 0) { |
| 3029 | Address address = object->address(); |
| 3030 | heap->CreateFillerObjectAt( |
| 3031 | address + new_instance_size, instance_size_delta); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3032 | heap->AdjustLiveBytes(*object, -instance_size_delta, |
| 3033 | Heap::CONCURRENT_TO_SWEEPER); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | // We are storing the new map using release store after creating a filler for |
| 3037 | // the left-over space to avoid races with the sweeper thread. |
| 3038 | object->synchronized_set_map(*new_map); |
| 3039 | } |
| 3040 | |
| 3041 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3042 | int Map::NumberOfFields() { |
| 3043 | DescriptorArray* descriptors = instance_descriptors(); |
| 3044 | int result = 0; |
| 3045 | for (int i = 0; i < NumberOfOwnDescriptors(); i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3046 | if (descriptors->GetDetails(i).location() == kField) result++; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3047 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3048 | return result; |
| 3049 | } |
| 3050 | |
| 3051 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3052 | Handle<Map> Map::CopyGeneralizeAllRepresentations( |
| 3053 | Handle<Map> map, int modify_index, StoreMode store_mode, PropertyKind kind, |
| 3054 | PropertyAttributes attributes, const char* reason) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3055 | Isolate* isolate = map->GetIsolate(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3056 | Handle<DescriptorArray> old_descriptors(map->instance_descriptors(), isolate); |
| 3057 | int number_of_own_descriptors = map->NumberOfOwnDescriptors(); |
| 3058 | Handle<DescriptorArray> descriptors = |
| 3059 | DescriptorArray::CopyUpTo(old_descriptors, number_of_own_descriptors); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3060 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3061 | for (int i = 0; i < number_of_own_descriptors; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3062 | descriptors->SetRepresentation(i, Representation::Tagged()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3063 | if (descriptors->GetDetails(i).type() == DATA) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3064 | descriptors->SetValue(i, HeapType::Any()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3065 | } |
| 3066 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3067 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3068 | Handle<LayoutDescriptor> new_layout_descriptor( |
| 3069 | LayoutDescriptor::FastPointerLayout(), isolate); |
| 3070 | Handle<Map> new_map = CopyReplaceDescriptors( |
| 3071 | map, descriptors, new_layout_descriptor, OMIT_TRANSITION, |
| 3072 | MaybeHandle<Name>(), reason, SPECIAL_TRANSITION); |
| 3073 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3074 | // Unless the instance is being migrated, ensure that modify_index is a field. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3075 | if (modify_index >= 0) { |
| 3076 | PropertyDetails details = descriptors->GetDetails(modify_index); |
| 3077 | if (store_mode == FORCE_FIELD && |
| 3078 | (details.type() != DATA || details.attributes() != attributes)) { |
| 3079 | int field_index = details.type() == DATA ? details.field_index() |
| 3080 | : new_map->NumberOfFields(); |
| 3081 | DataDescriptor d(handle(descriptors->GetKey(modify_index), isolate), |
| 3082 | field_index, attributes, Representation::Tagged()); |
| 3083 | descriptors->Replace(modify_index, &d); |
| 3084 | if (details.type() != DATA) { |
| 3085 | int unused_property_fields = new_map->unused_property_fields() - 1; |
| 3086 | if (unused_property_fields < 0) { |
| 3087 | unused_property_fields += JSObject::kFieldsAdded; |
| 3088 | } |
| 3089 | new_map->set_unused_property_fields(unused_property_fields); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3090 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3091 | } else { |
| 3092 | DCHECK(details.attributes() == attributes); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3093 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3094 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3095 | if (FLAG_trace_generalization) { |
| 3096 | HeapType* field_type = |
| 3097 | (details.type() == DATA) |
| 3098 | ? map->instance_descriptors()->GetFieldType(modify_index) |
| 3099 | : NULL; |
| 3100 | map->PrintGeneralization( |
| 3101 | stdout, reason, modify_index, new_map->NumberOfOwnDescriptors(), |
| 3102 | new_map->NumberOfOwnDescriptors(), |
| 3103 | details.type() == DATA_CONSTANT && store_mode == FORCE_FIELD, |
| 3104 | details.representation(), Representation::Tagged(), field_type, |
| 3105 | HeapType::Any()); |
| 3106 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3107 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3108 | return new_map; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
| 3111 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3112 | void Map::DeprecateTransitionTree() { |
| 3113 | if (is_deprecated()) return; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3114 | Object* transitions = raw_transitions(); |
| 3115 | int num_transitions = TransitionArray::NumberOfTransitions(transitions); |
| 3116 | for (int i = 0; i < num_transitions; ++i) { |
| 3117 | TransitionArray::GetTarget(transitions, i)->DeprecateTransitionTree(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3118 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3119 | deprecate(); |
| 3120 | dependent_code()->DeoptimizeDependentCodeGroup( |
| 3121 | GetIsolate(), DependentCode::kTransitionGroup); |
| 3122 | NotifyLeafMapLayoutChange(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3126 | static inline bool EqualImmutableValues(Object* obj1, Object* obj2) { |
| 3127 | if (obj1 == obj2) return true; // Valid for both kData and kAccessor kinds. |
| 3128 | // TODO(ishell): compare AccessorPairs. |
| 3129 | return false; |
| 3130 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3131 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3132 | |
| 3133 | // Installs |new_descriptors| over the current instance_descriptors to ensure |
| 3134 | // proper sharing of descriptor arrays. |
| 3135 | void Map::ReplaceDescriptors(DescriptorArray* new_descriptors, |
| 3136 | LayoutDescriptor* new_layout_descriptor) { |
| 3137 | // Don't overwrite the empty descriptor array or initial map's descriptors. |
| 3138 | if (NumberOfOwnDescriptors() == 0 || GetBackPointer()->IsUndefined()) { |
| 3139 | return; |
| 3140 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3141 | |
| 3142 | DescriptorArray* to_replace = instance_descriptors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3143 | GetHeap()->incremental_marking()->RecordWrites(to_replace); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3144 | Map* current = this; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3145 | while (current->instance_descriptors() == to_replace) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3146 | Object* next = current->GetBackPointer(); |
| 3147 | if (next->IsUndefined()) break; // Stop overwriting at initial map. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3148 | current->SetEnumLength(kInvalidEnumCacheSentinel); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3149 | current->UpdateDescriptors(new_descriptors, new_layout_descriptor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3150 | current = Map::cast(next); |
| 3151 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3152 | set_owns_descriptors(false); |
| 3153 | } |
| 3154 | |
| 3155 | |
| 3156 | Map* Map::FindRootMap() { |
| 3157 | Map* result = this; |
| 3158 | while (true) { |
| 3159 | Object* back = result->GetBackPointer(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3160 | if (back->IsUndefined()) { |
| 3161 | // Initial map always owns descriptors and doesn't have unused entries |
| 3162 | // in the descriptor array. |
| 3163 | DCHECK(result->owns_descriptors()); |
| 3164 | DCHECK_EQ(result->NumberOfOwnDescriptors(), |
| 3165 | result->instance_descriptors()->number_of_descriptors()); |
| 3166 | return result; |
| 3167 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3168 | result = Map::cast(back); |
| 3169 | } |
| 3170 | } |
| 3171 | |
| 3172 | |
| 3173 | Map* Map::FindLastMatchMap(int verbatim, |
| 3174 | int length, |
| 3175 | DescriptorArray* descriptors) { |
| 3176 | DisallowHeapAllocation no_allocation; |
| 3177 | |
| 3178 | // This can only be called on roots of transition trees. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3179 | DCHECK_EQ(verbatim, NumberOfOwnDescriptors()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3180 | |
| 3181 | Map* current = this; |
| 3182 | |
| 3183 | for (int i = verbatim; i < length; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3184 | Name* name = descriptors->GetKey(i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3185 | PropertyDetails details = descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3186 | Map* next = TransitionArray::SearchTransition(current, details.kind(), name, |
| 3187 | details.attributes()); |
| 3188 | if (next == NULL) break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3189 | DescriptorArray* next_descriptors = next->instance_descriptors(); |
| 3190 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3191 | PropertyDetails next_details = next_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3192 | DCHECK_EQ(details.kind(), next_details.kind()); |
| 3193 | DCHECK_EQ(details.attributes(), next_details.attributes()); |
| 3194 | if (details.location() != next_details.location()) break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3195 | if (!details.representation().Equals(next_details.representation())) break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3196 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3197 | if (next_details.location() == kField) { |
| 3198 | HeapType* next_field_type = next_descriptors->GetFieldType(i); |
| 3199 | if (!descriptors->GetFieldType(i)->NowIs(next_field_type)) { |
| 3200 | break; |
| 3201 | } |
| 3202 | } else { |
| 3203 | if (!EqualImmutableValues(descriptors->GetValue(i), |
| 3204 | next_descriptors->GetValue(i))) { |
| 3205 | break; |
| 3206 | } |
| 3207 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3208 | current = next; |
| 3209 | } |
| 3210 | return current; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3214 | Map* Map::FindFieldOwner(int descriptor) { |
| 3215 | DisallowHeapAllocation no_allocation; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3216 | DCHECK_EQ(DATA, instance_descriptors()->GetDetails(descriptor).type()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3217 | Map* result = this; |
| 3218 | while (true) { |
| 3219 | Object* back = result->GetBackPointer(); |
| 3220 | if (back->IsUndefined()) break; |
| 3221 | Map* parent = Map::cast(back); |
| 3222 | if (parent->NumberOfOwnDescriptors() <= descriptor) break; |
| 3223 | result = parent; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3224 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3225 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3229 | void Map::UpdateFieldType(int descriptor, Handle<Name> name, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3230 | Representation new_representation, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3231 | Handle<Object> new_wrapped_type) { |
| 3232 | DCHECK(new_wrapped_type->IsSmi() || new_wrapped_type->IsWeakCell()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3233 | DisallowHeapAllocation no_allocation; |
| 3234 | PropertyDetails details = instance_descriptors()->GetDetails(descriptor); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3235 | if (details.type() != DATA) return; |
| 3236 | Object* transitions = raw_transitions(); |
| 3237 | int num_transitions = TransitionArray::NumberOfTransitions(transitions); |
| 3238 | for (int i = 0; i < num_transitions; ++i) { |
| 3239 | Map* target = TransitionArray::GetTarget(transitions, i); |
| 3240 | target->UpdateFieldType(descriptor, name, new_representation, |
| 3241 | new_wrapped_type); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3242 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3243 | // It is allowed to change representation here only from None to something. |
| 3244 | DCHECK(details.representation().Equals(new_representation) || |
| 3245 | details.representation().IsNone()); |
| 3246 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3247 | // Skip if already updated the shared descriptor. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3248 | if (instance_descriptors()->GetValue(descriptor) == *new_wrapped_type) return; |
| 3249 | DataDescriptor d(name, instance_descriptors()->GetFieldIndex(descriptor), |
| 3250 | new_wrapped_type, details.attributes(), new_representation); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3251 | instance_descriptors()->Replace(descriptor, &d); |
| 3252 | } |
| 3253 | |
| 3254 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3255 | bool FieldTypeIsCleared(Representation rep, HeapType* type) { |
| 3256 | return type->Is(HeapType::None()) && rep.IsHeapObject(); |
| 3257 | } |
| 3258 | |
| 3259 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3260 | // static |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3261 | Handle<HeapType> Map::GeneralizeFieldType(Representation rep1, |
| 3262 | Handle<HeapType> type1, |
| 3263 | Representation rep2, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3264 | Handle<HeapType> type2, |
| 3265 | Isolate* isolate) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3266 | // Cleared field types need special treatment. They represent lost knowledge, |
| 3267 | // so we must be conservative, so their generalization with any other type |
| 3268 | // is "Any". |
| 3269 | if (FieldTypeIsCleared(rep1, *type1) || FieldTypeIsCleared(rep2, *type2)) { |
| 3270 | return HeapType::Any(isolate); |
| 3271 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3272 | if (type1->NowIs(type2)) return type2; |
| 3273 | if (type2->NowIs(type1)) return type1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3274 | return HeapType::Any(isolate); |
| 3275 | } |
| 3276 | |
| 3277 | |
| 3278 | // static |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3279 | void Map::GeneralizeFieldType(Handle<Map> map, int modify_index, |
| 3280 | Representation new_representation, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3281 | Handle<HeapType> new_field_type) { |
| 3282 | Isolate* isolate = map->GetIsolate(); |
| 3283 | |
| 3284 | // Check if we actually need to generalize the field type at all. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3285 | Handle<DescriptorArray> old_descriptors(map->instance_descriptors(), isolate); |
| 3286 | Representation old_representation = |
| 3287 | old_descriptors->GetDetails(modify_index).representation(); |
| 3288 | Handle<HeapType> old_field_type(old_descriptors->GetFieldType(modify_index), |
| 3289 | isolate); |
| 3290 | |
| 3291 | if (old_representation.Equals(new_representation) && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3292 | !FieldTypeIsCleared(new_representation, *new_field_type) && |
| 3293 | // Checking old_field_type for being cleared is not necessary because |
| 3294 | // the NowIs check below would fail anyway in that case. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3295 | new_field_type->NowIs(old_field_type)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3296 | DCHECK(Map::GeneralizeFieldType(old_representation, old_field_type, |
| 3297 | new_representation, new_field_type, isolate) |
| 3298 | ->NowIs(old_field_type)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3299 | return; |
| 3300 | } |
| 3301 | |
| 3302 | // Determine the field owner. |
| 3303 | Handle<Map> field_owner(map->FindFieldOwner(modify_index), isolate); |
| 3304 | Handle<DescriptorArray> descriptors( |
| 3305 | field_owner->instance_descriptors(), isolate); |
| 3306 | DCHECK_EQ(*old_field_type, descriptors->GetFieldType(modify_index)); |
| 3307 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3308 | new_field_type = |
| 3309 | Map::GeneralizeFieldType(old_representation, old_field_type, |
| 3310 | new_representation, new_field_type, isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3311 | |
| 3312 | PropertyDetails details = descriptors->GetDetails(modify_index); |
| 3313 | Handle<Name> name(descriptors->GetKey(modify_index)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3314 | |
| 3315 | Handle<Object> wrapped_type(WrapType(new_field_type)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3316 | field_owner->UpdateFieldType(modify_index, name, new_representation, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3317 | wrapped_type); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3318 | field_owner->dependent_code()->DeoptimizeDependentCodeGroup( |
| 3319 | isolate, DependentCode::kFieldTypeGroup); |
| 3320 | |
| 3321 | if (FLAG_trace_generalization) { |
| 3322 | map->PrintGeneralization( |
| 3323 | stdout, "field type generalization", |
| 3324 | modify_index, map->NumberOfOwnDescriptors(), |
| 3325 | map->NumberOfOwnDescriptors(), false, |
| 3326 | details.representation(), details.representation(), |
| 3327 | *old_field_type, *new_field_type); |
| 3328 | } |
| 3329 | } |
| 3330 | |
| 3331 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3332 | static inline Handle<HeapType> GetFieldType(Isolate* isolate, |
| 3333 | Handle<DescriptorArray> descriptors, |
| 3334 | int descriptor, |
| 3335 | PropertyLocation location, |
| 3336 | Representation representation) { |
| 3337 | #ifdef DEBUG |
| 3338 | PropertyDetails details = descriptors->GetDetails(descriptor); |
| 3339 | DCHECK_EQ(kData, details.kind()); |
| 3340 | DCHECK_EQ(details.location(), location); |
| 3341 | #endif |
| 3342 | if (location == kField) { |
| 3343 | return handle(descriptors->GetFieldType(descriptor), isolate); |
| 3344 | } else { |
| 3345 | return descriptors->GetValue(descriptor) |
| 3346 | ->OptimalType(isolate, representation); |
| 3347 | } |
| 3348 | } |
| 3349 | |
| 3350 | |
| 3351 | // Reconfigures property at |modify_index| with |new_kind|, |new_attributes|, |
| 3352 | // |store_mode| and/or |new_representation|/|new_field_type|. |
| 3353 | // If |modify_index| is negative then no properties are reconfigured but the |
| 3354 | // map is migrated to the up-to-date non-deprecated state. |
| 3355 | // |
| 3356 | // This method rewrites or completes the transition tree to reflect the new |
| 3357 | // change. To avoid high degrees over polymorphism, and to stabilize quickly, |
| 3358 | // on every rewrite the new type is deduced by merging the current type with |
| 3359 | // any potential new (partial) version of the type in the transition tree. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3360 | // To do this, on each rewrite: |
| 3361 | // - Search the root of the transition tree using FindRootMap. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3362 | // - Find |target_map|, the newest matching version of this map using the |
| 3363 | // virtually "enhanced" |old_map|'s descriptor array (i.e. whose entry at |
| 3364 | // |modify_index| is considered to be of |new_kind| and having |
| 3365 | // |new_attributes|) to walk the transition tree. |
| 3366 | // - Merge/generalize the "enhanced" descriptor array of the |old_map| and |
| 3367 | // descriptor array of the |target_map|. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3368 | // - Generalize the |modify_index| descriptor using |new_representation| and |
| 3369 | // |new_field_type|. |
| 3370 | // - Walk the tree again starting from the root towards |target_map|. Stop at |
| 3371 | // |split_map|, the first map who's descriptor array does not match the merged |
| 3372 | // descriptor array. |
| 3373 | // - If |target_map| == |split_map|, |target_map| is in the expected state. |
| 3374 | // Return it. |
| 3375 | // - Otherwise, invalidate the outdated transition target from |target_map|, and |
| 3376 | // replace its transition tree with a new branch for the updated descriptors. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3377 | Handle<Map> Map::ReconfigureProperty(Handle<Map> old_map, int modify_index, |
| 3378 | PropertyKind new_kind, |
| 3379 | PropertyAttributes new_attributes, |
| 3380 | Representation new_representation, |
| 3381 | Handle<HeapType> new_field_type, |
| 3382 | StoreMode store_mode) { |
| 3383 | DCHECK_NE(kAccessor, new_kind); // TODO(ishell): not supported yet. |
| 3384 | DCHECK(store_mode != FORCE_FIELD || modify_index >= 0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3385 | Isolate* isolate = old_map->GetIsolate(); |
| 3386 | |
| 3387 | Handle<DescriptorArray> old_descriptors( |
| 3388 | old_map->instance_descriptors(), isolate); |
| 3389 | int old_nof = old_map->NumberOfOwnDescriptors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3390 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3391 | // If it's just a representation generalization case (i.e. property kind and |
| 3392 | // attributes stays unchanged) it's fine to transition from None to anything |
| 3393 | // but double without any modification to the object, because the default |
| 3394 | // uninitialized value for representation None can be overwritten by both |
| 3395 | // smi and tagged values. Doubles, however, would require a box allocation. |
| 3396 | if (modify_index >= 0 && !new_representation.IsNone() && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3397 | !new_representation.IsDouble()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3398 | PropertyDetails old_details = old_descriptors->GetDetails(modify_index); |
| 3399 | Representation old_representation = old_details.representation(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3400 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3401 | if (old_representation.IsNone()) { |
| 3402 | DCHECK_EQ(new_kind, old_details.kind()); |
| 3403 | DCHECK_EQ(new_attributes, old_details.attributes()); |
| 3404 | DCHECK_EQ(DATA, old_details.type()); |
| 3405 | if (FLAG_trace_generalization) { |
| 3406 | old_map->PrintGeneralization( |
| 3407 | stdout, "uninitialized field", modify_index, |
| 3408 | old_map->NumberOfOwnDescriptors(), |
| 3409 | old_map->NumberOfOwnDescriptors(), false, old_representation, |
| 3410 | new_representation, old_descriptors->GetFieldType(modify_index), |
| 3411 | *new_field_type); |
| 3412 | } |
| 3413 | Handle<Map> field_owner(old_map->FindFieldOwner(modify_index), isolate); |
| 3414 | |
| 3415 | GeneralizeFieldType(field_owner, modify_index, new_representation, |
| 3416 | new_field_type); |
| 3417 | DCHECK(old_descriptors->GetDetails(modify_index) |
| 3418 | .representation() |
| 3419 | .Equals(new_representation)); |
| 3420 | DCHECK( |
| 3421 | old_descriptors->GetFieldType(modify_index)->NowIs(new_field_type)); |
| 3422 | return old_map; |
| 3423 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
| 3426 | // Check the state of the root map. |
| 3427 | Handle<Map> root_map(old_map->FindRootMap(), isolate); |
| 3428 | if (!old_map->EquivalentToForTransition(*root_map)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3429 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3430 | new_kind, new_attributes, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3431 | "GenAll_NotEquivalent"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3432 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3433 | |
| 3434 | ElementsKind from_kind = root_map->elements_kind(); |
| 3435 | ElementsKind to_kind = old_map->elements_kind(); |
| 3436 | // TODO(ishell): Add a test for SLOW_SLOPPY_ARGUMENTS_ELEMENTS. |
| 3437 | if (from_kind != to_kind && to_kind != DICTIONARY_ELEMENTS && |
| 3438 | to_kind != SLOW_SLOPPY_ARGUMENTS_ELEMENTS && |
| 3439 | !(IsTransitionableFastElementsKind(from_kind) && |
| 3440 | IsMoreGeneralElementsKindTransition(from_kind, to_kind))) { |
| 3441 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
| 3442 | new_kind, new_attributes, |
| 3443 | "GenAll_InvalidElementsTransition"); |
| 3444 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3445 | int root_nof = root_map->NumberOfOwnDescriptors(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3446 | if (modify_index >= 0 && modify_index < root_nof) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3447 | PropertyDetails old_details = old_descriptors->GetDetails(modify_index); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3448 | if (old_details.kind() != new_kind || |
| 3449 | old_details.attributes() != new_attributes) { |
| 3450 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
| 3451 | new_kind, new_attributes, |
| 3452 | "GenAll_RootModification1"); |
| 3453 | } |
| 3454 | if ((old_details.type() != DATA && store_mode == FORCE_FIELD) || |
| 3455 | (old_details.type() == DATA && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3456 | (!new_field_type->NowIs(old_descriptors->GetFieldType(modify_index)) || |
| 3457 | !new_representation.fits_into(old_details.representation())))) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3458 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3459 | new_kind, new_attributes, |
| 3460 | "GenAll_RootModification2"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3461 | } |
| 3462 | } |
| 3463 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3464 | // From here on, use the map with correct elements kind as root map. |
| 3465 | if (from_kind != to_kind) { |
| 3466 | root_map = Map::AsElementsKind(root_map, to_kind); |
| 3467 | } |
| 3468 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3469 | Handle<Map> target_map = root_map; |
| 3470 | for (int i = root_nof; i < old_nof; ++i) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3471 | PropertyDetails old_details = old_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3472 | PropertyKind next_kind; |
| 3473 | PropertyLocation next_location; |
| 3474 | PropertyAttributes next_attributes; |
| 3475 | Representation next_representation; |
| 3476 | bool property_kind_reconfiguration = false; |
| 3477 | |
| 3478 | if (modify_index == i) { |
| 3479 | DCHECK_EQ(FORCE_FIELD, store_mode); |
| 3480 | property_kind_reconfiguration = old_details.kind() != new_kind; |
| 3481 | |
| 3482 | next_kind = new_kind; |
| 3483 | next_location = kField; |
| 3484 | next_attributes = new_attributes; |
| 3485 | // If property kind is not reconfigured merge the result with |
| 3486 | // representation/field type from the old descriptor. |
| 3487 | next_representation = new_representation; |
| 3488 | if (!property_kind_reconfiguration) { |
| 3489 | next_representation = |
| 3490 | next_representation.generalize(old_details.representation()); |
| 3491 | } |
| 3492 | |
| 3493 | } else { |
| 3494 | next_kind = old_details.kind(); |
| 3495 | next_location = old_details.location(); |
| 3496 | next_attributes = old_details.attributes(); |
| 3497 | next_representation = old_details.representation(); |
| 3498 | } |
| 3499 | Map* transition = TransitionArray::SearchTransition( |
| 3500 | *target_map, next_kind, old_descriptors->GetKey(i), next_attributes); |
| 3501 | if (transition == NULL) break; |
| 3502 | Handle<Map> tmp_map(transition, isolate); |
| 3503 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3504 | Handle<DescriptorArray> tmp_descriptors = handle( |
| 3505 | tmp_map->instance_descriptors(), isolate); |
| 3506 | |
| 3507 | // Check if target map is incompatible. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3508 | PropertyDetails tmp_details = tmp_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3509 | DCHECK_EQ(next_kind, tmp_details.kind()); |
| 3510 | DCHECK_EQ(next_attributes, tmp_details.attributes()); |
| 3511 | if (next_kind == kAccessor && |
| 3512 | !EqualImmutableValues(old_descriptors->GetValue(i), |
| 3513 | tmp_descriptors->GetValue(i))) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3514 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3515 | new_kind, new_attributes, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3516 | "GenAll_Incompatible"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3517 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3518 | if (next_location == kField && tmp_details.location() == kDescriptor) break; |
| 3519 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3520 | Representation tmp_representation = tmp_details.representation(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3521 | if (!next_representation.fits_into(tmp_representation)) break; |
| 3522 | |
| 3523 | PropertyLocation old_location = old_details.location(); |
| 3524 | PropertyLocation tmp_location = tmp_details.location(); |
| 3525 | if (tmp_location == kField) { |
| 3526 | if (next_kind == kData) { |
| 3527 | Handle<HeapType> next_field_type; |
| 3528 | if (modify_index == i) { |
| 3529 | next_field_type = new_field_type; |
| 3530 | if (!property_kind_reconfiguration) { |
| 3531 | Handle<HeapType> old_field_type = |
| 3532 | GetFieldType(isolate, old_descriptors, i, |
| 3533 | old_details.location(), tmp_representation); |
| 3534 | Representation old_representation = old_details.representation(); |
| 3535 | next_field_type = GeneralizeFieldType( |
| 3536 | old_representation, old_field_type, new_representation, |
| 3537 | next_field_type, isolate); |
| 3538 | } |
| 3539 | } else { |
| 3540 | Handle<HeapType> old_field_type = |
| 3541 | GetFieldType(isolate, old_descriptors, i, old_details.location(), |
| 3542 | tmp_representation); |
| 3543 | next_field_type = old_field_type; |
| 3544 | } |
| 3545 | GeneralizeFieldType(tmp_map, i, tmp_representation, next_field_type); |
| 3546 | } |
| 3547 | } else if (old_location == kField || |
| 3548 | !EqualImmutableValues(old_descriptors->GetValue(i), |
| 3549 | tmp_descriptors->GetValue(i))) { |
Ben Murdoch | 7d3e7fc | 2011-07-12 16:37:06 +0100 | [diff] [blame] | 3550 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3551 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3552 | DCHECK(!tmp_map->is_deprecated()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3553 | target_map = tmp_map; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3554 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3555 | |
| 3556 | // Directly change the map if the target map is more general. |
| 3557 | Handle<DescriptorArray> target_descriptors( |
| 3558 | target_map->instance_descriptors(), isolate); |
| 3559 | int target_nof = target_map->NumberOfOwnDescriptors(); |
| 3560 | if (target_nof == old_nof && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3561 | (store_mode != FORCE_FIELD || |
| 3562 | (modify_index >= 0 && |
| 3563 | target_descriptors->GetDetails(modify_index).location() == kField))) { |
| 3564 | #ifdef DEBUG |
| 3565 | if (modify_index >= 0) { |
| 3566 | PropertyDetails details = target_descriptors->GetDetails(modify_index); |
| 3567 | DCHECK_EQ(new_kind, details.kind()); |
| 3568 | DCHECK_EQ(new_attributes, details.attributes()); |
| 3569 | DCHECK(new_representation.fits_into(details.representation())); |
| 3570 | DCHECK(details.location() != kField || |
| 3571 | new_field_type->NowIs( |
| 3572 | target_descriptors->GetFieldType(modify_index))); |
| 3573 | } |
| 3574 | #endif |
| 3575 | if (*target_map != *old_map) { |
| 3576 | old_map->NotifyLeafMapLayoutChange(); |
| 3577 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3578 | return target_map; |
| 3579 | } |
| 3580 | |
| 3581 | // Find the last compatible target map in the transition tree. |
| 3582 | for (int i = target_nof; i < old_nof; ++i) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3583 | PropertyDetails old_details = old_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3584 | PropertyKind next_kind; |
| 3585 | PropertyAttributes next_attributes; |
| 3586 | if (modify_index == i) { |
| 3587 | next_kind = new_kind; |
| 3588 | next_attributes = new_attributes; |
| 3589 | } else { |
| 3590 | next_kind = old_details.kind(); |
| 3591 | next_attributes = old_details.attributes(); |
| 3592 | } |
| 3593 | Map* transition = TransitionArray::SearchTransition( |
| 3594 | *target_map, next_kind, old_descriptors->GetKey(i), next_attributes); |
| 3595 | if (transition == NULL) break; |
| 3596 | Handle<Map> tmp_map(transition, isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3597 | Handle<DescriptorArray> tmp_descriptors( |
| 3598 | tmp_map->instance_descriptors(), isolate); |
| 3599 | |
| 3600 | // Check if target map is compatible. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3601 | #ifdef DEBUG |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3602 | PropertyDetails tmp_details = tmp_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3603 | DCHECK_EQ(next_kind, tmp_details.kind()); |
| 3604 | DCHECK_EQ(next_attributes, tmp_details.attributes()); |
| 3605 | #endif |
| 3606 | if (next_kind == kAccessor && |
| 3607 | !EqualImmutableValues(old_descriptors->GetValue(i), |
| 3608 | tmp_descriptors->GetValue(i))) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3609 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3610 | new_kind, new_attributes, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3611 | "GenAll_Incompatible"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3612 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3613 | DCHECK(!tmp_map->is_deprecated()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3614 | target_map = tmp_map; |
| 3615 | } |
| 3616 | target_nof = target_map->NumberOfOwnDescriptors(); |
| 3617 | target_descriptors = handle(target_map->instance_descriptors(), isolate); |
| 3618 | |
| 3619 | // Allocate a new descriptor array large enough to hold the required |
| 3620 | // descriptors, with minimally the exact same size as the old descriptor |
| 3621 | // array. |
| 3622 | int new_slack = Max( |
| 3623 | old_nof, old_descriptors->number_of_descriptors()) - old_nof; |
| 3624 | Handle<DescriptorArray> new_descriptors = DescriptorArray::Allocate( |
| 3625 | isolate, old_nof, new_slack); |
| 3626 | DCHECK(new_descriptors->length() > target_descriptors->length() || |
| 3627 | new_descriptors->NumberOfSlackDescriptors() > 0 || |
| 3628 | new_descriptors->number_of_descriptors() == |
| 3629 | old_descriptors->number_of_descriptors()); |
| 3630 | DCHECK(new_descriptors->number_of_descriptors() == old_nof); |
| 3631 | |
| 3632 | // 0 -> |root_nof| |
| 3633 | int current_offset = 0; |
| 3634 | for (int i = 0; i < root_nof; ++i) { |
| 3635 | PropertyDetails old_details = old_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3636 | if (old_details.location() == kField) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3637 | current_offset += old_details.field_width_in_words(); |
| 3638 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3639 | Descriptor d(handle(old_descriptors->GetKey(i), isolate), |
| 3640 | handle(old_descriptors->GetValue(i), isolate), |
| 3641 | old_details); |
| 3642 | new_descriptors->Set(i, &d); |
| 3643 | } |
| 3644 | |
| 3645 | // |root_nof| -> |target_nof| |
| 3646 | for (int i = root_nof; i < target_nof; ++i) { |
| 3647 | Handle<Name> target_key(target_descriptors->GetKey(i), isolate); |
| 3648 | PropertyDetails old_details = old_descriptors->GetDetails(i); |
| 3649 | PropertyDetails target_details = target_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3650 | |
| 3651 | PropertyKind next_kind; |
| 3652 | PropertyAttributes next_attributes; |
| 3653 | PropertyLocation next_location; |
| 3654 | Representation next_representation; |
| 3655 | bool property_kind_reconfiguration = false; |
| 3656 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3657 | if (modify_index == i) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3658 | DCHECK_EQ(FORCE_FIELD, store_mode); |
| 3659 | property_kind_reconfiguration = old_details.kind() != new_kind; |
| 3660 | |
| 3661 | next_kind = new_kind; |
| 3662 | next_attributes = new_attributes; |
| 3663 | next_location = kField; |
| 3664 | |
| 3665 | // Merge new representation/field type with ones from the target |
| 3666 | // descriptor. If property kind is not reconfigured merge the result with |
| 3667 | // representation/field type from the old descriptor. |
| 3668 | next_representation = |
| 3669 | new_representation.generalize(target_details.representation()); |
| 3670 | if (!property_kind_reconfiguration) { |
| 3671 | next_representation = |
| 3672 | next_representation.generalize(old_details.representation()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3673 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3674 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3675 | // Merge old_descriptor and target_descriptor entries. |
| 3676 | DCHECK_EQ(target_details.kind(), old_details.kind()); |
| 3677 | next_kind = target_details.kind(); |
| 3678 | next_attributes = target_details.attributes(); |
| 3679 | next_location = |
| 3680 | old_details.location() == kField || |
| 3681 | target_details.location() == kField || |
| 3682 | !EqualImmutableValues(target_descriptors->GetValue(i), |
| 3683 | old_descriptors->GetValue(i)) |
| 3684 | ? kField |
| 3685 | : kDescriptor; |
| 3686 | |
| 3687 | next_representation = old_details.representation().generalize( |
| 3688 | target_details.representation()); |
| 3689 | } |
| 3690 | DCHECK_EQ(next_kind, target_details.kind()); |
| 3691 | DCHECK_EQ(next_attributes, target_details.attributes()); |
| 3692 | |
| 3693 | if (next_location == kField) { |
| 3694 | if (next_kind == kData) { |
| 3695 | Handle<HeapType> target_field_type = |
| 3696 | GetFieldType(isolate, target_descriptors, i, |
| 3697 | target_details.location(), next_representation); |
| 3698 | |
| 3699 | Handle<HeapType> next_field_type; |
| 3700 | if (modify_index == i) { |
| 3701 | next_field_type = GeneralizeFieldType( |
| 3702 | target_details.representation(), target_field_type, |
| 3703 | new_representation, new_field_type, isolate); |
| 3704 | if (!property_kind_reconfiguration) { |
| 3705 | Handle<HeapType> old_field_type = |
| 3706 | GetFieldType(isolate, old_descriptors, i, |
| 3707 | old_details.location(), next_representation); |
| 3708 | next_field_type = GeneralizeFieldType( |
| 3709 | old_details.representation(), old_field_type, |
| 3710 | next_representation, next_field_type, isolate); |
| 3711 | } |
| 3712 | } else { |
| 3713 | Handle<HeapType> old_field_type = |
| 3714 | GetFieldType(isolate, old_descriptors, i, old_details.location(), |
| 3715 | next_representation); |
| 3716 | next_field_type = GeneralizeFieldType( |
| 3717 | old_details.representation(), old_field_type, next_representation, |
| 3718 | target_field_type, isolate); |
| 3719 | } |
| 3720 | Handle<Object> wrapped_type(WrapType(next_field_type)); |
| 3721 | DataDescriptor d(target_key, current_offset, wrapped_type, |
| 3722 | next_attributes, next_representation); |
| 3723 | current_offset += d.GetDetails().field_width_in_words(); |
| 3724 | new_descriptors->Set(i, &d); |
| 3725 | } else { |
| 3726 | UNIMPLEMENTED(); // TODO(ishell): implement. |
| 3727 | } |
| 3728 | } else { |
| 3729 | PropertyDetails details(next_attributes, next_kind, next_location, |
| 3730 | next_representation); |
| 3731 | Descriptor d(target_key, handle(target_descriptors->GetValue(i), isolate), |
| 3732 | details); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3733 | new_descriptors->Set(i, &d); |
| 3734 | } |
| 3735 | } |
| 3736 | |
| 3737 | // |target_nof| -> |old_nof| |
| 3738 | for (int i = target_nof; i < old_nof; ++i) { |
| 3739 | PropertyDetails old_details = old_descriptors->GetDetails(i); |
| 3740 | Handle<Name> old_key(old_descriptors->GetKey(i), isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3741 | |
| 3742 | // Merge old_descriptor entry and modified details together. |
| 3743 | PropertyKind next_kind; |
| 3744 | PropertyAttributes next_attributes; |
| 3745 | PropertyLocation next_location; |
| 3746 | Representation next_representation; |
| 3747 | bool property_kind_reconfiguration = false; |
| 3748 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3749 | if (modify_index == i) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3750 | DCHECK_EQ(FORCE_FIELD, store_mode); |
| 3751 | // In case of property kind reconfiguration it is not necessary to |
| 3752 | // take into account representation/field type of the old descriptor. |
| 3753 | property_kind_reconfiguration = old_details.kind() != new_kind; |
| 3754 | |
| 3755 | next_kind = new_kind; |
| 3756 | next_attributes = new_attributes; |
| 3757 | next_location = kField; |
| 3758 | next_representation = new_representation; |
| 3759 | if (!property_kind_reconfiguration) { |
| 3760 | next_representation = |
| 3761 | next_representation.generalize(old_details.representation()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3762 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3763 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3764 | next_kind = old_details.kind(); |
| 3765 | next_attributes = old_details.attributes(); |
| 3766 | next_location = old_details.location(); |
| 3767 | next_representation = old_details.representation(); |
| 3768 | } |
| 3769 | |
| 3770 | if (next_location == kField) { |
| 3771 | if (next_kind == kData) { |
| 3772 | Handle<HeapType> next_field_type; |
| 3773 | if (modify_index == i) { |
| 3774 | next_field_type = new_field_type; |
| 3775 | if (!property_kind_reconfiguration) { |
| 3776 | Handle<HeapType> old_field_type = |
| 3777 | GetFieldType(isolate, old_descriptors, i, |
| 3778 | old_details.location(), next_representation); |
| 3779 | next_field_type = GeneralizeFieldType( |
| 3780 | old_details.representation(), old_field_type, |
| 3781 | next_representation, next_field_type, isolate); |
| 3782 | } |
| 3783 | } else { |
| 3784 | Handle<HeapType> old_field_type = |
| 3785 | GetFieldType(isolate, old_descriptors, i, old_details.location(), |
| 3786 | next_representation); |
| 3787 | next_field_type = old_field_type; |
| 3788 | } |
| 3789 | |
| 3790 | Handle<Object> wrapped_type(WrapType(next_field_type)); |
| 3791 | |
| 3792 | DataDescriptor d(old_key, current_offset, wrapped_type, next_attributes, |
| 3793 | next_representation); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3794 | current_offset += d.GetDetails().field_width_in_words(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3795 | new_descriptors->Set(i, &d); |
| 3796 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3797 | UNIMPLEMENTED(); // TODO(ishell): implement. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3798 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3799 | } else { |
| 3800 | PropertyDetails details(next_attributes, next_kind, next_location, |
| 3801 | next_representation); |
| 3802 | Descriptor d(old_key, handle(old_descriptors->GetValue(i), isolate), |
| 3803 | details); |
| 3804 | new_descriptors->Set(i, &d); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3805 | } |
| 3806 | } |
| 3807 | |
| 3808 | new_descriptors->Sort(); |
| 3809 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3810 | DCHECK(store_mode != FORCE_FIELD || |
| 3811 | new_descriptors->GetDetails(modify_index).location() == kField); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3812 | |
| 3813 | Handle<Map> split_map(root_map->FindLastMatchMap( |
| 3814 | root_nof, old_nof, *new_descriptors), isolate); |
| 3815 | int split_nof = split_map->NumberOfOwnDescriptors(); |
| 3816 | DCHECK_NE(old_nof, split_nof); |
| 3817 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3818 | PropertyKind split_kind; |
| 3819 | PropertyAttributes split_attributes; |
| 3820 | if (modify_index == split_nof) { |
| 3821 | split_kind = new_kind; |
| 3822 | split_attributes = new_attributes; |
| 3823 | } else { |
| 3824 | PropertyDetails split_prop_details = old_descriptors->GetDetails(split_nof); |
| 3825 | split_kind = split_prop_details.kind(); |
| 3826 | split_attributes = split_prop_details.attributes(); |
| 3827 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3828 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3829 | // Invalidate a transition target at |key|. |
| 3830 | Map* maybe_transition = TransitionArray::SearchTransition( |
| 3831 | *split_map, split_kind, old_descriptors->GetKey(split_nof), |
| 3832 | split_attributes); |
| 3833 | if (maybe_transition != NULL) { |
| 3834 | maybe_transition->DeprecateTransitionTree(); |
| 3835 | } |
| 3836 | |
| 3837 | // If |maybe_transition| is not NULL then the transition array already |
| 3838 | // contains entry for given descriptor. This means that the transition |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3839 | // could be inserted regardless of whether transitions array is full or not. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3840 | if (maybe_transition == NULL && |
| 3841 | !TransitionArray::CanHaveMoreTransitions(split_map)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3842 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3843 | new_kind, new_attributes, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3844 | "GenAll_CantHaveMoreTransitions"); |
| 3845 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3846 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3847 | old_map->NotifyLeafMapLayoutChange(); |
| 3848 | |
| 3849 | if (FLAG_trace_generalization && modify_index >= 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3850 | PropertyDetails old_details = old_descriptors->GetDetails(modify_index); |
| 3851 | PropertyDetails new_details = new_descriptors->GetDetails(modify_index); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3852 | Handle<HeapType> old_field_type = |
| 3853 | (old_details.type() == DATA) |
| 3854 | ? handle(old_descriptors->GetFieldType(modify_index), isolate) |
| 3855 | : HeapType::Constant( |
| 3856 | handle(old_descriptors->GetValue(modify_index), isolate), |
| 3857 | isolate); |
| 3858 | Handle<HeapType> new_field_type = |
| 3859 | (new_details.type() == DATA) |
| 3860 | ? handle(new_descriptors->GetFieldType(modify_index), isolate) |
| 3861 | : HeapType::Constant( |
| 3862 | handle(new_descriptors->GetValue(modify_index), isolate), |
| 3863 | isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3864 | old_map->PrintGeneralization( |
| 3865 | stdout, "", modify_index, split_nof, old_nof, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3866 | old_details.location() == kDescriptor && store_mode == FORCE_FIELD, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3867 | old_details.representation(), new_details.representation(), |
| 3868 | *old_field_type, *new_field_type); |
| 3869 | } |
| 3870 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3871 | Handle<LayoutDescriptor> new_layout_descriptor = |
| 3872 | LayoutDescriptor::New(split_map, new_descriptors, old_nof); |
| 3873 | |
| 3874 | Handle<Map> new_map = |
| 3875 | AddMissingTransitions(split_map, new_descriptors, new_layout_descriptor); |
| 3876 | |
| 3877 | // Deprecated part of the transition tree is no longer reachable, so replace |
| 3878 | // current instance descriptors in the "survived" part of the tree with |
| 3879 | // the new descriptors to maintain descriptors sharing invariant. |
| 3880 | split_map->ReplaceDescriptors(*new_descriptors, *new_layout_descriptor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3881 | return new_map; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
| 3884 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3885 | // Generalize the representation of all DATA descriptors. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3886 | Handle<Map> Map::GeneralizeAllFieldRepresentations( |
| 3887 | Handle<Map> map) { |
| 3888 | Handle<DescriptorArray> descriptors(map->instance_descriptors()); |
| 3889 | for (int i = 0; i < map->NumberOfOwnDescriptors(); ++i) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3890 | PropertyDetails details = descriptors->GetDetails(i); |
| 3891 | if (details.type() == DATA) { |
| 3892 | map = ReconfigureProperty(map, i, kData, details.attributes(), |
| 3893 | Representation::Tagged(), |
| 3894 | HeapType::Any(map->GetIsolate()), FORCE_FIELD); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3895 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3896 | } |
| 3897 | return map; |
| 3898 | } |
| 3899 | |
| 3900 | |
| 3901 | // static |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3902 | MaybeHandle<Map> Map::TryUpdate(Handle<Map> old_map) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3903 | DisallowHeapAllocation no_allocation; |
| 3904 | DisallowDeoptimization no_deoptimization(old_map->GetIsolate()); |
| 3905 | |
| 3906 | if (!old_map->is_deprecated()) return old_map; |
| 3907 | |
| 3908 | // Check the state of the root map. |
| 3909 | Map* root_map = old_map->FindRootMap(); |
| 3910 | if (!old_map->EquivalentToForTransition(root_map)) return MaybeHandle<Map>(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3911 | |
| 3912 | ElementsKind from_kind = root_map->elements_kind(); |
| 3913 | ElementsKind to_kind = old_map->elements_kind(); |
| 3914 | if (from_kind != to_kind) { |
| 3915 | // Try to follow existing elements kind transitions. |
| 3916 | root_map = root_map->LookupElementsTransitionMap(to_kind); |
| 3917 | if (root_map == NULL) return MaybeHandle<Map>(); |
| 3918 | // From here on, use the map with correct elements kind as root map. |
| 3919 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3920 | int root_nof = root_map->NumberOfOwnDescriptors(); |
| 3921 | |
| 3922 | int old_nof = old_map->NumberOfOwnDescriptors(); |
| 3923 | DescriptorArray* old_descriptors = old_map->instance_descriptors(); |
| 3924 | |
| 3925 | Map* new_map = root_map; |
| 3926 | for (int i = root_nof; i < old_nof; ++i) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3927 | PropertyDetails old_details = old_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3928 | Map* transition = TransitionArray::SearchTransition( |
| 3929 | new_map, old_details.kind(), old_descriptors->GetKey(i), |
| 3930 | old_details.attributes()); |
| 3931 | if (transition == NULL) return MaybeHandle<Map>(); |
| 3932 | new_map = transition; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3933 | DescriptorArray* new_descriptors = new_map->instance_descriptors(); |
| 3934 | |
| 3935 | PropertyDetails new_details = new_descriptors->GetDetails(i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3936 | DCHECK_EQ(old_details.kind(), new_details.kind()); |
| 3937 | DCHECK_EQ(old_details.attributes(), new_details.attributes()); |
| 3938 | if (!old_details.representation().fits_into(new_details.representation())) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3939 | return MaybeHandle<Map>(); |
| 3940 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3941 | switch (new_details.type()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3942 | case DATA: { |
| 3943 | HeapType* new_type = new_descriptors->GetFieldType(i); |
| 3944 | // Cleared field types need special treatment. They represent lost |
| 3945 | // knowledge, so we must first generalize the new_type to "Any". |
| 3946 | if (FieldTypeIsCleared(new_details.representation(), new_type)) { |
| 3947 | return MaybeHandle<Map>(); |
| 3948 | } |
| 3949 | PropertyType old_property_type = old_details.type(); |
| 3950 | if (old_property_type == DATA) { |
| 3951 | HeapType* old_type = old_descriptors->GetFieldType(i); |
| 3952 | if (FieldTypeIsCleared(old_details.representation(), old_type) || |
| 3953 | !old_type->NowIs(new_type)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3954 | return MaybeHandle<Map>(); |
| 3955 | } |
| 3956 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3957 | DCHECK(old_property_type == DATA_CONSTANT); |
| 3958 | Object* old_value = old_descriptors->GetValue(i); |
| 3959 | if (!new_type->NowContains(old_value)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3960 | return MaybeHandle<Map>(); |
| 3961 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3962 | } |
| 3963 | break; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3964 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3965 | case ACCESSOR: { |
| 3966 | #ifdef DEBUG |
| 3967 | HeapType* new_type = new_descriptors->GetFieldType(i); |
| 3968 | DCHECK(HeapType::Any()->Is(new_type)); |
| 3969 | #endif |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3970 | break; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3971 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3972 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3973 | case DATA_CONSTANT: |
| 3974 | case ACCESSOR_CONSTANT: { |
| 3975 | Object* old_value = old_descriptors->GetValue(i); |
| 3976 | Object* new_value = new_descriptors->GetValue(i); |
| 3977 | if (old_details.location() == kField || old_value != new_value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3978 | return MaybeHandle<Map>(); |
| 3979 | } |
| 3980 | break; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3981 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3982 | } |
| 3983 | } |
| 3984 | if (new_map->NumberOfOwnDescriptors() != old_nof) return MaybeHandle<Map>(); |
| 3985 | return handle(new_map); |
| 3986 | } |
| 3987 | |
| 3988 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 3989 | // static |
| 3990 | Handle<Map> Map::Update(Handle<Map> map) { |
| 3991 | if (!map->is_deprecated()) return map; |
| 3992 | return ReconfigureProperty(map, -1, kData, NONE, Representation::None(), |
| 3993 | HeapType::None(map->GetIsolate()), |
| 3994 | ALLOW_IN_DESCRIPTOR); |
| 3995 | } |
| 3996 | |
| 3997 | |
| 3998 | Maybe<bool> JSObject::SetPropertyWithInterceptor(LookupIterator* it, |
| 3999 | Handle<Object> value) { |
| 4000 | Isolate* isolate = it->isolate(); |
| 4001 | // Make sure that the top context does not change when doing callbacks or |
| 4002 | // interceptor calls. |
| 4003 | AssertNoContextChange ncc(isolate); |
| 4004 | |
| 4005 | DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state()); |
| 4006 | Handle<InterceptorInfo> interceptor(it->GetInterceptor()); |
| 4007 | if (interceptor->setter()->IsUndefined()) return Just(false); |
| 4008 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4009 | Handle<JSObject> holder = it->GetHolder<JSObject>(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4010 | v8::Local<v8::Value> result; |
| 4011 | PropertyCallbackArguments args(isolate, interceptor->data(), |
| 4012 | *it->GetReceiver(), *holder); |
| 4013 | |
| 4014 | if (it->IsElement()) { |
| 4015 | uint32_t index = it->index(); |
| 4016 | v8::IndexedPropertySetterCallback setter = |
| 4017 | v8::ToCData<v8::IndexedPropertySetterCallback>(interceptor->setter()); |
| 4018 | LOG(isolate, |
| 4019 | ApiIndexedPropertyAccess("interceptor-indexed-set", *holder, index)); |
| 4020 | result = args.Call(setter, index, v8::Utils::ToLocal(value)); |
| 4021 | } else { |
| 4022 | Handle<Name> name = it->name(); |
| 4023 | DCHECK(!name->IsPrivate()); |
| 4024 | |
| 4025 | if (name->IsSymbol() && !interceptor->can_intercept_symbols()) { |
| 4026 | return Just(false); |
| 4027 | } |
| 4028 | |
| 4029 | v8::GenericNamedPropertySetterCallback setter = |
| 4030 | v8::ToCData<v8::GenericNamedPropertySetterCallback>( |
| 4031 | interceptor->setter()); |
| 4032 | LOG(it->isolate(), |
| 4033 | ApiNamedPropertyAccess("interceptor-named-set", *holder, *name)); |
| 4034 | result = |
| 4035 | args.Call(setter, v8::Utils::ToLocal(name), v8::Utils::ToLocal(value)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4036 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4037 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4038 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(it->isolate(), Nothing<bool>()); |
| 4039 | if (result.IsEmpty()) return Just(false); |
| 4040 | #ifdef DEBUG |
| 4041 | Handle<Object> result_internal = v8::Utils::OpenHandle(*result); |
| 4042 | result_internal->VerifyApiCallResultType(); |
| 4043 | #endif |
| 4044 | return Just(true); |
| 4045 | // TODO(neis): In the future, we may want to actually return the interceptor's |
| 4046 | // result, which then should be a boolean. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4047 | } |
| 4048 | |
| 4049 | |
| 4050 | MaybeHandle<Object> Object::SetProperty(Handle<Object> object, |
| 4051 | Handle<Name> name, Handle<Object> value, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4052 | LanguageMode language_mode, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4053 | StoreFromKeyed store_mode) { |
| 4054 | LookupIterator it(object, name); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4055 | MAYBE_RETURN_NULL(SetProperty(&it, value, language_mode, store_mode)); |
| 4056 | return value; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4057 | } |
| 4058 | |
| 4059 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4060 | Maybe<bool> Object::SetPropertyInternal(LookupIterator* it, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4061 | Handle<Object> value, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4062 | LanguageMode language_mode, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4063 | StoreFromKeyed store_mode, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4064 | bool* found) { |
| 4065 | ShouldThrow should_throw = |
| 4066 | is_sloppy(language_mode) ? DONT_THROW : THROW_ON_ERROR; |
| 4067 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4068 | // Make sure that the top context does not change when doing callbacks or |
| 4069 | // interceptor calls. |
| 4070 | AssertNoContextChange ncc(it->isolate()); |
| 4071 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4072 | *found = true; |
| 4073 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4074 | bool done = false; |
| 4075 | for (; it->IsFound(); it->Next()) { |
| 4076 | switch (it->state()) { |
| 4077 | case LookupIterator::NOT_FOUND: |
| 4078 | UNREACHABLE(); |
| 4079 | |
| 4080 | case LookupIterator::ACCESS_CHECK: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4081 | if (it->HasAccess()) break; |
| 4082 | // Check whether it makes sense to reuse the lookup iterator. Here it |
| 4083 | // might still call into setters up the prototype chain. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4084 | return JSObject::SetPropertyWithFailedAccessCheck(it, value, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4085 | should_throw); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4086 | |
| 4087 | case LookupIterator::JSPROXY: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4088 | return JSProxy::SetProperty(it->GetHolder<JSProxy>(), it->GetName(), |
| 4089 | value, it->GetReceiver(), language_mode); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4090 | |
| 4091 | case LookupIterator::INTERCEPTOR: |
| 4092 | if (it->HolderIsReceiverOrHiddenPrototype()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4093 | Maybe<bool> result = JSObject::SetPropertyWithInterceptor(it, value); |
| 4094 | if (result.IsNothing() || result.FromJust()) return result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4095 | } else { |
| 4096 | Maybe<PropertyAttributes> maybe_attributes = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4097 | JSObject::GetPropertyAttributesWithInterceptor(it); |
| 4098 | if (!maybe_attributes.IsJust()) return Nothing<bool>(); |
| 4099 | done = maybe_attributes.FromJust() != ABSENT; |
| 4100 | if (done && (maybe_attributes.FromJust() & READ_ONLY) != 0) { |
| 4101 | return WriteToReadOnlyProperty(it, value, should_throw); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4102 | } |
| 4103 | } |
| 4104 | break; |
| 4105 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4106 | case LookupIterator::ACCESSOR: { |
| 4107 | if (it->IsReadOnly()) { |
| 4108 | return WriteToReadOnlyProperty(it, value, should_throw); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4109 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4110 | Handle<Object> accessors = it->GetAccessors(); |
| 4111 | if (accessors->IsAccessorInfo() && |
| 4112 | !it->HolderIsReceiverOrHiddenPrototype() && |
| 4113 | AccessorInfo::cast(*accessors)->is_special_data_property()) { |
| 4114 | done = true; |
| 4115 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4116 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4117 | return SetPropertyWithAccessor(it, value, should_throw); |
| 4118 | } |
| 4119 | case LookupIterator::INTEGER_INDEXED_EXOTIC: |
| 4120 | // TODO(verwaest): We should throw an exception. |
| 4121 | return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4122 | |
| 4123 | case LookupIterator::DATA: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4124 | if (it->IsReadOnly()) { |
| 4125 | return WriteToReadOnlyProperty(it, value, should_throw); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4126 | } |
| 4127 | if (it->HolderIsReceiverOrHiddenPrototype()) { |
| 4128 | return SetDataProperty(it, value); |
| 4129 | } |
| 4130 | done = true; |
| 4131 | break; |
| 4132 | |
| 4133 | case LookupIterator::TRANSITION: |
| 4134 | done = true; |
| 4135 | break; |
| 4136 | } |
| 4137 | |
| 4138 | if (done) break; |
| 4139 | } |
| 4140 | |
| 4141 | // If the receiver is the JSGlobalObject, the store was contextual. In case |
| 4142 | // the property did not exist yet on the global object itself, we have to |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4143 | // throw a reference error in strict mode. In sloppy mode, we continue. |
| 4144 | if (it->GetReceiver()->IsJSGlobalObject() && is_strict(language_mode)) { |
| 4145 | it->isolate()->Throw(*it->isolate()->factory()->NewReferenceError( |
| 4146 | MessageTemplate::kNotDefined, it->name())); |
| 4147 | return Nothing<bool>(); |
| 4148 | } |
| 4149 | |
| 4150 | *found = false; |
| 4151 | return Nothing<bool>(); |
| 4152 | } |
| 4153 | |
| 4154 | |
| 4155 | Maybe<bool> Object::SetProperty(LookupIterator* it, Handle<Object> value, |
| 4156 | LanguageMode language_mode, |
| 4157 | StoreFromKeyed store_mode) { |
| 4158 | ShouldThrow should_throw = |
| 4159 | is_sloppy(language_mode) ? DONT_THROW : THROW_ON_ERROR; |
| 4160 | if (it->GetReceiver()->IsJSProxy() && it->GetName()->IsPrivate()) { |
| 4161 | RETURN_FAILURE(it->isolate(), should_throw, |
| 4162 | NewTypeError(MessageTemplate::kProxyPrivate)); |
| 4163 | } |
| 4164 | bool found = false; |
| 4165 | Maybe<bool> result = |
| 4166 | SetPropertyInternal(it, value, language_mode, store_mode, &found); |
| 4167 | if (found) return result; |
| 4168 | return AddDataProperty(it, value, NONE, should_throw, store_mode); |
| 4169 | } |
| 4170 | |
| 4171 | |
| 4172 | Maybe<bool> Object::SetSuperProperty(LookupIterator* it, Handle<Object> value, |
| 4173 | LanguageMode language_mode, |
| 4174 | StoreFromKeyed store_mode) { |
| 4175 | ShouldThrow should_throw = |
| 4176 | is_sloppy(language_mode) ? DONT_THROW : THROW_ON_ERROR; |
| 4177 | Isolate* isolate = it->isolate(); |
| 4178 | if (it->GetReceiver()->IsJSProxy() && it->GetName()->IsPrivate()) { |
| 4179 | RETURN_FAILURE(isolate, should_throw, |
| 4180 | NewTypeError(MessageTemplate::kProxyPrivate)); |
| 4181 | } |
| 4182 | |
| 4183 | bool found = false; |
| 4184 | Maybe<bool> result = |
| 4185 | SetPropertyInternal(it, value, language_mode, store_mode, &found); |
| 4186 | if (found) return result; |
| 4187 | |
| 4188 | // The property either doesn't exist on the holder or exists there as a data |
| 4189 | // property. |
| 4190 | |
| 4191 | if (!it->GetReceiver()->IsJSReceiver()) { |
| 4192 | return WriteToReadOnlyProperty(it, value, should_throw); |
| 4193 | } |
| 4194 | Handle<JSReceiver> receiver = Handle<JSReceiver>::cast(it->GetReceiver()); |
| 4195 | |
| 4196 | LookupIterator::Configuration c = LookupIterator::OWN; |
| 4197 | LookupIterator own_lookup = |
| 4198 | it->IsElement() ? LookupIterator(isolate, receiver, it->index(), c) |
| 4199 | : LookupIterator(receiver, it->name(), c); |
| 4200 | |
| 4201 | for (; own_lookup.IsFound(); own_lookup.Next()) { |
| 4202 | switch (own_lookup.state()) { |
| 4203 | case LookupIterator::ACCESS_CHECK: |
| 4204 | if (!own_lookup.HasAccess()) { |
| 4205 | return JSObject::SetPropertyWithFailedAccessCheck(&own_lookup, value, |
| 4206 | should_throw); |
| 4207 | } |
| 4208 | break; |
| 4209 | |
| 4210 | case LookupIterator::INTEGER_INDEXED_EXOTIC: |
| 4211 | case LookupIterator::ACCESSOR: |
| 4212 | return RedefineIncompatibleProperty(isolate, it->GetName(), value, |
| 4213 | should_throw); |
| 4214 | |
| 4215 | case LookupIterator::DATA: { |
| 4216 | PropertyDetails details = own_lookup.property_details(); |
| 4217 | if (details.IsReadOnly()) { |
| 4218 | return WriteToReadOnlyProperty(&own_lookup, value, should_throw); |
| 4219 | } |
| 4220 | return SetDataProperty(&own_lookup, value); |
| 4221 | } |
| 4222 | |
| 4223 | case LookupIterator::INTERCEPTOR: |
| 4224 | case LookupIterator::JSPROXY: { |
| 4225 | PropertyDescriptor desc; |
| 4226 | Maybe<bool> owned = |
| 4227 | JSReceiver::GetOwnPropertyDescriptor(&own_lookup, &desc); |
| 4228 | MAYBE_RETURN(owned, Nothing<bool>()); |
| 4229 | if (!owned.FromJust()) { |
| 4230 | return JSReceiver::CreateDataProperty(&own_lookup, value, |
| 4231 | should_throw); |
| 4232 | } |
| 4233 | if (PropertyDescriptor::IsAccessorDescriptor(&desc) || |
| 4234 | !desc.writable()) { |
| 4235 | return RedefineIncompatibleProperty(isolate, it->GetName(), value, |
| 4236 | should_throw); |
| 4237 | } |
| 4238 | |
| 4239 | PropertyDescriptor value_desc; |
| 4240 | value_desc.set_value(value); |
| 4241 | return JSReceiver::DefineOwnProperty(isolate, receiver, it->GetName(), |
| 4242 | &value_desc, should_throw); |
| 4243 | } |
| 4244 | |
| 4245 | case LookupIterator::NOT_FOUND: |
| 4246 | case LookupIterator::TRANSITION: |
| 4247 | UNREACHABLE(); |
| 4248 | } |
| 4249 | } |
| 4250 | |
| 4251 | return JSObject::AddDataProperty(&own_lookup, value, NONE, should_throw, |
| 4252 | store_mode); |
| 4253 | } |
| 4254 | |
| 4255 | |
| 4256 | MaybeHandle<Object> Object::ReadAbsentProperty(LookupIterator* it, |
| 4257 | LanguageMode language_mode) { |
| 4258 | if (is_strong(language_mode)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4259 | THROW_NEW_ERROR(it->isolate(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4260 | NewTypeError(MessageTemplate::kStrongPropertyAccess, |
| 4261 | it->GetName(), it->GetReceiver()), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4262 | Object); |
| 4263 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4264 | return it->isolate()->factory()->undefined_value(); |
| 4265 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4266 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4267 | MaybeHandle<Object> Object::ReadAbsentProperty(Isolate* isolate, |
| 4268 | Handle<Object> receiver, |
| 4269 | Handle<Object> name, |
| 4270 | LanguageMode language_mode) { |
| 4271 | if (is_strong(language_mode)) { |
| 4272 | THROW_NEW_ERROR( |
| 4273 | isolate, |
| 4274 | NewTypeError(MessageTemplate::kStrongPropertyAccess, name, receiver), |
| 4275 | Object); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4276 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4277 | return isolate->factory()->undefined_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4278 | } |
| 4279 | |
| 4280 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4281 | Maybe<bool> Object::CannotCreateProperty(Isolate* isolate, |
| 4282 | Handle<Object> receiver, |
| 4283 | Handle<Object> name, |
| 4284 | Handle<Object> value, |
| 4285 | ShouldThrow should_throw) { |
| 4286 | RETURN_FAILURE( |
| 4287 | isolate, should_throw, |
| 4288 | NewTypeError(MessageTemplate::kStrictCannotCreateProperty, name, |
| 4289 | Object::TypeOf(isolate, receiver), receiver)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4290 | } |
| 4291 | |
| 4292 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4293 | Maybe<bool> Object::WriteToReadOnlyProperty(LookupIterator* it, |
| 4294 | Handle<Object> value, |
| 4295 | ShouldThrow should_throw) { |
| 4296 | return WriteToReadOnlyProperty(it->isolate(), it->GetReceiver(), |
| 4297 | it->GetName(), value, should_throw); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4298 | } |
| 4299 | |
| 4300 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4301 | Maybe<bool> Object::WriteToReadOnlyProperty(Isolate* isolate, |
| 4302 | Handle<Object> receiver, |
| 4303 | Handle<Object> name, |
| 4304 | Handle<Object> value, |
| 4305 | ShouldThrow should_throw) { |
| 4306 | RETURN_FAILURE(isolate, should_throw, |
| 4307 | NewTypeError(MessageTemplate::kStrictReadOnlyProperty, name, |
| 4308 | Object::TypeOf(isolate, receiver), receiver)); |
| 4309 | } |
| 4310 | |
| 4311 | |
| 4312 | Maybe<bool> Object::RedefineIncompatibleProperty(Isolate* isolate, |
| 4313 | Handle<Object> name, |
| 4314 | Handle<Object> value, |
| 4315 | ShouldThrow should_throw) { |
| 4316 | RETURN_FAILURE(isolate, should_throw, |
| 4317 | NewTypeError(MessageTemplate::kRedefineDisallowed, name)); |
| 4318 | } |
| 4319 | |
| 4320 | |
| 4321 | Maybe<bool> Object::SetDataProperty(LookupIterator* it, Handle<Object> value) { |
| 4322 | // Proxies are handled elsewhere. Other non-JSObjects cannot have own |
| 4323 | // properties. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4324 | Handle<JSObject> receiver = Handle<JSObject>::cast(it->GetReceiver()); |
| 4325 | |
| 4326 | // Store on the holder which may be hidden behind the receiver. |
| 4327 | DCHECK(it->HolderIsReceiverOrHiddenPrototype()); |
| 4328 | |
| 4329 | // Old value for the observation change record. |
| 4330 | // Fetch before transforming the object since the encoding may become |
| 4331 | // incompatible with what's cached in |it|. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4332 | bool is_observed = receiver->map()->is_observed() && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4333 | (it->IsElement() || |
| 4334 | !it->isolate()->IsInternallyUsedPropertyName(it->name())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4335 | MaybeHandle<Object> maybe_old; |
| 4336 | if (is_observed) maybe_old = it->GetDataValue(); |
| 4337 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4338 | Handle<Object> to_assign = value; |
| 4339 | // Convert the incoming value to a number for storing into typed arrays. |
| 4340 | if (it->IsElement() && receiver->HasFixedTypedArrayElements()) { |
| 4341 | if (!value->IsNumber() && !value->IsUndefined()) { |
| 4342 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 4343 | it->isolate(), to_assign, Object::ToNumber(value), Nothing<bool>()); |
| 4344 | // ToNumber above might modify the receiver, causing the cached |
| 4345 | // holder_map to mismatch the actual holder->map() after this point. |
| 4346 | // Reload the map to be in consistent state. Other cached state cannot |
| 4347 | // have been invalidated since typed array elements cannot be reconfigured |
| 4348 | // in any way. |
| 4349 | it->ReloadHolderMap(); |
| 4350 | |
| 4351 | // We have to recheck the length. However, it can only change if the |
| 4352 | // underlying buffer was neutered, so just check that. |
| 4353 | if (Handle<JSArrayBufferView>::cast(receiver)->WasNeutered()) { |
| 4354 | return Just(true); |
| 4355 | // TODO(neis): According to the spec, this should throw a TypeError. |
| 4356 | } |
| 4357 | } |
| 4358 | } |
| 4359 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4360 | // Possibly migrate to the most up-to-date map that will be able to store |
| 4361 | // |value| under it->name(). |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4362 | it->PrepareForDataProperty(to_assign); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4363 | |
| 4364 | // Write the property value. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4365 | it->WriteDataValue(to_assign); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4366 | |
| 4367 | // Send the change record if there are observers. |
| 4368 | if (is_observed && !value->SameValue(*maybe_old.ToHandleChecked())) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4369 | RETURN_ON_EXCEPTION_VALUE( |
| 4370 | it->isolate(), |
| 4371 | JSObject::EnqueueChangeRecord(receiver, "update", it->GetName(), |
| 4372 | maybe_old.ToHandleChecked()), |
| 4373 | Nothing<bool>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4376 | #if VERIFY_HEAP |
| 4377 | if (FLAG_verify_heap) { |
| 4378 | receiver->JSObjectVerify(); |
| 4379 | } |
| 4380 | #endif |
| 4381 | return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4382 | } |
| 4383 | |
| 4384 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4385 | MUST_USE_RESULT static MaybeHandle<Object> BeginPerformSplice( |
| 4386 | Handle<JSArray> object) { |
| 4387 | Isolate* isolate = object->GetIsolate(); |
| 4388 | HandleScope scope(isolate); |
| 4389 | Handle<Object> args[] = {object}; |
| 4390 | |
| 4391 | return Execution::Call( |
| 4392 | isolate, Handle<JSFunction>(isolate->observers_begin_perform_splice()), |
| 4393 | isolate->factory()->undefined_value(), arraysize(args), args); |
| 4394 | } |
| 4395 | |
| 4396 | |
| 4397 | MUST_USE_RESULT static MaybeHandle<Object> EndPerformSplice( |
| 4398 | Handle<JSArray> object) { |
| 4399 | Isolate* isolate = object->GetIsolate(); |
| 4400 | HandleScope scope(isolate); |
| 4401 | Handle<Object> args[] = {object}; |
| 4402 | |
| 4403 | return Execution::Call( |
| 4404 | isolate, Handle<JSFunction>(isolate->observers_end_perform_splice()), |
| 4405 | isolate->factory()->undefined_value(), arraysize(args), args); |
| 4406 | } |
| 4407 | |
| 4408 | |
| 4409 | MUST_USE_RESULT static MaybeHandle<Object> EnqueueSpliceRecord( |
| 4410 | Handle<JSArray> object, uint32_t index, Handle<JSArray> deleted, |
| 4411 | uint32_t add_count) { |
| 4412 | Isolate* isolate = object->GetIsolate(); |
| 4413 | HandleScope scope(isolate); |
| 4414 | Handle<Object> index_object = isolate->factory()->NewNumberFromUint(index); |
| 4415 | Handle<Object> add_count_object = |
| 4416 | isolate->factory()->NewNumberFromUint(add_count); |
| 4417 | |
| 4418 | Handle<Object> args[] = {object, index_object, deleted, add_count_object}; |
| 4419 | |
| 4420 | return Execution::Call( |
| 4421 | isolate, Handle<JSFunction>(isolate->observers_enqueue_splice()), |
| 4422 | isolate->factory()->undefined_value(), arraysize(args), args); |
| 4423 | } |
| 4424 | |
| 4425 | |
| 4426 | Maybe<bool> Object::AddDataProperty(LookupIterator* it, Handle<Object> value, |
| 4427 | PropertyAttributes attributes, |
| 4428 | ShouldThrow should_throw, |
| 4429 | StoreFromKeyed store_mode) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4430 | DCHECK(!it->GetReceiver()->IsJSProxy()); |
| 4431 | if (!it->GetReceiver()->IsJSObject()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4432 | return CannotCreateProperty(it->isolate(), it->GetReceiver(), it->GetName(), |
| 4433 | value, should_throw); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4436 | DCHECK_NE(LookupIterator::INTEGER_INDEXED_EXOTIC, it->state()); |
| 4437 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4438 | Handle<JSObject> receiver = it->GetStoreTarget(); |
| 4439 | |
| 4440 | // If the receiver is a JSGlobalProxy, store on the prototype (JSGlobalObject) |
| 4441 | // instead. If the prototype is Null, the proxy is detached. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4442 | if (receiver->IsJSGlobalProxy()) return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4443 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4444 | Isolate* isolate = it->isolate(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4445 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4446 | if (!receiver->map()->is_extensible() && |
| 4447 | (it->IsElement() || !isolate->IsInternallyUsedPropertyName(it->name()))) { |
| 4448 | RETURN_FAILURE( |
| 4449 | isolate, should_throw, |
| 4450 | NewTypeError(MessageTemplate::kObjectNotExtensible, it->GetName())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4451 | } |
| 4452 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4453 | if (it->IsElement()) { |
| 4454 | if (receiver->IsJSArray()) { |
| 4455 | Handle<JSArray> array = Handle<JSArray>::cast(receiver); |
| 4456 | if (JSArray::WouldChangeReadOnlyLength(array, it->index())) { |
| 4457 | RETURN_FAILURE(array->GetIsolate(), should_throw, |
| 4458 | NewTypeError(MessageTemplate::kStrictReadOnlyProperty, |
| 4459 | isolate->factory()->length_string(), |
| 4460 | Object::TypeOf(isolate, array), array)); |
| 4461 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4462 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4463 | if (FLAG_trace_external_array_abuse && |
| 4464 | array->HasFixedTypedArrayElements()) { |
| 4465 | CheckArrayAbuse(array, "typed elements write", it->index(), true); |
| 4466 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4467 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4468 | if (FLAG_trace_js_array_abuse && !array->HasFixedTypedArrayElements()) { |
| 4469 | CheckArrayAbuse(array, "elements write", it->index(), false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4470 | } |
| 4471 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4472 | |
| 4473 | Maybe<bool> result = JSObject::AddDataElement(receiver, it->index(), value, |
| 4474 | attributes, should_throw); |
| 4475 | JSObject::ValidateElements(receiver); |
| 4476 | return result; |
| 4477 | } else { |
| 4478 | // Migrate to the most up-to-date map that will be able to store |value| |
| 4479 | // under it->name() with |attributes|. |
| 4480 | it->PrepareTransitionToDataProperty(value, attributes, store_mode); |
| 4481 | DCHECK_EQ(LookupIterator::TRANSITION, it->state()); |
| 4482 | it->ApplyTransitionToDataProperty(); |
| 4483 | |
| 4484 | // TODO(verwaest): Encapsulate dictionary handling better. |
| 4485 | if (receiver->map()->is_dictionary_map()) { |
| 4486 | // TODO(verwaest): Probably should ensure this is done beforehand. |
| 4487 | it->InternalizeName(); |
| 4488 | // TODO(dcarney): just populate TransitionPropertyCell here? |
| 4489 | JSObject::AddSlowProperty(receiver, it->name(), value, attributes); |
| 4490 | } else { |
| 4491 | // Write the property value. |
| 4492 | it->WriteDataValue(value); |
| 4493 | } |
| 4494 | |
| 4495 | // Send the change record if there are observers. |
| 4496 | if (receiver->map()->is_observed() && |
| 4497 | !isolate->IsInternallyUsedPropertyName(it->name())) { |
| 4498 | RETURN_ON_EXCEPTION_VALUE(isolate, JSObject::EnqueueChangeRecord( |
| 4499 | receiver, "add", it->name(), |
| 4500 | it->factory()->the_hole_value()), |
| 4501 | Nothing<bool>()); |
| 4502 | } |
| 4503 | #if VERIFY_HEAP |
| 4504 | if (FLAG_verify_heap) { |
| 4505 | receiver->JSObjectVerify(); |
| 4506 | } |
| 4507 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4508 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4509 | |
| 4510 | return Just(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4511 | } |
| 4512 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4513 | |
| 4514 | void Map::EnsureDescriptorSlack(Handle<Map> map, int slack) { |
| 4515 | // Only supports adding slack to owned descriptors. |
| 4516 | DCHECK(map->owns_descriptors()); |
| 4517 | |
| 4518 | Handle<DescriptorArray> descriptors(map->instance_descriptors()); |
| 4519 | int old_size = map->NumberOfOwnDescriptors(); |
| 4520 | if (slack <= descriptors->NumberOfSlackDescriptors()) return; |
| 4521 | |
| 4522 | Handle<DescriptorArray> new_descriptors = DescriptorArray::CopyUpTo( |
| 4523 | descriptors, old_size, slack); |
| 4524 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4525 | DisallowHeapAllocation no_allocation; |
| 4526 | // The descriptors are still the same, so keep the layout descriptor. |
| 4527 | LayoutDescriptor* layout_descriptor = map->GetLayoutDescriptor(); |
| 4528 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4529 | if (old_size == 0) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4530 | map->UpdateDescriptors(*new_descriptors, layout_descriptor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4531 | return; |
| 4532 | } |
| 4533 | |
| 4534 | // If the source descriptors had an enum cache we copy it. This ensures |
| 4535 | // that the maps to which we push the new descriptor array back can rely |
| 4536 | // on a cache always being available once it is set. If the map has more |
| 4537 | // enumerated descriptors than available in the original cache, the cache |
| 4538 | // will be lazily replaced by the extended cache when needed. |
| 4539 | if (descriptors->HasEnumCache()) { |
| 4540 | new_descriptors->CopyEnumCacheFrom(*descriptors); |
| 4541 | } |
| 4542 | |
| 4543 | // Replace descriptors by new_descriptors in all maps that share it. |
| 4544 | map->GetHeap()->incremental_marking()->RecordWrites(*descriptors); |
| 4545 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4546 | Map* current = *map; |
| 4547 | while (current->instance_descriptors() == *descriptors) { |
| 4548 | Object* next = current->GetBackPointer(); |
| 4549 | if (next->IsUndefined()) break; // Stop overwriting at initial map. |
| 4550 | current->UpdateDescriptors(*new_descriptors, layout_descriptor); |
| 4551 | current = Map::cast(next); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4552 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4553 | map->UpdateDescriptors(*new_descriptors, layout_descriptor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4554 | } |
| 4555 | |
| 4556 | |
| 4557 | template<class T> |
| 4558 | static int AppendUniqueCallbacks(NeanderArray* callbacks, |
| 4559 | Handle<typename T::Array> array, |
| 4560 | int valid_descriptors) { |
| 4561 | int nof_callbacks = callbacks->length(); |
| 4562 | |
| 4563 | Isolate* isolate = array->GetIsolate(); |
| 4564 | // Ensure the keys are unique names before writing them into the |
| 4565 | // instance descriptor. Since it may cause a GC, it has to be done before we |
| 4566 | // temporarily put the heap in an invalid state while appending descriptors. |
| 4567 | for (int i = 0; i < nof_callbacks; ++i) { |
| 4568 | Handle<AccessorInfo> entry(AccessorInfo::cast(callbacks->get(i))); |
| 4569 | if (entry->name()->IsUniqueName()) continue; |
| 4570 | Handle<String> key = |
| 4571 | isolate->factory()->InternalizeString( |
| 4572 | Handle<String>(String::cast(entry->name()))); |
| 4573 | entry->set_name(*key); |
| 4574 | } |
| 4575 | |
| 4576 | // Fill in new callback descriptors. Process the callbacks from |
| 4577 | // back to front so that the last callback with a given name takes |
| 4578 | // precedence over previously added callbacks with that name. |
| 4579 | for (int i = nof_callbacks - 1; i >= 0; i--) { |
| 4580 | Handle<AccessorInfo> entry(AccessorInfo::cast(callbacks->get(i))); |
| 4581 | Handle<Name> key(Name::cast(entry->name())); |
| 4582 | // Check if a descriptor with this name already exists before writing. |
| 4583 | if (!T::Contains(key, entry, valid_descriptors, array)) { |
| 4584 | T::Insert(key, entry, valid_descriptors, array); |
| 4585 | valid_descriptors++; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4586 | } |
| 4587 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4588 | |
| 4589 | return valid_descriptors; |
| 4590 | } |
| 4591 | |
| 4592 | struct DescriptorArrayAppender { |
| 4593 | typedef DescriptorArray Array; |
| 4594 | static bool Contains(Handle<Name> key, |
| 4595 | Handle<AccessorInfo> entry, |
| 4596 | int valid_descriptors, |
| 4597 | Handle<DescriptorArray> array) { |
| 4598 | DisallowHeapAllocation no_gc; |
| 4599 | return array->Search(*key, valid_descriptors) != DescriptorArray::kNotFound; |
| 4600 | } |
| 4601 | static void Insert(Handle<Name> key, |
| 4602 | Handle<AccessorInfo> entry, |
| 4603 | int valid_descriptors, |
| 4604 | Handle<DescriptorArray> array) { |
| 4605 | DisallowHeapAllocation no_gc; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4606 | AccessorConstantDescriptor desc(key, entry, entry->property_attributes()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4607 | array->Append(&desc); |
| 4608 | } |
| 4609 | }; |
| 4610 | |
| 4611 | |
| 4612 | struct FixedArrayAppender { |
| 4613 | typedef FixedArray Array; |
| 4614 | static bool Contains(Handle<Name> key, |
| 4615 | Handle<AccessorInfo> entry, |
| 4616 | int valid_descriptors, |
| 4617 | Handle<FixedArray> array) { |
| 4618 | for (int i = 0; i < valid_descriptors; i++) { |
| 4619 | if (*key == AccessorInfo::cast(array->get(i))->name()) return true; |
| 4620 | } |
| 4621 | return false; |
| 4622 | } |
| 4623 | static void Insert(Handle<Name> key, |
| 4624 | Handle<AccessorInfo> entry, |
| 4625 | int valid_descriptors, |
| 4626 | Handle<FixedArray> array) { |
| 4627 | DisallowHeapAllocation no_gc; |
| 4628 | array->set(valid_descriptors, *entry); |
| 4629 | } |
| 4630 | }; |
| 4631 | |
| 4632 | |
| 4633 | void Map::AppendCallbackDescriptors(Handle<Map> map, |
| 4634 | Handle<Object> descriptors) { |
| 4635 | int nof = map->NumberOfOwnDescriptors(); |
| 4636 | Handle<DescriptorArray> array(map->instance_descriptors()); |
| 4637 | NeanderArray callbacks(descriptors); |
| 4638 | DCHECK(array->NumberOfSlackDescriptors() >= callbacks.length()); |
| 4639 | nof = AppendUniqueCallbacks<DescriptorArrayAppender>(&callbacks, array, nof); |
| 4640 | map->SetNumberOfOwnDescriptors(nof); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4641 | } |
| 4642 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4643 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4644 | int AccessorInfo::AppendUnique(Handle<Object> descriptors, |
| 4645 | Handle<FixedArray> array, |
| 4646 | int valid_descriptors) { |
| 4647 | NeanderArray callbacks(descriptors); |
| 4648 | DCHECK(array->length() >= callbacks.length() + valid_descriptors); |
| 4649 | return AppendUniqueCallbacks<FixedArrayAppender>(&callbacks, |
| 4650 | array, |
| 4651 | valid_descriptors); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4652 | } |
| 4653 | |
| 4654 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4655 | static bool ContainsMap(MapHandleList* maps, Map* map) { |
| 4656 | DCHECK_NOT_NULL(map); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4657 | for (int i = 0; i < maps->length(); ++i) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4658 | if (!maps->at(i).is_null() && *maps->at(i) == map) return true; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4659 | } |
| 4660 | return false; |
| 4661 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4662 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 4663 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4664 | Handle<Map> Map::FindTransitionedMap(Handle<Map> map, |
| 4665 | MapHandleList* candidates) { |
| 4666 | ElementsKind kind = map->elements_kind(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4667 | bool packed = IsFastPackedElementsKind(kind); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4668 | |
| 4669 | Map* transition = nullptr; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4670 | if (IsTransitionableFastElementsKind(kind)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4671 | for (Map* current = map->ElementsTransitionMap(); |
| 4672 | current != nullptr && current->has_fast_elements(); |
| 4673 | current = current->ElementsTransitionMap()) { |
| 4674 | if (ContainsMap(candidates, current) && |
| 4675 | (packed || !IsFastPackedElementsKind(current->elements_kind()))) { |
| 4676 | transition = current; |
| 4677 | packed = packed && IsFastPackedElementsKind(current->elements_kind()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4678 | } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 4679 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4680 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4681 | return transition == nullptr ? Handle<Map>() : handle(transition); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4682 | } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 4683 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4684 | |
| 4685 | static Map* FindClosestElementsTransition(Map* map, ElementsKind to_kind) { |
| 4686 | Map* current_map = map; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4687 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4688 | ElementsKind kind = map->elements_kind(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4689 | while (kind != to_kind) { |
| 4690 | Map* next_map = current_map->ElementsTransitionMap(); |
| 4691 | if (next_map == nullptr) return current_map; |
| 4692 | kind = next_map->elements_kind(); |
| 4693 | current_map = next_map; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4696 | DCHECK_EQ(to_kind, current_map->elements_kind()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4697 | return current_map; |
| 4698 | } |
| 4699 | |
| 4700 | |
| 4701 | Map* Map::LookupElementsTransitionMap(ElementsKind to_kind) { |
| 4702 | Map* to_map = FindClosestElementsTransition(this, to_kind); |
| 4703 | if (to_map->elements_kind() == to_kind) return to_map; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4704 | return nullptr; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4705 | } |
| 4706 | |
| 4707 | |
| 4708 | bool Map::IsMapInArrayPrototypeChain() { |
| 4709 | Isolate* isolate = GetIsolate(); |
| 4710 | if (isolate->initial_array_prototype()->map() == this) { |
| 4711 | return true; |
| 4712 | } |
| 4713 | |
| 4714 | if (isolate->initial_object_prototype()->map() == this) { |
| 4715 | return true; |
| 4716 | } |
| 4717 | |
| 4718 | return false; |
| 4719 | } |
| 4720 | |
| 4721 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4722 | Handle<WeakCell> Map::WeakCellForMap(Handle<Map> map) { |
| 4723 | Isolate* isolate = map->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4724 | if (map->weak_cell_cache()->IsWeakCell()) { |
| 4725 | return Handle<WeakCell>(WeakCell::cast(map->weak_cell_cache())); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4726 | } |
| 4727 | Handle<WeakCell> weak_cell = isolate->factory()->NewWeakCell(map); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4728 | map->set_weak_cell_cache(*weak_cell); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4729 | return weak_cell; |
| 4730 | } |
| 4731 | |
| 4732 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4733 | static Handle<Map> AddMissingElementsTransitions(Handle<Map> map, |
| 4734 | ElementsKind to_kind) { |
| 4735 | DCHECK(IsTransitionElementsKind(map->elements_kind())); |
| 4736 | |
| 4737 | Handle<Map> current_map = map; |
| 4738 | |
| 4739 | ElementsKind kind = map->elements_kind(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4740 | TransitionFlag flag; |
| 4741 | if (map->is_prototype_map()) { |
| 4742 | flag = OMIT_TRANSITION; |
| 4743 | } else { |
| 4744 | flag = INSERT_TRANSITION; |
| 4745 | if (IsFastElementsKind(kind)) { |
| 4746 | while (kind != to_kind && !IsTerminalElementsKind(kind)) { |
| 4747 | kind = GetNextTransitionElementsKind(kind); |
| 4748 | current_map = Map::CopyAsElementsKind(current_map, kind, flag); |
| 4749 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4750 | } |
| 4751 | } |
| 4752 | |
| 4753 | // In case we are exiting the fast elements kind system, just add the map in |
| 4754 | // the end. |
| 4755 | if (kind != to_kind) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4756 | current_map = Map::CopyAsElementsKind(current_map, to_kind, flag); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4757 | } |
| 4758 | |
| 4759 | DCHECK(current_map->elements_kind() == to_kind); |
| 4760 | return current_map; |
| 4761 | } |
| 4762 | |
| 4763 | |
| 4764 | Handle<Map> Map::TransitionElementsTo(Handle<Map> map, |
| 4765 | ElementsKind to_kind) { |
| 4766 | ElementsKind from_kind = map->elements_kind(); |
| 4767 | if (from_kind == to_kind) return map; |
| 4768 | |
| 4769 | Isolate* isolate = map->GetIsolate(); |
| 4770 | Context* native_context = isolate->context()->native_context(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4771 | if (from_kind == FAST_SLOPPY_ARGUMENTS_ELEMENTS) { |
| 4772 | if (*map == native_context->fast_aliased_arguments_map()) { |
| 4773 | DCHECK_EQ(SLOW_SLOPPY_ARGUMENTS_ELEMENTS, to_kind); |
| 4774 | return handle(native_context->slow_aliased_arguments_map()); |
| 4775 | } |
| 4776 | } else if (from_kind == SLOW_SLOPPY_ARGUMENTS_ELEMENTS) { |
| 4777 | if (*map == native_context->slow_aliased_arguments_map()) { |
| 4778 | DCHECK_EQ(FAST_SLOPPY_ARGUMENTS_ELEMENTS, to_kind); |
| 4779 | return handle(native_context->fast_aliased_arguments_map()); |
| 4780 | } |
| 4781 | } else if (IsFastElementsKind(from_kind) && IsFastElementsKind(to_kind)) { |
| 4782 | // Reuse map transitions for JSArrays. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4783 | DisallowHeapAllocation no_gc; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4784 | Strength strength = map->is_strong() ? Strength::STRONG : Strength::WEAK; |
| 4785 | if (native_context->get(Context::ArrayMapIndex(from_kind, strength)) == |
| 4786 | *map) { |
| 4787 | Object* maybe_transitioned_map = |
| 4788 | native_context->get(Context::ArrayMapIndex(to_kind, strength)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4789 | if (maybe_transitioned_map->IsMap()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4790 | return handle(Map::cast(maybe_transitioned_map), isolate); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 4791 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4792 | } |
| 4793 | } |
| 4794 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4795 | DCHECK(!map->IsUndefined()); |
| 4796 | // Check if we can go back in the elements kind transition chain. |
| 4797 | if (IsHoleyElementsKind(from_kind) && |
| 4798 | to_kind == GetPackedElementsKind(from_kind) && |
| 4799 | map->GetBackPointer()->IsMap() && |
| 4800 | Map::cast(map->GetBackPointer())->elements_kind() == to_kind) { |
| 4801 | return handle(Map::cast(map->GetBackPointer())); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4802 | } |
| 4803 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4804 | bool allow_store_transition = IsTransitionElementsKind(from_kind); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4805 | // Only store fast element maps in ascending generality. |
| 4806 | if (IsFastElementsKind(to_kind)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4807 | allow_store_transition = |
| 4808 | allow_store_transition && IsTransitionableFastElementsKind(from_kind) && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4809 | IsMoreGeneralElementsKindTransition(from_kind, to_kind); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4810 | } |
| 4811 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4812 | if (!allow_store_transition) { |
| 4813 | return Map::CopyAsElementsKind(map, to_kind, OMIT_TRANSITION); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4814 | } |
| 4815 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4816 | return Map::AsElementsKind(map, to_kind); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4817 | } |
| 4818 | |
| 4819 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4820 | // static |
| 4821 | Handle<Map> Map::AsElementsKind(Handle<Map> map, ElementsKind kind) { |
| 4822 | Handle<Map> closest_map(FindClosestElementsTransition(*map, kind)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4823 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4824 | if (closest_map->elements_kind() == kind) { |
| 4825 | return closest_map; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4826 | } |
| 4827 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4828 | return AddMissingElementsTransitions(closest_map, kind); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | |
| 4832 | Handle<Map> JSObject::GetElementsTransitionMap(Handle<JSObject> object, |
| 4833 | ElementsKind to_kind) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4834 | Handle<Map> map(object->map()); |
| 4835 | return Map::TransitionElementsTo(map, to_kind); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4836 | } |
| 4837 | |
| 4838 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4839 | void JSProxy::Revoke(Handle<JSProxy> proxy) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4840 | Isolate* isolate = proxy->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4841 | if (!proxy->IsRevoked()) proxy->set_handler(isolate->heap()->null_value()); |
| 4842 | DCHECK(proxy->IsRevoked()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4843 | } |
| 4844 | |
| 4845 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4846 | Maybe<bool> JSProxy::HasProperty(Isolate* isolate, Handle<JSProxy> proxy, |
| 4847 | Handle<Name> name) { |
| 4848 | DCHECK(!name->IsPrivate()); |
| 4849 | STACK_CHECK(Nothing<bool>()); |
| 4850 | // 1. (Assert) |
| 4851 | // 2. Let handler be the value of the [[ProxyHandler]] internal slot of O. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4852 | Handle<Object> handler(proxy->handler(), isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4853 | // 3. If handler is null, throw a TypeError exception. |
| 4854 | // 4. Assert: Type(handler) is Object. |
| 4855 | if (proxy->IsRevoked()) { |
| 4856 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 4857 | MessageTemplate::kProxyRevoked, isolate->factory()->has_string())); |
| 4858 | return Nothing<bool>(); |
| 4859 | } |
| 4860 | // 5. Let target be the value of the [[ProxyTarget]] internal slot of O. |
| 4861 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 4862 | // 6. Let trap be ? GetMethod(handler, "has"). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4863 | Handle<Object> trap; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4864 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 4865 | isolate, trap, Object::GetMethod(Handle<JSReceiver>::cast(handler), |
| 4866 | isolate->factory()->has_string()), |
| 4867 | Nothing<bool>()); |
| 4868 | // 7. If trap is undefined, then |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4869 | if (trap->IsUndefined()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4870 | // 7a. Return target.[[HasProperty]](P). |
| 4871 | return JSReceiver::HasProperty(target, name); |
| 4872 | } |
| 4873 | // 8. Let booleanTrapResult be ToBoolean(? Call(trap, handler, «target, P»)). |
| 4874 | Handle<Object> trap_result_obj; |
| 4875 | Handle<Object> args[] = {target, name}; |
| 4876 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 4877 | isolate, trap_result_obj, |
| 4878 | Execution::Call(isolate, trap, handler, arraysize(args), args), |
| 4879 | Nothing<bool>()); |
| 4880 | bool boolean_trap_result = trap_result_obj->BooleanValue(); |
| 4881 | // 9. If booleanTrapResult is false, then: |
| 4882 | if (!boolean_trap_result) { |
| 4883 | // 9a. Let targetDesc be ? target.[[GetOwnProperty]](P). |
| 4884 | PropertyDescriptor target_desc; |
| 4885 | Maybe<bool> target_found = JSReceiver::GetOwnPropertyDescriptor( |
| 4886 | isolate, target, name, &target_desc); |
| 4887 | MAYBE_RETURN(target_found, Nothing<bool>()); |
| 4888 | // 9b. If targetDesc is not undefined, then: |
| 4889 | if (target_found.FromJust()) { |
| 4890 | // 9b i. If targetDesc.[[Configurable]] is false, throw a TypeError |
| 4891 | // exception. |
| 4892 | if (!target_desc.configurable()) { |
| 4893 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 4894 | MessageTemplate::kProxyHasNonConfigurable, name)); |
| 4895 | return Nothing<bool>(); |
| 4896 | } |
| 4897 | // 9b ii. Let extensibleTarget be ? IsExtensible(target). |
| 4898 | Maybe<bool> extensible_target = JSReceiver::IsExtensible(target); |
| 4899 | MAYBE_RETURN(extensible_target, Nothing<bool>()); |
| 4900 | // 9b iii. If extensibleTarget is false, throw a TypeError exception. |
| 4901 | if (!extensible_target.FromJust()) { |
| 4902 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 4903 | MessageTemplate::kProxyHasNonExtensible, name)); |
| 4904 | return Nothing<bool>(); |
| 4905 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4906 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4907 | } |
| 4908 | // 10. Return booleanTrapResult. |
| 4909 | return Just(boolean_trap_result); |
| 4910 | } |
| 4911 | |
| 4912 | |
| 4913 | Maybe<bool> JSProxy::SetProperty(Handle<JSProxy> proxy, Handle<Name> name, |
| 4914 | Handle<Object> value, Handle<Object> receiver, |
| 4915 | LanguageMode language_mode) { |
| 4916 | DCHECK(!name->IsPrivate()); |
| 4917 | Isolate* isolate = proxy->GetIsolate(); |
| 4918 | STACK_CHECK(Nothing<bool>()); |
| 4919 | Factory* factory = isolate->factory(); |
| 4920 | Handle<String> trap_name = factory->set_string(); |
| 4921 | ShouldThrow should_throw = |
| 4922 | is_sloppy(language_mode) ? DONT_THROW : THROW_ON_ERROR; |
| 4923 | |
| 4924 | if (proxy->IsRevoked()) { |
| 4925 | isolate->Throw( |
| 4926 | *factory->NewTypeError(MessageTemplate::kProxyRevoked, trap_name)); |
| 4927 | return Nothing<bool>(); |
| 4928 | } |
| 4929 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 4930 | Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate); |
| 4931 | |
| 4932 | Handle<Object> trap; |
| 4933 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 4934 | isolate, trap, Object::GetMethod(handler, trap_name), Nothing<bool>()); |
| 4935 | if (trap->IsUndefined()) { |
| 4936 | LookupIterator it = |
| 4937 | LookupIterator::PropertyOrElement(isolate, receiver, name, target); |
| 4938 | return Object::SetSuperProperty(&it, value, language_mode, |
| 4939 | Object::MAY_BE_STORE_FROM_KEYED); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4940 | } |
| 4941 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 4942 | Handle<Object> trap_result; |
| 4943 | Handle<Object> args[] = {target, name, value, receiver}; |
| 4944 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 4945 | isolate, trap_result, |
| 4946 | Execution::Call(isolate, trap, handler, arraysize(args), args), |
| 4947 | Nothing<bool>()); |
| 4948 | if (!trap_result->BooleanValue()) { |
| 4949 | RETURN_FAILURE(isolate, should_throw, |
| 4950 | NewTypeError(MessageTemplate::kProxyTrapReturnedFalsishFor, |
| 4951 | trap_name, name)); |
| 4952 | } |
| 4953 | |
| 4954 | // Enforce the invariant. |
| 4955 | PropertyDescriptor target_desc; |
| 4956 | Maybe<bool> owned = |
| 4957 | JSReceiver::GetOwnPropertyDescriptor(isolate, target, name, &target_desc); |
| 4958 | MAYBE_RETURN(owned, Nothing<bool>()); |
| 4959 | if (owned.FromJust()) { |
| 4960 | bool inconsistent = PropertyDescriptor::IsDataDescriptor(&target_desc) && |
| 4961 | !target_desc.configurable() && |
| 4962 | !target_desc.writable() && |
| 4963 | !value->SameValue(*target_desc.value()); |
| 4964 | if (inconsistent) { |
| 4965 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 4966 | MessageTemplate::kProxySetFrozenData, name)); |
| 4967 | return Nothing<bool>(); |
| 4968 | } |
| 4969 | inconsistent = PropertyDescriptor::IsAccessorDescriptor(&target_desc) && |
| 4970 | !target_desc.configurable() && |
| 4971 | target_desc.set()->IsUndefined(); |
| 4972 | if (inconsistent) { |
| 4973 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 4974 | MessageTemplate::kProxySetFrozenAccessor, name)); |
| 4975 | return Nothing<bool>(); |
| 4976 | } |
| 4977 | } |
| 4978 | return Just(true); |
| 4979 | } |
| 4980 | |
| 4981 | |
| 4982 | Maybe<bool> JSProxy::DeletePropertyOrElement(Handle<JSProxy> proxy, |
| 4983 | Handle<Name> name, |
| 4984 | LanguageMode language_mode) { |
| 4985 | DCHECK(!name->IsPrivate()); |
| 4986 | ShouldThrow should_throw = |
| 4987 | is_sloppy(language_mode) ? DONT_THROW : THROW_ON_ERROR; |
| 4988 | Isolate* isolate = proxy->GetIsolate(); |
| 4989 | STACK_CHECK(Nothing<bool>()); |
| 4990 | Factory* factory = isolate->factory(); |
| 4991 | Handle<String> trap_name = factory->deleteProperty_string(); |
| 4992 | |
| 4993 | if (proxy->IsRevoked()) { |
| 4994 | isolate->Throw( |
| 4995 | *factory->NewTypeError(MessageTemplate::kProxyRevoked, trap_name)); |
| 4996 | return Nothing<bool>(); |
| 4997 | } |
| 4998 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 4999 | Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate); |
| 5000 | |
| 5001 | Handle<Object> trap; |
| 5002 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 5003 | isolate, trap, Object::GetMethod(handler, trap_name), Nothing<bool>()); |
| 5004 | if (trap->IsUndefined()) { |
| 5005 | return JSReceiver::DeletePropertyOrElement(target, name, language_mode); |
| 5006 | } |
| 5007 | |
| 5008 | Handle<Object> trap_result; |
| 5009 | Handle<Object> args[] = {target, name}; |
| 5010 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 5011 | isolate, trap_result, |
| 5012 | Execution::Call(isolate, trap, handler, arraysize(args), args), |
| 5013 | Nothing<bool>()); |
| 5014 | if (!trap_result->BooleanValue()) { |
| 5015 | RETURN_FAILURE(isolate, should_throw, |
| 5016 | NewTypeError(MessageTemplate::kProxyTrapReturnedFalsishFor, |
| 5017 | trap_name, name)); |
| 5018 | } |
| 5019 | |
| 5020 | // Enforce the invariant. |
| 5021 | PropertyDescriptor target_desc; |
| 5022 | Maybe<bool> owned = |
| 5023 | JSReceiver::GetOwnPropertyDescriptor(isolate, target, name, &target_desc); |
| 5024 | MAYBE_RETURN(owned, Nothing<bool>()); |
| 5025 | if (owned.FromJust() && !target_desc.configurable()) { |
| 5026 | isolate->Throw(*factory->NewTypeError( |
| 5027 | MessageTemplate::kProxyDeletePropertyNonConfigurable, name)); |
| 5028 | return Nothing<bool>(); |
| 5029 | } |
| 5030 | return Just(true); |
| 5031 | } |
| 5032 | |
| 5033 | |
| 5034 | // static |
| 5035 | MaybeHandle<JSProxy> JSProxy::New(Isolate* isolate, Handle<Object> target, |
| 5036 | Handle<Object> handler) { |
| 5037 | if (!target->IsJSReceiver()) { |
| 5038 | THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kProxyNonObject), |
| 5039 | JSProxy); |
| 5040 | } |
| 5041 | if (target->IsJSProxy() && JSProxy::cast(*target)->IsRevoked()) { |
| 5042 | THROW_NEW_ERROR(isolate, |
| 5043 | NewTypeError(MessageTemplate::kProxyHandlerOrTargetRevoked), |
| 5044 | JSProxy); |
| 5045 | } |
| 5046 | if (!handler->IsJSReceiver()) { |
| 5047 | THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kProxyNonObject), |
| 5048 | JSProxy); |
| 5049 | } |
| 5050 | if (handler->IsJSProxy() && JSProxy::cast(*handler)->IsRevoked()) { |
| 5051 | THROW_NEW_ERROR(isolate, |
| 5052 | NewTypeError(MessageTemplate::kProxyHandlerOrTargetRevoked), |
| 5053 | JSProxy); |
| 5054 | } |
| 5055 | return isolate->factory()->NewJSProxy(Handle<JSReceiver>::cast(target), |
| 5056 | Handle<JSReceiver>::cast(handler)); |
| 5057 | } |
| 5058 | |
| 5059 | |
| 5060 | // static |
| 5061 | MaybeHandle<Context> JSProxy::GetFunctionRealm(Handle<JSProxy> proxy) { |
| 5062 | DCHECK(proxy->map()->is_constructor()); |
| 5063 | if (proxy->IsRevoked()) { |
| 5064 | THROW_NEW_ERROR(proxy->GetIsolate(), |
| 5065 | NewTypeError(MessageTemplate::kProxyRevoked), Context); |
| 5066 | } |
| 5067 | Handle<JSReceiver> target(JSReceiver::cast(proxy->target())); |
| 5068 | return JSReceiver::GetFunctionRealm(target); |
| 5069 | } |
| 5070 | |
| 5071 | |
| 5072 | // static |
| 5073 | MaybeHandle<Context> JSBoundFunction::GetFunctionRealm( |
| 5074 | Handle<JSBoundFunction> function) { |
| 5075 | DCHECK(function->map()->is_constructor()); |
| 5076 | return JSReceiver::GetFunctionRealm( |
| 5077 | handle(function->bound_target_function())); |
| 5078 | } |
| 5079 | |
| 5080 | |
| 5081 | // static |
| 5082 | Handle<Context> JSFunction::GetFunctionRealm(Handle<JSFunction> function) { |
| 5083 | DCHECK(function->map()->is_constructor()); |
| 5084 | return handle(function->context()->native_context()); |
| 5085 | } |
| 5086 | |
| 5087 | |
| 5088 | // static |
| 5089 | MaybeHandle<Context> JSObject::GetFunctionRealm(Handle<JSObject> object) { |
| 5090 | DCHECK(object->map()->is_constructor()); |
| 5091 | DCHECK(!object->IsJSFunction()); |
| 5092 | return handle(object->GetCreationContext()); |
| 5093 | } |
| 5094 | |
| 5095 | |
| 5096 | // static |
| 5097 | MaybeHandle<Context> JSReceiver::GetFunctionRealm(Handle<JSReceiver> receiver) { |
| 5098 | if (receiver->IsJSProxy()) { |
| 5099 | return JSProxy::GetFunctionRealm(Handle<JSProxy>::cast(receiver)); |
| 5100 | } |
| 5101 | |
| 5102 | if (receiver->IsJSFunction()) { |
| 5103 | return JSFunction::GetFunctionRealm(Handle<JSFunction>::cast(receiver)); |
| 5104 | } |
| 5105 | |
| 5106 | if (receiver->IsJSBoundFunction()) { |
| 5107 | return JSBoundFunction::GetFunctionRealm( |
| 5108 | Handle<JSBoundFunction>::cast(receiver)); |
| 5109 | } |
| 5110 | |
| 5111 | return JSObject::GetFunctionRealm(Handle<JSObject>::cast(receiver)); |
| 5112 | } |
| 5113 | |
| 5114 | |
| 5115 | Maybe<PropertyAttributes> JSProxy::GetPropertyAttributes(LookupIterator* it) { |
| 5116 | Isolate* isolate = it->isolate(); |
| 5117 | HandleScope scope(isolate); |
| 5118 | PropertyDescriptor desc; |
| 5119 | Maybe<bool> found = JSProxy::GetOwnPropertyDescriptor( |
| 5120 | isolate, it->GetHolder<JSProxy>(), it->GetName(), &desc); |
| 5121 | MAYBE_RETURN(found, Nothing<PropertyAttributes>()); |
| 5122 | if (!found.FromJust()) return Just(ABSENT); |
| 5123 | return Just(desc.ToAttributes()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5124 | } |
| 5125 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5126 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5127 | void JSObject::AllocateStorageForMap(Handle<JSObject> object, Handle<Map> map) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5128 | DCHECK(object->map()->GetInObjectProperties() == |
| 5129 | map->GetInObjectProperties()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5130 | ElementsKind obj_kind = object->map()->elements_kind(); |
| 5131 | ElementsKind map_kind = map->elements_kind(); |
| 5132 | if (map_kind != obj_kind) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5133 | ElementsKind to_kind = GetMoreGeneralElementsKind(map_kind, obj_kind); |
| 5134 | if (IsDictionaryElementsKind(obj_kind)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5135 | to_kind = obj_kind; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5136 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5137 | if (IsDictionaryElementsKind(to_kind)) { |
| 5138 | NormalizeElements(object); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5139 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5140 | TransitionElementsKind(object, to_kind); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5141 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5142 | map = Map::AsElementsKind(map, to_kind); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5143 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5144 | JSObject::MigrateToMap(object, map); |
| 5145 | } |
| 5146 | |
| 5147 | |
| 5148 | void JSObject::MigrateInstance(Handle<JSObject> object) { |
| 5149 | Handle<Map> original_map(object->map()); |
| 5150 | Handle<Map> map = Map::Update(original_map); |
| 5151 | map->set_migration_target(true); |
| 5152 | MigrateToMap(object, map); |
| 5153 | if (FLAG_trace_migration) { |
| 5154 | object->PrintInstanceMigration(stdout, *original_map, *map); |
| 5155 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5156 | #if VERIFY_HEAP |
| 5157 | if (FLAG_verify_heap) { |
| 5158 | object->JSObjectVerify(); |
| 5159 | } |
| 5160 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5161 | } |
| 5162 | |
| 5163 | |
| 5164 | // static |
| 5165 | bool JSObject::TryMigrateInstance(Handle<JSObject> object) { |
| 5166 | Isolate* isolate = object->GetIsolate(); |
| 5167 | DisallowDeoptimization no_deoptimization(isolate); |
| 5168 | Handle<Map> original_map(object->map(), isolate); |
| 5169 | Handle<Map> new_map; |
| 5170 | if (!Map::TryUpdate(original_map).ToHandle(&new_map)) { |
| 5171 | return false; |
| 5172 | } |
| 5173 | JSObject::MigrateToMap(object, new_map); |
| 5174 | if (FLAG_trace_migration) { |
| 5175 | object->PrintInstanceMigration(stdout, *original_map, object->map()); |
| 5176 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5177 | #if VERIFY_HEAP |
| 5178 | if (FLAG_verify_heap) { |
| 5179 | object->JSObjectVerify(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5180 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5181 | #endif |
| 5182 | return true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5183 | } |
| 5184 | |
| 5185 | |
| 5186 | void JSObject::AddProperty(Handle<JSObject> object, Handle<Name> name, |
| 5187 | Handle<Object> value, |
| 5188 | PropertyAttributes attributes) { |
| 5189 | LookupIterator it(object, name, LookupIterator::OWN_SKIP_INTERCEPTOR); |
| 5190 | CHECK_NE(LookupIterator::ACCESS_CHECK, it.state()); |
| 5191 | #ifdef DEBUG |
| 5192 | uint32_t index; |
| 5193 | DCHECK(!object->IsJSProxy()); |
| 5194 | DCHECK(!name->AsArrayIndex(&index)); |
| 5195 | Maybe<PropertyAttributes> maybe = GetPropertyAttributes(&it); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5196 | DCHECK(maybe.IsJust()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5197 | DCHECK(!it.IsFound()); |
| 5198 | DCHECK(object->map()->is_extensible() || |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5199 | it.isolate()->IsInternallyUsedPropertyName(name)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5200 | #endif |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5201 | CHECK(AddDataProperty(&it, value, attributes, THROW_ON_ERROR, |
| 5202 | CERTAINLY_NOT_STORE_FROM_KEYED) |
| 5203 | .IsJust()); |
| 5204 | } |
| 5205 | |
| 5206 | |
| 5207 | // static |
| 5208 | void ExecutableAccessorInfo::ClearSetter(Handle<ExecutableAccessorInfo> info) { |
| 5209 | Handle<Object> object = v8::FromCData(info->GetIsolate(), nullptr); |
| 5210 | info->set_setter(*object); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5211 | } |
| 5212 | |
| 5213 | |
| 5214 | // Reconfigures a property to a data property with attributes, even if it is not |
| 5215 | // reconfigurable. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5216 | // Requires a LookupIterator that does not look at the prototype chain beyond |
| 5217 | // hidden prototypes. |
| 5218 | MaybeHandle<Object> JSObject::DefineOwnPropertyIgnoreAttributes( |
| 5219 | LookupIterator* it, Handle<Object> value, PropertyAttributes attributes, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5220 | ExecutableAccessorInfoHandling handling) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5221 | MAYBE_RETURN_NULL(DefineOwnPropertyIgnoreAttributes( |
| 5222 | it, value, attributes, THROW_ON_ERROR, handling)); |
| 5223 | return value; |
| 5224 | } |
| 5225 | |
| 5226 | |
| 5227 | Maybe<bool> JSObject::DefineOwnPropertyIgnoreAttributes( |
| 5228 | LookupIterator* it, Handle<Object> value, PropertyAttributes attributes, |
| 5229 | ShouldThrow should_throw, ExecutableAccessorInfoHandling handling) { |
| 5230 | Handle<JSObject> object = Handle<JSObject>::cast(it->GetReceiver()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5231 | bool is_observed = object->map()->is_observed() && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5232 | (it->IsElement() || |
| 5233 | !it->isolate()->IsInternallyUsedPropertyName(it->name())); |
| 5234 | |
| 5235 | for (; it->IsFound(); it->Next()) { |
| 5236 | switch (it->state()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5237 | case LookupIterator::JSPROXY: |
| 5238 | case LookupIterator::NOT_FOUND: |
| 5239 | case LookupIterator::TRANSITION: |
| 5240 | UNREACHABLE(); |
| 5241 | |
| 5242 | case LookupIterator::ACCESS_CHECK: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5243 | if (!it->HasAccess()) { |
| 5244 | it->isolate()->ReportFailedAccessCheck(it->GetHolder<JSObject>()); |
| 5245 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(it->isolate(), Nothing<bool>()); |
| 5246 | return Just(true); |
| 5247 | } |
| 5248 | break; |
| 5249 | |
| 5250 | // If there's an interceptor, try to store the property with the |
| 5251 | // interceptor. |
| 5252 | // In case of success, the attributes will have been reset to the default |
| 5253 | // attributes of the interceptor, rather than the incoming attributes. |
| 5254 | // |
| 5255 | // TODO(verwaest): JSProxy afterwards verify the attributes that the |
| 5256 | // JSProxy claims it has, and verifies that they are compatible. If not, |
| 5257 | // they throw. Here we should do the same. |
| 5258 | case LookupIterator::INTERCEPTOR: |
| 5259 | if (handling == DONT_FORCE_FIELD) { |
| 5260 | Maybe<bool> result = JSObject::SetPropertyWithInterceptor(it, value); |
| 5261 | if (result.IsNothing() || result.FromJust()) return result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5262 | } |
| 5263 | break; |
| 5264 | |
| 5265 | case LookupIterator::ACCESSOR: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5266 | Handle<Object> accessors = it->GetAccessors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5267 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5268 | // Special handling for ExecutableAccessorInfo, which behaves like a |
| 5269 | // data property. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5270 | if (accessors->IsExecutableAccessorInfo() && |
| 5271 | handling == DONT_FORCE_FIELD) { |
| 5272 | PropertyDetails details = it->property_details(); |
| 5273 | // Ensure the context isn't changed after calling into accessors. |
| 5274 | AssertNoContextChange ncc(it->isolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5275 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5276 | Maybe<bool> result = |
| 5277 | JSObject::SetPropertyWithAccessor(it, value, should_throw); |
| 5278 | if (result.IsNothing() || !result.FromJust()) return result; |
| 5279 | |
| 5280 | if (details.attributes() == attributes) return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5281 | |
| 5282 | // Reconfigure the accessor if attributes mismatch. |
| 5283 | Handle<ExecutableAccessorInfo> new_data = Accessors::CloneAccessor( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5284 | it->isolate(), Handle<ExecutableAccessorInfo>::cast(accessors)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5285 | new_data->set_property_attributes(attributes); |
| 5286 | // By clearing the setter we don't have to introduce a lookup to |
| 5287 | // the setter, simply make it unavailable to reflect the |
| 5288 | // attributes. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5289 | if (attributes & READ_ONLY) { |
| 5290 | ExecutableAccessorInfo::ClearSetter(new_data); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5291 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5292 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5293 | it->TransitionToAccessorPair(new_data, attributes); |
| 5294 | } else { |
| 5295 | it->ReconfigureDataProperty(value, attributes); |
| 5296 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5297 | |
| 5298 | if (is_observed) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5299 | RETURN_ON_EXCEPTION_VALUE( |
| 5300 | it->isolate(), |
| 5301 | EnqueueChangeRecord(object, "reconfigure", it->GetName(), |
| 5302 | it->factory()->the_hole_value()), |
| 5303 | Nothing<bool>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5304 | } |
| 5305 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5306 | return Just(true); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5307 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5308 | case LookupIterator::INTEGER_INDEXED_EXOTIC: |
| 5309 | return RedefineIncompatibleProperty(it->isolate(), it->GetName(), value, |
| 5310 | should_throw); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5311 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5312 | case LookupIterator::DATA: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5313 | PropertyDetails details = it->property_details(); |
| 5314 | Handle<Object> old_value = it->factory()->the_hole_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5315 | // Regular property update if the attributes match. |
| 5316 | if (details.attributes() == attributes) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5317 | return SetDataProperty(it, value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5318 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5319 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5320 | // Special case: properties of typed arrays cannot be reconfigured to |
| 5321 | // non-writable nor to non-enumerable. |
| 5322 | if (it->IsElement() && object->HasFixedTypedArrayElements()) { |
| 5323 | return RedefineIncompatibleProperty(it->isolate(), it->GetName(), |
| 5324 | value, should_throw); |
| 5325 | } |
| 5326 | |
| 5327 | // Reconfigure the data property if the attributes mismatch. |
| 5328 | if (is_observed) old_value = it->GetDataValue(); |
| 5329 | |
| 5330 | it->ReconfigureDataProperty(value, attributes); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5331 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5332 | if (is_observed) { |
| 5333 | if (old_value->SameValue(*value)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5334 | old_value = it->factory()->the_hole_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5335 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5336 | RETURN_ON_EXCEPTION_VALUE( |
| 5337 | it->isolate(), EnqueueChangeRecord(object, "reconfigure", |
| 5338 | it->GetName(), old_value), |
| 5339 | Nothing<bool>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5340 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5341 | return Just(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5342 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5343 | } |
| 5344 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5345 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5346 | return AddDataProperty(it, value, attributes, should_throw, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5347 | CERTAINLY_NOT_STORE_FROM_KEYED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5348 | } |
| 5349 | |
| 5350 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5351 | MaybeHandle<Object> JSObject::SetOwnPropertyIgnoreAttributes( |
| 5352 | Handle<JSObject> object, Handle<Name> name, Handle<Object> value, |
| 5353 | PropertyAttributes attributes, ExecutableAccessorInfoHandling handling) { |
| 5354 | DCHECK(!value->IsTheHole()); |
| 5355 | LookupIterator it(object, name, LookupIterator::OWN); |
| 5356 | return DefineOwnPropertyIgnoreAttributes(&it, value, attributes, handling); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5357 | } |
| 5358 | |
| 5359 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5360 | MaybeHandle<Object> JSObject::SetOwnElementIgnoreAttributes( |
| 5361 | Handle<JSObject> object, uint32_t index, Handle<Object> value, |
| 5362 | PropertyAttributes attributes, ExecutableAccessorInfoHandling handling) { |
| 5363 | Isolate* isolate = object->GetIsolate(); |
| 5364 | LookupIterator it(isolate, object, index, LookupIterator::OWN); |
| 5365 | return DefineOwnPropertyIgnoreAttributes(&it, value, attributes, handling); |
| 5366 | } |
| 5367 | |
| 5368 | |
| 5369 | MaybeHandle<Object> JSObject::DefinePropertyOrElementIgnoreAttributes( |
| 5370 | Handle<JSObject> object, Handle<Name> name, Handle<Object> value, |
| 5371 | PropertyAttributes attributes, ExecutableAccessorInfoHandling handling) { |
| 5372 | Isolate* isolate = object->GetIsolate(); |
| 5373 | LookupIterator it = LookupIterator::PropertyOrElement(isolate, object, name, |
| 5374 | LookupIterator::OWN); |
| 5375 | return DefineOwnPropertyIgnoreAttributes(&it, value, attributes, handling); |
| 5376 | } |
| 5377 | |
| 5378 | |
| 5379 | Maybe<PropertyAttributes> JSObject::GetPropertyAttributesWithInterceptor( |
| 5380 | LookupIterator* it) { |
| 5381 | Isolate* isolate = it->isolate(); |
| 5382 | // Make sure that the top context does not change when doing |
| 5383 | // callbacks or interceptor calls. |
| 5384 | AssertNoContextChange ncc(isolate); |
| 5385 | HandleScope scope(isolate); |
| 5386 | |
| 5387 | Handle<JSObject> holder = it->GetHolder<JSObject>(); |
| 5388 | Handle<InterceptorInfo> interceptor(it->GetInterceptor()); |
| 5389 | if (!it->IsElement() && it->name()->IsSymbol() && |
| 5390 | !interceptor->can_intercept_symbols()) { |
| 5391 | return Just(ABSENT); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5392 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5393 | PropertyCallbackArguments args(isolate, interceptor->data(), |
| 5394 | *it->GetReceiver(), *holder); |
| 5395 | if (!interceptor->query()->IsUndefined()) { |
| 5396 | v8::Local<v8::Integer> result; |
| 5397 | if (it->IsElement()) { |
| 5398 | uint32_t index = it->index(); |
| 5399 | v8::IndexedPropertyQueryCallback query = |
| 5400 | v8::ToCData<v8::IndexedPropertyQueryCallback>(interceptor->query()); |
| 5401 | LOG(isolate, |
| 5402 | ApiIndexedPropertyAccess("interceptor-indexed-has", *holder, index)); |
| 5403 | result = args.Call(query, index); |
| 5404 | } else { |
| 5405 | Handle<Name> name = it->name(); |
| 5406 | DCHECK(!name->IsPrivate()); |
| 5407 | v8::GenericNamedPropertyQueryCallback query = |
| 5408 | v8::ToCData<v8::GenericNamedPropertyQueryCallback>( |
| 5409 | interceptor->query()); |
| 5410 | LOG(isolate, |
| 5411 | ApiNamedPropertyAccess("interceptor-named-has", *holder, *name)); |
| 5412 | result = args.Call(query, v8::Utils::ToLocal(name)); |
| 5413 | } |
| 5414 | if (!result.IsEmpty()) { |
| 5415 | DCHECK(result->IsInt32()); |
| 5416 | return Just(static_cast<PropertyAttributes>( |
| 5417 | result->Int32Value(reinterpret_cast<v8::Isolate*>(isolate) |
| 5418 | ->GetCurrentContext()).FromJust())); |
| 5419 | } |
| 5420 | } else if (!interceptor->getter()->IsUndefined()) { |
| 5421 | // TODO(verwaest): Use GetPropertyWithInterceptor? |
| 5422 | v8::Local<v8::Value> result; |
| 5423 | if (it->IsElement()) { |
| 5424 | uint32_t index = it->index(); |
| 5425 | v8::IndexedPropertyGetterCallback getter = |
| 5426 | v8::ToCData<v8::IndexedPropertyGetterCallback>(interceptor->getter()); |
| 5427 | LOG(isolate, ApiIndexedPropertyAccess("interceptor-indexed-get-has", |
| 5428 | *holder, index)); |
| 5429 | result = args.Call(getter, index); |
| 5430 | } else { |
| 5431 | Handle<Name> name = it->name(); |
| 5432 | DCHECK(!name->IsPrivate()); |
| 5433 | v8::GenericNamedPropertyGetterCallback getter = |
| 5434 | v8::ToCData<v8::GenericNamedPropertyGetterCallback>( |
| 5435 | interceptor->getter()); |
| 5436 | LOG(isolate, |
| 5437 | ApiNamedPropertyAccess("interceptor-named-get-has", *holder, *name)); |
| 5438 | result = args.Call(getter, v8::Utils::ToLocal(name)); |
| 5439 | } |
| 5440 | if (!result.IsEmpty()) return Just(DONT_ENUM); |
| 5441 | } |
| 5442 | |
| 5443 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<PropertyAttributes>()); |
| 5444 | return Just(ABSENT); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5445 | } |
| 5446 | |
| 5447 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5448 | Maybe<PropertyAttributes> JSReceiver::GetPropertyAttributes( |
| 5449 | LookupIterator* it) { |
| 5450 | for (; it->IsFound(); it->Next()) { |
| 5451 | switch (it->state()) { |
| 5452 | case LookupIterator::NOT_FOUND: |
| 5453 | case LookupIterator::TRANSITION: |
| 5454 | UNREACHABLE(); |
| 5455 | case LookupIterator::JSPROXY: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5456 | return JSProxy::GetPropertyAttributes(it); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5457 | case LookupIterator::INTERCEPTOR: { |
| 5458 | Maybe<PropertyAttributes> result = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5459 | JSObject::GetPropertyAttributesWithInterceptor(it); |
| 5460 | if (!result.IsJust()) return result; |
| 5461 | if (result.FromJust() != ABSENT) return result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5462 | break; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5463 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5464 | case LookupIterator::ACCESS_CHECK: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5465 | if (it->HasAccess()) break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5466 | return JSObject::GetPropertyAttributesWithFailedAccessCheck(it); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5467 | case LookupIterator::INTEGER_INDEXED_EXOTIC: |
| 5468 | return Just(ABSENT); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5469 | case LookupIterator::ACCESSOR: |
| 5470 | case LookupIterator::DATA: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5471 | return Just(it->property_details().attributes()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5472 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5473 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5474 | return Just(ABSENT); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5475 | } |
| 5476 | |
| 5477 | |
| 5478 | Handle<NormalizedMapCache> NormalizedMapCache::New(Isolate* isolate) { |
| 5479 | Handle<FixedArray> array( |
| 5480 | isolate->factory()->NewFixedArray(kEntries, TENURED)); |
| 5481 | return Handle<NormalizedMapCache>::cast(array); |
| 5482 | } |
| 5483 | |
| 5484 | |
| 5485 | MaybeHandle<Map> NormalizedMapCache::Get(Handle<Map> fast_map, |
| 5486 | PropertyNormalizationMode mode) { |
| 5487 | DisallowHeapAllocation no_gc; |
| 5488 | Object* value = FixedArray::get(GetIndex(fast_map)); |
| 5489 | if (!value->IsMap() || |
| 5490 | !Map::cast(value)->EquivalentToForNormalization(*fast_map, mode)) { |
| 5491 | return MaybeHandle<Map>(); |
| 5492 | } |
| 5493 | return handle(Map::cast(value)); |
| 5494 | } |
| 5495 | |
| 5496 | |
| 5497 | void NormalizedMapCache::Set(Handle<Map> fast_map, |
| 5498 | Handle<Map> normalized_map) { |
| 5499 | DisallowHeapAllocation no_gc; |
| 5500 | DCHECK(normalized_map->is_dictionary_map()); |
| 5501 | FixedArray::set(GetIndex(fast_map), *normalized_map); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5502 | } |
| 5503 | |
| 5504 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5505 | void NormalizedMapCache::Clear() { |
| 5506 | int entries = length(); |
| 5507 | for (int i = 0; i != entries; i++) { |
| 5508 | set_undefined(i); |
| 5509 | } |
| 5510 | } |
| 5511 | |
| 5512 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5513 | void HeapObject::UpdateMapCodeCache(Handle<HeapObject> object, |
| 5514 | Handle<Name> name, |
| 5515 | Handle<Code> code) { |
| 5516 | Handle<Map> map(object->map()); |
| 5517 | Map::UpdateCodeCache(map, name, code); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5518 | } |
| 5519 | |
| 5520 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5521 | void JSObject::NormalizeProperties(Handle<JSObject> object, |
| 5522 | PropertyNormalizationMode mode, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5523 | int expected_additional_properties, |
| 5524 | const char* reason) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5525 | if (!object->HasFastProperties()) return; |
| 5526 | |
| 5527 | Handle<Map> map(object->map()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5528 | Handle<Map> new_map = Map::Normalize(map, mode, reason); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5529 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5530 | MigrateToMap(object, new_map, expected_additional_properties); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5531 | } |
| 5532 | |
| 5533 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5534 | void JSObject::MigrateFastToSlow(Handle<JSObject> object, |
| 5535 | Handle<Map> new_map, |
| 5536 | int expected_additional_properties) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5537 | // The global object is always normalized. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5538 | DCHECK(!object->IsJSGlobalObject()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 5539 | // JSGlobalProxy must never be normalized |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5540 | DCHECK(!object->IsJSGlobalProxy()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 5541 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5542 | Isolate* isolate = object->GetIsolate(); |
| 5543 | HandleScope scope(isolate); |
| 5544 | Handle<Map> map(object->map()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5545 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5546 | // Allocate new content. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5547 | int real_size = map->NumberOfOwnDescriptors(); |
| 5548 | int property_count = real_size; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5549 | if (expected_additional_properties > 0) { |
| 5550 | property_count += expected_additional_properties; |
| 5551 | } else { |
| 5552 | property_count += 2; // Make space for two more properties. |
| 5553 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5554 | Handle<NameDictionary> dictionary = |
| 5555 | NameDictionary::New(isolate, property_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5556 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5557 | Handle<DescriptorArray> descs(map->instance_descriptors()); |
| 5558 | for (int i = 0; i < real_size; i++) { |
| 5559 | PropertyDetails details = descs->GetDetails(i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5560 | Handle<Name> key(descs->GetKey(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5561 | switch (details.type()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5562 | case DATA_CONSTANT: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5563 | Handle<Object> value(descs->GetConstant(i), isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5564 | PropertyDetails d(details.attributes(), DATA, i + 1, |
| 5565 | PropertyCellType::kNoCell); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5566 | dictionary = NameDictionary::Add(dictionary, key, value, d); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5567 | break; |
| 5568 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5569 | case DATA: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5570 | FieldIndex index = FieldIndex::ForDescriptor(*map, i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5571 | Handle<Object> value; |
| 5572 | if (object->IsUnboxedDoubleField(index)) { |
| 5573 | double old_value = object->RawFastDoublePropertyAt(index); |
| 5574 | value = isolate->factory()->NewHeapNumber(old_value); |
| 5575 | } else { |
| 5576 | value = handle(object->RawFastPropertyAt(index), isolate); |
| 5577 | if (details.representation().IsDouble()) { |
| 5578 | DCHECK(value->IsMutableHeapNumber()); |
| 5579 | Handle<HeapNumber> old = Handle<HeapNumber>::cast(value); |
| 5580 | value = isolate->factory()->NewHeapNumber(old->value()); |
| 5581 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5582 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5583 | PropertyDetails d(details.attributes(), DATA, i + 1, |
| 5584 | PropertyCellType::kNoCell); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5585 | dictionary = NameDictionary::Add(dictionary, key, value, d); |
| 5586 | break; |
| 5587 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5588 | case ACCESSOR: { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5589 | FieldIndex index = FieldIndex::ForDescriptor(*map, i); |
| 5590 | Handle<Object> value(object->RawFastPropertyAt(index), isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5591 | PropertyDetails d(details.attributes(), ACCESSOR_CONSTANT, i + 1, |
| 5592 | PropertyCellType::kNoCell); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5593 | dictionary = NameDictionary::Add(dictionary, key, value, d); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5594 | break; |
| 5595 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5596 | case ACCESSOR_CONSTANT: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5597 | Handle<Object> value(descs->GetCallbacksObject(i), isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5598 | PropertyDetails d(details.attributes(), ACCESSOR_CONSTANT, i + 1, |
| 5599 | PropertyCellType::kNoCell); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5600 | dictionary = NameDictionary::Add(dictionary, key, value, d); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5601 | break; |
| 5602 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5603 | } |
| 5604 | } |
| 5605 | |
| 5606 | // Copy the next enumeration index from instance descriptor. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5607 | dictionary->SetNextEnumerationIndex(real_size + 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5608 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5609 | // From here on we cannot fail and we shouldn't GC anymore. |
| 5610 | DisallowHeapAllocation no_allocation; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5611 | |
| 5612 | // Resize the object in the heap if necessary. |
| 5613 | int new_instance_size = new_map->instance_size(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5614 | int instance_size_delta = map->instance_size() - new_instance_size; |
| 5615 | DCHECK(instance_size_delta >= 0); |
| 5616 | |
| 5617 | if (instance_size_delta > 0) { |
| 5618 | Heap* heap = isolate->heap(); |
| 5619 | heap->CreateFillerObjectAt(object->address() + new_instance_size, |
| 5620 | instance_size_delta); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5621 | heap->AdjustLiveBytes(*object, -instance_size_delta, |
| 5622 | Heap::CONCURRENT_TO_SWEEPER); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5623 | } |
| 5624 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5625 | // We are storing the new map using release store after creating a filler for |
| 5626 | // the left-over space to avoid races with the sweeper thread. |
| 5627 | object->synchronized_set_map(*new_map); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5628 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5629 | object->set_properties(*dictionary); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5630 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5631 | // Ensure that in-object space of slow-mode object does not contain random |
| 5632 | // garbage. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5633 | int inobject_properties = new_map->GetInObjectProperties(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5634 | for (int i = 0; i < inobject_properties; i++) { |
| 5635 | FieldIndex index = FieldIndex::ForPropertyIndex(*new_map, i); |
| 5636 | object->RawFastPropertyAtPut(index, Smi::FromInt(0)); |
| 5637 | } |
| 5638 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5639 | isolate->counters()->props_to_dictionary()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5640 | |
| 5641 | #ifdef DEBUG |
| 5642 | if (FLAG_trace_normalization) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5643 | OFStream os(stdout); |
| 5644 | os << "Object properties have been normalized:\n"; |
| 5645 | object->Print(os); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5646 | } |
| 5647 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5648 | } |
| 5649 | |
| 5650 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5651 | void JSObject::MigrateSlowToFast(Handle<JSObject> object, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5652 | int unused_property_fields, |
| 5653 | const char* reason) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5654 | if (object->HasFastProperties()) return; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5655 | DCHECK(!object->IsJSGlobalObject()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5656 | Isolate* isolate = object->GetIsolate(); |
| 5657 | Factory* factory = isolate->factory(); |
| 5658 | Handle<NameDictionary> dictionary(object->property_dictionary()); |
| 5659 | |
| 5660 | // Make sure we preserve dictionary representation if there are too many |
| 5661 | // descriptors. |
| 5662 | int number_of_elements = dictionary->NumberOfElements(); |
| 5663 | if (number_of_elements > kMaxNumberOfDescriptors) return; |
| 5664 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5665 | Handle<FixedArray> iteration_order; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5666 | if (number_of_elements != dictionary->NextEnumerationIndex()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5667 | iteration_order = |
| 5668 | NameDictionary::DoGenerateNewEnumerationIndices(dictionary); |
| 5669 | } else { |
| 5670 | iteration_order = NameDictionary::BuildIterationIndicesArray(dictionary); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5671 | } |
| 5672 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5673 | int instance_descriptor_length = iteration_order->length(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5674 | int number_of_fields = 0; |
| 5675 | |
| 5676 | // Compute the length of the instance descriptor. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5677 | for (int i = 0; i < instance_descriptor_length; i++) { |
| 5678 | int index = Smi::cast(iteration_order->get(i))->value(); |
| 5679 | DCHECK(dictionary->IsKey(dictionary->KeyAt(index))); |
| 5680 | |
| 5681 | Object* value = dictionary->ValueAt(index); |
| 5682 | PropertyType type = dictionary->DetailsAt(index).type(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5683 | if (type == DATA && !value->IsJSFunction()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5684 | number_of_fields += 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5685 | } |
| 5686 | } |
| 5687 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5688 | Handle<Map> old_map(object->map(), isolate); |
| 5689 | |
| 5690 | int inobject_props = old_map->GetInObjectProperties(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5691 | |
| 5692 | // Allocate new map. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5693 | Handle<Map> new_map = Map::CopyDropDescriptors(old_map); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5694 | new_map->set_dictionary_map(false); |
| 5695 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5696 | UpdatePrototypeUserRegistration(old_map, new_map, isolate); |
| 5697 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5698 | #if TRACE_MAPS |
| 5699 | if (FLAG_trace_maps) { |
| 5700 | PrintF("[TraceMaps: SlowToFast from= %p to= %p reason= %s ]\n", |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5701 | reinterpret_cast<void*>(*old_map), reinterpret_cast<void*>(*new_map), |
| 5702 | reason); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5703 | } |
| 5704 | #endif |
| 5705 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5706 | if (instance_descriptor_length == 0) { |
| 5707 | DisallowHeapAllocation no_gc; |
| 5708 | DCHECK_LE(unused_property_fields, inobject_props); |
| 5709 | // Transform the object. |
| 5710 | new_map->set_unused_property_fields(inobject_props); |
| 5711 | object->synchronized_set_map(*new_map); |
| 5712 | object->set_properties(isolate->heap()->empty_fixed_array()); |
| 5713 | // Check that it really works. |
| 5714 | DCHECK(object->HasFastProperties()); |
| 5715 | return; |
| 5716 | } |
| 5717 | |
| 5718 | // Allocate the instance descriptor. |
| 5719 | Handle<DescriptorArray> descriptors = DescriptorArray::Allocate( |
| 5720 | isolate, instance_descriptor_length); |
| 5721 | |
| 5722 | int number_of_allocated_fields = |
| 5723 | number_of_fields + unused_property_fields - inobject_props; |
| 5724 | if (number_of_allocated_fields < 0) { |
| 5725 | // There is enough inobject space for all fields (including unused). |
| 5726 | number_of_allocated_fields = 0; |
| 5727 | unused_property_fields = inobject_props - number_of_fields; |
| 5728 | } |
| 5729 | |
| 5730 | // Allocate the fixed array for the fields. |
| 5731 | Handle<FixedArray> fields = factory->NewFixedArray( |
| 5732 | number_of_allocated_fields); |
| 5733 | |
| 5734 | // Fill in the instance descriptor and the fields. |
| 5735 | int current_offset = 0; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5736 | for (int i = 0; i < instance_descriptor_length; i++) { |
| 5737 | int index = Smi::cast(iteration_order->get(i))->value(); |
| 5738 | Object* k = dictionary->KeyAt(index); |
| 5739 | DCHECK(dictionary->IsKey(k)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5740 | // Dictionary keys are internalized upon insertion. |
| 5741 | // TODO(jkummerow): Turn this into a DCHECK if it's not hit in the wild. |
| 5742 | CHECK(k->IsUniqueName()); |
| 5743 | Handle<Name> key(Name::cast(k), isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5744 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5745 | Object* value = dictionary->ValueAt(index); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5746 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5747 | PropertyDetails details = dictionary->DetailsAt(index); |
| 5748 | int enumeration_index = details.dictionary_index(); |
| 5749 | PropertyType type = details.type(); |
| 5750 | |
| 5751 | if (value->IsJSFunction()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5752 | DataConstantDescriptor d(key, handle(value, isolate), |
| 5753 | details.attributes()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5754 | descriptors->Set(enumeration_index - 1, &d); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5755 | } else if (type == DATA) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5756 | if (current_offset < inobject_props) { |
| 5757 | object->InObjectPropertyAtPut(current_offset, value, |
| 5758 | UPDATE_WRITE_BARRIER); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5759 | } else { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5760 | int offset = current_offset - inobject_props; |
| 5761 | fields->set(offset, value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5762 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5763 | DataDescriptor d(key, current_offset, details.attributes(), |
| 5764 | // TODO(verwaest): value->OptimalRepresentation(); |
| 5765 | Representation::Tagged()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5766 | current_offset += d.GetDetails().field_width_in_words(); |
| 5767 | descriptors->Set(enumeration_index - 1, &d); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5768 | } else if (type == ACCESSOR_CONSTANT) { |
| 5769 | AccessorConstantDescriptor d(key, handle(value, isolate), |
| 5770 | details.attributes()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5771 | descriptors->Set(enumeration_index - 1, &d); |
| 5772 | } else { |
| 5773 | UNREACHABLE(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5774 | } |
| 5775 | } |
| 5776 | DCHECK(current_offset == number_of_fields); |
| 5777 | |
| 5778 | descriptors->Sort(); |
| 5779 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5780 | Handle<LayoutDescriptor> layout_descriptor = LayoutDescriptor::New( |
| 5781 | new_map, descriptors, descriptors->number_of_descriptors()); |
| 5782 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5783 | DisallowHeapAllocation no_gc; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5784 | new_map->InitializeDescriptors(*descriptors, *layout_descriptor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5785 | new_map->set_unused_property_fields(unused_property_fields); |
| 5786 | |
| 5787 | // Transform the object. |
| 5788 | object->synchronized_set_map(*new_map); |
| 5789 | |
| 5790 | object->set_properties(*fields); |
| 5791 | DCHECK(object->IsJSObject()); |
| 5792 | |
| 5793 | // Check that it really works. |
| 5794 | DCHECK(object->HasFastProperties()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5795 | } |
| 5796 | |
| 5797 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5798 | void JSObject::ResetElements(Handle<JSObject> object) { |
| 5799 | Isolate* isolate = object->GetIsolate(); |
| 5800 | CHECK(object->map() != isolate->heap()->sloppy_arguments_elements_map()); |
| 5801 | if (object->map()->has_dictionary_elements()) { |
| 5802 | Handle<SeededNumberDictionary> new_elements = |
| 5803 | SeededNumberDictionary::New(isolate, 0); |
| 5804 | object->set_elements(*new_elements); |
| 5805 | } else { |
| 5806 | object->set_elements(object->map()->GetInitialElements()); |
| 5807 | } |
| 5808 | } |
| 5809 | |
| 5810 | |
| 5811 | static Handle<SeededNumberDictionary> CopyFastElementsToDictionary( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5812 | Handle<FixedArrayBase> array, int length, |
| 5813 | Handle<SeededNumberDictionary> dictionary, bool used_as_prototype) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5814 | Isolate* isolate = array->GetIsolate(); |
| 5815 | Factory* factory = isolate->factory(); |
| 5816 | bool has_double_elements = array->IsFixedDoubleArray(); |
| 5817 | for (int i = 0; i < length; i++) { |
| 5818 | Handle<Object> value; |
| 5819 | if (has_double_elements) { |
| 5820 | Handle<FixedDoubleArray> double_array = |
| 5821 | Handle<FixedDoubleArray>::cast(array); |
| 5822 | if (double_array->is_the_hole(i)) { |
| 5823 | value = factory->the_hole_value(); |
| 5824 | } else { |
| 5825 | value = factory->NewHeapNumber(double_array->get_scalar(i)); |
| 5826 | } |
| 5827 | } else { |
| 5828 | value = handle(Handle<FixedArray>::cast(array)->get(i), isolate); |
| 5829 | } |
| 5830 | if (!value->IsTheHole()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5831 | PropertyDetails details = PropertyDetails::Empty(); |
| 5832 | dictionary = SeededNumberDictionary::AddNumberEntry( |
| 5833 | dictionary, i, value, details, used_as_prototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5834 | } |
| 5835 | } |
| 5836 | return dictionary; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5837 | } |
| 5838 | |
| 5839 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5840 | void JSObject::RequireSlowElements(SeededNumberDictionary* dictionary) { |
| 5841 | if (dictionary->requires_slow_elements()) return; |
| 5842 | dictionary->set_requires_slow_elements(); |
| 5843 | // TODO(verwaest): Remove this hack. |
| 5844 | if (map()->is_prototype_map()) { |
| 5845 | TypeFeedbackVector::ClearAllKeyedStoreICs(GetIsolate()); |
| 5846 | } |
| 5847 | } |
| 5848 | |
| 5849 | |
| 5850 | Handle<SeededNumberDictionary> JSObject::GetNormalizedElementDictionary( |
| 5851 | Handle<JSObject> object, Handle<FixedArrayBase> elements) { |
| 5852 | DCHECK(!object->HasDictionaryElements()); |
| 5853 | DCHECK(!object->HasSlowArgumentsElements()); |
| 5854 | Isolate* isolate = object->GetIsolate(); |
| 5855 | // Ensure that notifications fire if the array or object prototypes are |
| 5856 | // normalizing. |
| 5857 | isolate->UpdateArrayProtectorOnNormalizeElements(object); |
| 5858 | int length = object->IsJSArray() |
| 5859 | ? Smi::cast(Handle<JSArray>::cast(object)->length())->value() |
| 5860 | : elements->length(); |
| 5861 | int used = object->GetFastElementsUsage(); |
| 5862 | Handle<SeededNumberDictionary> dictionary = |
| 5863 | SeededNumberDictionary::New(isolate, used); |
| 5864 | return CopyFastElementsToDictionary(elements, length, dictionary, |
| 5865 | object->map()->is_prototype_map()); |
| 5866 | } |
| 5867 | |
| 5868 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5869 | Handle<SeededNumberDictionary> JSObject::NormalizeElements( |
| 5870 | Handle<JSObject> object) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5871 | DCHECK(!object->HasFixedTypedArrayElements()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5872 | Isolate* isolate = object->GetIsolate(); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5873 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5874 | // Find the backing store. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5875 | Handle<FixedArrayBase> elements(object->elements(), isolate); |
| 5876 | bool is_arguments = object->HasSloppyArgumentsElements(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5877 | if (is_arguments) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5878 | FixedArray* parameter_map = FixedArray::cast(*elements); |
| 5879 | elements = handle(FixedArrayBase::cast(parameter_map->get(1)), isolate); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5880 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5881 | |
| 5882 | if (elements->IsDictionary()) { |
| 5883 | return Handle<SeededNumberDictionary>::cast(elements); |
| 5884 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5885 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5886 | DCHECK(object->HasFastSmiOrObjectElements() || |
| 5887 | object->HasFastDoubleElements() || |
| 5888 | object->HasFastArgumentsElements()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5889 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5890 | Handle<SeededNumberDictionary> dictionary = |
| 5891 | GetNormalizedElementDictionary(object, elements); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5892 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5893 | // Switch to using the dictionary as the backing storage for elements. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5894 | ElementsKind target_kind = |
| 5895 | is_arguments ? SLOW_SLOPPY_ARGUMENTS_ELEMENTS : DICTIONARY_ELEMENTS; |
| 5896 | Handle<Map> new_map = JSObject::GetElementsTransitionMap(object, target_kind); |
| 5897 | // Set the new map first to satify the elements type assert in set_elements(). |
| 5898 | JSObject::MigrateToMap(object, new_map); |
| 5899 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5900 | if (is_arguments) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5901 | FixedArray::cast(object->elements())->set(1, *dictionary); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5902 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5903 | object->set_elements(*dictionary); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5904 | } |
| 5905 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5906 | isolate->counters()->elements_to_dictionary()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5907 | |
| 5908 | #ifdef DEBUG |
| 5909 | if (FLAG_trace_normalization) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5910 | OFStream os(stdout); |
| 5911 | os << "Object elements have been normalized:\n"; |
| 5912 | object->Print(os); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5913 | } |
| 5914 | #endif |
| 5915 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5916 | DCHECK(object->HasDictionaryElements() || object->HasSlowArgumentsElements()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5917 | return dictionary; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5918 | } |
| 5919 | |
| 5920 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5921 | static Smi* GenerateIdentityHash(Isolate* isolate) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5922 | int hash_value; |
| 5923 | int attempts = 0; |
| 5924 | do { |
| 5925 | // Generate a random 32-bit hash value but limit range to fit |
| 5926 | // within a smi. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5927 | hash_value = isolate->random_number_generator()->NextInt() & Smi::kMaxValue; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5928 | attempts++; |
| 5929 | } while (hash_value == 0 && attempts < 30); |
| 5930 | hash_value = hash_value != 0 ? hash_value : 1; // never return 0 |
| 5931 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5932 | return Smi::FromInt(hash_value); |
| 5933 | } |
| 5934 | |
| 5935 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5936 | void JSObject::SetIdentityHash(Handle<JSObject> object, Handle<Smi> hash) { |
| 5937 | DCHECK(!object->IsJSGlobalProxy()); |
| 5938 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5939 | Handle<Name> hash_code_symbol(isolate->heap()->hash_code_symbol()); |
| 5940 | JSObject::AddProperty(object, hash_code_symbol, hash, NONE); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5941 | } |
| 5942 | |
| 5943 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5944 | template<typename ProxyType> |
| 5945 | static Handle<Smi> GetOrCreateIdentityHashHelper(Handle<ProxyType> proxy) { |
| 5946 | Isolate* isolate = proxy->GetIsolate(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5947 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5948 | Handle<Object> maybe_hash(proxy->hash(), isolate); |
| 5949 | if (maybe_hash->IsSmi()) return Handle<Smi>::cast(maybe_hash); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5950 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5951 | Handle<Smi> hash(GenerateIdentityHash(isolate), isolate); |
| 5952 | proxy->set_hash(*hash); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5953 | return hash; |
| 5954 | } |
| 5955 | |
| 5956 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5957 | Object* JSObject::GetIdentityHash() { |
| 5958 | DisallowHeapAllocation no_gc; |
| 5959 | Isolate* isolate = GetIsolate(); |
| 5960 | if (IsJSGlobalProxy()) { |
| 5961 | return JSGlobalProxy::cast(this)->hash(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5962 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5963 | Handle<Name> hash_code_symbol(isolate->heap()->hash_code_symbol()); |
| 5964 | Handle<Object> stored_value = |
| 5965 | Object::GetPropertyOrElement(Handle<Object>(this, isolate), |
| 5966 | hash_code_symbol).ToHandleChecked(); |
| 5967 | return stored_value->IsSmi() ? *stored_value |
| 5968 | : isolate->heap()->undefined_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5969 | } |
| 5970 | |
| 5971 | |
| 5972 | Handle<Smi> JSObject::GetOrCreateIdentityHash(Handle<JSObject> object) { |
| 5973 | if (object->IsJSGlobalProxy()) { |
| 5974 | return GetOrCreateIdentityHashHelper(Handle<JSGlobalProxy>::cast(object)); |
| 5975 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5976 | Isolate* isolate = object->GetIsolate(); |
| 5977 | |
| 5978 | Handle<Object> maybe_hash(object->GetIdentityHash(), isolate); |
| 5979 | if (maybe_hash->IsSmi()) return Handle<Smi>::cast(maybe_hash); |
| 5980 | |
| 5981 | Handle<Smi> hash(GenerateIdentityHash(isolate), isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 5982 | Handle<Name> hash_code_symbol(isolate->heap()->hash_code_symbol()); |
| 5983 | JSObject::AddProperty(object, hash_code_symbol, hash, NONE); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5984 | return hash; |
| 5985 | } |
| 5986 | |
| 5987 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5988 | Object* JSProxy::GetIdentityHash() { |
| 5989 | return this->hash(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5990 | } |
| 5991 | |
| 5992 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5993 | Handle<Smi> JSProxy::GetOrCreateIdentityHash(Handle<JSProxy> proxy) { |
| 5994 | return GetOrCreateIdentityHashHelper(proxy); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5995 | } |
| 5996 | |
| 5997 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5998 | Object* JSObject::GetHiddenProperty(Handle<Name> key) { |
| 5999 | DisallowHeapAllocation no_gc; |
| 6000 | DCHECK(key->IsUniqueName()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6001 | if (IsJSGlobalProxy()) { |
| 6002 | // For a proxy, use the prototype as target object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6003 | PrototypeIterator iter(GetIsolate(), this); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6004 | // If the proxy is detached, return undefined. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6005 | if (iter.IsAtEnd()) return GetHeap()->the_hole_value(); |
| 6006 | DCHECK(iter.GetCurrent()->IsJSGlobalObject()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6007 | return iter.GetCurrent<JSObject>()->GetHiddenProperty(key); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6008 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6009 | DCHECK(!IsJSGlobalProxy()); |
| 6010 | Object* inline_value = GetHiddenPropertiesHashTable(); |
| 6011 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6012 | if (inline_value->IsUndefined()) return GetHeap()->the_hole_value(); |
| 6013 | |
| 6014 | ObjectHashTable* hashtable = ObjectHashTable::cast(inline_value); |
| 6015 | Object* entry = hashtable->Lookup(key); |
| 6016 | return entry; |
| 6017 | } |
| 6018 | |
| 6019 | |
| 6020 | Handle<Object> JSObject::SetHiddenProperty(Handle<JSObject> object, |
| 6021 | Handle<Name> key, |
| 6022 | Handle<Object> value) { |
| 6023 | Isolate* isolate = object->GetIsolate(); |
| 6024 | |
| 6025 | DCHECK(key->IsUniqueName()); |
| 6026 | if (object->IsJSGlobalProxy()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6027 | // For a proxy, use the prototype as target object. |
| 6028 | PrototypeIterator iter(isolate, object); |
| 6029 | // If the proxy is detached, return undefined. |
| 6030 | if (iter.IsAtEnd()) return isolate->factory()->undefined_value(); |
| 6031 | DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6032 | return SetHiddenProperty(PrototypeIterator::GetCurrent<JSObject>(iter), key, |
| 6033 | value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6034 | } |
| 6035 | DCHECK(!object->IsJSGlobalProxy()); |
| 6036 | |
| 6037 | Handle<Object> inline_value(object->GetHiddenPropertiesHashTable(), isolate); |
| 6038 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6039 | Handle<ObjectHashTable> hashtable = |
| 6040 | GetOrCreateHiddenPropertiesHashtable(object); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6041 | |
| 6042 | // If it was found, check if the key is already in the dictionary. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6043 | Handle<ObjectHashTable> new_table = ObjectHashTable::Put(hashtable, key, |
| 6044 | value); |
| 6045 | if (*new_table != *hashtable) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6046 | // If adding the key expanded the dictionary (i.e., Add returned a new |
| 6047 | // dictionary), store it back to the object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6048 | SetHiddenPropertiesHashTable(object, new_table); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6049 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6050 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6051 | // Return this to mark success. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6052 | return object; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6053 | } |
| 6054 | |
| 6055 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6056 | void JSObject::DeleteHiddenProperty(Handle<JSObject> object, Handle<Name> key) { |
| 6057 | Isolate* isolate = object->GetIsolate(); |
| 6058 | DCHECK(key->IsUniqueName()); |
| 6059 | |
| 6060 | if (object->IsJSGlobalProxy()) { |
| 6061 | PrototypeIterator iter(isolate, object); |
| 6062 | if (iter.IsAtEnd()) return; |
| 6063 | DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6064 | return DeleteHiddenProperty(PrototypeIterator::GetCurrent<JSObject>(iter), |
| 6065 | key); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6066 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6067 | |
| 6068 | Object* inline_value = object->GetHiddenPropertiesHashTable(); |
| 6069 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6070 | if (inline_value->IsUndefined()) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6071 | |
| 6072 | Handle<ObjectHashTable> hashtable(ObjectHashTable::cast(inline_value)); |
| 6073 | bool was_present = false; |
| 6074 | ObjectHashTable::Remove(hashtable, key, &was_present); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6075 | } |
| 6076 | |
| 6077 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6078 | bool JSObject::HasHiddenProperties(Handle<JSObject> object) { |
| 6079 | Handle<Name> hidden = object->GetIsolate()->factory()->hidden_string(); |
| 6080 | LookupIterator it(object, hidden, LookupIterator::OWN_SKIP_INTERCEPTOR); |
| 6081 | Maybe<PropertyAttributes> maybe = GetPropertyAttributes(&it); |
| 6082 | // Cannot get an exception since the hidden_string isn't accessible to JS. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6083 | DCHECK(maybe.IsJust()); |
| 6084 | return maybe.FromJust() != ABSENT; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6085 | } |
| 6086 | |
| 6087 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6088 | Object* JSObject::GetHiddenPropertiesHashTable() { |
| 6089 | DCHECK(!IsJSGlobalProxy()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6090 | if (HasFastProperties()) { |
| 6091 | // If the object has fast properties, check whether the first slot |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6092 | // in the descriptor array matches the hidden string. Since the |
| 6093 | // hidden strings hash code is zero (and no other name has hash |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6094 | // code zero) it will always occupy the first entry if present. |
| 6095 | DescriptorArray* descriptors = this->map()->instance_descriptors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6096 | if (descriptors->number_of_descriptors() > 0) { |
| 6097 | int sorted_index = descriptors->GetSortedKeyIndex(0); |
| 6098 | if (descriptors->GetKey(sorted_index) == GetHeap()->hidden_string() && |
| 6099 | sorted_index < map()->NumberOfOwnDescriptors()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6100 | DCHECK(descriptors->GetType(sorted_index) == DATA); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6101 | DCHECK(descriptors->GetDetails(sorted_index).representation(). |
| 6102 | IsCompatibleForLoad(Representation::Tagged())); |
| 6103 | FieldIndex index = FieldIndex::ForDescriptor(this->map(), |
| 6104 | sorted_index); |
| 6105 | return this->RawFastPropertyAt(index); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6106 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6107 | return GetHeap()->undefined_value(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6108 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6109 | } else { |
| 6110 | return GetHeap()->undefined_value(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6111 | } |
| 6112 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6113 | Isolate* isolate = GetIsolate(); |
| 6114 | LookupIterator it(handle(this), isolate->factory()->hidden_string(), |
| 6115 | LookupIterator::OWN_SKIP_INTERCEPTOR); |
| 6116 | // Access check is always skipped for the hidden string anyways. |
| 6117 | return *GetDataProperty(&it); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6118 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6119 | } |
| 6120 | |
| 6121 | Handle<ObjectHashTable> JSObject::GetOrCreateHiddenPropertiesHashtable( |
| 6122 | Handle<JSObject> object) { |
| 6123 | Isolate* isolate = object->GetIsolate(); |
| 6124 | |
| 6125 | static const int kInitialCapacity = 4; |
| 6126 | Handle<Object> inline_value(object->GetHiddenPropertiesHashTable(), isolate); |
| 6127 | if (inline_value->IsHashTable()) { |
| 6128 | return Handle<ObjectHashTable>::cast(inline_value); |
| 6129 | } |
| 6130 | |
| 6131 | Handle<ObjectHashTable> hashtable = ObjectHashTable::New( |
| 6132 | isolate, kInitialCapacity, USE_CUSTOM_MINIMUM_CAPACITY); |
| 6133 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6134 | DCHECK(inline_value->IsUndefined()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6135 | SetHiddenPropertiesHashTable(object, hashtable); |
| 6136 | return hashtable; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6137 | } |
| 6138 | |
| 6139 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6140 | Handle<Object> JSObject::SetHiddenPropertiesHashTable(Handle<JSObject> object, |
| 6141 | Handle<Object> value) { |
| 6142 | DCHECK(!object->IsJSGlobalProxy()); |
| 6143 | Isolate* isolate = object->GetIsolate(); |
| 6144 | Handle<Name> name = isolate->factory()->hidden_string(); |
| 6145 | SetOwnPropertyIgnoreAttributes(object, name, value, DONT_ENUM).Assert(); |
| 6146 | return object; |
| 6147 | } |
| 6148 | |
| 6149 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6150 | Maybe<bool> JSObject::DeletePropertyWithInterceptor(LookupIterator* it) { |
| 6151 | Isolate* isolate = it->isolate(); |
| 6152 | // Make sure that the top context does not change when doing callbacks or |
| 6153 | // interceptor calls. |
| 6154 | AssertNoContextChange ncc(isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6155 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6156 | DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state()); |
| 6157 | Handle<InterceptorInfo> interceptor(it->GetInterceptor()); |
| 6158 | if (interceptor->deleter()->IsUndefined()) return Nothing<bool>(); |
| 6159 | |
| 6160 | Handle<JSObject> holder = it->GetHolder<JSObject>(); |
| 6161 | |
| 6162 | PropertyCallbackArguments args(isolate, interceptor->data(), |
| 6163 | *it->GetReceiver(), *holder); |
| 6164 | v8::Local<v8::Boolean> result; |
| 6165 | if (it->IsElement()) { |
| 6166 | uint32_t index = it->index(); |
| 6167 | v8::IndexedPropertyDeleterCallback deleter = |
| 6168 | v8::ToCData<v8::IndexedPropertyDeleterCallback>(interceptor->deleter()); |
| 6169 | LOG(isolate, |
| 6170 | ApiIndexedPropertyAccess("interceptor-indexed-delete", *holder, index)); |
| 6171 | result = args.Call(deleter, index); |
| 6172 | } else if (it->name()->IsSymbol() && !interceptor->can_intercept_symbols()) { |
| 6173 | return Nothing<bool>(); |
| 6174 | } else { |
| 6175 | Handle<Name> name = it->name(); |
| 6176 | DCHECK(!name->IsPrivate()); |
| 6177 | v8::GenericNamedPropertyDeleterCallback deleter = |
| 6178 | v8::ToCData<v8::GenericNamedPropertyDeleterCallback>( |
| 6179 | interceptor->deleter()); |
| 6180 | LOG(isolate, |
| 6181 | ApiNamedPropertyAccess("interceptor-named-delete", *holder, *name)); |
| 6182 | result = args.Call(deleter, v8::Utils::ToLocal(name)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6183 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6184 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6185 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); |
| 6186 | if (result.IsEmpty()) return Nothing<bool>(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6187 | |
| 6188 | DCHECK(result->IsBoolean()); |
| 6189 | Handle<Object> result_internal = v8::Utils::OpenHandle(*result); |
| 6190 | result_internal->VerifyApiCallResultType(); |
| 6191 | // Rebox CustomArguments::kReturnValueOffset before returning. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6192 | return Just(result_internal->BooleanValue()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6193 | } |
| 6194 | |
| 6195 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6196 | void JSReceiver::DeleteNormalizedProperty(Handle<JSReceiver> object, |
| 6197 | Handle<Name> name, int entry) { |
| 6198 | DCHECK(!object->HasFastProperties()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6199 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6200 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6201 | if (object->IsJSGlobalObject()) { |
| 6202 | // If we have a global object, invalidate the cell and swap in a new one. |
| 6203 | Handle<GlobalDictionary> dictionary( |
| 6204 | JSObject::cast(*object)->global_dictionary()); |
| 6205 | DCHECK_NE(GlobalDictionary::kNotFound, entry); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6206 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6207 | auto cell = PropertyCell::InvalidateEntry(dictionary, entry); |
| 6208 | cell->set_value(isolate->heap()->the_hole_value()); |
| 6209 | // TODO(ishell): InvalidateForDelete |
| 6210 | cell->set_property_details( |
| 6211 | cell->property_details().set_cell_type(PropertyCellType::kInvalidated)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6212 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6213 | Handle<NameDictionary> dictionary(object->property_dictionary()); |
| 6214 | DCHECK_NE(NameDictionary::kNotFound, entry); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6215 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6216 | NameDictionary::DeleteProperty(dictionary, entry); |
| 6217 | Handle<NameDictionary> new_properties = |
| 6218 | NameDictionary::Shrink(dictionary, name); |
| 6219 | object->set_properties(*new_properties); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6220 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6221 | } |
| 6222 | |
| 6223 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6224 | Maybe<bool> JSReceiver::DeleteProperty(LookupIterator* it, |
| 6225 | LanguageMode language_mode) { |
| 6226 | Isolate* isolate = it->isolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6227 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6228 | if (it->state() == LookupIterator::JSPROXY) { |
| 6229 | return JSProxy::DeletePropertyOrElement(it->GetHolder<JSProxy>(), |
| 6230 | it->GetName(), language_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6231 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6232 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6233 | if (it->GetReceiver()->IsJSProxy()) { |
| 6234 | if (it->state() != LookupIterator::NOT_FOUND) { |
| 6235 | DCHECK_EQ(LookupIterator::DATA, it->state()); |
| 6236 | DCHECK(it->GetName()->IsPrivate()); |
| 6237 | it->Delete(); |
| 6238 | } |
| 6239 | return Just(true); |
| 6240 | } |
| 6241 | Handle<JSObject> receiver = Handle<JSObject>::cast(it->GetReceiver()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6242 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6243 | bool is_observed = |
| 6244 | receiver->map()->is_observed() && |
| 6245 | (it->IsElement() || !isolate->IsInternallyUsedPropertyName(it->name())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6246 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6247 | Handle<Object> old_value = it->factory()->the_hole_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6248 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6249 | for (; it->IsFound(); it->Next()) { |
| 6250 | switch (it->state()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6251 | case LookupIterator::JSPROXY: |
| 6252 | case LookupIterator::NOT_FOUND: |
| 6253 | case LookupIterator::TRANSITION: |
| 6254 | UNREACHABLE(); |
| 6255 | case LookupIterator::ACCESS_CHECK: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6256 | if (it->HasAccess()) break; |
| 6257 | isolate->ReportFailedAccessCheck(it->GetHolder<JSObject>()); |
| 6258 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); |
| 6259 | return Just(false); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6260 | case LookupIterator::INTERCEPTOR: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6261 | Maybe<bool> result = JSObject::DeletePropertyWithInterceptor(it); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6262 | // An exception was thrown in the interceptor. Propagate. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6263 | if (isolate->has_pending_exception()) return Nothing<bool>(); |
| 6264 | // Delete with interceptor succeeded. Return result. |
| 6265 | // TODO(neis): In strict mode, we should probably throw if the |
| 6266 | // interceptor returns false. |
| 6267 | if (result.IsJust()) return result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6268 | break; |
| 6269 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6270 | case LookupIterator::INTEGER_INDEXED_EXOTIC: |
| 6271 | return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6272 | case LookupIterator::DATA: |
| 6273 | if (is_observed) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6274 | old_value = it->GetDataValue(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6275 | } |
| 6276 | // Fall through. |
| 6277 | case LookupIterator::ACCESSOR: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6278 | if (!it->IsConfigurable() || receiver->map()->is_strong()) { |
| 6279 | // Fail if the property is not configurable, or on a strong object. |
| 6280 | if (is_strict(language_mode)) { |
| 6281 | MessageTemplate::Template templ = |
| 6282 | receiver->map()->is_strong() |
| 6283 | ? MessageTemplate::kStrongDeleteProperty |
| 6284 | : MessageTemplate::kStrictDeleteProperty; |
| 6285 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 6286 | templ, it->GetName(), receiver)); |
| 6287 | return Nothing<bool>(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6288 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6289 | return Just(false); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6290 | } |
| 6291 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6292 | it->Delete(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6293 | |
| 6294 | if (is_observed) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6295 | RETURN_ON_EXCEPTION_VALUE( |
| 6296 | isolate, JSObject::EnqueueChangeRecord(receiver, "delete", |
| 6297 | it->GetName(), old_value), |
| 6298 | Nothing<bool>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6299 | } |
| 6300 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6301 | return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6302 | } |
| 6303 | } |
| 6304 | } |
| 6305 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6306 | return Just(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6307 | } |
| 6308 | |
| 6309 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6310 | Maybe<bool> JSReceiver::DeleteElement(Handle<JSReceiver> object, uint32_t index, |
| 6311 | LanguageMode language_mode) { |
| 6312 | LookupIterator it(object->GetIsolate(), object, index, |
| 6313 | LookupIterator::HIDDEN); |
| 6314 | return DeleteProperty(&it, language_mode); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6315 | } |
| 6316 | |
| 6317 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6318 | Maybe<bool> JSReceiver::DeleteProperty(Handle<JSReceiver> object, |
| 6319 | Handle<Name> name, |
| 6320 | LanguageMode language_mode) { |
| 6321 | LookupIterator it(object, name, LookupIterator::HIDDEN); |
| 6322 | return DeleteProperty(&it, language_mode); |
| 6323 | } |
| 6324 | |
| 6325 | |
| 6326 | Maybe<bool> JSReceiver::DeletePropertyOrElement(Handle<JSReceiver> object, |
| 6327 | Handle<Name> name, |
| 6328 | LanguageMode language_mode) { |
| 6329 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 6330 | name->GetIsolate(), object, name, LookupIterator::HIDDEN); |
| 6331 | return DeleteProperty(&it, language_mode); |
| 6332 | } |
| 6333 | |
| 6334 | |
| 6335 | // ES6 7.1.14 |
| 6336 | MaybeHandle<Object> ToPropertyKey(Isolate* isolate, Handle<Object> value) { |
| 6337 | // 1. Let key be ToPrimitive(argument, hint String). |
| 6338 | MaybeHandle<Object> maybe_key = |
| 6339 | Object::ToPrimitive(value, ToPrimitiveHint::kString); |
| 6340 | // 2. ReturnIfAbrupt(key). |
| 6341 | Handle<Object> key; |
| 6342 | if (!maybe_key.ToHandle(&key)) return key; |
| 6343 | // 3. If Type(key) is Symbol, then return key. |
| 6344 | if (key->IsSymbol()) return key; |
| 6345 | // 4. Return ToString(key). |
| 6346 | // Extending spec'ed behavior, we'd be happy to return an element index. |
| 6347 | if (key->IsSmi()) return key; |
| 6348 | if (key->IsHeapNumber()) { |
| 6349 | uint32_t uint_value; |
| 6350 | if (value->ToArrayLength(&uint_value) && |
| 6351 | uint_value <= static_cast<uint32_t>(Smi::kMaxValue)) { |
| 6352 | return handle(Smi::FromInt(static_cast<int>(uint_value)), isolate); |
| 6353 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6354 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 6355 | return Object::ToString(isolate, key); |
| 6356 | } |
| 6357 | |
| 6358 | |
| 6359 | // ES6 19.1.2.4 |
| 6360 | // static |
| 6361 | Object* JSReceiver::DefineProperty(Isolate* isolate, Handle<Object> object, |
| 6362 | Handle<Object> key, |
| 6363 | Handle<Object> attributes) { |
| 6364 | // 1. If Type(O) is not Object, throw a TypeError exception. |
| 6365 | if (!object->IsJSReceiver()) { |
| 6366 | Handle<String> fun_name = |
| 6367 | isolate->factory()->InternalizeUtf8String("Object.defineProperty"); |
| 6368 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 6369 | isolate, NewTypeError(MessageTemplate::kCalledOnNonObject, fun_name)); |
| 6370 | } |
| 6371 | // 2. Let key be ToPropertyKey(P). |
| 6372 | // 3. ReturnIfAbrupt(key). |
| 6373 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, key, ToPropertyKey(isolate, key)); |
| 6374 | // 4. Let desc be ToPropertyDescriptor(Attributes). |
| 6375 | // 5. ReturnIfAbrupt(desc). |
| 6376 | PropertyDescriptor desc; |
| 6377 | if (!PropertyDescriptor::ToPropertyDescriptor(isolate, attributes, &desc)) { |
| 6378 | return isolate->heap()->exception(); |
| 6379 | } |
| 6380 | // 6. Let success be DefinePropertyOrThrow(O,key, desc). |
| 6381 | Maybe<bool> success = DefineOwnProperty( |
| 6382 | isolate, Handle<JSReceiver>::cast(object), key, &desc, THROW_ON_ERROR); |
| 6383 | // 7. ReturnIfAbrupt(success). |
| 6384 | MAYBE_RETURN(success, isolate->heap()->exception()); |
| 6385 | CHECK(success.FromJust()); |
| 6386 | // 8. Return O. |
| 6387 | return *object; |
| 6388 | } |
| 6389 | |
| 6390 | |
| 6391 | // ES6 19.1.2.3.1 |
| 6392 | // static |
| 6393 | MaybeHandle<Object> JSReceiver::DefineProperties(Isolate* isolate, |
| 6394 | Handle<Object> object, |
| 6395 | Handle<Object> properties) { |
| 6396 | // 1. If Type(O) is not Object, throw a TypeError exception. |
| 6397 | if (!object->IsJSReceiver()) { |
| 6398 | Handle<String> fun_name = |
| 6399 | isolate->factory()->InternalizeUtf8String("Object.defineProperties"); |
| 6400 | THROW_NEW_ERROR(isolate, |
| 6401 | NewTypeError(MessageTemplate::kCalledOnNonObject, fun_name), |
| 6402 | Object); |
| 6403 | } |
| 6404 | // 2. Let props be ToObject(Properties). |
| 6405 | // 3. ReturnIfAbrupt(props). |
| 6406 | Handle<JSReceiver> props; |
| 6407 | if (!Object::ToObject(isolate, properties).ToHandle(&props)) { |
| 6408 | THROW_NEW_ERROR(isolate, |
| 6409 | NewTypeError(MessageTemplate::kUndefinedOrNullToObject), |
| 6410 | Object); |
| 6411 | } |
| 6412 | // 4. Let keys be props.[[OwnPropertyKeys]](). |
| 6413 | // 5. ReturnIfAbrupt(keys). |
| 6414 | Handle<FixedArray> keys; |
| 6415 | ASSIGN_RETURN_ON_EXCEPTION( |
| 6416 | isolate, keys, |
| 6417 | JSReceiver::GetKeys(props, JSReceiver::OWN_ONLY, ALL_PROPERTIES), Object); |
| 6418 | // 6. Let descriptors be an empty List. |
| 6419 | int capacity = keys->length(); |
| 6420 | std::vector<PropertyDescriptor> descriptors(capacity); |
| 6421 | size_t descriptors_index = 0; |
| 6422 | // 7. Repeat for each element nextKey of keys in List order, |
| 6423 | for (int i = 0; i < keys->length(); ++i) { |
| 6424 | Handle<Object> next_key(keys->get(i), isolate); |
| 6425 | // 7a. Let propDesc be props.[[GetOwnProperty]](nextKey). |
| 6426 | // 7b. ReturnIfAbrupt(propDesc). |
| 6427 | bool success = false; |
| 6428 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 6429 | isolate, props, next_key, &success, LookupIterator::HIDDEN); |
| 6430 | DCHECK(success); |
| 6431 | Maybe<PropertyAttributes> maybe = JSReceiver::GetPropertyAttributes(&it); |
| 6432 | if (!maybe.IsJust()) return MaybeHandle<Object>(); |
| 6433 | PropertyAttributes attrs = maybe.FromJust(); |
| 6434 | // 7c. If propDesc is not undefined and propDesc.[[Enumerable]] is true: |
| 6435 | if (attrs == ABSENT) continue; |
| 6436 | if (attrs & DONT_ENUM) continue; |
| 6437 | // 7c i. Let descObj be Get(props, nextKey). |
| 6438 | // 7c ii. ReturnIfAbrupt(descObj). |
| 6439 | Handle<Object> desc_obj; |
| 6440 | ASSIGN_RETURN_ON_EXCEPTION(isolate, desc_obj, Object::GetProperty(&it), |
| 6441 | Object); |
| 6442 | // 7c iii. Let desc be ToPropertyDescriptor(descObj). |
| 6443 | success = PropertyDescriptor::ToPropertyDescriptor( |
| 6444 | isolate, desc_obj, &descriptors[descriptors_index]); |
| 6445 | // 7c iv. ReturnIfAbrupt(desc). |
| 6446 | if (!success) return MaybeHandle<Object>(); |
| 6447 | // 7c v. Append the pair (a two element List) consisting of nextKey and |
| 6448 | // desc to the end of descriptors. |
| 6449 | descriptors[descriptors_index].set_name(next_key); |
| 6450 | descriptors_index++; |
| 6451 | } |
| 6452 | // 8. For each pair from descriptors in list order, |
| 6453 | for (size_t i = 0; i < descriptors_index; ++i) { |
| 6454 | PropertyDescriptor* desc = &descriptors[i]; |
| 6455 | // 8a. Let P be the first element of pair. |
| 6456 | // 8b. Let desc be the second element of pair. |
| 6457 | // 8c. Let status be DefinePropertyOrThrow(O, P, desc). |
| 6458 | Maybe<bool> status = |
| 6459 | DefineOwnProperty(isolate, Handle<JSReceiver>::cast(object), |
| 6460 | desc->name(), desc, THROW_ON_ERROR); |
| 6461 | // 8d. ReturnIfAbrupt(status). |
| 6462 | if (!status.IsJust()) return MaybeHandle<Object>(); |
| 6463 | CHECK(status.FromJust()); |
| 6464 | } |
| 6465 | // 9. Return o. |
| 6466 | return object; |
| 6467 | } |
| 6468 | |
| 6469 | |
| 6470 | // static |
| 6471 | Maybe<bool> JSReceiver::DefineOwnProperty(Isolate* isolate, |
| 6472 | Handle<JSReceiver> object, |
| 6473 | Handle<Object> key, |
| 6474 | PropertyDescriptor* desc, |
| 6475 | ShouldThrow should_throw) { |
| 6476 | if (object->IsJSArray()) { |
| 6477 | return JSArray::DefineOwnProperty(isolate, Handle<JSArray>::cast(object), |
| 6478 | key, desc, should_throw); |
| 6479 | } |
| 6480 | if (object->IsJSProxy()) { |
| 6481 | return JSProxy::DefineOwnProperty(isolate, Handle<JSProxy>::cast(object), |
| 6482 | key, desc, should_throw); |
| 6483 | } |
| 6484 | // TODO(jkummerow): Support Modules (ES6 9.4.6.6) |
| 6485 | |
| 6486 | // OrdinaryDefineOwnProperty, by virtue of calling |
| 6487 | // DefineOwnPropertyIgnoreAttributes, can handle arguments (ES6 9.4.4.2) |
| 6488 | // and IntegerIndexedExotics (ES6 9.4.5.3), with one exception: |
| 6489 | // TODO(jkummerow): Setting an indexed accessor on a typed array should throw. |
| 6490 | return OrdinaryDefineOwnProperty(isolate, Handle<JSObject>::cast(object), key, |
| 6491 | desc, should_throw); |
| 6492 | } |
| 6493 | |
| 6494 | |
| 6495 | // static |
| 6496 | Maybe<bool> JSReceiver::OrdinaryDefineOwnProperty(Isolate* isolate, |
| 6497 | Handle<JSObject> object, |
| 6498 | Handle<Object> key, |
| 6499 | PropertyDescriptor* desc, |
| 6500 | ShouldThrow should_throw) { |
| 6501 | bool success = false; |
| 6502 | DCHECK(key->IsName() || key->IsNumber()); // |key| is a PropertyKey... |
| 6503 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 6504 | isolate, object, key, &success, LookupIterator::HIDDEN); |
| 6505 | DCHECK(success); // ...so creating a LookupIterator can't fail. |
| 6506 | |
| 6507 | // Deal with access checks first. |
| 6508 | if (it.state() == LookupIterator::ACCESS_CHECK) { |
| 6509 | if (!it.HasAccess()) { |
| 6510 | isolate->ReportFailedAccessCheck(it.GetHolder<JSObject>()); |
| 6511 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); |
| 6512 | return Just(true); |
| 6513 | } |
| 6514 | it.Next(); |
| 6515 | } |
| 6516 | |
| 6517 | return OrdinaryDefineOwnProperty(&it, desc, should_throw); |
| 6518 | } |
| 6519 | |
| 6520 | |
| 6521 | // ES6 9.1.6.1 |
| 6522 | // static |
| 6523 | Maybe<bool> JSReceiver::OrdinaryDefineOwnProperty(LookupIterator* it, |
| 6524 | PropertyDescriptor* desc, |
| 6525 | ShouldThrow should_throw) { |
| 6526 | Isolate* isolate = it->isolate(); |
| 6527 | // 1. Let current be O.[[GetOwnProperty]](P). |
| 6528 | // 2. ReturnIfAbrupt(current). |
| 6529 | PropertyDescriptor current; |
| 6530 | MAYBE_RETURN(GetOwnPropertyDescriptor(it, ¤t), Nothing<bool>()); |
| 6531 | |
| 6532 | // TODO(jkummerow/verwaest): It would be nice if we didn't have to reset |
| 6533 | // the iterator every time. Currently, the reasons why we need it are: |
| 6534 | // - handle interceptors correctly |
| 6535 | // - handle accessors correctly (which might change the holder's map) |
| 6536 | it->Restart(); |
| 6537 | // 3. Let extensible be the value of the [[Extensible]] internal slot of O. |
| 6538 | Handle<JSObject> object = Handle<JSObject>::cast(it->GetReceiver()); |
| 6539 | bool extensible = JSObject::IsExtensible(object); |
| 6540 | |
| 6541 | return ValidateAndApplyPropertyDescriptor(isolate, it, extensible, desc, |
| 6542 | ¤t, should_throw); |
| 6543 | } |
| 6544 | |
| 6545 | |
| 6546 | // ES6 9.1.6.2 |
| 6547 | // static |
| 6548 | Maybe<bool> JSReceiver::IsCompatiblePropertyDescriptor( |
| 6549 | Isolate* isolate, bool extensible, PropertyDescriptor* desc, |
| 6550 | PropertyDescriptor* current, Handle<Name> property_name, |
| 6551 | ShouldThrow should_throw) { |
| 6552 | // 1. Return ValidateAndApplyPropertyDescriptor(undefined, undefined, |
| 6553 | // Extensible, Desc, Current). |
| 6554 | return ValidateAndApplyPropertyDescriptor( |
| 6555 | isolate, NULL, extensible, desc, current, should_throw, property_name); |
| 6556 | } |
| 6557 | |
| 6558 | |
| 6559 | // ES6 9.1.6.3 |
| 6560 | // static |
| 6561 | Maybe<bool> JSReceiver::ValidateAndApplyPropertyDescriptor( |
| 6562 | Isolate* isolate, LookupIterator* it, bool extensible, |
| 6563 | PropertyDescriptor* desc, PropertyDescriptor* current, |
| 6564 | ShouldThrow should_throw, Handle<Name> property_name) { |
| 6565 | // We either need a LookupIterator, or a property name. |
| 6566 | DCHECK((it == NULL) != property_name.is_null()); |
| 6567 | Handle<JSObject> object; |
| 6568 | if (it != NULL) object = Handle<JSObject>::cast(it->GetReceiver()); |
| 6569 | bool desc_is_data_descriptor = PropertyDescriptor::IsDataDescriptor(desc); |
| 6570 | bool desc_is_accessor_descriptor = |
| 6571 | PropertyDescriptor::IsAccessorDescriptor(desc); |
| 6572 | bool desc_is_generic_descriptor = |
| 6573 | PropertyDescriptor::IsGenericDescriptor(desc); |
| 6574 | // 1. (Assert) |
| 6575 | // 2. If current is undefined, then |
| 6576 | if (current->is_empty()) { |
| 6577 | // 2a. If extensible is false, return false. |
| 6578 | if (!extensible) { |
| 6579 | RETURN_FAILURE(isolate, should_throw, |
| 6580 | NewTypeError(MessageTemplate::kDefineDisallowed, |
| 6581 | it != NULL ? it->GetName() : property_name)); |
| 6582 | } |
| 6583 | // 2c. If IsGenericDescriptor(Desc) or IsDataDescriptor(Desc) is true, then: |
| 6584 | // (This is equivalent to !IsAccessorDescriptor(desc).) |
| 6585 | DCHECK((desc_is_generic_descriptor || desc_is_data_descriptor) == |
| 6586 | !desc_is_accessor_descriptor); |
| 6587 | if (!desc_is_accessor_descriptor) { |
| 6588 | // 2c i. If O is not undefined, create an own data property named P of |
| 6589 | // object O whose [[Value]], [[Writable]], [[Enumerable]] and |
| 6590 | // [[Configurable]] attribute values are described by Desc. If the value |
| 6591 | // of an attribute field of Desc is absent, the attribute of the newly |
| 6592 | // created property is set to its default value. |
| 6593 | if (it != NULL) { |
| 6594 | if (!desc->has_writable()) desc->set_writable(false); |
| 6595 | if (!desc->has_enumerable()) desc->set_enumerable(false); |
| 6596 | if (!desc->has_configurable()) desc->set_configurable(false); |
| 6597 | Handle<Object> value( |
| 6598 | desc->has_value() |
| 6599 | ? desc->value() |
| 6600 | : Handle<Object>::cast(isolate->factory()->undefined_value())); |
| 6601 | MaybeHandle<Object> result = |
| 6602 | JSObject::DefineOwnPropertyIgnoreAttributes( |
| 6603 | it, value, desc->ToAttributes(), JSObject::DONT_FORCE_FIELD); |
| 6604 | if (result.is_null()) return Nothing<bool>(); |
| 6605 | } |
| 6606 | } else { |
| 6607 | // 2d. Else Desc must be an accessor Property Descriptor, |
| 6608 | DCHECK(desc_is_accessor_descriptor); |
| 6609 | // 2d i. If O is not undefined, create an own accessor property named P |
| 6610 | // of object O whose [[Get]], [[Set]], [[Enumerable]] and |
| 6611 | // [[Configurable]] attribute values are described by Desc. If the value |
| 6612 | // of an attribute field of Desc is absent, the attribute of the newly |
| 6613 | // created property is set to its default value. |
| 6614 | if (it != NULL) { |
| 6615 | if (!desc->has_enumerable()) desc->set_enumerable(false); |
| 6616 | if (!desc->has_configurable()) desc->set_configurable(false); |
| 6617 | Handle<Object> getter( |
| 6618 | desc->has_get() |
| 6619 | ? desc->get() |
| 6620 | : Handle<Object>::cast(isolate->factory()->null_value())); |
| 6621 | Handle<Object> setter( |
| 6622 | desc->has_set() |
| 6623 | ? desc->set() |
| 6624 | : Handle<Object>::cast(isolate->factory()->null_value())); |
| 6625 | MaybeHandle<Object> result = |
| 6626 | JSObject::DefineAccessor(it, getter, setter, desc->ToAttributes()); |
| 6627 | if (result.is_null()) return Nothing<bool>(); |
| 6628 | } |
| 6629 | } |
| 6630 | // 2e. Return true. |
| 6631 | return Just(true); |
| 6632 | } |
| 6633 | // 3. Return true, if every field in Desc is absent. |
| 6634 | // 4. Return true, if every field in Desc also occurs in current and the |
| 6635 | // value of every field in Desc is the same value as the corresponding field |
| 6636 | // in current when compared using the SameValue algorithm. |
| 6637 | if ((!desc->has_enumerable() || |
| 6638 | desc->enumerable() == current->enumerable()) && |
| 6639 | (!desc->has_configurable() || |
| 6640 | desc->configurable() == current->configurable()) && |
| 6641 | (!desc->has_value() || |
| 6642 | (current->has_value() && current->value()->SameValue(*desc->value()))) && |
| 6643 | (!desc->has_writable() || |
| 6644 | (current->has_writable() && current->writable() == desc->writable())) && |
| 6645 | (!desc->has_get() || |
| 6646 | (current->has_get() && current->get()->SameValue(*desc->get()))) && |
| 6647 | (!desc->has_set() || |
| 6648 | (current->has_set() && current->set()->SameValue(*desc->set())))) { |
| 6649 | return Just(true); |
| 6650 | } |
| 6651 | // 5. If the [[Configurable]] field of current is false, then |
| 6652 | if (!current->configurable()) { |
| 6653 | // 5a. Return false, if the [[Configurable]] field of Desc is true. |
| 6654 | if (desc->has_configurable() && desc->configurable()) { |
| 6655 | RETURN_FAILURE(isolate, should_throw, |
| 6656 | NewTypeError(MessageTemplate::kRedefineDisallowed, |
| 6657 | it != NULL ? it->GetName() : property_name)); |
| 6658 | } |
| 6659 | // 5b. Return false, if the [[Enumerable]] field of Desc is present and the |
| 6660 | // [[Enumerable]] fields of current and Desc are the Boolean negation of |
| 6661 | // each other. |
| 6662 | if (desc->has_enumerable() && desc->enumerable() != current->enumerable()) { |
| 6663 | RETURN_FAILURE(isolate, should_throw, |
| 6664 | NewTypeError(MessageTemplate::kRedefineDisallowed, |
| 6665 | it != NULL ? it->GetName() : property_name)); |
| 6666 | } |
| 6667 | } |
| 6668 | |
| 6669 | bool current_is_data_descriptor = |
| 6670 | PropertyDescriptor::IsDataDescriptor(current); |
| 6671 | // 6. If IsGenericDescriptor(Desc) is true, no further validation is required. |
| 6672 | if (desc_is_generic_descriptor) { |
| 6673 | // Nothing to see here. |
| 6674 | |
| 6675 | // 7. Else if IsDataDescriptor(current) and IsDataDescriptor(Desc) have |
| 6676 | // different results, then: |
| 6677 | } else if (current_is_data_descriptor != desc_is_data_descriptor) { |
| 6678 | // 7a. Return false, if the [[Configurable]] field of current is false. |
| 6679 | if (!current->configurable()) { |
| 6680 | RETURN_FAILURE(isolate, should_throw, |
| 6681 | NewTypeError(MessageTemplate::kRedefineDisallowed, |
| 6682 | it != NULL ? it->GetName() : property_name)); |
| 6683 | } |
| 6684 | // 7b. If IsDataDescriptor(current) is true, then: |
| 6685 | if (current_is_data_descriptor) { |
| 6686 | // 7b i. If O is not undefined, convert the property named P of object O |
| 6687 | // from a data property to an accessor property. Preserve the existing |
| 6688 | // values of the converted property's [[Configurable]] and [[Enumerable]] |
| 6689 | // attributes and set the rest of the property's attributes to their |
| 6690 | // default values. |
| 6691 | // --> Folded into step 10. |
| 6692 | } else { |
| 6693 | // 7c i. If O is not undefined, convert the property named P of object O |
| 6694 | // from an accessor property to a data property. Preserve the existing |
| 6695 | // values of the converted property’s [[Configurable]] and [[Enumerable]] |
| 6696 | // attributes and set the rest of the property’s attributes to their |
| 6697 | // default values. |
| 6698 | // --> Folded into step 10. |
| 6699 | } |
| 6700 | |
| 6701 | // 8. Else if IsDataDescriptor(current) and IsDataDescriptor(Desc) are both |
| 6702 | // true, then: |
| 6703 | } else if (current_is_data_descriptor && desc_is_data_descriptor) { |
| 6704 | // 8a. If the [[Configurable]] field of current is false, then: |
| 6705 | if (!current->configurable()) { |
| 6706 | // [Strong mode] Disallow changing writable -> readonly for |
| 6707 | // non-configurable properties. |
| 6708 | if (it != NULL && current->writable() && desc->has_writable() && |
| 6709 | !desc->writable() && object->map()->is_strong()) { |
| 6710 | RETURN_FAILURE(isolate, should_throw, |
| 6711 | NewTypeError(MessageTemplate::kStrongRedefineDisallowed, |
| 6712 | object, it->GetName())); |
| 6713 | } |
| 6714 | // 8a i. Return false, if the [[Writable]] field of current is false and |
| 6715 | // the [[Writable]] field of Desc is true. |
| 6716 | if (!current->writable() && desc->has_writable() && desc->writable()) { |
| 6717 | RETURN_FAILURE( |
| 6718 | isolate, should_throw, |
| 6719 | NewTypeError(MessageTemplate::kRedefineDisallowed, |
| 6720 | it != NULL ? it->GetName() : property_name)); |
| 6721 | } |
| 6722 | // 8a ii. If the [[Writable]] field of current is false, then: |
| 6723 | if (!current->writable()) { |
| 6724 | // 8a ii 1. Return false, if the [[Value]] field of Desc is present and |
| 6725 | // SameValue(Desc.[[Value]], current.[[Value]]) is false. |
| 6726 | if (desc->has_value() && !desc->value()->SameValue(*current->value())) { |
| 6727 | RETURN_FAILURE( |
| 6728 | isolate, should_throw, |
| 6729 | NewTypeError(MessageTemplate::kRedefineDisallowed, |
| 6730 | it != NULL ? it->GetName() : property_name)); |
| 6731 | } |
| 6732 | } |
| 6733 | } |
| 6734 | } else { |
| 6735 | // 9. Else IsAccessorDescriptor(current) and IsAccessorDescriptor(Desc) |
| 6736 | // are both true, |
| 6737 | DCHECK(PropertyDescriptor::IsAccessorDescriptor(current) && |
| 6738 | desc_is_accessor_descriptor); |
| 6739 | // 9a. If the [[Configurable]] field of current is false, then: |
| 6740 | if (!current->configurable()) { |
| 6741 | // 9a i. Return false, if the [[Set]] field of Desc is present and |
| 6742 | // SameValue(Desc.[[Set]], current.[[Set]]) is false. |
| 6743 | if (desc->has_set() && !desc->set()->SameValue(*current->set())) { |
| 6744 | RETURN_FAILURE( |
| 6745 | isolate, should_throw, |
| 6746 | NewTypeError(MessageTemplate::kRedefineDisallowed, |
| 6747 | it != NULL ? it->GetName() : property_name)); |
| 6748 | } |
| 6749 | // 9a ii. Return false, if the [[Get]] field of Desc is present and |
| 6750 | // SameValue(Desc.[[Get]], current.[[Get]]) is false. |
| 6751 | if (desc->has_get() && !desc->get()->SameValue(*current->get())) { |
| 6752 | RETURN_FAILURE( |
| 6753 | isolate, should_throw, |
| 6754 | NewTypeError(MessageTemplate::kRedefineDisallowed, |
| 6755 | it != NULL ? it->GetName() : property_name)); |
| 6756 | } |
| 6757 | } |
| 6758 | } |
| 6759 | |
| 6760 | // 10. If O is not undefined, then: |
| 6761 | if (it != NULL) { |
| 6762 | // 10a. For each field of Desc that is present, set the corresponding |
| 6763 | // attribute of the property named P of object O to the value of the field. |
| 6764 | PropertyAttributes attrs = NONE; |
| 6765 | |
| 6766 | if (desc->has_enumerable()) { |
| 6767 | attrs = static_cast<PropertyAttributes>( |
| 6768 | attrs | (desc->enumerable() ? NONE : DONT_ENUM)); |
| 6769 | } else { |
| 6770 | attrs = static_cast<PropertyAttributes>( |
| 6771 | attrs | (current->enumerable() ? NONE : DONT_ENUM)); |
| 6772 | } |
| 6773 | if (desc->has_configurable()) { |
| 6774 | attrs = static_cast<PropertyAttributes>( |
| 6775 | attrs | (desc->configurable() ? NONE : DONT_DELETE)); |
| 6776 | } else { |
| 6777 | attrs = static_cast<PropertyAttributes>( |
| 6778 | attrs | (current->configurable() ? NONE : DONT_DELETE)); |
| 6779 | } |
| 6780 | if (desc_is_data_descriptor || |
| 6781 | (desc_is_generic_descriptor && current_is_data_descriptor)) { |
| 6782 | if (desc->has_writable()) { |
| 6783 | attrs = static_cast<PropertyAttributes>( |
| 6784 | attrs | (desc->writable() ? NONE : READ_ONLY)); |
| 6785 | } else { |
| 6786 | attrs = static_cast<PropertyAttributes>( |
| 6787 | attrs | (current->writable() ? NONE : READ_ONLY)); |
| 6788 | } |
| 6789 | Handle<Object> value( |
| 6790 | desc->has_value() ? desc->value() |
| 6791 | : current->has_value() |
| 6792 | ? current->value() |
| 6793 | : Handle<Object>::cast( |
| 6794 | isolate->factory()->undefined_value())); |
| 6795 | MaybeHandle<Object> result = JSObject::DefineOwnPropertyIgnoreAttributes( |
| 6796 | it, value, attrs, JSObject::DONT_FORCE_FIELD); |
| 6797 | if (result.is_null()) return Nothing<bool>(); |
| 6798 | } else { |
| 6799 | DCHECK(desc_is_accessor_descriptor || |
| 6800 | (desc_is_generic_descriptor && |
| 6801 | PropertyDescriptor::IsAccessorDescriptor(current))); |
| 6802 | Handle<Object> getter( |
| 6803 | desc->has_get() |
| 6804 | ? desc->get() |
| 6805 | : current->has_get() |
| 6806 | ? current->get() |
| 6807 | : Handle<Object>::cast(isolate->factory()->null_value())); |
| 6808 | Handle<Object> setter( |
| 6809 | desc->has_set() |
| 6810 | ? desc->set() |
| 6811 | : current->has_set() |
| 6812 | ? current->set() |
| 6813 | : Handle<Object>::cast(isolate->factory()->null_value())); |
| 6814 | MaybeHandle<Object> result = |
| 6815 | JSObject::DefineAccessor(it, getter, setter, attrs); |
| 6816 | if (result.is_null()) return Nothing<bool>(); |
| 6817 | } |
| 6818 | } |
| 6819 | |
| 6820 | // 11. Return true. |
| 6821 | return Just(true); |
| 6822 | } |
| 6823 | |
| 6824 | |
| 6825 | // static |
| 6826 | Maybe<bool> JSReceiver::CreateDataProperty(LookupIterator* it, |
| 6827 | Handle<Object> value, |
| 6828 | ShouldThrow should_throw) { |
| 6829 | DCHECK(!it->check_prototype_chain()); |
| 6830 | Handle<JSReceiver> receiver = Handle<JSReceiver>::cast(it->GetReceiver()); |
| 6831 | Isolate* isolate = receiver->GetIsolate(); |
| 6832 | |
| 6833 | if (receiver->IsJSObject()) { |
| 6834 | return JSObject::CreateDataProperty(it, value); // Shortcut. |
| 6835 | } |
| 6836 | |
| 6837 | PropertyDescriptor new_desc; |
| 6838 | new_desc.set_value(value); |
| 6839 | new_desc.set_writable(true); |
| 6840 | new_desc.set_enumerable(true); |
| 6841 | new_desc.set_configurable(true); |
| 6842 | |
| 6843 | return JSReceiver::DefineOwnProperty(isolate, receiver, it->GetName(), |
| 6844 | &new_desc, should_throw); |
| 6845 | } |
| 6846 | |
| 6847 | |
| 6848 | Maybe<bool> JSObject::CreateDataProperty(LookupIterator* it, |
| 6849 | Handle<Object> value) { |
| 6850 | DCHECK(it->GetReceiver()->IsJSObject()); |
| 6851 | MAYBE_RETURN(JSReceiver::GetPropertyAttributes(it), Nothing<bool>()); |
| 6852 | |
| 6853 | if (it->IsFound()) { |
| 6854 | if (!it->IsConfigurable()) return Just(false); |
| 6855 | } else { |
| 6856 | if (!JSObject::IsExtensible(Handle<JSObject>::cast(it->GetReceiver()))) |
| 6857 | return Just(false); |
| 6858 | } |
| 6859 | |
| 6860 | RETURN_ON_EXCEPTION_VALUE( |
| 6861 | it->isolate(), |
| 6862 | DefineOwnPropertyIgnoreAttributes(it, value, NONE, DONT_FORCE_FIELD), |
| 6863 | Nothing<bool>()); |
| 6864 | |
| 6865 | return Just(true); |
| 6866 | } |
| 6867 | |
| 6868 | |
| 6869 | // TODO(jkummerow): Consider unification with FastAsArrayLength() in |
| 6870 | // accessors.cc. |
| 6871 | bool PropertyKeyToArrayLength(Handle<Object> value, uint32_t* length) { |
| 6872 | DCHECK(value->IsNumber() || value->IsName()); |
| 6873 | if (value->ToArrayLength(length)) return true; |
| 6874 | if (value->IsString()) return String::cast(*value)->AsArrayIndex(length); |
| 6875 | return false; |
| 6876 | } |
| 6877 | |
| 6878 | |
| 6879 | bool PropertyKeyToArrayIndex(Handle<Object> index_obj, uint32_t* output) { |
| 6880 | return PropertyKeyToArrayLength(index_obj, output) && *output != kMaxUInt32; |
| 6881 | } |
| 6882 | |
| 6883 | |
| 6884 | // ES6 9.4.2.1 |
| 6885 | // static |
| 6886 | Maybe<bool> JSArray::DefineOwnProperty(Isolate* isolate, Handle<JSArray> o, |
| 6887 | Handle<Object> name, |
| 6888 | PropertyDescriptor* desc, |
| 6889 | ShouldThrow should_throw) { |
| 6890 | // 1. Assert: IsPropertyKey(P) is true. ("P" is |name|.) |
| 6891 | // 2. If P is "length", then: |
| 6892 | // TODO(jkummerow): Check if we need slow string comparison. |
| 6893 | if (*name == isolate->heap()->length_string()) { |
| 6894 | // 2a. Return ArraySetLength(A, Desc). |
| 6895 | return ArraySetLength(isolate, o, desc, should_throw); |
| 6896 | } |
| 6897 | // 3. Else if P is an array index, then: |
| 6898 | uint32_t index = 0; |
| 6899 | if (PropertyKeyToArrayIndex(name, &index)) { |
| 6900 | // 3a. Let oldLenDesc be OrdinaryGetOwnProperty(A, "length"). |
| 6901 | PropertyDescriptor old_len_desc; |
| 6902 | Maybe<bool> success = GetOwnPropertyDescriptor( |
| 6903 | isolate, o, isolate->factory()->length_string(), &old_len_desc); |
| 6904 | // 3b. (Assert) |
| 6905 | DCHECK(success.FromJust()); |
| 6906 | USE(success); |
| 6907 | // 3c. Let oldLen be oldLenDesc.[[Value]]. |
| 6908 | uint32_t old_len = 0; |
| 6909 | CHECK(old_len_desc.value()->ToArrayLength(&old_len)); |
| 6910 | // 3d. Let index be ToUint32(P). |
| 6911 | // (Already done above.) |
| 6912 | // 3e. (Assert) |
| 6913 | // 3f. If index >= oldLen and oldLenDesc.[[Writable]] is false, |
| 6914 | // return false. |
| 6915 | if (index >= old_len && old_len_desc.has_writable() && |
| 6916 | !old_len_desc.writable()) { |
| 6917 | RETURN_FAILURE(isolate, should_throw, |
| 6918 | NewTypeError(MessageTemplate::kDefineDisallowed, name)); |
| 6919 | } |
| 6920 | // 3g. Let succeeded be OrdinaryDefineOwnProperty(A, P, Desc). |
| 6921 | Maybe<bool> succeeded = |
| 6922 | OrdinaryDefineOwnProperty(isolate, o, name, desc, should_throw); |
| 6923 | // 3h. Assert: succeeded is not an abrupt completion. |
| 6924 | // In our case, if should_throw == THROW_ON_ERROR, it can be! |
| 6925 | // 3i. If succeeded is false, return false. |
| 6926 | if (succeeded.IsNothing() || !succeeded.FromJust()) return succeeded; |
| 6927 | // 3j. If index >= oldLen, then: |
| 6928 | if (index >= old_len) { |
| 6929 | // 3j i. Set oldLenDesc.[[Value]] to index + 1. |
| 6930 | old_len_desc.set_value(isolate->factory()->NewNumberFromUint(index + 1)); |
| 6931 | // 3j ii. Let succeeded be |
| 6932 | // OrdinaryDefineOwnProperty(A, "length", oldLenDesc). |
| 6933 | succeeded = OrdinaryDefineOwnProperty(isolate, o, |
| 6934 | isolate->factory()->length_string(), |
| 6935 | &old_len_desc, should_throw); |
| 6936 | // 3j iii. Assert: succeeded is true. |
| 6937 | DCHECK(succeeded.FromJust()); |
| 6938 | USE(succeeded); |
| 6939 | } |
| 6940 | // 3k. Return true. |
| 6941 | return Just(true); |
| 6942 | } |
| 6943 | |
| 6944 | // 4. Return OrdinaryDefineOwnProperty(A, P, Desc). |
| 6945 | return OrdinaryDefineOwnProperty(isolate, o, name, desc, should_throw); |
| 6946 | } |
| 6947 | |
| 6948 | |
| 6949 | // Part of ES6 9.4.2.4 ArraySetLength. |
| 6950 | // static |
| 6951 | bool JSArray::AnythingToArrayLength(Isolate* isolate, |
| 6952 | Handle<Object> length_object, |
| 6953 | uint32_t* output) { |
| 6954 | // Fast path: check numbers and strings that can be converted directly |
| 6955 | // and unobservably. |
| 6956 | if (length_object->ToArrayLength(output)) return true; |
| 6957 | if (length_object->IsString() && |
| 6958 | Handle<String>::cast(length_object)->AsArrayIndex(output)) { |
| 6959 | return true; |
| 6960 | } |
| 6961 | // Slow path: follow steps in ES6 9.4.2.4 "ArraySetLength". |
| 6962 | // 3. Let newLen be ToUint32(Desc.[[Value]]). |
| 6963 | Handle<Object> uint32_v; |
| 6964 | if (!Object::ToUint32(isolate, length_object).ToHandle(&uint32_v)) { |
| 6965 | // 4. ReturnIfAbrupt(newLen). |
| 6966 | return false; |
| 6967 | } |
| 6968 | // 5. Let numberLen be ToNumber(Desc.[[Value]]). |
| 6969 | Handle<Object> number_v; |
| 6970 | if (!Object::ToNumber(length_object).ToHandle(&number_v)) { |
| 6971 | // 6. ReturnIfAbrupt(newLen). |
| 6972 | return false; |
| 6973 | } |
| 6974 | // 7. If newLen != numberLen, throw a RangeError exception. |
| 6975 | if (uint32_v->Number() != number_v->Number()) { |
| 6976 | Handle<Object> exception = |
| 6977 | isolate->factory()->NewRangeError(MessageTemplate::kInvalidArrayLength); |
| 6978 | isolate->Throw(*exception); |
| 6979 | return false; |
| 6980 | } |
| 6981 | CHECK(uint32_v->ToArrayLength(output)); |
| 6982 | return true; |
| 6983 | } |
| 6984 | |
| 6985 | |
| 6986 | // ES6 9.4.2.4 |
| 6987 | // static |
| 6988 | Maybe<bool> JSArray::ArraySetLength(Isolate* isolate, Handle<JSArray> a, |
| 6989 | PropertyDescriptor* desc, |
| 6990 | ShouldThrow should_throw) { |
| 6991 | // 1. If the [[Value]] field of Desc is absent, then |
| 6992 | if (!desc->has_value()) { |
| 6993 | // 1a. Return OrdinaryDefineOwnProperty(A, "length", Desc). |
| 6994 | return OrdinaryDefineOwnProperty( |
| 6995 | isolate, a, isolate->factory()->length_string(), desc, should_throw); |
| 6996 | } |
| 6997 | // 2. Let newLenDesc be a copy of Desc. |
| 6998 | // (Actual copying is not necessary.) |
| 6999 | PropertyDescriptor* new_len_desc = desc; |
| 7000 | // 3. - 7. Convert Desc.[[Value]] to newLen. |
| 7001 | uint32_t new_len = 0; |
| 7002 | if (!AnythingToArrayLength(isolate, desc->value(), &new_len)) { |
| 7003 | DCHECK(isolate->has_pending_exception()); |
| 7004 | return Nothing<bool>(); |
| 7005 | } |
| 7006 | // 8. Set newLenDesc.[[Value]] to newLen. |
| 7007 | // (Done below, if needed.) |
| 7008 | // 9. Let oldLenDesc be OrdinaryGetOwnProperty(A, "length"). |
| 7009 | PropertyDescriptor old_len_desc; |
| 7010 | Maybe<bool> success = GetOwnPropertyDescriptor( |
| 7011 | isolate, a, isolate->factory()->length_string(), &old_len_desc); |
| 7012 | // 10. (Assert) |
| 7013 | DCHECK(success.FromJust()); |
| 7014 | USE(success); |
| 7015 | // 11. Let oldLen be oldLenDesc.[[Value]]. |
| 7016 | uint32_t old_len = 0; |
| 7017 | CHECK(old_len_desc.value()->ToArrayLength(&old_len)); |
| 7018 | // 12. If newLen >= oldLen, then |
| 7019 | if (new_len >= old_len) { |
| 7020 | // 8. Set newLenDesc.[[Value]] to newLen. |
| 7021 | // 12a. Return OrdinaryDefineOwnProperty(A, "length", newLenDesc). |
| 7022 | new_len_desc->set_value(isolate->factory()->NewNumberFromUint(new_len)); |
| 7023 | return OrdinaryDefineOwnProperty(isolate, a, |
| 7024 | isolate->factory()->length_string(), |
| 7025 | new_len_desc, should_throw); |
| 7026 | } |
| 7027 | // 13. If oldLenDesc.[[Writable]] is false, return false. |
| 7028 | if (!old_len_desc.writable()) { |
| 7029 | RETURN_FAILURE(isolate, should_throw, |
| 7030 | NewTypeError(MessageTemplate::kRedefineDisallowed, |
| 7031 | isolate->factory()->length_string())); |
| 7032 | } |
| 7033 | // 14. If newLenDesc.[[Writable]] is absent or has the value true, |
| 7034 | // let newWritable be true. |
| 7035 | bool new_writable = false; |
| 7036 | if (!new_len_desc->has_writable() || new_len_desc->writable()) { |
| 7037 | new_writable = true; |
| 7038 | } else { |
| 7039 | // 15. Else, |
| 7040 | // 15a. Need to defer setting the [[Writable]] attribute to false in case |
| 7041 | // any elements cannot be deleted. |
| 7042 | // 15b. Let newWritable be false. (It's initialized as "false" anyway.) |
| 7043 | // 15c. Set newLenDesc.[[Writable]] to true. |
| 7044 | // (Not needed.) |
| 7045 | } |
| 7046 | // Most of steps 16 through 19 is implemented by JSArray::SetLength. |
| 7047 | if (JSArray::ObservableSetLength(a, new_len).is_null()) { |
| 7048 | DCHECK(isolate->has_pending_exception()); |
| 7049 | return Nothing<bool>(); |
| 7050 | } |
| 7051 | // Steps 19d-ii, 20. |
| 7052 | if (!new_writable) { |
| 7053 | PropertyDescriptor readonly; |
| 7054 | readonly.set_writable(false); |
| 7055 | Maybe<bool> success = OrdinaryDefineOwnProperty( |
| 7056 | isolate, a, isolate->factory()->length_string(), &readonly, |
| 7057 | should_throw); |
| 7058 | DCHECK(success.FromJust()); |
| 7059 | USE(success); |
| 7060 | } |
| 7061 | uint32_t actual_new_len = 0; |
| 7062 | CHECK(a->length()->ToArrayLength(&actual_new_len)); |
| 7063 | // Steps 19d-v, 21. Return false if there were non-deletable elements. |
| 7064 | bool result = actual_new_len == new_len; |
| 7065 | if (!result) { |
| 7066 | RETURN_FAILURE( |
| 7067 | isolate, should_throw, |
| 7068 | NewTypeError(MessageTemplate::kStrictDeleteProperty, |
| 7069 | isolate->factory()->NewNumberFromUint(actual_new_len - 1), |
| 7070 | a)); |
| 7071 | } |
| 7072 | return Just(result); |
| 7073 | } |
| 7074 | |
| 7075 | |
| 7076 | // ES6 9.5.6 |
| 7077 | // static |
| 7078 | Maybe<bool> JSProxy::DefineOwnProperty(Isolate* isolate, Handle<JSProxy> proxy, |
| 7079 | Handle<Object> key, |
| 7080 | PropertyDescriptor* desc, |
| 7081 | ShouldThrow should_throw) { |
| 7082 | STACK_CHECK(Nothing<bool>()); |
| 7083 | if (key->IsSymbol() && Handle<Symbol>::cast(key)->IsPrivate()) { |
| 7084 | return AddPrivateProperty(isolate, proxy, Handle<Symbol>::cast(key), desc, |
| 7085 | should_throw); |
| 7086 | } |
| 7087 | Handle<String> trap_name = isolate->factory()->defineProperty_string(); |
| 7088 | // 1. Assert: IsPropertyKey(P) is true. |
| 7089 | DCHECK(key->IsName() || key->IsNumber()); |
| 7090 | // 2. Let handler be the value of the [[ProxyHandler]] internal slot of O. |
| 7091 | Handle<Object> handler(proxy->handler(), isolate); |
| 7092 | // 3. If handler is null, throw a TypeError exception. |
| 7093 | // 4. Assert: Type(handler) is Object. |
| 7094 | if (proxy->IsRevoked()) { |
| 7095 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7096 | MessageTemplate::kProxyRevoked, trap_name)); |
| 7097 | return Nothing<bool>(); |
| 7098 | } |
| 7099 | // 5. Let target be the value of the [[ProxyTarget]] internal slot of O. |
| 7100 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 7101 | // 6. Let trap be ? GetMethod(handler, "defineProperty"). |
| 7102 | Handle<Object> trap; |
| 7103 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7104 | isolate, trap, |
| 7105 | Object::GetMethod(Handle<JSReceiver>::cast(handler), trap_name), |
| 7106 | Nothing<bool>()); |
| 7107 | // 7. If trap is undefined, then: |
| 7108 | if (trap->IsUndefined()) { |
| 7109 | // 7a. Return target.[[DefineOwnProperty]](P, Desc). |
| 7110 | return JSReceiver::DefineOwnProperty(isolate, target, key, desc, |
| 7111 | should_throw); |
| 7112 | } |
| 7113 | // 8. Let descObj be FromPropertyDescriptor(Desc). |
| 7114 | Handle<Object> desc_obj = desc->ToObject(isolate); |
| 7115 | // 9. Let booleanTrapResult be |
| 7116 | // ToBoolean(? Call(trap, handler, «target, P, descObj»)). |
| 7117 | Handle<Name> property_name = |
| 7118 | key->IsName() |
| 7119 | ? Handle<Name>::cast(key) |
| 7120 | : Handle<Name>::cast(isolate->factory()->NumberToString(key)); |
| 7121 | // Do not leak private property names. |
| 7122 | DCHECK(!property_name->IsPrivate()); |
| 7123 | Handle<Object> trap_result_obj; |
| 7124 | Handle<Object> args[] = {target, property_name, desc_obj}; |
| 7125 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7126 | isolate, trap_result_obj, |
| 7127 | Execution::Call(isolate, trap, handler, arraysize(args), args), |
| 7128 | Nothing<bool>()); |
| 7129 | // 10. If booleanTrapResult is false, return false. |
| 7130 | if (!trap_result_obj->BooleanValue()) { |
| 7131 | RETURN_FAILURE(isolate, should_throw, |
| 7132 | NewTypeError(MessageTemplate::kProxyTrapReturnedFalsishFor, |
| 7133 | trap_name, property_name)); |
| 7134 | } |
| 7135 | // 11. Let targetDesc be ? target.[[GetOwnProperty]](P). |
| 7136 | PropertyDescriptor target_desc; |
| 7137 | Maybe<bool> target_found = |
| 7138 | JSReceiver::GetOwnPropertyDescriptor(isolate, target, key, &target_desc); |
| 7139 | MAYBE_RETURN(target_found, Nothing<bool>()); |
| 7140 | // 12. Let extensibleTarget be ? IsExtensible(target). |
| 7141 | Maybe<bool> maybe_extensible = JSReceiver::IsExtensible(target); |
| 7142 | MAYBE_RETURN(maybe_extensible, Nothing<bool>()); |
| 7143 | bool extensible_target = maybe_extensible.FromJust(); |
| 7144 | // 13. If Desc has a [[Configurable]] field and if Desc.[[Configurable]] |
| 7145 | // is false, then: |
| 7146 | // 13a. Let settingConfigFalse be true. |
| 7147 | // 14. Else let settingConfigFalse be false. |
| 7148 | bool setting_config_false = desc->has_configurable() && !desc->configurable(); |
| 7149 | // 15. If targetDesc is undefined, then |
| 7150 | if (!target_found.FromJust()) { |
| 7151 | // 15a. If extensibleTarget is false, throw a TypeError exception. |
| 7152 | if (!extensible_target) { |
| 7153 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7154 | MessageTemplate::kProxyDefinePropertyNonExtensible, property_name)); |
| 7155 | return Nothing<bool>(); |
| 7156 | } |
| 7157 | // 15b. If settingConfigFalse is true, throw a TypeError exception. |
| 7158 | if (setting_config_false) { |
| 7159 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7160 | MessageTemplate::kProxyDefinePropertyNonConfigurable, property_name)); |
| 7161 | return Nothing<bool>(); |
| 7162 | } |
| 7163 | } else { |
| 7164 | // 16. Else targetDesc is not undefined, |
| 7165 | // 16a. If IsCompatiblePropertyDescriptor(extensibleTarget, Desc, |
| 7166 | // targetDesc) is false, throw a TypeError exception. |
| 7167 | Maybe<bool> valid = |
| 7168 | IsCompatiblePropertyDescriptor(isolate, extensible_target, desc, |
| 7169 | &target_desc, property_name, DONT_THROW); |
| 7170 | MAYBE_RETURN(valid, Nothing<bool>()); |
| 7171 | if (!valid.FromJust()) { |
| 7172 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7173 | MessageTemplate::kProxyDefinePropertyIncompatible, property_name)); |
| 7174 | return Nothing<bool>(); |
| 7175 | } |
| 7176 | // 16b. If settingConfigFalse is true and targetDesc.[[Configurable]] is |
| 7177 | // true, throw a TypeError exception. |
| 7178 | if (setting_config_false && target_desc.configurable()) { |
| 7179 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7180 | MessageTemplate::kProxyDefinePropertyNonConfigurable, property_name)); |
| 7181 | return Nothing<bool>(); |
| 7182 | } |
| 7183 | } |
| 7184 | // 17. Return true. |
| 7185 | return Just(true); |
| 7186 | } |
| 7187 | |
| 7188 | |
| 7189 | // static |
| 7190 | Maybe<bool> JSProxy::AddPrivateProperty(Isolate* isolate, Handle<JSProxy> proxy, |
| 7191 | Handle<Symbol> private_name, |
| 7192 | PropertyDescriptor* desc, |
| 7193 | ShouldThrow should_throw) { |
| 7194 | // Despite the generic name, this can only add private data properties. |
| 7195 | if (!PropertyDescriptor::IsDataDescriptor(desc) || |
| 7196 | desc->ToAttributes() != DONT_ENUM) { |
| 7197 | RETURN_FAILURE(isolate, should_throw, |
| 7198 | NewTypeError(MessageTemplate::kProxyPrivate)); |
| 7199 | } |
| 7200 | DCHECK(proxy->map()->is_dictionary_map()); |
| 7201 | Handle<Object> value = |
| 7202 | desc->has_value() |
| 7203 | ? desc->value() |
| 7204 | : Handle<Object>::cast(isolate->factory()->undefined_value()); |
| 7205 | |
| 7206 | LookupIterator it(proxy, private_name); |
| 7207 | |
| 7208 | if (it.IsFound()) { |
| 7209 | DCHECK_EQ(LookupIterator::DATA, it.state()); |
| 7210 | DCHECK_EQ(DONT_ENUM, it.property_details().attributes()); |
| 7211 | it.WriteDataValue(value); |
| 7212 | return Just(true); |
| 7213 | } |
| 7214 | |
| 7215 | Handle<NameDictionary> dict(proxy->property_dictionary()); |
| 7216 | PropertyDetails details(DONT_ENUM, DATA, 0, PropertyCellType::kNoCell); |
| 7217 | Handle<NameDictionary> result = |
| 7218 | NameDictionary::Add(dict, private_name, value, details); |
| 7219 | if (!dict.is_identical_to(result)) proxy->set_properties(*result); |
| 7220 | return Just(true); |
| 7221 | } |
| 7222 | |
| 7223 | |
| 7224 | // static |
| 7225 | Maybe<bool> JSReceiver::GetOwnPropertyDescriptor(Isolate* isolate, |
| 7226 | Handle<JSReceiver> object, |
| 7227 | Handle<Object> key, |
| 7228 | PropertyDescriptor* desc) { |
| 7229 | bool success = false; |
| 7230 | DCHECK(key->IsName() || key->IsNumber()); // |key| is a PropertyKey... |
| 7231 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 7232 | isolate, object, key, &success, LookupIterator::HIDDEN); |
| 7233 | DCHECK(success); // ...so creating a LookupIterator can't fail. |
| 7234 | return GetOwnPropertyDescriptor(&it, desc); |
| 7235 | } |
| 7236 | |
| 7237 | |
| 7238 | // ES6 9.1.5.1 |
| 7239 | // Returns true on success, false if the property didn't exist, nothing if |
| 7240 | // an exception was thrown. |
| 7241 | // static |
| 7242 | Maybe<bool> JSReceiver::GetOwnPropertyDescriptor(LookupIterator* it, |
| 7243 | PropertyDescriptor* desc) { |
| 7244 | Isolate* isolate = it->isolate(); |
| 7245 | // "Virtual" dispatch. |
| 7246 | if (it->IsFound() && it->GetHolder<JSReceiver>()->IsJSProxy()) { |
| 7247 | return JSProxy::GetOwnPropertyDescriptor(isolate, it->GetHolder<JSProxy>(), |
| 7248 | it->GetName(), desc); |
| 7249 | } |
| 7250 | |
| 7251 | // 1. (Assert) |
| 7252 | // 2. If O does not have an own property with key P, return undefined. |
| 7253 | Maybe<PropertyAttributes> maybe = JSObject::GetPropertyAttributes(it); |
| 7254 | MAYBE_RETURN(maybe, Nothing<bool>()); |
| 7255 | PropertyAttributes attrs = maybe.FromJust(); |
| 7256 | if (attrs == ABSENT) return Just(false); |
| 7257 | DCHECK(!isolate->has_pending_exception()); |
| 7258 | |
| 7259 | // 3. Let D be a newly created Property Descriptor with no fields. |
| 7260 | DCHECK(desc->is_empty()); |
| 7261 | // 4. Let X be O's own property whose key is P. |
| 7262 | // 5. If X is a data property, then |
| 7263 | bool is_accessor_pair = it->state() == LookupIterator::ACCESSOR && |
| 7264 | it->GetAccessors()->IsAccessorPair(); |
| 7265 | if (!is_accessor_pair) { |
| 7266 | // 5a. Set D.[[Value]] to the value of X's [[Value]] attribute. |
| 7267 | Handle<Object> value; |
| 7268 | if (!JSObject::GetProperty(it).ToHandle(&value)) { |
| 7269 | DCHECK(isolate->has_pending_exception()); |
| 7270 | return Nothing<bool>(); |
| 7271 | } |
| 7272 | desc->set_value(value); |
| 7273 | // 5b. Set D.[[Writable]] to the value of X's [[Writable]] attribute |
| 7274 | desc->set_writable((attrs & READ_ONLY) == 0); |
| 7275 | } else { |
| 7276 | // 6. Else X is an accessor property, so |
| 7277 | Handle<AccessorPair> accessors = |
| 7278 | Handle<AccessorPair>::cast(it->GetAccessors()); |
| 7279 | // 6a. Set D.[[Get]] to the value of X's [[Get]] attribute. |
| 7280 | desc->set_get(handle(accessors->GetComponent(ACCESSOR_GETTER), isolate)); |
| 7281 | // 6b. Set D.[[Set]] to the value of X's [[Set]] attribute. |
| 7282 | desc->set_set(handle(accessors->GetComponent(ACCESSOR_SETTER), isolate)); |
| 7283 | } |
| 7284 | |
| 7285 | // 7. Set D.[[Enumerable]] to the value of X's [[Enumerable]] attribute. |
| 7286 | desc->set_enumerable((attrs & DONT_ENUM) == 0); |
| 7287 | // 8. Set D.[[Configurable]] to the value of X's [[Configurable]] attribute. |
| 7288 | desc->set_configurable((attrs & DONT_DELETE) == 0); |
| 7289 | // 9. Return D. |
| 7290 | DCHECK(PropertyDescriptor::IsAccessorDescriptor(desc) != |
| 7291 | PropertyDescriptor::IsDataDescriptor(desc)); |
| 7292 | return Just(true); |
| 7293 | } |
| 7294 | |
| 7295 | |
| 7296 | // ES6 9.5.5 |
| 7297 | // static |
| 7298 | Maybe<bool> JSProxy::GetOwnPropertyDescriptor(Isolate* isolate, |
| 7299 | Handle<JSProxy> proxy, |
| 7300 | Handle<Name> name, |
| 7301 | PropertyDescriptor* desc) { |
| 7302 | DCHECK(!name->IsPrivate()); |
| 7303 | STACK_CHECK(Nothing<bool>()); |
| 7304 | |
| 7305 | Handle<String> trap_name = |
| 7306 | isolate->factory()->getOwnPropertyDescriptor_string(); |
| 7307 | // 1. (Assert) |
| 7308 | // 2. Let handler be the value of the [[ProxyHandler]] internal slot of O. |
| 7309 | Handle<Object> handler(proxy->handler(), isolate); |
| 7310 | // 3. If handler is null, throw a TypeError exception. |
| 7311 | // 4. Assert: Type(handler) is Object. |
| 7312 | if (proxy->IsRevoked()) { |
| 7313 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7314 | MessageTemplate::kProxyRevoked, trap_name)); |
| 7315 | return Nothing<bool>(); |
| 7316 | } |
| 7317 | // 5. Let target be the value of the [[ProxyTarget]] internal slot of O. |
| 7318 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 7319 | // 6. Let trap be ? GetMethod(handler, "getOwnPropertyDescriptor"). |
| 7320 | Handle<Object> trap; |
| 7321 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7322 | isolate, trap, |
| 7323 | Object::GetMethod(Handle<JSReceiver>::cast(handler), trap_name), |
| 7324 | Nothing<bool>()); |
| 7325 | // 7. If trap is undefined, then |
| 7326 | if (trap->IsUndefined()) { |
| 7327 | // 7a. Return target.[[GetOwnProperty]](P). |
| 7328 | return JSReceiver::GetOwnPropertyDescriptor(isolate, target, name, desc); |
| 7329 | } |
| 7330 | // 8. Let trapResultObj be ? Call(trap, handler, «target, P»). |
| 7331 | Handle<Object> trap_result_obj; |
| 7332 | Handle<Object> args[] = {target, name}; |
| 7333 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7334 | isolate, trap_result_obj, |
| 7335 | Execution::Call(isolate, trap, handler, arraysize(args), args), |
| 7336 | Nothing<bool>()); |
| 7337 | // 9. If Type(trapResultObj) is neither Object nor Undefined, throw a |
| 7338 | // TypeError exception. |
| 7339 | if (!trap_result_obj->IsJSReceiver() && !trap_result_obj->IsUndefined()) { |
| 7340 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7341 | MessageTemplate::kProxyGetOwnPropertyDescriptorInvalid, name)); |
| 7342 | return Nothing<bool>(); |
| 7343 | } |
| 7344 | // 10. Let targetDesc be ? target.[[GetOwnProperty]](P). |
| 7345 | PropertyDescriptor target_desc; |
| 7346 | Maybe<bool> found = |
| 7347 | JSReceiver::GetOwnPropertyDescriptor(isolate, target, name, &target_desc); |
| 7348 | MAYBE_RETURN(found, Nothing<bool>()); |
| 7349 | // 11. If trapResultObj is undefined, then |
| 7350 | if (trap_result_obj->IsUndefined()) { |
| 7351 | // 11a. If targetDesc is undefined, return undefined. |
| 7352 | if (!found.FromJust()) return Just(false); |
| 7353 | // 11b. If targetDesc.[[Configurable]] is false, throw a TypeError |
| 7354 | // exception. |
| 7355 | if (!target_desc.configurable()) { |
| 7356 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7357 | MessageTemplate::kProxyGetOwnPropertyDescriptorUndefined, name)); |
| 7358 | return Nothing<bool>(); |
| 7359 | } |
| 7360 | // 11c. Let extensibleTarget be ? IsExtensible(target). |
| 7361 | Maybe<bool> extensible_target = JSReceiver::IsExtensible(target); |
| 7362 | MAYBE_RETURN(extensible_target, Nothing<bool>()); |
| 7363 | // 11d. (Assert) |
| 7364 | // 11e. If extensibleTarget is false, throw a TypeError exception. |
| 7365 | if (!extensible_target.FromJust()) { |
| 7366 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7367 | MessageTemplate::kProxyGetOwnPropertyDescriptorNonExtensible, name)); |
| 7368 | return Nothing<bool>(); |
| 7369 | } |
| 7370 | // 11f. Return undefined. |
| 7371 | return Just(false); |
| 7372 | } |
| 7373 | // 12. Let extensibleTarget be ? IsExtensible(target). |
| 7374 | Maybe<bool> extensible_target = JSReceiver::IsExtensible(target); |
| 7375 | MAYBE_RETURN(extensible_target, Nothing<bool>()); |
| 7376 | // 13. Let resultDesc be ? ToPropertyDescriptor(trapResultObj). |
| 7377 | if (!PropertyDescriptor::ToPropertyDescriptor(isolate, trap_result_obj, |
| 7378 | desc)) { |
| 7379 | DCHECK(isolate->has_pending_exception()); |
| 7380 | return Nothing<bool>(); |
| 7381 | } |
| 7382 | // 14. Call CompletePropertyDescriptor(resultDesc). |
| 7383 | PropertyDescriptor::CompletePropertyDescriptor(isolate, desc); |
| 7384 | // 15. Let valid be IsCompatiblePropertyDescriptor (extensibleTarget, |
| 7385 | // resultDesc, targetDesc). |
| 7386 | Maybe<bool> valid = |
| 7387 | IsCompatiblePropertyDescriptor(isolate, extensible_target.FromJust(), |
| 7388 | desc, &target_desc, name, DONT_THROW); |
| 7389 | MAYBE_RETURN(valid, Nothing<bool>()); |
| 7390 | // 16. If valid is false, throw a TypeError exception. |
| 7391 | if (!valid.FromJust()) { |
| 7392 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7393 | MessageTemplate::kProxyGetOwnPropertyDescriptorIncompatible, name)); |
| 7394 | return Nothing<bool>(); |
| 7395 | } |
| 7396 | // 17. If resultDesc.[[Configurable]] is false, then |
| 7397 | if (!desc->configurable()) { |
| 7398 | // 17a. If targetDesc is undefined or targetDesc.[[Configurable]] is true: |
| 7399 | if (target_desc.is_empty() || target_desc.configurable()) { |
| 7400 | // 17a i. Throw a TypeError exception. |
| 7401 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7402 | MessageTemplate::kProxyGetOwnPropertyDescriptorNonConfigurable, |
| 7403 | name)); |
| 7404 | return Nothing<bool>(); |
| 7405 | } |
| 7406 | } |
| 7407 | // 18. Return resultDesc. |
| 7408 | return Just(true); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7409 | } |
| 7410 | |
| 7411 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7412 | bool JSObject::ReferencesObjectFromElements(FixedArray* elements, |
| 7413 | ElementsKind kind, |
| 7414 | Object* object) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7415 | DCHECK(IsFastObjectElementsKind(kind) || |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7416 | kind == DICTIONARY_ELEMENTS); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7417 | if (IsFastObjectElementsKind(kind)) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7418 | int length = IsJSArray() |
| 7419 | ? Smi::cast(JSArray::cast(this)->length())->value() |
| 7420 | : elements->length(); |
| 7421 | for (int i = 0; i < length; ++i) { |
| 7422 | Object* element = elements->get(i); |
| 7423 | if (!element->IsTheHole() && element == object) return true; |
| 7424 | } |
| 7425 | } else { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 7426 | Object* key = |
| 7427 | SeededNumberDictionary::cast(elements)->SlowReverseLookup(object); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7428 | if (!key->IsUndefined()) return true; |
| 7429 | } |
| 7430 | return false; |
| 7431 | } |
| 7432 | |
| 7433 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7434 | // Check whether this object references another object. |
| 7435 | bool JSObject::ReferencesObject(Object* obj) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7436 | Map* map_of_this = map(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7437 | Heap* heap = GetHeap(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7438 | DisallowHeapAllocation no_allocation; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7439 | |
| 7440 | // Is the object the constructor for this object? |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7441 | if (map_of_this->GetConstructor() == obj) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7442 | return true; |
| 7443 | } |
| 7444 | |
| 7445 | // Is the object the prototype for this object? |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7446 | if (map_of_this->prototype() == obj) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7447 | return true; |
| 7448 | } |
| 7449 | |
| 7450 | // Check if the object is among the named properties. |
| 7451 | Object* key = SlowReverseLookup(obj); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7452 | if (!key->IsUndefined()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7453 | return true; |
| 7454 | } |
| 7455 | |
| 7456 | // Check if the object is among the indexed properties. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7457 | ElementsKind kind = GetElementsKind(); |
| 7458 | switch (kind) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7459 | // Raw pixels and external arrays do not reference other |
| 7460 | // objects. |
| 7461 | #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7462 | case TYPE##_ELEMENTS: \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7463 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7464 | |
| 7465 | TYPED_ARRAYS(TYPED_ARRAY_CASE) |
| 7466 | #undef TYPED_ARRAY_CASE |
| 7467 | |
| 7468 | case FAST_DOUBLE_ELEMENTS: |
| 7469 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
| 7470 | break; |
| 7471 | case FAST_SMI_ELEMENTS: |
| 7472 | case FAST_HOLEY_SMI_ELEMENTS: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7473 | break; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7474 | case FAST_ELEMENTS: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7475 | case FAST_HOLEY_ELEMENTS: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7476 | case DICTIONARY_ELEMENTS: { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7477 | FixedArray* elements = FixedArray::cast(this->elements()); |
| 7478 | if (ReferencesObjectFromElements(elements, kind, obj)) return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7479 | break; |
| 7480 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7481 | case FAST_SLOPPY_ARGUMENTS_ELEMENTS: |
| 7482 | case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7483 | FixedArray* parameter_map = FixedArray::cast(elements()); |
| 7484 | // Check the mapped parameters. |
| 7485 | int length = parameter_map->length(); |
| 7486 | for (int i = 2; i < length; ++i) { |
| 7487 | Object* value = parameter_map->get(i); |
| 7488 | if (!value->IsTheHole() && value == obj) return true; |
| 7489 | } |
| 7490 | // Check the arguments. |
| 7491 | FixedArray* arguments = FixedArray::cast(parameter_map->get(1)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7492 | kind = arguments->IsDictionary() ? DICTIONARY_ELEMENTS : |
| 7493 | FAST_HOLEY_ELEMENTS; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7494 | if (ReferencesObjectFromElements(arguments, kind, obj)) return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7495 | break; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7496 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7497 | } |
| 7498 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7499 | // For functions check the context. |
| 7500 | if (IsJSFunction()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7501 | // Get the constructor function for arguments array. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7502 | Map* arguments_map = |
| 7503 | heap->isolate()->context()->native_context()->sloppy_arguments_map(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7504 | JSFunction* arguments_function = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7505 | JSFunction::cast(arguments_map->GetConstructor()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7506 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7507 | // Get the context and don't check if it is the native context. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7508 | JSFunction* f = JSFunction::cast(this); |
| 7509 | Context* context = f->context(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7510 | if (context->IsNativeContext()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7511 | return false; |
| 7512 | } |
| 7513 | |
| 7514 | // Check the non-special context slots. |
| 7515 | for (int i = Context::MIN_CONTEXT_SLOTS; i < context->length(); i++) { |
| 7516 | // Only check JS objects. |
| 7517 | if (context->get(i)->IsJSObject()) { |
| 7518 | JSObject* ctxobj = JSObject::cast(context->get(i)); |
| 7519 | // If it is an arguments array check the content. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7520 | if (ctxobj->map()->GetConstructor() == arguments_function) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7521 | if (ctxobj->ReferencesObject(obj)) { |
| 7522 | return true; |
| 7523 | } |
| 7524 | } else if (ctxobj == obj) { |
| 7525 | return true; |
| 7526 | } |
| 7527 | } |
| 7528 | } |
| 7529 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7530 | // Check the context extension (if any) if it can have references. |
| 7531 | if (context->has_extension() && !context->IsCatchContext()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7532 | // With harmony scoping, a JSFunction may have a script context. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7533 | // TODO(mvstanton): walk into the ScopeInfo. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7534 | if (context->IsScriptContext()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7535 | return false; |
| 7536 | } |
| 7537 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7538 | return context->extension_object()->ReferencesObject(obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7539 | } |
| 7540 | } |
| 7541 | |
| 7542 | // No references to object. |
| 7543 | return false; |
| 7544 | } |
| 7545 | |
| 7546 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7547 | Maybe<bool> JSReceiver::SetIntegrityLevel(Handle<JSReceiver> receiver, |
| 7548 | IntegrityLevel level, |
| 7549 | ShouldThrow should_throw) { |
| 7550 | DCHECK(level == SEALED || level == FROZEN); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7551 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7552 | if (receiver->IsJSObject()) { |
| 7553 | Handle<JSObject> object = Handle<JSObject>::cast(receiver); |
| 7554 | if (!object->HasSloppyArgumentsElements() && |
| 7555 | !object->map()->is_observed() && |
| 7556 | (!object->map()->is_strong() || level == SEALED)) { // Fast path. |
| 7557 | if (level == SEALED) { |
| 7558 | return JSObject::PreventExtensionsWithTransition<SEALED>(object, |
| 7559 | should_throw); |
| 7560 | } else { |
| 7561 | return JSObject::PreventExtensionsWithTransition<FROZEN>(object, |
| 7562 | should_throw); |
| 7563 | } |
| 7564 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7565 | } |
| 7566 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7567 | Isolate* isolate = receiver->GetIsolate(); |
| 7568 | |
| 7569 | MAYBE_RETURN(JSReceiver::PreventExtensions(receiver, should_throw), |
| 7570 | Nothing<bool>()); |
| 7571 | |
| 7572 | Handle<FixedArray> keys; |
| 7573 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7574 | isolate, keys, JSReceiver::OwnPropertyKeys(receiver), Nothing<bool>()); |
| 7575 | |
| 7576 | PropertyDescriptor no_conf; |
| 7577 | no_conf.set_configurable(false); |
| 7578 | |
| 7579 | PropertyDescriptor no_conf_no_write; |
| 7580 | no_conf_no_write.set_configurable(false); |
| 7581 | no_conf_no_write.set_writable(false); |
| 7582 | |
| 7583 | if (level == SEALED) { |
| 7584 | for (int i = 0; i < keys->length(); ++i) { |
| 7585 | Handle<Object> key(keys->get(i), isolate); |
| 7586 | MAYBE_RETURN( |
| 7587 | DefineOwnProperty(isolate, receiver, key, &no_conf, THROW_ON_ERROR), |
| 7588 | Nothing<bool>()); |
| 7589 | } |
| 7590 | return Just(true); |
| 7591 | } |
| 7592 | |
| 7593 | for (int i = 0; i < keys->length(); ++i) { |
| 7594 | Handle<Object> key(keys->get(i), isolate); |
| 7595 | PropertyDescriptor current_desc; |
| 7596 | Maybe<bool> owned = JSReceiver::GetOwnPropertyDescriptor( |
| 7597 | isolate, receiver, key, ¤t_desc); |
| 7598 | MAYBE_RETURN(owned, Nothing<bool>()); |
| 7599 | if (owned.FromJust()) { |
| 7600 | PropertyDescriptor desc = |
| 7601 | PropertyDescriptor::IsAccessorDescriptor(¤t_desc) |
| 7602 | ? no_conf |
| 7603 | : no_conf_no_write; |
| 7604 | MAYBE_RETURN( |
| 7605 | DefineOwnProperty(isolate, receiver, key, &desc, THROW_ON_ERROR), |
| 7606 | Nothing<bool>()); |
| 7607 | } |
| 7608 | } |
| 7609 | return Just(true); |
| 7610 | } |
| 7611 | |
| 7612 | |
| 7613 | Maybe<bool> JSReceiver::TestIntegrityLevel(Handle<JSReceiver> object, |
| 7614 | IntegrityLevel level) { |
| 7615 | DCHECK(level == SEALED || level == FROZEN); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7616 | Isolate* isolate = object->GetIsolate(); |
| 7617 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7618 | Maybe<bool> extensible = JSReceiver::IsExtensible(object); |
| 7619 | MAYBE_RETURN(extensible, Nothing<bool>()); |
| 7620 | if (extensible.FromJust()) return Just(false); |
| 7621 | |
| 7622 | Handle<FixedArray> keys; |
| 7623 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7624 | isolate, keys, JSReceiver::OwnPropertyKeys(object), Nothing<bool>()); |
| 7625 | |
| 7626 | for (int i = 0; i < keys->length(); ++i) { |
| 7627 | Handle<Object> key(keys->get(i), isolate); |
| 7628 | PropertyDescriptor current_desc; |
| 7629 | Maybe<bool> owned = JSReceiver::GetOwnPropertyDescriptor( |
| 7630 | isolate, object, key, ¤t_desc); |
| 7631 | MAYBE_RETURN(owned, Nothing<bool>()); |
| 7632 | if (owned.FromJust()) { |
| 7633 | if (current_desc.configurable()) return Just(false); |
| 7634 | if (level == FROZEN && |
| 7635 | PropertyDescriptor::IsDataDescriptor(¤t_desc) && |
| 7636 | current_desc.writable()) { |
| 7637 | return Just(false); |
| 7638 | } |
| 7639 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7640 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7641 | return Just(true); |
| 7642 | } |
| 7643 | |
| 7644 | |
| 7645 | Maybe<bool> JSReceiver::PreventExtensions(Handle<JSReceiver> object, |
| 7646 | ShouldThrow should_throw) { |
| 7647 | if (object->IsJSProxy()) { |
| 7648 | return JSProxy::PreventExtensions(Handle<JSProxy>::cast(object), |
| 7649 | should_throw); |
| 7650 | } |
| 7651 | DCHECK(object->IsJSObject()); |
| 7652 | return JSObject::PreventExtensions(Handle<JSObject>::cast(object), |
| 7653 | should_throw); |
| 7654 | } |
| 7655 | |
| 7656 | |
| 7657 | Maybe<bool> JSProxy::PreventExtensions(Handle<JSProxy> proxy, |
| 7658 | ShouldThrow should_throw) { |
| 7659 | Isolate* isolate = proxy->GetIsolate(); |
| 7660 | STACK_CHECK(Nothing<bool>()); |
| 7661 | Factory* factory = isolate->factory(); |
| 7662 | Handle<String> trap_name = factory->preventExtensions_string(); |
| 7663 | |
| 7664 | if (proxy->IsRevoked()) { |
| 7665 | isolate->Throw( |
| 7666 | *factory->NewTypeError(MessageTemplate::kProxyRevoked, trap_name)); |
| 7667 | return Nothing<bool>(); |
| 7668 | } |
| 7669 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 7670 | Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate); |
| 7671 | |
| 7672 | Handle<Object> trap; |
| 7673 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7674 | isolate, trap, Object::GetMethod(handler, trap_name), Nothing<bool>()); |
| 7675 | if (trap->IsUndefined()) { |
| 7676 | return JSReceiver::PreventExtensions(target, should_throw); |
| 7677 | } |
| 7678 | |
| 7679 | Handle<Object> trap_result; |
| 7680 | Handle<Object> args[] = {target}; |
| 7681 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7682 | isolate, trap_result, |
| 7683 | Execution::Call(isolate, trap, handler, arraysize(args), args), |
| 7684 | Nothing<bool>()); |
| 7685 | if (!trap_result->BooleanValue()) { |
| 7686 | RETURN_FAILURE( |
| 7687 | isolate, should_throw, |
| 7688 | NewTypeError(MessageTemplate::kProxyTrapReturnedFalsish, trap_name)); |
| 7689 | } |
| 7690 | |
| 7691 | // Enforce the invariant. |
| 7692 | Maybe<bool> target_result = JSReceiver::IsExtensible(target); |
| 7693 | MAYBE_RETURN(target_result, Nothing<bool>()); |
| 7694 | if (target_result.FromJust()) { |
| 7695 | isolate->Throw(*factory->NewTypeError( |
| 7696 | MessageTemplate::kProxyPreventExtensionsExtensible)); |
| 7697 | return Nothing<bool>(); |
| 7698 | } |
| 7699 | return Just(true); |
| 7700 | } |
| 7701 | |
| 7702 | |
| 7703 | Maybe<bool> JSObject::PreventExtensions(Handle<JSObject> object, |
| 7704 | ShouldThrow should_throw) { |
| 7705 | Isolate* isolate = object->GetIsolate(); |
| 7706 | |
| 7707 | if (!object->HasSloppyArgumentsElements() && !object->map()->is_observed()) { |
| 7708 | return PreventExtensionsWithTransition<NONE>(object, should_throw); |
| 7709 | } |
| 7710 | |
| 7711 | if (object->IsAccessCheckNeeded() && |
| 7712 | !isolate->MayAccess(handle(isolate->context()), object)) { |
| 7713 | isolate->ReportFailedAccessCheck(object); |
| 7714 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); |
| 7715 | RETURN_FAILURE(isolate, should_throw, |
| 7716 | NewTypeError(MessageTemplate::kNoAccess)); |
| 7717 | } |
| 7718 | |
| 7719 | if (!object->map()->is_extensible()) return Just(true); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7720 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7721 | if (object->IsJSGlobalProxy()) { |
| 7722 | PrototypeIterator iter(isolate, object); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7723 | if (iter.IsAtEnd()) return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7724 | DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7725 | return PreventExtensions(PrototypeIterator::GetCurrent<JSObject>(iter), |
| 7726 | should_throw); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 7727 | } |
| 7728 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7729 | if (!object->HasFixedTypedArrayElements()) { |
| 7730 | // If there are fast elements we normalize. |
| 7731 | Handle<SeededNumberDictionary> dictionary = NormalizeElements(object); |
| 7732 | DCHECK(object->HasDictionaryElements() || |
| 7733 | object->HasSlowArgumentsElements()); |
| 7734 | |
| 7735 | // Make sure that we never go back to fast case. |
| 7736 | object->RequireSlowElements(*dictionary); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7737 | } |
| 7738 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7739 | // Do a map transition, other objects with this map may still |
| 7740 | // be extensible. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7741 | // TODO(adamk): Extend the NormalizedMapCache to handle non-extensible maps. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7742 | Handle<Map> new_map = Map::Copy(handle(object->map()), "PreventExtensions"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7743 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7744 | new_map->set_is_extensible(false); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7745 | JSObject::MigrateToMap(object, new_map); |
| 7746 | DCHECK(!object->map()->is_extensible()); |
| 7747 | |
| 7748 | if (object->map()->is_observed()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7749 | RETURN_ON_EXCEPTION_VALUE( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7750 | isolate, |
| 7751 | EnqueueChangeRecord(object, "preventExtensions", Handle<Name>(), |
| 7752 | isolate->factory()->the_hole_value()), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7753 | Nothing<bool>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7754 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7755 | return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7756 | } |
| 7757 | |
| 7758 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7759 | Maybe<bool> JSReceiver::IsExtensible(Handle<JSReceiver> object) { |
| 7760 | if (object->IsJSProxy()) { |
| 7761 | return JSProxy::IsExtensible(Handle<JSProxy>::cast(object)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7762 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7763 | return Just(JSObject::IsExtensible(Handle<JSObject>::cast(object))); |
| 7764 | } |
| 7765 | |
| 7766 | |
| 7767 | Maybe<bool> JSProxy::IsExtensible(Handle<JSProxy> proxy) { |
| 7768 | Isolate* isolate = proxy->GetIsolate(); |
| 7769 | STACK_CHECK(Nothing<bool>()); |
| 7770 | Factory* factory = isolate->factory(); |
| 7771 | Handle<String> trap_name = factory->isExtensible_string(); |
| 7772 | |
| 7773 | if (proxy->IsRevoked()) { |
| 7774 | isolate->Throw( |
| 7775 | *factory->NewTypeError(MessageTemplate::kProxyRevoked, trap_name)); |
| 7776 | return Nothing<bool>(); |
| 7777 | } |
| 7778 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 7779 | Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate); |
| 7780 | |
| 7781 | Handle<Object> trap; |
| 7782 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7783 | isolate, trap, Object::GetMethod(handler, trap_name), Nothing<bool>()); |
| 7784 | if (trap->IsUndefined()) { |
| 7785 | return JSReceiver::IsExtensible(target); |
| 7786 | } |
| 7787 | |
| 7788 | Handle<Object> trap_result; |
| 7789 | Handle<Object> args[] = {target}; |
| 7790 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7791 | isolate, trap_result, |
| 7792 | Execution::Call(isolate, trap, handler, arraysize(args), args), |
| 7793 | Nothing<bool>()); |
| 7794 | |
| 7795 | // Enforce the invariant. |
| 7796 | Maybe<bool> target_result = JSReceiver::IsExtensible(target); |
| 7797 | MAYBE_RETURN(target_result, Nothing<bool>()); |
| 7798 | if (target_result.FromJust() != trap_result->BooleanValue()) { |
| 7799 | isolate->Throw( |
| 7800 | *factory->NewTypeError(MessageTemplate::kProxyIsExtensibleInconsistent, |
| 7801 | factory->ToBoolean(target_result.FromJust()))); |
| 7802 | return Nothing<bool>(); |
| 7803 | } |
| 7804 | return target_result; |
| 7805 | } |
| 7806 | |
| 7807 | |
| 7808 | bool JSObject::IsExtensible(Handle<JSObject> object) { |
| 7809 | Isolate* isolate = object->GetIsolate(); |
| 7810 | if (object->IsAccessCheckNeeded() && |
| 7811 | !isolate->MayAccess(handle(isolate->context()), object)) { |
| 7812 | return true; |
| 7813 | } |
| 7814 | if (object->IsJSGlobalProxy()) { |
| 7815 | PrototypeIterator iter(isolate, *object); |
| 7816 | if (iter.IsAtEnd()) return false; |
| 7817 | DCHECK(iter.GetCurrent()->IsJSGlobalObject()); |
| 7818 | return iter.GetCurrent<JSObject>()->map()->is_extensible(); |
| 7819 | } |
| 7820 | return object->map()->is_extensible(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7821 | } |
| 7822 | |
| 7823 | |
| 7824 | template <typename Dictionary> |
| 7825 | static void ApplyAttributesToDictionary(Dictionary* dictionary, |
| 7826 | const PropertyAttributes attributes) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7827 | int capacity = dictionary->Capacity(); |
| 7828 | for (int i = 0; i < capacity; i++) { |
| 7829 | Object* k = dictionary->KeyAt(i); |
| 7830 | if (dictionary->IsKey(k) && |
| 7831 | !(k->IsSymbol() && Symbol::cast(k)->is_private())) { |
| 7832 | PropertyDetails details = dictionary->DetailsAt(i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7833 | int attrs = attributes; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7834 | // READ_ONLY is an invalid attribute for JS setters/getters. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7835 | if ((attributes & READ_ONLY) && details.type() == ACCESSOR_CONSTANT) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7836 | Object* v = dictionary->ValueAt(i); |
| 7837 | if (v->IsPropertyCell()) v = PropertyCell::cast(v)->value(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7838 | if (v->IsAccessorPair()) attrs &= ~READ_ONLY; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7839 | } |
| 7840 | details = details.CopyAddAttributes( |
| 7841 | static_cast<PropertyAttributes>(attrs)); |
| 7842 | dictionary->DetailsAtPut(i, details); |
| 7843 | } |
| 7844 | } |
| 7845 | } |
| 7846 | |
| 7847 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7848 | template <PropertyAttributes attrs> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7849 | Maybe<bool> JSObject::PreventExtensionsWithTransition( |
| 7850 | Handle<JSObject> object, ShouldThrow should_throw) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7851 | STATIC_ASSERT(attrs == NONE || attrs == SEALED || attrs == FROZEN); |
| 7852 | |
| 7853 | // Sealing/freezing sloppy arguments should be handled elsewhere. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7854 | DCHECK(!object->HasSloppyArgumentsElements()); |
| 7855 | DCHECK(!object->map()->is_observed()); |
| 7856 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7857 | Isolate* isolate = object->GetIsolate(); |
| 7858 | if (object->IsAccessCheckNeeded() && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7859 | !isolate->MayAccess(handle(isolate->context()), object)) { |
| 7860 | isolate->ReportFailedAccessCheck(object); |
| 7861 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); |
| 7862 | RETURN_FAILURE(isolate, should_throw, |
| 7863 | NewTypeError(MessageTemplate::kNoAccess)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7864 | } |
| 7865 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7866 | if (attrs == NONE && !object->map()->is_extensible()) return Just(true); |
| 7867 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7868 | if (object->IsJSGlobalProxy()) { |
| 7869 | PrototypeIterator iter(isolate, object); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7870 | if (iter.IsAtEnd()) return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7871 | DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7872 | return PreventExtensionsWithTransition<attrs>( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7873 | PrototypeIterator::GetCurrent<JSObject>(iter), should_throw); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7874 | } |
| 7875 | |
| 7876 | Handle<SeededNumberDictionary> new_element_dictionary; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7877 | if (!object->HasFixedTypedArrayElements() && |
| 7878 | !object->HasDictionaryElements()) { |
| 7879 | int length = |
| 7880 | object->IsJSArray() |
| 7881 | ? Smi::cast(Handle<JSArray>::cast(object)->length())->value() |
| 7882 | : object->elements()->length(); |
| 7883 | new_element_dictionary = |
| 7884 | length == 0 ? isolate->factory()->empty_slow_element_dictionary() |
| 7885 | : GetNormalizedElementDictionary( |
| 7886 | object, handle(object->elements())); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7887 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7888 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7889 | Handle<Symbol> transition_marker; |
| 7890 | if (attrs == NONE) { |
| 7891 | transition_marker = isolate->factory()->nonextensible_symbol(); |
| 7892 | } else if (attrs == SEALED) { |
| 7893 | transition_marker = isolate->factory()->sealed_symbol(); |
| 7894 | } else { |
| 7895 | DCHECK(attrs == FROZEN); |
| 7896 | transition_marker = isolate->factory()->frozen_symbol(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7897 | } |
| 7898 | |
| 7899 | Handle<Map> old_map(object->map(), isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7900 | Map* transition = |
| 7901 | TransitionArray::SearchSpecial(*old_map, *transition_marker); |
| 7902 | if (transition != NULL) { |
| 7903 | Handle<Map> transition_map(transition, isolate); |
| 7904 | DCHECK(transition_map->has_dictionary_elements() || |
| 7905 | transition_map->has_fixed_typed_array_elements()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7906 | DCHECK(!transition_map->is_extensible()); |
| 7907 | JSObject::MigrateToMap(object, transition_map); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7908 | } else if (TransitionArray::CanHaveMoreTransitions(old_map)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7909 | // Create a new descriptor array with the appropriate property attributes |
| 7910 | Handle<Map> new_map = Map::CopyForPreventExtensions( |
| 7911 | old_map, attrs, transition_marker, "CopyForPreventExtensions"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7912 | JSObject::MigrateToMap(object, new_map); |
| 7913 | } else { |
| 7914 | DCHECK(old_map->is_dictionary_map() || !old_map->is_prototype_map()); |
| 7915 | // Slow path: need to normalize properties for safety |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7916 | NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0, |
| 7917 | "SlowPreventExtensions"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7918 | |
| 7919 | // Create a new map, since other objects with this map may be extensible. |
| 7920 | // TODO(adamk): Extend the NormalizedMapCache to handle non-extensible maps. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7921 | Handle<Map> new_map = |
| 7922 | Map::Copy(handle(object->map()), "SlowCopyForPreventExtensions"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7923 | new_map->set_is_extensible(false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7924 | if (!new_element_dictionary.is_null()) { |
| 7925 | new_map->set_elements_kind(DICTIONARY_ELEMENTS); |
| 7926 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7927 | JSObject::MigrateToMap(object, new_map); |
| 7928 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7929 | if (attrs != NONE) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7930 | if (object->IsJSGlobalObject()) { |
| 7931 | ApplyAttributesToDictionary(object->global_dictionary(), attrs); |
| 7932 | } else { |
| 7933 | ApplyAttributesToDictionary(object->property_dictionary(), attrs); |
| 7934 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7935 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7936 | } |
| 7937 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7938 | // Both seal and preventExtensions always go through without modifications to |
| 7939 | // typed array elements. Freeze works only if there are no actual elements. |
| 7940 | if (object->HasFixedTypedArrayElements()) { |
| 7941 | if (attrs == FROZEN && |
| 7942 | JSArrayBufferView::cast(*object)->byte_length()->Number() > 0) { |
| 7943 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7944 | MessageTemplate::kCannotFreezeArrayBufferView)); |
| 7945 | return Nothing<bool>(); |
| 7946 | } |
| 7947 | return Just(true); |
| 7948 | } |
| 7949 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7950 | DCHECK(object->map()->has_dictionary_elements()); |
| 7951 | if (!new_element_dictionary.is_null()) { |
| 7952 | object->set_elements(*new_element_dictionary); |
| 7953 | } |
| 7954 | |
| 7955 | if (object->elements() != isolate->heap()->empty_slow_element_dictionary()) { |
| 7956 | SeededNumberDictionary* dictionary = object->element_dictionary(); |
| 7957 | // Make sure we never go back to the fast case |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7958 | object->RequireSlowElements(dictionary); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7959 | if (attrs != NONE) { |
| 7960 | ApplyAttributesToDictionary(dictionary, attrs); |
| 7961 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7962 | } |
| 7963 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7964 | return Just(true); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7965 | } |
| 7966 | |
| 7967 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7968 | void JSObject::SetObserved(Handle<JSObject> object) { |
| 7969 | DCHECK(!object->IsJSGlobalProxy()); |
| 7970 | DCHECK(!object->IsJSGlobalObject()); |
| 7971 | Isolate* isolate = object->GetIsolate(); |
| 7972 | Handle<Map> new_map; |
| 7973 | Handle<Map> old_map(object->map(), isolate); |
| 7974 | DCHECK(!old_map->is_observed()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7975 | Map* transition = TransitionArray::SearchSpecial( |
| 7976 | *old_map, isolate->heap()->observed_symbol()); |
| 7977 | if (transition != NULL) { |
| 7978 | new_map = handle(transition, isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7979 | DCHECK(new_map->is_observed()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7980 | } else if (TransitionArray::CanHaveMoreTransitions(old_map)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7981 | new_map = Map::CopyForObserved(old_map); |
| 7982 | } else { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7983 | new_map = Map::Copy(old_map, "SlowObserved"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7984 | new_map->set_is_observed(); |
| 7985 | } |
| 7986 | JSObject::MigrateToMap(object, new_map); |
| 7987 | } |
| 7988 | |
| 7989 | |
| 7990 | Handle<Object> JSObject::FastPropertyAt(Handle<JSObject> object, |
| 7991 | Representation representation, |
| 7992 | FieldIndex index) { |
| 7993 | Isolate* isolate = object->GetIsolate(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7994 | if (object->IsUnboxedDoubleField(index)) { |
| 7995 | double value = object->RawFastDoublePropertyAt(index); |
| 7996 | return isolate->factory()->NewHeapNumber(value); |
| 7997 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7998 | Handle<Object> raw_value(object->RawFastPropertyAt(index), isolate); |
| 7999 | return Object::WrapForRead(isolate, raw_value, representation); |
| 8000 | } |
| 8001 | |
| 8002 | |
| 8003 | template<class ContextObject> |
| 8004 | class JSObjectWalkVisitor { |
| 8005 | public: |
| 8006 | JSObjectWalkVisitor(ContextObject* site_context, bool copying, |
| 8007 | JSObject::DeepCopyHints hints) |
| 8008 | : site_context_(site_context), |
| 8009 | copying_(copying), |
| 8010 | hints_(hints) {} |
| 8011 | |
| 8012 | MUST_USE_RESULT MaybeHandle<JSObject> StructureWalk(Handle<JSObject> object); |
| 8013 | |
| 8014 | protected: |
| 8015 | MUST_USE_RESULT inline MaybeHandle<JSObject> VisitElementOrProperty( |
| 8016 | Handle<JSObject> object, |
| 8017 | Handle<JSObject> value) { |
| 8018 | Handle<AllocationSite> current_site = site_context()->EnterNewScope(); |
| 8019 | MaybeHandle<JSObject> copy_of_value = StructureWalk(value); |
| 8020 | site_context()->ExitScope(current_site, value); |
| 8021 | return copy_of_value; |
| 8022 | } |
| 8023 | |
| 8024 | inline ContextObject* site_context() { return site_context_; } |
| 8025 | inline Isolate* isolate() { return site_context()->isolate(); } |
| 8026 | |
| 8027 | inline bool copying() const { return copying_; } |
| 8028 | |
| 8029 | private: |
| 8030 | ContextObject* site_context_; |
| 8031 | const bool copying_; |
| 8032 | const JSObject::DeepCopyHints hints_; |
| 8033 | }; |
| 8034 | |
| 8035 | |
| 8036 | template <class ContextObject> |
| 8037 | MaybeHandle<JSObject> JSObjectWalkVisitor<ContextObject>::StructureWalk( |
| 8038 | Handle<JSObject> object) { |
| 8039 | Isolate* isolate = this->isolate(); |
| 8040 | bool copying = this->copying(); |
| 8041 | bool shallow = hints_ == JSObject::kObjectIsShallow; |
| 8042 | |
| 8043 | if (!shallow) { |
| 8044 | StackLimitCheck check(isolate); |
| 8045 | |
| 8046 | if (check.HasOverflowed()) { |
| 8047 | isolate->StackOverflow(); |
| 8048 | return MaybeHandle<JSObject>(); |
| 8049 | } |
| 8050 | } |
| 8051 | |
| 8052 | if (object->map()->is_deprecated()) { |
| 8053 | JSObject::MigrateInstance(object); |
| 8054 | } |
| 8055 | |
| 8056 | Handle<JSObject> copy; |
| 8057 | if (copying) { |
| 8058 | Handle<AllocationSite> site_to_pass; |
| 8059 | if (site_context()->ShouldCreateMemento(object)) { |
| 8060 | site_to_pass = site_context()->current(); |
| 8061 | } |
| 8062 | copy = isolate->factory()->CopyJSObjectWithAllocationSite( |
| 8063 | object, site_to_pass); |
| 8064 | } else { |
| 8065 | copy = object; |
| 8066 | } |
| 8067 | |
| 8068 | DCHECK(copying || copy.is_identical_to(object)); |
| 8069 | |
| 8070 | ElementsKind kind = copy->GetElementsKind(); |
| 8071 | if (copying && IsFastSmiOrObjectElementsKind(kind) && |
| 8072 | FixedArray::cast(copy->elements())->map() == |
| 8073 | isolate->heap()->fixed_cow_array_map()) { |
| 8074 | isolate->counters()->cow_arrays_created_runtime()->Increment(); |
| 8075 | } |
| 8076 | |
| 8077 | if (!shallow) { |
| 8078 | HandleScope scope(isolate); |
| 8079 | |
| 8080 | // Deep copy own properties. |
| 8081 | if (copy->HasFastProperties()) { |
| 8082 | Handle<DescriptorArray> descriptors(copy->map()->instance_descriptors()); |
| 8083 | int limit = copy->map()->NumberOfOwnDescriptors(); |
| 8084 | for (int i = 0; i < limit; i++) { |
| 8085 | PropertyDetails details = descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8086 | if (details.type() != DATA) continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8087 | FieldIndex index = FieldIndex::ForDescriptor(copy->map(), i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8088 | if (object->IsUnboxedDoubleField(index)) { |
| 8089 | if (copying) { |
| 8090 | double value = object->RawFastDoublePropertyAt(index); |
| 8091 | copy->RawFastDoublePropertyAtPut(index, value); |
| 8092 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8093 | } else { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8094 | Handle<Object> value(object->RawFastPropertyAt(index), isolate); |
| 8095 | if (value->IsJSObject()) { |
| 8096 | ASSIGN_RETURN_ON_EXCEPTION( |
| 8097 | isolate, value, |
| 8098 | VisitElementOrProperty(copy, Handle<JSObject>::cast(value)), |
| 8099 | JSObject); |
| 8100 | if (copying) { |
| 8101 | copy->FastPropertyAtPut(index, *value); |
| 8102 | } |
| 8103 | } else { |
| 8104 | if (copying) { |
| 8105 | Representation representation = details.representation(); |
| 8106 | value = Object::NewStorageFor(isolate, value, representation); |
| 8107 | copy->FastPropertyAtPut(index, *value); |
| 8108 | } |
| 8109 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8110 | } |
| 8111 | } |
| 8112 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8113 | // Only deep copy fields from the object literal expression. |
| 8114 | // In particular, don't try to copy the length attribute of |
| 8115 | // an array. |
| 8116 | PropertyFilter filter = static_cast<PropertyFilter>( |
| 8117 | ONLY_WRITABLE | ONLY_ENUMERABLE | ONLY_CONFIGURABLE); |
| 8118 | KeyAccumulator accumulator(isolate, filter); |
| 8119 | accumulator.NextPrototype(); |
| 8120 | copy->CollectOwnPropertyNames(&accumulator, filter); |
| 8121 | Handle<FixedArray> names = accumulator.GetKeys(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8122 | for (int i = 0; i < names->length(); i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8123 | DCHECK(names->get(i)->IsName()); |
| 8124 | Handle<Name> name(Name::cast(names->get(i))); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8125 | Handle<Object> value = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8126 | Object::GetProperty(copy, name).ToHandleChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8127 | if (value->IsJSObject()) { |
| 8128 | Handle<JSObject> result; |
| 8129 | ASSIGN_RETURN_ON_EXCEPTION( |
| 8130 | isolate, result, |
| 8131 | VisitElementOrProperty(copy, Handle<JSObject>::cast(value)), |
| 8132 | JSObject); |
| 8133 | if (copying) { |
| 8134 | // Creating object copy for literals. No strict mode needed. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8135 | JSObject::SetProperty(copy, name, result, SLOPPY).Assert(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8136 | } |
| 8137 | } |
| 8138 | } |
| 8139 | } |
| 8140 | |
| 8141 | // Deep copy own elements. |
| 8142 | // Pixel elements cannot be created using an object literal. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8143 | DCHECK(!copy->HasFixedTypedArrayElements()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8144 | switch (kind) { |
| 8145 | case FAST_SMI_ELEMENTS: |
| 8146 | case FAST_ELEMENTS: |
| 8147 | case FAST_HOLEY_SMI_ELEMENTS: |
| 8148 | case FAST_HOLEY_ELEMENTS: { |
| 8149 | Handle<FixedArray> elements(FixedArray::cast(copy->elements())); |
| 8150 | if (elements->map() == isolate->heap()->fixed_cow_array_map()) { |
| 8151 | #ifdef DEBUG |
| 8152 | for (int i = 0; i < elements->length(); i++) { |
| 8153 | DCHECK(!elements->get(i)->IsJSObject()); |
| 8154 | } |
| 8155 | #endif |
| 8156 | } else { |
| 8157 | for (int i = 0; i < elements->length(); i++) { |
| 8158 | Handle<Object> value(elements->get(i), isolate); |
| 8159 | DCHECK(value->IsSmi() || |
| 8160 | value->IsTheHole() || |
| 8161 | (IsFastObjectElementsKind(copy->GetElementsKind()))); |
| 8162 | if (value->IsJSObject()) { |
| 8163 | Handle<JSObject> result; |
| 8164 | ASSIGN_RETURN_ON_EXCEPTION( |
| 8165 | isolate, result, |
| 8166 | VisitElementOrProperty(copy, Handle<JSObject>::cast(value)), |
| 8167 | JSObject); |
| 8168 | if (copying) { |
| 8169 | elements->set(i, *result); |
| 8170 | } |
| 8171 | } |
| 8172 | } |
| 8173 | } |
| 8174 | break; |
| 8175 | } |
| 8176 | case DICTIONARY_ELEMENTS: { |
| 8177 | Handle<SeededNumberDictionary> element_dictionary( |
| 8178 | copy->element_dictionary()); |
| 8179 | int capacity = element_dictionary->Capacity(); |
| 8180 | for (int i = 0; i < capacity; i++) { |
| 8181 | Object* k = element_dictionary->KeyAt(i); |
| 8182 | if (element_dictionary->IsKey(k)) { |
| 8183 | Handle<Object> value(element_dictionary->ValueAt(i), isolate); |
| 8184 | if (value->IsJSObject()) { |
| 8185 | Handle<JSObject> result; |
| 8186 | ASSIGN_RETURN_ON_EXCEPTION( |
| 8187 | isolate, result, |
| 8188 | VisitElementOrProperty(copy, Handle<JSObject>::cast(value)), |
| 8189 | JSObject); |
| 8190 | if (copying) { |
| 8191 | element_dictionary->ValueAtPut(i, *result); |
| 8192 | } |
| 8193 | } |
| 8194 | } |
| 8195 | } |
| 8196 | break; |
| 8197 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8198 | case FAST_SLOPPY_ARGUMENTS_ELEMENTS: |
| 8199 | case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8200 | UNIMPLEMENTED(); |
| 8201 | break; |
| 8202 | |
| 8203 | |
| 8204 | #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8205 | case TYPE##_ELEMENTS: \ |
| 8206 | |
| 8207 | TYPED_ARRAYS(TYPED_ARRAY_CASE) |
| 8208 | #undef TYPED_ARRAY_CASE |
| 8209 | |
| 8210 | case FAST_DOUBLE_ELEMENTS: |
| 8211 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
| 8212 | // No contained objects, nothing to do. |
| 8213 | break; |
| 8214 | } |
| 8215 | } |
| 8216 | |
| 8217 | return copy; |
| 8218 | } |
| 8219 | |
| 8220 | |
| 8221 | MaybeHandle<JSObject> JSObject::DeepWalk( |
| 8222 | Handle<JSObject> object, |
| 8223 | AllocationSiteCreationContext* site_context) { |
| 8224 | JSObjectWalkVisitor<AllocationSiteCreationContext> v(site_context, false, |
| 8225 | kNoHints); |
| 8226 | MaybeHandle<JSObject> result = v.StructureWalk(object); |
| 8227 | Handle<JSObject> for_assert; |
| 8228 | DCHECK(!result.ToHandle(&for_assert) || for_assert.is_identical_to(object)); |
| 8229 | return result; |
| 8230 | } |
| 8231 | |
| 8232 | |
| 8233 | MaybeHandle<JSObject> JSObject::DeepCopy( |
| 8234 | Handle<JSObject> object, |
| 8235 | AllocationSiteUsageContext* site_context, |
| 8236 | DeepCopyHints hints) { |
| 8237 | JSObjectWalkVisitor<AllocationSiteUsageContext> v(site_context, true, hints); |
| 8238 | MaybeHandle<JSObject> copy = v.StructureWalk(object); |
| 8239 | Handle<JSObject> for_assert; |
| 8240 | DCHECK(!copy.ToHandle(&for_assert) || !for_assert.is_identical_to(object)); |
| 8241 | return copy; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 8242 | } |
| 8243 | |
| 8244 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8245 | // static |
| 8246 | MaybeHandle<Object> JSReceiver::ToPrimitive(Handle<JSReceiver> receiver, |
| 8247 | ToPrimitiveHint hint) { |
| 8248 | Isolate* const isolate = receiver->GetIsolate(); |
| 8249 | Handle<Object> exotic_to_prim; |
| 8250 | ASSIGN_RETURN_ON_EXCEPTION( |
| 8251 | isolate, exotic_to_prim, |
| 8252 | GetMethod(receiver, isolate->factory()->to_primitive_symbol()), Object); |
| 8253 | if (!exotic_to_prim->IsUndefined()) { |
| 8254 | Handle<Object> hint_string; |
| 8255 | switch (hint) { |
| 8256 | case ToPrimitiveHint::kDefault: |
| 8257 | hint_string = isolate->factory()->default_string(); |
| 8258 | break; |
| 8259 | case ToPrimitiveHint::kNumber: |
| 8260 | hint_string = isolate->factory()->number_string(); |
| 8261 | break; |
| 8262 | case ToPrimitiveHint::kString: |
| 8263 | hint_string = isolate->factory()->string_string(); |
| 8264 | break; |
| 8265 | } |
| 8266 | Handle<Object> result; |
| 8267 | ASSIGN_RETURN_ON_EXCEPTION( |
| 8268 | isolate, result, |
| 8269 | Execution::Call(isolate, exotic_to_prim, receiver, 1, &hint_string), |
| 8270 | Object); |
| 8271 | if (result->IsPrimitive()) return result; |
| 8272 | THROW_NEW_ERROR(isolate, |
| 8273 | NewTypeError(MessageTemplate::kCannotConvertToPrimitive), |
| 8274 | Object); |
| 8275 | } |
| 8276 | return OrdinaryToPrimitive(receiver, (hint == ToPrimitiveHint::kString) |
| 8277 | ? OrdinaryToPrimitiveHint::kString |
| 8278 | : OrdinaryToPrimitiveHint::kNumber); |
| 8279 | } |
| 8280 | |
| 8281 | |
| 8282 | // static |
| 8283 | MaybeHandle<Object> JSReceiver::OrdinaryToPrimitive( |
| 8284 | Handle<JSReceiver> receiver, OrdinaryToPrimitiveHint hint) { |
| 8285 | Isolate* const isolate = receiver->GetIsolate(); |
| 8286 | Handle<String> method_names[2]; |
| 8287 | switch (hint) { |
| 8288 | case OrdinaryToPrimitiveHint::kNumber: |
| 8289 | method_names[0] = isolate->factory()->valueOf_string(); |
| 8290 | method_names[1] = isolate->factory()->toString_string(); |
| 8291 | break; |
| 8292 | case OrdinaryToPrimitiveHint::kString: |
| 8293 | method_names[0] = isolate->factory()->toString_string(); |
| 8294 | method_names[1] = isolate->factory()->valueOf_string(); |
| 8295 | break; |
| 8296 | } |
| 8297 | for (Handle<String> name : method_names) { |
| 8298 | Handle<Object> method; |
| 8299 | ASSIGN_RETURN_ON_EXCEPTION(isolate, method, |
| 8300 | JSReceiver::GetProperty(receiver, name), Object); |
| 8301 | if (method->IsCallable()) { |
| 8302 | Handle<Object> result; |
| 8303 | ASSIGN_RETURN_ON_EXCEPTION( |
| 8304 | isolate, result, Execution::Call(isolate, method, receiver, 0, NULL), |
| 8305 | Object); |
| 8306 | if (result->IsPrimitive()) return result; |
| 8307 | } |
| 8308 | } |
| 8309 | THROW_NEW_ERROR(isolate, |
| 8310 | NewTypeError(MessageTemplate::kCannotConvertToPrimitive), |
| 8311 | Object); |
| 8312 | } |
| 8313 | |
| 8314 | |
| 8315 | // TODO(cbruni/jkummerow): Consider moving this into elements.cc. |
| 8316 | bool HasEnumerableElements(JSObject* object) { |
| 8317 | if (object->IsJSValue()) { |
| 8318 | Object* value = JSValue::cast(object)->value(); |
| 8319 | if (value->IsString()) { |
| 8320 | if (String::cast(value)->length() > 0) return true; |
| 8321 | } |
| 8322 | } |
| 8323 | switch (object->GetElementsKind()) { |
| 8324 | case FAST_SMI_ELEMENTS: |
| 8325 | case FAST_ELEMENTS: |
| 8326 | case FAST_DOUBLE_ELEMENTS: { |
| 8327 | int length = object->IsJSArray() |
| 8328 | ? Smi::cast(JSArray::cast(object)->length())->value() |
| 8329 | : object->elements()->length(); |
| 8330 | return length > 0; |
| 8331 | } |
| 8332 | case FAST_HOLEY_SMI_ELEMENTS: |
| 8333 | case FAST_HOLEY_ELEMENTS: { |
| 8334 | FixedArray* elements = FixedArray::cast(object->elements()); |
| 8335 | int length = object->IsJSArray() |
| 8336 | ? Smi::cast(JSArray::cast(object)->length())->value() |
| 8337 | : elements->length(); |
| 8338 | for (int i = 0; i < length; i++) { |
| 8339 | if (!elements->is_the_hole(i)) return true; |
| 8340 | } |
| 8341 | return false; |
| 8342 | } |
| 8343 | case FAST_HOLEY_DOUBLE_ELEMENTS: { |
| 8344 | int length = object->IsJSArray() |
| 8345 | ? Smi::cast(JSArray::cast(object)->length())->value() |
| 8346 | : object->elements()->length(); |
| 8347 | // Zero-length arrays would use the empty FixedArray... |
| 8348 | if (length == 0) return false; |
| 8349 | // ...so only cast to FixedDoubleArray otherwise. |
| 8350 | FixedDoubleArray* elements = FixedDoubleArray::cast(object->elements()); |
| 8351 | for (int i = 0; i < length; i++) { |
| 8352 | if (!elements->is_the_hole(i)) return true; |
| 8353 | } |
| 8354 | return false; |
| 8355 | } |
| 8356 | #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ |
| 8357 | case TYPE##_ELEMENTS: |
| 8358 | |
| 8359 | TYPED_ARRAYS(TYPED_ARRAY_CASE) |
| 8360 | #undef TYPED_ARRAY_CASE |
| 8361 | { |
| 8362 | int length = object->elements()->length(); |
| 8363 | return length > 0; |
| 8364 | } |
| 8365 | case DICTIONARY_ELEMENTS: { |
| 8366 | SeededNumberDictionary* elements = |
| 8367 | SeededNumberDictionary::cast(object->elements()); |
| 8368 | return elements->NumberOfElementsFilterAttributes(ONLY_ENUMERABLE) > 0; |
| 8369 | } |
| 8370 | case FAST_SLOPPY_ARGUMENTS_ELEMENTS: |
| 8371 | case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: |
| 8372 | // We're approximating non-empty arguments objects here. |
| 8373 | return true; |
| 8374 | } |
| 8375 | UNREACHABLE(); |
| 8376 | return true; |
| 8377 | } |
| 8378 | |
| 8379 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8380 | // Tests for the fast common case for property enumeration: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8381 | // - This object and all prototypes has an enum cache (which means that |
| 8382 | // it is no proxy, has no interceptors and needs no access checks). |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8383 | // - This object has no elements. |
| 8384 | // - No prototype has enumerable properties/elements. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8385 | bool JSReceiver::IsSimpleEnum() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8386 | for (PrototypeIterator iter(GetIsolate(), this, |
| 8387 | PrototypeIterator::START_AT_RECEIVER); |
| 8388 | !iter.IsAtEnd(); iter.Advance()) { |
| 8389 | if (!iter.GetCurrent()->IsJSObject()) return false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8390 | JSObject* current = iter.GetCurrent<JSObject>(); |
| 8391 | int enum_length = current->map()->EnumLength(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8392 | if (enum_length == kInvalidEnumCacheSentinel) return false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8393 | if (current->IsAccessCheckNeeded()) return false; |
| 8394 | DCHECK(!current->HasNamedInterceptor()); |
| 8395 | DCHECK(!current->HasIndexedInterceptor()); |
| 8396 | if (HasEnumerableElements(current)) return false; |
| 8397 | if (current != this && enum_length != 0) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8398 | } |
| 8399 | return true; |
| 8400 | } |
| 8401 | |
| 8402 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8403 | int Map::NumberOfDescribedProperties(DescriptorFlag which, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8404 | PropertyFilter filter) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8405 | int result = 0; |
| 8406 | DescriptorArray* descs = instance_descriptors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8407 | int limit = which == ALL_DESCRIPTORS |
| 8408 | ? descs->number_of_descriptors() |
| 8409 | : NumberOfOwnDescriptors(); |
| 8410 | for (int i = 0; i < limit; i++) { |
| 8411 | if ((descs->GetDetails(i).attributes() & filter) == 0 && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8412 | !descs->GetKey(i)->FilterKey(filter)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8413 | result++; |
| 8414 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8415 | } |
| 8416 | return result; |
| 8417 | } |
| 8418 | |
| 8419 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8420 | int Map::NextFreePropertyIndex() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8421 | int free_index = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8422 | int number_of_own_descriptors = NumberOfOwnDescriptors(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8423 | DescriptorArray* descs = instance_descriptors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8424 | for (int i = 0; i < number_of_own_descriptors; i++) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8425 | PropertyDetails details = descs->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8426 | if (details.location() == kField) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8427 | int candidate = details.field_index() + details.field_width_in_words(); |
| 8428 | if (candidate > free_index) free_index = candidate; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8429 | } |
| 8430 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8431 | return free_index; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8432 | } |
| 8433 | |
| 8434 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8435 | static bool ContainsOnlyValidKeys(Handle<FixedArray> array) { |
| 8436 | int len = array->length(); |
| 8437 | for (int i = 0; i < len; i++) { |
| 8438 | Object* e = array->get(i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8439 | if (!(e->IsName() || e->IsNumber())) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8440 | } |
| 8441 | return true; |
| 8442 | } |
| 8443 | |
| 8444 | |
| 8445 | static Handle<FixedArray> ReduceFixedArrayTo( |
| 8446 | Handle<FixedArray> array, int length) { |
| 8447 | DCHECK(array->length() >= length); |
| 8448 | if (array->length() == length) return array; |
| 8449 | |
| 8450 | Handle<FixedArray> new_array = |
| 8451 | array->GetIsolate()->factory()->NewFixedArray(length); |
| 8452 | for (int i = 0; i < length; ++i) new_array->set(i, array->get(i)); |
| 8453 | return new_array; |
| 8454 | } |
| 8455 | |
| 8456 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8457 | namespace { |
| 8458 | |
| 8459 | Handle<FixedArray> GetFastEnumPropertyKeys(Isolate* isolate, |
| 8460 | Handle<JSObject> object, |
| 8461 | bool cache_enum_length) { |
| 8462 | Handle<Map> map(object->map()); |
| 8463 | Handle<DescriptorArray> descs = |
| 8464 | Handle<DescriptorArray>(map->instance_descriptors(), isolate); |
| 8465 | int own_property_count = map->EnumLength(); |
| 8466 | // If the enum length of the given map is set to kInvalidEnumCache, this |
| 8467 | // means that the map itself has never used the present enum cache. The |
| 8468 | // first step to using the cache is to set the enum length of the map by |
| 8469 | // counting the number of own descriptors that are ENUMERABLE_STRINGS. |
| 8470 | if (own_property_count == kInvalidEnumCacheSentinel) { |
| 8471 | own_property_count = |
| 8472 | map->NumberOfDescribedProperties(OWN_DESCRIPTORS, ENUMERABLE_STRINGS); |
| 8473 | } else { |
| 8474 | DCHECK( |
| 8475 | own_property_count == |
| 8476 | map->NumberOfDescribedProperties(OWN_DESCRIPTORS, ENUMERABLE_STRINGS)); |
| 8477 | } |
| 8478 | |
| 8479 | if (descs->HasEnumCache()) { |
| 8480 | Handle<FixedArray> keys(descs->GetEnumCache(), isolate); |
| 8481 | // In case the number of properties required in the enum are actually |
| 8482 | // present, we can reuse the enum cache. Otherwise, this means that the |
| 8483 | // enum cache was generated for a previous (smaller) version of the |
| 8484 | // Descriptor Array. In that case we regenerate the enum cache. |
| 8485 | if (own_property_count <= keys->length()) { |
| 8486 | isolate->counters()->enum_cache_hits()->Increment(); |
| 8487 | if (cache_enum_length) map->SetEnumLength(own_property_count); |
| 8488 | return ReduceFixedArrayTo(keys, own_property_count); |
| 8489 | } |
| 8490 | } |
| 8491 | |
| 8492 | if (descs->IsEmpty()) { |
| 8493 | isolate->counters()->enum_cache_hits()->Increment(); |
| 8494 | if (cache_enum_length) map->SetEnumLength(0); |
| 8495 | return isolate->factory()->empty_fixed_array(); |
| 8496 | } |
| 8497 | |
| 8498 | isolate->counters()->enum_cache_misses()->Increment(); |
| 8499 | |
| 8500 | Handle<FixedArray> storage = |
| 8501 | isolate->factory()->NewFixedArray(own_property_count); |
| 8502 | Handle<FixedArray> indices = |
| 8503 | isolate->factory()->NewFixedArray(own_property_count); |
| 8504 | |
| 8505 | int size = map->NumberOfOwnDescriptors(); |
| 8506 | int index = 0; |
| 8507 | |
| 8508 | for (int i = 0; i < size; i++) { |
| 8509 | PropertyDetails details = descs->GetDetails(i); |
| 8510 | Object* key = descs->GetKey(i); |
| 8511 | if (details.IsDontEnum() || key->IsSymbol()) continue; |
| 8512 | storage->set(index, key); |
| 8513 | if (!indices.is_null()) { |
| 8514 | if (details.type() != DATA) { |
| 8515 | indices = Handle<FixedArray>(); |
| 8516 | } else { |
| 8517 | FieldIndex field_index = FieldIndex::ForDescriptor(*map, i); |
| 8518 | int load_by_field_index = field_index.GetLoadByFieldIndex(); |
| 8519 | indices->set(index, Smi::FromInt(load_by_field_index)); |
| 8520 | } |
| 8521 | } |
| 8522 | index++; |
| 8523 | } |
| 8524 | DCHECK(index == storage->length()); |
| 8525 | |
| 8526 | DescriptorArray::SetEnumCache(descs, isolate, storage, indices); |
| 8527 | if (cache_enum_length) { |
| 8528 | map->SetEnumLength(own_property_count); |
| 8529 | } |
| 8530 | return storage; |
| 8531 | } |
| 8532 | |
| 8533 | } // namespace |
| 8534 | |
| 8535 | |
| 8536 | Handle<FixedArray> JSObject::GetEnumPropertyKeys(Handle<JSObject> object, |
| 8537 | bool cache_enum_length) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8538 | Isolate* isolate = object->GetIsolate(); |
| 8539 | if (object->HasFastProperties()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8540 | return GetFastEnumPropertyKeys(isolate, object, cache_enum_length); |
| 8541 | } else if (object->IsJSGlobalObject()) { |
| 8542 | Handle<GlobalDictionary> dictionary(object->global_dictionary()); |
| 8543 | int length = dictionary->NumberOfEnumElements(); |
| 8544 | if (length == 0) { |
| 8545 | return Handle<FixedArray>(isolate->heap()->empty_fixed_array()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8546 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8547 | Handle<FixedArray> storage = isolate->factory()->NewFixedArray(length); |
| 8548 | dictionary->CopyEnumKeysTo(*storage); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8549 | return storage; |
| 8550 | } else { |
| 8551 | Handle<NameDictionary> dictionary(object->property_dictionary()); |
| 8552 | int length = dictionary->NumberOfEnumElements(); |
| 8553 | if (length == 0) { |
| 8554 | return Handle<FixedArray>(isolate->heap()->empty_fixed_array()); |
| 8555 | } |
| 8556 | Handle<FixedArray> storage = isolate->factory()->NewFixedArray(length); |
| 8557 | dictionary->CopyEnumKeysTo(*storage); |
| 8558 | return storage; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8559 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8560 | } |
| 8561 | |
| 8562 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8563 | enum IndexedOrNamed { kIndexed, kNamed }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8564 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8565 | |
| 8566 | // Returns |true| on success, |nothing| on exception. |
| 8567 | template <class Callback, IndexedOrNamed type> |
| 8568 | static Maybe<bool> GetKeysFromInterceptor(Isolate* isolate, |
| 8569 | Handle<JSReceiver> receiver, |
| 8570 | Handle<JSObject> object, |
| 8571 | PropertyFilter filter, |
| 8572 | KeyAccumulator* accumulator) { |
| 8573 | if (type == kIndexed) { |
| 8574 | if (!object->HasIndexedInterceptor()) return Just(true); |
| 8575 | } else { |
| 8576 | if (!object->HasNamedInterceptor()) return Just(true); |
| 8577 | } |
| 8578 | Handle<InterceptorInfo> interceptor(type == kIndexed |
| 8579 | ? object->GetIndexedInterceptor() |
| 8580 | : object->GetNamedInterceptor(), |
| 8581 | isolate); |
| 8582 | if ((filter & ONLY_ALL_CAN_READ) && !interceptor->all_can_read()) { |
| 8583 | return Just(true); |
| 8584 | } |
| 8585 | PropertyCallbackArguments args(isolate, interceptor->data(), *receiver, |
| 8586 | *object); |
| 8587 | v8::Local<v8::Object> result; |
| 8588 | if (!interceptor->enumerator()->IsUndefined()) { |
| 8589 | Callback enum_fun = v8::ToCData<Callback>(interceptor->enumerator()); |
| 8590 | const char* log_tag = type == kIndexed ? "interceptor-indexed-enum" |
| 8591 | : "interceptor-named-enum"; |
| 8592 | LOG(isolate, ApiObjectAccess(log_tag, *object)); |
| 8593 | result = args.Call(enum_fun); |
| 8594 | } |
| 8595 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); |
| 8596 | if (result.IsEmpty()) return Just(true); |
| 8597 | DCHECK(v8::Utils::OpenHandle(*result)->IsJSArray() || |
| 8598 | (v8::Utils::OpenHandle(*result)->IsJSObject() && |
| 8599 | Handle<JSObject>::cast(v8::Utils::OpenHandle(*result)) |
| 8600 | ->HasSloppyArgumentsElements())); |
| 8601 | // The accumulator takes care of string/symbol filtering. |
| 8602 | if (type == kIndexed) { |
| 8603 | accumulator->AddElementKeysFromInterceptor( |
| 8604 | Handle<JSObject>::cast(v8::Utils::OpenHandle(*result))); |
| 8605 | } else { |
| 8606 | accumulator->AddKeys( |
| 8607 | Handle<JSObject>::cast(v8::Utils::OpenHandle(*result))); |
| 8608 | } |
| 8609 | return Just(true); |
| 8610 | } |
| 8611 | |
| 8612 | |
| 8613 | // Returns |true| on success, |false| if prototype walking should be stopped, |
| 8614 | // |nothing| if an exception was thrown. |
| 8615 | static Maybe<bool> GetKeysFromJSObject(Isolate* isolate, |
| 8616 | Handle<JSReceiver> receiver, |
| 8617 | Handle<JSObject> object, |
| 8618 | PropertyFilter* filter, |
| 8619 | JSReceiver::KeyCollectionType type, |
| 8620 | KeyAccumulator* accumulator) { |
| 8621 | accumulator->NextPrototype(); |
| 8622 | // Check access rights if required. |
| 8623 | if (object->IsAccessCheckNeeded() && |
| 8624 | !isolate->MayAccess(handle(isolate->context()), object)) { |
| 8625 | // The cross-origin spec says that [[Enumerate]] shall return an empty |
| 8626 | // iterator when it doesn't have access... |
| 8627 | if (type == JSReceiver::INCLUDE_PROTOS) { |
| 8628 | return Just(false); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8629 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8630 | // ...whereas [[OwnPropertyKeys]] shall return whitelisted properties. |
| 8631 | DCHECK(type == JSReceiver::OWN_ONLY); |
| 8632 | *filter = static_cast<PropertyFilter>(*filter | ONLY_ALL_CAN_READ); |
| 8633 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8634 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8635 | JSObject::CollectOwnElementKeys(object, accumulator, *filter); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8636 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8637 | // Add the element keys from the interceptor. |
| 8638 | Maybe<bool> success = |
| 8639 | GetKeysFromInterceptor<v8::IndexedPropertyEnumeratorCallback, kIndexed>( |
| 8640 | isolate, receiver, object, *filter, accumulator); |
| 8641 | MAYBE_RETURN(success, Nothing<bool>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8642 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8643 | if (*filter == ENUMERABLE_STRINGS) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8644 | // We can cache the computed property keys if access checks are |
| 8645 | // not needed and no interceptors are involved. |
| 8646 | // |
| 8647 | // We do not use the cache if the object has elements and |
| 8648 | // therefore it does not make sense to cache the property names |
| 8649 | // for arguments objects. Arguments objects will always have |
| 8650 | // elements. |
| 8651 | // Wrapped strings have elements, but don't have an elements |
| 8652 | // array or dictionary. So the fast inline test for whether to |
| 8653 | // use the cache says yes, so we should not create a cache. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8654 | Handle<JSFunction> arguments_function( |
| 8655 | JSFunction::cast(isolate->sloppy_arguments_map()->GetConstructor())); |
| 8656 | bool has_hidden_prototype = false; |
| 8657 | Object* prototype = object->map()->prototype(); |
| 8658 | if (prototype->IsJSObject()) { |
| 8659 | has_hidden_prototype = |
| 8660 | JSObject::cast(prototype)->map()->is_hidden_prototype(); |
| 8661 | } |
| 8662 | bool cache_enum_length = |
| 8663 | ((object->map()->GetConstructor() != *arguments_function) && |
| 8664 | !object->IsJSValue() && !object->IsAccessCheckNeeded() && |
| 8665 | !object->HasNamedInterceptor() && !object->HasIndexedInterceptor() && |
| 8666 | !has_hidden_prototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8667 | // Compute the property keys and cache them if possible. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8668 | Handle<FixedArray> enum_keys = |
| 8669 | JSObject::GetEnumPropertyKeys(object, cache_enum_length); |
| 8670 | accumulator->AddKeys(enum_keys); |
| 8671 | } else { |
| 8672 | object->CollectOwnPropertyNames(accumulator, *filter); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8673 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8674 | |
| 8675 | // Add the property keys from the interceptor. |
| 8676 | success = GetKeysFromInterceptor<v8::GenericNamedPropertyEnumeratorCallback, |
| 8677 | kNamed>(isolate, receiver, object, *filter, |
| 8678 | accumulator); |
| 8679 | MAYBE_RETURN(success, Nothing<bool>()); |
| 8680 | return Just(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8681 | } |
| 8682 | |
| 8683 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8684 | // Helper function for JSReceiver::GetKeys() below. Can be called recursively. |
| 8685 | // Returns |true| or |nothing|. |
| 8686 | static Maybe<bool> GetKeys_Internal(Isolate* isolate, |
| 8687 | Handle<JSReceiver> receiver, |
| 8688 | Handle<JSReceiver> object, |
| 8689 | JSReceiver::KeyCollectionType type, |
| 8690 | PropertyFilter filter, |
| 8691 | KeyAccumulator* accumulator) { |
| 8692 | PrototypeIterator::WhereToEnd end = type == JSReceiver::OWN_ONLY |
| 8693 | ? PrototypeIterator::END_AT_NON_HIDDEN |
| 8694 | : PrototypeIterator::END_AT_NULL; |
| 8695 | for (PrototypeIterator iter(isolate, object, |
| 8696 | PrototypeIterator::START_AT_RECEIVER); |
| 8697 | !iter.IsAtEnd(end); iter.Advance()) { |
| 8698 | Handle<JSReceiver> current = |
| 8699 | PrototypeIterator::GetCurrent<JSReceiver>(iter); |
| 8700 | Maybe<bool> result = Just(false); // Dummy initialization. |
| 8701 | if (current->IsJSProxy()) { |
| 8702 | if (type == JSReceiver::OWN_ONLY) { |
| 8703 | result = JSProxy::OwnPropertyKeys(isolate, receiver, |
| 8704 | Handle<JSProxy>::cast(current), |
| 8705 | filter, accumulator); |
| 8706 | } else { |
| 8707 | DCHECK(type == JSReceiver::INCLUDE_PROTOS); |
| 8708 | result = JSProxy::Enumerate( |
| 8709 | isolate, receiver, Handle<JSProxy>::cast(current), accumulator); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8710 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8711 | } else { |
| 8712 | DCHECK(current->IsJSObject()); |
| 8713 | result = GetKeysFromJSObject(isolate, receiver, |
| 8714 | Handle<JSObject>::cast(current), &filter, |
| 8715 | type, accumulator); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8716 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8717 | MAYBE_RETURN(result, Nothing<bool>()); |
| 8718 | if (!result.FromJust()) break; // |false| means "stop iterating". |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8719 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8720 | return Just(true); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8721 | } |
| 8722 | |
| 8723 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8724 | // ES6 9.5.11 |
| 8725 | // Returns false in case of exception. |
| 8726 | // static |
| 8727 | Maybe<bool> JSProxy::Enumerate(Isolate* isolate, Handle<JSReceiver> receiver, |
| 8728 | Handle<JSProxy> proxy, |
| 8729 | KeyAccumulator* accumulator) { |
| 8730 | STACK_CHECK(Nothing<bool>()); |
| 8731 | // 1. Let handler be the value of the [[ProxyHandler]] internal slot of O. |
| 8732 | Handle<Object> handler(proxy->handler(), isolate); |
| 8733 | // 2. If handler is null, throw a TypeError exception. |
| 8734 | // 3. Assert: Type(handler) is Object. |
| 8735 | if (proxy->IsRevoked()) { |
| 8736 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 8737 | MessageTemplate::kProxyRevoked, |
| 8738 | isolate->factory()->enumerate_string())); |
| 8739 | return Nothing<bool>(); |
| 8740 | } |
| 8741 | // 4. Let target be the value of the [[ProxyTarget]] internal slot of O. |
| 8742 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 8743 | // 5. Let trap be ? GetMethod(handler, "enumerate"). |
| 8744 | Handle<Object> trap; |
| 8745 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 8746 | isolate, trap, Object::GetMethod(Handle<JSReceiver>::cast(handler), |
| 8747 | isolate->factory()->enumerate_string()), |
| 8748 | Nothing<bool>()); |
| 8749 | // 6. If trap is undefined, then |
| 8750 | if (trap->IsUndefined()) { |
| 8751 | // 6a. Return target.[[Enumerate]](). |
| 8752 | return GetKeys_Internal(isolate, receiver, target, INCLUDE_PROTOS, |
| 8753 | ENUMERABLE_STRINGS, accumulator); |
| 8754 | } |
| 8755 | // The "proxy_enumerate" helper calls the trap (steps 7 - 9), which returns |
| 8756 | // a generator; it then iterates over that generator until it's exhausted |
| 8757 | // and returns an array containing the generated values. |
| 8758 | Handle<Object> trap_result_array; |
| 8759 | Handle<Object> args[] = {trap, handler, target}; |
| 8760 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 8761 | isolate, trap_result_array, |
| 8762 | Execution::Call(isolate, isolate->proxy_enumerate(), |
| 8763 | isolate->factory()->undefined_value(), arraysize(args), |
| 8764 | args), |
| 8765 | Nothing<bool>()); |
| 8766 | accumulator->NextPrototype(); |
| 8767 | accumulator->AddKeysFromProxy(Handle<JSObject>::cast(trap_result_array)); |
| 8768 | return Just(true); |
| 8769 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8770 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8771 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8772 | // ES6 9.5.12 |
| 8773 | // Returns |true| on success, |nothing| in case of exception. |
| 8774 | // static |
| 8775 | Maybe<bool> JSProxy::OwnPropertyKeys(Isolate* isolate, |
| 8776 | Handle<JSReceiver> receiver, |
| 8777 | Handle<JSProxy> proxy, |
| 8778 | PropertyFilter filter, |
| 8779 | KeyAccumulator* accumulator) { |
| 8780 | STACK_CHECK(Nothing<bool>()); |
| 8781 | // 1. Let handler be the value of the [[ProxyHandler]] internal slot of O. |
| 8782 | Handle<Object> handler(proxy->handler(), isolate); |
| 8783 | // 2. If handler is null, throw a TypeError exception. |
| 8784 | // 3. Assert: Type(handler) is Object. |
| 8785 | if (proxy->IsRevoked()) { |
| 8786 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 8787 | MessageTemplate::kProxyRevoked, isolate->factory()->ownKeys_string())); |
| 8788 | return Nothing<bool>(); |
| 8789 | } |
| 8790 | // 4. Let target be the value of the [[ProxyTarget]] internal slot of O. |
| 8791 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 8792 | // 5. Let trap be ? GetMethod(handler, "ownKeys"). |
| 8793 | Handle<Object> trap; |
| 8794 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 8795 | isolate, trap, Object::GetMethod(Handle<JSReceiver>::cast(handler), |
| 8796 | isolate->factory()->ownKeys_string()), |
| 8797 | Nothing<bool>()); |
| 8798 | // 6. If trap is undefined, then |
| 8799 | if (trap->IsUndefined()) { |
| 8800 | // 6a. Return target.[[OwnPropertyKeys]](). |
| 8801 | return GetKeys_Internal(isolate, receiver, target, OWN_ONLY, filter, |
| 8802 | accumulator); |
| 8803 | } |
| 8804 | // 7. Let trapResultArray be Call(trap, handler, «target»). |
| 8805 | Handle<Object> trap_result_array; |
| 8806 | Handle<Object> args[] = {target}; |
| 8807 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 8808 | isolate, trap_result_array, |
| 8809 | Execution::Call(isolate, trap, handler, arraysize(args), args), |
| 8810 | Nothing<bool>()); |
| 8811 | // 8. Let trapResult be ? CreateListFromArrayLike(trapResultArray, |
| 8812 | // «String, Symbol»). |
| 8813 | Handle<FixedArray> trap_result; |
| 8814 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 8815 | isolate, trap_result, |
| 8816 | Object::CreateListFromArrayLike(isolate, trap_result_array, |
| 8817 | ElementTypes::kStringAndSymbol), |
| 8818 | Nothing<bool>()); |
| 8819 | // 9. Let extensibleTarget be ? IsExtensible(target). |
| 8820 | Maybe<bool> maybe_extensible = JSReceiver::IsExtensible(target); |
| 8821 | MAYBE_RETURN(maybe_extensible, Nothing<bool>()); |
| 8822 | bool extensible_target = maybe_extensible.FromJust(); |
| 8823 | // 10. Let targetKeys be ? target.[[OwnPropertyKeys]](). |
| 8824 | Handle<FixedArray> target_keys; |
| 8825 | ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, target_keys, |
| 8826 | JSReceiver::OwnPropertyKeys(target), |
| 8827 | Nothing<bool>()); |
| 8828 | // 11. (Assert) |
| 8829 | // 12. Let targetConfigurableKeys be an empty List. |
| 8830 | // To save memory, we're re-using target_keys and will modify it in-place. |
| 8831 | Handle<FixedArray> target_configurable_keys = target_keys; |
| 8832 | // 13. Let targetNonconfigurableKeys be an empty List. |
| 8833 | Handle<FixedArray> target_nonconfigurable_keys = |
| 8834 | isolate->factory()->NewFixedArray(target_keys->length()); |
| 8835 | int nonconfigurable_keys_length = 0; |
| 8836 | // 14. Repeat, for each element key of targetKeys: |
| 8837 | for (int i = 0; i < target_keys->length(); ++i) { |
| 8838 | // 14a. Let desc be ? target.[[GetOwnProperty]](key). |
| 8839 | PropertyDescriptor desc; |
| 8840 | Maybe<bool> found = JSReceiver::GetOwnPropertyDescriptor( |
| 8841 | isolate, target, handle(target_keys->get(i), isolate), &desc); |
| 8842 | MAYBE_RETURN(found, Nothing<bool>()); |
| 8843 | // 14b. If desc is not undefined and desc.[[Configurable]] is false, then |
| 8844 | if (found.FromJust() && !desc.configurable()) { |
| 8845 | // 14b i. Append key as an element of targetNonconfigurableKeys. |
| 8846 | target_nonconfigurable_keys->set(nonconfigurable_keys_length, |
| 8847 | target_keys->get(i)); |
| 8848 | nonconfigurable_keys_length++; |
| 8849 | // The key was moved, null it out in the original list. |
| 8850 | target_keys->set(i, Smi::FromInt(0)); |
| 8851 | } else { |
| 8852 | // 14c. Else, |
| 8853 | // 14c i. Append key as an element of targetConfigurableKeys. |
| 8854 | // (No-op, just keep it in |target_keys|.) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8855 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8856 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8857 | accumulator->NextPrototype(); // Prepare for accumulating keys. |
| 8858 | // 15. If extensibleTarget is true and targetNonconfigurableKeys is empty, |
| 8859 | // then: |
| 8860 | if (extensible_target && nonconfigurable_keys_length == 0) { |
| 8861 | // 15a. Return trapResult. |
| 8862 | return accumulator->AddKeysFromProxy(proxy, trap_result); |
| 8863 | } |
| 8864 | // 16. Let uncheckedResultKeys be a new List which is a copy of trapResult. |
| 8865 | Zone set_zone; |
| 8866 | const int kPresent = 1; |
| 8867 | const int kGone = 0; |
| 8868 | IdentityMap<int> unchecked_result_keys(isolate->heap(), &set_zone); |
| 8869 | int unchecked_result_keys_size = trap_result->length(); |
| 8870 | for (int i = 0; i < trap_result->length(); ++i) { |
| 8871 | DCHECK(trap_result->get(i)->IsUniqueName()); |
| 8872 | unchecked_result_keys.Set(trap_result->get(i), kPresent); |
| 8873 | } |
| 8874 | // 17. Repeat, for each key that is an element of targetNonconfigurableKeys: |
| 8875 | for (int i = 0; i < nonconfigurable_keys_length; ++i) { |
| 8876 | Object* key = target_nonconfigurable_keys->get(i); |
| 8877 | // 17a. If key is not an element of uncheckedResultKeys, throw a |
| 8878 | // TypeError exception. |
| 8879 | int* found = unchecked_result_keys.Find(key); |
| 8880 | if (found == nullptr || *found == kGone) { |
| 8881 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 8882 | MessageTemplate::kProxyOwnKeysMissing, handle(key, isolate))); |
| 8883 | return Nothing<bool>(); |
| 8884 | } |
| 8885 | // 17b. Remove key from uncheckedResultKeys. |
| 8886 | *found = kGone; |
| 8887 | unchecked_result_keys_size--; |
| 8888 | } |
| 8889 | // 18. If extensibleTarget is true, return trapResult. |
| 8890 | if (extensible_target) { |
| 8891 | return accumulator->AddKeysFromProxy(proxy, trap_result); |
| 8892 | } |
| 8893 | // 19. Repeat, for each key that is an element of targetConfigurableKeys: |
| 8894 | for (int i = 0; i < target_configurable_keys->length(); ++i) { |
| 8895 | Object* key = target_configurable_keys->get(i); |
| 8896 | if (key->IsSmi()) continue; // Zapped entry, was nonconfigurable. |
| 8897 | // 19a. If key is not an element of uncheckedResultKeys, throw a |
| 8898 | // TypeError exception. |
| 8899 | int* found = unchecked_result_keys.Find(key); |
| 8900 | if (found == nullptr || *found == kGone) { |
| 8901 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 8902 | MessageTemplate::kProxyOwnKeysMissing, handle(key, isolate))); |
| 8903 | return Nothing<bool>(); |
| 8904 | } |
| 8905 | // 19b. Remove key from uncheckedResultKeys. |
| 8906 | *found = kGone; |
| 8907 | unchecked_result_keys_size--; |
| 8908 | } |
| 8909 | // 20. If uncheckedResultKeys is not empty, throw a TypeError exception. |
| 8910 | if (unchecked_result_keys_size != 0) { |
| 8911 | DCHECK_GT(unchecked_result_keys_size, 0); |
| 8912 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 8913 | MessageTemplate::kProxyOwnKeysNonExtensible)); |
| 8914 | return Nothing<bool>(); |
| 8915 | } |
| 8916 | // 21. Return trapResult. |
| 8917 | return accumulator->AddKeysFromProxy(proxy, trap_result); |
| 8918 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8919 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8920 | |
| 8921 | MaybeHandle<FixedArray> JSReceiver::GetKeys(Handle<JSReceiver> object, |
| 8922 | KeyCollectionType type, |
| 8923 | PropertyFilter filter, |
| 8924 | GetKeysConversion keys_conversion) { |
| 8925 | USE(ContainsOnlyValidKeys); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8926 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8927 | KeyAccumulator accumulator(isolate, filter); |
| 8928 | MAYBE_RETURN( |
| 8929 | GetKeys_Internal(isolate, object, object, type, filter, &accumulator), |
| 8930 | MaybeHandle<FixedArray>()); |
| 8931 | Handle<FixedArray> keys = accumulator.GetKeys(keys_conversion); |
| 8932 | DCHECK(ContainsOnlyValidKeys(keys)); |
| 8933 | return keys; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8934 | } |
| 8935 | |
| 8936 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8937 | bool Map::DictionaryElementsInPrototypeChainOnly() { |
| 8938 | if (IsDictionaryElementsKind(elements_kind())) { |
| 8939 | return false; |
| 8940 | } |
| 8941 | |
| 8942 | for (PrototypeIterator iter(this); !iter.IsAtEnd(); iter.Advance()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8943 | // Be conservative, don't walk into proxies. |
| 8944 | if (iter.GetCurrent()->IsJSProxy()) return true; |
| 8945 | // String wrappers have non-configurable, non-writable elements. |
| 8946 | if (iter.GetCurrent()->IsStringWrapper()) return true; |
| 8947 | JSObject* current = iter.GetCurrent<JSObject>(); |
| 8948 | |
| 8949 | if (current->HasDictionaryElements() && |
| 8950 | current->element_dictionary()->requires_slow_elements()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8951 | return true; |
| 8952 | } |
| 8953 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8954 | if (current->HasSlowArgumentsElements()) { |
| 8955 | FixedArray* parameter_map = FixedArray::cast(current->elements()); |
| 8956 | Object* arguments = parameter_map->get(1); |
| 8957 | if (SeededNumberDictionary::cast(arguments)->requires_slow_elements()) { |
| 8958 | return true; |
| 8959 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8960 | } |
| 8961 | } |
| 8962 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8963 | return false; |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 8964 | } |
| 8965 | |
| 8966 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8967 | MaybeHandle<Object> JSObject::DefineAccessor(Handle<JSObject> object, |
| 8968 | Handle<Name> name, |
| 8969 | Handle<Object> getter, |
| 8970 | Handle<Object> setter, |
| 8971 | PropertyAttributes attributes) { |
| 8972 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8973 | |
| 8974 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 8975 | isolate, object, name, LookupIterator::HIDDEN_SKIP_INTERCEPTOR); |
| 8976 | return DefineAccessor(&it, getter, setter, attributes); |
| 8977 | } |
| 8978 | |
| 8979 | |
| 8980 | MaybeHandle<Object> JSObject::DefineAccessor(LookupIterator* it, |
| 8981 | Handle<Object> getter, |
| 8982 | Handle<Object> setter, |
| 8983 | PropertyAttributes attributes) { |
| 8984 | Isolate* isolate = it->isolate(); |
| 8985 | |
| 8986 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8993 | } |
| 8994 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 8995 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8999 | } |
| 9000 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9001 | Handle<Object> old_value = isolate->factory()->the_hole_value(); |
| 9002 | bool is_observed = object->map()->is_observed() && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9003 | !isolate->IsInternallyUsedPropertyName(it->GetName()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9004 | bool preexists = false; |
| 9005 | if (is_observed) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9006 | CHECK(GetPropertyAttributes(it).IsJust()); |
| 9007 | preexists = it->IsFound(); |
| 9008 | if (preexists && (it->state() == LookupIterator::DATA || |
| 9009 | it->GetAccessors()->IsAccessorInfo())) { |
| 9010 | old_value = GetProperty(it).ToHandleChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9011 | } |
| 9012 | } |
| 9013 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9014 | DCHECK(getter->IsCallable() || getter->IsUndefined() || getter->IsNull()); |
| 9015 | DCHECK(setter->IsCallable() || setter->IsUndefined() || setter->IsNull()); |
| 9016 | // At least one of the accessors needs to be a new value. |
| 9017 | DCHECK(!getter->IsNull() || !setter->IsNull()); |
| 9018 | if (!getter->IsNull()) { |
| 9019 | it->TransitionToAccessorProperty(ACCESSOR_GETTER, getter, attributes); |
| 9020 | } |
| 9021 | if (!setter->IsNull()) { |
| 9022 | it->TransitionToAccessorProperty(ACCESSOR_SETTER, setter, attributes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9023 | } |
| 9024 | |
| 9025 | if (is_observed) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9026 | // Make sure the top context isn't changed. |
| 9027 | AssertNoContextChange ncc(isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9028 | const char* type = preexists ? "reconfigure" : "add"; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9029 | RETURN_ON_EXCEPTION( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9030 | isolate, EnqueueChangeRecord(object, type, it->GetName(), old_value), |
| 9031 | Object); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9032 | } |
| 9033 | |
| 9034 | return isolate->factory()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9035 | } |
| 9036 | |
| 9037 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9038 | MaybeHandle<Object> JSObject::SetAccessor(Handle<JSObject> object, |
| 9039 | Handle<AccessorInfo> info) { |
| 9040 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9041 | Handle<Name> name(Name::cast(info->name()), isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9042 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9043 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 9044 | isolate, object, name, LookupIterator::HIDDEN_SKIP_INTERCEPTOR); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 9045 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9046 | // Duplicate ACCESS_CHECK outside of GetPropertyAttributes for the case that |
| 9047 | // the FailedAccessCheckCallbackFunction doesn't throw an exception. |
| 9048 | // |
| 9049 | // TODO(verwaest): Force throw an exception if the callback doesn't, so we can |
| 9050 | // remove reliance on default return values. |
| 9051 | if (it.state() == LookupIterator::ACCESS_CHECK) { |
| 9052 | if (!it.HasAccess()) { |
| 9053 | isolate->ReportFailedAccessCheck(object); |
| 9054 | RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 9055 | return it.factory()->undefined_value(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 9056 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9057 | it.Next(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 9058 | } |
| 9059 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9060 | // Ignore accessors on typed arrays. |
| 9061 | if (it.IsElement() && object->HasFixedTypedArrayElements()) { |
| 9062 | return it.factory()->undefined_value(); |
| 9063 | } |
| 9064 | |
| 9065 | CHECK(GetPropertyAttributes(&it).IsJust()); |
| 9066 | |
| 9067 | // ES5 forbids turning a property into an accessor if it's not |
| 9068 | // configurable. See 8.6.1 (Table 5). |
| 9069 | if (it.IsFound() && !it.IsConfigurable()) { |
| 9070 | return it.factory()->undefined_value(); |
| 9071 | } |
| 9072 | |
| 9073 | it.TransitionToAccessorPair(info, info->property_attributes()); |
| 9074 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9075 | return object; |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 9076 | } |
| 9077 | |
| 9078 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9079 | MaybeHandle<Object> JSObject::GetAccessor(Handle<JSObject> object, |
| 9080 | Handle<Name> name, |
| 9081 | AccessorComponent component) { |
| 9082 | Isolate* isolate = object->GetIsolate(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9083 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9084 | // Make sure that the top context does not change when doing callbacks or |
| 9085 | // interceptor calls. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9086 | AssertNoContextChange ncc(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9087 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9088 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 9089 | isolate, object, name, LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR); |
| 9090 | |
| 9091 | for (; it.IsFound(); it.Next()) { |
| 9092 | switch (it.state()) { |
| 9093 | case LookupIterator::INTERCEPTOR: |
| 9094 | case LookupIterator::NOT_FOUND: |
| 9095 | case LookupIterator::TRANSITION: |
| 9096 | UNREACHABLE(); |
| 9097 | |
| 9098 | case LookupIterator::ACCESS_CHECK: |
| 9099 | if (it.HasAccess()) continue; |
| 9100 | isolate->ReportFailedAccessCheck(it.GetHolder<JSObject>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9101 | RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 9102 | return isolate->factory()->undefined_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9103 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9104 | case LookupIterator::JSPROXY: |
| 9105 | return isolate->factory()->undefined_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9106 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9107 | case LookupIterator::INTEGER_INDEXED_EXOTIC: |
| 9108 | return isolate->factory()->undefined_value(); |
| 9109 | case LookupIterator::DATA: |
| 9110 | continue; |
| 9111 | case LookupIterator::ACCESSOR: { |
| 9112 | Handle<Object> maybe_pair = it.GetAccessors(); |
| 9113 | if (maybe_pair->IsAccessorPair()) { |
| 9114 | return handle( |
| 9115 | AccessorPair::cast(*maybe_pair)->GetComponent(component), |
| 9116 | isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9117 | } |
| 9118 | } |
| 9119 | } |
| 9120 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9121 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9122 | return isolate->factory()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9123 | } |
| 9124 | |
| 9125 | |
| 9126 | Object* JSObject::SlowReverseLookup(Object* value) { |
| 9127 | if (HasFastProperties()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9128 | int number_of_own_descriptors = map()->NumberOfOwnDescriptors(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9129 | DescriptorArray* descs = map()->instance_descriptors(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9130 | bool value_is_number = value->IsNumber(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9131 | for (int i = 0; i < number_of_own_descriptors; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9132 | if (descs->GetType(i) == DATA) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9133 | FieldIndex field_index = FieldIndex::ForDescriptor(map(), i); |
| 9134 | if (IsUnboxedDoubleField(field_index)) { |
| 9135 | if (value_is_number) { |
| 9136 | double property = RawFastDoublePropertyAt(field_index); |
| 9137 | if (property == value->Number()) { |
| 9138 | return descs->GetKey(i); |
| 9139 | } |
| 9140 | } |
| 9141 | } else { |
| 9142 | Object* property = RawFastPropertyAt(field_index); |
| 9143 | if (field_index.is_double()) { |
| 9144 | DCHECK(property->IsMutableHeapNumber()); |
| 9145 | if (value_is_number && property->Number() == value->Number()) { |
| 9146 | return descs->GetKey(i); |
| 9147 | } |
| 9148 | } else if (property == value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9149 | return descs->GetKey(i); |
| 9150 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9151 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9152 | } else if (descs->GetType(i) == DATA_CONSTANT) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9153 | if (descs->GetConstant(i) == value) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9154 | return descs->GetKey(i); |
| 9155 | } |
| 9156 | } |
| 9157 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9158 | return GetHeap()->undefined_value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9159 | } else if (IsJSGlobalObject()) { |
| 9160 | return global_dictionary()->SlowReverseLookup(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9161 | } else { |
| 9162 | return property_dictionary()->SlowReverseLookup(value); |
| 9163 | } |
| 9164 | } |
| 9165 | |
| 9166 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9167 | Handle<Map> Map::RawCopy(Handle<Map> map, int instance_size) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9168 | Isolate* isolate = map->GetIsolate(); |
| 9169 | Handle<Map> result = |
| 9170 | isolate->factory()->NewMap(map->instance_type(), instance_size); |
| 9171 | Handle<Object> prototype(map->prototype(), isolate); |
| 9172 | Map::SetPrototype(result, prototype); |
| 9173 | result->set_constructor_or_backpointer(map->GetConstructor()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9174 | result->set_bit_field(map->bit_field()); |
| 9175 | result->set_bit_field2(map->bit_field2()); |
| 9176 | int new_bit_field3 = map->bit_field3(); |
| 9177 | new_bit_field3 = OwnsDescriptors::update(new_bit_field3, true); |
| 9178 | new_bit_field3 = NumberOfOwnDescriptorsBits::update(new_bit_field3, 0); |
| 9179 | new_bit_field3 = EnumLengthBits::update(new_bit_field3, |
| 9180 | kInvalidEnumCacheSentinel); |
| 9181 | new_bit_field3 = Deprecated::update(new_bit_field3, false); |
| 9182 | if (!map->is_dictionary_map()) { |
| 9183 | new_bit_field3 = IsUnstable::update(new_bit_field3, false); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9184 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9185 | new_bit_field3 = |
| 9186 | ConstructionCounter::update(new_bit_field3, kNoSlackTracking); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9187 | result->set_bit_field3(new_bit_field3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9188 | return result; |
| 9189 | } |
| 9190 | |
| 9191 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9192 | Handle<Map> Map::Normalize(Handle<Map> fast_map, PropertyNormalizationMode mode, |
| 9193 | const char* reason) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9194 | DCHECK(!fast_map->is_dictionary_map()); |
| 9195 | |
| 9196 | Isolate* isolate = fast_map->GetIsolate(); |
| 9197 | Handle<Object> maybe_cache(isolate->native_context()->normalized_map_cache(), |
| 9198 | isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9199 | bool use_cache = !fast_map->is_prototype_map() && !maybe_cache->IsUndefined(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9200 | Handle<NormalizedMapCache> cache; |
| 9201 | if (use_cache) cache = Handle<NormalizedMapCache>::cast(maybe_cache); |
| 9202 | |
| 9203 | Handle<Map> new_map; |
| 9204 | if (use_cache && cache->Get(fast_map, mode).ToHandle(&new_map)) { |
| 9205 | #ifdef VERIFY_HEAP |
| 9206 | if (FLAG_verify_heap) new_map->DictionaryMapVerify(); |
| 9207 | #endif |
| 9208 | #ifdef ENABLE_SLOW_DCHECKS |
| 9209 | if (FLAG_enable_slow_asserts) { |
| 9210 | // The cached map should match newly created normalized map bit-by-bit, |
| 9211 | // except for the code cache, which can contain some ics which can be |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9212 | // applied to the shared map, dependent code and weak cell cache. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9213 | Handle<Map> fresh = Map::CopyNormalized(fast_map, mode); |
| 9214 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9215 | if (new_map->is_prototype_map()) { |
| 9216 | // For prototype maps, the PrototypeInfo is not copied. |
| 9217 | DCHECK(memcmp(fresh->address(), new_map->address(), |
| 9218 | kTransitionsOrPrototypeInfoOffset) == 0); |
| 9219 | DCHECK(fresh->raw_transitions() == Smi::FromInt(0)); |
| 9220 | STATIC_ASSERT(kDescriptorsOffset == |
| 9221 | kTransitionsOrPrototypeInfoOffset + kPointerSize); |
| 9222 | DCHECK(memcmp(HeapObject::RawField(*fresh, kDescriptorsOffset), |
| 9223 | HeapObject::RawField(*new_map, kDescriptorsOffset), |
| 9224 | kCodeCacheOffset - kDescriptorsOffset) == 0); |
| 9225 | } else { |
| 9226 | DCHECK(memcmp(fresh->address(), new_map->address(), |
| 9227 | Map::kCodeCacheOffset) == 0); |
| 9228 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9229 | STATIC_ASSERT(Map::kDependentCodeOffset == |
| 9230 | Map::kCodeCacheOffset + kPointerSize); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9231 | STATIC_ASSERT(Map::kWeakCellCacheOffset == |
| 9232 | Map::kDependentCodeOffset + kPointerSize); |
| 9233 | int offset = Map::kWeakCellCacheOffset + kPointerSize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9234 | DCHECK(memcmp(fresh->address() + offset, |
| 9235 | new_map->address() + offset, |
| 9236 | Map::kSize - offset) == 0); |
| 9237 | } |
| 9238 | #endif |
| 9239 | } else { |
| 9240 | new_map = Map::CopyNormalized(fast_map, mode); |
| 9241 | if (use_cache) { |
| 9242 | cache->Set(fast_map, new_map); |
| 9243 | isolate->counters()->normalized_maps()->Increment(); |
| 9244 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9245 | #if TRACE_MAPS |
| 9246 | if (FLAG_trace_maps) { |
| 9247 | PrintF("[TraceMaps: Normalize from= %p to= %p reason= %s ]\n", |
| 9248 | reinterpret_cast<void*>(*fast_map), |
| 9249 | reinterpret_cast<void*>(*new_map), reason); |
| 9250 | } |
| 9251 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9252 | } |
| 9253 | fast_map->NotifyLeafMapLayoutChange(); |
| 9254 | return new_map; |
| 9255 | } |
| 9256 | |
| 9257 | |
| 9258 | Handle<Map> Map::CopyNormalized(Handle<Map> map, |
| 9259 | PropertyNormalizationMode mode) { |
| 9260 | int new_instance_size = map->instance_size(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 9261 | if (mode == CLEAR_INOBJECT_PROPERTIES) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9262 | new_instance_size -= map->GetInObjectProperties() * kPointerSize; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 9263 | } |
| 9264 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9265 | Handle<Map> result = RawCopy(map, new_instance_size); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 9266 | |
| 9267 | if (mode != CLEAR_INOBJECT_PROPERTIES) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9268 | result->SetInObjectProperties(map->GetInObjectProperties()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 9269 | } |
| 9270 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9271 | result->set_dictionary_map(true); |
| 9272 | result->set_migration_target(false); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 9273 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9274 | #ifdef VERIFY_HEAP |
| 9275 | if (FLAG_verify_heap) result->DictionaryMapVerify(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 9276 | #endif |
| 9277 | |
| 9278 | return result; |
| 9279 | } |
| 9280 | |
| 9281 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9282 | Handle<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(); |
| 9287 | // Strict and strong function maps have Function as a constructor but the |
| 9288 | // 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() || |
| 9294 | *map == *isolate->strong_function_map() || |
| 9295 | *map == *isolate->strict_generator_function_map() || |
| 9296 | *map == *isolate->strong_generator_function_map()); |
| 9297 | #endif |
| 9298 | // Initial maps must always own their descriptors and it's descriptor array |
| 9299 | // does not contain descriptors that do not belong to the map. |
| 9300 | DCHECK(map->owns_descriptors()); |
| 9301 | DCHECK_EQ(map->NumberOfOwnDescriptors(), |
| 9302 | map->instance_descriptors()->number_of_descriptors()); |
| 9303 | |
| 9304 | Handle<Map> result = RawCopy(map, instance_size); |
| 9305 | |
| 9306 | // Please note instance_type and instance_size are set when allocated. |
| 9307 | result->SetInObjectProperties(in_object_properties); |
| 9308 | result->set_unused_property_fields(unused_property_fields); |
| 9309 | |
| 9310 | int number_of_own_descriptors = map->NumberOfOwnDescriptors(); |
| 9311 | if (number_of_own_descriptors > 0) { |
| 9312 | // The copy will use the same descriptors array. |
| 9313 | result->UpdateDescriptors(map->instance_descriptors(), |
| 9314 | map->GetLayoutDescriptor()); |
| 9315 | result->SetNumberOfOwnDescriptors(number_of_own_descriptors); |
| 9316 | |
| 9317 | DCHECK_EQ(result->NumberOfFields(), |
| 9318 | in_object_properties - unused_property_fields); |
| 9319 | } |
| 9320 | |
| 9321 | return result; |
| 9322 | } |
| 9323 | |
| 9324 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9325 | Handle<Map> Map::CopyDropDescriptors(Handle<Map> map) { |
| 9326 | Handle<Map> result = RawCopy(map, map->instance_size()); |
| 9327 | |
| 9328 | // Please note instance_type and instance_size are set when allocated. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9329 | if (map->IsJSObjectMap()) { |
| 9330 | result->SetInObjectProperties(map->GetInObjectProperties()); |
| 9331 | result->set_unused_property_fields(map->unused_property_fields()); |
| 9332 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9333 | result->ClearCodeCache(map->GetHeap()); |
| 9334 | map->NotifyLeafMapLayoutChange(); |
| 9335 | return result; |
| 9336 | } |
| 9337 | |
| 9338 | |
| 9339 | Handle<Map> Map::ShareDescriptor(Handle<Map> map, |
| 9340 | Handle<DescriptorArray> descriptors, |
| 9341 | Descriptor* descriptor) { |
| 9342 | // Sanity check. This path is only to be taken if the map owns its descriptor |
| 9343 | // array, implying that its NumberOfOwnDescriptors equals the number of |
| 9344 | // descriptors in the descriptor array. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9345 | DCHECK_EQ(map->NumberOfOwnDescriptors(), |
| 9346 | map->instance_descriptors()->number_of_descriptors()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9347 | |
| 9348 | Handle<Map> result = CopyDropDescriptors(map); |
| 9349 | Handle<Name> name = descriptor->GetKey(); |
| 9350 | |
| 9351 | // Ensure there's space for the new descriptor in the shared descriptor array. |
| 9352 | if (descriptors->NumberOfSlackDescriptors() == 0) { |
| 9353 | int old_size = descriptors->number_of_descriptors(); |
| 9354 | if (old_size == 0) { |
| 9355 | descriptors = DescriptorArray::Allocate(map->GetIsolate(), 0, 1); |
| 9356 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9357 | int slack = SlackForArraySize(old_size, kMaxNumberOfDescriptors); |
| 9358 | EnsureDescriptorSlack(map, slack); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9359 | descriptors = handle(map->instance_descriptors()); |
| 9360 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9361 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9362 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9363 | Handle<LayoutDescriptor> layout_descriptor = |
| 9364 | FLAG_unbox_double_fields |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9365 | ? LayoutDescriptor::ShareAppend(map, descriptor->GetDetails()) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9366 | : handle(LayoutDescriptor::FastPointerLayout(), map->GetIsolate()); |
| 9367 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9368 | { |
| 9369 | DisallowHeapAllocation no_gc; |
| 9370 | descriptors->Append(descriptor); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9371 | result->InitializeDescriptors(*descriptors, *layout_descriptor); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9372 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9373 | |
| 9374 | DCHECK(result->NumberOfOwnDescriptors() == map->NumberOfOwnDescriptors() + 1); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9375 | ConnectTransition(map, result, name, SIMPLE_PROPERTY_TRANSITION); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9376 | |
| 9377 | return result; |
| 9378 | } |
| 9379 | |
| 9380 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9381 | #if TRACE_MAPS |
| 9382 | |
| 9383 | // static |
| 9384 | void Map::TraceTransition(const char* what, Map* from, Map* to, Name* name) { |
| 9385 | if (FLAG_trace_maps) { |
| 9386 | PrintF("[TraceMaps: %s from= %p to= %p name= ", what, |
| 9387 | reinterpret_cast<void*>(from), reinterpret_cast<void*>(to)); |
| 9388 | name->NameShortPrint(); |
| 9389 | PrintF(" ]\n"); |
| 9390 | } |
| 9391 | } |
| 9392 | |
| 9393 | |
| 9394 | // static |
| 9395 | void Map::TraceAllTransitions(Map* map) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9396 | Object* transitions = map->raw_transitions(); |
| 9397 | int num_transitions = TransitionArray::NumberOfTransitions(transitions); |
| 9398 | for (int i = -0; i < num_transitions; ++i) { |
| 9399 | Map* target = TransitionArray::GetTarget(transitions, i); |
| 9400 | Name* key = TransitionArray::GetKey(transitions, i); |
| 9401 | Map::TraceTransition("Transition", map, target, key); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9402 | Map::TraceAllTransitions(target); |
| 9403 | } |
| 9404 | } |
| 9405 | |
| 9406 | #endif // TRACE_MAPS |
| 9407 | |
| 9408 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9409 | void Map::ConnectTransition(Handle<Map> parent, Handle<Map> child, |
| 9410 | Handle<Name> name, SimpleTransitionFlag flag) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9411 | if (!parent->GetBackPointer()->IsUndefined()) { |
| 9412 | parent->set_owns_descriptors(false); |
| 9413 | } else { |
| 9414 | // |parent| is initial map and it must keep the ownership, there must be no |
| 9415 | // descriptors in the descriptors array that do not belong to the map. |
| 9416 | DCHECK(parent->owns_descriptors()); |
| 9417 | DCHECK_EQ(parent->NumberOfOwnDescriptors(), |
| 9418 | parent->instance_descriptors()->number_of_descriptors()); |
| 9419 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9420 | if (parent->is_prototype_map()) { |
| 9421 | DCHECK(child->is_prototype_map()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9422 | #if TRACE_MAPS |
| 9423 | Map::TraceTransition("NoTransition", *parent, *child, *name); |
| 9424 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9425 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9426 | TransitionArray::Insert(parent, name, child, flag); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9427 | #if TRACE_MAPS |
| 9428 | Map::TraceTransition("Transition", *parent, *child, *name); |
| 9429 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9430 | } |
| 9431 | } |
| 9432 | |
| 9433 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9434 | Handle<Map> Map::CopyReplaceDescriptors( |
| 9435 | Handle<Map> map, Handle<DescriptorArray> descriptors, |
| 9436 | Handle<LayoutDescriptor> layout_descriptor, TransitionFlag flag, |
| 9437 | MaybeHandle<Name> maybe_name, const char* reason, |
| 9438 | SimpleTransitionFlag simple_flag) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9439 | DCHECK(descriptors->IsSortedNoDuplicates()); |
| 9440 | |
| 9441 | Handle<Map> result = CopyDropDescriptors(map); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9442 | |
| 9443 | if (!map->is_prototype_map()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9444 | if (flag == INSERT_TRANSITION && |
| 9445 | TransitionArray::CanHaveMoreTransitions(map)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9446 | result->InitializeDescriptors(*descriptors, *layout_descriptor); |
| 9447 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9448 | Handle<Name> name; |
| 9449 | CHECK(maybe_name.ToHandle(&name)); |
| 9450 | ConnectTransition(map, result, name, simple_flag); |
| 9451 | } else { |
| 9452 | int length = descriptors->number_of_descriptors(); |
| 9453 | for (int i = 0; i < length; i++) { |
| 9454 | descriptors->SetRepresentation(i, Representation::Tagged()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9455 | if (descriptors->GetDetails(i).type() == DATA) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9456 | descriptors->SetValue(i, HeapType::Any()); |
| 9457 | } |
| 9458 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9459 | result->InitializeDescriptors(*descriptors, |
| 9460 | LayoutDescriptor::FastPointerLayout()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9461 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9462 | } else { |
| 9463 | result->InitializeDescriptors(*descriptors, *layout_descriptor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9464 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9465 | #if TRACE_MAPS |
| 9466 | if (FLAG_trace_maps && |
| 9467 | // Mirror conditions above that did not call ConnectTransition(). |
| 9468 | (map->is_prototype_map() || |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9469 | !(flag == INSERT_TRANSITION && |
| 9470 | TransitionArray::CanHaveMoreTransitions(map)))) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9471 | PrintF("[TraceMaps: ReplaceDescriptors from= %p to= %p reason= %s ]\n", |
| 9472 | reinterpret_cast<void*>(*map), reinterpret_cast<void*>(*result), |
| 9473 | reason); |
| 9474 | } |
| 9475 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9476 | |
| 9477 | return result; |
| 9478 | } |
| 9479 | |
| 9480 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9481 | // Creates transition tree starting from |split_map| and adding all descriptors |
| 9482 | // starting from descriptor with index |split_map|.NumberOfOwnDescriptors(). |
| 9483 | // The way how it is done is tricky because of GC and special descriptors |
| 9484 | // marking logic. |
| 9485 | Handle<Map> Map::AddMissingTransitions( |
| 9486 | Handle<Map> split_map, Handle<DescriptorArray> descriptors, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9487 | Handle<LayoutDescriptor> full_layout_descriptor) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9488 | DCHECK(descriptors->IsSortedNoDuplicates()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9489 | int split_nof = split_map->NumberOfOwnDescriptors(); |
| 9490 | int nof_descriptors = descriptors->number_of_descriptors(); |
| 9491 | DCHECK_LT(split_nof, nof_descriptors); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9492 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9493 | // Start with creating last map which will own full descriptors array. |
| 9494 | // This is necessary to guarantee that GC will mark the whole descriptor |
| 9495 | // array if any of the allocations happening below fail. |
| 9496 | // Number of unused properties is temporarily incorrect and the layout |
| 9497 | // descriptor could unnecessarily be in slow mode but we will fix after |
| 9498 | // all the other intermediate maps are created. |
| 9499 | Handle<Map> last_map = CopyDropDescriptors(split_map); |
| 9500 | last_map->InitializeDescriptors(*descriptors, *full_layout_descriptor); |
| 9501 | last_map->set_unused_property_fields(0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9502 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9503 | // During creation of intermediate maps we violate descriptors sharing |
| 9504 | // invariant since the last map is not yet connected to the transition tree |
| 9505 | // we create here. But it is safe because GC never trims map's descriptors |
| 9506 | // if there are no dead transitions from that map and this is exactly the |
| 9507 | // case for all the intermediate maps we create here. |
| 9508 | Handle<Map> map = split_map; |
| 9509 | for (int i = split_nof; i < nof_descriptors - 1; ++i) { |
| 9510 | Handle<Map> new_map = CopyDropDescriptors(map); |
| 9511 | InstallDescriptors(map, new_map, i, descriptors, full_layout_descriptor); |
| 9512 | map = new_map; |
| 9513 | } |
| 9514 | map->NotifyLeafMapLayoutChange(); |
| 9515 | InstallDescriptors(map, last_map, nof_descriptors - 1, descriptors, |
| 9516 | full_layout_descriptor); |
| 9517 | return last_map; |
| 9518 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9519 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9520 | |
| 9521 | // Since this method is used to rewrite an existing transition tree, it can |
| 9522 | // always insert transitions without checking. |
| 9523 | void Map::InstallDescriptors(Handle<Map> parent, Handle<Map> child, |
| 9524 | int new_descriptor, |
| 9525 | Handle<DescriptorArray> descriptors, |
| 9526 | Handle<LayoutDescriptor> full_layout_descriptor) { |
| 9527 | DCHECK(descriptors->IsSortedNoDuplicates()); |
| 9528 | |
| 9529 | child->set_instance_descriptors(*descriptors); |
| 9530 | child->SetNumberOfOwnDescriptors(new_descriptor + 1); |
| 9531 | |
| 9532 | int unused_property_fields = parent->unused_property_fields(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9533 | PropertyDetails details = descriptors->GetDetails(new_descriptor); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9534 | if (details.location() == kField) { |
| 9535 | unused_property_fields = parent->unused_property_fields() - 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9536 | if (unused_property_fields < 0) { |
| 9537 | unused_property_fields += JSObject::kFieldsAdded; |
| 9538 | } |
| 9539 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9540 | child->set_unused_property_fields(unused_property_fields); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9541 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9542 | if (FLAG_unbox_double_fields) { |
| 9543 | Handle<LayoutDescriptor> layout_descriptor = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9544 | LayoutDescriptor::AppendIfFastOrUseFull(parent, details, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9545 | full_layout_descriptor); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9546 | child->set_layout_descriptor(*layout_descriptor); |
| 9547 | #ifdef VERIFY_HEAP |
| 9548 | // TODO(ishell): remove these checks from VERIFY_HEAP mode. |
| 9549 | if (FLAG_verify_heap) { |
| 9550 | CHECK(child->layout_descriptor()->IsConsistentWithMap(*child)); |
| 9551 | } |
| 9552 | #else |
| 9553 | SLOW_DCHECK(child->layout_descriptor()->IsConsistentWithMap(*child)); |
| 9554 | #endif |
| 9555 | child->set_visitor_id(Heap::GetStaticVisitorIdForMap(*child)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9556 | } |
| 9557 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9558 | Handle<Name> name = handle(descriptors->GetKey(new_descriptor)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9559 | ConnectTransition(parent, child, name, SIMPLE_PROPERTY_TRANSITION); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9560 | } |
| 9561 | |
| 9562 | |
| 9563 | Handle<Map> Map::CopyAsElementsKind(Handle<Map> map, ElementsKind kind, |
| 9564 | TransitionFlag flag) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9565 | Map* maybe_elements_transition_map = NULL; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9566 | if (flag == INSERT_TRANSITION) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9567 | maybe_elements_transition_map = map->ElementsTransitionMap(); |
| 9568 | DCHECK(maybe_elements_transition_map == NULL || |
| 9569 | (maybe_elements_transition_map->elements_kind() == |
| 9570 | DICTIONARY_ELEMENTS && |
| 9571 | kind == DICTIONARY_ELEMENTS)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9572 | DCHECK(!IsFastElementsKind(kind) || |
| 9573 | IsMoreGeneralElementsKindTransition(map->elements_kind(), kind)); |
| 9574 | DCHECK(kind != map->elements_kind()); |
| 9575 | } |
| 9576 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9577 | bool insert_transition = flag == INSERT_TRANSITION && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9578 | TransitionArray::CanHaveMoreTransitions(map) && |
| 9579 | maybe_elements_transition_map == NULL; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9580 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9581 | if (insert_transition) { |
| 9582 | Handle<Map> new_map = CopyForTransition(map, "CopyAsElementsKind"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9583 | new_map->set_elements_kind(kind); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9584 | |
| 9585 | Isolate* isolate = map->GetIsolate(); |
| 9586 | Handle<Name> name = isolate->factory()->elements_transition_symbol(); |
| 9587 | ConnectTransition(map, new_map, name, SPECIAL_TRANSITION); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9588 | return new_map; |
| 9589 | } |
| 9590 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9591 | // Create a new free-floating map only if we are not allowed to store it. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9592 | Handle<Map> new_map = Copy(map, "CopyAsElementsKind"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9593 | new_map->set_elements_kind(kind); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9594 | return new_map; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9595 | } |
| 9596 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9597 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9598 | Handle<Map> Map::AsLanguageMode(Handle<Map> initial_map, |
| 9599 | LanguageMode language_mode, FunctionKind kind) { |
| 9600 | DCHECK_EQ(JS_FUNCTION_TYPE, initial_map->instance_type()); |
| 9601 | // Initial map for sloppy mode function is stored in the function |
| 9602 | // constructor. Initial maps for strict and strong modes are cached as |
| 9603 | // special transitions using |strict_function_transition_symbol| and |
| 9604 | // |strong_function_transition_symbol| respectively as a key. |
| 9605 | if (language_mode == SLOPPY) return initial_map; |
| 9606 | Isolate* isolate = initial_map->GetIsolate(); |
| 9607 | Factory* factory = isolate->factory(); |
| 9608 | Handle<Symbol> transition_symbol; |
| 9609 | |
| 9610 | int map_index = Context::FunctionMapIndex(language_mode, kind); |
| 9611 | Handle<Map> function_map( |
| 9612 | Map::cast(isolate->native_context()->get(map_index))); |
| 9613 | |
| 9614 | STATIC_ASSERT(LANGUAGE_END == 3); |
| 9615 | switch (language_mode) { |
| 9616 | case STRICT: |
| 9617 | transition_symbol = factory->strict_function_transition_symbol(); |
| 9618 | break; |
| 9619 | case STRONG: |
| 9620 | transition_symbol = factory->strong_function_transition_symbol(); |
| 9621 | break; |
| 9622 | default: |
| 9623 | UNREACHABLE(); |
| 9624 | break; |
| 9625 | } |
| 9626 | Map* maybe_transition = |
| 9627 | TransitionArray::SearchSpecial(*initial_map, *transition_symbol); |
| 9628 | if (maybe_transition != NULL) { |
| 9629 | return handle(maybe_transition, isolate); |
| 9630 | } |
| 9631 | initial_map->NotifyLeafMapLayoutChange(); |
| 9632 | |
| 9633 | // Create new map taking descriptors from the |function_map| and all |
| 9634 | // the other details from the |initial_map|. |
| 9635 | Handle<Map> map = |
| 9636 | Map::CopyInitialMap(function_map, initial_map->instance_size(), |
| 9637 | initial_map->GetInObjectProperties(), |
| 9638 | initial_map->unused_property_fields()); |
| 9639 | map->SetConstructor(initial_map->GetConstructor()); |
| 9640 | map->set_prototype(initial_map->prototype()); |
| 9641 | |
| 9642 | if (TransitionArray::CanHaveMoreTransitions(initial_map)) { |
| 9643 | Map::ConnectTransition(initial_map, map, transition_symbol, |
| 9644 | SPECIAL_TRANSITION); |
| 9645 | } |
| 9646 | return map; |
| 9647 | } |
| 9648 | |
| 9649 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9650 | Handle<Map> Map::CopyForObserved(Handle<Map> map) { |
| 9651 | DCHECK(!map->is_observed()); |
| 9652 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9653 | Isolate* isolate = map->GetIsolate(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9654 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9655 | bool insert_transition = |
| 9656 | TransitionArray::CanHaveMoreTransitions(map) && !map->is_prototype_map(); |
| 9657 | |
| 9658 | if (insert_transition) { |
| 9659 | Handle<Map> new_map = CopyForTransition(map, "CopyForObserved"); |
| 9660 | new_map->set_is_observed(); |
| 9661 | |
| 9662 | Handle<Name> name = isolate->factory()->observed_symbol(); |
| 9663 | ConnectTransition(map, new_map, name, SPECIAL_TRANSITION); |
| 9664 | return new_map; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9665 | } |
| 9666 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9667 | // Create a new free-floating map only if we are not allowed to store it. |
| 9668 | Handle<Map> new_map = Map::Copy(map, "CopyForObserved"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9669 | new_map->set_is_observed(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9670 | return new_map; |
| 9671 | } |
| 9672 | |
| 9673 | |
| 9674 | Handle<Map> Map::CopyForTransition(Handle<Map> map, const char* reason) { |
| 9675 | DCHECK(!map->is_prototype_map()); |
| 9676 | Handle<Map> new_map = CopyDropDescriptors(map); |
| 9677 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9678 | if (map->owns_descriptors()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9679 | // In case the map owned its own descriptors, share the descriptors and |
| 9680 | // transfer ownership to the new map. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9681 | // The properties did not change, so reuse descriptors. |
| 9682 | new_map->InitializeDescriptors(map->instance_descriptors(), |
| 9683 | map->GetLayoutDescriptor()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9684 | } else { |
| 9685 | // In case the map did not own its own descriptors, a split is forced by |
| 9686 | // copying the map; creating a new descriptor array cell. |
| 9687 | Handle<DescriptorArray> descriptors(map->instance_descriptors()); |
| 9688 | int number_of_own_descriptors = map->NumberOfOwnDescriptors(); |
| 9689 | Handle<DescriptorArray> new_descriptors = |
| 9690 | DescriptorArray::CopyUpTo(descriptors, number_of_own_descriptors); |
| 9691 | Handle<LayoutDescriptor> new_layout_descriptor(map->GetLayoutDescriptor(), |
| 9692 | map->GetIsolate()); |
| 9693 | new_map->InitializeDescriptors(*new_descriptors, *new_layout_descriptor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9694 | } |
| 9695 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9696 | #if TRACE_MAPS |
| 9697 | if (FLAG_trace_maps) { |
| 9698 | PrintF("[TraceMaps: CopyForTransition from= %p to= %p reason= %s ]\n", |
| 9699 | reinterpret_cast<void*>(*map), reinterpret_cast<void*>(*new_map), |
| 9700 | reason); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9701 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9702 | #endif |
| 9703 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9704 | return new_map; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9705 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9706 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9707 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9708 | Handle<Map> Map::Copy(Handle<Map> map, const char* reason) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9709 | Handle<DescriptorArray> descriptors(map->instance_descriptors()); |
| 9710 | int number_of_own_descriptors = map->NumberOfOwnDescriptors(); |
| 9711 | Handle<DescriptorArray> new_descriptors = |
| 9712 | DescriptorArray::CopyUpTo(descriptors, number_of_own_descriptors); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9713 | Handle<LayoutDescriptor> new_layout_descriptor(map->GetLayoutDescriptor(), |
| 9714 | map->GetIsolate()); |
| 9715 | return CopyReplaceDescriptors(map, new_descriptors, new_layout_descriptor, |
| 9716 | OMIT_TRANSITION, MaybeHandle<Name>(), reason, |
| 9717 | SPECIAL_TRANSITION); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9718 | } |
| 9719 | |
| 9720 | |
| 9721 | Handle<Map> Map::Create(Isolate* isolate, int inobject_properties) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9722 | Handle<Map> copy = |
| 9723 | Copy(handle(isolate->object_function()->initial_map()), "MapCreate"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9724 | |
| 9725 | // Check that we do not overflow the instance size when adding the extra |
| 9726 | // inobject properties. If the instance size overflows, we allocate as many |
| 9727 | // properties as we can as inobject properties. |
| 9728 | int max_extra_properties = |
| 9729 | (JSObject::kMaxInstanceSize - JSObject::kHeaderSize) >> kPointerSizeLog2; |
| 9730 | |
| 9731 | if (inobject_properties > max_extra_properties) { |
| 9732 | inobject_properties = max_extra_properties; |
| 9733 | } |
| 9734 | |
| 9735 | int new_instance_size = |
| 9736 | JSObject::kHeaderSize + kPointerSize * inobject_properties; |
| 9737 | |
| 9738 | // Adjust the map with the extra inobject properties. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9739 | copy->SetInObjectProperties(inobject_properties); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9740 | copy->set_unused_property_fields(inobject_properties); |
| 9741 | copy->set_instance_size(new_instance_size); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9742 | copy->set_visitor_id(Heap::GetStaticVisitorIdForMap(*copy)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9743 | return copy; |
| 9744 | } |
| 9745 | |
| 9746 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9747 | Handle<Map> Map::CopyForPreventExtensions(Handle<Map> map, |
| 9748 | PropertyAttributes attrs_to_add, |
| 9749 | Handle<Symbol> transition_marker, |
| 9750 | const char* reason) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9751 | int num_descriptors = map->NumberOfOwnDescriptors(); |
| 9752 | Isolate* isolate = map->GetIsolate(); |
| 9753 | Handle<DescriptorArray> new_desc = DescriptorArray::CopyUpToAddAttributes( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9754 | handle(map->instance_descriptors(), isolate), num_descriptors, |
| 9755 | attrs_to_add); |
| 9756 | Handle<LayoutDescriptor> new_layout_descriptor(map->GetLayoutDescriptor(), |
| 9757 | isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9758 | Handle<Map> new_map = CopyReplaceDescriptors( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9759 | map, new_desc, new_layout_descriptor, INSERT_TRANSITION, |
| 9760 | transition_marker, reason, SPECIAL_TRANSITION); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9761 | new_map->set_is_extensible(false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9762 | if (!IsFixedTypedArrayElementsKind(map->elements_kind())) { |
| 9763 | new_map->set_elements_kind(DICTIONARY_ELEMENTS); |
| 9764 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9765 | return new_map; |
| 9766 | } |
| 9767 | |
| 9768 | |
| 9769 | bool DescriptorArray::CanHoldValue(int descriptor, Object* value) { |
| 9770 | PropertyDetails details = GetDetails(descriptor); |
| 9771 | switch (details.type()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9772 | case DATA: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9773 | return value->FitsRepresentation(details.representation()) && |
| 9774 | GetFieldType(descriptor)->NowContains(value); |
| 9775 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9776 | case DATA_CONSTANT: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9777 | DCHECK(GetConstant(descriptor) != value || |
| 9778 | value->FitsRepresentation(details.representation())); |
| 9779 | return GetConstant(descriptor) == value; |
| 9780 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9781 | case ACCESSOR: |
| 9782 | case ACCESSOR_CONSTANT: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9783 | return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9784 | } |
| 9785 | |
| 9786 | UNREACHABLE(); |
| 9787 | return false; |
| 9788 | } |
| 9789 | |
| 9790 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9791 | // static |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9792 | Handle<Map> Map::PrepareForDataProperty(Handle<Map> map, int descriptor, |
| 9793 | Handle<Object> value) { |
| 9794 | // Dictionaries can store any property value. |
| 9795 | if (map->is_dictionary_map()) return map; |
| 9796 | |
| 9797 | // Migrate to the newest map before storing the property. |
| 9798 | map = Update(map); |
| 9799 | |
| 9800 | Handle<DescriptorArray> descriptors(map->instance_descriptors()); |
| 9801 | |
| 9802 | if (descriptors->CanHoldValue(descriptor, *value)) return map; |
| 9803 | |
| 9804 | Isolate* isolate = map->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9805 | PropertyAttributes attributes = |
| 9806 | descriptors->GetDetails(descriptor).attributes(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9807 | Representation representation = value->OptimalRepresentation(); |
| 9808 | Handle<HeapType> type = value->OptimalType(isolate, representation); |
| 9809 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9810 | return ReconfigureProperty(map, descriptor, kData, attributes, representation, |
| 9811 | type, FORCE_FIELD); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9812 | } |
| 9813 | |
| 9814 | |
| 9815 | Handle<Map> Map::TransitionToDataProperty(Handle<Map> map, Handle<Name> name, |
| 9816 | Handle<Object> value, |
| 9817 | PropertyAttributes attributes, |
| 9818 | StoreFromKeyed store_mode) { |
| 9819 | // Dictionary maps can always have additional data properties. |
| 9820 | if (map->is_dictionary_map()) return map; |
| 9821 | |
| 9822 | // Migrate to the newest map before storing the property. |
| 9823 | map = Update(map); |
| 9824 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9825 | Map* maybe_transition = |
| 9826 | TransitionArray::SearchTransition(*map, kData, *name, attributes); |
| 9827 | if (maybe_transition != NULL) { |
| 9828 | Handle<Map> transition(maybe_transition); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9829 | int descriptor = transition->LastAdded(); |
| 9830 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9831 | DCHECK_EQ(attributes, transition->instance_descriptors() |
| 9832 | ->GetDetails(descriptor) |
| 9833 | .attributes()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9834 | |
| 9835 | return Map::PrepareForDataProperty(transition, descriptor, value); |
| 9836 | } |
| 9837 | |
| 9838 | TransitionFlag flag = INSERT_TRANSITION; |
| 9839 | MaybeHandle<Map> maybe_map; |
| 9840 | if (value->IsJSFunction()) { |
| 9841 | maybe_map = Map::CopyWithConstant(map, name, value, attributes, flag); |
| 9842 | } else if (!map->TooManyFastProperties(store_mode)) { |
| 9843 | Isolate* isolate = name->GetIsolate(); |
| 9844 | Representation representation = value->OptimalRepresentation(); |
| 9845 | Handle<HeapType> type = value->OptimalType(isolate, representation); |
| 9846 | maybe_map = |
| 9847 | Map::CopyWithField(map, name, type, attributes, representation, flag); |
| 9848 | } |
| 9849 | |
| 9850 | Handle<Map> result; |
| 9851 | if (!maybe_map.ToHandle(&result)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9852 | #if TRACE_MAPS |
| 9853 | if (FLAG_trace_maps) { |
| 9854 | Vector<char> name_buffer = Vector<char>::New(100); |
| 9855 | name->NameShortPrint(name_buffer); |
| 9856 | Vector<char> buffer = Vector<char>::New(128); |
| 9857 | SNPrintF(buffer, "TooManyFastProperties %s", name_buffer.start()); |
| 9858 | return Map::Normalize(map, CLEAR_INOBJECT_PROPERTIES, buffer.start()); |
| 9859 | } |
| 9860 | #endif |
| 9861 | return Map::Normalize(map, CLEAR_INOBJECT_PROPERTIES, |
| 9862 | "TooManyFastProperties"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9863 | } |
| 9864 | |
| 9865 | return result; |
| 9866 | } |
| 9867 | |
| 9868 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9869 | Handle<Map> Map::ReconfigureExistingProperty(Handle<Map> map, int descriptor, |
| 9870 | PropertyKind kind, |
| 9871 | PropertyAttributes attributes) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9872 | // Dictionaries have to be reconfigured in-place. |
| 9873 | DCHECK(!map->is_dictionary_map()); |
| 9874 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9875 | if (!map->GetBackPointer()->IsMap()) { |
| 9876 | // There is no benefit from reconstructing transition tree for maps without |
| 9877 | // back pointers. |
| 9878 | return CopyGeneralizeAllRepresentations( |
| 9879 | map, descriptor, FORCE_FIELD, kind, attributes, |
| 9880 | "GenAll_AttributesMismatchProtoMap"); |
| 9881 | } |
| 9882 | |
| 9883 | if (FLAG_trace_generalization) { |
| 9884 | map->PrintReconfiguration(stdout, descriptor, kind, attributes); |
| 9885 | } |
| 9886 | |
| 9887 | Isolate* isolate = map->GetIsolate(); |
| 9888 | Handle<Map> new_map = ReconfigureProperty( |
| 9889 | map, descriptor, kind, attributes, Representation::None(), |
| 9890 | HeapType::None(isolate), FORCE_FIELD); |
| 9891 | return new_map; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9892 | } |
| 9893 | |
| 9894 | |
| 9895 | Handle<Map> Map::TransitionToAccessorProperty(Handle<Map> map, |
| 9896 | Handle<Name> name, |
| 9897 | AccessorComponent component, |
| 9898 | Handle<Object> accessor, |
| 9899 | PropertyAttributes attributes) { |
| 9900 | Isolate* isolate = name->GetIsolate(); |
| 9901 | |
| 9902 | // Dictionary maps can always have additional data properties. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9903 | if (map->is_dictionary_map()) return map; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9904 | |
| 9905 | // Migrate to the newest map before transitioning to the new property. |
| 9906 | map = Update(map); |
| 9907 | |
| 9908 | PropertyNormalizationMode mode = map->is_prototype_map() |
| 9909 | ? KEEP_INOBJECT_PROPERTIES |
| 9910 | : CLEAR_INOBJECT_PROPERTIES; |
| 9911 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9912 | Map* maybe_transition = |
| 9913 | TransitionArray::SearchTransition(*map, kAccessor, *name, attributes); |
| 9914 | if (maybe_transition != NULL) { |
| 9915 | Handle<Map> transition(maybe_transition, isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9916 | DescriptorArray* descriptors = transition->instance_descriptors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9917 | int descriptor = transition->LastAdded(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9918 | DCHECK(descriptors->GetKey(descriptor)->Equals(*name)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9919 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9920 | DCHECK_EQ(kAccessor, descriptors->GetDetails(descriptor).kind()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9921 | DCHECK_EQ(attributes, descriptors->GetDetails(descriptor).attributes()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9922 | |
| 9923 | Handle<Object> maybe_pair(descriptors->GetValue(descriptor), isolate); |
| 9924 | if (!maybe_pair->IsAccessorPair()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9925 | return Map::Normalize(map, mode, "TransitionToAccessorFromNonPair"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9926 | } |
| 9927 | |
| 9928 | Handle<AccessorPair> pair = Handle<AccessorPair>::cast(maybe_pair); |
| 9929 | if (pair->get(component) != *accessor) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9930 | return Map::Normalize(map, mode, "TransitionToDifferentAccessor"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9931 | } |
| 9932 | |
| 9933 | return transition; |
| 9934 | } |
| 9935 | |
| 9936 | Handle<AccessorPair> pair; |
| 9937 | DescriptorArray* old_descriptors = map->instance_descriptors(); |
| 9938 | int descriptor = old_descriptors->SearchWithCache(*name, *map); |
| 9939 | if (descriptor != DescriptorArray::kNotFound) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9940 | if (descriptor != map->LastAdded()) { |
| 9941 | return Map::Normalize(map, mode, "AccessorsOverwritingNonLast"); |
| 9942 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9943 | PropertyDetails old_details = old_descriptors->GetDetails(descriptor); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9944 | if (old_details.type() != ACCESSOR_CONSTANT) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9945 | return Map::Normalize(map, mode, "AccessorsOverwritingNonAccessors"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9946 | } |
| 9947 | |
| 9948 | if (old_details.attributes() != attributes) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9949 | return Map::Normalize(map, mode, "AccessorsWithAttributes"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9950 | } |
| 9951 | |
| 9952 | Handle<Object> maybe_pair(old_descriptors->GetValue(descriptor), isolate); |
| 9953 | if (!maybe_pair->IsAccessorPair()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9954 | return Map::Normalize(map, mode, "AccessorsOverwritingNonPair"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9955 | } |
| 9956 | |
| 9957 | Object* current = Handle<AccessorPair>::cast(maybe_pair)->get(component); |
| 9958 | if (current == *accessor) return map; |
| 9959 | |
| 9960 | if (!current->IsTheHole()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9961 | return Map::Normalize(map, mode, "AccessorsOverwritingAccessors"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9962 | } |
| 9963 | |
| 9964 | pair = AccessorPair::Copy(Handle<AccessorPair>::cast(maybe_pair)); |
| 9965 | } else if (map->NumberOfOwnDescriptors() >= kMaxNumberOfDescriptors || |
| 9966 | map->TooManyFastProperties(CERTAINLY_NOT_STORE_FROM_KEYED)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9967 | return Map::Normalize(map, CLEAR_INOBJECT_PROPERTIES, "TooManyAccessors"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9968 | } else { |
| 9969 | pair = isolate->factory()->NewAccessorPair(); |
| 9970 | } |
| 9971 | |
| 9972 | pair->set(component, *accessor); |
| 9973 | TransitionFlag flag = INSERT_TRANSITION; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9974 | AccessorConstantDescriptor new_desc(name, pair, attributes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9975 | return Map::CopyInsertDescriptor(map, &new_desc, flag); |
| 9976 | } |
| 9977 | |
| 9978 | |
| 9979 | Handle<Map> Map::CopyAddDescriptor(Handle<Map> map, |
| 9980 | Descriptor* descriptor, |
| 9981 | TransitionFlag flag) { |
| 9982 | Handle<DescriptorArray> descriptors(map->instance_descriptors()); |
| 9983 | |
| 9984 | // Ensure the key is unique. |
| 9985 | descriptor->KeyToUniqueName(); |
| 9986 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9987 | // Share descriptors only if map owns descriptors and it not an initial map. |
| 9988 | if (flag == INSERT_TRANSITION && map->owns_descriptors() && |
| 9989 | !map->GetBackPointer()->IsUndefined() && |
| 9990 | TransitionArray::CanHaveMoreTransitions(map)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9991 | return ShareDescriptor(map, descriptors, descriptor); |
| 9992 | } |
| 9993 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 9994 | int nof = map->NumberOfOwnDescriptors(); |
| 9995 | Handle<DescriptorArray> new_descriptors = |
| 9996 | DescriptorArray::CopyUpTo(descriptors, nof, 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9997 | new_descriptors->Append(descriptor); |
| 9998 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9999 | Handle<LayoutDescriptor> new_layout_descriptor = |
| 10000 | FLAG_unbox_double_fields |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10001 | ? LayoutDescriptor::New(map, new_descriptors, nof + 1) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10002 | : handle(LayoutDescriptor::FastPointerLayout(), map->GetIsolate()); |
| 10003 | |
| 10004 | return CopyReplaceDescriptors(map, new_descriptors, new_layout_descriptor, |
| 10005 | flag, descriptor->GetKey(), "CopyAddDescriptor", |
| 10006 | SIMPLE_PROPERTY_TRANSITION); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10007 | } |
| 10008 | |
| 10009 | |
| 10010 | Handle<Map> Map::CopyInsertDescriptor(Handle<Map> map, |
| 10011 | Descriptor* descriptor, |
| 10012 | TransitionFlag flag) { |
| 10013 | Handle<DescriptorArray> old_descriptors(map->instance_descriptors()); |
| 10014 | |
| 10015 | // Ensure the key is unique. |
| 10016 | descriptor->KeyToUniqueName(); |
| 10017 | |
| 10018 | // We replace the key if it is already present. |
| 10019 | int index = old_descriptors->SearchWithCache(*descriptor->GetKey(), *map); |
| 10020 | if (index != DescriptorArray::kNotFound) { |
| 10021 | return CopyReplaceDescriptor(map, old_descriptors, descriptor, index, flag); |
| 10022 | } |
| 10023 | return CopyAddDescriptor(map, descriptor, flag); |
| 10024 | } |
| 10025 | |
| 10026 | |
| 10027 | Handle<DescriptorArray> DescriptorArray::CopyUpTo( |
| 10028 | Handle<DescriptorArray> desc, |
| 10029 | int enumeration_index, |
| 10030 | int slack) { |
| 10031 | return DescriptorArray::CopyUpToAddAttributes( |
| 10032 | desc, enumeration_index, NONE, slack); |
| 10033 | } |
| 10034 | |
| 10035 | |
| 10036 | Handle<DescriptorArray> DescriptorArray::CopyUpToAddAttributes( |
| 10037 | Handle<DescriptorArray> desc, |
| 10038 | int enumeration_index, |
| 10039 | PropertyAttributes attributes, |
| 10040 | int slack) { |
| 10041 | if (enumeration_index + slack == 0) { |
| 10042 | return desc->GetIsolate()->factory()->empty_descriptor_array(); |
| 10043 | } |
| 10044 | |
| 10045 | int size = enumeration_index; |
| 10046 | |
| 10047 | Handle<DescriptorArray> descriptors = |
| 10048 | DescriptorArray::Allocate(desc->GetIsolate(), size, slack); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10049 | |
| 10050 | if (attributes != NONE) { |
| 10051 | for (int i = 0; i < size; ++i) { |
| 10052 | Object* value = desc->GetValue(i); |
| 10053 | Name* key = desc->GetKey(i); |
| 10054 | PropertyDetails details = desc->GetDetails(i); |
| 10055 | // Bulk attribute changes never affect private properties. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10056 | if (!key->IsPrivate()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10057 | int mask = DONT_DELETE | DONT_ENUM; |
| 10058 | // READ_ONLY is an invalid attribute for JS setters/getters. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10059 | if (details.type() != ACCESSOR_CONSTANT || !value->IsAccessorPair()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10060 | mask |= READ_ONLY; |
| 10061 | } |
| 10062 | details = details.CopyAddAttributes( |
| 10063 | static_cast<PropertyAttributes>(attributes & mask)); |
| 10064 | } |
| 10065 | Descriptor inner_desc( |
| 10066 | handle(key), handle(value, desc->GetIsolate()), details); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10067 | descriptors->SetDescriptor(i, &inner_desc); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10068 | } |
| 10069 | } else { |
| 10070 | for (int i = 0; i < size; ++i) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10071 | descriptors->CopyFrom(i, *desc); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10072 | } |
| 10073 | } |
| 10074 | |
| 10075 | if (desc->number_of_descriptors() != enumeration_index) descriptors->Sort(); |
| 10076 | |
| 10077 | return descriptors; |
| 10078 | } |
| 10079 | |
| 10080 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10081 | bool DescriptorArray::IsEqualUpTo(DescriptorArray* desc, int nof_descriptors) { |
| 10082 | for (int i = 0; i < nof_descriptors; i++) { |
| 10083 | if (GetKey(i) != desc->GetKey(i) || GetValue(i) != desc->GetValue(i)) { |
| 10084 | return false; |
| 10085 | } |
| 10086 | PropertyDetails details = GetDetails(i); |
| 10087 | PropertyDetails other_details = desc->GetDetails(i); |
| 10088 | if (details.type() != other_details.type() || |
| 10089 | !details.representation().Equals(other_details.representation())) { |
| 10090 | return false; |
| 10091 | } |
| 10092 | } |
| 10093 | return true; |
| 10094 | } |
| 10095 | |
| 10096 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10097 | Handle<Map> Map::CopyReplaceDescriptor(Handle<Map> map, |
| 10098 | Handle<DescriptorArray> descriptors, |
| 10099 | Descriptor* descriptor, |
| 10100 | int insertion_index, |
| 10101 | TransitionFlag flag) { |
| 10102 | // Ensure the key is unique. |
| 10103 | descriptor->KeyToUniqueName(); |
| 10104 | |
| 10105 | 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10112 | Handle<LayoutDescriptor> new_layout_descriptor = LayoutDescriptor::New( |
| 10113 | map, new_descriptors, new_descriptors->number_of_descriptors()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10114 | |
| 10115 | SimpleTransitionFlag simple_flag = |
| 10116 | (insertion_index == descriptors->number_of_descriptors() - 1) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10117 | ? SIMPLE_PROPERTY_TRANSITION |
| 10118 | : PROPERTY_TRANSITION; |
| 10119 | return CopyReplaceDescriptors(map, new_descriptors, new_layout_descriptor, |
| 10120 | flag, key, "CopyReplaceDescriptor", |
| 10121 | simple_flag); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10122 | } |
| 10123 | |
| 10124 | |
| 10125 | void 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 Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10134 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10135 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10136 | // Update the code cache. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10137 | Handle<CodeCache> code_cache(CodeCache::cast(map->code_cache()), isolate); |
| 10138 | CodeCache::Update(code_cache, name, code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10139 | } |
| 10140 | |
| 10141 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10142 | Object* Map::FindInCodeCache(Name* name, Code::Flags flags) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10143 | // 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 Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10147 | return GetHeap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10148 | } |
| 10149 | } |
| 10150 | |
| 10151 | |
| 10152 | int 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10161 | void Map::RemoveFromCodeCache(Name* name, Code* code, int index) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10162 | // No GC is supposed to happen between a call to IndexInCodeCache and |
| 10163 | // RemoveFromCodeCache so the code cache must be there. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10164 | DCHECK(!code_cache()->IsFixedArray()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10165 | CodeCache::cast(code_cache())->RemoveByIndex(name, code, index); |
| 10166 | } |
| 10167 | |
| 10168 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10169 | void CodeCache::Update( |
| 10170 | Handle<CodeCache> code_cache, Handle<Name> name, Handle<Code> code) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10171 | // 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10174 | if (code->type() == Code::NORMAL) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10175 | // Make sure that a hash table is allocated for the normal load code cache. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10176 | 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 Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10181 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10182 | UpdateNormalTypeCache(code_cache, name, code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10183 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10184 | DCHECK(code_cache->default_cache()->IsFixedArray()); |
| 10185 | UpdateDefaultCache(code_cache, name, code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10186 | } |
| 10187 | } |
| 10188 | |
| 10189 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10190 | void CodeCache::UpdateDefaultCache( |
| 10191 | Handle<CodeCache> code_cache, Handle<Name> name, Handle<Code> code) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10192 | // When updating the default code cache we disregard the type encoded in the |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10193 | // flags. This allows call constant stubs to overwrite call field |
| 10194 | // stubs, etc. |
| 10195 | Code::Flags flags = Code::RemoveTypeFromFlags(code->flags()); |
| 10196 | |
| 10197 | // First check whether we can update existing code cache without |
| 10198 | // extending it. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10199 | Handle<FixedArray> cache = handle(code_cache->default_cache()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10200 | int length = cache->length(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10201 | { |
| 10202 | DisallowHeapAllocation no_alloc; |
| 10203 | int deleted_index = -1; |
| 10204 | for (int i = 0; i < length; i += kCodeCacheEntrySize) { |
| 10205 | Object* key = cache->get(i); |
| 10206 | if (key->IsNull()) { |
| 10207 | if (deleted_index < 0) deleted_index = i; |
| 10208 | continue; |
| 10209 | } |
| 10210 | if (key->IsUndefined()) { |
| 10211 | if (deleted_index >= 0) i = deleted_index; |
| 10212 | cache->set(i + kCodeCacheEntryNameOffset, *name); |
| 10213 | cache->set(i + kCodeCacheEntryCodeOffset, *code); |
| 10214 | return; |
| 10215 | } |
| 10216 | if (name->Equals(Name::cast(key))) { |
| 10217 | Code::Flags found = |
| 10218 | Code::cast(cache->get(i + kCodeCacheEntryCodeOffset))->flags(); |
| 10219 | if (Code::RemoveTypeFromFlags(found) == flags) { |
| 10220 | cache->set(i + kCodeCacheEntryCodeOffset, *code); |
| 10221 | return; |
| 10222 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10223 | } |
| 10224 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10225 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10226 | // Reached the end of the code cache. If there were deleted |
| 10227 | // elements, reuse the space for the first of them. |
| 10228 | if (deleted_index >= 0) { |
| 10229 | cache->set(deleted_index + kCodeCacheEntryNameOffset, *name); |
| 10230 | cache->set(deleted_index + kCodeCacheEntryCodeOffset, *code); |
| 10231 | return; |
| 10232 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10233 | } |
| 10234 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10235 | // Extend the code cache with some new entries (at least one). Must be a |
| 10236 | // multiple of the entry size. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10237 | Isolate* isolate = cache->GetIsolate(); |
| 10238 | int new_length = length + (length >> 1) + kCodeCacheEntrySize; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10239 | new_length = new_length - new_length % kCodeCacheEntrySize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10240 | DCHECK((new_length % kCodeCacheEntrySize) == 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10241 | cache = isolate->factory()->CopyFixedArrayAndGrow(cache, new_length - length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10242 | |
| 10243 | // Add the (name, code) pair to the new cache. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10244 | cache->set(length + kCodeCacheEntryNameOffset, *name); |
| 10245 | cache->set(length + kCodeCacheEntryCodeOffset, *code); |
| 10246 | code_cache->set_default_cache(*cache); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10247 | } |
| 10248 | |
| 10249 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10250 | void CodeCache::UpdateNormalTypeCache( |
| 10251 | Handle<CodeCache> code_cache, Handle<Name> name, Handle<Code> code) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10252 | // Adding a new entry can cause a new cache to be allocated. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10253 | Handle<CodeCacheHashTable> cache( |
| 10254 | CodeCacheHashTable::cast(code_cache->normal_type_cache())); |
| 10255 | Handle<Object> new_cache = CodeCacheHashTable::Put(cache, name, code); |
| 10256 | code_cache->set_normal_type_cache(*new_cache); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10257 | } |
| 10258 | |
| 10259 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10260 | Object* CodeCache::Lookup(Name* name, Code::Flags flags) { |
| 10261 | Object* result = LookupDefaultCache(name, Code::RemoveTypeFromFlags(flags)); |
| 10262 | if (result->IsCode()) { |
| 10263 | if (Code::cast(result)->flags() == flags) return result; |
| 10264 | return GetHeap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10265 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10266 | return LookupNormalTypeCache(name, flags); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10267 | } |
| 10268 | |
| 10269 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10270 | Object* CodeCache::LookupDefaultCache(Name* name, Code::Flags flags) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10271 | FixedArray* cache = default_cache(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10272 | int length = cache->length(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10273 | for (int i = 0; i < length; i += kCodeCacheEntrySize) { |
| 10274 | Object* key = cache->get(i + kCodeCacheEntryNameOffset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10275 | // Skip deleted elements. |
| 10276 | if (key->IsNull()) continue; |
| 10277 | if (key->IsUndefined()) return key; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10278 | if (name->Equals(Name::cast(key))) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10279 | Code* code = Code::cast(cache->get(i + kCodeCacheEntryCodeOffset)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10280 | if (Code::RemoveTypeFromFlags(code->flags()) == flags) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10281 | return code; |
| 10282 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10283 | } |
| 10284 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10285 | return GetHeap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10286 | } |
| 10287 | |
| 10288 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10289 | Object* CodeCache::LookupNormalTypeCache(Name* name, Code::Flags flags) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10290 | if (!normal_type_cache()->IsUndefined()) { |
| 10291 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
| 10292 | return cache->Lookup(name, flags); |
| 10293 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10294 | return GetHeap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10295 | } |
| 10296 | } |
| 10297 | |
| 10298 | |
| 10299 | int CodeCache::GetIndex(Object* name, Code* code) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10300 | if (code->type() == Code::NORMAL) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10301 | if (normal_type_cache()->IsUndefined()) return -1; |
| 10302 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10303 | return cache->GetIndex(Name::cast(name), code->flags()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10304 | } |
| 10305 | |
| 10306 | FixedArray* array = default_cache(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10307 | int len = array->length(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10308 | for (int i = 0; i < len; i += kCodeCacheEntrySize) { |
| 10309 | if (array->get(i + kCodeCacheEntryCodeOffset) == code) return i + 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10310 | } |
| 10311 | return -1; |
| 10312 | } |
| 10313 | |
| 10314 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10315 | void CodeCache::RemoveByIndex(Object* name, Code* code, int index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10316 | if (code->type() == Code::NORMAL) { |
| 10317 | DCHECK(!normal_type_cache()->IsUndefined()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10318 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10319 | DCHECK(cache->GetIndex(Name::cast(name), code->flags()) == index); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10320 | cache->RemoveByIndex(index); |
| 10321 | } else { |
| 10322 | FixedArray* array = default_cache(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10323 | DCHECK(array->length() >= index && array->get(index)->IsCode()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10324 | // Use null instead of undefined for deleted elements to distinguish |
| 10325 | // deleted elements from unused elements. This distinction is used |
| 10326 | // when looking up in the cache and when updating the cache. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10327 | DCHECK_EQ(1, kCodeCacheEntryCodeOffset - kCodeCacheEntryNameOffset); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10328 | array->set_null(index - 1); // Name. |
| 10329 | array->set_null(index); // Code. |
| 10330 | } |
| 10331 | } |
| 10332 | |
| 10333 | |
| 10334 | // The key in the code cache hash table consists of the property name and the |
| 10335 | // code object. The actual match is on the name and the code flags. If a key |
| 10336 | // is created using the flags and not a code object it can only be used for |
| 10337 | // lookup not to create a new entry. |
| 10338 | class CodeCacheHashTableKey : public HashTableKey { |
| 10339 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10340 | CodeCacheHashTableKey(Handle<Name> name, Code::Flags flags) |
| 10341 | : name_(name), flags_(flags), code_() { } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10342 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10343 | CodeCacheHashTableKey(Handle<Name> name, Handle<Code> code) |
| 10344 | : name_(name), flags_(code->flags()), code_(code) { } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10345 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10346 | bool IsMatch(Object* other) override { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10347 | if (!other->IsFixedArray()) return false; |
| 10348 | FixedArray* pair = FixedArray::cast(other); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10349 | Name* name = Name::cast(pair->get(0)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10350 | Code::Flags flags = Code::cast(pair->get(1))->flags(); |
| 10351 | if (flags != flags_) { |
| 10352 | return false; |
| 10353 | } |
| 10354 | return name_->Equals(name); |
| 10355 | } |
| 10356 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10357 | static uint32_t NameFlagsHashHelper(Name* name, Code::Flags flags) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10358 | return name->Hash() ^ flags; |
| 10359 | } |
| 10360 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10361 | uint32_t Hash() override { return NameFlagsHashHelper(*name_, flags_); } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10362 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10363 | uint32_t HashForObject(Object* obj) override { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10364 | FixedArray* pair = FixedArray::cast(obj); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10365 | Name* name = Name::cast(pair->get(0)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10366 | Code* code = Code::cast(pair->get(1)); |
| 10367 | return NameFlagsHashHelper(name, code->flags()); |
| 10368 | } |
| 10369 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10370 | MUST_USE_RESULT Handle<Object> AsHandle(Isolate* isolate) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10371 | Handle<Code> code = code_.ToHandleChecked(); |
| 10372 | Handle<FixedArray> pair = isolate->factory()->NewFixedArray(2); |
| 10373 | pair->set(0, *name_); |
| 10374 | pair->set(1, *code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10375 | return pair; |
| 10376 | } |
| 10377 | |
| 10378 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10379 | Handle<Name> name_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10380 | Code::Flags flags_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10381 | // TODO(jkummerow): We should be able to get by without this. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10382 | MaybeHandle<Code> code_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10383 | }; |
| 10384 | |
| 10385 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10386 | Object* CodeCacheHashTable::Lookup(Name* name, Code::Flags flags) { |
| 10387 | DisallowHeapAllocation no_alloc; |
| 10388 | CodeCacheHashTableKey key(handle(name), flags); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10389 | int entry = FindEntry(&key); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10390 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10391 | return get(EntryToIndex(entry) + 1); |
| 10392 | } |
| 10393 | |
| 10394 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10395 | Handle<CodeCacheHashTable> CodeCacheHashTable::Put( |
| 10396 | Handle<CodeCacheHashTable> cache, Handle<Name> name, Handle<Code> code) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10397 | CodeCacheHashTableKey key(name, code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10398 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10399 | Handle<CodeCacheHashTable> new_cache = EnsureCapacity(cache, 1, &key); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10400 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10401 | int entry = new_cache->FindInsertionEntry(key.Hash()); |
| 10402 | Handle<Object> k = key.AsHandle(cache->GetIsolate()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10403 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10404 | new_cache->set(EntryToIndex(entry), *k); |
| 10405 | new_cache->set(EntryToIndex(entry) + 1, *code); |
| 10406 | new_cache->ElementAdded(); |
| 10407 | return new_cache; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10408 | } |
| 10409 | |
| 10410 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10411 | int CodeCacheHashTable::GetIndex(Name* name, Code::Flags flags) { |
| 10412 | DisallowHeapAllocation no_alloc; |
| 10413 | CodeCacheHashTableKey key(handle(name), flags); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10414 | int entry = FindEntry(&key); |
| 10415 | return (entry == kNotFound) ? -1 : entry; |
| 10416 | } |
| 10417 | |
| 10418 | |
| 10419 | void CodeCacheHashTable::RemoveByIndex(int index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10420 | DCHECK(index >= 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10421 | Heap* heap = GetHeap(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10422 | set(EntryToIndex(index), heap->the_hole_value()); |
| 10423 | set(EntryToIndex(index) + 1, heap->the_hole_value()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10424 | ElementRemoved(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10425 | } |
| 10426 | |
| 10427 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10428 | void PolymorphicCodeCache::Update(Handle<PolymorphicCodeCache> code_cache, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10429 | MapHandleList* maps, |
| 10430 | Code::Flags flags, |
| 10431 | Handle<Code> code) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10432 | Isolate* isolate = code_cache->GetIsolate(); |
| 10433 | if (code_cache->cache()->IsUndefined()) { |
| 10434 | Handle<PolymorphicCodeCacheHashTable> result = |
| 10435 | PolymorphicCodeCacheHashTable::New( |
| 10436 | isolate, |
| 10437 | PolymorphicCodeCacheHashTable::kInitialSize); |
| 10438 | code_cache->set_cache(*result); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10439 | } else { |
| 10440 | // This entry shouldn't be contained in the cache yet. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10441 | DCHECK(PolymorphicCodeCacheHashTable::cast(code_cache->cache()) |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10442 | ->Lookup(maps, flags)->IsUndefined()); |
| 10443 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10444 | Handle<PolymorphicCodeCacheHashTable> hash_table = |
| 10445 | handle(PolymorphicCodeCacheHashTable::cast(code_cache->cache())); |
| 10446 | Handle<PolymorphicCodeCacheHashTable> new_cache = |
| 10447 | PolymorphicCodeCacheHashTable::Put(hash_table, maps, flags, code); |
| 10448 | code_cache->set_cache(*new_cache); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10449 | } |
| 10450 | |
| 10451 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10452 | Handle<Object> PolymorphicCodeCache::Lookup(MapHandleList* maps, |
| 10453 | Code::Flags flags) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10454 | if (!cache()->IsUndefined()) { |
| 10455 | PolymorphicCodeCacheHashTable* hash_table = |
| 10456 | PolymorphicCodeCacheHashTable::cast(cache()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10457 | return Handle<Object>(hash_table->Lookup(maps, flags), GetIsolate()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10458 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10459 | return GetIsolate()->factory()->undefined_value(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10460 | } |
| 10461 | } |
| 10462 | |
| 10463 | |
| 10464 | // Despite their name, object of this class are not stored in the actual |
| 10465 | // hash table; instead they're temporarily used for lookups. It is therefore |
| 10466 | // safe to have a weak (non-owning) pointer to a MapList as a member field. |
| 10467 | class PolymorphicCodeCacheHashTableKey : public HashTableKey { |
| 10468 | public: |
| 10469 | // Callers must ensure that |maps| outlives the newly constructed object. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10470 | PolymorphicCodeCacheHashTableKey(MapHandleList* maps, int code_flags) |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10471 | : maps_(maps), |
| 10472 | code_flags_(code_flags) {} |
| 10473 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10474 | bool IsMatch(Object* other) override { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10475 | MapHandleList other_maps(kDefaultListAllocationSize); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10476 | int other_flags; |
| 10477 | FromObject(other, &other_flags, &other_maps); |
| 10478 | if (code_flags_ != other_flags) return false; |
| 10479 | if (maps_->length() != other_maps.length()) return false; |
| 10480 | // Compare just the hashes first because it's faster. |
| 10481 | int this_hash = MapsHashHelper(maps_, code_flags_); |
| 10482 | int other_hash = MapsHashHelper(&other_maps, other_flags); |
| 10483 | if (this_hash != other_hash) return false; |
| 10484 | |
| 10485 | // Full comparison: for each map in maps_, look for an equivalent map in |
| 10486 | // other_maps. This implementation is slow, but probably good enough for |
| 10487 | // now because the lists are short (<= 4 elements currently). |
| 10488 | for (int i = 0; i < maps_->length(); ++i) { |
| 10489 | bool match_found = false; |
| 10490 | for (int j = 0; j < other_maps.length(); ++j) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10491 | if (*(maps_->at(i)) == *(other_maps.at(j))) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10492 | match_found = true; |
| 10493 | break; |
| 10494 | } |
| 10495 | } |
| 10496 | if (!match_found) return false; |
| 10497 | } |
| 10498 | return true; |
| 10499 | } |
| 10500 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10501 | static uint32_t MapsHashHelper(MapHandleList* maps, int code_flags) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10502 | uint32_t hash = code_flags; |
| 10503 | for (int i = 0; i < maps->length(); ++i) { |
| 10504 | hash ^= maps->at(i)->Hash(); |
| 10505 | } |
| 10506 | return hash; |
| 10507 | } |
| 10508 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10509 | uint32_t Hash() override { return MapsHashHelper(maps_, code_flags_); } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10510 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10511 | uint32_t HashForObject(Object* obj) override { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10512 | MapHandleList other_maps(kDefaultListAllocationSize); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10513 | int other_flags; |
| 10514 | FromObject(obj, &other_flags, &other_maps); |
| 10515 | return MapsHashHelper(&other_maps, other_flags); |
| 10516 | } |
| 10517 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10518 | MUST_USE_RESULT Handle<Object> AsHandle(Isolate* isolate) override { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10519 | // The maps in |maps_| must be copied to a newly allocated FixedArray, |
| 10520 | // both because the referenced MapList is short-lived, and because C++ |
| 10521 | // objects can't be stored in the heap anyway. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10522 | Handle<FixedArray> list = |
| 10523 | isolate->factory()->NewUninitializedFixedArray(maps_->length() + 1); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10524 | list->set(0, Smi::FromInt(code_flags_)); |
| 10525 | for (int i = 0; i < maps_->length(); ++i) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10526 | list->set(i + 1, *maps_->at(i)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10527 | } |
| 10528 | return list; |
| 10529 | } |
| 10530 | |
| 10531 | private: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10532 | static MapHandleList* FromObject(Object* obj, |
| 10533 | int* code_flags, |
| 10534 | MapHandleList* maps) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10535 | FixedArray* list = FixedArray::cast(obj); |
| 10536 | maps->Rewind(0); |
| 10537 | *code_flags = Smi::cast(list->get(0))->value(); |
| 10538 | for (int i = 1; i < list->length(); ++i) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10539 | maps->Add(Handle<Map>(Map::cast(list->get(i)))); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10540 | } |
| 10541 | return maps; |
| 10542 | } |
| 10543 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10544 | MapHandleList* maps_; // weak. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10545 | int code_flags_; |
| 10546 | static const int kDefaultListAllocationSize = kMaxKeyedPolymorphism + 1; |
| 10547 | }; |
| 10548 | |
| 10549 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10550 | Object* PolymorphicCodeCacheHashTable::Lookup(MapHandleList* maps, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10551 | int code_kind) { |
| 10552 | DisallowHeapAllocation no_alloc; |
| 10553 | PolymorphicCodeCacheHashTableKey key(maps, code_kind); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10554 | int entry = FindEntry(&key); |
| 10555 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
| 10556 | return get(EntryToIndex(entry) + 1); |
| 10557 | } |
| 10558 | |
| 10559 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10560 | Handle<PolymorphicCodeCacheHashTable> PolymorphicCodeCacheHashTable::Put( |
| 10561 | Handle<PolymorphicCodeCacheHashTable> hash_table, |
| 10562 | MapHandleList* maps, |
| 10563 | int code_kind, |
| 10564 | Handle<Code> code) { |
| 10565 | PolymorphicCodeCacheHashTableKey key(maps, code_kind); |
| 10566 | Handle<PolymorphicCodeCacheHashTable> cache = |
| 10567 | EnsureCapacity(hash_table, 1, &key); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10568 | int entry = cache->FindInsertionEntry(key.Hash()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10569 | |
| 10570 | Handle<Object> obj = key.AsHandle(hash_table->GetIsolate()); |
| 10571 | cache->set(EntryToIndex(entry), *obj); |
| 10572 | cache->set(EntryToIndex(entry) + 1, *code); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10573 | cache->ElementAdded(); |
| 10574 | return cache; |
| 10575 | } |
| 10576 | |
| 10577 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10578 | void FixedArray::Shrink(int new_length) { |
| 10579 | DCHECK(0 <= new_length && new_length <= length()); |
| 10580 | if (new_length < length()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10581 | GetHeap()->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10582 | this, length() - new_length); |
| 10583 | } |
| 10584 | } |
| 10585 | |
| 10586 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10587 | void FixedArray::CopyTo(int pos, FixedArray* dest, int dest_pos, int len) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10588 | DisallowHeapAllocation no_gc; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 10589 | WriteBarrierMode mode = dest->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10590 | for (int index = 0; index < len; index++) { |
| 10591 | dest->set(dest_pos+index, get(pos+index), mode); |
| 10592 | } |
| 10593 | } |
| 10594 | |
| 10595 | |
| 10596 | #ifdef DEBUG |
| 10597 | bool FixedArray::IsEqualTo(FixedArray* other) { |
| 10598 | if (length() != other->length()) return false; |
| 10599 | for (int i = 0 ; i < length(); ++i) { |
| 10600 | if (get(i) != other->get(i)) return false; |
| 10601 | } |
| 10602 | return true; |
| 10603 | } |
| 10604 | #endif |
| 10605 | |
| 10606 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10607 | // static |
| 10608 | void WeakFixedArray::Set(Handle<WeakFixedArray> array, int index, |
| 10609 | Handle<HeapObject> value) { |
| 10610 | DCHECK(array->IsEmptySlot(index)); // Don't overwrite anything. |
| 10611 | Handle<WeakCell> cell = |
| 10612 | value->IsMap() ? Map::WeakCellForMap(Handle<Map>::cast(value)) |
| 10613 | : array->GetIsolate()->factory()->NewWeakCell(value); |
| 10614 | Handle<FixedArray>::cast(array)->set(index + kFirstIndex, *cell); |
| 10615 | if (FLAG_trace_weak_arrays) { |
| 10616 | PrintF("[WeakFixedArray: storing at index %d ]\n", index); |
| 10617 | } |
| 10618 | array->set_last_used_index(index); |
| 10619 | } |
| 10620 | |
| 10621 | |
| 10622 | // static |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10623 | Handle<WeakFixedArray> WeakFixedArray::Add(Handle<Object> maybe_array, |
| 10624 | Handle<HeapObject> value, |
| 10625 | int* assigned_index) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10626 | Handle<WeakFixedArray> array = |
| 10627 | (maybe_array.is_null() || !maybe_array->IsWeakFixedArray()) |
| 10628 | ? Allocate(value->GetIsolate(), 1, Handle<WeakFixedArray>::null()) |
| 10629 | : Handle<WeakFixedArray>::cast(maybe_array); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10630 | // Try to store the new entry if there's room. Optimize for consecutive |
| 10631 | // accesses. |
| 10632 | int first_index = array->last_used_index(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10633 | int length = array->Length(); |
| 10634 | if (length > 0) { |
| 10635 | for (int i = first_index;;) { |
| 10636 | if (array->IsEmptySlot((i))) { |
| 10637 | WeakFixedArray::Set(array, i, value); |
| 10638 | if (assigned_index != NULL) *assigned_index = i; |
| 10639 | return array; |
| 10640 | } |
| 10641 | if (FLAG_trace_weak_arrays) { |
| 10642 | PrintF("[WeakFixedArray: searching for free slot]\n"); |
| 10643 | } |
| 10644 | i = (i + 1) % length; |
| 10645 | if (i == first_index) break; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10646 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10647 | } |
| 10648 | |
| 10649 | // No usable slot found, grow the array. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10650 | int new_length = length == 0 ? 1 : length + (length >> 1) + 4; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10651 | Handle<WeakFixedArray> new_array = |
| 10652 | Allocate(array->GetIsolate(), new_length, array); |
| 10653 | if (FLAG_trace_weak_arrays) { |
| 10654 | PrintF("[WeakFixedArray: growing to size %d ]\n", new_length); |
| 10655 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10656 | WeakFixedArray::Set(new_array, length, value); |
| 10657 | if (assigned_index != NULL) *assigned_index = length; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10658 | return new_array; |
| 10659 | } |
| 10660 | |
| 10661 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10662 | template <class CompactionCallback> |
| 10663 | void WeakFixedArray::Compact() { |
| 10664 | FixedArray* array = FixedArray::cast(this); |
| 10665 | int new_length = kFirstIndex; |
| 10666 | for (int i = kFirstIndex; i < array->length(); i++) { |
| 10667 | Object* element = array->get(i); |
| 10668 | if (element->IsSmi()) continue; |
| 10669 | if (WeakCell::cast(element)->cleared()) continue; |
| 10670 | Object* value = WeakCell::cast(element)->value(); |
| 10671 | CompactionCallback::Callback(value, i - kFirstIndex, |
| 10672 | new_length - kFirstIndex); |
| 10673 | array->set(new_length++, element); |
| 10674 | } |
| 10675 | array->Shrink(new_length); |
| 10676 | set_last_used_index(0); |
| 10677 | } |
| 10678 | |
| 10679 | |
| 10680 | void WeakFixedArray::Iterator::Reset(Object* maybe_array) { |
| 10681 | if (maybe_array->IsWeakFixedArray()) { |
| 10682 | list_ = WeakFixedArray::cast(maybe_array); |
| 10683 | index_ = 0; |
| 10684 | #ifdef DEBUG |
| 10685 | last_used_index_ = list_->last_used_index(); |
| 10686 | #endif // DEBUG |
| 10687 | } |
| 10688 | } |
| 10689 | |
| 10690 | |
| 10691 | void JSObject::PrototypeRegistryCompactionCallback::Callback(Object* value, |
| 10692 | int old_index, |
| 10693 | int new_index) { |
| 10694 | DCHECK(value->IsMap() && Map::cast(value)->is_prototype_map()); |
| 10695 | Map* map = Map::cast(value); |
| 10696 | DCHECK(map->prototype_info()->IsPrototypeInfo()); |
| 10697 | PrototypeInfo* proto_info = PrototypeInfo::cast(map->prototype_info()); |
| 10698 | DCHECK_EQ(old_index, proto_info->registry_slot()); |
| 10699 | proto_info->set_registry_slot(new_index); |
| 10700 | } |
| 10701 | |
| 10702 | |
| 10703 | template void WeakFixedArray::Compact<WeakFixedArray::NullCallback>(); |
| 10704 | template void |
| 10705 | WeakFixedArray::Compact<JSObject::PrototypeRegistryCompactionCallback>(); |
| 10706 | |
| 10707 | |
| 10708 | bool WeakFixedArray::Remove(Handle<HeapObject> value) { |
| 10709 | if (Length() == 0) return false; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10710 | // Optimize for the most recently added element to be removed again. |
| 10711 | int first_index = last_used_index(); |
| 10712 | for (int i = first_index;;) { |
| 10713 | if (Get(i) == *value) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10714 | Clear(i); |
| 10715 | // Users of WeakFixedArray should make sure that there are no duplicates. |
| 10716 | return true; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10717 | } |
| 10718 | i = (i + 1) % Length(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10719 | if (i == first_index) return false; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10720 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10721 | UNREACHABLE(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10722 | } |
| 10723 | |
| 10724 | |
| 10725 | // static |
| 10726 | Handle<WeakFixedArray> WeakFixedArray::Allocate( |
| 10727 | Isolate* isolate, int size, Handle<WeakFixedArray> initialize_from) { |
| 10728 | DCHECK(0 <= size); |
| 10729 | Handle<FixedArray> result = |
| 10730 | isolate->factory()->NewUninitializedFixedArray(size + kFirstIndex); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10731 | int index = 0; |
| 10732 | if (!initialize_from.is_null()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10733 | DCHECK(initialize_from->Length() <= size); |
| 10734 | Handle<FixedArray> raw_source = Handle<FixedArray>::cast(initialize_from); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10735 | // Copy the entries without compacting, since the PrototypeInfo relies on |
| 10736 | // the index of the entries not to change. |
| 10737 | while (index < raw_source->length()) { |
| 10738 | result->set(index, raw_source->get(index)); |
| 10739 | index++; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10740 | } |
| 10741 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10742 | while (index < result->length()) { |
| 10743 | result->set(index, Smi::FromInt(0)); |
| 10744 | index++; |
| 10745 | } |
| 10746 | return Handle<WeakFixedArray>::cast(result); |
| 10747 | } |
| 10748 | |
| 10749 | |
| 10750 | Handle<ArrayList> ArrayList::Add(Handle<ArrayList> array, Handle<Object> obj, |
| 10751 | AddMode mode) { |
| 10752 | int length = array->Length(); |
| 10753 | array = EnsureSpace(array, length + 1); |
| 10754 | if (mode == kReloadLengthAfterAllocation) { |
| 10755 | DCHECK(array->Length() <= length); |
| 10756 | length = array->Length(); |
| 10757 | } |
| 10758 | array->Set(length, *obj); |
| 10759 | array->SetLength(length + 1); |
| 10760 | return array; |
| 10761 | } |
| 10762 | |
| 10763 | |
| 10764 | Handle<ArrayList> ArrayList::Add(Handle<ArrayList> array, Handle<Object> obj1, |
| 10765 | Handle<Object> obj2, AddMode mode) { |
| 10766 | int length = array->Length(); |
| 10767 | array = EnsureSpace(array, length + 2); |
| 10768 | if (mode == kReloadLengthAfterAllocation) { |
| 10769 | length = array->Length(); |
| 10770 | } |
| 10771 | array->Set(length, *obj1); |
| 10772 | array->Set(length + 1, *obj2); |
| 10773 | array->SetLength(length + 2); |
| 10774 | return array; |
| 10775 | } |
| 10776 | |
| 10777 | |
| 10778 | bool ArrayList::IsFull() { |
| 10779 | int capacity = length(); |
| 10780 | return kFirstIndex + Length() == capacity; |
| 10781 | } |
| 10782 | |
| 10783 | |
| 10784 | Handle<ArrayList> ArrayList::EnsureSpace(Handle<ArrayList> array, int length) { |
| 10785 | int capacity = array->length(); |
| 10786 | bool empty = (capacity == 0); |
| 10787 | if (capacity < kFirstIndex + length) { |
| 10788 | Isolate* isolate = array->GetIsolate(); |
| 10789 | int new_capacity = kFirstIndex + length; |
| 10790 | new_capacity = new_capacity + Max(new_capacity / 2, 2); |
| 10791 | int grow_by = new_capacity - capacity; |
| 10792 | array = Handle<ArrayList>::cast( |
| 10793 | isolate->factory()->CopyFixedArrayAndGrow(array, grow_by)); |
| 10794 | if (empty) array->SetLength(0); |
| 10795 | } |
| 10796 | return array; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10797 | } |
| 10798 | |
| 10799 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10800 | Handle<DescriptorArray> DescriptorArray::Allocate(Isolate* isolate, |
| 10801 | int number_of_descriptors, |
| 10802 | int slack) { |
| 10803 | DCHECK(0 <= number_of_descriptors); |
| 10804 | Factory* factory = isolate->factory(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10805 | // Do not use DescriptorArray::cast on incomplete object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10806 | int size = number_of_descriptors + slack; |
| 10807 | if (size == 0) return factory->empty_descriptor_array(); |
| 10808 | // Allocate the array of keys. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10809 | Handle<FixedArray> result = factory->NewFixedArray(LengthFor(size), TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10810 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10811 | result->set(kDescriptorLengthIndex, Smi::FromInt(number_of_descriptors)); |
| 10812 | result->set(kEnumCacheIndex, Smi::FromInt(0)); |
| 10813 | return Handle<DescriptorArray>::cast(result); |
| 10814 | } |
| 10815 | |
| 10816 | |
| 10817 | void DescriptorArray::ClearEnumCache() { |
| 10818 | set(kEnumCacheIndex, Smi::FromInt(0)); |
| 10819 | } |
| 10820 | |
| 10821 | |
| 10822 | void DescriptorArray::Replace(int index, Descriptor* descriptor) { |
| 10823 | descriptor->SetSortedKeyIndex(GetSortedKeyIndex(index)); |
| 10824 | Set(index, descriptor); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10825 | } |
| 10826 | |
| 10827 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10828 | // static |
| 10829 | void DescriptorArray::SetEnumCache(Handle<DescriptorArray> descriptors, |
| 10830 | Isolate* isolate, |
| 10831 | Handle<FixedArray> new_cache, |
| 10832 | Handle<FixedArray> new_index_cache) { |
| 10833 | DCHECK(!descriptors->IsEmpty()); |
| 10834 | FixedArray* bridge_storage; |
| 10835 | bool needs_new_enum_cache = !descriptors->HasEnumCache(); |
| 10836 | if (needs_new_enum_cache) { |
| 10837 | bridge_storage = *isolate->factory()->NewFixedArray( |
| 10838 | DescriptorArray::kEnumCacheBridgeLength); |
| 10839 | } else { |
| 10840 | bridge_storage = FixedArray::cast(descriptors->get(kEnumCacheIndex)); |
| 10841 | } |
| 10842 | bridge_storage->set(kEnumCacheBridgeCacheIndex, *new_cache); |
| 10843 | bridge_storage->set(kEnumCacheBridgeIndicesCacheIndex, |
| 10844 | new_index_cache.is_null() ? Object::cast(Smi::FromInt(0)) |
| 10845 | : *new_index_cache); |
| 10846 | if (needs_new_enum_cache) { |
| 10847 | descriptors->set(kEnumCacheIndex, bridge_storage); |
| 10848 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10849 | } |
| 10850 | |
| 10851 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10852 | void DescriptorArray::CopyFrom(int index, DescriptorArray* src) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10853 | Object* value = src->GetValue(index); |
| 10854 | PropertyDetails details = src->GetDetails(index); |
| 10855 | Descriptor desc(handle(src->GetKey(index)), |
| 10856 | handle(value, src->GetIsolate()), |
| 10857 | details); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10858 | SetDescriptor(index, &desc); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10859 | } |
| 10860 | |
| 10861 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10862 | void DescriptorArray::Sort() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10863 | // In-place heap sort. |
| 10864 | int len = number_of_descriptors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10865 | // Reset sorting since the descriptor array might contain invalid pointers. |
| 10866 | for (int i = 0; i < len; ++i) SetSortedKey(i, i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10867 | // Bottom-up max-heap construction. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10868 | // Index of the last node with children |
| 10869 | const int max_parent_index = (len / 2) - 1; |
| 10870 | for (int i = max_parent_index; i >= 0; --i) { |
| 10871 | int parent_index = i; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10872 | const uint32_t parent_hash = GetSortedKey(i)->Hash(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10873 | while (parent_index <= max_parent_index) { |
| 10874 | int child_index = 2 * parent_index + 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10875 | uint32_t child_hash = GetSortedKey(child_index)->Hash(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10876 | if (child_index + 1 < len) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10877 | uint32_t right_child_hash = GetSortedKey(child_index + 1)->Hash(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10878 | if (right_child_hash > child_hash) { |
| 10879 | child_index++; |
| 10880 | child_hash = right_child_hash; |
| 10881 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10882 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10883 | if (child_hash <= parent_hash) break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10884 | SwapSortedKeys(parent_index, child_index); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10885 | // Now element at child_index could be < its children. |
| 10886 | parent_index = child_index; // parent_hash remains correct. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10887 | } |
| 10888 | } |
| 10889 | |
| 10890 | // Extract elements and create sorted array. |
| 10891 | for (int i = len - 1; i > 0; --i) { |
| 10892 | // Put max element at the back of the array. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10893 | SwapSortedKeys(0, i); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10894 | // Shift down the new top element. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10895 | int parent_index = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10896 | const uint32_t parent_hash = GetSortedKey(parent_index)->Hash(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10897 | const int max_parent_index = (i / 2) - 1; |
| 10898 | while (parent_index <= max_parent_index) { |
| 10899 | int child_index = parent_index * 2 + 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10900 | uint32_t child_hash = GetSortedKey(child_index)->Hash(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10901 | if (child_index + 1 < i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10902 | uint32_t right_child_hash = GetSortedKey(child_index + 1)->Hash(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10903 | if (right_child_hash > child_hash) { |
| 10904 | child_index++; |
| 10905 | child_hash = right_child_hash; |
| 10906 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10907 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10908 | if (child_hash <= parent_hash) break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10909 | SwapSortedKeys(parent_index, child_index); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10910 | parent_index = child_index; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10911 | } |
| 10912 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10913 | DCHECK(IsSortedNoDuplicates()); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 10914 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10915 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 10916 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10917 | Handle<AccessorPair> AccessorPair::Copy(Handle<AccessorPair> pair) { |
| 10918 | Handle<AccessorPair> copy = pair->GetIsolate()->factory()->NewAccessorPair(); |
| 10919 | copy->set_getter(pair->getter()); |
| 10920 | copy->set_setter(pair->setter()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10921 | return copy; |
| 10922 | } |
| 10923 | |
| 10924 | |
| 10925 | Object* AccessorPair::GetComponent(AccessorComponent component) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10926 | Object* accessor = get(component); |
| 10927 | return accessor->IsTheHole() ? GetHeap()->undefined_value() : accessor; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10928 | } |
| 10929 | |
| 10930 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10931 | Handle<DeoptimizationInputData> DeoptimizationInputData::New( |
| 10932 | Isolate* isolate, int deopt_entry_count, PretenureFlag pretenure) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10933 | return Handle<DeoptimizationInputData>::cast( |
| 10934 | isolate->factory()->NewFixedArray(LengthFor(deopt_entry_count), |
| 10935 | pretenure)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 10936 | } |
| 10937 | |
| 10938 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10939 | Handle<DeoptimizationOutputData> DeoptimizationOutputData::New( |
| 10940 | Isolate* isolate, |
| 10941 | int number_of_deopt_points, |
| 10942 | PretenureFlag pretenure) { |
| 10943 | Handle<FixedArray> result; |
| 10944 | if (number_of_deopt_points == 0) { |
| 10945 | result = isolate->factory()->empty_fixed_array(); |
| 10946 | } else { |
| 10947 | result = isolate->factory()->NewFixedArray( |
| 10948 | LengthOfFixedArray(number_of_deopt_points), pretenure); |
| 10949 | } |
| 10950 | return Handle<DeoptimizationOutputData>::cast(result); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 10951 | } |
| 10952 | |
| 10953 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 10954 | // static |
| 10955 | Handle<LiteralsArray> LiteralsArray::New(Isolate* isolate, |
| 10956 | Handle<TypeFeedbackVector> vector, |
| 10957 | int number_of_literals, |
| 10958 | PretenureFlag pretenure) { |
| 10959 | Handle<FixedArray> literals = isolate->factory()->NewFixedArray( |
| 10960 | number_of_literals + kFirstLiteralIndex, pretenure); |
| 10961 | Handle<LiteralsArray> casted_literals = Handle<LiteralsArray>::cast(literals); |
| 10962 | casted_literals->set_feedback_vector(*vector); |
| 10963 | return casted_literals; |
| 10964 | } |
| 10965 | |
| 10966 | |
| 10967 | int HandlerTable::LookupRange(int pc_offset, int* stack_depth_out, |
| 10968 | CatchPrediction* prediction_out) { |
| 10969 | int innermost_handler = -1, innermost_start = -1; |
| 10970 | for (int i = 0; i < length(); i += kRangeEntrySize) { |
| 10971 | int start_offset = Smi::cast(get(i + kRangeStartIndex))->value(); |
| 10972 | int end_offset = Smi::cast(get(i + kRangeEndIndex))->value(); |
| 10973 | int handler_field = Smi::cast(get(i + kRangeHandlerIndex))->value(); |
| 10974 | int handler_offset = HandlerOffsetField::decode(handler_field); |
| 10975 | CatchPrediction prediction = HandlerPredictionField::decode(handler_field); |
| 10976 | int stack_depth = Smi::cast(get(i + kRangeDepthIndex))->value(); |
| 10977 | if (pc_offset > start_offset && pc_offset <= end_offset) { |
| 10978 | DCHECK_NE(start_offset, innermost_start); |
| 10979 | if (start_offset < innermost_start) continue; |
| 10980 | innermost_handler = handler_offset; |
| 10981 | innermost_start = start_offset; |
| 10982 | *stack_depth_out = stack_depth; |
| 10983 | if (prediction_out) *prediction_out = prediction; |
| 10984 | } |
| 10985 | } |
| 10986 | return innermost_handler; |
| 10987 | } |
| 10988 | |
| 10989 | |
| 10990 | // TODO(turbofan): Make sure table is sorted and use binary search. |
| 10991 | int HandlerTable::LookupReturn(int pc_offset, CatchPrediction* prediction_out) { |
| 10992 | for (int i = 0; i < length(); i += kReturnEntrySize) { |
| 10993 | int return_offset = Smi::cast(get(i + kReturnOffsetIndex))->value(); |
| 10994 | int handler_field = Smi::cast(get(i + kReturnHandlerIndex))->value(); |
| 10995 | if (pc_offset == return_offset) { |
| 10996 | if (prediction_out) { |
| 10997 | *prediction_out = HandlerPredictionField::decode(handler_field); |
| 10998 | } |
| 10999 | return HandlerOffsetField::decode(handler_field); |
| 11000 | } |
| 11001 | } |
| 11002 | return -1; |
| 11003 | } |
| 11004 | |
| 11005 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11006 | #ifdef DEBUG |
| 11007 | bool DescriptorArray::IsEqualTo(DescriptorArray* other) { |
| 11008 | if (IsEmpty()) return other->IsEmpty(); |
| 11009 | if (other->IsEmpty()) return false; |
| 11010 | if (length() != other->length()) return false; |
| 11011 | for (int i = 0; i < length(); ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11012 | if (get(i) != other->get(i)) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11013 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11014 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11015 | } |
| 11016 | #endif |
| 11017 | |
| 11018 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11019 | bool String::LooksValid() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11020 | if (!GetIsolate()->heap()->Contains(this)) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11021 | return true; |
| 11022 | } |
| 11023 | |
| 11024 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 11025 | // static |
| 11026 | MaybeHandle<String> Name::ToFunctionName(Handle<Name> name) { |
| 11027 | if (name->IsString()) return Handle<String>::cast(name); |
| 11028 | // ES6 section 9.2.11 SetFunctionName, step 4. |
| 11029 | Isolate* const isolate = name->GetIsolate(); |
| 11030 | Handle<Object> description(Handle<Symbol>::cast(name)->name(), isolate); |
| 11031 | if (description->IsUndefined()) return isolate->factory()->empty_string(); |
| 11032 | IncrementalStringBuilder builder(isolate); |
| 11033 | builder.AppendCharacter('['); |
| 11034 | builder.AppendString(Handle<String>::cast(description)); |
| 11035 | builder.AppendCharacter(']'); |
| 11036 | return builder.Finish(); |
| 11037 | } |
| 11038 | |
| 11039 | |
| 11040 | namespace { |
| 11041 | |
| 11042 | bool AreDigits(const uint8_t* s, int from, int to) { |
| 11043 | for (int i = from; i < to; i++) { |
| 11044 | if (s[i] < '0' || s[i] > '9') return false; |
| 11045 | } |
| 11046 | |
| 11047 | return true; |
| 11048 | } |
| 11049 | |
| 11050 | |
| 11051 | int ParseDecimalInteger(const uint8_t* s, int from, int to) { |
| 11052 | DCHECK(to - from < 10); // Overflow is not possible. |
| 11053 | DCHECK(from < to); |
| 11054 | int d = s[from] - '0'; |
| 11055 | |
| 11056 | for (int i = from + 1; i < to; i++) { |
| 11057 | d = 10 * d + (s[i] - '0'); |
| 11058 | } |
| 11059 | |
| 11060 | return d; |
| 11061 | } |
| 11062 | |
| 11063 | } // namespace |
| 11064 | |
| 11065 | |
| 11066 | // static |
| 11067 | Handle<Object> String::ToNumber(Handle<String> subject) { |
| 11068 | Isolate* const isolate = subject->GetIsolate(); |
| 11069 | |
| 11070 | // Flatten {subject} string first. |
| 11071 | subject = String::Flatten(subject); |
| 11072 | |
| 11073 | // Fast array index case. |
| 11074 | uint32_t index; |
| 11075 | if (subject->AsArrayIndex(&index)) { |
| 11076 | return isolate->factory()->NewNumberFromUint(index); |
| 11077 | } |
| 11078 | |
| 11079 | // Fast case: short integer or some sorts of junk values. |
| 11080 | if (subject->IsSeqOneByteString()) { |
| 11081 | int len = subject->length(); |
| 11082 | if (len == 0) return handle(Smi::FromInt(0), isolate); |
| 11083 | |
| 11084 | DisallowHeapAllocation no_gc; |
| 11085 | uint8_t const* data = Handle<SeqOneByteString>::cast(subject)->GetChars(); |
| 11086 | bool minus = (data[0] == '-'); |
| 11087 | int start_pos = (minus ? 1 : 0); |
| 11088 | |
| 11089 | if (start_pos == len) { |
| 11090 | return isolate->factory()->nan_value(); |
| 11091 | } else if (data[start_pos] > '9') { |
| 11092 | // Fast check for a junk value. A valid string may start from a |
| 11093 | // whitespace, a sign ('+' or '-'), the decimal point, a decimal digit |
| 11094 | // or the 'I' character ('Infinity'). All of that have codes not greater |
| 11095 | // than '9' except 'I' and . |
| 11096 | if (data[start_pos] != 'I' && data[start_pos] != 0xa0) { |
| 11097 | return isolate->factory()->nan_value(); |
| 11098 | } |
| 11099 | } else if (len - start_pos < 10 && AreDigits(data, start_pos, len)) { |
| 11100 | // The maximal/minimal smi has 10 digits. If the string has less digits |
| 11101 | // we know it will fit into the smi-data type. |
| 11102 | int d = ParseDecimalInteger(data, start_pos, len); |
| 11103 | if (minus) { |
| 11104 | if (d == 0) return isolate->factory()->minus_zero_value(); |
| 11105 | d = -d; |
| 11106 | } else if (!subject->HasHashCode() && len <= String::kMaxArrayIndexSize && |
| 11107 | (len == 1 || data[0] != '0')) { |
| 11108 | // String hash is not calculated yet but all the data are present. |
| 11109 | // Update the hash field to speed up sequential convertions. |
| 11110 | uint32_t hash = StringHasher::MakeArrayIndexHash(d, len); |
| 11111 | #ifdef DEBUG |
| 11112 | subject->Hash(); // Force hash calculation. |
| 11113 | DCHECK_EQ(static_cast<int>(subject->hash_field()), |
| 11114 | static_cast<int>(hash)); |
| 11115 | #endif |
| 11116 | subject->set_hash_field(hash); |
| 11117 | } |
| 11118 | return handle(Smi::FromInt(d), isolate); |
| 11119 | } |
| 11120 | } |
| 11121 | |
| 11122 | // Slower case. |
| 11123 | int flags = ALLOW_HEX | ALLOW_OCTAL | ALLOW_BINARY; |
| 11124 | return isolate->factory()->NewNumber( |
| 11125 | StringToDouble(isolate->unicode_cache(), subject, flags)); |
| 11126 | } |
| 11127 | |
| 11128 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11129 | String::FlatContent String::GetFlatContent() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11130 | DCHECK(!AllowHeapAllocation::IsAllowed()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11131 | int length = this->length(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11132 | StringShape shape(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11133 | String* string = this; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11134 | int offset = 0; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11135 | if (shape.representation_tag() == kConsStringTag) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11136 | ConsString* cons = ConsString::cast(string); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11137 | if (cons->second()->length() != 0) { |
| 11138 | return FlatContent(); |
| 11139 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11140 | string = cons->first(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11141 | shape = StringShape(string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11142 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11143 | if (shape.representation_tag() == kSlicedStringTag) { |
| 11144 | SlicedString* slice = SlicedString::cast(string); |
| 11145 | offset = slice->offset(); |
| 11146 | string = slice->parent(); |
| 11147 | shape = StringShape(string); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11148 | DCHECK(shape.representation_tag() != kConsStringTag && |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11149 | shape.representation_tag() != kSlicedStringTag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11150 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11151 | if (shape.encoding_tag() == kOneByteStringTag) { |
| 11152 | const uint8_t* start; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11153 | if (shape.representation_tag() == kSeqStringTag) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11154 | start = SeqOneByteString::cast(string)->GetChars(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11155 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11156 | start = ExternalOneByteString::cast(string)->GetChars(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11157 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11158 | return FlatContent(start + offset, length); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11159 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11160 | DCHECK(shape.encoding_tag() == kTwoByteStringTag); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11161 | const uc16* start; |
| 11162 | if (shape.representation_tag() == kSeqStringTag) { |
| 11163 | start = SeqTwoByteString::cast(string)->GetChars(); |
| 11164 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11165 | start = ExternalTwoByteString::cast(string)->GetChars(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11166 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11167 | return FlatContent(start + offset, length); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11168 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11169 | } |
| 11170 | |
| 11171 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 11172 | base::SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls, |
| 11173 | RobustnessFlag robust_flag, |
| 11174 | int offset, int length, |
| 11175 | int* length_return) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11176 | if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 11177 | return base::SmartArrayPointer<char>(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11178 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11179 | // Negative length means the to the end of the string. |
| 11180 | if (length < 0) length = kMaxInt - offset; |
| 11181 | |
| 11182 | // Compute the size of the UTF-8 string. Start at the specified offset. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11183 | StringCharacterStream stream(this, offset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11184 | int character_position = offset; |
| 11185 | int utf8_bytes = 0; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11186 | int last = unibrow::Utf16::kNoPreviousCharacter; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11187 | while (stream.HasMore() && character_position++ < offset + length) { |
| 11188 | uint16_t character = stream.GetNext(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11189 | utf8_bytes += unibrow::Utf8::Length(character, last); |
| 11190 | last = character; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11191 | } |
| 11192 | |
| 11193 | if (length_return) { |
| 11194 | *length_return = utf8_bytes; |
| 11195 | } |
| 11196 | |
| 11197 | char* result = NewArray<char>(utf8_bytes + 1); |
| 11198 | |
| 11199 | // Convert the UTF-16 string to a UTF-8 buffer. Start at the specified offset. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11200 | stream.Reset(this, offset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11201 | character_position = offset; |
| 11202 | int utf8_byte_position = 0; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11203 | last = unibrow::Utf16::kNoPreviousCharacter; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11204 | while (stream.HasMore() && character_position++ < offset + length) { |
| 11205 | uint16_t character = stream.GetNext(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11206 | if (allow_nulls == DISALLOW_NULLS && character == 0) { |
| 11207 | character = ' '; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11208 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11209 | utf8_byte_position += |
| 11210 | unibrow::Utf8::Encode(result + utf8_byte_position, character, last); |
| 11211 | last = character; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11212 | } |
| 11213 | result[utf8_byte_position] = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 11214 | return base::SmartArrayPointer<char>(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11215 | } |
| 11216 | |
| 11217 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 11218 | base::SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls, |
| 11219 | RobustnessFlag robust_flag, |
| 11220 | int* length_return) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11221 | return ToCString(allow_nulls, robust_flag, 0, -1, length_return); |
| 11222 | } |
| 11223 | |
| 11224 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11225 | const uc16* String::GetTwoByteData(unsigned start) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11226 | DCHECK(!IsOneByteRepresentationUnderneath()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11227 | switch (StringShape(this).representation_tag()) { |
| 11228 | case kSeqStringTag: |
| 11229 | return SeqTwoByteString::cast(this)->SeqTwoByteStringGetData(start); |
| 11230 | case kExternalStringTag: |
| 11231 | return ExternalTwoByteString::cast(this)-> |
| 11232 | ExternalTwoByteStringGetData(start); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11233 | case kSlicedStringTag: { |
| 11234 | SlicedString* slice = SlicedString::cast(this); |
| 11235 | return slice->parent()->GetTwoByteData(start + slice->offset()); |
| 11236 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11237 | case kConsStringTag: |
| 11238 | UNREACHABLE(); |
| 11239 | return NULL; |
| 11240 | } |
| 11241 | UNREACHABLE(); |
| 11242 | return NULL; |
| 11243 | } |
| 11244 | |
| 11245 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 11246 | base::SmartArrayPointer<uc16> String::ToWideCString( |
| 11247 | RobustnessFlag robust_flag) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11248 | if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 11249 | return base::SmartArrayPointer<uc16>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11250 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11251 | StringCharacterStream stream(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11252 | |
| 11253 | uc16* result = NewArray<uc16>(length() + 1); |
| 11254 | |
| 11255 | int i = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11256 | while (stream.HasMore()) { |
| 11257 | uint16_t character = stream.GetNext(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11258 | result[i++] = character; |
| 11259 | } |
| 11260 | result[i] = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 11261 | return base::SmartArrayPointer<uc16>(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11262 | } |
| 11263 | |
| 11264 | |
| 11265 | const uc16* SeqTwoByteString::SeqTwoByteStringGetData(unsigned start) { |
| 11266 | return reinterpret_cast<uc16*>( |
| 11267 | reinterpret_cast<char*>(this) - kHeapObjectTag + kHeaderSize) + start; |
| 11268 | } |
| 11269 | |
| 11270 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11271 | void Relocatable::PostGarbageCollectionProcessing(Isolate* isolate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11272 | Relocatable* current = isolate->relocatable_top(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11273 | while (current != NULL) { |
| 11274 | current->PostGarbageCollection(); |
| 11275 | current = current->prev_; |
| 11276 | } |
| 11277 | } |
| 11278 | |
| 11279 | |
| 11280 | // Reserve space for statics needing saving and restoring. |
| 11281 | int Relocatable::ArchiveSpacePerThread() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11282 | return sizeof(Relocatable*); // NOLINT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11283 | } |
| 11284 | |
| 11285 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11286 | // Archive statics that are thread-local. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 11287 | char* Relocatable::ArchiveState(Isolate* isolate, char* to) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11288 | *reinterpret_cast<Relocatable**>(to) = isolate->relocatable_top(); |
| 11289 | isolate->set_relocatable_top(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11290 | return to + ArchiveSpacePerThread(); |
| 11291 | } |
| 11292 | |
| 11293 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11294 | // Restore statics that are thread-local. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 11295 | char* Relocatable::RestoreState(Isolate* isolate, char* from) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11296 | isolate->set_relocatable_top(*reinterpret_cast<Relocatable**>(from)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11297 | return from + ArchiveSpacePerThread(); |
| 11298 | } |
| 11299 | |
| 11300 | |
| 11301 | char* Relocatable::Iterate(ObjectVisitor* v, char* thread_storage) { |
| 11302 | Relocatable* top = *reinterpret_cast<Relocatable**>(thread_storage); |
| 11303 | Iterate(v, top); |
| 11304 | return thread_storage + ArchiveSpacePerThread(); |
| 11305 | } |
| 11306 | |
| 11307 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11308 | void Relocatable::Iterate(Isolate* isolate, ObjectVisitor* v) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11309 | Iterate(v, isolate->relocatable_top()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11310 | } |
| 11311 | |
| 11312 | |
| 11313 | void Relocatable::Iterate(ObjectVisitor* v, Relocatable* top) { |
| 11314 | Relocatable* current = top; |
| 11315 | while (current != NULL) { |
| 11316 | current->IterateInstance(v); |
| 11317 | current = current->prev_; |
| 11318 | } |
| 11319 | } |
| 11320 | |
| 11321 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11322 | FlatStringReader::FlatStringReader(Isolate* isolate, Handle<String> str) |
| 11323 | : Relocatable(isolate), |
| 11324 | str_(str.location()), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11325 | length_(str->length()) { |
| 11326 | PostGarbageCollection(); |
| 11327 | } |
| 11328 | |
| 11329 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11330 | FlatStringReader::FlatStringReader(Isolate* isolate, Vector<const char> input) |
| 11331 | : Relocatable(isolate), |
| 11332 | str_(0), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11333 | is_one_byte_(true), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11334 | length_(input.length()), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11335 | start_(input.start()) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11336 | |
| 11337 | |
| 11338 | void FlatStringReader::PostGarbageCollection() { |
| 11339 | if (str_ == NULL) return; |
| 11340 | Handle<String> str(str_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11341 | DCHECK(str->IsFlat()); |
| 11342 | DisallowHeapAllocation no_gc; |
| 11343 | // This does not actually prevent the vector from being relocated later. |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11344 | String::FlatContent content = str->GetFlatContent(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11345 | DCHECK(content.IsFlat()); |
| 11346 | is_one_byte_ = content.IsOneByte(); |
| 11347 | if (is_one_byte_) { |
| 11348 | start_ = content.ToOneByteVector().start(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11349 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11350 | start_ = content.ToUC16Vector().start(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11351 | } |
| 11352 | } |
| 11353 | |
| 11354 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11355 | void ConsStringIterator::Initialize(ConsString* cons_string, int offset) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11356 | DCHECK(cons_string != NULL); |
| 11357 | root_ = cons_string; |
| 11358 | consumed_ = offset; |
| 11359 | // Force stack blown condition to trigger restart. |
| 11360 | depth_ = 1; |
| 11361 | maximum_depth_ = kStackSize + depth_; |
| 11362 | DCHECK(StackBlown()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11363 | } |
| 11364 | |
| 11365 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11366 | String* ConsStringIterator::Continue(int* offset_out) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11367 | DCHECK(depth_ != 0); |
| 11368 | DCHECK_EQ(0, *offset_out); |
| 11369 | bool blew_stack = StackBlown(); |
| 11370 | String* string = NULL; |
| 11371 | // Get the next leaf if there is one. |
| 11372 | if (!blew_stack) string = NextLeaf(&blew_stack); |
| 11373 | // Restart search from root. |
| 11374 | if (blew_stack) { |
| 11375 | DCHECK(string == NULL); |
| 11376 | string = Search(offset_out); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11377 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11378 | // Ensure future calls return null immediately. |
| 11379 | if (string == NULL) Reset(NULL); |
| 11380 | return string; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11381 | } |
| 11382 | |
| 11383 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11384 | String* ConsStringIterator::Search(int* offset_out) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11385 | ConsString* cons_string = root_; |
| 11386 | // Reset the stack, pushing the root string. |
| 11387 | depth_ = 1; |
| 11388 | maximum_depth_ = 1; |
| 11389 | frames_[0] = cons_string; |
| 11390 | const int consumed = consumed_; |
| 11391 | int offset = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11392 | while (true) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11393 | // Loop until the string is found which contains the target offset. |
| 11394 | String* string = cons_string->first(); |
| 11395 | int length = string->length(); |
| 11396 | int32_t type; |
| 11397 | if (consumed < offset + length) { |
| 11398 | // Target offset is in the left branch. |
| 11399 | // Keep going if we're still in a ConString. |
| 11400 | type = string->map()->instance_type(); |
| 11401 | if ((type & kStringRepresentationMask) == kConsStringTag) { |
| 11402 | cons_string = ConsString::cast(string); |
| 11403 | PushLeft(cons_string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11404 | continue; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11405 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11406 | // Tell the stack we're done descending. |
| 11407 | AdjustMaximumDepth(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11408 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11409 | // Descend right. |
| 11410 | // Update progress through the string. |
| 11411 | offset += length; |
| 11412 | // Keep going if we're still in a ConString. |
| 11413 | string = cons_string->second(); |
| 11414 | type = string->map()->instance_type(); |
| 11415 | if ((type & kStringRepresentationMask) == kConsStringTag) { |
| 11416 | cons_string = ConsString::cast(string); |
| 11417 | PushRight(cons_string); |
| 11418 | continue; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11419 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11420 | // Need this to be updated for the current string. |
| 11421 | length = string->length(); |
| 11422 | // Account for the possibility of an empty right leaf. |
| 11423 | // This happens only if we have asked for an offset outside the string. |
| 11424 | if (length == 0) { |
| 11425 | // Reset so future operations will return null immediately. |
| 11426 | Reset(NULL); |
| 11427 | return NULL; |
| 11428 | } |
| 11429 | // Tell the stack we're done descending. |
| 11430 | AdjustMaximumDepth(); |
| 11431 | // Pop stack so next iteration is in correct place. |
| 11432 | Pop(); |
| 11433 | } |
| 11434 | DCHECK(length != 0); |
| 11435 | // Adjust return values and exit. |
| 11436 | consumed_ = offset + length; |
| 11437 | *offset_out = consumed - offset; |
| 11438 | return string; |
| 11439 | } |
| 11440 | UNREACHABLE(); |
| 11441 | return NULL; |
| 11442 | } |
| 11443 | |
| 11444 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11445 | String* ConsStringIterator::NextLeaf(bool* blew_stack) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11446 | while (true) { |
| 11447 | // Tree traversal complete. |
| 11448 | if (depth_ == 0) { |
| 11449 | *blew_stack = false; |
| 11450 | return NULL; |
| 11451 | } |
| 11452 | // We've lost track of higher nodes. |
| 11453 | if (StackBlown()) { |
| 11454 | *blew_stack = true; |
| 11455 | return NULL; |
| 11456 | } |
| 11457 | // Go right. |
| 11458 | ConsString* cons_string = frames_[OffsetForDepth(depth_ - 1)]; |
| 11459 | String* string = cons_string->second(); |
| 11460 | int32_t type = string->map()->instance_type(); |
| 11461 | if ((type & kStringRepresentationMask) != kConsStringTag) { |
| 11462 | // Pop stack so next iteration is in correct place. |
| 11463 | Pop(); |
| 11464 | int length = string->length(); |
| 11465 | // Could be a flattened ConsString. |
| 11466 | if (length == 0) continue; |
| 11467 | consumed_ += length; |
| 11468 | return string; |
| 11469 | } |
| 11470 | cons_string = ConsString::cast(string); |
| 11471 | PushRight(cons_string); |
| 11472 | // Need to traverse all the way left. |
| 11473 | while (true) { |
| 11474 | // Continue left. |
| 11475 | string = cons_string->first(); |
| 11476 | type = string->map()->instance_type(); |
| 11477 | if ((type & kStringRepresentationMask) != kConsStringTag) { |
| 11478 | AdjustMaximumDepth(); |
| 11479 | int length = string->length(); |
| 11480 | DCHECK(length != 0); |
| 11481 | consumed_ += length; |
| 11482 | return string; |
| 11483 | } |
| 11484 | cons_string = ConsString::cast(string); |
| 11485 | PushLeft(cons_string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11486 | } |
| 11487 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11488 | UNREACHABLE(); |
| 11489 | return NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11490 | } |
| 11491 | |
| 11492 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11493 | uint16_t ConsString::ConsStringGet(int index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11494 | DCHECK(index >= 0 && index < this->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11495 | |
| 11496 | // Check for a flattened cons string |
| 11497 | if (second()->length() == 0) { |
| 11498 | String* left = first(); |
| 11499 | return left->Get(index); |
| 11500 | } |
| 11501 | |
| 11502 | String* string = String::cast(this); |
| 11503 | |
| 11504 | while (true) { |
| 11505 | if (StringShape(string).IsCons()) { |
| 11506 | ConsString* cons_string = ConsString::cast(string); |
| 11507 | String* left = cons_string->first(); |
| 11508 | if (left->length() > index) { |
| 11509 | string = left; |
| 11510 | } else { |
| 11511 | index -= left->length(); |
| 11512 | string = cons_string->second(); |
| 11513 | } |
| 11514 | } else { |
| 11515 | return string->Get(index); |
| 11516 | } |
| 11517 | } |
| 11518 | |
| 11519 | UNREACHABLE(); |
| 11520 | return 0; |
| 11521 | } |
| 11522 | |
| 11523 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11524 | uint16_t SlicedString::SlicedStringGet(int index) { |
| 11525 | return parent()->Get(offset() + index); |
| 11526 | } |
| 11527 | |
| 11528 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11529 | template <typename sinkchar> |
| 11530 | void String::WriteToFlat(String* src, |
| 11531 | sinkchar* sink, |
| 11532 | int f, |
| 11533 | int t) { |
| 11534 | String* source = src; |
| 11535 | int from = f; |
| 11536 | int to = t; |
| 11537 | while (true) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11538 | DCHECK(0 <= from && from <= to && to <= source->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11539 | switch (StringShape(source).full_representation_tag()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11540 | case kOneByteStringTag | kExternalStringTag: { |
| 11541 | CopyChars(sink, ExternalOneByteString::cast(source)->GetChars() + from, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11542 | to - from); |
| 11543 | return; |
| 11544 | } |
| 11545 | case kTwoByteStringTag | kExternalStringTag: { |
| 11546 | const uc16* data = |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11547 | ExternalTwoByteString::cast(source)->GetChars(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11548 | CopyChars(sink, |
| 11549 | data + from, |
| 11550 | to - from); |
| 11551 | return; |
| 11552 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11553 | case kOneByteStringTag | kSeqStringTag: { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11554 | CopyChars(sink, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11555 | SeqOneByteString::cast(source)->GetChars() + from, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11556 | to - from); |
| 11557 | return; |
| 11558 | } |
| 11559 | case kTwoByteStringTag | kSeqStringTag: { |
| 11560 | CopyChars(sink, |
| 11561 | SeqTwoByteString::cast(source)->GetChars() + from, |
| 11562 | to - from); |
| 11563 | return; |
| 11564 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11565 | case kOneByteStringTag | kConsStringTag: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11566 | case kTwoByteStringTag | kConsStringTag: { |
| 11567 | ConsString* cons_string = ConsString::cast(source); |
| 11568 | String* first = cons_string->first(); |
| 11569 | int boundary = first->length(); |
| 11570 | if (to - boundary >= boundary - from) { |
| 11571 | // Right hand side is longer. Recurse over left. |
| 11572 | if (from < boundary) { |
| 11573 | WriteToFlat(first, sink, from, boundary); |
| 11574 | sink += boundary - from; |
| 11575 | from = 0; |
| 11576 | } else { |
| 11577 | from -= boundary; |
| 11578 | } |
| 11579 | to -= boundary; |
| 11580 | source = cons_string->second(); |
| 11581 | } else { |
| 11582 | // Left hand side is longer. Recurse over right. |
| 11583 | if (to > boundary) { |
| 11584 | String* second = cons_string->second(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11585 | // When repeatedly appending to a string, we get a cons string that |
| 11586 | // is unbalanced to the left, a list, essentially. We inline the |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11587 | // common case of sequential one-byte right child. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11588 | if (to - boundary == 1) { |
| 11589 | sink[boundary - from] = static_cast<sinkchar>(second->Get(0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11590 | } else if (second->IsSeqOneByteString()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11591 | CopyChars(sink + boundary - from, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11592 | SeqOneByteString::cast(second)->GetChars(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11593 | to - boundary); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11594 | } else { |
| 11595 | WriteToFlat(second, |
| 11596 | sink + boundary - from, |
| 11597 | 0, |
| 11598 | to - boundary); |
| 11599 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11600 | to = boundary; |
| 11601 | } |
| 11602 | source = first; |
| 11603 | } |
| 11604 | break; |
| 11605 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11606 | case kOneByteStringTag | kSlicedStringTag: |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11607 | case kTwoByteStringTag | kSlicedStringTag: { |
| 11608 | SlicedString* slice = SlicedString::cast(source); |
| 11609 | unsigned offset = slice->offset(); |
| 11610 | WriteToFlat(slice->parent(), sink, from + offset, to + offset); |
| 11611 | return; |
| 11612 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11613 | } |
| 11614 | } |
| 11615 | } |
| 11616 | |
| 11617 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11618 | |
| 11619 | template <typename SourceChar> |
| 11620 | static void CalculateLineEndsImpl(Isolate* isolate, |
| 11621 | List<int>* line_ends, |
| 11622 | Vector<const SourceChar> src, |
| 11623 | bool include_ending_line) { |
| 11624 | const int src_len = src.length(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 11625 | UnicodeCache* cache = isolate->unicode_cache(); |
| 11626 | for (int i = 0; i < src_len - 1; i++) { |
| 11627 | SourceChar current = src[i]; |
| 11628 | SourceChar next = src[i + 1]; |
| 11629 | if (cache->IsLineTerminatorSequence(current, next)) line_ends->Add(i); |
| 11630 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11631 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 11632 | if (src_len > 0 && cache->IsLineTerminatorSequence(src[src_len - 1], 0)) { |
| 11633 | line_ends->Add(src_len - 1); |
| 11634 | } |
| 11635 | if (include_ending_line) { |
| 11636 | // Include one character beyond the end of script. The rewriter uses that |
| 11637 | // position for the implicit return statement. |
| 11638 | line_ends->Add(src_len); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11639 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11640 | } |
| 11641 | |
| 11642 | |
| 11643 | Handle<FixedArray> String::CalculateLineEnds(Handle<String> src, |
| 11644 | bool include_ending_line) { |
| 11645 | src = Flatten(src); |
| 11646 | // Rough estimate of line count based on a roughly estimated average |
| 11647 | // length of (unpacked) code. |
| 11648 | int line_count_estimate = src->length() >> 4; |
| 11649 | List<int> line_ends(line_count_estimate); |
| 11650 | Isolate* isolate = src->GetIsolate(); |
| 11651 | { DisallowHeapAllocation no_allocation; // ensure vectors stay valid. |
| 11652 | // Dispatch on type of strings. |
| 11653 | String::FlatContent content = src->GetFlatContent(); |
| 11654 | DCHECK(content.IsFlat()); |
| 11655 | if (content.IsOneByte()) { |
| 11656 | CalculateLineEndsImpl(isolate, |
| 11657 | &line_ends, |
| 11658 | content.ToOneByteVector(), |
| 11659 | include_ending_line); |
| 11660 | } else { |
| 11661 | CalculateLineEndsImpl(isolate, |
| 11662 | &line_ends, |
| 11663 | content.ToUC16Vector(), |
| 11664 | include_ending_line); |
| 11665 | } |
| 11666 | } |
| 11667 | int line_count = line_ends.length(); |
| 11668 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(line_count); |
| 11669 | for (int i = 0; i < line_count; i++) { |
| 11670 | array->set(i, Smi::FromInt(line_ends[i])); |
| 11671 | } |
| 11672 | return array; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11673 | } |
| 11674 | |
| 11675 | |
| 11676 | // Compares the contents of two strings by reading and comparing |
| 11677 | // int-sized blocks of characters. |
| 11678 | template <typename Char> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11679 | static inline bool CompareRawStringContents(const Char* const a, |
| 11680 | const Char* const b, |
| 11681 | int length) { |
| 11682 | return CompareChars(a, b, length) == 0; |
| 11683 | } |
| 11684 | |
| 11685 | |
| 11686 | template<typename Chars1, typename Chars2> |
| 11687 | class RawStringComparator : public AllStatic { |
| 11688 | public: |
| 11689 | static inline bool compare(const Chars1* a, const Chars2* b, int len) { |
| 11690 | DCHECK(sizeof(Chars1) != sizeof(Chars2)); |
| 11691 | for (int i = 0; i < len; i++) { |
| 11692 | if (a[i] != b[i]) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11693 | return false; |
| 11694 | } |
| 11695 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11696 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11697 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11698 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11699 | |
| 11700 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11701 | template<> |
| 11702 | class RawStringComparator<uint16_t, uint16_t> { |
| 11703 | public: |
| 11704 | static inline bool compare(const uint16_t* a, const uint16_t* b, int len) { |
| 11705 | return CompareRawStringContents(a, b, len); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11706 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11707 | }; |
| 11708 | |
| 11709 | |
| 11710 | template<> |
| 11711 | class RawStringComparator<uint8_t, uint8_t> { |
| 11712 | public: |
| 11713 | static inline bool compare(const uint8_t* a, const uint8_t* b, int len) { |
| 11714 | return CompareRawStringContents(a, b, len); |
| 11715 | } |
| 11716 | }; |
| 11717 | |
| 11718 | |
| 11719 | class StringComparator { |
| 11720 | class State { |
| 11721 | public: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11722 | State() : is_one_byte_(true), length_(0), buffer8_(NULL) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11723 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11724 | void Init(String* string) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11725 | ConsString* cons_string = String::VisitFlat(this, string); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11726 | iter_.Reset(cons_string); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11727 | if (cons_string != NULL) { |
| 11728 | int offset; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11729 | string = iter_.Next(&offset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11730 | String::VisitFlat(this, string, offset); |
| 11731 | } |
| 11732 | } |
| 11733 | |
| 11734 | inline void VisitOneByteString(const uint8_t* chars, int length) { |
| 11735 | is_one_byte_ = true; |
| 11736 | buffer8_ = chars; |
| 11737 | length_ = length; |
| 11738 | } |
| 11739 | |
| 11740 | inline void VisitTwoByteString(const uint16_t* chars, int length) { |
| 11741 | is_one_byte_ = false; |
| 11742 | buffer16_ = chars; |
| 11743 | length_ = length; |
| 11744 | } |
| 11745 | |
| 11746 | void Advance(int consumed) { |
| 11747 | DCHECK(consumed <= length_); |
| 11748 | // Still in buffer. |
| 11749 | if (length_ != consumed) { |
| 11750 | if (is_one_byte_) { |
| 11751 | buffer8_ += consumed; |
| 11752 | } else { |
| 11753 | buffer16_ += consumed; |
| 11754 | } |
| 11755 | length_ -= consumed; |
| 11756 | return; |
| 11757 | } |
| 11758 | // Advance state. |
| 11759 | int offset; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11760 | String* next = iter_.Next(&offset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11761 | DCHECK_EQ(0, offset); |
| 11762 | DCHECK(next != NULL); |
| 11763 | String::VisitFlat(this, next); |
| 11764 | } |
| 11765 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11766 | ConsStringIterator iter_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11767 | bool is_one_byte_; |
| 11768 | int length_; |
| 11769 | union { |
| 11770 | const uint8_t* buffer8_; |
| 11771 | const uint16_t* buffer16_; |
| 11772 | }; |
| 11773 | |
| 11774 | private: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11775 | DISALLOW_COPY_AND_ASSIGN(State); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11776 | }; |
| 11777 | |
| 11778 | public: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11779 | inline StringComparator() {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11780 | |
| 11781 | template<typename Chars1, typename Chars2> |
| 11782 | static inline bool Equals(State* state_1, State* state_2, int to_check) { |
| 11783 | const Chars1* a = reinterpret_cast<const Chars1*>(state_1->buffer8_); |
| 11784 | const Chars2* b = reinterpret_cast<const Chars2*>(state_2->buffer8_); |
| 11785 | return RawStringComparator<Chars1, Chars2>::compare(a, b, to_check); |
| 11786 | } |
| 11787 | |
| 11788 | bool Equals(String* string_1, String* string_2) { |
| 11789 | int length = string_1->length(); |
| 11790 | state_1_.Init(string_1); |
| 11791 | state_2_.Init(string_2); |
| 11792 | while (true) { |
| 11793 | int to_check = Min(state_1_.length_, state_2_.length_); |
| 11794 | DCHECK(to_check > 0 && to_check <= length); |
| 11795 | bool is_equal; |
| 11796 | if (state_1_.is_one_byte_) { |
| 11797 | if (state_2_.is_one_byte_) { |
| 11798 | is_equal = Equals<uint8_t, uint8_t>(&state_1_, &state_2_, to_check); |
| 11799 | } else { |
| 11800 | is_equal = Equals<uint8_t, uint16_t>(&state_1_, &state_2_, to_check); |
| 11801 | } |
| 11802 | } else { |
| 11803 | if (state_2_.is_one_byte_) { |
| 11804 | is_equal = Equals<uint16_t, uint8_t>(&state_1_, &state_2_, to_check); |
| 11805 | } else { |
| 11806 | is_equal = Equals<uint16_t, uint16_t>(&state_1_, &state_2_, to_check); |
| 11807 | } |
| 11808 | } |
| 11809 | // Looping done. |
| 11810 | if (!is_equal) return false; |
| 11811 | length -= to_check; |
| 11812 | // Exit condition. Strings are equal. |
| 11813 | if (length == 0) return true; |
| 11814 | state_1_.Advance(to_check); |
| 11815 | state_2_.Advance(to_check); |
| 11816 | } |
| 11817 | } |
| 11818 | |
| 11819 | private: |
| 11820 | State state_1_; |
| 11821 | State state_2_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11822 | |
| 11823 | DISALLOW_COPY_AND_ASSIGN(StringComparator); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11824 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11825 | |
| 11826 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11827 | bool String::SlowEquals(String* other) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11828 | DisallowHeapAllocation no_gc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11829 | // Fast check: negative check with lengths. |
| 11830 | int len = length(); |
| 11831 | if (len != other->length()) return false; |
| 11832 | if (len == 0) return true; |
| 11833 | |
| 11834 | // Fast check: if hash code is computed for both strings |
| 11835 | // a fast negative check can be performed. |
| 11836 | if (HasHashCode() && other->HasHashCode()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11837 | #ifdef ENABLE_SLOW_DCHECKS |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 11838 | if (FLAG_enable_slow_asserts) { |
| 11839 | if (Hash() != other->Hash()) { |
| 11840 | bool found_difference = false; |
| 11841 | for (int i = 0; i < len; i++) { |
| 11842 | if (Get(i) != other->Get(i)) { |
| 11843 | found_difference = true; |
| 11844 | break; |
| 11845 | } |
| 11846 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11847 | DCHECK(found_difference); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 11848 | } |
| 11849 | } |
| 11850 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11851 | if (Hash() != other->Hash()) return false; |
| 11852 | } |
| 11853 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 11854 | // We know the strings are both non-empty. Compare the first chars |
| 11855 | // before we try to flatten the strings. |
| 11856 | if (this->Get(0) != other->Get(0)) return false; |
| 11857 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11858 | if (IsSeqOneByteString() && other->IsSeqOneByteString()) { |
| 11859 | const uint8_t* str1 = SeqOneByteString::cast(this)->GetChars(); |
| 11860 | const uint8_t* str2 = SeqOneByteString::cast(other)->GetChars(); |
| 11861 | return CompareRawStringContents(str1, str2, len); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11862 | } |
| 11863 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11864 | StringComparator comparator; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11865 | return comparator.Equals(this, other); |
| 11866 | } |
| 11867 | |
| 11868 | |
| 11869 | bool String::SlowEquals(Handle<String> one, Handle<String> two) { |
| 11870 | // Fast check: negative check with lengths. |
| 11871 | int one_length = one->length(); |
| 11872 | if (one_length != two->length()) return false; |
| 11873 | if (one_length == 0) return true; |
| 11874 | |
| 11875 | // Fast check: if hash code is computed for both strings |
| 11876 | // a fast negative check can be performed. |
| 11877 | if (one->HasHashCode() && two->HasHashCode()) { |
| 11878 | #ifdef ENABLE_SLOW_DCHECKS |
| 11879 | if (FLAG_enable_slow_asserts) { |
| 11880 | if (one->Hash() != two->Hash()) { |
| 11881 | bool found_difference = false; |
| 11882 | for (int i = 0; i < one_length; i++) { |
| 11883 | if (one->Get(i) != two->Get(i)) { |
| 11884 | found_difference = true; |
| 11885 | break; |
| 11886 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11887 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11888 | DCHECK(found_difference); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11889 | } |
| 11890 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11891 | #endif |
| 11892 | if (one->Hash() != two->Hash()) return false; |
| 11893 | } |
| 11894 | |
| 11895 | // We know the strings are both non-empty. Compare the first chars |
| 11896 | // before we try to flatten the strings. |
| 11897 | if (one->Get(0) != two->Get(0)) return false; |
| 11898 | |
| 11899 | one = String::Flatten(one); |
| 11900 | two = String::Flatten(two); |
| 11901 | |
| 11902 | DisallowHeapAllocation no_gc; |
| 11903 | String::FlatContent flat1 = one->GetFlatContent(); |
| 11904 | String::FlatContent flat2 = two->GetFlatContent(); |
| 11905 | |
| 11906 | if (flat1.IsOneByte() && flat2.IsOneByte()) { |
| 11907 | return CompareRawStringContents(flat1.ToOneByteVector().start(), |
| 11908 | flat2.ToOneByteVector().start(), |
| 11909 | one_length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11910 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11911 | for (int i = 0; i < one_length; i++) { |
| 11912 | if (flat1.Get(i) != flat2.Get(i)) return false; |
| 11913 | } |
| 11914 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11915 | } |
| 11916 | } |
| 11917 | |
| 11918 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 11919 | // static |
| 11920 | ComparisonResult String::Compare(Handle<String> x, Handle<String> y) { |
| 11921 | // A few fast case tests before we flatten. |
| 11922 | if (x.is_identical_to(y)) { |
| 11923 | return ComparisonResult::kEqual; |
| 11924 | } else if (y->length() == 0) { |
| 11925 | return x->length() == 0 ? ComparisonResult::kEqual |
| 11926 | : ComparisonResult::kGreaterThan; |
| 11927 | } else if (x->length() == 0) { |
| 11928 | return ComparisonResult::kLessThan; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11929 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 11930 | |
| 11931 | int const d = x->Get(0) - y->Get(0); |
| 11932 | if (d < 0) { |
| 11933 | return ComparisonResult::kLessThan; |
| 11934 | } else if (d > 0) { |
| 11935 | return ComparisonResult::kGreaterThan; |
| 11936 | } |
| 11937 | |
| 11938 | // Slow case. |
| 11939 | x = String::Flatten(x); |
| 11940 | y = String::Flatten(y); |
| 11941 | |
| 11942 | DisallowHeapAllocation no_gc; |
| 11943 | ComparisonResult result = ComparisonResult::kEqual; |
| 11944 | int prefix_length = x->length(); |
| 11945 | if (y->length() < prefix_length) { |
| 11946 | prefix_length = y->length(); |
| 11947 | result = ComparisonResult::kGreaterThan; |
| 11948 | } else if (y->length() > prefix_length) { |
| 11949 | result = ComparisonResult::kLessThan; |
| 11950 | } |
| 11951 | int r; |
| 11952 | String::FlatContent x_content = x->GetFlatContent(); |
| 11953 | String::FlatContent y_content = y->GetFlatContent(); |
| 11954 | if (x_content.IsOneByte()) { |
| 11955 | Vector<const uint8_t> x_chars = x_content.ToOneByteVector(); |
| 11956 | if (y_content.IsOneByte()) { |
| 11957 | Vector<const uint8_t> y_chars = y_content.ToOneByteVector(); |
| 11958 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 11959 | } else { |
| 11960 | Vector<const uc16> y_chars = y_content.ToUC16Vector(); |
| 11961 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 11962 | } |
| 11963 | } else { |
| 11964 | Vector<const uc16> x_chars = x_content.ToUC16Vector(); |
| 11965 | if (y_content.IsOneByte()) { |
| 11966 | Vector<const uint8_t> y_chars = y_content.ToOneByteVector(); |
| 11967 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 11968 | } else { |
| 11969 | Vector<const uc16> y_chars = y_content.ToUC16Vector(); |
| 11970 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 11971 | } |
| 11972 | } |
| 11973 | if (r < 0) { |
| 11974 | result = ComparisonResult::kLessThan; |
| 11975 | } else if (r > 0) { |
| 11976 | result = ComparisonResult::kGreaterThan; |
| 11977 | } |
| 11978 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11979 | } |
| 11980 | |
| 11981 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11982 | bool String::IsUtf8EqualTo(Vector<const char> str, bool allow_prefix_match) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11983 | int slen = length(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11984 | // Can't check exact length equality, but we can check bounds. |
| 11985 | int str_len = str.length(); |
| 11986 | if (!allow_prefix_match && |
| 11987 | (str_len < slen || |
| 11988 | str_len > slen*static_cast<int>(unibrow::Utf8::kMaxEncodedSize))) { |
| 11989 | return false; |
| 11990 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11991 | int i; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 11992 | size_t remaining_in_str = static_cast<size_t>(str_len); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11993 | const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(str.start()); |
| 11994 | for (i = 0; i < slen && remaining_in_str > 0; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 11995 | size_t cursor = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11996 | uint32_t r = unibrow::Utf8::ValueOf(utf8_data, remaining_in_str, &cursor); |
| 11997 | DCHECK(cursor > 0 && cursor <= remaining_in_str); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11998 | if (r > unibrow::Utf16::kMaxNonSurrogateCharCode) { |
| 11999 | if (i > slen - 1) return false; |
| 12000 | if (Get(i++) != unibrow::Utf16::LeadSurrogate(r)) return false; |
| 12001 | if (Get(i) != unibrow::Utf16::TrailSurrogate(r)) return false; |
| 12002 | } else { |
| 12003 | if (Get(i) != r) return false; |
| 12004 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12005 | utf8_data += cursor; |
| 12006 | remaining_in_str -= cursor; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12007 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12008 | return (allow_prefix_match || i == slen) && remaining_in_str == 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12009 | } |
| 12010 | |
| 12011 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12012 | bool String::IsOneByteEqualTo(Vector<const uint8_t> str) { |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 12013 | int slen = length(); |
| 12014 | if (str.length() != slen) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12015 | DisallowHeapAllocation no_gc; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 12016 | FlatContent content = GetFlatContent(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12017 | if (content.IsOneByte()) { |
| 12018 | return CompareChars(content.ToOneByteVector().start(), |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 12019 | str.start(), slen) == 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12020 | } |
| 12021 | for (int i = 0; i < slen; i++) { |
| 12022 | if (Get(i) != static_cast<uint16_t>(str[i])) return false; |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 12023 | } |
| 12024 | return true; |
| 12025 | } |
| 12026 | |
| 12027 | |
| 12028 | bool String::IsTwoByteEqualTo(Vector<const uc16> str) { |
| 12029 | int slen = length(); |
| 12030 | if (str.length() != slen) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12031 | DisallowHeapAllocation no_gc; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 12032 | FlatContent content = GetFlatContent(); |
| 12033 | if (content.IsTwoByte()) { |
| 12034 | return CompareChars(content.ToUC16Vector().start(), str.start(), slen) == 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12035 | } |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 12036 | for (int i = 0; i < slen; i++) { |
| 12037 | if (Get(i) != str[i]) return false; |
| 12038 | } |
| 12039 | return true; |
| 12040 | } |
| 12041 | |
| 12042 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12043 | uint32_t String::ComputeAndSetHash() { |
| 12044 | // Should only be called if hash code has not yet been computed. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12045 | DCHECK(!HasHashCode()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12046 | |
| 12047 | // Store the hash code in the object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12048 | uint32_t field = IteratingStringHasher::Hash(this, GetHeap()->HashSeed()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 12049 | set_hash_field(field); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12050 | |
| 12051 | // Check the hash code is there. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12052 | DCHECK(HasHashCode()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12053 | uint32_t result = field >> kHashShift; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12054 | DCHECK(result != 0); // Ensure that the hash value of 0 is never computed. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12055 | return result; |
| 12056 | } |
| 12057 | |
| 12058 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12059 | bool String::ComputeArrayIndex(uint32_t* index) { |
| 12060 | int length = this->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12061 | if (length == 0 || length > kMaxArrayIndexSize) return false; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12062 | StringCharacterStream stream(this); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12063 | return StringToArrayIndex(&stream, index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12064 | } |
| 12065 | |
| 12066 | |
| 12067 | bool String::SlowAsArrayIndex(uint32_t* index) { |
| 12068 | if (length() <= kMaxCachedArrayIndexLength) { |
| 12069 | Hash(); // force computation of hash code |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 12070 | uint32_t field = hash_field(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 12071 | if ((field & kIsNotArrayIndexMask) != 0) return false; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 12072 | // Isolate the array index form the full hash field. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12073 | *index = ArrayIndexValueBits::decode(field); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12074 | return true; |
| 12075 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12076 | return ComputeArrayIndex(index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12077 | } |
| 12078 | } |
| 12079 | |
| 12080 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12081 | Handle<String> SeqString::Truncate(Handle<SeqString> string, int new_length) { |
| 12082 | int new_size, old_size; |
| 12083 | int old_length = string->length(); |
| 12084 | if (old_length <= new_length) return string; |
| 12085 | |
| 12086 | if (string->IsSeqOneByteString()) { |
| 12087 | old_size = SeqOneByteString::SizeFor(old_length); |
| 12088 | new_size = SeqOneByteString::SizeFor(new_length); |
| 12089 | } else { |
| 12090 | DCHECK(string->IsSeqTwoByteString()); |
| 12091 | old_size = SeqTwoByteString::SizeFor(old_length); |
| 12092 | new_size = SeqTwoByteString::SizeFor(new_length); |
| 12093 | } |
| 12094 | |
| 12095 | int delta = old_size - new_size; |
| 12096 | |
| 12097 | Address start_of_string = string->address(); |
| 12098 | DCHECK_OBJECT_ALIGNED(start_of_string); |
| 12099 | DCHECK_OBJECT_ALIGNED(start_of_string + new_size); |
| 12100 | |
| 12101 | Heap* heap = string->GetHeap(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12102 | // Sizes are pointer size aligned, so that we can use filler objects |
| 12103 | // that are a multiple of pointer size. |
| 12104 | heap->CreateFillerObjectAt(start_of_string + new_size, delta); |
| 12105 | heap->AdjustLiveBytes(*string, -delta, Heap::CONCURRENT_TO_SWEEPER); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12106 | |
| 12107 | // We are storing the new length using release store after creating a filler |
| 12108 | // for the left-over space to avoid races with the sweeper thread. |
| 12109 | string->synchronized_set_length(new_length); |
| 12110 | |
| 12111 | if (new_length == 0) return heap->isolate()->factory()->empty_string(); |
| 12112 | return string; |
| 12113 | } |
| 12114 | |
| 12115 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 12116 | uint32_t StringHasher::MakeArrayIndexHash(uint32_t value, int length) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 12117 | // For array indexes mix the length into the hash as an array index could |
| 12118 | // be zero. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12119 | DCHECK(length > 0); |
| 12120 | DCHECK(length <= String::kMaxArrayIndexSize); |
| 12121 | DCHECK(TenToThe(String::kMaxCachedArrayIndexLength) < |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 12122 | (1 << String::kArrayIndexValueBits)); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 12123 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12124 | value <<= String::ArrayIndexValueBits::kShift; |
| 12125 | value |= length << String::ArrayIndexLengthBits::kShift; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 12126 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12127 | DCHECK((value & String::kIsNotArrayIndexMask) == 0); |
| 12128 | DCHECK((length > String::kMaxCachedArrayIndexLength) || |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 12129 | (value & String::kContainsCachedArrayIndexMask) == 0); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 12130 | return value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12131 | } |
| 12132 | |
| 12133 | |
| 12134 | uint32_t StringHasher::GetHashField() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 12135 | if (length_ <= String::kMaxHashCalcLength) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12136 | if (is_array_index_) { |
| 12137 | return MakeArrayIndexHash(array_index_, length_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12138 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12139 | return (GetHashCore(raw_running_hash_) << String::kHashShift) | |
| 12140 | String::kIsNotArrayIndexMask; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12141 | } else { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 12142 | return (length_ << String::kHashShift) | String::kIsNotArrayIndexMask; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12143 | } |
| 12144 | } |
| 12145 | |
| 12146 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12147 | uint32_t StringHasher::ComputeUtf8Hash(Vector<const char> chars, |
| 12148 | uint32_t seed, |
| 12149 | int* utf16_length_out) { |
| 12150 | int vector_length = chars.length(); |
| 12151 | // Handle some edge cases |
| 12152 | if (vector_length <= 1) { |
| 12153 | DCHECK(vector_length == 0 || |
| 12154 | static_cast<uint8_t>(chars.start()[0]) <= |
| 12155 | unibrow::Utf8::kMaxOneByteChar); |
| 12156 | *utf16_length_out = vector_length; |
| 12157 | return HashSequentialString(chars.start(), vector_length, seed); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12158 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12159 | // Start with a fake length which won't affect computation. |
| 12160 | // It will be updated later. |
| 12161 | StringHasher hasher(String::kMaxArrayIndexSize, seed); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12162 | size_t remaining = static_cast<size_t>(vector_length); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12163 | const uint8_t* stream = reinterpret_cast<const uint8_t*>(chars.start()); |
| 12164 | int utf16_length = 0; |
| 12165 | bool is_index = true; |
| 12166 | DCHECK(hasher.is_array_index_); |
| 12167 | while (remaining > 0) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12168 | size_t consumed = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12169 | uint32_t c = unibrow::Utf8::ValueOf(stream, remaining, &consumed); |
| 12170 | DCHECK(consumed > 0 && consumed <= remaining); |
| 12171 | stream += consumed; |
| 12172 | remaining -= consumed; |
| 12173 | bool is_two_characters = c > unibrow::Utf16::kMaxNonSurrogateCharCode; |
| 12174 | utf16_length += is_two_characters ? 2 : 1; |
| 12175 | // No need to keep hashing. But we do need to calculate utf16_length. |
| 12176 | if (utf16_length > String::kMaxHashCalcLength) continue; |
| 12177 | if (is_two_characters) { |
| 12178 | uint16_t c1 = unibrow::Utf16::LeadSurrogate(c); |
| 12179 | uint16_t c2 = unibrow::Utf16::TrailSurrogate(c); |
| 12180 | hasher.AddCharacter(c1); |
| 12181 | hasher.AddCharacter(c2); |
| 12182 | if (is_index) is_index = hasher.UpdateIndex(c1); |
| 12183 | if (is_index) is_index = hasher.UpdateIndex(c2); |
| 12184 | } else { |
| 12185 | hasher.AddCharacter(c); |
| 12186 | if (is_index) is_index = hasher.UpdateIndex(c); |
| 12187 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12188 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12189 | *utf16_length_out = static_cast<int>(utf16_length); |
| 12190 | // Must set length here so that hash computation is correct. |
| 12191 | hasher.length_ = utf16_length; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12192 | return hasher.GetHashField(); |
| 12193 | } |
| 12194 | |
| 12195 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12196 | void IteratingStringHasher::VisitConsString(ConsString* cons_string) { |
| 12197 | // Run small ConsStrings through ConsStringIterator. |
| 12198 | if (cons_string->length() < 64) { |
| 12199 | ConsStringIterator iter(cons_string); |
| 12200 | int offset; |
| 12201 | String* string; |
| 12202 | while (nullptr != (string = iter.Next(&offset))) { |
| 12203 | DCHECK_EQ(0, offset); |
| 12204 | String::VisitFlat(this, string, 0); |
| 12205 | } |
| 12206 | return; |
| 12207 | } |
| 12208 | // Slow case. |
| 12209 | const int max_length = String::kMaxHashCalcLength; |
| 12210 | int length = std::min(cons_string->length(), max_length); |
| 12211 | if (cons_string->HasOnlyOneByteChars()) { |
| 12212 | uint8_t* buffer = new uint8_t[length]; |
| 12213 | String::WriteToFlat(cons_string, buffer, 0, length); |
| 12214 | AddCharacters(buffer, length); |
| 12215 | delete[] buffer; |
| 12216 | } else { |
| 12217 | uint16_t* buffer = new uint16_t[length]; |
| 12218 | String::WriteToFlat(cons_string, buffer, 0, length); |
| 12219 | AddCharacters(buffer, length); |
| 12220 | delete[] buffer; |
| 12221 | } |
| 12222 | } |
| 12223 | |
| 12224 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12225 | void String::PrintOn(FILE* file) { |
| 12226 | int length = this->length(); |
| 12227 | for (int i = 0; i < length; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12228 | PrintF(file, "%c", Get(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12229 | } |
| 12230 | } |
| 12231 | |
| 12232 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12233 | int Map::Hash() { |
| 12234 | // For performance reasons we only hash the 3 most variable fields of a map: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12235 | // constructor, prototype and bit_field2. For predictability reasons we |
| 12236 | // use objects' offsets in respective pages for hashing instead of raw |
| 12237 | // addresses. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12238 | |
| 12239 | // Shift away the tag. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12240 | int hash = ObjectAddressForHashing(GetConstructor()) >> 2; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12241 | |
| 12242 | // XOR-ing the prototype and constructor directly yields too many zero bits |
| 12243 | // when the two pointers are close (which is fairly common). |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12244 | // To avoid this we shift the prototype bits relatively to the constructor. |
| 12245 | hash ^= ObjectAddressForHashing(prototype()) << (32 - kPageSizeBits); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12246 | |
| 12247 | return hash ^ (hash >> 16) ^ bit_field2(); |
| 12248 | } |
| 12249 | |
| 12250 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12251 | namespace { |
| 12252 | |
| 12253 | bool CheckEquivalent(Map* first, Map* second) { |
| 12254 | return first->GetConstructor() == second->GetConstructor() && |
| 12255 | first->prototype() == second->prototype() && |
| 12256 | first->instance_type() == second->instance_type() && |
| 12257 | first->bit_field() == second->bit_field() && |
| 12258 | first->is_extensible() == second->is_extensible() && |
| 12259 | first->is_strong() == second->is_strong() && |
| 12260 | first->is_hidden_prototype() == second->is_hidden_prototype(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12261 | } |
| 12262 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12263 | } // namespace |
| 12264 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12265 | |
| 12266 | bool Map::EquivalentToForTransition(Map* other) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12267 | if (!CheckEquivalent(this, other)) return false; |
| 12268 | if (instance_type() == JS_FUNCTION_TYPE) { |
| 12269 | // JSFunctions require more checks to ensure that sloppy function is |
| 12270 | // not equvalent to strict function. |
| 12271 | int nof = Min(NumberOfOwnDescriptors(), other->NumberOfOwnDescriptors()); |
| 12272 | return instance_descriptors()->IsEqualUpTo(other->instance_descriptors(), |
| 12273 | nof); |
| 12274 | } |
| 12275 | return true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12276 | } |
| 12277 | |
| 12278 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12279 | bool Map::EquivalentToForNormalization(Map* other, |
| 12280 | PropertyNormalizationMode mode) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12281 | int properties = |
| 12282 | mode == CLEAR_INOBJECT_PROPERTIES ? 0 : other->GetInObjectProperties(); |
| 12283 | return CheckEquivalent(this, other) && bit_field2() == other->bit_field2() && |
| 12284 | GetInObjectProperties() == properties; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12285 | } |
| 12286 | |
| 12287 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12288 | bool JSFunction::Inlines(SharedFunctionInfo* candidate) { |
| 12289 | DisallowHeapAllocation no_gc; |
| 12290 | if (shared() == candidate) return true; |
| 12291 | if (code()->kind() != Code::OPTIMIZED_FUNCTION) return false; |
| 12292 | DeoptimizationInputData* const data = |
| 12293 | DeoptimizationInputData::cast(code()->deoptimization_data()); |
| 12294 | if (data->length() == 0) return false; |
| 12295 | FixedArray* const literals = data->LiteralArray(); |
| 12296 | int const inlined_count = data->InlinedFunctionCount()->value(); |
| 12297 | for (int i = 0; i < inlined_count; ++i) { |
| 12298 | if (SharedFunctionInfo::cast(literals->get(i)) == candidate) { |
| 12299 | return true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12300 | } |
| 12301 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12302 | return false; |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 12303 | } |
| 12304 | |
| 12305 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12306 | void JSFunction::MarkForOptimization() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12307 | Isolate* isolate = GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12308 | // Do not optimize if function contains break points. |
| 12309 | if (shared()->HasDebugInfo()) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12310 | DCHECK(!IsOptimized()); |
| 12311 | DCHECK(shared()->allows_lazy_compilation() || |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12312 | !shared()->optimization_disabled()); |
| 12313 | DCHECK(!shared()->HasDebugInfo()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12314 | set_code_no_write_barrier( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12315 | isolate->builtins()->builtin(Builtins::kCompileOptimized)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12316 | // No write barrier required, since the builtin is part of the root set. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12317 | } |
| 12318 | |
| 12319 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12320 | void JSFunction::AttemptConcurrentOptimization() { |
| 12321 | Isolate* isolate = GetIsolate(); |
| 12322 | if (!isolate->concurrent_recompilation_enabled() || |
| 12323 | isolate->bootstrapper()->IsActive()) { |
| 12324 | MarkForOptimization(); |
| 12325 | return; |
| 12326 | } |
| 12327 | if (isolate->concurrent_osr_enabled() && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12328 | isolate->optimizing_compile_dispatcher()->IsQueuedForOSR(this)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12329 | // Do not attempt regular recompilation if we already queued this for OSR. |
| 12330 | // TODO(yangguo): This is necessary so that we don't install optimized |
| 12331 | // code on a function that is already optimized, since OSR and regular |
| 12332 | // recompilation race. This goes away as soon as OSR becomes one-shot. |
| 12333 | return; |
| 12334 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12335 | DCHECK(!IsInOptimizationQueue()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12336 | DCHECK(!IsOptimized()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12337 | DCHECK(shared()->allows_lazy_compilation() || |
| 12338 | !shared()->optimization_disabled()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12339 | DCHECK(isolate->concurrent_recompilation_enabled()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12340 | if (FLAG_trace_concurrent_recompilation) { |
| 12341 | PrintF(" ** Marking "); |
| 12342 | ShortPrint(); |
| 12343 | PrintF(" for concurrent recompilation.\n"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12344 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12345 | set_code_no_write_barrier( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12346 | isolate->builtins()->builtin(Builtins::kCompileOptimizedConcurrent)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12347 | // No write barrier required, since the builtin is part of the root set. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12348 | } |
| 12349 | |
| 12350 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12351 | void SharedFunctionInfo::AddSharedCodeToOptimizedCodeMap( |
| 12352 | Handle<SharedFunctionInfo> shared, Handle<Code> code) { |
| 12353 | Isolate* isolate = shared->GetIsolate(); |
| 12354 | if (isolate->serializer_enabled()) return; |
| 12355 | DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION); |
| 12356 | // Empty code maps are unsupported. |
| 12357 | if (!shared->OptimizedCodeMapIsCleared()) { |
| 12358 | Handle<WeakCell> cell = isolate->factory()->NewWeakCell(code); |
| 12359 | // A collection may have occured and cleared the optimized code map in the |
| 12360 | // allocation above. |
| 12361 | if (!shared->OptimizedCodeMapIsCleared()) { |
| 12362 | shared->optimized_code_map()->set(kSharedCodeIndex, *cell); |
| 12363 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12364 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12365 | } |
| 12366 | |
| 12367 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12368 | void SharedFunctionInfo::AddToOptimizedCodeMapInternal( |
| 12369 | Handle<SharedFunctionInfo> shared, Handle<Context> native_context, |
| 12370 | Handle<HeapObject> code, Handle<LiteralsArray> literals, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12371 | BailoutId osr_ast_id) { |
| 12372 | Isolate* isolate = shared->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12373 | if (isolate->serializer_enabled()) return; |
| 12374 | DCHECK(*code == isolate->heap()->undefined_value() || |
| 12375 | !shared->SearchOptimizedCodeMap(*native_context, osr_ast_id).code); |
| 12376 | DCHECK(*code == isolate->heap()->undefined_value() || |
| 12377 | Code::cast(*code)->kind() == Code::OPTIMIZED_FUNCTION); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12378 | DCHECK(native_context->IsNativeContext()); |
| 12379 | STATIC_ASSERT(kEntryLength == 4); |
| 12380 | Handle<FixedArray> new_code_map; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12381 | int entry; |
| 12382 | |
| 12383 | if (shared->OptimizedCodeMapIsCleared()) { |
| 12384 | new_code_map = isolate->factory()->NewFixedArray(kInitialLength, TENURED); |
| 12385 | new_code_map->set(kSharedCodeIndex, *isolate->factory()->empty_weak_cell(), |
| 12386 | SKIP_WRITE_BARRIER); |
| 12387 | entry = kEntriesStart; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12388 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12389 | Handle<FixedArray> old_code_map(shared->optimized_code_map(), isolate); |
| 12390 | entry = shared->SearchOptimizedCodeMapEntry(*native_context, osr_ast_id); |
| 12391 | if (entry > kSharedCodeIndex) { |
| 12392 | // Found an existing context-specific entry. If the user provided valid |
| 12393 | // code, it must not contain any code. |
| 12394 | DCHECK(code->IsUndefined() || |
| 12395 | WeakCell::cast(old_code_map->get(entry + kCachedCodeOffset)) |
| 12396 | ->cleared()); |
| 12397 | |
| 12398 | // Just set the code and literals to the entry. |
| 12399 | if (!code->IsUndefined()) { |
| 12400 | Handle<WeakCell> code_cell = isolate->factory()->NewWeakCell(code); |
| 12401 | old_code_map->set(entry + kCachedCodeOffset, *code_cell); |
| 12402 | } |
| 12403 | Handle<WeakCell> literals_cell = |
| 12404 | isolate->factory()->NewWeakCell(literals); |
| 12405 | old_code_map->set(entry + kLiteralsOffset, *literals_cell); |
| 12406 | return; |
| 12407 | } |
| 12408 | |
| 12409 | // Can we reuse an entry? |
| 12410 | DCHECK(entry < kEntriesStart); |
| 12411 | int length = old_code_map->length(); |
| 12412 | for (int i = kEntriesStart; i < length; i += kEntryLength) { |
| 12413 | if (WeakCell::cast(old_code_map->get(i + kContextOffset))->cleared()) { |
| 12414 | new_code_map = old_code_map; |
| 12415 | entry = i; |
| 12416 | break; |
| 12417 | } |
| 12418 | } |
| 12419 | |
| 12420 | if (entry < kEntriesStart) { |
| 12421 | // Copy old optimized code map and append one new entry. |
| 12422 | new_code_map = isolate->factory()->CopyFixedArrayAndGrow( |
| 12423 | old_code_map, kEntryLength, TENURED); |
| 12424 | // TODO(mstarzinger): Temporary workaround. The allocation above might |
| 12425 | // have flushed the optimized code map and the copy we created is full of |
| 12426 | // holes. For now we just give up on adding the entry and pretend it got |
| 12427 | // flushed. |
| 12428 | if (shared->OptimizedCodeMapIsCleared()) return; |
| 12429 | entry = old_code_map->length(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12430 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12431 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12432 | |
| 12433 | Handle<WeakCell> code_cell = code->IsUndefined() |
| 12434 | ? isolate->factory()->empty_weak_cell() |
| 12435 | : isolate->factory()->NewWeakCell(code); |
| 12436 | Handle<WeakCell> literals_cell = isolate->factory()->NewWeakCell(literals); |
| 12437 | WeakCell* context_cell = native_context->self_weak_cell(); |
| 12438 | |
| 12439 | new_code_map->set(entry + kContextOffset, context_cell); |
| 12440 | new_code_map->set(entry + kCachedCodeOffset, *code_cell); |
| 12441 | new_code_map->set(entry + kLiteralsOffset, *literals_cell); |
| 12442 | new_code_map->set(entry + kOsrAstIdOffset, Smi::FromInt(osr_ast_id.ToInt())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12443 | |
| 12444 | #ifdef DEBUG |
| 12445 | for (int i = kEntriesStart; i < new_code_map->length(); i += kEntryLength) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12446 | WeakCell* cell = WeakCell::cast(new_code_map->get(i + kContextOffset)); |
| 12447 | DCHECK(cell->cleared() || cell->value()->IsNativeContext()); |
| 12448 | cell = WeakCell::cast(new_code_map->get(i + kCachedCodeOffset)); |
| 12449 | DCHECK(cell->cleared() || |
| 12450 | (cell->value()->IsCode() && |
| 12451 | Code::cast(cell->value())->kind() == Code::OPTIMIZED_FUNCTION)); |
| 12452 | cell = WeakCell::cast(new_code_map->get(i + kLiteralsOffset)); |
| 12453 | DCHECK(cell->cleared() || cell->value()->IsFixedArray()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12454 | DCHECK(new_code_map->get(i + kOsrAstIdOffset)->IsSmi()); |
| 12455 | } |
| 12456 | #endif |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12457 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12458 | FixedArray* old_code_map = shared->optimized_code_map(); |
| 12459 | if (old_code_map != *new_code_map) { |
| 12460 | shared->set_optimized_code_map(*new_code_map); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12461 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12462 | } |
| 12463 | |
| 12464 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12465 | void SharedFunctionInfo::ClearOptimizedCodeMap() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12466 | FixedArray* cleared_map = GetHeap()->cleared_optimized_code_map(); |
| 12467 | set_optimized_code_map(cleared_map, SKIP_WRITE_BARRIER); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12468 | } |
| 12469 | |
| 12470 | |
| 12471 | void SharedFunctionInfo::EvictFromOptimizedCodeMap(Code* optimized_code, |
| 12472 | const char* reason) { |
| 12473 | DisallowHeapAllocation no_gc; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12474 | if (OptimizedCodeMapIsCleared()) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12475 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12476 | Heap* heap = GetHeap(); |
| 12477 | FixedArray* code_map = optimized_code_map(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12478 | int dst = kEntriesStart; |
| 12479 | int length = code_map->length(); |
| 12480 | for (int src = kEntriesStart; src < length; src += kEntryLength) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12481 | DCHECK(WeakCell::cast(code_map->get(src))->cleared() || |
| 12482 | WeakCell::cast(code_map->get(src))->value()->IsNativeContext()); |
| 12483 | if (WeakCell::cast(code_map->get(src + kCachedCodeOffset))->value() == |
| 12484 | optimized_code) { |
| 12485 | BailoutId osr(Smi::cast(code_map->get(src + kOsrAstIdOffset))->value()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12486 | if (FLAG_trace_opt) { |
| 12487 | PrintF("[evicting entry from optimizing code map (%s) for ", reason); |
| 12488 | ShortPrint(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12489 | if (osr.IsNone()) { |
| 12490 | PrintF("]\n"); |
| 12491 | } else { |
| 12492 | PrintF(" (osr ast id %d)]\n", osr.ToInt()); |
| 12493 | } |
| 12494 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12495 | if (!osr.IsNone()) { |
| 12496 | // Evict the src entry by not copying it to the dst entry. |
| 12497 | continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12498 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12499 | // In case of non-OSR entry just clear the code in order to proceed |
| 12500 | // sharing literals. |
| 12501 | code_map->set(src + kCachedCodeOffset, heap->empty_weak_cell(), |
| 12502 | SKIP_WRITE_BARRIER); |
| 12503 | } |
| 12504 | |
| 12505 | // Keep the src entry by copying it to the dst entry. |
| 12506 | if (dst != src) { |
| 12507 | code_map->set(dst + kContextOffset, code_map->get(src + kContextOffset)); |
| 12508 | code_map->set(dst + kCachedCodeOffset, |
| 12509 | code_map->get(src + kCachedCodeOffset)); |
| 12510 | code_map->set(dst + kLiteralsOffset, |
| 12511 | code_map->get(src + kLiteralsOffset)); |
| 12512 | code_map->set(dst + kOsrAstIdOffset, |
| 12513 | code_map->get(src + kOsrAstIdOffset)); |
| 12514 | } |
| 12515 | dst += kEntryLength; |
| 12516 | } |
| 12517 | if (WeakCell::cast(code_map->get(kSharedCodeIndex))->value() == |
| 12518 | optimized_code) { |
| 12519 | // Evict context-independent code as well. |
| 12520 | code_map->set(kSharedCodeIndex, heap->empty_weak_cell(), |
| 12521 | SKIP_WRITE_BARRIER); |
| 12522 | if (FLAG_trace_opt) { |
| 12523 | PrintF("[evicting entry from optimizing code map (%s) for ", reason); |
| 12524 | ShortPrint(); |
| 12525 | PrintF(" (context-independent code)]\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12526 | } |
| 12527 | } |
| 12528 | if (dst != length) { |
| 12529 | // Always trim even when array is cleared because of heap verifier. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12530 | heap->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>(code_map, |
| 12531 | length - dst); |
| 12532 | if (code_map->length() == kEntriesStart && |
| 12533 | WeakCell::cast(code_map->get(kSharedCodeIndex))->cleared()) { |
| 12534 | ClearOptimizedCodeMap(); |
| 12535 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12536 | } |
| 12537 | } |
| 12538 | |
| 12539 | |
| 12540 | void SharedFunctionInfo::TrimOptimizedCodeMap(int shrink_by) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12541 | FixedArray* code_map = optimized_code_map(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12542 | DCHECK(shrink_by % kEntryLength == 0); |
| 12543 | DCHECK(shrink_by <= code_map->length() - kEntriesStart); |
| 12544 | // Always trim even when array is cleared because of heap verifier. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12545 | GetHeap()->RightTrimFixedArray<Heap::SEQUENTIAL_TO_SWEEPER>(code_map, |
| 12546 | shrink_by); |
| 12547 | if (code_map->length() == kEntriesStart && |
| 12548 | WeakCell::cast(code_map->get(kSharedCodeIndex))->cleared()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12549 | ClearOptimizedCodeMap(); |
| 12550 | } |
| 12551 | } |
| 12552 | |
| 12553 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12554 | static void GetMinInobjectSlack(Map* map, void* data) { |
| 12555 | int slack = map->unused_property_fields(); |
| 12556 | if (*reinterpret_cast<int*>(data) > slack) { |
| 12557 | *reinterpret_cast<int*>(data) = slack; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12558 | } |
| 12559 | } |
| 12560 | |
| 12561 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12562 | static void ShrinkInstanceSize(Map* map, void* data) { |
| 12563 | int slack = *reinterpret_cast<int*>(data); |
| 12564 | map->SetInObjectProperties(map->GetInObjectProperties() - slack); |
| 12565 | map->set_unused_property_fields(map->unused_property_fields() - slack); |
| 12566 | map->set_instance_size(map->instance_size() - slack * kPointerSize); |
| 12567 | |
| 12568 | // Visitor id might depend on the instance size, recalculate it. |
| 12569 | map->set_visitor_id(Heap::GetStaticVisitorIdForMap(map)); |
| 12570 | } |
| 12571 | |
| 12572 | |
| 12573 | void Map::CompleteInobjectSlackTracking() { |
| 12574 | // Has to be an initial map. |
| 12575 | DCHECK(GetBackPointer()->IsUndefined()); |
| 12576 | |
| 12577 | set_construction_counter(kNoSlackTracking); |
| 12578 | |
| 12579 | int slack = unused_property_fields(); |
| 12580 | TransitionArray::TraverseTransitionTree(this, &GetMinInobjectSlack, &slack); |
| 12581 | if (slack != 0) { |
| 12582 | // Resize the initial map and all maps in its transition tree. |
| 12583 | TransitionArray::TraverseTransitionTree(this, &ShrinkInstanceSize, &slack); |
| 12584 | } |
| 12585 | } |
| 12586 | |
| 12587 | |
| 12588 | static bool PrototypeBenefitsFromNormalization(Handle<JSObject> object) { |
| 12589 | DisallowHeapAllocation no_gc; |
| 12590 | if (!object->HasFastProperties()) return false; |
| 12591 | Map* map = object->map(); |
| 12592 | if (map->is_prototype_map()) return false; |
| 12593 | DescriptorArray* descriptors = map->instance_descriptors(); |
| 12594 | for (int i = 0; i < map->NumberOfOwnDescriptors(); i++) { |
| 12595 | PropertyDetails details = descriptors->GetDetails(i); |
| 12596 | if (details.location() == kDescriptor) continue; |
| 12597 | if (details.representation().IsHeapObject() || |
| 12598 | details.representation().IsTagged()) { |
| 12599 | FieldIndex index = FieldIndex::ForDescriptor(map, i); |
| 12600 | if (object->RawFastPropertyAt(index)->IsJSFunction()) return true; |
| 12601 | } |
| 12602 | } |
| 12603 | return false; |
| 12604 | } |
| 12605 | |
| 12606 | |
| 12607 | // static |
| 12608 | void JSObject::OptimizeAsPrototype(Handle<JSObject> object, |
| 12609 | PrototypeOptimizationMode mode) { |
| 12610 | if (object->IsJSGlobalObject()) return; |
| 12611 | if (mode == FAST_PROTOTYPE && PrototypeBenefitsFromNormalization(object)) { |
| 12612 | // First normalize to ensure all JSFunctions are DATA_CONSTANT. |
| 12613 | JSObject::NormalizeProperties(object, KEEP_INOBJECT_PROPERTIES, 0, |
| 12614 | "NormalizeAsPrototype"); |
| 12615 | } |
| 12616 | Handle<Map> previous_map(object->map()); |
| 12617 | if (!object->HasFastProperties()) { |
| 12618 | JSObject::MigrateSlowToFast(object, 0, "OptimizeAsPrototype"); |
| 12619 | } |
| 12620 | if (!object->map()->is_prototype_map()) { |
| 12621 | if (object->map() == *previous_map) { |
| 12622 | Handle<Map> new_map = Map::Copy(handle(object->map()), "CopyAsPrototype"); |
| 12623 | JSObject::MigrateToMap(object, new_map); |
| 12624 | } |
| 12625 | object->map()->set_is_prototype_map(true); |
| 12626 | |
| 12627 | // Replace the pointer to the exact constructor with the Object function |
| 12628 | // from the same context if undetectable from JS. This is to avoid keeping |
| 12629 | // memory alive unnecessarily. |
| 12630 | Object* maybe_constructor = object->map()->GetConstructor(); |
| 12631 | if (maybe_constructor->IsJSFunction()) { |
| 12632 | JSFunction* constructor = JSFunction::cast(maybe_constructor); |
| 12633 | Isolate* isolate = object->GetIsolate(); |
| 12634 | if (!constructor->shared()->IsApiFunction() && |
| 12635 | object->class_name() == isolate->heap()->Object_string()) { |
| 12636 | Context* context = constructor->context()->native_context(); |
| 12637 | JSFunction* object_function = context->object_function(); |
| 12638 | object->map()->SetConstructor(object_function); |
| 12639 | } |
| 12640 | } |
| 12641 | } |
| 12642 | } |
| 12643 | |
| 12644 | |
| 12645 | // static |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12646 | void JSObject::ReoptimizeIfPrototype(Handle<JSObject> object) { |
| 12647 | if (!object->map()->is_prototype_map()) return; |
| 12648 | OptimizeAsPrototype(object, FAST_PROTOTYPE); |
| 12649 | } |
| 12650 | |
| 12651 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12652 | // static |
| 12653 | void JSObject::LazyRegisterPrototypeUser(Handle<Map> user, Isolate* isolate) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12654 | DCHECK(FLAG_track_prototype_users); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12655 | // Contract: In line with InvalidatePrototypeChains()'s requirements, |
| 12656 | // leaf maps don't need to register as users, only prototypes do. |
| 12657 | DCHECK(user->is_prototype_map()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12658 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12659 | Handle<Map> current_user = user; |
| 12660 | Handle<PrototypeInfo> current_user_info = |
| 12661 | Map::GetOrCreatePrototypeInfo(user, isolate); |
| 12662 | for (PrototypeIterator iter(user); !iter.IsAtEnd(); iter.Advance()) { |
| 12663 | // Walk up the prototype chain as far as links haven't been registered yet. |
| 12664 | if (current_user_info->registry_slot() != PrototypeInfo::UNREGISTERED) { |
| 12665 | break; |
| 12666 | } |
| 12667 | Handle<Object> maybe_proto = PrototypeIterator::GetCurrent(iter); |
| 12668 | // Proxies on the prototype chain are not supported. They make it |
| 12669 | // impossible to make any assumptions about the prototype chain anyway. |
| 12670 | if (maybe_proto->IsJSProxy()) return; |
| 12671 | Handle<JSObject> proto = Handle<JSObject>::cast(maybe_proto); |
| 12672 | Handle<PrototypeInfo> proto_info = |
| 12673 | Map::GetOrCreatePrototypeInfo(proto, isolate); |
| 12674 | Handle<Object> maybe_registry(proto_info->prototype_users(), isolate); |
| 12675 | int slot = 0; |
| 12676 | Handle<WeakFixedArray> new_array = |
| 12677 | WeakFixedArray::Add(maybe_registry, current_user, &slot); |
| 12678 | current_user_info->set_registry_slot(slot); |
| 12679 | if (!maybe_registry.is_identical_to(new_array)) { |
| 12680 | proto_info->set_prototype_users(*new_array); |
| 12681 | } |
| 12682 | if (FLAG_trace_prototype_users) { |
| 12683 | PrintF("Registering %p as a user of prototype %p (map=%p).\n", |
| 12684 | reinterpret_cast<void*>(*current_user), |
| 12685 | reinterpret_cast<void*>(*proto), |
| 12686 | reinterpret_cast<void*>(proto->map())); |
| 12687 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12688 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12689 | current_user = handle(proto->map(), isolate); |
| 12690 | current_user_info = proto_info; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12691 | } |
| 12692 | } |
| 12693 | |
| 12694 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12695 | // Can be called regardless of whether |user| was actually registered with |
| 12696 | // |prototype|. Returns true when there was a registration. |
| 12697 | // static |
| 12698 | bool JSObject::UnregisterPrototypeUser(Handle<Map> user, Isolate* isolate) { |
| 12699 | DCHECK(user->is_prototype_map()); |
| 12700 | // If it doesn't have a PrototypeInfo, it was never registered. |
| 12701 | if (!user->prototype_info()->IsPrototypeInfo()) return false; |
| 12702 | // If it had no prototype before, see if it had users that might expect |
| 12703 | // registration. |
| 12704 | if (!user->prototype()->IsJSObject()) { |
| 12705 | Object* users = |
| 12706 | PrototypeInfo::cast(user->prototype_info())->prototype_users(); |
| 12707 | return users->IsWeakFixedArray(); |
| 12708 | } |
| 12709 | Handle<JSObject> prototype(JSObject::cast(user->prototype()), isolate); |
| 12710 | Handle<PrototypeInfo> user_info = |
| 12711 | Map::GetOrCreatePrototypeInfo(user, isolate); |
| 12712 | int slot = user_info->registry_slot(); |
| 12713 | if (slot == PrototypeInfo::UNREGISTERED) return false; |
| 12714 | DCHECK(prototype->map()->is_prototype_map()); |
| 12715 | Object* maybe_proto_info = prototype->map()->prototype_info(); |
| 12716 | // User knows its registry slot, prototype info and user registry must exist. |
| 12717 | DCHECK(maybe_proto_info->IsPrototypeInfo()); |
| 12718 | Handle<PrototypeInfo> proto_info(PrototypeInfo::cast(maybe_proto_info), |
| 12719 | isolate); |
| 12720 | Object* maybe_registry = proto_info->prototype_users(); |
| 12721 | DCHECK(maybe_registry->IsWeakFixedArray()); |
| 12722 | DCHECK(WeakFixedArray::cast(maybe_registry)->Get(slot) == *user); |
| 12723 | WeakFixedArray::cast(maybe_registry)->Clear(slot); |
| 12724 | if (FLAG_trace_prototype_users) { |
| 12725 | PrintF("Unregistering %p as a user of prototype %p.\n", |
| 12726 | reinterpret_cast<void*>(*user), reinterpret_cast<void*>(*prototype)); |
| 12727 | } |
| 12728 | return true; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12729 | } |
| 12730 | |
| 12731 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12732 | static void InvalidatePrototypeChainsInternal(Map* map) { |
| 12733 | if (!map->is_prototype_map()) return; |
| 12734 | if (FLAG_trace_prototype_users) { |
| 12735 | PrintF("Invalidating prototype map %p 's cell\n", |
| 12736 | reinterpret_cast<void*>(map)); |
| 12737 | } |
| 12738 | Object* maybe_proto_info = map->prototype_info(); |
| 12739 | if (!maybe_proto_info->IsPrototypeInfo()) return; |
| 12740 | PrototypeInfo* proto_info = PrototypeInfo::cast(maybe_proto_info); |
| 12741 | Object* maybe_cell = proto_info->validity_cell(); |
| 12742 | if (maybe_cell->IsCell()) { |
| 12743 | // Just set the value; the cell will be replaced lazily. |
| 12744 | Cell* cell = Cell::cast(maybe_cell); |
| 12745 | cell->set_value(Smi::FromInt(Map::kPrototypeChainInvalid)); |
| 12746 | } |
| 12747 | |
| 12748 | WeakFixedArray::Iterator iterator(proto_info->prototype_users()); |
| 12749 | // For now, only maps register themselves as users. |
| 12750 | Map* user; |
| 12751 | while ((user = iterator.Next<Map>())) { |
| 12752 | // Walk the prototype chain (backwards, towards leaf objects) if necessary. |
| 12753 | InvalidatePrototypeChainsInternal(user); |
| 12754 | } |
| 12755 | } |
| 12756 | |
| 12757 | |
| 12758 | // static |
| 12759 | void JSObject::InvalidatePrototypeChains(Map* map) { |
| 12760 | if (!FLAG_eliminate_prototype_chain_checks) return; |
| 12761 | DisallowHeapAllocation no_gc; |
| 12762 | InvalidatePrototypeChainsInternal(map); |
| 12763 | } |
| 12764 | |
| 12765 | |
| 12766 | // static |
| 12767 | Handle<PrototypeInfo> Map::GetOrCreatePrototypeInfo(Handle<JSObject> prototype, |
| 12768 | Isolate* isolate) { |
| 12769 | Object* maybe_proto_info = prototype->map()->prototype_info(); |
| 12770 | if (maybe_proto_info->IsPrototypeInfo()) { |
| 12771 | return handle(PrototypeInfo::cast(maybe_proto_info), isolate); |
| 12772 | } |
| 12773 | Handle<PrototypeInfo> proto_info = isolate->factory()->NewPrototypeInfo(); |
| 12774 | prototype->map()->set_prototype_info(*proto_info); |
| 12775 | return proto_info; |
| 12776 | } |
| 12777 | |
| 12778 | |
| 12779 | // static |
| 12780 | Handle<PrototypeInfo> Map::GetOrCreatePrototypeInfo(Handle<Map> prototype_map, |
| 12781 | Isolate* isolate) { |
| 12782 | Object* maybe_proto_info = prototype_map->prototype_info(); |
| 12783 | if (maybe_proto_info->IsPrototypeInfo()) { |
| 12784 | return handle(PrototypeInfo::cast(maybe_proto_info), isolate); |
| 12785 | } |
| 12786 | Handle<PrototypeInfo> proto_info = isolate->factory()->NewPrototypeInfo(); |
| 12787 | prototype_map->set_prototype_info(*proto_info); |
| 12788 | return proto_info; |
| 12789 | } |
| 12790 | |
| 12791 | |
| 12792 | // static |
| 12793 | Handle<Cell> Map::GetOrCreatePrototypeChainValidityCell(Handle<Map> map, |
| 12794 | Isolate* isolate) { |
| 12795 | Handle<Object> maybe_prototype(map->prototype(), isolate); |
| 12796 | if (!maybe_prototype->IsJSObject()) return Handle<Cell>::null(); |
| 12797 | Handle<JSObject> prototype = Handle<JSObject>::cast(maybe_prototype); |
| 12798 | // Ensure the prototype is registered with its own prototypes so its cell |
| 12799 | // will be invalidated when necessary. |
| 12800 | JSObject::LazyRegisterPrototypeUser(handle(prototype->map(), isolate), |
| 12801 | isolate); |
| 12802 | Handle<PrototypeInfo> proto_info = |
| 12803 | GetOrCreatePrototypeInfo(prototype, isolate); |
| 12804 | Object* maybe_cell = proto_info->validity_cell(); |
| 12805 | // Return existing cell if it's still valid. |
| 12806 | if (maybe_cell->IsCell()) { |
| 12807 | Handle<Cell> cell(Cell::cast(maybe_cell), isolate); |
| 12808 | if (cell->value() == Smi::FromInt(Map::kPrototypeChainValid)) { |
| 12809 | return cell; |
| 12810 | } |
| 12811 | } |
| 12812 | // Otherwise create a new cell. |
| 12813 | Handle<Cell> cell = isolate->factory()->NewCell( |
| 12814 | handle(Smi::FromInt(Map::kPrototypeChainValid), isolate)); |
| 12815 | proto_info->set_validity_cell(*cell); |
| 12816 | return cell; |
| 12817 | } |
| 12818 | |
| 12819 | |
| 12820 | // static |
| 12821 | void Map::SetPrototype(Handle<Map> map, Handle<Object> prototype, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12822 | PrototypeOptimizationMode proto_mode) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12823 | if (prototype->IsJSObject()) { |
| 12824 | Handle<JSObject> prototype_jsobj = Handle<JSObject>::cast(prototype); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12825 | JSObject::OptimizeAsPrototype(prototype_jsobj, proto_mode); |
| 12826 | } |
| 12827 | WriteBarrierMode wb_mode = |
| 12828 | prototype->IsNull() ? SKIP_WRITE_BARRIER : UPDATE_WRITE_BARRIER; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12829 | map->set_prototype(*prototype, wb_mode); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12830 | } |
| 12831 | |
| 12832 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12833 | Handle<Object> CacheInitialJSArrayMaps( |
| 12834 | Handle<Context> native_context, Handle<Map> initial_map) { |
| 12835 | // Replace all of the cached initial array maps in the native context with |
| 12836 | // the appropriate transitioned elements kind maps. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12837 | Strength strength = |
| 12838 | initial_map->is_strong() ? Strength::STRONG : Strength::WEAK; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12839 | Handle<Map> current_map = initial_map; |
| 12840 | ElementsKind kind = current_map->elements_kind(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12841 | DCHECK_EQ(GetInitialFastElementsKind(), kind); |
| 12842 | native_context->set(Context::ArrayMapIndex(kind, strength), *current_map); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12843 | for (int i = GetSequenceIndexFromFastElementsKind(kind) + 1; |
| 12844 | i < kFastElementsKindCount; ++i) { |
| 12845 | Handle<Map> new_map; |
| 12846 | ElementsKind next_kind = GetFastElementsKindFromSequenceIndex(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12847 | if (Map* maybe_elements_transition = current_map->ElementsTransitionMap()) { |
| 12848 | new_map = handle(maybe_elements_transition); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12849 | } else { |
| 12850 | new_map = Map::CopyAsElementsKind( |
| 12851 | current_map, next_kind, INSERT_TRANSITION); |
| 12852 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12853 | DCHECK_EQ(next_kind, new_map->elements_kind()); |
| 12854 | native_context->set(Context::ArrayMapIndex(next_kind, strength), *new_map); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12855 | current_map = new_map; |
| 12856 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12857 | return initial_map; |
| 12858 | } |
| 12859 | |
| 12860 | |
| 12861 | void JSFunction::SetInstancePrototype(Handle<JSFunction> function, |
| 12862 | Handle<Object> value) { |
| 12863 | Isolate* isolate = function->GetIsolate(); |
| 12864 | |
| 12865 | DCHECK(value->IsJSReceiver()); |
| 12866 | |
| 12867 | // Now some logic for the maps of the objects that are created by using this |
| 12868 | // function as a constructor. |
| 12869 | if (function->has_initial_map()) { |
| 12870 | // If the function has allocated the initial map replace it with a |
| 12871 | // copy containing the new prototype. Also complete any in-object |
| 12872 | // slack tracking that is in progress at this point because it is |
| 12873 | // still tracking the old copy. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12874 | function->CompleteInobjectSlackTrackingIfActive(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12875 | |
| 12876 | Handle<Map> initial_map(function->initial_map(), isolate); |
| 12877 | |
| 12878 | if (!initial_map->GetIsolate()->bootstrapper()->IsActive() && |
| 12879 | initial_map->instance_type() == JS_OBJECT_TYPE) { |
| 12880 | // Put the value in the initial map field until an initial map is needed. |
| 12881 | // At that point, a new initial map is created and the prototype is put |
| 12882 | // into the initial map where it belongs. |
| 12883 | function->set_prototype_or_initial_map(*value); |
| 12884 | } else { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12885 | Handle<Map> new_map = Map::Copy(initial_map, "SetInstancePrototype"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12886 | if (function->map()->is_strong()) { |
| 12887 | new_map->set_is_strong(); |
| 12888 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12889 | JSFunction::SetInitialMap(function, new_map, value); |
| 12890 | |
| 12891 | // If the function is used as the global Array function, cache the |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12892 | // updated initial maps (and transitioned versions) in the native context. |
| 12893 | Handle<Context> native_context(function->context()->native_context(), |
| 12894 | isolate); |
| 12895 | Handle<Object> array_function( |
| 12896 | native_context->get(Context::ARRAY_FUNCTION_INDEX), isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12897 | if (array_function->IsJSFunction() && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12898 | *function == JSFunction::cast(*array_function)) { |
| 12899 | CacheInitialJSArrayMaps(native_context, new_map); |
| 12900 | Handle<Map> new_strong_map = Map::Copy(new_map, "SetInstancePrototype"); |
| 12901 | new_strong_map->set_is_strong(); |
| 12902 | CacheInitialJSArrayMaps(native_context, new_strong_map); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12903 | } |
| 12904 | } |
| 12905 | |
| 12906 | // Deoptimize all code that embeds the previous initial map. |
| 12907 | initial_map->dependent_code()->DeoptimizeDependentCodeGroup( |
| 12908 | isolate, DependentCode::kInitialMapChangedGroup); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12909 | } else { |
| 12910 | // Put the value in the initial map field until an initial map is |
| 12911 | // needed. At that point, a new initial map is created and the |
| 12912 | // prototype is put into the initial map where it belongs. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12913 | function->set_prototype_or_initial_map(*value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12914 | if (value->IsJSObject()) { |
| 12915 | // Optimize as prototype to detach it from its transition tree. |
| 12916 | JSObject::OptimizeAsPrototype(Handle<JSObject>::cast(value), |
| 12917 | FAST_PROTOTYPE); |
| 12918 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12919 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12920 | isolate->heap()->ClearInstanceofCache(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12921 | } |
| 12922 | |
| 12923 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12924 | void JSFunction::SetPrototype(Handle<JSFunction> function, |
| 12925 | Handle<Object> value) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12926 | DCHECK(function->IsConstructor()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12927 | Handle<Object> construct_prototype = value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12928 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12929 | // If the value is not a JSReceiver, store the value in the map's |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12930 | // constructor field so it can be accessed. Also, set the prototype |
| 12931 | // used for constructing objects to the original object prototype. |
| 12932 | // See ECMA-262 13.2.2. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12933 | if (!value->IsJSReceiver()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12934 | // Copy the map so this does not affect unrelated functions. |
| 12935 | // Remove map transitions because they point to maps with a |
| 12936 | // different prototype. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12937 | Handle<Map> new_map = Map::Copy(handle(function->map()), "SetPrototype"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12938 | |
| 12939 | JSObject::MigrateToMap(function, new_map); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12940 | new_map->SetConstructor(*value); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12941 | new_map->set_non_instance_prototype(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12942 | Isolate* isolate = new_map->GetIsolate(); |
| 12943 | construct_prototype = handle( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12944 | function->context()->native_context()->initial_object_prototype(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12945 | isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12946 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12947 | function->map()->set_non_instance_prototype(false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12948 | } |
| 12949 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12950 | return SetInstancePrototype(function, construct_prototype); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12951 | } |
| 12952 | |
| 12953 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12954 | bool JSFunction::RemovePrototype() { |
| 12955 | Context* native_context = context()->native_context(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12956 | Map* no_prototype_map = |
| 12957 | is_strict(shared()->language_mode()) |
| 12958 | ? native_context->strict_function_without_prototype_map() |
| 12959 | : native_context->sloppy_function_without_prototype_map(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12960 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12961 | if (map() == no_prototype_map) return true; |
| 12962 | |
| 12963 | #ifdef DEBUG |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12964 | if (map() != (is_strict(shared()->language_mode()) |
| 12965 | ? native_context->strict_function_map() |
| 12966 | : native_context->sloppy_function_map())) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12967 | return false; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12968 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12969 | #endif |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12970 | |
| 12971 | set_map(no_prototype_map); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12972 | set_prototype_or_initial_map(no_prototype_map->GetHeap()->the_hole_value()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12973 | return true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12974 | } |
| 12975 | |
| 12976 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12977 | void JSFunction::SetInitialMap(Handle<JSFunction> function, Handle<Map> map, |
| 12978 | Handle<Object> prototype) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12979 | if (map->prototype() != *prototype) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12980 | Map::SetPrototype(map, prototype, FAST_PROTOTYPE); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12981 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12982 | function->set_prototype_or_initial_map(*map); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12983 | map->SetConstructor(*function); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12984 | #if TRACE_MAPS |
| 12985 | if (FLAG_trace_maps) { |
| 12986 | PrintF("[TraceMaps: InitialMap map= %p SFI= %d_%s ]\n", |
| 12987 | reinterpret_cast<void*>(*map), function->shared()->unique_id(), |
| 12988 | function->shared()->DebugName()->ToCString().get()); |
| 12989 | } |
| 12990 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12991 | } |
| 12992 | |
| 12993 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 12994 | #ifdef DEBUG |
| 12995 | namespace { |
| 12996 | |
| 12997 | bool CanSubclassHaveInobjectProperties(InstanceType instance_type) { |
| 12998 | switch (instance_type) { |
| 12999 | case JS_OBJECT_TYPE: |
| 13000 | case JS_CONTEXT_EXTENSION_OBJECT_TYPE: |
| 13001 | case JS_GENERATOR_OBJECT_TYPE: |
| 13002 | case JS_MODULE_TYPE: |
| 13003 | case JS_VALUE_TYPE: |
| 13004 | case JS_DATE_TYPE: |
| 13005 | case JS_ARRAY_TYPE: |
| 13006 | case JS_MESSAGE_OBJECT_TYPE: |
| 13007 | case JS_ARRAY_BUFFER_TYPE: |
| 13008 | case JS_TYPED_ARRAY_TYPE: |
| 13009 | case JS_DATA_VIEW_TYPE: |
| 13010 | case JS_SET_TYPE: |
| 13011 | case JS_MAP_TYPE: |
| 13012 | case JS_SET_ITERATOR_TYPE: |
| 13013 | case JS_MAP_ITERATOR_TYPE: |
| 13014 | case JS_ITERATOR_RESULT_TYPE: |
| 13015 | case JS_WEAK_MAP_TYPE: |
| 13016 | case JS_WEAK_SET_TYPE: |
| 13017 | case JS_PROMISE_TYPE: |
| 13018 | case JS_REGEXP_TYPE: |
| 13019 | case JS_FUNCTION_TYPE: |
| 13020 | return true; |
| 13021 | |
| 13022 | case JS_BOUND_FUNCTION_TYPE: |
| 13023 | case JS_PROXY_TYPE: |
| 13024 | case JS_GLOBAL_PROXY_TYPE: |
| 13025 | case JS_GLOBAL_OBJECT_TYPE: |
| 13026 | case FIXED_ARRAY_TYPE: |
| 13027 | case FIXED_DOUBLE_ARRAY_TYPE: |
| 13028 | case ODDBALL_TYPE: |
| 13029 | case FOREIGN_TYPE: |
| 13030 | case MAP_TYPE: |
| 13031 | case CODE_TYPE: |
| 13032 | case CELL_TYPE: |
| 13033 | case PROPERTY_CELL_TYPE: |
| 13034 | case WEAK_CELL_TYPE: |
| 13035 | case SYMBOL_TYPE: |
| 13036 | case BYTECODE_ARRAY_TYPE: |
| 13037 | case HEAP_NUMBER_TYPE: |
| 13038 | case MUTABLE_HEAP_NUMBER_TYPE: |
| 13039 | case SIMD128_VALUE_TYPE: |
| 13040 | case FILLER_TYPE: |
| 13041 | case BYTE_ARRAY_TYPE: |
| 13042 | case FREE_SPACE_TYPE: |
| 13043 | case SHARED_FUNCTION_INFO_TYPE: |
| 13044 | |
| 13045 | #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ |
| 13046 | case FIXED_##TYPE##_ARRAY_TYPE: |
| 13047 | #undef TYPED_ARRAY_CASE |
| 13048 | |
| 13049 | #define MAKE_STRUCT_CASE(NAME, Name, name) case NAME##_TYPE: |
| 13050 | STRUCT_LIST(MAKE_STRUCT_CASE) |
| 13051 | #undef MAKE_STRUCT_CASE |
| 13052 | // We must not end up here for these instance types at all. |
| 13053 | UNREACHABLE(); |
| 13054 | // Fall through. |
| 13055 | default: |
| 13056 | return false; |
| 13057 | } |
| 13058 | } |
| 13059 | |
| 13060 | } // namespace |
| 13061 | #endif |
| 13062 | |
| 13063 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13064 | void JSFunction::EnsureHasInitialMap(Handle<JSFunction> function) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13065 | DCHECK(function->IsConstructor() || function->shared()->is_generator()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13066 | if (function->has_initial_map()) return; |
| 13067 | Isolate* isolate = function->GetIsolate(); |
| 13068 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13069 | // The constructor should be compiled for the optimization hints to be |
| 13070 | // available. |
| 13071 | Compiler::Compile(function, CLEAR_EXCEPTION); |
| 13072 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13073 | // First create a new map with the size and number of in-object properties |
| 13074 | // suggested by the function. |
| 13075 | InstanceType instance_type; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13076 | if (function->shared()->is_generator()) { |
| 13077 | instance_type = JS_GENERATOR_OBJECT_TYPE; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13078 | } else { |
| 13079 | instance_type = JS_OBJECT_TYPE; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13080 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13081 | int instance_size; |
| 13082 | int in_object_properties; |
| 13083 | function->CalculateInstanceSize(instance_type, 0, &instance_size, |
| 13084 | &in_object_properties); |
| 13085 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13086 | Handle<Map> map = isolate->factory()->NewMap(instance_type, instance_size); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13087 | if (function->map()->is_strong()) { |
| 13088 | map->set_is_strong(); |
| 13089 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13090 | |
| 13091 | // Fetch or allocate prototype. |
| 13092 | Handle<Object> prototype; |
| 13093 | if (function->has_instance_prototype()) { |
| 13094 | prototype = handle(function->instance_prototype(), isolate); |
| 13095 | } else { |
| 13096 | prototype = isolate->factory()->NewFunctionPrototype(function); |
| 13097 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13098 | map->SetInObjectProperties(in_object_properties); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13099 | map->set_unused_property_fields(in_object_properties); |
| 13100 | DCHECK(map->has_fast_object_elements()); |
| 13101 | |
| 13102 | // Finally link initial map and constructor function. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13103 | DCHECK(prototype->IsJSReceiver()); |
| 13104 | JSFunction::SetInitialMap(function, map, prototype); |
| 13105 | map->StartInobjectSlackTracking(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13106 | } |
| 13107 | |
| 13108 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13109 | // static |
| 13110 | MaybeHandle<Map> JSFunction::GetDerivedMap(Isolate* isolate, |
| 13111 | Handle<JSFunction> constructor, |
| 13112 | Handle<JSReceiver> new_target) { |
| 13113 | EnsureHasInitialMap(constructor); |
| 13114 | |
| 13115 | Handle<Map> constructor_initial_map(constructor->initial_map(), isolate); |
| 13116 | if (*new_target == *constructor) return constructor_initial_map; |
| 13117 | |
| 13118 | // Fast case, new.target is a subclass of constructor. The map is cacheable |
| 13119 | // (and may already have been cached). new.target.prototype is guaranteed to |
| 13120 | // be a JSReceiver. |
| 13121 | if (new_target->IsJSFunction()) { |
| 13122 | Handle<JSFunction> function = Handle<JSFunction>::cast(new_target); |
| 13123 | |
| 13124 | // Check that |function|'s initial map still in sync with the |constructor|, |
| 13125 | // otherwise we must create a new initial map for |function|. |
| 13126 | if (function->has_initial_map() && |
| 13127 | function->initial_map()->GetConstructor() == *constructor) { |
| 13128 | return handle(function->initial_map(), isolate); |
| 13129 | } |
| 13130 | |
| 13131 | // Create a new map with the size and number of in-object properties |
| 13132 | // suggested by |function|. |
| 13133 | |
| 13134 | // Link initial map and constructor function if the new.target is actually a |
| 13135 | // subclass constructor. |
| 13136 | if (IsSubclassConstructor(function->shared()->kind())) { |
| 13137 | Handle<Object> prototype(function->instance_prototype(), isolate); |
| 13138 | InstanceType instance_type = constructor_initial_map->instance_type(); |
| 13139 | DCHECK(CanSubclassHaveInobjectProperties(instance_type)); |
| 13140 | int internal_fields = |
| 13141 | JSObject::GetInternalFieldCount(*constructor_initial_map); |
| 13142 | int pre_allocated = constructor_initial_map->GetInObjectProperties() - |
| 13143 | constructor_initial_map->unused_property_fields(); |
| 13144 | int instance_size; |
| 13145 | int in_object_properties; |
| 13146 | function->CalculateInstanceSizeForDerivedClass( |
| 13147 | instance_type, internal_fields, &instance_size, |
| 13148 | &in_object_properties); |
| 13149 | |
| 13150 | int unused_property_fields = in_object_properties - pre_allocated; |
| 13151 | Handle<Map> map = |
| 13152 | Map::CopyInitialMap(constructor_initial_map, instance_size, |
| 13153 | in_object_properties, unused_property_fields); |
| 13154 | map->set_new_target_is_base(false); |
| 13155 | |
| 13156 | JSFunction::SetInitialMap(function, map, prototype); |
| 13157 | map->SetConstructor(*constructor); |
| 13158 | map->StartInobjectSlackTracking(); |
| 13159 | return map; |
| 13160 | } |
| 13161 | } |
| 13162 | |
| 13163 | // Slow path, new.target is either a proxy or can't cache the map. |
| 13164 | // new.target.prototype is not guaranteed to be a JSReceiver, and may need to |
| 13165 | // fall back to the intrinsicDefaultProto. |
| 13166 | Handle<Object> prototype; |
| 13167 | if (new_target->IsJSFunction()) { |
| 13168 | Handle<JSFunction> function = Handle<JSFunction>::cast(new_target); |
| 13169 | // Make sure the new.target.prototype is cached. |
| 13170 | EnsureHasInitialMap(function); |
| 13171 | prototype = handle(function->prototype(), isolate); |
| 13172 | } else { |
| 13173 | Handle<String> prototype_string = isolate->factory()->prototype_string(); |
| 13174 | ASSIGN_RETURN_ON_EXCEPTION( |
| 13175 | isolate, prototype, |
| 13176 | JSReceiver::GetProperty(new_target, prototype_string), Map); |
| 13177 | // The above prototype lookup might change the constructor and its |
| 13178 | // prototype, hence we have to reload the initial map. |
| 13179 | EnsureHasInitialMap(constructor); |
| 13180 | constructor_initial_map = handle(constructor->initial_map(), isolate); |
| 13181 | } |
| 13182 | |
| 13183 | // If prototype is not a JSReceiver, fetch the intrinsicDefaultProto from the |
| 13184 | // correct realm. Rather than directly fetching the .prototype, we fetch the |
| 13185 | // constructor that points to the .prototype. This relies on |
| 13186 | // constructor.prototype being FROZEN for those constructors. |
| 13187 | if (!prototype->IsJSReceiver()) { |
| 13188 | Handle<Context> context; |
| 13189 | ASSIGN_RETURN_ON_EXCEPTION(isolate, context, |
| 13190 | JSReceiver::GetFunctionRealm(new_target), Map); |
| 13191 | DCHECK(context->IsNativeContext()); |
| 13192 | Handle<Object> maybe_index = JSReceiver::GetDataProperty( |
| 13193 | constructor, isolate->factory()->native_context_index_symbol()); |
| 13194 | int index = maybe_index->IsSmi() ? Smi::cast(*maybe_index)->value() |
| 13195 | : Context::OBJECT_FUNCTION_INDEX; |
| 13196 | Handle<JSFunction> realm_constructor(JSFunction::cast(context->get(index))); |
| 13197 | prototype = handle(realm_constructor->prototype(), isolate); |
| 13198 | } |
| 13199 | |
| 13200 | Handle<Map> map = Map::CopyInitialMap(constructor_initial_map); |
| 13201 | map->set_new_target_is_base(false); |
| 13202 | DCHECK(prototype->IsJSReceiver()); |
| 13203 | if (map->prototype() != *prototype) { |
| 13204 | Map::SetPrototype(map, prototype, FAST_PROTOTYPE); |
| 13205 | } |
| 13206 | map->SetConstructor(*constructor); |
| 13207 | return map; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13208 | } |
| 13209 | |
| 13210 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13211 | void JSFunction::PrintName(FILE* out) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13212 | base::SmartArrayPointer<char> name = shared()->DebugName()->ToCString(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13213 | PrintF(out, "%s", name.get()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13214 | } |
| 13215 | |
| 13216 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13217 | // The filter is a pattern that matches function names in this way: |
| 13218 | // "*" all; the default |
| 13219 | // "-" all but the top-level function |
| 13220 | // "-name" all but the function "name" |
| 13221 | // "" only the top-level function |
| 13222 | // "name" only the function "name" |
| 13223 | // "name*" only functions starting with "name" |
| 13224 | // "~" none; the tilde is not an identifier |
| 13225 | bool JSFunction::PassesFilter(const char* raw_filter) { |
| 13226 | if (*raw_filter == '*') return true; |
| 13227 | String* name = shared()->DebugName(); |
| 13228 | Vector<const char> filter = CStrVector(raw_filter); |
| 13229 | if (filter.length() == 0) return name->length() == 0; |
| 13230 | if (filter[0] == '-') { |
| 13231 | // Negative filter. |
| 13232 | if (filter.length() == 1) { |
| 13233 | return (name->length() != 0); |
| 13234 | } else if (name->IsUtf8EqualTo(filter.SubVector(1, filter.length()))) { |
| 13235 | return false; |
| 13236 | } |
| 13237 | if (filter[filter.length() - 1] == '*' && |
| 13238 | name->IsUtf8EqualTo(filter.SubVector(1, filter.length() - 1), true)) { |
| 13239 | return false; |
| 13240 | } |
| 13241 | return true; |
| 13242 | |
| 13243 | } else if (name->IsUtf8EqualTo(filter)) { |
| 13244 | return true; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 13245 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13246 | if (filter[filter.length() - 1] == '*' && |
| 13247 | name->IsUtf8EqualTo(filter.SubVector(0, filter.length() - 1), true)) { |
| 13248 | return true; |
| 13249 | } |
| 13250 | return false; |
| 13251 | } |
| 13252 | |
| 13253 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13254 | Handle<String> JSFunction::GetName(Handle<JSFunction> function) { |
| 13255 | Isolate* isolate = function->GetIsolate(); |
| 13256 | Handle<Object> name = |
| 13257 | JSReceiver::GetDataProperty(function, isolate->factory()->name_string()); |
| 13258 | if (name->IsString()) return Handle<String>::cast(name); |
| 13259 | return handle(function->shared()->DebugName(), isolate); |
| 13260 | } |
| 13261 | |
| 13262 | |
| 13263 | Handle<String> JSFunction::GetDebugName(Handle<JSFunction> function) { |
| 13264 | Isolate* isolate = function->GetIsolate(); |
| 13265 | Handle<Object> name = JSReceiver::GetDataProperty( |
| 13266 | function, isolate->factory()->display_name_string()); |
| 13267 | if (name->IsString()) return Handle<String>::cast(name); |
| 13268 | return JSFunction::GetName(function); |
| 13269 | } |
| 13270 | |
| 13271 | |
| 13272 | namespace { |
| 13273 | |
| 13274 | char const kNativeCodeSource[] = "function () { [native code] }"; |
| 13275 | |
| 13276 | |
| 13277 | Handle<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 |
| 13294 | Handle<String> JSBoundFunction::ToString(Handle<JSBoundFunction> function) { |
| 13295 | Isolate* const isolate = function->GetIsolate(); |
| 13296 | return isolate->factory()->NewStringFromAsciiChecked(kNativeCodeSource); |
| 13297 | } |
| 13298 | |
| 13299 | |
| 13300 | // static |
| 13301 | Handle<String> JSFunction::ToString(Handle<JSFunction> function) { |
| 13302 | Isolate* const isolate = function->GetIsolate(); |
| 13303 | Handle<SharedFunctionInfo> shared_info(function->shared(), isolate); |
| 13304 | |
| 13305 | // Check if {function} should hide its source code. |
| 13306 | if (!shared_info->script()->IsScript() || |
| 13307 | Script::cast(shared_info->script())->hide_source()) { |
| 13308 | return NativeCodeFunctionSourceString(shared_info); |
| 13309 | } |
| 13310 | |
| 13311 | // Check if we should print {function} as a class. |
| 13312 | Handle<Object> class_start_position = JSReceiver::GetDataProperty( |
| 13313 | function, isolate->factory()->class_start_position_symbol()); |
| 13314 | if (class_start_position->IsSmi()) { |
| 13315 | Handle<Object> class_end_position = JSReceiver::GetDataProperty( |
| 13316 | function, isolate->factory()->class_end_position_symbol()); |
| 13317 | Handle<String> script_source( |
| 13318 | String::cast(Script::cast(shared_info->script())->source()), isolate); |
| 13319 | return isolate->factory()->NewSubString( |
| 13320 | script_source, Handle<Smi>::cast(class_start_position)->value(), |
| 13321 | Handle<Smi>::cast(class_end_position)->value()); |
| 13322 | } |
| 13323 | |
| 13324 | // Check if we have source code for the {function}. |
| 13325 | if (!shared_info->HasSourceCode()) { |
| 13326 | return NativeCodeFunctionSourceString(shared_info); |
| 13327 | } |
| 13328 | |
| 13329 | IncrementalStringBuilder builder(isolate); |
| 13330 | if (!shared_info->is_arrow()) { |
| 13331 | if (shared_info->is_concise_method()) { |
| 13332 | if (shared_info->is_generator()) builder.AppendCharacter('*'); |
| 13333 | } else { |
| 13334 | if (shared_info->is_generator()) { |
| 13335 | builder.AppendCString("function* "); |
| 13336 | } else { |
| 13337 | builder.AppendCString("function "); |
| 13338 | } |
| 13339 | } |
| 13340 | if (shared_info->name_should_print_as_anonymous()) { |
| 13341 | builder.AppendCString("anonymous"); |
| 13342 | } else { |
| 13343 | builder.AppendString(handle(String::cast(shared_info->name()), isolate)); |
| 13344 | } |
| 13345 | } |
| 13346 | builder.AppendString(Handle<String>::cast(shared_info->GetSourceCode())); |
| 13347 | return builder.Finish().ToHandleChecked(); |
| 13348 | } |
| 13349 | |
| 13350 | |
| 13351 | void Oddball::Initialize(Isolate* isolate, Handle<Oddball> oddball, |
| 13352 | const char* to_string, Handle<Object> to_number, |
| 13353 | const char* type_of, byte kind) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13354 | Handle<String> internalized_to_string = |
| 13355 | isolate->factory()->InternalizeUtf8String(to_string); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13356 | Handle<String> internalized_type_of = |
| 13357 | isolate->factory()->InternalizeUtf8String(type_of); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13358 | oddball->set_to_number(*to_number); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13359 | oddball->set_to_string(*internalized_to_string); |
| 13360 | oddball->set_type_of(*internalized_type_of); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13361 | oddball->set_kind(kind); |
| 13362 | } |
| 13363 | |
| 13364 | |
| 13365 | void Script::InitLineEnds(Handle<Script> script) { |
| 13366 | if (!script->line_ends()->IsUndefined()) return; |
| 13367 | |
| 13368 | Isolate* isolate = script->GetIsolate(); |
| 13369 | |
| 13370 | if (!script->source()->IsString()) { |
| 13371 | DCHECK(script->source()->IsUndefined()); |
| 13372 | Handle<FixedArray> empty = isolate->factory()->NewFixedArray(0); |
| 13373 | script->set_line_ends(*empty); |
| 13374 | DCHECK(script->line_ends()->IsFixedArray()); |
| 13375 | return; |
| 13376 | } |
| 13377 | |
| 13378 | Handle<String> src(String::cast(script->source()), isolate); |
| 13379 | |
| 13380 | Handle<FixedArray> array = String::CalculateLineEnds(src, true); |
| 13381 | |
| 13382 | if (*array != isolate->heap()->empty_fixed_array()) { |
| 13383 | array->set_map(isolate->heap()->fixed_cow_array_map()); |
| 13384 | } |
| 13385 | |
| 13386 | script->set_line_ends(*array); |
| 13387 | DCHECK(script->line_ends()->IsFixedArray()); |
| 13388 | } |
| 13389 | |
| 13390 | |
| 13391 | int Script::GetColumnNumber(Handle<Script> script, int code_pos) { |
| 13392 | int line_number = GetLineNumber(script, code_pos); |
| 13393 | if (line_number == -1) return -1; |
| 13394 | |
| 13395 | DisallowHeapAllocation no_allocation; |
| 13396 | FixedArray* line_ends_array = FixedArray::cast(script->line_ends()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13397 | line_number = line_number - script->line_offset(); |
| 13398 | if (line_number == 0) return code_pos + script->column_offset(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13399 | int prev_line_end_pos = |
| 13400 | Smi::cast(line_ends_array->get(line_number - 1))->value(); |
| 13401 | return code_pos - (prev_line_end_pos + 1); |
| 13402 | } |
| 13403 | |
| 13404 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13405 | int Script::GetLineNumberWithArray(int code_pos) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13406 | DisallowHeapAllocation no_allocation; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13407 | DCHECK(line_ends()->IsFixedArray()); |
| 13408 | FixedArray* line_ends_array = FixedArray::cast(line_ends()); |
| 13409 | int line_ends_len = line_ends_array->length(); |
| 13410 | if (line_ends_len == 0) return -1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13411 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13412 | if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) { |
| 13413 | return line_offset(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13414 | } |
| 13415 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13416 | int left = 0; |
| 13417 | int right = line_ends_len; |
| 13418 | while (int half = (right - left) / 2) { |
| 13419 | if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) { |
| 13420 | right -= half; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13421 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13422 | left += half; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13423 | } |
| 13424 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13425 | return right + line_offset(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13426 | } |
| 13427 | |
| 13428 | |
| 13429 | int Script::GetLineNumber(Handle<Script> script, int code_pos) { |
| 13430 | InitLineEnds(script); |
| 13431 | return script->GetLineNumberWithArray(code_pos); |
| 13432 | } |
| 13433 | |
| 13434 | |
| 13435 | int Script::GetLineNumber(int code_pos) { |
| 13436 | DisallowHeapAllocation no_allocation; |
| 13437 | if (!line_ends()->IsUndefined()) return GetLineNumberWithArray(code_pos); |
| 13438 | |
| 13439 | // Slow mode: we do not have line_ends. We have to iterate through source. |
| 13440 | if (!source()->IsString()) return -1; |
| 13441 | |
| 13442 | String* source_string = String::cast(source()); |
| 13443 | int line = 0; |
| 13444 | int len = source_string->length(); |
| 13445 | for (int pos = 0; pos < len; pos++) { |
| 13446 | if (pos == code_pos) break; |
| 13447 | if (source_string->Get(pos) == '\n') line++; |
| 13448 | } |
| 13449 | return line; |
| 13450 | } |
| 13451 | |
| 13452 | |
| 13453 | Handle<Object> Script::GetNameOrSourceURL(Handle<Script> script) { |
| 13454 | Isolate* isolate = script->GetIsolate(); |
| 13455 | Handle<String> name_or_source_url_key = |
| 13456 | isolate->factory()->InternalizeOneByteString( |
| 13457 | STATIC_CHAR_VECTOR("nameOrSourceURL")); |
| 13458 | Handle<JSObject> script_wrapper = Script::GetWrapper(script); |
| 13459 | Handle<Object> property = Object::GetProperty( |
| 13460 | script_wrapper, name_or_source_url_key).ToHandleChecked(); |
| 13461 | DCHECK(property->IsJSFunction()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13462 | Handle<Object> result; |
| 13463 | // Do not check against pending exception, since this function may be called |
| 13464 | // when an exception has already been pending. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13465 | if (!Execution::TryCall(isolate, property, script_wrapper, 0, NULL) |
| 13466 | .ToHandle(&result)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13467 | return isolate->factory()->undefined_value(); |
| 13468 | } |
| 13469 | return result; |
| 13470 | } |
| 13471 | |
| 13472 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13473 | Handle<JSObject> Script::GetWrapper(Handle<Script> script) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13474 | Isolate* isolate = script->GetIsolate(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 13475 | if (!script->wrapper()->IsUndefined()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13476 | DCHECK(script->wrapper()->IsWeakCell()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 13477 | Handle<WeakCell> cell(WeakCell::cast(script->wrapper())); |
| 13478 | if (!cell->cleared()) { |
| 13479 | // Return a handle for the existing script wrapper from the cache. |
| 13480 | return handle(JSObject::cast(cell->value())); |
| 13481 | } |
| 13482 | // If we found an empty WeakCell, that means the script wrapper was |
| 13483 | // GCed. We are not notified directly of that, so we decrement here |
| 13484 | // so that we at least don't count double for any given script. |
| 13485 | isolate->counters()->script_wrappers()->Decrement(); |
| 13486 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13487 | // Construct a new script wrapper. |
| 13488 | isolate->counters()->script_wrappers()->Increment(); |
| 13489 | Handle<JSFunction> constructor = isolate->script_function(); |
| 13490 | Handle<JSValue> result = |
| 13491 | Handle<JSValue>::cast(isolate->factory()->NewJSObject(constructor)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13492 | result->set_value(*script); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 13493 | Handle<WeakCell> cell = isolate->factory()->NewWeakCell(result); |
| 13494 | script->set_wrapper(*cell); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13495 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13496 | } |
| 13497 | |
| 13498 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13499 | MaybeHandle<SharedFunctionInfo> Script::FindSharedFunctionInfo( |
| 13500 | FunctionLiteral* fun) { |
| 13501 | WeakFixedArray::Iterator iterator(shared_function_infos()); |
| 13502 | SharedFunctionInfo* shared; |
| 13503 | while ((shared = iterator.Next<SharedFunctionInfo>())) { |
| 13504 | if (fun->function_token_position() == shared->function_token_position() && |
| 13505 | fun->start_position() == shared->start_position()) { |
| 13506 | return Handle<SharedFunctionInfo>(shared); |
| 13507 | } |
| 13508 | } |
| 13509 | return MaybeHandle<SharedFunctionInfo>(); |
| 13510 | } |
| 13511 | |
| 13512 | |
| 13513 | Script::Iterator::Iterator(Isolate* isolate) |
| 13514 | : iterator_(isolate->heap()->script_list()) {} |
| 13515 | |
| 13516 | |
| 13517 | Script* Script::Iterator::Next() { return iterator_.Next<Script>(); } |
| 13518 | |
| 13519 | |
| 13520 | SharedFunctionInfo::Iterator::Iterator(Isolate* isolate) |
| 13521 | : script_iterator_(isolate), |
| 13522 | sfi_iterator_(isolate->heap()->noscript_shared_function_infos()) {} |
| 13523 | |
| 13524 | |
| 13525 | bool SharedFunctionInfo::Iterator::NextScript() { |
| 13526 | Script* script = script_iterator_.Next(); |
| 13527 | if (script == NULL) return false; |
| 13528 | sfi_iterator_.Reset(script->shared_function_infos()); |
| 13529 | return true; |
| 13530 | } |
| 13531 | |
| 13532 | |
| 13533 | SharedFunctionInfo* SharedFunctionInfo::Iterator::Next() { |
| 13534 | do { |
| 13535 | SharedFunctionInfo* next = sfi_iterator_.Next<SharedFunctionInfo>(); |
| 13536 | if (next != NULL) return next; |
| 13537 | } while (NextScript()); |
| 13538 | return NULL; |
| 13539 | } |
| 13540 | |
| 13541 | |
| 13542 | void SharedFunctionInfo::SetScript(Handle<SharedFunctionInfo> shared, |
| 13543 | Handle<Object> script_object) { |
| 13544 | if (shared->script() == *script_object) return; |
| 13545 | Isolate* isolate = shared->GetIsolate(); |
| 13546 | |
| 13547 | // Add shared function info to new script's list. If a collection occurs, |
| 13548 | // the shared function info may be temporarily in two lists. |
| 13549 | // This is okay because the gc-time processing of these lists can tolerate |
| 13550 | // duplicates. |
| 13551 | Handle<Object> list; |
| 13552 | if (script_object->IsScript()) { |
| 13553 | Handle<Script> script = Handle<Script>::cast(script_object); |
| 13554 | list = handle(script->shared_function_infos(), isolate); |
| 13555 | } else { |
| 13556 | list = isolate->factory()->noscript_shared_function_infos(); |
| 13557 | } |
| 13558 | |
| 13559 | #ifdef DEBUG |
| 13560 | { |
| 13561 | WeakFixedArray::Iterator iterator(*list); |
| 13562 | SharedFunctionInfo* next; |
| 13563 | while ((next = iterator.Next<SharedFunctionInfo>())) { |
| 13564 | DCHECK_NE(next, *shared); |
| 13565 | } |
| 13566 | } |
| 13567 | #endif // DEBUG |
| 13568 | list = WeakFixedArray::Add(list, shared); |
| 13569 | |
| 13570 | if (script_object->IsScript()) { |
| 13571 | Handle<Script> script = Handle<Script>::cast(script_object); |
| 13572 | script->set_shared_function_infos(*list); |
| 13573 | } else { |
| 13574 | isolate->heap()->SetRootNoScriptSharedFunctionInfos(*list); |
| 13575 | } |
| 13576 | |
| 13577 | // Remove shared function info from old script's list. |
| 13578 | if (shared->script()->IsScript()) { |
| 13579 | Script* old_script = Script::cast(shared->script()); |
| 13580 | if (old_script->shared_function_infos()->IsWeakFixedArray()) { |
| 13581 | WeakFixedArray* list = |
| 13582 | WeakFixedArray::cast(old_script->shared_function_infos()); |
| 13583 | list->Remove(shared); |
| 13584 | } |
| 13585 | } else { |
| 13586 | // Remove shared function info from root array. |
| 13587 | Object* list = isolate->heap()->noscript_shared_function_infos(); |
| 13588 | CHECK(WeakFixedArray::cast(list)->Remove(shared)); |
| 13589 | } |
| 13590 | |
| 13591 | // Finally set new script. |
| 13592 | shared->set_script(*script_object); |
| 13593 | } |
| 13594 | |
| 13595 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 13596 | String* SharedFunctionInfo::DebugName() { |
| 13597 | Object* n = name(); |
| 13598 | if (!n->IsString() || String::cast(n)->length() == 0) return inferred_name(); |
| 13599 | return String::cast(n); |
| 13600 | } |
| 13601 | |
| 13602 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13603 | bool SharedFunctionInfo::HasSourceCode() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13604 | return !script()->IsUndefined() && |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 13605 | !reinterpret_cast<Script*>(script())->source()->IsUndefined(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13606 | } |
| 13607 | |
| 13608 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13609 | Handle<Object> SharedFunctionInfo::GetSourceCode() { |
| 13610 | if (!HasSourceCode()) return GetIsolate()->factory()->undefined_value(); |
| 13611 | Handle<String> source(String::cast(Script::cast(script())->source())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13612 | return GetIsolate()->factory()->NewSubString( |
| 13613 | source, start_position(), end_position()); |
| 13614 | } |
| 13615 | |
| 13616 | |
| 13617 | bool SharedFunctionInfo::IsInlineable() { |
| 13618 | // Check that the function has a script associated with it. |
| 13619 | if (!script()->IsScript()) return false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13620 | return !optimization_disabled(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13621 | } |
| 13622 | |
| 13623 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13624 | int SharedFunctionInfo::SourceSize() { |
| 13625 | return end_position() - start_position(); |
| 13626 | } |
| 13627 | |
| 13628 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13629 | namespace { |
| 13630 | |
| 13631 | void CalculateInstanceSizeHelper(InstanceType instance_type, |
| 13632 | int requested_internal_fields, |
| 13633 | int requested_in_object_properties, |
| 13634 | int* instance_size, |
| 13635 | int* in_object_properties) { |
| 13636 | int header_size = JSObject::GetHeaderSize(instance_type); |
| 13637 | DCHECK_LE(requested_internal_fields, |
| 13638 | (JSObject::kMaxInstanceSize - header_size) >> kPointerSizeLog2); |
| 13639 | *instance_size = |
| 13640 | Min(header_size + |
| 13641 | ((requested_internal_fields + requested_in_object_properties) |
| 13642 | << kPointerSizeLog2), |
| 13643 | JSObject::kMaxInstanceSize); |
| 13644 | *in_object_properties = ((*instance_size - header_size) >> kPointerSizeLog2) - |
| 13645 | requested_internal_fields; |
| 13646 | } |
| 13647 | |
| 13648 | } // namespace |
| 13649 | |
| 13650 | |
| 13651 | void JSFunction::CalculateInstanceSize(InstanceType instance_type, |
| 13652 | int requested_internal_fields, |
| 13653 | int* instance_size, |
| 13654 | int* in_object_properties) { |
| 13655 | CalculateInstanceSizeHelper(instance_type, requested_internal_fields, |
| 13656 | shared()->expected_nof_properties(), |
| 13657 | instance_size, in_object_properties); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13658 | } |
| 13659 | |
| 13660 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13661 | void JSFunction::CalculateInstanceSizeForDerivedClass( |
| 13662 | InstanceType instance_type, int requested_internal_fields, |
| 13663 | int* instance_size, int* in_object_properties) { |
| 13664 | Isolate* isolate = GetIsolate(); |
| 13665 | int expected_nof_properties = 0; |
| 13666 | for (PrototypeIterator iter(isolate, this, |
| 13667 | PrototypeIterator::START_AT_RECEIVER); |
| 13668 | !iter.IsAtEnd(); iter.Advance()) { |
| 13669 | JSFunction* func = iter.GetCurrent<JSFunction>(); |
| 13670 | SharedFunctionInfo* shared = func->shared(); |
| 13671 | expected_nof_properties += shared->expected_nof_properties(); |
| 13672 | if (!IsSubclassConstructor(shared->kind())) { |
| 13673 | break; |
| 13674 | } |
| 13675 | } |
| 13676 | CalculateInstanceSizeHelper(instance_type, requested_internal_fields, |
| 13677 | expected_nof_properties, instance_size, |
| 13678 | in_object_properties); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13679 | } |
| 13680 | |
| 13681 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13682 | // Output the source code without any allocation in the heap. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 13683 | std::ostream& operator<<(std::ostream& os, const SourceCodeOf& v) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13684 | const SharedFunctionInfo* s = v.value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13685 | // For some native functions there is no source. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13686 | if (!s->HasSourceCode()) return os << "<No Source>"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13687 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 13688 | // Get the source for the script which this function came from. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13689 | // Don't use String::cast because we don't want more assertion errors while |
| 13690 | // we are already creating a stack dump. |
| 13691 | String* script_source = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13692 | reinterpret_cast<String*>(Script::cast(s->script())->source()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13693 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13694 | if (!script_source->LooksValid()) return os << "<Invalid Source>"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13695 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13696 | if (!s->is_toplevel()) { |
| 13697 | os << "function "; |
| 13698 | Object* name = s->name(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13699 | if (name->IsString() && String::cast(name)->length() > 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13700 | String::cast(name)->PrintUC16(os); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13701 | } |
| 13702 | } |
| 13703 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13704 | int len = s->end_position() - s->start_position(); |
| 13705 | if (len <= v.max_length || v.max_length < 0) { |
| 13706 | script_source->PrintUC16(os, s->start_position(), s->end_position()); |
| 13707 | return os; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13708 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13709 | script_source->PrintUC16(os, s->start_position(), |
| 13710 | s->start_position() + v.max_length); |
| 13711 | return os << "...\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13712 | } |
| 13713 | } |
| 13714 | |
| 13715 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13716 | static bool IsCodeEquivalent(Code* code, Code* recompiled) { |
| 13717 | if (code->instruction_size() != recompiled->instruction_size()) return false; |
| 13718 | ByteArray* code_relocation = code->relocation_info(); |
| 13719 | ByteArray* recompiled_relocation = recompiled->relocation_info(); |
| 13720 | int length = code_relocation->length(); |
| 13721 | if (length != recompiled_relocation->length()) return false; |
| 13722 | int compare = memcmp(code_relocation->GetDataStartAddress(), |
| 13723 | recompiled_relocation->GetDataStartAddress(), |
| 13724 | length); |
| 13725 | return compare == 0; |
| 13726 | } |
| 13727 | |
| 13728 | |
| 13729 | void SharedFunctionInfo::EnableDeoptimizationSupport(Code* recompiled) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13730 | DCHECK(!has_deoptimization_support()); |
| 13731 | DisallowHeapAllocation no_allocation; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13732 | Code* code = this->code(); |
| 13733 | if (IsCodeEquivalent(code, recompiled)) { |
| 13734 | // Copy the deoptimization data from the recompiled code. |
| 13735 | code->set_deoptimization_data(recompiled->deoptimization_data()); |
| 13736 | code->set_has_deoptimization_support(true); |
| 13737 | } else { |
| 13738 | // TODO(3025757): In case the recompiled isn't equivalent to the |
| 13739 | // old code, we have to replace it. We should try to avoid this |
| 13740 | // altogether because it flushes valuable type feedback by |
| 13741 | // effectively resetting all IC state. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13742 | ReplaceCode(recompiled); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13743 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13744 | DCHECK(has_deoptimization_support()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13745 | } |
| 13746 | |
| 13747 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13748 | void SharedFunctionInfo::DisableOptimization(BailoutReason reason) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 13749 | // Disable optimization for the shared function info and mark the |
| 13750 | // code as non-optimizable. The marker on the shared function info |
| 13751 | // is there because we flush non-optimized code thereby loosing the |
| 13752 | // non-optimizable information for the code. When the code is |
| 13753 | // regenerated and set on the shared function info it is marked as |
| 13754 | // non-optimizable if optimization is disabled for the shared |
| 13755 | // function info. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 13756 | DCHECK(reason != kNoReason); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 13757 | set_optimization_disabled(true); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 13758 | set_disable_optimization_reason(reason); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13759 | // Code should be the lazy compilation stub or else unoptimized. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13760 | DCHECK(code()->kind() == Code::FUNCTION || code()->kind() == Code::BUILTIN); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13761 | PROFILE(GetIsolate(), CodeDisableOptEvent(code(), this)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 13762 | if (FLAG_trace_opt) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13763 | PrintF("[disabled optimization for "); |
| 13764 | ShortPrint(); |
| 13765 | PrintF(", reason: %s]\n", GetBailoutReason(reason)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 13766 | } |
| 13767 | } |
| 13768 | |
| 13769 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13770 | void SharedFunctionInfo::InitFromFunctionLiteral( |
| 13771 | Handle<SharedFunctionInfo> shared_info, FunctionLiteral* lit) { |
| 13772 | shared_info->set_length(lit->scope()->default_function_length()); |
| 13773 | shared_info->set_internal_formal_parameter_count(lit->parameter_count()); |
| 13774 | shared_info->set_function_token_position(lit->function_token_position()); |
| 13775 | shared_info->set_start_position(lit->start_position()); |
| 13776 | shared_info->set_end_position(lit->end_position()); |
| 13777 | shared_info->set_is_expression(lit->is_expression()); |
| 13778 | shared_info->set_is_anonymous(lit->is_anonymous()); |
| 13779 | shared_info->set_inferred_name(*lit->inferred_name()); |
| 13780 | shared_info->set_allows_lazy_compilation(lit->AllowsLazyCompilation()); |
| 13781 | shared_info->set_allows_lazy_compilation_without_context( |
| 13782 | lit->AllowsLazyCompilationWithoutContext()); |
| 13783 | shared_info->set_language_mode(lit->language_mode()); |
| 13784 | shared_info->set_uses_arguments(lit->scope()->arguments() != NULL); |
| 13785 | shared_info->set_has_duplicate_parameters(lit->has_duplicate_parameters()); |
| 13786 | shared_info->set_ast_node_count(lit->ast_node_count()); |
| 13787 | shared_info->set_is_function(lit->is_function()); |
| 13788 | if (lit->dont_optimize_reason() != kNoReason) { |
| 13789 | shared_info->DisableOptimization(lit->dont_optimize_reason()); |
| 13790 | } |
| 13791 | shared_info->set_dont_crankshaft(lit->flags() & |
| 13792 | AstProperties::kDontCrankshaft); |
| 13793 | shared_info->set_kind(lit->kind()); |
| 13794 | if (!IsConstructable(lit->kind(), lit->language_mode())) { |
| 13795 | shared_info->set_construct_stub( |
| 13796 | *shared_info->GetIsolate()->builtins()->ConstructedNonConstructable()); |
| 13797 | } |
| 13798 | shared_info->set_needs_home_object(lit->scope()->NeedsHomeObject()); |
| 13799 | shared_info->set_asm_function(lit->scope()->asm_function()); |
| 13800 | } |
| 13801 | |
| 13802 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13803 | bool SharedFunctionInfo::VerifyBailoutId(BailoutId id) { |
| 13804 | DCHECK(!id.IsNone()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13805 | Code* unoptimized = code(); |
| 13806 | DeoptimizationOutputData* data = |
| 13807 | DeoptimizationOutputData::cast(unoptimized->deoptimization_data()); |
| 13808 | unsigned ignore = Deoptimizer::GetOutputInfo(data, id, this); |
| 13809 | USE(ignore); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13810 | return true; // Return true if there was no DCHECK. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13811 | } |
| 13812 | |
| 13813 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13814 | void Map::StartInobjectSlackTracking() { |
| 13815 | DCHECK(!IsInobjectSlackTrackingInProgress()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13816 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13817 | // No tracking during the snapshot construction phase. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13818 | Isolate* isolate = GetIsolate(); |
| 13819 | if (isolate->serializer_enabled()) return; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13820 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13821 | if (unused_property_fields() == 0) return; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13822 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13823 | set_construction_counter(Map::kSlackTrackingCounterStart); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13824 | } |
| 13825 | |
| 13826 | |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 13827 | void SharedFunctionInfo::ResetForNewContext(int new_ic_age) { |
| 13828 | code()->ClearInlineCaches(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13829 | // If we clear ICs, we need to clear the type feedback vector too, since |
| 13830 | // CallICs are synced with a feedback vector slot. |
| 13831 | ClearTypeFeedbackInfo(); |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 13832 | set_ic_age(new_ic_age); |
| 13833 | if (code()->kind() == Code::FUNCTION) { |
| 13834 | code()->set_profiler_ticks(0); |
| 13835 | if (optimization_disabled() && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13836 | opt_count() >= FLAG_max_opt_count) { |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 13837 | // Re-enable optimizations if they were disabled due to opt_count limit. |
| 13838 | set_optimization_disabled(false); |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 13839 | } |
| 13840 | set_opt_count(0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13841 | set_deopt_count(0); |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 13842 | } |
| 13843 | } |
| 13844 | |
| 13845 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13846 | int SharedFunctionInfo::SearchOptimizedCodeMapEntry(Context* native_context, |
| 13847 | BailoutId osr_ast_id) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13848 | DisallowHeapAllocation no_gc; |
| 13849 | DCHECK(native_context->IsNativeContext()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13850 | if (!OptimizedCodeMapIsCleared()) { |
| 13851 | FixedArray* optimized_code_map = this->optimized_code_map(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13852 | int length = optimized_code_map->length(); |
| 13853 | Smi* osr_ast_id_smi = Smi::FromInt(osr_ast_id.ToInt()); |
| 13854 | for (int i = kEntriesStart; i < length; i += kEntryLength) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13855 | if (WeakCell::cast(optimized_code_map->get(i + kContextOffset)) |
| 13856 | ->value() == native_context && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13857 | optimized_code_map->get(i + kOsrAstIdOffset) == osr_ast_id_smi) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13858 | return i; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13859 | } |
| 13860 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13861 | Object* shared_code = |
| 13862 | WeakCell::cast(optimized_code_map->get(kSharedCodeIndex))->value(); |
| 13863 | if (shared_code->IsCode() && osr_ast_id.IsNone()) { |
| 13864 | return kSharedCodeIndex; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13865 | } |
| 13866 | } |
| 13867 | return -1; |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 13868 | } |
| 13869 | |
| 13870 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13871 | CodeAndLiterals SharedFunctionInfo::SearchOptimizedCodeMap( |
| 13872 | Context* native_context, BailoutId osr_ast_id) { |
| 13873 | CodeAndLiterals result = {nullptr, nullptr}; |
| 13874 | int entry = SearchOptimizedCodeMapEntry(native_context, osr_ast_id); |
| 13875 | if (entry != kNotFound) { |
| 13876 | FixedArray* code_map = optimized_code_map(); |
| 13877 | if (entry == kSharedCodeIndex) { |
| 13878 | // We know the weak cell isn't cleared because we made sure of it in |
| 13879 | // SearchOptimizedCodeMapEntry and performed no allocations since that |
| 13880 | // call. |
| 13881 | result = { |
| 13882 | Code::cast(WeakCell::cast(code_map->get(kSharedCodeIndex))->value()), |
| 13883 | nullptr}; |
| 13884 | } else { |
| 13885 | DCHECK_LE(entry + kEntryLength, code_map->length()); |
| 13886 | WeakCell* cell = WeakCell::cast(code_map->get(entry + kCachedCodeOffset)); |
| 13887 | WeakCell* literals_cell = |
| 13888 | WeakCell::cast(code_map->get(entry + kLiteralsOffset)); |
| 13889 | |
| 13890 | result = {cell->cleared() ? nullptr : Code::cast(cell->value()), |
| 13891 | literals_cell->cleared() |
| 13892 | ? nullptr |
| 13893 | : LiteralsArray::cast(literals_cell->value())}; |
| 13894 | } |
| 13895 | } |
| 13896 | if (FLAG_trace_opt && !OptimizedCodeMapIsCleared() && |
| 13897 | result.code == nullptr) { |
| 13898 | PrintF("[didn't find optimized code in optimized code map for "); |
| 13899 | ShortPrint(); |
| 13900 | PrintF("]\n"); |
| 13901 | } |
| 13902 | return result; |
| 13903 | } |
| 13904 | |
| 13905 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13906 | #define DECLARE_TAG(ignore1, name, ignore2) name, |
| 13907 | const char* const VisitorSynchronization::kTags[ |
| 13908 | VisitorSynchronization::kNumberOfSyncTags] = { |
| 13909 | VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG) |
| 13910 | }; |
| 13911 | #undef DECLARE_TAG |
| 13912 | |
| 13913 | |
| 13914 | #define DECLARE_TAG(ignore1, ignore2, name) name, |
| 13915 | const char* const VisitorSynchronization::kTagNames[ |
| 13916 | VisitorSynchronization::kNumberOfSyncTags] = { |
| 13917 | VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG) |
| 13918 | }; |
| 13919 | #undef DECLARE_TAG |
| 13920 | |
| 13921 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13922 | void ObjectVisitor::VisitCodeTarget(RelocInfo* rinfo) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13923 | DCHECK(RelocInfo::IsCodeTarget(rinfo->rmode())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13924 | Object* target = Code::GetCodeFromTargetAddress(rinfo->target_address()); |
| 13925 | Object* old_target = target; |
| 13926 | VisitPointer(&target); |
| 13927 | CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target. |
| 13928 | } |
| 13929 | |
| 13930 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13931 | void ObjectVisitor::VisitCodeAgeSequence(RelocInfo* rinfo) { |
| 13932 | DCHECK(RelocInfo::IsCodeAgeSequence(rinfo->rmode())); |
| 13933 | Object* stub = rinfo->code_age_stub(); |
| 13934 | if (stub) { |
| 13935 | VisitPointer(&stub); |
| 13936 | } |
| 13937 | } |
| 13938 | |
| 13939 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 13940 | void ObjectVisitor::VisitCodeEntry(Address entry_address) { |
| 13941 | Object* code = Code::GetObjectFromEntryAddress(entry_address); |
| 13942 | Object* old_code = code; |
| 13943 | VisitPointer(&code); |
| 13944 | if (code != old_code) { |
| 13945 | Memory::Address_at(entry_address) = reinterpret_cast<Code*>(code)->entry(); |
| 13946 | } |
| 13947 | } |
| 13948 | |
| 13949 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13950 | void ObjectVisitor::VisitCell(RelocInfo* rinfo) { |
| 13951 | DCHECK(rinfo->rmode() == RelocInfo::CELL); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13952 | Object* cell = rinfo->target_cell(); |
| 13953 | Object* old_cell = cell; |
| 13954 | VisitPointer(&cell); |
| 13955 | if (cell != old_cell) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13956 | rinfo->set_target_cell(reinterpret_cast<Cell*>(cell)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13957 | } |
| 13958 | } |
| 13959 | |
| 13960 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13961 | void ObjectVisitor::VisitDebugTarget(RelocInfo* rinfo) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13962 | DCHECK(RelocInfo::IsDebugBreakSlot(rinfo->rmode()) && |
| 13963 | rinfo->IsPatchedDebugBreakSlotSequence()); |
| 13964 | Object* target = Code::GetCodeFromTargetAddress(rinfo->debug_call_address()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13965 | Object* old_target = target; |
| 13966 | VisitPointer(&target); |
| 13967 | CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target. |
| 13968 | } |
| 13969 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13970 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13971 | void ObjectVisitor::VisitEmbeddedPointer(RelocInfo* rinfo) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13972 | DCHECK(rinfo->rmode() == RelocInfo::EMBEDDED_OBJECT); |
| 13973 | Object* p = rinfo->target_object(); |
| 13974 | VisitPointer(&p); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13975 | } |
| 13976 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13977 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13978 | void ObjectVisitor::VisitExternalReference(RelocInfo* rinfo) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13979 | Address p = rinfo->target_external_reference(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13980 | VisitExternalReference(&p); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13981 | } |
| 13982 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13983 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13984 | void Code::InvalidateRelocation() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13985 | InvalidateEmbeddedObjects(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13986 | set_relocation_info(GetHeap()->empty_byte_array()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13987 | } |
| 13988 | |
| 13989 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13990 | void Code::InvalidateEmbeddedObjects() { |
| 13991 | Object* undefined = GetHeap()->undefined_value(); |
| 13992 | Cell* undefined_cell = GetHeap()->undefined_cell(); |
| 13993 | int mode_mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) | |
| 13994 | RelocInfo::ModeMask(RelocInfo::CELL); |
| 13995 | for (RelocIterator it(this, mode_mask); !it.done(); it.next()) { |
| 13996 | RelocInfo::Mode mode = it.rinfo()->rmode(); |
| 13997 | if (mode == RelocInfo::EMBEDDED_OBJECT) { |
| 13998 | it.rinfo()->set_target_object(undefined, SKIP_WRITE_BARRIER); |
| 13999 | } else if (mode == RelocInfo::CELL) { |
| 14000 | it.rinfo()->set_target_cell(undefined_cell, SKIP_WRITE_BARRIER); |
| 14001 | } |
| 14002 | } |
| 14003 | } |
| 14004 | |
| 14005 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 14006 | void Code::Relocate(intptr_t delta) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14007 | for (RelocIterator it(this, RelocInfo::kApplyMask); !it.done(); it.next()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14008 | it.rinfo()->apply(delta); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14009 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14010 | Assembler::FlushICache(GetIsolate(), instruction_start(), instruction_size()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14011 | } |
| 14012 | |
| 14013 | |
| 14014 | void Code::CopyFrom(const CodeDesc& desc) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14015 | DCHECK(Marking::Color(this) == Marking::WHITE_OBJECT); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14016 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14017 | // copy code |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14018 | CopyBytes(instruction_start(), desc.buffer, |
| 14019 | static_cast<size_t>(desc.instr_size)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14020 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14021 | // copy reloc info |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14022 | CopyBytes(relocation_start(), |
| 14023 | desc.buffer + desc.buffer_size - desc.reloc_size, |
| 14024 | static_cast<size_t>(desc.reloc_size)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14025 | |
| 14026 | // unbox handles and relocate |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 14027 | intptr_t delta = instruction_start() - desc.buffer; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14028 | int mode_mask = RelocInfo::kCodeTargetMask | |
| 14029 | RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14030 | RelocInfo::ModeMask(RelocInfo::CELL) | |
| 14031 | RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY) | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14032 | RelocInfo::kApplyMask; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14033 | // Needed to find target_object and runtime_entry on X64 |
| 14034 | Assembler* origin = desc.origin; |
| 14035 | AllowDeferredHandleDereference embedding_raw_address; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14036 | for (RelocIterator it(this, mode_mask); !it.done(); it.next()) { |
| 14037 | RelocInfo::Mode mode = it.rinfo()->rmode(); |
| 14038 | if (mode == RelocInfo::EMBEDDED_OBJECT) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 14039 | Handle<Object> p = it.rinfo()->target_object_handle(origin); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14040 | it.rinfo()->set_target_object(*p, SKIP_WRITE_BARRIER, SKIP_ICACHE_FLUSH); |
| 14041 | } else if (mode == RelocInfo::CELL) { |
| 14042 | Handle<Cell> cell = it.rinfo()->target_cell_handle(); |
| 14043 | it.rinfo()->set_target_cell(*cell, SKIP_WRITE_BARRIER, SKIP_ICACHE_FLUSH); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14044 | } else if (RelocInfo::IsCodeTarget(mode)) { |
| 14045 | // rewrite code handles in inline cache targets to direct |
| 14046 | // pointers to the first instruction in the code object |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 14047 | Handle<Object> p = it.rinfo()->target_object_handle(origin); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14048 | Code* code = Code::cast(*p); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14049 | it.rinfo()->set_target_address(code->instruction_start(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14050 | SKIP_WRITE_BARRIER, |
| 14051 | SKIP_ICACHE_FLUSH); |
| 14052 | } else if (RelocInfo::IsRuntimeEntry(mode)) { |
| 14053 | Address p = it.rinfo()->target_runtime_entry(origin); |
| 14054 | it.rinfo()->set_target_runtime_entry(p, SKIP_WRITE_BARRIER, |
| 14055 | SKIP_ICACHE_FLUSH); |
| 14056 | } else if (mode == RelocInfo::CODE_AGE_SEQUENCE) { |
| 14057 | Handle<Object> p = it.rinfo()->code_age_stub_handle(origin); |
| 14058 | Code* code = Code::cast(*p); |
| 14059 | it.rinfo()->set_code_age_stub(code, SKIP_ICACHE_FLUSH); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14060 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14061 | it.rinfo()->apply(delta); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14062 | } |
| 14063 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14064 | Assembler::FlushICache(GetIsolate(), instruction_start(), instruction_size()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14065 | } |
| 14066 | |
| 14067 | |
| 14068 | // Locate the source position which is closest to the address in the code. This |
| 14069 | // is using the source position information embedded in the relocation info. |
| 14070 | // The position returned is relative to the beginning of the script where the |
| 14071 | // source for this function is found. |
| 14072 | int Code::SourcePosition(Address pc) { |
| 14073 | int distance = kMaxInt; |
| 14074 | int position = RelocInfo::kNoPosition; // Initially no position found. |
| 14075 | // Run through all the relocation info to find the best matching source |
| 14076 | // position. All the code needs to be considered as the sequence of the |
| 14077 | // instructions in the code does not necessarily follow the same order as the |
| 14078 | // source. |
| 14079 | RelocIterator it(this, RelocInfo::kPositionMask); |
| 14080 | while (!it.done()) { |
| 14081 | // Only look at positions after the current pc. |
| 14082 | if (it.rinfo()->pc() < pc) { |
| 14083 | // Get position and distance. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 14084 | |
| 14085 | int dist = static_cast<int>(pc - it.rinfo()->pc()); |
| 14086 | int pos = static_cast<int>(it.rinfo()->data()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14087 | // If this position is closer than the current candidate or if it has the |
| 14088 | // same distance as the current candidate and the position is higher then |
| 14089 | // this position is the new candidate. |
| 14090 | if ((dist < distance) || |
| 14091 | (dist == distance && pos > position)) { |
| 14092 | position = pos; |
| 14093 | distance = dist; |
| 14094 | } |
| 14095 | } |
| 14096 | it.next(); |
| 14097 | } |
| 14098 | return position; |
| 14099 | } |
| 14100 | |
| 14101 | |
| 14102 | // Same as Code::SourcePosition above except it only looks for statement |
| 14103 | // positions. |
| 14104 | int Code::SourceStatementPosition(Address pc) { |
| 14105 | // First find the position as close as possible using all position |
| 14106 | // information. |
| 14107 | int position = SourcePosition(pc); |
| 14108 | // Now find the closest statement position before the position. |
| 14109 | int statement_position = 0; |
| 14110 | RelocIterator it(this, RelocInfo::kPositionMask); |
| 14111 | while (!it.done()) { |
| 14112 | if (RelocInfo::IsStatementPosition(it.rinfo()->rmode())) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 14113 | int p = static_cast<int>(it.rinfo()->data()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14114 | if (statement_position < p && p <= position) { |
| 14115 | statement_position = p; |
| 14116 | } |
| 14117 | } |
| 14118 | it.next(); |
| 14119 | } |
| 14120 | return statement_position; |
| 14121 | } |
| 14122 | |
| 14123 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 14124 | SafepointEntry Code::GetSafepointEntry(Address pc) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14125 | SafepointTable table(this); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 14126 | return table.FindEntry(pc); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14127 | } |
| 14128 | |
| 14129 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14130 | Object* Code::FindNthObject(int n, Map* match_map) { |
| 14131 | DCHECK(is_inline_cache_stub()); |
| 14132 | DisallowHeapAllocation no_allocation; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14133 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 14134 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 14135 | RelocInfo* info = it.rinfo(); |
| 14136 | Object* object = info->target_object(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14137 | if (object->IsWeakCell()) object = WeakCell::cast(object)->value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14138 | if (object->IsHeapObject()) { |
| 14139 | if (HeapObject::cast(object)->map() == match_map) { |
| 14140 | if (--n == 0) return object; |
| 14141 | } |
| 14142 | } |
| 14143 | } |
| 14144 | return NULL; |
| 14145 | } |
| 14146 | |
| 14147 | |
| 14148 | AllocationSite* Code::FindFirstAllocationSite() { |
| 14149 | Object* result = FindNthObject(1, GetHeap()->allocation_site_map()); |
| 14150 | return (result != NULL) ? AllocationSite::cast(result) : NULL; |
| 14151 | } |
| 14152 | |
| 14153 | |
| 14154 | Map* Code::FindFirstMap() { |
| 14155 | Object* result = FindNthObject(1, GetHeap()->meta_map()); |
| 14156 | return (result != NULL) ? Map::cast(result) : NULL; |
| 14157 | } |
| 14158 | |
| 14159 | |
| 14160 | void Code::FindAndReplace(const FindAndReplacePattern& pattern) { |
| 14161 | DCHECK(is_inline_cache_stub() || is_handler()); |
| 14162 | DisallowHeapAllocation no_allocation; |
| 14163 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 14164 | STATIC_ASSERT(FindAndReplacePattern::kMaxCount < 32); |
| 14165 | int current_pattern = 0; |
| 14166 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 14167 | RelocInfo* info = it.rinfo(); |
| 14168 | Object* object = info->target_object(); |
| 14169 | if (object->IsHeapObject()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14170 | if (object->IsWeakCell()) { |
| 14171 | object = HeapObject::cast(WeakCell::cast(object)->value()); |
| 14172 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14173 | Map* map = HeapObject::cast(object)->map(); |
| 14174 | if (map == *pattern.find_[current_pattern]) { |
| 14175 | info->set_target_object(*pattern.replace_[current_pattern]); |
| 14176 | if (++current_pattern == pattern.count_) return; |
| 14177 | } |
| 14178 | } |
| 14179 | } |
| 14180 | UNREACHABLE(); |
| 14181 | } |
| 14182 | |
| 14183 | |
| 14184 | void Code::FindAllMaps(MapHandleList* maps) { |
| 14185 | DCHECK(is_inline_cache_stub()); |
| 14186 | DisallowHeapAllocation no_allocation; |
| 14187 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 14188 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 14189 | RelocInfo* info = it.rinfo(); |
| 14190 | Object* object = info->target_object(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14191 | if (object->IsWeakCell()) object = WeakCell::cast(object)->value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14192 | if (object->IsMap()) maps->Add(handle(Map::cast(object))); |
| 14193 | } |
| 14194 | } |
| 14195 | |
| 14196 | |
| 14197 | Code* Code::FindFirstHandler() { |
| 14198 | DCHECK(is_inline_cache_stub()); |
| 14199 | DisallowHeapAllocation no_allocation; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14200 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) | |
| 14201 | RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 14202 | bool skip_next_handler = false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14203 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 14204 | RelocInfo* info = it.rinfo(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14205 | if (info->rmode() == RelocInfo::EMBEDDED_OBJECT) { |
| 14206 | Object* obj = info->target_object(); |
| 14207 | skip_next_handler |= obj->IsWeakCell() && WeakCell::cast(obj)->cleared(); |
| 14208 | } else { |
| 14209 | Code* code = Code::GetCodeFromTargetAddress(info->target_address()); |
| 14210 | if (code->kind() == Code::HANDLER) { |
| 14211 | if (!skip_next_handler) return code; |
| 14212 | skip_next_handler = false; |
| 14213 | } |
| 14214 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14215 | } |
| 14216 | return NULL; |
| 14217 | } |
| 14218 | |
| 14219 | |
| 14220 | bool Code::FindHandlers(CodeHandleList* code_list, int length) { |
| 14221 | DCHECK(is_inline_cache_stub()); |
| 14222 | DisallowHeapAllocation no_allocation; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14223 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) | |
| 14224 | RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 14225 | bool skip_next_handler = false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14226 | int i = 0; |
| 14227 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 14228 | if (i == length) return true; |
| 14229 | RelocInfo* info = it.rinfo(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14230 | if (info->rmode() == RelocInfo::EMBEDDED_OBJECT) { |
| 14231 | Object* obj = info->target_object(); |
| 14232 | skip_next_handler |= obj->IsWeakCell() && WeakCell::cast(obj)->cleared(); |
| 14233 | } else { |
| 14234 | Code* code = Code::GetCodeFromTargetAddress(info->target_address()); |
| 14235 | // IC stubs with handlers never contain non-handler code objects before |
| 14236 | // handler targets. |
| 14237 | if (code->kind() != Code::HANDLER) break; |
| 14238 | if (!skip_next_handler) { |
| 14239 | code_list->Add(Handle<Code>(code)); |
| 14240 | i++; |
| 14241 | } |
| 14242 | skip_next_handler = false; |
| 14243 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14244 | } |
| 14245 | return i == length; |
| 14246 | } |
| 14247 | |
| 14248 | |
| 14249 | MaybeHandle<Code> Code::FindHandlerForMap(Map* map) { |
| 14250 | DCHECK(is_inline_cache_stub()); |
| 14251 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) | |
| 14252 | RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 14253 | bool return_next = false; |
| 14254 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 14255 | RelocInfo* info = it.rinfo(); |
| 14256 | if (info->rmode() == RelocInfo::EMBEDDED_OBJECT) { |
| 14257 | Object* object = info->target_object(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14258 | if (object->IsWeakCell()) object = WeakCell::cast(object)->value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14259 | if (object == map) return_next = true; |
| 14260 | } else if (return_next) { |
| 14261 | Code* code = Code::GetCodeFromTargetAddress(info->target_address()); |
| 14262 | DCHECK(code->kind() == Code::HANDLER); |
| 14263 | return handle(code); |
| 14264 | } |
| 14265 | } |
| 14266 | return MaybeHandle<Code>(); |
| 14267 | } |
| 14268 | |
| 14269 | |
| 14270 | Name* Code::FindFirstName() { |
| 14271 | DCHECK(is_inline_cache_stub()); |
| 14272 | DisallowHeapAllocation no_allocation; |
| 14273 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 14274 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 14275 | RelocInfo* info = it.rinfo(); |
| 14276 | Object* object = info->target_object(); |
| 14277 | if (object->IsName()) return Name::cast(object); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14278 | } |
| 14279 | return NULL; |
| 14280 | } |
| 14281 | |
| 14282 | |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 14283 | void Code::ClearInlineCaches() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14284 | ClearInlineCaches(NULL); |
| 14285 | } |
| 14286 | |
| 14287 | |
| 14288 | void Code::ClearInlineCaches(Code::Kind kind) { |
| 14289 | ClearInlineCaches(&kind); |
| 14290 | } |
| 14291 | |
| 14292 | |
| 14293 | void Code::ClearInlineCaches(Code::Kind* kind) { |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 14294 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14295 | RelocInfo::ModeMask(RelocInfo::CODE_TARGET_WITH_ID); |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 14296 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 14297 | RelocInfo* info = it.rinfo(); |
| 14298 | Code* target(Code::GetCodeFromTargetAddress(info->target_address())); |
| 14299 | if (target->is_inline_cache_stub()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14300 | if (kind == NULL || *kind == target->kind()) { |
| 14301 | IC::Clear(this->GetIsolate(), info->pc(), |
| 14302 | info->host()->constant_pool()); |
| 14303 | } |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 14304 | } |
| 14305 | } |
| 14306 | } |
| 14307 | |
| 14308 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14309 | void SharedFunctionInfo::ClearTypeFeedbackInfo() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14310 | feedback_vector()->ClearSlots(this); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14311 | } |
| 14312 | |
| 14313 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14314 | void SharedFunctionInfo::ClearTypeFeedbackInfoAtGCTime() { |
| 14315 | feedback_vector()->ClearSlotsAtGCTime(this); |
| 14316 | } |
| 14317 | |
| 14318 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14319 | BailoutId Code::TranslatePcOffsetToAstId(uint32_t pc_offset) { |
| 14320 | DisallowHeapAllocation no_gc; |
| 14321 | DCHECK(kind() == FUNCTION); |
| 14322 | BackEdgeTable back_edges(this, &no_gc); |
| 14323 | for (uint32_t i = 0; i < back_edges.length(); i++) { |
| 14324 | if (back_edges.pc_offset(i) == pc_offset) return back_edges.ast_id(i); |
| 14325 | } |
| 14326 | return BailoutId::None(); |
| 14327 | } |
| 14328 | |
| 14329 | |
| 14330 | uint32_t Code::TranslateAstIdToPcOffset(BailoutId ast_id) { |
| 14331 | DisallowHeapAllocation no_gc; |
| 14332 | DCHECK(kind() == FUNCTION); |
| 14333 | BackEdgeTable back_edges(this, &no_gc); |
| 14334 | for (uint32_t i = 0; i < back_edges.length(); i++) { |
| 14335 | if (back_edges.ast_id(i) == ast_id) return back_edges.pc_offset(i); |
| 14336 | } |
| 14337 | UNREACHABLE(); // We expect to find the back edge. |
| 14338 | return 0; |
| 14339 | } |
| 14340 | |
| 14341 | |
| 14342 | void Code::MakeCodeAgeSequenceYoung(byte* sequence, Isolate* isolate) { |
| 14343 | PatchPlatformCodeAge(isolate, sequence, kNoAgeCodeAge, NO_MARKING_PARITY); |
| 14344 | } |
| 14345 | |
| 14346 | |
| 14347 | void Code::MarkCodeAsExecuted(byte* sequence, Isolate* isolate) { |
| 14348 | PatchPlatformCodeAge(isolate, sequence, kExecutedOnceCodeAge, |
| 14349 | NO_MARKING_PARITY); |
| 14350 | } |
| 14351 | |
| 14352 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14353 | // NextAge defines the Code::Age state transitions during a GC cycle. |
| 14354 | static Code::Age NextAge(Code::Age age) { |
| 14355 | switch (age) { |
| 14356 | case Code::kNotExecutedCodeAge: // Keep, until we've been executed. |
| 14357 | case Code::kToBeExecutedOnceCodeAge: // Keep, until we've been executed. |
| 14358 | case Code::kLastCodeAge: // Clamp at last Code::Age value. |
| 14359 | return age; |
| 14360 | case Code::kExecutedOnceCodeAge: |
| 14361 | // Pre-age code that has only been executed once. |
| 14362 | return static_cast<Code::Age>(Code::kPreAgedCodeAge + 1); |
| 14363 | default: |
| 14364 | return static_cast<Code::Age>(age + 1); // Default case: Increase age. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14365 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14366 | } |
| 14367 | |
| 14368 | |
| 14369 | // IsOldAge defines the collection criteria for a Code object. |
| 14370 | static bool IsOldAge(Code::Age age) { |
| 14371 | return age >= Code::kIsOldCodeAge || age == Code::kNotExecutedCodeAge; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14372 | } |
| 14373 | |
| 14374 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14375 | void Code::MakeYoung(Isolate* isolate) { |
| 14376 | byte* sequence = FindCodeAgeSequence(); |
| 14377 | if (sequence != NULL) MakeCodeAgeSequenceYoung(sequence, isolate); |
| 14378 | } |
| 14379 | |
| 14380 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14381 | void Code::MarkToBeExecutedOnce(Isolate* isolate) { |
| 14382 | byte* sequence = FindCodeAgeSequence(); |
| 14383 | if (sequence != NULL) { |
| 14384 | PatchPlatformCodeAge(isolate, sequence, kToBeExecutedOnceCodeAge, |
| 14385 | NO_MARKING_PARITY); |
| 14386 | } |
| 14387 | } |
| 14388 | |
| 14389 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14390 | void Code::MakeOlder(MarkingParity current_parity) { |
| 14391 | byte* sequence = FindCodeAgeSequence(); |
| 14392 | if (sequence != NULL) { |
| 14393 | Age age; |
| 14394 | MarkingParity code_parity; |
| 14395 | Isolate* isolate = GetIsolate(); |
| 14396 | GetCodeAgeAndParity(isolate, sequence, &age, &code_parity); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14397 | Age next_age = NextAge(age); |
| 14398 | if (age != next_age && code_parity != current_parity) { |
| 14399 | PatchPlatformCodeAge(isolate, sequence, next_age, current_parity); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14400 | } |
| 14401 | } |
| 14402 | } |
| 14403 | |
| 14404 | |
| 14405 | bool Code::IsOld() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14406 | return IsOldAge(GetAge()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14407 | } |
| 14408 | |
| 14409 | |
| 14410 | byte* Code::FindCodeAgeSequence() { |
| 14411 | return FLAG_age_code && |
| 14412 | prologue_offset() != Code::kPrologueOffsetNotSet && |
| 14413 | (kind() == OPTIMIZED_FUNCTION || |
| 14414 | (kind() == FUNCTION && !has_debug_break_slots())) |
| 14415 | ? instruction_start() + prologue_offset() |
| 14416 | : NULL; |
| 14417 | } |
| 14418 | |
| 14419 | |
| 14420 | Code::Age Code::GetAge() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14421 | byte* sequence = FindCodeAgeSequence(); |
| 14422 | if (sequence == NULL) { |
| 14423 | return kNoAgeCodeAge; |
| 14424 | } |
| 14425 | Age age; |
| 14426 | MarkingParity parity; |
| 14427 | GetCodeAgeAndParity(GetIsolate(), sequence, &age, &parity); |
| 14428 | return age; |
| 14429 | } |
| 14430 | |
| 14431 | |
| 14432 | void Code::GetCodeAgeAndParity(Code* code, Age* age, |
| 14433 | MarkingParity* parity) { |
| 14434 | Isolate* isolate = code->GetIsolate(); |
| 14435 | Builtins* builtins = isolate->builtins(); |
| 14436 | Code* stub = NULL; |
| 14437 | #define HANDLE_CODE_AGE(AGE) \ |
| 14438 | stub = *builtins->Make##AGE##CodeYoungAgainEvenMarking(); \ |
| 14439 | if (code == stub) { \ |
| 14440 | *age = k##AGE##CodeAge; \ |
| 14441 | *parity = EVEN_MARKING_PARITY; \ |
| 14442 | return; \ |
| 14443 | } \ |
| 14444 | stub = *builtins->Make##AGE##CodeYoungAgainOddMarking(); \ |
| 14445 | if (code == stub) { \ |
| 14446 | *age = k##AGE##CodeAge; \ |
| 14447 | *parity = ODD_MARKING_PARITY; \ |
| 14448 | return; \ |
| 14449 | } |
| 14450 | CODE_AGE_LIST(HANDLE_CODE_AGE) |
| 14451 | #undef HANDLE_CODE_AGE |
| 14452 | stub = *builtins->MarkCodeAsExecutedOnce(); |
| 14453 | if (code == stub) { |
| 14454 | *age = kNotExecutedCodeAge; |
| 14455 | *parity = NO_MARKING_PARITY; |
| 14456 | return; |
| 14457 | } |
| 14458 | stub = *builtins->MarkCodeAsExecutedTwice(); |
| 14459 | if (code == stub) { |
| 14460 | *age = kExecutedOnceCodeAge; |
| 14461 | *parity = NO_MARKING_PARITY; |
| 14462 | return; |
| 14463 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14464 | stub = *builtins->MarkCodeAsToBeExecutedOnce(); |
| 14465 | if (code == stub) { |
| 14466 | *age = kToBeExecutedOnceCodeAge; |
| 14467 | *parity = NO_MARKING_PARITY; |
| 14468 | return; |
| 14469 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14470 | UNREACHABLE(); |
| 14471 | } |
| 14472 | |
| 14473 | |
| 14474 | Code* Code::GetCodeAgeStub(Isolate* isolate, Age age, MarkingParity parity) { |
| 14475 | Builtins* builtins = isolate->builtins(); |
| 14476 | switch (age) { |
| 14477 | #define HANDLE_CODE_AGE(AGE) \ |
| 14478 | case k##AGE##CodeAge: { \ |
| 14479 | Code* stub = parity == EVEN_MARKING_PARITY \ |
| 14480 | ? *builtins->Make##AGE##CodeYoungAgainEvenMarking() \ |
| 14481 | : *builtins->Make##AGE##CodeYoungAgainOddMarking(); \ |
| 14482 | return stub; \ |
| 14483 | } |
| 14484 | CODE_AGE_LIST(HANDLE_CODE_AGE) |
| 14485 | #undef HANDLE_CODE_AGE |
| 14486 | case kNotExecutedCodeAge: { |
| 14487 | DCHECK(parity == NO_MARKING_PARITY); |
| 14488 | return *builtins->MarkCodeAsExecutedOnce(); |
| 14489 | } |
| 14490 | case kExecutedOnceCodeAge: { |
| 14491 | DCHECK(parity == NO_MARKING_PARITY); |
| 14492 | return *builtins->MarkCodeAsExecutedTwice(); |
| 14493 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14494 | case kToBeExecutedOnceCodeAge: { |
| 14495 | DCHECK(parity == NO_MARKING_PARITY); |
| 14496 | return *builtins->MarkCodeAsToBeExecutedOnce(); |
| 14497 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14498 | default: |
| 14499 | UNREACHABLE(); |
| 14500 | break; |
| 14501 | } |
| 14502 | return NULL; |
| 14503 | } |
| 14504 | |
| 14505 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14506 | void Code::PrintDeoptLocation(FILE* out, Address pc) { |
| 14507 | Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(this, pc); |
| 14508 | class SourcePosition pos = info.position; |
| 14509 | if (info.deopt_reason != Deoptimizer::kNoReason || !pos.IsUnknown()) { |
| 14510 | if (FLAG_hydrogen_track_positions) { |
| 14511 | PrintF(out, " ;;; deoptimize at %d_%d: %s\n", |
| 14512 | pos.inlining_id(), pos.position(), |
| 14513 | Deoptimizer::GetDeoptReason(info.deopt_reason)); |
| 14514 | } else { |
| 14515 | PrintF(out, " ;;; deoptimize at %d: %s\n", pos.raw(), |
| 14516 | Deoptimizer::GetDeoptReason(info.deopt_reason)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14517 | } |
| 14518 | } |
| 14519 | } |
| 14520 | |
| 14521 | |
| 14522 | bool Code::CanDeoptAt(Address pc) { |
| 14523 | DeoptimizationInputData* deopt_data = |
| 14524 | DeoptimizationInputData::cast(deoptimization_data()); |
| 14525 | Address code_start_address = instruction_start(); |
| 14526 | for (int i = 0; i < deopt_data->DeoptCount(); i++) { |
| 14527 | if (deopt_data->Pc(i)->value() == -1) continue; |
| 14528 | Address address = code_start_address + deopt_data->Pc(i)->value(); |
| 14529 | if (address == pc) return true; |
| 14530 | } |
| 14531 | return false; |
| 14532 | } |
| 14533 | |
| 14534 | |
| 14535 | // Identify kind of code. |
| 14536 | const char* Code::Kind2String(Kind kind) { |
| 14537 | switch (kind) { |
| 14538 | #define CASE(name) case name: return #name; |
| 14539 | CODE_KIND_LIST(CASE) |
| 14540 | #undef CASE |
| 14541 | case NUMBER_OF_KINDS: break; |
| 14542 | } |
| 14543 | UNREACHABLE(); |
| 14544 | return NULL; |
| 14545 | } |
| 14546 | |
| 14547 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14548 | Handle<WeakCell> Code::WeakCellFor(Handle<Code> code) { |
| 14549 | DCHECK(code->kind() == OPTIMIZED_FUNCTION); |
| 14550 | WeakCell* raw_cell = code->CachedWeakCell(); |
| 14551 | if (raw_cell != NULL) return Handle<WeakCell>(raw_cell); |
| 14552 | Handle<WeakCell> cell = code->GetIsolate()->factory()->NewWeakCell(code); |
| 14553 | DeoptimizationInputData::cast(code->deoptimization_data()) |
| 14554 | ->SetWeakCellCache(*cell); |
| 14555 | return cell; |
| 14556 | } |
| 14557 | |
| 14558 | |
| 14559 | WeakCell* Code::CachedWeakCell() { |
| 14560 | DCHECK(kind() == OPTIMIZED_FUNCTION); |
| 14561 | Object* weak_cell_cache = |
| 14562 | DeoptimizationInputData::cast(deoptimization_data())->WeakCellCache(); |
| 14563 | if (weak_cell_cache->IsWeakCell()) { |
| 14564 | DCHECK(this == WeakCell::cast(weak_cell_cache)->value()); |
| 14565 | return WeakCell::cast(weak_cell_cache); |
| 14566 | } |
| 14567 | return NULL; |
| 14568 | } |
| 14569 | |
| 14570 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14571 | #ifdef ENABLE_DISASSEMBLER |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14572 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14573 | void DeoptimizationInputData::DeoptimizationInputDataPrint( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14574 | std::ostream& os) { // NOLINT |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14575 | disasm::NameConverter converter; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14576 | int const inlined_function_count = InlinedFunctionCount()->value(); |
| 14577 | os << "Inlined functions (count = " << inlined_function_count << ")\n"; |
| 14578 | for (int id = 0; id < inlined_function_count; ++id) { |
| 14579 | Object* info = LiteralArray()->get(id); |
| 14580 | os << " " << Brief(SharedFunctionInfo::cast(info)) << "\n"; |
| 14581 | } |
| 14582 | os << "\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14583 | int deopt_count = DeoptCount(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14584 | os << "Deoptimization Input Data (deopt points = " << deopt_count << ")\n"; |
| 14585 | if (0 != deopt_count) { |
| 14586 | os << " index ast id argc pc"; |
| 14587 | if (FLAG_print_code_verbose) os << " commands"; |
| 14588 | os << "\n"; |
| 14589 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14590 | for (int i = 0; i < deopt_count; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14591 | os << std::setw(6) << i << " " << std::setw(6) << AstId(i).ToInt() << " " |
| 14592 | << std::setw(6) << ArgumentsStackHeight(i)->value() << " " |
| 14593 | << std::setw(6) << Pc(i)->value(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14594 | |
| 14595 | if (!FLAG_print_code_verbose) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14596 | os << "\n"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14597 | continue; |
| 14598 | } |
| 14599 | // Print details of the frame translation. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14600 | int translation_index = TranslationIndex(i)->value(); |
| 14601 | TranslationIterator iterator(TranslationByteArray(), translation_index); |
| 14602 | Translation::Opcode opcode = |
| 14603 | static_cast<Translation::Opcode>(iterator.Next()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14604 | DCHECK(Translation::BEGIN == opcode); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14605 | int frame_count = iterator.Next(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14606 | int jsframe_count = iterator.Next(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14607 | os << " " << Translation::StringFor(opcode) |
| 14608 | << " {frame count=" << frame_count |
| 14609 | << ", js frame count=" << jsframe_count << "}\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14610 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14611 | while (iterator.HasNext() && |
| 14612 | Translation::BEGIN != |
| 14613 | (opcode = static_cast<Translation::Opcode>(iterator.Next()))) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14614 | os << std::setw(31) << " " << Translation::StringFor(opcode) << " "; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14615 | |
| 14616 | switch (opcode) { |
| 14617 | case Translation::BEGIN: |
| 14618 | UNREACHABLE(); |
| 14619 | break; |
| 14620 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14621 | case Translation::JS_FRAME: { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14622 | int ast_id = iterator.Next(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14623 | int shared_info_id = iterator.Next(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14624 | unsigned height = iterator.Next(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14625 | Object* shared_info = LiteralArray()->get(shared_info_id); |
| 14626 | os << "{ast_id=" << ast_id << ", function=" |
| 14627 | << Brief(SharedFunctionInfo::cast(shared_info)->DebugName()) |
| 14628 | << ", height=" << height << "}"; |
| 14629 | break; |
| 14630 | } |
| 14631 | |
| 14632 | case Translation::INTERPRETED_FRAME: { |
| 14633 | int bytecode_offset = iterator.Next(); |
| 14634 | int shared_info_id = iterator.Next(); |
| 14635 | unsigned height = iterator.Next(); |
| 14636 | Object* shared_info = LiteralArray()->get(shared_info_id); |
| 14637 | os << "{bytecode_offset=" << bytecode_offset << ", function=" |
| 14638 | << Brief(SharedFunctionInfo::cast(shared_info)->DebugName()) |
| 14639 | << ", height=" << height << "}"; |
| 14640 | break; |
| 14641 | } |
| 14642 | |
| 14643 | case Translation::JS_FRAME_FUNCTION: { |
| 14644 | os << "{function}"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14645 | break; |
| 14646 | } |
| 14647 | |
| 14648 | case Translation::COMPILED_STUB_FRAME: { |
| 14649 | Code::Kind stub_kind = static_cast<Code::Kind>(iterator.Next()); |
| 14650 | os << "{kind=" << stub_kind << "}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14651 | break; |
| 14652 | } |
| 14653 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14654 | case Translation::ARGUMENTS_ADAPTOR_FRAME: |
| 14655 | case Translation::CONSTRUCT_STUB_FRAME: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14656 | int shared_info_id = iterator.Next(); |
| 14657 | Object* shared_info = LiteralArray()->get(shared_info_id); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14658 | unsigned height = iterator.Next(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14659 | os << "{function=" |
| 14660 | << Brief(SharedFunctionInfo::cast(shared_info)->DebugName()) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14661 | << ", height=" << height << "}"; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14662 | break; |
| 14663 | } |
| 14664 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14665 | case Translation::GETTER_STUB_FRAME: |
| 14666 | case Translation::SETTER_STUB_FRAME: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14667 | int shared_info_id = iterator.Next(); |
| 14668 | Object* shared_info = LiteralArray()->get(shared_info_id); |
| 14669 | os << "{function=" << Brief(SharedFunctionInfo::cast(shared_info) |
| 14670 | ->DebugName()) << "}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14671 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14672 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14673 | |
| 14674 | case Translation::REGISTER: { |
| 14675 | int reg_code = iterator.Next(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14676 | os << "{input=" << converter.NameOfCPURegister(reg_code) << "}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14677 | break; |
| 14678 | } |
| 14679 | |
| 14680 | case Translation::INT32_REGISTER: { |
| 14681 | int reg_code = iterator.Next(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14682 | os << "{input=" << converter.NameOfCPURegister(reg_code) << "}"; |
| 14683 | break; |
| 14684 | } |
| 14685 | |
| 14686 | case Translation::UINT32_REGISTER: { |
| 14687 | int reg_code = iterator.Next(); |
| 14688 | os << "{input=" << converter.NameOfCPURegister(reg_code) |
| 14689 | << " (unsigned)}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14690 | break; |
| 14691 | } |
| 14692 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14693 | case Translation::BOOL_REGISTER: { |
| 14694 | int reg_code = iterator.Next(); |
| 14695 | os << "{input=" << converter.NameOfCPURegister(reg_code) |
| 14696 | << " (bool)}"; |
| 14697 | break; |
| 14698 | } |
| 14699 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14700 | case Translation::DOUBLE_REGISTER: { |
| 14701 | int reg_code = iterator.Next(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14702 | os << "{input=" << DoubleRegister::from_code(reg_code).ToString() |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14703 | << "}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14704 | break; |
| 14705 | } |
| 14706 | |
| 14707 | case Translation::STACK_SLOT: { |
| 14708 | int input_slot_index = iterator.Next(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14709 | os << "{input=" << input_slot_index << "}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14710 | break; |
| 14711 | } |
| 14712 | |
| 14713 | case Translation::INT32_STACK_SLOT: { |
| 14714 | int input_slot_index = iterator.Next(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14715 | os << "{input=" << input_slot_index << "}"; |
| 14716 | break; |
| 14717 | } |
| 14718 | |
| 14719 | case Translation::UINT32_STACK_SLOT: { |
| 14720 | int input_slot_index = iterator.Next(); |
| 14721 | os << "{input=" << input_slot_index << " (unsigned)}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14722 | break; |
| 14723 | } |
| 14724 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14725 | case Translation::BOOL_STACK_SLOT: { |
| 14726 | int input_slot_index = iterator.Next(); |
| 14727 | os << "{input=" << input_slot_index << " (bool)}"; |
| 14728 | break; |
| 14729 | } |
| 14730 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14731 | case Translation::DOUBLE_STACK_SLOT: { |
| 14732 | int input_slot_index = iterator.Next(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14733 | os << "{input=" << input_slot_index << "}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14734 | break; |
| 14735 | } |
| 14736 | |
| 14737 | case Translation::LITERAL: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14738 | int literal_index = iterator.Next(); |
| 14739 | Object* literal_value = LiteralArray()->get(literal_index); |
| 14740 | os << "{literal_id=" << literal_index << " (" << Brief(literal_value) |
| 14741 | << ")}"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14742 | break; |
| 14743 | } |
| 14744 | |
| 14745 | case Translation::DUPLICATED_OBJECT: { |
| 14746 | int object_index = iterator.Next(); |
| 14747 | os << "{object_index=" << object_index << "}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14748 | break; |
| 14749 | } |
| 14750 | |
| 14751 | case Translation::ARGUMENTS_OBJECT: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14752 | case Translation::CAPTURED_OBJECT: { |
| 14753 | int args_length = iterator.Next(); |
| 14754 | os << "{length=" << args_length << "}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14755 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14756 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14757 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14758 | os << "\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14759 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14760 | } |
| 14761 | } |
| 14762 | |
| 14763 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14764 | void DeoptimizationOutputData::DeoptimizationOutputDataPrint( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14765 | std::ostream& os) { // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14766 | os << "Deoptimization Output Data (deopt points = " << this->DeoptPoints() |
| 14767 | << ")\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14768 | if (this->DeoptPoints() == 0) return; |
| 14769 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14770 | os << "ast id pc state\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14771 | for (int i = 0; i < this->DeoptPoints(); i++) { |
| 14772 | int pc_and_state = this->PcAndState(i)->value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14773 | os << std::setw(6) << this->AstId(i).ToInt() << " " << std::setw(8) |
| 14774 | << FullCodeGenerator::PcField::decode(pc_and_state) << " " |
| 14775 | << FullCodeGenerator::State2String( |
| 14776 | FullCodeGenerator::StateField::decode(pc_and_state)) << "\n"; |
| 14777 | } |
| 14778 | } |
| 14779 | |
| 14780 | |
| 14781 | void HandlerTable::HandlerTableRangePrint(std::ostream& os) { |
| 14782 | os << " from to hdlr\n"; |
| 14783 | for (int i = 0; i < length(); i += kRangeEntrySize) { |
| 14784 | int pc_start = Smi::cast(get(i + kRangeStartIndex))->value(); |
| 14785 | int pc_end = Smi::cast(get(i + kRangeEndIndex))->value(); |
| 14786 | int handler_field = Smi::cast(get(i + kRangeHandlerIndex))->value(); |
| 14787 | int handler_offset = HandlerOffsetField::decode(handler_field); |
| 14788 | CatchPrediction prediction = HandlerPredictionField::decode(handler_field); |
| 14789 | int depth = Smi::cast(get(i + kRangeDepthIndex))->value(); |
| 14790 | os << " (" << std::setw(4) << pc_start << "," << std::setw(4) << pc_end |
| 14791 | << ") -> " << std::setw(4) << handler_offset |
| 14792 | << " (prediction=" << prediction << ", depth=" << depth << ")\n"; |
| 14793 | } |
| 14794 | } |
| 14795 | |
| 14796 | |
| 14797 | void HandlerTable::HandlerTableReturnPrint(std::ostream& os) { |
| 14798 | os << " off hdlr (c)\n"; |
| 14799 | for (int i = 0; i < length(); i += kReturnEntrySize) { |
| 14800 | int pc_offset = Smi::cast(get(i + kReturnOffsetIndex))->value(); |
| 14801 | int handler_field = Smi::cast(get(i + kReturnHandlerIndex))->value(); |
| 14802 | int handler_offset = HandlerOffsetField::decode(handler_field); |
| 14803 | CatchPrediction prediction = HandlerPredictionField::decode(handler_field); |
| 14804 | os << " " << std::setw(4) << pc_offset << " -> " << std::setw(4) |
| 14805 | << handler_offset << " (prediction=" << prediction << ")\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14806 | } |
| 14807 | } |
| 14808 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14809 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14810 | const char* Code::ICState2String(InlineCacheState state) { |
| 14811 | switch (state) { |
| 14812 | case UNINITIALIZED: return "UNINITIALIZED"; |
| 14813 | case PREMONOMORPHIC: return "PREMONOMORPHIC"; |
| 14814 | case MONOMORPHIC: return "MONOMORPHIC"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14815 | case PROTOTYPE_FAILURE: |
| 14816 | return "PROTOTYPE_FAILURE"; |
| 14817 | case POLYMORPHIC: return "POLYMORPHIC"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14818 | case MEGAMORPHIC: return "MEGAMORPHIC"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14819 | case GENERIC: return "GENERIC"; |
| 14820 | case DEBUG_STUB: return "DEBUG_STUB"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14821 | } |
| 14822 | UNREACHABLE(); |
| 14823 | return NULL; |
| 14824 | } |
| 14825 | |
| 14826 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14827 | const char* Code::StubType2String(StubType type) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14828 | switch (type) { |
| 14829 | case NORMAL: return "NORMAL"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14830 | case FAST: return "FAST"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14831 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14832 | UNREACHABLE(); // keep the compiler happy |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14833 | return NULL; |
| 14834 | } |
| 14835 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14836 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14837 | void Code::PrintExtraICState(std::ostream& os, // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14838 | Kind kind, ExtraICState extra) { |
| 14839 | os << "extra_ic_state = "; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14840 | if ((kind == STORE_IC || kind == KEYED_STORE_IC) && |
| 14841 | is_strict(static_cast<LanguageMode>(extra))) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14842 | os << "STRICT\n"; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 14843 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14844 | os << extra << "\n"; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 14845 | } |
| 14846 | } |
| 14847 | |
| 14848 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14849 | void Code::Disassemble(const char* name, std::ostream& os) { // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14850 | os << "kind = " << Kind2String(kind()) << "\n"; |
| 14851 | if (IsCodeStubOrIC()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14852 | const char* n = CodeStub::MajorName(CodeStub::GetMajorKey(this)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14853 | os << "major_key = " << (n == NULL ? "null" : n) << "\n"; |
| 14854 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14855 | if (is_inline_cache_stub()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14856 | os << "ic_state = " << ICState2String(ic_state()) << "\n"; |
| 14857 | PrintExtraICState(os, kind(), extra_ic_state()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14858 | if (ic_state() == MONOMORPHIC) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14859 | os << "type = " << StubType2String(type()) << "\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14860 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14861 | if (is_compare_ic_stub()) { |
| 14862 | DCHECK(CodeStub::GetMajorKey(this) == CodeStub::CompareIC); |
| 14863 | CompareICStub stub(stub_key(), GetIsolate()); |
| 14864 | os << "compare_state = " << CompareICState::GetStateName(stub.left()) |
| 14865 | << "*" << CompareICState::GetStateName(stub.right()) << " -> " |
| 14866 | << CompareICState::GetStateName(stub.state()) << "\n"; |
| 14867 | os << "compare_operation = " << Token::Name(stub.op()) << "\n"; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 14868 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14869 | } |
| 14870 | if ((name != NULL) && (name[0] != '\0')) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14871 | os << "name = " << name << "\n"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14872 | } else if (kind() == BUILTIN) { |
| 14873 | name = GetIsolate()->builtins()->Lookup(instruction_start()); |
| 14874 | if (name != NULL) { |
| 14875 | os << "name = " << name << "\n"; |
| 14876 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14877 | } |
| 14878 | if (kind() == OPTIMIZED_FUNCTION) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14879 | os << "stack_slots = " << stack_slots() << "\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14880 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14881 | os << "compiler = " << (is_turbofanned() |
| 14882 | ? "turbofan" |
| 14883 | : is_crankshafted() ? "crankshaft" |
| 14884 | : kind() == Code::FUNCTION |
| 14885 | ? "full-codegen" |
| 14886 | : "unknown") << "\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14887 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14888 | os << "Instructions (size = " << instruction_size() << ")\n"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14889 | { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14890 | Isolate* isolate = GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14891 | int size = instruction_size(); |
| 14892 | int safepoint_offset = |
| 14893 | is_crankshafted() ? static_cast<int>(safepoint_table_offset()) : size; |
| 14894 | int back_edge_offset = (kind() == Code::FUNCTION) |
| 14895 | ? static_cast<int>(back_edge_table_offset()) |
| 14896 | : size; |
| 14897 | int constant_pool_offset = FLAG_enable_embedded_constant_pool |
| 14898 | ? this->constant_pool_offset() |
| 14899 | : size; |
| 14900 | |
| 14901 | // Stop before reaching any embedded tables |
| 14902 | int code_size = Min(safepoint_offset, back_edge_offset); |
| 14903 | code_size = Min(code_size, constant_pool_offset); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14904 | byte* begin = instruction_start(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14905 | byte* end = begin + code_size; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14906 | Disassembler::Decode(isolate, &os, begin, end, this); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14907 | |
| 14908 | if (constant_pool_offset < size) { |
| 14909 | int constant_pool_size = size - constant_pool_offset; |
| 14910 | DCHECK((constant_pool_size & kPointerAlignmentMask) == 0); |
| 14911 | os << "\nConstant Pool (size = " << constant_pool_size << ")\n"; |
| 14912 | Vector<char> buf = Vector<char>::New(50); |
| 14913 | intptr_t* ptr = reinterpret_cast<intptr_t*>(begin + constant_pool_offset); |
| 14914 | for (int i = 0; i < constant_pool_size; i += kPointerSize, ptr++) { |
| 14915 | SNPrintF(buf, "%4d %08" V8PRIxPTR, i, *ptr); |
| 14916 | os << static_cast<const void*>(ptr) << " " << buf.start() << "\n"; |
| 14917 | } |
| 14918 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14919 | } |
| 14920 | os << "\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14921 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14922 | if (kind() == FUNCTION) { |
| 14923 | DeoptimizationOutputData* data = |
| 14924 | DeoptimizationOutputData::cast(this->deoptimization_data()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14925 | data->DeoptimizationOutputDataPrint(os); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14926 | } else if (kind() == OPTIMIZED_FUNCTION) { |
| 14927 | DeoptimizationInputData* data = |
| 14928 | DeoptimizationInputData::cast(this->deoptimization_data()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14929 | data->DeoptimizationInputDataPrint(os); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14930 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14931 | os << "\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14932 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14933 | if (is_crankshafted()) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14934 | SafepointTable table(this); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14935 | os << "Safepoints (size = " << table.size() << ")\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14936 | for (unsigned i = 0; i < table.length(); i++) { |
| 14937 | unsigned pc_offset = table.GetPcOffset(i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14938 | os << static_cast<const void*>(instruction_start() + pc_offset) << " "; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14939 | os << std::setw(4) << pc_offset << " "; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14940 | table.PrintEntry(i, os); |
| 14941 | os << " (sp -> fp) "; |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 14942 | SafepointEntry entry = table.GetEntry(i); |
| 14943 | if (entry.deoptimization_index() != Safepoint::kNoDeoptimizationIndex) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14944 | os << std::setw(6) << entry.deoptimization_index(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14945 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14946 | os << "<none>"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14947 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 14948 | if (entry.argument_count() > 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14949 | os << " argc: " << entry.argument_count(); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 14950 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14951 | os << "\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14952 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14953 | os << "\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14954 | } else if (kind() == FUNCTION) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14955 | unsigned offset = back_edge_table_offset(); |
| 14956 | // If there is no back edge table, the "table start" will be at or after |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14957 | // (due to alignment) the end of the instruction stream. |
| 14958 | if (static_cast<int>(offset) < instruction_size()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14959 | DisallowHeapAllocation no_gc; |
| 14960 | BackEdgeTable back_edges(this, &no_gc); |
| 14961 | |
| 14962 | os << "Back edges (size = " << back_edges.length() << ")\n"; |
| 14963 | os << "ast_id pc_offset loop_depth\n"; |
| 14964 | |
| 14965 | for (uint32_t i = 0; i < back_edges.length(); i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14966 | os << std::setw(6) << back_edges.ast_id(i).ToInt() << " " |
| 14967 | << std::setw(9) << back_edges.pc_offset(i) << " " << std::setw(10) |
| 14968 | << back_edges.loop_depth(i) << "\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14969 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14970 | |
| 14971 | os << "\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14972 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14973 | #ifdef OBJECT_PRINT |
| 14974 | if (!type_feedback_info()->IsUndefined()) { |
| 14975 | OFStream os(stdout); |
| 14976 | TypeFeedbackInfo::cast(type_feedback_info())->TypeFeedbackInfoPrint(os); |
| 14977 | os << "\n"; |
| 14978 | } |
| 14979 | #endif |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14980 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14981 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 14982 | if (handler_table()->length() > 0) { |
| 14983 | os << "Handler Table (size = " << handler_table()->Size() << ")\n"; |
| 14984 | if (kind() == FUNCTION) { |
| 14985 | HandlerTable::cast(handler_table())->HandlerTableRangePrint(os); |
| 14986 | } else if (kind() == OPTIMIZED_FUNCTION) { |
| 14987 | HandlerTable::cast(handler_table())->HandlerTableReturnPrint(os); |
| 14988 | } |
| 14989 | os << "\n"; |
| 14990 | } |
| 14991 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14992 | os << "RelocInfo (size = " << relocation_size() << ")\n"; |
| 14993 | for (RelocIterator it(this); !it.done(); it.next()) { |
| 14994 | it.rinfo()->Print(GetIsolate(), os); |
| 14995 | } |
| 14996 | os << "\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14997 | } |
| 14998 | #endif // ENABLE_DISASSEMBLER |
| 14999 | |
| 15000 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15001 | void BytecodeArray::Disassemble(std::ostream& os) { |
| 15002 | os << "Parameter count " << parameter_count() << "\n"; |
| 15003 | os << "Frame size " << frame_size() << "\n"; |
| 15004 | Vector<char> buf = Vector<char>::New(50); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 15005 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15006 | const uint8_t* first_bytecode_address = GetFirstBytecodeAddress(); |
| 15007 | int bytecode_size = 0; |
| 15008 | for (int i = 0; i < this->length(); i += bytecode_size) { |
| 15009 | const uint8_t* bytecode_start = &first_bytecode_address[i]; |
| 15010 | interpreter::Bytecode bytecode = |
| 15011 | interpreter::Bytecodes::FromByte(bytecode_start[0]); |
| 15012 | bytecode_size = interpreter::Bytecodes::Size(bytecode); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 15013 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15014 | SNPrintF(buf, "%p", bytecode_start); |
| 15015 | os << buf.start() << " : "; |
| 15016 | interpreter::Bytecodes::Decode(os, bytecode_start, parameter_count()); |
| 15017 | |
| 15018 | if (interpreter::Bytecodes::IsJumpConstantWide(bytecode)) { |
| 15019 | DCHECK_EQ(bytecode_size, 3); |
| 15020 | int index = static_cast<int>(ReadUnalignedUInt16(bytecode_start + 1)); |
| 15021 | int offset = Smi::cast(constant_pool()->get(index))->value(); |
| 15022 | SNPrintF(buf, " (%p)", bytecode_start + offset); |
| 15023 | os << buf.start(); |
| 15024 | } else if (interpreter::Bytecodes::IsJumpConstant(bytecode)) { |
| 15025 | DCHECK_EQ(bytecode_size, 2); |
| 15026 | int index = static_cast<int>(bytecode_start[1]); |
| 15027 | int offset = Smi::cast(constant_pool()->get(index))->value(); |
| 15028 | SNPrintF(buf, " (%p)", bytecode_start + offset); |
| 15029 | os << buf.start(); |
| 15030 | } else if (interpreter::Bytecodes::IsJump(bytecode)) { |
| 15031 | DCHECK_EQ(bytecode_size, 2); |
| 15032 | int offset = static_cast<int8_t>(bytecode_start[1]); |
| 15033 | SNPrintF(buf, " (%p)", bytecode_start + offset); |
| 15034 | os << buf.start(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15035 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15036 | os << "\n"; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 15037 | } |
| 15038 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15039 | os << "Constant pool (size = " << constant_pool()->length() << ")\n"; |
| 15040 | constant_pool()->Print(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15041 | } |
| 15042 | |
| 15043 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15044 | // static |
| 15045 | void JSArray::Initialize(Handle<JSArray> array, int capacity, int length) { |
| 15046 | DCHECK(capacity >= 0); |
| 15047 | array->GetIsolate()->factory()->NewJSArrayStorage( |
| 15048 | array, length, capacity, INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE); |
| 15049 | } |
| 15050 | |
| 15051 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15052 | // Returns false if the passed-in index is marked non-configurable, which will |
| 15053 | // cause the truncation operation to halt, and thus no further old values need |
| 15054 | // be collected. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15055 | static bool GetOldValue(Isolate* isolate, |
| 15056 | Handle<JSObject> object, |
| 15057 | uint32_t index, |
| 15058 | List<Handle<Object> >* old_values, |
| 15059 | List<uint32_t>* indices) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15060 | LookupIterator it(isolate, object, index, LookupIterator::HIDDEN); |
| 15061 | CHECK(JSReceiver::GetPropertyAttributes(&it).IsJust()); |
| 15062 | DCHECK(it.IsFound()); |
| 15063 | if (!it.IsConfigurable()) return false; |
| 15064 | Handle<Object> value = |
| 15065 | it.state() == LookupIterator::ACCESSOR |
| 15066 | ? Handle<Object>::cast(isolate->factory()->the_hole_value()) |
| 15067 | : JSReceiver::GetDataProperty(&it); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15068 | old_values->Add(value); |
| 15069 | indices->Add(index); |
| 15070 | return true; |
| 15071 | } |
| 15072 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15073 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15074 | void JSArray::SetLength(Handle<JSArray> array, uint32_t new_length) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 15075 | // We should never end in here with a pixel or external array. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15076 | DCHECK(array->AllowsSetLength()); |
| 15077 | if (array->SetLengthWouldNormalize(new_length)) { |
| 15078 | JSObject::NormalizeElements(array); |
| 15079 | } |
| 15080 | array->GetElementsAccessor()->SetLength(array, new_length); |
| 15081 | } |
| 15082 | |
| 15083 | |
| 15084 | MaybeHandle<Object> JSArray::ObservableSetLength(Handle<JSArray> array, |
| 15085 | uint32_t new_length) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15086 | if (!array->map()->is_observed()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15087 | SetLength(array, new_length); |
| 15088 | return array; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15089 | } |
| 15090 | |
| 15091 | Isolate* isolate = array->GetIsolate(); |
| 15092 | List<uint32_t> indices; |
| 15093 | List<Handle<Object> > old_values; |
| 15094 | Handle<Object> old_length_handle(array->length(), isolate); |
| 15095 | uint32_t old_length = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15096 | CHECK(old_length_handle->ToArrayLength(&old_length)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15097 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15098 | int num_elements = array->NumberOfOwnElements(ALL_PROPERTIES); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15099 | if (num_elements > 0) { |
| 15100 | if (old_length == static_cast<uint32_t>(num_elements)) { |
| 15101 | // Simple case for arrays without holes. |
| 15102 | for (uint32_t i = old_length - 1; i + 1 > new_length; --i) { |
| 15103 | if (!GetOldValue(isolate, array, i, &old_values, &indices)) break; |
| 15104 | } |
| 15105 | } else { |
| 15106 | // For sparse arrays, only iterate over existing elements. |
| 15107 | // TODO(rafaelw): For fast, sparse arrays, we can avoid iterating over |
| 15108 | // the to-be-removed indices twice. |
| 15109 | Handle<FixedArray> keys = isolate->factory()->NewFixedArray(num_elements); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15110 | array->GetOwnElementKeys(*keys, ALL_PROPERTIES); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15111 | while (num_elements-- > 0) { |
| 15112 | uint32_t index = NumberToUint32(keys->get(num_elements)); |
| 15113 | if (index < new_length) break; |
| 15114 | if (!GetOldValue(isolate, array, index, &old_values, &indices)) break; |
| 15115 | } |
| 15116 | } |
| 15117 | } |
| 15118 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15119 | SetLength(array, new_length); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15120 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15121 | CHECK(array->length()->ToArrayLength(&new_length)); |
| 15122 | if (old_length == new_length) return array; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15123 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15124 | RETURN_ON_EXCEPTION(isolate, BeginPerformSplice(array), Object); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15125 | |
| 15126 | for (int i = 0; i < indices.length(); ++i) { |
| 15127 | // For deletions where the property was an accessor, old_values[i] |
| 15128 | // will be the hole, which instructs EnqueueChangeRecord to elide |
| 15129 | // the "oldValue" property. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15130 | RETURN_ON_EXCEPTION( |
| 15131 | isolate, |
| 15132 | JSObject::EnqueueChangeRecord( |
| 15133 | array, "delete", isolate->factory()->Uint32ToString(indices[i]), |
| 15134 | old_values[i]), |
| 15135 | Object); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15136 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15137 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15138 | RETURN_ON_EXCEPTION(isolate, |
| 15139 | JSObject::EnqueueChangeRecord( |
| 15140 | array, "update", isolate->factory()->length_string(), |
| 15141 | old_length_handle), |
| 15142 | Object); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15143 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15144 | RETURN_ON_EXCEPTION(isolate, EndPerformSplice(array), Object); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15145 | |
| 15146 | uint32_t index = Min(old_length, new_length); |
| 15147 | uint32_t add_count = new_length > old_length ? new_length - old_length : 0; |
| 15148 | uint32_t delete_count = new_length < old_length ? old_length - new_length : 0; |
| 15149 | Handle<JSArray> deleted = isolate->factory()->NewJSArray(0); |
| 15150 | if (delete_count > 0) { |
| 15151 | for (int i = indices.length() - 1; i >= 0; i--) { |
| 15152 | // Skip deletions where the property was an accessor, leaving holes |
| 15153 | // in the array of old values. |
| 15154 | if (old_values[i]->IsTheHole()) continue; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15155 | JSObject::AddDataElement(deleted, indices[i] - index, old_values[i], NONE) |
| 15156 | .Assert(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15157 | } |
| 15158 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15159 | JSArray::SetLength(deleted, delete_count); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15160 | } |
| 15161 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15162 | RETURN_ON_EXCEPTION( |
| 15163 | isolate, EnqueueSpliceRecord(array, index, deleted, add_count), Object); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15164 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15165 | return array; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15166 | } |
| 15167 | |
| 15168 | |
| 15169 | // static |
| 15170 | void Map::AddDependentCode(Handle<Map> map, |
| 15171 | DependentCode::DependencyGroup group, |
| 15172 | Handle<Code> code) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15173 | Handle<WeakCell> cell = Code::WeakCellFor(code); |
| 15174 | Handle<DependentCode> codes = DependentCode::InsertWeakCode( |
| 15175 | Handle<DependentCode>(map->dependent_code()), group, cell); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15176 | if (*codes != map->dependent_code()) map->set_dependent_code(*codes); |
| 15177 | } |
| 15178 | |
| 15179 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15180 | Handle<DependentCode> DependentCode::InsertCompilationDependencies( |
| 15181 | Handle<DependentCode> entries, DependencyGroup group, |
| 15182 | Handle<Foreign> info) { |
| 15183 | return Insert(entries, group, info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15184 | } |
| 15185 | |
| 15186 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15187 | Handle<DependentCode> DependentCode::InsertWeakCode( |
| 15188 | Handle<DependentCode> entries, DependencyGroup group, |
| 15189 | Handle<WeakCell> code_cell) { |
| 15190 | return Insert(entries, group, code_cell); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15191 | } |
| 15192 | |
| 15193 | |
| 15194 | Handle<DependentCode> DependentCode::Insert(Handle<DependentCode> entries, |
| 15195 | DependencyGroup group, |
| 15196 | Handle<Object> object) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15197 | if (entries->length() == 0 || entries->group() > group) { |
| 15198 | // There is no such group. |
| 15199 | return DependentCode::New(group, object, entries); |
| 15200 | } |
| 15201 | if (entries->group() < group) { |
| 15202 | // The group comes later in the list. |
| 15203 | Handle<DependentCode> old_next(entries->next_link()); |
| 15204 | Handle<DependentCode> new_next = Insert(old_next, group, object); |
| 15205 | if (!old_next.is_identical_to(new_next)) { |
| 15206 | entries->set_next_link(*new_next); |
| 15207 | } |
| 15208 | return entries; |
| 15209 | } |
| 15210 | DCHECK_EQ(group, entries->group()); |
| 15211 | int count = entries->count(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15212 | // Check for existing entry to avoid duplicates. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15213 | for (int i = 0; i < count; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15214 | if (entries->object_at(i) == *object) return entries; |
| 15215 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15216 | if (entries->length() < kCodesStartIndex + count + 1) { |
| 15217 | entries = EnsureSpace(entries); |
| 15218 | // Count could have changed, reload it. |
| 15219 | count = entries->count(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15220 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15221 | entries->set_object_at(count, *object); |
| 15222 | entries->set_count(count + 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15223 | return entries; |
| 15224 | } |
| 15225 | |
| 15226 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15227 | Handle<DependentCode> DependentCode::New(DependencyGroup group, |
| 15228 | Handle<Object> object, |
| 15229 | Handle<DependentCode> next) { |
| 15230 | Isolate* isolate = next->GetIsolate(); |
| 15231 | Handle<DependentCode> result = Handle<DependentCode>::cast( |
| 15232 | isolate->factory()->NewFixedArray(kCodesStartIndex + 1, TENURED)); |
| 15233 | result->set_next_link(*next); |
| 15234 | result->set_flags(GroupField::encode(group) | CountField::encode(1)); |
| 15235 | result->set_object_at(0, *object); |
| 15236 | return result; |
| 15237 | } |
| 15238 | |
| 15239 | |
| 15240 | Handle<DependentCode> DependentCode::EnsureSpace( |
| 15241 | Handle<DependentCode> entries) { |
| 15242 | if (entries->Compact()) return entries; |
| 15243 | Isolate* isolate = entries->GetIsolate(); |
| 15244 | int capacity = kCodesStartIndex + DependentCode::Grow(entries->count()); |
| 15245 | int grow_by = capacity - entries->length(); |
| 15246 | return Handle<DependentCode>::cast( |
| 15247 | isolate->factory()->CopyFixedArrayAndGrow(entries, grow_by, TENURED)); |
| 15248 | } |
| 15249 | |
| 15250 | |
| 15251 | bool DependentCode::Compact() { |
| 15252 | int old_count = count(); |
| 15253 | int new_count = 0; |
| 15254 | for (int i = 0; i < old_count; i++) { |
| 15255 | Object* obj = object_at(i); |
| 15256 | if (!obj->IsWeakCell() || !WeakCell::cast(obj)->cleared()) { |
| 15257 | if (i != new_count) { |
| 15258 | copy(i, new_count); |
| 15259 | } |
| 15260 | new_count++; |
| 15261 | } |
| 15262 | } |
| 15263 | set_count(new_count); |
| 15264 | for (int i = new_count; i < old_count; i++) { |
| 15265 | clear_at(i); |
| 15266 | } |
| 15267 | return new_count < old_count; |
| 15268 | } |
| 15269 | |
| 15270 | |
| 15271 | void DependentCode::UpdateToFinishedCode(DependencyGroup group, Foreign* info, |
| 15272 | WeakCell* code_cell) { |
| 15273 | if (this->length() == 0 || this->group() > group) { |
| 15274 | // There is no such group. |
| 15275 | return; |
| 15276 | } |
| 15277 | if (this->group() < group) { |
| 15278 | // The group comes later in the list. |
| 15279 | next_link()->UpdateToFinishedCode(group, info, code_cell); |
| 15280 | return; |
| 15281 | } |
| 15282 | DCHECK_EQ(group, this->group()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15283 | DisallowHeapAllocation no_gc; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15284 | int count = this->count(); |
| 15285 | for (int i = 0; i < count; i++) { |
| 15286 | if (object_at(i) == info) { |
| 15287 | set_object_at(i, code_cell); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15288 | break; |
| 15289 | } |
| 15290 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 15291 | #ifdef DEBUG |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15292 | for (int i = 0; i < count; i++) { |
| 15293 | DCHECK(object_at(i) != info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15294 | } |
| 15295 | #endif |
| 15296 | } |
| 15297 | |
| 15298 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15299 | void DependentCode::RemoveCompilationDependencies( |
| 15300 | DependentCode::DependencyGroup group, Foreign* info) { |
| 15301 | if (this->length() == 0 || this->group() > group) { |
| 15302 | // There is no such group. |
| 15303 | return; |
| 15304 | } |
| 15305 | if (this->group() < group) { |
| 15306 | // The group comes later in the list. |
| 15307 | next_link()->RemoveCompilationDependencies(group, info); |
| 15308 | return; |
| 15309 | } |
| 15310 | DCHECK_EQ(group, this->group()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15311 | DisallowHeapAllocation no_allocation; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15312 | int old_count = count(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15313 | // Find compilation info wrapper. |
| 15314 | int info_pos = -1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15315 | for (int i = 0; i < old_count; i++) { |
| 15316 | if (object_at(i) == info) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15317 | info_pos = i; |
| 15318 | break; |
| 15319 | } |
| 15320 | } |
| 15321 | if (info_pos == -1) return; // Not found. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15322 | // Use the last code to fill the gap. |
| 15323 | if (info_pos < old_count - 1) { |
| 15324 | copy(old_count - 1, info_pos); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15325 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15326 | clear_at(old_count - 1); |
| 15327 | set_count(old_count - 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15328 | |
| 15329 | #ifdef DEBUG |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15330 | for (int i = 0; i < old_count - 1; i++) { |
| 15331 | DCHECK(object_at(i) != info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15332 | } |
| 15333 | #endif |
| 15334 | } |
| 15335 | |
| 15336 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15337 | bool DependentCode::Contains(DependencyGroup group, WeakCell* code_cell) { |
| 15338 | if (this->length() == 0 || this->group() > group) { |
| 15339 | // There is no such group. |
| 15340 | return false; |
| 15341 | } |
| 15342 | if (this->group() < group) { |
| 15343 | // The group comes later in the list. |
| 15344 | return next_link()->Contains(group, code_cell); |
| 15345 | } |
| 15346 | DCHECK_EQ(group, this->group()); |
| 15347 | int count = this->count(); |
| 15348 | for (int i = 0; i < count; i++) { |
| 15349 | if (object_at(i) == code_cell) return true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15350 | } |
| 15351 | return false; |
| 15352 | } |
| 15353 | |
| 15354 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15355 | bool DependentCode::IsEmpty(DependencyGroup group) { |
| 15356 | if (this->length() == 0 || this->group() > group) { |
| 15357 | // There is no such group. |
| 15358 | return true; |
| 15359 | } |
| 15360 | if (this->group() < group) { |
| 15361 | // The group comes later in the list. |
| 15362 | return next_link()->IsEmpty(group); |
| 15363 | } |
| 15364 | DCHECK_EQ(group, this->group()); |
| 15365 | return count() == 0; |
| 15366 | } |
| 15367 | |
| 15368 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15369 | bool DependentCode::MarkCodeForDeoptimization( |
| 15370 | Isolate* isolate, |
| 15371 | DependentCode::DependencyGroup group) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15372 | if (this->length() == 0 || this->group() > group) { |
| 15373 | // There is no such group. |
| 15374 | return false; |
| 15375 | } |
| 15376 | if (this->group() < group) { |
| 15377 | // The group comes later in the list. |
| 15378 | return next_link()->MarkCodeForDeoptimization(isolate, group); |
| 15379 | } |
| 15380 | DCHECK_EQ(group, this->group()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15381 | DisallowHeapAllocation no_allocation_scope; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15382 | // Mark all the code that needs to be deoptimized. |
| 15383 | bool marked = false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15384 | bool invalidate_embedded_objects = group == kWeakCodeGroup; |
| 15385 | int count = this->count(); |
| 15386 | for (int i = 0; i < count; i++) { |
| 15387 | Object* obj = object_at(i); |
| 15388 | if (obj->IsWeakCell()) { |
| 15389 | WeakCell* cell = WeakCell::cast(obj); |
| 15390 | if (cell->cleared()) continue; |
| 15391 | Code* code = Code::cast(cell->value()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15392 | if (!code->marked_for_deoptimization()) { |
| 15393 | SetMarkedForDeoptimization(code, group); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15394 | if (invalidate_embedded_objects) { |
| 15395 | code->InvalidateEmbeddedObjects(); |
| 15396 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15397 | marked = true; |
| 15398 | } |
| 15399 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15400 | DCHECK(obj->IsForeign()); |
| 15401 | CompilationDependencies* info = |
| 15402 | reinterpret_cast<CompilationDependencies*>( |
| 15403 | Foreign::cast(obj)->foreign_address()); |
| 15404 | info->Abort(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15405 | } |
| 15406 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15407 | for (int i = 0; i < count; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15408 | clear_at(i); |
| 15409 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15410 | set_count(0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15411 | return marked; |
| 15412 | } |
| 15413 | |
| 15414 | |
| 15415 | void DependentCode::DeoptimizeDependentCodeGroup( |
| 15416 | Isolate* isolate, |
| 15417 | DependentCode::DependencyGroup group) { |
| 15418 | DCHECK(AllowCodeDependencyChange::IsAllowed()); |
| 15419 | DisallowHeapAllocation no_allocation_scope; |
| 15420 | bool marked = MarkCodeForDeoptimization(isolate, group); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15421 | if (marked) Deoptimizer::DeoptimizeMarkedCode(isolate); |
| 15422 | } |
| 15423 | |
| 15424 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15425 | void DependentCode::SetMarkedForDeoptimization(Code* code, |
| 15426 | DependencyGroup group) { |
| 15427 | code->set_marked_for_deoptimization(true); |
| 15428 | if (FLAG_trace_deopt && |
| 15429 | (code->deoptimization_data() != code->GetHeap()->empty_fixed_array())) { |
| 15430 | DeoptimizationInputData* deopt_data = |
| 15431 | DeoptimizationInputData::cast(code->deoptimization_data()); |
| 15432 | CodeTracer::Scope scope(code->GetHeap()->isolate()->GetCodeTracer()); |
| 15433 | PrintF(scope.file(), "[marking dependent code 0x%08" V8PRIxPTR |
| 15434 | " (opt #%d) for deoptimization, reason: %s]\n", |
| 15435 | reinterpret_cast<intptr_t>(code), |
| 15436 | deopt_data->OptimizationId()->value(), DependencyGroupName(group)); |
| 15437 | } |
| 15438 | } |
| 15439 | |
| 15440 | |
| 15441 | const char* DependentCode::DependencyGroupName(DependencyGroup group) { |
| 15442 | switch (group) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15443 | case kWeakCodeGroup: |
| 15444 | return "weak-code"; |
| 15445 | case kTransitionGroup: |
| 15446 | return "transition"; |
| 15447 | case kPrototypeCheckGroup: |
| 15448 | return "prototype-check"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15449 | case kPropertyCellChangedGroup: |
| 15450 | return "property-cell-changed"; |
| 15451 | case kFieldTypeGroup: |
| 15452 | return "field-type"; |
| 15453 | case kInitialMapChangedGroup: |
| 15454 | return "initial-map-changed"; |
| 15455 | case kAllocationSiteTenuringChangedGroup: |
| 15456 | return "allocation-site-tenuring-changed"; |
| 15457 | case kAllocationSiteTransitionChangedGroup: |
| 15458 | return "allocation-site-transition-changed"; |
| 15459 | } |
| 15460 | UNREACHABLE(); |
| 15461 | return "?"; |
| 15462 | } |
| 15463 | |
| 15464 | |
| 15465 | Handle<Map> Map::TransitionToPrototype(Handle<Map> map, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15466 | Handle<Object> prototype, |
| 15467 | PrototypeOptimizationMode mode) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15468 | Handle<Map> new_map = TransitionArray::GetPrototypeTransition(map, prototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15469 | if (new_map.is_null()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15470 | new_map = Copy(map, "TransitionToPrototype"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15471 | TransitionArray::PutPrototypeTransition(map, prototype, new_map); |
| 15472 | Map::SetPrototype(new_map, prototype, mode); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15473 | } |
| 15474 | return new_map; |
| 15475 | } |
| 15476 | |
| 15477 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15478 | Maybe<bool> JSReceiver::SetPrototype(Handle<JSReceiver> object, |
| 15479 | Handle<Object> value, bool from_javascript, |
| 15480 | ShouldThrow should_throw) { |
| 15481 | if (object->IsJSProxy()) { |
| 15482 | return JSProxy::SetPrototype(Handle<JSProxy>::cast(object), value, |
| 15483 | from_javascript, should_throw); |
| 15484 | } |
| 15485 | return JSObject::SetPrototype(Handle<JSObject>::cast(object), value, |
| 15486 | from_javascript, should_throw); |
| 15487 | } |
| 15488 | |
| 15489 | |
| 15490 | // ES6: 9.5.2 [[SetPrototypeOf]] (V) |
| 15491 | // static |
| 15492 | Maybe<bool> JSProxy::SetPrototype(Handle<JSProxy> proxy, Handle<Object> value, |
| 15493 | bool from_javascript, |
| 15494 | ShouldThrow should_throw) { |
| 15495 | Isolate* isolate = proxy->GetIsolate(); |
| 15496 | STACK_CHECK(Nothing<bool>()); |
| 15497 | Handle<Name> trap_name = isolate->factory()->setPrototypeOf_string(); |
| 15498 | // 1. Assert: Either Type(V) is Object or Type(V) is Null. |
| 15499 | DCHECK(value->IsJSReceiver() || value->IsNull()); |
| 15500 | // 2. Let handler be the value of the [[ProxyHandler]] internal slot of O. |
| 15501 | Handle<Object> handler(proxy->handler(), isolate); |
| 15502 | // 3. If handler is null, throw a TypeError exception. |
| 15503 | // 4. Assert: Type(handler) is Object. |
| 15504 | if (proxy->IsRevoked()) { |
| 15505 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 15506 | MessageTemplate::kProxyRevoked, trap_name)); |
| 15507 | return Nothing<bool>(); |
| 15508 | } |
| 15509 | // 5. Let target be the value of the [[ProxyTarget]] internal slot. |
| 15510 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 15511 | // 6. Let trap be ? GetMethod(handler, "getPrototypeOf"). |
| 15512 | Handle<Object> trap; |
| 15513 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 15514 | isolate, trap, |
| 15515 | Object::GetMethod(Handle<JSReceiver>::cast(handler), trap_name), |
| 15516 | Nothing<bool>()); |
| 15517 | // 7. If trap is undefined, then return target.[[SetPrototypeOf]](). |
| 15518 | if (trap->IsUndefined()) { |
| 15519 | return JSReceiver::SetPrototype(target, value, from_javascript, |
| 15520 | should_throw); |
| 15521 | } |
| 15522 | // 8. Let booleanTrapResult be ToBoolean(? Call(trap, handler, «target, V»)). |
| 15523 | Handle<Object> argv[] = {target, value}; |
| 15524 | Handle<Object> trap_result; |
| 15525 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 15526 | isolate, trap_result, |
| 15527 | Execution::Call(isolate, trap, handler, arraysize(argv), argv), |
| 15528 | Nothing<bool>()); |
| 15529 | bool bool_trap_result = trap_result->BooleanValue(); |
| 15530 | // 9. Let extensibleTarget be ? IsExtensible(target). |
| 15531 | Maybe<bool> is_extensible = JSReceiver::IsExtensible(target); |
| 15532 | if (is_extensible.IsNothing()) return Nothing<bool>(); |
| 15533 | // 10. If extensibleTarget is true, return booleanTrapResult. |
| 15534 | if (is_extensible.FromJust()) { |
| 15535 | if (bool_trap_result) return Just(true); |
| 15536 | RETURN_FAILURE( |
| 15537 | isolate, should_throw, |
| 15538 | NewTypeError(MessageTemplate::kProxyTrapReturnedFalsish, trap_name)); |
| 15539 | } |
| 15540 | // 11. Let targetProto be ? target.[[GetPrototypeOf]](). |
| 15541 | Handle<Object> target_proto; |
| 15542 | ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, target_proto, |
| 15543 | Object::GetPrototype(isolate, target), |
| 15544 | Nothing<bool>()); |
| 15545 | // 12. If booleanTrapResult is true and SameValue(V, targetProto) is false, |
| 15546 | // throw a TypeError exception. |
| 15547 | if (bool_trap_result && !value->SameValue(*target_proto)) { |
| 15548 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 15549 | MessageTemplate::kProxySetPrototypeOfNonExtensible)); |
| 15550 | return Nothing<bool>(); |
| 15551 | } |
| 15552 | // 13. Return booleanTrapResult. |
| 15553 | if (bool_trap_result) return Just(true); |
| 15554 | RETURN_FAILURE( |
| 15555 | isolate, should_throw, |
| 15556 | NewTypeError(MessageTemplate::kProxyTrapReturnedFalsish, trap_name)); |
| 15557 | } |
| 15558 | |
| 15559 | |
| 15560 | Maybe<bool> JSObject::SetPrototype(Handle<JSObject> object, |
| 15561 | Handle<Object> value, bool from_javascript, |
| 15562 | ShouldThrow should_throw) { |
| 15563 | Isolate* isolate = object->GetIsolate(); |
| 15564 | |
| 15565 | const bool observed = from_javascript && object->map()->is_observed(); |
| 15566 | Handle<Object> old_value; |
| 15567 | if (observed) { |
| 15568 | ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, old_value, |
| 15569 | Object::GetPrototype(isolate, object), |
| 15570 | Nothing<bool>()); |
| 15571 | } |
| 15572 | |
| 15573 | Maybe<bool> result = |
| 15574 | SetPrototypeUnobserved(object, value, from_javascript, should_throw); |
| 15575 | MAYBE_RETURN(result, Nothing<bool>()); |
| 15576 | |
| 15577 | if (result.FromJust() && observed) { |
| 15578 | Handle<Object> new_value; |
| 15579 | ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, new_value, |
| 15580 | Object::GetPrototype(isolate, object), |
| 15581 | Nothing<bool>()); |
| 15582 | if (!new_value->SameValue(*old_value)) { |
| 15583 | RETURN_ON_EXCEPTION_VALUE( |
| 15584 | isolate, JSObject::EnqueueChangeRecord( |
| 15585 | object, "setPrototype", |
| 15586 | isolate->factory()->proto_string(), old_value), |
| 15587 | Nothing<bool>()); |
| 15588 | } |
| 15589 | } |
| 15590 | |
| 15591 | return result; |
| 15592 | } |
| 15593 | |
| 15594 | |
| 15595 | Maybe<bool> JSObject::SetPrototypeUnobserved(Handle<JSObject> object, |
| 15596 | Handle<Object> value, |
| 15597 | bool from_javascript, |
| 15598 | ShouldThrow should_throw) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15599 | #ifdef DEBUG |
| 15600 | int size = object->Size(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 15601 | #endif |
| 15602 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15603 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15604 | |
| 15605 | if (from_javascript) { |
| 15606 | if (object->IsAccessCheckNeeded() && |
| 15607 | !isolate->MayAccess(handle(isolate->context()), object)) { |
| 15608 | isolate->ReportFailedAccessCheck(object); |
| 15609 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); |
| 15610 | RETURN_FAILURE(isolate, should_throw, |
| 15611 | NewTypeError(MessageTemplate::kNoAccess)); |
| 15612 | } |
| 15613 | } else { |
| 15614 | DCHECK(!object->IsAccessCheckNeeded()); |
| 15615 | } |
| 15616 | |
| 15617 | // Strong objects may not have their prototype set via __proto__ or |
| 15618 | // setPrototypeOf. |
| 15619 | if (from_javascript && object->map()->is_strong()) { |
| 15620 | RETURN_FAILURE(isolate, should_throw, |
| 15621 | NewTypeError(MessageTemplate::kStrongSetProto, object)); |
| 15622 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15623 | Heap* heap = isolate->heap(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 15624 | // Silently ignore the change if value is not a JSObject or null. |
| 15625 | // SpiderMonkey behaves this way. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15626 | if (!value->IsJSReceiver() && !value->IsNull()) return Just(true); |
| 15627 | |
| 15628 | bool dictionary_elements_in_chain = |
| 15629 | object->map()->DictionaryElementsInPrototypeChainOnly(); |
| 15630 | |
| 15631 | bool all_extensible = object->map()->is_extensible(); |
| 15632 | Handle<JSObject> real_receiver = object; |
| 15633 | if (from_javascript) { |
| 15634 | // Find the first object in the chain whose prototype object is not |
| 15635 | // hidden. |
| 15636 | PrototypeIterator iter(isolate, real_receiver); |
| 15637 | while (!iter.IsAtEnd(PrototypeIterator::END_AT_NON_HIDDEN)) { |
| 15638 | // Casting to JSObject is fine because hidden prototypes are never |
| 15639 | // JSProxies. |
| 15640 | real_receiver = PrototypeIterator::GetCurrent<JSObject>(iter); |
| 15641 | iter.Advance(); |
| 15642 | all_extensible = all_extensible && real_receiver->map()->is_extensible(); |
| 15643 | } |
| 15644 | } |
| 15645 | Handle<Map> map(real_receiver->map()); |
| 15646 | |
| 15647 | // Nothing to do if prototype is already set. |
| 15648 | if (map->prototype() == *value) return Just(true); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 15649 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 15650 | // From 8.6.2 Object Internal Methods |
| 15651 | // ... |
| 15652 | // In addition, if [[Extensible]] is false the value of the [[Class]] and |
| 15653 | // [[Prototype]] internal properties of the object may not be modified. |
| 15654 | // ... |
| 15655 | // Implementation specific extensions that modify [[Class]], [[Prototype]] |
| 15656 | // or [[Extensible]] must not violate the invariants defined in the preceding |
| 15657 | // paragraph. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15658 | if (!all_extensible) { |
| 15659 | RETURN_FAILURE(isolate, should_throw, |
| 15660 | NewTypeError(MessageTemplate::kNonExtensibleProto, object)); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 15661 | } |
| 15662 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15663 | // Before we can set the prototype we need to be sure prototype cycles are |
| 15664 | // prevented. It is sufficient to validate that the receiver is not in the |
| 15665 | // new prototype chain. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15666 | for (PrototypeIterator iter(isolate, *value, |
| 15667 | PrototypeIterator::START_AT_RECEIVER); |
| 15668 | !iter.IsAtEnd(); iter.Advance()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15669 | if (iter.GetCurrent<JSReceiver>() == *object) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 15670 | // Cycle detected. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15671 | RETURN_FAILURE(isolate, should_throw, |
| 15672 | NewTypeError(MessageTemplate::kCyclicProto)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 15673 | } |
| 15674 | } |
| 15675 | |
| 15676 | // Set the new prototype of the object. |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 15677 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15678 | isolate->UpdateArrayProtectorOnSetPrototype(real_receiver); |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 15679 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15680 | PrototypeOptimizationMode mode = |
| 15681 | from_javascript ? REGULAR_PROTOTYPE : FAST_PROTOTYPE; |
| 15682 | Handle<Map> new_map = Map::TransitionToPrototype(map, value, mode); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15683 | DCHECK(new_map->prototype() == *value); |
| 15684 | JSObject::MigrateToMap(real_receiver, new_map); |
| 15685 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15686 | if (from_javascript && !dictionary_elements_in_chain && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15687 | new_map->DictionaryElementsInPrototypeChainOnly()) { |
| 15688 | // If the prototype chain didn't previously have element callbacks, then |
| 15689 | // KeyedStoreICs need to be cleared to ensure any that involve this |
| 15690 | // map go generic. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15691 | TypeFeedbackVector::ClearAllKeyedStoreICs(isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15692 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 15693 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 15694 | heap->ClearInstanceofCache(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15695 | DCHECK(size == object->Size()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15696 | return Just(true); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 15697 | } |
| 15698 | |
| 15699 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15700 | void JSObject::EnsureCanContainElements(Handle<JSObject> object, |
| 15701 | Arguments* args, |
| 15702 | uint32_t first_arg, |
| 15703 | uint32_t arg_count, |
| 15704 | EnsureElementsMode mode) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 15705 | // Elements in |Arguments| are ordered backwards (because they're on the |
| 15706 | // stack), but the method that's called here iterates over them in forward |
| 15707 | // direction. |
| 15708 | return EnsureCanContainElements( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15709 | object, args->arguments() - first_arg - (arg_count - 1), arg_count, mode); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 15710 | } |
| 15711 | |
| 15712 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15713 | ElementsAccessor* JSObject::GetElementsAccessor() { |
| 15714 | return ElementsAccessor::ForKind(GetElementsKind()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15715 | } |
| 15716 | |
| 15717 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15718 | void JSObject::ValidateElements(Handle<JSObject> object) { |
| 15719 | #ifdef ENABLE_SLOW_DCHECKS |
| 15720 | if (FLAG_enable_slow_asserts) { |
| 15721 | ElementsAccessor* accessor = object->GetElementsAccessor(); |
| 15722 | accessor->Validate(object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15723 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15724 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15725 | } |
| 15726 | |
| 15727 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15728 | static bool ShouldConvertToSlowElements(JSObject* object, uint32_t capacity, |
| 15729 | uint32_t index, |
| 15730 | uint32_t* new_capacity) { |
| 15731 | STATIC_ASSERT(JSObject::kMaxUncheckedOldFastElementsLength <= |
| 15732 | JSObject::kMaxUncheckedFastElementsLength); |
| 15733 | if (index < capacity) { |
| 15734 | *new_capacity = capacity; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 15735 | return false; |
| 15736 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15737 | if (index - capacity >= JSObject::kMaxGap) return true; |
| 15738 | *new_capacity = JSObject::NewElementsCapacity(index + 1); |
| 15739 | DCHECK_LT(index, *new_capacity); |
| 15740 | if (*new_capacity <= JSObject::kMaxUncheckedOldFastElementsLength || |
| 15741 | (*new_capacity <= JSObject::kMaxUncheckedFastElementsLength && |
| 15742 | object->GetHeap()->InNewSpace(object))) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 15743 | return false; |
| 15744 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15745 | // If the fast-case backing storage takes up roughly three times as |
| 15746 | // much space (in machine words) as a dictionary backing storage |
| 15747 | // would, the object should have slow elements. |
| 15748 | int used_elements = object->GetFastElementsUsage(); |
| 15749 | int dictionary_size = SeededNumberDictionary::ComputeCapacity(used_elements) * |
| 15750 | SeededNumberDictionary::kEntrySize; |
| 15751 | return 3 * static_cast<uint32_t>(dictionary_size) <= *new_capacity; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 15752 | } |
| 15753 | |
| 15754 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15755 | bool JSObject::WouldConvertToSlowElements(uint32_t index) { |
| 15756 | if (HasFastElements()) { |
| 15757 | Handle<FixedArrayBase> backing_store(FixedArrayBase::cast(elements())); |
| 15758 | uint32_t capacity = static_cast<uint32_t>(backing_store->length()); |
| 15759 | uint32_t new_capacity; |
| 15760 | return ShouldConvertToSlowElements(this, capacity, index, &new_capacity); |
| 15761 | } |
| 15762 | return false; |
| 15763 | } |
| 15764 | |
| 15765 | |
| 15766 | static ElementsKind BestFittingFastElementsKind(JSObject* object) { |
| 15767 | if (object->HasSloppyArgumentsElements()) { |
| 15768 | return FAST_SLOPPY_ARGUMENTS_ELEMENTS; |
| 15769 | } |
| 15770 | DCHECK(object->HasDictionaryElements()); |
| 15771 | SeededNumberDictionary* dictionary = object->element_dictionary(); |
| 15772 | ElementsKind kind = FAST_HOLEY_SMI_ELEMENTS; |
| 15773 | for (int i = 0; i < dictionary->Capacity(); i++) { |
| 15774 | Object* key = dictionary->KeyAt(i); |
| 15775 | if (key->IsNumber()) { |
| 15776 | Object* value = dictionary->ValueAt(i); |
| 15777 | if (!value->IsNumber()) return FAST_HOLEY_ELEMENTS; |
| 15778 | if (!value->IsSmi()) { |
| 15779 | if (!FLAG_unbox_double_arrays) return FAST_HOLEY_ELEMENTS; |
| 15780 | kind = FAST_HOLEY_DOUBLE_ELEMENTS; |
| 15781 | } |
| 15782 | } |
| 15783 | } |
| 15784 | return kind; |
| 15785 | } |
| 15786 | |
| 15787 | |
| 15788 | static bool ShouldConvertToFastElements(JSObject* object, |
| 15789 | SeededNumberDictionary* dictionary, |
| 15790 | uint32_t index, |
| 15791 | uint32_t* new_capacity) { |
| 15792 | // If properties with non-standard attributes or accessors were added, we |
| 15793 | // cannot go back to fast elements. |
| 15794 | if (dictionary->requires_slow_elements()) return false; |
| 15795 | |
| 15796 | // Adding a property with this index will require slow elements. |
| 15797 | if (index >= static_cast<uint32_t>(Smi::kMaxValue)) return false; |
| 15798 | |
| 15799 | if (object->IsJSArray()) { |
| 15800 | Object* length = JSArray::cast(object)->length(); |
| 15801 | if (!length->IsSmi()) return false; |
| 15802 | *new_capacity = static_cast<uint32_t>(Smi::cast(length)->value()); |
| 15803 | } else { |
| 15804 | *new_capacity = dictionary->max_number_key() + 1; |
| 15805 | } |
| 15806 | *new_capacity = Max(index + 1, *new_capacity); |
| 15807 | |
| 15808 | uint32_t dictionary_size = static_cast<uint32_t>(dictionary->Capacity()) * |
| 15809 | SeededNumberDictionary::kEntrySize; |
| 15810 | |
| 15811 | // Turn fast if the dictionary only saves 50% space. |
| 15812 | return 2 * dictionary_size >= *new_capacity; |
| 15813 | } |
| 15814 | |
| 15815 | |
| 15816 | // static |
| 15817 | MaybeHandle<Object> JSObject::AddDataElement(Handle<JSObject> object, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15818 | uint32_t index, |
| 15819 | Handle<Object> value, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15820 | PropertyAttributes attributes) { |
| 15821 | MAYBE_RETURN_NULL( |
| 15822 | AddDataElement(object, index, value, attributes, THROW_ON_ERROR)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 15823 | return value; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 15824 | } |
| 15825 | |
| 15826 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15827 | // static |
| 15828 | Maybe<bool> JSObject::AddDataElement(Handle<JSObject> object, uint32_t index, |
| 15829 | Handle<Object> value, |
| 15830 | PropertyAttributes attributes, |
| 15831 | ShouldThrow should_throw) { |
| 15832 | DCHECK(object->map()->is_extensible()); |
| 15833 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15834 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 15835 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15836 | uint32_t old_length = 0; |
| 15837 | uint32_t new_capacity = 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 15838 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15839 | Handle<Object> old_length_handle; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15840 | if (object->IsJSArray()) { |
| 15841 | CHECK(JSArray::cast(*object)->length()->ToArrayLength(&old_length)); |
| 15842 | if (object->map()->is_observed()) { |
| 15843 | old_length_handle = handle(JSArray::cast(*object)->length(), isolate); |
| 15844 | } |
| 15845 | } |
| 15846 | |
| 15847 | ElementsKind kind = object->GetElementsKind(); |
| 15848 | FixedArrayBase* elements = object->elements(); |
| 15849 | ElementsKind dictionary_kind = DICTIONARY_ELEMENTS; |
| 15850 | if (IsSloppyArgumentsElements(kind)) { |
| 15851 | elements = FixedArrayBase::cast(FixedArray::cast(elements)->get(1)); |
| 15852 | dictionary_kind = SLOW_SLOPPY_ARGUMENTS_ELEMENTS; |
| 15853 | } |
| 15854 | |
| 15855 | if (attributes != NONE) { |
| 15856 | kind = dictionary_kind; |
| 15857 | } else if (elements->IsSeededNumberDictionary()) { |
| 15858 | kind = ShouldConvertToFastElements(*object, |
| 15859 | SeededNumberDictionary::cast(elements), |
| 15860 | index, &new_capacity) |
| 15861 | ? BestFittingFastElementsKind(*object) |
| 15862 | : dictionary_kind; // Overwrite in case of arguments. |
| 15863 | } else if (ShouldConvertToSlowElements( |
| 15864 | *object, static_cast<uint32_t>(elements->length()), index, |
| 15865 | &new_capacity)) { |
| 15866 | kind = dictionary_kind; |
| 15867 | } |
| 15868 | |
| 15869 | ElementsKind to = value->OptimalElementsKind(); |
| 15870 | if (IsHoleyElementsKind(kind) || !object->IsJSArray() || index > old_length) { |
| 15871 | to = GetHoleyElementsKind(to); |
| 15872 | kind = GetHoleyElementsKind(kind); |
| 15873 | } |
| 15874 | to = GetMoreGeneralElementsKind(kind, to); |
| 15875 | ElementsAccessor* accessor = ElementsAccessor::ForKind(to); |
| 15876 | accessor->Add(object, index, value, attributes, new_capacity); |
| 15877 | |
| 15878 | uint32_t new_length = old_length; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15879 | Handle<Object> new_length_handle; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15880 | if (object->IsJSArray() && index >= old_length) { |
| 15881 | new_length = index + 1; |
| 15882 | new_length_handle = isolate->factory()->NewNumberFromUint(new_length); |
| 15883 | JSArray::cast(*object)->set_length(*new_length_handle); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15884 | } |
| 15885 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15886 | if (!old_length_handle.is_null() && new_length != old_length) { |
| 15887 | // |old_length_handle| is kept null above unless the object is observed. |
| 15888 | DCHECK(object->map()->is_observed()); |
| 15889 | Handle<JSArray> array = Handle<JSArray>::cast(object); |
| 15890 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15891 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15892 | RETURN_ON_EXCEPTION_VALUE(isolate, BeginPerformSplice(array), |
| 15893 | Nothing<bool>()); |
| 15894 | RETURN_ON_EXCEPTION_VALUE( |
| 15895 | isolate, EnqueueChangeRecord(array, "add", name, |
| 15896 | isolate->factory()->the_hole_value()), |
| 15897 | Nothing<bool>()); |
| 15898 | RETURN_ON_EXCEPTION_VALUE( |
| 15899 | isolate, EnqueueChangeRecord(array, "update", |
| 15900 | isolate->factory()->length_string(), |
| 15901 | old_length_handle), |
| 15902 | Nothing<bool>()); |
| 15903 | RETURN_ON_EXCEPTION_VALUE(isolate, EndPerformSplice(array), |
| 15904 | Nothing<bool>()); |
| 15905 | Handle<JSArray> deleted = isolate->factory()->NewJSArray(0); |
| 15906 | RETURN_ON_EXCEPTION_VALUE(isolate, |
| 15907 | EnqueueSpliceRecord(array, old_length, deleted, |
| 15908 | new_length - old_length), |
| 15909 | Nothing<bool>()); |
| 15910 | } else if (object->map()->is_observed()) { |
| 15911 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
| 15912 | RETURN_ON_EXCEPTION_VALUE( |
| 15913 | isolate, EnqueueChangeRecord(object, "add", name, |
| 15914 | isolate->factory()->the_hole_value()), |
| 15915 | Nothing<bool>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15916 | } |
| 15917 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15918 | return Just(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15919 | } |
| 15920 | |
| 15921 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15922 | bool JSArray::SetLengthWouldNormalize(uint32_t new_length) { |
| 15923 | if (!HasFastElements()) return false; |
| 15924 | uint32_t capacity = static_cast<uint32_t>(elements()->length()); |
| 15925 | uint32_t new_capacity; |
| 15926 | return JSArray::SetLengthWouldNormalize(GetHeap(), new_length) && |
| 15927 | ShouldConvertToSlowElements(this, capacity, new_length - 1, |
| 15928 | &new_capacity); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15929 | } |
| 15930 | |
| 15931 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15932 | const double AllocationSite::kPretenureRatio = 0.85; |
| 15933 | |
| 15934 | |
| 15935 | void AllocationSite::ResetPretenureDecision() { |
| 15936 | set_pretenure_decision(kUndecided); |
| 15937 | set_memento_found_count(0); |
| 15938 | set_memento_create_count(0); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 15939 | } |
| 15940 | |
| 15941 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15942 | PretenureFlag AllocationSite::GetPretenureMode() { |
| 15943 | PretenureDecision mode = pretenure_decision(); |
| 15944 | // Zombie objects "decide" to be untenured. |
| 15945 | return mode == kTenure ? TENURED : NOT_TENURED; |
| 15946 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 15947 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15948 | |
| 15949 | bool AllocationSite::IsNestedSite() { |
| 15950 | DCHECK(FLAG_trace_track_allocation_sites); |
| 15951 | Object* current = GetHeap()->allocation_sites_list(); |
| 15952 | while (current->IsAllocationSite()) { |
| 15953 | AllocationSite* current_site = AllocationSite::cast(current); |
| 15954 | if (current_site->nested_site() == this) { |
| 15955 | return true; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 15956 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15957 | current = current_site->weak_next(); |
| 15958 | } |
| 15959 | return false; |
| 15960 | } |
| 15961 | |
| 15962 | |
| 15963 | void AllocationSite::DigestTransitionFeedback(Handle<AllocationSite> site, |
| 15964 | ElementsKind to_kind) { |
| 15965 | Isolate* isolate = site->GetIsolate(); |
| 15966 | |
| 15967 | if (site->SitePointsToLiteral() && site->transition_info()->IsJSArray()) { |
| 15968 | Handle<JSArray> transition_info = |
| 15969 | handle(JSArray::cast(site->transition_info())); |
| 15970 | ElementsKind kind = transition_info->GetElementsKind(); |
| 15971 | // if kind is holey ensure that to_kind is as well. |
| 15972 | if (IsHoleyElementsKind(kind)) { |
| 15973 | to_kind = GetHoleyElementsKind(to_kind); |
| 15974 | } |
| 15975 | if (IsMoreGeneralElementsKindTransition(kind, to_kind)) { |
| 15976 | // If the array is huge, it's not likely to be defined in a local |
| 15977 | // function, so we shouldn't make new instances of it very often. |
| 15978 | uint32_t length = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 15979 | CHECK(transition_info->length()->ToArrayLength(&length)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15980 | if (length <= kMaximumArrayBytesToPretransition) { |
| 15981 | if (FLAG_trace_track_allocation_sites) { |
| 15982 | bool is_nested = site->IsNestedSite(); |
| 15983 | PrintF( |
| 15984 | "AllocationSite: JSArray %p boilerplate %s updated %s->%s\n", |
| 15985 | reinterpret_cast<void*>(*site), |
| 15986 | is_nested ? "(nested)" : "", |
| 15987 | ElementsKindToString(kind), |
| 15988 | ElementsKindToString(to_kind)); |
| 15989 | } |
| 15990 | JSObject::TransitionElementsKind(transition_info, to_kind); |
| 15991 | site->dependent_code()->DeoptimizeDependentCodeGroup( |
| 15992 | isolate, DependentCode::kAllocationSiteTransitionChangedGroup); |
| 15993 | } |
| 15994 | } |
| 15995 | } else { |
| 15996 | ElementsKind kind = site->GetElementsKind(); |
| 15997 | // if kind is holey ensure that to_kind is as well. |
| 15998 | if (IsHoleyElementsKind(kind)) { |
| 15999 | to_kind = GetHoleyElementsKind(to_kind); |
| 16000 | } |
| 16001 | if (IsMoreGeneralElementsKindTransition(kind, to_kind)) { |
| 16002 | if (FLAG_trace_track_allocation_sites) { |
| 16003 | PrintF("AllocationSite: JSArray %p site updated %s->%s\n", |
| 16004 | reinterpret_cast<void*>(*site), |
| 16005 | ElementsKindToString(kind), |
| 16006 | ElementsKindToString(to_kind)); |
| 16007 | } |
| 16008 | site->SetElementsKind(to_kind); |
| 16009 | site->dependent_code()->DeoptimizeDependentCodeGroup( |
| 16010 | isolate, DependentCode::kAllocationSiteTransitionChangedGroup); |
| 16011 | } |
| 16012 | } |
| 16013 | } |
| 16014 | |
| 16015 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16016 | const char* AllocationSite::PretenureDecisionName(PretenureDecision decision) { |
| 16017 | switch (decision) { |
| 16018 | case kUndecided: return "undecided"; |
| 16019 | case kDontTenure: return "don't tenure"; |
| 16020 | case kMaybeTenure: return "maybe tenure"; |
| 16021 | case kTenure: return "tenure"; |
| 16022 | case kZombie: return "zombie"; |
| 16023 | default: UNREACHABLE(); |
| 16024 | } |
| 16025 | return NULL; |
| 16026 | } |
| 16027 | |
| 16028 | |
| 16029 | void JSObject::UpdateAllocationSite(Handle<JSObject> object, |
| 16030 | ElementsKind to_kind) { |
| 16031 | if (!object->IsJSArray()) return; |
| 16032 | |
| 16033 | Heap* heap = object->GetHeap(); |
| 16034 | if (!heap->InNewSpace(*object)) return; |
| 16035 | |
| 16036 | Handle<AllocationSite> site; |
| 16037 | { |
| 16038 | DisallowHeapAllocation no_allocation; |
| 16039 | |
| 16040 | AllocationMemento* memento = heap->FindAllocationMemento(*object); |
| 16041 | if (memento == NULL) return; |
| 16042 | |
| 16043 | // Walk through to the Allocation Site |
| 16044 | site = handle(memento->GetAllocationSite()); |
| 16045 | } |
| 16046 | AllocationSite::DigestTransitionFeedback(site, to_kind); |
| 16047 | } |
| 16048 | |
| 16049 | |
| 16050 | void JSObject::TransitionElementsKind(Handle<JSObject> object, |
| 16051 | ElementsKind to_kind) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16052 | ElementsKind from_kind = object->GetElementsKind(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16053 | |
| 16054 | if (IsFastHoleyElementsKind(from_kind)) { |
| 16055 | to_kind = GetHoleyElementsKind(to_kind); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16056 | } |
| 16057 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16058 | if (from_kind == to_kind) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16059 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16060 | // This method should never be called for any other case. |
| 16061 | DCHECK(IsFastElementsKind(from_kind)); |
| 16062 | DCHECK(IsFastElementsKind(to_kind)); |
| 16063 | DCHECK_NE(TERMINAL_FAST_ELEMENTS_KIND, from_kind); |
| 16064 | |
| 16065 | UpdateAllocationSite(object, to_kind); |
| 16066 | if (object->elements() == object->GetHeap()->empty_fixed_array() || |
| 16067 | IsFastDoubleElementsKind(from_kind) == |
| 16068 | IsFastDoubleElementsKind(to_kind)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16069 | // No change is needed to the elements() buffer, the transition |
| 16070 | // only requires a map change. |
| 16071 | Handle<Map> new_map = GetElementsTransitionMap(object, to_kind); |
| 16072 | MigrateToMap(object, new_map); |
| 16073 | if (FLAG_trace_elements_transitions) { |
| 16074 | Handle<FixedArrayBase> elms(object->elements()); |
| 16075 | PrintElementsTransition(stdout, object, from_kind, elms, to_kind, elms); |
| 16076 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16077 | } else { |
| 16078 | DCHECK((IsFastSmiElementsKind(from_kind) && |
| 16079 | IsFastDoubleElementsKind(to_kind)) || |
| 16080 | (IsFastDoubleElementsKind(from_kind) && |
| 16081 | IsFastObjectElementsKind(to_kind))); |
| 16082 | uint32_t c = static_cast<uint32_t>(object->elements()->length()); |
| 16083 | ElementsAccessor::ForKind(to_kind)->GrowCapacityAndConvert(object, c); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16084 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16085 | } |
| 16086 | |
| 16087 | |
| 16088 | // static |
| 16089 | bool Map::IsValidElementsTransition(ElementsKind from_kind, |
| 16090 | ElementsKind to_kind) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16091 | // Transitions can't go backwards. |
| 16092 | if (!IsMoreGeneralElementsKindTransition(from_kind, to_kind)) { |
| 16093 | return false; |
| 16094 | } |
| 16095 | |
| 16096 | // Transitions from HOLEY -> PACKED are not allowed. |
| 16097 | return !IsFastHoleyElementsKind(from_kind) || |
| 16098 | IsFastHoleyElementsKind(to_kind); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16099 | } |
| 16100 | |
| 16101 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16102 | bool JSArray::HasReadOnlyLength(Handle<JSArray> array) { |
| 16103 | LookupIterator it(array, array->GetIsolate()->factory()->length_string(), |
| 16104 | LookupIterator::OWN_SKIP_INTERCEPTOR); |
| 16105 | CHECK_NE(LookupIterator::ACCESS_CHECK, it.state()); |
| 16106 | CHECK(it.IsFound()); |
| 16107 | CHECK_EQ(LookupIterator::ACCESSOR, it.state()); |
| 16108 | return it.IsReadOnly(); |
| 16109 | } |
| 16110 | |
| 16111 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16112 | bool JSArray::WouldChangeReadOnlyLength(Handle<JSArray> array, |
| 16113 | uint32_t index) { |
| 16114 | uint32_t length = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16115 | CHECK(array->length()->ToArrayLength(&length)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16116 | if (length <= index) return HasReadOnlyLength(array); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16117 | return false; |
| 16118 | } |
| 16119 | |
| 16120 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16121 | template <typename BackingStore> |
| 16122 | static int FastHoleyElementsUsage(JSObject* object, BackingStore* store) { |
| 16123 | int limit = object->IsJSArray() |
| 16124 | ? Smi::cast(JSArray::cast(object)->length())->value() |
| 16125 | : store->length(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 16126 | int used = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16127 | for (int i = 0; i < limit; ++i) { |
| 16128 | if (!store->is_the_hole(i)) ++used; |
| 16129 | } |
| 16130 | return used; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 16131 | } |
| 16132 | |
| 16133 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16134 | int JSObject::GetFastElementsUsage() { |
| 16135 | FixedArrayBase* store = elements(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16136 | switch (GetElementsKind()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16137 | case FAST_SMI_ELEMENTS: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16138 | case FAST_DOUBLE_ELEMENTS: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16139 | case FAST_ELEMENTS: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16140 | return IsJSArray() ? Smi::cast(JSArray::cast(this)->length())->value() |
| 16141 | : store->length(); |
| 16142 | case FAST_SLOPPY_ARGUMENTS_ELEMENTS: |
| 16143 | store = FixedArray::cast(FixedArray::cast(store)->get(1)); |
| 16144 | // Fall through. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16145 | case FAST_HOLEY_SMI_ELEMENTS: |
| 16146 | case FAST_HOLEY_ELEMENTS: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16147 | return FastHoleyElementsUsage(this, FixedArray::cast(store)); |
| 16148 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
| 16149 | if (elements()->length() == 0) return 0; |
| 16150 | return FastHoleyElementsUsage(this, FixedDoubleArray::cast(store)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16151 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16152 | case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: |
| 16153 | case DICTIONARY_ELEMENTS: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16154 | #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16155 | case TYPE##_ELEMENTS: \ |
| 16156 | |
| 16157 | TYPED_ARRAYS(TYPED_ARRAY_CASE) |
| 16158 | #undef TYPED_ARRAY_CASE |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16159 | UNREACHABLE(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16160 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16161 | return 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16162 | } |
| 16163 | |
| 16164 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16165 | // Certain compilers request function template instantiation when they |
| 16166 | // see the definition of the other template functions in the |
| 16167 | // class. This requires us to have the template functions put |
| 16168 | // together, so even though this function belongs in objects-debug.cc, |
| 16169 | // we keep it here instead to satisfy certain compilers. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 16170 | #ifdef OBJECT_PRINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16171 | template <typename Derived, typename Shape, typename Key> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16172 | void Dictionary<Derived, Shape, Key>::Print(std::ostream& os) { // NOLINT |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16173 | int capacity = this->Capacity(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16174 | for (int i = 0; i < capacity; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16175 | Object* k = this->KeyAt(i); |
| 16176 | if (this->IsKey(k)) { |
| 16177 | os << "\n "; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16178 | if (k->IsString()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16179 | String::cast(k)->StringPrint(os); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16180 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16181 | os << Brief(k); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16182 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16183 | os << ": " << Brief(this->ValueAt(i)) << " " << this->DetailsAt(i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16184 | } |
| 16185 | } |
| 16186 | } |
| 16187 | #endif |
| 16188 | |
| 16189 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16190 | template<typename Derived, typename Shape, typename Key> |
| 16191 | void Dictionary<Derived, Shape, Key>::CopyValuesTo(FixedArray* elements) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16192 | int pos = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16193 | int capacity = this->Capacity(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16194 | DisallowHeapAllocation no_gc; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 16195 | WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16196 | for (int i = 0; i < capacity; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16197 | Object* k = this->KeyAt(i); |
| 16198 | if (this->IsKey(k)) { |
| 16199 | elements->set(pos++, this->ValueAt(i), mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16200 | } |
| 16201 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16202 | DCHECK(pos == elements->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16203 | } |
| 16204 | |
| 16205 | |
| 16206 | InterceptorInfo* JSObject::GetNamedInterceptor() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16207 | DCHECK(map()->has_named_interceptor()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16208 | JSFunction* constructor = JSFunction::cast(map()->GetConstructor()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16209 | DCHECK(constructor->shared()->IsApiFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16210 | Object* result = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 16211 | constructor->shared()->get_api_func_data()->named_property_handler(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16212 | return InterceptorInfo::cast(result); |
| 16213 | } |
| 16214 | |
| 16215 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16216 | MaybeHandle<Object> JSObject::GetPropertyWithInterceptor(LookupIterator* it, |
| 16217 | bool* done) { |
| 16218 | *done = false; |
| 16219 | Isolate* isolate = it->isolate(); |
| 16220 | // Make sure that the top context does not change when doing callbacks or |
| 16221 | // interceptor calls. |
| 16222 | AssertNoContextChange ncc(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16223 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16224 | DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state()); |
| 16225 | Handle<InterceptorInfo> interceptor = it->GetInterceptor(); |
| 16226 | if (interceptor->getter()->IsUndefined()) { |
| 16227 | return isolate->factory()->undefined_value(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16228 | } |
| 16229 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16230 | Handle<JSObject> holder = it->GetHolder<JSObject>(); |
| 16231 | v8::Local<v8::Value> result; |
| 16232 | PropertyCallbackArguments args(isolate, interceptor->data(), |
| 16233 | *it->GetReceiver(), *holder); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16234 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16235 | if (it->IsElement()) { |
| 16236 | uint32_t index = it->index(); |
| 16237 | v8::IndexedPropertyGetterCallback getter = |
| 16238 | v8::ToCData<v8::IndexedPropertyGetterCallback>(interceptor->getter()); |
| 16239 | LOG(isolate, |
| 16240 | ApiIndexedPropertyAccess("interceptor-indexed-get", *holder, index)); |
| 16241 | result = args.Call(getter, index); |
| 16242 | } else { |
| 16243 | Handle<Name> name = it->name(); |
| 16244 | DCHECK(!name->IsPrivate()); |
| 16245 | |
| 16246 | if (name->IsSymbol() && !interceptor->can_intercept_symbols()) { |
| 16247 | return isolate->factory()->undefined_value(); |
| 16248 | } |
| 16249 | |
| 16250 | v8::GenericNamedPropertyGetterCallback getter = |
| 16251 | v8::ToCData<v8::GenericNamedPropertyGetterCallback>( |
| 16252 | interceptor->getter()); |
| 16253 | LOG(isolate, |
| 16254 | ApiNamedPropertyAccess("interceptor-named-get", *holder, *name)); |
| 16255 | result = args.Call(getter, v8::Utils::ToLocal(name)); |
| 16256 | } |
| 16257 | |
| 16258 | RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 16259 | if (result.IsEmpty()) return isolate->factory()->undefined_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16260 | Handle<Object> result_internal = v8::Utils::OpenHandle(*result); |
| 16261 | result_internal->VerifyApiCallResultType(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16262 | *done = true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16263 | // Rebox handle before return |
| 16264 | return handle(*result_internal, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16265 | } |
| 16266 | |
| 16267 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16268 | Maybe<bool> JSObject::HasRealNamedProperty(Handle<JSObject> object, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16269 | Handle<Name> name) { |
| 16270 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 16271 | name->GetIsolate(), object, name, LookupIterator::OWN_SKIP_INTERCEPTOR); |
| 16272 | return HasProperty(&it); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16273 | } |
| 16274 | |
| 16275 | |
| 16276 | Maybe<bool> JSObject::HasRealElementProperty(Handle<JSObject> object, |
| 16277 | uint32_t index) { |
| 16278 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16279 | LookupIterator it(isolate, object, index, |
| 16280 | LookupIterator::OWN_SKIP_INTERCEPTOR); |
| 16281 | return HasProperty(&it); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16282 | } |
| 16283 | |
| 16284 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16285 | Maybe<bool> JSObject::HasRealNamedCallbackProperty(Handle<JSObject> object, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16286 | Handle<Name> name) { |
| 16287 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 16288 | name->GetIsolate(), object, name, LookupIterator::OWN_SKIP_INTERCEPTOR); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16289 | Maybe<PropertyAttributes> maybe_result = GetPropertyAttributes(&it); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16290 | return maybe_result.IsJust() ? Just(it.state() == LookupIterator::ACCESSOR) |
| 16291 | : Nothing<bool>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16292 | } |
| 16293 | |
| 16294 | |
| 16295 | void FixedArray::SwapPairs(FixedArray* numbers, int i, int j) { |
| 16296 | Object* temp = get(i); |
| 16297 | set(i, get(j)); |
| 16298 | set(j, temp); |
| 16299 | if (this != numbers) { |
| 16300 | temp = numbers->get(i); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16301 | numbers->set(i, Smi::cast(numbers->get(j))); |
| 16302 | numbers->set(j, Smi::cast(temp)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16303 | } |
| 16304 | } |
| 16305 | |
| 16306 | |
| 16307 | static void InsertionSortPairs(FixedArray* content, |
| 16308 | FixedArray* numbers, |
| 16309 | int len) { |
| 16310 | for (int i = 1; i < len; i++) { |
| 16311 | int j = i; |
| 16312 | while (j > 0 && |
| 16313 | (NumberToUint32(numbers->get(j - 1)) > |
| 16314 | NumberToUint32(numbers->get(j)))) { |
| 16315 | content->SwapPairs(numbers, j - 1, j); |
| 16316 | j--; |
| 16317 | } |
| 16318 | } |
| 16319 | } |
| 16320 | |
| 16321 | |
| 16322 | void HeapSortPairs(FixedArray* content, FixedArray* numbers, int len) { |
| 16323 | // In-place heap sort. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16324 | DCHECK(content->length() == numbers->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16325 | |
| 16326 | // Bottom-up max-heap construction. |
| 16327 | for (int i = 1; i < len; ++i) { |
| 16328 | int child_index = i; |
| 16329 | while (child_index > 0) { |
| 16330 | int parent_index = ((child_index + 1) >> 1) - 1; |
| 16331 | uint32_t parent_value = NumberToUint32(numbers->get(parent_index)); |
| 16332 | uint32_t child_value = NumberToUint32(numbers->get(child_index)); |
| 16333 | if (parent_value < child_value) { |
| 16334 | content->SwapPairs(numbers, parent_index, child_index); |
| 16335 | } else { |
| 16336 | break; |
| 16337 | } |
| 16338 | child_index = parent_index; |
| 16339 | } |
| 16340 | } |
| 16341 | |
| 16342 | // Extract elements and create sorted array. |
| 16343 | for (int i = len - 1; i > 0; --i) { |
| 16344 | // Put max element at the back of the array. |
| 16345 | content->SwapPairs(numbers, 0, i); |
| 16346 | // Sift down the new top element. |
| 16347 | int parent_index = 0; |
| 16348 | while (true) { |
| 16349 | int child_index = ((parent_index + 1) << 1) - 1; |
| 16350 | if (child_index >= i) break; |
| 16351 | uint32_t child1_value = NumberToUint32(numbers->get(child_index)); |
| 16352 | uint32_t child2_value = NumberToUint32(numbers->get(child_index + 1)); |
| 16353 | uint32_t parent_value = NumberToUint32(numbers->get(parent_index)); |
| 16354 | if (child_index + 1 >= i || child1_value > child2_value) { |
| 16355 | if (parent_value > child1_value) break; |
| 16356 | content->SwapPairs(numbers, parent_index, child_index); |
| 16357 | parent_index = child_index; |
| 16358 | } else { |
| 16359 | if (parent_value > child2_value) break; |
| 16360 | content->SwapPairs(numbers, parent_index, child_index + 1); |
| 16361 | parent_index = child_index + 1; |
| 16362 | } |
| 16363 | } |
| 16364 | } |
| 16365 | } |
| 16366 | |
| 16367 | |
| 16368 | // Sort this array and the numbers as pairs wrt. the (distinct) numbers. |
| 16369 | void FixedArray::SortPairs(FixedArray* numbers, uint32_t len) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16370 | DCHECK(this->length() == numbers->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16371 | // For small arrays, simply use insertion sort. |
| 16372 | if (len <= 10) { |
| 16373 | InsertionSortPairs(this, numbers, len); |
| 16374 | return; |
| 16375 | } |
| 16376 | // Check the range of indices. |
| 16377 | uint32_t min_index = NumberToUint32(numbers->get(0)); |
| 16378 | uint32_t max_index = min_index; |
| 16379 | uint32_t i; |
| 16380 | for (i = 1; i < len; i++) { |
| 16381 | if (NumberToUint32(numbers->get(i)) < min_index) { |
| 16382 | min_index = NumberToUint32(numbers->get(i)); |
| 16383 | } else if (NumberToUint32(numbers->get(i)) > max_index) { |
| 16384 | max_index = NumberToUint32(numbers->get(i)); |
| 16385 | } |
| 16386 | } |
| 16387 | if (max_index - min_index + 1 == len) { |
| 16388 | // Indices form a contiguous range, unless there are duplicates. |
| 16389 | // Do an in-place linear time sort assuming distinct numbers, but |
| 16390 | // avoid hanging in case they are not. |
| 16391 | for (i = 0; i < len; i++) { |
| 16392 | uint32_t p; |
| 16393 | uint32_t j = 0; |
| 16394 | // While the current element at i is not at its correct position p, |
| 16395 | // swap the elements at these two positions. |
| 16396 | while ((p = NumberToUint32(numbers->get(i)) - min_index) != i && |
| 16397 | j++ < len) { |
| 16398 | SwapPairs(numbers, i, p); |
| 16399 | } |
| 16400 | } |
| 16401 | } else { |
| 16402 | HeapSortPairs(this, numbers, len); |
| 16403 | return; |
| 16404 | } |
| 16405 | } |
| 16406 | |
| 16407 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16408 | void JSObject::CollectOwnPropertyNames(KeyAccumulator* keys, |
| 16409 | PropertyFilter filter) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16410 | if (HasFastProperties()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16411 | int real_size = map()->NumberOfOwnDescriptors(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16412 | Handle<DescriptorArray> descs(map()->instance_descriptors()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16413 | for (int i = 0; i < real_size; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16414 | PropertyDetails details = descs->GetDetails(i); |
| 16415 | if ((details.attributes() & filter) != 0) continue; |
| 16416 | if (filter & ONLY_ALL_CAN_READ) { |
| 16417 | if (details.kind() != kAccessor) continue; |
| 16418 | Object* accessors = descs->GetValue(i); |
| 16419 | if (!accessors->IsAccessorInfo()) continue; |
| 16420 | if (!AccessorInfo::cast(accessors)->all_can_read()) continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16421 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16422 | Name* key = descs->GetKey(i); |
| 16423 | if (key->FilterKey(filter)) continue; |
| 16424 | keys->AddKey(key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16425 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16426 | } else if (IsJSGlobalObject()) { |
| 16427 | GlobalDictionary::CollectKeysTo(handle(global_dictionary()), keys, filter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16428 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16429 | NameDictionary::CollectKeysTo(handle(property_dictionary()), keys, filter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16430 | } |
| 16431 | } |
| 16432 | |
| 16433 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16434 | int JSObject::NumberOfOwnElements(PropertyFilter filter) { |
| 16435 | // Fast case for objects with no elements. |
| 16436 | if (!IsJSValue() && HasFastElements()) { |
| 16437 | uint32_t length = |
| 16438 | IsJSArray() |
| 16439 | ? static_cast<uint32_t>( |
| 16440 | Smi::cast(JSArray::cast(this)->length())->value()) |
| 16441 | : static_cast<uint32_t>(FixedArrayBase::cast(elements())->length()); |
| 16442 | if (length == 0) return 0; |
| 16443 | } |
| 16444 | // Compute the number of enumerable elements. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16445 | return GetOwnElementKeys(NULL, filter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16446 | } |
| 16447 | |
| 16448 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16449 | void JSObject::CollectOwnElementKeys(Handle<JSObject> object, |
| 16450 | KeyAccumulator* keys, |
| 16451 | PropertyFilter filter) { |
| 16452 | if (filter & SKIP_STRINGS) return; |
| 16453 | uint32_t string_keys = 0; |
| 16454 | |
| 16455 | // If this is a String wrapper, add the string indices first, |
| 16456 | // as they're guaranteed to precede the elements in numerical order |
| 16457 | // and ascending order is required by ECMA-262, 6th, 9.1.12. |
| 16458 | if (object->IsJSValue()) { |
| 16459 | Object* val = JSValue::cast(*object)->value(); |
| 16460 | if (val->IsString() && (filter & ONLY_ALL_CAN_READ) == 0) { |
| 16461 | String* str = String::cast(val); |
| 16462 | string_keys = str->length(); |
| 16463 | for (uint32_t i = 0; i < string_keys; i++) { |
| 16464 | keys->AddKey(i); |
| 16465 | } |
| 16466 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 16467 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16468 | ElementsAccessor* accessor = object->GetElementsAccessor(); |
| 16469 | accessor->CollectElementIndices(object, keys, kMaxUInt32, filter, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16470 | } |
| 16471 | |
| 16472 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16473 | int JSObject::GetOwnElementKeys(FixedArray* storage, PropertyFilter filter) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16474 | int counter = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16475 | |
| 16476 | // If this is a String wrapper, add the string indices first, |
| 16477 | // as they're guaranteed to precede the elements in numerical order |
| 16478 | // and ascending order is required by ECMA-262, 6th, 9.1.12. |
| 16479 | if (IsJSValue()) { |
| 16480 | Object* val = JSValue::cast(this)->value(); |
| 16481 | if (val->IsString()) { |
| 16482 | String* str = String::cast(val); |
| 16483 | if (storage) { |
| 16484 | for (int i = 0; i < str->length(); i++) { |
| 16485 | storage->set(counter + i, Smi::FromInt(i)); |
| 16486 | } |
| 16487 | } |
| 16488 | counter += str->length(); |
| 16489 | } |
| 16490 | } |
| 16491 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16492 | switch (GetElementsKind()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16493 | case FAST_SMI_ELEMENTS: |
| 16494 | case FAST_ELEMENTS: |
| 16495 | case FAST_HOLEY_SMI_ELEMENTS: |
| 16496 | case FAST_HOLEY_ELEMENTS: { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16497 | int length = IsJSArray() ? |
| 16498 | Smi::cast(JSArray::cast(this)->length())->value() : |
| 16499 | FixedArray::cast(elements())->length(); |
| 16500 | for (int i = 0; i < length; i++) { |
| 16501 | if (!FixedArray::cast(elements())->get(i)->IsTheHole()) { |
| 16502 | if (storage != NULL) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 16503 | storage->set(counter, Smi::FromInt(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16504 | } |
| 16505 | counter++; |
| 16506 | } |
| 16507 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16508 | DCHECK(!storage || storage->length() >= counter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16509 | break; |
| 16510 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16511 | case FAST_DOUBLE_ELEMENTS: |
| 16512 | case FAST_HOLEY_DOUBLE_ELEMENTS: { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16513 | int length = IsJSArray() ? |
| 16514 | Smi::cast(JSArray::cast(this)->length())->value() : |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16515 | FixedArrayBase::cast(elements())->length(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16516 | for (int i = 0; i < length; i++) { |
| 16517 | if (!FixedDoubleArray::cast(elements())->is_the_hole(i)) { |
| 16518 | if (storage != NULL) { |
| 16519 | storage->set(counter, Smi::FromInt(i)); |
| 16520 | } |
| 16521 | counter++; |
| 16522 | } |
| 16523 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16524 | DCHECK(!storage || storage->length() >= counter); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16525 | break; |
| 16526 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16527 | |
| 16528 | #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16529 | case TYPE##_ELEMENTS: \ |
| 16530 | |
| 16531 | TYPED_ARRAYS(TYPED_ARRAY_CASE) |
| 16532 | #undef TYPED_ARRAY_CASE |
| 16533 | { |
| 16534 | int length = FixedArrayBase::cast(elements())->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16535 | while (counter < length) { |
| 16536 | if (storage != NULL) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 16537 | storage->set(counter, Smi::FromInt(counter)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16538 | } |
| 16539 | counter++; |
| 16540 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16541 | DCHECK(!storage || storage->length() >= counter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16542 | break; |
| 16543 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16544 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16545 | case DICTIONARY_ELEMENTS: { |
| 16546 | if (storage != NULL) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16547 | element_dictionary()->CopyKeysTo(storage, counter, filter, |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 16548 | SeededNumberDictionary::SORTED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16549 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16550 | counter += element_dictionary()->NumberOfElementsFilterAttributes(filter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16551 | break; |
| 16552 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16553 | case FAST_SLOPPY_ARGUMENTS_ELEMENTS: |
| 16554 | case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16555 | FixedArray* parameter_map = FixedArray::cast(elements()); |
| 16556 | int mapped_length = parameter_map->length() - 2; |
| 16557 | FixedArray* arguments = FixedArray::cast(parameter_map->get(1)); |
| 16558 | if (arguments->IsDictionary()) { |
| 16559 | // Copy the keys from arguments first, because Dictionary::CopyKeysTo |
| 16560 | // will insert in storage starting at index 0. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 16561 | SeededNumberDictionary* dictionary = |
| 16562 | SeededNumberDictionary::cast(arguments); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16563 | if (storage != NULL) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16564 | dictionary->CopyKeysTo(storage, counter, filter, |
| 16565 | SeededNumberDictionary::UNSORTED); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16566 | } |
| 16567 | counter += dictionary->NumberOfElementsFilterAttributes(filter); |
| 16568 | for (int i = 0; i < mapped_length; ++i) { |
| 16569 | if (!parameter_map->get(i + 2)->IsTheHole()) { |
| 16570 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
| 16571 | ++counter; |
| 16572 | } |
| 16573 | } |
| 16574 | if (storage != NULL) storage->SortPairs(storage, counter); |
| 16575 | |
| 16576 | } else { |
| 16577 | int backing_length = arguments->length(); |
| 16578 | int i = 0; |
| 16579 | for (; i < mapped_length; ++i) { |
| 16580 | if (!parameter_map->get(i + 2)->IsTheHole()) { |
| 16581 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
| 16582 | ++counter; |
| 16583 | } else if (i < backing_length && !arguments->get(i)->IsTheHole()) { |
| 16584 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
| 16585 | ++counter; |
| 16586 | } |
| 16587 | } |
| 16588 | for (; i < backing_length; ++i) { |
| 16589 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
| 16590 | ++counter; |
| 16591 | } |
| 16592 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16593 | break; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16594 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16595 | } |
| 16596 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16597 | DCHECK(!storage || storage->length() == counter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16598 | return counter; |
| 16599 | } |
| 16600 | |
| 16601 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16602 | MaybeHandle<String> Object::ObjectProtoToString(Isolate* isolate, |
| 16603 | Handle<Object> object) { |
| 16604 | if (object->IsUndefined()) return isolate->factory()->undefined_to_string(); |
| 16605 | if (object->IsNull()) return isolate->factory()->null_to_string(); |
| 16606 | |
| 16607 | Handle<JSReceiver> receiver; |
| 16608 | CHECK(Object::ToObject(isolate, object).ToHandle(&receiver)); |
| 16609 | |
| 16610 | Handle<String> tag; |
| 16611 | if (FLAG_harmony_tostring) { |
| 16612 | Handle<Object> to_string_tag; |
| 16613 | ASSIGN_RETURN_ON_EXCEPTION( |
| 16614 | isolate, to_string_tag, |
| 16615 | GetProperty(receiver, isolate->factory()->to_string_tag_symbol()), |
| 16616 | String); |
| 16617 | if (to_string_tag->IsString()) { |
| 16618 | tag = Handle<String>::cast(to_string_tag); |
| 16619 | } |
| 16620 | } |
| 16621 | |
| 16622 | if (tag.is_null()) { |
| 16623 | ASSIGN_RETURN_ON_EXCEPTION(isolate, tag, |
| 16624 | JSReceiver::BuiltinStringTag(receiver), String); |
| 16625 | } |
| 16626 | |
| 16627 | IncrementalStringBuilder builder(isolate); |
| 16628 | builder.AppendCString("[object "); |
| 16629 | builder.AppendString(tag); |
| 16630 | builder.AppendCharacter(']'); |
| 16631 | return builder.Finish(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16632 | } |
| 16633 | |
| 16634 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16635 | const char* Symbol::PrivateSymbolToName() const { |
| 16636 | Heap* heap = GetIsolate()->heap(); |
| 16637 | #define SYMBOL_CHECK_AND_PRINT(name) \ |
| 16638 | if (this == heap->name()) return #name; |
| 16639 | PRIVATE_SYMBOL_LIST(SYMBOL_CHECK_AND_PRINT) |
| 16640 | #undef SYMBOL_CHECK_AND_PRINT |
| 16641 | return "UNKNOWN"; |
| 16642 | } |
| 16643 | |
| 16644 | |
| 16645 | void Symbol::SymbolShortPrint(std::ostream& os) { |
| 16646 | os << "<Symbol: " << Hash(); |
| 16647 | if (!name()->IsUndefined()) { |
| 16648 | os << " "; |
| 16649 | HeapStringAllocator allocator; |
| 16650 | StringStream accumulator(&allocator); |
| 16651 | String::cast(name())->StringShortPrint(&accumulator); |
| 16652 | os << accumulator.ToCString().get(); |
| 16653 | } else { |
| 16654 | os << " (" << PrivateSymbolToName() << ")"; |
| 16655 | } |
| 16656 | os << ">"; |
| 16657 | } |
| 16658 | |
| 16659 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16660 | // StringSharedKeys are used as keys in the eval cache. |
| 16661 | class StringSharedKey : public HashTableKey { |
| 16662 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16663 | StringSharedKey(Handle<String> source, Handle<SharedFunctionInfo> shared, |
| 16664 | LanguageMode language_mode, int scope_position) |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 16665 | : source_(source), |
| 16666 | shared_(shared), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16667 | language_mode_(language_mode), |
| 16668 | scope_position_(scope_position) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16669 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16670 | bool IsMatch(Object* other) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16671 | DisallowHeapAllocation no_allocation; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16672 | if (!other->IsFixedArray()) { |
| 16673 | if (!other->IsNumber()) return false; |
| 16674 | uint32_t other_hash = static_cast<uint32_t>(other->Number()); |
| 16675 | return Hash() == other_hash; |
| 16676 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16677 | FixedArray* other_array = FixedArray::cast(other); |
| 16678 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16679 | if (shared != *shared_) return false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16680 | int language_unchecked = Smi::cast(other_array->get(2))->value(); |
| 16681 | DCHECK(is_valid_language_mode(language_unchecked)); |
| 16682 | LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked); |
| 16683 | if (language_mode != language_mode_) return false; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16684 | int scope_position = Smi::cast(other_array->get(3))->value(); |
| 16685 | if (scope_position != scope_position_) return false; |
| 16686 | String* source = String::cast(other_array->get(1)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16687 | return source->Equals(*source_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16688 | } |
| 16689 | |
| 16690 | static uint32_t StringSharedHashHelper(String* source, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 16691 | SharedFunctionInfo* shared, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16692 | LanguageMode language_mode, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16693 | int scope_position) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16694 | uint32_t hash = source->Hash(); |
| 16695 | if (shared->HasSourceCode()) { |
| 16696 | // Instead of using the SharedFunctionInfo pointer in the hash |
| 16697 | // code computation, we use a combination of the hash of the |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16698 | // script source code and the start position of the calling scope. |
| 16699 | // We do this to ensure that the cache entries can survive garbage |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16700 | // collection. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16701 | Script* script(Script::cast(shared->script())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16702 | hash ^= String::cast(script->source())->Hash(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16703 | STATIC_ASSERT(LANGUAGE_END == 3); |
| 16704 | if (is_strict(language_mode)) hash ^= 0x8000; |
| 16705 | if (is_strong(language_mode)) hash ^= 0x10000; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16706 | hash += scope_position; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16707 | } |
| 16708 | return hash; |
| 16709 | } |
| 16710 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16711 | uint32_t Hash() override { |
| 16712 | return StringSharedHashHelper(*source_, *shared_, language_mode_, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16713 | scope_position_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16714 | } |
| 16715 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16716 | uint32_t HashForObject(Object* obj) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16717 | DisallowHeapAllocation no_allocation; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16718 | if (obj->IsNumber()) { |
| 16719 | return static_cast<uint32_t>(obj->Number()); |
| 16720 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16721 | FixedArray* other_array = FixedArray::cast(obj); |
| 16722 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0)); |
| 16723 | String* source = String::cast(other_array->get(1)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16724 | int language_unchecked = Smi::cast(other_array->get(2))->value(); |
| 16725 | DCHECK(is_valid_language_mode(language_unchecked)); |
| 16726 | LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16727 | int scope_position = Smi::cast(other_array->get(3))->value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16728 | return StringSharedHashHelper(source, shared, language_mode, |
| 16729 | scope_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16730 | } |
| 16731 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16732 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16733 | Handle<Object> AsHandle(Isolate* isolate) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16734 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(4); |
| 16735 | array->set(0, *shared_); |
| 16736 | array->set(1, *source_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16737 | array->set(2, Smi::FromInt(language_mode_)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16738 | array->set(3, Smi::FromInt(scope_position_)); |
| 16739 | return array; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16740 | } |
| 16741 | |
| 16742 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16743 | Handle<String> source_; |
| 16744 | Handle<SharedFunctionInfo> shared_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16745 | LanguageMode language_mode_; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16746 | int scope_position_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16747 | }; |
| 16748 | |
| 16749 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16750 | namespace { |
| 16751 | |
| 16752 | JSRegExp::Flags RegExpFlagsFromString(Handle<String> flags, bool* success) { |
| 16753 | JSRegExp::Flags value = JSRegExp::kNone; |
| 16754 | int length = flags->length(); |
| 16755 | // A longer flags string cannot be valid. |
| 16756 | if (length > 5) return JSRegExp::Flags(0); |
| 16757 | for (int i = 0; i < length; i++) { |
| 16758 | JSRegExp::Flag flag = JSRegExp::kNone; |
| 16759 | switch (flags->Get(i)) { |
| 16760 | case 'g': |
| 16761 | flag = JSRegExp::kGlobal; |
| 16762 | break; |
| 16763 | case 'i': |
| 16764 | flag = JSRegExp::kIgnoreCase; |
| 16765 | break; |
| 16766 | case 'm': |
| 16767 | flag = JSRegExp::kMultiline; |
| 16768 | break; |
| 16769 | case 'u': |
| 16770 | if (!FLAG_harmony_unicode_regexps) return JSRegExp::Flags(0); |
| 16771 | flag = JSRegExp::kUnicode; |
| 16772 | break; |
| 16773 | case 'y': |
| 16774 | if (!FLAG_harmony_regexps) return JSRegExp::Flags(0); |
| 16775 | flag = JSRegExp::kSticky; |
| 16776 | break; |
| 16777 | default: |
| 16778 | return JSRegExp::Flags(0); |
| 16779 | } |
| 16780 | // Duplicate flag. |
| 16781 | if (value & flag) return JSRegExp::Flags(0); |
| 16782 | value |= flag; |
| 16783 | } |
| 16784 | *success = true; |
| 16785 | return value; |
| 16786 | } |
| 16787 | |
| 16788 | } // namespace |
| 16789 | |
| 16790 | |
| 16791 | // static |
| 16792 | MaybeHandle<JSRegExp> JSRegExp::New(Handle<String> pattern, Flags flags) { |
| 16793 | Isolate* isolate = pattern->GetIsolate(); |
| 16794 | Handle<JSFunction> constructor = isolate->regexp_function(); |
| 16795 | Handle<JSRegExp> regexp = |
| 16796 | Handle<JSRegExp>::cast(isolate->factory()->NewJSObject(constructor)); |
| 16797 | |
| 16798 | return JSRegExp::Initialize(regexp, pattern, flags); |
| 16799 | } |
| 16800 | |
| 16801 | |
| 16802 | // static |
| 16803 | MaybeHandle<JSRegExp> JSRegExp::New(Handle<String> pattern, |
| 16804 | Handle<String> flags_string) { |
| 16805 | Isolate* isolate = pattern->GetIsolate(); |
| 16806 | bool success = false; |
| 16807 | Flags flags = RegExpFlagsFromString(flags_string, &success); |
| 16808 | if (!success) { |
| 16809 | THROW_NEW_ERROR( |
| 16810 | isolate, |
| 16811 | NewSyntaxError(MessageTemplate::kInvalidRegExpFlags, flags_string), |
| 16812 | JSRegExp); |
| 16813 | } |
| 16814 | return New(pattern, flags); |
| 16815 | } |
| 16816 | |
| 16817 | |
| 16818 | // static |
| 16819 | Handle<JSRegExp> JSRegExp::Copy(Handle<JSRegExp> regexp) { |
| 16820 | Isolate* const isolate = regexp->GetIsolate(); |
| 16821 | return Handle<JSRegExp>::cast(isolate->factory()->CopyJSObject(regexp)); |
| 16822 | } |
| 16823 | |
| 16824 | |
| 16825 | template <typename Char> |
| 16826 | inline int CountRequiredEscapes(Handle<String> source) { |
| 16827 | DisallowHeapAllocation no_gc; |
| 16828 | int escapes = 0; |
| 16829 | Vector<const Char> src = source->GetCharVector<Char>(); |
| 16830 | for (int i = 0; i < src.length(); i++) { |
| 16831 | if (src[i] == '/' && (i == 0 || src[i - 1] != '\\')) escapes++; |
| 16832 | } |
| 16833 | return escapes; |
| 16834 | } |
| 16835 | |
| 16836 | |
| 16837 | template <typename Char, typename StringType> |
| 16838 | inline Handle<StringType> WriteEscapedRegExpSource(Handle<String> source, |
| 16839 | Handle<StringType> result) { |
| 16840 | DisallowHeapAllocation no_gc; |
| 16841 | Vector<const Char> src = source->GetCharVector<Char>(); |
| 16842 | Vector<Char> dst(result->GetChars(), result->length()); |
| 16843 | int s = 0; |
| 16844 | int d = 0; |
| 16845 | while (s < src.length()) { |
| 16846 | if (src[s] == '/' && (s == 0 || src[s - 1] != '\\')) dst[d++] = '\\'; |
| 16847 | dst[d++] = src[s++]; |
| 16848 | } |
| 16849 | DCHECK_EQ(result->length(), d); |
| 16850 | return result; |
| 16851 | } |
| 16852 | |
| 16853 | |
| 16854 | MaybeHandle<String> EscapeRegExpSource(Isolate* isolate, |
| 16855 | Handle<String> source) { |
| 16856 | String::Flatten(source); |
| 16857 | if (source->length() == 0) return isolate->factory()->query_colon_string(); |
| 16858 | bool one_byte = source->IsOneByteRepresentationUnderneath(); |
| 16859 | int escapes = one_byte ? CountRequiredEscapes<uint8_t>(source) |
| 16860 | : CountRequiredEscapes<uc16>(source); |
| 16861 | if (escapes == 0) return source; |
| 16862 | int length = source->length() + escapes; |
| 16863 | if (one_byte) { |
| 16864 | Handle<SeqOneByteString> result; |
| 16865 | ASSIGN_RETURN_ON_EXCEPTION(isolate, result, |
| 16866 | isolate->factory()->NewRawOneByteString(length), |
| 16867 | String); |
| 16868 | return WriteEscapedRegExpSource<uint8_t>(source, result); |
| 16869 | } else { |
| 16870 | Handle<SeqTwoByteString> result; |
| 16871 | ASSIGN_RETURN_ON_EXCEPTION(isolate, result, |
| 16872 | isolate->factory()->NewRawTwoByteString(length), |
| 16873 | String); |
| 16874 | return WriteEscapedRegExpSource<uc16>(source, result); |
| 16875 | } |
| 16876 | } |
| 16877 | |
| 16878 | |
| 16879 | // static |
| 16880 | MaybeHandle<JSRegExp> JSRegExp::Initialize(Handle<JSRegExp> regexp, |
| 16881 | Handle<String> source, |
| 16882 | Handle<String> flags_string) { |
| 16883 | Isolate* isolate = source->GetIsolate(); |
| 16884 | bool success = false; |
| 16885 | Flags flags = RegExpFlagsFromString(flags_string, &success); |
| 16886 | if (!success) { |
| 16887 | THROW_NEW_ERROR( |
| 16888 | isolate, |
| 16889 | NewSyntaxError(MessageTemplate::kInvalidRegExpFlags, flags_string), |
| 16890 | JSRegExp); |
| 16891 | } |
| 16892 | return Initialize(regexp, source, flags); |
| 16893 | } |
| 16894 | |
| 16895 | |
| 16896 | // static |
| 16897 | MaybeHandle<JSRegExp> JSRegExp::Initialize(Handle<JSRegExp> regexp, |
| 16898 | Handle<String> source, Flags flags) { |
| 16899 | Isolate* isolate = regexp->GetIsolate(); |
| 16900 | Factory* factory = isolate->factory(); |
| 16901 | // If source is the empty string we set it to "(?:)" instead as |
| 16902 | // suggested by ECMA-262, 5th, section 15.10.4.1. |
| 16903 | if (source->length() == 0) source = factory->query_colon_string(); |
| 16904 | |
| 16905 | Handle<String> escaped_source; |
| 16906 | ASSIGN_RETURN_ON_EXCEPTION(isolate, escaped_source, |
| 16907 | EscapeRegExpSource(isolate, source), JSRegExp); |
| 16908 | |
| 16909 | regexp->set_source(*escaped_source); |
| 16910 | regexp->set_flags(Smi::FromInt(flags)); |
| 16911 | |
| 16912 | Map* map = regexp->map(); |
| 16913 | Object* constructor = map->GetConstructor(); |
| 16914 | if (constructor->IsJSFunction() && |
| 16915 | JSFunction::cast(constructor)->initial_map() == map) { |
| 16916 | // If we still have the original map, set in-object properties directly. |
| 16917 | regexp->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex, |
| 16918 | Smi::FromInt(0), SKIP_WRITE_BARRIER); |
| 16919 | } else { |
| 16920 | // Map has changed, so use generic, but slower, method. |
| 16921 | PropertyAttributes writable = |
| 16922 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE); |
| 16923 | JSObject::SetOwnPropertyIgnoreAttributes( |
| 16924 | regexp, factory->last_index_string(), |
| 16925 | Handle<Smi>(Smi::FromInt(0), isolate), writable) |
| 16926 | .Check(); |
| 16927 | } |
| 16928 | |
| 16929 | RETURN_ON_EXCEPTION(isolate, RegExpImpl::Compile(regexp, source, flags), |
| 16930 | JSRegExp); |
| 16931 | |
| 16932 | return regexp; |
| 16933 | } |
| 16934 | |
| 16935 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16936 | // RegExpKey carries the source and flags of a regular expression as key. |
| 16937 | class RegExpKey : public HashTableKey { |
| 16938 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16939 | RegExpKey(Handle<String> string, JSRegExp::Flags flags) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16940 | : string_(string), flags_(Smi::FromInt(flags)) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16941 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 16942 | // Rather than storing the key in the hash table, a pointer to the |
| 16943 | // stored value is stored where the key should be. IsMatch then |
| 16944 | // compares the search key to the found object, rather than comparing |
| 16945 | // a key to a key. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16946 | bool IsMatch(Object* obj) override { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16947 | FixedArray* val = FixedArray::cast(obj); |
| 16948 | return string_->Equals(String::cast(val->get(JSRegExp::kSourceIndex))) |
| 16949 | && (flags_ == val->get(JSRegExp::kFlagsIndex)); |
| 16950 | } |
| 16951 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16952 | uint32_t Hash() override { return RegExpHash(*string_, flags_); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16953 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16954 | Handle<Object> AsHandle(Isolate* isolate) override { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16955 | // Plain hash maps, which is where regexp keys are used, don't |
| 16956 | // use this function. |
| 16957 | UNREACHABLE(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16958 | return MaybeHandle<Object>().ToHandleChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16959 | } |
| 16960 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 16961 | uint32_t HashForObject(Object* obj) override { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16962 | FixedArray* val = FixedArray::cast(obj); |
| 16963 | return RegExpHash(String::cast(val->get(JSRegExp::kSourceIndex)), |
| 16964 | Smi::cast(val->get(JSRegExp::kFlagsIndex))); |
| 16965 | } |
| 16966 | |
| 16967 | static uint32_t RegExpHash(String* string, Smi* flags) { |
| 16968 | return string->Hash() + flags->value(); |
| 16969 | } |
| 16970 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16971 | Handle<String> string_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16972 | Smi* flags_; |
| 16973 | }; |
| 16974 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16975 | |
| 16976 | Handle<Object> OneByteStringKey::AsHandle(Isolate* isolate) { |
| 16977 | if (hash_field_ == 0) Hash(); |
| 16978 | return isolate->factory()->NewOneByteInternalizedString(string_, hash_field_); |
| 16979 | } |
| 16980 | |
| 16981 | |
| 16982 | Handle<Object> TwoByteStringKey::AsHandle(Isolate* isolate) { |
| 16983 | if (hash_field_ == 0) Hash(); |
| 16984 | return isolate->factory()->NewTwoByteInternalizedString(string_, hash_field_); |
| 16985 | } |
| 16986 | |
| 16987 | |
| 16988 | Handle<Object> SeqOneByteSubStringKey::AsHandle(Isolate* isolate) { |
| 16989 | if (hash_field_ == 0) Hash(); |
| 16990 | return isolate->factory()->NewOneByteInternalizedSubString( |
| 16991 | string_, from_, length_, hash_field_); |
| 16992 | } |
| 16993 | |
| 16994 | |
| 16995 | bool SeqOneByteSubStringKey::IsMatch(Object* string) { |
| 16996 | Vector<const uint8_t> chars(string_->GetChars() + from_, length_); |
| 16997 | return String::cast(string)->IsOneByteEqualTo(chars); |
| 16998 | } |
| 16999 | |
| 17000 | |
| 17001 | // InternalizedStringKey carries a string/internalized-string object as key. |
| 17002 | class InternalizedStringKey : public HashTableKey { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17003 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17004 | explicit InternalizedStringKey(Handle<String> string) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 17005 | : string_(string) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17006 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17007 | bool IsMatch(Object* string) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17008 | return String::cast(string)->Equals(*string_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17009 | } |
| 17010 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17011 | uint32_t Hash() override { return string_->Hash(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17012 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17013 | uint32_t HashForObject(Object* other) override { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17014 | return String::cast(other)->Hash(); |
| 17015 | } |
| 17016 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17017 | Handle<Object> AsHandle(Isolate* isolate) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17018 | // Internalize the string if possible. |
| 17019 | MaybeHandle<Map> maybe_map = |
| 17020 | isolate->factory()->InternalizedStringMapForString(string_); |
| 17021 | Handle<Map> map; |
| 17022 | if (maybe_map.ToHandle(&map)) { |
| 17023 | string_->set_map_no_write_barrier(*map); |
| 17024 | DCHECK(string_->IsInternalizedString()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17025 | return string_; |
| 17026 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17027 | // Otherwise allocate a new internalized string. |
| 17028 | return isolate->factory()->NewInternalizedStringImpl( |
| 17029 | string_, string_->length(), string_->hash_field()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17030 | } |
| 17031 | |
| 17032 | static uint32_t StringHash(Object* obj) { |
| 17033 | return String::cast(obj)->Hash(); |
| 17034 | } |
| 17035 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17036 | Handle<String> string_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17037 | }; |
| 17038 | |
| 17039 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17040 | template<typename Derived, typename Shape, typename Key> |
| 17041 | void HashTable<Derived, Shape, Key>::IteratePrefix(ObjectVisitor* v) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17042 | BodyDescriptorBase::IteratePointers(this, 0, kElementsStartOffset, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17043 | } |
| 17044 | |
| 17045 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17046 | template<typename Derived, typename Shape, typename Key> |
| 17047 | void HashTable<Derived, Shape, Key>::IterateElements(ObjectVisitor* v) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17048 | BodyDescriptorBase::IteratePointers(this, kElementsStartOffset, |
| 17049 | kHeaderSize + length() * kPointerSize, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17050 | } |
| 17051 | |
| 17052 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17053 | template<typename Derived, typename Shape, typename Key> |
| 17054 | Handle<Derived> HashTable<Derived, Shape, Key>::New( |
| 17055 | Isolate* isolate, |
| 17056 | int at_least_space_for, |
| 17057 | MinimumCapacity capacity_option, |
| 17058 | PretenureFlag pretenure) { |
| 17059 | DCHECK(0 <= at_least_space_for); |
| 17060 | DCHECK(!capacity_option || base::bits::IsPowerOfTwo32(at_least_space_for)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17061 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17062 | int capacity = (capacity_option == USE_CUSTOM_MINIMUM_CAPACITY) |
| 17063 | ? at_least_space_for |
| 17064 | : ComputeCapacity(at_least_space_for); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17065 | if (capacity > HashTable::kMaxCapacity) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17066 | v8::internal::Heap::FatalProcessOutOfMemory("invalid table size", true); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 17067 | } |
| 17068 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17069 | Factory* factory = isolate->factory(); |
| 17070 | int length = EntryToIndex(capacity); |
| 17071 | Handle<FixedArray> array = factory->NewFixedArray(length, pretenure); |
| 17072 | array->set_map_no_write_barrier(*factory->hash_table_map()); |
| 17073 | Handle<Derived> table = Handle<Derived>::cast(array); |
| 17074 | |
| 17075 | table->SetNumberOfElements(0); |
| 17076 | table->SetNumberOfDeletedElements(0); |
| 17077 | table->SetCapacity(capacity); |
| 17078 | return table; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17079 | } |
| 17080 | |
| 17081 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 17082 | // Find entry for key otherwise return kNotFound. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17083 | template <typename Derived, typename Shape> |
| 17084 | int NameDictionaryBase<Derived, Shape>::FindEntry(Handle<Name> key) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17085 | if (!key->IsUniqueName()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17086 | return DerivedDictionary::FindEntry(key); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 17087 | } |
| 17088 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17089 | // Optimized for unique names. Knowledge of the key type allows: |
| 17090 | // 1. Move the check if the key is unique out of the loop. |
| 17091 | // 2. Avoid comparing hash codes in unique-to-unique comparison. |
| 17092 | // 3. Detect a case when a dictionary key is not unique but the key is. |
| 17093 | // In case of positive result the dictionary key may be replaced by the |
| 17094 | // internalized string with minimal performance penalty. It gives a chance |
| 17095 | // to perform further lookups in code stubs (and significant performance |
| 17096 | // boost a certain style of code). |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 17097 | |
| 17098 | // EnsureCapacity will guarantee the hash table is never full. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17099 | uint32_t capacity = this->Capacity(); |
| 17100 | uint32_t entry = Derived::FirstProbe(key->Hash(), capacity); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 17101 | uint32_t count = 1; |
| 17102 | |
| 17103 | while (true) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17104 | int index = Derived::EntryToIndex(entry); |
| 17105 | Object* element = this->get(index); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 17106 | if (element->IsUndefined()) break; // Empty entry. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17107 | if (*key == element) return entry; |
| 17108 | if (!element->IsUniqueName() && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 17109 | !element->IsTheHole() && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17110 | Name::cast(element)->Equals(*key)) { |
| 17111 | // Replace a key that is a non-internalized string by the equivalent |
| 17112 | // internalized string for faster further lookups. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17113 | this->set(index, *key); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 17114 | return entry; |
| 17115 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17116 | DCHECK(element->IsTheHole() || !Name::cast(element)->Equals(*key)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17117 | entry = Derived::NextProbe(entry, count++, capacity); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 17118 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17119 | return Derived::kNotFound; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 17120 | } |
| 17121 | |
| 17122 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17123 | template<typename Derived, typename Shape, typename Key> |
| 17124 | void HashTable<Derived, Shape, Key>::Rehash( |
| 17125 | Handle<Derived> new_table, |
| 17126 | Key key) { |
| 17127 | DCHECK(NumberOfElements() < new_table->Capacity()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 17128 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17129 | DisallowHeapAllocation no_gc; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17130 | WriteBarrierMode mode = new_table->GetWriteBarrierMode(no_gc); |
| 17131 | |
| 17132 | // Copy prefix to new array. |
| 17133 | for (int i = kPrefixStartIndex; |
| 17134 | i < kPrefixStartIndex + Shape::kPrefixSize; |
| 17135 | i++) { |
| 17136 | new_table->set(i, get(i), mode); |
| 17137 | } |
| 17138 | |
| 17139 | // Rehash the elements. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17140 | int capacity = this->Capacity(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17141 | for (int i = 0; i < capacity; i++) { |
| 17142 | uint32_t from_index = EntryToIndex(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17143 | Object* k = this->get(from_index); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17144 | if (IsKey(k)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17145 | uint32_t hash = this->HashForObject(key, k); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17146 | uint32_t insertion_index = |
| 17147 | EntryToIndex(new_table->FindInsertionEntry(hash)); |
| 17148 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 17149 | new_table->set(insertion_index + j, get(from_index + j), mode); |
| 17150 | } |
| 17151 | } |
| 17152 | } |
| 17153 | new_table->SetNumberOfElements(NumberOfElements()); |
| 17154 | new_table->SetNumberOfDeletedElements(0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17155 | } |
| 17156 | |
| 17157 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17158 | template<typename Derived, typename Shape, typename Key> |
| 17159 | uint32_t HashTable<Derived, Shape, Key>::EntryForProbe( |
| 17160 | Key key, |
| 17161 | Object* k, |
| 17162 | int probe, |
| 17163 | uint32_t expected) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17164 | uint32_t hash = this->HashForObject(key, k); |
| 17165 | uint32_t capacity = this->Capacity(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17166 | uint32_t entry = FirstProbe(hash, capacity); |
| 17167 | for (int i = 1; i < probe; i++) { |
| 17168 | if (entry == expected) return expected; |
| 17169 | entry = NextProbe(entry, i, capacity); |
| 17170 | } |
| 17171 | return entry; |
| 17172 | } |
| 17173 | |
| 17174 | |
| 17175 | template<typename Derived, typename Shape, typename Key> |
| 17176 | void HashTable<Derived, Shape, Key>::Swap(uint32_t entry1, |
| 17177 | uint32_t entry2, |
| 17178 | WriteBarrierMode mode) { |
| 17179 | int index1 = EntryToIndex(entry1); |
| 17180 | int index2 = EntryToIndex(entry2); |
| 17181 | Object* temp[Shape::kEntrySize]; |
| 17182 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 17183 | temp[j] = get(index1 + j); |
| 17184 | } |
| 17185 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 17186 | set(index1 + j, get(index2 + j), mode); |
| 17187 | } |
| 17188 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 17189 | set(index2 + j, temp[j], mode); |
| 17190 | } |
| 17191 | } |
| 17192 | |
| 17193 | |
| 17194 | template<typename Derived, typename Shape, typename Key> |
| 17195 | void HashTable<Derived, Shape, Key>::Rehash(Key key) { |
| 17196 | DisallowHeapAllocation no_gc; |
| 17197 | WriteBarrierMode mode = GetWriteBarrierMode(no_gc); |
| 17198 | uint32_t capacity = Capacity(); |
| 17199 | bool done = false; |
| 17200 | for (int probe = 1; !done; probe++) { |
| 17201 | // All elements at entries given by one of the first _probe_ probes |
| 17202 | // are placed correctly. Other elements might need to be moved. |
| 17203 | done = true; |
| 17204 | for (uint32_t current = 0; current < capacity; current++) { |
| 17205 | Object* current_key = get(EntryToIndex(current)); |
| 17206 | if (IsKey(current_key)) { |
| 17207 | uint32_t target = EntryForProbe(key, current_key, probe, current); |
| 17208 | if (current == target) continue; |
| 17209 | Object* target_key = get(EntryToIndex(target)); |
| 17210 | if (!IsKey(target_key) || |
| 17211 | EntryForProbe(key, target_key, probe, target) != target) { |
| 17212 | // Put the current element into the correct position. |
| 17213 | Swap(current, target, mode); |
| 17214 | // The other element will be processed on the next iteration. |
| 17215 | current--; |
| 17216 | } else { |
| 17217 | // The place for the current element is occupied. Leave the element |
| 17218 | // for the next probe. |
| 17219 | done = false; |
| 17220 | } |
| 17221 | } |
| 17222 | } |
| 17223 | } |
| 17224 | } |
| 17225 | |
| 17226 | |
| 17227 | template<typename Derived, typename Shape, typename Key> |
| 17228 | Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity( |
| 17229 | Handle<Derived> table, |
| 17230 | int n, |
| 17231 | Key key, |
| 17232 | PretenureFlag pretenure) { |
| 17233 | Isolate* isolate = table->GetIsolate(); |
| 17234 | int capacity = table->Capacity(); |
| 17235 | int nof = table->NumberOfElements() + n; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17236 | |
| 17237 | if (table->HasSufficientCapacity(n)) return table; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17238 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 17239 | const int kMinCapacityForPretenure = 256; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17240 | bool should_pretenure = pretenure == TENURED || |
| 17241 | ((capacity > kMinCapacityForPretenure) && |
| 17242 | !isolate->heap()->InNewSpace(*table)); |
| 17243 | Handle<Derived> new_table = HashTable::New( |
| 17244 | isolate, |
| 17245 | nof * 2, |
| 17246 | USE_DEFAULT_MINIMUM_CAPACITY, |
| 17247 | should_pretenure ? TENURED : NOT_TENURED); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 17248 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17249 | table->Rehash(new_table, key); |
| 17250 | return new_table; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17251 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17252 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17253 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17254 | template <typename Derived, typename Shape, typename Key> |
| 17255 | bool HashTable<Derived, Shape, Key>::HasSufficientCapacity(int n) { |
| 17256 | int capacity = Capacity(); |
| 17257 | int nof = NumberOfElements() + n; |
| 17258 | int nod = NumberOfDeletedElements(); |
| 17259 | // Return true if: |
| 17260 | // 50% is still free after adding n elements and |
| 17261 | // at most 50% of the free elements are deleted elements. |
| 17262 | if (nod <= (capacity - nof) >> 1) { |
| 17263 | int needed_free = nof >> 1; |
| 17264 | if (nof + needed_free <= capacity) return true; |
| 17265 | } |
| 17266 | return false; |
| 17267 | } |
| 17268 | |
| 17269 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17270 | template<typename Derived, typename Shape, typename Key> |
| 17271 | Handle<Derived> HashTable<Derived, Shape, Key>::Shrink(Handle<Derived> table, |
| 17272 | Key key) { |
| 17273 | int capacity = table->Capacity(); |
| 17274 | int nof = table->NumberOfElements(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17275 | |
| 17276 | // Shrink to fit the number of elements if only a quarter of the |
| 17277 | // capacity is filled with elements. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17278 | if (nof > (capacity >> 2)) return table; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17279 | // Allocate a new dictionary with room for at least the current |
| 17280 | // number of elements. The allocation method will make sure that |
| 17281 | // there is extra room in the dictionary for additions. Don't go |
| 17282 | // lower than room for 16 elements. |
| 17283 | int at_least_room_for = nof; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17284 | if (at_least_room_for < 16) return table; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17285 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17286 | Isolate* isolate = table->GetIsolate(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17287 | const int kMinCapacityForPretenure = 256; |
| 17288 | bool pretenure = |
| 17289 | (at_least_room_for > kMinCapacityForPretenure) && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17290 | !isolate->heap()->InNewSpace(*table); |
| 17291 | Handle<Derived> new_table = HashTable::New( |
| 17292 | isolate, |
| 17293 | at_least_room_for, |
| 17294 | USE_DEFAULT_MINIMUM_CAPACITY, |
| 17295 | pretenure ? TENURED : NOT_TENURED); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17296 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17297 | table->Rehash(new_table, key); |
| 17298 | return new_table; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17299 | } |
| 17300 | |
| 17301 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17302 | template<typename Derived, typename Shape, typename Key> |
| 17303 | uint32_t HashTable<Derived, Shape, Key>::FindInsertionEntry(uint32_t hash) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17304 | uint32_t capacity = Capacity(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 17305 | uint32_t entry = FirstProbe(hash, capacity); |
| 17306 | uint32_t count = 1; |
| 17307 | // EnsureCapacity will guarantee the hash table is never full. |
| 17308 | while (true) { |
| 17309 | Object* element = KeyAt(entry); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 17310 | if (element->IsUndefined() || element->IsTheHole()) break; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 17311 | entry = NextProbe(entry, count++, capacity); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17312 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17313 | return entry; |
| 17314 | } |
| 17315 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17316 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17317 | // Force instantiation of template instances class. |
| 17318 | // Please note this list is compiler dependent. |
| 17319 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17320 | template class HashTable<StringTable, StringTableShape, HashTableKey*>; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17321 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17322 | template class HashTable<CompilationCacheTable, |
| 17323 | CompilationCacheShape, |
| 17324 | HashTableKey*>; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17325 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17326 | template class HashTable<ObjectHashTable, |
| 17327 | ObjectHashTableShape, |
| 17328 | Handle<Object> >; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 17329 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17330 | template class HashTable<WeakHashTable, WeakHashTableShape<2>, Handle<Object> >; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17331 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17332 | template class Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17333 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17334 | template class Dictionary<GlobalDictionary, GlobalDictionaryShape, |
| 17335 | Handle<Name> >; |
| 17336 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17337 | template class Dictionary<SeededNumberDictionary, |
| 17338 | SeededNumberDictionaryShape, |
| 17339 | uint32_t>; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17340 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17341 | template class Dictionary<UnseededNumberDictionary, |
| 17342 | UnseededNumberDictionaryShape, |
| 17343 | uint32_t>; |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 17344 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17345 | template Handle<SeededNumberDictionary> |
| 17346 | Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>:: |
| 17347 | New(Isolate*, int at_least_space_for, PretenureFlag pretenure); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 17348 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17349 | template Handle<UnseededNumberDictionary> |
| 17350 | Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>:: |
| 17351 | New(Isolate*, int at_least_space_for, PretenureFlag pretenure); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17352 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17353 | template Handle<NameDictionary> |
| 17354 | Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >:: |
| 17355 | New(Isolate*, int n, PretenureFlag pretenure); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17356 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17357 | template Handle<GlobalDictionary> |
| 17358 | Dictionary<GlobalDictionary, GlobalDictionaryShape, Handle<Name> >::New( |
| 17359 | Isolate*, int n, PretenureFlag pretenure); |
| 17360 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17361 | template Handle<SeededNumberDictionary> |
| 17362 | Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>:: |
| 17363 | AtPut(Handle<SeededNumberDictionary>, uint32_t, Handle<Object>); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17364 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17365 | template Handle<UnseededNumberDictionary> |
| 17366 | Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>:: |
| 17367 | AtPut(Handle<UnseededNumberDictionary>, uint32_t, Handle<Object>); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 17368 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17369 | template Object* |
| 17370 | Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>:: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 17371 | SlowReverseLookup(Object* value); |
| 17372 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17373 | template Object* |
| 17374 | Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >:: |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 17375 | SlowReverseLookup(Object* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17376 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17377 | template Handle<Object> |
| 17378 | Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::DeleteProperty( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17379 | Handle<NameDictionary>, int); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17380 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17381 | template Handle<Object> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17382 | Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, |
| 17383 | uint32_t>::DeleteProperty(Handle<SeededNumberDictionary>, int); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17384 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17385 | template Handle<NameDictionary> |
| 17386 | HashTable<NameDictionary, NameDictionaryShape, Handle<Name> >:: |
| 17387 | New(Isolate*, int, MinimumCapacity, PretenureFlag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17388 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17389 | template Handle<NameDictionary> |
| 17390 | HashTable<NameDictionary, NameDictionaryShape, Handle<Name> >:: |
| 17391 | Shrink(Handle<NameDictionary>, Handle<Name>); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17392 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17393 | template Handle<SeededNumberDictionary> |
| 17394 | HashTable<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>:: |
| 17395 | Shrink(Handle<SeededNumberDictionary>, uint32_t); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17396 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17397 | template Handle<NameDictionary> |
| 17398 | Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::Add( |
| 17399 | Handle<NameDictionary>, Handle<Name>, Handle<Object>, PropertyDetails); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17400 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17401 | template Handle<GlobalDictionary> |
| 17402 | Dictionary<GlobalDictionary, GlobalDictionaryShape, Handle<Name> >::Add( |
| 17403 | Handle<GlobalDictionary>, Handle<Name>, Handle<Object>, |
| 17404 | PropertyDetails); |
| 17405 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17406 | template Handle<FixedArray> Dictionary< |
| 17407 | NameDictionary, NameDictionaryShape, |
| 17408 | Handle<Name> >::BuildIterationIndicesArray(Handle<NameDictionary>); |
| 17409 | |
| 17410 | template Handle<FixedArray> Dictionary< |
| 17411 | NameDictionary, NameDictionaryShape, |
| 17412 | Handle<Name> >::GenerateNewEnumerationIndices(Handle<NameDictionary>); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 17413 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17414 | template Handle<SeededNumberDictionary> |
| 17415 | Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>:: |
| 17416 | Add(Handle<SeededNumberDictionary>, |
| 17417 | uint32_t, |
| 17418 | Handle<Object>, |
| 17419 | PropertyDetails); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17420 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17421 | template Handle<UnseededNumberDictionary> |
| 17422 | Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>:: |
| 17423 | Add(Handle<UnseededNumberDictionary>, |
| 17424 | uint32_t, |
| 17425 | Handle<Object>, |
| 17426 | PropertyDetails); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17427 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17428 | template Handle<SeededNumberDictionary> |
| 17429 | Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>:: |
| 17430 | EnsureCapacity(Handle<SeededNumberDictionary>, int, uint32_t); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 17431 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17432 | template Handle<UnseededNumberDictionary> |
| 17433 | Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>:: |
| 17434 | EnsureCapacity(Handle<UnseededNumberDictionary>, int, uint32_t); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17435 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17436 | template void Dictionary<NameDictionary, NameDictionaryShape, |
| 17437 | Handle<Name> >::SetRequiresCopyOnCapacityChange(); |
| 17438 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17439 | template Handle<NameDictionary> |
| 17440 | Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >:: |
| 17441 | EnsureCapacity(Handle<NameDictionary>, int, Handle<Name>); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17442 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17443 | template bool Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, |
| 17444 | uint32_t>::HasComplexElements(); |
| 17445 | |
| 17446 | template int HashTable<SeededNumberDictionary, SeededNumberDictionaryShape, |
| 17447 | uint32_t>::FindEntry(uint32_t); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 17448 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17449 | template int NameDictionaryBase<NameDictionary, NameDictionaryShape>::FindEntry( |
| 17450 | Handle<Name>); |
| 17451 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 17452 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17453 | Handle<Object> JSObject::PrepareSlowElementsForSort( |
| 17454 | Handle<JSObject> object, uint32_t limit) { |
| 17455 | DCHECK(object->HasDictionaryElements()); |
| 17456 | Isolate* isolate = object->GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17457 | // Must stay in dictionary mode, either because of requires_slow_elements, |
| 17458 | // or because we are not going to sort (and therefore compact) all of the |
| 17459 | // elements. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17460 | Handle<SeededNumberDictionary> dict(object->element_dictionary(), isolate); |
| 17461 | Handle<SeededNumberDictionary> new_dict = |
| 17462 | SeededNumberDictionary::New(isolate, dict->NumberOfElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17463 | |
| 17464 | uint32_t pos = 0; |
| 17465 | uint32_t undefs = 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 17466 | int capacity = dict->Capacity(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17467 | Handle<Smi> bailout(Smi::FromInt(-1), isolate); |
| 17468 | // Entry to the new dictionary does not cause it to grow, as we have |
| 17469 | // allocated one that is large enough for all entries. |
| 17470 | DisallowHeapAllocation no_gc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17471 | for (int i = 0; i < capacity; i++) { |
| 17472 | Object* k = dict->KeyAt(i); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17473 | if (!dict->IsKey(k)) continue; |
| 17474 | |
| 17475 | DCHECK(k->IsNumber()); |
| 17476 | DCHECK(!k->IsSmi() || Smi::cast(k)->value() >= 0); |
| 17477 | DCHECK(!k->IsHeapNumber() || HeapNumber::cast(k)->value() >= 0); |
| 17478 | DCHECK(!k->IsHeapNumber() || HeapNumber::cast(k)->value() <= kMaxUInt32); |
| 17479 | |
| 17480 | HandleScope scope(isolate); |
| 17481 | Handle<Object> value(dict->ValueAt(i), isolate); |
| 17482 | PropertyDetails details = dict->DetailsAt(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17483 | if (details.type() == ACCESSOR_CONSTANT || details.IsReadOnly()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17484 | // Bail out and do the sorting of undefineds and array holes in JS. |
| 17485 | // Also bail out if the element is not supposed to be moved. |
| 17486 | return bailout; |
| 17487 | } |
| 17488 | |
| 17489 | uint32_t key = NumberToUint32(k); |
| 17490 | if (key < limit) { |
| 17491 | if (value->IsUndefined()) { |
| 17492 | undefs++; |
| 17493 | } else if (pos > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 17494 | // Adding an entry with the key beyond smi-range requires |
| 17495 | // allocation. Bailout. |
| 17496 | return bailout; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17497 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17498 | Handle<Object> result = SeededNumberDictionary::AddNumberEntry( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17499 | new_dict, pos, value, details, object->map()->is_prototype_map()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17500 | DCHECK(result.is_identical_to(new_dict)); |
| 17501 | USE(result); |
| 17502 | pos++; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17503 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17504 | } else if (key > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 17505 | // Adding an entry with the key beyond smi-range requires |
| 17506 | // allocation. Bailout. |
| 17507 | return bailout; |
| 17508 | } else { |
| 17509 | Handle<Object> result = SeededNumberDictionary::AddNumberEntry( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17510 | new_dict, key, value, details, object->map()->is_prototype_map()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17511 | DCHECK(result.is_identical_to(new_dict)); |
| 17512 | USE(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17513 | } |
| 17514 | } |
| 17515 | |
| 17516 | uint32_t result = pos; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17517 | PropertyDetails no_details = PropertyDetails::Empty(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17518 | while (undefs > 0) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 17519 | if (pos > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 17520 | // Adding an entry with the key beyond smi-range requires |
| 17521 | // allocation. Bailout. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17522 | return bailout; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 17523 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17524 | HandleScope scope(isolate); |
| 17525 | Handle<Object> result = SeededNumberDictionary::AddNumberEntry( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17526 | new_dict, pos, isolate->factory()->undefined_value(), no_details, |
| 17527 | object->map()->is_prototype_map()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17528 | DCHECK(result.is_identical_to(new_dict)); |
| 17529 | USE(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17530 | pos++; |
| 17531 | undefs--; |
| 17532 | } |
| 17533 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17534 | object->set_elements(*new_dict); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17535 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17536 | AllowHeapAllocation allocate_return_value; |
| 17537 | return isolate->factory()->NewNumberFromUint(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17538 | } |
| 17539 | |
| 17540 | |
| 17541 | // Collects all defined (non-hole) and non-undefined (array) elements at |
| 17542 | // the start of the elements array. |
| 17543 | // If the object is in dictionary mode, it is converted to fast elements |
| 17544 | // mode. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17545 | Handle<Object> JSObject::PrepareElementsForSort(Handle<JSObject> object, |
| 17546 | uint32_t limit) { |
| 17547 | Isolate* isolate = object->GetIsolate(); |
| 17548 | if (object->HasSloppyArgumentsElements() || |
| 17549 | object->map()->is_observed()) { |
| 17550 | return handle(Smi::FromInt(-1), isolate); |
| 17551 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 17552 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17553 | if (object->HasDictionaryElements()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17554 | // Convert to fast elements containing only the existing properties. |
| 17555 | // Ordering is irrelevant, since we are going to sort anyway. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17556 | Handle<SeededNumberDictionary> dict(object->element_dictionary()); |
| 17557 | if (object->IsJSArray() || dict->requires_slow_elements() || |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17558 | dict->max_number_key() >= limit) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17559 | return JSObject::PrepareSlowElementsForSort(object, limit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17560 | } |
| 17561 | // Convert to fast elements. |
| 17562 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17563 | Handle<Map> new_map = |
| 17564 | JSObject::GetElementsTransitionMap(object, FAST_HOLEY_ELEMENTS); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 17565 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17566 | PretenureFlag tenure = isolate->heap()->InNewSpace(*object) ? |
| 17567 | NOT_TENURED: TENURED; |
| 17568 | Handle<FixedArray> fast_elements = |
| 17569 | isolate->factory()->NewFixedArray(dict->NumberOfElements(), tenure); |
| 17570 | dict->CopyValuesTo(*fast_elements); |
| 17571 | JSObject::ValidateElements(object); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 17572 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17573 | JSObject::SetMapAndElements(object, new_map, fast_elements); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17574 | } else if (object->HasFixedTypedArrayElements()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17575 | // Typed arrays cannot have holes or undefined elements. |
| 17576 | return handle(Smi::FromInt( |
| 17577 | FixedArrayBase::cast(object->elements())->length()), isolate); |
| 17578 | } else if (!object->HasFastDoubleElements()) { |
| 17579 | EnsureWritableFastElements(object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17580 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17581 | DCHECK(object->HasFastSmiOrObjectElements() || |
| 17582 | object->HasFastDoubleElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17583 | |
| 17584 | // Collect holes at the end, undefined before that and the rest at the |
| 17585 | // start, and return the number of non-hole, non-undefined values. |
| 17586 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17587 | Handle<FixedArrayBase> elements_base(object->elements()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17588 | uint32_t elements_length = static_cast<uint32_t>(elements_base->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17589 | if (limit > elements_length) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17590 | limit = elements_length; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17591 | } |
| 17592 | if (limit == 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17593 | return handle(Smi::FromInt(0), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17594 | } |
| 17595 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17596 | uint32_t result = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17597 | if (elements_base->map() == isolate->heap()->fixed_double_array_map()) { |
| 17598 | FixedDoubleArray* elements = FixedDoubleArray::cast(*elements_base); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17599 | // Split elements into defined and the_hole, in that order. |
| 17600 | unsigned int holes = limit; |
| 17601 | // Assume most arrays contain no holes and undefined values, so minimize the |
| 17602 | // number of stores of non-undefined, non-the-hole values. |
| 17603 | for (unsigned int i = 0; i < holes; i++) { |
| 17604 | if (elements->is_the_hole(i)) { |
| 17605 | holes--; |
| 17606 | } else { |
| 17607 | continue; |
| 17608 | } |
| 17609 | // Position i needs to be filled. |
| 17610 | while (holes > i) { |
| 17611 | if (elements->is_the_hole(holes)) { |
| 17612 | holes--; |
| 17613 | } else { |
| 17614 | elements->set(i, elements->get_scalar(holes)); |
| 17615 | break; |
| 17616 | } |
| 17617 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17618 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17619 | result = holes; |
| 17620 | while (holes < limit) { |
| 17621 | elements->set_the_hole(holes); |
| 17622 | holes++; |
| 17623 | } |
| 17624 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17625 | FixedArray* elements = FixedArray::cast(*elements_base); |
| 17626 | DisallowHeapAllocation no_gc; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17627 | |
| 17628 | // Split elements into defined, undefined and the_hole, in that order. Only |
| 17629 | // count locations for undefined and the hole, and fill them afterwards. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17630 | WriteBarrierMode write_barrier = elements->GetWriteBarrierMode(no_gc); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17631 | unsigned int undefs = limit; |
| 17632 | unsigned int holes = limit; |
| 17633 | // Assume most arrays contain no holes and undefined values, so minimize the |
| 17634 | // number of stores of non-undefined, non-the-hole values. |
| 17635 | for (unsigned int i = 0; i < undefs; i++) { |
| 17636 | Object* current = elements->get(i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17637 | if (current->IsTheHole()) { |
| 17638 | holes--; |
| 17639 | undefs--; |
| 17640 | } else if (current->IsUndefined()) { |
| 17641 | undefs--; |
| 17642 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17643 | continue; |
| 17644 | } |
| 17645 | // Position i needs to be filled. |
| 17646 | while (undefs > i) { |
| 17647 | current = elements->get(undefs); |
| 17648 | if (current->IsTheHole()) { |
| 17649 | holes--; |
| 17650 | undefs--; |
| 17651 | } else if (current->IsUndefined()) { |
| 17652 | undefs--; |
| 17653 | } else { |
| 17654 | elements->set(i, current, write_barrier); |
| 17655 | break; |
| 17656 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17657 | } |
| 17658 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17659 | result = undefs; |
| 17660 | while (undefs < holes) { |
| 17661 | elements->set_undefined(undefs); |
| 17662 | undefs++; |
| 17663 | } |
| 17664 | while (holes < limit) { |
| 17665 | elements->set_the_hole(holes); |
| 17666 | holes++; |
| 17667 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17668 | } |
| 17669 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17670 | return isolate->factory()->NewNumberFromUint(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17671 | } |
| 17672 | |
| 17673 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17674 | ExternalArrayType JSTypedArray::type() { |
| 17675 | switch (elements()->map()->instance_type()) { |
| 17676 | #define INSTANCE_TYPE_TO_ARRAY_TYPE(Type, type, TYPE, ctype, size) \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17677 | case FIXED_##TYPE##_ARRAY_TYPE: \ |
| 17678 | return kExternal##Type##Array; |
| 17679 | |
| 17680 | TYPED_ARRAYS(INSTANCE_TYPE_TO_ARRAY_TYPE) |
| 17681 | #undef INSTANCE_TYPE_TO_ARRAY_TYPE |
| 17682 | |
| 17683 | default: |
| 17684 | UNREACHABLE(); |
| 17685 | return static_cast<ExternalArrayType>(-1); |
| 17686 | } |
| 17687 | } |
| 17688 | |
| 17689 | |
| 17690 | size_t JSTypedArray::element_size() { |
| 17691 | switch (elements()->map()->instance_type()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17692 | #define INSTANCE_TYPE_TO_ELEMENT_SIZE(Type, type, TYPE, ctype, size) \ |
| 17693 | case FIXED_##TYPE##_ARRAY_TYPE: \ |
| 17694 | return size; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17695 | |
| 17696 | TYPED_ARRAYS(INSTANCE_TYPE_TO_ELEMENT_SIZE) |
| 17697 | #undef INSTANCE_TYPE_TO_ELEMENT_SIZE |
| 17698 | |
| 17699 | default: |
| 17700 | UNREACHABLE(); |
| 17701 | return 0; |
| 17702 | } |
| 17703 | } |
| 17704 | |
| 17705 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17706 | void JSGlobalObject::InvalidatePropertyCell(Handle<JSGlobalObject> global, |
| 17707 | Handle<Name> name) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17708 | DCHECK(!global->HasFastProperties()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17709 | auto dictionary = handle(global->global_dictionary()); |
| 17710 | int entry = dictionary->FindEntry(name); |
| 17711 | if (entry == GlobalDictionary::kNotFound) return; |
| 17712 | PropertyCell::InvalidateEntry(dictionary, entry); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17713 | } |
| 17714 | |
| 17715 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17716 | // TODO(ishell): rename to EnsureEmptyPropertyCell or something. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17717 | Handle<PropertyCell> JSGlobalObject::EnsurePropertyCell( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17718 | Handle<JSGlobalObject> global, Handle<Name> name) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17719 | DCHECK(!global->HasFastProperties()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17720 | auto dictionary = handle(global->global_dictionary()); |
| 17721 | int entry = dictionary->FindEntry(name); |
| 17722 | Handle<PropertyCell> cell; |
| 17723 | if (entry != GlobalDictionary::kNotFound) { |
| 17724 | // This call should be idempotent. |
| 17725 | DCHECK(dictionary->ValueAt(entry)->IsPropertyCell()); |
| 17726 | cell = handle(PropertyCell::cast(dictionary->ValueAt(entry))); |
| 17727 | DCHECK(cell->property_details().cell_type() == |
| 17728 | PropertyCellType::kUninitialized || |
| 17729 | cell->property_details().cell_type() == |
| 17730 | PropertyCellType::kInvalidated); |
| 17731 | DCHECK(cell->value()->IsTheHole()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17732 | return cell; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17733 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17734 | Isolate* isolate = global->GetIsolate(); |
| 17735 | cell = isolate->factory()->NewPropertyCell(); |
| 17736 | PropertyDetails details(NONE, DATA, 0, PropertyCellType::kUninitialized); |
| 17737 | dictionary = GlobalDictionary::Add(dictionary, name, cell, details); |
| 17738 | global->set_properties(*dictionary); |
| 17739 | return cell; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17740 | } |
| 17741 | |
| 17742 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17743 | // This class is used for looking up two character strings in the string table. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17744 | // If we don't have a hit we don't want to waste much time so we unroll the |
| 17745 | // string hash calculation loop here for speed. Doesn't work if the two |
| 17746 | // characters form a decimal integer, since such strings have a different hash |
| 17747 | // algorithm. |
| 17748 | class TwoCharHashTableKey : public HashTableKey { |
| 17749 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17750 | TwoCharHashTableKey(uint16_t c1, uint16_t c2, uint32_t seed) |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17751 | : c1_(c1), c2_(c2) { |
| 17752 | // Char 1. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 17753 | uint32_t hash = seed; |
| 17754 | hash += c1; |
| 17755 | hash += hash << 10; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17756 | hash ^= hash >> 6; |
| 17757 | // Char 2. |
| 17758 | hash += c2; |
| 17759 | hash += hash << 10; |
| 17760 | hash ^= hash >> 6; |
| 17761 | // GetHash. |
| 17762 | hash += hash << 3; |
| 17763 | hash ^= hash >> 11; |
| 17764 | hash += hash << 15; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17765 | if ((hash & String::kHashBitMask) == 0) hash = StringHasher::kZeroHash; |
| 17766 | hash_ = hash; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17767 | #ifdef DEBUG |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17768 | // If this assert fails then we failed to reproduce the two-character |
| 17769 | // version of the string hashing algorithm above. One reason could be |
| 17770 | // that we were passed two digits as characters, since the hash |
| 17771 | // algorithm is different in that case. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17772 | uint16_t chars[2] = {c1, c2}; |
| 17773 | uint32_t check_hash = StringHasher::HashSequentialString(chars, 2, seed); |
| 17774 | hash = (hash << String::kHashShift) | String::kIsNotArrayIndexMask; |
| 17775 | DCHECK_EQ(static_cast<int32_t>(hash), static_cast<int32_t>(check_hash)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17776 | #endif |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17777 | } |
| 17778 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17779 | bool IsMatch(Object* o) override { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17780 | if (!o->IsString()) return false; |
| 17781 | String* other = String::cast(o); |
| 17782 | if (other->length() != 2) return false; |
| 17783 | if (other->Get(0) != c1_) return false; |
| 17784 | return other->Get(1) == c2_; |
| 17785 | } |
| 17786 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17787 | uint32_t Hash() override { return hash_; } |
| 17788 | uint32_t HashForObject(Object* key) override { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17789 | if (!key->IsString()) return 0; |
| 17790 | return String::cast(key)->Hash(); |
| 17791 | } |
| 17792 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17793 | Handle<Object> AsHandle(Isolate* isolate) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17794 | // The TwoCharHashTableKey is only used for looking in the string |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17795 | // table, not for adding to it. |
| 17796 | UNREACHABLE(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17797 | return MaybeHandle<Object>().ToHandleChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17798 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17799 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17800 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17801 | uint16_t c1_; |
| 17802 | uint16_t c2_; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17803 | uint32_t hash_; |
| 17804 | }; |
| 17805 | |
| 17806 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17807 | MaybeHandle<String> StringTable::InternalizeStringIfExists( |
| 17808 | Isolate* isolate, |
| 17809 | Handle<String> string) { |
| 17810 | if (string->IsInternalizedString()) { |
| 17811 | return string; |
| 17812 | } |
| 17813 | return LookupStringIfExists(isolate, string); |
| 17814 | } |
| 17815 | |
| 17816 | |
| 17817 | MaybeHandle<String> StringTable::LookupStringIfExists( |
| 17818 | Isolate* isolate, |
| 17819 | Handle<String> string) { |
| 17820 | Handle<StringTable> string_table = isolate->factory()->string_table(); |
| 17821 | InternalizedStringKey key(string); |
| 17822 | int entry = string_table->FindEntry(&key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17823 | if (entry == kNotFound) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17824 | return MaybeHandle<String>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17825 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17826 | Handle<String> result(String::cast(string_table->KeyAt(entry)), isolate); |
| 17827 | DCHECK(StringShape(*result).IsInternalized()); |
| 17828 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17829 | } |
| 17830 | } |
| 17831 | |
| 17832 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17833 | MaybeHandle<String> StringTable::LookupTwoCharsStringIfExists( |
| 17834 | Isolate* isolate, |
| 17835 | uint16_t c1, |
| 17836 | uint16_t c2) { |
| 17837 | Handle<StringTable> string_table = isolate->factory()->string_table(); |
| 17838 | TwoCharHashTableKey key(c1, c2, isolate->heap()->HashSeed()); |
| 17839 | int entry = string_table->FindEntry(&key); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17840 | if (entry == kNotFound) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17841 | return MaybeHandle<String>(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17842 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17843 | Handle<String> result(String::cast(string_table->KeyAt(entry)), isolate); |
| 17844 | DCHECK(StringShape(*result).IsInternalized()); |
| 17845 | return result; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17846 | } |
| 17847 | } |
| 17848 | |
| 17849 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17850 | void StringTable::EnsureCapacityForDeserialization(Isolate* isolate, |
| 17851 | int expected) { |
| 17852 | Handle<StringTable> table = isolate->factory()->string_table(); |
| 17853 | // We need a key instance for the virtual hash function. |
| 17854 | InternalizedStringKey dummy_key(Handle<String>::null()); |
| 17855 | table = StringTable::EnsureCapacity(table, expected, &dummy_key); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17856 | isolate->heap()->SetRootStringTable(*table); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17857 | } |
| 17858 | |
| 17859 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17860 | Handle<String> StringTable::LookupString(Isolate* isolate, |
| 17861 | Handle<String> string) { |
| 17862 | InternalizedStringKey key(string); |
| 17863 | return LookupKey(isolate, &key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17864 | } |
| 17865 | |
| 17866 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17867 | Handle<String> StringTable::LookupKey(Isolate* isolate, HashTableKey* key) { |
| 17868 | Handle<StringTable> table = isolate->factory()->string_table(); |
| 17869 | int entry = table->FindEntry(key); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 17870 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17871 | // String already in table. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17872 | if (entry != kNotFound) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17873 | return handle(String::cast(table->KeyAt(entry)), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17874 | } |
| 17875 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17876 | // Adding new string. Grow table if needed. |
| 17877 | table = StringTable::EnsureCapacity(table, 1, key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17878 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17879 | // Create string object. |
| 17880 | Handle<Object> string = key->AsHandle(isolate); |
| 17881 | // There must be no attempts to internalize strings that could throw |
| 17882 | // InvalidStringLength error. |
| 17883 | CHECK(!string.is_null()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17884 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17885 | // Add the new string and return it along with the string table. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17886 | entry = table->FindInsertionEntry(key->Hash()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17887 | table->set(EntryToIndex(entry), *string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17888 | table->ElementAdded(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17889 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17890 | isolate->heap()->SetRootStringTable(*table); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17891 | return Handle<String>::cast(string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17892 | } |
| 17893 | |
| 17894 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17895 | String* StringTable::LookupKeyIfExists(Isolate* isolate, HashTableKey* key) { |
| 17896 | Handle<StringTable> table = isolate->factory()->string_table(); |
| 17897 | int entry = table->FindEntry(key); |
| 17898 | if (entry != kNotFound) return String::cast(table->KeyAt(entry)); |
| 17899 | return NULL; |
| 17900 | } |
| 17901 | |
| 17902 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17903 | Handle<Object> CompilationCacheTable::Lookup(Handle<String> src, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17904 | Handle<Context> context, |
| 17905 | LanguageMode language_mode) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17906 | Isolate* isolate = GetIsolate(); |
| 17907 | Handle<SharedFunctionInfo> shared(context->closure()->shared()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17908 | StringSharedKey key(src, shared, language_mode, RelocInfo::kNoPosition); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17909 | int entry = FindEntry(&key); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17910 | if (entry == kNotFound) return isolate->factory()->undefined_value(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17911 | int index = EntryToIndex(entry); |
| 17912 | if (!get(index)->IsFixedArray()) return isolate->factory()->undefined_value(); |
| 17913 | return Handle<Object>(get(index + 1), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17914 | } |
| 17915 | |
| 17916 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17917 | Handle<Object> CompilationCacheTable::LookupEval( |
| 17918 | Handle<String> src, Handle<SharedFunctionInfo> outer_info, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17919 | LanguageMode language_mode, int scope_position) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17920 | Isolate* isolate = GetIsolate(); |
| 17921 | // Cache key is the tuple (source, outer shared function info, scope position) |
| 17922 | // to unambiguously identify the context chain the cached eval code assumes. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17923 | StringSharedKey key(src, outer_info, language_mode, scope_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17924 | int entry = FindEntry(&key); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17925 | if (entry == kNotFound) return isolate->factory()->undefined_value(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17926 | int index = EntryToIndex(entry); |
| 17927 | if (!get(index)->IsFixedArray()) return isolate->factory()->undefined_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17928 | return Handle<Object>(get(EntryToIndex(entry) + 1), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17929 | } |
| 17930 | |
| 17931 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17932 | Handle<Object> CompilationCacheTable::LookupRegExp(Handle<String> src, |
| 17933 | JSRegExp::Flags flags) { |
| 17934 | Isolate* isolate = GetIsolate(); |
| 17935 | DisallowHeapAllocation no_allocation; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17936 | RegExpKey key(src, flags); |
| 17937 | int entry = FindEntry(&key); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17938 | if (entry == kNotFound) return isolate->factory()->undefined_value(); |
| 17939 | return Handle<Object>(get(EntryToIndex(entry) + 1), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17940 | } |
| 17941 | |
| 17942 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17943 | Handle<CompilationCacheTable> CompilationCacheTable::Put( |
| 17944 | Handle<CompilationCacheTable> cache, Handle<String> src, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17945 | Handle<Context> context, LanguageMode language_mode, Handle<Object> value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17946 | Isolate* isolate = cache->GetIsolate(); |
| 17947 | Handle<SharedFunctionInfo> shared(context->closure()->shared()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17948 | StringSharedKey key(src, shared, language_mode, RelocInfo::kNoPosition); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17949 | { |
| 17950 | Handle<Object> k = key.AsHandle(isolate); |
| 17951 | DisallowHeapAllocation no_allocation_scope; |
| 17952 | int entry = cache->FindEntry(&key); |
| 17953 | if (entry != kNotFound) { |
| 17954 | cache->set(EntryToIndex(entry), *k); |
| 17955 | cache->set(EntryToIndex(entry) + 1, *value); |
| 17956 | return cache; |
| 17957 | } |
| 17958 | } |
| 17959 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17960 | cache = EnsureCapacity(cache, 1, &key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17961 | int entry = cache->FindInsertionEntry(key.Hash()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17962 | Handle<Object> k = |
| 17963 | isolate->factory()->NewNumber(static_cast<double>(key.Hash())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17964 | cache->set(EntryToIndex(entry), *k); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17965 | cache->set(EntryToIndex(entry) + 1, Smi::FromInt(kHashGenerations)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17966 | cache->ElementAdded(); |
| 17967 | return cache; |
| 17968 | } |
| 17969 | |
| 17970 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17971 | Handle<CompilationCacheTable> CompilationCacheTable::PutEval( |
| 17972 | Handle<CompilationCacheTable> cache, Handle<String> src, |
| 17973 | Handle<SharedFunctionInfo> outer_info, Handle<SharedFunctionInfo> value, |
| 17974 | int scope_position) { |
| 17975 | Isolate* isolate = cache->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 17976 | StringSharedKey key(src, outer_info, value->language_mode(), scope_position); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17977 | { |
| 17978 | Handle<Object> k = key.AsHandle(isolate); |
| 17979 | DisallowHeapAllocation no_allocation_scope; |
| 17980 | int entry = cache->FindEntry(&key); |
| 17981 | if (entry != kNotFound) { |
| 17982 | cache->set(EntryToIndex(entry), *k); |
| 17983 | cache->set(EntryToIndex(entry) + 1, *value); |
| 17984 | return cache; |
| 17985 | } |
| 17986 | } |
| 17987 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17988 | cache = EnsureCapacity(cache, 1, &key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17989 | int entry = cache->FindInsertionEntry(key.Hash()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17990 | Handle<Object> k = |
| 17991 | isolate->factory()->NewNumber(static_cast<double>(key.Hash())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17992 | cache->set(EntryToIndex(entry), *k); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17993 | cache->set(EntryToIndex(entry) + 1, Smi::FromInt(kHashGenerations)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17994 | cache->ElementAdded(); |
| 17995 | return cache; |
| 17996 | } |
| 17997 | |
| 17998 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17999 | Handle<CompilationCacheTable> CompilationCacheTable::PutRegExp( |
| 18000 | Handle<CompilationCacheTable> cache, Handle<String> src, |
| 18001 | JSRegExp::Flags flags, Handle<FixedArray> value) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18002 | RegExpKey key(src, flags); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18003 | cache = EnsureCapacity(cache, 1, &key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18004 | int entry = cache->FindInsertionEntry(key.Hash()); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 18005 | // We store the value in the key slot, and compare the search key |
| 18006 | // to the stored value with a custon IsMatch function during lookups. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18007 | cache->set(EntryToIndex(entry), *value); |
| 18008 | cache->set(EntryToIndex(entry) + 1, *value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18009 | cache->ElementAdded(); |
| 18010 | return cache; |
| 18011 | } |
| 18012 | |
| 18013 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18014 | void CompilationCacheTable::Age() { |
| 18015 | DisallowHeapAllocation no_allocation; |
| 18016 | Object* the_hole_value = GetHeap()->the_hole_value(); |
| 18017 | for (int entry = 0, size = Capacity(); entry < size; entry++) { |
| 18018 | int entry_index = EntryToIndex(entry); |
| 18019 | int value_index = entry_index + 1; |
| 18020 | |
| 18021 | if (get(entry_index)->IsNumber()) { |
| 18022 | Smi* count = Smi::cast(get(value_index)); |
| 18023 | count = Smi::FromInt(count->value() - 1); |
| 18024 | if (count->value() == 0) { |
| 18025 | NoWriteBarrierSet(this, entry_index, the_hole_value); |
| 18026 | NoWriteBarrierSet(this, value_index, the_hole_value); |
| 18027 | ElementRemoved(); |
| 18028 | } else { |
| 18029 | NoWriteBarrierSet(this, value_index, count); |
| 18030 | } |
| 18031 | } else if (get(entry_index)->IsFixedArray()) { |
| 18032 | SharedFunctionInfo* info = SharedFunctionInfo::cast(get(value_index)); |
| 18033 | if (info->code()->kind() != Code::FUNCTION || info->code()->IsOld()) { |
| 18034 | NoWriteBarrierSet(this, entry_index, the_hole_value); |
| 18035 | NoWriteBarrierSet(this, value_index, the_hole_value); |
| 18036 | ElementRemoved(); |
| 18037 | } |
| 18038 | } |
| 18039 | } |
| 18040 | } |
| 18041 | |
| 18042 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 18043 | void CompilationCacheTable::Remove(Object* value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18044 | DisallowHeapAllocation no_allocation; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18045 | Object* the_hole_value = GetHeap()->the_hole_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 18046 | for (int entry = 0, size = Capacity(); entry < size; entry++) { |
| 18047 | int entry_index = EntryToIndex(entry); |
| 18048 | int value_index = entry_index + 1; |
| 18049 | if (get(value_index) == value) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18050 | NoWriteBarrierSet(this, entry_index, the_hole_value); |
| 18051 | NoWriteBarrierSet(this, value_index, the_hole_value); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 18052 | ElementRemoved(); |
| 18053 | } |
| 18054 | } |
| 18055 | return; |
| 18056 | } |
| 18057 | |
| 18058 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18059 | // StringsKey used for HashTable where key is array of internalized strings. |
| 18060 | class StringsKey : public HashTableKey { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18061 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18062 | explicit StringsKey(Handle<FixedArray> strings) : strings_(strings) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18063 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18064 | bool IsMatch(Object* strings) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18065 | FixedArray* o = FixedArray::cast(strings); |
| 18066 | int len = strings_->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18067 | if (o->length() != len) return false; |
| 18068 | for (int i = 0; i < len; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18069 | if (o->get(i) != strings_->get(i)) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18070 | } |
| 18071 | return true; |
| 18072 | } |
| 18073 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18074 | uint32_t Hash() override { return HashForObject(*strings_); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18075 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18076 | uint32_t HashForObject(Object* obj) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18077 | FixedArray* strings = FixedArray::cast(obj); |
| 18078 | int len = strings->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18079 | uint32_t hash = 0; |
| 18080 | for (int i = 0; i < len; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18081 | hash ^= String::cast(strings->get(i))->Hash(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18082 | } |
| 18083 | return hash; |
| 18084 | } |
| 18085 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18086 | Handle<Object> AsHandle(Isolate* isolate) override { return strings_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18087 | |
| 18088 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18089 | Handle<FixedArray> strings_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18090 | }; |
| 18091 | |
| 18092 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18093 | template<typename Derived, typename Shape, typename Key> |
| 18094 | Handle<Derived> Dictionary<Derived, Shape, Key>::New( |
| 18095 | Isolate* isolate, |
| 18096 | int at_least_space_for, |
| 18097 | PretenureFlag pretenure) { |
| 18098 | DCHECK(0 <= at_least_space_for); |
| 18099 | Handle<Derived> dict = DerivedHashTable::New(isolate, |
| 18100 | at_least_space_for, |
| 18101 | USE_DEFAULT_MINIMUM_CAPACITY, |
| 18102 | pretenure); |
| 18103 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 18104 | // Initialize the next enumeration index. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18105 | dict->SetNextEnumerationIndex(PropertyDetails::kInitialIndex); |
| 18106 | return dict; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18107 | } |
| 18108 | |
| 18109 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18110 | template <typename Derived, typename Shape, typename Key> |
| 18111 | Handle<FixedArray> Dictionary<Derived, Shape, Key>::BuildIterationIndicesArray( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18112 | Handle<Derived> dictionary) { |
| 18113 | Factory* factory = dictionary->GetIsolate()->factory(); |
| 18114 | int length = dictionary->NumberOfElements(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18115 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18116 | Handle<FixedArray> iteration_order = factory->NewFixedArray(length); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18117 | Handle<FixedArray> enumeration_order = factory->NewFixedArray(length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18118 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18119 | // Fill both the iteration order array and the enumeration order array |
| 18120 | // with property details. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18121 | int capacity = dictionary->Capacity(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18122 | int pos = 0; |
| 18123 | for (int i = 0; i < capacity; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18124 | if (dictionary->IsKey(dictionary->KeyAt(i))) { |
| 18125 | int index = dictionary->DetailsAt(i).dictionary_index(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18126 | iteration_order->set(pos, Smi::FromInt(i)); |
| 18127 | enumeration_order->set(pos, Smi::FromInt(index)); |
| 18128 | pos++; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18129 | } |
| 18130 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18131 | DCHECK(pos == length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18132 | |
| 18133 | // Sort the arrays wrt. enumeration order. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18134 | iteration_order->SortPairs(*enumeration_order, enumeration_order->length()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18135 | return iteration_order; |
| 18136 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18137 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18138 | |
| 18139 | template <typename Derived, typename Shape, typename Key> |
| 18140 | Handle<FixedArray> |
| 18141 | Dictionary<Derived, Shape, Key>::GenerateNewEnumerationIndices( |
| 18142 | Handle<Derived> dictionary) { |
| 18143 | int length = dictionary->NumberOfElements(); |
| 18144 | |
| 18145 | Handle<FixedArray> iteration_order = BuildIterationIndicesArray(dictionary); |
| 18146 | DCHECK(iteration_order->length() == length); |
| 18147 | |
| 18148 | // Iterate over the dictionary using the enumeration order and update |
| 18149 | // the dictionary with new enumeration indices. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18150 | for (int i = 0; i < length; i++) { |
| 18151 | int index = Smi::cast(iteration_order->get(i))->value(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18152 | DCHECK(dictionary->IsKey(dictionary->KeyAt(index))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18153 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18154 | int enum_index = PropertyDetails::kInitialIndex + i; |
| 18155 | |
| 18156 | PropertyDetails details = dictionary->DetailsAt(index); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18157 | PropertyDetails new_details = details.set_index(enum_index); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18158 | dictionary->DetailsAtPut(index, new_details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18159 | } |
| 18160 | |
| 18161 | // Set the next enumeration index. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18162 | dictionary->SetNextEnumerationIndex(PropertyDetails::kInitialIndex+length); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18163 | return iteration_order; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18164 | } |
| 18165 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18166 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18167 | template <typename Derived, typename Shape, typename Key> |
| 18168 | void Dictionary<Derived, Shape, Key>::SetRequiresCopyOnCapacityChange() { |
| 18169 | DCHECK_EQ(0, DerivedHashTable::NumberOfElements()); |
| 18170 | DCHECK_EQ(0, DerivedHashTable::NumberOfDeletedElements()); |
| 18171 | // Make sure that HashTable::EnsureCapacity will create a copy. |
| 18172 | DerivedHashTable::SetNumberOfDeletedElements(DerivedHashTable::Capacity()); |
| 18173 | DCHECK(!DerivedHashTable::HasSufficientCapacity(1)); |
| 18174 | } |
| 18175 | |
| 18176 | |
| 18177 | template <typename Derived, typename Shape, typename Key> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18178 | Handle<Derived> Dictionary<Derived, Shape, Key>::EnsureCapacity( |
| 18179 | Handle<Derived> dictionary, int n, Key key) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18180 | // Check whether there are enough enumeration indices to add n elements. |
| 18181 | if (Shape::kIsEnumerable && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18182 | !PropertyDetails::IsValidIndex(dictionary->NextEnumerationIndex() + n)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18183 | // If not, we generate new indices for the properties. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18184 | GenerateNewEnumerationIndices(dictionary); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18185 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18186 | return DerivedHashTable::EnsureCapacity(dictionary, n, key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18187 | } |
| 18188 | |
| 18189 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18190 | template <typename Derived, typename Shape, typename Key> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18191 | Handle<Object> Dictionary<Derived, Shape, Key>::DeleteProperty( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18192 | Handle<Derived> dictionary, int entry) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18193 | Factory* factory = dictionary->GetIsolate()->factory(); |
| 18194 | PropertyDetails details = dictionary->DetailsAt(entry); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18195 | if (!details.IsConfigurable()) return factory->false_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18196 | |
| 18197 | dictionary->SetEntry( |
| 18198 | entry, factory->the_hole_value(), factory->the_hole_value()); |
| 18199 | dictionary->ElementRemoved(); |
| 18200 | return factory->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18201 | } |
| 18202 | |
| 18203 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18204 | template<typename Derived, typename Shape, typename Key> |
| 18205 | Handle<Derived> Dictionary<Derived, Shape, Key>::AtPut( |
| 18206 | Handle<Derived> dictionary, Key key, Handle<Object> value) { |
| 18207 | int entry = dictionary->FindEntry(key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18208 | |
| 18209 | // If the entry is present set the value; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18210 | if (entry != Dictionary::kNotFound) { |
| 18211 | dictionary->ValueAtPut(entry, *value); |
| 18212 | return dictionary; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18213 | } |
| 18214 | |
| 18215 | // Check whether the dictionary should be extended. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18216 | dictionary = EnsureCapacity(dictionary, 1, key); |
| 18217 | #ifdef DEBUG |
| 18218 | USE(Shape::AsHandle(dictionary->GetIsolate(), key)); |
| 18219 | #endif |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18220 | PropertyDetails details = PropertyDetails::Empty(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18221 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18222 | AddEntry(dictionary, key, value, details, dictionary->Hash(key)); |
| 18223 | return dictionary; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18224 | } |
| 18225 | |
| 18226 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18227 | template<typename Derived, typename Shape, typename Key> |
| 18228 | Handle<Derived> Dictionary<Derived, Shape, Key>::Add( |
| 18229 | Handle<Derived> dictionary, |
| 18230 | Key key, |
| 18231 | Handle<Object> value, |
| 18232 | PropertyDetails details) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18233 | // Valdate key is absent. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18234 | SLOW_DCHECK((dictionary->FindEntry(key) == Dictionary::kNotFound)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18235 | // Check whether the dictionary should be extended. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18236 | dictionary = EnsureCapacity(dictionary, 1, key); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 18237 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18238 | AddEntry(dictionary, key, value, details, dictionary->Hash(key)); |
| 18239 | return dictionary; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18240 | } |
| 18241 | |
| 18242 | |
| 18243 | // Add a key, value pair to the dictionary. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18244 | template<typename Derived, typename Shape, typename Key> |
| 18245 | void Dictionary<Derived, Shape, Key>::AddEntry( |
| 18246 | Handle<Derived> dictionary, |
| 18247 | Key key, |
| 18248 | Handle<Object> value, |
| 18249 | PropertyDetails details, |
| 18250 | uint32_t hash) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18251 | // Compute the key object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18252 | Handle<Object> k = Shape::AsHandle(dictionary->GetIsolate(), key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18253 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18254 | uint32_t entry = dictionary->FindInsertionEntry(hash); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18255 | // Insert element at empty or deleted entry |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18256 | if (details.dictionary_index() == 0 && Shape::kIsEnumerable) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18257 | // Assign an enumeration index to the property and update |
| 18258 | // SetNextEnumerationIndex. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18259 | int index = dictionary->NextEnumerationIndex(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18260 | details = details.set_index(index); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18261 | dictionary->SetNextEnumerationIndex(index + 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18262 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18263 | dictionary->SetEntry(entry, k, value, details); |
| 18264 | DCHECK((dictionary->KeyAt(entry)->IsNumber() || |
| 18265 | dictionary->KeyAt(entry)->IsName())); |
| 18266 | dictionary->ElementAdded(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18267 | } |
| 18268 | |
| 18269 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18270 | void SeededNumberDictionary::UpdateMaxNumberKey(uint32_t key, |
| 18271 | bool used_as_prototype) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18272 | DisallowHeapAllocation no_allocation; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18273 | // If the dictionary requires slow elements an element has already |
| 18274 | // been added at a high index. |
| 18275 | if (requires_slow_elements()) return; |
| 18276 | // Check if this index is high enough that we should require slow |
| 18277 | // elements. |
| 18278 | if (key > kRequiresSlowElementsLimit) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18279 | if (used_as_prototype) { |
| 18280 | // TODO(verwaest): Remove this hack. |
| 18281 | TypeFeedbackVector::ClearAllKeyedStoreICs(GetIsolate()); |
| 18282 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18283 | set_requires_slow_elements(); |
| 18284 | return; |
| 18285 | } |
| 18286 | // Update max key value. |
| 18287 | Object* max_index_object = get(kMaxNumberKeyIndex); |
| 18288 | if (!max_index_object->IsSmi() || max_number_key() < key) { |
| 18289 | FixedArray::set(kMaxNumberKeyIndex, |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 18290 | Smi::FromInt(key << kRequiresSlowElementsTagSize)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18291 | } |
| 18292 | } |
| 18293 | |
| 18294 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18295 | Handle<SeededNumberDictionary> SeededNumberDictionary::AddNumberEntry( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18296 | Handle<SeededNumberDictionary> dictionary, uint32_t key, |
| 18297 | Handle<Object> value, PropertyDetails details, bool used_as_prototype) { |
| 18298 | dictionary->UpdateMaxNumberKey(key, used_as_prototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18299 | SLOW_DCHECK(dictionary->FindEntry(key) == kNotFound); |
| 18300 | return Add(dictionary, key, value, details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18301 | } |
| 18302 | |
| 18303 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18304 | Handle<UnseededNumberDictionary> UnseededNumberDictionary::AddNumberEntry( |
| 18305 | Handle<UnseededNumberDictionary> dictionary, |
| 18306 | uint32_t key, |
| 18307 | Handle<Object> value) { |
| 18308 | SLOW_DCHECK(dictionary->FindEntry(key) == kNotFound); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18309 | return Add(dictionary, key, value, PropertyDetails::Empty()); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 18310 | } |
| 18311 | |
| 18312 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18313 | Handle<SeededNumberDictionary> SeededNumberDictionary::AtNumberPut( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18314 | Handle<SeededNumberDictionary> dictionary, uint32_t key, |
| 18315 | Handle<Object> value, bool used_as_prototype) { |
| 18316 | dictionary->UpdateMaxNumberKey(key, used_as_prototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18317 | return AtPut(dictionary, key, value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18318 | } |
| 18319 | |
| 18320 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18321 | Handle<UnseededNumberDictionary> UnseededNumberDictionary::AtNumberPut( |
| 18322 | Handle<UnseededNumberDictionary> dictionary, |
| 18323 | uint32_t key, |
| 18324 | Handle<Object> value) { |
| 18325 | return AtPut(dictionary, key, value); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 18326 | } |
| 18327 | |
| 18328 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18329 | Handle<SeededNumberDictionary> SeededNumberDictionary::Set( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18330 | Handle<SeededNumberDictionary> dictionary, uint32_t key, |
| 18331 | Handle<Object> value, PropertyDetails details, bool used_as_prototype) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18332 | int entry = dictionary->FindEntry(key); |
| 18333 | if (entry == kNotFound) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18334 | return AddNumberEntry(dictionary, key, value, details, used_as_prototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18335 | } |
| 18336 | // Preserve enumeration index. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18337 | details = details.set_index(dictionary->DetailsAt(entry).dictionary_index()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18338 | Handle<Object> object_key = |
| 18339 | SeededNumberDictionaryShape::AsHandle(dictionary->GetIsolate(), key); |
| 18340 | dictionary->SetEntry(entry, object_key, value, details); |
| 18341 | return dictionary; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18342 | } |
| 18343 | |
| 18344 | |
| 18345 | Handle<UnseededNumberDictionary> UnseededNumberDictionary::Set( |
| 18346 | Handle<UnseededNumberDictionary> dictionary, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18347 | uint32_t key, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18348 | Handle<Object> value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18349 | int entry = dictionary->FindEntry(key); |
| 18350 | if (entry == kNotFound) return AddNumberEntry(dictionary, key, value); |
| 18351 | Handle<Object> object_key = |
| 18352 | UnseededNumberDictionaryShape::AsHandle(dictionary->GetIsolate(), key); |
| 18353 | dictionary->SetEntry(entry, object_key, value); |
| 18354 | return dictionary; |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 18355 | } |
| 18356 | |
| 18357 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18358 | template <typename Derived, typename Shape, typename Key> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18359 | int Dictionary<Derived, Shape, Key>::NumberOfElementsFilterAttributes( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18360 | PropertyFilter filter) { |
| 18361 | int capacity = this->Capacity(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18362 | int result = 0; |
| 18363 | for (int i = 0; i < capacity; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18364 | Object* k = this->KeyAt(i); |
| 18365 | if (this->IsKey(k) && !k->FilterKey(filter)) { |
| 18366 | if (this->IsDeleted(i)) continue; |
| 18367 | PropertyDetails details = this->DetailsAt(i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18368 | PropertyAttributes attr = details.attributes(); |
| 18369 | if ((attr & filter) == 0) result++; |
| 18370 | } |
| 18371 | } |
| 18372 | return result; |
| 18373 | } |
| 18374 | |
| 18375 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18376 | template <typename Derived, typename Shape, typename Key> |
| 18377 | bool Dictionary<Derived, Shape, Key>::HasComplexElements() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18378 | int capacity = this->Capacity(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18379 | for (int i = 0; i < capacity; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18380 | Object* k = this->KeyAt(i); |
| 18381 | if (this->IsKey(k) && !k->FilterKey(ALL_PROPERTIES)) { |
| 18382 | if (this->IsDeleted(i)) continue; |
| 18383 | PropertyDetails details = this->DetailsAt(i); |
| 18384 | if (details.type() == ACCESSOR_CONSTANT) return true; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18385 | PropertyAttributes attr = details.attributes(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18386 | if (attr & ALL_ATTRIBUTES_MASK) return true; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18387 | } |
| 18388 | } |
| 18389 | return false; |
| 18390 | } |
| 18391 | |
| 18392 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18393 | template <typename Dictionary> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18394 | struct EnumIndexComparator { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18395 | explicit EnumIndexComparator(Dictionary* dict) : dict(dict) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18396 | bool operator() (Smi* a, Smi* b) { |
| 18397 | PropertyDetails da(dict->DetailsAt(a->value())); |
| 18398 | PropertyDetails db(dict->DetailsAt(b->value())); |
| 18399 | return da.dictionary_index() < db.dictionary_index(); |
| 18400 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18401 | Dictionary* dict; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18402 | }; |
| 18403 | |
| 18404 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18405 | template <typename Derived, typename Shape, typename Key> |
| 18406 | void Dictionary<Derived, Shape, Key>::CopyEnumKeysTo(FixedArray* storage) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18407 | int length = storage->length(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18408 | int capacity = this->Capacity(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18409 | int properties = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18410 | for (int i = 0; i < capacity; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18411 | Object* k = this->KeyAt(i); |
| 18412 | if (this->IsKey(k) && !k->IsSymbol()) { |
| 18413 | PropertyDetails details = this->DetailsAt(i); |
| 18414 | if (details.IsDontEnum() || this->IsDeleted(i)) continue; |
| 18415 | storage->set(properties, Smi::FromInt(i)); |
| 18416 | properties++; |
| 18417 | if (properties == length) break; |
| 18418 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18419 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18420 | CHECK_EQ(length, properties); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18421 | EnumIndexComparator<Derived> cmp(static_cast<Derived*>(this)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18422 | Smi** start = reinterpret_cast<Smi**>(storage->GetFirstElementAddress()); |
| 18423 | std::sort(start, start + length, cmp); |
| 18424 | for (int i = 0; i < length; i++) { |
| 18425 | int index = Smi::cast(storage->get(i))->value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18426 | storage->set(i, this->KeyAt(index)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18427 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18428 | } |
| 18429 | |
| 18430 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18431 | template <typename Derived, typename Shape, typename Key> |
| 18432 | int Dictionary<Derived, Shape, Key>::CopyKeysTo( |
| 18433 | FixedArray* storage, int index, PropertyFilter filter, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18434 | typename Dictionary<Derived, Shape, Key>::SortMode sort_mode) { |
| 18435 | DCHECK(storage->length() >= NumberOfElementsFilterAttributes(filter)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18436 | int start_index = index; |
| 18437 | int capacity = this->Capacity(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18438 | for (int i = 0; i < capacity; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18439 | Object* k = this->KeyAt(i); |
| 18440 | if (!this->IsKey(k) || k->FilterKey(filter)) continue; |
| 18441 | if (this->IsDeleted(i)) continue; |
| 18442 | PropertyDetails details = this->DetailsAt(i); |
| 18443 | PropertyAttributes attr = details.attributes(); |
| 18444 | if ((attr & filter) != 0) continue; |
| 18445 | storage->set(index++, k); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18446 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18447 | if (sort_mode == Dictionary::SORTED) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 18448 | storage->SortPairs(storage, index); |
| 18449 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18450 | DCHECK(storage->length() >= index); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18451 | return index - start_index; |
| 18452 | } |
| 18453 | |
| 18454 | |
| 18455 | template <typename Derived, typename Shape, typename Key> |
| 18456 | void Dictionary<Derived, Shape, Key>::CollectKeysTo( |
| 18457 | Handle<Dictionary<Derived, Shape, Key> > dictionary, KeyAccumulator* keys, |
| 18458 | PropertyFilter filter) { |
| 18459 | int capacity = dictionary->Capacity(); |
| 18460 | Handle<FixedArray> array = |
| 18461 | keys->isolate()->factory()->NewFixedArray(dictionary->NumberOfElements()); |
| 18462 | int array_size = 0; |
| 18463 | |
| 18464 | { |
| 18465 | DisallowHeapAllocation no_gc; |
| 18466 | Dictionary<Derived, Shape, Key>* raw_dict = *dictionary; |
| 18467 | for (int i = 0; i < capacity; i++) { |
| 18468 | Object* k = raw_dict->KeyAt(i); |
| 18469 | if (!raw_dict->IsKey(k) || k->FilterKey(filter)) continue; |
| 18470 | if (raw_dict->IsDeleted(i)) continue; |
| 18471 | PropertyDetails details = raw_dict->DetailsAt(i); |
| 18472 | if ((details.attributes() & filter) != 0) continue; |
| 18473 | if (filter & ONLY_ALL_CAN_READ) { |
| 18474 | if (details.kind() != kAccessor) continue; |
| 18475 | Object* accessors = raw_dict->ValueAt(i); |
| 18476 | if (accessors->IsPropertyCell()) { |
| 18477 | accessors = PropertyCell::cast(accessors)->value(); |
| 18478 | } |
| 18479 | if (!accessors->IsAccessorInfo()) continue; |
| 18480 | if (!AccessorInfo::cast(accessors)->all_can_read()) continue; |
| 18481 | } |
| 18482 | array->set(array_size++, Smi::FromInt(i)); |
| 18483 | } |
| 18484 | |
| 18485 | EnumIndexComparator<Derived> cmp(static_cast<Derived*>(raw_dict)); |
| 18486 | Smi** start = reinterpret_cast<Smi**>(array->GetFirstElementAddress()); |
| 18487 | std::sort(start, start + array_size, cmp); |
| 18488 | } |
| 18489 | |
| 18490 | for (int i = 0; i < array_size; i++) { |
| 18491 | int index = Smi::cast(array->get(i))->value(); |
| 18492 | keys->AddKey(dictionary->KeyAt(index)); |
| 18493 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18494 | } |
| 18495 | |
| 18496 | |
| 18497 | // Backwards lookup (slow). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18498 | template<typename Derived, typename Shape, typename Key> |
| 18499 | Object* Dictionary<Derived, Shape, Key>::SlowReverseLookup(Object* value) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18500 | int capacity = this->Capacity(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18501 | for (int i = 0; i < capacity; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18502 | Object* k = this->KeyAt(i); |
| 18503 | if (this->IsKey(k)) { |
| 18504 | Object* e = this->ValueAt(i); |
| 18505 | // TODO(dcarney): this should be templatized. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18506 | if (e->IsPropertyCell()) { |
| 18507 | e = PropertyCell::cast(e)->value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18508 | } |
| 18509 | if (e == value) return k; |
| 18510 | } |
| 18511 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18512 | Heap* heap = Dictionary::GetHeap(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 18513 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18514 | } |
| 18515 | |
| 18516 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18517 | Object* ObjectHashTable::Lookup(Isolate* isolate, Handle<Object> key, |
| 18518 | int32_t hash) { |
| 18519 | DisallowHeapAllocation no_gc; |
| 18520 | DCHECK(IsKey(*key)); |
| 18521 | |
| 18522 | int entry = FindEntry(isolate, key, hash); |
| 18523 | if (entry == kNotFound) return isolate->heap()->the_hole_value(); |
| 18524 | return get(EntryToIndex(entry) + 1); |
| 18525 | } |
| 18526 | |
| 18527 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18528 | Object* ObjectHashTable::Lookup(Handle<Object> key) { |
| 18529 | DisallowHeapAllocation no_gc; |
| 18530 | DCHECK(IsKey(*key)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18531 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18532 | Isolate* isolate = GetIsolate(); |
| 18533 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18534 | // If the object does not have an identity hash, it was never used as a key. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18535 | Object* hash = key->GetHash(); |
| 18536 | if (hash->IsUndefined()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18537 | return isolate->heap()->the_hole_value(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18538 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18539 | return Lookup(isolate, key, Smi::cast(hash)->value()); |
| 18540 | } |
| 18541 | |
| 18542 | |
| 18543 | Object* ObjectHashTable::Lookup(Handle<Object> key, int32_t hash) { |
| 18544 | return Lookup(GetIsolate(), key, hash); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18545 | } |
| 18546 | |
| 18547 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18548 | Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table, |
| 18549 | Handle<Object> key, |
| 18550 | Handle<Object> value) { |
| 18551 | DCHECK(table->IsKey(*key)); |
| 18552 | DCHECK(!value->IsTheHole()); |
| 18553 | |
| 18554 | Isolate* isolate = table->GetIsolate(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18555 | // Make sure the key object has an identity hash code. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18556 | int32_t hash = Object::GetOrCreateHash(isolate, key)->value(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18557 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18558 | return Put(table, key, value, hash); |
| 18559 | } |
| 18560 | |
| 18561 | |
| 18562 | Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table, |
| 18563 | Handle<Object> key, |
| 18564 | Handle<Object> value, |
| 18565 | int32_t hash) { |
| 18566 | DCHECK(table->IsKey(*key)); |
| 18567 | DCHECK(!value->IsTheHole()); |
| 18568 | |
| 18569 | Isolate* isolate = table->GetIsolate(); |
| 18570 | |
| 18571 | int entry = table->FindEntry(isolate, key, hash); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18572 | |
| 18573 | // Key is already in table, just overwrite value. |
| 18574 | if (entry != kNotFound) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18575 | table->set(EntryToIndex(entry) + 1, *value); |
| 18576 | return table; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18577 | } |
| 18578 | |
| 18579 | // Check whether the hash table should be extended. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18580 | table = EnsureCapacity(table, 1, key); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18581 | table->AddEntry(table->FindInsertionEntry(hash), *key, *value); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18582 | return table; |
| 18583 | } |
| 18584 | |
| 18585 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18586 | Handle<ObjectHashTable> ObjectHashTable::Remove(Handle<ObjectHashTable> table, |
| 18587 | Handle<Object> key, |
| 18588 | bool* was_present) { |
| 18589 | DCHECK(table->IsKey(*key)); |
| 18590 | |
| 18591 | Object* hash = key->GetHash(); |
| 18592 | if (hash->IsUndefined()) { |
| 18593 | *was_present = false; |
| 18594 | return table; |
| 18595 | } |
| 18596 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18597 | return Remove(table, key, was_present, Smi::cast(hash)->value()); |
| 18598 | } |
| 18599 | |
| 18600 | |
| 18601 | Handle<ObjectHashTable> ObjectHashTable::Remove(Handle<ObjectHashTable> table, |
| 18602 | Handle<Object> key, |
| 18603 | bool* was_present, |
| 18604 | int32_t hash) { |
| 18605 | DCHECK(table->IsKey(*key)); |
| 18606 | |
| 18607 | int entry = table->FindEntry(table->GetIsolate(), key, hash); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18608 | if (entry == kNotFound) { |
| 18609 | *was_present = false; |
| 18610 | return table; |
| 18611 | } |
| 18612 | |
| 18613 | *was_present = true; |
| 18614 | table->RemoveEntry(entry); |
| 18615 | return Shrink(table, key); |
| 18616 | } |
| 18617 | |
| 18618 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18619 | void ObjectHashTable::AddEntry(int entry, Object* key, Object* value) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18620 | set(EntryToIndex(entry), key); |
| 18621 | set(EntryToIndex(entry) + 1, value); |
| 18622 | ElementAdded(); |
| 18623 | } |
| 18624 | |
| 18625 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18626 | void ObjectHashTable::RemoveEntry(int entry) { |
| 18627 | set_the_hole(EntryToIndex(entry)); |
| 18628 | set_the_hole(EntryToIndex(entry) + 1); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18629 | ElementRemoved(); |
| 18630 | } |
| 18631 | |
| 18632 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18633 | Object* WeakHashTable::Lookup(Handle<HeapObject> key) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18634 | DisallowHeapAllocation no_gc; |
| 18635 | DCHECK(IsKey(*key)); |
| 18636 | int entry = FindEntry(key); |
| 18637 | if (entry == kNotFound) return GetHeap()->the_hole_value(); |
| 18638 | return get(EntryToValueIndex(entry)); |
| 18639 | } |
| 18640 | |
| 18641 | |
| 18642 | Handle<WeakHashTable> WeakHashTable::Put(Handle<WeakHashTable> table, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18643 | Handle<HeapObject> key, |
| 18644 | Handle<HeapObject> value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18645 | DCHECK(table->IsKey(*key)); |
| 18646 | int entry = table->FindEntry(key); |
| 18647 | // Key is already in table, just overwrite value. |
| 18648 | if (entry != kNotFound) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18649 | table->set(EntryToValueIndex(entry), *value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18650 | return table; |
| 18651 | } |
| 18652 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18653 | Handle<WeakCell> key_cell = key->GetIsolate()->factory()->NewWeakCell(key); |
| 18654 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18655 | // Check whether the hash table should be extended. |
| 18656 | table = EnsureCapacity(table, 1, key, TENURED); |
| 18657 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18658 | table->AddEntry(table->FindInsertionEntry(table->Hash(key)), key_cell, value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18659 | return table; |
| 18660 | } |
| 18661 | |
| 18662 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18663 | void WeakHashTable::AddEntry(int entry, Handle<WeakCell> key_cell, |
| 18664 | Handle<HeapObject> value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18665 | DisallowHeapAllocation no_allocation; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18666 | set(EntryToIndex(entry), *key_cell); |
| 18667 | set(EntryToValueIndex(entry), *value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18668 | ElementAdded(); |
| 18669 | } |
| 18670 | |
| 18671 | |
| 18672 | template<class Derived, class Iterator, int entrysize> |
| 18673 | Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Allocate( |
| 18674 | Isolate* isolate, int capacity, PretenureFlag pretenure) { |
| 18675 | // Capacity must be a power of two, since we depend on being able |
| 18676 | // to divide and multiple by 2 (kLoadFactor) to derive capacity |
| 18677 | // from number of buckets. If we decide to change kLoadFactor |
| 18678 | // to something other than 2, capacity should be stored as another |
| 18679 | // field of this object. |
| 18680 | capacity = base::bits::RoundUpToPowerOfTwo32(Max(kMinCapacity, capacity)); |
| 18681 | if (capacity > kMaxCapacity) { |
| 18682 | v8::internal::Heap::FatalProcessOutOfMemory("invalid table size", true); |
| 18683 | } |
| 18684 | int num_buckets = capacity / kLoadFactor; |
| 18685 | Handle<FixedArray> backing_store = isolate->factory()->NewFixedArray( |
| 18686 | kHashTableStartIndex + num_buckets + (capacity * kEntrySize), pretenure); |
| 18687 | backing_store->set_map_no_write_barrier( |
| 18688 | isolate->heap()->ordered_hash_table_map()); |
| 18689 | Handle<Derived> table = Handle<Derived>::cast(backing_store); |
| 18690 | for (int i = 0; i < num_buckets; ++i) { |
| 18691 | table->set(kHashTableStartIndex + i, Smi::FromInt(kNotFound)); |
| 18692 | } |
| 18693 | table->SetNumberOfBuckets(num_buckets); |
| 18694 | table->SetNumberOfElements(0); |
| 18695 | table->SetNumberOfDeletedElements(0); |
| 18696 | return table; |
| 18697 | } |
| 18698 | |
| 18699 | |
| 18700 | template<class Derived, class Iterator, int entrysize> |
| 18701 | Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::EnsureGrowable( |
| 18702 | Handle<Derived> table) { |
| 18703 | DCHECK(!table->IsObsolete()); |
| 18704 | |
| 18705 | int nof = table->NumberOfElements(); |
| 18706 | int nod = table->NumberOfDeletedElements(); |
| 18707 | int capacity = table->Capacity(); |
| 18708 | if ((nof + nod) < capacity) return table; |
| 18709 | // Don't need to grow if we can simply clear out deleted entries instead. |
| 18710 | // Note that we can't compact in place, though, so we always allocate |
| 18711 | // a new table. |
| 18712 | return Rehash(table, (nod < (capacity >> 1)) ? capacity << 1 : capacity); |
| 18713 | } |
| 18714 | |
| 18715 | |
| 18716 | template<class Derived, class Iterator, int entrysize> |
| 18717 | Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Shrink( |
| 18718 | Handle<Derived> table) { |
| 18719 | DCHECK(!table->IsObsolete()); |
| 18720 | |
| 18721 | int nof = table->NumberOfElements(); |
| 18722 | int capacity = table->Capacity(); |
| 18723 | if (nof >= (capacity >> 2)) return table; |
| 18724 | return Rehash(table, capacity / 2); |
| 18725 | } |
| 18726 | |
| 18727 | |
| 18728 | template<class Derived, class Iterator, int entrysize> |
| 18729 | Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Clear( |
| 18730 | Handle<Derived> table) { |
| 18731 | DCHECK(!table->IsObsolete()); |
| 18732 | |
| 18733 | Handle<Derived> new_table = |
| 18734 | Allocate(table->GetIsolate(), |
| 18735 | kMinCapacity, |
| 18736 | table->GetHeap()->InNewSpace(*table) ? NOT_TENURED : TENURED); |
| 18737 | |
| 18738 | table->SetNextTable(*new_table); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18739 | table->SetNumberOfDeletedElements(kClearedTableSentinel); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18740 | |
| 18741 | return new_table; |
| 18742 | } |
| 18743 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18744 | template <class Derived, class Iterator, int entrysize> |
| 18745 | bool OrderedHashTable<Derived, Iterator, entrysize>::HasKey( |
| 18746 | Handle<Derived> table, Handle<Object> key) { |
| 18747 | int entry = table->KeyToFirstEntry(*key); |
| 18748 | // Walk the chain in the bucket to find the key. |
| 18749 | while (entry != kNotFound) { |
| 18750 | Object* candidate_key = table->KeyAt(entry); |
| 18751 | if (candidate_key->SameValueZero(*key)) return true; |
| 18752 | entry = table->NextChainEntry(entry); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18753 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18754 | return false; |
| 18755 | } |
| 18756 | |
| 18757 | |
| 18758 | Handle<OrderedHashSet> OrderedHashSet::Add(Handle<OrderedHashSet> table, |
| 18759 | Handle<Object> key) { |
| 18760 | int hash = Object::GetOrCreateHash(table->GetIsolate(), key)->value(); |
| 18761 | int entry = table->HashToEntry(hash); |
| 18762 | // Walk the chain of the bucket and try finding the key. |
| 18763 | while (entry != kNotFound) { |
| 18764 | Object* candidate_key = table->KeyAt(entry); |
| 18765 | // Do not add if we have the key already |
| 18766 | if (candidate_key->SameValueZero(*key)) return table; |
| 18767 | entry = table->NextChainEntry(entry); |
| 18768 | } |
| 18769 | |
| 18770 | table = OrderedHashSet::EnsureGrowable(table); |
| 18771 | // Read the existing bucket values. |
| 18772 | int bucket = table->HashToBucket(hash); |
| 18773 | int previous_entry = table->HashToEntry(hash); |
| 18774 | int nof = table->NumberOfElements(); |
| 18775 | // Insert a new entry at the end, |
| 18776 | int new_entry = nof + table->NumberOfDeletedElements(); |
| 18777 | int new_index = table->EntryToIndex(new_entry); |
| 18778 | table->set(new_index, *key); |
| 18779 | table->set(new_index + kChainOffset, Smi::FromInt(previous_entry)); |
| 18780 | // and point the bucket to the new entry. |
| 18781 | table->set(kHashTableStartIndex + bucket, Smi::FromInt(new_entry)); |
| 18782 | table->SetNumberOfElements(nof + 1); |
| 18783 | return table; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18784 | } |
| 18785 | |
| 18786 | |
| 18787 | template<class Derived, class Iterator, int entrysize> |
| 18788 | Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Rehash( |
| 18789 | Handle<Derived> table, int new_capacity) { |
| 18790 | DCHECK(!table->IsObsolete()); |
| 18791 | |
| 18792 | Handle<Derived> new_table = |
| 18793 | Allocate(table->GetIsolate(), |
| 18794 | new_capacity, |
| 18795 | table->GetHeap()->InNewSpace(*table) ? NOT_TENURED : TENURED); |
| 18796 | int nof = table->NumberOfElements(); |
| 18797 | int nod = table->NumberOfDeletedElements(); |
| 18798 | int new_buckets = new_table->NumberOfBuckets(); |
| 18799 | int new_entry = 0; |
| 18800 | int removed_holes_index = 0; |
| 18801 | |
| 18802 | for (int old_entry = 0; old_entry < (nof + nod); ++old_entry) { |
| 18803 | Object* key = table->KeyAt(old_entry); |
| 18804 | if (key->IsTheHole()) { |
| 18805 | table->SetRemovedIndexAt(removed_holes_index++, old_entry); |
| 18806 | continue; |
| 18807 | } |
| 18808 | |
| 18809 | Object* hash = key->GetHash(); |
| 18810 | int bucket = Smi::cast(hash)->value() & (new_buckets - 1); |
| 18811 | Object* chain_entry = new_table->get(kHashTableStartIndex + bucket); |
| 18812 | new_table->set(kHashTableStartIndex + bucket, Smi::FromInt(new_entry)); |
| 18813 | int new_index = new_table->EntryToIndex(new_entry); |
| 18814 | int old_index = table->EntryToIndex(old_entry); |
| 18815 | for (int i = 0; i < entrysize; ++i) { |
| 18816 | Object* value = table->get(old_index + i); |
| 18817 | new_table->set(new_index + i, value); |
| 18818 | } |
| 18819 | new_table->set(new_index + kChainOffset, chain_entry); |
| 18820 | ++new_entry; |
| 18821 | } |
| 18822 | |
| 18823 | DCHECK_EQ(nod, removed_holes_index); |
| 18824 | |
| 18825 | new_table->SetNumberOfElements(nof); |
| 18826 | table->SetNextTable(*new_table); |
| 18827 | |
| 18828 | return new_table; |
| 18829 | } |
| 18830 | |
| 18831 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18832 | template Handle<OrderedHashSet> |
| 18833 | OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Allocate( |
| 18834 | Isolate* isolate, int capacity, PretenureFlag pretenure); |
| 18835 | |
| 18836 | template Handle<OrderedHashSet> |
| 18837 | OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::EnsureGrowable( |
| 18838 | Handle<OrderedHashSet> table); |
| 18839 | |
| 18840 | template Handle<OrderedHashSet> |
| 18841 | OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Shrink( |
| 18842 | Handle<OrderedHashSet> table); |
| 18843 | |
| 18844 | template Handle<OrderedHashSet> |
| 18845 | OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Clear( |
| 18846 | Handle<OrderedHashSet> table); |
| 18847 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18848 | template bool OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::HasKey( |
| 18849 | Handle<OrderedHashSet> table, Handle<Object> key); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18850 | |
| 18851 | |
| 18852 | template Handle<OrderedHashMap> |
| 18853 | OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Allocate( |
| 18854 | Isolate* isolate, int capacity, PretenureFlag pretenure); |
| 18855 | |
| 18856 | template Handle<OrderedHashMap> |
| 18857 | OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::EnsureGrowable( |
| 18858 | Handle<OrderedHashMap> table); |
| 18859 | |
| 18860 | template Handle<OrderedHashMap> |
| 18861 | OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Shrink( |
| 18862 | Handle<OrderedHashMap> table); |
| 18863 | |
| 18864 | template Handle<OrderedHashMap> |
| 18865 | OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Clear( |
| 18866 | Handle<OrderedHashMap> table); |
| 18867 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18868 | template bool OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::HasKey( |
| 18869 | Handle<OrderedHashMap> table, Handle<Object> key); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18870 | |
| 18871 | |
| 18872 | template<class Derived, class TableType> |
| 18873 | void OrderedHashTableIterator<Derived, TableType>::Transition() { |
| 18874 | DisallowHeapAllocation no_allocation; |
| 18875 | TableType* table = TableType::cast(this->table()); |
| 18876 | if (!table->IsObsolete()) return; |
| 18877 | |
| 18878 | int index = Smi::cast(this->index())->value(); |
| 18879 | while (table->IsObsolete()) { |
| 18880 | TableType* next_table = table->NextTable(); |
| 18881 | |
| 18882 | if (index > 0) { |
| 18883 | int nod = table->NumberOfDeletedElements(); |
| 18884 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18885 | if (nod == TableType::kClearedTableSentinel) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18886 | index = 0; |
| 18887 | } else { |
| 18888 | int old_index = index; |
| 18889 | for (int i = 0; i < nod; ++i) { |
| 18890 | int removed_index = table->RemovedIndexAt(i); |
| 18891 | if (removed_index >= old_index) break; |
| 18892 | --index; |
| 18893 | } |
| 18894 | } |
| 18895 | } |
| 18896 | |
| 18897 | table = next_table; |
| 18898 | } |
| 18899 | |
| 18900 | set_table(table); |
| 18901 | set_index(Smi::FromInt(index)); |
| 18902 | } |
| 18903 | |
| 18904 | |
| 18905 | template<class Derived, class TableType> |
| 18906 | bool OrderedHashTableIterator<Derived, TableType>::HasMore() { |
| 18907 | DisallowHeapAllocation no_allocation; |
| 18908 | if (this->table()->IsUndefined()) return false; |
| 18909 | |
| 18910 | Transition(); |
| 18911 | |
| 18912 | TableType* table = TableType::cast(this->table()); |
| 18913 | int index = Smi::cast(this->index())->value(); |
| 18914 | int used_capacity = table->UsedCapacity(); |
| 18915 | |
| 18916 | while (index < used_capacity && table->KeyAt(index)->IsTheHole()) { |
| 18917 | index++; |
| 18918 | } |
| 18919 | |
| 18920 | set_index(Smi::FromInt(index)); |
| 18921 | |
| 18922 | if (index < used_capacity) return true; |
| 18923 | |
| 18924 | set_table(GetHeap()->undefined_value()); |
| 18925 | return false; |
| 18926 | } |
| 18927 | |
| 18928 | |
| 18929 | template<class Derived, class TableType> |
| 18930 | Smi* OrderedHashTableIterator<Derived, TableType>::Next(JSArray* value_array) { |
| 18931 | DisallowHeapAllocation no_allocation; |
| 18932 | if (HasMore()) { |
| 18933 | FixedArray* array = FixedArray::cast(value_array->elements()); |
| 18934 | static_cast<Derived*>(this)->PopulateValueArray(array); |
| 18935 | MoveNext(); |
| 18936 | return Smi::cast(kind()); |
| 18937 | } |
| 18938 | return Smi::FromInt(0); |
| 18939 | } |
| 18940 | |
| 18941 | |
| 18942 | template Smi* |
| 18943 | OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::Next( |
| 18944 | JSArray* value_array); |
| 18945 | |
| 18946 | template bool |
| 18947 | OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::HasMore(); |
| 18948 | |
| 18949 | template void |
| 18950 | OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::MoveNext(); |
| 18951 | |
| 18952 | template Object* |
| 18953 | OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::CurrentKey(); |
| 18954 | |
| 18955 | template void |
| 18956 | OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::Transition(); |
| 18957 | |
| 18958 | |
| 18959 | template Smi* |
| 18960 | OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::Next( |
| 18961 | JSArray* value_array); |
| 18962 | |
| 18963 | template bool |
| 18964 | OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::HasMore(); |
| 18965 | |
| 18966 | template void |
| 18967 | OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::MoveNext(); |
| 18968 | |
| 18969 | template Object* |
| 18970 | OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::CurrentKey(); |
| 18971 | |
| 18972 | template void |
| 18973 | OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::Transition(); |
| 18974 | |
| 18975 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18976 | void JSSet::Initialize(Handle<JSSet> set, Isolate* isolate) { |
| 18977 | Handle<OrderedHashSet> table = isolate->factory()->NewOrderedHashSet(); |
| 18978 | set->set_table(*table); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18979 | } |
| 18980 | |
| 18981 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18982 | void JSSet::Clear(Handle<JSSet> set) { |
| 18983 | Handle<OrderedHashSet> table(OrderedHashSet::cast(set->table())); |
| 18984 | table = OrderedHashSet::Clear(table); |
| 18985 | set->set_table(*table); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18986 | } |
| 18987 | |
| 18988 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18989 | void JSMap::Initialize(Handle<JSMap> map, Isolate* isolate) { |
| 18990 | Handle<OrderedHashMap> table = isolate->factory()->NewOrderedHashMap(); |
| 18991 | map->set_table(*table); |
| 18992 | } |
| 18993 | |
| 18994 | |
| 18995 | void JSMap::Clear(Handle<JSMap> map) { |
| 18996 | Handle<OrderedHashMap> table(OrderedHashMap::cast(map->table())); |
| 18997 | table = OrderedHashMap::Clear(table); |
| 18998 | map->set_table(*table); |
| 18999 | } |
| 19000 | |
| 19001 | |
| 19002 | void JSWeakCollection::Initialize(Handle<JSWeakCollection> weak_collection, |
| 19003 | Isolate* isolate) { |
| 19004 | Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 0); |
| 19005 | weak_collection->set_table(*table); |
| 19006 | } |
| 19007 | |
| 19008 | |
| 19009 | void JSWeakCollection::Set(Handle<JSWeakCollection> weak_collection, |
| 19010 | Handle<Object> key, Handle<Object> value, |
| 19011 | int32_t hash) { |
| 19012 | DCHECK(key->IsJSReceiver() || key->IsSymbol()); |
| 19013 | Handle<ObjectHashTable> table( |
| 19014 | ObjectHashTable::cast(weak_collection->table())); |
| 19015 | DCHECK(table->IsKey(*key)); |
| 19016 | Handle<ObjectHashTable> new_table = |
| 19017 | ObjectHashTable::Put(table, key, value, hash); |
| 19018 | weak_collection->set_table(*new_table); |
| 19019 | if (*table != *new_table) { |
| 19020 | // Zap the old table since we didn't record slots for its elements. |
| 19021 | table->FillWithHoles(0, table->length()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19022 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19023 | } |
| 19024 | |
| 19025 | |
| 19026 | bool JSWeakCollection::Delete(Handle<JSWeakCollection> weak_collection, |
| 19027 | Handle<Object> key, int32_t hash) { |
| 19028 | DCHECK(key->IsJSReceiver() || key->IsSymbol()); |
| 19029 | Handle<ObjectHashTable> table( |
| 19030 | ObjectHashTable::cast(weak_collection->table())); |
| 19031 | DCHECK(table->IsKey(*key)); |
| 19032 | bool was_present = false; |
| 19033 | Handle<ObjectHashTable> new_table = |
| 19034 | ObjectHashTable::Remove(table, key, &was_present, hash); |
| 19035 | weak_collection->set_table(*new_table); |
| 19036 | if (*table != *new_table) { |
| 19037 | // Zap the old table since we didn't record slots for its elements. |
| 19038 | table->FillWithHoles(0, table->length()); |
| 19039 | } |
| 19040 | return was_present; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19041 | } |
| 19042 | |
| 19043 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19044 | // Check if there is a break point at this code position. |
| 19045 | bool DebugInfo::HasBreakPoint(int code_position) { |
| 19046 | // Get the break point info object for this code position. |
| 19047 | Object* break_point_info = GetBreakPointInfo(code_position); |
| 19048 | |
| 19049 | // If there is no break point info object or no break points in the break |
| 19050 | // point info object there is no break point at this code position. |
| 19051 | if (break_point_info->IsUndefined()) return false; |
| 19052 | return BreakPointInfo::cast(break_point_info)->GetBreakPointCount() > 0; |
| 19053 | } |
| 19054 | |
| 19055 | |
| 19056 | // Get the break point info object for this code position. |
| 19057 | Object* DebugInfo::GetBreakPointInfo(int code_position) { |
| 19058 | // Find the index of the break point info object for this code position. |
| 19059 | int index = GetBreakPointInfoIndex(code_position); |
| 19060 | |
| 19061 | // Return the break point info object if any. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 19062 | if (index == kNoBreakPointInfo) return GetHeap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19063 | return BreakPointInfo::cast(break_points()->get(index)); |
| 19064 | } |
| 19065 | |
| 19066 | |
| 19067 | // Clear a break point at the specified code position. |
| 19068 | void DebugInfo::ClearBreakPoint(Handle<DebugInfo> debug_info, |
| 19069 | int code_position, |
| 19070 | Handle<Object> break_point_object) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19071 | Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position), |
| 19072 | debug_info->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19073 | if (break_point_info->IsUndefined()) return; |
| 19074 | BreakPointInfo::ClearBreakPoint( |
| 19075 | Handle<BreakPointInfo>::cast(break_point_info), |
| 19076 | break_point_object); |
| 19077 | } |
| 19078 | |
| 19079 | |
| 19080 | void DebugInfo::SetBreakPoint(Handle<DebugInfo> debug_info, |
| 19081 | int code_position, |
| 19082 | int source_position, |
| 19083 | int statement_position, |
| 19084 | Handle<Object> break_point_object) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19085 | Isolate* isolate = debug_info->GetIsolate(); |
| 19086 | Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position), |
| 19087 | isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19088 | if (!break_point_info->IsUndefined()) { |
| 19089 | BreakPointInfo::SetBreakPoint( |
| 19090 | Handle<BreakPointInfo>::cast(break_point_info), |
| 19091 | break_point_object); |
| 19092 | return; |
| 19093 | } |
| 19094 | |
| 19095 | // Adding a new break point for a code position which did not have any |
| 19096 | // break points before. Try to find a free slot. |
| 19097 | int index = kNoBreakPointInfo; |
| 19098 | for (int i = 0; i < debug_info->break_points()->length(); i++) { |
| 19099 | if (debug_info->break_points()->get(i)->IsUndefined()) { |
| 19100 | index = i; |
| 19101 | break; |
| 19102 | } |
| 19103 | } |
| 19104 | if (index == kNoBreakPointInfo) { |
| 19105 | // No free slot - extend break point info array. |
| 19106 | Handle<FixedArray> old_break_points = |
| 19107 | Handle<FixedArray>(FixedArray::cast(debug_info->break_points())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19108 | Handle<FixedArray> new_break_points = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 19109 | isolate->factory()->NewFixedArray( |
| 19110 | old_break_points->length() + |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19111 | DebugInfo::kEstimatedNofBreakPointsInFunction); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 19112 | |
| 19113 | debug_info->set_break_points(*new_break_points); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19114 | for (int i = 0; i < old_break_points->length(); i++) { |
| 19115 | new_break_points->set(i, old_break_points->get(i)); |
| 19116 | } |
| 19117 | index = old_break_points->length(); |
| 19118 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19119 | DCHECK(index != kNoBreakPointInfo); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19120 | |
| 19121 | // Allocate new BreakPointInfo object and set the break point. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 19122 | Handle<BreakPointInfo> new_break_point_info = Handle<BreakPointInfo>::cast( |
| 19123 | isolate->factory()->NewStruct(BREAK_POINT_INFO_TYPE)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19124 | new_break_point_info->set_code_position(code_position); |
| 19125 | new_break_point_info->set_source_position(source_position); |
| 19126 | new_break_point_info->set_statement_position(statement_position); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 19127 | new_break_point_info->set_break_point_objects( |
| 19128 | isolate->heap()->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19129 | BreakPointInfo::SetBreakPoint(new_break_point_info, break_point_object); |
| 19130 | debug_info->break_points()->set(index, *new_break_point_info); |
| 19131 | } |
| 19132 | |
| 19133 | |
| 19134 | // Get the break point objects for a code position. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19135 | Handle<Object> DebugInfo::GetBreakPointObjects(int code_position) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19136 | Object* break_point_info = GetBreakPointInfo(code_position); |
| 19137 | if (break_point_info->IsUndefined()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19138 | return GetIsolate()->factory()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19139 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19140 | return Handle<Object>( |
| 19141 | BreakPointInfo::cast(break_point_info)->break_point_objects(), |
| 19142 | GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19143 | } |
| 19144 | |
| 19145 | |
| 19146 | // Get the total number of break points. |
| 19147 | int DebugInfo::GetBreakPointCount() { |
| 19148 | if (break_points()->IsUndefined()) return 0; |
| 19149 | int count = 0; |
| 19150 | for (int i = 0; i < break_points()->length(); i++) { |
| 19151 | if (!break_points()->get(i)->IsUndefined()) { |
| 19152 | BreakPointInfo* break_point_info = |
| 19153 | BreakPointInfo::cast(break_points()->get(i)); |
| 19154 | count += break_point_info->GetBreakPointCount(); |
| 19155 | } |
| 19156 | } |
| 19157 | return count; |
| 19158 | } |
| 19159 | |
| 19160 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19161 | Handle<Object> DebugInfo::FindBreakPointInfo( |
| 19162 | Handle<DebugInfo> debug_info, Handle<Object> break_point_object) { |
| 19163 | Isolate* isolate = debug_info->GetIsolate(); |
| 19164 | if (!debug_info->break_points()->IsUndefined()) { |
| 19165 | for (int i = 0; i < debug_info->break_points()->length(); i++) { |
| 19166 | if (!debug_info->break_points()->get(i)->IsUndefined()) { |
| 19167 | Handle<BreakPointInfo> break_point_info = Handle<BreakPointInfo>( |
| 19168 | BreakPointInfo::cast(debug_info->break_points()->get(i)), isolate); |
| 19169 | if (BreakPointInfo::HasBreakPointObject(break_point_info, |
| 19170 | break_point_object)) { |
| 19171 | return break_point_info; |
| 19172 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19173 | } |
| 19174 | } |
| 19175 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19176 | return isolate->factory()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19177 | } |
| 19178 | |
| 19179 | |
| 19180 | // Find the index of the break point info object for the specified code |
| 19181 | // position. |
| 19182 | int DebugInfo::GetBreakPointInfoIndex(int code_position) { |
| 19183 | if (break_points()->IsUndefined()) return kNoBreakPointInfo; |
| 19184 | for (int i = 0; i < break_points()->length(); i++) { |
| 19185 | if (!break_points()->get(i)->IsUndefined()) { |
| 19186 | BreakPointInfo* break_point_info = |
| 19187 | BreakPointInfo::cast(break_points()->get(i)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19188 | if (break_point_info->code_position() == code_position) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19189 | return i; |
| 19190 | } |
| 19191 | } |
| 19192 | } |
| 19193 | return kNoBreakPointInfo; |
| 19194 | } |
| 19195 | |
| 19196 | |
| 19197 | // Remove the specified break point object. |
| 19198 | void BreakPointInfo::ClearBreakPoint(Handle<BreakPointInfo> break_point_info, |
| 19199 | Handle<Object> break_point_object) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19200 | Isolate* isolate = break_point_info->GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19201 | // If there are no break points just ignore. |
| 19202 | if (break_point_info->break_point_objects()->IsUndefined()) return; |
| 19203 | // If there is a single break point clear it if it is the same. |
| 19204 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
| 19205 | if (break_point_info->break_point_objects() == *break_point_object) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 19206 | break_point_info->set_break_point_objects( |
| 19207 | isolate->heap()->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19208 | } |
| 19209 | return; |
| 19210 | } |
| 19211 | // If there are multiple break points shrink the array |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19212 | DCHECK(break_point_info->break_point_objects()->IsFixedArray()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19213 | Handle<FixedArray> old_array = |
| 19214 | Handle<FixedArray>( |
| 19215 | FixedArray::cast(break_point_info->break_point_objects())); |
| 19216 | Handle<FixedArray> new_array = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 19217 | isolate->factory()->NewFixedArray(old_array->length() - 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19218 | int found_count = 0; |
| 19219 | for (int i = 0; i < old_array->length(); i++) { |
| 19220 | if (old_array->get(i) == *break_point_object) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19221 | DCHECK(found_count == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19222 | found_count++; |
| 19223 | } else { |
| 19224 | new_array->set(i - found_count, old_array->get(i)); |
| 19225 | } |
| 19226 | } |
| 19227 | // If the break point was found in the list change it. |
| 19228 | if (found_count > 0) break_point_info->set_break_point_objects(*new_array); |
| 19229 | } |
| 19230 | |
| 19231 | |
| 19232 | // Add the specified break point object. |
| 19233 | void BreakPointInfo::SetBreakPoint(Handle<BreakPointInfo> break_point_info, |
| 19234 | Handle<Object> break_point_object) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19235 | Isolate* isolate = break_point_info->GetIsolate(); |
| 19236 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19237 | // If there was no break point objects before just set it. |
| 19238 | if (break_point_info->break_point_objects()->IsUndefined()) { |
| 19239 | break_point_info->set_break_point_objects(*break_point_object); |
| 19240 | return; |
| 19241 | } |
| 19242 | // If the break point object is the same as before just ignore. |
| 19243 | if (break_point_info->break_point_objects() == *break_point_object) return; |
| 19244 | // If there was one break point object before replace with array. |
| 19245 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19246 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19247 | array->set(0, break_point_info->break_point_objects()); |
| 19248 | array->set(1, *break_point_object); |
| 19249 | break_point_info->set_break_point_objects(*array); |
| 19250 | return; |
| 19251 | } |
| 19252 | // If there was more than one break point before extend array. |
| 19253 | Handle<FixedArray> old_array = |
| 19254 | Handle<FixedArray>( |
| 19255 | FixedArray::cast(break_point_info->break_point_objects())); |
| 19256 | Handle<FixedArray> new_array = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19257 | isolate->factory()->NewFixedArray(old_array->length() + 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19258 | for (int i = 0; i < old_array->length(); i++) { |
| 19259 | // If the break point was there before just ignore. |
| 19260 | if (old_array->get(i) == *break_point_object) return; |
| 19261 | new_array->set(i, old_array->get(i)); |
| 19262 | } |
| 19263 | // Add the new break point. |
| 19264 | new_array->set(old_array->length(), *break_point_object); |
| 19265 | break_point_info->set_break_point_objects(*new_array); |
| 19266 | } |
| 19267 | |
| 19268 | |
| 19269 | bool BreakPointInfo::HasBreakPointObject( |
| 19270 | Handle<BreakPointInfo> break_point_info, |
| 19271 | Handle<Object> break_point_object) { |
| 19272 | // No break point. |
| 19273 | if (break_point_info->break_point_objects()->IsUndefined()) return false; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 19274 | // Single break point. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19275 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
| 19276 | return break_point_info->break_point_objects() == *break_point_object; |
| 19277 | } |
| 19278 | // Multiple break points. |
| 19279 | FixedArray* array = FixedArray::cast(break_point_info->break_point_objects()); |
| 19280 | for (int i = 0; i < array->length(); i++) { |
| 19281 | if (array->get(i) == *break_point_object) { |
| 19282 | return true; |
| 19283 | } |
| 19284 | } |
| 19285 | return false; |
| 19286 | } |
| 19287 | |
| 19288 | |
| 19289 | // Get the number of break points. |
| 19290 | int BreakPointInfo::GetBreakPointCount() { |
| 19291 | // No break point. |
| 19292 | if (break_point_objects()->IsUndefined()) return 0; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 19293 | // Single break point. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19294 | if (!break_point_objects()->IsFixedArray()) return 1; |
| 19295 | // Multiple break points. |
| 19296 | return FixedArray::cast(break_point_objects())->length(); |
| 19297 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19298 | |
| 19299 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19300 | // static |
| 19301 | MaybeHandle<JSDate> JSDate::New(Handle<JSFunction> constructor, |
| 19302 | Handle<JSReceiver> new_target, double tv) { |
| 19303 | Isolate* const isolate = constructor->GetIsolate(); |
| 19304 | Handle<JSObject> result; |
| 19305 | ASSIGN_RETURN_ON_EXCEPTION(isolate, result, |
| 19306 | JSObject::New(constructor, new_target), JSDate); |
| 19307 | if (-DateCache::kMaxTimeInMs <= tv && tv <= DateCache::kMaxTimeInMs) { |
| 19308 | tv = DoubleToInteger(tv) + 0.0; |
| 19309 | } else { |
| 19310 | tv = std::numeric_limits<double>::quiet_NaN(); |
| 19311 | } |
| 19312 | Handle<Object> value = isolate->factory()->NewNumber(tv); |
| 19313 | Handle<JSDate>::cast(result)->SetValue(*value, std::isnan(tv)); |
| 19314 | return Handle<JSDate>::cast(result); |
| 19315 | } |
| 19316 | |
| 19317 | |
| 19318 | // static |
| 19319 | double JSDate::CurrentTimeValue(Isolate* isolate) { |
| 19320 | if (FLAG_log_timer_events || FLAG_prof_cpp) LOG(isolate, CurrentTimeEvent()); |
| 19321 | |
| 19322 | // According to ECMA-262, section 15.9.1, page 117, the precision of |
| 19323 | // the number in a Date object representing a particular instant in |
| 19324 | // time is milliseconds. Therefore, we floor the result of getting |
| 19325 | // the OS time. |
| 19326 | return Floor(FLAG_verify_predictable |
| 19327 | ? isolate->heap()->MonotonicallyIncreasingTimeInMs() |
| 19328 | : base::OS::TimeCurrentMillis()); |
| 19329 | } |
| 19330 | |
| 19331 | |
| 19332 | // static |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19333 | Object* JSDate::GetField(Object* object, Smi* index) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19334 | return JSDate::cast(object)->DoGetField( |
| 19335 | static_cast<FieldIndex>(index->value())); |
| 19336 | } |
| 19337 | |
| 19338 | |
| 19339 | Object* JSDate::DoGetField(FieldIndex index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19340 | DCHECK(index != kDateValue); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19341 | |
| 19342 | DateCache* date_cache = GetIsolate()->date_cache(); |
| 19343 | |
| 19344 | if (index < kFirstUncachedField) { |
| 19345 | Object* stamp = cache_stamp(); |
| 19346 | if (stamp != date_cache->stamp() && stamp->IsSmi()) { |
| 19347 | // Since the stamp is not NaN, the value is also not NaN. |
| 19348 | int64_t local_time_ms = |
| 19349 | date_cache->ToLocal(static_cast<int64_t>(value()->Number())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19350 | SetCachedFields(local_time_ms, date_cache); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19351 | } |
| 19352 | switch (index) { |
| 19353 | case kYear: return year(); |
| 19354 | case kMonth: return month(); |
| 19355 | case kDay: return day(); |
| 19356 | case kWeekday: return weekday(); |
| 19357 | case kHour: return hour(); |
| 19358 | case kMinute: return min(); |
| 19359 | case kSecond: return sec(); |
| 19360 | default: UNREACHABLE(); |
| 19361 | } |
| 19362 | } |
| 19363 | |
| 19364 | if (index >= kFirstUTCField) { |
| 19365 | return GetUTCField(index, value()->Number(), date_cache); |
| 19366 | } |
| 19367 | |
| 19368 | double time = value()->Number(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19369 | if (std::isnan(time)) return GetIsolate()->heap()->nan_value(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19370 | |
| 19371 | int64_t local_time_ms = date_cache->ToLocal(static_cast<int64_t>(time)); |
| 19372 | int days = DateCache::DaysFromTime(local_time_ms); |
| 19373 | |
| 19374 | if (index == kDays) return Smi::FromInt(days); |
| 19375 | |
| 19376 | int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days); |
| 19377 | if (index == kMillisecond) return Smi::FromInt(time_in_day_ms % 1000); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19378 | DCHECK(index == kTimeInDay); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19379 | return Smi::FromInt(time_in_day_ms); |
| 19380 | } |
| 19381 | |
| 19382 | |
| 19383 | Object* JSDate::GetUTCField(FieldIndex index, |
| 19384 | double value, |
| 19385 | DateCache* date_cache) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19386 | DCHECK(index >= kFirstUTCField); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19387 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19388 | if (std::isnan(value)) return GetIsolate()->heap()->nan_value(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19389 | |
| 19390 | int64_t time_ms = static_cast<int64_t>(value); |
| 19391 | |
| 19392 | if (index == kTimezoneOffset) { |
| 19393 | return Smi::FromInt(date_cache->TimezoneOffset(time_ms)); |
| 19394 | } |
| 19395 | |
| 19396 | int days = DateCache::DaysFromTime(time_ms); |
| 19397 | |
| 19398 | if (index == kWeekdayUTC) return Smi::FromInt(date_cache->Weekday(days)); |
| 19399 | |
| 19400 | if (index <= kDayUTC) { |
| 19401 | int year, month, day; |
| 19402 | date_cache->YearMonthDayFromDays(days, &year, &month, &day); |
| 19403 | if (index == kYearUTC) return Smi::FromInt(year); |
| 19404 | if (index == kMonthUTC) return Smi::FromInt(month); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19405 | DCHECK(index == kDayUTC); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19406 | return Smi::FromInt(day); |
| 19407 | } |
| 19408 | |
| 19409 | int time_in_day_ms = DateCache::TimeInDay(time_ms, days); |
| 19410 | switch (index) { |
| 19411 | case kHourUTC: return Smi::FromInt(time_in_day_ms / (60 * 60 * 1000)); |
| 19412 | case kMinuteUTC: return Smi::FromInt((time_in_day_ms / (60 * 1000)) % 60); |
| 19413 | case kSecondUTC: return Smi::FromInt((time_in_day_ms / 1000) % 60); |
| 19414 | case kMillisecondUTC: return Smi::FromInt(time_in_day_ms % 1000); |
| 19415 | case kDaysUTC: return Smi::FromInt(days); |
| 19416 | case kTimeInDayUTC: return Smi::FromInt(time_in_day_ms); |
| 19417 | default: UNREACHABLE(); |
| 19418 | } |
| 19419 | |
| 19420 | UNREACHABLE(); |
| 19421 | return NULL; |
| 19422 | } |
| 19423 | |
| 19424 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19425 | // static |
| 19426 | Handle<Object> JSDate::SetValue(Handle<JSDate> date, double v) { |
| 19427 | Isolate* const isolate = date->GetIsolate(); |
| 19428 | Handle<Object> value = isolate->factory()->NewNumber(v); |
| 19429 | bool value_is_nan = std::isnan(v); |
| 19430 | date->SetValue(*value, value_is_nan); |
| 19431 | return value; |
| 19432 | } |
| 19433 | |
| 19434 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19435 | void JSDate::SetValue(Object* value, bool is_value_nan) { |
| 19436 | set_value(value); |
| 19437 | if (is_value_nan) { |
| 19438 | HeapNumber* nan = GetIsolate()->heap()->nan_value(); |
| 19439 | set_cache_stamp(nan, SKIP_WRITE_BARRIER); |
| 19440 | set_year(nan, SKIP_WRITE_BARRIER); |
| 19441 | set_month(nan, SKIP_WRITE_BARRIER); |
| 19442 | set_day(nan, SKIP_WRITE_BARRIER); |
| 19443 | set_hour(nan, SKIP_WRITE_BARRIER); |
| 19444 | set_min(nan, SKIP_WRITE_BARRIER); |
| 19445 | set_sec(nan, SKIP_WRITE_BARRIER); |
| 19446 | set_weekday(nan, SKIP_WRITE_BARRIER); |
| 19447 | } else { |
| 19448 | set_cache_stamp(Smi::FromInt(DateCache::kInvalidStamp), SKIP_WRITE_BARRIER); |
| 19449 | } |
| 19450 | } |
| 19451 | |
| 19452 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19453 | // static |
| 19454 | MaybeHandle<Object> JSDate::ToPrimitive(Handle<JSReceiver> receiver, |
| 19455 | Handle<Object> hint) { |
| 19456 | Isolate* const isolate = receiver->GetIsolate(); |
| 19457 | if (hint->IsString()) { |
| 19458 | Handle<String> hint_string = Handle<String>::cast(hint); |
| 19459 | if (hint_string->Equals(isolate->heap()->number_string())) { |
| 19460 | return JSReceiver::OrdinaryToPrimitive(receiver, |
| 19461 | OrdinaryToPrimitiveHint::kNumber); |
| 19462 | } |
| 19463 | if (hint_string->Equals(isolate->heap()->default_string()) || |
| 19464 | hint_string->Equals(isolate->heap()->string_string())) { |
| 19465 | return JSReceiver::OrdinaryToPrimitive(receiver, |
| 19466 | OrdinaryToPrimitiveHint::kString); |
| 19467 | } |
| 19468 | } |
| 19469 | THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kInvalidHint, hint), |
| 19470 | Object); |
| 19471 | } |
| 19472 | |
| 19473 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19474 | void JSDate::SetCachedFields(int64_t local_time_ms, DateCache* date_cache) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19475 | int days = DateCache::DaysFromTime(local_time_ms); |
| 19476 | int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days); |
| 19477 | int year, month, day; |
| 19478 | date_cache->YearMonthDayFromDays(days, &year, &month, &day); |
| 19479 | int weekday = date_cache->Weekday(days); |
| 19480 | int hour = time_in_day_ms / (60 * 60 * 1000); |
| 19481 | int min = (time_in_day_ms / (60 * 1000)) % 60; |
| 19482 | int sec = (time_in_day_ms / 1000) % 60; |
| 19483 | set_cache_stamp(date_cache->stamp()); |
| 19484 | set_year(Smi::FromInt(year), SKIP_WRITE_BARRIER); |
| 19485 | set_month(Smi::FromInt(month), SKIP_WRITE_BARRIER); |
| 19486 | set_day(Smi::FromInt(day), SKIP_WRITE_BARRIER); |
| 19487 | set_weekday(Smi::FromInt(weekday), SKIP_WRITE_BARRIER); |
| 19488 | set_hour(Smi::FromInt(hour), SKIP_WRITE_BARRIER); |
| 19489 | set_min(Smi::FromInt(min), SKIP_WRITE_BARRIER); |
| 19490 | set_sec(Smi::FromInt(sec), SKIP_WRITE_BARRIER); |
| 19491 | } |
| 19492 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19493 | |
| 19494 | void JSArrayBuffer::Neuter() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 19495 | CHECK(is_neuterable()); |
| 19496 | CHECK(is_external()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19497 | set_backing_store(NULL); |
| 19498 | set_byte_length(Smi::FromInt(0)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19499 | set_was_neutered(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19500 | } |
| 19501 | |
| 19502 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19503 | void JSArrayBuffer::Setup(Handle<JSArrayBuffer> array_buffer, Isolate* isolate, |
| 19504 | bool is_external, void* data, size_t allocated_length, |
| 19505 | SharedFlag shared) { |
| 19506 | DCHECK(array_buffer->GetInternalFieldCount() == |
| 19507 | v8::ArrayBuffer::kInternalFieldCount); |
| 19508 | for (int i = 0; i < v8::ArrayBuffer::kInternalFieldCount; i++) { |
| 19509 | array_buffer->SetInternalField(i, Smi::FromInt(0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19510 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19511 | array_buffer->set_bit_field(0); |
| 19512 | array_buffer->set_is_external(is_external); |
| 19513 | array_buffer->set_is_neuterable(shared == SharedFlag::kNotShared); |
| 19514 | array_buffer->set_is_shared(shared == SharedFlag::kShared); |
| 19515 | |
| 19516 | Handle<Object> byte_length = |
| 19517 | isolate->factory()->NewNumberFromSize(allocated_length); |
| 19518 | CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber()); |
| 19519 | array_buffer->set_byte_length(*byte_length); |
| 19520 | // Initialize backing store at last to avoid handling of |JSArrayBuffers| that |
| 19521 | // are currently being constructed in the |ArrayBufferTracker|. The |
| 19522 | // registration method below handles the case of registering a buffer that has |
| 19523 | // already been promoted. |
| 19524 | array_buffer->set_backing_store(data); |
| 19525 | |
| 19526 | if (data && !is_external) { |
| 19527 | isolate->heap()->RegisterNewArrayBuffer(*array_buffer); |
| 19528 | } |
| 19529 | } |
| 19530 | |
| 19531 | |
| 19532 | bool JSArrayBuffer::SetupAllocatingData(Handle<JSArrayBuffer> array_buffer, |
| 19533 | Isolate* isolate, |
| 19534 | size_t allocated_length, |
| 19535 | bool initialize, SharedFlag shared) { |
| 19536 | void* data; |
| 19537 | CHECK(isolate->array_buffer_allocator() != NULL); |
| 19538 | // Prevent creating array buffers when serializing. |
| 19539 | DCHECK(!isolate->serializer_enabled()); |
| 19540 | if (allocated_length != 0) { |
| 19541 | if (initialize) { |
| 19542 | data = isolate->array_buffer_allocator()->Allocate(allocated_length); |
| 19543 | } else { |
| 19544 | data = isolate->array_buffer_allocator()->AllocateUninitialized( |
| 19545 | allocated_length); |
| 19546 | } |
| 19547 | if (data == NULL) return false; |
| 19548 | } else { |
| 19549 | data = NULL; |
| 19550 | } |
| 19551 | |
| 19552 | JSArrayBuffer::Setup(array_buffer, isolate, false, data, allocated_length, |
| 19553 | shared); |
| 19554 | return true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19555 | } |
| 19556 | |
| 19557 | |
| 19558 | Handle<JSArrayBuffer> JSTypedArray::MaterializeArrayBuffer( |
| 19559 | Handle<JSTypedArray> typed_array) { |
| 19560 | |
| 19561 | Handle<Map> map(typed_array->map()); |
| 19562 | Isolate* isolate = typed_array->GetIsolate(); |
| 19563 | |
| 19564 | DCHECK(IsFixedTypedArrayElementsKind(map->elements_kind())); |
| 19565 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19566 | Handle<FixedTypedArrayBase> fixed_typed_array( |
| 19567 | FixedTypedArrayBase::cast(typed_array->elements())); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19568 | |
| 19569 | Handle<JSArrayBuffer> buffer(JSArrayBuffer::cast(typed_array->buffer()), |
| 19570 | isolate); |
| 19571 | void* backing_store = |
| 19572 | isolate->array_buffer_allocator()->AllocateUninitialized( |
| 19573 | fixed_typed_array->DataSize()); |
| 19574 | buffer->set_is_external(false); |
| 19575 | DCHECK(buffer->byte_length()->IsSmi() || |
| 19576 | buffer->byte_length()->IsHeapNumber()); |
| 19577 | DCHECK(NumberToInt32(buffer->byte_length()) == fixed_typed_array->DataSize()); |
| 19578 | // Initialize backing store at last to avoid handling of |JSArrayBuffers| that |
| 19579 | // are currently being constructed in the |ArrayBufferTracker|. The |
| 19580 | // registration method below handles the case of registering a buffer that has |
| 19581 | // already been promoted. |
| 19582 | buffer->set_backing_store(backing_store); |
| 19583 | isolate->heap()->RegisterNewArrayBuffer(*buffer); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19584 | memcpy(buffer->backing_store(), |
| 19585 | fixed_typed_array->DataPtr(), |
| 19586 | fixed_typed_array->DataSize()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19587 | Handle<FixedTypedArrayBase> new_elements = |
| 19588 | isolate->factory()->NewFixedTypedArrayWithExternalPointer( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19589 | fixed_typed_array->length(), typed_array->type(), |
| 19590 | static_cast<uint8_t*>(buffer->backing_store())); |
| 19591 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19592 | typed_array->set_elements(*new_elements); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19593 | |
| 19594 | return buffer; |
| 19595 | } |
| 19596 | |
| 19597 | |
| 19598 | Handle<JSArrayBuffer> JSTypedArray::GetBuffer() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19599 | Handle<JSArrayBuffer> array_buffer(JSArrayBuffer::cast(buffer()), |
| 19600 | GetIsolate()); |
| 19601 | if (array_buffer->was_neutered() || |
| 19602 | array_buffer->backing_store() != nullptr) { |
| 19603 | return array_buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19604 | } |
| 19605 | Handle<JSTypedArray> self(this); |
| 19606 | return MaterializeArrayBuffer(self); |
| 19607 | } |
| 19608 | |
| 19609 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19610 | Handle<PropertyCell> PropertyCell::InvalidateEntry( |
| 19611 | Handle<GlobalDictionary> dictionary, int entry) { |
| 19612 | Isolate* isolate = dictionary->GetIsolate(); |
| 19613 | // Swap with a copy. |
| 19614 | DCHECK(dictionary->ValueAt(entry)->IsPropertyCell()); |
| 19615 | Handle<PropertyCell> cell(PropertyCell::cast(dictionary->ValueAt(entry))); |
| 19616 | auto new_cell = isolate->factory()->NewPropertyCell(); |
| 19617 | new_cell->set_value(cell->value()); |
| 19618 | dictionary->ValueAtPut(entry, *new_cell); |
| 19619 | bool is_the_hole = cell->value()->IsTheHole(); |
| 19620 | // Cell is officially mutable henceforth. |
| 19621 | PropertyDetails details = cell->property_details(); |
| 19622 | details = details.set_cell_type(is_the_hole ? PropertyCellType::kInvalidated |
| 19623 | : PropertyCellType::kMutable); |
| 19624 | new_cell->set_property_details(details); |
| 19625 | // Old cell is ready for invalidation. |
| 19626 | if (is_the_hole) { |
| 19627 | cell->set_value(isolate->heap()->undefined_value()); |
| 19628 | } else { |
| 19629 | cell->set_value(isolate->heap()->the_hole_value()); |
| 19630 | } |
| 19631 | details = details.set_cell_type(PropertyCellType::kInvalidated); |
| 19632 | cell->set_property_details(details); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19633 | cell->dependent_code()->DeoptimizeDependentCodeGroup( |
| 19634 | isolate, DependentCode::kPropertyCellChangedGroup); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19635 | return new_cell; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19636 | } |
| 19637 | |
| 19638 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19639 | PropertyCellConstantType PropertyCell::GetConstantType() { |
| 19640 | if (value()->IsSmi()) return PropertyCellConstantType::kSmi; |
| 19641 | return PropertyCellConstantType::kStableMap; |
| 19642 | } |
| 19643 | |
| 19644 | |
| 19645 | static bool RemainsConstantType(Handle<PropertyCell> cell, |
| 19646 | Handle<Object> value) { |
| 19647 | // TODO(dcarney): double->smi and smi->double transition from kConstant |
| 19648 | if (cell->value()->IsSmi() && value->IsSmi()) { |
| 19649 | return true; |
| 19650 | } else if (cell->value()->IsHeapObject() && value->IsHeapObject()) { |
| 19651 | return HeapObject::cast(cell->value())->map() == |
| 19652 | HeapObject::cast(*value)->map() && |
| 19653 | HeapObject::cast(*value)->map()->is_stable(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 19654 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19655 | return false; |
| 19656 | } |
| 19657 | |
| 19658 | |
| 19659 | PropertyCellType PropertyCell::UpdatedType(Handle<PropertyCell> cell, |
| 19660 | Handle<Object> value, |
| 19661 | PropertyDetails details) { |
| 19662 | PropertyCellType type = details.cell_type(); |
| 19663 | DCHECK(!value->IsTheHole()); |
| 19664 | if (cell->value()->IsTheHole()) { |
| 19665 | switch (type) { |
| 19666 | // Only allow a cell to transition once into constant state. |
| 19667 | case PropertyCellType::kUninitialized: |
| 19668 | if (value->IsUndefined()) return PropertyCellType::kUndefined; |
| 19669 | return PropertyCellType::kConstant; |
| 19670 | case PropertyCellType::kInvalidated: |
| 19671 | return PropertyCellType::kMutable; |
| 19672 | default: |
| 19673 | UNREACHABLE(); |
| 19674 | return PropertyCellType::kMutable; |
| 19675 | } |
| 19676 | } |
| 19677 | switch (type) { |
| 19678 | case PropertyCellType::kUndefined: |
| 19679 | return PropertyCellType::kConstant; |
| 19680 | case PropertyCellType::kConstant: |
| 19681 | if (*value == cell->value()) return PropertyCellType::kConstant; |
| 19682 | // Fall through. |
| 19683 | case PropertyCellType::kConstantType: |
| 19684 | if (RemainsConstantType(cell, value)) { |
| 19685 | return PropertyCellType::kConstantType; |
| 19686 | } |
| 19687 | // Fall through. |
| 19688 | case PropertyCellType::kMutable: |
| 19689 | return PropertyCellType::kMutable; |
| 19690 | } |
| 19691 | UNREACHABLE(); |
| 19692 | return PropertyCellType::kMutable; |
| 19693 | } |
| 19694 | |
| 19695 | |
| 19696 | void PropertyCell::UpdateCell(Handle<GlobalDictionary> dictionary, int entry, |
| 19697 | Handle<Object> value, PropertyDetails details) { |
| 19698 | DCHECK(!value->IsTheHole()); |
| 19699 | DCHECK(dictionary->ValueAt(entry)->IsPropertyCell()); |
| 19700 | Handle<PropertyCell> cell(PropertyCell::cast(dictionary->ValueAt(entry))); |
| 19701 | const PropertyDetails original_details = cell->property_details(); |
| 19702 | // Data accesses could be cached in ics or optimized code. |
| 19703 | bool invalidate = |
| 19704 | original_details.kind() == kData && details.kind() == kAccessor; |
| 19705 | int index = original_details.dictionary_index(); |
| 19706 | PropertyCellType old_type = original_details.cell_type(); |
| 19707 | // Preserve the enumeration index unless the property was deleted or never |
| 19708 | // initialized. |
| 19709 | if (cell->value()->IsTheHole()) { |
| 19710 | index = dictionary->NextEnumerationIndex(); |
| 19711 | dictionary->SetNextEnumerationIndex(index + 1); |
| 19712 | // Negative lookup cells must be invalidated. |
| 19713 | invalidate = true; |
| 19714 | } |
| 19715 | DCHECK(index > 0); |
| 19716 | details = details.set_index(index); |
| 19717 | |
| 19718 | PropertyCellType new_type = UpdatedType(cell, value, original_details); |
| 19719 | if (invalidate) cell = PropertyCell::InvalidateEntry(dictionary, entry); |
| 19720 | |
| 19721 | // Install new property details and cell value. |
| 19722 | details = details.set_cell_type(new_type); |
| 19723 | cell->set_property_details(details); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19724 | cell->set_value(*value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19725 | |
| 19726 | // Deopt when transitioning from a constant type. |
| 19727 | if (!invalidate && (old_type != new_type || |
| 19728 | original_details.IsReadOnly() != details.IsReadOnly())) { |
| 19729 | Isolate* isolate = dictionary->GetIsolate(); |
| 19730 | cell->dependent_code()->DeoptimizeDependentCodeGroup( |
| 19731 | isolate, DependentCode::kPropertyCellChangedGroup); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19732 | } |
| 19733 | } |
| 19734 | |
| 19735 | |
| 19736 | // static |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19737 | void PropertyCell::SetValueWithInvalidation(Handle<PropertyCell> cell, |
| 19738 | Handle<Object> new_value) { |
| 19739 | if (cell->value() != *new_value) { |
| 19740 | cell->set_value(*new_value); |
| 19741 | Isolate* isolate = cell->GetIsolate(); |
| 19742 | cell->dependent_code()->DeoptimizeDependentCodeGroup( |
| 19743 | isolate, DependentCode::kPropertyCellChangedGroup); |
| 19744 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19745 | } |
| 19746 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 19747 | } // namespace internal |
| 19748 | } // namespace v8 |