Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | |
| 30 | #include "v8.h" |
| 31 | |
| 32 | #include "accessors.h" |
| 33 | #include "api.h" |
| 34 | #include "arguments.h" |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 35 | #include "bootstrapper.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 36 | #include "codegen.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 37 | #include "compilation-cache.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 38 | #include "compiler.h" |
| 39 | #include "cpu.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 40 | #include "dateparser-inl.h" |
| 41 | #include "debug.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 42 | #include "deoptimizer.h" |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 43 | #include "date.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | #include "execution.h" |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 45 | #include "global-handles.h" |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 46 | #include "isolate-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 47 | #include "jsregexp.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 48 | #include "json-parser.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 49 | #include "liveedit.h" |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 50 | #include "liveobjectlist-inl.h" |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 51 | #include "misc-intrinsics.h" |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 52 | #include "parser.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 53 | #include "platform.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 54 | #include "runtime-profiler.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 55 | #include "runtime.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 56 | #include "scopeinfo.h" |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 57 | #include "smart-array-pointer.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 58 | #include "string-search.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 59 | #include "stub-cache.h" |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 60 | #include "v8threads.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 61 | #include "vm-state-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 62 | |
| 63 | namespace v8 { |
| 64 | namespace internal { |
| 65 | |
| 66 | |
| 67 | #define RUNTIME_ASSERT(value) \ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 68 | if (!(value)) return isolate->ThrowIllegalOperation(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 69 | |
| 70 | // Cast the given object to a value of the specified type and store |
| 71 | // it in a variable with the given name. If the object is not of the |
| 72 | // expected type call IllegalOperation and return. |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 73 | #define CONVERT_ARG_CHECKED(Type, name, index) \ |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 74 | RUNTIME_ASSERT(args[index]->Is##Type()); \ |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 75 | Type* name = Type::cast(args[index]); |
| 76 | |
| 77 | #define CONVERT_ARG_HANDLE_CHECKED(Type, name, index) \ |
| 78 | RUNTIME_ASSERT(args[index]->Is##Type()); \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 79 | Handle<Type> name = args.at<Type>(index); |
| 80 | |
| 81 | // Cast the given object to a boolean and store it in a variable with |
| 82 | // the given name. If the object is not a boolean call IllegalOperation |
| 83 | // and return. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 84 | #define CONVERT_BOOLEAN_ARG_CHECKED(name, index) \ |
| 85 | RUNTIME_ASSERT(args[index]->IsBoolean()); \ |
| 86 | bool name = args[index]->IsTrue(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 87 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 88 | // Cast the given argument to a Smi and store its value in an int variable |
| 89 | // with the given name. If the argument is not a Smi call IllegalOperation |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 90 | // and return. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 91 | #define CONVERT_SMI_ARG_CHECKED(name, index) \ |
| 92 | RUNTIME_ASSERT(args[index]->IsSmi()); \ |
| 93 | int name = args.smi_at(index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 94 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 95 | // Cast the given argument to a double and store it in a variable with |
| 96 | // the given name. If the argument is not a number (as opposed to |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 97 | // the number not-a-number) call IllegalOperation and return. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 98 | #define CONVERT_DOUBLE_ARG_CHECKED(name, index) \ |
| 99 | RUNTIME_ASSERT(args[index]->IsNumber()); \ |
| 100 | double name = args.number_at(index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 101 | |
| 102 | // Call the specified converter on the object *comand store the result in |
| 103 | // a variable of the specified type with the given name. If the |
| 104 | // object is not a Number call IllegalOperation and return. |
| 105 | #define CONVERT_NUMBER_CHECKED(type, name, Type, obj) \ |
| 106 | RUNTIME_ASSERT(obj->IsNumber()); \ |
| 107 | type name = NumberTo##Type(obj); |
| 108 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 109 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 110 | // Cast the given argument to PropertyDetails and store its value in a |
| 111 | // variable with the given name. If the argument is not a Smi call |
| 112 | // IllegalOperation and return. |
| 113 | #define CONVERT_PROPERTY_DETAILS_CHECKED(name, index) \ |
| 114 | RUNTIME_ASSERT(args[index]->IsSmi()); \ |
| 115 | PropertyDetails name = PropertyDetails(Smi::cast(args[index])); |
| 116 | |
| 117 | |
| 118 | // Assert that the given argument has a valid value for a StrictModeFlag |
| 119 | // and store it in a StrictModeFlag variable with the given name. |
| 120 | #define CONVERT_STRICT_MODE_ARG_CHECKED(name, index) \ |
| 121 | RUNTIME_ASSERT(args[index]->IsSmi()); \ |
| 122 | RUNTIME_ASSERT(args.smi_at(index) == kStrictMode || \ |
| 123 | args.smi_at(index) == kNonStrictMode); \ |
| 124 | StrictModeFlag name = \ |
| 125 | static_cast<StrictModeFlag>(args.smi_at(index)); |
| 126 | |
| 127 | |
| 128 | // Assert that the given argument has a valid value for a LanguageMode |
| 129 | // and store it in a LanguageMode variable with the given name. |
| 130 | #define CONVERT_LANGUAGE_MODE_ARG(name, index) \ |
| 131 | ASSERT(args[index]->IsSmi()); \ |
| 132 | ASSERT(args.smi_at(index) == CLASSIC_MODE || \ |
| 133 | args.smi_at(index) == STRICT_MODE || \ |
| 134 | args.smi_at(index) == EXTENDED_MODE); \ |
| 135 | LanguageMode name = \ |
| 136 | static_cast<LanguageMode>(args.smi_at(index)); |
| 137 | |
| 138 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 139 | MUST_USE_RESULT static MaybeObject* DeepCopyBoilerplate(Isolate* isolate, |
| 140 | JSObject* boilerplate) { |
| 141 | StackLimitCheck check(isolate); |
| 142 | if (check.HasOverflowed()) return isolate->StackOverflow(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 143 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 144 | Heap* heap = isolate->heap(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 145 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 146 | { MaybeObject* maybe_result = heap->CopyJSObject(boilerplate); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 147 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 148 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 149 | JSObject* copy = JSObject::cast(result); |
| 150 | |
| 151 | // Deep copy local properties. |
| 152 | if (copy->HasFastProperties()) { |
| 153 | FixedArray* properties = copy->properties(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 154 | for (int i = 0; i < properties->length(); i++) { |
| 155 | Object* value = properties->get(i); |
| 156 | if (value->IsJSObject()) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 157 | JSObject* js_object = JSObject::cast(value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 158 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 159 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 160 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 161 | properties->set(i, result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 164 | int nof = copy->map()->inobject_properties(); |
| 165 | for (int i = 0; i < nof; i++) { |
| 166 | Object* value = copy->InObjectPropertyAt(i); |
| 167 | if (value->IsJSObject()) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 168 | JSObject* js_object = JSObject::cast(value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 169 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 170 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 171 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 172 | copy->InObjectPropertyAtPut(i, result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 176 | { MaybeObject* maybe_result = |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 177 | heap->AllocateFixedArray(copy->NumberOfLocalProperties()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 178 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 179 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 180 | FixedArray* names = FixedArray::cast(result); |
| 181 | copy->GetLocalPropertyNames(names, 0); |
| 182 | for (int i = 0; i < names->length(); i++) { |
| 183 | ASSERT(names->get(i)->IsString()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 184 | String* key_string = String::cast(names->get(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 185 | PropertyAttributes attributes = |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 186 | copy->GetLocalPropertyAttribute(key_string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 187 | // Only deep copy fields from the object literal expression. |
| 188 | // In particular, don't try to copy the length attribute of |
| 189 | // an array. |
| 190 | if (attributes != NONE) continue; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 191 | Object* value = |
| 192 | copy->GetProperty(key_string, &attributes)->ToObjectUnchecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 193 | if (value->IsJSObject()) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 194 | JSObject* js_object = JSObject::cast(value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 195 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 196 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 197 | } |
| 198 | { MaybeObject* maybe_result = |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 199 | // Creating object copy for literals. No strict mode needed. |
| 200 | copy->SetProperty(key_string, result, NONE, kNonStrictMode); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 201 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 202 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // Deep copy local elements. |
| 208 | // Pixel elements cannot be created using an object literal. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 209 | ASSERT(!copy->HasExternalArrayElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 210 | switch (copy->GetElementsKind()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 211 | case FAST_SMI_ONLY_ELEMENTS: |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 212 | case FAST_ELEMENTS: { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 213 | FixedArray* elements = FixedArray::cast(copy->elements()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 214 | if (elements->map() == heap->fixed_cow_array_map()) { |
| 215 | isolate->counters()->cow_arrays_created_runtime()->Increment(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 216 | #ifdef DEBUG |
| 217 | for (int i = 0; i < elements->length(); i++) { |
| 218 | ASSERT(!elements->get(i)->IsJSObject()); |
| 219 | } |
| 220 | #endif |
| 221 | } else { |
| 222 | for (int i = 0; i < elements->length(); i++) { |
| 223 | Object* value = elements->get(i); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 224 | ASSERT(value->IsSmi() || |
| 225 | value->IsTheHole() || |
| 226 | (copy->GetElementsKind() == FAST_ELEMENTS)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 227 | if (value->IsJSObject()) { |
| 228 | JSObject* js_object = JSObject::cast(value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 229 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, |
| 230 | js_object); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 231 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 232 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 233 | elements->set(i, result); |
| 234 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | break; |
| 238 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 239 | case DICTIONARY_ELEMENTS: { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 240 | SeededNumberDictionary* element_dictionary = copy->element_dictionary(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 241 | int capacity = element_dictionary->Capacity(); |
| 242 | for (int i = 0; i < capacity; i++) { |
| 243 | Object* k = element_dictionary->KeyAt(i); |
| 244 | if (element_dictionary->IsKey(k)) { |
| 245 | Object* value = element_dictionary->ValueAt(i); |
| 246 | if (value->IsJSObject()) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 247 | JSObject* js_object = JSObject::cast(value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 248 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, |
| 249 | js_object); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 250 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 251 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 252 | element_dictionary->ValueAtPut(i, result); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | break; |
| 257 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 258 | case NON_STRICT_ARGUMENTS_ELEMENTS: |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 259 | UNIMPLEMENTED(); |
| 260 | break; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 261 | case EXTERNAL_PIXEL_ELEMENTS: |
| 262 | case EXTERNAL_BYTE_ELEMENTS: |
| 263 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 264 | case EXTERNAL_SHORT_ELEMENTS: |
| 265 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 266 | case EXTERNAL_INT_ELEMENTS: |
| 267 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 268 | case EXTERNAL_FLOAT_ELEMENTS: |
| 269 | case EXTERNAL_DOUBLE_ELEMENTS: |
| 270 | case FAST_DOUBLE_ELEMENTS: |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 271 | // No contained objects, nothing to do. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 272 | break; |
| 273 | } |
| 274 | return copy; |
| 275 | } |
| 276 | |
| 277 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 278 | static Handle<Map> ComputeObjectLiteralMap( |
| 279 | Handle<Context> context, |
| 280 | Handle<FixedArray> constant_properties, |
| 281 | bool* is_result_from_cache) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 282 | Isolate* isolate = context->GetIsolate(); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 283 | int properties_length = constant_properties->length(); |
| 284 | int number_of_properties = properties_length / 2; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 285 | // Check that there are only symbols and array indices among keys. |
| 286 | int number_of_symbol_keys = 0; |
| 287 | for (int p = 0; p != properties_length; p += 2) { |
| 288 | Object* key = constant_properties->get(p); |
| 289 | uint32_t element_index = 0; |
| 290 | if (key->IsSymbol()) { |
| 291 | number_of_symbol_keys++; |
| 292 | } else if (key->ToArrayIndex(&element_index)) { |
| 293 | // An index key does not require space in the property backing store. |
| 294 | number_of_properties--; |
| 295 | } else { |
| 296 | // Bail out as a non-symbol non-index key makes caching impossible. |
| 297 | // ASSERT to make sure that the if condition after the loop is false. |
| 298 | ASSERT(number_of_symbol_keys != number_of_properties); |
| 299 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 300 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 301 | } |
| 302 | // If we only have symbols and array indices among keys then we can |
| 303 | // use the map cache in the global context. |
| 304 | const int kMaxKeys = 10; |
| 305 | if ((number_of_symbol_keys == number_of_properties) && |
| 306 | (number_of_symbol_keys < kMaxKeys)) { |
| 307 | // Create the fixed array with the key. |
| 308 | Handle<FixedArray> keys = |
| 309 | isolate->factory()->NewFixedArray(number_of_symbol_keys); |
| 310 | if (number_of_symbol_keys > 0) { |
| 311 | int index = 0; |
| 312 | for (int p = 0; p < properties_length; p += 2) { |
| 313 | Object* key = constant_properties->get(p); |
| 314 | if (key->IsSymbol()) { |
| 315 | keys->set(index++, key); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 316 | } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 317 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 318 | ASSERT(index == number_of_symbol_keys); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 319 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 320 | *is_result_from_cache = true; |
| 321 | return isolate->factory()->ObjectLiteralMapFromCache(context, keys); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 322 | } |
| 323 | *is_result_from_cache = false; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 324 | return isolate->factory()->CopyMap( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 325 | Handle<Map>(context->object_function()->initial_map()), |
| 326 | number_of_properties); |
| 327 | } |
| 328 | |
| 329 | |
| 330 | static Handle<Object> CreateLiteralBoilerplate( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 331 | Isolate* isolate, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 332 | Handle<FixedArray> literals, |
| 333 | Handle<FixedArray> constant_properties); |
| 334 | |
| 335 | |
| 336 | static Handle<Object> CreateObjectLiteralBoilerplate( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 337 | Isolate* isolate, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 338 | Handle<FixedArray> literals, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 339 | Handle<FixedArray> constant_properties, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 340 | bool should_have_fast_elements, |
| 341 | bool has_function_literal) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 342 | // Get the global context from the literals array. This is the |
| 343 | // context in which the function was created and we use the object |
| 344 | // function from this context to create the object literal. We do |
| 345 | // not use the object function from the current global context |
| 346 | // because this might be the object function from another context |
| 347 | // which we should not have access to. |
| 348 | Handle<Context> context = |
| 349 | Handle<Context>(JSFunction::GlobalContextFromLiterals(*literals)); |
| 350 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 351 | // In case we have function literals, we want the object to be in |
| 352 | // slow properties mode for now. We don't go in the map cache because |
| 353 | // maps with constant functions can't be shared if the functions are |
| 354 | // not the same (which is the common case). |
| 355 | bool is_result_from_cache = false; |
| 356 | Handle<Map> map = has_function_literal |
| 357 | ? Handle<Map>(context->object_function()->initial_map()) |
| 358 | : ComputeObjectLiteralMap(context, |
| 359 | constant_properties, |
| 360 | &is_result_from_cache); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 361 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 362 | Handle<JSObject> boilerplate = isolate->factory()->NewJSObjectFromMap(map); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 363 | |
| 364 | // Normalize the elements of the boilerplate to save space if needed. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 365 | if (!should_have_fast_elements) JSObject::NormalizeElements(boilerplate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 366 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 367 | // Add the constant properties to the boilerplate. |
| 368 | int length = constant_properties->length(); |
| 369 | bool should_transform = |
| 370 | !is_result_from_cache && boilerplate->HasFastProperties(); |
| 371 | if (should_transform || has_function_literal) { |
| 372 | // Normalize the properties of object to avoid n^2 behavior |
| 373 | // when extending the object multiple properties. Indicate the number of |
| 374 | // properties to be added. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 375 | JSObject::NormalizeProperties( |
| 376 | boilerplate, KEEP_INOBJECT_PROPERTIES, length / 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | for (int index = 0; index < length; index +=2) { |
| 380 | Handle<Object> key(constant_properties->get(index+0), isolate); |
| 381 | Handle<Object> value(constant_properties->get(index+1), isolate); |
| 382 | if (value->IsFixedArray()) { |
| 383 | // The value contains the constant_properties of a |
| 384 | // simple object or array literal. |
| 385 | Handle<FixedArray> array = Handle<FixedArray>::cast(value); |
| 386 | value = CreateLiteralBoilerplate(isolate, literals, array); |
| 387 | if (value.is_null()) return value; |
| 388 | } |
| 389 | Handle<Object> result; |
| 390 | uint32_t element_index = 0; |
| 391 | if (key->IsSymbol()) { |
| 392 | if (Handle<String>::cast(key)->AsArrayIndex(&element_index)) { |
| 393 | // Array index as string (uint32). |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 394 | result = JSObject::SetOwnElement( |
| 395 | boilerplate, element_index, value, kNonStrictMode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 396 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 397 | Handle<String> name(String::cast(*key)); |
| 398 | ASSERT(!name->AsArrayIndex(&element_index)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 399 | result = JSObject::SetLocalPropertyIgnoreAttributes( |
| 400 | boilerplate, name, value, NONE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 401 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 402 | } else if (key->ToArrayIndex(&element_index)) { |
| 403 | // Array index (uint32). |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 404 | result = JSObject::SetOwnElement( |
| 405 | boilerplate, element_index, value, kNonStrictMode); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 406 | } else { |
| 407 | // Non-uint32 number. |
| 408 | ASSERT(key->IsNumber()); |
| 409 | double num = key->Number(); |
| 410 | char arr[100]; |
| 411 | Vector<char> buffer(arr, ARRAY_SIZE(arr)); |
| 412 | const char* str = DoubleToCString(num, buffer); |
| 413 | Handle<String> name = |
| 414 | isolate->factory()->NewStringFromAscii(CStrVector(str)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 415 | result = JSObject::SetLocalPropertyIgnoreAttributes( |
| 416 | boilerplate, name, value, NONE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 417 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 418 | // If setting the property on the boilerplate throws an |
| 419 | // exception, the exception is converted to an empty handle in |
| 420 | // the handle based operations. In that case, we need to |
| 421 | // convert back to an exception. |
| 422 | if (result.is_null()) return result; |
| 423 | } |
| 424 | |
| 425 | // Transform to fast properties if necessary. For object literals with |
| 426 | // containing function literals we defer this operation until after all |
| 427 | // computed properties have been assigned so that we can generate |
| 428 | // constant function properties. |
| 429 | if (should_transform && !has_function_literal) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 430 | JSObject::TransformToFastProperties( |
| 431 | boilerplate, boilerplate->map()->unused_property_fields()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | return boilerplate; |
| 435 | } |
| 436 | |
| 437 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 438 | MaybeObject* TransitionElements(Handle<Object> object, |
| 439 | ElementsKind to_kind, |
| 440 | Isolate* isolate) { |
| 441 | HandleScope scope(isolate); |
| 442 | if (!object->IsJSObject()) return isolate->ThrowIllegalOperation(); |
| 443 | ElementsKind from_kind = |
| 444 | Handle<JSObject>::cast(object)->map()->elements_kind(); |
| 445 | if (Map::IsValidElementsTransition(from_kind, to_kind)) { |
| 446 | Handle<Object> result = JSObject::TransitionElementsKind( |
| 447 | Handle<JSObject>::cast(object), to_kind); |
| 448 | if (result.is_null()) return isolate->ThrowIllegalOperation(); |
| 449 | return *result; |
| 450 | } |
| 451 | return isolate->ThrowIllegalOperation(); |
| 452 | } |
| 453 | |
| 454 | |
| 455 | static const int kSmiOnlyLiteralMinimumLength = 1024; |
| 456 | |
| 457 | |
| 458 | Handle<Object> Runtime::CreateArrayLiteralBoilerplate( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 459 | Isolate* isolate, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 460 | Handle<FixedArray> literals, |
| 461 | Handle<FixedArray> elements) { |
| 462 | // Create the JSArray. |
| 463 | Handle<JSFunction> constructor( |
| 464 | JSFunction::GlobalContextFromLiterals(*literals)->array_function()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 465 | Handle<JSArray> object = |
| 466 | Handle<JSArray>::cast(isolate->factory()->NewJSObject(constructor)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 467 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 468 | ElementsKind constant_elements_kind = |
| 469 | static_cast<ElementsKind>(Smi::cast(elements->get(0))->value()); |
| 470 | Handle<FixedArrayBase> constant_elements_values( |
| 471 | FixedArrayBase::cast(elements->get(1))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 472 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 473 | Context* global_context = isolate->context()->global_context(); |
| 474 | if (constant_elements_kind == FAST_SMI_ONLY_ELEMENTS) { |
| 475 | object->set_map(Map::cast(global_context->smi_js_array_map())); |
| 476 | } else if (constant_elements_kind == FAST_DOUBLE_ELEMENTS) { |
| 477 | object->set_map(Map::cast(global_context->double_js_array_map())); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 478 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 479 | object->set_map(Map::cast(global_context->object_js_array_map())); |
| 480 | } |
| 481 | |
| 482 | Handle<FixedArrayBase> copied_elements_values; |
| 483 | if (constant_elements_kind == FAST_DOUBLE_ELEMENTS) { |
| 484 | ASSERT(FLAG_smi_only_arrays); |
| 485 | copied_elements_values = isolate->factory()->CopyFixedDoubleArray( |
| 486 | Handle<FixedDoubleArray>::cast(constant_elements_values)); |
| 487 | } else { |
| 488 | ASSERT(constant_elements_kind == FAST_SMI_ONLY_ELEMENTS || |
| 489 | constant_elements_kind == FAST_ELEMENTS); |
| 490 | const bool is_cow = |
| 491 | (constant_elements_values->map() == |
| 492 | isolate->heap()->fixed_cow_array_map()); |
| 493 | if (is_cow) { |
| 494 | copied_elements_values = constant_elements_values; |
| 495 | #if DEBUG |
| 496 | Handle<FixedArray> fixed_array_values = |
| 497 | Handle<FixedArray>::cast(copied_elements_values); |
| 498 | for (int i = 0; i < fixed_array_values->length(); i++) { |
| 499 | ASSERT(!fixed_array_values->get(i)->IsFixedArray()); |
| 500 | } |
| 501 | #endif |
| 502 | } else { |
| 503 | Handle<FixedArray> fixed_array_values = |
| 504 | Handle<FixedArray>::cast(constant_elements_values); |
| 505 | Handle<FixedArray> fixed_array_values_copy = |
| 506 | isolate->factory()->CopyFixedArray(fixed_array_values); |
| 507 | copied_elements_values = fixed_array_values_copy; |
| 508 | for (int i = 0; i < fixed_array_values->length(); i++) { |
| 509 | Object* current = fixed_array_values->get(i); |
| 510 | if (current->IsFixedArray()) { |
| 511 | // The value contains the constant_properties of a |
| 512 | // simple object or array literal. |
| 513 | Handle<FixedArray> fa(FixedArray::cast(fixed_array_values->get(i))); |
| 514 | Handle<Object> result = |
| 515 | CreateLiteralBoilerplate(isolate, literals, fa); |
| 516 | if (result.is_null()) return result; |
| 517 | fixed_array_values_copy->set(i, *result); |
| 518 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 519 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 522 | object->set_elements(*copied_elements_values); |
| 523 | object->set_length(Smi::FromInt(copied_elements_values->length())); |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 524 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 525 | // Ensure that the boilerplate object has FAST_ELEMENTS, unless the flag is |
| 526 | // on or the object is larger than the threshold. |
| 527 | if (!FLAG_smi_only_arrays && |
| 528 | constant_elements_values->length() < kSmiOnlyLiteralMinimumLength) { |
| 529 | if (object->GetElementsKind() != FAST_ELEMENTS) { |
| 530 | CHECK(!TransitionElements(object, FAST_ELEMENTS, isolate)->IsFailure()); |
| 531 | } |
| 532 | } |
| 533 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 534 | return object; |
| 535 | } |
| 536 | |
| 537 | |
| 538 | static Handle<Object> CreateLiteralBoilerplate( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 539 | Isolate* isolate, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 540 | Handle<FixedArray> literals, |
| 541 | Handle<FixedArray> array) { |
| 542 | Handle<FixedArray> elements = CompileTimeValue::GetElements(array); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 543 | const bool kHasNoFunctionLiteral = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 544 | switch (CompileTimeValue::GetType(array)) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 545 | case CompileTimeValue::OBJECT_LITERAL_FAST_ELEMENTS: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 546 | return CreateObjectLiteralBoilerplate(isolate, |
| 547 | literals, |
| 548 | elements, |
| 549 | true, |
| 550 | kHasNoFunctionLiteral); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 551 | case CompileTimeValue::OBJECT_LITERAL_SLOW_ELEMENTS: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 552 | return CreateObjectLiteralBoilerplate(isolate, |
| 553 | literals, |
| 554 | elements, |
| 555 | false, |
| 556 | kHasNoFunctionLiteral); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 557 | case CompileTimeValue::ARRAY_LITERAL: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 558 | return Runtime::CreateArrayLiteralBoilerplate( |
| 559 | isolate, literals, elements); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 560 | default: |
| 561 | UNREACHABLE(); |
| 562 | return Handle<Object>::null(); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 567 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateObjectLiteral) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 568 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 569 | ASSERT(args.length() == 4); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 570 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, literals, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 571 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 572 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, constant_properties, 2); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 573 | CONVERT_SMI_ARG_CHECKED(flags, 3); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 574 | bool should_have_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; |
| 575 | bool has_function_literal = (flags & ObjectLiteral::kHasFunction) != 0; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 576 | |
| 577 | // Check if boilerplate exists. If not, create it first. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 578 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 579 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 580 | boilerplate = CreateObjectLiteralBoilerplate(isolate, |
| 581 | literals, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 582 | constant_properties, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 583 | should_have_fast_elements, |
| 584 | has_function_literal); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 585 | if (boilerplate.is_null()) return Failure::Exception(); |
| 586 | // Update the functions literal and return the boilerplate. |
| 587 | literals->set(literals_index, *boilerplate); |
| 588 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 589 | return DeepCopyBoilerplate(isolate, JSObject::cast(*boilerplate)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 593 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateObjectLiteralShallow) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 594 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 595 | ASSERT(args.length() == 4); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 596 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, literals, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 597 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 598 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, constant_properties, 2); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 599 | CONVERT_SMI_ARG_CHECKED(flags, 3); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 600 | bool should_have_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; |
| 601 | bool has_function_literal = (flags & ObjectLiteral::kHasFunction) != 0; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 602 | |
| 603 | // Check if boilerplate exists. If not, create it first. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 604 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 605 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 606 | boilerplate = CreateObjectLiteralBoilerplate(isolate, |
| 607 | literals, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 608 | constant_properties, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 609 | should_have_fast_elements, |
| 610 | has_function_literal); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 611 | if (boilerplate.is_null()) return Failure::Exception(); |
| 612 | // Update the functions literal and return the boilerplate. |
| 613 | literals->set(literals_index, *boilerplate); |
| 614 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 615 | return isolate->heap()->CopyJSObject(JSObject::cast(*boilerplate)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 619 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateArrayLiteral) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 620 | HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 621 | ASSERT(args.length() == 3); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 622 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, literals, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 623 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 624 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, elements, 2); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 625 | |
| 626 | // Check if boilerplate exists. If not, create it first. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 627 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 628 | if (*boilerplate == isolate->heap()->undefined_value()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 629 | boilerplate = |
| 630 | Runtime::CreateArrayLiteralBoilerplate(isolate, literals, elements); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 631 | if (boilerplate.is_null()) return Failure::Exception(); |
| 632 | // Update the functions literal and return the boilerplate. |
| 633 | literals->set(literals_index, *boilerplate); |
| 634 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 635 | return DeepCopyBoilerplate(isolate, JSObject::cast(*boilerplate)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 639 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateArrayLiteralShallow) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 640 | HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 641 | ASSERT(args.length() == 3); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 642 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, literals, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 643 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 644 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, elements, 2); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 645 | |
| 646 | // Check if boilerplate exists. If not, create it first. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 647 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 648 | if (*boilerplate == isolate->heap()->undefined_value()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 649 | ASSERT(*elements != isolate->heap()->empty_fixed_array()); |
| 650 | boilerplate = |
| 651 | Runtime::CreateArrayLiteralBoilerplate(isolate, literals, elements); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 652 | if (boilerplate.is_null()) return Failure::Exception(); |
| 653 | // Update the functions literal and return the boilerplate. |
| 654 | literals->set(literals_index, *boilerplate); |
| 655 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 656 | if (JSObject::cast(*boilerplate)->elements()->map() == |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 657 | isolate->heap()->fixed_cow_array_map()) { |
| 658 | isolate->counters()->cow_arrays_created_runtime()->Increment(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 659 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 660 | return isolate->heap()->CopyJSObject(JSObject::cast(*boilerplate)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 664 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateJSProxy) { |
| 665 | ASSERT(args.length() == 2); |
| 666 | Object* handler = args[0]; |
| 667 | Object* prototype = args[1]; |
| 668 | Object* used_prototype = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 669 | prototype->IsJSReceiver() ? prototype : isolate->heap()->null_value(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 670 | return isolate->heap()->AllocateJSProxy(handler, used_prototype); |
| 671 | } |
| 672 | |
| 673 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 674 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateJSFunctionProxy) { |
| 675 | ASSERT(args.length() == 4); |
| 676 | Object* handler = args[0]; |
| 677 | Object* call_trap = args[1]; |
| 678 | Object* construct_trap = args[2]; |
| 679 | Object* prototype = args[3]; |
| 680 | Object* used_prototype = |
| 681 | prototype->IsJSReceiver() ? prototype : isolate->heap()->null_value(); |
| 682 | return isolate->heap()->AllocateJSFunctionProxy( |
| 683 | handler, call_trap, construct_trap, used_prototype); |
| 684 | } |
| 685 | |
| 686 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 687 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsJSProxy) { |
| 688 | ASSERT(args.length() == 1); |
| 689 | Object* obj = args[0]; |
| 690 | return isolate->heap()->ToBoolean(obj->IsJSProxy()); |
| 691 | } |
| 692 | |
| 693 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 694 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsJSFunctionProxy) { |
| 695 | ASSERT(args.length() == 1); |
| 696 | Object* obj = args[0]; |
| 697 | return isolate->heap()->ToBoolean(obj->IsJSFunctionProxy()); |
| 698 | } |
| 699 | |
| 700 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 701 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetHandler) { |
| 702 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 703 | CONVERT_ARG_CHECKED(JSProxy, proxy, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 704 | return proxy->handler(); |
| 705 | } |
| 706 | |
| 707 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 708 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetCallTrap) { |
| 709 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 710 | CONVERT_ARG_CHECKED(JSFunctionProxy, proxy, 0); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 711 | return proxy->call_trap(); |
| 712 | } |
| 713 | |
| 714 | |
| 715 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetConstructTrap) { |
| 716 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 717 | CONVERT_ARG_CHECKED(JSFunctionProxy, proxy, 0); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 718 | return proxy->construct_trap(); |
| 719 | } |
| 720 | |
| 721 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 722 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Fix) { |
| 723 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 724 | CONVERT_ARG_CHECKED(JSProxy, proxy, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 725 | proxy->Fix(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 726 | return isolate->heap()->undefined_value(); |
| 727 | } |
| 728 | |
| 729 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 730 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetInitialize) { |
| 731 | HandleScope scope(isolate); |
| 732 | ASSERT(args.length() == 1); |
| 733 | CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); |
| 734 | Handle<ObjectHashSet> table = isolate->factory()->NewObjectHashSet(0); |
| 735 | holder->set_table(*table); |
| 736 | return *holder; |
| 737 | } |
| 738 | |
| 739 | |
| 740 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetAdd) { |
| 741 | HandleScope scope(isolate); |
| 742 | ASSERT(args.length() == 2); |
| 743 | CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); |
| 744 | Handle<Object> key(args[1]); |
| 745 | Handle<ObjectHashSet> table(ObjectHashSet::cast(holder->table())); |
| 746 | table = ObjectHashSetAdd(table, key); |
| 747 | holder->set_table(*table); |
| 748 | return isolate->heap()->undefined_symbol(); |
| 749 | } |
| 750 | |
| 751 | |
| 752 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetHas) { |
| 753 | HandleScope scope(isolate); |
| 754 | ASSERT(args.length() == 2); |
| 755 | CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); |
| 756 | Handle<Object> key(args[1]); |
| 757 | Handle<ObjectHashSet> table(ObjectHashSet::cast(holder->table())); |
| 758 | return isolate->heap()->ToBoolean(table->Contains(*key)); |
| 759 | } |
| 760 | |
| 761 | |
| 762 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetDelete) { |
| 763 | HandleScope scope(isolate); |
| 764 | ASSERT(args.length() == 2); |
| 765 | CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); |
| 766 | Handle<Object> key(args[1]); |
| 767 | Handle<ObjectHashSet> table(ObjectHashSet::cast(holder->table())); |
| 768 | table = ObjectHashSetRemove(table, key); |
| 769 | holder->set_table(*table); |
| 770 | return isolate->heap()->undefined_symbol(); |
| 771 | } |
| 772 | |
| 773 | |
| 774 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MapInitialize) { |
| 775 | HandleScope scope(isolate); |
| 776 | ASSERT(args.length() == 1); |
| 777 | CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0); |
| 778 | Handle<ObjectHashTable> table = isolate->factory()->NewObjectHashTable(0); |
| 779 | holder->set_table(*table); |
| 780 | return *holder; |
| 781 | } |
| 782 | |
| 783 | |
| 784 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MapGet) { |
| 785 | HandleScope scope(isolate); |
| 786 | ASSERT(args.length() == 2); |
| 787 | CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0); |
| 788 | Handle<Object> key(args[1]); |
| 789 | return ObjectHashTable::cast(holder->table())->Lookup(*key); |
| 790 | } |
| 791 | |
| 792 | |
| 793 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MapSet) { |
| 794 | HandleScope scope(isolate); |
| 795 | ASSERT(args.length() == 3); |
| 796 | CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0); |
| 797 | Handle<Object> key(args[1]); |
| 798 | Handle<Object> value(args[2]); |
| 799 | Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table())); |
| 800 | Handle<ObjectHashTable> new_table = PutIntoObjectHashTable(table, key, value); |
| 801 | holder->set_table(*new_table); |
| 802 | return *value; |
| 803 | } |
| 804 | |
| 805 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 806 | RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapInitialize) { |
| 807 | HandleScope scope(isolate); |
| 808 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 809 | CONVERT_ARG_HANDLE_CHECKED(JSWeakMap, weakmap, 0); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 810 | ASSERT(weakmap->map()->inobject_properties() == 0); |
| 811 | Handle<ObjectHashTable> table = isolate->factory()->NewObjectHashTable(0); |
| 812 | weakmap->set_table(*table); |
| 813 | weakmap->set_next(Smi::FromInt(0)); |
| 814 | return *weakmap; |
| 815 | } |
| 816 | |
| 817 | |
| 818 | RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapGet) { |
| 819 | NoHandleAllocation ha; |
| 820 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 821 | CONVERT_ARG_HANDLE_CHECKED(JSWeakMap, weakmap, 0); |
| 822 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, key, 1); |
| 823 | return ObjectHashTable::cast(weakmap->table())->Lookup(*key); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | |
| 827 | RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapSet) { |
| 828 | HandleScope scope(isolate); |
| 829 | ASSERT(args.length() == 3); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 830 | CONVERT_ARG_HANDLE_CHECKED(JSWeakMap, weakmap, 0); |
| 831 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, key, 1); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 832 | Handle<Object> value(args[2]); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 833 | Handle<ObjectHashTable> table(ObjectHashTable::cast(weakmap->table())); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 834 | Handle<ObjectHashTable> new_table = PutIntoObjectHashTable(table, key, value); |
| 835 | weakmap->set_table(*new_table); |
| 836 | return *value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 840 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ClassOf) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 841 | NoHandleAllocation ha; |
| 842 | ASSERT(args.length() == 1); |
| 843 | Object* obj = args[0]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 844 | if (!obj->IsJSObject()) return isolate->heap()->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 845 | return JSObject::cast(obj)->class_name(); |
| 846 | } |
| 847 | |
| 848 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 849 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetPrototype) { |
| 850 | NoHandleAllocation ha; |
| 851 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 852 | CONVERT_ARG_CHECKED(JSReceiver, input_obj, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 853 | Object* obj = input_obj; |
| 854 | // We don't expect access checks to be needed on JSProxy objects. |
| 855 | ASSERT(!obj->IsAccessCheckNeeded() || obj->IsJSObject()); |
| 856 | do { |
| 857 | if (obj->IsAccessCheckNeeded() && |
| 858 | !isolate->MayNamedAccess(JSObject::cast(obj), |
| 859 | isolate->heap()->Proto_symbol(), |
| 860 | v8::ACCESS_GET)) { |
| 861 | isolate->ReportFailedAccessCheck(JSObject::cast(obj), v8::ACCESS_GET); |
| 862 | return isolate->heap()->undefined_value(); |
| 863 | } |
| 864 | obj = obj->GetPrototype(); |
| 865 | } while (obj->IsJSObject() && |
| 866 | JSObject::cast(obj)->map()->is_hidden_prototype()); |
| 867 | return obj; |
| 868 | } |
| 869 | |
| 870 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 871 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsInPrototypeChain) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 872 | NoHandleAllocation ha; |
| 873 | ASSERT(args.length() == 2); |
| 874 | // See ECMA-262, section 15.3.5.3, page 88 (steps 5 - 8). |
| 875 | Object* O = args[0]; |
| 876 | Object* V = args[1]; |
| 877 | while (true) { |
| 878 | Object* prototype = V->GetPrototype(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 879 | if (prototype->IsNull()) return isolate->heap()->false_value(); |
| 880 | if (O == prototype) return isolate->heap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 881 | V = prototype; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 886 | // Recursively traverses hidden prototypes if property is not found |
| 887 | static void GetOwnPropertyImplementation(JSObject* obj, |
| 888 | String* name, |
| 889 | LookupResult* result) { |
| 890 | obj->LocalLookupRealNamedProperty(name, result); |
| 891 | |
| 892 | if (!result->IsProperty()) { |
| 893 | Object* proto = obj->GetPrototype(); |
| 894 | if (proto->IsJSObject() && |
| 895 | JSObject::cast(proto)->map()->is_hidden_prototype()) |
| 896 | GetOwnPropertyImplementation(JSObject::cast(proto), |
| 897 | name, result); |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 902 | static bool CheckAccessException(LookupResult* result, |
| 903 | v8::AccessType access_type) { |
| 904 | if (result->type() == CALLBACKS) { |
| 905 | Object* callback = result->GetCallbackObject(); |
| 906 | if (callback->IsAccessorInfo()) { |
| 907 | AccessorInfo* info = AccessorInfo::cast(callback); |
| 908 | bool can_access = |
| 909 | (access_type == v8::ACCESS_HAS && |
| 910 | (info->all_can_read() || info->all_can_write())) || |
| 911 | (access_type == v8::ACCESS_GET && info->all_can_read()) || |
| 912 | (access_type == v8::ACCESS_SET && info->all_can_write()); |
| 913 | return can_access; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | return false; |
| 918 | } |
| 919 | |
| 920 | |
| 921 | static bool CheckAccess(JSObject* obj, |
| 922 | String* name, |
| 923 | LookupResult* result, |
| 924 | v8::AccessType access_type) { |
| 925 | ASSERT(result->IsProperty()); |
| 926 | |
| 927 | JSObject* holder = result->holder(); |
| 928 | JSObject* current = obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 929 | Isolate* isolate = obj->GetIsolate(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 930 | while (true) { |
| 931 | if (current->IsAccessCheckNeeded() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 932 | !isolate->MayNamedAccess(current, name, access_type)) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 933 | // Access check callback denied the access, but some properties |
| 934 | // can have a special permissions which override callbacks descision |
| 935 | // (currently see v8::AccessControl). |
| 936 | break; |
| 937 | } |
| 938 | |
| 939 | if (current == holder) { |
| 940 | return true; |
| 941 | } |
| 942 | |
| 943 | current = JSObject::cast(current->GetPrototype()); |
| 944 | } |
| 945 | |
| 946 | // API callbacks can have per callback access exceptions. |
| 947 | switch (result->type()) { |
| 948 | case CALLBACKS: { |
| 949 | if (CheckAccessException(result, access_type)) { |
| 950 | return true; |
| 951 | } |
| 952 | break; |
| 953 | } |
| 954 | case INTERCEPTOR: { |
| 955 | // If the object has an interceptor, try real named properties. |
| 956 | // Overwrite the result to fetch the correct property later. |
| 957 | holder->LookupRealNamedProperty(name, result); |
| 958 | if (result->IsProperty()) { |
| 959 | if (CheckAccessException(result, access_type)) { |
| 960 | return true; |
| 961 | } |
| 962 | } |
| 963 | break; |
| 964 | } |
| 965 | default: |
| 966 | break; |
| 967 | } |
| 968 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 969 | isolate->ReportFailedAccessCheck(current, access_type); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 970 | return false; |
| 971 | } |
| 972 | |
| 973 | |
| 974 | // TODO(1095): we should traverse hidden prototype hierachy as well. |
| 975 | static bool CheckElementAccess(JSObject* obj, |
| 976 | uint32_t index, |
| 977 | v8::AccessType access_type) { |
| 978 | if (obj->IsAccessCheckNeeded() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 979 | !obj->GetIsolate()->MayIndexedAccess(obj, index, access_type)) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 980 | return false; |
| 981 | } |
| 982 | |
| 983 | return true; |
| 984 | } |
| 985 | |
| 986 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 987 | // Enumerator used as indices into the array returned from GetOwnProperty |
| 988 | enum PropertyDescriptorIndices { |
| 989 | IS_ACCESSOR_INDEX, |
| 990 | VALUE_INDEX, |
| 991 | GETTER_INDEX, |
| 992 | SETTER_INDEX, |
| 993 | WRITABLE_INDEX, |
| 994 | ENUMERABLE_INDEX, |
| 995 | CONFIGURABLE_INDEX, |
| 996 | DESCRIPTOR_SIZE |
| 997 | }; |
| 998 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 999 | |
| 1000 | static MaybeObject* GetOwnProperty(Isolate* isolate, |
| 1001 | Handle<JSObject> obj, |
| 1002 | Handle<String> name) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1003 | Heap* heap = isolate->heap(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1004 | Handle<FixedArray> elms = isolate->factory()->NewFixedArray(DESCRIPTOR_SIZE); |
| 1005 | Handle<JSArray> desc = isolate->factory()->NewJSArrayWithElements(elms); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1006 | LookupResult result(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1007 | // This could be an element. |
| 1008 | uint32_t index; |
| 1009 | if (name->AsArrayIndex(&index)) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1010 | switch (obj->HasLocalElement(index)) { |
| 1011 | case JSObject::UNDEFINED_ELEMENT: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1012 | return heap->undefined_value(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1013 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1014 | case JSObject::STRING_CHARACTER_ELEMENT: { |
| 1015 | // Special handling of string objects according to ECMAScript 5 |
| 1016 | // 15.5.5.2. Note that this might be a string object with elements |
| 1017 | // other than the actual string value. This is covered by the |
| 1018 | // subsequent cases. |
| 1019 | Handle<JSValue> js_value = Handle<JSValue>::cast(obj); |
| 1020 | Handle<String> str(String::cast(js_value->value())); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1021 | Handle<String> substr = SubString(str, index, index + 1, NOT_TENURED); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1022 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1023 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1024 | elms->set(VALUE_INDEX, *substr); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1025 | elms->set(WRITABLE_INDEX, heap->false_value()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1026 | elms->set(ENUMERABLE_INDEX, heap->true_value()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1027 | elms->set(CONFIGURABLE_INDEX, heap->false_value()); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1028 | return *desc; |
| 1029 | } |
| 1030 | |
| 1031 | case JSObject::INTERCEPTED_ELEMENT: |
| 1032 | case JSObject::FAST_ELEMENT: { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1033 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1034 | Handle<Object> value = Object::GetElement(obj, index); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1035 | RETURN_IF_EMPTY_HANDLE(isolate, value); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1036 | elms->set(VALUE_INDEX, *value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1037 | elms->set(WRITABLE_INDEX, heap->true_value()); |
| 1038 | elms->set(ENUMERABLE_INDEX, heap->true_value()); |
| 1039 | elms->set(CONFIGURABLE_INDEX, heap->true_value()); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1040 | return *desc; |
| 1041 | } |
| 1042 | |
| 1043 | case JSObject::DICTIONARY_ELEMENT: { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1044 | Handle<JSObject> holder = obj; |
| 1045 | if (obj->IsJSGlobalProxy()) { |
| 1046 | Object* proto = obj->GetPrototype(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1047 | if (proto->IsNull()) return heap->undefined_value(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1048 | ASSERT(proto->IsJSGlobalObject()); |
| 1049 | holder = Handle<JSObject>(JSObject::cast(proto)); |
| 1050 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1051 | FixedArray* elements = FixedArray::cast(holder->elements()); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 1052 | SeededNumberDictionary* dictionary = NULL; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1053 | if (elements->map() == heap->non_strict_arguments_elements_map()) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 1054 | dictionary = SeededNumberDictionary::cast(elements->get(1)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1055 | } else { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 1056 | dictionary = SeededNumberDictionary::cast(elements); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1057 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1058 | int entry = dictionary->FindEntry(index); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 1059 | ASSERT(entry != SeededNumberDictionary::kNotFound); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1060 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 1061 | switch (details.type()) { |
| 1062 | case CALLBACKS: { |
| 1063 | // This is an accessor property with getter and/or setter. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1064 | AccessorPair* accessors = |
| 1065 | AccessorPair::cast(dictionary->ValueAt(entry)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1066 | elms->set(IS_ACCESSOR_INDEX, heap->true_value()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1067 | if (CheckElementAccess(*obj, index, v8::ACCESS_GET)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1068 | elms->set(GETTER_INDEX, accessors->GetComponent(ACCESSOR_GETTER)); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1069 | } |
| 1070 | if (CheckElementAccess(*obj, index, v8::ACCESS_SET)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1071 | elms->set(SETTER_INDEX, accessors->GetComponent(ACCESSOR_SETTER)); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1072 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1073 | break; |
| 1074 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1075 | case NORMAL: { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1076 | // This is a data property. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1077 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1078 | Handle<Object> value = Object::GetElement(obj, index); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1079 | ASSERT(!value.is_null()); |
| 1080 | elms->set(VALUE_INDEX, *value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1081 | elms->set(WRITABLE_INDEX, heap->ToBoolean(!details.IsReadOnly())); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1082 | break; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1083 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1084 | default: |
| 1085 | UNREACHABLE(); |
| 1086 | break; |
| 1087 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1088 | elms->set(ENUMERABLE_INDEX, heap->ToBoolean(!details.IsDontEnum())); |
| 1089 | elms->set(CONFIGURABLE_INDEX, heap->ToBoolean(!details.IsDontDelete())); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1090 | return *desc; |
| 1091 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1095 | // Use recursive implementation to also traverse hidden prototypes |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1096 | GetOwnPropertyImplementation(*obj, *name, &result); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1097 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1098 | if (!result.IsProperty()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1099 | return heap->undefined_value(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1100 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1101 | |
| 1102 | if (!CheckAccess(*obj, *name, &result, v8::ACCESS_HAS)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1103 | return heap->false_value(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1106 | elms->set(ENUMERABLE_INDEX, heap->ToBoolean(!result.IsDontEnum())); |
| 1107 | elms->set(CONFIGURABLE_INDEX, heap->ToBoolean(!result.IsDontDelete())); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1108 | |
| 1109 | bool is_js_accessor = (result.type() == CALLBACKS) && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1110 | (result.GetCallbackObject()->IsAccessorPair()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1111 | |
| 1112 | if (is_js_accessor) { |
| 1113 | // __defineGetter__/__defineSetter__ callback. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1114 | elms->set(IS_ACCESSOR_INDEX, heap->true_value()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1115 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1116 | AccessorPair* accessors = AccessorPair::cast(result.GetCallbackObject()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1117 | if (CheckAccess(*obj, *name, &result, v8::ACCESS_GET)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1118 | elms->set(GETTER_INDEX, accessors->GetComponent(ACCESSOR_GETTER)); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1119 | } |
| 1120 | if (CheckAccess(*obj, *name, &result, v8::ACCESS_SET)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1121 | elms->set(SETTER_INDEX, accessors->GetComponent(ACCESSOR_SETTER)); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1122 | } |
| 1123 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1124 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
| 1125 | elms->set(WRITABLE_INDEX, heap->ToBoolean(!result.IsReadOnly())); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1126 | |
| 1127 | PropertyAttributes attrs; |
| 1128 | Object* value; |
| 1129 | // GetProperty will check access and report any violations. |
| 1130 | { MaybeObject* maybe_value = obj->GetProperty(*obj, &result, *name, &attrs); |
| 1131 | if (!maybe_value->ToObject(&value)) return maybe_value; |
| 1132 | } |
| 1133 | elms->set(VALUE_INDEX, value); |
| 1134 | } |
| 1135 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1136 | return *desc; |
| 1137 | } |
| 1138 | |
| 1139 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1140 | // Returns an array with the property description: |
| 1141 | // if args[1] is not a property on args[0] |
| 1142 | // returns undefined |
| 1143 | // if args[1] is a data property on args[0] |
| 1144 | // [false, value, Writeable, Enumerable, Configurable] |
| 1145 | // if args[1] is an accessor on args[0] |
| 1146 | // [true, GetFunction, SetFunction, Enumerable, Configurable] |
| 1147 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetOwnProperty) { |
| 1148 | ASSERT(args.length() == 2); |
| 1149 | HandleScope scope(isolate); |
| 1150 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
| 1151 | CONVERT_ARG_HANDLE_CHECKED(String, name, 1); |
| 1152 | return GetOwnProperty(isolate, obj, name); |
| 1153 | } |
| 1154 | |
| 1155 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1156 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PreventExtensions) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1157 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1158 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1159 | return obj->PreventExtensions(); |
| 1160 | } |
| 1161 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1162 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1163 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsExtensible) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1164 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1165 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1166 | if (obj->IsJSGlobalProxy()) { |
| 1167 | Object* proto = obj->GetPrototype(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1168 | if (proto->IsNull()) return isolate->heap()->false_value(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1169 | ASSERT(proto->IsJSGlobalObject()); |
| 1170 | obj = JSObject::cast(proto); |
| 1171 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1172 | return isolate->heap()->ToBoolean(obj->map()->is_extensible()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1176 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpCompile) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1177 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1178 | ASSERT(args.length() == 3); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1179 | CONVERT_ARG_HANDLE_CHECKED(JSRegExp, re, 0); |
| 1180 | CONVERT_ARG_HANDLE_CHECKED(String, pattern, 1); |
| 1181 | CONVERT_ARG_HANDLE_CHECKED(String, flags, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1182 | Handle<Object> result = RegExpImpl::Compile(re, pattern, flags); |
| 1183 | if (result.is_null()) return Failure::Exception(); |
| 1184 | return *result; |
| 1185 | } |
| 1186 | |
| 1187 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1188 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateApiFunction) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1189 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1190 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1191 | CONVERT_ARG_HANDLE_CHECKED(FunctionTemplateInfo, data, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1192 | return *isolate->factory()->CreateApiFunction(data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1196 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsTemplate) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1197 | ASSERT(args.length() == 1); |
| 1198 | Object* arg = args[0]; |
| 1199 | bool result = arg->IsObjectTemplateInfo() || arg->IsFunctionTemplateInfo(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1200 | return isolate->heap()->ToBoolean(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1204 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetTemplateField) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1205 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1206 | CONVERT_ARG_CHECKED(HeapObject, templ, 0); |
| 1207 | CONVERT_SMI_ARG_CHECKED(index, 1) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1208 | int offset = index * kPointerSize + HeapObject::kHeaderSize; |
| 1209 | InstanceType type = templ->map()->instance_type(); |
| 1210 | RUNTIME_ASSERT(type == FUNCTION_TEMPLATE_INFO_TYPE || |
| 1211 | type == OBJECT_TEMPLATE_INFO_TYPE); |
| 1212 | RUNTIME_ASSERT(offset > 0); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1213 | if (type == FUNCTION_TEMPLATE_INFO_TYPE) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1214 | RUNTIME_ASSERT(offset < FunctionTemplateInfo::kSize); |
| 1215 | } else { |
| 1216 | RUNTIME_ASSERT(offset < ObjectTemplateInfo::kSize); |
| 1217 | } |
| 1218 | return *HeapObject::RawField(templ, offset); |
| 1219 | } |
| 1220 | |
| 1221 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1222 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DisableAccessChecks) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1223 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1224 | CONVERT_ARG_CHECKED(HeapObject, object, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1225 | Map* old_map = object->map(); |
| 1226 | bool needs_access_checks = old_map->is_access_check_needed(); |
| 1227 | if (needs_access_checks) { |
| 1228 | // Copy map so it won't interfere constructor's initial map. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1229 | Object* new_map; |
| 1230 | { MaybeObject* maybe_new_map = old_map->CopyDropTransitions(); |
| 1231 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 1232 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1233 | |
| 1234 | Map::cast(new_map)->set_is_access_check_needed(false); |
| 1235 | object->set_map(Map::cast(new_map)); |
| 1236 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1237 | return isolate->heap()->ToBoolean(needs_access_checks); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1241 | RUNTIME_FUNCTION(MaybeObject*, Runtime_EnableAccessChecks) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1242 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1243 | CONVERT_ARG_CHECKED(HeapObject, object, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1244 | Map* old_map = object->map(); |
| 1245 | if (!old_map->is_access_check_needed()) { |
| 1246 | // Copy map so it won't interfere constructor's initial map. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1247 | Object* new_map; |
| 1248 | { MaybeObject* maybe_new_map = old_map->CopyDropTransitions(); |
| 1249 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 1250 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1251 | |
| 1252 | Map::cast(new_map)->set_is_access_check_needed(true); |
| 1253 | object->set_map(Map::cast(new_map)); |
| 1254 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1255 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1259 | static Failure* ThrowRedeclarationError(Isolate* isolate, |
| 1260 | const char* type, |
| 1261 | Handle<String> name) { |
| 1262 | HandleScope scope(isolate); |
| 1263 | Handle<Object> type_handle = |
| 1264 | isolate->factory()->NewStringFromAscii(CStrVector(type)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1265 | Handle<Object> args[2] = { type_handle, name }; |
| 1266 | Handle<Object> error = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1267 | isolate->factory()->NewTypeError("redeclaration", HandleVector(args, 2)); |
| 1268 | return isolate->Throw(*error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1272 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeclareGlobals) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1273 | ASSERT(args.length() == 3); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1274 | HandleScope scope(isolate); |
| 1275 | Handle<GlobalObject> global = Handle<GlobalObject>( |
| 1276 | isolate->context()->global()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1277 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1278 | Handle<Context> context = args.at<Context>(0); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1279 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, pairs, 1); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1280 | CONVERT_SMI_ARG_CHECKED(flags, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1281 | |
| 1282 | // Traverse the name/value pairs and set the properties. |
| 1283 | int length = pairs->length(); |
| 1284 | for (int i = 0; i < length; i += 2) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1285 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1286 | Handle<String> name(String::cast(pairs->get(i))); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1287 | Handle<Object> value(pairs->get(i + 1), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1288 | |
| 1289 | // We have to declare a global const property. To capture we only |
| 1290 | // assign to it when evaluating the assignment for "const x = |
| 1291 | // <expr>" the initial value is the hole. |
| 1292 | bool is_const_property = value->IsTheHole(); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1293 | bool is_function_declaration = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1294 | if (value->IsUndefined() || is_const_property) { |
| 1295 | // Lookup the property in the global object, and don't set the |
| 1296 | // value of the variable if the property is already there. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1297 | LookupResult lookup(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1298 | global->Lookup(*name, &lookup); |
| 1299 | if (lookup.IsProperty()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1300 | // We found an existing property. Unless it was an interceptor |
| 1301 | // that claims the property is absent, skip this declaration. |
| 1302 | if (lookup.type() != INTERCEPTOR) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1303 | continue; |
| 1304 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1305 | PropertyAttributes attributes = global->GetPropertyAttribute(*name); |
| 1306 | if (attributes != ABSENT) { |
| 1307 | continue; |
| 1308 | } |
| 1309 | // Fall-through and introduce the absent property by using |
| 1310 | // SetProperty. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1311 | } |
| 1312 | } else { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1313 | is_function_declaration = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1314 | // Copy the function and update its context. Use it as value. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1315 | Handle<SharedFunctionInfo> shared = |
| 1316 | Handle<SharedFunctionInfo>::cast(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1317 | Handle<JSFunction> function = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1318 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 1319 | context, |
| 1320 | TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1321 | value = function; |
| 1322 | } |
| 1323 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1324 | LookupResult lookup(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1325 | global->LocalLookup(*name, &lookup); |
| 1326 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1327 | // Compute the property attributes. According to ECMA-262, section |
| 1328 | // 13, page 71, the property must be read-only and |
| 1329 | // non-deletable. However, neither SpiderMonkey nor KJS creates the |
| 1330 | // property as read-only, so we don't either. |
| 1331 | int attr = NONE; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1332 | if (!DeclareGlobalsEvalFlag::decode(flags)) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1333 | attr |= DONT_DELETE; |
| 1334 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1335 | bool is_native = DeclareGlobalsNativeFlag::decode(flags); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1336 | if (is_const_property || (is_native && is_function_declaration)) { |
| 1337 | attr |= READ_ONLY; |
| 1338 | } |
| 1339 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1340 | LanguageMode language_mode = DeclareGlobalsLanguageMode::decode(flags); |
| 1341 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1342 | // Safari does not allow the invocation of callback setters for |
| 1343 | // function declarations. To mimic this behavior, we do not allow |
| 1344 | // the invocation of setters for function values. This makes a |
| 1345 | // difference for global functions with the same names as event |
| 1346 | // handlers such as "function onload() {}". Firefox does call the |
| 1347 | // onload setter in those case and Safari does not. We follow |
| 1348 | // Safari for compatibility. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1349 | if (is_function_declaration) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1350 | if (lookup.IsProperty() && (lookup.type() != INTERCEPTOR)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1351 | // Do not overwrite READ_ONLY properties. |
| 1352 | if (lookup.GetAttributes() & READ_ONLY) { |
| 1353 | if (language_mode != CLASSIC_MODE) { |
| 1354 | Handle<Object> args[] = { name }; |
| 1355 | return isolate->Throw(*isolate->factory()->NewTypeError( |
| 1356 | "strict_cannot_assign", HandleVector(args, ARRAY_SIZE(args)))); |
| 1357 | } |
| 1358 | continue; |
| 1359 | } |
| 1360 | // Do not change DONT_DELETE to false from true. |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1361 | attr |= lookup.GetAttributes() & DONT_DELETE; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1362 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1363 | PropertyAttributes attributes = static_cast<PropertyAttributes>(attr); |
| 1364 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1365 | RETURN_IF_EMPTY_HANDLE( |
| 1366 | isolate, |
| 1367 | JSObject::SetLocalPropertyIgnoreAttributes(global, name, value, |
| 1368 | attributes)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1369 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1370 | RETURN_IF_EMPTY_HANDLE( |
| 1371 | isolate, |
| 1372 | JSReceiver::SetProperty(global, name, value, |
| 1373 | static_cast<PropertyAttributes>(attr), |
| 1374 | language_mode == CLASSIC_MODE |
| 1375 | ? kNonStrictMode : kStrictMode)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1376 | } |
| 1377 | } |
| 1378 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1379 | ASSERT(!isolate->has_pending_exception()); |
| 1380 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1384 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeclareContextSlot) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1385 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1386 | ASSERT(args.length() == 4); |
| 1387 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1388 | // Declarations are always made in a function or global context. In the |
| 1389 | // case of eval code, the context passed is the context of the caller, |
| 1390 | // which may be some nested context and not the declaration context. |
| 1391 | RUNTIME_ASSERT(args[0]->IsContext()); |
| 1392 | Handle<Context> context(Context::cast(args[0])->declaration_context()); |
| 1393 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1394 | Handle<String> name(String::cast(args[1])); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1395 | PropertyAttributes mode = static_cast<PropertyAttributes>(args.smi_at(2)); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1396 | RUNTIME_ASSERT(mode == READ_ONLY || mode == NONE); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1397 | Handle<Object> initial_value(args[3], isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1398 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1399 | int index; |
| 1400 | PropertyAttributes attributes; |
| 1401 | ContextLookupFlags flags = DONT_FOLLOW_CHAINS; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1402 | BindingFlags binding_flags; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1403 | Handle<Object> holder = |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1404 | context->Lookup(name, flags, &index, &attributes, &binding_flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1405 | |
| 1406 | if (attributes != ABSENT) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1407 | // The name was declared before; check for conflicting re-declarations. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1408 | if (((attributes & READ_ONLY) != 0) || (mode == READ_ONLY)) { |
| 1409 | // Functions are not read-only. |
| 1410 | ASSERT(mode != READ_ONLY || initial_value->IsTheHole()); |
| 1411 | const char* type = ((attributes & READ_ONLY) != 0) ? "const" : "var"; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1412 | return ThrowRedeclarationError(isolate, type, name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | // Initialize it if necessary. |
| 1416 | if (*initial_value != NULL) { |
| 1417 | if (index >= 0) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1418 | ASSERT(holder.is_identical_to(context)); |
| 1419 | if (((attributes & READ_ONLY) == 0) || |
| 1420 | context->get(index)->IsTheHole()) { |
| 1421 | context->set(index, *initial_value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1422 | } |
| 1423 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1424 | // Slow case: The property is in the context extension object of a |
| 1425 | // function context or the global object of a global context. |
| 1426 | Handle<JSObject> object = Handle<JSObject>::cast(holder); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1427 | RETURN_IF_EMPTY_HANDLE( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1428 | isolate, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1429 | JSReceiver::SetProperty(object, name, initial_value, mode, |
| 1430 | kNonStrictMode)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | } else { |
| 1435 | // The property is not in the function context. It needs to be |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1436 | // "declared" in the function context's extension context or as a |
| 1437 | // property of the the global object. |
| 1438 | Handle<JSObject> object; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1439 | if (context->has_extension()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1440 | object = Handle<JSObject>(JSObject::cast(context->extension())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1441 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1442 | // Context extension objects are allocated lazily. |
| 1443 | ASSERT(context->IsFunctionContext()); |
| 1444 | object = isolate->factory()->NewJSObject( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1445 | isolate->context_extension_function()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1446 | context->set_extension(*object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1447 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1448 | ASSERT(*object != NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1449 | |
| 1450 | // Declare the property by setting it to the initial value if provided, |
| 1451 | // or undefined, and use the correct mode (e.g. READ_ONLY attribute for |
| 1452 | // constant declarations). |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1453 | ASSERT(!object->HasLocalProperty(*name)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1454 | Handle<Object> value(isolate->heap()->undefined_value(), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1455 | if (*initial_value != NULL) value = initial_value; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1456 | // Declaring a const context slot is a conflicting declaration if |
| 1457 | // there is a callback with that name in a prototype. It is |
| 1458 | // allowed to introduce const variables in |
| 1459 | // JSContextExtensionObjects. They are treated specially in |
| 1460 | // SetProperty and no setters are invoked for those since they are |
| 1461 | // not real JSObjects. |
| 1462 | if (initial_value->IsTheHole() && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1463 | !object->IsJSContextExtensionObject()) { |
| 1464 | LookupResult lookup(isolate); |
| 1465 | object->Lookup(*name, &lookup); |
| 1466 | if (lookup.IsFound() && (lookup.type() == CALLBACKS)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1467 | return ThrowRedeclarationError(isolate, "const", name); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1468 | } |
| 1469 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1470 | RETURN_IF_EMPTY_HANDLE( |
| 1471 | isolate, |
| 1472 | JSReceiver::SetProperty(object, name, value, mode, kNonStrictMode)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1475 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1479 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeVarGlobal) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1480 | NoHandleAllocation nha; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1481 | // args[0] == name |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1482 | // args[1] == language_mode |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1483 | // args[2] == value (optional) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1484 | |
| 1485 | // Determine if we need to assign to the variable if it already |
| 1486 | // exists (based on the number of arguments). |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1487 | RUNTIME_ASSERT(args.length() == 2 || args.length() == 3); |
| 1488 | bool assign = args.length() == 3; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1489 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1490 | CONVERT_ARG_HANDLE_CHECKED(String, name, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1491 | GlobalObject* global = isolate->context()->global(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1492 | RUNTIME_ASSERT(args[1]->IsSmi()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1493 | CONVERT_LANGUAGE_MODE_ARG(language_mode, 1); |
| 1494 | StrictModeFlag strict_mode_flag = (language_mode == CLASSIC_MODE) |
| 1495 | ? kNonStrictMode : kStrictMode; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1496 | |
| 1497 | // According to ECMA-262, section 12.2, page 62, the property must |
| 1498 | // not be deletable. |
| 1499 | PropertyAttributes attributes = DONT_DELETE; |
| 1500 | |
| 1501 | // Lookup the property locally in the global object. If it isn't |
| 1502 | // there, there is a property with this name in the prototype chain. |
| 1503 | // We follow Safari and Firefox behavior and only set the property |
| 1504 | // locally if there is an explicit initialization value that we have |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1505 | // to assign to the property. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1506 | // Note that objects can have hidden prototypes, so we need to traverse |
| 1507 | // the whole chain of hidden prototypes to do a 'local' lookup. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1508 | Object* object = global; |
| 1509 | LookupResult lookup(isolate); |
| 1510 | while (object->IsJSObject() && |
| 1511 | JSObject::cast(object)->map()->is_hidden_prototype()) { |
| 1512 | JSObject* raw_holder = JSObject::cast(object); |
| 1513 | raw_holder->LocalLookup(*name, &lookup); |
| 1514 | if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { |
| 1515 | HandleScope handle_scope(isolate); |
| 1516 | Handle<JSObject> holder(raw_holder); |
| 1517 | PropertyAttributes intercepted = holder->GetPropertyAttribute(*name); |
| 1518 | // Update the raw pointer in case it's changed due to GC. |
| 1519 | raw_holder = *holder; |
| 1520 | if (intercepted != ABSENT && (intercepted & READ_ONLY) == 0) { |
| 1521 | // Found an interceptor that's not read only. |
| 1522 | if (assign) { |
| 1523 | return raw_holder->SetProperty( |
| 1524 | &lookup, *name, args[2], attributes, strict_mode_flag); |
| 1525 | } else { |
| 1526 | return isolate->heap()->undefined_value(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1527 | } |
| 1528 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1529 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1530 | object = raw_holder->GetPrototype(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1533 | // Reload global in case the loop above performed a GC. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1534 | global = isolate->context()->global(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1535 | if (assign) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1536 | return global->SetProperty(*name, args[2], attributes, strict_mode_flag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1537 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1538 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1542 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeConstGlobal) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1543 | // All constants are declared with an initial value. The name |
| 1544 | // of the constant is the first argument and the initial value |
| 1545 | // is the second. |
| 1546 | RUNTIME_ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1547 | CONVERT_ARG_HANDLE_CHECKED(String, name, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1548 | Handle<Object> value = args.at<Object>(1); |
| 1549 | |
| 1550 | // Get the current global object from top. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1551 | GlobalObject* global = isolate->context()->global(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1552 | |
| 1553 | // According to ECMA-262, section 12.2, page 62, the property must |
| 1554 | // not be deletable. Since it's a const, it must be READ_ONLY too. |
| 1555 | PropertyAttributes attributes = |
| 1556 | static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY); |
| 1557 | |
| 1558 | // Lookup the property locally in the global object. If it isn't |
| 1559 | // there, we add the property and take special precautions to always |
| 1560 | // add it as a local property even in case of callbacks in the |
| 1561 | // prototype chain (this rules out using SetProperty). |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 1562 | // We use SetLocalPropertyIgnoreAttributes instead |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1563 | LookupResult lookup(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1564 | global->LocalLookup(*name, &lookup); |
| 1565 | if (!lookup.IsProperty()) { |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 1566 | return global->SetLocalPropertyIgnoreAttributes(*name, |
| 1567 | *value, |
| 1568 | attributes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1571 | if (!lookup.IsReadOnly()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1572 | // Restore global object from context (in case of GC) and continue |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1573 | // with setting the value. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1574 | HandleScope handle_scope(isolate); |
| 1575 | Handle<GlobalObject> global(isolate->context()->global()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1576 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1577 | // BUG 1213575: Handle the case where we have to set a read-only |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1578 | // property through an interceptor and only do it if it's |
| 1579 | // uninitialized, e.g. the hole. Nirk... |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1580 | // Passing non-strict mode because the property is writable. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1581 | RETURN_IF_EMPTY_HANDLE( |
| 1582 | isolate, |
| 1583 | JSReceiver::SetProperty(global, name, value, attributes, |
| 1584 | kNonStrictMode)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1585 | return *value; |
| 1586 | } |
| 1587 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1588 | // Set the value, but only if we're assigning the initial value to a |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1589 | // constant. For now, we determine this by checking if the |
| 1590 | // current value is the hole. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1591 | // Strict mode handling not needed (const is disallowed in strict mode). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1592 | PropertyType type = lookup.type(); |
| 1593 | if (type == FIELD) { |
| 1594 | FixedArray* properties = global->properties(); |
| 1595 | int index = lookup.GetFieldIndex(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1596 | if (properties->get(index)->IsTheHole() || !lookup.IsReadOnly()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1597 | properties->set(index, *value); |
| 1598 | } |
| 1599 | } else if (type == NORMAL) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1600 | if (global->GetNormalizedProperty(&lookup)->IsTheHole() || |
| 1601 | !lookup.IsReadOnly()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1602 | global->SetNormalizedProperty(&lookup, *value); |
| 1603 | } |
| 1604 | } else { |
| 1605 | // Ignore re-initialization of constants that have already been |
| 1606 | // assigned a function value. |
| 1607 | ASSERT(lookup.IsReadOnly() && type == CONSTANT_FUNCTION); |
| 1608 | } |
| 1609 | |
| 1610 | // Use the set value as the result of the operation. |
| 1611 | return *value; |
| 1612 | } |
| 1613 | |
| 1614 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1615 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeConstContextSlot) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1616 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1617 | ASSERT(args.length() == 3); |
| 1618 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1619 | Handle<Object> value(args[0], isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1620 | ASSERT(!value->IsTheHole()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1621 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1622 | // Initializations are always done in a function or global context. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1623 | RUNTIME_ASSERT(args[1]->IsContext()); |
| 1624 | Handle<Context> context(Context::cast(args[1])->declaration_context()); |
| 1625 | |
| 1626 | Handle<String> name(String::cast(args[2])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1627 | |
| 1628 | int index; |
| 1629 | PropertyAttributes attributes; |
| 1630 | ContextLookupFlags flags = FOLLOW_CHAINS; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1631 | BindingFlags binding_flags; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1632 | Handle<Object> holder = |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1633 | context->Lookup(name, flags, &index, &attributes, &binding_flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1634 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 1635 | if (index >= 0) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1636 | ASSERT(holder->IsContext()); |
| 1637 | // Property was found in a context. Perform the assignment if we |
| 1638 | // found some non-constant or an uninitialized constant. |
| 1639 | Handle<Context> context = Handle<Context>::cast(holder); |
| 1640 | if ((attributes & READ_ONLY) == 0 || context->get(index)->IsTheHole()) { |
| 1641 | context->set(index, *value); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 1642 | } |
| 1643 | return *value; |
| 1644 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1645 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1646 | // The property could not be found, we introduce it as a property of the |
| 1647 | // global object. |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 1648 | if (attributes == ABSENT) { |
| 1649 | Handle<JSObject> global = Handle<JSObject>( |
| 1650 | isolate->context()->global()); |
| 1651 | // Strict mode not needed (const disallowed in strict mode). |
| 1652 | RETURN_IF_EMPTY_HANDLE( |
| 1653 | isolate, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1654 | JSReceiver::SetProperty(global, name, value, NONE, kNonStrictMode)); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 1655 | return *value; |
| 1656 | } |
| 1657 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1658 | // The property was present in some function's context extension object, |
| 1659 | // as a property on the subject of a with, or as a property of the global |
| 1660 | // object. |
| 1661 | // |
| 1662 | // In most situations, eval-introduced consts should still be present in |
| 1663 | // the context extension object. However, because declaration and |
| 1664 | // initialization are separate, the property might have been deleted |
| 1665 | // before we reach the initialization point. |
| 1666 | // |
| 1667 | // Example: |
| 1668 | // |
| 1669 | // function f() { eval("delete x; const x;"); } |
| 1670 | // |
| 1671 | // In that case, the initialization behaves like a normal assignment. |
| 1672 | Handle<JSObject> object = Handle<JSObject>::cast(holder); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 1673 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1674 | if (*object == context->extension()) { |
| 1675 | // This is the property that was introduced by the const declaration. |
| 1676 | // Set it if it hasn't been set before. NOTE: We cannot use |
| 1677 | // GetProperty() to get the current value as it 'unholes' the value. |
| 1678 | LookupResult lookup(isolate); |
| 1679 | object->LocalLookupRealNamedProperty(*name, &lookup); |
| 1680 | ASSERT(lookup.IsFound()); // the property was declared |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1681 | ASSERT(lookup.IsReadOnly()); // and it was declared as read-only |
| 1682 | |
| 1683 | PropertyType type = lookup.type(); |
| 1684 | if (type == FIELD) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1685 | FixedArray* properties = object->properties(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1686 | int index = lookup.GetFieldIndex(); |
| 1687 | if (properties->get(index)->IsTheHole()) { |
| 1688 | properties->set(index, *value); |
| 1689 | } |
| 1690 | } else if (type == NORMAL) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1691 | if (object->GetNormalizedProperty(&lookup)->IsTheHole()) { |
| 1692 | object->SetNormalizedProperty(&lookup, *value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1693 | } |
| 1694 | } else { |
| 1695 | // We should not reach here. Any real, named property should be |
| 1696 | // either a field or a dictionary slot. |
| 1697 | UNREACHABLE(); |
| 1698 | } |
| 1699 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1700 | // The property was found on some other object. Set it if it is not a |
| 1701 | // read-only property. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1702 | if ((attributes & READ_ONLY) == 0) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1703 | // Strict mode not needed (const disallowed in strict mode). |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1704 | RETURN_IF_EMPTY_HANDLE( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1705 | isolate, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1706 | JSReceiver::SetProperty(object, name, value, attributes, |
| 1707 | kNonStrictMode)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | return *value; |
| 1712 | } |
| 1713 | |
| 1714 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1715 | RUNTIME_FUNCTION(MaybeObject*, |
| 1716 | Runtime_OptimizeObjectForAddingMultipleProperties) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1717 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1718 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1719 | CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1720 | CONVERT_SMI_ARG_CHECKED(properties, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1721 | if (object->HasFastProperties()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1722 | JSObject::NormalizeProperties(object, KEEP_INOBJECT_PROPERTIES, properties); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1723 | } |
| 1724 | return *object; |
| 1725 | } |
| 1726 | |
| 1727 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1728 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpExec) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1729 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1730 | ASSERT(args.length() == 4); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1731 | CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 0); |
| 1732 | CONVERT_ARG_HANDLE_CHECKED(String, subject, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1733 | // Due to the way the JS calls are constructed this must be less than the |
| 1734 | // length of a string, i.e. it is always a Smi. We check anyway for security. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1735 | CONVERT_SMI_ARG_CHECKED(index, 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1736 | CONVERT_ARG_HANDLE_CHECKED(JSArray, last_match_info, 3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1737 | RUNTIME_ASSERT(last_match_info->HasFastElements()); |
| 1738 | RUNTIME_ASSERT(index >= 0); |
| 1739 | RUNTIME_ASSERT(index <= subject->length()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1740 | isolate->counters()->regexp_entry_runtime()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1741 | Handle<Object> result = RegExpImpl::Exec(regexp, |
| 1742 | subject, |
| 1743 | index, |
| 1744 | last_match_info); |
| 1745 | if (result.is_null()) return Failure::Exception(); |
| 1746 | return *result; |
| 1747 | } |
| 1748 | |
| 1749 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1750 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpConstructResult) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1751 | ASSERT(args.length() == 3); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1752 | CONVERT_SMI_ARG_CHECKED(elements_count, 0); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1753 | if (elements_count < 0 || |
| 1754 | elements_count > FixedArray::kMaxLength || |
| 1755 | !Smi::IsValid(elements_count)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1756 | return isolate->ThrowIllegalOperation(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1757 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1758 | Object* new_object; |
| 1759 | { MaybeObject* maybe_new_object = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1760 | isolate->heap()->AllocateFixedArrayWithHoles(elements_count); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1761 | if (!maybe_new_object->ToObject(&new_object)) return maybe_new_object; |
| 1762 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1763 | FixedArray* elements = FixedArray::cast(new_object); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1764 | { MaybeObject* maybe_new_object = isolate->heap()->AllocateRaw( |
| 1765 | JSRegExpResult::kSize, NEW_SPACE, OLD_POINTER_SPACE); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1766 | if (!maybe_new_object->ToObject(&new_object)) return maybe_new_object; |
| 1767 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1768 | { |
| 1769 | AssertNoAllocation no_gc; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1770 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1771 | reinterpret_cast<HeapObject*>(new_object)-> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1772 | set_map(isolate->global_context()->regexp_result_map()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1773 | } |
| 1774 | JSArray* array = JSArray::cast(new_object); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1775 | array->set_properties(isolate->heap()->empty_fixed_array()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1776 | array->set_elements(elements); |
| 1777 | array->set_length(Smi::FromInt(elements_count)); |
| 1778 | // Write in-object properties after the length of the array. |
| 1779 | array->InObjectPropertyAtPut(JSRegExpResult::kIndexIndex, args[1]); |
| 1780 | array->InObjectPropertyAtPut(JSRegExpResult::kInputIndex, args[2]); |
| 1781 | return array; |
| 1782 | } |
| 1783 | |
| 1784 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1785 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpInitializeObject) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1786 | AssertNoAllocation no_alloc; |
| 1787 | ASSERT(args.length() == 5); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1788 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 0); |
| 1789 | CONVERT_ARG_CHECKED(String, source, 1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1790 | |
| 1791 | Object* global = args[2]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1792 | if (!global->IsTrue()) global = isolate->heap()->false_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1793 | |
| 1794 | Object* ignoreCase = args[3]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1795 | if (!ignoreCase->IsTrue()) ignoreCase = isolate->heap()->false_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1796 | |
| 1797 | Object* multiline = args[4]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1798 | if (!multiline->IsTrue()) multiline = isolate->heap()->false_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1799 | |
| 1800 | Map* map = regexp->map(); |
| 1801 | Object* constructor = map->constructor(); |
| 1802 | if (constructor->IsJSFunction() && |
| 1803 | JSFunction::cast(constructor)->initial_map() == map) { |
| 1804 | // If we still have the original map, set in-object properties directly. |
| 1805 | regexp->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex, source); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1806 | // Both true and false are immovable immortal objects so no need for write |
| 1807 | // barrier. |
| 1808 | regexp->InObjectPropertyAtPut( |
| 1809 | JSRegExp::kGlobalFieldIndex, global, SKIP_WRITE_BARRIER); |
| 1810 | regexp->InObjectPropertyAtPut( |
| 1811 | JSRegExp::kIgnoreCaseFieldIndex, ignoreCase, SKIP_WRITE_BARRIER); |
| 1812 | regexp->InObjectPropertyAtPut( |
| 1813 | JSRegExp::kMultilineFieldIndex, multiline, SKIP_WRITE_BARRIER); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1814 | regexp->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex, |
| 1815 | Smi::FromInt(0), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1816 | SKIP_WRITE_BARRIER); // It's a Smi. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1817 | return regexp; |
| 1818 | } |
| 1819 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1820 | // Map has changed, so use generic, but slower, method. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1821 | PropertyAttributes final = |
| 1822 | static_cast<PropertyAttributes>(READ_ONLY | DONT_ENUM | DONT_DELETE); |
| 1823 | PropertyAttributes writable = |
| 1824 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1825 | Heap* heap = isolate->heap(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1826 | MaybeObject* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1827 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->source_symbol(), |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 1828 | source, |
| 1829 | final); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1830 | ASSERT(!result->IsFailure()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1831 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->global_symbol(), |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 1832 | global, |
| 1833 | final); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1834 | ASSERT(!result->IsFailure()); |
| 1835 | result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1836 | regexp->SetLocalPropertyIgnoreAttributes(heap->ignore_case_symbol(), |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 1837 | ignoreCase, |
| 1838 | final); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1839 | ASSERT(!result->IsFailure()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1840 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->multiline_symbol(), |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 1841 | multiline, |
| 1842 | final); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1843 | ASSERT(!result->IsFailure()); |
| 1844 | result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1845 | regexp->SetLocalPropertyIgnoreAttributes(heap->last_index_symbol(), |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 1846 | Smi::FromInt(0), |
| 1847 | writable); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1848 | ASSERT(!result->IsFailure()); |
| 1849 | USE(result); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1850 | return regexp; |
| 1851 | } |
| 1852 | |
| 1853 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1854 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FinishArrayPrototypeSetup) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1855 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1856 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1857 | CONVERT_ARG_HANDLE_CHECKED(JSArray, prototype, 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1858 | // This is necessary to enable fast checks for absence of elements |
| 1859 | // on Array.prototype and below. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1860 | prototype->set_elements(isolate->heap()->empty_fixed_array()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1861 | return Smi::FromInt(0); |
| 1862 | } |
| 1863 | |
| 1864 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1865 | static Handle<JSFunction> InstallBuiltin(Isolate* isolate, |
| 1866 | Handle<JSObject> holder, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1867 | const char* name, |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1868 | Builtins::Name builtin_name) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1869 | Handle<String> key = isolate->factory()->LookupAsciiSymbol(name); |
| 1870 | Handle<Code> code(isolate->builtins()->builtin(builtin_name)); |
| 1871 | Handle<JSFunction> optimized = |
| 1872 | isolate->factory()->NewFunction(key, |
| 1873 | JS_OBJECT_TYPE, |
| 1874 | JSObject::kHeaderSize, |
| 1875 | code, |
| 1876 | false); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1877 | optimized->shared()->DontAdaptArguments(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1878 | JSReceiver::SetProperty(holder, key, optimized, NONE, kStrictMode); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1879 | return optimized; |
| 1880 | } |
| 1881 | |
| 1882 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1883 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SpecialArrayFunctions) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1884 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1885 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1886 | CONVERT_ARG_HANDLE_CHECKED(JSObject, holder, 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1887 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1888 | InstallBuiltin(isolate, holder, "pop", Builtins::kArrayPop); |
| 1889 | InstallBuiltin(isolate, holder, "push", Builtins::kArrayPush); |
| 1890 | InstallBuiltin(isolate, holder, "shift", Builtins::kArrayShift); |
| 1891 | InstallBuiltin(isolate, holder, "unshift", Builtins::kArrayUnshift); |
| 1892 | InstallBuiltin(isolate, holder, "slice", Builtins::kArraySlice); |
| 1893 | InstallBuiltin(isolate, holder, "splice", Builtins::kArraySplice); |
| 1894 | InstallBuiltin(isolate, holder, "concat", Builtins::kArrayConcat); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1895 | |
| 1896 | return *holder; |
| 1897 | } |
| 1898 | |
| 1899 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1900 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetDefaultReceiver) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1901 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1902 | CONVERT_ARG_CHECKED(JSReceiver, callable, 0); |
| 1903 | |
| 1904 | if (!callable->IsJSFunction()) { |
| 1905 | HandleScope scope(isolate); |
| 1906 | bool threw = false; |
| 1907 | Handle<Object> delegate = |
| 1908 | Execution::TryGetFunctionDelegate(Handle<JSReceiver>(callable), &threw); |
| 1909 | if (threw) return Failure::Exception(); |
| 1910 | callable = JSFunction::cast(*delegate); |
| 1911 | } |
| 1912 | JSFunction* function = JSFunction::cast(callable); |
| 1913 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1914 | SharedFunctionInfo* shared = function->shared(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1915 | if (shared->native() || !shared->is_classic_mode()) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1916 | return isolate->heap()->undefined_value(); |
| 1917 | } |
| 1918 | // Returns undefined for strict or native functions, or |
| 1919 | // the associated global receiver for "normal" functions. |
| 1920 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1921 | Context* global_context = |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1922 | function->context()->global()->global_context(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1923 | return global_context->global()->global_receiver(); |
| 1924 | } |
| 1925 | |
| 1926 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1927 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MaterializeRegExpLiteral) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1928 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1929 | ASSERT(args.length() == 4); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1930 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, literals, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1931 | int index = args.smi_at(1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1932 | Handle<String> pattern = args.at<String>(2); |
| 1933 | Handle<String> flags = args.at<String>(3); |
| 1934 | |
| 1935 | // Get the RegExp function from the context in the literals array. |
| 1936 | // This is the RegExp function from the context in which the |
| 1937 | // function was created. We do not use the RegExp function from the |
| 1938 | // current global context because this might be the RegExp function |
| 1939 | // from another context which we should not have access to. |
| 1940 | Handle<JSFunction> constructor = |
| 1941 | Handle<JSFunction>( |
| 1942 | JSFunction::GlobalContextFromLiterals(*literals)->regexp_function()); |
| 1943 | // Compute the regular expression literal. |
| 1944 | bool has_pending_exception; |
| 1945 | Handle<Object> regexp = |
| 1946 | RegExpImpl::CreateRegExpLiteral(constructor, pattern, flags, |
| 1947 | &has_pending_exception); |
| 1948 | if (has_pending_exception) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1949 | ASSERT(isolate->has_pending_exception()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1950 | return Failure::Exception(); |
| 1951 | } |
| 1952 | literals->set(index, *regexp); |
| 1953 | return *regexp; |
| 1954 | } |
| 1955 | |
| 1956 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1957 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetName) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1958 | NoHandleAllocation ha; |
| 1959 | ASSERT(args.length() == 1); |
| 1960 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1961 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1962 | return f->shared()->name(); |
| 1963 | } |
| 1964 | |
| 1965 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1966 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetName) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1967 | NoHandleAllocation ha; |
| 1968 | ASSERT(args.length() == 2); |
| 1969 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1970 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
| 1971 | CONVERT_ARG_CHECKED(String, name, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1972 | f->shared()->set_name(name); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1973 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1977 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionNameShouldPrintAsAnonymous) { |
| 1978 | NoHandleAllocation ha; |
| 1979 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1980 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1981 | return isolate->heap()->ToBoolean( |
| 1982 | f->shared()->name_should_print_as_anonymous()); |
| 1983 | } |
| 1984 | |
| 1985 | |
| 1986 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionMarkNameShouldPrintAsAnonymous) { |
| 1987 | NoHandleAllocation ha; |
| 1988 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1989 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1990 | f->shared()->set_name_should_print_as_anonymous(true); |
| 1991 | return isolate->heap()->undefined_value(); |
| 1992 | } |
| 1993 | |
| 1994 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1995 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionRemovePrototype) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1996 | NoHandleAllocation ha; |
| 1997 | ASSERT(args.length() == 1); |
| 1998 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1999 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2000 | Object* obj = f->RemovePrototype(); |
| 2001 | if (obj->IsFailure()) return obj; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2002 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2003 | return isolate->heap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2007 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetScript) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2008 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2009 | ASSERT(args.length() == 1); |
| 2010 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2011 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2012 | Handle<Object> script = Handle<Object>(fun->shared()->script(), isolate); |
| 2013 | if (!script->IsScript()) return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2014 | |
| 2015 | return *GetScriptWrapper(Handle<Script>::cast(script)); |
| 2016 | } |
| 2017 | |
| 2018 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2019 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetSourceCode) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2020 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2021 | ASSERT(args.length() == 1); |
| 2022 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2023 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, f, 0); |
| 2024 | Handle<SharedFunctionInfo> shared(f->shared()); |
| 2025 | return *shared->GetSourceCode(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2029 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetScriptSourcePosition) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2030 | NoHandleAllocation ha; |
| 2031 | ASSERT(args.length() == 1); |
| 2032 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2033 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2034 | int pos = fun->shared()->start_position(); |
| 2035 | return Smi::FromInt(pos); |
| 2036 | } |
| 2037 | |
| 2038 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2039 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetPositionForOffset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2040 | ASSERT(args.length() == 2); |
| 2041 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2042 | CONVERT_ARG_CHECKED(Code, code, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2043 | CONVERT_NUMBER_CHECKED(int, offset, Int32, args[1]); |
| 2044 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2045 | RUNTIME_ASSERT(0 <= offset && offset < code->Size()); |
| 2046 | |
| 2047 | Address pc = code->address() + offset; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2048 | return Smi::FromInt(code->SourcePosition(pc)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2052 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetInstanceClassName) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2053 | NoHandleAllocation ha; |
| 2054 | ASSERT(args.length() == 2); |
| 2055 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2056 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
| 2057 | CONVERT_ARG_CHECKED(String, name, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2058 | fun->SetInstanceClassName(name); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2059 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2063 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetLength) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2064 | NoHandleAllocation ha; |
| 2065 | ASSERT(args.length() == 2); |
| 2066 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2067 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
| 2068 | CONVERT_SMI_ARG_CHECKED(length, 1); |
| 2069 | fun->shared()->set_length(length); |
| 2070 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2074 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetPrototype) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2075 | NoHandleAllocation ha; |
| 2076 | ASSERT(args.length() == 2); |
| 2077 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2078 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2079 | ASSERT(fun->should_have_prototype()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2080 | Object* obj; |
| 2081 | { MaybeObject* maybe_obj = |
| 2082 | Accessors::FunctionSetPrototype(fun, args[1], NULL); |
| 2083 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 2084 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2085 | return args[0]; // return TOS |
| 2086 | } |
| 2087 | |
| 2088 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2089 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetReadOnlyPrototype) { |
| 2090 | NoHandleAllocation ha; |
| 2091 | RUNTIME_ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2092 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2093 | |
| 2094 | MaybeObject* maybe_name = |
| 2095 | isolate->heap()->AllocateStringFromAscii(CStrVector("prototype")); |
| 2096 | String* name; |
| 2097 | if (!maybe_name->To(&name)) return maybe_name; |
| 2098 | |
| 2099 | if (function->HasFastProperties()) { |
| 2100 | // Construct a new field descriptor with updated attributes. |
| 2101 | DescriptorArray* instance_desc = function->map()->instance_descriptors(); |
| 2102 | int index = instance_desc->Search(name); |
| 2103 | ASSERT(index != DescriptorArray::kNotFound); |
| 2104 | PropertyDetails details(instance_desc->GetDetails(index)); |
| 2105 | CallbacksDescriptor new_desc(name, |
| 2106 | instance_desc->GetValue(index), |
| 2107 | static_cast<PropertyAttributes>(details.attributes() | READ_ONLY), |
| 2108 | details.index()); |
| 2109 | // Construct a new field descriptors array containing the new descriptor. |
| 2110 | Object* descriptors_unchecked; |
| 2111 | { MaybeObject* maybe_descriptors_unchecked = |
| 2112 | instance_desc->CopyInsert(&new_desc, REMOVE_TRANSITIONS); |
| 2113 | if (!maybe_descriptors_unchecked->ToObject(&descriptors_unchecked)) { |
| 2114 | return maybe_descriptors_unchecked; |
| 2115 | } |
| 2116 | } |
| 2117 | DescriptorArray* new_descriptors = |
| 2118 | DescriptorArray::cast(descriptors_unchecked); |
| 2119 | // Create a new map featuring the new field descriptors array. |
| 2120 | Object* map_unchecked; |
| 2121 | { MaybeObject* maybe_map_unchecked = function->map()->CopyDropDescriptors(); |
| 2122 | if (!maybe_map_unchecked->ToObject(&map_unchecked)) { |
| 2123 | return maybe_map_unchecked; |
| 2124 | } |
| 2125 | } |
| 2126 | Map* new_map = Map::cast(map_unchecked); |
| 2127 | new_map->set_instance_descriptors(new_descriptors); |
| 2128 | function->set_map(new_map); |
| 2129 | } else { // Dictionary properties. |
| 2130 | // Directly manipulate the property details. |
| 2131 | int entry = function->property_dictionary()->FindEntry(name); |
| 2132 | ASSERT(entry != StringDictionary::kNotFound); |
| 2133 | PropertyDetails details = function->property_dictionary()->DetailsAt(entry); |
| 2134 | PropertyDetails new_details( |
| 2135 | static_cast<PropertyAttributes>(details.attributes() | READ_ONLY), |
| 2136 | details.type(), |
| 2137 | details.index()); |
| 2138 | function->property_dictionary()->DetailsAtPut(entry, new_details); |
| 2139 | } |
| 2140 | return function; |
| 2141 | } |
| 2142 | |
| 2143 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2144 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionIsAPIFunction) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2145 | NoHandleAllocation ha; |
| 2146 | ASSERT(args.length() == 1); |
| 2147 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2148 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2149 | return isolate->heap()->ToBoolean(f->shared()->IsApiFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2152 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2153 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionIsBuiltin) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2154 | NoHandleAllocation ha; |
| 2155 | ASSERT(args.length() == 1); |
| 2156 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2157 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2158 | return isolate->heap()->ToBoolean(f->IsBuiltin()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2162 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetCode) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2163 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2164 | ASSERT(args.length() == 2); |
| 2165 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2166 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, target, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2167 | Handle<Object> code = args.at<Object>(1); |
| 2168 | |
| 2169 | Handle<Context> context(target->context()); |
| 2170 | |
| 2171 | if (!code->IsNull()) { |
| 2172 | RUNTIME_ASSERT(code->IsJSFunction()); |
| 2173 | Handle<JSFunction> fun = Handle<JSFunction>::cast(code); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2174 | Handle<SharedFunctionInfo> shared(fun->shared()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2175 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2176 | if (!SharedFunctionInfo::EnsureCompiled(shared, KEEP_EXCEPTION)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2177 | return Failure::Exception(); |
| 2178 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2179 | // Since we don't store the source for this we should never |
| 2180 | // optimize this. |
| 2181 | shared->code()->set_optimizable(false); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2182 | // Set the code, scope info, formal parameter count, |
| 2183 | // and the length of the target function. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2184 | target->shared()->set_code(shared->code()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2185 | target->ReplaceCode(shared->code()); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2186 | target->shared()->set_scope_info(shared->scope_info()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2187 | target->shared()->set_length(shared->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2188 | target->shared()->set_formal_parameter_count( |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2189 | shared->formal_parameter_count()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2190 | // Set the source code of the target function to undefined. |
| 2191 | // SetCode is only used for built-in constructors like String, |
| 2192 | // Array, and Object, and some web code |
| 2193 | // doesn't like seeing source code for constructors. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2194 | target->shared()->set_script(isolate->heap()->undefined_value()); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 2195 | target->shared()->code()->set_optimizable(false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2196 | // Clear the optimization hints related to the compiled code as these are no |
| 2197 | // longer valid when the code is overwritten. |
| 2198 | target->shared()->ClearThisPropertyAssignmentsInfo(); |
| 2199 | context = Handle<Context>(fun->context()); |
| 2200 | |
| 2201 | // Make sure we get a fresh copy of the literal vector to avoid |
| 2202 | // cross context contamination. |
| 2203 | int number_of_literals = fun->NumberOfLiterals(); |
| 2204 | Handle<FixedArray> literals = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2205 | isolate->factory()->NewFixedArray(number_of_literals, TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2206 | if (number_of_literals > 0) { |
| 2207 | // Insert the object, regexp and array functions in the literals |
| 2208 | // array prefix. These are the functions that will be used when |
| 2209 | // creating object, regexp and array literals. |
| 2210 | literals->set(JSFunction::kLiteralGlobalContextIndex, |
| 2211 | context->global_context()); |
| 2212 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2213 | target->set_literals(*literals); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2214 | target->set_next_function_link(isolate->heap()->undefined_value()); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2215 | |
| 2216 | if (isolate->logger()->is_logging() || CpuProfiler::is_profiling(isolate)) { |
| 2217 | isolate->logger()->LogExistingFunction( |
| 2218 | shared, Handle<Code>(shared->code())); |
| 2219 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
| 2222 | target->set_context(*context); |
| 2223 | return *target; |
| 2224 | } |
| 2225 | |
| 2226 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2227 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetExpectedNumberOfProperties) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2228 | HandleScope scope(isolate); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2229 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2230 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2231 | CONVERT_SMI_ARG_CHECKED(num, 1); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2232 | RUNTIME_ASSERT(num >= 0); |
| 2233 | SetExpectedNofProperties(function, num); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2234 | return isolate->heap()->undefined_value(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2238 | MUST_USE_RESULT static MaybeObject* CharFromCode(Isolate* isolate, |
| 2239 | Object* char_code) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2240 | uint32_t code; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2241 | if (char_code->ToArrayIndex(&code)) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2242 | if (code <= 0xffff) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2243 | return isolate->heap()->LookupSingleCharacterStringFromCode(code); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2244 | } |
| 2245 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2246 | return isolate->heap()->empty_string(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
| 2249 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2250 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringCharCodeAt) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2251 | NoHandleAllocation ha; |
| 2252 | ASSERT(args.length() == 2); |
| 2253 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2254 | CONVERT_ARG_CHECKED(String, subject, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2255 | Object* index = args[1]; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2256 | RUNTIME_ASSERT(index->IsNumber()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2257 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2258 | uint32_t i = 0; |
| 2259 | if (index->IsSmi()) { |
| 2260 | int value = Smi::cast(index)->value(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2261 | if (value < 0) return isolate->heap()->nan_value(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2262 | i = value; |
| 2263 | } else { |
| 2264 | ASSERT(index->IsHeapNumber()); |
| 2265 | double value = HeapNumber::cast(index)->value(); |
| 2266 | i = static_cast<uint32_t>(DoubleToInteger(value)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2267 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2268 | |
| 2269 | // Flatten the string. If someone wants to get a char at an index |
| 2270 | // in a cons string, it is likely that more indices will be |
| 2271 | // accessed. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2272 | Object* flat; |
| 2273 | { MaybeObject* maybe_flat = subject->TryFlatten(); |
| 2274 | if (!maybe_flat->ToObject(&flat)) return maybe_flat; |
| 2275 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2276 | subject = String::cast(flat); |
| 2277 | |
| 2278 | if (i >= static_cast<uint32_t>(subject->length())) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2279 | return isolate->heap()->nan_value(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | return Smi::FromInt(subject->Get(i)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2286 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CharFromCode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2287 | NoHandleAllocation ha; |
| 2288 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2289 | return CharFromCode(isolate, args[0]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2292 | |
| 2293 | class FixedArrayBuilder { |
| 2294 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2295 | explicit FixedArrayBuilder(Isolate* isolate, int initial_capacity) |
| 2296 | : array_(isolate->factory()->NewFixedArrayWithHoles(initial_capacity)), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2297 | length_(0), |
| 2298 | has_non_smi_elements_(false) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2299 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2300 | // extend the array will work. |
| 2301 | ASSERT(initial_capacity > 0); |
| 2302 | } |
| 2303 | |
| 2304 | explicit FixedArrayBuilder(Handle<FixedArray> backing_store) |
| 2305 | : array_(backing_store), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2306 | length_(0), |
| 2307 | has_non_smi_elements_(false) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2308 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2309 | // extend the array will work. |
| 2310 | ASSERT(backing_store->length() > 0); |
| 2311 | } |
| 2312 | |
| 2313 | bool HasCapacity(int elements) { |
| 2314 | int length = array_->length(); |
| 2315 | int required_length = length_ + elements; |
| 2316 | return (length >= required_length); |
| 2317 | } |
| 2318 | |
| 2319 | void EnsureCapacity(int elements) { |
| 2320 | int length = array_->length(); |
| 2321 | int required_length = length_ + elements; |
| 2322 | if (length < required_length) { |
| 2323 | int new_length = length; |
| 2324 | do { |
| 2325 | new_length *= 2; |
| 2326 | } while (new_length < required_length); |
| 2327 | Handle<FixedArray> extended_array = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2328 | array_->GetIsolate()->factory()->NewFixedArrayWithHoles(new_length); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2329 | array_->CopyTo(0, *extended_array, 0, length_); |
| 2330 | array_ = extended_array; |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | void Add(Object* value) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2335 | ASSERT(!value->IsSmi()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2336 | ASSERT(length_ < capacity()); |
| 2337 | array_->set(length_, value); |
| 2338 | length_++; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2339 | has_non_smi_elements_ = true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | void Add(Smi* value) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2343 | ASSERT(value->IsSmi()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2344 | ASSERT(length_ < capacity()); |
| 2345 | array_->set(length_, value); |
| 2346 | length_++; |
| 2347 | } |
| 2348 | |
| 2349 | Handle<FixedArray> array() { |
| 2350 | return array_; |
| 2351 | } |
| 2352 | |
| 2353 | int length() { |
| 2354 | return length_; |
| 2355 | } |
| 2356 | |
| 2357 | int capacity() { |
| 2358 | return array_->length(); |
| 2359 | } |
| 2360 | |
| 2361 | Handle<JSArray> ToJSArray() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2362 | Handle<JSArray> result_array = FACTORY->NewJSArrayWithElements(array_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2363 | result_array->set_length(Smi::FromInt(length_)); |
| 2364 | return result_array; |
| 2365 | } |
| 2366 | |
| 2367 | Handle<JSArray> ToJSArray(Handle<JSArray> target_array) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2368 | FACTORY->SetContent(target_array, array_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2369 | target_array->set_length(Smi::FromInt(length_)); |
| 2370 | return target_array; |
| 2371 | } |
| 2372 | |
| 2373 | private: |
| 2374 | Handle<FixedArray> array_; |
| 2375 | int length_; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2376 | bool has_non_smi_elements_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2377 | }; |
| 2378 | |
| 2379 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2380 | // Forward declarations. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2381 | const int kStringBuilderConcatHelperLengthBits = 11; |
| 2382 | const int kStringBuilderConcatHelperPositionBits = 19; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2383 | |
| 2384 | template <typename schar> |
| 2385 | static inline void StringBuilderConcatHelper(String*, |
| 2386 | schar*, |
| 2387 | FixedArray*, |
| 2388 | int); |
| 2389 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2390 | typedef BitField<int, 0, kStringBuilderConcatHelperLengthBits> |
| 2391 | StringBuilderSubstringLength; |
| 2392 | typedef BitField<int, |
| 2393 | kStringBuilderConcatHelperLengthBits, |
| 2394 | kStringBuilderConcatHelperPositionBits> |
| 2395 | StringBuilderSubstringPosition; |
| 2396 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2397 | |
| 2398 | class ReplacementStringBuilder { |
| 2399 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2400 | ReplacementStringBuilder(Heap* heap, |
| 2401 | Handle<String> subject, |
| 2402 | int estimated_part_count) |
| 2403 | : heap_(heap), |
| 2404 | array_builder_(heap->isolate(), estimated_part_count), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2405 | subject_(subject), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2406 | character_count_(0), |
| 2407 | is_ascii_(subject->IsAsciiRepresentation()) { |
| 2408 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2409 | // extend the array will work. |
| 2410 | ASSERT(estimated_part_count > 0); |
| 2411 | } |
| 2412 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2413 | static inline void AddSubjectSlice(FixedArrayBuilder* builder, |
| 2414 | int from, |
| 2415 | int to) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2416 | ASSERT(from >= 0); |
| 2417 | int length = to - from; |
| 2418 | ASSERT(length > 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2419 | if (StringBuilderSubstringLength::is_valid(length) && |
| 2420 | StringBuilderSubstringPosition::is_valid(from)) { |
| 2421 | int encoded_slice = StringBuilderSubstringLength::encode(length) | |
| 2422 | StringBuilderSubstringPosition::encode(from); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2423 | builder->Add(Smi::FromInt(encoded_slice)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2424 | } else { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2425 | // Otherwise encode as two smis. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2426 | builder->Add(Smi::FromInt(-length)); |
| 2427 | builder->Add(Smi::FromInt(from)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2428 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | |
| 2432 | void EnsureCapacity(int elements) { |
| 2433 | array_builder_.EnsureCapacity(elements); |
| 2434 | } |
| 2435 | |
| 2436 | |
| 2437 | void AddSubjectSlice(int from, int to) { |
| 2438 | AddSubjectSlice(&array_builder_, from, to); |
| 2439 | IncrementCharacterCount(to - from); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | |
| 2443 | void AddString(Handle<String> string) { |
| 2444 | int length = string->length(); |
| 2445 | ASSERT(length > 0); |
| 2446 | AddElement(*string); |
| 2447 | if (!string->IsAsciiRepresentation()) { |
| 2448 | is_ascii_ = false; |
| 2449 | } |
| 2450 | IncrementCharacterCount(length); |
| 2451 | } |
| 2452 | |
| 2453 | |
| 2454 | Handle<String> ToString() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2455 | if (array_builder_.length() == 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2456 | return heap_->isolate()->factory()->empty_string(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | Handle<String> joined_string; |
| 2460 | if (is_ascii_) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2461 | Handle<SeqAsciiString> seq = NewRawAsciiString(character_count_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2462 | AssertNoAllocation no_alloc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2463 | char* char_buffer = seq->GetChars(); |
| 2464 | StringBuilderConcatHelper(*subject_, |
| 2465 | char_buffer, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2466 | *array_builder_.array(), |
| 2467 | array_builder_.length()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2468 | joined_string = Handle<String>::cast(seq); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2469 | } else { |
| 2470 | // Non-ASCII. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2471 | Handle<SeqTwoByteString> seq = NewRawTwoByteString(character_count_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2472 | AssertNoAllocation no_alloc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2473 | uc16* char_buffer = seq->GetChars(); |
| 2474 | StringBuilderConcatHelper(*subject_, |
| 2475 | char_buffer, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2476 | *array_builder_.array(), |
| 2477 | array_builder_.length()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2478 | joined_string = Handle<String>::cast(seq); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2479 | } |
| 2480 | return joined_string; |
| 2481 | } |
| 2482 | |
| 2483 | |
| 2484 | void IncrementCharacterCount(int by) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2485 | if (character_count_ > String::kMaxLength - by) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2486 | V8::FatalProcessOutOfMemory("String.replace result too large."); |
| 2487 | } |
| 2488 | character_count_ += by; |
| 2489 | } |
| 2490 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2491 | Handle<JSArray> GetParts() { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2492 | return array_builder_.ToJSArray(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2493 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2494 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2495 | private: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2496 | Handle<SeqAsciiString> NewRawAsciiString(int length) { |
| 2497 | return heap_->isolate()->factory()->NewRawAsciiString(length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2498 | } |
| 2499 | |
| 2500 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2501 | Handle<SeqTwoByteString> NewRawTwoByteString(int length) { |
| 2502 | return heap_->isolate()->factory()->NewRawTwoByteString(length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | |
| 2506 | void AddElement(Object* element) { |
| 2507 | ASSERT(element->IsSmi() || element->IsString()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2508 | ASSERT(array_builder_.capacity() > array_builder_.length()); |
| 2509 | array_builder_.Add(element); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2510 | } |
| 2511 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2512 | Heap* heap_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2513 | FixedArrayBuilder array_builder_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2514 | Handle<String> subject_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2515 | int character_count_; |
| 2516 | bool is_ascii_; |
| 2517 | }; |
| 2518 | |
| 2519 | |
| 2520 | class CompiledReplacement { |
| 2521 | public: |
| 2522 | CompiledReplacement() |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2523 | : parts_(1), replacement_substrings_(0), simple_hint_(false) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2524 | |
| 2525 | void Compile(Handle<String> replacement, |
| 2526 | int capture_count, |
| 2527 | int subject_length); |
| 2528 | |
| 2529 | void Apply(ReplacementStringBuilder* builder, |
| 2530 | int match_from, |
| 2531 | int match_to, |
| 2532 | Handle<JSArray> last_match_info); |
| 2533 | |
| 2534 | // Number of distinct parts of the replacement pattern. |
| 2535 | int parts() { |
| 2536 | return parts_.length(); |
| 2537 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2538 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2539 | bool simple_hint() { |
| 2540 | return simple_hint_; |
| 2541 | } |
| 2542 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2543 | private: |
| 2544 | enum PartType { |
| 2545 | SUBJECT_PREFIX = 1, |
| 2546 | SUBJECT_SUFFIX, |
| 2547 | SUBJECT_CAPTURE, |
| 2548 | REPLACEMENT_SUBSTRING, |
| 2549 | REPLACEMENT_STRING, |
| 2550 | |
| 2551 | NUMBER_OF_PART_TYPES |
| 2552 | }; |
| 2553 | |
| 2554 | struct ReplacementPart { |
| 2555 | static inline ReplacementPart SubjectMatch() { |
| 2556 | return ReplacementPart(SUBJECT_CAPTURE, 0); |
| 2557 | } |
| 2558 | static inline ReplacementPart SubjectCapture(int capture_index) { |
| 2559 | return ReplacementPart(SUBJECT_CAPTURE, capture_index); |
| 2560 | } |
| 2561 | static inline ReplacementPart SubjectPrefix() { |
| 2562 | return ReplacementPart(SUBJECT_PREFIX, 0); |
| 2563 | } |
| 2564 | static inline ReplacementPart SubjectSuffix(int subject_length) { |
| 2565 | return ReplacementPart(SUBJECT_SUFFIX, subject_length); |
| 2566 | } |
| 2567 | static inline ReplacementPart ReplacementString() { |
| 2568 | return ReplacementPart(REPLACEMENT_STRING, 0); |
| 2569 | } |
| 2570 | static inline ReplacementPart ReplacementSubString(int from, int to) { |
| 2571 | ASSERT(from >= 0); |
| 2572 | ASSERT(to > from); |
| 2573 | return ReplacementPart(-from, to); |
| 2574 | } |
| 2575 | |
| 2576 | // If tag <= 0 then it is the negation of a start index of a substring of |
| 2577 | // the replacement pattern, otherwise it's a value from PartType. |
| 2578 | ReplacementPart(int tag, int data) |
| 2579 | : tag(tag), data(data) { |
| 2580 | // Must be non-positive or a PartType value. |
| 2581 | ASSERT(tag < NUMBER_OF_PART_TYPES); |
| 2582 | } |
| 2583 | // Either a value of PartType or a non-positive number that is |
| 2584 | // the negation of an index into the replacement string. |
| 2585 | int tag; |
| 2586 | // The data value's interpretation depends on the value of tag: |
| 2587 | // tag == SUBJECT_PREFIX || |
| 2588 | // tag == SUBJECT_SUFFIX: data is unused. |
| 2589 | // tag == SUBJECT_CAPTURE: data is the number of the capture. |
| 2590 | // tag == REPLACEMENT_SUBSTRING || |
| 2591 | // tag == REPLACEMENT_STRING: data is index into array of substrings |
| 2592 | // of the replacement string. |
| 2593 | // tag <= 0: Temporary representation of the substring of the replacement |
| 2594 | // string ranging over -tag .. data. |
| 2595 | // Is replaced by REPLACEMENT_{SUB,}STRING when we create the |
| 2596 | // substring objects. |
| 2597 | int data; |
| 2598 | }; |
| 2599 | |
| 2600 | template<typename Char> |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2601 | static bool ParseReplacementPattern(ZoneList<ReplacementPart>* parts, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2602 | Vector<Char> characters, |
| 2603 | int capture_count, |
| 2604 | int subject_length) { |
| 2605 | int length = characters.length(); |
| 2606 | int last = 0; |
| 2607 | for (int i = 0; i < length; i++) { |
| 2608 | Char c = characters[i]; |
| 2609 | if (c == '$') { |
| 2610 | int next_index = i + 1; |
| 2611 | if (next_index == length) { // No next character! |
| 2612 | break; |
| 2613 | } |
| 2614 | Char c2 = characters[next_index]; |
| 2615 | switch (c2) { |
| 2616 | case '$': |
| 2617 | if (i > last) { |
| 2618 | // There is a substring before. Include the first "$". |
| 2619 | parts->Add(ReplacementPart::ReplacementSubString(last, next_index)); |
| 2620 | last = next_index + 1; // Continue after the second "$". |
| 2621 | } else { |
| 2622 | // Let the next substring start with the second "$". |
| 2623 | last = next_index; |
| 2624 | } |
| 2625 | i = next_index; |
| 2626 | break; |
| 2627 | case '`': |
| 2628 | if (i > last) { |
| 2629 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2630 | } |
| 2631 | parts->Add(ReplacementPart::SubjectPrefix()); |
| 2632 | i = next_index; |
| 2633 | last = i + 1; |
| 2634 | break; |
| 2635 | case '\'': |
| 2636 | if (i > last) { |
| 2637 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2638 | } |
| 2639 | parts->Add(ReplacementPart::SubjectSuffix(subject_length)); |
| 2640 | i = next_index; |
| 2641 | last = i + 1; |
| 2642 | break; |
| 2643 | case '&': |
| 2644 | if (i > last) { |
| 2645 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2646 | } |
| 2647 | parts->Add(ReplacementPart::SubjectMatch()); |
| 2648 | i = next_index; |
| 2649 | last = i + 1; |
| 2650 | break; |
| 2651 | case '0': |
| 2652 | case '1': |
| 2653 | case '2': |
| 2654 | case '3': |
| 2655 | case '4': |
| 2656 | case '5': |
| 2657 | case '6': |
| 2658 | case '7': |
| 2659 | case '8': |
| 2660 | case '9': { |
| 2661 | int capture_ref = c2 - '0'; |
| 2662 | if (capture_ref > capture_count) { |
| 2663 | i = next_index; |
| 2664 | continue; |
| 2665 | } |
| 2666 | int second_digit_index = next_index + 1; |
| 2667 | if (second_digit_index < length) { |
| 2668 | // Peek ahead to see if we have two digits. |
| 2669 | Char c3 = characters[second_digit_index]; |
| 2670 | if ('0' <= c3 && c3 <= '9') { // Double digits. |
| 2671 | int double_digit_ref = capture_ref * 10 + c3 - '0'; |
| 2672 | if (double_digit_ref <= capture_count) { |
| 2673 | next_index = second_digit_index; |
| 2674 | capture_ref = double_digit_ref; |
| 2675 | } |
| 2676 | } |
| 2677 | } |
| 2678 | if (capture_ref > 0) { |
| 2679 | if (i > last) { |
| 2680 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2681 | } |
| 2682 | ASSERT(capture_ref <= capture_count); |
| 2683 | parts->Add(ReplacementPart::SubjectCapture(capture_ref)); |
| 2684 | last = next_index + 1; |
| 2685 | } |
| 2686 | i = next_index; |
| 2687 | break; |
| 2688 | } |
| 2689 | default: |
| 2690 | i = next_index; |
| 2691 | break; |
| 2692 | } |
| 2693 | } |
| 2694 | } |
| 2695 | if (length > last) { |
| 2696 | if (last == 0) { |
| 2697 | parts->Add(ReplacementPart::ReplacementString()); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2698 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2699 | } else { |
| 2700 | parts->Add(ReplacementPart::ReplacementSubString(last, length)); |
| 2701 | } |
| 2702 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2703 | return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
| 2706 | ZoneList<ReplacementPart> parts_; |
| 2707 | ZoneList<Handle<String> > replacement_substrings_; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2708 | bool simple_hint_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2709 | }; |
| 2710 | |
| 2711 | |
| 2712 | void CompiledReplacement::Compile(Handle<String> replacement, |
| 2713 | int capture_count, |
| 2714 | int subject_length) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2715 | { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2716 | AssertNoAllocation no_alloc; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2717 | String::FlatContent content = replacement->GetFlatContent(); |
| 2718 | ASSERT(content.IsFlat()); |
| 2719 | if (content.IsAscii()) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2720 | simple_hint_ = ParseReplacementPattern(&parts_, |
| 2721 | content.ToAsciiVector(), |
| 2722 | capture_count, |
| 2723 | subject_length); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2724 | } else { |
| 2725 | ASSERT(content.IsTwoByte()); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2726 | simple_hint_ = ParseReplacementPattern(&parts_, |
| 2727 | content.ToUC16Vector(), |
| 2728 | capture_count, |
| 2729 | subject_length); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2730 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2731 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2732 | Isolate* isolate = replacement->GetIsolate(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2733 | // Find substrings of replacement string and create them as String objects. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2734 | int substring_index = 0; |
| 2735 | for (int i = 0, n = parts_.length(); i < n; i++) { |
| 2736 | int tag = parts_[i].tag; |
| 2737 | if (tag <= 0) { // A replacement string slice. |
| 2738 | int from = -tag; |
| 2739 | int to = parts_[i].data; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2740 | replacement_substrings_.Add( |
| 2741 | isolate->factory()->NewSubString(replacement, from, to)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2742 | parts_[i].tag = REPLACEMENT_SUBSTRING; |
| 2743 | parts_[i].data = substring_index; |
| 2744 | substring_index++; |
| 2745 | } else if (tag == REPLACEMENT_STRING) { |
| 2746 | replacement_substrings_.Add(replacement); |
| 2747 | parts_[i].data = substring_index; |
| 2748 | substring_index++; |
| 2749 | } |
| 2750 | } |
| 2751 | } |
| 2752 | |
| 2753 | |
| 2754 | void CompiledReplacement::Apply(ReplacementStringBuilder* builder, |
| 2755 | int match_from, |
| 2756 | int match_to, |
| 2757 | Handle<JSArray> last_match_info) { |
| 2758 | for (int i = 0, n = parts_.length(); i < n; i++) { |
| 2759 | ReplacementPart part = parts_[i]; |
| 2760 | switch (part.tag) { |
| 2761 | case SUBJECT_PREFIX: |
| 2762 | if (match_from > 0) builder->AddSubjectSlice(0, match_from); |
| 2763 | break; |
| 2764 | case SUBJECT_SUFFIX: { |
| 2765 | int subject_length = part.data; |
| 2766 | if (match_to < subject_length) { |
| 2767 | builder->AddSubjectSlice(match_to, subject_length); |
| 2768 | } |
| 2769 | break; |
| 2770 | } |
| 2771 | case SUBJECT_CAPTURE: { |
| 2772 | int capture = part.data; |
| 2773 | FixedArray* match_info = FixedArray::cast(last_match_info->elements()); |
| 2774 | int from = RegExpImpl::GetCapture(match_info, capture * 2); |
| 2775 | int to = RegExpImpl::GetCapture(match_info, capture * 2 + 1); |
| 2776 | if (from >= 0 && to > from) { |
| 2777 | builder->AddSubjectSlice(from, to); |
| 2778 | } |
| 2779 | break; |
| 2780 | } |
| 2781 | case REPLACEMENT_SUBSTRING: |
| 2782 | case REPLACEMENT_STRING: |
| 2783 | builder->AddString(replacement_substrings_[part.data]); |
| 2784 | break; |
| 2785 | default: |
| 2786 | UNREACHABLE(); |
| 2787 | } |
| 2788 | } |
| 2789 | } |
| 2790 | |
| 2791 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2792 | void FindAsciiStringIndices(Vector<const char> subject, |
| 2793 | char pattern, |
| 2794 | ZoneList<int>* indices, |
| 2795 | unsigned int limit) { |
| 2796 | ASSERT(limit > 0); |
| 2797 | // Collect indices of pattern in subject using memchr. |
| 2798 | // Stop after finding at most limit values. |
| 2799 | const char* subject_start = reinterpret_cast<const char*>(subject.start()); |
| 2800 | const char* subject_end = subject_start + subject.length(); |
| 2801 | const char* pos = subject_start; |
| 2802 | while (limit > 0) { |
| 2803 | pos = reinterpret_cast<const char*>( |
| 2804 | memchr(pos, pattern, subject_end - pos)); |
| 2805 | if (pos == NULL) return; |
| 2806 | indices->Add(static_cast<int>(pos - subject_start)); |
| 2807 | pos++; |
| 2808 | limit--; |
| 2809 | } |
| 2810 | } |
| 2811 | |
| 2812 | |
| 2813 | template <typename SubjectChar, typename PatternChar> |
| 2814 | void FindStringIndices(Isolate* isolate, |
| 2815 | Vector<const SubjectChar> subject, |
| 2816 | Vector<const PatternChar> pattern, |
| 2817 | ZoneList<int>* indices, |
| 2818 | unsigned int limit) { |
| 2819 | ASSERT(limit > 0); |
| 2820 | // Collect indices of pattern in subject. |
| 2821 | // Stop after finding at most limit values. |
| 2822 | int pattern_length = pattern.length(); |
| 2823 | int index = 0; |
| 2824 | StringSearch<PatternChar, SubjectChar> search(isolate, pattern); |
| 2825 | while (limit > 0) { |
| 2826 | index = search.Search(subject, index); |
| 2827 | if (index < 0) return; |
| 2828 | indices->Add(index); |
| 2829 | index += pattern_length; |
| 2830 | limit--; |
| 2831 | } |
| 2832 | } |
| 2833 | |
| 2834 | |
| 2835 | void FindStringIndicesDispatch(Isolate* isolate, |
| 2836 | String* subject, |
| 2837 | String* pattern, |
| 2838 | ZoneList<int>* indices, |
| 2839 | unsigned int limit) { |
| 2840 | { |
| 2841 | AssertNoAllocation no_gc; |
| 2842 | String::FlatContent subject_content = subject->GetFlatContent(); |
| 2843 | String::FlatContent pattern_content = pattern->GetFlatContent(); |
| 2844 | ASSERT(subject_content.IsFlat()); |
| 2845 | ASSERT(pattern_content.IsFlat()); |
| 2846 | if (subject_content.IsAscii()) { |
| 2847 | Vector<const char> subject_vector = subject_content.ToAsciiVector(); |
| 2848 | if (pattern_content.IsAscii()) { |
| 2849 | Vector<const char> pattern_vector = pattern_content.ToAsciiVector(); |
| 2850 | if (pattern_vector.length() == 1) { |
| 2851 | FindAsciiStringIndices(subject_vector, |
| 2852 | pattern_vector[0], |
| 2853 | indices, |
| 2854 | limit); |
| 2855 | } else { |
| 2856 | FindStringIndices(isolate, |
| 2857 | subject_vector, |
| 2858 | pattern_vector, |
| 2859 | indices, |
| 2860 | limit); |
| 2861 | } |
| 2862 | } else { |
| 2863 | FindStringIndices(isolate, |
| 2864 | subject_vector, |
| 2865 | pattern_content.ToUC16Vector(), |
| 2866 | indices, |
| 2867 | limit); |
| 2868 | } |
| 2869 | } else { |
| 2870 | Vector<const uc16> subject_vector = subject_content.ToUC16Vector(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2871 | if (pattern_content.IsAscii()) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2872 | FindStringIndices(isolate, |
| 2873 | subject_vector, |
| 2874 | pattern_content.ToAsciiVector(), |
| 2875 | indices, |
| 2876 | limit); |
| 2877 | } else { |
| 2878 | FindStringIndices(isolate, |
| 2879 | subject_vector, |
| 2880 | pattern_content.ToUC16Vector(), |
| 2881 | indices, |
| 2882 | limit); |
| 2883 | } |
| 2884 | } |
| 2885 | } |
| 2886 | } |
| 2887 | |
| 2888 | |
| 2889 | template<typename ResultSeqString> |
| 2890 | MUST_USE_RESULT static MaybeObject* StringReplaceStringWithString( |
| 2891 | Isolate* isolate, |
| 2892 | Handle<String> subject, |
| 2893 | Handle<JSRegExp> pattern_regexp, |
| 2894 | Handle<String> replacement) { |
| 2895 | ASSERT(subject->IsFlat()); |
| 2896 | ASSERT(replacement->IsFlat()); |
| 2897 | |
| 2898 | ZoneScope zone_space(isolate, DELETE_ON_EXIT); |
| 2899 | ZoneList<int> indices(8); |
| 2900 | ASSERT_EQ(JSRegExp::ATOM, pattern_regexp->TypeTag()); |
| 2901 | String* pattern = |
| 2902 | String::cast(pattern_regexp->DataAt(JSRegExp::kAtomPatternIndex)); |
| 2903 | int subject_len = subject->length(); |
| 2904 | int pattern_len = pattern->length(); |
| 2905 | int replacement_len = replacement->length(); |
| 2906 | |
| 2907 | FindStringIndicesDispatch(isolate, *subject, pattern, &indices, 0xffffffff); |
| 2908 | |
| 2909 | int matches = indices.length(); |
| 2910 | if (matches == 0) return *subject; |
| 2911 | |
| 2912 | int result_len = (replacement_len - pattern_len) * matches + subject_len; |
| 2913 | int subject_pos = 0; |
| 2914 | int result_pos = 0; |
| 2915 | |
| 2916 | Handle<ResultSeqString> result; |
| 2917 | if (ResultSeqString::kHasAsciiEncoding) { |
| 2918 | result = Handle<ResultSeqString>::cast( |
| 2919 | isolate->factory()->NewRawAsciiString(result_len)); |
| 2920 | } else { |
| 2921 | result = Handle<ResultSeqString>::cast( |
| 2922 | isolate->factory()->NewRawTwoByteString(result_len)); |
| 2923 | } |
| 2924 | |
| 2925 | for (int i = 0; i < matches; i++) { |
| 2926 | // Copy non-matched subject content. |
| 2927 | if (subject_pos < indices.at(i)) { |
| 2928 | String::WriteToFlat(*subject, |
| 2929 | result->GetChars() + result_pos, |
| 2930 | subject_pos, |
| 2931 | indices.at(i)); |
| 2932 | result_pos += indices.at(i) - subject_pos; |
| 2933 | } |
| 2934 | |
| 2935 | // Replace match. |
| 2936 | if (replacement_len > 0) { |
| 2937 | String::WriteToFlat(*replacement, |
| 2938 | result->GetChars() + result_pos, |
| 2939 | 0, |
| 2940 | replacement_len); |
| 2941 | result_pos += replacement_len; |
| 2942 | } |
| 2943 | |
| 2944 | subject_pos = indices.at(i) + pattern_len; |
| 2945 | } |
| 2946 | // Add remaining subject content at the end. |
| 2947 | if (subject_pos < subject_len) { |
| 2948 | String::WriteToFlat(*subject, |
| 2949 | result->GetChars() + result_pos, |
| 2950 | subject_pos, |
| 2951 | subject_len); |
| 2952 | } |
| 2953 | return *result; |
| 2954 | } |
| 2955 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2956 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2957 | MUST_USE_RESULT static MaybeObject* StringReplaceRegExpWithString( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2958 | Isolate* isolate, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2959 | String* subject, |
| 2960 | JSRegExp* regexp, |
| 2961 | String* replacement, |
| 2962 | JSArray* last_match_info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2963 | ASSERT(subject->IsFlat()); |
| 2964 | ASSERT(replacement->IsFlat()); |
| 2965 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2966 | HandleScope handles(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2967 | |
| 2968 | int length = subject->length(); |
| 2969 | Handle<String> subject_handle(subject); |
| 2970 | Handle<JSRegExp> regexp_handle(regexp); |
| 2971 | Handle<String> replacement_handle(replacement); |
| 2972 | Handle<JSArray> last_match_info_handle(last_match_info); |
| 2973 | Handle<Object> match = RegExpImpl::Exec(regexp_handle, |
| 2974 | subject_handle, |
| 2975 | 0, |
| 2976 | last_match_info_handle); |
| 2977 | if (match.is_null()) { |
| 2978 | return Failure::Exception(); |
| 2979 | } |
| 2980 | if (match->IsNull()) { |
| 2981 | return *subject_handle; |
| 2982 | } |
| 2983 | |
| 2984 | int capture_count = regexp_handle->CaptureCount(); |
| 2985 | |
| 2986 | // CompiledReplacement uses zone allocation. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2987 | ZoneScope zone(isolate, DELETE_ON_EXIT); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2988 | CompiledReplacement compiled_replacement; |
| 2989 | compiled_replacement.Compile(replacement_handle, |
| 2990 | capture_count, |
| 2991 | length); |
| 2992 | |
| 2993 | bool is_global = regexp_handle->GetFlags().is_global(); |
| 2994 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2995 | // Shortcut for simple non-regexp global replacements |
| 2996 | if (is_global && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2997 | regexp_handle->TypeTag() == JSRegExp::ATOM && |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2998 | compiled_replacement.simple_hint()) { |
| 2999 | if (subject_handle->HasOnlyAsciiChars() && |
| 3000 | replacement_handle->HasOnlyAsciiChars()) { |
| 3001 | return StringReplaceStringWithString<SeqAsciiString>( |
| 3002 | isolate, subject_handle, regexp_handle, replacement_handle); |
| 3003 | } else { |
| 3004 | return StringReplaceStringWithString<SeqTwoByteString>( |
| 3005 | isolate, subject_handle, regexp_handle, replacement_handle); |
| 3006 | } |
| 3007 | } |
| 3008 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3009 | // Guessing the number of parts that the final result string is built |
| 3010 | // from. Global regexps can match any number of times, so we guess |
| 3011 | // conservatively. |
| 3012 | int expected_parts = |
| 3013 | (compiled_replacement.parts() + 1) * (is_global ? 4 : 1) + 1; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3014 | ReplacementStringBuilder builder(isolate->heap(), |
| 3015 | subject_handle, |
| 3016 | expected_parts); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3017 | |
| 3018 | // Index of end of last match. |
| 3019 | int prev = 0; |
| 3020 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3021 | // Number of parts added by compiled replacement plus preceeding |
| 3022 | // string and possibly suffix after last match. It is possible for |
| 3023 | // all components to use two elements when encoded as two smis. |
| 3024 | const int parts_added_per_loop = 2 * (compiled_replacement.parts() + 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3025 | bool matched = true; |
| 3026 | do { |
| 3027 | ASSERT(last_match_info_handle->HasFastElements()); |
| 3028 | // Increase the capacity of the builder before entering local handle-scope, |
| 3029 | // so its internal buffer can safely allocate a new handle if it grows. |
| 3030 | builder.EnsureCapacity(parts_added_per_loop); |
| 3031 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3032 | HandleScope loop_scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3033 | int start, end; |
| 3034 | { |
| 3035 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
| 3036 | FixedArray* match_info_array = |
| 3037 | FixedArray::cast(last_match_info_handle->elements()); |
| 3038 | |
| 3039 | ASSERT_EQ(capture_count * 2 + 2, |
| 3040 | RegExpImpl::GetLastCaptureCount(match_info_array)); |
| 3041 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 3042 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 3043 | } |
| 3044 | |
| 3045 | if (prev < start) { |
| 3046 | builder.AddSubjectSlice(prev, start); |
| 3047 | } |
| 3048 | compiled_replacement.Apply(&builder, |
| 3049 | start, |
| 3050 | end, |
| 3051 | last_match_info_handle); |
| 3052 | prev = end; |
| 3053 | |
| 3054 | // Only continue checking for global regexps. |
| 3055 | if (!is_global) break; |
| 3056 | |
| 3057 | // Continue from where the match ended, unless it was an empty match. |
| 3058 | int next = end; |
| 3059 | if (start == end) { |
| 3060 | next = end + 1; |
| 3061 | if (next > length) break; |
| 3062 | } |
| 3063 | |
| 3064 | match = RegExpImpl::Exec(regexp_handle, |
| 3065 | subject_handle, |
| 3066 | next, |
| 3067 | last_match_info_handle); |
| 3068 | if (match.is_null()) { |
| 3069 | return Failure::Exception(); |
| 3070 | } |
| 3071 | matched = !match->IsNull(); |
| 3072 | } while (matched); |
| 3073 | |
| 3074 | if (prev < length) { |
| 3075 | builder.AddSubjectSlice(prev, length); |
| 3076 | } |
| 3077 | |
| 3078 | return *(builder.ToString()); |
| 3079 | } |
| 3080 | |
| 3081 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3082 | template <typename ResultSeqString> |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3083 | MUST_USE_RESULT static MaybeObject* StringReplaceRegExpWithEmptyString( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3084 | Isolate* isolate, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3085 | String* subject, |
| 3086 | JSRegExp* regexp, |
| 3087 | JSArray* last_match_info) { |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3088 | ASSERT(subject->IsFlat()); |
| 3089 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3090 | HandleScope handles(isolate); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3091 | |
| 3092 | Handle<String> subject_handle(subject); |
| 3093 | Handle<JSRegExp> regexp_handle(regexp); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 3094 | |
| 3095 | // Shortcut for simple non-regexp global replacements |
| 3096 | if (regexp_handle->GetFlags().is_global() && |
| 3097 | regexp_handle->TypeTag() == JSRegExp::ATOM) { |
| 3098 | Handle<String> empty_string_handle(HEAP->empty_string()); |
| 3099 | if (subject_handle->HasOnlyAsciiChars()) { |
| 3100 | return StringReplaceStringWithString<SeqAsciiString>( |
| 3101 | isolate, subject_handle, regexp_handle, empty_string_handle); |
| 3102 | } else { |
| 3103 | return StringReplaceStringWithString<SeqTwoByteString>( |
| 3104 | isolate, subject_handle, regexp_handle, empty_string_handle); |
| 3105 | } |
| 3106 | } |
| 3107 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3108 | Handle<JSArray> last_match_info_handle(last_match_info); |
| 3109 | Handle<Object> match = RegExpImpl::Exec(regexp_handle, |
| 3110 | subject_handle, |
| 3111 | 0, |
| 3112 | last_match_info_handle); |
| 3113 | if (match.is_null()) return Failure::Exception(); |
| 3114 | if (match->IsNull()) return *subject_handle; |
| 3115 | |
| 3116 | ASSERT(last_match_info_handle->HasFastElements()); |
| 3117 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3118 | int start, end; |
| 3119 | { |
| 3120 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
| 3121 | FixedArray* match_info_array = |
| 3122 | FixedArray::cast(last_match_info_handle->elements()); |
| 3123 | |
| 3124 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 3125 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 3126 | } |
| 3127 | |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 3128 | int length = subject_handle->length(); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3129 | int new_length = length - (end - start); |
| 3130 | if (new_length == 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3131 | return isolate->heap()->empty_string(); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3132 | } |
| 3133 | Handle<ResultSeqString> answer; |
| 3134 | if (ResultSeqString::kHasAsciiEncoding) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3135 | answer = Handle<ResultSeqString>::cast( |
| 3136 | isolate->factory()->NewRawAsciiString(new_length)); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3137 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3138 | answer = Handle<ResultSeqString>::cast( |
| 3139 | isolate->factory()->NewRawTwoByteString(new_length)); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3140 | } |
| 3141 | |
| 3142 | // If the regexp isn't global, only match once. |
| 3143 | if (!regexp_handle->GetFlags().is_global()) { |
| 3144 | if (start > 0) { |
| 3145 | String::WriteToFlat(*subject_handle, |
| 3146 | answer->GetChars(), |
| 3147 | 0, |
| 3148 | start); |
| 3149 | } |
| 3150 | if (end < length) { |
| 3151 | String::WriteToFlat(*subject_handle, |
| 3152 | answer->GetChars() + start, |
| 3153 | end, |
| 3154 | length); |
| 3155 | } |
| 3156 | return *answer; |
| 3157 | } |
| 3158 | |
| 3159 | int prev = 0; // Index of end of last match. |
| 3160 | int next = 0; // Start of next search (prev unless last match was empty). |
| 3161 | int position = 0; |
| 3162 | |
| 3163 | do { |
| 3164 | if (prev < start) { |
| 3165 | // Add substring subject[prev;start] to answer string. |
| 3166 | String::WriteToFlat(*subject_handle, |
| 3167 | answer->GetChars() + position, |
| 3168 | prev, |
| 3169 | start); |
| 3170 | position += start - prev; |
| 3171 | } |
| 3172 | prev = end; |
| 3173 | next = end; |
| 3174 | // Continue from where the match ended, unless it was an empty match. |
| 3175 | if (start == end) { |
| 3176 | next++; |
| 3177 | if (next > length) break; |
| 3178 | } |
| 3179 | match = RegExpImpl::Exec(regexp_handle, |
| 3180 | subject_handle, |
| 3181 | next, |
| 3182 | last_match_info_handle); |
| 3183 | if (match.is_null()) return Failure::Exception(); |
| 3184 | if (match->IsNull()) break; |
| 3185 | |
| 3186 | ASSERT(last_match_info_handle->HasFastElements()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3187 | HandleScope loop_scope(isolate); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3188 | { |
| 3189 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
| 3190 | FixedArray* match_info_array = |
| 3191 | FixedArray::cast(last_match_info_handle->elements()); |
| 3192 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 3193 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 3194 | } |
| 3195 | } while (true); |
| 3196 | |
| 3197 | if (prev < length) { |
| 3198 | // Add substring subject[prev;length] to answer string. |
| 3199 | String::WriteToFlat(*subject_handle, |
| 3200 | answer->GetChars() + position, |
| 3201 | prev, |
| 3202 | length); |
| 3203 | position += length - prev; |
| 3204 | } |
| 3205 | |
| 3206 | if (position == 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3207 | return isolate->heap()->empty_string(); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3208 | } |
| 3209 | |
| 3210 | // Shorten string and fill |
| 3211 | int string_size = ResultSeqString::SizeFor(position); |
| 3212 | int allocated_string_size = ResultSeqString::SizeFor(new_length); |
| 3213 | int delta = allocated_string_size - string_size; |
| 3214 | |
| 3215 | answer->set_length(position); |
| 3216 | if (delta == 0) return *answer; |
| 3217 | |
| 3218 | Address end_of_string = answer->address() + string_size; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3219 | isolate->heap()->CreateFillerObjectAt(end_of_string, delta); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3220 | if (Marking::IsBlack(Marking::MarkBitFrom(*answer))) { |
| 3221 | MemoryChunk::IncrementLiveBytesFromMutator(answer->address(), -delta); |
| 3222 | } |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3223 | |
| 3224 | return *answer; |
| 3225 | } |
| 3226 | |
| 3227 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3228 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringReplaceRegExpWithString) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3229 | ASSERT(args.length() == 4); |
| 3230 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3231 | CONVERT_ARG_CHECKED(String, subject, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3232 | if (!subject->IsFlat()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3233 | Object* flat_subject; |
| 3234 | { MaybeObject* maybe_flat_subject = subject->TryFlatten(); |
| 3235 | if (!maybe_flat_subject->ToObject(&flat_subject)) { |
| 3236 | return maybe_flat_subject; |
| 3237 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3238 | } |
| 3239 | subject = String::cast(flat_subject); |
| 3240 | } |
| 3241 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3242 | CONVERT_ARG_CHECKED(String, replacement, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3243 | if (!replacement->IsFlat()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3244 | Object* flat_replacement; |
| 3245 | { MaybeObject* maybe_flat_replacement = replacement->TryFlatten(); |
| 3246 | if (!maybe_flat_replacement->ToObject(&flat_replacement)) { |
| 3247 | return maybe_flat_replacement; |
| 3248 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3249 | } |
| 3250 | replacement = String::cast(flat_replacement); |
| 3251 | } |
| 3252 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3253 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 1); |
| 3254 | CONVERT_ARG_CHECKED(JSArray, last_match_info, 3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3255 | |
| 3256 | ASSERT(last_match_info->HasFastElements()); |
| 3257 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3258 | if (replacement->length() == 0) { |
| 3259 | if (subject->HasOnlyAsciiChars()) { |
| 3260 | return StringReplaceRegExpWithEmptyString<SeqAsciiString>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3261 | isolate, subject, regexp, last_match_info); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3262 | } else { |
| 3263 | return StringReplaceRegExpWithEmptyString<SeqTwoByteString>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3264 | isolate, subject, regexp, last_match_info); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3265 | } |
| 3266 | } |
| 3267 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3268 | return StringReplaceRegExpWithString(isolate, |
| 3269 | subject, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3270 | regexp, |
| 3271 | replacement, |
| 3272 | last_match_info); |
| 3273 | } |
| 3274 | |
| 3275 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3276 | Handle<String> Runtime::StringReplaceOneCharWithString(Isolate* isolate, |
| 3277 | Handle<String> subject, |
| 3278 | Handle<String> search, |
| 3279 | Handle<String> replace, |
| 3280 | bool* found, |
| 3281 | int recursion_limit) { |
| 3282 | if (recursion_limit == 0) return Handle<String>::null(); |
| 3283 | if (subject->IsConsString()) { |
| 3284 | ConsString* cons = ConsString::cast(*subject); |
| 3285 | Handle<String> first = Handle<String>(cons->first()); |
| 3286 | Handle<String> second = Handle<String>(cons->second()); |
| 3287 | Handle<String> new_first = |
| 3288 | StringReplaceOneCharWithString(isolate, |
| 3289 | first, |
| 3290 | search, |
| 3291 | replace, |
| 3292 | found, |
| 3293 | recursion_limit - 1); |
| 3294 | if (*found) return isolate->factory()->NewConsString(new_first, second); |
| 3295 | if (new_first.is_null()) return new_first; |
| 3296 | |
| 3297 | Handle<String> new_second = |
| 3298 | StringReplaceOneCharWithString(isolate, |
| 3299 | second, |
| 3300 | search, |
| 3301 | replace, |
| 3302 | found, |
| 3303 | recursion_limit - 1); |
| 3304 | if (*found) return isolate->factory()->NewConsString(first, new_second); |
| 3305 | if (new_second.is_null()) return new_second; |
| 3306 | |
| 3307 | return subject; |
| 3308 | } else { |
| 3309 | int index = StringMatch(isolate, subject, search, 0); |
| 3310 | if (index == -1) return subject; |
| 3311 | *found = true; |
| 3312 | Handle<String> first = isolate->factory()->NewSubString(subject, 0, index); |
| 3313 | Handle<String> cons1 = isolate->factory()->NewConsString(first, replace); |
| 3314 | Handle<String> second = |
| 3315 | isolate->factory()->NewSubString(subject, index + 1, subject->length()); |
| 3316 | return isolate->factory()->NewConsString(cons1, second); |
| 3317 | } |
| 3318 | } |
| 3319 | |
| 3320 | |
| 3321 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringReplaceOneCharWithString) { |
| 3322 | ASSERT(args.length() == 3); |
| 3323 | HandleScope scope(isolate); |
| 3324 | CONVERT_ARG_HANDLE_CHECKED(String, subject, 0); |
| 3325 | CONVERT_ARG_HANDLE_CHECKED(String, search, 1); |
| 3326 | CONVERT_ARG_HANDLE_CHECKED(String, replace, 2); |
| 3327 | |
| 3328 | // If the cons string tree is too deep, we simply abort the recursion and |
| 3329 | // retry with a flattened subject string. |
| 3330 | const int kRecursionLimit = 0x1000; |
| 3331 | bool found = false; |
| 3332 | Handle<String> result = |
| 3333 | Runtime::StringReplaceOneCharWithString(isolate, |
| 3334 | subject, |
| 3335 | search, |
| 3336 | replace, |
| 3337 | &found, |
| 3338 | kRecursionLimit); |
| 3339 | if (!result.is_null()) return *result; |
| 3340 | return *Runtime::StringReplaceOneCharWithString(isolate, |
| 3341 | FlattenGetString(subject), |
| 3342 | search, |
| 3343 | replace, |
| 3344 | &found, |
| 3345 | kRecursionLimit); |
| 3346 | } |
| 3347 | |
| 3348 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3349 | // Perform string match of pattern on subject, starting at start index. |
| 3350 | // Caller must ensure that 0 <= start_index <= sub->length(), |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3351 | // and should check that pat->length() + start_index <= sub->length(). |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3352 | int Runtime::StringMatch(Isolate* isolate, |
| 3353 | Handle<String> sub, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3354 | Handle<String> pat, |
| 3355 | int start_index) { |
| 3356 | ASSERT(0 <= start_index); |
| 3357 | ASSERT(start_index <= sub->length()); |
| 3358 | |
| 3359 | int pattern_length = pat->length(); |
| 3360 | if (pattern_length == 0) return start_index; |
| 3361 | |
| 3362 | int subject_length = sub->length(); |
| 3363 | if (start_index + pattern_length > subject_length) return -1; |
| 3364 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3365 | if (!sub->IsFlat()) FlattenString(sub); |
| 3366 | if (!pat->IsFlat()) FlattenString(pat); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3367 | |
| 3368 | AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3369 | // Extract flattened substrings of cons strings before determining asciiness. |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3370 | String::FlatContent seq_sub = sub->GetFlatContent(); |
| 3371 | String::FlatContent seq_pat = pat->GetFlatContent(); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3372 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3373 | // dispatch on type of strings |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3374 | if (seq_pat.IsAscii()) { |
| 3375 | Vector<const char> pat_vector = seq_pat.ToAsciiVector(); |
| 3376 | if (seq_sub.IsAscii()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3377 | return SearchString(isolate, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3378 | seq_sub.ToAsciiVector(), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3379 | pat_vector, |
| 3380 | start_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3381 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3382 | return SearchString(isolate, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3383 | seq_sub.ToUC16Vector(), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3384 | pat_vector, |
| 3385 | start_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3386 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3387 | Vector<const uc16> pat_vector = seq_pat.ToUC16Vector(); |
| 3388 | if (seq_sub.IsAscii()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3389 | return SearchString(isolate, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3390 | seq_sub.ToAsciiVector(), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3391 | pat_vector, |
| 3392 | start_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3393 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3394 | return SearchString(isolate, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3395 | seq_sub.ToUC16Vector(), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3396 | pat_vector, |
| 3397 | start_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3398 | } |
| 3399 | |
| 3400 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3401 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringIndexOf) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3402 | HandleScope scope(isolate); // create a new handle scope |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3403 | ASSERT(args.length() == 3); |
| 3404 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3405 | CONVERT_ARG_HANDLE_CHECKED(String, sub, 0); |
| 3406 | CONVERT_ARG_HANDLE_CHECKED(String, pat, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3407 | |
| 3408 | Object* index = args[2]; |
| 3409 | uint32_t start_index; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3410 | if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3411 | |
| 3412 | RUNTIME_ASSERT(start_index <= static_cast<uint32_t>(sub->length())); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3413 | int position = |
| 3414 | Runtime::StringMatch(isolate, sub, pat, start_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3415 | return Smi::FromInt(position); |
| 3416 | } |
| 3417 | |
| 3418 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3419 | template <typename schar, typename pchar> |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 3420 | static int StringMatchBackwards(Vector<const schar> subject, |
| 3421 | Vector<const pchar> pattern, |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3422 | int idx) { |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 3423 | int pattern_length = pattern.length(); |
| 3424 | ASSERT(pattern_length >= 1); |
| 3425 | ASSERT(idx + pattern_length <= subject.length()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3426 | |
| 3427 | if (sizeof(schar) == 1 && sizeof(pchar) > 1) { |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 3428 | for (int i = 0; i < pattern_length; i++) { |
| 3429 | uc16 c = pattern[i]; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3430 | if (c > String::kMaxAsciiCharCode) { |
| 3431 | return -1; |
| 3432 | } |
| 3433 | } |
| 3434 | } |
| 3435 | |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 3436 | pchar pattern_first_char = pattern[0]; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3437 | for (int i = idx; i >= 0; i--) { |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 3438 | if (subject[i] != pattern_first_char) continue; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3439 | int j = 1; |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 3440 | while (j < pattern_length) { |
| 3441 | if (pattern[j] != subject[i+j]) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3442 | break; |
| 3443 | } |
| 3444 | j++; |
| 3445 | } |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 3446 | if (j == pattern_length) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3447 | return i; |
| 3448 | } |
| 3449 | } |
| 3450 | return -1; |
| 3451 | } |
| 3452 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3453 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringLastIndexOf) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3454 | HandleScope scope(isolate); // create a new handle scope |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3455 | ASSERT(args.length() == 3); |
| 3456 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3457 | CONVERT_ARG_HANDLE_CHECKED(String, sub, 0); |
| 3458 | CONVERT_ARG_HANDLE_CHECKED(String, pat, 1); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3459 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3460 | Object* index = args[2]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3461 | uint32_t start_index; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3462 | if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3463 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3464 | uint32_t pat_length = pat->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3465 | uint32_t sub_length = sub->length(); |
| 3466 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3467 | if (start_index + pat_length > sub_length) { |
| 3468 | start_index = sub_length - pat_length; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3471 | if (pat_length == 0) { |
| 3472 | return Smi::FromInt(start_index); |
| 3473 | } |
| 3474 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3475 | if (!sub->IsFlat()) FlattenString(sub); |
| 3476 | if (!pat->IsFlat()) FlattenString(pat); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3477 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3478 | int position = -1; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3479 | AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
| 3480 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3481 | String::FlatContent sub_content = sub->GetFlatContent(); |
| 3482 | String::FlatContent pat_content = pat->GetFlatContent(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3483 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3484 | if (pat_content.IsAscii()) { |
| 3485 | Vector<const char> pat_vector = pat_content.ToAsciiVector(); |
| 3486 | if (sub_content.IsAscii()) { |
| 3487 | position = StringMatchBackwards(sub_content.ToAsciiVector(), |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3488 | pat_vector, |
| 3489 | start_index); |
| 3490 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3491 | position = StringMatchBackwards(sub_content.ToUC16Vector(), |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3492 | pat_vector, |
| 3493 | start_index); |
| 3494 | } |
| 3495 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3496 | Vector<const uc16> pat_vector = pat_content.ToUC16Vector(); |
| 3497 | if (sub_content.IsAscii()) { |
| 3498 | position = StringMatchBackwards(sub_content.ToAsciiVector(), |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3499 | pat_vector, |
| 3500 | start_index); |
| 3501 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3502 | position = StringMatchBackwards(sub_content.ToUC16Vector(), |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3503 | pat_vector, |
| 3504 | start_index); |
| 3505 | } |
| 3506 | } |
| 3507 | |
| 3508 | return Smi::FromInt(position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3509 | } |
| 3510 | |
| 3511 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3512 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringLocaleCompare) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3513 | NoHandleAllocation ha; |
| 3514 | ASSERT(args.length() == 2); |
| 3515 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3516 | CONVERT_ARG_CHECKED(String, str1, 0); |
| 3517 | CONVERT_ARG_CHECKED(String, str2, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3518 | |
| 3519 | if (str1 == str2) return Smi::FromInt(0); // Equal. |
| 3520 | int str1_length = str1->length(); |
| 3521 | int str2_length = str2->length(); |
| 3522 | |
| 3523 | // Decide trivial cases without flattening. |
| 3524 | if (str1_length == 0) { |
| 3525 | if (str2_length == 0) return Smi::FromInt(0); // Equal. |
| 3526 | return Smi::FromInt(-str2_length); |
| 3527 | } else { |
| 3528 | if (str2_length == 0) return Smi::FromInt(str1_length); |
| 3529 | } |
| 3530 | |
| 3531 | int end = str1_length < str2_length ? str1_length : str2_length; |
| 3532 | |
| 3533 | // No need to flatten if we are going to find the answer on the first |
| 3534 | // character. At this point we know there is at least one character |
| 3535 | // in each string, due to the trivial case handling above. |
| 3536 | int d = str1->Get(0) - str2->Get(0); |
| 3537 | if (d != 0) return Smi::FromInt(d); |
| 3538 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3539 | str1->TryFlatten(); |
| 3540 | str2->TryFlatten(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3541 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3542 | StringInputBuffer& buf1 = |
| 3543 | *isolate->runtime_state()->string_locale_compare_buf1(); |
| 3544 | StringInputBuffer& buf2 = |
| 3545 | *isolate->runtime_state()->string_locale_compare_buf2(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3546 | |
| 3547 | buf1.Reset(str1); |
| 3548 | buf2.Reset(str2); |
| 3549 | |
| 3550 | for (int i = 0; i < end; i++) { |
| 3551 | uint16_t char1 = buf1.GetNext(); |
| 3552 | uint16_t char2 = buf2.GetNext(); |
| 3553 | if (char1 != char2) return Smi::FromInt(char1 - char2); |
| 3554 | } |
| 3555 | |
| 3556 | return Smi::FromInt(str1_length - str2_length); |
| 3557 | } |
| 3558 | |
| 3559 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3560 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SubString) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3561 | NoHandleAllocation ha; |
| 3562 | ASSERT(args.length() == 3); |
| 3563 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3564 | CONVERT_ARG_CHECKED(String, value, 0); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3565 | int start, end; |
| 3566 | // We have a fast integer-only case here to avoid a conversion to double in |
| 3567 | // the common case where from and to are Smis. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3568 | if (args[1]->IsSmi() && args[2]->IsSmi()) { |
| 3569 | CONVERT_SMI_ARG_CHECKED(from_number, 1); |
| 3570 | CONVERT_SMI_ARG_CHECKED(to_number, 2); |
| 3571 | start = from_number; |
| 3572 | end = to_number; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3573 | } else { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3574 | CONVERT_DOUBLE_ARG_CHECKED(from_number, 1); |
| 3575 | CONVERT_DOUBLE_ARG_CHECKED(to_number, 2); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3576 | start = FastD2I(from_number); |
| 3577 | end = FastD2I(to_number); |
| 3578 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3579 | RUNTIME_ASSERT(end >= start); |
| 3580 | RUNTIME_ASSERT(start >= 0); |
| 3581 | RUNTIME_ASSERT(end <= value->length()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3582 | isolate->counters()->sub_string_runtime()->Increment(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3583 | return value->SubString(start, end); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3587 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringMatch) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3588 | ASSERT_EQ(3, args.length()); |
| 3589 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3590 | CONVERT_ARG_HANDLE_CHECKED(String, subject, 0); |
| 3591 | CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 1); |
| 3592 | CONVERT_ARG_HANDLE_CHECKED(JSArray, regexp_info, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3593 | HandleScope handles; |
| 3594 | |
| 3595 | Handle<Object> match = RegExpImpl::Exec(regexp, subject, 0, regexp_info); |
| 3596 | |
| 3597 | if (match.is_null()) { |
| 3598 | return Failure::Exception(); |
| 3599 | } |
| 3600 | if (match->IsNull()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3601 | return isolate->heap()->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3602 | } |
| 3603 | int length = subject->length(); |
| 3604 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3605 | ZoneScope zone_space(isolate, DELETE_ON_EXIT); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3606 | ZoneList<int> offsets(8); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3607 | int start; |
| 3608 | int end; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3609 | do { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3610 | { |
| 3611 | AssertNoAllocation no_alloc; |
| 3612 | FixedArray* elements = FixedArray::cast(regexp_info->elements()); |
| 3613 | start = Smi::cast(elements->get(RegExpImpl::kFirstCapture))->value(); |
| 3614 | end = Smi::cast(elements->get(RegExpImpl::kFirstCapture + 1))->value(); |
| 3615 | } |
| 3616 | offsets.Add(start); |
| 3617 | offsets.Add(end); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3618 | if (start == end) if (++end > length) break; |
| 3619 | match = RegExpImpl::Exec(regexp, subject, end, regexp_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3620 | if (match.is_null()) { |
| 3621 | return Failure::Exception(); |
| 3622 | } |
| 3623 | } while (!match->IsNull()); |
| 3624 | int matches = offsets.length() / 2; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3625 | Handle<FixedArray> elements = isolate->factory()->NewFixedArray(matches); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3626 | Handle<String> substring = isolate->factory()-> |
| 3627 | NewSubString(subject, offsets.at(0), offsets.at(1)); |
| 3628 | elements->set(0, *substring); |
| 3629 | for (int i = 1; i < matches ; i++) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3630 | int from = offsets.at(i * 2); |
| 3631 | int to = offsets.at(i * 2 + 1); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3632 | Handle<String> substring = isolate->factory()-> |
| 3633 | NewProperSubString(subject, from, to); |
| 3634 | elements->set(i, *substring); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3635 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3636 | Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(elements); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3637 | result->set_length(Smi::FromInt(matches)); |
| 3638 | return *result; |
| 3639 | } |
| 3640 | |
| 3641 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3642 | // Two smis before and after the match, for very long strings. |
| 3643 | const int kMaxBuilderEntriesPerRegExpMatch = 5; |
| 3644 | |
| 3645 | |
| 3646 | static void SetLastMatchInfoNoCaptures(Handle<String> subject, |
| 3647 | Handle<JSArray> last_match_info, |
| 3648 | int match_start, |
| 3649 | int match_end) { |
| 3650 | // Fill last_match_info with a single capture. |
| 3651 | last_match_info->EnsureSize(2 + RegExpImpl::kLastMatchOverhead); |
| 3652 | AssertNoAllocation no_gc; |
| 3653 | FixedArray* elements = FixedArray::cast(last_match_info->elements()); |
| 3654 | RegExpImpl::SetLastCaptureCount(elements, 2); |
| 3655 | RegExpImpl::SetLastInput(elements, *subject); |
| 3656 | RegExpImpl::SetLastSubject(elements, *subject); |
| 3657 | RegExpImpl::SetCapture(elements, 0, match_start); |
| 3658 | RegExpImpl::SetCapture(elements, 1, match_end); |
| 3659 | } |
| 3660 | |
| 3661 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3662 | template <typename SubjectChar, typename PatternChar> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3663 | static bool SearchStringMultiple(Isolate* isolate, |
| 3664 | Vector<const SubjectChar> subject, |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3665 | Vector<const PatternChar> pattern, |
| 3666 | String* pattern_string, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3667 | FixedArrayBuilder* builder, |
| 3668 | int* match_pos) { |
| 3669 | int pos = *match_pos; |
| 3670 | int subject_length = subject.length(); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3671 | int pattern_length = pattern.length(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3672 | int max_search_start = subject_length - pattern_length; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3673 | StringSearch<PatternChar, SubjectChar> search(isolate, pattern); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3674 | while (pos <= max_search_start) { |
| 3675 | if (!builder->HasCapacity(kMaxBuilderEntriesPerRegExpMatch)) { |
| 3676 | *match_pos = pos; |
| 3677 | return false; |
| 3678 | } |
| 3679 | // Position of end of previous match. |
| 3680 | int match_end = pos + pattern_length; |
| 3681 | int new_pos = search.Search(subject, match_end); |
| 3682 | if (new_pos >= 0) { |
| 3683 | // A match. |
| 3684 | if (new_pos > match_end) { |
| 3685 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3686 | match_end, |
| 3687 | new_pos); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3688 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3689 | pos = new_pos; |
| 3690 | builder->Add(pattern_string); |
| 3691 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3692 | break; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3693 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3694 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3695 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3696 | if (pos < max_search_start) { |
| 3697 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3698 | pos + pattern_length, |
| 3699 | subject_length); |
| 3700 | } |
| 3701 | *match_pos = pos; |
| 3702 | return true; |
| 3703 | } |
| 3704 | |
| 3705 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3706 | static bool SearchStringMultiple(Isolate* isolate, |
| 3707 | Handle<String> subject, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3708 | Handle<String> pattern, |
| 3709 | Handle<JSArray> last_match_info, |
| 3710 | FixedArrayBuilder* builder) { |
| 3711 | ASSERT(subject->IsFlat()); |
| 3712 | ASSERT(pattern->IsFlat()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3713 | |
| 3714 | // Treating as if a previous match was before first character. |
| 3715 | int match_pos = -pattern->length(); |
| 3716 | |
| 3717 | for (;;) { // Break when search complete. |
| 3718 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3719 | AssertNoAllocation no_gc; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3720 | String::FlatContent subject_content = subject->GetFlatContent(); |
| 3721 | String::FlatContent pattern_content = pattern->GetFlatContent(); |
| 3722 | if (subject_content.IsAscii()) { |
| 3723 | Vector<const char> subject_vector = subject_content.ToAsciiVector(); |
| 3724 | if (pattern_content.IsAscii()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3725 | if (SearchStringMultiple(isolate, |
| 3726 | subject_vector, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3727 | pattern_content.ToAsciiVector(), |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3728 | *pattern, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3729 | builder, |
| 3730 | &match_pos)) break; |
| 3731 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3732 | if (SearchStringMultiple(isolate, |
| 3733 | subject_vector, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3734 | pattern_content.ToUC16Vector(), |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3735 | *pattern, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3736 | builder, |
| 3737 | &match_pos)) break; |
| 3738 | } |
| 3739 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3740 | Vector<const uc16> subject_vector = subject_content.ToUC16Vector(); |
| 3741 | if (pattern_content.IsAscii()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3742 | if (SearchStringMultiple(isolate, |
| 3743 | subject_vector, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3744 | pattern_content.ToAsciiVector(), |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3745 | *pattern, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3746 | builder, |
| 3747 | &match_pos)) break; |
| 3748 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3749 | if (SearchStringMultiple(isolate, |
| 3750 | subject_vector, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3751 | pattern_content.ToUC16Vector(), |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3752 | *pattern, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3753 | builder, |
| 3754 | &match_pos)) break; |
| 3755 | } |
| 3756 | } |
| 3757 | } |
| 3758 | |
| 3759 | if (match_pos >= 0) { |
| 3760 | SetLastMatchInfoNoCaptures(subject, |
| 3761 | last_match_info, |
| 3762 | match_pos, |
| 3763 | match_pos + pattern->length()); |
| 3764 | return true; |
| 3765 | } |
| 3766 | return false; // No matches at all. |
| 3767 | } |
| 3768 | |
| 3769 | |
| 3770 | static RegExpImpl::IrregexpResult SearchRegExpNoCaptureMultiple( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3771 | Isolate* isolate, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3772 | Handle<String> subject, |
| 3773 | Handle<JSRegExp> regexp, |
| 3774 | Handle<JSArray> last_match_array, |
| 3775 | FixedArrayBuilder* builder) { |
| 3776 | ASSERT(subject->IsFlat()); |
| 3777 | int match_start = -1; |
| 3778 | int match_end = 0; |
| 3779 | int pos = 0; |
| 3780 | int required_registers = RegExpImpl::IrregexpPrepare(regexp, subject); |
| 3781 | if (required_registers < 0) return RegExpImpl::RE_EXCEPTION; |
| 3782 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3783 | OffsetsVector registers(required_registers, isolate); |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 3784 | Vector<int32_t> register_vector(registers.vector(), registers.length()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3785 | int subject_length = subject->length(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3786 | bool first = true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3787 | |
| 3788 | for (;;) { // Break on failure, return on exception. |
| 3789 | RegExpImpl::IrregexpResult result = |
| 3790 | RegExpImpl::IrregexpExecOnce(regexp, |
| 3791 | subject, |
| 3792 | pos, |
| 3793 | register_vector); |
| 3794 | if (result == RegExpImpl::RE_SUCCESS) { |
| 3795 | match_start = register_vector[0]; |
| 3796 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3797 | if (match_end < match_start) { |
| 3798 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3799 | match_end, |
| 3800 | match_start); |
| 3801 | } |
| 3802 | match_end = register_vector[1]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3803 | HandleScope loop_scope(isolate); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3804 | if (!first) { |
| 3805 | builder->Add(*isolate->factory()->NewProperSubString(subject, |
| 3806 | match_start, |
| 3807 | match_end)); |
| 3808 | } else { |
| 3809 | builder->Add(*isolate->factory()->NewSubString(subject, |
| 3810 | match_start, |
| 3811 | match_end)); |
| 3812 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3813 | if (match_start != match_end) { |
| 3814 | pos = match_end; |
| 3815 | } else { |
| 3816 | pos = match_end + 1; |
| 3817 | if (pos > subject_length) break; |
| 3818 | } |
| 3819 | } else if (result == RegExpImpl::RE_FAILURE) { |
| 3820 | break; |
| 3821 | } else { |
| 3822 | ASSERT_EQ(result, RegExpImpl::RE_EXCEPTION); |
| 3823 | return result; |
| 3824 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3825 | first = false; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3826 | } |
| 3827 | |
| 3828 | if (match_start >= 0) { |
| 3829 | if (match_end < subject_length) { |
| 3830 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3831 | match_end, |
| 3832 | subject_length); |
| 3833 | } |
| 3834 | SetLastMatchInfoNoCaptures(subject, |
| 3835 | last_match_array, |
| 3836 | match_start, |
| 3837 | match_end); |
| 3838 | return RegExpImpl::RE_SUCCESS; |
| 3839 | } else { |
| 3840 | return RegExpImpl::RE_FAILURE; // No matches at all. |
| 3841 | } |
| 3842 | } |
| 3843 | |
| 3844 | |
| 3845 | static RegExpImpl::IrregexpResult SearchRegExpMultiple( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3846 | Isolate* isolate, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3847 | Handle<String> subject, |
| 3848 | Handle<JSRegExp> regexp, |
| 3849 | Handle<JSArray> last_match_array, |
| 3850 | FixedArrayBuilder* builder) { |
| 3851 | |
| 3852 | ASSERT(subject->IsFlat()); |
| 3853 | int required_registers = RegExpImpl::IrregexpPrepare(regexp, subject); |
| 3854 | if (required_registers < 0) return RegExpImpl::RE_EXCEPTION; |
| 3855 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3856 | OffsetsVector registers(required_registers, isolate); |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 3857 | Vector<int32_t> register_vector(registers.vector(), registers.length()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3858 | |
| 3859 | RegExpImpl::IrregexpResult result = |
| 3860 | RegExpImpl::IrregexpExecOnce(regexp, |
| 3861 | subject, |
| 3862 | 0, |
| 3863 | register_vector); |
| 3864 | |
| 3865 | int capture_count = regexp->CaptureCount(); |
| 3866 | int subject_length = subject->length(); |
| 3867 | |
| 3868 | // Position to search from. |
| 3869 | int pos = 0; |
| 3870 | // End of previous match. Differs from pos if match was empty. |
| 3871 | int match_end = 0; |
| 3872 | if (result == RegExpImpl::RE_SUCCESS) { |
| 3873 | // Need to keep a copy of the previous match for creating last_match_info |
| 3874 | // at the end, so we have two vectors that we swap between. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3875 | OffsetsVector registers2(required_registers, isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3876 | Vector<int> prev_register_vector(registers2.vector(), registers2.length()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3877 | bool first = true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3878 | do { |
| 3879 | int match_start = register_vector[0]; |
| 3880 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3881 | if (match_end < match_start) { |
| 3882 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3883 | match_end, |
| 3884 | match_start); |
| 3885 | } |
| 3886 | match_end = register_vector[1]; |
| 3887 | |
| 3888 | { |
| 3889 | // Avoid accumulating new handles inside loop. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3890 | HandleScope temp_scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3891 | // Arguments array to replace function is match, captures, index and |
| 3892 | // subject, i.e., 3 + capture count in total. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3893 | Handle<FixedArray> elements = |
| 3894 | isolate->factory()->NewFixedArray(3 + capture_count); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3895 | Handle<String> match; |
| 3896 | if (!first) { |
| 3897 | match = isolate->factory()->NewProperSubString(subject, |
| 3898 | match_start, |
| 3899 | match_end); |
| 3900 | } else { |
| 3901 | match = isolate->factory()->NewSubString(subject, |
| 3902 | match_start, |
| 3903 | match_end); |
| 3904 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3905 | elements->set(0, *match); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3906 | for (int i = 1; i <= capture_count; i++) { |
| 3907 | int start = register_vector[i * 2]; |
| 3908 | if (start >= 0) { |
| 3909 | int end = register_vector[i * 2 + 1]; |
| 3910 | ASSERT(start <= end); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3911 | Handle<String> substring; |
| 3912 | if (!first) { |
| 3913 | substring = isolate->factory()->NewProperSubString(subject, |
| 3914 | start, |
| 3915 | end); |
| 3916 | } else { |
| 3917 | substring = isolate->factory()->NewSubString(subject, start, end); |
| 3918 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3919 | elements->set(i, *substring); |
| 3920 | } else { |
| 3921 | ASSERT(register_vector[i * 2 + 1] < 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3922 | elements->set(i, isolate->heap()->undefined_value()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3923 | } |
| 3924 | } |
| 3925 | elements->set(capture_count + 1, Smi::FromInt(match_start)); |
| 3926 | elements->set(capture_count + 2, *subject); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3927 | builder->Add(*isolate->factory()->NewJSArrayWithElements(elements)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3928 | } |
| 3929 | // Swap register vectors, so the last successful match is in |
| 3930 | // prev_register_vector. |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 3931 | Vector<int32_t> tmp = prev_register_vector; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3932 | prev_register_vector = register_vector; |
| 3933 | register_vector = tmp; |
| 3934 | |
| 3935 | if (match_end > match_start) { |
| 3936 | pos = match_end; |
| 3937 | } else { |
| 3938 | pos = match_end + 1; |
| 3939 | if (pos > subject_length) { |
| 3940 | break; |
| 3941 | } |
| 3942 | } |
| 3943 | |
| 3944 | result = RegExpImpl::IrregexpExecOnce(regexp, |
| 3945 | subject, |
| 3946 | pos, |
| 3947 | register_vector); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3948 | first = false; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3949 | } while (result == RegExpImpl::RE_SUCCESS); |
| 3950 | |
| 3951 | if (result != RegExpImpl::RE_EXCEPTION) { |
| 3952 | // Finished matching, with at least one match. |
| 3953 | if (match_end < subject_length) { |
| 3954 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3955 | match_end, |
| 3956 | subject_length); |
| 3957 | } |
| 3958 | |
| 3959 | int last_match_capture_count = (capture_count + 1) * 2; |
| 3960 | int last_match_array_size = |
| 3961 | last_match_capture_count + RegExpImpl::kLastMatchOverhead; |
| 3962 | last_match_array->EnsureSize(last_match_array_size); |
| 3963 | AssertNoAllocation no_gc; |
| 3964 | FixedArray* elements = FixedArray::cast(last_match_array->elements()); |
| 3965 | RegExpImpl::SetLastCaptureCount(elements, last_match_capture_count); |
| 3966 | RegExpImpl::SetLastSubject(elements, *subject); |
| 3967 | RegExpImpl::SetLastInput(elements, *subject); |
| 3968 | for (int i = 0; i < last_match_capture_count; i++) { |
| 3969 | RegExpImpl::SetCapture(elements, i, prev_register_vector[i]); |
| 3970 | } |
| 3971 | return RegExpImpl::RE_SUCCESS; |
| 3972 | } |
| 3973 | } |
| 3974 | // No matches at all, return failure or exception result directly. |
| 3975 | return result; |
| 3976 | } |
| 3977 | |
| 3978 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3979 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpExecMultiple) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3980 | ASSERT(args.length() == 4); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3981 | HandleScope handles(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3982 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3983 | CONVERT_ARG_HANDLE_CHECKED(String, subject, 1); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3984 | if (!subject->IsFlat()) FlattenString(subject); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3985 | CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 0); |
| 3986 | CONVERT_ARG_HANDLE_CHECKED(JSArray, last_match_info, 2); |
| 3987 | CONVERT_ARG_HANDLE_CHECKED(JSArray, result_array, 3); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3988 | |
| 3989 | ASSERT(last_match_info->HasFastElements()); |
| 3990 | ASSERT(regexp->GetFlags().is_global()); |
| 3991 | Handle<FixedArray> result_elements; |
| 3992 | if (result_array->HasFastElements()) { |
| 3993 | result_elements = |
| 3994 | Handle<FixedArray>(FixedArray::cast(result_array->elements())); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3995 | } |
| 3996 | if (result_elements.is_null() || result_elements->length() < 16) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3997 | result_elements = isolate->factory()->NewFixedArrayWithHoles(16); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3998 | } |
| 3999 | FixedArrayBuilder builder(result_elements); |
| 4000 | |
| 4001 | if (regexp->TypeTag() == JSRegExp::ATOM) { |
| 4002 | Handle<String> pattern( |
| 4003 | String::cast(regexp->DataAt(JSRegExp::kAtomPatternIndex))); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4004 | ASSERT(pattern->IsFlat()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4005 | if (SearchStringMultiple(isolate, subject, pattern, |
| 4006 | last_match_info, &builder)) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4007 | return *builder.ToJSArray(result_array); |
| 4008 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4009 | return isolate->heap()->null_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4010 | } |
| 4011 | |
| 4012 | ASSERT_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP); |
| 4013 | |
| 4014 | RegExpImpl::IrregexpResult result; |
| 4015 | if (regexp->CaptureCount() == 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4016 | result = SearchRegExpNoCaptureMultiple(isolate, |
| 4017 | subject, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4018 | regexp, |
| 4019 | last_match_info, |
| 4020 | &builder); |
| 4021 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4022 | result = SearchRegExpMultiple(isolate, |
| 4023 | subject, |
| 4024 | regexp, |
| 4025 | last_match_info, |
| 4026 | &builder); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4027 | } |
| 4028 | if (result == RegExpImpl::RE_SUCCESS) return *builder.ToJSArray(result_array); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4029 | if (result == RegExpImpl::RE_FAILURE) return isolate->heap()->null_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4030 | ASSERT_EQ(result, RegExpImpl::RE_EXCEPTION); |
| 4031 | return Failure::Exception(); |
| 4032 | } |
| 4033 | |
| 4034 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4035 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToRadixString) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4036 | NoHandleAllocation ha; |
| 4037 | ASSERT(args.length() == 2); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4038 | CONVERT_SMI_ARG_CHECKED(radix, 1); |
| 4039 | RUNTIME_ASSERT(2 <= radix && radix <= 36); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4040 | |
| 4041 | // Fast case where the result is a one character string. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4042 | if (args[0]->IsSmi()) { |
| 4043 | int value = args.smi_at(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4044 | if (value >= 0 && value < radix) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4045 | // Character array used for conversion. |
| 4046 | static const char kCharTable[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4047 | return isolate->heap()-> |
| 4048 | LookupSingleCharacterStringFromCode(kCharTable[value]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4049 | } |
| 4050 | } |
| 4051 | |
| 4052 | // Slow case. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4053 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4054 | if (isnan(value)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4055 | return *isolate->factory()->nan_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4056 | } |
| 4057 | if (isinf(value)) { |
| 4058 | if (value < 0) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4059 | return *isolate->factory()->minus_infinity_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4060 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4061 | return *isolate->factory()->infinity_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4062 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4063 | char* str = DoubleToRadixCString(value, radix); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4064 | MaybeObject* result = |
| 4065 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4066 | DeleteArray(str); |
| 4067 | return result; |
| 4068 | } |
| 4069 | |
| 4070 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4071 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToFixed) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4072 | NoHandleAllocation ha; |
| 4073 | ASSERT(args.length() == 2); |
| 4074 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4075 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4076 | if (isnan(value)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4077 | return *isolate->factory()->nan_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4078 | } |
| 4079 | if (isinf(value)) { |
| 4080 | if (value < 0) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4081 | return *isolate->factory()->minus_infinity_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4082 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4083 | return *isolate->factory()->infinity_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4084 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4085 | CONVERT_DOUBLE_ARG_CHECKED(f_number, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4086 | int f = FastD2I(f_number); |
| 4087 | RUNTIME_ASSERT(f >= 0); |
| 4088 | char* str = DoubleToFixedCString(value, f); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4089 | MaybeObject* res = |
| 4090 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4091 | DeleteArray(str); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4092 | return res; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4093 | } |
| 4094 | |
| 4095 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4096 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToExponential) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4097 | NoHandleAllocation ha; |
| 4098 | ASSERT(args.length() == 2); |
| 4099 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4100 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4101 | if (isnan(value)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4102 | return *isolate->factory()->nan_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4103 | } |
| 4104 | if (isinf(value)) { |
| 4105 | if (value < 0) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4106 | return *isolate->factory()->minus_infinity_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4107 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4108 | return *isolate->factory()->infinity_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4109 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4110 | CONVERT_DOUBLE_ARG_CHECKED(f_number, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4111 | int f = FastD2I(f_number); |
| 4112 | RUNTIME_ASSERT(f >= -1 && f <= 20); |
| 4113 | char* str = DoubleToExponentialCString(value, f); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4114 | MaybeObject* res = |
| 4115 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4116 | DeleteArray(str); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4117 | return res; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4121 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToPrecision) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4122 | NoHandleAllocation ha; |
| 4123 | ASSERT(args.length() == 2); |
| 4124 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4125 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4126 | if (isnan(value)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4127 | return *isolate->factory()->nan_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4128 | } |
| 4129 | if (isinf(value)) { |
| 4130 | if (value < 0) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4131 | return *isolate->factory()->minus_infinity_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4132 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4133 | return *isolate->factory()->infinity_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4134 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4135 | CONVERT_DOUBLE_ARG_CHECKED(f_number, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4136 | int f = FastD2I(f_number); |
| 4137 | RUNTIME_ASSERT(f >= 1 && f <= 21); |
| 4138 | char* str = DoubleToPrecisionCString(value, f); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4139 | MaybeObject* res = |
| 4140 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4141 | DeleteArray(str); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4142 | return res; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | |
| 4146 | // Returns a single character string where first character equals |
| 4147 | // string->Get(index). |
| 4148 | static Handle<Object> GetCharAt(Handle<String> string, uint32_t index) { |
| 4149 | if (index < static_cast<uint32_t>(string->length())) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4150 | string->TryFlatten(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4151 | return LookupSingleCharacterStringFromCode( |
| 4152 | string->Get(index)); |
| 4153 | } |
| 4154 | return Execution::CharAt(string, index); |
| 4155 | } |
| 4156 | |
| 4157 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4158 | MaybeObject* Runtime::GetElementOrCharAt(Isolate* isolate, |
| 4159 | Handle<Object> object, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4160 | uint32_t index) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4161 | // Handle [] indexing on Strings |
| 4162 | if (object->IsString()) { |
| 4163 | Handle<Object> result = GetCharAt(Handle<String>::cast(object), index); |
| 4164 | if (!result->IsUndefined()) return *result; |
| 4165 | } |
| 4166 | |
| 4167 | // Handle [] indexing on String objects |
| 4168 | if (object->IsStringObjectWithCharacterAt(index)) { |
| 4169 | Handle<JSValue> js_value = Handle<JSValue>::cast(object); |
| 4170 | Handle<Object> result = |
| 4171 | GetCharAt(Handle<String>(String::cast(js_value->value())), index); |
| 4172 | if (!result->IsUndefined()) return *result; |
| 4173 | } |
| 4174 | |
| 4175 | if (object->IsString() || object->IsNumber() || object->IsBoolean()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4176 | return object->GetPrototype()->GetElement(index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
| 4179 | return object->GetElement(index); |
| 4180 | } |
| 4181 | |
| 4182 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4183 | MaybeObject* Runtime::GetObjectProperty(Isolate* isolate, |
| 4184 | Handle<Object> object, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4185 | Handle<Object> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4186 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4187 | |
| 4188 | if (object->IsUndefined() || object->IsNull()) { |
| 4189 | Handle<Object> args[2] = { key, object }; |
| 4190 | Handle<Object> error = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4191 | isolate->factory()->NewTypeError("non_object_property_load", |
| 4192 | HandleVector(args, 2)); |
| 4193 | return isolate->Throw(*error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4194 | } |
| 4195 | |
| 4196 | // Check if the given key is an array index. |
| 4197 | uint32_t index; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4198 | if (key->ToArrayIndex(&index)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4199 | return GetElementOrCharAt(isolate, object, index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4200 | } |
| 4201 | |
| 4202 | // Convert the key to a string - possibly by calling back into JavaScript. |
| 4203 | Handle<String> name; |
| 4204 | if (key->IsString()) { |
| 4205 | name = Handle<String>::cast(key); |
| 4206 | } else { |
| 4207 | bool has_pending_exception = false; |
| 4208 | Handle<Object> converted = |
| 4209 | Execution::ToString(key, &has_pending_exception); |
| 4210 | if (has_pending_exception) return Failure::Exception(); |
| 4211 | name = Handle<String>::cast(converted); |
| 4212 | } |
| 4213 | |
| 4214 | // Check if the name is trivially convertible to an index and get |
| 4215 | // the element if so. |
| 4216 | if (name->AsArrayIndex(&index)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4217 | return GetElementOrCharAt(isolate, object, index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4218 | } else { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4219 | return object->GetProperty(*name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4220 | } |
| 4221 | } |
| 4222 | |
| 4223 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4224 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetProperty) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4225 | NoHandleAllocation ha; |
| 4226 | ASSERT(args.length() == 2); |
| 4227 | |
| 4228 | Handle<Object> object = args.at<Object>(0); |
| 4229 | Handle<Object> key = args.at<Object>(1); |
| 4230 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4231 | return Runtime::GetObjectProperty(isolate, object, key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4232 | } |
| 4233 | |
| 4234 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4235 | // KeyedStringGetProperty is called from KeyedLoadIC::GenerateGeneric. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4236 | RUNTIME_FUNCTION(MaybeObject*, Runtime_KeyedGetProperty) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4237 | NoHandleAllocation ha; |
| 4238 | ASSERT(args.length() == 2); |
| 4239 | |
| 4240 | // Fast cases for getting named properties of the receiver JSObject |
| 4241 | // itself. |
| 4242 | // |
| 4243 | // The global proxy objects has to be excluded since LocalLookup on |
| 4244 | // the global proxy object can return a valid result even though the |
| 4245 | // global proxy object never has properties. This is the case |
| 4246 | // because the global proxy object forwards everything to its hidden |
| 4247 | // prototype including local lookups. |
| 4248 | // |
| 4249 | // Additionally, we need to make sure that we do not cache results |
| 4250 | // for objects that require access checks. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4251 | if (args[0]->IsJSObject()) { |
| 4252 | if (!args[0]->IsJSGlobalProxy() && |
| 4253 | !args[0]->IsAccessCheckNeeded() && |
| 4254 | args[1]->IsString()) { |
| 4255 | JSObject* receiver = JSObject::cast(args[0]); |
| 4256 | String* key = String::cast(args[1]); |
| 4257 | if (receiver->HasFastProperties()) { |
| 4258 | // Attempt to use lookup cache. |
| 4259 | Map* receiver_map = receiver->map(); |
| 4260 | KeyedLookupCache* keyed_lookup_cache = isolate->keyed_lookup_cache(); |
| 4261 | int offset = keyed_lookup_cache->Lookup(receiver_map, key); |
| 4262 | if (offset != -1) { |
| 4263 | Object* value = receiver->FastPropertyAt(offset); |
| 4264 | return value->IsTheHole() |
| 4265 | ? isolate->heap()->undefined_value() |
| 4266 | : value; |
| 4267 | } |
| 4268 | // Lookup cache miss. Perform lookup and update the cache if |
| 4269 | // appropriate. |
| 4270 | LookupResult result(isolate); |
| 4271 | receiver->LocalLookup(key, &result); |
| 4272 | if (result.IsFound() && result.type() == FIELD) { |
| 4273 | int offset = result.GetFieldIndex(); |
| 4274 | keyed_lookup_cache->Update(receiver_map, key, offset); |
| 4275 | return receiver->FastPropertyAt(offset); |
| 4276 | } |
| 4277 | } else { |
| 4278 | // Attempt dictionary lookup. |
| 4279 | StringDictionary* dictionary = receiver->property_dictionary(); |
| 4280 | int entry = dictionary->FindEntry(key); |
| 4281 | if ((entry != StringDictionary::kNotFound) && |
| 4282 | (dictionary->DetailsAt(entry).type() == NORMAL)) { |
| 4283 | Object* value = dictionary->ValueAt(entry); |
| 4284 | if (!receiver->IsGlobalObject()) return value; |
| 4285 | value = JSGlobalPropertyCell::cast(value)->value(); |
| 4286 | if (!value->IsTheHole()) return value; |
| 4287 | // If value is the hole do the general lookup. |
| 4288 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4289 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4290 | } else if (FLAG_smi_only_arrays && args.at<Object>(1)->IsSmi()) { |
| 4291 | // JSObject without a string key. If the key is a Smi, check for a |
| 4292 | // definite out-of-bounds access to elements, which is a strong indicator |
| 4293 | // that subsequent accesses will also call the runtime. Proactively |
| 4294 | // transition elements to FAST_ELEMENTS to avoid excessive boxing of |
| 4295 | // doubles for those future calls in the case that the elements would |
| 4296 | // become FAST_DOUBLE_ELEMENTS. |
| 4297 | Handle<JSObject> js_object(args.at<JSObject>(0)); |
| 4298 | ElementsKind elements_kind = js_object->GetElementsKind(); |
| 4299 | if (elements_kind == FAST_SMI_ONLY_ELEMENTS || |
| 4300 | elements_kind == FAST_DOUBLE_ELEMENTS) { |
| 4301 | FixedArrayBase* elements = js_object->elements(); |
| 4302 | if (args.at<Smi>(1)->value() >= elements->length()) { |
| 4303 | MaybeObject* maybe_object = TransitionElements(js_object, |
| 4304 | FAST_ELEMENTS, |
| 4305 | isolate); |
| 4306 | if (maybe_object->IsFailure()) return maybe_object; |
| 4307 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4308 | } |
| 4309 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4310 | } else if (args[0]->IsString() && args[1]->IsSmi()) { |
| 4311 | // Fast case for string indexing using [] with a smi index. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4312 | HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4313 | Handle<String> str = args.at<String>(0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4314 | int index = args.smi_at(1); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4315 | if (index >= 0 && index < str->length()) { |
| 4316 | Handle<Object> result = GetCharAt(str, index); |
| 4317 | return *result; |
| 4318 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4319 | } |
| 4320 | |
| 4321 | // Fall back to GetObjectProperty. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4322 | return Runtime::GetObjectProperty(isolate, |
| 4323 | args.at<Object>(0), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4324 | args.at<Object>(1)); |
| 4325 | } |
| 4326 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4327 | |
| 4328 | static bool IsValidAccessor(Handle<Object> obj) { |
| 4329 | return obj->IsUndefined() || obj->IsSpecFunction() || obj->IsNull(); |
| 4330 | } |
| 4331 | |
| 4332 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4333 | // Implements part of 8.12.9 DefineOwnProperty. |
| 4334 | // There are 3 cases that lead here: |
| 4335 | // Step 4b - define a new accessor property. |
| 4336 | // Steps 9c & 12 - replace an existing data property with an accessor property. |
| 4337 | // Step 12 - update an existing accessor property with an accessor or generic |
| 4338 | // descriptor. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4339 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DefineOrRedefineAccessorProperty) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4340 | ASSERT(args.length() == 5); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4341 | HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4342 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 4343 | RUNTIME_ASSERT(!obj->IsNull()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4344 | CONVERT_ARG_HANDLE_CHECKED(String, name, 1); |
| 4345 | CONVERT_ARG_HANDLE_CHECKED(Object, getter, 2); |
| 4346 | RUNTIME_ASSERT(IsValidAccessor(getter)); |
| 4347 | CONVERT_ARG_HANDLE_CHECKED(Object, setter, 3); |
| 4348 | RUNTIME_ASSERT(IsValidAccessor(setter)); |
| 4349 | CONVERT_SMI_ARG_CHECKED(unchecked, 4); |
| 4350 | RUNTIME_ASSERT((unchecked & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 4351 | PropertyAttributes attr = static_cast<PropertyAttributes>(unchecked); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4352 | |
| 4353 | bool fast = obj->HasFastProperties(); |
| 4354 | JSObject::DefineAccessor(obj, name, getter, setter, attr); |
| 4355 | if (fast) JSObject::TransformToFastProperties(obj, 0); |
| 4356 | return isolate->heap()->undefined_value(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4357 | } |
| 4358 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4359 | // Implements part of 8.12.9 DefineOwnProperty. |
| 4360 | // There are 3 cases that lead here: |
| 4361 | // Step 4a - define a new data property. |
| 4362 | // Steps 9b & 12 - replace an existing accessor property with a data property. |
| 4363 | // Step 12 - update an existing data property with a data or generic |
| 4364 | // descriptor. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4365 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DefineOrRedefineDataProperty) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4366 | ASSERT(args.length() == 4); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4367 | HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4368 | CONVERT_ARG_HANDLE_CHECKED(JSObject, js_object, 0); |
| 4369 | CONVERT_ARG_HANDLE_CHECKED(String, name, 1); |
| 4370 | CONVERT_ARG_HANDLE_CHECKED(Object, obj_value, 2); |
| 4371 | CONVERT_SMI_ARG_CHECKED(unchecked, 3); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4372 | RUNTIME_ASSERT((unchecked & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4373 | PropertyAttributes attr = static_cast<PropertyAttributes>(unchecked); |
| 4374 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4375 | LookupResult result(isolate); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4376 | js_object->LocalLookupRealNamedProperty(*name, &result); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4377 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4378 | // Special case for callback properties. |
| 4379 | if (result.IsFound() && result.type() == CALLBACKS) { |
| 4380 | Object* callback = result.GetCallbackObject(); |
| 4381 | // To be compatible with Safari we do not change the value on API objects |
| 4382 | // in Object.defineProperty(). Firefox disagrees here, and actually changes |
| 4383 | // the value. |
| 4384 | if (callback->IsAccessorInfo()) { |
| 4385 | return isolate->heap()->undefined_value(); |
| 4386 | } |
| 4387 | // Avoid redefining foreign callback as data property, just use the stored |
| 4388 | // setter to update the value instead. |
| 4389 | // TODO(mstarzinger): So far this only works if property attributes don't |
| 4390 | // change, this should be fixed once we cleanup the underlying code. |
| 4391 | if (callback->IsForeign() && result.GetAttributes() == attr) { |
| 4392 | return js_object->SetPropertyWithCallback(callback, |
| 4393 | *name, |
| 4394 | *obj_value, |
| 4395 | result.holder(), |
| 4396 | kStrictMode); |
| 4397 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4398 | } |
| 4399 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4400 | // Take special care when attributes are different and there is already |
| 4401 | // a property. For simplicity we normalize the property which enables us |
| 4402 | // to not worry about changing the instance_descriptor and creating a new |
| 4403 | // map. The current version of SetObjectProperty does not handle attributes |
| 4404 | // correctly in the case where a property is a field and is reset with |
| 4405 | // new attributes. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4406 | if (result.IsProperty() && |
| 4407 | (attr != result.GetAttributes() || result.type() == CALLBACKS)) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4408 | // New attributes - normalize to avoid writing to instance descriptor |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4409 | if (js_object->IsJSGlobalProxy()) { |
| 4410 | // Since the result is a property, the prototype will exist so |
| 4411 | // we don't have to check for null. |
| 4412 | js_object = Handle<JSObject>(JSObject::cast(js_object->GetPrototype())); |
| 4413 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4414 | JSObject::NormalizeProperties(js_object, CLEAR_INOBJECT_PROPERTIES, 0); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 4415 | // Use IgnoreAttributes version since a readonly property may be |
| 4416 | // overridden and SetProperty does not allow this. |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 4417 | return js_object->SetLocalPropertyIgnoreAttributes(*name, |
| 4418 | *obj_value, |
| 4419 | attr); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4420 | } |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4421 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4422 | return Runtime::ForceSetObjectProperty(isolate, |
| 4423 | js_object, |
| 4424 | name, |
| 4425 | obj_value, |
| 4426 | attr); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
| 4429 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4430 | MaybeObject* Runtime::SetObjectProperty(Isolate* isolate, |
| 4431 | Handle<Object> object, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4432 | Handle<Object> key, |
| 4433 | Handle<Object> value, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4434 | PropertyAttributes attr, |
| 4435 | StrictModeFlag strict_mode) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4436 | SetPropertyMode set_mode = attr == NONE ? SET_PROPERTY : DEFINE_PROPERTY; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4437 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4438 | |
| 4439 | if (object->IsUndefined() || object->IsNull()) { |
| 4440 | Handle<Object> args[2] = { key, object }; |
| 4441 | Handle<Object> error = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4442 | isolate->factory()->NewTypeError("non_object_property_store", |
| 4443 | HandleVector(args, 2)); |
| 4444 | return isolate->Throw(*error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4445 | } |
| 4446 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4447 | if (object->IsJSProxy()) { |
| 4448 | bool has_pending_exception = false; |
| 4449 | Handle<Object> name = Execution::ToString(key, &has_pending_exception); |
| 4450 | if (has_pending_exception) return Failure::Exception(); |
| 4451 | return JSProxy::cast(*object)->SetProperty( |
| 4452 | String::cast(*name), *value, attr, strict_mode); |
| 4453 | } |
| 4454 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4455 | // If the object isn't a JavaScript object, we ignore the store. |
| 4456 | if (!object->IsJSObject()) return *value; |
| 4457 | |
| 4458 | Handle<JSObject> js_object = Handle<JSObject>::cast(object); |
| 4459 | |
| 4460 | // Check if the given key is an array index. |
| 4461 | uint32_t index; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4462 | if (key->ToArrayIndex(&index)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4463 | // In Firefox/SpiderMonkey, Safari and Opera you can access the characters |
| 4464 | // of a string using [] notation. We need to support this too in |
| 4465 | // JavaScript. |
| 4466 | // In the case of a String object we just need to redirect the assignment to |
| 4467 | // the underlying string if the index is in range. Since the underlying |
| 4468 | // string does nothing with the assignment then we can ignore such |
| 4469 | // assignments. |
| 4470 | if (js_object->IsStringObjectWithCharacterAt(index)) { |
| 4471 | return *value; |
| 4472 | } |
| 4473 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4474 | Handle<Object> result = JSObject::SetElement( |
| 4475 | js_object, index, value, attr, strict_mode, set_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4476 | if (result.is_null()) return Failure::Exception(); |
| 4477 | return *value; |
| 4478 | } |
| 4479 | |
| 4480 | if (key->IsString()) { |
| 4481 | Handle<Object> result; |
| 4482 | if (Handle<String>::cast(key)->AsArrayIndex(&index)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4483 | result = JSObject::SetElement( |
| 4484 | js_object, index, value, attr, strict_mode, set_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4485 | } else { |
| 4486 | Handle<String> key_string = Handle<String>::cast(key); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4487 | key_string->TryFlatten(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4488 | result = JSReceiver::SetProperty( |
| 4489 | js_object, key_string, value, attr, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4490 | } |
| 4491 | if (result.is_null()) return Failure::Exception(); |
| 4492 | return *value; |
| 4493 | } |
| 4494 | |
| 4495 | // Call-back into JavaScript to convert the key to a string. |
| 4496 | bool has_pending_exception = false; |
| 4497 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4498 | if (has_pending_exception) return Failure::Exception(); |
| 4499 | Handle<String> name = Handle<String>::cast(converted); |
| 4500 | |
| 4501 | if (name->AsArrayIndex(&index)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4502 | return js_object->SetElement( |
| 4503 | index, *value, attr, strict_mode, true, set_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4504 | } else { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4505 | return js_object->SetProperty(*name, *value, attr, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4506 | } |
| 4507 | } |
| 4508 | |
| 4509 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4510 | MaybeObject* Runtime::ForceSetObjectProperty(Isolate* isolate, |
| 4511 | Handle<JSObject> js_object, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4512 | Handle<Object> key, |
| 4513 | Handle<Object> value, |
| 4514 | PropertyAttributes attr) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4515 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4516 | |
| 4517 | // Check if the given key is an array index. |
| 4518 | uint32_t index; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4519 | if (key->ToArrayIndex(&index)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4520 | // In Firefox/SpiderMonkey, Safari and Opera you can access the characters |
| 4521 | // of a string using [] notation. We need to support this too in |
| 4522 | // JavaScript. |
| 4523 | // In the case of a String object we just need to redirect the assignment to |
| 4524 | // the underlying string if the index is in range. Since the underlying |
| 4525 | // string does nothing with the assignment then we can ignore such |
| 4526 | // assignments. |
| 4527 | if (js_object->IsStringObjectWithCharacterAt(index)) { |
| 4528 | return *value; |
| 4529 | } |
| 4530 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4531 | return js_object->SetElement( |
| 4532 | index, *value, attr, kNonStrictMode, false, DEFINE_PROPERTY); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
| 4535 | if (key->IsString()) { |
| 4536 | if (Handle<String>::cast(key)->AsArrayIndex(&index)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4537 | return js_object->SetElement( |
| 4538 | index, *value, attr, kNonStrictMode, false, DEFINE_PROPERTY); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4539 | } else { |
| 4540 | Handle<String> key_string = Handle<String>::cast(key); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4541 | key_string->TryFlatten(); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 4542 | return js_object->SetLocalPropertyIgnoreAttributes(*key_string, |
| 4543 | *value, |
| 4544 | attr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4545 | } |
| 4546 | } |
| 4547 | |
| 4548 | // Call-back into JavaScript to convert the key to a string. |
| 4549 | bool has_pending_exception = false; |
| 4550 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4551 | if (has_pending_exception) return Failure::Exception(); |
| 4552 | Handle<String> name = Handle<String>::cast(converted); |
| 4553 | |
| 4554 | if (name->AsArrayIndex(&index)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4555 | return js_object->SetElement( |
| 4556 | index, *value, attr, kNonStrictMode, false, DEFINE_PROPERTY); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4557 | } else { |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 4558 | return js_object->SetLocalPropertyIgnoreAttributes(*name, *value, attr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4559 | } |
| 4560 | } |
| 4561 | |
| 4562 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4563 | MaybeObject* Runtime::ForceDeleteObjectProperty(Isolate* isolate, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4564 | Handle<JSReceiver> receiver, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4565 | Handle<Object> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4566 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4567 | |
| 4568 | // Check if the given key is an array index. |
| 4569 | uint32_t index; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4570 | if (key->ToArrayIndex(&index)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4571 | // In Firefox/SpiderMonkey, Safari and Opera you can access the |
| 4572 | // characters of a string using [] notation. In the case of a |
| 4573 | // String object we just need to redirect the deletion to the |
| 4574 | // underlying string if the index is in range. Since the |
| 4575 | // underlying string does nothing with the deletion, we can ignore |
| 4576 | // such deletions. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4577 | if (receiver->IsStringObjectWithCharacterAt(index)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4578 | return isolate->heap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4579 | } |
| 4580 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4581 | return receiver->DeleteElement(index, JSReceiver::FORCE_DELETION); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4582 | } |
| 4583 | |
| 4584 | Handle<String> key_string; |
| 4585 | if (key->IsString()) { |
| 4586 | key_string = Handle<String>::cast(key); |
| 4587 | } else { |
| 4588 | // Call-back into JavaScript to convert the key to a string. |
| 4589 | bool has_pending_exception = false; |
| 4590 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4591 | if (has_pending_exception) return Failure::Exception(); |
| 4592 | key_string = Handle<String>::cast(converted); |
| 4593 | } |
| 4594 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4595 | key_string->TryFlatten(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4596 | return receiver->DeleteProperty(*key_string, JSReceiver::FORCE_DELETION); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4597 | } |
| 4598 | |
| 4599 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4600 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetProperty) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4601 | NoHandleAllocation ha; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4602 | RUNTIME_ASSERT(args.length() == 4 || args.length() == 5); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4603 | |
| 4604 | Handle<Object> object = args.at<Object>(0); |
| 4605 | Handle<Object> key = args.at<Object>(1); |
| 4606 | Handle<Object> value = args.at<Object>(2); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4607 | CONVERT_SMI_ARG_CHECKED(unchecked_attributes, 3); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4608 | RUNTIME_ASSERT( |
| 4609 | (unchecked_attributes & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4610 | // Compute attributes. |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4611 | PropertyAttributes attributes = |
| 4612 | static_cast<PropertyAttributes>(unchecked_attributes); |
| 4613 | |
| 4614 | StrictModeFlag strict_mode = kNonStrictMode; |
| 4615 | if (args.length() == 5) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4616 | CONVERT_STRICT_MODE_ARG_CHECKED(strict_mode_flag, 4); |
| 4617 | strict_mode = strict_mode_flag; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4618 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4619 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4620 | return Runtime::SetObjectProperty(isolate, |
| 4621 | object, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4622 | key, |
| 4623 | value, |
| 4624 | attributes, |
| 4625 | strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4626 | } |
| 4627 | |
| 4628 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4629 | RUNTIME_FUNCTION(MaybeObject*, Runtime_TransitionElementsSmiToDouble) { |
| 4630 | NoHandleAllocation ha; |
| 4631 | RUNTIME_ASSERT(args.length() == 1); |
| 4632 | Handle<Object> object = args.at<Object>(0); |
| 4633 | return TransitionElements(object, FAST_DOUBLE_ELEMENTS, isolate); |
| 4634 | } |
| 4635 | |
| 4636 | |
| 4637 | RUNTIME_FUNCTION(MaybeObject*, Runtime_TransitionElementsDoubleToObject) { |
| 4638 | NoHandleAllocation ha; |
| 4639 | RUNTIME_ASSERT(args.length() == 1); |
| 4640 | Handle<Object> object = args.at<Object>(0); |
| 4641 | return TransitionElements(object, FAST_ELEMENTS, isolate); |
| 4642 | } |
| 4643 | |
| 4644 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4645 | // Set the native flag on the function. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4646 | // This is used to decide if we should transform null and undefined |
| 4647 | // into the global object when doing call and apply. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4648 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetNativeFlag) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4649 | NoHandleAllocation ha; |
| 4650 | RUNTIME_ASSERT(args.length() == 1); |
| 4651 | |
| 4652 | Handle<Object> object = args.at<Object>(0); |
| 4653 | |
| 4654 | if (object->IsJSFunction()) { |
| 4655 | JSFunction* func = JSFunction::cast(*object); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4656 | func->shared()->set_native(true); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4657 | } |
| 4658 | return isolate->heap()->undefined_value(); |
| 4659 | } |
| 4660 | |
| 4661 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4662 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StoreArrayLiteralElement) { |
| 4663 | RUNTIME_ASSERT(args.length() == 5); |
| 4664 | CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); |
| 4665 | CONVERT_SMI_ARG_CHECKED(store_index, 1); |
| 4666 | Handle<Object> value = args.at<Object>(2); |
| 4667 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, literals, 3); |
| 4668 | CONVERT_SMI_ARG_CHECKED(literal_index, 4); |
| 4669 | HandleScope scope; |
| 4670 | |
| 4671 | Object* raw_boilerplate_object = literals->get(literal_index); |
| 4672 | Handle<JSArray> boilerplate_object(JSArray::cast(raw_boilerplate_object)); |
| 4673 | #if DEBUG |
| 4674 | ElementsKind elements_kind = object->GetElementsKind(); |
| 4675 | #endif |
| 4676 | ASSERT(elements_kind <= FAST_DOUBLE_ELEMENTS); |
| 4677 | // Smis should never trigger transitions. |
| 4678 | ASSERT(!value->IsSmi()); |
| 4679 | |
| 4680 | if (value->IsNumber()) { |
| 4681 | ASSERT(elements_kind == FAST_SMI_ONLY_ELEMENTS); |
| 4682 | JSObject::TransitionElementsKind(object, FAST_DOUBLE_ELEMENTS); |
| 4683 | JSObject::TransitionElementsKind(boilerplate_object, FAST_DOUBLE_ELEMENTS); |
| 4684 | ASSERT(object->GetElementsKind() == FAST_DOUBLE_ELEMENTS); |
| 4685 | FixedDoubleArray* double_array = |
| 4686 | FixedDoubleArray::cast(object->elements()); |
| 4687 | HeapNumber* number = HeapNumber::cast(*value); |
| 4688 | double_array->set(store_index, number->Number()); |
| 4689 | } else { |
| 4690 | ASSERT(elements_kind == FAST_SMI_ONLY_ELEMENTS || |
| 4691 | elements_kind == FAST_DOUBLE_ELEMENTS); |
| 4692 | JSObject::TransitionElementsKind(object, FAST_ELEMENTS); |
| 4693 | JSObject::TransitionElementsKind(boilerplate_object, FAST_ELEMENTS); |
| 4694 | FixedArray* object_array = |
| 4695 | FixedArray::cast(object->elements()); |
| 4696 | object_array->set(store_index, *value); |
| 4697 | } |
| 4698 | return *object; |
| 4699 | } |
| 4700 | |
| 4701 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4702 | // Set a local property, even if it is READ_ONLY. If the property does not |
| 4703 | // exist, it will be added with attributes NONE. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4704 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IgnoreAttributesAndSetProperty) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4705 | NoHandleAllocation ha; |
| 4706 | RUNTIME_ASSERT(args.length() == 3 || args.length() == 4); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4707 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
| 4708 | CONVERT_ARG_CHECKED(String, name, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4709 | // Compute attributes. |
| 4710 | PropertyAttributes attributes = NONE; |
| 4711 | if (args.length() == 4) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4712 | CONVERT_SMI_ARG_CHECKED(unchecked_value, 3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4713 | // Only attribute bits should be set. |
| 4714 | RUNTIME_ASSERT( |
| 4715 | (unchecked_value & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 4716 | attributes = static_cast<PropertyAttributes>(unchecked_value); |
| 4717 | } |
| 4718 | |
| 4719 | return object-> |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 4720 | SetLocalPropertyIgnoreAttributes(name, args[2], attributes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4721 | } |
| 4722 | |
| 4723 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4724 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeleteProperty) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4725 | NoHandleAllocation ha; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4726 | ASSERT(args.length() == 3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4727 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4728 | CONVERT_ARG_CHECKED(JSReceiver, object, 0); |
| 4729 | CONVERT_ARG_CHECKED(String, key, 1); |
| 4730 | CONVERT_STRICT_MODE_ARG_CHECKED(strict_mode, 2); |
| 4731 | return object->DeleteProperty(key, (strict_mode == kStrictMode) |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4732 | ? JSReceiver::STRICT_DELETION |
| 4733 | : JSReceiver::NORMAL_DELETION); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4734 | } |
| 4735 | |
| 4736 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4737 | static Object* HasLocalPropertyImplementation(Isolate* isolate, |
| 4738 | Handle<JSObject> object, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4739 | Handle<String> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4740 | if (object->HasLocalProperty(*key)) return isolate->heap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4741 | // Handle hidden prototypes. If there's a hidden prototype above this thing |
| 4742 | // then we have to check it for properties, because they are supposed to |
| 4743 | // look like they are on this object. |
| 4744 | Handle<Object> proto(object->GetPrototype()); |
| 4745 | if (proto->IsJSObject() && |
| 4746 | Handle<JSObject>::cast(proto)->map()->is_hidden_prototype()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4747 | return HasLocalPropertyImplementation(isolate, |
| 4748 | Handle<JSObject>::cast(proto), |
| 4749 | key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4750 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4751 | return isolate->heap()->false_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4755 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasLocalProperty) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4756 | NoHandleAllocation ha; |
| 4757 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4758 | CONVERT_ARG_CHECKED(String, key, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4759 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4760 | uint32_t index; |
| 4761 | const bool key_is_array_index = key->AsArrayIndex(&index); |
| 4762 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4763 | Object* obj = args[0]; |
| 4764 | // Only JS objects can have properties. |
| 4765 | if (obj->IsJSObject()) { |
| 4766 | JSObject* object = JSObject::cast(obj); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4767 | // Fast case: either the key is a real named property or it is not |
| 4768 | // an array index and there are no interceptors or hidden |
| 4769 | // prototypes. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4770 | if (object->HasRealNamedProperty(key)) return isolate->heap()->true_value(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4771 | Map* map = object->map(); |
| 4772 | if (!key_is_array_index && |
| 4773 | !map->has_named_interceptor() && |
| 4774 | !HeapObject::cast(map->prototype())->map()->is_hidden_prototype()) { |
| 4775 | return isolate->heap()->false_value(); |
| 4776 | } |
| 4777 | // Slow case. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4778 | HandleScope scope(isolate); |
| 4779 | return HasLocalPropertyImplementation(isolate, |
| 4780 | Handle<JSObject>(object), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4781 | Handle<String>(key)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4782 | } else if (obj->IsString() && key_is_array_index) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4783 | // Well, there is one exception: Handle [] on strings. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4784 | String* string = String::cast(obj); |
| 4785 | if (index < static_cast<uint32_t>(string->length())) { |
| 4786 | return isolate->heap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4787 | } |
| 4788 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4789 | return isolate->heap()->false_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
| 4792 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4793 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasProperty) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4794 | NoHandleAllocation na; |
| 4795 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4796 | CONVERT_ARG_CHECKED(JSReceiver, receiver, 0); |
| 4797 | CONVERT_ARG_CHECKED(String, key, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4798 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4799 | bool result = receiver->HasProperty(key); |
| 4800 | if (isolate->has_pending_exception()) return Failure::Exception(); |
| 4801 | return isolate->heap()->ToBoolean(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4802 | } |
| 4803 | |
| 4804 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4805 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasElement) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4806 | NoHandleAllocation na; |
| 4807 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4808 | CONVERT_ARG_CHECKED(JSReceiver, receiver, 0); |
| 4809 | CONVERT_SMI_ARG_CHECKED(index, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4810 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4811 | bool result = receiver->HasElement(index); |
| 4812 | if (isolate->has_pending_exception()) return Failure::Exception(); |
| 4813 | return isolate->heap()->ToBoolean(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4814 | } |
| 4815 | |
| 4816 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4817 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsPropertyEnumerable) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4818 | NoHandleAllocation ha; |
| 4819 | ASSERT(args.length() == 2); |
| 4820 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4821 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
| 4822 | CONVERT_ARG_CHECKED(String, key, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4823 | |
| 4824 | uint32_t index; |
| 4825 | if (key->AsArrayIndex(&index)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4826 | JSObject::LocalElementType type = object->HasLocalElement(index); |
| 4827 | switch (type) { |
| 4828 | case JSObject::UNDEFINED_ELEMENT: |
| 4829 | case JSObject::STRING_CHARACTER_ELEMENT: |
| 4830 | return isolate->heap()->false_value(); |
| 4831 | case JSObject::INTERCEPTED_ELEMENT: |
| 4832 | case JSObject::FAST_ELEMENT: |
| 4833 | return isolate->heap()->true_value(); |
| 4834 | case JSObject::DICTIONARY_ELEMENT: { |
| 4835 | if (object->IsJSGlobalProxy()) { |
| 4836 | Object* proto = object->GetPrototype(); |
| 4837 | if (proto->IsNull()) { |
| 4838 | return isolate->heap()->false_value(); |
| 4839 | } |
| 4840 | ASSERT(proto->IsJSGlobalObject()); |
| 4841 | object = JSObject::cast(proto); |
| 4842 | } |
| 4843 | FixedArray* elements = FixedArray::cast(object->elements()); |
| 4844 | SeededNumberDictionary* dictionary = NULL; |
| 4845 | if (elements->map() == |
| 4846 | isolate->heap()->non_strict_arguments_elements_map()) { |
| 4847 | dictionary = SeededNumberDictionary::cast(elements->get(1)); |
| 4848 | } else { |
| 4849 | dictionary = SeededNumberDictionary::cast(elements); |
| 4850 | } |
| 4851 | int entry = dictionary->FindEntry(index); |
| 4852 | ASSERT(entry != SeededNumberDictionary::kNotFound); |
| 4853 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 4854 | return isolate->heap()->ToBoolean(!details.IsDontEnum()); |
| 4855 | } |
| 4856 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4857 | } |
| 4858 | |
| 4859 | PropertyAttributes att = object->GetLocalPropertyAttribute(key); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4860 | return isolate->heap()->ToBoolean(att != ABSENT && (att & DONT_ENUM) == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4861 | } |
| 4862 | |
| 4863 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4864 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetPropertyNames) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4865 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4866 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4867 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, object, 0); |
| 4868 | bool threw = false; |
| 4869 | Handle<JSArray> result = GetKeysFor(object, &threw); |
| 4870 | if (threw) return Failure::Exception(); |
| 4871 | return *result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4872 | } |
| 4873 | |
| 4874 | |
| 4875 | // Returns either a FixedArray as Runtime_GetPropertyNames, |
| 4876 | // or, if the given object has an enum cache that contains |
| 4877 | // all enumerable properties of the object and its prototypes |
| 4878 | // have none, the map of the object. This is used to speed up |
| 4879 | // the check for deletions during a for-in. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4880 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetPropertyNamesFast) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4881 | ASSERT(args.length() == 1); |
| 4882 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4883 | CONVERT_ARG_CHECKED(JSReceiver, raw_object, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4884 | |
| 4885 | if (raw_object->IsSimpleEnum()) return raw_object->map(); |
| 4886 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4887 | HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4888 | Handle<JSReceiver> object(raw_object); |
| 4889 | bool threw = false; |
| 4890 | Handle<FixedArray> content = |
| 4891 | GetKeysInFixedArrayFor(object, INCLUDE_PROTOS, &threw); |
| 4892 | if (threw) return Failure::Exception(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4893 | |
| 4894 | // Test again, since cache may have been built by preceding call. |
| 4895 | if (object->IsSimpleEnum()) return object->map(); |
| 4896 | |
| 4897 | return *content; |
| 4898 | } |
| 4899 | |
| 4900 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4901 | // Find the length of the prototype chain that is to to handled as one. If a |
| 4902 | // prototype object is hidden it is to be viewed as part of the the object it |
| 4903 | // is prototype for. |
| 4904 | static int LocalPrototypeChainLength(JSObject* obj) { |
| 4905 | int count = 1; |
| 4906 | Object* proto = obj->GetPrototype(); |
| 4907 | while (proto->IsJSObject() && |
| 4908 | JSObject::cast(proto)->map()->is_hidden_prototype()) { |
| 4909 | count++; |
| 4910 | proto = JSObject::cast(proto)->GetPrototype(); |
| 4911 | } |
| 4912 | return count; |
| 4913 | } |
| 4914 | |
| 4915 | |
| 4916 | // Return the names of the local named properties. |
| 4917 | // args[0]: object |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4918 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLocalPropertyNames) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4919 | HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4920 | ASSERT(args.length() == 1); |
| 4921 | if (!args[0]->IsJSObject()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4922 | return isolate->heap()->undefined_value(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4923 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4924 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4925 | |
| 4926 | // Skip the global proxy as it has no properties and always delegates to the |
| 4927 | // real global object. |
| 4928 | if (obj->IsJSGlobalProxy()) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 4929 | // Only collect names if access is permitted. |
| 4930 | if (obj->IsAccessCheckNeeded() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4931 | !isolate->MayNamedAccess(*obj, |
| 4932 | isolate->heap()->undefined_value(), |
| 4933 | v8::ACCESS_KEYS)) { |
| 4934 | isolate->ReportFailedAccessCheck(*obj, v8::ACCESS_KEYS); |
| 4935 | return *isolate->factory()->NewJSArray(0); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 4936 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4937 | obj = Handle<JSObject>(JSObject::cast(obj->GetPrototype())); |
| 4938 | } |
| 4939 | |
| 4940 | // Find the number of objects making up this. |
| 4941 | int length = LocalPrototypeChainLength(*obj); |
| 4942 | |
| 4943 | // Find the number of local properties for each of the objects. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4944 | ScopedVector<int> local_property_count(length); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4945 | int total_property_count = 0; |
| 4946 | Handle<JSObject> jsproto = obj; |
| 4947 | for (int i = 0; i < length; i++) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 4948 | // Only collect names if access is permitted. |
| 4949 | if (jsproto->IsAccessCheckNeeded() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4950 | !isolate->MayNamedAccess(*jsproto, |
| 4951 | isolate->heap()->undefined_value(), |
| 4952 | v8::ACCESS_KEYS)) { |
| 4953 | isolate->ReportFailedAccessCheck(*jsproto, v8::ACCESS_KEYS); |
| 4954 | return *isolate->factory()->NewJSArray(0); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 4955 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4956 | int n; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4957 | n = jsproto->NumberOfLocalProperties(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4958 | local_property_count[i] = n; |
| 4959 | total_property_count += n; |
| 4960 | if (i < length - 1) { |
| 4961 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 4962 | } |
| 4963 | } |
| 4964 | |
| 4965 | // Allocate an array with storage for all the property names. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4966 | Handle<FixedArray> names = |
| 4967 | isolate->factory()->NewFixedArray(total_property_count); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4968 | |
| 4969 | // Get the property names. |
| 4970 | jsproto = obj; |
| 4971 | int proto_with_hidden_properties = 0; |
Ben Murdoch | 6d7cb00 | 2011-08-04 19:25:22 +0100 | [diff] [blame] | 4972 | int next_copy_index = 0; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4973 | for (int i = 0; i < length; i++) { |
Ben Murdoch | 6d7cb00 | 2011-08-04 19:25:22 +0100 | [diff] [blame] | 4974 | jsproto->GetLocalPropertyNames(*names, next_copy_index); |
| 4975 | next_copy_index += local_property_count[i]; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 4976 | if (jsproto->HasHiddenProperties()) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4977 | proto_with_hidden_properties++; |
| 4978 | } |
| 4979 | if (i < length - 1) { |
| 4980 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 4981 | } |
| 4982 | } |
| 4983 | |
| 4984 | // Filter out name of hidden propeties object. |
| 4985 | if (proto_with_hidden_properties > 0) { |
| 4986 | Handle<FixedArray> old_names = names; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4987 | names = isolate->factory()->NewFixedArray( |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4988 | names->length() - proto_with_hidden_properties); |
| 4989 | int dest_pos = 0; |
| 4990 | for (int i = 0; i < total_property_count; i++) { |
| 4991 | Object* name = old_names->get(i); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4992 | if (name == isolate->heap()->hidden_symbol()) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4993 | continue; |
| 4994 | } |
| 4995 | names->set(dest_pos++, name); |
| 4996 | } |
| 4997 | } |
| 4998 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4999 | return *isolate->factory()->NewJSArrayWithElements(names); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5000 | } |
| 5001 | |
| 5002 | |
| 5003 | // Return the names of the local indexed properties. |
| 5004 | // args[0]: object |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5005 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLocalElementNames) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5006 | HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5007 | ASSERT(args.length() == 1); |
| 5008 | if (!args[0]->IsJSObject()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5009 | return isolate->heap()->undefined_value(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5010 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5011 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5012 | |
| 5013 | int n = obj->NumberOfLocalElements(static_cast<PropertyAttributes>(NONE)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5014 | Handle<FixedArray> names = isolate->factory()->NewFixedArray(n); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5015 | obj->GetLocalElementKeys(*names, static_cast<PropertyAttributes>(NONE)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5016 | return *isolate->factory()->NewJSArrayWithElements(names); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
| 5019 | |
| 5020 | // Return information on whether an object has a named or indexed interceptor. |
| 5021 | // args[0]: object |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5022 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetInterceptorInfo) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5023 | HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5024 | ASSERT(args.length() == 1); |
| 5025 | if (!args[0]->IsJSObject()) { |
| 5026 | return Smi::FromInt(0); |
| 5027 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5028 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5029 | |
| 5030 | int result = 0; |
| 5031 | if (obj->HasNamedInterceptor()) result |= 2; |
| 5032 | if (obj->HasIndexedInterceptor()) result |= 1; |
| 5033 | |
| 5034 | return Smi::FromInt(result); |
| 5035 | } |
| 5036 | |
| 5037 | |
| 5038 | // Return property names from named interceptor. |
| 5039 | // args[0]: object |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5040 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetNamedInterceptorPropertyNames) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5041 | HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5042 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5043 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5044 | |
| 5045 | if (obj->HasNamedInterceptor()) { |
| 5046 | v8::Handle<v8::Array> result = GetKeysForNamedInterceptor(obj, obj); |
| 5047 | if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result); |
| 5048 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5049 | return isolate->heap()->undefined_value(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5050 | } |
| 5051 | |
| 5052 | |
| 5053 | // Return element names from indexed interceptor. |
| 5054 | // args[0]: object |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5055 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetIndexedInterceptorElementNames) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5056 | HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5057 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5058 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5059 | |
| 5060 | if (obj->HasIndexedInterceptor()) { |
| 5061 | v8::Handle<v8::Array> result = GetKeysForIndexedInterceptor(obj, obj); |
| 5062 | if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result); |
| 5063 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5064 | return isolate->heap()->undefined_value(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5065 | } |
| 5066 | |
| 5067 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5068 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LocalKeys) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5069 | ASSERT_EQ(args.length(), 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5070 | CONVERT_ARG_CHECKED(JSObject, raw_object, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5071 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5072 | Handle<JSObject> object(raw_object); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 5073 | |
| 5074 | if (object->IsJSGlobalProxy()) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5075 | // Do access checks before going to the global object. |
| 5076 | if (object->IsAccessCheckNeeded() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5077 | !isolate->MayNamedAccess(*object, isolate->heap()->undefined_value(), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5078 | v8::ACCESS_KEYS)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5079 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_KEYS); |
| 5080 | return *isolate->factory()->NewJSArray(0); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5081 | } |
| 5082 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 5083 | Handle<Object> proto(object->GetPrototype()); |
| 5084 | // If proxy is detached we simply return an empty array. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5085 | if (proto->IsNull()) return *isolate->factory()->NewJSArray(0); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 5086 | object = Handle<JSObject>::cast(proto); |
| 5087 | } |
| 5088 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5089 | bool threw = false; |
| 5090 | Handle<FixedArray> contents = |
| 5091 | GetKeysInFixedArrayFor(object, LOCAL_ONLY, &threw); |
| 5092 | if (threw) return Failure::Exception(); |
| 5093 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5094 | // Some fast paths through GetKeysInFixedArrayFor reuse a cached |
| 5095 | // property array and since the result is mutable we have to create |
| 5096 | // a fresh clone on each invocation. |
| 5097 | int length = contents->length(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5098 | Handle<FixedArray> copy = isolate->factory()->NewFixedArray(length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5099 | for (int i = 0; i < length; i++) { |
| 5100 | Object* entry = contents->get(i); |
| 5101 | if (entry->IsString()) { |
| 5102 | copy->set(i, entry); |
| 5103 | } else { |
| 5104 | ASSERT(entry->IsNumber()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5105 | HandleScope scope(isolate); |
| 5106 | Handle<Object> entry_handle(entry, isolate); |
| 5107 | Handle<Object> entry_str = |
| 5108 | isolate->factory()->NumberToString(entry_handle); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5109 | copy->set(i, *entry_str); |
| 5110 | } |
| 5111 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5112 | return *isolate->factory()->NewJSArrayWithElements(copy); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5113 | } |
| 5114 | |
| 5115 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5116 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetArgumentsProperty) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5117 | NoHandleAllocation ha; |
| 5118 | ASSERT(args.length() == 1); |
| 5119 | |
| 5120 | // Compute the frame holding the arguments. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5121 | JavaScriptFrameIterator it(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5122 | it.AdvanceToArgumentsFrame(); |
| 5123 | JavaScriptFrame* frame = it.frame(); |
| 5124 | |
| 5125 | // Get the actual number of provided arguments. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5126 | const uint32_t n = frame->ComputeParametersCount(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5127 | |
| 5128 | // Try to convert the key to an index. If successful and within |
| 5129 | // index return the the argument from the frame. |
| 5130 | uint32_t index; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5131 | if (args[0]->ToArrayIndex(&index) && index < n) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5132 | return frame->GetParameter(index); |
| 5133 | } |
| 5134 | |
| 5135 | // Convert the key to a string. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5136 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5137 | bool exception = false; |
| 5138 | Handle<Object> converted = |
| 5139 | Execution::ToString(args.at<Object>(0), &exception); |
| 5140 | if (exception) return Failure::Exception(); |
| 5141 | Handle<String> key = Handle<String>::cast(converted); |
| 5142 | |
| 5143 | // Try to convert the string key into an array index. |
| 5144 | if (key->AsArrayIndex(&index)) { |
| 5145 | if (index < n) { |
| 5146 | return frame->GetParameter(index); |
| 5147 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5148 | return isolate->initial_object_prototype()->GetElement(index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5149 | } |
| 5150 | } |
| 5151 | |
| 5152 | // Handle special arguments properties. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5153 | if (key->Equals(isolate->heap()->length_symbol())) return Smi::FromInt(n); |
| 5154 | if (key->Equals(isolate->heap()->callee_symbol())) { |
| 5155 | Object* function = frame->function(); |
| 5156 | if (function->IsJSFunction() && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5157 | !JSFunction::cast(function)->shared()->is_classic_mode()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5158 | return isolate->Throw(*isolate->factory()->NewTypeError( |
| 5159 | "strict_arguments_callee", HandleVector<Object>(NULL, 0))); |
| 5160 | } |
| 5161 | return function; |
| 5162 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5163 | |
| 5164 | // Lookup in the initial Object.prototype object. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5165 | return isolate->initial_object_prototype()->GetProperty(*key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5166 | } |
| 5167 | |
| 5168 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5169 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ToFastProperties) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5170 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5171 | Object* object = args[0]; |
| 5172 | return (object->IsJSObject() && !object->IsGlobalObject()) |
| 5173 | ? JSObject::cast(object)->TransformToFastProperties(0) |
| 5174 | : object; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5175 | } |
| 5176 | |
| 5177 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5178 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ToSlowProperties) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5179 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5180 | Object* obj = args[0]; |
| 5181 | return (obj->IsJSObject() && !obj->IsJSGlobalProxy()) |
| 5182 | ? JSObject::cast(obj)->NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0) |
| 5183 | : obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5184 | } |
| 5185 | |
| 5186 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5187 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ToBool) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5188 | NoHandleAllocation ha; |
| 5189 | ASSERT(args.length() == 1); |
| 5190 | |
| 5191 | return args[0]->ToBoolean(); |
| 5192 | } |
| 5193 | |
| 5194 | |
| 5195 | // Returns the type string of a value; see ECMA-262, 11.4.3 (p 47). |
| 5196 | // Possible optimizations: put the type string into the oddballs. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5197 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Typeof) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5198 | NoHandleAllocation ha; |
| 5199 | |
| 5200 | Object* obj = args[0]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5201 | if (obj->IsNumber()) return isolate->heap()->number_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5202 | HeapObject* heap_obj = HeapObject::cast(obj); |
| 5203 | |
| 5204 | // typeof an undetectable object is 'undefined' |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5205 | if (heap_obj->map()->is_undetectable()) { |
| 5206 | return isolate->heap()->undefined_symbol(); |
| 5207 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5208 | |
| 5209 | InstanceType instance_type = heap_obj->map()->instance_type(); |
| 5210 | if (instance_type < FIRST_NONSTRING_TYPE) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5211 | return isolate->heap()->string_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5212 | } |
| 5213 | |
| 5214 | switch (instance_type) { |
| 5215 | case ODDBALL_TYPE: |
| 5216 | if (heap_obj->IsTrue() || heap_obj->IsFalse()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5217 | return isolate->heap()->boolean_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5218 | } |
| 5219 | if (heap_obj->IsNull()) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5220 | return FLAG_harmony_typeof |
| 5221 | ? isolate->heap()->null_symbol() |
| 5222 | : isolate->heap()->object_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5223 | } |
| 5224 | ASSERT(heap_obj->IsUndefined()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5225 | return isolate->heap()->undefined_symbol(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5226 | case JS_FUNCTION_TYPE: |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 5227 | case JS_FUNCTION_PROXY_TYPE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5228 | return isolate->heap()->function_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5229 | default: |
| 5230 | // For any kind of object not handled above, the spec rule for |
| 5231 | // host objects gives that it is okay to return "object" |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5232 | return isolate->heap()->object_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5233 | } |
| 5234 | } |
| 5235 | |
| 5236 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5237 | static bool AreDigits(const char*s, int from, int to) { |
| 5238 | for (int i = from; i < to; i++) { |
| 5239 | if (s[i] < '0' || s[i] > '9') return false; |
| 5240 | } |
| 5241 | |
| 5242 | return true; |
| 5243 | } |
| 5244 | |
| 5245 | |
| 5246 | static int ParseDecimalInteger(const char*s, int from, int to) { |
| 5247 | ASSERT(to - from < 10); // Overflow is not possible. |
| 5248 | ASSERT(from < to); |
| 5249 | int d = s[from] - '0'; |
| 5250 | |
| 5251 | for (int i = from + 1; i < to; i++) { |
| 5252 | d = 10 * d + (s[i] - '0'); |
| 5253 | } |
| 5254 | |
| 5255 | return d; |
| 5256 | } |
| 5257 | |
| 5258 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5259 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToNumber) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5260 | NoHandleAllocation ha; |
| 5261 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5262 | CONVERT_ARG_CHECKED(String, subject, 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5263 | subject->TryFlatten(); |
| 5264 | |
| 5265 | // Fast case: short integer or some sorts of junk values. |
| 5266 | int len = subject->length(); |
| 5267 | if (subject->IsSeqAsciiString()) { |
| 5268 | if (len == 0) return Smi::FromInt(0); |
| 5269 | |
| 5270 | char const* data = SeqAsciiString::cast(subject)->GetChars(); |
| 5271 | bool minus = (data[0] == '-'); |
| 5272 | int start_pos = (minus ? 1 : 0); |
| 5273 | |
| 5274 | if (start_pos == len) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5275 | return isolate->heap()->nan_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5276 | } else if (data[start_pos] > '9') { |
| 5277 | // Fast check for a junk value. A valid string may start from a |
| 5278 | // whitespace, a sign ('+' or '-'), the decimal point, a decimal digit or |
| 5279 | // the 'I' character ('Infinity'). All of that have codes not greater than |
| 5280 | // '9' except 'I'. |
| 5281 | if (data[start_pos] != 'I') { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5282 | return isolate->heap()->nan_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5283 | } |
| 5284 | } else if (len - start_pos < 10 && AreDigits(data, start_pos, len)) { |
| 5285 | // The maximal/minimal smi has 10 digits. If the string has less digits we |
| 5286 | // know it will fit into the smi-data type. |
| 5287 | int d = ParseDecimalInteger(data, start_pos, len); |
| 5288 | if (minus) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5289 | if (d == 0) return isolate->heap()->minus_zero_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5290 | d = -d; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5291 | } else if (!subject->HasHashCode() && |
| 5292 | len <= String::kMaxArrayIndexSize && |
| 5293 | (len == 1 || data[0] != '0')) { |
| 5294 | // String hash is not calculated yet but all the data are present. |
| 5295 | // Update the hash field to speed up sequential convertions. |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 5296 | uint32_t hash = StringHasher::MakeArrayIndexHash(d, len); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5297 | #ifdef DEBUG |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5298 | subject->Hash(); // Force hash calculation. |
| 5299 | ASSERT_EQ(static_cast<int>(subject->hash_field()), |
| 5300 | static_cast<int>(hash)); |
| 5301 | #endif |
| 5302 | subject->set_hash_field(hash); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5303 | } |
| 5304 | return Smi::FromInt(d); |
| 5305 | } |
| 5306 | } |
| 5307 | |
| 5308 | // Slower case. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5309 | return isolate->heap()->NumberFromDouble( |
| 5310 | StringToDouble(isolate->unicode_cache(), subject, ALLOW_HEX)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5311 | } |
| 5312 | |
| 5313 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5314 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringFromCharCodeArray) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5315 | NoHandleAllocation ha; |
| 5316 | ASSERT(args.length() == 1); |
| 5317 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5318 | CONVERT_ARG_CHECKED(JSArray, codes, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5319 | int length = Smi::cast(codes->length())->value(); |
| 5320 | |
| 5321 | // Check if the string can be ASCII. |
| 5322 | int i; |
| 5323 | for (i = 0; i < length; i++) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5324 | Object* element; |
| 5325 | { MaybeObject* maybe_element = codes->GetElement(i); |
| 5326 | // We probably can't get an exception here, but just in order to enforce |
| 5327 | // the checking of inputs in the runtime calls we check here. |
| 5328 | if (!maybe_element->ToObject(&element)) return maybe_element; |
| 5329 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5330 | CONVERT_NUMBER_CHECKED(int, chr, Int32, element); |
| 5331 | if ((chr & 0xffff) > String::kMaxAsciiCharCode) |
| 5332 | break; |
| 5333 | } |
| 5334 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5335 | MaybeObject* maybe_object = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5336 | if (i == length) { // The string is ASCII. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5337 | maybe_object = isolate->heap()->AllocateRawAsciiString(length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5338 | } else { // The string is not ASCII. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5339 | maybe_object = isolate->heap()->AllocateRawTwoByteString(length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5340 | } |
| 5341 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5342 | Object* object = NULL; |
| 5343 | if (!maybe_object->ToObject(&object)) return maybe_object; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5344 | String* result = String::cast(object); |
| 5345 | for (int i = 0; i < length; i++) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5346 | Object* element; |
| 5347 | { MaybeObject* maybe_element = codes->GetElement(i); |
| 5348 | if (!maybe_element->ToObject(&element)) return maybe_element; |
| 5349 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5350 | CONVERT_NUMBER_CHECKED(int, chr, Int32, element); |
| 5351 | result->Set(i, chr & 0xffff); |
| 5352 | } |
| 5353 | return result; |
| 5354 | } |
| 5355 | |
| 5356 | |
| 5357 | // kNotEscaped is generated by the following: |
| 5358 | // |
| 5359 | // #!/bin/perl |
| 5360 | // for (my $i = 0; $i < 256; $i++) { |
| 5361 | // print "\n" if $i % 16 == 0; |
| 5362 | // my $c = chr($i); |
| 5363 | // my $escaped = 1; |
| 5364 | // $escaped = 0 if $c =~ m#[A-Za-z0-9@*_+./-]#; |
| 5365 | // print $escaped ? "0, " : "1, "; |
| 5366 | // } |
| 5367 | |
| 5368 | |
| 5369 | static bool IsNotEscaped(uint16_t character) { |
| 5370 | // Only for 8 bit characters, the rest are always escaped (in a different way) |
| 5371 | ASSERT(character < 256); |
| 5372 | static const char kNotEscaped[256] = { |
| 5373 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5374 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5375 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, |
| 5376 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, |
| 5377 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 5378 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, |
| 5379 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 5380 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, |
| 5381 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5382 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5383 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5384 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5385 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5386 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5387 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5388 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5389 | }; |
| 5390 | return kNotEscaped[character] != 0; |
| 5391 | } |
| 5392 | |
| 5393 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5394 | RUNTIME_FUNCTION(MaybeObject*, Runtime_URIEscape) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5395 | const char hex_chars[] = "0123456789ABCDEF"; |
| 5396 | NoHandleAllocation ha; |
| 5397 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5398 | CONVERT_ARG_CHECKED(String, source, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5399 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5400 | source->TryFlatten(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5401 | |
| 5402 | int escaped_length = 0; |
| 5403 | int length = source->length(); |
| 5404 | { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5405 | Access<StringInputBuffer> buffer( |
| 5406 | isolate->runtime_state()->string_input_buffer()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5407 | buffer->Reset(source); |
| 5408 | while (buffer->has_more()) { |
| 5409 | uint16_t character = buffer->GetNext(); |
| 5410 | if (character >= 256) { |
| 5411 | escaped_length += 6; |
| 5412 | } else if (IsNotEscaped(character)) { |
| 5413 | escaped_length++; |
| 5414 | } else { |
| 5415 | escaped_length += 3; |
| 5416 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5417 | // We don't allow strings that are longer than a maximal length. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5418 | ASSERT(String::kMaxLength < 0x7fffffff - 6); // Cannot overflow. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5419 | if (escaped_length > String::kMaxLength) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5420 | isolate->context()->mark_out_of_memory(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5421 | return Failure::OutOfMemoryException(); |
| 5422 | } |
| 5423 | } |
| 5424 | } |
| 5425 | // No length change implies no change. Return original string if no change. |
| 5426 | if (escaped_length == length) { |
| 5427 | return source; |
| 5428 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5429 | Object* o; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5430 | { MaybeObject* maybe_o = |
| 5431 | isolate->heap()->AllocateRawAsciiString(escaped_length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5432 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 5433 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5434 | String* destination = String::cast(o); |
| 5435 | int dest_position = 0; |
| 5436 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5437 | Access<StringInputBuffer> buffer( |
| 5438 | isolate->runtime_state()->string_input_buffer()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5439 | buffer->Rewind(); |
| 5440 | while (buffer->has_more()) { |
| 5441 | uint16_t chr = buffer->GetNext(); |
| 5442 | if (chr >= 256) { |
| 5443 | destination->Set(dest_position, '%'); |
| 5444 | destination->Set(dest_position+1, 'u'); |
| 5445 | destination->Set(dest_position+2, hex_chars[chr >> 12]); |
| 5446 | destination->Set(dest_position+3, hex_chars[(chr >> 8) & 0xf]); |
| 5447 | destination->Set(dest_position+4, hex_chars[(chr >> 4) & 0xf]); |
| 5448 | destination->Set(dest_position+5, hex_chars[chr & 0xf]); |
| 5449 | dest_position += 6; |
| 5450 | } else if (IsNotEscaped(chr)) { |
| 5451 | destination->Set(dest_position, chr); |
| 5452 | dest_position++; |
| 5453 | } else { |
| 5454 | destination->Set(dest_position, '%'); |
| 5455 | destination->Set(dest_position+1, hex_chars[chr >> 4]); |
| 5456 | destination->Set(dest_position+2, hex_chars[chr & 0xf]); |
| 5457 | dest_position += 3; |
| 5458 | } |
| 5459 | } |
| 5460 | return destination; |
| 5461 | } |
| 5462 | |
| 5463 | |
| 5464 | static inline int TwoDigitHex(uint16_t character1, uint16_t character2) { |
| 5465 | static const signed char kHexValue['g'] = { |
| 5466 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5467 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5468 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5469 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, |
| 5470 | -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5471 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5472 | -1, 10, 11, 12, 13, 14, 15 }; |
| 5473 | |
| 5474 | if (character1 > 'f') return -1; |
| 5475 | int hi = kHexValue[character1]; |
| 5476 | if (hi == -1) return -1; |
| 5477 | if (character2 > 'f') return -1; |
| 5478 | int lo = kHexValue[character2]; |
| 5479 | if (lo == -1) return -1; |
| 5480 | return (hi << 4) + lo; |
| 5481 | } |
| 5482 | |
| 5483 | |
| 5484 | static inline int Unescape(String* source, |
| 5485 | int i, |
| 5486 | int length, |
| 5487 | int* step) { |
| 5488 | uint16_t character = source->Get(i); |
| 5489 | int32_t hi = 0; |
| 5490 | int32_t lo = 0; |
| 5491 | if (character == '%' && |
| 5492 | i <= length - 6 && |
| 5493 | source->Get(i + 1) == 'u' && |
| 5494 | (hi = TwoDigitHex(source->Get(i + 2), |
| 5495 | source->Get(i + 3))) != -1 && |
| 5496 | (lo = TwoDigitHex(source->Get(i + 4), |
| 5497 | source->Get(i + 5))) != -1) { |
| 5498 | *step = 6; |
| 5499 | return (hi << 8) + lo; |
| 5500 | } else if (character == '%' && |
| 5501 | i <= length - 3 && |
| 5502 | (lo = TwoDigitHex(source->Get(i + 1), |
| 5503 | source->Get(i + 2))) != -1) { |
| 5504 | *step = 3; |
| 5505 | return lo; |
| 5506 | } else { |
| 5507 | *step = 1; |
| 5508 | return character; |
| 5509 | } |
| 5510 | } |
| 5511 | |
| 5512 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5513 | RUNTIME_FUNCTION(MaybeObject*, Runtime_URIUnescape) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5514 | NoHandleAllocation ha; |
| 5515 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5516 | CONVERT_ARG_CHECKED(String, source, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5517 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5518 | source->TryFlatten(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5519 | |
| 5520 | bool ascii = true; |
| 5521 | int length = source->length(); |
| 5522 | |
| 5523 | int unescaped_length = 0; |
| 5524 | for (int i = 0; i < length; unescaped_length++) { |
| 5525 | int step; |
| 5526 | if (Unescape(source, i, length, &step) > String::kMaxAsciiCharCode) { |
| 5527 | ascii = false; |
| 5528 | } |
| 5529 | i += step; |
| 5530 | } |
| 5531 | |
| 5532 | // No length change implies no change. Return original string if no change. |
| 5533 | if (unescaped_length == length) |
| 5534 | return source; |
| 5535 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5536 | Object* o; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5537 | { MaybeObject* maybe_o = |
| 5538 | ascii ? |
| 5539 | isolate->heap()->AllocateRawAsciiString(unescaped_length) : |
| 5540 | isolate->heap()->AllocateRawTwoByteString(unescaped_length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5541 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 5542 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5543 | String* destination = String::cast(o); |
| 5544 | |
| 5545 | int dest_position = 0; |
| 5546 | for (int i = 0; i < length; dest_position++) { |
| 5547 | int step; |
| 5548 | destination->Set(dest_position, Unescape(source, i, length, &step)); |
| 5549 | i += step; |
| 5550 | } |
| 5551 | return destination; |
| 5552 | } |
| 5553 | |
| 5554 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5555 | static const unsigned int kQuoteTableLength = 128u; |
| 5556 | |
| 5557 | static const int kJsonQuotesCharactersPerEntry = 8; |
| 5558 | static const char* const JsonQuotes = |
| 5559 | "\\u0000 \\u0001 \\u0002 \\u0003 " |
| 5560 | "\\u0004 \\u0005 \\u0006 \\u0007 " |
| 5561 | "\\b \\t \\n \\u000b " |
| 5562 | "\\f \\r \\u000e \\u000f " |
| 5563 | "\\u0010 \\u0011 \\u0012 \\u0013 " |
| 5564 | "\\u0014 \\u0015 \\u0016 \\u0017 " |
| 5565 | "\\u0018 \\u0019 \\u001a \\u001b " |
| 5566 | "\\u001c \\u001d \\u001e \\u001f " |
| 5567 | " ! \\\" # " |
| 5568 | "$ % & ' " |
| 5569 | "( ) * + " |
| 5570 | ", - . / " |
| 5571 | "0 1 2 3 " |
| 5572 | "4 5 6 7 " |
| 5573 | "8 9 : ; " |
| 5574 | "< = > ? " |
| 5575 | "@ A B C " |
| 5576 | "D E F G " |
| 5577 | "H I J K " |
| 5578 | "L M N O " |
| 5579 | "P Q R S " |
| 5580 | "T U V W " |
| 5581 | "X Y Z [ " |
| 5582 | "\\\\ ] ^ _ " |
| 5583 | "` a b c " |
| 5584 | "d e f g " |
| 5585 | "h i j k " |
| 5586 | "l m n o " |
| 5587 | "p q r s " |
| 5588 | "t u v w " |
| 5589 | "x y z { " |
| 5590 | "| } ~ \177 "; |
| 5591 | |
| 5592 | |
| 5593 | // For a string that is less than 32k characters it should always be |
| 5594 | // possible to allocate it in new space. |
| 5595 | static const int kMaxGuaranteedNewSpaceString = 32 * 1024; |
| 5596 | |
| 5597 | |
| 5598 | // Doing JSON quoting cannot make the string more than this many times larger. |
| 5599 | static const int kJsonQuoteWorstCaseBlowup = 6; |
| 5600 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5601 | static const int kSpaceForQuotesAndComma = 3; |
| 5602 | static const int kSpaceForBrackets = 2; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5603 | |
| 5604 | // Covers the entire ASCII range (all other characters are unchanged by JSON |
| 5605 | // quoting). |
| 5606 | static const byte JsonQuoteLengths[kQuoteTableLength] = { |
| 5607 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5608 | 2, 2, 2, 6, 2, 2, 6, 6, |
| 5609 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5610 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5611 | 1, 1, 2, 1, 1, 1, 1, 1, |
| 5612 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5613 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5614 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5615 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5616 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5617 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5618 | 1, 1, 1, 1, 2, 1, 1, 1, |
| 5619 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5620 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5621 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5622 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5623 | }; |
| 5624 | |
| 5625 | |
| 5626 | template <typename StringType> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5627 | MaybeObject* AllocateRawString(Isolate* isolate, int length); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5628 | |
| 5629 | |
| 5630 | template <> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5631 | MaybeObject* AllocateRawString<SeqTwoByteString>(Isolate* isolate, int length) { |
| 5632 | return isolate->heap()->AllocateRawTwoByteString(length); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5633 | } |
| 5634 | |
| 5635 | |
| 5636 | template <> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5637 | MaybeObject* AllocateRawString<SeqAsciiString>(Isolate* isolate, int length) { |
| 5638 | return isolate->heap()->AllocateRawAsciiString(length); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5639 | } |
| 5640 | |
| 5641 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5642 | template <typename Char, typename StringType, bool comma> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5643 | static MaybeObject* SlowQuoteJsonString(Isolate* isolate, |
| 5644 | Vector<const Char> characters) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5645 | int length = characters.length(); |
| 5646 | const Char* read_cursor = characters.start(); |
| 5647 | const Char* end = read_cursor + length; |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5648 | const int kSpaceForQuotes = 2 + (comma ? 1 :0); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5649 | int quoted_length = kSpaceForQuotes; |
| 5650 | while (read_cursor < end) { |
| 5651 | Char c = *(read_cursor++); |
| 5652 | if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5653 | quoted_length++; |
| 5654 | } else { |
| 5655 | quoted_length += JsonQuoteLengths[static_cast<unsigned>(c)]; |
| 5656 | } |
| 5657 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5658 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5659 | quoted_length); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5660 | Object* new_object; |
| 5661 | if (!new_alloc->ToObject(&new_object)) { |
| 5662 | return new_alloc; |
| 5663 | } |
| 5664 | StringType* new_string = StringType::cast(new_object); |
| 5665 | |
| 5666 | Char* write_cursor = reinterpret_cast<Char*>( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5667 | new_string->address() + SeqString::kHeaderSize); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5668 | if (comma) *(write_cursor++) = ','; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5669 | *(write_cursor++) = '"'; |
| 5670 | |
| 5671 | read_cursor = characters.start(); |
| 5672 | while (read_cursor < end) { |
| 5673 | Char c = *(read_cursor++); |
| 5674 | if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5675 | *(write_cursor++) = c; |
| 5676 | } else { |
| 5677 | int len = JsonQuoteLengths[static_cast<unsigned>(c)]; |
| 5678 | const char* replacement = JsonQuotes + |
| 5679 | static_cast<unsigned>(c) * kJsonQuotesCharactersPerEntry; |
| 5680 | for (int i = 0; i < len; i++) { |
| 5681 | *write_cursor++ = *replacement++; |
| 5682 | } |
| 5683 | } |
| 5684 | } |
| 5685 | *(write_cursor++) = '"'; |
| 5686 | return new_string; |
| 5687 | } |
| 5688 | |
| 5689 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5690 | template <typename SinkChar, typename SourceChar> |
| 5691 | static inline SinkChar* WriteQuoteJsonString( |
| 5692 | Isolate* isolate, |
| 5693 | SinkChar* write_cursor, |
| 5694 | Vector<const SourceChar> characters) { |
| 5695 | // SinkChar is only char if SourceChar is guaranteed to be char. |
| 5696 | ASSERT(sizeof(SinkChar) >= sizeof(SourceChar)); |
| 5697 | const SourceChar* read_cursor = characters.start(); |
| 5698 | const SourceChar* end = read_cursor + characters.length(); |
| 5699 | *(write_cursor++) = '"'; |
| 5700 | while (read_cursor < end) { |
| 5701 | SourceChar c = *(read_cursor++); |
| 5702 | if (sizeof(SourceChar) > 1u && |
| 5703 | static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5704 | *(write_cursor++) = static_cast<SinkChar>(c); |
| 5705 | } else { |
| 5706 | int len = JsonQuoteLengths[static_cast<unsigned>(c)]; |
| 5707 | const char* replacement = JsonQuotes + |
| 5708 | static_cast<unsigned>(c) * kJsonQuotesCharactersPerEntry; |
| 5709 | write_cursor[0] = replacement[0]; |
| 5710 | if (len > 1) { |
| 5711 | write_cursor[1] = replacement[1]; |
| 5712 | if (len > 2) { |
| 5713 | ASSERT(len == 6); |
| 5714 | write_cursor[2] = replacement[2]; |
| 5715 | write_cursor[3] = replacement[3]; |
| 5716 | write_cursor[4] = replacement[4]; |
| 5717 | write_cursor[5] = replacement[5]; |
| 5718 | } |
| 5719 | } |
| 5720 | write_cursor += len; |
| 5721 | } |
| 5722 | } |
| 5723 | *(write_cursor++) = '"'; |
| 5724 | return write_cursor; |
| 5725 | } |
| 5726 | |
| 5727 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5728 | template <typename Char, typename StringType, bool comma> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5729 | static MaybeObject* QuoteJsonString(Isolate* isolate, |
| 5730 | Vector<const Char> characters) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5731 | int length = characters.length(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5732 | isolate->counters()->quote_json_char_count()->Increment(length); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5733 | int worst_case_length = |
| 5734 | length * kJsonQuoteWorstCaseBlowup + kSpaceForQuotesAndComma; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5735 | if (worst_case_length > kMaxGuaranteedNewSpaceString) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5736 | return SlowQuoteJsonString<Char, StringType, comma>(isolate, characters); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5737 | } |
| 5738 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5739 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5740 | worst_case_length); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5741 | Object* new_object; |
| 5742 | if (!new_alloc->ToObject(&new_object)) { |
| 5743 | return new_alloc; |
| 5744 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5745 | if (!isolate->heap()->new_space()->Contains(new_object)) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5746 | // Even if our string is small enough to fit in new space we still have to |
| 5747 | // handle it being allocated in old space as may happen in the third |
| 5748 | // attempt. See CALL_AND_RETRY in heap-inl.h and similar code in |
| 5749 | // CEntryStub::GenerateCore. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5750 | return SlowQuoteJsonString<Char, StringType, comma>(isolate, characters); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5751 | } |
| 5752 | StringType* new_string = StringType::cast(new_object); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5753 | ASSERT(isolate->heap()->new_space()->Contains(new_string)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5754 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5755 | Char* write_cursor = reinterpret_cast<Char*>( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5756 | new_string->address() + SeqString::kHeaderSize); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5757 | if (comma) *(write_cursor++) = ','; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5758 | write_cursor = WriteQuoteJsonString<Char, Char>(isolate, |
| 5759 | write_cursor, |
| 5760 | characters); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5761 | int final_length = static_cast<int>( |
| 5762 | write_cursor - reinterpret_cast<Char*>( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5763 | new_string->address() + SeqString::kHeaderSize)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5764 | isolate->heap()->new_space()-> |
| 5765 | template ShrinkStringAtAllocationBoundary<StringType>( |
| 5766 | new_string, final_length); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5767 | return new_string; |
| 5768 | } |
| 5769 | |
| 5770 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5771 | RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONString) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5772 | NoHandleAllocation ha; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5773 | CONVERT_ARG_CHECKED(String, str, 0); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5774 | if (!str->IsFlat()) { |
| 5775 | MaybeObject* try_flatten = str->TryFlatten(); |
| 5776 | Object* flat; |
| 5777 | if (!try_flatten->ToObject(&flat)) { |
| 5778 | return try_flatten; |
| 5779 | } |
| 5780 | str = String::cast(flat); |
| 5781 | ASSERT(str->IsFlat()); |
| 5782 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5783 | String::FlatContent flat = str->GetFlatContent(); |
| 5784 | ASSERT(flat.IsFlat()); |
| 5785 | if (flat.IsTwoByte()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5786 | return QuoteJsonString<uc16, SeqTwoByteString, false>(isolate, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5787 | flat.ToUC16Vector()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5788 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5789 | return QuoteJsonString<char, SeqAsciiString, false>(isolate, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5790 | flat.ToAsciiVector()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5791 | } |
| 5792 | } |
| 5793 | |
| 5794 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5795 | RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONStringComma) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5796 | NoHandleAllocation ha; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5797 | CONVERT_ARG_CHECKED(String, str, 0); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5798 | if (!str->IsFlat()) { |
| 5799 | MaybeObject* try_flatten = str->TryFlatten(); |
| 5800 | Object* flat; |
| 5801 | if (!try_flatten->ToObject(&flat)) { |
| 5802 | return try_flatten; |
| 5803 | } |
| 5804 | str = String::cast(flat); |
| 5805 | ASSERT(str->IsFlat()); |
| 5806 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5807 | String::FlatContent flat = str->GetFlatContent(); |
| 5808 | if (flat.IsTwoByte()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5809 | return QuoteJsonString<uc16, SeqTwoByteString, true>(isolate, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5810 | flat.ToUC16Vector()); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5811 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5812 | return QuoteJsonString<char, SeqAsciiString, true>(isolate, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5813 | flat.ToAsciiVector()); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5814 | } |
| 5815 | } |
| 5816 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5817 | |
| 5818 | template <typename Char, typename StringType> |
| 5819 | static MaybeObject* QuoteJsonStringArray(Isolate* isolate, |
| 5820 | FixedArray* array, |
| 5821 | int worst_case_length) { |
| 5822 | int length = array->length(); |
| 5823 | |
| 5824 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5825 | worst_case_length); |
| 5826 | Object* new_object; |
| 5827 | if (!new_alloc->ToObject(&new_object)) { |
| 5828 | return new_alloc; |
| 5829 | } |
| 5830 | if (!isolate->heap()->new_space()->Contains(new_object)) { |
| 5831 | // Even if our string is small enough to fit in new space we still have to |
| 5832 | // handle it being allocated in old space as may happen in the third |
| 5833 | // attempt. See CALL_AND_RETRY in heap-inl.h and similar code in |
| 5834 | // CEntryStub::GenerateCore. |
| 5835 | return isolate->heap()->undefined_value(); |
| 5836 | } |
| 5837 | AssertNoAllocation no_gc; |
| 5838 | StringType* new_string = StringType::cast(new_object); |
| 5839 | ASSERT(isolate->heap()->new_space()->Contains(new_string)); |
| 5840 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5841 | Char* write_cursor = reinterpret_cast<Char*>( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5842 | new_string->address() + SeqString::kHeaderSize); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5843 | *(write_cursor++) = '['; |
| 5844 | for (int i = 0; i < length; i++) { |
| 5845 | if (i != 0) *(write_cursor++) = ','; |
| 5846 | String* str = String::cast(array->get(i)); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5847 | String::FlatContent content = str->GetFlatContent(); |
| 5848 | ASSERT(content.IsFlat()); |
| 5849 | if (content.IsTwoByte()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5850 | write_cursor = WriteQuoteJsonString<Char, uc16>(isolate, |
| 5851 | write_cursor, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5852 | content.ToUC16Vector()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5853 | } else { |
| 5854 | write_cursor = WriteQuoteJsonString<Char, char>(isolate, |
| 5855 | write_cursor, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5856 | content.ToAsciiVector()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5857 | } |
| 5858 | } |
| 5859 | *(write_cursor++) = ']'; |
| 5860 | |
| 5861 | int final_length = static_cast<int>( |
| 5862 | write_cursor - reinterpret_cast<Char*>( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5863 | new_string->address() + SeqString::kHeaderSize)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5864 | isolate->heap()->new_space()-> |
| 5865 | template ShrinkStringAtAllocationBoundary<StringType>( |
| 5866 | new_string, final_length); |
| 5867 | return new_string; |
| 5868 | } |
| 5869 | |
| 5870 | |
| 5871 | RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONStringArray) { |
| 5872 | NoHandleAllocation ha; |
| 5873 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5874 | CONVERT_ARG_CHECKED(JSArray, array, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5875 | |
| 5876 | if (!array->HasFastElements()) return isolate->heap()->undefined_value(); |
| 5877 | FixedArray* elements = FixedArray::cast(array->elements()); |
| 5878 | int n = elements->length(); |
| 5879 | bool ascii = true; |
| 5880 | int total_length = 0; |
| 5881 | |
| 5882 | for (int i = 0; i < n; i++) { |
| 5883 | Object* elt = elements->get(i); |
| 5884 | if (!elt->IsString()) return isolate->heap()->undefined_value(); |
| 5885 | String* element = String::cast(elt); |
| 5886 | if (!element->IsFlat()) return isolate->heap()->undefined_value(); |
| 5887 | total_length += element->length(); |
| 5888 | if (ascii && element->IsTwoByteRepresentation()) { |
| 5889 | ascii = false; |
| 5890 | } |
| 5891 | } |
| 5892 | |
| 5893 | int worst_case_length = |
| 5894 | kSpaceForBrackets + n * kSpaceForQuotesAndComma |
| 5895 | + total_length * kJsonQuoteWorstCaseBlowup; |
| 5896 | |
| 5897 | if (worst_case_length > kMaxGuaranteedNewSpaceString) { |
| 5898 | return isolate->heap()->undefined_value(); |
| 5899 | } |
| 5900 | |
| 5901 | if (ascii) { |
| 5902 | return QuoteJsonStringArray<char, SeqAsciiString>(isolate, |
| 5903 | elements, |
| 5904 | worst_case_length); |
| 5905 | } else { |
| 5906 | return QuoteJsonStringArray<uc16, SeqTwoByteString>(isolate, |
| 5907 | elements, |
| 5908 | worst_case_length); |
| 5909 | } |
| 5910 | } |
| 5911 | |
| 5912 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5913 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringParseInt) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5914 | NoHandleAllocation ha; |
| 5915 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5916 | CONVERT_ARG_CHECKED(String, s, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5917 | CONVERT_SMI_ARG_CHECKED(radix, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5918 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5919 | s->TryFlatten(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5920 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5921 | RUNTIME_ASSERT(radix == 0 || (2 <= radix && radix <= 36)); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5922 | double value = StringToInt(isolate->unicode_cache(), s, radix); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5923 | return isolate->heap()->NumberFromDouble(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5924 | } |
| 5925 | |
| 5926 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5927 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringParseFloat) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5928 | NoHandleAllocation ha; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5929 | CONVERT_ARG_CHECKED(String, str, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5930 | |
| 5931 | // ECMA-262 section 15.1.2.3, empty string is NaN |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5932 | double value = StringToDouble(isolate->unicode_cache(), |
| 5933 | str, ALLOW_TRAILING_JUNK, OS::nan_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5934 | |
| 5935 | // Create a number object from the value. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5936 | return isolate->heap()->NumberFromDouble(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5937 | } |
| 5938 | |
| 5939 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5940 | template <class Converter> |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5941 | MUST_USE_RESULT static MaybeObject* ConvertCaseHelper( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5942 | Isolate* isolate, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5943 | String* s, |
| 5944 | int length, |
| 5945 | int input_string_length, |
| 5946 | unibrow::Mapping<Converter, 128>* mapping) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5947 | // We try this twice, once with the assumption that the result is no longer |
| 5948 | // than the input and, if that assumption breaks, again with the exact |
| 5949 | // length. This may not be pretty, but it is nicer than what was here before |
| 5950 | // and I hereby claim my vaffel-is. |
| 5951 | // |
| 5952 | // Allocate the resulting string. |
| 5953 | // |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5954 | // NOTE: This assumes that the upper/lower case of an ASCII |
| 5955 | // character is also ASCII. This is currently the case, but it |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5956 | // might break in the future if we implement more context and locale |
| 5957 | // dependent upper/lower conversions. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5958 | Object* o; |
| 5959 | { MaybeObject* maybe_o = s->IsAsciiRepresentation() |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5960 | ? isolate->heap()->AllocateRawAsciiString(length) |
| 5961 | : isolate->heap()->AllocateRawTwoByteString(length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5962 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 5963 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5964 | String* result = String::cast(o); |
| 5965 | bool has_changed_character = false; |
| 5966 | |
| 5967 | // Convert all characters to upper case, assuming that they will fit |
| 5968 | // in the buffer |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5969 | Access<StringInputBuffer> buffer( |
| 5970 | isolate->runtime_state()->string_input_buffer()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5971 | buffer->Reset(s); |
| 5972 | unibrow::uchar chars[Converter::kMaxWidth]; |
| 5973 | // We can assume that the string is not empty |
| 5974 | uc32 current = buffer->GetNext(); |
| 5975 | for (int i = 0; i < length;) { |
| 5976 | bool has_next = buffer->has_more(); |
| 5977 | uc32 next = has_next ? buffer->GetNext() : 0; |
| 5978 | int char_length = mapping->get(current, next, chars); |
| 5979 | if (char_length == 0) { |
| 5980 | // The case conversion of this character is the character itself. |
| 5981 | result->Set(i, current); |
| 5982 | i++; |
| 5983 | } else if (char_length == 1) { |
| 5984 | // Common case: converting the letter resulted in one character. |
| 5985 | ASSERT(static_cast<uc32>(chars[0]) != current); |
| 5986 | result->Set(i, chars[0]); |
| 5987 | has_changed_character = true; |
| 5988 | i++; |
| 5989 | } else if (length == input_string_length) { |
| 5990 | // We've assumed that the result would be as long as the |
| 5991 | // input but here is a character that converts to several |
| 5992 | // characters. No matter, we calculate the exact length |
| 5993 | // of the result and try the whole thing again. |
| 5994 | // |
| 5995 | // Note that this leaves room for optimization. We could just |
| 5996 | // memcpy what we already have to the result string. Also, |
| 5997 | // the result string is the last object allocated we could |
| 5998 | // "realloc" it and probably, in the vast majority of cases, |
| 5999 | // extend the existing string to be able to hold the full |
| 6000 | // result. |
| 6001 | int next_length = 0; |
| 6002 | if (has_next) { |
| 6003 | next_length = mapping->get(next, 0, chars); |
| 6004 | if (next_length == 0) next_length = 1; |
| 6005 | } |
| 6006 | int current_length = i + char_length + next_length; |
| 6007 | while (buffer->has_more()) { |
| 6008 | current = buffer->GetNext(); |
| 6009 | // NOTE: we use 0 as the next character here because, while |
| 6010 | // the next character may affect what a character converts to, |
| 6011 | // it does not in any case affect the length of what it convert |
| 6012 | // to. |
| 6013 | int char_length = mapping->get(current, 0, chars); |
| 6014 | if (char_length == 0) char_length = 1; |
| 6015 | current_length += char_length; |
| 6016 | if (current_length > Smi::kMaxValue) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6017 | isolate->context()->mark_out_of_memory(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6018 | return Failure::OutOfMemoryException(); |
| 6019 | } |
| 6020 | } |
| 6021 | // Try again with the real length. |
| 6022 | return Smi::FromInt(current_length); |
| 6023 | } else { |
| 6024 | for (int j = 0; j < char_length; j++) { |
| 6025 | result->Set(i, chars[j]); |
| 6026 | i++; |
| 6027 | } |
| 6028 | has_changed_character = true; |
| 6029 | } |
| 6030 | current = next; |
| 6031 | } |
| 6032 | if (has_changed_character) { |
| 6033 | return result; |
| 6034 | } else { |
| 6035 | // If we didn't actually change anything in doing the conversion |
| 6036 | // we simple return the result and let the converted string |
| 6037 | // become garbage; there is no reason to keep two identical strings |
| 6038 | // alive. |
| 6039 | return s; |
| 6040 | } |
| 6041 | } |
| 6042 | |
| 6043 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6044 | namespace { |
| 6045 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6046 | static const uintptr_t kOneInEveryByte = kUintptrAllBitsSet / 0xFF; |
| 6047 | |
| 6048 | |
| 6049 | // Given a word and two range boundaries returns a word with high bit |
| 6050 | // set in every byte iff the corresponding input byte was strictly in |
| 6051 | // the range (m, n). All the other bits in the result are cleared. |
| 6052 | // This function is only useful when it can be inlined and the |
| 6053 | // boundaries are statically known. |
| 6054 | // Requires: all bytes in the input word and the boundaries must be |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6055 | // ASCII (less than 0x7F). |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6056 | static inline uintptr_t AsciiRangeMask(uintptr_t w, char m, char n) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6057 | // Every byte in an ASCII string is less than or equal to 0x7F. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6058 | ASSERT((w & (kOneInEveryByte * 0x7F)) == w); |
| 6059 | // Use strict inequalities since in edge cases the function could be |
| 6060 | // further simplified. |
| 6061 | ASSERT(0 < m && m < n && n < 0x7F); |
| 6062 | // Has high bit set in every w byte less than n. |
| 6063 | uintptr_t tmp1 = kOneInEveryByte * (0x7F + n) - w; |
| 6064 | // Has high bit set in every w byte greater than m. |
| 6065 | uintptr_t tmp2 = w + kOneInEveryByte * (0x7F - m); |
| 6066 | return (tmp1 & tmp2 & (kOneInEveryByte * 0x80)); |
| 6067 | } |
| 6068 | |
| 6069 | |
| 6070 | enum AsciiCaseConversion { |
| 6071 | ASCII_TO_LOWER, |
| 6072 | ASCII_TO_UPPER |
| 6073 | }; |
| 6074 | |
| 6075 | |
| 6076 | template <AsciiCaseConversion dir> |
| 6077 | struct FastAsciiConverter { |
| 6078 | static bool Convert(char* dst, char* src, int length) { |
| 6079 | #ifdef DEBUG |
| 6080 | char* saved_dst = dst; |
| 6081 | char* saved_src = src; |
| 6082 | #endif |
| 6083 | // We rely on the distance between upper and lower case letters |
| 6084 | // being a known power of 2. |
| 6085 | ASSERT('a' - 'A' == (1 << 5)); |
| 6086 | // Boundaries for the range of input characters than require conversion. |
| 6087 | const char lo = (dir == ASCII_TO_LOWER) ? 'A' - 1 : 'a' - 1; |
| 6088 | const char hi = (dir == ASCII_TO_LOWER) ? 'Z' + 1 : 'z' + 1; |
| 6089 | bool changed = false; |
| 6090 | char* const limit = src + length; |
| 6091 | #ifdef V8_HOST_CAN_READ_UNALIGNED |
| 6092 | // Process the prefix of the input that requires no conversion one |
| 6093 | // (machine) word at a time. |
| 6094 | while (src <= limit - sizeof(uintptr_t)) { |
| 6095 | uintptr_t w = *reinterpret_cast<uintptr_t*>(src); |
| 6096 | if (AsciiRangeMask(w, lo, hi) != 0) { |
| 6097 | changed = true; |
| 6098 | break; |
| 6099 | } |
| 6100 | *reinterpret_cast<uintptr_t*>(dst) = w; |
| 6101 | src += sizeof(uintptr_t); |
| 6102 | dst += sizeof(uintptr_t); |
| 6103 | } |
| 6104 | // Process the remainder of the input performing conversion when |
| 6105 | // required one word at a time. |
| 6106 | while (src <= limit - sizeof(uintptr_t)) { |
| 6107 | uintptr_t w = *reinterpret_cast<uintptr_t*>(src); |
| 6108 | uintptr_t m = AsciiRangeMask(w, lo, hi); |
| 6109 | // The mask has high (7th) bit set in every byte that needs |
| 6110 | // conversion and we know that the distance between cases is |
| 6111 | // 1 << 5. |
| 6112 | *reinterpret_cast<uintptr_t*>(dst) = w ^ (m >> 2); |
| 6113 | src += sizeof(uintptr_t); |
| 6114 | dst += sizeof(uintptr_t); |
| 6115 | } |
| 6116 | #endif |
| 6117 | // Process the last few bytes of the input (or the whole input if |
| 6118 | // unaligned access is not supported). |
| 6119 | while (src < limit) { |
| 6120 | char c = *src; |
| 6121 | if (lo < c && c < hi) { |
| 6122 | c ^= (1 << 5); |
| 6123 | changed = true; |
| 6124 | } |
| 6125 | *dst = c; |
| 6126 | ++src; |
| 6127 | ++dst; |
| 6128 | } |
| 6129 | #ifdef DEBUG |
| 6130 | CheckConvert(saved_dst, saved_src, length, changed); |
| 6131 | #endif |
| 6132 | return changed; |
| 6133 | } |
| 6134 | |
| 6135 | #ifdef DEBUG |
| 6136 | static void CheckConvert(char* dst, char* src, int length, bool changed) { |
| 6137 | bool expected_changed = false; |
| 6138 | for (int i = 0; i < length; i++) { |
| 6139 | if (dst[i] == src[i]) continue; |
| 6140 | expected_changed = true; |
| 6141 | if (dir == ASCII_TO_LOWER) { |
| 6142 | ASSERT('A' <= src[i] && src[i] <= 'Z'); |
| 6143 | ASSERT(dst[i] == src[i] + ('a' - 'A')); |
| 6144 | } else { |
| 6145 | ASSERT(dir == ASCII_TO_UPPER); |
| 6146 | ASSERT('a' <= src[i] && src[i] <= 'z'); |
| 6147 | ASSERT(dst[i] == src[i] - ('a' - 'A')); |
| 6148 | } |
| 6149 | } |
| 6150 | ASSERT(expected_changed == changed); |
| 6151 | } |
| 6152 | #endif |
| 6153 | }; |
| 6154 | |
| 6155 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6156 | struct ToLowerTraits { |
| 6157 | typedef unibrow::ToLowercase UnibrowConverter; |
| 6158 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6159 | typedef FastAsciiConverter<ASCII_TO_LOWER> AsciiConverter; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6160 | }; |
| 6161 | |
| 6162 | |
| 6163 | struct ToUpperTraits { |
| 6164 | typedef unibrow::ToUppercase UnibrowConverter; |
| 6165 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6166 | typedef FastAsciiConverter<ASCII_TO_UPPER> AsciiConverter; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6167 | }; |
| 6168 | |
| 6169 | } // namespace |
| 6170 | |
| 6171 | |
| 6172 | template <typename ConvertTraits> |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6173 | MUST_USE_RESULT static MaybeObject* ConvertCase( |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6174 | Arguments args, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6175 | Isolate* isolate, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6176 | unibrow::Mapping<typename ConvertTraits::UnibrowConverter, 128>* mapping) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6177 | NoHandleAllocation ha; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6178 | CONVERT_ARG_CHECKED(String, s, 0); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 6179 | s = s->TryFlattenGetString(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6180 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6181 | const int length = s->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6182 | // Assume that the string is not empty; we need this assumption later |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6183 | if (length == 0) return s; |
| 6184 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6185 | // Simpler handling of ASCII strings. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6186 | // |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6187 | // NOTE: This assumes that the upper/lower case of an ASCII |
| 6188 | // character is also ASCII. This is currently the case, but it |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6189 | // might break in the future if we implement more context and locale |
| 6190 | // dependent upper/lower conversions. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 6191 | if (s->IsSeqAsciiString()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6192 | Object* o; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6193 | { MaybeObject* maybe_o = isolate->heap()->AllocateRawAsciiString(length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6194 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 6195 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6196 | SeqAsciiString* result = SeqAsciiString::cast(o); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6197 | bool has_changed_character = ConvertTraits::AsciiConverter::Convert( |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 6198 | result->GetChars(), SeqAsciiString::cast(s)->GetChars(), length); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6199 | return has_changed_character ? result : s; |
| 6200 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6201 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6202 | Object* answer; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6203 | { MaybeObject* maybe_answer = |
| 6204 | ConvertCaseHelper(isolate, s, length, length, mapping); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6205 | if (!maybe_answer->ToObject(&answer)) return maybe_answer; |
| 6206 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6207 | if (answer->IsSmi()) { |
| 6208 | // Retry with correct length. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6209 | { MaybeObject* maybe_answer = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6210 | ConvertCaseHelper(isolate, |
| 6211 | s, Smi::cast(answer)->value(), length, mapping); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6212 | if (!maybe_answer->ToObject(&answer)) return maybe_answer; |
| 6213 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6214 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6215 | return answer; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6216 | } |
| 6217 | |
| 6218 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6219 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToLowerCase) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6220 | return ConvertCase<ToLowerTraits>( |
| 6221 | args, isolate, isolate->runtime_state()->to_lower_mapping()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6222 | } |
| 6223 | |
| 6224 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6225 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToUpperCase) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6226 | return ConvertCase<ToUpperTraits>( |
| 6227 | args, isolate, isolate->runtime_state()->to_upper_mapping()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6228 | } |
| 6229 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6230 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6231 | static inline bool IsTrimWhiteSpace(unibrow::uchar c) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6232 | return unibrow::WhiteSpace::Is(c) || c == 0x200b || c == 0xfeff; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6233 | } |
| 6234 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6235 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6236 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringTrim) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6237 | NoHandleAllocation ha; |
| 6238 | ASSERT(args.length() == 3); |
| 6239 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6240 | CONVERT_ARG_CHECKED(String, s, 0); |
| 6241 | CONVERT_BOOLEAN_ARG_CHECKED(trimLeft, 1); |
| 6242 | CONVERT_BOOLEAN_ARG_CHECKED(trimRight, 2); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6243 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6244 | s->TryFlatten(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6245 | int length = s->length(); |
| 6246 | |
| 6247 | int left = 0; |
| 6248 | if (trimLeft) { |
| 6249 | while (left < length && IsTrimWhiteSpace(s->Get(left))) { |
| 6250 | left++; |
| 6251 | } |
| 6252 | } |
| 6253 | |
| 6254 | int right = length; |
| 6255 | if (trimRight) { |
| 6256 | while (right > left && IsTrimWhiteSpace(s->Get(right - 1))) { |
| 6257 | right--; |
| 6258 | } |
| 6259 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6260 | return s->SubString(left, right); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6261 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6262 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6263 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6264 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringSplit) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6265 | ASSERT(args.length() == 3); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6266 | HandleScope handle_scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6267 | CONVERT_ARG_HANDLE_CHECKED(String, subject, 0); |
| 6268 | CONVERT_ARG_HANDLE_CHECKED(String, pattern, 1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6269 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[2]); |
| 6270 | |
| 6271 | int subject_length = subject->length(); |
| 6272 | int pattern_length = pattern->length(); |
| 6273 | RUNTIME_ASSERT(pattern_length > 0); |
| 6274 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 6275 | if (limit == 0xffffffffu) { |
| 6276 | Handle<Object> cached_answer(StringSplitCache::Lookup( |
| 6277 | isolate->heap()->string_split_cache(), |
| 6278 | *subject, |
| 6279 | *pattern)); |
| 6280 | if (*cached_answer != Smi::FromInt(0)) { |
| 6281 | Handle<JSArray> result = |
| 6282 | isolate->factory()->NewJSArrayWithElements( |
| 6283 | Handle<FixedArray>::cast(cached_answer)); |
| 6284 | return *result; |
| 6285 | } |
| 6286 | } |
| 6287 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6288 | // The limit can be very large (0xffffffffu), but since the pattern |
| 6289 | // isn't empty, we can never create more parts than ~half the length |
| 6290 | // of the subject. |
| 6291 | |
| 6292 | if (!subject->IsFlat()) FlattenString(subject); |
| 6293 | |
| 6294 | static const int kMaxInitialListCapacity = 16; |
| 6295 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6296 | ZoneScope scope(isolate, DELETE_ON_EXIT); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6297 | |
| 6298 | // Find (up to limit) indices of separator and end-of-string in subject |
| 6299 | int initial_capacity = Min<uint32_t>(kMaxInitialListCapacity, limit); |
| 6300 | ZoneList<int> indices(initial_capacity); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 6301 | if (!pattern->IsFlat()) FlattenString(pattern); |
| 6302 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 6303 | FindStringIndicesDispatch(isolate, *subject, *pattern, &indices, limit); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 6304 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6305 | if (static_cast<uint32_t>(indices.length()) < limit) { |
| 6306 | indices.Add(subject_length); |
| 6307 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6308 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 6309 | // The list indices now contains the end of each part to create. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6310 | |
| 6311 | // Create JSArray of substrings separated by separator. |
| 6312 | int part_count = indices.length(); |
| 6313 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6314 | Handle<JSArray> result = isolate->factory()->NewJSArray(part_count); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6315 | MaybeObject* maybe_result = result->EnsureCanContainHeapObjectElements(); |
| 6316 | if (maybe_result->IsFailure()) return maybe_result; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6317 | result->set_length(Smi::FromInt(part_count)); |
| 6318 | |
| 6319 | ASSERT(result->HasFastElements()); |
| 6320 | |
| 6321 | if (part_count == 1 && indices.at(0) == subject_length) { |
| 6322 | FixedArray::cast(result->elements())->set(0, *subject); |
| 6323 | return *result; |
| 6324 | } |
| 6325 | |
| 6326 | Handle<FixedArray> elements(FixedArray::cast(result->elements())); |
| 6327 | int part_start = 0; |
| 6328 | for (int i = 0; i < part_count; i++) { |
| 6329 | HandleScope local_loop_handle; |
| 6330 | int part_end = indices.at(i); |
| 6331 | Handle<String> substring = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6332 | isolate->factory()->NewProperSubString(subject, part_start, part_end); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6333 | elements->set(i, *substring); |
| 6334 | part_start = part_end + pattern_length; |
| 6335 | } |
| 6336 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 6337 | if (limit == 0xffffffffu) { |
| 6338 | if (result->HasFastElements()) { |
| 6339 | StringSplitCache::Enter(isolate->heap(), |
| 6340 | isolate->heap()->string_split_cache(), |
| 6341 | *subject, |
| 6342 | *pattern, |
| 6343 | *elements); |
| 6344 | } |
| 6345 | } |
| 6346 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6347 | return *result; |
| 6348 | } |
| 6349 | |
| 6350 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6351 | // Copies ASCII characters to the given fixed array looking up |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6352 | // one-char strings in the cache. Gives up on the first char that is |
| 6353 | // not in the cache and fills the remainder with smi zeros. Returns |
| 6354 | // the length of the successfully copied prefix. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6355 | static int CopyCachedAsciiCharsToArray(Heap* heap, |
| 6356 | const char* chars, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6357 | FixedArray* elements, |
| 6358 | int length) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 6359 | AssertNoAllocation no_gc; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6360 | FixedArray* ascii_cache = heap->single_character_string_cache(); |
| 6361 | Object* undefined = heap->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6362 | int i; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6363 | WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6364 | for (i = 0; i < length; ++i) { |
| 6365 | Object* value = ascii_cache->get(chars[i]); |
| 6366 | if (value == undefined) break; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6367 | elements->set(i, value, mode); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6368 | } |
| 6369 | if (i < length) { |
| 6370 | ASSERT(Smi::FromInt(0) == 0); |
| 6371 | memset(elements->data_start() + i, 0, kPointerSize * (length - i)); |
| 6372 | } |
| 6373 | #ifdef DEBUG |
| 6374 | for (int j = 0; j < length; ++j) { |
| 6375 | Object* element = elements->get(j); |
| 6376 | ASSERT(element == Smi::FromInt(0) || |
| 6377 | (element->IsString() && String::cast(element)->LooksValid())); |
| 6378 | } |
| 6379 | #endif |
| 6380 | return i; |
| 6381 | } |
| 6382 | |
| 6383 | |
| 6384 | // Converts a String to JSArray. |
| 6385 | // For example, "foo" => ["f", "o", "o"]. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6386 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToArray) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6387 | HandleScope scope(isolate); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 6388 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6389 | CONVERT_ARG_HANDLE_CHECKED(String, s, 0); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 6390 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6391 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 6392 | s = FlattenGetString(s); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 6393 | const int length = static_cast<int>(Min<uint32_t>(s->length(), limit)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6394 | |
| 6395 | Handle<FixedArray> elements; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 6396 | int position = 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6397 | if (s->IsFlat() && s->IsAsciiRepresentation()) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 6398 | // Try using cached chars where possible. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6399 | Object* obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6400 | { MaybeObject* maybe_obj = |
| 6401 | isolate->heap()->AllocateUninitializedFixedArray(length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6402 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 6403 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6404 | elements = Handle<FixedArray>(FixedArray::cast(obj), isolate); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 6405 | String::FlatContent content = s->GetFlatContent(); |
| 6406 | if (content.IsAscii()) { |
| 6407 | Vector<const char> chars = content.ToAsciiVector(); |
| 6408 | // Note, this will initialize all elements (not only the prefix) |
| 6409 | // to prevent GC from seeing partially initialized array. |
| 6410 | position = CopyCachedAsciiCharsToArray(isolate->heap(), |
| 6411 | chars.start(), |
| 6412 | *elements, |
| 6413 | length); |
| 6414 | } else { |
| 6415 | MemsetPointer(elements->data_start(), |
| 6416 | isolate->heap()->undefined_value(), |
| 6417 | length); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6418 | } |
| 6419 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6420 | elements = isolate->factory()->NewFixedArray(length); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 6421 | } |
| 6422 | for (int i = position; i < length; ++i) { |
| 6423 | Handle<Object> str = LookupSingleCharacterStringFromCode(s->Get(i)); |
| 6424 | elements->set(i, *str); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6425 | } |
| 6426 | |
| 6427 | #ifdef DEBUG |
| 6428 | for (int i = 0; i < length; ++i) { |
| 6429 | ASSERT(String::cast(elements->get(i))->length() == 1); |
| 6430 | } |
| 6431 | #endif |
| 6432 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6433 | return *isolate->factory()->NewJSArrayWithElements(elements); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6434 | } |
| 6435 | |
| 6436 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6437 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewStringWrapper) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 6438 | NoHandleAllocation ha; |
| 6439 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6440 | CONVERT_ARG_CHECKED(String, value, 0); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 6441 | return value->ToObject(); |
| 6442 | } |
| 6443 | |
| 6444 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6445 | bool Runtime::IsUpperCaseChar(RuntimeState* runtime_state, uint16_t ch) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6446 | unibrow::uchar chars[unibrow::ToUppercase::kMaxWidth]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6447 | int char_length = runtime_state->to_upper_mapping()->get(ch, 0, chars); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6448 | return char_length == 0; |
| 6449 | } |
| 6450 | |
| 6451 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6452 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToString) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6453 | NoHandleAllocation ha; |
| 6454 | ASSERT(args.length() == 1); |
| 6455 | |
| 6456 | Object* number = args[0]; |
| 6457 | RUNTIME_ASSERT(number->IsNumber()); |
| 6458 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6459 | return isolate->heap()->NumberToString(number); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6460 | } |
| 6461 | |
| 6462 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6463 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToStringSkipCache) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6464 | NoHandleAllocation ha; |
| 6465 | ASSERT(args.length() == 1); |
| 6466 | |
| 6467 | Object* number = args[0]; |
| 6468 | RUNTIME_ASSERT(number->IsNumber()); |
| 6469 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6470 | return isolate->heap()->NumberToString(number, false); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6471 | } |
| 6472 | |
| 6473 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6474 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToInteger) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6475 | NoHandleAllocation ha; |
| 6476 | ASSERT(args.length() == 1); |
| 6477 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6478 | CONVERT_DOUBLE_ARG_CHECKED(number, 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6479 | |
| 6480 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 6481 | if (number > 0 && number <= Smi::kMaxValue) { |
| 6482 | return Smi::FromInt(static_cast<int>(number)); |
| 6483 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6484 | return isolate->heap()->NumberFromDouble(DoubleToInteger(number)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6485 | } |
| 6486 | |
| 6487 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6488 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToIntegerMapMinusZero) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6489 | NoHandleAllocation ha; |
| 6490 | ASSERT(args.length() == 1); |
| 6491 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6492 | CONVERT_DOUBLE_ARG_CHECKED(number, 0); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6493 | |
| 6494 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 6495 | if (number > 0 && number <= Smi::kMaxValue) { |
| 6496 | return Smi::FromInt(static_cast<int>(number)); |
| 6497 | } |
| 6498 | |
| 6499 | double double_value = DoubleToInteger(number); |
| 6500 | // Map both -0 and +0 to +0. |
| 6501 | if (double_value == 0) double_value = 0; |
| 6502 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6503 | return isolate->heap()->NumberFromDouble(double_value); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6504 | } |
| 6505 | |
| 6506 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6507 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToJSUint32) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6508 | NoHandleAllocation ha; |
| 6509 | ASSERT(args.length() == 1); |
| 6510 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6511 | CONVERT_NUMBER_CHECKED(int32_t, number, Uint32, args[0]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6512 | return isolate->heap()->NumberFromUint32(number); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6513 | } |
| 6514 | |
| 6515 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6516 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToJSInt32) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6517 | NoHandleAllocation ha; |
| 6518 | ASSERT(args.length() == 1); |
| 6519 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6520 | CONVERT_DOUBLE_ARG_CHECKED(number, 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6521 | |
| 6522 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 6523 | if (number > 0 && number <= Smi::kMaxValue) { |
| 6524 | return Smi::FromInt(static_cast<int>(number)); |
| 6525 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6526 | return isolate->heap()->NumberFromInt32(DoubleToInt32(number)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6527 | } |
| 6528 | |
| 6529 | |
| 6530 | // Converts a Number to a Smi, if possible. Returns NaN if the number is not |
| 6531 | // a small integer. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6532 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToSmi) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6533 | NoHandleAllocation ha; |
| 6534 | ASSERT(args.length() == 1); |
| 6535 | |
| 6536 | Object* obj = args[0]; |
| 6537 | if (obj->IsSmi()) { |
| 6538 | return obj; |
| 6539 | } |
| 6540 | if (obj->IsHeapNumber()) { |
| 6541 | double value = HeapNumber::cast(obj)->value(); |
| 6542 | int int_value = FastD2I(value); |
| 6543 | if (value == FastI2D(int_value) && Smi::IsValid(int_value)) { |
| 6544 | return Smi::FromInt(int_value); |
| 6545 | } |
| 6546 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6547 | return isolate->heap()->nan_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6548 | } |
| 6549 | |
| 6550 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6551 | RUNTIME_FUNCTION(MaybeObject*, Runtime_AllocateHeapNumber) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6552 | NoHandleAllocation ha; |
| 6553 | ASSERT(args.length() == 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6554 | return isolate->heap()->AllocateHeapNumber(0); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6555 | } |
| 6556 | |
| 6557 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6558 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberAdd) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6559 | NoHandleAllocation ha; |
| 6560 | ASSERT(args.length() == 2); |
| 6561 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6562 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6563 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6564 | return isolate->heap()->NumberFromDouble(x + y); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6565 | } |
| 6566 | |
| 6567 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6568 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberSub) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6569 | NoHandleAllocation ha; |
| 6570 | ASSERT(args.length() == 2); |
| 6571 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6572 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6573 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6574 | return isolate->heap()->NumberFromDouble(x - y); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6575 | } |
| 6576 | |
| 6577 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6578 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberMul) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6579 | NoHandleAllocation ha; |
| 6580 | ASSERT(args.length() == 2); |
| 6581 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6582 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6583 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6584 | return isolate->heap()->NumberFromDouble(x * y); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6585 | } |
| 6586 | |
| 6587 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6588 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberUnaryMinus) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6589 | NoHandleAllocation ha; |
| 6590 | ASSERT(args.length() == 1); |
| 6591 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6592 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6593 | return isolate->heap()->NumberFromDouble(-x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6594 | } |
| 6595 | |
| 6596 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6597 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberAlloc) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 6598 | NoHandleAllocation ha; |
| 6599 | ASSERT(args.length() == 0); |
| 6600 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6601 | return isolate->heap()->NumberFromDouble(9876543210.0); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 6602 | } |
| 6603 | |
| 6604 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6605 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberDiv) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6606 | NoHandleAllocation ha; |
| 6607 | ASSERT(args.length() == 2); |
| 6608 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6609 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6610 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6611 | return isolate->heap()->NumberFromDouble(x / y); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6612 | } |
| 6613 | |
| 6614 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6615 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberMod) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6616 | NoHandleAllocation ha; |
| 6617 | ASSERT(args.length() == 2); |
| 6618 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6619 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6620 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6621 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6622 | x = modulo(x, y); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6623 | // NumberFromDouble may return a Smi instead of a Number object |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6624 | return isolate->heap()->NumberFromDouble(x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6625 | } |
| 6626 | |
| 6627 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6628 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringAdd) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6629 | NoHandleAllocation ha; |
| 6630 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6631 | CONVERT_ARG_CHECKED(String, str1, 0); |
| 6632 | CONVERT_ARG_CHECKED(String, str2, 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6633 | isolate->counters()->string_add_runtime()->Increment(); |
| 6634 | return isolate->heap()->AllocateConsString(str1, str2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6635 | } |
| 6636 | |
| 6637 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6638 | template <typename sinkchar> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6639 | static inline void StringBuilderConcatHelper(String* special, |
| 6640 | sinkchar* sink, |
| 6641 | FixedArray* fixed_array, |
| 6642 | int array_length) { |
| 6643 | int position = 0; |
| 6644 | for (int i = 0; i < array_length; i++) { |
| 6645 | Object* element = fixed_array->get(i); |
| 6646 | if (element->IsSmi()) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6647 | // Smi encoding of position and length. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6648 | int encoded_slice = Smi::cast(element)->value(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6649 | int pos; |
| 6650 | int len; |
| 6651 | if (encoded_slice > 0) { |
| 6652 | // Position and length encoded in one smi. |
| 6653 | pos = StringBuilderSubstringPosition::decode(encoded_slice); |
| 6654 | len = StringBuilderSubstringLength::decode(encoded_slice); |
| 6655 | } else { |
| 6656 | // Position and length encoded in two smis. |
| 6657 | Object* obj = fixed_array->get(++i); |
| 6658 | ASSERT(obj->IsSmi()); |
| 6659 | pos = Smi::cast(obj)->value(); |
| 6660 | len = -encoded_slice; |
| 6661 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6662 | String::WriteToFlat(special, |
| 6663 | sink + position, |
| 6664 | pos, |
| 6665 | pos + len); |
| 6666 | position += len; |
| 6667 | } else { |
| 6668 | String* string = String::cast(element); |
| 6669 | int element_length = string->length(); |
| 6670 | String::WriteToFlat(string, sink + position, 0, element_length); |
| 6671 | position += element_length; |
| 6672 | } |
| 6673 | } |
| 6674 | } |
| 6675 | |
| 6676 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6677 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringBuilderConcat) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6678 | NoHandleAllocation ha; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6679 | ASSERT(args.length() == 3); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6680 | CONVERT_ARG_CHECKED(JSArray, array, 0); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6681 | if (!args[1]->IsSmi()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6682 | isolate->context()->mark_out_of_memory(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6683 | return Failure::OutOfMemoryException(); |
| 6684 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6685 | int array_length = args.smi_at(1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6686 | CONVERT_ARG_CHECKED(String, special, 2); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6687 | |
| 6688 | // This assumption is used by the slice encoding in one or two smis. |
| 6689 | ASSERT(Smi::kMaxValue >= String::kMaxLength); |
| 6690 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6691 | MaybeObject* maybe_result = array->EnsureCanContainHeapObjectElements(); |
| 6692 | if (maybe_result->IsFailure()) return maybe_result; |
| 6693 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6694 | int special_length = special->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6695 | if (!array->HasFastElements()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6696 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6697 | } |
| 6698 | FixedArray* fixed_array = FixedArray::cast(array->elements()); |
| 6699 | if (fixed_array->length() < array_length) { |
| 6700 | array_length = fixed_array->length(); |
| 6701 | } |
| 6702 | |
| 6703 | if (array_length == 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6704 | return isolate->heap()->empty_string(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6705 | } else if (array_length == 1) { |
| 6706 | Object* first = fixed_array->get(0); |
| 6707 | if (first->IsString()) return first; |
| 6708 | } |
| 6709 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 6710 | bool ascii = special->HasOnlyAsciiChars(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6711 | int position = 0; |
| 6712 | for (int i = 0; i < array_length; i++) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6713 | int increment = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6714 | Object* elt = fixed_array->get(i); |
| 6715 | if (elt->IsSmi()) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6716 | // Smi encoding of position and length. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6717 | int smi_value = Smi::cast(elt)->value(); |
| 6718 | int pos; |
| 6719 | int len; |
| 6720 | if (smi_value > 0) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6721 | // Position and length encoded in one smi. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6722 | pos = StringBuilderSubstringPosition::decode(smi_value); |
| 6723 | len = StringBuilderSubstringLength::decode(smi_value); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6724 | } else { |
| 6725 | // Position and length encoded in two smis. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6726 | len = -smi_value; |
| 6727 | // Get the position and check that it is a positive smi. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6728 | i++; |
| 6729 | if (i >= array_length) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6730 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6731 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6732 | Object* next_smi = fixed_array->get(i); |
| 6733 | if (!next_smi->IsSmi()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6734 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6735 | } |
| 6736 | pos = Smi::cast(next_smi)->value(); |
| 6737 | if (pos < 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6738 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6739 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6740 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6741 | ASSERT(pos >= 0); |
| 6742 | ASSERT(len >= 0); |
| 6743 | if (pos > special_length || len > special_length - pos) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6744 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6745 | } |
| 6746 | increment = len; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6747 | } else if (elt->IsString()) { |
| 6748 | String* element = String::cast(elt); |
| 6749 | int element_length = element->length(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6750 | increment = element_length; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 6751 | if (ascii && !element->HasOnlyAsciiChars()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6752 | ascii = false; |
| 6753 | } |
| 6754 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6755 | ASSERT(!elt->IsTheHole()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6756 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6757 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6758 | if (increment > String::kMaxLength - position) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6759 | isolate->context()->mark_out_of_memory(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6760 | return Failure::OutOfMemoryException(); |
| 6761 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6762 | position += increment; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6763 | } |
| 6764 | |
| 6765 | int length = position; |
| 6766 | Object* object; |
| 6767 | |
| 6768 | if (ascii) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6769 | { MaybeObject* maybe_object = |
| 6770 | isolate->heap()->AllocateRawAsciiString(length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6771 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6772 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6773 | SeqAsciiString* answer = SeqAsciiString::cast(object); |
| 6774 | StringBuilderConcatHelper(special, |
| 6775 | answer->GetChars(), |
| 6776 | fixed_array, |
| 6777 | array_length); |
| 6778 | return answer; |
| 6779 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6780 | { MaybeObject* maybe_object = |
| 6781 | isolate->heap()->AllocateRawTwoByteString(length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6782 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6783 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6784 | SeqTwoByteString* answer = SeqTwoByteString::cast(object); |
| 6785 | StringBuilderConcatHelper(special, |
| 6786 | answer->GetChars(), |
| 6787 | fixed_array, |
| 6788 | array_length); |
| 6789 | return answer; |
| 6790 | } |
| 6791 | } |
| 6792 | |
| 6793 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6794 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringBuilderJoin) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6795 | NoHandleAllocation ha; |
| 6796 | ASSERT(args.length() == 3); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6797 | CONVERT_ARG_CHECKED(JSArray, array, 0); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6798 | if (!args[1]->IsSmi()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6799 | isolate->context()->mark_out_of_memory(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6800 | return Failure::OutOfMemoryException(); |
| 6801 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6802 | int array_length = args.smi_at(1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6803 | CONVERT_ARG_CHECKED(String, separator, 2); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6804 | |
| 6805 | if (!array->HasFastElements()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6806 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6807 | } |
| 6808 | FixedArray* fixed_array = FixedArray::cast(array->elements()); |
| 6809 | if (fixed_array->length() < array_length) { |
| 6810 | array_length = fixed_array->length(); |
| 6811 | } |
| 6812 | |
| 6813 | if (array_length == 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6814 | return isolate->heap()->empty_string(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6815 | } else if (array_length == 1) { |
| 6816 | Object* first = fixed_array->get(0); |
| 6817 | if (first->IsString()) return first; |
| 6818 | } |
| 6819 | |
| 6820 | int separator_length = separator->length(); |
| 6821 | int max_nof_separators = |
| 6822 | (String::kMaxLength + separator_length - 1) / separator_length; |
| 6823 | if (max_nof_separators < (array_length - 1)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6824 | isolate->context()->mark_out_of_memory(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6825 | return Failure::OutOfMemoryException(); |
| 6826 | } |
| 6827 | int length = (array_length - 1) * separator_length; |
| 6828 | for (int i = 0; i < array_length; i++) { |
| 6829 | Object* element_obj = fixed_array->get(i); |
| 6830 | if (!element_obj->IsString()) { |
| 6831 | // TODO(1161): handle this case. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6832 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6833 | } |
| 6834 | String* element = String::cast(element_obj); |
| 6835 | int increment = element->length(); |
| 6836 | if (increment > String::kMaxLength - length) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6837 | isolate->context()->mark_out_of_memory(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6838 | return Failure::OutOfMemoryException(); |
| 6839 | } |
| 6840 | length += increment; |
| 6841 | } |
| 6842 | |
| 6843 | Object* object; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6844 | { MaybeObject* maybe_object = |
| 6845 | isolate->heap()->AllocateRawTwoByteString(length); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6846 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6847 | } |
| 6848 | SeqTwoByteString* answer = SeqTwoByteString::cast(object); |
| 6849 | |
| 6850 | uc16* sink = answer->GetChars(); |
| 6851 | #ifdef DEBUG |
| 6852 | uc16* end = sink + length; |
| 6853 | #endif |
| 6854 | |
| 6855 | String* first = String::cast(fixed_array->get(0)); |
| 6856 | int first_length = first->length(); |
| 6857 | String::WriteToFlat(first, sink, 0, first_length); |
| 6858 | sink += first_length; |
| 6859 | |
| 6860 | for (int i = 1; i < array_length; i++) { |
| 6861 | ASSERT(sink + separator_length <= end); |
| 6862 | String::WriteToFlat(separator, sink, 0, separator_length); |
| 6863 | sink += separator_length; |
| 6864 | |
| 6865 | String* element = String::cast(fixed_array->get(i)); |
| 6866 | int element_length = element->length(); |
| 6867 | ASSERT(sink + element_length <= end); |
| 6868 | String::WriteToFlat(element, sink, 0, element_length); |
| 6869 | sink += element_length; |
| 6870 | } |
| 6871 | ASSERT(sink == end); |
| 6872 | |
| 6873 | ASSERT(!answer->HasOnlyAsciiChars()); // Use %_FastAsciiArrayJoin instead. |
| 6874 | return answer; |
| 6875 | } |
| 6876 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6877 | template <typename Char> |
| 6878 | static void JoinSparseArrayWithSeparator(FixedArray* elements, |
| 6879 | int elements_length, |
| 6880 | uint32_t array_length, |
| 6881 | String* separator, |
| 6882 | Vector<Char> buffer) { |
| 6883 | int previous_separator_position = 0; |
| 6884 | int separator_length = separator->length(); |
| 6885 | int cursor = 0; |
| 6886 | for (int i = 0; i < elements_length; i += 2) { |
| 6887 | int position = NumberToInt32(elements->get(i)); |
| 6888 | String* string = String::cast(elements->get(i + 1)); |
| 6889 | int string_length = string->length(); |
| 6890 | if (string->length() > 0) { |
| 6891 | while (previous_separator_position < position) { |
| 6892 | String::WriteToFlat<Char>(separator, &buffer[cursor], |
| 6893 | 0, separator_length); |
| 6894 | cursor += separator_length; |
| 6895 | previous_separator_position++; |
| 6896 | } |
| 6897 | String::WriteToFlat<Char>(string, &buffer[cursor], |
| 6898 | 0, string_length); |
| 6899 | cursor += string->length(); |
| 6900 | } |
| 6901 | } |
| 6902 | if (separator_length > 0) { |
| 6903 | // Array length must be representable as a signed 32-bit number, |
| 6904 | // otherwise the total string length would have been too large. |
| 6905 | ASSERT(array_length <= 0x7fffffff); // Is int32_t. |
| 6906 | int last_array_index = static_cast<int>(array_length - 1); |
| 6907 | while (previous_separator_position < last_array_index) { |
| 6908 | String::WriteToFlat<Char>(separator, &buffer[cursor], |
| 6909 | 0, separator_length); |
| 6910 | cursor += separator_length; |
| 6911 | previous_separator_position++; |
| 6912 | } |
| 6913 | } |
| 6914 | ASSERT(cursor <= buffer.length()); |
| 6915 | } |
| 6916 | |
| 6917 | |
| 6918 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SparseJoinWithSeparator) { |
| 6919 | NoHandleAllocation ha; |
| 6920 | ASSERT(args.length() == 3); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6921 | CONVERT_ARG_CHECKED(JSArray, elements_array, 0); |
| 6922 | RUNTIME_ASSERT(elements_array->HasFastElements() || |
| 6923 | elements_array->HasFastSmiOnlyElements()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6924 | CONVERT_NUMBER_CHECKED(uint32_t, array_length, Uint32, args[1]); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6925 | CONVERT_ARG_CHECKED(String, separator, 2); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6926 | // elements_array is fast-mode JSarray of alternating positions |
| 6927 | // (increasing order) and strings. |
| 6928 | // array_length is length of original array (used to add separators); |
| 6929 | // separator is string to put between elements. Assumed to be non-empty. |
| 6930 | |
| 6931 | // Find total length of join result. |
| 6932 | int string_length = 0; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 6933 | bool is_ascii = separator->IsAsciiRepresentation(); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 6934 | int max_string_length; |
| 6935 | if (is_ascii) { |
| 6936 | max_string_length = SeqAsciiString::kMaxLength; |
| 6937 | } else { |
| 6938 | max_string_length = SeqTwoByteString::kMaxLength; |
| 6939 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6940 | bool overflow = false; |
| 6941 | CONVERT_NUMBER_CHECKED(int, elements_length, |
| 6942 | Int32, elements_array->length()); |
| 6943 | RUNTIME_ASSERT((elements_length & 1) == 0); // Even length. |
| 6944 | FixedArray* elements = FixedArray::cast(elements_array->elements()); |
| 6945 | for (int i = 0; i < elements_length; i += 2) { |
| 6946 | RUNTIME_ASSERT(elements->get(i)->IsNumber()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6947 | RUNTIME_ASSERT(elements->get(i + 1)->IsString()); |
| 6948 | String* string = String::cast(elements->get(i + 1)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6949 | int length = string->length(); |
| 6950 | if (is_ascii && !string->IsAsciiRepresentation()) { |
| 6951 | is_ascii = false; |
| 6952 | max_string_length = SeqTwoByteString::kMaxLength; |
| 6953 | } |
| 6954 | if (length > max_string_length || |
| 6955 | max_string_length - length < string_length) { |
| 6956 | overflow = true; |
| 6957 | break; |
| 6958 | } |
| 6959 | string_length += length; |
| 6960 | } |
| 6961 | int separator_length = separator->length(); |
| 6962 | if (!overflow && separator_length > 0) { |
| 6963 | if (array_length <= 0x7fffffffu) { |
| 6964 | int separator_count = static_cast<int>(array_length) - 1; |
| 6965 | int remaining_length = max_string_length - string_length; |
| 6966 | if ((remaining_length / separator_length) >= separator_count) { |
| 6967 | string_length += separator_length * (array_length - 1); |
| 6968 | } else { |
| 6969 | // Not room for the separators within the maximal string length. |
| 6970 | overflow = true; |
| 6971 | } |
| 6972 | } else { |
| 6973 | // Nonempty separator and at least 2^31-1 separators necessary |
| 6974 | // means that the string is too large to create. |
| 6975 | STATIC_ASSERT(String::kMaxLength < 0x7fffffff); |
| 6976 | overflow = true; |
| 6977 | } |
| 6978 | } |
| 6979 | if (overflow) { |
| 6980 | // Throw OutOfMemory exception for creating too large a string. |
| 6981 | V8::FatalProcessOutOfMemory("Array join result too large."); |
| 6982 | } |
| 6983 | |
| 6984 | if (is_ascii) { |
| 6985 | MaybeObject* result_allocation = |
| 6986 | isolate->heap()->AllocateRawAsciiString(string_length); |
| 6987 | if (result_allocation->IsFailure()) return result_allocation; |
| 6988 | SeqAsciiString* result_string = |
| 6989 | SeqAsciiString::cast(result_allocation->ToObjectUnchecked()); |
| 6990 | JoinSparseArrayWithSeparator<char>(elements, |
| 6991 | elements_length, |
| 6992 | array_length, |
| 6993 | separator, |
| 6994 | Vector<char>(result_string->GetChars(), |
| 6995 | string_length)); |
| 6996 | return result_string; |
| 6997 | } else { |
| 6998 | MaybeObject* result_allocation = |
| 6999 | isolate->heap()->AllocateRawTwoByteString(string_length); |
| 7000 | if (result_allocation->IsFailure()) return result_allocation; |
| 7001 | SeqTwoByteString* result_string = |
| 7002 | SeqTwoByteString::cast(result_allocation->ToObjectUnchecked()); |
| 7003 | JoinSparseArrayWithSeparator<uc16>(elements, |
| 7004 | elements_length, |
| 7005 | array_length, |
| 7006 | separator, |
| 7007 | Vector<uc16>(result_string->GetChars(), |
| 7008 | string_length)); |
| 7009 | return result_string; |
| 7010 | } |
| 7011 | } |
| 7012 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7013 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7014 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberOr) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7015 | NoHandleAllocation ha; |
| 7016 | ASSERT(args.length() == 2); |
| 7017 | |
| 7018 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 7019 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7020 | return isolate->heap()->NumberFromInt32(x | y); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7021 | } |
| 7022 | |
| 7023 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7024 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberAnd) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7025 | NoHandleAllocation ha; |
| 7026 | ASSERT(args.length() == 2); |
| 7027 | |
| 7028 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 7029 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7030 | return isolate->heap()->NumberFromInt32(x & y); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7031 | } |
| 7032 | |
| 7033 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7034 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberXor) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7035 | NoHandleAllocation ha; |
| 7036 | ASSERT(args.length() == 2); |
| 7037 | |
| 7038 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 7039 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7040 | return isolate->heap()->NumberFromInt32(x ^ y); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7041 | } |
| 7042 | |
| 7043 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7044 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberNot) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7045 | NoHandleAllocation ha; |
| 7046 | ASSERT(args.length() == 1); |
| 7047 | |
| 7048 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7049 | return isolate->heap()->NumberFromInt32(~x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7050 | } |
| 7051 | |
| 7052 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7053 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberShl) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7054 | NoHandleAllocation ha; |
| 7055 | ASSERT(args.length() == 2); |
| 7056 | |
| 7057 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 7058 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7059 | return isolate->heap()->NumberFromInt32(x << (y & 0x1f)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7060 | } |
| 7061 | |
| 7062 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7063 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberShr) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7064 | NoHandleAllocation ha; |
| 7065 | ASSERT(args.length() == 2); |
| 7066 | |
| 7067 | CONVERT_NUMBER_CHECKED(uint32_t, x, Uint32, args[0]); |
| 7068 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7069 | return isolate->heap()->NumberFromUint32(x >> (y & 0x1f)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7070 | } |
| 7071 | |
| 7072 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7073 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberSar) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7074 | NoHandleAllocation ha; |
| 7075 | ASSERT(args.length() == 2); |
| 7076 | |
| 7077 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 7078 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7079 | return isolate->heap()->NumberFromInt32(ArithmeticShiftRight(x, y & 0x1f)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7080 | } |
| 7081 | |
| 7082 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7083 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberEquals) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7084 | NoHandleAllocation ha; |
| 7085 | ASSERT(args.length() == 2); |
| 7086 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7087 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 7088 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7089 | if (isnan(x)) return Smi::FromInt(NOT_EQUAL); |
| 7090 | if (isnan(y)) return Smi::FromInt(NOT_EQUAL); |
| 7091 | if (x == y) return Smi::FromInt(EQUAL); |
| 7092 | Object* result; |
| 7093 | if ((fpclassify(x) == FP_ZERO) && (fpclassify(y) == FP_ZERO)) { |
| 7094 | result = Smi::FromInt(EQUAL); |
| 7095 | } else { |
| 7096 | result = Smi::FromInt(NOT_EQUAL); |
| 7097 | } |
| 7098 | return result; |
| 7099 | } |
| 7100 | |
| 7101 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7102 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringEquals) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7103 | NoHandleAllocation ha; |
| 7104 | ASSERT(args.length() == 2); |
| 7105 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7106 | CONVERT_ARG_CHECKED(String, x, 0); |
| 7107 | CONVERT_ARG_CHECKED(String, y, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7108 | |
| 7109 | bool not_equal = !x->Equals(y); |
| 7110 | // This is slightly convoluted because the value that signifies |
| 7111 | // equality is 0 and inequality is 1 so we have to negate the result |
| 7112 | // from String::Equals. |
| 7113 | ASSERT(not_equal == 0 || not_equal == 1); |
| 7114 | STATIC_CHECK(EQUAL == 0); |
| 7115 | STATIC_CHECK(NOT_EQUAL == 1); |
| 7116 | return Smi::FromInt(not_equal); |
| 7117 | } |
| 7118 | |
| 7119 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7120 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberCompare) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7121 | NoHandleAllocation ha; |
| 7122 | ASSERT(args.length() == 3); |
| 7123 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7124 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 7125 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7126 | if (isnan(x) || isnan(y)) return args[2]; |
| 7127 | if (x == y) return Smi::FromInt(EQUAL); |
| 7128 | if (isless(x, y)) return Smi::FromInt(LESS); |
| 7129 | return Smi::FromInt(GREATER); |
| 7130 | } |
| 7131 | |
| 7132 | |
| 7133 | // Compare two Smis as if they were converted to strings and then |
| 7134 | // compared lexicographically. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7135 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SmiLexicographicCompare) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7136 | NoHandleAllocation ha; |
| 7137 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7138 | CONVERT_SMI_ARG_CHECKED(x_value, 0); |
| 7139 | CONVERT_SMI_ARG_CHECKED(y_value, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7140 | |
| 7141 | // If the integers are equal so are the string representations. |
| 7142 | if (x_value == y_value) return Smi::FromInt(EQUAL); |
| 7143 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7144 | // If one of the integers is zero the normal integer order is the |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7145 | // same as the lexicographic order of the string representations. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7146 | if (x_value == 0 || y_value == 0) |
| 7147 | return Smi::FromInt(x_value < y_value ? LESS : GREATER); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7148 | |
| 7149 | // If only one of the integers is negative the negative number is |
| 7150 | // smallest because the char code of '-' is less than the char code |
| 7151 | // of any digit. Otherwise, we make both values positive. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7152 | |
| 7153 | // Use unsigned values otherwise the logic is incorrect for -MIN_INT on |
| 7154 | // architectures using 32-bit Smis. |
| 7155 | uint32_t x_scaled = x_value; |
| 7156 | uint32_t y_scaled = y_value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7157 | if (x_value < 0 || y_value < 0) { |
| 7158 | if (y_value >= 0) return Smi::FromInt(LESS); |
| 7159 | if (x_value >= 0) return Smi::FromInt(GREATER); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7160 | x_scaled = -x_value; |
| 7161 | y_scaled = -y_value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7162 | } |
| 7163 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7164 | static const uint32_t kPowersOf10[] = { |
| 7165 | 1, 10, 100, 1000, 10*1000, 100*1000, |
| 7166 | 1000*1000, 10*1000*1000, 100*1000*1000, |
| 7167 | 1000*1000*1000 |
| 7168 | }; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7169 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7170 | // If the integers have the same number of decimal digits they can be |
| 7171 | // compared directly as the numeric order is the same as the |
| 7172 | // lexicographic order. If one integer has fewer digits, it is scaled |
| 7173 | // by some power of 10 to have the same number of digits as the longer |
| 7174 | // integer. If the scaled integers are equal it means the shorter |
| 7175 | // integer comes first in the lexicographic order. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7176 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7177 | // From http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 |
| 7178 | int x_log2 = IntegerLog2(x_scaled); |
| 7179 | int x_log10 = ((x_log2 + 1) * 1233) >> 12; |
| 7180 | x_log10 -= x_scaled < kPowersOf10[x_log10]; |
| 7181 | |
| 7182 | int y_log2 = IntegerLog2(y_scaled); |
| 7183 | int y_log10 = ((y_log2 + 1) * 1233) >> 12; |
| 7184 | y_log10 -= y_scaled < kPowersOf10[y_log10]; |
| 7185 | |
| 7186 | int tie = EQUAL; |
| 7187 | |
| 7188 | if (x_log10 < y_log10) { |
| 7189 | // X has fewer digits. We would like to simply scale up X but that |
| 7190 | // might overflow, e.g when comparing 9 with 1_000_000_000, 9 would |
| 7191 | // be scaled up to 9_000_000_000. So we scale up by the next |
| 7192 | // smallest power and scale down Y to drop one digit. It is OK to |
| 7193 | // drop one digit from the longer integer since the final digit is |
| 7194 | // past the length of the shorter integer. |
| 7195 | x_scaled *= kPowersOf10[y_log10 - x_log10 - 1]; |
| 7196 | y_scaled /= 10; |
| 7197 | tie = LESS; |
| 7198 | } else if (y_log10 < x_log10) { |
| 7199 | y_scaled *= kPowersOf10[x_log10 - y_log10 - 1]; |
| 7200 | x_scaled /= 10; |
| 7201 | tie = GREATER; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7202 | } |
| 7203 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7204 | if (x_scaled < y_scaled) return Smi::FromInt(LESS); |
| 7205 | if (x_scaled > y_scaled) return Smi::FromInt(GREATER); |
| 7206 | return Smi::FromInt(tie); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7207 | } |
| 7208 | |
| 7209 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7210 | static Object* StringInputBufferCompare(RuntimeState* state, |
| 7211 | String* x, |
| 7212 | String* y) { |
| 7213 | StringInputBuffer& bufx = *state->string_input_buffer_compare_bufx(); |
| 7214 | StringInputBuffer& bufy = *state->string_input_buffer_compare_bufy(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7215 | bufx.Reset(x); |
| 7216 | bufy.Reset(y); |
| 7217 | while (bufx.has_more() && bufy.has_more()) { |
| 7218 | int d = bufx.GetNext() - bufy.GetNext(); |
| 7219 | if (d < 0) return Smi::FromInt(LESS); |
| 7220 | else if (d > 0) return Smi::FromInt(GREATER); |
| 7221 | } |
| 7222 | |
| 7223 | // x is (non-trivial) prefix of y: |
| 7224 | if (bufy.has_more()) return Smi::FromInt(LESS); |
| 7225 | // y is prefix of x: |
| 7226 | return Smi::FromInt(bufx.has_more() ? GREATER : EQUAL); |
| 7227 | } |
| 7228 | |
| 7229 | |
| 7230 | static Object* FlatStringCompare(String* x, String* y) { |
| 7231 | ASSERT(x->IsFlat()); |
| 7232 | ASSERT(y->IsFlat()); |
| 7233 | Object* equal_prefix_result = Smi::FromInt(EQUAL); |
| 7234 | int prefix_length = x->length(); |
| 7235 | if (y->length() < prefix_length) { |
| 7236 | prefix_length = y->length(); |
| 7237 | equal_prefix_result = Smi::FromInt(GREATER); |
| 7238 | } else if (y->length() > prefix_length) { |
| 7239 | equal_prefix_result = Smi::FromInt(LESS); |
| 7240 | } |
| 7241 | int r; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 7242 | String::FlatContent x_content = x->GetFlatContent(); |
| 7243 | String::FlatContent y_content = y->GetFlatContent(); |
| 7244 | if (x_content.IsAscii()) { |
| 7245 | Vector<const char> x_chars = x_content.ToAsciiVector(); |
| 7246 | if (y_content.IsAscii()) { |
| 7247 | Vector<const char> y_chars = y_content.ToAsciiVector(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7248 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 7249 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 7250 | Vector<const uc16> y_chars = y_content.ToUC16Vector(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7251 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 7252 | } |
| 7253 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 7254 | Vector<const uc16> x_chars = x_content.ToUC16Vector(); |
| 7255 | if (y_content.IsAscii()) { |
| 7256 | Vector<const char> y_chars = y_content.ToAsciiVector(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7257 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 7258 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 7259 | Vector<const uc16> y_chars = y_content.ToUC16Vector(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7260 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 7261 | } |
| 7262 | } |
| 7263 | Object* result; |
| 7264 | if (r == 0) { |
| 7265 | result = equal_prefix_result; |
| 7266 | } else { |
| 7267 | result = (r < 0) ? Smi::FromInt(LESS) : Smi::FromInt(GREATER); |
| 7268 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7269 | ASSERT(result == |
| 7270 | StringInputBufferCompare(Isolate::Current()->runtime_state(), x, y)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7271 | return result; |
| 7272 | } |
| 7273 | |
| 7274 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7275 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringCompare) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7276 | NoHandleAllocation ha; |
| 7277 | ASSERT(args.length() == 2); |
| 7278 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7279 | CONVERT_ARG_CHECKED(String, x, 0); |
| 7280 | CONVERT_ARG_CHECKED(String, y, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7281 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7282 | isolate->counters()->string_compare_runtime()->Increment(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7283 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7284 | // A few fast case tests before we flatten. |
| 7285 | if (x == y) return Smi::FromInt(EQUAL); |
| 7286 | if (y->length() == 0) { |
| 7287 | if (x->length() == 0) return Smi::FromInt(EQUAL); |
| 7288 | return Smi::FromInt(GREATER); |
| 7289 | } else if (x->length() == 0) { |
| 7290 | return Smi::FromInt(LESS); |
| 7291 | } |
| 7292 | |
| 7293 | int d = x->Get(0) - y->Get(0); |
| 7294 | if (d < 0) return Smi::FromInt(LESS); |
| 7295 | else if (d > 0) return Smi::FromInt(GREATER); |
| 7296 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7297 | Object* obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7298 | { MaybeObject* maybe_obj = isolate->heap()->PrepareForCompare(x); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7299 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7300 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7301 | { MaybeObject* maybe_obj = isolate->heap()->PrepareForCompare(y); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7302 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7303 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7304 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7305 | return (x->IsFlat() && y->IsFlat()) ? FlatStringCompare(x, y) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7306 | : StringInputBufferCompare(isolate->runtime_state(), x, y); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7307 | } |
| 7308 | |
| 7309 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7310 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_acos) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7311 | NoHandleAllocation ha; |
| 7312 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7313 | isolate->counters()->math_acos()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7314 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7315 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7316 | return isolate->transcendental_cache()->Get(TranscendentalCache::ACOS, x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7317 | } |
| 7318 | |
| 7319 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7320 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_asin) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7321 | NoHandleAllocation ha; |
| 7322 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7323 | isolate->counters()->math_asin()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7324 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7325 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7326 | return isolate->transcendental_cache()->Get(TranscendentalCache::ASIN, x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7327 | } |
| 7328 | |
| 7329 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7330 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_atan) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7331 | NoHandleAllocation ha; |
| 7332 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7333 | isolate->counters()->math_atan()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7334 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7335 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7336 | return isolate->transcendental_cache()->Get(TranscendentalCache::ATAN, x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7337 | } |
| 7338 | |
| 7339 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7340 | static const double kPiDividedBy4 = 0.78539816339744830962; |
| 7341 | |
| 7342 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7343 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_atan2) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7344 | NoHandleAllocation ha; |
| 7345 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7346 | isolate->counters()->math_atan2()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7347 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7348 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 7349 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7350 | double result; |
| 7351 | if (isinf(x) && isinf(y)) { |
| 7352 | // Make sure that the result in case of two infinite arguments |
| 7353 | // is a multiple of Pi / 4. The sign of the result is determined |
| 7354 | // by the first argument (x) and the sign of the second argument |
| 7355 | // determines the multiplier: one or three. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7356 | int multiplier = (x < 0) ? -1 : 1; |
| 7357 | if (y < 0) multiplier *= 3; |
| 7358 | result = multiplier * kPiDividedBy4; |
| 7359 | } else { |
| 7360 | result = atan2(x, y); |
| 7361 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7362 | return isolate->heap()->AllocateHeapNumber(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7363 | } |
| 7364 | |
| 7365 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7366 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_ceil) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7367 | NoHandleAllocation ha; |
| 7368 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7369 | isolate->counters()->math_ceil()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7370 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7371 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7372 | return isolate->heap()->NumberFromDouble(ceiling(x)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7373 | } |
| 7374 | |
| 7375 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7376 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_cos) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7377 | NoHandleAllocation ha; |
| 7378 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7379 | isolate->counters()->math_cos()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7380 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7381 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7382 | return isolate->transcendental_cache()->Get(TranscendentalCache::COS, x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7383 | } |
| 7384 | |
| 7385 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7386 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_exp) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7387 | NoHandleAllocation ha; |
| 7388 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7389 | isolate->counters()->math_exp()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7390 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7391 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7392 | return isolate->transcendental_cache()->Get(TranscendentalCache::EXP, x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7393 | } |
| 7394 | |
| 7395 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7396 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_floor) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7397 | NoHandleAllocation ha; |
| 7398 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7399 | isolate->counters()->math_floor()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7400 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7401 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7402 | return isolate->heap()->NumberFromDouble(floor(x)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7403 | } |
| 7404 | |
| 7405 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7406 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_log) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7407 | NoHandleAllocation ha; |
| 7408 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7409 | isolate->counters()->math_log()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7410 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7411 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7412 | return isolate->transcendental_cache()->Get(TranscendentalCache::LOG, x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7413 | } |
| 7414 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7415 | // Slow version of Math.pow. We check for fast paths for special cases. |
| 7416 | // Used if SSE2/VFP3 is not available. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7417 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_pow) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7418 | NoHandleAllocation ha; |
| 7419 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7420 | isolate->counters()->math_pow()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7421 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7422 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7423 | |
| 7424 | // If the second argument is a smi, it is much faster to call the |
| 7425 | // custom powi() function than the generic pow(). |
| 7426 | if (args[1]->IsSmi()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7427 | int y = args.smi_at(1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7428 | return isolate->heap()->NumberFromDouble(power_double_int(x, y)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7429 | } |
| 7430 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7431 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7432 | int y_int = static_cast<int>(y); |
| 7433 | double result; |
| 7434 | if (y == y_int) { |
| 7435 | result = power_double_int(x, y_int); // Returns 1 if exponent is 0. |
| 7436 | } else if (y == 0.5) { |
| 7437 | result = (isinf(x)) ? V8_INFINITY |
| 7438 | : fast_sqrt(x + 0.0); // Convert -0 to +0. |
| 7439 | } else if (y == -0.5) { |
| 7440 | result = (isinf(x)) ? 0 |
| 7441 | : 1.0 / fast_sqrt(x + 0.0); // Convert -0 to +0. |
| 7442 | } else { |
| 7443 | result = power_double_double(x, y); |
| 7444 | } |
| 7445 | if (isnan(result)) return isolate->heap()->nan_value(); |
| 7446 | return isolate->heap()->AllocateHeapNumber(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7447 | } |
| 7448 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7449 | // Fast version of Math.pow if we know that y is not an integer and y is not |
| 7450 | // -0.5 or 0.5. Used as slow case from full codegen. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7451 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_pow_cfunction) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7452 | NoHandleAllocation ha; |
| 7453 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7454 | isolate->counters()->math_pow()->Increment(); |
| 7455 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7456 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 7457 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7458 | if (y == 0) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7459 | return Smi::FromInt(1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7460 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7461 | double result = power_double_double(x, y); |
| 7462 | if (isnan(result)) return isolate->heap()->nan_value(); |
| 7463 | return isolate->heap()->AllocateHeapNumber(result); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7464 | } |
| 7465 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7466 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7467 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7468 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RoundNumber) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7469 | NoHandleAllocation ha; |
| 7470 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7471 | isolate->counters()->math_round()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7472 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7473 | if (!args[0]->IsHeapNumber()) { |
| 7474 | // Must be smi. Return the argument unchanged for all the other types |
| 7475 | // to make fuzz-natives test happy. |
| 7476 | return args[0]; |
| 7477 | } |
| 7478 | |
| 7479 | HeapNumber* number = reinterpret_cast<HeapNumber*>(args[0]); |
| 7480 | |
| 7481 | double value = number->value(); |
| 7482 | int exponent = number->get_exponent(); |
| 7483 | int sign = number->get_sign(); |
| 7484 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7485 | if (exponent < -1) { |
| 7486 | // Number in range ]-0.5..0.5[. These always round to +/-zero. |
| 7487 | if (sign) return isolate->heap()->minus_zero_value(); |
| 7488 | return Smi::FromInt(0); |
| 7489 | } |
| 7490 | |
| 7491 | // We compare with kSmiValueSize - 2 because (2^30 - 0.1) has exponent 29 and |
| 7492 | // should be rounded to 2^30, which is not smi (for 31-bit smis, similar |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7493 | // argument holds for 32-bit smis). |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7494 | if (!sign && exponent < kSmiValueSize - 2) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7495 | return Smi::FromInt(static_cast<int>(value + 0.5)); |
| 7496 | } |
| 7497 | |
| 7498 | // If the magnitude is big enough, there's no place for fraction part. If we |
| 7499 | // try to add 0.5 to this number, 1.0 will be added instead. |
| 7500 | if (exponent >= 52) { |
| 7501 | return number; |
| 7502 | } |
| 7503 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7504 | if (sign && value >= -0.5) return isolate->heap()->minus_zero_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7505 | |
| 7506 | // Do not call NumberFromDouble() to avoid extra checks. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7507 | return isolate->heap()->AllocateHeapNumber(floor(value + 0.5)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7508 | } |
| 7509 | |
| 7510 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7511 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_sin) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7512 | NoHandleAllocation ha; |
| 7513 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7514 | isolate->counters()->math_sin()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7515 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7516 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7517 | return isolate->transcendental_cache()->Get(TranscendentalCache::SIN, x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7518 | } |
| 7519 | |
| 7520 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7521 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_sqrt) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7522 | NoHandleAllocation ha; |
| 7523 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7524 | isolate->counters()->math_sqrt()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7525 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7526 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7527 | return isolate->heap()->AllocateHeapNumber(fast_sqrt(x)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7528 | } |
| 7529 | |
| 7530 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7531 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_tan) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7532 | NoHandleAllocation ha; |
| 7533 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7534 | isolate->counters()->math_tan()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7535 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7536 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7537 | return isolate->transcendental_cache()->Get(TranscendentalCache::TAN, x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7538 | } |
| 7539 | |
| 7540 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 7541 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateMakeDay) { |
| 7542 | NoHandleAllocation ha; |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 7543 | ASSERT(args.length() == 2); |
| 7544 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7545 | CONVERT_SMI_ARG_CHECKED(year, 0); |
| 7546 | CONVERT_SMI_ARG_CHECKED(month, 1); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 7547 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7548 | return Smi::FromInt(isolate->date_cache()->DaysFromYearMonth(year, month)); |
| 7549 | } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 7550 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 7551 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7552 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateSetValue) { |
| 7553 | HandleScope scope(isolate); |
| 7554 | ASSERT(args.length() == 3); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 7555 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7556 | CONVERT_ARG_HANDLE_CHECKED(JSDate, date, 0); |
| 7557 | CONVERT_DOUBLE_ARG_CHECKED(time, 1); |
| 7558 | CONVERT_SMI_ARG_CHECKED(is_utc, 2); |
| 7559 | |
| 7560 | DateCache* date_cache = isolate->date_cache(); |
| 7561 | |
| 7562 | Object* value = NULL; |
| 7563 | bool is_value_nan = false; |
| 7564 | if (isnan(time)) { |
| 7565 | value = isolate->heap()->nan_value(); |
| 7566 | is_value_nan = true; |
| 7567 | } else if (!is_utc && |
| 7568 | (time < -DateCache::kMaxTimeBeforeUTCInMs || |
| 7569 | time > DateCache::kMaxTimeBeforeUTCInMs)) { |
| 7570 | value = isolate->heap()->nan_value(); |
| 7571 | is_value_nan = true; |
| 7572 | } else { |
| 7573 | time = is_utc ? time : date_cache->ToUTC(static_cast<int64_t>(time)); |
| 7574 | if (time < -DateCache::kMaxTimeInMs || |
| 7575 | time > DateCache::kMaxTimeInMs) { |
| 7576 | value = isolate->heap()->nan_value(); |
| 7577 | is_value_nan = true; |
| 7578 | } else { |
| 7579 | MaybeObject* maybe_result = |
| 7580 | isolate->heap()->AllocateHeapNumber(DoubleToInteger(time)); |
| 7581 | if (!maybe_result->ToObject(&value)) return maybe_result; |
| 7582 | } |
| 7583 | } |
| 7584 | date->SetValue(value, is_value_nan); |
Ben Murdoch | 84774f4 | 2012-05-17 10:59:30 +0100 | [diff] [blame^] | 7585 | return value; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7586 | } |
| 7587 | |
| 7588 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7589 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewArgumentsFast) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7590 | HandleScope scope(isolate); |
| 7591 | ASSERT(args.length() == 3); |
| 7592 | |
| 7593 | Handle<JSFunction> callee = args.at<JSFunction>(0); |
| 7594 | Object** parameters = reinterpret_cast<Object**>(args[1]); |
| 7595 | const int argument_count = Smi::cast(args[2])->value(); |
| 7596 | |
| 7597 | Handle<JSObject> result = |
| 7598 | isolate->factory()->NewArgumentsObject(callee, argument_count); |
| 7599 | // Allocate the elements if needed. |
| 7600 | int parameter_count = callee->shared()->formal_parameter_count(); |
| 7601 | if (argument_count > 0) { |
| 7602 | if (parameter_count > 0) { |
| 7603 | int mapped_count = Min(argument_count, parameter_count); |
| 7604 | Handle<FixedArray> parameter_map = |
| 7605 | isolate->factory()->NewFixedArray(mapped_count + 2, NOT_TENURED); |
| 7606 | parameter_map->set_map( |
| 7607 | isolate->heap()->non_strict_arguments_elements_map()); |
| 7608 | |
| 7609 | Handle<Map> old_map(result->map()); |
| 7610 | Handle<Map> new_map = |
| 7611 | isolate->factory()->CopyMapDropTransitions(old_map); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 7612 | new_map->set_elements_kind(NON_STRICT_ARGUMENTS_ELEMENTS); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7613 | |
| 7614 | result->set_map(*new_map); |
| 7615 | result->set_elements(*parameter_map); |
| 7616 | |
| 7617 | // Store the context and the arguments array at the beginning of the |
| 7618 | // parameter map. |
| 7619 | Handle<Context> context(isolate->context()); |
| 7620 | Handle<FixedArray> arguments = |
| 7621 | isolate->factory()->NewFixedArray(argument_count, NOT_TENURED); |
| 7622 | parameter_map->set(0, *context); |
| 7623 | parameter_map->set(1, *arguments); |
| 7624 | |
| 7625 | // Loop over the actual parameters backwards. |
| 7626 | int index = argument_count - 1; |
| 7627 | while (index >= mapped_count) { |
| 7628 | // These go directly in the arguments array and have no |
| 7629 | // corresponding slot in the parameter map. |
| 7630 | arguments->set(index, *(parameters - index - 1)); |
| 7631 | --index; |
| 7632 | } |
| 7633 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7634 | Handle<ScopeInfo> scope_info(callee->shared()->scope_info()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7635 | while (index >= 0) { |
| 7636 | // Detect duplicate names to the right in the parameter list. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7637 | Handle<String> name(scope_info->ParameterName(index)); |
| 7638 | int context_local_count = scope_info->ContextLocalCount(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7639 | bool duplicate = false; |
| 7640 | for (int j = index + 1; j < parameter_count; ++j) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7641 | if (scope_info->ParameterName(j) == *name) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7642 | duplicate = true; |
| 7643 | break; |
| 7644 | } |
| 7645 | } |
| 7646 | |
| 7647 | if (duplicate) { |
| 7648 | // This goes directly in the arguments array with a hole in the |
| 7649 | // parameter map. |
| 7650 | arguments->set(index, *(parameters - index - 1)); |
| 7651 | parameter_map->set_the_hole(index + 2); |
| 7652 | } else { |
| 7653 | // The context index goes in the parameter map with a hole in the |
| 7654 | // arguments array. |
| 7655 | int context_index = -1; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7656 | for (int j = 0; j < context_local_count; ++j) { |
| 7657 | if (scope_info->ContextLocalName(j) == *name) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7658 | context_index = j; |
| 7659 | break; |
| 7660 | } |
| 7661 | } |
| 7662 | ASSERT(context_index >= 0); |
| 7663 | arguments->set_the_hole(index); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7664 | parameter_map->set(index + 2, Smi::FromInt( |
| 7665 | Context::MIN_CONTEXT_SLOTS + context_index)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7666 | } |
| 7667 | |
| 7668 | --index; |
| 7669 | } |
| 7670 | } else { |
| 7671 | // If there is no aliasing, the arguments object elements are not |
| 7672 | // special in any way. |
| 7673 | Handle<FixedArray> elements = |
| 7674 | isolate->factory()->NewFixedArray(argument_count, NOT_TENURED); |
| 7675 | result->set_elements(*elements); |
| 7676 | for (int i = 0; i < argument_count; ++i) { |
| 7677 | elements->set(i, *(parameters - i - 1)); |
| 7678 | } |
| 7679 | } |
| 7680 | } |
| 7681 | return *result; |
| 7682 | } |
| 7683 | |
| 7684 | |
| 7685 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewStrictArgumentsFast) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7686 | NoHandleAllocation ha; |
| 7687 | ASSERT(args.length() == 3); |
| 7688 | |
| 7689 | JSFunction* callee = JSFunction::cast(args[0]); |
| 7690 | Object** parameters = reinterpret_cast<Object**>(args[1]); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7691 | const int length = args.smi_at(2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7692 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7693 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7694 | { MaybeObject* maybe_result = |
| 7695 | isolate->heap()->AllocateArgumentsObject(callee, length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7696 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7697 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7698 | // Allocate the elements if needed. |
| 7699 | if (length > 0) { |
| 7700 | // Allocate the fixed array. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7701 | Object* obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7702 | { MaybeObject* maybe_obj = isolate->heap()->AllocateRawFixedArray(length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7703 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7704 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 7705 | |
| 7706 | AssertNoAllocation no_gc; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 7707 | FixedArray* array = reinterpret_cast<FixedArray*>(obj); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7708 | array->set_map_no_write_barrier(isolate->heap()->fixed_array_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7709 | array->set_length(length); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 7710 | |
| 7711 | WriteBarrierMode mode = array->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7712 | for (int i = 0; i < length; i++) { |
| 7713 | array->set(i, *--parameters, mode); |
| 7714 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7715 | JSObject::cast(result)->set_elements(FixedArray::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7716 | } |
| 7717 | return result; |
| 7718 | } |
| 7719 | |
| 7720 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7721 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewClosure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7722 | HandleScope scope(isolate); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7723 | ASSERT(args.length() == 3); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7724 | CONVERT_ARG_HANDLE_CHECKED(Context, context, 0); |
| 7725 | CONVERT_ARG_HANDLE_CHECKED(SharedFunctionInfo, shared, 1); |
| 7726 | CONVERT_BOOLEAN_ARG_CHECKED(pretenure, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7727 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7728 | // The caller ensures that we pretenure closures that are assigned |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7729 | // directly to properties. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7730 | PretenureFlag pretenure_flag = pretenure ? TENURED : NOT_TENURED; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7731 | Handle<JSFunction> result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7732 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 7733 | context, |
| 7734 | pretenure_flag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7735 | return *result; |
| 7736 | } |
| 7737 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7738 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7739 | // Find the arguments of the JavaScript function invocation that called |
| 7740 | // into C++ code. Collect these in a newly allocated array of handles (possibly |
| 7741 | // prefixed by a number of empty handles). |
| 7742 | static SmartArrayPointer<Handle<Object> > GetCallerArguments( |
| 7743 | int prefix_argc, |
| 7744 | int* total_argc) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7745 | // Find frame containing arguments passed to the caller. |
| 7746 | JavaScriptFrameIterator it; |
| 7747 | JavaScriptFrame* frame = it.frame(); |
| 7748 | List<JSFunction*> functions(2); |
| 7749 | frame->GetFunctions(&functions); |
| 7750 | if (functions.length() > 1) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7751 | int inlined_jsframe_index = functions.length() - 1; |
| 7752 | JSFunction* inlined_function = functions[inlined_jsframe_index]; |
| 7753 | Vector<SlotRef> args_slots = |
| 7754 | SlotRef::ComputeSlotMappingForArguments( |
| 7755 | frame, |
| 7756 | inlined_jsframe_index, |
| 7757 | inlined_function->shared()->formal_parameter_count()); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 7758 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7759 | int args_count = args_slots.length(); |
| 7760 | |
| 7761 | *total_argc = prefix_argc + args_count; |
| 7762 | SmartArrayPointer<Handle<Object> > param_data( |
| 7763 | NewArray<Handle<Object> >(*total_argc)); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7764 | for (int i = 0; i < args_count; i++) { |
| 7765 | Handle<Object> val = args_slots[i].GetValue(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7766 | param_data[prefix_argc + i] = val; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7767 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7768 | |
| 7769 | args_slots.Dispose(); |
| 7770 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7771 | return param_data; |
| 7772 | } else { |
| 7773 | it.AdvanceToArgumentsFrame(); |
| 7774 | frame = it.frame(); |
| 7775 | int args_count = frame->ComputeParametersCount(); |
| 7776 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7777 | *total_argc = prefix_argc + args_count; |
| 7778 | SmartArrayPointer<Handle<Object> > param_data( |
| 7779 | NewArray<Handle<Object> >(*total_argc)); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7780 | for (int i = 0; i < args_count; i++) { |
| 7781 | Handle<Object> val = Handle<Object>(frame->GetParameter(i)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7782 | param_data[prefix_argc + i] = val; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7783 | } |
| 7784 | return param_data; |
| 7785 | } |
| 7786 | } |
| 7787 | |
| 7788 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7789 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionBindArguments) { |
| 7790 | HandleScope scope(isolate); |
| 7791 | ASSERT(args.length() == 4); |
| 7792 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, bound_function, 0); |
| 7793 | RUNTIME_ASSERT(args[3]->IsNumber()); |
| 7794 | Handle<Object> bindee = args.at<Object>(1); |
| 7795 | |
| 7796 | // TODO(lrn): Create bound function in C++ code from premade shared info. |
| 7797 | bound_function->shared()->set_bound(true); |
| 7798 | // Get all arguments of calling function (Function.prototype.bind). |
| 7799 | int argc = 0; |
| 7800 | SmartArrayPointer<Handle<Object> > arguments = GetCallerArguments(0, &argc); |
| 7801 | // Don't count the this-arg. |
| 7802 | if (argc > 0) { |
| 7803 | ASSERT(*arguments[0] == args[2]); |
| 7804 | argc--; |
| 7805 | } else { |
| 7806 | ASSERT(args[2]->IsUndefined()); |
| 7807 | } |
| 7808 | // Initialize array of bindings (function, this, and any existing arguments |
| 7809 | // if the function was already bound). |
| 7810 | Handle<FixedArray> new_bindings; |
| 7811 | int i; |
| 7812 | if (bindee->IsJSFunction() && JSFunction::cast(*bindee)->shared()->bound()) { |
| 7813 | Handle<FixedArray> old_bindings( |
| 7814 | JSFunction::cast(*bindee)->function_bindings()); |
| 7815 | new_bindings = |
| 7816 | isolate->factory()->NewFixedArray(old_bindings->length() + argc); |
| 7817 | bindee = Handle<Object>(old_bindings->get(JSFunction::kBoundFunctionIndex)); |
| 7818 | i = 0; |
| 7819 | for (int n = old_bindings->length(); i < n; i++) { |
| 7820 | new_bindings->set(i, old_bindings->get(i)); |
| 7821 | } |
| 7822 | } else { |
| 7823 | int array_size = JSFunction::kBoundArgumentsStartIndex + argc; |
| 7824 | new_bindings = isolate->factory()->NewFixedArray(array_size); |
| 7825 | new_bindings->set(JSFunction::kBoundFunctionIndex, *bindee); |
| 7826 | new_bindings->set(JSFunction::kBoundThisIndex, args[2]); |
| 7827 | i = 2; |
| 7828 | } |
| 7829 | // Copy arguments, skipping the first which is "this_arg". |
| 7830 | for (int j = 0; j < argc; j++, i++) { |
| 7831 | new_bindings->set(i, *arguments[j + 1]); |
| 7832 | } |
| 7833 | new_bindings->set_map_no_write_barrier( |
| 7834 | isolate->heap()->fixed_cow_array_map()); |
| 7835 | bound_function->set_function_bindings(*new_bindings); |
| 7836 | |
| 7837 | // Update length. |
| 7838 | Handle<String> length_symbol = isolate->factory()->length_symbol(); |
| 7839 | Handle<Object> new_length(args.at<Object>(3)); |
| 7840 | PropertyAttributes attr = |
| 7841 | static_cast<PropertyAttributes>(DONT_DELETE | DONT_ENUM | READ_ONLY); |
| 7842 | ForceSetProperty(bound_function, length_symbol, new_length, attr); |
| 7843 | return *bound_function; |
| 7844 | } |
| 7845 | |
| 7846 | |
| 7847 | RUNTIME_FUNCTION(MaybeObject*, Runtime_BoundFunctionGetBindings) { |
| 7848 | HandleScope handles(isolate); |
| 7849 | ASSERT(args.length() == 1); |
| 7850 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, callable, 0); |
| 7851 | if (callable->IsJSFunction()) { |
| 7852 | Handle<JSFunction> function = Handle<JSFunction>::cast(callable); |
| 7853 | if (function->shared()->bound()) { |
| 7854 | Handle<FixedArray> bindings(function->function_bindings()); |
| 7855 | ASSERT(bindings->map() == isolate->heap()->fixed_cow_array_map()); |
| 7856 | return *isolate->factory()->NewJSArrayWithElements(bindings); |
| 7857 | } |
| 7858 | } |
| 7859 | return isolate->heap()->undefined_value(); |
| 7860 | } |
| 7861 | |
| 7862 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7863 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewObjectFromBound) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7864 | HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7865 | ASSERT(args.length() == 1); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 7866 | // First argument is a function to use as a constructor. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7867 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
| 7868 | RUNTIME_ASSERT(function->shared()->bound()); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 7869 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7870 | // The argument is a bound function. Extract its bound arguments |
| 7871 | // and callable. |
| 7872 | Handle<FixedArray> bound_args = |
| 7873 | Handle<FixedArray>(FixedArray::cast(function->function_bindings())); |
| 7874 | int bound_argc = bound_args->length() - JSFunction::kBoundArgumentsStartIndex; |
| 7875 | Handle<Object> bound_function( |
| 7876 | JSReceiver::cast(bound_args->get(JSFunction::kBoundFunctionIndex))); |
| 7877 | ASSERT(!bound_function->IsJSFunction() || |
| 7878 | !Handle<JSFunction>::cast(bound_function)->shared()->bound()); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 7879 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7880 | int total_argc = 0; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7881 | SmartArrayPointer<Handle<Object> > param_data = |
| 7882 | GetCallerArguments(bound_argc, &total_argc); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 7883 | for (int i = 0; i < bound_argc; i++) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7884 | param_data[i] = Handle<Object>(bound_args->get( |
| 7885 | JSFunction::kBoundArgumentsStartIndex + i)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 7886 | } |
| 7887 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7888 | if (!bound_function->IsJSFunction()) { |
| 7889 | bool exception_thrown; |
| 7890 | bound_function = Execution::TryGetConstructorDelegate(bound_function, |
| 7891 | &exception_thrown); |
| 7892 | if (exception_thrown) return Failure::Exception(); |
| 7893 | } |
| 7894 | ASSERT(bound_function->IsJSFunction()); |
| 7895 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 7896 | bool exception = false; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 7897 | Handle<Object> result = |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7898 | Execution::New(Handle<JSFunction>::cast(bound_function), |
| 7899 | total_argc, *param_data, &exception); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 7900 | if (exception) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7901 | return Failure::Exception(); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 7902 | } |
| 7903 | ASSERT(!result.is_null()); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 7904 | return *result; |
| 7905 | } |
| 7906 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7907 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7908 | static void TrySettingInlineConstructStub(Isolate* isolate, |
| 7909 | Handle<JSFunction> function) { |
| 7910 | Handle<Object> prototype = isolate->factory()->null_value(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 7911 | if (function->has_instance_prototype()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7912 | prototype = Handle<Object>(function->instance_prototype(), isolate); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 7913 | } |
| 7914 | if (function->shared()->CanGenerateInlineConstructor(*prototype)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7915 | ConstructStubCompiler compiler(isolate); |
| 7916 | Handle<Code> code = compiler.CompileConstructStub(function); |
| 7917 | function->shared()->set_construct_stub(*code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7918 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7919 | } |
| 7920 | |
| 7921 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7922 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewObject) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7923 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7924 | ASSERT(args.length() == 1); |
| 7925 | |
| 7926 | Handle<Object> constructor = args.at<Object>(0); |
| 7927 | |
| 7928 | // If the constructor isn't a proper function we throw a type error. |
| 7929 | if (!constructor->IsJSFunction()) { |
| 7930 | Vector< Handle<Object> > arguments = HandleVector(&constructor, 1); |
| 7931 | Handle<Object> type_error = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7932 | isolate->factory()->NewTypeError("not_constructor", arguments); |
| 7933 | return isolate->Throw(*type_error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7934 | } |
| 7935 | |
| 7936 | Handle<JSFunction> function = Handle<JSFunction>::cast(constructor); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7937 | |
| 7938 | // If function should not have prototype, construction is not allowed. In this |
| 7939 | // case generated code bailouts here, since function has no initial_map. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7940 | if (!function->should_have_prototype() && !function->shared()->bound()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7941 | Vector< Handle<Object> > arguments = HandleVector(&constructor, 1); |
| 7942 | Handle<Object> type_error = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7943 | isolate->factory()->NewTypeError("not_constructor", arguments); |
| 7944 | return isolate->Throw(*type_error); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7945 | } |
| 7946 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7947 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7948 | Debug* debug = isolate->debug(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7949 | // Handle stepping into constructors if step into is active. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7950 | if (debug->StepInActive()) { |
| 7951 | debug->HandleStepIn(function, Handle<Object>::null(), 0, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7952 | } |
| 7953 | #endif |
| 7954 | |
| 7955 | if (function->has_initial_map()) { |
| 7956 | if (function->initial_map()->instance_type() == JS_FUNCTION_TYPE) { |
| 7957 | // The 'Function' function ignores the receiver object when |
| 7958 | // called using 'new' and creates a new JSFunction object that |
| 7959 | // is returned. The receiver object is only used for error |
| 7960 | // reporting if an error occurs when constructing the new |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7961 | // JSFunction. FACTORY->NewJSObject() should not be used to |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7962 | // allocate JSFunctions since it does not properly initialize |
| 7963 | // the shared part of the function. Since the receiver is |
| 7964 | // ignored anyway, we use the global object as the receiver |
| 7965 | // instead of a new JSFunction object. This way, errors are |
| 7966 | // reported the same way whether or not 'Function' is called |
| 7967 | // using 'new'. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7968 | return isolate->context()->global(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7969 | } |
| 7970 | } |
| 7971 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7972 | // The function should be compiled for the optimization hints to be |
| 7973 | // available. We cannot use EnsureCompiled because that forces a |
| 7974 | // compilation through the shared function info which makes it |
| 7975 | // impossible for us to optimize. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7976 | if (!function->is_compiled()) { |
| 7977 | JSFunction::CompileLazy(function, CLEAR_EXCEPTION); |
| 7978 | } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 7979 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7980 | Handle<SharedFunctionInfo> shared(function->shared(), isolate); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 7981 | if (!function->has_initial_map() && |
| 7982 | shared->IsInobjectSlackTrackingInProgress()) { |
| 7983 | // The tracking is already in progress for another function. We can only |
| 7984 | // track one initial_map at a time, so we force the completion before the |
| 7985 | // function is called as a constructor for the first time. |
| 7986 | shared->CompleteInobjectSlackTracking(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 7987 | } |
| 7988 | |
| 7989 | bool first_allocation = !shared->live_objects_may_exist(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7990 | Handle<JSObject> result = isolate->factory()->NewJSObject(function); |
| 7991 | RETURN_IF_EMPTY_HANDLE(isolate, result); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 7992 | // Delay setting the stub if inobject slack tracking is in progress. |
| 7993 | if (first_allocation && !shared->IsInobjectSlackTrackingInProgress()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7994 | TrySettingInlineConstructStub(isolate, function); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7995 | } |
| 7996 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7997 | isolate->counters()->constructed_objects()->Increment(); |
| 7998 | isolate->counters()->constructed_objects_runtime()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7999 | |
| 8000 | return *result; |
| 8001 | } |
| 8002 | |
| 8003 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8004 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FinalizeInstanceSize) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8005 | HandleScope scope(isolate); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 8006 | ASSERT(args.length() == 1); |
| 8007 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8008 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 8009 | function->shared()->CompleteInobjectSlackTracking(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8010 | TrySettingInlineConstructStub(isolate, function); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 8011 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8012 | return isolate->heap()->undefined_value(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 8013 | } |
| 8014 | |
| 8015 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8016 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LazyCompile) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8017 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8018 | ASSERT(args.length() == 1); |
| 8019 | |
| 8020 | Handle<JSFunction> function = args.at<JSFunction>(0); |
| 8021 | #ifdef DEBUG |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 8022 | if (FLAG_trace_lazy && !function->shared()->is_compiled()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8023 | PrintF("[lazy: "); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8024 | function->PrintName(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8025 | PrintF("]\n"); |
| 8026 | } |
| 8027 | #endif |
| 8028 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 8029 | // Compile the target function. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8030 | ASSERT(!function->is_compiled()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8031 | if (!JSFunction::CompileLazy(function, KEEP_EXCEPTION)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8032 | return Failure::Exception(); |
| 8033 | } |
| 8034 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8035 | // All done. Return the compiled code. |
| 8036 | ASSERT(function->is_compiled()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8037 | return function->code(); |
| 8038 | } |
| 8039 | |
| 8040 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8041 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LazyRecompile) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8042 | HandleScope scope(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8043 | ASSERT(args.length() == 1); |
| 8044 | Handle<JSFunction> function = args.at<JSFunction>(0); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 8045 | |
| 8046 | // If the function is not compiled ignore the lazy |
| 8047 | // recompilation. This can happen if the debugger is activated and |
| 8048 | // the function is returned to the not compiled state. |
| 8049 | if (!function->shared()->is_compiled()) { |
| 8050 | function->ReplaceCode(function->shared()->code()); |
| 8051 | return function->code(); |
| 8052 | } |
| 8053 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8054 | // If the function is not optimizable or debugger is active continue using the |
| 8055 | // code from the full compiler. |
| 8056 | if (!function->shared()->code()->optimizable() || |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 8057 | isolate->DebuggerHasBreakPoints()) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 8058 | if (FLAG_trace_opt) { |
| 8059 | PrintF("[failed to optimize "); |
| 8060 | function->PrintName(); |
| 8061 | PrintF(": is code optimizable: %s, is debugger enabled: %s]\n", |
| 8062 | function->shared()->code()->optimizable() ? "T" : "F", |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 8063 | isolate->DebuggerHasBreakPoints() ? "T" : "F"); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 8064 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8065 | function->ReplaceCode(function->shared()->code()); |
| 8066 | return function->code(); |
| 8067 | } |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 8068 | function->shared()->code()->set_profiler_ticks(0); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8069 | if (JSFunction::CompileOptimized(function, |
| 8070 | AstNode::kNoNumber, |
| 8071 | CLEAR_EXCEPTION)) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8072 | return function->code(); |
| 8073 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 8074 | if (FLAG_trace_opt) { |
| 8075 | PrintF("[failed to optimize "); |
| 8076 | function->PrintName(); |
| 8077 | PrintF(": optimized compilation failed]\n"); |
| 8078 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8079 | function->ReplaceCode(function->shared()->code()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 8080 | return function->code(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8081 | } |
| 8082 | |
| 8083 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8084 | class ActivationsFinder : public ThreadVisitor { |
| 8085 | public: |
| 8086 | explicit ActivationsFinder(JSFunction* function) |
| 8087 | : function_(function), has_activations_(false) {} |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8088 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8089 | void VisitThread(Isolate* isolate, ThreadLocalTop* top) { |
| 8090 | if (has_activations_) return; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8091 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8092 | for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) { |
| 8093 | JavaScriptFrame* frame = it.frame(); |
| 8094 | if (frame->is_optimized() && frame->function() == function_) { |
| 8095 | has_activations_ = true; |
| 8096 | return; |
| 8097 | } |
| 8098 | } |
| 8099 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8100 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8101 | bool has_activations() { return has_activations_; } |
| 8102 | |
| 8103 | private: |
| 8104 | JSFunction* function_; |
| 8105 | bool has_activations_; |
| 8106 | }; |
| 8107 | |
| 8108 | |
| 8109 | static void MaterializeArgumentsObjectInFrame(Isolate* isolate, |
| 8110 | JavaScriptFrame* frame) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8111 | Handle<JSFunction> function(JSFunction::cast(frame->function()), isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8112 | Handle<Object> arguments; |
| 8113 | for (int i = frame->ComputeExpressionsCount() - 1; i >= 0; --i) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8114 | if (frame->GetExpression(i) == isolate->heap()->arguments_marker()) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8115 | if (arguments.is_null()) { |
| 8116 | // FunctionGetArguments can't throw an exception, so cast away the |
| 8117 | // doubt with an assert. |
| 8118 | arguments = Handle<Object>( |
| 8119 | Accessors::FunctionGetArguments(*function, |
| 8120 | NULL)->ToObjectUnchecked()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8121 | ASSERT(*arguments != isolate->heap()->null_value()); |
| 8122 | ASSERT(*arguments != isolate->heap()->undefined_value()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8123 | } |
| 8124 | frame->SetExpression(i, *arguments); |
| 8125 | } |
| 8126 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8127 | } |
| 8128 | |
| 8129 | |
| 8130 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NotifyDeoptimized) { |
| 8131 | HandleScope scope(isolate); |
| 8132 | ASSERT(args.length() == 1); |
| 8133 | RUNTIME_ASSERT(args[0]->IsSmi()); |
| 8134 | Deoptimizer::BailoutType type = |
| 8135 | static_cast<Deoptimizer::BailoutType>(args.smi_at(0)); |
| 8136 | Deoptimizer* deoptimizer = Deoptimizer::Grab(isolate); |
| 8137 | ASSERT(isolate->heap()->IsAllocationAllowed()); |
| 8138 | int jsframes = deoptimizer->jsframe_count(); |
| 8139 | |
| 8140 | deoptimizer->MaterializeHeapNumbers(); |
| 8141 | delete deoptimizer; |
| 8142 | |
| 8143 | JavaScriptFrameIterator it(isolate); |
| 8144 | for (int i = 0; i < jsframes - 1; i++) { |
| 8145 | MaterializeArgumentsObjectInFrame(isolate, it.frame()); |
| 8146 | it.Advance(); |
| 8147 | } |
| 8148 | |
| 8149 | JavaScriptFrame* frame = it.frame(); |
| 8150 | RUNTIME_ASSERT(frame->function()->IsJSFunction()); |
| 8151 | Handle<JSFunction> function(JSFunction::cast(frame->function()), isolate); |
| 8152 | MaterializeArgumentsObjectInFrame(isolate, frame); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8153 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8154 | if (type == Deoptimizer::EAGER) { |
| 8155 | RUNTIME_ASSERT(function->IsOptimized()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8156 | } |
| 8157 | |
| 8158 | // Avoid doing too much work when running with --always-opt and keep |
| 8159 | // the optimized code around. |
| 8160 | if (FLAG_always_opt || type == Deoptimizer::LAZY) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8161 | return isolate->heap()->undefined_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8162 | } |
| 8163 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8164 | // Find other optimized activations of the function. |
| 8165 | bool has_other_activations = false; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8166 | while (!it.done()) { |
| 8167 | JavaScriptFrame* frame = it.frame(); |
| 8168 | if (frame->is_optimized() && frame->function() == *function) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8169 | has_other_activations = true; |
| 8170 | break; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8171 | } |
| 8172 | it.Advance(); |
| 8173 | } |
| 8174 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8175 | if (!has_other_activations) { |
| 8176 | ActivationsFinder activations_finder(*function); |
| 8177 | isolate->thread_manager()->IterateArchivedThreads(&activations_finder); |
| 8178 | has_other_activations = activations_finder.has_activations(); |
| 8179 | } |
| 8180 | |
| 8181 | if (!has_other_activations) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8182 | if (FLAG_trace_deopt) { |
| 8183 | PrintF("[removing optimized code for: "); |
| 8184 | function->PrintName(); |
| 8185 | PrintF("]\n"); |
| 8186 | } |
| 8187 | function->ReplaceCode(function->shared()->code()); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 8188 | } else { |
| 8189 | Deoptimizer::DeoptimizeFunction(*function); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8190 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8191 | return isolate->heap()->undefined_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8192 | } |
| 8193 | |
| 8194 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8195 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NotifyOSR) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8196 | Deoptimizer* deoptimizer = Deoptimizer::Grab(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8197 | delete deoptimizer; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8198 | return isolate->heap()->undefined_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8199 | } |
| 8200 | |
| 8201 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8202 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeoptimizeFunction) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8203 | HandleScope scope(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8204 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8205 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8206 | if (!function->IsOptimized()) return isolate->heap()->undefined_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8207 | |
| 8208 | Deoptimizer::DeoptimizeFunction(*function); |
| 8209 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8210 | return isolate->heap()->undefined_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8211 | } |
| 8212 | |
| 8213 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8214 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RunningInSimulator) { |
| 8215 | #if defined(USE_SIMULATOR) |
| 8216 | return isolate->heap()->true_value(); |
| 8217 | #else |
| 8218 | return isolate->heap()->false_value(); |
| 8219 | #endif |
| 8220 | } |
| 8221 | |
| 8222 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8223 | RUNTIME_FUNCTION(MaybeObject*, Runtime_OptimizeFunctionOnNextCall) { |
| 8224 | HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8225 | RUNTIME_ASSERT(args.length() == 1 || args.length() == 2); |
| 8226 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
| 8227 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8228 | if (!function->IsOptimizable()) return isolate->heap()->undefined_value(); |
| 8229 | function->MarkForLazyRecompilation(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8230 | |
| 8231 | Code* unoptimized = function->shared()->code(); |
| 8232 | if (args.length() == 2 && |
| 8233 | unoptimized->kind() == Code::FUNCTION) { |
| 8234 | CONVERT_ARG_HANDLE_CHECKED(String, type, 1); |
| 8235 | CHECK(type->IsEqualTo(CStrVector("osr"))); |
| 8236 | isolate->runtime_profiler()->AttemptOnStackReplacement(*function); |
| 8237 | unoptimized->set_allow_osr_at_loop_nesting_level( |
| 8238 | Code::kMaxLoopNestingMarker); |
| 8239 | } |
| 8240 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8241 | return isolate->heap()->undefined_value(); |
| 8242 | } |
| 8243 | |
| 8244 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 8245 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetOptimizationStatus) { |
| 8246 | HandleScope scope(isolate); |
| 8247 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8248 | // The least significant bit (after untagging) indicates whether the |
| 8249 | // function is currently optimized, regardless of reason. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 8250 | if (!V8::UseCrankshaft()) { |
| 8251 | return Smi::FromInt(4); // 4 == "never". |
| 8252 | } |
| 8253 | if (FLAG_always_opt) { |
| 8254 | return Smi::FromInt(3); // 3 == "always". |
| 8255 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8256 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 8257 | return function->IsOptimized() ? Smi::FromInt(1) // 1 == "yes". |
| 8258 | : Smi::FromInt(2); // 2 == "no". |
| 8259 | } |
| 8260 | |
| 8261 | |
| 8262 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetOptimizationCount) { |
| 8263 | HandleScope scope(isolate); |
| 8264 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8265 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 8266 | return Smi::FromInt(function->shared()->opt_count()); |
| 8267 | } |
| 8268 | |
| 8269 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8270 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CompileForOnStackReplacement) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8271 | HandleScope scope(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8272 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8273 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8274 | |
| 8275 | // We're not prepared to handle a function with arguments object. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8276 | ASSERT(!function->shared()->uses_arguments()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8277 | |
| 8278 | // We have hit a back edge in an unoptimized frame for a function that was |
| 8279 | // selected for on-stack replacement. Find the unoptimized code object. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8280 | Handle<Code> unoptimized(function->shared()->code(), isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8281 | // Keep track of whether we've succeeded in optimizing. |
| 8282 | bool succeeded = unoptimized->optimizable(); |
| 8283 | if (succeeded) { |
| 8284 | // If we are trying to do OSR when there are already optimized |
| 8285 | // activations of the function, it means (a) the function is directly or |
| 8286 | // indirectly recursive and (b) an optimized invocation has been |
| 8287 | // deoptimized so that we are currently in an unoptimized activation. |
| 8288 | // Check for optimized activations of this function. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8289 | JavaScriptFrameIterator it(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8290 | while (succeeded && !it.done()) { |
| 8291 | JavaScriptFrame* frame = it.frame(); |
| 8292 | succeeded = !frame->is_optimized() || frame->function() != *function; |
| 8293 | it.Advance(); |
| 8294 | } |
| 8295 | } |
| 8296 | |
| 8297 | int ast_id = AstNode::kNoNumber; |
| 8298 | if (succeeded) { |
| 8299 | // The top JS function is this one, the PC is somewhere in the |
| 8300 | // unoptimized code. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8301 | JavaScriptFrameIterator it(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8302 | JavaScriptFrame* frame = it.frame(); |
| 8303 | ASSERT(frame->function() == *function); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8304 | ASSERT(frame->LookupCode() == *unoptimized); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8305 | ASSERT(unoptimized->contains(frame->pc())); |
| 8306 | |
| 8307 | // Use linear search of the unoptimized code's stack check table to find |
| 8308 | // the AST id matching the PC. |
| 8309 | Address start = unoptimized->instruction_start(); |
| 8310 | unsigned target_pc_offset = static_cast<unsigned>(frame->pc() - start); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8311 | Address table_cursor = start + unoptimized->stack_check_table_offset(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8312 | uint32_t table_length = Memory::uint32_at(table_cursor); |
| 8313 | table_cursor += kIntSize; |
| 8314 | for (unsigned i = 0; i < table_length; ++i) { |
| 8315 | // Table entries are (AST id, pc offset) pairs. |
| 8316 | uint32_t pc_offset = Memory::uint32_at(table_cursor + kIntSize); |
| 8317 | if (pc_offset == target_pc_offset) { |
| 8318 | ast_id = static_cast<int>(Memory::uint32_at(table_cursor)); |
| 8319 | break; |
| 8320 | } |
| 8321 | table_cursor += 2 * kIntSize; |
| 8322 | } |
| 8323 | ASSERT(ast_id != AstNode::kNoNumber); |
| 8324 | if (FLAG_trace_osr) { |
| 8325 | PrintF("[replacing on-stack at AST id %d in ", ast_id); |
| 8326 | function->PrintName(); |
| 8327 | PrintF("]\n"); |
| 8328 | } |
| 8329 | |
| 8330 | // Try to compile the optimized code. A true return value from |
| 8331 | // CompileOptimized means that compilation succeeded, not necessarily |
| 8332 | // that optimization succeeded. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8333 | if (JSFunction::CompileOptimized(function, ast_id, CLEAR_EXCEPTION) && |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 8334 | function->IsOptimized()) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8335 | DeoptimizationInputData* data = DeoptimizationInputData::cast( |
| 8336 | function->code()->deoptimization_data()); |
| 8337 | if (data->OsrPcOffset()->value() >= 0) { |
| 8338 | if (FLAG_trace_osr) { |
| 8339 | PrintF("[on-stack replacement offset %d in optimized code]\n", |
| 8340 | data->OsrPcOffset()->value()); |
| 8341 | } |
| 8342 | ASSERT(data->OsrAstId()->value() == ast_id); |
| 8343 | } else { |
| 8344 | // We may never generate the desired OSR entry if we emit an |
| 8345 | // early deoptimize. |
| 8346 | succeeded = false; |
| 8347 | } |
| 8348 | } else { |
| 8349 | succeeded = false; |
| 8350 | } |
| 8351 | } |
| 8352 | |
| 8353 | // Revert to the original stack checks in the original unoptimized code. |
| 8354 | if (FLAG_trace_osr) { |
| 8355 | PrintF("[restoring original stack checks in "); |
| 8356 | function->PrintName(); |
| 8357 | PrintF("]\n"); |
| 8358 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8359 | Handle<Code> check_code; |
| 8360 | #if defined(V8_TARGET_ARCH_IA32) || \ |
| 8361 | defined(V8_TARGET_ARCH_ARM) || \ |
| 8362 | defined(V8_TARGET_ARCH_MIPS) |
| 8363 | if (FLAG_count_based_interrupts) { |
| 8364 | InterruptStub interrupt_stub; |
| 8365 | check_code = interrupt_stub.GetCode(); |
| 8366 | } else // NOLINT |
| 8367 | #endif |
| 8368 | { // NOLINT |
| 8369 | StackCheckStub check_stub; |
| 8370 | check_code = check_stub.GetCode(); |
| 8371 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8372 | Handle<Code> replacement_code = isolate->builtins()->OnStackReplacement(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8373 | Deoptimizer::RevertStackCheckCode(*unoptimized, |
| 8374 | *check_code, |
| 8375 | *replacement_code); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8376 | |
| 8377 | // Allow OSR only at nesting level zero again. |
| 8378 | unoptimized->set_allow_osr_at_loop_nesting_level(0); |
| 8379 | |
| 8380 | // If the optimization attempt succeeded, return the AST id tagged as a |
| 8381 | // smi. This tells the builtin that we need to translate the unoptimized |
| 8382 | // frame to an optimized one. |
| 8383 | if (succeeded) { |
| 8384 | ASSERT(function->code()->kind() == Code::OPTIMIZED_FUNCTION); |
| 8385 | return Smi::FromInt(ast_id); |
| 8386 | } else { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 8387 | if (function->IsMarkedForLazyRecompilation()) { |
| 8388 | function->ReplaceCode(function->shared()->code()); |
| 8389 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8390 | return Smi::FromInt(-1); |
| 8391 | } |
| 8392 | } |
| 8393 | |
| 8394 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 8395 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CheckIsBootstrapping) { |
| 8396 | RUNTIME_ASSERT(isolate->bootstrapper()->IsActive()); |
| 8397 | return isolate->heap()->undefined_value(); |
| 8398 | } |
| 8399 | |
| 8400 | |
Ben Murdoch | db1b438 | 2012-04-26 19:03:50 +0100 | [diff] [blame] | 8401 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetRootNaN) { |
| 8402 | RUNTIME_ASSERT(isolate->bootstrapper()->IsActive()); |
| 8403 | return isolate->heap()->nan_value(); |
| 8404 | } |
| 8405 | |
| 8406 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8407 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Call) { |
| 8408 | HandleScope scope(isolate); |
| 8409 | ASSERT(args.length() >= 2); |
| 8410 | int argc = args.length() - 2; |
| 8411 | CONVERT_ARG_CHECKED(JSReceiver, fun, argc + 1); |
| 8412 | Object* receiver = args[0]; |
| 8413 | |
| 8414 | // If there are too many arguments, allocate argv via malloc. |
| 8415 | const int argv_small_size = 10; |
| 8416 | Handle<Object> argv_small_buffer[argv_small_size]; |
| 8417 | SmartArrayPointer<Handle<Object> > argv_large_buffer; |
| 8418 | Handle<Object>* argv = argv_small_buffer; |
| 8419 | if (argc > argv_small_size) { |
| 8420 | argv = new Handle<Object>[argc]; |
| 8421 | if (argv == NULL) return isolate->StackOverflow(); |
| 8422 | argv_large_buffer = SmartArrayPointer<Handle<Object> >(argv); |
| 8423 | } |
| 8424 | |
| 8425 | for (int i = 0; i < argc; ++i) { |
| 8426 | MaybeObject* maybe = args[1 + i]; |
| 8427 | Object* object; |
| 8428 | if (!maybe->To<Object>(&object)) return maybe; |
| 8429 | argv[i] = Handle<Object>(object); |
| 8430 | } |
| 8431 | |
| 8432 | bool threw; |
| 8433 | Handle<JSReceiver> hfun(fun); |
| 8434 | Handle<Object> hreceiver(receiver); |
| 8435 | Handle<Object> result = |
| 8436 | Execution::Call(hfun, hreceiver, argc, argv, &threw, true); |
| 8437 | |
| 8438 | if (threw) return Failure::Exception(); |
| 8439 | return *result; |
| 8440 | } |
| 8441 | |
| 8442 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 8443 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Apply) { |
| 8444 | HandleScope scope(isolate); |
| 8445 | ASSERT(args.length() == 5); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8446 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, fun, 0); |
| 8447 | Handle<Object> receiver = args.at<Object>(1); |
| 8448 | CONVERT_ARG_HANDLE_CHECKED(JSObject, arguments, 2); |
| 8449 | CONVERT_SMI_ARG_CHECKED(offset, 3); |
| 8450 | CONVERT_SMI_ARG_CHECKED(argc, 4); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 8451 | ASSERT(offset >= 0); |
| 8452 | ASSERT(argc >= 0); |
| 8453 | |
| 8454 | // If there are too many arguments, allocate argv via malloc. |
| 8455 | const int argv_small_size = 10; |
| 8456 | Handle<Object> argv_small_buffer[argv_small_size]; |
| 8457 | SmartArrayPointer<Handle<Object> > argv_large_buffer; |
| 8458 | Handle<Object>* argv = argv_small_buffer; |
| 8459 | if (argc > argv_small_size) { |
| 8460 | argv = new Handle<Object>[argc]; |
| 8461 | if (argv == NULL) return isolate->StackOverflow(); |
| 8462 | argv_large_buffer = SmartArrayPointer<Handle<Object> >(argv); |
| 8463 | } |
| 8464 | |
| 8465 | for (int i = 0; i < argc; ++i) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8466 | argv[i] = Object::GetElement(arguments, offset + i); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 8467 | } |
| 8468 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8469 | bool threw; |
| 8470 | Handle<Object> result = |
| 8471 | Execution::Call(fun, receiver, argc, argv, &threw, true); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 8472 | |
| 8473 | if (threw) return Failure::Exception(); |
| 8474 | return *result; |
| 8475 | } |
| 8476 | |
| 8477 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8478 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFunctionDelegate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8479 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8480 | ASSERT(args.length() == 1); |
| 8481 | RUNTIME_ASSERT(!args[0]->IsJSFunction()); |
| 8482 | return *Execution::GetFunctionDelegate(args.at<Object>(0)); |
| 8483 | } |
| 8484 | |
| 8485 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8486 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetConstructorDelegate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8487 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8488 | ASSERT(args.length() == 1); |
| 8489 | RUNTIME_ASSERT(!args[0]->IsJSFunction()); |
| 8490 | return *Execution::GetConstructorDelegate(args.at<Object>(0)); |
| 8491 | } |
| 8492 | |
| 8493 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8494 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewFunctionContext) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8495 | NoHandleAllocation ha; |
| 8496 | ASSERT(args.length() == 1); |
| 8497 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8498 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 8499 | int length = function->shared()->scope_info()->ContextLength(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8500 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8501 | { MaybeObject* maybe_result = |
| 8502 | isolate->heap()->AllocateFunctionContext(length, function); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8503 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 8504 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8505 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8506 | isolate->set_context(Context::cast(result)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8507 | |
| 8508 | return result; // non-failure |
| 8509 | } |
| 8510 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8511 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8512 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushWithContext) { |
| 8513 | NoHandleAllocation ha; |
| 8514 | ASSERT(args.length() == 2); |
| 8515 | JSObject* extension_object; |
| 8516 | if (args[0]->IsJSObject()) { |
| 8517 | extension_object = JSObject::cast(args[0]); |
| 8518 | } else { |
| 8519 | // Convert the object to a proper JavaScript object. |
| 8520 | MaybeObject* maybe_js_object = args[0]->ToObject(); |
| 8521 | if (!maybe_js_object->To(&extension_object)) { |
| 8522 | if (Failure::cast(maybe_js_object)->IsInternalError()) { |
| 8523 | HandleScope scope(isolate); |
| 8524 | Handle<Object> handle = args.at<Object>(0); |
| 8525 | Handle<Object> result = |
| 8526 | isolate->factory()->NewTypeError("with_expression", |
| 8527 | HandleVector(&handle, 1)); |
| 8528 | return isolate->Throw(*result); |
| 8529 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8530 | return maybe_js_object; |
| 8531 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8532 | } |
| 8533 | } |
| 8534 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8535 | JSFunction* function; |
| 8536 | if (args[1]->IsSmi()) { |
| 8537 | // A smi sentinel indicates a context nested inside global code rather |
| 8538 | // than some function. There is a canonical empty function that can be |
| 8539 | // gotten from the global context. |
| 8540 | function = isolate->context()->global_context()->closure(); |
| 8541 | } else { |
| 8542 | function = JSFunction::cast(args[1]); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8543 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8544 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8545 | Context* context; |
| 8546 | MaybeObject* maybe_context = |
| 8547 | isolate->heap()->AllocateWithContext(function, |
| 8548 | isolate->context(), |
| 8549 | extension_object); |
| 8550 | if (!maybe_context->To(&context)) return maybe_context; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8551 | isolate->set_context(context); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8552 | return context; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8553 | } |
| 8554 | |
| 8555 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8556 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushCatchContext) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8557 | NoHandleAllocation ha; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8558 | ASSERT(args.length() == 3); |
| 8559 | String* name = String::cast(args[0]); |
| 8560 | Object* thrown_object = args[1]; |
| 8561 | JSFunction* function; |
| 8562 | if (args[2]->IsSmi()) { |
| 8563 | // A smi sentinel indicates a context nested inside global code rather |
| 8564 | // than some function. There is a canonical empty function that can be |
| 8565 | // gotten from the global context. |
| 8566 | function = isolate->context()->global_context()->closure(); |
| 8567 | } else { |
| 8568 | function = JSFunction::cast(args[2]); |
| 8569 | } |
| 8570 | Context* context; |
| 8571 | MaybeObject* maybe_context = |
| 8572 | isolate->heap()->AllocateCatchContext(function, |
| 8573 | isolate->context(), |
| 8574 | name, |
| 8575 | thrown_object); |
| 8576 | if (!maybe_context->To(&context)) return maybe_context; |
| 8577 | isolate->set_context(context); |
| 8578 | return context; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8579 | } |
| 8580 | |
| 8581 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 8582 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushBlockContext) { |
| 8583 | NoHandleAllocation ha; |
| 8584 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8585 | ScopeInfo* scope_info = ScopeInfo::cast(args[0]); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 8586 | JSFunction* function; |
| 8587 | if (args[1]->IsSmi()) { |
| 8588 | // A smi sentinel indicates a context nested inside global code rather |
| 8589 | // than some function. There is a canonical empty function that can be |
| 8590 | // gotten from the global context. |
| 8591 | function = isolate->context()->global_context()->closure(); |
| 8592 | } else { |
| 8593 | function = JSFunction::cast(args[1]); |
| 8594 | } |
| 8595 | Context* context; |
| 8596 | MaybeObject* maybe_context = |
| 8597 | isolate->heap()->AllocateBlockContext(function, |
| 8598 | isolate->context(), |
| 8599 | scope_info); |
| 8600 | if (!maybe_context->To(&context)) return maybe_context; |
| 8601 | isolate->set_context(context); |
| 8602 | return context; |
| 8603 | } |
| 8604 | |
| 8605 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8606 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeleteContextSlot) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8607 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8608 | ASSERT(args.length() == 2); |
| 8609 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8610 | CONVERT_ARG_HANDLE_CHECKED(Context, context, 0); |
| 8611 | CONVERT_ARG_HANDLE_CHECKED(String, name, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8612 | |
| 8613 | int index; |
| 8614 | PropertyAttributes attributes; |
| 8615 | ContextLookupFlags flags = FOLLOW_CHAINS; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 8616 | BindingFlags binding_flags; |
| 8617 | Handle<Object> holder = context->Lookup(name, |
| 8618 | flags, |
| 8619 | &index, |
| 8620 | &attributes, |
| 8621 | &binding_flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8622 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8623 | // If the slot was not found the result is true. |
| 8624 | if (holder.is_null()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8625 | return isolate->heap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8626 | } |
| 8627 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8628 | // If the slot was found in a context, it should be DONT_DELETE. |
| 8629 | if (holder->IsContext()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8630 | return isolate->heap()->false_value(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8631 | } |
| 8632 | |
| 8633 | // The slot was found in a JSObject, either a context extension object, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8634 | // the global object, or the subject of a with. Try to delete it |
| 8635 | // (respecting DONT_DELETE). |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8636 | Handle<JSObject> object = Handle<JSObject>::cast(holder); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8637 | return object->DeleteProperty(*name, JSReceiver::NORMAL_DELETION); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8638 | } |
| 8639 | |
| 8640 | |
| 8641 | // A mechanism to return a pair of Object pointers in registers (if possible). |
| 8642 | // How this is achieved is calling convention-dependent. |
| 8643 | // All currently supported x86 compiles uses calling conventions that are cdecl |
| 8644 | // variants where a 64-bit value is returned in two 32-bit registers |
| 8645 | // (edx:eax on ia32, r1:r0 on ARM). |
| 8646 | // In AMD-64 calling convention a struct of two pointers is returned in rdx:rax. |
| 8647 | // In Win64 calling convention, a struct of two pointers is returned in memory, |
| 8648 | // allocated by the caller, and passed as a pointer in a hidden first parameter. |
| 8649 | #ifdef V8_HOST_ARCH_64_BIT |
| 8650 | struct ObjectPair { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8651 | MaybeObject* x; |
| 8652 | MaybeObject* y; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8653 | }; |
| 8654 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8655 | static inline ObjectPair MakePair(MaybeObject* x, MaybeObject* y) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8656 | ObjectPair result = {x, y}; |
| 8657 | // Pointers x and y returned in rax and rdx, in AMD-x64-abi. |
| 8658 | // In Win64 they are assigned to a hidden first argument. |
| 8659 | return result; |
| 8660 | } |
| 8661 | #else |
| 8662 | typedef uint64_t ObjectPair; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8663 | static inline ObjectPair MakePair(MaybeObject* x, MaybeObject* y) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8664 | return reinterpret_cast<uint32_t>(x) | |
| 8665 | (reinterpret_cast<ObjectPair>(y) << 32); |
| 8666 | } |
| 8667 | #endif |
| 8668 | |
| 8669 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8670 | static inline MaybeObject* Unhole(Heap* heap, |
| 8671 | MaybeObject* x, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8672 | PropertyAttributes attributes) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8673 | ASSERT(!x->IsTheHole() || (attributes & READ_ONLY) != 0); |
| 8674 | USE(attributes); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8675 | return x->IsTheHole() ? heap->undefined_value() : x; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8676 | } |
| 8677 | |
| 8678 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 8679 | static Object* ComputeReceiverForNonGlobal(Isolate* isolate, |
| 8680 | JSObject* holder) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8681 | ASSERT(!holder->IsGlobalObject()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8682 | Context* top = isolate->context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8683 | // Get the context extension function. |
| 8684 | JSFunction* context_extension_function = |
| 8685 | top->global_context()->context_extension_function(); |
| 8686 | // If the holder isn't a context extension object, we just return it |
| 8687 | // as the receiver. This allows arguments objects to be used as |
| 8688 | // receivers, but only if they are put in the context scope chain |
| 8689 | // explicitly via a with-statement. |
| 8690 | Object* constructor = holder->map()->constructor(); |
| 8691 | if (constructor != context_extension_function) return holder; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 8692 | // Fall back to using the global object as the implicit receiver if |
| 8693 | // the property turns out to be a local variable allocated in a |
| 8694 | // context extension object - introduced via eval. Implicit global |
| 8695 | // receivers are indicated with the hole value. |
| 8696 | return isolate->heap()->the_hole_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8697 | } |
| 8698 | |
| 8699 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8700 | static ObjectPair LoadContextSlotHelper(Arguments args, |
| 8701 | Isolate* isolate, |
| 8702 | bool throw_error) { |
| 8703 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8704 | ASSERT_EQ(2, args.length()); |
| 8705 | |
| 8706 | if (!args[0]->IsContext() || !args[1]->IsString()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8707 | return MakePair(isolate->ThrowIllegalOperation(), NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8708 | } |
| 8709 | Handle<Context> context = args.at<Context>(0); |
| 8710 | Handle<String> name = args.at<String>(1); |
| 8711 | |
| 8712 | int index; |
| 8713 | PropertyAttributes attributes; |
| 8714 | ContextLookupFlags flags = FOLLOW_CHAINS; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 8715 | BindingFlags binding_flags; |
| 8716 | Handle<Object> holder = context->Lookup(name, |
| 8717 | flags, |
| 8718 | &index, |
| 8719 | &attributes, |
| 8720 | &binding_flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8721 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8722 | // If the index is non-negative, the slot has been found in a context. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8723 | if (index >= 0) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8724 | ASSERT(holder->IsContext()); |
| 8725 | // If the "property" we were looking for is a local variable, the |
| 8726 | // receiver is the global object; see ECMA-262, 3rd., 10.1.6 and 10.2.3. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 8727 | // |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8728 | // Use the hole as the receiver to signal that the receiver is implicit |
| 8729 | // and that the global receiver should be used (as distinguished from an |
| 8730 | // explicit receiver that happens to be a global object). |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 8731 | Handle<Object> receiver = isolate->factory()->the_hole_value(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8732 | Object* value = Context::cast(*holder)->get(index); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 8733 | // Check for uninitialized bindings. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8734 | switch (binding_flags) { |
| 8735 | case MUTABLE_CHECK_INITIALIZED: |
| 8736 | case IMMUTABLE_CHECK_INITIALIZED_HARMONY: |
| 8737 | if (value->IsTheHole()) { |
| 8738 | Handle<Object> reference_error = |
| 8739 | isolate->factory()->NewReferenceError("not_defined", |
| 8740 | HandleVector(&name, 1)); |
| 8741 | return MakePair(isolate->Throw(*reference_error), NULL); |
| 8742 | } |
| 8743 | // FALLTHROUGH |
| 8744 | case MUTABLE_IS_INITIALIZED: |
| 8745 | case IMMUTABLE_IS_INITIALIZED: |
| 8746 | case IMMUTABLE_IS_INITIALIZED_HARMONY: |
| 8747 | ASSERT(!value->IsTheHole()); |
| 8748 | return MakePair(value, *receiver); |
| 8749 | case IMMUTABLE_CHECK_INITIALIZED: |
| 8750 | return MakePair(Unhole(isolate->heap(), value, attributes), *receiver); |
| 8751 | case MISSING_BINDING: |
| 8752 | UNREACHABLE(); |
| 8753 | return MakePair(NULL, NULL); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 8754 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8755 | } |
| 8756 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8757 | // Otherwise, if the slot was found the holder is a context extension |
| 8758 | // object, subject of a with, or a global object. We read the named |
| 8759 | // property from it. |
| 8760 | if (!holder.is_null()) { |
| 8761 | Handle<JSObject> object = Handle<JSObject>::cast(holder); |
| 8762 | ASSERT(object->HasProperty(*name)); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 8763 | // GetProperty below can cause GC. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8764 | Handle<Object> receiver_handle(object->IsGlobalObject() |
| 8765 | ? GlobalObject::cast(*object)->global_receiver() |
| 8766 | : ComputeReceiverForNonGlobal(isolate, *object)); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 8767 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8768 | // No need to unhole the value here. This is taken care of by the |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8769 | // GetProperty function. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8770 | MaybeObject* value = object->GetProperty(*name); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 8771 | return MakePair(value, *receiver_handle); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8772 | } |
| 8773 | |
| 8774 | if (throw_error) { |
| 8775 | // The property doesn't exist - throw exception. |
| 8776 | Handle<Object> reference_error = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8777 | isolate->factory()->NewReferenceError("not_defined", |
| 8778 | HandleVector(&name, 1)); |
| 8779 | return MakePair(isolate->Throw(*reference_error), NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8780 | } else { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 8781 | // The property doesn't exist - return undefined. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8782 | return MakePair(isolate->heap()->undefined_value(), |
| 8783 | isolate->heap()->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8784 | } |
| 8785 | } |
| 8786 | |
| 8787 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8788 | RUNTIME_FUNCTION(ObjectPair, Runtime_LoadContextSlot) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8789 | return LoadContextSlotHelper(args, isolate, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8790 | } |
| 8791 | |
| 8792 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8793 | RUNTIME_FUNCTION(ObjectPair, Runtime_LoadContextSlotNoReferenceError) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8794 | return LoadContextSlotHelper(args, isolate, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8795 | } |
| 8796 | |
| 8797 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8798 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StoreContextSlot) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8799 | HandleScope scope(isolate); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 8800 | ASSERT(args.length() == 4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8801 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8802 | Handle<Object> value(args[0], isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8803 | CONVERT_ARG_HANDLE_CHECKED(Context, context, 1); |
| 8804 | CONVERT_ARG_HANDLE_CHECKED(String, name, 2); |
| 8805 | CONVERT_LANGUAGE_MODE_ARG(language_mode, 3); |
| 8806 | StrictModeFlag strict_mode = (language_mode == CLASSIC_MODE) |
| 8807 | ? kNonStrictMode : kStrictMode; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8808 | |
| 8809 | int index; |
| 8810 | PropertyAttributes attributes; |
| 8811 | ContextLookupFlags flags = FOLLOW_CHAINS; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 8812 | BindingFlags binding_flags; |
| 8813 | Handle<Object> holder = context->Lookup(name, |
| 8814 | flags, |
| 8815 | &index, |
| 8816 | &attributes, |
| 8817 | &binding_flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8818 | |
| 8819 | if (index >= 0) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8820 | // The property was found in a context slot. |
| 8821 | Handle<Context> context = Handle<Context>::cast(holder); |
| 8822 | if (binding_flags == MUTABLE_CHECK_INITIALIZED && |
| 8823 | context->get(index)->IsTheHole()) { |
| 8824 | Handle<Object> error = |
| 8825 | isolate->factory()->NewReferenceError("not_defined", |
| 8826 | HandleVector(&name, 1)); |
| 8827 | return isolate->Throw(*error); |
| 8828 | } |
| 8829 | // Ignore if read_only variable. |
| 8830 | if ((attributes & READ_ONLY) == 0) { |
| 8831 | // Context is a fixed array and set cannot fail. |
| 8832 | context->set(index, *value); |
| 8833 | } else if (strict_mode == kStrictMode) { |
| 8834 | // Setting read only property in strict mode. |
| 8835 | Handle<Object> error = |
| 8836 | isolate->factory()->NewTypeError("strict_cannot_assign", |
| 8837 | HandleVector(&name, 1)); |
| 8838 | return isolate->Throw(*error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8839 | } |
| 8840 | return *value; |
| 8841 | } |
| 8842 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8843 | // Slow case: The property is not in a context slot. It is either in a |
| 8844 | // context extension object, a property of the subject of a with, or a |
| 8845 | // property of the global object. |
| 8846 | Handle<JSObject> object; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8847 | |
| 8848 | if (!holder.is_null()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8849 | // The property exists on the holder. |
| 8850 | object = Handle<JSObject>::cast(holder); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8851 | } else { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8852 | // The property was not found. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8853 | ASSERT(attributes == ABSENT); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8854 | |
| 8855 | if (strict_mode == kStrictMode) { |
| 8856 | // Throw in strict mode (assignment to undefined variable). |
| 8857 | Handle<Object> error = |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8858 | isolate->factory()->NewReferenceError( |
| 8859 | "not_defined", HandleVector(&name, 1)); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8860 | return isolate->Throw(*error); |
| 8861 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8862 | // In non-strict mode, the property is added to the global object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8863 | attributes = NONE; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8864 | object = Handle<JSObject>(isolate->context()->global()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8865 | } |
| 8866 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8867 | // Set the property if it's not read only or doesn't yet exist. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8868 | if ((attributes & READ_ONLY) == 0 || |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8869 | (object->GetLocalPropertyAttribute(*name) == ABSENT)) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 8870 | RETURN_IF_EMPTY_HANDLE( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8871 | isolate, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8872 | JSReceiver::SetProperty(object, name, value, NONE, strict_mode)); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 8873 | } else if (strict_mode == kStrictMode && (attributes & READ_ONLY) != 0) { |
| 8874 | // Setting read only property in strict mode. |
| 8875 | Handle<Object> error = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8876 | isolate->factory()->NewTypeError( |
| 8877 | "strict_cannot_assign", HandleVector(&name, 1)); |
| 8878 | return isolate->Throw(*error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8879 | } |
| 8880 | return *value; |
| 8881 | } |
| 8882 | |
| 8883 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8884 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Throw) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8885 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8886 | ASSERT(args.length() == 1); |
| 8887 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8888 | return isolate->Throw(args[0]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8889 | } |
| 8890 | |
| 8891 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8892 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ReThrow) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8893 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8894 | ASSERT(args.length() == 1); |
| 8895 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8896 | return isolate->ReThrow(args[0]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8897 | } |
| 8898 | |
| 8899 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8900 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PromoteScheduledException) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8901 | ASSERT_EQ(0, args.length()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8902 | return isolate->PromoteScheduledException(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8903 | } |
| 8904 | |
| 8905 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8906 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ThrowReferenceError) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8907 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8908 | ASSERT(args.length() == 1); |
| 8909 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8910 | Handle<Object> name(args[0], isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8911 | Handle<Object> reference_error = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8912 | isolate->factory()->NewReferenceError("not_defined", |
| 8913 | HandleVector(&name, 1)); |
| 8914 | return isolate->Throw(*reference_error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8915 | } |
| 8916 | |
| 8917 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8918 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StackGuard) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 8919 | ASSERT(args.length() == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8920 | |
| 8921 | // First check if this is a real stack overflow. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8922 | if (isolate->stack_guard()->IsStackOverflow()) { |
| 8923 | NoHandleAllocation na; |
| 8924 | return isolate->StackOverflow(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8925 | } |
| 8926 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8927 | return Execution::HandleStackGuardInterrupt(isolate); |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 8928 | } |
| 8929 | |
| 8930 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8931 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Interrupt) { |
| 8932 | ASSERT(args.length() == 0); |
| 8933 | return Execution::HandleStackGuardInterrupt(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8934 | } |
| 8935 | |
| 8936 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8937 | static int StackSize() { |
| 8938 | int n = 0; |
| 8939 | for (JavaScriptFrameIterator it; !it.done(); it.Advance()) n++; |
| 8940 | return n; |
| 8941 | } |
| 8942 | |
| 8943 | |
| 8944 | static void PrintTransition(Object* result) { |
| 8945 | // indentation |
| 8946 | { const int nmax = 80; |
| 8947 | int n = StackSize(); |
| 8948 | if (n <= nmax) |
| 8949 | PrintF("%4d:%*s", n, n, ""); |
| 8950 | else |
| 8951 | PrintF("%4d:%*s", n, nmax, "..."); |
| 8952 | } |
| 8953 | |
| 8954 | if (result == NULL) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8955 | JavaScriptFrame::PrintTop(stdout, true, false); |
| 8956 | PrintF(" {\n"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8957 | } else { |
| 8958 | // function result |
| 8959 | PrintF("} -> "); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 8960 | result->ShortPrint(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8961 | PrintF("\n"); |
| 8962 | } |
| 8963 | } |
| 8964 | |
| 8965 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8966 | RUNTIME_FUNCTION(MaybeObject*, Runtime_TraceEnter) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8967 | ASSERT(args.length() == 0); |
| 8968 | NoHandleAllocation ha; |
| 8969 | PrintTransition(NULL); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8970 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8971 | } |
| 8972 | |
| 8973 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8974 | RUNTIME_FUNCTION(MaybeObject*, Runtime_TraceExit) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8975 | NoHandleAllocation ha; |
| 8976 | PrintTransition(args[0]); |
| 8977 | return args[0]; // return TOS |
| 8978 | } |
| 8979 | |
| 8980 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8981 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPrint) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8982 | NoHandleAllocation ha; |
| 8983 | ASSERT(args.length() == 1); |
| 8984 | |
| 8985 | #ifdef DEBUG |
| 8986 | if (args[0]->IsString()) { |
| 8987 | // If we have a string, assume it's a code "marker" |
| 8988 | // and print some interesting cpu debugging info. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8989 | JavaScriptFrameIterator it(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8990 | JavaScriptFrame* frame = it.frame(); |
| 8991 | PrintF("fp = %p, sp = %p, caller_sp = %p: ", |
| 8992 | frame->fp(), frame->sp(), frame->caller_sp()); |
| 8993 | } else { |
| 8994 | PrintF("DebugPrint: "); |
| 8995 | } |
| 8996 | args[0]->Print(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8997 | if (args[0]->IsHeapObject()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8998 | PrintF("\n"); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8999 | HeapObject::cast(args[0])->map()->Print(); |
| 9000 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9001 | #else |
| 9002 | // ShortPrint is available in release mode. Print is not. |
| 9003 | args[0]->ShortPrint(); |
| 9004 | #endif |
| 9005 | PrintF("\n"); |
| 9006 | Flush(); |
| 9007 | |
| 9008 | return args[0]; // return TOS |
| 9009 | } |
| 9010 | |
| 9011 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9012 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugTrace) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9013 | ASSERT(args.length() == 0); |
| 9014 | NoHandleAllocation ha; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9015 | isolate->PrintStack(); |
| 9016 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9017 | } |
| 9018 | |
| 9019 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9020 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateCurrentTime) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9021 | NoHandleAllocation ha; |
| 9022 | ASSERT(args.length() == 0); |
| 9023 | |
| 9024 | // According to ECMA-262, section 15.9.1, page 117, the precision of |
| 9025 | // the number in a Date object representing a particular instant in |
| 9026 | // time is milliseconds. Therefore, we floor the result of getting |
| 9027 | // the OS time. |
| 9028 | double millis = floor(OS::TimeCurrentMillis()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9029 | return isolate->heap()->NumberFromDouble(millis); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9030 | } |
| 9031 | |
| 9032 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9033 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateParseString) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9034 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9035 | ASSERT(args.length() == 2); |
| 9036 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9037 | CONVERT_ARG_HANDLE_CHECKED(String, str, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9038 | FlattenString(str); |
| 9039 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9040 | CONVERT_ARG_HANDLE_CHECKED(JSArray, output, 1); |
| 9041 | |
| 9042 | MaybeObject* maybe_result_array = |
| 9043 | output->EnsureCanContainHeapObjectElements(); |
| 9044 | if (maybe_result_array->IsFailure()) return maybe_result_array; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9045 | RUNTIME_ASSERT(output->HasFastElements()); |
| 9046 | |
| 9047 | AssertNoAllocation no_allocation; |
| 9048 | |
| 9049 | FixedArray* output_array = FixedArray::cast(output->elements()); |
| 9050 | RUNTIME_ASSERT(output_array->length() >= DateParser::OUTPUT_SIZE); |
| 9051 | bool result; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9052 | String::FlatContent str_content = str->GetFlatContent(); |
| 9053 | if (str_content.IsAscii()) { |
| 9054 | result = DateParser::Parse(str_content.ToAsciiVector(), |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9055 | output_array, |
| 9056 | isolate->unicode_cache()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9057 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9058 | ASSERT(str_content.IsTwoByte()); |
| 9059 | result = DateParser::Parse(str_content.ToUC16Vector(), |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9060 | output_array, |
| 9061 | isolate->unicode_cache()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9062 | } |
| 9063 | |
| 9064 | if (result) { |
| 9065 | return *output; |
| 9066 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9067 | return isolate->heap()->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9068 | } |
| 9069 | } |
| 9070 | |
| 9071 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9072 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateLocalTimezone) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9073 | NoHandleAllocation ha; |
| 9074 | ASSERT(args.length() == 1); |
| 9075 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 9076 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9077 | int64_t time = isolate->date_cache()->EquivalentTime(static_cast<int64_t>(x)); |
| 9078 | const char* zone = OS::LocalTimezone(static_cast<double>(time)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9079 | return isolate->heap()->AllocateStringFromUtf8(CStrVector(zone)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9080 | } |
| 9081 | |
| 9082 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9083 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateToUTC) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9084 | NoHandleAllocation ha; |
| 9085 | ASSERT(args.length() == 1); |
| 9086 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 9087 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9088 | int64_t time = isolate->date_cache()->ToUTC(static_cast<int64_t>(x)); |
| 9089 | |
| 9090 | return isolate->heap()->NumberFromDouble(static_cast<double>(time)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9091 | } |
| 9092 | |
| 9093 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9094 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GlobalReceiver) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9095 | ASSERT(args.length() == 1); |
| 9096 | Object* global = args[0]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9097 | if (!global->IsJSGlobalObject()) return isolate->heap()->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9098 | return JSGlobalObject::cast(global)->global_receiver(); |
| 9099 | } |
| 9100 | |
| 9101 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9102 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ParseJson) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9103 | HandleScope scope(isolate); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 9104 | ASSERT_EQ(1, args.length()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9105 | CONVERT_ARG_HANDLE_CHECKED(String, source, 0); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 9106 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 9107 | source = Handle<String>(source->TryFlattenGetString()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9108 | // Optimized fast case where we only have ASCII characters. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 9109 | Handle<Object> result; |
| 9110 | if (source->IsSeqAsciiString()) { |
| 9111 | result = JsonParser<true>::Parse(source); |
| 9112 | } else { |
| 9113 | result = JsonParser<false>::Parse(source); |
| 9114 | } |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 9115 | if (result.is_null()) { |
| 9116 | // Syntax error or stack overflow in scanner. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9117 | ASSERT(isolate->has_pending_exception()); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 9118 | return Failure::Exception(); |
| 9119 | } |
| 9120 | return *result; |
| 9121 | } |
| 9122 | |
| 9123 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9124 | bool CodeGenerationFromStringsAllowed(Isolate* isolate, |
| 9125 | Handle<Context> context) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9126 | ASSERT(context->allow_code_gen_from_strings()->IsFalse()); |
| 9127 | // Check with callback if set. |
| 9128 | AllowCodeGenerationFromStringsCallback callback = |
| 9129 | isolate->allow_code_gen_callback(); |
| 9130 | if (callback == NULL) { |
| 9131 | // No callback set and code generation disallowed. |
| 9132 | return false; |
| 9133 | } else { |
| 9134 | // Callback set. Let it decide if code generation is allowed. |
| 9135 | VMState state(isolate, EXTERNAL); |
| 9136 | return callback(v8::Utils::ToLocal(context)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9137 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9138 | } |
| 9139 | |
| 9140 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9141 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CompileString) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9142 | HandleScope scope(isolate); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 9143 | ASSERT_EQ(1, args.length()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9144 | CONVERT_ARG_HANDLE_CHECKED(String, source, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9145 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9146 | // Extract global context. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9147 | Handle<Context> context(isolate->context()->global_context()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9148 | |
| 9149 | // Check if global context allows code generation from |
| 9150 | // strings. Throw an exception if it doesn't. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9151 | if (context->allow_code_gen_from_strings()->IsFalse() && |
| 9152 | !CodeGenerationFromStringsAllowed(isolate, context)) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9153 | return isolate->Throw(*isolate->factory()->NewError( |
| 9154 | "code_gen_from_strings", HandleVector<Object>(NULL, 0))); |
| 9155 | } |
| 9156 | |
| 9157 | // Compile source string in the global context. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9158 | Handle<SharedFunctionInfo> shared = Compiler::CompileEval( |
| 9159 | source, context, true, CLASSIC_MODE, RelocInfo::kNoPosition); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9160 | if (shared.is_null()) return Failure::Exception(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9161 | Handle<JSFunction> fun = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9162 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 9163 | context, |
| 9164 | NOT_TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9165 | return *fun; |
| 9166 | } |
| 9167 | |
| 9168 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9169 | static ObjectPair CompileGlobalEval(Isolate* isolate, |
| 9170 | Handle<String> source, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 9171 | Handle<Object> receiver, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9172 | LanguageMode language_mode, |
| 9173 | int scope_position) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9174 | Handle<Context> context = Handle<Context>(isolate->context()); |
| 9175 | Handle<Context> global_context = Handle<Context>(context->global_context()); |
| 9176 | |
| 9177 | // Check if global context allows code generation from |
| 9178 | // strings. Throw an exception if it doesn't. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9179 | if (global_context->allow_code_gen_from_strings()->IsFalse() && |
| 9180 | !CodeGenerationFromStringsAllowed(isolate, global_context)) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9181 | isolate->Throw(*isolate->factory()->NewError( |
| 9182 | "code_gen_from_strings", HandleVector<Object>(NULL, 0))); |
| 9183 | return MakePair(Failure::Exception(), NULL); |
| 9184 | } |
| 9185 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 9186 | // Deal with a normal eval call with a string argument. Compile it |
| 9187 | // and return the compiled function bound in the local context. |
| 9188 | Handle<SharedFunctionInfo> shared = Compiler::CompileEval( |
| 9189 | source, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9190 | Handle<Context>(isolate->context()), |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9191 | context->IsGlobalContext(), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9192 | language_mode, |
| 9193 | scope_position); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 9194 | if (shared.is_null()) return MakePair(Failure::Exception(), NULL); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9195 | Handle<JSFunction> compiled = |
| 9196 | isolate->factory()->NewFunctionFromSharedFunctionInfo( |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9197 | shared, context, NOT_TENURED); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 9198 | return MakePair(*compiled, *receiver); |
| 9199 | } |
| 9200 | |
| 9201 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9202 | RUNTIME_FUNCTION(ObjectPair, Runtime_ResolvePossiblyDirectEval) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9203 | ASSERT(args.length() == 5); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9204 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9205 | HandleScope scope(isolate); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9206 | Handle<Object> callee = args.at<Object>(0); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 9207 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9208 | // If "eval" didn't refer to the original GlobalEval, it's not a |
| 9209 | // direct call to eval. |
| 9210 | // (And even if it is, but the first argument isn't a string, just let |
| 9211 | // execution default to an indirect call to eval, which will also return |
| 9212 | // the first argument without doing anything). |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9213 | if (*callee != isolate->global_context()->global_eval_fun() || |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 9214 | !args[1]->IsString()) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9215 | return MakePair(*callee, isolate->heap()->the_hole_value()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 9216 | } |
| 9217 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9218 | CONVERT_LANGUAGE_MODE_ARG(language_mode, 3); |
| 9219 | ASSERT(args[4]->IsSmi()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9220 | return CompileGlobalEval(isolate, |
| 9221 | args.at<String>(1), |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 9222 | args.at<Object>(2), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9223 | language_mode, |
| 9224 | args.smi_at(4)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9225 | } |
| 9226 | |
| 9227 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9228 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetNewFunctionAttributes) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9229 | // This utility adjusts the property attributes for newly created Function |
| 9230 | // object ("new Function(...)") by changing the map. |
| 9231 | // All it does is changing the prototype property to enumerable |
| 9232 | // as specified in ECMA262, 15.3.5.2. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9233 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9234 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9235 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, func, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9236 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9237 | Handle<Map> map = func->shared()->is_classic_mode() |
| 9238 | ? isolate->function_instance_map() |
| 9239 | : isolate->strict_mode_function_instance_map(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9240 | |
| 9241 | ASSERT(func->map()->instance_type() == map->instance_type()); |
| 9242 | ASSERT(func->map()->instance_size() == map->instance_size()); |
| 9243 | func->set_map(*map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9244 | return *func; |
| 9245 | } |
| 9246 | |
| 9247 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9248 | RUNTIME_FUNCTION(MaybeObject*, Runtime_AllocateInNewSpace) { |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 9249 | // Allocate a block of memory in NewSpace (filled with a filler). |
| 9250 | // Use as fallback for allocation in generated code when NewSpace |
| 9251 | // is full. |
| 9252 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9253 | CONVERT_ARG_HANDLE_CHECKED(Smi, size_smi, 0); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 9254 | int size = size_smi->value(); |
| 9255 | RUNTIME_ASSERT(IsAligned(size, kPointerSize)); |
| 9256 | RUNTIME_ASSERT(size > 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9257 | Heap* heap = isolate->heap(); |
| 9258 | const int kMinFreeNewSpaceAfterGC = heap->InitialSemiSpaceSize() * 3/4; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 9259 | RUNTIME_ASSERT(size <= kMinFreeNewSpaceAfterGC); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9260 | Object* allocation; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9261 | { MaybeObject* maybe_allocation = heap->new_space()->AllocateRaw(size); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9262 | if (maybe_allocation->ToObject(&allocation)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9263 | heap->CreateFillerObjectAt(HeapObject::cast(allocation)->address(), size); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9264 | } |
| 9265 | return maybe_allocation; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 9266 | } |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 9267 | } |
| 9268 | |
| 9269 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 9270 | // Push an object unto an array of objects if it is not already in the |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9271 | // array. Returns true if the element was pushed on the stack and |
| 9272 | // false otherwise. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9273 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushIfAbsent) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9274 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9275 | CONVERT_ARG_CHECKED(JSArray, array, 0); |
| 9276 | CONVERT_ARG_CHECKED(JSObject, element, 1); |
| 9277 | RUNTIME_ASSERT(array->HasFastElements() || array->HasFastSmiOnlyElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9278 | int length = Smi::cast(array->length())->value(); |
| 9279 | FixedArray* elements = FixedArray::cast(array->elements()); |
| 9280 | for (int i = 0; i < length; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9281 | if (elements->get(i) == element) return isolate->heap()->false_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9282 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9283 | Object* obj; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9284 | // Strict not needed. Used for cycle detection in Array join implementation. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 9285 | { MaybeObject* maybe_obj = |
| 9286 | array->SetFastElement(length, element, kNonStrictMode, true); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9287 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9288 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9289 | return isolate->heap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9290 | } |
| 9291 | |
| 9292 | |
| 9293 | /** |
| 9294 | * A simple visitor visits every element of Array's. |
| 9295 | * The backend storage can be a fixed array for fast elements case, |
| 9296 | * or a dictionary for sparse array. Since Dictionary is a subtype |
| 9297 | * of FixedArray, the class can be used by both fast and slow cases. |
| 9298 | * The second parameter of the constructor, fast_elements, specifies |
| 9299 | * whether the storage is a FixedArray or Dictionary. |
| 9300 | * |
| 9301 | * An index limit is used to deal with the situation that a result array |
| 9302 | * length overflows 32-bit non-negative integer. |
| 9303 | */ |
| 9304 | class ArrayConcatVisitor { |
| 9305 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9306 | ArrayConcatVisitor(Isolate* isolate, |
| 9307 | Handle<FixedArray> storage, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9308 | bool fast_elements) : |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9309 | isolate_(isolate), |
| 9310 | storage_(Handle<FixedArray>::cast( |
| 9311 | isolate->global_handles()->Create(*storage))), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9312 | index_offset_(0u), |
| 9313 | fast_elements_(fast_elements) { } |
| 9314 | |
| 9315 | ~ArrayConcatVisitor() { |
| 9316 | clear_storage(); |
| 9317 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9318 | |
| 9319 | void visit(uint32_t i, Handle<Object> elm) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9320 | if (i >= JSObject::kMaxElementCount - index_offset_) return; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 9321 | uint32_t index = index_offset_ + i; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9322 | |
| 9323 | if (fast_elements_) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9324 | if (index < static_cast<uint32_t>(storage_->length())) { |
| 9325 | storage_->set(index, *elm); |
| 9326 | return; |
| 9327 | } |
| 9328 | // Our initial estimate of length was foiled, possibly by |
| 9329 | // getters on the arrays increasing the length of later arrays |
| 9330 | // during iteration. |
| 9331 | // This shouldn't happen in anything but pathological cases. |
| 9332 | SetDictionaryMode(index); |
| 9333 | // Fall-through to dictionary mode. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9334 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9335 | ASSERT(!fast_elements_); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 9336 | Handle<SeededNumberDictionary> dict( |
| 9337 | SeededNumberDictionary::cast(*storage_)); |
| 9338 | Handle<SeededNumberDictionary> result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9339 | isolate_->factory()->DictionaryAtNumberPut(dict, index, elm); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9340 | if (!result.is_identical_to(dict)) { |
| 9341 | // Dictionary needed to grow. |
| 9342 | clear_storage(); |
| 9343 | set_storage(*result); |
| 9344 | } |
| 9345 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9346 | |
| 9347 | void increase_index_offset(uint32_t delta) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9348 | if (JSObject::kMaxElementCount - index_offset_ < delta) { |
| 9349 | index_offset_ = JSObject::kMaxElementCount; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 9350 | } else { |
| 9351 | index_offset_ += delta; |
| 9352 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9353 | } |
| 9354 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9355 | Handle<JSArray> ToArray() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9356 | Handle<JSArray> array = isolate_->factory()->NewJSArray(0); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9357 | Handle<Object> length = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9358 | isolate_->factory()->NewNumber(static_cast<double>(index_offset_)); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9359 | Handle<Map> map; |
| 9360 | if (fast_elements_) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9361 | map = isolate_->factory()->GetElementsTransitionMap(array, |
| 9362 | FAST_ELEMENTS); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9363 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9364 | map = isolate_->factory()->GetElementsTransitionMap(array, |
| 9365 | DICTIONARY_ELEMENTS); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9366 | } |
| 9367 | array->set_map(*map); |
| 9368 | array->set_length(*length); |
| 9369 | array->set_elements(*storage_); |
| 9370 | return array; |
| 9371 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 9372 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9373 | private: |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9374 | // Convert storage to dictionary mode. |
| 9375 | void SetDictionaryMode(uint32_t index) { |
| 9376 | ASSERT(fast_elements_); |
| 9377 | Handle<FixedArray> current_storage(*storage_); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 9378 | Handle<SeededNumberDictionary> slow_storage( |
| 9379 | isolate_->factory()->NewSeededNumberDictionary( |
| 9380 | current_storage->length())); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9381 | uint32_t current_length = static_cast<uint32_t>(current_storage->length()); |
| 9382 | for (uint32_t i = 0; i < current_length; i++) { |
| 9383 | HandleScope loop_scope; |
| 9384 | Handle<Object> element(current_storage->get(i)); |
| 9385 | if (!element->IsTheHole()) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 9386 | Handle<SeededNumberDictionary> new_storage = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9387 | isolate_->factory()->DictionaryAtNumberPut(slow_storage, i, element); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9388 | if (!new_storage.is_identical_to(slow_storage)) { |
| 9389 | slow_storage = loop_scope.CloseAndEscape(new_storage); |
| 9390 | } |
| 9391 | } |
| 9392 | } |
| 9393 | clear_storage(); |
| 9394 | set_storage(*slow_storage); |
| 9395 | fast_elements_ = false; |
| 9396 | } |
| 9397 | |
| 9398 | inline void clear_storage() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9399 | isolate_->global_handles()->Destroy( |
| 9400 | Handle<Object>::cast(storage_).location()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9401 | } |
| 9402 | |
| 9403 | inline void set_storage(FixedArray* storage) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9404 | storage_ = Handle<FixedArray>::cast( |
| 9405 | isolate_->global_handles()->Create(storage)); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9406 | } |
| 9407 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9408 | Isolate* isolate_; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9409 | Handle<FixedArray> storage_; // Always a global handle. |
| 9410 | // Index after last seen index. Always less than or equal to |
| 9411 | // JSObject::kMaxElementCount. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9412 | uint32_t index_offset_; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9413 | bool fast_elements_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9414 | }; |
| 9415 | |
| 9416 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9417 | static uint32_t EstimateElementCount(Handle<JSArray> array) { |
| 9418 | uint32_t length = static_cast<uint32_t>(array->length()->Number()); |
| 9419 | int element_count = 0; |
| 9420 | switch (array->GetElementsKind()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9421 | case FAST_SMI_ONLY_ELEMENTS: |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9422 | case FAST_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9423 | // Fast elements can't have lengths that are not representable by |
| 9424 | // a 32-bit signed integer. |
| 9425 | ASSERT(static_cast<int32_t>(FixedArray::kMaxLength) >= 0); |
| 9426 | int fast_length = static_cast<int>(length); |
| 9427 | Handle<FixedArray> elements(FixedArray::cast(array->elements())); |
| 9428 | for (int i = 0; i < fast_length; i++) { |
| 9429 | if (!elements->get(i)->IsTheHole()) element_count++; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9430 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9431 | break; |
| 9432 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9433 | case FAST_DOUBLE_ELEMENTS: |
| 9434 | // TODO(1810): Decide if it's worthwhile to implement this. |
| 9435 | UNREACHABLE(); |
| 9436 | break; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9437 | case DICTIONARY_ELEMENTS: { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 9438 | Handle<SeededNumberDictionary> dictionary( |
| 9439 | SeededNumberDictionary::cast(array->elements())); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9440 | int capacity = dictionary->Capacity(); |
| 9441 | for (int i = 0; i < capacity; i++) { |
| 9442 | Handle<Object> key(dictionary->KeyAt(i)); |
| 9443 | if (dictionary->IsKey(*key)) { |
| 9444 | element_count++; |
| 9445 | } |
| 9446 | } |
| 9447 | break; |
| 9448 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9449 | case NON_STRICT_ARGUMENTS_ELEMENTS: |
| 9450 | case EXTERNAL_BYTE_ELEMENTS: |
| 9451 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 9452 | case EXTERNAL_SHORT_ELEMENTS: |
| 9453 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 9454 | case EXTERNAL_INT_ELEMENTS: |
| 9455 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 9456 | case EXTERNAL_FLOAT_ELEMENTS: |
| 9457 | case EXTERNAL_DOUBLE_ELEMENTS: |
| 9458 | case EXTERNAL_PIXEL_ELEMENTS: |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9459 | // External arrays are always dense. |
| 9460 | return length; |
| 9461 | } |
| 9462 | // As an estimate, we assume that the prototype doesn't contain any |
| 9463 | // inherited elements. |
| 9464 | return element_count; |
| 9465 | } |
| 9466 | |
| 9467 | |
| 9468 | |
| 9469 | template<class ExternalArrayClass, class ElementType> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9470 | static void IterateExternalArrayElements(Isolate* isolate, |
| 9471 | Handle<JSObject> receiver, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9472 | bool elements_are_ints, |
| 9473 | bool elements_are_guaranteed_smis, |
| 9474 | ArrayConcatVisitor* visitor) { |
| 9475 | Handle<ExternalArrayClass> array( |
| 9476 | ExternalArrayClass::cast(receiver->elements())); |
| 9477 | uint32_t len = static_cast<uint32_t>(array->length()); |
| 9478 | |
| 9479 | ASSERT(visitor != NULL); |
| 9480 | if (elements_are_ints) { |
| 9481 | if (elements_are_guaranteed_smis) { |
| 9482 | for (uint32_t j = 0; j < len; j++) { |
| 9483 | HandleScope loop_scope; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9484 | Handle<Smi> e(Smi::FromInt(static_cast<int>(array->get_scalar(j)))); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9485 | visitor->visit(j, e); |
| 9486 | } |
| 9487 | } else { |
| 9488 | for (uint32_t j = 0; j < len; j++) { |
| 9489 | HandleScope loop_scope; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9490 | int64_t val = static_cast<int64_t>(array->get_scalar(j)); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9491 | if (Smi::IsValid(static_cast<intptr_t>(val))) { |
| 9492 | Handle<Smi> e(Smi::FromInt(static_cast<int>(val))); |
| 9493 | visitor->visit(j, e); |
| 9494 | } else { |
| 9495 | Handle<Object> e = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9496 | isolate->factory()->NewNumber(static_cast<ElementType>(val)); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9497 | visitor->visit(j, e); |
| 9498 | } |
| 9499 | } |
| 9500 | } |
| 9501 | } else { |
| 9502 | for (uint32_t j = 0; j < len; j++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9503 | HandleScope loop_scope(isolate); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9504 | Handle<Object> e = isolate->factory()->NewNumber(array->get_scalar(j)); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9505 | visitor->visit(j, e); |
| 9506 | } |
| 9507 | } |
| 9508 | } |
| 9509 | |
| 9510 | |
| 9511 | // Used for sorting indices in a List<uint32_t>. |
| 9512 | static int compareUInt32(const uint32_t* ap, const uint32_t* bp) { |
| 9513 | uint32_t a = *ap; |
| 9514 | uint32_t b = *bp; |
| 9515 | return (a == b) ? 0 : (a < b) ? -1 : 1; |
| 9516 | } |
| 9517 | |
| 9518 | |
| 9519 | static void CollectElementIndices(Handle<JSObject> object, |
| 9520 | uint32_t range, |
| 9521 | List<uint32_t>* indices) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9522 | ElementsKind kind = object->GetElementsKind(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9523 | switch (kind) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9524 | case FAST_SMI_ONLY_ELEMENTS: |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9525 | case FAST_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9526 | Handle<FixedArray> elements(FixedArray::cast(object->elements())); |
| 9527 | uint32_t length = static_cast<uint32_t>(elements->length()); |
| 9528 | if (range < length) length = range; |
| 9529 | for (uint32_t i = 0; i < length; i++) { |
| 9530 | if (!elements->get(i)->IsTheHole()) { |
| 9531 | indices->Add(i); |
| 9532 | } |
| 9533 | } |
| 9534 | break; |
| 9535 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9536 | case FAST_DOUBLE_ELEMENTS: { |
| 9537 | // TODO(1810): Decide if it's worthwhile to implement this. |
| 9538 | UNREACHABLE(); |
| 9539 | break; |
| 9540 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9541 | case DICTIONARY_ELEMENTS: { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 9542 | Handle<SeededNumberDictionary> dict( |
| 9543 | SeededNumberDictionary::cast(object->elements())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9544 | uint32_t capacity = dict->Capacity(); |
| 9545 | for (uint32_t j = 0; j < capacity; j++) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9546 | HandleScope loop_scope; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9547 | Handle<Object> k(dict->KeyAt(j)); |
| 9548 | if (dict->IsKey(*k)) { |
| 9549 | ASSERT(k->IsNumber()); |
| 9550 | uint32_t index = static_cast<uint32_t>(k->Number()); |
| 9551 | if (index < range) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9552 | indices->Add(index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9553 | } |
| 9554 | } |
| 9555 | } |
| 9556 | break; |
| 9557 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9558 | default: { |
| 9559 | int dense_elements_length; |
| 9560 | switch (kind) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9561 | case EXTERNAL_PIXEL_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9562 | dense_elements_length = |
| 9563 | ExternalPixelArray::cast(object->elements())->length(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9564 | break; |
| 9565 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9566 | case EXTERNAL_BYTE_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9567 | dense_elements_length = |
| 9568 | ExternalByteArray::cast(object->elements())->length(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9569 | break; |
| 9570 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9571 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9572 | dense_elements_length = |
| 9573 | ExternalUnsignedByteArray::cast(object->elements())->length(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9574 | break; |
| 9575 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9576 | case EXTERNAL_SHORT_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9577 | dense_elements_length = |
| 9578 | ExternalShortArray::cast(object->elements())->length(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9579 | break; |
| 9580 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9581 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9582 | dense_elements_length = |
| 9583 | ExternalUnsignedShortArray::cast(object->elements())->length(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9584 | break; |
| 9585 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9586 | case EXTERNAL_INT_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9587 | dense_elements_length = |
| 9588 | ExternalIntArray::cast(object->elements())->length(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9589 | break; |
| 9590 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9591 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9592 | dense_elements_length = |
| 9593 | ExternalUnsignedIntArray::cast(object->elements())->length(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9594 | break; |
| 9595 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9596 | case EXTERNAL_FLOAT_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9597 | dense_elements_length = |
| 9598 | ExternalFloatArray::cast(object->elements())->length(); |
| 9599 | break; |
| 9600 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9601 | case EXTERNAL_DOUBLE_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9602 | dense_elements_length = |
| 9603 | ExternalDoubleArray::cast(object->elements())->length(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9604 | break; |
| 9605 | } |
| 9606 | default: |
| 9607 | UNREACHABLE(); |
| 9608 | dense_elements_length = 0; |
| 9609 | break; |
| 9610 | } |
| 9611 | uint32_t length = static_cast<uint32_t>(dense_elements_length); |
| 9612 | if (range <= length) { |
| 9613 | length = range; |
| 9614 | // We will add all indices, so we might as well clear it first |
| 9615 | // and avoid duplicates. |
| 9616 | indices->Clear(); |
| 9617 | } |
| 9618 | for (uint32_t i = 0; i < length; i++) { |
| 9619 | indices->Add(i); |
| 9620 | } |
| 9621 | if (length == range) return; // All indices accounted for already. |
| 9622 | break; |
| 9623 | } |
| 9624 | } |
| 9625 | |
| 9626 | Handle<Object> prototype(object->GetPrototype()); |
| 9627 | if (prototype->IsJSObject()) { |
| 9628 | // The prototype will usually have no inherited element indices, |
| 9629 | // but we have to check. |
| 9630 | CollectElementIndices(Handle<JSObject>::cast(prototype), range, indices); |
| 9631 | } |
| 9632 | } |
| 9633 | |
| 9634 | |
| 9635 | /** |
| 9636 | * A helper function that visits elements of a JSArray in numerical |
| 9637 | * order. |
| 9638 | * |
| 9639 | * The visitor argument called for each existing element in the array |
| 9640 | * with the element index and the element's value. |
| 9641 | * Afterwards it increments the base-index of the visitor by the array |
| 9642 | * length. |
| 9643 | * Returns false if any access threw an exception, otherwise true. |
| 9644 | */ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9645 | static bool IterateElements(Isolate* isolate, |
| 9646 | Handle<JSArray> receiver, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9647 | ArrayConcatVisitor* visitor) { |
| 9648 | uint32_t length = static_cast<uint32_t>(receiver->length()->Number()); |
| 9649 | switch (receiver->GetElementsKind()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9650 | case FAST_SMI_ONLY_ELEMENTS: |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9651 | case FAST_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9652 | // Run through the elements FixedArray and use HasElement and GetElement |
| 9653 | // to check the prototype for missing elements. |
| 9654 | Handle<FixedArray> elements(FixedArray::cast(receiver->elements())); |
| 9655 | int fast_length = static_cast<int>(length); |
| 9656 | ASSERT(fast_length <= elements->length()); |
| 9657 | for (int j = 0; j < fast_length; j++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9658 | HandleScope loop_scope(isolate); |
| 9659 | Handle<Object> element_value(elements->get(j), isolate); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9660 | if (!element_value->IsTheHole()) { |
| 9661 | visitor->visit(j, element_value); |
| 9662 | } else if (receiver->HasElement(j)) { |
| 9663 | // Call GetElement on receiver, not its prototype, or getters won't |
| 9664 | // have the correct receiver. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9665 | element_value = Object::GetElement(receiver, j); |
| 9666 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, element_value, false); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9667 | visitor->visit(j, element_value); |
| 9668 | } |
| 9669 | } |
| 9670 | break; |
| 9671 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9672 | case FAST_DOUBLE_ELEMENTS: { |
| 9673 | // TODO(1810): Decide if it's worthwhile to implement this. |
| 9674 | UNREACHABLE(); |
| 9675 | break; |
| 9676 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9677 | case DICTIONARY_ELEMENTS: { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 9678 | Handle<SeededNumberDictionary> dict(receiver->element_dictionary()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9679 | List<uint32_t> indices(dict->Capacity() / 2); |
| 9680 | // Collect all indices in the object and the prototypes less |
| 9681 | // than length. This might introduce duplicates in the indices list. |
| 9682 | CollectElementIndices(receiver, length, &indices); |
| 9683 | indices.Sort(&compareUInt32); |
| 9684 | int j = 0; |
| 9685 | int n = indices.length(); |
| 9686 | while (j < n) { |
| 9687 | HandleScope loop_scope; |
| 9688 | uint32_t index = indices[j]; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9689 | Handle<Object> element = Object::GetElement(receiver, index); |
| 9690 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, element, false); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9691 | visitor->visit(index, element); |
| 9692 | // Skip to next different index (i.e., omit duplicates). |
| 9693 | do { |
| 9694 | j++; |
| 9695 | } while (j < n && indices[j] == index); |
| 9696 | } |
| 9697 | break; |
| 9698 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9699 | case EXTERNAL_PIXEL_ELEMENTS: { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9700 | Handle<ExternalPixelArray> pixels(ExternalPixelArray::cast( |
| 9701 | receiver->elements())); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9702 | for (uint32_t j = 0; j < length; j++) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9703 | Handle<Smi> e(Smi::FromInt(pixels->get_scalar(j))); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9704 | visitor->visit(j, e); |
| 9705 | } |
| 9706 | break; |
| 9707 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9708 | case EXTERNAL_BYTE_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9709 | IterateExternalArrayElements<ExternalByteArray, int8_t>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9710 | isolate, receiver, true, true, visitor); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9711 | break; |
| 9712 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9713 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9714 | IterateExternalArrayElements<ExternalUnsignedByteArray, uint8_t>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9715 | isolate, receiver, true, true, visitor); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9716 | break; |
| 9717 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9718 | case EXTERNAL_SHORT_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9719 | IterateExternalArrayElements<ExternalShortArray, int16_t>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9720 | isolate, receiver, true, true, visitor); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9721 | break; |
| 9722 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9723 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9724 | IterateExternalArrayElements<ExternalUnsignedShortArray, uint16_t>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9725 | isolate, receiver, true, true, visitor); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9726 | break; |
| 9727 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9728 | case EXTERNAL_INT_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9729 | IterateExternalArrayElements<ExternalIntArray, int32_t>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9730 | isolate, receiver, true, false, visitor); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9731 | break; |
| 9732 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9733 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9734 | IterateExternalArrayElements<ExternalUnsignedIntArray, uint32_t>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9735 | isolate, receiver, true, false, visitor); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9736 | break; |
| 9737 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9738 | case EXTERNAL_FLOAT_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9739 | IterateExternalArrayElements<ExternalFloatArray, float>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9740 | isolate, receiver, false, false, visitor); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9741 | break; |
| 9742 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9743 | case EXTERNAL_DOUBLE_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9744 | IterateExternalArrayElements<ExternalDoubleArray, double>( |
| 9745 | isolate, receiver, false, false, visitor); |
| 9746 | break; |
| 9747 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9748 | default: |
| 9749 | UNREACHABLE(); |
| 9750 | break; |
| 9751 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9752 | visitor->increase_index_offset(length); |
| 9753 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9754 | } |
| 9755 | |
| 9756 | |
| 9757 | /** |
| 9758 | * Array::concat implementation. |
| 9759 | * See ECMAScript 262, 15.4.4.4. |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9760 | * TODO(581): Fix non-compliance for very large concatenations and update to |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 9761 | * following the ECMAScript 5 specification. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9762 | */ |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9763 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayConcat) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9764 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9765 | HandleScope handle_scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9766 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9767 | CONVERT_ARG_HANDLE_CHECKED(JSArray, arguments, 0); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9768 | int argument_count = static_cast<int>(arguments->length()->Number()); |
| 9769 | RUNTIME_ASSERT(arguments->HasFastElements()); |
| 9770 | Handle<FixedArray> elements(FixedArray::cast(arguments->elements())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9771 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9772 | // Pass 1: estimate the length and number of elements of the result. |
| 9773 | // The actual length can be larger if any of the arguments have getters |
| 9774 | // that mutate other arguments (but will otherwise be precise). |
| 9775 | // The number of elements is precise if there are no inherited elements. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9776 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9777 | uint32_t estimate_result_length = 0; |
| 9778 | uint32_t estimate_nof_elements = 0; |
| 9779 | { |
| 9780 | for (int i = 0; i < argument_count; i++) { |
| 9781 | HandleScope loop_scope; |
| 9782 | Handle<Object> obj(elements->get(i)); |
| 9783 | uint32_t length_estimate; |
| 9784 | uint32_t element_estimate; |
| 9785 | if (obj->IsJSArray()) { |
| 9786 | Handle<JSArray> array(Handle<JSArray>::cast(obj)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9787 | // TODO(1810): Find out if it's worthwhile to properly support |
| 9788 | // arbitrary ElementsKinds. For now, pessimistically transition to |
| 9789 | // FAST_ELEMENTS. |
| 9790 | if (array->HasFastDoubleElements()) { |
| 9791 | array = Handle<JSArray>::cast( |
| 9792 | JSObject::TransitionElementsKind(array, FAST_ELEMENTS)); |
| 9793 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9794 | length_estimate = |
| 9795 | static_cast<uint32_t>(array->length()->Number()); |
| 9796 | element_estimate = |
| 9797 | EstimateElementCount(array); |
| 9798 | } else { |
| 9799 | length_estimate = 1; |
| 9800 | element_estimate = 1; |
| 9801 | } |
| 9802 | // Avoid overflows by capping at kMaxElementCount. |
| 9803 | if (JSObject::kMaxElementCount - estimate_result_length < |
| 9804 | length_estimate) { |
| 9805 | estimate_result_length = JSObject::kMaxElementCount; |
| 9806 | } else { |
| 9807 | estimate_result_length += length_estimate; |
| 9808 | } |
| 9809 | if (JSObject::kMaxElementCount - estimate_nof_elements < |
| 9810 | element_estimate) { |
| 9811 | estimate_nof_elements = JSObject::kMaxElementCount; |
| 9812 | } else { |
| 9813 | estimate_nof_elements += element_estimate; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9814 | } |
| 9815 | } |
| 9816 | } |
| 9817 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9818 | // If estimated number of elements is more than half of length, a |
| 9819 | // fixed array (fast case) is more time and space-efficient than a |
| 9820 | // dictionary. |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9821 | bool fast_case = (estimate_nof_elements * 2) >= estimate_result_length; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9822 | |
| 9823 | Handle<FixedArray> storage; |
| 9824 | if (fast_case) { |
| 9825 | // The backing storage array must have non-existing elements to |
| 9826 | // preserve holes across concat operations. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9827 | storage = isolate->factory()->NewFixedArrayWithHoles( |
| 9828 | estimate_result_length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9829 | } else { |
| 9830 | // TODO(126): move 25% pre-allocation logic into Dictionary::Allocate |
| 9831 | uint32_t at_least_space_for = estimate_nof_elements + |
| 9832 | (estimate_nof_elements >> 2); |
| 9833 | storage = Handle<FixedArray>::cast( |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 9834 | isolate->factory()->NewSeededNumberDictionary(at_least_space_for)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9835 | } |
| 9836 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9837 | ArrayConcatVisitor visitor(isolate, storage, fast_case); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9838 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9839 | for (int i = 0; i < argument_count; i++) { |
| 9840 | Handle<Object> obj(elements->get(i)); |
| 9841 | if (obj->IsJSArray()) { |
| 9842 | Handle<JSArray> array = Handle<JSArray>::cast(obj); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9843 | if (!IterateElements(isolate, array, &visitor)) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9844 | return Failure::Exception(); |
| 9845 | } |
| 9846 | } else { |
| 9847 | visitor.visit(0, obj); |
| 9848 | visitor.increase_index_offset(1); |
| 9849 | } |
| 9850 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9851 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9852 | return *visitor.ToArray(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9853 | } |
| 9854 | |
| 9855 | |
| 9856 | // This will not allocate (flatten the string), but it may run |
| 9857 | // very slowly for very deeply nested ConsStrings. For debugging use only. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9858 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GlobalPrint) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9859 | NoHandleAllocation ha; |
| 9860 | ASSERT(args.length() == 1); |
| 9861 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9862 | CONVERT_ARG_CHECKED(String, string, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9863 | StringInputBuffer buffer(string); |
| 9864 | while (buffer.has_more()) { |
| 9865 | uint16_t character = buffer.GetNext(); |
| 9866 | PrintF("%c", character); |
| 9867 | } |
| 9868 | return string; |
| 9869 | } |
| 9870 | |
| 9871 | // Moves all own elements of an object, that are below a limit, to positions |
| 9872 | // starting at zero. All undefined values are placed after non-undefined values, |
| 9873 | // and are followed by non-existing element. Does not change the length |
| 9874 | // property. |
| 9875 | // Returns the number of non-undefined elements collected. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9876 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RemoveArrayHoles) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9877 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9878 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9879 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]); |
| 9880 | return object->PrepareElementsForSort(limit); |
| 9881 | } |
| 9882 | |
| 9883 | |
| 9884 | // Move contents of argument 0 (an array) to argument 1 (an array) |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9885 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MoveArrayContents) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9886 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9887 | CONVERT_ARG_CHECKED(JSArray, from, 0); |
| 9888 | CONVERT_ARG_CHECKED(JSArray, to, 1); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9889 | FixedArrayBase* new_elements = from->elements(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9890 | MaybeObject* maybe_new_map; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9891 | ElementsKind elements_kind; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9892 | if (new_elements->map() == isolate->heap()->fixed_array_map() || |
| 9893 | new_elements->map() == isolate->heap()->fixed_cow_array_map()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9894 | elements_kind = FAST_ELEMENTS; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9895 | } else if (new_elements->map() == |
| 9896 | isolate->heap()->fixed_double_array_map()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9897 | elements_kind = FAST_DOUBLE_ELEMENTS; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9898 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9899 | elements_kind = DICTIONARY_ELEMENTS; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9900 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9901 | maybe_new_map = to->GetElementsTransitionMap(isolate, elements_kind); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9902 | Object* new_map; |
| 9903 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9904 | to->set_map(Map::cast(new_map)); |
| 9905 | to->set_elements(new_elements); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9906 | to->set_length(from->length()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9907 | Object* obj; |
| 9908 | { MaybeObject* maybe_obj = from->ResetElements(); |
| 9909 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9910 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9911 | from->set_length(Smi::FromInt(0)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9912 | return to; |
| 9913 | } |
| 9914 | |
| 9915 | |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 9916 | // How many elements does this object/array have? |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9917 | RUNTIME_FUNCTION(MaybeObject*, Runtime_EstimateNumberOfElements) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9918 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9919 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 9920 | HeapObject* elements = object->elements(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9921 | if (elements->IsDictionary()) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 9922 | int result = SeededNumberDictionary::cast(elements)->NumberOfElements(); |
| 9923 | return Smi::FromInt(result); |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 9924 | } else if (object->IsJSArray()) { |
| 9925 | return JSArray::cast(object)->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9926 | } else { |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 9927 | return Smi::FromInt(FixedArray::cast(elements)->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9928 | } |
| 9929 | } |
| 9930 | |
| 9931 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9932 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SwapElements) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9933 | HandleScope handle_scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9934 | |
| 9935 | ASSERT_EQ(3, args.length()); |
| 9936 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9937 | CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9938 | Handle<Object> key1 = args.at<Object>(1); |
| 9939 | Handle<Object> key2 = args.at<Object>(2); |
| 9940 | |
| 9941 | uint32_t index1, index2; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 9942 | if (!key1->ToArrayIndex(&index1) |
| 9943 | || !key2->ToArrayIndex(&index2)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9944 | return isolate->ThrowIllegalOperation(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9945 | } |
| 9946 | |
| 9947 | Handle<JSObject> jsobject = Handle<JSObject>::cast(object); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9948 | Handle<Object> tmp1 = Object::GetElement(jsobject, index1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9949 | RETURN_IF_EMPTY_HANDLE(isolate, tmp1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9950 | Handle<Object> tmp2 = Object::GetElement(jsobject, index2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9951 | RETURN_IF_EMPTY_HANDLE(isolate, tmp2); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9952 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9953 | RETURN_IF_EMPTY_HANDLE( |
| 9954 | isolate, JSObject::SetElement(jsobject, index1, tmp2, NONE, kStrictMode)); |
| 9955 | RETURN_IF_EMPTY_HANDLE( |
| 9956 | isolate, JSObject::SetElement(jsobject, index2, tmp1, NONE, kStrictMode)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9957 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9958 | return isolate->heap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9959 | } |
| 9960 | |
| 9961 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9962 | // Returns an array that tells you where in the [0, length) interval an array |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 9963 | // might have elements. Can either return keys (positive integers) or |
| 9964 | // intervals (pair of a negative integer (-start-1) followed by a |
| 9965 | // positive (length)) or undefined values. |
| 9966 | // Intervals can span over some keys that are not in the object. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9967 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetArrayKeys) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9968 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9969 | HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9970 | CONVERT_ARG_HANDLE_CHECKED(JSObject, array, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9971 | CONVERT_NUMBER_CHECKED(uint32_t, length, Uint32, args[1]); |
| 9972 | if (array->elements()->IsDictionary()) { |
| 9973 | // Create an array and get all the keys into it, then remove all the |
| 9974 | // keys that are not integers in the range 0 to length-1. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9975 | bool threw = false; |
| 9976 | Handle<FixedArray> keys = |
| 9977 | GetKeysInFixedArrayFor(array, INCLUDE_PROTOS, &threw); |
| 9978 | if (threw) return Failure::Exception(); |
| 9979 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9980 | int keys_length = keys->length(); |
| 9981 | for (int i = 0; i < keys_length; i++) { |
| 9982 | Object* key = keys->get(i); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 9983 | uint32_t index = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 9984 | if (!key->ToArrayIndex(&index) || index >= length) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9985 | // Zap invalid keys. |
| 9986 | keys->set_undefined(i); |
| 9987 | } |
| 9988 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9989 | return *isolate->factory()->NewJSArrayWithElements(keys); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9990 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 9991 | ASSERT(array->HasFastElements() || |
| 9992 | array->HasFastSmiOnlyElements() || |
| 9993 | array->HasFastDoubleElements()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9994 | Handle<FixedArray> single_interval = isolate->factory()->NewFixedArray(2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9995 | // -1 means start of array. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9996 | single_interval->set(0, Smi::FromInt(-1)); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9997 | FixedArrayBase* elements = FixedArrayBase::cast(array->elements()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 9998 | uint32_t actual_length = |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9999 | static_cast<uint32_t>(elements->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10000 | uint32_t min_length = actual_length < length ? actual_length : length; |
| 10001 | Handle<Object> length_object = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10002 | isolate->factory()->NewNumber(static_cast<double>(min_length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10003 | single_interval->set(1, *length_object); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10004 | return *isolate->factory()->NewJSArrayWithElements(single_interval); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10005 | } |
| 10006 | } |
| 10007 | |
| 10008 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10009 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LookupAccessor) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10010 | ASSERT(args.length() == 3); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10011 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 10012 | CONVERT_ARG_CHECKED(String, name, 1); |
| 10013 | CONVERT_SMI_ARG_CHECKED(flag, 2); |
| 10014 | AccessorComponent component = flag == 0 ? ACCESSOR_GETTER : ACCESSOR_SETTER; |
| 10015 | return obj->LookupAccessor(name, component); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10016 | } |
| 10017 | |
| 10018 | |
| 10019 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10020 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugBreak) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10021 | ASSERT(args.length() == 0); |
| 10022 | return Execution::DebugBreakHelper(); |
| 10023 | } |
| 10024 | |
| 10025 | |
| 10026 | // Helper functions for wrapping and unwrapping stack frame ids. |
| 10027 | static Smi* WrapFrameId(StackFrame::Id id) { |
| 10028 | ASSERT(IsAligned(OffsetFrom(id), static_cast<intptr_t>(4))); |
| 10029 | return Smi::FromInt(id >> 2); |
| 10030 | } |
| 10031 | |
| 10032 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10033 | static StackFrame::Id UnwrapFrameId(int wrapped) { |
| 10034 | return static_cast<StackFrame::Id>(wrapped << 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10035 | } |
| 10036 | |
| 10037 | |
| 10038 | // Adds a JavaScript function as a debug event listener. |
| 10039 | // args[0]: debug event listener function to set or null or undefined for |
| 10040 | // clearing the event listener function |
| 10041 | // args[1]: object supplied during callback |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10042 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetDebugEventListener) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10043 | ASSERT(args.length() == 2); |
| 10044 | RUNTIME_ASSERT(args[0]->IsJSFunction() || |
| 10045 | args[0]->IsUndefined() || |
| 10046 | args[0]->IsNull()); |
| 10047 | Handle<Object> callback = args.at<Object>(0); |
| 10048 | Handle<Object> data = args.at<Object>(1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10049 | isolate->debugger()->SetEventListener(callback, data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10050 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10051 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10052 | } |
| 10053 | |
| 10054 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10055 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Break) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10056 | ASSERT(args.length() == 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10057 | isolate->stack_guard()->DebugBreak(); |
| 10058 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10059 | } |
| 10060 | |
| 10061 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10062 | static MaybeObject* DebugLookupResultValue(Heap* heap, |
| 10063 | Object* receiver, |
| 10064 | String* name, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10065 | LookupResult* result, |
| 10066 | bool* caught_exception) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10067 | Object* value; |
| 10068 | switch (result->type()) { |
| 10069 | case NORMAL: |
| 10070 | value = result->holder()->GetNormalizedProperty(result); |
| 10071 | if (value->IsTheHole()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10072 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10073 | } |
| 10074 | return value; |
| 10075 | case FIELD: |
| 10076 | value = |
| 10077 | JSObject::cast( |
| 10078 | result->holder())->FastPropertyAt(result->GetFieldIndex()); |
| 10079 | if (value->IsTheHole()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10080 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10081 | } |
| 10082 | return value; |
| 10083 | case CONSTANT_FUNCTION: |
| 10084 | return result->GetConstantFunction(); |
| 10085 | case CALLBACKS: { |
| 10086 | Object* structure = result->GetCallbackObject(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 10087 | if (structure->IsForeign() || structure->IsAccessorInfo()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10088 | MaybeObject* maybe_value = result->holder()->GetPropertyWithCallback( |
| 10089 | receiver, structure, name); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10090 | if (!maybe_value->ToObject(&value)) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 10091 | if (maybe_value->IsRetryAfterGC()) return maybe_value; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10092 | ASSERT(maybe_value->IsException()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10093 | maybe_value = heap->isolate()->pending_exception(); |
| 10094 | heap->isolate()->clear_pending_exception(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10095 | if (caught_exception != NULL) { |
| 10096 | *caught_exception = true; |
| 10097 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10098 | return maybe_value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10099 | } |
| 10100 | return value; |
| 10101 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10102 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10103 | } |
| 10104 | } |
| 10105 | case INTERCEPTOR: |
| 10106 | case MAP_TRANSITION: |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 10107 | case ELEMENTS_TRANSITION: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10108 | case CONSTANT_TRANSITION: |
| 10109 | case NULL_DESCRIPTOR: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10110 | return heap->undefined_value(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10111 | case HANDLER: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10112 | UNREACHABLE(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10113 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10114 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10115 | UNREACHABLE(); // keep the compiler happy |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10116 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10117 | } |
| 10118 | |
| 10119 | |
| 10120 | // Get debugger related details for an object property. |
| 10121 | // args[0]: object holding property |
| 10122 | // args[1]: name of the property |
| 10123 | // |
| 10124 | // The array returned contains the following information: |
| 10125 | // 0: Property value |
| 10126 | // 1: Property details |
| 10127 | // 2: Property value is exception |
| 10128 | // 3: Getter function if defined |
| 10129 | // 4: Setter function if defined |
| 10130 | // Items 2-4 are only filled if the property has either a getter or a setter |
| 10131 | // defined through __defineGetter__ and/or __defineSetter__. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10132 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetPropertyDetails) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10133 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10134 | |
| 10135 | ASSERT(args.length() == 2); |
| 10136 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10137 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
| 10138 | CONVERT_ARG_HANDLE_CHECKED(String, name, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10139 | |
| 10140 | // Make sure to set the current context to the context before the debugger was |
| 10141 | // entered (if the debugger is entered). The reason for switching context here |
| 10142 | // is that for some property lookups (accessors and interceptors) callbacks |
| 10143 | // into the embedding application can occour, and the embedding application |
| 10144 | // could have the assumption that its own global context is the current |
| 10145 | // context and not some internal debugger context. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10146 | SaveContext save(isolate); |
| 10147 | if (isolate->debug()->InDebugger()) { |
| 10148 | isolate->set_context(*isolate->debug()->debugger_entry()->GetContext()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10149 | } |
| 10150 | |
| 10151 | // Skip the global proxy as it has no properties and always delegates to the |
| 10152 | // real global object. |
| 10153 | if (obj->IsJSGlobalProxy()) { |
| 10154 | obj = Handle<JSObject>(JSObject::cast(obj->GetPrototype())); |
| 10155 | } |
| 10156 | |
| 10157 | |
| 10158 | // Check if the name is trivially convertible to an index and get the element |
| 10159 | // if so. |
| 10160 | uint32_t index; |
| 10161 | if (name->AsArrayIndex(&index)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10162 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(2); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10163 | Object* element_or_char; |
| 10164 | { MaybeObject* maybe_element_or_char = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10165 | Runtime::GetElementOrCharAt(isolate, obj, index); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10166 | if (!maybe_element_or_char->ToObject(&element_or_char)) { |
| 10167 | return maybe_element_or_char; |
| 10168 | } |
| 10169 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 10170 | details->set(0, element_or_char); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10171 | details->set(1, PropertyDetails(NONE, NORMAL).AsSmi()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10172 | return *isolate->factory()->NewJSArrayWithElements(details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10173 | } |
| 10174 | |
| 10175 | // Find the number of objects making up this. |
| 10176 | int length = LocalPrototypeChainLength(*obj); |
| 10177 | |
| 10178 | // Try local lookup on each of the objects. |
| 10179 | Handle<JSObject> jsproto = obj; |
| 10180 | for (int i = 0; i < length; i++) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10181 | LookupResult result(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10182 | jsproto->LocalLookup(*name, &result); |
| 10183 | if (result.IsProperty()) { |
| 10184 | // LookupResult is not GC safe as it holds raw object pointers. |
| 10185 | // GC can happen later in this code so put the required fields into |
| 10186 | // local variables using handles when required for later use. |
| 10187 | PropertyType result_type = result.type(); |
| 10188 | Handle<Object> result_callback_obj; |
| 10189 | if (result_type == CALLBACKS) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10190 | result_callback_obj = Handle<Object>(result.GetCallbackObject(), |
| 10191 | isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10192 | } |
| 10193 | Smi* property_details = result.GetPropertyDetails().AsSmi(); |
| 10194 | // DebugLookupResultValue can cause GC so details from LookupResult needs |
| 10195 | // to be copied to handles before this. |
| 10196 | bool caught_exception = false; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10197 | Object* raw_value; |
| 10198 | { MaybeObject* maybe_raw_value = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10199 | DebugLookupResultValue(isolate->heap(), *obj, *name, |
| 10200 | &result, &caught_exception); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10201 | if (!maybe_raw_value->ToObject(&raw_value)) return maybe_raw_value; |
| 10202 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10203 | Handle<Object> value(raw_value, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10204 | |
| 10205 | // If the callback object is a fixed array then it contains JavaScript |
| 10206 | // getter and/or setter. |
| 10207 | bool hasJavaScriptAccessors = result_type == CALLBACKS && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10208 | result_callback_obj->IsAccessorPair(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10209 | Handle<FixedArray> details = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10210 | isolate->factory()->NewFixedArray(hasJavaScriptAccessors ? 5 : 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10211 | details->set(0, *value); |
| 10212 | details->set(1, property_details); |
| 10213 | if (hasJavaScriptAccessors) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10214 | AccessorPair* accessors = AccessorPair::cast(*result_callback_obj); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10215 | details->set(2, isolate->heap()->ToBoolean(caught_exception)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10216 | details->set(3, accessors->GetComponent(ACCESSOR_GETTER)); |
| 10217 | details->set(4, accessors->GetComponent(ACCESSOR_SETTER)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10218 | } |
| 10219 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10220 | return *isolate->factory()->NewJSArrayWithElements(details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10221 | } |
| 10222 | if (i < length - 1) { |
| 10223 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 10224 | } |
| 10225 | } |
| 10226 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10227 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10228 | } |
| 10229 | |
| 10230 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10231 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetProperty) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10232 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10233 | |
| 10234 | ASSERT(args.length() == 2); |
| 10235 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10236 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
| 10237 | CONVERT_ARG_HANDLE_CHECKED(String, name, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10238 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10239 | LookupResult result(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10240 | obj->Lookup(*name, &result); |
| 10241 | if (result.IsProperty()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10242 | return DebugLookupResultValue(isolate->heap(), *obj, *name, &result, NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10243 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10244 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10245 | } |
| 10246 | |
| 10247 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10248 | // Return the property type calculated from the property details. |
| 10249 | // args[0]: smi with property details. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10250 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPropertyTypeFromDetails) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10251 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10252 | CONVERT_PROPERTY_DETAILS_CHECKED(details, 0); |
| 10253 | return Smi::FromInt(static_cast<int>(details.type())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10254 | } |
| 10255 | |
| 10256 | |
| 10257 | // Return the property attribute calculated from the property details. |
| 10258 | // args[0]: smi with property details. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10259 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPropertyAttributesFromDetails) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10260 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10261 | CONVERT_PROPERTY_DETAILS_CHECKED(details, 0); |
| 10262 | return Smi::FromInt(static_cast<int>(details.attributes())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10263 | } |
| 10264 | |
| 10265 | |
| 10266 | // Return the property insertion index calculated from the property details. |
| 10267 | // args[0]: smi with property details. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10268 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPropertyIndexFromDetails) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10269 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10270 | CONVERT_PROPERTY_DETAILS_CHECKED(details, 0); |
| 10271 | return Smi::FromInt(details.index()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10272 | } |
| 10273 | |
| 10274 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10275 | // Return property value from named interceptor. |
| 10276 | // args[0]: object |
| 10277 | // args[1]: property name |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10278 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugNamedInterceptorPropertyValue) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10279 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10280 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10281 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10282 | RUNTIME_ASSERT(obj->HasNamedInterceptor()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10283 | CONVERT_ARG_HANDLE_CHECKED(String, name, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10284 | |
| 10285 | PropertyAttributes attributes; |
| 10286 | return obj->GetPropertyWithInterceptor(*obj, *name, &attributes); |
| 10287 | } |
| 10288 | |
| 10289 | |
| 10290 | // Return element value from indexed interceptor. |
| 10291 | // args[0]: object |
| 10292 | // args[1]: index |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10293 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugIndexedInterceptorElementValue) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10294 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10295 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10296 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10297 | RUNTIME_ASSERT(obj->HasIndexedInterceptor()); |
| 10298 | CONVERT_NUMBER_CHECKED(uint32_t, index, Uint32, args[1]); |
| 10299 | |
| 10300 | return obj->GetElementWithInterceptor(*obj, index); |
| 10301 | } |
| 10302 | |
| 10303 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10304 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CheckExecutionState) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10305 | ASSERT(args.length() >= 1); |
| 10306 | CONVERT_NUMBER_CHECKED(int, break_id, Int32, args[0]); |
| 10307 | // Check that the break id is valid. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10308 | if (isolate->debug()->break_id() == 0 || |
| 10309 | break_id != isolate->debug()->break_id()) { |
| 10310 | return isolate->Throw( |
| 10311 | isolate->heap()->illegal_execution_state_symbol()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10312 | } |
| 10313 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10314 | return isolate->heap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10315 | } |
| 10316 | |
| 10317 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10318 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFrameCount) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10319 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10320 | ASSERT(args.length() == 1); |
| 10321 | |
| 10322 | // Check arguments. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10323 | Object* result; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10324 | { MaybeObject* maybe_result = Runtime_CheckExecutionState( |
| 10325 | RUNTIME_ARGUMENTS(isolate, args)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10326 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 10327 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10328 | |
| 10329 | // Count all frames which are relevant to debugging stack trace. |
| 10330 | int n = 0; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10331 | StackFrame::Id id = isolate->debug()->break_frame_id(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10332 | if (id == StackFrame::NO_ID) { |
| 10333 | // If there is no JavaScript stack frame count is 0. |
| 10334 | return Smi::FromInt(0); |
| 10335 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10336 | |
| 10337 | for (JavaScriptFrameIterator it(isolate, id); !it.done(); it.Advance()) { |
| 10338 | n += it.frame()->GetInlineCount(); |
| 10339 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10340 | return Smi::FromInt(n); |
| 10341 | } |
| 10342 | |
| 10343 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10344 | class FrameInspector { |
| 10345 | public: |
| 10346 | FrameInspector(JavaScriptFrame* frame, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10347 | int inlined_jsframe_index, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10348 | Isolate* isolate) |
| 10349 | : frame_(frame), deoptimized_frame_(NULL), isolate_(isolate) { |
| 10350 | // Calculate the deoptimized frame. |
| 10351 | if (frame->is_optimized()) { |
| 10352 | deoptimized_frame_ = Deoptimizer::DebuggerInspectableFrame( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10353 | frame, inlined_jsframe_index, isolate); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10354 | } |
| 10355 | has_adapted_arguments_ = frame_->has_adapted_arguments(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10356 | is_bottommost_ = inlined_jsframe_index == 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10357 | is_optimized_ = frame_->is_optimized(); |
| 10358 | } |
| 10359 | |
| 10360 | ~FrameInspector() { |
| 10361 | // Get rid of the calculated deoptimized frame if any. |
| 10362 | if (deoptimized_frame_ != NULL) { |
| 10363 | Deoptimizer::DeleteDebuggerInspectableFrame(deoptimized_frame_, |
| 10364 | isolate_); |
| 10365 | } |
| 10366 | } |
| 10367 | |
| 10368 | int GetParametersCount() { |
| 10369 | return is_optimized_ |
| 10370 | ? deoptimized_frame_->parameters_count() |
| 10371 | : frame_->ComputeParametersCount(); |
| 10372 | } |
| 10373 | int expression_count() { return deoptimized_frame_->expression_count(); } |
| 10374 | Object* GetFunction() { |
| 10375 | return is_optimized_ |
| 10376 | ? deoptimized_frame_->GetFunction() |
| 10377 | : frame_->function(); |
| 10378 | } |
| 10379 | Object* GetParameter(int index) { |
| 10380 | return is_optimized_ |
| 10381 | ? deoptimized_frame_->GetParameter(index) |
| 10382 | : frame_->GetParameter(index); |
| 10383 | } |
| 10384 | Object* GetExpression(int index) { |
| 10385 | return is_optimized_ |
| 10386 | ? deoptimized_frame_->GetExpression(index) |
| 10387 | : frame_->GetExpression(index); |
| 10388 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10389 | int GetSourcePosition() { |
| 10390 | return is_optimized_ |
| 10391 | ? deoptimized_frame_->GetSourcePosition() |
| 10392 | : frame_->LookupCode()->SourcePosition(frame_->pc()); |
| 10393 | } |
| 10394 | bool IsConstructor() { |
| 10395 | return is_optimized_ && !is_bottommost_ |
| 10396 | ? deoptimized_frame_->HasConstructStub() |
| 10397 | : frame_->IsConstructor(); |
| 10398 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10399 | |
| 10400 | // To inspect all the provided arguments the frame might need to be |
| 10401 | // replaced with the arguments frame. |
| 10402 | void SetArgumentsFrame(JavaScriptFrame* frame) { |
| 10403 | ASSERT(has_adapted_arguments_); |
| 10404 | frame_ = frame; |
| 10405 | is_optimized_ = frame_->is_optimized(); |
| 10406 | ASSERT(!is_optimized_); |
| 10407 | } |
| 10408 | |
| 10409 | private: |
| 10410 | JavaScriptFrame* frame_; |
| 10411 | DeoptimizedFrameInfo* deoptimized_frame_; |
| 10412 | Isolate* isolate_; |
| 10413 | bool is_optimized_; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10414 | bool is_bottommost_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10415 | bool has_adapted_arguments_; |
| 10416 | |
| 10417 | DISALLOW_COPY_AND_ASSIGN(FrameInspector); |
| 10418 | }; |
| 10419 | |
| 10420 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10421 | static const int kFrameDetailsFrameIdIndex = 0; |
| 10422 | static const int kFrameDetailsReceiverIndex = 1; |
| 10423 | static const int kFrameDetailsFunctionIndex = 2; |
| 10424 | static const int kFrameDetailsArgumentCountIndex = 3; |
| 10425 | static const int kFrameDetailsLocalCountIndex = 4; |
| 10426 | static const int kFrameDetailsSourcePositionIndex = 5; |
| 10427 | static const int kFrameDetailsConstructCallIndex = 6; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10428 | static const int kFrameDetailsAtReturnIndex = 7; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10429 | static const int kFrameDetailsFlagsIndex = 8; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10430 | static const int kFrameDetailsFirstDynamicIndex = 9; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10431 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10432 | |
| 10433 | static SaveContext* FindSavedContextForFrame(Isolate* isolate, |
| 10434 | JavaScriptFrame* frame) { |
| 10435 | SaveContext* save = isolate->save_context(); |
| 10436 | while (save != NULL && !save->IsBelowFrame(frame)) { |
| 10437 | save = save->prev(); |
| 10438 | } |
| 10439 | ASSERT(save != NULL); |
| 10440 | return save; |
| 10441 | } |
| 10442 | |
| 10443 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10444 | // Return an array with frame details |
| 10445 | // args[0]: number: break id |
| 10446 | // args[1]: number: frame index |
| 10447 | // |
| 10448 | // The array returned contains the following information: |
| 10449 | // 0: Frame id |
| 10450 | // 1: Receiver |
| 10451 | // 2: Function |
| 10452 | // 3: Argument count |
| 10453 | // 4: Local count |
| 10454 | // 5: Source position |
| 10455 | // 6: Constructor call |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10456 | // 7: Is at return |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10457 | // 8: Flags |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10458 | // Arguments name, value |
| 10459 | // Locals name, value |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10460 | // Return value if any |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10461 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFrameDetails) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10462 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10463 | ASSERT(args.length() == 2); |
| 10464 | |
| 10465 | // Check arguments. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10466 | Object* check; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10467 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 10468 | RUNTIME_ARGUMENTS(isolate, args)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10469 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 10470 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10471 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[1]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10472 | Heap* heap = isolate->heap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10473 | |
| 10474 | // Find the relevant frame with the requested index. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10475 | StackFrame::Id id = isolate->debug()->break_frame_id(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10476 | if (id == StackFrame::NO_ID) { |
| 10477 | // If there are no JavaScript stack frames return undefined. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10478 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10479 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10480 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10481 | int count = 0; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10482 | JavaScriptFrameIterator it(isolate, id); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10483 | for (; !it.done(); it.Advance()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10484 | if (index < count + it.frame()->GetInlineCount()) break; |
| 10485 | count += it.frame()->GetInlineCount(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10486 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10487 | if (it.done()) return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10488 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10489 | bool is_optimized = it.frame()->is_optimized(); |
| 10490 | |
| 10491 | int inlined_jsframe_index = 0; // Inlined frame index in optimized frame. |
| 10492 | if (is_optimized) { |
| 10493 | inlined_jsframe_index = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10494 | it.frame()->GetInlineCount() - (index - count) - 1; |
| 10495 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10496 | FrameInspector frame_inspector(it.frame(), inlined_jsframe_index, isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 10497 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10498 | // Traverse the saved contexts chain to find the active context for the |
| 10499 | // selected frame. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10500 | SaveContext* save = FindSavedContextForFrame(isolate, it.frame()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10501 | |
| 10502 | // Get the frame id. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10503 | Handle<Object> frame_id(WrapFrameId(it.frame()->id()), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10504 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10505 | // Find source position in unoptimized code. |
| 10506 | int position = frame_inspector.GetSourcePosition(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10507 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10508 | // Check for constructor frame. |
| 10509 | bool constructor = frame_inspector.IsConstructor(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10510 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 10511 | // Get scope info and read from it for local variable information. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10512 | Handle<JSFunction> function(JSFunction::cast(frame_inspector.GetFunction())); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10513 | Handle<SharedFunctionInfo> shared(function->shared()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10514 | Handle<ScopeInfo> scope_info(shared->scope_info()); |
| 10515 | ASSERT(*scope_info != ScopeInfo::Empty()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10516 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10517 | // Get the locals names and values into a temporary array. |
| 10518 | // |
| 10519 | // TODO(1240907): Hide compiler-introduced stack variables |
| 10520 | // (e.g. .result)? For users of the debugger, they will probably be |
| 10521 | // confusing. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10522 | Handle<FixedArray> locals = |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10523 | isolate->factory()->NewFixedArray(scope_info->LocalCount() * 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10524 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 10525 | // Fill in the values of the locals. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10526 | int i = 0; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10527 | for (; i < scope_info->StackLocalCount(); ++i) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10528 | // Use the value from the stack. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10529 | locals->set(i * 2, scope_info->LocalName(i)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10530 | locals->set(i * 2 + 1, frame_inspector.GetExpression(i)); |
| 10531 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10532 | if (i < scope_info->LocalCount()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10533 | // Get the context containing declarations. |
| 10534 | Handle<Context> context( |
| 10535 | Context::cast(it.frame()->context())->declaration_context()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10536 | for (; i < scope_info->LocalCount(); ++i) { |
| 10537 | Handle<String> name(scope_info->LocalName(i)); |
| 10538 | VariableMode mode; |
| 10539 | InitializationFlag init_flag; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10540 | locals->set(i * 2, *name); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10541 | locals->set(i * 2 + 1, context->get( |
| 10542 | scope_info->ContextSlotIndex(*name, &mode, &init_flag))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10543 | } |
| 10544 | } |
| 10545 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 10546 | // Check whether this frame is positioned at return. If not top |
| 10547 | // frame or if the frame is optimized it cannot be at a return. |
| 10548 | bool at_return = false; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10549 | if (!is_optimized && index == 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10550 | at_return = isolate->debug()->IsBreakAtReturn(it.frame()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 10551 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10552 | |
| 10553 | // If positioned just before return find the value to be returned and add it |
| 10554 | // to the frame information. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10555 | Handle<Object> return_value = isolate->factory()->undefined_value(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10556 | if (at_return) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10557 | StackFrameIterator it2(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10558 | Address internal_frame_sp = NULL; |
| 10559 | while (!it2.done()) { |
| 10560 | if (it2.frame()->is_internal()) { |
| 10561 | internal_frame_sp = it2.frame()->sp(); |
| 10562 | } else { |
| 10563 | if (it2.frame()->is_java_script()) { |
| 10564 | if (it2.frame()->id() == it.frame()->id()) { |
| 10565 | // The internal frame just before the JavaScript frame contains the |
| 10566 | // value to return on top. A debug break at return will create an |
| 10567 | // internal frame to store the return value (eax/rax/r0) before |
| 10568 | // entering the debug break exit frame. |
| 10569 | if (internal_frame_sp != NULL) { |
| 10570 | return_value = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10571 | Handle<Object>(Memory::Object_at(internal_frame_sp), |
| 10572 | isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10573 | break; |
| 10574 | } |
| 10575 | } |
| 10576 | } |
| 10577 | |
| 10578 | // Indicate that the previous frame was not an internal frame. |
| 10579 | internal_frame_sp = NULL; |
| 10580 | } |
| 10581 | it2.Advance(); |
| 10582 | } |
| 10583 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10584 | |
| 10585 | // Now advance to the arguments adapter frame (if any). It contains all |
| 10586 | // the provided parameters whereas the function frame always have the number |
| 10587 | // of arguments matching the functions parameters. The rest of the |
| 10588 | // information (except for what is collected above) is the same. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10589 | if ((inlined_jsframe_index == 0) && it.frame()->has_adapted_arguments()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10590 | it.AdvanceToArgumentsFrame(); |
| 10591 | frame_inspector.SetArgumentsFrame(it.frame()); |
| 10592 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10593 | |
| 10594 | // Find the number of arguments to fill. At least fill the number of |
| 10595 | // parameters for the function and fill more if more parameters are provided. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10596 | int argument_count = scope_info->ParameterCount(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10597 | if (argument_count < frame_inspector.GetParametersCount()) { |
| 10598 | argument_count = frame_inspector.GetParametersCount(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10599 | } |
| 10600 | |
| 10601 | // Calculate the size of the result. |
| 10602 | int details_size = kFrameDetailsFirstDynamicIndex + |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10603 | 2 * (argument_count + scope_info->LocalCount()) + |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10604 | (at_return ? 1 : 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10605 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(details_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10606 | |
| 10607 | // Add the frame id. |
| 10608 | details->set(kFrameDetailsFrameIdIndex, *frame_id); |
| 10609 | |
| 10610 | // Add the function (same as in function frame). |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10611 | details->set(kFrameDetailsFunctionIndex, frame_inspector.GetFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10612 | |
| 10613 | // Add the arguments count. |
| 10614 | details->set(kFrameDetailsArgumentCountIndex, Smi::FromInt(argument_count)); |
| 10615 | |
| 10616 | // Add the locals count |
| 10617 | details->set(kFrameDetailsLocalCountIndex, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10618 | Smi::FromInt(scope_info->LocalCount())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10619 | |
| 10620 | // Add the source position. |
| 10621 | if (position != RelocInfo::kNoPosition) { |
| 10622 | details->set(kFrameDetailsSourcePositionIndex, Smi::FromInt(position)); |
| 10623 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10624 | details->set(kFrameDetailsSourcePositionIndex, heap->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10625 | } |
| 10626 | |
| 10627 | // Add the constructor information. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10628 | details->set(kFrameDetailsConstructCallIndex, heap->ToBoolean(constructor)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10629 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10630 | // Add the at return information. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10631 | details->set(kFrameDetailsAtReturnIndex, heap->ToBoolean(at_return)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10632 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10633 | // Add flags to indicate information on whether this frame is |
| 10634 | // bit 0: invoked in the debugger context. |
| 10635 | // bit 1: optimized frame. |
| 10636 | // bit 2: inlined in optimized frame |
| 10637 | int flags = 0; |
| 10638 | if (*save->context() == *isolate->debug()->debug_context()) { |
| 10639 | flags |= 1 << 0; |
| 10640 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10641 | if (is_optimized) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10642 | flags |= 1 << 1; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10643 | flags |= inlined_jsframe_index << 2; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10644 | } |
| 10645 | details->set(kFrameDetailsFlagsIndex, Smi::FromInt(flags)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10646 | |
| 10647 | // Fill the dynamic part. |
| 10648 | int details_index = kFrameDetailsFirstDynamicIndex; |
| 10649 | |
| 10650 | // Add arguments name and value. |
| 10651 | for (int i = 0; i < argument_count; i++) { |
| 10652 | // Name of the argument. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10653 | if (i < scope_info->ParameterCount()) { |
| 10654 | details->set(details_index++, scope_info->ParameterName(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10655 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10656 | details->set(details_index++, heap->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10657 | } |
| 10658 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10659 | // Parameter value. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10660 | if (i < frame_inspector.GetParametersCount()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10661 | // Get the value from the stack. |
| 10662 | details->set(details_index++, frame_inspector.GetParameter(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10663 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10664 | details->set(details_index++, heap->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10665 | } |
| 10666 | } |
| 10667 | |
| 10668 | // Add locals name and value from the temporary copy from the function frame. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10669 | for (int i = 0; i < scope_info->LocalCount() * 2; i++) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10670 | details->set(details_index++, locals->get(i)); |
| 10671 | } |
| 10672 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10673 | // Add the value being returned. |
| 10674 | if (at_return) { |
| 10675 | details->set(details_index++, *return_value); |
| 10676 | } |
| 10677 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10678 | // Add the receiver (same as in function frame). |
| 10679 | // THIS MUST BE DONE LAST SINCE WE MIGHT ADVANCE |
| 10680 | // THE FRAME ITERATOR TO WRAP THE RECEIVER. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10681 | Handle<Object> receiver(it.frame()->receiver(), isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10682 | if (!receiver->IsJSObject() && |
| 10683 | shared->is_classic_mode() && |
| 10684 | !shared->native()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10685 | // If the receiver is not a JSObject and the function is not a |
| 10686 | // builtin or strict-mode we have hit an optimization where a |
| 10687 | // value object is not converted into a wrapped JS objects. To |
| 10688 | // hide this optimization from the debugger, we wrap the receiver |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10689 | // by creating correct wrapper object based on the calling frame's |
| 10690 | // global context. |
| 10691 | it.Advance(); |
| 10692 | Handle<Context> calling_frames_global_context( |
| 10693 | Context::cast(Context::cast(it.frame()->context())->global_context())); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10694 | receiver = |
| 10695 | isolate->factory()->ToObject(receiver, calling_frames_global_context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10696 | } |
| 10697 | details->set(kFrameDetailsReceiverIndex, *receiver); |
| 10698 | |
| 10699 | ASSERT_EQ(details_size, details_index); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10700 | return *isolate->factory()->NewJSArrayWithElements(details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10701 | } |
| 10702 | |
| 10703 | |
| 10704 | // Copy all the context locals into an object used to materialize a scope. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 10705 | static bool CopyContextLocalsToScopeObject( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10706 | Isolate* isolate, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10707 | Handle<ScopeInfo> scope_info, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 10708 | Handle<Context> context, |
| 10709 | Handle<JSObject> scope_object) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10710 | // Fill all context locals to the context extension. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10711 | for (int i = 0; i < scope_info->ContextLocalCount(); i++) { |
| 10712 | VariableMode mode; |
| 10713 | InitializationFlag init_flag; |
| 10714 | int context_index = scope_info->ContextSlotIndex( |
| 10715 | scope_info->ContextLocalName(i), &mode, &init_flag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10716 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10717 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 10718 | isolate, |
| 10719 | SetProperty(scope_object, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10720 | Handle<String>(scope_info->ContextLocalName(i)), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10721 | Handle<Object>(context->get(context_index), isolate), |
| 10722 | NONE, |
| 10723 | kNonStrictMode), |
| 10724 | false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10725 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 10726 | |
| 10727 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10728 | } |
| 10729 | |
| 10730 | |
| 10731 | // Create a plain JSObject which materializes the local scope for the specified |
| 10732 | // frame. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10733 | static Handle<JSObject> MaterializeLocalScopeWithFrameInspector( |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10734 | Isolate* isolate, |
| 10735 | JavaScriptFrame* frame, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10736 | FrameInspector* frame_inspector) { |
| 10737 | Handle<JSFunction> function(JSFunction::cast(frame_inspector->GetFunction())); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 10738 | Handle<SharedFunctionInfo> shared(function->shared()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10739 | Handle<ScopeInfo> scope_info(shared->scope_info()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10740 | |
| 10741 | // Allocate and initialize a JSObject with all the arguments, stack locals |
| 10742 | // heap locals and extension properties of the debugged function. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10743 | Handle<JSObject> local_scope = |
| 10744 | isolate->factory()->NewJSObject(isolate->object_function()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10745 | |
| 10746 | // First fill all parameters. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10747 | for (int i = 0; i < scope_info->ParameterCount(); ++i) { |
| 10748 | Handle<Object> value( |
| 10749 | i < frame_inspector->GetParametersCount() ? |
| 10750 | frame_inspector->GetParameter(i) : isolate->heap()->undefined_value()); |
| 10751 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 10752 | RETURN_IF_EMPTY_HANDLE_VALUE( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10753 | isolate, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 10754 | SetProperty(local_scope, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10755 | Handle<String>(scope_info->ParameterName(i)), |
| 10756 | value, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10757 | NONE, |
| 10758 | kNonStrictMode), |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 10759 | Handle<JSObject>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10760 | } |
| 10761 | |
| 10762 | // Second fill all stack locals. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10763 | for (int i = 0; i < scope_info->StackLocalCount(); ++i) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 10764 | RETURN_IF_EMPTY_HANDLE_VALUE( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10765 | isolate, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 10766 | SetProperty(local_scope, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10767 | Handle<String>(scope_info->StackLocalName(i)), |
| 10768 | Handle<Object>(frame_inspector->GetExpression(i)), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10769 | NONE, |
| 10770 | kNonStrictMode), |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 10771 | Handle<JSObject>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10772 | } |
| 10773 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10774 | if (scope_info->HasContext()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10775 | // Third fill all context locals. |
| 10776 | Handle<Context> frame_context(Context::cast(frame->context())); |
| 10777 | Handle<Context> function_context(frame_context->declaration_context()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10778 | if (!CopyContextLocalsToScopeObject( |
| 10779 | isolate, scope_info, function_context, local_scope)) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10780 | return Handle<JSObject>(); |
| 10781 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10782 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10783 | // Finally copy any properties from the function context extension. |
| 10784 | // These will be variables introduced by eval. |
| 10785 | if (function_context->closure() == *function) { |
| 10786 | if (function_context->has_extension() && |
| 10787 | !function_context->IsGlobalContext()) { |
| 10788 | Handle<JSObject> ext(JSObject::cast(function_context->extension())); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10789 | bool threw = false; |
| 10790 | Handle<FixedArray> keys = |
| 10791 | GetKeysInFixedArrayFor(ext, INCLUDE_PROTOS, &threw); |
| 10792 | if (threw) return Handle<JSObject>(); |
| 10793 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10794 | for (int i = 0; i < keys->length(); i++) { |
| 10795 | // Names of variables introduced by eval are strings. |
| 10796 | ASSERT(keys->get(i)->IsString()); |
| 10797 | Handle<String> key(String::cast(keys->get(i))); |
| 10798 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 10799 | isolate, |
| 10800 | SetProperty(local_scope, |
| 10801 | key, |
| 10802 | GetProperty(ext, key), |
| 10803 | NONE, |
| 10804 | kNonStrictMode), |
| 10805 | Handle<JSObject>()); |
| 10806 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10807 | } |
| 10808 | } |
| 10809 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10810 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10811 | return local_scope; |
| 10812 | } |
| 10813 | |
| 10814 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10815 | static Handle<JSObject> MaterializeLocalScope( |
| 10816 | Isolate* isolate, |
| 10817 | JavaScriptFrame* frame, |
| 10818 | int inlined_jsframe_index) { |
| 10819 | FrameInspector frame_inspector(frame, inlined_jsframe_index, isolate); |
| 10820 | return MaterializeLocalScopeWithFrameInspector(isolate, |
| 10821 | frame, |
| 10822 | &frame_inspector); |
| 10823 | } |
| 10824 | |
| 10825 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10826 | // Create a plain JSObject which materializes the closure content for the |
| 10827 | // context. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10828 | static Handle<JSObject> MaterializeClosure(Isolate* isolate, |
| 10829 | Handle<Context> context) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10830 | ASSERT(context->IsFunctionContext()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10831 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 10832 | Handle<SharedFunctionInfo> shared(context->closure()->shared()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10833 | Handle<ScopeInfo> scope_info(shared->scope_info()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10834 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10835 | // Allocate and initialize a JSObject with all the content of this function |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10836 | // closure. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10837 | Handle<JSObject> closure_scope = |
| 10838 | isolate->factory()->NewJSObject(isolate->object_function()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10839 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10840 | // Fill all context locals to the context extension. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10841 | if (!CopyContextLocalsToScopeObject( |
| 10842 | isolate, scope_info, context, closure_scope)) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 10843 | return Handle<JSObject>(); |
| 10844 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10845 | |
| 10846 | // Finally copy any properties from the function context extension. This will |
| 10847 | // be variables introduced by eval. |
| 10848 | if (context->has_extension()) { |
| 10849 | Handle<JSObject> ext(JSObject::cast(context->extension())); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10850 | bool threw = false; |
| 10851 | Handle<FixedArray> keys = |
| 10852 | GetKeysInFixedArrayFor(ext, INCLUDE_PROTOS, &threw); |
| 10853 | if (threw) return Handle<JSObject>(); |
| 10854 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10855 | for (int i = 0; i < keys->length(); i++) { |
| 10856 | // Names of variables introduced by eval are strings. |
| 10857 | ASSERT(keys->get(i)->IsString()); |
| 10858 | Handle<String> key(String::cast(keys->get(i))); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10859 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 10860 | isolate, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10861 | SetProperty(closure_scope, |
| 10862 | key, |
| 10863 | GetProperty(ext, key), |
| 10864 | NONE, |
| 10865 | kNonStrictMode), |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 10866 | Handle<JSObject>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10867 | } |
| 10868 | } |
| 10869 | |
| 10870 | return closure_scope; |
| 10871 | } |
| 10872 | |
| 10873 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10874 | // Create a plain JSObject which materializes the scope for the specified |
| 10875 | // catch context. |
| 10876 | static Handle<JSObject> MaterializeCatchScope(Isolate* isolate, |
| 10877 | Handle<Context> context) { |
| 10878 | ASSERT(context->IsCatchContext()); |
| 10879 | Handle<String> name(String::cast(context->extension())); |
| 10880 | Handle<Object> thrown_object(context->get(Context::THROWN_OBJECT_INDEX)); |
| 10881 | Handle<JSObject> catch_scope = |
| 10882 | isolate->factory()->NewJSObject(isolate->object_function()); |
| 10883 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 10884 | isolate, |
| 10885 | SetProperty(catch_scope, name, thrown_object, NONE, kNonStrictMode), |
| 10886 | Handle<JSObject>()); |
| 10887 | return catch_scope; |
| 10888 | } |
| 10889 | |
| 10890 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 10891 | // Create a plain JSObject which materializes the block scope for the specified |
| 10892 | // block context. |
| 10893 | static Handle<JSObject> MaterializeBlockScope( |
| 10894 | Isolate* isolate, |
| 10895 | Handle<Context> context) { |
| 10896 | ASSERT(context->IsBlockContext()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10897 | Handle<ScopeInfo> scope_info(ScopeInfo::cast(context->extension())); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 10898 | |
| 10899 | // Allocate and initialize a JSObject with all the arguments, stack locals |
| 10900 | // heap locals and extension properties of the debugged function. |
| 10901 | Handle<JSObject> block_scope = |
| 10902 | isolate->factory()->NewJSObject(isolate->object_function()); |
| 10903 | |
| 10904 | // Fill all context locals. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10905 | if (!CopyContextLocalsToScopeObject( |
| 10906 | isolate, scope_info, context, block_scope)) { |
| 10907 | return Handle<JSObject>(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 10908 | } |
| 10909 | |
| 10910 | return block_scope; |
| 10911 | } |
| 10912 | |
| 10913 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10914 | // Create a plain JSObject which materializes the module scope for the specified |
| 10915 | // module context. |
| 10916 | static Handle<JSObject> MaterializeModuleScope( |
| 10917 | Isolate* isolate, |
| 10918 | Handle<Context> context) { |
| 10919 | ASSERT(context->IsModuleContext()); |
| 10920 | Handle<ScopeInfo> scope_info(ScopeInfo::cast(context->extension())); |
| 10921 | |
| 10922 | // Allocate and initialize a JSObject with all the members of the debugged |
| 10923 | // module. |
| 10924 | Handle<JSObject> module_scope = |
| 10925 | isolate->factory()->NewJSObject(isolate->object_function()); |
| 10926 | |
| 10927 | // Fill all context locals. |
| 10928 | if (!CopyContextLocalsToScopeObject( |
| 10929 | isolate, scope_info, context, module_scope)) { |
| 10930 | return Handle<JSObject>(); |
| 10931 | } |
| 10932 | |
| 10933 | return module_scope; |
| 10934 | } |
| 10935 | |
| 10936 | |
| 10937 | // Iterate over the actual scopes visible from a stack frame. The iteration |
| 10938 | // proceeds from the innermost visible nested scope outwards. All scopes are |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10939 | // backed by an actual context except the local scope, which is inserted |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10940 | // "artificially" in the context chain. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10941 | class ScopeIterator { |
| 10942 | public: |
| 10943 | enum ScopeType { |
| 10944 | ScopeTypeGlobal = 0, |
| 10945 | ScopeTypeLocal, |
| 10946 | ScopeTypeWith, |
| 10947 | ScopeTypeClosure, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 10948 | ScopeTypeCatch, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10949 | ScopeTypeBlock, |
| 10950 | ScopeTypeModule |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10951 | }; |
| 10952 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10953 | ScopeIterator(Isolate* isolate, |
| 10954 | JavaScriptFrame* frame, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10955 | int inlined_jsframe_index) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10956 | : isolate_(isolate), |
| 10957 | frame_(frame), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10958 | inlined_jsframe_index_(inlined_jsframe_index), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10959 | function_(JSFunction::cast(frame->function())), |
| 10960 | context_(Context::cast(frame->context())), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10961 | nested_scope_chain_(4) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10962 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 10963 | // Catch the case when the debugger stops in an internal function. |
| 10964 | Handle<SharedFunctionInfo> shared_info(function_->shared()); |
| 10965 | Handle<ScopeInfo> scope_info(shared_info->scope_info()); |
| 10966 | if (shared_info->script() == isolate->heap()->undefined_value()) { |
| 10967 | while (context_->closure() == *function_) { |
| 10968 | context_ = Handle<Context>(context_->previous(), isolate_); |
| 10969 | } |
| 10970 | return; |
| 10971 | } |
| 10972 | |
| 10973 | // Get the debug info (create it if it does not exist). |
| 10974 | if (!isolate->debug()->EnsureDebugInfo(shared_info)) { |
| 10975 | // Return if ensuring debug info failed. |
| 10976 | return; |
| 10977 | } |
| 10978 | Handle<DebugInfo> debug_info = Debug::GetDebugInfo(shared_info); |
| 10979 | |
| 10980 | // Find the break point where execution has stopped. |
| 10981 | BreakLocationIterator break_location_iterator(debug_info, |
| 10982 | ALL_BREAK_LOCATIONS); |
| 10983 | break_location_iterator.FindBreakLocationFromAddress(frame->pc()); |
| 10984 | if (break_location_iterator.IsExit()) { |
| 10985 | // We are within the return sequence. At the momemt it is not possible to |
| 10986 | // get a source position which is consistent with the current scope chain. |
| 10987 | // Thus all nested with, catch and block contexts are skipped and we only |
| 10988 | // provide the function scope. |
| 10989 | if (scope_info->HasContext()) { |
| 10990 | context_ = Handle<Context>(context_->declaration_context(), isolate_); |
| 10991 | } else { |
| 10992 | while (context_->closure() == *function_) { |
| 10993 | context_ = Handle<Context>(context_->previous(), isolate_); |
| 10994 | } |
| 10995 | } |
| 10996 | if (scope_info->Type() != EVAL_SCOPE) nested_scope_chain_.Add(scope_info); |
| 10997 | } else { |
| 10998 | // Reparse the code and analyze the scopes. |
| 10999 | ZoneScope zone_scope(isolate, DELETE_ON_EXIT); |
| 11000 | Handle<Script> script(Script::cast(shared_info->script())); |
| 11001 | Scope* scope = NULL; |
| 11002 | |
| 11003 | // Check whether we are in global, eval or function code. |
| 11004 | Handle<ScopeInfo> scope_info(shared_info->scope_info()); |
| 11005 | if (scope_info->Type() != FUNCTION_SCOPE) { |
| 11006 | // Global or eval code. |
| 11007 | CompilationInfo info(script); |
| 11008 | if (scope_info->Type() == GLOBAL_SCOPE) { |
| 11009 | info.MarkAsGlobal(); |
| 11010 | } else { |
| 11011 | ASSERT(scope_info->Type() == EVAL_SCOPE); |
| 11012 | info.MarkAsEval(); |
| 11013 | info.SetCallingContext(Handle<Context>(function_->context())); |
| 11014 | } |
| 11015 | if (ParserApi::Parse(&info, kNoParsingFlags) && Scope::Analyze(&info)) { |
| 11016 | scope = info.function()->scope(); |
| 11017 | } |
| 11018 | } else { |
| 11019 | // Function code |
| 11020 | CompilationInfo info(shared_info); |
| 11021 | if (ParserApi::Parse(&info, kNoParsingFlags) && Scope::Analyze(&info)) { |
| 11022 | scope = info.function()->scope(); |
| 11023 | } |
| 11024 | } |
| 11025 | |
| 11026 | // Retrieve the scope chain for the current position. |
| 11027 | if (scope != NULL) { |
| 11028 | int source_position = shared_info->code()->SourcePosition(frame_->pc()); |
| 11029 | scope->GetNestedScopeChain(&nested_scope_chain_, source_position); |
| 11030 | } else { |
| 11031 | // A failed reparse indicates that the preparser has diverged from the |
| 11032 | // parser or that the preparse data given to the initial parse has been |
| 11033 | // faulty. We fail in debug mode but in release mode we only provide the |
| 11034 | // information we get from the context chain but nothing about |
| 11035 | // completely stack allocated scopes or stack allocated locals. |
| 11036 | UNREACHABLE(); |
| 11037 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11038 | } |
| 11039 | } |
| 11040 | |
| 11041 | // More scopes? |
| 11042 | bool Done() { return context_.is_null(); } |
| 11043 | |
| 11044 | // Move to the next scope. |
| 11045 | void Next() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11046 | ScopeType scope_type = Type(); |
| 11047 | if (scope_type == ScopeTypeGlobal) { |
| 11048 | // The global scope is always the last in the chain. |
| 11049 | ASSERT(context_->IsGlobalContext()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11050 | context_ = Handle<Context>(); |
| 11051 | return; |
| 11052 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11053 | if (nested_scope_chain_.is_empty()) { |
| 11054 | context_ = Handle<Context>(context_->previous(), isolate_); |
| 11055 | } else { |
| 11056 | if (nested_scope_chain_.last()->HasContext()) { |
| 11057 | ASSERT(context_->previous() != NULL); |
| 11058 | context_ = Handle<Context>(context_->previous(), isolate_); |
| 11059 | } |
| 11060 | nested_scope_chain_.RemoveLast(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11061 | } |
| 11062 | } |
| 11063 | |
| 11064 | // Return the type of the current scope. |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 11065 | ScopeType Type() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11066 | if (!nested_scope_chain_.is_empty()) { |
| 11067 | Handle<ScopeInfo> scope_info = nested_scope_chain_.last(); |
| 11068 | switch (scope_info->Type()) { |
| 11069 | case FUNCTION_SCOPE: |
| 11070 | ASSERT(context_->IsFunctionContext() || |
| 11071 | !scope_info->HasContext()); |
| 11072 | return ScopeTypeLocal; |
| 11073 | case MODULE_SCOPE: |
| 11074 | ASSERT(context_->IsModuleContext()); |
| 11075 | return ScopeTypeModule; |
| 11076 | case GLOBAL_SCOPE: |
| 11077 | ASSERT(context_->IsGlobalContext()); |
| 11078 | return ScopeTypeGlobal; |
| 11079 | case WITH_SCOPE: |
| 11080 | ASSERT(context_->IsWithContext()); |
| 11081 | return ScopeTypeWith; |
| 11082 | case CATCH_SCOPE: |
| 11083 | ASSERT(context_->IsCatchContext()); |
| 11084 | return ScopeTypeCatch; |
| 11085 | case BLOCK_SCOPE: |
| 11086 | ASSERT(!scope_info->HasContext() || |
| 11087 | context_->IsBlockContext()); |
| 11088 | return ScopeTypeBlock; |
| 11089 | case EVAL_SCOPE: |
| 11090 | UNREACHABLE(); |
| 11091 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11092 | } |
| 11093 | if (context_->IsGlobalContext()) { |
| 11094 | ASSERT(context_->global()->IsGlobalObject()); |
| 11095 | return ScopeTypeGlobal; |
| 11096 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11097 | if (context_->IsFunctionContext()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11098 | return ScopeTypeClosure; |
| 11099 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11100 | if (context_->IsCatchContext()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11101 | return ScopeTypeCatch; |
| 11102 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11103 | if (context_->IsBlockContext()) { |
| 11104 | return ScopeTypeBlock; |
| 11105 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11106 | if (context_->IsModuleContext()) { |
| 11107 | return ScopeTypeModule; |
| 11108 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11109 | ASSERT(context_->IsWithContext()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11110 | return ScopeTypeWith; |
| 11111 | } |
| 11112 | |
| 11113 | // Return the JavaScript object with the content of the current scope. |
| 11114 | Handle<JSObject> ScopeObject() { |
| 11115 | switch (Type()) { |
| 11116 | case ScopeIterator::ScopeTypeGlobal: |
| 11117 | return Handle<JSObject>(CurrentContext()->global()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11118 | case ScopeIterator::ScopeTypeLocal: |
| 11119 | // Materialize the content of the local scope into a JSObject. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11120 | ASSERT(nested_scope_chain_.length() == 1); |
| 11121 | return MaterializeLocalScope(isolate_, frame_, inlined_jsframe_index_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11122 | case ScopeIterator::ScopeTypeWith: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11123 | // Return the with object. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11124 | return Handle<JSObject>(JSObject::cast(CurrentContext()->extension())); |
| 11125 | case ScopeIterator::ScopeTypeCatch: |
| 11126 | return MaterializeCatchScope(isolate_, CurrentContext()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11127 | case ScopeIterator::ScopeTypeClosure: |
| 11128 | // Materialize the content of the closure scope into a JSObject. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11129 | return MaterializeClosure(isolate_, CurrentContext()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11130 | case ScopeIterator::ScopeTypeBlock: |
| 11131 | return MaterializeBlockScope(isolate_, CurrentContext()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11132 | case ScopeIterator::ScopeTypeModule: |
| 11133 | return MaterializeModuleScope(isolate_, CurrentContext()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11134 | } |
| 11135 | UNREACHABLE(); |
| 11136 | return Handle<JSObject>(); |
| 11137 | } |
| 11138 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11139 | Handle<ScopeInfo> CurrentScopeInfo() { |
| 11140 | if (!nested_scope_chain_.is_empty()) { |
| 11141 | return nested_scope_chain_.last(); |
| 11142 | } else if (context_->IsBlockContext()) { |
| 11143 | return Handle<ScopeInfo>(ScopeInfo::cast(context_->extension())); |
| 11144 | } else if (context_->IsFunctionContext()) { |
| 11145 | return Handle<ScopeInfo>(context_->closure()->shared()->scope_info()); |
| 11146 | } |
| 11147 | return Handle<ScopeInfo>::null(); |
| 11148 | } |
| 11149 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11150 | // Return the context for this scope. For the local context there might not |
| 11151 | // be an actual context. |
| 11152 | Handle<Context> CurrentContext() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11153 | if (Type() == ScopeTypeGlobal || |
| 11154 | nested_scope_chain_.is_empty()) { |
| 11155 | return context_; |
| 11156 | } else if (nested_scope_chain_.last()->HasContext()) { |
| 11157 | return context_; |
| 11158 | } else { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11159 | return Handle<Context>(); |
| 11160 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11161 | } |
| 11162 | |
| 11163 | #ifdef DEBUG |
| 11164 | // Debug print of the content of the current scope. |
| 11165 | void DebugPrint() { |
| 11166 | switch (Type()) { |
| 11167 | case ScopeIterator::ScopeTypeGlobal: |
| 11168 | PrintF("Global:\n"); |
| 11169 | CurrentContext()->Print(); |
| 11170 | break; |
| 11171 | |
| 11172 | case ScopeIterator::ScopeTypeLocal: { |
| 11173 | PrintF("Local:\n"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11174 | function_->shared()->scope_info()->Print(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11175 | if (!CurrentContext().is_null()) { |
| 11176 | CurrentContext()->Print(); |
| 11177 | if (CurrentContext()->has_extension()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11178 | Handle<Object> extension(CurrentContext()->extension()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11179 | if (extension->IsJSContextExtensionObject()) { |
| 11180 | extension->Print(); |
| 11181 | } |
| 11182 | } |
| 11183 | } |
| 11184 | break; |
| 11185 | } |
| 11186 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11187 | case ScopeIterator::ScopeTypeWith: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11188 | PrintF("With:\n"); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11189 | CurrentContext()->extension()->Print(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11190 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11191 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11192 | case ScopeIterator::ScopeTypeCatch: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11193 | PrintF("Catch:\n"); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11194 | CurrentContext()->extension()->Print(); |
| 11195 | CurrentContext()->get(Context::THROWN_OBJECT_INDEX)->Print(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11196 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11197 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11198 | case ScopeIterator::ScopeTypeClosure: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11199 | PrintF("Closure:\n"); |
| 11200 | CurrentContext()->Print(); |
| 11201 | if (CurrentContext()->has_extension()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11202 | Handle<Object> extension(CurrentContext()->extension()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11203 | if (extension->IsJSContextExtensionObject()) { |
| 11204 | extension->Print(); |
| 11205 | } |
| 11206 | } |
| 11207 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11208 | |
| 11209 | default: |
| 11210 | UNREACHABLE(); |
| 11211 | } |
| 11212 | PrintF("\n"); |
| 11213 | } |
| 11214 | #endif |
| 11215 | |
| 11216 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11217 | Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11218 | JavaScriptFrame* frame_; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11219 | int inlined_jsframe_index_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11220 | Handle<JSFunction> function_; |
| 11221 | Handle<Context> context_; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11222 | List<Handle<ScopeInfo> > nested_scope_chain_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11223 | |
| 11224 | DISALLOW_IMPLICIT_CONSTRUCTORS(ScopeIterator); |
| 11225 | }; |
| 11226 | |
| 11227 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11228 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetScopeCount) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11229 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11230 | ASSERT(args.length() == 2); |
| 11231 | |
| 11232 | // Check arguments. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11233 | Object* check; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11234 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 11235 | RUNTIME_ARGUMENTS(isolate, args)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11236 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 11237 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11238 | CONVERT_SMI_ARG_CHECKED(wrapped_id, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11239 | |
| 11240 | // Get the frame where the debugging is performed. |
| 11241 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11242 | JavaScriptFrameIterator it(isolate, id); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11243 | JavaScriptFrame* frame = it.frame(); |
| 11244 | |
| 11245 | // Count the visible scopes. |
| 11246 | int n = 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11247 | for (ScopeIterator it(isolate, frame, 0); |
| 11248 | !it.Done(); |
| 11249 | it.Next()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11250 | n++; |
| 11251 | } |
| 11252 | |
| 11253 | return Smi::FromInt(n); |
| 11254 | } |
| 11255 | |
| 11256 | |
| 11257 | static const int kScopeDetailsTypeIndex = 0; |
| 11258 | static const int kScopeDetailsObjectIndex = 1; |
| 11259 | static const int kScopeDetailsSize = 2; |
| 11260 | |
| 11261 | // Return an array with scope details |
| 11262 | // args[0]: number: break id |
| 11263 | // args[1]: number: frame index |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11264 | // args[2]: number: inlined frame index |
| 11265 | // args[3]: number: scope index |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11266 | // |
| 11267 | // The array returned contains the following information: |
| 11268 | // 0: Scope type |
| 11269 | // 1: Scope object |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11270 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetScopeDetails) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11271 | HandleScope scope(isolate); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11272 | ASSERT(args.length() == 4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11273 | |
| 11274 | // Check arguments. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11275 | Object* check; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11276 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 11277 | RUNTIME_ARGUMENTS(isolate, args)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11278 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 11279 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11280 | CONVERT_SMI_ARG_CHECKED(wrapped_id, 1); |
| 11281 | CONVERT_NUMBER_CHECKED(int, inlined_jsframe_index, Int32, args[2]); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11282 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[3]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11283 | |
| 11284 | // Get the frame where the debugging is performed. |
| 11285 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11286 | JavaScriptFrameIterator frame_it(isolate, id); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11287 | JavaScriptFrame* frame = frame_it.frame(); |
| 11288 | |
| 11289 | // Find the requested scope. |
| 11290 | int n = 0; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11291 | ScopeIterator it(isolate, frame, inlined_jsframe_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11292 | for (; !it.Done() && n < index; it.Next()) { |
| 11293 | n++; |
| 11294 | } |
| 11295 | if (it.Done()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11296 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11297 | } |
| 11298 | |
| 11299 | // Calculate the size of the result. |
| 11300 | int details_size = kScopeDetailsSize; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11301 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(details_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11302 | |
| 11303 | // Fill in scope details. |
| 11304 | details->set(kScopeDetailsTypeIndex, Smi::FromInt(it.Type())); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 11305 | Handle<JSObject> scope_object = it.ScopeObject(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11306 | RETURN_IF_EMPTY_HANDLE(isolate, scope_object); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 11307 | details->set(kScopeDetailsObjectIndex, *scope_object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11308 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11309 | return *isolate->factory()->NewJSArrayWithElements(details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11310 | } |
| 11311 | |
| 11312 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11313 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPrintScopes) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11314 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11315 | ASSERT(args.length() == 0); |
| 11316 | |
| 11317 | #ifdef DEBUG |
| 11318 | // Print the scopes for the top frame. |
| 11319 | StackFrameLocator locator; |
| 11320 | JavaScriptFrame* frame = locator.FindJavaScriptFrame(0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11321 | for (ScopeIterator it(isolate, frame, 0); |
| 11322 | !it.Done(); |
| 11323 | it.Next()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11324 | it.DebugPrint(); |
| 11325 | } |
| 11326 | #endif |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11327 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11328 | } |
| 11329 | |
| 11330 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11331 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetThreadCount) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11332 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11333 | ASSERT(args.length() == 1); |
| 11334 | |
| 11335 | // Check arguments. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11336 | Object* result; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11337 | { MaybeObject* maybe_result = Runtime_CheckExecutionState( |
| 11338 | RUNTIME_ARGUMENTS(isolate, args)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11339 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 11340 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11341 | |
| 11342 | // Count all archived V8 threads. |
| 11343 | int n = 0; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11344 | for (ThreadState* thread = |
| 11345 | isolate->thread_manager()->FirstThreadStateInUse(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11346 | thread != NULL; |
| 11347 | thread = thread->Next()) { |
| 11348 | n++; |
| 11349 | } |
| 11350 | |
| 11351 | // Total number of threads is current thread and archived threads. |
| 11352 | return Smi::FromInt(n + 1); |
| 11353 | } |
| 11354 | |
| 11355 | |
| 11356 | static const int kThreadDetailsCurrentThreadIndex = 0; |
| 11357 | static const int kThreadDetailsThreadIdIndex = 1; |
| 11358 | static const int kThreadDetailsSize = 2; |
| 11359 | |
| 11360 | // Return an array with thread details |
| 11361 | // args[0]: number: break id |
| 11362 | // args[1]: number: thread index |
| 11363 | // |
| 11364 | // The array returned contains the following information: |
| 11365 | // 0: Is current thread? |
| 11366 | // 1: Thread id |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11367 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetThreadDetails) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11368 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11369 | ASSERT(args.length() == 2); |
| 11370 | |
| 11371 | // Check arguments. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11372 | Object* check; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11373 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 11374 | RUNTIME_ARGUMENTS(isolate, args)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11375 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 11376 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11377 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[1]); |
| 11378 | |
| 11379 | // Allocate array for result. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11380 | Handle<FixedArray> details = |
| 11381 | isolate->factory()->NewFixedArray(kThreadDetailsSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11382 | |
| 11383 | // Thread index 0 is current thread. |
| 11384 | if (index == 0) { |
| 11385 | // Fill the details. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11386 | details->set(kThreadDetailsCurrentThreadIndex, |
| 11387 | isolate->heap()->true_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11388 | details->set(kThreadDetailsThreadIdIndex, |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11389 | Smi::FromInt(ThreadId::Current().ToInteger())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11390 | } else { |
| 11391 | // Find the thread with the requested index. |
| 11392 | int n = 1; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11393 | ThreadState* thread = |
| 11394 | isolate->thread_manager()->FirstThreadStateInUse(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11395 | while (index != n && thread != NULL) { |
| 11396 | thread = thread->Next(); |
| 11397 | n++; |
| 11398 | } |
| 11399 | if (thread == NULL) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11400 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11401 | } |
| 11402 | |
| 11403 | // Fill the details. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11404 | details->set(kThreadDetailsCurrentThreadIndex, |
| 11405 | isolate->heap()->false_value()); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11406 | details->set(kThreadDetailsThreadIdIndex, |
| 11407 | Smi::FromInt(thread->id().ToInteger())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11408 | } |
| 11409 | |
| 11410 | // Convert to JS array and return. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11411 | return *isolate->factory()->NewJSArrayWithElements(details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11412 | } |
| 11413 | |
| 11414 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 11415 | // Sets the disable break state |
| 11416 | // args[0]: disable break state |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11417 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetDisableBreak) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11418 | HandleScope scope(isolate); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 11419 | ASSERT(args.length() == 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11420 | CONVERT_BOOLEAN_ARG_CHECKED(disable_break, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11421 | isolate->debug()->set_disable_break(disable_break); |
| 11422 | return isolate->heap()->undefined_value(); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 11423 | } |
| 11424 | |
| 11425 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11426 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetBreakLocations) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11427 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11428 | ASSERT(args.length() == 1); |
| 11429 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11430 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, fun, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11431 | Handle<SharedFunctionInfo> shared(fun->shared()); |
| 11432 | // Find the number of break points |
| 11433 | Handle<Object> break_locations = Debug::GetSourceBreakLocations(shared); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11434 | if (break_locations->IsUndefined()) return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11435 | // Return array as JS array |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11436 | return *isolate->factory()->NewJSArrayWithElements( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11437 | Handle<FixedArray>::cast(break_locations)); |
| 11438 | } |
| 11439 | |
| 11440 | |
| 11441 | // Set a break point in a function |
| 11442 | // args[0]: function |
| 11443 | // args[1]: number: break source position (within the function source) |
| 11444 | // args[2]: number: break point object |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11445 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetFunctionBreakPoint) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11446 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11447 | ASSERT(args.length() == 3); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11448 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, fun, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11449 | Handle<SharedFunctionInfo> shared(fun->shared()); |
| 11450 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 11451 | RUNTIME_ASSERT(source_position >= 0); |
| 11452 | Handle<Object> break_point_object_arg = args.at<Object>(2); |
| 11453 | |
| 11454 | // Set break point. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11455 | isolate->debug()->SetBreakPoint(shared, break_point_object_arg, |
| 11456 | &source_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11457 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 11458 | return Smi::FromInt(source_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11459 | } |
| 11460 | |
| 11461 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11462 | Object* Runtime::FindSharedFunctionInfoInScript(Isolate* isolate, |
| 11463 | Handle<Script> script, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11464 | int position) { |
| 11465 | // Iterate the heap looking for SharedFunctionInfo generated from the |
| 11466 | // script. The inner most SharedFunctionInfo containing the source position |
| 11467 | // for the requested break point is found. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 11468 | // NOTE: This might require several heap iterations. If the SharedFunctionInfo |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11469 | // which is found is not compiled it is compiled and the heap is iterated |
| 11470 | // again as the compilation might create inner functions from the newly |
| 11471 | // compiled function and the actual requested break point might be in one of |
| 11472 | // these functions. |
| 11473 | bool done = false; |
| 11474 | // The current candidate for the source position: |
| 11475 | int target_start_position = RelocInfo::kNoPosition; |
| 11476 | Handle<SharedFunctionInfo> target; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11477 | while (!done) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11478 | { // Extra scope for iterator and no-allocation. |
| 11479 | isolate->heap()->EnsureHeapIsIterable(); |
| 11480 | AssertNoAllocation no_alloc_during_heap_iteration; |
| 11481 | HeapIterator iterator; |
| 11482 | for (HeapObject* obj = iterator.next(); |
| 11483 | obj != NULL; obj = iterator.next()) { |
| 11484 | if (obj->IsSharedFunctionInfo()) { |
| 11485 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(obj)); |
| 11486 | if (shared->script() == *script) { |
| 11487 | // If the SharedFunctionInfo found has the requested script data and |
| 11488 | // contains the source position it is a candidate. |
| 11489 | int start_position = shared->function_token_position(); |
| 11490 | if (start_position == RelocInfo::kNoPosition) { |
| 11491 | start_position = shared->start_position(); |
| 11492 | } |
| 11493 | if (start_position <= position && |
| 11494 | position <= shared->end_position()) { |
| 11495 | // If there is no candidate or this function is within the current |
| 11496 | // candidate this is the new candidate. |
| 11497 | if (target.is_null()) { |
| 11498 | target_start_position = start_position; |
| 11499 | target = shared; |
| 11500 | } else { |
| 11501 | if (target_start_position == start_position && |
| 11502 | shared->end_position() == target->end_position()) { |
| 11503 | // If a top-level function contain only one function |
| 11504 | // declartion the source for the top-level and the |
| 11505 | // function is the same. In that case prefer the non |
| 11506 | // top-level function. |
| 11507 | if (!shared->is_toplevel()) { |
| 11508 | target_start_position = start_position; |
| 11509 | target = shared; |
| 11510 | } |
| 11511 | } else if (target_start_position <= start_position && |
| 11512 | shared->end_position() <= target->end_position()) { |
| 11513 | // This containment check includes equality as a function |
| 11514 | // inside a top-level function can share either start or end |
| 11515 | // position with the top-level function. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11516 | target_start_position = start_position; |
| 11517 | target = shared; |
| 11518 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11519 | } |
| 11520 | } |
| 11521 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11522 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11523 | } // End for loop. |
| 11524 | } // End No allocation scope. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11525 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11526 | if (target.is_null()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11527 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11528 | } |
| 11529 | |
| 11530 | // If the candidate found is compiled we are done. NOTE: when lazy |
| 11531 | // compilation of inner functions is introduced some additional checking |
| 11532 | // needs to be done here to compile inner functions. |
| 11533 | done = target->is_compiled(); |
| 11534 | if (!done) { |
| 11535 | // If the candidate is not compiled compile it to reveal any inner |
| 11536 | // functions which might contain the requested source position. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11537 | SharedFunctionInfo::CompileLazy(target, KEEP_EXCEPTION); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11538 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11539 | } // End while loop. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11540 | |
| 11541 | return *target; |
| 11542 | } |
| 11543 | |
| 11544 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 11545 | // Changes the state of a break point in a script and returns source position |
| 11546 | // where break point was set. NOTE: Regarding performance see the NOTE for |
| 11547 | // GetScriptFromScriptData. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11548 | // args[0]: script to set break point in |
| 11549 | // args[1]: number: break source position (within the script source) |
| 11550 | // args[2]: number: break point object |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11551 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetScriptBreakPoint) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11552 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11553 | ASSERT(args.length() == 3); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11554 | CONVERT_ARG_HANDLE_CHECKED(JSValue, wrapper, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11555 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 11556 | RUNTIME_ASSERT(source_position >= 0); |
| 11557 | Handle<Object> break_point_object_arg = args.at<Object>(2); |
| 11558 | |
| 11559 | // Get the script from the script wrapper. |
| 11560 | RUNTIME_ASSERT(wrapper->value()->IsScript()); |
| 11561 | Handle<Script> script(Script::cast(wrapper->value())); |
| 11562 | |
| 11563 | Object* result = Runtime::FindSharedFunctionInfoInScript( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11564 | isolate, script, source_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11565 | if (!result->IsUndefined()) { |
| 11566 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result)); |
| 11567 | // Find position within function. The script position might be before the |
| 11568 | // source position of the first function. |
| 11569 | int position; |
| 11570 | if (shared->start_position() > source_position) { |
| 11571 | position = 0; |
| 11572 | } else { |
| 11573 | position = source_position - shared->start_position(); |
| 11574 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11575 | isolate->debug()->SetBreakPoint(shared, break_point_object_arg, &position); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 11576 | position += shared->start_position(); |
| 11577 | return Smi::FromInt(position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11578 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11579 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11580 | } |
| 11581 | |
| 11582 | |
| 11583 | // Clear a break point |
| 11584 | // args[0]: number: break point object |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11585 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ClearBreakPoint) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11586 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11587 | ASSERT(args.length() == 1); |
| 11588 | Handle<Object> break_point_object_arg = args.at<Object>(0); |
| 11589 | |
| 11590 | // Clear break point. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11591 | isolate->debug()->ClearBreakPoint(break_point_object_arg); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11592 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11593 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11594 | } |
| 11595 | |
| 11596 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 11597 | // Change the state of break on exceptions. |
| 11598 | // args[0]: Enum value indicating whether to affect caught/uncaught exceptions. |
| 11599 | // args[1]: Boolean indicating on/off. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11600 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ChangeBreakOnException) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11601 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11602 | ASSERT(args.length() == 2); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 11603 | RUNTIME_ASSERT(args[0]->IsNumber()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11604 | CONVERT_BOOLEAN_ARG_CHECKED(enable, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11605 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 11606 | // If the number doesn't match an enum value, the ChangeBreakOnException |
| 11607 | // function will default to affecting caught exceptions. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11608 | ExceptionBreakType type = |
| 11609 | static_cast<ExceptionBreakType>(NumberToUint32(args[0])); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 11610 | // Update break point state. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11611 | isolate->debug()->ChangeBreakOnException(type, enable); |
| 11612 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11613 | } |
| 11614 | |
| 11615 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 11616 | // Returns the state of break on exceptions |
| 11617 | // args[0]: boolean indicating uncaught exceptions |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11618 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsBreakOnException) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11619 | HandleScope scope(isolate); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 11620 | ASSERT(args.length() == 1); |
| 11621 | RUNTIME_ASSERT(args[0]->IsNumber()); |
| 11622 | |
| 11623 | ExceptionBreakType type = |
| 11624 | static_cast<ExceptionBreakType>(NumberToUint32(args[0])); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11625 | bool result = isolate->debug()->IsBreakOnException(type); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 11626 | return Smi::FromInt(result); |
| 11627 | } |
| 11628 | |
| 11629 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11630 | // Prepare for stepping |
| 11631 | // args[0]: break id for checking execution state |
| 11632 | // args[1]: step action from the enumeration StepAction |
| 11633 | // args[2]: number of times to perform the step, for step out it is the number |
| 11634 | // of frames to step down. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11635 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PrepareStep) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11636 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11637 | ASSERT(args.length() == 3); |
| 11638 | // Check arguments. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11639 | Object* check; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11640 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 11641 | RUNTIME_ARGUMENTS(isolate, args)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11642 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 11643 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11644 | if (!args[1]->IsNumber() || !args[2]->IsNumber()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11645 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11646 | } |
| 11647 | |
| 11648 | // Get the step action and check validity. |
| 11649 | StepAction step_action = static_cast<StepAction>(NumberToInt32(args[1])); |
| 11650 | if (step_action != StepIn && |
| 11651 | step_action != StepNext && |
| 11652 | step_action != StepOut && |
| 11653 | step_action != StepInMin && |
| 11654 | step_action != StepMin) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11655 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11656 | } |
| 11657 | |
| 11658 | // Get the number of steps. |
| 11659 | int step_count = NumberToInt32(args[2]); |
| 11660 | if (step_count < 1) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11661 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11662 | } |
| 11663 | |
| 11664 | // Clear all current stepping setup. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11665 | isolate->debug()->ClearStepping(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11666 | |
| 11667 | // Prepare step. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11668 | isolate->debug()->PrepareStep(static_cast<StepAction>(step_action), |
| 11669 | step_count); |
| 11670 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11671 | } |
| 11672 | |
| 11673 | |
| 11674 | // Clear all stepping set by PrepareStep. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11675 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ClearStepping) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11676 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11677 | ASSERT(args.length() == 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11678 | isolate->debug()->ClearStepping(); |
| 11679 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11680 | } |
| 11681 | |
| 11682 | |
| 11683 | // Creates a copy of the with context chain. The copy of the context chain is |
| 11684 | // is linked to the function context supplied. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11685 | static Handle<Context> CopyNestedScopeContextChain(Isolate* isolate, |
| 11686 | Handle<JSFunction> function, |
| 11687 | Handle<Context> base, |
| 11688 | JavaScriptFrame* frame, |
| 11689 | int inlined_jsframe_index) { |
| 11690 | HandleScope scope(isolate); |
| 11691 | List<Handle<ScopeInfo> > scope_chain; |
| 11692 | List<Handle<Context> > context_chain; |
| 11693 | |
| 11694 | ScopeIterator it(isolate, frame, inlined_jsframe_index); |
| 11695 | for (; it.Type() != ScopeIterator::ScopeTypeGlobal && |
| 11696 | it.Type() != ScopeIterator::ScopeTypeLocal ; it.Next()) { |
| 11697 | ASSERT(!it.Done()); |
| 11698 | scope_chain.Add(it.CurrentScopeInfo()); |
| 11699 | context_chain.Add(it.CurrentContext()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11700 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11701 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11702 | // At the end of the chain. Return the base context to link to. |
| 11703 | Handle<Context> context = base; |
| 11704 | |
| 11705 | // Iteratively copy and or materialize the nested contexts. |
| 11706 | while (!scope_chain.is_empty()) { |
| 11707 | Handle<ScopeInfo> scope_info = scope_chain.RemoveLast(); |
| 11708 | Handle<Context> current = context_chain.RemoveLast(); |
| 11709 | ASSERT(!(scope_info->HasContext() & current.is_null())); |
| 11710 | |
| 11711 | if (scope_info->Type() == CATCH_SCOPE) { |
| 11712 | Handle<String> name(String::cast(current->extension())); |
| 11713 | Handle<Object> thrown_object(current->get(Context::THROWN_OBJECT_INDEX)); |
| 11714 | context = |
| 11715 | isolate->factory()->NewCatchContext(function, |
| 11716 | context, |
| 11717 | name, |
| 11718 | thrown_object); |
| 11719 | } else if (scope_info->Type() == BLOCK_SCOPE) { |
| 11720 | // Materialize the contents of the block scope into a JSObject. |
| 11721 | Handle<JSObject> block_scope_object = |
| 11722 | MaterializeBlockScope(isolate, current); |
| 11723 | if (block_scope_object.is_null()) { |
| 11724 | return Handle<Context>::null(); |
| 11725 | } |
| 11726 | // Allocate a new function context for the debug evaluation and set the |
| 11727 | // extension object. |
| 11728 | Handle<Context> new_context = |
| 11729 | isolate->factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, |
| 11730 | function); |
| 11731 | new_context->set_extension(*block_scope_object); |
| 11732 | new_context->set_previous(*context); |
| 11733 | context = new_context; |
| 11734 | } else { |
| 11735 | ASSERT(scope_info->Type() == WITH_SCOPE); |
| 11736 | ASSERT(current->IsWithContext()); |
| 11737 | Handle<JSObject> extension(JSObject::cast(current->extension())); |
| 11738 | context = |
| 11739 | isolate->factory()->NewWithContext(function, context, extension); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 11740 | } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 11741 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11742 | |
| 11743 | return scope.CloseAndEscape(context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11744 | } |
| 11745 | |
| 11746 | |
| 11747 | // Helper function to find or create the arguments object for |
| 11748 | // Runtime_DebugEvaluate. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11749 | static Handle<Object> GetArgumentsObject(Isolate* isolate, |
| 11750 | JavaScriptFrame* frame, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11751 | FrameInspector* frame_inspector, |
| 11752 | Handle<ScopeInfo> scope_info, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11753 | Handle<Context> function_context) { |
| 11754 | // Try to find the value of 'arguments' to pass as parameter. If it is not |
| 11755 | // found (that is the debugged function does not reference 'arguments' and |
| 11756 | // does not support eval) then create an 'arguments' object. |
| 11757 | int index; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11758 | if (scope_info->StackLocalCount() > 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11759 | index = scope_info->StackSlotIndex(isolate->heap()->arguments_symbol()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11760 | if (index != -1) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11761 | return Handle<Object>(frame->GetExpression(index), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11762 | } |
| 11763 | } |
| 11764 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11765 | if (scope_info->HasHeapAllocatedLocals()) { |
| 11766 | VariableMode mode; |
| 11767 | InitializationFlag init_flag; |
| 11768 | index = scope_info->ContextSlotIndex( |
| 11769 | isolate->heap()->arguments_symbol(), &mode, &init_flag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11770 | if (index != -1) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11771 | return Handle<Object>(function_context->get(index), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11772 | } |
| 11773 | } |
| 11774 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11775 | Handle<JSFunction> function(JSFunction::cast(frame_inspector->GetFunction())); |
| 11776 | int length = frame_inspector->GetParametersCount(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11777 | Handle<JSObject> arguments = |
| 11778 | isolate->factory()->NewArgumentsObject(function, length); |
| 11779 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(length); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 11780 | |
| 11781 | AssertNoAllocation no_gc; |
| 11782 | WriteBarrierMode mode = array->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11783 | for (int i = 0; i < length; i++) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11784 | array->set(i, frame_inspector->GetParameter(i), mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11785 | } |
| 11786 | arguments->set_elements(*array); |
| 11787 | return arguments; |
| 11788 | } |
| 11789 | |
| 11790 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11791 | static const char kSourceStr[] = |
| 11792 | "(function(arguments,__source__){return eval(__source__);})"; |
| 11793 | |
| 11794 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11795 | // Evaluate a piece of JavaScript in the context of a stack frame for |
| 11796 | // debugging. This is accomplished by creating a new context which in its |
| 11797 | // extension part has all the parameters and locals of the function on the |
| 11798 | // stack frame. A function which calls eval with the code to evaluate is then |
| 11799 | // compiled in this context and called in this context. As this context |
| 11800 | // replaces the context of the function on the stack frame a new (empty) |
| 11801 | // function is created as well to be used as the closure for the context. |
| 11802 | // This function and the context acts as replacements for the function on the |
| 11803 | // stack frame presenting the same view of the values of parameters and |
| 11804 | // local variables as if the piece of JavaScript was evaluated at the point |
| 11805 | // where the function on the stack frame is currently stopped. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11806 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugEvaluate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11807 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11808 | |
| 11809 | // Check the execution state and decode arguments frame and source to be |
| 11810 | // evaluated. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11811 | ASSERT(args.length() == 6); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11812 | Object* check_result; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11813 | { MaybeObject* maybe_check_result = Runtime_CheckExecutionState( |
| 11814 | RUNTIME_ARGUMENTS(isolate, args)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11815 | if (!maybe_check_result->ToObject(&check_result)) { |
| 11816 | return maybe_check_result; |
| 11817 | } |
| 11818 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11819 | CONVERT_SMI_ARG_CHECKED(wrapped_id, 1); |
| 11820 | CONVERT_NUMBER_CHECKED(int, inlined_jsframe_index, Int32, args[2]); |
| 11821 | CONVERT_ARG_HANDLE_CHECKED(String, source, 3); |
| 11822 | CONVERT_BOOLEAN_ARG_CHECKED(disable_break, 4); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11823 | Handle<Object> additional_context(args[5]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11824 | |
| 11825 | // Handle the processing of break. |
| 11826 | DisableBreak disable_break_save(disable_break); |
| 11827 | |
| 11828 | // Get the frame where the debugging is performed. |
| 11829 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11830 | JavaScriptFrameIterator it(isolate, id); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11831 | JavaScriptFrame* frame = it.frame(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11832 | FrameInspector frame_inspector(frame, inlined_jsframe_index, isolate); |
| 11833 | Handle<JSFunction> function(JSFunction::cast(frame_inspector.GetFunction())); |
| 11834 | Handle<ScopeInfo> scope_info(function->shared()->scope_info()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11835 | |
| 11836 | // Traverse the saved contexts chain to find the active context for the |
| 11837 | // selected frame. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11838 | SaveContext* save = FindSavedContextForFrame(isolate, frame); |
| 11839 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11840 | SaveContext savex(isolate); |
| 11841 | isolate->set_context(*(save->context())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11842 | |
| 11843 | // Create the (empty) function replacing the function on the stack frame for |
| 11844 | // the purpose of evaluating in the context created below. It is important |
| 11845 | // that this function does not describe any parameters and local variables |
| 11846 | // in the context. If it does then this will cause problems with the lookup |
| 11847 | // in Context::Lookup, where context slots for parameters and local variables |
| 11848 | // are looked at before the extension object. |
| 11849 | Handle<JSFunction> go_between = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11850 | isolate->factory()->NewFunction(isolate->factory()->empty_string(), |
| 11851 | isolate->factory()->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11852 | go_between->set_context(function->context()); |
| 11853 | #ifdef DEBUG |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11854 | Handle<ScopeInfo> go_between_scope_info(go_between->shared()->scope_info()); |
| 11855 | ASSERT(go_between_scope_info->ParameterCount() == 0); |
| 11856 | ASSERT(go_between_scope_info->ContextLocalCount() == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11857 | #endif |
| 11858 | |
| 11859 | // Materialize the content of the local scope into a JSObject. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11860 | Handle<JSObject> local_scope = MaterializeLocalScopeWithFrameInspector( |
| 11861 | isolate, frame, &frame_inspector); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11862 | RETURN_IF_EMPTY_HANDLE(isolate, local_scope); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11863 | |
| 11864 | // Allocate a new context for the debug evaluation and set the extension |
| 11865 | // object build. |
| 11866 | Handle<Context> context = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11867 | isolate->factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, |
| 11868 | go_between); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11869 | context->set_extension(*local_scope); |
| 11870 | // Copy any with contexts present and chain them in front of this context. |
| 11871 | Handle<Context> frame_context(Context::cast(frame->context())); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11872 | Handle<Context> function_context; |
| 11873 | // Get the function's context if it has one. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11874 | if (scope_info->HasContext()) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11875 | function_context = Handle<Context>(frame_context->declaration_context()); |
| 11876 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11877 | context = CopyNestedScopeContextChain(isolate, |
| 11878 | go_between, |
| 11879 | context, |
| 11880 | frame, |
| 11881 | inlined_jsframe_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11882 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 11883 | if (additional_context->IsJSObject()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11884 | Handle<JSObject> extension = Handle<JSObject>::cast(additional_context); |
| 11885 | context = |
| 11886 | isolate->factory()->NewWithContext(go_between, context, extension); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 11887 | } |
| 11888 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11889 | // Wrap the evaluation statement in a new function compiled in the newly |
| 11890 | // created context. The function has one parameter which has to be called |
| 11891 | // 'arguments'. This it to have access to what would have been 'arguments' in |
| 11892 | // the function being debugged. |
| 11893 | // function(arguments,__source__) {return eval(__source__);} |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11894 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11895 | Handle<String> function_source = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11896 | isolate->factory()->NewStringFromAscii( |
| 11897 | Vector<const char>(kSourceStr, sizeof(kSourceStr) - 1)); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 11898 | |
| 11899 | // Currently, the eval code will be executed in non-strict mode, |
| 11900 | // even in the strict code context. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11901 | Handle<SharedFunctionInfo> shared = |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11902 | Compiler::CompileEval(function_source, |
| 11903 | context, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 11904 | context->IsGlobalContext(), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11905 | CLASSIC_MODE, |
| 11906 | RelocInfo::kNoPosition); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11907 | if (shared.is_null()) return Failure::Exception(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11908 | Handle<JSFunction> compiled_function = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11909 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11910 | |
| 11911 | // Invoke the result of the compilation to get the evaluation function. |
| 11912 | bool has_pending_exception; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11913 | Handle<Object> receiver(frame->receiver(), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11914 | Handle<Object> evaluation_function = |
| 11915 | Execution::Call(compiled_function, receiver, 0, NULL, |
| 11916 | &has_pending_exception); |
| 11917 | if (has_pending_exception) return Failure::Exception(); |
| 11918 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11919 | Handle<Object> arguments = GetArgumentsObject(isolate, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11920 | frame, |
| 11921 | &frame_inspector, |
| 11922 | scope_info, |
| 11923 | function_context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11924 | |
| 11925 | // Invoke the evaluation function and return the result. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11926 | Handle<Object> argv[] = { arguments, source }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11927 | Handle<Object> result = |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11928 | Execution::Call(Handle<JSFunction>::cast(evaluation_function), |
| 11929 | receiver, |
| 11930 | ARRAY_SIZE(argv), |
| 11931 | argv, |
| 11932 | &has_pending_exception); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11933 | if (has_pending_exception) return Failure::Exception(); |
| 11934 | |
| 11935 | // Skip the global proxy as it has no properties and always delegates to the |
| 11936 | // real global object. |
| 11937 | if (result->IsJSGlobalProxy()) { |
| 11938 | result = Handle<JSObject>(JSObject::cast(result->GetPrototype())); |
| 11939 | } |
| 11940 | |
| 11941 | return *result; |
| 11942 | } |
| 11943 | |
| 11944 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11945 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugEvaluateGlobal) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11946 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11947 | |
| 11948 | // Check the execution state and decode arguments frame and source to be |
| 11949 | // evaluated. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 11950 | ASSERT(args.length() == 4); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11951 | Object* check_result; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11952 | { MaybeObject* maybe_check_result = Runtime_CheckExecutionState( |
| 11953 | RUNTIME_ARGUMENTS(isolate, args)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11954 | if (!maybe_check_result->ToObject(&check_result)) { |
| 11955 | return maybe_check_result; |
| 11956 | } |
| 11957 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11958 | CONVERT_ARG_HANDLE_CHECKED(String, source, 1); |
| 11959 | CONVERT_BOOLEAN_ARG_CHECKED(disable_break, 2); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 11960 | Handle<Object> additional_context(args[3]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11961 | |
| 11962 | // Handle the processing of break. |
| 11963 | DisableBreak disable_break_save(disable_break); |
| 11964 | |
| 11965 | // Enter the top context from before the debugger was invoked. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11966 | SaveContext save(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11967 | SaveContext* top = &save; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11968 | while (top != NULL && *top->context() == *isolate->debug()->debug_context()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11969 | top = top->prev(); |
| 11970 | } |
| 11971 | if (top != NULL) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11972 | isolate->set_context(*top->context()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11973 | } |
| 11974 | |
| 11975 | // Get the global context now set to the top context from before the |
| 11976 | // debugger was invoked. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11977 | Handle<Context> context = isolate->global_context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11978 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 11979 | bool is_global = true; |
| 11980 | |
| 11981 | if (additional_context->IsJSObject()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11982 | // Create a new with context with the additional context information between |
| 11983 | // the context of the debugged function and the eval code to be executed. |
| 11984 | context = isolate->factory()->NewWithContext( |
| 11985 | Handle<JSFunction>(context->closure()), |
| 11986 | context, |
| 11987 | Handle<JSObject>::cast(additional_context)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 11988 | is_global = false; |
| 11989 | } |
| 11990 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11991 | // Compile the source to be evaluated. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 11992 | // Currently, the eval code will be executed in non-strict mode, |
| 11993 | // even in the strict code context. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 11994 | Handle<SharedFunctionInfo> shared = |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 11995 | Compiler::CompileEval(source, |
| 11996 | context, |
| 11997 | is_global, |
| 11998 | CLASSIC_MODE, |
| 11999 | RelocInfo::kNoPosition); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12000 | if (shared.is_null()) return Failure::Exception(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12001 | Handle<JSFunction> compiled_function = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12002 | Handle<JSFunction>( |
| 12003 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 12004 | context)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12005 | |
| 12006 | // Invoke the result of the compilation to get the evaluation function. |
| 12007 | bool has_pending_exception; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12008 | Handle<Object> receiver = isolate->global(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12009 | Handle<Object> result = |
| 12010 | Execution::Call(compiled_function, receiver, 0, NULL, |
| 12011 | &has_pending_exception); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12012 | // Clear the oneshot breakpoints so that the debugger does not step further. |
| 12013 | isolate->debug()->ClearStepping(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12014 | if (has_pending_exception) return Failure::Exception(); |
| 12015 | return *result; |
| 12016 | } |
| 12017 | |
| 12018 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12019 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetLoadedScripts) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12020 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12021 | ASSERT(args.length() == 0); |
| 12022 | |
| 12023 | // Fill the script objects. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12024 | Handle<FixedArray> instances = isolate->debug()->GetLoadedScripts(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12025 | |
| 12026 | // Convert the script objects to proper JS objects. |
| 12027 | for (int i = 0; i < instances->length(); i++) { |
| 12028 | Handle<Script> script = Handle<Script>(Script::cast(instances->get(i))); |
| 12029 | // Get the script wrapper in a local handle before calling GetScriptWrapper, |
| 12030 | // because using |
| 12031 | // instances->set(i, *GetScriptWrapper(script)) |
| 12032 | // is unsafe as GetScriptWrapper might call GC and the C++ compiler might |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12033 | // already have dereferenced the instances handle. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12034 | Handle<JSValue> wrapper = GetScriptWrapper(script); |
| 12035 | instances->set(i, *wrapper); |
| 12036 | } |
| 12037 | |
| 12038 | // Return result as a JS array. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12039 | Handle<JSObject> result = |
| 12040 | isolate->factory()->NewJSObject(isolate->array_function()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12041 | isolate->factory()->SetContent(Handle<JSArray>::cast(result), instances); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12042 | return *result; |
| 12043 | } |
| 12044 | |
| 12045 | |
| 12046 | // Helper function used by Runtime_DebugReferencedBy below. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12047 | static int DebugReferencedBy(HeapIterator* iterator, |
| 12048 | JSObject* target, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12049 | Object* instance_filter, int max_references, |
| 12050 | FixedArray* instances, int instances_size, |
| 12051 | JSFunction* arguments_function) { |
| 12052 | NoHandleAllocation ha; |
| 12053 | AssertNoAllocation no_alloc; |
| 12054 | |
| 12055 | // Iterate the heap. |
| 12056 | int count = 0; |
| 12057 | JSObject* last = NULL; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 12058 | HeapObject* heap_obj = NULL; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12059 | while (((heap_obj = iterator->next()) != NULL) && |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12060 | (max_references == 0 || count < max_references)) { |
| 12061 | // Only look at all JSObjects. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12062 | if (heap_obj->IsJSObject()) { |
| 12063 | // Skip context extension objects and argument arrays as these are |
| 12064 | // checked in the context of functions using them. |
| 12065 | JSObject* obj = JSObject::cast(heap_obj); |
| 12066 | if (obj->IsJSContextExtensionObject() || |
| 12067 | obj->map()->constructor() == arguments_function) { |
| 12068 | continue; |
| 12069 | } |
| 12070 | |
| 12071 | // Check if the JS object has a reference to the object looked for. |
| 12072 | if (obj->ReferencesObject(target)) { |
| 12073 | // Check instance filter if supplied. This is normally used to avoid |
| 12074 | // references from mirror objects (see Runtime_IsInPrototypeChain). |
| 12075 | if (!instance_filter->IsUndefined()) { |
| 12076 | Object* V = obj; |
| 12077 | while (true) { |
| 12078 | Object* prototype = V->GetPrototype(); |
| 12079 | if (prototype->IsNull()) { |
| 12080 | break; |
| 12081 | } |
| 12082 | if (instance_filter == prototype) { |
| 12083 | obj = NULL; // Don't add this object. |
| 12084 | break; |
| 12085 | } |
| 12086 | V = prototype; |
| 12087 | } |
| 12088 | } |
| 12089 | |
| 12090 | if (obj != NULL) { |
| 12091 | // Valid reference found add to instance array if supplied an update |
| 12092 | // count. |
| 12093 | if (instances != NULL && count < instances_size) { |
| 12094 | instances->set(count, obj); |
| 12095 | } |
| 12096 | last = obj; |
| 12097 | count++; |
| 12098 | } |
| 12099 | } |
| 12100 | } |
| 12101 | } |
| 12102 | |
| 12103 | // Check for circular reference only. This can happen when the object is only |
| 12104 | // referenced from mirrors and has a circular reference in which case the |
| 12105 | // object is not really alive and would have been garbage collected if not |
| 12106 | // referenced from the mirror. |
| 12107 | if (count == 1 && last == target) { |
| 12108 | count = 0; |
| 12109 | } |
| 12110 | |
| 12111 | // Return the number of referencing objects found. |
| 12112 | return count; |
| 12113 | } |
| 12114 | |
| 12115 | |
| 12116 | // Scan the heap for objects with direct references to an object |
| 12117 | // args[0]: the object to find references to |
| 12118 | // args[1]: constructor function for instances to exclude (Mirror) |
| 12119 | // args[2]: the the maximum number of objects to return |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12120 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugReferencedBy) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12121 | ASSERT(args.length() == 3); |
| 12122 | |
| 12123 | // First perform a full GC in order to avoid references from dead objects. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12124 | isolate->heap()->CollectAllGarbage(Heap::kMakeHeapIterableMask, |
| 12125 | "%DebugReferencedBy"); |
| 12126 | // The heap iterator reserves the right to do a GC to make the heap iterable. |
| 12127 | // Due to the GC above we know it won't need to do that, but it seems cleaner |
| 12128 | // to get the heap iterator constructed before we start having unprotected |
| 12129 | // Object* locals that are not protected by handles. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12130 | |
| 12131 | // Check parameters. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12132 | CONVERT_ARG_CHECKED(JSObject, target, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12133 | Object* instance_filter = args[1]; |
| 12134 | RUNTIME_ASSERT(instance_filter->IsUndefined() || |
| 12135 | instance_filter->IsJSObject()); |
| 12136 | CONVERT_NUMBER_CHECKED(int32_t, max_references, Int32, args[2]); |
| 12137 | RUNTIME_ASSERT(max_references >= 0); |
| 12138 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12139 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12140 | // Get the constructor function for context extension and arguments array. |
| 12141 | JSObject* arguments_boilerplate = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12142 | isolate->context()->global_context()->arguments_boilerplate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12143 | JSFunction* arguments_function = |
| 12144 | JSFunction::cast(arguments_boilerplate->map()->constructor()); |
| 12145 | |
| 12146 | // Get the number of referencing objects. |
| 12147 | int count; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12148 | HeapIterator heap_iterator; |
| 12149 | count = DebugReferencedBy(&heap_iterator, |
| 12150 | target, instance_filter, max_references, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12151 | NULL, 0, arguments_function); |
| 12152 | |
| 12153 | // Allocate an array to hold the result. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 12154 | Object* object; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12155 | { MaybeObject* maybe_object = isolate->heap()->AllocateFixedArray(count); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 12156 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 12157 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12158 | FixedArray* instances = FixedArray::cast(object); |
| 12159 | |
| 12160 | // Fill the referencing objects. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12161 | // AllocateFixedArray above does not make the heap non-iterable. |
| 12162 | ASSERT(HEAP->IsHeapIterable()); |
| 12163 | HeapIterator heap_iterator2; |
| 12164 | count = DebugReferencedBy(&heap_iterator2, |
| 12165 | target, instance_filter, max_references, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12166 | instances, count, arguments_function); |
| 12167 | |
| 12168 | // Return result as JS array. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 12169 | Object* result; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12170 | MaybeObject* maybe_result = isolate->heap()->AllocateJSObject( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12171 | isolate->context()->global_context()->array_function()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12172 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 12173 | return JSArray::cast(result)->SetContent(instances); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12174 | } |
| 12175 | |
| 12176 | |
| 12177 | // Helper function used by Runtime_DebugConstructedBy below. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12178 | static int DebugConstructedBy(HeapIterator* iterator, |
| 12179 | JSFunction* constructor, |
| 12180 | int max_references, |
| 12181 | FixedArray* instances, |
| 12182 | int instances_size) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12183 | AssertNoAllocation no_alloc; |
| 12184 | |
| 12185 | // Iterate the heap. |
| 12186 | int count = 0; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 12187 | HeapObject* heap_obj = NULL; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12188 | while (((heap_obj = iterator->next()) != NULL) && |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12189 | (max_references == 0 || count < max_references)) { |
| 12190 | // Only look at all JSObjects. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12191 | if (heap_obj->IsJSObject()) { |
| 12192 | JSObject* obj = JSObject::cast(heap_obj); |
| 12193 | if (obj->map()->constructor() == constructor) { |
| 12194 | // Valid reference found add to instance array if supplied an update |
| 12195 | // count. |
| 12196 | if (instances != NULL && count < instances_size) { |
| 12197 | instances->set(count, obj); |
| 12198 | } |
| 12199 | count++; |
| 12200 | } |
| 12201 | } |
| 12202 | } |
| 12203 | |
| 12204 | // Return the number of referencing objects found. |
| 12205 | return count; |
| 12206 | } |
| 12207 | |
| 12208 | |
| 12209 | // Scan the heap for objects constructed by a specific function. |
| 12210 | // args[0]: the constructor to find instances of |
| 12211 | // args[1]: the the maximum number of objects to return |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12212 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugConstructedBy) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12213 | ASSERT(args.length() == 2); |
| 12214 | |
| 12215 | // First perform a full GC in order to avoid dead objects. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12216 | isolate->heap()->CollectAllGarbage(Heap::kMakeHeapIterableMask, |
| 12217 | "%DebugConstructedBy"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12218 | |
| 12219 | // Check parameters. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12220 | CONVERT_ARG_CHECKED(JSFunction, constructor, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12221 | CONVERT_NUMBER_CHECKED(int32_t, max_references, Int32, args[1]); |
| 12222 | RUNTIME_ASSERT(max_references >= 0); |
| 12223 | |
| 12224 | // Get the number of referencing objects. |
| 12225 | int count; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12226 | HeapIterator heap_iterator; |
| 12227 | count = DebugConstructedBy(&heap_iterator, |
| 12228 | constructor, |
| 12229 | max_references, |
| 12230 | NULL, |
| 12231 | 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12232 | |
| 12233 | // Allocate an array to hold the result. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 12234 | Object* object; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12235 | { MaybeObject* maybe_object = isolate->heap()->AllocateFixedArray(count); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 12236 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 12237 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12238 | FixedArray* instances = FixedArray::cast(object); |
| 12239 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12240 | ASSERT(HEAP->IsHeapIterable()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12241 | // Fill the referencing objects. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12242 | HeapIterator heap_iterator2; |
| 12243 | count = DebugConstructedBy(&heap_iterator2, |
| 12244 | constructor, |
| 12245 | max_references, |
| 12246 | instances, |
| 12247 | count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12248 | |
| 12249 | // Return result as JS array. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 12250 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12251 | { MaybeObject* maybe_result = isolate->heap()->AllocateJSObject( |
| 12252 | isolate->context()->global_context()->array_function()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 12253 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 12254 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12255 | return JSArray::cast(result)->SetContent(instances); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12256 | } |
| 12257 | |
| 12258 | |
| 12259 | // Find the effective prototype object as returned by __proto__. |
| 12260 | // args[0]: the object to find the prototype for. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12261 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetPrototype) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12262 | ASSERT(args.length() == 1); |
| 12263 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12264 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12265 | |
| 12266 | // Use the __proto__ accessor. |
| 12267 | return Accessors::ObjectPrototype.getter(obj, NULL); |
| 12268 | } |
| 12269 | |
| 12270 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12271 | // Patches script source (should be called upon BeforeCompile event). |
| 12272 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugSetScriptSource) { |
| 12273 | HandleScope scope(isolate); |
| 12274 | ASSERT(args.length() == 2); |
| 12275 | |
| 12276 | CONVERT_ARG_HANDLE_CHECKED(JSValue, script_wrapper, 0); |
| 12277 | Handle<String> source(String::cast(args[1])); |
| 12278 | |
| 12279 | RUNTIME_ASSERT(script_wrapper->value()->IsScript()); |
| 12280 | Handle<Script> script(Script::cast(script_wrapper->value())); |
| 12281 | |
| 12282 | int compilation_state = Smi::cast(script->compilation_state())->value(); |
| 12283 | RUNTIME_ASSERT(compilation_state == Script::COMPILATION_STATE_INITIAL); |
| 12284 | script->set_source(*source); |
| 12285 | |
| 12286 | return isolate->heap()->undefined_value(); |
| 12287 | } |
| 12288 | |
| 12289 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12290 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SystemBreak) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12291 | ASSERT(args.length() == 0); |
| 12292 | CPU::DebugBreak(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12293 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12294 | } |
| 12295 | |
| 12296 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12297 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugDisassembleFunction) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12298 | #ifdef DEBUG |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12299 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12300 | ASSERT(args.length() == 1); |
| 12301 | // Get the function and make sure it is compiled. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12302 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, func, 0); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 12303 | Handle<SharedFunctionInfo> shared(func->shared()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12304 | if (!SharedFunctionInfo::EnsureCompiled(shared, KEEP_EXCEPTION)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12305 | return Failure::Exception(); |
| 12306 | } |
| 12307 | func->code()->PrintLn(); |
| 12308 | #endif // DEBUG |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12309 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12310 | } |
| 12311 | |
| 12312 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12313 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugDisassembleConstructor) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12314 | #ifdef DEBUG |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12315 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12316 | ASSERT(args.length() == 1); |
| 12317 | // Get the function and make sure it is compiled. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12318 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, func, 0); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 12319 | Handle<SharedFunctionInfo> shared(func->shared()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12320 | if (!SharedFunctionInfo::EnsureCompiled(shared, KEEP_EXCEPTION)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12321 | return Failure::Exception(); |
| 12322 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 12323 | shared->construct_stub()->PrintLn(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12324 | #endif // DEBUG |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12325 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12326 | } |
| 12327 | |
| 12328 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12329 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetInferredName) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12330 | NoHandleAllocation ha; |
| 12331 | ASSERT(args.length() == 1); |
| 12332 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12333 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12334 | return f->shared()->inferred_name(); |
| 12335 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 12336 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12337 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12338 | static int FindSharedFunctionInfosForScript(HeapIterator* iterator, |
| 12339 | Script* script, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12340 | FixedArray* buffer) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12341 | AssertNoAllocation no_allocations; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12342 | int counter = 0; |
| 12343 | int buffer_size = buffer->length(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12344 | for (HeapObject* obj = iterator->next(); |
| 12345 | obj != NULL; |
| 12346 | obj = iterator->next()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12347 | ASSERT(obj != NULL); |
| 12348 | if (!obj->IsSharedFunctionInfo()) { |
| 12349 | continue; |
| 12350 | } |
| 12351 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(obj); |
| 12352 | if (shared->script() != script) { |
| 12353 | continue; |
| 12354 | } |
| 12355 | if (counter < buffer_size) { |
| 12356 | buffer->set(counter, shared); |
| 12357 | } |
| 12358 | counter++; |
| 12359 | } |
| 12360 | return counter; |
| 12361 | } |
| 12362 | |
| 12363 | // For a script finds all SharedFunctionInfo's in the heap that points |
| 12364 | // to this script. Returns JSArray of SharedFunctionInfo wrapped |
| 12365 | // in OpaqueReferences. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12366 | RUNTIME_FUNCTION(MaybeObject*, |
| 12367 | Runtime_LiveEditFindSharedFunctionInfosForScript) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12368 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12369 | HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12370 | CONVERT_ARG_CHECKED(JSValue, script_value, 0); |
| 12371 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12372 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12373 | Handle<Script> script = Handle<Script>(Script::cast(script_value->value())); |
| 12374 | |
| 12375 | const int kBufferSize = 32; |
| 12376 | |
| 12377 | Handle<FixedArray> array; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12378 | array = isolate->factory()->NewFixedArray(kBufferSize); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12379 | int number; |
| 12380 | { |
| 12381 | isolate->heap()->EnsureHeapIsIterable(); |
| 12382 | AssertNoAllocation no_allocations; |
| 12383 | HeapIterator heap_iterator; |
| 12384 | Script* scr = *script; |
| 12385 | FixedArray* arr = *array; |
| 12386 | number = FindSharedFunctionInfosForScript(&heap_iterator, scr, arr); |
| 12387 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12388 | if (number > kBufferSize) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12389 | array = isolate->factory()->NewFixedArray(number); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12390 | isolate->heap()->EnsureHeapIsIterable(); |
| 12391 | AssertNoAllocation no_allocations; |
| 12392 | HeapIterator heap_iterator; |
| 12393 | Script* scr = *script; |
| 12394 | FixedArray* arr = *array; |
| 12395 | FindSharedFunctionInfosForScript(&heap_iterator, scr, arr); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12396 | } |
| 12397 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12398 | Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(array); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12399 | result->set_length(Smi::FromInt(number)); |
| 12400 | |
| 12401 | LiveEdit::WrapSharedFunctionInfos(result); |
| 12402 | |
| 12403 | return *result; |
| 12404 | } |
| 12405 | |
| 12406 | // For a script calculates compilation information about all its functions. |
| 12407 | // The script source is explicitly specified by the second argument. |
| 12408 | // The source of the actual script is not used, however it is important that |
| 12409 | // all generated code keeps references to this particular instance of script. |
| 12410 | // Returns a JSArray of compilation infos. The array is ordered so that |
| 12411 | // each function with all its descendant is always stored in a continues range |
| 12412 | // with the function itself going first. The root function is a script function. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12413 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditGatherCompileInfo) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12414 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12415 | HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12416 | CONVERT_ARG_CHECKED(JSValue, script, 0); |
| 12417 | CONVERT_ARG_HANDLE_CHECKED(String, source, 1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12418 | Handle<Script> script_handle = Handle<Script>(Script::cast(script->value())); |
| 12419 | |
| 12420 | JSArray* result = LiveEdit::GatherCompileInfo(script_handle, source); |
| 12421 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12422 | if (isolate->has_pending_exception()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12423 | return Failure::Exception(); |
| 12424 | } |
| 12425 | |
| 12426 | return result; |
| 12427 | } |
| 12428 | |
| 12429 | // Changes the source of the script to a new_source. |
| 12430 | // If old_script_name is provided (i.e. is a String), also creates a copy of |
| 12431 | // the script with its original source and sends notification to debugger. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12432 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditReplaceScript) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12433 | ASSERT(args.length() == 3); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12434 | HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12435 | CONVERT_ARG_CHECKED(JSValue, original_script_value, 0); |
| 12436 | CONVERT_ARG_HANDLE_CHECKED(String, new_source, 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12437 | Handle<Object> old_script_name(args[2], isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12438 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12439 | RUNTIME_ASSERT(original_script_value->value()->IsScript()); |
| 12440 | Handle<Script> original_script(Script::cast(original_script_value->value())); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12441 | |
| 12442 | Object* old_script = LiveEdit::ChangeScriptSource(original_script, |
| 12443 | new_source, |
| 12444 | old_script_name); |
| 12445 | |
| 12446 | if (old_script->IsScript()) { |
| 12447 | Handle<Script> script_handle(Script::cast(old_script)); |
| 12448 | return *(GetScriptWrapper(script_handle)); |
| 12449 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12450 | return isolate->heap()->null_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12451 | } |
| 12452 | } |
| 12453 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12454 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12455 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditFunctionSourceUpdated) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12456 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12457 | HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12458 | CONVERT_ARG_HANDLE_CHECKED(JSArray, shared_info, 0); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12459 | return LiveEdit::FunctionSourceUpdated(shared_info); |
| 12460 | } |
| 12461 | |
| 12462 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12463 | // Replaces code of SharedFunctionInfo with a new one. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12464 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditReplaceFunctionCode) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12465 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12466 | HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12467 | CONVERT_ARG_HANDLE_CHECKED(JSArray, new_compile_info, 0); |
| 12468 | CONVERT_ARG_HANDLE_CHECKED(JSArray, shared_info, 1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12469 | |
| 12470 | return LiveEdit::ReplaceFunctionCode(new_compile_info, shared_info); |
| 12471 | } |
| 12472 | |
| 12473 | // Connects SharedFunctionInfo to another script. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12474 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditFunctionSetScript) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12475 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12476 | HandleScope scope(isolate); |
| 12477 | Handle<Object> function_object(args[0], isolate); |
| 12478 | Handle<Object> script_object(args[1], isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12479 | |
| 12480 | if (function_object->IsJSValue()) { |
| 12481 | Handle<JSValue> function_wrapper = Handle<JSValue>::cast(function_object); |
| 12482 | if (script_object->IsJSValue()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12483 | RUNTIME_ASSERT(JSValue::cast(*script_object)->value()->IsScript()); |
| 12484 | Script* script = Script::cast(JSValue::cast(*script_object)->value()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12485 | script_object = Handle<Object>(script, isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12486 | } |
| 12487 | |
| 12488 | LiveEdit::SetFunctionScript(function_wrapper, script_object); |
| 12489 | } else { |
| 12490 | // Just ignore this. We may not have a SharedFunctionInfo for some functions |
| 12491 | // and we check it in this function. |
| 12492 | } |
| 12493 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12494 | return isolate->heap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12495 | } |
| 12496 | |
| 12497 | |
| 12498 | // In a code of a parent function replaces original function as embedded object |
| 12499 | // with a substitution one. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12500 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditReplaceRefToNestedFunction) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12501 | ASSERT(args.length() == 3); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12502 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12503 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12504 | CONVERT_ARG_HANDLE_CHECKED(JSValue, parent_wrapper, 0); |
| 12505 | CONVERT_ARG_HANDLE_CHECKED(JSValue, orig_wrapper, 1); |
| 12506 | CONVERT_ARG_HANDLE_CHECKED(JSValue, subst_wrapper, 2); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12507 | |
| 12508 | LiveEdit::ReplaceRefToNestedFunction(parent_wrapper, orig_wrapper, |
| 12509 | subst_wrapper); |
| 12510 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12511 | return isolate->heap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12512 | } |
| 12513 | |
| 12514 | |
| 12515 | // Updates positions of a shared function info (first parameter) according |
| 12516 | // to script source change. Text change is described in second parameter as |
| 12517 | // array of groups of 3 numbers: |
| 12518 | // (change_begin, change_end, change_end_new_position). |
| 12519 | // Each group describes a change in text; groups are sorted by change_begin. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12520 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditPatchFunctionPositions) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12521 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12522 | HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12523 | CONVERT_ARG_HANDLE_CHECKED(JSArray, shared_array, 0); |
| 12524 | CONVERT_ARG_HANDLE_CHECKED(JSArray, position_change_array, 1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12525 | |
| 12526 | return LiveEdit::PatchFunctionPositions(shared_array, position_change_array); |
| 12527 | } |
| 12528 | |
| 12529 | |
| 12530 | // For array of SharedFunctionInfo's (each wrapped in JSValue) |
| 12531 | // checks that none of them have activations on stacks (of any thread). |
| 12532 | // Returns array of the same length with corresponding results of |
| 12533 | // LiveEdit::FunctionPatchabilityStatus type. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12534 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditCheckAndDropActivations) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12535 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12536 | HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12537 | CONVERT_ARG_HANDLE_CHECKED(JSArray, shared_array, 0); |
| 12538 | CONVERT_BOOLEAN_ARG_CHECKED(do_drop, 1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12539 | |
| 12540 | return *LiveEdit::CheckAndDropActivations(shared_array, do_drop); |
| 12541 | } |
| 12542 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 12543 | // Compares 2 strings line-by-line, then token-wise and returns diff in form |
| 12544 | // of JSArray of triplets (pos1, pos1_end, pos2_end) describing list |
| 12545 | // of diff chunks. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12546 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditCompareStrings) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12547 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12548 | HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12549 | CONVERT_ARG_HANDLE_CHECKED(String, s1, 0); |
| 12550 | CONVERT_ARG_HANDLE_CHECKED(String, s2, 1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12551 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 12552 | return *LiveEdit::CompareStrings(s1, s2); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12553 | } |
| 12554 | |
| 12555 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12556 | // A testing entry. Returns statement position which is the closest to |
| 12557 | // source_position. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12558 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFunctionCodePositionFromSource) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12559 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12560 | HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12561 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12562 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 12563 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12564 | Handle<Code> code(function->code(), isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12565 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12566 | if (code->kind() != Code::FUNCTION && |
| 12567 | code->kind() != Code::OPTIMIZED_FUNCTION) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12568 | return isolate->heap()->undefined_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12569 | } |
| 12570 | |
| 12571 | RelocIterator it(*code, RelocInfo::ModeMask(RelocInfo::STATEMENT_POSITION)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12572 | int closest_pc = 0; |
| 12573 | int distance = kMaxInt; |
| 12574 | while (!it.done()) { |
| 12575 | int statement_position = static_cast<int>(it.rinfo()->data()); |
| 12576 | // Check if this break point is closer that what was previously found. |
| 12577 | if (source_position <= statement_position && |
| 12578 | statement_position - source_position < distance) { |
| 12579 | closest_pc = |
| 12580 | static_cast<int>(it.rinfo()->pc() - code->instruction_start()); |
| 12581 | distance = statement_position - source_position; |
| 12582 | // Check whether we can't get any closer. |
| 12583 | if (distance == 0) break; |
| 12584 | } |
| 12585 | it.next(); |
| 12586 | } |
| 12587 | |
| 12588 | return Smi::FromInt(closest_pc); |
| 12589 | } |
| 12590 | |
| 12591 | |
| 12592 | // Calls specified function with or without entering the debugger. |
| 12593 | // This is used in unit tests to run code as if debugger is entered or simply |
| 12594 | // to have a stack with C++ frame in the middle. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12595 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ExecuteInDebugContext) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12596 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12597 | HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12598 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
| 12599 | CONVERT_BOOLEAN_ARG_CHECKED(without_debugger, 1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12600 | |
| 12601 | Handle<Object> result; |
| 12602 | bool pending_exception; |
| 12603 | { |
| 12604 | if (without_debugger) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12605 | result = Execution::Call(function, isolate->global(), 0, NULL, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12606 | &pending_exception); |
| 12607 | } else { |
| 12608 | EnterDebugger enter_debugger; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12609 | result = Execution::Call(function, isolate->global(), 0, NULL, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12610 | &pending_exception); |
| 12611 | } |
| 12612 | } |
| 12613 | if (!pending_exception) { |
| 12614 | return *result; |
| 12615 | } else { |
| 12616 | return Failure::Exception(); |
| 12617 | } |
| 12618 | } |
| 12619 | |
| 12620 | |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 12621 | // Sets a v8 flag. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12622 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetFlags) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12623 | CONVERT_ARG_CHECKED(String, arg, 0); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 12624 | SmartArrayPointer<char> flags = |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 12625 | arg->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 12626 | FlagList::SetFlagsFromString(*flags, StrLength(*flags)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12627 | return isolate->heap()->undefined_value(); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 12628 | } |
| 12629 | |
| 12630 | |
| 12631 | // Performs a GC. |
| 12632 | // Presently, it only does a full GC. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12633 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CollectGarbage) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12634 | isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, "%CollectGarbage"); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12635 | return isolate->heap()->undefined_value(); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 12636 | } |
| 12637 | |
| 12638 | |
| 12639 | // Gets the current heap usage. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12640 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetHeapUsage) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12641 | int usage = static_cast<int>(isolate->heap()->SizeOfObjects()); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 12642 | if (!Smi::IsValid(usage)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12643 | return *isolate->factory()->NewNumberFromInt(usage); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 12644 | } |
| 12645 | return Smi::FromInt(usage); |
| 12646 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12647 | |
| 12648 | |
| 12649 | // Captures a live object list from the present heap. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12650 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasLOLEnabled) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12651 | #ifdef LIVE_OBJECT_LIST |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12652 | return isolate->heap()->true_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12653 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12654 | return isolate->heap()->false_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12655 | #endif |
| 12656 | } |
| 12657 | |
| 12658 | |
| 12659 | // Captures a live object list from the present heap. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12660 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CaptureLOL) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12661 | #ifdef LIVE_OBJECT_LIST |
| 12662 | return LiveObjectList::Capture(); |
| 12663 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12664 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12665 | #endif |
| 12666 | } |
| 12667 | |
| 12668 | |
| 12669 | // Deletes the specified live object list. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12670 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeleteLOL) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12671 | #ifdef LIVE_OBJECT_LIST |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12672 | CONVERT_SMI_ARG_CHECKED(id, 0); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12673 | bool success = LiveObjectList::Delete(id); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12674 | return isolate->heap()->ToBoolean(success); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12675 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12676 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12677 | #endif |
| 12678 | } |
| 12679 | |
| 12680 | |
| 12681 | // Generates the response to a debugger request for a dump of the objects |
| 12682 | // contained in the difference between the captured live object lists |
| 12683 | // specified by id1 and id2. |
| 12684 | // If id1 is 0 (i.e. not a valid lol), then the whole of lol id2 will be |
| 12685 | // dumped. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12686 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DumpLOL) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12687 | #ifdef LIVE_OBJECT_LIST |
| 12688 | HandleScope scope; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12689 | CONVERT_SMI_ARG_CHECKED(id1, 0); |
| 12690 | CONVERT_SMI_ARG_CHECKED(id2, 1); |
| 12691 | CONVERT_SMI_ARG_CHECKED(start, 2); |
| 12692 | CONVERT_SMI_ARG_CHECKED(count, 3); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12693 | CONVERT_ARG_HANDLE_CHECKED(JSObject, filter_obj, 4); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12694 | EnterDebugger enter_debugger; |
| 12695 | return LiveObjectList::Dump(id1, id2, start, count, filter_obj); |
| 12696 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12697 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12698 | #endif |
| 12699 | } |
| 12700 | |
| 12701 | |
| 12702 | // Gets the specified object as requested by the debugger. |
| 12703 | // This is only used for obj ids shown in live object lists. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12704 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLObj) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12705 | #ifdef LIVE_OBJECT_LIST |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12706 | CONVERT_SMI_ARG_CHECKED(obj_id, 0); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12707 | Object* result = LiveObjectList::GetObj(obj_id); |
| 12708 | return result; |
| 12709 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12710 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12711 | #endif |
| 12712 | } |
| 12713 | |
| 12714 | |
| 12715 | // Gets the obj id for the specified address if valid. |
| 12716 | // This is only used for obj ids shown in live object lists. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12717 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLObjId) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12718 | #ifdef LIVE_OBJECT_LIST |
| 12719 | HandleScope scope; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12720 | CONVERT_ARG_HANDLE_CHECKED(String, address, 0); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12721 | Object* result = LiveObjectList::GetObjId(address); |
| 12722 | return result; |
| 12723 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12724 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12725 | #endif |
| 12726 | } |
| 12727 | |
| 12728 | |
| 12729 | // Gets the retainers that references the specified object alive. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12730 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLObjRetainers) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12731 | #ifdef LIVE_OBJECT_LIST |
| 12732 | HandleScope scope; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12733 | CONVERT_SMI_ARG_CHECKED(obj_id, 0); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12734 | RUNTIME_ASSERT(args[1]->IsUndefined() || args[1]->IsJSObject()); |
| 12735 | RUNTIME_ASSERT(args[2]->IsUndefined() || args[2]->IsBoolean()); |
| 12736 | RUNTIME_ASSERT(args[3]->IsUndefined() || args[3]->IsSmi()); |
| 12737 | RUNTIME_ASSERT(args[4]->IsUndefined() || args[4]->IsSmi()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12738 | CONVERT_ARG_HANDLE_CHECKED(JSObject, filter_obj, 5); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12739 | |
| 12740 | Handle<JSObject> instance_filter; |
| 12741 | if (args[1]->IsJSObject()) { |
| 12742 | instance_filter = args.at<JSObject>(1); |
| 12743 | } |
| 12744 | bool verbose = false; |
| 12745 | if (args[2]->IsBoolean()) { |
| 12746 | verbose = args[2]->IsTrue(); |
| 12747 | } |
| 12748 | int start = 0; |
| 12749 | if (args[3]->IsSmi()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12750 | start = args.smi_at(3); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12751 | } |
| 12752 | int limit = Smi::kMaxValue; |
| 12753 | if (args[4]->IsSmi()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12754 | limit = args.smi_at(4); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12755 | } |
| 12756 | |
| 12757 | return LiveObjectList::GetObjRetainers(obj_id, |
| 12758 | instance_filter, |
| 12759 | verbose, |
| 12760 | start, |
| 12761 | limit, |
| 12762 | filter_obj); |
| 12763 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12764 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12765 | #endif |
| 12766 | } |
| 12767 | |
| 12768 | |
| 12769 | // Gets the reference path between 2 objects. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12770 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLPath) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12771 | #ifdef LIVE_OBJECT_LIST |
| 12772 | HandleScope scope; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12773 | CONVERT_SMI_ARG_CHECKED(obj_id1, 0); |
| 12774 | CONVERT_SMI_ARG_CHECKED(obj_id2, 1); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12775 | RUNTIME_ASSERT(args[2]->IsUndefined() || args[2]->IsJSObject()); |
| 12776 | |
| 12777 | Handle<JSObject> instance_filter; |
| 12778 | if (args[2]->IsJSObject()) { |
| 12779 | instance_filter = args.at<JSObject>(2); |
| 12780 | } |
| 12781 | |
| 12782 | Object* result = |
| 12783 | LiveObjectList::GetPath(obj_id1, obj_id2, instance_filter); |
| 12784 | return result; |
| 12785 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12786 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12787 | #endif |
| 12788 | } |
| 12789 | |
| 12790 | |
| 12791 | // Generates the response to a debugger request for a list of all |
| 12792 | // previously captured live object lists. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12793 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InfoLOL) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12794 | #ifdef LIVE_OBJECT_LIST |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12795 | CONVERT_SMI_ARG_CHECKED(start, 0); |
| 12796 | CONVERT_SMI_ARG_CHECKED(count, 1); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12797 | return LiveObjectList::Info(start, count); |
| 12798 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12799 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12800 | #endif |
| 12801 | } |
| 12802 | |
| 12803 | |
| 12804 | // Gets a dump of the specified object as requested by the debugger. |
| 12805 | // This is only used for obj ids shown in live object lists. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12806 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PrintLOLObj) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12807 | #ifdef LIVE_OBJECT_LIST |
| 12808 | HandleScope scope; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12809 | CONVERT_SMI_ARG_CHECKED(obj_id, 0); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12810 | Object* result = LiveObjectList::PrintObj(obj_id); |
| 12811 | return result; |
| 12812 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12813 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12814 | #endif |
| 12815 | } |
| 12816 | |
| 12817 | |
| 12818 | // Resets and releases all previously captured live object lists. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12819 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ResetLOL) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12820 | #ifdef LIVE_OBJECT_LIST |
| 12821 | LiveObjectList::Reset(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12822 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12823 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12824 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12825 | #endif |
| 12826 | } |
| 12827 | |
| 12828 | |
| 12829 | // Generates the response to a debugger request for a summary of the types |
| 12830 | // of objects in the difference between the captured live object lists |
| 12831 | // specified by id1 and id2. |
| 12832 | // If id1 is 0 (i.e. not a valid lol), then the whole of lol id2 will be |
| 12833 | // summarized. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12834 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SummarizeLOL) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12835 | #ifdef LIVE_OBJECT_LIST |
| 12836 | HandleScope scope; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12837 | CONVERT_SMI_ARG_CHECKED(id1, 0); |
| 12838 | CONVERT_SMI_ARG_CHECKED(id2, 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12839 | CONVERT_ARG_HANDLE_CHECKED(JSObject, filter_obj, 2); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12840 | |
| 12841 | EnterDebugger enter_debugger; |
| 12842 | return LiveObjectList::Summarize(id1, id2, filter_obj); |
| 12843 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12844 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12845 | #endif |
| 12846 | } |
| 12847 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12848 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 12849 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 12850 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12851 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ProfilerResume) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 12852 | NoHandleAllocation ha; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12853 | v8::V8::ResumeProfiler(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12854 | return isolate->heap()->undefined_value(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 12855 | } |
| 12856 | |
| 12857 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12858 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ProfilerPause) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 12859 | NoHandleAllocation ha; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12860 | v8::V8::PauseProfiler(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12861 | return isolate->heap()->undefined_value(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 12862 | } |
| 12863 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12864 | |
| 12865 | // Finds the script object from the script data. NOTE: This operation uses |
| 12866 | // heap traversal to find the function generated for the source position |
| 12867 | // for the requested break point. For lazily compiled functions several heap |
| 12868 | // traversals might be required rendering this operation as a rather slow |
| 12869 | // operation. However for setting break points which is normally done through |
| 12870 | // some kind of user interaction the performance is not crucial. |
| 12871 | static Handle<Object> Runtime_GetScriptFromScriptName( |
| 12872 | Handle<String> script_name) { |
| 12873 | // Scan the heap for Script objects to find the script with the requested |
| 12874 | // script data. |
| 12875 | Handle<Script> script; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12876 | script_name->GetHeap()->EnsureHeapIsIterable(); |
| 12877 | AssertNoAllocation no_allocation_during_heap_iteration; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12878 | HeapIterator iterator; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 12879 | HeapObject* obj = NULL; |
| 12880 | while (script.is_null() && ((obj = iterator.next()) != NULL)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12881 | // If a script is found check if it has the script data requested. |
| 12882 | if (obj->IsScript()) { |
| 12883 | if (Script::cast(obj)->name()->IsString()) { |
| 12884 | if (String::cast(Script::cast(obj)->name())->Equals(*script_name)) { |
| 12885 | script = Handle<Script>(Script::cast(obj)); |
| 12886 | } |
| 12887 | } |
| 12888 | } |
| 12889 | } |
| 12890 | |
| 12891 | // If no script with the requested script data is found return undefined. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12892 | if (script.is_null()) return FACTORY->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12893 | |
| 12894 | // Return the script found. |
| 12895 | return GetScriptWrapper(script); |
| 12896 | } |
| 12897 | |
| 12898 | |
| 12899 | // Get the script object from script data. NOTE: Regarding performance |
| 12900 | // see the NOTE for GetScriptFromScriptData. |
| 12901 | // args[0]: script data for the script to find the source for |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12902 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetScript) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12903 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12904 | |
| 12905 | ASSERT(args.length() == 1); |
| 12906 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12907 | CONVERT_ARG_CHECKED(String, script_name, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12908 | |
| 12909 | // Find the requested script. |
| 12910 | Handle<Object> result = |
| 12911 | Runtime_GetScriptFromScriptName(Handle<String>(script_name)); |
| 12912 | return *result; |
| 12913 | } |
| 12914 | |
| 12915 | |
| 12916 | // Determines whether the given stack frame should be displayed in |
| 12917 | // a stack trace. The caller is the error constructor that asked |
| 12918 | // for the stack trace to be collected. The first time a construct |
| 12919 | // call to this function is encountered it is skipped. The seen_caller |
| 12920 | // in/out parameter is used to remember if the caller has been seen |
| 12921 | // yet. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12922 | static bool ShowFrameInStackTrace(StackFrame* raw_frame, |
| 12923 | Object* caller, |
| 12924 | bool* seen_caller) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12925 | // Only display JS frames. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12926 | if (!raw_frame->is_java_script()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12927 | return false; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12928 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12929 | JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame); |
| 12930 | Object* raw_fun = frame->function(); |
| 12931 | // Not sure when this can happen but skip it just in case. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12932 | if (!raw_fun->IsJSFunction()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12933 | return false; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12934 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12935 | if ((raw_fun == caller) && !(*seen_caller)) { |
| 12936 | *seen_caller = true; |
| 12937 | return false; |
| 12938 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12939 | // Skip all frames until we've seen the caller. |
| 12940 | if (!(*seen_caller)) return false; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12941 | // Also, skip non-visible built-in functions and any call with the builtins |
| 12942 | // object as receiver, so as to not reveal either the builtins object or |
| 12943 | // an internal function. |
| 12944 | // The --builtins-in-stack-traces command line flag allows including |
| 12945 | // internal call sites in the stack trace for debugging purposes. |
| 12946 | if (!FLAG_builtins_in_stack_traces) { |
| 12947 | JSFunction* fun = JSFunction::cast(raw_fun); |
| 12948 | if (frame->receiver()->IsJSBuiltinsObject() || |
| 12949 | (fun->IsBuiltin() && !fun->shared()->native())) { |
| 12950 | return false; |
| 12951 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12952 | } |
| 12953 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12954 | } |
| 12955 | |
| 12956 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12957 | // Collect the raw data for a stack trace. Returns an array of 4 |
| 12958 | // element segments each containing a receiver, function, code and |
| 12959 | // native code offset. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12960 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CollectStackTrace) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 12961 | ASSERT_EQ(args.length(), 3); |
| 12962 | CONVERT_ARG_HANDLE_CHECKED(JSObject, error_object, 0); |
| 12963 | Handle<Object> caller = args.at<Object>(1); |
| 12964 | CONVERT_NUMBER_CHECKED(int32_t, limit, Int32, args[2]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12965 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12966 | HandleScope scope(isolate); |
| 12967 | Factory* factory = isolate->factory(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12968 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 12969 | limit = Max(limit, 0); // Ensure that limit is not negative. |
| 12970 | int initial_size = Min(limit, 10); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12971 | Handle<FixedArray> elements = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12972 | factory->NewFixedArrayWithHoles(initial_size * 4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12973 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12974 | StackFrameIterator iter(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12975 | // If the caller parameter is a function we skip frames until we're |
| 12976 | // under it before starting to collect. |
| 12977 | bool seen_caller = !caller->IsJSFunction(); |
| 12978 | int cursor = 0; |
| 12979 | int frames_seen = 0; |
| 12980 | while (!iter.done() && frames_seen < limit) { |
| 12981 | StackFrame* raw_frame = iter.frame(); |
| 12982 | if (ShowFrameInStackTrace(raw_frame, *caller, &seen_caller)) { |
| 12983 | frames_seen++; |
| 12984 | JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12985 | // Set initial size to the maximum inlining level + 1 for the outermost |
| 12986 | // function. |
| 12987 | List<FrameSummary> frames(Compiler::kMaxInliningLevels + 1); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12988 | frame->Summarize(&frames); |
| 12989 | for (int i = frames.length() - 1; i >= 0; i--) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12990 | if (cursor + 4 > elements->length()) { |
| 12991 | int new_capacity = JSObject::NewElementsCapacity(elements->length()); |
| 12992 | Handle<FixedArray> new_elements = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12993 | factory->NewFixedArrayWithHoles(new_capacity); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12994 | for (int i = 0; i < cursor; i++) { |
| 12995 | new_elements->set(i, elements->get(i)); |
| 12996 | } |
| 12997 | elements = new_elements; |
| 12998 | } |
| 12999 | ASSERT(cursor + 4 <= elements->length()); |
| 13000 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13001 | Handle<Object> recv = frames[i].receiver(); |
| 13002 | Handle<JSFunction> fun = frames[i].function(); |
| 13003 | Handle<Code> code = frames[i].code(); |
| 13004 | Handle<Smi> offset(Smi::FromInt(frames[i].offset())); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13005 | elements->set(cursor++, *recv); |
| 13006 | elements->set(cursor++, *fun); |
| 13007 | elements->set(cursor++, *code); |
| 13008 | elements->set(cursor++, *offset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13009 | } |
| 13010 | } |
| 13011 | iter.Advance(); |
| 13012 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13013 | Handle<JSArray> result = factory->NewJSArrayWithElements(elements); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13014 | // Capture and attach a more detailed stack trace if necessary. |
| 13015 | isolate->CaptureAndSetCurrentStackTraceFor(error_object); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 13016 | result->set_length(Smi::FromInt(cursor)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13017 | return *result; |
| 13018 | } |
| 13019 | |
| 13020 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 13021 | // Returns V8 version as a string. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13022 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetV8Version) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 13023 | ASSERT_EQ(args.length(), 0); |
| 13024 | |
| 13025 | NoHandleAllocation ha; |
| 13026 | |
| 13027 | const char* version_string = v8::V8::GetVersion(); |
| 13028 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13029 | return isolate->heap()->AllocateStringFromAscii(CStrVector(version_string), |
| 13030 | NOT_TENURED); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 13031 | } |
| 13032 | |
| 13033 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13034 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Abort) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13035 | ASSERT(args.length() == 2); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13036 | OS::PrintError("abort: %s\n", |
| 13037 | reinterpret_cast<char*>(args[0]) + args.smi_at(1)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13038 | isolate->PrintStack(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13039 | OS::Abort(); |
| 13040 | UNREACHABLE(); |
| 13041 | return NULL; |
| 13042 | } |
| 13043 | |
| 13044 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13045 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFromCache) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13046 | // This is only called from codegen, so checks might be more lax. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13047 | CONVERT_ARG_CHECKED(JSFunctionResultCache, cache, 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13048 | Object* key = args[1]; |
| 13049 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13050 | int finger_index = cache->finger_index(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13051 | Object* o = cache->get(finger_index); |
| 13052 | if (o == key) { |
| 13053 | // The fastest case: hit the same place again. |
| 13054 | return cache->get(finger_index + 1); |
| 13055 | } |
| 13056 | |
| 13057 | for (int i = finger_index - 2; |
| 13058 | i >= JSFunctionResultCache::kEntriesIndex; |
| 13059 | i -= 2) { |
| 13060 | o = cache->get(i); |
| 13061 | if (o == key) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13062 | cache->set_finger_index(i); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13063 | return cache->get(i + 1); |
| 13064 | } |
| 13065 | } |
| 13066 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13067 | int size = cache->size(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13068 | ASSERT(size <= cache->length()); |
| 13069 | |
| 13070 | for (int i = size - 2; i > finger_index; i -= 2) { |
| 13071 | o = cache->get(i); |
| 13072 | if (o == key) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13073 | cache->set_finger_index(i); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13074 | return cache->get(i + 1); |
| 13075 | } |
| 13076 | } |
| 13077 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13078 | // There is no value in the cache. Invoke the function and cache result. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13079 | HandleScope scope(isolate); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13080 | |
| 13081 | Handle<JSFunctionResultCache> cache_handle(cache); |
| 13082 | Handle<Object> key_handle(key); |
| 13083 | Handle<Object> value; |
| 13084 | { |
| 13085 | Handle<JSFunction> factory(JSFunction::cast( |
| 13086 | cache_handle->get(JSFunctionResultCache::kFactoryIndex))); |
| 13087 | // TODO(antonm): consider passing a receiver when constructing a cache. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13088 | Handle<Object> receiver(isolate->global_context()->global()); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13089 | // This handle is nor shared, nor used later, so it's safe. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13090 | Handle<Object> argv[] = { key_handle }; |
| 13091 | bool pending_exception; |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13092 | value = Execution::Call(factory, |
| 13093 | receiver, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13094 | ARRAY_SIZE(argv), |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13095 | argv, |
| 13096 | &pending_exception); |
| 13097 | if (pending_exception) return Failure::Exception(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13098 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13099 | |
| 13100 | #ifdef DEBUG |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13101 | if (FLAG_verify_heap) { |
| 13102 | cache_handle->JSFunctionResultCacheVerify(); |
| 13103 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13104 | #endif |
| 13105 | |
| 13106 | // Function invocation may have cleared the cache. Reread all the data. |
| 13107 | finger_index = cache_handle->finger_index(); |
| 13108 | size = cache_handle->size(); |
| 13109 | |
| 13110 | // If we have spare room, put new data into it, otherwise evict post finger |
| 13111 | // entry which is likely to be the least recently used. |
| 13112 | int index = -1; |
| 13113 | if (size < cache_handle->length()) { |
| 13114 | cache_handle->set_size(size + JSFunctionResultCache::kEntrySize); |
| 13115 | index = size; |
| 13116 | } else { |
| 13117 | index = finger_index + JSFunctionResultCache::kEntrySize; |
| 13118 | if (index == cache_handle->length()) { |
| 13119 | index = JSFunctionResultCache::kEntriesIndex; |
| 13120 | } |
| 13121 | } |
| 13122 | |
| 13123 | ASSERT(index % 2 == 0); |
| 13124 | ASSERT(index >= JSFunctionResultCache::kEntriesIndex); |
| 13125 | ASSERT(index < cache_handle->length()); |
| 13126 | |
| 13127 | cache_handle->set(index, *key_handle); |
| 13128 | cache_handle->set(index + 1, *value); |
| 13129 | cache_handle->set_finger_index(index); |
| 13130 | |
| 13131 | #ifdef DEBUG |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13132 | if (FLAG_verify_heap) { |
| 13133 | cache_handle->JSFunctionResultCacheVerify(); |
| 13134 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13135 | #endif |
| 13136 | |
| 13137 | return *value; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13138 | } |
| 13139 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 13140 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13141 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewMessageObject) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13142 | HandleScope scope(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13143 | CONVERT_ARG_HANDLE_CHECKED(String, type, 0); |
| 13144 | CONVERT_ARG_HANDLE_CHECKED(JSArray, arguments, 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13145 | return *isolate->factory()->NewJSMessageObject( |
| 13146 | type, |
| 13147 | arguments, |
| 13148 | 0, |
| 13149 | 0, |
| 13150 | isolate->factory()->undefined_value(), |
| 13151 | isolate->factory()->undefined_value(), |
| 13152 | isolate->factory()->undefined_value()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 13153 | } |
| 13154 | |
| 13155 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13156 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetType) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13157 | CONVERT_ARG_CHECKED(JSMessageObject, message, 0); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 13158 | return message->type(); |
| 13159 | } |
| 13160 | |
| 13161 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13162 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetArguments) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13163 | CONVERT_ARG_CHECKED(JSMessageObject, message, 0); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 13164 | return message->arguments(); |
| 13165 | } |
| 13166 | |
| 13167 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13168 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetStartPosition) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13169 | CONVERT_ARG_CHECKED(JSMessageObject, message, 0); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 13170 | return Smi::FromInt(message->start_position()); |
| 13171 | } |
| 13172 | |
| 13173 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13174 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetScript) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13175 | CONVERT_ARG_CHECKED(JSMessageObject, message, 0); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 13176 | return message->script(); |
| 13177 | } |
| 13178 | |
| 13179 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13180 | #ifdef DEBUG |
| 13181 | // ListNatives is ONLY used by the fuzz-natives.js in debug mode |
| 13182 | // Exclude the code in release mode. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13183 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ListNatives) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13184 | ASSERT(args.length() == 0); |
| 13185 | HandleScope scope; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13186 | #define COUNT_ENTRY(Name, argc, ressize) + 1 |
| 13187 | int entry_count = 0 |
| 13188 | RUNTIME_FUNCTION_LIST(COUNT_ENTRY) |
| 13189 | INLINE_FUNCTION_LIST(COUNT_ENTRY) |
| 13190 | INLINE_RUNTIME_FUNCTION_LIST(COUNT_ENTRY); |
| 13191 | #undef COUNT_ENTRY |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13192 | Factory* factory = isolate->factory(); |
| 13193 | Handle<FixedArray> elements = factory->NewFixedArray(entry_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13194 | int index = 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13195 | bool inline_runtime_functions = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13196 | #define ADD_ENTRY(Name, argc, ressize) \ |
| 13197 | { \ |
| 13198 | HandleScope inner; \ |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13199 | Handle<String> name; \ |
| 13200 | /* Inline runtime functions have an underscore in front of the name. */ \ |
| 13201 | if (inline_runtime_functions) { \ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13202 | name = factory->NewStringFromAscii( \ |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13203 | Vector<const char>("_" #Name, StrLength("_" #Name))); \ |
| 13204 | } else { \ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13205 | name = factory->NewStringFromAscii( \ |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13206 | Vector<const char>(#Name, StrLength(#Name))); \ |
| 13207 | } \ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13208 | Handle<FixedArray> pair_elements = factory->NewFixedArray(2); \ |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13209 | pair_elements->set(0, *name); \ |
| 13210 | pair_elements->set(1, Smi::FromInt(argc)); \ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13211 | Handle<JSArray> pair = factory->NewJSArrayWithElements(pair_elements); \ |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13212 | elements->set(index++, *pair); \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13213 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13214 | inline_runtime_functions = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13215 | RUNTIME_FUNCTION_LIST(ADD_ENTRY) |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13216 | inline_runtime_functions = true; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13217 | INLINE_FUNCTION_LIST(ADD_ENTRY) |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13218 | INLINE_RUNTIME_FUNCTION_LIST(ADD_ENTRY) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13219 | #undef ADD_ENTRY |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13220 | ASSERT_EQ(index, entry_count); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13221 | Handle<JSArray> result = factory->NewJSArrayWithElements(elements); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13222 | return *result; |
| 13223 | } |
| 13224 | #endif |
| 13225 | |
| 13226 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13227 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Log) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13228 | ASSERT(args.length() == 2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13229 | CONVERT_ARG_CHECKED(String, format, 0); |
| 13230 | CONVERT_ARG_CHECKED(JSArray, elms, 1); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 13231 | String::FlatContent format_content = format->GetFlatContent(); |
| 13232 | RUNTIME_ASSERT(format_content.IsAscii()); |
| 13233 | Vector<const char> chars = format_content.ToAsciiVector(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13234 | LOGGER->LogRuntime(chars, elms); |
| 13235 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13236 | } |
| 13237 | |
| 13238 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13239 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IS_VAR) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13240 | UNREACHABLE(); // implemented as macro in the parser |
| 13241 | return NULL; |
| 13242 | } |
| 13243 | |
| 13244 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13245 | #define ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(Name) \ |
| 13246 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Has##Name) { \ |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13247 | CONVERT_ARG_CHECKED(JSObject, obj, 0); \ |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13248 | return isolate->heap()->ToBoolean(obj->Has##Name()); \ |
| 13249 | } |
| 13250 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13251 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(FastSmiOnlyElements) |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13252 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(FastElements) |
| 13253 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(FastDoubleElements) |
| 13254 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(DictionaryElements) |
| 13255 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalPixelElements) |
| 13256 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalArrayElements) |
| 13257 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalByteElements) |
| 13258 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalUnsignedByteElements) |
| 13259 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalShortElements) |
| 13260 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalUnsignedShortElements) |
| 13261 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalIntElements) |
| 13262 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalUnsignedIntElements) |
| 13263 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalFloatElements) |
| 13264 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalDoubleElements) |
| 13265 | |
| 13266 | #undef ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION |
| 13267 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13268 | |
| 13269 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HaveSameMap) { |
| 13270 | ASSERT(args.length() == 2); |
| 13271 | CONVERT_ARG_CHECKED(JSObject, obj1, 0); |
| 13272 | CONVERT_ARG_CHECKED(JSObject, obj2, 1); |
| 13273 | return isolate->heap()->ToBoolean(obj1->map() == obj2->map()); |
| 13274 | } |
| 13275 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13276 | // ---------------------------------------------------------------------------- |
| 13277 | // Implementation of Runtime |
| 13278 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13279 | #define F(name, number_of_args, result_size) \ |
| 13280 | { Runtime::k##name, Runtime::RUNTIME, #name, \ |
| 13281 | FUNCTION_ADDR(Runtime_##name), number_of_args, result_size }, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13282 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13283 | |
| 13284 | #define I(name, number_of_args, result_size) \ |
| 13285 | { Runtime::kInline##name, Runtime::INLINE, \ |
| 13286 | "_" #name, NULL, number_of_args, result_size }, |
| 13287 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13288 | static const Runtime::Function kIntrinsicFunctions[] = { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13289 | RUNTIME_FUNCTION_LIST(F) |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13290 | INLINE_FUNCTION_LIST(I) |
| 13291 | INLINE_RUNTIME_FUNCTION_LIST(I) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13292 | }; |
| 13293 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13294 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13295 | MaybeObject* Runtime::InitializeIntrinsicFunctionNames(Heap* heap, |
| 13296 | Object* dictionary) { |
| 13297 | ASSERT(Isolate::Current()->heap() == heap); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13298 | ASSERT(dictionary != NULL); |
| 13299 | ASSERT(StringDictionary::cast(dictionary)->NumberOfElements() == 0); |
| 13300 | for (int i = 0; i < kNumFunctions; ++i) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 13301 | Object* name_symbol; |
| 13302 | { MaybeObject* maybe_name_symbol = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13303 | heap->LookupAsciiSymbol(kIntrinsicFunctions[i].name); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 13304 | if (!maybe_name_symbol->ToObject(&name_symbol)) return maybe_name_symbol; |
| 13305 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13306 | StringDictionary* string_dictionary = StringDictionary::cast(dictionary); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 13307 | { MaybeObject* maybe_dictionary = string_dictionary->Add( |
| 13308 | String::cast(name_symbol), |
| 13309 | Smi::FromInt(i), |
| 13310 | PropertyDetails(NONE, NORMAL)); |
| 13311 | if (!maybe_dictionary->ToObject(&dictionary)) { |
| 13312 | // Non-recoverable failure. Calling code must restart heap |
| 13313 | // initialization. |
| 13314 | return maybe_dictionary; |
| 13315 | } |
| 13316 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13317 | } |
| 13318 | return dictionary; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13319 | } |
| 13320 | |
| 13321 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13322 | const Runtime::Function* Runtime::FunctionForSymbol(Handle<String> name) { |
| 13323 | Heap* heap = name->GetHeap(); |
| 13324 | int entry = heap->intrinsic_function_names()->FindEntry(*name); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13325 | if (entry != kNotFound) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13326 | Object* smi_index = heap->intrinsic_function_names()->ValueAt(entry); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13327 | int function_index = Smi::cast(smi_index)->value(); |
| 13328 | return &(kIntrinsicFunctions[function_index]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13329 | } |
| 13330 | return NULL; |
| 13331 | } |
| 13332 | |
| 13333 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13334 | const Runtime::Function* Runtime::FunctionForId(Runtime::FunctionId id) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13335 | return &(kIntrinsicFunctions[static_cast<int>(id)]); |
| 13336 | } |
| 13337 | |
| 13338 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13339 | void Runtime::PerformGC(Object* result) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13340 | Isolate* isolate = Isolate::Current(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13341 | Failure* failure = Failure::cast(result); |
| 13342 | if (failure->IsRetryAfterGC()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13343 | if (isolate->heap()->new_space()->AddFreshPage()) { |
| 13344 | return; |
| 13345 | } |
| 13346 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13347 | // Try to do a garbage collection; ignore it if it fails. The C |
| 13348 | // entry stub will throw an out-of-memory exception in that case. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13349 | isolate->heap()->CollectGarbage(failure->allocation_space(), |
| 13350 | "Runtime::PerformGC"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13351 | } else { |
| 13352 | // Handle last resort GC and make sure to allow future allocations |
| 13353 | // to grow the heap without causing GCs (if possible). |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13354 | isolate->counters()->gc_last_resort_from_js()->Increment(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 13355 | isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, |
| 13356 | "Runtime::PerformGC"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13357 | } |
| 13358 | } |
| 13359 | |
| 13360 | |
| 13361 | } } // namespace v8::internal |