| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 #include "execution.h" | 42 #include "execution.h" |
| 43 #include "full-codegen.h" | 43 #include "full-codegen.h" |
| 44 #include "hydrogen.h" | 44 #include "hydrogen.h" |
| 45 #include "isolate-inl.h" | 45 #include "isolate-inl.h" |
| 46 #include "log.h" | 46 #include "log.h" |
| 47 #include "objects-inl.h" | 47 #include "objects-inl.h" |
| 48 #include "objects-visiting-inl.h" | 48 #include "objects-visiting-inl.h" |
| 49 #include "macro-assembler.h" | 49 #include "macro-assembler.h" |
| 50 #include "mark-compact.h" | 50 #include "mark-compact.h" |
| 51 #include "safepoint-table.h" | 51 #include "safepoint-table.h" |
| 52 #include "string-search.h" | |
| 53 #include "string-stream.h" | 52 #include "string-stream.h" |
| 54 #include "utils.h" | 53 #include "utils.h" |
| 55 | 54 |
| 56 #ifdef ENABLE_DISASSEMBLER | 55 #ifdef ENABLE_DISASSEMBLER |
| 57 #include "disasm.h" | 56 #include "disasm.h" |
| 58 #include "disassembler.h" | 57 #include "disassembler.h" |
| 59 #endif | 58 #endif |
| 60 | 59 |
| 61 namespace v8 { | 60 namespace v8 { |
| 62 namespace internal { | 61 namespace internal { |
| (...skipping 6170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6233 for (Object* current = this; | 6232 for (Object* current = this; |
| 6234 current != heap->null_value() && current->IsJSObject(); | 6233 current != heap->null_value() && current->IsJSObject(); |
| 6235 current = JSObject::cast(current)->GetPrototype()) { | 6234 current = JSObject::cast(current)->GetPrototype()) { |
| 6236 JSObject::cast(current)->LocalLookupRealNamedProperty(name, result); | 6235 JSObject::cast(current)->LocalLookupRealNamedProperty(name, result); |
| 6237 if (result->IsPropertyCallbacks()) return; | 6236 if (result->IsPropertyCallbacks()) return; |
| 6238 } | 6237 } |
| 6239 result->NotFound(); | 6238 result->NotFound(); |
| 6240 } | 6239 } |
| 6241 | 6240 |
| 6242 | 6241 |
| 6243 static bool ContainsOnlyValidKeys(Handle<FixedArray> array) { | |
| 6244 int len = array->length(); | |
| 6245 for (int i = 0; i < len; i++) { | |
| 6246 Object* e = array->get(i); | |
| 6247 if (!(e->IsString() || e->IsNumber())) return false; | |
| 6248 } | |
| 6249 return true; | |
| 6250 } | |
| 6251 | |
| 6252 | |
| 6253 static Handle<FixedArray> ReduceFixedArrayTo( | |
| 6254 Handle<FixedArray> array, int length) { | |
| 6255 ASSERT(array->length() >= length); | |
| 6256 if (array->length() == length) return array; | |
| 6257 | |
| 6258 Handle<FixedArray> new_array = | |
| 6259 array->GetIsolate()->factory()->NewFixedArray(length); | |
| 6260 for (int i = 0; i < length; ++i) new_array->set(i, array->get(i)); | |
| 6261 return new_array; | |
| 6262 } | |
| 6263 | |
| 6264 | |
| 6265 static Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object, | |
| 6266 bool cache_result) { | |
| 6267 Isolate* isolate = object->GetIsolate(); | |
| 6268 if (object->HasFastProperties()) { | |
| 6269 int own_property_count = object->map()->EnumLength(); | |
| 6270 // If the enum length of the given map is set to kInvalidEnumCache, this | |
| 6271 // means that the map itself has never used the present enum cache. The | |
| 6272 // first step to using the cache is to set the enum length of the map by | |
| 6273 // counting the number of own descriptors that are not DONT_ENUM or | |
| 6274 // SYMBOLIC. | |
| 6275 if (own_property_count == kInvalidEnumCacheSentinel) { | |
| 6276 own_property_count = object->map()->NumberOfDescribedProperties( | |
| 6277 OWN_DESCRIPTORS, DONT_SHOW); | |
| 6278 } else { | |
| 6279 ASSERT(own_property_count == object->map()->NumberOfDescribedProperties( | |
| 6280 OWN_DESCRIPTORS, DONT_SHOW)); | |
| 6281 } | |
| 6282 | |
| 6283 if (object->map()->instance_descriptors()->HasEnumCache()) { | |
| 6284 DescriptorArray* desc = object->map()->instance_descriptors(); | |
| 6285 Handle<FixedArray> keys(desc->GetEnumCache(), isolate); | |
| 6286 | |
| 6287 // In case the number of properties required in the enum are actually | |
| 6288 // present, we can reuse the enum cache. Otherwise, this means that the | |
| 6289 // enum cache was generated for a previous (smaller) version of the | |
| 6290 // Descriptor Array. In that case we regenerate the enum cache. | |
| 6291 if (own_property_count <= keys->length()) { | |
| 6292 if (cache_result) object->map()->SetEnumLength(own_property_count); | |
| 6293 isolate->counters()->enum_cache_hits()->Increment(); | |
| 6294 return ReduceFixedArrayTo(keys, own_property_count); | |
| 6295 } | |
| 6296 } | |
| 6297 | |
| 6298 Handle<Map> map(object->map()); | |
| 6299 | |
| 6300 if (map->instance_descriptors()->IsEmpty()) { | |
| 6301 isolate->counters()->enum_cache_hits()->Increment(); | |
| 6302 if (cache_result) map->SetEnumLength(0); | |
| 6303 return isolate->factory()->empty_fixed_array(); | |
| 6304 } | |
| 6305 | |
| 6306 isolate->counters()->enum_cache_misses()->Increment(); | |
| 6307 | |
| 6308 Handle<FixedArray> storage = isolate->factory()->NewFixedArray( | |
| 6309 own_property_count); | |
| 6310 Handle<FixedArray> indices = isolate->factory()->NewFixedArray( | |
| 6311 own_property_count); | |
| 6312 | |
| 6313 Handle<DescriptorArray> descs = | |
| 6314 Handle<DescriptorArray>(object->map()->instance_descriptors(), isolate); | |
| 6315 | |
| 6316 int size = map->NumberOfOwnDescriptors(); | |
| 6317 int index = 0; | |
| 6318 | |
| 6319 for (int i = 0; i < size; i++) { | |
| 6320 PropertyDetails details = descs->GetDetails(i); | |
| 6321 Object* key = descs->GetKey(i); | |
| 6322 if (!(details.IsDontEnum() || key->IsSymbol())) { | |
| 6323 storage->set(index, key); | |
| 6324 if (!indices.is_null()) { | |
| 6325 if (details.type() != FIELD) { | |
| 6326 indices = Handle<FixedArray>(); | |
| 6327 } else { | |
| 6328 int field_index = descs->GetFieldIndex(i); | |
| 6329 if (field_index >= map->inobject_properties()) { | |
| 6330 field_index = -(field_index - map->inobject_properties() + 1); | |
| 6331 } | |
| 6332 field_index = field_index << 1; | |
| 6333 if (details.representation().IsDouble()) { | |
| 6334 field_index |= 1; | |
| 6335 } | |
| 6336 indices->set(index, Smi::FromInt(field_index)); | |
| 6337 } | |
| 6338 } | |
| 6339 index++; | |
| 6340 } | |
| 6341 } | |
| 6342 ASSERT(index == storage->length()); | |
| 6343 | |
| 6344 Handle<FixedArray> bridge_storage = | |
| 6345 isolate->factory()->NewFixedArray( | |
| 6346 DescriptorArray::kEnumCacheBridgeLength); | |
| 6347 DescriptorArray* desc = object->map()->instance_descriptors(); | |
| 6348 desc->SetEnumCache(*bridge_storage, | |
| 6349 *storage, | |
| 6350 indices.is_null() ? Object::cast(Smi::FromInt(0)) | |
| 6351 : Object::cast(*indices)); | |
| 6352 if (cache_result) { | |
| 6353 object->map()->SetEnumLength(own_property_count); | |
| 6354 } | |
| 6355 return storage; | |
| 6356 } else { | |
| 6357 Handle<NameDictionary> dictionary(object->property_dictionary()); | |
| 6358 int length = dictionary->NumberOfEnumElements(); | |
| 6359 if (length == 0) { | |
| 6360 return Handle<FixedArray>(isolate->heap()->empty_fixed_array()); | |
| 6361 } | |
| 6362 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(length); | |
| 6363 dictionary->CopyEnumKeysTo(*storage); | |
| 6364 return storage; | |
| 6365 } | |
| 6366 } | |
| 6367 | |
| 6368 | |
| 6369 MaybeHandle<FixedArray> JSReceiver::GetKeys(Handle<JSReceiver> object, | |
| 6370 KeyCollectionType type) { | |
| 6371 USE(ContainsOnlyValidKeys); | |
| 6372 Isolate* isolate = object->GetIsolate(); | |
| 6373 Handle<FixedArray> content = isolate->factory()->empty_fixed_array(); | |
| 6374 Handle<JSObject> arguments_boilerplate = Handle<JSObject>( | |
| 6375 isolate->context()->native_context()->sloppy_arguments_boilerplate(), | |
| 6376 isolate); | |
| 6377 Handle<JSFunction> arguments_function = Handle<JSFunction>( | |
| 6378 JSFunction::cast(arguments_boilerplate->map()->constructor()), | |
| 6379 isolate); | |
| 6380 | |
| 6381 // Only collect keys if access is permitted. | |
| 6382 for (Handle<Object> p = object; | |
| 6383 *p != isolate->heap()->null_value(); | |
| 6384 p = Handle<Object>(p->GetPrototype(isolate), isolate)) { | |
| 6385 if (p->IsJSProxy()) { | |
| 6386 Handle<JSProxy> proxy(JSProxy::cast(*p), isolate); | |
| 6387 Handle<Object> args[] = { proxy }; | |
| 6388 Handle<Object> names; | |
| 6389 ASSIGN_RETURN_ON_EXCEPTION( | |
| 6390 isolate, names, | |
| 6391 Execution::Call(isolate, | |
| 6392 isolate->proxy_enumerate(), | |
| 6393 object, | |
| 6394 ARRAY_SIZE(args), | |
| 6395 args), | |
| 6396 FixedArray); | |
| 6397 ASSIGN_RETURN_ON_EXCEPTION( | |
| 6398 isolate, content, | |
| 6399 FixedArray::AddKeysFromJSArray( | |
| 6400 content, Handle<JSArray>::cast(names)), | |
| 6401 FixedArray); | |
| 6402 break; | |
| 6403 } | |
| 6404 | |
| 6405 Handle<JSObject> current(JSObject::cast(*p), isolate); | |
| 6406 | |
| 6407 // Check access rights if required. | |
| 6408 if (current->IsAccessCheckNeeded() && | |
| 6409 !isolate->MayNamedAccess( | |
| 6410 current, isolate->factory()->undefined_value(), v8::ACCESS_KEYS)) { | |
| 6411 isolate->ReportFailedAccessCheck(current, v8::ACCESS_KEYS); | |
| 6412 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, FixedArray); | |
| 6413 break; | |
| 6414 } | |
| 6415 | |
| 6416 // Compute the element keys. | |
| 6417 Handle<FixedArray> element_keys = | |
| 6418 isolate->factory()->NewFixedArray(current->NumberOfEnumElements()); | |
| 6419 current->GetEnumElementKeys(*element_keys); | |
| 6420 ASSIGN_RETURN_ON_EXCEPTION( | |
| 6421 isolate, content, | |
| 6422 FixedArray::UnionOfKeys(content, element_keys), | |
| 6423 FixedArray); | |
| 6424 ASSERT(ContainsOnlyValidKeys(content)); | |
| 6425 | |
| 6426 // Add the element keys from the interceptor. | |
| 6427 if (current->HasIndexedInterceptor()) { | |
| 6428 Handle<JSArray> result; | |
| 6429 if (JSObject::GetKeysForIndexedInterceptor( | |
| 6430 current, object).ToHandle(&result)) { | |
| 6431 ASSIGN_RETURN_ON_EXCEPTION( | |
| 6432 isolate, content, | |
| 6433 FixedArray::AddKeysFromJSArray(content, result), | |
| 6434 FixedArray); | |
| 6435 } | |
| 6436 ASSERT(ContainsOnlyValidKeys(content)); | |
| 6437 } | |
| 6438 | |
| 6439 // We can cache the computed property keys if access checks are | |
| 6440 // not needed and no interceptors are involved. | |
| 6441 // | |
| 6442 // We do not use the cache if the object has elements and | |
| 6443 // therefore it does not make sense to cache the property names | |
| 6444 // for arguments objects. Arguments objects will always have | |
| 6445 // elements. | |
| 6446 // Wrapped strings have elements, but don't have an elements | |
| 6447 // array or dictionary. So the fast inline test for whether to | |
| 6448 // use the cache says yes, so we should not create a cache. | |
| 6449 bool cache_enum_keys = | |
| 6450 ((current->map()->constructor() != *arguments_function) && | |
| 6451 !current->IsJSValue() && | |
| 6452 !current->IsAccessCheckNeeded() && | |
| 6453 !current->HasNamedInterceptor() && | |
| 6454 !current->HasIndexedInterceptor()); | |
| 6455 // Compute the property keys and cache them if possible. | |
| 6456 ASSIGN_RETURN_ON_EXCEPTION( | |
| 6457 isolate, content, | |
| 6458 FixedArray::UnionOfKeys( | |
| 6459 content, GetEnumPropertyKeys(current, cache_enum_keys)), | |
| 6460 FixedArray); | |
| 6461 ASSERT(ContainsOnlyValidKeys(content)); | |
| 6462 | |
| 6463 // Add the property keys from the interceptor. | |
| 6464 if (current->HasNamedInterceptor()) { | |
| 6465 Handle<JSArray> result; | |
| 6466 if (JSObject::GetKeysForNamedInterceptor( | |
| 6467 current, object).ToHandle(&result)) { | |
| 6468 ASSIGN_RETURN_ON_EXCEPTION( | |
| 6469 isolate, content, | |
| 6470 FixedArray::AddKeysFromJSArray(content, result), | |
| 6471 FixedArray); | |
| 6472 } | |
| 6473 ASSERT(ContainsOnlyValidKeys(content)); | |
| 6474 } | |
| 6475 | |
| 6476 // If we only want local properties we bail out after the first | |
| 6477 // iteration. | |
| 6478 if (type == LOCAL_ONLY) break; | |
| 6479 } | |
| 6480 return content; | |
| 6481 } | |
| 6482 | |
| 6483 | |
| 6484 // Try to update an accessor in an elements dictionary. Return true if the | 6242 // Try to update an accessor in an elements dictionary. Return true if the |
| 6485 // update succeeded, and false otherwise. | 6243 // update succeeded, and false otherwise. |
| 6486 static bool UpdateGetterSetterInDictionary( | 6244 static bool UpdateGetterSetterInDictionary( |
| 6487 SeededNumberDictionary* dictionary, | 6245 SeededNumberDictionary* dictionary, |
| 6488 uint32_t index, | 6246 uint32_t index, |
| 6489 Object* getter, | 6247 Object* getter, |
| 6490 Object* setter, | 6248 Object* setter, |
| 6491 PropertyAttributes attributes) { | 6249 PropertyAttributes attributes) { |
| 6492 int entry = dictionary->FindEntry(index); | 6250 int entry = dictionary->FindEntry(index); |
| 6493 if (entry != SeededNumberDictionary::kNotFound) { | 6251 if (entry != SeededNumberDictionary::kNotFound) { |
| (...skipping 2642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9136 SlicedString* slice = SlicedString::cast(source); | 8894 SlicedString* slice = SlicedString::cast(source); |
| 9137 unsigned offset = slice->offset(); | 8895 unsigned offset = slice->offset(); |
| 9138 WriteToFlat(slice->parent(), sink, from + offset, to + offset); | 8896 WriteToFlat(slice->parent(), sink, from + offset, to + offset); |
| 9139 return; | 8897 return; |
| 9140 } | 8898 } |
| 9141 } | 8899 } |
| 9142 } | 8900 } |
| 9143 } | 8901 } |
| 9144 | 8902 |
| 9145 | 8903 |
| 9146 | |
| 9147 template <typename SourceChar> | |
| 9148 static void CalculateLineEndsImpl(Isolate* isolate, | |
| 9149 List<int>* line_ends, | |
| 9150 Vector<const SourceChar> src, | |
| 9151 bool include_ending_line) { | |
| 9152 const int src_len = src.length(); | |
| 9153 StringSearch<uint8_t, SourceChar> search(isolate, STATIC_ASCII_VECTOR("\n")); | |
| 9154 | |
| 9155 // Find and record line ends. | |
| 9156 int position = 0; | |
| 9157 while (position != -1 && position < src_len) { | |
| 9158 position = search.Search(src, position); | |
| 9159 if (position != -1) { | |
| 9160 line_ends->Add(position); | |
| 9161 position++; | |
| 9162 } else if (include_ending_line) { | |
| 9163 // Even if the last line misses a line end, it is counted. | |
| 9164 line_ends->Add(src_len); | |
| 9165 return; | |
| 9166 } | |
| 9167 } | |
| 9168 } | |
| 9169 | |
| 9170 | |
| 9171 Handle<FixedArray> String::CalculateLineEnds(Handle<String> src, | |
| 9172 bool include_ending_line) { | |
| 9173 src = Flatten(src); | |
| 9174 // Rough estimate of line count based on a roughly estimated average | |
| 9175 // length of (unpacked) code. | |
| 9176 int line_count_estimate = src->length() >> 4; | |
| 9177 List<int> line_ends(line_count_estimate); | |
| 9178 Isolate* isolate = src->GetIsolate(); | |
| 9179 { DisallowHeapAllocation no_allocation; // ensure vectors stay valid. | |
| 9180 // Dispatch on type of strings. | |
| 9181 String::FlatContent content = src->GetFlatContent(); | |
| 9182 ASSERT(content.IsFlat()); | |
| 9183 if (content.IsAscii()) { | |
| 9184 CalculateLineEndsImpl(isolate, | |
| 9185 &line_ends, | |
| 9186 content.ToOneByteVector(), | |
| 9187 include_ending_line); | |
| 9188 } else { | |
| 9189 CalculateLineEndsImpl(isolate, | |
| 9190 &line_ends, | |
| 9191 content.ToUC16Vector(), | |
| 9192 include_ending_line); | |
| 9193 } | |
| 9194 } | |
| 9195 int line_count = line_ends.length(); | |
| 9196 Handle<FixedArray> array = isolate->factory()->NewFixedArray(line_count); | |
| 9197 for (int i = 0; i < line_count; i++) { | |
| 9198 array->set(i, Smi::FromInt(line_ends[i])); | |
| 9199 } | |
| 9200 return array; | |
| 9201 } | |
| 9202 | |
| 9203 | |
| 9204 // Compares the contents of two strings by reading and comparing | 8904 // Compares the contents of two strings by reading and comparing |
| 9205 // int-sized blocks of characters. | 8905 // int-sized blocks of characters. |
| 9206 template <typename Char> | 8906 template <typename Char> |
| 9207 static inline bool CompareRawStringContents(const Char* const a, | 8907 static inline bool CompareRawStringContents(const Char* const a, |
| 9208 const Char* const b, | 8908 const Char* const b, |
| 9209 int length) { | 8909 int length) { |
| 9210 int i = 0; | 8910 int i = 0; |
| 9211 #ifndef V8_HOST_CAN_READ_UNALIGNED | 8911 #ifndef V8_HOST_CAN_READ_UNALIGNED |
| 9212 // If this architecture isn't comfortable reading unaligned ints | 8912 // If this architecture isn't comfortable reading unaligned ints |
| 9213 // then we have to check that the strings are aligned before | 8913 // then we have to check that the strings are aligned before |
| (...skipping 1189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 10403 CStrVector(to_string)); | 10103 CStrVector(to_string)); |
| 10404 if (!maybe_string->To(&internalized_to_string)) return maybe_string; | 10104 if (!maybe_string->To(&internalized_to_string)) return maybe_string; |
| 10405 } | 10105 } |
| 10406 set_to_string(internalized_to_string); | 10106 set_to_string(internalized_to_string); |
| 10407 set_to_number(to_number); | 10107 set_to_number(to_number); |
| 10408 set_kind(kind); | 10108 set_kind(kind); |
| 10409 return this; | 10109 return this; |
| 10410 } | 10110 } |
| 10411 | 10111 |
| 10412 | 10112 |
| 10413 void Script::InitLineEnds(Handle<Script> script) { | |
| 10414 if (!script->line_ends()->IsUndefined()) return; | |
| 10415 | |
| 10416 Isolate* isolate = script->GetIsolate(); | |
| 10417 | |
| 10418 if (!script->source()->IsString()) { | |
| 10419 ASSERT(script->source()->IsUndefined()); | |
| 10420 Handle<FixedArray> empty = isolate->factory()->NewFixedArray(0); | |
| 10421 script->set_line_ends(*empty); | |
| 10422 ASSERT(script->line_ends()->IsFixedArray()); | |
| 10423 return; | |
| 10424 } | |
| 10425 | |
| 10426 Handle<String> src(String::cast(script->source()), isolate); | |
| 10427 | |
| 10428 Handle<FixedArray> array = String::CalculateLineEnds(src, true); | |
| 10429 | |
| 10430 if (*array != isolate->heap()->empty_fixed_array()) { | |
| 10431 array->set_map(isolate->heap()->fixed_cow_array_map()); | |
| 10432 } | |
| 10433 | |
| 10434 script->set_line_ends(*array); | |
| 10435 ASSERT(script->line_ends()->IsFixedArray()); | |
| 10436 } | |
| 10437 | |
| 10438 | |
| 10439 int Script::GetColumnNumber(Handle<Script> script, int code_pos) { | |
| 10440 int line_number = GetLineNumber(script, code_pos); | |
| 10441 if (line_number == -1) return -1; | |
| 10442 | |
| 10443 DisallowHeapAllocation no_allocation; | |
| 10444 FixedArray* line_ends_array = FixedArray::cast(script->line_ends()); | |
| 10445 line_number = line_number - script->line_offset()->value(); | |
| 10446 if (line_number == 0) return code_pos + script->column_offset()->value(); | |
| 10447 int prev_line_end_pos = | |
| 10448 Smi::cast(line_ends_array->get(line_number - 1))->value(); | |
| 10449 return code_pos - (prev_line_end_pos + 1); | |
| 10450 } | |
| 10451 | |
| 10452 | |
| 10453 int Script::GetLineNumberWithArray(int code_pos) { | |
| 10454 DisallowHeapAllocation no_allocation; | |
| 10455 ASSERT(line_ends()->IsFixedArray()); | |
| 10456 FixedArray* line_ends_array = FixedArray::cast(line_ends()); | |
| 10457 int line_ends_len = line_ends_array->length(); | |
| 10458 if (line_ends_len == 0) return -1; | |
| 10459 | |
| 10460 if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) { | |
| 10461 return line_offset()->value(); | |
| 10462 } | |
| 10463 | |
| 10464 int left = 0; | |
| 10465 int right = line_ends_len; | |
| 10466 while (int half = (right - left) / 2) { | |
| 10467 if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) { | |
| 10468 right -= half; | |
| 10469 } else { | |
| 10470 left += half; | |
| 10471 } | |
| 10472 } | |
| 10473 return right + line_offset()->value(); | |
| 10474 } | |
| 10475 | |
| 10476 | |
| 10477 int Script::GetLineNumber(Handle<Script> script, int code_pos) { | |
| 10478 InitLineEnds(script); | |
| 10479 return script->GetLineNumberWithArray(code_pos); | |
| 10480 } | |
| 10481 | |
| 10482 | |
| 10483 int Script::GetLineNumber(int code_pos) { | |
| 10484 DisallowHeapAllocation no_allocation; | |
| 10485 if (!line_ends()->IsUndefined()) return GetLineNumberWithArray(code_pos); | |
| 10486 | |
| 10487 // Slow mode: we do not have line_ends. We have to iterate through source. | |
| 10488 if (!source()->IsString()) return -1; | |
| 10489 | |
| 10490 String* source_string = String::cast(source()); | |
| 10491 int line = 0; | |
| 10492 int len = source_string->length(); | |
| 10493 for (int pos = 0; pos < len; pos++) { | |
| 10494 if (pos == code_pos) break; | |
| 10495 if (source_string->Get(pos) == '\n') line++; | |
| 10496 } | |
| 10497 return line; | |
| 10498 } | |
| 10499 | |
| 10500 | |
| 10501 Handle<Object> Script::GetNameOrSourceURL(Handle<Script> script) { | |
| 10502 Isolate* isolate = script->GetIsolate(); | |
| 10503 Handle<String> name_or_source_url_key = | |
| 10504 isolate->factory()->InternalizeOneByteString( | |
| 10505 STATIC_ASCII_VECTOR("nameOrSourceURL")); | |
| 10506 Handle<JSObject> script_wrapper = Script::GetWrapper(script); | |
| 10507 Handle<Object> property = Object::GetProperty( | |
| 10508 script_wrapper, name_or_source_url_key).ToHandleChecked(); | |
| 10509 ASSERT(property->IsJSFunction()); | |
| 10510 Handle<JSFunction> method = Handle<JSFunction>::cast(property); | |
| 10511 Handle<Object> result; | |
| 10512 // Do not check against pending exception, since this function may be called | |
| 10513 // when an exception has already been pending. | |
| 10514 if (!Execution::TryCall(method, script_wrapper, 0, NULL).ToHandle(&result)) { | |
| 10515 return isolate->factory()->undefined_value(); | |
| 10516 } | |
| 10517 return result; | |
| 10518 } | |
| 10519 | |
| 10520 | |
| 10521 // Wrappers for scripts are kept alive and cached in weak global | |
| 10522 // handles referred from foreign objects held by the scripts as long as | |
| 10523 // they are used. When they are not used anymore, the garbage | |
| 10524 // collector will call the weak callback on the global handle | |
| 10525 // associated with the wrapper and get rid of both the wrapper and the | |
| 10526 // handle. | |
| 10527 static void ClearWrapperCache( | |
| 10528 const v8::WeakCallbackData<v8::Value, void>& data) { | |
| 10529 Object** location = reinterpret_cast<Object**>(data.GetParameter()); | |
| 10530 JSValue* wrapper = JSValue::cast(*location); | |
| 10531 Foreign* foreign = Script::cast(wrapper->value())->wrapper(); | |
| 10532 ASSERT_EQ(foreign->foreign_address(), reinterpret_cast<Address>(location)); | |
| 10533 foreign->set_foreign_address(0); | |
| 10534 GlobalHandles::Destroy(location); | |
| 10535 Isolate* isolate = reinterpret_cast<Isolate*>(data.GetIsolate()); | |
| 10536 isolate->counters()->script_wrappers()->Decrement(); | |
| 10537 } | |
| 10538 | |
| 10539 | |
| 10540 Handle<JSObject> Script::GetWrapper(Handle<Script> script) { | |
| 10541 if (script->wrapper()->foreign_address() != NULL) { | |
| 10542 // Return a handle for the existing script wrapper from the cache. | |
| 10543 return Handle<JSValue>( | |
| 10544 *reinterpret_cast<JSValue**>(script->wrapper()->foreign_address())); | |
| 10545 } | |
| 10546 Isolate* isolate = script->GetIsolate(); | |
| 10547 // Construct a new script wrapper. | |
| 10548 isolate->counters()->script_wrappers()->Increment(); | |
| 10549 Handle<JSFunction> constructor = isolate->script_function(); | |
| 10550 Handle<JSValue> result = | |
| 10551 Handle<JSValue>::cast(isolate->factory()->NewJSObject(constructor)); | |
| 10552 | |
| 10553 result->set_value(*script); | |
| 10554 | |
| 10555 // Create a new weak global handle and use it to cache the wrapper | |
| 10556 // for future use. The cache will automatically be cleared by the | |
| 10557 // garbage collector when it is not used anymore. | |
| 10558 Handle<Object> handle = isolate->global_handles()->Create(*result); | |
| 10559 GlobalHandles::MakeWeak(handle.location(), | |
| 10560 reinterpret_cast<void*>(handle.location()), | |
| 10561 &ClearWrapperCache); | |
| 10562 script->wrapper()->set_foreign_address( | |
| 10563 reinterpret_cast<Address>(handle.location())); | |
| 10564 return result; | |
| 10565 } | |
| 10566 | |
| 10567 | |
| 10568 String* SharedFunctionInfo::DebugName() { | 10113 String* SharedFunctionInfo::DebugName() { |
| 10569 Object* n = name(); | 10114 Object* n = name(); |
| 10570 if (!n->IsString() || String::cast(n)->length() == 0) return inferred_name(); | 10115 if (!n->IsString() || String::cast(n)->length() == 0) return inferred_name(); |
| 10571 return String::cast(n); | 10116 return String::cast(n); |
| 10572 } | 10117 } |
| 10573 | 10118 |
| 10574 | 10119 |
| 10575 bool SharedFunctionInfo::HasSourceCode() { | 10120 bool SharedFunctionInfo::HasSourceCode() { |
| 10576 return !script()->IsUndefined() && | 10121 return !script()->IsUndefined() && |
| 10577 !reinterpret_cast<Script*>(script())->source()->IsUndefined(); | 10122 !reinterpret_cast<Script*>(script())->source()->IsUndefined(); |
| (...skipping 3307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 13885 result_internal->VerifyApiCallResultType(); | 13430 result_internal->VerifyApiCallResultType(); |
| 13886 // Rebox handle before return. | 13431 // Rebox handle before return. |
| 13887 return handle(*result_internal, isolate); | 13432 return handle(*result_internal, isolate); |
| 13888 } | 13433 } |
| 13889 } | 13434 } |
| 13890 | 13435 |
| 13891 return GetPropertyPostInterceptor(object, receiver, name, attributes); | 13436 return GetPropertyPostInterceptor(object, receiver, name, attributes); |
| 13892 } | 13437 } |
| 13893 | 13438 |
| 13894 | 13439 |
| 13895 // Compute the property keys from the interceptor. | |
| 13896 // TODO(rossberg): support symbols in API, and filter here if needed. | |
| 13897 MaybeHandle<JSArray> JSObject::GetKeysForNamedInterceptor( | |
| 13898 Handle<JSObject> object, Handle<JSReceiver> receiver) { | |
| 13899 Isolate* isolate = receiver->GetIsolate(); | |
| 13900 Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor()); | |
| 13901 PropertyCallbackArguments | |
| 13902 args(isolate, interceptor->data(), *receiver, *object); | |
| 13903 v8::Handle<v8::Array> result; | |
| 13904 if (!interceptor->enumerator()->IsUndefined()) { | |
| 13905 v8::NamedPropertyEnumeratorCallback enum_fun = | |
| 13906 v8::ToCData<v8::NamedPropertyEnumeratorCallback>( | |
| 13907 interceptor->enumerator()); | |
| 13908 LOG(isolate, ApiObjectAccess("interceptor-named-enum", *object)); | |
| 13909 result = args.Call(enum_fun); | |
| 13910 } | |
| 13911 if (result.IsEmpty()) return MaybeHandle<JSArray>(); | |
| 13912 #if ENABLE_EXTRA_CHECKS | |
| 13913 CHECK(v8::Utils::OpenHandle(*result)->IsJSObject()); | |
| 13914 #endif | |
| 13915 // Rebox before returning. | |
| 13916 return handle(*v8::Utils::OpenHandle(*result), isolate); | |
| 13917 } | |
| 13918 | |
| 13919 | |
| 13920 // Compute the element keys from the interceptor. | |
| 13921 MaybeHandle<JSArray> JSObject::GetKeysForIndexedInterceptor( | |
| 13922 Handle<JSObject> object, Handle<JSReceiver> receiver) { | |
| 13923 Isolate* isolate = receiver->GetIsolate(); | |
| 13924 Handle<InterceptorInfo> interceptor(object->GetIndexedInterceptor()); | |
| 13925 PropertyCallbackArguments | |
| 13926 args(isolate, interceptor->data(), *receiver, *object); | |
| 13927 v8::Handle<v8::Array> result; | |
| 13928 if (!interceptor->enumerator()->IsUndefined()) { | |
| 13929 v8::IndexedPropertyEnumeratorCallback enum_fun = | |
| 13930 v8::ToCData<v8::IndexedPropertyEnumeratorCallback>( | |
| 13931 interceptor->enumerator()); | |
| 13932 LOG(isolate, ApiObjectAccess("interceptor-indexed-enum", *object)); | |
| 13933 result = args.Call(enum_fun); | |
| 13934 } | |
| 13935 if (result.IsEmpty()) return MaybeHandle<JSArray>(); | |
| 13936 #if ENABLE_EXTRA_CHECKS | |
| 13937 CHECK(v8::Utils::OpenHandle(*result)->IsJSObject()); | |
| 13938 #endif | |
| 13939 // Rebox before returning. | |
| 13940 return handle(*v8::Utils::OpenHandle(*result), isolate); | |
| 13941 } | |
| 13942 | |
| 13943 | |
| 13944 bool JSObject::HasRealNamedProperty(Handle<JSObject> object, | 13440 bool JSObject::HasRealNamedProperty(Handle<JSObject> object, |
| 13945 Handle<Name> key) { | 13441 Handle<Name> key) { |
| 13946 Isolate* isolate = object->GetIsolate(); | 13442 Isolate* isolate = object->GetIsolate(); |
| 13947 SealHandleScope shs(isolate); | 13443 SealHandleScope shs(isolate); |
| 13948 // Check access rights if needed. | 13444 // Check access rights if needed. |
| 13949 if (object->IsAccessCheckNeeded()) { | 13445 if (object->IsAccessCheckNeeded()) { |
| 13950 if (!isolate->MayNamedAccess(object, key, v8::ACCESS_HAS)) { | 13446 if (!isolate->MayNamedAccess(object, key, v8::ACCESS_HAS)) { |
| 13951 isolate->ReportFailedAccessCheck(object, v8::ACCESS_HAS); | 13447 isolate->ReportFailedAccessCheck(object, v8::ACCESS_HAS); |
| 13952 // TODO(yangguo): Issue 3269, check for scheduled exception missing? | 13448 // TODO(yangguo): Issue 3269, check for scheduled exception missing? |
| 13953 return false; | 13449 return false; |
| (...skipping 3143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 17097 #define ERROR_MESSAGES_TEXTS(C, T) T, | 16593 #define ERROR_MESSAGES_TEXTS(C, T) T, |
| 17098 static const char* error_messages_[] = { | 16594 static const char* error_messages_[] = { |
| 17099 ERROR_MESSAGES_LIST(ERROR_MESSAGES_TEXTS) | 16595 ERROR_MESSAGES_LIST(ERROR_MESSAGES_TEXTS) |
| 17100 }; | 16596 }; |
| 17101 #undef ERROR_MESSAGES_TEXTS | 16597 #undef ERROR_MESSAGES_TEXTS |
| 17102 return error_messages_[reason]; | 16598 return error_messages_[reason]; |
| 17103 } | 16599 } |
| 17104 | 16600 |
| 17105 | 16601 |
| 17106 } } // namespace v8::internal | 16602 } } // namespace v8::internal |
| OLD | NEW |