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 | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 11 | #include "src/objects-inl.h" |
| 12 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13 | #include "src/accessors.h" |
| 14 | #include "src/allocation-site-scopes.h" |
| 15 | #include "src/api.h" |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 16 | #include "src/api-arguments.h" |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 17 | #include "src/api-natives.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18 | #include "src/base/bits.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19 | #include "src/base/utils/random-number-generator.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 20 | #include "src/bootstrapper.h" |
| 21 | #include "src/code-stubs.h" |
| 22 | #include "src/codegen.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 23 | #include "src/compilation-dependencies.h" |
| 24 | #include "src/compiler.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 25 | #include "src/date.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 26 | #include "src/debug/debug.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 27 | #include "src/deoptimizer.h" |
| 28 | #include "src/elements.h" |
| 29 | #include "src/execution.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 30 | #include "src/field-index-inl.h" |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 31 | #include "src/field-index.h" |
| 32 | #include "src/field-type.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 33 | #include "src/full-codegen/full-codegen.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 34 | #include "src/ic/ic.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 35 | #include "src/identity-map.h" |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 36 | #include "src/interpreter/bytecode-array-iterator.h" |
| 37 | #include "src/interpreter/interpreter.h" |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 38 | #include "src/interpreter/source-position-table.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 39 | #include "src/isolate-inl.h" |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 40 | #include "src/keys.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 41 | #include "src/list.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 42 | #include "src/log.h" |
| 43 | #include "src/lookup.h" |
| 44 | #include "src/macro-assembler.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 45 | #include "src/messages.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 46 | #include "src/objects-body-descriptors-inl.h" |
| 47 | #include "src/profiler/cpu-profiler.h" |
| 48 | #include "src/property-descriptor.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 49 | #include "src/prototype.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 50 | #include "src/regexp/jsregexp.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 51 | #include "src/safepoint-table.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 52 | #include "src/string-builder.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 53 | #include "src/string-search.h" |
| 54 | #include "src/string-stream.h" |
| 55 | #include "src/utils.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 56 | #include "src/zone.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 57 | |
| 58 | #ifdef ENABLE_DISASSEMBLER |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 59 | #include "src/disasm.h" |
| 60 | #include "src/disassembler.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 61 | #endif |
| 62 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 63 | namespace v8 { |
| 64 | namespace internal { |
| 65 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 66 | std::ostream& operator<<(std::ostream& os, InstanceType instance_type) { |
| 67 | switch (instance_type) { |
| 68 | #define WRITE_TYPE(TYPE) \ |
| 69 | case TYPE: \ |
| 70 | return os << #TYPE; |
| 71 | INSTANCE_TYPE_LIST(WRITE_TYPE) |
| 72 | #undef WRITE_TYPE |
| 73 | } |
| 74 | UNREACHABLE(); |
| 75 | return os << "UNKNOWN"; // Keep the compiler happy. |
| 76 | } |
| 77 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 78 | Handle<FieldType> Object::OptimalType(Isolate* isolate, |
| 79 | Representation representation) { |
| 80 | if (representation.IsNone()) return FieldType::None(isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 81 | if (FLAG_track_field_types) { |
| 82 | if (representation.IsHeapObject() && IsHeapObject()) { |
| 83 | // We can track only JavaScript objects with stable maps. |
| 84 | Handle<Map> map(HeapObject::cast(this)->map(), isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 85 | if (map->is_stable() && map->IsJSReceiverMap()) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 86 | return FieldType::Class(map, isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 90 | return FieldType::Any(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 93 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 94 | MaybeHandle<JSReceiver> Object::ToObject(Isolate* isolate, |
| 95 | Handle<Object> object, |
| 96 | Handle<Context> native_context) { |
| 97 | if (object->IsJSReceiver()) return Handle<JSReceiver>::cast(object); |
| 98 | Handle<JSFunction> constructor; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 99 | if (object->IsSmi()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 100 | constructor = handle(native_context->number_function(), isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 101 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 102 | int constructor_function_index = |
| 103 | Handle<HeapObject>::cast(object)->map()->GetConstructorFunctionIndex(); |
| 104 | if (constructor_function_index == Map::kNoConstructorFunctionIndex) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 105 | THROW_NEW_ERROR(isolate, |
| 106 | NewTypeError(MessageTemplate::kUndefinedOrNullToObject), |
| 107 | JSReceiver); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 108 | } |
| 109 | constructor = handle( |
| 110 | JSFunction::cast(native_context->get(constructor_function_index)), |
| 111 | isolate); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 112 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 113 | Handle<JSObject> result = isolate->factory()->NewJSObject(constructor); |
| 114 | Handle<JSValue>::cast(result)->set_value(*object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 115 | return result; |
| 116 | } |
| 117 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 118 | // ES6 section 9.2.1.2, OrdinaryCallBindThis for sloppy callee. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 119 | // static |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 120 | MaybeHandle<JSReceiver> Object::ConvertReceiver(Isolate* isolate, |
| 121 | Handle<Object> object) { |
| 122 | if (object->IsJSReceiver()) return Handle<JSReceiver>::cast(object); |
| 123 | if (*object == isolate->heap()->null_value() || |
| 124 | *object == isolate->heap()->undefined_value()) { |
| 125 | return handle(isolate->global_proxy(), isolate); |
| 126 | } |
| 127 | return Object::ToObject(isolate, object); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 130 | // static |
| 131 | MaybeHandle<Object> Object::ToNumber(Handle<Object> input) { |
| 132 | while (true) { |
| 133 | if (input->IsNumber()) { |
| 134 | return input; |
| 135 | } |
| 136 | if (input->IsString()) { |
| 137 | return String::ToNumber(Handle<String>::cast(input)); |
| 138 | } |
| 139 | if (input->IsOddball()) { |
| 140 | return Oddball::ToNumber(Handle<Oddball>::cast(input)); |
| 141 | } |
| 142 | Isolate* const isolate = Handle<HeapObject>::cast(input)->GetIsolate(); |
| 143 | if (input->IsSymbol()) { |
| 144 | THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSymbolToNumber), |
| 145 | Object); |
| 146 | } |
| 147 | if (input->IsSimd128Value()) { |
| 148 | THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSimdToNumber), |
| 149 | Object); |
| 150 | } |
| 151 | ASSIGN_RETURN_ON_EXCEPTION( |
| 152 | isolate, input, JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(input), |
| 153 | ToPrimitiveHint::kNumber), |
| 154 | Object); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | |
| 159 | // static |
| 160 | MaybeHandle<Object> Object::ToInteger(Isolate* isolate, Handle<Object> input) { |
| 161 | ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object); |
| 162 | return isolate->factory()->NewNumber(DoubleToInteger(input->Number())); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | // static |
| 167 | MaybeHandle<Object> Object::ToInt32(Isolate* isolate, Handle<Object> input) { |
| 168 | ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object); |
| 169 | return isolate->factory()->NewNumberFromInt(DoubleToInt32(input->Number())); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | // static |
| 174 | MaybeHandle<Object> Object::ToUint32(Isolate* isolate, Handle<Object> input) { |
| 175 | ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object); |
| 176 | return isolate->factory()->NewNumberFromUint(DoubleToUint32(input->Number())); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | // static |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 181 | MaybeHandle<Name> Object::ConvertToName(Isolate* isolate, |
| 182 | Handle<Object> input) { |
| 183 | ASSIGN_RETURN_ON_EXCEPTION( |
| 184 | isolate, input, Object::ToPrimitive(input, ToPrimitiveHint::kString), |
| 185 | Name); |
| 186 | if (input->IsName()) return Handle<Name>::cast(input); |
| 187 | return ToString(isolate, input); |
| 188 | } |
| 189 | |
| 190 | // static |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 191 | MaybeHandle<String> Object::ToString(Isolate* isolate, Handle<Object> input) { |
| 192 | while (true) { |
| 193 | if (input->IsString()) { |
| 194 | return Handle<String>::cast(input); |
| 195 | } |
| 196 | if (input->IsOddball()) { |
| 197 | return handle(Handle<Oddball>::cast(input)->to_string(), isolate); |
| 198 | } |
| 199 | if (input->IsNumber()) { |
| 200 | return isolate->factory()->NumberToString(input); |
| 201 | } |
| 202 | if (input->IsSymbol()) { |
| 203 | THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSymbolToString), |
| 204 | String); |
| 205 | } |
| 206 | if (input->IsSimd128Value()) { |
| 207 | return Simd128Value::ToString(Handle<Simd128Value>::cast(input)); |
| 208 | } |
| 209 | ASSIGN_RETURN_ON_EXCEPTION( |
| 210 | isolate, input, JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(input), |
| 211 | ToPrimitiveHint::kString), |
| 212 | String); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | |
| 217 | // static |
| 218 | MaybeHandle<Object> Object::ToLength(Isolate* isolate, Handle<Object> input) { |
| 219 | ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(input), Object); |
| 220 | double len = DoubleToInteger(input->Number()); |
| 221 | if (len <= 0.0) { |
| 222 | len = 0.0; |
| 223 | } else if (len >= kMaxSafeInteger) { |
| 224 | len = kMaxSafeInteger; |
| 225 | } |
| 226 | return isolate->factory()->NewNumber(len); |
| 227 | } |
| 228 | |
| 229 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 230 | bool Object::BooleanValue() { |
| 231 | if (IsBoolean()) return IsTrue(); |
| 232 | if (IsSmi()) return Smi::cast(this)->value() != 0; |
| 233 | if (IsUndefined() || IsNull()) return false; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 234 | if (IsUndetectable()) return false; // Undetectable object is false. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 235 | if (IsString()) return String::cast(this)->length() != 0; |
| 236 | if (IsHeapNumber()) return HeapNumber::cast(this)->HeapNumberBooleanValue(); |
| 237 | return true; |
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 | namespace { |
| 242 | |
| 243 | // TODO(bmeurer): Maybe we should introduce a marker interface Number, |
| 244 | // where we put all these methods at some point? |
| 245 | ComparisonResult NumberCompare(double x, double y) { |
| 246 | if (std::isnan(x) || std::isnan(y)) { |
| 247 | return ComparisonResult::kUndefined; |
| 248 | } else if (x < y) { |
| 249 | return ComparisonResult::kLessThan; |
| 250 | } else if (x > y) { |
| 251 | return ComparisonResult::kGreaterThan; |
| 252 | } else { |
| 253 | return ComparisonResult::kEqual; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 254 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 258 | bool NumberEquals(double x, double y) { |
| 259 | // Must check explicitly for NaN's on Windows, but -0 works fine. |
| 260 | if (std::isnan(x)) return false; |
| 261 | if (std::isnan(y)) return false; |
| 262 | return x == y; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | bool NumberEquals(const Object* x, const Object* y) { |
| 267 | return NumberEquals(x->Number(), y->Number()); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | bool NumberEquals(Handle<Object> x, Handle<Object> y) { |
| 272 | return NumberEquals(*x, *y); |
| 273 | } |
| 274 | |
| 275 | } // namespace |
| 276 | |
| 277 | |
| 278 | // static |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 279 | Maybe<ComparisonResult> Object::Compare(Handle<Object> x, Handle<Object> y) { |
| 280 | // ES6 section 7.2.11 Abstract Relational Comparison step 3 and 4. |
| 281 | if (!Object::ToPrimitive(x, ToPrimitiveHint::kNumber).ToHandle(&x) || |
| 282 | !Object::ToPrimitive(y, ToPrimitiveHint::kNumber).ToHandle(&y)) { |
| 283 | return Nothing<ComparisonResult>(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 284 | } |
| 285 | if (x->IsString() && y->IsString()) { |
| 286 | // ES6 section 7.2.11 Abstract Relational Comparison step 5. |
| 287 | return Just( |
| 288 | String::Compare(Handle<String>::cast(x), Handle<String>::cast(y))); |
| 289 | } |
| 290 | // ES6 section 7.2.11 Abstract Relational Comparison step 6. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 291 | if (!Object::ToNumber(x).ToHandle(&x) || !Object::ToNumber(y).ToHandle(&y)) { |
| 292 | return Nothing<ComparisonResult>(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 293 | } |
| 294 | return Just(NumberCompare(x->Number(), y->Number())); |
| 295 | } |
| 296 | |
| 297 | |
| 298 | // static |
| 299 | Maybe<bool> Object::Equals(Handle<Object> x, Handle<Object> y) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 300 | // This is the generic version of Abstract Equality Comparison; a version in |
| 301 | // JavaScript land is available in the EqualStub and NotEqualStub. Whenever |
| 302 | // you change something functionality wise in here, remember to update the |
| 303 | // TurboFan code stubs as well. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 304 | while (true) { |
| 305 | if (x->IsNumber()) { |
| 306 | if (y->IsNumber()) { |
| 307 | return Just(NumberEquals(x, y)); |
| 308 | } else if (y->IsBoolean()) { |
| 309 | return Just(NumberEquals(*x, Handle<Oddball>::cast(y)->to_number())); |
| 310 | } else if (y->IsString()) { |
| 311 | return Just(NumberEquals(x, String::ToNumber(Handle<String>::cast(y)))); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 312 | } else if (y->IsJSReceiver()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 313 | if (!JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(y)) |
| 314 | .ToHandle(&y)) { |
| 315 | return Nothing<bool>(); |
| 316 | } |
| 317 | } else { |
| 318 | return Just(false); |
| 319 | } |
| 320 | } else if (x->IsString()) { |
| 321 | if (y->IsString()) { |
| 322 | return Just( |
| 323 | String::Equals(Handle<String>::cast(x), Handle<String>::cast(y))); |
| 324 | } else if (y->IsNumber()) { |
| 325 | x = String::ToNumber(Handle<String>::cast(x)); |
| 326 | return Just(NumberEquals(x, y)); |
| 327 | } else if (y->IsBoolean()) { |
| 328 | x = String::ToNumber(Handle<String>::cast(x)); |
| 329 | return Just(NumberEquals(*x, Handle<Oddball>::cast(y)->to_number())); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 330 | } else if (y->IsJSReceiver()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 331 | if (!JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(y)) |
| 332 | .ToHandle(&y)) { |
| 333 | return Nothing<bool>(); |
| 334 | } |
| 335 | } else { |
| 336 | return Just(false); |
| 337 | } |
| 338 | } else if (x->IsBoolean()) { |
| 339 | if (y->IsOddball()) { |
| 340 | return Just(x.is_identical_to(y)); |
| 341 | } else if (y->IsNumber()) { |
| 342 | return Just(NumberEquals(Handle<Oddball>::cast(x)->to_number(), *y)); |
| 343 | } else if (y->IsString()) { |
| 344 | y = String::ToNumber(Handle<String>::cast(y)); |
| 345 | return Just(NumberEquals(Handle<Oddball>::cast(x)->to_number(), *y)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 346 | } else if (y->IsJSReceiver()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 347 | if (!JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(y)) |
| 348 | .ToHandle(&y)) { |
| 349 | return Nothing<bool>(); |
| 350 | } |
| 351 | x = Oddball::ToNumber(Handle<Oddball>::cast(x)); |
| 352 | } else { |
| 353 | return Just(false); |
| 354 | } |
| 355 | } else if (x->IsSymbol()) { |
| 356 | if (y->IsSymbol()) { |
| 357 | return Just(x.is_identical_to(y)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 358 | } else if (y->IsJSReceiver()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 359 | if (!JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(y)) |
| 360 | .ToHandle(&y)) { |
| 361 | return Nothing<bool>(); |
| 362 | } |
| 363 | } else { |
| 364 | return Just(false); |
| 365 | } |
| 366 | } else if (x->IsSimd128Value()) { |
| 367 | if (y->IsSimd128Value()) { |
| 368 | return Just(Simd128Value::Equals(Handle<Simd128Value>::cast(x), |
| 369 | Handle<Simd128Value>::cast(y))); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 370 | } else if (y->IsJSReceiver()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 371 | if (!JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(y)) |
| 372 | .ToHandle(&y)) { |
| 373 | return Nothing<bool>(); |
| 374 | } |
| 375 | } else { |
| 376 | return Just(false); |
| 377 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 378 | } else if (x->IsJSReceiver()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 379 | if (y->IsJSReceiver()) { |
| 380 | return Just(x.is_identical_to(y)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 381 | } else if (y->IsUndetectable()) { |
| 382 | return Just(x->IsUndetectable()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 383 | } else if (y->IsBoolean()) { |
| 384 | y = Oddball::ToNumber(Handle<Oddball>::cast(y)); |
| 385 | } else if (!JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(x)) |
| 386 | .ToHandle(&x)) { |
| 387 | return Nothing<bool>(); |
| 388 | } |
| 389 | } else { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 390 | return Just(x->IsUndetectable() && y->IsUndetectable()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | |
| 396 | bool Object::StrictEquals(Object* that) { |
| 397 | if (this->IsNumber()) { |
| 398 | if (!that->IsNumber()) return false; |
| 399 | return NumberEquals(this, that); |
| 400 | } else if (this->IsString()) { |
| 401 | if (!that->IsString()) return false; |
| 402 | return String::cast(this)->Equals(String::cast(that)); |
| 403 | } else if (this->IsSimd128Value()) { |
| 404 | if (!that->IsSimd128Value()) return false; |
| 405 | return Simd128Value::cast(this)->Equals(Simd128Value::cast(that)); |
| 406 | } |
| 407 | return this == that; |
| 408 | } |
| 409 | |
| 410 | |
| 411 | // static |
| 412 | Handle<String> Object::TypeOf(Isolate* isolate, Handle<Object> object) { |
| 413 | if (object->IsNumber()) return isolate->factory()->number_string(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 414 | if (object->IsOddball()) return handle(Oddball::cast(*object)->type_of()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 415 | if (object->IsUndetectable()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 416 | return isolate->factory()->undefined_string(); |
| 417 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 418 | if (object->IsString()) return isolate->factory()->string_string(); |
| 419 | if (object->IsSymbol()) return isolate->factory()->symbol_string(); |
| 420 | if (object->IsString()) return isolate->factory()->string_string(); |
| 421 | #define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \ |
| 422 | if (object->Is##Type()) return isolate->factory()->type##_string(); |
| 423 | SIMD128_TYPES(SIMD128_TYPE) |
| 424 | #undef SIMD128_TYPE |
| 425 | if (object->IsCallable()) return isolate->factory()->function_string(); |
| 426 | return isolate->factory()->object_string(); |
| 427 | } |
| 428 | |
| 429 | |
| 430 | // static |
| 431 | MaybeHandle<Object> Object::Multiply(Isolate* isolate, Handle<Object> lhs, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 432 | Handle<Object> rhs) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 433 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 434 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 435 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 436 | } |
| 437 | return isolate->factory()->NewNumber(lhs->Number() * rhs->Number()); |
| 438 | } |
| 439 | |
| 440 | |
| 441 | // static |
| 442 | MaybeHandle<Object> Object::Divide(Isolate* isolate, Handle<Object> lhs, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 443 | Handle<Object> rhs) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 444 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 445 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 446 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 447 | } |
| 448 | return isolate->factory()->NewNumber(lhs->Number() / rhs->Number()); |
| 449 | } |
| 450 | |
| 451 | |
| 452 | // static |
| 453 | MaybeHandle<Object> Object::Modulus(Isolate* isolate, Handle<Object> lhs, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 454 | Handle<Object> rhs) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 455 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 456 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 457 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 458 | } |
| 459 | return isolate->factory()->NewNumber(modulo(lhs->Number(), rhs->Number())); |
| 460 | } |
| 461 | |
| 462 | |
| 463 | // static |
| 464 | MaybeHandle<Object> Object::Add(Isolate* isolate, Handle<Object> lhs, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 465 | Handle<Object> rhs) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 466 | if (lhs->IsNumber() && rhs->IsNumber()) { |
| 467 | return isolate->factory()->NewNumber(lhs->Number() + rhs->Number()); |
| 468 | } else if (lhs->IsString() && rhs->IsString()) { |
| 469 | return isolate->factory()->NewConsString(Handle<String>::cast(lhs), |
| 470 | Handle<String>::cast(rhs)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 471 | } |
| 472 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToPrimitive(lhs), Object); |
| 473 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToPrimitive(rhs), Object); |
| 474 | if (lhs->IsString() || rhs->IsString()) { |
| 475 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToString(isolate, rhs), |
| 476 | Object); |
| 477 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToString(isolate, lhs), |
| 478 | Object); |
| 479 | return isolate->factory()->NewConsString(Handle<String>::cast(lhs), |
| 480 | Handle<String>::cast(rhs)); |
| 481 | } |
| 482 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 483 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 484 | return isolate->factory()->NewNumber(lhs->Number() + rhs->Number()); |
| 485 | } |
| 486 | |
| 487 | |
| 488 | // static |
| 489 | MaybeHandle<Object> Object::Subtract(Isolate* isolate, Handle<Object> lhs, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 490 | Handle<Object> rhs) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 491 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 492 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 493 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 494 | } |
| 495 | return isolate->factory()->NewNumber(lhs->Number() - rhs->Number()); |
| 496 | } |
| 497 | |
| 498 | |
| 499 | // static |
| 500 | MaybeHandle<Object> Object::ShiftLeft(Isolate* isolate, Handle<Object> lhs, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 501 | Handle<Object> rhs) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 502 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 503 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 504 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 505 | } |
| 506 | return isolate->factory()->NewNumberFromInt(NumberToInt32(*lhs) |
| 507 | << (NumberToUint32(*rhs) & 0x1F)); |
| 508 | } |
| 509 | |
| 510 | |
| 511 | // static |
| 512 | MaybeHandle<Object> Object::ShiftRight(Isolate* isolate, Handle<Object> lhs, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 513 | Handle<Object> rhs) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 514 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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()->NewNumberFromInt(NumberToInt32(*lhs) >> |
| 519 | (NumberToUint32(*rhs) & 0x1F)); |
| 520 | } |
| 521 | |
| 522 | |
| 523 | // static |
| 524 | MaybeHandle<Object> Object::ShiftRightLogical(Isolate* isolate, |
| 525 | Handle<Object> lhs, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 526 | Handle<Object> rhs) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 527 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 528 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 529 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 530 | } |
| 531 | return isolate->factory()->NewNumberFromUint(NumberToUint32(*lhs) >> |
| 532 | (NumberToUint32(*rhs) & 0x1F)); |
| 533 | } |
| 534 | |
| 535 | |
| 536 | // static |
| 537 | MaybeHandle<Object> Object::BitwiseAnd(Isolate* isolate, Handle<Object> lhs, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 538 | Handle<Object> rhs) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 539 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 540 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 541 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 542 | } |
| 543 | return isolate->factory()->NewNumberFromInt(NumberToInt32(*lhs) & |
| 544 | NumberToInt32(*rhs)); |
| 545 | } |
| 546 | |
| 547 | |
| 548 | // static |
| 549 | MaybeHandle<Object> Object::BitwiseOr(Isolate* isolate, Handle<Object> lhs, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 550 | Handle<Object> rhs) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 551 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 552 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 553 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 554 | } |
| 555 | return isolate->factory()->NewNumberFromInt(NumberToInt32(*lhs) | |
| 556 | NumberToInt32(*rhs)); |
| 557 | } |
| 558 | |
| 559 | |
| 560 | // static |
| 561 | MaybeHandle<Object> Object::BitwiseXor(Isolate* isolate, Handle<Object> lhs, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 562 | Handle<Object> rhs) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 563 | if (!lhs->IsNumber() || !rhs->IsNumber()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 564 | ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToNumber(lhs), Object); |
| 565 | ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToNumber(rhs), Object); |
| 566 | } |
| 567 | return isolate->factory()->NewNumberFromInt(NumberToInt32(*lhs) ^ |
| 568 | NumberToInt32(*rhs)); |
| 569 | } |
| 570 | |
| 571 | |
| 572 | Maybe<bool> Object::IsArray(Handle<Object> object) { |
| 573 | if (object->IsJSArray()) return Just(true); |
| 574 | if (object->IsJSProxy()) { |
| 575 | Handle<JSProxy> proxy = Handle<JSProxy>::cast(object); |
| 576 | Isolate* isolate = proxy->GetIsolate(); |
| 577 | if (proxy->IsRevoked()) { |
| 578 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 579 | MessageTemplate::kProxyRevoked, |
| 580 | isolate->factory()->NewStringFromAsciiChecked("IsArray"))); |
| 581 | return Nothing<bool>(); |
| 582 | } |
| 583 | return Object::IsArray(handle(proxy->target(), isolate)); |
| 584 | } |
| 585 | return Just(false); |
| 586 | } |
| 587 | |
| 588 | |
| 589 | bool Object::IsPromise(Handle<Object> object) { |
| 590 | if (!object->IsJSObject()) return false; |
| 591 | auto js_object = Handle<JSObject>::cast(object); |
| 592 | // Promises can't have access checks. |
| 593 | if (js_object->map()->is_access_check_needed()) return false; |
| 594 | auto isolate = js_object->GetIsolate(); |
| 595 | // TODO(dcarney): this should just be read from the symbol registry so as not |
| 596 | // to be context dependent. |
| 597 | auto key = isolate->factory()->promise_status_symbol(); |
| 598 | // Shouldn't be possible to throw here. |
| 599 | return JSObject::HasRealNamedProperty(js_object, key).FromJust(); |
| 600 | } |
| 601 | |
| 602 | |
| 603 | // static |
| 604 | MaybeHandle<Object> Object::GetMethod(Handle<JSReceiver> receiver, |
| 605 | Handle<Name> name) { |
| 606 | Handle<Object> func; |
| 607 | Isolate* isolate = receiver->GetIsolate(); |
| 608 | ASSIGN_RETURN_ON_EXCEPTION(isolate, func, |
| 609 | JSReceiver::GetProperty(receiver, name), Object); |
| 610 | if (func->IsNull() || func->IsUndefined()) { |
| 611 | return isolate->factory()->undefined_value(); |
| 612 | } |
| 613 | if (!func->IsCallable()) { |
| 614 | THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kPropertyNotFunction, |
| 615 | func, name, receiver), |
| 616 | Object); |
| 617 | } |
| 618 | return func; |
| 619 | } |
| 620 | |
| 621 | |
| 622 | // static |
| 623 | MaybeHandle<FixedArray> Object::CreateListFromArrayLike( |
| 624 | Isolate* isolate, Handle<Object> object, ElementTypes element_types) { |
| 625 | // 1. ReturnIfAbrupt(object). |
| 626 | // 2. (default elementTypes -- not applicable.) |
| 627 | // 3. If Type(obj) is not Object, throw a TypeError exception. |
| 628 | if (!object->IsJSReceiver()) { |
| 629 | THROW_NEW_ERROR(isolate, |
| 630 | NewTypeError(MessageTemplate::kCalledOnNonObject, |
| 631 | isolate->factory()->NewStringFromAsciiChecked( |
| 632 | "CreateListFromArrayLike")), |
| 633 | FixedArray); |
| 634 | } |
| 635 | // 4. Let len be ? ToLength(? Get(obj, "length")). |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 636 | Handle<JSReceiver> receiver = Handle<JSReceiver>::cast(object); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 637 | Handle<Object> raw_length_obj; |
| 638 | ASSIGN_RETURN_ON_EXCEPTION( |
| 639 | isolate, raw_length_obj, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 640 | JSReceiver::GetProperty(receiver, isolate->factory()->length_string()), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 641 | FixedArray); |
| 642 | Handle<Object> raw_length_number; |
| 643 | ASSIGN_RETURN_ON_EXCEPTION(isolate, raw_length_number, |
| 644 | Object::ToLength(isolate, raw_length_obj), |
| 645 | FixedArray); |
| 646 | uint32_t len; |
| 647 | if (!raw_length_number->ToUint32(&len) || |
| 648 | len > static_cast<uint32_t>(FixedArray::kMaxLength)) { |
| 649 | THROW_NEW_ERROR(isolate, |
| 650 | NewRangeError(MessageTemplate::kInvalidArrayLength), |
| 651 | FixedArray); |
| 652 | } |
| 653 | // 5. Let list be an empty List. |
| 654 | Handle<FixedArray> list = isolate->factory()->NewFixedArray(len); |
| 655 | // 6. Let index be 0. |
| 656 | // 7. Repeat while index < len: |
| 657 | for (uint32_t index = 0; index < len; ++index) { |
| 658 | // 7a. Let indexName be ToString(index). |
| 659 | // 7b. Let next be ? Get(obj, indexName). |
| 660 | Handle<Object> next; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 661 | ASSIGN_RETURN_ON_EXCEPTION(isolate, next, |
| 662 | JSReceiver::GetElement(isolate, receiver, index), |
| 663 | FixedArray); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 664 | switch (element_types) { |
| 665 | case ElementTypes::kAll: |
| 666 | // Nothing to do. |
| 667 | break; |
| 668 | case ElementTypes::kStringAndSymbol: { |
| 669 | // 7c. If Type(next) is not an element of elementTypes, throw a |
| 670 | // TypeError exception. |
| 671 | if (!next->IsName()) { |
| 672 | THROW_NEW_ERROR(isolate, |
| 673 | NewTypeError(MessageTemplate::kNotPropertyName, next), |
| 674 | FixedArray); |
| 675 | } |
| 676 | // 7d. Append next as the last element of list. |
| 677 | // Internalize on the fly so we can use pointer identity later. |
| 678 | next = isolate->factory()->InternalizeName(Handle<Name>::cast(next)); |
| 679 | break; |
| 680 | } |
| 681 | } |
| 682 | list->set(index, *next); |
| 683 | // 7e. Set index to index + 1. (See loop header.) |
| 684 | } |
| 685 | // 8. Return list. |
| 686 | return list; |
| 687 | } |
| 688 | |
| 689 | |
| 690 | // static |
| 691 | Maybe<bool> JSReceiver::HasProperty(LookupIterator* it) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 692 | for (; it->IsFound(); it->Next()) { |
| 693 | switch (it->state()) { |
| 694 | case LookupIterator::NOT_FOUND: |
| 695 | case LookupIterator::TRANSITION: |
| 696 | UNREACHABLE(); |
| 697 | case LookupIterator::JSPROXY: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 698 | return JSProxy::HasProperty(it->isolate(), it->GetHolder<JSProxy>(), |
| 699 | it->GetName()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 700 | case LookupIterator::INTERCEPTOR: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 701 | Maybe<PropertyAttributes> result = |
| 702 | JSObject::GetPropertyAttributesWithInterceptor(it); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 703 | if (result.IsNothing()) return Nothing<bool>(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 704 | if (result.FromJust() != ABSENT) return Just(true); |
| 705 | break; |
| 706 | } |
| 707 | case LookupIterator::ACCESS_CHECK: { |
| 708 | if (it->HasAccess()) break; |
| 709 | Maybe<PropertyAttributes> result = |
| 710 | JSObject::GetPropertyAttributesWithFailedAccessCheck(it); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 711 | if (result.IsNothing()) return Nothing<bool>(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 712 | return Just(result.FromJust() != ABSENT); |
| 713 | } |
| 714 | case LookupIterator::INTEGER_INDEXED_EXOTIC: |
| 715 | // TypedArray out-of-bounds access. |
| 716 | return Just(false); |
| 717 | case LookupIterator::ACCESSOR: |
| 718 | case LookupIterator::DATA: |
| 719 | return Just(true); |
| 720 | } |
| 721 | } |
| 722 | return Just(false); |
| 723 | } |
| 724 | |
| 725 | |
| 726 | // static |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 727 | MaybeHandle<Object> Object::GetProperty(LookupIterator* it) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 728 | for (; it->IsFound(); it->Next()) { |
| 729 | switch (it->state()) { |
| 730 | case LookupIterator::NOT_FOUND: |
| 731 | case LookupIterator::TRANSITION: |
| 732 | UNREACHABLE(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 733 | case LookupIterator::JSPROXY: { |
| 734 | bool was_found; |
| 735 | MaybeHandle<Object> result = |
| 736 | JSProxy::GetProperty(it->isolate(), it->GetHolder<JSProxy>(), |
| 737 | it->GetName(), it->GetReceiver(), &was_found); |
| 738 | if (!was_found) it->NotFound(); |
| 739 | return result; |
| 740 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 741 | case LookupIterator::INTERCEPTOR: { |
| 742 | bool done; |
| 743 | Handle<Object> result; |
| 744 | ASSIGN_RETURN_ON_EXCEPTION( |
| 745 | it->isolate(), result, |
| 746 | JSObject::GetPropertyWithInterceptor(it, &done), Object); |
| 747 | if (done) return result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 748 | break; |
| 749 | } |
| 750 | case LookupIterator::ACCESS_CHECK: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 751 | if (it->HasAccess()) break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 752 | return JSObject::GetPropertyWithFailedAccessCheck(it); |
| 753 | case LookupIterator::ACCESSOR: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 754 | return GetPropertyWithAccessor(it); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 755 | case LookupIterator::INTEGER_INDEXED_EXOTIC: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 756 | return ReadAbsentProperty(it); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 757 | case LookupIterator::DATA: |
| 758 | return it->GetDataValue(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 759 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 760 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 761 | return ReadAbsentProperty(it); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 765 | #define STACK_CHECK(result_value) \ |
| 766 | do { \ |
| 767 | StackLimitCheck stack_check(isolate); \ |
| 768 | if (stack_check.HasOverflowed()) { \ |
| 769 | isolate->Throw(*isolate->factory()->NewRangeError( \ |
| 770 | MessageTemplate::kStackOverflow)); \ |
| 771 | return result_value; \ |
| 772 | } \ |
| 773 | } while (false) |
| 774 | |
| 775 | |
| 776 | // static |
| 777 | MaybeHandle<Object> JSProxy::GetProperty(Isolate* isolate, |
| 778 | Handle<JSProxy> proxy, |
| 779 | Handle<Name> name, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 780 | Handle<Object> receiver, |
| 781 | bool* was_found) { |
| 782 | *was_found = true; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 783 | if (receiver->IsJSGlobalObject()) { |
| 784 | THROW_NEW_ERROR( |
| 785 | isolate, |
| 786 | NewTypeError(MessageTemplate::kReadGlobalReferenceThroughProxy, name), |
| 787 | Object); |
| 788 | } |
| 789 | |
| 790 | DCHECK(!name->IsPrivate()); |
| 791 | STACK_CHECK(MaybeHandle<Object>()); |
| 792 | Handle<Name> trap_name = isolate->factory()->get_string(); |
| 793 | // 1. Assert: IsPropertyKey(P) is true. |
| 794 | // 2. Let handler be the value of the [[ProxyHandler]] internal slot of O. |
| 795 | Handle<Object> handler(proxy->handler(), isolate); |
| 796 | // 3. If handler is null, throw a TypeError exception. |
| 797 | // 4. Assert: Type(handler) is Object. |
| 798 | if (proxy->IsRevoked()) { |
| 799 | THROW_NEW_ERROR(isolate, |
| 800 | NewTypeError(MessageTemplate::kProxyRevoked, trap_name), |
| 801 | Object); |
| 802 | } |
| 803 | // 5. Let target be the value of the [[ProxyTarget]] internal slot of O. |
| 804 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 805 | // 6. Let trap be ? GetMethod(handler, "get"). |
| 806 | Handle<Object> trap; |
| 807 | ASSIGN_RETURN_ON_EXCEPTION( |
| 808 | isolate, trap, |
| 809 | Object::GetMethod(Handle<JSReceiver>::cast(handler), trap_name), Object); |
| 810 | // 7. If trap is undefined, then |
| 811 | if (trap->IsUndefined()) { |
| 812 | // 7.a Return target.[[Get]](P, Receiver). |
| 813 | LookupIterator it = |
| 814 | LookupIterator::PropertyOrElement(isolate, receiver, name, target); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 815 | MaybeHandle<Object> result = Object::GetProperty(&it); |
| 816 | *was_found = it.IsFound(); |
| 817 | return result; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 818 | } |
| 819 | // 8. Let trapResult be ? Call(trap, handler, «target, P, Receiver»). |
| 820 | Handle<Object> trap_result; |
| 821 | Handle<Object> args[] = {target, name, receiver}; |
| 822 | ASSIGN_RETURN_ON_EXCEPTION( |
| 823 | isolate, trap_result, |
| 824 | Execution::Call(isolate, trap, handler, arraysize(args), args), Object); |
| 825 | // 9. Let targetDesc be ? target.[[GetOwnProperty]](P). |
| 826 | PropertyDescriptor target_desc; |
| 827 | Maybe<bool> target_found = |
| 828 | JSReceiver::GetOwnPropertyDescriptor(isolate, target, name, &target_desc); |
| 829 | MAYBE_RETURN_NULL(target_found); |
| 830 | // 10. If targetDesc is not undefined, then |
| 831 | if (target_found.FromJust()) { |
| 832 | // 10.a. If IsDataDescriptor(targetDesc) and targetDesc.[[Configurable]] is |
| 833 | // false and targetDesc.[[Writable]] is false, then |
| 834 | // 10.a.i. If SameValue(trapResult, targetDesc.[[Value]]) is false, |
| 835 | // throw a TypeError exception. |
| 836 | bool inconsistent = PropertyDescriptor::IsDataDescriptor(&target_desc) && |
| 837 | !target_desc.configurable() && |
| 838 | !target_desc.writable() && |
| 839 | !trap_result->SameValue(*target_desc.value()); |
| 840 | if (inconsistent) { |
| 841 | THROW_NEW_ERROR( |
| 842 | isolate, NewTypeError(MessageTemplate::kProxyGetNonConfigurableData, |
| 843 | name, target_desc.value(), trap_result), |
| 844 | Object); |
| 845 | } |
| 846 | // 10.b. If IsAccessorDescriptor(targetDesc) and targetDesc.[[Configurable]] |
| 847 | // is false and targetDesc.[[Get]] is undefined, then |
| 848 | // 10.b.i. If trapResult is not undefined, throw a TypeError exception. |
| 849 | inconsistent = PropertyDescriptor::IsAccessorDescriptor(&target_desc) && |
| 850 | !target_desc.configurable() && |
| 851 | target_desc.get()->IsUndefined() && |
| 852 | !trap_result->IsUndefined(); |
| 853 | if (inconsistent) { |
| 854 | THROW_NEW_ERROR( |
| 855 | isolate, |
| 856 | NewTypeError(MessageTemplate::kProxyGetNonConfigurableAccessor, name, |
| 857 | trap_result), |
| 858 | Object); |
| 859 | } |
| 860 | } |
| 861 | // 11. Return trap_result |
| 862 | return trap_result; |
| 863 | } |
| 864 | |
| 865 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 866 | Handle<Object> JSReceiver::GetDataProperty(LookupIterator* it) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 867 | for (; it->IsFound(); it->Next()) { |
| 868 | switch (it->state()) { |
| 869 | case LookupIterator::INTERCEPTOR: |
| 870 | case LookupIterator::NOT_FOUND: |
| 871 | case LookupIterator::TRANSITION: |
| 872 | UNREACHABLE(); |
| 873 | case LookupIterator::ACCESS_CHECK: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 874 | // Support calling this method without an active context, but refuse |
| 875 | // access to access-checked objects in that case. |
| 876 | if (it->isolate()->context() != nullptr && it->HasAccess()) continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 877 | // Fall through. |
| 878 | case LookupIterator::JSPROXY: |
| 879 | it->NotFound(); |
| 880 | return it->isolate()->factory()->undefined_value(); |
| 881 | case LookupIterator::ACCESSOR: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 882 | // TODO(verwaest): For now this doesn't call into AccessorInfo, since |
| 883 | // clients don't need it. Update once relevant. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 884 | it->NotFound(); |
| 885 | return it->isolate()->factory()->undefined_value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 886 | case LookupIterator::INTEGER_INDEXED_EXOTIC: |
| 887 | return it->isolate()->factory()->undefined_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 888 | case LookupIterator::DATA: |
| 889 | return it->GetDataValue(); |
| 890 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 891 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 892 | return it->isolate()->factory()->undefined_value(); |
| 893 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 894 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 895 | |
| 896 | bool Object::ToInt32(int32_t* value) { |
| 897 | if (IsSmi()) { |
| 898 | *value = Smi::cast(this)->value(); |
| 899 | return true; |
| 900 | } |
| 901 | if (IsHeapNumber()) { |
| 902 | double num = HeapNumber::cast(this)->value(); |
| 903 | if (FastI2D(FastD2I(num)) == num) { |
| 904 | *value = FastD2I(num); |
| 905 | return true; |
| 906 | } |
| 907 | } |
| 908 | return false; |
| 909 | } |
| 910 | |
| 911 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 912 | bool FunctionTemplateInfo::IsTemplateFor(Object* object) { |
| 913 | if (!object->IsHeapObject()) return false; |
| 914 | return IsTemplateFor(HeapObject::cast(object)->map()); |
| 915 | } |
| 916 | |
| 917 | |
| 918 | bool FunctionTemplateInfo::IsTemplateFor(Map* map) { |
| 919 | // There is a constraint on the object; check. |
| 920 | if (!map->IsJSObjectMap()) return false; |
| 921 | // Fetch the constructor function of the object. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 922 | Object* cons_obj = map->GetConstructor(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 923 | if (!cons_obj->IsJSFunction()) return false; |
| 924 | JSFunction* fun = JSFunction::cast(cons_obj); |
| 925 | // Iterate through the chain of inheriting function templates to |
| 926 | // see if the required one occurs. |
| 927 | for (Object* type = fun->shared()->function_data(); |
| 928 | type->IsFunctionTemplateInfo(); |
| 929 | type = FunctionTemplateInfo::cast(type)->parent_template()) { |
| 930 | if (type == this) return true; |
| 931 | } |
| 932 | // Didn't find the required type in the inheritance chain. |
| 933 | return false; |
| 934 | } |
| 935 | |
| 936 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 937 | // TODO(dcarney): CallOptimization duplicates this logic, merge. |
| 938 | Object* FunctionTemplateInfo::GetCompatibleReceiver(Isolate* isolate, |
| 939 | Object* receiver) { |
| 940 | // API calls are only supported with JSObject receivers. |
| 941 | if (!receiver->IsJSObject()) return isolate->heap()->null_value(); |
| 942 | Object* recv_type = this->signature(); |
| 943 | // No signature, return holder. |
| 944 | if (recv_type->IsUndefined()) return receiver; |
| 945 | FunctionTemplateInfo* signature = FunctionTemplateInfo::cast(recv_type); |
| 946 | // Check the receiver. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 947 | for (PrototypeIterator iter(isolate, JSObject::cast(receiver), |
| 948 | PrototypeIterator::START_AT_RECEIVER, |
| 949 | PrototypeIterator::END_AT_NON_HIDDEN); |
| 950 | !iter.IsAtEnd(); iter.Advance()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 951 | if (signature->IsTemplateFor(iter.GetCurrent())) return iter.GetCurrent(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 952 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 953 | return isolate->heap()->null_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 957 | // static |
| 958 | MaybeHandle<JSObject> JSObject::New(Handle<JSFunction> constructor, |
| 959 | Handle<JSReceiver> new_target, |
| 960 | Handle<AllocationSite> site) { |
| 961 | // If called through new, new.target can be: |
| 962 | // - a subclass of constructor, |
| 963 | // - a proxy wrapper around constructor, or |
| 964 | // - the constructor itself. |
| 965 | // If called through Reflect.construct, it's guaranteed to be a constructor. |
| 966 | Isolate* const isolate = constructor->GetIsolate(); |
| 967 | DCHECK(constructor->IsConstructor()); |
| 968 | DCHECK(new_target->IsConstructor()); |
| 969 | DCHECK(!constructor->has_initial_map() || |
| 970 | constructor->initial_map()->instance_type() != JS_FUNCTION_TYPE); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 971 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 972 | Handle<Map> initial_map; |
| 973 | ASSIGN_RETURN_ON_EXCEPTION( |
| 974 | isolate, initial_map, |
| 975 | JSFunction::GetDerivedMap(isolate, constructor, new_target), JSObject); |
| 976 | Handle<JSObject> result = |
| 977 | isolate->factory()->NewJSObjectFromMap(initial_map, NOT_TENURED, site); |
| 978 | isolate->counters()->constructed_objects()->Increment(); |
| 979 | isolate->counters()->constructed_objects_runtime()->Increment(); |
| 980 | return result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 983 | void JSObject::EnsureWritableFastElements(Handle<JSObject> object) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 984 | DCHECK(object->HasFastSmiOrObjectElements() || |
| 985 | object->HasFastStringWrapperElements()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 986 | FixedArray* raw_elems = FixedArray::cast(object->elements()); |
| 987 | Heap* heap = object->GetHeap(); |
| 988 | if (raw_elems->map() != heap->fixed_cow_array_map()) return; |
| 989 | Isolate* isolate = heap->isolate(); |
| 990 | Handle<FixedArray> elems(raw_elems, isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 991 | Handle<FixedArray> writable_elems = isolate->factory()->CopyFixedArrayWithMap( |
| 992 | elems, isolate->factory()->fixed_array_map()); |
| 993 | object->set_elements(*writable_elems); |
| 994 | isolate->counters()->cow_arrays_converted()->Increment(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 998 | // ES6 9.5.1 |
| 999 | // static |
| 1000 | MaybeHandle<Object> JSProxy::GetPrototype(Handle<JSProxy> proxy) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1001 | Isolate* isolate = proxy->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1002 | Handle<String> trap_name = isolate->factory()->getPrototypeOf_string(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1003 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1004 | STACK_CHECK(MaybeHandle<Object>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1005 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1006 | // 1. Let handler be the value of the [[ProxyHandler]] internal slot. |
| 1007 | // 2. If handler is null, throw a TypeError exception. |
| 1008 | // 3. Assert: Type(handler) is Object. |
| 1009 | // 4. Let target be the value of the [[ProxyTarget]] internal slot. |
| 1010 | if (proxy->IsRevoked()) { |
| 1011 | THROW_NEW_ERROR(isolate, |
| 1012 | NewTypeError(MessageTemplate::kProxyRevoked, trap_name), |
| 1013 | Object); |
| 1014 | } |
| 1015 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 1016 | Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate); |
| 1017 | |
| 1018 | // 5. Let trap be ? GetMethod(handler, "getPrototypeOf"). |
| 1019 | Handle<Object> trap; |
| 1020 | ASSIGN_RETURN_ON_EXCEPTION(isolate, trap, GetMethod(handler, trap_name), |
| 1021 | Object); |
| 1022 | // 6. If trap is undefined, then return target.[[GetPrototypeOf]](). |
| 1023 | if (trap->IsUndefined()) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1024 | return JSReceiver::GetPrototype(isolate, target); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1025 | } |
| 1026 | // 7. Let handlerProto be ? Call(trap, handler, «target»). |
| 1027 | Handle<Object> argv[] = {target}; |
| 1028 | Handle<Object> handler_proto; |
| 1029 | ASSIGN_RETURN_ON_EXCEPTION( |
| 1030 | isolate, handler_proto, |
| 1031 | Execution::Call(isolate, trap, handler, arraysize(argv), argv), Object); |
| 1032 | // 8. If Type(handlerProto) is neither Object nor Null, throw a TypeError. |
| 1033 | if (!(handler_proto->IsJSReceiver() || handler_proto->IsNull())) { |
| 1034 | THROW_NEW_ERROR(isolate, |
| 1035 | NewTypeError(MessageTemplate::kProxyGetPrototypeOfInvalid), |
| 1036 | Object); |
| 1037 | } |
| 1038 | // 9. Let extensibleTarget be ? IsExtensible(target). |
| 1039 | Maybe<bool> is_extensible = JSReceiver::IsExtensible(target); |
| 1040 | MAYBE_RETURN_NULL(is_extensible); |
| 1041 | // 10. If extensibleTarget is true, return handlerProto. |
| 1042 | if (is_extensible.FromJust()) return handler_proto; |
| 1043 | // 11. Let targetProto be ? target.[[GetPrototypeOf]](). |
| 1044 | Handle<Object> target_proto; |
| 1045 | ASSIGN_RETURN_ON_EXCEPTION(isolate, target_proto, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1046 | JSReceiver::GetPrototype(isolate, target), Object); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1047 | // 12. If SameValue(handlerProto, targetProto) is false, throw a TypeError. |
| 1048 | if (!handler_proto->SameValue(*target_proto)) { |
| 1049 | THROW_NEW_ERROR( |
| 1050 | isolate, |
| 1051 | NewTypeError(MessageTemplate::kProxyGetPrototypeOfNonExtensible), |
| 1052 | Object); |
| 1053 | } |
| 1054 | // 13. Return handlerProto. |
| 1055 | return handler_proto; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1058 | MaybeHandle<Object> Object::GetPropertyWithAccessor(LookupIterator* it) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1059 | Isolate* isolate = it->isolate(); |
| 1060 | Handle<Object> structure = it->GetAccessors(); |
| 1061 | Handle<Object> receiver = it->GetReceiver(); |
| 1062 | |
| 1063 | // We should never get here to initialize a const with the hole value since a |
| 1064 | // const declaration would conflict with the getter. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1065 | DCHECK(!structure->IsForeign()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1066 | |
| 1067 | // API style callbacks. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1068 | if (structure->IsAccessorInfo()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1069 | Handle<JSObject> holder = it->GetHolder<JSObject>(); |
| 1070 | Handle<Name> name = it->GetName(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1071 | Handle<AccessorInfo> info = Handle<AccessorInfo>::cast(structure); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1072 | if (!info->IsCompatibleReceiver(*receiver)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1073 | THROW_NEW_ERROR(isolate, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1074 | NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, |
| 1075 | name, receiver), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1076 | Object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1077 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1078 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1079 | v8::AccessorNameGetterCallback call_fun = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1080 | v8::ToCData<v8::AccessorNameGetterCallback>(info->getter()); |
| 1081 | if (call_fun == nullptr) return isolate->factory()->undefined_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1082 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1083 | if (info->is_sloppy() && !receiver->IsJSReceiver()) { |
| 1084 | ASSIGN_RETURN_ON_EXCEPTION(isolate, receiver, |
| 1085 | Object::ConvertReceiver(isolate, receiver), |
| 1086 | Object); |
| 1087 | } |
| 1088 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1089 | PropertyCallbackArguments args(isolate, info->data(), *receiver, *holder, |
| 1090 | Object::DONT_THROW); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1091 | Handle<Object> result = args.Call(call_fun, name); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1092 | RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1093 | if (result.is_null()) return ReadAbsentProperty(isolate, receiver, name); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1094 | // Rebox handle before return. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1095 | return handle(*result, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1098 | // Regular accessor. |
| 1099 | Handle<Object> getter(AccessorPair::cast(*structure)->getter(), isolate); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1100 | if (getter->IsFunctionTemplateInfo()) { |
| 1101 | auto result = Builtins::InvokeApiFunction( |
| 1102 | Handle<FunctionTemplateInfo>::cast(getter), receiver, 0, nullptr); |
| 1103 | if (isolate->has_pending_exception()) { |
| 1104 | return MaybeHandle<Object>(); |
| 1105 | } |
| 1106 | Handle<Object> return_value; |
| 1107 | if (result.ToHandle(&return_value)) { |
| 1108 | return_value->VerifyApiCallResultType(); |
| 1109 | return handle(*return_value, isolate); |
| 1110 | } |
| 1111 | } else if (getter->IsCallable()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1112 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
| 1113 | return Object::GetPropertyWithDefinedGetter( |
| 1114 | receiver, Handle<JSReceiver>::cast(getter)); |
| 1115 | } |
| 1116 | // Getter is not a function. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1117 | return ReadAbsentProperty(isolate, receiver, it->GetName()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1121 | bool AccessorInfo::IsCompatibleReceiverMap(Isolate* isolate, |
| 1122 | Handle<AccessorInfo> info, |
| 1123 | Handle<Map> map) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1124 | if (!info->HasExpectedReceiverType()) return true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1125 | if (!map->IsJSObjectMap()) return false; |
| 1126 | return FunctionTemplateInfo::cast(info->expected_receiver_type()) |
| 1127 | ->IsTemplateFor(*map); |
| 1128 | } |
| 1129 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1130 | Maybe<bool> Object::SetPropertyWithAccessor(LookupIterator* it, |
| 1131 | Handle<Object> value, |
| 1132 | ShouldThrow should_throw) { |
| 1133 | Isolate* isolate = it->isolate(); |
| 1134 | Handle<Object> structure = it->GetAccessors(); |
| 1135 | Handle<Object> receiver = it->GetReceiver(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1136 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1137 | // We should never get here to initialize a const with the hole value since a |
| 1138 | // const declaration would conflict with the setter. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1139 | DCHECK(!structure->IsForeign()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1140 | |
| 1141 | // API style callbacks. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1142 | if (structure->IsAccessorInfo()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1143 | Handle<JSObject> holder = it->GetHolder<JSObject>(); |
| 1144 | Handle<Name> name = it->GetName(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1145 | Handle<AccessorInfo> info = Handle<AccessorInfo>::cast(structure); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1146 | if (!info->IsCompatibleReceiver(*receiver)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1147 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 1148 | MessageTemplate::kIncompatibleMethodReceiver, name, receiver)); |
| 1149 | return Nothing<bool>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1150 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1151 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1152 | v8::AccessorNameSetterCallback call_fun = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1153 | v8::ToCData<v8::AccessorNameSetterCallback>(info->setter()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1154 | // TODO(verwaest): We should not get here anymore once all AccessorInfos are |
| 1155 | // marked as special_data_property. They cannot both be writable and not |
| 1156 | // have a setter. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1157 | if (call_fun == nullptr) return Just(true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1158 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1159 | if (info->is_sloppy() && !receiver->IsJSReceiver()) { |
| 1160 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 1161 | isolate, receiver, Object::ConvertReceiver(isolate, receiver), |
| 1162 | Nothing<bool>()); |
| 1163 | } |
| 1164 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1165 | PropertyCallbackArguments args(isolate, info->data(), *receiver, *holder, |
| 1166 | should_throw); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1167 | args.Call(call_fun, name, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1168 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); |
| 1169 | return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1172 | // Regular accessor. |
| 1173 | Handle<Object> setter(AccessorPair::cast(*structure)->setter(), isolate); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1174 | if (setter->IsFunctionTemplateInfo()) { |
| 1175 | Handle<Object> argv[] = {value}; |
| 1176 | auto result = |
| 1177 | Builtins::InvokeApiFunction(Handle<FunctionTemplateInfo>::cast(setter), |
| 1178 | receiver, arraysize(argv), argv); |
| 1179 | if (isolate->has_pending_exception()) { |
| 1180 | return Nothing<bool>(); |
| 1181 | } |
| 1182 | return Just(true); |
| 1183 | } else if (setter->IsCallable()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1184 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
| 1185 | return SetPropertyWithDefinedSetter( |
| 1186 | receiver, Handle<JSReceiver>::cast(setter), value, should_throw); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1189 | RETURN_FAILURE(isolate, should_throw, |
| 1190 | NewTypeError(MessageTemplate::kNoSetterInCallback, |
| 1191 | it->GetName(), it->GetHolder<JSObject>())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1195 | MaybeHandle<Object> Object::GetPropertyWithDefinedGetter( |
| 1196 | Handle<Object> receiver, |
| 1197 | Handle<JSReceiver> getter) { |
| 1198 | Isolate* isolate = getter->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1199 | |
| 1200 | // Platforms with simulators like arm/arm64 expose a funny issue. If the |
| 1201 | // simulator has a separate JS stack pointer from the C++ stack pointer, it |
| 1202 | // can miss C++ stack overflows in the stack guard at the start of JavaScript |
| 1203 | // functions. It would be very expensive to check the C++ stack pointer at |
| 1204 | // that location. The best solution seems to be to break the impasse by |
| 1205 | // adding checks at possible recursion points. What's more, we don't put |
| 1206 | // this stack check behind the USE_SIMULATOR define in order to keep |
| 1207 | // behavior the same between hardware and simulators. |
| 1208 | StackLimitCheck check(isolate); |
| 1209 | if (check.JsHasOverflowed()) { |
| 1210 | isolate->StackOverflow(); |
| 1211 | return MaybeHandle<Object>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1212 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1213 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1214 | return Execution::Call(isolate, getter, receiver, 0, NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1218 | Maybe<bool> Object::SetPropertyWithDefinedSetter(Handle<Object> receiver, |
| 1219 | Handle<JSReceiver> setter, |
| 1220 | Handle<Object> value, |
| 1221 | ShouldThrow should_throw) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1222 | Isolate* isolate = setter->GetIsolate(); |
| 1223 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1224 | Handle<Object> argv[] = { value }; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1225 | RETURN_ON_EXCEPTION_VALUE(isolate, Execution::Call(isolate, setter, receiver, |
| 1226 | arraysize(argv), argv), |
| 1227 | Nothing<bool>()); |
| 1228 | return Just(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1232 | // static |
| 1233 | bool Object::IsErrorObject(Isolate* isolate, Handle<Object> object) { |
| 1234 | if (!object->IsJSObject()) return false; |
| 1235 | // Use stack_trace_symbol as proxy for [[ErrorData]]. |
| 1236 | Handle<Name> symbol = isolate->factory()->stack_trace_symbol(); |
| 1237 | Maybe<bool> has_stack_trace = |
| 1238 | JSReceiver::HasOwnProperty(Handle<JSReceiver>::cast(object), symbol); |
| 1239 | DCHECK(!has_stack_trace.IsNothing()); |
| 1240 | return has_stack_trace.FromJust(); |
| 1241 | } |
| 1242 | |
| 1243 | |
| 1244 | // static |
| 1245 | bool JSObject::AllCanRead(LookupIterator* it) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1246 | // Skip current iteration, it's in state ACCESS_CHECK or INTERCEPTOR, both of |
| 1247 | // which have already been checked. |
| 1248 | DCHECK(it->state() == LookupIterator::ACCESS_CHECK || |
| 1249 | it->state() == LookupIterator::INTERCEPTOR); |
| 1250 | for (it->Next(); it->IsFound(); it->Next()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1251 | if (it->state() == LookupIterator::ACCESSOR) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1252 | auto accessors = it->GetAccessors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1253 | if (accessors->IsAccessorInfo()) { |
| 1254 | if (AccessorInfo::cast(*accessors)->all_can_read()) return true; |
| 1255 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1256 | } else if (it->state() == LookupIterator::INTERCEPTOR) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1257 | if (it->GetInterceptor()->all_can_read()) return true; |
| 1258 | } else if (it->state() == LookupIterator::JSPROXY) { |
| 1259 | // Stop lookupiterating. And no, AllCanNotRead. |
| 1260 | return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | return false; |
| 1264 | } |
| 1265 | |
| 1266 | |
| 1267 | MaybeHandle<Object> JSObject::GetPropertyWithFailedAccessCheck( |
| 1268 | LookupIterator* it) { |
| 1269 | Handle<JSObject> checked = it->GetHolder<JSObject>(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1270 | while (AllCanRead(it)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1271 | if (it->state() == LookupIterator::ACCESSOR) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1272 | return GetPropertyWithAccessor(it); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1273 | } |
| 1274 | DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1275 | bool done; |
| 1276 | Handle<Object> result; |
| 1277 | ASSIGN_RETURN_ON_EXCEPTION(it->isolate(), result, |
| 1278 | GetPropertyWithInterceptor(it, &done), Object); |
| 1279 | if (done) return result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1280 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1281 | |
| 1282 | // Cross-Origin [[Get]] of Well-Known Symbols does not throw, and returns |
| 1283 | // undefined. |
| 1284 | Handle<Name> name = it->GetName(); |
| 1285 | if (name->IsSymbol() && Symbol::cast(*name)->is_well_known_symbol()) { |
| 1286 | return it->factory()->undefined_value(); |
| 1287 | } |
| 1288 | |
| 1289 | it->isolate()->ReportFailedAccessCheck(checked); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1290 | RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(it->isolate(), Object); |
| 1291 | return it->factory()->undefined_value(); |
| 1292 | } |
| 1293 | |
| 1294 | |
| 1295 | Maybe<PropertyAttributes> JSObject::GetPropertyAttributesWithFailedAccessCheck( |
| 1296 | LookupIterator* it) { |
| 1297 | Handle<JSObject> checked = it->GetHolder<JSObject>(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1298 | while (AllCanRead(it)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1299 | if (it->state() == LookupIterator::ACCESSOR) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1300 | return Just(it->property_attributes()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1301 | } |
| 1302 | DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1303 | auto result = GetPropertyAttributesWithInterceptor(it); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1304 | if (it->isolate()->has_scheduled_exception()) break; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1305 | if (result.IsJust() && result.FromJust() != ABSENT) return result; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1306 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1307 | it->isolate()->ReportFailedAccessCheck(checked); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1308 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(it->isolate(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1309 | Nothing<PropertyAttributes>()); |
| 1310 | return Just(ABSENT); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1314 | // static |
| 1315 | bool JSObject::AllCanWrite(LookupIterator* it) { |
| 1316 | for (; it->IsFound() && it->state() != LookupIterator::JSPROXY; it->Next()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1317 | if (it->state() == LookupIterator::ACCESSOR) { |
| 1318 | Handle<Object> accessors = it->GetAccessors(); |
| 1319 | if (accessors->IsAccessorInfo()) { |
| 1320 | if (AccessorInfo::cast(*accessors)->all_can_write()) return true; |
| 1321 | } |
| 1322 | } |
| 1323 | } |
| 1324 | return false; |
| 1325 | } |
| 1326 | |
| 1327 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1328 | Maybe<bool> JSObject::SetPropertyWithFailedAccessCheck( |
| 1329 | LookupIterator* it, Handle<Object> value, ShouldThrow should_throw) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1330 | Handle<JSObject> checked = it->GetHolder<JSObject>(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1331 | if (AllCanWrite(it)) { |
| 1332 | return SetPropertyWithAccessor(it, value, should_throw); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1335 | it->isolate()->ReportFailedAccessCheck(checked); |
| 1336 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(it->isolate(), Nothing<bool>()); |
| 1337 | return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | |
| 1341 | void JSObject::SetNormalizedProperty(Handle<JSObject> object, |
| 1342 | Handle<Name> name, |
| 1343 | Handle<Object> value, |
| 1344 | PropertyDetails details) { |
| 1345 | DCHECK(!object->HasFastProperties()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1346 | if (!name->IsUniqueName()) { |
| 1347 | name = object->GetIsolate()->factory()->InternalizeString( |
| 1348 | Handle<String>::cast(name)); |
| 1349 | } |
| 1350 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1351 | if (object->IsJSGlobalObject()) { |
| 1352 | Handle<GlobalDictionary> property_dictionary(object->global_dictionary()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1353 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1354 | int entry = property_dictionary->FindEntry(name); |
| 1355 | if (entry == GlobalDictionary::kNotFound) { |
| 1356 | auto cell = object->GetIsolate()->factory()->NewPropertyCell(); |
| 1357 | cell->set_value(*value); |
| 1358 | auto cell_type = value->IsUndefined() ? PropertyCellType::kUndefined |
| 1359 | : PropertyCellType::kConstant; |
| 1360 | details = details.set_cell_type(cell_type); |
| 1361 | value = cell; |
| 1362 | property_dictionary = |
| 1363 | GlobalDictionary::Add(property_dictionary, name, value, details); |
| 1364 | object->set_properties(*property_dictionary); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1365 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1366 | PropertyCell::UpdateCell(property_dictionary, entry, value, details); |
| 1367 | } |
| 1368 | } else { |
| 1369 | Handle<NameDictionary> property_dictionary(object->property_dictionary()); |
| 1370 | |
| 1371 | int entry = property_dictionary->FindEntry(name); |
| 1372 | if (entry == NameDictionary::kNotFound) { |
| 1373 | property_dictionary = |
| 1374 | NameDictionary::Add(property_dictionary, name, value, details); |
| 1375 | object->set_properties(*property_dictionary); |
| 1376 | } else { |
| 1377 | PropertyDetails original_details = property_dictionary->DetailsAt(entry); |
| 1378 | int enumeration_index = original_details.dictionary_index(); |
| 1379 | DCHECK(enumeration_index > 0); |
| 1380 | details = details.set_index(enumeration_index); |
| 1381 | property_dictionary->SetEntry(entry, name, value, details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1382 | } |
| 1383 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1386 | Maybe<bool> JSReceiver::HasInPrototypeChain(Isolate* isolate, |
| 1387 | Handle<JSReceiver> object, |
| 1388 | Handle<Object> proto) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1389 | PrototypeIterator iter(isolate, object, PrototypeIterator::START_AT_RECEIVER); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1390 | while (true) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1391 | if (!iter.AdvanceFollowingProxies()) return Nothing<bool>(); |
| 1392 | if (iter.IsAtEnd()) return Just(false); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1393 | if (PrototypeIterator::GetCurrent(iter).is_identical_to(proto)) { |
| 1394 | return Just(true); |
| 1395 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1396 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1400 | Map* Object::GetRootMap(Isolate* isolate) { |
| 1401 | DisallowHeapAllocation no_alloc; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1402 | if (IsSmi()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1403 | Context* native_context = isolate->context()->native_context(); |
| 1404 | return native_context->number_function()->initial_map(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1405 | } |
| 1406 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1407 | // 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] | 1408 | // a real JS object, or a Harmony proxy. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1409 | HeapObject* heap_object = HeapObject::cast(this); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1410 | if (heap_object->IsJSReceiver()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1411 | return heap_object->map(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1412 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1413 | int constructor_function_index = |
| 1414 | heap_object->map()->GetConstructorFunctionIndex(); |
| 1415 | if (constructor_function_index != Map::kNoConstructorFunctionIndex) { |
| 1416 | Context* native_context = isolate->context()->native_context(); |
| 1417 | JSFunction* constructor_function = |
| 1418 | JSFunction::cast(native_context->get(constructor_function_index)); |
| 1419 | return constructor_function->initial_map(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1420 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1421 | return isolate->heap()->null_value()->map(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1425 | Object* Object::GetHash() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1426 | Object* hash = GetSimpleHash(); |
| 1427 | if (hash->IsSmi()) return hash; |
| 1428 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1429 | DisallowHeapAllocation no_gc; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1430 | DCHECK(IsJSReceiver()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1431 | JSReceiver* receiver = JSReceiver::cast(this); |
| 1432 | Isolate* isolate = receiver->GetIsolate(); |
| 1433 | return *JSReceiver::GetIdentityHash(isolate, handle(receiver, isolate)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | |
| 1437 | Object* Object::GetSimpleHash() { |
| 1438 | // The object is either a Smi, a HeapNumber, a name, an odd-ball, |
| 1439 | // a SIMD value type, a real JS object, or a Harmony proxy. |
| 1440 | if (IsSmi()) { |
| 1441 | uint32_t hash = ComputeIntegerHash(Smi::cast(this)->value(), kZeroHashSeed); |
| 1442 | return Smi::FromInt(hash & Smi::kMaxValue); |
| 1443 | } |
| 1444 | if (IsHeapNumber()) { |
| 1445 | double num = HeapNumber::cast(this)->value(); |
| 1446 | if (std::isnan(num)) return Smi::FromInt(Smi::kMaxValue); |
| 1447 | if (i::IsMinusZero(num)) num = 0; |
| 1448 | if (IsSmiDouble(num)) { |
| 1449 | return Smi::FromInt(FastD2I(num))->GetHash(); |
| 1450 | } |
| 1451 | uint32_t hash = ComputeLongHash(double_to_uint64(num)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1452 | return Smi::FromInt(hash & Smi::kMaxValue); |
| 1453 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1454 | if (IsName()) { |
| 1455 | uint32_t hash = Name::cast(this)->Hash(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1456 | return Smi::FromInt(hash); |
| 1457 | } |
| 1458 | if (IsOddball()) { |
| 1459 | uint32_t hash = Oddball::cast(this)->to_string()->Hash(); |
| 1460 | return Smi::FromInt(hash); |
| 1461 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1462 | if (IsSimd128Value()) { |
| 1463 | uint32_t hash = Simd128Value::cast(this)->Hash(); |
| 1464 | return Smi::FromInt(hash & Smi::kMaxValue); |
| 1465 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1466 | DCHECK(IsJSReceiver()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1467 | JSReceiver* receiver = JSReceiver::cast(this); |
| 1468 | return receiver->GetHeap()->undefined_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | |
| 1472 | Handle<Smi> Object::GetOrCreateHash(Isolate* isolate, Handle<Object> object) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1473 | Handle<Object> hash(object->GetSimpleHash(), isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1474 | if (hash->IsSmi()) return Handle<Smi>::cast(hash); |
| 1475 | |
| 1476 | DCHECK(object->IsJSReceiver()); |
| 1477 | return JSReceiver::GetOrCreateIdentityHash(Handle<JSReceiver>::cast(object)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | |
| 1481 | bool Object::SameValue(Object* other) { |
| 1482 | if (other == this) return true; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1483 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1484 | // The object is either a number, a name, an odd-ball, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1485 | // a real JS object, or a Harmony proxy. |
| 1486 | if (IsNumber() && other->IsNumber()) { |
| 1487 | double this_value = Number(); |
| 1488 | double other_value = other->Number(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1489 | // SameValue(NaN, NaN) is true. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1490 | if (this_value != other_value) { |
| 1491 | return std::isnan(this_value) && std::isnan(other_value); |
| 1492 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1493 | // SameValue(0.0, -0.0) is false. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1494 | return (std::signbit(this_value) == std::signbit(other_value)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1495 | } |
| 1496 | if (IsString() && other->IsString()) { |
| 1497 | return String::cast(this)->Equals(String::cast(other)); |
| 1498 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1499 | if (IsFloat32x4() && other->IsFloat32x4()) { |
| 1500 | Float32x4* a = Float32x4::cast(this); |
| 1501 | Float32x4* b = Float32x4::cast(other); |
| 1502 | for (int i = 0; i < 4; i++) { |
| 1503 | float x = a->get_lane(i); |
| 1504 | float y = b->get_lane(i); |
| 1505 | // Implements the ES5 SameValue operation for floating point types. |
| 1506 | // http://www.ecma-international.org/ecma-262/6.0/#sec-samevalue |
| 1507 | if (x != y && !(std::isnan(x) && std::isnan(y))) return false; |
| 1508 | if (std::signbit(x) != std::signbit(y)) return false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1509 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1510 | return true; |
| 1511 | } else if (IsSimd128Value() && other->IsSimd128Value()) { |
| 1512 | Simd128Value* a = Simd128Value::cast(this); |
| 1513 | Simd128Value* b = Simd128Value::cast(other); |
| 1514 | return a->map() == b->map() && a->BitwiseEquals(b); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1515 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1516 | return false; |
| 1517 | } |
| 1518 | |
| 1519 | |
| 1520 | bool Object::SameValueZero(Object* other) { |
| 1521 | if (other == this) return true; |
| 1522 | |
| 1523 | // The object is either a number, a name, an odd-ball, |
| 1524 | // a real JS object, or a Harmony proxy. |
| 1525 | if (IsNumber() && other->IsNumber()) { |
| 1526 | double this_value = Number(); |
| 1527 | double other_value = other->Number(); |
| 1528 | // +0 == -0 is true |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1529 | return this_value == other_value || |
| 1530 | (std::isnan(this_value) && std::isnan(other_value)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1531 | } |
| 1532 | if (IsString() && other->IsString()) { |
| 1533 | return String::cast(this)->Equals(String::cast(other)); |
| 1534 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1535 | if (IsFloat32x4() && other->IsFloat32x4()) { |
| 1536 | Float32x4* a = Float32x4::cast(this); |
| 1537 | Float32x4* b = Float32x4::cast(other); |
| 1538 | for (int i = 0; i < 4; i++) { |
| 1539 | float x = a->get_lane(i); |
| 1540 | float y = b->get_lane(i); |
| 1541 | // Implements the ES6 SameValueZero operation for floating point types. |
| 1542 | // http://www.ecma-international.org/ecma-262/6.0/#sec-samevaluezero |
| 1543 | if (x != y && !(std::isnan(x) && std::isnan(y))) return false; |
| 1544 | // SameValueZero doesn't distinguish between 0 and -0. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1545 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1546 | return true; |
| 1547 | } else if (IsSimd128Value() && other->IsSimd128Value()) { |
| 1548 | Simd128Value* a = Simd128Value::cast(this); |
| 1549 | Simd128Value* b = Simd128Value::cast(other); |
| 1550 | return a->map() == b->map() && a->BitwiseEquals(b); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1551 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1552 | return false; |
| 1553 | } |
| 1554 | |
| 1555 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1556 | MaybeHandle<Object> Object::ArraySpeciesConstructor( |
| 1557 | Isolate* isolate, Handle<Object> original_array) { |
| 1558 | Handle<Context> native_context = isolate->native_context(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1559 | Handle<Object> default_species = isolate->array_function(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1560 | if (!FLAG_harmony_species) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1561 | return default_species; |
| 1562 | } |
| 1563 | if (original_array->IsJSArray() && |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1564 | Handle<JSArray>::cast(original_array)->HasArrayPrototype(isolate) && |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1565 | isolate->IsArraySpeciesLookupChainIntact()) { |
| 1566 | return default_species; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1567 | } |
| 1568 | Handle<Object> constructor = isolate->factory()->undefined_value(); |
| 1569 | Maybe<bool> is_array = Object::IsArray(original_array); |
| 1570 | MAYBE_RETURN_NULL(is_array); |
| 1571 | if (is_array.FromJust()) { |
| 1572 | ASSIGN_RETURN_ON_EXCEPTION( |
| 1573 | isolate, constructor, |
| 1574 | Object::GetProperty(original_array, |
| 1575 | isolate->factory()->constructor_string()), |
| 1576 | Object); |
| 1577 | if (constructor->IsConstructor()) { |
| 1578 | Handle<Context> constructor_context; |
| 1579 | ASSIGN_RETURN_ON_EXCEPTION( |
| 1580 | isolate, constructor_context, |
| 1581 | JSReceiver::GetFunctionRealm(Handle<JSReceiver>::cast(constructor)), |
| 1582 | Object); |
| 1583 | if (*constructor_context != *native_context && |
| 1584 | *constructor == constructor_context->array_function()) { |
| 1585 | constructor = isolate->factory()->undefined_value(); |
| 1586 | } |
| 1587 | } |
| 1588 | if (constructor->IsJSReceiver()) { |
| 1589 | ASSIGN_RETURN_ON_EXCEPTION( |
| 1590 | isolate, constructor, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1591 | JSReceiver::GetProperty(Handle<JSReceiver>::cast(constructor), |
| 1592 | isolate->factory()->species_symbol()), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1593 | Object); |
| 1594 | if (constructor->IsNull()) { |
| 1595 | constructor = isolate->factory()->undefined_value(); |
| 1596 | } |
| 1597 | } |
| 1598 | } |
| 1599 | if (constructor->IsUndefined()) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1600 | return default_species; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1601 | } else { |
| 1602 | if (!constructor->IsConstructor()) { |
| 1603 | THROW_NEW_ERROR(isolate, |
| 1604 | NewTypeError(MessageTemplate::kSpeciesNotConstructor), |
| 1605 | Object); |
| 1606 | } |
| 1607 | return constructor; |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1612 | void Object::ShortPrint(FILE* out) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1613 | OFStream os(out); |
| 1614 | os << Brief(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | |
| 1618 | void Object::ShortPrint(StringStream* accumulator) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1619 | std::ostringstream os; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1620 | os << Brief(this); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1621 | accumulator->Add(os.str().c_str()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1625 | void Object::ShortPrint(std::ostream& os) { os << Brief(this); } |
| 1626 | |
| 1627 | |
| 1628 | std::ostream& operator<<(std::ostream& os, const Brief& v) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1629 | if (v.value->IsSmi()) { |
| 1630 | Smi::cast(v.value)->SmiPrint(os); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1631 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1632 | // TODO(svenpanne) Const-correct HeapObjectShortPrint! |
| 1633 | HeapObject* obj = const_cast<HeapObject*>(HeapObject::cast(v.value)); |
| 1634 | obj->HeapObjectShortPrint(os); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1635 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1636 | return os; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1640 | void Smi::SmiPrint(std::ostream& os) const { // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1641 | os << value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1645 | // Should a word be prefixed by 'a' or 'an' in order to read naturally in |
| 1646 | // English? Returns false for non-ASCII or words that don't start with |
| 1647 | // a capital letter. The a/an rule follows pronunciation in English. |
| 1648 | // We don't use the BBC's overcorrect "an historic occasion" though if |
| 1649 | // you speak a dialect you may well say "an 'istoric occasion". |
| 1650 | static bool AnWord(String* str) { |
| 1651 | if (str->length() == 0) return false; // A nothing. |
| 1652 | int c0 = str->Get(0); |
| 1653 | int c1 = str->length() > 1 ? str->Get(1) : 0; |
| 1654 | if (c0 == 'U') { |
| 1655 | if (c1 > 'Z') { |
| 1656 | return true; // An Umpire, but a UTF8String, a U. |
| 1657 | } |
| 1658 | } else if (c0 == 'A' || c0 == 'E' || c0 == 'I' || c0 == 'O') { |
| 1659 | return true; // An Ape, an ABCBook. |
| 1660 | } else if ((c1 == 0 || (c1 >= 'A' && c1 <= 'Z')) && |
| 1661 | (c0 == 'F' || c0 == 'H' || c0 == 'M' || c0 == 'N' || c0 == 'R' || |
| 1662 | c0 == 'S' || c0 == 'X')) { |
| 1663 | return true; // An MP3File, an M. |
| 1664 | } |
| 1665 | return false; |
| 1666 | } |
| 1667 | |
| 1668 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1669 | Handle<String> String::SlowFlatten(Handle<ConsString> cons, |
| 1670 | PretenureFlag pretenure) { |
| 1671 | DCHECK(AllowHeapAllocation::IsAllowed()); |
| 1672 | DCHECK(cons->second()->length() != 0); |
| 1673 | Isolate* isolate = cons->GetIsolate(); |
| 1674 | int length = cons->length(); |
| 1675 | PretenureFlag tenure = isolate->heap()->InNewSpace(*cons) ? pretenure |
| 1676 | : TENURED; |
| 1677 | Handle<SeqString> result; |
| 1678 | if (cons->IsOneByteRepresentation()) { |
| 1679 | Handle<SeqOneByteString> flat = isolate->factory()->NewRawOneByteString( |
| 1680 | length, tenure).ToHandleChecked(); |
| 1681 | DisallowHeapAllocation no_gc; |
| 1682 | WriteToFlat(*cons, flat->GetChars(), 0, length); |
| 1683 | result = flat; |
| 1684 | } else { |
| 1685 | Handle<SeqTwoByteString> flat = isolate->factory()->NewRawTwoByteString( |
| 1686 | length, tenure).ToHandleChecked(); |
| 1687 | DisallowHeapAllocation no_gc; |
| 1688 | WriteToFlat(*cons, flat->GetChars(), 0, length); |
| 1689 | result = flat; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1690 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1691 | cons->set_first(*result); |
| 1692 | cons->set_second(isolate->heap()->empty_string()); |
| 1693 | DCHECK(result->IsFlat()); |
| 1694 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1698 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1699 | bool String::MakeExternal(v8::String::ExternalStringResource* resource) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1700 | // Externalizing twice leaks the external resource, so it's |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1701 | // prohibited by the API. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1702 | DCHECK(!this->IsExternalString()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1703 | DCHECK(!resource->IsCompressible()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1704 | #ifdef ENABLE_SLOW_DCHECKS |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1705 | if (FLAG_enable_slow_asserts) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1706 | // Assert that the resource and the string are equivalent. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1707 | DCHECK(static_cast<size_t>(this->length()) == resource->length()); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1708 | ScopedVector<uc16> smart_chars(this->length()); |
| 1709 | String::WriteToFlat(this, smart_chars.start(), 0, this->length()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1710 | DCHECK(memcmp(smart_chars.start(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1711 | resource->data(), |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1712 | resource->length() * sizeof(smart_chars[0])) == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1713 | } |
| 1714 | #endif // DEBUG |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1715 | int size = this->Size(); // Byte size of the original string. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1716 | // Abort if size does not allow in-place conversion. |
| 1717 | if (size < ExternalString::kShortSize) return false; |
| 1718 | Heap* heap = GetHeap(); |
| 1719 | bool is_one_byte = this->IsOneByteRepresentation(); |
| 1720 | bool is_internalized = this->IsInternalizedString(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1721 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1722 | // Morph the string to an external string by replacing the map and |
| 1723 | // reinitializing the fields. This won't work if the space the existing |
| 1724 | // string occupies is too small for a regular external string. |
| 1725 | // Instead, we resort to a short external string instead, omitting |
| 1726 | // the field caching the address of the backing store. When we encounter |
| 1727 | // short external strings in generated code, we need to bailout to runtime. |
| 1728 | Map* new_map; |
| 1729 | if (size < ExternalString::kSize) { |
| 1730 | new_map = is_internalized |
| 1731 | ? (is_one_byte |
| 1732 | ? heap->short_external_internalized_string_with_one_byte_data_map() |
| 1733 | : heap->short_external_internalized_string_map()) |
| 1734 | : (is_one_byte ? heap->short_external_string_with_one_byte_data_map() |
| 1735 | : heap->short_external_string_map()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1736 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1737 | new_map = is_internalized |
| 1738 | ? (is_one_byte |
| 1739 | ? heap->external_internalized_string_with_one_byte_data_map() |
| 1740 | : heap->external_internalized_string_map()) |
| 1741 | : (is_one_byte ? heap->external_string_with_one_byte_data_map() |
| 1742 | : heap->external_string_map()); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 1743 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1744 | |
| 1745 | // Byte size of the external String object. |
| 1746 | int new_size = this->SizeFromMap(new_map); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1747 | heap->CreateFillerObjectAt(this->address() + new_size, size - new_size, |
| 1748 | ClearRecordedSlots::kNo); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1749 | |
| 1750 | // We are storing the new map using release store after creating a filler for |
| 1751 | // the left-over space to avoid races with the sweeper thread. |
| 1752 | this->synchronized_set_map(new_map); |
| 1753 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1754 | ExternalTwoByteString* self = ExternalTwoByteString::cast(this); |
| 1755 | self->set_resource(resource); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1756 | if (is_internalized) self->Hash(); // Force regeneration of the hash value. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1757 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1758 | heap->AdjustLiveBytes(this, new_size - size, Heap::CONCURRENT_TO_SWEEPER); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1759 | return true; |
| 1760 | } |
| 1761 | |
| 1762 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1763 | bool String::MakeExternal(v8::String::ExternalOneByteStringResource* resource) { |
| 1764 | // Externalizing twice leaks the external resource, so it's |
| 1765 | // prohibited by the API. |
| 1766 | DCHECK(!this->IsExternalString()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1767 | DCHECK(!resource->IsCompressible()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1768 | #ifdef ENABLE_SLOW_DCHECKS |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1769 | if (FLAG_enable_slow_asserts) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1770 | // Assert that the resource and the string are equivalent. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1771 | DCHECK(static_cast<size_t>(this->length()) == resource->length()); |
| 1772 | if (this->IsTwoByteRepresentation()) { |
| 1773 | ScopedVector<uint16_t> smart_chars(this->length()); |
| 1774 | String::WriteToFlat(this, smart_chars.start(), 0, this->length()); |
| 1775 | DCHECK(String::IsOneByte(smart_chars.start(), this->length())); |
| 1776 | } |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1777 | ScopedVector<char> smart_chars(this->length()); |
| 1778 | String::WriteToFlat(this, smart_chars.start(), 0, this->length()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1779 | DCHECK(memcmp(smart_chars.start(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1780 | resource->data(), |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1781 | resource->length() * sizeof(smart_chars[0])) == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1782 | } |
| 1783 | #endif // DEBUG |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1784 | int size = this->Size(); // Byte size of the original string. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1785 | // Abort if size does not allow in-place conversion. |
| 1786 | if (size < ExternalString::kShortSize) return false; |
| 1787 | Heap* heap = GetHeap(); |
| 1788 | bool is_internalized = this->IsInternalizedString(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1789 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1790 | // Morph the string to an external string by replacing the map and |
| 1791 | // reinitializing the fields. This won't work if the space the existing |
| 1792 | // string occupies is too small for a regular external string. |
| 1793 | // Instead, we resort to a short external string instead, omitting |
| 1794 | // the field caching the address of the backing store. When we encounter |
| 1795 | // short external strings in generated code, we need to bailout to runtime. |
| 1796 | Map* new_map; |
| 1797 | if (size < ExternalString::kSize) { |
| 1798 | new_map = is_internalized |
| 1799 | ? heap->short_external_one_byte_internalized_string_map() |
| 1800 | : heap->short_external_one_byte_string_map(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1801 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1802 | new_map = is_internalized |
| 1803 | ? heap->external_one_byte_internalized_string_map() |
| 1804 | : heap->external_one_byte_string_map(); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 1805 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1806 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1807 | // Byte size of the external String object. |
| 1808 | int new_size = this->SizeFromMap(new_map); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1809 | heap->CreateFillerObjectAt(this->address() + new_size, size - new_size, |
| 1810 | ClearRecordedSlots::kNo); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1811 | |
| 1812 | // We are storing the new map using release store after creating a filler for |
| 1813 | // the left-over space to avoid races with the sweeper thread. |
| 1814 | this->synchronized_set_map(new_map); |
| 1815 | |
| 1816 | ExternalOneByteString* self = ExternalOneByteString::cast(this); |
| 1817 | self->set_resource(resource); |
| 1818 | if (is_internalized) self->Hash(); // Force regeneration of the hash value. |
| 1819 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1820 | heap->AdjustLiveBytes(this, new_size - size, Heap::CONCURRENT_TO_SWEEPER); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1821 | return true; |
| 1822 | } |
| 1823 | |
| 1824 | |
| 1825 | void String::StringShortPrint(StringStream* accumulator) { |
| 1826 | int len = length(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1827 | if (len > kMaxShortPrintLength) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1828 | accumulator->Add("<Very long string[%u]>", len); |
| 1829 | return; |
| 1830 | } |
| 1831 | |
| 1832 | if (!LooksValid()) { |
| 1833 | accumulator->Add("<Invalid String>"); |
| 1834 | return; |
| 1835 | } |
| 1836 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1837 | StringCharacterStream stream(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1838 | |
| 1839 | bool truncated = false; |
| 1840 | if (len > kMaxShortPrintLength) { |
| 1841 | len = kMaxShortPrintLength; |
| 1842 | truncated = true; |
| 1843 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1844 | bool one_byte = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1845 | for (int i = 0; i < len; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1846 | uint16_t c = stream.GetNext(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1847 | |
| 1848 | if (c < 32 || c >= 127) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1849 | one_byte = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1850 | } |
| 1851 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1852 | stream.Reset(this); |
| 1853 | if (one_byte) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1854 | accumulator->Add("<String[%u]: ", length()); |
| 1855 | for (int i = 0; i < len; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1856 | accumulator->Put(static_cast<char>(stream.GetNext())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1857 | } |
| 1858 | accumulator->Put('>'); |
| 1859 | } else { |
| 1860 | // Backslash indicates that the string contains control |
| 1861 | // characters and that backslashes are therefore escaped. |
| 1862 | accumulator->Add("<String[%u]\\: ", length()); |
| 1863 | for (int i = 0; i < len; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1864 | uint16_t c = stream.GetNext(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1865 | if (c == '\n') { |
| 1866 | accumulator->Add("\\n"); |
| 1867 | } else if (c == '\r') { |
| 1868 | accumulator->Add("\\r"); |
| 1869 | } else if (c == '\\') { |
| 1870 | accumulator->Add("\\\\"); |
| 1871 | } else if (c < 32 || c > 126) { |
| 1872 | accumulator->Add("\\x%02x", c); |
| 1873 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1874 | accumulator->Put(static_cast<char>(c)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1875 | } |
| 1876 | } |
| 1877 | if (truncated) { |
| 1878 | accumulator->Put('.'); |
| 1879 | accumulator->Put('.'); |
| 1880 | accumulator->Put('.'); |
| 1881 | } |
| 1882 | accumulator->Put('>'); |
| 1883 | } |
| 1884 | return; |
| 1885 | } |
| 1886 | |
| 1887 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1888 | void String::PrintUC16(std::ostream& os, int start, int end) { // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1889 | if (end < 0) end = length(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1890 | StringCharacterStream stream(this, start); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1891 | for (int i = start; i < end && stream.HasMore(); i++) { |
| 1892 | os << AsUC16(stream.GetNext()); |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1897 | void JSObject::JSObjectShortPrint(StringStream* accumulator) { |
| 1898 | switch (map()->instance_type()) { |
| 1899 | case JS_ARRAY_TYPE: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1900 | double length = JSArray::cast(this)->length()->IsUndefined() |
| 1901 | ? 0 |
| 1902 | : JSArray::cast(this)->length()->Number(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1903 | accumulator->Add("<JS Array[%u]>", static_cast<uint32_t>(length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1904 | break; |
| 1905 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1906 | case JS_BOUND_FUNCTION_TYPE: { |
| 1907 | JSBoundFunction* bound_function = JSBoundFunction::cast(this); |
| 1908 | Object* name = bound_function->name(); |
| 1909 | accumulator->Add("<JS BoundFunction"); |
| 1910 | if (name->IsString()) { |
| 1911 | String* str = String::cast(name); |
| 1912 | if (str->length() > 0) { |
| 1913 | accumulator->Add(" "); |
| 1914 | accumulator->Put(str); |
| 1915 | } |
| 1916 | } |
| 1917 | accumulator->Add( |
| 1918 | " (BoundTargetFunction %p)>", |
| 1919 | reinterpret_cast<void*>(bound_function->bound_target_function())); |
| 1920 | break; |
| 1921 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1922 | case JS_WEAK_MAP_TYPE: { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1923 | accumulator->Add("<JS WeakMap>"); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1924 | break; |
| 1925 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1926 | case JS_WEAK_SET_TYPE: { |
| 1927 | accumulator->Add("<JS WeakSet>"); |
| 1928 | break; |
| 1929 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1930 | case JS_REGEXP_TYPE: { |
| 1931 | accumulator->Add("<JS RegExp>"); |
| 1932 | break; |
| 1933 | } |
| 1934 | case JS_FUNCTION_TYPE: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1935 | JSFunction* function = JSFunction::cast(this); |
| 1936 | Object* fun_name = function->shared()->DebugName(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1937 | bool printed = false; |
| 1938 | if (fun_name->IsString()) { |
| 1939 | String* str = String::cast(fun_name); |
| 1940 | if (str->length() > 0) { |
| 1941 | accumulator->Add("<JS Function "); |
| 1942 | accumulator->Put(str); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1943 | printed = true; |
| 1944 | } |
| 1945 | } |
| 1946 | if (!printed) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1947 | accumulator->Add("<JS Function"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1948 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1949 | accumulator->Add(" (SharedFunctionInfo %p)", |
| 1950 | reinterpret_cast<void*>(function->shared())); |
| 1951 | accumulator->Put('>'); |
| 1952 | break; |
| 1953 | } |
| 1954 | case JS_GENERATOR_OBJECT_TYPE: { |
| 1955 | accumulator->Add("<JS Generator>"); |
| 1956 | break; |
| 1957 | } |
| 1958 | case JS_MODULE_TYPE: { |
| 1959 | accumulator->Add("<JS Module>"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1960 | break; |
| 1961 | } |
| 1962 | // All other JSObjects are rather similar to each other (JSObject, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1963 | // JSGlobalProxy, JSGlobalObject, JSUndetectable, JSValue). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1964 | default: { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1965 | Map* map_of_this = map(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1966 | Heap* heap = GetHeap(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1967 | Object* constructor = map_of_this->GetConstructor(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1968 | bool printed = false; |
| 1969 | if (constructor->IsHeapObject() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1970 | !heap->Contains(HeapObject::cast(constructor))) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1971 | accumulator->Add("!!!INVALID CONSTRUCTOR!!!"); |
| 1972 | } else { |
| 1973 | bool global_object = IsJSGlobalProxy(); |
| 1974 | if (constructor->IsJSFunction()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1975 | if (!heap->Contains(JSFunction::cast(constructor)->shared())) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1976 | accumulator->Add("!!!INVALID SHARED ON CONSTRUCTOR!!!"); |
| 1977 | } else { |
| 1978 | Object* constructor_name = |
| 1979 | JSFunction::cast(constructor)->shared()->name(); |
| 1980 | if (constructor_name->IsString()) { |
| 1981 | String* str = String::cast(constructor_name); |
| 1982 | if (str->length() > 0) { |
| 1983 | bool vowel = AnWord(str); |
| 1984 | accumulator->Add("<%sa%s ", |
| 1985 | global_object ? "Global Object: " : "", |
| 1986 | vowel ? "n" : ""); |
| 1987 | accumulator->Put(str); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1988 | accumulator->Add(" with %smap %p", |
| 1989 | map_of_this->is_deprecated() ? "deprecated " : "", |
| 1990 | map_of_this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1991 | printed = true; |
| 1992 | } |
| 1993 | } |
| 1994 | } |
| 1995 | } |
| 1996 | if (!printed) { |
| 1997 | accumulator->Add("<JS %sObject", global_object ? "Global " : ""); |
| 1998 | } |
| 1999 | } |
| 2000 | if (IsJSValue()) { |
| 2001 | accumulator->Add(" value = "); |
| 2002 | JSValue::cast(this)->value()->ShortPrint(accumulator); |
| 2003 | } |
| 2004 | accumulator->Put('>'); |
| 2005 | break; |
| 2006 | } |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2011 | void JSObject::PrintElementsTransition( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2012 | FILE* file, Handle<JSObject> object, |
| 2013 | ElementsKind from_kind, Handle<FixedArrayBase> from_elements, |
| 2014 | ElementsKind to_kind, Handle<FixedArrayBase> to_elements) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2015 | if (from_kind != to_kind) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2016 | OFStream os(file); |
| 2017 | os << "elements transition [" << ElementsKindToString(from_kind) << " -> " |
| 2018 | << ElementsKindToString(to_kind) << "] in "; |
| 2019 | JavaScriptFrame::PrintTop(object->GetIsolate(), file, false, true); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2020 | PrintF(file, " for "); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2021 | object->ShortPrint(file); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2022 | PrintF(file, " from "); |
| 2023 | from_elements->ShortPrint(file); |
| 2024 | PrintF(file, " to "); |
| 2025 | to_elements->ShortPrint(file); |
| 2026 | PrintF(file, "\n"); |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2031 | // static |
| 2032 | MaybeHandle<JSFunction> Map::GetConstructorFunction( |
| 2033 | Handle<Map> map, Handle<Context> native_context) { |
| 2034 | if (map->IsPrimitiveMap()) { |
| 2035 | int const constructor_function_index = map->GetConstructorFunctionIndex(); |
| 2036 | if (constructor_function_index != kNoConstructorFunctionIndex) { |
| 2037 | return handle( |
| 2038 | JSFunction::cast(native_context->get(constructor_function_index))); |
| 2039 | } |
| 2040 | } |
| 2041 | return MaybeHandle<JSFunction>(); |
| 2042 | } |
| 2043 | |
| 2044 | |
| 2045 | void Map::PrintReconfiguration(FILE* file, int modify_index, PropertyKind kind, |
| 2046 | PropertyAttributes attributes) { |
| 2047 | OFStream os(file); |
| 2048 | os << "[reconfiguring]"; |
| 2049 | Name* name = instance_descriptors()->GetKey(modify_index); |
| 2050 | if (name->IsString()) { |
| 2051 | String::cast(name)->PrintOn(file); |
| 2052 | } else { |
| 2053 | os << "{symbol " << static_cast<void*>(name) << "}"; |
| 2054 | } |
| 2055 | os << ": " << (kind == kData ? "kData" : "ACCESSORS") << ", attrs: "; |
| 2056 | os << attributes << " ["; |
| 2057 | JavaScriptFrame::PrintTop(GetIsolate(), file, false, true); |
| 2058 | os << "]\n"; |
| 2059 | } |
| 2060 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2061 | void Map::PrintGeneralization( |
| 2062 | FILE* file, const char* reason, int modify_index, int split, |
| 2063 | int descriptors, bool constant_to_field, Representation old_representation, |
| 2064 | Representation new_representation, MaybeHandle<FieldType> old_field_type, |
| 2065 | MaybeHandle<Object> old_value, MaybeHandle<FieldType> new_field_type, |
| 2066 | MaybeHandle<Object> new_value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2067 | OFStream os(file); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2068 | os << "[generalizing]"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2069 | Name* name = instance_descriptors()->GetKey(modify_index); |
| 2070 | if (name->IsString()) { |
| 2071 | String::cast(name)->PrintOn(file); |
| 2072 | } else { |
| 2073 | os << "{symbol " << static_cast<void*>(name) << "}"; |
| 2074 | } |
| 2075 | os << ":"; |
| 2076 | if (constant_to_field) { |
| 2077 | os << "c"; |
| 2078 | } else { |
| 2079 | os << old_representation.Mnemonic() << "{"; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2080 | if (old_field_type.is_null()) { |
| 2081 | os << Brief(*(old_value.ToHandleChecked())); |
| 2082 | } else { |
| 2083 | old_field_type.ToHandleChecked()->PrintTo(os); |
| 2084 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2085 | os << "}"; |
| 2086 | } |
| 2087 | os << "->" << new_representation.Mnemonic() << "{"; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2088 | if (new_field_type.is_null()) { |
| 2089 | os << Brief(*(new_value.ToHandleChecked())); |
| 2090 | } else { |
| 2091 | new_field_type.ToHandleChecked()->PrintTo(os); |
| 2092 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2093 | os << "} ("; |
| 2094 | if (strlen(reason) > 0) { |
| 2095 | os << reason; |
| 2096 | } else { |
| 2097 | os << "+" << (descriptors - split) << " maps"; |
| 2098 | } |
| 2099 | os << ") ["; |
| 2100 | JavaScriptFrame::PrintTop(GetIsolate(), file, false, true); |
| 2101 | os << "]\n"; |
| 2102 | } |
| 2103 | |
| 2104 | |
| 2105 | void JSObject::PrintInstanceMigration(FILE* file, |
| 2106 | Map* original_map, |
| 2107 | Map* new_map) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2108 | PrintF(file, "[migrating]"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2109 | DescriptorArray* o = original_map->instance_descriptors(); |
| 2110 | DescriptorArray* n = new_map->instance_descriptors(); |
| 2111 | for (int i = 0; i < original_map->NumberOfOwnDescriptors(); i++) { |
| 2112 | Representation o_r = o->GetDetails(i).representation(); |
| 2113 | Representation n_r = n->GetDetails(i).representation(); |
| 2114 | if (!o_r.Equals(n_r)) { |
| 2115 | String::cast(o->GetKey(i))->PrintOn(file); |
| 2116 | PrintF(file, ":%s->%s ", o_r.Mnemonic(), n_r.Mnemonic()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2117 | } else if (o->GetDetails(i).type() == DATA_CONSTANT && |
| 2118 | n->GetDetails(i).type() == DATA) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2119 | Name* name = o->GetKey(i); |
| 2120 | if (name->IsString()) { |
| 2121 | String::cast(name)->PrintOn(file); |
| 2122 | } else { |
| 2123 | PrintF(file, "{symbol %p}", static_cast<void*>(name)); |
| 2124 | } |
| 2125 | PrintF(file, " "); |
| 2126 | } |
| 2127 | } |
| 2128 | PrintF(file, "\n"); |
| 2129 | } |
| 2130 | |
| 2131 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2132 | void HeapObject::HeapObjectShortPrint(std::ostream& os) { // NOLINT |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2133 | Heap* heap = GetHeap(); |
| 2134 | if (!heap->Contains(this)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2135 | os << "!!!INVALID POINTER!!!"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2136 | return; |
| 2137 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2138 | if (!heap->Contains(map())) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2139 | os << "!!!INVALID MAP!!!"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2140 | return; |
| 2141 | } |
| 2142 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2143 | os << this << " "; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2144 | |
| 2145 | if (IsString()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2146 | HeapStringAllocator allocator; |
| 2147 | StringStream accumulator(&allocator); |
| 2148 | String::cast(this)->StringShortPrint(&accumulator); |
| 2149 | os << accumulator.ToCString().get(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2150 | return; |
| 2151 | } |
| 2152 | if (IsJSObject()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2153 | HeapStringAllocator allocator; |
| 2154 | StringStream accumulator(&allocator); |
| 2155 | JSObject::cast(this)->JSObjectShortPrint(&accumulator); |
| 2156 | os << accumulator.ToCString().get(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2157 | return; |
| 2158 | } |
| 2159 | switch (map()->instance_type()) { |
| 2160 | case MAP_TYPE: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2161 | os << "<Map(" << ElementsKindToString(Map::cast(this)->elements_kind()) |
| 2162 | << ")>"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2163 | break; |
| 2164 | case FIXED_ARRAY_TYPE: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2165 | os << "<FixedArray[" << FixedArray::cast(this)->length() << "]>"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2166 | break; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2167 | case FIXED_DOUBLE_ARRAY_TYPE: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2168 | os << "<FixedDoubleArray[" << FixedDoubleArray::cast(this)->length() |
| 2169 | << "]>"; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2170 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2171 | case BYTE_ARRAY_TYPE: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2172 | os << "<ByteArray[" << ByteArray::cast(this)->length() << "]>"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2173 | break; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2174 | case BYTECODE_ARRAY_TYPE: |
| 2175 | os << "<BytecodeArray[" << BytecodeArray::cast(this)->length() << "]>"; |
| 2176 | break; |
| 2177 | case TRANSITION_ARRAY_TYPE: |
| 2178 | os << "<TransitionArray[" << TransitionArray::cast(this)->length() |
| 2179 | << "]>"; |
| 2180 | break; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2181 | case FREE_SPACE_TYPE: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2182 | os << "<FreeSpace[" << FreeSpace::cast(this)->size() << "]>"; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2183 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2184 | #define TYPED_ARRAY_SHORT_PRINT(Type, type, TYPE, ctype, size) \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2185 | case FIXED_##TYPE##_ARRAY_TYPE: \ |
| 2186 | os << "<Fixed" #Type "Array[" << Fixed##Type##Array::cast(this)->length() \ |
| 2187 | << "]>"; \ |
| 2188 | break; |
| 2189 | |
| 2190 | TYPED_ARRAYS(TYPED_ARRAY_SHORT_PRINT) |
| 2191 | #undef TYPED_ARRAY_SHORT_PRINT |
| 2192 | |
| 2193 | case SHARED_FUNCTION_INFO_TYPE: { |
| 2194 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(this); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2195 | base::SmartArrayPointer<char> debug_name = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2196 | shared->DebugName()->ToCString(); |
| 2197 | if (debug_name[0] != 0) { |
| 2198 | os << "<SharedFunctionInfo " << debug_name.get() << ">"; |
| 2199 | } else { |
| 2200 | os << "<SharedFunctionInfo>"; |
| 2201 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2202 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2203 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2204 | case JS_MESSAGE_OBJECT_TYPE: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2205 | os << "<JSMessageObject>"; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2206 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2207 | #define MAKE_STRUCT_CASE(NAME, Name, name) \ |
| 2208 | case NAME##_TYPE: \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2209 | os << "<" #Name ">"; \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2210 | break; |
| 2211 | STRUCT_LIST(MAKE_STRUCT_CASE) |
| 2212 | #undef MAKE_STRUCT_CASE |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2213 | case CODE_TYPE: { |
| 2214 | Code* code = Code::cast(this); |
| 2215 | os << "<Code: " << Code::Kind2String(code->kind()) << ">"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2216 | break; |
| 2217 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2218 | case ODDBALL_TYPE: { |
| 2219 | if (IsUndefined()) { |
| 2220 | os << "<undefined>"; |
| 2221 | } else if (IsTheHole()) { |
| 2222 | os << "<the hole>"; |
| 2223 | } else if (IsNull()) { |
| 2224 | os << "<null>"; |
| 2225 | } else if (IsTrue()) { |
| 2226 | os << "<true>"; |
| 2227 | } else if (IsFalse()) { |
| 2228 | os << "<false>"; |
| 2229 | } else { |
| 2230 | os << "<Odd Oddball>"; |
| 2231 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2232 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2233 | } |
| 2234 | case SYMBOL_TYPE: { |
| 2235 | Symbol* symbol = Symbol::cast(this); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2236 | symbol->SymbolShortPrint(os); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2237 | break; |
| 2238 | } |
| 2239 | case HEAP_NUMBER_TYPE: { |
| 2240 | os << "<Number: "; |
| 2241 | HeapNumber::cast(this)->HeapNumberPrint(os); |
| 2242 | os << ">"; |
| 2243 | break; |
| 2244 | } |
| 2245 | case MUTABLE_HEAP_NUMBER_TYPE: { |
| 2246 | os << "<MutableNumber: "; |
| 2247 | HeapNumber::cast(this)->HeapNumberPrint(os); |
| 2248 | os << '>'; |
| 2249 | break; |
| 2250 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2251 | case SIMD128_VALUE_TYPE: { |
| 2252 | #define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \ |
| 2253 | if (Is##Type()) { \ |
| 2254 | os << "<" #Type ">"; \ |
| 2255 | break; \ |
| 2256 | } |
| 2257 | SIMD128_TYPES(SIMD128_TYPE) |
| 2258 | #undef SIMD128_TYPE |
| 2259 | UNREACHABLE(); |
| 2260 | break; |
| 2261 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2262 | case JS_PROXY_TYPE: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2263 | os << "<JSProxy>"; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2264 | break; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2265 | case FOREIGN_TYPE: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2266 | os << "<Foreign>"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2267 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2268 | case CELL_TYPE: { |
| 2269 | os << "Cell for "; |
| 2270 | HeapStringAllocator allocator; |
| 2271 | StringStream accumulator(&allocator); |
| 2272 | Cell::cast(this)->value()->ShortPrint(&accumulator); |
| 2273 | os << accumulator.ToCString().get(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2274 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2275 | } |
| 2276 | case PROPERTY_CELL_TYPE: { |
| 2277 | os << "PropertyCell for "; |
| 2278 | HeapStringAllocator allocator; |
| 2279 | StringStream accumulator(&allocator); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2280 | PropertyCell* cell = PropertyCell::cast(this); |
| 2281 | cell->value()->ShortPrint(&accumulator); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2282 | os << accumulator.ToCString().get(); |
| 2283 | break; |
| 2284 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2285 | case WEAK_CELL_TYPE: { |
| 2286 | os << "WeakCell for "; |
| 2287 | HeapStringAllocator allocator; |
| 2288 | StringStream accumulator(&allocator); |
| 2289 | WeakCell::cast(this)->value()->ShortPrint(&accumulator); |
| 2290 | os << accumulator.ToCString().get(); |
| 2291 | break; |
| 2292 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2293 | default: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2294 | os << "<Other heap object (" << map()->instance_type() << ")>"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2295 | break; |
| 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2300 | void HeapObject::Iterate(ObjectVisitor* v) { IterateFast<ObjectVisitor>(v); } |
| 2301 | |
| 2302 | |
| 2303 | void HeapObject::IterateBody(ObjectVisitor* v) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2304 | Map* m = map(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2305 | IterateBodyFast<ObjectVisitor>(m->instance_type(), SizeFromMap(m), v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | |
| 2309 | void HeapObject::IterateBody(InstanceType type, int object_size, |
| 2310 | ObjectVisitor* v) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2311 | IterateBodyFast<ObjectVisitor>(type, object_size, v); |
| 2312 | } |
| 2313 | |
| 2314 | |
| 2315 | struct CallIsValidSlot { |
| 2316 | template <typename BodyDescriptor> |
| 2317 | static bool apply(HeapObject* obj, int offset, int) { |
| 2318 | return BodyDescriptor::IsValidSlot(obj, offset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2319 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2320 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2321 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2322 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2323 | bool HeapObject::IsValidSlot(int offset) { |
| 2324 | DCHECK_NE(0, offset); |
| 2325 | return BodyDescriptorApply<CallIsValidSlot, bool>(map()->instance_type(), |
| 2326 | this, offset, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2330 | bool HeapNumber::HeapNumberBooleanValue() { |
| 2331 | return DoubleToBoolean(value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2335 | void HeapNumber::HeapNumberPrint(std::ostream& os) { // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2336 | os << value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2340 | #define FIELD_ADDR_CONST(p, offset) \ |
| 2341 | (reinterpret_cast<const byte*>(p) + offset - kHeapObjectTag) |
| 2342 | |
| 2343 | #define READ_INT32_FIELD(p, offset) \ |
| 2344 | (*reinterpret_cast<const int32_t*>(FIELD_ADDR_CONST(p, offset))) |
| 2345 | |
| 2346 | #define READ_INT64_FIELD(p, offset) \ |
| 2347 | (*reinterpret_cast<const int64_t*>(FIELD_ADDR_CONST(p, offset))) |
| 2348 | |
| 2349 | #define READ_BYTE_FIELD(p, offset) \ |
| 2350 | (*reinterpret_cast<const byte*>(FIELD_ADDR_CONST(p, offset))) |
| 2351 | |
| 2352 | |
| 2353 | // static |
| 2354 | Handle<String> Simd128Value::ToString(Handle<Simd128Value> input) { |
| 2355 | #define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \ |
| 2356 | if (input->Is##Type()) return Type::ToString(Handle<Type>::cast(input)); |
| 2357 | SIMD128_TYPES(SIMD128_TYPE) |
| 2358 | #undef SIMD128_TYPE |
| 2359 | UNREACHABLE(); |
| 2360 | return Handle<String>::null(); |
| 2361 | } |
| 2362 | |
| 2363 | |
| 2364 | // static |
| 2365 | Handle<String> Float32x4::ToString(Handle<Float32x4> input) { |
| 2366 | Isolate* const isolate = input->GetIsolate(); |
| 2367 | char arr[100]; |
| 2368 | Vector<char> buffer(arr, arraysize(arr)); |
| 2369 | std::ostringstream os; |
| 2370 | os << "SIMD.Float32x4(" |
| 2371 | << std::string(DoubleToCString(input->get_lane(0), buffer)) << ", " |
| 2372 | << std::string(DoubleToCString(input->get_lane(1), buffer)) << ", " |
| 2373 | << std::string(DoubleToCString(input->get_lane(2), buffer)) << ", " |
| 2374 | << std::string(DoubleToCString(input->get_lane(3), buffer)) << ")"; |
| 2375 | return isolate->factory()->NewStringFromAsciiChecked(os.str().c_str()); |
| 2376 | } |
| 2377 | |
| 2378 | |
| 2379 | #define SIMD128_BOOL_TO_STRING(Type, lane_count) \ |
| 2380 | Handle<String> Type::ToString(Handle<Type> input) { \ |
| 2381 | Isolate* const isolate = input->GetIsolate(); \ |
| 2382 | std::ostringstream os; \ |
| 2383 | os << "SIMD." #Type "("; \ |
| 2384 | os << (input->get_lane(0) ? "true" : "false"); \ |
| 2385 | for (int i = 1; i < lane_count; i++) { \ |
| 2386 | os << ", " << (input->get_lane(i) ? "true" : "false"); \ |
| 2387 | } \ |
| 2388 | os << ")"; \ |
| 2389 | return isolate->factory()->NewStringFromAsciiChecked(os.str().c_str()); \ |
| 2390 | } |
| 2391 | SIMD128_BOOL_TO_STRING(Bool32x4, 4) |
| 2392 | SIMD128_BOOL_TO_STRING(Bool16x8, 8) |
| 2393 | SIMD128_BOOL_TO_STRING(Bool8x16, 16) |
| 2394 | #undef SIMD128_BOOL_TO_STRING |
| 2395 | |
| 2396 | |
| 2397 | #define SIMD128_INT_TO_STRING(Type, lane_count) \ |
| 2398 | Handle<String> Type::ToString(Handle<Type> input) { \ |
| 2399 | Isolate* const isolate = input->GetIsolate(); \ |
| 2400 | char arr[100]; \ |
| 2401 | Vector<char> buffer(arr, arraysize(arr)); \ |
| 2402 | std::ostringstream os; \ |
| 2403 | os << "SIMD." #Type "("; \ |
| 2404 | os << IntToCString(input->get_lane(0), buffer); \ |
| 2405 | for (int i = 1; i < lane_count; i++) { \ |
| 2406 | os << ", " << IntToCString(input->get_lane(i), buffer); \ |
| 2407 | } \ |
| 2408 | os << ")"; \ |
| 2409 | return isolate->factory()->NewStringFromAsciiChecked(os.str().c_str()); \ |
| 2410 | } |
| 2411 | SIMD128_INT_TO_STRING(Int32x4, 4) |
| 2412 | SIMD128_INT_TO_STRING(Uint32x4, 4) |
| 2413 | SIMD128_INT_TO_STRING(Int16x8, 8) |
| 2414 | SIMD128_INT_TO_STRING(Uint16x8, 8) |
| 2415 | SIMD128_INT_TO_STRING(Int8x16, 16) |
| 2416 | SIMD128_INT_TO_STRING(Uint8x16, 16) |
| 2417 | #undef SIMD128_INT_TO_STRING |
| 2418 | |
| 2419 | |
| 2420 | bool Simd128Value::BitwiseEquals(const Simd128Value* other) const { |
| 2421 | return READ_INT64_FIELD(this, kValueOffset) == |
| 2422 | READ_INT64_FIELD(other, kValueOffset) && |
| 2423 | READ_INT64_FIELD(this, kValueOffset + kInt64Size) == |
| 2424 | READ_INT64_FIELD(other, kValueOffset + kInt64Size); |
| 2425 | } |
| 2426 | |
| 2427 | |
| 2428 | uint32_t Simd128Value::Hash() const { |
| 2429 | uint32_t seed = v8::internal::kZeroHashSeed; |
| 2430 | uint32_t hash; |
| 2431 | hash = ComputeIntegerHash(READ_INT32_FIELD(this, kValueOffset), seed); |
| 2432 | hash = ComputeIntegerHash( |
| 2433 | READ_INT32_FIELD(this, kValueOffset + 1 * kInt32Size), hash * 31); |
| 2434 | hash = ComputeIntegerHash( |
| 2435 | READ_INT32_FIELD(this, kValueOffset + 2 * kInt32Size), hash * 31); |
| 2436 | hash = ComputeIntegerHash( |
| 2437 | READ_INT32_FIELD(this, kValueOffset + 3 * kInt32Size), hash * 31); |
| 2438 | return hash; |
| 2439 | } |
| 2440 | |
| 2441 | |
| 2442 | void Simd128Value::CopyBits(void* destination) const { |
| 2443 | memcpy(destination, &READ_BYTE_FIELD(this, kValueOffset), kSimd128Size); |
| 2444 | } |
| 2445 | |
| 2446 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2447 | String* JSReceiver::class_name() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2448 | if (IsFunction()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2449 | return GetHeap()->Function_string(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2450 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2451 | Object* maybe_constructor = map()->GetConstructor(); |
| 2452 | if (maybe_constructor->IsJSFunction()) { |
| 2453 | JSFunction* constructor = JSFunction::cast(maybe_constructor); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2454 | return String::cast(constructor->shared()->instance_class_name()); |
| 2455 | } |
| 2456 | // If the constructor is not present, return "Object". |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2457 | return GetHeap()->Object_string(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2461 | MaybeHandle<String> JSReceiver::BuiltinStringTag(Handle<JSReceiver> object) { |
| 2462 | Maybe<bool> is_array = Object::IsArray(object); |
| 2463 | MAYBE_RETURN(is_array, MaybeHandle<String>()); |
| 2464 | Isolate* const isolate = object->GetIsolate(); |
| 2465 | if (is_array.FromJust()) { |
| 2466 | return isolate->factory()->Array_string(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2467 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2468 | // TODO(adamk): According to ES2015, we should return "Function" when |
| 2469 | // object has a [[Call]] internal method (corresponds to IsCallable). |
| 2470 | // But this is well cemented in layout tests and might cause webbreakage. |
| 2471 | // if (object->IsCallable()) { |
| 2472 | // return isolate->factory()->Function_string(); |
| 2473 | // } |
| 2474 | // TODO(adamk): class_name() is expensive, replace with instance type |
| 2475 | // checks where possible. |
| 2476 | return handle(object->class_name(), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2480 | // static |
| 2481 | Handle<String> JSReceiver::GetConstructorName(Handle<JSReceiver> receiver) { |
| 2482 | Isolate* isolate = receiver->GetIsolate(); |
| 2483 | |
| 2484 | // If the object was instantiated simply with base == new.target, the |
| 2485 | // constructor on the map provides the most accurate name. |
| 2486 | // Don't provide the info for prototypes, since their constructors are |
| 2487 | // reclaimed and replaced by Object in OptimizeAsPrototype. |
| 2488 | if (!receiver->IsJSProxy() && receiver->map()->new_target_is_base() && |
| 2489 | !receiver->map()->is_prototype_map()) { |
| 2490 | Object* maybe_constructor = receiver->map()->GetConstructor(); |
| 2491 | if (maybe_constructor->IsJSFunction()) { |
| 2492 | JSFunction* constructor = JSFunction::cast(maybe_constructor); |
| 2493 | String* name = String::cast(constructor->shared()->name()); |
| 2494 | if (name->length() == 0) name = constructor->shared()->inferred_name(); |
| 2495 | if (name->length() != 0 && |
| 2496 | !name->Equals(isolate->heap()->Object_string())) { |
| 2497 | return handle(name, isolate); |
| 2498 | } |
| 2499 | } |
| 2500 | } |
| 2501 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2502 | Handle<Object> maybe_tag = JSReceiver::GetDataProperty( |
| 2503 | receiver, isolate->factory()->to_string_tag_symbol()); |
| 2504 | if (maybe_tag->IsString()) return Handle<String>::cast(maybe_tag); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2505 | |
| 2506 | PrototypeIterator iter(isolate, receiver); |
| 2507 | if (iter.IsAtEnd()) return handle(receiver->class_name()); |
| 2508 | Handle<JSReceiver> start = PrototypeIterator::GetCurrent<JSReceiver>(iter); |
| 2509 | LookupIterator it(receiver, isolate->factory()->constructor_string(), start, |
| 2510 | LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR); |
| 2511 | Handle<Object> maybe_constructor = JSReceiver::GetDataProperty(&it); |
| 2512 | Handle<String> result = isolate->factory()->Object_string(); |
| 2513 | if (maybe_constructor->IsJSFunction()) { |
| 2514 | JSFunction* constructor = JSFunction::cast(*maybe_constructor); |
| 2515 | String* name = String::cast(constructor->shared()->name()); |
| 2516 | if (name->length() == 0) name = constructor->shared()->inferred_name(); |
| 2517 | if (name->length() > 0) result = handle(name, isolate); |
| 2518 | } |
| 2519 | |
| 2520 | return result.is_identical_to(isolate->factory()->Object_string()) |
| 2521 | ? handle(receiver->class_name()) |
| 2522 | : result; |
| 2523 | } |
| 2524 | |
| 2525 | |
| 2526 | Context* JSReceiver::GetCreationContext() { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2527 | JSReceiver* receiver = this; |
| 2528 | while (receiver->IsJSBoundFunction()) { |
| 2529 | receiver = JSBoundFunction::cast(receiver)->bound_target_function(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2530 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2531 | Object* constructor = receiver->map()->GetConstructor(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2532 | JSFunction* function; |
| 2533 | if (constructor->IsJSFunction()) { |
| 2534 | function = JSFunction::cast(constructor); |
| 2535 | } else { |
| 2536 | // Functions have null as a constructor, |
| 2537 | // but any JSFunction knows its context immediately. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2538 | CHECK(receiver->IsJSFunction()); |
| 2539 | function = JSFunction::cast(receiver); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2540 | } |
| 2541 | |
| 2542 | return function->context()->native_context(); |
| 2543 | } |
| 2544 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2545 | static Handle<Object> WrapType(Handle<FieldType> type) { |
| 2546 | if (type->IsClass()) return Map::WeakCellForMap(type->AsClass()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2547 | return type; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2550 | MaybeHandle<Map> Map::CopyWithField(Handle<Map> map, Handle<Name> name, |
| 2551 | Handle<FieldType> type, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2552 | PropertyAttributes attributes, |
| 2553 | Representation representation, |
| 2554 | TransitionFlag flag) { |
| 2555 | DCHECK(DescriptorArray::kNotFound == |
| 2556 | map->instance_descriptors()->Search( |
| 2557 | *name, map->NumberOfOwnDescriptors())); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2558 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2559 | // Ensure the descriptor array does not get too big. |
| 2560 | if (map->NumberOfOwnDescriptors() >= kMaxNumberOfDescriptors) { |
| 2561 | return MaybeHandle<Map>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2564 | Isolate* isolate = map->GetIsolate(); |
| 2565 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2566 | // Compute the new index for new field. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2567 | int index = map->NextFreePropertyIndex(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2568 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2569 | if (map->instance_type() == JS_CONTEXT_EXTENSION_OBJECT_TYPE) { |
| 2570 | representation = Representation::Tagged(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2571 | type = FieldType::Any(isolate); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2572 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2573 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2574 | Handle<Object> wrapped_type(WrapType(type)); |
| 2575 | |
| 2576 | DataDescriptor new_field_desc(name, index, wrapped_type, attributes, |
| 2577 | representation); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2578 | Handle<Map> new_map = Map::CopyAddDescriptor(map, &new_field_desc, flag); |
| 2579 | int unused_property_fields = new_map->unused_property_fields() - 1; |
| 2580 | if (unused_property_fields < 0) { |
| 2581 | unused_property_fields += JSObject::kFieldsAdded; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2582 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2583 | new_map->set_unused_property_fields(unused_property_fields); |
| 2584 | return new_map; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2588 | MaybeHandle<Map> Map::CopyWithConstant(Handle<Map> map, |
| 2589 | Handle<Name> name, |
| 2590 | Handle<Object> constant, |
| 2591 | PropertyAttributes attributes, |
| 2592 | TransitionFlag flag) { |
| 2593 | // Ensure the descriptor array does not get too big. |
| 2594 | if (map->NumberOfOwnDescriptors() >= kMaxNumberOfDescriptors) { |
| 2595 | return MaybeHandle<Map>(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2596 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2597 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2598 | // Allocate new instance descriptors with (name, constant) added. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2599 | DataConstantDescriptor new_constant_desc(name, constant, attributes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2600 | return Map::CopyAddDescriptor(map, &new_constant_desc, flag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2604 | void JSObject::AddSlowProperty(Handle<JSObject> object, |
| 2605 | Handle<Name> name, |
| 2606 | Handle<Object> value, |
| 2607 | PropertyAttributes attributes) { |
| 2608 | DCHECK(!object->HasFastProperties()); |
| 2609 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2610 | if (object->IsJSGlobalObject()) { |
| 2611 | Handle<GlobalDictionary> dict(object->global_dictionary()); |
| 2612 | PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2613 | int entry = dict->FindEntry(name); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2614 | // If there's a cell there, just invalidate and set the property. |
| 2615 | if (entry != GlobalDictionary::kNotFound) { |
| 2616 | PropertyCell::UpdateCell(dict, entry, value, details); |
| 2617 | // TODO(ishell): move this to UpdateCell. |
| 2618 | // Need to adjust the details. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2619 | int index = dict->NextEnumerationIndex(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2620 | dict->SetNextEnumerationIndex(index + 1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2621 | PropertyCell* cell = PropertyCell::cast(dict->ValueAt(entry)); |
| 2622 | details = cell->property_details().set_index(index); |
| 2623 | cell->set_property_details(details); |
| 2624 | |
| 2625 | } else { |
| 2626 | auto cell = isolate->factory()->NewPropertyCell(); |
| 2627 | cell->set_value(*value); |
| 2628 | auto cell_type = value->IsUndefined() ? PropertyCellType::kUndefined |
| 2629 | : PropertyCellType::kConstant; |
| 2630 | details = details.set_cell_type(cell_type); |
| 2631 | value = cell; |
| 2632 | |
| 2633 | Handle<GlobalDictionary> result = |
| 2634 | GlobalDictionary::Add(dict, name, value, details); |
| 2635 | if (*dict != *result) object->set_properties(*result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2636 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2637 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2638 | Handle<NameDictionary> dict(object->property_dictionary()); |
| 2639 | PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell); |
| 2640 | Handle<NameDictionary> result = |
| 2641 | NameDictionary::Add(dict, name, value, details); |
| 2642 | if (*dict != *result) object->set_properties(*result); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2643 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2644 | } |
| 2645 | |
| 2646 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2647 | MaybeHandle<Object> JSObject::EnqueueChangeRecord(Handle<JSObject> object, |
| 2648 | const char* type_str, |
| 2649 | Handle<Name> name, |
| 2650 | Handle<Object> old_value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2651 | DCHECK(!object->IsJSGlobalProxy()); |
| 2652 | DCHECK(!object->IsJSGlobalObject()); |
| 2653 | Isolate* isolate = object->GetIsolate(); |
| 2654 | HandleScope scope(isolate); |
| 2655 | Handle<String> type = isolate->factory()->InternalizeUtf8String(type_str); |
| 2656 | Handle<Object> args[] = { type, object, name, old_value }; |
| 2657 | int argc = name.is_null() ? 2 : old_value->IsTheHole() ? 3 : 4; |
| 2658 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2659 | return Execution::Call(isolate, |
| 2660 | Handle<JSFunction>(isolate->observers_notify_change()), |
| 2661 | isolate->factory()->undefined_value(), argc, args); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | |
| 2665 | const char* Representation::Mnemonic() const { |
| 2666 | switch (kind_) { |
| 2667 | case kNone: return "v"; |
| 2668 | case kTagged: return "t"; |
| 2669 | case kSmi: return "s"; |
| 2670 | case kDouble: return "d"; |
| 2671 | case kInteger32: return "i"; |
| 2672 | case kHeapObject: return "h"; |
| 2673 | case kExternal: return "x"; |
| 2674 | default: |
| 2675 | UNREACHABLE(); |
| 2676 | return NULL; |
| 2677 | } |
| 2678 | } |
| 2679 | |
| 2680 | |
| 2681 | bool Map::InstancesNeedRewriting(Map* target, int target_number_of_fields, |
| 2682 | int target_inobject, int target_unused, |
| 2683 | int* old_number_of_fields) { |
| 2684 | // If fields were added (or removed), rewrite the instance. |
| 2685 | *old_number_of_fields = NumberOfFields(); |
| 2686 | DCHECK(target_number_of_fields >= *old_number_of_fields); |
| 2687 | if (target_number_of_fields != *old_number_of_fields) return true; |
| 2688 | |
| 2689 | // If smi descriptors were replaced by double descriptors, rewrite. |
| 2690 | DescriptorArray* old_desc = instance_descriptors(); |
| 2691 | DescriptorArray* new_desc = target->instance_descriptors(); |
| 2692 | int limit = NumberOfOwnDescriptors(); |
| 2693 | for (int i = 0; i < limit; i++) { |
| 2694 | if (new_desc->GetDetails(i).representation().IsDouble() != |
| 2695 | old_desc->GetDetails(i).representation().IsDouble()) { |
| 2696 | return true; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2697 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2698 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2699 | |
| 2700 | // If no fields were added, and no inobject properties were removed, setting |
| 2701 | // the map is sufficient. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2702 | if (target_inobject == GetInObjectProperties()) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2703 | // In-object slack tracking may have reduced the object size of the new map. |
| 2704 | // In that case, succeed if all existing fields were inobject, and they still |
| 2705 | // fit within the new inobject size. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2706 | DCHECK(target_inobject < GetInObjectProperties()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2707 | if (target_number_of_fields <= target_inobject) { |
| 2708 | DCHECK(target_number_of_fields + target_unused == target_inobject); |
| 2709 | return false; |
| 2710 | } |
| 2711 | // Otherwise, properties will need to be moved to the backing store. |
| 2712 | return true; |
| 2713 | } |
| 2714 | |
| 2715 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2716 | // static |
| 2717 | void JSObject::UpdatePrototypeUserRegistration(Handle<Map> old_map, |
| 2718 | Handle<Map> new_map, |
| 2719 | Isolate* isolate) { |
| 2720 | if (!FLAG_track_prototype_users) return; |
| 2721 | if (!old_map->is_prototype_map()) return; |
| 2722 | DCHECK(new_map->is_prototype_map()); |
| 2723 | bool was_registered = JSObject::UnregisterPrototypeUser(old_map, isolate); |
| 2724 | new_map->set_prototype_info(old_map->prototype_info()); |
| 2725 | old_map->set_prototype_info(Smi::FromInt(0)); |
| 2726 | if (FLAG_trace_prototype_users) { |
| 2727 | PrintF("Moving prototype_info %p from map %p to map %p.\n", |
| 2728 | reinterpret_cast<void*>(new_map->prototype_info()), |
| 2729 | reinterpret_cast<void*>(*old_map), |
| 2730 | reinterpret_cast<void*>(*new_map)); |
| 2731 | } |
| 2732 | if (was_registered) { |
| 2733 | if (new_map->prototype_info()->IsPrototypeInfo()) { |
| 2734 | // The new map isn't registered with its prototype yet; reflect this fact |
| 2735 | // in the PrototypeInfo it just inherited from the old map. |
| 2736 | PrototypeInfo::cast(new_map->prototype_info()) |
| 2737 | ->set_registry_slot(PrototypeInfo::UNREGISTERED); |
| 2738 | } |
| 2739 | JSObject::LazyRegisterPrototypeUser(new_map, isolate); |
| 2740 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2743 | namespace { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2744 | // To migrate a fast instance to a fast map: |
| 2745 | // - First check whether the instance needs to be rewritten. If not, simply |
| 2746 | // change the map. |
| 2747 | // - Otherwise, allocate a fixed array large enough to hold all fields, in |
| 2748 | // addition to unused space. |
| 2749 | // - Copy all existing properties in, in the following order: backing store |
| 2750 | // properties, unused fields, inobject properties. |
| 2751 | // - If all allocation succeeded, commit the state atomically: |
| 2752 | // * Copy inobject properties from the backing store back into the object. |
| 2753 | // * Trim the difference in instance size of the object. This also cleanly |
| 2754 | // frees inobject properties that moved to the backing store. |
| 2755 | // * If there are properties left in the backing store, trim of the space used |
| 2756 | // to temporarily store the inobject properties. |
| 2757 | // * If there are properties left in the backing store, install the backing |
| 2758 | // store. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2759 | void MigrateFastToFast(Handle<JSObject> object, Handle<Map> new_map) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2760 | Isolate* isolate = object->GetIsolate(); |
| 2761 | Handle<Map> old_map(object->map()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2762 | // In case of a regular transition. |
| 2763 | if (new_map->GetBackPointer() == *old_map) { |
| 2764 | // If the map does not add named properties, simply set the map. |
| 2765 | if (old_map->NumberOfOwnDescriptors() == |
| 2766 | new_map->NumberOfOwnDescriptors()) { |
| 2767 | object->synchronized_set_map(*new_map); |
| 2768 | return; |
| 2769 | } |
| 2770 | |
| 2771 | PropertyDetails details = new_map->GetLastDescriptorDetails(); |
| 2772 | // Either new_map adds an kDescriptor property, or a kField property for |
| 2773 | // which there is still space, and which does not require a mutable double |
| 2774 | // box (an out-of-object double). |
| 2775 | if (details.location() == kDescriptor || |
| 2776 | (old_map->unused_property_fields() > 0 && |
| 2777 | ((FLAG_unbox_double_fields && object->properties()->length() == 0) || |
| 2778 | !details.representation().IsDouble()))) { |
| 2779 | object->synchronized_set_map(*new_map); |
| 2780 | return; |
| 2781 | } |
| 2782 | |
| 2783 | // If there is still space in the object, we need to allocate a mutable |
| 2784 | // double box. |
| 2785 | if (old_map->unused_property_fields() > 0) { |
| 2786 | FieldIndex index = |
| 2787 | FieldIndex::ForDescriptor(*new_map, new_map->LastAdded()); |
| 2788 | DCHECK(details.representation().IsDouble()); |
| 2789 | DCHECK(!new_map->IsUnboxedDoubleField(index)); |
| 2790 | Handle<Object> value = isolate->factory()->NewHeapNumber(0, MUTABLE); |
| 2791 | object->RawFastPropertyAtPut(index, *value); |
| 2792 | object->synchronized_set_map(*new_map); |
| 2793 | return; |
| 2794 | } |
| 2795 | |
| 2796 | // This migration is a transition from a map that has run out of property |
| 2797 | // space. Extend the backing store. |
| 2798 | int grow_by = new_map->unused_property_fields() + 1; |
| 2799 | Handle<FixedArray> old_storage = handle(object->properties(), isolate); |
| 2800 | Handle<FixedArray> new_storage = |
| 2801 | isolate->factory()->CopyFixedArrayAndGrow(old_storage, grow_by); |
| 2802 | |
| 2803 | // Properly initialize newly added property. |
| 2804 | Handle<Object> value; |
| 2805 | if (details.representation().IsDouble()) { |
| 2806 | value = isolate->factory()->NewHeapNumber(0, MUTABLE); |
| 2807 | } else { |
| 2808 | value = isolate->factory()->uninitialized_value(); |
| 2809 | } |
| 2810 | DCHECK_EQ(DATA, details.type()); |
| 2811 | int target_index = details.field_index() - new_map->GetInObjectProperties(); |
| 2812 | DCHECK(target_index >= 0); // Must be a backing store index. |
| 2813 | new_storage->set(target_index, *value); |
| 2814 | |
| 2815 | // From here on we cannot fail and we shouldn't GC anymore. |
| 2816 | DisallowHeapAllocation no_allocation; |
| 2817 | |
| 2818 | // Set the new property value and do the map transition. |
| 2819 | object->set_properties(*new_storage); |
| 2820 | object->synchronized_set_map(*new_map); |
| 2821 | return; |
| 2822 | } |
| 2823 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2824 | int old_number_of_fields; |
| 2825 | int number_of_fields = new_map->NumberOfFields(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2826 | int inobject = new_map->GetInObjectProperties(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2827 | int unused = new_map->unused_property_fields(); |
| 2828 | |
| 2829 | // Nothing to do if no functions were converted to fields and no smis were |
| 2830 | // converted to doubles. |
| 2831 | if (!old_map->InstancesNeedRewriting(*new_map, number_of_fields, inobject, |
| 2832 | unused, &old_number_of_fields)) { |
| 2833 | object->synchronized_set_map(*new_map); |
| 2834 | return; |
| 2835 | } |
| 2836 | |
| 2837 | int total_size = number_of_fields + unused; |
| 2838 | int external = total_size - inobject; |
| 2839 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2840 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(total_size); |
| 2841 | |
| 2842 | Handle<DescriptorArray> old_descriptors(old_map->instance_descriptors()); |
| 2843 | Handle<DescriptorArray> new_descriptors(new_map->instance_descriptors()); |
| 2844 | int old_nof = old_map->NumberOfOwnDescriptors(); |
| 2845 | int new_nof = new_map->NumberOfOwnDescriptors(); |
| 2846 | |
| 2847 | // This method only supports generalizing instances to at least the same |
| 2848 | // number of properties. |
| 2849 | DCHECK(old_nof <= new_nof); |
| 2850 | |
| 2851 | for (int i = 0; i < old_nof; i++) { |
| 2852 | PropertyDetails details = new_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2853 | if (details.type() != DATA) continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2854 | PropertyDetails old_details = old_descriptors->GetDetails(i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2855 | Representation old_representation = old_details.representation(); |
| 2856 | Representation representation = details.representation(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2857 | Handle<Object> value; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2858 | if (old_details.type() == ACCESSOR_CONSTANT) { |
| 2859 | // In case of kAccessor -> kData property reconfiguration, the property |
| 2860 | // must already be prepared for data or certain type. |
| 2861 | DCHECK(!details.representation().IsNone()); |
| 2862 | if (details.representation().IsDouble()) { |
| 2863 | value = isolate->factory()->NewHeapNumber(0, MUTABLE); |
| 2864 | } else { |
| 2865 | value = isolate->factory()->uninitialized_value(); |
| 2866 | } |
| 2867 | } else if (old_details.type() == DATA_CONSTANT) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2868 | value = handle(old_descriptors->GetValue(i), isolate); |
| 2869 | DCHECK(!old_representation.IsDouble() && !representation.IsDouble()); |
| 2870 | } else { |
| 2871 | FieldIndex index = FieldIndex::ForDescriptor(*old_map, i); |
| 2872 | if (object->IsUnboxedDoubleField(index)) { |
| 2873 | double old = object->RawFastDoublePropertyAt(index); |
| 2874 | value = isolate->factory()->NewHeapNumber( |
| 2875 | old, representation.IsDouble() ? MUTABLE : IMMUTABLE); |
| 2876 | |
| 2877 | } else { |
| 2878 | value = handle(object->RawFastPropertyAt(index), isolate); |
| 2879 | if (!old_representation.IsDouble() && representation.IsDouble()) { |
| 2880 | if (old_representation.IsNone()) { |
| 2881 | value = handle(Smi::FromInt(0), isolate); |
| 2882 | } |
| 2883 | value = Object::NewStorageFor(isolate, value, representation); |
| 2884 | } else if (old_representation.IsDouble() && |
| 2885 | !representation.IsDouble()) { |
| 2886 | value = Object::WrapForRead(isolate, value, old_representation); |
| 2887 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2888 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2889 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2890 | DCHECK(!(representation.IsDouble() && value->IsSmi())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2891 | int target_index = new_descriptors->GetFieldIndex(i) - inobject; |
| 2892 | if (target_index < 0) target_index += total_size; |
| 2893 | array->set(target_index, *value); |
| 2894 | } |
| 2895 | |
| 2896 | for (int i = old_nof; i < new_nof; i++) { |
| 2897 | PropertyDetails details = new_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2898 | if (details.type() != DATA) continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2899 | Handle<Object> value; |
| 2900 | if (details.representation().IsDouble()) { |
| 2901 | value = isolate->factory()->NewHeapNumber(0, MUTABLE); |
| 2902 | } else { |
| 2903 | value = isolate->factory()->uninitialized_value(); |
| 2904 | } |
| 2905 | int target_index = new_descriptors->GetFieldIndex(i) - inobject; |
| 2906 | if (target_index < 0) target_index += total_size; |
| 2907 | array->set(target_index, *value); |
| 2908 | } |
| 2909 | |
| 2910 | // From here on we cannot fail and we shouldn't GC anymore. |
| 2911 | DisallowHeapAllocation no_allocation; |
| 2912 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2913 | Heap* heap = isolate->heap(); |
| 2914 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2915 | // Copy (real) inobject properties. If necessary, stop at number_of_fields to |
| 2916 | // avoid overwriting |one_pointer_filler_map|. |
| 2917 | int limit = Min(inobject, number_of_fields); |
| 2918 | for (int i = 0; i < limit; i++) { |
| 2919 | FieldIndex index = FieldIndex::ForPropertyIndex(*new_map, i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2920 | Object* value = array->get(external + i); |
| 2921 | // Can't use JSObject::FastPropertyAtPut() because proper map was not set |
| 2922 | // yet. |
| 2923 | if (new_map->IsUnboxedDoubleField(index)) { |
| 2924 | DCHECK(value->IsMutableHeapNumber()); |
| 2925 | object->RawFastDoublePropertyAtPut(index, |
| 2926 | HeapNumber::cast(value)->value()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2927 | if (i < old_number_of_fields && !old_map->IsUnboxedDoubleField(index)) { |
| 2928 | // Transition from tagged to untagged slot. |
| 2929 | heap->ClearRecordedSlot(*object, |
| 2930 | HeapObject::RawField(*object, index.offset())); |
| 2931 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2932 | } else { |
| 2933 | object->RawFastPropertyAtPut(index, value); |
| 2934 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2937 | |
| 2938 | // If there are properties in the new backing store, trim it to the correct |
| 2939 | // size and install the backing store into the object. |
| 2940 | if (external > 0) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2941 | heap->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>(*array, inobject); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2942 | object->set_properties(*array); |
| 2943 | } |
| 2944 | |
| 2945 | // Create filler object past the new instance size. |
| 2946 | int new_instance_size = new_map->instance_size(); |
| 2947 | int instance_size_delta = old_map->instance_size() - new_instance_size; |
| 2948 | DCHECK(instance_size_delta >= 0); |
| 2949 | |
| 2950 | if (instance_size_delta > 0) { |
| 2951 | Address address = object->address(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2952 | heap->CreateFillerObjectAt(address + new_instance_size, instance_size_delta, |
| 2953 | ClearRecordedSlots::kYes); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2954 | heap->AdjustLiveBytes(*object, -instance_size_delta, |
| 2955 | Heap::CONCURRENT_TO_SWEEPER); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2956 | } |
| 2957 | |
| 2958 | // We are storing the new map using release store after creating a filler for |
| 2959 | // the left-over space to avoid races with the sweeper thread. |
| 2960 | object->synchronized_set_map(*new_map); |
| 2961 | } |
| 2962 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2963 | void MigrateFastToSlow(Handle<JSObject> object, Handle<Map> new_map, |
| 2964 | int expected_additional_properties) { |
| 2965 | // The global object is always normalized. |
| 2966 | DCHECK(!object->IsJSGlobalObject()); |
| 2967 | // JSGlobalProxy must never be normalized |
| 2968 | DCHECK(!object->IsJSGlobalProxy()); |
| 2969 | |
| 2970 | Isolate* isolate = object->GetIsolate(); |
| 2971 | HandleScope scope(isolate); |
| 2972 | Handle<Map> map(object->map()); |
| 2973 | |
| 2974 | // Allocate new content. |
| 2975 | int real_size = map->NumberOfOwnDescriptors(); |
| 2976 | int property_count = real_size; |
| 2977 | if (expected_additional_properties > 0) { |
| 2978 | property_count += expected_additional_properties; |
| 2979 | } else { |
| 2980 | property_count += 2; // Make space for two more properties. |
| 2981 | } |
| 2982 | Handle<NameDictionary> dictionary = |
| 2983 | NameDictionary::New(isolate, property_count); |
| 2984 | |
| 2985 | Handle<DescriptorArray> descs(map->instance_descriptors()); |
| 2986 | for (int i = 0; i < real_size; i++) { |
| 2987 | PropertyDetails details = descs->GetDetails(i); |
| 2988 | Handle<Name> key(descs->GetKey(i)); |
| 2989 | switch (details.type()) { |
| 2990 | case DATA_CONSTANT: { |
| 2991 | Handle<Object> value(descs->GetConstant(i), isolate); |
| 2992 | PropertyDetails d(details.attributes(), DATA, i + 1, |
| 2993 | PropertyCellType::kNoCell); |
| 2994 | dictionary = NameDictionary::Add(dictionary, key, value, d); |
| 2995 | break; |
| 2996 | } |
| 2997 | case DATA: { |
| 2998 | FieldIndex index = FieldIndex::ForDescriptor(*map, i); |
| 2999 | Handle<Object> value; |
| 3000 | if (object->IsUnboxedDoubleField(index)) { |
| 3001 | double old_value = object->RawFastDoublePropertyAt(index); |
| 3002 | value = isolate->factory()->NewHeapNumber(old_value); |
| 3003 | } else { |
| 3004 | value = handle(object->RawFastPropertyAt(index), isolate); |
| 3005 | if (details.representation().IsDouble()) { |
| 3006 | DCHECK(value->IsMutableHeapNumber()); |
| 3007 | Handle<HeapNumber> old = Handle<HeapNumber>::cast(value); |
| 3008 | value = isolate->factory()->NewHeapNumber(old->value()); |
| 3009 | } |
| 3010 | } |
| 3011 | PropertyDetails d(details.attributes(), DATA, i + 1, |
| 3012 | PropertyCellType::kNoCell); |
| 3013 | dictionary = NameDictionary::Add(dictionary, key, value, d); |
| 3014 | break; |
| 3015 | } |
| 3016 | case ACCESSOR: { |
| 3017 | FieldIndex index = FieldIndex::ForDescriptor(*map, i); |
| 3018 | Handle<Object> value(object->RawFastPropertyAt(index), isolate); |
| 3019 | PropertyDetails d(details.attributes(), ACCESSOR_CONSTANT, i + 1, |
| 3020 | PropertyCellType::kNoCell); |
| 3021 | dictionary = NameDictionary::Add(dictionary, key, value, d); |
| 3022 | break; |
| 3023 | } |
| 3024 | case ACCESSOR_CONSTANT: { |
| 3025 | Handle<Object> value(descs->GetCallbacksObject(i), isolate); |
| 3026 | PropertyDetails d(details.attributes(), ACCESSOR_CONSTANT, i + 1, |
| 3027 | PropertyCellType::kNoCell); |
| 3028 | dictionary = NameDictionary::Add(dictionary, key, value, d); |
| 3029 | break; |
| 3030 | } |
| 3031 | } |
| 3032 | } |
| 3033 | |
| 3034 | // Copy the next enumeration index from instance descriptor. |
| 3035 | dictionary->SetNextEnumerationIndex(real_size + 1); |
| 3036 | |
| 3037 | // From here on we cannot fail and we shouldn't GC anymore. |
| 3038 | DisallowHeapAllocation no_allocation; |
| 3039 | |
| 3040 | // Resize the object in the heap if necessary. |
| 3041 | int new_instance_size = new_map->instance_size(); |
| 3042 | int instance_size_delta = map->instance_size() - new_instance_size; |
| 3043 | DCHECK(instance_size_delta >= 0); |
| 3044 | |
| 3045 | if (instance_size_delta > 0) { |
| 3046 | Heap* heap = isolate->heap(); |
| 3047 | heap->CreateFillerObjectAt(object->address() + new_instance_size, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3048 | instance_size_delta, ClearRecordedSlots::kYes); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3049 | heap->AdjustLiveBytes(*object, -instance_size_delta, |
| 3050 | Heap::CONCURRENT_TO_SWEEPER); |
| 3051 | } |
| 3052 | |
| 3053 | // We are storing the new map using release store after creating a filler for |
| 3054 | // the left-over space to avoid races with the sweeper thread. |
| 3055 | object->synchronized_set_map(*new_map); |
| 3056 | |
| 3057 | object->set_properties(*dictionary); |
| 3058 | |
| 3059 | // Ensure that in-object space of slow-mode object does not contain random |
| 3060 | // garbage. |
| 3061 | int inobject_properties = new_map->GetInObjectProperties(); |
| 3062 | for (int i = 0; i < inobject_properties; i++) { |
| 3063 | FieldIndex index = FieldIndex::ForPropertyIndex(*new_map, i); |
| 3064 | object->RawFastPropertyAtPut(index, Smi::FromInt(0)); |
| 3065 | } |
| 3066 | |
| 3067 | isolate->counters()->props_to_dictionary()->Increment(); |
| 3068 | |
| 3069 | #ifdef DEBUG |
| 3070 | if (FLAG_trace_normalization) { |
| 3071 | OFStream os(stdout); |
| 3072 | os << "Object properties have been normalized:\n"; |
| 3073 | object->Print(os); |
| 3074 | } |
| 3075 | #endif |
| 3076 | } |
| 3077 | |
| 3078 | } // namespace |
| 3079 | |
| 3080 | void JSObject::MigrateToMap(Handle<JSObject> object, Handle<Map> new_map, |
| 3081 | int expected_additional_properties) { |
| 3082 | if (object->map() == *new_map) return; |
| 3083 | Handle<Map> old_map(object->map()); |
| 3084 | if (old_map->is_prototype_map()) { |
| 3085 | // If this object is a prototype (the callee will check), invalidate any |
| 3086 | // prototype chains involving it. |
| 3087 | InvalidatePrototypeChains(object->map()); |
| 3088 | |
| 3089 | // If the map was registered with its prototype before, ensure that it |
| 3090 | // registers with its new prototype now. This preserves the invariant that |
| 3091 | // when a map on a prototype chain is registered with its prototype, then |
| 3092 | // all prototypes further up the chain are also registered with their |
| 3093 | // respective prototypes. |
| 3094 | UpdatePrototypeUserRegistration(old_map, new_map, new_map->GetIsolate()); |
| 3095 | } |
| 3096 | |
| 3097 | if (old_map->is_dictionary_map()) { |
| 3098 | // For slow-to-fast migrations JSObject::MigrateSlowToFast() |
| 3099 | // must be used instead. |
| 3100 | CHECK(new_map->is_dictionary_map()); |
| 3101 | |
| 3102 | // Slow-to-slow migration is trivial. |
| 3103 | object->set_map(*new_map); |
| 3104 | } else if (!new_map->is_dictionary_map()) { |
| 3105 | MigrateFastToFast(object, new_map); |
| 3106 | if (old_map->is_prototype_map()) { |
| 3107 | DCHECK(!old_map->is_stable()); |
| 3108 | DCHECK(new_map->is_stable()); |
| 3109 | // Clear out the old descriptor array to avoid problems to sharing |
| 3110 | // the descriptor array without using an explicit. |
| 3111 | old_map->InitializeDescriptors( |
| 3112 | old_map->GetHeap()->empty_descriptor_array(), |
| 3113 | LayoutDescriptor::FastPointerLayout()); |
| 3114 | // Ensure that no transition was inserted for prototype migrations. |
| 3115 | DCHECK_EQ( |
| 3116 | 0, TransitionArray::NumberOfTransitions(old_map->raw_transitions())); |
| 3117 | DCHECK(new_map->GetBackPointer()->IsUndefined()); |
| 3118 | } |
| 3119 | } else { |
| 3120 | MigrateFastToSlow(object, new_map, expected_additional_properties); |
| 3121 | } |
| 3122 | |
| 3123 | // Careful: Don't allocate here! |
| 3124 | // For some callers of this method, |object| might be in an inconsistent |
| 3125 | // state now: the new map might have a new elements_kind, but the object's |
| 3126 | // elements pointer hasn't been updated yet. Callers will fix this, but in |
| 3127 | // the meantime, (indirectly) calling JSObjectVerify() must be avoided. |
| 3128 | // When adding code here, add a DisallowHeapAllocation too. |
| 3129 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3130 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3131 | int Map::NumberOfFields() { |
| 3132 | DescriptorArray* descriptors = instance_descriptors(); |
| 3133 | int result = 0; |
| 3134 | for (int i = 0; i < NumberOfOwnDescriptors(); i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3135 | if (descriptors->GetDetails(i).location() == kField) result++; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3136 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3137 | return result; |
| 3138 | } |
| 3139 | |
| 3140 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3141 | Handle<Map> Map::CopyGeneralizeAllRepresentations( |
| 3142 | Handle<Map> map, int modify_index, StoreMode store_mode, PropertyKind kind, |
| 3143 | PropertyAttributes attributes, const char* reason) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3144 | Isolate* isolate = map->GetIsolate(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3145 | Handle<DescriptorArray> old_descriptors(map->instance_descriptors(), isolate); |
| 3146 | int number_of_own_descriptors = map->NumberOfOwnDescriptors(); |
| 3147 | Handle<DescriptorArray> descriptors = |
| 3148 | DescriptorArray::CopyUpTo(old_descriptors, number_of_own_descriptors); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3149 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3150 | for (int i = 0; i < number_of_own_descriptors; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3151 | descriptors->SetRepresentation(i, Representation::Tagged()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3152 | if (descriptors->GetDetails(i).type() == DATA) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3153 | descriptors->SetValue(i, FieldType::Any()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3154 | } |
| 3155 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3156 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3157 | Handle<LayoutDescriptor> new_layout_descriptor( |
| 3158 | LayoutDescriptor::FastPointerLayout(), isolate); |
| 3159 | Handle<Map> new_map = CopyReplaceDescriptors( |
| 3160 | map, descriptors, new_layout_descriptor, OMIT_TRANSITION, |
| 3161 | MaybeHandle<Name>(), reason, SPECIAL_TRANSITION); |
| 3162 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3163 | // 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] | 3164 | if (modify_index >= 0) { |
| 3165 | PropertyDetails details = descriptors->GetDetails(modify_index); |
| 3166 | if (store_mode == FORCE_FIELD && |
| 3167 | (details.type() != DATA || details.attributes() != attributes)) { |
| 3168 | int field_index = details.type() == DATA ? details.field_index() |
| 3169 | : new_map->NumberOfFields(); |
| 3170 | DataDescriptor d(handle(descriptors->GetKey(modify_index), isolate), |
| 3171 | field_index, attributes, Representation::Tagged()); |
| 3172 | descriptors->Replace(modify_index, &d); |
| 3173 | if (details.type() != DATA) { |
| 3174 | int unused_property_fields = new_map->unused_property_fields() - 1; |
| 3175 | if (unused_property_fields < 0) { |
| 3176 | unused_property_fields += JSObject::kFieldsAdded; |
| 3177 | } |
| 3178 | new_map->set_unused_property_fields(unused_property_fields); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3179 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3180 | } else { |
| 3181 | DCHECK(details.attributes() == attributes); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3182 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3183 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3184 | if (FLAG_trace_generalization) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3185 | MaybeHandle<FieldType> field_type = FieldType::None(isolate); |
| 3186 | if (details.type() == DATA) { |
| 3187 | field_type = handle( |
| 3188 | map->instance_descriptors()->GetFieldType(modify_index), isolate); |
| 3189 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3190 | map->PrintGeneralization( |
| 3191 | stdout, reason, modify_index, new_map->NumberOfOwnDescriptors(), |
| 3192 | new_map->NumberOfOwnDescriptors(), |
| 3193 | details.type() == DATA_CONSTANT && store_mode == FORCE_FIELD, |
| 3194 | details.representation(), Representation::Tagged(), field_type, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3195 | MaybeHandle<Object>(), FieldType::Any(isolate), |
| 3196 | MaybeHandle<Object>()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3197 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3198 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3199 | return new_map; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
| 3202 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3203 | void Map::DeprecateTransitionTree() { |
| 3204 | if (is_deprecated()) return; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3205 | Object* transitions = raw_transitions(); |
| 3206 | int num_transitions = TransitionArray::NumberOfTransitions(transitions); |
| 3207 | for (int i = 0; i < num_transitions; ++i) { |
| 3208 | TransitionArray::GetTarget(transitions, i)->DeprecateTransitionTree(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3209 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3210 | deprecate(); |
| 3211 | dependent_code()->DeoptimizeDependentCodeGroup( |
| 3212 | GetIsolate(), DependentCode::kTransitionGroup); |
| 3213 | NotifyLeafMapLayoutChange(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3214 | } |
| 3215 | |
| 3216 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3217 | static inline bool EqualImmutableValues(Object* obj1, Object* obj2) { |
| 3218 | if (obj1 == obj2) return true; // Valid for both kData and kAccessor kinds. |
| 3219 | // TODO(ishell): compare AccessorPairs. |
| 3220 | return false; |
| 3221 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3222 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3223 | |
| 3224 | // Installs |new_descriptors| over the current instance_descriptors to ensure |
| 3225 | // proper sharing of descriptor arrays. |
| 3226 | void Map::ReplaceDescriptors(DescriptorArray* new_descriptors, |
| 3227 | LayoutDescriptor* new_layout_descriptor) { |
| 3228 | // Don't overwrite the empty descriptor array or initial map's descriptors. |
| 3229 | if (NumberOfOwnDescriptors() == 0 || GetBackPointer()->IsUndefined()) { |
| 3230 | return; |
| 3231 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3232 | |
| 3233 | DescriptorArray* to_replace = instance_descriptors(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3234 | GetHeap()->incremental_marking()->IterateBlackObject(to_replace); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3235 | Map* current = this; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3236 | while (current->instance_descriptors() == to_replace) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3237 | Object* next = current->GetBackPointer(); |
| 3238 | if (next->IsUndefined()) break; // Stop overwriting at initial map. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3239 | current->SetEnumLength(kInvalidEnumCacheSentinel); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3240 | current->UpdateDescriptors(new_descriptors, new_layout_descriptor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3241 | current = Map::cast(next); |
| 3242 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3243 | set_owns_descriptors(false); |
| 3244 | } |
| 3245 | |
| 3246 | |
| 3247 | Map* Map::FindRootMap() { |
| 3248 | Map* result = this; |
| 3249 | while (true) { |
| 3250 | Object* back = result->GetBackPointer(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3251 | if (back->IsUndefined()) { |
| 3252 | // Initial map always owns descriptors and doesn't have unused entries |
| 3253 | // in the descriptor array. |
| 3254 | DCHECK(result->owns_descriptors()); |
| 3255 | DCHECK_EQ(result->NumberOfOwnDescriptors(), |
| 3256 | result->instance_descriptors()->number_of_descriptors()); |
| 3257 | return result; |
| 3258 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3259 | result = Map::cast(back); |
| 3260 | } |
| 3261 | } |
| 3262 | |
| 3263 | |
| 3264 | Map* Map::FindLastMatchMap(int verbatim, |
| 3265 | int length, |
| 3266 | DescriptorArray* descriptors) { |
| 3267 | DisallowHeapAllocation no_allocation; |
| 3268 | |
| 3269 | // This can only be called on roots of transition trees. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3270 | DCHECK_EQ(verbatim, NumberOfOwnDescriptors()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3271 | |
| 3272 | Map* current = this; |
| 3273 | |
| 3274 | for (int i = verbatim; i < length; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3275 | Name* name = descriptors->GetKey(i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3276 | PropertyDetails details = descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3277 | Map* next = TransitionArray::SearchTransition(current, details.kind(), name, |
| 3278 | details.attributes()); |
| 3279 | if (next == NULL) break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3280 | DescriptorArray* next_descriptors = next->instance_descriptors(); |
| 3281 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3282 | PropertyDetails next_details = next_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3283 | DCHECK_EQ(details.kind(), next_details.kind()); |
| 3284 | DCHECK_EQ(details.attributes(), next_details.attributes()); |
| 3285 | if (details.location() != next_details.location()) break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3286 | if (!details.representation().Equals(next_details.representation())) break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3287 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3288 | if (next_details.location() == kField) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3289 | FieldType* next_field_type = next_descriptors->GetFieldType(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3290 | if (!descriptors->GetFieldType(i)->NowIs(next_field_type)) { |
| 3291 | break; |
| 3292 | } |
| 3293 | } else { |
| 3294 | if (!EqualImmutableValues(descriptors->GetValue(i), |
| 3295 | next_descriptors->GetValue(i))) { |
| 3296 | break; |
| 3297 | } |
| 3298 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3299 | current = next; |
| 3300 | } |
| 3301 | return current; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3302 | } |
| 3303 | |
| 3304 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3305 | Map* Map::FindFieldOwner(int descriptor) { |
| 3306 | DisallowHeapAllocation no_allocation; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3307 | DCHECK_EQ(DATA, instance_descriptors()->GetDetails(descriptor).type()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3308 | Map* result = this; |
| 3309 | while (true) { |
| 3310 | Object* back = result->GetBackPointer(); |
| 3311 | if (back->IsUndefined()) break; |
| 3312 | Map* parent = Map::cast(back); |
| 3313 | if (parent->NumberOfOwnDescriptors() <= descriptor) break; |
| 3314 | result = parent; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3315 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3316 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
| 3319 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3320 | void Map::UpdateFieldType(int descriptor, Handle<Name> name, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3321 | Representation new_representation, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3322 | Handle<Object> new_wrapped_type) { |
| 3323 | DCHECK(new_wrapped_type->IsSmi() || new_wrapped_type->IsWeakCell()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3324 | // We store raw pointers in the queue, so no allocations are allowed. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3325 | DisallowHeapAllocation no_allocation; |
| 3326 | PropertyDetails details = instance_descriptors()->GetDetails(descriptor); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3327 | if (details.type() != DATA) return; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3328 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3329 | Zone zone(GetIsolate()->allocator()); |
| 3330 | ZoneQueue<Map*> backlog(&zone); |
| 3331 | backlog.push(this); |
| 3332 | |
| 3333 | while (!backlog.empty()) { |
| 3334 | Map* current = backlog.front(); |
| 3335 | backlog.pop(); |
| 3336 | |
| 3337 | Object* transitions = current->raw_transitions(); |
| 3338 | int num_transitions = TransitionArray::NumberOfTransitions(transitions); |
| 3339 | for (int i = 0; i < num_transitions; ++i) { |
| 3340 | Map* target = TransitionArray::GetTarget(transitions, i); |
| 3341 | backlog.push(target); |
| 3342 | } |
| 3343 | DescriptorArray* descriptors = current->instance_descriptors(); |
| 3344 | PropertyDetails details = descriptors->GetDetails(descriptor); |
| 3345 | |
| 3346 | // It is allowed to change representation here only from None to something. |
| 3347 | DCHECK(details.representation().Equals(new_representation) || |
| 3348 | details.representation().IsNone()); |
| 3349 | |
| 3350 | // Skip if already updated the shared descriptor. |
| 3351 | if (descriptors->GetValue(descriptor) != *new_wrapped_type) { |
| 3352 | DataDescriptor d(name, descriptors->GetFieldIndex(descriptor), |
| 3353 | new_wrapped_type, details.attributes(), |
| 3354 | new_representation); |
| 3355 | descriptors->Replace(descriptor, &d); |
| 3356 | } |
| 3357 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3360 | bool FieldTypeIsCleared(Representation rep, FieldType* type) { |
| 3361 | return type->IsNone() && rep.IsHeapObject(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3365 | // static |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3366 | Handle<FieldType> Map::GeneralizeFieldType(Representation rep1, |
| 3367 | Handle<FieldType> type1, |
| 3368 | Representation rep2, |
| 3369 | Handle<FieldType> type2, |
| 3370 | Isolate* isolate) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3371 | // Cleared field types need special treatment. They represent lost knowledge, |
| 3372 | // so we must be conservative, so their generalization with any other type |
| 3373 | // is "Any". |
| 3374 | if (FieldTypeIsCleared(rep1, *type1) || FieldTypeIsCleared(rep2, *type2)) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3375 | return FieldType::Any(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3376 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3377 | if (type1->NowIs(type2)) return type2; |
| 3378 | if (type2->NowIs(type1)) return type1; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3379 | return FieldType::Any(isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
| 3382 | |
| 3383 | // static |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3384 | void Map::GeneralizeFieldType(Handle<Map> map, int modify_index, |
| 3385 | Representation new_representation, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3386 | Handle<FieldType> new_field_type) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3387 | Isolate* isolate = map->GetIsolate(); |
| 3388 | |
| 3389 | // Check if we actually need to generalize the field type at all. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3390 | Handle<DescriptorArray> old_descriptors(map->instance_descriptors(), isolate); |
| 3391 | Representation old_representation = |
| 3392 | old_descriptors->GetDetails(modify_index).representation(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3393 | Handle<FieldType> old_field_type(old_descriptors->GetFieldType(modify_index), |
| 3394 | isolate); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3395 | |
| 3396 | if (old_representation.Equals(new_representation) && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3397 | !FieldTypeIsCleared(new_representation, *new_field_type) && |
| 3398 | // Checking old_field_type for being cleared is not necessary because |
| 3399 | // the NowIs check below would fail anyway in that case. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3400 | new_field_type->NowIs(old_field_type)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3401 | DCHECK(Map::GeneralizeFieldType(old_representation, old_field_type, |
| 3402 | new_representation, new_field_type, isolate) |
| 3403 | ->NowIs(old_field_type)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3404 | return; |
| 3405 | } |
| 3406 | |
| 3407 | // Determine the field owner. |
| 3408 | Handle<Map> field_owner(map->FindFieldOwner(modify_index), isolate); |
| 3409 | Handle<DescriptorArray> descriptors( |
| 3410 | field_owner->instance_descriptors(), isolate); |
| 3411 | DCHECK_EQ(*old_field_type, descriptors->GetFieldType(modify_index)); |
| 3412 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3413 | new_field_type = |
| 3414 | Map::GeneralizeFieldType(old_representation, old_field_type, |
| 3415 | new_representation, new_field_type, isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3416 | |
| 3417 | PropertyDetails details = descriptors->GetDetails(modify_index); |
| 3418 | Handle<Name> name(descriptors->GetKey(modify_index)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3419 | |
| 3420 | Handle<Object> wrapped_type(WrapType(new_field_type)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3421 | field_owner->UpdateFieldType(modify_index, name, new_representation, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3422 | wrapped_type); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3423 | field_owner->dependent_code()->DeoptimizeDependentCodeGroup( |
| 3424 | isolate, DependentCode::kFieldTypeGroup); |
| 3425 | |
| 3426 | if (FLAG_trace_generalization) { |
| 3427 | map->PrintGeneralization( |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3428 | stdout, "field type generalization", modify_index, |
| 3429 | map->NumberOfOwnDescriptors(), map->NumberOfOwnDescriptors(), false, |
| 3430 | details.representation(), details.representation(), old_field_type, |
| 3431 | MaybeHandle<Object>(), new_field_type, MaybeHandle<Object>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3432 | } |
| 3433 | } |
| 3434 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3435 | static inline Handle<FieldType> GetFieldType( |
| 3436 | Isolate* isolate, Handle<DescriptorArray> descriptors, int descriptor, |
| 3437 | PropertyLocation location, Representation representation) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3438 | #ifdef DEBUG |
| 3439 | PropertyDetails details = descriptors->GetDetails(descriptor); |
| 3440 | DCHECK_EQ(kData, details.kind()); |
| 3441 | DCHECK_EQ(details.location(), location); |
| 3442 | #endif |
| 3443 | if (location == kField) { |
| 3444 | return handle(descriptors->GetFieldType(descriptor), isolate); |
| 3445 | } else { |
| 3446 | return descriptors->GetValue(descriptor) |
| 3447 | ->OptimalType(isolate, representation); |
| 3448 | } |
| 3449 | } |
| 3450 | |
| 3451 | |
| 3452 | // Reconfigures property at |modify_index| with |new_kind|, |new_attributes|, |
| 3453 | // |store_mode| and/or |new_representation|/|new_field_type|. |
| 3454 | // If |modify_index| is negative then no properties are reconfigured but the |
| 3455 | // map is migrated to the up-to-date non-deprecated state. |
| 3456 | // |
| 3457 | // This method rewrites or completes the transition tree to reflect the new |
| 3458 | // change. To avoid high degrees over polymorphism, and to stabilize quickly, |
| 3459 | // on every rewrite the new type is deduced by merging the current type with |
| 3460 | // any potential new (partial) version of the type in the transition tree. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3461 | // To do this, on each rewrite: |
| 3462 | // - Search the root of the transition tree using FindRootMap. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3463 | // - Find |target_map|, the newest matching version of this map using the |
| 3464 | // virtually "enhanced" |old_map|'s descriptor array (i.e. whose entry at |
| 3465 | // |modify_index| is considered to be of |new_kind| and having |
| 3466 | // |new_attributes|) to walk the transition tree. |
| 3467 | // - Merge/generalize the "enhanced" descriptor array of the |old_map| and |
| 3468 | // descriptor array of the |target_map|. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3469 | // - Generalize the |modify_index| descriptor using |new_representation| and |
| 3470 | // |new_field_type|. |
| 3471 | // - Walk the tree again starting from the root towards |target_map|. Stop at |
| 3472 | // |split_map|, the first map who's descriptor array does not match the merged |
| 3473 | // descriptor array. |
| 3474 | // - If |target_map| == |split_map|, |target_map| is in the expected state. |
| 3475 | // Return it. |
| 3476 | // - Otherwise, invalidate the outdated transition target from |target_map|, and |
| 3477 | // replace its transition tree with a new branch for the updated descriptors. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3478 | Handle<Map> Map::ReconfigureProperty(Handle<Map> old_map, int modify_index, |
| 3479 | PropertyKind new_kind, |
| 3480 | PropertyAttributes new_attributes, |
| 3481 | Representation new_representation, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3482 | Handle<FieldType> new_field_type, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3483 | StoreMode store_mode) { |
| 3484 | DCHECK_NE(kAccessor, new_kind); // TODO(ishell): not supported yet. |
| 3485 | DCHECK(store_mode != FORCE_FIELD || modify_index >= 0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3486 | Isolate* isolate = old_map->GetIsolate(); |
| 3487 | |
| 3488 | Handle<DescriptorArray> old_descriptors( |
| 3489 | old_map->instance_descriptors(), isolate); |
| 3490 | int old_nof = old_map->NumberOfOwnDescriptors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3491 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3492 | // If it's just a representation generalization case (i.e. property kind and |
| 3493 | // attributes stays unchanged) it's fine to transition from None to anything |
| 3494 | // but double without any modification to the object, because the default |
| 3495 | // uninitialized value for representation None can be overwritten by both |
| 3496 | // smi and tagged values. Doubles, however, would require a box allocation. |
| 3497 | if (modify_index >= 0 && !new_representation.IsNone() && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3498 | !new_representation.IsDouble()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3499 | PropertyDetails old_details = old_descriptors->GetDetails(modify_index); |
| 3500 | Representation old_representation = old_details.representation(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3501 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3502 | if (old_representation.IsNone()) { |
| 3503 | DCHECK_EQ(new_kind, old_details.kind()); |
| 3504 | DCHECK_EQ(new_attributes, old_details.attributes()); |
| 3505 | DCHECK_EQ(DATA, old_details.type()); |
| 3506 | if (FLAG_trace_generalization) { |
| 3507 | old_map->PrintGeneralization( |
| 3508 | stdout, "uninitialized field", modify_index, |
| 3509 | old_map->NumberOfOwnDescriptors(), |
| 3510 | old_map->NumberOfOwnDescriptors(), false, old_representation, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3511 | new_representation, |
| 3512 | handle(old_descriptors->GetFieldType(modify_index), isolate), |
| 3513 | MaybeHandle<Object>(), new_field_type, MaybeHandle<Object>()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3514 | } |
| 3515 | Handle<Map> field_owner(old_map->FindFieldOwner(modify_index), isolate); |
| 3516 | |
| 3517 | GeneralizeFieldType(field_owner, modify_index, new_representation, |
| 3518 | new_field_type); |
| 3519 | DCHECK(old_descriptors->GetDetails(modify_index) |
| 3520 | .representation() |
| 3521 | .Equals(new_representation)); |
| 3522 | DCHECK( |
| 3523 | old_descriptors->GetFieldType(modify_index)->NowIs(new_field_type)); |
| 3524 | return old_map; |
| 3525 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | // Check the state of the root map. |
| 3529 | Handle<Map> root_map(old_map->FindRootMap(), isolate); |
| 3530 | if (!old_map->EquivalentToForTransition(*root_map)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3531 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3532 | new_kind, new_attributes, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3533 | "GenAll_NotEquivalent"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3534 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3535 | |
| 3536 | ElementsKind from_kind = root_map->elements_kind(); |
| 3537 | ElementsKind to_kind = old_map->elements_kind(); |
| 3538 | // TODO(ishell): Add a test for SLOW_SLOPPY_ARGUMENTS_ELEMENTS. |
| 3539 | if (from_kind != to_kind && to_kind != DICTIONARY_ELEMENTS && |
| 3540 | to_kind != SLOW_SLOPPY_ARGUMENTS_ELEMENTS && |
| 3541 | !(IsTransitionableFastElementsKind(from_kind) && |
| 3542 | IsMoreGeneralElementsKindTransition(from_kind, to_kind))) { |
| 3543 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
| 3544 | new_kind, new_attributes, |
| 3545 | "GenAll_InvalidElementsTransition"); |
| 3546 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3547 | int root_nof = root_map->NumberOfOwnDescriptors(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3548 | if (modify_index >= 0 && modify_index < root_nof) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3549 | PropertyDetails old_details = old_descriptors->GetDetails(modify_index); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3550 | if (old_details.kind() != new_kind || |
| 3551 | old_details.attributes() != new_attributes) { |
| 3552 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
| 3553 | new_kind, new_attributes, |
| 3554 | "GenAll_RootModification1"); |
| 3555 | } |
| 3556 | if ((old_details.type() != DATA && store_mode == FORCE_FIELD) || |
| 3557 | (old_details.type() == DATA && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3558 | (!new_field_type->NowIs(old_descriptors->GetFieldType(modify_index)) || |
| 3559 | !new_representation.fits_into(old_details.representation())))) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3560 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3561 | new_kind, new_attributes, |
| 3562 | "GenAll_RootModification2"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3563 | } |
| 3564 | } |
| 3565 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3566 | // From here on, use the map with correct elements kind as root map. |
| 3567 | if (from_kind != to_kind) { |
| 3568 | root_map = Map::AsElementsKind(root_map, to_kind); |
| 3569 | } |
| 3570 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3571 | Handle<Map> target_map = root_map; |
| 3572 | for (int i = root_nof; i < old_nof; ++i) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3573 | PropertyDetails old_details = old_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3574 | PropertyKind next_kind; |
| 3575 | PropertyLocation next_location; |
| 3576 | PropertyAttributes next_attributes; |
| 3577 | Representation next_representation; |
| 3578 | bool property_kind_reconfiguration = false; |
| 3579 | |
| 3580 | if (modify_index == i) { |
| 3581 | DCHECK_EQ(FORCE_FIELD, store_mode); |
| 3582 | property_kind_reconfiguration = old_details.kind() != new_kind; |
| 3583 | |
| 3584 | next_kind = new_kind; |
| 3585 | next_location = kField; |
| 3586 | next_attributes = new_attributes; |
| 3587 | // If property kind is not reconfigured merge the result with |
| 3588 | // representation/field type from the old descriptor. |
| 3589 | next_representation = new_representation; |
| 3590 | if (!property_kind_reconfiguration) { |
| 3591 | next_representation = |
| 3592 | next_representation.generalize(old_details.representation()); |
| 3593 | } |
| 3594 | |
| 3595 | } else { |
| 3596 | next_kind = old_details.kind(); |
| 3597 | next_location = old_details.location(); |
| 3598 | next_attributes = old_details.attributes(); |
| 3599 | next_representation = old_details.representation(); |
| 3600 | } |
| 3601 | Map* transition = TransitionArray::SearchTransition( |
| 3602 | *target_map, next_kind, old_descriptors->GetKey(i), next_attributes); |
| 3603 | if (transition == NULL) break; |
| 3604 | Handle<Map> tmp_map(transition, isolate); |
| 3605 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3606 | Handle<DescriptorArray> tmp_descriptors = handle( |
| 3607 | tmp_map->instance_descriptors(), isolate); |
| 3608 | |
| 3609 | // Check if target map is incompatible. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3610 | PropertyDetails tmp_details = tmp_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3611 | DCHECK_EQ(next_kind, tmp_details.kind()); |
| 3612 | DCHECK_EQ(next_attributes, tmp_details.attributes()); |
| 3613 | if (next_kind == kAccessor && |
| 3614 | !EqualImmutableValues(old_descriptors->GetValue(i), |
| 3615 | tmp_descriptors->GetValue(i))) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3616 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3617 | new_kind, new_attributes, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3618 | "GenAll_Incompatible"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3619 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3620 | if (next_location == kField && tmp_details.location() == kDescriptor) break; |
| 3621 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3622 | Representation tmp_representation = tmp_details.representation(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3623 | if (!next_representation.fits_into(tmp_representation)) break; |
| 3624 | |
| 3625 | PropertyLocation old_location = old_details.location(); |
| 3626 | PropertyLocation tmp_location = tmp_details.location(); |
| 3627 | if (tmp_location == kField) { |
| 3628 | if (next_kind == kData) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3629 | Handle<FieldType> next_field_type; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3630 | if (modify_index == i) { |
| 3631 | next_field_type = new_field_type; |
| 3632 | if (!property_kind_reconfiguration) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3633 | Handle<FieldType> old_field_type = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3634 | GetFieldType(isolate, old_descriptors, i, |
| 3635 | old_details.location(), tmp_representation); |
| 3636 | Representation old_representation = old_details.representation(); |
| 3637 | next_field_type = GeneralizeFieldType( |
| 3638 | old_representation, old_field_type, new_representation, |
| 3639 | next_field_type, isolate); |
| 3640 | } |
| 3641 | } else { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3642 | Handle<FieldType> old_field_type = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3643 | GetFieldType(isolate, old_descriptors, i, old_details.location(), |
| 3644 | tmp_representation); |
| 3645 | next_field_type = old_field_type; |
| 3646 | } |
| 3647 | GeneralizeFieldType(tmp_map, i, tmp_representation, next_field_type); |
| 3648 | } |
| 3649 | } else if (old_location == kField || |
| 3650 | !EqualImmutableValues(old_descriptors->GetValue(i), |
| 3651 | tmp_descriptors->GetValue(i))) { |
Ben Murdoch | 7d3e7fc | 2011-07-12 16:37:06 +0100 | [diff] [blame] | 3652 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3653 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3654 | DCHECK(!tmp_map->is_deprecated()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3655 | target_map = tmp_map; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3656 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3657 | |
| 3658 | // Directly change the map if the target map is more general. |
| 3659 | Handle<DescriptorArray> target_descriptors( |
| 3660 | target_map->instance_descriptors(), isolate); |
| 3661 | int target_nof = target_map->NumberOfOwnDescriptors(); |
| 3662 | if (target_nof == old_nof && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3663 | (store_mode != FORCE_FIELD || |
| 3664 | (modify_index >= 0 && |
| 3665 | target_descriptors->GetDetails(modify_index).location() == kField))) { |
| 3666 | #ifdef DEBUG |
| 3667 | if (modify_index >= 0) { |
| 3668 | PropertyDetails details = target_descriptors->GetDetails(modify_index); |
| 3669 | DCHECK_EQ(new_kind, details.kind()); |
| 3670 | DCHECK_EQ(new_attributes, details.attributes()); |
| 3671 | DCHECK(new_representation.fits_into(details.representation())); |
| 3672 | DCHECK(details.location() != kField || |
| 3673 | new_field_type->NowIs( |
| 3674 | target_descriptors->GetFieldType(modify_index))); |
| 3675 | } |
| 3676 | #endif |
| 3677 | if (*target_map != *old_map) { |
| 3678 | old_map->NotifyLeafMapLayoutChange(); |
| 3679 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3680 | return target_map; |
| 3681 | } |
| 3682 | |
| 3683 | // Find the last compatible target map in the transition tree. |
| 3684 | for (int i = target_nof; i < old_nof; ++i) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3685 | PropertyDetails old_details = old_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3686 | PropertyKind next_kind; |
| 3687 | PropertyAttributes next_attributes; |
| 3688 | if (modify_index == i) { |
| 3689 | next_kind = new_kind; |
| 3690 | next_attributes = new_attributes; |
| 3691 | } else { |
| 3692 | next_kind = old_details.kind(); |
| 3693 | next_attributes = old_details.attributes(); |
| 3694 | } |
| 3695 | Map* transition = TransitionArray::SearchTransition( |
| 3696 | *target_map, next_kind, old_descriptors->GetKey(i), next_attributes); |
| 3697 | if (transition == NULL) break; |
| 3698 | Handle<Map> tmp_map(transition, isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3699 | Handle<DescriptorArray> tmp_descriptors( |
| 3700 | tmp_map->instance_descriptors(), isolate); |
| 3701 | |
| 3702 | // Check if target map is compatible. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3703 | #ifdef DEBUG |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3704 | PropertyDetails tmp_details = tmp_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3705 | DCHECK_EQ(next_kind, tmp_details.kind()); |
| 3706 | DCHECK_EQ(next_attributes, tmp_details.attributes()); |
| 3707 | #endif |
| 3708 | if (next_kind == kAccessor && |
| 3709 | !EqualImmutableValues(old_descriptors->GetValue(i), |
| 3710 | tmp_descriptors->GetValue(i))) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3711 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3712 | new_kind, new_attributes, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3713 | "GenAll_Incompatible"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3714 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3715 | DCHECK(!tmp_map->is_deprecated()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3716 | target_map = tmp_map; |
| 3717 | } |
| 3718 | target_nof = target_map->NumberOfOwnDescriptors(); |
| 3719 | target_descriptors = handle(target_map->instance_descriptors(), isolate); |
| 3720 | |
| 3721 | // Allocate a new descriptor array large enough to hold the required |
| 3722 | // descriptors, with minimally the exact same size as the old descriptor |
| 3723 | // array. |
| 3724 | int new_slack = Max( |
| 3725 | old_nof, old_descriptors->number_of_descriptors()) - old_nof; |
| 3726 | Handle<DescriptorArray> new_descriptors = DescriptorArray::Allocate( |
| 3727 | isolate, old_nof, new_slack); |
| 3728 | DCHECK(new_descriptors->length() > target_descriptors->length() || |
| 3729 | new_descriptors->NumberOfSlackDescriptors() > 0 || |
| 3730 | new_descriptors->number_of_descriptors() == |
| 3731 | old_descriptors->number_of_descriptors()); |
| 3732 | DCHECK(new_descriptors->number_of_descriptors() == old_nof); |
| 3733 | |
| 3734 | // 0 -> |root_nof| |
| 3735 | int current_offset = 0; |
| 3736 | for (int i = 0; i < root_nof; ++i) { |
| 3737 | PropertyDetails old_details = old_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3738 | if (old_details.location() == kField) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3739 | current_offset += old_details.field_width_in_words(); |
| 3740 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3741 | Descriptor d(handle(old_descriptors->GetKey(i), isolate), |
| 3742 | handle(old_descriptors->GetValue(i), isolate), |
| 3743 | old_details); |
| 3744 | new_descriptors->Set(i, &d); |
| 3745 | } |
| 3746 | |
| 3747 | // |root_nof| -> |target_nof| |
| 3748 | for (int i = root_nof; i < target_nof; ++i) { |
| 3749 | Handle<Name> target_key(target_descriptors->GetKey(i), isolate); |
| 3750 | PropertyDetails old_details = old_descriptors->GetDetails(i); |
| 3751 | PropertyDetails target_details = target_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3752 | |
| 3753 | PropertyKind next_kind; |
| 3754 | PropertyAttributes next_attributes; |
| 3755 | PropertyLocation next_location; |
| 3756 | Representation next_representation; |
| 3757 | bool property_kind_reconfiguration = false; |
| 3758 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3759 | if (modify_index == i) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3760 | DCHECK_EQ(FORCE_FIELD, store_mode); |
| 3761 | property_kind_reconfiguration = old_details.kind() != new_kind; |
| 3762 | |
| 3763 | next_kind = new_kind; |
| 3764 | next_attributes = new_attributes; |
| 3765 | next_location = kField; |
| 3766 | |
| 3767 | // Merge new representation/field type with ones from the target |
| 3768 | // descriptor. If property kind is not reconfigured merge the result with |
| 3769 | // representation/field type from the old descriptor. |
| 3770 | next_representation = |
| 3771 | new_representation.generalize(target_details.representation()); |
| 3772 | if (!property_kind_reconfiguration) { |
| 3773 | next_representation = |
| 3774 | next_representation.generalize(old_details.representation()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3775 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3776 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3777 | // Merge old_descriptor and target_descriptor entries. |
| 3778 | DCHECK_EQ(target_details.kind(), old_details.kind()); |
| 3779 | next_kind = target_details.kind(); |
| 3780 | next_attributes = target_details.attributes(); |
| 3781 | next_location = |
| 3782 | old_details.location() == kField || |
| 3783 | target_details.location() == kField || |
| 3784 | !EqualImmutableValues(target_descriptors->GetValue(i), |
| 3785 | old_descriptors->GetValue(i)) |
| 3786 | ? kField |
| 3787 | : kDescriptor; |
| 3788 | |
| 3789 | next_representation = old_details.representation().generalize( |
| 3790 | target_details.representation()); |
| 3791 | } |
| 3792 | DCHECK_EQ(next_kind, target_details.kind()); |
| 3793 | DCHECK_EQ(next_attributes, target_details.attributes()); |
| 3794 | |
| 3795 | if (next_location == kField) { |
| 3796 | if (next_kind == kData) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3797 | Handle<FieldType> target_field_type = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3798 | GetFieldType(isolate, target_descriptors, i, |
| 3799 | target_details.location(), next_representation); |
| 3800 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3801 | Handle<FieldType> next_field_type; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3802 | if (modify_index == i) { |
| 3803 | next_field_type = GeneralizeFieldType( |
| 3804 | target_details.representation(), target_field_type, |
| 3805 | new_representation, new_field_type, isolate); |
| 3806 | if (!property_kind_reconfiguration) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3807 | Handle<FieldType> old_field_type = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3808 | GetFieldType(isolate, old_descriptors, i, |
| 3809 | old_details.location(), next_representation); |
| 3810 | next_field_type = GeneralizeFieldType( |
| 3811 | old_details.representation(), old_field_type, |
| 3812 | next_representation, next_field_type, isolate); |
| 3813 | } |
| 3814 | } else { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3815 | Handle<FieldType> old_field_type = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3816 | GetFieldType(isolate, old_descriptors, i, old_details.location(), |
| 3817 | next_representation); |
| 3818 | next_field_type = GeneralizeFieldType( |
| 3819 | old_details.representation(), old_field_type, next_representation, |
| 3820 | target_field_type, isolate); |
| 3821 | } |
| 3822 | Handle<Object> wrapped_type(WrapType(next_field_type)); |
| 3823 | DataDescriptor d(target_key, current_offset, wrapped_type, |
| 3824 | next_attributes, next_representation); |
| 3825 | current_offset += d.GetDetails().field_width_in_words(); |
| 3826 | new_descriptors->Set(i, &d); |
| 3827 | } else { |
| 3828 | UNIMPLEMENTED(); // TODO(ishell): implement. |
| 3829 | } |
| 3830 | } else { |
| 3831 | PropertyDetails details(next_attributes, next_kind, next_location, |
| 3832 | next_representation); |
| 3833 | Descriptor d(target_key, handle(target_descriptors->GetValue(i), isolate), |
| 3834 | details); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3835 | new_descriptors->Set(i, &d); |
| 3836 | } |
| 3837 | } |
| 3838 | |
| 3839 | // |target_nof| -> |old_nof| |
| 3840 | for (int i = target_nof; i < old_nof; ++i) { |
| 3841 | PropertyDetails old_details = old_descriptors->GetDetails(i); |
| 3842 | Handle<Name> old_key(old_descriptors->GetKey(i), isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3843 | |
| 3844 | // Merge old_descriptor entry and modified details together. |
| 3845 | PropertyKind next_kind; |
| 3846 | PropertyAttributes next_attributes; |
| 3847 | PropertyLocation next_location; |
| 3848 | Representation next_representation; |
| 3849 | bool property_kind_reconfiguration = false; |
| 3850 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3851 | if (modify_index == i) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3852 | DCHECK_EQ(FORCE_FIELD, store_mode); |
| 3853 | // In case of property kind reconfiguration it is not necessary to |
| 3854 | // take into account representation/field type of the old descriptor. |
| 3855 | property_kind_reconfiguration = old_details.kind() != new_kind; |
| 3856 | |
| 3857 | next_kind = new_kind; |
| 3858 | next_attributes = new_attributes; |
| 3859 | next_location = kField; |
| 3860 | next_representation = new_representation; |
| 3861 | if (!property_kind_reconfiguration) { |
| 3862 | next_representation = |
| 3863 | next_representation.generalize(old_details.representation()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3864 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3865 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3866 | next_kind = old_details.kind(); |
| 3867 | next_attributes = old_details.attributes(); |
| 3868 | next_location = old_details.location(); |
| 3869 | next_representation = old_details.representation(); |
| 3870 | } |
| 3871 | |
| 3872 | if (next_location == kField) { |
| 3873 | if (next_kind == kData) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3874 | Handle<FieldType> next_field_type; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3875 | if (modify_index == i) { |
| 3876 | next_field_type = new_field_type; |
| 3877 | if (!property_kind_reconfiguration) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3878 | Handle<FieldType> old_field_type = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3879 | GetFieldType(isolate, old_descriptors, i, |
| 3880 | old_details.location(), next_representation); |
| 3881 | next_field_type = GeneralizeFieldType( |
| 3882 | old_details.representation(), old_field_type, |
| 3883 | next_representation, next_field_type, isolate); |
| 3884 | } |
| 3885 | } else { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3886 | Handle<FieldType> old_field_type = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3887 | GetFieldType(isolate, old_descriptors, i, old_details.location(), |
| 3888 | next_representation); |
| 3889 | next_field_type = old_field_type; |
| 3890 | } |
| 3891 | |
| 3892 | Handle<Object> wrapped_type(WrapType(next_field_type)); |
| 3893 | |
| 3894 | DataDescriptor d(old_key, current_offset, wrapped_type, next_attributes, |
| 3895 | next_representation); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3896 | current_offset += d.GetDetails().field_width_in_words(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3897 | new_descriptors->Set(i, &d); |
| 3898 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3899 | UNIMPLEMENTED(); // TODO(ishell): implement. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3900 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3901 | } else { |
| 3902 | PropertyDetails details(next_attributes, next_kind, next_location, |
| 3903 | next_representation); |
| 3904 | Descriptor d(old_key, handle(old_descriptors->GetValue(i), isolate), |
| 3905 | details); |
| 3906 | new_descriptors->Set(i, &d); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3907 | } |
| 3908 | } |
| 3909 | |
| 3910 | new_descriptors->Sort(); |
| 3911 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3912 | DCHECK(store_mode != FORCE_FIELD || |
| 3913 | new_descriptors->GetDetails(modify_index).location() == kField); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3914 | |
| 3915 | Handle<Map> split_map(root_map->FindLastMatchMap( |
| 3916 | root_nof, old_nof, *new_descriptors), isolate); |
| 3917 | int split_nof = split_map->NumberOfOwnDescriptors(); |
| 3918 | DCHECK_NE(old_nof, split_nof); |
| 3919 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3920 | PropertyKind split_kind; |
| 3921 | PropertyAttributes split_attributes; |
| 3922 | if (modify_index == split_nof) { |
| 3923 | split_kind = new_kind; |
| 3924 | split_attributes = new_attributes; |
| 3925 | } else { |
| 3926 | PropertyDetails split_prop_details = old_descriptors->GetDetails(split_nof); |
| 3927 | split_kind = split_prop_details.kind(); |
| 3928 | split_attributes = split_prop_details.attributes(); |
| 3929 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3930 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3931 | // Invalidate a transition target at |key|. |
| 3932 | Map* maybe_transition = TransitionArray::SearchTransition( |
| 3933 | *split_map, split_kind, old_descriptors->GetKey(split_nof), |
| 3934 | split_attributes); |
| 3935 | if (maybe_transition != NULL) { |
| 3936 | maybe_transition->DeprecateTransitionTree(); |
| 3937 | } |
| 3938 | |
| 3939 | // If |maybe_transition| is not NULL then the transition array already |
| 3940 | // contains entry for given descriptor. This means that the transition |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3941 | // could be inserted regardless of whether transitions array is full or not. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3942 | if (maybe_transition == NULL && |
| 3943 | !TransitionArray::CanHaveMoreTransitions(split_map)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3944 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3945 | new_kind, new_attributes, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3946 | "GenAll_CantHaveMoreTransitions"); |
| 3947 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3948 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3949 | old_map->NotifyLeafMapLayoutChange(); |
| 3950 | |
| 3951 | if (FLAG_trace_generalization && modify_index >= 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3952 | PropertyDetails old_details = old_descriptors->GetDetails(modify_index); |
| 3953 | PropertyDetails new_details = new_descriptors->GetDetails(modify_index); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3954 | MaybeHandle<FieldType> old_field_type; |
| 3955 | MaybeHandle<FieldType> new_field_type; |
| 3956 | MaybeHandle<Object> old_value; |
| 3957 | MaybeHandle<Object> new_value; |
| 3958 | if (old_details.type() == DATA) { |
| 3959 | old_field_type = |
| 3960 | handle(old_descriptors->GetFieldType(modify_index), isolate); |
| 3961 | } else { |
| 3962 | old_value = handle(old_descriptors->GetValue(modify_index), isolate); |
| 3963 | } |
| 3964 | if (new_details.type() == DATA) { |
| 3965 | new_field_type = |
| 3966 | handle(new_descriptors->GetFieldType(modify_index), isolate); |
| 3967 | } else { |
| 3968 | new_value = handle(new_descriptors->GetValue(modify_index), isolate); |
| 3969 | } |
| 3970 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3971 | old_map->PrintGeneralization( |
| 3972 | stdout, "", modify_index, split_nof, old_nof, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3973 | old_details.location() == kDescriptor && store_mode == FORCE_FIELD, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3974 | old_details.representation(), new_details.representation(), |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3975 | old_field_type, old_value, new_field_type, new_value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3978 | Handle<LayoutDescriptor> new_layout_descriptor = |
| 3979 | LayoutDescriptor::New(split_map, new_descriptors, old_nof); |
| 3980 | |
| 3981 | Handle<Map> new_map = |
| 3982 | AddMissingTransitions(split_map, new_descriptors, new_layout_descriptor); |
| 3983 | |
| 3984 | // Deprecated part of the transition tree is no longer reachable, so replace |
| 3985 | // current instance descriptors in the "survived" part of the tree with |
| 3986 | // the new descriptors to maintain descriptors sharing invariant. |
| 3987 | split_map->ReplaceDescriptors(*new_descriptors, *new_layout_descriptor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3988 | return new_map; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3989 | } |
| 3990 | |
| 3991 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3992 | // Generalize the representation of all DATA descriptors. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3993 | Handle<Map> Map::GeneralizeAllFieldRepresentations( |
| 3994 | Handle<Map> map) { |
| 3995 | Handle<DescriptorArray> descriptors(map->instance_descriptors()); |
| 3996 | for (int i = 0; i < map->NumberOfOwnDescriptors(); ++i) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3997 | PropertyDetails details = descriptors->GetDetails(i); |
| 3998 | if (details.type() == DATA) { |
| 3999 | map = ReconfigureProperty(map, i, kData, details.attributes(), |
| 4000 | Representation::Tagged(), |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4001 | FieldType::Any(map->GetIsolate()), FORCE_FIELD); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4002 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4003 | } |
| 4004 | return map; |
| 4005 | } |
| 4006 | |
| 4007 | |
| 4008 | // static |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4009 | MaybeHandle<Map> Map::TryUpdate(Handle<Map> old_map) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4010 | DisallowHeapAllocation no_allocation; |
| 4011 | DisallowDeoptimization no_deoptimization(old_map->GetIsolate()); |
| 4012 | |
| 4013 | if (!old_map->is_deprecated()) return old_map; |
| 4014 | |
| 4015 | // Check the state of the root map. |
| 4016 | Map* root_map = old_map->FindRootMap(); |
| 4017 | if (!old_map->EquivalentToForTransition(root_map)) return MaybeHandle<Map>(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4018 | |
| 4019 | ElementsKind from_kind = root_map->elements_kind(); |
| 4020 | ElementsKind to_kind = old_map->elements_kind(); |
| 4021 | if (from_kind != to_kind) { |
| 4022 | // Try to follow existing elements kind transitions. |
| 4023 | root_map = root_map->LookupElementsTransitionMap(to_kind); |
| 4024 | if (root_map == NULL) return MaybeHandle<Map>(); |
| 4025 | // From here on, use the map with correct elements kind as root map. |
| 4026 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4027 | int root_nof = root_map->NumberOfOwnDescriptors(); |
| 4028 | |
| 4029 | int old_nof = old_map->NumberOfOwnDescriptors(); |
| 4030 | DescriptorArray* old_descriptors = old_map->instance_descriptors(); |
| 4031 | |
| 4032 | Map* new_map = root_map; |
| 4033 | for (int i = root_nof; i < old_nof; ++i) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4034 | PropertyDetails old_details = old_descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4035 | Map* transition = TransitionArray::SearchTransition( |
| 4036 | new_map, old_details.kind(), old_descriptors->GetKey(i), |
| 4037 | old_details.attributes()); |
| 4038 | if (transition == NULL) return MaybeHandle<Map>(); |
| 4039 | new_map = transition; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4040 | DescriptorArray* new_descriptors = new_map->instance_descriptors(); |
| 4041 | |
| 4042 | PropertyDetails new_details = new_descriptors->GetDetails(i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4043 | DCHECK_EQ(old_details.kind(), new_details.kind()); |
| 4044 | DCHECK_EQ(old_details.attributes(), new_details.attributes()); |
| 4045 | if (!old_details.representation().fits_into(new_details.representation())) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4046 | return MaybeHandle<Map>(); |
| 4047 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4048 | switch (new_details.type()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4049 | case DATA: { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4050 | FieldType* new_type = new_descriptors->GetFieldType(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4051 | // Cleared field types need special treatment. They represent lost |
| 4052 | // knowledge, so we must first generalize the new_type to "Any". |
| 4053 | if (FieldTypeIsCleared(new_details.representation(), new_type)) { |
| 4054 | return MaybeHandle<Map>(); |
| 4055 | } |
| 4056 | PropertyType old_property_type = old_details.type(); |
| 4057 | if (old_property_type == DATA) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4058 | FieldType* old_type = old_descriptors->GetFieldType(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4059 | if (FieldTypeIsCleared(old_details.representation(), old_type) || |
| 4060 | !old_type->NowIs(new_type)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4061 | return MaybeHandle<Map>(); |
| 4062 | } |
| 4063 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4064 | DCHECK(old_property_type == DATA_CONSTANT); |
| 4065 | Object* old_value = old_descriptors->GetValue(i); |
| 4066 | if (!new_type->NowContains(old_value)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4067 | return MaybeHandle<Map>(); |
| 4068 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4069 | } |
| 4070 | break; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4071 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4072 | case ACCESSOR: { |
| 4073 | #ifdef DEBUG |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4074 | FieldType* new_type = new_descriptors->GetFieldType(i); |
| 4075 | DCHECK(new_type->IsAny()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4076 | #endif |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4077 | break; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4078 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4079 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4080 | case DATA_CONSTANT: |
| 4081 | case ACCESSOR_CONSTANT: { |
| 4082 | Object* old_value = old_descriptors->GetValue(i); |
| 4083 | Object* new_value = new_descriptors->GetValue(i); |
| 4084 | if (old_details.location() == kField || old_value != new_value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4085 | return MaybeHandle<Map>(); |
| 4086 | } |
| 4087 | break; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4088 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4089 | } |
| 4090 | } |
| 4091 | if (new_map->NumberOfOwnDescriptors() != old_nof) return MaybeHandle<Map>(); |
| 4092 | return handle(new_map); |
| 4093 | } |
| 4094 | |
| 4095 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4096 | // static |
| 4097 | Handle<Map> Map::Update(Handle<Map> map) { |
| 4098 | if (!map->is_deprecated()) return map; |
| 4099 | return ReconfigureProperty(map, -1, kData, NONE, Representation::None(), |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4100 | FieldType::None(map->GetIsolate()), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4101 | ALLOW_IN_DESCRIPTOR); |
| 4102 | } |
| 4103 | |
| 4104 | |
| 4105 | Maybe<bool> JSObject::SetPropertyWithInterceptor(LookupIterator* it, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4106 | ShouldThrow should_throw, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4107 | Handle<Object> value) { |
| 4108 | Isolate* isolate = it->isolate(); |
| 4109 | // Make sure that the top context does not change when doing callbacks or |
| 4110 | // interceptor calls. |
| 4111 | AssertNoContextChange ncc(isolate); |
| 4112 | |
| 4113 | DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state()); |
| 4114 | Handle<InterceptorInfo> interceptor(it->GetInterceptor()); |
| 4115 | if (interceptor->setter()->IsUndefined()) return Just(false); |
| 4116 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4117 | Handle<JSObject> holder = it->GetHolder<JSObject>(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4118 | bool result; |
| 4119 | Handle<Object> receiver = it->GetReceiver(); |
| 4120 | if (!receiver->IsJSReceiver()) { |
| 4121 | ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, receiver, |
| 4122 | Object::ConvertReceiver(isolate, receiver), |
| 4123 | Nothing<bool>()); |
| 4124 | } |
| 4125 | PropertyCallbackArguments args(isolate, interceptor->data(), *receiver, |
| 4126 | *holder, should_throw); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4127 | |
| 4128 | if (it->IsElement()) { |
| 4129 | uint32_t index = it->index(); |
| 4130 | v8::IndexedPropertySetterCallback setter = |
| 4131 | v8::ToCData<v8::IndexedPropertySetterCallback>(interceptor->setter()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4132 | // TODO(neis): In the future, we may want to actually return the |
| 4133 | // interceptor's result, which then should be a boolean. |
| 4134 | result = !args.Call(setter, index, value).is_null(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4135 | } else { |
| 4136 | Handle<Name> name = it->name(); |
| 4137 | DCHECK(!name->IsPrivate()); |
| 4138 | |
| 4139 | if (name->IsSymbol() && !interceptor->can_intercept_symbols()) { |
| 4140 | return Just(false); |
| 4141 | } |
| 4142 | |
| 4143 | v8::GenericNamedPropertySetterCallback setter = |
| 4144 | v8::ToCData<v8::GenericNamedPropertySetterCallback>( |
| 4145 | interceptor->setter()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4146 | result = !args.Call(setter, name, value).is_null(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4147 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4148 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4149 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(it->isolate(), Nothing<bool>()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4150 | return Just(result); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4151 | } |
| 4152 | |
| 4153 | |
| 4154 | MaybeHandle<Object> Object::SetProperty(Handle<Object> object, |
| 4155 | Handle<Name> name, Handle<Object> value, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4156 | LanguageMode language_mode, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4157 | StoreFromKeyed store_mode) { |
| 4158 | LookupIterator it(object, name); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4159 | MAYBE_RETURN_NULL(SetProperty(&it, value, language_mode, store_mode)); |
| 4160 | return value; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
| 4163 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4164 | Maybe<bool> Object::SetPropertyInternal(LookupIterator* it, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4165 | Handle<Object> value, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4166 | LanguageMode language_mode, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4167 | StoreFromKeyed store_mode, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4168 | bool* found) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4169 | DCHECK(it->IsFound()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4170 | ShouldThrow should_throw = |
| 4171 | is_sloppy(language_mode) ? DONT_THROW : THROW_ON_ERROR; |
| 4172 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4173 | // Make sure that the top context does not change when doing callbacks or |
| 4174 | // interceptor calls. |
| 4175 | AssertNoContextChange ncc(it->isolate()); |
| 4176 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4177 | do { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4178 | switch (it->state()) { |
| 4179 | case LookupIterator::NOT_FOUND: |
| 4180 | UNREACHABLE(); |
| 4181 | |
| 4182 | case LookupIterator::ACCESS_CHECK: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4183 | if (it->HasAccess()) break; |
| 4184 | // Check whether it makes sense to reuse the lookup iterator. Here it |
| 4185 | // might still call into setters up the prototype chain. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4186 | return JSObject::SetPropertyWithFailedAccessCheck(it, value, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4187 | should_throw); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4188 | |
| 4189 | case LookupIterator::JSPROXY: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4190 | return JSProxy::SetProperty(it->GetHolder<JSProxy>(), it->GetName(), |
| 4191 | value, it->GetReceiver(), language_mode); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4192 | |
| 4193 | case LookupIterator::INTERCEPTOR: |
| 4194 | if (it->HolderIsReceiverOrHiddenPrototype()) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4195 | Maybe<bool> result = |
| 4196 | JSObject::SetPropertyWithInterceptor(it, should_throw, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4197 | if (result.IsNothing() || result.FromJust()) return result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4198 | } else { |
| 4199 | Maybe<PropertyAttributes> maybe_attributes = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4200 | JSObject::GetPropertyAttributesWithInterceptor(it); |
| 4201 | if (!maybe_attributes.IsJust()) return Nothing<bool>(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4202 | if (maybe_attributes.FromJust() == ABSENT) break; |
| 4203 | if ((maybe_attributes.FromJust() & READ_ONLY) != 0) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4204 | return WriteToReadOnlyProperty(it, value, should_throw); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4205 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4206 | *found = false; |
| 4207 | return Nothing<bool>(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4208 | } |
| 4209 | break; |
| 4210 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4211 | case LookupIterator::ACCESSOR: { |
| 4212 | if (it->IsReadOnly()) { |
| 4213 | return WriteToReadOnlyProperty(it, value, should_throw); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4214 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4215 | Handle<Object> accessors = it->GetAccessors(); |
| 4216 | if (accessors->IsAccessorInfo() && |
| 4217 | !it->HolderIsReceiverOrHiddenPrototype() && |
| 4218 | AccessorInfo::cast(*accessors)->is_special_data_property()) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4219 | *found = false; |
| 4220 | return Nothing<bool>(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4221 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4222 | return SetPropertyWithAccessor(it, value, should_throw); |
| 4223 | } |
| 4224 | case LookupIterator::INTEGER_INDEXED_EXOTIC: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4225 | // TODO(verwaest): We should throw an exception if holder is receiver. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4226 | return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4227 | |
| 4228 | case LookupIterator::DATA: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4229 | if (it->IsReadOnly()) { |
| 4230 | return WriteToReadOnlyProperty(it, value, should_throw); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4231 | } |
| 4232 | if (it->HolderIsReceiverOrHiddenPrototype()) { |
| 4233 | return SetDataProperty(it, value); |
| 4234 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4235 | // Fall through. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4236 | case LookupIterator::TRANSITION: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4237 | *found = false; |
| 4238 | return Nothing<bool>(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4239 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4240 | it->Next(); |
| 4241 | } while (it->IsFound()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4242 | |
| 4243 | *found = false; |
| 4244 | return Nothing<bool>(); |
| 4245 | } |
| 4246 | |
| 4247 | |
| 4248 | Maybe<bool> Object::SetProperty(LookupIterator* it, Handle<Object> value, |
| 4249 | LanguageMode language_mode, |
| 4250 | StoreFromKeyed store_mode) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4251 | it->UpdateProtector(); |
| 4252 | if (it->IsFound()) { |
| 4253 | bool found = true; |
| 4254 | Maybe<bool> result = |
| 4255 | SetPropertyInternal(it, value, language_mode, store_mode, &found); |
| 4256 | if (found) return result; |
| 4257 | } |
| 4258 | |
| 4259 | // If the receiver is the JSGlobalObject, the store was contextual. In case |
| 4260 | // the property did not exist yet on the global object itself, we have to |
| 4261 | // throw a reference error in strict mode. In sloppy mode, we continue. |
| 4262 | if (is_strict(language_mode) && it->GetReceiver()->IsJSGlobalObject()) { |
| 4263 | it->isolate()->Throw(*it->isolate()->factory()->NewReferenceError( |
| 4264 | MessageTemplate::kNotDefined, it->name())); |
| 4265 | return Nothing<bool>(); |
| 4266 | } |
| 4267 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4268 | ShouldThrow should_throw = |
| 4269 | is_sloppy(language_mode) ? DONT_THROW : THROW_ON_ERROR; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4270 | return AddDataProperty(it, value, NONE, should_throw, store_mode); |
| 4271 | } |
| 4272 | |
| 4273 | |
| 4274 | Maybe<bool> Object::SetSuperProperty(LookupIterator* it, Handle<Object> value, |
| 4275 | LanguageMode language_mode, |
| 4276 | StoreFromKeyed store_mode) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4277 | Isolate* isolate = it->isolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4278 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4279 | it->UpdateProtector(); |
| 4280 | if (it->IsFound()) { |
| 4281 | bool found = true; |
| 4282 | Maybe<bool> result = |
| 4283 | SetPropertyInternal(it, value, language_mode, store_mode, &found); |
| 4284 | if (found) return result; |
| 4285 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4286 | |
| 4287 | // The property either doesn't exist on the holder or exists there as a data |
| 4288 | // property. |
| 4289 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4290 | ShouldThrow should_throw = |
| 4291 | is_sloppy(language_mode) ? DONT_THROW : THROW_ON_ERROR; |
| 4292 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4293 | if (!it->GetReceiver()->IsJSReceiver()) { |
| 4294 | return WriteToReadOnlyProperty(it, value, should_throw); |
| 4295 | } |
| 4296 | Handle<JSReceiver> receiver = Handle<JSReceiver>::cast(it->GetReceiver()); |
| 4297 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4298 | LookupIterator::Configuration c = LookupIterator::HIDDEN; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4299 | LookupIterator own_lookup = |
| 4300 | it->IsElement() ? LookupIterator(isolate, receiver, it->index(), c) |
| 4301 | : LookupIterator(receiver, it->name(), c); |
| 4302 | |
| 4303 | for (; own_lookup.IsFound(); own_lookup.Next()) { |
| 4304 | switch (own_lookup.state()) { |
| 4305 | case LookupIterator::ACCESS_CHECK: |
| 4306 | if (!own_lookup.HasAccess()) { |
| 4307 | return JSObject::SetPropertyWithFailedAccessCheck(&own_lookup, value, |
| 4308 | should_throw); |
| 4309 | } |
| 4310 | break; |
| 4311 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4312 | case LookupIterator::ACCESSOR: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4313 | if (own_lookup.GetAccessors()->IsAccessorInfo()) { |
| 4314 | if (own_lookup.IsReadOnly()) { |
| 4315 | return WriteToReadOnlyProperty(&own_lookup, value, should_throw); |
| 4316 | } |
| 4317 | return JSObject::SetPropertyWithAccessor(&own_lookup, value, |
| 4318 | should_throw); |
| 4319 | } |
| 4320 | // Fall through. |
| 4321 | case LookupIterator::INTEGER_INDEXED_EXOTIC: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4322 | return RedefineIncompatibleProperty(isolate, it->GetName(), value, |
| 4323 | should_throw); |
| 4324 | |
| 4325 | case LookupIterator::DATA: { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4326 | if (own_lookup.IsReadOnly()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4327 | return WriteToReadOnlyProperty(&own_lookup, value, should_throw); |
| 4328 | } |
| 4329 | return SetDataProperty(&own_lookup, value); |
| 4330 | } |
| 4331 | |
| 4332 | case LookupIterator::INTERCEPTOR: |
| 4333 | case LookupIterator::JSPROXY: { |
| 4334 | PropertyDescriptor desc; |
| 4335 | Maybe<bool> owned = |
| 4336 | JSReceiver::GetOwnPropertyDescriptor(&own_lookup, &desc); |
| 4337 | MAYBE_RETURN(owned, Nothing<bool>()); |
| 4338 | if (!owned.FromJust()) { |
| 4339 | return JSReceiver::CreateDataProperty(&own_lookup, value, |
| 4340 | should_throw); |
| 4341 | } |
| 4342 | if (PropertyDescriptor::IsAccessorDescriptor(&desc) || |
| 4343 | !desc.writable()) { |
| 4344 | return RedefineIncompatibleProperty(isolate, it->GetName(), value, |
| 4345 | should_throw); |
| 4346 | } |
| 4347 | |
| 4348 | PropertyDescriptor value_desc; |
| 4349 | value_desc.set_value(value); |
| 4350 | return JSReceiver::DefineOwnProperty(isolate, receiver, it->GetName(), |
| 4351 | &value_desc, should_throw); |
| 4352 | } |
| 4353 | |
| 4354 | case LookupIterator::NOT_FOUND: |
| 4355 | case LookupIterator::TRANSITION: |
| 4356 | UNREACHABLE(); |
| 4357 | } |
| 4358 | } |
| 4359 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4360 | return AddDataProperty(&own_lookup, value, NONE, should_throw, store_mode); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4363 | MaybeHandle<Object> Object::ReadAbsentProperty(LookupIterator* it) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4364 | return it->isolate()->factory()->undefined_value(); |
| 4365 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4366 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4367 | MaybeHandle<Object> Object::ReadAbsentProperty(Isolate* isolate, |
| 4368 | Handle<Object> receiver, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4369 | Handle<Object> name) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4370 | return isolate->factory()->undefined_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4371 | } |
| 4372 | |
| 4373 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4374 | Maybe<bool> Object::CannotCreateProperty(Isolate* isolate, |
| 4375 | Handle<Object> receiver, |
| 4376 | Handle<Object> name, |
| 4377 | Handle<Object> value, |
| 4378 | ShouldThrow should_throw) { |
| 4379 | RETURN_FAILURE( |
| 4380 | isolate, should_throw, |
| 4381 | NewTypeError(MessageTemplate::kStrictCannotCreateProperty, name, |
| 4382 | Object::TypeOf(isolate, receiver), receiver)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4383 | } |
| 4384 | |
| 4385 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4386 | Maybe<bool> Object::WriteToReadOnlyProperty(LookupIterator* it, |
| 4387 | Handle<Object> value, |
| 4388 | ShouldThrow should_throw) { |
| 4389 | return WriteToReadOnlyProperty(it->isolate(), it->GetReceiver(), |
| 4390 | it->GetName(), value, should_throw); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4391 | } |
| 4392 | |
| 4393 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4394 | Maybe<bool> Object::WriteToReadOnlyProperty(Isolate* isolate, |
| 4395 | Handle<Object> receiver, |
| 4396 | Handle<Object> name, |
| 4397 | Handle<Object> value, |
| 4398 | ShouldThrow should_throw) { |
| 4399 | RETURN_FAILURE(isolate, should_throw, |
| 4400 | NewTypeError(MessageTemplate::kStrictReadOnlyProperty, name, |
| 4401 | Object::TypeOf(isolate, receiver), receiver)); |
| 4402 | } |
| 4403 | |
| 4404 | |
| 4405 | Maybe<bool> Object::RedefineIncompatibleProperty(Isolate* isolate, |
| 4406 | Handle<Object> name, |
| 4407 | Handle<Object> value, |
| 4408 | ShouldThrow should_throw) { |
| 4409 | RETURN_FAILURE(isolate, should_throw, |
| 4410 | NewTypeError(MessageTemplate::kRedefineDisallowed, name)); |
| 4411 | } |
| 4412 | |
| 4413 | |
| 4414 | Maybe<bool> Object::SetDataProperty(LookupIterator* it, Handle<Object> value) { |
| 4415 | // Proxies are handled elsewhere. Other non-JSObjects cannot have own |
| 4416 | // properties. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4417 | Handle<JSObject> receiver = Handle<JSObject>::cast(it->GetReceiver()); |
| 4418 | |
| 4419 | // Store on the holder which may be hidden behind the receiver. |
| 4420 | DCHECK(it->HolderIsReceiverOrHiddenPrototype()); |
| 4421 | |
| 4422 | // Old value for the observation change record. |
| 4423 | // Fetch before transforming the object since the encoding may become |
| 4424 | // incompatible with what's cached in |it|. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4425 | bool is_observed = receiver->map()->is_observed() && |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4426 | (it->IsElement() || !it->name()->IsPrivate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4427 | MaybeHandle<Object> maybe_old; |
| 4428 | if (is_observed) maybe_old = it->GetDataValue(); |
| 4429 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4430 | Handle<Object> to_assign = value; |
| 4431 | // Convert the incoming value to a number for storing into typed arrays. |
| 4432 | if (it->IsElement() && receiver->HasFixedTypedArrayElements()) { |
| 4433 | if (!value->IsNumber() && !value->IsUndefined()) { |
| 4434 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 4435 | it->isolate(), to_assign, Object::ToNumber(value), Nothing<bool>()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4436 | // We have to recheck the length. However, it can only change if the |
| 4437 | // underlying buffer was neutered, so just check that. |
| 4438 | if (Handle<JSArrayBufferView>::cast(receiver)->WasNeutered()) { |
| 4439 | return Just(true); |
| 4440 | // TODO(neis): According to the spec, this should throw a TypeError. |
| 4441 | } |
| 4442 | } |
| 4443 | } |
| 4444 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4445 | // Possibly migrate to the most up-to-date map that will be able to store |
| 4446 | // |value| under it->name(). |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4447 | it->PrepareForDataProperty(to_assign); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4448 | |
| 4449 | // Write the property value. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4450 | it->WriteDataValue(to_assign); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4451 | |
| 4452 | // Send the change record if there are observers. |
| 4453 | if (is_observed && !value->SameValue(*maybe_old.ToHandleChecked())) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4454 | RETURN_ON_EXCEPTION_VALUE( |
| 4455 | it->isolate(), |
| 4456 | JSObject::EnqueueChangeRecord(receiver, "update", it->GetName(), |
| 4457 | maybe_old.ToHandleChecked()), |
| 4458 | Nothing<bool>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4459 | } |
| 4460 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4461 | #if VERIFY_HEAP |
| 4462 | if (FLAG_verify_heap) { |
| 4463 | receiver->JSObjectVerify(); |
| 4464 | } |
| 4465 | #endif |
| 4466 | return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4467 | } |
| 4468 | |
| 4469 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4470 | MUST_USE_RESULT static MaybeHandle<Object> BeginPerformSplice( |
| 4471 | Handle<JSArray> object) { |
| 4472 | Isolate* isolate = object->GetIsolate(); |
| 4473 | HandleScope scope(isolate); |
| 4474 | Handle<Object> args[] = {object}; |
| 4475 | |
| 4476 | return Execution::Call( |
| 4477 | isolate, Handle<JSFunction>(isolate->observers_begin_perform_splice()), |
| 4478 | isolate->factory()->undefined_value(), arraysize(args), args); |
| 4479 | } |
| 4480 | |
| 4481 | |
| 4482 | MUST_USE_RESULT static MaybeHandle<Object> EndPerformSplice( |
| 4483 | Handle<JSArray> object) { |
| 4484 | Isolate* isolate = object->GetIsolate(); |
| 4485 | HandleScope scope(isolate); |
| 4486 | Handle<Object> args[] = {object}; |
| 4487 | |
| 4488 | return Execution::Call( |
| 4489 | isolate, Handle<JSFunction>(isolate->observers_end_perform_splice()), |
| 4490 | isolate->factory()->undefined_value(), arraysize(args), args); |
| 4491 | } |
| 4492 | |
| 4493 | |
| 4494 | MUST_USE_RESULT static MaybeHandle<Object> EnqueueSpliceRecord( |
| 4495 | Handle<JSArray> object, uint32_t index, Handle<JSArray> deleted, |
| 4496 | uint32_t add_count) { |
| 4497 | Isolate* isolate = object->GetIsolate(); |
| 4498 | HandleScope scope(isolate); |
| 4499 | Handle<Object> index_object = isolate->factory()->NewNumberFromUint(index); |
| 4500 | Handle<Object> add_count_object = |
| 4501 | isolate->factory()->NewNumberFromUint(add_count); |
| 4502 | |
| 4503 | Handle<Object> args[] = {object, index_object, deleted, add_count_object}; |
| 4504 | |
| 4505 | return Execution::Call( |
| 4506 | isolate, Handle<JSFunction>(isolate->observers_enqueue_splice()), |
| 4507 | isolate->factory()->undefined_value(), arraysize(args), args); |
| 4508 | } |
| 4509 | |
| 4510 | |
| 4511 | Maybe<bool> Object::AddDataProperty(LookupIterator* it, Handle<Object> value, |
| 4512 | PropertyAttributes attributes, |
| 4513 | ShouldThrow should_throw, |
| 4514 | StoreFromKeyed store_mode) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4515 | if (!it->GetReceiver()->IsJSObject()) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4516 | if (it->GetReceiver()->IsJSProxy() && it->GetName()->IsPrivate()) { |
| 4517 | RETURN_FAILURE(it->isolate(), should_throw, |
| 4518 | NewTypeError(MessageTemplate::kProxyPrivate)); |
| 4519 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4520 | return CannotCreateProperty(it->isolate(), it->GetReceiver(), it->GetName(), |
| 4521 | value, should_throw); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4522 | } |
| 4523 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4524 | DCHECK_NE(LookupIterator::INTEGER_INDEXED_EXOTIC, it->state()); |
| 4525 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4526 | Handle<JSObject> receiver = it->GetStoreTarget(); |
| 4527 | |
| 4528 | // If the receiver is a JSGlobalProxy, store on the prototype (JSGlobalObject) |
| 4529 | // instead. If the prototype is Null, the proxy is detached. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4530 | if (receiver->IsJSGlobalProxy()) return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4531 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4532 | Isolate* isolate = it->isolate(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4533 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4534 | if (it->ExtendingNonExtensible(receiver)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4535 | RETURN_FAILURE( |
| 4536 | isolate, should_throw, |
| 4537 | NewTypeError(MessageTemplate::kObjectNotExtensible, it->GetName())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4538 | } |
| 4539 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4540 | if (it->IsElement()) { |
| 4541 | if (receiver->IsJSArray()) { |
| 4542 | Handle<JSArray> array = Handle<JSArray>::cast(receiver); |
| 4543 | if (JSArray::WouldChangeReadOnlyLength(array, it->index())) { |
| 4544 | RETURN_FAILURE(array->GetIsolate(), should_throw, |
| 4545 | NewTypeError(MessageTemplate::kStrictReadOnlyProperty, |
| 4546 | isolate->factory()->length_string(), |
| 4547 | Object::TypeOf(isolate, array), array)); |
| 4548 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4549 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4550 | if (FLAG_trace_external_array_abuse && |
| 4551 | array->HasFixedTypedArrayElements()) { |
| 4552 | CheckArrayAbuse(array, "typed elements write", it->index(), true); |
| 4553 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4554 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4555 | if (FLAG_trace_js_array_abuse && !array->HasFixedTypedArrayElements()) { |
| 4556 | CheckArrayAbuse(array, "elements write", it->index(), false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4557 | } |
| 4558 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4559 | |
| 4560 | Maybe<bool> result = JSObject::AddDataElement(receiver, it->index(), value, |
| 4561 | attributes, should_throw); |
| 4562 | JSObject::ValidateElements(receiver); |
| 4563 | return result; |
| 4564 | } else { |
| 4565 | // Migrate to the most up-to-date map that will be able to store |value| |
| 4566 | // under it->name() with |attributes|. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4567 | it->PrepareTransitionToDataProperty(receiver, value, attributes, |
| 4568 | store_mode); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4569 | DCHECK_EQ(LookupIterator::TRANSITION, it->state()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4570 | it->ApplyTransitionToDataProperty(receiver); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4571 | |
| 4572 | // TODO(verwaest): Encapsulate dictionary handling better. |
| 4573 | if (receiver->map()->is_dictionary_map()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4574 | // TODO(dcarney): just populate TransitionPropertyCell here? |
| 4575 | JSObject::AddSlowProperty(receiver, it->name(), value, attributes); |
| 4576 | } else { |
| 4577 | // Write the property value. |
| 4578 | it->WriteDataValue(value); |
| 4579 | } |
| 4580 | |
| 4581 | // Send the change record if there are observers. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4582 | if (receiver->map()->is_observed() && !it->name()->IsPrivate()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4583 | RETURN_ON_EXCEPTION_VALUE(isolate, JSObject::EnqueueChangeRecord( |
| 4584 | receiver, "add", it->name(), |
| 4585 | it->factory()->the_hole_value()), |
| 4586 | Nothing<bool>()); |
| 4587 | } |
| 4588 | #if VERIFY_HEAP |
| 4589 | if (FLAG_verify_heap) { |
| 4590 | receiver->JSObjectVerify(); |
| 4591 | } |
| 4592 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4593 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4594 | |
| 4595 | return Just(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4596 | } |
| 4597 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4598 | |
| 4599 | void Map::EnsureDescriptorSlack(Handle<Map> map, int slack) { |
| 4600 | // Only supports adding slack to owned descriptors. |
| 4601 | DCHECK(map->owns_descriptors()); |
| 4602 | |
| 4603 | Handle<DescriptorArray> descriptors(map->instance_descriptors()); |
| 4604 | int old_size = map->NumberOfOwnDescriptors(); |
| 4605 | if (slack <= descriptors->NumberOfSlackDescriptors()) return; |
| 4606 | |
| 4607 | Handle<DescriptorArray> new_descriptors = DescriptorArray::CopyUpTo( |
| 4608 | descriptors, old_size, slack); |
| 4609 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4610 | DisallowHeapAllocation no_allocation; |
| 4611 | // The descriptors are still the same, so keep the layout descriptor. |
| 4612 | LayoutDescriptor* layout_descriptor = map->GetLayoutDescriptor(); |
| 4613 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4614 | if (old_size == 0) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4615 | map->UpdateDescriptors(*new_descriptors, layout_descriptor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4616 | return; |
| 4617 | } |
| 4618 | |
| 4619 | // If the source descriptors had an enum cache we copy it. This ensures |
| 4620 | // that the maps to which we push the new descriptor array back can rely |
| 4621 | // on a cache always being available once it is set. If the map has more |
| 4622 | // enumerated descriptors than available in the original cache, the cache |
| 4623 | // will be lazily replaced by the extended cache when needed. |
| 4624 | if (descriptors->HasEnumCache()) { |
| 4625 | new_descriptors->CopyEnumCacheFrom(*descriptors); |
| 4626 | } |
| 4627 | |
| 4628 | // Replace descriptors by new_descriptors in all maps that share it. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4629 | map->GetHeap()->incremental_marking()->IterateBlackObject(*descriptors); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4630 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4631 | Map* current = *map; |
| 4632 | while (current->instance_descriptors() == *descriptors) { |
| 4633 | Object* next = current->GetBackPointer(); |
| 4634 | if (next->IsUndefined()) break; // Stop overwriting at initial map. |
| 4635 | current->UpdateDescriptors(*new_descriptors, layout_descriptor); |
| 4636 | current = Map::cast(next); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4637 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4638 | map->UpdateDescriptors(*new_descriptors, layout_descriptor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | |
| 4642 | template<class T> |
| 4643 | static int AppendUniqueCallbacks(NeanderArray* callbacks, |
| 4644 | Handle<typename T::Array> array, |
| 4645 | int valid_descriptors) { |
| 4646 | int nof_callbacks = callbacks->length(); |
| 4647 | |
| 4648 | Isolate* isolate = array->GetIsolate(); |
| 4649 | // Ensure the keys are unique names before writing them into the |
| 4650 | // instance descriptor. Since it may cause a GC, it has to be done before we |
| 4651 | // temporarily put the heap in an invalid state while appending descriptors. |
| 4652 | for (int i = 0; i < nof_callbacks; ++i) { |
| 4653 | Handle<AccessorInfo> entry(AccessorInfo::cast(callbacks->get(i))); |
| 4654 | if (entry->name()->IsUniqueName()) continue; |
| 4655 | Handle<String> key = |
| 4656 | isolate->factory()->InternalizeString( |
| 4657 | Handle<String>(String::cast(entry->name()))); |
| 4658 | entry->set_name(*key); |
| 4659 | } |
| 4660 | |
| 4661 | // Fill in new callback descriptors. Process the callbacks from |
| 4662 | // back to front so that the last callback with a given name takes |
| 4663 | // precedence over previously added callbacks with that name. |
| 4664 | for (int i = nof_callbacks - 1; i >= 0; i--) { |
| 4665 | Handle<AccessorInfo> entry(AccessorInfo::cast(callbacks->get(i))); |
| 4666 | Handle<Name> key(Name::cast(entry->name())); |
| 4667 | // Check if a descriptor with this name already exists before writing. |
| 4668 | if (!T::Contains(key, entry, valid_descriptors, array)) { |
| 4669 | T::Insert(key, entry, valid_descriptors, array); |
| 4670 | valid_descriptors++; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4671 | } |
| 4672 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4673 | |
| 4674 | return valid_descriptors; |
| 4675 | } |
| 4676 | |
| 4677 | struct DescriptorArrayAppender { |
| 4678 | typedef DescriptorArray Array; |
| 4679 | static bool Contains(Handle<Name> key, |
| 4680 | Handle<AccessorInfo> entry, |
| 4681 | int valid_descriptors, |
| 4682 | Handle<DescriptorArray> array) { |
| 4683 | DisallowHeapAllocation no_gc; |
| 4684 | return array->Search(*key, valid_descriptors) != DescriptorArray::kNotFound; |
| 4685 | } |
| 4686 | static void Insert(Handle<Name> key, |
| 4687 | Handle<AccessorInfo> entry, |
| 4688 | int valid_descriptors, |
| 4689 | Handle<DescriptorArray> array) { |
| 4690 | DisallowHeapAllocation no_gc; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4691 | AccessorConstantDescriptor desc(key, entry, entry->property_attributes()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4692 | array->Append(&desc); |
| 4693 | } |
| 4694 | }; |
| 4695 | |
| 4696 | |
| 4697 | struct FixedArrayAppender { |
| 4698 | typedef FixedArray Array; |
| 4699 | static bool Contains(Handle<Name> key, |
| 4700 | Handle<AccessorInfo> entry, |
| 4701 | int valid_descriptors, |
| 4702 | Handle<FixedArray> array) { |
| 4703 | for (int i = 0; i < valid_descriptors; i++) { |
| 4704 | if (*key == AccessorInfo::cast(array->get(i))->name()) return true; |
| 4705 | } |
| 4706 | return false; |
| 4707 | } |
| 4708 | static void Insert(Handle<Name> key, |
| 4709 | Handle<AccessorInfo> entry, |
| 4710 | int valid_descriptors, |
| 4711 | Handle<FixedArray> array) { |
| 4712 | DisallowHeapAllocation no_gc; |
| 4713 | array->set(valid_descriptors, *entry); |
| 4714 | } |
| 4715 | }; |
| 4716 | |
| 4717 | |
| 4718 | void Map::AppendCallbackDescriptors(Handle<Map> map, |
| 4719 | Handle<Object> descriptors) { |
| 4720 | int nof = map->NumberOfOwnDescriptors(); |
| 4721 | Handle<DescriptorArray> array(map->instance_descriptors()); |
| 4722 | NeanderArray callbacks(descriptors); |
| 4723 | DCHECK(array->NumberOfSlackDescriptors() >= callbacks.length()); |
| 4724 | nof = AppendUniqueCallbacks<DescriptorArrayAppender>(&callbacks, array, nof); |
| 4725 | map->SetNumberOfOwnDescriptors(nof); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4726 | } |
| 4727 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4728 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4729 | int AccessorInfo::AppendUnique(Handle<Object> descriptors, |
| 4730 | Handle<FixedArray> array, |
| 4731 | int valid_descriptors) { |
| 4732 | NeanderArray callbacks(descriptors); |
| 4733 | DCHECK(array->length() >= callbacks.length() + valid_descriptors); |
| 4734 | return AppendUniqueCallbacks<FixedArrayAppender>(&callbacks, |
| 4735 | array, |
| 4736 | valid_descriptors); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4737 | } |
| 4738 | |
| 4739 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4740 | static bool ContainsMap(MapHandleList* maps, Map* map) { |
| 4741 | DCHECK_NOT_NULL(map); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4742 | for (int i = 0; i < maps->length(); ++i) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4743 | if (!maps->at(i).is_null() && *maps->at(i) == map) return true; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4744 | } |
| 4745 | return false; |
| 4746 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4747 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 4748 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4749 | Handle<Map> Map::FindTransitionedMap(Handle<Map> map, |
| 4750 | MapHandleList* candidates) { |
| 4751 | ElementsKind kind = map->elements_kind(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4752 | bool packed = IsFastPackedElementsKind(kind); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4753 | |
| 4754 | Map* transition = nullptr; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4755 | if (IsTransitionableFastElementsKind(kind)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4756 | for (Map* current = map->ElementsTransitionMap(); |
| 4757 | current != nullptr && current->has_fast_elements(); |
| 4758 | current = current->ElementsTransitionMap()) { |
| 4759 | if (ContainsMap(candidates, current) && |
| 4760 | (packed || !IsFastPackedElementsKind(current->elements_kind()))) { |
| 4761 | transition = current; |
| 4762 | packed = packed && IsFastPackedElementsKind(current->elements_kind()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4763 | } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 4764 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4765 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4766 | return transition == nullptr ? Handle<Map>() : handle(transition); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4767 | } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 4768 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4769 | |
| 4770 | static Map* FindClosestElementsTransition(Map* map, ElementsKind to_kind) { |
| 4771 | Map* current_map = map; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4772 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4773 | ElementsKind kind = map->elements_kind(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4774 | while (kind != to_kind) { |
| 4775 | Map* next_map = current_map->ElementsTransitionMap(); |
| 4776 | if (next_map == nullptr) return current_map; |
| 4777 | kind = next_map->elements_kind(); |
| 4778 | current_map = next_map; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4779 | } |
| 4780 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4781 | DCHECK_EQ(to_kind, current_map->elements_kind()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4782 | return current_map; |
| 4783 | } |
| 4784 | |
| 4785 | |
| 4786 | Map* Map::LookupElementsTransitionMap(ElementsKind to_kind) { |
| 4787 | Map* to_map = FindClosestElementsTransition(this, to_kind); |
| 4788 | if (to_map->elements_kind() == to_kind) return to_map; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4789 | return nullptr; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
| 4792 | |
| 4793 | bool Map::IsMapInArrayPrototypeChain() { |
| 4794 | Isolate* isolate = GetIsolate(); |
| 4795 | if (isolate->initial_array_prototype()->map() == this) { |
| 4796 | return true; |
| 4797 | } |
| 4798 | |
| 4799 | if (isolate->initial_object_prototype()->map() == this) { |
| 4800 | return true; |
| 4801 | } |
| 4802 | |
| 4803 | return false; |
| 4804 | } |
| 4805 | |
| 4806 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4807 | Handle<WeakCell> Map::WeakCellForMap(Handle<Map> map) { |
| 4808 | Isolate* isolate = map->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4809 | if (map->weak_cell_cache()->IsWeakCell()) { |
| 4810 | return Handle<WeakCell>(WeakCell::cast(map->weak_cell_cache())); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4811 | } |
| 4812 | Handle<WeakCell> weak_cell = isolate->factory()->NewWeakCell(map); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4813 | map->set_weak_cell_cache(*weak_cell); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4814 | return weak_cell; |
| 4815 | } |
| 4816 | |
| 4817 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4818 | static Handle<Map> AddMissingElementsTransitions(Handle<Map> map, |
| 4819 | ElementsKind to_kind) { |
| 4820 | DCHECK(IsTransitionElementsKind(map->elements_kind())); |
| 4821 | |
| 4822 | Handle<Map> current_map = map; |
| 4823 | |
| 4824 | ElementsKind kind = map->elements_kind(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4825 | TransitionFlag flag; |
| 4826 | if (map->is_prototype_map()) { |
| 4827 | flag = OMIT_TRANSITION; |
| 4828 | } else { |
| 4829 | flag = INSERT_TRANSITION; |
| 4830 | if (IsFastElementsKind(kind)) { |
| 4831 | while (kind != to_kind && !IsTerminalElementsKind(kind)) { |
| 4832 | kind = GetNextTransitionElementsKind(kind); |
| 4833 | current_map = Map::CopyAsElementsKind(current_map, kind, flag); |
| 4834 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4835 | } |
| 4836 | } |
| 4837 | |
| 4838 | // In case we are exiting the fast elements kind system, just add the map in |
| 4839 | // the end. |
| 4840 | if (kind != to_kind) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4841 | current_map = Map::CopyAsElementsKind(current_map, to_kind, flag); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4842 | } |
| 4843 | |
| 4844 | DCHECK(current_map->elements_kind() == to_kind); |
| 4845 | return current_map; |
| 4846 | } |
| 4847 | |
| 4848 | |
| 4849 | Handle<Map> Map::TransitionElementsTo(Handle<Map> map, |
| 4850 | ElementsKind to_kind) { |
| 4851 | ElementsKind from_kind = map->elements_kind(); |
| 4852 | if (from_kind == to_kind) return map; |
| 4853 | |
| 4854 | Isolate* isolate = map->GetIsolate(); |
| 4855 | Context* native_context = isolate->context()->native_context(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4856 | if (from_kind == FAST_SLOPPY_ARGUMENTS_ELEMENTS) { |
| 4857 | if (*map == native_context->fast_aliased_arguments_map()) { |
| 4858 | DCHECK_EQ(SLOW_SLOPPY_ARGUMENTS_ELEMENTS, to_kind); |
| 4859 | return handle(native_context->slow_aliased_arguments_map()); |
| 4860 | } |
| 4861 | } else if (from_kind == SLOW_SLOPPY_ARGUMENTS_ELEMENTS) { |
| 4862 | if (*map == native_context->slow_aliased_arguments_map()) { |
| 4863 | DCHECK_EQ(FAST_SLOPPY_ARGUMENTS_ELEMENTS, to_kind); |
| 4864 | return handle(native_context->fast_aliased_arguments_map()); |
| 4865 | } |
| 4866 | } else if (IsFastElementsKind(from_kind) && IsFastElementsKind(to_kind)) { |
| 4867 | // Reuse map transitions for JSArrays. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4868 | DisallowHeapAllocation no_gc; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4869 | if (native_context->get(Context::ArrayMapIndex(from_kind)) == *map) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4870 | Object* maybe_transitioned_map = |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4871 | native_context->get(Context::ArrayMapIndex(to_kind)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4872 | if (maybe_transitioned_map->IsMap()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4873 | return handle(Map::cast(maybe_transitioned_map), isolate); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 4874 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4875 | } |
| 4876 | } |
| 4877 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4878 | DCHECK(!map->IsUndefined()); |
| 4879 | // Check if we can go back in the elements kind transition chain. |
| 4880 | if (IsHoleyElementsKind(from_kind) && |
| 4881 | to_kind == GetPackedElementsKind(from_kind) && |
| 4882 | map->GetBackPointer()->IsMap() && |
| 4883 | Map::cast(map->GetBackPointer())->elements_kind() == to_kind) { |
| 4884 | return handle(Map::cast(map->GetBackPointer())); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4885 | } |
| 4886 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4887 | bool allow_store_transition = IsTransitionElementsKind(from_kind); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4888 | // Only store fast element maps in ascending generality. |
| 4889 | if (IsFastElementsKind(to_kind)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4890 | allow_store_transition = |
| 4891 | allow_store_transition && IsTransitionableFastElementsKind(from_kind) && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4892 | IsMoreGeneralElementsKindTransition(from_kind, to_kind); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4893 | } |
| 4894 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4895 | if (!allow_store_transition) { |
| 4896 | return Map::CopyAsElementsKind(map, to_kind, OMIT_TRANSITION); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4897 | } |
| 4898 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4899 | return Map::AsElementsKind(map, to_kind); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4900 | } |
| 4901 | |
| 4902 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4903 | // static |
| 4904 | Handle<Map> Map::AsElementsKind(Handle<Map> map, ElementsKind kind) { |
| 4905 | Handle<Map> closest_map(FindClosestElementsTransition(*map, kind)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4906 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4907 | if (closest_map->elements_kind() == kind) { |
| 4908 | return closest_map; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4909 | } |
| 4910 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4911 | return AddMissingElementsTransitions(closest_map, kind); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4912 | } |
| 4913 | |
| 4914 | |
| 4915 | Handle<Map> JSObject::GetElementsTransitionMap(Handle<JSObject> object, |
| 4916 | ElementsKind to_kind) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4917 | Handle<Map> map(object->map()); |
| 4918 | return Map::TransitionElementsTo(map, to_kind); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4919 | } |
| 4920 | |
| 4921 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4922 | void JSProxy::Revoke(Handle<JSProxy> proxy) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4923 | Isolate* isolate = proxy->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4924 | if (!proxy->IsRevoked()) proxy->set_handler(isolate->heap()->null_value()); |
| 4925 | DCHECK(proxy->IsRevoked()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4926 | } |
| 4927 | |
| 4928 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4929 | Maybe<bool> JSProxy::HasProperty(Isolate* isolate, Handle<JSProxy> proxy, |
| 4930 | Handle<Name> name) { |
| 4931 | DCHECK(!name->IsPrivate()); |
| 4932 | STACK_CHECK(Nothing<bool>()); |
| 4933 | // 1. (Assert) |
| 4934 | // 2. Let handler be the value of the [[ProxyHandler]] internal slot of O. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4935 | Handle<Object> handler(proxy->handler(), isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4936 | // 3. If handler is null, throw a TypeError exception. |
| 4937 | // 4. Assert: Type(handler) is Object. |
| 4938 | if (proxy->IsRevoked()) { |
| 4939 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 4940 | MessageTemplate::kProxyRevoked, isolate->factory()->has_string())); |
| 4941 | return Nothing<bool>(); |
| 4942 | } |
| 4943 | // 5. Let target be the value of the [[ProxyTarget]] internal slot of O. |
| 4944 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 4945 | // 6. Let trap be ? GetMethod(handler, "has"). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4946 | Handle<Object> trap; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4947 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 4948 | isolate, trap, Object::GetMethod(Handle<JSReceiver>::cast(handler), |
| 4949 | isolate->factory()->has_string()), |
| 4950 | Nothing<bool>()); |
| 4951 | // 7. If trap is undefined, then |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4952 | if (trap->IsUndefined()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4953 | // 7a. Return target.[[HasProperty]](P). |
| 4954 | return JSReceiver::HasProperty(target, name); |
| 4955 | } |
| 4956 | // 8. Let booleanTrapResult be ToBoolean(? Call(trap, handler, «target, P»)). |
| 4957 | Handle<Object> trap_result_obj; |
| 4958 | Handle<Object> args[] = {target, name}; |
| 4959 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 4960 | isolate, trap_result_obj, |
| 4961 | Execution::Call(isolate, trap, handler, arraysize(args), args), |
| 4962 | Nothing<bool>()); |
| 4963 | bool boolean_trap_result = trap_result_obj->BooleanValue(); |
| 4964 | // 9. If booleanTrapResult is false, then: |
| 4965 | if (!boolean_trap_result) { |
| 4966 | // 9a. Let targetDesc be ? target.[[GetOwnProperty]](P). |
| 4967 | PropertyDescriptor target_desc; |
| 4968 | Maybe<bool> target_found = JSReceiver::GetOwnPropertyDescriptor( |
| 4969 | isolate, target, name, &target_desc); |
| 4970 | MAYBE_RETURN(target_found, Nothing<bool>()); |
| 4971 | // 9b. If targetDesc is not undefined, then: |
| 4972 | if (target_found.FromJust()) { |
| 4973 | // 9b i. If targetDesc.[[Configurable]] is false, throw a TypeError |
| 4974 | // exception. |
| 4975 | if (!target_desc.configurable()) { |
| 4976 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 4977 | MessageTemplate::kProxyHasNonConfigurable, name)); |
| 4978 | return Nothing<bool>(); |
| 4979 | } |
| 4980 | // 9b ii. Let extensibleTarget be ? IsExtensible(target). |
| 4981 | Maybe<bool> extensible_target = JSReceiver::IsExtensible(target); |
| 4982 | MAYBE_RETURN(extensible_target, Nothing<bool>()); |
| 4983 | // 9b iii. If extensibleTarget is false, throw a TypeError exception. |
| 4984 | if (!extensible_target.FromJust()) { |
| 4985 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 4986 | MessageTemplate::kProxyHasNonExtensible, name)); |
| 4987 | return Nothing<bool>(); |
| 4988 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4989 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4990 | } |
| 4991 | // 10. Return booleanTrapResult. |
| 4992 | return Just(boolean_trap_result); |
| 4993 | } |
| 4994 | |
| 4995 | |
| 4996 | Maybe<bool> JSProxy::SetProperty(Handle<JSProxy> proxy, Handle<Name> name, |
| 4997 | Handle<Object> value, Handle<Object> receiver, |
| 4998 | LanguageMode language_mode) { |
| 4999 | DCHECK(!name->IsPrivate()); |
| 5000 | Isolate* isolate = proxy->GetIsolate(); |
| 5001 | STACK_CHECK(Nothing<bool>()); |
| 5002 | Factory* factory = isolate->factory(); |
| 5003 | Handle<String> trap_name = factory->set_string(); |
| 5004 | ShouldThrow should_throw = |
| 5005 | is_sloppy(language_mode) ? DONT_THROW : THROW_ON_ERROR; |
| 5006 | |
| 5007 | if (proxy->IsRevoked()) { |
| 5008 | isolate->Throw( |
| 5009 | *factory->NewTypeError(MessageTemplate::kProxyRevoked, trap_name)); |
| 5010 | return Nothing<bool>(); |
| 5011 | } |
| 5012 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 5013 | Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate); |
| 5014 | |
| 5015 | Handle<Object> trap; |
| 5016 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 5017 | isolate, trap, Object::GetMethod(handler, trap_name), Nothing<bool>()); |
| 5018 | if (trap->IsUndefined()) { |
| 5019 | LookupIterator it = |
| 5020 | LookupIterator::PropertyOrElement(isolate, receiver, name, target); |
| 5021 | return Object::SetSuperProperty(&it, value, language_mode, |
| 5022 | Object::MAY_BE_STORE_FROM_KEYED); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5023 | } |
| 5024 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5025 | Handle<Object> trap_result; |
| 5026 | Handle<Object> args[] = {target, name, value, receiver}; |
| 5027 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 5028 | isolate, trap_result, |
| 5029 | Execution::Call(isolate, trap, handler, arraysize(args), args), |
| 5030 | Nothing<bool>()); |
| 5031 | if (!trap_result->BooleanValue()) { |
| 5032 | RETURN_FAILURE(isolate, should_throw, |
| 5033 | NewTypeError(MessageTemplate::kProxyTrapReturnedFalsishFor, |
| 5034 | trap_name, name)); |
| 5035 | } |
| 5036 | |
| 5037 | // Enforce the invariant. |
| 5038 | PropertyDescriptor target_desc; |
| 5039 | Maybe<bool> owned = |
| 5040 | JSReceiver::GetOwnPropertyDescriptor(isolate, target, name, &target_desc); |
| 5041 | MAYBE_RETURN(owned, Nothing<bool>()); |
| 5042 | if (owned.FromJust()) { |
| 5043 | bool inconsistent = PropertyDescriptor::IsDataDescriptor(&target_desc) && |
| 5044 | !target_desc.configurable() && |
| 5045 | !target_desc.writable() && |
| 5046 | !value->SameValue(*target_desc.value()); |
| 5047 | if (inconsistent) { |
| 5048 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 5049 | MessageTemplate::kProxySetFrozenData, name)); |
| 5050 | return Nothing<bool>(); |
| 5051 | } |
| 5052 | inconsistent = PropertyDescriptor::IsAccessorDescriptor(&target_desc) && |
| 5053 | !target_desc.configurable() && |
| 5054 | target_desc.set()->IsUndefined(); |
| 5055 | if (inconsistent) { |
| 5056 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 5057 | MessageTemplate::kProxySetFrozenAccessor, name)); |
| 5058 | return Nothing<bool>(); |
| 5059 | } |
| 5060 | } |
| 5061 | return Just(true); |
| 5062 | } |
| 5063 | |
| 5064 | |
| 5065 | Maybe<bool> JSProxy::DeletePropertyOrElement(Handle<JSProxy> proxy, |
| 5066 | Handle<Name> name, |
| 5067 | LanguageMode language_mode) { |
| 5068 | DCHECK(!name->IsPrivate()); |
| 5069 | ShouldThrow should_throw = |
| 5070 | is_sloppy(language_mode) ? DONT_THROW : THROW_ON_ERROR; |
| 5071 | Isolate* isolate = proxy->GetIsolate(); |
| 5072 | STACK_CHECK(Nothing<bool>()); |
| 5073 | Factory* factory = isolate->factory(); |
| 5074 | Handle<String> trap_name = factory->deleteProperty_string(); |
| 5075 | |
| 5076 | if (proxy->IsRevoked()) { |
| 5077 | isolate->Throw( |
| 5078 | *factory->NewTypeError(MessageTemplate::kProxyRevoked, trap_name)); |
| 5079 | return Nothing<bool>(); |
| 5080 | } |
| 5081 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 5082 | Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate); |
| 5083 | |
| 5084 | Handle<Object> trap; |
| 5085 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 5086 | isolate, trap, Object::GetMethod(handler, trap_name), Nothing<bool>()); |
| 5087 | if (trap->IsUndefined()) { |
| 5088 | return JSReceiver::DeletePropertyOrElement(target, name, language_mode); |
| 5089 | } |
| 5090 | |
| 5091 | Handle<Object> trap_result; |
| 5092 | Handle<Object> args[] = {target, name}; |
| 5093 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 5094 | isolate, trap_result, |
| 5095 | Execution::Call(isolate, trap, handler, arraysize(args), args), |
| 5096 | Nothing<bool>()); |
| 5097 | if (!trap_result->BooleanValue()) { |
| 5098 | RETURN_FAILURE(isolate, should_throw, |
| 5099 | NewTypeError(MessageTemplate::kProxyTrapReturnedFalsishFor, |
| 5100 | trap_name, name)); |
| 5101 | } |
| 5102 | |
| 5103 | // Enforce the invariant. |
| 5104 | PropertyDescriptor target_desc; |
| 5105 | Maybe<bool> owned = |
| 5106 | JSReceiver::GetOwnPropertyDescriptor(isolate, target, name, &target_desc); |
| 5107 | MAYBE_RETURN(owned, Nothing<bool>()); |
| 5108 | if (owned.FromJust() && !target_desc.configurable()) { |
| 5109 | isolate->Throw(*factory->NewTypeError( |
| 5110 | MessageTemplate::kProxyDeletePropertyNonConfigurable, name)); |
| 5111 | return Nothing<bool>(); |
| 5112 | } |
| 5113 | return Just(true); |
| 5114 | } |
| 5115 | |
| 5116 | |
| 5117 | // static |
| 5118 | MaybeHandle<JSProxy> JSProxy::New(Isolate* isolate, Handle<Object> target, |
| 5119 | Handle<Object> handler) { |
| 5120 | if (!target->IsJSReceiver()) { |
| 5121 | THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kProxyNonObject), |
| 5122 | JSProxy); |
| 5123 | } |
| 5124 | if (target->IsJSProxy() && JSProxy::cast(*target)->IsRevoked()) { |
| 5125 | THROW_NEW_ERROR(isolate, |
| 5126 | NewTypeError(MessageTemplate::kProxyHandlerOrTargetRevoked), |
| 5127 | JSProxy); |
| 5128 | } |
| 5129 | if (!handler->IsJSReceiver()) { |
| 5130 | THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kProxyNonObject), |
| 5131 | JSProxy); |
| 5132 | } |
| 5133 | if (handler->IsJSProxy() && JSProxy::cast(*handler)->IsRevoked()) { |
| 5134 | THROW_NEW_ERROR(isolate, |
| 5135 | NewTypeError(MessageTemplate::kProxyHandlerOrTargetRevoked), |
| 5136 | JSProxy); |
| 5137 | } |
| 5138 | return isolate->factory()->NewJSProxy(Handle<JSReceiver>::cast(target), |
| 5139 | Handle<JSReceiver>::cast(handler)); |
| 5140 | } |
| 5141 | |
| 5142 | |
| 5143 | // static |
| 5144 | MaybeHandle<Context> JSProxy::GetFunctionRealm(Handle<JSProxy> proxy) { |
| 5145 | DCHECK(proxy->map()->is_constructor()); |
| 5146 | if (proxy->IsRevoked()) { |
| 5147 | THROW_NEW_ERROR(proxy->GetIsolate(), |
| 5148 | NewTypeError(MessageTemplate::kProxyRevoked), Context); |
| 5149 | } |
| 5150 | Handle<JSReceiver> target(JSReceiver::cast(proxy->target())); |
| 5151 | return JSReceiver::GetFunctionRealm(target); |
| 5152 | } |
| 5153 | |
| 5154 | |
| 5155 | // static |
| 5156 | MaybeHandle<Context> JSBoundFunction::GetFunctionRealm( |
| 5157 | Handle<JSBoundFunction> function) { |
| 5158 | DCHECK(function->map()->is_constructor()); |
| 5159 | return JSReceiver::GetFunctionRealm( |
| 5160 | handle(function->bound_target_function())); |
| 5161 | } |
| 5162 | |
| 5163 | |
| 5164 | // static |
| 5165 | Handle<Context> JSFunction::GetFunctionRealm(Handle<JSFunction> function) { |
| 5166 | DCHECK(function->map()->is_constructor()); |
| 5167 | return handle(function->context()->native_context()); |
| 5168 | } |
| 5169 | |
| 5170 | |
| 5171 | // static |
| 5172 | MaybeHandle<Context> JSObject::GetFunctionRealm(Handle<JSObject> object) { |
| 5173 | DCHECK(object->map()->is_constructor()); |
| 5174 | DCHECK(!object->IsJSFunction()); |
| 5175 | return handle(object->GetCreationContext()); |
| 5176 | } |
| 5177 | |
| 5178 | |
| 5179 | // static |
| 5180 | MaybeHandle<Context> JSReceiver::GetFunctionRealm(Handle<JSReceiver> receiver) { |
| 5181 | if (receiver->IsJSProxy()) { |
| 5182 | return JSProxy::GetFunctionRealm(Handle<JSProxy>::cast(receiver)); |
| 5183 | } |
| 5184 | |
| 5185 | if (receiver->IsJSFunction()) { |
| 5186 | return JSFunction::GetFunctionRealm(Handle<JSFunction>::cast(receiver)); |
| 5187 | } |
| 5188 | |
| 5189 | if (receiver->IsJSBoundFunction()) { |
| 5190 | return JSBoundFunction::GetFunctionRealm( |
| 5191 | Handle<JSBoundFunction>::cast(receiver)); |
| 5192 | } |
| 5193 | |
| 5194 | return JSObject::GetFunctionRealm(Handle<JSObject>::cast(receiver)); |
| 5195 | } |
| 5196 | |
| 5197 | |
| 5198 | Maybe<PropertyAttributes> JSProxy::GetPropertyAttributes(LookupIterator* it) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5199 | PropertyDescriptor desc; |
| 5200 | Maybe<bool> found = JSProxy::GetOwnPropertyDescriptor( |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5201 | it->isolate(), it->GetHolder<JSProxy>(), it->GetName(), &desc); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5202 | MAYBE_RETURN(found, Nothing<PropertyAttributes>()); |
| 5203 | if (!found.FromJust()) return Just(ABSENT); |
| 5204 | return Just(desc.ToAttributes()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5205 | } |
| 5206 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5207 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5208 | void JSObject::AllocateStorageForMap(Handle<JSObject> object, Handle<Map> map) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5209 | DCHECK(object->map()->GetInObjectProperties() == |
| 5210 | map->GetInObjectProperties()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5211 | ElementsKind obj_kind = object->map()->elements_kind(); |
| 5212 | ElementsKind map_kind = map->elements_kind(); |
| 5213 | if (map_kind != obj_kind) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5214 | ElementsKind to_kind = GetMoreGeneralElementsKind(map_kind, obj_kind); |
| 5215 | if (IsDictionaryElementsKind(obj_kind)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5216 | to_kind = obj_kind; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5217 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5218 | if (IsDictionaryElementsKind(to_kind)) { |
| 5219 | NormalizeElements(object); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5220 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5221 | TransitionElementsKind(object, to_kind); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5222 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5223 | map = Map::AsElementsKind(map, to_kind); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5224 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5225 | JSObject::MigrateToMap(object, map); |
| 5226 | } |
| 5227 | |
| 5228 | |
| 5229 | void JSObject::MigrateInstance(Handle<JSObject> object) { |
| 5230 | Handle<Map> original_map(object->map()); |
| 5231 | Handle<Map> map = Map::Update(original_map); |
| 5232 | map->set_migration_target(true); |
| 5233 | MigrateToMap(object, map); |
| 5234 | if (FLAG_trace_migration) { |
| 5235 | object->PrintInstanceMigration(stdout, *original_map, *map); |
| 5236 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5237 | #if VERIFY_HEAP |
| 5238 | if (FLAG_verify_heap) { |
| 5239 | object->JSObjectVerify(); |
| 5240 | } |
| 5241 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5242 | } |
| 5243 | |
| 5244 | |
| 5245 | // static |
| 5246 | bool JSObject::TryMigrateInstance(Handle<JSObject> object) { |
| 5247 | Isolate* isolate = object->GetIsolate(); |
| 5248 | DisallowDeoptimization no_deoptimization(isolate); |
| 5249 | Handle<Map> original_map(object->map(), isolate); |
| 5250 | Handle<Map> new_map; |
| 5251 | if (!Map::TryUpdate(original_map).ToHandle(&new_map)) { |
| 5252 | return false; |
| 5253 | } |
| 5254 | JSObject::MigrateToMap(object, new_map); |
| 5255 | if (FLAG_trace_migration) { |
| 5256 | object->PrintInstanceMigration(stdout, *original_map, object->map()); |
| 5257 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5258 | #if VERIFY_HEAP |
| 5259 | if (FLAG_verify_heap) { |
| 5260 | object->JSObjectVerify(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5261 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5262 | #endif |
| 5263 | return true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5264 | } |
| 5265 | |
| 5266 | |
| 5267 | void JSObject::AddProperty(Handle<JSObject> object, Handle<Name> name, |
| 5268 | Handle<Object> value, |
| 5269 | PropertyAttributes attributes) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5270 | LookupIterator it(object, name, object, LookupIterator::OWN_SKIP_INTERCEPTOR); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5271 | CHECK_NE(LookupIterator::ACCESS_CHECK, it.state()); |
| 5272 | #ifdef DEBUG |
| 5273 | uint32_t index; |
| 5274 | DCHECK(!object->IsJSProxy()); |
| 5275 | DCHECK(!name->AsArrayIndex(&index)); |
| 5276 | Maybe<PropertyAttributes> maybe = GetPropertyAttributes(&it); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5277 | DCHECK(maybe.IsJust()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5278 | DCHECK(!it.IsFound()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5279 | DCHECK(object->map()->is_extensible() || name->IsPrivate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5280 | #endif |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5281 | CHECK(AddDataProperty(&it, value, attributes, THROW_ON_ERROR, |
| 5282 | CERTAINLY_NOT_STORE_FROM_KEYED) |
| 5283 | .IsJust()); |
| 5284 | } |
| 5285 | |
| 5286 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5287 | // Reconfigures a property to a data property with attributes, even if it is not |
| 5288 | // reconfigurable. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5289 | // Requires a LookupIterator that does not look at the prototype chain beyond |
| 5290 | // hidden prototypes. |
| 5291 | MaybeHandle<Object> JSObject::DefineOwnPropertyIgnoreAttributes( |
| 5292 | LookupIterator* it, Handle<Object> value, PropertyAttributes attributes, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5293 | AccessorInfoHandling handling) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5294 | MAYBE_RETURN_NULL(DefineOwnPropertyIgnoreAttributes( |
| 5295 | it, value, attributes, THROW_ON_ERROR, handling)); |
| 5296 | return value; |
| 5297 | } |
| 5298 | |
| 5299 | |
| 5300 | Maybe<bool> JSObject::DefineOwnPropertyIgnoreAttributes( |
| 5301 | LookupIterator* it, Handle<Object> value, PropertyAttributes attributes, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5302 | ShouldThrow should_throw, AccessorInfoHandling handling) { |
| 5303 | it->UpdateProtector(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5304 | Handle<JSObject> object = Handle<JSObject>::cast(it->GetReceiver()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5305 | bool is_observed = object->map()->is_observed() && |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5306 | (it->IsElement() || !it->name()->IsPrivate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5307 | |
| 5308 | for (; it->IsFound(); it->Next()) { |
| 5309 | switch (it->state()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5310 | case LookupIterator::JSPROXY: |
| 5311 | case LookupIterator::NOT_FOUND: |
| 5312 | case LookupIterator::TRANSITION: |
| 5313 | UNREACHABLE(); |
| 5314 | |
| 5315 | case LookupIterator::ACCESS_CHECK: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5316 | if (!it->HasAccess()) { |
| 5317 | it->isolate()->ReportFailedAccessCheck(it->GetHolder<JSObject>()); |
| 5318 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(it->isolate(), Nothing<bool>()); |
| 5319 | return Just(true); |
| 5320 | } |
| 5321 | break; |
| 5322 | |
| 5323 | // If there's an interceptor, try to store the property with the |
| 5324 | // interceptor. |
| 5325 | // In case of success, the attributes will have been reset to the default |
| 5326 | // attributes of the interceptor, rather than the incoming attributes. |
| 5327 | // |
| 5328 | // TODO(verwaest): JSProxy afterwards verify the attributes that the |
| 5329 | // JSProxy claims it has, and verifies that they are compatible. If not, |
| 5330 | // they throw. Here we should do the same. |
| 5331 | case LookupIterator::INTERCEPTOR: |
| 5332 | if (handling == DONT_FORCE_FIELD) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5333 | Maybe<bool> result = |
| 5334 | JSObject::SetPropertyWithInterceptor(it, should_throw, value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5335 | if (result.IsNothing() || result.FromJust()) return result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5336 | } |
| 5337 | break; |
| 5338 | |
| 5339 | case LookupIterator::ACCESSOR: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5340 | Handle<Object> accessors = it->GetAccessors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5341 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5342 | // Special handling for AccessorInfo, which behaves like a data |
| 5343 | // property. |
| 5344 | if (accessors->IsAccessorInfo() && handling == DONT_FORCE_FIELD) { |
| 5345 | PropertyAttributes current_attributes = it->property_attributes(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5346 | // Ensure the context isn't changed after calling into accessors. |
| 5347 | AssertNoContextChange ncc(it->isolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5348 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5349 | // Update the attributes before calling the setter. The setter may |
| 5350 | // later change the shape of the property. |
| 5351 | if (current_attributes != attributes) { |
| 5352 | it->TransitionToAccessorPair(accessors, attributes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5353 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5354 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5355 | Maybe<bool> result = |
| 5356 | JSObject::SetPropertyWithAccessor(it, value, should_throw); |
| 5357 | |
| 5358 | if (current_attributes == attributes || result.IsNothing()) { |
| 5359 | return result; |
| 5360 | } |
| 5361 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5362 | } else { |
| 5363 | it->ReconfigureDataProperty(value, attributes); |
| 5364 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5365 | |
| 5366 | if (is_observed) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5367 | RETURN_ON_EXCEPTION_VALUE( |
| 5368 | it->isolate(), |
| 5369 | EnqueueChangeRecord(object, "reconfigure", it->GetName(), |
| 5370 | it->factory()->the_hole_value()), |
| 5371 | Nothing<bool>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5372 | } |
| 5373 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5374 | return Just(true); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5375 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5376 | case LookupIterator::INTEGER_INDEXED_EXOTIC: |
| 5377 | return RedefineIncompatibleProperty(it->isolate(), it->GetName(), value, |
| 5378 | should_throw); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5379 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5380 | case LookupIterator::DATA: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5381 | // Regular property update if the attributes match. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5382 | if (it->property_attributes() == attributes) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5383 | return SetDataProperty(it, value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5384 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5385 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5386 | // Special case: properties of typed arrays cannot be reconfigured to |
| 5387 | // non-writable nor to non-enumerable. |
| 5388 | if (it->IsElement() && object->HasFixedTypedArrayElements()) { |
| 5389 | return RedefineIncompatibleProperty(it->isolate(), it->GetName(), |
| 5390 | value, should_throw); |
| 5391 | } |
| 5392 | |
| 5393 | // Reconfigure the data property if the attributes mismatch. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5394 | Handle<Object> old_value = it->factory()->the_hole_value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5395 | if (is_observed) old_value = it->GetDataValue(); |
| 5396 | |
| 5397 | it->ReconfigureDataProperty(value, attributes); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5398 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5399 | if (is_observed) { |
| 5400 | if (old_value->SameValue(*value)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5401 | old_value = it->factory()->the_hole_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5402 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5403 | RETURN_ON_EXCEPTION_VALUE( |
| 5404 | it->isolate(), EnqueueChangeRecord(object, "reconfigure", |
| 5405 | it->GetName(), old_value), |
| 5406 | Nothing<bool>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5407 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5408 | return Just(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5409 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5410 | } |
| 5411 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5412 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5413 | return AddDataProperty(it, value, attributes, should_throw, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5414 | CERTAINLY_NOT_STORE_FROM_KEYED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5415 | } |
| 5416 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5417 | MaybeHandle<Object> JSObject::SetOwnPropertyIgnoreAttributes( |
| 5418 | Handle<JSObject> object, Handle<Name> name, Handle<Object> value, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5419 | PropertyAttributes attributes) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5420 | DCHECK(!value->IsTheHole()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5421 | LookupIterator it(object, name, object, LookupIterator::OWN); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5422 | return DefineOwnPropertyIgnoreAttributes(&it, value, attributes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5423 | } |
| 5424 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5425 | MaybeHandle<Object> JSObject::SetOwnElementIgnoreAttributes( |
| 5426 | Handle<JSObject> object, uint32_t index, Handle<Object> value, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5427 | PropertyAttributes attributes) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5428 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5429 | LookupIterator it(isolate, object, index, object, LookupIterator::OWN); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5430 | return DefineOwnPropertyIgnoreAttributes(&it, value, attributes); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5431 | } |
| 5432 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5433 | MaybeHandle<Object> JSObject::DefinePropertyOrElementIgnoreAttributes( |
| 5434 | Handle<JSObject> object, Handle<Name> name, Handle<Object> value, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5435 | PropertyAttributes attributes) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5436 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5437 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 5438 | isolate, object, name, object, LookupIterator::OWN); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5439 | return DefineOwnPropertyIgnoreAttributes(&it, value, attributes); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5440 | } |
| 5441 | |
| 5442 | |
| 5443 | Maybe<PropertyAttributes> JSObject::GetPropertyAttributesWithInterceptor( |
| 5444 | LookupIterator* it) { |
| 5445 | Isolate* isolate = it->isolate(); |
| 5446 | // Make sure that the top context does not change when doing |
| 5447 | // callbacks or interceptor calls. |
| 5448 | AssertNoContextChange ncc(isolate); |
| 5449 | HandleScope scope(isolate); |
| 5450 | |
| 5451 | Handle<JSObject> holder = it->GetHolder<JSObject>(); |
| 5452 | Handle<InterceptorInfo> interceptor(it->GetInterceptor()); |
| 5453 | if (!it->IsElement() && it->name()->IsSymbol() && |
| 5454 | !interceptor->can_intercept_symbols()) { |
| 5455 | return Just(ABSENT); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5456 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5457 | Handle<Object> receiver = it->GetReceiver(); |
| 5458 | if (!receiver->IsJSReceiver()) { |
| 5459 | ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, receiver, |
| 5460 | Object::ConvertReceiver(isolate, receiver), |
| 5461 | Nothing<PropertyAttributes>()); |
| 5462 | } |
| 5463 | PropertyCallbackArguments args(isolate, interceptor->data(), *receiver, |
| 5464 | *holder, Object::DONT_THROW); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5465 | if (!interceptor->query()->IsUndefined()) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5466 | Handle<Object> result; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5467 | if (it->IsElement()) { |
| 5468 | uint32_t index = it->index(); |
| 5469 | v8::IndexedPropertyQueryCallback query = |
| 5470 | v8::ToCData<v8::IndexedPropertyQueryCallback>(interceptor->query()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5471 | result = args.Call(query, index); |
| 5472 | } else { |
| 5473 | Handle<Name> name = it->name(); |
| 5474 | DCHECK(!name->IsPrivate()); |
| 5475 | v8::GenericNamedPropertyQueryCallback query = |
| 5476 | v8::ToCData<v8::GenericNamedPropertyQueryCallback>( |
| 5477 | interceptor->query()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5478 | result = args.Call(query, name); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5479 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5480 | if (!result.is_null()) { |
| 5481 | int32_t value; |
| 5482 | CHECK(result->ToInt32(&value)); |
| 5483 | return Just(static_cast<PropertyAttributes>(value)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5484 | } |
| 5485 | } else if (!interceptor->getter()->IsUndefined()) { |
| 5486 | // TODO(verwaest): Use GetPropertyWithInterceptor? |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5487 | Handle<Object> result; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5488 | if (it->IsElement()) { |
| 5489 | uint32_t index = it->index(); |
| 5490 | v8::IndexedPropertyGetterCallback getter = |
| 5491 | v8::ToCData<v8::IndexedPropertyGetterCallback>(interceptor->getter()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5492 | result = args.Call(getter, index); |
| 5493 | } else { |
| 5494 | Handle<Name> name = it->name(); |
| 5495 | DCHECK(!name->IsPrivate()); |
| 5496 | v8::GenericNamedPropertyGetterCallback getter = |
| 5497 | v8::ToCData<v8::GenericNamedPropertyGetterCallback>( |
| 5498 | interceptor->getter()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5499 | result = args.Call(getter, name); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5500 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5501 | if (!result.is_null()) return Just(DONT_ENUM); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5502 | } |
| 5503 | |
| 5504 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<PropertyAttributes>()); |
| 5505 | return Just(ABSENT); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5506 | } |
| 5507 | |
| 5508 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5509 | Maybe<PropertyAttributes> JSReceiver::GetPropertyAttributes( |
| 5510 | LookupIterator* it) { |
| 5511 | for (; it->IsFound(); it->Next()) { |
| 5512 | switch (it->state()) { |
| 5513 | case LookupIterator::NOT_FOUND: |
| 5514 | case LookupIterator::TRANSITION: |
| 5515 | UNREACHABLE(); |
| 5516 | case LookupIterator::JSPROXY: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5517 | return JSProxy::GetPropertyAttributes(it); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5518 | case LookupIterator::INTERCEPTOR: { |
| 5519 | Maybe<PropertyAttributes> result = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5520 | JSObject::GetPropertyAttributesWithInterceptor(it); |
| 5521 | if (!result.IsJust()) return result; |
| 5522 | if (result.FromJust() != ABSENT) return result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5523 | break; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5524 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5525 | case LookupIterator::ACCESS_CHECK: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5526 | if (it->HasAccess()) break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5527 | return JSObject::GetPropertyAttributesWithFailedAccessCheck(it); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5528 | case LookupIterator::INTEGER_INDEXED_EXOTIC: |
| 5529 | return Just(ABSENT); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5530 | case LookupIterator::ACCESSOR: |
| 5531 | case LookupIterator::DATA: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5532 | return Just(it->property_attributes()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5533 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5534 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5535 | return Just(ABSENT); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5536 | } |
| 5537 | |
| 5538 | |
| 5539 | Handle<NormalizedMapCache> NormalizedMapCache::New(Isolate* isolate) { |
| 5540 | Handle<FixedArray> array( |
| 5541 | isolate->factory()->NewFixedArray(kEntries, TENURED)); |
| 5542 | return Handle<NormalizedMapCache>::cast(array); |
| 5543 | } |
| 5544 | |
| 5545 | |
| 5546 | MaybeHandle<Map> NormalizedMapCache::Get(Handle<Map> fast_map, |
| 5547 | PropertyNormalizationMode mode) { |
| 5548 | DisallowHeapAllocation no_gc; |
| 5549 | Object* value = FixedArray::get(GetIndex(fast_map)); |
| 5550 | if (!value->IsMap() || |
| 5551 | !Map::cast(value)->EquivalentToForNormalization(*fast_map, mode)) { |
| 5552 | return MaybeHandle<Map>(); |
| 5553 | } |
| 5554 | return handle(Map::cast(value)); |
| 5555 | } |
| 5556 | |
| 5557 | |
| 5558 | void NormalizedMapCache::Set(Handle<Map> fast_map, |
| 5559 | Handle<Map> normalized_map) { |
| 5560 | DisallowHeapAllocation no_gc; |
| 5561 | DCHECK(normalized_map->is_dictionary_map()); |
| 5562 | FixedArray::set(GetIndex(fast_map), *normalized_map); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5563 | } |
| 5564 | |
| 5565 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5566 | void NormalizedMapCache::Clear() { |
| 5567 | int entries = length(); |
| 5568 | for (int i = 0; i != entries; i++) { |
| 5569 | set_undefined(i); |
| 5570 | } |
| 5571 | } |
| 5572 | |
| 5573 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5574 | void HeapObject::UpdateMapCodeCache(Handle<HeapObject> object, |
| 5575 | Handle<Name> name, |
| 5576 | Handle<Code> code) { |
| 5577 | Handle<Map> map(object->map()); |
| 5578 | Map::UpdateCodeCache(map, name, code); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5579 | } |
| 5580 | |
| 5581 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5582 | void JSObject::NormalizeProperties(Handle<JSObject> object, |
| 5583 | PropertyNormalizationMode mode, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5584 | int expected_additional_properties, |
| 5585 | const char* reason) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5586 | if (!object->HasFastProperties()) return; |
| 5587 | |
| 5588 | Handle<Map> map(object->map()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5589 | Handle<Map> new_map = Map::Normalize(map, mode, reason); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5590 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5591 | MigrateToMap(object, new_map, expected_additional_properties); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5592 | } |
| 5593 | |
| 5594 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5595 | void JSObject::MigrateSlowToFast(Handle<JSObject> object, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5596 | int unused_property_fields, |
| 5597 | const char* reason) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5598 | if (object->HasFastProperties()) return; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5599 | DCHECK(!object->IsJSGlobalObject()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5600 | Isolate* isolate = object->GetIsolate(); |
| 5601 | Factory* factory = isolate->factory(); |
| 5602 | Handle<NameDictionary> dictionary(object->property_dictionary()); |
| 5603 | |
| 5604 | // Make sure we preserve dictionary representation if there are too many |
| 5605 | // descriptors. |
| 5606 | int number_of_elements = dictionary->NumberOfElements(); |
| 5607 | if (number_of_elements > kMaxNumberOfDescriptors) return; |
| 5608 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5609 | Handle<FixedArray> iteration_order; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5610 | if (number_of_elements != dictionary->NextEnumerationIndex()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5611 | iteration_order = |
| 5612 | NameDictionary::DoGenerateNewEnumerationIndices(dictionary); |
| 5613 | } else { |
| 5614 | iteration_order = NameDictionary::BuildIterationIndicesArray(dictionary); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5615 | } |
| 5616 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5617 | int instance_descriptor_length = iteration_order->length(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5618 | int number_of_fields = 0; |
| 5619 | |
| 5620 | // Compute the length of the instance descriptor. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5621 | for (int i = 0; i < instance_descriptor_length; i++) { |
| 5622 | int index = Smi::cast(iteration_order->get(i))->value(); |
| 5623 | DCHECK(dictionary->IsKey(dictionary->KeyAt(index))); |
| 5624 | |
| 5625 | Object* value = dictionary->ValueAt(index); |
| 5626 | PropertyType type = dictionary->DetailsAt(index).type(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5627 | if (type == DATA && !value->IsJSFunction()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5628 | number_of_fields += 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5629 | } |
| 5630 | } |
| 5631 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5632 | Handle<Map> old_map(object->map(), isolate); |
| 5633 | |
| 5634 | int inobject_props = old_map->GetInObjectProperties(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5635 | |
| 5636 | // Allocate new map. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5637 | Handle<Map> new_map = Map::CopyDropDescriptors(old_map); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5638 | new_map->set_dictionary_map(false); |
| 5639 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5640 | UpdatePrototypeUserRegistration(old_map, new_map, isolate); |
| 5641 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5642 | #if TRACE_MAPS |
| 5643 | if (FLAG_trace_maps) { |
| 5644 | PrintF("[TraceMaps: SlowToFast from= %p to= %p reason= %s ]\n", |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5645 | reinterpret_cast<void*>(*old_map), reinterpret_cast<void*>(*new_map), |
| 5646 | reason); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5647 | } |
| 5648 | #endif |
| 5649 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5650 | if (instance_descriptor_length == 0) { |
| 5651 | DisallowHeapAllocation no_gc; |
| 5652 | DCHECK_LE(unused_property_fields, inobject_props); |
| 5653 | // Transform the object. |
| 5654 | new_map->set_unused_property_fields(inobject_props); |
| 5655 | object->synchronized_set_map(*new_map); |
| 5656 | object->set_properties(isolate->heap()->empty_fixed_array()); |
| 5657 | // Check that it really works. |
| 5658 | DCHECK(object->HasFastProperties()); |
| 5659 | return; |
| 5660 | } |
| 5661 | |
| 5662 | // Allocate the instance descriptor. |
| 5663 | Handle<DescriptorArray> descriptors = DescriptorArray::Allocate( |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5664 | isolate, instance_descriptor_length, 0, TENURED); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5665 | |
| 5666 | int number_of_allocated_fields = |
| 5667 | number_of_fields + unused_property_fields - inobject_props; |
| 5668 | if (number_of_allocated_fields < 0) { |
| 5669 | // There is enough inobject space for all fields (including unused). |
| 5670 | number_of_allocated_fields = 0; |
| 5671 | unused_property_fields = inobject_props - number_of_fields; |
| 5672 | } |
| 5673 | |
| 5674 | // Allocate the fixed array for the fields. |
| 5675 | Handle<FixedArray> fields = factory->NewFixedArray( |
| 5676 | number_of_allocated_fields); |
| 5677 | |
| 5678 | // Fill in the instance descriptor and the fields. |
| 5679 | int current_offset = 0; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5680 | for (int i = 0; i < instance_descriptor_length; i++) { |
| 5681 | int index = Smi::cast(iteration_order->get(i))->value(); |
| 5682 | Object* k = dictionary->KeyAt(index); |
| 5683 | DCHECK(dictionary->IsKey(k)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5684 | // Dictionary keys are internalized upon insertion. |
| 5685 | // TODO(jkummerow): Turn this into a DCHECK if it's not hit in the wild. |
| 5686 | CHECK(k->IsUniqueName()); |
| 5687 | Handle<Name> key(Name::cast(k), isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5688 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5689 | Object* value = dictionary->ValueAt(index); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5690 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5691 | PropertyDetails details = dictionary->DetailsAt(index); |
| 5692 | int enumeration_index = details.dictionary_index(); |
| 5693 | PropertyType type = details.type(); |
| 5694 | |
| 5695 | if (value->IsJSFunction()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5696 | DataConstantDescriptor d(key, handle(value, isolate), |
| 5697 | details.attributes()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5698 | descriptors->Set(enumeration_index - 1, &d); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5699 | } else if (type == DATA) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5700 | if (current_offset < inobject_props) { |
| 5701 | object->InObjectPropertyAtPut(current_offset, value, |
| 5702 | UPDATE_WRITE_BARRIER); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5703 | } else { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5704 | int offset = current_offset - inobject_props; |
| 5705 | fields->set(offset, value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5706 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5707 | DataDescriptor d(key, current_offset, details.attributes(), |
| 5708 | // TODO(verwaest): value->OptimalRepresentation(); |
| 5709 | Representation::Tagged()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5710 | current_offset += d.GetDetails().field_width_in_words(); |
| 5711 | descriptors->Set(enumeration_index - 1, &d); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5712 | } else if (type == ACCESSOR_CONSTANT) { |
| 5713 | AccessorConstantDescriptor d(key, handle(value, isolate), |
| 5714 | details.attributes()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5715 | descriptors->Set(enumeration_index - 1, &d); |
| 5716 | } else { |
| 5717 | UNREACHABLE(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5718 | } |
| 5719 | } |
| 5720 | DCHECK(current_offset == number_of_fields); |
| 5721 | |
| 5722 | descriptors->Sort(); |
| 5723 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5724 | Handle<LayoutDescriptor> layout_descriptor = LayoutDescriptor::New( |
| 5725 | new_map, descriptors, descriptors->number_of_descriptors()); |
| 5726 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5727 | DisallowHeapAllocation no_gc; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5728 | new_map->InitializeDescriptors(*descriptors, *layout_descriptor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5729 | new_map->set_unused_property_fields(unused_property_fields); |
| 5730 | |
| 5731 | // Transform the object. |
| 5732 | object->synchronized_set_map(*new_map); |
| 5733 | |
| 5734 | object->set_properties(*fields); |
| 5735 | DCHECK(object->IsJSObject()); |
| 5736 | |
| 5737 | // Check that it really works. |
| 5738 | DCHECK(object->HasFastProperties()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5739 | } |
| 5740 | |
| 5741 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5742 | void JSObject::ResetElements(Handle<JSObject> object) { |
| 5743 | Isolate* isolate = object->GetIsolate(); |
| 5744 | CHECK(object->map() != isolate->heap()->sloppy_arguments_elements_map()); |
| 5745 | if (object->map()->has_dictionary_elements()) { |
| 5746 | Handle<SeededNumberDictionary> new_elements = |
| 5747 | SeededNumberDictionary::New(isolate, 0); |
| 5748 | object->set_elements(*new_elements); |
| 5749 | } else { |
| 5750 | object->set_elements(object->map()->GetInitialElements()); |
| 5751 | } |
| 5752 | } |
| 5753 | |
| 5754 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5755 | void JSObject::RequireSlowElements(SeededNumberDictionary* dictionary) { |
| 5756 | if (dictionary->requires_slow_elements()) return; |
| 5757 | dictionary->set_requires_slow_elements(); |
| 5758 | // TODO(verwaest): Remove this hack. |
| 5759 | if (map()->is_prototype_map()) { |
| 5760 | TypeFeedbackVector::ClearAllKeyedStoreICs(GetIsolate()); |
| 5761 | } |
| 5762 | } |
| 5763 | |
| 5764 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5765 | Handle<SeededNumberDictionary> JSObject::NormalizeElements( |
| 5766 | Handle<JSObject> object) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5767 | DCHECK(!object->HasFixedTypedArrayElements()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5768 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5769 | bool is_arguments = object->HasSloppyArgumentsElements(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5770 | { |
| 5771 | DisallowHeapAllocation no_gc; |
| 5772 | FixedArrayBase* elements = object->elements(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5773 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5774 | if (is_arguments) { |
| 5775 | FixedArray* parameter_map = FixedArray::cast(elements); |
| 5776 | elements = FixedArrayBase::cast(parameter_map->get(1)); |
| 5777 | } |
| 5778 | |
| 5779 | if (elements->IsDictionary()) { |
| 5780 | return handle(SeededNumberDictionary::cast(elements), isolate); |
| 5781 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5782 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5783 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5784 | DCHECK(object->HasFastSmiOrObjectElements() || |
| 5785 | object->HasFastDoubleElements() || |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5786 | object->HasFastArgumentsElements() || |
| 5787 | object->HasFastStringWrapperElements()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5788 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5789 | Handle<SeededNumberDictionary> dictionary = |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5790 | object->GetElementsAccessor()->Normalize(object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5791 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5792 | // Switch to using the dictionary as the backing storage for elements. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5793 | ElementsKind target_kind = is_arguments |
| 5794 | ? SLOW_SLOPPY_ARGUMENTS_ELEMENTS |
| 5795 | : object->HasFastStringWrapperElements() |
| 5796 | ? SLOW_STRING_WRAPPER_ELEMENTS |
| 5797 | : DICTIONARY_ELEMENTS; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5798 | Handle<Map> new_map = JSObject::GetElementsTransitionMap(object, target_kind); |
| 5799 | // Set the new map first to satify the elements type assert in set_elements(). |
| 5800 | JSObject::MigrateToMap(object, new_map); |
| 5801 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5802 | if (is_arguments) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5803 | FixedArray::cast(object->elements())->set(1, *dictionary); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5804 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5805 | object->set_elements(*dictionary); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5806 | } |
| 5807 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5808 | isolate->counters()->elements_to_dictionary()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5809 | |
| 5810 | #ifdef DEBUG |
| 5811 | if (FLAG_trace_normalization) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5812 | OFStream os(stdout); |
| 5813 | os << "Object elements have been normalized:\n"; |
| 5814 | object->Print(os); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5815 | } |
| 5816 | #endif |
| 5817 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5818 | DCHECK(object->HasDictionaryElements() || |
| 5819 | object->HasSlowArgumentsElements() || |
| 5820 | object->HasSlowStringWrapperElements()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5821 | return dictionary; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5822 | } |
| 5823 | |
| 5824 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5825 | static Smi* GenerateIdentityHash(Isolate* isolate) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5826 | int hash_value; |
| 5827 | int attempts = 0; |
| 5828 | do { |
| 5829 | // Generate a random 32-bit hash value but limit range to fit |
| 5830 | // within a smi. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5831 | hash_value = isolate->random_number_generator()->NextInt() & Smi::kMaxValue; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5832 | attempts++; |
| 5833 | } while (hash_value == 0 && attempts < 30); |
| 5834 | hash_value = hash_value != 0 ? hash_value : 1; // never return 0 |
| 5835 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5836 | return Smi::FromInt(hash_value); |
| 5837 | } |
| 5838 | |
| 5839 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5840 | template<typename ProxyType> |
| 5841 | static Handle<Smi> GetOrCreateIdentityHashHelper(Handle<ProxyType> proxy) { |
| 5842 | Isolate* isolate = proxy->GetIsolate(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5843 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5844 | Handle<Object> maybe_hash(proxy->hash(), isolate); |
| 5845 | if (maybe_hash->IsSmi()) return Handle<Smi>::cast(maybe_hash); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5846 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5847 | Handle<Smi> hash(GenerateIdentityHash(isolate), isolate); |
| 5848 | proxy->set_hash(*hash); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5849 | return hash; |
| 5850 | } |
| 5851 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5852 | // static |
| 5853 | Handle<Object> JSObject::GetIdentityHash(Isolate* isolate, |
| 5854 | Handle<JSObject> object) { |
| 5855 | if (object->IsJSGlobalProxy()) { |
| 5856 | return handle(JSGlobalProxy::cast(*object)->hash(), isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5857 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5858 | Handle<Name> hash_code_symbol = isolate->factory()->hash_code_symbol(); |
| 5859 | return JSReceiver::GetDataProperty(object, hash_code_symbol); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5860 | } |
| 5861 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5862 | // static |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5863 | Handle<Smi> JSObject::GetOrCreateIdentityHash(Handle<JSObject> object) { |
| 5864 | if (object->IsJSGlobalProxy()) { |
| 5865 | return GetOrCreateIdentityHashHelper(Handle<JSGlobalProxy>::cast(object)); |
| 5866 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5867 | Isolate* isolate = object->GetIsolate(); |
| 5868 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5869 | Handle<Name> hash_code_symbol = isolate->factory()->hash_code_symbol(); |
| 5870 | LookupIterator it(object, hash_code_symbol, object, LookupIterator::OWN); |
| 5871 | if (it.IsFound()) { |
| 5872 | DCHECK_EQ(LookupIterator::DATA, it.state()); |
| 5873 | Handle<Object> maybe_hash = it.GetDataValue(); |
| 5874 | if (maybe_hash->IsSmi()) return Handle<Smi>::cast(maybe_hash); |
| 5875 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5876 | |
| 5877 | Handle<Smi> hash(GenerateIdentityHash(isolate), isolate); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5878 | CHECK(AddDataProperty(&it, hash, NONE, THROW_ON_ERROR, |
| 5879 | CERTAINLY_NOT_STORE_FROM_KEYED) |
| 5880 | .IsJust()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5881 | return hash; |
| 5882 | } |
| 5883 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5884 | // static |
| 5885 | Handle<Object> JSProxy::GetIdentityHash(Isolate* isolate, |
| 5886 | Handle<JSProxy> proxy) { |
| 5887 | return handle(proxy->hash(), isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5888 | } |
| 5889 | |
| 5890 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5891 | Handle<Smi> JSProxy::GetOrCreateIdentityHash(Handle<JSProxy> proxy) { |
| 5892 | return GetOrCreateIdentityHashHelper(proxy); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5893 | } |
| 5894 | |
| 5895 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5896 | Object* JSObject::GetHiddenProperty(Handle<Name> key) { |
| 5897 | DisallowHeapAllocation no_gc; |
| 5898 | DCHECK(key->IsUniqueName()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5899 | if (IsJSGlobalProxy()) { |
| 5900 | // For a proxy, use the prototype as target object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5901 | PrototypeIterator iter(GetIsolate(), this); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5902 | // If the proxy is detached, return undefined. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5903 | if (iter.IsAtEnd()) return GetHeap()->the_hole_value(); |
| 5904 | DCHECK(iter.GetCurrent()->IsJSGlobalObject()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5905 | return iter.GetCurrent<JSObject>()->GetHiddenProperty(key); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5906 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5907 | DCHECK(!IsJSGlobalProxy()); |
| 5908 | Object* inline_value = GetHiddenPropertiesHashTable(); |
| 5909 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5910 | if (inline_value->IsUndefined()) return GetHeap()->the_hole_value(); |
| 5911 | |
| 5912 | ObjectHashTable* hashtable = ObjectHashTable::cast(inline_value); |
| 5913 | Object* entry = hashtable->Lookup(key); |
| 5914 | return entry; |
| 5915 | } |
| 5916 | |
| 5917 | |
| 5918 | Handle<Object> JSObject::SetHiddenProperty(Handle<JSObject> object, |
| 5919 | Handle<Name> key, |
| 5920 | Handle<Object> value) { |
| 5921 | Isolate* isolate = object->GetIsolate(); |
| 5922 | |
| 5923 | DCHECK(key->IsUniqueName()); |
| 5924 | if (object->IsJSGlobalProxy()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5925 | // For a proxy, use the prototype as target object. |
| 5926 | PrototypeIterator iter(isolate, object); |
| 5927 | // If the proxy is detached, return undefined. |
| 5928 | if (iter.IsAtEnd()) return isolate->factory()->undefined_value(); |
| 5929 | DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5930 | return SetHiddenProperty(PrototypeIterator::GetCurrent<JSObject>(iter), key, |
| 5931 | value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5932 | } |
| 5933 | DCHECK(!object->IsJSGlobalProxy()); |
| 5934 | |
| 5935 | Handle<Object> inline_value(object->GetHiddenPropertiesHashTable(), isolate); |
| 5936 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5937 | Handle<ObjectHashTable> hashtable = |
| 5938 | GetOrCreateHiddenPropertiesHashtable(object); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5939 | |
| 5940 | // 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] | 5941 | Handle<ObjectHashTable> new_table = ObjectHashTable::Put(hashtable, key, |
| 5942 | value); |
| 5943 | if (*new_table != *hashtable) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5944 | // If adding the key expanded the dictionary (i.e., Add returned a new |
| 5945 | // dictionary), store it back to the object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5946 | SetHiddenPropertiesHashTable(object, new_table); |
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 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5949 | // Return this to mark success. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5950 | return object; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5951 | } |
| 5952 | |
| 5953 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5954 | void JSObject::DeleteHiddenProperty(Handle<JSObject> object, Handle<Name> key) { |
| 5955 | Isolate* isolate = object->GetIsolate(); |
| 5956 | DCHECK(key->IsUniqueName()); |
| 5957 | |
| 5958 | if (object->IsJSGlobalProxy()) { |
| 5959 | PrototypeIterator iter(isolate, object); |
| 5960 | if (iter.IsAtEnd()) return; |
| 5961 | DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5962 | return DeleteHiddenProperty(PrototypeIterator::GetCurrent<JSObject>(iter), |
| 5963 | key); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5964 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5965 | |
| 5966 | Object* inline_value = object->GetHiddenPropertiesHashTable(); |
| 5967 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5968 | if (inline_value->IsUndefined()) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5969 | |
| 5970 | Handle<ObjectHashTable> hashtable(ObjectHashTable::cast(inline_value)); |
| 5971 | bool was_present = false; |
| 5972 | ObjectHashTable::Remove(hashtable, key, &was_present); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5973 | } |
| 5974 | |
| 5975 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5976 | bool JSObject::HasHiddenProperties(Handle<JSObject> object) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5977 | Isolate* isolate = object->GetIsolate(); |
| 5978 | Handle<Symbol> hidden = isolate->factory()->hidden_properties_symbol(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5979 | LookupIterator it(object, hidden, object); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5980 | Maybe<PropertyAttributes> maybe = GetPropertyAttributes(&it); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5981 | // Cannot get an exception since the hidden_properties_symbol isn't exposed to |
| 5982 | // JS. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5983 | DCHECK(maybe.IsJust()); |
| 5984 | return maybe.FromJust() != ABSENT; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5985 | } |
| 5986 | |
| 5987 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5988 | Object* JSObject::GetHiddenPropertiesHashTable() { |
| 5989 | DCHECK(!IsJSGlobalProxy()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5990 | if (HasFastProperties()) { |
| 5991 | // If the object has fast properties, check whether the first slot |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5992 | // in the descriptor array matches the hidden string. Since the |
| 5993 | // hidden strings hash code is zero (and no other name has hash |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5994 | // code zero) it will always occupy the first entry if present. |
| 5995 | DescriptorArray* descriptors = this->map()->instance_descriptors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5996 | if (descriptors->number_of_descriptors() > 0) { |
| 5997 | int sorted_index = descriptors->GetSortedKeyIndex(0); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5998 | if (descriptors->GetKey(sorted_index) == |
| 5999 | GetHeap()->hidden_properties_symbol() && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6000 | sorted_index < map()->NumberOfOwnDescriptors()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6001 | DCHECK(descriptors->GetType(sorted_index) == DATA); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6002 | DCHECK(descriptors->GetDetails(sorted_index).representation(). |
| 6003 | IsCompatibleForLoad(Representation::Tagged())); |
| 6004 | FieldIndex index = FieldIndex::ForDescriptor(this->map(), |
| 6005 | sorted_index); |
| 6006 | return this->RawFastPropertyAt(index); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6007 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6008 | return GetHeap()->undefined_value(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6009 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6010 | } else { |
| 6011 | return GetHeap()->undefined_value(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6012 | } |
| 6013 | } else { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6014 | Isolate* isolate = GetIsolate(); |
| 6015 | Handle<Symbol> hidden = isolate->factory()->hidden_properties_symbol(); |
| 6016 | Handle<JSObject> receiver(this, isolate); |
| 6017 | LookupIterator it(receiver, hidden, receiver); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6018 | // Access check is always skipped for the hidden string anyways. |
| 6019 | return *GetDataProperty(&it); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6020 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6021 | } |
| 6022 | |
| 6023 | Handle<ObjectHashTable> JSObject::GetOrCreateHiddenPropertiesHashtable( |
| 6024 | Handle<JSObject> object) { |
| 6025 | Isolate* isolate = object->GetIsolate(); |
| 6026 | |
| 6027 | static const int kInitialCapacity = 4; |
| 6028 | Handle<Object> inline_value(object->GetHiddenPropertiesHashTable(), isolate); |
| 6029 | if (inline_value->IsHashTable()) { |
| 6030 | return Handle<ObjectHashTable>::cast(inline_value); |
| 6031 | } |
| 6032 | |
| 6033 | Handle<ObjectHashTable> hashtable = ObjectHashTable::New( |
| 6034 | isolate, kInitialCapacity, USE_CUSTOM_MINIMUM_CAPACITY); |
| 6035 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6036 | DCHECK(inline_value->IsUndefined()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6037 | SetHiddenPropertiesHashTable(object, hashtable); |
| 6038 | return hashtable; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6039 | } |
| 6040 | |
| 6041 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6042 | Handle<Object> JSObject::SetHiddenPropertiesHashTable(Handle<JSObject> object, |
| 6043 | Handle<Object> value) { |
| 6044 | DCHECK(!object->IsJSGlobalProxy()); |
| 6045 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 6046 | Handle<Symbol> name = isolate->factory()->hidden_properties_symbol(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6047 | SetOwnPropertyIgnoreAttributes(object, name, value, DONT_ENUM).Assert(); |
| 6048 | return object; |
| 6049 | } |
| 6050 | |
| 6051 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 6052 | Maybe<bool> JSObject::DeletePropertyWithInterceptor(LookupIterator* it, |
| 6053 | ShouldThrow should_throw) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6054 | Isolate* isolate = it->isolate(); |
| 6055 | // Make sure that the top context does not change when doing callbacks or |
| 6056 | // interceptor calls. |
| 6057 | AssertNoContextChange ncc(isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6058 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6059 | DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state()); |
| 6060 | Handle<InterceptorInfo> interceptor(it->GetInterceptor()); |
| 6061 | if (interceptor->deleter()->IsUndefined()) return Nothing<bool>(); |
| 6062 | |
| 6063 | Handle<JSObject> holder = it->GetHolder<JSObject>(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6064 | Handle<Object> receiver = it->GetReceiver(); |
| 6065 | if (!receiver->IsJSReceiver()) { |
| 6066 | ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, receiver, |
| 6067 | Object::ConvertReceiver(isolate, receiver), |
| 6068 | Nothing<bool>()); |
| 6069 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6070 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6071 | PropertyCallbackArguments args(isolate, interceptor->data(), *receiver, |
| 6072 | *holder, should_throw); |
| 6073 | Handle<Object> result; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6074 | if (it->IsElement()) { |
| 6075 | uint32_t index = it->index(); |
| 6076 | v8::IndexedPropertyDeleterCallback deleter = |
| 6077 | v8::ToCData<v8::IndexedPropertyDeleterCallback>(interceptor->deleter()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6078 | result = args.Call(deleter, index); |
| 6079 | } else if (it->name()->IsSymbol() && !interceptor->can_intercept_symbols()) { |
| 6080 | return Nothing<bool>(); |
| 6081 | } else { |
| 6082 | Handle<Name> name = it->name(); |
| 6083 | DCHECK(!name->IsPrivate()); |
| 6084 | v8::GenericNamedPropertyDeleterCallback deleter = |
| 6085 | v8::ToCData<v8::GenericNamedPropertyDeleterCallback>( |
| 6086 | interceptor->deleter()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6087 | result = args.Call(deleter, name); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6088 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6089 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6090 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6091 | if (result.is_null()) return Nothing<bool>(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6092 | |
| 6093 | DCHECK(result->IsBoolean()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6094 | // Rebox CustomArguments::kReturnValueOffset before returning. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6095 | return Just(result->IsTrue()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6096 | } |
| 6097 | |
| 6098 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6099 | void JSReceiver::DeleteNormalizedProperty(Handle<JSReceiver> object, |
| 6100 | Handle<Name> name, int entry) { |
| 6101 | DCHECK(!object->HasFastProperties()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6102 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6103 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6104 | if (object->IsJSGlobalObject()) { |
| 6105 | // If we have a global object, invalidate the cell and swap in a new one. |
| 6106 | Handle<GlobalDictionary> dictionary( |
| 6107 | JSObject::cast(*object)->global_dictionary()); |
| 6108 | DCHECK_NE(GlobalDictionary::kNotFound, entry); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6109 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6110 | auto cell = PropertyCell::InvalidateEntry(dictionary, entry); |
| 6111 | cell->set_value(isolate->heap()->the_hole_value()); |
| 6112 | // TODO(ishell): InvalidateForDelete |
| 6113 | cell->set_property_details( |
| 6114 | cell->property_details().set_cell_type(PropertyCellType::kInvalidated)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6115 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6116 | Handle<NameDictionary> dictionary(object->property_dictionary()); |
| 6117 | DCHECK_NE(NameDictionary::kNotFound, entry); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6118 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6119 | NameDictionary::DeleteProperty(dictionary, entry); |
| 6120 | Handle<NameDictionary> new_properties = |
| 6121 | NameDictionary::Shrink(dictionary, name); |
| 6122 | object->set_properties(*new_properties); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6123 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6124 | } |
| 6125 | |
| 6126 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6127 | Maybe<bool> JSReceiver::DeleteProperty(LookupIterator* it, |
| 6128 | LanguageMode language_mode) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 6129 | it->UpdateProtector(); |
| 6130 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6131 | Isolate* isolate = it->isolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6132 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6133 | if (it->state() == LookupIterator::JSPROXY) { |
| 6134 | return JSProxy::DeletePropertyOrElement(it->GetHolder<JSProxy>(), |
| 6135 | it->GetName(), language_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6136 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6137 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6138 | if (it->GetReceiver()->IsJSProxy()) { |
| 6139 | if (it->state() != LookupIterator::NOT_FOUND) { |
| 6140 | DCHECK_EQ(LookupIterator::DATA, it->state()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 6141 | DCHECK(it->name()->IsPrivate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6142 | it->Delete(); |
| 6143 | } |
| 6144 | return Just(true); |
| 6145 | } |
| 6146 | Handle<JSObject> receiver = Handle<JSObject>::cast(it->GetReceiver()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6147 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 6148 | bool is_observed = receiver->map()->is_observed() && |
| 6149 | (it->IsElement() || !it->name()->IsPrivate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6150 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6151 | Handle<Object> old_value = it->factory()->the_hole_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6152 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6153 | for (; it->IsFound(); it->Next()) { |
| 6154 | switch (it->state()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6155 | case LookupIterator::JSPROXY: |
| 6156 | case LookupIterator::NOT_FOUND: |
| 6157 | case LookupIterator::TRANSITION: |
| 6158 | UNREACHABLE(); |
| 6159 | case LookupIterator::ACCESS_CHECK: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6160 | if (it->HasAccess()) break; |
| 6161 | isolate->ReportFailedAccessCheck(it->GetHolder<JSObject>()); |
| 6162 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); |
| 6163 | return Just(false); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6164 | case LookupIterator::INTERCEPTOR: { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 6165 | ShouldThrow should_throw = |
| 6166 | is_sloppy(language_mode) ? DONT_THROW : THROW_ON_ERROR; |
| 6167 | Maybe<bool> result = |
| 6168 | JSObject::DeletePropertyWithInterceptor(it, should_throw); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6169 | // An exception was thrown in the interceptor. Propagate. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6170 | if (isolate->has_pending_exception()) return Nothing<bool>(); |
| 6171 | // Delete with interceptor succeeded. Return result. |
| 6172 | // TODO(neis): In strict mode, we should probably throw if the |
| 6173 | // interceptor returns false. |
| 6174 | if (result.IsJust()) return result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6175 | break; |
| 6176 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6177 | case LookupIterator::INTEGER_INDEXED_EXOTIC: |
| 6178 | return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6179 | case LookupIterator::DATA: |
| 6180 | if (is_observed) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6181 | old_value = it->GetDataValue(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6182 | } |
| 6183 | // Fall through. |
| 6184 | case LookupIterator::ACCESSOR: { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6185 | if (!it->IsConfigurable()) { |
| 6186 | // Fail if the property is not configurable. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6187 | if (is_strict(language_mode)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6188 | isolate->Throw(*isolate->factory()->NewTypeError( |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6189 | MessageTemplate::kStrictDeleteProperty, it->GetName(), |
| 6190 | receiver)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6191 | return Nothing<bool>(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6192 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6193 | return Just(false); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6194 | } |
| 6195 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6196 | it->Delete(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6197 | |
| 6198 | if (is_observed) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6199 | RETURN_ON_EXCEPTION_VALUE( |
| 6200 | isolate, JSObject::EnqueueChangeRecord(receiver, "delete", |
| 6201 | it->GetName(), old_value), |
| 6202 | Nothing<bool>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6203 | } |
| 6204 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6205 | return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6206 | } |
| 6207 | } |
| 6208 | } |
| 6209 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6210 | return Just(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6211 | } |
| 6212 | |
| 6213 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6214 | Maybe<bool> JSReceiver::DeleteElement(Handle<JSReceiver> object, uint32_t index, |
| 6215 | LanguageMode language_mode) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6216 | LookupIterator it(object->GetIsolate(), object, index, object, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6217 | LookupIterator::HIDDEN); |
| 6218 | return DeleteProperty(&it, language_mode); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6219 | } |
| 6220 | |
| 6221 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6222 | Maybe<bool> JSReceiver::DeleteProperty(Handle<JSReceiver> object, |
| 6223 | Handle<Name> name, |
| 6224 | LanguageMode language_mode) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6225 | LookupIterator it(object, name, object, LookupIterator::HIDDEN); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6226 | return DeleteProperty(&it, language_mode); |
| 6227 | } |
| 6228 | |
| 6229 | |
| 6230 | Maybe<bool> JSReceiver::DeletePropertyOrElement(Handle<JSReceiver> object, |
| 6231 | Handle<Name> name, |
| 6232 | LanguageMode language_mode) { |
| 6233 | LookupIterator it = LookupIterator::PropertyOrElement( |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6234 | name->GetIsolate(), object, name, object, LookupIterator::HIDDEN); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6235 | return DeleteProperty(&it, language_mode); |
| 6236 | } |
| 6237 | |
| 6238 | |
| 6239 | // ES6 7.1.14 |
| 6240 | MaybeHandle<Object> ToPropertyKey(Isolate* isolate, Handle<Object> value) { |
| 6241 | // 1. Let key be ToPrimitive(argument, hint String). |
| 6242 | MaybeHandle<Object> maybe_key = |
| 6243 | Object::ToPrimitive(value, ToPrimitiveHint::kString); |
| 6244 | // 2. ReturnIfAbrupt(key). |
| 6245 | Handle<Object> key; |
| 6246 | if (!maybe_key.ToHandle(&key)) return key; |
| 6247 | // 3. If Type(key) is Symbol, then return key. |
| 6248 | if (key->IsSymbol()) return key; |
| 6249 | // 4. Return ToString(key). |
| 6250 | // Extending spec'ed behavior, we'd be happy to return an element index. |
| 6251 | if (key->IsSmi()) return key; |
| 6252 | if (key->IsHeapNumber()) { |
| 6253 | uint32_t uint_value; |
| 6254 | if (value->ToArrayLength(&uint_value) && |
| 6255 | uint_value <= static_cast<uint32_t>(Smi::kMaxValue)) { |
| 6256 | return handle(Smi::FromInt(static_cast<int>(uint_value)), isolate); |
| 6257 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6258 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6259 | return Object::ToString(isolate, key); |
| 6260 | } |
| 6261 | |
| 6262 | |
| 6263 | // ES6 19.1.2.4 |
| 6264 | // static |
| 6265 | Object* JSReceiver::DefineProperty(Isolate* isolate, Handle<Object> object, |
| 6266 | Handle<Object> key, |
| 6267 | Handle<Object> attributes) { |
| 6268 | // 1. If Type(O) is not Object, throw a TypeError exception. |
| 6269 | if (!object->IsJSReceiver()) { |
| 6270 | Handle<String> fun_name = |
| 6271 | isolate->factory()->InternalizeUtf8String("Object.defineProperty"); |
| 6272 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 6273 | isolate, NewTypeError(MessageTemplate::kCalledOnNonObject, fun_name)); |
| 6274 | } |
| 6275 | // 2. Let key be ToPropertyKey(P). |
| 6276 | // 3. ReturnIfAbrupt(key). |
| 6277 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, key, ToPropertyKey(isolate, key)); |
| 6278 | // 4. Let desc be ToPropertyDescriptor(Attributes). |
| 6279 | // 5. ReturnIfAbrupt(desc). |
| 6280 | PropertyDescriptor desc; |
| 6281 | if (!PropertyDescriptor::ToPropertyDescriptor(isolate, attributes, &desc)) { |
| 6282 | return isolate->heap()->exception(); |
| 6283 | } |
| 6284 | // 6. Let success be DefinePropertyOrThrow(O,key, desc). |
| 6285 | Maybe<bool> success = DefineOwnProperty( |
| 6286 | isolate, Handle<JSReceiver>::cast(object), key, &desc, THROW_ON_ERROR); |
| 6287 | // 7. ReturnIfAbrupt(success). |
| 6288 | MAYBE_RETURN(success, isolate->heap()->exception()); |
| 6289 | CHECK(success.FromJust()); |
| 6290 | // 8. Return O. |
| 6291 | return *object; |
| 6292 | } |
| 6293 | |
| 6294 | |
| 6295 | // ES6 19.1.2.3.1 |
| 6296 | // static |
| 6297 | MaybeHandle<Object> JSReceiver::DefineProperties(Isolate* isolate, |
| 6298 | Handle<Object> object, |
| 6299 | Handle<Object> properties) { |
| 6300 | // 1. If Type(O) is not Object, throw a TypeError exception. |
| 6301 | if (!object->IsJSReceiver()) { |
| 6302 | Handle<String> fun_name = |
| 6303 | isolate->factory()->InternalizeUtf8String("Object.defineProperties"); |
| 6304 | THROW_NEW_ERROR(isolate, |
| 6305 | NewTypeError(MessageTemplate::kCalledOnNonObject, fun_name), |
| 6306 | Object); |
| 6307 | } |
| 6308 | // 2. Let props be ToObject(Properties). |
| 6309 | // 3. ReturnIfAbrupt(props). |
| 6310 | Handle<JSReceiver> props; |
| 6311 | if (!Object::ToObject(isolate, properties).ToHandle(&props)) { |
| 6312 | THROW_NEW_ERROR(isolate, |
| 6313 | NewTypeError(MessageTemplate::kUndefinedOrNullToObject), |
| 6314 | Object); |
| 6315 | } |
| 6316 | // 4. Let keys be props.[[OwnPropertyKeys]](). |
| 6317 | // 5. ReturnIfAbrupt(keys). |
| 6318 | Handle<FixedArray> keys; |
| 6319 | ASSIGN_RETURN_ON_EXCEPTION( |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 6320 | isolate, keys, JSReceiver::GetKeys(props, OWN_ONLY, ALL_PROPERTIES), |
| 6321 | Object); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6322 | // 6. Let descriptors be an empty List. |
| 6323 | int capacity = keys->length(); |
| 6324 | std::vector<PropertyDescriptor> descriptors(capacity); |
| 6325 | size_t descriptors_index = 0; |
| 6326 | // 7. Repeat for each element nextKey of keys in List order, |
| 6327 | for (int i = 0; i < keys->length(); ++i) { |
| 6328 | Handle<Object> next_key(keys->get(i), isolate); |
| 6329 | // 7a. Let propDesc be props.[[GetOwnProperty]](nextKey). |
| 6330 | // 7b. ReturnIfAbrupt(propDesc). |
| 6331 | bool success = false; |
| 6332 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 6333 | isolate, props, next_key, &success, LookupIterator::HIDDEN); |
| 6334 | DCHECK(success); |
| 6335 | Maybe<PropertyAttributes> maybe = JSReceiver::GetPropertyAttributes(&it); |
| 6336 | if (!maybe.IsJust()) return MaybeHandle<Object>(); |
| 6337 | PropertyAttributes attrs = maybe.FromJust(); |
| 6338 | // 7c. If propDesc is not undefined and propDesc.[[Enumerable]] is true: |
| 6339 | if (attrs == ABSENT) continue; |
| 6340 | if (attrs & DONT_ENUM) continue; |
| 6341 | // 7c i. Let descObj be Get(props, nextKey). |
| 6342 | // 7c ii. ReturnIfAbrupt(descObj). |
| 6343 | Handle<Object> desc_obj; |
| 6344 | ASSIGN_RETURN_ON_EXCEPTION(isolate, desc_obj, Object::GetProperty(&it), |
| 6345 | Object); |
| 6346 | // 7c iii. Let desc be ToPropertyDescriptor(descObj). |
| 6347 | success = PropertyDescriptor::ToPropertyDescriptor( |
| 6348 | isolate, desc_obj, &descriptors[descriptors_index]); |
| 6349 | // 7c iv. ReturnIfAbrupt(desc). |
| 6350 | if (!success) return MaybeHandle<Object>(); |
| 6351 | // 7c v. Append the pair (a two element List) consisting of nextKey and |
| 6352 | // desc to the end of descriptors. |
| 6353 | descriptors[descriptors_index].set_name(next_key); |
| 6354 | descriptors_index++; |
| 6355 | } |
| 6356 | // 8. For each pair from descriptors in list order, |
| 6357 | for (size_t i = 0; i < descriptors_index; ++i) { |
| 6358 | PropertyDescriptor* desc = &descriptors[i]; |
| 6359 | // 8a. Let P be the first element of pair. |
| 6360 | // 8b. Let desc be the second element of pair. |
| 6361 | // 8c. Let status be DefinePropertyOrThrow(O, P, desc). |
| 6362 | Maybe<bool> status = |
| 6363 | DefineOwnProperty(isolate, Handle<JSReceiver>::cast(object), |
| 6364 | desc->name(), desc, THROW_ON_ERROR); |
| 6365 | // 8d. ReturnIfAbrupt(status). |
| 6366 | if (!status.IsJust()) return MaybeHandle<Object>(); |
| 6367 | CHECK(status.FromJust()); |
| 6368 | } |
| 6369 | // 9. Return o. |
| 6370 | return object; |
| 6371 | } |
| 6372 | |
| 6373 | |
| 6374 | // static |
| 6375 | Maybe<bool> JSReceiver::DefineOwnProperty(Isolate* isolate, |
| 6376 | Handle<JSReceiver> object, |
| 6377 | Handle<Object> key, |
| 6378 | PropertyDescriptor* desc, |
| 6379 | ShouldThrow should_throw) { |
| 6380 | if (object->IsJSArray()) { |
| 6381 | return JSArray::DefineOwnProperty(isolate, Handle<JSArray>::cast(object), |
| 6382 | key, desc, should_throw); |
| 6383 | } |
| 6384 | if (object->IsJSProxy()) { |
| 6385 | return JSProxy::DefineOwnProperty(isolate, Handle<JSProxy>::cast(object), |
| 6386 | key, desc, should_throw); |
| 6387 | } |
| 6388 | // TODO(jkummerow): Support Modules (ES6 9.4.6.6) |
| 6389 | |
| 6390 | // OrdinaryDefineOwnProperty, by virtue of calling |
| 6391 | // DefineOwnPropertyIgnoreAttributes, can handle arguments (ES6 9.4.4.2) |
| 6392 | // and IntegerIndexedExotics (ES6 9.4.5.3), with one exception: |
| 6393 | // TODO(jkummerow): Setting an indexed accessor on a typed array should throw. |
| 6394 | return OrdinaryDefineOwnProperty(isolate, Handle<JSObject>::cast(object), key, |
| 6395 | desc, should_throw); |
| 6396 | } |
| 6397 | |
| 6398 | |
| 6399 | // static |
| 6400 | Maybe<bool> JSReceiver::OrdinaryDefineOwnProperty(Isolate* isolate, |
| 6401 | Handle<JSObject> object, |
| 6402 | Handle<Object> key, |
| 6403 | PropertyDescriptor* desc, |
| 6404 | ShouldThrow should_throw) { |
| 6405 | bool success = false; |
| 6406 | DCHECK(key->IsName() || key->IsNumber()); // |key| is a PropertyKey... |
| 6407 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 6408 | isolate, object, key, &success, LookupIterator::HIDDEN); |
| 6409 | DCHECK(success); // ...so creating a LookupIterator can't fail. |
| 6410 | |
| 6411 | // Deal with access checks first. |
| 6412 | if (it.state() == LookupIterator::ACCESS_CHECK) { |
| 6413 | if (!it.HasAccess()) { |
| 6414 | isolate->ReportFailedAccessCheck(it.GetHolder<JSObject>()); |
| 6415 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); |
| 6416 | return Just(true); |
| 6417 | } |
| 6418 | it.Next(); |
| 6419 | } |
| 6420 | |
| 6421 | return OrdinaryDefineOwnProperty(&it, desc, should_throw); |
| 6422 | } |
| 6423 | |
| 6424 | |
| 6425 | // ES6 9.1.6.1 |
| 6426 | // static |
| 6427 | Maybe<bool> JSReceiver::OrdinaryDefineOwnProperty(LookupIterator* it, |
| 6428 | PropertyDescriptor* desc, |
| 6429 | ShouldThrow should_throw) { |
| 6430 | Isolate* isolate = it->isolate(); |
| 6431 | // 1. Let current be O.[[GetOwnProperty]](P). |
| 6432 | // 2. ReturnIfAbrupt(current). |
| 6433 | PropertyDescriptor current; |
| 6434 | MAYBE_RETURN(GetOwnPropertyDescriptor(it, ¤t), Nothing<bool>()); |
| 6435 | |
| 6436 | // TODO(jkummerow/verwaest): It would be nice if we didn't have to reset |
| 6437 | // the iterator every time. Currently, the reasons why we need it are: |
| 6438 | // - handle interceptors correctly |
| 6439 | // - handle accessors correctly (which might change the holder's map) |
| 6440 | it->Restart(); |
| 6441 | // 3. Let extensible be the value of the [[Extensible]] internal slot of O. |
| 6442 | Handle<JSObject> object = Handle<JSObject>::cast(it->GetReceiver()); |
| 6443 | bool extensible = JSObject::IsExtensible(object); |
| 6444 | |
| 6445 | return ValidateAndApplyPropertyDescriptor(isolate, it, extensible, desc, |
| 6446 | ¤t, should_throw); |
| 6447 | } |
| 6448 | |
| 6449 | |
| 6450 | // ES6 9.1.6.2 |
| 6451 | // static |
| 6452 | Maybe<bool> JSReceiver::IsCompatiblePropertyDescriptor( |
| 6453 | Isolate* isolate, bool extensible, PropertyDescriptor* desc, |
| 6454 | PropertyDescriptor* current, Handle<Name> property_name, |
| 6455 | ShouldThrow should_throw) { |
| 6456 | // 1. Return ValidateAndApplyPropertyDescriptor(undefined, undefined, |
| 6457 | // Extensible, Desc, Current). |
| 6458 | return ValidateAndApplyPropertyDescriptor( |
| 6459 | isolate, NULL, extensible, desc, current, should_throw, property_name); |
| 6460 | } |
| 6461 | |
| 6462 | |
| 6463 | // ES6 9.1.6.3 |
| 6464 | // static |
| 6465 | Maybe<bool> JSReceiver::ValidateAndApplyPropertyDescriptor( |
| 6466 | Isolate* isolate, LookupIterator* it, bool extensible, |
| 6467 | PropertyDescriptor* desc, PropertyDescriptor* current, |
| 6468 | ShouldThrow should_throw, Handle<Name> property_name) { |
| 6469 | // We either need a LookupIterator, or a property name. |
| 6470 | DCHECK((it == NULL) != property_name.is_null()); |
| 6471 | Handle<JSObject> object; |
| 6472 | if (it != NULL) object = Handle<JSObject>::cast(it->GetReceiver()); |
| 6473 | bool desc_is_data_descriptor = PropertyDescriptor::IsDataDescriptor(desc); |
| 6474 | bool desc_is_accessor_descriptor = |
| 6475 | PropertyDescriptor::IsAccessorDescriptor(desc); |
| 6476 | bool desc_is_generic_descriptor = |
| 6477 | PropertyDescriptor::IsGenericDescriptor(desc); |
| 6478 | // 1. (Assert) |
| 6479 | // 2. If current is undefined, then |
| 6480 | if (current->is_empty()) { |
| 6481 | // 2a. If extensible is false, return false. |
| 6482 | if (!extensible) { |
| 6483 | RETURN_FAILURE(isolate, should_throw, |
| 6484 | NewTypeError(MessageTemplate::kDefineDisallowed, |
| 6485 | it != NULL ? it->GetName() : property_name)); |
| 6486 | } |
| 6487 | // 2c. If IsGenericDescriptor(Desc) or IsDataDescriptor(Desc) is true, then: |
| 6488 | // (This is equivalent to !IsAccessorDescriptor(desc).) |
| 6489 | DCHECK((desc_is_generic_descriptor || desc_is_data_descriptor) == |
| 6490 | !desc_is_accessor_descriptor); |
| 6491 | if (!desc_is_accessor_descriptor) { |
| 6492 | // 2c i. If O is not undefined, create an own data property named P of |
| 6493 | // object O whose [[Value]], [[Writable]], [[Enumerable]] and |
| 6494 | // [[Configurable]] attribute values are described by Desc. If the value |
| 6495 | // of an attribute field of Desc is absent, the attribute of the newly |
| 6496 | // created property is set to its default value. |
| 6497 | if (it != NULL) { |
| 6498 | if (!desc->has_writable()) desc->set_writable(false); |
| 6499 | if (!desc->has_enumerable()) desc->set_enumerable(false); |
| 6500 | if (!desc->has_configurable()) desc->set_configurable(false); |
| 6501 | Handle<Object> value( |
| 6502 | desc->has_value() |
| 6503 | ? desc->value() |
| 6504 | : Handle<Object>::cast(isolate->factory()->undefined_value())); |
| 6505 | MaybeHandle<Object> result = |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 6506 | JSObject::DefineOwnPropertyIgnoreAttributes(it, value, |
| 6507 | desc->ToAttributes()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6508 | if (result.is_null()) return Nothing<bool>(); |
| 6509 | } |
| 6510 | } else { |
| 6511 | // 2d. Else Desc must be an accessor Property Descriptor, |
| 6512 | DCHECK(desc_is_accessor_descriptor); |
| 6513 | // 2d i. If O is not undefined, create an own accessor property named P |
| 6514 | // of object O whose [[Get]], [[Set]], [[Enumerable]] and |
| 6515 | // [[Configurable]] attribute values are described by Desc. If the value |
| 6516 | // of an attribute field of Desc is absent, the attribute of the newly |
| 6517 | // created property is set to its default value. |
| 6518 | if (it != NULL) { |
| 6519 | if (!desc->has_enumerable()) desc->set_enumerable(false); |
| 6520 | if (!desc->has_configurable()) desc->set_configurable(false); |
| 6521 | Handle<Object> getter( |
| 6522 | desc->has_get() |
| 6523 | ? desc->get() |
| 6524 | : Handle<Object>::cast(isolate->factory()->null_value())); |
| 6525 | Handle<Object> setter( |
| 6526 | desc->has_set() |
| 6527 | ? desc->set() |
| 6528 | : Handle<Object>::cast(isolate->factory()->null_value())); |
| 6529 | MaybeHandle<Object> result = |
| 6530 | JSObject::DefineAccessor(it, getter, setter, desc->ToAttributes()); |
| 6531 | if (result.is_null()) return Nothing<bool>(); |
| 6532 | } |
| 6533 | } |
| 6534 | // 2e. Return true. |
| 6535 | return Just(true); |
| 6536 | } |
| 6537 | // 3. Return true, if every field in Desc is absent. |
| 6538 | // 4. Return true, if every field in Desc also occurs in current and the |
| 6539 | // value of every field in Desc is the same value as the corresponding field |
| 6540 | // in current when compared using the SameValue algorithm. |
| 6541 | if ((!desc->has_enumerable() || |
| 6542 | desc->enumerable() == current->enumerable()) && |
| 6543 | (!desc->has_configurable() || |
| 6544 | desc->configurable() == current->configurable()) && |
| 6545 | (!desc->has_value() || |
| 6546 | (current->has_value() && current->value()->SameValue(*desc->value()))) && |
| 6547 | (!desc->has_writable() || |
| 6548 | (current->has_writable() && current->writable() == desc->writable())) && |
| 6549 | (!desc->has_get() || |
| 6550 | (current->has_get() && current->get()->SameValue(*desc->get()))) && |
| 6551 | (!desc->has_set() || |
| 6552 | (current->has_set() && current->set()->SameValue(*desc->set())))) { |
| 6553 | return Just(true); |
| 6554 | } |
| 6555 | // 5. If the [[Configurable]] field of current is false, then |
| 6556 | if (!current->configurable()) { |
| 6557 | // 5a. Return false, if the [[Configurable]] field of Desc is true. |
| 6558 | if (desc->has_configurable() && desc->configurable()) { |
| 6559 | RETURN_FAILURE(isolate, should_throw, |
| 6560 | NewTypeError(MessageTemplate::kRedefineDisallowed, |
| 6561 | it != NULL ? it->GetName() : property_name)); |
| 6562 | } |
| 6563 | // 5b. Return false, if the [[Enumerable]] field of Desc is present and the |
| 6564 | // [[Enumerable]] fields of current and Desc are the Boolean negation of |
| 6565 | // each other. |
| 6566 | if (desc->has_enumerable() && desc->enumerable() != current->enumerable()) { |
| 6567 | RETURN_FAILURE(isolate, should_throw, |
| 6568 | NewTypeError(MessageTemplate::kRedefineDisallowed, |
| 6569 | it != NULL ? it->GetName() : property_name)); |
| 6570 | } |
| 6571 | } |
| 6572 | |
| 6573 | bool current_is_data_descriptor = |
| 6574 | PropertyDescriptor::IsDataDescriptor(current); |
| 6575 | // 6. If IsGenericDescriptor(Desc) is true, no further validation is required. |
| 6576 | if (desc_is_generic_descriptor) { |
| 6577 | // Nothing to see here. |
| 6578 | |
| 6579 | // 7. Else if IsDataDescriptor(current) and IsDataDescriptor(Desc) have |
| 6580 | // different results, then: |
| 6581 | } else if (current_is_data_descriptor != desc_is_data_descriptor) { |
| 6582 | // 7a. Return false, if the [[Configurable]] field of current is false. |
| 6583 | if (!current->configurable()) { |
| 6584 | RETURN_FAILURE(isolate, should_throw, |
| 6585 | NewTypeError(MessageTemplate::kRedefineDisallowed, |
| 6586 | it != NULL ? it->GetName() : property_name)); |
| 6587 | } |
| 6588 | // 7b. If IsDataDescriptor(current) is true, then: |
| 6589 | if (current_is_data_descriptor) { |
| 6590 | // 7b i. If O is not undefined, convert the property named P of object O |
| 6591 | // from a data property to an accessor property. Preserve the existing |
| 6592 | // values of the converted property's [[Configurable]] and [[Enumerable]] |
| 6593 | // attributes and set the rest of the property's attributes to their |
| 6594 | // default values. |
| 6595 | // --> Folded into step 10. |
| 6596 | } else { |
| 6597 | // 7c i. If O is not undefined, convert the property named P of object O |
| 6598 | // from an accessor property to a data property. Preserve the existing |
| 6599 | // values of the converted property’s [[Configurable]] and [[Enumerable]] |
| 6600 | // attributes and set the rest of the property’s attributes to their |
| 6601 | // default values. |
| 6602 | // --> Folded into step 10. |
| 6603 | } |
| 6604 | |
| 6605 | // 8. Else if IsDataDescriptor(current) and IsDataDescriptor(Desc) are both |
| 6606 | // true, then: |
| 6607 | } else if (current_is_data_descriptor && desc_is_data_descriptor) { |
| 6608 | // 8a. If the [[Configurable]] field of current is false, then: |
| 6609 | if (!current->configurable()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6610 | // 8a i. Return false, if the [[Writable]] field of current is false and |
| 6611 | // the [[Writable]] field of Desc is true. |
| 6612 | if (!current->writable() && desc->has_writable() && desc->writable()) { |
| 6613 | RETURN_FAILURE( |
| 6614 | isolate, should_throw, |
| 6615 | NewTypeError(MessageTemplate::kRedefineDisallowed, |
| 6616 | it != NULL ? it->GetName() : property_name)); |
| 6617 | } |
| 6618 | // 8a ii. If the [[Writable]] field of current is false, then: |
| 6619 | if (!current->writable()) { |
| 6620 | // 8a ii 1. Return false, if the [[Value]] field of Desc is present and |
| 6621 | // SameValue(Desc.[[Value]], current.[[Value]]) is false. |
| 6622 | if (desc->has_value() && !desc->value()->SameValue(*current->value())) { |
| 6623 | RETURN_FAILURE( |
| 6624 | isolate, should_throw, |
| 6625 | NewTypeError(MessageTemplate::kRedefineDisallowed, |
| 6626 | it != NULL ? it->GetName() : property_name)); |
| 6627 | } |
| 6628 | } |
| 6629 | } |
| 6630 | } else { |
| 6631 | // 9. Else IsAccessorDescriptor(current) and IsAccessorDescriptor(Desc) |
| 6632 | // are both true, |
| 6633 | DCHECK(PropertyDescriptor::IsAccessorDescriptor(current) && |
| 6634 | desc_is_accessor_descriptor); |
| 6635 | // 9a. If the [[Configurable]] field of current is false, then: |
| 6636 | if (!current->configurable()) { |
| 6637 | // 9a i. Return false, if the [[Set]] field of Desc is present and |
| 6638 | // SameValue(Desc.[[Set]], current.[[Set]]) is false. |
| 6639 | if (desc->has_set() && !desc->set()->SameValue(*current->set())) { |
| 6640 | RETURN_FAILURE( |
| 6641 | isolate, should_throw, |
| 6642 | NewTypeError(MessageTemplate::kRedefineDisallowed, |
| 6643 | it != NULL ? it->GetName() : property_name)); |
| 6644 | } |
| 6645 | // 9a ii. Return false, if the [[Get]] field of Desc is present and |
| 6646 | // SameValue(Desc.[[Get]], current.[[Get]]) is false. |
| 6647 | if (desc->has_get() && !desc->get()->SameValue(*current->get())) { |
| 6648 | RETURN_FAILURE( |
| 6649 | isolate, should_throw, |
| 6650 | NewTypeError(MessageTemplate::kRedefineDisallowed, |
| 6651 | it != NULL ? it->GetName() : property_name)); |
| 6652 | } |
| 6653 | } |
| 6654 | } |
| 6655 | |
| 6656 | // 10. If O is not undefined, then: |
| 6657 | if (it != NULL) { |
| 6658 | // 10a. For each field of Desc that is present, set the corresponding |
| 6659 | // attribute of the property named P of object O to the value of the field. |
| 6660 | PropertyAttributes attrs = NONE; |
| 6661 | |
| 6662 | if (desc->has_enumerable()) { |
| 6663 | attrs = static_cast<PropertyAttributes>( |
| 6664 | attrs | (desc->enumerable() ? NONE : DONT_ENUM)); |
| 6665 | } else { |
| 6666 | attrs = static_cast<PropertyAttributes>( |
| 6667 | attrs | (current->enumerable() ? NONE : DONT_ENUM)); |
| 6668 | } |
| 6669 | if (desc->has_configurable()) { |
| 6670 | attrs = static_cast<PropertyAttributes>( |
| 6671 | attrs | (desc->configurable() ? NONE : DONT_DELETE)); |
| 6672 | } else { |
| 6673 | attrs = static_cast<PropertyAttributes>( |
| 6674 | attrs | (current->configurable() ? NONE : DONT_DELETE)); |
| 6675 | } |
| 6676 | if (desc_is_data_descriptor || |
| 6677 | (desc_is_generic_descriptor && current_is_data_descriptor)) { |
| 6678 | if (desc->has_writable()) { |
| 6679 | attrs = static_cast<PropertyAttributes>( |
| 6680 | attrs | (desc->writable() ? NONE : READ_ONLY)); |
| 6681 | } else { |
| 6682 | attrs = static_cast<PropertyAttributes>( |
| 6683 | attrs | (current->writable() ? NONE : READ_ONLY)); |
| 6684 | } |
| 6685 | Handle<Object> value( |
| 6686 | desc->has_value() ? desc->value() |
| 6687 | : current->has_value() |
| 6688 | ? current->value() |
| 6689 | : Handle<Object>::cast( |
| 6690 | isolate->factory()->undefined_value())); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 6691 | MaybeHandle<Object> result = |
| 6692 | JSObject::DefineOwnPropertyIgnoreAttributes(it, value, attrs); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6693 | if (result.is_null()) return Nothing<bool>(); |
| 6694 | } else { |
| 6695 | DCHECK(desc_is_accessor_descriptor || |
| 6696 | (desc_is_generic_descriptor && |
| 6697 | PropertyDescriptor::IsAccessorDescriptor(current))); |
| 6698 | Handle<Object> getter( |
| 6699 | desc->has_get() |
| 6700 | ? desc->get() |
| 6701 | : current->has_get() |
| 6702 | ? current->get() |
| 6703 | : Handle<Object>::cast(isolate->factory()->null_value())); |
| 6704 | Handle<Object> setter( |
| 6705 | desc->has_set() |
| 6706 | ? desc->set() |
| 6707 | : current->has_set() |
| 6708 | ? current->set() |
| 6709 | : Handle<Object>::cast(isolate->factory()->null_value())); |
| 6710 | MaybeHandle<Object> result = |
| 6711 | JSObject::DefineAccessor(it, getter, setter, attrs); |
| 6712 | if (result.is_null()) return Nothing<bool>(); |
| 6713 | } |
| 6714 | } |
| 6715 | |
| 6716 | // 11. Return true. |
| 6717 | return Just(true); |
| 6718 | } |
| 6719 | |
| 6720 | |
| 6721 | // static |
| 6722 | Maybe<bool> JSReceiver::CreateDataProperty(LookupIterator* it, |
| 6723 | Handle<Object> value, |
| 6724 | ShouldThrow should_throw) { |
| 6725 | DCHECK(!it->check_prototype_chain()); |
| 6726 | Handle<JSReceiver> receiver = Handle<JSReceiver>::cast(it->GetReceiver()); |
| 6727 | Isolate* isolate = receiver->GetIsolate(); |
| 6728 | |
| 6729 | if (receiver->IsJSObject()) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6730 | return JSObject::CreateDataProperty(it, value, should_throw); // Shortcut. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6731 | } |
| 6732 | |
| 6733 | PropertyDescriptor new_desc; |
| 6734 | new_desc.set_value(value); |
| 6735 | new_desc.set_writable(true); |
| 6736 | new_desc.set_enumerable(true); |
| 6737 | new_desc.set_configurable(true); |
| 6738 | |
| 6739 | return JSReceiver::DefineOwnProperty(isolate, receiver, it->GetName(), |
| 6740 | &new_desc, should_throw); |
| 6741 | } |
| 6742 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6743 | Maybe<bool> JSObject::CreateDataProperty(LookupIterator* it, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6744 | Handle<Object> value, |
| 6745 | ShouldThrow should_throw) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6746 | DCHECK(it->GetReceiver()->IsJSObject()); |
| 6747 | MAYBE_RETURN(JSReceiver::GetPropertyAttributes(it), Nothing<bool>()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6748 | Handle<JSReceiver> receiver = Handle<JSReceiver>::cast(it->GetReceiver()); |
| 6749 | Isolate* isolate = receiver->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6750 | |
| 6751 | if (it->IsFound()) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6752 | Maybe<PropertyAttributes> attributes = GetPropertyAttributes(it); |
| 6753 | MAYBE_RETURN(attributes, Nothing<bool>()); |
| 6754 | if ((attributes.FromJust() & DONT_DELETE) != 0) { |
| 6755 | RETURN_FAILURE( |
| 6756 | isolate, should_throw, |
| 6757 | NewTypeError(MessageTemplate::kRedefineDisallowed, it->GetName())); |
| 6758 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6759 | } else { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6760 | if (!JSObject::IsExtensible(Handle<JSObject>::cast(it->GetReceiver()))) { |
| 6761 | RETURN_FAILURE( |
| 6762 | isolate, should_throw, |
| 6763 | NewTypeError(MessageTemplate::kDefineDisallowed, it->GetName())); |
| 6764 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6765 | } |
| 6766 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 6767 | RETURN_ON_EXCEPTION_VALUE(it->isolate(), |
| 6768 | DefineOwnPropertyIgnoreAttributes(it, value, NONE), |
| 6769 | Nothing<bool>()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6770 | |
| 6771 | return Just(true); |
| 6772 | } |
| 6773 | |
| 6774 | |
| 6775 | // TODO(jkummerow): Consider unification with FastAsArrayLength() in |
| 6776 | // accessors.cc. |
| 6777 | bool PropertyKeyToArrayLength(Handle<Object> value, uint32_t* length) { |
| 6778 | DCHECK(value->IsNumber() || value->IsName()); |
| 6779 | if (value->ToArrayLength(length)) return true; |
| 6780 | if (value->IsString()) return String::cast(*value)->AsArrayIndex(length); |
| 6781 | return false; |
| 6782 | } |
| 6783 | |
| 6784 | |
| 6785 | bool PropertyKeyToArrayIndex(Handle<Object> index_obj, uint32_t* output) { |
| 6786 | return PropertyKeyToArrayLength(index_obj, output) && *output != kMaxUInt32; |
| 6787 | } |
| 6788 | |
| 6789 | |
| 6790 | // ES6 9.4.2.1 |
| 6791 | // static |
| 6792 | Maybe<bool> JSArray::DefineOwnProperty(Isolate* isolate, Handle<JSArray> o, |
| 6793 | Handle<Object> name, |
| 6794 | PropertyDescriptor* desc, |
| 6795 | ShouldThrow should_throw) { |
| 6796 | // 1. Assert: IsPropertyKey(P) is true. ("P" is |name|.) |
| 6797 | // 2. If P is "length", then: |
| 6798 | // TODO(jkummerow): Check if we need slow string comparison. |
| 6799 | if (*name == isolate->heap()->length_string()) { |
| 6800 | // 2a. Return ArraySetLength(A, Desc). |
| 6801 | return ArraySetLength(isolate, o, desc, should_throw); |
| 6802 | } |
| 6803 | // 3. Else if P is an array index, then: |
| 6804 | uint32_t index = 0; |
| 6805 | if (PropertyKeyToArrayIndex(name, &index)) { |
| 6806 | // 3a. Let oldLenDesc be OrdinaryGetOwnProperty(A, "length"). |
| 6807 | PropertyDescriptor old_len_desc; |
| 6808 | Maybe<bool> success = GetOwnPropertyDescriptor( |
| 6809 | isolate, o, isolate->factory()->length_string(), &old_len_desc); |
| 6810 | // 3b. (Assert) |
| 6811 | DCHECK(success.FromJust()); |
| 6812 | USE(success); |
| 6813 | // 3c. Let oldLen be oldLenDesc.[[Value]]. |
| 6814 | uint32_t old_len = 0; |
| 6815 | CHECK(old_len_desc.value()->ToArrayLength(&old_len)); |
| 6816 | // 3d. Let index be ToUint32(P). |
| 6817 | // (Already done above.) |
| 6818 | // 3e. (Assert) |
| 6819 | // 3f. If index >= oldLen and oldLenDesc.[[Writable]] is false, |
| 6820 | // return false. |
| 6821 | if (index >= old_len && old_len_desc.has_writable() && |
| 6822 | !old_len_desc.writable()) { |
| 6823 | RETURN_FAILURE(isolate, should_throw, |
| 6824 | NewTypeError(MessageTemplate::kDefineDisallowed, name)); |
| 6825 | } |
| 6826 | // 3g. Let succeeded be OrdinaryDefineOwnProperty(A, P, Desc). |
| 6827 | Maybe<bool> succeeded = |
| 6828 | OrdinaryDefineOwnProperty(isolate, o, name, desc, should_throw); |
| 6829 | // 3h. Assert: succeeded is not an abrupt completion. |
| 6830 | // In our case, if should_throw == THROW_ON_ERROR, it can be! |
| 6831 | // 3i. If succeeded is false, return false. |
| 6832 | if (succeeded.IsNothing() || !succeeded.FromJust()) return succeeded; |
| 6833 | // 3j. If index >= oldLen, then: |
| 6834 | if (index >= old_len) { |
| 6835 | // 3j i. Set oldLenDesc.[[Value]] to index + 1. |
| 6836 | old_len_desc.set_value(isolate->factory()->NewNumberFromUint(index + 1)); |
| 6837 | // 3j ii. Let succeeded be |
| 6838 | // OrdinaryDefineOwnProperty(A, "length", oldLenDesc). |
| 6839 | succeeded = OrdinaryDefineOwnProperty(isolate, o, |
| 6840 | isolate->factory()->length_string(), |
| 6841 | &old_len_desc, should_throw); |
| 6842 | // 3j iii. Assert: succeeded is true. |
| 6843 | DCHECK(succeeded.FromJust()); |
| 6844 | USE(succeeded); |
| 6845 | } |
| 6846 | // 3k. Return true. |
| 6847 | return Just(true); |
| 6848 | } |
| 6849 | |
| 6850 | // 4. Return OrdinaryDefineOwnProperty(A, P, Desc). |
| 6851 | return OrdinaryDefineOwnProperty(isolate, o, name, desc, should_throw); |
| 6852 | } |
| 6853 | |
| 6854 | |
| 6855 | // Part of ES6 9.4.2.4 ArraySetLength. |
| 6856 | // static |
| 6857 | bool JSArray::AnythingToArrayLength(Isolate* isolate, |
| 6858 | Handle<Object> length_object, |
| 6859 | uint32_t* output) { |
| 6860 | // Fast path: check numbers and strings that can be converted directly |
| 6861 | // and unobservably. |
| 6862 | if (length_object->ToArrayLength(output)) return true; |
| 6863 | if (length_object->IsString() && |
| 6864 | Handle<String>::cast(length_object)->AsArrayIndex(output)) { |
| 6865 | return true; |
| 6866 | } |
| 6867 | // Slow path: follow steps in ES6 9.4.2.4 "ArraySetLength". |
| 6868 | // 3. Let newLen be ToUint32(Desc.[[Value]]). |
| 6869 | Handle<Object> uint32_v; |
| 6870 | if (!Object::ToUint32(isolate, length_object).ToHandle(&uint32_v)) { |
| 6871 | // 4. ReturnIfAbrupt(newLen). |
| 6872 | return false; |
| 6873 | } |
| 6874 | // 5. Let numberLen be ToNumber(Desc.[[Value]]). |
| 6875 | Handle<Object> number_v; |
| 6876 | if (!Object::ToNumber(length_object).ToHandle(&number_v)) { |
| 6877 | // 6. ReturnIfAbrupt(newLen). |
| 6878 | return false; |
| 6879 | } |
| 6880 | // 7. If newLen != numberLen, throw a RangeError exception. |
| 6881 | if (uint32_v->Number() != number_v->Number()) { |
| 6882 | Handle<Object> exception = |
| 6883 | isolate->factory()->NewRangeError(MessageTemplate::kInvalidArrayLength); |
| 6884 | isolate->Throw(*exception); |
| 6885 | return false; |
| 6886 | } |
| 6887 | CHECK(uint32_v->ToArrayLength(output)); |
| 6888 | return true; |
| 6889 | } |
| 6890 | |
| 6891 | |
| 6892 | // ES6 9.4.2.4 |
| 6893 | // static |
| 6894 | Maybe<bool> JSArray::ArraySetLength(Isolate* isolate, Handle<JSArray> a, |
| 6895 | PropertyDescriptor* desc, |
| 6896 | ShouldThrow should_throw) { |
| 6897 | // 1. If the [[Value]] field of Desc is absent, then |
| 6898 | if (!desc->has_value()) { |
| 6899 | // 1a. Return OrdinaryDefineOwnProperty(A, "length", Desc). |
| 6900 | return OrdinaryDefineOwnProperty( |
| 6901 | isolate, a, isolate->factory()->length_string(), desc, should_throw); |
| 6902 | } |
| 6903 | // 2. Let newLenDesc be a copy of Desc. |
| 6904 | // (Actual copying is not necessary.) |
| 6905 | PropertyDescriptor* new_len_desc = desc; |
| 6906 | // 3. - 7. Convert Desc.[[Value]] to newLen. |
| 6907 | uint32_t new_len = 0; |
| 6908 | if (!AnythingToArrayLength(isolate, desc->value(), &new_len)) { |
| 6909 | DCHECK(isolate->has_pending_exception()); |
| 6910 | return Nothing<bool>(); |
| 6911 | } |
| 6912 | // 8. Set newLenDesc.[[Value]] to newLen. |
| 6913 | // (Done below, if needed.) |
| 6914 | // 9. Let oldLenDesc be OrdinaryGetOwnProperty(A, "length"). |
| 6915 | PropertyDescriptor old_len_desc; |
| 6916 | Maybe<bool> success = GetOwnPropertyDescriptor( |
| 6917 | isolate, a, isolate->factory()->length_string(), &old_len_desc); |
| 6918 | // 10. (Assert) |
| 6919 | DCHECK(success.FromJust()); |
| 6920 | USE(success); |
| 6921 | // 11. Let oldLen be oldLenDesc.[[Value]]. |
| 6922 | uint32_t old_len = 0; |
| 6923 | CHECK(old_len_desc.value()->ToArrayLength(&old_len)); |
| 6924 | // 12. If newLen >= oldLen, then |
| 6925 | if (new_len >= old_len) { |
| 6926 | // 8. Set newLenDesc.[[Value]] to newLen. |
| 6927 | // 12a. Return OrdinaryDefineOwnProperty(A, "length", newLenDesc). |
| 6928 | new_len_desc->set_value(isolate->factory()->NewNumberFromUint(new_len)); |
| 6929 | return OrdinaryDefineOwnProperty(isolate, a, |
| 6930 | isolate->factory()->length_string(), |
| 6931 | new_len_desc, should_throw); |
| 6932 | } |
| 6933 | // 13. If oldLenDesc.[[Writable]] is false, return false. |
| 6934 | if (!old_len_desc.writable()) { |
| 6935 | RETURN_FAILURE(isolate, should_throw, |
| 6936 | NewTypeError(MessageTemplate::kRedefineDisallowed, |
| 6937 | isolate->factory()->length_string())); |
| 6938 | } |
| 6939 | // 14. If newLenDesc.[[Writable]] is absent or has the value true, |
| 6940 | // let newWritable be true. |
| 6941 | bool new_writable = false; |
| 6942 | if (!new_len_desc->has_writable() || new_len_desc->writable()) { |
| 6943 | new_writable = true; |
| 6944 | } else { |
| 6945 | // 15. Else, |
| 6946 | // 15a. Need to defer setting the [[Writable]] attribute to false in case |
| 6947 | // any elements cannot be deleted. |
| 6948 | // 15b. Let newWritable be false. (It's initialized as "false" anyway.) |
| 6949 | // 15c. Set newLenDesc.[[Writable]] to true. |
| 6950 | // (Not needed.) |
| 6951 | } |
| 6952 | // Most of steps 16 through 19 is implemented by JSArray::SetLength. |
| 6953 | if (JSArray::ObservableSetLength(a, new_len).is_null()) { |
| 6954 | DCHECK(isolate->has_pending_exception()); |
| 6955 | return Nothing<bool>(); |
| 6956 | } |
| 6957 | // Steps 19d-ii, 20. |
| 6958 | if (!new_writable) { |
| 6959 | PropertyDescriptor readonly; |
| 6960 | readonly.set_writable(false); |
| 6961 | Maybe<bool> success = OrdinaryDefineOwnProperty( |
| 6962 | isolate, a, isolate->factory()->length_string(), &readonly, |
| 6963 | should_throw); |
| 6964 | DCHECK(success.FromJust()); |
| 6965 | USE(success); |
| 6966 | } |
| 6967 | uint32_t actual_new_len = 0; |
| 6968 | CHECK(a->length()->ToArrayLength(&actual_new_len)); |
| 6969 | // Steps 19d-v, 21. Return false if there were non-deletable elements. |
| 6970 | bool result = actual_new_len == new_len; |
| 6971 | if (!result) { |
| 6972 | RETURN_FAILURE( |
| 6973 | isolate, should_throw, |
| 6974 | NewTypeError(MessageTemplate::kStrictDeleteProperty, |
| 6975 | isolate->factory()->NewNumberFromUint(actual_new_len - 1), |
| 6976 | a)); |
| 6977 | } |
| 6978 | return Just(result); |
| 6979 | } |
| 6980 | |
| 6981 | |
| 6982 | // ES6 9.5.6 |
| 6983 | // static |
| 6984 | Maybe<bool> JSProxy::DefineOwnProperty(Isolate* isolate, Handle<JSProxy> proxy, |
| 6985 | Handle<Object> key, |
| 6986 | PropertyDescriptor* desc, |
| 6987 | ShouldThrow should_throw) { |
| 6988 | STACK_CHECK(Nothing<bool>()); |
| 6989 | if (key->IsSymbol() && Handle<Symbol>::cast(key)->IsPrivate()) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 6990 | return SetPrivateProperty(isolate, proxy, Handle<Symbol>::cast(key), desc, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6991 | should_throw); |
| 6992 | } |
| 6993 | Handle<String> trap_name = isolate->factory()->defineProperty_string(); |
| 6994 | // 1. Assert: IsPropertyKey(P) is true. |
| 6995 | DCHECK(key->IsName() || key->IsNumber()); |
| 6996 | // 2. Let handler be the value of the [[ProxyHandler]] internal slot of O. |
| 6997 | Handle<Object> handler(proxy->handler(), isolate); |
| 6998 | // 3. If handler is null, throw a TypeError exception. |
| 6999 | // 4. Assert: Type(handler) is Object. |
| 7000 | if (proxy->IsRevoked()) { |
| 7001 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7002 | MessageTemplate::kProxyRevoked, trap_name)); |
| 7003 | return Nothing<bool>(); |
| 7004 | } |
| 7005 | // 5. Let target be the value of the [[ProxyTarget]] internal slot of O. |
| 7006 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 7007 | // 6. Let trap be ? GetMethod(handler, "defineProperty"). |
| 7008 | Handle<Object> trap; |
| 7009 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7010 | isolate, trap, |
| 7011 | Object::GetMethod(Handle<JSReceiver>::cast(handler), trap_name), |
| 7012 | Nothing<bool>()); |
| 7013 | // 7. If trap is undefined, then: |
| 7014 | if (trap->IsUndefined()) { |
| 7015 | // 7a. Return target.[[DefineOwnProperty]](P, Desc). |
| 7016 | return JSReceiver::DefineOwnProperty(isolate, target, key, desc, |
| 7017 | should_throw); |
| 7018 | } |
| 7019 | // 8. Let descObj be FromPropertyDescriptor(Desc). |
| 7020 | Handle<Object> desc_obj = desc->ToObject(isolate); |
| 7021 | // 9. Let booleanTrapResult be |
| 7022 | // ToBoolean(? Call(trap, handler, «target, P, descObj»)). |
| 7023 | Handle<Name> property_name = |
| 7024 | key->IsName() |
| 7025 | ? Handle<Name>::cast(key) |
| 7026 | : Handle<Name>::cast(isolate->factory()->NumberToString(key)); |
| 7027 | // Do not leak private property names. |
| 7028 | DCHECK(!property_name->IsPrivate()); |
| 7029 | Handle<Object> trap_result_obj; |
| 7030 | Handle<Object> args[] = {target, property_name, desc_obj}; |
| 7031 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7032 | isolate, trap_result_obj, |
| 7033 | Execution::Call(isolate, trap, handler, arraysize(args), args), |
| 7034 | Nothing<bool>()); |
| 7035 | // 10. If booleanTrapResult is false, return false. |
| 7036 | if (!trap_result_obj->BooleanValue()) { |
| 7037 | RETURN_FAILURE(isolate, should_throw, |
| 7038 | NewTypeError(MessageTemplate::kProxyTrapReturnedFalsishFor, |
| 7039 | trap_name, property_name)); |
| 7040 | } |
| 7041 | // 11. Let targetDesc be ? target.[[GetOwnProperty]](P). |
| 7042 | PropertyDescriptor target_desc; |
| 7043 | Maybe<bool> target_found = |
| 7044 | JSReceiver::GetOwnPropertyDescriptor(isolate, target, key, &target_desc); |
| 7045 | MAYBE_RETURN(target_found, Nothing<bool>()); |
| 7046 | // 12. Let extensibleTarget be ? IsExtensible(target). |
| 7047 | Maybe<bool> maybe_extensible = JSReceiver::IsExtensible(target); |
| 7048 | MAYBE_RETURN(maybe_extensible, Nothing<bool>()); |
| 7049 | bool extensible_target = maybe_extensible.FromJust(); |
| 7050 | // 13. If Desc has a [[Configurable]] field and if Desc.[[Configurable]] |
| 7051 | // is false, then: |
| 7052 | // 13a. Let settingConfigFalse be true. |
| 7053 | // 14. Else let settingConfigFalse be false. |
| 7054 | bool setting_config_false = desc->has_configurable() && !desc->configurable(); |
| 7055 | // 15. If targetDesc is undefined, then |
| 7056 | if (!target_found.FromJust()) { |
| 7057 | // 15a. If extensibleTarget is false, throw a TypeError exception. |
| 7058 | if (!extensible_target) { |
| 7059 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7060 | MessageTemplate::kProxyDefinePropertyNonExtensible, property_name)); |
| 7061 | return Nothing<bool>(); |
| 7062 | } |
| 7063 | // 15b. If settingConfigFalse is true, throw a TypeError exception. |
| 7064 | if (setting_config_false) { |
| 7065 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7066 | MessageTemplate::kProxyDefinePropertyNonConfigurable, property_name)); |
| 7067 | return Nothing<bool>(); |
| 7068 | } |
| 7069 | } else { |
| 7070 | // 16. Else targetDesc is not undefined, |
| 7071 | // 16a. If IsCompatiblePropertyDescriptor(extensibleTarget, Desc, |
| 7072 | // targetDesc) is false, throw a TypeError exception. |
| 7073 | Maybe<bool> valid = |
| 7074 | IsCompatiblePropertyDescriptor(isolate, extensible_target, desc, |
| 7075 | &target_desc, property_name, DONT_THROW); |
| 7076 | MAYBE_RETURN(valid, Nothing<bool>()); |
| 7077 | if (!valid.FromJust()) { |
| 7078 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7079 | MessageTemplate::kProxyDefinePropertyIncompatible, property_name)); |
| 7080 | return Nothing<bool>(); |
| 7081 | } |
| 7082 | // 16b. If settingConfigFalse is true and targetDesc.[[Configurable]] is |
| 7083 | // true, throw a TypeError exception. |
| 7084 | if (setting_config_false && target_desc.configurable()) { |
| 7085 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7086 | MessageTemplate::kProxyDefinePropertyNonConfigurable, property_name)); |
| 7087 | return Nothing<bool>(); |
| 7088 | } |
| 7089 | } |
| 7090 | // 17. Return true. |
| 7091 | return Just(true); |
| 7092 | } |
| 7093 | |
| 7094 | |
| 7095 | // static |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 7096 | Maybe<bool> JSProxy::SetPrivateProperty(Isolate* isolate, Handle<JSProxy> proxy, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7097 | Handle<Symbol> private_name, |
| 7098 | PropertyDescriptor* desc, |
| 7099 | ShouldThrow should_throw) { |
| 7100 | // Despite the generic name, this can only add private data properties. |
| 7101 | if (!PropertyDescriptor::IsDataDescriptor(desc) || |
| 7102 | desc->ToAttributes() != DONT_ENUM) { |
| 7103 | RETURN_FAILURE(isolate, should_throw, |
| 7104 | NewTypeError(MessageTemplate::kProxyPrivate)); |
| 7105 | } |
| 7106 | DCHECK(proxy->map()->is_dictionary_map()); |
| 7107 | Handle<Object> value = |
| 7108 | desc->has_value() |
| 7109 | ? desc->value() |
| 7110 | : Handle<Object>::cast(isolate->factory()->undefined_value()); |
| 7111 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 7112 | LookupIterator it(proxy, private_name, proxy); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7113 | |
| 7114 | if (it.IsFound()) { |
| 7115 | DCHECK_EQ(LookupIterator::DATA, it.state()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 7116 | DCHECK_EQ(DONT_ENUM, it.property_attributes()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7117 | it.WriteDataValue(value); |
| 7118 | return Just(true); |
| 7119 | } |
| 7120 | |
| 7121 | Handle<NameDictionary> dict(proxy->property_dictionary()); |
| 7122 | PropertyDetails details(DONT_ENUM, DATA, 0, PropertyCellType::kNoCell); |
| 7123 | Handle<NameDictionary> result = |
| 7124 | NameDictionary::Add(dict, private_name, value, details); |
| 7125 | if (!dict.is_identical_to(result)) proxy->set_properties(*result); |
| 7126 | return Just(true); |
| 7127 | } |
| 7128 | |
| 7129 | |
| 7130 | // static |
| 7131 | Maybe<bool> JSReceiver::GetOwnPropertyDescriptor(Isolate* isolate, |
| 7132 | Handle<JSReceiver> object, |
| 7133 | Handle<Object> key, |
| 7134 | PropertyDescriptor* desc) { |
| 7135 | bool success = false; |
| 7136 | DCHECK(key->IsName() || key->IsNumber()); // |key| is a PropertyKey... |
| 7137 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 7138 | isolate, object, key, &success, LookupIterator::HIDDEN); |
| 7139 | DCHECK(success); // ...so creating a LookupIterator can't fail. |
| 7140 | return GetOwnPropertyDescriptor(&it, desc); |
| 7141 | } |
| 7142 | |
| 7143 | |
| 7144 | // ES6 9.1.5.1 |
| 7145 | // Returns true on success, false if the property didn't exist, nothing if |
| 7146 | // an exception was thrown. |
| 7147 | // static |
| 7148 | Maybe<bool> JSReceiver::GetOwnPropertyDescriptor(LookupIterator* it, |
| 7149 | PropertyDescriptor* desc) { |
| 7150 | Isolate* isolate = it->isolate(); |
| 7151 | // "Virtual" dispatch. |
| 7152 | if (it->IsFound() && it->GetHolder<JSReceiver>()->IsJSProxy()) { |
| 7153 | return JSProxy::GetOwnPropertyDescriptor(isolate, it->GetHolder<JSProxy>(), |
| 7154 | it->GetName(), desc); |
| 7155 | } |
| 7156 | |
| 7157 | // 1. (Assert) |
| 7158 | // 2. If O does not have an own property with key P, return undefined. |
| 7159 | Maybe<PropertyAttributes> maybe = JSObject::GetPropertyAttributes(it); |
| 7160 | MAYBE_RETURN(maybe, Nothing<bool>()); |
| 7161 | PropertyAttributes attrs = maybe.FromJust(); |
| 7162 | if (attrs == ABSENT) return Just(false); |
| 7163 | DCHECK(!isolate->has_pending_exception()); |
| 7164 | |
| 7165 | // 3. Let D be a newly created Property Descriptor with no fields. |
| 7166 | DCHECK(desc->is_empty()); |
| 7167 | // 4. Let X be O's own property whose key is P. |
| 7168 | // 5. If X is a data property, then |
| 7169 | bool is_accessor_pair = it->state() == LookupIterator::ACCESSOR && |
| 7170 | it->GetAccessors()->IsAccessorPair(); |
| 7171 | if (!is_accessor_pair) { |
| 7172 | // 5a. Set D.[[Value]] to the value of X's [[Value]] attribute. |
| 7173 | Handle<Object> value; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 7174 | if (!Object::GetProperty(it).ToHandle(&value)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7175 | DCHECK(isolate->has_pending_exception()); |
| 7176 | return Nothing<bool>(); |
| 7177 | } |
| 7178 | desc->set_value(value); |
| 7179 | // 5b. Set D.[[Writable]] to the value of X's [[Writable]] attribute |
| 7180 | desc->set_writable((attrs & READ_ONLY) == 0); |
| 7181 | } else { |
| 7182 | // 6. Else X is an accessor property, so |
| 7183 | Handle<AccessorPair> accessors = |
| 7184 | Handle<AccessorPair>::cast(it->GetAccessors()); |
| 7185 | // 6a. Set D.[[Get]] to the value of X's [[Get]] attribute. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 7186 | desc->set_get(AccessorPair::GetComponent(accessors, ACCESSOR_GETTER)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7187 | // 6b. Set D.[[Set]] to the value of X's [[Set]] attribute. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 7188 | desc->set_set(AccessorPair::GetComponent(accessors, ACCESSOR_SETTER)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7189 | } |
| 7190 | |
| 7191 | // 7. Set D.[[Enumerable]] to the value of X's [[Enumerable]] attribute. |
| 7192 | desc->set_enumerable((attrs & DONT_ENUM) == 0); |
| 7193 | // 8. Set D.[[Configurable]] to the value of X's [[Configurable]] attribute. |
| 7194 | desc->set_configurable((attrs & DONT_DELETE) == 0); |
| 7195 | // 9. Return D. |
| 7196 | DCHECK(PropertyDescriptor::IsAccessorDescriptor(desc) != |
| 7197 | PropertyDescriptor::IsDataDescriptor(desc)); |
| 7198 | return Just(true); |
| 7199 | } |
| 7200 | |
| 7201 | |
| 7202 | // ES6 9.5.5 |
| 7203 | // static |
| 7204 | Maybe<bool> JSProxy::GetOwnPropertyDescriptor(Isolate* isolate, |
| 7205 | Handle<JSProxy> proxy, |
| 7206 | Handle<Name> name, |
| 7207 | PropertyDescriptor* desc) { |
| 7208 | DCHECK(!name->IsPrivate()); |
| 7209 | STACK_CHECK(Nothing<bool>()); |
| 7210 | |
| 7211 | Handle<String> trap_name = |
| 7212 | isolate->factory()->getOwnPropertyDescriptor_string(); |
| 7213 | // 1. (Assert) |
| 7214 | // 2. Let handler be the value of the [[ProxyHandler]] internal slot of O. |
| 7215 | Handle<Object> handler(proxy->handler(), isolate); |
| 7216 | // 3. If handler is null, throw a TypeError exception. |
| 7217 | // 4. Assert: Type(handler) is Object. |
| 7218 | if (proxy->IsRevoked()) { |
| 7219 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7220 | MessageTemplate::kProxyRevoked, trap_name)); |
| 7221 | return Nothing<bool>(); |
| 7222 | } |
| 7223 | // 5. Let target be the value of the [[ProxyTarget]] internal slot of O. |
| 7224 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 7225 | // 6. Let trap be ? GetMethod(handler, "getOwnPropertyDescriptor"). |
| 7226 | Handle<Object> trap; |
| 7227 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7228 | isolate, trap, |
| 7229 | Object::GetMethod(Handle<JSReceiver>::cast(handler), trap_name), |
| 7230 | Nothing<bool>()); |
| 7231 | // 7. If trap is undefined, then |
| 7232 | if (trap->IsUndefined()) { |
| 7233 | // 7a. Return target.[[GetOwnProperty]](P). |
| 7234 | return JSReceiver::GetOwnPropertyDescriptor(isolate, target, name, desc); |
| 7235 | } |
| 7236 | // 8. Let trapResultObj be ? Call(trap, handler, «target, P»). |
| 7237 | Handle<Object> trap_result_obj; |
| 7238 | Handle<Object> args[] = {target, name}; |
| 7239 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7240 | isolate, trap_result_obj, |
| 7241 | Execution::Call(isolate, trap, handler, arraysize(args), args), |
| 7242 | Nothing<bool>()); |
| 7243 | // 9. If Type(trapResultObj) is neither Object nor Undefined, throw a |
| 7244 | // TypeError exception. |
| 7245 | if (!trap_result_obj->IsJSReceiver() && !trap_result_obj->IsUndefined()) { |
| 7246 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7247 | MessageTemplate::kProxyGetOwnPropertyDescriptorInvalid, name)); |
| 7248 | return Nothing<bool>(); |
| 7249 | } |
| 7250 | // 10. Let targetDesc be ? target.[[GetOwnProperty]](P). |
| 7251 | PropertyDescriptor target_desc; |
| 7252 | Maybe<bool> found = |
| 7253 | JSReceiver::GetOwnPropertyDescriptor(isolate, target, name, &target_desc); |
| 7254 | MAYBE_RETURN(found, Nothing<bool>()); |
| 7255 | // 11. If trapResultObj is undefined, then |
| 7256 | if (trap_result_obj->IsUndefined()) { |
| 7257 | // 11a. If targetDesc is undefined, return undefined. |
| 7258 | if (!found.FromJust()) return Just(false); |
| 7259 | // 11b. If targetDesc.[[Configurable]] is false, throw a TypeError |
| 7260 | // exception. |
| 7261 | if (!target_desc.configurable()) { |
| 7262 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7263 | MessageTemplate::kProxyGetOwnPropertyDescriptorUndefined, name)); |
| 7264 | return Nothing<bool>(); |
| 7265 | } |
| 7266 | // 11c. Let extensibleTarget be ? IsExtensible(target). |
| 7267 | Maybe<bool> extensible_target = JSReceiver::IsExtensible(target); |
| 7268 | MAYBE_RETURN(extensible_target, Nothing<bool>()); |
| 7269 | // 11d. (Assert) |
| 7270 | // 11e. If extensibleTarget is false, throw a TypeError exception. |
| 7271 | if (!extensible_target.FromJust()) { |
| 7272 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7273 | MessageTemplate::kProxyGetOwnPropertyDescriptorNonExtensible, name)); |
| 7274 | return Nothing<bool>(); |
| 7275 | } |
| 7276 | // 11f. Return undefined. |
| 7277 | return Just(false); |
| 7278 | } |
| 7279 | // 12. Let extensibleTarget be ? IsExtensible(target). |
| 7280 | Maybe<bool> extensible_target = JSReceiver::IsExtensible(target); |
| 7281 | MAYBE_RETURN(extensible_target, Nothing<bool>()); |
| 7282 | // 13. Let resultDesc be ? ToPropertyDescriptor(trapResultObj). |
| 7283 | if (!PropertyDescriptor::ToPropertyDescriptor(isolate, trap_result_obj, |
| 7284 | desc)) { |
| 7285 | DCHECK(isolate->has_pending_exception()); |
| 7286 | return Nothing<bool>(); |
| 7287 | } |
| 7288 | // 14. Call CompletePropertyDescriptor(resultDesc). |
| 7289 | PropertyDescriptor::CompletePropertyDescriptor(isolate, desc); |
| 7290 | // 15. Let valid be IsCompatiblePropertyDescriptor (extensibleTarget, |
| 7291 | // resultDesc, targetDesc). |
| 7292 | Maybe<bool> valid = |
| 7293 | IsCompatiblePropertyDescriptor(isolate, extensible_target.FromJust(), |
| 7294 | desc, &target_desc, name, DONT_THROW); |
| 7295 | MAYBE_RETURN(valid, Nothing<bool>()); |
| 7296 | // 16. If valid is false, throw a TypeError exception. |
| 7297 | if (!valid.FromJust()) { |
| 7298 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7299 | MessageTemplate::kProxyGetOwnPropertyDescriptorIncompatible, name)); |
| 7300 | return Nothing<bool>(); |
| 7301 | } |
| 7302 | // 17. If resultDesc.[[Configurable]] is false, then |
| 7303 | if (!desc->configurable()) { |
| 7304 | // 17a. If targetDesc is undefined or targetDesc.[[Configurable]] is true: |
| 7305 | if (target_desc.is_empty() || target_desc.configurable()) { |
| 7306 | // 17a i. Throw a TypeError exception. |
| 7307 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7308 | MessageTemplate::kProxyGetOwnPropertyDescriptorNonConfigurable, |
| 7309 | name)); |
| 7310 | return Nothing<bool>(); |
| 7311 | } |
| 7312 | } |
| 7313 | // 18. Return resultDesc. |
| 7314 | return Just(true); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7315 | } |
| 7316 | |
| 7317 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7318 | bool JSObject::ReferencesObjectFromElements(FixedArray* elements, |
| 7319 | ElementsKind kind, |
| 7320 | Object* object) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 7321 | if (IsFastObjectElementsKind(kind) || kind == FAST_STRING_WRAPPER_ELEMENTS) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7322 | int length = IsJSArray() |
| 7323 | ? Smi::cast(JSArray::cast(this)->length())->value() |
| 7324 | : elements->length(); |
| 7325 | for (int i = 0; i < length; ++i) { |
| 7326 | Object* element = elements->get(i); |
| 7327 | if (!element->IsTheHole() && element == object) return true; |
| 7328 | } |
| 7329 | } else { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 7330 | DCHECK(kind == DICTIONARY_ELEMENTS || kind == SLOW_STRING_WRAPPER_ELEMENTS); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 7331 | Object* key = |
| 7332 | SeededNumberDictionary::cast(elements)->SlowReverseLookup(object); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7333 | if (!key->IsUndefined()) return true; |
| 7334 | } |
| 7335 | return false; |
| 7336 | } |
| 7337 | |
| 7338 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7339 | // Check whether this object references another object. |
| 7340 | bool JSObject::ReferencesObject(Object* obj) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7341 | Map* map_of_this = map(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7342 | Heap* heap = GetHeap(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7343 | DisallowHeapAllocation no_allocation; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7344 | |
| 7345 | // Is the object the constructor for this object? |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7346 | if (map_of_this->GetConstructor() == obj) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7347 | return true; |
| 7348 | } |
| 7349 | |
| 7350 | // Is the object the prototype for this object? |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7351 | if (map_of_this->prototype() == obj) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7352 | return true; |
| 7353 | } |
| 7354 | |
| 7355 | // Check if the object is among the named properties. |
| 7356 | Object* key = SlowReverseLookup(obj); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7357 | if (!key->IsUndefined()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7358 | return true; |
| 7359 | } |
| 7360 | |
| 7361 | // Check if the object is among the indexed properties. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7362 | ElementsKind kind = GetElementsKind(); |
| 7363 | switch (kind) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7364 | // Raw pixels and external arrays do not reference other |
| 7365 | // objects. |
| 7366 | #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7367 | case TYPE##_ELEMENTS: \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7368 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7369 | |
| 7370 | TYPED_ARRAYS(TYPED_ARRAY_CASE) |
| 7371 | #undef TYPED_ARRAY_CASE |
| 7372 | |
| 7373 | case FAST_DOUBLE_ELEMENTS: |
| 7374 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
| 7375 | break; |
| 7376 | case FAST_SMI_ELEMENTS: |
| 7377 | case FAST_HOLEY_SMI_ELEMENTS: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7378 | break; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7379 | case FAST_ELEMENTS: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7380 | case FAST_HOLEY_ELEMENTS: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 7381 | case DICTIONARY_ELEMENTS: |
| 7382 | case FAST_STRING_WRAPPER_ELEMENTS: |
| 7383 | case SLOW_STRING_WRAPPER_ELEMENTS: { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7384 | FixedArray* elements = FixedArray::cast(this->elements()); |
| 7385 | if (ReferencesObjectFromElements(elements, kind, obj)) return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7386 | break; |
| 7387 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7388 | case FAST_SLOPPY_ARGUMENTS_ELEMENTS: |
| 7389 | case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7390 | FixedArray* parameter_map = FixedArray::cast(elements()); |
| 7391 | // Check the mapped parameters. |
| 7392 | int length = parameter_map->length(); |
| 7393 | for (int i = 2; i < length; ++i) { |
| 7394 | Object* value = parameter_map->get(i); |
| 7395 | if (!value->IsTheHole() && value == obj) return true; |
| 7396 | } |
| 7397 | // Check the arguments. |
| 7398 | FixedArray* arguments = FixedArray::cast(parameter_map->get(1)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7399 | kind = arguments->IsDictionary() ? DICTIONARY_ELEMENTS : |
| 7400 | FAST_HOLEY_ELEMENTS; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7401 | if (ReferencesObjectFromElements(arguments, kind, obj)) return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7402 | break; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7403 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 7404 | case NO_ELEMENTS: |
| 7405 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7406 | } |
| 7407 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7408 | // For functions check the context. |
| 7409 | if (IsJSFunction()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7410 | // Get the constructor function for arguments array. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7411 | Map* arguments_map = |
| 7412 | heap->isolate()->context()->native_context()->sloppy_arguments_map(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7413 | JSFunction* arguments_function = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7414 | JSFunction::cast(arguments_map->GetConstructor()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7415 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7416 | // 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] | 7417 | JSFunction* f = JSFunction::cast(this); |
| 7418 | Context* context = f->context(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7419 | if (context->IsNativeContext()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7420 | return false; |
| 7421 | } |
| 7422 | |
| 7423 | // Check the non-special context slots. |
| 7424 | for (int i = Context::MIN_CONTEXT_SLOTS; i < context->length(); i++) { |
| 7425 | // Only check JS objects. |
| 7426 | if (context->get(i)->IsJSObject()) { |
| 7427 | JSObject* ctxobj = JSObject::cast(context->get(i)); |
| 7428 | // If it is an arguments array check the content. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7429 | if (ctxobj->map()->GetConstructor() == arguments_function) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7430 | if (ctxobj->ReferencesObject(obj)) { |
| 7431 | return true; |
| 7432 | } |
| 7433 | } else if (ctxobj == obj) { |
| 7434 | return true; |
| 7435 | } |
| 7436 | } |
| 7437 | } |
| 7438 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7439 | // Check the context extension (if any) if it can have references. |
| 7440 | if (context->has_extension() && !context->IsCatchContext()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7441 | // With harmony scoping, a JSFunction may have a script context. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7442 | // TODO(mvstanton): walk into the ScopeInfo. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7443 | if (context->IsScriptContext()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7444 | return false; |
| 7445 | } |
| 7446 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7447 | return context->extension_object()->ReferencesObject(obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7448 | } |
| 7449 | } |
| 7450 | |
| 7451 | // No references to object. |
| 7452 | return false; |
| 7453 | } |
| 7454 | |
| 7455 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7456 | Maybe<bool> JSReceiver::SetIntegrityLevel(Handle<JSReceiver> receiver, |
| 7457 | IntegrityLevel level, |
| 7458 | ShouldThrow should_throw) { |
| 7459 | DCHECK(level == SEALED || level == FROZEN); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7460 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7461 | if (receiver->IsJSObject()) { |
| 7462 | Handle<JSObject> object = Handle<JSObject>::cast(receiver); |
| 7463 | if (!object->HasSloppyArgumentsElements() && |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 7464 | !object->map()->is_observed()) { // Fast path. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7465 | if (level == SEALED) { |
| 7466 | return JSObject::PreventExtensionsWithTransition<SEALED>(object, |
| 7467 | should_throw); |
| 7468 | } else { |
| 7469 | return JSObject::PreventExtensionsWithTransition<FROZEN>(object, |
| 7470 | should_throw); |
| 7471 | } |
| 7472 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7473 | } |
| 7474 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7475 | Isolate* isolate = receiver->GetIsolate(); |
| 7476 | |
| 7477 | MAYBE_RETURN(JSReceiver::PreventExtensions(receiver, should_throw), |
| 7478 | Nothing<bool>()); |
| 7479 | |
| 7480 | Handle<FixedArray> keys; |
| 7481 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7482 | isolate, keys, JSReceiver::OwnPropertyKeys(receiver), Nothing<bool>()); |
| 7483 | |
| 7484 | PropertyDescriptor no_conf; |
| 7485 | no_conf.set_configurable(false); |
| 7486 | |
| 7487 | PropertyDescriptor no_conf_no_write; |
| 7488 | no_conf_no_write.set_configurable(false); |
| 7489 | no_conf_no_write.set_writable(false); |
| 7490 | |
| 7491 | if (level == SEALED) { |
| 7492 | for (int i = 0; i < keys->length(); ++i) { |
| 7493 | Handle<Object> key(keys->get(i), isolate); |
| 7494 | MAYBE_RETURN( |
| 7495 | DefineOwnProperty(isolate, receiver, key, &no_conf, THROW_ON_ERROR), |
| 7496 | Nothing<bool>()); |
| 7497 | } |
| 7498 | return Just(true); |
| 7499 | } |
| 7500 | |
| 7501 | for (int i = 0; i < keys->length(); ++i) { |
| 7502 | Handle<Object> key(keys->get(i), isolate); |
| 7503 | PropertyDescriptor current_desc; |
| 7504 | Maybe<bool> owned = JSReceiver::GetOwnPropertyDescriptor( |
| 7505 | isolate, receiver, key, ¤t_desc); |
| 7506 | MAYBE_RETURN(owned, Nothing<bool>()); |
| 7507 | if (owned.FromJust()) { |
| 7508 | PropertyDescriptor desc = |
| 7509 | PropertyDescriptor::IsAccessorDescriptor(¤t_desc) |
| 7510 | ? no_conf |
| 7511 | : no_conf_no_write; |
| 7512 | MAYBE_RETURN( |
| 7513 | DefineOwnProperty(isolate, receiver, key, &desc, THROW_ON_ERROR), |
| 7514 | Nothing<bool>()); |
| 7515 | } |
| 7516 | } |
| 7517 | return Just(true); |
| 7518 | } |
| 7519 | |
| 7520 | |
| 7521 | Maybe<bool> JSReceiver::TestIntegrityLevel(Handle<JSReceiver> object, |
| 7522 | IntegrityLevel level) { |
| 7523 | DCHECK(level == SEALED || level == FROZEN); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7524 | Isolate* isolate = object->GetIsolate(); |
| 7525 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7526 | Maybe<bool> extensible = JSReceiver::IsExtensible(object); |
| 7527 | MAYBE_RETURN(extensible, Nothing<bool>()); |
| 7528 | if (extensible.FromJust()) return Just(false); |
| 7529 | |
| 7530 | Handle<FixedArray> keys; |
| 7531 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7532 | isolate, keys, JSReceiver::OwnPropertyKeys(object), Nothing<bool>()); |
| 7533 | |
| 7534 | for (int i = 0; i < keys->length(); ++i) { |
| 7535 | Handle<Object> key(keys->get(i), isolate); |
| 7536 | PropertyDescriptor current_desc; |
| 7537 | Maybe<bool> owned = JSReceiver::GetOwnPropertyDescriptor( |
| 7538 | isolate, object, key, ¤t_desc); |
| 7539 | MAYBE_RETURN(owned, Nothing<bool>()); |
| 7540 | if (owned.FromJust()) { |
| 7541 | if (current_desc.configurable()) return Just(false); |
| 7542 | if (level == FROZEN && |
| 7543 | PropertyDescriptor::IsDataDescriptor(¤t_desc) && |
| 7544 | current_desc.writable()) { |
| 7545 | return Just(false); |
| 7546 | } |
| 7547 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7548 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7549 | return Just(true); |
| 7550 | } |
| 7551 | |
| 7552 | |
| 7553 | Maybe<bool> JSReceiver::PreventExtensions(Handle<JSReceiver> object, |
| 7554 | ShouldThrow should_throw) { |
| 7555 | if (object->IsJSProxy()) { |
| 7556 | return JSProxy::PreventExtensions(Handle<JSProxy>::cast(object), |
| 7557 | should_throw); |
| 7558 | } |
| 7559 | DCHECK(object->IsJSObject()); |
| 7560 | return JSObject::PreventExtensions(Handle<JSObject>::cast(object), |
| 7561 | should_throw); |
| 7562 | } |
| 7563 | |
| 7564 | |
| 7565 | Maybe<bool> JSProxy::PreventExtensions(Handle<JSProxy> proxy, |
| 7566 | ShouldThrow should_throw) { |
| 7567 | Isolate* isolate = proxy->GetIsolate(); |
| 7568 | STACK_CHECK(Nothing<bool>()); |
| 7569 | Factory* factory = isolate->factory(); |
| 7570 | Handle<String> trap_name = factory->preventExtensions_string(); |
| 7571 | |
| 7572 | if (proxy->IsRevoked()) { |
| 7573 | isolate->Throw( |
| 7574 | *factory->NewTypeError(MessageTemplate::kProxyRevoked, trap_name)); |
| 7575 | return Nothing<bool>(); |
| 7576 | } |
| 7577 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 7578 | Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate); |
| 7579 | |
| 7580 | Handle<Object> trap; |
| 7581 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7582 | isolate, trap, Object::GetMethod(handler, trap_name), Nothing<bool>()); |
| 7583 | if (trap->IsUndefined()) { |
| 7584 | return JSReceiver::PreventExtensions(target, should_throw); |
| 7585 | } |
| 7586 | |
| 7587 | Handle<Object> trap_result; |
| 7588 | Handle<Object> args[] = {target}; |
| 7589 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7590 | isolate, trap_result, |
| 7591 | Execution::Call(isolate, trap, handler, arraysize(args), args), |
| 7592 | Nothing<bool>()); |
| 7593 | if (!trap_result->BooleanValue()) { |
| 7594 | RETURN_FAILURE( |
| 7595 | isolate, should_throw, |
| 7596 | NewTypeError(MessageTemplate::kProxyTrapReturnedFalsish, trap_name)); |
| 7597 | } |
| 7598 | |
| 7599 | // Enforce the invariant. |
| 7600 | Maybe<bool> target_result = JSReceiver::IsExtensible(target); |
| 7601 | MAYBE_RETURN(target_result, Nothing<bool>()); |
| 7602 | if (target_result.FromJust()) { |
| 7603 | isolate->Throw(*factory->NewTypeError( |
| 7604 | MessageTemplate::kProxyPreventExtensionsExtensible)); |
| 7605 | return Nothing<bool>(); |
| 7606 | } |
| 7607 | return Just(true); |
| 7608 | } |
| 7609 | |
| 7610 | |
| 7611 | Maybe<bool> JSObject::PreventExtensions(Handle<JSObject> object, |
| 7612 | ShouldThrow should_throw) { |
| 7613 | Isolate* isolate = object->GetIsolate(); |
| 7614 | |
| 7615 | if (!object->HasSloppyArgumentsElements() && !object->map()->is_observed()) { |
| 7616 | return PreventExtensionsWithTransition<NONE>(object, should_throw); |
| 7617 | } |
| 7618 | |
| 7619 | if (object->IsAccessCheckNeeded() && |
| 7620 | !isolate->MayAccess(handle(isolate->context()), object)) { |
| 7621 | isolate->ReportFailedAccessCheck(object); |
| 7622 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); |
| 7623 | RETURN_FAILURE(isolate, should_throw, |
| 7624 | NewTypeError(MessageTemplate::kNoAccess)); |
| 7625 | } |
| 7626 | |
| 7627 | if (!object->map()->is_extensible()) return Just(true); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7628 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7629 | if (object->IsJSGlobalProxy()) { |
| 7630 | PrototypeIterator iter(isolate, object); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7631 | if (iter.IsAtEnd()) return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7632 | DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7633 | return PreventExtensions(PrototypeIterator::GetCurrent<JSObject>(iter), |
| 7634 | should_throw); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 7635 | } |
| 7636 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7637 | if (!object->HasFixedTypedArrayElements()) { |
| 7638 | // If there are fast elements we normalize. |
| 7639 | Handle<SeededNumberDictionary> dictionary = NormalizeElements(object); |
| 7640 | DCHECK(object->HasDictionaryElements() || |
| 7641 | object->HasSlowArgumentsElements()); |
| 7642 | |
| 7643 | // Make sure that we never go back to fast case. |
| 7644 | object->RequireSlowElements(*dictionary); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7645 | } |
| 7646 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7647 | // Do a map transition, other objects with this map may still |
| 7648 | // be extensible. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7649 | // TODO(adamk): Extend the NormalizedMapCache to handle non-extensible maps. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7650 | Handle<Map> new_map = Map::Copy(handle(object->map()), "PreventExtensions"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7651 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7652 | new_map->set_is_extensible(false); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7653 | JSObject::MigrateToMap(object, new_map); |
| 7654 | DCHECK(!object->map()->is_extensible()); |
| 7655 | |
| 7656 | if (object->map()->is_observed()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7657 | RETURN_ON_EXCEPTION_VALUE( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7658 | isolate, |
| 7659 | EnqueueChangeRecord(object, "preventExtensions", Handle<Name>(), |
| 7660 | isolate->factory()->the_hole_value()), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7661 | Nothing<bool>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7662 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7663 | return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7664 | } |
| 7665 | |
| 7666 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7667 | Maybe<bool> JSReceiver::IsExtensible(Handle<JSReceiver> object) { |
| 7668 | if (object->IsJSProxy()) { |
| 7669 | return JSProxy::IsExtensible(Handle<JSProxy>::cast(object)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7670 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7671 | return Just(JSObject::IsExtensible(Handle<JSObject>::cast(object))); |
| 7672 | } |
| 7673 | |
| 7674 | |
| 7675 | Maybe<bool> JSProxy::IsExtensible(Handle<JSProxy> proxy) { |
| 7676 | Isolate* isolate = proxy->GetIsolate(); |
| 7677 | STACK_CHECK(Nothing<bool>()); |
| 7678 | Factory* factory = isolate->factory(); |
| 7679 | Handle<String> trap_name = factory->isExtensible_string(); |
| 7680 | |
| 7681 | if (proxy->IsRevoked()) { |
| 7682 | isolate->Throw( |
| 7683 | *factory->NewTypeError(MessageTemplate::kProxyRevoked, trap_name)); |
| 7684 | return Nothing<bool>(); |
| 7685 | } |
| 7686 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 7687 | Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate); |
| 7688 | |
| 7689 | Handle<Object> trap; |
| 7690 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7691 | isolate, trap, Object::GetMethod(handler, trap_name), Nothing<bool>()); |
| 7692 | if (trap->IsUndefined()) { |
| 7693 | return JSReceiver::IsExtensible(target); |
| 7694 | } |
| 7695 | |
| 7696 | Handle<Object> trap_result; |
| 7697 | Handle<Object> args[] = {target}; |
| 7698 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 7699 | isolate, trap_result, |
| 7700 | Execution::Call(isolate, trap, handler, arraysize(args), args), |
| 7701 | Nothing<bool>()); |
| 7702 | |
| 7703 | // Enforce the invariant. |
| 7704 | Maybe<bool> target_result = JSReceiver::IsExtensible(target); |
| 7705 | MAYBE_RETURN(target_result, Nothing<bool>()); |
| 7706 | if (target_result.FromJust() != trap_result->BooleanValue()) { |
| 7707 | isolate->Throw( |
| 7708 | *factory->NewTypeError(MessageTemplate::kProxyIsExtensibleInconsistent, |
| 7709 | factory->ToBoolean(target_result.FromJust()))); |
| 7710 | return Nothing<bool>(); |
| 7711 | } |
| 7712 | return target_result; |
| 7713 | } |
| 7714 | |
| 7715 | |
| 7716 | bool JSObject::IsExtensible(Handle<JSObject> object) { |
| 7717 | Isolate* isolate = object->GetIsolate(); |
| 7718 | if (object->IsAccessCheckNeeded() && |
| 7719 | !isolate->MayAccess(handle(isolate->context()), object)) { |
| 7720 | return true; |
| 7721 | } |
| 7722 | if (object->IsJSGlobalProxy()) { |
| 7723 | PrototypeIterator iter(isolate, *object); |
| 7724 | if (iter.IsAtEnd()) return false; |
| 7725 | DCHECK(iter.GetCurrent()->IsJSGlobalObject()); |
| 7726 | return iter.GetCurrent<JSObject>()->map()->is_extensible(); |
| 7727 | } |
| 7728 | return object->map()->is_extensible(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7729 | } |
| 7730 | |
| 7731 | |
| 7732 | template <typename Dictionary> |
| 7733 | static void ApplyAttributesToDictionary(Dictionary* dictionary, |
| 7734 | const PropertyAttributes attributes) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7735 | int capacity = dictionary->Capacity(); |
| 7736 | for (int i = 0; i < capacity; i++) { |
| 7737 | Object* k = dictionary->KeyAt(i); |
| 7738 | if (dictionary->IsKey(k) && |
| 7739 | !(k->IsSymbol() && Symbol::cast(k)->is_private())) { |
| 7740 | PropertyDetails details = dictionary->DetailsAt(i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7741 | int attrs = attributes; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7742 | // READ_ONLY is an invalid attribute for JS setters/getters. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7743 | if ((attributes & READ_ONLY) && details.type() == ACCESSOR_CONSTANT) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7744 | Object* v = dictionary->ValueAt(i); |
| 7745 | if (v->IsPropertyCell()) v = PropertyCell::cast(v)->value(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7746 | if (v->IsAccessorPair()) attrs &= ~READ_ONLY; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7747 | } |
| 7748 | details = details.CopyAddAttributes( |
| 7749 | static_cast<PropertyAttributes>(attrs)); |
| 7750 | dictionary->DetailsAtPut(i, details); |
| 7751 | } |
| 7752 | } |
| 7753 | } |
| 7754 | |
| 7755 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7756 | template <PropertyAttributes attrs> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7757 | Maybe<bool> JSObject::PreventExtensionsWithTransition( |
| 7758 | Handle<JSObject> object, ShouldThrow should_throw) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7759 | STATIC_ASSERT(attrs == NONE || attrs == SEALED || attrs == FROZEN); |
| 7760 | |
| 7761 | // Sealing/freezing sloppy arguments should be handled elsewhere. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7762 | DCHECK(!object->HasSloppyArgumentsElements()); |
| 7763 | DCHECK(!object->map()->is_observed()); |
| 7764 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7765 | Isolate* isolate = object->GetIsolate(); |
| 7766 | if (object->IsAccessCheckNeeded() && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7767 | !isolate->MayAccess(handle(isolate->context()), object)) { |
| 7768 | isolate->ReportFailedAccessCheck(object); |
| 7769 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); |
| 7770 | RETURN_FAILURE(isolate, should_throw, |
| 7771 | NewTypeError(MessageTemplate::kNoAccess)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7772 | } |
| 7773 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7774 | if (attrs == NONE && !object->map()->is_extensible()) return Just(true); |
| 7775 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7776 | if (object->IsJSGlobalProxy()) { |
| 7777 | PrototypeIterator iter(isolate, object); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7778 | if (iter.IsAtEnd()) return Just(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7779 | DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7780 | return PreventExtensionsWithTransition<attrs>( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7781 | PrototypeIterator::GetCurrent<JSObject>(iter), should_throw); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7782 | } |
| 7783 | |
| 7784 | Handle<SeededNumberDictionary> new_element_dictionary; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7785 | if (!object->HasFixedTypedArrayElements() && |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 7786 | !object->HasDictionaryElements() && |
| 7787 | !object->HasSlowStringWrapperElements()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7788 | int length = |
| 7789 | object->IsJSArray() |
| 7790 | ? Smi::cast(Handle<JSArray>::cast(object)->length())->value() |
| 7791 | : object->elements()->length(); |
| 7792 | new_element_dictionary = |
| 7793 | length == 0 ? isolate->factory()->empty_slow_element_dictionary() |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 7794 | : object->GetElementsAccessor()->Normalize(object); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7795 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7796 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7797 | Handle<Symbol> transition_marker; |
| 7798 | if (attrs == NONE) { |
| 7799 | transition_marker = isolate->factory()->nonextensible_symbol(); |
| 7800 | } else if (attrs == SEALED) { |
| 7801 | transition_marker = isolate->factory()->sealed_symbol(); |
| 7802 | } else { |
| 7803 | DCHECK(attrs == FROZEN); |
| 7804 | transition_marker = isolate->factory()->frozen_symbol(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7805 | } |
| 7806 | |
| 7807 | Handle<Map> old_map(object->map(), isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7808 | Map* transition = |
| 7809 | TransitionArray::SearchSpecial(*old_map, *transition_marker); |
| 7810 | if (transition != NULL) { |
| 7811 | Handle<Map> transition_map(transition, isolate); |
| 7812 | DCHECK(transition_map->has_dictionary_elements() || |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 7813 | transition_map->has_fixed_typed_array_elements() || |
| 7814 | transition_map->elements_kind() == SLOW_STRING_WRAPPER_ELEMENTS); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7815 | DCHECK(!transition_map->is_extensible()); |
| 7816 | JSObject::MigrateToMap(object, transition_map); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7817 | } else if (TransitionArray::CanHaveMoreTransitions(old_map)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7818 | // Create a new descriptor array with the appropriate property attributes |
| 7819 | Handle<Map> new_map = Map::CopyForPreventExtensions( |
| 7820 | old_map, attrs, transition_marker, "CopyForPreventExtensions"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7821 | JSObject::MigrateToMap(object, new_map); |
| 7822 | } else { |
| 7823 | DCHECK(old_map->is_dictionary_map() || !old_map->is_prototype_map()); |
| 7824 | // Slow path: need to normalize properties for safety |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7825 | NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0, |
| 7826 | "SlowPreventExtensions"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7827 | |
| 7828 | // Create a new map, since other objects with this map may be extensible. |
| 7829 | // TODO(adamk): Extend the NormalizedMapCache to handle non-extensible maps. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7830 | Handle<Map> new_map = |
| 7831 | Map::Copy(handle(object->map()), "SlowCopyForPreventExtensions"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7832 | new_map->set_is_extensible(false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7833 | if (!new_element_dictionary.is_null()) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 7834 | ElementsKind new_kind = |
| 7835 | IsStringWrapperElementsKind(old_map->elements_kind()) |
| 7836 | ? SLOW_STRING_WRAPPER_ELEMENTS |
| 7837 | : DICTIONARY_ELEMENTS; |
| 7838 | new_map->set_elements_kind(new_kind); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7839 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7840 | JSObject::MigrateToMap(object, new_map); |
| 7841 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7842 | if (attrs != NONE) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7843 | if (object->IsJSGlobalObject()) { |
| 7844 | ApplyAttributesToDictionary(object->global_dictionary(), attrs); |
| 7845 | } else { |
| 7846 | ApplyAttributesToDictionary(object->property_dictionary(), attrs); |
| 7847 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7848 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7849 | } |
| 7850 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7851 | // Both seal and preventExtensions always go through without modifications to |
| 7852 | // typed array elements. Freeze works only if there are no actual elements. |
| 7853 | if (object->HasFixedTypedArrayElements()) { |
| 7854 | if (attrs == FROZEN && |
| 7855 | JSArrayBufferView::cast(*object)->byte_length()->Number() > 0) { |
| 7856 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 7857 | MessageTemplate::kCannotFreezeArrayBufferView)); |
| 7858 | return Nothing<bool>(); |
| 7859 | } |
| 7860 | return Just(true); |
| 7861 | } |
| 7862 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 7863 | DCHECK(object->map()->has_dictionary_elements() || |
| 7864 | object->map()->elements_kind() == SLOW_STRING_WRAPPER_ELEMENTS); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7865 | if (!new_element_dictionary.is_null()) { |
| 7866 | object->set_elements(*new_element_dictionary); |
| 7867 | } |
| 7868 | |
| 7869 | if (object->elements() != isolate->heap()->empty_slow_element_dictionary()) { |
| 7870 | SeededNumberDictionary* dictionary = object->element_dictionary(); |
| 7871 | // Make sure we never go back to the fast case |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7872 | object->RequireSlowElements(dictionary); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7873 | if (attrs != NONE) { |
| 7874 | ApplyAttributesToDictionary(dictionary, attrs); |
| 7875 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7876 | } |
| 7877 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7878 | return Just(true); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7879 | } |
| 7880 | |
| 7881 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7882 | void JSObject::SetObserved(Handle<JSObject> object) { |
| 7883 | DCHECK(!object->IsJSGlobalProxy()); |
| 7884 | DCHECK(!object->IsJSGlobalObject()); |
| 7885 | Isolate* isolate = object->GetIsolate(); |
| 7886 | Handle<Map> new_map; |
| 7887 | Handle<Map> old_map(object->map(), isolate); |
| 7888 | DCHECK(!old_map->is_observed()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7889 | Map* transition = TransitionArray::SearchSpecial( |
| 7890 | *old_map, isolate->heap()->observed_symbol()); |
| 7891 | if (transition != NULL) { |
| 7892 | new_map = handle(transition, isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7893 | DCHECK(new_map->is_observed()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7894 | } else if (TransitionArray::CanHaveMoreTransitions(old_map)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7895 | new_map = Map::CopyForObserved(old_map); |
| 7896 | } else { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7897 | new_map = Map::Copy(old_map, "SlowObserved"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7898 | new_map->set_is_observed(); |
| 7899 | } |
| 7900 | JSObject::MigrateToMap(object, new_map); |
| 7901 | } |
| 7902 | |
| 7903 | |
| 7904 | Handle<Object> JSObject::FastPropertyAt(Handle<JSObject> object, |
| 7905 | Representation representation, |
| 7906 | FieldIndex index) { |
| 7907 | Isolate* isolate = object->GetIsolate(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7908 | if (object->IsUnboxedDoubleField(index)) { |
| 7909 | double value = object->RawFastDoublePropertyAt(index); |
| 7910 | return isolate->factory()->NewHeapNumber(value); |
| 7911 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7912 | Handle<Object> raw_value(object->RawFastPropertyAt(index), isolate); |
| 7913 | return Object::WrapForRead(isolate, raw_value, representation); |
| 7914 | } |
| 7915 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 7916 | template <class ContextObject> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7917 | class JSObjectWalkVisitor { |
| 7918 | public: |
| 7919 | JSObjectWalkVisitor(ContextObject* site_context, bool copying, |
| 7920 | JSObject::DeepCopyHints hints) |
| 7921 | : site_context_(site_context), |
| 7922 | copying_(copying), |
| 7923 | hints_(hints) {} |
| 7924 | |
| 7925 | MUST_USE_RESULT MaybeHandle<JSObject> StructureWalk(Handle<JSObject> object); |
| 7926 | |
| 7927 | protected: |
| 7928 | MUST_USE_RESULT inline MaybeHandle<JSObject> VisitElementOrProperty( |
| 7929 | Handle<JSObject> object, |
| 7930 | Handle<JSObject> value) { |
| 7931 | Handle<AllocationSite> current_site = site_context()->EnterNewScope(); |
| 7932 | MaybeHandle<JSObject> copy_of_value = StructureWalk(value); |
| 7933 | site_context()->ExitScope(current_site, value); |
| 7934 | return copy_of_value; |
| 7935 | } |
| 7936 | |
| 7937 | inline ContextObject* site_context() { return site_context_; } |
| 7938 | inline Isolate* isolate() { return site_context()->isolate(); } |
| 7939 | |
| 7940 | inline bool copying() const { return copying_; } |
| 7941 | |
| 7942 | private: |
| 7943 | ContextObject* site_context_; |
| 7944 | const bool copying_; |
| 7945 | const JSObject::DeepCopyHints hints_; |
| 7946 | }; |
| 7947 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 7948 | template <class ContextObject> |
| 7949 | MaybeHandle<JSObject> JSObjectWalkVisitor<ContextObject>::StructureWalk( |
| 7950 | Handle<JSObject> object) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7951 | Isolate* isolate = this->isolate(); |
| 7952 | bool copying = this->copying(); |
| 7953 | bool shallow = hints_ == JSObject::kObjectIsShallow; |
| 7954 | |
| 7955 | if (!shallow) { |
| 7956 | StackLimitCheck check(isolate); |
| 7957 | |
| 7958 | if (check.HasOverflowed()) { |
| 7959 | isolate->StackOverflow(); |
| 7960 | return MaybeHandle<JSObject>(); |
| 7961 | } |
| 7962 | } |
| 7963 | |
| 7964 | if (object->map()->is_deprecated()) { |
| 7965 | JSObject::MigrateInstance(object); |
| 7966 | } |
| 7967 | |
| 7968 | Handle<JSObject> copy; |
| 7969 | if (copying) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 7970 | // JSFunction objects are not allowed to be in normal boilerplates at all. |
| 7971 | DCHECK(!object->IsJSFunction()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7972 | Handle<AllocationSite> site_to_pass; |
| 7973 | if (site_context()->ShouldCreateMemento(object)) { |
| 7974 | site_to_pass = site_context()->current(); |
| 7975 | } |
| 7976 | copy = isolate->factory()->CopyJSObjectWithAllocationSite( |
| 7977 | object, site_to_pass); |
| 7978 | } else { |
| 7979 | copy = object; |
| 7980 | } |
| 7981 | |
| 7982 | DCHECK(copying || copy.is_identical_to(object)); |
| 7983 | |
| 7984 | ElementsKind kind = copy->GetElementsKind(); |
| 7985 | if (copying && IsFastSmiOrObjectElementsKind(kind) && |
| 7986 | FixedArray::cast(copy->elements())->map() == |
| 7987 | isolate->heap()->fixed_cow_array_map()) { |
| 7988 | isolate->counters()->cow_arrays_created_runtime()->Increment(); |
| 7989 | } |
| 7990 | |
| 7991 | if (!shallow) { |
| 7992 | HandleScope scope(isolate); |
| 7993 | |
| 7994 | // Deep copy own properties. |
| 7995 | if (copy->HasFastProperties()) { |
| 7996 | Handle<DescriptorArray> descriptors(copy->map()->instance_descriptors()); |
| 7997 | int limit = copy->map()->NumberOfOwnDescriptors(); |
| 7998 | for (int i = 0; i < limit; i++) { |
| 7999 | PropertyDetails details = descriptors->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8000 | if (details.type() != DATA) continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8001 | FieldIndex index = FieldIndex::ForDescriptor(copy->map(), i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8002 | if (object->IsUnboxedDoubleField(index)) { |
| 8003 | if (copying) { |
| 8004 | double value = object->RawFastDoublePropertyAt(index); |
| 8005 | copy->RawFastDoublePropertyAtPut(index, value); |
| 8006 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8007 | } else { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8008 | Handle<Object> value(object->RawFastPropertyAt(index), isolate); |
| 8009 | if (value->IsJSObject()) { |
| 8010 | ASSIGN_RETURN_ON_EXCEPTION( |
| 8011 | isolate, value, |
| 8012 | VisitElementOrProperty(copy, Handle<JSObject>::cast(value)), |
| 8013 | JSObject); |
| 8014 | if (copying) { |
| 8015 | copy->FastPropertyAtPut(index, *value); |
| 8016 | } |
| 8017 | } else { |
| 8018 | if (copying) { |
| 8019 | Representation representation = details.representation(); |
| 8020 | value = Object::NewStorageFor(isolate, value, representation); |
| 8021 | copy->FastPropertyAtPut(index, *value); |
| 8022 | } |
| 8023 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8024 | } |
| 8025 | } |
| 8026 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8027 | // Only deep copy fields from the object literal expression. |
| 8028 | // In particular, don't try to copy the length attribute of |
| 8029 | // an array. |
| 8030 | PropertyFilter filter = static_cast<PropertyFilter>( |
| 8031 | ONLY_WRITABLE | ONLY_ENUMERABLE | ONLY_CONFIGURABLE); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8032 | KeyAccumulator accumulator(isolate, OWN_ONLY, filter); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8033 | accumulator.NextPrototype(); |
| 8034 | copy->CollectOwnPropertyNames(&accumulator, filter); |
| 8035 | Handle<FixedArray> names = accumulator.GetKeys(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8036 | for (int i = 0; i < names->length(); i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8037 | DCHECK(names->get(i)->IsName()); |
| 8038 | Handle<Name> name(Name::cast(names->get(i))); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8039 | Handle<Object> value = |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8040 | JSObject::GetProperty(copy, name).ToHandleChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8041 | if (value->IsJSObject()) { |
| 8042 | Handle<JSObject> result; |
| 8043 | ASSIGN_RETURN_ON_EXCEPTION( |
| 8044 | isolate, result, |
| 8045 | VisitElementOrProperty(copy, Handle<JSObject>::cast(value)), |
| 8046 | JSObject); |
| 8047 | if (copying) { |
| 8048 | // Creating object copy for literals. No strict mode needed. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8049 | JSObject::SetProperty(copy, name, result, SLOPPY).Assert(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8050 | } |
| 8051 | } |
| 8052 | } |
| 8053 | } |
| 8054 | |
| 8055 | // Deep copy own elements. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8056 | switch (kind) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8057 | case FAST_ELEMENTS: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8058 | case FAST_HOLEY_ELEMENTS: { |
| 8059 | Handle<FixedArray> elements(FixedArray::cast(copy->elements())); |
| 8060 | if (elements->map() == isolate->heap()->fixed_cow_array_map()) { |
| 8061 | #ifdef DEBUG |
| 8062 | for (int i = 0; i < elements->length(); i++) { |
| 8063 | DCHECK(!elements->get(i)->IsJSObject()); |
| 8064 | } |
| 8065 | #endif |
| 8066 | } else { |
| 8067 | for (int i = 0; i < elements->length(); i++) { |
| 8068 | Handle<Object> value(elements->get(i), isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8069 | if (value->IsJSObject()) { |
| 8070 | Handle<JSObject> result; |
| 8071 | ASSIGN_RETURN_ON_EXCEPTION( |
| 8072 | isolate, result, |
| 8073 | VisitElementOrProperty(copy, Handle<JSObject>::cast(value)), |
| 8074 | JSObject); |
| 8075 | if (copying) { |
| 8076 | elements->set(i, *result); |
| 8077 | } |
| 8078 | } |
| 8079 | } |
| 8080 | } |
| 8081 | break; |
| 8082 | } |
| 8083 | case DICTIONARY_ELEMENTS: { |
| 8084 | Handle<SeededNumberDictionary> element_dictionary( |
| 8085 | copy->element_dictionary()); |
| 8086 | int capacity = element_dictionary->Capacity(); |
| 8087 | for (int i = 0; i < capacity; i++) { |
| 8088 | Object* k = element_dictionary->KeyAt(i); |
| 8089 | if (element_dictionary->IsKey(k)) { |
| 8090 | Handle<Object> value(element_dictionary->ValueAt(i), isolate); |
| 8091 | if (value->IsJSObject()) { |
| 8092 | Handle<JSObject> result; |
| 8093 | ASSIGN_RETURN_ON_EXCEPTION( |
| 8094 | isolate, result, |
| 8095 | VisitElementOrProperty(copy, Handle<JSObject>::cast(value)), |
| 8096 | JSObject); |
| 8097 | if (copying) { |
| 8098 | element_dictionary->ValueAtPut(i, *result); |
| 8099 | } |
| 8100 | } |
| 8101 | } |
| 8102 | } |
| 8103 | break; |
| 8104 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8105 | case FAST_SLOPPY_ARGUMENTS_ELEMENTS: |
| 8106 | case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8107 | UNIMPLEMENTED(); |
| 8108 | break; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8109 | case FAST_STRING_WRAPPER_ELEMENTS: |
| 8110 | case SLOW_STRING_WRAPPER_ELEMENTS: |
| 8111 | UNREACHABLE(); |
| 8112 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8113 | |
| 8114 | #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8115 | case TYPE##_ELEMENTS: \ |
| 8116 | |
| 8117 | TYPED_ARRAYS(TYPED_ARRAY_CASE) |
| 8118 | #undef TYPED_ARRAY_CASE |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8119 | // Typed elements cannot be created using an object literal. |
| 8120 | UNREACHABLE(); |
| 8121 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8122 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8123 | case FAST_SMI_ELEMENTS: |
| 8124 | case FAST_HOLEY_SMI_ELEMENTS: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8125 | case FAST_DOUBLE_ELEMENTS: |
| 8126 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8127 | case NO_ELEMENTS: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8128 | // No contained objects, nothing to do. |
| 8129 | break; |
| 8130 | } |
| 8131 | } |
| 8132 | |
| 8133 | return copy; |
| 8134 | } |
| 8135 | |
| 8136 | |
| 8137 | MaybeHandle<JSObject> JSObject::DeepWalk( |
| 8138 | Handle<JSObject> object, |
| 8139 | AllocationSiteCreationContext* site_context) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8140 | JSObjectWalkVisitor<AllocationSiteCreationContext> v(site_context, false, |
| 8141 | kNoHints); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8142 | MaybeHandle<JSObject> result = v.StructureWalk(object); |
| 8143 | Handle<JSObject> for_assert; |
| 8144 | DCHECK(!result.ToHandle(&for_assert) || for_assert.is_identical_to(object)); |
| 8145 | return result; |
| 8146 | } |
| 8147 | |
| 8148 | |
| 8149 | MaybeHandle<JSObject> JSObject::DeepCopy( |
| 8150 | Handle<JSObject> object, |
| 8151 | AllocationSiteUsageContext* site_context, |
| 8152 | DeepCopyHints hints) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8153 | JSObjectWalkVisitor<AllocationSiteUsageContext> v(site_context, true, hints); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8154 | MaybeHandle<JSObject> copy = v.StructureWalk(object); |
| 8155 | Handle<JSObject> for_assert; |
| 8156 | DCHECK(!copy.ToHandle(&for_assert) || !for_assert.is_identical_to(object)); |
| 8157 | return copy; |
| 8158 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 8159 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8160 | // static |
| 8161 | MaybeHandle<Object> JSReceiver::ToPrimitive(Handle<JSReceiver> receiver, |
| 8162 | ToPrimitiveHint hint) { |
| 8163 | Isolate* const isolate = receiver->GetIsolate(); |
| 8164 | Handle<Object> exotic_to_prim; |
| 8165 | ASSIGN_RETURN_ON_EXCEPTION( |
| 8166 | isolate, exotic_to_prim, |
| 8167 | GetMethod(receiver, isolate->factory()->to_primitive_symbol()), Object); |
| 8168 | if (!exotic_to_prim->IsUndefined()) { |
| 8169 | Handle<Object> hint_string; |
| 8170 | switch (hint) { |
| 8171 | case ToPrimitiveHint::kDefault: |
| 8172 | hint_string = isolate->factory()->default_string(); |
| 8173 | break; |
| 8174 | case ToPrimitiveHint::kNumber: |
| 8175 | hint_string = isolate->factory()->number_string(); |
| 8176 | break; |
| 8177 | case ToPrimitiveHint::kString: |
| 8178 | hint_string = isolate->factory()->string_string(); |
| 8179 | break; |
| 8180 | } |
| 8181 | Handle<Object> result; |
| 8182 | ASSIGN_RETURN_ON_EXCEPTION( |
| 8183 | isolate, result, |
| 8184 | Execution::Call(isolate, exotic_to_prim, receiver, 1, &hint_string), |
| 8185 | Object); |
| 8186 | if (result->IsPrimitive()) return result; |
| 8187 | THROW_NEW_ERROR(isolate, |
| 8188 | NewTypeError(MessageTemplate::kCannotConvertToPrimitive), |
| 8189 | Object); |
| 8190 | } |
| 8191 | return OrdinaryToPrimitive(receiver, (hint == ToPrimitiveHint::kString) |
| 8192 | ? OrdinaryToPrimitiveHint::kString |
| 8193 | : OrdinaryToPrimitiveHint::kNumber); |
| 8194 | } |
| 8195 | |
| 8196 | |
| 8197 | // static |
| 8198 | MaybeHandle<Object> JSReceiver::OrdinaryToPrimitive( |
| 8199 | Handle<JSReceiver> receiver, OrdinaryToPrimitiveHint hint) { |
| 8200 | Isolate* const isolate = receiver->GetIsolate(); |
| 8201 | Handle<String> method_names[2]; |
| 8202 | switch (hint) { |
| 8203 | case OrdinaryToPrimitiveHint::kNumber: |
| 8204 | method_names[0] = isolate->factory()->valueOf_string(); |
| 8205 | method_names[1] = isolate->factory()->toString_string(); |
| 8206 | break; |
| 8207 | case OrdinaryToPrimitiveHint::kString: |
| 8208 | method_names[0] = isolate->factory()->toString_string(); |
| 8209 | method_names[1] = isolate->factory()->valueOf_string(); |
| 8210 | break; |
| 8211 | } |
| 8212 | for (Handle<String> name : method_names) { |
| 8213 | Handle<Object> method; |
| 8214 | ASSIGN_RETURN_ON_EXCEPTION(isolate, method, |
| 8215 | JSReceiver::GetProperty(receiver, name), Object); |
| 8216 | if (method->IsCallable()) { |
| 8217 | Handle<Object> result; |
| 8218 | ASSIGN_RETURN_ON_EXCEPTION( |
| 8219 | isolate, result, Execution::Call(isolate, method, receiver, 0, NULL), |
| 8220 | Object); |
| 8221 | if (result->IsPrimitive()) return result; |
| 8222 | } |
| 8223 | } |
| 8224 | THROW_NEW_ERROR(isolate, |
| 8225 | NewTypeError(MessageTemplate::kCannotConvertToPrimitive), |
| 8226 | Object); |
| 8227 | } |
| 8228 | |
| 8229 | |
| 8230 | // TODO(cbruni/jkummerow): Consider moving this into elements.cc. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8231 | bool JSObject::HasEnumerableElements() { |
| 8232 | // TODO(cbruni): cleanup |
| 8233 | JSObject* object = this; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8234 | switch (object->GetElementsKind()) { |
| 8235 | case FAST_SMI_ELEMENTS: |
| 8236 | case FAST_ELEMENTS: |
| 8237 | case FAST_DOUBLE_ELEMENTS: { |
| 8238 | int length = object->IsJSArray() |
| 8239 | ? Smi::cast(JSArray::cast(object)->length())->value() |
| 8240 | : object->elements()->length(); |
| 8241 | return length > 0; |
| 8242 | } |
| 8243 | case FAST_HOLEY_SMI_ELEMENTS: |
| 8244 | case FAST_HOLEY_ELEMENTS: { |
| 8245 | FixedArray* elements = FixedArray::cast(object->elements()); |
| 8246 | int length = object->IsJSArray() |
| 8247 | ? Smi::cast(JSArray::cast(object)->length())->value() |
| 8248 | : elements->length(); |
| 8249 | for (int i = 0; i < length; i++) { |
| 8250 | if (!elements->is_the_hole(i)) return true; |
| 8251 | } |
| 8252 | return false; |
| 8253 | } |
| 8254 | case FAST_HOLEY_DOUBLE_ELEMENTS: { |
| 8255 | int length = object->IsJSArray() |
| 8256 | ? Smi::cast(JSArray::cast(object)->length())->value() |
| 8257 | : object->elements()->length(); |
| 8258 | // Zero-length arrays would use the empty FixedArray... |
| 8259 | if (length == 0) return false; |
| 8260 | // ...so only cast to FixedDoubleArray otherwise. |
| 8261 | FixedDoubleArray* elements = FixedDoubleArray::cast(object->elements()); |
| 8262 | for (int i = 0; i < length; i++) { |
| 8263 | if (!elements->is_the_hole(i)) return true; |
| 8264 | } |
| 8265 | return false; |
| 8266 | } |
| 8267 | #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ |
| 8268 | case TYPE##_ELEMENTS: |
| 8269 | |
| 8270 | TYPED_ARRAYS(TYPED_ARRAY_CASE) |
| 8271 | #undef TYPED_ARRAY_CASE |
| 8272 | { |
| 8273 | int length = object->elements()->length(); |
| 8274 | return length > 0; |
| 8275 | } |
| 8276 | case DICTIONARY_ELEMENTS: { |
| 8277 | SeededNumberDictionary* elements = |
| 8278 | SeededNumberDictionary::cast(object->elements()); |
| 8279 | return elements->NumberOfElementsFilterAttributes(ONLY_ENUMERABLE) > 0; |
| 8280 | } |
| 8281 | case FAST_SLOPPY_ARGUMENTS_ELEMENTS: |
| 8282 | case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: |
| 8283 | // We're approximating non-empty arguments objects here. |
| 8284 | return true; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8285 | case FAST_STRING_WRAPPER_ELEMENTS: |
| 8286 | case SLOW_STRING_WRAPPER_ELEMENTS: |
| 8287 | if (String::cast(JSValue::cast(object)->value())->length() > 0) { |
| 8288 | return true; |
| 8289 | } |
| 8290 | return object->elements()->length() > 0; |
| 8291 | case NO_ELEMENTS: |
| 8292 | return false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8293 | } |
| 8294 | UNREACHABLE(); |
| 8295 | return true; |
| 8296 | } |
| 8297 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8298 | // Tests for the fast common case for property enumeration: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8299 | // - This object and all prototypes has an enum cache (which means that |
| 8300 | // it is no proxy, has no interceptors and needs no access checks). |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8301 | // - This object has no elements. |
| 8302 | // - No prototype has enumerable properties/elements. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8303 | bool JSReceiver::IsSimpleEnum() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8304 | for (PrototypeIterator iter(GetIsolate(), this, |
| 8305 | PrototypeIterator::START_AT_RECEIVER); |
| 8306 | !iter.IsAtEnd(); iter.Advance()) { |
| 8307 | if (!iter.GetCurrent()->IsJSObject()) return false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8308 | JSObject* current = iter.GetCurrent<JSObject>(); |
| 8309 | int enum_length = current->map()->EnumLength(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8310 | if (enum_length == kInvalidEnumCacheSentinel) return false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8311 | if (current->IsAccessCheckNeeded()) return false; |
| 8312 | DCHECK(!current->HasNamedInterceptor()); |
| 8313 | DCHECK(!current->HasIndexedInterceptor()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8314 | if (current->HasEnumerableElements()) return false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8315 | if (current != this && enum_length != 0) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8316 | } |
| 8317 | return true; |
| 8318 | } |
| 8319 | |
| 8320 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8321 | int Map::NumberOfDescribedProperties(DescriptorFlag which, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8322 | PropertyFilter filter) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8323 | int result = 0; |
| 8324 | DescriptorArray* descs = instance_descriptors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8325 | int limit = which == ALL_DESCRIPTORS |
| 8326 | ? descs->number_of_descriptors() |
| 8327 | : NumberOfOwnDescriptors(); |
| 8328 | for (int i = 0; i < limit; i++) { |
| 8329 | if ((descs->GetDetails(i).attributes() & filter) == 0 && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8330 | !descs->GetKey(i)->FilterKey(filter)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8331 | result++; |
| 8332 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8333 | } |
| 8334 | return result; |
| 8335 | } |
| 8336 | |
| 8337 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8338 | int Map::NextFreePropertyIndex() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8339 | int free_index = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8340 | int number_of_own_descriptors = NumberOfOwnDescriptors(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8341 | DescriptorArray* descs = instance_descriptors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8342 | for (int i = 0; i < number_of_own_descriptors; i++) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8343 | PropertyDetails details = descs->GetDetails(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8344 | if (details.location() == kField) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8345 | int candidate = details.field_index() + details.field_width_in_words(); |
| 8346 | if (candidate > free_index) free_index = candidate; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8347 | } |
| 8348 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8349 | return free_index; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8350 | } |
| 8351 | |
| 8352 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8353 | static bool ContainsOnlyValidKeys(Handle<FixedArray> array) { |
| 8354 | int len = array->length(); |
| 8355 | for (int i = 0; i < len; i++) { |
| 8356 | Object* e = array->get(i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8357 | if (!(e->IsName() || e->IsNumber())) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8358 | } |
| 8359 | return true; |
| 8360 | } |
| 8361 | |
| 8362 | |
| 8363 | static Handle<FixedArray> ReduceFixedArrayTo( |
| 8364 | Handle<FixedArray> array, int length) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8365 | DCHECK_LE(length, array->length()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8366 | if (array->length() == length) return array; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8367 | return array->GetIsolate()->factory()->CopyFixedArrayUpTo(array, length); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8368 | } |
| 8369 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8370 | bool Map::OnlyHasSimpleProperties() { |
| 8371 | // Wrapped string elements aren't explicitly stored in the elements backing |
| 8372 | // store, but are loaded indirectly from the underlying string. |
| 8373 | return !IsStringWrapperElementsKind(elements_kind()) && |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8374 | instance_type() > LAST_SPECIAL_RECEIVER_TYPE && |
| 8375 | !has_hidden_prototype() && !is_dictionary_map(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8376 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8377 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8378 | // static |
| 8379 | Handle<FixedArray> JSObject::GetFastEnumPropertyKeys(Isolate* isolate, |
| 8380 | Handle<JSObject> object) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8381 | Handle<Map> map(object->map()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8382 | bool cache_enum_length = map->OnlyHasSimpleProperties(); |
| 8383 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8384 | Handle<DescriptorArray> descs = |
| 8385 | Handle<DescriptorArray>(map->instance_descriptors(), isolate); |
| 8386 | int own_property_count = map->EnumLength(); |
| 8387 | // If the enum length of the given map is set to kInvalidEnumCache, this |
| 8388 | // means that the map itself has never used the present enum cache. The |
| 8389 | // first step to using the cache is to set the enum length of the map by |
| 8390 | // counting the number of own descriptors that are ENUMERABLE_STRINGS. |
| 8391 | if (own_property_count == kInvalidEnumCacheSentinel) { |
| 8392 | own_property_count = |
| 8393 | map->NumberOfDescribedProperties(OWN_DESCRIPTORS, ENUMERABLE_STRINGS); |
| 8394 | } else { |
| 8395 | DCHECK( |
| 8396 | own_property_count == |
| 8397 | map->NumberOfDescribedProperties(OWN_DESCRIPTORS, ENUMERABLE_STRINGS)); |
| 8398 | } |
| 8399 | |
| 8400 | if (descs->HasEnumCache()) { |
| 8401 | Handle<FixedArray> keys(descs->GetEnumCache(), isolate); |
| 8402 | // In case the number of properties required in the enum are actually |
| 8403 | // present, we can reuse the enum cache. Otherwise, this means that the |
| 8404 | // enum cache was generated for a previous (smaller) version of the |
| 8405 | // Descriptor Array. In that case we regenerate the enum cache. |
| 8406 | if (own_property_count <= keys->length()) { |
| 8407 | isolate->counters()->enum_cache_hits()->Increment(); |
| 8408 | if (cache_enum_length) map->SetEnumLength(own_property_count); |
| 8409 | return ReduceFixedArrayTo(keys, own_property_count); |
| 8410 | } |
| 8411 | } |
| 8412 | |
| 8413 | if (descs->IsEmpty()) { |
| 8414 | isolate->counters()->enum_cache_hits()->Increment(); |
| 8415 | if (cache_enum_length) map->SetEnumLength(0); |
| 8416 | return isolate->factory()->empty_fixed_array(); |
| 8417 | } |
| 8418 | |
| 8419 | isolate->counters()->enum_cache_misses()->Increment(); |
| 8420 | |
| 8421 | Handle<FixedArray> storage = |
| 8422 | isolate->factory()->NewFixedArray(own_property_count); |
| 8423 | Handle<FixedArray> indices = |
| 8424 | isolate->factory()->NewFixedArray(own_property_count); |
| 8425 | |
| 8426 | int size = map->NumberOfOwnDescriptors(); |
| 8427 | int index = 0; |
| 8428 | |
| 8429 | for (int i = 0; i < size; i++) { |
| 8430 | PropertyDetails details = descs->GetDetails(i); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8431 | if (details.IsDontEnum()) continue; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8432 | Object* key = descs->GetKey(i); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8433 | if (key->IsSymbol()) continue; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8434 | storage->set(index, key); |
| 8435 | if (!indices.is_null()) { |
| 8436 | if (details.type() != DATA) { |
| 8437 | indices = Handle<FixedArray>(); |
| 8438 | } else { |
| 8439 | FieldIndex field_index = FieldIndex::ForDescriptor(*map, i); |
| 8440 | int load_by_field_index = field_index.GetLoadByFieldIndex(); |
| 8441 | indices->set(index, Smi::FromInt(load_by_field_index)); |
| 8442 | } |
| 8443 | } |
| 8444 | index++; |
| 8445 | } |
| 8446 | DCHECK(index == storage->length()); |
| 8447 | |
| 8448 | DescriptorArray::SetEnumCache(descs, isolate, storage, indices); |
| 8449 | if (cache_enum_length) { |
| 8450 | map->SetEnumLength(own_property_count); |
| 8451 | } |
| 8452 | return storage; |
| 8453 | } |
| 8454 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8455 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8456 | Handle<FixedArray> JSObject::GetEnumPropertyKeys(Handle<JSObject> object) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8457 | Isolate* isolate = object->GetIsolate(); |
| 8458 | if (object->HasFastProperties()) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8459 | return GetFastEnumPropertyKeys(isolate, object); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8460 | } else if (object->IsJSGlobalObject()) { |
| 8461 | Handle<GlobalDictionary> dictionary(object->global_dictionary()); |
| 8462 | int length = dictionary->NumberOfEnumElements(); |
| 8463 | if (length == 0) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8464 | return isolate->factory()->empty_fixed_array(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8465 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8466 | Handle<FixedArray> storage = isolate->factory()->NewFixedArray(length); |
| 8467 | dictionary->CopyEnumKeysTo(*storage); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8468 | return storage; |
| 8469 | } else { |
| 8470 | Handle<NameDictionary> dictionary(object->property_dictionary()); |
| 8471 | int length = dictionary->NumberOfEnumElements(); |
| 8472 | if (length == 0) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8473 | return isolate->factory()->empty_fixed_array(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8474 | } |
| 8475 | Handle<FixedArray> storage = isolate->factory()->NewFixedArray(length); |
| 8476 | dictionary->CopyEnumKeysTo(*storage); |
| 8477 | return storage; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8478 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8479 | } |
| 8480 | |
| 8481 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8482 | enum IndexedOrNamed { kIndexed, kNamed }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8483 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8484 | |
| 8485 | // Returns |true| on success, |nothing| on exception. |
| 8486 | template <class Callback, IndexedOrNamed type> |
| 8487 | static Maybe<bool> GetKeysFromInterceptor(Isolate* isolate, |
| 8488 | Handle<JSReceiver> receiver, |
| 8489 | Handle<JSObject> object, |
| 8490 | PropertyFilter filter, |
| 8491 | KeyAccumulator* accumulator) { |
| 8492 | if (type == kIndexed) { |
| 8493 | if (!object->HasIndexedInterceptor()) return Just(true); |
| 8494 | } else { |
| 8495 | if (!object->HasNamedInterceptor()) return Just(true); |
| 8496 | } |
| 8497 | Handle<InterceptorInfo> interceptor(type == kIndexed |
| 8498 | ? object->GetIndexedInterceptor() |
| 8499 | : object->GetNamedInterceptor(), |
| 8500 | isolate); |
| 8501 | if ((filter & ONLY_ALL_CAN_READ) && !interceptor->all_can_read()) { |
| 8502 | return Just(true); |
| 8503 | } |
| 8504 | PropertyCallbackArguments args(isolate, interceptor->data(), *receiver, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8505 | *object, Object::DONT_THROW); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8506 | Handle<JSObject> result; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8507 | if (!interceptor->enumerator()->IsUndefined()) { |
| 8508 | Callback enum_fun = v8::ToCData<Callback>(interceptor->enumerator()); |
| 8509 | const char* log_tag = type == kIndexed ? "interceptor-indexed-enum" |
| 8510 | : "interceptor-named-enum"; |
| 8511 | LOG(isolate, ApiObjectAccess(log_tag, *object)); |
| 8512 | result = args.Call(enum_fun); |
| 8513 | } |
| 8514 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8515 | if (result.is_null()) return Just(true); |
| 8516 | DCHECK(result->IsJSArray() || result->HasSloppyArgumentsElements()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8517 | // The accumulator takes care of string/symbol filtering. |
| 8518 | if (type == kIndexed) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8519 | accumulator->AddElementKeysFromInterceptor(result); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8520 | } else { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8521 | accumulator->AddKeys(result, DO_NOT_CONVERT); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8522 | } |
| 8523 | return Just(true); |
| 8524 | } |
| 8525 | |
| 8526 | |
| 8527 | // Returns |true| on success, |false| if prototype walking should be stopped, |
| 8528 | // |nothing| if an exception was thrown. |
| 8529 | static Maybe<bool> GetKeysFromJSObject(Isolate* isolate, |
| 8530 | Handle<JSReceiver> receiver, |
| 8531 | Handle<JSObject> object, |
| 8532 | PropertyFilter* filter, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8533 | KeyCollectionType type, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8534 | KeyAccumulator* accumulator) { |
| 8535 | accumulator->NextPrototype(); |
| 8536 | // Check access rights if required. |
| 8537 | if (object->IsAccessCheckNeeded() && |
| 8538 | !isolate->MayAccess(handle(isolate->context()), object)) { |
| 8539 | // The cross-origin spec says that [[Enumerate]] shall return an empty |
| 8540 | // iterator when it doesn't have access... |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8541 | if (type == INCLUDE_PROTOS) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8542 | return Just(false); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8543 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8544 | // ...whereas [[OwnPropertyKeys]] shall return whitelisted properties. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8545 | DCHECK_EQ(OWN_ONLY, type); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8546 | *filter = static_cast<PropertyFilter>(*filter | ONLY_ALL_CAN_READ); |
| 8547 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8548 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8549 | JSObject::CollectOwnElementKeys(object, accumulator, *filter); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8550 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8551 | // Add the element keys from the interceptor. |
| 8552 | Maybe<bool> success = |
| 8553 | GetKeysFromInterceptor<v8::IndexedPropertyEnumeratorCallback, kIndexed>( |
| 8554 | isolate, receiver, object, *filter, accumulator); |
| 8555 | MAYBE_RETURN(success, Nothing<bool>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8556 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8557 | if (*filter == ENUMERABLE_STRINGS) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8558 | Handle<FixedArray> enum_keys = JSObject::GetEnumPropertyKeys(object); |
| 8559 | accumulator->AddKeys(enum_keys, DO_NOT_CONVERT); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8560 | } else { |
| 8561 | object->CollectOwnPropertyNames(accumulator, *filter); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8562 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8563 | |
| 8564 | // Add the property keys from the interceptor. |
| 8565 | success = GetKeysFromInterceptor<v8::GenericNamedPropertyEnumeratorCallback, |
| 8566 | kNamed>(isolate, receiver, object, *filter, |
| 8567 | accumulator); |
| 8568 | MAYBE_RETURN(success, Nothing<bool>()); |
| 8569 | return Just(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8570 | } |
| 8571 | |
| 8572 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8573 | // Helper function for JSReceiver::GetKeys() below. Can be called recursively. |
| 8574 | // Returns |true| or |nothing|. |
| 8575 | static Maybe<bool> GetKeys_Internal(Isolate* isolate, |
| 8576 | Handle<JSReceiver> receiver, |
| 8577 | Handle<JSReceiver> object, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8578 | KeyCollectionType type, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8579 | PropertyFilter filter, |
| 8580 | KeyAccumulator* accumulator) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8581 | // Proxies have no hidden prototype and we should not trigger the |
| 8582 | // [[GetPrototypeOf]] trap on the last iteration when using |
| 8583 | // AdvanceFollowingProxies. |
| 8584 | if (type == OWN_ONLY && object->IsJSProxy()) { |
| 8585 | MAYBE_RETURN(JSProxy::OwnPropertyKeys(isolate, receiver, |
| 8586 | Handle<JSProxy>::cast(object), filter, |
| 8587 | accumulator), |
| 8588 | Nothing<bool>()); |
| 8589 | return Just(true); |
| 8590 | } |
| 8591 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8592 | PrototypeIterator::WhereToEnd end = type == OWN_ONLY |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8593 | ? PrototypeIterator::END_AT_NON_HIDDEN |
| 8594 | : PrototypeIterator::END_AT_NULL; |
| 8595 | for (PrototypeIterator iter(isolate, object, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8596 | PrototypeIterator::START_AT_RECEIVER, end); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8597 | !iter.IsAtEnd();) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8598 | Handle<JSReceiver> current = |
| 8599 | PrototypeIterator::GetCurrent<JSReceiver>(iter); |
| 8600 | Maybe<bool> result = Just(false); // Dummy initialization. |
| 8601 | if (current->IsJSProxy()) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8602 | result = JSProxy::OwnPropertyKeys(isolate, receiver, |
| 8603 | Handle<JSProxy>::cast(current), filter, |
| 8604 | accumulator); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8605 | } else { |
| 8606 | DCHECK(current->IsJSObject()); |
| 8607 | result = GetKeysFromJSObject(isolate, receiver, |
| 8608 | Handle<JSObject>::cast(current), &filter, |
| 8609 | type, accumulator); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8610 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8611 | MAYBE_RETURN(result, Nothing<bool>()); |
| 8612 | if (!result.FromJust()) break; // |false| means "stop iterating". |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8613 | // Iterate through proxies but ignore access checks for the ALL_CAN_READ |
| 8614 | // case on API objects for OWN_ONLY keys handlede in GgetKeysFromJSObject. |
| 8615 | if (!iter.AdvanceFollowingProxiesIgnoringAccessChecks()) { |
| 8616 | return Nothing<bool>(); |
| 8617 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8618 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8619 | return Just(true); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8620 | } |
| 8621 | |
| 8622 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8623 | // ES6 9.5.12 |
| 8624 | // Returns |true| on success, |nothing| in case of exception. |
| 8625 | // static |
| 8626 | Maybe<bool> JSProxy::OwnPropertyKeys(Isolate* isolate, |
| 8627 | Handle<JSReceiver> receiver, |
| 8628 | Handle<JSProxy> proxy, |
| 8629 | PropertyFilter filter, |
| 8630 | KeyAccumulator* accumulator) { |
| 8631 | STACK_CHECK(Nothing<bool>()); |
| 8632 | // 1. Let handler be the value of the [[ProxyHandler]] internal slot of O. |
| 8633 | Handle<Object> handler(proxy->handler(), isolate); |
| 8634 | // 2. If handler is null, throw a TypeError exception. |
| 8635 | // 3. Assert: Type(handler) is Object. |
| 8636 | if (proxy->IsRevoked()) { |
| 8637 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 8638 | MessageTemplate::kProxyRevoked, isolate->factory()->ownKeys_string())); |
| 8639 | return Nothing<bool>(); |
| 8640 | } |
| 8641 | // 4. Let target be the value of the [[ProxyTarget]] internal slot of O. |
| 8642 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 8643 | // 5. Let trap be ? GetMethod(handler, "ownKeys"). |
| 8644 | Handle<Object> trap; |
| 8645 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 8646 | isolate, trap, Object::GetMethod(Handle<JSReceiver>::cast(handler), |
| 8647 | isolate->factory()->ownKeys_string()), |
| 8648 | Nothing<bool>()); |
| 8649 | // 6. If trap is undefined, then |
| 8650 | if (trap->IsUndefined()) { |
| 8651 | // 6a. Return target.[[OwnPropertyKeys]](). |
| 8652 | return GetKeys_Internal(isolate, receiver, target, OWN_ONLY, filter, |
| 8653 | accumulator); |
| 8654 | } |
| 8655 | // 7. Let trapResultArray be Call(trap, handler, «target»). |
| 8656 | Handle<Object> trap_result_array; |
| 8657 | Handle<Object> args[] = {target}; |
| 8658 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 8659 | isolate, trap_result_array, |
| 8660 | Execution::Call(isolate, trap, handler, arraysize(args), args), |
| 8661 | Nothing<bool>()); |
| 8662 | // 8. Let trapResult be ? CreateListFromArrayLike(trapResultArray, |
| 8663 | // «String, Symbol»). |
| 8664 | Handle<FixedArray> trap_result; |
| 8665 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 8666 | isolate, trap_result, |
| 8667 | Object::CreateListFromArrayLike(isolate, trap_result_array, |
| 8668 | ElementTypes::kStringAndSymbol), |
| 8669 | Nothing<bool>()); |
| 8670 | // 9. Let extensibleTarget be ? IsExtensible(target). |
| 8671 | Maybe<bool> maybe_extensible = JSReceiver::IsExtensible(target); |
| 8672 | MAYBE_RETURN(maybe_extensible, Nothing<bool>()); |
| 8673 | bool extensible_target = maybe_extensible.FromJust(); |
| 8674 | // 10. Let targetKeys be ? target.[[OwnPropertyKeys]](). |
| 8675 | Handle<FixedArray> target_keys; |
| 8676 | ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, target_keys, |
| 8677 | JSReceiver::OwnPropertyKeys(target), |
| 8678 | Nothing<bool>()); |
| 8679 | // 11. (Assert) |
| 8680 | // 12. Let targetConfigurableKeys be an empty List. |
| 8681 | // To save memory, we're re-using target_keys and will modify it in-place. |
| 8682 | Handle<FixedArray> target_configurable_keys = target_keys; |
| 8683 | // 13. Let targetNonconfigurableKeys be an empty List. |
| 8684 | Handle<FixedArray> target_nonconfigurable_keys = |
| 8685 | isolate->factory()->NewFixedArray(target_keys->length()); |
| 8686 | int nonconfigurable_keys_length = 0; |
| 8687 | // 14. Repeat, for each element key of targetKeys: |
| 8688 | for (int i = 0; i < target_keys->length(); ++i) { |
| 8689 | // 14a. Let desc be ? target.[[GetOwnProperty]](key). |
| 8690 | PropertyDescriptor desc; |
| 8691 | Maybe<bool> found = JSReceiver::GetOwnPropertyDescriptor( |
| 8692 | isolate, target, handle(target_keys->get(i), isolate), &desc); |
| 8693 | MAYBE_RETURN(found, Nothing<bool>()); |
| 8694 | // 14b. If desc is not undefined and desc.[[Configurable]] is false, then |
| 8695 | if (found.FromJust() && !desc.configurable()) { |
| 8696 | // 14b i. Append key as an element of targetNonconfigurableKeys. |
| 8697 | target_nonconfigurable_keys->set(nonconfigurable_keys_length, |
| 8698 | target_keys->get(i)); |
| 8699 | nonconfigurable_keys_length++; |
| 8700 | // The key was moved, null it out in the original list. |
| 8701 | target_keys->set(i, Smi::FromInt(0)); |
| 8702 | } else { |
| 8703 | // 14c. Else, |
| 8704 | // 14c i. Append key as an element of targetConfigurableKeys. |
| 8705 | // (No-op, just keep it in |target_keys|.) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8706 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8707 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8708 | accumulator->NextPrototype(); // Prepare for accumulating keys. |
| 8709 | // 15. If extensibleTarget is true and targetNonconfigurableKeys is empty, |
| 8710 | // then: |
| 8711 | if (extensible_target && nonconfigurable_keys_length == 0) { |
| 8712 | // 15a. Return trapResult. |
| 8713 | return accumulator->AddKeysFromProxy(proxy, trap_result); |
| 8714 | } |
| 8715 | // 16. Let uncheckedResultKeys be a new List which is a copy of trapResult. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8716 | Zone set_zone(isolate->allocator()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8717 | const int kPresent = 1; |
| 8718 | const int kGone = 0; |
| 8719 | IdentityMap<int> unchecked_result_keys(isolate->heap(), &set_zone); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8720 | int unchecked_result_keys_size = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8721 | for (int i = 0; i < trap_result->length(); ++i) { |
| 8722 | DCHECK(trap_result->get(i)->IsUniqueName()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8723 | Object* key = trap_result->get(i); |
| 8724 | int* entry = unchecked_result_keys.Get(key); |
| 8725 | if (*entry != kPresent) { |
| 8726 | *entry = kPresent; |
| 8727 | unchecked_result_keys_size++; |
| 8728 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8729 | } |
| 8730 | // 17. Repeat, for each key that is an element of targetNonconfigurableKeys: |
| 8731 | for (int i = 0; i < nonconfigurable_keys_length; ++i) { |
| 8732 | Object* key = target_nonconfigurable_keys->get(i); |
| 8733 | // 17a. If key is not an element of uncheckedResultKeys, throw a |
| 8734 | // TypeError exception. |
| 8735 | int* found = unchecked_result_keys.Find(key); |
| 8736 | if (found == nullptr || *found == kGone) { |
| 8737 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 8738 | MessageTemplate::kProxyOwnKeysMissing, handle(key, isolate))); |
| 8739 | return Nothing<bool>(); |
| 8740 | } |
| 8741 | // 17b. Remove key from uncheckedResultKeys. |
| 8742 | *found = kGone; |
| 8743 | unchecked_result_keys_size--; |
| 8744 | } |
| 8745 | // 18. If extensibleTarget is true, return trapResult. |
| 8746 | if (extensible_target) { |
| 8747 | return accumulator->AddKeysFromProxy(proxy, trap_result); |
| 8748 | } |
| 8749 | // 19. Repeat, for each key that is an element of targetConfigurableKeys: |
| 8750 | for (int i = 0; i < target_configurable_keys->length(); ++i) { |
| 8751 | Object* key = target_configurable_keys->get(i); |
| 8752 | if (key->IsSmi()) continue; // Zapped entry, was nonconfigurable. |
| 8753 | // 19a. If key is not an element of uncheckedResultKeys, throw a |
| 8754 | // TypeError exception. |
| 8755 | int* found = unchecked_result_keys.Find(key); |
| 8756 | if (found == nullptr || *found == kGone) { |
| 8757 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 8758 | MessageTemplate::kProxyOwnKeysMissing, handle(key, isolate))); |
| 8759 | return Nothing<bool>(); |
| 8760 | } |
| 8761 | // 19b. Remove key from uncheckedResultKeys. |
| 8762 | *found = kGone; |
| 8763 | unchecked_result_keys_size--; |
| 8764 | } |
| 8765 | // 20. If uncheckedResultKeys is not empty, throw a TypeError exception. |
| 8766 | if (unchecked_result_keys_size != 0) { |
| 8767 | DCHECK_GT(unchecked_result_keys_size, 0); |
| 8768 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 8769 | MessageTemplate::kProxyOwnKeysNonExtensible)); |
| 8770 | return Nothing<bool>(); |
| 8771 | } |
| 8772 | // 21. Return trapResult. |
| 8773 | return accumulator->AddKeysFromProxy(proxy, trap_result); |
| 8774 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8775 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8776 | MaybeHandle<FixedArray> JSReceiver::GetKeys(Handle<JSReceiver> object, |
| 8777 | KeyCollectionType type, |
| 8778 | PropertyFilter filter, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8779 | GetKeysConversion keys_conversion, |
| 8780 | bool filter_proxy_keys) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8781 | USE(ContainsOnlyValidKeys); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8782 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8783 | KeyAccumulator accumulator(isolate, type, filter); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8784 | accumulator.set_filter_proxy_keys(filter_proxy_keys); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8785 | MAYBE_RETURN( |
| 8786 | GetKeys_Internal(isolate, object, object, type, filter, &accumulator), |
| 8787 | MaybeHandle<FixedArray>()); |
| 8788 | Handle<FixedArray> keys = accumulator.GetKeys(keys_conversion); |
| 8789 | DCHECK(ContainsOnlyValidKeys(keys)); |
| 8790 | return keys; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8791 | } |
| 8792 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8793 | MUST_USE_RESULT Maybe<bool> FastGetOwnValuesOrEntries( |
| 8794 | Isolate* isolate, Handle<JSReceiver> receiver, bool get_entries, |
| 8795 | Handle<FixedArray>* result) { |
| 8796 | Handle<Map> map(JSReceiver::cast(*receiver)->map(), isolate); |
| 8797 | |
| 8798 | if (!map->IsJSObjectMap()) return Just(false); |
| 8799 | if (!map->OnlyHasSimpleProperties()) return Just(false); |
| 8800 | |
| 8801 | Handle<JSObject> object(JSObject::cast(*receiver)); |
| 8802 | |
| 8803 | Handle<DescriptorArray> descriptors(map->instance_descriptors(), isolate); |
| 8804 | int number_of_own_descriptors = map->NumberOfOwnDescriptors(); |
| 8805 | int number_of_own_elements = |
| 8806 | object->GetElementsAccessor()->GetCapacity(*object, object->elements()); |
| 8807 | Handle<FixedArray> values_or_entries = isolate->factory()->NewFixedArray( |
| 8808 | number_of_own_descriptors + number_of_own_elements); |
| 8809 | int count = 0; |
| 8810 | |
| 8811 | if (object->elements() != isolate->heap()->empty_fixed_array()) { |
| 8812 | MAYBE_RETURN(object->GetElementsAccessor()->CollectValuesOrEntries( |
| 8813 | isolate, object, values_or_entries, get_entries, &count, |
| 8814 | ENUMERABLE_STRINGS), |
| 8815 | Nothing<bool>()); |
| 8816 | } |
| 8817 | |
| 8818 | bool stable = object->map() == *map; |
| 8819 | |
| 8820 | for (int index = 0; index < number_of_own_descriptors; index++) { |
| 8821 | Handle<Name> next_key(descriptors->GetKey(index), isolate); |
| 8822 | if (!next_key->IsString()) continue; |
| 8823 | Handle<Object> prop_value; |
| 8824 | |
| 8825 | // Directly decode from the descriptor array if |from| did not change shape. |
| 8826 | if (stable) { |
| 8827 | PropertyDetails details = descriptors->GetDetails(index); |
| 8828 | if (!details.IsEnumerable()) continue; |
| 8829 | if (details.kind() == kData) { |
| 8830 | if (details.location() == kDescriptor) { |
| 8831 | prop_value = handle(descriptors->GetValue(index), isolate); |
| 8832 | } else { |
| 8833 | Representation representation = details.representation(); |
| 8834 | FieldIndex field_index = FieldIndex::ForDescriptor(*map, index); |
| 8835 | prop_value = |
| 8836 | JSObject::FastPropertyAt(object, representation, field_index); |
| 8837 | } |
| 8838 | } else { |
| 8839 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 8840 | isolate, prop_value, JSReceiver::GetProperty(object, next_key), |
| 8841 | Nothing<bool>()); |
| 8842 | stable = object->map() == *map; |
| 8843 | } |
| 8844 | } else { |
| 8845 | // If the map did change, do a slower lookup. We are still guaranteed that |
| 8846 | // the object has a simple shape, and that the key is a name. |
| 8847 | LookupIterator it(object, next_key, LookupIterator::OWN_SKIP_INTERCEPTOR); |
| 8848 | if (!it.IsFound()) continue; |
| 8849 | DCHECK(it.state() == LookupIterator::DATA || |
| 8850 | it.state() == LookupIterator::ACCESSOR); |
| 8851 | if (!it.IsEnumerable()) continue; |
| 8852 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 8853 | isolate, prop_value, Object::GetProperty(&it), Nothing<bool>()); |
| 8854 | } |
| 8855 | |
| 8856 | if (get_entries) { |
| 8857 | prop_value = MakeEntryPair(isolate, next_key, prop_value); |
| 8858 | } |
| 8859 | |
| 8860 | values_or_entries->set(count, *prop_value); |
| 8861 | count++; |
| 8862 | } |
| 8863 | |
| 8864 | if (count < values_or_entries->length()) values_or_entries->Shrink(count); |
| 8865 | *result = values_or_entries; |
| 8866 | return Just(true); |
| 8867 | } |
| 8868 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8869 | MaybeHandle<FixedArray> GetOwnValuesOrEntries(Isolate* isolate, |
| 8870 | Handle<JSReceiver> object, |
| 8871 | PropertyFilter filter, |
| 8872 | bool get_entries) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8873 | Handle<FixedArray> values_or_entries; |
| 8874 | if (filter == ENUMERABLE_STRINGS) { |
| 8875 | Maybe<bool> fast_values_or_entries = FastGetOwnValuesOrEntries( |
| 8876 | isolate, object, get_entries, &values_or_entries); |
| 8877 | if (fast_values_or_entries.IsNothing()) return MaybeHandle<FixedArray>(); |
| 8878 | if (fast_values_or_entries.FromJust()) return values_or_entries; |
| 8879 | } |
| 8880 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8881 | PropertyFilter key_filter = |
| 8882 | static_cast<PropertyFilter>(filter & ~ONLY_ENUMERABLE); |
| 8883 | KeyAccumulator accumulator(isolate, OWN_ONLY, key_filter); |
| 8884 | MAYBE_RETURN(GetKeys_Internal(isolate, object, object, OWN_ONLY, key_filter, |
| 8885 | &accumulator), |
| 8886 | MaybeHandle<FixedArray>()); |
| 8887 | Handle<FixedArray> keys = accumulator.GetKeys(CONVERT_TO_STRING); |
| 8888 | DCHECK(ContainsOnlyValidKeys(keys)); |
| 8889 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8890 | values_or_entries = isolate->factory()->NewFixedArray(keys->length()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8891 | int length = 0; |
| 8892 | |
| 8893 | for (int i = 0; i < keys->length(); ++i) { |
| 8894 | Handle<Name> key = Handle<Name>::cast(handle(keys->get(i), isolate)); |
| 8895 | |
| 8896 | if (filter & ONLY_ENUMERABLE) { |
| 8897 | PropertyDescriptor descriptor; |
| 8898 | Maybe<bool> did_get_descriptor = JSReceiver::GetOwnPropertyDescriptor( |
| 8899 | isolate, object, key, &descriptor); |
| 8900 | MAYBE_RETURN(did_get_descriptor, MaybeHandle<FixedArray>()); |
| 8901 | if (!did_get_descriptor.FromJust() || !descriptor.enumerable()) continue; |
| 8902 | } |
| 8903 | |
| 8904 | Handle<Object> value; |
| 8905 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 8906 | isolate, value, JSReceiver::GetPropertyOrElement(object, key), |
| 8907 | MaybeHandle<FixedArray>()); |
| 8908 | |
| 8909 | if (get_entries) { |
| 8910 | Handle<FixedArray> entry_storage = |
| 8911 | isolate->factory()->NewUninitializedFixedArray(2); |
| 8912 | entry_storage->set(0, *key); |
| 8913 | entry_storage->set(1, *value); |
| 8914 | value = isolate->factory()->NewJSArrayWithElements(entry_storage, |
| 8915 | FAST_ELEMENTS, 2); |
| 8916 | } |
| 8917 | |
| 8918 | values_or_entries->set(length, *value); |
| 8919 | length++; |
| 8920 | } |
| 8921 | if (length < values_or_entries->length()) values_or_entries->Shrink(length); |
| 8922 | return values_or_entries; |
| 8923 | } |
| 8924 | |
| 8925 | MaybeHandle<FixedArray> JSReceiver::GetOwnValues(Handle<JSReceiver> object, |
| 8926 | PropertyFilter filter) { |
| 8927 | return GetOwnValuesOrEntries(object->GetIsolate(), object, filter, false); |
| 8928 | } |
| 8929 | |
| 8930 | MaybeHandle<FixedArray> JSReceiver::GetOwnEntries(Handle<JSReceiver> object, |
| 8931 | PropertyFilter filter) { |
| 8932 | return GetOwnValuesOrEntries(object->GetIsolate(), object, filter, true); |
| 8933 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8934 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8935 | bool Map::DictionaryElementsInPrototypeChainOnly() { |
| 8936 | if (IsDictionaryElementsKind(elements_kind())) { |
| 8937 | return false; |
| 8938 | } |
| 8939 | |
| 8940 | for (PrototypeIterator iter(this); !iter.IsAtEnd(); iter.Advance()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8941 | // Be conservative, don't walk into proxies. |
| 8942 | if (iter.GetCurrent()->IsJSProxy()) return true; |
| 8943 | // String wrappers have non-configurable, non-writable elements. |
| 8944 | if (iter.GetCurrent()->IsStringWrapper()) return true; |
| 8945 | JSObject* current = iter.GetCurrent<JSObject>(); |
| 8946 | |
| 8947 | if (current->HasDictionaryElements() && |
| 8948 | current->element_dictionary()->requires_slow_elements()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8949 | return true; |
| 8950 | } |
| 8951 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8952 | if (current->HasSlowArgumentsElements()) { |
| 8953 | FixedArray* parameter_map = FixedArray::cast(current->elements()); |
| 8954 | Object* arguments = parameter_map->get(1); |
| 8955 | if (SeededNumberDictionary::cast(arguments)->requires_slow_elements()) { |
| 8956 | return true; |
| 8957 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8958 | } |
| 8959 | } |
| 8960 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8961 | return false; |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 8962 | } |
| 8963 | |
| 8964 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8965 | MaybeHandle<Object> JSObject::DefineAccessor(Handle<JSObject> object, |
| 8966 | Handle<Name> name, |
| 8967 | Handle<Object> getter, |
| 8968 | Handle<Object> setter, |
| 8969 | PropertyAttributes attributes) { |
| 8970 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8971 | |
| 8972 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 8973 | isolate, object, name, LookupIterator::HIDDEN_SKIP_INTERCEPTOR); |
| 8974 | return DefineAccessor(&it, getter, setter, attributes); |
| 8975 | } |
| 8976 | |
| 8977 | |
| 8978 | MaybeHandle<Object> JSObject::DefineAccessor(LookupIterator* it, |
| 8979 | Handle<Object> getter, |
| 8980 | Handle<Object> setter, |
| 8981 | PropertyAttributes attributes) { |
| 8982 | Isolate* isolate = it->isolate(); |
| 8983 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8984 | it->UpdateProtector(); |
| 8985 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9003 | (it->IsElement() || !it->name()->IsPrivate()); |
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())) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 9010 | old_value = Object::GetProperty(it).ToHandleChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9011 | } |
| 9012 | } |
| 9013 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9014 | DCHECK(getter->IsCallable() || getter->IsUndefined() || getter->IsNull() || |
| 9015 | getter->IsFunctionTemplateInfo()); |
| 9016 | DCHECK(setter->IsCallable() || setter->IsUndefined() || setter->IsNull() || |
| 9017 | getter->IsFunctionTemplateInfo()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9018 | // At least one of the accessors needs to be a new value. |
| 9019 | DCHECK(!getter->IsNull() || !setter->IsNull()); |
| 9020 | if (!getter->IsNull()) { |
| 9021 | it->TransitionToAccessorProperty(ACCESSOR_GETTER, getter, attributes); |
| 9022 | } |
| 9023 | if (!setter->IsNull()) { |
| 9024 | it->TransitionToAccessorProperty(ACCESSOR_SETTER, setter, attributes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9025 | } |
| 9026 | |
| 9027 | if (is_observed) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9028 | // Make sure the top context isn't changed. |
| 9029 | AssertNoContextChange ncc(isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9030 | const char* type = preexists ? "reconfigure" : "add"; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9031 | RETURN_ON_EXCEPTION( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9032 | isolate, EnqueueChangeRecord(object, type, it->GetName(), old_value), |
| 9033 | Object); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9034 | } |
| 9035 | |
| 9036 | return isolate->factory()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9037 | } |
| 9038 | |
| 9039 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9040 | MaybeHandle<Object> JSObject::SetAccessor(Handle<JSObject> object, |
| 9041 | Handle<AccessorInfo> info) { |
| 9042 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9043 | Handle<Name> name(Name::cast(info->name()), isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9044 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9045 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 9046 | isolate, object, name, LookupIterator::HIDDEN_SKIP_INTERCEPTOR); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 9047 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9048 | // Duplicate ACCESS_CHECK outside of GetPropertyAttributes for the case that |
| 9049 | // the FailedAccessCheckCallbackFunction doesn't throw an exception. |
| 9050 | // |
| 9051 | // TODO(verwaest): Force throw an exception if the callback doesn't, so we can |
| 9052 | // remove reliance on default return values. |
| 9053 | if (it.state() == LookupIterator::ACCESS_CHECK) { |
| 9054 | if (!it.HasAccess()) { |
| 9055 | isolate->ReportFailedAccessCheck(object); |
| 9056 | RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 9057 | return it.factory()->undefined_value(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 9058 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9059 | it.Next(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 9060 | } |
| 9061 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9062 | // Ignore accessors on typed arrays. |
| 9063 | if (it.IsElement() && object->HasFixedTypedArrayElements()) { |
| 9064 | return it.factory()->undefined_value(); |
| 9065 | } |
| 9066 | |
| 9067 | CHECK(GetPropertyAttributes(&it).IsJust()); |
| 9068 | |
| 9069 | // ES5 forbids turning a property into an accessor if it's not |
| 9070 | // configurable. See 8.6.1 (Table 5). |
| 9071 | if (it.IsFound() && !it.IsConfigurable()) { |
| 9072 | return it.factory()->undefined_value(); |
| 9073 | } |
| 9074 | |
| 9075 | it.TransitionToAccessorPair(info, info->property_attributes()); |
| 9076 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9077 | return object; |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 9078 | } |
| 9079 | |
| 9080 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9081 | MaybeHandle<Object> JSObject::GetAccessor(Handle<JSObject> object, |
| 9082 | Handle<Name> name, |
| 9083 | AccessorComponent component) { |
| 9084 | Isolate* isolate = object->GetIsolate(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9085 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9086 | // Make sure that the top context does not change when doing callbacks or |
| 9087 | // interceptor calls. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9088 | AssertNoContextChange ncc(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9089 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9090 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 9091 | isolate, object, name, LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR); |
| 9092 | |
| 9093 | for (; it.IsFound(); it.Next()) { |
| 9094 | switch (it.state()) { |
| 9095 | case LookupIterator::INTERCEPTOR: |
| 9096 | case LookupIterator::NOT_FOUND: |
| 9097 | case LookupIterator::TRANSITION: |
| 9098 | UNREACHABLE(); |
| 9099 | |
| 9100 | case LookupIterator::ACCESS_CHECK: |
| 9101 | if (it.HasAccess()) continue; |
| 9102 | isolate->ReportFailedAccessCheck(it.GetHolder<JSObject>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9103 | RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 9104 | return isolate->factory()->undefined_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9105 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9106 | case LookupIterator::JSPROXY: |
| 9107 | return isolate->factory()->undefined_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9108 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9109 | case LookupIterator::INTEGER_INDEXED_EXOTIC: |
| 9110 | return isolate->factory()->undefined_value(); |
| 9111 | case LookupIterator::DATA: |
| 9112 | continue; |
| 9113 | case LookupIterator::ACCESSOR: { |
| 9114 | Handle<Object> maybe_pair = it.GetAccessors(); |
| 9115 | if (maybe_pair->IsAccessorPair()) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9116 | return AccessorPair::GetComponent( |
| 9117 | Handle<AccessorPair>::cast(maybe_pair), component); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9118 | } |
| 9119 | } |
| 9120 | } |
| 9121 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9122 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9123 | return isolate->factory()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9124 | } |
| 9125 | |
| 9126 | |
| 9127 | Object* JSObject::SlowReverseLookup(Object* value) { |
| 9128 | if (HasFastProperties()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9129 | int number_of_own_descriptors = map()->NumberOfOwnDescriptors(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9130 | DescriptorArray* descs = map()->instance_descriptors(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9131 | bool value_is_number = value->IsNumber(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9132 | for (int i = 0; i < number_of_own_descriptors; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9133 | if (descs->GetType(i) == DATA) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9134 | FieldIndex field_index = FieldIndex::ForDescriptor(map(), i); |
| 9135 | if (IsUnboxedDoubleField(field_index)) { |
| 9136 | if (value_is_number) { |
| 9137 | double property = RawFastDoublePropertyAt(field_index); |
| 9138 | if (property == value->Number()) { |
| 9139 | return descs->GetKey(i); |
| 9140 | } |
| 9141 | } |
| 9142 | } else { |
| 9143 | Object* property = RawFastPropertyAt(field_index); |
| 9144 | if (field_index.is_double()) { |
| 9145 | DCHECK(property->IsMutableHeapNumber()); |
| 9146 | if (value_is_number && property->Number() == value->Number()) { |
| 9147 | return descs->GetKey(i); |
| 9148 | } |
| 9149 | } else if (property == value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9150 | return descs->GetKey(i); |
| 9151 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9152 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9153 | } else if (descs->GetType(i) == DATA_CONSTANT) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9154 | if (descs->GetConstant(i) == value) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9155 | return descs->GetKey(i); |
| 9156 | } |
| 9157 | } |
| 9158 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9159 | return GetHeap()->undefined_value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9160 | } else if (IsJSGlobalObject()) { |
| 9161 | return global_dictionary()->SlowReverseLookup(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9162 | } else { |
| 9163 | return property_dictionary()->SlowReverseLookup(value); |
| 9164 | } |
| 9165 | } |
| 9166 | |
| 9167 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9168 | Handle<Map> Map::RawCopy(Handle<Map> map, int instance_size) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9169 | Isolate* isolate = map->GetIsolate(); |
| 9170 | Handle<Map> result = |
| 9171 | isolate->factory()->NewMap(map->instance_type(), instance_size); |
| 9172 | Handle<Object> prototype(map->prototype(), isolate); |
| 9173 | Map::SetPrototype(result, prototype); |
| 9174 | result->set_constructor_or_backpointer(map->GetConstructor()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9175 | result->set_bit_field(map->bit_field()); |
| 9176 | result->set_bit_field2(map->bit_field2()); |
| 9177 | int new_bit_field3 = map->bit_field3(); |
| 9178 | new_bit_field3 = OwnsDescriptors::update(new_bit_field3, true); |
| 9179 | new_bit_field3 = NumberOfOwnDescriptorsBits::update(new_bit_field3, 0); |
| 9180 | new_bit_field3 = EnumLengthBits::update(new_bit_field3, |
| 9181 | kInvalidEnumCacheSentinel); |
| 9182 | new_bit_field3 = Deprecated::update(new_bit_field3, false); |
| 9183 | if (!map->is_dictionary_map()) { |
| 9184 | new_bit_field3 = IsUnstable::update(new_bit_field3, false); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9185 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9186 | result->set_bit_field3(new_bit_field3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9187 | return result; |
| 9188 | } |
| 9189 | |
| 9190 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9191 | Handle<Map> Map::Normalize(Handle<Map> fast_map, PropertyNormalizationMode mode, |
| 9192 | const char* reason) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9193 | DCHECK(!fast_map->is_dictionary_map()); |
| 9194 | |
| 9195 | Isolate* isolate = fast_map->GetIsolate(); |
| 9196 | Handle<Object> maybe_cache(isolate->native_context()->normalized_map_cache(), |
| 9197 | isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9198 | bool use_cache = !fast_map->is_prototype_map() && !maybe_cache->IsUndefined(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9199 | Handle<NormalizedMapCache> cache; |
| 9200 | if (use_cache) cache = Handle<NormalizedMapCache>::cast(maybe_cache); |
| 9201 | |
| 9202 | Handle<Map> new_map; |
| 9203 | if (use_cache && cache->Get(fast_map, mode).ToHandle(&new_map)) { |
| 9204 | #ifdef VERIFY_HEAP |
| 9205 | if (FLAG_verify_heap) new_map->DictionaryMapVerify(); |
| 9206 | #endif |
| 9207 | #ifdef ENABLE_SLOW_DCHECKS |
| 9208 | if (FLAG_enable_slow_asserts) { |
| 9209 | // The cached map should match newly created normalized map bit-by-bit, |
| 9210 | // except for the code cache, which can contain some ics which can be |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9211 | // applied to the shared map, dependent code and weak cell cache. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9212 | Handle<Map> fresh = Map::CopyNormalized(fast_map, mode); |
| 9213 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9214 | if (new_map->is_prototype_map()) { |
| 9215 | // For prototype maps, the PrototypeInfo is not copied. |
| 9216 | DCHECK(memcmp(fresh->address(), new_map->address(), |
| 9217 | kTransitionsOrPrototypeInfoOffset) == 0); |
| 9218 | DCHECK(fresh->raw_transitions() == Smi::FromInt(0)); |
| 9219 | STATIC_ASSERT(kDescriptorsOffset == |
| 9220 | kTransitionsOrPrototypeInfoOffset + kPointerSize); |
| 9221 | DCHECK(memcmp(HeapObject::RawField(*fresh, kDescriptorsOffset), |
| 9222 | HeapObject::RawField(*new_map, kDescriptorsOffset), |
| 9223 | kCodeCacheOffset - kDescriptorsOffset) == 0); |
| 9224 | } else { |
| 9225 | DCHECK(memcmp(fresh->address(), new_map->address(), |
| 9226 | Map::kCodeCacheOffset) == 0); |
| 9227 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9228 | STATIC_ASSERT(Map::kDependentCodeOffset == |
| 9229 | Map::kCodeCacheOffset + kPointerSize); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9230 | STATIC_ASSERT(Map::kWeakCellCacheOffset == |
| 9231 | Map::kDependentCodeOffset + kPointerSize); |
| 9232 | int offset = Map::kWeakCellCacheOffset + kPointerSize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9233 | DCHECK(memcmp(fresh->address() + offset, |
| 9234 | new_map->address() + offset, |
| 9235 | Map::kSize - offset) == 0); |
| 9236 | } |
| 9237 | #endif |
| 9238 | } else { |
| 9239 | new_map = Map::CopyNormalized(fast_map, mode); |
| 9240 | if (use_cache) { |
| 9241 | cache->Set(fast_map, new_map); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9242 | isolate->counters()->maps_normalized()->Increment(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9243 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9244 | #if TRACE_MAPS |
| 9245 | if (FLAG_trace_maps) { |
| 9246 | PrintF("[TraceMaps: Normalize from= %p to= %p reason= %s ]\n", |
| 9247 | reinterpret_cast<void*>(*fast_map), |
| 9248 | reinterpret_cast<void*>(*new_map), reason); |
| 9249 | } |
| 9250 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9251 | } |
| 9252 | fast_map->NotifyLeafMapLayoutChange(); |
| 9253 | return new_map; |
| 9254 | } |
| 9255 | |
| 9256 | |
| 9257 | Handle<Map> Map::CopyNormalized(Handle<Map> map, |
| 9258 | PropertyNormalizationMode mode) { |
| 9259 | int new_instance_size = map->instance_size(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 9260 | if (mode == CLEAR_INOBJECT_PROPERTIES) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9261 | new_instance_size -= map->GetInObjectProperties() * kPointerSize; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 9262 | } |
| 9263 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9264 | Handle<Map> result = RawCopy(map, new_instance_size); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 9265 | |
| 9266 | if (mode != CLEAR_INOBJECT_PROPERTIES) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9267 | result->SetInObjectProperties(map->GetInObjectProperties()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 9268 | } |
| 9269 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9270 | result->set_dictionary_map(true); |
| 9271 | result->set_migration_target(false); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9272 | result->set_construction_counter(kNoSlackTracking); |
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(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 9287 | // Strict function maps have Function as a constructor but the |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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() || |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 9294 | *map == *isolate->strict_generator_function_map()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9295 | #endif |
| 9296 | // Initial maps must always own their descriptors and it's descriptor array |
| 9297 | // does not contain descriptors that do not belong to the map. |
| 9298 | DCHECK(map->owns_descriptors()); |
| 9299 | DCHECK_EQ(map->NumberOfOwnDescriptors(), |
| 9300 | map->instance_descriptors()->number_of_descriptors()); |
| 9301 | |
| 9302 | Handle<Map> result = RawCopy(map, instance_size); |
| 9303 | |
| 9304 | // Please note instance_type and instance_size are set when allocated. |
| 9305 | result->SetInObjectProperties(in_object_properties); |
| 9306 | result->set_unused_property_fields(unused_property_fields); |
| 9307 | |
| 9308 | int number_of_own_descriptors = map->NumberOfOwnDescriptors(); |
| 9309 | if (number_of_own_descriptors > 0) { |
| 9310 | // The copy will use the same descriptors array. |
| 9311 | result->UpdateDescriptors(map->instance_descriptors(), |
| 9312 | map->GetLayoutDescriptor()); |
| 9313 | result->SetNumberOfOwnDescriptors(number_of_own_descriptors); |
| 9314 | |
| 9315 | DCHECK_EQ(result->NumberOfFields(), |
| 9316 | in_object_properties - unused_property_fields); |
| 9317 | } |
| 9318 | |
| 9319 | return result; |
| 9320 | } |
| 9321 | |
| 9322 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9323 | Handle<Map> Map::CopyDropDescriptors(Handle<Map> map) { |
| 9324 | Handle<Map> result = RawCopy(map, map->instance_size()); |
| 9325 | |
| 9326 | // Please note instance_type and instance_size are set when allocated. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9327 | if (map->IsJSObjectMap()) { |
| 9328 | result->SetInObjectProperties(map->GetInObjectProperties()); |
| 9329 | result->set_unused_property_fields(map->unused_property_fields()); |
| 9330 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9331 | result->ClearCodeCache(map->GetHeap()); |
| 9332 | map->NotifyLeafMapLayoutChange(); |
| 9333 | return result; |
| 9334 | } |
| 9335 | |
| 9336 | |
| 9337 | Handle<Map> Map::ShareDescriptor(Handle<Map> map, |
| 9338 | Handle<DescriptorArray> descriptors, |
| 9339 | Descriptor* descriptor) { |
| 9340 | // Sanity check. This path is only to be taken if the map owns its descriptor |
| 9341 | // array, implying that its NumberOfOwnDescriptors equals the number of |
| 9342 | // descriptors in the descriptor array. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9343 | DCHECK_EQ(map->NumberOfOwnDescriptors(), |
| 9344 | map->instance_descriptors()->number_of_descriptors()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9345 | |
| 9346 | Handle<Map> result = CopyDropDescriptors(map); |
| 9347 | Handle<Name> name = descriptor->GetKey(); |
| 9348 | |
| 9349 | // Ensure there's space for the new descriptor in the shared descriptor array. |
| 9350 | if (descriptors->NumberOfSlackDescriptors() == 0) { |
| 9351 | int old_size = descriptors->number_of_descriptors(); |
| 9352 | if (old_size == 0) { |
| 9353 | descriptors = DescriptorArray::Allocate(map->GetIsolate(), 0, 1); |
| 9354 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9355 | int slack = SlackForArraySize(old_size, kMaxNumberOfDescriptors); |
| 9356 | EnsureDescriptorSlack(map, slack); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9357 | descriptors = handle(map->instance_descriptors()); |
| 9358 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9359 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9360 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9361 | Handle<LayoutDescriptor> layout_descriptor = |
| 9362 | FLAG_unbox_double_fields |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9363 | ? LayoutDescriptor::ShareAppend(map, descriptor->GetDetails()) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9364 | : handle(LayoutDescriptor::FastPointerLayout(), map->GetIsolate()); |
| 9365 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9366 | { |
| 9367 | DisallowHeapAllocation no_gc; |
| 9368 | descriptors->Append(descriptor); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9369 | result->InitializeDescriptors(*descriptors, *layout_descriptor); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9370 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9371 | |
| 9372 | DCHECK(result->NumberOfOwnDescriptors() == map->NumberOfOwnDescriptors() + 1); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9373 | ConnectTransition(map, result, name, SIMPLE_PROPERTY_TRANSITION); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9374 | |
| 9375 | return result; |
| 9376 | } |
| 9377 | |
| 9378 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9379 | #if TRACE_MAPS |
| 9380 | |
| 9381 | // static |
| 9382 | void Map::TraceTransition(const char* what, Map* from, Map* to, Name* name) { |
| 9383 | if (FLAG_trace_maps) { |
| 9384 | PrintF("[TraceMaps: %s from= %p to= %p name= ", what, |
| 9385 | reinterpret_cast<void*>(from), reinterpret_cast<void*>(to)); |
| 9386 | name->NameShortPrint(); |
| 9387 | PrintF(" ]\n"); |
| 9388 | } |
| 9389 | } |
| 9390 | |
| 9391 | |
| 9392 | // static |
| 9393 | void Map::TraceAllTransitions(Map* map) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9394 | Object* transitions = map->raw_transitions(); |
| 9395 | int num_transitions = TransitionArray::NumberOfTransitions(transitions); |
| 9396 | for (int i = -0; i < num_transitions; ++i) { |
| 9397 | Map* target = TransitionArray::GetTarget(transitions, i); |
| 9398 | Name* key = TransitionArray::GetKey(transitions, i); |
| 9399 | Map::TraceTransition("Transition", map, target, key); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9400 | Map::TraceAllTransitions(target); |
| 9401 | } |
| 9402 | } |
| 9403 | |
| 9404 | #endif // TRACE_MAPS |
| 9405 | |
| 9406 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9407 | void Map::ConnectTransition(Handle<Map> parent, Handle<Map> child, |
| 9408 | Handle<Name> name, SimpleTransitionFlag flag) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9409 | if (!parent->GetBackPointer()->IsUndefined()) { |
| 9410 | parent->set_owns_descriptors(false); |
| 9411 | } else { |
| 9412 | // |parent| is initial map and it must keep the ownership, there must be no |
| 9413 | // descriptors in the descriptors array that do not belong to the map. |
| 9414 | DCHECK(parent->owns_descriptors()); |
| 9415 | DCHECK_EQ(parent->NumberOfOwnDescriptors(), |
| 9416 | parent->instance_descriptors()->number_of_descriptors()); |
| 9417 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9418 | if (parent->is_prototype_map()) { |
| 9419 | DCHECK(child->is_prototype_map()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9420 | #if TRACE_MAPS |
| 9421 | Map::TraceTransition("NoTransition", *parent, *child, *name); |
| 9422 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9423 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9424 | TransitionArray::Insert(parent, name, child, flag); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9425 | #if TRACE_MAPS |
| 9426 | Map::TraceTransition("Transition", *parent, *child, *name); |
| 9427 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9428 | } |
| 9429 | } |
| 9430 | |
| 9431 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9432 | Handle<Map> Map::CopyReplaceDescriptors( |
| 9433 | Handle<Map> map, Handle<DescriptorArray> descriptors, |
| 9434 | Handle<LayoutDescriptor> layout_descriptor, TransitionFlag flag, |
| 9435 | MaybeHandle<Name> maybe_name, const char* reason, |
| 9436 | SimpleTransitionFlag simple_flag) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9437 | DCHECK(descriptors->IsSortedNoDuplicates()); |
| 9438 | |
| 9439 | Handle<Map> result = CopyDropDescriptors(map); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9440 | |
| 9441 | if (!map->is_prototype_map()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9442 | if (flag == INSERT_TRANSITION && |
| 9443 | TransitionArray::CanHaveMoreTransitions(map)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9444 | result->InitializeDescriptors(*descriptors, *layout_descriptor); |
| 9445 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9446 | Handle<Name> name; |
| 9447 | CHECK(maybe_name.ToHandle(&name)); |
| 9448 | ConnectTransition(map, result, name, simple_flag); |
| 9449 | } else { |
| 9450 | int length = descriptors->number_of_descriptors(); |
| 9451 | for (int i = 0; i < length; i++) { |
| 9452 | descriptors->SetRepresentation(i, Representation::Tagged()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9453 | if (descriptors->GetDetails(i).type() == DATA) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9454 | descriptors->SetValue(i, FieldType::Any()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9455 | } |
| 9456 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9457 | result->InitializeDescriptors(*descriptors, |
| 9458 | LayoutDescriptor::FastPointerLayout()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9459 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9460 | } else { |
| 9461 | result->InitializeDescriptors(*descriptors, *layout_descriptor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9462 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9463 | #if TRACE_MAPS |
| 9464 | if (FLAG_trace_maps && |
| 9465 | // Mirror conditions above that did not call ConnectTransition(). |
| 9466 | (map->is_prototype_map() || |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9467 | !(flag == INSERT_TRANSITION && |
| 9468 | TransitionArray::CanHaveMoreTransitions(map)))) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9469 | PrintF("[TraceMaps: ReplaceDescriptors from= %p to= %p reason= %s ]\n", |
| 9470 | reinterpret_cast<void*>(*map), reinterpret_cast<void*>(*result), |
| 9471 | reason); |
| 9472 | } |
| 9473 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9474 | |
| 9475 | return result; |
| 9476 | } |
| 9477 | |
| 9478 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9479 | // Creates transition tree starting from |split_map| and adding all descriptors |
| 9480 | // starting from descriptor with index |split_map|.NumberOfOwnDescriptors(). |
| 9481 | // The way how it is done is tricky because of GC and special descriptors |
| 9482 | // marking logic. |
| 9483 | Handle<Map> Map::AddMissingTransitions( |
| 9484 | Handle<Map> split_map, Handle<DescriptorArray> descriptors, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9485 | Handle<LayoutDescriptor> full_layout_descriptor) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9486 | DCHECK(descriptors->IsSortedNoDuplicates()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9487 | int split_nof = split_map->NumberOfOwnDescriptors(); |
| 9488 | int nof_descriptors = descriptors->number_of_descriptors(); |
| 9489 | DCHECK_LT(split_nof, nof_descriptors); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9490 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9491 | // Start with creating last map which will own full descriptors array. |
| 9492 | // This is necessary to guarantee that GC will mark the whole descriptor |
| 9493 | // array if any of the allocations happening below fail. |
| 9494 | // Number of unused properties is temporarily incorrect and the layout |
| 9495 | // descriptor could unnecessarily be in slow mode but we will fix after |
| 9496 | // all the other intermediate maps are created. |
| 9497 | Handle<Map> last_map = CopyDropDescriptors(split_map); |
| 9498 | last_map->InitializeDescriptors(*descriptors, *full_layout_descriptor); |
| 9499 | last_map->set_unused_property_fields(0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9500 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9501 | // During creation of intermediate maps we violate descriptors sharing |
| 9502 | // invariant since the last map is not yet connected to the transition tree |
| 9503 | // we create here. But it is safe because GC never trims map's descriptors |
| 9504 | // if there are no dead transitions from that map and this is exactly the |
| 9505 | // case for all the intermediate maps we create here. |
| 9506 | Handle<Map> map = split_map; |
| 9507 | for (int i = split_nof; i < nof_descriptors - 1; ++i) { |
| 9508 | Handle<Map> new_map = CopyDropDescriptors(map); |
| 9509 | InstallDescriptors(map, new_map, i, descriptors, full_layout_descriptor); |
| 9510 | map = new_map; |
| 9511 | } |
| 9512 | map->NotifyLeafMapLayoutChange(); |
| 9513 | InstallDescriptors(map, last_map, nof_descriptors - 1, descriptors, |
| 9514 | full_layout_descriptor); |
| 9515 | return last_map; |
| 9516 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9517 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9518 | |
| 9519 | // Since this method is used to rewrite an existing transition tree, it can |
| 9520 | // always insert transitions without checking. |
| 9521 | void Map::InstallDescriptors(Handle<Map> parent, Handle<Map> child, |
| 9522 | int new_descriptor, |
| 9523 | Handle<DescriptorArray> descriptors, |
| 9524 | Handle<LayoutDescriptor> full_layout_descriptor) { |
| 9525 | DCHECK(descriptors->IsSortedNoDuplicates()); |
| 9526 | |
| 9527 | child->set_instance_descriptors(*descriptors); |
| 9528 | child->SetNumberOfOwnDescriptors(new_descriptor + 1); |
| 9529 | |
| 9530 | int unused_property_fields = parent->unused_property_fields(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9531 | PropertyDetails details = descriptors->GetDetails(new_descriptor); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9532 | if (details.location() == kField) { |
| 9533 | unused_property_fields = parent->unused_property_fields() - 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9534 | if (unused_property_fields < 0) { |
| 9535 | unused_property_fields += JSObject::kFieldsAdded; |
| 9536 | } |
| 9537 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9538 | child->set_unused_property_fields(unused_property_fields); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9539 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9540 | if (FLAG_unbox_double_fields) { |
| 9541 | Handle<LayoutDescriptor> layout_descriptor = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9542 | LayoutDescriptor::AppendIfFastOrUseFull(parent, details, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9543 | full_layout_descriptor); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9544 | child->set_layout_descriptor(*layout_descriptor); |
| 9545 | #ifdef VERIFY_HEAP |
| 9546 | // TODO(ishell): remove these checks from VERIFY_HEAP mode. |
| 9547 | if (FLAG_verify_heap) { |
| 9548 | CHECK(child->layout_descriptor()->IsConsistentWithMap(*child)); |
| 9549 | } |
| 9550 | #else |
| 9551 | SLOW_DCHECK(child->layout_descriptor()->IsConsistentWithMap(*child)); |
| 9552 | #endif |
| 9553 | child->set_visitor_id(Heap::GetStaticVisitorIdForMap(*child)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9554 | } |
| 9555 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9556 | Handle<Name> name = handle(descriptors->GetKey(new_descriptor)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9557 | ConnectTransition(parent, child, name, SIMPLE_PROPERTY_TRANSITION); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9558 | } |
| 9559 | |
| 9560 | |
| 9561 | Handle<Map> Map::CopyAsElementsKind(Handle<Map> map, ElementsKind kind, |
| 9562 | TransitionFlag flag) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9563 | Map* maybe_elements_transition_map = NULL; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9564 | if (flag == INSERT_TRANSITION) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9565 | maybe_elements_transition_map = map->ElementsTransitionMap(); |
| 9566 | DCHECK(maybe_elements_transition_map == NULL || |
| 9567 | (maybe_elements_transition_map->elements_kind() == |
| 9568 | DICTIONARY_ELEMENTS && |
| 9569 | kind == DICTIONARY_ELEMENTS)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9570 | DCHECK(!IsFastElementsKind(kind) || |
| 9571 | IsMoreGeneralElementsKindTransition(map->elements_kind(), kind)); |
| 9572 | DCHECK(kind != map->elements_kind()); |
| 9573 | } |
| 9574 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9575 | bool insert_transition = flag == INSERT_TRANSITION && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9576 | TransitionArray::CanHaveMoreTransitions(map) && |
| 9577 | maybe_elements_transition_map == NULL; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9578 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9579 | if (insert_transition) { |
| 9580 | Handle<Map> new_map = CopyForTransition(map, "CopyAsElementsKind"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9581 | new_map->set_elements_kind(kind); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9582 | |
| 9583 | Isolate* isolate = map->GetIsolate(); |
| 9584 | Handle<Name> name = isolate->factory()->elements_transition_symbol(); |
| 9585 | ConnectTransition(map, new_map, name, SPECIAL_TRANSITION); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9586 | return new_map; |
| 9587 | } |
| 9588 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9589 | // 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] | 9590 | Handle<Map> new_map = Copy(map, "CopyAsElementsKind"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9591 | new_map->set_elements_kind(kind); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9592 | return new_map; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9593 | } |
| 9594 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9595 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9596 | Handle<Map> Map::AsLanguageMode(Handle<Map> initial_map, |
| 9597 | LanguageMode language_mode, FunctionKind kind) { |
| 9598 | DCHECK_EQ(JS_FUNCTION_TYPE, initial_map->instance_type()); |
| 9599 | // Initial map for sloppy mode function is stored in the function |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 9600 | // constructor. Initial maps for strict mode are cached as special transitions |
| 9601 | // using |strict_function_transition_symbol| as a key. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9602 | if (language_mode == SLOPPY) return initial_map; |
| 9603 | Isolate* isolate = initial_map->GetIsolate(); |
| 9604 | Factory* factory = isolate->factory(); |
| 9605 | Handle<Symbol> transition_symbol; |
| 9606 | |
| 9607 | int map_index = Context::FunctionMapIndex(language_mode, kind); |
| 9608 | Handle<Map> function_map( |
| 9609 | Map::cast(isolate->native_context()->get(map_index))); |
| 9610 | |
| 9611 | STATIC_ASSERT(LANGUAGE_END == 3); |
| 9612 | switch (language_mode) { |
| 9613 | case STRICT: |
| 9614 | transition_symbol = factory->strict_function_transition_symbol(); |
| 9615 | break; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9616 | default: |
| 9617 | UNREACHABLE(); |
| 9618 | break; |
| 9619 | } |
| 9620 | Map* maybe_transition = |
| 9621 | TransitionArray::SearchSpecial(*initial_map, *transition_symbol); |
| 9622 | if (maybe_transition != NULL) { |
| 9623 | return handle(maybe_transition, isolate); |
| 9624 | } |
| 9625 | initial_map->NotifyLeafMapLayoutChange(); |
| 9626 | |
| 9627 | // Create new map taking descriptors from the |function_map| and all |
| 9628 | // the other details from the |initial_map|. |
| 9629 | Handle<Map> map = |
| 9630 | Map::CopyInitialMap(function_map, initial_map->instance_size(), |
| 9631 | initial_map->GetInObjectProperties(), |
| 9632 | initial_map->unused_property_fields()); |
| 9633 | map->SetConstructor(initial_map->GetConstructor()); |
| 9634 | map->set_prototype(initial_map->prototype()); |
| 9635 | |
| 9636 | if (TransitionArray::CanHaveMoreTransitions(initial_map)) { |
| 9637 | Map::ConnectTransition(initial_map, map, transition_symbol, |
| 9638 | SPECIAL_TRANSITION); |
| 9639 | } |
| 9640 | return map; |
| 9641 | } |
| 9642 | |
| 9643 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9644 | Handle<Map> Map::CopyForObserved(Handle<Map> map) { |
| 9645 | DCHECK(!map->is_observed()); |
| 9646 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9647 | Isolate* isolate = map->GetIsolate(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9648 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9649 | bool insert_transition = |
| 9650 | TransitionArray::CanHaveMoreTransitions(map) && !map->is_prototype_map(); |
| 9651 | |
| 9652 | if (insert_transition) { |
| 9653 | Handle<Map> new_map = CopyForTransition(map, "CopyForObserved"); |
| 9654 | new_map->set_is_observed(); |
| 9655 | |
| 9656 | Handle<Name> name = isolate->factory()->observed_symbol(); |
| 9657 | ConnectTransition(map, new_map, name, SPECIAL_TRANSITION); |
| 9658 | return new_map; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9659 | } |
| 9660 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9661 | // Create a new free-floating map only if we are not allowed to store it. |
| 9662 | Handle<Map> new_map = Map::Copy(map, "CopyForObserved"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9663 | new_map->set_is_observed(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9664 | return new_map; |
| 9665 | } |
| 9666 | |
| 9667 | |
| 9668 | Handle<Map> Map::CopyForTransition(Handle<Map> map, const char* reason) { |
| 9669 | DCHECK(!map->is_prototype_map()); |
| 9670 | Handle<Map> new_map = CopyDropDescriptors(map); |
| 9671 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9672 | if (map->owns_descriptors()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9673 | // In case the map owned its own descriptors, share the descriptors and |
| 9674 | // transfer ownership to the new map. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9675 | // The properties did not change, so reuse descriptors. |
| 9676 | new_map->InitializeDescriptors(map->instance_descriptors(), |
| 9677 | map->GetLayoutDescriptor()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9678 | } else { |
| 9679 | // In case the map did not own its own descriptors, a split is forced by |
| 9680 | // copying the map; creating a new descriptor array cell. |
| 9681 | Handle<DescriptorArray> descriptors(map->instance_descriptors()); |
| 9682 | int number_of_own_descriptors = map->NumberOfOwnDescriptors(); |
| 9683 | Handle<DescriptorArray> new_descriptors = |
| 9684 | DescriptorArray::CopyUpTo(descriptors, number_of_own_descriptors); |
| 9685 | Handle<LayoutDescriptor> new_layout_descriptor(map->GetLayoutDescriptor(), |
| 9686 | map->GetIsolate()); |
| 9687 | new_map->InitializeDescriptors(*new_descriptors, *new_layout_descriptor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9688 | } |
| 9689 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9690 | #if TRACE_MAPS |
| 9691 | if (FLAG_trace_maps) { |
| 9692 | PrintF("[TraceMaps: CopyForTransition from= %p to= %p reason= %s ]\n", |
| 9693 | reinterpret_cast<void*>(*map), reinterpret_cast<void*>(*new_map), |
| 9694 | reason); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9695 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9696 | #endif |
| 9697 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9698 | return new_map; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9699 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9700 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9701 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9702 | Handle<Map> Map::Copy(Handle<Map> map, const char* reason) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9703 | Handle<DescriptorArray> descriptors(map->instance_descriptors()); |
| 9704 | int number_of_own_descriptors = map->NumberOfOwnDescriptors(); |
| 9705 | Handle<DescriptorArray> new_descriptors = |
| 9706 | DescriptorArray::CopyUpTo(descriptors, number_of_own_descriptors); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9707 | Handle<LayoutDescriptor> new_layout_descriptor(map->GetLayoutDescriptor(), |
| 9708 | map->GetIsolate()); |
| 9709 | return CopyReplaceDescriptors(map, new_descriptors, new_layout_descriptor, |
| 9710 | OMIT_TRANSITION, MaybeHandle<Name>(), reason, |
| 9711 | SPECIAL_TRANSITION); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9712 | } |
| 9713 | |
| 9714 | |
| 9715 | Handle<Map> Map::Create(Isolate* isolate, int inobject_properties) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9716 | Handle<Map> copy = |
| 9717 | Copy(handle(isolate->object_function()->initial_map()), "MapCreate"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9718 | |
| 9719 | // Check that we do not overflow the instance size when adding the extra |
| 9720 | // inobject properties. If the instance size overflows, we allocate as many |
| 9721 | // properties as we can as inobject properties. |
| 9722 | int max_extra_properties = |
| 9723 | (JSObject::kMaxInstanceSize - JSObject::kHeaderSize) >> kPointerSizeLog2; |
| 9724 | |
| 9725 | if (inobject_properties > max_extra_properties) { |
| 9726 | inobject_properties = max_extra_properties; |
| 9727 | } |
| 9728 | |
| 9729 | int new_instance_size = |
| 9730 | JSObject::kHeaderSize + kPointerSize * inobject_properties; |
| 9731 | |
| 9732 | // Adjust the map with the extra inobject properties. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9733 | copy->SetInObjectProperties(inobject_properties); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9734 | copy->set_unused_property_fields(inobject_properties); |
| 9735 | copy->set_instance_size(new_instance_size); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9736 | copy->set_visitor_id(Heap::GetStaticVisitorIdForMap(*copy)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9737 | return copy; |
| 9738 | } |
| 9739 | |
| 9740 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9741 | Handle<Map> Map::CopyForPreventExtensions(Handle<Map> map, |
| 9742 | PropertyAttributes attrs_to_add, |
| 9743 | Handle<Symbol> transition_marker, |
| 9744 | const char* reason) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9745 | int num_descriptors = map->NumberOfOwnDescriptors(); |
| 9746 | Isolate* isolate = map->GetIsolate(); |
| 9747 | Handle<DescriptorArray> new_desc = DescriptorArray::CopyUpToAddAttributes( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9748 | handle(map->instance_descriptors(), isolate), num_descriptors, |
| 9749 | attrs_to_add); |
| 9750 | Handle<LayoutDescriptor> new_layout_descriptor(map->GetLayoutDescriptor(), |
| 9751 | isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9752 | Handle<Map> new_map = CopyReplaceDescriptors( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9753 | map, new_desc, new_layout_descriptor, INSERT_TRANSITION, |
| 9754 | transition_marker, reason, SPECIAL_TRANSITION); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9755 | new_map->set_is_extensible(false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9756 | if (!IsFixedTypedArrayElementsKind(map->elements_kind())) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9757 | ElementsKind new_kind = IsStringWrapperElementsKind(map->elements_kind()) |
| 9758 | ? SLOW_STRING_WRAPPER_ELEMENTS |
| 9759 | : DICTIONARY_ELEMENTS; |
| 9760 | new_map->set_elements_kind(new_kind); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9761 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9762 | return new_map; |
| 9763 | } |
| 9764 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9765 | FieldType* DescriptorArray::GetFieldType(int descriptor_number) { |
| 9766 | DCHECK(GetDetails(descriptor_number).location() == kField); |
| 9767 | Object* value = GetValue(descriptor_number); |
| 9768 | if (value->IsWeakCell()) { |
| 9769 | if (WeakCell::cast(value)->cleared()) return FieldType::None(); |
| 9770 | value = WeakCell::cast(value)->value(); |
| 9771 | } |
| 9772 | return FieldType::cast(value); |
| 9773 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9774 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9775 | namespace { |
| 9776 | |
| 9777 | bool CanHoldValue(DescriptorArray* descriptors, int descriptor, Object* value) { |
| 9778 | PropertyDetails details = descriptors->GetDetails(descriptor); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9779 | switch (details.type()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9780 | case DATA: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9781 | return value->FitsRepresentation(details.representation()) && |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9782 | descriptors->GetFieldType(descriptor)->NowContains(value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9783 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9784 | case DATA_CONSTANT: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9785 | DCHECK(descriptors->GetConstant(descriptor) != value || |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9786 | value->FitsRepresentation(details.representation())); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9787 | return descriptors->GetConstant(descriptor) == value; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9788 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9789 | case ACCESSOR: |
| 9790 | case ACCESSOR_CONSTANT: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9791 | return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9792 | } |
| 9793 | |
| 9794 | UNREACHABLE(); |
| 9795 | return false; |
| 9796 | } |
| 9797 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9798 | Handle<Map> UpdateDescriptorForValue(Handle<Map> map, int descriptor, |
| 9799 | Handle<Object> value) { |
| 9800 | if (CanHoldValue(map->instance_descriptors(), descriptor, *value)) return map; |
| 9801 | |
| 9802 | Isolate* isolate = map->GetIsolate(); |
| 9803 | PropertyAttributes attributes = |
| 9804 | map->instance_descriptors()->GetDetails(descriptor).attributes(); |
| 9805 | Representation representation = value->OptimalRepresentation(); |
| 9806 | Handle<FieldType> type = value->OptimalType(isolate, representation); |
| 9807 | |
| 9808 | return Map::ReconfigureProperty(map, descriptor, kData, attributes, |
| 9809 | representation, type, FORCE_FIELD); |
| 9810 | } |
| 9811 | |
| 9812 | } // namespace |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9813 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9814 | // static |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9815 | Handle<Map> Map::PrepareForDataProperty(Handle<Map> map, int descriptor, |
| 9816 | Handle<Object> value) { |
| 9817 | // Dictionaries can store any property value. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9818 | DCHECK(!map->is_dictionary_map()); |
| 9819 | // Update to the newest map before storing the property. |
| 9820 | return UpdateDescriptorForValue(Update(map), descriptor, value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9821 | } |
| 9822 | |
| 9823 | |
| 9824 | Handle<Map> Map::TransitionToDataProperty(Handle<Map> map, Handle<Name> name, |
| 9825 | Handle<Object> value, |
| 9826 | PropertyAttributes attributes, |
| 9827 | StoreFromKeyed store_mode) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9828 | DCHECK(name->IsUniqueName()); |
| 9829 | DCHECK(!map->is_dictionary_map()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9830 | |
| 9831 | // Migrate to the newest map before storing the property. |
| 9832 | map = Update(map); |
| 9833 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9834 | Map* maybe_transition = |
| 9835 | TransitionArray::SearchTransition(*map, kData, *name, attributes); |
| 9836 | if (maybe_transition != NULL) { |
| 9837 | Handle<Map> transition(maybe_transition); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9838 | int descriptor = transition->LastAdded(); |
| 9839 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9840 | DCHECK_EQ(attributes, transition->instance_descriptors() |
| 9841 | ->GetDetails(descriptor) |
| 9842 | .attributes()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9843 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9844 | return UpdateDescriptorForValue(transition, descriptor, value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9845 | } |
| 9846 | |
| 9847 | TransitionFlag flag = INSERT_TRANSITION; |
| 9848 | MaybeHandle<Map> maybe_map; |
| 9849 | if (value->IsJSFunction()) { |
| 9850 | maybe_map = Map::CopyWithConstant(map, name, value, attributes, flag); |
| 9851 | } else if (!map->TooManyFastProperties(store_mode)) { |
| 9852 | Isolate* isolate = name->GetIsolate(); |
| 9853 | Representation representation = value->OptimalRepresentation(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9854 | Handle<FieldType> type = value->OptimalType(isolate, representation); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9855 | maybe_map = |
| 9856 | Map::CopyWithField(map, name, type, attributes, representation, flag); |
| 9857 | } |
| 9858 | |
| 9859 | Handle<Map> result; |
| 9860 | if (!maybe_map.ToHandle(&result)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9861 | #if TRACE_MAPS |
| 9862 | if (FLAG_trace_maps) { |
| 9863 | Vector<char> name_buffer = Vector<char>::New(100); |
| 9864 | name->NameShortPrint(name_buffer); |
| 9865 | Vector<char> buffer = Vector<char>::New(128); |
| 9866 | SNPrintF(buffer, "TooManyFastProperties %s", name_buffer.start()); |
| 9867 | return Map::Normalize(map, CLEAR_INOBJECT_PROPERTIES, buffer.start()); |
| 9868 | } |
| 9869 | #endif |
| 9870 | return Map::Normalize(map, CLEAR_INOBJECT_PROPERTIES, |
| 9871 | "TooManyFastProperties"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9872 | } |
| 9873 | |
| 9874 | return result; |
| 9875 | } |
| 9876 | |
| 9877 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9878 | Handle<Map> Map::ReconfigureExistingProperty(Handle<Map> map, int descriptor, |
| 9879 | PropertyKind kind, |
| 9880 | PropertyAttributes attributes) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9881 | // Dictionaries have to be reconfigured in-place. |
| 9882 | DCHECK(!map->is_dictionary_map()); |
| 9883 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9884 | if (!map->GetBackPointer()->IsMap()) { |
| 9885 | // There is no benefit from reconstructing transition tree for maps without |
| 9886 | // back pointers. |
| 9887 | return CopyGeneralizeAllRepresentations( |
| 9888 | map, descriptor, FORCE_FIELD, kind, attributes, |
| 9889 | "GenAll_AttributesMismatchProtoMap"); |
| 9890 | } |
| 9891 | |
| 9892 | if (FLAG_trace_generalization) { |
| 9893 | map->PrintReconfiguration(stdout, descriptor, kind, attributes); |
| 9894 | } |
| 9895 | |
| 9896 | Isolate* isolate = map->GetIsolate(); |
| 9897 | Handle<Map> new_map = ReconfigureProperty( |
| 9898 | map, descriptor, kind, attributes, Representation::None(), |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9899 | FieldType::None(isolate), FORCE_FIELD); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9900 | return new_map; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9901 | } |
| 9902 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9903 | Handle<Map> Map::TransitionToAccessorProperty(Handle<Map> map, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 9904 | Handle<Name> name, int descriptor, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9905 | AccessorComponent component, |
| 9906 | Handle<Object> accessor, |
| 9907 | PropertyAttributes attributes) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 9908 | DCHECK(name->IsUniqueName()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9909 | Isolate* isolate = name->GetIsolate(); |
| 9910 | |
| 9911 | // Dictionary maps can always have additional data properties. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9912 | if (map->is_dictionary_map()) return map; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9913 | |
| 9914 | // Migrate to the newest map before transitioning to the new property. |
| 9915 | map = Update(map); |
| 9916 | |
| 9917 | PropertyNormalizationMode mode = map->is_prototype_map() |
| 9918 | ? KEEP_INOBJECT_PROPERTIES |
| 9919 | : CLEAR_INOBJECT_PROPERTIES; |
| 9920 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9921 | Map* maybe_transition = |
| 9922 | TransitionArray::SearchTransition(*map, kAccessor, *name, attributes); |
| 9923 | if (maybe_transition != NULL) { |
| 9924 | Handle<Map> transition(maybe_transition, isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9925 | DescriptorArray* descriptors = transition->instance_descriptors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9926 | int descriptor = transition->LastAdded(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9927 | DCHECK(descriptors->GetKey(descriptor)->Equals(*name)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9928 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9929 | DCHECK_EQ(kAccessor, descriptors->GetDetails(descriptor).kind()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9930 | DCHECK_EQ(attributes, descriptors->GetDetails(descriptor).attributes()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9931 | |
| 9932 | Handle<Object> maybe_pair(descriptors->GetValue(descriptor), isolate); |
| 9933 | if (!maybe_pair->IsAccessorPair()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9934 | return Map::Normalize(map, mode, "TransitionToAccessorFromNonPair"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9935 | } |
| 9936 | |
| 9937 | Handle<AccessorPair> pair = Handle<AccessorPair>::cast(maybe_pair); |
| 9938 | if (pair->get(component) != *accessor) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9939 | return Map::Normalize(map, mode, "TransitionToDifferentAccessor"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9940 | } |
| 9941 | |
| 9942 | return transition; |
| 9943 | } |
| 9944 | |
| 9945 | Handle<AccessorPair> pair; |
| 9946 | DescriptorArray* old_descriptors = map->instance_descriptors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9947 | if (descriptor != DescriptorArray::kNotFound) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9948 | if (descriptor != map->LastAdded()) { |
| 9949 | return Map::Normalize(map, mode, "AccessorsOverwritingNonLast"); |
| 9950 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9951 | PropertyDetails old_details = old_descriptors->GetDetails(descriptor); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9952 | if (old_details.type() != ACCESSOR_CONSTANT) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9953 | return Map::Normalize(map, mode, "AccessorsOverwritingNonAccessors"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9954 | } |
| 9955 | |
| 9956 | if (old_details.attributes() != attributes) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9957 | return Map::Normalize(map, mode, "AccessorsWithAttributes"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9958 | } |
| 9959 | |
| 9960 | Handle<Object> maybe_pair(old_descriptors->GetValue(descriptor), isolate); |
| 9961 | if (!maybe_pair->IsAccessorPair()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9962 | return Map::Normalize(map, mode, "AccessorsOverwritingNonPair"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9963 | } |
| 9964 | |
| 9965 | Object* current = Handle<AccessorPair>::cast(maybe_pair)->get(component); |
| 9966 | if (current == *accessor) return map; |
| 9967 | |
| 9968 | if (!current->IsTheHole()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9969 | return Map::Normalize(map, mode, "AccessorsOverwritingAccessors"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9970 | } |
| 9971 | |
| 9972 | pair = AccessorPair::Copy(Handle<AccessorPair>::cast(maybe_pair)); |
| 9973 | } else if (map->NumberOfOwnDescriptors() >= kMaxNumberOfDescriptors || |
| 9974 | map->TooManyFastProperties(CERTAINLY_NOT_STORE_FROM_KEYED)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 9975 | return Map::Normalize(map, CLEAR_INOBJECT_PROPERTIES, "TooManyAccessors"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9976 | } else { |
| 9977 | pair = isolate->factory()->NewAccessorPair(); |
| 9978 | } |
| 9979 | |
| 9980 | pair->set(component, *accessor); |
| 9981 | TransitionFlag flag = INSERT_TRANSITION; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9982 | AccessorConstantDescriptor new_desc(name, pair, attributes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9983 | return Map::CopyInsertDescriptor(map, &new_desc, flag); |
| 9984 | } |
| 9985 | |
| 9986 | |
| 9987 | Handle<Map> Map::CopyAddDescriptor(Handle<Map> map, |
| 9988 | Descriptor* descriptor, |
| 9989 | TransitionFlag flag) { |
| 9990 | Handle<DescriptorArray> descriptors(map->instance_descriptors()); |
| 9991 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9992 | // Share descriptors only if map owns descriptors and it not an initial map. |
| 9993 | if (flag == INSERT_TRANSITION && map->owns_descriptors() && |
| 9994 | !map->GetBackPointer()->IsUndefined() && |
| 9995 | TransitionArray::CanHaveMoreTransitions(map)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9996 | return ShareDescriptor(map, descriptors, descriptor); |
| 9997 | } |
| 9998 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9999 | int nof = map->NumberOfOwnDescriptors(); |
| 10000 | Handle<DescriptorArray> new_descriptors = |
| 10001 | DescriptorArray::CopyUpTo(descriptors, nof, 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10002 | new_descriptors->Append(descriptor); |
| 10003 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10004 | Handle<LayoutDescriptor> new_layout_descriptor = |
| 10005 | FLAG_unbox_double_fields |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10006 | ? LayoutDescriptor::New(map, new_descriptors, nof + 1) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10007 | : handle(LayoutDescriptor::FastPointerLayout(), map->GetIsolate()); |
| 10008 | |
| 10009 | return CopyReplaceDescriptors(map, new_descriptors, new_layout_descriptor, |
| 10010 | flag, descriptor->GetKey(), "CopyAddDescriptor", |
| 10011 | SIMPLE_PROPERTY_TRANSITION); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10012 | } |
| 10013 | |
| 10014 | |
| 10015 | Handle<Map> Map::CopyInsertDescriptor(Handle<Map> map, |
| 10016 | Descriptor* descriptor, |
| 10017 | TransitionFlag flag) { |
| 10018 | Handle<DescriptorArray> old_descriptors(map->instance_descriptors()); |
| 10019 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10020 | // We replace the key if it is already present. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 10021 | int index = old_descriptors->SearchWithCache(map->GetIsolate(), |
| 10022 | *descriptor->GetKey(), *map); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10023 | if (index != DescriptorArray::kNotFound) { |
| 10024 | return CopyReplaceDescriptor(map, old_descriptors, descriptor, index, flag); |
| 10025 | } |
| 10026 | return CopyAddDescriptor(map, descriptor, flag); |
| 10027 | } |
| 10028 | |
| 10029 | |
| 10030 | Handle<DescriptorArray> DescriptorArray::CopyUpTo( |
| 10031 | Handle<DescriptorArray> desc, |
| 10032 | int enumeration_index, |
| 10033 | int slack) { |
| 10034 | return DescriptorArray::CopyUpToAddAttributes( |
| 10035 | desc, enumeration_index, NONE, slack); |
| 10036 | } |
| 10037 | |
| 10038 | |
| 10039 | Handle<DescriptorArray> DescriptorArray::CopyUpToAddAttributes( |
| 10040 | Handle<DescriptorArray> desc, |
| 10041 | int enumeration_index, |
| 10042 | PropertyAttributes attributes, |
| 10043 | int slack) { |
| 10044 | if (enumeration_index + slack == 0) { |
| 10045 | return desc->GetIsolate()->factory()->empty_descriptor_array(); |
| 10046 | } |
| 10047 | |
| 10048 | int size = enumeration_index; |
| 10049 | |
| 10050 | Handle<DescriptorArray> descriptors = |
| 10051 | DescriptorArray::Allocate(desc->GetIsolate(), size, slack); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10052 | |
| 10053 | if (attributes != NONE) { |
| 10054 | for (int i = 0; i < size; ++i) { |
| 10055 | Object* value = desc->GetValue(i); |
| 10056 | Name* key = desc->GetKey(i); |
| 10057 | PropertyDetails details = desc->GetDetails(i); |
| 10058 | // Bulk attribute changes never affect private properties. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10059 | if (!key->IsPrivate()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10060 | int mask = DONT_DELETE | DONT_ENUM; |
| 10061 | // READ_ONLY is an invalid attribute for JS setters/getters. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10062 | if (details.type() != ACCESSOR_CONSTANT || !value->IsAccessorPair()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10063 | mask |= READ_ONLY; |
| 10064 | } |
| 10065 | details = details.CopyAddAttributes( |
| 10066 | static_cast<PropertyAttributes>(attributes & mask)); |
| 10067 | } |
| 10068 | Descriptor inner_desc( |
| 10069 | handle(key), handle(value, desc->GetIsolate()), details); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10070 | descriptors->SetDescriptor(i, &inner_desc); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10071 | } |
| 10072 | } else { |
| 10073 | for (int i = 0; i < size; ++i) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10074 | descriptors->CopyFrom(i, *desc); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10075 | } |
| 10076 | } |
| 10077 | |
| 10078 | if (desc->number_of_descriptors() != enumeration_index) descriptors->Sort(); |
| 10079 | |
| 10080 | return descriptors; |
| 10081 | } |
| 10082 | |
| 10083 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10084 | bool DescriptorArray::IsEqualUpTo(DescriptorArray* desc, int nof_descriptors) { |
| 10085 | for (int i = 0; i < nof_descriptors; i++) { |
| 10086 | if (GetKey(i) != desc->GetKey(i) || GetValue(i) != desc->GetValue(i)) { |
| 10087 | return false; |
| 10088 | } |
| 10089 | PropertyDetails details = GetDetails(i); |
| 10090 | PropertyDetails other_details = desc->GetDetails(i); |
| 10091 | if (details.type() != other_details.type() || |
| 10092 | !details.representation().Equals(other_details.representation())) { |
| 10093 | return false; |
| 10094 | } |
| 10095 | } |
| 10096 | return true; |
| 10097 | } |
| 10098 | |
| 10099 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10100 | Handle<Map> Map::CopyReplaceDescriptor(Handle<Map> map, |
| 10101 | Handle<DescriptorArray> descriptors, |
| 10102 | Descriptor* descriptor, |
| 10103 | int insertion_index, |
| 10104 | TransitionFlag flag) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 10192 | Isolate* isolate = code_cache->GetIsolate(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10193 | // 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] | 10194 | // flags. This allows call constant stubs to overwrite call field |
| 10195 | // stubs, etc. |
| 10196 | Code::Flags flags = Code::RemoveTypeFromFlags(code->flags()); |
| 10197 | |
| 10198 | // First check whether we can update existing code cache without |
| 10199 | // extending it. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10200 | Handle<FixedArray> cache = handle(code_cache->default_cache()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10201 | int length = cache->length(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10202 | { |
| 10203 | DisallowHeapAllocation no_alloc; |
| 10204 | int deleted_index = -1; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 10205 | Object* null = isolate->heap()->null_value(); |
| 10206 | Object* undefined = isolate->heap()->undefined_value(); |
| 10207 | DCHECK(name->IsUniqueName()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10208 | for (int i = 0; i < length; i += kCodeCacheEntrySize) { |
| 10209 | Object* key = cache->get(i); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 10210 | if (key == null) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10211 | if (deleted_index < 0) deleted_index = i; |
| 10212 | continue; |
| 10213 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 10214 | if (key == undefined) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10215 | if (deleted_index >= 0) i = deleted_index; |
| 10216 | cache->set(i + kCodeCacheEntryNameOffset, *name); |
| 10217 | cache->set(i + kCodeCacheEntryCodeOffset, *code); |
| 10218 | return; |
| 10219 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 10220 | DCHECK(key->IsUniqueName()); |
| 10221 | if (*name == key) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10222 | Code::Flags found = |
| 10223 | Code::cast(cache->get(i + kCodeCacheEntryCodeOffset))->flags(); |
| 10224 | if (Code::RemoveTypeFromFlags(found) == flags) { |
| 10225 | cache->set(i + kCodeCacheEntryCodeOffset, *code); |
| 10226 | return; |
| 10227 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10228 | } |
| 10229 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10230 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10231 | // Reached the end of the code cache. If there were deleted |
| 10232 | // elements, reuse the space for the first of them. |
| 10233 | if (deleted_index >= 0) { |
| 10234 | cache->set(deleted_index + kCodeCacheEntryNameOffset, *name); |
| 10235 | cache->set(deleted_index + kCodeCacheEntryCodeOffset, *code); |
| 10236 | return; |
| 10237 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10238 | } |
| 10239 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10240 | // Extend the code cache with some new entries (at least one). Must be a |
| 10241 | // multiple of the entry size. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10242 | int new_length = length + (length >> 1) + kCodeCacheEntrySize; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10243 | new_length = new_length - new_length % kCodeCacheEntrySize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10244 | DCHECK((new_length % kCodeCacheEntrySize) == 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10245 | cache = isolate->factory()->CopyFixedArrayAndGrow(cache, new_length - length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10246 | |
| 10247 | // Add the (name, code) pair to the new cache. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10248 | cache->set(length + kCodeCacheEntryNameOffset, *name); |
| 10249 | cache->set(length + kCodeCacheEntryCodeOffset, *code); |
| 10250 | code_cache->set_default_cache(*cache); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10251 | } |
| 10252 | |
| 10253 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10254 | void CodeCache::UpdateNormalTypeCache( |
| 10255 | Handle<CodeCache> code_cache, Handle<Name> name, Handle<Code> code) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10256 | // Adding a new entry can cause a new cache to be allocated. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10257 | Handle<CodeCacheHashTable> cache( |
| 10258 | CodeCacheHashTable::cast(code_cache->normal_type_cache())); |
| 10259 | Handle<Object> new_cache = CodeCacheHashTable::Put(cache, name, code); |
| 10260 | code_cache->set_normal_type_cache(*new_cache); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10261 | } |
| 10262 | |
| 10263 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10264 | Object* CodeCache::Lookup(Name* name, Code::Flags flags) { |
| 10265 | Object* result = LookupDefaultCache(name, Code::RemoveTypeFromFlags(flags)); |
| 10266 | if (result->IsCode()) { |
| 10267 | if (Code::cast(result)->flags() == flags) return result; |
| 10268 | return GetHeap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10269 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10270 | return LookupNormalTypeCache(name, flags); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10271 | } |
| 10272 | |
| 10273 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10274 | Object* CodeCache::LookupDefaultCache(Name* name, Code::Flags flags) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10275 | FixedArray* cache = default_cache(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 10276 | Heap* heap = GetHeap(); |
| 10277 | Object* null = heap->null_value(); |
| 10278 | Object* undefined = heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10279 | int length = cache->length(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 10280 | DCHECK(name->IsUniqueName()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10281 | for (int i = 0; i < length; i += kCodeCacheEntrySize) { |
| 10282 | Object* key = cache->get(i + kCodeCacheEntryNameOffset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10283 | // Skip deleted elements. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 10284 | if (key == null) continue; |
| 10285 | if (key == undefined) return key; |
| 10286 | DCHECK(key->IsUniqueName()); |
| 10287 | if (name == key) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10288 | Code* code = Code::cast(cache->get(i + kCodeCacheEntryCodeOffset)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10289 | if (Code::RemoveTypeFromFlags(code->flags()) == flags) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10290 | return code; |
| 10291 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10292 | } |
| 10293 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10294 | return GetHeap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10295 | } |
| 10296 | |
| 10297 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10298 | Object* CodeCache::LookupNormalTypeCache(Name* name, Code::Flags flags) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10299 | if (!normal_type_cache()->IsUndefined()) { |
| 10300 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
| 10301 | return cache->Lookup(name, flags); |
| 10302 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10303 | return GetHeap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10304 | } |
| 10305 | } |
| 10306 | |
| 10307 | |
| 10308 | int CodeCache::GetIndex(Object* name, Code* code) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10309 | if (code->type() == Code::NORMAL) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10310 | if (normal_type_cache()->IsUndefined()) return -1; |
| 10311 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10312 | return cache->GetIndex(Name::cast(name), code->flags()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10313 | } |
| 10314 | |
| 10315 | FixedArray* array = default_cache(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10316 | int len = array->length(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10317 | for (int i = 0; i < len; i += kCodeCacheEntrySize) { |
| 10318 | if (array->get(i + kCodeCacheEntryCodeOffset) == code) return i + 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10319 | } |
| 10320 | return -1; |
| 10321 | } |
| 10322 | |
| 10323 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10324 | void CodeCache::RemoveByIndex(Object* name, Code* code, int index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10325 | if (code->type() == Code::NORMAL) { |
| 10326 | DCHECK(!normal_type_cache()->IsUndefined()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10327 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10328 | DCHECK(cache->GetIndex(Name::cast(name), code->flags()) == index); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10329 | cache->RemoveByIndex(index); |
| 10330 | } else { |
| 10331 | FixedArray* array = default_cache(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10332 | DCHECK(array->length() >= index && array->get(index)->IsCode()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10333 | // Use null instead of undefined for deleted elements to distinguish |
| 10334 | // deleted elements from unused elements. This distinction is used |
| 10335 | // when looking up in the cache and when updating the cache. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10336 | DCHECK_EQ(1, kCodeCacheEntryCodeOffset - kCodeCacheEntryNameOffset); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10337 | array->set_null(index - 1); // Name. |
| 10338 | array->set_null(index); // Code. |
| 10339 | } |
| 10340 | } |
| 10341 | |
| 10342 | |
| 10343 | // The key in the code cache hash table consists of the property name and the |
| 10344 | // code object. The actual match is on the name and the code flags. If a key |
| 10345 | // is created using the flags and not a code object it can only be used for |
| 10346 | // lookup not to create a new entry. |
| 10347 | class CodeCacheHashTableKey : public HashTableKey { |
| 10348 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10349 | CodeCacheHashTableKey(Handle<Name> name, Code::Flags flags) |
| 10350 | : name_(name), flags_(flags), code_() { } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10351 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10352 | CodeCacheHashTableKey(Handle<Name> name, Handle<Code> code) |
| 10353 | : name_(name), flags_(code->flags()), code_(code) { } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10354 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10355 | bool IsMatch(Object* other) override { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10356 | if (!other->IsFixedArray()) return false; |
| 10357 | FixedArray* pair = FixedArray::cast(other); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10358 | Name* name = Name::cast(pair->get(0)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10359 | Code::Flags flags = Code::cast(pair->get(1))->flags(); |
| 10360 | if (flags != flags_) { |
| 10361 | return false; |
| 10362 | } |
| 10363 | return name_->Equals(name); |
| 10364 | } |
| 10365 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10366 | static uint32_t NameFlagsHashHelper(Name* name, Code::Flags flags) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10367 | return name->Hash() ^ flags; |
| 10368 | } |
| 10369 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10370 | uint32_t Hash() override { return NameFlagsHashHelper(*name_, flags_); } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10371 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10372 | uint32_t HashForObject(Object* obj) override { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10373 | FixedArray* pair = FixedArray::cast(obj); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10374 | Name* name = Name::cast(pair->get(0)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10375 | Code* code = Code::cast(pair->get(1)); |
| 10376 | return NameFlagsHashHelper(name, code->flags()); |
| 10377 | } |
| 10378 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10379 | MUST_USE_RESULT Handle<Object> AsHandle(Isolate* isolate) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10380 | Handle<Code> code = code_.ToHandleChecked(); |
| 10381 | Handle<FixedArray> pair = isolate->factory()->NewFixedArray(2); |
| 10382 | pair->set(0, *name_); |
| 10383 | pair->set(1, *code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10384 | return pair; |
| 10385 | } |
| 10386 | |
| 10387 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10388 | Handle<Name> name_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10389 | Code::Flags flags_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10390 | // TODO(jkummerow): We should be able to get by without this. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10391 | MaybeHandle<Code> code_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10392 | }; |
| 10393 | |
| 10394 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10395 | Object* CodeCacheHashTable::Lookup(Name* name, Code::Flags flags) { |
| 10396 | DisallowHeapAllocation no_alloc; |
| 10397 | CodeCacheHashTableKey key(handle(name), flags); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10398 | int entry = FindEntry(&key); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10399 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10400 | return get(EntryToIndex(entry) + 1); |
| 10401 | } |
| 10402 | |
| 10403 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10404 | Handle<CodeCacheHashTable> CodeCacheHashTable::Put( |
| 10405 | Handle<CodeCacheHashTable> cache, Handle<Name> name, Handle<Code> code) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10406 | CodeCacheHashTableKey key(name, code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10407 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10408 | Handle<CodeCacheHashTable> new_cache = EnsureCapacity(cache, 1, &key); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10409 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10410 | int entry = new_cache->FindInsertionEntry(key.Hash()); |
| 10411 | Handle<Object> k = key.AsHandle(cache->GetIsolate()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10412 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10413 | new_cache->set(EntryToIndex(entry), *k); |
| 10414 | new_cache->set(EntryToIndex(entry) + 1, *code); |
| 10415 | new_cache->ElementAdded(); |
| 10416 | return new_cache; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10417 | } |
| 10418 | |
| 10419 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10420 | int CodeCacheHashTable::GetIndex(Name* name, Code::Flags flags) { |
| 10421 | DisallowHeapAllocation no_alloc; |
| 10422 | CodeCacheHashTableKey key(handle(name), flags); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10423 | int entry = FindEntry(&key); |
| 10424 | return (entry == kNotFound) ? -1 : entry; |
| 10425 | } |
| 10426 | |
| 10427 | |
| 10428 | void CodeCacheHashTable::RemoveByIndex(int index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10429 | DCHECK(index >= 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10430 | Heap* heap = GetHeap(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10431 | set(EntryToIndex(index), heap->the_hole_value()); |
| 10432 | set(EntryToIndex(index) + 1, heap->the_hole_value()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10433 | ElementRemoved(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10434 | } |
| 10435 | |
| 10436 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10437 | void PolymorphicCodeCache::Update(Handle<PolymorphicCodeCache> code_cache, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10438 | MapHandleList* maps, |
| 10439 | Code::Flags flags, |
| 10440 | Handle<Code> code) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10441 | Isolate* isolate = code_cache->GetIsolate(); |
| 10442 | if (code_cache->cache()->IsUndefined()) { |
| 10443 | Handle<PolymorphicCodeCacheHashTable> result = |
| 10444 | PolymorphicCodeCacheHashTable::New( |
| 10445 | isolate, |
| 10446 | PolymorphicCodeCacheHashTable::kInitialSize); |
| 10447 | code_cache->set_cache(*result); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10448 | } else { |
| 10449 | // This entry shouldn't be contained in the cache yet. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10450 | DCHECK(PolymorphicCodeCacheHashTable::cast(code_cache->cache()) |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10451 | ->Lookup(maps, flags)->IsUndefined()); |
| 10452 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10453 | Handle<PolymorphicCodeCacheHashTable> hash_table = |
| 10454 | handle(PolymorphicCodeCacheHashTable::cast(code_cache->cache())); |
| 10455 | Handle<PolymorphicCodeCacheHashTable> new_cache = |
| 10456 | PolymorphicCodeCacheHashTable::Put(hash_table, maps, flags, code); |
| 10457 | code_cache->set_cache(*new_cache); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10458 | } |
| 10459 | |
| 10460 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10461 | Handle<Object> PolymorphicCodeCache::Lookup(MapHandleList* maps, |
| 10462 | Code::Flags flags) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10463 | if (!cache()->IsUndefined()) { |
| 10464 | PolymorphicCodeCacheHashTable* hash_table = |
| 10465 | PolymorphicCodeCacheHashTable::cast(cache()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10466 | return Handle<Object>(hash_table->Lookup(maps, flags), GetIsolate()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10467 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10468 | return GetIsolate()->factory()->undefined_value(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10469 | } |
| 10470 | } |
| 10471 | |
| 10472 | |
| 10473 | // Despite their name, object of this class are not stored in the actual |
| 10474 | // hash table; instead they're temporarily used for lookups. It is therefore |
| 10475 | // safe to have a weak (non-owning) pointer to a MapList as a member field. |
| 10476 | class PolymorphicCodeCacheHashTableKey : public HashTableKey { |
| 10477 | public: |
| 10478 | // Callers must ensure that |maps| outlives the newly constructed object. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10479 | PolymorphicCodeCacheHashTableKey(MapHandleList* maps, int code_flags) |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10480 | : maps_(maps), |
| 10481 | code_flags_(code_flags) {} |
| 10482 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10483 | bool IsMatch(Object* other) override { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10484 | MapHandleList other_maps(kDefaultListAllocationSize); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10485 | int other_flags; |
| 10486 | FromObject(other, &other_flags, &other_maps); |
| 10487 | if (code_flags_ != other_flags) return false; |
| 10488 | if (maps_->length() != other_maps.length()) return false; |
| 10489 | // Compare just the hashes first because it's faster. |
| 10490 | int this_hash = MapsHashHelper(maps_, code_flags_); |
| 10491 | int other_hash = MapsHashHelper(&other_maps, other_flags); |
| 10492 | if (this_hash != other_hash) return false; |
| 10493 | |
| 10494 | // Full comparison: for each map in maps_, look for an equivalent map in |
| 10495 | // other_maps. This implementation is slow, but probably good enough for |
| 10496 | // now because the lists are short (<= 4 elements currently). |
| 10497 | for (int i = 0; i < maps_->length(); ++i) { |
| 10498 | bool match_found = false; |
| 10499 | for (int j = 0; j < other_maps.length(); ++j) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10500 | if (*(maps_->at(i)) == *(other_maps.at(j))) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10501 | match_found = true; |
| 10502 | break; |
| 10503 | } |
| 10504 | } |
| 10505 | if (!match_found) return false; |
| 10506 | } |
| 10507 | return true; |
| 10508 | } |
| 10509 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10510 | static uint32_t MapsHashHelper(MapHandleList* maps, int code_flags) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10511 | uint32_t hash = code_flags; |
| 10512 | for (int i = 0; i < maps->length(); ++i) { |
| 10513 | hash ^= maps->at(i)->Hash(); |
| 10514 | } |
| 10515 | return hash; |
| 10516 | } |
| 10517 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10518 | uint32_t Hash() override { return MapsHashHelper(maps_, code_flags_); } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10519 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10520 | uint32_t HashForObject(Object* obj) override { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10521 | MapHandleList other_maps(kDefaultListAllocationSize); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10522 | int other_flags; |
| 10523 | FromObject(obj, &other_flags, &other_maps); |
| 10524 | return MapsHashHelper(&other_maps, other_flags); |
| 10525 | } |
| 10526 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10527 | MUST_USE_RESULT Handle<Object> AsHandle(Isolate* isolate) override { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10528 | // The maps in |maps_| must be copied to a newly allocated FixedArray, |
| 10529 | // both because the referenced MapList is short-lived, and because C++ |
| 10530 | // objects can't be stored in the heap anyway. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10531 | Handle<FixedArray> list = |
| 10532 | isolate->factory()->NewUninitializedFixedArray(maps_->length() + 1); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10533 | list->set(0, Smi::FromInt(code_flags_)); |
| 10534 | for (int i = 0; i < maps_->length(); ++i) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10535 | list->set(i + 1, *maps_->at(i)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10536 | } |
| 10537 | return list; |
| 10538 | } |
| 10539 | |
| 10540 | private: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10541 | static MapHandleList* FromObject(Object* obj, |
| 10542 | int* code_flags, |
| 10543 | MapHandleList* maps) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10544 | FixedArray* list = FixedArray::cast(obj); |
| 10545 | maps->Rewind(0); |
| 10546 | *code_flags = Smi::cast(list->get(0))->value(); |
| 10547 | for (int i = 1; i < list->length(); ++i) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10548 | maps->Add(Handle<Map>(Map::cast(list->get(i)))); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10549 | } |
| 10550 | return maps; |
| 10551 | } |
| 10552 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10553 | MapHandleList* maps_; // weak. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10554 | int code_flags_; |
| 10555 | static const int kDefaultListAllocationSize = kMaxKeyedPolymorphism + 1; |
| 10556 | }; |
| 10557 | |
| 10558 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10559 | Object* PolymorphicCodeCacheHashTable::Lookup(MapHandleList* maps, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10560 | int code_kind) { |
| 10561 | DisallowHeapAllocation no_alloc; |
| 10562 | PolymorphicCodeCacheHashTableKey key(maps, code_kind); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10563 | int entry = FindEntry(&key); |
| 10564 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
| 10565 | return get(EntryToIndex(entry) + 1); |
| 10566 | } |
| 10567 | |
| 10568 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10569 | Handle<PolymorphicCodeCacheHashTable> PolymorphicCodeCacheHashTable::Put( |
| 10570 | Handle<PolymorphicCodeCacheHashTable> hash_table, |
| 10571 | MapHandleList* maps, |
| 10572 | int code_kind, |
| 10573 | Handle<Code> code) { |
| 10574 | PolymorphicCodeCacheHashTableKey key(maps, code_kind); |
| 10575 | Handle<PolymorphicCodeCacheHashTable> cache = |
| 10576 | EnsureCapacity(hash_table, 1, &key); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10577 | int entry = cache->FindInsertionEntry(key.Hash()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10578 | |
| 10579 | Handle<Object> obj = key.AsHandle(hash_table->GetIsolate()); |
| 10580 | cache->set(EntryToIndex(entry), *obj); |
| 10581 | cache->set(EntryToIndex(entry) + 1, *code); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10582 | cache->ElementAdded(); |
| 10583 | return cache; |
| 10584 | } |
| 10585 | |
| 10586 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10587 | void FixedArray::Shrink(int new_length) { |
| 10588 | DCHECK(0 <= new_length && new_length <= length()); |
| 10589 | if (new_length < length()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10590 | GetHeap()->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10591 | this, length() - new_length); |
| 10592 | } |
| 10593 | } |
| 10594 | |
| 10595 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10596 | void FixedArray::CopyTo(int pos, FixedArray* dest, int dest_pos, int len) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10597 | DisallowHeapAllocation no_gc; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 10598 | WriteBarrierMode mode = dest->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10599 | for (int index = 0; index < len; index++) { |
| 10600 | dest->set(dest_pos+index, get(pos+index), mode); |
| 10601 | } |
| 10602 | } |
| 10603 | |
| 10604 | |
| 10605 | #ifdef DEBUG |
| 10606 | bool FixedArray::IsEqualTo(FixedArray* other) { |
| 10607 | if (length() != other->length()) return false; |
| 10608 | for (int i = 0 ; i < length(); ++i) { |
| 10609 | if (get(i) != other->get(i)) return false; |
| 10610 | } |
| 10611 | return true; |
| 10612 | } |
| 10613 | #endif |
| 10614 | |
| 10615 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10616 | // static |
| 10617 | void WeakFixedArray::Set(Handle<WeakFixedArray> array, int index, |
| 10618 | Handle<HeapObject> value) { |
| 10619 | DCHECK(array->IsEmptySlot(index)); // Don't overwrite anything. |
| 10620 | Handle<WeakCell> cell = |
| 10621 | value->IsMap() ? Map::WeakCellForMap(Handle<Map>::cast(value)) |
| 10622 | : array->GetIsolate()->factory()->NewWeakCell(value); |
| 10623 | Handle<FixedArray>::cast(array)->set(index + kFirstIndex, *cell); |
| 10624 | if (FLAG_trace_weak_arrays) { |
| 10625 | PrintF("[WeakFixedArray: storing at index %d ]\n", index); |
| 10626 | } |
| 10627 | array->set_last_used_index(index); |
| 10628 | } |
| 10629 | |
| 10630 | |
| 10631 | // static |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10632 | Handle<WeakFixedArray> WeakFixedArray::Add(Handle<Object> maybe_array, |
| 10633 | Handle<HeapObject> value, |
| 10634 | int* assigned_index) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10635 | Handle<WeakFixedArray> array = |
| 10636 | (maybe_array.is_null() || !maybe_array->IsWeakFixedArray()) |
| 10637 | ? Allocate(value->GetIsolate(), 1, Handle<WeakFixedArray>::null()) |
| 10638 | : Handle<WeakFixedArray>::cast(maybe_array); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10639 | // Try to store the new entry if there's room. Optimize for consecutive |
| 10640 | // accesses. |
| 10641 | int first_index = array->last_used_index(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10642 | int length = array->Length(); |
| 10643 | if (length > 0) { |
| 10644 | for (int i = first_index;;) { |
| 10645 | if (array->IsEmptySlot((i))) { |
| 10646 | WeakFixedArray::Set(array, i, value); |
| 10647 | if (assigned_index != NULL) *assigned_index = i; |
| 10648 | return array; |
| 10649 | } |
| 10650 | if (FLAG_trace_weak_arrays) { |
| 10651 | PrintF("[WeakFixedArray: searching for free slot]\n"); |
| 10652 | } |
| 10653 | i = (i + 1) % length; |
| 10654 | if (i == first_index) break; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10655 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10656 | } |
| 10657 | |
| 10658 | // No usable slot found, grow the array. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10659 | int new_length = length == 0 ? 1 : length + (length >> 1) + 4; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10660 | Handle<WeakFixedArray> new_array = |
| 10661 | Allocate(array->GetIsolate(), new_length, array); |
| 10662 | if (FLAG_trace_weak_arrays) { |
| 10663 | PrintF("[WeakFixedArray: growing to size %d ]\n", new_length); |
| 10664 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10665 | WeakFixedArray::Set(new_array, length, value); |
| 10666 | if (assigned_index != NULL) *assigned_index = length; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10667 | return new_array; |
| 10668 | } |
| 10669 | |
| 10670 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10671 | template <class CompactionCallback> |
| 10672 | void WeakFixedArray::Compact() { |
| 10673 | FixedArray* array = FixedArray::cast(this); |
| 10674 | int new_length = kFirstIndex; |
| 10675 | for (int i = kFirstIndex; i < array->length(); i++) { |
| 10676 | Object* element = array->get(i); |
| 10677 | if (element->IsSmi()) continue; |
| 10678 | if (WeakCell::cast(element)->cleared()) continue; |
| 10679 | Object* value = WeakCell::cast(element)->value(); |
| 10680 | CompactionCallback::Callback(value, i - kFirstIndex, |
| 10681 | new_length - kFirstIndex); |
| 10682 | array->set(new_length++, element); |
| 10683 | } |
| 10684 | array->Shrink(new_length); |
| 10685 | set_last_used_index(0); |
| 10686 | } |
| 10687 | |
| 10688 | |
| 10689 | void WeakFixedArray::Iterator::Reset(Object* maybe_array) { |
| 10690 | if (maybe_array->IsWeakFixedArray()) { |
| 10691 | list_ = WeakFixedArray::cast(maybe_array); |
| 10692 | index_ = 0; |
| 10693 | #ifdef DEBUG |
| 10694 | last_used_index_ = list_->last_used_index(); |
| 10695 | #endif // DEBUG |
| 10696 | } |
| 10697 | } |
| 10698 | |
| 10699 | |
| 10700 | void JSObject::PrototypeRegistryCompactionCallback::Callback(Object* value, |
| 10701 | int old_index, |
| 10702 | int new_index) { |
| 10703 | DCHECK(value->IsMap() && Map::cast(value)->is_prototype_map()); |
| 10704 | Map* map = Map::cast(value); |
| 10705 | DCHECK(map->prototype_info()->IsPrototypeInfo()); |
| 10706 | PrototypeInfo* proto_info = PrototypeInfo::cast(map->prototype_info()); |
| 10707 | DCHECK_EQ(old_index, proto_info->registry_slot()); |
| 10708 | proto_info->set_registry_slot(new_index); |
| 10709 | } |
| 10710 | |
| 10711 | |
| 10712 | template void WeakFixedArray::Compact<WeakFixedArray::NullCallback>(); |
| 10713 | template void |
| 10714 | WeakFixedArray::Compact<JSObject::PrototypeRegistryCompactionCallback>(); |
| 10715 | |
| 10716 | |
| 10717 | bool WeakFixedArray::Remove(Handle<HeapObject> value) { |
| 10718 | if (Length() == 0) return false; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10719 | // Optimize for the most recently added element to be removed again. |
| 10720 | int first_index = last_used_index(); |
| 10721 | for (int i = first_index;;) { |
| 10722 | if (Get(i) == *value) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10723 | Clear(i); |
| 10724 | // Users of WeakFixedArray should make sure that there are no duplicates. |
| 10725 | return true; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10726 | } |
| 10727 | i = (i + 1) % Length(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10728 | if (i == first_index) return false; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10729 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10730 | UNREACHABLE(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10731 | } |
| 10732 | |
| 10733 | |
| 10734 | // static |
| 10735 | Handle<WeakFixedArray> WeakFixedArray::Allocate( |
| 10736 | Isolate* isolate, int size, Handle<WeakFixedArray> initialize_from) { |
| 10737 | DCHECK(0 <= size); |
| 10738 | Handle<FixedArray> result = |
| 10739 | isolate->factory()->NewUninitializedFixedArray(size + kFirstIndex); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10740 | int index = 0; |
| 10741 | if (!initialize_from.is_null()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10742 | DCHECK(initialize_from->Length() <= size); |
| 10743 | Handle<FixedArray> raw_source = Handle<FixedArray>::cast(initialize_from); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10744 | // Copy the entries without compacting, since the PrototypeInfo relies on |
| 10745 | // the index of the entries not to change. |
| 10746 | while (index < raw_source->length()) { |
| 10747 | result->set(index, raw_source->get(index)); |
| 10748 | index++; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10749 | } |
| 10750 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10751 | while (index < result->length()) { |
| 10752 | result->set(index, Smi::FromInt(0)); |
| 10753 | index++; |
| 10754 | } |
| 10755 | return Handle<WeakFixedArray>::cast(result); |
| 10756 | } |
| 10757 | |
| 10758 | |
| 10759 | Handle<ArrayList> ArrayList::Add(Handle<ArrayList> array, Handle<Object> obj, |
| 10760 | AddMode mode) { |
| 10761 | int length = array->Length(); |
| 10762 | array = EnsureSpace(array, length + 1); |
| 10763 | if (mode == kReloadLengthAfterAllocation) { |
| 10764 | DCHECK(array->Length() <= length); |
| 10765 | length = array->Length(); |
| 10766 | } |
| 10767 | array->Set(length, *obj); |
| 10768 | array->SetLength(length + 1); |
| 10769 | return array; |
| 10770 | } |
| 10771 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10772 | Handle<ArrayList> ArrayList::Add(Handle<ArrayList> array, Handle<Object> obj1, |
| 10773 | Handle<Object> obj2, AddMode mode) { |
| 10774 | int length = array->Length(); |
| 10775 | array = EnsureSpace(array, length + 2); |
| 10776 | if (mode == kReloadLengthAfterAllocation) { |
| 10777 | length = array->Length(); |
| 10778 | } |
| 10779 | array->Set(length, *obj1); |
| 10780 | array->Set(length + 1, *obj2); |
| 10781 | array->SetLength(length + 2); |
| 10782 | return array; |
| 10783 | } |
| 10784 | |
| 10785 | |
| 10786 | bool ArrayList::IsFull() { |
| 10787 | int capacity = length(); |
| 10788 | return kFirstIndex + Length() == capacity; |
| 10789 | } |
| 10790 | |
| 10791 | |
| 10792 | Handle<ArrayList> ArrayList::EnsureSpace(Handle<ArrayList> array, int length) { |
| 10793 | int capacity = array->length(); |
| 10794 | bool empty = (capacity == 0); |
| 10795 | if (capacity < kFirstIndex + length) { |
| 10796 | Isolate* isolate = array->GetIsolate(); |
| 10797 | int new_capacity = kFirstIndex + length; |
| 10798 | new_capacity = new_capacity + Max(new_capacity / 2, 2); |
| 10799 | int grow_by = new_capacity - capacity; |
| 10800 | array = Handle<ArrayList>::cast( |
| 10801 | isolate->factory()->CopyFixedArrayAndGrow(array, grow_by)); |
| 10802 | if (empty) array->SetLength(0); |
| 10803 | } |
| 10804 | return array; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 10805 | } |
| 10806 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10807 | Handle<DescriptorArray> DescriptorArray::Allocate(Isolate* isolate, |
| 10808 | int number_of_descriptors, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 10809 | int slack, |
| 10810 | PretenureFlag pretenure) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10811 | DCHECK(0 <= number_of_descriptors); |
| 10812 | Factory* factory = isolate->factory(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10813 | // Do not use DescriptorArray::cast on incomplete object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10814 | int size = number_of_descriptors + slack; |
| 10815 | if (size == 0) return factory->empty_descriptor_array(); |
| 10816 | // Allocate the array of keys. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 10817 | Handle<FixedArray> result = |
| 10818 | factory->NewFixedArray(LengthFor(size), pretenure); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10819 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10820 | result->set(kDescriptorLengthIndex, Smi::FromInt(number_of_descriptors)); |
| 10821 | result->set(kEnumCacheIndex, Smi::FromInt(0)); |
| 10822 | return Handle<DescriptorArray>::cast(result); |
| 10823 | } |
| 10824 | |
| 10825 | |
| 10826 | void DescriptorArray::ClearEnumCache() { |
| 10827 | set(kEnumCacheIndex, Smi::FromInt(0)); |
| 10828 | } |
| 10829 | |
| 10830 | |
| 10831 | void DescriptorArray::Replace(int index, Descriptor* descriptor) { |
| 10832 | descriptor->SetSortedKeyIndex(GetSortedKeyIndex(index)); |
| 10833 | Set(index, descriptor); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10834 | } |
| 10835 | |
| 10836 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10837 | // static |
| 10838 | void DescriptorArray::SetEnumCache(Handle<DescriptorArray> descriptors, |
| 10839 | Isolate* isolate, |
| 10840 | Handle<FixedArray> new_cache, |
| 10841 | Handle<FixedArray> new_index_cache) { |
| 10842 | DCHECK(!descriptors->IsEmpty()); |
| 10843 | FixedArray* bridge_storage; |
| 10844 | bool needs_new_enum_cache = !descriptors->HasEnumCache(); |
| 10845 | if (needs_new_enum_cache) { |
| 10846 | bridge_storage = *isolate->factory()->NewFixedArray( |
| 10847 | DescriptorArray::kEnumCacheBridgeLength); |
| 10848 | } else { |
| 10849 | bridge_storage = FixedArray::cast(descriptors->get(kEnumCacheIndex)); |
| 10850 | } |
| 10851 | bridge_storage->set(kEnumCacheBridgeCacheIndex, *new_cache); |
| 10852 | bridge_storage->set(kEnumCacheBridgeIndicesCacheIndex, |
| 10853 | new_index_cache.is_null() ? Object::cast(Smi::FromInt(0)) |
| 10854 | : *new_index_cache); |
| 10855 | if (needs_new_enum_cache) { |
| 10856 | descriptors->set(kEnumCacheIndex, bridge_storage); |
| 10857 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10858 | } |
| 10859 | |
| 10860 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10861 | void DescriptorArray::CopyFrom(int index, DescriptorArray* src) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10862 | Object* value = src->GetValue(index); |
| 10863 | PropertyDetails details = src->GetDetails(index); |
| 10864 | Descriptor desc(handle(src->GetKey(index)), |
| 10865 | handle(value, src->GetIsolate()), |
| 10866 | details); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10867 | SetDescriptor(index, &desc); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10868 | } |
| 10869 | |
| 10870 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10871 | void DescriptorArray::Sort() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10872 | // In-place heap sort. |
| 10873 | int len = number_of_descriptors(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10874 | // Reset sorting since the descriptor array might contain invalid pointers. |
| 10875 | for (int i = 0; i < len; ++i) SetSortedKey(i, i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10876 | // Bottom-up max-heap construction. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10877 | // Index of the last node with children |
| 10878 | const int max_parent_index = (len / 2) - 1; |
| 10879 | for (int i = max_parent_index; i >= 0; --i) { |
| 10880 | int parent_index = i; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10881 | const uint32_t parent_hash = GetSortedKey(i)->Hash(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10882 | while (parent_index <= max_parent_index) { |
| 10883 | int child_index = 2 * parent_index + 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10884 | uint32_t child_hash = GetSortedKey(child_index)->Hash(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10885 | if (child_index + 1 < len) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10886 | uint32_t right_child_hash = GetSortedKey(child_index + 1)->Hash(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10887 | if (right_child_hash > child_hash) { |
| 10888 | child_index++; |
| 10889 | child_hash = right_child_hash; |
| 10890 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10891 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10892 | if (child_hash <= parent_hash) break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10893 | SwapSortedKeys(parent_index, child_index); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10894 | // Now element at child_index could be < its children. |
| 10895 | parent_index = child_index; // parent_hash remains correct. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10896 | } |
| 10897 | } |
| 10898 | |
| 10899 | // Extract elements and create sorted array. |
| 10900 | for (int i = len - 1; i > 0; --i) { |
| 10901 | // Put max element at the back of the array. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10902 | SwapSortedKeys(0, i); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10903 | // Shift down the new top element. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10904 | int parent_index = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10905 | const uint32_t parent_hash = GetSortedKey(parent_index)->Hash(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10906 | const int max_parent_index = (i / 2) - 1; |
| 10907 | while (parent_index <= max_parent_index) { |
| 10908 | int child_index = parent_index * 2 + 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10909 | uint32_t child_hash = GetSortedKey(child_index)->Hash(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10910 | if (child_index + 1 < i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10911 | uint32_t right_child_hash = GetSortedKey(child_index + 1)->Hash(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10912 | if (right_child_hash > child_hash) { |
| 10913 | child_index++; |
| 10914 | child_hash = right_child_hash; |
| 10915 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10916 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10917 | if (child_hash <= parent_hash) break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10918 | SwapSortedKeys(parent_index, child_index); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10919 | parent_index = child_index; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10920 | } |
| 10921 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10922 | DCHECK(IsSortedNoDuplicates()); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 10923 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10924 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 10925 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10926 | Handle<AccessorPair> AccessorPair::Copy(Handle<AccessorPair> pair) { |
| 10927 | Handle<AccessorPair> copy = pair->GetIsolate()->factory()->NewAccessorPair(); |
| 10928 | copy->set_getter(pair->getter()); |
| 10929 | copy->set_setter(pair->setter()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10930 | return copy; |
| 10931 | } |
| 10932 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 10933 | Handle<Object> AccessorPair::GetComponent(Handle<AccessorPair> accessor_pair, |
| 10934 | AccessorComponent component) { |
| 10935 | Object* accessor = accessor_pair->get(component); |
| 10936 | if (accessor->IsFunctionTemplateInfo()) { |
| 10937 | return ApiNatives::InstantiateFunction( |
| 10938 | handle(FunctionTemplateInfo::cast(accessor))) |
| 10939 | .ToHandleChecked(); |
| 10940 | } |
| 10941 | Isolate* isolate = accessor_pair->GetIsolate(); |
| 10942 | if (accessor->IsTheHole()) { |
| 10943 | return isolate->factory()->undefined_value(); |
| 10944 | } |
| 10945 | return handle(accessor, isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10946 | } |
| 10947 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10948 | Handle<DeoptimizationInputData> DeoptimizationInputData::New( |
| 10949 | Isolate* isolate, int deopt_entry_count, PretenureFlag pretenure) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10950 | return Handle<DeoptimizationInputData>::cast( |
| 10951 | isolate->factory()->NewFixedArray(LengthFor(deopt_entry_count), |
| 10952 | pretenure)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 10953 | } |
| 10954 | |
| 10955 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10956 | Handle<DeoptimizationOutputData> DeoptimizationOutputData::New( |
| 10957 | Isolate* isolate, |
| 10958 | int number_of_deopt_points, |
| 10959 | PretenureFlag pretenure) { |
| 10960 | Handle<FixedArray> result; |
| 10961 | if (number_of_deopt_points == 0) { |
| 10962 | result = isolate->factory()->empty_fixed_array(); |
| 10963 | } else { |
| 10964 | result = isolate->factory()->NewFixedArray( |
| 10965 | LengthOfFixedArray(number_of_deopt_points), pretenure); |
| 10966 | } |
| 10967 | return Handle<DeoptimizationOutputData>::cast(result); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 10968 | } |
| 10969 | |
| 10970 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10971 | // static |
| 10972 | Handle<LiteralsArray> LiteralsArray::New(Isolate* isolate, |
| 10973 | Handle<TypeFeedbackVector> vector, |
| 10974 | int number_of_literals, |
| 10975 | PretenureFlag pretenure) { |
| 10976 | Handle<FixedArray> literals = isolate->factory()->NewFixedArray( |
| 10977 | number_of_literals + kFirstLiteralIndex, pretenure); |
| 10978 | Handle<LiteralsArray> casted_literals = Handle<LiteralsArray>::cast(literals); |
| 10979 | casted_literals->set_feedback_vector(*vector); |
| 10980 | return casted_literals; |
| 10981 | } |
| 10982 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 10983 | int HandlerTable::LookupRange(int pc_offset, int* data_out, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10984 | CatchPrediction* prediction_out) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 10985 | int innermost_handler = -1; |
| 10986 | #ifdef DEBUG |
| 10987 | // Assuming that ranges are well nested, we don't need to track the innermost |
| 10988 | // offsets. This is just to verify that the table is actually well nested. |
| 10989 | int innermost_start = std::numeric_limits<int>::min(); |
| 10990 | int innermost_end = std::numeric_limits<int>::max(); |
| 10991 | #endif |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10992 | for (int i = 0; i < length(); i += kRangeEntrySize) { |
| 10993 | int start_offset = Smi::cast(get(i + kRangeStartIndex))->value(); |
| 10994 | int end_offset = Smi::cast(get(i + kRangeEndIndex))->value(); |
| 10995 | int handler_field = Smi::cast(get(i + kRangeHandlerIndex))->value(); |
| 10996 | int handler_offset = HandlerOffsetField::decode(handler_field); |
| 10997 | CatchPrediction prediction = HandlerPredictionField::decode(handler_field); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 10998 | int handler_data = Smi::cast(get(i + kRangeDataIndex))->value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10999 | if (pc_offset > start_offset && pc_offset <= end_offset) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 11000 | DCHECK_GE(start_offset, innermost_start); |
| 11001 | DCHECK_LT(end_offset, innermost_end); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11002 | innermost_handler = handler_offset; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 11003 | #ifdef DEBUG |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11004 | innermost_start = start_offset; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 11005 | innermost_end = end_offset; |
| 11006 | #endif |
| 11007 | if (data_out) *data_out = handler_data; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11008 | if (prediction_out) *prediction_out = prediction; |
| 11009 | } |
| 11010 | } |
| 11011 | return innermost_handler; |
| 11012 | } |
| 11013 | |
| 11014 | |
| 11015 | // TODO(turbofan): Make sure table is sorted and use binary search. |
| 11016 | int HandlerTable::LookupReturn(int pc_offset, CatchPrediction* prediction_out) { |
| 11017 | for (int i = 0; i < length(); i += kReturnEntrySize) { |
| 11018 | int return_offset = Smi::cast(get(i + kReturnOffsetIndex))->value(); |
| 11019 | int handler_field = Smi::cast(get(i + kReturnHandlerIndex))->value(); |
| 11020 | if (pc_offset == return_offset) { |
| 11021 | if (prediction_out) { |
| 11022 | *prediction_out = HandlerPredictionField::decode(handler_field); |
| 11023 | } |
| 11024 | return HandlerOffsetField::decode(handler_field); |
| 11025 | } |
| 11026 | } |
| 11027 | return -1; |
| 11028 | } |
| 11029 | |
| 11030 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11031 | #ifdef DEBUG |
| 11032 | bool DescriptorArray::IsEqualTo(DescriptorArray* other) { |
| 11033 | if (IsEmpty()) return other->IsEmpty(); |
| 11034 | if (other->IsEmpty()) return false; |
| 11035 | if (length() != other->length()) return false; |
| 11036 | for (int i = 0; i < length(); ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11037 | if (get(i) != other->get(i)) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11038 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11039 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11040 | } |
| 11041 | #endif |
| 11042 | |
| 11043 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11044 | bool String::LooksValid() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11045 | if (!GetIsolate()->heap()->Contains(this)) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11046 | return true; |
| 11047 | } |
| 11048 | |
| 11049 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11050 | // static |
| 11051 | MaybeHandle<String> Name::ToFunctionName(Handle<Name> name) { |
| 11052 | if (name->IsString()) return Handle<String>::cast(name); |
| 11053 | // ES6 section 9.2.11 SetFunctionName, step 4. |
| 11054 | Isolate* const isolate = name->GetIsolate(); |
| 11055 | Handle<Object> description(Handle<Symbol>::cast(name)->name(), isolate); |
| 11056 | if (description->IsUndefined()) return isolate->factory()->empty_string(); |
| 11057 | IncrementalStringBuilder builder(isolate); |
| 11058 | builder.AppendCharacter('['); |
| 11059 | builder.AppendString(Handle<String>::cast(description)); |
| 11060 | builder.AppendCharacter(']'); |
| 11061 | return builder.Finish(); |
| 11062 | } |
| 11063 | |
| 11064 | |
| 11065 | namespace { |
| 11066 | |
| 11067 | bool AreDigits(const uint8_t* s, int from, int to) { |
| 11068 | for (int i = from; i < to; i++) { |
| 11069 | if (s[i] < '0' || s[i] > '9') return false; |
| 11070 | } |
| 11071 | |
| 11072 | return true; |
| 11073 | } |
| 11074 | |
| 11075 | |
| 11076 | int ParseDecimalInteger(const uint8_t* s, int from, int to) { |
| 11077 | DCHECK(to - from < 10); // Overflow is not possible. |
| 11078 | DCHECK(from < to); |
| 11079 | int d = s[from] - '0'; |
| 11080 | |
| 11081 | for (int i = from + 1; i < to; i++) { |
| 11082 | d = 10 * d + (s[i] - '0'); |
| 11083 | } |
| 11084 | |
| 11085 | return d; |
| 11086 | } |
| 11087 | |
| 11088 | } // namespace |
| 11089 | |
| 11090 | |
| 11091 | // static |
| 11092 | Handle<Object> String::ToNumber(Handle<String> subject) { |
| 11093 | Isolate* const isolate = subject->GetIsolate(); |
| 11094 | |
| 11095 | // Flatten {subject} string first. |
| 11096 | subject = String::Flatten(subject); |
| 11097 | |
| 11098 | // Fast array index case. |
| 11099 | uint32_t index; |
| 11100 | if (subject->AsArrayIndex(&index)) { |
| 11101 | return isolate->factory()->NewNumberFromUint(index); |
| 11102 | } |
| 11103 | |
| 11104 | // Fast case: short integer or some sorts of junk values. |
| 11105 | if (subject->IsSeqOneByteString()) { |
| 11106 | int len = subject->length(); |
| 11107 | if (len == 0) return handle(Smi::FromInt(0), isolate); |
| 11108 | |
| 11109 | DisallowHeapAllocation no_gc; |
| 11110 | uint8_t const* data = Handle<SeqOneByteString>::cast(subject)->GetChars(); |
| 11111 | bool minus = (data[0] == '-'); |
| 11112 | int start_pos = (minus ? 1 : 0); |
| 11113 | |
| 11114 | if (start_pos == len) { |
| 11115 | return isolate->factory()->nan_value(); |
| 11116 | } else if (data[start_pos] > '9') { |
| 11117 | // Fast check for a junk value. A valid string may start from a |
| 11118 | // whitespace, a sign ('+' or '-'), the decimal point, a decimal digit |
| 11119 | // or the 'I' character ('Infinity'). All of that have codes not greater |
| 11120 | // than '9' except 'I' and . |
| 11121 | if (data[start_pos] != 'I' && data[start_pos] != 0xa0) { |
| 11122 | return isolate->factory()->nan_value(); |
| 11123 | } |
| 11124 | } else if (len - start_pos < 10 && AreDigits(data, start_pos, len)) { |
| 11125 | // The maximal/minimal smi has 10 digits. If the string has less digits |
| 11126 | // we know it will fit into the smi-data type. |
| 11127 | int d = ParseDecimalInteger(data, start_pos, len); |
| 11128 | if (minus) { |
| 11129 | if (d == 0) return isolate->factory()->minus_zero_value(); |
| 11130 | d = -d; |
| 11131 | } else if (!subject->HasHashCode() && len <= String::kMaxArrayIndexSize && |
| 11132 | (len == 1 || data[0] != '0')) { |
| 11133 | // String hash is not calculated yet but all the data are present. |
| 11134 | // Update the hash field to speed up sequential convertions. |
| 11135 | uint32_t hash = StringHasher::MakeArrayIndexHash(d, len); |
| 11136 | #ifdef DEBUG |
| 11137 | subject->Hash(); // Force hash calculation. |
| 11138 | DCHECK_EQ(static_cast<int>(subject->hash_field()), |
| 11139 | static_cast<int>(hash)); |
| 11140 | #endif |
| 11141 | subject->set_hash_field(hash); |
| 11142 | } |
| 11143 | return handle(Smi::FromInt(d), isolate); |
| 11144 | } |
| 11145 | } |
| 11146 | |
| 11147 | // Slower case. |
| 11148 | int flags = ALLOW_HEX | ALLOW_OCTAL | ALLOW_BINARY; |
| 11149 | return isolate->factory()->NewNumber( |
| 11150 | StringToDouble(isolate->unicode_cache(), subject, flags)); |
| 11151 | } |
| 11152 | |
| 11153 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11154 | String::FlatContent String::GetFlatContent() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11155 | DCHECK(!AllowHeapAllocation::IsAllowed()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11156 | int length = this->length(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11157 | StringShape shape(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11158 | String* string = this; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11159 | int offset = 0; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11160 | if (shape.representation_tag() == kConsStringTag) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11161 | ConsString* cons = ConsString::cast(string); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11162 | if (cons->second()->length() != 0) { |
| 11163 | return FlatContent(); |
| 11164 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11165 | string = cons->first(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11166 | shape = StringShape(string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11167 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11168 | if (shape.representation_tag() == kSlicedStringTag) { |
| 11169 | SlicedString* slice = SlicedString::cast(string); |
| 11170 | offset = slice->offset(); |
| 11171 | string = slice->parent(); |
| 11172 | shape = StringShape(string); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11173 | DCHECK(shape.representation_tag() != kConsStringTag && |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11174 | shape.representation_tag() != kSlicedStringTag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11175 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11176 | if (shape.encoding_tag() == kOneByteStringTag) { |
| 11177 | const uint8_t* start; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11178 | if (shape.representation_tag() == kSeqStringTag) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11179 | start = SeqOneByteString::cast(string)->GetChars(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11180 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11181 | start = ExternalOneByteString::cast(string)->GetChars(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11182 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11183 | return FlatContent(start + offset, length); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11184 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11185 | DCHECK(shape.encoding_tag() == kTwoByteStringTag); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11186 | const uc16* start; |
| 11187 | if (shape.representation_tag() == kSeqStringTag) { |
| 11188 | start = SeqTwoByteString::cast(string)->GetChars(); |
| 11189 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11190 | start = ExternalTwoByteString::cast(string)->GetChars(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11191 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11192 | return FlatContent(start + offset, length); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11193 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11194 | } |
| 11195 | |
| 11196 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11197 | base::SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls, |
| 11198 | RobustnessFlag robust_flag, |
| 11199 | int offset, int length, |
| 11200 | int* length_return) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11201 | if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11202 | return base::SmartArrayPointer<char>(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11203 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11204 | // Negative length means the to the end of the string. |
| 11205 | if (length < 0) length = kMaxInt - offset; |
| 11206 | |
| 11207 | // Compute the size of the UTF-8 string. Start at the specified offset. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11208 | StringCharacterStream stream(this, offset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11209 | int character_position = offset; |
| 11210 | int utf8_bytes = 0; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11211 | int last = unibrow::Utf16::kNoPreviousCharacter; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11212 | while (stream.HasMore() && character_position++ < offset + length) { |
| 11213 | uint16_t character = stream.GetNext(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11214 | utf8_bytes += unibrow::Utf8::Length(character, last); |
| 11215 | last = character; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11216 | } |
| 11217 | |
| 11218 | if (length_return) { |
| 11219 | *length_return = utf8_bytes; |
| 11220 | } |
| 11221 | |
| 11222 | char* result = NewArray<char>(utf8_bytes + 1); |
| 11223 | |
| 11224 | // Convert the UTF-16 string to a UTF-8 buffer. Start at the specified offset. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11225 | stream.Reset(this, offset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11226 | character_position = offset; |
| 11227 | int utf8_byte_position = 0; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11228 | last = unibrow::Utf16::kNoPreviousCharacter; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11229 | while (stream.HasMore() && character_position++ < offset + length) { |
| 11230 | uint16_t character = stream.GetNext(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11231 | if (allow_nulls == DISALLOW_NULLS && character == 0) { |
| 11232 | character = ' '; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11233 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11234 | utf8_byte_position += |
| 11235 | unibrow::Utf8::Encode(result + utf8_byte_position, character, last); |
| 11236 | last = character; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11237 | } |
| 11238 | result[utf8_byte_position] = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11239 | return base::SmartArrayPointer<char>(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11240 | } |
| 11241 | |
| 11242 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11243 | base::SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls, |
| 11244 | RobustnessFlag robust_flag, |
| 11245 | int* length_return) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11246 | return ToCString(allow_nulls, robust_flag, 0, -1, length_return); |
| 11247 | } |
| 11248 | |
| 11249 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11250 | const uc16* String::GetTwoByteData(unsigned start) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11251 | DCHECK(!IsOneByteRepresentationUnderneath()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11252 | switch (StringShape(this).representation_tag()) { |
| 11253 | case kSeqStringTag: |
| 11254 | return SeqTwoByteString::cast(this)->SeqTwoByteStringGetData(start); |
| 11255 | case kExternalStringTag: |
| 11256 | return ExternalTwoByteString::cast(this)-> |
| 11257 | ExternalTwoByteStringGetData(start); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11258 | case kSlicedStringTag: { |
| 11259 | SlicedString* slice = SlicedString::cast(this); |
| 11260 | return slice->parent()->GetTwoByteData(start + slice->offset()); |
| 11261 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11262 | case kConsStringTag: |
| 11263 | UNREACHABLE(); |
| 11264 | return NULL; |
| 11265 | } |
| 11266 | UNREACHABLE(); |
| 11267 | return NULL; |
| 11268 | } |
| 11269 | |
| 11270 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11271 | base::SmartArrayPointer<uc16> String::ToWideCString( |
| 11272 | RobustnessFlag robust_flag) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11273 | if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11274 | return base::SmartArrayPointer<uc16>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11275 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11276 | StringCharacterStream stream(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11277 | |
| 11278 | uc16* result = NewArray<uc16>(length() + 1); |
| 11279 | |
| 11280 | int i = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11281 | while (stream.HasMore()) { |
| 11282 | uint16_t character = stream.GetNext(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11283 | result[i++] = character; |
| 11284 | } |
| 11285 | result[i] = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11286 | return base::SmartArrayPointer<uc16>(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11287 | } |
| 11288 | |
| 11289 | |
| 11290 | const uc16* SeqTwoByteString::SeqTwoByteStringGetData(unsigned start) { |
| 11291 | return reinterpret_cast<uc16*>( |
| 11292 | reinterpret_cast<char*>(this) - kHeapObjectTag + kHeaderSize) + start; |
| 11293 | } |
| 11294 | |
| 11295 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11296 | void Relocatable::PostGarbageCollectionProcessing(Isolate* isolate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11297 | Relocatable* current = isolate->relocatable_top(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11298 | while (current != NULL) { |
| 11299 | current->PostGarbageCollection(); |
| 11300 | current = current->prev_; |
| 11301 | } |
| 11302 | } |
| 11303 | |
| 11304 | |
| 11305 | // Reserve space for statics needing saving and restoring. |
| 11306 | int Relocatable::ArchiveSpacePerThread() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11307 | return sizeof(Relocatable*); // NOLINT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11308 | } |
| 11309 | |
| 11310 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11311 | // Archive statics that are thread-local. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 11312 | char* Relocatable::ArchiveState(Isolate* isolate, char* to) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11313 | *reinterpret_cast<Relocatable**>(to) = isolate->relocatable_top(); |
| 11314 | isolate->set_relocatable_top(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11315 | return to + ArchiveSpacePerThread(); |
| 11316 | } |
| 11317 | |
| 11318 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11319 | // Restore statics that are thread-local. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 11320 | char* Relocatable::RestoreState(Isolate* isolate, char* from) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11321 | isolate->set_relocatable_top(*reinterpret_cast<Relocatable**>(from)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11322 | return from + ArchiveSpacePerThread(); |
| 11323 | } |
| 11324 | |
| 11325 | |
| 11326 | char* Relocatable::Iterate(ObjectVisitor* v, char* thread_storage) { |
| 11327 | Relocatable* top = *reinterpret_cast<Relocatable**>(thread_storage); |
| 11328 | Iterate(v, top); |
| 11329 | return thread_storage + ArchiveSpacePerThread(); |
| 11330 | } |
| 11331 | |
| 11332 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11333 | void Relocatable::Iterate(Isolate* isolate, ObjectVisitor* v) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11334 | Iterate(v, isolate->relocatable_top()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11335 | } |
| 11336 | |
| 11337 | |
| 11338 | void Relocatable::Iterate(ObjectVisitor* v, Relocatable* top) { |
| 11339 | Relocatable* current = top; |
| 11340 | while (current != NULL) { |
| 11341 | current->IterateInstance(v); |
| 11342 | current = current->prev_; |
| 11343 | } |
| 11344 | } |
| 11345 | |
| 11346 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11347 | FlatStringReader::FlatStringReader(Isolate* isolate, Handle<String> str) |
| 11348 | : Relocatable(isolate), |
| 11349 | str_(str.location()), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11350 | length_(str->length()) { |
| 11351 | PostGarbageCollection(); |
| 11352 | } |
| 11353 | |
| 11354 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11355 | FlatStringReader::FlatStringReader(Isolate* isolate, Vector<const char> input) |
| 11356 | : Relocatable(isolate), |
| 11357 | str_(0), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11358 | is_one_byte_(true), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11359 | length_(input.length()), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11360 | start_(input.start()) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11361 | |
| 11362 | |
| 11363 | void FlatStringReader::PostGarbageCollection() { |
| 11364 | if (str_ == NULL) return; |
| 11365 | Handle<String> str(str_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11366 | DCHECK(str->IsFlat()); |
| 11367 | DisallowHeapAllocation no_gc; |
| 11368 | // This does not actually prevent the vector from being relocated later. |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11369 | String::FlatContent content = str->GetFlatContent(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11370 | DCHECK(content.IsFlat()); |
| 11371 | is_one_byte_ = content.IsOneByte(); |
| 11372 | if (is_one_byte_) { |
| 11373 | start_ = content.ToOneByteVector().start(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11374 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11375 | start_ = content.ToUC16Vector().start(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11376 | } |
| 11377 | } |
| 11378 | |
| 11379 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11380 | void ConsStringIterator::Initialize(ConsString* cons_string, int offset) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11381 | DCHECK(cons_string != NULL); |
| 11382 | root_ = cons_string; |
| 11383 | consumed_ = offset; |
| 11384 | // Force stack blown condition to trigger restart. |
| 11385 | depth_ = 1; |
| 11386 | maximum_depth_ = kStackSize + depth_; |
| 11387 | DCHECK(StackBlown()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11388 | } |
| 11389 | |
| 11390 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11391 | String* ConsStringIterator::Continue(int* offset_out) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11392 | DCHECK(depth_ != 0); |
| 11393 | DCHECK_EQ(0, *offset_out); |
| 11394 | bool blew_stack = StackBlown(); |
| 11395 | String* string = NULL; |
| 11396 | // Get the next leaf if there is one. |
| 11397 | if (!blew_stack) string = NextLeaf(&blew_stack); |
| 11398 | // Restart search from root. |
| 11399 | if (blew_stack) { |
| 11400 | DCHECK(string == NULL); |
| 11401 | string = Search(offset_out); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11402 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11403 | // Ensure future calls return null immediately. |
| 11404 | if (string == NULL) Reset(NULL); |
| 11405 | return string; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11406 | } |
| 11407 | |
| 11408 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11409 | String* ConsStringIterator::Search(int* offset_out) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11410 | ConsString* cons_string = root_; |
| 11411 | // Reset the stack, pushing the root string. |
| 11412 | depth_ = 1; |
| 11413 | maximum_depth_ = 1; |
| 11414 | frames_[0] = cons_string; |
| 11415 | const int consumed = consumed_; |
| 11416 | int offset = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11417 | while (true) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11418 | // Loop until the string is found which contains the target offset. |
| 11419 | String* string = cons_string->first(); |
| 11420 | int length = string->length(); |
| 11421 | int32_t type; |
| 11422 | if (consumed < offset + length) { |
| 11423 | // Target offset is in the left branch. |
| 11424 | // Keep going if we're still in a ConString. |
| 11425 | type = string->map()->instance_type(); |
| 11426 | if ((type & kStringRepresentationMask) == kConsStringTag) { |
| 11427 | cons_string = ConsString::cast(string); |
| 11428 | PushLeft(cons_string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11429 | continue; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11430 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11431 | // Tell the stack we're done descending. |
| 11432 | AdjustMaximumDepth(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11433 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11434 | // Descend right. |
| 11435 | // Update progress through the string. |
| 11436 | offset += length; |
| 11437 | // Keep going if we're still in a ConString. |
| 11438 | string = cons_string->second(); |
| 11439 | type = string->map()->instance_type(); |
| 11440 | if ((type & kStringRepresentationMask) == kConsStringTag) { |
| 11441 | cons_string = ConsString::cast(string); |
| 11442 | PushRight(cons_string); |
| 11443 | continue; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11444 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11445 | // Need this to be updated for the current string. |
| 11446 | length = string->length(); |
| 11447 | // Account for the possibility of an empty right leaf. |
| 11448 | // This happens only if we have asked for an offset outside the string. |
| 11449 | if (length == 0) { |
| 11450 | // Reset so future operations will return null immediately. |
| 11451 | Reset(NULL); |
| 11452 | return NULL; |
| 11453 | } |
| 11454 | // Tell the stack we're done descending. |
| 11455 | AdjustMaximumDepth(); |
| 11456 | // Pop stack so next iteration is in correct place. |
| 11457 | Pop(); |
| 11458 | } |
| 11459 | DCHECK(length != 0); |
| 11460 | // Adjust return values and exit. |
| 11461 | consumed_ = offset + length; |
| 11462 | *offset_out = consumed - offset; |
| 11463 | return string; |
| 11464 | } |
| 11465 | UNREACHABLE(); |
| 11466 | return NULL; |
| 11467 | } |
| 11468 | |
| 11469 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11470 | String* ConsStringIterator::NextLeaf(bool* blew_stack) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11471 | while (true) { |
| 11472 | // Tree traversal complete. |
| 11473 | if (depth_ == 0) { |
| 11474 | *blew_stack = false; |
| 11475 | return NULL; |
| 11476 | } |
| 11477 | // We've lost track of higher nodes. |
| 11478 | if (StackBlown()) { |
| 11479 | *blew_stack = true; |
| 11480 | return NULL; |
| 11481 | } |
| 11482 | // Go right. |
| 11483 | ConsString* cons_string = frames_[OffsetForDepth(depth_ - 1)]; |
| 11484 | String* string = cons_string->second(); |
| 11485 | int32_t type = string->map()->instance_type(); |
| 11486 | if ((type & kStringRepresentationMask) != kConsStringTag) { |
| 11487 | // Pop stack so next iteration is in correct place. |
| 11488 | Pop(); |
| 11489 | int length = string->length(); |
| 11490 | // Could be a flattened ConsString. |
| 11491 | if (length == 0) continue; |
| 11492 | consumed_ += length; |
| 11493 | return string; |
| 11494 | } |
| 11495 | cons_string = ConsString::cast(string); |
| 11496 | PushRight(cons_string); |
| 11497 | // Need to traverse all the way left. |
| 11498 | while (true) { |
| 11499 | // Continue left. |
| 11500 | string = cons_string->first(); |
| 11501 | type = string->map()->instance_type(); |
| 11502 | if ((type & kStringRepresentationMask) != kConsStringTag) { |
| 11503 | AdjustMaximumDepth(); |
| 11504 | int length = string->length(); |
| 11505 | DCHECK(length != 0); |
| 11506 | consumed_ += length; |
| 11507 | return string; |
| 11508 | } |
| 11509 | cons_string = ConsString::cast(string); |
| 11510 | PushLeft(cons_string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11511 | } |
| 11512 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11513 | UNREACHABLE(); |
| 11514 | return NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11515 | } |
| 11516 | |
| 11517 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11518 | uint16_t ConsString::ConsStringGet(int index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11519 | DCHECK(index >= 0 && index < this->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11520 | |
| 11521 | // Check for a flattened cons string |
| 11522 | if (second()->length() == 0) { |
| 11523 | String* left = first(); |
| 11524 | return left->Get(index); |
| 11525 | } |
| 11526 | |
| 11527 | String* string = String::cast(this); |
| 11528 | |
| 11529 | while (true) { |
| 11530 | if (StringShape(string).IsCons()) { |
| 11531 | ConsString* cons_string = ConsString::cast(string); |
| 11532 | String* left = cons_string->first(); |
| 11533 | if (left->length() > index) { |
| 11534 | string = left; |
| 11535 | } else { |
| 11536 | index -= left->length(); |
| 11537 | string = cons_string->second(); |
| 11538 | } |
| 11539 | } else { |
| 11540 | return string->Get(index); |
| 11541 | } |
| 11542 | } |
| 11543 | |
| 11544 | UNREACHABLE(); |
| 11545 | return 0; |
| 11546 | } |
| 11547 | |
| 11548 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11549 | uint16_t SlicedString::SlicedStringGet(int index) { |
| 11550 | return parent()->Get(offset() + index); |
| 11551 | } |
| 11552 | |
| 11553 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11554 | template <typename sinkchar> |
| 11555 | void String::WriteToFlat(String* src, |
| 11556 | sinkchar* sink, |
| 11557 | int f, |
| 11558 | int t) { |
| 11559 | String* source = src; |
| 11560 | int from = f; |
| 11561 | int to = t; |
| 11562 | while (true) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11563 | DCHECK(0 <= from && from <= to && to <= source->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11564 | switch (StringShape(source).full_representation_tag()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11565 | case kOneByteStringTag | kExternalStringTag: { |
| 11566 | CopyChars(sink, ExternalOneByteString::cast(source)->GetChars() + from, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11567 | to - from); |
| 11568 | return; |
| 11569 | } |
| 11570 | case kTwoByteStringTag | kExternalStringTag: { |
| 11571 | const uc16* data = |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11572 | ExternalTwoByteString::cast(source)->GetChars(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11573 | CopyChars(sink, |
| 11574 | data + from, |
| 11575 | to - from); |
| 11576 | return; |
| 11577 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11578 | case kOneByteStringTag | kSeqStringTag: { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11579 | CopyChars(sink, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11580 | SeqOneByteString::cast(source)->GetChars() + from, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11581 | to - from); |
| 11582 | return; |
| 11583 | } |
| 11584 | case kTwoByteStringTag | kSeqStringTag: { |
| 11585 | CopyChars(sink, |
| 11586 | SeqTwoByteString::cast(source)->GetChars() + from, |
| 11587 | to - from); |
| 11588 | return; |
| 11589 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11590 | case kOneByteStringTag | kConsStringTag: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11591 | case kTwoByteStringTag | kConsStringTag: { |
| 11592 | ConsString* cons_string = ConsString::cast(source); |
| 11593 | String* first = cons_string->first(); |
| 11594 | int boundary = first->length(); |
| 11595 | if (to - boundary >= boundary - from) { |
| 11596 | // Right hand side is longer. Recurse over left. |
| 11597 | if (from < boundary) { |
| 11598 | WriteToFlat(first, sink, from, boundary); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 11599 | if (from == 0 && cons_string->second() == first) { |
| 11600 | CopyChars(sink + boundary, sink, boundary); |
| 11601 | return; |
| 11602 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11603 | sink += boundary - from; |
| 11604 | from = 0; |
| 11605 | } else { |
| 11606 | from -= boundary; |
| 11607 | } |
| 11608 | to -= boundary; |
| 11609 | source = cons_string->second(); |
| 11610 | } else { |
| 11611 | // Left hand side is longer. Recurse over right. |
| 11612 | if (to > boundary) { |
| 11613 | String* second = cons_string->second(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11614 | // When repeatedly appending to a string, we get a cons string that |
| 11615 | // is unbalanced to the left, a list, essentially. We inline the |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11616 | // common case of sequential one-byte right child. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11617 | if (to - boundary == 1) { |
| 11618 | sink[boundary - from] = static_cast<sinkchar>(second->Get(0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11619 | } else if (second->IsSeqOneByteString()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11620 | CopyChars(sink + boundary - from, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11621 | SeqOneByteString::cast(second)->GetChars(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11622 | to - boundary); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11623 | } else { |
| 11624 | WriteToFlat(second, |
| 11625 | sink + boundary - from, |
| 11626 | 0, |
| 11627 | to - boundary); |
| 11628 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11629 | to = boundary; |
| 11630 | } |
| 11631 | source = first; |
| 11632 | } |
| 11633 | break; |
| 11634 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11635 | case kOneByteStringTag | kSlicedStringTag: |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11636 | case kTwoByteStringTag | kSlicedStringTag: { |
| 11637 | SlicedString* slice = SlicedString::cast(source); |
| 11638 | unsigned offset = slice->offset(); |
| 11639 | WriteToFlat(slice->parent(), sink, from + offset, to + offset); |
| 11640 | return; |
| 11641 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11642 | } |
| 11643 | } |
| 11644 | } |
| 11645 | |
| 11646 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11647 | |
| 11648 | template <typename SourceChar> |
| 11649 | static void CalculateLineEndsImpl(Isolate* isolate, |
| 11650 | List<int>* line_ends, |
| 11651 | Vector<const SourceChar> src, |
| 11652 | bool include_ending_line) { |
| 11653 | const int src_len = src.length(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11654 | UnicodeCache* cache = isolate->unicode_cache(); |
| 11655 | for (int i = 0; i < src_len - 1; i++) { |
| 11656 | SourceChar current = src[i]; |
| 11657 | SourceChar next = src[i + 1]; |
| 11658 | if (cache->IsLineTerminatorSequence(current, next)) line_ends->Add(i); |
| 11659 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11660 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11661 | if (src_len > 0 && cache->IsLineTerminatorSequence(src[src_len - 1], 0)) { |
| 11662 | line_ends->Add(src_len - 1); |
| 11663 | } |
| 11664 | if (include_ending_line) { |
| 11665 | // Include one character beyond the end of script. The rewriter uses that |
| 11666 | // position for the implicit return statement. |
| 11667 | line_ends->Add(src_len); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11668 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11669 | } |
| 11670 | |
| 11671 | |
| 11672 | Handle<FixedArray> String::CalculateLineEnds(Handle<String> src, |
| 11673 | bool include_ending_line) { |
| 11674 | src = Flatten(src); |
| 11675 | // Rough estimate of line count based on a roughly estimated average |
| 11676 | // length of (unpacked) code. |
| 11677 | int line_count_estimate = src->length() >> 4; |
| 11678 | List<int> line_ends(line_count_estimate); |
| 11679 | Isolate* isolate = src->GetIsolate(); |
| 11680 | { DisallowHeapAllocation no_allocation; // ensure vectors stay valid. |
| 11681 | // Dispatch on type of strings. |
| 11682 | String::FlatContent content = src->GetFlatContent(); |
| 11683 | DCHECK(content.IsFlat()); |
| 11684 | if (content.IsOneByte()) { |
| 11685 | CalculateLineEndsImpl(isolate, |
| 11686 | &line_ends, |
| 11687 | content.ToOneByteVector(), |
| 11688 | include_ending_line); |
| 11689 | } else { |
| 11690 | CalculateLineEndsImpl(isolate, |
| 11691 | &line_ends, |
| 11692 | content.ToUC16Vector(), |
| 11693 | include_ending_line); |
| 11694 | } |
| 11695 | } |
| 11696 | int line_count = line_ends.length(); |
| 11697 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(line_count); |
| 11698 | for (int i = 0; i < line_count; i++) { |
| 11699 | array->set(i, Smi::FromInt(line_ends[i])); |
| 11700 | } |
| 11701 | return array; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11702 | } |
| 11703 | |
| 11704 | |
| 11705 | // Compares the contents of two strings by reading and comparing |
| 11706 | // int-sized blocks of characters. |
| 11707 | template <typename Char> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11708 | static inline bool CompareRawStringContents(const Char* const a, |
| 11709 | const Char* const b, |
| 11710 | int length) { |
| 11711 | return CompareChars(a, b, length) == 0; |
| 11712 | } |
| 11713 | |
| 11714 | |
| 11715 | template<typename Chars1, typename Chars2> |
| 11716 | class RawStringComparator : public AllStatic { |
| 11717 | public: |
| 11718 | static inline bool compare(const Chars1* a, const Chars2* b, int len) { |
| 11719 | DCHECK(sizeof(Chars1) != sizeof(Chars2)); |
| 11720 | for (int i = 0; i < len; i++) { |
| 11721 | if (a[i] != b[i]) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11722 | return false; |
| 11723 | } |
| 11724 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11725 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11726 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11727 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11728 | |
| 11729 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11730 | template<> |
| 11731 | class RawStringComparator<uint16_t, uint16_t> { |
| 11732 | public: |
| 11733 | static inline bool compare(const uint16_t* a, const uint16_t* b, int len) { |
| 11734 | return CompareRawStringContents(a, b, len); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11735 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11736 | }; |
| 11737 | |
| 11738 | |
| 11739 | template<> |
| 11740 | class RawStringComparator<uint8_t, uint8_t> { |
| 11741 | public: |
| 11742 | static inline bool compare(const uint8_t* a, const uint8_t* b, int len) { |
| 11743 | return CompareRawStringContents(a, b, len); |
| 11744 | } |
| 11745 | }; |
| 11746 | |
| 11747 | |
| 11748 | class StringComparator { |
| 11749 | class State { |
| 11750 | public: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11751 | State() : is_one_byte_(true), length_(0), buffer8_(NULL) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11752 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11753 | void Init(String* string) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11754 | ConsString* cons_string = String::VisitFlat(this, string); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11755 | iter_.Reset(cons_string); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11756 | if (cons_string != NULL) { |
| 11757 | int offset; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11758 | string = iter_.Next(&offset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11759 | String::VisitFlat(this, string, offset); |
| 11760 | } |
| 11761 | } |
| 11762 | |
| 11763 | inline void VisitOneByteString(const uint8_t* chars, int length) { |
| 11764 | is_one_byte_ = true; |
| 11765 | buffer8_ = chars; |
| 11766 | length_ = length; |
| 11767 | } |
| 11768 | |
| 11769 | inline void VisitTwoByteString(const uint16_t* chars, int length) { |
| 11770 | is_one_byte_ = false; |
| 11771 | buffer16_ = chars; |
| 11772 | length_ = length; |
| 11773 | } |
| 11774 | |
| 11775 | void Advance(int consumed) { |
| 11776 | DCHECK(consumed <= length_); |
| 11777 | // Still in buffer. |
| 11778 | if (length_ != consumed) { |
| 11779 | if (is_one_byte_) { |
| 11780 | buffer8_ += consumed; |
| 11781 | } else { |
| 11782 | buffer16_ += consumed; |
| 11783 | } |
| 11784 | length_ -= consumed; |
| 11785 | return; |
| 11786 | } |
| 11787 | // Advance state. |
| 11788 | int offset; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11789 | String* next = iter_.Next(&offset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11790 | DCHECK_EQ(0, offset); |
| 11791 | DCHECK(next != NULL); |
| 11792 | String::VisitFlat(this, next); |
| 11793 | } |
| 11794 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11795 | ConsStringIterator iter_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11796 | bool is_one_byte_; |
| 11797 | int length_; |
| 11798 | union { |
| 11799 | const uint8_t* buffer8_; |
| 11800 | const uint16_t* buffer16_; |
| 11801 | }; |
| 11802 | |
| 11803 | private: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11804 | DISALLOW_COPY_AND_ASSIGN(State); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11805 | }; |
| 11806 | |
| 11807 | public: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11808 | inline StringComparator() {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11809 | |
| 11810 | template<typename Chars1, typename Chars2> |
| 11811 | static inline bool Equals(State* state_1, State* state_2, int to_check) { |
| 11812 | const Chars1* a = reinterpret_cast<const Chars1*>(state_1->buffer8_); |
| 11813 | const Chars2* b = reinterpret_cast<const Chars2*>(state_2->buffer8_); |
| 11814 | return RawStringComparator<Chars1, Chars2>::compare(a, b, to_check); |
| 11815 | } |
| 11816 | |
| 11817 | bool Equals(String* string_1, String* string_2) { |
| 11818 | int length = string_1->length(); |
| 11819 | state_1_.Init(string_1); |
| 11820 | state_2_.Init(string_2); |
| 11821 | while (true) { |
| 11822 | int to_check = Min(state_1_.length_, state_2_.length_); |
| 11823 | DCHECK(to_check > 0 && to_check <= length); |
| 11824 | bool is_equal; |
| 11825 | if (state_1_.is_one_byte_) { |
| 11826 | if (state_2_.is_one_byte_) { |
| 11827 | is_equal = Equals<uint8_t, uint8_t>(&state_1_, &state_2_, to_check); |
| 11828 | } else { |
| 11829 | is_equal = Equals<uint8_t, uint16_t>(&state_1_, &state_2_, to_check); |
| 11830 | } |
| 11831 | } else { |
| 11832 | if (state_2_.is_one_byte_) { |
| 11833 | is_equal = Equals<uint16_t, uint8_t>(&state_1_, &state_2_, to_check); |
| 11834 | } else { |
| 11835 | is_equal = Equals<uint16_t, uint16_t>(&state_1_, &state_2_, to_check); |
| 11836 | } |
| 11837 | } |
| 11838 | // Looping done. |
| 11839 | if (!is_equal) return false; |
| 11840 | length -= to_check; |
| 11841 | // Exit condition. Strings are equal. |
| 11842 | if (length == 0) return true; |
| 11843 | state_1_.Advance(to_check); |
| 11844 | state_2_.Advance(to_check); |
| 11845 | } |
| 11846 | } |
| 11847 | |
| 11848 | private: |
| 11849 | State state_1_; |
| 11850 | State state_2_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11851 | |
| 11852 | DISALLOW_COPY_AND_ASSIGN(StringComparator); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11853 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11854 | |
| 11855 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11856 | bool String::SlowEquals(String* other) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11857 | DisallowHeapAllocation no_gc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11858 | // Fast check: negative check with lengths. |
| 11859 | int len = length(); |
| 11860 | if (len != other->length()) return false; |
| 11861 | if (len == 0) return true; |
| 11862 | |
| 11863 | // Fast check: if hash code is computed for both strings |
| 11864 | // a fast negative check can be performed. |
| 11865 | if (HasHashCode() && other->HasHashCode()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11866 | #ifdef ENABLE_SLOW_DCHECKS |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 11867 | if (FLAG_enable_slow_asserts) { |
| 11868 | if (Hash() != other->Hash()) { |
| 11869 | bool found_difference = false; |
| 11870 | for (int i = 0; i < len; i++) { |
| 11871 | if (Get(i) != other->Get(i)) { |
| 11872 | found_difference = true; |
| 11873 | break; |
| 11874 | } |
| 11875 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11876 | DCHECK(found_difference); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 11877 | } |
| 11878 | } |
| 11879 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11880 | if (Hash() != other->Hash()) return false; |
| 11881 | } |
| 11882 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 11883 | // We know the strings are both non-empty. Compare the first chars |
| 11884 | // before we try to flatten the strings. |
| 11885 | if (this->Get(0) != other->Get(0)) return false; |
| 11886 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11887 | if (IsSeqOneByteString() && other->IsSeqOneByteString()) { |
| 11888 | const uint8_t* str1 = SeqOneByteString::cast(this)->GetChars(); |
| 11889 | const uint8_t* str2 = SeqOneByteString::cast(other)->GetChars(); |
| 11890 | return CompareRawStringContents(str1, str2, len); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11891 | } |
| 11892 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 11893 | StringComparator comparator; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11894 | return comparator.Equals(this, other); |
| 11895 | } |
| 11896 | |
| 11897 | |
| 11898 | bool String::SlowEquals(Handle<String> one, Handle<String> two) { |
| 11899 | // Fast check: negative check with lengths. |
| 11900 | int one_length = one->length(); |
| 11901 | if (one_length != two->length()) return false; |
| 11902 | if (one_length == 0) return true; |
| 11903 | |
| 11904 | // Fast check: if hash code is computed for both strings |
| 11905 | // a fast negative check can be performed. |
| 11906 | if (one->HasHashCode() && two->HasHashCode()) { |
| 11907 | #ifdef ENABLE_SLOW_DCHECKS |
| 11908 | if (FLAG_enable_slow_asserts) { |
| 11909 | if (one->Hash() != two->Hash()) { |
| 11910 | bool found_difference = false; |
| 11911 | for (int i = 0; i < one_length; i++) { |
| 11912 | if (one->Get(i) != two->Get(i)) { |
| 11913 | found_difference = true; |
| 11914 | break; |
| 11915 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11916 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11917 | DCHECK(found_difference); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11918 | } |
| 11919 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11920 | #endif |
| 11921 | if (one->Hash() != two->Hash()) return false; |
| 11922 | } |
| 11923 | |
| 11924 | // We know the strings are both non-empty. Compare the first chars |
| 11925 | // before we try to flatten the strings. |
| 11926 | if (one->Get(0) != two->Get(0)) return false; |
| 11927 | |
| 11928 | one = String::Flatten(one); |
| 11929 | two = String::Flatten(two); |
| 11930 | |
| 11931 | DisallowHeapAllocation no_gc; |
| 11932 | String::FlatContent flat1 = one->GetFlatContent(); |
| 11933 | String::FlatContent flat2 = two->GetFlatContent(); |
| 11934 | |
| 11935 | if (flat1.IsOneByte() && flat2.IsOneByte()) { |
| 11936 | return CompareRawStringContents(flat1.ToOneByteVector().start(), |
| 11937 | flat2.ToOneByteVector().start(), |
| 11938 | one_length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11939 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11940 | for (int i = 0; i < one_length; i++) { |
| 11941 | if (flat1.Get(i) != flat2.Get(i)) return false; |
| 11942 | } |
| 11943 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11944 | } |
| 11945 | } |
| 11946 | |
| 11947 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11948 | // static |
| 11949 | ComparisonResult String::Compare(Handle<String> x, Handle<String> y) { |
| 11950 | // A few fast case tests before we flatten. |
| 11951 | if (x.is_identical_to(y)) { |
| 11952 | return ComparisonResult::kEqual; |
| 11953 | } else if (y->length() == 0) { |
| 11954 | return x->length() == 0 ? ComparisonResult::kEqual |
| 11955 | : ComparisonResult::kGreaterThan; |
| 11956 | } else if (x->length() == 0) { |
| 11957 | return ComparisonResult::kLessThan; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11958 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11959 | |
| 11960 | int const d = x->Get(0) - y->Get(0); |
| 11961 | if (d < 0) { |
| 11962 | return ComparisonResult::kLessThan; |
| 11963 | } else if (d > 0) { |
| 11964 | return ComparisonResult::kGreaterThan; |
| 11965 | } |
| 11966 | |
| 11967 | // Slow case. |
| 11968 | x = String::Flatten(x); |
| 11969 | y = String::Flatten(y); |
| 11970 | |
| 11971 | DisallowHeapAllocation no_gc; |
| 11972 | ComparisonResult result = ComparisonResult::kEqual; |
| 11973 | int prefix_length = x->length(); |
| 11974 | if (y->length() < prefix_length) { |
| 11975 | prefix_length = y->length(); |
| 11976 | result = ComparisonResult::kGreaterThan; |
| 11977 | } else if (y->length() > prefix_length) { |
| 11978 | result = ComparisonResult::kLessThan; |
| 11979 | } |
| 11980 | int r; |
| 11981 | String::FlatContent x_content = x->GetFlatContent(); |
| 11982 | String::FlatContent y_content = y->GetFlatContent(); |
| 11983 | if (x_content.IsOneByte()) { |
| 11984 | Vector<const uint8_t> x_chars = x_content.ToOneByteVector(); |
| 11985 | if (y_content.IsOneByte()) { |
| 11986 | Vector<const uint8_t> y_chars = y_content.ToOneByteVector(); |
| 11987 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 11988 | } else { |
| 11989 | Vector<const uc16> y_chars = y_content.ToUC16Vector(); |
| 11990 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 11991 | } |
| 11992 | } else { |
| 11993 | Vector<const uc16> x_chars = x_content.ToUC16Vector(); |
| 11994 | if (y_content.IsOneByte()) { |
| 11995 | Vector<const uint8_t> y_chars = y_content.ToOneByteVector(); |
| 11996 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 11997 | } else { |
| 11998 | Vector<const uc16> y_chars = y_content.ToUC16Vector(); |
| 11999 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 12000 | } |
| 12001 | } |
| 12002 | if (r < 0) { |
| 12003 | result = ComparisonResult::kLessThan; |
| 12004 | } else if (r > 0) { |
| 12005 | result = ComparisonResult::kGreaterThan; |
| 12006 | } |
| 12007 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12008 | } |
| 12009 | |
| 12010 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12011 | bool String::IsUtf8EqualTo(Vector<const char> str, bool allow_prefix_match) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12012 | int slen = length(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12013 | // Can't check exact length equality, but we can check bounds. |
| 12014 | int str_len = str.length(); |
| 12015 | if (!allow_prefix_match && |
| 12016 | (str_len < slen || |
| 12017 | str_len > slen*static_cast<int>(unibrow::Utf8::kMaxEncodedSize))) { |
| 12018 | return false; |
| 12019 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12020 | int i; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12021 | size_t remaining_in_str = static_cast<size_t>(str_len); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12022 | const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(str.start()); |
| 12023 | for (i = 0; i < slen && remaining_in_str > 0; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12024 | size_t cursor = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12025 | uint32_t r = unibrow::Utf8::ValueOf(utf8_data, remaining_in_str, &cursor); |
| 12026 | DCHECK(cursor > 0 && cursor <= remaining_in_str); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12027 | if (r > unibrow::Utf16::kMaxNonSurrogateCharCode) { |
| 12028 | if (i > slen - 1) return false; |
| 12029 | if (Get(i++) != unibrow::Utf16::LeadSurrogate(r)) return false; |
| 12030 | if (Get(i) != unibrow::Utf16::TrailSurrogate(r)) return false; |
| 12031 | } else { |
| 12032 | if (Get(i) != r) return false; |
| 12033 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12034 | utf8_data += cursor; |
| 12035 | remaining_in_str -= cursor; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12036 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12037 | return (allow_prefix_match || i == slen) && remaining_in_str == 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12038 | } |
| 12039 | |
| 12040 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12041 | bool String::IsOneByteEqualTo(Vector<const uint8_t> str) { |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 12042 | int slen = length(); |
| 12043 | if (str.length() != slen) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12044 | DisallowHeapAllocation no_gc; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 12045 | FlatContent content = GetFlatContent(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12046 | if (content.IsOneByte()) { |
| 12047 | return CompareChars(content.ToOneByteVector().start(), |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 12048 | str.start(), slen) == 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12049 | } |
| 12050 | for (int i = 0; i < slen; i++) { |
| 12051 | if (Get(i) != static_cast<uint16_t>(str[i])) return false; |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 12052 | } |
| 12053 | return true; |
| 12054 | } |
| 12055 | |
| 12056 | |
| 12057 | bool String::IsTwoByteEqualTo(Vector<const uc16> str) { |
| 12058 | int slen = length(); |
| 12059 | if (str.length() != slen) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12060 | DisallowHeapAllocation no_gc; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 12061 | FlatContent content = GetFlatContent(); |
| 12062 | if (content.IsTwoByte()) { |
| 12063 | return CompareChars(content.ToUC16Vector().start(), str.start(), slen) == 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12064 | } |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 12065 | for (int i = 0; i < slen; i++) { |
| 12066 | if (Get(i) != str[i]) return false; |
| 12067 | } |
| 12068 | return true; |
| 12069 | } |
| 12070 | |
| 12071 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12072 | uint32_t String::ComputeAndSetHash() { |
| 12073 | // Should only be called if hash code has not yet been computed. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12074 | DCHECK(!HasHashCode()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12075 | |
| 12076 | // Store the hash code in the object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12077 | uint32_t field = IteratingStringHasher::Hash(this, GetHeap()->HashSeed()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 12078 | set_hash_field(field); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12079 | |
| 12080 | // Check the hash code is there. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12081 | DCHECK(HasHashCode()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12082 | uint32_t result = field >> kHashShift; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12083 | 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] | 12084 | return result; |
| 12085 | } |
| 12086 | |
| 12087 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12088 | bool String::ComputeArrayIndex(uint32_t* index) { |
| 12089 | int length = this->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12090 | if (length == 0 || length > kMaxArrayIndexSize) return false; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12091 | StringCharacterStream stream(this); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12092 | return StringToArrayIndex(&stream, index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12093 | } |
| 12094 | |
| 12095 | |
| 12096 | bool String::SlowAsArrayIndex(uint32_t* index) { |
| 12097 | if (length() <= kMaxCachedArrayIndexLength) { |
| 12098 | Hash(); // force computation of hash code |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 12099 | uint32_t field = hash_field(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 12100 | if ((field & kIsNotArrayIndexMask) != 0) return false; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 12101 | // Isolate the array index form the full hash field. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12102 | *index = ArrayIndexValueBits::decode(field); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12103 | return true; |
| 12104 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12105 | return ComputeArrayIndex(index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12106 | } |
| 12107 | } |
| 12108 | |
| 12109 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12110 | Handle<String> SeqString::Truncate(Handle<SeqString> string, int new_length) { |
| 12111 | int new_size, old_size; |
| 12112 | int old_length = string->length(); |
| 12113 | if (old_length <= new_length) return string; |
| 12114 | |
| 12115 | if (string->IsSeqOneByteString()) { |
| 12116 | old_size = SeqOneByteString::SizeFor(old_length); |
| 12117 | new_size = SeqOneByteString::SizeFor(new_length); |
| 12118 | } else { |
| 12119 | DCHECK(string->IsSeqTwoByteString()); |
| 12120 | old_size = SeqTwoByteString::SizeFor(old_length); |
| 12121 | new_size = SeqTwoByteString::SizeFor(new_length); |
| 12122 | } |
| 12123 | |
| 12124 | int delta = old_size - new_size; |
| 12125 | |
| 12126 | Address start_of_string = string->address(); |
| 12127 | DCHECK_OBJECT_ALIGNED(start_of_string); |
| 12128 | DCHECK_OBJECT_ALIGNED(start_of_string + new_size); |
| 12129 | |
| 12130 | Heap* heap = string->GetHeap(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12131 | // Sizes are pointer size aligned, so that we can use filler objects |
| 12132 | // that are a multiple of pointer size. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 12133 | heap->CreateFillerObjectAt(start_of_string + new_size, delta, |
| 12134 | ClearRecordedSlots::kNo); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12135 | heap->AdjustLiveBytes(*string, -delta, Heap::CONCURRENT_TO_SWEEPER); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12136 | |
| 12137 | // We are storing the new length using release store after creating a filler |
| 12138 | // for the left-over space to avoid races with the sweeper thread. |
| 12139 | string->synchronized_set_length(new_length); |
| 12140 | |
| 12141 | if (new_length == 0) return heap->isolate()->factory()->empty_string(); |
| 12142 | return string; |
| 12143 | } |
| 12144 | |
| 12145 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 12146 | uint32_t StringHasher::MakeArrayIndexHash(uint32_t value, int length) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 12147 | // For array indexes mix the length into the hash as an array index could |
| 12148 | // be zero. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12149 | DCHECK(length > 0); |
| 12150 | DCHECK(length <= String::kMaxArrayIndexSize); |
| 12151 | DCHECK(TenToThe(String::kMaxCachedArrayIndexLength) < |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 12152 | (1 << String::kArrayIndexValueBits)); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 12153 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12154 | value <<= String::ArrayIndexValueBits::kShift; |
| 12155 | value |= length << String::ArrayIndexLengthBits::kShift; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 12156 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12157 | DCHECK((value & String::kIsNotArrayIndexMask) == 0); |
| 12158 | DCHECK((length > String::kMaxCachedArrayIndexLength) || |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 12159 | (value & String::kContainsCachedArrayIndexMask) == 0); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 12160 | return value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12161 | } |
| 12162 | |
| 12163 | |
| 12164 | uint32_t StringHasher::GetHashField() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 12165 | if (length_ <= String::kMaxHashCalcLength) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12166 | if (is_array_index_) { |
| 12167 | return MakeArrayIndexHash(array_index_, length_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12168 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12169 | return (GetHashCore(raw_running_hash_) << String::kHashShift) | |
| 12170 | String::kIsNotArrayIndexMask; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12171 | } else { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 12172 | return (length_ << String::kHashShift) | String::kIsNotArrayIndexMask; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12173 | } |
| 12174 | } |
| 12175 | |
| 12176 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12177 | uint32_t StringHasher::ComputeUtf8Hash(Vector<const char> chars, |
| 12178 | uint32_t seed, |
| 12179 | int* utf16_length_out) { |
| 12180 | int vector_length = chars.length(); |
| 12181 | // Handle some edge cases |
| 12182 | if (vector_length <= 1) { |
| 12183 | DCHECK(vector_length == 0 || |
| 12184 | static_cast<uint8_t>(chars.start()[0]) <= |
| 12185 | unibrow::Utf8::kMaxOneByteChar); |
| 12186 | *utf16_length_out = vector_length; |
| 12187 | return HashSequentialString(chars.start(), vector_length, seed); |
Steve 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 | // Start with a fake length which won't affect computation. |
| 12190 | // It will be updated later. |
| 12191 | StringHasher hasher(String::kMaxArrayIndexSize, seed); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12192 | size_t remaining = static_cast<size_t>(vector_length); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12193 | const uint8_t* stream = reinterpret_cast<const uint8_t*>(chars.start()); |
| 12194 | int utf16_length = 0; |
| 12195 | bool is_index = true; |
| 12196 | DCHECK(hasher.is_array_index_); |
| 12197 | while (remaining > 0) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12198 | size_t consumed = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12199 | uint32_t c = unibrow::Utf8::ValueOf(stream, remaining, &consumed); |
| 12200 | DCHECK(consumed > 0 && consumed <= remaining); |
| 12201 | stream += consumed; |
| 12202 | remaining -= consumed; |
| 12203 | bool is_two_characters = c > unibrow::Utf16::kMaxNonSurrogateCharCode; |
| 12204 | utf16_length += is_two_characters ? 2 : 1; |
| 12205 | // No need to keep hashing. But we do need to calculate utf16_length. |
| 12206 | if (utf16_length > String::kMaxHashCalcLength) continue; |
| 12207 | if (is_two_characters) { |
| 12208 | uint16_t c1 = unibrow::Utf16::LeadSurrogate(c); |
| 12209 | uint16_t c2 = unibrow::Utf16::TrailSurrogate(c); |
| 12210 | hasher.AddCharacter(c1); |
| 12211 | hasher.AddCharacter(c2); |
| 12212 | if (is_index) is_index = hasher.UpdateIndex(c1); |
| 12213 | if (is_index) is_index = hasher.UpdateIndex(c2); |
| 12214 | } else { |
| 12215 | hasher.AddCharacter(c); |
| 12216 | if (is_index) is_index = hasher.UpdateIndex(c); |
| 12217 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12218 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12219 | *utf16_length_out = static_cast<int>(utf16_length); |
| 12220 | // Must set length here so that hash computation is correct. |
| 12221 | hasher.length_ = utf16_length; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12222 | return hasher.GetHashField(); |
| 12223 | } |
| 12224 | |
| 12225 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12226 | void IteratingStringHasher::VisitConsString(ConsString* cons_string) { |
| 12227 | // Run small ConsStrings through ConsStringIterator. |
| 12228 | if (cons_string->length() < 64) { |
| 12229 | ConsStringIterator iter(cons_string); |
| 12230 | int offset; |
| 12231 | String* string; |
| 12232 | while (nullptr != (string = iter.Next(&offset))) { |
| 12233 | DCHECK_EQ(0, offset); |
| 12234 | String::VisitFlat(this, string, 0); |
| 12235 | } |
| 12236 | return; |
| 12237 | } |
| 12238 | // Slow case. |
| 12239 | const int max_length = String::kMaxHashCalcLength; |
| 12240 | int length = std::min(cons_string->length(), max_length); |
| 12241 | if (cons_string->HasOnlyOneByteChars()) { |
| 12242 | uint8_t* buffer = new uint8_t[length]; |
| 12243 | String::WriteToFlat(cons_string, buffer, 0, length); |
| 12244 | AddCharacters(buffer, length); |
| 12245 | delete[] buffer; |
| 12246 | } else { |
| 12247 | uint16_t* buffer = new uint16_t[length]; |
| 12248 | String::WriteToFlat(cons_string, buffer, 0, length); |
| 12249 | AddCharacters(buffer, length); |
| 12250 | delete[] buffer; |
| 12251 | } |
| 12252 | } |
| 12253 | |
| 12254 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12255 | void String::PrintOn(FILE* file) { |
| 12256 | int length = this->length(); |
| 12257 | for (int i = 0; i < length; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12258 | PrintF(file, "%c", Get(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12259 | } |
| 12260 | } |
| 12261 | |
| 12262 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12263 | int Map::Hash() { |
| 12264 | // 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] | 12265 | // constructor, prototype and bit_field2. For predictability reasons we |
| 12266 | // use objects' offsets in respective pages for hashing instead of raw |
| 12267 | // addresses. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12268 | |
| 12269 | // Shift away the tag. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12270 | int hash = ObjectAddressForHashing(GetConstructor()) >> 2; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12271 | |
| 12272 | // XOR-ing the prototype and constructor directly yields too many zero bits |
| 12273 | // when the two pointers are close (which is fairly common). |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12274 | // To avoid this we shift the prototype bits relatively to the constructor. |
| 12275 | hash ^= ObjectAddressForHashing(prototype()) << (32 - kPageSizeBits); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12276 | |
| 12277 | return hash ^ (hash >> 16) ^ bit_field2(); |
| 12278 | } |
| 12279 | |
| 12280 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12281 | namespace { |
| 12282 | |
| 12283 | bool CheckEquivalent(Map* first, Map* second) { |
| 12284 | return first->GetConstructor() == second->GetConstructor() && |
| 12285 | first->prototype() == second->prototype() && |
| 12286 | first->instance_type() == second->instance_type() && |
| 12287 | first->bit_field() == second->bit_field() && |
| 12288 | first->is_extensible() == second->is_extensible() && |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 12289 | first->new_target_is_base() == second->new_target_is_base() && |
| 12290 | first->has_hidden_prototype() == second->has_hidden_prototype(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12291 | } |
| 12292 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12293 | } // namespace |
| 12294 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12295 | |
| 12296 | bool Map::EquivalentToForTransition(Map* other) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12297 | if (!CheckEquivalent(this, other)) return false; |
| 12298 | if (instance_type() == JS_FUNCTION_TYPE) { |
| 12299 | // JSFunctions require more checks to ensure that sloppy function is |
| 12300 | // not equvalent to strict function. |
| 12301 | int nof = Min(NumberOfOwnDescriptors(), other->NumberOfOwnDescriptors()); |
| 12302 | return instance_descriptors()->IsEqualUpTo(other->instance_descriptors(), |
| 12303 | nof); |
| 12304 | } |
| 12305 | return true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12306 | } |
| 12307 | |
| 12308 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12309 | bool Map::EquivalentToForNormalization(Map* other, |
| 12310 | PropertyNormalizationMode mode) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12311 | int properties = |
| 12312 | mode == CLEAR_INOBJECT_PROPERTIES ? 0 : other->GetInObjectProperties(); |
| 12313 | return CheckEquivalent(this, other) && bit_field2() == other->bit_field2() && |
| 12314 | GetInObjectProperties() == properties; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12315 | } |
| 12316 | |
| 12317 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12318 | bool JSFunction::Inlines(SharedFunctionInfo* candidate) { |
| 12319 | DisallowHeapAllocation no_gc; |
| 12320 | if (shared() == candidate) return true; |
| 12321 | if (code()->kind() != Code::OPTIMIZED_FUNCTION) return false; |
| 12322 | DeoptimizationInputData* const data = |
| 12323 | DeoptimizationInputData::cast(code()->deoptimization_data()); |
| 12324 | if (data->length() == 0) return false; |
| 12325 | FixedArray* const literals = data->LiteralArray(); |
| 12326 | int const inlined_count = data->InlinedFunctionCount()->value(); |
| 12327 | for (int i = 0; i < inlined_count; ++i) { |
| 12328 | if (SharedFunctionInfo::cast(literals->get(i)) == candidate) { |
| 12329 | return true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12330 | } |
| 12331 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12332 | return false; |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 12333 | } |
| 12334 | |
| 12335 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12336 | void JSFunction::MarkForOptimization() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12337 | Isolate* isolate = GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12338 | // Do not optimize if function contains break points. |
| 12339 | if (shared()->HasDebugInfo()) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12340 | DCHECK(!IsOptimized()); |
| 12341 | DCHECK(shared()->allows_lazy_compilation() || |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12342 | !shared()->optimization_disabled()); |
| 12343 | DCHECK(!shared()->HasDebugInfo()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12344 | set_code_no_write_barrier( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12345 | isolate->builtins()->builtin(Builtins::kCompileOptimized)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12346 | // 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] | 12347 | } |
| 12348 | |
| 12349 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12350 | void JSFunction::AttemptConcurrentOptimization() { |
| 12351 | Isolate* isolate = GetIsolate(); |
| 12352 | if (!isolate->concurrent_recompilation_enabled() || |
| 12353 | isolate->bootstrapper()->IsActive()) { |
| 12354 | MarkForOptimization(); |
| 12355 | return; |
| 12356 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12357 | DCHECK(!IsInOptimizationQueue()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12358 | DCHECK(!IsOptimized()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12359 | DCHECK(shared()->allows_lazy_compilation() || |
| 12360 | !shared()->optimization_disabled()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12361 | DCHECK(isolate->concurrent_recompilation_enabled()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12362 | if (FLAG_trace_concurrent_recompilation) { |
| 12363 | PrintF(" ** Marking "); |
| 12364 | ShortPrint(); |
| 12365 | PrintF(" for concurrent recompilation.\n"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12366 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12367 | set_code_no_write_barrier( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12368 | isolate->builtins()->builtin(Builtins::kCompileOptimizedConcurrent)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12369 | // 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] | 12370 | } |
| 12371 | |
| 12372 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12373 | void SharedFunctionInfo::AddSharedCodeToOptimizedCodeMap( |
| 12374 | Handle<SharedFunctionInfo> shared, Handle<Code> code) { |
| 12375 | Isolate* isolate = shared->GetIsolate(); |
| 12376 | if (isolate->serializer_enabled()) return; |
| 12377 | DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION); |
| 12378 | // Empty code maps are unsupported. |
| 12379 | if (!shared->OptimizedCodeMapIsCleared()) { |
| 12380 | Handle<WeakCell> cell = isolate->factory()->NewWeakCell(code); |
| 12381 | // A collection may have occured and cleared the optimized code map in the |
| 12382 | // allocation above. |
| 12383 | if (!shared->OptimizedCodeMapIsCleared()) { |
| 12384 | shared->optimized_code_map()->set(kSharedCodeIndex, *cell); |
| 12385 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12386 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12387 | } |
| 12388 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 12389 | // static |
| 12390 | void SharedFunctionInfo::AddToOptimizedCodeMap( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12391 | Handle<SharedFunctionInfo> shared, Handle<Context> native_context, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 12392 | MaybeHandle<Code> code, Handle<LiteralsArray> literals, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12393 | BailoutId osr_ast_id) { |
| 12394 | Isolate* isolate = shared->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12395 | if (isolate->serializer_enabled()) return; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 12396 | DCHECK(code.is_null() || |
| 12397 | code.ToHandleChecked()->kind() == Code::OPTIMIZED_FUNCTION); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12398 | DCHECK(native_context->IsNativeContext()); |
| 12399 | STATIC_ASSERT(kEntryLength == 4); |
| 12400 | Handle<FixedArray> new_code_map; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12401 | int entry; |
| 12402 | |
| 12403 | if (shared->OptimizedCodeMapIsCleared()) { |
| 12404 | new_code_map = isolate->factory()->NewFixedArray(kInitialLength, TENURED); |
| 12405 | new_code_map->set(kSharedCodeIndex, *isolate->factory()->empty_weak_cell(), |
| 12406 | SKIP_WRITE_BARRIER); |
| 12407 | entry = kEntriesStart; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12408 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12409 | Handle<FixedArray> old_code_map(shared->optimized_code_map(), isolate); |
| 12410 | entry = shared->SearchOptimizedCodeMapEntry(*native_context, osr_ast_id); |
| 12411 | if (entry > kSharedCodeIndex) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 12412 | // Just set the code and literals of the entry. |
| 12413 | if (!code.is_null()) { |
| 12414 | Handle<WeakCell> code_cell = |
| 12415 | isolate->factory()->NewWeakCell(code.ToHandleChecked()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12416 | old_code_map->set(entry + kCachedCodeOffset, *code_cell); |
| 12417 | } |
| 12418 | Handle<WeakCell> literals_cell = |
| 12419 | isolate->factory()->NewWeakCell(literals); |
| 12420 | old_code_map->set(entry + kLiteralsOffset, *literals_cell); |
| 12421 | return; |
| 12422 | } |
| 12423 | |
| 12424 | // Can we reuse an entry? |
| 12425 | DCHECK(entry < kEntriesStart); |
| 12426 | int length = old_code_map->length(); |
| 12427 | for (int i = kEntriesStart; i < length; i += kEntryLength) { |
| 12428 | if (WeakCell::cast(old_code_map->get(i + kContextOffset))->cleared()) { |
| 12429 | new_code_map = old_code_map; |
| 12430 | entry = i; |
| 12431 | break; |
| 12432 | } |
| 12433 | } |
| 12434 | |
| 12435 | if (entry < kEntriesStart) { |
| 12436 | // Copy old optimized code map and append one new entry. |
| 12437 | new_code_map = isolate->factory()->CopyFixedArrayAndGrow( |
| 12438 | old_code_map, kEntryLength, TENURED); |
| 12439 | // TODO(mstarzinger): Temporary workaround. The allocation above might |
| 12440 | // have flushed the optimized code map and the copy we created is full of |
| 12441 | // holes. For now we just give up on adding the entry and pretend it got |
| 12442 | // flushed. |
| 12443 | if (shared->OptimizedCodeMapIsCleared()) return; |
| 12444 | entry = old_code_map->length(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12445 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12446 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12447 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 12448 | Handle<WeakCell> code_cell = |
| 12449 | code.is_null() ? isolate->factory()->empty_weak_cell() |
| 12450 | : isolate->factory()->NewWeakCell(code.ToHandleChecked()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12451 | Handle<WeakCell> literals_cell = isolate->factory()->NewWeakCell(literals); |
| 12452 | WeakCell* context_cell = native_context->self_weak_cell(); |
| 12453 | |
| 12454 | new_code_map->set(entry + kContextOffset, context_cell); |
| 12455 | new_code_map->set(entry + kCachedCodeOffset, *code_cell); |
| 12456 | new_code_map->set(entry + kLiteralsOffset, *literals_cell); |
| 12457 | new_code_map->set(entry + kOsrAstIdOffset, Smi::FromInt(osr_ast_id.ToInt())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12458 | |
| 12459 | #ifdef DEBUG |
| 12460 | for (int i = kEntriesStart; i < new_code_map->length(); i += kEntryLength) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12461 | WeakCell* cell = WeakCell::cast(new_code_map->get(i + kContextOffset)); |
| 12462 | DCHECK(cell->cleared() || cell->value()->IsNativeContext()); |
| 12463 | cell = WeakCell::cast(new_code_map->get(i + kCachedCodeOffset)); |
| 12464 | DCHECK(cell->cleared() || |
| 12465 | (cell->value()->IsCode() && |
| 12466 | Code::cast(cell->value())->kind() == Code::OPTIMIZED_FUNCTION)); |
| 12467 | cell = WeakCell::cast(new_code_map->get(i + kLiteralsOffset)); |
| 12468 | DCHECK(cell->cleared() || cell->value()->IsFixedArray()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12469 | DCHECK(new_code_map->get(i + kOsrAstIdOffset)->IsSmi()); |
| 12470 | } |
| 12471 | #endif |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12472 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12473 | FixedArray* old_code_map = shared->optimized_code_map(); |
| 12474 | if (old_code_map != *new_code_map) { |
| 12475 | shared->set_optimized_code_map(*new_code_map); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12476 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12477 | } |
| 12478 | |
| 12479 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12480 | void SharedFunctionInfo::ClearOptimizedCodeMap() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12481 | FixedArray* cleared_map = GetHeap()->cleared_optimized_code_map(); |
| 12482 | set_optimized_code_map(cleared_map, SKIP_WRITE_BARRIER); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12483 | } |
| 12484 | |
| 12485 | |
| 12486 | void SharedFunctionInfo::EvictFromOptimizedCodeMap(Code* optimized_code, |
| 12487 | const char* reason) { |
| 12488 | DisallowHeapAllocation no_gc; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12489 | if (OptimizedCodeMapIsCleared()) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12490 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12491 | Heap* heap = GetHeap(); |
| 12492 | FixedArray* code_map = optimized_code_map(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12493 | int dst = kEntriesStart; |
| 12494 | int length = code_map->length(); |
| 12495 | for (int src = kEntriesStart; src < length; src += kEntryLength) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12496 | DCHECK(WeakCell::cast(code_map->get(src))->cleared() || |
| 12497 | WeakCell::cast(code_map->get(src))->value()->IsNativeContext()); |
| 12498 | if (WeakCell::cast(code_map->get(src + kCachedCodeOffset))->value() == |
| 12499 | optimized_code) { |
| 12500 | BailoutId osr(Smi::cast(code_map->get(src + kOsrAstIdOffset))->value()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12501 | if (FLAG_trace_opt) { |
| 12502 | PrintF("[evicting entry from optimizing code map (%s) for ", reason); |
| 12503 | ShortPrint(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12504 | if (osr.IsNone()) { |
| 12505 | PrintF("]\n"); |
| 12506 | } else { |
| 12507 | PrintF(" (osr ast id %d)]\n", osr.ToInt()); |
| 12508 | } |
| 12509 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12510 | if (!osr.IsNone()) { |
| 12511 | // Evict the src entry by not copying it to the dst entry. |
| 12512 | continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12513 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12514 | // In case of non-OSR entry just clear the code in order to proceed |
| 12515 | // sharing literals. |
| 12516 | code_map->set(src + kCachedCodeOffset, heap->empty_weak_cell(), |
| 12517 | SKIP_WRITE_BARRIER); |
| 12518 | } |
| 12519 | |
| 12520 | // Keep the src entry by copying it to the dst entry. |
| 12521 | if (dst != src) { |
| 12522 | code_map->set(dst + kContextOffset, code_map->get(src + kContextOffset)); |
| 12523 | code_map->set(dst + kCachedCodeOffset, |
| 12524 | code_map->get(src + kCachedCodeOffset)); |
| 12525 | code_map->set(dst + kLiteralsOffset, |
| 12526 | code_map->get(src + kLiteralsOffset)); |
| 12527 | code_map->set(dst + kOsrAstIdOffset, |
| 12528 | code_map->get(src + kOsrAstIdOffset)); |
| 12529 | } |
| 12530 | dst += kEntryLength; |
| 12531 | } |
| 12532 | if (WeakCell::cast(code_map->get(kSharedCodeIndex))->value() == |
| 12533 | optimized_code) { |
| 12534 | // Evict context-independent code as well. |
| 12535 | code_map->set(kSharedCodeIndex, heap->empty_weak_cell(), |
| 12536 | SKIP_WRITE_BARRIER); |
| 12537 | if (FLAG_trace_opt) { |
| 12538 | PrintF("[evicting entry from optimizing code map (%s) for ", reason); |
| 12539 | ShortPrint(); |
| 12540 | PrintF(" (context-independent code)]\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12541 | } |
| 12542 | } |
| 12543 | if (dst != length) { |
| 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 | heap->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>(code_map, |
| 12546 | length - dst); |
| 12547 | if (code_map->length() == kEntriesStart && |
| 12548 | WeakCell::cast(code_map->get(kSharedCodeIndex))->cleared()) { |
| 12549 | ClearOptimizedCodeMap(); |
| 12550 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12551 | } |
| 12552 | } |
| 12553 | |
| 12554 | |
| 12555 | void SharedFunctionInfo::TrimOptimizedCodeMap(int shrink_by) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12556 | FixedArray* code_map = optimized_code_map(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12557 | DCHECK(shrink_by % kEntryLength == 0); |
| 12558 | DCHECK(shrink_by <= code_map->length() - kEntriesStart); |
| 12559 | // Always trim even when array is cleared because of heap verifier. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12560 | GetHeap()->RightTrimFixedArray<Heap::SEQUENTIAL_TO_SWEEPER>(code_map, |
| 12561 | shrink_by); |
| 12562 | if (code_map->length() == kEntriesStart && |
| 12563 | WeakCell::cast(code_map->get(kSharedCodeIndex))->cleared()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12564 | ClearOptimizedCodeMap(); |
| 12565 | } |
| 12566 | } |
| 12567 | |
| 12568 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12569 | static void GetMinInobjectSlack(Map* map, void* data) { |
| 12570 | int slack = map->unused_property_fields(); |
| 12571 | if (*reinterpret_cast<int*>(data) > slack) { |
| 12572 | *reinterpret_cast<int*>(data) = slack; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12573 | } |
| 12574 | } |
| 12575 | |
| 12576 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12577 | static void ShrinkInstanceSize(Map* map, void* data) { |
| 12578 | int slack = *reinterpret_cast<int*>(data); |
| 12579 | map->SetInObjectProperties(map->GetInObjectProperties() - slack); |
| 12580 | map->set_unused_property_fields(map->unused_property_fields() - slack); |
| 12581 | map->set_instance_size(map->instance_size() - slack * kPointerSize); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 12582 | map->set_construction_counter(Map::kNoSlackTracking); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12583 | |
| 12584 | // Visitor id might depend on the instance size, recalculate it. |
| 12585 | map->set_visitor_id(Heap::GetStaticVisitorIdForMap(map)); |
| 12586 | } |
| 12587 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 12588 | static void StopSlackTracking(Map* map, void* data) { |
| 12589 | map->set_construction_counter(Map::kNoSlackTracking); |
| 12590 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12591 | |
| 12592 | void Map::CompleteInobjectSlackTracking() { |
| 12593 | // Has to be an initial map. |
| 12594 | DCHECK(GetBackPointer()->IsUndefined()); |
| 12595 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12596 | int slack = unused_property_fields(); |
| 12597 | TransitionArray::TraverseTransitionTree(this, &GetMinInobjectSlack, &slack); |
| 12598 | if (slack != 0) { |
| 12599 | // Resize the initial map and all maps in its transition tree. |
| 12600 | TransitionArray::TraverseTransitionTree(this, &ShrinkInstanceSize, &slack); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 12601 | } else { |
| 12602 | TransitionArray::TraverseTransitionTree(this, &StopSlackTracking, nullptr); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12603 | } |
| 12604 | } |
| 12605 | |
| 12606 | |
| 12607 | static bool PrototypeBenefitsFromNormalization(Handle<JSObject> object) { |
| 12608 | DisallowHeapAllocation no_gc; |
| 12609 | if (!object->HasFastProperties()) return false; |
| 12610 | Map* map = object->map(); |
| 12611 | if (map->is_prototype_map()) return false; |
| 12612 | DescriptorArray* descriptors = map->instance_descriptors(); |
| 12613 | for (int i = 0; i < map->NumberOfOwnDescriptors(); i++) { |
| 12614 | PropertyDetails details = descriptors->GetDetails(i); |
| 12615 | if (details.location() == kDescriptor) continue; |
| 12616 | if (details.representation().IsHeapObject() || |
| 12617 | details.representation().IsTagged()) { |
| 12618 | FieldIndex index = FieldIndex::ForDescriptor(map, i); |
| 12619 | if (object->RawFastPropertyAt(index)->IsJSFunction()) return true; |
| 12620 | } |
| 12621 | } |
| 12622 | return false; |
| 12623 | } |
| 12624 | |
| 12625 | |
| 12626 | // static |
| 12627 | void JSObject::OptimizeAsPrototype(Handle<JSObject> object, |
| 12628 | PrototypeOptimizationMode mode) { |
| 12629 | if (object->IsJSGlobalObject()) return; |
| 12630 | if (mode == FAST_PROTOTYPE && PrototypeBenefitsFromNormalization(object)) { |
| 12631 | // First normalize to ensure all JSFunctions are DATA_CONSTANT. |
| 12632 | JSObject::NormalizeProperties(object, KEEP_INOBJECT_PROPERTIES, 0, |
| 12633 | "NormalizeAsPrototype"); |
| 12634 | } |
| 12635 | Handle<Map> previous_map(object->map()); |
| 12636 | if (!object->HasFastProperties()) { |
| 12637 | JSObject::MigrateSlowToFast(object, 0, "OptimizeAsPrototype"); |
| 12638 | } |
| 12639 | if (!object->map()->is_prototype_map()) { |
| 12640 | if (object->map() == *previous_map) { |
| 12641 | Handle<Map> new_map = Map::Copy(handle(object->map()), "CopyAsPrototype"); |
| 12642 | JSObject::MigrateToMap(object, new_map); |
| 12643 | } |
| 12644 | object->map()->set_is_prototype_map(true); |
| 12645 | |
| 12646 | // Replace the pointer to the exact constructor with the Object function |
| 12647 | // from the same context if undetectable from JS. This is to avoid keeping |
| 12648 | // memory alive unnecessarily. |
| 12649 | Object* maybe_constructor = object->map()->GetConstructor(); |
| 12650 | if (maybe_constructor->IsJSFunction()) { |
| 12651 | JSFunction* constructor = JSFunction::cast(maybe_constructor); |
| 12652 | Isolate* isolate = object->GetIsolate(); |
| 12653 | if (!constructor->shared()->IsApiFunction() && |
| 12654 | object->class_name() == isolate->heap()->Object_string()) { |
| 12655 | Context* context = constructor->context()->native_context(); |
| 12656 | JSFunction* object_function = context->object_function(); |
| 12657 | object->map()->SetConstructor(object_function); |
| 12658 | } |
| 12659 | } |
| 12660 | } |
| 12661 | } |
| 12662 | |
| 12663 | |
| 12664 | // static |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12665 | void JSObject::ReoptimizeIfPrototype(Handle<JSObject> object) { |
| 12666 | if (!object->map()->is_prototype_map()) return; |
| 12667 | OptimizeAsPrototype(object, FAST_PROTOTYPE); |
| 12668 | } |
| 12669 | |
| 12670 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12671 | // static |
| 12672 | void JSObject::LazyRegisterPrototypeUser(Handle<Map> user, Isolate* isolate) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12673 | DCHECK(FLAG_track_prototype_users); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12674 | // Contract: In line with InvalidatePrototypeChains()'s requirements, |
| 12675 | // leaf maps don't need to register as users, only prototypes do. |
| 12676 | DCHECK(user->is_prototype_map()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12677 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12678 | Handle<Map> current_user = user; |
| 12679 | Handle<PrototypeInfo> current_user_info = |
| 12680 | Map::GetOrCreatePrototypeInfo(user, isolate); |
| 12681 | for (PrototypeIterator iter(user); !iter.IsAtEnd(); iter.Advance()) { |
| 12682 | // Walk up the prototype chain as far as links haven't been registered yet. |
| 12683 | if (current_user_info->registry_slot() != PrototypeInfo::UNREGISTERED) { |
| 12684 | break; |
| 12685 | } |
| 12686 | Handle<Object> maybe_proto = PrototypeIterator::GetCurrent(iter); |
| 12687 | // Proxies on the prototype chain are not supported. They make it |
| 12688 | // impossible to make any assumptions about the prototype chain anyway. |
| 12689 | if (maybe_proto->IsJSProxy()) return; |
| 12690 | Handle<JSObject> proto = Handle<JSObject>::cast(maybe_proto); |
| 12691 | Handle<PrototypeInfo> proto_info = |
| 12692 | Map::GetOrCreatePrototypeInfo(proto, isolate); |
| 12693 | Handle<Object> maybe_registry(proto_info->prototype_users(), isolate); |
| 12694 | int slot = 0; |
| 12695 | Handle<WeakFixedArray> new_array = |
| 12696 | WeakFixedArray::Add(maybe_registry, current_user, &slot); |
| 12697 | current_user_info->set_registry_slot(slot); |
| 12698 | if (!maybe_registry.is_identical_to(new_array)) { |
| 12699 | proto_info->set_prototype_users(*new_array); |
| 12700 | } |
| 12701 | if (FLAG_trace_prototype_users) { |
| 12702 | PrintF("Registering %p as a user of prototype %p (map=%p).\n", |
| 12703 | reinterpret_cast<void*>(*current_user), |
| 12704 | reinterpret_cast<void*>(*proto), |
| 12705 | reinterpret_cast<void*>(proto->map())); |
| 12706 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12707 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12708 | current_user = handle(proto->map(), isolate); |
| 12709 | current_user_info = proto_info; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12710 | } |
| 12711 | } |
| 12712 | |
| 12713 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12714 | // Can be called regardless of whether |user| was actually registered with |
| 12715 | // |prototype|. Returns true when there was a registration. |
| 12716 | // static |
| 12717 | bool JSObject::UnregisterPrototypeUser(Handle<Map> user, Isolate* isolate) { |
| 12718 | DCHECK(user->is_prototype_map()); |
| 12719 | // If it doesn't have a PrototypeInfo, it was never registered. |
| 12720 | if (!user->prototype_info()->IsPrototypeInfo()) return false; |
| 12721 | // If it had no prototype before, see if it had users that might expect |
| 12722 | // registration. |
| 12723 | if (!user->prototype()->IsJSObject()) { |
| 12724 | Object* users = |
| 12725 | PrototypeInfo::cast(user->prototype_info())->prototype_users(); |
| 12726 | return users->IsWeakFixedArray(); |
| 12727 | } |
| 12728 | Handle<JSObject> prototype(JSObject::cast(user->prototype()), isolate); |
| 12729 | Handle<PrototypeInfo> user_info = |
| 12730 | Map::GetOrCreatePrototypeInfo(user, isolate); |
| 12731 | int slot = user_info->registry_slot(); |
| 12732 | if (slot == PrototypeInfo::UNREGISTERED) return false; |
| 12733 | DCHECK(prototype->map()->is_prototype_map()); |
| 12734 | Object* maybe_proto_info = prototype->map()->prototype_info(); |
| 12735 | // User knows its registry slot, prototype info and user registry must exist. |
| 12736 | DCHECK(maybe_proto_info->IsPrototypeInfo()); |
| 12737 | Handle<PrototypeInfo> proto_info(PrototypeInfo::cast(maybe_proto_info), |
| 12738 | isolate); |
| 12739 | Object* maybe_registry = proto_info->prototype_users(); |
| 12740 | DCHECK(maybe_registry->IsWeakFixedArray()); |
| 12741 | DCHECK(WeakFixedArray::cast(maybe_registry)->Get(slot) == *user); |
| 12742 | WeakFixedArray::cast(maybe_registry)->Clear(slot); |
| 12743 | if (FLAG_trace_prototype_users) { |
| 12744 | PrintF("Unregistering %p as a user of prototype %p.\n", |
| 12745 | reinterpret_cast<void*>(*user), reinterpret_cast<void*>(*prototype)); |
| 12746 | } |
| 12747 | return true; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12748 | } |
| 12749 | |
| 12750 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12751 | static void InvalidatePrototypeChainsInternal(Map* map) { |
| 12752 | if (!map->is_prototype_map()) return; |
| 12753 | if (FLAG_trace_prototype_users) { |
| 12754 | PrintF("Invalidating prototype map %p 's cell\n", |
| 12755 | reinterpret_cast<void*>(map)); |
| 12756 | } |
| 12757 | Object* maybe_proto_info = map->prototype_info(); |
| 12758 | if (!maybe_proto_info->IsPrototypeInfo()) return; |
| 12759 | PrototypeInfo* proto_info = PrototypeInfo::cast(maybe_proto_info); |
| 12760 | Object* maybe_cell = proto_info->validity_cell(); |
| 12761 | if (maybe_cell->IsCell()) { |
| 12762 | // Just set the value; the cell will be replaced lazily. |
| 12763 | Cell* cell = Cell::cast(maybe_cell); |
| 12764 | cell->set_value(Smi::FromInt(Map::kPrototypeChainInvalid)); |
| 12765 | } |
| 12766 | |
| 12767 | WeakFixedArray::Iterator iterator(proto_info->prototype_users()); |
| 12768 | // For now, only maps register themselves as users. |
| 12769 | Map* user; |
| 12770 | while ((user = iterator.Next<Map>())) { |
| 12771 | // Walk the prototype chain (backwards, towards leaf objects) if necessary. |
| 12772 | InvalidatePrototypeChainsInternal(user); |
| 12773 | } |
| 12774 | } |
| 12775 | |
| 12776 | |
| 12777 | // static |
| 12778 | void JSObject::InvalidatePrototypeChains(Map* map) { |
| 12779 | if (!FLAG_eliminate_prototype_chain_checks) return; |
| 12780 | DisallowHeapAllocation no_gc; |
| 12781 | InvalidatePrototypeChainsInternal(map); |
| 12782 | } |
| 12783 | |
| 12784 | |
| 12785 | // static |
| 12786 | Handle<PrototypeInfo> Map::GetOrCreatePrototypeInfo(Handle<JSObject> prototype, |
| 12787 | Isolate* isolate) { |
| 12788 | Object* maybe_proto_info = prototype->map()->prototype_info(); |
| 12789 | if (maybe_proto_info->IsPrototypeInfo()) { |
| 12790 | return handle(PrototypeInfo::cast(maybe_proto_info), isolate); |
| 12791 | } |
| 12792 | Handle<PrototypeInfo> proto_info = isolate->factory()->NewPrototypeInfo(); |
| 12793 | prototype->map()->set_prototype_info(*proto_info); |
| 12794 | return proto_info; |
| 12795 | } |
| 12796 | |
| 12797 | |
| 12798 | // static |
| 12799 | Handle<PrototypeInfo> Map::GetOrCreatePrototypeInfo(Handle<Map> prototype_map, |
| 12800 | Isolate* isolate) { |
| 12801 | Object* maybe_proto_info = prototype_map->prototype_info(); |
| 12802 | if (maybe_proto_info->IsPrototypeInfo()) { |
| 12803 | return handle(PrototypeInfo::cast(maybe_proto_info), isolate); |
| 12804 | } |
| 12805 | Handle<PrototypeInfo> proto_info = isolate->factory()->NewPrototypeInfo(); |
| 12806 | prototype_map->set_prototype_info(*proto_info); |
| 12807 | return proto_info; |
| 12808 | } |
| 12809 | |
| 12810 | |
| 12811 | // static |
| 12812 | Handle<Cell> Map::GetOrCreatePrototypeChainValidityCell(Handle<Map> map, |
| 12813 | Isolate* isolate) { |
| 12814 | Handle<Object> maybe_prototype(map->prototype(), isolate); |
| 12815 | if (!maybe_prototype->IsJSObject()) return Handle<Cell>::null(); |
| 12816 | Handle<JSObject> prototype = Handle<JSObject>::cast(maybe_prototype); |
| 12817 | // Ensure the prototype is registered with its own prototypes so its cell |
| 12818 | // will be invalidated when necessary. |
| 12819 | JSObject::LazyRegisterPrototypeUser(handle(prototype->map(), isolate), |
| 12820 | isolate); |
| 12821 | Handle<PrototypeInfo> proto_info = |
| 12822 | GetOrCreatePrototypeInfo(prototype, isolate); |
| 12823 | Object* maybe_cell = proto_info->validity_cell(); |
| 12824 | // Return existing cell if it's still valid. |
| 12825 | if (maybe_cell->IsCell()) { |
| 12826 | Handle<Cell> cell(Cell::cast(maybe_cell), isolate); |
| 12827 | if (cell->value() == Smi::FromInt(Map::kPrototypeChainValid)) { |
| 12828 | return cell; |
| 12829 | } |
| 12830 | } |
| 12831 | // Otherwise create a new cell. |
| 12832 | Handle<Cell> cell = isolate->factory()->NewCell( |
| 12833 | handle(Smi::FromInt(Map::kPrototypeChainValid), isolate)); |
| 12834 | proto_info->set_validity_cell(*cell); |
| 12835 | return cell; |
| 12836 | } |
| 12837 | |
| 12838 | |
| 12839 | // static |
| 12840 | void Map::SetPrototype(Handle<Map> map, Handle<Object> prototype, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12841 | PrototypeOptimizationMode proto_mode) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 12842 | bool is_hidden = false; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12843 | if (prototype->IsJSObject()) { |
| 12844 | Handle<JSObject> prototype_jsobj = Handle<JSObject>::cast(prototype); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12845 | JSObject::OptimizeAsPrototype(prototype_jsobj, proto_mode); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 12846 | |
| 12847 | Object* maybe_constructor = prototype_jsobj->map()->GetConstructor(); |
| 12848 | if (maybe_constructor->IsJSFunction()) { |
| 12849 | JSFunction* constructor = JSFunction::cast(maybe_constructor); |
| 12850 | Object* data = constructor->shared()->function_data(); |
| 12851 | is_hidden = (data->IsFunctionTemplateInfo() && |
| 12852 | FunctionTemplateInfo::cast(data)->hidden_prototype()) || |
| 12853 | prototype->IsJSGlobalObject(); |
| 12854 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12855 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 12856 | map->set_has_hidden_prototype(is_hidden); |
| 12857 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12858 | WriteBarrierMode wb_mode = |
| 12859 | prototype->IsNull() ? SKIP_WRITE_BARRIER : UPDATE_WRITE_BARRIER; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12860 | map->set_prototype(*prototype, wb_mode); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12861 | } |
| 12862 | |
| 12863 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12864 | Handle<Object> CacheInitialJSArrayMaps( |
| 12865 | Handle<Context> native_context, Handle<Map> initial_map) { |
| 12866 | // Replace all of the cached initial array maps in the native context with |
| 12867 | // the appropriate transitioned elements kind maps. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12868 | Handle<Map> current_map = initial_map; |
| 12869 | ElementsKind kind = current_map->elements_kind(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12870 | DCHECK_EQ(GetInitialFastElementsKind(), kind); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 12871 | native_context->set(Context::ArrayMapIndex(kind), *current_map); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12872 | for (int i = GetSequenceIndexFromFastElementsKind(kind) + 1; |
| 12873 | i < kFastElementsKindCount; ++i) { |
| 12874 | Handle<Map> new_map; |
| 12875 | ElementsKind next_kind = GetFastElementsKindFromSequenceIndex(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12876 | if (Map* maybe_elements_transition = current_map->ElementsTransitionMap()) { |
| 12877 | new_map = handle(maybe_elements_transition); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12878 | } else { |
| 12879 | new_map = Map::CopyAsElementsKind( |
| 12880 | current_map, next_kind, INSERT_TRANSITION); |
| 12881 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12882 | DCHECK_EQ(next_kind, new_map->elements_kind()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 12883 | native_context->set(Context::ArrayMapIndex(next_kind), *new_map); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12884 | current_map = new_map; |
| 12885 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12886 | return initial_map; |
| 12887 | } |
| 12888 | |
| 12889 | |
| 12890 | void JSFunction::SetInstancePrototype(Handle<JSFunction> function, |
| 12891 | Handle<Object> value) { |
| 12892 | Isolate* isolate = function->GetIsolate(); |
| 12893 | |
| 12894 | DCHECK(value->IsJSReceiver()); |
| 12895 | |
| 12896 | // Now some logic for the maps of the objects that are created by using this |
| 12897 | // function as a constructor. |
| 12898 | if (function->has_initial_map()) { |
| 12899 | // If the function has allocated the initial map replace it with a |
| 12900 | // copy containing the new prototype. Also complete any in-object |
| 12901 | // slack tracking that is in progress at this point because it is |
| 12902 | // still tracking the old copy. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12903 | function->CompleteInobjectSlackTrackingIfActive(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12904 | |
| 12905 | Handle<Map> initial_map(function->initial_map(), isolate); |
| 12906 | |
| 12907 | if (!initial_map->GetIsolate()->bootstrapper()->IsActive() && |
| 12908 | initial_map->instance_type() == JS_OBJECT_TYPE) { |
| 12909 | // Put the value in the initial map field until an initial map is needed. |
| 12910 | // At that point, a new initial map is created and the prototype is put |
| 12911 | // into the initial map where it belongs. |
| 12912 | function->set_prototype_or_initial_map(*value); |
| 12913 | } else { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12914 | Handle<Map> new_map = Map::Copy(initial_map, "SetInstancePrototype"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12915 | JSFunction::SetInitialMap(function, new_map, value); |
| 12916 | |
| 12917 | // If the function is used as the global Array function, cache the |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12918 | // updated initial maps (and transitioned versions) in the native context. |
| 12919 | Handle<Context> native_context(function->context()->native_context(), |
| 12920 | isolate); |
| 12921 | Handle<Object> array_function( |
| 12922 | native_context->get(Context::ARRAY_FUNCTION_INDEX), isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12923 | if (array_function->IsJSFunction() && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12924 | *function == JSFunction::cast(*array_function)) { |
| 12925 | CacheInitialJSArrayMaps(native_context, new_map); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12926 | } |
| 12927 | } |
| 12928 | |
| 12929 | // Deoptimize all code that embeds the previous initial map. |
| 12930 | initial_map->dependent_code()->DeoptimizeDependentCodeGroup( |
| 12931 | isolate, DependentCode::kInitialMapChangedGroup); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12932 | } else { |
| 12933 | // Put the value in the initial map field until an initial map is |
| 12934 | // needed. At that point, a new initial map is created and the |
| 12935 | // prototype is put into the initial map where it belongs. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12936 | function->set_prototype_or_initial_map(*value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12937 | if (value->IsJSObject()) { |
| 12938 | // Optimize as prototype to detach it from its transition tree. |
| 12939 | JSObject::OptimizeAsPrototype(Handle<JSObject>::cast(value), |
| 12940 | FAST_PROTOTYPE); |
| 12941 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12942 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12943 | isolate->heap()->ClearInstanceofCache(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12944 | } |
| 12945 | |
| 12946 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12947 | void JSFunction::SetPrototype(Handle<JSFunction> function, |
| 12948 | Handle<Object> value) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 12949 | DCHECK(function->IsConstructor() || |
| 12950 | IsGeneratorFunction(function->shared()->kind())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12951 | Handle<Object> construct_prototype = value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12952 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12953 | // 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] | 12954 | // constructor field so it can be accessed. Also, set the prototype |
| 12955 | // used for constructing objects to the original object prototype. |
| 12956 | // See ECMA-262 13.2.2. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12957 | if (!value->IsJSReceiver()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12958 | // Copy the map so this does not affect unrelated functions. |
| 12959 | // Remove map transitions because they point to maps with a |
| 12960 | // different prototype. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 12961 | Handle<Map> new_map = Map::Copy(handle(function->map()), "SetPrototype"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12962 | |
| 12963 | JSObject::MigrateToMap(function, new_map); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12964 | new_map->SetConstructor(*value); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12965 | new_map->set_non_instance_prototype(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12966 | Isolate* isolate = new_map->GetIsolate(); |
| 12967 | construct_prototype = handle( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12968 | function->context()->native_context()->initial_object_prototype(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12969 | isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12970 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12971 | function->map()->set_non_instance_prototype(false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12972 | } |
| 12973 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12974 | return SetInstancePrototype(function, construct_prototype); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12975 | } |
| 12976 | |
| 12977 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12978 | bool JSFunction::RemovePrototype() { |
| 12979 | Context* native_context = context()->native_context(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12980 | Map* no_prototype_map = |
| 12981 | is_strict(shared()->language_mode()) |
| 12982 | ? native_context->strict_function_without_prototype_map() |
| 12983 | : native_context->sloppy_function_without_prototype_map(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12984 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12985 | if (map() == no_prototype_map) return true; |
| 12986 | |
| 12987 | #ifdef DEBUG |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12988 | if (map() != (is_strict(shared()->language_mode()) |
| 12989 | ? native_context->strict_function_map() |
| 12990 | : native_context->sloppy_function_map())) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12991 | return false; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12992 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12993 | #endif |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12994 | |
| 12995 | set_map(no_prototype_map); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12996 | set_prototype_or_initial_map(no_prototype_map->GetHeap()->the_hole_value()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12997 | return true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12998 | } |
| 12999 | |
| 13000 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13001 | void JSFunction::SetInitialMap(Handle<JSFunction> function, Handle<Map> map, |
| 13002 | Handle<Object> prototype) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 13003 | if (map->prototype() != *prototype) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13004 | Map::SetPrototype(map, prototype, FAST_PROTOTYPE); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13005 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13006 | function->set_prototype_or_initial_map(*map); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13007 | map->SetConstructor(*function); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 13008 | #if TRACE_MAPS |
| 13009 | if (FLAG_trace_maps) { |
| 13010 | PrintF("[TraceMaps: InitialMap map= %p SFI= %d_%s ]\n", |
| 13011 | reinterpret_cast<void*>(*map), function->shared()->unique_id(), |
| 13012 | function->shared()->DebugName()->ToCString().get()); |
| 13013 | } |
| 13014 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13015 | } |
| 13016 | |
| 13017 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13018 | #ifdef DEBUG |
| 13019 | namespace { |
| 13020 | |
| 13021 | bool CanSubclassHaveInobjectProperties(InstanceType instance_type) { |
| 13022 | switch (instance_type) { |
| 13023 | case JS_OBJECT_TYPE: |
| 13024 | case JS_CONTEXT_EXTENSION_OBJECT_TYPE: |
| 13025 | case JS_GENERATOR_OBJECT_TYPE: |
| 13026 | case JS_MODULE_TYPE: |
| 13027 | case JS_VALUE_TYPE: |
| 13028 | case JS_DATE_TYPE: |
| 13029 | case JS_ARRAY_TYPE: |
| 13030 | case JS_MESSAGE_OBJECT_TYPE: |
| 13031 | case JS_ARRAY_BUFFER_TYPE: |
| 13032 | case JS_TYPED_ARRAY_TYPE: |
| 13033 | case JS_DATA_VIEW_TYPE: |
| 13034 | case JS_SET_TYPE: |
| 13035 | case JS_MAP_TYPE: |
| 13036 | case JS_SET_ITERATOR_TYPE: |
| 13037 | case JS_MAP_ITERATOR_TYPE: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13038 | case JS_WEAK_MAP_TYPE: |
| 13039 | case JS_WEAK_SET_TYPE: |
| 13040 | case JS_PROMISE_TYPE: |
| 13041 | case JS_REGEXP_TYPE: |
| 13042 | case JS_FUNCTION_TYPE: |
| 13043 | return true; |
| 13044 | |
| 13045 | case JS_BOUND_FUNCTION_TYPE: |
| 13046 | case JS_PROXY_TYPE: |
| 13047 | case JS_GLOBAL_PROXY_TYPE: |
| 13048 | case JS_GLOBAL_OBJECT_TYPE: |
| 13049 | case FIXED_ARRAY_TYPE: |
| 13050 | case FIXED_DOUBLE_ARRAY_TYPE: |
| 13051 | case ODDBALL_TYPE: |
| 13052 | case FOREIGN_TYPE: |
| 13053 | case MAP_TYPE: |
| 13054 | case CODE_TYPE: |
| 13055 | case CELL_TYPE: |
| 13056 | case PROPERTY_CELL_TYPE: |
| 13057 | case WEAK_CELL_TYPE: |
| 13058 | case SYMBOL_TYPE: |
| 13059 | case BYTECODE_ARRAY_TYPE: |
| 13060 | case HEAP_NUMBER_TYPE: |
| 13061 | case MUTABLE_HEAP_NUMBER_TYPE: |
| 13062 | case SIMD128_VALUE_TYPE: |
| 13063 | case FILLER_TYPE: |
| 13064 | case BYTE_ARRAY_TYPE: |
| 13065 | case FREE_SPACE_TYPE: |
| 13066 | case SHARED_FUNCTION_INFO_TYPE: |
| 13067 | |
| 13068 | #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ |
| 13069 | case FIXED_##TYPE##_ARRAY_TYPE: |
| 13070 | #undef TYPED_ARRAY_CASE |
| 13071 | |
| 13072 | #define MAKE_STRUCT_CASE(NAME, Name, name) case NAME##_TYPE: |
| 13073 | STRUCT_LIST(MAKE_STRUCT_CASE) |
| 13074 | #undef MAKE_STRUCT_CASE |
| 13075 | // We must not end up here for these instance types at all. |
| 13076 | UNREACHABLE(); |
| 13077 | // Fall through. |
| 13078 | default: |
| 13079 | return false; |
| 13080 | } |
| 13081 | } |
| 13082 | |
| 13083 | } // namespace |
| 13084 | #endif |
| 13085 | |
| 13086 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13087 | void JSFunction::EnsureHasInitialMap(Handle<JSFunction> function) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13088 | DCHECK(function->IsConstructor() || function->shared()->is_generator()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13089 | if (function->has_initial_map()) return; |
| 13090 | Isolate* isolate = function->GetIsolate(); |
| 13091 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13092 | // The constructor should be compiled for the optimization hints to be |
| 13093 | // available. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 13094 | Compiler::Compile(function, Compiler::CLEAR_EXCEPTION); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13095 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13096 | // First create a new map with the size and number of in-object properties |
| 13097 | // suggested by the function. |
| 13098 | InstanceType instance_type; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13099 | if (function->shared()->is_generator()) { |
| 13100 | instance_type = JS_GENERATOR_OBJECT_TYPE; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13101 | } else { |
| 13102 | instance_type = JS_OBJECT_TYPE; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13103 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13104 | int instance_size; |
| 13105 | int in_object_properties; |
| 13106 | function->CalculateInstanceSize(instance_type, 0, &instance_size, |
| 13107 | &in_object_properties); |
| 13108 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13109 | Handle<Map> map = isolate->factory()->NewMap(instance_type, instance_size); |
| 13110 | |
| 13111 | // Fetch or allocate prototype. |
| 13112 | Handle<Object> prototype; |
| 13113 | if (function->has_instance_prototype()) { |
| 13114 | prototype = handle(function->instance_prototype(), isolate); |
| 13115 | } else { |
| 13116 | prototype = isolate->factory()->NewFunctionPrototype(function); |
| 13117 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13118 | map->SetInObjectProperties(in_object_properties); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13119 | map->set_unused_property_fields(in_object_properties); |
| 13120 | DCHECK(map->has_fast_object_elements()); |
| 13121 | |
| 13122 | // Finally link initial map and constructor function. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13123 | DCHECK(prototype->IsJSReceiver()); |
| 13124 | JSFunction::SetInitialMap(function, map, prototype); |
| 13125 | map->StartInobjectSlackTracking(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13126 | } |
| 13127 | |
| 13128 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13129 | // static |
| 13130 | MaybeHandle<Map> JSFunction::GetDerivedMap(Isolate* isolate, |
| 13131 | Handle<JSFunction> constructor, |
| 13132 | Handle<JSReceiver> new_target) { |
| 13133 | EnsureHasInitialMap(constructor); |
| 13134 | |
| 13135 | Handle<Map> constructor_initial_map(constructor->initial_map(), isolate); |
| 13136 | if (*new_target == *constructor) return constructor_initial_map; |
| 13137 | |
| 13138 | // Fast case, new.target is a subclass of constructor. The map is cacheable |
| 13139 | // (and may already have been cached). new.target.prototype is guaranteed to |
| 13140 | // be a JSReceiver. |
| 13141 | if (new_target->IsJSFunction()) { |
| 13142 | Handle<JSFunction> function = Handle<JSFunction>::cast(new_target); |
| 13143 | |
| 13144 | // Check that |function|'s initial map still in sync with the |constructor|, |
| 13145 | // otherwise we must create a new initial map for |function|. |
| 13146 | if (function->has_initial_map() && |
| 13147 | function->initial_map()->GetConstructor() == *constructor) { |
| 13148 | return handle(function->initial_map(), isolate); |
| 13149 | } |
| 13150 | |
| 13151 | // Create a new map with the size and number of in-object properties |
| 13152 | // suggested by |function|. |
| 13153 | |
| 13154 | // Link initial map and constructor function if the new.target is actually a |
| 13155 | // subclass constructor. |
| 13156 | if (IsSubclassConstructor(function->shared()->kind())) { |
| 13157 | Handle<Object> prototype(function->instance_prototype(), isolate); |
| 13158 | InstanceType instance_type = constructor_initial_map->instance_type(); |
| 13159 | DCHECK(CanSubclassHaveInobjectProperties(instance_type)); |
| 13160 | int internal_fields = |
| 13161 | JSObject::GetInternalFieldCount(*constructor_initial_map); |
| 13162 | int pre_allocated = constructor_initial_map->GetInObjectProperties() - |
| 13163 | constructor_initial_map->unused_property_fields(); |
| 13164 | int instance_size; |
| 13165 | int in_object_properties; |
| 13166 | function->CalculateInstanceSizeForDerivedClass( |
| 13167 | instance_type, internal_fields, &instance_size, |
| 13168 | &in_object_properties); |
| 13169 | |
| 13170 | int unused_property_fields = in_object_properties - pre_allocated; |
| 13171 | Handle<Map> map = |
| 13172 | Map::CopyInitialMap(constructor_initial_map, instance_size, |
| 13173 | in_object_properties, unused_property_fields); |
| 13174 | map->set_new_target_is_base(false); |
| 13175 | |
| 13176 | JSFunction::SetInitialMap(function, map, prototype); |
| 13177 | map->SetConstructor(*constructor); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 13178 | map->set_construction_counter(Map::kNoSlackTracking); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13179 | map->StartInobjectSlackTracking(); |
| 13180 | return map; |
| 13181 | } |
| 13182 | } |
| 13183 | |
| 13184 | // Slow path, new.target is either a proxy or can't cache the map. |
| 13185 | // new.target.prototype is not guaranteed to be a JSReceiver, and may need to |
| 13186 | // fall back to the intrinsicDefaultProto. |
| 13187 | Handle<Object> prototype; |
| 13188 | if (new_target->IsJSFunction()) { |
| 13189 | Handle<JSFunction> function = Handle<JSFunction>::cast(new_target); |
| 13190 | // Make sure the new.target.prototype is cached. |
| 13191 | EnsureHasInitialMap(function); |
| 13192 | prototype = handle(function->prototype(), isolate); |
| 13193 | } else { |
| 13194 | Handle<String> prototype_string = isolate->factory()->prototype_string(); |
| 13195 | ASSIGN_RETURN_ON_EXCEPTION( |
| 13196 | isolate, prototype, |
| 13197 | JSReceiver::GetProperty(new_target, prototype_string), Map); |
| 13198 | // The above prototype lookup might change the constructor and its |
| 13199 | // prototype, hence we have to reload the initial map. |
| 13200 | EnsureHasInitialMap(constructor); |
| 13201 | constructor_initial_map = handle(constructor->initial_map(), isolate); |
| 13202 | } |
| 13203 | |
| 13204 | // If prototype is not a JSReceiver, fetch the intrinsicDefaultProto from the |
| 13205 | // correct realm. Rather than directly fetching the .prototype, we fetch the |
| 13206 | // constructor that points to the .prototype. This relies on |
| 13207 | // constructor.prototype being FROZEN for those constructors. |
| 13208 | if (!prototype->IsJSReceiver()) { |
| 13209 | Handle<Context> context; |
| 13210 | ASSIGN_RETURN_ON_EXCEPTION(isolate, context, |
| 13211 | JSReceiver::GetFunctionRealm(new_target), Map); |
| 13212 | DCHECK(context->IsNativeContext()); |
| 13213 | Handle<Object> maybe_index = JSReceiver::GetDataProperty( |
| 13214 | constructor, isolate->factory()->native_context_index_symbol()); |
| 13215 | int index = maybe_index->IsSmi() ? Smi::cast(*maybe_index)->value() |
| 13216 | : Context::OBJECT_FUNCTION_INDEX; |
| 13217 | Handle<JSFunction> realm_constructor(JSFunction::cast(context->get(index))); |
| 13218 | prototype = handle(realm_constructor->prototype(), isolate); |
| 13219 | } |
| 13220 | |
| 13221 | Handle<Map> map = Map::CopyInitialMap(constructor_initial_map); |
| 13222 | map->set_new_target_is_base(false); |
| 13223 | DCHECK(prototype->IsJSReceiver()); |
| 13224 | if (map->prototype() != *prototype) { |
| 13225 | Map::SetPrototype(map, prototype, FAST_PROTOTYPE); |
| 13226 | } |
| 13227 | map->SetConstructor(*constructor); |
| 13228 | return map; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13229 | } |
| 13230 | |
| 13231 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13232 | void JSFunction::PrintName(FILE* out) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13233 | base::SmartArrayPointer<char> name = shared()->DebugName()->ToCString(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13234 | PrintF(out, "%s", name.get()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13235 | } |
| 13236 | |
| 13237 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13238 | Handle<String> JSFunction::GetName(Handle<JSFunction> function) { |
| 13239 | Isolate* isolate = function->GetIsolate(); |
| 13240 | Handle<Object> name = |
| 13241 | JSReceiver::GetDataProperty(function, isolate->factory()->name_string()); |
| 13242 | if (name->IsString()) return Handle<String>::cast(name); |
| 13243 | return handle(function->shared()->DebugName(), isolate); |
| 13244 | } |
| 13245 | |
| 13246 | |
| 13247 | Handle<String> JSFunction::GetDebugName(Handle<JSFunction> function) { |
| 13248 | Isolate* isolate = function->GetIsolate(); |
| 13249 | Handle<Object> name = JSReceiver::GetDataProperty( |
| 13250 | function, isolate->factory()->display_name_string()); |
| 13251 | if (name->IsString()) return Handle<String>::cast(name); |
| 13252 | return JSFunction::GetName(function); |
| 13253 | } |
| 13254 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 13255 | void JSFunction::SetName(Handle<JSFunction> function, Handle<Name> name, |
| 13256 | Handle<String> prefix) { |
| 13257 | Isolate* isolate = function->GetIsolate(); |
| 13258 | Handle<String> function_name = Name::ToFunctionName(name).ToHandleChecked(); |
| 13259 | if (prefix->length() > 0) { |
| 13260 | IncrementalStringBuilder builder(isolate); |
| 13261 | builder.AppendString(prefix); |
| 13262 | builder.AppendCharacter(' '); |
| 13263 | builder.AppendString(function_name); |
| 13264 | function_name = builder.Finish().ToHandleChecked(); |
| 13265 | } |
| 13266 | JSObject::DefinePropertyOrElementIgnoreAttributes( |
| 13267 | function, isolate->factory()->name_string(), function_name, |
| 13268 | static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY)) |
| 13269 | .ToHandleChecked(); |
| 13270 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 13299 | // static |
| 13300 | MaybeHandle<String> JSBoundFunction::GetName(Isolate* isolate, |
| 13301 | Handle<JSBoundFunction> function) { |
| 13302 | Handle<String> prefix = isolate->factory()->bound__string(); |
| 13303 | if (!function->bound_target_function()->IsJSFunction()) return prefix; |
| 13304 | Handle<JSFunction> target(JSFunction::cast(function->bound_target_function()), |
| 13305 | isolate); |
| 13306 | Handle<Object> target_name = JSFunction::GetName(target); |
| 13307 | if (!target_name->IsString()) return prefix; |
| 13308 | Factory* factory = isolate->factory(); |
| 13309 | return factory->NewConsString(prefix, Handle<String>::cast(target_name)); |
| 13310 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13311 | |
| 13312 | // static |
| 13313 | Handle<String> JSFunction::ToString(Handle<JSFunction> function) { |
| 13314 | Isolate* const isolate = function->GetIsolate(); |
| 13315 | Handle<SharedFunctionInfo> shared_info(function->shared(), isolate); |
| 13316 | |
| 13317 | // Check if {function} should hide its source code. |
| 13318 | if (!shared_info->script()->IsScript() || |
| 13319 | Script::cast(shared_info->script())->hide_source()) { |
| 13320 | return NativeCodeFunctionSourceString(shared_info); |
| 13321 | } |
| 13322 | |
| 13323 | // Check if we should print {function} as a class. |
| 13324 | Handle<Object> class_start_position = JSReceiver::GetDataProperty( |
| 13325 | function, isolate->factory()->class_start_position_symbol()); |
| 13326 | if (class_start_position->IsSmi()) { |
| 13327 | Handle<Object> class_end_position = JSReceiver::GetDataProperty( |
| 13328 | function, isolate->factory()->class_end_position_symbol()); |
| 13329 | Handle<String> script_source( |
| 13330 | String::cast(Script::cast(shared_info->script())->source()), isolate); |
| 13331 | return isolate->factory()->NewSubString( |
| 13332 | script_source, Handle<Smi>::cast(class_start_position)->value(), |
| 13333 | Handle<Smi>::cast(class_end_position)->value()); |
| 13334 | } |
| 13335 | |
| 13336 | // Check if we have source code for the {function}. |
| 13337 | if (!shared_info->HasSourceCode()) { |
| 13338 | return NativeCodeFunctionSourceString(shared_info); |
| 13339 | } |
| 13340 | |
| 13341 | IncrementalStringBuilder builder(isolate); |
| 13342 | if (!shared_info->is_arrow()) { |
| 13343 | if (shared_info->is_concise_method()) { |
| 13344 | if (shared_info->is_generator()) builder.AppendCharacter('*'); |
| 13345 | } else { |
| 13346 | if (shared_info->is_generator()) { |
| 13347 | builder.AppendCString("function* "); |
| 13348 | } else { |
| 13349 | builder.AppendCString("function "); |
| 13350 | } |
| 13351 | } |
| 13352 | if (shared_info->name_should_print_as_anonymous()) { |
| 13353 | builder.AppendCString("anonymous"); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 13354 | } else if (!shared_info->is_anonymous_expression()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13355 | builder.AppendString(handle(String::cast(shared_info->name()), isolate)); |
| 13356 | } |
| 13357 | } |
| 13358 | builder.AppendString(Handle<String>::cast(shared_info->GetSourceCode())); |
| 13359 | return builder.Finish().ToHandleChecked(); |
| 13360 | } |
| 13361 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13362 | void Oddball::Initialize(Isolate* isolate, Handle<Oddball> oddball, |
| 13363 | const char* to_string, Handle<Object> to_number, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 13364 | bool to_boolean, const char* type_of, byte kind) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13365 | Handle<String> internalized_to_string = |
| 13366 | isolate->factory()->InternalizeUtf8String(to_string); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13367 | Handle<String> internalized_type_of = |
| 13368 | isolate->factory()->InternalizeUtf8String(type_of); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 13369 | oddball->set_to_boolean(isolate->heap()->ToBoolean(to_boolean)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13370 | oddball->set_to_number(*to_number); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13371 | oddball->set_to_string(*internalized_to_string); |
| 13372 | oddball->set_type_of(*internalized_type_of); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13373 | oddball->set_kind(kind); |
| 13374 | } |
| 13375 | |
| 13376 | |
| 13377 | void Script::InitLineEnds(Handle<Script> script) { |
| 13378 | if (!script->line_ends()->IsUndefined()) return; |
| 13379 | |
| 13380 | Isolate* isolate = script->GetIsolate(); |
| 13381 | |
| 13382 | if (!script->source()->IsString()) { |
| 13383 | DCHECK(script->source()->IsUndefined()); |
| 13384 | Handle<FixedArray> empty = isolate->factory()->NewFixedArray(0); |
| 13385 | script->set_line_ends(*empty); |
| 13386 | DCHECK(script->line_ends()->IsFixedArray()); |
| 13387 | return; |
| 13388 | } |
| 13389 | |
| 13390 | Handle<String> src(String::cast(script->source()), isolate); |
| 13391 | |
| 13392 | Handle<FixedArray> array = String::CalculateLineEnds(src, true); |
| 13393 | |
| 13394 | if (*array != isolate->heap()->empty_fixed_array()) { |
| 13395 | array->set_map(isolate->heap()->fixed_cow_array_map()); |
| 13396 | } |
| 13397 | |
| 13398 | script->set_line_ends(*array); |
| 13399 | DCHECK(script->line_ends()->IsFixedArray()); |
| 13400 | } |
| 13401 | |
| 13402 | |
| 13403 | int Script::GetColumnNumber(Handle<Script> script, int code_pos) { |
| 13404 | int line_number = GetLineNumber(script, code_pos); |
| 13405 | if (line_number == -1) return -1; |
| 13406 | |
| 13407 | DisallowHeapAllocation no_allocation; |
| 13408 | FixedArray* line_ends_array = FixedArray::cast(script->line_ends()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13409 | line_number = line_number - script->line_offset(); |
| 13410 | if (line_number == 0) return code_pos + script->column_offset(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13411 | int prev_line_end_pos = |
| 13412 | Smi::cast(line_ends_array->get(line_number - 1))->value(); |
| 13413 | return code_pos - (prev_line_end_pos + 1); |
| 13414 | } |
| 13415 | |
| 13416 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13417 | int Script::GetLineNumberWithArray(int code_pos) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13418 | DisallowHeapAllocation no_allocation; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13419 | DCHECK(line_ends()->IsFixedArray()); |
| 13420 | FixedArray* line_ends_array = FixedArray::cast(line_ends()); |
| 13421 | int line_ends_len = line_ends_array->length(); |
| 13422 | if (line_ends_len == 0) return -1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13423 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13424 | if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) { |
| 13425 | return line_offset(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13426 | } |
| 13427 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13428 | int left = 0; |
| 13429 | int right = line_ends_len; |
| 13430 | while (int half = (right - left) / 2) { |
| 13431 | if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) { |
| 13432 | right -= half; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13433 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13434 | left += half; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13435 | } |
| 13436 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13437 | return right + line_offset(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13438 | } |
| 13439 | |
| 13440 | |
| 13441 | int Script::GetLineNumber(Handle<Script> script, int code_pos) { |
| 13442 | InitLineEnds(script); |
| 13443 | return script->GetLineNumberWithArray(code_pos); |
| 13444 | } |
| 13445 | |
| 13446 | |
| 13447 | int Script::GetLineNumber(int code_pos) { |
| 13448 | DisallowHeapAllocation no_allocation; |
| 13449 | if (!line_ends()->IsUndefined()) return GetLineNumberWithArray(code_pos); |
| 13450 | |
| 13451 | // Slow mode: we do not have line_ends. We have to iterate through source. |
| 13452 | if (!source()->IsString()) return -1; |
| 13453 | |
| 13454 | String* source_string = String::cast(source()); |
| 13455 | int line = 0; |
| 13456 | int len = source_string->length(); |
| 13457 | for (int pos = 0; pos < len; pos++) { |
| 13458 | if (pos == code_pos) break; |
| 13459 | if (source_string->Get(pos) == '\n') line++; |
| 13460 | } |
| 13461 | return line; |
| 13462 | } |
| 13463 | |
| 13464 | |
| 13465 | Handle<Object> Script::GetNameOrSourceURL(Handle<Script> script) { |
| 13466 | Isolate* isolate = script->GetIsolate(); |
| 13467 | Handle<String> name_or_source_url_key = |
| 13468 | isolate->factory()->InternalizeOneByteString( |
| 13469 | STATIC_CHAR_VECTOR("nameOrSourceURL")); |
| 13470 | Handle<JSObject> script_wrapper = Script::GetWrapper(script); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 13471 | Handle<Object> property = |
| 13472 | JSReceiver::GetProperty(script_wrapper, name_or_source_url_key) |
| 13473 | .ToHandleChecked(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13474 | DCHECK(property->IsJSFunction()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13475 | Handle<Object> result; |
| 13476 | // Do not check against pending exception, since this function may be called |
| 13477 | // when an exception has already been pending. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13478 | if (!Execution::TryCall(isolate, property, script_wrapper, 0, NULL) |
| 13479 | .ToHandle(&result)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13480 | return isolate->factory()->undefined_value(); |
| 13481 | } |
| 13482 | return result; |
| 13483 | } |
| 13484 | |
| 13485 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13486 | Handle<JSObject> Script::GetWrapper(Handle<Script> script) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13487 | Isolate* isolate = script->GetIsolate(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 13488 | if (!script->wrapper()->IsUndefined()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13489 | DCHECK(script->wrapper()->IsWeakCell()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 13490 | Handle<WeakCell> cell(WeakCell::cast(script->wrapper())); |
| 13491 | if (!cell->cleared()) { |
| 13492 | // Return a handle for the existing script wrapper from the cache. |
| 13493 | return handle(JSObject::cast(cell->value())); |
| 13494 | } |
| 13495 | // If we found an empty WeakCell, that means the script wrapper was |
| 13496 | // GCed. We are not notified directly of that, so we decrement here |
| 13497 | // so that we at least don't count double for any given script. |
| 13498 | isolate->counters()->script_wrappers()->Decrement(); |
| 13499 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13500 | // Construct a new script wrapper. |
| 13501 | isolate->counters()->script_wrappers()->Increment(); |
| 13502 | Handle<JSFunction> constructor = isolate->script_function(); |
| 13503 | Handle<JSValue> result = |
| 13504 | Handle<JSValue>::cast(isolate->factory()->NewJSObject(constructor)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13505 | result->set_value(*script); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 13506 | Handle<WeakCell> cell = isolate->factory()->NewWeakCell(result); |
| 13507 | script->set_wrapper(*cell); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13508 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13509 | } |
| 13510 | |
| 13511 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13512 | MaybeHandle<SharedFunctionInfo> Script::FindSharedFunctionInfo( |
| 13513 | FunctionLiteral* fun) { |
| 13514 | WeakFixedArray::Iterator iterator(shared_function_infos()); |
| 13515 | SharedFunctionInfo* shared; |
| 13516 | while ((shared = iterator.Next<SharedFunctionInfo>())) { |
| 13517 | if (fun->function_token_position() == shared->function_token_position() && |
| 13518 | fun->start_position() == shared->start_position()) { |
| 13519 | return Handle<SharedFunctionInfo>(shared); |
| 13520 | } |
| 13521 | } |
| 13522 | return MaybeHandle<SharedFunctionInfo>(); |
| 13523 | } |
| 13524 | |
| 13525 | |
| 13526 | Script::Iterator::Iterator(Isolate* isolate) |
| 13527 | : iterator_(isolate->heap()->script_list()) {} |
| 13528 | |
| 13529 | |
| 13530 | Script* Script::Iterator::Next() { return iterator_.Next<Script>(); } |
| 13531 | |
| 13532 | |
| 13533 | SharedFunctionInfo::Iterator::Iterator(Isolate* isolate) |
| 13534 | : script_iterator_(isolate), |
| 13535 | sfi_iterator_(isolate->heap()->noscript_shared_function_infos()) {} |
| 13536 | |
| 13537 | |
| 13538 | bool SharedFunctionInfo::Iterator::NextScript() { |
| 13539 | Script* script = script_iterator_.Next(); |
| 13540 | if (script == NULL) return false; |
| 13541 | sfi_iterator_.Reset(script->shared_function_infos()); |
| 13542 | return true; |
| 13543 | } |
| 13544 | |
| 13545 | |
| 13546 | SharedFunctionInfo* SharedFunctionInfo::Iterator::Next() { |
| 13547 | do { |
| 13548 | SharedFunctionInfo* next = sfi_iterator_.Next<SharedFunctionInfo>(); |
| 13549 | if (next != NULL) return next; |
| 13550 | } while (NextScript()); |
| 13551 | return NULL; |
| 13552 | } |
| 13553 | |
| 13554 | |
| 13555 | void SharedFunctionInfo::SetScript(Handle<SharedFunctionInfo> shared, |
| 13556 | Handle<Object> script_object) { |
| 13557 | if (shared->script() == *script_object) return; |
| 13558 | Isolate* isolate = shared->GetIsolate(); |
| 13559 | |
| 13560 | // Add shared function info to new script's list. If a collection occurs, |
| 13561 | // the shared function info may be temporarily in two lists. |
| 13562 | // This is okay because the gc-time processing of these lists can tolerate |
| 13563 | // duplicates. |
| 13564 | Handle<Object> list; |
| 13565 | if (script_object->IsScript()) { |
| 13566 | Handle<Script> script = Handle<Script>::cast(script_object); |
| 13567 | list = handle(script->shared_function_infos(), isolate); |
| 13568 | } else { |
| 13569 | list = isolate->factory()->noscript_shared_function_infos(); |
| 13570 | } |
| 13571 | |
| 13572 | #ifdef DEBUG |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 13573 | if (FLAG_enable_slow_asserts) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13574 | WeakFixedArray::Iterator iterator(*list); |
| 13575 | SharedFunctionInfo* next; |
| 13576 | while ((next = iterator.Next<SharedFunctionInfo>())) { |
| 13577 | DCHECK_NE(next, *shared); |
| 13578 | } |
| 13579 | } |
| 13580 | #endif // DEBUG |
| 13581 | list = WeakFixedArray::Add(list, shared); |
| 13582 | |
| 13583 | if (script_object->IsScript()) { |
| 13584 | Handle<Script> script = Handle<Script>::cast(script_object); |
| 13585 | script->set_shared_function_infos(*list); |
| 13586 | } else { |
| 13587 | isolate->heap()->SetRootNoScriptSharedFunctionInfos(*list); |
| 13588 | } |
| 13589 | |
| 13590 | // Remove shared function info from old script's list. |
| 13591 | if (shared->script()->IsScript()) { |
| 13592 | Script* old_script = Script::cast(shared->script()); |
| 13593 | if (old_script->shared_function_infos()->IsWeakFixedArray()) { |
| 13594 | WeakFixedArray* list = |
| 13595 | WeakFixedArray::cast(old_script->shared_function_infos()); |
| 13596 | list->Remove(shared); |
| 13597 | } |
| 13598 | } else { |
| 13599 | // Remove shared function info from root array. |
| 13600 | Object* list = isolate->heap()->noscript_shared_function_infos(); |
| 13601 | CHECK(WeakFixedArray::cast(list)->Remove(shared)); |
| 13602 | } |
| 13603 | |
| 13604 | // Finally set new script. |
| 13605 | shared->set_script(*script_object); |
| 13606 | } |
| 13607 | |
| 13608 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 13609 | String* SharedFunctionInfo::DebugName() { |
| 13610 | Object* n = name(); |
| 13611 | if (!n->IsString() || String::cast(n)->length() == 0) return inferred_name(); |
| 13612 | return String::cast(n); |
| 13613 | } |
| 13614 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 13615 | // The filter is a pattern that matches function names in this way: |
| 13616 | // "*" all; the default |
| 13617 | // "-" all but the top-level function |
| 13618 | // "-name" all but the function "name" |
| 13619 | // "" only the top-level function |
| 13620 | // "name" only the function "name" |
| 13621 | // "name*" only functions starting with "name" |
| 13622 | // "~" none; the tilde is not an identifier |
| 13623 | bool SharedFunctionInfo::PassesFilter(const char* raw_filter) { |
| 13624 | if (*raw_filter == '*') return true; |
| 13625 | String* name = DebugName(); |
| 13626 | Vector<const char> filter = CStrVector(raw_filter); |
| 13627 | if (filter.length() == 0) return name->length() == 0; |
| 13628 | if (filter[0] == '-') { |
| 13629 | // Negative filter. |
| 13630 | if (filter.length() == 1) { |
| 13631 | return (name->length() != 0); |
| 13632 | } else if (name->IsUtf8EqualTo(filter.SubVector(1, filter.length()))) { |
| 13633 | return false; |
| 13634 | } |
| 13635 | if (filter[filter.length() - 1] == '*' && |
| 13636 | name->IsUtf8EqualTo(filter.SubVector(1, filter.length() - 1), true)) { |
| 13637 | return false; |
| 13638 | } |
| 13639 | return true; |
| 13640 | |
| 13641 | } else if (name->IsUtf8EqualTo(filter)) { |
| 13642 | return true; |
| 13643 | } |
| 13644 | if (filter[filter.length() - 1] == '*' && |
| 13645 | name->IsUtf8EqualTo(filter.SubVector(0, filter.length() - 1), true)) { |
| 13646 | return true; |
| 13647 | } |
| 13648 | return false; |
| 13649 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 13650 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13651 | bool SharedFunctionInfo::HasSourceCode() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13652 | return !script()->IsUndefined() && |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 13653 | !reinterpret_cast<Script*>(script())->source()->IsUndefined(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13654 | } |
| 13655 | |
| 13656 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13657 | Handle<Object> SharedFunctionInfo::GetSourceCode() { |
| 13658 | if (!HasSourceCode()) return GetIsolate()->factory()->undefined_value(); |
| 13659 | Handle<String> source(String::cast(Script::cast(script())->source())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13660 | return GetIsolate()->factory()->NewSubString( |
| 13661 | source, start_position(), end_position()); |
| 13662 | } |
| 13663 | |
| 13664 | |
| 13665 | bool SharedFunctionInfo::IsInlineable() { |
| 13666 | // Check that the function has a script associated with it. |
| 13667 | if (!script()->IsScript()) return false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13668 | return !optimization_disabled(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13669 | } |
| 13670 | |
| 13671 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13672 | int SharedFunctionInfo::SourceSize() { |
| 13673 | return end_position() - start_position(); |
| 13674 | } |
| 13675 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 13676 | void JSFunction::CalculateInstanceSizeHelper(InstanceType instance_type, |
| 13677 | int requested_internal_fields, |
| 13678 | int requested_in_object_properties, |
| 13679 | int* instance_size, |
| 13680 | int* in_object_properties) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13681 | int header_size = JSObject::GetHeaderSize(instance_type); |
| 13682 | DCHECK_LE(requested_internal_fields, |
| 13683 | (JSObject::kMaxInstanceSize - header_size) >> kPointerSizeLog2); |
| 13684 | *instance_size = |
| 13685 | Min(header_size + |
| 13686 | ((requested_internal_fields + requested_in_object_properties) |
| 13687 | << kPointerSizeLog2), |
| 13688 | JSObject::kMaxInstanceSize); |
| 13689 | *in_object_properties = ((*instance_size - header_size) >> kPointerSizeLog2) - |
| 13690 | requested_internal_fields; |
| 13691 | } |
| 13692 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13693 | |
| 13694 | void JSFunction::CalculateInstanceSize(InstanceType instance_type, |
| 13695 | int requested_internal_fields, |
| 13696 | int* instance_size, |
| 13697 | int* in_object_properties) { |
| 13698 | CalculateInstanceSizeHelper(instance_type, requested_internal_fields, |
| 13699 | shared()->expected_nof_properties(), |
| 13700 | instance_size, in_object_properties); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13701 | } |
| 13702 | |
| 13703 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13704 | void JSFunction::CalculateInstanceSizeForDerivedClass( |
| 13705 | InstanceType instance_type, int requested_internal_fields, |
| 13706 | int* instance_size, int* in_object_properties) { |
| 13707 | Isolate* isolate = GetIsolate(); |
| 13708 | int expected_nof_properties = 0; |
| 13709 | for (PrototypeIterator iter(isolate, this, |
| 13710 | PrototypeIterator::START_AT_RECEIVER); |
| 13711 | !iter.IsAtEnd(); iter.Advance()) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 13712 | JSReceiver* current = iter.GetCurrent<JSReceiver>(); |
| 13713 | if (!current->IsJSFunction()) break; |
| 13714 | JSFunction* func = JSFunction::cast(current); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13715 | SharedFunctionInfo* shared = func->shared(); |
| 13716 | expected_nof_properties += shared->expected_nof_properties(); |
| 13717 | if (!IsSubclassConstructor(shared->kind())) { |
| 13718 | break; |
| 13719 | } |
| 13720 | } |
| 13721 | CalculateInstanceSizeHelper(instance_type, requested_internal_fields, |
| 13722 | expected_nof_properties, instance_size, |
| 13723 | in_object_properties); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13724 | } |
| 13725 | |
| 13726 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13727 | // Output the source code without any allocation in the heap. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 13728 | std::ostream& operator<<(std::ostream& os, const SourceCodeOf& v) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13729 | const SharedFunctionInfo* s = v.value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13730 | // For some native functions there is no source. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13731 | if (!s->HasSourceCode()) return os << "<No Source>"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13732 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 13733 | // Get the source for the script which this function came from. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13734 | // Don't use String::cast because we don't want more assertion errors while |
| 13735 | // we are already creating a stack dump. |
| 13736 | String* script_source = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13737 | reinterpret_cast<String*>(Script::cast(s->script())->source()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13738 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13739 | if (!script_source->LooksValid()) return os << "<Invalid Source>"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13740 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13741 | if (!s->is_toplevel()) { |
| 13742 | os << "function "; |
| 13743 | Object* name = s->name(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13744 | if (name->IsString() && String::cast(name)->length() > 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13745 | String::cast(name)->PrintUC16(os); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13746 | } |
| 13747 | } |
| 13748 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13749 | int len = s->end_position() - s->start_position(); |
| 13750 | if (len <= v.max_length || v.max_length < 0) { |
| 13751 | script_source->PrintUC16(os, s->start_position(), s->end_position()); |
| 13752 | return os; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13753 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13754 | script_source->PrintUC16(os, s->start_position(), |
| 13755 | s->start_position() + v.max_length); |
| 13756 | return os << "...\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13757 | } |
| 13758 | } |
| 13759 | |
| 13760 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13761 | static bool IsCodeEquivalent(Code* code, Code* recompiled) { |
| 13762 | if (code->instruction_size() != recompiled->instruction_size()) return false; |
| 13763 | ByteArray* code_relocation = code->relocation_info(); |
| 13764 | ByteArray* recompiled_relocation = recompiled->relocation_info(); |
| 13765 | int length = code_relocation->length(); |
| 13766 | if (length != recompiled_relocation->length()) return false; |
| 13767 | int compare = memcmp(code_relocation->GetDataStartAddress(), |
| 13768 | recompiled_relocation->GetDataStartAddress(), |
| 13769 | length); |
| 13770 | return compare == 0; |
| 13771 | } |
| 13772 | |
| 13773 | |
| 13774 | void SharedFunctionInfo::EnableDeoptimizationSupport(Code* recompiled) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13775 | DCHECK(!has_deoptimization_support()); |
| 13776 | DisallowHeapAllocation no_allocation; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13777 | Code* code = this->code(); |
| 13778 | if (IsCodeEquivalent(code, recompiled)) { |
| 13779 | // Copy the deoptimization data from the recompiled code. |
| 13780 | code->set_deoptimization_data(recompiled->deoptimization_data()); |
| 13781 | code->set_has_deoptimization_support(true); |
| 13782 | } else { |
| 13783 | // TODO(3025757): In case the recompiled isn't equivalent to the |
| 13784 | // old code, we have to replace it. We should try to avoid this |
| 13785 | // altogether because it flushes valuable type feedback by |
| 13786 | // effectively resetting all IC state. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13787 | ReplaceCode(recompiled); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13788 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13789 | DCHECK(has_deoptimization_support()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13790 | } |
| 13791 | |
| 13792 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13793 | void SharedFunctionInfo::DisableOptimization(BailoutReason reason) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 13794 | // Disable optimization for the shared function info and mark the |
| 13795 | // code as non-optimizable. The marker on the shared function info |
| 13796 | // is there because we flush non-optimized code thereby loosing the |
| 13797 | // non-optimizable information for the code. When the code is |
| 13798 | // regenerated and set on the shared function info it is marked as |
| 13799 | // non-optimizable if optimization is disabled for the shared |
| 13800 | // function info. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 13801 | DCHECK(reason != kNoReason); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 13802 | set_optimization_disabled(true); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 13803 | set_disable_optimization_reason(reason); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13804 | // Code should be the lazy compilation stub or else unoptimized. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 13805 | DCHECK(abstract_code()->kind() == AbstractCode::FUNCTION || |
| 13806 | abstract_code()->kind() == AbstractCode::INTERPRETED_FUNCTION || |
| 13807 | abstract_code()->kind() == AbstractCode::BUILTIN); |
| 13808 | PROFILE(GetIsolate(), CodeDisableOptEvent(abstract_code(), this)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 13809 | if (FLAG_trace_opt) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13810 | PrintF("[disabled optimization for "); |
| 13811 | ShortPrint(); |
| 13812 | PrintF(", reason: %s]\n", GetBailoutReason(reason)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 13813 | } |
| 13814 | } |
| 13815 | |
| 13816 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13817 | void SharedFunctionInfo::InitFromFunctionLiteral( |
| 13818 | Handle<SharedFunctionInfo> shared_info, FunctionLiteral* lit) { |
| 13819 | shared_info->set_length(lit->scope()->default_function_length()); |
| 13820 | shared_info->set_internal_formal_parameter_count(lit->parameter_count()); |
| 13821 | shared_info->set_function_token_position(lit->function_token_position()); |
| 13822 | shared_info->set_start_position(lit->start_position()); |
| 13823 | shared_info->set_end_position(lit->end_position()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 13824 | shared_info->set_is_declaration(lit->is_declaration()); |
| 13825 | shared_info->set_is_named_expression(lit->is_named_expression()); |
| 13826 | shared_info->set_is_anonymous_expression(lit->is_anonymous_expression()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13827 | shared_info->set_inferred_name(*lit->inferred_name()); |
| 13828 | shared_info->set_allows_lazy_compilation(lit->AllowsLazyCompilation()); |
| 13829 | shared_info->set_allows_lazy_compilation_without_context( |
| 13830 | lit->AllowsLazyCompilationWithoutContext()); |
| 13831 | shared_info->set_language_mode(lit->language_mode()); |
| 13832 | shared_info->set_uses_arguments(lit->scope()->arguments() != NULL); |
| 13833 | shared_info->set_has_duplicate_parameters(lit->has_duplicate_parameters()); |
| 13834 | shared_info->set_ast_node_count(lit->ast_node_count()); |
| 13835 | shared_info->set_is_function(lit->is_function()); |
| 13836 | if (lit->dont_optimize_reason() != kNoReason) { |
| 13837 | shared_info->DisableOptimization(lit->dont_optimize_reason()); |
| 13838 | } |
| 13839 | shared_info->set_dont_crankshaft(lit->flags() & |
| 13840 | AstProperties::kDontCrankshaft); |
| 13841 | shared_info->set_kind(lit->kind()); |
| 13842 | if (!IsConstructable(lit->kind(), lit->language_mode())) { |
| 13843 | shared_info->set_construct_stub( |
| 13844 | *shared_info->GetIsolate()->builtins()->ConstructedNonConstructable()); |
| 13845 | } |
| 13846 | shared_info->set_needs_home_object(lit->scope()->NeedsHomeObject()); |
| 13847 | shared_info->set_asm_function(lit->scope()->asm_function()); |
| 13848 | } |
| 13849 | |
| 13850 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13851 | bool SharedFunctionInfo::VerifyBailoutId(BailoutId id) { |
| 13852 | DCHECK(!id.IsNone()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13853 | Code* unoptimized = code(); |
| 13854 | DeoptimizationOutputData* data = |
| 13855 | DeoptimizationOutputData::cast(unoptimized->deoptimization_data()); |
| 13856 | unsigned ignore = Deoptimizer::GetOutputInfo(data, id, this); |
| 13857 | USE(ignore); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13858 | return true; // Return true if there was no DCHECK. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13859 | } |
| 13860 | |
| 13861 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13862 | void Map::StartInobjectSlackTracking() { |
| 13863 | DCHECK(!IsInobjectSlackTrackingInProgress()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13864 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13865 | // No tracking during the snapshot construction phase. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13866 | Isolate* isolate = GetIsolate(); |
| 13867 | if (isolate->serializer_enabled()) return; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13868 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13869 | if (unused_property_fields() == 0) return; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13870 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13871 | set_construction_counter(Map::kSlackTrackingCounterStart); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13872 | } |
| 13873 | |
| 13874 | |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 13875 | void SharedFunctionInfo::ResetForNewContext(int new_ic_age) { |
| 13876 | code()->ClearInlineCaches(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13877 | // If we clear ICs, we need to clear the type feedback vector too, since |
| 13878 | // CallICs are synced with a feedback vector slot. |
| 13879 | ClearTypeFeedbackInfo(); |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 13880 | set_ic_age(new_ic_age); |
| 13881 | if (code()->kind() == Code::FUNCTION) { |
| 13882 | code()->set_profiler_ticks(0); |
| 13883 | if (optimization_disabled() && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13884 | opt_count() >= FLAG_max_opt_count) { |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 13885 | // Re-enable optimizations if they were disabled due to opt_count limit. |
| 13886 | set_optimization_disabled(false); |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 13887 | } |
| 13888 | set_opt_count(0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13889 | set_deopt_count(0); |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 13890 | } |
| 13891 | } |
| 13892 | |
| 13893 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13894 | int SharedFunctionInfo::SearchOptimizedCodeMapEntry(Context* native_context, |
| 13895 | BailoutId osr_ast_id) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13896 | DisallowHeapAllocation no_gc; |
| 13897 | DCHECK(native_context->IsNativeContext()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13898 | if (!OptimizedCodeMapIsCleared()) { |
| 13899 | FixedArray* optimized_code_map = this->optimized_code_map(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13900 | int length = optimized_code_map->length(); |
| 13901 | Smi* osr_ast_id_smi = Smi::FromInt(osr_ast_id.ToInt()); |
| 13902 | for (int i = kEntriesStart; i < length; i += kEntryLength) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13903 | if (WeakCell::cast(optimized_code_map->get(i + kContextOffset)) |
| 13904 | ->value() == native_context && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13905 | optimized_code_map->get(i + kOsrAstIdOffset) == osr_ast_id_smi) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13906 | return i; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13907 | } |
| 13908 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13909 | Object* shared_code = |
| 13910 | WeakCell::cast(optimized_code_map->get(kSharedCodeIndex))->value(); |
| 13911 | if (shared_code->IsCode() && osr_ast_id.IsNone()) { |
| 13912 | return kSharedCodeIndex; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13913 | } |
| 13914 | } |
| 13915 | return -1; |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 13916 | } |
| 13917 | |
| 13918 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13919 | CodeAndLiterals SharedFunctionInfo::SearchOptimizedCodeMap( |
| 13920 | Context* native_context, BailoutId osr_ast_id) { |
| 13921 | CodeAndLiterals result = {nullptr, nullptr}; |
| 13922 | int entry = SearchOptimizedCodeMapEntry(native_context, osr_ast_id); |
| 13923 | if (entry != kNotFound) { |
| 13924 | FixedArray* code_map = optimized_code_map(); |
| 13925 | if (entry == kSharedCodeIndex) { |
| 13926 | // We know the weak cell isn't cleared because we made sure of it in |
| 13927 | // SearchOptimizedCodeMapEntry and performed no allocations since that |
| 13928 | // call. |
| 13929 | result = { |
| 13930 | Code::cast(WeakCell::cast(code_map->get(kSharedCodeIndex))->value()), |
| 13931 | nullptr}; |
| 13932 | } else { |
| 13933 | DCHECK_LE(entry + kEntryLength, code_map->length()); |
| 13934 | WeakCell* cell = WeakCell::cast(code_map->get(entry + kCachedCodeOffset)); |
| 13935 | WeakCell* literals_cell = |
| 13936 | WeakCell::cast(code_map->get(entry + kLiteralsOffset)); |
| 13937 | |
| 13938 | result = {cell->cleared() ? nullptr : Code::cast(cell->value()), |
| 13939 | literals_cell->cleared() |
| 13940 | ? nullptr |
| 13941 | : LiteralsArray::cast(literals_cell->value())}; |
| 13942 | } |
| 13943 | } |
| 13944 | if (FLAG_trace_opt && !OptimizedCodeMapIsCleared() && |
| 13945 | result.code == nullptr) { |
| 13946 | PrintF("[didn't find optimized code in optimized code map for "); |
| 13947 | ShortPrint(); |
| 13948 | PrintF("]\n"); |
| 13949 | } |
| 13950 | return result; |
| 13951 | } |
| 13952 | |
| 13953 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13954 | #define DECLARE_TAG(ignore1, name, ignore2) name, |
| 13955 | const char* const VisitorSynchronization::kTags[ |
| 13956 | VisitorSynchronization::kNumberOfSyncTags] = { |
| 13957 | VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG) |
| 13958 | }; |
| 13959 | #undef DECLARE_TAG |
| 13960 | |
| 13961 | |
| 13962 | #define DECLARE_TAG(ignore1, ignore2, name) name, |
| 13963 | const char* const VisitorSynchronization::kTagNames[ |
| 13964 | VisitorSynchronization::kNumberOfSyncTags] = { |
| 13965 | VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG) |
| 13966 | }; |
| 13967 | #undef DECLARE_TAG |
| 13968 | |
| 13969 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13970 | void ObjectVisitor::VisitCodeTarget(RelocInfo* rinfo) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13971 | DCHECK(RelocInfo::IsCodeTarget(rinfo->rmode())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13972 | Object* target = Code::GetCodeFromTargetAddress(rinfo->target_address()); |
| 13973 | Object* old_target = target; |
| 13974 | VisitPointer(&target); |
| 13975 | CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target. |
| 13976 | } |
| 13977 | |
| 13978 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13979 | void ObjectVisitor::VisitCodeAgeSequence(RelocInfo* rinfo) { |
| 13980 | DCHECK(RelocInfo::IsCodeAgeSequence(rinfo->rmode())); |
| 13981 | Object* stub = rinfo->code_age_stub(); |
| 13982 | if (stub) { |
| 13983 | VisitPointer(&stub); |
| 13984 | } |
| 13985 | } |
| 13986 | |
| 13987 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 13988 | void ObjectVisitor::VisitCodeEntry(Address entry_address) { |
| 13989 | Object* code = Code::GetObjectFromEntryAddress(entry_address); |
| 13990 | Object* old_code = code; |
| 13991 | VisitPointer(&code); |
| 13992 | if (code != old_code) { |
| 13993 | Memory::Address_at(entry_address) = reinterpret_cast<Code*>(code)->entry(); |
| 13994 | } |
| 13995 | } |
| 13996 | |
| 13997 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13998 | void ObjectVisitor::VisitCell(RelocInfo* rinfo) { |
| 13999 | DCHECK(rinfo->rmode() == RelocInfo::CELL); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14000 | Object* cell = rinfo->target_cell(); |
| 14001 | Object* old_cell = cell; |
| 14002 | VisitPointer(&cell); |
| 14003 | if (cell != old_cell) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14004 | rinfo->set_target_cell(reinterpret_cast<Cell*>(cell)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14005 | } |
| 14006 | } |
| 14007 | |
| 14008 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14009 | void ObjectVisitor::VisitDebugTarget(RelocInfo* rinfo) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14010 | DCHECK(RelocInfo::IsDebugBreakSlot(rinfo->rmode()) && |
| 14011 | rinfo->IsPatchedDebugBreakSlotSequence()); |
| 14012 | Object* target = Code::GetCodeFromTargetAddress(rinfo->debug_call_address()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14013 | Object* old_target = target; |
| 14014 | VisitPointer(&target); |
| 14015 | CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target. |
| 14016 | } |
| 14017 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14018 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14019 | void ObjectVisitor::VisitEmbeddedPointer(RelocInfo* rinfo) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14020 | DCHECK(rinfo->rmode() == RelocInfo::EMBEDDED_OBJECT); |
| 14021 | Object* p = rinfo->target_object(); |
| 14022 | VisitPointer(&p); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14023 | } |
| 14024 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14025 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14026 | void ObjectVisitor::VisitExternalReference(RelocInfo* rinfo) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14027 | Address p = rinfo->target_external_reference(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14028 | VisitExternalReference(&p); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14029 | } |
| 14030 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14031 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14032 | void Code::InvalidateRelocation() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14033 | InvalidateEmbeddedObjects(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14034 | set_relocation_info(GetHeap()->empty_byte_array()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14035 | } |
| 14036 | |
| 14037 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14038 | void Code::InvalidateEmbeddedObjects() { |
| 14039 | Object* undefined = GetHeap()->undefined_value(); |
| 14040 | Cell* undefined_cell = GetHeap()->undefined_cell(); |
| 14041 | int mode_mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) | |
| 14042 | RelocInfo::ModeMask(RelocInfo::CELL); |
| 14043 | for (RelocIterator it(this, mode_mask); !it.done(); it.next()) { |
| 14044 | RelocInfo::Mode mode = it.rinfo()->rmode(); |
| 14045 | if (mode == RelocInfo::EMBEDDED_OBJECT) { |
| 14046 | it.rinfo()->set_target_object(undefined, SKIP_WRITE_BARRIER); |
| 14047 | } else if (mode == RelocInfo::CELL) { |
| 14048 | it.rinfo()->set_target_cell(undefined_cell, SKIP_WRITE_BARRIER); |
| 14049 | } |
| 14050 | } |
| 14051 | } |
| 14052 | |
| 14053 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 14054 | void Code::Relocate(intptr_t delta) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14055 | for (RelocIterator it(this, RelocInfo::kApplyMask); !it.done(); it.next()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14056 | it.rinfo()->apply(delta); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14057 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14058 | Assembler::FlushICache(GetIsolate(), instruction_start(), instruction_size()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14059 | } |
| 14060 | |
| 14061 | |
| 14062 | void Code::CopyFrom(const CodeDesc& desc) { |
| 14063 | // copy code |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14064 | CopyBytes(instruction_start(), desc.buffer, |
| 14065 | static_cast<size_t>(desc.instr_size)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14066 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14067 | // copy reloc info |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14068 | CopyBytes(relocation_start(), |
| 14069 | desc.buffer + desc.buffer_size - desc.reloc_size, |
| 14070 | static_cast<size_t>(desc.reloc_size)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14071 | |
| 14072 | // unbox handles and relocate |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 14073 | intptr_t delta = instruction_start() - desc.buffer; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14074 | int mode_mask = RelocInfo::kCodeTargetMask | |
| 14075 | RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14076 | RelocInfo::ModeMask(RelocInfo::CELL) | |
| 14077 | RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY) | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14078 | RelocInfo::kApplyMask; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14079 | // Needed to find target_object and runtime_entry on X64 |
| 14080 | Assembler* origin = desc.origin; |
| 14081 | AllowDeferredHandleDereference embedding_raw_address; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14082 | for (RelocIterator it(this, mode_mask); !it.done(); it.next()) { |
| 14083 | RelocInfo::Mode mode = it.rinfo()->rmode(); |
| 14084 | if (mode == RelocInfo::EMBEDDED_OBJECT) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 14085 | Handle<Object> p = it.rinfo()->target_object_handle(origin); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 14086 | it.rinfo()->set_target_object(*p, UPDATE_WRITE_BARRIER, |
| 14087 | SKIP_ICACHE_FLUSH); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14088 | } else if (mode == RelocInfo::CELL) { |
| 14089 | Handle<Cell> cell = it.rinfo()->target_cell_handle(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 14090 | it.rinfo()->set_target_cell(*cell, UPDATE_WRITE_BARRIER, |
| 14091 | SKIP_ICACHE_FLUSH); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14092 | } else if (RelocInfo::IsCodeTarget(mode)) { |
| 14093 | // rewrite code handles in inline cache targets to direct |
| 14094 | // pointers to the first instruction in the code object |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 14095 | Handle<Object> p = it.rinfo()->target_object_handle(origin); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14096 | Code* code = Code::cast(*p); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14097 | it.rinfo()->set_target_address(code->instruction_start(), |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 14098 | UPDATE_WRITE_BARRIER, SKIP_ICACHE_FLUSH); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14099 | } else if (RelocInfo::IsRuntimeEntry(mode)) { |
| 14100 | Address p = it.rinfo()->target_runtime_entry(origin); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 14101 | it.rinfo()->set_target_runtime_entry(p, UPDATE_WRITE_BARRIER, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14102 | SKIP_ICACHE_FLUSH); |
| 14103 | } else if (mode == RelocInfo::CODE_AGE_SEQUENCE) { |
| 14104 | Handle<Object> p = it.rinfo()->code_age_stub_handle(origin); |
| 14105 | Code* code = Code::cast(*p); |
| 14106 | it.rinfo()->set_code_age_stub(code, SKIP_ICACHE_FLUSH); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14107 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14108 | it.rinfo()->apply(delta); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14109 | } |
| 14110 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14111 | Assembler::FlushICache(GetIsolate(), instruction_start(), instruction_size()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14112 | } |
| 14113 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 14114 | // Locate the source position which is closest to the code offset. This is |
| 14115 | // using the source position information embedded in the relocation info. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14116 | // The position returned is relative to the beginning of the script where the |
| 14117 | // source for this function is found. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 14118 | int Code::SourcePosition(int code_offset) { |
| 14119 | Address pc = instruction_start() + code_offset; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14120 | int distance = kMaxInt; |
| 14121 | int position = RelocInfo::kNoPosition; // Initially no position found. |
| 14122 | // Run through all the relocation info to find the best matching source |
| 14123 | // position. All the code needs to be considered as the sequence of the |
| 14124 | // instructions in the code does not necessarily follow the same order as the |
| 14125 | // source. |
| 14126 | RelocIterator it(this, RelocInfo::kPositionMask); |
| 14127 | while (!it.done()) { |
| 14128 | // Only look at positions after the current pc. |
| 14129 | if (it.rinfo()->pc() < pc) { |
| 14130 | // Get position and distance. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 14131 | |
| 14132 | int dist = static_cast<int>(pc - it.rinfo()->pc()); |
| 14133 | int pos = static_cast<int>(it.rinfo()->data()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14134 | // If this position is closer than the current candidate or if it has the |
| 14135 | // same distance as the current candidate and the position is higher then |
| 14136 | // this position is the new candidate. |
| 14137 | if ((dist < distance) || |
| 14138 | (dist == distance && pos > position)) { |
| 14139 | position = pos; |
| 14140 | distance = dist; |
| 14141 | } |
| 14142 | } |
| 14143 | it.next(); |
| 14144 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 14145 | DCHECK(kind() == FUNCTION || (is_optimized_code() && is_turbofanned()) || |
| 14146 | is_wasm_code() || position == RelocInfo::kNoPosition); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14147 | return position; |
| 14148 | } |
| 14149 | |
| 14150 | |
| 14151 | // Same as Code::SourcePosition above except it only looks for statement |
| 14152 | // positions. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 14153 | int Code::SourceStatementPosition(int code_offset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14154 | // First find the position as close as possible using all position |
| 14155 | // information. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 14156 | int position = SourcePosition(code_offset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14157 | // Now find the closest statement position before the position. |
| 14158 | int statement_position = 0; |
| 14159 | RelocIterator it(this, RelocInfo::kPositionMask); |
| 14160 | while (!it.done()) { |
| 14161 | if (RelocInfo::IsStatementPosition(it.rinfo()->rmode())) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 14162 | int p = static_cast<int>(it.rinfo()->data()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14163 | if (statement_position < p && p <= position) { |
| 14164 | statement_position = p; |
| 14165 | } |
| 14166 | } |
| 14167 | it.next(); |
| 14168 | } |
| 14169 | return statement_position; |
| 14170 | } |
| 14171 | |
| 14172 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 14173 | SafepointEntry Code::GetSafepointEntry(Address pc) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14174 | SafepointTable table(this); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 14175 | return table.FindEntry(pc); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14176 | } |
| 14177 | |
| 14178 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14179 | Object* Code::FindNthObject(int n, Map* match_map) { |
| 14180 | DCHECK(is_inline_cache_stub()); |
| 14181 | DisallowHeapAllocation no_allocation; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14182 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 14183 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 14184 | RelocInfo* info = it.rinfo(); |
| 14185 | Object* object = info->target_object(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14186 | if (object->IsWeakCell()) object = WeakCell::cast(object)->value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14187 | if (object->IsHeapObject()) { |
| 14188 | if (HeapObject::cast(object)->map() == match_map) { |
| 14189 | if (--n == 0) return object; |
| 14190 | } |
| 14191 | } |
| 14192 | } |
| 14193 | return NULL; |
| 14194 | } |
| 14195 | |
| 14196 | |
| 14197 | AllocationSite* Code::FindFirstAllocationSite() { |
| 14198 | Object* result = FindNthObject(1, GetHeap()->allocation_site_map()); |
| 14199 | return (result != NULL) ? AllocationSite::cast(result) : NULL; |
| 14200 | } |
| 14201 | |
| 14202 | |
| 14203 | Map* Code::FindFirstMap() { |
| 14204 | Object* result = FindNthObject(1, GetHeap()->meta_map()); |
| 14205 | return (result != NULL) ? Map::cast(result) : NULL; |
| 14206 | } |
| 14207 | |
| 14208 | |
| 14209 | void Code::FindAndReplace(const FindAndReplacePattern& pattern) { |
| 14210 | DCHECK(is_inline_cache_stub() || is_handler()); |
| 14211 | DisallowHeapAllocation no_allocation; |
| 14212 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 14213 | STATIC_ASSERT(FindAndReplacePattern::kMaxCount < 32); |
| 14214 | int current_pattern = 0; |
| 14215 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 14216 | RelocInfo* info = it.rinfo(); |
| 14217 | Object* object = info->target_object(); |
| 14218 | if (object->IsHeapObject()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14219 | if (object->IsWeakCell()) { |
| 14220 | object = HeapObject::cast(WeakCell::cast(object)->value()); |
| 14221 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14222 | Map* map = HeapObject::cast(object)->map(); |
| 14223 | if (map == *pattern.find_[current_pattern]) { |
| 14224 | info->set_target_object(*pattern.replace_[current_pattern]); |
| 14225 | if (++current_pattern == pattern.count_) return; |
| 14226 | } |
| 14227 | } |
| 14228 | } |
| 14229 | UNREACHABLE(); |
| 14230 | } |
| 14231 | |
| 14232 | |
| 14233 | void Code::FindAllMaps(MapHandleList* maps) { |
| 14234 | DCHECK(is_inline_cache_stub()); |
| 14235 | DisallowHeapAllocation no_allocation; |
| 14236 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 14237 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 14238 | RelocInfo* info = it.rinfo(); |
| 14239 | Object* object = info->target_object(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14240 | if (object->IsWeakCell()) object = WeakCell::cast(object)->value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14241 | if (object->IsMap()) maps->Add(handle(Map::cast(object))); |
| 14242 | } |
| 14243 | } |
| 14244 | |
| 14245 | |
| 14246 | Code* Code::FindFirstHandler() { |
| 14247 | DCHECK(is_inline_cache_stub()); |
| 14248 | DisallowHeapAllocation no_allocation; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14249 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) | |
| 14250 | RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 14251 | bool skip_next_handler = false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14252 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 14253 | RelocInfo* info = it.rinfo(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14254 | if (info->rmode() == RelocInfo::EMBEDDED_OBJECT) { |
| 14255 | Object* obj = info->target_object(); |
| 14256 | skip_next_handler |= obj->IsWeakCell() && WeakCell::cast(obj)->cleared(); |
| 14257 | } else { |
| 14258 | Code* code = Code::GetCodeFromTargetAddress(info->target_address()); |
| 14259 | if (code->kind() == Code::HANDLER) { |
| 14260 | if (!skip_next_handler) return code; |
| 14261 | skip_next_handler = false; |
| 14262 | } |
| 14263 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14264 | } |
| 14265 | return NULL; |
| 14266 | } |
| 14267 | |
| 14268 | |
| 14269 | bool Code::FindHandlers(CodeHandleList* code_list, int length) { |
| 14270 | DCHECK(is_inline_cache_stub()); |
| 14271 | DisallowHeapAllocation no_allocation; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14272 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) | |
| 14273 | RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 14274 | bool skip_next_handler = false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14275 | int i = 0; |
| 14276 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 14277 | if (i == length) return true; |
| 14278 | RelocInfo* info = it.rinfo(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14279 | if (info->rmode() == RelocInfo::EMBEDDED_OBJECT) { |
| 14280 | Object* obj = info->target_object(); |
| 14281 | skip_next_handler |= obj->IsWeakCell() && WeakCell::cast(obj)->cleared(); |
| 14282 | } else { |
| 14283 | Code* code = Code::GetCodeFromTargetAddress(info->target_address()); |
| 14284 | // IC stubs with handlers never contain non-handler code objects before |
| 14285 | // handler targets. |
| 14286 | if (code->kind() != Code::HANDLER) break; |
| 14287 | if (!skip_next_handler) { |
| 14288 | code_list->Add(Handle<Code>(code)); |
| 14289 | i++; |
| 14290 | } |
| 14291 | skip_next_handler = false; |
| 14292 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14293 | } |
| 14294 | return i == length; |
| 14295 | } |
| 14296 | |
| 14297 | |
| 14298 | MaybeHandle<Code> Code::FindHandlerForMap(Map* map) { |
| 14299 | DCHECK(is_inline_cache_stub()); |
| 14300 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) | |
| 14301 | RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 14302 | bool return_next = false; |
| 14303 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 14304 | RelocInfo* info = it.rinfo(); |
| 14305 | if (info->rmode() == RelocInfo::EMBEDDED_OBJECT) { |
| 14306 | Object* object = info->target_object(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14307 | if (object->IsWeakCell()) object = WeakCell::cast(object)->value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14308 | if (object == map) return_next = true; |
| 14309 | } else if (return_next) { |
| 14310 | Code* code = Code::GetCodeFromTargetAddress(info->target_address()); |
| 14311 | DCHECK(code->kind() == Code::HANDLER); |
| 14312 | return handle(code); |
| 14313 | } |
| 14314 | } |
| 14315 | return MaybeHandle<Code>(); |
| 14316 | } |
| 14317 | |
| 14318 | |
| 14319 | Name* Code::FindFirstName() { |
| 14320 | DCHECK(is_inline_cache_stub()); |
| 14321 | DisallowHeapAllocation no_allocation; |
| 14322 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 14323 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 14324 | RelocInfo* info = it.rinfo(); |
| 14325 | Object* object = info->target_object(); |
| 14326 | if (object->IsName()) return Name::cast(object); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14327 | } |
| 14328 | return NULL; |
| 14329 | } |
| 14330 | |
| 14331 | |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 14332 | void Code::ClearInlineCaches() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14333 | ClearInlineCaches(NULL); |
| 14334 | } |
| 14335 | |
| 14336 | |
| 14337 | void Code::ClearInlineCaches(Code::Kind kind) { |
| 14338 | ClearInlineCaches(&kind); |
| 14339 | } |
| 14340 | |
| 14341 | |
| 14342 | void Code::ClearInlineCaches(Code::Kind* kind) { |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 14343 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14344 | RelocInfo::ModeMask(RelocInfo::CODE_TARGET_WITH_ID); |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 14345 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 14346 | RelocInfo* info = it.rinfo(); |
| 14347 | Code* target(Code::GetCodeFromTargetAddress(info->target_address())); |
| 14348 | if (target->is_inline_cache_stub()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14349 | if (kind == NULL || *kind == target->kind()) { |
| 14350 | IC::Clear(this->GetIsolate(), info->pc(), |
| 14351 | info->host()->constant_pool()); |
| 14352 | } |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 14353 | } |
| 14354 | } |
| 14355 | } |
| 14356 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 14357 | int AbstractCode::SourcePosition(int offset) { |
| 14358 | return IsBytecodeArray() ? GetBytecodeArray()->SourcePosition(offset) |
| 14359 | : GetCode()->SourcePosition(offset); |
| 14360 | } |
| 14361 | |
| 14362 | int AbstractCode::SourceStatementPosition(int offset) { |
| 14363 | return IsBytecodeArray() ? GetBytecodeArray()->SourceStatementPosition(offset) |
| 14364 | : GetCode()->SourceStatementPosition(offset); |
| 14365 | } |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 14366 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14367 | void SharedFunctionInfo::ClearTypeFeedbackInfo() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14368 | feedback_vector()->ClearSlots(this); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14369 | } |
| 14370 | |
| 14371 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14372 | void SharedFunctionInfo::ClearTypeFeedbackInfoAtGCTime() { |
| 14373 | feedback_vector()->ClearSlotsAtGCTime(this); |
| 14374 | } |
| 14375 | |
| 14376 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14377 | BailoutId Code::TranslatePcOffsetToAstId(uint32_t pc_offset) { |
| 14378 | DisallowHeapAllocation no_gc; |
| 14379 | DCHECK(kind() == FUNCTION); |
| 14380 | BackEdgeTable back_edges(this, &no_gc); |
| 14381 | for (uint32_t i = 0; i < back_edges.length(); i++) { |
| 14382 | if (back_edges.pc_offset(i) == pc_offset) return back_edges.ast_id(i); |
| 14383 | } |
| 14384 | return BailoutId::None(); |
| 14385 | } |
| 14386 | |
| 14387 | |
| 14388 | uint32_t Code::TranslateAstIdToPcOffset(BailoutId ast_id) { |
| 14389 | DisallowHeapAllocation no_gc; |
| 14390 | DCHECK(kind() == FUNCTION); |
| 14391 | BackEdgeTable back_edges(this, &no_gc); |
| 14392 | for (uint32_t i = 0; i < back_edges.length(); i++) { |
| 14393 | if (back_edges.ast_id(i) == ast_id) return back_edges.pc_offset(i); |
| 14394 | } |
| 14395 | UNREACHABLE(); // We expect to find the back edge. |
| 14396 | return 0; |
| 14397 | } |
| 14398 | |
| 14399 | |
| 14400 | void Code::MakeCodeAgeSequenceYoung(byte* sequence, Isolate* isolate) { |
| 14401 | PatchPlatformCodeAge(isolate, sequence, kNoAgeCodeAge, NO_MARKING_PARITY); |
| 14402 | } |
| 14403 | |
| 14404 | |
| 14405 | void Code::MarkCodeAsExecuted(byte* sequence, Isolate* isolate) { |
| 14406 | PatchPlatformCodeAge(isolate, sequence, kExecutedOnceCodeAge, |
| 14407 | NO_MARKING_PARITY); |
| 14408 | } |
| 14409 | |
| 14410 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14411 | // NextAge defines the Code::Age state transitions during a GC cycle. |
| 14412 | static Code::Age NextAge(Code::Age age) { |
| 14413 | switch (age) { |
| 14414 | case Code::kNotExecutedCodeAge: // Keep, until we've been executed. |
| 14415 | case Code::kToBeExecutedOnceCodeAge: // Keep, until we've been executed. |
| 14416 | case Code::kLastCodeAge: // Clamp at last Code::Age value. |
| 14417 | return age; |
| 14418 | case Code::kExecutedOnceCodeAge: |
| 14419 | // Pre-age code that has only been executed once. |
| 14420 | return static_cast<Code::Age>(Code::kPreAgedCodeAge + 1); |
| 14421 | default: |
| 14422 | return static_cast<Code::Age>(age + 1); // Default case: Increase age. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14423 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14424 | } |
| 14425 | |
| 14426 | |
| 14427 | // IsOldAge defines the collection criteria for a Code object. |
| 14428 | static bool IsOldAge(Code::Age age) { |
| 14429 | return age >= Code::kIsOldCodeAge || age == Code::kNotExecutedCodeAge; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14430 | } |
| 14431 | |
| 14432 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14433 | void Code::MakeYoung(Isolate* isolate) { |
| 14434 | byte* sequence = FindCodeAgeSequence(); |
| 14435 | if (sequence != NULL) MakeCodeAgeSequenceYoung(sequence, isolate); |
| 14436 | } |
| 14437 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 14438 | void Code::PreAge(Isolate* isolate) { |
| 14439 | byte* sequence = FindCodeAgeSequence(); |
| 14440 | if (sequence != NULL) { |
| 14441 | PatchPlatformCodeAge(isolate, sequence, kPreAgedCodeAge, NO_MARKING_PARITY); |
| 14442 | } |
| 14443 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14444 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14445 | void Code::MarkToBeExecutedOnce(Isolate* isolate) { |
| 14446 | byte* sequence = FindCodeAgeSequence(); |
| 14447 | if (sequence != NULL) { |
| 14448 | PatchPlatformCodeAge(isolate, sequence, kToBeExecutedOnceCodeAge, |
| 14449 | NO_MARKING_PARITY); |
| 14450 | } |
| 14451 | } |
| 14452 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14453 | void Code::MakeOlder(MarkingParity current_parity) { |
| 14454 | byte* sequence = FindCodeAgeSequence(); |
| 14455 | if (sequence != NULL) { |
| 14456 | Age age; |
| 14457 | MarkingParity code_parity; |
| 14458 | Isolate* isolate = GetIsolate(); |
| 14459 | GetCodeAgeAndParity(isolate, sequence, &age, &code_parity); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14460 | Age next_age = NextAge(age); |
| 14461 | if (age != next_age && code_parity != current_parity) { |
| 14462 | PatchPlatformCodeAge(isolate, sequence, next_age, current_parity); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14463 | } |
| 14464 | } |
| 14465 | } |
| 14466 | |
| 14467 | |
| 14468 | bool Code::IsOld() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14469 | return IsOldAge(GetAge()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14470 | } |
| 14471 | |
| 14472 | |
| 14473 | byte* Code::FindCodeAgeSequence() { |
| 14474 | return FLAG_age_code && |
| 14475 | prologue_offset() != Code::kPrologueOffsetNotSet && |
| 14476 | (kind() == OPTIMIZED_FUNCTION || |
| 14477 | (kind() == FUNCTION && !has_debug_break_slots())) |
| 14478 | ? instruction_start() + prologue_offset() |
| 14479 | : NULL; |
| 14480 | } |
| 14481 | |
| 14482 | |
| 14483 | Code::Age Code::GetAge() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14484 | byte* sequence = FindCodeAgeSequence(); |
| 14485 | if (sequence == NULL) { |
| 14486 | return kNoAgeCodeAge; |
| 14487 | } |
| 14488 | Age age; |
| 14489 | MarkingParity parity; |
| 14490 | GetCodeAgeAndParity(GetIsolate(), sequence, &age, &parity); |
| 14491 | return age; |
| 14492 | } |
| 14493 | |
| 14494 | |
| 14495 | void Code::GetCodeAgeAndParity(Code* code, Age* age, |
| 14496 | MarkingParity* parity) { |
| 14497 | Isolate* isolate = code->GetIsolate(); |
| 14498 | Builtins* builtins = isolate->builtins(); |
| 14499 | Code* stub = NULL; |
| 14500 | #define HANDLE_CODE_AGE(AGE) \ |
| 14501 | stub = *builtins->Make##AGE##CodeYoungAgainEvenMarking(); \ |
| 14502 | if (code == stub) { \ |
| 14503 | *age = k##AGE##CodeAge; \ |
| 14504 | *parity = EVEN_MARKING_PARITY; \ |
| 14505 | return; \ |
| 14506 | } \ |
| 14507 | stub = *builtins->Make##AGE##CodeYoungAgainOddMarking(); \ |
| 14508 | if (code == stub) { \ |
| 14509 | *age = k##AGE##CodeAge; \ |
| 14510 | *parity = ODD_MARKING_PARITY; \ |
| 14511 | return; \ |
| 14512 | } |
| 14513 | CODE_AGE_LIST(HANDLE_CODE_AGE) |
| 14514 | #undef HANDLE_CODE_AGE |
| 14515 | stub = *builtins->MarkCodeAsExecutedOnce(); |
| 14516 | if (code == stub) { |
| 14517 | *age = kNotExecutedCodeAge; |
| 14518 | *parity = NO_MARKING_PARITY; |
| 14519 | return; |
| 14520 | } |
| 14521 | stub = *builtins->MarkCodeAsExecutedTwice(); |
| 14522 | if (code == stub) { |
| 14523 | *age = kExecutedOnceCodeAge; |
| 14524 | *parity = NO_MARKING_PARITY; |
| 14525 | return; |
| 14526 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14527 | stub = *builtins->MarkCodeAsToBeExecutedOnce(); |
| 14528 | if (code == stub) { |
| 14529 | *age = kToBeExecutedOnceCodeAge; |
| 14530 | *parity = NO_MARKING_PARITY; |
| 14531 | return; |
| 14532 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14533 | UNREACHABLE(); |
| 14534 | } |
| 14535 | |
| 14536 | |
| 14537 | Code* Code::GetCodeAgeStub(Isolate* isolate, Age age, MarkingParity parity) { |
| 14538 | Builtins* builtins = isolate->builtins(); |
| 14539 | switch (age) { |
| 14540 | #define HANDLE_CODE_AGE(AGE) \ |
| 14541 | case k##AGE##CodeAge: { \ |
| 14542 | Code* stub = parity == EVEN_MARKING_PARITY \ |
| 14543 | ? *builtins->Make##AGE##CodeYoungAgainEvenMarking() \ |
| 14544 | : *builtins->Make##AGE##CodeYoungAgainOddMarking(); \ |
| 14545 | return stub; \ |
| 14546 | } |
| 14547 | CODE_AGE_LIST(HANDLE_CODE_AGE) |
| 14548 | #undef HANDLE_CODE_AGE |
| 14549 | case kNotExecutedCodeAge: { |
| 14550 | DCHECK(parity == NO_MARKING_PARITY); |
| 14551 | return *builtins->MarkCodeAsExecutedOnce(); |
| 14552 | } |
| 14553 | case kExecutedOnceCodeAge: { |
| 14554 | DCHECK(parity == NO_MARKING_PARITY); |
| 14555 | return *builtins->MarkCodeAsExecutedTwice(); |
| 14556 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14557 | case kToBeExecutedOnceCodeAge: { |
| 14558 | DCHECK(parity == NO_MARKING_PARITY); |
| 14559 | return *builtins->MarkCodeAsToBeExecutedOnce(); |
| 14560 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14561 | default: |
| 14562 | UNREACHABLE(); |
| 14563 | break; |
| 14564 | } |
| 14565 | return NULL; |
| 14566 | } |
| 14567 | |
| 14568 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14569 | void Code::PrintDeoptLocation(FILE* out, Address pc) { |
| 14570 | Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(this, pc); |
| 14571 | class SourcePosition pos = info.position; |
| 14572 | if (info.deopt_reason != Deoptimizer::kNoReason || !pos.IsUnknown()) { |
| 14573 | if (FLAG_hydrogen_track_positions) { |
| 14574 | PrintF(out, " ;;; deoptimize at %d_%d: %s\n", |
| 14575 | pos.inlining_id(), pos.position(), |
| 14576 | Deoptimizer::GetDeoptReason(info.deopt_reason)); |
| 14577 | } else { |
| 14578 | PrintF(out, " ;;; deoptimize at %d: %s\n", pos.raw(), |
| 14579 | Deoptimizer::GetDeoptReason(info.deopt_reason)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14580 | } |
| 14581 | } |
| 14582 | } |
| 14583 | |
| 14584 | |
| 14585 | bool Code::CanDeoptAt(Address pc) { |
| 14586 | DeoptimizationInputData* deopt_data = |
| 14587 | DeoptimizationInputData::cast(deoptimization_data()); |
| 14588 | Address code_start_address = instruction_start(); |
| 14589 | for (int i = 0; i < deopt_data->DeoptCount(); i++) { |
| 14590 | if (deopt_data->Pc(i)->value() == -1) continue; |
| 14591 | Address address = code_start_address + deopt_data->Pc(i)->value(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 14592 | if (address == pc && deopt_data->AstId(i) != BailoutId::None()) { |
| 14593 | return true; |
| 14594 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14595 | } |
| 14596 | return false; |
| 14597 | } |
| 14598 | |
| 14599 | |
| 14600 | // Identify kind of code. |
| 14601 | const char* Code::Kind2String(Kind kind) { |
| 14602 | switch (kind) { |
| 14603 | #define CASE(name) case name: return #name; |
| 14604 | CODE_KIND_LIST(CASE) |
| 14605 | #undef CASE |
| 14606 | case NUMBER_OF_KINDS: break; |
| 14607 | } |
| 14608 | UNREACHABLE(); |
| 14609 | return NULL; |
| 14610 | } |
| 14611 | |
| 14612 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14613 | Handle<WeakCell> Code::WeakCellFor(Handle<Code> code) { |
| 14614 | DCHECK(code->kind() == OPTIMIZED_FUNCTION); |
| 14615 | WeakCell* raw_cell = code->CachedWeakCell(); |
| 14616 | if (raw_cell != NULL) return Handle<WeakCell>(raw_cell); |
| 14617 | Handle<WeakCell> cell = code->GetIsolate()->factory()->NewWeakCell(code); |
| 14618 | DeoptimizationInputData::cast(code->deoptimization_data()) |
| 14619 | ->SetWeakCellCache(*cell); |
| 14620 | return cell; |
| 14621 | } |
| 14622 | |
| 14623 | |
| 14624 | WeakCell* Code::CachedWeakCell() { |
| 14625 | DCHECK(kind() == OPTIMIZED_FUNCTION); |
| 14626 | Object* weak_cell_cache = |
| 14627 | DeoptimizationInputData::cast(deoptimization_data())->WeakCellCache(); |
| 14628 | if (weak_cell_cache->IsWeakCell()) { |
| 14629 | DCHECK(this == WeakCell::cast(weak_cell_cache)->value()); |
| 14630 | return WeakCell::cast(weak_cell_cache); |
| 14631 | } |
| 14632 | return NULL; |
| 14633 | } |
| 14634 | |
| 14635 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14636 | #ifdef ENABLE_DISASSEMBLER |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14637 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14638 | void DeoptimizationInputData::DeoptimizationInputDataPrint( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14639 | std::ostream& os) { // NOLINT |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14640 | disasm::NameConverter converter; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14641 | int const inlined_function_count = InlinedFunctionCount()->value(); |
| 14642 | os << "Inlined functions (count = " << inlined_function_count << ")\n"; |
| 14643 | for (int id = 0; id < inlined_function_count; ++id) { |
| 14644 | Object* info = LiteralArray()->get(id); |
| 14645 | os << " " << Brief(SharedFunctionInfo::cast(info)) << "\n"; |
| 14646 | } |
| 14647 | os << "\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14648 | int deopt_count = DeoptCount(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14649 | os << "Deoptimization Input Data (deopt points = " << deopt_count << ")\n"; |
| 14650 | if (0 != deopt_count) { |
| 14651 | os << " index ast id argc pc"; |
| 14652 | if (FLAG_print_code_verbose) os << " commands"; |
| 14653 | os << "\n"; |
| 14654 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14655 | for (int i = 0; i < deopt_count; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14656 | os << std::setw(6) << i << " " << std::setw(6) << AstId(i).ToInt() << " " |
| 14657 | << std::setw(6) << ArgumentsStackHeight(i)->value() << " " |
| 14658 | << std::setw(6) << Pc(i)->value(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14659 | |
| 14660 | if (!FLAG_print_code_verbose) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14661 | os << "\n"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14662 | continue; |
| 14663 | } |
| 14664 | // Print details of the frame translation. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14665 | int translation_index = TranslationIndex(i)->value(); |
| 14666 | TranslationIterator iterator(TranslationByteArray(), translation_index); |
| 14667 | Translation::Opcode opcode = |
| 14668 | static_cast<Translation::Opcode>(iterator.Next()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14669 | DCHECK(Translation::BEGIN == opcode); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14670 | int frame_count = iterator.Next(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14671 | int jsframe_count = iterator.Next(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14672 | os << " " << Translation::StringFor(opcode) |
| 14673 | << " {frame count=" << frame_count |
| 14674 | << ", js frame count=" << jsframe_count << "}\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14675 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14676 | while (iterator.HasNext() && |
| 14677 | Translation::BEGIN != |
| 14678 | (opcode = static_cast<Translation::Opcode>(iterator.Next()))) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14679 | os << std::setw(31) << " " << Translation::StringFor(opcode) << " "; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14680 | |
| 14681 | switch (opcode) { |
| 14682 | case Translation::BEGIN: |
| 14683 | UNREACHABLE(); |
| 14684 | break; |
| 14685 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14686 | case Translation::JS_FRAME: { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14687 | int ast_id = iterator.Next(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14688 | int shared_info_id = iterator.Next(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14689 | unsigned height = iterator.Next(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14690 | Object* shared_info = LiteralArray()->get(shared_info_id); |
| 14691 | os << "{ast_id=" << ast_id << ", function=" |
| 14692 | << Brief(SharedFunctionInfo::cast(shared_info)->DebugName()) |
| 14693 | << ", height=" << height << "}"; |
| 14694 | break; |
| 14695 | } |
| 14696 | |
| 14697 | case Translation::INTERPRETED_FRAME: { |
| 14698 | int bytecode_offset = iterator.Next(); |
| 14699 | int shared_info_id = iterator.Next(); |
| 14700 | unsigned height = iterator.Next(); |
| 14701 | Object* shared_info = LiteralArray()->get(shared_info_id); |
| 14702 | os << "{bytecode_offset=" << bytecode_offset << ", function=" |
| 14703 | << Brief(SharedFunctionInfo::cast(shared_info)->DebugName()) |
| 14704 | << ", height=" << height << "}"; |
| 14705 | break; |
| 14706 | } |
| 14707 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14708 | case Translation::COMPILED_STUB_FRAME: { |
| 14709 | Code::Kind stub_kind = static_cast<Code::Kind>(iterator.Next()); |
| 14710 | os << "{kind=" << stub_kind << "}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14711 | break; |
| 14712 | } |
| 14713 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14714 | case Translation::ARGUMENTS_ADAPTOR_FRAME: |
| 14715 | case Translation::CONSTRUCT_STUB_FRAME: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14716 | int shared_info_id = iterator.Next(); |
| 14717 | Object* shared_info = LiteralArray()->get(shared_info_id); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14718 | unsigned height = iterator.Next(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14719 | os << "{function=" |
| 14720 | << Brief(SharedFunctionInfo::cast(shared_info)->DebugName()) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14721 | << ", height=" << height << "}"; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14722 | break; |
| 14723 | } |
| 14724 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 14725 | case Translation::TAIL_CALLER_FRAME: { |
| 14726 | int shared_info_id = iterator.Next(); |
| 14727 | Object* shared_info = LiteralArray()->get(shared_info_id); |
| 14728 | os << "{function=" |
| 14729 | << Brief(SharedFunctionInfo::cast(shared_info)->DebugName()) |
| 14730 | << "}"; |
| 14731 | break; |
| 14732 | } |
| 14733 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14734 | case Translation::GETTER_STUB_FRAME: |
| 14735 | case Translation::SETTER_STUB_FRAME: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14736 | int shared_info_id = iterator.Next(); |
| 14737 | Object* shared_info = LiteralArray()->get(shared_info_id); |
| 14738 | os << "{function=" << Brief(SharedFunctionInfo::cast(shared_info) |
| 14739 | ->DebugName()) << "}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14740 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14741 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14742 | |
| 14743 | case Translation::REGISTER: { |
| 14744 | int reg_code = iterator.Next(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14745 | os << "{input=" << converter.NameOfCPURegister(reg_code) << "}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14746 | break; |
| 14747 | } |
| 14748 | |
| 14749 | case Translation::INT32_REGISTER: { |
| 14750 | int reg_code = iterator.Next(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14751 | os << "{input=" << converter.NameOfCPURegister(reg_code) << "}"; |
| 14752 | break; |
| 14753 | } |
| 14754 | |
| 14755 | case Translation::UINT32_REGISTER: { |
| 14756 | int reg_code = iterator.Next(); |
| 14757 | os << "{input=" << converter.NameOfCPURegister(reg_code) |
| 14758 | << " (unsigned)}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14759 | break; |
| 14760 | } |
| 14761 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14762 | case Translation::BOOL_REGISTER: { |
| 14763 | int reg_code = iterator.Next(); |
| 14764 | os << "{input=" << converter.NameOfCPURegister(reg_code) |
| 14765 | << " (bool)}"; |
| 14766 | break; |
| 14767 | } |
| 14768 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14769 | case Translation::DOUBLE_REGISTER: { |
| 14770 | int reg_code = iterator.Next(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14771 | os << "{input=" << DoubleRegister::from_code(reg_code).ToString() |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14772 | << "}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14773 | break; |
| 14774 | } |
| 14775 | |
| 14776 | case Translation::STACK_SLOT: { |
| 14777 | int input_slot_index = iterator.Next(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14778 | os << "{input=" << input_slot_index << "}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14779 | break; |
| 14780 | } |
| 14781 | |
| 14782 | case Translation::INT32_STACK_SLOT: { |
| 14783 | int input_slot_index = iterator.Next(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14784 | os << "{input=" << input_slot_index << "}"; |
| 14785 | break; |
| 14786 | } |
| 14787 | |
| 14788 | case Translation::UINT32_STACK_SLOT: { |
| 14789 | int input_slot_index = iterator.Next(); |
| 14790 | os << "{input=" << input_slot_index << " (unsigned)}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14791 | break; |
| 14792 | } |
| 14793 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14794 | case Translation::BOOL_STACK_SLOT: { |
| 14795 | int input_slot_index = iterator.Next(); |
| 14796 | os << "{input=" << input_slot_index << " (bool)}"; |
| 14797 | break; |
| 14798 | } |
| 14799 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14800 | case Translation::DOUBLE_STACK_SLOT: { |
| 14801 | int input_slot_index = iterator.Next(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14802 | os << "{input=" << input_slot_index << "}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14803 | break; |
| 14804 | } |
| 14805 | |
| 14806 | case Translation::LITERAL: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14807 | int literal_index = iterator.Next(); |
| 14808 | Object* literal_value = LiteralArray()->get(literal_index); |
| 14809 | os << "{literal_id=" << literal_index << " (" << Brief(literal_value) |
| 14810 | << ")}"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14811 | break; |
| 14812 | } |
| 14813 | |
| 14814 | case Translation::DUPLICATED_OBJECT: { |
| 14815 | int object_index = iterator.Next(); |
| 14816 | os << "{object_index=" << object_index << "}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14817 | break; |
| 14818 | } |
| 14819 | |
| 14820 | case Translation::ARGUMENTS_OBJECT: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14821 | case Translation::CAPTURED_OBJECT: { |
| 14822 | int args_length = iterator.Next(); |
| 14823 | os << "{length=" << args_length << "}"; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 14824 | break; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14825 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14826 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14827 | os << "\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14828 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14829 | } |
| 14830 | } |
| 14831 | |
| 14832 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14833 | void DeoptimizationOutputData::DeoptimizationOutputDataPrint( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14834 | std::ostream& os) { // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14835 | os << "Deoptimization Output Data (deopt points = " << this->DeoptPoints() |
| 14836 | << ")\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14837 | if (this->DeoptPoints() == 0) return; |
| 14838 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14839 | os << "ast id pc state\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14840 | for (int i = 0; i < this->DeoptPoints(); i++) { |
| 14841 | int pc_and_state = this->PcAndState(i)->value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14842 | os << std::setw(6) << this->AstId(i).ToInt() << " " << std::setw(8) |
| 14843 | << FullCodeGenerator::PcField::decode(pc_and_state) << " " |
| 14844 | << FullCodeGenerator::State2String( |
| 14845 | FullCodeGenerator::StateField::decode(pc_and_state)) << "\n"; |
| 14846 | } |
| 14847 | } |
| 14848 | |
| 14849 | |
| 14850 | void HandlerTable::HandlerTableRangePrint(std::ostream& os) { |
| 14851 | os << " from to hdlr\n"; |
| 14852 | for (int i = 0; i < length(); i += kRangeEntrySize) { |
| 14853 | int pc_start = Smi::cast(get(i + kRangeStartIndex))->value(); |
| 14854 | int pc_end = Smi::cast(get(i + kRangeEndIndex))->value(); |
| 14855 | int handler_field = Smi::cast(get(i + kRangeHandlerIndex))->value(); |
| 14856 | int handler_offset = HandlerOffsetField::decode(handler_field); |
| 14857 | CatchPrediction prediction = HandlerPredictionField::decode(handler_field); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 14858 | int data = Smi::cast(get(i + kRangeDataIndex))->value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14859 | os << " (" << std::setw(4) << pc_start << "," << std::setw(4) << pc_end |
| 14860 | << ") -> " << std::setw(4) << handler_offset |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 14861 | << " (prediction=" << prediction << ", data=" << data << ")\n"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14862 | } |
| 14863 | } |
| 14864 | |
| 14865 | |
| 14866 | void HandlerTable::HandlerTableReturnPrint(std::ostream& os) { |
| 14867 | os << " off hdlr (c)\n"; |
| 14868 | for (int i = 0; i < length(); i += kReturnEntrySize) { |
| 14869 | int pc_offset = Smi::cast(get(i + kReturnOffsetIndex))->value(); |
| 14870 | int handler_field = Smi::cast(get(i + kReturnHandlerIndex))->value(); |
| 14871 | int handler_offset = HandlerOffsetField::decode(handler_field); |
| 14872 | CatchPrediction prediction = HandlerPredictionField::decode(handler_field); |
| 14873 | os << " " << std::setw(4) << pc_offset << " -> " << std::setw(4) |
| 14874 | << handler_offset << " (prediction=" << prediction << ")\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14875 | } |
| 14876 | } |
| 14877 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14878 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14879 | const char* Code::ICState2String(InlineCacheState state) { |
| 14880 | switch (state) { |
| 14881 | case UNINITIALIZED: return "UNINITIALIZED"; |
| 14882 | case PREMONOMORPHIC: return "PREMONOMORPHIC"; |
| 14883 | case MONOMORPHIC: return "MONOMORPHIC"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14884 | case PROTOTYPE_FAILURE: |
| 14885 | return "PROTOTYPE_FAILURE"; |
| 14886 | case POLYMORPHIC: return "POLYMORPHIC"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14887 | case MEGAMORPHIC: return "MEGAMORPHIC"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14888 | case GENERIC: return "GENERIC"; |
| 14889 | case DEBUG_STUB: return "DEBUG_STUB"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14890 | } |
| 14891 | UNREACHABLE(); |
| 14892 | return NULL; |
| 14893 | } |
| 14894 | |
| 14895 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14896 | const char* Code::StubType2String(StubType type) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14897 | switch (type) { |
| 14898 | case NORMAL: return "NORMAL"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14899 | case FAST: return "FAST"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14900 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 14901 | UNREACHABLE(); // keep the compiler happy |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14902 | return NULL; |
| 14903 | } |
| 14904 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14905 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14906 | void Code::PrintExtraICState(std::ostream& os, // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14907 | Kind kind, ExtraICState extra) { |
| 14908 | os << "extra_ic_state = "; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14909 | if ((kind == STORE_IC || kind == KEYED_STORE_IC) && |
| 14910 | is_strict(static_cast<LanguageMode>(extra))) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14911 | os << "STRICT\n"; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 14912 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14913 | os << extra << "\n"; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 14914 | } |
| 14915 | } |
| 14916 | |
| 14917 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14918 | void Code::Disassemble(const char* name, std::ostream& os) { // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14919 | os << "kind = " << Kind2String(kind()) << "\n"; |
| 14920 | if (IsCodeStubOrIC()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14921 | const char* n = CodeStub::MajorName(CodeStub::GetMajorKey(this)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14922 | os << "major_key = " << (n == NULL ? "null" : n) << "\n"; |
| 14923 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14924 | if (is_inline_cache_stub()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14925 | os << "ic_state = " << ICState2String(ic_state()) << "\n"; |
| 14926 | PrintExtraICState(os, kind(), extra_ic_state()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14927 | if (ic_state() == MONOMORPHIC) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14928 | os << "type = " << StubType2String(type()) << "\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14929 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14930 | if (is_compare_ic_stub()) { |
| 14931 | DCHECK(CodeStub::GetMajorKey(this) == CodeStub::CompareIC); |
| 14932 | CompareICStub stub(stub_key(), GetIsolate()); |
| 14933 | os << "compare_state = " << CompareICState::GetStateName(stub.left()) |
| 14934 | << "*" << CompareICState::GetStateName(stub.right()) << " -> " |
| 14935 | << CompareICState::GetStateName(stub.state()) << "\n"; |
| 14936 | os << "compare_operation = " << Token::Name(stub.op()) << "\n"; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 14937 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14938 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 14939 | if ((name != nullptr) && (name[0] != '\0')) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14940 | os << "name = " << name << "\n"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14941 | } else if (kind() == BUILTIN) { |
| 14942 | name = GetIsolate()->builtins()->Lookup(instruction_start()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 14943 | if (name != nullptr) { |
| 14944 | os << "name = " << name << "\n"; |
| 14945 | } |
| 14946 | } else if (kind() == BYTECODE_HANDLER) { |
| 14947 | name = GetIsolate()->interpreter()->LookupNameOfBytecodeHandler(this); |
| 14948 | if (name != nullptr) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14949 | os << "name = " << name << "\n"; |
| 14950 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14951 | } |
| 14952 | if (kind() == OPTIMIZED_FUNCTION) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14953 | os << "stack_slots = " << stack_slots() << "\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14954 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14955 | os << "compiler = " << (is_turbofanned() |
| 14956 | ? "turbofan" |
| 14957 | : is_crankshafted() ? "crankshaft" |
| 14958 | : kind() == Code::FUNCTION |
| 14959 | ? "full-codegen" |
| 14960 | : "unknown") << "\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14961 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14962 | os << "Instructions (size = " << instruction_size() << ")\n"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14963 | { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14964 | Isolate* isolate = GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14965 | int size = instruction_size(); |
| 14966 | int safepoint_offset = |
| 14967 | is_crankshafted() ? static_cast<int>(safepoint_table_offset()) : size; |
| 14968 | int back_edge_offset = (kind() == Code::FUNCTION) |
| 14969 | ? static_cast<int>(back_edge_table_offset()) |
| 14970 | : size; |
| 14971 | int constant_pool_offset = FLAG_enable_embedded_constant_pool |
| 14972 | ? this->constant_pool_offset() |
| 14973 | : size; |
| 14974 | |
| 14975 | // Stop before reaching any embedded tables |
| 14976 | int code_size = Min(safepoint_offset, back_edge_offset); |
| 14977 | code_size = Min(code_size, constant_pool_offset); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14978 | byte* begin = instruction_start(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14979 | byte* end = begin + code_size; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14980 | Disassembler::Decode(isolate, &os, begin, end, this); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14981 | |
| 14982 | if (constant_pool_offset < size) { |
| 14983 | int constant_pool_size = size - constant_pool_offset; |
| 14984 | DCHECK((constant_pool_size & kPointerAlignmentMask) == 0); |
| 14985 | os << "\nConstant Pool (size = " << constant_pool_size << ")\n"; |
| 14986 | Vector<char> buf = Vector<char>::New(50); |
| 14987 | intptr_t* ptr = reinterpret_cast<intptr_t*>(begin + constant_pool_offset); |
| 14988 | for (int i = 0; i < constant_pool_size; i += kPointerSize, ptr++) { |
| 14989 | SNPrintF(buf, "%4d %08" V8PRIxPTR, i, *ptr); |
| 14990 | os << static_cast<const void*>(ptr) << " " << buf.start() << "\n"; |
| 14991 | } |
| 14992 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14993 | } |
| 14994 | os << "\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14995 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 14996 | if (kind() == FUNCTION) { |
| 14997 | DeoptimizationOutputData* data = |
| 14998 | DeoptimizationOutputData::cast(this->deoptimization_data()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14999 | data->DeoptimizationOutputDataPrint(os); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 15000 | } else if (kind() == OPTIMIZED_FUNCTION) { |
| 15001 | DeoptimizationInputData* data = |
| 15002 | DeoptimizationInputData::cast(this->deoptimization_data()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15003 | data->DeoptimizationInputDataPrint(os); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 15004 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15005 | os << "\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 15006 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15007 | if (is_crankshafted()) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 15008 | SafepointTable table(this); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15009 | os << "Safepoints (size = " << table.size() << ")\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 15010 | for (unsigned i = 0; i < table.length(); i++) { |
| 15011 | unsigned pc_offset = table.GetPcOffset(i); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15012 | os << static_cast<const void*>(instruction_start() + pc_offset) << " "; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15013 | os << std::setw(4) << pc_offset << " "; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15014 | table.PrintEntry(i, os); |
| 15015 | os << " (sp -> fp) "; |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 15016 | SafepointEntry entry = table.GetEntry(i); |
| 15017 | if (entry.deoptimization_index() != Safepoint::kNoDeoptimizationIndex) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15018 | os << std::setw(6) << entry.deoptimization_index(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 15019 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15020 | os << "<none>"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 15021 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 15022 | if (entry.argument_count() > 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15023 | os << " argc: " << entry.argument_count(); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 15024 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15025 | os << "\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 15026 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15027 | os << "\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 15028 | } else if (kind() == FUNCTION) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15029 | unsigned offset = back_edge_table_offset(); |
| 15030 | // 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] | 15031 | // (due to alignment) the end of the instruction stream. |
| 15032 | if (static_cast<int>(offset) < instruction_size()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15033 | DisallowHeapAllocation no_gc; |
| 15034 | BackEdgeTable back_edges(this, &no_gc); |
| 15035 | |
| 15036 | os << "Back edges (size = " << back_edges.length() << ")\n"; |
| 15037 | os << "ast_id pc_offset loop_depth\n"; |
| 15038 | |
| 15039 | for (uint32_t i = 0; i < back_edges.length(); i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15040 | os << std::setw(6) << back_edges.ast_id(i).ToInt() << " " |
| 15041 | << std::setw(9) << back_edges.pc_offset(i) << " " << std::setw(10) |
| 15042 | << back_edges.loop_depth(i) << "\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 15043 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15044 | |
| 15045 | os << "\n"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 15046 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15047 | #ifdef OBJECT_PRINT |
| 15048 | if (!type_feedback_info()->IsUndefined()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15049 | TypeFeedbackInfo::cast(type_feedback_info())->TypeFeedbackInfoPrint(os); |
| 15050 | os << "\n"; |
| 15051 | } |
| 15052 | #endif |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 15053 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15054 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15055 | if (handler_table()->length() > 0) { |
| 15056 | os << "Handler Table (size = " << handler_table()->Size() << ")\n"; |
| 15057 | if (kind() == FUNCTION) { |
| 15058 | HandlerTable::cast(handler_table())->HandlerTableRangePrint(os); |
| 15059 | } else if (kind() == OPTIMIZED_FUNCTION) { |
| 15060 | HandlerTable::cast(handler_table())->HandlerTableReturnPrint(os); |
| 15061 | } |
| 15062 | os << "\n"; |
| 15063 | } |
| 15064 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15065 | os << "RelocInfo (size = " << relocation_size() << ")\n"; |
| 15066 | for (RelocIterator it(this); !it.done(); it.next()) { |
| 15067 | it.rinfo()->Print(GetIsolate(), os); |
| 15068 | } |
| 15069 | os << "\n"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15070 | } |
| 15071 | #endif // ENABLE_DISASSEMBLER |
| 15072 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15073 | int BytecodeArray::SourcePosition(int offset) { |
| 15074 | int last_position = 0; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 15075 | for (interpreter::SourcePositionTableIterator iterator( |
| 15076 | source_position_table()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15077 | !iterator.done() && iterator.bytecode_offset() <= offset; |
| 15078 | iterator.Advance()) { |
| 15079 | last_position = iterator.source_position(); |
| 15080 | } |
| 15081 | return last_position; |
| 15082 | } |
| 15083 | |
| 15084 | int BytecodeArray::SourceStatementPosition(int offset) { |
| 15085 | // First find the position as close as possible using all position |
| 15086 | // information. |
| 15087 | int position = SourcePosition(offset); |
| 15088 | // Now find the closest statement position before the position. |
| 15089 | int statement_position = 0; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 15090 | interpreter::SourcePositionTableIterator iterator(source_position_table()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15091 | while (!iterator.done()) { |
| 15092 | if (iterator.is_statement()) { |
| 15093 | int p = iterator.source_position(); |
| 15094 | if (statement_position < p && p <= position) { |
| 15095 | statement_position = p; |
| 15096 | } |
| 15097 | } |
| 15098 | iterator.Advance(); |
| 15099 | } |
| 15100 | return statement_position; |
| 15101 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15102 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15103 | void BytecodeArray::Disassemble(std::ostream& os) { |
| 15104 | os << "Parameter count " << parameter_count() << "\n"; |
| 15105 | os << "Frame size " << frame_size() << "\n"; |
| 15106 | Vector<char> buf = Vector<char>::New(50); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 15107 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 15108 | const uint8_t* base_address = GetFirstBytecodeAddress(); |
| 15109 | interpreter::SourcePositionTableIterator source_positions( |
| 15110 | source_position_table()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15111 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 15112 | interpreter::BytecodeArrayIterator iterator(handle(this)); |
| 15113 | while (!iterator.done()) { |
| 15114 | if (!source_positions.done() && |
| 15115 | iterator.current_offset() == source_positions.bytecode_offset()) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15116 | os << std::setw(5) << source_positions.source_position(); |
| 15117 | os << (source_positions.is_statement() ? " S> " : " E> "); |
| 15118 | source_positions.Advance(); |
| 15119 | } else { |
| 15120 | os << " "; |
| 15121 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 15122 | const uint8_t* current_address = base_address + iterator.current_offset(); |
| 15123 | SNPrintF(buf, "%p", current_address); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15124 | os << buf.start() << " : "; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 15125 | interpreter::Bytecodes::Decode(os, current_address, parameter_count()); |
| 15126 | if (interpreter::Bytecodes::IsJump(iterator.current_bytecode())) { |
| 15127 | SNPrintF(buf, " (%p)", base_address + iterator.GetJumpTargetOffset()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15128 | os << buf.start(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15129 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15130 | os << std::endl; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 15131 | iterator.Advance(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 15132 | } |
| 15133 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15134 | if (constant_pool()->length() > 0) { |
| 15135 | os << "Constant pool (size = " << constant_pool()->length() << ")\n"; |
| 15136 | constant_pool()->Print(); |
| 15137 | } |
| 15138 | |
| 15139 | #ifdef ENABLE_DISASSEMBLER |
| 15140 | if (handler_table()->length() > 0) { |
| 15141 | os << "Handler Table (size = " << handler_table()->Size() << ")\n"; |
| 15142 | HandlerTable::cast(handler_table())->HandlerTableRangePrint(os); |
| 15143 | } |
| 15144 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15145 | } |
| 15146 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15147 | void BytecodeArray::CopyBytecodesTo(BytecodeArray* to) { |
| 15148 | BytecodeArray* from = this; |
| 15149 | DCHECK_EQ(from->length(), to->length()); |
| 15150 | CopyBytes(to->GetFirstBytecodeAddress(), from->GetFirstBytecodeAddress(), |
| 15151 | from->length()); |
| 15152 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15153 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15154 | // static |
| 15155 | void JSArray::Initialize(Handle<JSArray> array, int capacity, int length) { |
| 15156 | DCHECK(capacity >= 0); |
| 15157 | array->GetIsolate()->factory()->NewJSArrayStorage( |
| 15158 | array, length, capacity, INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE); |
| 15159 | } |
| 15160 | |
| 15161 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15162 | // Returns false if the passed-in index is marked non-configurable, which will |
| 15163 | // cause the truncation operation to halt, and thus no further old values need |
| 15164 | // be collected. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15165 | static bool GetOldValue(Isolate* isolate, |
| 15166 | Handle<JSObject> object, |
| 15167 | uint32_t index, |
| 15168 | List<Handle<Object> >* old_values, |
| 15169 | List<uint32_t>* indices) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 15170 | LookupIterator it(isolate, object, index, object, LookupIterator::HIDDEN); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15171 | CHECK(JSReceiver::GetPropertyAttributes(&it).IsJust()); |
| 15172 | DCHECK(it.IsFound()); |
| 15173 | if (!it.IsConfigurable()) return false; |
| 15174 | Handle<Object> value = |
| 15175 | it.state() == LookupIterator::ACCESSOR |
| 15176 | ? Handle<Object>::cast(isolate->factory()->the_hole_value()) |
| 15177 | : JSReceiver::GetDataProperty(&it); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15178 | old_values->Add(value); |
| 15179 | indices->Add(index); |
| 15180 | return true; |
| 15181 | } |
| 15182 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15183 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15184 | void JSArray::SetLength(Handle<JSArray> array, uint32_t new_length) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 15185 | // We should never end in here with a pixel or external array. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15186 | DCHECK(array->AllowsSetLength()); |
| 15187 | if (array->SetLengthWouldNormalize(new_length)) { |
| 15188 | JSObject::NormalizeElements(array); |
| 15189 | } |
| 15190 | array->GetElementsAccessor()->SetLength(array, new_length); |
| 15191 | } |
| 15192 | |
| 15193 | |
| 15194 | MaybeHandle<Object> JSArray::ObservableSetLength(Handle<JSArray> array, |
| 15195 | uint32_t new_length) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15196 | if (!array->map()->is_observed()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15197 | SetLength(array, new_length); |
| 15198 | return array; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15199 | } |
| 15200 | |
| 15201 | Isolate* isolate = array->GetIsolate(); |
| 15202 | List<uint32_t> indices; |
| 15203 | List<Handle<Object> > old_values; |
| 15204 | Handle<Object> old_length_handle(array->length(), isolate); |
| 15205 | uint32_t old_length = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15206 | CHECK(old_length_handle->ToArrayLength(&old_length)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15207 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15208 | int num_elements = array->NumberOfOwnElements(ALL_PROPERTIES); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15209 | if (num_elements > 0) { |
| 15210 | if (old_length == static_cast<uint32_t>(num_elements)) { |
| 15211 | // Simple case for arrays without holes. |
| 15212 | for (uint32_t i = old_length - 1; i + 1 > new_length; --i) { |
| 15213 | if (!GetOldValue(isolate, array, i, &old_values, &indices)) break; |
| 15214 | } |
| 15215 | } else { |
| 15216 | // For sparse arrays, only iterate over existing elements. |
| 15217 | // TODO(rafaelw): For fast, sparse arrays, we can avoid iterating over |
| 15218 | // the to-be-removed indices twice. |
| 15219 | Handle<FixedArray> keys = isolate->factory()->NewFixedArray(num_elements); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15220 | array->GetOwnElementKeys(*keys, ALL_PROPERTIES); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15221 | while (num_elements-- > 0) { |
| 15222 | uint32_t index = NumberToUint32(keys->get(num_elements)); |
| 15223 | if (index < new_length) break; |
| 15224 | if (!GetOldValue(isolate, array, index, &old_values, &indices)) break; |
| 15225 | } |
| 15226 | } |
| 15227 | } |
| 15228 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15229 | SetLength(array, new_length); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15230 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15231 | CHECK(array->length()->ToArrayLength(&new_length)); |
| 15232 | if (old_length == new_length) return array; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15233 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15234 | RETURN_ON_EXCEPTION(isolate, BeginPerformSplice(array), Object); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15235 | |
| 15236 | for (int i = 0; i < indices.length(); ++i) { |
| 15237 | // For deletions where the property was an accessor, old_values[i] |
| 15238 | // will be the hole, which instructs EnqueueChangeRecord to elide |
| 15239 | // the "oldValue" property. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15240 | RETURN_ON_EXCEPTION( |
| 15241 | isolate, |
| 15242 | JSObject::EnqueueChangeRecord( |
| 15243 | array, "delete", isolate->factory()->Uint32ToString(indices[i]), |
| 15244 | old_values[i]), |
| 15245 | Object); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15246 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15247 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15248 | RETURN_ON_EXCEPTION(isolate, |
| 15249 | JSObject::EnqueueChangeRecord( |
| 15250 | array, "update", isolate->factory()->length_string(), |
| 15251 | old_length_handle), |
| 15252 | Object); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15253 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15254 | RETURN_ON_EXCEPTION(isolate, EndPerformSplice(array), Object); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15255 | |
| 15256 | uint32_t index = Min(old_length, new_length); |
| 15257 | uint32_t add_count = new_length > old_length ? new_length - old_length : 0; |
| 15258 | uint32_t delete_count = new_length < old_length ? old_length - new_length : 0; |
| 15259 | Handle<JSArray> deleted = isolate->factory()->NewJSArray(0); |
| 15260 | if (delete_count > 0) { |
| 15261 | for (int i = indices.length() - 1; i >= 0; i--) { |
| 15262 | // Skip deletions where the property was an accessor, leaving holes |
| 15263 | // in the array of old values. |
| 15264 | if (old_values[i]->IsTheHole()) continue; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15265 | JSObject::AddDataElement(deleted, indices[i] - index, old_values[i], NONE) |
| 15266 | .Assert(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15267 | } |
| 15268 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15269 | JSArray::SetLength(deleted, delete_count); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15270 | } |
| 15271 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15272 | RETURN_ON_EXCEPTION( |
| 15273 | isolate, EnqueueSpliceRecord(array, index, deleted, add_count), Object); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15274 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15275 | return array; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15276 | } |
| 15277 | |
| 15278 | |
| 15279 | // static |
| 15280 | void Map::AddDependentCode(Handle<Map> map, |
| 15281 | DependentCode::DependencyGroup group, |
| 15282 | Handle<Code> code) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15283 | Handle<WeakCell> cell = Code::WeakCellFor(code); |
| 15284 | Handle<DependentCode> codes = DependentCode::InsertWeakCode( |
| 15285 | Handle<DependentCode>(map->dependent_code()), group, cell); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15286 | if (*codes != map->dependent_code()) map->set_dependent_code(*codes); |
| 15287 | } |
| 15288 | |
| 15289 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15290 | Handle<DependentCode> DependentCode::InsertCompilationDependencies( |
| 15291 | Handle<DependentCode> entries, DependencyGroup group, |
| 15292 | Handle<Foreign> info) { |
| 15293 | return Insert(entries, group, info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15294 | } |
| 15295 | |
| 15296 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15297 | Handle<DependentCode> DependentCode::InsertWeakCode( |
| 15298 | Handle<DependentCode> entries, DependencyGroup group, |
| 15299 | Handle<WeakCell> code_cell) { |
| 15300 | return Insert(entries, group, code_cell); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15301 | } |
| 15302 | |
| 15303 | |
| 15304 | Handle<DependentCode> DependentCode::Insert(Handle<DependentCode> entries, |
| 15305 | DependencyGroup group, |
| 15306 | Handle<Object> object) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15307 | if (entries->length() == 0 || entries->group() > group) { |
| 15308 | // There is no such group. |
| 15309 | return DependentCode::New(group, object, entries); |
| 15310 | } |
| 15311 | if (entries->group() < group) { |
| 15312 | // The group comes later in the list. |
| 15313 | Handle<DependentCode> old_next(entries->next_link()); |
| 15314 | Handle<DependentCode> new_next = Insert(old_next, group, object); |
| 15315 | if (!old_next.is_identical_to(new_next)) { |
| 15316 | entries->set_next_link(*new_next); |
| 15317 | } |
| 15318 | return entries; |
| 15319 | } |
| 15320 | DCHECK_EQ(group, entries->group()); |
| 15321 | int count = entries->count(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15322 | // Check for existing entry to avoid duplicates. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15323 | for (int i = 0; i < count; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15324 | if (entries->object_at(i) == *object) return entries; |
| 15325 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15326 | if (entries->length() < kCodesStartIndex + count + 1) { |
| 15327 | entries = EnsureSpace(entries); |
| 15328 | // Count could have changed, reload it. |
| 15329 | count = entries->count(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15330 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15331 | entries->set_object_at(count, *object); |
| 15332 | entries->set_count(count + 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15333 | return entries; |
| 15334 | } |
| 15335 | |
| 15336 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15337 | Handle<DependentCode> DependentCode::New(DependencyGroup group, |
| 15338 | Handle<Object> object, |
| 15339 | Handle<DependentCode> next) { |
| 15340 | Isolate* isolate = next->GetIsolate(); |
| 15341 | Handle<DependentCode> result = Handle<DependentCode>::cast( |
| 15342 | isolate->factory()->NewFixedArray(kCodesStartIndex + 1, TENURED)); |
| 15343 | result->set_next_link(*next); |
| 15344 | result->set_flags(GroupField::encode(group) | CountField::encode(1)); |
| 15345 | result->set_object_at(0, *object); |
| 15346 | return result; |
| 15347 | } |
| 15348 | |
| 15349 | |
| 15350 | Handle<DependentCode> DependentCode::EnsureSpace( |
| 15351 | Handle<DependentCode> entries) { |
| 15352 | if (entries->Compact()) return entries; |
| 15353 | Isolate* isolate = entries->GetIsolate(); |
| 15354 | int capacity = kCodesStartIndex + DependentCode::Grow(entries->count()); |
| 15355 | int grow_by = capacity - entries->length(); |
| 15356 | return Handle<DependentCode>::cast( |
| 15357 | isolate->factory()->CopyFixedArrayAndGrow(entries, grow_by, TENURED)); |
| 15358 | } |
| 15359 | |
| 15360 | |
| 15361 | bool DependentCode::Compact() { |
| 15362 | int old_count = count(); |
| 15363 | int new_count = 0; |
| 15364 | for (int i = 0; i < old_count; i++) { |
| 15365 | Object* obj = object_at(i); |
| 15366 | if (!obj->IsWeakCell() || !WeakCell::cast(obj)->cleared()) { |
| 15367 | if (i != new_count) { |
| 15368 | copy(i, new_count); |
| 15369 | } |
| 15370 | new_count++; |
| 15371 | } |
| 15372 | } |
| 15373 | set_count(new_count); |
| 15374 | for (int i = new_count; i < old_count; i++) { |
| 15375 | clear_at(i); |
| 15376 | } |
| 15377 | return new_count < old_count; |
| 15378 | } |
| 15379 | |
| 15380 | |
| 15381 | void DependentCode::UpdateToFinishedCode(DependencyGroup group, Foreign* info, |
| 15382 | WeakCell* code_cell) { |
| 15383 | if (this->length() == 0 || this->group() > group) { |
| 15384 | // There is no such group. |
| 15385 | return; |
| 15386 | } |
| 15387 | if (this->group() < group) { |
| 15388 | // The group comes later in the list. |
| 15389 | next_link()->UpdateToFinishedCode(group, info, code_cell); |
| 15390 | return; |
| 15391 | } |
| 15392 | DCHECK_EQ(group, this->group()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15393 | DisallowHeapAllocation no_gc; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15394 | int count = this->count(); |
| 15395 | for (int i = 0; i < count; i++) { |
| 15396 | if (object_at(i) == info) { |
| 15397 | set_object_at(i, code_cell); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15398 | break; |
| 15399 | } |
| 15400 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 15401 | #ifdef DEBUG |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15402 | for (int i = 0; i < count; i++) { |
| 15403 | DCHECK(object_at(i) != info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15404 | } |
| 15405 | #endif |
| 15406 | } |
| 15407 | |
| 15408 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15409 | void DependentCode::RemoveCompilationDependencies( |
| 15410 | DependentCode::DependencyGroup group, Foreign* info) { |
| 15411 | if (this->length() == 0 || this->group() > group) { |
| 15412 | // There is no such group. |
| 15413 | return; |
| 15414 | } |
| 15415 | if (this->group() < group) { |
| 15416 | // The group comes later in the list. |
| 15417 | next_link()->RemoveCompilationDependencies(group, info); |
| 15418 | return; |
| 15419 | } |
| 15420 | DCHECK_EQ(group, this->group()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15421 | DisallowHeapAllocation no_allocation; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15422 | int old_count = count(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15423 | // Find compilation info wrapper. |
| 15424 | int info_pos = -1; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15425 | for (int i = 0; i < old_count; i++) { |
| 15426 | if (object_at(i) == info) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15427 | info_pos = i; |
| 15428 | break; |
| 15429 | } |
| 15430 | } |
| 15431 | if (info_pos == -1) return; // Not found. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15432 | // Use the last code to fill the gap. |
| 15433 | if (info_pos < old_count - 1) { |
| 15434 | copy(old_count - 1, info_pos); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15435 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15436 | clear_at(old_count - 1); |
| 15437 | set_count(old_count - 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15438 | |
| 15439 | #ifdef DEBUG |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15440 | for (int i = 0; i < old_count - 1; i++) { |
| 15441 | DCHECK(object_at(i) != info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15442 | } |
| 15443 | #endif |
| 15444 | } |
| 15445 | |
| 15446 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15447 | bool DependentCode::Contains(DependencyGroup group, WeakCell* code_cell) { |
| 15448 | if (this->length() == 0 || this->group() > group) { |
| 15449 | // There is no such group. |
| 15450 | return false; |
| 15451 | } |
| 15452 | if (this->group() < group) { |
| 15453 | // The group comes later in the list. |
| 15454 | return next_link()->Contains(group, code_cell); |
| 15455 | } |
| 15456 | DCHECK_EQ(group, this->group()); |
| 15457 | int count = this->count(); |
| 15458 | for (int i = 0; i < count; i++) { |
| 15459 | if (object_at(i) == code_cell) return true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15460 | } |
| 15461 | return false; |
| 15462 | } |
| 15463 | |
| 15464 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15465 | bool DependentCode::IsEmpty(DependencyGroup group) { |
| 15466 | if (this->length() == 0 || this->group() > group) { |
| 15467 | // There is no such group. |
| 15468 | return true; |
| 15469 | } |
| 15470 | if (this->group() < group) { |
| 15471 | // The group comes later in the list. |
| 15472 | return next_link()->IsEmpty(group); |
| 15473 | } |
| 15474 | DCHECK_EQ(group, this->group()); |
| 15475 | return count() == 0; |
| 15476 | } |
| 15477 | |
| 15478 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15479 | bool DependentCode::MarkCodeForDeoptimization( |
| 15480 | Isolate* isolate, |
| 15481 | DependentCode::DependencyGroup group) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15482 | if (this->length() == 0 || this->group() > group) { |
| 15483 | // There is no such group. |
| 15484 | return false; |
| 15485 | } |
| 15486 | if (this->group() < group) { |
| 15487 | // The group comes later in the list. |
| 15488 | return next_link()->MarkCodeForDeoptimization(isolate, group); |
| 15489 | } |
| 15490 | DCHECK_EQ(group, this->group()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15491 | DisallowHeapAllocation no_allocation_scope; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15492 | // Mark all the code that needs to be deoptimized. |
| 15493 | bool marked = false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15494 | bool invalidate_embedded_objects = group == kWeakCodeGroup; |
| 15495 | int count = this->count(); |
| 15496 | for (int i = 0; i < count; i++) { |
| 15497 | Object* obj = object_at(i); |
| 15498 | if (obj->IsWeakCell()) { |
| 15499 | WeakCell* cell = WeakCell::cast(obj); |
| 15500 | if (cell->cleared()) continue; |
| 15501 | Code* code = Code::cast(cell->value()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15502 | if (!code->marked_for_deoptimization()) { |
| 15503 | SetMarkedForDeoptimization(code, group); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15504 | if (invalidate_embedded_objects) { |
| 15505 | code->InvalidateEmbeddedObjects(); |
| 15506 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15507 | marked = true; |
| 15508 | } |
| 15509 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15510 | DCHECK(obj->IsForeign()); |
| 15511 | CompilationDependencies* info = |
| 15512 | reinterpret_cast<CompilationDependencies*>( |
| 15513 | Foreign::cast(obj)->foreign_address()); |
| 15514 | info->Abort(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15515 | } |
| 15516 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15517 | for (int i = 0; i < count; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15518 | clear_at(i); |
| 15519 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15520 | set_count(0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15521 | return marked; |
| 15522 | } |
| 15523 | |
| 15524 | |
| 15525 | void DependentCode::DeoptimizeDependentCodeGroup( |
| 15526 | Isolate* isolate, |
| 15527 | DependentCode::DependencyGroup group) { |
| 15528 | DCHECK(AllowCodeDependencyChange::IsAllowed()); |
| 15529 | DisallowHeapAllocation no_allocation_scope; |
| 15530 | bool marked = MarkCodeForDeoptimization(isolate, group); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15531 | if (marked) Deoptimizer::DeoptimizeMarkedCode(isolate); |
| 15532 | } |
| 15533 | |
| 15534 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15535 | void DependentCode::SetMarkedForDeoptimization(Code* code, |
| 15536 | DependencyGroup group) { |
| 15537 | code->set_marked_for_deoptimization(true); |
| 15538 | if (FLAG_trace_deopt && |
| 15539 | (code->deoptimization_data() != code->GetHeap()->empty_fixed_array())) { |
| 15540 | DeoptimizationInputData* deopt_data = |
| 15541 | DeoptimizationInputData::cast(code->deoptimization_data()); |
| 15542 | CodeTracer::Scope scope(code->GetHeap()->isolate()->GetCodeTracer()); |
| 15543 | PrintF(scope.file(), "[marking dependent code 0x%08" V8PRIxPTR |
| 15544 | " (opt #%d) for deoptimization, reason: %s]\n", |
| 15545 | reinterpret_cast<intptr_t>(code), |
| 15546 | deopt_data->OptimizationId()->value(), DependencyGroupName(group)); |
| 15547 | } |
| 15548 | } |
| 15549 | |
| 15550 | |
| 15551 | const char* DependentCode::DependencyGroupName(DependencyGroup group) { |
| 15552 | switch (group) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15553 | case kWeakCodeGroup: |
| 15554 | return "weak-code"; |
| 15555 | case kTransitionGroup: |
| 15556 | return "transition"; |
| 15557 | case kPrototypeCheckGroup: |
| 15558 | return "prototype-check"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15559 | case kPropertyCellChangedGroup: |
| 15560 | return "property-cell-changed"; |
| 15561 | case kFieldTypeGroup: |
| 15562 | return "field-type"; |
| 15563 | case kInitialMapChangedGroup: |
| 15564 | return "initial-map-changed"; |
| 15565 | case kAllocationSiteTenuringChangedGroup: |
| 15566 | return "allocation-site-tenuring-changed"; |
| 15567 | case kAllocationSiteTransitionChangedGroup: |
| 15568 | return "allocation-site-transition-changed"; |
| 15569 | } |
| 15570 | UNREACHABLE(); |
| 15571 | return "?"; |
| 15572 | } |
| 15573 | |
| 15574 | |
| 15575 | Handle<Map> Map::TransitionToPrototype(Handle<Map> map, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15576 | Handle<Object> prototype, |
| 15577 | PrototypeOptimizationMode mode) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15578 | Handle<Map> new_map = TransitionArray::GetPrototypeTransition(map, prototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15579 | if (new_map.is_null()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15580 | new_map = Copy(map, "TransitionToPrototype"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15581 | TransitionArray::PutPrototypeTransition(map, prototype, new_map); |
| 15582 | Map::SetPrototype(new_map, prototype, mode); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15583 | } |
| 15584 | return new_map; |
| 15585 | } |
| 15586 | |
| 15587 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15588 | Maybe<bool> JSReceiver::SetPrototype(Handle<JSReceiver> object, |
| 15589 | Handle<Object> value, bool from_javascript, |
| 15590 | ShouldThrow should_throw) { |
| 15591 | if (object->IsJSProxy()) { |
| 15592 | return JSProxy::SetPrototype(Handle<JSProxy>::cast(object), value, |
| 15593 | from_javascript, should_throw); |
| 15594 | } |
| 15595 | return JSObject::SetPrototype(Handle<JSObject>::cast(object), value, |
| 15596 | from_javascript, should_throw); |
| 15597 | } |
| 15598 | |
| 15599 | |
| 15600 | // ES6: 9.5.2 [[SetPrototypeOf]] (V) |
| 15601 | // static |
| 15602 | Maybe<bool> JSProxy::SetPrototype(Handle<JSProxy> proxy, Handle<Object> value, |
| 15603 | bool from_javascript, |
| 15604 | ShouldThrow should_throw) { |
| 15605 | Isolate* isolate = proxy->GetIsolate(); |
| 15606 | STACK_CHECK(Nothing<bool>()); |
| 15607 | Handle<Name> trap_name = isolate->factory()->setPrototypeOf_string(); |
| 15608 | // 1. Assert: Either Type(V) is Object or Type(V) is Null. |
| 15609 | DCHECK(value->IsJSReceiver() || value->IsNull()); |
| 15610 | // 2. Let handler be the value of the [[ProxyHandler]] internal slot of O. |
| 15611 | Handle<Object> handler(proxy->handler(), isolate); |
| 15612 | // 3. If handler is null, throw a TypeError exception. |
| 15613 | // 4. Assert: Type(handler) is Object. |
| 15614 | if (proxy->IsRevoked()) { |
| 15615 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 15616 | MessageTemplate::kProxyRevoked, trap_name)); |
| 15617 | return Nothing<bool>(); |
| 15618 | } |
| 15619 | // 5. Let target be the value of the [[ProxyTarget]] internal slot. |
| 15620 | Handle<JSReceiver> target(proxy->target(), isolate); |
| 15621 | // 6. Let trap be ? GetMethod(handler, "getPrototypeOf"). |
| 15622 | Handle<Object> trap; |
| 15623 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 15624 | isolate, trap, |
| 15625 | Object::GetMethod(Handle<JSReceiver>::cast(handler), trap_name), |
| 15626 | Nothing<bool>()); |
| 15627 | // 7. If trap is undefined, then return target.[[SetPrototypeOf]](). |
| 15628 | if (trap->IsUndefined()) { |
| 15629 | return JSReceiver::SetPrototype(target, value, from_javascript, |
| 15630 | should_throw); |
| 15631 | } |
| 15632 | // 8. Let booleanTrapResult be ToBoolean(? Call(trap, handler, «target, V»)). |
| 15633 | Handle<Object> argv[] = {target, value}; |
| 15634 | Handle<Object> trap_result; |
| 15635 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 15636 | isolate, trap_result, |
| 15637 | Execution::Call(isolate, trap, handler, arraysize(argv), argv), |
| 15638 | Nothing<bool>()); |
| 15639 | bool bool_trap_result = trap_result->BooleanValue(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15640 | // 9. If booleanTrapResult is false, return false. |
| 15641 | if (!bool_trap_result) { |
| 15642 | RETURN_FAILURE( |
| 15643 | isolate, should_throw, |
| 15644 | NewTypeError(MessageTemplate::kProxyTrapReturnedFalsish, trap_name)); |
| 15645 | } |
| 15646 | // 10. Let extensibleTarget be ? IsExtensible(target). |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15647 | Maybe<bool> is_extensible = JSReceiver::IsExtensible(target); |
| 15648 | if (is_extensible.IsNothing()) return Nothing<bool>(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15649 | // 11. If extensibleTarget is true, return true. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15650 | if (is_extensible.FromJust()) { |
| 15651 | if (bool_trap_result) return Just(true); |
| 15652 | RETURN_FAILURE( |
| 15653 | isolate, should_throw, |
| 15654 | NewTypeError(MessageTemplate::kProxyTrapReturnedFalsish, trap_name)); |
| 15655 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15656 | // 12. Let targetProto be ? target.[[GetPrototypeOf]](). |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15657 | Handle<Object> target_proto; |
| 15658 | ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, target_proto, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15659 | JSReceiver::GetPrototype(isolate, target), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15660 | Nothing<bool>()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15661 | // 13. If SameValue(V, targetProto) is false, throw a TypeError exception. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15662 | if (bool_trap_result && !value->SameValue(*target_proto)) { |
| 15663 | isolate->Throw(*isolate->factory()->NewTypeError( |
| 15664 | MessageTemplate::kProxySetPrototypeOfNonExtensible)); |
| 15665 | return Nothing<bool>(); |
| 15666 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15667 | // 14. Return true. |
| 15668 | return Just(true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15669 | } |
| 15670 | |
| 15671 | |
| 15672 | Maybe<bool> JSObject::SetPrototype(Handle<JSObject> object, |
| 15673 | Handle<Object> value, bool from_javascript, |
| 15674 | ShouldThrow should_throw) { |
| 15675 | Isolate* isolate = object->GetIsolate(); |
| 15676 | |
| 15677 | const bool observed = from_javascript && object->map()->is_observed(); |
| 15678 | Handle<Object> old_value; |
| 15679 | if (observed) { |
| 15680 | ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, old_value, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15681 | JSReceiver::GetPrototype(isolate, object), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15682 | Nothing<bool>()); |
| 15683 | } |
| 15684 | |
| 15685 | Maybe<bool> result = |
| 15686 | SetPrototypeUnobserved(object, value, from_javascript, should_throw); |
| 15687 | MAYBE_RETURN(result, Nothing<bool>()); |
| 15688 | |
| 15689 | if (result.FromJust() && observed) { |
| 15690 | Handle<Object> new_value; |
| 15691 | ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, new_value, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15692 | JSReceiver::GetPrototype(isolate, object), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15693 | Nothing<bool>()); |
| 15694 | if (!new_value->SameValue(*old_value)) { |
| 15695 | RETURN_ON_EXCEPTION_VALUE( |
| 15696 | isolate, JSObject::EnqueueChangeRecord( |
| 15697 | object, "setPrototype", |
| 15698 | isolate->factory()->proto_string(), old_value), |
| 15699 | Nothing<bool>()); |
| 15700 | } |
| 15701 | } |
| 15702 | |
| 15703 | return result; |
| 15704 | } |
| 15705 | |
| 15706 | |
| 15707 | Maybe<bool> JSObject::SetPrototypeUnobserved(Handle<JSObject> object, |
| 15708 | Handle<Object> value, |
| 15709 | bool from_javascript, |
| 15710 | ShouldThrow should_throw) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15711 | #ifdef DEBUG |
| 15712 | int size = object->Size(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 15713 | #endif |
| 15714 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15715 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15716 | |
| 15717 | if (from_javascript) { |
| 15718 | if (object->IsAccessCheckNeeded() && |
| 15719 | !isolate->MayAccess(handle(isolate->context()), object)) { |
| 15720 | isolate->ReportFailedAccessCheck(object); |
| 15721 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); |
| 15722 | RETURN_FAILURE(isolate, should_throw, |
| 15723 | NewTypeError(MessageTemplate::kNoAccess)); |
| 15724 | } |
| 15725 | } else { |
| 15726 | DCHECK(!object->IsAccessCheckNeeded()); |
| 15727 | } |
| 15728 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15729 | Heap* heap = isolate->heap(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 15730 | // Silently ignore the change if value is not a JSObject or null. |
| 15731 | // SpiderMonkey behaves this way. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15732 | if (!value->IsJSReceiver() && !value->IsNull()) return Just(true); |
| 15733 | |
| 15734 | bool dictionary_elements_in_chain = |
| 15735 | object->map()->DictionaryElementsInPrototypeChainOnly(); |
| 15736 | |
| 15737 | bool all_extensible = object->map()->is_extensible(); |
| 15738 | Handle<JSObject> real_receiver = object; |
| 15739 | if (from_javascript) { |
| 15740 | // Find the first object in the chain whose prototype object is not |
| 15741 | // hidden. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15742 | PrototypeIterator iter(isolate, real_receiver, |
| 15743 | PrototypeIterator::START_AT_PROTOTYPE, |
| 15744 | PrototypeIterator::END_AT_NON_HIDDEN); |
| 15745 | while (!iter.IsAtEnd()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15746 | // Casting to JSObject is fine because hidden prototypes are never |
| 15747 | // JSProxies. |
| 15748 | real_receiver = PrototypeIterator::GetCurrent<JSObject>(iter); |
| 15749 | iter.Advance(); |
| 15750 | all_extensible = all_extensible && real_receiver->map()->is_extensible(); |
| 15751 | } |
| 15752 | } |
| 15753 | Handle<Map> map(real_receiver->map()); |
| 15754 | |
| 15755 | // Nothing to do if prototype is already set. |
| 15756 | if (map->prototype() == *value) return Just(true); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 15757 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 15758 | // From 8.6.2 Object Internal Methods |
| 15759 | // ... |
| 15760 | // In addition, if [[Extensible]] is false the value of the [[Class]] and |
| 15761 | // [[Prototype]] internal properties of the object may not be modified. |
| 15762 | // ... |
| 15763 | // Implementation specific extensions that modify [[Class]], [[Prototype]] |
| 15764 | // or [[Extensible]] must not violate the invariants defined in the preceding |
| 15765 | // paragraph. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15766 | if (!all_extensible) { |
| 15767 | RETURN_FAILURE(isolate, should_throw, |
| 15768 | NewTypeError(MessageTemplate::kNonExtensibleProto, object)); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 15769 | } |
| 15770 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15771 | // Before we can set the prototype we need to be sure prototype cycles are |
| 15772 | // prevented. It is sufficient to validate that the receiver is not in the |
| 15773 | // new prototype chain. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15774 | if (value->IsJSReceiver()) { |
| 15775 | for (PrototypeIterator iter(isolate, JSReceiver::cast(*value), |
| 15776 | PrototypeIterator::START_AT_RECEIVER); |
| 15777 | !iter.IsAtEnd(); iter.Advance()) { |
| 15778 | if (iter.GetCurrent<JSReceiver>() == *object) { |
| 15779 | // Cycle detected. |
| 15780 | RETURN_FAILURE(isolate, should_throw, |
| 15781 | NewTypeError(MessageTemplate::kCyclicProto)); |
| 15782 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 15783 | } |
| 15784 | } |
| 15785 | |
| 15786 | // Set the new prototype of the object. |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 15787 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15788 | isolate->UpdateArrayProtectorOnSetPrototype(real_receiver); |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 15789 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15790 | PrototypeOptimizationMode mode = |
| 15791 | from_javascript ? REGULAR_PROTOTYPE : FAST_PROTOTYPE; |
| 15792 | Handle<Map> new_map = Map::TransitionToPrototype(map, value, mode); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15793 | DCHECK(new_map->prototype() == *value); |
| 15794 | JSObject::MigrateToMap(real_receiver, new_map); |
| 15795 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 15796 | if (from_javascript && !dictionary_elements_in_chain && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15797 | new_map->DictionaryElementsInPrototypeChainOnly()) { |
| 15798 | // If the prototype chain didn't previously have element callbacks, then |
| 15799 | // KeyedStoreICs need to be cleared to ensure any that involve this |
| 15800 | // map go generic. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15801 | TypeFeedbackVector::ClearAllKeyedStoreICs(isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15802 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 15803 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 15804 | heap->ClearInstanceofCache(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15805 | DCHECK(size == object->Size()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15806 | return Just(true); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 15807 | } |
| 15808 | |
| 15809 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15810 | void JSObject::EnsureCanContainElements(Handle<JSObject> object, |
| 15811 | Arguments* args, |
| 15812 | uint32_t first_arg, |
| 15813 | uint32_t arg_count, |
| 15814 | EnsureElementsMode mode) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 15815 | // Elements in |Arguments| are ordered backwards (because they're on the |
| 15816 | // stack), but the method that's called here iterates over them in forward |
| 15817 | // direction. |
| 15818 | return EnsureCanContainElements( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15819 | object, args->arguments() - first_arg - (arg_count - 1), arg_count, mode); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 15820 | } |
| 15821 | |
| 15822 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15823 | ElementsAccessor* JSObject::GetElementsAccessor() { |
| 15824 | return ElementsAccessor::ForKind(GetElementsKind()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15825 | } |
| 15826 | |
| 15827 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15828 | void JSObject::ValidateElements(Handle<JSObject> object) { |
| 15829 | #ifdef ENABLE_SLOW_DCHECKS |
| 15830 | if (FLAG_enable_slow_asserts) { |
| 15831 | ElementsAccessor* accessor = object->GetElementsAccessor(); |
| 15832 | accessor->Validate(object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15833 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15834 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15835 | } |
| 15836 | |
| 15837 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15838 | static bool ShouldConvertToSlowElements(JSObject* object, uint32_t capacity, |
| 15839 | uint32_t index, |
| 15840 | uint32_t* new_capacity) { |
| 15841 | STATIC_ASSERT(JSObject::kMaxUncheckedOldFastElementsLength <= |
| 15842 | JSObject::kMaxUncheckedFastElementsLength); |
| 15843 | if (index < capacity) { |
| 15844 | *new_capacity = capacity; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 15845 | return false; |
| 15846 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15847 | if (index - capacity >= JSObject::kMaxGap) return true; |
| 15848 | *new_capacity = JSObject::NewElementsCapacity(index + 1); |
| 15849 | DCHECK_LT(index, *new_capacity); |
| 15850 | if (*new_capacity <= JSObject::kMaxUncheckedOldFastElementsLength || |
| 15851 | (*new_capacity <= JSObject::kMaxUncheckedFastElementsLength && |
| 15852 | object->GetHeap()->InNewSpace(object))) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 15853 | return false; |
| 15854 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15855 | // If the fast-case backing storage takes up roughly three times as |
| 15856 | // much space (in machine words) as a dictionary backing storage |
| 15857 | // would, the object should have slow elements. |
| 15858 | int used_elements = object->GetFastElementsUsage(); |
| 15859 | int dictionary_size = SeededNumberDictionary::ComputeCapacity(used_elements) * |
| 15860 | SeededNumberDictionary::kEntrySize; |
| 15861 | return 3 * static_cast<uint32_t>(dictionary_size) <= *new_capacity; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 15862 | } |
| 15863 | |
| 15864 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15865 | bool JSObject::WouldConvertToSlowElements(uint32_t index) { |
| 15866 | if (HasFastElements()) { |
| 15867 | Handle<FixedArrayBase> backing_store(FixedArrayBase::cast(elements())); |
| 15868 | uint32_t capacity = static_cast<uint32_t>(backing_store->length()); |
| 15869 | uint32_t new_capacity; |
| 15870 | return ShouldConvertToSlowElements(this, capacity, index, &new_capacity); |
| 15871 | } |
| 15872 | return false; |
| 15873 | } |
| 15874 | |
| 15875 | |
| 15876 | static ElementsKind BestFittingFastElementsKind(JSObject* object) { |
| 15877 | if (object->HasSloppyArgumentsElements()) { |
| 15878 | return FAST_SLOPPY_ARGUMENTS_ELEMENTS; |
| 15879 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15880 | if (object->HasStringWrapperElements()) { |
| 15881 | return FAST_STRING_WRAPPER_ELEMENTS; |
| 15882 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15883 | DCHECK(object->HasDictionaryElements()); |
| 15884 | SeededNumberDictionary* dictionary = object->element_dictionary(); |
| 15885 | ElementsKind kind = FAST_HOLEY_SMI_ELEMENTS; |
| 15886 | for (int i = 0; i < dictionary->Capacity(); i++) { |
| 15887 | Object* key = dictionary->KeyAt(i); |
| 15888 | if (key->IsNumber()) { |
| 15889 | Object* value = dictionary->ValueAt(i); |
| 15890 | if (!value->IsNumber()) return FAST_HOLEY_ELEMENTS; |
| 15891 | if (!value->IsSmi()) { |
| 15892 | if (!FLAG_unbox_double_arrays) return FAST_HOLEY_ELEMENTS; |
| 15893 | kind = FAST_HOLEY_DOUBLE_ELEMENTS; |
| 15894 | } |
| 15895 | } |
| 15896 | } |
| 15897 | return kind; |
| 15898 | } |
| 15899 | |
| 15900 | |
| 15901 | static bool ShouldConvertToFastElements(JSObject* object, |
| 15902 | SeededNumberDictionary* dictionary, |
| 15903 | uint32_t index, |
| 15904 | uint32_t* new_capacity) { |
| 15905 | // If properties with non-standard attributes or accessors were added, we |
| 15906 | // cannot go back to fast elements. |
| 15907 | if (dictionary->requires_slow_elements()) return false; |
| 15908 | |
| 15909 | // Adding a property with this index will require slow elements. |
| 15910 | if (index >= static_cast<uint32_t>(Smi::kMaxValue)) return false; |
| 15911 | |
| 15912 | if (object->IsJSArray()) { |
| 15913 | Object* length = JSArray::cast(object)->length(); |
| 15914 | if (!length->IsSmi()) return false; |
| 15915 | *new_capacity = static_cast<uint32_t>(Smi::cast(length)->value()); |
| 15916 | } else { |
| 15917 | *new_capacity = dictionary->max_number_key() + 1; |
| 15918 | } |
| 15919 | *new_capacity = Max(index + 1, *new_capacity); |
| 15920 | |
| 15921 | uint32_t dictionary_size = static_cast<uint32_t>(dictionary->Capacity()) * |
| 15922 | SeededNumberDictionary::kEntrySize; |
| 15923 | |
| 15924 | // Turn fast if the dictionary only saves 50% space. |
| 15925 | return 2 * dictionary_size >= *new_capacity; |
| 15926 | } |
| 15927 | |
| 15928 | |
| 15929 | // static |
| 15930 | MaybeHandle<Object> JSObject::AddDataElement(Handle<JSObject> object, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15931 | uint32_t index, |
| 15932 | Handle<Object> value, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15933 | PropertyAttributes attributes) { |
| 15934 | MAYBE_RETURN_NULL( |
| 15935 | AddDataElement(object, index, value, attributes, THROW_ON_ERROR)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 15936 | return value; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 15937 | } |
| 15938 | |
| 15939 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15940 | // static |
| 15941 | Maybe<bool> JSObject::AddDataElement(Handle<JSObject> object, uint32_t index, |
| 15942 | Handle<Object> value, |
| 15943 | PropertyAttributes attributes, |
| 15944 | ShouldThrow should_throw) { |
| 15945 | DCHECK(object->map()->is_extensible()); |
| 15946 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15947 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 15948 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15949 | uint32_t old_length = 0; |
| 15950 | uint32_t new_capacity = 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 15951 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15952 | Handle<Object> old_length_handle; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15953 | if (object->IsJSArray()) { |
| 15954 | CHECK(JSArray::cast(*object)->length()->ToArrayLength(&old_length)); |
| 15955 | if (object->map()->is_observed()) { |
| 15956 | old_length_handle = handle(JSArray::cast(*object)->length(), isolate); |
| 15957 | } |
| 15958 | } |
| 15959 | |
| 15960 | ElementsKind kind = object->GetElementsKind(); |
| 15961 | FixedArrayBase* elements = object->elements(); |
| 15962 | ElementsKind dictionary_kind = DICTIONARY_ELEMENTS; |
| 15963 | if (IsSloppyArgumentsElements(kind)) { |
| 15964 | elements = FixedArrayBase::cast(FixedArray::cast(elements)->get(1)); |
| 15965 | dictionary_kind = SLOW_SLOPPY_ARGUMENTS_ELEMENTS; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15966 | } else if (IsStringWrapperElementsKind(kind)) { |
| 15967 | dictionary_kind = SLOW_STRING_WRAPPER_ELEMENTS; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15968 | } |
| 15969 | |
| 15970 | if (attributes != NONE) { |
| 15971 | kind = dictionary_kind; |
| 15972 | } else if (elements->IsSeededNumberDictionary()) { |
| 15973 | kind = ShouldConvertToFastElements(*object, |
| 15974 | SeededNumberDictionary::cast(elements), |
| 15975 | index, &new_capacity) |
| 15976 | ? BestFittingFastElementsKind(*object) |
| 15977 | : dictionary_kind; // Overwrite in case of arguments. |
| 15978 | } else if (ShouldConvertToSlowElements( |
| 15979 | *object, static_cast<uint32_t>(elements->length()), index, |
| 15980 | &new_capacity)) { |
| 15981 | kind = dictionary_kind; |
| 15982 | } |
| 15983 | |
| 15984 | ElementsKind to = value->OptimalElementsKind(); |
| 15985 | if (IsHoleyElementsKind(kind) || !object->IsJSArray() || index > old_length) { |
| 15986 | to = GetHoleyElementsKind(to); |
| 15987 | kind = GetHoleyElementsKind(kind); |
| 15988 | } |
| 15989 | to = GetMoreGeneralElementsKind(kind, to); |
| 15990 | ElementsAccessor* accessor = ElementsAccessor::ForKind(to); |
| 15991 | accessor->Add(object, index, value, attributes, new_capacity); |
| 15992 | |
| 15993 | uint32_t new_length = old_length; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15994 | Handle<Object> new_length_handle; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15995 | if (object->IsJSArray() && index >= old_length) { |
| 15996 | new_length = index + 1; |
| 15997 | new_length_handle = isolate->factory()->NewNumberFromUint(new_length); |
| 15998 | JSArray::cast(*object)->set_length(*new_length_handle); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15999 | } |
| 16000 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16001 | if (!old_length_handle.is_null() && new_length != old_length) { |
| 16002 | // |old_length_handle| is kept null above unless the object is observed. |
| 16003 | DCHECK(object->map()->is_observed()); |
| 16004 | Handle<JSArray> array = Handle<JSArray>::cast(object); |
| 16005 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16006 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16007 | RETURN_ON_EXCEPTION_VALUE(isolate, BeginPerformSplice(array), |
| 16008 | Nothing<bool>()); |
| 16009 | RETURN_ON_EXCEPTION_VALUE( |
| 16010 | isolate, EnqueueChangeRecord(array, "add", name, |
| 16011 | isolate->factory()->the_hole_value()), |
| 16012 | Nothing<bool>()); |
| 16013 | RETURN_ON_EXCEPTION_VALUE( |
| 16014 | isolate, EnqueueChangeRecord(array, "update", |
| 16015 | isolate->factory()->length_string(), |
| 16016 | old_length_handle), |
| 16017 | Nothing<bool>()); |
| 16018 | RETURN_ON_EXCEPTION_VALUE(isolate, EndPerformSplice(array), |
| 16019 | Nothing<bool>()); |
| 16020 | Handle<JSArray> deleted = isolate->factory()->NewJSArray(0); |
| 16021 | RETURN_ON_EXCEPTION_VALUE(isolate, |
| 16022 | EnqueueSpliceRecord(array, old_length, deleted, |
| 16023 | new_length - old_length), |
| 16024 | Nothing<bool>()); |
| 16025 | } else if (object->map()->is_observed()) { |
| 16026 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
| 16027 | RETURN_ON_EXCEPTION_VALUE( |
| 16028 | isolate, EnqueueChangeRecord(object, "add", name, |
| 16029 | isolate->factory()->the_hole_value()), |
| 16030 | Nothing<bool>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16031 | } |
| 16032 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16033 | return Just(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16034 | } |
| 16035 | |
| 16036 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16037 | bool JSArray::SetLengthWouldNormalize(uint32_t new_length) { |
| 16038 | if (!HasFastElements()) return false; |
| 16039 | uint32_t capacity = static_cast<uint32_t>(elements()->length()); |
| 16040 | uint32_t new_capacity; |
| 16041 | return JSArray::SetLengthWouldNormalize(GetHeap(), new_length) && |
| 16042 | ShouldConvertToSlowElements(this, capacity, new_length - 1, |
| 16043 | &new_capacity); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16044 | } |
| 16045 | |
| 16046 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16047 | const double AllocationSite::kPretenureRatio = 0.85; |
| 16048 | |
| 16049 | |
| 16050 | void AllocationSite::ResetPretenureDecision() { |
| 16051 | set_pretenure_decision(kUndecided); |
| 16052 | set_memento_found_count(0); |
| 16053 | set_memento_create_count(0); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16054 | } |
| 16055 | |
| 16056 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16057 | PretenureFlag AllocationSite::GetPretenureMode() { |
| 16058 | PretenureDecision mode = pretenure_decision(); |
| 16059 | // Zombie objects "decide" to be untenured. |
| 16060 | return mode == kTenure ? TENURED : NOT_TENURED; |
| 16061 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16062 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16063 | |
| 16064 | bool AllocationSite::IsNestedSite() { |
| 16065 | DCHECK(FLAG_trace_track_allocation_sites); |
| 16066 | Object* current = GetHeap()->allocation_sites_list(); |
| 16067 | while (current->IsAllocationSite()) { |
| 16068 | AllocationSite* current_site = AllocationSite::cast(current); |
| 16069 | if (current_site->nested_site() == this) { |
| 16070 | return true; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16071 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16072 | current = current_site->weak_next(); |
| 16073 | } |
| 16074 | return false; |
| 16075 | } |
| 16076 | |
| 16077 | |
| 16078 | void AllocationSite::DigestTransitionFeedback(Handle<AllocationSite> site, |
| 16079 | ElementsKind to_kind) { |
| 16080 | Isolate* isolate = site->GetIsolate(); |
| 16081 | |
| 16082 | if (site->SitePointsToLiteral() && site->transition_info()->IsJSArray()) { |
| 16083 | Handle<JSArray> transition_info = |
| 16084 | handle(JSArray::cast(site->transition_info())); |
| 16085 | ElementsKind kind = transition_info->GetElementsKind(); |
| 16086 | // if kind is holey ensure that to_kind is as well. |
| 16087 | if (IsHoleyElementsKind(kind)) { |
| 16088 | to_kind = GetHoleyElementsKind(to_kind); |
| 16089 | } |
| 16090 | if (IsMoreGeneralElementsKindTransition(kind, to_kind)) { |
| 16091 | // If the array is huge, it's not likely to be defined in a local |
| 16092 | // function, so we shouldn't make new instances of it very often. |
| 16093 | uint32_t length = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16094 | CHECK(transition_info->length()->ToArrayLength(&length)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16095 | if (length <= kMaximumArrayBytesToPretransition) { |
| 16096 | if (FLAG_trace_track_allocation_sites) { |
| 16097 | bool is_nested = site->IsNestedSite(); |
| 16098 | PrintF( |
| 16099 | "AllocationSite: JSArray %p boilerplate %s updated %s->%s\n", |
| 16100 | reinterpret_cast<void*>(*site), |
| 16101 | is_nested ? "(nested)" : "", |
| 16102 | ElementsKindToString(kind), |
| 16103 | ElementsKindToString(to_kind)); |
| 16104 | } |
| 16105 | JSObject::TransitionElementsKind(transition_info, to_kind); |
| 16106 | site->dependent_code()->DeoptimizeDependentCodeGroup( |
| 16107 | isolate, DependentCode::kAllocationSiteTransitionChangedGroup); |
| 16108 | } |
| 16109 | } |
| 16110 | } else { |
| 16111 | ElementsKind kind = site->GetElementsKind(); |
| 16112 | // if kind is holey ensure that to_kind is as well. |
| 16113 | if (IsHoleyElementsKind(kind)) { |
| 16114 | to_kind = GetHoleyElementsKind(to_kind); |
| 16115 | } |
| 16116 | if (IsMoreGeneralElementsKindTransition(kind, to_kind)) { |
| 16117 | if (FLAG_trace_track_allocation_sites) { |
| 16118 | PrintF("AllocationSite: JSArray %p site updated %s->%s\n", |
| 16119 | reinterpret_cast<void*>(*site), |
| 16120 | ElementsKindToString(kind), |
| 16121 | ElementsKindToString(to_kind)); |
| 16122 | } |
| 16123 | site->SetElementsKind(to_kind); |
| 16124 | site->dependent_code()->DeoptimizeDependentCodeGroup( |
| 16125 | isolate, DependentCode::kAllocationSiteTransitionChangedGroup); |
| 16126 | } |
| 16127 | } |
| 16128 | } |
| 16129 | |
| 16130 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16131 | const char* AllocationSite::PretenureDecisionName(PretenureDecision decision) { |
| 16132 | switch (decision) { |
| 16133 | case kUndecided: return "undecided"; |
| 16134 | case kDontTenure: return "don't tenure"; |
| 16135 | case kMaybeTenure: return "maybe tenure"; |
| 16136 | case kTenure: return "tenure"; |
| 16137 | case kZombie: return "zombie"; |
| 16138 | default: UNREACHABLE(); |
| 16139 | } |
| 16140 | return NULL; |
| 16141 | } |
| 16142 | |
| 16143 | |
| 16144 | void JSObject::UpdateAllocationSite(Handle<JSObject> object, |
| 16145 | ElementsKind to_kind) { |
| 16146 | if (!object->IsJSArray()) return; |
| 16147 | |
| 16148 | Heap* heap = object->GetHeap(); |
| 16149 | if (!heap->InNewSpace(*object)) return; |
| 16150 | |
| 16151 | Handle<AllocationSite> site; |
| 16152 | { |
| 16153 | DisallowHeapAllocation no_allocation; |
| 16154 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 16155 | AllocationMemento* memento = |
| 16156 | heap->FindAllocationMemento<Heap::kForRuntime>(*object); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16157 | if (memento == NULL) return; |
| 16158 | |
| 16159 | // Walk through to the Allocation Site |
| 16160 | site = handle(memento->GetAllocationSite()); |
| 16161 | } |
| 16162 | AllocationSite::DigestTransitionFeedback(site, to_kind); |
| 16163 | } |
| 16164 | |
| 16165 | |
| 16166 | void JSObject::TransitionElementsKind(Handle<JSObject> object, |
| 16167 | ElementsKind to_kind) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16168 | ElementsKind from_kind = object->GetElementsKind(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16169 | |
| 16170 | if (IsFastHoleyElementsKind(from_kind)) { |
| 16171 | to_kind = GetHoleyElementsKind(to_kind); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16172 | } |
| 16173 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16174 | if (from_kind == to_kind) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16175 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16176 | // This method should never be called for any other case. |
| 16177 | DCHECK(IsFastElementsKind(from_kind)); |
| 16178 | DCHECK(IsFastElementsKind(to_kind)); |
| 16179 | DCHECK_NE(TERMINAL_FAST_ELEMENTS_KIND, from_kind); |
| 16180 | |
| 16181 | UpdateAllocationSite(object, to_kind); |
| 16182 | if (object->elements() == object->GetHeap()->empty_fixed_array() || |
| 16183 | IsFastDoubleElementsKind(from_kind) == |
| 16184 | IsFastDoubleElementsKind(to_kind)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16185 | // No change is needed to the elements() buffer, the transition |
| 16186 | // only requires a map change. |
| 16187 | Handle<Map> new_map = GetElementsTransitionMap(object, to_kind); |
| 16188 | MigrateToMap(object, new_map); |
| 16189 | if (FLAG_trace_elements_transitions) { |
| 16190 | Handle<FixedArrayBase> elms(object->elements()); |
| 16191 | PrintElementsTransition(stdout, object, from_kind, elms, to_kind, elms); |
| 16192 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16193 | } else { |
| 16194 | DCHECK((IsFastSmiElementsKind(from_kind) && |
| 16195 | IsFastDoubleElementsKind(to_kind)) || |
| 16196 | (IsFastDoubleElementsKind(from_kind) && |
| 16197 | IsFastObjectElementsKind(to_kind))); |
| 16198 | uint32_t c = static_cast<uint32_t>(object->elements()->length()); |
| 16199 | ElementsAccessor::ForKind(to_kind)->GrowCapacityAndConvert(object, c); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16200 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16201 | } |
| 16202 | |
| 16203 | |
| 16204 | // static |
| 16205 | bool Map::IsValidElementsTransition(ElementsKind from_kind, |
| 16206 | ElementsKind to_kind) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16207 | // Transitions can't go backwards. |
| 16208 | if (!IsMoreGeneralElementsKindTransition(from_kind, to_kind)) { |
| 16209 | return false; |
| 16210 | } |
| 16211 | |
| 16212 | // Transitions from HOLEY -> PACKED are not allowed. |
| 16213 | return !IsFastHoleyElementsKind(from_kind) || |
| 16214 | IsFastHoleyElementsKind(to_kind); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16215 | } |
| 16216 | |
| 16217 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16218 | bool JSArray::HasReadOnlyLength(Handle<JSArray> array) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 16219 | Map* map = array->map(); |
| 16220 | // Fast path: "length" is the first fast property of arrays. Since it's not |
| 16221 | // configurable, it's guaranteed to be the first in the descriptor array. |
| 16222 | if (!map->is_dictionary_map()) { |
| 16223 | DCHECK(map->instance_descriptors()->GetKey(0) == |
| 16224 | array->GetHeap()->length_string()); |
| 16225 | return map->instance_descriptors()->GetDetails(0).IsReadOnly(); |
| 16226 | } |
| 16227 | |
| 16228 | Isolate* isolate = array->GetIsolate(); |
| 16229 | LookupIterator it(array, isolate->factory()->length_string(), array, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16230 | LookupIterator::OWN_SKIP_INTERCEPTOR); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16231 | CHECK_EQ(LookupIterator::ACCESSOR, it.state()); |
| 16232 | return it.IsReadOnly(); |
| 16233 | } |
| 16234 | |
| 16235 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16236 | bool JSArray::WouldChangeReadOnlyLength(Handle<JSArray> array, |
| 16237 | uint32_t index) { |
| 16238 | uint32_t length = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16239 | CHECK(array->length()->ToArrayLength(&length)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16240 | if (length <= index) return HasReadOnlyLength(array); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16241 | return false; |
| 16242 | } |
| 16243 | |
| 16244 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16245 | template <typename BackingStore> |
| 16246 | static int FastHoleyElementsUsage(JSObject* object, BackingStore* store) { |
| 16247 | int limit = object->IsJSArray() |
| 16248 | ? Smi::cast(JSArray::cast(object)->length())->value() |
| 16249 | : store->length(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 16250 | int used = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16251 | for (int i = 0; i < limit; ++i) { |
| 16252 | if (!store->is_the_hole(i)) ++used; |
| 16253 | } |
| 16254 | return used; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 16255 | } |
| 16256 | |
| 16257 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16258 | int JSObject::GetFastElementsUsage() { |
| 16259 | FixedArrayBase* store = elements(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16260 | switch (GetElementsKind()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16261 | case FAST_SMI_ELEMENTS: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16262 | case FAST_DOUBLE_ELEMENTS: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16263 | case FAST_ELEMENTS: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16264 | return IsJSArray() ? Smi::cast(JSArray::cast(this)->length())->value() |
| 16265 | : store->length(); |
| 16266 | case FAST_SLOPPY_ARGUMENTS_ELEMENTS: |
| 16267 | store = FixedArray::cast(FixedArray::cast(store)->get(1)); |
| 16268 | // Fall through. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16269 | case FAST_HOLEY_SMI_ELEMENTS: |
| 16270 | case FAST_HOLEY_ELEMENTS: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 16271 | case FAST_STRING_WRAPPER_ELEMENTS: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16272 | return FastHoleyElementsUsage(this, FixedArray::cast(store)); |
| 16273 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
| 16274 | if (elements()->length() == 0) return 0; |
| 16275 | return FastHoleyElementsUsage(this, FixedDoubleArray::cast(store)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16276 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16277 | case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 16278 | case SLOW_STRING_WRAPPER_ELEMENTS: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16279 | case DICTIONARY_ELEMENTS: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 16280 | case NO_ELEMENTS: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16281 | #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16282 | case TYPE##_ELEMENTS: \ |
| 16283 | |
| 16284 | TYPED_ARRAYS(TYPED_ARRAY_CASE) |
| 16285 | #undef TYPED_ARRAY_CASE |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16286 | UNREACHABLE(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16287 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16288 | return 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16289 | } |
| 16290 | |
| 16291 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16292 | // Certain compilers request function template instantiation when they |
| 16293 | // see the definition of the other template functions in the |
| 16294 | // class. This requires us to have the template functions put |
| 16295 | // together, so even though this function belongs in objects-debug.cc, |
| 16296 | // we keep it here instead to satisfy certain compilers. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 16297 | #ifdef OBJECT_PRINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16298 | template <typename Derived, typename Shape, typename Key> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16299 | void Dictionary<Derived, Shape, Key>::Print(std::ostream& os) { // NOLINT |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16300 | int capacity = this->Capacity(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16301 | for (int i = 0; i < capacity; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16302 | Object* k = this->KeyAt(i); |
| 16303 | if (this->IsKey(k)) { |
| 16304 | os << "\n "; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16305 | if (k->IsString()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16306 | String::cast(k)->StringPrint(os); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16307 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16308 | os << Brief(k); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16309 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16310 | os << ": " << Brief(this->ValueAt(i)) << " " << this->DetailsAt(i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16311 | } |
| 16312 | } |
| 16313 | } |
| 16314 | #endif |
| 16315 | |
| 16316 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16317 | template<typename Derived, typename Shape, typename Key> |
| 16318 | void Dictionary<Derived, Shape, Key>::CopyValuesTo(FixedArray* elements) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16319 | int pos = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16320 | int capacity = this->Capacity(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16321 | DisallowHeapAllocation no_gc; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 16322 | WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16323 | for (int i = 0; i < capacity; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16324 | Object* k = this->KeyAt(i); |
| 16325 | if (this->IsKey(k)) { |
| 16326 | elements->set(pos++, this->ValueAt(i), mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16327 | } |
| 16328 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16329 | DCHECK(pos == elements->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16330 | } |
| 16331 | |
| 16332 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16333 | MaybeHandle<Object> JSObject::GetPropertyWithInterceptor(LookupIterator* it, |
| 16334 | bool* done) { |
| 16335 | *done = false; |
| 16336 | Isolate* isolate = it->isolate(); |
| 16337 | // Make sure that the top context does not change when doing callbacks or |
| 16338 | // interceptor calls. |
| 16339 | AssertNoContextChange ncc(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16340 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16341 | DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state()); |
| 16342 | Handle<InterceptorInfo> interceptor = it->GetInterceptor(); |
| 16343 | if (interceptor->getter()->IsUndefined()) { |
| 16344 | return isolate->factory()->undefined_value(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16345 | } |
| 16346 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16347 | Handle<JSObject> holder = it->GetHolder<JSObject>(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 16348 | Handle<Object> result; |
| 16349 | Handle<Object> receiver = it->GetReceiver(); |
| 16350 | if (!receiver->IsJSReceiver()) { |
| 16351 | ASSIGN_RETURN_ON_EXCEPTION( |
| 16352 | isolate, receiver, Object::ConvertReceiver(isolate, receiver), Object); |
| 16353 | } |
| 16354 | PropertyCallbackArguments args(isolate, interceptor->data(), *receiver, |
| 16355 | *holder, Object::DONT_THROW); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16356 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16357 | if (it->IsElement()) { |
| 16358 | uint32_t index = it->index(); |
| 16359 | v8::IndexedPropertyGetterCallback getter = |
| 16360 | v8::ToCData<v8::IndexedPropertyGetterCallback>(interceptor->getter()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16361 | result = args.Call(getter, index); |
| 16362 | } else { |
| 16363 | Handle<Name> name = it->name(); |
| 16364 | DCHECK(!name->IsPrivate()); |
| 16365 | |
| 16366 | if (name->IsSymbol() && !interceptor->can_intercept_symbols()) { |
| 16367 | return isolate->factory()->undefined_value(); |
| 16368 | } |
| 16369 | |
| 16370 | v8::GenericNamedPropertyGetterCallback getter = |
| 16371 | v8::ToCData<v8::GenericNamedPropertyGetterCallback>( |
| 16372 | interceptor->getter()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 16373 | result = args.Call(getter, name); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16374 | } |
| 16375 | |
| 16376 | RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 16377 | if (result.is_null()) return isolate->factory()->undefined_value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16378 | *done = true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16379 | // Rebox handle before return |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 16380 | return handle(*result, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16381 | } |
| 16382 | |
| 16383 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16384 | Maybe<bool> JSObject::HasRealNamedProperty(Handle<JSObject> object, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16385 | Handle<Name> name) { |
| 16386 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 16387 | name->GetIsolate(), object, name, LookupIterator::OWN_SKIP_INTERCEPTOR); |
| 16388 | return HasProperty(&it); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16389 | } |
| 16390 | |
| 16391 | |
| 16392 | Maybe<bool> JSObject::HasRealElementProperty(Handle<JSObject> object, |
| 16393 | uint32_t index) { |
| 16394 | Isolate* isolate = object->GetIsolate(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 16395 | LookupIterator it(isolate, object, index, object, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16396 | LookupIterator::OWN_SKIP_INTERCEPTOR); |
| 16397 | return HasProperty(&it); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16398 | } |
| 16399 | |
| 16400 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16401 | Maybe<bool> JSObject::HasRealNamedCallbackProperty(Handle<JSObject> object, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16402 | Handle<Name> name) { |
| 16403 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 16404 | name->GetIsolate(), object, name, LookupIterator::OWN_SKIP_INTERCEPTOR); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16405 | Maybe<PropertyAttributes> maybe_result = GetPropertyAttributes(&it); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16406 | return maybe_result.IsJust() ? Just(it.state() == LookupIterator::ACCESSOR) |
| 16407 | : Nothing<bool>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16408 | } |
| 16409 | |
| 16410 | |
| 16411 | void FixedArray::SwapPairs(FixedArray* numbers, int i, int j) { |
| 16412 | Object* temp = get(i); |
| 16413 | set(i, get(j)); |
| 16414 | set(j, temp); |
| 16415 | if (this != numbers) { |
| 16416 | temp = numbers->get(i); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16417 | numbers->set(i, Smi::cast(numbers->get(j))); |
| 16418 | numbers->set(j, Smi::cast(temp)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16419 | } |
| 16420 | } |
| 16421 | |
| 16422 | |
| 16423 | static void InsertionSortPairs(FixedArray* content, |
| 16424 | FixedArray* numbers, |
| 16425 | int len) { |
| 16426 | for (int i = 1; i < len; i++) { |
| 16427 | int j = i; |
| 16428 | while (j > 0 && |
| 16429 | (NumberToUint32(numbers->get(j - 1)) > |
| 16430 | NumberToUint32(numbers->get(j)))) { |
| 16431 | content->SwapPairs(numbers, j - 1, j); |
| 16432 | j--; |
| 16433 | } |
| 16434 | } |
| 16435 | } |
| 16436 | |
| 16437 | |
| 16438 | void HeapSortPairs(FixedArray* content, FixedArray* numbers, int len) { |
| 16439 | // In-place heap sort. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16440 | DCHECK(content->length() == numbers->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16441 | |
| 16442 | // Bottom-up max-heap construction. |
| 16443 | for (int i = 1; i < len; ++i) { |
| 16444 | int child_index = i; |
| 16445 | while (child_index > 0) { |
| 16446 | int parent_index = ((child_index + 1) >> 1) - 1; |
| 16447 | uint32_t parent_value = NumberToUint32(numbers->get(parent_index)); |
| 16448 | uint32_t child_value = NumberToUint32(numbers->get(child_index)); |
| 16449 | if (parent_value < child_value) { |
| 16450 | content->SwapPairs(numbers, parent_index, child_index); |
| 16451 | } else { |
| 16452 | break; |
| 16453 | } |
| 16454 | child_index = parent_index; |
| 16455 | } |
| 16456 | } |
| 16457 | |
| 16458 | // Extract elements and create sorted array. |
| 16459 | for (int i = len - 1; i > 0; --i) { |
| 16460 | // Put max element at the back of the array. |
| 16461 | content->SwapPairs(numbers, 0, i); |
| 16462 | // Sift down the new top element. |
| 16463 | int parent_index = 0; |
| 16464 | while (true) { |
| 16465 | int child_index = ((parent_index + 1) << 1) - 1; |
| 16466 | if (child_index >= i) break; |
| 16467 | uint32_t child1_value = NumberToUint32(numbers->get(child_index)); |
| 16468 | uint32_t child2_value = NumberToUint32(numbers->get(child_index + 1)); |
| 16469 | uint32_t parent_value = NumberToUint32(numbers->get(parent_index)); |
| 16470 | if (child_index + 1 >= i || child1_value > child2_value) { |
| 16471 | if (parent_value > child1_value) break; |
| 16472 | content->SwapPairs(numbers, parent_index, child_index); |
| 16473 | parent_index = child_index; |
| 16474 | } else { |
| 16475 | if (parent_value > child2_value) break; |
| 16476 | content->SwapPairs(numbers, parent_index, child_index + 1); |
| 16477 | parent_index = child_index + 1; |
| 16478 | } |
| 16479 | } |
| 16480 | } |
| 16481 | } |
| 16482 | |
| 16483 | |
| 16484 | // Sort this array and the numbers as pairs wrt. the (distinct) numbers. |
| 16485 | void FixedArray::SortPairs(FixedArray* numbers, uint32_t len) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16486 | DCHECK(this->length() == numbers->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16487 | // For small arrays, simply use insertion sort. |
| 16488 | if (len <= 10) { |
| 16489 | InsertionSortPairs(this, numbers, len); |
| 16490 | return; |
| 16491 | } |
| 16492 | // Check the range of indices. |
| 16493 | uint32_t min_index = NumberToUint32(numbers->get(0)); |
| 16494 | uint32_t max_index = min_index; |
| 16495 | uint32_t i; |
| 16496 | for (i = 1; i < len; i++) { |
| 16497 | if (NumberToUint32(numbers->get(i)) < min_index) { |
| 16498 | min_index = NumberToUint32(numbers->get(i)); |
| 16499 | } else if (NumberToUint32(numbers->get(i)) > max_index) { |
| 16500 | max_index = NumberToUint32(numbers->get(i)); |
| 16501 | } |
| 16502 | } |
| 16503 | if (max_index - min_index + 1 == len) { |
| 16504 | // Indices form a contiguous range, unless there are duplicates. |
| 16505 | // Do an in-place linear time sort assuming distinct numbers, but |
| 16506 | // avoid hanging in case they are not. |
| 16507 | for (i = 0; i < len; i++) { |
| 16508 | uint32_t p; |
| 16509 | uint32_t j = 0; |
| 16510 | // While the current element at i is not at its correct position p, |
| 16511 | // swap the elements at these two positions. |
| 16512 | while ((p = NumberToUint32(numbers->get(i)) - min_index) != i && |
| 16513 | j++ < len) { |
| 16514 | SwapPairs(numbers, i, p); |
| 16515 | } |
| 16516 | } |
| 16517 | } else { |
| 16518 | HeapSortPairs(this, numbers, len); |
| 16519 | return; |
| 16520 | } |
| 16521 | } |
| 16522 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16523 | void JSObject::CollectOwnPropertyNames(KeyAccumulator* keys, |
| 16524 | PropertyFilter filter) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16525 | if (HasFastProperties()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16526 | int real_size = map()->NumberOfOwnDescriptors(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16527 | Handle<DescriptorArray> descs(map()->instance_descriptors()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16528 | for (int i = 0; i < real_size; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16529 | PropertyDetails details = descs->GetDetails(i); |
| 16530 | if ((details.attributes() & filter) != 0) continue; |
| 16531 | if (filter & ONLY_ALL_CAN_READ) { |
| 16532 | if (details.kind() != kAccessor) continue; |
| 16533 | Object* accessors = descs->GetValue(i); |
| 16534 | if (!accessors->IsAccessorInfo()) continue; |
| 16535 | if (!AccessorInfo::cast(accessors)->all_can_read()) continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16536 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16537 | Name* key = descs->GetKey(i); |
| 16538 | if (key->FilterKey(filter)) continue; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 16539 | keys->AddKey(key, DO_NOT_CONVERT); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16540 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16541 | } else if (IsJSGlobalObject()) { |
| 16542 | GlobalDictionary::CollectKeysTo(handle(global_dictionary()), keys, filter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16543 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16544 | NameDictionary::CollectKeysTo(handle(property_dictionary()), keys, filter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16545 | } |
| 16546 | } |
| 16547 | |
| 16548 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16549 | int JSObject::NumberOfOwnElements(PropertyFilter filter) { |
| 16550 | // Fast case for objects with no elements. |
| 16551 | if (!IsJSValue() && HasFastElements()) { |
| 16552 | uint32_t length = |
| 16553 | IsJSArray() |
| 16554 | ? static_cast<uint32_t>( |
| 16555 | Smi::cast(JSArray::cast(this)->length())->value()) |
| 16556 | : static_cast<uint32_t>(FixedArrayBase::cast(elements())->length()); |
| 16557 | if (length == 0) return 0; |
| 16558 | } |
| 16559 | // Compute the number of enumerable elements. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16560 | return GetOwnElementKeys(NULL, filter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16561 | } |
| 16562 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16563 | void JSObject::CollectOwnElementKeys(Handle<JSObject> object, |
| 16564 | KeyAccumulator* keys, |
| 16565 | PropertyFilter filter) { |
| 16566 | if (filter & SKIP_STRINGS) return; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16567 | ElementsAccessor* accessor = object->GetElementsAccessor(); |
| 16568 | accessor->CollectElementIndices(object, keys, kMaxUInt32, filter, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16569 | } |
| 16570 | |
| 16571 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16572 | int JSObject::GetOwnElementKeys(FixedArray* storage, PropertyFilter filter) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16573 | int counter = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16574 | |
| 16575 | // If this is a String wrapper, add the string indices first, |
| 16576 | // as they're guaranteed to precede the elements in numerical order |
| 16577 | // and ascending order is required by ECMA-262, 6th, 9.1.12. |
| 16578 | if (IsJSValue()) { |
| 16579 | Object* val = JSValue::cast(this)->value(); |
| 16580 | if (val->IsString()) { |
| 16581 | String* str = String::cast(val); |
| 16582 | if (storage) { |
| 16583 | for (int i = 0; i < str->length(); i++) { |
| 16584 | storage->set(counter + i, Smi::FromInt(i)); |
| 16585 | } |
| 16586 | } |
| 16587 | counter += str->length(); |
| 16588 | } |
| 16589 | } |
| 16590 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16591 | switch (GetElementsKind()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16592 | case FAST_SMI_ELEMENTS: |
| 16593 | case FAST_ELEMENTS: |
| 16594 | case FAST_HOLEY_SMI_ELEMENTS: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 16595 | case FAST_HOLEY_ELEMENTS: |
| 16596 | case FAST_STRING_WRAPPER_ELEMENTS: { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16597 | int length = IsJSArray() ? |
| 16598 | Smi::cast(JSArray::cast(this)->length())->value() : |
| 16599 | FixedArray::cast(elements())->length(); |
| 16600 | for (int i = 0; i < length; i++) { |
| 16601 | if (!FixedArray::cast(elements())->get(i)->IsTheHole()) { |
| 16602 | if (storage != NULL) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 16603 | storage->set(counter, Smi::FromInt(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16604 | } |
| 16605 | counter++; |
| 16606 | } |
| 16607 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16608 | DCHECK(!storage || storage->length() >= counter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16609 | break; |
| 16610 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16611 | case FAST_DOUBLE_ELEMENTS: |
| 16612 | case FAST_HOLEY_DOUBLE_ELEMENTS: { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16613 | int length = IsJSArray() ? |
| 16614 | Smi::cast(JSArray::cast(this)->length())->value() : |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16615 | FixedArrayBase::cast(elements())->length(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16616 | for (int i = 0; i < length; i++) { |
| 16617 | if (!FixedDoubleArray::cast(elements())->is_the_hole(i)) { |
| 16618 | if (storage != NULL) { |
| 16619 | storage->set(counter, Smi::FromInt(i)); |
| 16620 | } |
| 16621 | counter++; |
| 16622 | } |
| 16623 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16624 | DCHECK(!storage || storage->length() >= counter); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16625 | break; |
| 16626 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16627 | |
| 16628 | #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16629 | case TYPE##_ELEMENTS: \ |
| 16630 | |
| 16631 | TYPED_ARRAYS(TYPED_ARRAY_CASE) |
| 16632 | #undef TYPED_ARRAY_CASE |
| 16633 | { |
| 16634 | int length = FixedArrayBase::cast(elements())->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16635 | while (counter < length) { |
| 16636 | if (storage != NULL) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 16637 | storage->set(counter, Smi::FromInt(counter)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16638 | } |
| 16639 | counter++; |
| 16640 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16641 | DCHECK(!storage || storage->length() >= counter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16642 | break; |
| 16643 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16644 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 16645 | case DICTIONARY_ELEMENTS: |
| 16646 | case SLOW_STRING_WRAPPER_ELEMENTS: { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16647 | if (storage != NULL) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16648 | element_dictionary()->CopyKeysTo(storage, counter, filter, |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 16649 | SeededNumberDictionary::SORTED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16650 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16651 | counter += element_dictionary()->NumberOfElementsFilterAttributes(filter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16652 | break; |
| 16653 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16654 | case FAST_SLOPPY_ARGUMENTS_ELEMENTS: |
| 16655 | case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16656 | FixedArray* parameter_map = FixedArray::cast(elements()); |
| 16657 | int mapped_length = parameter_map->length() - 2; |
| 16658 | FixedArray* arguments = FixedArray::cast(parameter_map->get(1)); |
| 16659 | if (arguments->IsDictionary()) { |
| 16660 | // Copy the keys from arguments first, because Dictionary::CopyKeysTo |
| 16661 | // will insert in storage starting at index 0. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 16662 | SeededNumberDictionary* dictionary = |
| 16663 | SeededNumberDictionary::cast(arguments); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16664 | if (storage != NULL) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16665 | dictionary->CopyKeysTo(storage, counter, filter, |
| 16666 | SeededNumberDictionary::UNSORTED); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16667 | } |
| 16668 | counter += dictionary->NumberOfElementsFilterAttributes(filter); |
| 16669 | for (int i = 0; i < mapped_length; ++i) { |
| 16670 | if (!parameter_map->get(i + 2)->IsTheHole()) { |
| 16671 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
| 16672 | ++counter; |
| 16673 | } |
| 16674 | } |
| 16675 | if (storage != NULL) storage->SortPairs(storage, counter); |
| 16676 | |
| 16677 | } else { |
| 16678 | int backing_length = arguments->length(); |
| 16679 | int i = 0; |
| 16680 | for (; i < mapped_length; ++i) { |
| 16681 | if (!parameter_map->get(i + 2)->IsTheHole()) { |
| 16682 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
| 16683 | ++counter; |
| 16684 | } else if (i < backing_length && !arguments->get(i)->IsTheHole()) { |
| 16685 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
| 16686 | ++counter; |
| 16687 | } |
| 16688 | } |
| 16689 | for (; i < backing_length; ++i) { |
| 16690 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
| 16691 | ++counter; |
| 16692 | } |
| 16693 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16694 | break; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 16695 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 16696 | case NO_ELEMENTS: |
| 16697 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16698 | } |
| 16699 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16700 | DCHECK(!storage || storage->length() == counter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16701 | return counter; |
| 16702 | } |
| 16703 | |
| 16704 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16705 | MaybeHandle<String> Object::ObjectProtoToString(Isolate* isolate, |
| 16706 | Handle<Object> object) { |
| 16707 | if (object->IsUndefined()) return isolate->factory()->undefined_to_string(); |
| 16708 | if (object->IsNull()) return isolate->factory()->null_to_string(); |
| 16709 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 16710 | Handle<JSReceiver> receiver = |
| 16711 | Object::ToObject(isolate, object).ToHandleChecked(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16712 | |
| 16713 | Handle<String> tag; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 16714 | Handle<Object> to_string_tag; |
| 16715 | ASSIGN_RETURN_ON_EXCEPTION( |
| 16716 | isolate, to_string_tag, |
| 16717 | JSReceiver::GetProperty(receiver, |
| 16718 | isolate->factory()->to_string_tag_symbol()), |
| 16719 | String); |
| 16720 | if (to_string_tag->IsString()) { |
| 16721 | tag = Handle<String>::cast(to_string_tag); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16722 | } |
| 16723 | |
| 16724 | if (tag.is_null()) { |
| 16725 | ASSIGN_RETURN_ON_EXCEPTION(isolate, tag, |
| 16726 | JSReceiver::BuiltinStringTag(receiver), String); |
| 16727 | } |
| 16728 | |
| 16729 | IncrementalStringBuilder builder(isolate); |
| 16730 | builder.AppendCString("[object "); |
| 16731 | builder.AppendString(tag); |
| 16732 | builder.AppendCharacter(']'); |
| 16733 | return builder.Finish(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16734 | } |
| 16735 | |
| 16736 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16737 | const char* Symbol::PrivateSymbolToName() const { |
| 16738 | Heap* heap = GetIsolate()->heap(); |
| 16739 | #define SYMBOL_CHECK_AND_PRINT(name) \ |
| 16740 | if (this == heap->name()) return #name; |
| 16741 | PRIVATE_SYMBOL_LIST(SYMBOL_CHECK_AND_PRINT) |
| 16742 | #undef SYMBOL_CHECK_AND_PRINT |
| 16743 | return "UNKNOWN"; |
| 16744 | } |
| 16745 | |
| 16746 | |
| 16747 | void Symbol::SymbolShortPrint(std::ostream& os) { |
| 16748 | os << "<Symbol: " << Hash(); |
| 16749 | if (!name()->IsUndefined()) { |
| 16750 | os << " "; |
| 16751 | HeapStringAllocator allocator; |
| 16752 | StringStream accumulator(&allocator); |
| 16753 | String::cast(name())->StringShortPrint(&accumulator); |
| 16754 | os << accumulator.ToCString().get(); |
| 16755 | } else { |
| 16756 | os << " (" << PrivateSymbolToName() << ")"; |
| 16757 | } |
| 16758 | os << ">"; |
| 16759 | } |
| 16760 | |
| 16761 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16762 | // StringSharedKeys are used as keys in the eval cache. |
| 16763 | class StringSharedKey : public HashTableKey { |
| 16764 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16765 | StringSharedKey(Handle<String> source, Handle<SharedFunctionInfo> shared, |
| 16766 | LanguageMode language_mode, int scope_position) |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 16767 | : source_(source), |
| 16768 | shared_(shared), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16769 | language_mode_(language_mode), |
| 16770 | scope_position_(scope_position) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16771 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16772 | bool IsMatch(Object* other) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16773 | DisallowHeapAllocation no_allocation; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16774 | if (!other->IsFixedArray()) { |
| 16775 | if (!other->IsNumber()) return false; |
| 16776 | uint32_t other_hash = static_cast<uint32_t>(other->Number()); |
| 16777 | return Hash() == other_hash; |
| 16778 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16779 | FixedArray* other_array = FixedArray::cast(other); |
| 16780 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16781 | if (shared != *shared_) return false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16782 | int language_unchecked = Smi::cast(other_array->get(2))->value(); |
| 16783 | DCHECK(is_valid_language_mode(language_unchecked)); |
| 16784 | LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked); |
| 16785 | if (language_mode != language_mode_) return false; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16786 | int scope_position = Smi::cast(other_array->get(3))->value(); |
| 16787 | if (scope_position != scope_position_) return false; |
| 16788 | String* source = String::cast(other_array->get(1)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16789 | return source->Equals(*source_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16790 | } |
| 16791 | |
| 16792 | static uint32_t StringSharedHashHelper(String* source, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 16793 | SharedFunctionInfo* shared, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16794 | LanguageMode language_mode, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16795 | int scope_position) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16796 | uint32_t hash = source->Hash(); |
| 16797 | if (shared->HasSourceCode()) { |
| 16798 | // Instead of using the SharedFunctionInfo pointer in the hash |
| 16799 | // code computation, we use a combination of the hash of the |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16800 | // script source code and the start position of the calling scope. |
| 16801 | // We do this to ensure that the cache entries can survive garbage |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16802 | // collection. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16803 | Script* script(Script::cast(shared->script())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16804 | hash ^= String::cast(script->source())->Hash(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16805 | STATIC_ASSERT(LANGUAGE_END == 3); |
| 16806 | if (is_strict(language_mode)) hash ^= 0x8000; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16807 | hash += scope_position; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16808 | } |
| 16809 | return hash; |
| 16810 | } |
| 16811 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16812 | uint32_t Hash() override { |
| 16813 | return StringSharedHashHelper(*source_, *shared_, language_mode_, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16814 | scope_position_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16815 | } |
| 16816 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16817 | uint32_t HashForObject(Object* obj) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16818 | DisallowHeapAllocation no_allocation; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 16819 | if (obj->IsNumber()) { |
| 16820 | return static_cast<uint32_t>(obj->Number()); |
| 16821 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16822 | FixedArray* other_array = FixedArray::cast(obj); |
| 16823 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0)); |
| 16824 | String* source = String::cast(other_array->get(1)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16825 | int language_unchecked = Smi::cast(other_array->get(2))->value(); |
| 16826 | DCHECK(is_valid_language_mode(language_unchecked)); |
| 16827 | LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16828 | int scope_position = Smi::cast(other_array->get(3))->value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16829 | return StringSharedHashHelper(source, shared, language_mode, |
| 16830 | scope_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16831 | } |
| 16832 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16833 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16834 | Handle<Object> AsHandle(Isolate* isolate) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16835 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(4); |
| 16836 | array->set(0, *shared_); |
| 16837 | array->set(1, *source_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16838 | array->set(2, Smi::FromInt(language_mode_)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16839 | array->set(3, Smi::FromInt(scope_position_)); |
| 16840 | return array; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16841 | } |
| 16842 | |
| 16843 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16844 | Handle<String> source_; |
| 16845 | Handle<SharedFunctionInfo> shared_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16846 | LanguageMode language_mode_; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 16847 | int scope_position_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16848 | }; |
| 16849 | |
| 16850 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16851 | namespace { |
| 16852 | |
| 16853 | JSRegExp::Flags RegExpFlagsFromString(Handle<String> flags, bool* success) { |
| 16854 | JSRegExp::Flags value = JSRegExp::kNone; |
| 16855 | int length = flags->length(); |
| 16856 | // A longer flags string cannot be valid. |
| 16857 | if (length > 5) return JSRegExp::Flags(0); |
| 16858 | for (int i = 0; i < length; i++) { |
| 16859 | JSRegExp::Flag flag = JSRegExp::kNone; |
| 16860 | switch (flags->Get(i)) { |
| 16861 | case 'g': |
| 16862 | flag = JSRegExp::kGlobal; |
| 16863 | break; |
| 16864 | case 'i': |
| 16865 | flag = JSRegExp::kIgnoreCase; |
| 16866 | break; |
| 16867 | case 'm': |
| 16868 | flag = JSRegExp::kMultiline; |
| 16869 | break; |
| 16870 | case 'u': |
| 16871 | if (!FLAG_harmony_unicode_regexps) return JSRegExp::Flags(0); |
| 16872 | flag = JSRegExp::kUnicode; |
| 16873 | break; |
| 16874 | case 'y': |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16875 | flag = JSRegExp::kSticky; |
| 16876 | break; |
| 16877 | default: |
| 16878 | return JSRegExp::Flags(0); |
| 16879 | } |
| 16880 | // Duplicate flag. |
| 16881 | if (value & flag) return JSRegExp::Flags(0); |
| 16882 | value |= flag; |
| 16883 | } |
| 16884 | *success = true; |
| 16885 | return value; |
| 16886 | } |
| 16887 | |
| 16888 | } // namespace |
| 16889 | |
| 16890 | |
| 16891 | // static |
| 16892 | MaybeHandle<JSRegExp> JSRegExp::New(Handle<String> pattern, Flags flags) { |
| 16893 | Isolate* isolate = pattern->GetIsolate(); |
| 16894 | Handle<JSFunction> constructor = isolate->regexp_function(); |
| 16895 | Handle<JSRegExp> regexp = |
| 16896 | Handle<JSRegExp>::cast(isolate->factory()->NewJSObject(constructor)); |
| 16897 | |
| 16898 | return JSRegExp::Initialize(regexp, pattern, flags); |
| 16899 | } |
| 16900 | |
| 16901 | |
| 16902 | // static |
| 16903 | MaybeHandle<JSRegExp> JSRegExp::New(Handle<String> pattern, |
| 16904 | Handle<String> flags_string) { |
| 16905 | Isolate* isolate = pattern->GetIsolate(); |
| 16906 | bool success = false; |
| 16907 | Flags flags = RegExpFlagsFromString(flags_string, &success); |
| 16908 | if (!success) { |
| 16909 | THROW_NEW_ERROR( |
| 16910 | isolate, |
| 16911 | NewSyntaxError(MessageTemplate::kInvalidRegExpFlags, flags_string), |
| 16912 | JSRegExp); |
| 16913 | } |
| 16914 | return New(pattern, flags); |
| 16915 | } |
| 16916 | |
| 16917 | |
| 16918 | // static |
| 16919 | Handle<JSRegExp> JSRegExp::Copy(Handle<JSRegExp> regexp) { |
| 16920 | Isolate* const isolate = regexp->GetIsolate(); |
| 16921 | return Handle<JSRegExp>::cast(isolate->factory()->CopyJSObject(regexp)); |
| 16922 | } |
| 16923 | |
| 16924 | |
| 16925 | template <typename Char> |
| 16926 | inline int CountRequiredEscapes(Handle<String> source) { |
| 16927 | DisallowHeapAllocation no_gc; |
| 16928 | int escapes = 0; |
| 16929 | Vector<const Char> src = source->GetCharVector<Char>(); |
| 16930 | for (int i = 0; i < src.length(); i++) { |
| 16931 | if (src[i] == '/' && (i == 0 || src[i - 1] != '\\')) escapes++; |
| 16932 | } |
| 16933 | return escapes; |
| 16934 | } |
| 16935 | |
| 16936 | |
| 16937 | template <typename Char, typename StringType> |
| 16938 | inline Handle<StringType> WriteEscapedRegExpSource(Handle<String> source, |
| 16939 | Handle<StringType> result) { |
| 16940 | DisallowHeapAllocation no_gc; |
| 16941 | Vector<const Char> src = source->GetCharVector<Char>(); |
| 16942 | Vector<Char> dst(result->GetChars(), result->length()); |
| 16943 | int s = 0; |
| 16944 | int d = 0; |
| 16945 | while (s < src.length()) { |
| 16946 | if (src[s] == '/' && (s == 0 || src[s - 1] != '\\')) dst[d++] = '\\'; |
| 16947 | dst[d++] = src[s++]; |
| 16948 | } |
| 16949 | DCHECK_EQ(result->length(), d); |
| 16950 | return result; |
| 16951 | } |
| 16952 | |
| 16953 | |
| 16954 | MaybeHandle<String> EscapeRegExpSource(Isolate* isolate, |
| 16955 | Handle<String> source) { |
| 16956 | String::Flatten(source); |
| 16957 | if (source->length() == 0) return isolate->factory()->query_colon_string(); |
| 16958 | bool one_byte = source->IsOneByteRepresentationUnderneath(); |
| 16959 | int escapes = one_byte ? CountRequiredEscapes<uint8_t>(source) |
| 16960 | : CountRequiredEscapes<uc16>(source); |
| 16961 | if (escapes == 0) return source; |
| 16962 | int length = source->length() + escapes; |
| 16963 | if (one_byte) { |
| 16964 | Handle<SeqOneByteString> result; |
| 16965 | ASSIGN_RETURN_ON_EXCEPTION(isolate, result, |
| 16966 | isolate->factory()->NewRawOneByteString(length), |
| 16967 | String); |
| 16968 | return WriteEscapedRegExpSource<uint8_t>(source, result); |
| 16969 | } else { |
| 16970 | Handle<SeqTwoByteString> result; |
| 16971 | ASSIGN_RETURN_ON_EXCEPTION(isolate, result, |
| 16972 | isolate->factory()->NewRawTwoByteString(length), |
| 16973 | String); |
| 16974 | return WriteEscapedRegExpSource<uc16>(source, result); |
| 16975 | } |
| 16976 | } |
| 16977 | |
| 16978 | |
| 16979 | // static |
| 16980 | MaybeHandle<JSRegExp> JSRegExp::Initialize(Handle<JSRegExp> regexp, |
| 16981 | Handle<String> source, |
| 16982 | Handle<String> flags_string) { |
| 16983 | Isolate* isolate = source->GetIsolate(); |
| 16984 | bool success = false; |
| 16985 | Flags flags = RegExpFlagsFromString(flags_string, &success); |
| 16986 | if (!success) { |
| 16987 | THROW_NEW_ERROR( |
| 16988 | isolate, |
| 16989 | NewSyntaxError(MessageTemplate::kInvalidRegExpFlags, flags_string), |
| 16990 | JSRegExp); |
| 16991 | } |
| 16992 | return Initialize(regexp, source, flags); |
| 16993 | } |
| 16994 | |
| 16995 | |
| 16996 | // static |
| 16997 | MaybeHandle<JSRegExp> JSRegExp::Initialize(Handle<JSRegExp> regexp, |
| 16998 | Handle<String> source, Flags flags) { |
| 16999 | Isolate* isolate = regexp->GetIsolate(); |
| 17000 | Factory* factory = isolate->factory(); |
| 17001 | // If source is the empty string we set it to "(?:)" instead as |
| 17002 | // suggested by ECMA-262, 5th, section 15.10.4.1. |
| 17003 | if (source->length() == 0) source = factory->query_colon_string(); |
| 17004 | |
| 17005 | Handle<String> escaped_source; |
| 17006 | ASSIGN_RETURN_ON_EXCEPTION(isolate, escaped_source, |
| 17007 | EscapeRegExpSource(isolate, source), JSRegExp); |
| 17008 | |
| 17009 | regexp->set_source(*escaped_source); |
| 17010 | regexp->set_flags(Smi::FromInt(flags)); |
| 17011 | |
| 17012 | Map* map = regexp->map(); |
| 17013 | Object* constructor = map->GetConstructor(); |
| 17014 | if (constructor->IsJSFunction() && |
| 17015 | JSFunction::cast(constructor)->initial_map() == map) { |
| 17016 | // If we still have the original map, set in-object properties directly. |
| 17017 | regexp->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex, |
| 17018 | Smi::FromInt(0), SKIP_WRITE_BARRIER); |
| 17019 | } else { |
| 17020 | // Map has changed, so use generic, but slower, method. |
| 17021 | PropertyAttributes writable = |
| 17022 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE); |
| 17023 | JSObject::SetOwnPropertyIgnoreAttributes( |
| 17024 | regexp, factory->last_index_string(), |
| 17025 | Handle<Smi>(Smi::FromInt(0), isolate), writable) |
| 17026 | .Check(); |
| 17027 | } |
| 17028 | |
| 17029 | RETURN_ON_EXCEPTION(isolate, RegExpImpl::Compile(regexp, source, flags), |
| 17030 | JSRegExp); |
| 17031 | |
| 17032 | return regexp; |
| 17033 | } |
| 17034 | |
| 17035 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17036 | // RegExpKey carries the source and flags of a regular expression as key. |
| 17037 | class RegExpKey : public HashTableKey { |
| 17038 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17039 | RegExpKey(Handle<String> string, JSRegExp::Flags flags) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17040 | : string_(string), flags_(Smi::FromInt(flags)) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17041 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 17042 | // Rather than storing the key in the hash table, a pointer to the |
| 17043 | // stored value is stored where the key should be. IsMatch then |
| 17044 | // compares the search key to the found object, rather than comparing |
| 17045 | // a key to a key. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17046 | bool IsMatch(Object* obj) override { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17047 | FixedArray* val = FixedArray::cast(obj); |
| 17048 | return string_->Equals(String::cast(val->get(JSRegExp::kSourceIndex))) |
| 17049 | && (flags_ == val->get(JSRegExp::kFlagsIndex)); |
| 17050 | } |
| 17051 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17052 | uint32_t Hash() override { return RegExpHash(*string_, flags_); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17053 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17054 | Handle<Object> AsHandle(Isolate* isolate) override { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17055 | // Plain hash maps, which is where regexp keys are used, don't |
| 17056 | // use this function. |
| 17057 | UNREACHABLE(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17058 | return MaybeHandle<Object>().ToHandleChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17059 | } |
| 17060 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17061 | uint32_t HashForObject(Object* obj) override { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17062 | FixedArray* val = FixedArray::cast(obj); |
| 17063 | return RegExpHash(String::cast(val->get(JSRegExp::kSourceIndex)), |
| 17064 | Smi::cast(val->get(JSRegExp::kFlagsIndex))); |
| 17065 | } |
| 17066 | |
| 17067 | static uint32_t RegExpHash(String* string, Smi* flags) { |
| 17068 | return string->Hash() + flags->value(); |
| 17069 | } |
| 17070 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17071 | Handle<String> string_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17072 | Smi* flags_; |
| 17073 | }; |
| 17074 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17075 | |
| 17076 | Handle<Object> OneByteStringKey::AsHandle(Isolate* isolate) { |
| 17077 | if (hash_field_ == 0) Hash(); |
| 17078 | return isolate->factory()->NewOneByteInternalizedString(string_, hash_field_); |
| 17079 | } |
| 17080 | |
| 17081 | |
| 17082 | Handle<Object> TwoByteStringKey::AsHandle(Isolate* isolate) { |
| 17083 | if (hash_field_ == 0) Hash(); |
| 17084 | return isolate->factory()->NewTwoByteInternalizedString(string_, hash_field_); |
| 17085 | } |
| 17086 | |
| 17087 | |
| 17088 | Handle<Object> SeqOneByteSubStringKey::AsHandle(Isolate* isolate) { |
| 17089 | if (hash_field_ == 0) Hash(); |
| 17090 | return isolate->factory()->NewOneByteInternalizedSubString( |
| 17091 | string_, from_, length_, hash_field_); |
| 17092 | } |
| 17093 | |
| 17094 | |
| 17095 | bool SeqOneByteSubStringKey::IsMatch(Object* string) { |
| 17096 | Vector<const uint8_t> chars(string_->GetChars() + from_, length_); |
| 17097 | return String::cast(string)->IsOneByteEqualTo(chars); |
| 17098 | } |
| 17099 | |
| 17100 | |
| 17101 | // InternalizedStringKey carries a string/internalized-string object as key. |
| 17102 | class InternalizedStringKey : public HashTableKey { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17103 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17104 | explicit InternalizedStringKey(Handle<String> string) |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 17105 | : string_(String::Flatten(string)) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17106 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17107 | bool IsMatch(Object* string) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17108 | return String::cast(string)->Equals(*string_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17109 | } |
| 17110 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17111 | uint32_t Hash() override { return string_->Hash(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17112 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17113 | uint32_t HashForObject(Object* other) override { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17114 | return String::cast(other)->Hash(); |
| 17115 | } |
| 17116 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17117 | Handle<Object> AsHandle(Isolate* isolate) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17118 | // Internalize the string if possible. |
| 17119 | MaybeHandle<Map> maybe_map = |
| 17120 | isolate->factory()->InternalizedStringMapForString(string_); |
| 17121 | Handle<Map> map; |
| 17122 | if (maybe_map.ToHandle(&map)) { |
| 17123 | string_->set_map_no_write_barrier(*map); |
| 17124 | DCHECK(string_->IsInternalizedString()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17125 | return string_; |
| 17126 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17127 | // Otherwise allocate a new internalized string. |
| 17128 | return isolate->factory()->NewInternalizedStringImpl( |
| 17129 | string_, string_->length(), string_->hash_field()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17130 | } |
| 17131 | |
| 17132 | static uint32_t StringHash(Object* obj) { |
| 17133 | return String::cast(obj)->Hash(); |
| 17134 | } |
| 17135 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17136 | Handle<String> string_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17137 | }; |
| 17138 | |
| 17139 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17140 | template<typename Derived, typename Shape, typename Key> |
| 17141 | void HashTable<Derived, Shape, Key>::IteratePrefix(ObjectVisitor* v) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17142 | BodyDescriptorBase::IteratePointers(this, 0, kElementsStartOffset, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17143 | } |
| 17144 | |
| 17145 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17146 | template<typename Derived, typename Shape, typename Key> |
| 17147 | void HashTable<Derived, Shape, Key>::IterateElements(ObjectVisitor* v) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17148 | BodyDescriptorBase::IteratePointers(this, kElementsStartOffset, |
| 17149 | kHeaderSize + length() * kPointerSize, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17150 | } |
| 17151 | |
| 17152 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17153 | template<typename Derived, typename Shape, typename Key> |
| 17154 | Handle<Derived> HashTable<Derived, Shape, Key>::New( |
| 17155 | Isolate* isolate, |
| 17156 | int at_least_space_for, |
| 17157 | MinimumCapacity capacity_option, |
| 17158 | PretenureFlag pretenure) { |
| 17159 | DCHECK(0 <= at_least_space_for); |
| 17160 | DCHECK(!capacity_option || base::bits::IsPowerOfTwo32(at_least_space_for)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17161 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17162 | int capacity = (capacity_option == USE_CUSTOM_MINIMUM_CAPACITY) |
| 17163 | ? at_least_space_for |
| 17164 | : ComputeCapacity(at_least_space_for); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17165 | if (capacity > HashTable::kMaxCapacity) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17166 | v8::internal::Heap::FatalProcessOutOfMemory("invalid table size", true); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 17167 | } |
| 17168 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17169 | Factory* factory = isolate->factory(); |
| 17170 | int length = EntryToIndex(capacity); |
| 17171 | Handle<FixedArray> array = factory->NewFixedArray(length, pretenure); |
| 17172 | array->set_map_no_write_barrier(*factory->hash_table_map()); |
| 17173 | Handle<Derived> table = Handle<Derived>::cast(array); |
| 17174 | |
| 17175 | table->SetNumberOfElements(0); |
| 17176 | table->SetNumberOfDeletedElements(0); |
| 17177 | table->SetCapacity(capacity); |
| 17178 | return table; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17179 | } |
| 17180 | |
| 17181 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 17182 | // Find entry for key otherwise return kNotFound. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17183 | template <typename Derived, typename Shape> |
| 17184 | int NameDictionaryBase<Derived, Shape>::FindEntry(Handle<Name> key) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17185 | if (!key->IsUniqueName()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17186 | return DerivedDictionary::FindEntry(key); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 17187 | } |
| 17188 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17189 | // Optimized for unique names. Knowledge of the key type allows: |
| 17190 | // 1. Move the check if the key is unique out of the loop. |
| 17191 | // 2. Avoid comparing hash codes in unique-to-unique comparison. |
| 17192 | // 3. Detect a case when a dictionary key is not unique but the key is. |
| 17193 | // In case of positive result the dictionary key may be replaced by the |
| 17194 | // internalized string with minimal performance penalty. It gives a chance |
| 17195 | // to perform further lookups in code stubs (and significant performance |
| 17196 | // boost a certain style of code). |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 17197 | |
| 17198 | // EnsureCapacity will guarantee the hash table is never full. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17199 | uint32_t capacity = this->Capacity(); |
| 17200 | uint32_t entry = Derived::FirstProbe(key->Hash(), capacity); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 17201 | uint32_t count = 1; |
| 17202 | |
| 17203 | while (true) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17204 | int index = Derived::EntryToIndex(entry); |
| 17205 | Object* element = this->get(index); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 17206 | if (element->IsUndefined()) break; // Empty entry. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17207 | if (*key == element) return entry; |
| 17208 | if (!element->IsUniqueName() && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 17209 | !element->IsTheHole() && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17210 | Name::cast(element)->Equals(*key)) { |
| 17211 | // Replace a key that is a non-internalized string by the equivalent |
| 17212 | // internalized string for faster further lookups. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17213 | this->set(index, *key); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 17214 | return entry; |
| 17215 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17216 | DCHECK(element->IsTheHole() || !Name::cast(element)->Equals(*key)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17217 | entry = Derived::NextProbe(entry, count++, capacity); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 17218 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17219 | return Derived::kNotFound; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 17220 | } |
| 17221 | |
| 17222 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17223 | template<typename Derived, typename Shape, typename Key> |
| 17224 | void HashTable<Derived, Shape, Key>::Rehash( |
| 17225 | Handle<Derived> new_table, |
| 17226 | Key key) { |
| 17227 | DCHECK(NumberOfElements() < new_table->Capacity()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 17228 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17229 | DisallowHeapAllocation no_gc; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17230 | WriteBarrierMode mode = new_table->GetWriteBarrierMode(no_gc); |
| 17231 | |
| 17232 | // Copy prefix to new array. |
| 17233 | for (int i = kPrefixStartIndex; |
| 17234 | i < kPrefixStartIndex + Shape::kPrefixSize; |
| 17235 | i++) { |
| 17236 | new_table->set(i, get(i), mode); |
| 17237 | } |
| 17238 | |
| 17239 | // Rehash the elements. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17240 | int capacity = this->Capacity(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17241 | for (int i = 0; i < capacity; i++) { |
| 17242 | uint32_t from_index = EntryToIndex(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17243 | Object* k = this->get(from_index); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17244 | if (IsKey(k)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17245 | uint32_t hash = this->HashForObject(key, k); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17246 | uint32_t insertion_index = |
| 17247 | EntryToIndex(new_table->FindInsertionEntry(hash)); |
| 17248 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 17249 | new_table->set(insertion_index + j, get(from_index + j), mode); |
| 17250 | } |
| 17251 | } |
| 17252 | } |
| 17253 | new_table->SetNumberOfElements(NumberOfElements()); |
| 17254 | new_table->SetNumberOfDeletedElements(0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17255 | } |
| 17256 | |
| 17257 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17258 | template<typename Derived, typename Shape, typename Key> |
| 17259 | uint32_t HashTable<Derived, Shape, Key>::EntryForProbe( |
| 17260 | Key key, |
| 17261 | Object* k, |
| 17262 | int probe, |
| 17263 | uint32_t expected) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17264 | uint32_t hash = this->HashForObject(key, k); |
| 17265 | uint32_t capacity = this->Capacity(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17266 | uint32_t entry = FirstProbe(hash, capacity); |
| 17267 | for (int i = 1; i < probe; i++) { |
| 17268 | if (entry == expected) return expected; |
| 17269 | entry = NextProbe(entry, i, capacity); |
| 17270 | } |
| 17271 | return entry; |
| 17272 | } |
| 17273 | |
| 17274 | |
| 17275 | template<typename Derived, typename Shape, typename Key> |
| 17276 | void HashTable<Derived, Shape, Key>::Swap(uint32_t entry1, |
| 17277 | uint32_t entry2, |
| 17278 | WriteBarrierMode mode) { |
| 17279 | int index1 = EntryToIndex(entry1); |
| 17280 | int index2 = EntryToIndex(entry2); |
| 17281 | Object* temp[Shape::kEntrySize]; |
| 17282 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 17283 | temp[j] = get(index1 + j); |
| 17284 | } |
| 17285 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 17286 | set(index1 + j, get(index2 + j), mode); |
| 17287 | } |
| 17288 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 17289 | set(index2 + j, temp[j], mode); |
| 17290 | } |
| 17291 | } |
| 17292 | |
| 17293 | |
| 17294 | template<typename Derived, typename Shape, typename Key> |
| 17295 | void HashTable<Derived, Shape, Key>::Rehash(Key key) { |
| 17296 | DisallowHeapAllocation no_gc; |
| 17297 | WriteBarrierMode mode = GetWriteBarrierMode(no_gc); |
| 17298 | uint32_t capacity = Capacity(); |
| 17299 | bool done = false; |
| 17300 | for (int probe = 1; !done; probe++) { |
| 17301 | // All elements at entries given by one of the first _probe_ probes |
| 17302 | // are placed correctly. Other elements might need to be moved. |
| 17303 | done = true; |
| 17304 | for (uint32_t current = 0; current < capacity; current++) { |
| 17305 | Object* current_key = get(EntryToIndex(current)); |
| 17306 | if (IsKey(current_key)) { |
| 17307 | uint32_t target = EntryForProbe(key, current_key, probe, current); |
| 17308 | if (current == target) continue; |
| 17309 | Object* target_key = get(EntryToIndex(target)); |
| 17310 | if (!IsKey(target_key) || |
| 17311 | EntryForProbe(key, target_key, probe, target) != target) { |
| 17312 | // Put the current element into the correct position. |
| 17313 | Swap(current, target, mode); |
| 17314 | // The other element will be processed on the next iteration. |
| 17315 | current--; |
| 17316 | } else { |
| 17317 | // The place for the current element is occupied. Leave the element |
| 17318 | // for the next probe. |
| 17319 | done = false; |
| 17320 | } |
| 17321 | } |
| 17322 | } |
| 17323 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 17324 | // Wipe deleted entries. |
| 17325 | Heap* heap = GetHeap(); |
| 17326 | Object* the_hole = heap->the_hole_value(); |
| 17327 | Object* undefined = heap->undefined_value(); |
| 17328 | for (uint32_t current = 0; current < capacity; current++) { |
| 17329 | if (get(EntryToIndex(current)) == the_hole) { |
| 17330 | set(EntryToIndex(current), undefined); |
| 17331 | } |
| 17332 | } |
| 17333 | SetNumberOfDeletedElements(0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17334 | } |
| 17335 | |
| 17336 | |
| 17337 | template<typename Derived, typename Shape, typename Key> |
| 17338 | Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity( |
| 17339 | Handle<Derived> table, |
| 17340 | int n, |
| 17341 | Key key, |
| 17342 | PretenureFlag pretenure) { |
| 17343 | Isolate* isolate = table->GetIsolate(); |
| 17344 | int capacity = table->Capacity(); |
| 17345 | int nof = table->NumberOfElements() + n; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17346 | |
| 17347 | if (table->HasSufficientCapacity(n)) return table; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17348 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 17349 | const int kMinCapacityForPretenure = 256; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17350 | bool should_pretenure = pretenure == TENURED || |
| 17351 | ((capacity > kMinCapacityForPretenure) && |
| 17352 | !isolate->heap()->InNewSpace(*table)); |
| 17353 | Handle<Derived> new_table = HashTable::New( |
| 17354 | isolate, |
| 17355 | nof * 2, |
| 17356 | USE_DEFAULT_MINIMUM_CAPACITY, |
| 17357 | should_pretenure ? TENURED : NOT_TENURED); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 17358 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17359 | table->Rehash(new_table, key); |
| 17360 | return new_table; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17361 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17362 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17363 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17364 | template <typename Derived, typename Shape, typename Key> |
| 17365 | bool HashTable<Derived, Shape, Key>::HasSufficientCapacity(int n) { |
| 17366 | int capacity = Capacity(); |
| 17367 | int nof = NumberOfElements() + n; |
| 17368 | int nod = NumberOfDeletedElements(); |
| 17369 | // Return true if: |
| 17370 | // 50% is still free after adding n elements and |
| 17371 | // at most 50% of the free elements are deleted elements. |
| 17372 | if (nod <= (capacity - nof) >> 1) { |
| 17373 | int needed_free = nof >> 1; |
| 17374 | if (nof + needed_free <= capacity) return true; |
| 17375 | } |
| 17376 | return false; |
| 17377 | } |
| 17378 | |
| 17379 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17380 | template<typename Derived, typename Shape, typename Key> |
| 17381 | Handle<Derived> HashTable<Derived, Shape, Key>::Shrink(Handle<Derived> table, |
| 17382 | Key key) { |
| 17383 | int capacity = table->Capacity(); |
| 17384 | int nof = table->NumberOfElements(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17385 | |
| 17386 | // Shrink to fit the number of elements if only a quarter of the |
| 17387 | // capacity is filled with elements. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17388 | if (nof > (capacity >> 2)) return table; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17389 | // Allocate a new dictionary with room for at least the current |
| 17390 | // number of elements. The allocation method will make sure that |
| 17391 | // there is extra room in the dictionary for additions. Don't go |
| 17392 | // lower than room for 16 elements. |
| 17393 | int at_least_room_for = nof; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17394 | if (at_least_room_for < 16) return table; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17395 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17396 | Isolate* isolate = table->GetIsolate(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17397 | const int kMinCapacityForPretenure = 256; |
| 17398 | bool pretenure = |
| 17399 | (at_least_room_for > kMinCapacityForPretenure) && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17400 | !isolate->heap()->InNewSpace(*table); |
| 17401 | Handle<Derived> new_table = HashTable::New( |
| 17402 | isolate, |
| 17403 | at_least_room_for, |
| 17404 | USE_DEFAULT_MINIMUM_CAPACITY, |
| 17405 | pretenure ? TENURED : NOT_TENURED); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17406 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17407 | table->Rehash(new_table, key); |
| 17408 | return new_table; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17409 | } |
| 17410 | |
| 17411 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17412 | template<typename Derived, typename Shape, typename Key> |
| 17413 | uint32_t HashTable<Derived, Shape, Key>::FindInsertionEntry(uint32_t hash) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17414 | uint32_t capacity = Capacity(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 17415 | uint32_t entry = FirstProbe(hash, capacity); |
| 17416 | uint32_t count = 1; |
| 17417 | // EnsureCapacity will guarantee the hash table is never full. |
| 17418 | while (true) { |
| 17419 | Object* element = KeyAt(entry); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 17420 | if (element->IsUndefined() || element->IsTheHole()) break; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 17421 | entry = NextProbe(entry, count++, capacity); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17422 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17423 | return entry; |
| 17424 | } |
| 17425 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17426 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17427 | // Force instantiation of template instances class. |
| 17428 | // Please note this list is compiler dependent. |
| 17429 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17430 | template class HashTable<StringTable, StringTableShape, HashTableKey*>; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17431 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17432 | template class HashTable<CompilationCacheTable, |
| 17433 | CompilationCacheShape, |
| 17434 | HashTableKey*>; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17435 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17436 | template class HashTable<ObjectHashTable, |
| 17437 | ObjectHashTableShape, |
| 17438 | Handle<Object> >; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 17439 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17440 | template class HashTable<WeakHashTable, WeakHashTableShape<2>, Handle<Object> >; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17441 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17442 | template class Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17443 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17444 | template class Dictionary<GlobalDictionary, GlobalDictionaryShape, |
| 17445 | Handle<Name> >; |
| 17446 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17447 | template class Dictionary<SeededNumberDictionary, |
| 17448 | SeededNumberDictionaryShape, |
| 17449 | uint32_t>; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17450 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17451 | template class Dictionary<UnseededNumberDictionary, |
| 17452 | UnseededNumberDictionaryShape, |
| 17453 | uint32_t>; |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 17454 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17455 | template Handle<SeededNumberDictionary> |
| 17456 | Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>:: |
| 17457 | New(Isolate*, int at_least_space_for, PretenureFlag pretenure); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 17458 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17459 | template Handle<UnseededNumberDictionary> |
| 17460 | Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>:: |
| 17461 | New(Isolate*, int at_least_space_for, PretenureFlag pretenure); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17462 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17463 | template Handle<NameDictionary> |
| 17464 | Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >:: |
| 17465 | New(Isolate*, int n, PretenureFlag pretenure); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17466 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17467 | template Handle<GlobalDictionary> |
| 17468 | Dictionary<GlobalDictionary, GlobalDictionaryShape, Handle<Name> >::New( |
| 17469 | Isolate*, int n, PretenureFlag pretenure); |
| 17470 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17471 | template Handle<SeededNumberDictionary> |
| 17472 | Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>:: |
| 17473 | AtPut(Handle<SeededNumberDictionary>, uint32_t, Handle<Object>); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17474 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17475 | template Handle<UnseededNumberDictionary> |
| 17476 | Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>:: |
| 17477 | AtPut(Handle<UnseededNumberDictionary>, uint32_t, Handle<Object>); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 17478 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17479 | template Object* |
| 17480 | Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>:: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 17481 | SlowReverseLookup(Object* value); |
| 17482 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17483 | template Object* |
| 17484 | Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >:: |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 17485 | SlowReverseLookup(Object* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17486 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17487 | template Handle<Object> |
| 17488 | Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::DeleteProperty( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17489 | Handle<NameDictionary>, int); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17490 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17491 | template Handle<Object> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17492 | Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, |
| 17493 | uint32_t>::DeleteProperty(Handle<SeededNumberDictionary>, int); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17494 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 17495 | template Handle<Object> |
| 17496 | Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, |
| 17497 | uint32_t>::DeleteProperty(Handle<UnseededNumberDictionary>, int); |
| 17498 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17499 | template Handle<NameDictionary> |
| 17500 | HashTable<NameDictionary, NameDictionaryShape, Handle<Name> >:: |
| 17501 | New(Isolate*, int, MinimumCapacity, PretenureFlag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17502 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17503 | template Handle<NameDictionary> |
| 17504 | HashTable<NameDictionary, NameDictionaryShape, Handle<Name> >:: |
| 17505 | Shrink(Handle<NameDictionary>, Handle<Name>); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17506 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17507 | template Handle<SeededNumberDictionary> |
| 17508 | HashTable<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>:: |
| 17509 | Shrink(Handle<SeededNumberDictionary>, uint32_t); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17510 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 17511 | template Handle<UnseededNumberDictionary> |
| 17512 | HashTable<UnseededNumberDictionary, UnseededNumberDictionaryShape, |
| 17513 | uint32_t>::Shrink(Handle<UnseededNumberDictionary>, uint32_t); |
| 17514 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17515 | template Handle<NameDictionary> |
| 17516 | Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::Add( |
| 17517 | Handle<NameDictionary>, Handle<Name>, Handle<Object>, PropertyDetails); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17518 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17519 | template Handle<GlobalDictionary> |
| 17520 | Dictionary<GlobalDictionary, GlobalDictionaryShape, Handle<Name> >::Add( |
| 17521 | Handle<GlobalDictionary>, Handle<Name>, Handle<Object>, |
| 17522 | PropertyDetails); |
| 17523 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17524 | template Handle<FixedArray> Dictionary< |
| 17525 | NameDictionary, NameDictionaryShape, |
| 17526 | Handle<Name> >::BuildIterationIndicesArray(Handle<NameDictionary>); |
| 17527 | |
| 17528 | template Handle<FixedArray> Dictionary< |
| 17529 | NameDictionary, NameDictionaryShape, |
| 17530 | Handle<Name> >::GenerateNewEnumerationIndices(Handle<NameDictionary>); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 17531 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17532 | template Handle<SeededNumberDictionary> |
| 17533 | Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>:: |
| 17534 | Add(Handle<SeededNumberDictionary>, |
| 17535 | uint32_t, |
| 17536 | Handle<Object>, |
| 17537 | PropertyDetails); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17538 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17539 | template Handle<UnseededNumberDictionary> |
| 17540 | Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>:: |
| 17541 | Add(Handle<UnseededNumberDictionary>, |
| 17542 | uint32_t, |
| 17543 | Handle<Object>, |
| 17544 | PropertyDetails); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17545 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17546 | template Handle<SeededNumberDictionary> |
| 17547 | Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>:: |
| 17548 | EnsureCapacity(Handle<SeededNumberDictionary>, int, uint32_t); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 17549 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17550 | template Handle<UnseededNumberDictionary> |
| 17551 | Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>:: |
| 17552 | EnsureCapacity(Handle<UnseededNumberDictionary>, int, uint32_t); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17553 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17554 | template void Dictionary<NameDictionary, NameDictionaryShape, |
| 17555 | Handle<Name> >::SetRequiresCopyOnCapacityChange(); |
| 17556 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17557 | template Handle<NameDictionary> |
| 17558 | Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >:: |
| 17559 | EnsureCapacity(Handle<NameDictionary>, int, Handle<Name>); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17560 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17561 | template int HashTable<SeededNumberDictionary, SeededNumberDictionaryShape, |
| 17562 | uint32_t>::FindEntry(uint32_t); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 17563 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17564 | template int NameDictionaryBase<NameDictionary, NameDictionaryShape>::FindEntry( |
| 17565 | Handle<Name>); |
| 17566 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 17567 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17568 | Handle<Object> JSObject::PrepareSlowElementsForSort( |
| 17569 | Handle<JSObject> object, uint32_t limit) { |
| 17570 | DCHECK(object->HasDictionaryElements()); |
| 17571 | Isolate* isolate = object->GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17572 | // Must stay in dictionary mode, either because of requires_slow_elements, |
| 17573 | // or because we are not going to sort (and therefore compact) all of the |
| 17574 | // elements. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17575 | Handle<SeededNumberDictionary> dict(object->element_dictionary(), isolate); |
| 17576 | Handle<SeededNumberDictionary> new_dict = |
| 17577 | SeededNumberDictionary::New(isolate, dict->NumberOfElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17578 | |
| 17579 | uint32_t pos = 0; |
| 17580 | uint32_t undefs = 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 17581 | int capacity = dict->Capacity(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17582 | Handle<Smi> bailout(Smi::FromInt(-1), isolate); |
| 17583 | // Entry to the new dictionary does not cause it to grow, as we have |
| 17584 | // allocated one that is large enough for all entries. |
| 17585 | DisallowHeapAllocation no_gc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17586 | for (int i = 0; i < capacity; i++) { |
| 17587 | Object* k = dict->KeyAt(i); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17588 | if (!dict->IsKey(k)) continue; |
| 17589 | |
| 17590 | DCHECK(k->IsNumber()); |
| 17591 | DCHECK(!k->IsSmi() || Smi::cast(k)->value() >= 0); |
| 17592 | DCHECK(!k->IsHeapNumber() || HeapNumber::cast(k)->value() >= 0); |
| 17593 | DCHECK(!k->IsHeapNumber() || HeapNumber::cast(k)->value() <= kMaxUInt32); |
| 17594 | |
| 17595 | HandleScope scope(isolate); |
| 17596 | Handle<Object> value(dict->ValueAt(i), isolate); |
| 17597 | PropertyDetails details = dict->DetailsAt(i); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17598 | if (details.type() == ACCESSOR_CONSTANT || details.IsReadOnly()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17599 | // Bail out and do the sorting of undefineds and array holes in JS. |
| 17600 | // Also bail out if the element is not supposed to be moved. |
| 17601 | return bailout; |
| 17602 | } |
| 17603 | |
| 17604 | uint32_t key = NumberToUint32(k); |
| 17605 | if (key < limit) { |
| 17606 | if (value->IsUndefined()) { |
| 17607 | undefs++; |
| 17608 | } else if (pos > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 17609 | // Adding an entry with the key beyond smi-range requires |
| 17610 | // allocation. Bailout. |
| 17611 | return bailout; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17612 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17613 | Handle<Object> result = SeededNumberDictionary::AddNumberEntry( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17614 | new_dict, pos, value, details, object->map()->is_prototype_map()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17615 | DCHECK(result.is_identical_to(new_dict)); |
| 17616 | USE(result); |
| 17617 | pos++; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17618 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17619 | } else if (key > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 17620 | // Adding an entry with the key beyond smi-range requires |
| 17621 | // allocation. Bailout. |
| 17622 | return bailout; |
| 17623 | } else { |
| 17624 | Handle<Object> result = SeededNumberDictionary::AddNumberEntry( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17625 | new_dict, key, value, details, object->map()->is_prototype_map()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17626 | DCHECK(result.is_identical_to(new_dict)); |
| 17627 | USE(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17628 | } |
| 17629 | } |
| 17630 | |
| 17631 | uint32_t result = pos; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17632 | PropertyDetails no_details = PropertyDetails::Empty(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17633 | while (undefs > 0) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 17634 | if (pos > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 17635 | // Adding an entry with the key beyond smi-range requires |
| 17636 | // allocation. Bailout. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17637 | return bailout; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 17638 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17639 | HandleScope scope(isolate); |
| 17640 | Handle<Object> result = SeededNumberDictionary::AddNumberEntry( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17641 | new_dict, pos, isolate->factory()->undefined_value(), no_details, |
| 17642 | object->map()->is_prototype_map()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17643 | DCHECK(result.is_identical_to(new_dict)); |
| 17644 | USE(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17645 | pos++; |
| 17646 | undefs--; |
| 17647 | } |
| 17648 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17649 | object->set_elements(*new_dict); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17650 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17651 | AllowHeapAllocation allocate_return_value; |
| 17652 | return isolate->factory()->NewNumberFromUint(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17653 | } |
| 17654 | |
| 17655 | |
| 17656 | // Collects all defined (non-hole) and non-undefined (array) elements at |
| 17657 | // the start of the elements array. |
| 17658 | // If the object is in dictionary mode, it is converted to fast elements |
| 17659 | // mode. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17660 | Handle<Object> JSObject::PrepareElementsForSort(Handle<JSObject> object, |
| 17661 | uint32_t limit) { |
| 17662 | Isolate* isolate = object->GetIsolate(); |
| 17663 | if (object->HasSloppyArgumentsElements() || |
| 17664 | object->map()->is_observed()) { |
| 17665 | return handle(Smi::FromInt(-1), isolate); |
| 17666 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 17667 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 17668 | if (object->HasStringWrapperElements()) { |
| 17669 | int len = String::cast(Handle<JSValue>::cast(object)->value())->length(); |
| 17670 | return handle(Smi::FromInt(len), isolate); |
| 17671 | } |
| 17672 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17673 | if (object->HasDictionaryElements()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17674 | // Convert to fast elements containing only the existing properties. |
| 17675 | // Ordering is irrelevant, since we are going to sort anyway. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17676 | Handle<SeededNumberDictionary> dict(object->element_dictionary()); |
| 17677 | if (object->IsJSArray() || dict->requires_slow_elements() || |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17678 | dict->max_number_key() >= limit) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17679 | return JSObject::PrepareSlowElementsForSort(object, limit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17680 | } |
| 17681 | // Convert to fast elements. |
| 17682 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17683 | Handle<Map> new_map = |
| 17684 | JSObject::GetElementsTransitionMap(object, FAST_HOLEY_ELEMENTS); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 17685 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17686 | PretenureFlag tenure = isolate->heap()->InNewSpace(*object) ? |
| 17687 | NOT_TENURED: TENURED; |
| 17688 | Handle<FixedArray> fast_elements = |
| 17689 | isolate->factory()->NewFixedArray(dict->NumberOfElements(), tenure); |
| 17690 | dict->CopyValuesTo(*fast_elements); |
| 17691 | JSObject::ValidateElements(object); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 17692 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17693 | JSObject::SetMapAndElements(object, new_map, fast_elements); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17694 | } else if (object->HasFixedTypedArrayElements()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17695 | // Typed arrays cannot have holes or undefined elements. |
| 17696 | return handle(Smi::FromInt( |
| 17697 | FixedArrayBase::cast(object->elements())->length()), isolate); |
| 17698 | } else if (!object->HasFastDoubleElements()) { |
| 17699 | EnsureWritableFastElements(object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17700 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17701 | DCHECK(object->HasFastSmiOrObjectElements() || |
| 17702 | object->HasFastDoubleElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17703 | |
| 17704 | // Collect holes at the end, undefined before that and the rest at the |
| 17705 | // start, and return the number of non-hole, non-undefined values. |
| 17706 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17707 | Handle<FixedArrayBase> elements_base(object->elements()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17708 | uint32_t elements_length = static_cast<uint32_t>(elements_base->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17709 | if (limit > elements_length) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17710 | limit = elements_length; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17711 | } |
| 17712 | if (limit == 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17713 | return handle(Smi::FromInt(0), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17714 | } |
| 17715 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17716 | uint32_t result = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17717 | if (elements_base->map() == isolate->heap()->fixed_double_array_map()) { |
| 17718 | FixedDoubleArray* elements = FixedDoubleArray::cast(*elements_base); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17719 | // Split elements into defined and the_hole, in that order. |
| 17720 | unsigned int holes = limit; |
| 17721 | // Assume most arrays contain no holes and undefined values, so minimize the |
| 17722 | // number of stores of non-undefined, non-the-hole values. |
| 17723 | for (unsigned int i = 0; i < holes; i++) { |
| 17724 | if (elements->is_the_hole(i)) { |
| 17725 | holes--; |
| 17726 | } else { |
| 17727 | continue; |
| 17728 | } |
| 17729 | // Position i needs to be filled. |
| 17730 | while (holes > i) { |
| 17731 | if (elements->is_the_hole(holes)) { |
| 17732 | holes--; |
| 17733 | } else { |
| 17734 | elements->set(i, elements->get_scalar(holes)); |
| 17735 | break; |
| 17736 | } |
| 17737 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17738 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17739 | result = holes; |
| 17740 | while (holes < limit) { |
| 17741 | elements->set_the_hole(holes); |
| 17742 | holes++; |
| 17743 | } |
| 17744 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17745 | FixedArray* elements = FixedArray::cast(*elements_base); |
| 17746 | DisallowHeapAllocation no_gc; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17747 | |
| 17748 | // Split elements into defined, undefined and the_hole, in that order. Only |
| 17749 | // count locations for undefined and the hole, and fill them afterwards. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17750 | WriteBarrierMode write_barrier = elements->GetWriteBarrierMode(no_gc); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17751 | unsigned int undefs = limit; |
| 17752 | unsigned int holes = limit; |
| 17753 | // Assume most arrays contain no holes and undefined values, so minimize the |
| 17754 | // number of stores of non-undefined, non-the-hole values. |
| 17755 | for (unsigned int i = 0; i < undefs; i++) { |
| 17756 | Object* current = elements->get(i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17757 | if (current->IsTheHole()) { |
| 17758 | holes--; |
| 17759 | undefs--; |
| 17760 | } else if (current->IsUndefined()) { |
| 17761 | undefs--; |
| 17762 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17763 | continue; |
| 17764 | } |
| 17765 | // Position i needs to be filled. |
| 17766 | while (undefs > i) { |
| 17767 | current = elements->get(undefs); |
| 17768 | if (current->IsTheHole()) { |
| 17769 | holes--; |
| 17770 | undefs--; |
| 17771 | } else if (current->IsUndefined()) { |
| 17772 | undefs--; |
| 17773 | } else { |
| 17774 | elements->set(i, current, write_barrier); |
| 17775 | break; |
| 17776 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17777 | } |
| 17778 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 17779 | result = undefs; |
| 17780 | while (undefs < holes) { |
| 17781 | elements->set_undefined(undefs); |
| 17782 | undefs++; |
| 17783 | } |
| 17784 | while (holes < limit) { |
| 17785 | elements->set_the_hole(holes); |
| 17786 | holes++; |
| 17787 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17788 | } |
| 17789 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17790 | return isolate->factory()->NewNumberFromUint(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17791 | } |
| 17792 | |
| 17793 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17794 | ExternalArrayType JSTypedArray::type() { |
| 17795 | switch (elements()->map()->instance_type()) { |
| 17796 | #define INSTANCE_TYPE_TO_ARRAY_TYPE(Type, type, TYPE, ctype, size) \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17797 | case FIXED_##TYPE##_ARRAY_TYPE: \ |
| 17798 | return kExternal##Type##Array; |
| 17799 | |
| 17800 | TYPED_ARRAYS(INSTANCE_TYPE_TO_ARRAY_TYPE) |
| 17801 | #undef INSTANCE_TYPE_TO_ARRAY_TYPE |
| 17802 | |
| 17803 | default: |
| 17804 | UNREACHABLE(); |
| 17805 | return static_cast<ExternalArrayType>(-1); |
| 17806 | } |
| 17807 | } |
| 17808 | |
| 17809 | |
| 17810 | size_t JSTypedArray::element_size() { |
| 17811 | switch (elements()->map()->instance_type()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17812 | #define INSTANCE_TYPE_TO_ELEMENT_SIZE(Type, type, TYPE, ctype, size) \ |
| 17813 | case FIXED_##TYPE##_ARRAY_TYPE: \ |
| 17814 | return size; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17815 | |
| 17816 | TYPED_ARRAYS(INSTANCE_TYPE_TO_ELEMENT_SIZE) |
| 17817 | #undef INSTANCE_TYPE_TO_ELEMENT_SIZE |
| 17818 | |
| 17819 | default: |
| 17820 | UNREACHABLE(); |
| 17821 | return 0; |
| 17822 | } |
| 17823 | } |
| 17824 | |
| 17825 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17826 | void JSGlobalObject::InvalidatePropertyCell(Handle<JSGlobalObject> global, |
| 17827 | Handle<Name> name) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17828 | DCHECK(!global->HasFastProperties()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17829 | auto dictionary = handle(global->global_dictionary()); |
| 17830 | int entry = dictionary->FindEntry(name); |
| 17831 | if (entry == GlobalDictionary::kNotFound) return; |
| 17832 | PropertyCell::InvalidateEntry(dictionary, entry); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17833 | } |
| 17834 | |
| 17835 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17836 | // TODO(ishell): rename to EnsureEmptyPropertyCell or something. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17837 | Handle<PropertyCell> JSGlobalObject::EnsurePropertyCell( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17838 | Handle<JSGlobalObject> global, Handle<Name> name) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17839 | DCHECK(!global->HasFastProperties()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17840 | auto dictionary = handle(global->global_dictionary()); |
| 17841 | int entry = dictionary->FindEntry(name); |
| 17842 | Handle<PropertyCell> cell; |
| 17843 | if (entry != GlobalDictionary::kNotFound) { |
| 17844 | // This call should be idempotent. |
| 17845 | DCHECK(dictionary->ValueAt(entry)->IsPropertyCell()); |
| 17846 | cell = handle(PropertyCell::cast(dictionary->ValueAt(entry))); |
| 17847 | DCHECK(cell->property_details().cell_type() == |
| 17848 | PropertyCellType::kUninitialized || |
| 17849 | cell->property_details().cell_type() == |
| 17850 | PropertyCellType::kInvalidated); |
| 17851 | DCHECK(cell->value()->IsTheHole()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17852 | return cell; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17853 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17854 | Isolate* isolate = global->GetIsolate(); |
| 17855 | cell = isolate->factory()->NewPropertyCell(); |
| 17856 | PropertyDetails details(NONE, DATA, 0, PropertyCellType::kUninitialized); |
| 17857 | dictionary = GlobalDictionary::Add(dictionary, name, cell, details); |
| 17858 | global->set_properties(*dictionary); |
| 17859 | return cell; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17860 | } |
| 17861 | |
| 17862 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17863 | // 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] | 17864 | // If we don't have a hit we don't want to waste much time so we unroll the |
| 17865 | // string hash calculation loop here for speed. Doesn't work if the two |
| 17866 | // characters form a decimal integer, since such strings have a different hash |
| 17867 | // algorithm. |
| 17868 | class TwoCharHashTableKey : public HashTableKey { |
| 17869 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17870 | TwoCharHashTableKey(uint16_t c1, uint16_t c2, uint32_t seed) |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17871 | : c1_(c1), c2_(c2) { |
| 17872 | // Char 1. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 17873 | uint32_t hash = seed; |
| 17874 | hash += c1; |
| 17875 | hash += hash << 10; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17876 | hash ^= hash >> 6; |
| 17877 | // Char 2. |
| 17878 | hash += c2; |
| 17879 | hash += hash << 10; |
| 17880 | hash ^= hash >> 6; |
| 17881 | // GetHash. |
| 17882 | hash += hash << 3; |
| 17883 | hash ^= hash >> 11; |
| 17884 | hash += hash << 15; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17885 | if ((hash & String::kHashBitMask) == 0) hash = StringHasher::kZeroHash; |
| 17886 | hash_ = hash; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17887 | #ifdef DEBUG |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17888 | // If this assert fails then we failed to reproduce the two-character |
| 17889 | // version of the string hashing algorithm above. One reason could be |
| 17890 | // that we were passed two digits as characters, since the hash |
| 17891 | // algorithm is different in that case. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17892 | uint16_t chars[2] = {c1, c2}; |
| 17893 | uint32_t check_hash = StringHasher::HashSequentialString(chars, 2, seed); |
| 17894 | hash = (hash << String::kHashShift) | String::kIsNotArrayIndexMask; |
| 17895 | DCHECK_EQ(static_cast<int32_t>(hash), static_cast<int32_t>(check_hash)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17896 | #endif |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17897 | } |
| 17898 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17899 | bool IsMatch(Object* o) override { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17900 | if (!o->IsString()) return false; |
| 17901 | String* other = String::cast(o); |
| 17902 | if (other->length() != 2) return false; |
| 17903 | if (other->Get(0) != c1_) return false; |
| 17904 | return other->Get(1) == c2_; |
| 17905 | } |
| 17906 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17907 | uint32_t Hash() override { return hash_; } |
| 17908 | uint32_t HashForObject(Object* key) override { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17909 | if (!key->IsString()) return 0; |
| 17910 | return String::cast(key)->Hash(); |
| 17911 | } |
| 17912 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17913 | Handle<Object> AsHandle(Isolate* isolate) override { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17914 | // The TwoCharHashTableKey is only used for looking in the string |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17915 | // table, not for adding to it. |
| 17916 | UNREACHABLE(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17917 | return MaybeHandle<Object>().ToHandleChecked(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17918 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 17919 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17920 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17921 | uint16_t c1_; |
| 17922 | uint16_t c2_; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17923 | uint32_t hash_; |
| 17924 | }; |
| 17925 | |
| 17926 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17927 | MaybeHandle<String> StringTable::InternalizeStringIfExists( |
| 17928 | Isolate* isolate, |
| 17929 | Handle<String> string) { |
| 17930 | if (string->IsInternalizedString()) { |
| 17931 | return string; |
| 17932 | } |
| 17933 | return LookupStringIfExists(isolate, string); |
| 17934 | } |
| 17935 | |
| 17936 | |
| 17937 | MaybeHandle<String> StringTable::LookupStringIfExists( |
| 17938 | Isolate* isolate, |
| 17939 | Handle<String> string) { |
| 17940 | Handle<StringTable> string_table = isolate->factory()->string_table(); |
| 17941 | InternalizedStringKey key(string); |
| 17942 | int entry = string_table->FindEntry(&key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17943 | if (entry == kNotFound) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17944 | return MaybeHandle<String>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17945 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17946 | Handle<String> result(String::cast(string_table->KeyAt(entry)), isolate); |
| 17947 | DCHECK(StringShape(*result).IsInternalized()); |
| 17948 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17949 | } |
| 17950 | } |
| 17951 | |
| 17952 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17953 | MaybeHandle<String> StringTable::LookupTwoCharsStringIfExists( |
| 17954 | Isolate* isolate, |
| 17955 | uint16_t c1, |
| 17956 | uint16_t c2) { |
| 17957 | Handle<StringTable> string_table = isolate->factory()->string_table(); |
| 17958 | TwoCharHashTableKey key(c1, c2, isolate->heap()->HashSeed()); |
| 17959 | int entry = string_table->FindEntry(&key); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17960 | if (entry == kNotFound) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17961 | return MaybeHandle<String>(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17962 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17963 | Handle<String> result(String::cast(string_table->KeyAt(entry)), isolate); |
| 17964 | DCHECK(StringShape(*result).IsInternalized()); |
| 17965 | return result; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 17966 | } |
| 17967 | } |
| 17968 | |
| 17969 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17970 | void StringTable::EnsureCapacityForDeserialization(Isolate* isolate, |
| 17971 | int expected) { |
| 17972 | Handle<StringTable> table = isolate->factory()->string_table(); |
| 17973 | // We need a key instance for the virtual hash function. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 17974 | InternalizedStringKey dummy_key(isolate->factory()->empty_string()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17975 | table = StringTable::EnsureCapacity(table, expected, &dummy_key); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17976 | isolate->heap()->SetRootStringTable(*table); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 17977 | } |
| 17978 | |
| 17979 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17980 | Handle<String> StringTable::LookupString(Isolate* isolate, |
| 17981 | Handle<String> string) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 17982 | if (string->IsConsString() && string->IsFlat()) { |
| 17983 | string = String::Flatten(string); |
| 17984 | if (string->IsInternalizedString()) return string; |
| 17985 | } |
| 17986 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17987 | InternalizedStringKey key(string); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 17988 | Handle<String> result = LookupKey(isolate, &key); |
| 17989 | |
| 17990 | if (string->IsConsString()) { |
| 17991 | Handle<ConsString> cons = Handle<ConsString>::cast(string); |
| 17992 | cons->set_first(*result); |
| 17993 | cons->set_second(isolate->heap()->empty_string()); |
| 17994 | } |
| 17995 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17996 | } |
| 17997 | |
| 17998 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17999 | Handle<String> StringTable::LookupKey(Isolate* isolate, HashTableKey* key) { |
| 18000 | Handle<StringTable> table = isolate->factory()->string_table(); |
| 18001 | int entry = table->FindEntry(key); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 18002 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18003 | // String already in table. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18004 | if (entry != kNotFound) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18005 | return handle(String::cast(table->KeyAt(entry)), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18006 | } |
| 18007 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18008 | // Adding new string. Grow table if needed. |
| 18009 | table = StringTable::EnsureCapacity(table, 1, key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18010 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18011 | // Create string object. |
| 18012 | Handle<Object> string = key->AsHandle(isolate); |
| 18013 | // There must be no attempts to internalize strings that could throw |
| 18014 | // InvalidStringLength error. |
| 18015 | CHECK(!string.is_null()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18016 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18017 | // Add the new string and return it along with the string table. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18018 | entry = table->FindInsertionEntry(key->Hash()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18019 | table->set(EntryToIndex(entry), *string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18020 | table->ElementAdded(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18021 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18022 | isolate->heap()->SetRootStringTable(*table); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18023 | return Handle<String>::cast(string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18024 | } |
| 18025 | |
| 18026 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18027 | String* StringTable::LookupKeyIfExists(Isolate* isolate, HashTableKey* key) { |
| 18028 | Handle<StringTable> table = isolate->factory()->string_table(); |
| 18029 | int entry = table->FindEntry(key); |
| 18030 | if (entry != kNotFound) return String::cast(table->KeyAt(entry)); |
| 18031 | return NULL; |
| 18032 | } |
| 18033 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 18034 | Handle<StringSet> StringSet::New(Isolate* isolate) { |
| 18035 | return HashTable::New(isolate, 0); |
| 18036 | } |
| 18037 | |
| 18038 | Handle<StringSet> StringSet::Add(Handle<StringSet> stringset, |
| 18039 | Handle<String> name) { |
| 18040 | if (!stringset->Has(name)) { |
| 18041 | stringset = EnsureCapacity(stringset, 1, *name); |
| 18042 | uint32_t hash = StringSetShape::Hash(*name); |
| 18043 | int entry = stringset->FindInsertionEntry(hash); |
| 18044 | stringset->set(EntryToIndex(entry), *name); |
| 18045 | stringset->ElementAdded(); |
| 18046 | } |
| 18047 | return stringset; |
| 18048 | } |
| 18049 | |
| 18050 | bool StringSet::Has(Handle<String> name) { |
| 18051 | return FindEntry(*name) != kNotFound; |
| 18052 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18053 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18054 | Handle<Object> CompilationCacheTable::Lookup(Handle<String> src, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18055 | Handle<Context> context, |
| 18056 | LanguageMode language_mode) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18057 | Isolate* isolate = GetIsolate(); |
| 18058 | Handle<SharedFunctionInfo> shared(context->closure()->shared()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18059 | StringSharedKey key(src, shared, language_mode, RelocInfo::kNoPosition); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18060 | int entry = FindEntry(&key); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18061 | if (entry == kNotFound) return isolate->factory()->undefined_value(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18062 | int index = EntryToIndex(entry); |
| 18063 | if (!get(index)->IsFixedArray()) return isolate->factory()->undefined_value(); |
| 18064 | return Handle<Object>(get(index + 1), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18065 | } |
| 18066 | |
| 18067 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18068 | Handle<Object> CompilationCacheTable::LookupEval( |
| 18069 | Handle<String> src, Handle<SharedFunctionInfo> outer_info, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18070 | LanguageMode language_mode, int scope_position) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18071 | Isolate* isolate = GetIsolate(); |
| 18072 | // Cache key is the tuple (source, outer shared function info, scope position) |
| 18073 | // to unambiguously identify the context chain the cached eval code assumes. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18074 | StringSharedKey key(src, outer_info, language_mode, scope_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18075 | int entry = FindEntry(&key); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18076 | if (entry == kNotFound) return isolate->factory()->undefined_value(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18077 | int index = EntryToIndex(entry); |
| 18078 | if (!get(index)->IsFixedArray()) return isolate->factory()->undefined_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18079 | return Handle<Object>(get(EntryToIndex(entry) + 1), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18080 | } |
| 18081 | |
| 18082 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18083 | Handle<Object> CompilationCacheTable::LookupRegExp(Handle<String> src, |
| 18084 | JSRegExp::Flags flags) { |
| 18085 | Isolate* isolate = GetIsolate(); |
| 18086 | DisallowHeapAllocation no_allocation; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18087 | RegExpKey key(src, flags); |
| 18088 | int entry = FindEntry(&key); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18089 | if (entry == kNotFound) return isolate->factory()->undefined_value(); |
| 18090 | return Handle<Object>(get(EntryToIndex(entry) + 1), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18091 | } |
| 18092 | |
| 18093 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18094 | Handle<CompilationCacheTable> CompilationCacheTable::Put( |
| 18095 | Handle<CompilationCacheTable> cache, Handle<String> src, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18096 | Handle<Context> context, LanguageMode language_mode, Handle<Object> value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18097 | Isolate* isolate = cache->GetIsolate(); |
| 18098 | Handle<SharedFunctionInfo> shared(context->closure()->shared()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18099 | StringSharedKey key(src, shared, language_mode, RelocInfo::kNoPosition); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18100 | { |
| 18101 | Handle<Object> k = key.AsHandle(isolate); |
| 18102 | DisallowHeapAllocation no_allocation_scope; |
| 18103 | int entry = cache->FindEntry(&key); |
| 18104 | if (entry != kNotFound) { |
| 18105 | cache->set(EntryToIndex(entry), *k); |
| 18106 | cache->set(EntryToIndex(entry) + 1, *value); |
| 18107 | return cache; |
| 18108 | } |
| 18109 | } |
| 18110 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18111 | cache = EnsureCapacity(cache, 1, &key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18112 | int entry = cache->FindInsertionEntry(key.Hash()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18113 | Handle<Object> k = |
| 18114 | isolate->factory()->NewNumber(static_cast<double>(key.Hash())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18115 | cache->set(EntryToIndex(entry), *k); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18116 | cache->set(EntryToIndex(entry) + 1, Smi::FromInt(kHashGenerations)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18117 | cache->ElementAdded(); |
| 18118 | return cache; |
| 18119 | } |
| 18120 | |
| 18121 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18122 | Handle<CompilationCacheTable> CompilationCacheTable::PutEval( |
| 18123 | Handle<CompilationCacheTable> cache, Handle<String> src, |
| 18124 | Handle<SharedFunctionInfo> outer_info, Handle<SharedFunctionInfo> value, |
| 18125 | int scope_position) { |
| 18126 | Isolate* isolate = cache->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18127 | StringSharedKey key(src, outer_info, value->language_mode(), scope_position); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18128 | { |
| 18129 | Handle<Object> k = key.AsHandle(isolate); |
| 18130 | DisallowHeapAllocation no_allocation_scope; |
| 18131 | int entry = cache->FindEntry(&key); |
| 18132 | if (entry != kNotFound) { |
| 18133 | cache->set(EntryToIndex(entry), *k); |
| 18134 | cache->set(EntryToIndex(entry) + 1, *value); |
| 18135 | return cache; |
| 18136 | } |
| 18137 | } |
| 18138 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18139 | cache = EnsureCapacity(cache, 1, &key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18140 | int entry = cache->FindInsertionEntry(key.Hash()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18141 | Handle<Object> k = |
| 18142 | isolate->factory()->NewNumber(static_cast<double>(key.Hash())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18143 | cache->set(EntryToIndex(entry), *k); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18144 | cache->set(EntryToIndex(entry) + 1, Smi::FromInt(kHashGenerations)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18145 | cache->ElementAdded(); |
| 18146 | return cache; |
| 18147 | } |
| 18148 | |
| 18149 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18150 | Handle<CompilationCacheTable> CompilationCacheTable::PutRegExp( |
| 18151 | Handle<CompilationCacheTable> cache, Handle<String> src, |
| 18152 | JSRegExp::Flags flags, Handle<FixedArray> value) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18153 | RegExpKey key(src, flags); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18154 | cache = EnsureCapacity(cache, 1, &key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18155 | int entry = cache->FindInsertionEntry(key.Hash()); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 18156 | // We store the value in the key slot, and compare the search key |
| 18157 | // to the stored value with a custon IsMatch function during lookups. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18158 | cache->set(EntryToIndex(entry), *value); |
| 18159 | cache->set(EntryToIndex(entry) + 1, *value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18160 | cache->ElementAdded(); |
| 18161 | return cache; |
| 18162 | } |
| 18163 | |
| 18164 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18165 | void CompilationCacheTable::Age() { |
| 18166 | DisallowHeapAllocation no_allocation; |
| 18167 | Object* the_hole_value = GetHeap()->the_hole_value(); |
| 18168 | for (int entry = 0, size = Capacity(); entry < size; entry++) { |
| 18169 | int entry_index = EntryToIndex(entry); |
| 18170 | int value_index = entry_index + 1; |
| 18171 | |
| 18172 | if (get(entry_index)->IsNumber()) { |
| 18173 | Smi* count = Smi::cast(get(value_index)); |
| 18174 | count = Smi::FromInt(count->value() - 1); |
| 18175 | if (count->value() == 0) { |
| 18176 | NoWriteBarrierSet(this, entry_index, the_hole_value); |
| 18177 | NoWriteBarrierSet(this, value_index, the_hole_value); |
| 18178 | ElementRemoved(); |
| 18179 | } else { |
| 18180 | NoWriteBarrierSet(this, value_index, count); |
| 18181 | } |
| 18182 | } else if (get(entry_index)->IsFixedArray()) { |
| 18183 | SharedFunctionInfo* info = SharedFunctionInfo::cast(get(value_index)); |
| 18184 | if (info->code()->kind() != Code::FUNCTION || info->code()->IsOld()) { |
| 18185 | NoWriteBarrierSet(this, entry_index, the_hole_value); |
| 18186 | NoWriteBarrierSet(this, value_index, the_hole_value); |
| 18187 | ElementRemoved(); |
| 18188 | } |
| 18189 | } |
| 18190 | } |
| 18191 | } |
| 18192 | |
| 18193 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 18194 | void CompilationCacheTable::Remove(Object* value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18195 | DisallowHeapAllocation no_allocation; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18196 | Object* the_hole_value = GetHeap()->the_hole_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 18197 | for (int entry = 0, size = Capacity(); entry < size; entry++) { |
| 18198 | int entry_index = EntryToIndex(entry); |
| 18199 | int value_index = entry_index + 1; |
| 18200 | if (get(value_index) == value) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18201 | NoWriteBarrierSet(this, entry_index, the_hole_value); |
| 18202 | NoWriteBarrierSet(this, value_index, the_hole_value); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 18203 | ElementRemoved(); |
| 18204 | } |
| 18205 | } |
| 18206 | return; |
| 18207 | } |
| 18208 | |
| 18209 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18210 | template<typename Derived, typename Shape, typename Key> |
| 18211 | Handle<Derived> Dictionary<Derived, Shape, Key>::New( |
| 18212 | Isolate* isolate, |
| 18213 | int at_least_space_for, |
| 18214 | PretenureFlag pretenure) { |
| 18215 | DCHECK(0 <= at_least_space_for); |
| 18216 | Handle<Derived> dict = DerivedHashTable::New(isolate, |
| 18217 | at_least_space_for, |
| 18218 | USE_DEFAULT_MINIMUM_CAPACITY, |
| 18219 | pretenure); |
| 18220 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 18221 | // Initialize the next enumeration index. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18222 | dict->SetNextEnumerationIndex(PropertyDetails::kInitialIndex); |
| 18223 | return dict; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18224 | } |
| 18225 | |
| 18226 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18227 | template <typename Derived, typename Shape, typename Key> |
| 18228 | Handle<FixedArray> Dictionary<Derived, Shape, Key>::BuildIterationIndicesArray( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18229 | Handle<Derived> dictionary) { |
| 18230 | Factory* factory = dictionary->GetIsolate()->factory(); |
| 18231 | int length = dictionary->NumberOfElements(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18232 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18233 | Handle<FixedArray> iteration_order = factory->NewFixedArray(length); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18234 | Handle<FixedArray> enumeration_order = factory->NewFixedArray(length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18235 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18236 | // Fill both the iteration order array and the enumeration order array |
| 18237 | // with property details. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18238 | int capacity = dictionary->Capacity(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18239 | int pos = 0; |
| 18240 | for (int i = 0; i < capacity; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18241 | if (dictionary->IsKey(dictionary->KeyAt(i))) { |
| 18242 | int index = dictionary->DetailsAt(i).dictionary_index(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18243 | iteration_order->set(pos, Smi::FromInt(i)); |
| 18244 | enumeration_order->set(pos, Smi::FromInt(index)); |
| 18245 | pos++; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18246 | } |
| 18247 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18248 | DCHECK(pos == length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18249 | |
| 18250 | // Sort the arrays wrt. enumeration order. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18251 | iteration_order->SortPairs(*enumeration_order, enumeration_order->length()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18252 | return iteration_order; |
| 18253 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18254 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18255 | |
| 18256 | template <typename Derived, typename Shape, typename Key> |
| 18257 | Handle<FixedArray> |
| 18258 | Dictionary<Derived, Shape, Key>::GenerateNewEnumerationIndices( |
| 18259 | Handle<Derived> dictionary) { |
| 18260 | int length = dictionary->NumberOfElements(); |
| 18261 | |
| 18262 | Handle<FixedArray> iteration_order = BuildIterationIndicesArray(dictionary); |
| 18263 | DCHECK(iteration_order->length() == length); |
| 18264 | |
| 18265 | // Iterate over the dictionary using the enumeration order and update |
| 18266 | // the dictionary with new enumeration indices. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18267 | for (int i = 0; i < length; i++) { |
| 18268 | int index = Smi::cast(iteration_order->get(i))->value(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18269 | DCHECK(dictionary->IsKey(dictionary->KeyAt(index))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18270 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18271 | int enum_index = PropertyDetails::kInitialIndex + i; |
| 18272 | |
| 18273 | PropertyDetails details = dictionary->DetailsAt(index); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18274 | PropertyDetails new_details = details.set_index(enum_index); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18275 | dictionary->DetailsAtPut(index, new_details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18276 | } |
| 18277 | |
| 18278 | // Set the next enumeration index. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18279 | dictionary->SetNextEnumerationIndex(PropertyDetails::kInitialIndex+length); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18280 | return iteration_order; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18281 | } |
| 18282 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18283 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18284 | template <typename Derived, typename Shape, typename Key> |
| 18285 | void Dictionary<Derived, Shape, Key>::SetRequiresCopyOnCapacityChange() { |
| 18286 | DCHECK_EQ(0, DerivedHashTable::NumberOfElements()); |
| 18287 | DCHECK_EQ(0, DerivedHashTable::NumberOfDeletedElements()); |
| 18288 | // Make sure that HashTable::EnsureCapacity will create a copy. |
| 18289 | DerivedHashTable::SetNumberOfDeletedElements(DerivedHashTable::Capacity()); |
| 18290 | DCHECK(!DerivedHashTable::HasSufficientCapacity(1)); |
| 18291 | } |
| 18292 | |
| 18293 | |
| 18294 | template <typename Derived, typename Shape, typename Key> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18295 | Handle<Derived> Dictionary<Derived, Shape, Key>::EnsureCapacity( |
| 18296 | Handle<Derived> dictionary, int n, Key key) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18297 | // Check whether there are enough enumeration indices to add n elements. |
| 18298 | if (Shape::kIsEnumerable && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18299 | !PropertyDetails::IsValidIndex(dictionary->NextEnumerationIndex() + n)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18300 | // If not, we generate new indices for the properties. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18301 | GenerateNewEnumerationIndices(dictionary); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18302 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18303 | return DerivedHashTable::EnsureCapacity(dictionary, n, key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18304 | } |
| 18305 | |
| 18306 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18307 | template <typename Derived, typename Shape, typename Key> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18308 | Handle<Object> Dictionary<Derived, Shape, Key>::DeleteProperty( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18309 | Handle<Derived> dictionary, int entry) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18310 | Factory* factory = dictionary->GetIsolate()->factory(); |
| 18311 | PropertyDetails details = dictionary->DetailsAt(entry); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18312 | if (!details.IsConfigurable()) return factory->false_value(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18313 | |
| 18314 | dictionary->SetEntry( |
| 18315 | entry, factory->the_hole_value(), factory->the_hole_value()); |
| 18316 | dictionary->ElementRemoved(); |
| 18317 | return factory->true_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 | template<typename Derived, typename Shape, typename Key> |
| 18322 | Handle<Derived> Dictionary<Derived, Shape, Key>::AtPut( |
| 18323 | Handle<Derived> dictionary, Key key, Handle<Object> value) { |
| 18324 | int entry = dictionary->FindEntry(key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18325 | |
| 18326 | // If the entry is present set the value; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18327 | if (entry != Dictionary::kNotFound) { |
| 18328 | dictionary->ValueAtPut(entry, *value); |
| 18329 | return dictionary; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18330 | } |
| 18331 | |
| 18332 | // Check whether the dictionary should be extended. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18333 | dictionary = EnsureCapacity(dictionary, 1, key); |
| 18334 | #ifdef DEBUG |
| 18335 | USE(Shape::AsHandle(dictionary->GetIsolate(), key)); |
| 18336 | #endif |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18337 | PropertyDetails details = PropertyDetails::Empty(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18338 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18339 | AddEntry(dictionary, key, value, details, dictionary->Hash(key)); |
| 18340 | return dictionary; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18341 | } |
| 18342 | |
| 18343 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18344 | template<typename Derived, typename Shape, typename Key> |
| 18345 | Handle<Derived> Dictionary<Derived, Shape, Key>::Add( |
| 18346 | Handle<Derived> dictionary, |
| 18347 | Key key, |
| 18348 | Handle<Object> value, |
| 18349 | PropertyDetails details) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18350 | // Valdate key is absent. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18351 | SLOW_DCHECK((dictionary->FindEntry(key) == Dictionary::kNotFound)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18352 | // Check whether the dictionary should be extended. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18353 | dictionary = EnsureCapacity(dictionary, 1, key); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 18354 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18355 | AddEntry(dictionary, key, value, details, dictionary->Hash(key)); |
| 18356 | return dictionary; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18357 | } |
| 18358 | |
| 18359 | |
| 18360 | // Add a key, value pair to the dictionary. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18361 | template<typename Derived, typename Shape, typename Key> |
| 18362 | void Dictionary<Derived, Shape, Key>::AddEntry( |
| 18363 | Handle<Derived> dictionary, |
| 18364 | Key key, |
| 18365 | Handle<Object> value, |
| 18366 | PropertyDetails details, |
| 18367 | uint32_t hash) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18368 | // Compute the key object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18369 | Handle<Object> k = Shape::AsHandle(dictionary->GetIsolate(), key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18370 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18371 | uint32_t entry = dictionary->FindInsertionEntry(hash); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18372 | // Insert element at empty or deleted entry |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18373 | if (details.dictionary_index() == 0 && Shape::kIsEnumerable) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18374 | // Assign an enumeration index to the property and update |
| 18375 | // SetNextEnumerationIndex. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18376 | int index = dictionary->NextEnumerationIndex(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18377 | details = details.set_index(index); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18378 | dictionary->SetNextEnumerationIndex(index + 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18379 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18380 | dictionary->SetEntry(entry, k, value, details); |
| 18381 | DCHECK((dictionary->KeyAt(entry)->IsNumber() || |
| 18382 | dictionary->KeyAt(entry)->IsName())); |
| 18383 | dictionary->ElementAdded(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18384 | } |
| 18385 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 18386 | bool SeededNumberDictionary::HasComplexElements() { |
| 18387 | if (!requires_slow_elements()) return false; |
| 18388 | int capacity = this->Capacity(); |
| 18389 | for (int i = 0; i < capacity; i++) { |
| 18390 | Object* k = this->KeyAt(i); |
| 18391 | if (this->IsKey(k)) { |
| 18392 | DCHECK(!IsDeleted(i)); |
| 18393 | PropertyDetails details = this->DetailsAt(i); |
| 18394 | if (details.type() == ACCESSOR_CONSTANT) return true; |
| 18395 | PropertyAttributes attr = details.attributes(); |
| 18396 | if (attr & ALL_ATTRIBUTES_MASK) return true; |
| 18397 | } |
| 18398 | } |
| 18399 | return false; |
| 18400 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18401 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18402 | void SeededNumberDictionary::UpdateMaxNumberKey(uint32_t key, |
| 18403 | bool used_as_prototype) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18404 | DisallowHeapAllocation no_allocation; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18405 | // If the dictionary requires slow elements an element has already |
| 18406 | // been added at a high index. |
| 18407 | if (requires_slow_elements()) return; |
| 18408 | // Check if this index is high enough that we should require slow |
| 18409 | // elements. |
| 18410 | if (key > kRequiresSlowElementsLimit) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18411 | if (used_as_prototype) { |
| 18412 | // TODO(verwaest): Remove this hack. |
| 18413 | TypeFeedbackVector::ClearAllKeyedStoreICs(GetIsolate()); |
| 18414 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18415 | set_requires_slow_elements(); |
| 18416 | return; |
| 18417 | } |
| 18418 | // Update max key value. |
| 18419 | Object* max_index_object = get(kMaxNumberKeyIndex); |
| 18420 | if (!max_index_object->IsSmi() || max_number_key() < key) { |
| 18421 | FixedArray::set(kMaxNumberKeyIndex, |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 18422 | Smi::FromInt(key << kRequiresSlowElementsTagSize)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18423 | } |
| 18424 | } |
| 18425 | |
| 18426 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18427 | Handle<SeededNumberDictionary> SeededNumberDictionary::AddNumberEntry( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18428 | Handle<SeededNumberDictionary> dictionary, uint32_t key, |
| 18429 | Handle<Object> value, PropertyDetails details, bool used_as_prototype) { |
| 18430 | dictionary->UpdateMaxNumberKey(key, used_as_prototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18431 | SLOW_DCHECK(dictionary->FindEntry(key) == kNotFound); |
| 18432 | return Add(dictionary, key, value, details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18433 | } |
| 18434 | |
| 18435 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18436 | Handle<UnseededNumberDictionary> UnseededNumberDictionary::AddNumberEntry( |
| 18437 | Handle<UnseededNumberDictionary> dictionary, |
| 18438 | uint32_t key, |
| 18439 | Handle<Object> value) { |
| 18440 | SLOW_DCHECK(dictionary->FindEntry(key) == kNotFound); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18441 | return Add(dictionary, key, value, PropertyDetails::Empty()); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 18442 | } |
| 18443 | |
| 18444 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18445 | Handle<SeededNumberDictionary> SeededNumberDictionary::AtNumberPut( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18446 | Handle<SeededNumberDictionary> dictionary, uint32_t key, |
| 18447 | Handle<Object> value, bool used_as_prototype) { |
| 18448 | dictionary->UpdateMaxNumberKey(key, used_as_prototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18449 | return AtPut(dictionary, key, value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18450 | } |
| 18451 | |
| 18452 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18453 | Handle<UnseededNumberDictionary> UnseededNumberDictionary::AtNumberPut( |
| 18454 | Handle<UnseededNumberDictionary> dictionary, |
| 18455 | uint32_t key, |
| 18456 | Handle<Object> value) { |
| 18457 | return AtPut(dictionary, key, value); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 18458 | } |
| 18459 | |
| 18460 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18461 | Handle<SeededNumberDictionary> SeededNumberDictionary::Set( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18462 | Handle<SeededNumberDictionary> dictionary, uint32_t key, |
| 18463 | Handle<Object> value, PropertyDetails details, bool used_as_prototype) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18464 | int entry = dictionary->FindEntry(key); |
| 18465 | if (entry == kNotFound) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18466 | return AddNumberEntry(dictionary, key, value, details, used_as_prototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18467 | } |
| 18468 | // Preserve enumeration index. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18469 | details = details.set_index(dictionary->DetailsAt(entry).dictionary_index()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18470 | Handle<Object> object_key = |
| 18471 | SeededNumberDictionaryShape::AsHandle(dictionary->GetIsolate(), key); |
| 18472 | dictionary->SetEntry(entry, object_key, value, details); |
| 18473 | return dictionary; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18474 | } |
| 18475 | |
| 18476 | |
| 18477 | Handle<UnseededNumberDictionary> UnseededNumberDictionary::Set( |
| 18478 | Handle<UnseededNumberDictionary> dictionary, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18479 | uint32_t key, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18480 | Handle<Object> value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18481 | int entry = dictionary->FindEntry(key); |
| 18482 | if (entry == kNotFound) return AddNumberEntry(dictionary, key, value); |
| 18483 | Handle<Object> object_key = |
| 18484 | UnseededNumberDictionaryShape::AsHandle(dictionary->GetIsolate(), key); |
| 18485 | dictionary->SetEntry(entry, object_key, value); |
| 18486 | return dictionary; |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 18487 | } |
| 18488 | |
| 18489 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18490 | template <typename Derived, typename Shape, typename Key> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18491 | int Dictionary<Derived, Shape, Key>::NumberOfElementsFilterAttributes( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18492 | PropertyFilter filter) { |
| 18493 | int capacity = this->Capacity(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18494 | int result = 0; |
| 18495 | for (int i = 0; i < capacity; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18496 | Object* k = this->KeyAt(i); |
| 18497 | if (this->IsKey(k) && !k->FilterKey(filter)) { |
| 18498 | if (this->IsDeleted(i)) continue; |
| 18499 | PropertyDetails details = this->DetailsAt(i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18500 | PropertyAttributes attr = details.attributes(); |
| 18501 | if ((attr & filter) == 0) result++; |
| 18502 | } |
| 18503 | } |
| 18504 | return result; |
| 18505 | } |
| 18506 | |
| 18507 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18508 | template <typename Dictionary> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18509 | struct EnumIndexComparator { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18510 | explicit EnumIndexComparator(Dictionary* dict) : dict(dict) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18511 | bool operator() (Smi* a, Smi* b) { |
| 18512 | PropertyDetails da(dict->DetailsAt(a->value())); |
| 18513 | PropertyDetails db(dict->DetailsAt(b->value())); |
| 18514 | return da.dictionary_index() < db.dictionary_index(); |
| 18515 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18516 | Dictionary* dict; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18517 | }; |
| 18518 | |
| 18519 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18520 | template <typename Derived, typename Shape, typename Key> |
| 18521 | void Dictionary<Derived, Shape, Key>::CopyEnumKeysTo(FixedArray* storage) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18522 | int length = storage->length(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18523 | int capacity = this->Capacity(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18524 | int properties = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18525 | for (int i = 0; i < capacity; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18526 | Object* k = this->KeyAt(i); |
| 18527 | if (this->IsKey(k) && !k->IsSymbol()) { |
| 18528 | PropertyDetails details = this->DetailsAt(i); |
| 18529 | if (details.IsDontEnum() || this->IsDeleted(i)) continue; |
| 18530 | storage->set(properties, Smi::FromInt(i)); |
| 18531 | properties++; |
| 18532 | if (properties == length) break; |
| 18533 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18534 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18535 | CHECK_EQ(length, properties); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18536 | EnumIndexComparator<Derived> cmp(static_cast<Derived*>(this)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18537 | Smi** start = reinterpret_cast<Smi**>(storage->GetFirstElementAddress()); |
| 18538 | std::sort(start, start + length, cmp); |
| 18539 | for (int i = 0; i < length; i++) { |
| 18540 | int index = Smi::cast(storage->get(i))->value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18541 | storage->set(i, this->KeyAt(index)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18542 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18543 | } |
| 18544 | |
| 18545 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18546 | template <typename Derived, typename Shape, typename Key> |
| 18547 | int Dictionary<Derived, Shape, Key>::CopyKeysTo( |
| 18548 | FixedArray* storage, int index, PropertyFilter filter, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18549 | typename Dictionary<Derived, Shape, Key>::SortMode sort_mode) { |
| 18550 | DCHECK(storage->length() >= NumberOfElementsFilterAttributes(filter)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18551 | int start_index = index; |
| 18552 | int capacity = this->Capacity(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18553 | for (int i = 0; i < capacity; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18554 | Object* k = this->KeyAt(i); |
| 18555 | if (!this->IsKey(k) || k->FilterKey(filter)) continue; |
| 18556 | if (this->IsDeleted(i)) continue; |
| 18557 | PropertyDetails details = this->DetailsAt(i); |
| 18558 | PropertyAttributes attr = details.attributes(); |
| 18559 | if ((attr & filter) != 0) continue; |
| 18560 | storage->set(index++, k); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18561 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18562 | if (sort_mode == Dictionary::SORTED) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 18563 | storage->SortPairs(storage, index); |
| 18564 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18565 | DCHECK(storage->length() >= index); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18566 | return index - start_index; |
| 18567 | } |
| 18568 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18569 | template <typename Derived, typename Shape, typename Key> |
| 18570 | void Dictionary<Derived, Shape, Key>::CollectKeysTo( |
| 18571 | Handle<Dictionary<Derived, Shape, Key> > dictionary, KeyAccumulator* keys, |
| 18572 | PropertyFilter filter) { |
| 18573 | int capacity = dictionary->Capacity(); |
| 18574 | Handle<FixedArray> array = |
| 18575 | keys->isolate()->factory()->NewFixedArray(dictionary->NumberOfElements()); |
| 18576 | int array_size = 0; |
| 18577 | |
| 18578 | { |
| 18579 | DisallowHeapAllocation no_gc; |
| 18580 | Dictionary<Derived, Shape, Key>* raw_dict = *dictionary; |
| 18581 | for (int i = 0; i < capacity; i++) { |
| 18582 | Object* k = raw_dict->KeyAt(i); |
| 18583 | if (!raw_dict->IsKey(k) || k->FilterKey(filter)) continue; |
| 18584 | if (raw_dict->IsDeleted(i)) continue; |
| 18585 | PropertyDetails details = raw_dict->DetailsAt(i); |
| 18586 | if ((details.attributes() & filter) != 0) continue; |
| 18587 | if (filter & ONLY_ALL_CAN_READ) { |
| 18588 | if (details.kind() != kAccessor) continue; |
| 18589 | Object* accessors = raw_dict->ValueAt(i); |
| 18590 | if (accessors->IsPropertyCell()) { |
| 18591 | accessors = PropertyCell::cast(accessors)->value(); |
| 18592 | } |
| 18593 | if (!accessors->IsAccessorInfo()) continue; |
| 18594 | if (!AccessorInfo::cast(accessors)->all_can_read()) continue; |
| 18595 | } |
| 18596 | array->set(array_size++, Smi::FromInt(i)); |
| 18597 | } |
| 18598 | |
| 18599 | EnumIndexComparator<Derived> cmp(static_cast<Derived*>(raw_dict)); |
| 18600 | Smi** start = reinterpret_cast<Smi**>(array->GetFirstElementAddress()); |
| 18601 | std::sort(start, start + array_size, cmp); |
| 18602 | } |
| 18603 | |
| 18604 | for (int i = 0; i < array_size; i++) { |
| 18605 | int index = Smi::cast(array->get(i))->value(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 18606 | keys->AddKey(dictionary->KeyAt(index), DO_NOT_CONVERT); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18607 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18608 | } |
| 18609 | |
| 18610 | |
| 18611 | // Backwards lookup (slow). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18612 | template<typename Derived, typename Shape, typename Key> |
| 18613 | Object* Dictionary<Derived, Shape, Key>::SlowReverseLookup(Object* value) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18614 | int capacity = this->Capacity(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18615 | for (int i = 0; i < capacity; i++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18616 | Object* k = this->KeyAt(i); |
| 18617 | if (this->IsKey(k)) { |
| 18618 | Object* e = this->ValueAt(i); |
| 18619 | // TODO(dcarney): this should be templatized. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18620 | if (e->IsPropertyCell()) { |
| 18621 | e = PropertyCell::cast(e)->value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18622 | } |
| 18623 | if (e == value) return k; |
| 18624 | } |
| 18625 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18626 | Heap* heap = Dictionary::GetHeap(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 18627 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18628 | } |
| 18629 | |
| 18630 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18631 | Object* ObjectHashTable::Lookup(Isolate* isolate, Handle<Object> key, |
| 18632 | int32_t hash) { |
| 18633 | DisallowHeapAllocation no_gc; |
| 18634 | DCHECK(IsKey(*key)); |
| 18635 | |
| 18636 | int entry = FindEntry(isolate, key, hash); |
| 18637 | if (entry == kNotFound) return isolate->heap()->the_hole_value(); |
| 18638 | return get(EntryToIndex(entry) + 1); |
| 18639 | } |
| 18640 | |
| 18641 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18642 | Object* ObjectHashTable::Lookup(Handle<Object> key) { |
| 18643 | DisallowHeapAllocation no_gc; |
| 18644 | DCHECK(IsKey(*key)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18645 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18646 | Isolate* isolate = GetIsolate(); |
| 18647 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18648 | // 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] | 18649 | Object* hash = key->GetHash(); |
| 18650 | if (hash->IsUndefined()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18651 | return isolate->heap()->the_hole_value(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18652 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18653 | return Lookup(isolate, key, Smi::cast(hash)->value()); |
| 18654 | } |
| 18655 | |
| 18656 | |
| 18657 | Object* ObjectHashTable::Lookup(Handle<Object> key, int32_t hash) { |
| 18658 | return Lookup(GetIsolate(), key, hash); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18659 | } |
| 18660 | |
| 18661 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18662 | Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table, |
| 18663 | Handle<Object> key, |
| 18664 | Handle<Object> value) { |
| 18665 | DCHECK(table->IsKey(*key)); |
| 18666 | DCHECK(!value->IsTheHole()); |
| 18667 | |
| 18668 | Isolate* isolate = table->GetIsolate(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18669 | // Make sure the key object has an identity hash code. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18670 | int32_t hash = Object::GetOrCreateHash(isolate, key)->value(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18671 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18672 | return Put(table, key, value, hash); |
| 18673 | } |
| 18674 | |
| 18675 | |
| 18676 | Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table, |
| 18677 | Handle<Object> key, |
| 18678 | Handle<Object> value, |
| 18679 | int32_t hash) { |
| 18680 | DCHECK(table->IsKey(*key)); |
| 18681 | DCHECK(!value->IsTheHole()); |
| 18682 | |
| 18683 | Isolate* isolate = table->GetIsolate(); |
| 18684 | |
| 18685 | int entry = table->FindEntry(isolate, key, hash); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18686 | |
| 18687 | // Key is already in table, just overwrite value. |
| 18688 | if (entry != kNotFound) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18689 | table->set(EntryToIndex(entry) + 1, *value); |
| 18690 | return table; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18691 | } |
| 18692 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 18693 | // Rehash if more than 25% of the entries are deleted entries. |
| 18694 | // TODO(jochen): Consider to shrink the fixed array in place. |
| 18695 | if ((table->NumberOfDeletedElements() << 1) > table->NumberOfElements()) { |
| 18696 | table->Rehash(isolate->factory()->undefined_value()); |
| 18697 | } |
| 18698 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18699 | // Check whether the hash table should be extended. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18700 | table = EnsureCapacity(table, 1, key); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18701 | table->AddEntry(table->FindInsertionEntry(hash), *key, *value); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18702 | return table; |
| 18703 | } |
| 18704 | |
| 18705 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18706 | Handle<ObjectHashTable> ObjectHashTable::Remove(Handle<ObjectHashTable> table, |
| 18707 | Handle<Object> key, |
| 18708 | bool* was_present) { |
| 18709 | DCHECK(table->IsKey(*key)); |
| 18710 | |
| 18711 | Object* hash = key->GetHash(); |
| 18712 | if (hash->IsUndefined()) { |
| 18713 | *was_present = false; |
| 18714 | return table; |
| 18715 | } |
| 18716 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18717 | return Remove(table, key, was_present, Smi::cast(hash)->value()); |
| 18718 | } |
| 18719 | |
| 18720 | |
| 18721 | Handle<ObjectHashTable> ObjectHashTable::Remove(Handle<ObjectHashTable> table, |
| 18722 | Handle<Object> key, |
| 18723 | bool* was_present, |
| 18724 | int32_t hash) { |
| 18725 | DCHECK(table->IsKey(*key)); |
| 18726 | |
| 18727 | int entry = table->FindEntry(table->GetIsolate(), key, hash); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18728 | if (entry == kNotFound) { |
| 18729 | *was_present = false; |
| 18730 | return table; |
| 18731 | } |
| 18732 | |
| 18733 | *was_present = true; |
| 18734 | table->RemoveEntry(entry); |
| 18735 | return Shrink(table, key); |
| 18736 | } |
| 18737 | |
| 18738 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18739 | void ObjectHashTable::AddEntry(int entry, Object* key, Object* value) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18740 | set(EntryToIndex(entry), key); |
| 18741 | set(EntryToIndex(entry) + 1, value); |
| 18742 | ElementAdded(); |
| 18743 | } |
| 18744 | |
| 18745 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18746 | void ObjectHashTable::RemoveEntry(int entry) { |
| 18747 | set_the_hole(EntryToIndex(entry)); |
| 18748 | set_the_hole(EntryToIndex(entry) + 1); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 18749 | ElementRemoved(); |
| 18750 | } |
| 18751 | |
| 18752 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18753 | Object* WeakHashTable::Lookup(Handle<HeapObject> key) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18754 | DisallowHeapAllocation no_gc; |
| 18755 | DCHECK(IsKey(*key)); |
| 18756 | int entry = FindEntry(key); |
| 18757 | if (entry == kNotFound) return GetHeap()->the_hole_value(); |
| 18758 | return get(EntryToValueIndex(entry)); |
| 18759 | } |
| 18760 | |
| 18761 | |
| 18762 | Handle<WeakHashTable> WeakHashTable::Put(Handle<WeakHashTable> table, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18763 | Handle<HeapObject> key, |
| 18764 | Handle<HeapObject> value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18765 | DCHECK(table->IsKey(*key)); |
| 18766 | int entry = table->FindEntry(key); |
| 18767 | // Key is already in table, just overwrite value. |
| 18768 | if (entry != kNotFound) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18769 | table->set(EntryToValueIndex(entry), *value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18770 | return table; |
| 18771 | } |
| 18772 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18773 | Handle<WeakCell> key_cell = key->GetIsolate()->factory()->NewWeakCell(key); |
| 18774 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18775 | // Check whether the hash table should be extended. |
| 18776 | table = EnsureCapacity(table, 1, key, TENURED); |
| 18777 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18778 | table->AddEntry(table->FindInsertionEntry(table->Hash(key)), key_cell, value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18779 | return table; |
| 18780 | } |
| 18781 | |
| 18782 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18783 | void WeakHashTable::AddEntry(int entry, Handle<WeakCell> key_cell, |
| 18784 | Handle<HeapObject> value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18785 | DisallowHeapAllocation no_allocation; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18786 | set(EntryToIndex(entry), *key_cell); |
| 18787 | set(EntryToValueIndex(entry), *value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18788 | ElementAdded(); |
| 18789 | } |
| 18790 | |
| 18791 | |
| 18792 | template<class Derived, class Iterator, int entrysize> |
| 18793 | Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Allocate( |
| 18794 | Isolate* isolate, int capacity, PretenureFlag pretenure) { |
| 18795 | // Capacity must be a power of two, since we depend on being able |
| 18796 | // to divide and multiple by 2 (kLoadFactor) to derive capacity |
| 18797 | // from number of buckets. If we decide to change kLoadFactor |
| 18798 | // to something other than 2, capacity should be stored as another |
| 18799 | // field of this object. |
| 18800 | capacity = base::bits::RoundUpToPowerOfTwo32(Max(kMinCapacity, capacity)); |
| 18801 | if (capacity > kMaxCapacity) { |
| 18802 | v8::internal::Heap::FatalProcessOutOfMemory("invalid table size", true); |
| 18803 | } |
| 18804 | int num_buckets = capacity / kLoadFactor; |
| 18805 | Handle<FixedArray> backing_store = isolate->factory()->NewFixedArray( |
| 18806 | kHashTableStartIndex + num_buckets + (capacity * kEntrySize), pretenure); |
| 18807 | backing_store->set_map_no_write_barrier( |
| 18808 | isolate->heap()->ordered_hash_table_map()); |
| 18809 | Handle<Derived> table = Handle<Derived>::cast(backing_store); |
| 18810 | for (int i = 0; i < num_buckets; ++i) { |
| 18811 | table->set(kHashTableStartIndex + i, Smi::FromInt(kNotFound)); |
| 18812 | } |
| 18813 | table->SetNumberOfBuckets(num_buckets); |
| 18814 | table->SetNumberOfElements(0); |
| 18815 | table->SetNumberOfDeletedElements(0); |
| 18816 | return table; |
| 18817 | } |
| 18818 | |
| 18819 | |
| 18820 | template<class Derived, class Iterator, int entrysize> |
| 18821 | Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::EnsureGrowable( |
| 18822 | Handle<Derived> table) { |
| 18823 | DCHECK(!table->IsObsolete()); |
| 18824 | |
| 18825 | int nof = table->NumberOfElements(); |
| 18826 | int nod = table->NumberOfDeletedElements(); |
| 18827 | int capacity = table->Capacity(); |
| 18828 | if ((nof + nod) < capacity) return table; |
| 18829 | // Don't need to grow if we can simply clear out deleted entries instead. |
| 18830 | // Note that we can't compact in place, though, so we always allocate |
| 18831 | // a new table. |
| 18832 | return Rehash(table, (nod < (capacity >> 1)) ? capacity << 1 : capacity); |
| 18833 | } |
| 18834 | |
| 18835 | |
| 18836 | template<class Derived, class Iterator, int entrysize> |
| 18837 | Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Shrink( |
| 18838 | Handle<Derived> table) { |
| 18839 | DCHECK(!table->IsObsolete()); |
| 18840 | |
| 18841 | int nof = table->NumberOfElements(); |
| 18842 | int capacity = table->Capacity(); |
| 18843 | if (nof >= (capacity >> 2)) return table; |
| 18844 | return Rehash(table, capacity / 2); |
| 18845 | } |
| 18846 | |
| 18847 | |
| 18848 | template<class Derived, class Iterator, int entrysize> |
| 18849 | Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Clear( |
| 18850 | Handle<Derived> table) { |
| 18851 | DCHECK(!table->IsObsolete()); |
| 18852 | |
| 18853 | Handle<Derived> new_table = |
| 18854 | Allocate(table->GetIsolate(), |
| 18855 | kMinCapacity, |
| 18856 | table->GetHeap()->InNewSpace(*table) ? NOT_TENURED : TENURED); |
| 18857 | |
| 18858 | table->SetNextTable(*new_table); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18859 | table->SetNumberOfDeletedElements(kClearedTableSentinel); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18860 | |
| 18861 | return new_table; |
| 18862 | } |
| 18863 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18864 | template <class Derived, class Iterator, int entrysize> |
| 18865 | bool OrderedHashTable<Derived, Iterator, entrysize>::HasKey( |
| 18866 | Handle<Derived> table, Handle<Object> key) { |
| 18867 | int entry = table->KeyToFirstEntry(*key); |
| 18868 | // Walk the chain in the bucket to find the key. |
| 18869 | while (entry != kNotFound) { |
| 18870 | Object* candidate_key = table->KeyAt(entry); |
| 18871 | if (candidate_key->SameValueZero(*key)) return true; |
| 18872 | entry = table->NextChainEntry(entry); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18873 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18874 | return false; |
| 18875 | } |
| 18876 | |
| 18877 | |
| 18878 | Handle<OrderedHashSet> OrderedHashSet::Add(Handle<OrderedHashSet> table, |
| 18879 | Handle<Object> key) { |
| 18880 | int hash = Object::GetOrCreateHash(table->GetIsolate(), key)->value(); |
| 18881 | int entry = table->HashToEntry(hash); |
| 18882 | // Walk the chain of the bucket and try finding the key. |
| 18883 | while (entry != kNotFound) { |
| 18884 | Object* candidate_key = table->KeyAt(entry); |
| 18885 | // Do not add if we have the key already |
| 18886 | if (candidate_key->SameValueZero(*key)) return table; |
| 18887 | entry = table->NextChainEntry(entry); |
| 18888 | } |
| 18889 | |
| 18890 | table = OrderedHashSet::EnsureGrowable(table); |
| 18891 | // Read the existing bucket values. |
| 18892 | int bucket = table->HashToBucket(hash); |
| 18893 | int previous_entry = table->HashToEntry(hash); |
| 18894 | int nof = table->NumberOfElements(); |
| 18895 | // Insert a new entry at the end, |
| 18896 | int new_entry = nof + table->NumberOfDeletedElements(); |
| 18897 | int new_index = table->EntryToIndex(new_entry); |
| 18898 | table->set(new_index, *key); |
| 18899 | table->set(new_index + kChainOffset, Smi::FromInt(previous_entry)); |
| 18900 | // and point the bucket to the new entry. |
| 18901 | table->set(kHashTableStartIndex + bucket, Smi::FromInt(new_entry)); |
| 18902 | table->SetNumberOfElements(nof + 1); |
| 18903 | return table; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18904 | } |
| 18905 | |
| 18906 | |
| 18907 | template<class Derived, class Iterator, int entrysize> |
| 18908 | Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Rehash( |
| 18909 | Handle<Derived> table, int new_capacity) { |
| 18910 | DCHECK(!table->IsObsolete()); |
| 18911 | |
| 18912 | Handle<Derived> new_table = |
| 18913 | Allocate(table->GetIsolate(), |
| 18914 | new_capacity, |
| 18915 | table->GetHeap()->InNewSpace(*table) ? NOT_TENURED : TENURED); |
| 18916 | int nof = table->NumberOfElements(); |
| 18917 | int nod = table->NumberOfDeletedElements(); |
| 18918 | int new_buckets = new_table->NumberOfBuckets(); |
| 18919 | int new_entry = 0; |
| 18920 | int removed_holes_index = 0; |
| 18921 | |
| 18922 | for (int old_entry = 0; old_entry < (nof + nod); ++old_entry) { |
| 18923 | Object* key = table->KeyAt(old_entry); |
| 18924 | if (key->IsTheHole()) { |
| 18925 | table->SetRemovedIndexAt(removed_holes_index++, old_entry); |
| 18926 | continue; |
| 18927 | } |
| 18928 | |
| 18929 | Object* hash = key->GetHash(); |
| 18930 | int bucket = Smi::cast(hash)->value() & (new_buckets - 1); |
| 18931 | Object* chain_entry = new_table->get(kHashTableStartIndex + bucket); |
| 18932 | new_table->set(kHashTableStartIndex + bucket, Smi::FromInt(new_entry)); |
| 18933 | int new_index = new_table->EntryToIndex(new_entry); |
| 18934 | int old_index = table->EntryToIndex(old_entry); |
| 18935 | for (int i = 0; i < entrysize; ++i) { |
| 18936 | Object* value = table->get(old_index + i); |
| 18937 | new_table->set(new_index + i, value); |
| 18938 | } |
| 18939 | new_table->set(new_index + kChainOffset, chain_entry); |
| 18940 | ++new_entry; |
| 18941 | } |
| 18942 | |
| 18943 | DCHECK_EQ(nod, removed_holes_index); |
| 18944 | |
| 18945 | new_table->SetNumberOfElements(nof); |
| 18946 | table->SetNextTable(*new_table); |
| 18947 | |
| 18948 | return new_table; |
| 18949 | } |
| 18950 | |
| 18951 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18952 | template Handle<OrderedHashSet> |
| 18953 | OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Allocate( |
| 18954 | Isolate* isolate, int capacity, PretenureFlag pretenure); |
| 18955 | |
| 18956 | template Handle<OrderedHashSet> |
| 18957 | OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::EnsureGrowable( |
| 18958 | Handle<OrderedHashSet> table); |
| 18959 | |
| 18960 | template Handle<OrderedHashSet> |
| 18961 | OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Shrink( |
| 18962 | Handle<OrderedHashSet> table); |
| 18963 | |
| 18964 | template Handle<OrderedHashSet> |
| 18965 | OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Clear( |
| 18966 | Handle<OrderedHashSet> table); |
| 18967 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18968 | template bool OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::HasKey( |
| 18969 | Handle<OrderedHashSet> table, Handle<Object> key); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18970 | |
| 18971 | |
| 18972 | template Handle<OrderedHashMap> |
| 18973 | OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Allocate( |
| 18974 | Isolate* isolate, int capacity, PretenureFlag pretenure); |
| 18975 | |
| 18976 | template Handle<OrderedHashMap> |
| 18977 | OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::EnsureGrowable( |
| 18978 | Handle<OrderedHashMap> table); |
| 18979 | |
| 18980 | template Handle<OrderedHashMap> |
| 18981 | OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Shrink( |
| 18982 | Handle<OrderedHashMap> table); |
| 18983 | |
| 18984 | template Handle<OrderedHashMap> |
| 18985 | OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Clear( |
| 18986 | Handle<OrderedHashMap> table); |
| 18987 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18988 | template bool OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::HasKey( |
| 18989 | Handle<OrderedHashMap> table, Handle<Object> key); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18990 | |
| 18991 | |
| 18992 | template<class Derived, class TableType> |
| 18993 | void OrderedHashTableIterator<Derived, TableType>::Transition() { |
| 18994 | DisallowHeapAllocation no_allocation; |
| 18995 | TableType* table = TableType::cast(this->table()); |
| 18996 | if (!table->IsObsolete()) return; |
| 18997 | |
| 18998 | int index = Smi::cast(this->index())->value(); |
| 18999 | while (table->IsObsolete()) { |
| 19000 | TableType* next_table = table->NextTable(); |
| 19001 | |
| 19002 | if (index > 0) { |
| 19003 | int nod = table->NumberOfDeletedElements(); |
| 19004 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 19005 | if (nod == TableType::kClearedTableSentinel) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19006 | index = 0; |
| 19007 | } else { |
| 19008 | int old_index = index; |
| 19009 | for (int i = 0; i < nod; ++i) { |
| 19010 | int removed_index = table->RemovedIndexAt(i); |
| 19011 | if (removed_index >= old_index) break; |
| 19012 | --index; |
| 19013 | } |
| 19014 | } |
| 19015 | } |
| 19016 | |
| 19017 | table = next_table; |
| 19018 | } |
| 19019 | |
| 19020 | set_table(table); |
| 19021 | set_index(Smi::FromInt(index)); |
| 19022 | } |
| 19023 | |
| 19024 | |
| 19025 | template<class Derived, class TableType> |
| 19026 | bool OrderedHashTableIterator<Derived, TableType>::HasMore() { |
| 19027 | DisallowHeapAllocation no_allocation; |
| 19028 | if (this->table()->IsUndefined()) return false; |
| 19029 | |
| 19030 | Transition(); |
| 19031 | |
| 19032 | TableType* table = TableType::cast(this->table()); |
| 19033 | int index = Smi::cast(this->index())->value(); |
| 19034 | int used_capacity = table->UsedCapacity(); |
| 19035 | |
| 19036 | while (index < used_capacity && table->KeyAt(index)->IsTheHole()) { |
| 19037 | index++; |
| 19038 | } |
| 19039 | |
| 19040 | set_index(Smi::FromInt(index)); |
| 19041 | |
| 19042 | if (index < used_capacity) return true; |
| 19043 | |
| 19044 | set_table(GetHeap()->undefined_value()); |
| 19045 | return false; |
| 19046 | } |
| 19047 | |
| 19048 | |
| 19049 | template<class Derived, class TableType> |
| 19050 | Smi* OrderedHashTableIterator<Derived, TableType>::Next(JSArray* value_array) { |
| 19051 | DisallowHeapAllocation no_allocation; |
| 19052 | if (HasMore()) { |
| 19053 | FixedArray* array = FixedArray::cast(value_array->elements()); |
| 19054 | static_cast<Derived*>(this)->PopulateValueArray(array); |
| 19055 | MoveNext(); |
| 19056 | return Smi::cast(kind()); |
| 19057 | } |
| 19058 | return Smi::FromInt(0); |
| 19059 | } |
| 19060 | |
| 19061 | |
| 19062 | template Smi* |
| 19063 | OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::Next( |
| 19064 | JSArray* value_array); |
| 19065 | |
| 19066 | template bool |
| 19067 | OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::HasMore(); |
| 19068 | |
| 19069 | template void |
| 19070 | OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::MoveNext(); |
| 19071 | |
| 19072 | template Object* |
| 19073 | OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::CurrentKey(); |
| 19074 | |
| 19075 | template void |
| 19076 | OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::Transition(); |
| 19077 | |
| 19078 | |
| 19079 | template Smi* |
| 19080 | OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::Next( |
| 19081 | JSArray* value_array); |
| 19082 | |
| 19083 | template bool |
| 19084 | OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::HasMore(); |
| 19085 | |
| 19086 | template void |
| 19087 | OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::MoveNext(); |
| 19088 | |
| 19089 | template Object* |
| 19090 | OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::CurrentKey(); |
| 19091 | |
| 19092 | template void |
| 19093 | OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::Transition(); |
| 19094 | |
| 19095 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19096 | void JSSet::Initialize(Handle<JSSet> set, Isolate* isolate) { |
| 19097 | Handle<OrderedHashSet> table = isolate->factory()->NewOrderedHashSet(); |
| 19098 | set->set_table(*table); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19099 | } |
| 19100 | |
| 19101 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19102 | void JSSet::Clear(Handle<JSSet> set) { |
| 19103 | Handle<OrderedHashSet> table(OrderedHashSet::cast(set->table())); |
| 19104 | table = OrderedHashSet::Clear(table); |
| 19105 | set->set_table(*table); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19106 | } |
| 19107 | |
| 19108 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19109 | void JSMap::Initialize(Handle<JSMap> map, Isolate* isolate) { |
| 19110 | Handle<OrderedHashMap> table = isolate->factory()->NewOrderedHashMap(); |
| 19111 | map->set_table(*table); |
| 19112 | } |
| 19113 | |
| 19114 | |
| 19115 | void JSMap::Clear(Handle<JSMap> map) { |
| 19116 | Handle<OrderedHashMap> table(OrderedHashMap::cast(map->table())); |
| 19117 | table = OrderedHashMap::Clear(table); |
| 19118 | map->set_table(*table); |
| 19119 | } |
| 19120 | |
| 19121 | |
| 19122 | void JSWeakCollection::Initialize(Handle<JSWeakCollection> weak_collection, |
| 19123 | Isolate* isolate) { |
| 19124 | Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 0); |
| 19125 | weak_collection->set_table(*table); |
| 19126 | } |
| 19127 | |
| 19128 | |
| 19129 | void JSWeakCollection::Set(Handle<JSWeakCollection> weak_collection, |
| 19130 | Handle<Object> key, Handle<Object> value, |
| 19131 | int32_t hash) { |
| 19132 | DCHECK(key->IsJSReceiver() || key->IsSymbol()); |
| 19133 | Handle<ObjectHashTable> table( |
| 19134 | ObjectHashTable::cast(weak_collection->table())); |
| 19135 | DCHECK(table->IsKey(*key)); |
| 19136 | Handle<ObjectHashTable> new_table = |
| 19137 | ObjectHashTable::Put(table, key, value, hash); |
| 19138 | weak_collection->set_table(*new_table); |
| 19139 | if (*table != *new_table) { |
| 19140 | // Zap the old table since we didn't record slots for its elements. |
| 19141 | table->FillWithHoles(0, table->length()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19142 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19143 | } |
| 19144 | |
| 19145 | |
| 19146 | bool JSWeakCollection::Delete(Handle<JSWeakCollection> weak_collection, |
| 19147 | Handle<Object> key, int32_t hash) { |
| 19148 | DCHECK(key->IsJSReceiver() || key->IsSymbol()); |
| 19149 | Handle<ObjectHashTable> table( |
| 19150 | ObjectHashTable::cast(weak_collection->table())); |
| 19151 | DCHECK(table->IsKey(*key)); |
| 19152 | bool was_present = false; |
| 19153 | Handle<ObjectHashTable> new_table = |
| 19154 | ObjectHashTable::Remove(table, key, &was_present, hash); |
| 19155 | weak_collection->set_table(*new_table); |
| 19156 | if (*table != *new_table) { |
| 19157 | // Zap the old table since we didn't record slots for its elements. |
| 19158 | table->FillWithHoles(0, table->length()); |
| 19159 | } |
| 19160 | return was_present; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19161 | } |
| 19162 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 19163 | // Check if there is a break point at this code offset. |
| 19164 | bool DebugInfo::HasBreakPoint(int code_offset) { |
| 19165 | // Get the break point info object for this code offset. |
| 19166 | Object* break_point_info = GetBreakPointInfo(code_offset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19167 | |
| 19168 | // If there is no break point info object or no break points in the break |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 19169 | // point info object there is no break point at this code offset. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19170 | if (break_point_info->IsUndefined()) return false; |
| 19171 | return BreakPointInfo::cast(break_point_info)->GetBreakPointCount() > 0; |
| 19172 | } |
| 19173 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 19174 | // Get the break point info object for this code offset. |
| 19175 | Object* DebugInfo::GetBreakPointInfo(int code_offset) { |
| 19176 | // Find the index of the break point info object for this code offset. |
| 19177 | int index = GetBreakPointInfoIndex(code_offset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19178 | |
| 19179 | // Return the break point info object if any. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 19180 | if (index == kNoBreakPointInfo) return GetHeap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19181 | return BreakPointInfo::cast(break_points()->get(index)); |
| 19182 | } |
| 19183 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 19184 | // Clear a break point at the specified code offset. |
| 19185 | void DebugInfo::ClearBreakPoint(Handle<DebugInfo> debug_info, int code_offset, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19186 | Handle<Object> break_point_object) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 19187 | Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_offset), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19188 | debug_info->GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19189 | if (break_point_info->IsUndefined()) return; |
| 19190 | BreakPointInfo::ClearBreakPoint( |
| 19191 | Handle<BreakPointInfo>::cast(break_point_info), |
| 19192 | break_point_object); |
| 19193 | } |
| 19194 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 19195 | void DebugInfo::SetBreakPoint(Handle<DebugInfo> debug_info, int code_offset, |
| 19196 | int source_position, int statement_position, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19197 | Handle<Object> break_point_object) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19198 | Isolate* isolate = debug_info->GetIsolate(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 19199 | Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_offset), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19200 | isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19201 | if (!break_point_info->IsUndefined()) { |
| 19202 | BreakPointInfo::SetBreakPoint( |
| 19203 | Handle<BreakPointInfo>::cast(break_point_info), |
| 19204 | break_point_object); |
| 19205 | return; |
| 19206 | } |
| 19207 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 19208 | // Adding a new break point for a code offset which did not have any |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19209 | // break points before. Try to find a free slot. |
| 19210 | int index = kNoBreakPointInfo; |
| 19211 | for (int i = 0; i < debug_info->break_points()->length(); i++) { |
| 19212 | if (debug_info->break_points()->get(i)->IsUndefined()) { |
| 19213 | index = i; |
| 19214 | break; |
| 19215 | } |
| 19216 | } |
| 19217 | if (index == kNoBreakPointInfo) { |
| 19218 | // No free slot - extend break point info array. |
| 19219 | Handle<FixedArray> old_break_points = |
| 19220 | Handle<FixedArray>(FixedArray::cast(debug_info->break_points())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19221 | Handle<FixedArray> new_break_points = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 19222 | isolate->factory()->NewFixedArray( |
| 19223 | old_break_points->length() + |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19224 | DebugInfo::kEstimatedNofBreakPointsInFunction); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 19225 | |
| 19226 | debug_info->set_break_points(*new_break_points); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19227 | for (int i = 0; i < old_break_points->length(); i++) { |
| 19228 | new_break_points->set(i, old_break_points->get(i)); |
| 19229 | } |
| 19230 | index = old_break_points->length(); |
| 19231 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19232 | DCHECK(index != kNoBreakPointInfo); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19233 | |
| 19234 | // Allocate new BreakPointInfo object and set the break point. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 19235 | Handle<BreakPointInfo> new_break_point_info = Handle<BreakPointInfo>::cast( |
| 19236 | isolate->factory()->NewStruct(BREAK_POINT_INFO_TYPE)); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 19237 | new_break_point_info->set_code_offset(code_offset); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19238 | new_break_point_info->set_source_position(source_position); |
| 19239 | new_break_point_info->set_statement_position(statement_position); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 19240 | new_break_point_info->set_break_point_objects( |
| 19241 | isolate->heap()->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19242 | BreakPointInfo::SetBreakPoint(new_break_point_info, break_point_object); |
| 19243 | debug_info->break_points()->set(index, *new_break_point_info); |
| 19244 | } |
| 19245 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 19246 | // Get the break point objects for a code offset. |
| 19247 | Handle<Object> DebugInfo::GetBreakPointObjects(int code_offset) { |
| 19248 | Object* break_point_info = GetBreakPointInfo(code_offset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19249 | if (break_point_info->IsUndefined()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19250 | return GetIsolate()->factory()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19251 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19252 | return Handle<Object>( |
| 19253 | BreakPointInfo::cast(break_point_info)->break_point_objects(), |
| 19254 | GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19255 | } |
| 19256 | |
| 19257 | |
| 19258 | // Get the total number of break points. |
| 19259 | int DebugInfo::GetBreakPointCount() { |
| 19260 | if (break_points()->IsUndefined()) return 0; |
| 19261 | int count = 0; |
| 19262 | for (int i = 0; i < break_points()->length(); i++) { |
| 19263 | if (!break_points()->get(i)->IsUndefined()) { |
| 19264 | BreakPointInfo* break_point_info = |
| 19265 | BreakPointInfo::cast(break_points()->get(i)); |
| 19266 | count += break_point_info->GetBreakPointCount(); |
| 19267 | } |
| 19268 | } |
| 19269 | return count; |
| 19270 | } |
| 19271 | |
| 19272 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19273 | Handle<Object> DebugInfo::FindBreakPointInfo( |
| 19274 | Handle<DebugInfo> debug_info, Handle<Object> break_point_object) { |
| 19275 | Isolate* isolate = debug_info->GetIsolate(); |
| 19276 | if (!debug_info->break_points()->IsUndefined()) { |
| 19277 | for (int i = 0; i < debug_info->break_points()->length(); i++) { |
| 19278 | if (!debug_info->break_points()->get(i)->IsUndefined()) { |
| 19279 | Handle<BreakPointInfo> break_point_info = Handle<BreakPointInfo>( |
| 19280 | BreakPointInfo::cast(debug_info->break_points()->get(i)), isolate); |
| 19281 | if (BreakPointInfo::HasBreakPointObject(break_point_info, |
| 19282 | break_point_object)) { |
| 19283 | return break_point_info; |
| 19284 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19285 | } |
| 19286 | } |
| 19287 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19288 | return isolate->factory()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19289 | } |
| 19290 | |
| 19291 | |
| 19292 | // Find the index of the break point info object for the specified code |
| 19293 | // position. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 19294 | int DebugInfo::GetBreakPointInfoIndex(int code_offset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19295 | if (break_points()->IsUndefined()) return kNoBreakPointInfo; |
| 19296 | for (int i = 0; i < break_points()->length(); i++) { |
| 19297 | if (!break_points()->get(i)->IsUndefined()) { |
| 19298 | BreakPointInfo* break_point_info = |
| 19299 | BreakPointInfo::cast(break_points()->get(i)); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 19300 | if (break_point_info->code_offset() == code_offset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19301 | return i; |
| 19302 | } |
| 19303 | } |
| 19304 | } |
| 19305 | return kNoBreakPointInfo; |
| 19306 | } |
| 19307 | |
| 19308 | |
| 19309 | // Remove the specified break point object. |
| 19310 | void BreakPointInfo::ClearBreakPoint(Handle<BreakPointInfo> break_point_info, |
| 19311 | Handle<Object> break_point_object) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19312 | Isolate* isolate = break_point_info->GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19313 | // If there are no break points just ignore. |
| 19314 | if (break_point_info->break_point_objects()->IsUndefined()) return; |
| 19315 | // If there is a single break point clear it if it is the same. |
| 19316 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
| 19317 | if (break_point_info->break_point_objects() == *break_point_object) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 19318 | break_point_info->set_break_point_objects( |
| 19319 | isolate->heap()->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19320 | } |
| 19321 | return; |
| 19322 | } |
| 19323 | // If there are multiple break points shrink the array |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19324 | DCHECK(break_point_info->break_point_objects()->IsFixedArray()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19325 | Handle<FixedArray> old_array = |
| 19326 | Handle<FixedArray>( |
| 19327 | FixedArray::cast(break_point_info->break_point_objects())); |
| 19328 | Handle<FixedArray> new_array = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 19329 | isolate->factory()->NewFixedArray(old_array->length() - 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19330 | int found_count = 0; |
| 19331 | for (int i = 0; i < old_array->length(); i++) { |
| 19332 | if (old_array->get(i) == *break_point_object) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19333 | DCHECK(found_count == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19334 | found_count++; |
| 19335 | } else { |
| 19336 | new_array->set(i - found_count, old_array->get(i)); |
| 19337 | } |
| 19338 | } |
| 19339 | // If the break point was found in the list change it. |
| 19340 | if (found_count > 0) break_point_info->set_break_point_objects(*new_array); |
| 19341 | } |
| 19342 | |
| 19343 | |
| 19344 | // Add the specified break point object. |
| 19345 | void BreakPointInfo::SetBreakPoint(Handle<BreakPointInfo> break_point_info, |
| 19346 | Handle<Object> break_point_object) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19347 | Isolate* isolate = break_point_info->GetIsolate(); |
| 19348 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19349 | // If there was no break point objects before just set it. |
| 19350 | if (break_point_info->break_point_objects()->IsUndefined()) { |
| 19351 | break_point_info->set_break_point_objects(*break_point_object); |
| 19352 | return; |
| 19353 | } |
| 19354 | // If the break point object is the same as before just ignore. |
| 19355 | if (break_point_info->break_point_objects() == *break_point_object) return; |
| 19356 | // If there was one break point object before replace with array. |
| 19357 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19358 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19359 | array->set(0, break_point_info->break_point_objects()); |
| 19360 | array->set(1, *break_point_object); |
| 19361 | break_point_info->set_break_point_objects(*array); |
| 19362 | return; |
| 19363 | } |
| 19364 | // If there was more than one break point before extend array. |
| 19365 | Handle<FixedArray> old_array = |
| 19366 | Handle<FixedArray>( |
| 19367 | FixedArray::cast(break_point_info->break_point_objects())); |
| 19368 | Handle<FixedArray> new_array = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19369 | isolate->factory()->NewFixedArray(old_array->length() + 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19370 | for (int i = 0; i < old_array->length(); i++) { |
| 19371 | // If the break point was there before just ignore. |
| 19372 | if (old_array->get(i) == *break_point_object) return; |
| 19373 | new_array->set(i, old_array->get(i)); |
| 19374 | } |
| 19375 | // Add the new break point. |
| 19376 | new_array->set(old_array->length(), *break_point_object); |
| 19377 | break_point_info->set_break_point_objects(*new_array); |
| 19378 | } |
| 19379 | |
| 19380 | |
| 19381 | bool BreakPointInfo::HasBreakPointObject( |
| 19382 | Handle<BreakPointInfo> break_point_info, |
| 19383 | Handle<Object> break_point_object) { |
| 19384 | // No break point. |
| 19385 | if (break_point_info->break_point_objects()->IsUndefined()) return false; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 19386 | // Single break point. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19387 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
| 19388 | return break_point_info->break_point_objects() == *break_point_object; |
| 19389 | } |
| 19390 | // Multiple break points. |
| 19391 | FixedArray* array = FixedArray::cast(break_point_info->break_point_objects()); |
| 19392 | for (int i = 0; i < array->length(); i++) { |
| 19393 | if (array->get(i) == *break_point_object) { |
| 19394 | return true; |
| 19395 | } |
| 19396 | } |
| 19397 | return false; |
| 19398 | } |
| 19399 | |
| 19400 | |
| 19401 | // Get the number of break points. |
| 19402 | int BreakPointInfo::GetBreakPointCount() { |
| 19403 | // No break point. |
| 19404 | if (break_point_objects()->IsUndefined()) return 0; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 19405 | // Single break point. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19406 | if (!break_point_objects()->IsFixedArray()) return 1; |
| 19407 | // Multiple break points. |
| 19408 | return FixedArray::cast(break_point_objects())->length(); |
| 19409 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19410 | |
| 19411 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19412 | // static |
| 19413 | MaybeHandle<JSDate> JSDate::New(Handle<JSFunction> constructor, |
| 19414 | Handle<JSReceiver> new_target, double tv) { |
| 19415 | Isolate* const isolate = constructor->GetIsolate(); |
| 19416 | Handle<JSObject> result; |
| 19417 | ASSIGN_RETURN_ON_EXCEPTION(isolate, result, |
| 19418 | JSObject::New(constructor, new_target), JSDate); |
| 19419 | if (-DateCache::kMaxTimeInMs <= tv && tv <= DateCache::kMaxTimeInMs) { |
| 19420 | tv = DoubleToInteger(tv) + 0.0; |
| 19421 | } else { |
| 19422 | tv = std::numeric_limits<double>::quiet_NaN(); |
| 19423 | } |
| 19424 | Handle<Object> value = isolate->factory()->NewNumber(tv); |
| 19425 | Handle<JSDate>::cast(result)->SetValue(*value, std::isnan(tv)); |
| 19426 | return Handle<JSDate>::cast(result); |
| 19427 | } |
| 19428 | |
| 19429 | |
| 19430 | // static |
| 19431 | double JSDate::CurrentTimeValue(Isolate* isolate) { |
| 19432 | if (FLAG_log_timer_events || FLAG_prof_cpp) LOG(isolate, CurrentTimeEvent()); |
| 19433 | |
| 19434 | // According to ECMA-262, section 15.9.1, page 117, the precision of |
| 19435 | // the number in a Date object representing a particular instant in |
| 19436 | // time is milliseconds. Therefore, we floor the result of getting |
| 19437 | // the OS time. |
| 19438 | return Floor(FLAG_verify_predictable |
| 19439 | ? isolate->heap()->MonotonicallyIncreasingTimeInMs() |
| 19440 | : base::OS::TimeCurrentMillis()); |
| 19441 | } |
| 19442 | |
| 19443 | |
| 19444 | // static |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19445 | Object* JSDate::GetField(Object* object, Smi* index) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19446 | return JSDate::cast(object)->DoGetField( |
| 19447 | static_cast<FieldIndex>(index->value())); |
| 19448 | } |
| 19449 | |
| 19450 | |
| 19451 | Object* JSDate::DoGetField(FieldIndex index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19452 | DCHECK(index != kDateValue); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19453 | |
| 19454 | DateCache* date_cache = GetIsolate()->date_cache(); |
| 19455 | |
| 19456 | if (index < kFirstUncachedField) { |
| 19457 | Object* stamp = cache_stamp(); |
| 19458 | if (stamp != date_cache->stamp() && stamp->IsSmi()) { |
| 19459 | // Since the stamp is not NaN, the value is also not NaN. |
| 19460 | int64_t local_time_ms = |
| 19461 | date_cache->ToLocal(static_cast<int64_t>(value()->Number())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19462 | SetCachedFields(local_time_ms, date_cache); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19463 | } |
| 19464 | switch (index) { |
| 19465 | case kYear: return year(); |
| 19466 | case kMonth: return month(); |
| 19467 | case kDay: return day(); |
| 19468 | case kWeekday: return weekday(); |
| 19469 | case kHour: return hour(); |
| 19470 | case kMinute: return min(); |
| 19471 | case kSecond: return sec(); |
| 19472 | default: UNREACHABLE(); |
| 19473 | } |
| 19474 | } |
| 19475 | |
| 19476 | if (index >= kFirstUTCField) { |
| 19477 | return GetUTCField(index, value()->Number(), date_cache); |
| 19478 | } |
| 19479 | |
| 19480 | double time = value()->Number(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19481 | if (std::isnan(time)) return GetIsolate()->heap()->nan_value(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19482 | |
| 19483 | int64_t local_time_ms = date_cache->ToLocal(static_cast<int64_t>(time)); |
| 19484 | int days = DateCache::DaysFromTime(local_time_ms); |
| 19485 | |
| 19486 | if (index == kDays) return Smi::FromInt(days); |
| 19487 | |
| 19488 | int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days); |
| 19489 | if (index == kMillisecond) return Smi::FromInt(time_in_day_ms % 1000); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19490 | DCHECK(index == kTimeInDay); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19491 | return Smi::FromInt(time_in_day_ms); |
| 19492 | } |
| 19493 | |
| 19494 | |
| 19495 | Object* JSDate::GetUTCField(FieldIndex index, |
| 19496 | double value, |
| 19497 | DateCache* date_cache) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19498 | DCHECK(index >= kFirstUTCField); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19499 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19500 | if (std::isnan(value)) return GetIsolate()->heap()->nan_value(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19501 | |
| 19502 | int64_t time_ms = static_cast<int64_t>(value); |
| 19503 | |
| 19504 | if (index == kTimezoneOffset) { |
| 19505 | return Smi::FromInt(date_cache->TimezoneOffset(time_ms)); |
| 19506 | } |
| 19507 | |
| 19508 | int days = DateCache::DaysFromTime(time_ms); |
| 19509 | |
| 19510 | if (index == kWeekdayUTC) return Smi::FromInt(date_cache->Weekday(days)); |
| 19511 | |
| 19512 | if (index <= kDayUTC) { |
| 19513 | int year, month, day; |
| 19514 | date_cache->YearMonthDayFromDays(days, &year, &month, &day); |
| 19515 | if (index == kYearUTC) return Smi::FromInt(year); |
| 19516 | if (index == kMonthUTC) return Smi::FromInt(month); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19517 | DCHECK(index == kDayUTC); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19518 | return Smi::FromInt(day); |
| 19519 | } |
| 19520 | |
| 19521 | int time_in_day_ms = DateCache::TimeInDay(time_ms, days); |
| 19522 | switch (index) { |
| 19523 | case kHourUTC: return Smi::FromInt(time_in_day_ms / (60 * 60 * 1000)); |
| 19524 | case kMinuteUTC: return Smi::FromInt((time_in_day_ms / (60 * 1000)) % 60); |
| 19525 | case kSecondUTC: return Smi::FromInt((time_in_day_ms / 1000) % 60); |
| 19526 | case kMillisecondUTC: return Smi::FromInt(time_in_day_ms % 1000); |
| 19527 | case kDaysUTC: return Smi::FromInt(days); |
| 19528 | case kTimeInDayUTC: return Smi::FromInt(time_in_day_ms); |
| 19529 | default: UNREACHABLE(); |
| 19530 | } |
| 19531 | |
| 19532 | UNREACHABLE(); |
| 19533 | return NULL; |
| 19534 | } |
| 19535 | |
| 19536 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19537 | // static |
| 19538 | Handle<Object> JSDate::SetValue(Handle<JSDate> date, double v) { |
| 19539 | Isolate* const isolate = date->GetIsolate(); |
| 19540 | Handle<Object> value = isolate->factory()->NewNumber(v); |
| 19541 | bool value_is_nan = std::isnan(v); |
| 19542 | date->SetValue(*value, value_is_nan); |
| 19543 | return value; |
| 19544 | } |
| 19545 | |
| 19546 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19547 | void JSDate::SetValue(Object* value, bool is_value_nan) { |
| 19548 | set_value(value); |
| 19549 | if (is_value_nan) { |
| 19550 | HeapNumber* nan = GetIsolate()->heap()->nan_value(); |
| 19551 | set_cache_stamp(nan, SKIP_WRITE_BARRIER); |
| 19552 | set_year(nan, SKIP_WRITE_BARRIER); |
| 19553 | set_month(nan, SKIP_WRITE_BARRIER); |
| 19554 | set_day(nan, SKIP_WRITE_BARRIER); |
| 19555 | set_hour(nan, SKIP_WRITE_BARRIER); |
| 19556 | set_min(nan, SKIP_WRITE_BARRIER); |
| 19557 | set_sec(nan, SKIP_WRITE_BARRIER); |
| 19558 | set_weekday(nan, SKIP_WRITE_BARRIER); |
| 19559 | } else { |
| 19560 | set_cache_stamp(Smi::FromInt(DateCache::kInvalidStamp), SKIP_WRITE_BARRIER); |
| 19561 | } |
| 19562 | } |
| 19563 | |
| 19564 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19565 | // static |
| 19566 | MaybeHandle<Object> JSDate::ToPrimitive(Handle<JSReceiver> receiver, |
| 19567 | Handle<Object> hint) { |
| 19568 | Isolate* const isolate = receiver->GetIsolate(); |
| 19569 | if (hint->IsString()) { |
| 19570 | Handle<String> hint_string = Handle<String>::cast(hint); |
| 19571 | if (hint_string->Equals(isolate->heap()->number_string())) { |
| 19572 | return JSReceiver::OrdinaryToPrimitive(receiver, |
| 19573 | OrdinaryToPrimitiveHint::kNumber); |
| 19574 | } |
| 19575 | if (hint_string->Equals(isolate->heap()->default_string()) || |
| 19576 | hint_string->Equals(isolate->heap()->string_string())) { |
| 19577 | return JSReceiver::OrdinaryToPrimitive(receiver, |
| 19578 | OrdinaryToPrimitiveHint::kString); |
| 19579 | } |
| 19580 | } |
| 19581 | THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kInvalidHint, hint), |
| 19582 | Object); |
| 19583 | } |
| 19584 | |
| 19585 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19586 | void JSDate::SetCachedFields(int64_t local_time_ms, DateCache* date_cache) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19587 | int days = DateCache::DaysFromTime(local_time_ms); |
| 19588 | int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days); |
| 19589 | int year, month, day; |
| 19590 | date_cache->YearMonthDayFromDays(days, &year, &month, &day); |
| 19591 | int weekday = date_cache->Weekday(days); |
| 19592 | int hour = time_in_day_ms / (60 * 60 * 1000); |
| 19593 | int min = (time_in_day_ms / (60 * 1000)) % 60; |
| 19594 | int sec = (time_in_day_ms / 1000) % 60; |
| 19595 | set_cache_stamp(date_cache->stamp()); |
| 19596 | set_year(Smi::FromInt(year), SKIP_WRITE_BARRIER); |
| 19597 | set_month(Smi::FromInt(month), SKIP_WRITE_BARRIER); |
| 19598 | set_day(Smi::FromInt(day), SKIP_WRITE_BARRIER); |
| 19599 | set_weekday(Smi::FromInt(weekday), SKIP_WRITE_BARRIER); |
| 19600 | set_hour(Smi::FromInt(hour), SKIP_WRITE_BARRIER); |
| 19601 | set_min(Smi::FromInt(min), SKIP_WRITE_BARRIER); |
| 19602 | set_sec(Smi::FromInt(sec), SKIP_WRITE_BARRIER); |
| 19603 | } |
| 19604 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19605 | |
| 19606 | void JSArrayBuffer::Neuter() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 19607 | CHECK(is_neuterable()); |
| 19608 | CHECK(is_external()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19609 | set_backing_store(NULL); |
| 19610 | set_byte_length(Smi::FromInt(0)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19611 | set_was_neutered(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19612 | } |
| 19613 | |
| 19614 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19615 | void JSArrayBuffer::Setup(Handle<JSArrayBuffer> array_buffer, Isolate* isolate, |
| 19616 | bool is_external, void* data, size_t allocated_length, |
| 19617 | SharedFlag shared) { |
| 19618 | DCHECK(array_buffer->GetInternalFieldCount() == |
| 19619 | v8::ArrayBuffer::kInternalFieldCount); |
| 19620 | for (int i = 0; i < v8::ArrayBuffer::kInternalFieldCount; i++) { |
| 19621 | array_buffer->SetInternalField(i, Smi::FromInt(0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19622 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19623 | array_buffer->set_bit_field(0); |
| 19624 | array_buffer->set_is_external(is_external); |
| 19625 | array_buffer->set_is_neuterable(shared == SharedFlag::kNotShared); |
| 19626 | array_buffer->set_is_shared(shared == SharedFlag::kShared); |
| 19627 | |
| 19628 | Handle<Object> byte_length = |
| 19629 | isolate->factory()->NewNumberFromSize(allocated_length); |
| 19630 | CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber()); |
| 19631 | array_buffer->set_byte_length(*byte_length); |
| 19632 | // Initialize backing store at last to avoid handling of |JSArrayBuffers| that |
| 19633 | // are currently being constructed in the |ArrayBufferTracker|. The |
| 19634 | // registration method below handles the case of registering a buffer that has |
| 19635 | // already been promoted. |
| 19636 | array_buffer->set_backing_store(data); |
| 19637 | |
| 19638 | if (data && !is_external) { |
| 19639 | isolate->heap()->RegisterNewArrayBuffer(*array_buffer); |
| 19640 | } |
| 19641 | } |
| 19642 | |
| 19643 | |
| 19644 | bool JSArrayBuffer::SetupAllocatingData(Handle<JSArrayBuffer> array_buffer, |
| 19645 | Isolate* isolate, |
| 19646 | size_t allocated_length, |
| 19647 | bool initialize, SharedFlag shared) { |
| 19648 | void* data; |
| 19649 | CHECK(isolate->array_buffer_allocator() != NULL); |
| 19650 | // Prevent creating array buffers when serializing. |
| 19651 | DCHECK(!isolate->serializer_enabled()); |
| 19652 | if (allocated_length != 0) { |
| 19653 | if (initialize) { |
| 19654 | data = isolate->array_buffer_allocator()->Allocate(allocated_length); |
| 19655 | } else { |
| 19656 | data = isolate->array_buffer_allocator()->AllocateUninitialized( |
| 19657 | allocated_length); |
| 19658 | } |
| 19659 | if (data == NULL) return false; |
| 19660 | } else { |
| 19661 | data = NULL; |
| 19662 | } |
| 19663 | |
| 19664 | JSArrayBuffer::Setup(array_buffer, isolate, false, data, allocated_length, |
| 19665 | shared); |
| 19666 | return true; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19667 | } |
| 19668 | |
| 19669 | |
| 19670 | Handle<JSArrayBuffer> JSTypedArray::MaterializeArrayBuffer( |
| 19671 | Handle<JSTypedArray> typed_array) { |
| 19672 | |
| 19673 | Handle<Map> map(typed_array->map()); |
| 19674 | Isolate* isolate = typed_array->GetIsolate(); |
| 19675 | |
| 19676 | DCHECK(IsFixedTypedArrayElementsKind(map->elements_kind())); |
| 19677 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19678 | Handle<FixedTypedArrayBase> fixed_typed_array( |
| 19679 | FixedTypedArrayBase::cast(typed_array->elements())); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19680 | |
| 19681 | Handle<JSArrayBuffer> buffer(JSArrayBuffer::cast(typed_array->buffer()), |
| 19682 | isolate); |
| 19683 | void* backing_store = |
| 19684 | isolate->array_buffer_allocator()->AllocateUninitialized( |
| 19685 | fixed_typed_array->DataSize()); |
| 19686 | buffer->set_is_external(false); |
| 19687 | DCHECK(buffer->byte_length()->IsSmi() || |
| 19688 | buffer->byte_length()->IsHeapNumber()); |
| 19689 | DCHECK(NumberToInt32(buffer->byte_length()) == fixed_typed_array->DataSize()); |
| 19690 | // Initialize backing store at last to avoid handling of |JSArrayBuffers| that |
| 19691 | // are currently being constructed in the |ArrayBufferTracker|. The |
| 19692 | // registration method below handles the case of registering a buffer that has |
| 19693 | // already been promoted. |
| 19694 | buffer->set_backing_store(backing_store); |
| 19695 | isolate->heap()->RegisterNewArrayBuffer(*buffer); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19696 | memcpy(buffer->backing_store(), |
| 19697 | fixed_typed_array->DataPtr(), |
| 19698 | fixed_typed_array->DataSize()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19699 | Handle<FixedTypedArrayBase> new_elements = |
| 19700 | isolate->factory()->NewFixedTypedArrayWithExternalPointer( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19701 | fixed_typed_array->length(), typed_array->type(), |
| 19702 | static_cast<uint8_t*>(buffer->backing_store())); |
| 19703 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19704 | typed_array->set_elements(*new_elements); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19705 | |
| 19706 | return buffer; |
| 19707 | } |
| 19708 | |
| 19709 | |
| 19710 | Handle<JSArrayBuffer> JSTypedArray::GetBuffer() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19711 | Handle<JSArrayBuffer> array_buffer(JSArrayBuffer::cast(buffer()), |
| 19712 | GetIsolate()); |
| 19713 | if (array_buffer->was_neutered() || |
| 19714 | array_buffer->backing_store() != nullptr) { |
| 19715 | return array_buffer; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19716 | } |
| 19717 | Handle<JSTypedArray> self(this); |
| 19718 | return MaterializeArrayBuffer(self); |
| 19719 | } |
| 19720 | |
| 19721 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19722 | Handle<PropertyCell> PropertyCell::InvalidateEntry( |
| 19723 | Handle<GlobalDictionary> dictionary, int entry) { |
| 19724 | Isolate* isolate = dictionary->GetIsolate(); |
| 19725 | // Swap with a copy. |
| 19726 | DCHECK(dictionary->ValueAt(entry)->IsPropertyCell()); |
| 19727 | Handle<PropertyCell> cell(PropertyCell::cast(dictionary->ValueAt(entry))); |
| 19728 | auto new_cell = isolate->factory()->NewPropertyCell(); |
| 19729 | new_cell->set_value(cell->value()); |
| 19730 | dictionary->ValueAtPut(entry, *new_cell); |
| 19731 | bool is_the_hole = cell->value()->IsTheHole(); |
| 19732 | // Cell is officially mutable henceforth. |
| 19733 | PropertyDetails details = cell->property_details(); |
| 19734 | details = details.set_cell_type(is_the_hole ? PropertyCellType::kInvalidated |
| 19735 | : PropertyCellType::kMutable); |
| 19736 | new_cell->set_property_details(details); |
| 19737 | // Old cell is ready for invalidation. |
| 19738 | if (is_the_hole) { |
| 19739 | cell->set_value(isolate->heap()->undefined_value()); |
| 19740 | } else { |
| 19741 | cell->set_value(isolate->heap()->the_hole_value()); |
| 19742 | } |
| 19743 | details = details.set_cell_type(PropertyCellType::kInvalidated); |
| 19744 | cell->set_property_details(details); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19745 | cell->dependent_code()->DeoptimizeDependentCodeGroup( |
| 19746 | isolate, DependentCode::kPropertyCellChangedGroup); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19747 | return new_cell; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19748 | } |
| 19749 | |
| 19750 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19751 | PropertyCellConstantType PropertyCell::GetConstantType() { |
| 19752 | if (value()->IsSmi()) return PropertyCellConstantType::kSmi; |
| 19753 | return PropertyCellConstantType::kStableMap; |
| 19754 | } |
| 19755 | |
| 19756 | |
| 19757 | static bool RemainsConstantType(Handle<PropertyCell> cell, |
| 19758 | Handle<Object> value) { |
| 19759 | // TODO(dcarney): double->smi and smi->double transition from kConstant |
| 19760 | if (cell->value()->IsSmi() && value->IsSmi()) { |
| 19761 | return true; |
| 19762 | } else if (cell->value()->IsHeapObject() && value->IsHeapObject()) { |
| 19763 | return HeapObject::cast(cell->value())->map() == |
| 19764 | HeapObject::cast(*value)->map() && |
| 19765 | HeapObject::cast(*value)->map()->is_stable(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 19766 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19767 | return false; |
| 19768 | } |
| 19769 | |
| 19770 | |
| 19771 | PropertyCellType PropertyCell::UpdatedType(Handle<PropertyCell> cell, |
| 19772 | Handle<Object> value, |
| 19773 | PropertyDetails details) { |
| 19774 | PropertyCellType type = details.cell_type(); |
| 19775 | DCHECK(!value->IsTheHole()); |
| 19776 | if (cell->value()->IsTheHole()) { |
| 19777 | switch (type) { |
| 19778 | // Only allow a cell to transition once into constant state. |
| 19779 | case PropertyCellType::kUninitialized: |
| 19780 | if (value->IsUndefined()) return PropertyCellType::kUndefined; |
| 19781 | return PropertyCellType::kConstant; |
| 19782 | case PropertyCellType::kInvalidated: |
| 19783 | return PropertyCellType::kMutable; |
| 19784 | default: |
| 19785 | UNREACHABLE(); |
| 19786 | return PropertyCellType::kMutable; |
| 19787 | } |
| 19788 | } |
| 19789 | switch (type) { |
| 19790 | case PropertyCellType::kUndefined: |
| 19791 | return PropertyCellType::kConstant; |
| 19792 | case PropertyCellType::kConstant: |
| 19793 | if (*value == cell->value()) return PropertyCellType::kConstant; |
| 19794 | // Fall through. |
| 19795 | case PropertyCellType::kConstantType: |
| 19796 | if (RemainsConstantType(cell, value)) { |
| 19797 | return PropertyCellType::kConstantType; |
| 19798 | } |
| 19799 | // Fall through. |
| 19800 | case PropertyCellType::kMutable: |
| 19801 | return PropertyCellType::kMutable; |
| 19802 | } |
| 19803 | UNREACHABLE(); |
| 19804 | return PropertyCellType::kMutable; |
| 19805 | } |
| 19806 | |
| 19807 | |
| 19808 | void PropertyCell::UpdateCell(Handle<GlobalDictionary> dictionary, int entry, |
| 19809 | Handle<Object> value, PropertyDetails details) { |
| 19810 | DCHECK(!value->IsTheHole()); |
| 19811 | DCHECK(dictionary->ValueAt(entry)->IsPropertyCell()); |
| 19812 | Handle<PropertyCell> cell(PropertyCell::cast(dictionary->ValueAt(entry))); |
| 19813 | const PropertyDetails original_details = cell->property_details(); |
| 19814 | // Data accesses could be cached in ics or optimized code. |
| 19815 | bool invalidate = |
| 19816 | original_details.kind() == kData && details.kind() == kAccessor; |
| 19817 | int index = original_details.dictionary_index(); |
| 19818 | PropertyCellType old_type = original_details.cell_type(); |
| 19819 | // Preserve the enumeration index unless the property was deleted or never |
| 19820 | // initialized. |
| 19821 | if (cell->value()->IsTheHole()) { |
| 19822 | index = dictionary->NextEnumerationIndex(); |
| 19823 | dictionary->SetNextEnumerationIndex(index + 1); |
| 19824 | // Negative lookup cells must be invalidated. |
| 19825 | invalidate = true; |
| 19826 | } |
| 19827 | DCHECK(index > 0); |
| 19828 | details = details.set_index(index); |
| 19829 | |
| 19830 | PropertyCellType new_type = UpdatedType(cell, value, original_details); |
| 19831 | if (invalidate) cell = PropertyCell::InvalidateEntry(dictionary, entry); |
| 19832 | |
| 19833 | // Install new property details and cell value. |
| 19834 | details = details.set_cell_type(new_type); |
| 19835 | cell->set_property_details(details); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19836 | cell->set_value(*value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19837 | |
| 19838 | // Deopt when transitioning from a constant type. |
| 19839 | if (!invalidate && (old_type != new_type || |
| 19840 | original_details.IsReadOnly() != details.IsReadOnly())) { |
| 19841 | Isolate* isolate = dictionary->GetIsolate(); |
| 19842 | cell->dependent_code()->DeoptimizeDependentCodeGroup( |
| 19843 | isolate, DependentCode::kPropertyCellChangedGroup); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19844 | } |
| 19845 | } |
| 19846 | |
| 19847 | |
| 19848 | // static |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19849 | void PropertyCell::SetValueWithInvalidation(Handle<PropertyCell> cell, |
| 19850 | Handle<Object> new_value) { |
| 19851 | if (cell->value() != *new_value) { |
| 19852 | cell->set_value(*new_value); |
| 19853 | Isolate* isolate = cell->GetIsolate(); |
| 19854 | cell->dependent_code()->DeoptimizeDependentCodeGroup( |
| 19855 | isolate, DependentCode::kPropertyCellChangedGroup); |
| 19856 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19857 | } |
| 19858 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19859 | } // namespace internal |
| 19860 | } // namespace v8 |