Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [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" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 43 | #include "execution.h" |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 44 | #include "global-handles.h" |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 45 | #include "isolate-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 46 | #include "jsregexp.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 47 | #include "json-parser.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 48 | #include "liveedit.h" |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 49 | #include "liveobjectlist-inl.h" |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 50 | #include "misc-intrinsics.h" |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 51 | #include "parser.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 52 | #include "platform.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 53 | #include "runtime-profiler.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 54 | #include "runtime.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 55 | #include "scopeinfo.h" |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 56 | #include "smart-array-pointer.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 57 | #include "string-search.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 58 | #include "stub-cache.h" |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 59 | #include "v8threads.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 60 | #include "vm-state-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 61 | |
| 62 | namespace v8 { |
| 63 | namespace internal { |
| 64 | |
| 65 | |
| 66 | #define RUNTIME_ASSERT(value) \ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 67 | if (!(value)) return isolate->ThrowIllegalOperation(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 68 | |
| 69 | // Cast the given object to a value of the specified type and store |
| 70 | // it in a variable with the given name. If the object is not of the |
| 71 | // expected type call IllegalOperation and return. |
| 72 | #define CONVERT_CHECKED(Type, name, obj) \ |
| 73 | RUNTIME_ASSERT(obj->Is##Type()); \ |
| 74 | Type* name = Type::cast(obj); |
| 75 | |
| 76 | #define CONVERT_ARG_CHECKED(Type, name, index) \ |
| 77 | RUNTIME_ASSERT(args[index]->Is##Type()); \ |
| 78 | Handle<Type> name = args.at<Type>(index); |
| 79 | |
| 80 | // Cast the given object to a boolean and store it in a variable with |
| 81 | // the given name. If the object is not a boolean call IllegalOperation |
| 82 | // and return. |
| 83 | #define CONVERT_BOOLEAN_CHECKED(name, obj) \ |
| 84 | RUNTIME_ASSERT(obj->IsBoolean()); \ |
| 85 | bool name = (obj)->IsTrue(); |
| 86 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 87 | // Cast the given argument to a Smi and store its value in an int variable |
| 88 | // 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] | 89 | // and return. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 90 | #define CONVERT_SMI_ARG_CHECKED(name, index) \ |
| 91 | RUNTIME_ASSERT(args[index]->IsSmi()); \ |
| 92 | int name = args.smi_at(index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 93 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 94 | // Cast the given argument to a double and store it in a variable with |
| 95 | // 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] | 96 | // the number not-a-number) call IllegalOperation and return. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 97 | #define CONVERT_DOUBLE_ARG_CHECKED(name, index) \ |
| 98 | RUNTIME_ASSERT(args[index]->IsNumber()); \ |
| 99 | double name = args.number_at(index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 100 | |
| 101 | // Call the specified converter on the object *comand store the result in |
| 102 | // a variable of the specified type with the given name. If the |
| 103 | // object is not a Number call IllegalOperation and return. |
| 104 | #define CONVERT_NUMBER_CHECKED(type, name, Type, obj) \ |
| 105 | RUNTIME_ASSERT(obj->IsNumber()); \ |
| 106 | type name = NumberTo##Type(obj); |
| 107 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 108 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 109 | // Assert that the given argument has a valid value for a StrictModeFlag |
| 110 | // and store it in a StrictModeFlag variable with the given name. |
| 111 | #define CONVERT_STRICT_MODE_ARG(name, index) \ |
| 112 | ASSERT(args[index]->IsSmi()); \ |
| 113 | ASSERT(args.smi_at(index) == kStrictMode || \ |
| 114 | args.smi_at(index) == kNonStrictMode); \ |
| 115 | StrictModeFlag name = \ |
| 116 | static_cast<StrictModeFlag>(args.smi_at(index)); |
| 117 | |
| 118 | |
| 119 | // Assert that the given argument has a valid value for a LanguageMode |
| 120 | // and store it in a LanguageMode variable with the given name. |
| 121 | #define CONVERT_LANGUAGE_MODE_ARG(name, index) \ |
| 122 | ASSERT(args[index]->IsSmi()); \ |
| 123 | ASSERT(args.smi_at(index) == CLASSIC_MODE || \ |
| 124 | args.smi_at(index) == STRICT_MODE || \ |
| 125 | args.smi_at(index) == EXTENDED_MODE); \ |
| 126 | LanguageMode name = \ |
| 127 | static_cast<LanguageMode>(args.smi_at(index)); |
| 128 | |
| 129 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 130 | MUST_USE_RESULT static MaybeObject* DeepCopyBoilerplate(Isolate* isolate, |
| 131 | JSObject* boilerplate) { |
| 132 | StackLimitCheck check(isolate); |
| 133 | if (check.HasOverflowed()) return isolate->StackOverflow(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 134 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 135 | Heap* heap = isolate->heap(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 136 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 137 | { MaybeObject* maybe_result = heap->CopyJSObject(boilerplate); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 138 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 139 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 140 | JSObject* copy = JSObject::cast(result); |
| 141 | |
| 142 | // Deep copy local properties. |
| 143 | if (copy->HasFastProperties()) { |
| 144 | FixedArray* properties = copy->properties(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 145 | for (int i = 0; i < properties->length(); i++) { |
| 146 | Object* value = properties->get(i); |
| 147 | if (value->IsJSObject()) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 148 | JSObject* js_object = JSObject::cast(value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 149 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 150 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 151 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 152 | properties->set(i, result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 155 | int nof = copy->map()->inobject_properties(); |
| 156 | for (int i = 0; i < nof; i++) { |
| 157 | Object* value = copy->InObjectPropertyAt(i); |
| 158 | if (value->IsJSObject()) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 159 | JSObject* js_object = JSObject::cast(value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 160 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 161 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 162 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 163 | copy->InObjectPropertyAtPut(i, result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 167 | { MaybeObject* maybe_result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 168 | heap->AllocateFixedArray(copy->NumberOfLocalProperties(NONE)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 169 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 170 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 171 | FixedArray* names = FixedArray::cast(result); |
| 172 | copy->GetLocalPropertyNames(names, 0); |
| 173 | for (int i = 0; i < names->length(); i++) { |
| 174 | ASSERT(names->get(i)->IsString()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 175 | String* key_string = String::cast(names->get(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 176 | PropertyAttributes attributes = |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 177 | copy->GetLocalPropertyAttribute(key_string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 178 | // Only deep copy fields from the object literal expression. |
| 179 | // In particular, don't try to copy the length attribute of |
| 180 | // an array. |
| 181 | if (attributes != NONE) continue; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 182 | Object* value = |
| 183 | copy->GetProperty(key_string, &attributes)->ToObjectUnchecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 184 | if (value->IsJSObject()) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 185 | JSObject* js_object = JSObject::cast(value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 186 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 187 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 188 | } |
| 189 | { MaybeObject* maybe_result = |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 190 | // Creating object copy for literals. No strict mode needed. |
| 191 | copy->SetProperty(key_string, result, NONE, kNonStrictMode); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 192 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 193 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Deep copy local elements. |
| 199 | // Pixel elements cannot be created using an object literal. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 200 | ASSERT(!copy->HasExternalArrayElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 201 | switch (copy->GetElementsKind()) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 202 | case FAST_SMI_ONLY_ELEMENTS: |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 203 | case FAST_ELEMENTS: { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 204 | FixedArray* elements = FixedArray::cast(copy->elements()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 205 | if (elements->map() == heap->fixed_cow_array_map()) { |
| 206 | isolate->counters()->cow_arrays_created_runtime()->Increment(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 207 | #ifdef DEBUG |
| 208 | for (int i = 0; i < elements->length(); i++) { |
| 209 | ASSERT(!elements->get(i)->IsJSObject()); |
| 210 | } |
| 211 | #endif |
| 212 | } else { |
| 213 | for (int i = 0; i < elements->length(); i++) { |
| 214 | Object* value = elements->get(i); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 215 | ASSERT(value->IsSmi() || |
| 216 | value->IsTheHole() || |
| 217 | (copy->GetElementsKind() == FAST_ELEMENTS)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 218 | if (value->IsJSObject()) { |
| 219 | JSObject* js_object = JSObject::cast(value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 220 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, |
| 221 | js_object); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 222 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 223 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 224 | elements->set(i, result); |
| 225 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | break; |
| 229 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 230 | case DICTIONARY_ELEMENTS: { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 231 | SeededNumberDictionary* element_dictionary = copy->element_dictionary(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 232 | int capacity = element_dictionary->Capacity(); |
| 233 | for (int i = 0; i < capacity; i++) { |
| 234 | Object* k = element_dictionary->KeyAt(i); |
| 235 | if (element_dictionary->IsKey(k)) { |
| 236 | Object* value = element_dictionary->ValueAt(i); |
| 237 | if (value->IsJSObject()) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 238 | JSObject* js_object = JSObject::cast(value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 239 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, |
| 240 | js_object); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 241 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 242 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 243 | element_dictionary->ValueAtPut(i, result); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | break; |
| 248 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 249 | case NON_STRICT_ARGUMENTS_ELEMENTS: |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 250 | UNIMPLEMENTED(); |
| 251 | break; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 252 | case EXTERNAL_PIXEL_ELEMENTS: |
| 253 | case EXTERNAL_BYTE_ELEMENTS: |
| 254 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 255 | case EXTERNAL_SHORT_ELEMENTS: |
| 256 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 257 | case EXTERNAL_INT_ELEMENTS: |
| 258 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 259 | case EXTERNAL_FLOAT_ELEMENTS: |
| 260 | case EXTERNAL_DOUBLE_ELEMENTS: |
| 261 | case FAST_DOUBLE_ELEMENTS: |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 262 | // No contained objects, nothing to do. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 263 | break; |
| 264 | } |
| 265 | return copy; |
| 266 | } |
| 267 | |
| 268 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 269 | static Handle<Map> ComputeObjectLiteralMap( |
| 270 | Handle<Context> context, |
| 271 | Handle<FixedArray> constant_properties, |
| 272 | bool* is_result_from_cache) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 273 | Isolate* isolate = context->GetIsolate(); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 274 | int properties_length = constant_properties->length(); |
| 275 | int number_of_properties = properties_length / 2; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 276 | if (FLAG_canonicalize_object_literal_maps) { |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 277 | // Check that there are only symbols and array indices among keys. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 278 | int number_of_symbol_keys = 0; |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 279 | for (int p = 0; p != properties_length; p += 2) { |
| 280 | Object* key = constant_properties->get(p); |
| 281 | uint32_t element_index = 0; |
| 282 | if (key->IsSymbol()) { |
| 283 | number_of_symbol_keys++; |
| 284 | } else if (key->ToArrayIndex(&element_index)) { |
| 285 | // An index key does not require space in the property backing store. |
| 286 | number_of_properties--; |
| 287 | } else { |
| 288 | // Bail out as a non-symbol non-index key makes caching impossible. |
| 289 | // ASSERT to make sure that the if condition after the loop is false. |
| 290 | ASSERT(number_of_symbol_keys != number_of_properties); |
| 291 | break; |
| 292 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 293 | } |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 294 | // If we only have symbols and array indices among keys then we can |
| 295 | // use the map cache in the global context. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 296 | const int kMaxKeys = 10; |
| 297 | if ((number_of_symbol_keys == number_of_properties) && |
| 298 | (number_of_symbol_keys < kMaxKeys)) { |
| 299 | // Create the fixed array with the key. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 300 | Handle<FixedArray> keys = |
| 301 | isolate->factory()->NewFixedArray(number_of_symbol_keys); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 302 | if (number_of_symbol_keys > 0) { |
| 303 | int index = 0; |
| 304 | for (int p = 0; p < properties_length; p += 2) { |
| 305 | Object* key = constant_properties->get(p); |
| 306 | if (key->IsSymbol()) { |
| 307 | keys->set(index++, key); |
| 308 | } |
| 309 | } |
| 310 | ASSERT(index == number_of_symbol_keys); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 311 | } |
| 312 | *is_result_from_cache = true; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 313 | return isolate->factory()->ObjectLiteralMapFromCache(context, keys); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | *is_result_from_cache = false; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 317 | return isolate->factory()->CopyMap( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 318 | Handle<Map>(context->object_function()->initial_map()), |
| 319 | number_of_properties); |
| 320 | } |
| 321 | |
| 322 | |
| 323 | static Handle<Object> CreateLiteralBoilerplate( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 324 | Isolate* isolate, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 325 | Handle<FixedArray> literals, |
| 326 | Handle<FixedArray> constant_properties); |
| 327 | |
| 328 | |
| 329 | static Handle<Object> CreateObjectLiteralBoilerplate( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 330 | Isolate* isolate, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 331 | Handle<FixedArray> literals, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 332 | Handle<FixedArray> constant_properties, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 333 | bool should_have_fast_elements, |
| 334 | bool has_function_literal) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 335 | // Get the global context from the literals array. This is the |
| 336 | // context in which the function was created and we use the object |
| 337 | // function from this context to create the object literal. We do |
| 338 | // not use the object function from the current global context |
| 339 | // because this might be the object function from another context |
| 340 | // which we should not have access to. |
| 341 | Handle<Context> context = |
| 342 | Handle<Context>(JSFunction::GlobalContextFromLiterals(*literals)); |
| 343 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 344 | // In case we have function literals, we want the object to be in |
| 345 | // slow properties mode for now. We don't go in the map cache because |
| 346 | // maps with constant functions can't be shared if the functions are |
| 347 | // not the same (which is the common case). |
| 348 | bool is_result_from_cache = false; |
| 349 | Handle<Map> map = has_function_literal |
| 350 | ? Handle<Map>(context->object_function()->initial_map()) |
| 351 | : ComputeObjectLiteralMap(context, |
| 352 | constant_properties, |
| 353 | &is_result_from_cache); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 354 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 355 | Handle<JSObject> boilerplate = isolate->factory()->NewJSObjectFromMap(map); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 356 | |
| 357 | // Normalize the elements of the boilerplate to save space if needed. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 358 | if (!should_have_fast_elements) JSObject::NormalizeElements(boilerplate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 359 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 360 | // Add the constant properties to the boilerplate. |
| 361 | int length = constant_properties->length(); |
| 362 | bool should_transform = |
| 363 | !is_result_from_cache && boilerplate->HasFastProperties(); |
| 364 | if (should_transform || has_function_literal) { |
| 365 | // Normalize the properties of object to avoid n^2 behavior |
| 366 | // when extending the object multiple properties. Indicate the number of |
| 367 | // properties to be added. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 368 | JSObject::NormalizeProperties( |
| 369 | boilerplate, KEEP_INOBJECT_PROPERTIES, length / 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | for (int index = 0; index < length; index +=2) { |
| 373 | Handle<Object> key(constant_properties->get(index+0), isolate); |
| 374 | Handle<Object> value(constant_properties->get(index+1), isolate); |
| 375 | if (value->IsFixedArray()) { |
| 376 | // The value contains the constant_properties of a |
| 377 | // simple object or array literal. |
| 378 | Handle<FixedArray> array = Handle<FixedArray>::cast(value); |
| 379 | value = CreateLiteralBoilerplate(isolate, literals, array); |
| 380 | if (value.is_null()) return value; |
| 381 | } |
| 382 | Handle<Object> result; |
| 383 | uint32_t element_index = 0; |
| 384 | if (key->IsSymbol()) { |
| 385 | if (Handle<String>::cast(key)->AsArrayIndex(&element_index)) { |
| 386 | // Array index as string (uint32). |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 387 | result = JSObject::SetOwnElement( |
| 388 | boilerplate, element_index, value, kNonStrictMode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 389 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 390 | Handle<String> name(String::cast(*key)); |
| 391 | ASSERT(!name->AsArrayIndex(&element_index)); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 392 | result = JSObject::SetLocalPropertyIgnoreAttributes( |
| 393 | boilerplate, name, value, NONE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 394 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 395 | } else if (key->ToArrayIndex(&element_index)) { |
| 396 | // Array index (uint32). |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 397 | result = JSObject::SetOwnElement( |
| 398 | boilerplate, element_index, value, kNonStrictMode); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 399 | } else { |
| 400 | // Non-uint32 number. |
| 401 | ASSERT(key->IsNumber()); |
| 402 | double num = key->Number(); |
| 403 | char arr[100]; |
| 404 | Vector<char> buffer(arr, ARRAY_SIZE(arr)); |
| 405 | const char* str = DoubleToCString(num, buffer); |
| 406 | Handle<String> name = |
| 407 | isolate->factory()->NewStringFromAscii(CStrVector(str)); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 408 | result = JSObject::SetLocalPropertyIgnoreAttributes( |
| 409 | boilerplate, name, value, NONE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 410 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 411 | // If setting the property on the boilerplate throws an |
| 412 | // exception, the exception is converted to an empty handle in |
| 413 | // the handle based operations. In that case, we need to |
| 414 | // convert back to an exception. |
| 415 | if (result.is_null()) return result; |
| 416 | } |
| 417 | |
| 418 | // Transform to fast properties if necessary. For object literals with |
| 419 | // containing function literals we defer this operation until after all |
| 420 | // computed properties have been assigned so that we can generate |
| 421 | // constant function properties. |
| 422 | if (should_transform && !has_function_literal) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 423 | JSObject::TransformToFastProperties( |
| 424 | boilerplate, boilerplate->map()->unused_property_fields()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | return boilerplate; |
| 428 | } |
| 429 | |
| 430 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 431 | static const int kSmiOnlyLiteralMinimumLength = 1024; |
| 432 | |
| 433 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 434 | Handle<Object> Runtime::CreateArrayLiteralBoilerplate( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 435 | Isolate* isolate, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 436 | Handle<FixedArray> literals, |
| 437 | Handle<FixedArray> elements) { |
| 438 | // Create the JSArray. |
| 439 | Handle<JSFunction> constructor( |
| 440 | JSFunction::GlobalContextFromLiterals(*literals)->array_function()); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 441 | Handle<JSArray> object = |
| 442 | Handle<JSArray>::cast(isolate->factory()->NewJSObject(constructor)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 443 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 444 | ElementsKind constant_elements_kind = |
| 445 | static_cast<ElementsKind>(Smi::cast(elements->get(0))->value()); |
| 446 | Handle<FixedArrayBase> constant_elements_values( |
| 447 | FixedArrayBase::cast(elements->get(1))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 448 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 449 | ASSERT(FLAG_smi_only_arrays || constant_elements_kind == FAST_ELEMENTS || |
| 450 | constant_elements_kind == FAST_SMI_ONLY_ELEMENTS); |
| 451 | bool allow_literal_kind_transition = FLAG_smi_only_arrays && |
| 452 | constant_elements_kind > object->GetElementsKind(); |
| 453 | |
| 454 | if (!FLAG_smi_only_arrays && |
| 455 | constant_elements_values->length() > kSmiOnlyLiteralMinimumLength && |
| 456 | constant_elements_kind != object->GetElementsKind()) { |
| 457 | allow_literal_kind_transition = true; |
| 458 | } |
| 459 | |
| 460 | // If the ElementsKind of the constant values of the array literal are less |
| 461 | // specific than the ElementsKind of the boilerplate array object, change the |
| 462 | // boilerplate array object's map to reflect that kind. |
| 463 | if (allow_literal_kind_transition) { |
| 464 | Handle<Map> transitioned_array_map = |
| 465 | isolate->factory()->GetElementsTransitionMap(object, |
| 466 | constant_elements_kind); |
| 467 | object->set_map(*transitioned_array_map); |
| 468 | } |
| 469 | |
| 470 | Handle<FixedArrayBase> copied_elements_values; |
| 471 | if (constant_elements_kind == FAST_DOUBLE_ELEMENTS) { |
| 472 | ASSERT(FLAG_smi_only_arrays); |
| 473 | copied_elements_values = isolate->factory()->CopyFixedDoubleArray( |
| 474 | Handle<FixedDoubleArray>::cast(constant_elements_values)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 475 | } else { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 476 | ASSERT(constant_elements_kind == FAST_SMI_ONLY_ELEMENTS || |
| 477 | constant_elements_kind == FAST_ELEMENTS); |
| 478 | const bool is_cow = |
| 479 | (constant_elements_values->map() == |
| 480 | isolate->heap()->fixed_cow_array_map()); |
| 481 | if (is_cow) { |
| 482 | copied_elements_values = constant_elements_values; |
| 483 | #if DEBUG |
| 484 | Handle<FixedArray> fixed_array_values = |
| 485 | Handle<FixedArray>::cast(copied_elements_values); |
| 486 | for (int i = 0; i < fixed_array_values->length(); i++) { |
| 487 | ASSERT(!fixed_array_values->get(i)->IsFixedArray()); |
| 488 | } |
| 489 | #endif |
| 490 | } else { |
| 491 | Handle<FixedArray> fixed_array_values = |
| 492 | Handle<FixedArray>::cast(constant_elements_values); |
| 493 | Handle<FixedArray> fixed_array_values_copy = |
| 494 | isolate->factory()->CopyFixedArray(fixed_array_values); |
| 495 | copied_elements_values = fixed_array_values_copy; |
| 496 | for (int i = 0; i < fixed_array_values->length(); i++) { |
| 497 | Object* current = fixed_array_values->get(i); |
| 498 | if (current->IsFixedArray()) { |
| 499 | // The value contains the constant_properties of a |
| 500 | // simple object or array literal. |
| 501 | Handle<FixedArray> fa(FixedArray::cast(fixed_array_values->get(i))); |
| 502 | Handle<Object> result = |
| 503 | CreateLiteralBoilerplate(isolate, literals, fa); |
| 504 | if (result.is_null()) return result; |
| 505 | fixed_array_values_copy->set(i, *result); |
| 506 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 507 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 508 | } |
| 509 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 510 | object->set_elements(*copied_elements_values); |
| 511 | object->set_length(Smi::FromInt(copied_elements_values->length())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 512 | return object; |
| 513 | } |
| 514 | |
| 515 | |
| 516 | static Handle<Object> CreateLiteralBoilerplate( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 517 | Isolate* isolate, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 518 | Handle<FixedArray> literals, |
| 519 | Handle<FixedArray> array) { |
| 520 | Handle<FixedArray> elements = CompileTimeValue::GetElements(array); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 521 | const bool kHasNoFunctionLiteral = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 522 | switch (CompileTimeValue::GetType(array)) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 523 | case CompileTimeValue::OBJECT_LITERAL_FAST_ELEMENTS: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 524 | return CreateObjectLiteralBoilerplate(isolate, |
| 525 | literals, |
| 526 | elements, |
| 527 | true, |
| 528 | kHasNoFunctionLiteral); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 529 | case CompileTimeValue::OBJECT_LITERAL_SLOW_ELEMENTS: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 530 | return CreateObjectLiteralBoilerplate(isolate, |
| 531 | literals, |
| 532 | elements, |
| 533 | false, |
| 534 | kHasNoFunctionLiteral); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 535 | case CompileTimeValue::ARRAY_LITERAL: |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 536 | return Runtime::CreateArrayLiteralBoilerplate( |
| 537 | isolate, literals, elements); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 538 | default: |
| 539 | UNREACHABLE(); |
| 540 | return Handle<Object>::null(); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 545 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateObjectLiteral) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 546 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 547 | ASSERT(args.length() == 4); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 548 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 549 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 550 | CONVERT_ARG_CHECKED(FixedArray, constant_properties, 2); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 551 | CONVERT_SMI_ARG_CHECKED(flags, 3); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 552 | bool should_have_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; |
| 553 | bool has_function_literal = (flags & ObjectLiteral::kHasFunction) != 0; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 554 | |
| 555 | // Check if boilerplate exists. If not, create it first. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 556 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 557 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 558 | boilerplate = CreateObjectLiteralBoilerplate(isolate, |
| 559 | literals, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 560 | constant_properties, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 561 | should_have_fast_elements, |
| 562 | has_function_literal); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 563 | if (boilerplate.is_null()) return Failure::Exception(); |
| 564 | // Update the functions literal and return the boilerplate. |
| 565 | literals->set(literals_index, *boilerplate); |
| 566 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 567 | return DeepCopyBoilerplate(isolate, JSObject::cast(*boilerplate)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 571 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateObjectLiteralShallow) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 572 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 573 | ASSERT(args.length() == 4); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 574 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 575 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 576 | CONVERT_ARG_CHECKED(FixedArray, constant_properties, 2); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 577 | CONVERT_SMI_ARG_CHECKED(flags, 3); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 578 | bool should_have_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; |
| 579 | bool has_function_literal = (flags & ObjectLiteral::kHasFunction) != 0; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 580 | |
| 581 | // Check if boilerplate exists. If not, create it first. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 582 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 583 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 584 | boilerplate = CreateObjectLiteralBoilerplate(isolate, |
| 585 | literals, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 586 | constant_properties, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 587 | should_have_fast_elements, |
| 588 | has_function_literal); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 589 | if (boilerplate.is_null()) return Failure::Exception(); |
| 590 | // Update the functions literal and return the boilerplate. |
| 591 | literals->set(literals_index, *boilerplate); |
| 592 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 593 | return isolate->heap()->CopyJSObject(JSObject::cast(*boilerplate)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 597 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateArrayLiteral) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 598 | HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 599 | ASSERT(args.length() == 3); |
| 600 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 601 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 602 | CONVERT_ARG_CHECKED(FixedArray, elements, 2); |
| 603 | |
| 604 | // Check if boilerplate exists. If not, create it first. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 605 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 606 | if (*boilerplate == isolate->heap()->undefined_value()) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 607 | boilerplate = |
| 608 | Runtime::CreateArrayLiteralBoilerplate(isolate, literals, elements); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 609 | if (boilerplate.is_null()) return Failure::Exception(); |
| 610 | // Update the functions literal and return the boilerplate. |
| 611 | literals->set(literals_index, *boilerplate); |
| 612 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 613 | return DeepCopyBoilerplate(isolate, JSObject::cast(*boilerplate)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 617 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateArrayLiteralShallow) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 618 | HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 619 | ASSERT(args.length() == 3); |
| 620 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 621 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 622 | CONVERT_ARG_CHECKED(FixedArray, elements, 2); |
| 623 | |
| 624 | // Check if boilerplate exists. If not, create it first. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 625 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 626 | if (*boilerplate == isolate->heap()->undefined_value()) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 627 | ASSERT(*elements != isolate->heap()->empty_fixed_array()); |
| 628 | boilerplate = |
| 629 | Runtime::CreateArrayLiteralBoilerplate(isolate, literals, elements); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 630 | if (boilerplate.is_null()) return Failure::Exception(); |
| 631 | // Update the functions literal and return the boilerplate. |
| 632 | literals->set(literals_index, *boilerplate); |
| 633 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 634 | if (JSObject::cast(*boilerplate)->elements()->map() == |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 635 | isolate->heap()->fixed_cow_array_map()) { |
| 636 | isolate->counters()->cow_arrays_created_runtime()->Increment(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 637 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 638 | return isolate->heap()->CopyJSObject(JSObject::cast(*boilerplate)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 642 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateJSProxy) { |
| 643 | ASSERT(args.length() == 2); |
| 644 | Object* handler = args[0]; |
| 645 | Object* prototype = args[1]; |
| 646 | Object* used_prototype = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 647 | prototype->IsJSReceiver() ? prototype : isolate->heap()->null_value(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 648 | return isolate->heap()->AllocateJSProxy(handler, used_prototype); |
| 649 | } |
| 650 | |
| 651 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 652 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateJSFunctionProxy) { |
| 653 | ASSERT(args.length() == 4); |
| 654 | Object* handler = args[0]; |
| 655 | Object* call_trap = args[1]; |
| 656 | Object* construct_trap = args[2]; |
| 657 | Object* prototype = args[3]; |
| 658 | Object* used_prototype = |
| 659 | prototype->IsJSReceiver() ? prototype : isolate->heap()->null_value(); |
| 660 | return isolate->heap()->AllocateJSFunctionProxy( |
| 661 | handler, call_trap, construct_trap, used_prototype); |
| 662 | } |
| 663 | |
| 664 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 665 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsJSProxy) { |
| 666 | ASSERT(args.length() == 1); |
| 667 | Object* obj = args[0]; |
| 668 | return isolate->heap()->ToBoolean(obj->IsJSProxy()); |
| 669 | } |
| 670 | |
| 671 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 672 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsJSFunctionProxy) { |
| 673 | ASSERT(args.length() == 1); |
| 674 | Object* obj = args[0]; |
| 675 | return isolate->heap()->ToBoolean(obj->IsJSFunctionProxy()); |
| 676 | } |
| 677 | |
| 678 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 679 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetHandler) { |
| 680 | ASSERT(args.length() == 1); |
| 681 | CONVERT_CHECKED(JSProxy, proxy, args[0]); |
| 682 | return proxy->handler(); |
| 683 | } |
| 684 | |
| 685 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 686 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetCallTrap) { |
| 687 | ASSERT(args.length() == 1); |
| 688 | CONVERT_CHECKED(JSFunctionProxy, proxy, args[0]); |
| 689 | return proxy->call_trap(); |
| 690 | } |
| 691 | |
| 692 | |
| 693 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetConstructTrap) { |
| 694 | ASSERT(args.length() == 1); |
| 695 | CONVERT_CHECKED(JSFunctionProxy, proxy, args[0]); |
| 696 | return proxy->construct_trap(); |
| 697 | } |
| 698 | |
| 699 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 700 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Fix) { |
| 701 | ASSERT(args.length() == 1); |
| 702 | CONVERT_CHECKED(JSProxy, proxy, args[0]); |
| 703 | proxy->Fix(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 704 | return isolate->heap()->undefined_value(); |
| 705 | } |
| 706 | |
| 707 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 708 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetInitialize) { |
| 709 | HandleScope scope(isolate); |
| 710 | ASSERT(args.length() == 1); |
| 711 | CONVERT_ARG_CHECKED(JSSet, holder, 0); |
| 712 | Handle<ObjectHashSet> table = isolate->factory()->NewObjectHashSet(0); |
| 713 | holder->set_table(*table); |
| 714 | return *holder; |
| 715 | } |
| 716 | |
| 717 | |
| 718 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetAdd) { |
| 719 | HandleScope scope(isolate); |
| 720 | ASSERT(args.length() == 2); |
| 721 | CONVERT_ARG_CHECKED(JSSet, holder, 0); |
| 722 | Handle<Object> key(args[1]); |
| 723 | Handle<ObjectHashSet> table(ObjectHashSet::cast(holder->table())); |
| 724 | table = ObjectHashSetAdd(table, key); |
| 725 | holder->set_table(*table); |
| 726 | return isolate->heap()->undefined_symbol(); |
| 727 | } |
| 728 | |
| 729 | |
| 730 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetHas) { |
| 731 | HandleScope scope(isolate); |
| 732 | ASSERT(args.length() == 2); |
| 733 | CONVERT_ARG_CHECKED(JSSet, holder, 0); |
| 734 | Handle<Object> key(args[1]); |
| 735 | Handle<ObjectHashSet> table(ObjectHashSet::cast(holder->table())); |
| 736 | return isolate->heap()->ToBoolean(table->Contains(*key)); |
| 737 | } |
| 738 | |
| 739 | |
| 740 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetDelete) { |
| 741 | HandleScope scope(isolate); |
| 742 | ASSERT(args.length() == 2); |
| 743 | CONVERT_ARG_CHECKED(JSSet, holder, 0); |
| 744 | Handle<Object> key(args[1]); |
| 745 | Handle<ObjectHashSet> table(ObjectHashSet::cast(holder->table())); |
| 746 | table = ObjectHashSetRemove(table, key); |
| 747 | holder->set_table(*table); |
| 748 | return isolate->heap()->undefined_symbol(); |
| 749 | } |
| 750 | |
| 751 | |
| 752 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MapInitialize) { |
| 753 | HandleScope scope(isolate); |
| 754 | ASSERT(args.length() == 1); |
| 755 | CONVERT_ARG_CHECKED(JSMap, holder, 0); |
| 756 | Handle<ObjectHashTable> table = isolate->factory()->NewObjectHashTable(0); |
| 757 | holder->set_table(*table); |
| 758 | return *holder; |
| 759 | } |
| 760 | |
| 761 | |
| 762 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MapGet) { |
| 763 | HandleScope scope(isolate); |
| 764 | ASSERT(args.length() == 2); |
| 765 | CONVERT_ARG_CHECKED(JSMap, holder, 0); |
| 766 | Handle<Object> key(args[1]); |
| 767 | return ObjectHashTable::cast(holder->table())->Lookup(*key); |
| 768 | } |
| 769 | |
| 770 | |
| 771 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MapSet) { |
| 772 | HandleScope scope(isolate); |
| 773 | ASSERT(args.length() == 3); |
| 774 | CONVERT_ARG_CHECKED(JSMap, holder, 0); |
| 775 | Handle<Object> key(args[1]); |
| 776 | Handle<Object> value(args[2]); |
| 777 | Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table())); |
| 778 | Handle<ObjectHashTable> new_table = PutIntoObjectHashTable(table, key, value); |
| 779 | holder->set_table(*new_table); |
| 780 | return *value; |
| 781 | } |
| 782 | |
| 783 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 784 | RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapInitialize) { |
| 785 | HandleScope scope(isolate); |
| 786 | ASSERT(args.length() == 1); |
| 787 | CONVERT_ARG_CHECKED(JSWeakMap, weakmap, 0); |
| 788 | ASSERT(weakmap->map()->inobject_properties() == 0); |
| 789 | Handle<ObjectHashTable> table = isolate->factory()->NewObjectHashTable(0); |
| 790 | weakmap->set_table(*table); |
| 791 | weakmap->set_next(Smi::FromInt(0)); |
| 792 | return *weakmap; |
| 793 | } |
| 794 | |
| 795 | |
| 796 | RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapGet) { |
| 797 | NoHandleAllocation ha; |
| 798 | ASSERT(args.length() == 2); |
| 799 | CONVERT_ARG_CHECKED(JSWeakMap, weakmap, 0); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 800 | CONVERT_ARG_CHECKED(JSReceiver, key, 1); |
| 801 | return ObjectHashTable::cast(weakmap->table())->Lookup(*key); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | |
| 805 | RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapSet) { |
| 806 | HandleScope scope(isolate); |
| 807 | ASSERT(args.length() == 3); |
| 808 | CONVERT_ARG_CHECKED(JSWeakMap, weakmap, 0); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 809 | CONVERT_ARG_CHECKED(JSReceiver, key, 1); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 810 | Handle<Object> value(args[2]); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 811 | Handle<ObjectHashTable> table(ObjectHashTable::cast(weakmap->table())); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 812 | Handle<ObjectHashTable> new_table = PutIntoObjectHashTable(table, key, value); |
| 813 | weakmap->set_table(*new_table); |
| 814 | return *value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 818 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ClassOf) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 819 | NoHandleAllocation ha; |
| 820 | ASSERT(args.length() == 1); |
| 821 | Object* obj = args[0]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 822 | if (!obj->IsJSObject()) return isolate->heap()->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 823 | return JSObject::cast(obj)->class_name(); |
| 824 | } |
| 825 | |
| 826 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 827 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetPrototype) { |
| 828 | NoHandleAllocation ha; |
| 829 | ASSERT(args.length() == 1); |
| 830 | CONVERT_CHECKED(JSReceiver, input_obj, args[0]); |
| 831 | Object* obj = input_obj; |
| 832 | // We don't expect access checks to be needed on JSProxy objects. |
| 833 | ASSERT(!obj->IsAccessCheckNeeded() || obj->IsJSObject()); |
| 834 | do { |
| 835 | if (obj->IsAccessCheckNeeded() && |
| 836 | !isolate->MayNamedAccess(JSObject::cast(obj), |
| 837 | isolate->heap()->Proto_symbol(), |
| 838 | v8::ACCESS_GET)) { |
| 839 | isolate->ReportFailedAccessCheck(JSObject::cast(obj), v8::ACCESS_GET); |
| 840 | return isolate->heap()->undefined_value(); |
| 841 | } |
| 842 | obj = obj->GetPrototype(); |
| 843 | } while (obj->IsJSObject() && |
| 844 | JSObject::cast(obj)->map()->is_hidden_prototype()); |
| 845 | return obj; |
| 846 | } |
| 847 | |
| 848 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 849 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsInPrototypeChain) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 850 | NoHandleAllocation ha; |
| 851 | ASSERT(args.length() == 2); |
| 852 | // See ECMA-262, section 15.3.5.3, page 88 (steps 5 - 8). |
| 853 | Object* O = args[0]; |
| 854 | Object* V = args[1]; |
| 855 | while (true) { |
| 856 | Object* prototype = V->GetPrototype(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 857 | if (prototype->IsNull()) return isolate->heap()->false_value(); |
| 858 | if (O == prototype) return isolate->heap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 859 | V = prototype; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 864 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsConstructCall) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 865 | NoHandleAllocation ha; |
| 866 | ASSERT(args.length() == 0); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 867 | JavaScriptFrameIterator it(isolate); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 868 | return isolate->heap()->ToBoolean(it.frame()->IsConstructor()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 872 | // Recursively traverses hidden prototypes if property is not found |
| 873 | static void GetOwnPropertyImplementation(JSObject* obj, |
| 874 | String* name, |
| 875 | LookupResult* result) { |
| 876 | obj->LocalLookupRealNamedProperty(name, result); |
| 877 | |
| 878 | if (!result->IsProperty()) { |
| 879 | Object* proto = obj->GetPrototype(); |
| 880 | if (proto->IsJSObject() && |
| 881 | JSObject::cast(proto)->map()->is_hidden_prototype()) |
| 882 | GetOwnPropertyImplementation(JSObject::cast(proto), |
| 883 | name, result); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 888 | static bool CheckAccessException(LookupResult* result, |
| 889 | v8::AccessType access_type) { |
| 890 | if (result->type() == CALLBACKS) { |
| 891 | Object* callback = result->GetCallbackObject(); |
| 892 | if (callback->IsAccessorInfo()) { |
| 893 | AccessorInfo* info = AccessorInfo::cast(callback); |
| 894 | bool can_access = |
| 895 | (access_type == v8::ACCESS_HAS && |
| 896 | (info->all_can_read() || info->all_can_write())) || |
| 897 | (access_type == v8::ACCESS_GET && info->all_can_read()) || |
| 898 | (access_type == v8::ACCESS_SET && info->all_can_write()); |
| 899 | return can_access; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | return false; |
| 904 | } |
| 905 | |
| 906 | |
| 907 | static bool CheckAccess(JSObject* obj, |
| 908 | String* name, |
| 909 | LookupResult* result, |
| 910 | v8::AccessType access_type) { |
| 911 | ASSERT(result->IsProperty()); |
| 912 | |
| 913 | JSObject* holder = result->holder(); |
| 914 | JSObject* current = obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 915 | Isolate* isolate = obj->GetIsolate(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 916 | while (true) { |
| 917 | if (current->IsAccessCheckNeeded() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 918 | !isolate->MayNamedAccess(current, name, access_type)) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 919 | // Access check callback denied the access, but some properties |
| 920 | // can have a special permissions which override callbacks descision |
| 921 | // (currently see v8::AccessControl). |
| 922 | break; |
| 923 | } |
| 924 | |
| 925 | if (current == holder) { |
| 926 | return true; |
| 927 | } |
| 928 | |
| 929 | current = JSObject::cast(current->GetPrototype()); |
| 930 | } |
| 931 | |
| 932 | // API callbacks can have per callback access exceptions. |
| 933 | switch (result->type()) { |
| 934 | case CALLBACKS: { |
| 935 | if (CheckAccessException(result, access_type)) { |
| 936 | return true; |
| 937 | } |
| 938 | break; |
| 939 | } |
| 940 | case INTERCEPTOR: { |
| 941 | // If the object has an interceptor, try real named properties. |
| 942 | // Overwrite the result to fetch the correct property later. |
| 943 | holder->LookupRealNamedProperty(name, result); |
| 944 | if (result->IsProperty()) { |
| 945 | if (CheckAccessException(result, access_type)) { |
| 946 | return true; |
| 947 | } |
| 948 | } |
| 949 | break; |
| 950 | } |
| 951 | default: |
| 952 | break; |
| 953 | } |
| 954 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 955 | isolate->ReportFailedAccessCheck(current, access_type); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 956 | return false; |
| 957 | } |
| 958 | |
| 959 | |
| 960 | // TODO(1095): we should traverse hidden prototype hierachy as well. |
| 961 | static bool CheckElementAccess(JSObject* obj, |
| 962 | uint32_t index, |
| 963 | v8::AccessType access_type) { |
| 964 | if (obj->IsAccessCheckNeeded() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 965 | !obj->GetIsolate()->MayIndexedAccess(obj, index, access_type)) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 966 | return false; |
| 967 | } |
| 968 | |
| 969 | return true; |
| 970 | } |
| 971 | |
| 972 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 973 | // Enumerator used as indices into the array returned from GetOwnProperty |
| 974 | enum PropertyDescriptorIndices { |
| 975 | IS_ACCESSOR_INDEX, |
| 976 | VALUE_INDEX, |
| 977 | GETTER_INDEX, |
| 978 | SETTER_INDEX, |
| 979 | WRITABLE_INDEX, |
| 980 | ENUMERABLE_INDEX, |
| 981 | CONFIGURABLE_INDEX, |
| 982 | DESCRIPTOR_SIZE |
| 983 | }; |
| 984 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 985 | // Returns an array with the property description: |
| 986 | // if args[1] is not a property on args[0] |
| 987 | // returns undefined |
| 988 | // if args[1] is a data property on args[0] |
| 989 | // [false, value, Writeable, Enumerable, Configurable] |
| 990 | // if args[1] is an accessor on args[0] |
| 991 | // [true, GetFunction, SetFunction, Enumerable, Configurable] |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 992 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetOwnProperty) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 993 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 994 | Heap* heap = isolate->heap(); |
| 995 | HandleScope scope(isolate); |
| 996 | Handle<FixedArray> elms = isolate->factory()->NewFixedArray(DESCRIPTOR_SIZE); |
| 997 | Handle<JSArray> desc = isolate->factory()->NewJSArrayWithElements(elms); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 998 | LookupResult result(isolate); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 999 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 1000 | CONVERT_ARG_CHECKED(String, name, 1); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1001 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1002 | // This could be an element. |
| 1003 | uint32_t index; |
| 1004 | if (name->AsArrayIndex(&index)) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1005 | switch (obj->HasLocalElement(index)) { |
| 1006 | case JSObject::UNDEFINED_ELEMENT: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1007 | return heap->undefined_value(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1008 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1009 | case JSObject::STRING_CHARACTER_ELEMENT: { |
| 1010 | // Special handling of string objects according to ECMAScript 5 |
| 1011 | // 15.5.5.2. Note that this might be a string object with elements |
| 1012 | // other than the actual string value. This is covered by the |
| 1013 | // subsequent cases. |
| 1014 | Handle<JSValue> js_value = Handle<JSValue>::cast(obj); |
| 1015 | Handle<String> str(String::cast(js_value->value())); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1016 | Handle<String> substr = SubString(str, index, index + 1, NOT_TENURED); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1017 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1018 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1019 | elms->set(VALUE_INDEX, *substr); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1020 | elms->set(WRITABLE_INDEX, heap->false_value()); |
| 1021 | elms->set(ENUMERABLE_INDEX, heap->false_value()); |
| 1022 | elms->set(CONFIGURABLE_INDEX, heap->false_value()); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1023 | return *desc; |
| 1024 | } |
| 1025 | |
| 1026 | case JSObject::INTERCEPTED_ELEMENT: |
| 1027 | case JSObject::FAST_ELEMENT: { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1028 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1029 | Handle<Object> value = Object::GetElement(obj, index); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1030 | RETURN_IF_EMPTY_HANDLE(isolate, value); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1031 | elms->set(VALUE_INDEX, *value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1032 | elms->set(WRITABLE_INDEX, heap->true_value()); |
| 1033 | elms->set(ENUMERABLE_INDEX, heap->true_value()); |
| 1034 | elms->set(CONFIGURABLE_INDEX, heap->true_value()); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1035 | return *desc; |
| 1036 | } |
| 1037 | |
| 1038 | case JSObject::DICTIONARY_ELEMENT: { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1039 | Handle<JSObject> holder = obj; |
| 1040 | if (obj->IsJSGlobalProxy()) { |
| 1041 | Object* proto = obj->GetPrototype(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1042 | if (proto->IsNull()) return heap->undefined_value(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1043 | ASSERT(proto->IsJSGlobalObject()); |
| 1044 | holder = Handle<JSObject>(JSObject::cast(proto)); |
| 1045 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1046 | FixedArray* elements = FixedArray::cast(holder->elements()); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1047 | SeededNumberDictionary* dictionary = NULL; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1048 | if (elements->map() == heap->non_strict_arguments_elements_map()) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1049 | dictionary = SeededNumberDictionary::cast(elements->get(1)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1050 | } else { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1051 | dictionary = SeededNumberDictionary::cast(elements); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1052 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1053 | int entry = dictionary->FindEntry(index); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1054 | ASSERT(entry != SeededNumberDictionary::kNotFound); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1055 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 1056 | switch (details.type()) { |
| 1057 | case CALLBACKS: { |
| 1058 | // This is an accessor property with getter and/or setter. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1059 | AccessorPair* accessors = |
| 1060 | AccessorPair::cast(dictionary->ValueAt(entry)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1061 | elms->set(IS_ACCESSOR_INDEX, heap->true_value()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1062 | if (CheckElementAccess(*obj, index, v8::ACCESS_GET)) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1063 | elms->set(GETTER_INDEX, accessors->getter()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1064 | } |
| 1065 | if (CheckElementAccess(*obj, index, v8::ACCESS_SET)) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1066 | elms->set(SETTER_INDEX, accessors->setter()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1067 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1068 | break; |
| 1069 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1070 | case NORMAL: { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1071 | // This is a data property. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1072 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1073 | Handle<Object> value = Object::GetElement(obj, index); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1074 | ASSERT(!value.is_null()); |
| 1075 | elms->set(VALUE_INDEX, *value); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1076 | elms->set(WRITABLE_INDEX, heap->ToBoolean(!details.IsReadOnly())); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1077 | break; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1078 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1079 | default: |
| 1080 | UNREACHABLE(); |
| 1081 | break; |
| 1082 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1083 | elms->set(ENUMERABLE_INDEX, heap->ToBoolean(!details.IsDontEnum())); |
| 1084 | elms->set(CONFIGURABLE_INDEX, heap->ToBoolean(!details.IsDontDelete())); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1085 | return *desc; |
| 1086 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1090 | // Use recursive implementation to also traverse hidden prototypes |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1091 | GetOwnPropertyImplementation(*obj, *name, &result); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1092 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1093 | if (!result.IsProperty()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1094 | return heap->undefined_value(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1095 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1096 | |
| 1097 | if (!CheckAccess(*obj, *name, &result, v8::ACCESS_HAS)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1098 | return heap->false_value(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1101 | elms->set(ENUMERABLE_INDEX, heap->ToBoolean(!result.IsDontEnum())); |
| 1102 | elms->set(CONFIGURABLE_INDEX, heap->ToBoolean(!result.IsDontDelete())); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1103 | |
| 1104 | bool is_js_accessor = (result.type() == CALLBACKS) && |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1105 | (result.GetCallbackObject()->IsAccessorPair()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1106 | |
| 1107 | if (is_js_accessor) { |
| 1108 | // __defineGetter__/__defineSetter__ callback. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1109 | elms->set(IS_ACCESSOR_INDEX, heap->true_value()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1110 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1111 | AccessorPair* accessors = AccessorPair::cast(result.GetCallbackObject()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1112 | if (CheckAccess(*obj, *name, &result, v8::ACCESS_GET)) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1113 | elms->set(GETTER_INDEX, accessors->getter()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1114 | } |
| 1115 | if (CheckAccess(*obj, *name, &result, v8::ACCESS_SET)) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1116 | elms->set(SETTER_INDEX, accessors->setter()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1117 | } |
| 1118 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1119 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
| 1120 | elms->set(WRITABLE_INDEX, heap->ToBoolean(!result.IsReadOnly())); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1121 | |
| 1122 | PropertyAttributes attrs; |
| 1123 | Object* value; |
| 1124 | // GetProperty will check access and report any violations. |
| 1125 | { MaybeObject* maybe_value = obj->GetProperty(*obj, &result, *name, &attrs); |
| 1126 | if (!maybe_value->ToObject(&value)) return maybe_value; |
| 1127 | } |
| 1128 | elms->set(VALUE_INDEX, value); |
| 1129 | } |
| 1130 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1131 | return *desc; |
| 1132 | } |
| 1133 | |
| 1134 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1135 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PreventExtensions) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1136 | ASSERT(args.length() == 1); |
| 1137 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 1138 | return obj->PreventExtensions(); |
| 1139 | } |
| 1140 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1141 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1142 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsExtensible) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1143 | ASSERT(args.length() == 1); |
| 1144 | CONVERT_CHECKED(JSObject, obj, args[0]); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1145 | if (obj->IsJSGlobalProxy()) { |
| 1146 | Object* proto = obj->GetPrototype(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1147 | if (proto->IsNull()) return isolate->heap()->false_value(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1148 | ASSERT(proto->IsJSGlobalObject()); |
| 1149 | obj = JSObject::cast(proto); |
| 1150 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1151 | return isolate->heap()->ToBoolean(obj->map()->is_extensible()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1155 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpCompile) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1156 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1157 | ASSERT(args.length() == 3); |
| 1158 | CONVERT_ARG_CHECKED(JSRegExp, re, 0); |
| 1159 | CONVERT_ARG_CHECKED(String, pattern, 1); |
| 1160 | CONVERT_ARG_CHECKED(String, flags, 2); |
| 1161 | Handle<Object> result = RegExpImpl::Compile(re, pattern, flags); |
| 1162 | if (result.is_null()) return Failure::Exception(); |
| 1163 | return *result; |
| 1164 | } |
| 1165 | |
| 1166 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1167 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateApiFunction) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1168 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1169 | ASSERT(args.length() == 1); |
| 1170 | CONVERT_ARG_CHECKED(FunctionTemplateInfo, data, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1171 | return *isolate->factory()->CreateApiFunction(data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1175 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsTemplate) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1176 | ASSERT(args.length() == 1); |
| 1177 | Object* arg = args[0]; |
| 1178 | bool result = arg->IsObjectTemplateInfo() || arg->IsFunctionTemplateInfo(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1179 | return isolate->heap()->ToBoolean(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1183 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetTemplateField) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1184 | ASSERT(args.length() == 2); |
| 1185 | CONVERT_CHECKED(HeapObject, templ, args[0]); |
| 1186 | CONVERT_CHECKED(Smi, field, args[1]); |
| 1187 | int index = field->value(); |
| 1188 | int offset = index * kPointerSize + HeapObject::kHeaderSize; |
| 1189 | InstanceType type = templ->map()->instance_type(); |
| 1190 | RUNTIME_ASSERT(type == FUNCTION_TEMPLATE_INFO_TYPE || |
| 1191 | type == OBJECT_TEMPLATE_INFO_TYPE); |
| 1192 | RUNTIME_ASSERT(offset > 0); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1193 | if (type == FUNCTION_TEMPLATE_INFO_TYPE) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1194 | RUNTIME_ASSERT(offset < FunctionTemplateInfo::kSize); |
| 1195 | } else { |
| 1196 | RUNTIME_ASSERT(offset < ObjectTemplateInfo::kSize); |
| 1197 | } |
| 1198 | return *HeapObject::RawField(templ, offset); |
| 1199 | } |
| 1200 | |
| 1201 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1202 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DisableAccessChecks) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1203 | ASSERT(args.length() == 1); |
| 1204 | CONVERT_CHECKED(HeapObject, object, args[0]); |
| 1205 | Map* old_map = object->map(); |
| 1206 | bool needs_access_checks = old_map->is_access_check_needed(); |
| 1207 | if (needs_access_checks) { |
| 1208 | // Copy map so it won't interfere constructor's initial map. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1209 | Object* new_map; |
| 1210 | { MaybeObject* maybe_new_map = old_map->CopyDropTransitions(); |
| 1211 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 1212 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1213 | |
| 1214 | Map::cast(new_map)->set_is_access_check_needed(false); |
| 1215 | object->set_map(Map::cast(new_map)); |
| 1216 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1217 | return isolate->heap()->ToBoolean(needs_access_checks); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1221 | RUNTIME_FUNCTION(MaybeObject*, Runtime_EnableAccessChecks) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1222 | ASSERT(args.length() == 1); |
| 1223 | CONVERT_CHECKED(HeapObject, object, args[0]); |
| 1224 | Map* old_map = object->map(); |
| 1225 | if (!old_map->is_access_check_needed()) { |
| 1226 | // Copy map so it won't interfere constructor's initial map. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1227 | Object* new_map; |
| 1228 | { MaybeObject* maybe_new_map = old_map->CopyDropTransitions(); |
| 1229 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 1230 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1231 | |
| 1232 | Map::cast(new_map)->set_is_access_check_needed(true); |
| 1233 | object->set_map(Map::cast(new_map)); |
| 1234 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1235 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1239 | static Failure* ThrowRedeclarationError(Isolate* isolate, |
| 1240 | const char* type, |
| 1241 | Handle<String> name) { |
| 1242 | HandleScope scope(isolate); |
| 1243 | Handle<Object> type_handle = |
| 1244 | isolate->factory()->NewStringFromAscii(CStrVector(type)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1245 | Handle<Object> args[2] = { type_handle, name }; |
| 1246 | Handle<Object> error = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1247 | isolate->factory()->NewTypeError("redeclaration", HandleVector(args, 2)); |
| 1248 | return isolate->Throw(*error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1252 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeclareGlobals) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1253 | ASSERT(args.length() == 3); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1254 | HandleScope scope(isolate); |
| 1255 | Handle<GlobalObject> global = Handle<GlobalObject>( |
| 1256 | isolate->context()->global()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1257 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1258 | Handle<Context> context = args.at<Context>(0); |
| 1259 | CONVERT_ARG_CHECKED(FixedArray, pairs, 1); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1260 | CONVERT_SMI_ARG_CHECKED(flags, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1261 | |
| 1262 | // Traverse the name/value pairs and set the properties. |
| 1263 | int length = pairs->length(); |
| 1264 | for (int i = 0; i < length; i += 2) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1265 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1266 | Handle<String> name(String::cast(pairs->get(i))); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1267 | Handle<Object> value(pairs->get(i + 1), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1268 | |
| 1269 | // We have to declare a global const property. To capture we only |
| 1270 | // assign to it when evaluating the assignment for "const x = |
| 1271 | // <expr>" the initial value is the hole. |
| 1272 | bool is_const_property = value->IsTheHole(); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1273 | bool is_function_declaration = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1274 | if (value->IsUndefined() || is_const_property) { |
| 1275 | // Lookup the property in the global object, and don't set the |
| 1276 | // value of the variable if the property is already there. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1277 | LookupResult lookup(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1278 | global->Lookup(*name, &lookup); |
| 1279 | if (lookup.IsProperty()) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1280 | // We found an existing property. Unless it was an interceptor |
| 1281 | // that claims the property is absent, skip this declaration. |
| 1282 | if (lookup.type() != INTERCEPTOR) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1283 | continue; |
| 1284 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1285 | PropertyAttributes attributes = global->GetPropertyAttribute(*name); |
| 1286 | if (attributes != ABSENT) { |
| 1287 | continue; |
| 1288 | } |
| 1289 | // Fall-through and introduce the absent property by using |
| 1290 | // SetProperty. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1291 | } |
| 1292 | } else { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1293 | is_function_declaration = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1294 | // Copy the function and update its context. Use it as value. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1295 | Handle<SharedFunctionInfo> shared = |
| 1296 | Handle<SharedFunctionInfo>::cast(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1297 | Handle<JSFunction> function = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1298 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 1299 | context, |
| 1300 | TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1301 | value = function; |
| 1302 | } |
| 1303 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1304 | LookupResult lookup(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1305 | global->LocalLookup(*name, &lookup); |
| 1306 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1307 | // Compute the property attributes. According to ECMA-262, section |
| 1308 | // 13, page 71, the property must be read-only and |
| 1309 | // non-deletable. However, neither SpiderMonkey nor KJS creates the |
| 1310 | // property as read-only, so we don't either. |
| 1311 | int attr = NONE; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1312 | if (!DeclareGlobalsEvalFlag::decode(flags)) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1313 | attr |= DONT_DELETE; |
| 1314 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1315 | bool is_native = DeclareGlobalsNativeFlag::decode(flags); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1316 | if (is_const_property || (is_native && is_function_declaration)) { |
| 1317 | attr |= READ_ONLY; |
| 1318 | } |
| 1319 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1320 | // Safari does not allow the invocation of callback setters for |
| 1321 | // function declarations. To mimic this behavior, we do not allow |
| 1322 | // the invocation of setters for function values. This makes a |
| 1323 | // difference for global functions with the same names as event |
| 1324 | // handlers such as "function onload() {}". Firefox does call the |
| 1325 | // onload setter in those case and Safari does not. We follow |
| 1326 | // Safari for compatibility. |
| 1327 | if (value->IsJSFunction()) { |
| 1328 | // Do not change DONT_DELETE to false from true. |
| 1329 | if (lookup.IsProperty() && (lookup.type() != INTERCEPTOR)) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1330 | attr |= lookup.GetAttributes() & DONT_DELETE; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1331 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1332 | PropertyAttributes attributes = static_cast<PropertyAttributes>(attr); |
| 1333 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1334 | RETURN_IF_EMPTY_HANDLE( |
| 1335 | isolate, |
| 1336 | JSObject::SetLocalPropertyIgnoreAttributes(global, name, value, |
| 1337 | attributes)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1338 | } else { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1339 | LanguageMode language_mode = DeclareGlobalsLanguageMode::decode(flags); |
| 1340 | StrictModeFlag strict_mode_flag = (language_mode == CLASSIC_MODE) |
| 1341 | ? kNonStrictMode : kStrictMode; |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1342 | RETURN_IF_EMPTY_HANDLE( |
| 1343 | isolate, |
| 1344 | JSReceiver::SetProperty(global, name, value, |
| 1345 | static_cast<PropertyAttributes>(attr), |
| 1346 | strict_mode_flag)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1347 | } |
| 1348 | } |
| 1349 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1350 | ASSERT(!isolate->has_pending_exception()); |
| 1351 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1355 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeclareContextSlot) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1356 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1357 | ASSERT(args.length() == 4); |
| 1358 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1359 | // Declarations are always made in a function or global context. In the |
| 1360 | // case of eval code, the context passed is the context of the caller, |
| 1361 | // which may be some nested context and not the declaration context. |
| 1362 | RUNTIME_ASSERT(args[0]->IsContext()); |
| 1363 | Handle<Context> context(Context::cast(args[0])->declaration_context()); |
| 1364 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1365 | Handle<String> name(String::cast(args[1])); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1366 | PropertyAttributes mode = static_cast<PropertyAttributes>(args.smi_at(2)); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1367 | RUNTIME_ASSERT(mode == READ_ONLY || mode == NONE); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1368 | Handle<Object> initial_value(args[3], isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1369 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1370 | int index; |
| 1371 | PropertyAttributes attributes; |
| 1372 | ContextLookupFlags flags = DONT_FOLLOW_CHAINS; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1373 | BindingFlags binding_flags; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1374 | Handle<Object> holder = |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1375 | context->Lookup(name, flags, &index, &attributes, &binding_flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1376 | |
| 1377 | if (attributes != ABSENT) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1378 | // The name was declared before; check for conflicting re-declarations. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1379 | if (((attributes & READ_ONLY) != 0) || (mode == READ_ONLY)) { |
| 1380 | // Functions are not read-only. |
| 1381 | ASSERT(mode != READ_ONLY || initial_value->IsTheHole()); |
| 1382 | const char* type = ((attributes & READ_ONLY) != 0) ? "const" : "var"; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1383 | return ThrowRedeclarationError(isolate, type, name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | // Initialize it if necessary. |
| 1387 | if (*initial_value != NULL) { |
| 1388 | if (index >= 0) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1389 | ASSERT(holder.is_identical_to(context)); |
| 1390 | if (((attributes & READ_ONLY) == 0) || |
| 1391 | context->get(index)->IsTheHole()) { |
| 1392 | context->set(index, *initial_value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1393 | } |
| 1394 | } else { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1395 | // Slow case: The property is in the context extension object of a |
| 1396 | // function context or the global object of a global context. |
| 1397 | Handle<JSObject> object = Handle<JSObject>::cast(holder); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1398 | RETURN_IF_EMPTY_HANDLE( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1399 | isolate, |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1400 | JSReceiver::SetProperty(object, name, initial_value, mode, |
| 1401 | kNonStrictMode)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | } else { |
| 1406 | // The property is not in the function context. It needs to be |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1407 | // "declared" in the function context's extension context or as a |
| 1408 | // property of the the global object. |
| 1409 | Handle<JSObject> object; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1410 | if (context->has_extension()) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1411 | object = Handle<JSObject>(JSObject::cast(context->extension())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1412 | } else { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1413 | // Context extension objects are allocated lazily. |
| 1414 | ASSERT(context->IsFunctionContext()); |
| 1415 | object = isolate->factory()->NewJSObject( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1416 | isolate->context_extension_function()); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1417 | context->set_extension(*object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1418 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1419 | ASSERT(*object != NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1420 | |
| 1421 | // Declare the property by setting it to the initial value if provided, |
| 1422 | // or undefined, and use the correct mode (e.g. READ_ONLY attribute for |
| 1423 | // constant declarations). |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1424 | ASSERT(!object->HasLocalProperty(*name)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1425 | Handle<Object> value(isolate->heap()->undefined_value(), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1426 | if (*initial_value != NULL) value = initial_value; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1427 | // Declaring a const context slot is a conflicting declaration if |
| 1428 | // there is a callback with that name in a prototype. It is |
| 1429 | // allowed to introduce const variables in |
| 1430 | // JSContextExtensionObjects. They are treated specially in |
| 1431 | // SetProperty and no setters are invoked for those since they are |
| 1432 | // not real JSObjects. |
| 1433 | if (initial_value->IsTheHole() && |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1434 | !object->IsJSContextExtensionObject()) { |
| 1435 | LookupResult lookup(isolate); |
| 1436 | object->Lookup(*name, &lookup); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1437 | if (lookup.IsFound() && (lookup.type() == CALLBACKS)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1438 | return ThrowRedeclarationError(isolate, "const", name); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1439 | } |
| 1440 | } |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1441 | RETURN_IF_EMPTY_HANDLE( |
| 1442 | isolate, |
| 1443 | JSReceiver::SetProperty(object, name, value, mode, kNonStrictMode)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1446 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1450 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeVarGlobal) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1451 | NoHandleAllocation nha; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1452 | // args[0] == name |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1453 | // args[1] == language_mode |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1454 | // args[2] == value (optional) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1455 | |
| 1456 | // Determine if we need to assign to the variable if it already |
| 1457 | // exists (based on the number of arguments). |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1458 | RUNTIME_ASSERT(args.length() == 2 || args.length() == 3); |
| 1459 | bool assign = args.length() == 3; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1460 | |
| 1461 | CONVERT_ARG_CHECKED(String, name, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1462 | GlobalObject* global = isolate->context()->global(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1463 | RUNTIME_ASSERT(args[1]->IsSmi()); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1464 | CONVERT_LANGUAGE_MODE_ARG(language_mode, 1); |
| 1465 | StrictModeFlag strict_mode_flag = (language_mode == CLASSIC_MODE) |
| 1466 | ? kNonStrictMode : kStrictMode; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1467 | |
| 1468 | // According to ECMA-262, section 12.2, page 62, the property must |
| 1469 | // not be deletable. |
| 1470 | PropertyAttributes attributes = DONT_DELETE; |
| 1471 | |
| 1472 | // Lookup the property locally in the global object. If it isn't |
| 1473 | // there, there is a property with this name in the prototype chain. |
| 1474 | // We follow Safari and Firefox behavior and only set the property |
| 1475 | // locally if there is an explicit initialization value that we have |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1476 | // to assign to the property. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1477 | // Note that objects can have hidden prototypes, so we need to traverse |
| 1478 | // the whole chain of hidden prototypes to do a 'local' lookup. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1479 | Object* object = global; |
| 1480 | LookupResult lookup(isolate); |
| 1481 | while (object->IsJSObject() && |
| 1482 | JSObject::cast(object)->map()->is_hidden_prototype()) { |
| 1483 | JSObject* raw_holder = JSObject::cast(object); |
| 1484 | raw_holder->LocalLookup(*name, &lookup); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1485 | if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1486 | HandleScope handle_scope(isolate); |
| 1487 | Handle<JSObject> holder(raw_holder); |
| 1488 | PropertyAttributes intercepted = holder->GetPropertyAttribute(*name); |
| 1489 | // Update the raw pointer in case it's changed due to GC. |
| 1490 | raw_holder = *holder; |
| 1491 | if (intercepted != ABSENT && (intercepted & READ_ONLY) == 0) { |
| 1492 | // Found an interceptor that's not read only. |
| 1493 | if (assign) { |
| 1494 | return raw_holder->SetProperty( |
| 1495 | &lookup, *name, args[2], attributes, strict_mode_flag); |
| 1496 | } else { |
| 1497 | return isolate->heap()->undefined_value(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1498 | } |
| 1499 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1500 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1501 | object = raw_holder->GetPrototype(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1504 | // Reload global in case the loop above performed a GC. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1505 | global = isolate->context()->global(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1506 | if (assign) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1507 | return global->SetProperty(*name, args[2], attributes, strict_mode_flag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1508 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1509 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1513 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeConstGlobal) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1514 | // All constants are declared with an initial value. The name |
| 1515 | // of the constant is the first argument and the initial value |
| 1516 | // is the second. |
| 1517 | RUNTIME_ASSERT(args.length() == 2); |
| 1518 | CONVERT_ARG_CHECKED(String, name, 0); |
| 1519 | Handle<Object> value = args.at<Object>(1); |
| 1520 | |
| 1521 | // Get the current global object from top. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1522 | GlobalObject* global = isolate->context()->global(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1523 | |
| 1524 | // According to ECMA-262, section 12.2, page 62, the property must |
| 1525 | // not be deletable. Since it's a const, it must be READ_ONLY too. |
| 1526 | PropertyAttributes attributes = |
| 1527 | static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY); |
| 1528 | |
| 1529 | // Lookup the property locally in the global object. If it isn't |
| 1530 | // there, we add the property and take special precautions to always |
| 1531 | // add it as a local property even in case of callbacks in the |
| 1532 | // prototype chain (this rules out using SetProperty). |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 1533 | // We use SetLocalPropertyIgnoreAttributes instead |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1534 | LookupResult lookup(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1535 | global->LocalLookup(*name, &lookup); |
| 1536 | if (!lookup.IsProperty()) { |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 1537 | return global->SetLocalPropertyIgnoreAttributes(*name, |
| 1538 | *value, |
| 1539 | attributes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1542 | if (!lookup.IsReadOnly()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1543 | // Restore global object from context (in case of GC) and continue |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1544 | // with setting the value. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1545 | HandleScope handle_scope(isolate); |
| 1546 | Handle<GlobalObject> global(isolate->context()->global()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1547 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1548 | // 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] | 1549 | // property through an interceptor and only do it if it's |
| 1550 | // uninitialized, e.g. the hole. Nirk... |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1551 | // Passing non-strict mode because the property is writable. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1552 | RETURN_IF_EMPTY_HANDLE( |
| 1553 | isolate, |
| 1554 | JSReceiver::SetProperty(global, name, value, attributes, |
| 1555 | kNonStrictMode)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1556 | return *value; |
| 1557 | } |
| 1558 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1559 | // 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] | 1560 | // constant. For now, we determine this by checking if the |
| 1561 | // current value is the hole. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1562 | // Strict mode handling not needed (const is disallowed in strict mode). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1563 | PropertyType type = lookup.type(); |
| 1564 | if (type == FIELD) { |
| 1565 | FixedArray* properties = global->properties(); |
| 1566 | int index = lookup.GetFieldIndex(); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1567 | if (properties->get(index)->IsTheHole() || !lookup.IsReadOnly()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1568 | properties->set(index, *value); |
| 1569 | } |
| 1570 | } else if (type == NORMAL) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1571 | if (global->GetNormalizedProperty(&lookup)->IsTheHole() || |
| 1572 | !lookup.IsReadOnly()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1573 | global->SetNormalizedProperty(&lookup, *value); |
| 1574 | } |
| 1575 | } else { |
| 1576 | // Ignore re-initialization of constants that have already been |
| 1577 | // assigned a function value. |
| 1578 | ASSERT(lookup.IsReadOnly() && type == CONSTANT_FUNCTION); |
| 1579 | } |
| 1580 | |
| 1581 | // Use the set value as the result of the operation. |
| 1582 | return *value; |
| 1583 | } |
| 1584 | |
| 1585 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1586 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeConstContextSlot) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1587 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1588 | ASSERT(args.length() == 3); |
| 1589 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1590 | Handle<Object> value(args[0], isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1591 | ASSERT(!value->IsTheHole()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1592 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1593 | // Initializations are always done in a function or global context. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1594 | RUNTIME_ASSERT(args[1]->IsContext()); |
| 1595 | Handle<Context> context(Context::cast(args[1])->declaration_context()); |
| 1596 | |
| 1597 | Handle<String> name(String::cast(args[2])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1598 | |
| 1599 | int index; |
| 1600 | PropertyAttributes attributes; |
| 1601 | ContextLookupFlags flags = FOLLOW_CHAINS; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1602 | BindingFlags binding_flags; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1603 | Handle<Object> holder = |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1604 | context->Lookup(name, flags, &index, &attributes, &binding_flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1605 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1606 | if (index >= 0) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1607 | ASSERT(holder->IsContext()); |
| 1608 | // Property was found in a context. Perform the assignment if we |
| 1609 | // found some non-constant or an uninitialized constant. |
| 1610 | Handle<Context> context = Handle<Context>::cast(holder); |
| 1611 | if ((attributes & READ_ONLY) == 0 || context->get(index)->IsTheHole()) { |
| 1612 | context->set(index, *value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1613 | } |
| 1614 | return *value; |
| 1615 | } |
| 1616 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1617 | // The property could not be found, we introduce it as a property of the |
| 1618 | // global object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1619 | if (attributes == ABSENT) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1620 | Handle<JSObject> global = Handle<JSObject>( |
| 1621 | isolate->context()->global()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1622 | // Strict mode not needed (const disallowed in strict mode). |
| 1623 | RETURN_IF_EMPTY_HANDLE( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1624 | isolate, |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1625 | JSReceiver::SetProperty(global, name, value, NONE, kNonStrictMode)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1626 | return *value; |
| 1627 | } |
| 1628 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1629 | // The property was present in some function's context extension object, |
| 1630 | // as a property on the subject of a with, or as a property of the global |
| 1631 | // object. |
| 1632 | // |
| 1633 | // In most situations, eval-introduced consts should still be present in |
| 1634 | // the context extension object. However, because declaration and |
| 1635 | // initialization are separate, the property might have been deleted |
| 1636 | // before we reach the initialization point. |
| 1637 | // |
| 1638 | // Example: |
| 1639 | // |
| 1640 | // function f() { eval("delete x; const x;"); } |
| 1641 | // |
| 1642 | // In that case, the initialization behaves like a normal assignment. |
| 1643 | Handle<JSObject> object = Handle<JSObject>::cast(holder); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1644 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1645 | if (*object == context->extension()) { |
| 1646 | // This is the property that was introduced by the const declaration. |
| 1647 | // Set it if it hasn't been set before. NOTE: We cannot use |
| 1648 | // GetProperty() to get the current value as it 'unholes' the value. |
| 1649 | LookupResult lookup(isolate); |
| 1650 | object->LocalLookupRealNamedProperty(*name, &lookup); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1651 | ASSERT(lookup.IsFound()); // the property was declared |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1652 | ASSERT(lookup.IsReadOnly()); // and it was declared as read-only |
| 1653 | |
| 1654 | PropertyType type = lookup.type(); |
| 1655 | if (type == FIELD) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1656 | FixedArray* properties = object->properties(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1657 | int index = lookup.GetFieldIndex(); |
| 1658 | if (properties->get(index)->IsTheHole()) { |
| 1659 | properties->set(index, *value); |
| 1660 | } |
| 1661 | } else if (type == NORMAL) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1662 | if (object->GetNormalizedProperty(&lookup)->IsTheHole()) { |
| 1663 | object->SetNormalizedProperty(&lookup, *value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1664 | } |
| 1665 | } else { |
| 1666 | // We should not reach here. Any real, named property should be |
| 1667 | // either a field or a dictionary slot. |
| 1668 | UNREACHABLE(); |
| 1669 | } |
| 1670 | } else { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1671 | // The property was found on some other object. Set it if it is not a |
| 1672 | // read-only property. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1673 | if ((attributes & READ_ONLY) == 0) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1674 | // Strict mode not needed (const disallowed in strict mode). |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1675 | RETURN_IF_EMPTY_HANDLE( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1676 | isolate, |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1677 | JSReceiver::SetProperty(object, name, value, attributes, |
| 1678 | kNonStrictMode)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | return *value; |
| 1683 | } |
| 1684 | |
| 1685 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1686 | RUNTIME_FUNCTION(MaybeObject*, |
| 1687 | Runtime_OptimizeObjectForAddingMultipleProperties) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1688 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1689 | ASSERT(args.length() == 2); |
| 1690 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1691 | CONVERT_SMI_ARG_CHECKED(properties, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1692 | if (object->HasFastProperties()) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1693 | JSObject::NormalizeProperties(object, KEEP_INOBJECT_PROPERTIES, properties); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1694 | } |
| 1695 | return *object; |
| 1696 | } |
| 1697 | |
| 1698 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1699 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpExec) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1700 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1701 | ASSERT(args.length() == 4); |
| 1702 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 0); |
| 1703 | CONVERT_ARG_CHECKED(String, subject, 1); |
| 1704 | // Due to the way the JS calls are constructed this must be less than the |
| 1705 | // 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] | 1706 | CONVERT_SMI_ARG_CHECKED(index, 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1707 | CONVERT_ARG_CHECKED(JSArray, last_match_info, 3); |
| 1708 | RUNTIME_ASSERT(last_match_info->HasFastElements()); |
| 1709 | RUNTIME_ASSERT(index >= 0); |
| 1710 | RUNTIME_ASSERT(index <= subject->length()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1711 | isolate->counters()->regexp_entry_runtime()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1712 | Handle<Object> result = RegExpImpl::Exec(regexp, |
| 1713 | subject, |
| 1714 | index, |
| 1715 | last_match_info); |
| 1716 | if (result.is_null()) return Failure::Exception(); |
| 1717 | return *result; |
| 1718 | } |
| 1719 | |
| 1720 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1721 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpConstructResult) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1722 | ASSERT(args.length() == 3); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1723 | CONVERT_SMI_ARG_CHECKED(elements_count, 0); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1724 | if (elements_count < 0 || |
| 1725 | elements_count > FixedArray::kMaxLength || |
| 1726 | !Smi::IsValid(elements_count)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1727 | return isolate->ThrowIllegalOperation(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1728 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1729 | Object* new_object; |
| 1730 | { MaybeObject* maybe_new_object = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1731 | isolate->heap()->AllocateFixedArrayWithHoles(elements_count); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1732 | if (!maybe_new_object->ToObject(&new_object)) return maybe_new_object; |
| 1733 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1734 | FixedArray* elements = FixedArray::cast(new_object); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1735 | { MaybeObject* maybe_new_object = isolate->heap()->AllocateRaw( |
| 1736 | JSRegExpResult::kSize, NEW_SPACE, OLD_POINTER_SPACE); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1737 | if (!maybe_new_object->ToObject(&new_object)) return maybe_new_object; |
| 1738 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1739 | { |
| 1740 | AssertNoAllocation no_gc; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1741 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1742 | reinterpret_cast<HeapObject*>(new_object)-> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1743 | set_map(isolate->global_context()->regexp_result_map()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1744 | } |
| 1745 | JSArray* array = JSArray::cast(new_object); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1746 | array->set_properties(isolate->heap()->empty_fixed_array()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1747 | array->set_elements(elements); |
| 1748 | array->set_length(Smi::FromInt(elements_count)); |
| 1749 | // Write in-object properties after the length of the array. |
| 1750 | array->InObjectPropertyAtPut(JSRegExpResult::kIndexIndex, args[1]); |
| 1751 | array->InObjectPropertyAtPut(JSRegExpResult::kInputIndex, args[2]); |
| 1752 | return array; |
| 1753 | } |
| 1754 | |
| 1755 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1756 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpInitializeObject) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1757 | AssertNoAllocation no_alloc; |
| 1758 | ASSERT(args.length() == 5); |
| 1759 | CONVERT_CHECKED(JSRegExp, regexp, args[0]); |
| 1760 | CONVERT_CHECKED(String, source, args[1]); |
| 1761 | |
| 1762 | Object* global = args[2]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1763 | if (!global->IsTrue()) global = isolate->heap()->false_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1764 | |
| 1765 | Object* ignoreCase = args[3]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1766 | if (!ignoreCase->IsTrue()) ignoreCase = isolate->heap()->false_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1767 | |
| 1768 | Object* multiline = args[4]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1769 | if (!multiline->IsTrue()) multiline = isolate->heap()->false_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1770 | |
| 1771 | Map* map = regexp->map(); |
| 1772 | Object* constructor = map->constructor(); |
| 1773 | if (constructor->IsJSFunction() && |
| 1774 | JSFunction::cast(constructor)->initial_map() == map) { |
| 1775 | // If we still have the original map, set in-object properties directly. |
| 1776 | regexp->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex, source); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1777 | // Both true and false are immovable immortal objects so no need for write |
| 1778 | // barrier. |
| 1779 | regexp->InObjectPropertyAtPut( |
| 1780 | JSRegExp::kGlobalFieldIndex, global, SKIP_WRITE_BARRIER); |
| 1781 | regexp->InObjectPropertyAtPut( |
| 1782 | JSRegExp::kIgnoreCaseFieldIndex, ignoreCase, SKIP_WRITE_BARRIER); |
| 1783 | regexp->InObjectPropertyAtPut( |
| 1784 | JSRegExp::kMultilineFieldIndex, multiline, SKIP_WRITE_BARRIER); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1785 | regexp->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex, |
| 1786 | Smi::FromInt(0), |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1787 | SKIP_WRITE_BARRIER); // It's a Smi. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1788 | return regexp; |
| 1789 | } |
| 1790 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1791 | // Map has changed, so use generic, but slower, method. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1792 | PropertyAttributes final = |
| 1793 | static_cast<PropertyAttributes>(READ_ONLY | DONT_ENUM | DONT_DELETE); |
| 1794 | PropertyAttributes writable = |
| 1795 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1796 | Heap* heap = isolate->heap(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1797 | MaybeObject* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1798 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->source_symbol(), |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 1799 | source, |
| 1800 | final); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1801 | ASSERT(!result->IsFailure()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1802 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->global_symbol(), |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 1803 | global, |
| 1804 | final); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1805 | ASSERT(!result->IsFailure()); |
| 1806 | result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1807 | regexp->SetLocalPropertyIgnoreAttributes(heap->ignore_case_symbol(), |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 1808 | ignoreCase, |
| 1809 | final); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1810 | ASSERT(!result->IsFailure()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1811 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->multiline_symbol(), |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 1812 | multiline, |
| 1813 | final); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1814 | ASSERT(!result->IsFailure()); |
| 1815 | result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1816 | regexp->SetLocalPropertyIgnoreAttributes(heap->last_index_symbol(), |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 1817 | Smi::FromInt(0), |
| 1818 | writable); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1819 | ASSERT(!result->IsFailure()); |
| 1820 | USE(result); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1821 | return regexp; |
| 1822 | } |
| 1823 | |
| 1824 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1825 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FinishArrayPrototypeSetup) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1826 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1827 | ASSERT(args.length() == 1); |
| 1828 | CONVERT_ARG_CHECKED(JSArray, prototype, 0); |
| 1829 | // This is necessary to enable fast checks for absence of elements |
| 1830 | // on Array.prototype and below. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1831 | prototype->set_elements(isolate->heap()->empty_fixed_array()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1832 | return Smi::FromInt(0); |
| 1833 | } |
| 1834 | |
| 1835 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1836 | static Handle<JSFunction> InstallBuiltin(Isolate* isolate, |
| 1837 | Handle<JSObject> holder, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1838 | const char* name, |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1839 | Builtins::Name builtin_name) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1840 | Handle<String> key = isolate->factory()->LookupAsciiSymbol(name); |
| 1841 | Handle<Code> code(isolate->builtins()->builtin(builtin_name)); |
| 1842 | Handle<JSFunction> optimized = |
| 1843 | isolate->factory()->NewFunction(key, |
| 1844 | JS_OBJECT_TYPE, |
| 1845 | JSObject::kHeaderSize, |
| 1846 | code, |
| 1847 | false); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1848 | optimized->shared()->DontAdaptArguments(); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1849 | JSReceiver::SetProperty(holder, key, optimized, NONE, kStrictMode); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1850 | return optimized; |
| 1851 | } |
| 1852 | |
| 1853 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1854 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SpecialArrayFunctions) { |
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); |
| 1857 | CONVERT_ARG_CHECKED(JSObject, holder, 0); |
| 1858 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1859 | InstallBuiltin(isolate, holder, "pop", Builtins::kArrayPop); |
| 1860 | InstallBuiltin(isolate, holder, "push", Builtins::kArrayPush); |
| 1861 | InstallBuiltin(isolate, holder, "shift", Builtins::kArrayShift); |
| 1862 | InstallBuiltin(isolate, holder, "unshift", Builtins::kArrayUnshift); |
| 1863 | InstallBuiltin(isolate, holder, "slice", Builtins::kArraySlice); |
| 1864 | InstallBuiltin(isolate, holder, "splice", Builtins::kArraySplice); |
| 1865 | InstallBuiltin(isolate, holder, "concat", Builtins::kArrayConcat); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1866 | |
| 1867 | return *holder; |
| 1868 | } |
| 1869 | |
| 1870 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1871 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetDefaultReceiver) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1872 | ASSERT(args.length() == 1); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 1873 | CONVERT_CHECKED(JSReceiver, callable, args[0]); |
| 1874 | |
| 1875 | if (!callable->IsJSFunction()) { |
| 1876 | HandleScope scope(isolate); |
| 1877 | bool threw = false; |
| 1878 | Handle<Object> delegate = |
| 1879 | Execution::TryGetFunctionDelegate(Handle<JSReceiver>(callable), &threw); |
| 1880 | if (threw) return Failure::Exception(); |
| 1881 | callable = JSFunction::cast(*delegate); |
| 1882 | } |
| 1883 | JSFunction* function = JSFunction::cast(callable); |
| 1884 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1885 | SharedFunctionInfo* shared = function->shared(); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 1886 | if (shared->native() || !shared->is_classic_mode()) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1887 | return isolate->heap()->undefined_value(); |
| 1888 | } |
| 1889 | // Returns undefined for strict or native functions, or |
| 1890 | // the associated global receiver for "normal" functions. |
| 1891 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1892 | Context* global_context = |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1893 | function->context()->global()->global_context(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1894 | return global_context->global()->global_receiver(); |
| 1895 | } |
| 1896 | |
| 1897 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1898 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MaterializeRegExpLiteral) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1899 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1900 | ASSERT(args.length() == 4); |
| 1901 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1902 | int index = args.smi_at(1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1903 | Handle<String> pattern = args.at<String>(2); |
| 1904 | Handle<String> flags = args.at<String>(3); |
| 1905 | |
| 1906 | // Get the RegExp function from the context in the literals array. |
| 1907 | // This is the RegExp function from the context in which the |
| 1908 | // function was created. We do not use the RegExp function from the |
| 1909 | // current global context because this might be the RegExp function |
| 1910 | // from another context which we should not have access to. |
| 1911 | Handle<JSFunction> constructor = |
| 1912 | Handle<JSFunction>( |
| 1913 | JSFunction::GlobalContextFromLiterals(*literals)->regexp_function()); |
| 1914 | // Compute the regular expression literal. |
| 1915 | bool has_pending_exception; |
| 1916 | Handle<Object> regexp = |
| 1917 | RegExpImpl::CreateRegExpLiteral(constructor, pattern, flags, |
| 1918 | &has_pending_exception); |
| 1919 | if (has_pending_exception) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1920 | ASSERT(isolate->has_pending_exception()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1921 | return Failure::Exception(); |
| 1922 | } |
| 1923 | literals->set(index, *regexp); |
| 1924 | return *regexp; |
| 1925 | } |
| 1926 | |
| 1927 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1928 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetName) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1929 | NoHandleAllocation ha; |
| 1930 | ASSERT(args.length() == 1); |
| 1931 | |
| 1932 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 1933 | return f->shared()->name(); |
| 1934 | } |
| 1935 | |
| 1936 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1937 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetName) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1938 | NoHandleAllocation ha; |
| 1939 | ASSERT(args.length() == 2); |
| 1940 | |
| 1941 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 1942 | CONVERT_CHECKED(String, name, args[1]); |
| 1943 | f->shared()->set_name(name); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1944 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1948 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionNameShouldPrintAsAnonymous) { |
| 1949 | NoHandleAllocation ha; |
| 1950 | ASSERT(args.length() == 1); |
| 1951 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 1952 | return isolate->heap()->ToBoolean( |
| 1953 | f->shared()->name_should_print_as_anonymous()); |
| 1954 | } |
| 1955 | |
| 1956 | |
| 1957 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionMarkNameShouldPrintAsAnonymous) { |
| 1958 | NoHandleAllocation ha; |
| 1959 | ASSERT(args.length() == 1); |
| 1960 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 1961 | f->shared()->set_name_should_print_as_anonymous(true); |
| 1962 | return isolate->heap()->undefined_value(); |
| 1963 | } |
| 1964 | |
| 1965 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1966 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionRemovePrototype) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1967 | NoHandleAllocation ha; |
| 1968 | ASSERT(args.length() == 1); |
| 1969 | |
| 1970 | CONVERT_CHECKED(JSFunction, f, args[0]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1971 | Object* obj = f->RemovePrototype(); |
| 1972 | if (obj->IsFailure()) return obj; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1973 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1974 | return isolate->heap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1978 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetScript) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1979 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1980 | ASSERT(args.length() == 1); |
| 1981 | |
| 1982 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1983 | Handle<Object> script = Handle<Object>(fun->shared()->script(), isolate); |
| 1984 | if (!script->IsScript()) return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1985 | |
| 1986 | return *GetScriptWrapper(Handle<Script>::cast(script)); |
| 1987 | } |
| 1988 | |
| 1989 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1990 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetSourceCode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1991 | NoHandleAllocation ha; |
| 1992 | ASSERT(args.length() == 1); |
| 1993 | |
| 1994 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 1995 | return f->shared()->GetSourceCode(); |
| 1996 | } |
| 1997 | |
| 1998 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1999 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetScriptSourcePosition) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2000 | NoHandleAllocation ha; |
| 2001 | ASSERT(args.length() == 1); |
| 2002 | |
| 2003 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
| 2004 | int pos = fun->shared()->start_position(); |
| 2005 | return Smi::FromInt(pos); |
| 2006 | } |
| 2007 | |
| 2008 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2009 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetPositionForOffset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2010 | ASSERT(args.length() == 2); |
| 2011 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2012 | CONVERT_CHECKED(Code, code, args[0]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2013 | CONVERT_NUMBER_CHECKED(int, offset, Int32, args[1]); |
| 2014 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2015 | RUNTIME_ASSERT(0 <= offset && offset < code->Size()); |
| 2016 | |
| 2017 | Address pc = code->address() + offset; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2018 | return Smi::FromInt(code->SourcePosition(pc)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2022 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetInstanceClassName) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2023 | NoHandleAllocation ha; |
| 2024 | ASSERT(args.length() == 2); |
| 2025 | |
| 2026 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
| 2027 | CONVERT_CHECKED(String, name, args[1]); |
| 2028 | fun->SetInstanceClassName(name); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2029 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2033 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetLength) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2034 | NoHandleAllocation ha; |
| 2035 | ASSERT(args.length() == 2); |
| 2036 | |
| 2037 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
| 2038 | CONVERT_CHECKED(Smi, length, args[1]); |
| 2039 | fun->shared()->set_length(length->value()); |
| 2040 | return length; |
| 2041 | } |
| 2042 | |
| 2043 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2044 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetPrototype) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2045 | NoHandleAllocation ha; |
| 2046 | ASSERT(args.length() == 2); |
| 2047 | |
| 2048 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2049 | ASSERT(fun->should_have_prototype()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2050 | Object* obj; |
| 2051 | { MaybeObject* maybe_obj = |
| 2052 | Accessors::FunctionSetPrototype(fun, args[1], NULL); |
| 2053 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 2054 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2055 | return args[0]; // return TOS |
| 2056 | } |
| 2057 | |
| 2058 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2059 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetReadOnlyPrototype) { |
| 2060 | NoHandleAllocation ha; |
| 2061 | RUNTIME_ASSERT(args.length() == 1); |
| 2062 | CONVERT_CHECKED(JSFunction, function, args[0]); |
| 2063 | |
| 2064 | MaybeObject* maybe_name = |
| 2065 | isolate->heap()->AllocateStringFromAscii(CStrVector("prototype")); |
| 2066 | String* name; |
| 2067 | if (!maybe_name->To(&name)) return maybe_name; |
| 2068 | |
| 2069 | if (function->HasFastProperties()) { |
| 2070 | // Construct a new field descriptor with updated attributes. |
| 2071 | DescriptorArray* instance_desc = function->map()->instance_descriptors(); |
| 2072 | int index = instance_desc->Search(name); |
| 2073 | ASSERT(index != DescriptorArray::kNotFound); |
| 2074 | PropertyDetails details(instance_desc->GetDetails(index)); |
| 2075 | CallbacksDescriptor new_desc(name, |
| 2076 | instance_desc->GetValue(index), |
| 2077 | static_cast<PropertyAttributes>(details.attributes() | READ_ONLY), |
| 2078 | details.index()); |
| 2079 | // Construct a new field descriptors array containing the new descriptor. |
| 2080 | Object* descriptors_unchecked; |
| 2081 | { MaybeObject* maybe_descriptors_unchecked = |
| 2082 | instance_desc->CopyInsert(&new_desc, REMOVE_TRANSITIONS); |
| 2083 | if (!maybe_descriptors_unchecked->ToObject(&descriptors_unchecked)) { |
| 2084 | return maybe_descriptors_unchecked; |
| 2085 | } |
| 2086 | } |
| 2087 | DescriptorArray* new_descriptors = |
| 2088 | DescriptorArray::cast(descriptors_unchecked); |
| 2089 | // Create a new map featuring the new field descriptors array. |
| 2090 | Object* map_unchecked; |
| 2091 | { MaybeObject* maybe_map_unchecked = function->map()->CopyDropDescriptors(); |
| 2092 | if (!maybe_map_unchecked->ToObject(&map_unchecked)) { |
| 2093 | return maybe_map_unchecked; |
| 2094 | } |
| 2095 | } |
| 2096 | Map* new_map = Map::cast(map_unchecked); |
| 2097 | new_map->set_instance_descriptors(new_descriptors); |
| 2098 | function->set_map(new_map); |
| 2099 | } else { // Dictionary properties. |
| 2100 | // Directly manipulate the property details. |
| 2101 | int entry = function->property_dictionary()->FindEntry(name); |
| 2102 | ASSERT(entry != StringDictionary::kNotFound); |
| 2103 | PropertyDetails details = function->property_dictionary()->DetailsAt(entry); |
| 2104 | PropertyDetails new_details( |
| 2105 | static_cast<PropertyAttributes>(details.attributes() | READ_ONLY), |
| 2106 | details.type(), |
| 2107 | details.index()); |
| 2108 | function->property_dictionary()->DetailsAtPut(entry, new_details); |
| 2109 | } |
| 2110 | return function; |
| 2111 | } |
| 2112 | |
| 2113 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2114 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionIsAPIFunction) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2115 | NoHandleAllocation ha; |
| 2116 | ASSERT(args.length() == 1); |
| 2117 | |
| 2118 | CONVERT_CHECKED(JSFunction, f, args[0]); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2119 | return isolate->heap()->ToBoolean(f->shared()->IsApiFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2122 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2123 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionIsBuiltin) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2124 | NoHandleAllocation ha; |
| 2125 | ASSERT(args.length() == 1); |
| 2126 | |
| 2127 | CONVERT_CHECKED(JSFunction, f, args[0]); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2128 | return isolate->heap()->ToBoolean(f->IsBuiltin()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2132 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetCode) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2133 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2134 | ASSERT(args.length() == 2); |
| 2135 | |
| 2136 | CONVERT_ARG_CHECKED(JSFunction, target, 0); |
| 2137 | Handle<Object> code = args.at<Object>(1); |
| 2138 | |
| 2139 | Handle<Context> context(target->context()); |
| 2140 | |
| 2141 | if (!code->IsNull()) { |
| 2142 | RUNTIME_ASSERT(code->IsJSFunction()); |
| 2143 | Handle<JSFunction> fun = Handle<JSFunction>::cast(code); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2144 | Handle<SharedFunctionInfo> shared(fun->shared()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2145 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 2146 | if (!SharedFunctionInfo::EnsureCompiled(shared, KEEP_EXCEPTION)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2147 | return Failure::Exception(); |
| 2148 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2149 | // Since we don't store the source for this we should never |
| 2150 | // optimize this. |
| 2151 | shared->code()->set_optimizable(false); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2152 | // Set the code, scope info, formal parameter count, |
| 2153 | // and the length of the target function. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2154 | target->shared()->set_code(shared->code()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2155 | target->ReplaceCode(shared->code()); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2156 | target->shared()->set_scope_info(shared->scope_info()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2157 | target->shared()->set_length(shared->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2158 | target->shared()->set_formal_parameter_count( |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2159 | shared->formal_parameter_count()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2160 | // Set the source code of the target function to undefined. |
| 2161 | // SetCode is only used for built-in constructors like String, |
| 2162 | // Array, and Object, and some web code |
| 2163 | // doesn't like seeing source code for constructors. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2164 | target->shared()->set_script(isolate->heap()->undefined_value()); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 2165 | target->shared()->code()->set_optimizable(false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2166 | // Clear the optimization hints related to the compiled code as these are no |
| 2167 | // longer valid when the code is overwritten. |
| 2168 | target->shared()->ClearThisPropertyAssignmentsInfo(); |
| 2169 | context = Handle<Context>(fun->context()); |
| 2170 | |
| 2171 | // Make sure we get a fresh copy of the literal vector to avoid |
| 2172 | // cross context contamination. |
| 2173 | int number_of_literals = fun->NumberOfLiterals(); |
| 2174 | Handle<FixedArray> literals = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2175 | isolate->factory()->NewFixedArray(number_of_literals, TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2176 | if (number_of_literals > 0) { |
| 2177 | // Insert the object, regexp and array functions in the literals |
| 2178 | // array prefix. These are the functions that will be used when |
| 2179 | // creating object, regexp and array literals. |
| 2180 | literals->set(JSFunction::kLiteralGlobalContextIndex, |
| 2181 | context->global_context()); |
| 2182 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 2183 | target->set_literals(*literals); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2184 | target->set_next_function_link(isolate->heap()->undefined_value()); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2185 | |
| 2186 | if (isolate->logger()->is_logging() || CpuProfiler::is_profiling(isolate)) { |
| 2187 | isolate->logger()->LogExistingFunction( |
| 2188 | shared, Handle<Code>(shared->code())); |
| 2189 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | target->set_context(*context); |
| 2193 | return *target; |
| 2194 | } |
| 2195 | |
| 2196 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2197 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetExpectedNumberOfProperties) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2198 | HandleScope scope(isolate); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2199 | ASSERT(args.length() == 2); |
| 2200 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2201 | CONVERT_SMI_ARG_CHECKED(num, 1); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2202 | RUNTIME_ASSERT(num >= 0); |
| 2203 | SetExpectedNofProperties(function, num); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2204 | return isolate->heap()->undefined_value(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2208 | MUST_USE_RESULT static MaybeObject* CharFromCode(Isolate* isolate, |
| 2209 | Object* char_code) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2210 | uint32_t code; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2211 | if (char_code->ToArrayIndex(&code)) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2212 | if (code <= 0xffff) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2213 | return isolate->heap()->LookupSingleCharacterStringFromCode(code); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2214 | } |
| 2215 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2216 | return isolate->heap()->empty_string(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2220 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringCharCodeAt) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2221 | NoHandleAllocation ha; |
| 2222 | ASSERT(args.length() == 2); |
| 2223 | |
| 2224 | CONVERT_CHECKED(String, subject, args[0]); |
| 2225 | Object* index = args[1]; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2226 | RUNTIME_ASSERT(index->IsNumber()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2227 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2228 | uint32_t i = 0; |
| 2229 | if (index->IsSmi()) { |
| 2230 | int value = Smi::cast(index)->value(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2231 | if (value < 0) return isolate->heap()->nan_value(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2232 | i = value; |
| 2233 | } else { |
| 2234 | ASSERT(index->IsHeapNumber()); |
| 2235 | double value = HeapNumber::cast(index)->value(); |
| 2236 | i = static_cast<uint32_t>(DoubleToInteger(value)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2237 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2238 | |
| 2239 | // Flatten the string. If someone wants to get a char at an index |
| 2240 | // in a cons string, it is likely that more indices will be |
| 2241 | // accessed. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2242 | Object* flat; |
| 2243 | { MaybeObject* maybe_flat = subject->TryFlatten(); |
| 2244 | if (!maybe_flat->ToObject(&flat)) return maybe_flat; |
| 2245 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2246 | subject = String::cast(flat); |
| 2247 | |
| 2248 | if (i >= static_cast<uint32_t>(subject->length())) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2249 | return isolate->heap()->nan_value(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | return Smi::FromInt(subject->Get(i)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2256 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CharFromCode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2257 | NoHandleAllocation ha; |
| 2258 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2259 | return CharFromCode(isolate, args[0]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2262 | |
| 2263 | class FixedArrayBuilder { |
| 2264 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2265 | explicit FixedArrayBuilder(Isolate* isolate, int initial_capacity) |
| 2266 | : array_(isolate->factory()->NewFixedArrayWithHoles(initial_capacity)), |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 2267 | length_(0), |
| 2268 | has_non_smi_elements_(false) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2269 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2270 | // extend the array will work. |
| 2271 | ASSERT(initial_capacity > 0); |
| 2272 | } |
| 2273 | |
| 2274 | explicit FixedArrayBuilder(Handle<FixedArray> backing_store) |
| 2275 | : array_(backing_store), |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 2276 | length_(0), |
| 2277 | has_non_smi_elements_(false) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2278 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2279 | // extend the array will work. |
| 2280 | ASSERT(backing_store->length() > 0); |
| 2281 | } |
| 2282 | |
| 2283 | bool HasCapacity(int elements) { |
| 2284 | int length = array_->length(); |
| 2285 | int required_length = length_ + elements; |
| 2286 | return (length >= required_length); |
| 2287 | } |
| 2288 | |
| 2289 | void EnsureCapacity(int elements) { |
| 2290 | int length = array_->length(); |
| 2291 | int required_length = length_ + elements; |
| 2292 | if (length < required_length) { |
| 2293 | int new_length = length; |
| 2294 | do { |
| 2295 | new_length *= 2; |
| 2296 | } while (new_length < required_length); |
| 2297 | Handle<FixedArray> extended_array = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2298 | array_->GetIsolate()->factory()->NewFixedArrayWithHoles(new_length); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2299 | array_->CopyTo(0, *extended_array, 0, length_); |
| 2300 | array_ = extended_array; |
| 2301 | } |
| 2302 | } |
| 2303 | |
| 2304 | void Add(Object* value) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 2305 | ASSERT(!value->IsSmi()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2306 | ASSERT(length_ < capacity()); |
| 2307 | array_->set(length_, value); |
| 2308 | length_++; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 2309 | has_non_smi_elements_ = true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2310 | } |
| 2311 | |
| 2312 | void Add(Smi* value) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 2313 | ASSERT(value->IsSmi()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2314 | ASSERT(length_ < capacity()); |
| 2315 | array_->set(length_, value); |
| 2316 | length_++; |
| 2317 | } |
| 2318 | |
| 2319 | Handle<FixedArray> array() { |
| 2320 | return array_; |
| 2321 | } |
| 2322 | |
| 2323 | int length() { |
| 2324 | return length_; |
| 2325 | } |
| 2326 | |
| 2327 | int capacity() { |
| 2328 | return array_->length(); |
| 2329 | } |
| 2330 | |
| 2331 | Handle<JSArray> ToJSArray() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2332 | Handle<JSArray> result_array = FACTORY->NewJSArrayWithElements(array_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2333 | result_array->set_length(Smi::FromInt(length_)); |
| 2334 | return result_array; |
| 2335 | } |
| 2336 | |
| 2337 | Handle<JSArray> ToJSArray(Handle<JSArray> target_array) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 2338 | FACTORY->SetContent(target_array, array_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2339 | target_array->set_length(Smi::FromInt(length_)); |
| 2340 | return target_array; |
| 2341 | } |
| 2342 | |
| 2343 | private: |
| 2344 | Handle<FixedArray> array_; |
| 2345 | int length_; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 2346 | bool has_non_smi_elements_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2347 | }; |
| 2348 | |
| 2349 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2350 | // Forward declarations. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2351 | const int kStringBuilderConcatHelperLengthBits = 11; |
| 2352 | const int kStringBuilderConcatHelperPositionBits = 19; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2353 | |
| 2354 | template <typename schar> |
| 2355 | static inline void StringBuilderConcatHelper(String*, |
| 2356 | schar*, |
| 2357 | FixedArray*, |
| 2358 | int); |
| 2359 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2360 | typedef BitField<int, 0, kStringBuilderConcatHelperLengthBits> |
| 2361 | StringBuilderSubstringLength; |
| 2362 | typedef BitField<int, |
| 2363 | kStringBuilderConcatHelperLengthBits, |
| 2364 | kStringBuilderConcatHelperPositionBits> |
| 2365 | StringBuilderSubstringPosition; |
| 2366 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2367 | |
| 2368 | class ReplacementStringBuilder { |
| 2369 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2370 | ReplacementStringBuilder(Heap* heap, |
| 2371 | Handle<String> subject, |
| 2372 | int estimated_part_count) |
| 2373 | : heap_(heap), |
| 2374 | array_builder_(heap->isolate(), estimated_part_count), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2375 | subject_(subject), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2376 | character_count_(0), |
| 2377 | is_ascii_(subject->IsAsciiRepresentation()) { |
| 2378 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2379 | // extend the array will work. |
| 2380 | ASSERT(estimated_part_count > 0); |
| 2381 | } |
| 2382 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2383 | static inline void AddSubjectSlice(FixedArrayBuilder* builder, |
| 2384 | int from, |
| 2385 | int to) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2386 | ASSERT(from >= 0); |
| 2387 | int length = to - from; |
| 2388 | ASSERT(length > 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2389 | if (StringBuilderSubstringLength::is_valid(length) && |
| 2390 | StringBuilderSubstringPosition::is_valid(from)) { |
| 2391 | int encoded_slice = StringBuilderSubstringLength::encode(length) | |
| 2392 | StringBuilderSubstringPosition::encode(from); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2393 | builder->Add(Smi::FromInt(encoded_slice)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2394 | } else { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2395 | // Otherwise encode as two smis. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2396 | builder->Add(Smi::FromInt(-length)); |
| 2397 | builder->Add(Smi::FromInt(from)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2398 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | |
| 2402 | void EnsureCapacity(int elements) { |
| 2403 | array_builder_.EnsureCapacity(elements); |
| 2404 | } |
| 2405 | |
| 2406 | |
| 2407 | void AddSubjectSlice(int from, int to) { |
| 2408 | AddSubjectSlice(&array_builder_, from, to); |
| 2409 | IncrementCharacterCount(to - from); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
| 2412 | |
| 2413 | void AddString(Handle<String> string) { |
| 2414 | int length = string->length(); |
| 2415 | ASSERT(length > 0); |
| 2416 | AddElement(*string); |
| 2417 | if (!string->IsAsciiRepresentation()) { |
| 2418 | is_ascii_ = false; |
| 2419 | } |
| 2420 | IncrementCharacterCount(length); |
| 2421 | } |
| 2422 | |
| 2423 | |
| 2424 | Handle<String> ToString() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2425 | if (array_builder_.length() == 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2426 | return heap_->isolate()->factory()->empty_string(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
| 2429 | Handle<String> joined_string; |
| 2430 | if (is_ascii_) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2431 | Handle<SeqAsciiString> seq = NewRawAsciiString(character_count_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2432 | AssertNoAllocation no_alloc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2433 | char* char_buffer = seq->GetChars(); |
| 2434 | StringBuilderConcatHelper(*subject_, |
| 2435 | char_buffer, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2436 | *array_builder_.array(), |
| 2437 | array_builder_.length()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2438 | joined_string = Handle<String>::cast(seq); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2439 | } else { |
| 2440 | // Non-ASCII. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2441 | Handle<SeqTwoByteString> seq = NewRawTwoByteString(character_count_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2442 | AssertNoAllocation no_alloc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2443 | uc16* char_buffer = seq->GetChars(); |
| 2444 | StringBuilderConcatHelper(*subject_, |
| 2445 | char_buffer, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2446 | *array_builder_.array(), |
| 2447 | array_builder_.length()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2448 | joined_string = Handle<String>::cast(seq); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2449 | } |
| 2450 | return joined_string; |
| 2451 | } |
| 2452 | |
| 2453 | |
| 2454 | void IncrementCharacterCount(int by) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2455 | if (character_count_ > String::kMaxLength - by) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2456 | V8::FatalProcessOutOfMemory("String.replace result too large."); |
| 2457 | } |
| 2458 | character_count_ += by; |
| 2459 | } |
| 2460 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2461 | Handle<JSArray> GetParts() { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2462 | return array_builder_.ToJSArray(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2463 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2464 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2465 | private: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2466 | Handle<SeqAsciiString> NewRawAsciiString(int length) { |
| 2467 | return heap_->isolate()->factory()->NewRawAsciiString(length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2471 | Handle<SeqTwoByteString> NewRawTwoByteString(int length) { |
| 2472 | return heap_->isolate()->factory()->NewRawTwoByteString(length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | |
| 2476 | void AddElement(Object* element) { |
| 2477 | ASSERT(element->IsSmi() || element->IsString()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2478 | ASSERT(array_builder_.capacity() > array_builder_.length()); |
| 2479 | array_builder_.Add(element); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2482 | Heap* heap_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2483 | FixedArrayBuilder array_builder_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2484 | Handle<String> subject_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2485 | int character_count_; |
| 2486 | bool is_ascii_; |
| 2487 | }; |
| 2488 | |
| 2489 | |
| 2490 | class CompiledReplacement { |
| 2491 | public: |
| 2492 | CompiledReplacement() |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2493 | : parts_(1), replacement_substrings_(0), simple_hint_(false) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2494 | |
| 2495 | void Compile(Handle<String> replacement, |
| 2496 | int capture_count, |
| 2497 | int subject_length); |
| 2498 | |
| 2499 | void Apply(ReplacementStringBuilder* builder, |
| 2500 | int match_from, |
| 2501 | int match_to, |
| 2502 | Handle<JSArray> last_match_info); |
| 2503 | |
| 2504 | // Number of distinct parts of the replacement pattern. |
| 2505 | int parts() { |
| 2506 | return parts_.length(); |
| 2507 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2508 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2509 | bool simple_hint() { |
| 2510 | return simple_hint_; |
| 2511 | } |
| 2512 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2513 | private: |
| 2514 | enum PartType { |
| 2515 | SUBJECT_PREFIX = 1, |
| 2516 | SUBJECT_SUFFIX, |
| 2517 | SUBJECT_CAPTURE, |
| 2518 | REPLACEMENT_SUBSTRING, |
| 2519 | REPLACEMENT_STRING, |
| 2520 | |
| 2521 | NUMBER_OF_PART_TYPES |
| 2522 | }; |
| 2523 | |
| 2524 | struct ReplacementPart { |
| 2525 | static inline ReplacementPart SubjectMatch() { |
| 2526 | return ReplacementPart(SUBJECT_CAPTURE, 0); |
| 2527 | } |
| 2528 | static inline ReplacementPart SubjectCapture(int capture_index) { |
| 2529 | return ReplacementPart(SUBJECT_CAPTURE, capture_index); |
| 2530 | } |
| 2531 | static inline ReplacementPart SubjectPrefix() { |
| 2532 | return ReplacementPart(SUBJECT_PREFIX, 0); |
| 2533 | } |
| 2534 | static inline ReplacementPart SubjectSuffix(int subject_length) { |
| 2535 | return ReplacementPart(SUBJECT_SUFFIX, subject_length); |
| 2536 | } |
| 2537 | static inline ReplacementPart ReplacementString() { |
| 2538 | return ReplacementPart(REPLACEMENT_STRING, 0); |
| 2539 | } |
| 2540 | static inline ReplacementPart ReplacementSubString(int from, int to) { |
| 2541 | ASSERT(from >= 0); |
| 2542 | ASSERT(to > from); |
| 2543 | return ReplacementPart(-from, to); |
| 2544 | } |
| 2545 | |
| 2546 | // If tag <= 0 then it is the negation of a start index of a substring of |
| 2547 | // the replacement pattern, otherwise it's a value from PartType. |
| 2548 | ReplacementPart(int tag, int data) |
| 2549 | : tag(tag), data(data) { |
| 2550 | // Must be non-positive or a PartType value. |
| 2551 | ASSERT(tag < NUMBER_OF_PART_TYPES); |
| 2552 | } |
| 2553 | // Either a value of PartType or a non-positive number that is |
| 2554 | // the negation of an index into the replacement string. |
| 2555 | int tag; |
| 2556 | // The data value's interpretation depends on the value of tag: |
| 2557 | // tag == SUBJECT_PREFIX || |
| 2558 | // tag == SUBJECT_SUFFIX: data is unused. |
| 2559 | // tag == SUBJECT_CAPTURE: data is the number of the capture. |
| 2560 | // tag == REPLACEMENT_SUBSTRING || |
| 2561 | // tag == REPLACEMENT_STRING: data is index into array of substrings |
| 2562 | // of the replacement string. |
| 2563 | // tag <= 0: Temporary representation of the substring of the replacement |
| 2564 | // string ranging over -tag .. data. |
| 2565 | // Is replaced by REPLACEMENT_{SUB,}STRING when we create the |
| 2566 | // substring objects. |
| 2567 | int data; |
| 2568 | }; |
| 2569 | |
| 2570 | template<typename Char> |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2571 | static bool ParseReplacementPattern(ZoneList<ReplacementPart>* parts, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2572 | Vector<Char> characters, |
| 2573 | int capture_count, |
| 2574 | int subject_length) { |
| 2575 | int length = characters.length(); |
| 2576 | int last = 0; |
| 2577 | for (int i = 0; i < length; i++) { |
| 2578 | Char c = characters[i]; |
| 2579 | if (c == '$') { |
| 2580 | int next_index = i + 1; |
| 2581 | if (next_index == length) { // No next character! |
| 2582 | break; |
| 2583 | } |
| 2584 | Char c2 = characters[next_index]; |
| 2585 | switch (c2) { |
| 2586 | case '$': |
| 2587 | if (i > last) { |
| 2588 | // There is a substring before. Include the first "$". |
| 2589 | parts->Add(ReplacementPart::ReplacementSubString(last, next_index)); |
| 2590 | last = next_index + 1; // Continue after the second "$". |
| 2591 | } else { |
| 2592 | // Let the next substring start with the second "$". |
| 2593 | last = next_index; |
| 2594 | } |
| 2595 | i = next_index; |
| 2596 | break; |
| 2597 | case '`': |
| 2598 | if (i > last) { |
| 2599 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2600 | } |
| 2601 | parts->Add(ReplacementPart::SubjectPrefix()); |
| 2602 | i = next_index; |
| 2603 | last = i + 1; |
| 2604 | break; |
| 2605 | case '\'': |
| 2606 | if (i > last) { |
| 2607 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2608 | } |
| 2609 | parts->Add(ReplacementPart::SubjectSuffix(subject_length)); |
| 2610 | i = next_index; |
| 2611 | last = i + 1; |
| 2612 | break; |
| 2613 | case '&': |
| 2614 | if (i > last) { |
| 2615 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2616 | } |
| 2617 | parts->Add(ReplacementPart::SubjectMatch()); |
| 2618 | i = next_index; |
| 2619 | last = i + 1; |
| 2620 | break; |
| 2621 | case '0': |
| 2622 | case '1': |
| 2623 | case '2': |
| 2624 | case '3': |
| 2625 | case '4': |
| 2626 | case '5': |
| 2627 | case '6': |
| 2628 | case '7': |
| 2629 | case '8': |
| 2630 | case '9': { |
| 2631 | int capture_ref = c2 - '0'; |
| 2632 | if (capture_ref > capture_count) { |
| 2633 | i = next_index; |
| 2634 | continue; |
| 2635 | } |
| 2636 | int second_digit_index = next_index + 1; |
| 2637 | if (second_digit_index < length) { |
| 2638 | // Peek ahead to see if we have two digits. |
| 2639 | Char c3 = characters[second_digit_index]; |
| 2640 | if ('0' <= c3 && c3 <= '9') { // Double digits. |
| 2641 | int double_digit_ref = capture_ref * 10 + c3 - '0'; |
| 2642 | if (double_digit_ref <= capture_count) { |
| 2643 | next_index = second_digit_index; |
| 2644 | capture_ref = double_digit_ref; |
| 2645 | } |
| 2646 | } |
| 2647 | } |
| 2648 | if (capture_ref > 0) { |
| 2649 | if (i > last) { |
| 2650 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2651 | } |
| 2652 | ASSERT(capture_ref <= capture_count); |
| 2653 | parts->Add(ReplacementPart::SubjectCapture(capture_ref)); |
| 2654 | last = next_index + 1; |
| 2655 | } |
| 2656 | i = next_index; |
| 2657 | break; |
| 2658 | } |
| 2659 | default: |
| 2660 | i = next_index; |
| 2661 | break; |
| 2662 | } |
| 2663 | } |
| 2664 | } |
| 2665 | if (length > last) { |
| 2666 | if (last == 0) { |
| 2667 | parts->Add(ReplacementPart::ReplacementString()); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2668 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2669 | } else { |
| 2670 | parts->Add(ReplacementPart::ReplacementSubString(last, length)); |
| 2671 | } |
| 2672 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2673 | return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2674 | } |
| 2675 | |
| 2676 | ZoneList<ReplacementPart> parts_; |
| 2677 | ZoneList<Handle<String> > replacement_substrings_; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2678 | bool simple_hint_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2679 | }; |
| 2680 | |
| 2681 | |
| 2682 | void CompiledReplacement::Compile(Handle<String> replacement, |
| 2683 | int capture_count, |
| 2684 | int subject_length) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2685 | { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2686 | AssertNoAllocation no_alloc; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2687 | String::FlatContent content = replacement->GetFlatContent(); |
| 2688 | ASSERT(content.IsFlat()); |
| 2689 | if (content.IsAscii()) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2690 | simple_hint_ = ParseReplacementPattern(&parts_, |
| 2691 | content.ToAsciiVector(), |
| 2692 | capture_count, |
| 2693 | subject_length); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2694 | } else { |
| 2695 | ASSERT(content.IsTwoByte()); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2696 | simple_hint_ = ParseReplacementPattern(&parts_, |
| 2697 | content.ToUC16Vector(), |
| 2698 | capture_count, |
| 2699 | subject_length); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2700 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2701 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2702 | Isolate* isolate = replacement->GetIsolate(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2703 | // Find substrings of replacement string and create them as String objects. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2704 | int substring_index = 0; |
| 2705 | for (int i = 0, n = parts_.length(); i < n; i++) { |
| 2706 | int tag = parts_[i].tag; |
| 2707 | if (tag <= 0) { // A replacement string slice. |
| 2708 | int from = -tag; |
| 2709 | int to = parts_[i].data; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2710 | replacement_substrings_.Add( |
| 2711 | isolate->factory()->NewSubString(replacement, from, to)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2712 | parts_[i].tag = REPLACEMENT_SUBSTRING; |
| 2713 | parts_[i].data = substring_index; |
| 2714 | substring_index++; |
| 2715 | } else if (tag == REPLACEMENT_STRING) { |
| 2716 | replacement_substrings_.Add(replacement); |
| 2717 | parts_[i].data = substring_index; |
| 2718 | substring_index++; |
| 2719 | } |
| 2720 | } |
| 2721 | } |
| 2722 | |
| 2723 | |
| 2724 | void CompiledReplacement::Apply(ReplacementStringBuilder* builder, |
| 2725 | int match_from, |
| 2726 | int match_to, |
| 2727 | Handle<JSArray> last_match_info) { |
| 2728 | for (int i = 0, n = parts_.length(); i < n; i++) { |
| 2729 | ReplacementPart part = parts_[i]; |
| 2730 | switch (part.tag) { |
| 2731 | case SUBJECT_PREFIX: |
| 2732 | if (match_from > 0) builder->AddSubjectSlice(0, match_from); |
| 2733 | break; |
| 2734 | case SUBJECT_SUFFIX: { |
| 2735 | int subject_length = part.data; |
| 2736 | if (match_to < subject_length) { |
| 2737 | builder->AddSubjectSlice(match_to, subject_length); |
| 2738 | } |
| 2739 | break; |
| 2740 | } |
| 2741 | case SUBJECT_CAPTURE: { |
| 2742 | int capture = part.data; |
| 2743 | FixedArray* match_info = FixedArray::cast(last_match_info->elements()); |
| 2744 | int from = RegExpImpl::GetCapture(match_info, capture * 2); |
| 2745 | int to = RegExpImpl::GetCapture(match_info, capture * 2 + 1); |
| 2746 | if (from >= 0 && to > from) { |
| 2747 | builder->AddSubjectSlice(from, to); |
| 2748 | } |
| 2749 | break; |
| 2750 | } |
| 2751 | case REPLACEMENT_SUBSTRING: |
| 2752 | case REPLACEMENT_STRING: |
| 2753 | builder->AddString(replacement_substrings_[part.data]); |
| 2754 | break; |
| 2755 | default: |
| 2756 | UNREACHABLE(); |
| 2757 | } |
| 2758 | } |
| 2759 | } |
| 2760 | |
| 2761 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2762 | void FindAsciiStringIndices(Vector<const char> subject, |
| 2763 | char pattern, |
| 2764 | ZoneList<int>* indices, |
| 2765 | unsigned int limit) { |
| 2766 | ASSERT(limit > 0); |
| 2767 | // Collect indices of pattern in subject using memchr. |
| 2768 | // Stop after finding at most limit values. |
| 2769 | const char* subject_start = reinterpret_cast<const char*>(subject.start()); |
| 2770 | const char* subject_end = subject_start + subject.length(); |
| 2771 | const char* pos = subject_start; |
| 2772 | while (limit > 0) { |
| 2773 | pos = reinterpret_cast<const char*>( |
| 2774 | memchr(pos, pattern, subject_end - pos)); |
| 2775 | if (pos == NULL) return; |
| 2776 | indices->Add(static_cast<int>(pos - subject_start)); |
| 2777 | pos++; |
| 2778 | limit--; |
| 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | |
| 2783 | template <typename SubjectChar, typename PatternChar> |
| 2784 | void FindStringIndices(Isolate* isolate, |
| 2785 | Vector<const SubjectChar> subject, |
| 2786 | Vector<const PatternChar> pattern, |
| 2787 | ZoneList<int>* indices, |
| 2788 | unsigned int limit) { |
| 2789 | ASSERT(limit > 0); |
| 2790 | // Collect indices of pattern in subject. |
| 2791 | // Stop after finding at most limit values. |
| 2792 | int pattern_length = pattern.length(); |
| 2793 | int index = 0; |
| 2794 | StringSearch<PatternChar, SubjectChar> search(isolate, pattern); |
| 2795 | while (limit > 0) { |
| 2796 | index = search.Search(subject, index); |
| 2797 | if (index < 0) return; |
| 2798 | indices->Add(index); |
| 2799 | index += pattern_length; |
| 2800 | limit--; |
| 2801 | } |
| 2802 | } |
| 2803 | |
| 2804 | |
| 2805 | void FindStringIndicesDispatch(Isolate* isolate, |
| 2806 | String* subject, |
| 2807 | String* pattern, |
| 2808 | ZoneList<int>* indices, |
| 2809 | unsigned int limit) { |
| 2810 | { |
| 2811 | AssertNoAllocation no_gc; |
| 2812 | String::FlatContent subject_content = subject->GetFlatContent(); |
| 2813 | String::FlatContent pattern_content = pattern->GetFlatContent(); |
| 2814 | ASSERT(subject_content.IsFlat()); |
| 2815 | ASSERT(pattern_content.IsFlat()); |
| 2816 | if (subject_content.IsAscii()) { |
| 2817 | Vector<const char> subject_vector = subject_content.ToAsciiVector(); |
| 2818 | if (pattern_content.IsAscii()) { |
| 2819 | Vector<const char> pattern_vector = pattern_content.ToAsciiVector(); |
| 2820 | if (pattern_vector.length() == 1) { |
| 2821 | FindAsciiStringIndices(subject_vector, |
| 2822 | pattern_vector[0], |
| 2823 | indices, |
| 2824 | limit); |
| 2825 | } else { |
| 2826 | FindStringIndices(isolate, |
| 2827 | subject_vector, |
| 2828 | pattern_vector, |
| 2829 | indices, |
| 2830 | limit); |
| 2831 | } |
| 2832 | } else { |
| 2833 | FindStringIndices(isolate, |
| 2834 | subject_vector, |
| 2835 | pattern_content.ToUC16Vector(), |
| 2836 | indices, |
| 2837 | limit); |
| 2838 | } |
| 2839 | } else { |
| 2840 | Vector<const uc16> subject_vector = subject_content.ToUC16Vector(); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 2841 | if (pattern_content.IsAscii()) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2842 | FindStringIndices(isolate, |
| 2843 | subject_vector, |
| 2844 | pattern_content.ToAsciiVector(), |
| 2845 | indices, |
| 2846 | limit); |
| 2847 | } else { |
| 2848 | FindStringIndices(isolate, |
| 2849 | subject_vector, |
| 2850 | pattern_content.ToUC16Vector(), |
| 2851 | indices, |
| 2852 | limit); |
| 2853 | } |
| 2854 | } |
| 2855 | } |
| 2856 | } |
| 2857 | |
| 2858 | |
| 2859 | template<typename ResultSeqString> |
| 2860 | MUST_USE_RESULT static MaybeObject* StringReplaceStringWithString( |
| 2861 | Isolate* isolate, |
| 2862 | Handle<String> subject, |
| 2863 | Handle<JSRegExp> pattern_regexp, |
| 2864 | Handle<String> replacement) { |
| 2865 | ASSERT(subject->IsFlat()); |
| 2866 | ASSERT(replacement->IsFlat()); |
| 2867 | |
| 2868 | ZoneScope zone_space(isolate, DELETE_ON_EXIT); |
| 2869 | ZoneList<int> indices(8); |
| 2870 | ASSERT_EQ(JSRegExp::ATOM, pattern_regexp->TypeTag()); |
| 2871 | String* pattern = |
| 2872 | String::cast(pattern_regexp->DataAt(JSRegExp::kAtomPatternIndex)); |
| 2873 | int subject_len = subject->length(); |
| 2874 | int pattern_len = pattern->length(); |
| 2875 | int replacement_len = replacement->length(); |
| 2876 | |
| 2877 | FindStringIndicesDispatch(isolate, *subject, pattern, &indices, 0xffffffff); |
| 2878 | |
| 2879 | int matches = indices.length(); |
| 2880 | if (matches == 0) return *subject; |
| 2881 | |
| 2882 | int result_len = (replacement_len - pattern_len) * matches + subject_len; |
| 2883 | int subject_pos = 0; |
| 2884 | int result_pos = 0; |
| 2885 | |
| 2886 | Handle<ResultSeqString> result; |
| 2887 | if (ResultSeqString::kHasAsciiEncoding) { |
| 2888 | result = Handle<ResultSeqString>::cast( |
| 2889 | isolate->factory()->NewRawAsciiString(result_len)); |
| 2890 | } else { |
| 2891 | result = Handle<ResultSeqString>::cast( |
| 2892 | isolate->factory()->NewRawTwoByteString(result_len)); |
| 2893 | } |
| 2894 | |
| 2895 | for (int i = 0; i < matches; i++) { |
| 2896 | // Copy non-matched subject content. |
| 2897 | if (subject_pos < indices.at(i)) { |
| 2898 | String::WriteToFlat(*subject, |
| 2899 | result->GetChars() + result_pos, |
| 2900 | subject_pos, |
| 2901 | indices.at(i)); |
| 2902 | result_pos += indices.at(i) - subject_pos; |
| 2903 | } |
| 2904 | |
| 2905 | // Replace match. |
| 2906 | if (replacement_len > 0) { |
| 2907 | String::WriteToFlat(*replacement, |
| 2908 | result->GetChars() + result_pos, |
| 2909 | 0, |
| 2910 | replacement_len); |
| 2911 | result_pos += replacement_len; |
| 2912 | } |
| 2913 | |
| 2914 | subject_pos = indices.at(i) + pattern_len; |
| 2915 | } |
| 2916 | // Add remaining subject content at the end. |
| 2917 | if (subject_pos < subject_len) { |
| 2918 | String::WriteToFlat(*subject, |
| 2919 | result->GetChars() + result_pos, |
| 2920 | subject_pos, |
| 2921 | subject_len); |
| 2922 | } |
| 2923 | return *result; |
| 2924 | } |
| 2925 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2926 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2927 | MUST_USE_RESULT static MaybeObject* StringReplaceRegExpWithString( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2928 | Isolate* isolate, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2929 | String* subject, |
| 2930 | JSRegExp* regexp, |
| 2931 | String* replacement, |
| 2932 | JSArray* last_match_info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2933 | ASSERT(subject->IsFlat()); |
| 2934 | ASSERT(replacement->IsFlat()); |
| 2935 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2936 | HandleScope handles(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2937 | |
| 2938 | int length = subject->length(); |
| 2939 | Handle<String> subject_handle(subject); |
| 2940 | Handle<JSRegExp> regexp_handle(regexp); |
| 2941 | Handle<String> replacement_handle(replacement); |
| 2942 | Handle<JSArray> last_match_info_handle(last_match_info); |
| 2943 | Handle<Object> match = RegExpImpl::Exec(regexp_handle, |
| 2944 | subject_handle, |
| 2945 | 0, |
| 2946 | last_match_info_handle); |
| 2947 | if (match.is_null()) { |
| 2948 | return Failure::Exception(); |
| 2949 | } |
| 2950 | if (match->IsNull()) { |
| 2951 | return *subject_handle; |
| 2952 | } |
| 2953 | |
| 2954 | int capture_count = regexp_handle->CaptureCount(); |
| 2955 | |
| 2956 | // CompiledReplacement uses zone allocation. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2957 | ZoneScope zone(isolate, DELETE_ON_EXIT); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2958 | CompiledReplacement compiled_replacement; |
| 2959 | compiled_replacement.Compile(replacement_handle, |
| 2960 | capture_count, |
| 2961 | length); |
| 2962 | |
| 2963 | bool is_global = regexp_handle->GetFlags().is_global(); |
| 2964 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2965 | // Shortcut for simple non-regexp global replacements |
| 2966 | if (is_global && |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 2967 | regexp_handle->TypeTag() == JSRegExp::ATOM && |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2968 | compiled_replacement.simple_hint()) { |
| 2969 | if (subject_handle->HasOnlyAsciiChars() && |
| 2970 | replacement_handle->HasOnlyAsciiChars()) { |
| 2971 | return StringReplaceStringWithString<SeqAsciiString>( |
| 2972 | isolate, subject_handle, regexp_handle, replacement_handle); |
| 2973 | } else { |
| 2974 | return StringReplaceStringWithString<SeqTwoByteString>( |
| 2975 | isolate, subject_handle, regexp_handle, replacement_handle); |
| 2976 | } |
| 2977 | } |
| 2978 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2979 | // Guessing the number of parts that the final result string is built |
| 2980 | // from. Global regexps can match any number of times, so we guess |
| 2981 | // conservatively. |
| 2982 | int expected_parts = |
| 2983 | (compiled_replacement.parts() + 1) * (is_global ? 4 : 1) + 1; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2984 | ReplacementStringBuilder builder(isolate->heap(), |
| 2985 | subject_handle, |
| 2986 | expected_parts); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2987 | |
| 2988 | // Index of end of last match. |
| 2989 | int prev = 0; |
| 2990 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2991 | // Number of parts added by compiled replacement plus preceeding |
| 2992 | // string and possibly suffix after last match. It is possible for |
| 2993 | // all components to use two elements when encoded as two smis. |
| 2994 | const int parts_added_per_loop = 2 * (compiled_replacement.parts() + 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2995 | bool matched = true; |
| 2996 | do { |
| 2997 | ASSERT(last_match_info_handle->HasFastElements()); |
| 2998 | // Increase the capacity of the builder before entering local handle-scope, |
| 2999 | // so its internal buffer can safely allocate a new handle if it grows. |
| 3000 | builder.EnsureCapacity(parts_added_per_loop); |
| 3001 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3002 | HandleScope loop_scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3003 | int start, end; |
| 3004 | { |
| 3005 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
| 3006 | FixedArray* match_info_array = |
| 3007 | FixedArray::cast(last_match_info_handle->elements()); |
| 3008 | |
| 3009 | ASSERT_EQ(capture_count * 2 + 2, |
| 3010 | RegExpImpl::GetLastCaptureCount(match_info_array)); |
| 3011 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 3012 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 3013 | } |
| 3014 | |
| 3015 | if (prev < start) { |
| 3016 | builder.AddSubjectSlice(prev, start); |
| 3017 | } |
| 3018 | compiled_replacement.Apply(&builder, |
| 3019 | start, |
| 3020 | end, |
| 3021 | last_match_info_handle); |
| 3022 | prev = end; |
| 3023 | |
| 3024 | // Only continue checking for global regexps. |
| 3025 | if (!is_global) break; |
| 3026 | |
| 3027 | // Continue from where the match ended, unless it was an empty match. |
| 3028 | int next = end; |
| 3029 | if (start == end) { |
| 3030 | next = end + 1; |
| 3031 | if (next > length) break; |
| 3032 | } |
| 3033 | |
| 3034 | match = RegExpImpl::Exec(regexp_handle, |
| 3035 | subject_handle, |
| 3036 | next, |
| 3037 | last_match_info_handle); |
| 3038 | if (match.is_null()) { |
| 3039 | return Failure::Exception(); |
| 3040 | } |
| 3041 | matched = !match->IsNull(); |
| 3042 | } while (matched); |
| 3043 | |
| 3044 | if (prev < length) { |
| 3045 | builder.AddSubjectSlice(prev, length); |
| 3046 | } |
| 3047 | |
| 3048 | return *(builder.ToString()); |
| 3049 | } |
| 3050 | |
| 3051 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3052 | template <typename ResultSeqString> |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3053 | MUST_USE_RESULT static MaybeObject* StringReplaceRegExpWithEmptyString( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3054 | Isolate* isolate, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3055 | String* subject, |
| 3056 | JSRegExp* regexp, |
| 3057 | JSArray* last_match_info) { |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3058 | ASSERT(subject->IsFlat()); |
| 3059 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3060 | HandleScope handles(isolate); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3061 | |
| 3062 | Handle<String> subject_handle(subject); |
| 3063 | Handle<JSRegExp> regexp_handle(regexp); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 3064 | |
| 3065 | // Shortcut for simple non-regexp global replacements |
| 3066 | if (regexp_handle->GetFlags().is_global() && |
| 3067 | regexp_handle->TypeTag() == JSRegExp::ATOM) { |
| 3068 | Handle<String> empty_string_handle(HEAP->empty_string()); |
| 3069 | if (subject_handle->HasOnlyAsciiChars()) { |
| 3070 | return StringReplaceStringWithString<SeqAsciiString>( |
| 3071 | isolate, subject_handle, regexp_handle, empty_string_handle); |
| 3072 | } else { |
| 3073 | return StringReplaceStringWithString<SeqTwoByteString>( |
| 3074 | isolate, subject_handle, regexp_handle, empty_string_handle); |
| 3075 | } |
| 3076 | } |
| 3077 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3078 | Handle<JSArray> last_match_info_handle(last_match_info); |
| 3079 | Handle<Object> match = RegExpImpl::Exec(regexp_handle, |
| 3080 | subject_handle, |
| 3081 | 0, |
| 3082 | last_match_info_handle); |
| 3083 | if (match.is_null()) return Failure::Exception(); |
| 3084 | if (match->IsNull()) return *subject_handle; |
| 3085 | |
| 3086 | ASSERT(last_match_info_handle->HasFastElements()); |
| 3087 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3088 | int start, end; |
| 3089 | { |
| 3090 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
| 3091 | FixedArray* match_info_array = |
| 3092 | FixedArray::cast(last_match_info_handle->elements()); |
| 3093 | |
| 3094 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 3095 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 3096 | } |
| 3097 | |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 3098 | int length = subject_handle->length(); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3099 | int new_length = length - (end - start); |
| 3100 | if (new_length == 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3101 | return isolate->heap()->empty_string(); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3102 | } |
| 3103 | Handle<ResultSeqString> answer; |
| 3104 | if (ResultSeqString::kHasAsciiEncoding) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3105 | answer = Handle<ResultSeqString>::cast( |
| 3106 | isolate->factory()->NewRawAsciiString(new_length)); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3107 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3108 | answer = Handle<ResultSeqString>::cast( |
| 3109 | isolate->factory()->NewRawTwoByteString(new_length)); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3110 | } |
| 3111 | |
| 3112 | // If the regexp isn't global, only match once. |
| 3113 | if (!regexp_handle->GetFlags().is_global()) { |
| 3114 | if (start > 0) { |
| 3115 | String::WriteToFlat(*subject_handle, |
| 3116 | answer->GetChars(), |
| 3117 | 0, |
| 3118 | start); |
| 3119 | } |
| 3120 | if (end < length) { |
| 3121 | String::WriteToFlat(*subject_handle, |
| 3122 | answer->GetChars() + start, |
| 3123 | end, |
| 3124 | length); |
| 3125 | } |
| 3126 | return *answer; |
| 3127 | } |
| 3128 | |
| 3129 | int prev = 0; // Index of end of last match. |
| 3130 | int next = 0; // Start of next search (prev unless last match was empty). |
| 3131 | int position = 0; |
| 3132 | |
| 3133 | do { |
| 3134 | if (prev < start) { |
| 3135 | // Add substring subject[prev;start] to answer string. |
| 3136 | String::WriteToFlat(*subject_handle, |
| 3137 | answer->GetChars() + position, |
| 3138 | prev, |
| 3139 | start); |
| 3140 | position += start - prev; |
| 3141 | } |
| 3142 | prev = end; |
| 3143 | next = end; |
| 3144 | // Continue from where the match ended, unless it was an empty match. |
| 3145 | if (start == end) { |
| 3146 | next++; |
| 3147 | if (next > length) break; |
| 3148 | } |
| 3149 | match = RegExpImpl::Exec(regexp_handle, |
| 3150 | subject_handle, |
| 3151 | next, |
| 3152 | last_match_info_handle); |
| 3153 | if (match.is_null()) return Failure::Exception(); |
| 3154 | if (match->IsNull()) break; |
| 3155 | |
| 3156 | ASSERT(last_match_info_handle->HasFastElements()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3157 | HandleScope loop_scope(isolate); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3158 | { |
| 3159 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
| 3160 | FixedArray* match_info_array = |
| 3161 | FixedArray::cast(last_match_info_handle->elements()); |
| 3162 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 3163 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 3164 | } |
| 3165 | } while (true); |
| 3166 | |
| 3167 | if (prev < length) { |
| 3168 | // Add substring subject[prev;length] to answer string. |
| 3169 | String::WriteToFlat(*subject_handle, |
| 3170 | answer->GetChars() + position, |
| 3171 | prev, |
| 3172 | length); |
| 3173 | position += length - prev; |
| 3174 | } |
| 3175 | |
| 3176 | if (position == 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3177 | return isolate->heap()->empty_string(); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3178 | } |
| 3179 | |
| 3180 | // Shorten string and fill |
| 3181 | int string_size = ResultSeqString::SizeFor(position); |
| 3182 | int allocated_string_size = ResultSeqString::SizeFor(new_length); |
| 3183 | int delta = allocated_string_size - string_size; |
| 3184 | |
| 3185 | answer->set_length(position); |
| 3186 | if (delta == 0) return *answer; |
| 3187 | |
| 3188 | Address end_of_string = answer->address() + string_size; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3189 | isolate->heap()->CreateFillerObjectAt(end_of_string, delta); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 3190 | if (Marking::IsBlack(Marking::MarkBitFrom(*answer))) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 3191 | MemoryChunk::IncrementLiveBytesFromMutator(answer->address(), -delta); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 3192 | } |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3193 | |
| 3194 | return *answer; |
| 3195 | } |
| 3196 | |
| 3197 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3198 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringReplaceRegExpWithString) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3199 | ASSERT(args.length() == 4); |
| 3200 | |
| 3201 | CONVERT_CHECKED(String, subject, args[0]); |
| 3202 | if (!subject->IsFlat()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3203 | Object* flat_subject; |
| 3204 | { MaybeObject* maybe_flat_subject = subject->TryFlatten(); |
| 3205 | if (!maybe_flat_subject->ToObject(&flat_subject)) { |
| 3206 | return maybe_flat_subject; |
| 3207 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3208 | } |
| 3209 | subject = String::cast(flat_subject); |
| 3210 | } |
| 3211 | |
| 3212 | CONVERT_CHECKED(String, replacement, args[2]); |
| 3213 | if (!replacement->IsFlat()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3214 | Object* flat_replacement; |
| 3215 | { MaybeObject* maybe_flat_replacement = replacement->TryFlatten(); |
| 3216 | if (!maybe_flat_replacement->ToObject(&flat_replacement)) { |
| 3217 | return maybe_flat_replacement; |
| 3218 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3219 | } |
| 3220 | replacement = String::cast(flat_replacement); |
| 3221 | } |
| 3222 | |
| 3223 | CONVERT_CHECKED(JSRegExp, regexp, args[1]); |
| 3224 | CONVERT_CHECKED(JSArray, last_match_info, args[3]); |
| 3225 | |
| 3226 | ASSERT(last_match_info->HasFastElements()); |
| 3227 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3228 | if (replacement->length() == 0) { |
| 3229 | if (subject->HasOnlyAsciiChars()) { |
| 3230 | return StringReplaceRegExpWithEmptyString<SeqAsciiString>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3231 | isolate, subject, regexp, last_match_info); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3232 | } else { |
| 3233 | return StringReplaceRegExpWithEmptyString<SeqTwoByteString>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3234 | isolate, subject, regexp, last_match_info); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3235 | } |
| 3236 | } |
| 3237 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3238 | return StringReplaceRegExpWithString(isolate, |
| 3239 | subject, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3240 | regexp, |
| 3241 | replacement, |
| 3242 | last_match_info); |
| 3243 | } |
| 3244 | |
| 3245 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 3246 | Handle<String> Runtime::StringReplaceOneCharWithString(Isolate* isolate, |
| 3247 | Handle<String> subject, |
| 3248 | Handle<String> search, |
| 3249 | Handle<String> replace, |
| 3250 | bool* found, |
| 3251 | int recursion_limit) { |
| 3252 | if (recursion_limit == 0) return Handle<String>::null(); |
| 3253 | if (subject->IsConsString()) { |
| 3254 | ConsString* cons = ConsString::cast(*subject); |
| 3255 | Handle<String> first = Handle<String>(cons->first()); |
| 3256 | Handle<String> second = Handle<String>(cons->second()); |
| 3257 | Handle<String> new_first = |
| 3258 | StringReplaceOneCharWithString(isolate, |
| 3259 | first, |
| 3260 | search, |
| 3261 | replace, |
| 3262 | found, |
| 3263 | recursion_limit - 1); |
| 3264 | if (*found) return isolate->factory()->NewConsString(new_first, second); |
| 3265 | if (new_first.is_null()) return new_first; |
| 3266 | |
| 3267 | Handle<String> new_second = |
| 3268 | StringReplaceOneCharWithString(isolate, |
| 3269 | second, |
| 3270 | search, |
| 3271 | replace, |
| 3272 | found, |
| 3273 | recursion_limit - 1); |
| 3274 | if (*found) return isolate->factory()->NewConsString(first, new_second); |
| 3275 | if (new_second.is_null()) return new_second; |
| 3276 | |
| 3277 | return subject; |
| 3278 | } else { |
| 3279 | int index = StringMatch(isolate, subject, search, 0); |
| 3280 | if (index == -1) return subject; |
| 3281 | *found = true; |
| 3282 | Handle<String> first = isolate->factory()->NewSubString(subject, 0, index); |
| 3283 | Handle<String> cons1 = isolate->factory()->NewConsString(first, replace); |
| 3284 | Handle<String> second = |
| 3285 | isolate->factory()->NewSubString(subject, index + 1, subject->length()); |
| 3286 | return isolate->factory()->NewConsString(cons1, second); |
| 3287 | } |
| 3288 | } |
| 3289 | |
| 3290 | |
| 3291 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringReplaceOneCharWithString) { |
| 3292 | ASSERT(args.length() == 3); |
| 3293 | HandleScope scope(isolate); |
| 3294 | CONVERT_ARG_CHECKED(String, subject, 0); |
| 3295 | CONVERT_ARG_CHECKED(String, search, 1); |
| 3296 | CONVERT_ARG_CHECKED(String, replace, 2); |
| 3297 | |
| 3298 | // If the cons string tree is too deep, we simply abort the recursion and |
| 3299 | // retry with a flattened subject string. |
| 3300 | const int kRecursionLimit = 0x1000; |
| 3301 | bool found = false; |
| 3302 | Handle<String> result = |
| 3303 | Runtime::StringReplaceOneCharWithString(isolate, |
| 3304 | subject, |
| 3305 | search, |
| 3306 | replace, |
| 3307 | &found, |
| 3308 | kRecursionLimit); |
| 3309 | if (!result.is_null()) return *result; |
| 3310 | return *Runtime::StringReplaceOneCharWithString(isolate, |
| 3311 | FlattenGetString(subject), |
| 3312 | search, |
| 3313 | replace, |
| 3314 | &found, |
| 3315 | kRecursionLimit); |
| 3316 | } |
| 3317 | |
| 3318 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3319 | // Perform string match of pattern on subject, starting at start index. |
| 3320 | // Caller must ensure that 0 <= start_index <= sub->length(), |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3321 | // and should check that pat->length() + start_index <= sub->length(). |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3322 | int Runtime::StringMatch(Isolate* isolate, |
| 3323 | Handle<String> sub, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3324 | Handle<String> pat, |
| 3325 | int start_index) { |
| 3326 | ASSERT(0 <= start_index); |
| 3327 | ASSERT(start_index <= sub->length()); |
| 3328 | |
| 3329 | int pattern_length = pat->length(); |
| 3330 | if (pattern_length == 0) return start_index; |
| 3331 | |
| 3332 | int subject_length = sub->length(); |
| 3333 | if (start_index + pattern_length > subject_length) return -1; |
| 3334 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3335 | if (!sub->IsFlat()) FlattenString(sub); |
| 3336 | if (!pat->IsFlat()) FlattenString(pat); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3337 | |
| 3338 | AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3339 | // Extract flattened substrings of cons strings before determining asciiness. |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3340 | String::FlatContent seq_sub = sub->GetFlatContent(); |
| 3341 | String::FlatContent seq_pat = pat->GetFlatContent(); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3342 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3343 | // dispatch on type of strings |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3344 | if (seq_pat.IsAscii()) { |
| 3345 | Vector<const char> pat_vector = seq_pat.ToAsciiVector(); |
| 3346 | if (seq_sub.IsAscii()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3347 | return SearchString(isolate, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3348 | seq_sub.ToAsciiVector(), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3349 | pat_vector, |
| 3350 | start_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3351 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3352 | return SearchString(isolate, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3353 | seq_sub.ToUC16Vector(), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3354 | pat_vector, |
| 3355 | start_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3356 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3357 | Vector<const uc16> pat_vector = seq_pat.ToUC16Vector(); |
| 3358 | if (seq_sub.IsAscii()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3359 | return SearchString(isolate, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3360 | seq_sub.ToAsciiVector(), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3361 | pat_vector, |
| 3362 | start_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3363 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3364 | return SearchString(isolate, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3365 | seq_sub.ToUC16Vector(), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3366 | pat_vector, |
| 3367 | start_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3368 | } |
| 3369 | |
| 3370 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3371 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringIndexOf) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3372 | HandleScope scope(isolate); // create a new handle scope |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3373 | ASSERT(args.length() == 3); |
| 3374 | |
| 3375 | CONVERT_ARG_CHECKED(String, sub, 0); |
| 3376 | CONVERT_ARG_CHECKED(String, pat, 1); |
| 3377 | |
| 3378 | Object* index = args[2]; |
| 3379 | uint32_t start_index; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3380 | if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3381 | |
| 3382 | RUNTIME_ASSERT(start_index <= static_cast<uint32_t>(sub->length())); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3383 | int position = |
| 3384 | Runtime::StringMatch(isolate, sub, pat, start_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3385 | return Smi::FromInt(position); |
| 3386 | } |
| 3387 | |
| 3388 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3389 | template <typename schar, typename pchar> |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 3390 | static int StringMatchBackwards(Vector<const schar> subject, |
| 3391 | Vector<const pchar> pattern, |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3392 | int idx) { |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 3393 | int pattern_length = pattern.length(); |
| 3394 | ASSERT(pattern_length >= 1); |
| 3395 | ASSERT(idx + pattern_length <= subject.length()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3396 | |
| 3397 | if (sizeof(schar) == 1 && sizeof(pchar) > 1) { |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 3398 | for (int i = 0; i < pattern_length; i++) { |
| 3399 | uc16 c = pattern[i]; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3400 | if (c > String::kMaxAsciiCharCode) { |
| 3401 | return -1; |
| 3402 | } |
| 3403 | } |
| 3404 | } |
| 3405 | |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 3406 | pchar pattern_first_char = pattern[0]; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3407 | for (int i = idx; i >= 0; i--) { |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 3408 | if (subject[i] != pattern_first_char) continue; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3409 | int j = 1; |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 3410 | while (j < pattern_length) { |
| 3411 | if (pattern[j] != subject[i+j]) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3412 | break; |
| 3413 | } |
| 3414 | j++; |
| 3415 | } |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 3416 | if (j == pattern_length) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3417 | return i; |
| 3418 | } |
| 3419 | } |
| 3420 | return -1; |
| 3421 | } |
| 3422 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3423 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringLastIndexOf) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3424 | HandleScope scope(isolate); // create a new handle scope |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3425 | ASSERT(args.length() == 3); |
| 3426 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3427 | CONVERT_ARG_CHECKED(String, sub, 0); |
| 3428 | CONVERT_ARG_CHECKED(String, pat, 1); |
| 3429 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3430 | Object* index = args[2]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3431 | uint32_t start_index; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3432 | if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3433 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3434 | uint32_t pat_length = pat->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3435 | uint32_t sub_length = sub->length(); |
| 3436 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3437 | if (start_index + pat_length > sub_length) { |
| 3438 | start_index = sub_length - pat_length; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3439 | } |
| 3440 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3441 | if (pat_length == 0) { |
| 3442 | return Smi::FromInt(start_index); |
| 3443 | } |
| 3444 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3445 | if (!sub->IsFlat()) FlattenString(sub); |
| 3446 | if (!pat->IsFlat()) FlattenString(pat); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3447 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3448 | int position = -1; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3449 | AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
| 3450 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3451 | String::FlatContent sub_content = sub->GetFlatContent(); |
| 3452 | String::FlatContent pat_content = pat->GetFlatContent(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3453 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3454 | if (pat_content.IsAscii()) { |
| 3455 | Vector<const char> pat_vector = pat_content.ToAsciiVector(); |
| 3456 | if (sub_content.IsAscii()) { |
| 3457 | position = StringMatchBackwards(sub_content.ToAsciiVector(), |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3458 | pat_vector, |
| 3459 | start_index); |
| 3460 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3461 | position = StringMatchBackwards(sub_content.ToUC16Vector(), |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3462 | pat_vector, |
| 3463 | start_index); |
| 3464 | } |
| 3465 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3466 | Vector<const uc16> pat_vector = pat_content.ToUC16Vector(); |
| 3467 | if (sub_content.IsAscii()) { |
| 3468 | position = StringMatchBackwards(sub_content.ToAsciiVector(), |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3469 | pat_vector, |
| 3470 | start_index); |
| 3471 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3472 | position = StringMatchBackwards(sub_content.ToUC16Vector(), |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3473 | pat_vector, |
| 3474 | start_index); |
| 3475 | } |
| 3476 | } |
| 3477 | |
| 3478 | return Smi::FromInt(position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
| 3481 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3482 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringLocaleCompare) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3483 | NoHandleAllocation ha; |
| 3484 | ASSERT(args.length() == 2); |
| 3485 | |
| 3486 | CONVERT_CHECKED(String, str1, args[0]); |
| 3487 | CONVERT_CHECKED(String, str2, args[1]); |
| 3488 | |
| 3489 | if (str1 == str2) return Smi::FromInt(0); // Equal. |
| 3490 | int str1_length = str1->length(); |
| 3491 | int str2_length = str2->length(); |
| 3492 | |
| 3493 | // Decide trivial cases without flattening. |
| 3494 | if (str1_length == 0) { |
| 3495 | if (str2_length == 0) return Smi::FromInt(0); // Equal. |
| 3496 | return Smi::FromInt(-str2_length); |
| 3497 | } else { |
| 3498 | if (str2_length == 0) return Smi::FromInt(str1_length); |
| 3499 | } |
| 3500 | |
| 3501 | int end = str1_length < str2_length ? str1_length : str2_length; |
| 3502 | |
| 3503 | // No need to flatten if we are going to find the answer on the first |
| 3504 | // character. At this point we know there is at least one character |
| 3505 | // in each string, due to the trivial case handling above. |
| 3506 | int d = str1->Get(0) - str2->Get(0); |
| 3507 | if (d != 0) return Smi::FromInt(d); |
| 3508 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3509 | str1->TryFlatten(); |
| 3510 | str2->TryFlatten(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3511 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3512 | StringInputBuffer& buf1 = |
| 3513 | *isolate->runtime_state()->string_locale_compare_buf1(); |
| 3514 | StringInputBuffer& buf2 = |
| 3515 | *isolate->runtime_state()->string_locale_compare_buf2(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3516 | |
| 3517 | buf1.Reset(str1); |
| 3518 | buf2.Reset(str2); |
| 3519 | |
| 3520 | for (int i = 0; i < end; i++) { |
| 3521 | uint16_t char1 = buf1.GetNext(); |
| 3522 | uint16_t char2 = buf2.GetNext(); |
| 3523 | if (char1 != char2) return Smi::FromInt(char1 - char2); |
| 3524 | } |
| 3525 | |
| 3526 | return Smi::FromInt(str1_length - str2_length); |
| 3527 | } |
| 3528 | |
| 3529 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3530 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SubString) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3531 | NoHandleAllocation ha; |
| 3532 | ASSERT(args.length() == 3); |
| 3533 | |
| 3534 | CONVERT_CHECKED(String, value, args[0]); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3535 | int start, end; |
| 3536 | // We have a fast integer-only case here to avoid a conversion to double in |
| 3537 | // the common case where from and to are Smis. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3538 | if (args[1]->IsSmi() && args[2]->IsSmi()) { |
| 3539 | CONVERT_SMI_ARG_CHECKED(from_number, 1); |
| 3540 | CONVERT_SMI_ARG_CHECKED(to_number, 2); |
| 3541 | start = from_number; |
| 3542 | end = to_number; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3543 | } else { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3544 | CONVERT_DOUBLE_ARG_CHECKED(from_number, 1); |
| 3545 | CONVERT_DOUBLE_ARG_CHECKED(to_number, 2); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3546 | start = FastD2I(from_number); |
| 3547 | end = FastD2I(to_number); |
| 3548 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3549 | RUNTIME_ASSERT(end >= start); |
| 3550 | RUNTIME_ASSERT(start >= 0); |
| 3551 | RUNTIME_ASSERT(end <= value->length()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3552 | isolate->counters()->sub_string_runtime()->Increment(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3553 | return value->SubString(start, end); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
| 3556 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3557 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringMatch) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3558 | ASSERT_EQ(3, args.length()); |
| 3559 | |
| 3560 | CONVERT_ARG_CHECKED(String, subject, 0); |
| 3561 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 1); |
| 3562 | CONVERT_ARG_CHECKED(JSArray, regexp_info, 2); |
| 3563 | HandleScope handles; |
| 3564 | |
| 3565 | Handle<Object> match = RegExpImpl::Exec(regexp, subject, 0, regexp_info); |
| 3566 | |
| 3567 | if (match.is_null()) { |
| 3568 | return Failure::Exception(); |
| 3569 | } |
| 3570 | if (match->IsNull()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3571 | return isolate->heap()->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3572 | } |
| 3573 | int length = subject->length(); |
| 3574 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3575 | ZoneScope zone_space(isolate, DELETE_ON_EXIT); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3576 | ZoneList<int> offsets(8); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3577 | int start; |
| 3578 | int end; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3579 | do { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3580 | { |
| 3581 | AssertNoAllocation no_alloc; |
| 3582 | FixedArray* elements = FixedArray::cast(regexp_info->elements()); |
| 3583 | start = Smi::cast(elements->get(RegExpImpl::kFirstCapture))->value(); |
| 3584 | end = Smi::cast(elements->get(RegExpImpl::kFirstCapture + 1))->value(); |
| 3585 | } |
| 3586 | offsets.Add(start); |
| 3587 | offsets.Add(end); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3588 | if (start == end) if (++end > length) break; |
| 3589 | match = RegExpImpl::Exec(regexp, subject, end, regexp_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3590 | if (match.is_null()) { |
| 3591 | return Failure::Exception(); |
| 3592 | } |
| 3593 | } while (!match->IsNull()); |
| 3594 | int matches = offsets.length() / 2; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3595 | Handle<FixedArray> elements = isolate->factory()->NewFixedArray(matches); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3596 | Handle<String> substring = isolate->factory()-> |
| 3597 | NewSubString(subject, offsets.at(0), offsets.at(1)); |
| 3598 | elements->set(0, *substring); |
| 3599 | for (int i = 1; i < matches ; i++) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3600 | int from = offsets.at(i * 2); |
| 3601 | int to = offsets.at(i * 2 + 1); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3602 | Handle<String> substring = isolate->factory()-> |
| 3603 | NewProperSubString(subject, from, to); |
| 3604 | elements->set(i, *substring); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3605 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3606 | Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(elements); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3607 | result->set_length(Smi::FromInt(matches)); |
| 3608 | return *result; |
| 3609 | } |
| 3610 | |
| 3611 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3612 | // Two smis before and after the match, for very long strings. |
| 3613 | const int kMaxBuilderEntriesPerRegExpMatch = 5; |
| 3614 | |
| 3615 | |
| 3616 | static void SetLastMatchInfoNoCaptures(Handle<String> subject, |
| 3617 | Handle<JSArray> last_match_info, |
| 3618 | int match_start, |
| 3619 | int match_end) { |
| 3620 | // Fill last_match_info with a single capture. |
| 3621 | last_match_info->EnsureSize(2 + RegExpImpl::kLastMatchOverhead); |
| 3622 | AssertNoAllocation no_gc; |
| 3623 | FixedArray* elements = FixedArray::cast(last_match_info->elements()); |
| 3624 | RegExpImpl::SetLastCaptureCount(elements, 2); |
| 3625 | RegExpImpl::SetLastInput(elements, *subject); |
| 3626 | RegExpImpl::SetLastSubject(elements, *subject); |
| 3627 | RegExpImpl::SetCapture(elements, 0, match_start); |
| 3628 | RegExpImpl::SetCapture(elements, 1, match_end); |
| 3629 | } |
| 3630 | |
| 3631 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3632 | template <typename SubjectChar, typename PatternChar> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3633 | static bool SearchStringMultiple(Isolate* isolate, |
| 3634 | Vector<const SubjectChar> subject, |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3635 | Vector<const PatternChar> pattern, |
| 3636 | String* pattern_string, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3637 | FixedArrayBuilder* builder, |
| 3638 | int* match_pos) { |
| 3639 | int pos = *match_pos; |
| 3640 | int subject_length = subject.length(); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3641 | int pattern_length = pattern.length(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3642 | int max_search_start = subject_length - pattern_length; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3643 | StringSearch<PatternChar, SubjectChar> search(isolate, pattern); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3644 | while (pos <= max_search_start) { |
| 3645 | if (!builder->HasCapacity(kMaxBuilderEntriesPerRegExpMatch)) { |
| 3646 | *match_pos = pos; |
| 3647 | return false; |
| 3648 | } |
| 3649 | // Position of end of previous match. |
| 3650 | int match_end = pos + pattern_length; |
| 3651 | int new_pos = search.Search(subject, match_end); |
| 3652 | if (new_pos >= 0) { |
| 3653 | // A match. |
| 3654 | if (new_pos > match_end) { |
| 3655 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3656 | match_end, |
| 3657 | new_pos); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3658 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3659 | pos = new_pos; |
| 3660 | builder->Add(pattern_string); |
| 3661 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3662 | break; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3663 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3664 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3665 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3666 | if (pos < max_search_start) { |
| 3667 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3668 | pos + pattern_length, |
| 3669 | subject_length); |
| 3670 | } |
| 3671 | *match_pos = pos; |
| 3672 | return true; |
| 3673 | } |
| 3674 | |
| 3675 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3676 | static bool SearchStringMultiple(Isolate* isolate, |
| 3677 | Handle<String> subject, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3678 | Handle<String> pattern, |
| 3679 | Handle<JSArray> last_match_info, |
| 3680 | FixedArrayBuilder* builder) { |
| 3681 | ASSERT(subject->IsFlat()); |
| 3682 | ASSERT(pattern->IsFlat()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3683 | |
| 3684 | // Treating as if a previous match was before first character. |
| 3685 | int match_pos = -pattern->length(); |
| 3686 | |
| 3687 | for (;;) { // Break when search complete. |
| 3688 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3689 | AssertNoAllocation no_gc; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3690 | String::FlatContent subject_content = subject->GetFlatContent(); |
| 3691 | String::FlatContent pattern_content = pattern->GetFlatContent(); |
| 3692 | if (subject_content.IsAscii()) { |
| 3693 | Vector<const char> subject_vector = subject_content.ToAsciiVector(); |
| 3694 | if (pattern_content.IsAscii()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3695 | if (SearchStringMultiple(isolate, |
| 3696 | subject_vector, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3697 | pattern_content.ToAsciiVector(), |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3698 | *pattern, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3699 | builder, |
| 3700 | &match_pos)) break; |
| 3701 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3702 | if (SearchStringMultiple(isolate, |
| 3703 | subject_vector, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3704 | pattern_content.ToUC16Vector(), |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3705 | *pattern, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3706 | builder, |
| 3707 | &match_pos)) break; |
| 3708 | } |
| 3709 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3710 | Vector<const uc16> subject_vector = subject_content.ToUC16Vector(); |
| 3711 | if (pattern_content.IsAscii()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3712 | if (SearchStringMultiple(isolate, |
| 3713 | subject_vector, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3714 | pattern_content.ToAsciiVector(), |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3715 | *pattern, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3716 | builder, |
| 3717 | &match_pos)) break; |
| 3718 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3719 | if (SearchStringMultiple(isolate, |
| 3720 | subject_vector, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3721 | pattern_content.ToUC16Vector(), |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3722 | *pattern, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3723 | builder, |
| 3724 | &match_pos)) break; |
| 3725 | } |
| 3726 | } |
| 3727 | } |
| 3728 | |
| 3729 | if (match_pos >= 0) { |
| 3730 | SetLastMatchInfoNoCaptures(subject, |
| 3731 | last_match_info, |
| 3732 | match_pos, |
| 3733 | match_pos + pattern->length()); |
| 3734 | return true; |
| 3735 | } |
| 3736 | return false; // No matches at all. |
| 3737 | } |
| 3738 | |
| 3739 | |
| 3740 | static RegExpImpl::IrregexpResult SearchRegExpNoCaptureMultiple( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3741 | Isolate* isolate, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3742 | Handle<String> subject, |
| 3743 | Handle<JSRegExp> regexp, |
| 3744 | Handle<JSArray> last_match_array, |
| 3745 | FixedArrayBuilder* builder) { |
| 3746 | ASSERT(subject->IsFlat()); |
| 3747 | int match_start = -1; |
| 3748 | int match_end = 0; |
| 3749 | int pos = 0; |
| 3750 | int required_registers = RegExpImpl::IrregexpPrepare(regexp, subject); |
| 3751 | if (required_registers < 0) return RegExpImpl::RE_EXCEPTION; |
| 3752 | |
| 3753 | OffsetsVector registers(required_registers); |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 3754 | Vector<int32_t> register_vector(registers.vector(), registers.length()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3755 | int subject_length = subject->length(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3756 | bool first = true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3757 | |
| 3758 | for (;;) { // Break on failure, return on exception. |
| 3759 | RegExpImpl::IrregexpResult result = |
| 3760 | RegExpImpl::IrregexpExecOnce(regexp, |
| 3761 | subject, |
| 3762 | pos, |
| 3763 | register_vector); |
| 3764 | if (result == RegExpImpl::RE_SUCCESS) { |
| 3765 | match_start = register_vector[0]; |
| 3766 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3767 | if (match_end < match_start) { |
| 3768 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3769 | match_end, |
| 3770 | match_start); |
| 3771 | } |
| 3772 | match_end = register_vector[1]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3773 | HandleScope loop_scope(isolate); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3774 | if (!first) { |
| 3775 | builder->Add(*isolate->factory()->NewProperSubString(subject, |
| 3776 | match_start, |
| 3777 | match_end)); |
| 3778 | } else { |
| 3779 | builder->Add(*isolate->factory()->NewSubString(subject, |
| 3780 | match_start, |
| 3781 | match_end)); |
| 3782 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3783 | if (match_start != match_end) { |
| 3784 | pos = match_end; |
| 3785 | } else { |
| 3786 | pos = match_end + 1; |
| 3787 | if (pos > subject_length) break; |
| 3788 | } |
| 3789 | } else if (result == RegExpImpl::RE_FAILURE) { |
| 3790 | break; |
| 3791 | } else { |
| 3792 | ASSERT_EQ(result, RegExpImpl::RE_EXCEPTION); |
| 3793 | return result; |
| 3794 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3795 | first = false; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3796 | } |
| 3797 | |
| 3798 | if (match_start >= 0) { |
| 3799 | if (match_end < subject_length) { |
| 3800 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3801 | match_end, |
| 3802 | subject_length); |
| 3803 | } |
| 3804 | SetLastMatchInfoNoCaptures(subject, |
| 3805 | last_match_array, |
| 3806 | match_start, |
| 3807 | match_end); |
| 3808 | return RegExpImpl::RE_SUCCESS; |
| 3809 | } else { |
| 3810 | return RegExpImpl::RE_FAILURE; // No matches at all. |
| 3811 | } |
| 3812 | } |
| 3813 | |
| 3814 | |
| 3815 | static RegExpImpl::IrregexpResult SearchRegExpMultiple( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3816 | Isolate* isolate, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3817 | Handle<String> subject, |
| 3818 | Handle<JSRegExp> regexp, |
| 3819 | Handle<JSArray> last_match_array, |
| 3820 | FixedArrayBuilder* builder) { |
| 3821 | |
| 3822 | ASSERT(subject->IsFlat()); |
| 3823 | int required_registers = RegExpImpl::IrregexpPrepare(regexp, subject); |
| 3824 | if (required_registers < 0) return RegExpImpl::RE_EXCEPTION; |
| 3825 | |
| 3826 | OffsetsVector registers(required_registers); |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 3827 | Vector<int32_t> register_vector(registers.vector(), registers.length()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3828 | |
| 3829 | RegExpImpl::IrregexpResult result = |
| 3830 | RegExpImpl::IrregexpExecOnce(regexp, |
| 3831 | subject, |
| 3832 | 0, |
| 3833 | register_vector); |
| 3834 | |
| 3835 | int capture_count = regexp->CaptureCount(); |
| 3836 | int subject_length = subject->length(); |
| 3837 | |
| 3838 | // Position to search from. |
| 3839 | int pos = 0; |
| 3840 | // End of previous match. Differs from pos if match was empty. |
| 3841 | int match_end = 0; |
| 3842 | if (result == RegExpImpl::RE_SUCCESS) { |
| 3843 | // Need to keep a copy of the previous match for creating last_match_info |
| 3844 | // at the end, so we have two vectors that we swap between. |
| 3845 | OffsetsVector registers2(required_registers); |
| 3846 | Vector<int> prev_register_vector(registers2.vector(), registers2.length()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3847 | bool first = true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3848 | do { |
| 3849 | int match_start = register_vector[0]; |
| 3850 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3851 | if (match_end < match_start) { |
| 3852 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3853 | match_end, |
| 3854 | match_start); |
| 3855 | } |
| 3856 | match_end = register_vector[1]; |
| 3857 | |
| 3858 | { |
| 3859 | // Avoid accumulating new handles inside loop. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3860 | HandleScope temp_scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3861 | // Arguments array to replace function is match, captures, index and |
| 3862 | // subject, i.e., 3 + capture count in total. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3863 | Handle<FixedArray> elements = |
| 3864 | isolate->factory()->NewFixedArray(3 + capture_count); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3865 | Handle<String> match; |
| 3866 | if (!first) { |
| 3867 | match = isolate->factory()->NewProperSubString(subject, |
| 3868 | match_start, |
| 3869 | match_end); |
| 3870 | } else { |
| 3871 | match = isolate->factory()->NewSubString(subject, |
| 3872 | match_start, |
| 3873 | match_end); |
| 3874 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3875 | elements->set(0, *match); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3876 | for (int i = 1; i <= capture_count; i++) { |
| 3877 | int start = register_vector[i * 2]; |
| 3878 | if (start >= 0) { |
| 3879 | int end = register_vector[i * 2 + 1]; |
| 3880 | ASSERT(start <= end); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3881 | Handle<String> substring; |
| 3882 | if (!first) { |
| 3883 | substring = isolate->factory()->NewProperSubString(subject, |
| 3884 | start, |
| 3885 | end); |
| 3886 | } else { |
| 3887 | substring = isolate->factory()->NewSubString(subject, start, end); |
| 3888 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3889 | elements->set(i, *substring); |
| 3890 | } else { |
| 3891 | ASSERT(register_vector[i * 2 + 1] < 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3892 | elements->set(i, isolate->heap()->undefined_value()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3893 | } |
| 3894 | } |
| 3895 | elements->set(capture_count + 1, Smi::FromInt(match_start)); |
| 3896 | elements->set(capture_count + 2, *subject); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3897 | builder->Add(*isolate->factory()->NewJSArrayWithElements(elements)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3898 | } |
| 3899 | // Swap register vectors, so the last successful match is in |
| 3900 | // prev_register_vector. |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 3901 | Vector<int32_t> tmp = prev_register_vector; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3902 | prev_register_vector = register_vector; |
| 3903 | register_vector = tmp; |
| 3904 | |
| 3905 | if (match_end > match_start) { |
| 3906 | pos = match_end; |
| 3907 | } else { |
| 3908 | pos = match_end + 1; |
| 3909 | if (pos > subject_length) { |
| 3910 | break; |
| 3911 | } |
| 3912 | } |
| 3913 | |
| 3914 | result = RegExpImpl::IrregexpExecOnce(regexp, |
| 3915 | subject, |
| 3916 | pos, |
| 3917 | register_vector); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3918 | first = false; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3919 | } while (result == RegExpImpl::RE_SUCCESS); |
| 3920 | |
| 3921 | if (result != RegExpImpl::RE_EXCEPTION) { |
| 3922 | // Finished matching, with at least one match. |
| 3923 | if (match_end < subject_length) { |
| 3924 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3925 | match_end, |
| 3926 | subject_length); |
| 3927 | } |
| 3928 | |
| 3929 | int last_match_capture_count = (capture_count + 1) * 2; |
| 3930 | int last_match_array_size = |
| 3931 | last_match_capture_count + RegExpImpl::kLastMatchOverhead; |
| 3932 | last_match_array->EnsureSize(last_match_array_size); |
| 3933 | AssertNoAllocation no_gc; |
| 3934 | FixedArray* elements = FixedArray::cast(last_match_array->elements()); |
| 3935 | RegExpImpl::SetLastCaptureCount(elements, last_match_capture_count); |
| 3936 | RegExpImpl::SetLastSubject(elements, *subject); |
| 3937 | RegExpImpl::SetLastInput(elements, *subject); |
| 3938 | for (int i = 0; i < last_match_capture_count; i++) { |
| 3939 | RegExpImpl::SetCapture(elements, i, prev_register_vector[i]); |
| 3940 | } |
| 3941 | return RegExpImpl::RE_SUCCESS; |
| 3942 | } |
| 3943 | } |
| 3944 | // No matches at all, return failure or exception result directly. |
| 3945 | return result; |
| 3946 | } |
| 3947 | |
| 3948 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3949 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpExecMultiple) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3950 | ASSERT(args.length() == 4); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3951 | HandleScope handles(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3952 | |
| 3953 | CONVERT_ARG_CHECKED(String, subject, 1); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3954 | if (!subject->IsFlat()) FlattenString(subject); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3955 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 0); |
| 3956 | CONVERT_ARG_CHECKED(JSArray, last_match_info, 2); |
| 3957 | CONVERT_ARG_CHECKED(JSArray, result_array, 3); |
| 3958 | |
| 3959 | ASSERT(last_match_info->HasFastElements()); |
| 3960 | ASSERT(regexp->GetFlags().is_global()); |
| 3961 | Handle<FixedArray> result_elements; |
| 3962 | if (result_array->HasFastElements()) { |
| 3963 | result_elements = |
| 3964 | Handle<FixedArray>(FixedArray::cast(result_array->elements())); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3965 | } |
| 3966 | if (result_elements.is_null() || result_elements->length() < 16) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3967 | result_elements = isolate->factory()->NewFixedArrayWithHoles(16); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3968 | } |
| 3969 | FixedArrayBuilder builder(result_elements); |
| 3970 | |
| 3971 | if (regexp->TypeTag() == JSRegExp::ATOM) { |
| 3972 | Handle<String> pattern( |
| 3973 | String::cast(regexp->DataAt(JSRegExp::kAtomPatternIndex))); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3974 | ASSERT(pattern->IsFlat()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3975 | if (SearchStringMultiple(isolate, subject, pattern, |
| 3976 | last_match_info, &builder)) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3977 | return *builder.ToJSArray(result_array); |
| 3978 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3979 | return isolate->heap()->null_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3980 | } |
| 3981 | |
| 3982 | ASSERT_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP); |
| 3983 | |
| 3984 | RegExpImpl::IrregexpResult result; |
| 3985 | if (regexp->CaptureCount() == 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3986 | result = SearchRegExpNoCaptureMultiple(isolate, |
| 3987 | subject, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3988 | regexp, |
| 3989 | last_match_info, |
| 3990 | &builder); |
| 3991 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3992 | result = SearchRegExpMultiple(isolate, |
| 3993 | subject, |
| 3994 | regexp, |
| 3995 | last_match_info, |
| 3996 | &builder); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3997 | } |
| 3998 | if (result == RegExpImpl::RE_SUCCESS) return *builder.ToJSArray(result_array); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3999 | if (result == RegExpImpl::RE_FAILURE) return isolate->heap()->null_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4000 | ASSERT_EQ(result, RegExpImpl::RE_EXCEPTION); |
| 4001 | return Failure::Exception(); |
| 4002 | } |
| 4003 | |
| 4004 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4005 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToRadixString) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4006 | NoHandleAllocation ha; |
| 4007 | ASSERT(args.length() == 2); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4008 | CONVERT_SMI_ARG_CHECKED(radix, 1); |
| 4009 | RUNTIME_ASSERT(2 <= radix && radix <= 36); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4010 | |
| 4011 | // Fast case where the result is a one character string. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4012 | if (args[0]->IsSmi()) { |
| 4013 | int value = args.smi_at(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4014 | if (value >= 0 && value < radix) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4015 | // Character array used for conversion. |
| 4016 | static const char kCharTable[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4017 | return isolate->heap()-> |
| 4018 | LookupSingleCharacterStringFromCode(kCharTable[value]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4019 | } |
| 4020 | } |
| 4021 | |
| 4022 | // Slow case. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4023 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4024 | if (isnan(value)) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4025 | return *isolate->factory()->nan_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4026 | } |
| 4027 | if (isinf(value)) { |
| 4028 | if (value < 0) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4029 | return *isolate->factory()->minus_infinity_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4030 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4031 | return *isolate->factory()->infinity_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4032 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4033 | char* str = DoubleToRadixCString(value, radix); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4034 | MaybeObject* result = |
| 4035 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4036 | DeleteArray(str); |
| 4037 | return result; |
| 4038 | } |
| 4039 | |
| 4040 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4041 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToFixed) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4042 | NoHandleAllocation ha; |
| 4043 | ASSERT(args.length() == 2); |
| 4044 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4045 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4046 | if (isnan(value)) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4047 | return *isolate->factory()->nan_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4048 | } |
| 4049 | if (isinf(value)) { |
| 4050 | if (value < 0) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4051 | return *isolate->factory()->minus_infinity_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4052 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4053 | return *isolate->factory()->infinity_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4054 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4055 | CONVERT_DOUBLE_ARG_CHECKED(f_number, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4056 | int f = FastD2I(f_number); |
| 4057 | RUNTIME_ASSERT(f >= 0); |
| 4058 | char* str = DoubleToFixedCString(value, f); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4059 | MaybeObject* res = |
| 4060 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4061 | DeleteArray(str); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4062 | return res; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4063 | } |
| 4064 | |
| 4065 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4066 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToExponential) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4067 | NoHandleAllocation ha; |
| 4068 | ASSERT(args.length() == 2); |
| 4069 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4070 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4071 | if (isnan(value)) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4072 | return *isolate->factory()->nan_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4073 | } |
| 4074 | if (isinf(value)) { |
| 4075 | if (value < 0) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4076 | return *isolate->factory()->minus_infinity_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4077 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4078 | return *isolate->factory()->infinity_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4079 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4080 | CONVERT_DOUBLE_ARG_CHECKED(f_number, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4081 | int f = FastD2I(f_number); |
| 4082 | RUNTIME_ASSERT(f >= -1 && f <= 20); |
| 4083 | char* str = DoubleToExponentialCString(value, f); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4084 | MaybeObject* res = |
| 4085 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4086 | DeleteArray(str); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4087 | return res; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4088 | } |
| 4089 | |
| 4090 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4091 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToPrecision) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4092 | NoHandleAllocation ha; |
| 4093 | ASSERT(args.length() == 2); |
| 4094 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4095 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4096 | if (isnan(value)) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4097 | return *isolate->factory()->nan_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4098 | } |
| 4099 | if (isinf(value)) { |
| 4100 | if (value < 0) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4101 | return *isolate->factory()->minus_infinity_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4102 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4103 | return *isolate->factory()->infinity_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4104 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4105 | CONVERT_DOUBLE_ARG_CHECKED(f_number, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4106 | int f = FastD2I(f_number); |
| 4107 | RUNTIME_ASSERT(f >= 1 && f <= 21); |
| 4108 | char* str = DoubleToPrecisionCString(value, f); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4109 | MaybeObject* res = |
| 4110 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4111 | DeleteArray(str); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4112 | return res; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4113 | } |
| 4114 | |
| 4115 | |
| 4116 | // Returns a single character string where first character equals |
| 4117 | // string->Get(index). |
| 4118 | static Handle<Object> GetCharAt(Handle<String> string, uint32_t index) { |
| 4119 | if (index < static_cast<uint32_t>(string->length())) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4120 | string->TryFlatten(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4121 | return LookupSingleCharacterStringFromCode( |
| 4122 | string->Get(index)); |
| 4123 | } |
| 4124 | return Execution::CharAt(string, index); |
| 4125 | } |
| 4126 | |
| 4127 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4128 | MaybeObject* Runtime::GetElementOrCharAt(Isolate* isolate, |
| 4129 | Handle<Object> object, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4130 | uint32_t index) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4131 | // Handle [] indexing on Strings |
| 4132 | if (object->IsString()) { |
| 4133 | Handle<Object> result = GetCharAt(Handle<String>::cast(object), index); |
| 4134 | if (!result->IsUndefined()) return *result; |
| 4135 | } |
| 4136 | |
| 4137 | // Handle [] indexing on String objects |
| 4138 | if (object->IsStringObjectWithCharacterAt(index)) { |
| 4139 | Handle<JSValue> js_value = Handle<JSValue>::cast(object); |
| 4140 | Handle<Object> result = |
| 4141 | GetCharAt(Handle<String>(String::cast(js_value->value())), index); |
| 4142 | if (!result->IsUndefined()) return *result; |
| 4143 | } |
| 4144 | |
| 4145 | if (object->IsString() || object->IsNumber() || object->IsBoolean()) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4146 | return object->GetPrototype()->GetElement(index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
| 4149 | return object->GetElement(index); |
| 4150 | } |
| 4151 | |
| 4152 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4153 | MaybeObject* Runtime::GetObjectProperty(Isolate* isolate, |
| 4154 | Handle<Object> object, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4155 | Handle<Object> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4156 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4157 | |
| 4158 | if (object->IsUndefined() || object->IsNull()) { |
| 4159 | Handle<Object> args[2] = { key, object }; |
| 4160 | Handle<Object> error = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4161 | isolate->factory()->NewTypeError("non_object_property_load", |
| 4162 | HandleVector(args, 2)); |
| 4163 | return isolate->Throw(*error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4164 | } |
| 4165 | |
| 4166 | // Check if the given key is an array index. |
| 4167 | uint32_t index; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4168 | if (key->ToArrayIndex(&index)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4169 | return GetElementOrCharAt(isolate, object, index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | // Convert the key to a string - possibly by calling back into JavaScript. |
| 4173 | Handle<String> name; |
| 4174 | if (key->IsString()) { |
| 4175 | name = Handle<String>::cast(key); |
| 4176 | } else { |
| 4177 | bool has_pending_exception = false; |
| 4178 | Handle<Object> converted = |
| 4179 | Execution::ToString(key, &has_pending_exception); |
| 4180 | if (has_pending_exception) return Failure::Exception(); |
| 4181 | name = Handle<String>::cast(converted); |
| 4182 | } |
| 4183 | |
| 4184 | // Check if the name is trivially convertible to an index and get |
| 4185 | // the element if so. |
| 4186 | if (name->AsArrayIndex(&index)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4187 | return GetElementOrCharAt(isolate, object, index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4188 | } else { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4189 | return object->GetProperty(*name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4190 | } |
| 4191 | } |
| 4192 | |
| 4193 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4194 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetProperty) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4195 | NoHandleAllocation ha; |
| 4196 | ASSERT(args.length() == 2); |
| 4197 | |
| 4198 | Handle<Object> object = args.at<Object>(0); |
| 4199 | Handle<Object> key = args.at<Object>(1); |
| 4200 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4201 | return Runtime::GetObjectProperty(isolate, object, key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4202 | } |
| 4203 | |
| 4204 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4205 | MaybeObject* TransitionElements(Handle<Object> object, |
| 4206 | ElementsKind to_kind, |
| 4207 | Isolate* isolate) { |
| 4208 | HandleScope scope(isolate); |
| 4209 | if (!object->IsJSObject()) return isolate->ThrowIllegalOperation(); |
| 4210 | ElementsKind from_kind = |
| 4211 | Handle<JSObject>::cast(object)->map()->elements_kind(); |
| 4212 | if (Map::IsValidElementsTransition(from_kind, to_kind)) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4213 | Handle<Object> result = JSObject::TransitionElementsKind( |
| 4214 | Handle<JSObject>::cast(object), to_kind); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4215 | if (result.is_null()) return isolate->ThrowIllegalOperation(); |
| 4216 | return *result; |
| 4217 | } |
| 4218 | return isolate->ThrowIllegalOperation(); |
| 4219 | } |
| 4220 | |
| 4221 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4222 | // KeyedStringGetProperty is called from KeyedLoadIC::GenerateGeneric. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4223 | RUNTIME_FUNCTION(MaybeObject*, Runtime_KeyedGetProperty) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4224 | NoHandleAllocation ha; |
| 4225 | ASSERT(args.length() == 2); |
| 4226 | |
| 4227 | // Fast cases for getting named properties of the receiver JSObject |
| 4228 | // itself. |
| 4229 | // |
| 4230 | // The global proxy objects has to be excluded since LocalLookup on |
| 4231 | // the global proxy object can return a valid result even though the |
| 4232 | // global proxy object never has properties. This is the case |
| 4233 | // because the global proxy object forwards everything to its hidden |
| 4234 | // prototype including local lookups. |
| 4235 | // |
| 4236 | // Additionally, we need to make sure that we do not cache results |
| 4237 | // for objects that require access checks. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4238 | if (args[0]->IsJSObject()) { |
| 4239 | if (!args[0]->IsJSGlobalProxy() && |
| 4240 | !args[0]->IsAccessCheckNeeded() && |
| 4241 | args[1]->IsString()) { |
| 4242 | JSObject* receiver = JSObject::cast(args[0]); |
| 4243 | String* key = String::cast(args[1]); |
| 4244 | if (receiver->HasFastProperties()) { |
| 4245 | // Attempt to use lookup cache. |
| 4246 | Map* receiver_map = receiver->map(); |
| 4247 | KeyedLookupCache* keyed_lookup_cache = isolate->keyed_lookup_cache(); |
| 4248 | int offset = keyed_lookup_cache->Lookup(receiver_map, key); |
| 4249 | if (offset != -1) { |
| 4250 | Object* value = receiver->FastPropertyAt(offset); |
| 4251 | return value->IsTheHole() |
| 4252 | ? isolate->heap()->undefined_value() |
| 4253 | : value; |
| 4254 | } |
| 4255 | // Lookup cache miss. Perform lookup and update the cache if |
| 4256 | // appropriate. |
| 4257 | LookupResult result(isolate); |
| 4258 | receiver->LocalLookup(key, &result); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4259 | if (result.IsFound() && result.type() == FIELD) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4260 | int offset = result.GetFieldIndex(); |
| 4261 | keyed_lookup_cache->Update(receiver_map, key, offset); |
| 4262 | return receiver->FastPropertyAt(offset); |
| 4263 | } |
| 4264 | } else { |
| 4265 | // Attempt dictionary lookup. |
| 4266 | StringDictionary* dictionary = receiver->property_dictionary(); |
| 4267 | int entry = dictionary->FindEntry(key); |
| 4268 | if ((entry != StringDictionary::kNotFound) && |
| 4269 | (dictionary->DetailsAt(entry).type() == NORMAL)) { |
| 4270 | Object* value = dictionary->ValueAt(entry); |
| 4271 | if (!receiver->IsGlobalObject()) return value; |
| 4272 | value = JSGlobalPropertyCell::cast(value)->value(); |
| 4273 | if (!value->IsTheHole()) return value; |
| 4274 | // If value is the hole do the general lookup. |
| 4275 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4276 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4277 | } else if (FLAG_smi_only_arrays && args.at<Object>(1)->IsSmi()) { |
| 4278 | // JSObject without a string key. If the key is a Smi, check for a |
| 4279 | // definite out-of-bounds access to elements, which is a strong indicator |
| 4280 | // that subsequent accesses will also call the runtime. Proactively |
| 4281 | // transition elements to FAST_ELEMENTS to avoid excessive boxing of |
| 4282 | // doubles for those future calls in the case that the elements would |
| 4283 | // become FAST_DOUBLE_ELEMENTS. |
| 4284 | Handle<JSObject> js_object(args.at<JSObject>(0)); |
| 4285 | ElementsKind elements_kind = js_object->GetElementsKind(); |
| 4286 | if (elements_kind == FAST_SMI_ONLY_ELEMENTS || |
| 4287 | elements_kind == FAST_DOUBLE_ELEMENTS) { |
| 4288 | FixedArrayBase* elements = js_object->elements(); |
| 4289 | if (args.at<Smi>(1)->value() >= elements->length()) { |
| 4290 | MaybeObject* maybe_object = TransitionElements(js_object, |
| 4291 | FAST_ELEMENTS, |
| 4292 | isolate); |
| 4293 | if (maybe_object->IsFailure()) return maybe_object; |
| 4294 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4295 | } |
| 4296 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4297 | } else if (args[0]->IsString() && args[1]->IsSmi()) { |
| 4298 | // Fast case for string indexing using [] with a smi index. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4299 | HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4300 | Handle<String> str = args.at<String>(0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4301 | int index = args.smi_at(1); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4302 | if (index >= 0 && index < str->length()) { |
| 4303 | Handle<Object> result = GetCharAt(str, index); |
| 4304 | return *result; |
| 4305 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
| 4308 | // Fall back to GetObjectProperty. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4309 | return Runtime::GetObjectProperty(isolate, |
| 4310 | args.at<Object>(0), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4311 | args.at<Object>(1)); |
| 4312 | } |
| 4313 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4314 | // Implements part of 8.12.9 DefineOwnProperty. |
| 4315 | // There are 3 cases that lead here: |
| 4316 | // Step 4b - define a new accessor property. |
| 4317 | // Steps 9c & 12 - replace an existing data property with an accessor property. |
| 4318 | // Step 12 - update an existing accessor property with an accessor or generic |
| 4319 | // descriptor. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4320 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DefineOrRedefineAccessorProperty) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4321 | ASSERT(args.length() == 5); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4322 | HandleScope scope(isolate); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4323 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4324 | CONVERT_CHECKED(String, name, args[1]); |
| 4325 | CONVERT_CHECKED(Smi, flag_setter, args[2]); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4326 | Object* fun = args[3]; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4327 | CONVERT_CHECKED(Smi, flag_attr, args[4]); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4328 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4329 | int unchecked = flag_attr->value(); |
| 4330 | RUNTIME_ASSERT((unchecked & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4331 | PropertyAttributes attr = static_cast<PropertyAttributes>(unchecked); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4332 | |
| 4333 | RUNTIME_ASSERT(!obj->IsNull()); |
| 4334 | RUNTIME_ASSERT(fun->IsSpecFunction() || fun->IsUndefined()); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4335 | return obj->DefineAccessor(name, flag_setter->value() == 0, fun, attr); |
| 4336 | } |
| 4337 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4338 | // Implements part of 8.12.9 DefineOwnProperty. |
| 4339 | // There are 3 cases that lead here: |
| 4340 | // Step 4a - define a new data property. |
| 4341 | // Steps 9b & 12 - replace an existing accessor property with a data property. |
| 4342 | // Step 12 - update an existing data property with a data or generic |
| 4343 | // descriptor. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4344 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DefineOrRedefineDataProperty) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4345 | ASSERT(args.length() == 4); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4346 | HandleScope scope(isolate); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4347 | CONVERT_ARG_CHECKED(JSObject, js_object, 0); |
| 4348 | CONVERT_ARG_CHECKED(String, name, 1); |
| 4349 | Handle<Object> obj_value = args.at<Object>(2); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4350 | CONVERT_CHECKED(Smi, flag, args[3]); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4351 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4352 | int unchecked = flag->value(); |
| 4353 | RUNTIME_ASSERT((unchecked & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4354 | PropertyAttributes attr = static_cast<PropertyAttributes>(unchecked); |
| 4355 | |
| 4356 | // Check if this is an element. |
| 4357 | uint32_t index; |
| 4358 | bool is_element = name->AsArrayIndex(&index); |
| 4359 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4360 | // Special case for elements if any of the flags might be involved. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4361 | // If elements are in fast case we always implicitly assume that: |
| 4362 | // DONT_DELETE: false, DONT_ENUM: false, READ_ONLY: false. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4363 | if (is_element && (attr != NONE || |
| 4364 | js_object->HasLocalElement(index) == JSObject::DICTIONARY_ELEMENT)) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4365 | // Normalize the elements to enable attributes on the property. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4366 | if (js_object->IsJSGlobalProxy()) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4367 | // We do not need to do access checks here since these has already |
| 4368 | // been performed by the call to GetOwnProperty. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4369 | Handle<Object> proto(js_object->GetPrototype()); |
| 4370 | // If proxy is detached, ignore the assignment. Alternatively, |
| 4371 | // we could throw an exception. |
| 4372 | if (proto->IsNull()) return *obj_value; |
| 4373 | js_object = Handle<JSObject>::cast(proto); |
| 4374 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4375 | |
| 4376 | // Don't allow element properties to be redefined on objects with external |
| 4377 | // array elements. |
| 4378 | if (js_object->HasExternalArrayElements()) { |
| 4379 | Handle<Object> args[2] = { js_object, name }; |
| 4380 | Handle<Object> error = |
| 4381 | isolate->factory()->NewTypeError("redef_external_array_element", |
| 4382 | HandleVector(args, 2)); |
| 4383 | return isolate->Throw(*error); |
| 4384 | } |
| 4385 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4386 | Handle<SeededNumberDictionary> dictionary = |
| 4387 | JSObject::NormalizeElements(js_object); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4388 | // Make sure that we never go back to fast case. |
| 4389 | dictionary->set_requires_slow_elements(); |
| 4390 | PropertyDetails details = PropertyDetails(attr, NORMAL); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4391 | Handle<SeededNumberDictionary> extended_dictionary = |
| 4392 | SeededNumberDictionary::Set(dictionary, index, obj_value, details); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4393 | if (*extended_dictionary != *dictionary) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 4394 | if (js_object->GetElementsKind() == NON_STRICT_ARGUMENTS_ELEMENTS) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4395 | FixedArray::cast(js_object->elements())->set(1, *extended_dictionary); |
| 4396 | } else { |
| 4397 | js_object->set_elements(*extended_dictionary); |
| 4398 | } |
| 4399 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4400 | return *obj_value; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4401 | } |
| 4402 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4403 | LookupResult result(isolate); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4404 | js_object->LocalLookupRealNamedProperty(*name, &result); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4405 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4406 | // Special case for callback properties. |
| 4407 | if (result.IsFound() && result.type() == CALLBACKS) { |
| 4408 | Object* callback = result.GetCallbackObject(); |
| 4409 | // To be compatible with Safari we do not change the value on API objects |
| 4410 | // in Object.defineProperty(). Firefox disagrees here, and actually changes |
| 4411 | // the value. |
| 4412 | if (callback->IsAccessorInfo()) { |
| 4413 | return isolate->heap()->undefined_value(); |
| 4414 | } |
| 4415 | // Avoid redefining foreign callback as data property, just use the stored |
| 4416 | // setter to update the value instead. |
| 4417 | // TODO(mstarzinger): So far this only works if property attributes don't |
| 4418 | // change, this should be fixed once we cleanup the underlying code. |
| 4419 | if (callback->IsForeign() && result.GetAttributes() == attr) { |
| 4420 | return js_object->SetPropertyWithCallback(callback, |
| 4421 | *name, |
| 4422 | *obj_value, |
| 4423 | result.holder(), |
| 4424 | kStrictMode); |
| 4425 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4426 | } |
| 4427 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4428 | // Take special care when attributes are different and there is already |
| 4429 | // a property. For simplicity we normalize the property which enables us |
| 4430 | // to not worry about changing the instance_descriptor and creating a new |
| 4431 | // map. The current version of SetObjectProperty does not handle attributes |
| 4432 | // correctly in the case where a property is a field and is reset with |
| 4433 | // new attributes. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4434 | if (result.IsProperty() && |
| 4435 | (attr != result.GetAttributes() || result.type() == CALLBACKS)) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4436 | // New attributes - normalize to avoid writing to instance descriptor |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4437 | if (js_object->IsJSGlobalProxy()) { |
| 4438 | // Since the result is a property, the prototype will exist so |
| 4439 | // we don't have to check for null. |
| 4440 | js_object = Handle<JSObject>(JSObject::cast(js_object->GetPrototype())); |
| 4441 | } |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4442 | JSObject::NormalizeProperties(js_object, CLEAR_INOBJECT_PROPERTIES, 0); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 4443 | // Use IgnoreAttributes version since a readonly property may be |
| 4444 | // overridden and SetProperty does not allow this. |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 4445 | return js_object->SetLocalPropertyIgnoreAttributes(*name, |
| 4446 | *obj_value, |
| 4447 | attr); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4448 | } |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4449 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4450 | return Runtime::ForceSetObjectProperty(isolate, |
| 4451 | js_object, |
| 4452 | name, |
| 4453 | obj_value, |
| 4454 | attr); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4455 | } |
| 4456 | |
| 4457 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4458 | // Special case for elements if any of the flags are true. |
| 4459 | // If elements are in fast case we always implicitly assume that: |
| 4460 | // DONT_DELETE: false, DONT_ENUM: false, READ_ONLY: false. |
| 4461 | static MaybeObject* NormalizeObjectSetElement(Isolate* isolate, |
| 4462 | Handle<JSObject> js_object, |
| 4463 | uint32_t index, |
| 4464 | Handle<Object> value, |
| 4465 | PropertyAttributes attr) { |
| 4466 | // Normalize the elements to enable attributes on the property. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4467 | Handle<SeededNumberDictionary> dictionary = |
| 4468 | JSObject::NormalizeElements(js_object); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4469 | // Make sure that we never go back to fast case. |
| 4470 | dictionary->set_requires_slow_elements(); |
| 4471 | PropertyDetails details = PropertyDetails(attr, NORMAL); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4472 | Handle<SeededNumberDictionary> extended_dictionary = |
| 4473 | SeededNumberDictionary::Set(dictionary, index, value, details); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4474 | if (*extended_dictionary != *dictionary) { |
| 4475 | js_object->set_elements(*extended_dictionary); |
| 4476 | } |
| 4477 | return *value; |
| 4478 | } |
| 4479 | |
| 4480 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4481 | MaybeObject* Runtime::SetObjectProperty(Isolate* isolate, |
| 4482 | Handle<Object> object, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4483 | Handle<Object> key, |
| 4484 | Handle<Object> value, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4485 | PropertyAttributes attr, |
| 4486 | StrictModeFlag strict_mode) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4487 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4488 | |
| 4489 | if (object->IsUndefined() || object->IsNull()) { |
| 4490 | Handle<Object> args[2] = { key, object }; |
| 4491 | Handle<Object> error = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4492 | isolate->factory()->NewTypeError("non_object_property_store", |
| 4493 | HandleVector(args, 2)); |
| 4494 | return isolate->Throw(*error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4497 | if (object->IsJSProxy()) { |
| 4498 | bool has_pending_exception = false; |
| 4499 | Handle<Object> name = Execution::ToString(key, &has_pending_exception); |
| 4500 | if (has_pending_exception) return Failure::Exception(); |
| 4501 | return JSProxy::cast(*object)->SetProperty( |
| 4502 | String::cast(*name), *value, attr, strict_mode); |
| 4503 | } |
| 4504 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4505 | // If the object isn't a JavaScript object, we ignore the store. |
| 4506 | if (!object->IsJSObject()) return *value; |
| 4507 | |
| 4508 | Handle<JSObject> js_object = Handle<JSObject>::cast(object); |
| 4509 | |
| 4510 | // Check if the given key is an array index. |
| 4511 | uint32_t index; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4512 | if (key->ToArrayIndex(&index)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4513 | // In Firefox/SpiderMonkey, Safari and Opera you can access the characters |
| 4514 | // of a string using [] notation. We need to support this too in |
| 4515 | // JavaScript. |
| 4516 | // In the case of a String object we just need to redirect the assignment to |
| 4517 | // the underlying string if the index is in range. Since the underlying |
| 4518 | // string does nothing with the assignment then we can ignore such |
| 4519 | // assignments. |
| 4520 | if (js_object->IsStringObjectWithCharacterAt(index)) { |
| 4521 | return *value; |
| 4522 | } |
| 4523 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4524 | if (((attr & (DONT_DELETE | DONT_ENUM | READ_ONLY)) != 0)) { |
| 4525 | return NormalizeObjectSetElement(isolate, js_object, index, value, attr); |
| 4526 | } |
| 4527 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4528 | Handle<Object> result = |
| 4529 | JSObject::SetElement(js_object, index, value, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4530 | if (result.is_null()) return Failure::Exception(); |
| 4531 | return *value; |
| 4532 | } |
| 4533 | |
| 4534 | if (key->IsString()) { |
| 4535 | Handle<Object> result; |
| 4536 | if (Handle<String>::cast(key)->AsArrayIndex(&index)) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4537 | if (((attr & (DONT_DELETE | DONT_ENUM | READ_ONLY)) != 0)) { |
| 4538 | return NormalizeObjectSetElement(isolate, |
| 4539 | js_object, |
| 4540 | index, |
| 4541 | value, |
| 4542 | attr); |
| 4543 | } |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4544 | result = |
| 4545 | JSObject::SetElement(js_object, index, value, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4546 | } else { |
| 4547 | Handle<String> key_string = Handle<String>::cast(key); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4548 | key_string->TryFlatten(); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4549 | result = JSReceiver::SetProperty( |
| 4550 | js_object, key_string, value, attr, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4551 | } |
| 4552 | if (result.is_null()) return Failure::Exception(); |
| 4553 | return *value; |
| 4554 | } |
| 4555 | |
| 4556 | // Call-back into JavaScript to convert the key to a string. |
| 4557 | bool has_pending_exception = false; |
| 4558 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4559 | if (has_pending_exception) return Failure::Exception(); |
| 4560 | Handle<String> name = Handle<String>::cast(converted); |
| 4561 | |
| 4562 | if (name->AsArrayIndex(&index)) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4563 | return js_object->SetElement(index, *value, strict_mode, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4564 | } else { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4565 | return js_object->SetProperty(*name, *value, attr, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4566 | } |
| 4567 | } |
| 4568 | |
| 4569 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4570 | MaybeObject* Runtime::ForceSetObjectProperty(Isolate* isolate, |
| 4571 | Handle<JSObject> js_object, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4572 | Handle<Object> key, |
| 4573 | Handle<Object> value, |
| 4574 | PropertyAttributes attr) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4575 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4576 | |
| 4577 | // Check if the given key is an array index. |
| 4578 | uint32_t index; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4579 | if (key->ToArrayIndex(&index)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4580 | // In Firefox/SpiderMonkey, Safari and Opera you can access the characters |
| 4581 | // of a string using [] notation. We need to support this too in |
| 4582 | // JavaScript. |
| 4583 | // In the case of a String object we just need to redirect the assignment to |
| 4584 | // the underlying string if the index is in range. Since the underlying |
| 4585 | // string does nothing with the assignment then we can ignore such |
| 4586 | // assignments. |
| 4587 | if (js_object->IsStringObjectWithCharacterAt(index)) { |
| 4588 | return *value; |
| 4589 | } |
| 4590 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4591 | return js_object->SetElement(index, *value, kNonStrictMode, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4592 | } |
| 4593 | |
| 4594 | if (key->IsString()) { |
| 4595 | if (Handle<String>::cast(key)->AsArrayIndex(&index)) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4596 | return js_object->SetElement(index, *value, kNonStrictMode, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4597 | } else { |
| 4598 | Handle<String> key_string = Handle<String>::cast(key); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4599 | key_string->TryFlatten(); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 4600 | return js_object->SetLocalPropertyIgnoreAttributes(*key_string, |
| 4601 | *value, |
| 4602 | attr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4603 | } |
| 4604 | } |
| 4605 | |
| 4606 | // Call-back into JavaScript to convert the key to a string. |
| 4607 | bool has_pending_exception = false; |
| 4608 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4609 | if (has_pending_exception) return Failure::Exception(); |
| 4610 | Handle<String> name = Handle<String>::cast(converted); |
| 4611 | |
| 4612 | if (name->AsArrayIndex(&index)) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4613 | return js_object->SetElement(index, *value, kNonStrictMode, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4614 | } else { |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 4615 | return js_object->SetLocalPropertyIgnoreAttributes(*name, *value, attr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4616 | } |
| 4617 | } |
| 4618 | |
| 4619 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4620 | MaybeObject* Runtime::ForceDeleteObjectProperty(Isolate* isolate, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4621 | Handle<JSReceiver> receiver, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4622 | Handle<Object> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4623 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4624 | |
| 4625 | // Check if the given key is an array index. |
| 4626 | uint32_t index; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4627 | if (key->ToArrayIndex(&index)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4628 | // In Firefox/SpiderMonkey, Safari and Opera you can access the |
| 4629 | // characters of a string using [] notation. In the case of a |
| 4630 | // String object we just need to redirect the deletion to the |
| 4631 | // underlying string if the index is in range. Since the |
| 4632 | // underlying string does nothing with the deletion, we can ignore |
| 4633 | // such deletions. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4634 | if (receiver->IsStringObjectWithCharacterAt(index)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4635 | return isolate->heap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4636 | } |
| 4637 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4638 | return receiver->DeleteElement(index, JSReceiver::FORCE_DELETION); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | Handle<String> key_string; |
| 4642 | if (key->IsString()) { |
| 4643 | key_string = Handle<String>::cast(key); |
| 4644 | } else { |
| 4645 | // Call-back into JavaScript to convert the key to a string. |
| 4646 | bool has_pending_exception = false; |
| 4647 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4648 | if (has_pending_exception) return Failure::Exception(); |
| 4649 | key_string = Handle<String>::cast(converted); |
| 4650 | } |
| 4651 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4652 | key_string->TryFlatten(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4653 | return receiver->DeleteProperty(*key_string, JSReceiver::FORCE_DELETION); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4654 | } |
| 4655 | |
| 4656 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4657 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetProperty) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4658 | NoHandleAllocation ha; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4659 | RUNTIME_ASSERT(args.length() == 4 || args.length() == 5); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4660 | |
| 4661 | Handle<Object> object = args.at<Object>(0); |
| 4662 | Handle<Object> key = args.at<Object>(1); |
| 4663 | Handle<Object> value = args.at<Object>(2); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4664 | CONVERT_SMI_ARG_CHECKED(unchecked_attributes, 3); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4665 | RUNTIME_ASSERT( |
| 4666 | (unchecked_attributes & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4667 | // Compute attributes. |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4668 | PropertyAttributes attributes = |
| 4669 | static_cast<PropertyAttributes>(unchecked_attributes); |
| 4670 | |
| 4671 | StrictModeFlag strict_mode = kNonStrictMode; |
| 4672 | if (args.length() == 5) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4673 | CONVERT_STRICT_MODE_ARG(strict_mode_flag, 4); |
| 4674 | strict_mode = strict_mode_flag; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4675 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4676 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4677 | return Runtime::SetObjectProperty(isolate, |
| 4678 | object, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4679 | key, |
| 4680 | value, |
| 4681 | attributes, |
| 4682 | strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
| 4685 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4686 | RUNTIME_FUNCTION(MaybeObject*, Runtime_TransitionElementsSmiToDouble) { |
| 4687 | NoHandleAllocation ha; |
| 4688 | RUNTIME_ASSERT(args.length() == 1); |
| 4689 | Handle<Object> object = args.at<Object>(0); |
| 4690 | return TransitionElements(object, FAST_DOUBLE_ELEMENTS, isolate); |
| 4691 | } |
| 4692 | |
| 4693 | |
| 4694 | RUNTIME_FUNCTION(MaybeObject*, Runtime_TransitionElementsDoubleToObject) { |
| 4695 | NoHandleAllocation ha; |
| 4696 | RUNTIME_ASSERT(args.length() == 1); |
| 4697 | Handle<Object> object = args.at<Object>(0); |
| 4698 | return TransitionElements(object, FAST_ELEMENTS, isolate); |
| 4699 | } |
| 4700 | |
| 4701 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4702 | // Set the native flag on the function. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4703 | // This is used to decide if we should transform null and undefined |
| 4704 | // into the global object when doing call and apply. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4705 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetNativeFlag) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4706 | NoHandleAllocation ha; |
| 4707 | RUNTIME_ASSERT(args.length() == 1); |
| 4708 | |
| 4709 | Handle<Object> object = args.at<Object>(0); |
| 4710 | |
| 4711 | if (object->IsJSFunction()) { |
| 4712 | JSFunction* func = JSFunction::cast(*object); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4713 | func->shared()->set_native(true); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4714 | } |
| 4715 | return isolate->heap()->undefined_value(); |
| 4716 | } |
| 4717 | |
| 4718 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4719 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StoreArrayLiteralElement) { |
| 4720 | RUNTIME_ASSERT(args.length() == 5); |
| 4721 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
| 4722 | CONVERT_SMI_ARG_CHECKED(store_index, 1); |
| 4723 | Handle<Object> value = args.at<Object>(2); |
| 4724 | CONVERT_ARG_CHECKED(FixedArray, literals, 3); |
| 4725 | CONVERT_SMI_ARG_CHECKED(literal_index, 4); |
| 4726 | HandleScope scope; |
| 4727 | |
| 4728 | Object* raw_boilerplate_object = literals->get(literal_index); |
| 4729 | Handle<JSArray> boilerplate_object(JSArray::cast(raw_boilerplate_object)); |
| 4730 | #if DEBUG |
| 4731 | ElementsKind elements_kind = object->GetElementsKind(); |
| 4732 | #endif |
| 4733 | ASSERT(elements_kind <= FAST_DOUBLE_ELEMENTS); |
| 4734 | // Smis should never trigger transitions. |
| 4735 | ASSERT(!value->IsSmi()); |
| 4736 | |
| 4737 | if (value->IsNumber()) { |
| 4738 | ASSERT(elements_kind == FAST_SMI_ONLY_ELEMENTS); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4739 | JSObject::TransitionElementsKind(object, FAST_DOUBLE_ELEMENTS); |
| 4740 | JSObject::TransitionElementsKind(boilerplate_object, FAST_DOUBLE_ELEMENTS); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4741 | ASSERT(object->GetElementsKind() == FAST_DOUBLE_ELEMENTS); |
| 4742 | FixedDoubleArray* double_array = |
| 4743 | FixedDoubleArray::cast(object->elements()); |
| 4744 | HeapNumber* number = HeapNumber::cast(*value); |
| 4745 | double_array->set(store_index, number->Number()); |
| 4746 | } else { |
| 4747 | ASSERT(elements_kind == FAST_SMI_ONLY_ELEMENTS || |
| 4748 | elements_kind == FAST_DOUBLE_ELEMENTS); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4749 | JSObject::TransitionElementsKind(object, FAST_ELEMENTS); |
| 4750 | JSObject::TransitionElementsKind(boilerplate_object, FAST_ELEMENTS); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4751 | FixedArray* object_array = |
| 4752 | FixedArray::cast(object->elements()); |
| 4753 | object_array->set(store_index, *value); |
| 4754 | } |
| 4755 | return *object; |
| 4756 | } |
| 4757 | |
| 4758 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4759 | // Set a local property, even if it is READ_ONLY. If the property does not |
| 4760 | // exist, it will be added with attributes NONE. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4761 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IgnoreAttributesAndSetProperty) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4762 | NoHandleAllocation ha; |
| 4763 | RUNTIME_ASSERT(args.length() == 3 || args.length() == 4); |
| 4764 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 4765 | CONVERT_CHECKED(String, name, args[1]); |
| 4766 | // Compute attributes. |
| 4767 | PropertyAttributes attributes = NONE; |
| 4768 | if (args.length() == 4) { |
| 4769 | CONVERT_CHECKED(Smi, value_obj, args[3]); |
| 4770 | int unchecked_value = value_obj->value(); |
| 4771 | // Only attribute bits should be set. |
| 4772 | RUNTIME_ASSERT( |
| 4773 | (unchecked_value & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 4774 | attributes = static_cast<PropertyAttributes>(unchecked_value); |
| 4775 | } |
| 4776 | |
| 4777 | return object-> |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 4778 | SetLocalPropertyIgnoreAttributes(name, args[2], attributes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4779 | } |
| 4780 | |
| 4781 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4782 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeleteProperty) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4783 | NoHandleAllocation ha; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4784 | ASSERT(args.length() == 3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4785 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4786 | CONVERT_CHECKED(JSReceiver, object, args[0]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4787 | CONVERT_CHECKED(String, key, args[1]); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4788 | CONVERT_STRICT_MODE_ARG(strict_mode, 2); |
| 4789 | return object->DeleteProperty(key, (strict_mode == kStrictMode) |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4790 | ? JSReceiver::STRICT_DELETION |
| 4791 | : JSReceiver::NORMAL_DELETION); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
| 4794 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4795 | static Object* HasLocalPropertyImplementation(Isolate* isolate, |
| 4796 | Handle<JSObject> object, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4797 | Handle<String> key) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4798 | if (object->HasLocalProperty(*key)) return isolate->heap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4799 | // Handle hidden prototypes. If there's a hidden prototype above this thing |
| 4800 | // then we have to check it for properties, because they are supposed to |
| 4801 | // look like they are on this object. |
| 4802 | Handle<Object> proto(object->GetPrototype()); |
| 4803 | if (proto->IsJSObject() && |
| 4804 | Handle<JSObject>::cast(proto)->map()->is_hidden_prototype()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4805 | return HasLocalPropertyImplementation(isolate, |
| 4806 | Handle<JSObject>::cast(proto), |
| 4807 | key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4808 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4809 | return isolate->heap()->false_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4810 | } |
| 4811 | |
| 4812 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4813 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasLocalProperty) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4814 | NoHandleAllocation ha; |
| 4815 | ASSERT(args.length() == 2); |
| 4816 | CONVERT_CHECKED(String, key, args[1]); |
| 4817 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4818 | uint32_t index; |
| 4819 | const bool key_is_array_index = key->AsArrayIndex(&index); |
| 4820 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4821 | Object* obj = args[0]; |
| 4822 | // Only JS objects can have properties. |
| 4823 | if (obj->IsJSObject()) { |
| 4824 | JSObject* object = JSObject::cast(obj); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4825 | // Fast case: either the key is a real named property or it is not |
| 4826 | // an array index and there are no interceptors or hidden |
| 4827 | // prototypes. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4828 | if (object->HasRealNamedProperty(key)) return isolate->heap()->true_value(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4829 | Map* map = object->map(); |
| 4830 | if (!key_is_array_index && |
| 4831 | !map->has_named_interceptor() && |
| 4832 | !HeapObject::cast(map->prototype())->map()->is_hidden_prototype()) { |
| 4833 | return isolate->heap()->false_value(); |
| 4834 | } |
| 4835 | // Slow case. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4836 | HandleScope scope(isolate); |
| 4837 | return HasLocalPropertyImplementation(isolate, |
| 4838 | Handle<JSObject>(object), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4839 | Handle<String>(key)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4840 | } else if (obj->IsString() && key_is_array_index) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4841 | // Well, there is one exception: Handle [] on strings. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4842 | String* string = String::cast(obj); |
| 4843 | if (index < static_cast<uint32_t>(string->length())) { |
| 4844 | return isolate->heap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4845 | } |
| 4846 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4847 | return isolate->heap()->false_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4848 | } |
| 4849 | |
| 4850 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4851 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasProperty) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4852 | NoHandleAllocation na; |
| 4853 | ASSERT(args.length() == 2); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4854 | CONVERT_CHECKED(JSReceiver, receiver, args[0]); |
| 4855 | CONVERT_CHECKED(String, key, args[1]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4856 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4857 | bool result = receiver->HasProperty(key); |
| 4858 | if (isolate->has_pending_exception()) return Failure::Exception(); |
| 4859 | return isolate->heap()->ToBoolean(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4860 | } |
| 4861 | |
| 4862 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4863 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasElement) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4864 | NoHandleAllocation na; |
| 4865 | ASSERT(args.length() == 2); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4866 | CONVERT_CHECKED(JSReceiver, receiver, args[0]); |
| 4867 | CONVERT_CHECKED(Smi, index, args[1]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4868 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4869 | bool result = receiver->HasElement(index->value()); |
| 4870 | if (isolate->has_pending_exception()) return Failure::Exception(); |
| 4871 | return isolate->heap()->ToBoolean(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4872 | } |
| 4873 | |
| 4874 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4875 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsPropertyEnumerable) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4876 | NoHandleAllocation ha; |
| 4877 | ASSERT(args.length() == 2); |
| 4878 | |
| 4879 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 4880 | CONVERT_CHECKED(String, key, args[1]); |
| 4881 | |
| 4882 | uint32_t index; |
| 4883 | if (key->AsArrayIndex(&index)) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4884 | JSObject::LocalElementType type = object->HasLocalElement(index); |
| 4885 | switch (type) { |
| 4886 | case JSObject::UNDEFINED_ELEMENT: |
| 4887 | case JSObject::STRING_CHARACTER_ELEMENT: |
| 4888 | return isolate->heap()->false_value(); |
| 4889 | case JSObject::INTERCEPTED_ELEMENT: |
| 4890 | case JSObject::FAST_ELEMENT: |
| 4891 | return isolate->heap()->true_value(); |
| 4892 | case JSObject::DICTIONARY_ELEMENT: { |
| 4893 | if (object->IsJSGlobalProxy()) { |
| 4894 | Object* proto = object->GetPrototype(); |
| 4895 | if (proto->IsNull()) { |
| 4896 | return isolate->heap()->false_value(); |
| 4897 | } |
| 4898 | ASSERT(proto->IsJSGlobalObject()); |
| 4899 | object = JSObject::cast(proto); |
| 4900 | } |
| 4901 | FixedArray* elements = FixedArray::cast(object->elements()); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4902 | SeededNumberDictionary* dictionary = NULL; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4903 | if (elements->map() == |
| 4904 | isolate->heap()->non_strict_arguments_elements_map()) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4905 | dictionary = SeededNumberDictionary::cast(elements->get(1)); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4906 | } else { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4907 | dictionary = SeededNumberDictionary::cast(elements); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4908 | } |
| 4909 | int entry = dictionary->FindEntry(index); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 4910 | ASSERT(entry != SeededNumberDictionary::kNotFound); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4911 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 4912 | return isolate->heap()->ToBoolean(!details.IsDontEnum()); |
| 4913 | } |
| 4914 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4915 | } |
| 4916 | |
| 4917 | PropertyAttributes att = object->GetLocalPropertyAttribute(key); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4918 | return isolate->heap()->ToBoolean(att != ABSENT && (att & DONT_ENUM) == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4919 | } |
| 4920 | |
| 4921 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4922 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetPropertyNames) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4923 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4924 | ASSERT(args.length() == 1); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4925 | CONVERT_ARG_CHECKED(JSReceiver, object, 0); |
| 4926 | bool threw = false; |
| 4927 | Handle<JSArray> result = GetKeysFor(object, &threw); |
| 4928 | if (threw) return Failure::Exception(); |
| 4929 | return *result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4930 | } |
| 4931 | |
| 4932 | |
| 4933 | // Returns either a FixedArray as Runtime_GetPropertyNames, |
| 4934 | // or, if the given object has an enum cache that contains |
| 4935 | // all enumerable properties of the object and its prototypes |
| 4936 | // have none, the map of the object. This is used to speed up |
| 4937 | // the check for deletions during a for-in. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4938 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetPropertyNamesFast) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4939 | ASSERT(args.length() == 1); |
| 4940 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4941 | CONVERT_CHECKED(JSReceiver, raw_object, args[0]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4942 | |
| 4943 | if (raw_object->IsSimpleEnum()) return raw_object->map(); |
| 4944 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4945 | HandleScope scope(isolate); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 4946 | Handle<JSReceiver> object(raw_object); |
| 4947 | bool threw = false; |
| 4948 | Handle<FixedArray> content = |
| 4949 | GetKeysInFixedArrayFor(object, INCLUDE_PROTOS, &threw); |
| 4950 | if (threw) return Failure::Exception(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4951 | |
| 4952 | // Test again, since cache may have been built by preceding call. |
| 4953 | if (object->IsSimpleEnum()) return object->map(); |
| 4954 | |
| 4955 | return *content; |
| 4956 | } |
| 4957 | |
| 4958 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4959 | // Find the length of the prototype chain that is to to handled as one. If a |
| 4960 | // prototype object is hidden it is to be viewed as part of the the object it |
| 4961 | // is prototype for. |
| 4962 | static int LocalPrototypeChainLength(JSObject* obj) { |
| 4963 | int count = 1; |
| 4964 | Object* proto = obj->GetPrototype(); |
| 4965 | while (proto->IsJSObject() && |
| 4966 | JSObject::cast(proto)->map()->is_hidden_prototype()) { |
| 4967 | count++; |
| 4968 | proto = JSObject::cast(proto)->GetPrototype(); |
| 4969 | } |
| 4970 | return count; |
| 4971 | } |
| 4972 | |
| 4973 | |
| 4974 | // Return the names of the local named properties. |
| 4975 | // args[0]: object |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4976 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLocalPropertyNames) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4977 | HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4978 | ASSERT(args.length() == 1); |
| 4979 | if (!args[0]->IsJSObject()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4980 | return isolate->heap()->undefined_value(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4981 | } |
| 4982 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4983 | |
| 4984 | // Skip the global proxy as it has no properties and always delegates to the |
| 4985 | // real global object. |
| 4986 | if (obj->IsJSGlobalProxy()) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 4987 | // Only collect names if access is permitted. |
| 4988 | if (obj->IsAccessCheckNeeded() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4989 | !isolate->MayNamedAccess(*obj, |
| 4990 | isolate->heap()->undefined_value(), |
| 4991 | v8::ACCESS_KEYS)) { |
| 4992 | isolate->ReportFailedAccessCheck(*obj, v8::ACCESS_KEYS); |
| 4993 | return *isolate->factory()->NewJSArray(0); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 4994 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4995 | obj = Handle<JSObject>(JSObject::cast(obj->GetPrototype())); |
| 4996 | } |
| 4997 | |
| 4998 | // Find the number of objects making up this. |
| 4999 | int length = LocalPrototypeChainLength(*obj); |
| 5000 | |
| 5001 | // Find the number of local properties for each of the objects. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5002 | ScopedVector<int> local_property_count(length); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5003 | int total_property_count = 0; |
| 5004 | Handle<JSObject> jsproto = obj; |
| 5005 | for (int i = 0; i < length; i++) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 5006 | // Only collect names if access is permitted. |
| 5007 | if (jsproto->IsAccessCheckNeeded() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5008 | !isolate->MayNamedAccess(*jsproto, |
| 5009 | isolate->heap()->undefined_value(), |
| 5010 | v8::ACCESS_KEYS)) { |
| 5011 | isolate->ReportFailedAccessCheck(*jsproto, v8::ACCESS_KEYS); |
| 5012 | return *isolate->factory()->NewJSArray(0); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 5013 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5014 | int n; |
| 5015 | n = jsproto->NumberOfLocalProperties(static_cast<PropertyAttributes>(NONE)); |
| 5016 | local_property_count[i] = n; |
| 5017 | total_property_count += n; |
| 5018 | if (i < length - 1) { |
| 5019 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 5020 | } |
| 5021 | } |
| 5022 | |
| 5023 | // Allocate an array with storage for all the property names. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5024 | Handle<FixedArray> names = |
| 5025 | isolate->factory()->NewFixedArray(total_property_count); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5026 | |
| 5027 | // Get the property names. |
| 5028 | jsproto = obj; |
| 5029 | int proto_with_hidden_properties = 0; |
Ben Murdoch | 6d7cb00 | 2011-08-04 19:25:22 +0100 | [diff] [blame] | 5030 | int next_copy_index = 0; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5031 | for (int i = 0; i < length; i++) { |
Ben Murdoch | 6d7cb00 | 2011-08-04 19:25:22 +0100 | [diff] [blame] | 5032 | jsproto->GetLocalPropertyNames(*names, next_copy_index); |
| 5033 | next_copy_index += local_property_count[i]; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5034 | if (jsproto->HasHiddenProperties()) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5035 | proto_with_hidden_properties++; |
| 5036 | } |
| 5037 | if (i < length - 1) { |
| 5038 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 5039 | } |
| 5040 | } |
| 5041 | |
| 5042 | // Filter out name of hidden propeties object. |
| 5043 | if (proto_with_hidden_properties > 0) { |
| 5044 | Handle<FixedArray> old_names = names; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5045 | names = isolate->factory()->NewFixedArray( |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5046 | names->length() - proto_with_hidden_properties); |
| 5047 | int dest_pos = 0; |
| 5048 | for (int i = 0; i < total_property_count; i++) { |
| 5049 | Object* name = old_names->get(i); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5050 | if (name == isolate->heap()->hidden_symbol()) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5051 | continue; |
| 5052 | } |
| 5053 | names->set(dest_pos++, name); |
| 5054 | } |
| 5055 | } |
| 5056 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5057 | return *isolate->factory()->NewJSArrayWithElements(names); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5058 | } |
| 5059 | |
| 5060 | |
| 5061 | // Return the names of the local indexed properties. |
| 5062 | // args[0]: object |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5063 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLocalElementNames) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5064 | HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5065 | ASSERT(args.length() == 1); |
| 5066 | if (!args[0]->IsJSObject()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5067 | return isolate->heap()->undefined_value(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5068 | } |
| 5069 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 5070 | |
| 5071 | int n = obj->NumberOfLocalElements(static_cast<PropertyAttributes>(NONE)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5072 | Handle<FixedArray> names = isolate->factory()->NewFixedArray(n); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5073 | obj->GetLocalElementKeys(*names, static_cast<PropertyAttributes>(NONE)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5074 | return *isolate->factory()->NewJSArrayWithElements(names); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5075 | } |
| 5076 | |
| 5077 | |
| 5078 | // Return information on whether an object has a named or indexed interceptor. |
| 5079 | // args[0]: object |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5080 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetInterceptorInfo) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5081 | HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5082 | ASSERT(args.length() == 1); |
| 5083 | if (!args[0]->IsJSObject()) { |
| 5084 | return Smi::FromInt(0); |
| 5085 | } |
| 5086 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 5087 | |
| 5088 | int result = 0; |
| 5089 | if (obj->HasNamedInterceptor()) result |= 2; |
| 5090 | if (obj->HasIndexedInterceptor()) result |= 1; |
| 5091 | |
| 5092 | return Smi::FromInt(result); |
| 5093 | } |
| 5094 | |
| 5095 | |
| 5096 | // Return property names from named interceptor. |
| 5097 | // args[0]: object |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5098 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetNamedInterceptorPropertyNames) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5099 | HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5100 | ASSERT(args.length() == 1); |
| 5101 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 5102 | |
| 5103 | if (obj->HasNamedInterceptor()) { |
| 5104 | v8::Handle<v8::Array> result = GetKeysForNamedInterceptor(obj, obj); |
| 5105 | if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result); |
| 5106 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5107 | return isolate->heap()->undefined_value(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5108 | } |
| 5109 | |
| 5110 | |
| 5111 | // Return element names from indexed interceptor. |
| 5112 | // args[0]: object |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5113 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetIndexedInterceptorElementNames) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5114 | HandleScope scope(isolate); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5115 | ASSERT(args.length() == 1); |
| 5116 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 5117 | |
| 5118 | if (obj->HasIndexedInterceptor()) { |
| 5119 | v8::Handle<v8::Array> result = GetKeysForIndexedInterceptor(obj, obj); |
| 5120 | if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result); |
| 5121 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5122 | return isolate->heap()->undefined_value(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5123 | } |
| 5124 | |
| 5125 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5126 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LocalKeys) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5127 | ASSERT_EQ(args.length(), 1); |
| 5128 | CONVERT_CHECKED(JSObject, raw_object, args[0]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5129 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5130 | Handle<JSObject> object(raw_object); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 5131 | |
| 5132 | if (object->IsJSGlobalProxy()) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5133 | // Do access checks before going to the global object. |
| 5134 | if (object->IsAccessCheckNeeded() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5135 | !isolate->MayNamedAccess(*object, isolate->heap()->undefined_value(), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5136 | v8::ACCESS_KEYS)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5137 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_KEYS); |
| 5138 | return *isolate->factory()->NewJSArray(0); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5139 | } |
| 5140 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 5141 | Handle<Object> proto(object->GetPrototype()); |
| 5142 | // If proxy is detached we simply return an empty array. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5143 | if (proto->IsNull()) return *isolate->factory()->NewJSArray(0); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 5144 | object = Handle<JSObject>::cast(proto); |
| 5145 | } |
| 5146 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 5147 | bool threw = false; |
| 5148 | Handle<FixedArray> contents = |
| 5149 | GetKeysInFixedArrayFor(object, LOCAL_ONLY, &threw); |
| 5150 | if (threw) return Failure::Exception(); |
| 5151 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5152 | // Some fast paths through GetKeysInFixedArrayFor reuse a cached |
| 5153 | // property array and since the result is mutable we have to create |
| 5154 | // a fresh clone on each invocation. |
| 5155 | int length = contents->length(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5156 | Handle<FixedArray> copy = isolate->factory()->NewFixedArray(length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5157 | for (int i = 0; i < length; i++) { |
| 5158 | Object* entry = contents->get(i); |
| 5159 | if (entry->IsString()) { |
| 5160 | copy->set(i, entry); |
| 5161 | } else { |
| 5162 | ASSERT(entry->IsNumber()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5163 | HandleScope scope(isolate); |
| 5164 | Handle<Object> entry_handle(entry, isolate); |
| 5165 | Handle<Object> entry_str = |
| 5166 | isolate->factory()->NumberToString(entry_handle); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5167 | copy->set(i, *entry_str); |
| 5168 | } |
| 5169 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5170 | return *isolate->factory()->NewJSArrayWithElements(copy); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5171 | } |
| 5172 | |
| 5173 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5174 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetArgumentsProperty) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5175 | NoHandleAllocation ha; |
| 5176 | ASSERT(args.length() == 1); |
| 5177 | |
| 5178 | // Compute the frame holding the arguments. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5179 | JavaScriptFrameIterator it(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5180 | it.AdvanceToArgumentsFrame(); |
| 5181 | JavaScriptFrame* frame = it.frame(); |
| 5182 | |
| 5183 | // Get the actual number of provided arguments. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5184 | const uint32_t n = frame->ComputeParametersCount(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5185 | |
| 5186 | // Try to convert the key to an index. If successful and within |
| 5187 | // index return the the argument from the frame. |
| 5188 | uint32_t index; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5189 | if (args[0]->ToArrayIndex(&index) && index < n) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5190 | return frame->GetParameter(index); |
| 5191 | } |
| 5192 | |
| 5193 | // Convert the key to a string. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5194 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5195 | bool exception = false; |
| 5196 | Handle<Object> converted = |
| 5197 | Execution::ToString(args.at<Object>(0), &exception); |
| 5198 | if (exception) return Failure::Exception(); |
| 5199 | Handle<String> key = Handle<String>::cast(converted); |
| 5200 | |
| 5201 | // Try to convert the string key into an array index. |
| 5202 | if (key->AsArrayIndex(&index)) { |
| 5203 | if (index < n) { |
| 5204 | return frame->GetParameter(index); |
| 5205 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5206 | return isolate->initial_object_prototype()->GetElement(index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5207 | } |
| 5208 | } |
| 5209 | |
| 5210 | // Handle special arguments properties. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5211 | if (key->Equals(isolate->heap()->length_symbol())) return Smi::FromInt(n); |
| 5212 | if (key->Equals(isolate->heap()->callee_symbol())) { |
| 5213 | Object* function = frame->function(); |
| 5214 | if (function->IsJSFunction() && |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 5215 | !JSFunction::cast(function)->shared()->is_classic_mode()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5216 | return isolate->Throw(*isolate->factory()->NewTypeError( |
| 5217 | "strict_arguments_callee", HandleVector<Object>(NULL, 0))); |
| 5218 | } |
| 5219 | return function; |
| 5220 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5221 | |
| 5222 | // Lookup in the initial Object.prototype object. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5223 | return isolate->initial_object_prototype()->GetProperty(*key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5224 | } |
| 5225 | |
| 5226 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5227 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ToFastProperties) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5228 | ASSERT(args.length() == 1); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 5229 | Object* object = args[0]; |
| 5230 | return (object->IsJSObject() && !object->IsGlobalObject()) |
| 5231 | ? JSObject::cast(object)->TransformToFastProperties(0) |
| 5232 | : object; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5233 | } |
| 5234 | |
| 5235 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5236 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ToSlowProperties) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5237 | ASSERT(args.length() == 1); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 5238 | Object* obj = args[0]; |
| 5239 | return (obj->IsJSObject() && !obj->IsJSGlobalProxy()) |
| 5240 | ? JSObject::cast(obj)->NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0) |
| 5241 | : obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5242 | } |
| 5243 | |
| 5244 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5245 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ToBool) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5246 | NoHandleAllocation ha; |
| 5247 | ASSERT(args.length() == 1); |
| 5248 | |
| 5249 | return args[0]->ToBoolean(); |
| 5250 | } |
| 5251 | |
| 5252 | |
| 5253 | // Returns the type string of a value; see ECMA-262, 11.4.3 (p 47). |
| 5254 | // Possible optimizations: put the type string into the oddballs. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5255 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Typeof) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5256 | NoHandleAllocation ha; |
| 5257 | |
| 5258 | Object* obj = args[0]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5259 | if (obj->IsNumber()) return isolate->heap()->number_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5260 | HeapObject* heap_obj = HeapObject::cast(obj); |
| 5261 | |
| 5262 | // typeof an undetectable object is 'undefined' |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5263 | if (heap_obj->map()->is_undetectable()) { |
| 5264 | return isolate->heap()->undefined_symbol(); |
| 5265 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5266 | |
| 5267 | InstanceType instance_type = heap_obj->map()->instance_type(); |
| 5268 | if (instance_type < FIRST_NONSTRING_TYPE) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5269 | return isolate->heap()->string_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5270 | } |
| 5271 | |
| 5272 | switch (instance_type) { |
| 5273 | case ODDBALL_TYPE: |
| 5274 | if (heap_obj->IsTrue() || heap_obj->IsFalse()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5275 | return isolate->heap()->boolean_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5276 | } |
| 5277 | if (heap_obj->IsNull()) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5278 | return FLAG_harmony_typeof |
| 5279 | ? isolate->heap()->null_symbol() |
| 5280 | : isolate->heap()->object_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5281 | } |
| 5282 | ASSERT(heap_obj->IsUndefined()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5283 | return isolate->heap()->undefined_symbol(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5284 | case JS_FUNCTION_TYPE: |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 5285 | case JS_FUNCTION_PROXY_TYPE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5286 | return isolate->heap()->function_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5287 | default: |
| 5288 | // For any kind of object not handled above, the spec rule for |
| 5289 | // host objects gives that it is okay to return "object" |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5290 | return isolate->heap()->object_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5291 | } |
| 5292 | } |
| 5293 | |
| 5294 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5295 | static bool AreDigits(const char*s, int from, int to) { |
| 5296 | for (int i = from; i < to; i++) { |
| 5297 | if (s[i] < '0' || s[i] > '9') return false; |
| 5298 | } |
| 5299 | |
| 5300 | return true; |
| 5301 | } |
| 5302 | |
| 5303 | |
| 5304 | static int ParseDecimalInteger(const char*s, int from, int to) { |
| 5305 | ASSERT(to - from < 10); // Overflow is not possible. |
| 5306 | ASSERT(from < to); |
| 5307 | int d = s[from] - '0'; |
| 5308 | |
| 5309 | for (int i = from + 1; i < to; i++) { |
| 5310 | d = 10 * d + (s[i] - '0'); |
| 5311 | } |
| 5312 | |
| 5313 | return d; |
| 5314 | } |
| 5315 | |
| 5316 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5317 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToNumber) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5318 | NoHandleAllocation ha; |
| 5319 | ASSERT(args.length() == 1); |
| 5320 | CONVERT_CHECKED(String, subject, args[0]); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5321 | subject->TryFlatten(); |
| 5322 | |
| 5323 | // Fast case: short integer or some sorts of junk values. |
| 5324 | int len = subject->length(); |
| 5325 | if (subject->IsSeqAsciiString()) { |
| 5326 | if (len == 0) return Smi::FromInt(0); |
| 5327 | |
| 5328 | char const* data = SeqAsciiString::cast(subject)->GetChars(); |
| 5329 | bool minus = (data[0] == '-'); |
| 5330 | int start_pos = (minus ? 1 : 0); |
| 5331 | |
| 5332 | if (start_pos == len) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5333 | return isolate->heap()->nan_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5334 | } else if (data[start_pos] > '9') { |
| 5335 | // Fast check for a junk value. A valid string may start from a |
| 5336 | // whitespace, a sign ('+' or '-'), the decimal point, a decimal digit or |
| 5337 | // the 'I' character ('Infinity'). All of that have codes not greater than |
| 5338 | // '9' except 'I'. |
| 5339 | if (data[start_pos] != 'I') { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5340 | return isolate->heap()->nan_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5341 | } |
| 5342 | } else if (len - start_pos < 10 && AreDigits(data, start_pos, len)) { |
| 5343 | // The maximal/minimal smi has 10 digits. If the string has less digits we |
| 5344 | // know it will fit into the smi-data type. |
| 5345 | int d = ParseDecimalInteger(data, start_pos, len); |
| 5346 | if (minus) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5347 | if (d == 0) return isolate->heap()->minus_zero_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5348 | d = -d; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5349 | } else if (!subject->HasHashCode() && |
| 5350 | len <= String::kMaxArrayIndexSize && |
| 5351 | (len == 1 || data[0] != '0')) { |
| 5352 | // String hash is not calculated yet but all the data are present. |
| 5353 | // Update the hash field to speed up sequential convertions. |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 5354 | uint32_t hash = StringHasher::MakeArrayIndexHash(d, len); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5355 | #ifdef DEBUG |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5356 | subject->Hash(); // Force hash calculation. |
| 5357 | ASSERT_EQ(static_cast<int>(subject->hash_field()), |
| 5358 | static_cast<int>(hash)); |
| 5359 | #endif |
| 5360 | subject->set_hash_field(hash); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5361 | } |
| 5362 | return Smi::FromInt(d); |
| 5363 | } |
| 5364 | } |
| 5365 | |
| 5366 | // Slower case. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5367 | return isolate->heap()->NumberFromDouble( |
| 5368 | StringToDouble(isolate->unicode_cache(), subject, ALLOW_HEX)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5369 | } |
| 5370 | |
| 5371 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5372 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringFromCharCodeArray) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5373 | NoHandleAllocation ha; |
| 5374 | ASSERT(args.length() == 1); |
| 5375 | |
| 5376 | CONVERT_CHECKED(JSArray, codes, args[0]); |
| 5377 | int length = Smi::cast(codes->length())->value(); |
| 5378 | |
| 5379 | // Check if the string can be ASCII. |
| 5380 | int i; |
| 5381 | for (i = 0; i < length; i++) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5382 | Object* element; |
| 5383 | { MaybeObject* maybe_element = codes->GetElement(i); |
| 5384 | // We probably can't get an exception here, but just in order to enforce |
| 5385 | // the checking of inputs in the runtime calls we check here. |
| 5386 | if (!maybe_element->ToObject(&element)) return maybe_element; |
| 5387 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5388 | CONVERT_NUMBER_CHECKED(int, chr, Int32, element); |
| 5389 | if ((chr & 0xffff) > String::kMaxAsciiCharCode) |
| 5390 | break; |
| 5391 | } |
| 5392 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5393 | MaybeObject* maybe_object = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5394 | if (i == length) { // The string is ASCII. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5395 | maybe_object = isolate->heap()->AllocateRawAsciiString(length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5396 | } else { // The string is not ASCII. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5397 | maybe_object = isolate->heap()->AllocateRawTwoByteString(length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5398 | } |
| 5399 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5400 | Object* object = NULL; |
| 5401 | if (!maybe_object->ToObject(&object)) return maybe_object; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5402 | String* result = String::cast(object); |
| 5403 | for (int i = 0; i < length; i++) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5404 | Object* element; |
| 5405 | { MaybeObject* maybe_element = codes->GetElement(i); |
| 5406 | if (!maybe_element->ToObject(&element)) return maybe_element; |
| 5407 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5408 | CONVERT_NUMBER_CHECKED(int, chr, Int32, element); |
| 5409 | result->Set(i, chr & 0xffff); |
| 5410 | } |
| 5411 | return result; |
| 5412 | } |
| 5413 | |
| 5414 | |
| 5415 | // kNotEscaped is generated by the following: |
| 5416 | // |
| 5417 | // #!/bin/perl |
| 5418 | // for (my $i = 0; $i < 256; $i++) { |
| 5419 | // print "\n" if $i % 16 == 0; |
| 5420 | // my $c = chr($i); |
| 5421 | // my $escaped = 1; |
| 5422 | // $escaped = 0 if $c =~ m#[A-Za-z0-9@*_+./-]#; |
| 5423 | // print $escaped ? "0, " : "1, "; |
| 5424 | // } |
| 5425 | |
| 5426 | |
| 5427 | static bool IsNotEscaped(uint16_t character) { |
| 5428 | // Only for 8 bit characters, the rest are always escaped (in a different way) |
| 5429 | ASSERT(character < 256); |
| 5430 | static const char kNotEscaped[256] = { |
| 5431 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5432 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5433 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, |
| 5434 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, |
| 5435 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 5436 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, |
| 5437 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 5438 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, |
| 5439 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5440 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5441 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5442 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5443 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5444 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5445 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5446 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5447 | }; |
| 5448 | return kNotEscaped[character] != 0; |
| 5449 | } |
| 5450 | |
| 5451 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5452 | RUNTIME_FUNCTION(MaybeObject*, Runtime_URIEscape) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5453 | const char hex_chars[] = "0123456789ABCDEF"; |
| 5454 | NoHandleAllocation ha; |
| 5455 | ASSERT(args.length() == 1); |
| 5456 | CONVERT_CHECKED(String, source, args[0]); |
| 5457 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5458 | source->TryFlatten(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5459 | |
| 5460 | int escaped_length = 0; |
| 5461 | int length = source->length(); |
| 5462 | { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5463 | Access<StringInputBuffer> buffer( |
| 5464 | isolate->runtime_state()->string_input_buffer()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5465 | buffer->Reset(source); |
| 5466 | while (buffer->has_more()) { |
| 5467 | uint16_t character = buffer->GetNext(); |
| 5468 | if (character >= 256) { |
| 5469 | escaped_length += 6; |
| 5470 | } else if (IsNotEscaped(character)) { |
| 5471 | escaped_length++; |
| 5472 | } else { |
| 5473 | escaped_length += 3; |
| 5474 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5475 | // We don't allow strings that are longer than a maximal length. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5476 | ASSERT(String::kMaxLength < 0x7fffffff - 6); // Cannot overflow. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5477 | if (escaped_length > String::kMaxLength) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5478 | isolate->context()->mark_out_of_memory(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5479 | return Failure::OutOfMemoryException(); |
| 5480 | } |
| 5481 | } |
| 5482 | } |
| 5483 | // No length change implies no change. Return original string if no change. |
| 5484 | if (escaped_length == length) { |
| 5485 | return source; |
| 5486 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5487 | Object* o; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5488 | { MaybeObject* maybe_o = |
| 5489 | isolate->heap()->AllocateRawAsciiString(escaped_length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5490 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 5491 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5492 | String* destination = String::cast(o); |
| 5493 | int dest_position = 0; |
| 5494 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5495 | Access<StringInputBuffer> buffer( |
| 5496 | isolate->runtime_state()->string_input_buffer()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5497 | buffer->Rewind(); |
| 5498 | while (buffer->has_more()) { |
| 5499 | uint16_t chr = buffer->GetNext(); |
| 5500 | if (chr >= 256) { |
| 5501 | destination->Set(dest_position, '%'); |
| 5502 | destination->Set(dest_position+1, 'u'); |
| 5503 | destination->Set(dest_position+2, hex_chars[chr >> 12]); |
| 5504 | destination->Set(dest_position+3, hex_chars[(chr >> 8) & 0xf]); |
| 5505 | destination->Set(dest_position+4, hex_chars[(chr >> 4) & 0xf]); |
| 5506 | destination->Set(dest_position+5, hex_chars[chr & 0xf]); |
| 5507 | dest_position += 6; |
| 5508 | } else if (IsNotEscaped(chr)) { |
| 5509 | destination->Set(dest_position, chr); |
| 5510 | dest_position++; |
| 5511 | } else { |
| 5512 | destination->Set(dest_position, '%'); |
| 5513 | destination->Set(dest_position+1, hex_chars[chr >> 4]); |
| 5514 | destination->Set(dest_position+2, hex_chars[chr & 0xf]); |
| 5515 | dest_position += 3; |
| 5516 | } |
| 5517 | } |
| 5518 | return destination; |
| 5519 | } |
| 5520 | |
| 5521 | |
| 5522 | static inline int TwoDigitHex(uint16_t character1, uint16_t character2) { |
| 5523 | static const signed char kHexValue['g'] = { |
| 5524 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5525 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5526 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5527 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, |
| 5528 | -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5529 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5530 | -1, 10, 11, 12, 13, 14, 15 }; |
| 5531 | |
| 5532 | if (character1 > 'f') return -1; |
| 5533 | int hi = kHexValue[character1]; |
| 5534 | if (hi == -1) return -1; |
| 5535 | if (character2 > 'f') return -1; |
| 5536 | int lo = kHexValue[character2]; |
| 5537 | if (lo == -1) return -1; |
| 5538 | return (hi << 4) + lo; |
| 5539 | } |
| 5540 | |
| 5541 | |
| 5542 | static inline int Unescape(String* source, |
| 5543 | int i, |
| 5544 | int length, |
| 5545 | int* step) { |
| 5546 | uint16_t character = source->Get(i); |
| 5547 | int32_t hi = 0; |
| 5548 | int32_t lo = 0; |
| 5549 | if (character == '%' && |
| 5550 | i <= length - 6 && |
| 5551 | source->Get(i + 1) == 'u' && |
| 5552 | (hi = TwoDigitHex(source->Get(i + 2), |
| 5553 | source->Get(i + 3))) != -1 && |
| 5554 | (lo = TwoDigitHex(source->Get(i + 4), |
| 5555 | source->Get(i + 5))) != -1) { |
| 5556 | *step = 6; |
| 5557 | return (hi << 8) + lo; |
| 5558 | } else if (character == '%' && |
| 5559 | i <= length - 3 && |
| 5560 | (lo = TwoDigitHex(source->Get(i + 1), |
| 5561 | source->Get(i + 2))) != -1) { |
| 5562 | *step = 3; |
| 5563 | return lo; |
| 5564 | } else { |
| 5565 | *step = 1; |
| 5566 | return character; |
| 5567 | } |
| 5568 | } |
| 5569 | |
| 5570 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5571 | RUNTIME_FUNCTION(MaybeObject*, Runtime_URIUnescape) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5572 | NoHandleAllocation ha; |
| 5573 | ASSERT(args.length() == 1); |
| 5574 | CONVERT_CHECKED(String, source, args[0]); |
| 5575 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5576 | source->TryFlatten(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5577 | |
| 5578 | bool ascii = true; |
| 5579 | int length = source->length(); |
| 5580 | |
| 5581 | int unescaped_length = 0; |
| 5582 | for (int i = 0; i < length; unescaped_length++) { |
| 5583 | int step; |
| 5584 | if (Unescape(source, i, length, &step) > String::kMaxAsciiCharCode) { |
| 5585 | ascii = false; |
| 5586 | } |
| 5587 | i += step; |
| 5588 | } |
| 5589 | |
| 5590 | // No length change implies no change. Return original string if no change. |
| 5591 | if (unescaped_length == length) |
| 5592 | return source; |
| 5593 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5594 | Object* o; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5595 | { MaybeObject* maybe_o = |
| 5596 | ascii ? |
| 5597 | isolate->heap()->AllocateRawAsciiString(unescaped_length) : |
| 5598 | isolate->heap()->AllocateRawTwoByteString(unescaped_length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5599 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 5600 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5601 | String* destination = String::cast(o); |
| 5602 | |
| 5603 | int dest_position = 0; |
| 5604 | for (int i = 0; i < length; dest_position++) { |
| 5605 | int step; |
| 5606 | destination->Set(dest_position, Unescape(source, i, length, &step)); |
| 5607 | i += step; |
| 5608 | } |
| 5609 | return destination; |
| 5610 | } |
| 5611 | |
| 5612 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5613 | static const unsigned int kQuoteTableLength = 128u; |
| 5614 | |
| 5615 | static const int kJsonQuotesCharactersPerEntry = 8; |
| 5616 | static const char* const JsonQuotes = |
| 5617 | "\\u0000 \\u0001 \\u0002 \\u0003 " |
| 5618 | "\\u0004 \\u0005 \\u0006 \\u0007 " |
| 5619 | "\\b \\t \\n \\u000b " |
| 5620 | "\\f \\r \\u000e \\u000f " |
| 5621 | "\\u0010 \\u0011 \\u0012 \\u0013 " |
| 5622 | "\\u0014 \\u0015 \\u0016 \\u0017 " |
| 5623 | "\\u0018 \\u0019 \\u001a \\u001b " |
| 5624 | "\\u001c \\u001d \\u001e \\u001f " |
| 5625 | " ! \\\" # " |
| 5626 | "$ % & ' " |
| 5627 | "( ) * + " |
| 5628 | ", - . / " |
| 5629 | "0 1 2 3 " |
| 5630 | "4 5 6 7 " |
| 5631 | "8 9 : ; " |
| 5632 | "< = > ? " |
| 5633 | "@ A B C " |
| 5634 | "D E F G " |
| 5635 | "H I J K " |
| 5636 | "L M N O " |
| 5637 | "P Q R S " |
| 5638 | "T U V W " |
| 5639 | "X Y Z [ " |
| 5640 | "\\\\ ] ^ _ " |
| 5641 | "` a b c " |
| 5642 | "d e f g " |
| 5643 | "h i j k " |
| 5644 | "l m n o " |
| 5645 | "p q r s " |
| 5646 | "t u v w " |
| 5647 | "x y z { " |
| 5648 | "| } ~ \177 "; |
| 5649 | |
| 5650 | |
| 5651 | // For a string that is less than 32k characters it should always be |
| 5652 | // possible to allocate it in new space. |
| 5653 | static const int kMaxGuaranteedNewSpaceString = 32 * 1024; |
| 5654 | |
| 5655 | |
| 5656 | // Doing JSON quoting cannot make the string more than this many times larger. |
| 5657 | static const int kJsonQuoteWorstCaseBlowup = 6; |
| 5658 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5659 | static const int kSpaceForQuotesAndComma = 3; |
| 5660 | static const int kSpaceForBrackets = 2; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5661 | |
| 5662 | // Covers the entire ASCII range (all other characters are unchanged by JSON |
| 5663 | // quoting). |
| 5664 | static const byte JsonQuoteLengths[kQuoteTableLength] = { |
| 5665 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5666 | 2, 2, 2, 6, 2, 2, 6, 6, |
| 5667 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5668 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5669 | 1, 1, 2, 1, 1, 1, 1, 1, |
| 5670 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5671 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5672 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5673 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5674 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5675 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5676 | 1, 1, 1, 1, 2, 1, 1, 1, |
| 5677 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5678 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5679 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5680 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5681 | }; |
| 5682 | |
| 5683 | |
| 5684 | template <typename StringType> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5685 | MaybeObject* AllocateRawString(Isolate* isolate, int length); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5686 | |
| 5687 | |
| 5688 | template <> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5689 | MaybeObject* AllocateRawString<SeqTwoByteString>(Isolate* isolate, int length) { |
| 5690 | return isolate->heap()->AllocateRawTwoByteString(length); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5691 | } |
| 5692 | |
| 5693 | |
| 5694 | template <> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5695 | MaybeObject* AllocateRawString<SeqAsciiString>(Isolate* isolate, int length) { |
| 5696 | return isolate->heap()->AllocateRawAsciiString(length); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5697 | } |
| 5698 | |
| 5699 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5700 | template <typename Char, typename StringType, bool comma> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5701 | static MaybeObject* SlowQuoteJsonString(Isolate* isolate, |
| 5702 | Vector<const Char> characters) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5703 | int length = characters.length(); |
| 5704 | const Char* read_cursor = characters.start(); |
| 5705 | const Char* end = read_cursor + length; |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5706 | const int kSpaceForQuotes = 2 + (comma ? 1 :0); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5707 | int quoted_length = kSpaceForQuotes; |
| 5708 | while (read_cursor < end) { |
| 5709 | Char c = *(read_cursor++); |
| 5710 | if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5711 | quoted_length++; |
| 5712 | } else { |
| 5713 | quoted_length += JsonQuoteLengths[static_cast<unsigned>(c)]; |
| 5714 | } |
| 5715 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5716 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5717 | quoted_length); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5718 | Object* new_object; |
| 5719 | if (!new_alloc->ToObject(&new_object)) { |
| 5720 | return new_alloc; |
| 5721 | } |
| 5722 | StringType* new_string = StringType::cast(new_object); |
| 5723 | |
| 5724 | Char* write_cursor = reinterpret_cast<Char*>( |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 5725 | new_string->address() + SeqString::kHeaderSize); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5726 | if (comma) *(write_cursor++) = ','; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5727 | *(write_cursor++) = '"'; |
| 5728 | |
| 5729 | read_cursor = characters.start(); |
| 5730 | while (read_cursor < end) { |
| 5731 | Char c = *(read_cursor++); |
| 5732 | if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5733 | *(write_cursor++) = c; |
| 5734 | } else { |
| 5735 | int len = JsonQuoteLengths[static_cast<unsigned>(c)]; |
| 5736 | const char* replacement = JsonQuotes + |
| 5737 | static_cast<unsigned>(c) * kJsonQuotesCharactersPerEntry; |
| 5738 | for (int i = 0; i < len; i++) { |
| 5739 | *write_cursor++ = *replacement++; |
| 5740 | } |
| 5741 | } |
| 5742 | } |
| 5743 | *(write_cursor++) = '"'; |
| 5744 | return new_string; |
| 5745 | } |
| 5746 | |
| 5747 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5748 | template <typename SinkChar, typename SourceChar> |
| 5749 | static inline SinkChar* WriteQuoteJsonString( |
| 5750 | Isolate* isolate, |
| 5751 | SinkChar* write_cursor, |
| 5752 | Vector<const SourceChar> characters) { |
| 5753 | // SinkChar is only char if SourceChar is guaranteed to be char. |
| 5754 | ASSERT(sizeof(SinkChar) >= sizeof(SourceChar)); |
| 5755 | const SourceChar* read_cursor = characters.start(); |
| 5756 | const SourceChar* end = read_cursor + characters.length(); |
| 5757 | *(write_cursor++) = '"'; |
| 5758 | while (read_cursor < end) { |
| 5759 | SourceChar c = *(read_cursor++); |
| 5760 | if (sizeof(SourceChar) > 1u && |
| 5761 | static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5762 | *(write_cursor++) = static_cast<SinkChar>(c); |
| 5763 | } else { |
| 5764 | int len = JsonQuoteLengths[static_cast<unsigned>(c)]; |
| 5765 | const char* replacement = JsonQuotes + |
| 5766 | static_cast<unsigned>(c) * kJsonQuotesCharactersPerEntry; |
| 5767 | write_cursor[0] = replacement[0]; |
| 5768 | if (len > 1) { |
| 5769 | write_cursor[1] = replacement[1]; |
| 5770 | if (len > 2) { |
| 5771 | ASSERT(len == 6); |
| 5772 | write_cursor[2] = replacement[2]; |
| 5773 | write_cursor[3] = replacement[3]; |
| 5774 | write_cursor[4] = replacement[4]; |
| 5775 | write_cursor[5] = replacement[5]; |
| 5776 | } |
| 5777 | } |
| 5778 | write_cursor += len; |
| 5779 | } |
| 5780 | } |
| 5781 | *(write_cursor++) = '"'; |
| 5782 | return write_cursor; |
| 5783 | } |
| 5784 | |
| 5785 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5786 | template <typename Char, typename StringType, bool comma> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5787 | static MaybeObject* QuoteJsonString(Isolate* isolate, |
| 5788 | Vector<const Char> characters) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5789 | int length = characters.length(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5790 | isolate->counters()->quote_json_char_count()->Increment(length); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5791 | int worst_case_length = |
| 5792 | length * kJsonQuoteWorstCaseBlowup + kSpaceForQuotesAndComma; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5793 | if (worst_case_length > kMaxGuaranteedNewSpaceString) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5794 | return SlowQuoteJsonString<Char, StringType, comma>(isolate, characters); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5795 | } |
| 5796 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5797 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5798 | worst_case_length); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5799 | Object* new_object; |
| 5800 | if (!new_alloc->ToObject(&new_object)) { |
| 5801 | return new_alloc; |
| 5802 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5803 | if (!isolate->heap()->new_space()->Contains(new_object)) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5804 | // Even if our string is small enough to fit in new space we still have to |
| 5805 | // handle it being allocated in old space as may happen in the third |
| 5806 | // attempt. See CALL_AND_RETRY in heap-inl.h and similar code in |
| 5807 | // CEntryStub::GenerateCore. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5808 | return SlowQuoteJsonString<Char, StringType, comma>(isolate, characters); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5809 | } |
| 5810 | StringType* new_string = StringType::cast(new_object); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5811 | ASSERT(isolate->heap()->new_space()->Contains(new_string)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5812 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5813 | Char* write_cursor = reinterpret_cast<Char*>( |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 5814 | new_string->address() + SeqString::kHeaderSize); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5815 | if (comma) *(write_cursor++) = ','; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5816 | write_cursor = WriteQuoteJsonString<Char, Char>(isolate, |
| 5817 | write_cursor, |
| 5818 | characters); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5819 | int final_length = static_cast<int>( |
| 5820 | write_cursor - reinterpret_cast<Char*>( |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 5821 | new_string->address() + SeqString::kHeaderSize)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5822 | isolate->heap()->new_space()-> |
| 5823 | template ShrinkStringAtAllocationBoundary<StringType>( |
| 5824 | new_string, final_length); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5825 | return new_string; |
| 5826 | } |
| 5827 | |
| 5828 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5829 | RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONString) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5830 | NoHandleAllocation ha; |
| 5831 | CONVERT_CHECKED(String, str, args[0]); |
| 5832 | if (!str->IsFlat()) { |
| 5833 | MaybeObject* try_flatten = str->TryFlatten(); |
| 5834 | Object* flat; |
| 5835 | if (!try_flatten->ToObject(&flat)) { |
| 5836 | return try_flatten; |
| 5837 | } |
| 5838 | str = String::cast(flat); |
| 5839 | ASSERT(str->IsFlat()); |
| 5840 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5841 | String::FlatContent flat = str->GetFlatContent(); |
| 5842 | ASSERT(flat.IsFlat()); |
| 5843 | if (flat.IsTwoByte()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5844 | return QuoteJsonString<uc16, SeqTwoByteString, false>(isolate, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5845 | flat.ToUC16Vector()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5846 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5847 | return QuoteJsonString<char, SeqAsciiString, false>(isolate, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5848 | flat.ToAsciiVector()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5849 | } |
| 5850 | } |
| 5851 | |
| 5852 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5853 | RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONStringComma) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5854 | NoHandleAllocation ha; |
| 5855 | CONVERT_CHECKED(String, str, args[0]); |
| 5856 | if (!str->IsFlat()) { |
| 5857 | MaybeObject* try_flatten = str->TryFlatten(); |
| 5858 | Object* flat; |
| 5859 | if (!try_flatten->ToObject(&flat)) { |
| 5860 | return try_flatten; |
| 5861 | } |
| 5862 | str = String::cast(flat); |
| 5863 | ASSERT(str->IsFlat()); |
| 5864 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5865 | String::FlatContent flat = str->GetFlatContent(); |
| 5866 | if (flat.IsTwoByte()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5867 | return QuoteJsonString<uc16, SeqTwoByteString, true>(isolate, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5868 | flat.ToUC16Vector()); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5869 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5870 | return QuoteJsonString<char, SeqAsciiString, true>(isolate, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5871 | flat.ToAsciiVector()); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5872 | } |
| 5873 | } |
| 5874 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5875 | |
| 5876 | template <typename Char, typename StringType> |
| 5877 | static MaybeObject* QuoteJsonStringArray(Isolate* isolate, |
| 5878 | FixedArray* array, |
| 5879 | int worst_case_length) { |
| 5880 | int length = array->length(); |
| 5881 | |
| 5882 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5883 | worst_case_length); |
| 5884 | Object* new_object; |
| 5885 | if (!new_alloc->ToObject(&new_object)) { |
| 5886 | return new_alloc; |
| 5887 | } |
| 5888 | if (!isolate->heap()->new_space()->Contains(new_object)) { |
| 5889 | // Even if our string is small enough to fit in new space we still have to |
| 5890 | // handle it being allocated in old space as may happen in the third |
| 5891 | // attempt. See CALL_AND_RETRY in heap-inl.h and similar code in |
| 5892 | // CEntryStub::GenerateCore. |
| 5893 | return isolate->heap()->undefined_value(); |
| 5894 | } |
| 5895 | AssertNoAllocation no_gc; |
| 5896 | StringType* new_string = StringType::cast(new_object); |
| 5897 | ASSERT(isolate->heap()->new_space()->Contains(new_string)); |
| 5898 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5899 | Char* write_cursor = reinterpret_cast<Char*>( |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 5900 | new_string->address() + SeqString::kHeaderSize); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5901 | *(write_cursor++) = '['; |
| 5902 | for (int i = 0; i < length; i++) { |
| 5903 | if (i != 0) *(write_cursor++) = ','; |
| 5904 | String* str = String::cast(array->get(i)); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5905 | String::FlatContent content = str->GetFlatContent(); |
| 5906 | ASSERT(content.IsFlat()); |
| 5907 | if (content.IsTwoByte()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5908 | write_cursor = WriteQuoteJsonString<Char, uc16>(isolate, |
| 5909 | write_cursor, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5910 | content.ToUC16Vector()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5911 | } else { |
| 5912 | write_cursor = WriteQuoteJsonString<Char, char>(isolate, |
| 5913 | write_cursor, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 5914 | content.ToAsciiVector()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5915 | } |
| 5916 | } |
| 5917 | *(write_cursor++) = ']'; |
| 5918 | |
| 5919 | int final_length = static_cast<int>( |
| 5920 | write_cursor - reinterpret_cast<Char*>( |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 5921 | new_string->address() + SeqString::kHeaderSize)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5922 | isolate->heap()->new_space()-> |
| 5923 | template ShrinkStringAtAllocationBoundary<StringType>( |
| 5924 | new_string, final_length); |
| 5925 | return new_string; |
| 5926 | } |
| 5927 | |
| 5928 | |
| 5929 | RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONStringArray) { |
| 5930 | NoHandleAllocation ha; |
| 5931 | ASSERT(args.length() == 1); |
| 5932 | CONVERT_CHECKED(JSArray, array, args[0]); |
| 5933 | |
| 5934 | if (!array->HasFastElements()) return isolate->heap()->undefined_value(); |
| 5935 | FixedArray* elements = FixedArray::cast(array->elements()); |
| 5936 | int n = elements->length(); |
| 5937 | bool ascii = true; |
| 5938 | int total_length = 0; |
| 5939 | |
| 5940 | for (int i = 0; i < n; i++) { |
| 5941 | Object* elt = elements->get(i); |
| 5942 | if (!elt->IsString()) return isolate->heap()->undefined_value(); |
| 5943 | String* element = String::cast(elt); |
| 5944 | if (!element->IsFlat()) return isolate->heap()->undefined_value(); |
| 5945 | total_length += element->length(); |
| 5946 | if (ascii && element->IsTwoByteRepresentation()) { |
| 5947 | ascii = false; |
| 5948 | } |
| 5949 | } |
| 5950 | |
| 5951 | int worst_case_length = |
| 5952 | kSpaceForBrackets + n * kSpaceForQuotesAndComma |
| 5953 | + total_length * kJsonQuoteWorstCaseBlowup; |
| 5954 | |
| 5955 | if (worst_case_length > kMaxGuaranteedNewSpaceString) { |
| 5956 | return isolate->heap()->undefined_value(); |
| 5957 | } |
| 5958 | |
| 5959 | if (ascii) { |
| 5960 | return QuoteJsonStringArray<char, SeqAsciiString>(isolate, |
| 5961 | elements, |
| 5962 | worst_case_length); |
| 5963 | } else { |
| 5964 | return QuoteJsonStringArray<uc16, SeqTwoByteString>(isolate, |
| 5965 | elements, |
| 5966 | worst_case_length); |
| 5967 | } |
| 5968 | } |
| 5969 | |
| 5970 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5971 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringParseInt) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5972 | NoHandleAllocation ha; |
| 5973 | |
| 5974 | CONVERT_CHECKED(String, s, args[0]); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5975 | CONVERT_SMI_ARG_CHECKED(radix, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5976 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5977 | s->TryFlatten(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5978 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5979 | RUNTIME_ASSERT(radix == 0 || (2 <= radix && radix <= 36)); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5980 | double value = StringToInt(isolate->unicode_cache(), s, radix); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5981 | return isolate->heap()->NumberFromDouble(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5982 | } |
| 5983 | |
| 5984 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5985 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringParseFloat) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5986 | NoHandleAllocation ha; |
| 5987 | CONVERT_CHECKED(String, str, args[0]); |
| 5988 | |
| 5989 | // ECMA-262 section 15.1.2.3, empty string is NaN |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5990 | double value = StringToDouble(isolate->unicode_cache(), |
| 5991 | str, ALLOW_TRAILING_JUNK, OS::nan_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5992 | |
| 5993 | // Create a number object from the value. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5994 | return isolate->heap()->NumberFromDouble(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5995 | } |
| 5996 | |
| 5997 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5998 | template <class Converter> |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5999 | MUST_USE_RESULT static MaybeObject* ConvertCaseHelper( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6000 | Isolate* isolate, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6001 | String* s, |
| 6002 | int length, |
| 6003 | int input_string_length, |
| 6004 | unibrow::Mapping<Converter, 128>* mapping) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6005 | // We try this twice, once with the assumption that the result is no longer |
| 6006 | // than the input and, if that assumption breaks, again with the exact |
| 6007 | // length. This may not be pretty, but it is nicer than what was here before |
| 6008 | // and I hereby claim my vaffel-is. |
| 6009 | // |
| 6010 | // Allocate the resulting string. |
| 6011 | // |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 6012 | // NOTE: This assumes that the upper/lower case of an ASCII |
| 6013 | // character is also ASCII. This is currently the case, but it |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6014 | // might break in the future if we implement more context and locale |
| 6015 | // dependent upper/lower conversions. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6016 | Object* o; |
| 6017 | { MaybeObject* maybe_o = s->IsAsciiRepresentation() |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6018 | ? isolate->heap()->AllocateRawAsciiString(length) |
| 6019 | : isolate->heap()->AllocateRawTwoByteString(length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6020 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 6021 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6022 | String* result = String::cast(o); |
| 6023 | bool has_changed_character = false; |
| 6024 | |
| 6025 | // Convert all characters to upper case, assuming that they will fit |
| 6026 | // in the buffer |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6027 | Access<StringInputBuffer> buffer( |
| 6028 | isolate->runtime_state()->string_input_buffer()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6029 | buffer->Reset(s); |
| 6030 | unibrow::uchar chars[Converter::kMaxWidth]; |
| 6031 | // We can assume that the string is not empty |
| 6032 | uc32 current = buffer->GetNext(); |
| 6033 | for (int i = 0; i < length;) { |
| 6034 | bool has_next = buffer->has_more(); |
| 6035 | uc32 next = has_next ? buffer->GetNext() : 0; |
| 6036 | int char_length = mapping->get(current, next, chars); |
| 6037 | if (char_length == 0) { |
| 6038 | // The case conversion of this character is the character itself. |
| 6039 | result->Set(i, current); |
| 6040 | i++; |
| 6041 | } else if (char_length == 1) { |
| 6042 | // Common case: converting the letter resulted in one character. |
| 6043 | ASSERT(static_cast<uc32>(chars[0]) != current); |
| 6044 | result->Set(i, chars[0]); |
| 6045 | has_changed_character = true; |
| 6046 | i++; |
| 6047 | } else if (length == input_string_length) { |
| 6048 | // We've assumed that the result would be as long as the |
| 6049 | // input but here is a character that converts to several |
| 6050 | // characters. No matter, we calculate the exact length |
| 6051 | // of the result and try the whole thing again. |
| 6052 | // |
| 6053 | // Note that this leaves room for optimization. We could just |
| 6054 | // memcpy what we already have to the result string. Also, |
| 6055 | // the result string is the last object allocated we could |
| 6056 | // "realloc" it and probably, in the vast majority of cases, |
| 6057 | // extend the existing string to be able to hold the full |
| 6058 | // result. |
| 6059 | int next_length = 0; |
| 6060 | if (has_next) { |
| 6061 | next_length = mapping->get(next, 0, chars); |
| 6062 | if (next_length == 0) next_length = 1; |
| 6063 | } |
| 6064 | int current_length = i + char_length + next_length; |
| 6065 | while (buffer->has_more()) { |
| 6066 | current = buffer->GetNext(); |
| 6067 | // NOTE: we use 0 as the next character here because, while |
| 6068 | // the next character may affect what a character converts to, |
| 6069 | // it does not in any case affect the length of what it convert |
| 6070 | // to. |
| 6071 | int char_length = mapping->get(current, 0, chars); |
| 6072 | if (char_length == 0) char_length = 1; |
| 6073 | current_length += char_length; |
| 6074 | if (current_length > Smi::kMaxValue) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6075 | isolate->context()->mark_out_of_memory(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6076 | return Failure::OutOfMemoryException(); |
| 6077 | } |
| 6078 | } |
| 6079 | // Try again with the real length. |
| 6080 | return Smi::FromInt(current_length); |
| 6081 | } else { |
| 6082 | for (int j = 0; j < char_length; j++) { |
| 6083 | result->Set(i, chars[j]); |
| 6084 | i++; |
| 6085 | } |
| 6086 | has_changed_character = true; |
| 6087 | } |
| 6088 | current = next; |
| 6089 | } |
| 6090 | if (has_changed_character) { |
| 6091 | return result; |
| 6092 | } else { |
| 6093 | // If we didn't actually change anything in doing the conversion |
| 6094 | // we simple return the result and let the converted string |
| 6095 | // become garbage; there is no reason to keep two identical strings |
| 6096 | // alive. |
| 6097 | return s; |
| 6098 | } |
| 6099 | } |
| 6100 | |
| 6101 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6102 | namespace { |
| 6103 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6104 | static const uintptr_t kOneInEveryByte = kUintptrAllBitsSet / 0xFF; |
| 6105 | |
| 6106 | |
| 6107 | // Given a word and two range boundaries returns a word with high bit |
| 6108 | // set in every byte iff the corresponding input byte was strictly in |
| 6109 | // the range (m, n). All the other bits in the result are cleared. |
| 6110 | // This function is only useful when it can be inlined and the |
| 6111 | // boundaries are statically known. |
| 6112 | // Requires: all bytes in the input word and the boundaries must be |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 6113 | // ASCII (less than 0x7F). |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6114 | static inline uintptr_t AsciiRangeMask(uintptr_t w, char m, char n) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 6115 | // 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] | 6116 | ASSERT((w & (kOneInEveryByte * 0x7F)) == w); |
| 6117 | // Use strict inequalities since in edge cases the function could be |
| 6118 | // further simplified. |
| 6119 | ASSERT(0 < m && m < n && n < 0x7F); |
| 6120 | // Has high bit set in every w byte less than n. |
| 6121 | uintptr_t tmp1 = kOneInEveryByte * (0x7F + n) - w; |
| 6122 | // Has high bit set in every w byte greater than m. |
| 6123 | uintptr_t tmp2 = w + kOneInEveryByte * (0x7F - m); |
| 6124 | return (tmp1 & tmp2 & (kOneInEveryByte * 0x80)); |
| 6125 | } |
| 6126 | |
| 6127 | |
| 6128 | enum AsciiCaseConversion { |
| 6129 | ASCII_TO_LOWER, |
| 6130 | ASCII_TO_UPPER |
| 6131 | }; |
| 6132 | |
| 6133 | |
| 6134 | template <AsciiCaseConversion dir> |
| 6135 | struct FastAsciiConverter { |
| 6136 | static bool Convert(char* dst, char* src, int length) { |
| 6137 | #ifdef DEBUG |
| 6138 | char* saved_dst = dst; |
| 6139 | char* saved_src = src; |
| 6140 | #endif |
| 6141 | // We rely on the distance between upper and lower case letters |
| 6142 | // being a known power of 2. |
| 6143 | ASSERT('a' - 'A' == (1 << 5)); |
| 6144 | // Boundaries for the range of input characters than require conversion. |
| 6145 | const char lo = (dir == ASCII_TO_LOWER) ? 'A' - 1 : 'a' - 1; |
| 6146 | const char hi = (dir == ASCII_TO_LOWER) ? 'Z' + 1 : 'z' + 1; |
| 6147 | bool changed = false; |
| 6148 | char* const limit = src + length; |
| 6149 | #ifdef V8_HOST_CAN_READ_UNALIGNED |
| 6150 | // Process the prefix of the input that requires no conversion one |
| 6151 | // (machine) word at a time. |
| 6152 | while (src <= limit - sizeof(uintptr_t)) { |
| 6153 | uintptr_t w = *reinterpret_cast<uintptr_t*>(src); |
| 6154 | if (AsciiRangeMask(w, lo, hi) != 0) { |
| 6155 | changed = true; |
| 6156 | break; |
| 6157 | } |
| 6158 | *reinterpret_cast<uintptr_t*>(dst) = w; |
| 6159 | src += sizeof(uintptr_t); |
| 6160 | dst += sizeof(uintptr_t); |
| 6161 | } |
| 6162 | // Process the remainder of the input performing conversion when |
| 6163 | // required one word at a time. |
| 6164 | while (src <= limit - sizeof(uintptr_t)) { |
| 6165 | uintptr_t w = *reinterpret_cast<uintptr_t*>(src); |
| 6166 | uintptr_t m = AsciiRangeMask(w, lo, hi); |
| 6167 | // The mask has high (7th) bit set in every byte that needs |
| 6168 | // conversion and we know that the distance between cases is |
| 6169 | // 1 << 5. |
| 6170 | *reinterpret_cast<uintptr_t*>(dst) = w ^ (m >> 2); |
| 6171 | src += sizeof(uintptr_t); |
| 6172 | dst += sizeof(uintptr_t); |
| 6173 | } |
| 6174 | #endif |
| 6175 | // Process the last few bytes of the input (or the whole input if |
| 6176 | // unaligned access is not supported). |
| 6177 | while (src < limit) { |
| 6178 | char c = *src; |
| 6179 | if (lo < c && c < hi) { |
| 6180 | c ^= (1 << 5); |
| 6181 | changed = true; |
| 6182 | } |
| 6183 | *dst = c; |
| 6184 | ++src; |
| 6185 | ++dst; |
| 6186 | } |
| 6187 | #ifdef DEBUG |
| 6188 | CheckConvert(saved_dst, saved_src, length, changed); |
| 6189 | #endif |
| 6190 | return changed; |
| 6191 | } |
| 6192 | |
| 6193 | #ifdef DEBUG |
| 6194 | static void CheckConvert(char* dst, char* src, int length, bool changed) { |
| 6195 | bool expected_changed = false; |
| 6196 | for (int i = 0; i < length; i++) { |
| 6197 | if (dst[i] == src[i]) continue; |
| 6198 | expected_changed = true; |
| 6199 | if (dir == ASCII_TO_LOWER) { |
| 6200 | ASSERT('A' <= src[i] && src[i] <= 'Z'); |
| 6201 | ASSERT(dst[i] == src[i] + ('a' - 'A')); |
| 6202 | } else { |
| 6203 | ASSERT(dir == ASCII_TO_UPPER); |
| 6204 | ASSERT('a' <= src[i] && src[i] <= 'z'); |
| 6205 | ASSERT(dst[i] == src[i] - ('a' - 'A')); |
| 6206 | } |
| 6207 | } |
| 6208 | ASSERT(expected_changed == changed); |
| 6209 | } |
| 6210 | #endif |
| 6211 | }; |
| 6212 | |
| 6213 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6214 | struct ToLowerTraits { |
| 6215 | typedef unibrow::ToLowercase UnibrowConverter; |
| 6216 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6217 | typedef FastAsciiConverter<ASCII_TO_LOWER> AsciiConverter; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6218 | }; |
| 6219 | |
| 6220 | |
| 6221 | struct ToUpperTraits { |
| 6222 | typedef unibrow::ToUppercase UnibrowConverter; |
| 6223 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6224 | typedef FastAsciiConverter<ASCII_TO_UPPER> AsciiConverter; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6225 | }; |
| 6226 | |
| 6227 | } // namespace |
| 6228 | |
| 6229 | |
| 6230 | template <typename ConvertTraits> |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6231 | MUST_USE_RESULT static MaybeObject* ConvertCase( |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6232 | Arguments args, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6233 | Isolate* isolate, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6234 | unibrow::Mapping<typename ConvertTraits::UnibrowConverter, 128>* mapping) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6235 | NoHandleAllocation ha; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6236 | CONVERT_CHECKED(String, s, args[0]); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 6237 | s = s->TryFlattenGetString(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6238 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6239 | const int length = s->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6240 | // Assume that the string is not empty; we need this assumption later |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6241 | if (length == 0) return s; |
| 6242 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 6243 | // Simpler handling of ASCII strings. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6244 | // |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 6245 | // NOTE: This assumes that the upper/lower case of an ASCII |
| 6246 | // character is also ASCII. This is currently the case, but it |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6247 | // might break in the future if we implement more context and locale |
| 6248 | // dependent upper/lower conversions. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 6249 | if (s->IsSeqAsciiString()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6250 | Object* o; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6251 | { MaybeObject* maybe_o = isolate->heap()->AllocateRawAsciiString(length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6252 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 6253 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6254 | SeqAsciiString* result = SeqAsciiString::cast(o); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6255 | bool has_changed_character = ConvertTraits::AsciiConverter::Convert( |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 6256 | result->GetChars(), SeqAsciiString::cast(s)->GetChars(), length); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6257 | return has_changed_character ? result : s; |
| 6258 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6259 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6260 | Object* answer; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6261 | { MaybeObject* maybe_answer = |
| 6262 | ConvertCaseHelper(isolate, s, length, length, mapping); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6263 | if (!maybe_answer->ToObject(&answer)) return maybe_answer; |
| 6264 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6265 | if (answer->IsSmi()) { |
| 6266 | // Retry with correct length. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6267 | { MaybeObject* maybe_answer = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6268 | ConvertCaseHelper(isolate, |
| 6269 | s, Smi::cast(answer)->value(), length, mapping); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6270 | if (!maybe_answer->ToObject(&answer)) return maybe_answer; |
| 6271 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6272 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6273 | return answer; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6274 | } |
| 6275 | |
| 6276 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6277 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToLowerCase) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6278 | return ConvertCase<ToLowerTraits>( |
| 6279 | args, isolate, isolate->runtime_state()->to_lower_mapping()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6280 | } |
| 6281 | |
| 6282 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6283 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToUpperCase) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6284 | return ConvertCase<ToUpperTraits>( |
| 6285 | args, isolate, isolate->runtime_state()->to_upper_mapping()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6286 | } |
| 6287 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6288 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6289 | static inline bool IsTrimWhiteSpace(unibrow::uchar c) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 6290 | return unibrow::WhiteSpace::Is(c) || c == 0x200b || c == 0xfeff; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6291 | } |
| 6292 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6293 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6294 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringTrim) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6295 | NoHandleAllocation ha; |
| 6296 | ASSERT(args.length() == 3); |
| 6297 | |
| 6298 | CONVERT_CHECKED(String, s, args[0]); |
| 6299 | CONVERT_BOOLEAN_CHECKED(trimLeft, args[1]); |
| 6300 | CONVERT_BOOLEAN_CHECKED(trimRight, args[2]); |
| 6301 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6302 | s->TryFlatten(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6303 | int length = s->length(); |
| 6304 | |
| 6305 | int left = 0; |
| 6306 | if (trimLeft) { |
| 6307 | while (left < length && IsTrimWhiteSpace(s->Get(left))) { |
| 6308 | left++; |
| 6309 | } |
| 6310 | } |
| 6311 | |
| 6312 | int right = length; |
| 6313 | if (trimRight) { |
| 6314 | while (right > left && IsTrimWhiteSpace(s->Get(right - 1))) { |
| 6315 | right--; |
| 6316 | } |
| 6317 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6318 | return s->SubString(left, right); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6319 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6320 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6321 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6322 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringSplit) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6323 | ASSERT(args.length() == 3); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6324 | HandleScope handle_scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6325 | CONVERT_ARG_CHECKED(String, subject, 0); |
| 6326 | CONVERT_ARG_CHECKED(String, pattern, 1); |
| 6327 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[2]); |
| 6328 | |
| 6329 | int subject_length = subject->length(); |
| 6330 | int pattern_length = pattern->length(); |
| 6331 | RUNTIME_ASSERT(pattern_length > 0); |
| 6332 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 6333 | if (limit == 0xffffffffu) { |
| 6334 | Handle<Object> cached_answer(StringSplitCache::Lookup( |
| 6335 | isolate->heap()->string_split_cache(), |
| 6336 | *subject, |
| 6337 | *pattern)); |
| 6338 | if (*cached_answer != Smi::FromInt(0)) { |
| 6339 | Handle<JSArray> result = |
| 6340 | isolate->factory()->NewJSArrayWithElements( |
| 6341 | Handle<FixedArray>::cast(cached_answer)); |
| 6342 | return *result; |
| 6343 | } |
| 6344 | } |
| 6345 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6346 | // The limit can be very large (0xffffffffu), but since the pattern |
| 6347 | // isn't empty, we can never create more parts than ~half the length |
| 6348 | // of the subject. |
| 6349 | |
| 6350 | if (!subject->IsFlat()) FlattenString(subject); |
| 6351 | |
| 6352 | static const int kMaxInitialListCapacity = 16; |
| 6353 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6354 | ZoneScope scope(isolate, DELETE_ON_EXIT); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6355 | |
| 6356 | // Find (up to limit) indices of separator and end-of-string in subject |
| 6357 | int initial_capacity = Min<uint32_t>(kMaxInitialListCapacity, limit); |
| 6358 | ZoneList<int> indices(initial_capacity); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 6359 | if (!pattern->IsFlat()) FlattenString(pattern); |
| 6360 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 6361 | FindStringIndicesDispatch(isolate, *subject, *pattern, &indices, limit); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 6362 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6363 | if (static_cast<uint32_t>(indices.length()) < limit) { |
| 6364 | indices.Add(subject_length); |
| 6365 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6366 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 6367 | // The list indices now contains the end of each part to create. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6368 | |
| 6369 | // Create JSArray of substrings separated by separator. |
| 6370 | int part_count = indices.length(); |
| 6371 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6372 | Handle<JSArray> result = isolate->factory()->NewJSArray(part_count); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 6373 | MaybeObject* maybe_result = result->EnsureCanContainHeapObjectElements(); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 6374 | if (maybe_result->IsFailure()) return maybe_result; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6375 | result->set_length(Smi::FromInt(part_count)); |
| 6376 | |
| 6377 | ASSERT(result->HasFastElements()); |
| 6378 | |
| 6379 | if (part_count == 1 && indices.at(0) == subject_length) { |
| 6380 | FixedArray::cast(result->elements())->set(0, *subject); |
| 6381 | return *result; |
| 6382 | } |
| 6383 | |
| 6384 | Handle<FixedArray> elements(FixedArray::cast(result->elements())); |
| 6385 | int part_start = 0; |
| 6386 | for (int i = 0; i < part_count; i++) { |
| 6387 | HandleScope local_loop_handle; |
| 6388 | int part_end = indices.at(i); |
| 6389 | Handle<String> substring = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6390 | isolate->factory()->NewProperSubString(subject, part_start, part_end); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6391 | elements->set(i, *substring); |
| 6392 | part_start = part_end + pattern_length; |
| 6393 | } |
| 6394 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 6395 | if (limit == 0xffffffffu) { |
| 6396 | if (result->HasFastElements()) { |
| 6397 | StringSplitCache::Enter(isolate->heap(), |
| 6398 | isolate->heap()->string_split_cache(), |
| 6399 | *subject, |
| 6400 | *pattern, |
| 6401 | *elements); |
| 6402 | } |
| 6403 | } |
| 6404 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6405 | return *result; |
| 6406 | } |
| 6407 | |
| 6408 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 6409 | // Copies ASCII characters to the given fixed array looking up |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6410 | // one-char strings in the cache. Gives up on the first char that is |
| 6411 | // not in the cache and fills the remainder with smi zeros. Returns |
| 6412 | // the length of the successfully copied prefix. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6413 | static int CopyCachedAsciiCharsToArray(Heap* heap, |
| 6414 | const char* chars, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6415 | FixedArray* elements, |
| 6416 | int length) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 6417 | AssertNoAllocation no_gc; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6418 | FixedArray* ascii_cache = heap->single_character_string_cache(); |
| 6419 | Object* undefined = heap->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6420 | int i; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 6421 | WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6422 | for (i = 0; i < length; ++i) { |
| 6423 | Object* value = ascii_cache->get(chars[i]); |
| 6424 | if (value == undefined) break; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 6425 | elements->set(i, value, mode); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6426 | } |
| 6427 | if (i < length) { |
| 6428 | ASSERT(Smi::FromInt(0) == 0); |
| 6429 | memset(elements->data_start() + i, 0, kPointerSize * (length - i)); |
| 6430 | } |
| 6431 | #ifdef DEBUG |
| 6432 | for (int j = 0; j < length; ++j) { |
| 6433 | Object* element = elements->get(j); |
| 6434 | ASSERT(element == Smi::FromInt(0) || |
| 6435 | (element->IsString() && String::cast(element)->LooksValid())); |
| 6436 | } |
| 6437 | #endif |
| 6438 | return i; |
| 6439 | } |
| 6440 | |
| 6441 | |
| 6442 | // Converts a String to JSArray. |
| 6443 | // For example, "foo" => ["f", "o", "o"]. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6444 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToArray) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6445 | HandleScope scope(isolate); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 6446 | ASSERT(args.length() == 2); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6447 | CONVERT_ARG_CHECKED(String, s, 0); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 6448 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6449 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 6450 | s = FlattenGetString(s); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 6451 | const int length = static_cast<int>(Min<uint32_t>(s->length(), limit)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6452 | |
| 6453 | Handle<FixedArray> elements; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 6454 | int position = 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6455 | if (s->IsFlat() && s->IsAsciiRepresentation()) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 6456 | // Try using cached chars where possible. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6457 | Object* obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6458 | { MaybeObject* maybe_obj = |
| 6459 | isolate->heap()->AllocateUninitializedFixedArray(length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6460 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 6461 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6462 | elements = Handle<FixedArray>(FixedArray::cast(obj), isolate); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 6463 | String::FlatContent content = s->GetFlatContent(); |
| 6464 | if (content.IsAscii()) { |
| 6465 | Vector<const char> chars = content.ToAsciiVector(); |
| 6466 | // Note, this will initialize all elements (not only the prefix) |
| 6467 | // to prevent GC from seeing partially initialized array. |
| 6468 | position = CopyCachedAsciiCharsToArray(isolate->heap(), |
| 6469 | chars.start(), |
| 6470 | *elements, |
| 6471 | length); |
| 6472 | } else { |
| 6473 | MemsetPointer(elements->data_start(), |
| 6474 | isolate->heap()->undefined_value(), |
| 6475 | length); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6476 | } |
| 6477 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6478 | elements = isolate->factory()->NewFixedArray(length); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 6479 | } |
| 6480 | for (int i = position; i < length; ++i) { |
| 6481 | Handle<Object> str = LookupSingleCharacterStringFromCode(s->Get(i)); |
| 6482 | elements->set(i, *str); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6483 | } |
| 6484 | |
| 6485 | #ifdef DEBUG |
| 6486 | for (int i = 0; i < length; ++i) { |
| 6487 | ASSERT(String::cast(elements->get(i))->length() == 1); |
| 6488 | } |
| 6489 | #endif |
| 6490 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6491 | return *isolate->factory()->NewJSArrayWithElements(elements); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6492 | } |
| 6493 | |
| 6494 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6495 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewStringWrapper) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 6496 | NoHandleAllocation ha; |
| 6497 | ASSERT(args.length() == 1); |
| 6498 | CONVERT_CHECKED(String, value, args[0]); |
| 6499 | return value->ToObject(); |
| 6500 | } |
| 6501 | |
| 6502 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6503 | bool Runtime::IsUpperCaseChar(RuntimeState* runtime_state, uint16_t ch) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6504 | unibrow::uchar chars[unibrow::ToUppercase::kMaxWidth]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6505 | int char_length = runtime_state->to_upper_mapping()->get(ch, 0, chars); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6506 | return char_length == 0; |
| 6507 | } |
| 6508 | |
| 6509 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6510 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToString) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6511 | NoHandleAllocation ha; |
| 6512 | ASSERT(args.length() == 1); |
| 6513 | |
| 6514 | Object* number = args[0]; |
| 6515 | RUNTIME_ASSERT(number->IsNumber()); |
| 6516 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6517 | return isolate->heap()->NumberToString(number); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6518 | } |
| 6519 | |
| 6520 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6521 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToStringSkipCache) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6522 | NoHandleAllocation ha; |
| 6523 | ASSERT(args.length() == 1); |
| 6524 | |
| 6525 | Object* number = args[0]; |
| 6526 | RUNTIME_ASSERT(number->IsNumber()); |
| 6527 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6528 | return isolate->heap()->NumberToString(number, false); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6529 | } |
| 6530 | |
| 6531 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6532 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToInteger) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6533 | NoHandleAllocation ha; |
| 6534 | ASSERT(args.length() == 1); |
| 6535 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6536 | CONVERT_DOUBLE_ARG_CHECKED(number, 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6537 | |
| 6538 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 6539 | if (number > 0 && number <= Smi::kMaxValue) { |
| 6540 | return Smi::FromInt(static_cast<int>(number)); |
| 6541 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6542 | return isolate->heap()->NumberFromDouble(DoubleToInteger(number)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6543 | } |
| 6544 | |
| 6545 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6546 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToIntegerMapMinusZero) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6547 | NoHandleAllocation ha; |
| 6548 | ASSERT(args.length() == 1); |
| 6549 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6550 | CONVERT_DOUBLE_ARG_CHECKED(number, 0); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6551 | |
| 6552 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 6553 | if (number > 0 && number <= Smi::kMaxValue) { |
| 6554 | return Smi::FromInt(static_cast<int>(number)); |
| 6555 | } |
| 6556 | |
| 6557 | double double_value = DoubleToInteger(number); |
| 6558 | // Map both -0 and +0 to +0. |
| 6559 | if (double_value == 0) double_value = 0; |
| 6560 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6561 | return isolate->heap()->NumberFromDouble(double_value); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6562 | } |
| 6563 | |
| 6564 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6565 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToJSUint32) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6566 | NoHandleAllocation ha; |
| 6567 | ASSERT(args.length() == 1); |
| 6568 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6569 | CONVERT_NUMBER_CHECKED(int32_t, number, Uint32, args[0]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6570 | return isolate->heap()->NumberFromUint32(number); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6571 | } |
| 6572 | |
| 6573 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6574 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToJSInt32) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6575 | NoHandleAllocation ha; |
| 6576 | ASSERT(args.length() == 1); |
| 6577 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6578 | CONVERT_DOUBLE_ARG_CHECKED(number, 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6579 | |
| 6580 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 6581 | if (number > 0 && number <= Smi::kMaxValue) { |
| 6582 | return Smi::FromInt(static_cast<int>(number)); |
| 6583 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6584 | return isolate->heap()->NumberFromInt32(DoubleToInt32(number)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6585 | } |
| 6586 | |
| 6587 | |
| 6588 | // Converts a Number to a Smi, if possible. Returns NaN if the number is not |
| 6589 | // a small integer. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6590 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToSmi) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6591 | NoHandleAllocation ha; |
| 6592 | ASSERT(args.length() == 1); |
| 6593 | |
| 6594 | Object* obj = args[0]; |
| 6595 | if (obj->IsSmi()) { |
| 6596 | return obj; |
| 6597 | } |
| 6598 | if (obj->IsHeapNumber()) { |
| 6599 | double value = HeapNumber::cast(obj)->value(); |
| 6600 | int int_value = FastD2I(value); |
| 6601 | if (value == FastI2D(int_value) && Smi::IsValid(int_value)) { |
| 6602 | return Smi::FromInt(int_value); |
| 6603 | } |
| 6604 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6605 | return isolate->heap()->nan_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6606 | } |
| 6607 | |
| 6608 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6609 | RUNTIME_FUNCTION(MaybeObject*, Runtime_AllocateHeapNumber) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6610 | NoHandleAllocation ha; |
| 6611 | ASSERT(args.length() == 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6612 | return isolate->heap()->AllocateHeapNumber(0); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6613 | } |
| 6614 | |
| 6615 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6616 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberAdd) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6617 | NoHandleAllocation ha; |
| 6618 | ASSERT(args.length() == 2); |
| 6619 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6620 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6621 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6622 | return isolate->heap()->NumberFromDouble(x + y); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6623 | } |
| 6624 | |
| 6625 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6626 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberSub) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6627 | NoHandleAllocation ha; |
| 6628 | ASSERT(args.length() == 2); |
| 6629 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6630 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6631 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6632 | return isolate->heap()->NumberFromDouble(x - y); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6633 | } |
| 6634 | |
| 6635 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6636 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberMul) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6637 | NoHandleAllocation ha; |
| 6638 | ASSERT(args.length() == 2); |
| 6639 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6640 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6641 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6642 | return isolate->heap()->NumberFromDouble(x * y); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6643 | } |
| 6644 | |
| 6645 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6646 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberUnaryMinus) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6647 | NoHandleAllocation ha; |
| 6648 | ASSERT(args.length() == 1); |
| 6649 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6650 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6651 | return isolate->heap()->NumberFromDouble(-x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6652 | } |
| 6653 | |
| 6654 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6655 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberAlloc) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 6656 | NoHandleAllocation ha; |
| 6657 | ASSERT(args.length() == 0); |
| 6658 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6659 | return isolate->heap()->NumberFromDouble(9876543210.0); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 6660 | } |
| 6661 | |
| 6662 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6663 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberDiv) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6664 | NoHandleAllocation ha; |
| 6665 | ASSERT(args.length() == 2); |
| 6666 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6667 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6668 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6669 | return isolate->heap()->NumberFromDouble(x / y); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6670 | } |
| 6671 | |
| 6672 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6673 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberMod) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6674 | NoHandleAllocation ha; |
| 6675 | ASSERT(args.length() == 2); |
| 6676 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6677 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6678 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6679 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6680 | x = modulo(x, y); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6681 | // NumberFromDouble may return a Smi instead of a Number object |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6682 | return isolate->heap()->NumberFromDouble(x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6683 | } |
| 6684 | |
| 6685 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6686 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringAdd) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6687 | NoHandleAllocation ha; |
| 6688 | ASSERT(args.length() == 2); |
| 6689 | CONVERT_CHECKED(String, str1, args[0]); |
| 6690 | CONVERT_CHECKED(String, str2, args[1]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6691 | isolate->counters()->string_add_runtime()->Increment(); |
| 6692 | return isolate->heap()->AllocateConsString(str1, str2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6693 | } |
| 6694 | |
| 6695 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6696 | template <typename sinkchar> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6697 | static inline void StringBuilderConcatHelper(String* special, |
| 6698 | sinkchar* sink, |
| 6699 | FixedArray* fixed_array, |
| 6700 | int array_length) { |
| 6701 | int position = 0; |
| 6702 | for (int i = 0; i < array_length; i++) { |
| 6703 | Object* element = fixed_array->get(i); |
| 6704 | if (element->IsSmi()) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6705 | // Smi encoding of position and length. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6706 | int encoded_slice = Smi::cast(element)->value(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6707 | int pos; |
| 6708 | int len; |
| 6709 | if (encoded_slice > 0) { |
| 6710 | // Position and length encoded in one smi. |
| 6711 | pos = StringBuilderSubstringPosition::decode(encoded_slice); |
| 6712 | len = StringBuilderSubstringLength::decode(encoded_slice); |
| 6713 | } else { |
| 6714 | // Position and length encoded in two smis. |
| 6715 | Object* obj = fixed_array->get(++i); |
| 6716 | ASSERT(obj->IsSmi()); |
| 6717 | pos = Smi::cast(obj)->value(); |
| 6718 | len = -encoded_slice; |
| 6719 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6720 | String::WriteToFlat(special, |
| 6721 | sink + position, |
| 6722 | pos, |
| 6723 | pos + len); |
| 6724 | position += len; |
| 6725 | } else { |
| 6726 | String* string = String::cast(element); |
| 6727 | int element_length = string->length(); |
| 6728 | String::WriteToFlat(string, sink + position, 0, element_length); |
| 6729 | position += element_length; |
| 6730 | } |
| 6731 | } |
| 6732 | } |
| 6733 | |
| 6734 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6735 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringBuilderConcat) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6736 | NoHandleAllocation ha; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6737 | ASSERT(args.length() == 3); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6738 | CONVERT_CHECKED(JSArray, array, args[0]); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6739 | if (!args[1]->IsSmi()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6740 | isolate->context()->mark_out_of_memory(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6741 | return Failure::OutOfMemoryException(); |
| 6742 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6743 | int array_length = args.smi_at(1); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6744 | CONVERT_CHECKED(String, special, args[2]); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6745 | |
| 6746 | // This assumption is used by the slice encoding in one or two smis. |
| 6747 | ASSERT(Smi::kMaxValue >= String::kMaxLength); |
| 6748 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 6749 | MaybeObject* maybe_result = array->EnsureCanContainHeapObjectElements(); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 6750 | if (maybe_result->IsFailure()) return maybe_result; |
| 6751 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6752 | int special_length = special->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6753 | if (!array->HasFastElements()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6754 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6755 | } |
| 6756 | FixedArray* fixed_array = FixedArray::cast(array->elements()); |
| 6757 | if (fixed_array->length() < array_length) { |
| 6758 | array_length = fixed_array->length(); |
| 6759 | } |
| 6760 | |
| 6761 | if (array_length == 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6762 | return isolate->heap()->empty_string(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6763 | } else if (array_length == 1) { |
| 6764 | Object* first = fixed_array->get(0); |
| 6765 | if (first->IsString()) return first; |
| 6766 | } |
| 6767 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 6768 | bool ascii = special->HasOnlyAsciiChars(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6769 | int position = 0; |
| 6770 | for (int i = 0; i < array_length; i++) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6771 | int increment = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6772 | Object* elt = fixed_array->get(i); |
| 6773 | if (elt->IsSmi()) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6774 | // Smi encoding of position and length. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6775 | int smi_value = Smi::cast(elt)->value(); |
| 6776 | int pos; |
| 6777 | int len; |
| 6778 | if (smi_value > 0) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6779 | // Position and length encoded in one smi. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6780 | pos = StringBuilderSubstringPosition::decode(smi_value); |
| 6781 | len = StringBuilderSubstringLength::decode(smi_value); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6782 | } else { |
| 6783 | // Position and length encoded in two smis. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6784 | len = -smi_value; |
| 6785 | // Get the position and check that it is a positive smi. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6786 | i++; |
| 6787 | if (i >= array_length) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6788 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6789 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6790 | Object* next_smi = fixed_array->get(i); |
| 6791 | if (!next_smi->IsSmi()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6792 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6793 | } |
| 6794 | pos = Smi::cast(next_smi)->value(); |
| 6795 | if (pos < 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6796 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6797 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6798 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6799 | ASSERT(pos >= 0); |
| 6800 | ASSERT(len >= 0); |
| 6801 | if (pos > special_length || len > special_length - pos) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6802 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6803 | } |
| 6804 | increment = len; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6805 | } else if (elt->IsString()) { |
| 6806 | String* element = String::cast(elt); |
| 6807 | int element_length = element->length(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6808 | increment = element_length; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 6809 | if (ascii && !element->HasOnlyAsciiChars()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6810 | ascii = false; |
| 6811 | } |
| 6812 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6813 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6814 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6815 | if (increment > String::kMaxLength - position) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6816 | isolate->context()->mark_out_of_memory(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6817 | return Failure::OutOfMemoryException(); |
| 6818 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6819 | position += increment; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6820 | } |
| 6821 | |
| 6822 | int length = position; |
| 6823 | Object* object; |
| 6824 | |
| 6825 | if (ascii) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6826 | { MaybeObject* maybe_object = |
| 6827 | isolate->heap()->AllocateRawAsciiString(length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6828 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6829 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6830 | SeqAsciiString* answer = SeqAsciiString::cast(object); |
| 6831 | StringBuilderConcatHelper(special, |
| 6832 | answer->GetChars(), |
| 6833 | fixed_array, |
| 6834 | array_length); |
| 6835 | return answer; |
| 6836 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6837 | { MaybeObject* maybe_object = |
| 6838 | isolate->heap()->AllocateRawTwoByteString(length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6839 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6840 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6841 | SeqTwoByteString* answer = SeqTwoByteString::cast(object); |
| 6842 | StringBuilderConcatHelper(special, |
| 6843 | answer->GetChars(), |
| 6844 | fixed_array, |
| 6845 | array_length); |
| 6846 | return answer; |
| 6847 | } |
| 6848 | } |
| 6849 | |
| 6850 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6851 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringBuilderJoin) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6852 | NoHandleAllocation ha; |
| 6853 | ASSERT(args.length() == 3); |
| 6854 | CONVERT_CHECKED(JSArray, array, args[0]); |
| 6855 | if (!args[1]->IsSmi()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6856 | isolate->context()->mark_out_of_memory(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6857 | return Failure::OutOfMemoryException(); |
| 6858 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6859 | int array_length = args.smi_at(1); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6860 | CONVERT_CHECKED(String, separator, args[2]); |
| 6861 | |
| 6862 | if (!array->HasFastElements()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6863 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6864 | } |
| 6865 | FixedArray* fixed_array = FixedArray::cast(array->elements()); |
| 6866 | if (fixed_array->length() < array_length) { |
| 6867 | array_length = fixed_array->length(); |
| 6868 | } |
| 6869 | |
| 6870 | if (array_length == 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6871 | return isolate->heap()->empty_string(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6872 | } else if (array_length == 1) { |
| 6873 | Object* first = fixed_array->get(0); |
| 6874 | if (first->IsString()) return first; |
| 6875 | } |
| 6876 | |
| 6877 | int separator_length = separator->length(); |
| 6878 | int max_nof_separators = |
| 6879 | (String::kMaxLength + separator_length - 1) / separator_length; |
| 6880 | if (max_nof_separators < (array_length - 1)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6881 | isolate->context()->mark_out_of_memory(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6882 | return Failure::OutOfMemoryException(); |
| 6883 | } |
| 6884 | int length = (array_length - 1) * separator_length; |
| 6885 | for (int i = 0; i < array_length; i++) { |
| 6886 | Object* element_obj = fixed_array->get(i); |
| 6887 | if (!element_obj->IsString()) { |
| 6888 | // TODO(1161): handle this case. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6889 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6890 | } |
| 6891 | String* element = String::cast(element_obj); |
| 6892 | int increment = element->length(); |
| 6893 | if (increment > String::kMaxLength - length) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6894 | isolate->context()->mark_out_of_memory(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6895 | return Failure::OutOfMemoryException(); |
| 6896 | } |
| 6897 | length += increment; |
| 6898 | } |
| 6899 | |
| 6900 | Object* object; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6901 | { MaybeObject* maybe_object = |
| 6902 | isolate->heap()->AllocateRawTwoByteString(length); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6903 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6904 | } |
| 6905 | SeqTwoByteString* answer = SeqTwoByteString::cast(object); |
| 6906 | |
| 6907 | uc16* sink = answer->GetChars(); |
| 6908 | #ifdef DEBUG |
| 6909 | uc16* end = sink + length; |
| 6910 | #endif |
| 6911 | |
| 6912 | String* first = String::cast(fixed_array->get(0)); |
| 6913 | int first_length = first->length(); |
| 6914 | String::WriteToFlat(first, sink, 0, first_length); |
| 6915 | sink += first_length; |
| 6916 | |
| 6917 | for (int i = 1; i < array_length; i++) { |
| 6918 | ASSERT(sink + separator_length <= end); |
| 6919 | String::WriteToFlat(separator, sink, 0, separator_length); |
| 6920 | sink += separator_length; |
| 6921 | |
| 6922 | String* element = String::cast(fixed_array->get(i)); |
| 6923 | int element_length = element->length(); |
| 6924 | ASSERT(sink + element_length <= end); |
| 6925 | String::WriteToFlat(element, sink, 0, element_length); |
| 6926 | sink += element_length; |
| 6927 | } |
| 6928 | ASSERT(sink == end); |
| 6929 | |
| 6930 | ASSERT(!answer->HasOnlyAsciiChars()); // Use %_FastAsciiArrayJoin instead. |
| 6931 | return answer; |
| 6932 | } |
| 6933 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6934 | template <typename Char> |
| 6935 | static void JoinSparseArrayWithSeparator(FixedArray* elements, |
| 6936 | int elements_length, |
| 6937 | uint32_t array_length, |
| 6938 | String* separator, |
| 6939 | Vector<Char> buffer) { |
| 6940 | int previous_separator_position = 0; |
| 6941 | int separator_length = separator->length(); |
| 6942 | int cursor = 0; |
| 6943 | for (int i = 0; i < elements_length; i += 2) { |
| 6944 | int position = NumberToInt32(elements->get(i)); |
| 6945 | String* string = String::cast(elements->get(i + 1)); |
| 6946 | int string_length = string->length(); |
| 6947 | if (string->length() > 0) { |
| 6948 | while (previous_separator_position < position) { |
| 6949 | String::WriteToFlat<Char>(separator, &buffer[cursor], |
| 6950 | 0, separator_length); |
| 6951 | cursor += separator_length; |
| 6952 | previous_separator_position++; |
| 6953 | } |
| 6954 | String::WriteToFlat<Char>(string, &buffer[cursor], |
| 6955 | 0, string_length); |
| 6956 | cursor += string->length(); |
| 6957 | } |
| 6958 | } |
| 6959 | if (separator_length > 0) { |
| 6960 | // Array length must be representable as a signed 32-bit number, |
| 6961 | // otherwise the total string length would have been too large. |
| 6962 | ASSERT(array_length <= 0x7fffffff); // Is int32_t. |
| 6963 | int last_array_index = static_cast<int>(array_length - 1); |
| 6964 | while (previous_separator_position < last_array_index) { |
| 6965 | String::WriteToFlat<Char>(separator, &buffer[cursor], |
| 6966 | 0, separator_length); |
| 6967 | cursor += separator_length; |
| 6968 | previous_separator_position++; |
| 6969 | } |
| 6970 | } |
| 6971 | ASSERT(cursor <= buffer.length()); |
| 6972 | } |
| 6973 | |
| 6974 | |
| 6975 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SparseJoinWithSeparator) { |
| 6976 | NoHandleAllocation ha; |
| 6977 | ASSERT(args.length() == 3); |
| 6978 | CONVERT_CHECKED(JSArray, elements_array, args[0]); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 6979 | RUNTIME_ASSERT(elements_array->HasFastElements() || |
| 6980 | elements_array->HasFastSmiOnlyElements()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6981 | CONVERT_NUMBER_CHECKED(uint32_t, array_length, Uint32, args[1]); |
| 6982 | CONVERT_CHECKED(String, separator, args[2]); |
| 6983 | // elements_array is fast-mode JSarray of alternating positions |
| 6984 | // (increasing order) and strings. |
| 6985 | // array_length is length of original array (used to add separators); |
| 6986 | // separator is string to put between elements. Assumed to be non-empty. |
| 6987 | |
| 6988 | // Find total length of join result. |
| 6989 | int string_length = 0; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 6990 | bool is_ascii = separator->IsAsciiRepresentation(); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 6991 | int max_string_length; |
| 6992 | if (is_ascii) { |
| 6993 | max_string_length = SeqAsciiString::kMaxLength; |
| 6994 | } else { |
| 6995 | max_string_length = SeqTwoByteString::kMaxLength; |
| 6996 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6997 | bool overflow = false; |
| 6998 | CONVERT_NUMBER_CHECKED(int, elements_length, |
| 6999 | Int32, elements_array->length()); |
| 7000 | RUNTIME_ASSERT((elements_length & 1) == 0); // Even length. |
| 7001 | FixedArray* elements = FixedArray::cast(elements_array->elements()); |
| 7002 | for (int i = 0; i < elements_length; i += 2) { |
| 7003 | RUNTIME_ASSERT(elements->get(i)->IsNumber()); |
| 7004 | CONVERT_CHECKED(String, string, elements->get(i + 1)); |
| 7005 | int length = string->length(); |
| 7006 | if (is_ascii && !string->IsAsciiRepresentation()) { |
| 7007 | is_ascii = false; |
| 7008 | max_string_length = SeqTwoByteString::kMaxLength; |
| 7009 | } |
| 7010 | if (length > max_string_length || |
| 7011 | max_string_length - length < string_length) { |
| 7012 | overflow = true; |
| 7013 | break; |
| 7014 | } |
| 7015 | string_length += length; |
| 7016 | } |
| 7017 | int separator_length = separator->length(); |
| 7018 | if (!overflow && separator_length > 0) { |
| 7019 | if (array_length <= 0x7fffffffu) { |
| 7020 | int separator_count = static_cast<int>(array_length) - 1; |
| 7021 | int remaining_length = max_string_length - string_length; |
| 7022 | if ((remaining_length / separator_length) >= separator_count) { |
| 7023 | string_length += separator_length * (array_length - 1); |
| 7024 | } else { |
| 7025 | // Not room for the separators within the maximal string length. |
| 7026 | overflow = true; |
| 7027 | } |
| 7028 | } else { |
| 7029 | // Nonempty separator and at least 2^31-1 separators necessary |
| 7030 | // means that the string is too large to create. |
| 7031 | STATIC_ASSERT(String::kMaxLength < 0x7fffffff); |
| 7032 | overflow = true; |
| 7033 | } |
| 7034 | } |
| 7035 | if (overflow) { |
| 7036 | // Throw OutOfMemory exception for creating too large a string. |
| 7037 | V8::FatalProcessOutOfMemory("Array join result too large."); |
| 7038 | } |
| 7039 | |
| 7040 | if (is_ascii) { |
| 7041 | MaybeObject* result_allocation = |
| 7042 | isolate->heap()->AllocateRawAsciiString(string_length); |
| 7043 | if (result_allocation->IsFailure()) return result_allocation; |
| 7044 | SeqAsciiString* result_string = |
| 7045 | SeqAsciiString::cast(result_allocation->ToObjectUnchecked()); |
| 7046 | JoinSparseArrayWithSeparator<char>(elements, |
| 7047 | elements_length, |
| 7048 | array_length, |
| 7049 | separator, |
| 7050 | Vector<char>(result_string->GetChars(), |
| 7051 | string_length)); |
| 7052 | return result_string; |
| 7053 | } else { |
| 7054 | MaybeObject* result_allocation = |
| 7055 | isolate->heap()->AllocateRawTwoByteString(string_length); |
| 7056 | if (result_allocation->IsFailure()) return result_allocation; |
| 7057 | SeqTwoByteString* result_string = |
| 7058 | SeqTwoByteString::cast(result_allocation->ToObjectUnchecked()); |
| 7059 | JoinSparseArrayWithSeparator<uc16>(elements, |
| 7060 | elements_length, |
| 7061 | array_length, |
| 7062 | separator, |
| 7063 | Vector<uc16>(result_string->GetChars(), |
| 7064 | string_length)); |
| 7065 | return result_string; |
| 7066 | } |
| 7067 | } |
| 7068 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7069 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7070 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberOr) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7071 | NoHandleAllocation ha; |
| 7072 | ASSERT(args.length() == 2); |
| 7073 | |
| 7074 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 7075 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7076 | return isolate->heap()->NumberFromInt32(x | y); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7077 | } |
| 7078 | |
| 7079 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7080 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberAnd) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7081 | NoHandleAllocation ha; |
| 7082 | ASSERT(args.length() == 2); |
| 7083 | |
| 7084 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 7085 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7086 | return isolate->heap()->NumberFromInt32(x & y); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7087 | } |
| 7088 | |
| 7089 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7090 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberXor) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7091 | NoHandleAllocation ha; |
| 7092 | ASSERT(args.length() == 2); |
| 7093 | |
| 7094 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 7095 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7096 | return isolate->heap()->NumberFromInt32(x ^ y); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7097 | } |
| 7098 | |
| 7099 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7100 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberNot) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7101 | NoHandleAllocation ha; |
| 7102 | ASSERT(args.length() == 1); |
| 7103 | |
| 7104 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7105 | return isolate->heap()->NumberFromInt32(~x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7106 | } |
| 7107 | |
| 7108 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7109 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberShl) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7110 | NoHandleAllocation ha; |
| 7111 | ASSERT(args.length() == 2); |
| 7112 | |
| 7113 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 7114 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7115 | return isolate->heap()->NumberFromInt32(x << (y & 0x1f)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7116 | } |
| 7117 | |
| 7118 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7119 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberShr) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7120 | NoHandleAllocation ha; |
| 7121 | ASSERT(args.length() == 2); |
| 7122 | |
| 7123 | CONVERT_NUMBER_CHECKED(uint32_t, x, Uint32, args[0]); |
| 7124 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7125 | return isolate->heap()->NumberFromUint32(x >> (y & 0x1f)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7126 | } |
| 7127 | |
| 7128 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7129 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberSar) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7130 | NoHandleAllocation ha; |
| 7131 | ASSERT(args.length() == 2); |
| 7132 | |
| 7133 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 7134 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7135 | return isolate->heap()->NumberFromInt32(ArithmeticShiftRight(x, y & 0x1f)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7136 | } |
| 7137 | |
| 7138 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7139 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberEquals) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7140 | NoHandleAllocation ha; |
| 7141 | ASSERT(args.length() == 2); |
| 7142 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7143 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 7144 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7145 | if (isnan(x)) return Smi::FromInt(NOT_EQUAL); |
| 7146 | if (isnan(y)) return Smi::FromInt(NOT_EQUAL); |
| 7147 | if (x == y) return Smi::FromInt(EQUAL); |
| 7148 | Object* result; |
| 7149 | if ((fpclassify(x) == FP_ZERO) && (fpclassify(y) == FP_ZERO)) { |
| 7150 | result = Smi::FromInt(EQUAL); |
| 7151 | } else { |
| 7152 | result = Smi::FromInt(NOT_EQUAL); |
| 7153 | } |
| 7154 | return result; |
| 7155 | } |
| 7156 | |
| 7157 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7158 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringEquals) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7159 | NoHandleAllocation ha; |
| 7160 | ASSERT(args.length() == 2); |
| 7161 | |
| 7162 | CONVERT_CHECKED(String, x, args[0]); |
| 7163 | CONVERT_CHECKED(String, y, args[1]); |
| 7164 | |
| 7165 | bool not_equal = !x->Equals(y); |
| 7166 | // This is slightly convoluted because the value that signifies |
| 7167 | // equality is 0 and inequality is 1 so we have to negate the result |
| 7168 | // from String::Equals. |
| 7169 | ASSERT(not_equal == 0 || not_equal == 1); |
| 7170 | STATIC_CHECK(EQUAL == 0); |
| 7171 | STATIC_CHECK(NOT_EQUAL == 1); |
| 7172 | return Smi::FromInt(not_equal); |
| 7173 | } |
| 7174 | |
| 7175 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7176 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberCompare) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7177 | NoHandleAllocation ha; |
| 7178 | ASSERT(args.length() == 3); |
| 7179 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7180 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 7181 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7182 | if (isnan(x) || isnan(y)) return args[2]; |
| 7183 | if (x == y) return Smi::FromInt(EQUAL); |
| 7184 | if (isless(x, y)) return Smi::FromInt(LESS); |
| 7185 | return Smi::FromInt(GREATER); |
| 7186 | } |
| 7187 | |
| 7188 | |
| 7189 | // Compare two Smis as if they were converted to strings and then |
| 7190 | // compared lexicographically. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7191 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SmiLexicographicCompare) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7192 | NoHandleAllocation ha; |
| 7193 | ASSERT(args.length() == 2); |
| 7194 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7195 | // Extract the integer values from the Smis. |
| 7196 | CONVERT_CHECKED(Smi, x, args[0]); |
| 7197 | CONVERT_CHECKED(Smi, y, args[1]); |
| 7198 | int x_value = x->value(); |
| 7199 | int y_value = y->value(); |
| 7200 | |
| 7201 | // If the integers are equal so are the string representations. |
| 7202 | if (x_value == y_value) return Smi::FromInt(EQUAL); |
| 7203 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7204 | // 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] | 7205 | // same as the lexicographic order of the string representations. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7206 | if (x_value == 0 || y_value == 0) |
| 7207 | return Smi::FromInt(x_value < y_value ? LESS : GREATER); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7208 | |
| 7209 | // If only one of the integers is negative the negative number is |
| 7210 | // smallest because the char code of '-' is less than the char code |
| 7211 | // of any digit. Otherwise, we make both values positive. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7212 | |
| 7213 | // Use unsigned values otherwise the logic is incorrect for -MIN_INT on |
| 7214 | // architectures using 32-bit Smis. |
| 7215 | uint32_t x_scaled = x_value; |
| 7216 | uint32_t y_scaled = y_value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7217 | if (x_value < 0 || y_value < 0) { |
| 7218 | if (y_value >= 0) return Smi::FromInt(LESS); |
| 7219 | if (x_value >= 0) return Smi::FromInt(GREATER); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7220 | x_scaled = -x_value; |
| 7221 | y_scaled = -y_value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7222 | } |
| 7223 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7224 | static const uint32_t kPowersOf10[] = { |
| 7225 | 1, 10, 100, 1000, 10*1000, 100*1000, |
| 7226 | 1000*1000, 10*1000*1000, 100*1000*1000, |
| 7227 | 1000*1000*1000 |
| 7228 | }; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7229 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7230 | // If the integers have the same number of decimal digits they can be |
| 7231 | // compared directly as the numeric order is the same as the |
| 7232 | // lexicographic order. If one integer has fewer digits, it is scaled |
| 7233 | // by some power of 10 to have the same number of digits as the longer |
| 7234 | // integer. If the scaled integers are equal it means the shorter |
| 7235 | // integer comes first in the lexicographic order. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7236 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7237 | // From http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 |
| 7238 | int x_log2 = IntegerLog2(x_scaled); |
| 7239 | int x_log10 = ((x_log2 + 1) * 1233) >> 12; |
| 7240 | x_log10 -= x_scaled < kPowersOf10[x_log10]; |
| 7241 | |
| 7242 | int y_log2 = IntegerLog2(y_scaled); |
| 7243 | int y_log10 = ((y_log2 + 1) * 1233) >> 12; |
| 7244 | y_log10 -= y_scaled < kPowersOf10[y_log10]; |
| 7245 | |
| 7246 | int tie = EQUAL; |
| 7247 | |
| 7248 | if (x_log10 < y_log10) { |
| 7249 | // X has fewer digits. We would like to simply scale up X but that |
| 7250 | // might overflow, e.g when comparing 9 with 1_000_000_000, 9 would |
| 7251 | // be scaled up to 9_000_000_000. So we scale up by the next |
| 7252 | // smallest power and scale down Y to drop one digit. It is OK to |
| 7253 | // drop one digit from the longer integer since the final digit is |
| 7254 | // past the length of the shorter integer. |
| 7255 | x_scaled *= kPowersOf10[y_log10 - x_log10 - 1]; |
| 7256 | y_scaled /= 10; |
| 7257 | tie = LESS; |
| 7258 | } else if (y_log10 < x_log10) { |
| 7259 | y_scaled *= kPowersOf10[x_log10 - y_log10 - 1]; |
| 7260 | x_scaled /= 10; |
| 7261 | tie = GREATER; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7262 | } |
| 7263 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7264 | if (x_scaled < y_scaled) return Smi::FromInt(LESS); |
| 7265 | if (x_scaled > y_scaled) return Smi::FromInt(GREATER); |
| 7266 | return Smi::FromInt(tie); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7267 | } |
| 7268 | |
| 7269 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7270 | static Object* StringInputBufferCompare(RuntimeState* state, |
| 7271 | String* x, |
| 7272 | String* y) { |
| 7273 | StringInputBuffer& bufx = *state->string_input_buffer_compare_bufx(); |
| 7274 | StringInputBuffer& bufy = *state->string_input_buffer_compare_bufy(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7275 | bufx.Reset(x); |
| 7276 | bufy.Reset(y); |
| 7277 | while (bufx.has_more() && bufy.has_more()) { |
| 7278 | int d = bufx.GetNext() - bufy.GetNext(); |
| 7279 | if (d < 0) return Smi::FromInt(LESS); |
| 7280 | else if (d > 0) return Smi::FromInt(GREATER); |
| 7281 | } |
| 7282 | |
| 7283 | // x is (non-trivial) prefix of y: |
| 7284 | if (bufy.has_more()) return Smi::FromInt(LESS); |
| 7285 | // y is prefix of x: |
| 7286 | return Smi::FromInt(bufx.has_more() ? GREATER : EQUAL); |
| 7287 | } |
| 7288 | |
| 7289 | |
| 7290 | static Object* FlatStringCompare(String* x, String* y) { |
| 7291 | ASSERT(x->IsFlat()); |
| 7292 | ASSERT(y->IsFlat()); |
| 7293 | Object* equal_prefix_result = Smi::FromInt(EQUAL); |
| 7294 | int prefix_length = x->length(); |
| 7295 | if (y->length() < prefix_length) { |
| 7296 | prefix_length = y->length(); |
| 7297 | equal_prefix_result = Smi::FromInt(GREATER); |
| 7298 | } else if (y->length() > prefix_length) { |
| 7299 | equal_prefix_result = Smi::FromInt(LESS); |
| 7300 | } |
| 7301 | int r; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 7302 | String::FlatContent x_content = x->GetFlatContent(); |
| 7303 | String::FlatContent y_content = y->GetFlatContent(); |
| 7304 | if (x_content.IsAscii()) { |
| 7305 | Vector<const char> x_chars = x_content.ToAsciiVector(); |
| 7306 | if (y_content.IsAscii()) { |
| 7307 | Vector<const char> y_chars = y_content.ToAsciiVector(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7308 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 7309 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 7310 | Vector<const uc16> y_chars = y_content.ToUC16Vector(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7311 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 7312 | } |
| 7313 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 7314 | Vector<const uc16> x_chars = x_content.ToUC16Vector(); |
| 7315 | if (y_content.IsAscii()) { |
| 7316 | Vector<const char> y_chars = y_content.ToAsciiVector(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7317 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 7318 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 7319 | Vector<const uc16> y_chars = y_content.ToUC16Vector(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7320 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 7321 | } |
| 7322 | } |
| 7323 | Object* result; |
| 7324 | if (r == 0) { |
| 7325 | result = equal_prefix_result; |
| 7326 | } else { |
| 7327 | result = (r < 0) ? Smi::FromInt(LESS) : Smi::FromInt(GREATER); |
| 7328 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7329 | ASSERT(result == |
| 7330 | StringInputBufferCompare(Isolate::Current()->runtime_state(), x, y)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7331 | return result; |
| 7332 | } |
| 7333 | |
| 7334 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7335 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringCompare) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7336 | NoHandleAllocation ha; |
| 7337 | ASSERT(args.length() == 2); |
| 7338 | |
| 7339 | CONVERT_CHECKED(String, x, args[0]); |
| 7340 | CONVERT_CHECKED(String, y, args[1]); |
| 7341 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7342 | isolate->counters()->string_compare_runtime()->Increment(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7343 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7344 | // A few fast case tests before we flatten. |
| 7345 | if (x == y) return Smi::FromInt(EQUAL); |
| 7346 | if (y->length() == 0) { |
| 7347 | if (x->length() == 0) return Smi::FromInt(EQUAL); |
| 7348 | return Smi::FromInt(GREATER); |
| 7349 | } else if (x->length() == 0) { |
| 7350 | return Smi::FromInt(LESS); |
| 7351 | } |
| 7352 | |
| 7353 | int d = x->Get(0) - y->Get(0); |
| 7354 | if (d < 0) return Smi::FromInt(LESS); |
| 7355 | else if (d > 0) return Smi::FromInt(GREATER); |
| 7356 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7357 | Object* obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7358 | { MaybeObject* maybe_obj = isolate->heap()->PrepareForCompare(x); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7359 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7360 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7361 | { MaybeObject* maybe_obj = isolate->heap()->PrepareForCompare(y); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7362 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7363 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7364 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7365 | return (x->IsFlat() && y->IsFlat()) ? FlatStringCompare(x, y) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7366 | : StringInputBufferCompare(isolate->runtime_state(), x, y); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7367 | } |
| 7368 | |
| 7369 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7370 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_acos) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7371 | NoHandleAllocation ha; |
| 7372 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7373 | isolate->counters()->math_acos()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7374 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7375 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7376 | return isolate->transcendental_cache()->Get(TranscendentalCache::ACOS, x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7377 | } |
| 7378 | |
| 7379 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7380 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_asin) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7381 | NoHandleAllocation ha; |
| 7382 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7383 | isolate->counters()->math_asin()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7384 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7385 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7386 | return isolate->transcendental_cache()->Get(TranscendentalCache::ASIN, x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7387 | } |
| 7388 | |
| 7389 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7390 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_atan) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7391 | NoHandleAllocation ha; |
| 7392 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7393 | isolate->counters()->math_atan()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7394 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7395 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7396 | return isolate->transcendental_cache()->Get(TranscendentalCache::ATAN, x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7397 | } |
| 7398 | |
| 7399 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7400 | static const double kPiDividedBy4 = 0.78539816339744830962; |
| 7401 | |
| 7402 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7403 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_atan2) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7404 | NoHandleAllocation ha; |
| 7405 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7406 | isolate->counters()->math_atan2()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7407 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7408 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 7409 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7410 | double result; |
| 7411 | if (isinf(x) && isinf(y)) { |
| 7412 | // Make sure that the result in case of two infinite arguments |
| 7413 | // is a multiple of Pi / 4. The sign of the result is determined |
| 7414 | // by the first argument (x) and the sign of the second argument |
| 7415 | // determines the multiplier: one or three. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7416 | int multiplier = (x < 0) ? -1 : 1; |
| 7417 | if (y < 0) multiplier *= 3; |
| 7418 | result = multiplier * kPiDividedBy4; |
| 7419 | } else { |
| 7420 | result = atan2(x, y); |
| 7421 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7422 | return isolate->heap()->AllocateHeapNumber(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7423 | } |
| 7424 | |
| 7425 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7426 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_ceil) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7427 | NoHandleAllocation ha; |
| 7428 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7429 | isolate->counters()->math_ceil()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7430 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7431 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7432 | return isolate->heap()->NumberFromDouble(ceiling(x)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7433 | } |
| 7434 | |
| 7435 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7436 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_cos) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7437 | NoHandleAllocation ha; |
| 7438 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7439 | isolate->counters()->math_cos()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7440 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7441 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7442 | return isolate->transcendental_cache()->Get(TranscendentalCache::COS, x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7443 | } |
| 7444 | |
| 7445 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7446 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_exp) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7447 | NoHandleAllocation ha; |
| 7448 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7449 | isolate->counters()->math_exp()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7450 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7451 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7452 | return isolate->transcendental_cache()->Get(TranscendentalCache::EXP, x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7453 | } |
| 7454 | |
| 7455 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7456 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_floor) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7457 | NoHandleAllocation ha; |
| 7458 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7459 | isolate->counters()->math_floor()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7460 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7461 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7462 | return isolate->heap()->NumberFromDouble(floor(x)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7463 | } |
| 7464 | |
| 7465 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7466 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_log) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7467 | NoHandleAllocation ha; |
| 7468 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7469 | isolate->counters()->math_log()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7470 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7471 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7472 | return isolate->transcendental_cache()->Get(TranscendentalCache::LOG, x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7473 | } |
| 7474 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 7475 | // Slow version of Math.pow. We check for fast paths for special cases. |
| 7476 | // Used if SSE2/VFP3 is not available. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7477 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_pow) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7478 | NoHandleAllocation ha; |
| 7479 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7480 | isolate->counters()->math_pow()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7481 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7482 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7483 | |
| 7484 | // If the second argument is a smi, it is much faster to call the |
| 7485 | // custom powi() function than the generic pow(). |
| 7486 | if (args[1]->IsSmi()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7487 | int y = args.smi_at(1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7488 | return isolate->heap()->NumberFromDouble(power_double_int(x, y)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7489 | } |
| 7490 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7491 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 7492 | int y_int = static_cast<int>(y); |
| 7493 | double result; |
| 7494 | if (y == y_int) { |
| 7495 | result = power_double_int(x, y_int); // Returns 1 if exponent is 0. |
| 7496 | } else if (y == 0.5) { |
| 7497 | result = (isinf(x)) ? V8_INFINITY : sqrt(x + 0.0); // Convert -0 to +0. |
| 7498 | } else if (y == -0.5) { |
| 7499 | result = (isinf(x)) ? 0 : 1.0 / sqrt(x + 0.0); // Convert -0 to +0. |
| 7500 | } else { |
| 7501 | result = power_double_double(x, y); |
| 7502 | } |
| 7503 | if (isnan(result)) return isolate->heap()->nan_value(); |
| 7504 | return isolate->heap()->AllocateHeapNumber(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7505 | } |
| 7506 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 7507 | // Fast version of Math.pow if we know that y is not an integer and y is not |
| 7508 | // -0.5 or 0.5. Used as slow case from full codegen. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7509 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_pow_cfunction) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7510 | NoHandleAllocation ha; |
| 7511 | ASSERT(args.length() == 2); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 7512 | isolate->counters()->math_pow()->Increment(); |
| 7513 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7514 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 7515 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7516 | if (y == 0) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7517 | return Smi::FromInt(1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7518 | } else { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 7519 | double result = power_double_double(x, y); |
| 7520 | if (isnan(result)) return isolate->heap()->nan_value(); |
| 7521 | return isolate->heap()->AllocateHeapNumber(result); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7522 | } |
| 7523 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7524 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7525 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7526 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RoundNumber) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7527 | NoHandleAllocation ha; |
| 7528 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7529 | isolate->counters()->math_round()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7530 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7531 | if (!args[0]->IsHeapNumber()) { |
| 7532 | // Must be smi. Return the argument unchanged for all the other types |
| 7533 | // to make fuzz-natives test happy. |
| 7534 | return args[0]; |
| 7535 | } |
| 7536 | |
| 7537 | HeapNumber* number = reinterpret_cast<HeapNumber*>(args[0]); |
| 7538 | |
| 7539 | double value = number->value(); |
| 7540 | int exponent = number->get_exponent(); |
| 7541 | int sign = number->get_sign(); |
| 7542 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7543 | if (exponent < -1) { |
| 7544 | // Number in range ]-0.5..0.5[. These always round to +/-zero. |
| 7545 | if (sign) return isolate->heap()->minus_zero_value(); |
| 7546 | return Smi::FromInt(0); |
| 7547 | } |
| 7548 | |
| 7549 | // We compare with kSmiValueSize - 2 because (2^30 - 0.1) has exponent 29 and |
| 7550 | // should be rounded to 2^30, which is not smi (for 31-bit smis, similar |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 7551 | // argument holds for 32-bit smis). |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7552 | if (!sign && exponent < kSmiValueSize - 2) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7553 | return Smi::FromInt(static_cast<int>(value + 0.5)); |
| 7554 | } |
| 7555 | |
| 7556 | // If the magnitude is big enough, there's no place for fraction part. If we |
| 7557 | // try to add 0.5 to this number, 1.0 will be added instead. |
| 7558 | if (exponent >= 52) { |
| 7559 | return number; |
| 7560 | } |
| 7561 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7562 | if (sign && value >= -0.5) return isolate->heap()->minus_zero_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7563 | |
| 7564 | // Do not call NumberFromDouble() to avoid extra checks. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7565 | return isolate->heap()->AllocateHeapNumber(floor(value + 0.5)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7566 | } |
| 7567 | |
| 7568 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7569 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_sin) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7570 | NoHandleAllocation ha; |
| 7571 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7572 | isolate->counters()->math_sin()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7573 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7574 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7575 | return isolate->transcendental_cache()->Get(TranscendentalCache::SIN, x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7576 | } |
| 7577 | |
| 7578 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7579 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_sqrt) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7580 | NoHandleAllocation ha; |
| 7581 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7582 | isolate->counters()->math_sqrt()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7583 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7584 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7585 | return isolate->heap()->AllocateHeapNumber(sqrt(x)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7586 | } |
| 7587 | |
| 7588 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7589 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_tan) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7590 | NoHandleAllocation ha; |
| 7591 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7592 | isolate->counters()->math_tan()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7593 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7594 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7595 | return isolate->transcendental_cache()->Get(TranscendentalCache::TAN, x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7596 | } |
| 7597 | |
| 7598 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 7599 | static int MakeDay(int year, int month) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7600 | static const int day_from_month[] = {0, 31, 59, 90, 120, 151, |
| 7601 | 181, 212, 243, 273, 304, 334}; |
| 7602 | static const int day_from_month_leap[] = {0, 31, 60, 91, 121, 152, |
| 7603 | 182, 213, 244, 274, 305, 335}; |
| 7604 | |
| 7605 | year += month / 12; |
| 7606 | month %= 12; |
| 7607 | if (month < 0) { |
| 7608 | year--; |
| 7609 | month += 12; |
| 7610 | } |
| 7611 | |
| 7612 | ASSERT(month >= 0); |
| 7613 | ASSERT(month < 12); |
| 7614 | |
| 7615 | // year_delta is an arbitrary number such that: |
| 7616 | // a) year_delta = -1 (mod 400) |
| 7617 | // b) year + year_delta > 0 for years in the range defined by |
| 7618 | // ECMA 262 - 15.9.1.1, i.e. upto 100,000,000 days on either side of |
| 7619 | // Jan 1 1970. This is required so that we don't run into integer |
| 7620 | // division of negative numbers. |
| 7621 | // c) there shouldn't be an overflow for 32-bit integers in the following |
| 7622 | // operations. |
| 7623 | static const int year_delta = 399999; |
| 7624 | static const int base_day = 365 * (1970 + year_delta) + |
| 7625 | (1970 + year_delta) / 4 - |
| 7626 | (1970 + year_delta) / 100 + |
| 7627 | (1970 + year_delta) / 400; |
| 7628 | |
| 7629 | int year1 = year + year_delta; |
| 7630 | int day_from_year = 365 * year1 + |
| 7631 | year1 / 4 - |
| 7632 | year1 / 100 + |
| 7633 | year1 / 400 - |
| 7634 | base_day; |
| 7635 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 7636 | if ((year % 4 != 0) || (year % 100 == 0 && year % 400 != 0)) { |
| 7637 | return day_from_year + day_from_month[month]; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7638 | } |
| 7639 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 7640 | return day_from_year + day_from_month_leap[month]; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7641 | } |
| 7642 | |
| 7643 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7644 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateMakeDay) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7645 | NoHandleAllocation ha; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 7646 | ASSERT(args.length() == 2); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7647 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7648 | CONVERT_SMI_ARG_CHECKED(year, 0); |
| 7649 | CONVERT_SMI_ARG_CHECKED(month, 1); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7650 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 7651 | return Smi::FromInt(MakeDay(year, month)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7652 | } |
| 7653 | |
| 7654 | |
| 7655 | static const int kDays4Years[] = {0, 365, 2 * 365, 3 * 365 + 1}; |
| 7656 | static const int kDaysIn4Years = 4 * 365 + 1; |
| 7657 | static const int kDaysIn100Years = 25 * kDaysIn4Years - 1; |
| 7658 | static const int kDaysIn400Years = 4 * kDaysIn100Years + 1; |
| 7659 | static const int kDays1970to2000 = 30 * 365 + 7; |
| 7660 | static const int kDaysOffset = 1000 * kDaysIn400Years + 5 * kDaysIn400Years - |
| 7661 | kDays1970to2000; |
| 7662 | static const int kYearsOffset = 400000; |
| 7663 | |
| 7664 | static const char kDayInYear[] = { |
| 7665 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7666 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7667 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7668 | 22, 23, 24, 25, 26, 27, 28, |
| 7669 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7670 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7671 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7672 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7673 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7674 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7675 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7676 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7677 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7678 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7679 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7680 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7681 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7682 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7683 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7684 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7685 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7686 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7687 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7688 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7689 | |
| 7690 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7691 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7692 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7693 | 22, 23, 24, 25, 26, 27, 28, |
| 7694 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7695 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7696 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7697 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7698 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7699 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7700 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7701 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7702 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7703 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7704 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7705 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7706 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7707 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7708 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7709 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7710 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7711 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7712 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7713 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7714 | |
| 7715 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7716 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7717 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7718 | 22, 23, 24, 25, 26, 27, 28, 29, |
| 7719 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7720 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7721 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7722 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7723 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7724 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7725 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7726 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7727 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7728 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7729 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7730 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7731 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7732 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7733 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7734 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7735 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7736 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7737 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7738 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7739 | |
| 7740 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7741 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7742 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7743 | 22, 23, 24, 25, 26, 27, 28, |
| 7744 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7745 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7746 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7747 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7748 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7749 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7750 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7751 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7752 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7753 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7754 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7755 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7756 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7757 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7758 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7759 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7760 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7761 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7762 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7763 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; |
| 7764 | |
| 7765 | static const char kMonthInYear[] = { |
| 7766 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7767 | 0, 0, 0, 0, 0, 0, |
| 7768 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 7769 | 1, 1, 1, |
| 7770 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 7771 | 2, 2, 2, 2, 2, 2, |
| 7772 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 7773 | 3, 3, 3, 3, 3, |
| 7774 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 7775 | 4, 4, 4, 4, 4, 4, |
| 7776 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 7777 | 5, 5, 5, 5, 5, |
| 7778 | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
| 7779 | 6, 6, 6, 6, 6, 6, |
| 7780 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 7781 | 7, 7, 7, 7, 7, 7, |
| 7782 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| 7783 | 8, 8, 8, 8, 8, |
| 7784 | 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, |
| 7785 | 9, 9, 9, 9, 9, 9, |
| 7786 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7787 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7788 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7789 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7790 | |
| 7791 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7792 | 0, 0, 0, 0, 0, 0, |
| 7793 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 7794 | 1, 1, 1, |
| 7795 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 7796 | 2, 2, 2, 2, 2, 2, |
| 7797 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 7798 | 3, 3, 3, 3, 3, |
| 7799 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 7800 | 4, 4, 4, 4, 4, 4, |
| 7801 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 7802 | 5, 5, 5, 5, 5, |
| 7803 | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
| 7804 | 6, 6, 6, 6, 6, 6, |
| 7805 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 7806 | 7, 7, 7, 7, 7, 7, |
| 7807 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| 7808 | 8, 8, 8, 8, 8, |
| 7809 | 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, |
| 7810 | 9, 9, 9, 9, 9, 9, |
| 7811 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7812 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7813 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7814 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7815 | |
| 7816 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7817 | 0, 0, 0, 0, 0, 0, |
| 7818 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 7819 | 1, 1, 1, 1, |
| 7820 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 7821 | 2, 2, 2, 2, 2, 2, |
| 7822 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 7823 | 3, 3, 3, 3, 3, |
| 7824 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 7825 | 4, 4, 4, 4, 4, 4, |
| 7826 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 7827 | 5, 5, 5, 5, 5, |
| 7828 | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
| 7829 | 6, 6, 6, 6, 6, 6, |
| 7830 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 7831 | 7, 7, 7, 7, 7, 7, |
| 7832 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| 7833 | 8, 8, 8, 8, 8, |
| 7834 | 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, |
| 7835 | 9, 9, 9, 9, 9, 9, |
| 7836 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7837 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7838 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7839 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7840 | |
| 7841 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 7842 | 0, 0, 0, 0, 0, 0, |
| 7843 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 7844 | 1, 1, 1, |
| 7845 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 7846 | 2, 2, 2, 2, 2, 2, |
| 7847 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 7848 | 3, 3, 3, 3, 3, |
| 7849 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 7850 | 4, 4, 4, 4, 4, 4, |
| 7851 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 7852 | 5, 5, 5, 5, 5, |
| 7853 | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
| 7854 | 6, 6, 6, 6, 6, 6, |
| 7855 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 7856 | 7, 7, 7, 7, 7, 7, |
| 7857 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| 7858 | 8, 8, 8, 8, 8, |
| 7859 | 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, |
| 7860 | 9, 9, 9, 9, 9, 9, |
| 7861 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7862 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7863 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7864 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}; |
| 7865 | |
| 7866 | |
| 7867 | // This function works for dates from 1970 to 2099. |
| 7868 | static inline void DateYMDFromTimeAfter1970(int date, |
| 7869 | int& year, int& month, int& day) { |
| 7870 | #ifdef DEBUG |
| 7871 | int save_date = date; // Need this for ASSERT in the end. |
| 7872 | #endif |
| 7873 | |
| 7874 | year = 1970 + (4 * date + 2) / kDaysIn4Years; |
| 7875 | date %= kDaysIn4Years; |
| 7876 | |
| 7877 | month = kMonthInYear[date]; |
| 7878 | day = kDayInYear[date]; |
| 7879 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 7880 | ASSERT(MakeDay(year, month) + day - 1 == save_date); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7881 | } |
| 7882 | |
| 7883 | |
| 7884 | static inline void DateYMDFromTimeSlow(int date, |
| 7885 | int& year, int& month, int& day) { |
| 7886 | #ifdef DEBUG |
| 7887 | int save_date = date; // Need this for ASSERT in the end. |
| 7888 | #endif |
| 7889 | |
| 7890 | date += kDaysOffset; |
| 7891 | year = 400 * (date / kDaysIn400Years) - kYearsOffset; |
| 7892 | date %= kDaysIn400Years; |
| 7893 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 7894 | ASSERT(MakeDay(year, 0) + date == save_date); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7895 | |
| 7896 | date--; |
| 7897 | int yd1 = date / kDaysIn100Years; |
| 7898 | date %= kDaysIn100Years; |
| 7899 | year += 100 * yd1; |
| 7900 | |
| 7901 | date++; |
| 7902 | int yd2 = date / kDaysIn4Years; |
| 7903 | date %= kDaysIn4Years; |
| 7904 | year += 4 * yd2; |
| 7905 | |
| 7906 | date--; |
| 7907 | int yd3 = date / 365; |
| 7908 | date %= 365; |
| 7909 | year += yd3; |
| 7910 | |
| 7911 | bool is_leap = (!yd1 || yd2) && !yd3; |
| 7912 | |
| 7913 | ASSERT(date >= -1); |
| 7914 | ASSERT(is_leap || (date >= 0)); |
| 7915 | ASSERT((date < 365) || (is_leap && (date < 366))); |
| 7916 | ASSERT(is_leap == ((year % 4 == 0) && (year % 100 || (year % 400 == 0)))); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 7917 | ASSERT(is_leap || ((MakeDay(year, 0) + date) == save_date)); |
| 7918 | ASSERT(!is_leap || ((MakeDay(year, 0) + date + 1) == save_date)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7919 | |
| 7920 | if (is_leap) { |
| 7921 | day = kDayInYear[2*365 + 1 + date]; |
| 7922 | month = kMonthInYear[2*365 + 1 + date]; |
| 7923 | } else { |
| 7924 | day = kDayInYear[date]; |
| 7925 | month = kMonthInYear[date]; |
| 7926 | } |
| 7927 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 7928 | ASSERT(MakeDay(year, month) + day - 1 == save_date); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7929 | } |
| 7930 | |
| 7931 | |
| 7932 | static inline void DateYMDFromTime(int date, |
| 7933 | int& year, int& month, int& day) { |
| 7934 | if (date >= 0 && date < 32 * kDaysIn4Years) { |
| 7935 | DateYMDFromTimeAfter1970(date, year, month, day); |
| 7936 | } else { |
| 7937 | DateYMDFromTimeSlow(date, year, month, day); |
| 7938 | } |
| 7939 | } |
| 7940 | |
| 7941 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7942 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateYMDFromTime) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7943 | NoHandleAllocation ha; |
| 7944 | ASSERT(args.length() == 2); |
| 7945 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7946 | CONVERT_DOUBLE_ARG_CHECKED(t, 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7947 | CONVERT_CHECKED(JSArray, res_array, args[1]); |
| 7948 | |
| 7949 | int year, month, day; |
| 7950 | DateYMDFromTime(static_cast<int>(floor(t / 86400000)), year, month, day); |
| 7951 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 7952 | FixedArrayBase* elms_base = FixedArrayBase::cast(res_array->elements()); |
| 7953 | RUNTIME_ASSERT(elms_base->length() == 3); |
| 7954 | RUNTIME_ASSERT(res_array->HasFastTypeElements()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 7955 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 7956 | MaybeObject* maybe = res_array->EnsureWritableFastElements(); |
| 7957 | if (maybe->IsFailure()) return maybe; |
| 7958 | FixedArray* elms = FixedArray::cast(res_array->elements()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 7959 | elms->set(0, Smi::FromInt(year)); |
| 7960 | elms->set(1, Smi::FromInt(month)); |
| 7961 | elms->set(2, Smi::FromInt(day)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7962 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7963 | return isolate->heap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7964 | } |
| 7965 | |
| 7966 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7967 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewArgumentsFast) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7968 | HandleScope scope(isolate); |
| 7969 | ASSERT(args.length() == 3); |
| 7970 | |
| 7971 | Handle<JSFunction> callee = args.at<JSFunction>(0); |
| 7972 | Object** parameters = reinterpret_cast<Object**>(args[1]); |
| 7973 | const int argument_count = Smi::cast(args[2])->value(); |
| 7974 | |
| 7975 | Handle<JSObject> result = |
| 7976 | isolate->factory()->NewArgumentsObject(callee, argument_count); |
| 7977 | // Allocate the elements if needed. |
| 7978 | int parameter_count = callee->shared()->formal_parameter_count(); |
| 7979 | if (argument_count > 0) { |
| 7980 | if (parameter_count > 0) { |
| 7981 | int mapped_count = Min(argument_count, parameter_count); |
| 7982 | Handle<FixedArray> parameter_map = |
| 7983 | isolate->factory()->NewFixedArray(mapped_count + 2, NOT_TENURED); |
| 7984 | parameter_map->set_map( |
| 7985 | isolate->heap()->non_strict_arguments_elements_map()); |
| 7986 | |
| 7987 | Handle<Map> old_map(result->map()); |
| 7988 | Handle<Map> new_map = |
| 7989 | isolate->factory()->CopyMapDropTransitions(old_map); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 7990 | new_map->set_elements_kind(NON_STRICT_ARGUMENTS_ELEMENTS); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 7991 | |
| 7992 | result->set_map(*new_map); |
| 7993 | result->set_elements(*parameter_map); |
| 7994 | |
| 7995 | // Store the context and the arguments array at the beginning of the |
| 7996 | // parameter map. |
| 7997 | Handle<Context> context(isolate->context()); |
| 7998 | Handle<FixedArray> arguments = |
| 7999 | isolate->factory()->NewFixedArray(argument_count, NOT_TENURED); |
| 8000 | parameter_map->set(0, *context); |
| 8001 | parameter_map->set(1, *arguments); |
| 8002 | |
| 8003 | // Loop over the actual parameters backwards. |
| 8004 | int index = argument_count - 1; |
| 8005 | while (index >= mapped_count) { |
| 8006 | // These go directly in the arguments array and have no |
| 8007 | // corresponding slot in the parameter map. |
| 8008 | arguments->set(index, *(parameters - index - 1)); |
| 8009 | --index; |
| 8010 | } |
| 8011 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8012 | Handle<ScopeInfo> scope_info(callee->shared()->scope_info()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8013 | while (index >= 0) { |
| 8014 | // Detect duplicate names to the right in the parameter list. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8015 | Handle<String> name(scope_info->ParameterName(index)); |
| 8016 | int context_local_count = scope_info->ContextLocalCount(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8017 | bool duplicate = false; |
| 8018 | for (int j = index + 1; j < parameter_count; ++j) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8019 | if (scope_info->ParameterName(j) == *name) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8020 | duplicate = true; |
| 8021 | break; |
| 8022 | } |
| 8023 | } |
| 8024 | |
| 8025 | if (duplicate) { |
| 8026 | // This goes directly in the arguments array with a hole in the |
| 8027 | // parameter map. |
| 8028 | arguments->set(index, *(parameters - index - 1)); |
| 8029 | parameter_map->set_the_hole(index + 2); |
| 8030 | } else { |
| 8031 | // The context index goes in the parameter map with a hole in the |
| 8032 | // arguments array. |
| 8033 | int context_index = -1; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8034 | for (int j = 0; j < context_local_count; ++j) { |
| 8035 | if (scope_info->ContextLocalName(j) == *name) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8036 | context_index = j; |
| 8037 | break; |
| 8038 | } |
| 8039 | } |
| 8040 | ASSERT(context_index >= 0); |
| 8041 | arguments->set_the_hole(index); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8042 | parameter_map->set(index + 2, Smi::FromInt( |
| 8043 | Context::MIN_CONTEXT_SLOTS + context_index)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8044 | } |
| 8045 | |
| 8046 | --index; |
| 8047 | } |
| 8048 | } else { |
| 8049 | // If there is no aliasing, the arguments object elements are not |
| 8050 | // special in any way. |
| 8051 | Handle<FixedArray> elements = |
| 8052 | isolate->factory()->NewFixedArray(argument_count, NOT_TENURED); |
| 8053 | result->set_elements(*elements); |
| 8054 | for (int i = 0; i < argument_count; ++i) { |
| 8055 | elements->set(i, *(parameters - i - 1)); |
| 8056 | } |
| 8057 | } |
| 8058 | } |
| 8059 | return *result; |
| 8060 | } |
| 8061 | |
| 8062 | |
| 8063 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewStrictArgumentsFast) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8064 | NoHandleAllocation ha; |
| 8065 | ASSERT(args.length() == 3); |
| 8066 | |
| 8067 | JSFunction* callee = JSFunction::cast(args[0]); |
| 8068 | Object** parameters = reinterpret_cast<Object**>(args[1]); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8069 | const int length = args.smi_at(2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8070 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8071 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8072 | { MaybeObject* maybe_result = |
| 8073 | isolate->heap()->AllocateArgumentsObject(callee, length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8074 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 8075 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8076 | // Allocate the elements if needed. |
| 8077 | if (length > 0) { |
| 8078 | // Allocate the fixed array. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8079 | Object* obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8080 | { MaybeObject* maybe_obj = isolate->heap()->AllocateRawFixedArray(length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8081 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 8082 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 8083 | |
| 8084 | AssertNoAllocation no_gc; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 8085 | FixedArray* array = reinterpret_cast<FixedArray*>(obj); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 8086 | array->set_map_no_write_barrier(isolate->heap()->fixed_array_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8087 | array->set_length(length); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 8088 | |
| 8089 | WriteBarrierMode mode = array->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8090 | for (int i = 0; i < length; i++) { |
| 8091 | array->set(i, *--parameters, mode); |
| 8092 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8093 | JSObject::cast(result)->set_elements(FixedArray::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8094 | } |
| 8095 | return result; |
| 8096 | } |
| 8097 | |
| 8098 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8099 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewClosure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8100 | HandleScope scope(isolate); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 8101 | ASSERT(args.length() == 3); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 8102 | CONVERT_ARG_CHECKED(Context, context, 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8103 | CONVERT_ARG_CHECKED(SharedFunctionInfo, shared, 1); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 8104 | CONVERT_BOOLEAN_CHECKED(pretenure, args[2]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8105 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8106 | // The caller ensures that we pretenure closures that are assigned |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 8107 | // directly to properties. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 8108 | PretenureFlag pretenure_flag = pretenure ? TENURED : NOT_TENURED; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8109 | Handle<JSFunction> result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8110 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 8111 | context, |
| 8112 | pretenure_flag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8113 | return *result; |
| 8114 | } |
| 8115 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8116 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8117 | // Find the arguments of the JavaScript function invocation that called |
| 8118 | // into C++ code. Collect these in a newly allocated array of handles (possibly |
| 8119 | // prefixed by a number of empty handles). |
| 8120 | static SmartArrayPointer<Handle<Object> > GetCallerArguments( |
| 8121 | int prefix_argc, |
| 8122 | int* total_argc) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8123 | // Find frame containing arguments passed to the caller. |
| 8124 | JavaScriptFrameIterator it; |
| 8125 | JavaScriptFrame* frame = it.frame(); |
| 8126 | List<JSFunction*> functions(2); |
| 8127 | frame->GetFunctions(&functions); |
| 8128 | if (functions.length() > 1) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 8129 | int inlined_jsframe_index = functions.length() - 1; |
| 8130 | JSFunction* inlined_function = functions[inlined_jsframe_index]; |
| 8131 | Vector<SlotRef> args_slots = |
| 8132 | SlotRef::ComputeSlotMappingForArguments( |
| 8133 | frame, |
| 8134 | inlined_jsframe_index, |
| 8135 | inlined_function->shared()->formal_parameter_count()); |
| 8136 | |
| 8137 | int args_count = args_slots.length(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8138 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8139 | *total_argc = prefix_argc + args_count; |
| 8140 | SmartArrayPointer<Handle<Object> > param_data( |
| 8141 | NewArray<Handle<Object> >(*total_argc)); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8142 | for (int i = 0; i < args_count; i++) { |
| 8143 | Handle<Object> val = args_slots[i].GetValue(); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8144 | param_data[prefix_argc + i] = val; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8145 | } |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 8146 | |
| 8147 | args_slots.Dispose(); |
| 8148 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8149 | return param_data; |
| 8150 | } else { |
| 8151 | it.AdvanceToArgumentsFrame(); |
| 8152 | frame = it.frame(); |
| 8153 | int args_count = frame->ComputeParametersCount(); |
| 8154 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8155 | *total_argc = prefix_argc + args_count; |
| 8156 | SmartArrayPointer<Handle<Object> > param_data( |
| 8157 | NewArray<Handle<Object> >(*total_argc)); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8158 | for (int i = 0; i < args_count; i++) { |
| 8159 | Handle<Object> val = Handle<Object>(frame->GetParameter(i)); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8160 | param_data[prefix_argc + i] = val; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8161 | } |
| 8162 | return param_data; |
| 8163 | } |
| 8164 | } |
| 8165 | |
| 8166 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8167 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionBindArguments) { |
| 8168 | HandleScope scope(isolate); |
| 8169 | ASSERT(args.length() == 4); |
| 8170 | CONVERT_ARG_CHECKED(JSFunction, bound_function, 0); |
| 8171 | RUNTIME_ASSERT(args[3]->IsNumber()); |
| 8172 | Handle<Object> bindee = args.at<Object>(1); |
| 8173 | |
| 8174 | // TODO(lrn): Create bound function in C++ code from premade shared info. |
| 8175 | bound_function->shared()->set_bound(true); |
| 8176 | // Get all arguments of calling function (Function.prototype.bind). |
| 8177 | int argc = 0; |
| 8178 | SmartArrayPointer<Handle<Object> > arguments = GetCallerArguments(0, &argc); |
| 8179 | // Don't count the this-arg. |
| 8180 | if (argc > 0) { |
| 8181 | ASSERT(*arguments[0] == args[2]); |
| 8182 | argc--; |
| 8183 | } else { |
| 8184 | ASSERT(args[2]->IsUndefined()); |
| 8185 | } |
| 8186 | // Initialize array of bindings (function, this, and any existing arguments |
| 8187 | // if the function was already bound). |
| 8188 | Handle<FixedArray> new_bindings; |
| 8189 | int i; |
| 8190 | if (bindee->IsJSFunction() && JSFunction::cast(*bindee)->shared()->bound()) { |
| 8191 | Handle<FixedArray> old_bindings( |
| 8192 | JSFunction::cast(*bindee)->function_bindings()); |
| 8193 | new_bindings = |
| 8194 | isolate->factory()->NewFixedArray(old_bindings->length() + argc); |
| 8195 | bindee = Handle<Object>(old_bindings->get(JSFunction::kBoundFunctionIndex)); |
| 8196 | i = 0; |
| 8197 | for (int n = old_bindings->length(); i < n; i++) { |
| 8198 | new_bindings->set(i, old_bindings->get(i)); |
| 8199 | } |
| 8200 | } else { |
| 8201 | int array_size = JSFunction::kBoundArgumentsStartIndex + argc; |
| 8202 | new_bindings = isolate->factory()->NewFixedArray(array_size); |
| 8203 | new_bindings->set(JSFunction::kBoundFunctionIndex, *bindee); |
| 8204 | new_bindings->set(JSFunction::kBoundThisIndex, args[2]); |
| 8205 | i = 2; |
| 8206 | } |
| 8207 | // Copy arguments, skipping the first which is "this_arg". |
| 8208 | for (int j = 0; j < argc; j++, i++) { |
| 8209 | new_bindings->set(i, *arguments[j + 1]); |
| 8210 | } |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 8211 | new_bindings->set_map_no_write_barrier( |
| 8212 | isolate->heap()->fixed_cow_array_map()); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8213 | bound_function->set_function_bindings(*new_bindings); |
| 8214 | |
| 8215 | // Update length. |
| 8216 | Handle<String> length_symbol = isolate->factory()->length_symbol(); |
| 8217 | Handle<Object> new_length(args.at<Object>(3)); |
| 8218 | PropertyAttributes attr = |
| 8219 | static_cast<PropertyAttributes>(DONT_DELETE | DONT_ENUM | READ_ONLY); |
| 8220 | ForceSetProperty(bound_function, length_symbol, new_length, attr); |
| 8221 | return *bound_function; |
| 8222 | } |
| 8223 | |
| 8224 | |
| 8225 | RUNTIME_FUNCTION(MaybeObject*, Runtime_BoundFunctionGetBindings) { |
| 8226 | HandleScope handles(isolate); |
| 8227 | ASSERT(args.length() == 1); |
| 8228 | CONVERT_ARG_CHECKED(JSReceiver, callable, 0); |
| 8229 | if (callable->IsJSFunction()) { |
| 8230 | Handle<JSFunction> function = Handle<JSFunction>::cast(callable); |
| 8231 | if (function->shared()->bound()) { |
| 8232 | Handle<FixedArray> bindings(function->function_bindings()); |
| 8233 | ASSERT(bindings->map() == isolate->heap()->fixed_cow_array_map()); |
| 8234 | return *isolate->factory()->NewJSArrayWithElements(bindings); |
| 8235 | } |
| 8236 | } |
| 8237 | return isolate->heap()->undefined_value(); |
| 8238 | } |
| 8239 | |
| 8240 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8241 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewObjectFromBound) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8242 | HandleScope scope(isolate); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8243 | ASSERT(args.length() == 1); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8244 | // First argument is a function to use as a constructor. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 8245 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8246 | RUNTIME_ASSERT(function->shared()->bound()); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 8247 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8248 | // The argument is a bound function. Extract its bound arguments |
| 8249 | // and callable. |
| 8250 | Handle<FixedArray> bound_args = |
| 8251 | Handle<FixedArray>(FixedArray::cast(function->function_bindings())); |
| 8252 | int bound_argc = bound_args->length() - JSFunction::kBoundArgumentsStartIndex; |
| 8253 | Handle<Object> bound_function( |
| 8254 | JSReceiver::cast(bound_args->get(JSFunction::kBoundFunctionIndex))); |
| 8255 | ASSERT(!bound_function->IsJSFunction() || |
| 8256 | !Handle<JSFunction>::cast(bound_function)->shared()->bound()); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 8257 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8258 | int total_argc = 0; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8259 | SmartArrayPointer<Handle<Object> > param_data = |
| 8260 | GetCallerArguments(bound_argc, &total_argc); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8261 | for (int i = 0; i < bound_argc; i++) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8262 | param_data[i] = Handle<Object>(bound_args->get( |
| 8263 | JSFunction::kBoundArgumentsStartIndex + i)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 8264 | } |
| 8265 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8266 | if (!bound_function->IsJSFunction()) { |
| 8267 | bool exception_thrown; |
| 8268 | bound_function = Execution::TryGetConstructorDelegate(bound_function, |
| 8269 | &exception_thrown); |
| 8270 | if (exception_thrown) return Failure::Exception(); |
| 8271 | } |
| 8272 | ASSERT(bound_function->IsJSFunction()); |
| 8273 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 8274 | bool exception = false; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8275 | Handle<Object> result = |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8276 | Execution::New(Handle<JSFunction>::cast(bound_function), |
| 8277 | total_argc, *param_data, &exception); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 8278 | if (exception) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8279 | return Failure::Exception(); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 8280 | } |
| 8281 | ASSERT(!result.is_null()); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 8282 | return *result; |
| 8283 | } |
| 8284 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8285 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8286 | static void TrySettingInlineConstructStub(Isolate* isolate, |
| 8287 | Handle<JSFunction> function) { |
| 8288 | Handle<Object> prototype = isolate->factory()->null_value(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 8289 | if (function->has_instance_prototype()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8290 | prototype = Handle<Object>(function->instance_prototype(), isolate); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 8291 | } |
| 8292 | if (function->shared()->CanGenerateInlineConstructor(*prototype)) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8293 | ConstructStubCompiler compiler(isolate); |
| 8294 | Handle<Code> code = compiler.CompileConstructStub(function); |
| 8295 | function->shared()->set_construct_stub(*code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8296 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8297 | } |
| 8298 | |
| 8299 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8300 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewObject) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8301 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8302 | ASSERT(args.length() == 1); |
| 8303 | |
| 8304 | Handle<Object> constructor = args.at<Object>(0); |
| 8305 | |
| 8306 | // If the constructor isn't a proper function we throw a type error. |
| 8307 | if (!constructor->IsJSFunction()) { |
| 8308 | Vector< Handle<Object> > arguments = HandleVector(&constructor, 1); |
| 8309 | Handle<Object> type_error = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8310 | isolate->factory()->NewTypeError("not_constructor", arguments); |
| 8311 | return isolate->Throw(*type_error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8312 | } |
| 8313 | |
| 8314 | Handle<JSFunction> function = Handle<JSFunction>::cast(constructor); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8315 | |
| 8316 | // If function should not have prototype, construction is not allowed. In this |
| 8317 | // case generated code bailouts here, since function has no initial_map. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8318 | if (!function->should_have_prototype() && !function->shared()->bound()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8319 | Vector< Handle<Object> > arguments = HandleVector(&constructor, 1); |
| 8320 | Handle<Object> type_error = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8321 | isolate->factory()->NewTypeError("not_constructor", arguments); |
| 8322 | return isolate->Throw(*type_error); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8323 | } |
| 8324 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8325 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8326 | Debug* debug = isolate->debug(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8327 | // Handle stepping into constructors if step into is active. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8328 | if (debug->StepInActive()) { |
| 8329 | debug->HandleStepIn(function, Handle<Object>::null(), 0, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8330 | } |
| 8331 | #endif |
| 8332 | |
| 8333 | if (function->has_initial_map()) { |
| 8334 | if (function->initial_map()->instance_type() == JS_FUNCTION_TYPE) { |
| 8335 | // The 'Function' function ignores the receiver object when |
| 8336 | // called using 'new' and creates a new JSFunction object that |
| 8337 | // is returned. The receiver object is only used for error |
| 8338 | // reporting if an error occurs when constructing the new |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8339 | // JSFunction. FACTORY->NewJSObject() should not be used to |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8340 | // allocate JSFunctions since it does not properly initialize |
| 8341 | // the shared part of the function. Since the receiver is |
| 8342 | // ignored anyway, we use the global object as the receiver |
| 8343 | // instead of a new JSFunction object. This way, errors are |
| 8344 | // reported the same way whether or not 'Function' is called |
| 8345 | // using 'new'. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8346 | return isolate->context()->global(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8347 | } |
| 8348 | } |
| 8349 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8350 | // The function should be compiled for the optimization hints to be |
| 8351 | // available. We cannot use EnsureCompiled because that forces a |
| 8352 | // compilation through the shared function info which makes it |
| 8353 | // impossible for us to optimize. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8354 | if (!function->is_compiled()) { |
| 8355 | JSFunction::CompileLazy(function, CLEAR_EXCEPTION); |
| 8356 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8357 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8358 | Handle<SharedFunctionInfo> shared(function->shared(), isolate); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 8359 | if (!function->has_initial_map() && |
| 8360 | shared->IsInobjectSlackTrackingInProgress()) { |
| 8361 | // The tracking is already in progress for another function. We can only |
| 8362 | // track one initial_map at a time, so we force the completion before the |
| 8363 | // function is called as a constructor for the first time. |
| 8364 | shared->CompleteInobjectSlackTracking(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 8365 | } |
| 8366 | |
| 8367 | bool first_allocation = !shared->live_objects_may_exist(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8368 | Handle<JSObject> result = isolate->factory()->NewJSObject(function); |
| 8369 | RETURN_IF_EMPTY_HANDLE(isolate, result); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 8370 | // Delay setting the stub if inobject slack tracking is in progress. |
| 8371 | if (first_allocation && !shared->IsInobjectSlackTrackingInProgress()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8372 | TrySettingInlineConstructStub(isolate, function); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8373 | } |
| 8374 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8375 | isolate->counters()->constructed_objects()->Increment(); |
| 8376 | isolate->counters()->constructed_objects_runtime()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8377 | |
| 8378 | return *result; |
| 8379 | } |
| 8380 | |
| 8381 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8382 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FinalizeInstanceSize) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8383 | HandleScope scope(isolate); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 8384 | ASSERT(args.length() == 1); |
| 8385 | |
| 8386 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 8387 | function->shared()->CompleteInobjectSlackTracking(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8388 | TrySettingInlineConstructStub(isolate, function); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 8389 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8390 | return isolate->heap()->undefined_value(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 8391 | } |
| 8392 | |
| 8393 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8394 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LazyCompile) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8395 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8396 | ASSERT(args.length() == 1); |
| 8397 | |
| 8398 | Handle<JSFunction> function = args.at<JSFunction>(0); |
| 8399 | #ifdef DEBUG |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 8400 | if (FLAG_trace_lazy && !function->shared()->is_compiled()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8401 | PrintF("[lazy: "); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8402 | function->PrintName(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8403 | PrintF("]\n"); |
| 8404 | } |
| 8405 | #endif |
| 8406 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 8407 | // Compile the target function. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8408 | ASSERT(!function->is_compiled()); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8409 | if (!JSFunction::CompileLazy(function, KEEP_EXCEPTION)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8410 | return Failure::Exception(); |
| 8411 | } |
| 8412 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8413 | // All done. Return the compiled code. |
| 8414 | ASSERT(function->is_compiled()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8415 | return function->code(); |
| 8416 | } |
| 8417 | |
| 8418 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8419 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LazyRecompile) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8420 | HandleScope scope(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8421 | ASSERT(args.length() == 1); |
| 8422 | Handle<JSFunction> function = args.at<JSFunction>(0); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 8423 | |
| 8424 | // If the function is not compiled ignore the lazy |
| 8425 | // recompilation. This can happen if the debugger is activated and |
| 8426 | // the function is returned to the not compiled state. |
| 8427 | if (!function->shared()->is_compiled()) { |
| 8428 | function->ReplaceCode(function->shared()->code()); |
| 8429 | return function->code(); |
| 8430 | } |
| 8431 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8432 | // If the function is not optimizable or debugger is active continue using the |
| 8433 | // code from the full compiler. |
| 8434 | if (!function->shared()->code()->optimizable() || |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 8435 | isolate->DebuggerHasBreakPoints()) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 8436 | if (FLAG_trace_opt) { |
| 8437 | PrintF("[failed to optimize "); |
| 8438 | function->PrintName(); |
| 8439 | PrintF(": is code optimizable: %s, is debugger enabled: %s]\n", |
| 8440 | function->shared()->code()->optimizable() ? "T" : "F", |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 8441 | isolate->DebuggerHasBreakPoints() ? "T" : "F"); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 8442 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8443 | function->ReplaceCode(function->shared()->code()); |
| 8444 | return function->code(); |
| 8445 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8446 | if (JSFunction::CompileOptimized(function, |
| 8447 | AstNode::kNoNumber, |
| 8448 | CLEAR_EXCEPTION)) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8449 | return function->code(); |
| 8450 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 8451 | if (FLAG_trace_opt) { |
| 8452 | PrintF("[failed to optimize "); |
| 8453 | function->PrintName(); |
| 8454 | PrintF(": optimized compilation failed]\n"); |
| 8455 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8456 | function->ReplaceCode(function->shared()->code()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 8457 | return function->code(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8458 | } |
| 8459 | |
| 8460 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8461 | class ActivationsFinder : public ThreadVisitor { |
| 8462 | public: |
| 8463 | explicit ActivationsFinder(JSFunction* function) |
| 8464 | : function_(function), has_activations_(false) {} |
| 8465 | |
| 8466 | void VisitThread(Isolate* isolate, ThreadLocalTop* top) { |
| 8467 | if (has_activations_) return; |
| 8468 | |
| 8469 | for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) { |
| 8470 | JavaScriptFrame* frame = it.frame(); |
| 8471 | if (frame->is_optimized() && frame->function() == function_) { |
| 8472 | has_activations_ = true; |
| 8473 | return; |
| 8474 | } |
| 8475 | } |
| 8476 | } |
| 8477 | |
| 8478 | bool has_activations() { return has_activations_; } |
| 8479 | |
| 8480 | private: |
| 8481 | JSFunction* function_; |
| 8482 | bool has_activations_; |
| 8483 | }; |
| 8484 | |
| 8485 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8486 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NotifyDeoptimized) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8487 | HandleScope scope(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8488 | ASSERT(args.length() == 1); |
| 8489 | RUNTIME_ASSERT(args[0]->IsSmi()); |
| 8490 | Deoptimizer::BailoutType type = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8491 | static_cast<Deoptimizer::BailoutType>(args.smi_at(0)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8492 | Deoptimizer* deoptimizer = Deoptimizer::Grab(isolate); |
| 8493 | ASSERT(isolate->heap()->IsAllocationAllowed()); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 8494 | int jsframes = deoptimizer->jsframe_count(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8495 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8496 | deoptimizer->MaterializeHeapNumbers(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8497 | delete deoptimizer; |
| 8498 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8499 | JavaScriptFrameIterator it(isolate); |
| 8500 | JavaScriptFrame* frame = NULL; |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 8501 | for (int i = 0; i < jsframes - 1; i++) it.Advance(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8502 | frame = it.frame(); |
| 8503 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8504 | RUNTIME_ASSERT(frame->function()->IsJSFunction()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8505 | Handle<JSFunction> function(JSFunction::cast(frame->function()), isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8506 | Handle<Object> arguments; |
| 8507 | for (int i = frame->ComputeExpressionsCount() - 1; i >= 0; --i) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8508 | if (frame->GetExpression(i) == isolate->heap()->arguments_marker()) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8509 | if (arguments.is_null()) { |
| 8510 | // FunctionGetArguments can't throw an exception, so cast away the |
| 8511 | // doubt with an assert. |
| 8512 | arguments = Handle<Object>( |
| 8513 | Accessors::FunctionGetArguments(*function, |
| 8514 | NULL)->ToObjectUnchecked()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8515 | ASSERT(*arguments != isolate->heap()->null_value()); |
| 8516 | ASSERT(*arguments != isolate->heap()->undefined_value()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8517 | } |
| 8518 | frame->SetExpression(i, *arguments); |
| 8519 | } |
| 8520 | } |
| 8521 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8522 | if (type == Deoptimizer::EAGER) { |
| 8523 | RUNTIME_ASSERT(function->IsOptimized()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8524 | } |
| 8525 | |
| 8526 | // Avoid doing too much work when running with --always-opt and keep |
| 8527 | // the optimized code around. |
| 8528 | if (FLAG_always_opt || type == Deoptimizer::LAZY) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8529 | return isolate->heap()->undefined_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8530 | } |
| 8531 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8532 | // Find other optimized activations of the function. |
| 8533 | bool has_other_activations = false; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8534 | while (!it.done()) { |
| 8535 | JavaScriptFrame* frame = it.frame(); |
| 8536 | if (frame->is_optimized() && frame->function() == *function) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8537 | has_other_activations = true; |
| 8538 | break; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8539 | } |
| 8540 | it.Advance(); |
| 8541 | } |
| 8542 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8543 | if (!has_other_activations) { |
| 8544 | ActivationsFinder activations_finder(*function); |
| 8545 | isolate->thread_manager()->IterateArchivedThreads(&activations_finder); |
| 8546 | has_other_activations = activations_finder.has_activations(); |
| 8547 | } |
| 8548 | |
| 8549 | if (!has_other_activations) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8550 | if (FLAG_trace_deopt) { |
| 8551 | PrintF("[removing optimized code for: "); |
| 8552 | function->PrintName(); |
| 8553 | PrintF("]\n"); |
| 8554 | } |
| 8555 | function->ReplaceCode(function->shared()->code()); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 8556 | } else { |
| 8557 | Deoptimizer::DeoptimizeFunction(*function); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8558 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8559 | return isolate->heap()->undefined_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8560 | } |
| 8561 | |
| 8562 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8563 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NotifyOSR) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8564 | Deoptimizer* deoptimizer = Deoptimizer::Grab(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8565 | delete deoptimizer; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8566 | return isolate->heap()->undefined_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8567 | } |
| 8568 | |
| 8569 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8570 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeoptimizeFunction) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8571 | HandleScope scope(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8572 | ASSERT(args.length() == 1); |
| 8573 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8574 | if (!function->IsOptimized()) return isolate->heap()->undefined_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8575 | |
| 8576 | Deoptimizer::DeoptimizeFunction(*function); |
| 8577 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8578 | return isolate->heap()->undefined_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8579 | } |
| 8580 | |
| 8581 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8582 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RunningInSimulator) { |
| 8583 | #if defined(USE_SIMULATOR) |
| 8584 | return isolate->heap()->true_value(); |
| 8585 | #else |
| 8586 | return isolate->heap()->false_value(); |
| 8587 | #endif |
| 8588 | } |
| 8589 | |
| 8590 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8591 | RUNTIME_FUNCTION(MaybeObject*, Runtime_OptimizeFunctionOnNextCall) { |
| 8592 | HandleScope scope(isolate); |
| 8593 | ASSERT(args.length() == 1); |
| 8594 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 8595 | if (!function->IsOptimizable()) return isolate->heap()->undefined_value(); |
| 8596 | function->MarkForLazyRecompilation(); |
| 8597 | return isolate->heap()->undefined_value(); |
| 8598 | } |
| 8599 | |
| 8600 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 8601 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetOptimizationStatus) { |
| 8602 | HandleScope scope(isolate); |
| 8603 | ASSERT(args.length() == 1); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8604 | // The least significant bit (after untagging) indicates whether the |
| 8605 | // function is currently optimized, regardless of reason. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 8606 | if (!V8::UseCrankshaft()) { |
| 8607 | return Smi::FromInt(4); // 4 == "never". |
| 8608 | } |
| 8609 | if (FLAG_always_opt) { |
| 8610 | return Smi::FromInt(3); // 3 == "always". |
| 8611 | } |
| 8612 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 8613 | return function->IsOptimized() ? Smi::FromInt(1) // 1 == "yes". |
| 8614 | : Smi::FromInt(2); // 2 == "no". |
| 8615 | } |
| 8616 | |
| 8617 | |
| 8618 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetOptimizationCount) { |
| 8619 | HandleScope scope(isolate); |
| 8620 | ASSERT(args.length() == 1); |
| 8621 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 8622 | return Smi::FromInt(function->shared()->opt_count()); |
| 8623 | } |
| 8624 | |
| 8625 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8626 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CompileForOnStackReplacement) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8627 | HandleScope scope(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8628 | ASSERT(args.length() == 1); |
| 8629 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 8630 | |
| 8631 | // We're not prepared to handle a function with arguments object. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8632 | ASSERT(!function->shared()->uses_arguments()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8633 | |
| 8634 | // We have hit a back edge in an unoptimized frame for a function that was |
| 8635 | // selected for on-stack replacement. Find the unoptimized code object. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8636 | Handle<Code> unoptimized(function->shared()->code(), isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8637 | // Keep track of whether we've succeeded in optimizing. |
| 8638 | bool succeeded = unoptimized->optimizable(); |
| 8639 | if (succeeded) { |
| 8640 | // If we are trying to do OSR when there are already optimized |
| 8641 | // activations of the function, it means (a) the function is directly or |
| 8642 | // indirectly recursive and (b) an optimized invocation has been |
| 8643 | // deoptimized so that we are currently in an unoptimized activation. |
| 8644 | // Check for optimized activations of this function. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8645 | JavaScriptFrameIterator it(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8646 | while (succeeded && !it.done()) { |
| 8647 | JavaScriptFrame* frame = it.frame(); |
| 8648 | succeeded = !frame->is_optimized() || frame->function() != *function; |
| 8649 | it.Advance(); |
| 8650 | } |
| 8651 | } |
| 8652 | |
| 8653 | int ast_id = AstNode::kNoNumber; |
| 8654 | if (succeeded) { |
| 8655 | // The top JS function is this one, the PC is somewhere in the |
| 8656 | // unoptimized code. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8657 | JavaScriptFrameIterator it(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8658 | JavaScriptFrame* frame = it.frame(); |
| 8659 | ASSERT(frame->function() == *function); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8660 | ASSERT(frame->LookupCode() == *unoptimized); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8661 | ASSERT(unoptimized->contains(frame->pc())); |
| 8662 | |
| 8663 | // Use linear search of the unoptimized code's stack check table to find |
| 8664 | // the AST id matching the PC. |
| 8665 | Address start = unoptimized->instruction_start(); |
| 8666 | unsigned target_pc_offset = static_cast<unsigned>(frame->pc() - start); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8667 | Address table_cursor = start + unoptimized->stack_check_table_offset(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8668 | uint32_t table_length = Memory::uint32_at(table_cursor); |
| 8669 | table_cursor += kIntSize; |
| 8670 | for (unsigned i = 0; i < table_length; ++i) { |
| 8671 | // Table entries are (AST id, pc offset) pairs. |
| 8672 | uint32_t pc_offset = Memory::uint32_at(table_cursor + kIntSize); |
| 8673 | if (pc_offset == target_pc_offset) { |
| 8674 | ast_id = static_cast<int>(Memory::uint32_at(table_cursor)); |
| 8675 | break; |
| 8676 | } |
| 8677 | table_cursor += 2 * kIntSize; |
| 8678 | } |
| 8679 | ASSERT(ast_id != AstNode::kNoNumber); |
| 8680 | if (FLAG_trace_osr) { |
| 8681 | PrintF("[replacing on-stack at AST id %d in ", ast_id); |
| 8682 | function->PrintName(); |
| 8683 | PrintF("]\n"); |
| 8684 | } |
| 8685 | |
| 8686 | // Try to compile the optimized code. A true return value from |
| 8687 | // CompileOptimized means that compilation succeeded, not necessarily |
| 8688 | // that optimization succeeded. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8689 | if (JSFunction::CompileOptimized(function, ast_id, CLEAR_EXCEPTION) && |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 8690 | function->IsOptimized()) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8691 | DeoptimizationInputData* data = DeoptimizationInputData::cast( |
| 8692 | function->code()->deoptimization_data()); |
| 8693 | if (data->OsrPcOffset()->value() >= 0) { |
| 8694 | if (FLAG_trace_osr) { |
| 8695 | PrintF("[on-stack replacement offset %d in optimized code]\n", |
| 8696 | data->OsrPcOffset()->value()); |
| 8697 | } |
| 8698 | ASSERT(data->OsrAstId()->value() == ast_id); |
| 8699 | } else { |
| 8700 | // We may never generate the desired OSR entry if we emit an |
| 8701 | // early deoptimize. |
| 8702 | succeeded = false; |
| 8703 | } |
| 8704 | } else { |
| 8705 | succeeded = false; |
| 8706 | } |
| 8707 | } |
| 8708 | |
| 8709 | // Revert to the original stack checks in the original unoptimized code. |
| 8710 | if (FLAG_trace_osr) { |
| 8711 | PrintF("[restoring original stack checks in "); |
| 8712 | function->PrintName(); |
| 8713 | PrintF("]\n"); |
| 8714 | } |
| 8715 | StackCheckStub check_stub; |
| 8716 | Handle<Code> check_code = check_stub.GetCode(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8717 | Handle<Code> replacement_code = isolate->builtins()->OnStackReplacement(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8718 | Deoptimizer::RevertStackCheckCode(*unoptimized, |
| 8719 | *check_code, |
| 8720 | *replacement_code); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8721 | |
| 8722 | // Allow OSR only at nesting level zero again. |
| 8723 | unoptimized->set_allow_osr_at_loop_nesting_level(0); |
| 8724 | |
| 8725 | // If the optimization attempt succeeded, return the AST id tagged as a |
| 8726 | // smi. This tells the builtin that we need to translate the unoptimized |
| 8727 | // frame to an optimized one. |
| 8728 | if (succeeded) { |
| 8729 | ASSERT(function->code()->kind() == Code::OPTIMIZED_FUNCTION); |
| 8730 | return Smi::FromInt(ast_id); |
| 8731 | } else { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 8732 | if (function->IsMarkedForLazyRecompilation()) { |
| 8733 | function->ReplaceCode(function->shared()->code()); |
| 8734 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8735 | return Smi::FromInt(-1); |
| 8736 | } |
| 8737 | } |
| 8738 | |
| 8739 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 8740 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CheckIsBootstrapping) { |
| 8741 | RUNTIME_ASSERT(isolate->bootstrapper()->IsActive()); |
| 8742 | return isolate->heap()->undefined_value(); |
| 8743 | } |
| 8744 | |
| 8745 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8746 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Call) { |
| 8747 | HandleScope scope(isolate); |
| 8748 | ASSERT(args.length() >= 2); |
| 8749 | CONVERT_CHECKED(JSReceiver, fun, args[args.length() - 1]); |
| 8750 | Object* receiver = args[0]; |
| 8751 | int argc = args.length() - 2; |
| 8752 | |
| 8753 | // If there are too many arguments, allocate argv via malloc. |
| 8754 | const int argv_small_size = 10; |
| 8755 | Handle<Object> argv_small_buffer[argv_small_size]; |
| 8756 | SmartArrayPointer<Handle<Object> > argv_large_buffer; |
| 8757 | Handle<Object>* argv = argv_small_buffer; |
| 8758 | if (argc > argv_small_size) { |
| 8759 | argv = new Handle<Object>[argc]; |
| 8760 | if (argv == NULL) return isolate->StackOverflow(); |
| 8761 | argv_large_buffer = SmartArrayPointer<Handle<Object> >(argv); |
| 8762 | } |
| 8763 | |
| 8764 | for (int i = 0; i < argc; ++i) { |
| 8765 | MaybeObject* maybe = args[1 + i]; |
| 8766 | Object* object; |
| 8767 | if (!maybe->To<Object>(&object)) return maybe; |
| 8768 | argv[i] = Handle<Object>(object); |
| 8769 | } |
| 8770 | |
| 8771 | bool threw; |
| 8772 | Handle<JSReceiver> hfun(fun); |
| 8773 | Handle<Object> hreceiver(receiver); |
| 8774 | Handle<Object> result = |
| 8775 | Execution::Call(hfun, hreceiver, argc, argv, &threw, true); |
| 8776 | |
| 8777 | if (threw) return Failure::Exception(); |
| 8778 | return *result; |
| 8779 | } |
| 8780 | |
| 8781 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 8782 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Apply) { |
| 8783 | HandleScope scope(isolate); |
| 8784 | ASSERT(args.length() == 5); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 8785 | CONVERT_ARG_CHECKED(JSReceiver, fun, 0); |
| 8786 | Handle<Object> receiver = args.at<Object>(1); |
| 8787 | CONVERT_ARG_CHECKED(JSObject, arguments, 2); |
| 8788 | CONVERT_SMI_ARG_CHECKED(offset, 3); |
| 8789 | CONVERT_SMI_ARG_CHECKED(argc, 4); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 8790 | ASSERT(offset >= 0); |
| 8791 | ASSERT(argc >= 0); |
| 8792 | |
| 8793 | // If there are too many arguments, allocate argv via malloc. |
| 8794 | const int argv_small_size = 10; |
| 8795 | Handle<Object> argv_small_buffer[argv_small_size]; |
| 8796 | SmartArrayPointer<Handle<Object> > argv_large_buffer; |
| 8797 | Handle<Object>* argv = argv_small_buffer; |
| 8798 | if (argc > argv_small_size) { |
| 8799 | argv = new Handle<Object>[argc]; |
| 8800 | if (argv == NULL) return isolate->StackOverflow(); |
| 8801 | argv_large_buffer = SmartArrayPointer<Handle<Object> >(argv); |
| 8802 | } |
| 8803 | |
| 8804 | for (int i = 0; i < argc; ++i) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 8805 | argv[i] = Object::GetElement(arguments, offset + i); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 8806 | } |
| 8807 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8808 | bool threw; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8809 | Handle<Object> result = |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 8810 | Execution::Call(fun, receiver, argc, argv, &threw, true); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 8811 | |
| 8812 | if (threw) return Failure::Exception(); |
| 8813 | return *result; |
| 8814 | } |
| 8815 | |
| 8816 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8817 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFunctionDelegate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8818 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8819 | ASSERT(args.length() == 1); |
| 8820 | RUNTIME_ASSERT(!args[0]->IsJSFunction()); |
| 8821 | return *Execution::GetFunctionDelegate(args.at<Object>(0)); |
| 8822 | } |
| 8823 | |
| 8824 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8825 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetConstructorDelegate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8826 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8827 | ASSERT(args.length() == 1); |
| 8828 | RUNTIME_ASSERT(!args[0]->IsJSFunction()); |
| 8829 | return *Execution::GetConstructorDelegate(args.at<Object>(0)); |
| 8830 | } |
| 8831 | |
| 8832 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8833 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewFunctionContext) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8834 | NoHandleAllocation ha; |
| 8835 | ASSERT(args.length() == 1); |
| 8836 | |
| 8837 | CONVERT_CHECKED(JSFunction, function, args[0]); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8838 | int length = function->shared()->scope_info()->ContextLength(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8839 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8840 | { MaybeObject* maybe_result = |
| 8841 | isolate->heap()->AllocateFunctionContext(length, function); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8842 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 8843 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8844 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8845 | isolate->set_context(Context::cast(result)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8846 | |
| 8847 | return result; // non-failure |
| 8848 | } |
| 8849 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8850 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8851 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushWithContext) { |
| 8852 | NoHandleAllocation ha; |
| 8853 | ASSERT(args.length() == 2); |
| 8854 | JSObject* extension_object; |
| 8855 | if (args[0]->IsJSObject()) { |
| 8856 | extension_object = JSObject::cast(args[0]); |
| 8857 | } else { |
| 8858 | // Convert the object to a proper JavaScript object. |
| 8859 | MaybeObject* maybe_js_object = args[0]->ToObject(); |
| 8860 | if (!maybe_js_object->To(&extension_object)) { |
| 8861 | if (Failure::cast(maybe_js_object)->IsInternalError()) { |
| 8862 | HandleScope scope(isolate); |
| 8863 | Handle<Object> handle = args.at<Object>(0); |
| 8864 | Handle<Object> result = |
| 8865 | isolate->factory()->NewTypeError("with_expression", |
| 8866 | HandleVector(&handle, 1)); |
| 8867 | return isolate->Throw(*result); |
| 8868 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8869 | return maybe_js_object; |
| 8870 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8871 | } |
| 8872 | } |
| 8873 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8874 | JSFunction* function; |
| 8875 | if (args[1]->IsSmi()) { |
| 8876 | // A smi sentinel indicates a context nested inside global code rather |
| 8877 | // than some function. There is a canonical empty function that can be |
| 8878 | // gotten from the global context. |
| 8879 | function = isolate->context()->global_context()->closure(); |
| 8880 | } else { |
| 8881 | function = JSFunction::cast(args[1]); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8882 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8883 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8884 | Context* context; |
| 8885 | MaybeObject* maybe_context = |
| 8886 | isolate->heap()->AllocateWithContext(function, |
| 8887 | isolate->context(), |
| 8888 | extension_object); |
| 8889 | if (!maybe_context->To(&context)) return maybe_context; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8890 | isolate->set_context(context); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8891 | return context; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8892 | } |
| 8893 | |
| 8894 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8895 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushCatchContext) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8896 | NoHandleAllocation ha; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8897 | ASSERT(args.length() == 3); |
| 8898 | String* name = String::cast(args[0]); |
| 8899 | Object* thrown_object = args[1]; |
| 8900 | JSFunction* function; |
| 8901 | if (args[2]->IsSmi()) { |
| 8902 | // A smi sentinel indicates a context nested inside global code rather |
| 8903 | // than some function. There is a canonical empty function that can be |
| 8904 | // gotten from the global context. |
| 8905 | function = isolate->context()->global_context()->closure(); |
| 8906 | } else { |
| 8907 | function = JSFunction::cast(args[2]); |
| 8908 | } |
| 8909 | Context* context; |
| 8910 | MaybeObject* maybe_context = |
| 8911 | isolate->heap()->AllocateCatchContext(function, |
| 8912 | isolate->context(), |
| 8913 | name, |
| 8914 | thrown_object); |
| 8915 | if (!maybe_context->To(&context)) return maybe_context; |
| 8916 | isolate->set_context(context); |
| 8917 | return context; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8918 | } |
| 8919 | |
| 8920 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 8921 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushBlockContext) { |
| 8922 | NoHandleAllocation ha; |
| 8923 | ASSERT(args.length() == 2); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8924 | ScopeInfo* scope_info = ScopeInfo::cast(args[0]); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 8925 | JSFunction* function; |
| 8926 | if (args[1]->IsSmi()) { |
| 8927 | // A smi sentinel indicates a context nested inside global code rather |
| 8928 | // than some function. There is a canonical empty function that can be |
| 8929 | // gotten from the global context. |
| 8930 | function = isolate->context()->global_context()->closure(); |
| 8931 | } else { |
| 8932 | function = JSFunction::cast(args[1]); |
| 8933 | } |
| 8934 | Context* context; |
| 8935 | MaybeObject* maybe_context = |
| 8936 | isolate->heap()->AllocateBlockContext(function, |
| 8937 | isolate->context(), |
| 8938 | scope_info); |
| 8939 | if (!maybe_context->To(&context)) return maybe_context; |
| 8940 | isolate->set_context(context); |
| 8941 | return context; |
| 8942 | } |
| 8943 | |
| 8944 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8945 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeleteContextSlot) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8946 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8947 | ASSERT(args.length() == 2); |
| 8948 | |
| 8949 | CONVERT_ARG_CHECKED(Context, context, 0); |
| 8950 | CONVERT_ARG_CHECKED(String, name, 1); |
| 8951 | |
| 8952 | int index; |
| 8953 | PropertyAttributes attributes; |
| 8954 | ContextLookupFlags flags = FOLLOW_CHAINS; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 8955 | BindingFlags binding_flags; |
| 8956 | Handle<Object> holder = context->Lookup(name, |
| 8957 | flags, |
| 8958 | &index, |
| 8959 | &attributes, |
| 8960 | &binding_flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8961 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8962 | // If the slot was not found the result is true. |
| 8963 | if (holder.is_null()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8964 | return isolate->heap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8965 | } |
| 8966 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8967 | // If the slot was found in a context, it should be DONT_DELETE. |
| 8968 | if (holder->IsContext()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8969 | return isolate->heap()->false_value(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8970 | } |
| 8971 | |
| 8972 | // The slot was found in a JSObject, either a context extension object, |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8973 | // the global object, or the subject of a with. Try to delete it |
| 8974 | // (respecting DONT_DELETE). |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8975 | Handle<JSObject> object = Handle<JSObject>::cast(holder); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 8976 | return object->DeleteProperty(*name, JSReceiver::NORMAL_DELETION); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8977 | } |
| 8978 | |
| 8979 | |
| 8980 | // A mechanism to return a pair of Object pointers in registers (if possible). |
| 8981 | // How this is achieved is calling convention-dependent. |
| 8982 | // All currently supported x86 compiles uses calling conventions that are cdecl |
| 8983 | // variants where a 64-bit value is returned in two 32-bit registers |
| 8984 | // (edx:eax on ia32, r1:r0 on ARM). |
| 8985 | // In AMD-64 calling convention a struct of two pointers is returned in rdx:rax. |
| 8986 | // In Win64 calling convention, a struct of two pointers is returned in memory, |
| 8987 | // allocated by the caller, and passed as a pointer in a hidden first parameter. |
| 8988 | #ifdef V8_HOST_ARCH_64_BIT |
| 8989 | struct ObjectPair { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8990 | MaybeObject* x; |
| 8991 | MaybeObject* y; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8992 | }; |
| 8993 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8994 | static inline ObjectPair MakePair(MaybeObject* x, MaybeObject* y) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8995 | ObjectPair result = {x, y}; |
| 8996 | // Pointers x and y returned in rax and rdx, in AMD-x64-abi. |
| 8997 | // In Win64 they are assigned to a hidden first argument. |
| 8998 | return result; |
| 8999 | } |
| 9000 | #else |
| 9001 | typedef uint64_t ObjectPair; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9002 | static inline ObjectPair MakePair(MaybeObject* x, MaybeObject* y) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9003 | return reinterpret_cast<uint32_t>(x) | |
| 9004 | (reinterpret_cast<ObjectPair>(y) << 32); |
| 9005 | } |
| 9006 | #endif |
| 9007 | |
| 9008 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9009 | static inline MaybeObject* Unhole(Heap* heap, |
| 9010 | MaybeObject* x, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9011 | PropertyAttributes attributes) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9012 | ASSERT(!x->IsTheHole() || (attributes & READ_ONLY) != 0); |
| 9013 | USE(attributes); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9014 | return x->IsTheHole() ? heap->undefined_value() : x; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9015 | } |
| 9016 | |
| 9017 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9018 | static Object* ComputeReceiverForNonGlobal(Isolate* isolate, |
| 9019 | JSObject* holder) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9020 | ASSERT(!holder->IsGlobalObject()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9021 | Context* top = isolate->context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9022 | // Get the context extension function. |
| 9023 | JSFunction* context_extension_function = |
| 9024 | top->global_context()->context_extension_function(); |
| 9025 | // If the holder isn't a context extension object, we just return it |
| 9026 | // as the receiver. This allows arguments objects to be used as |
| 9027 | // receivers, but only if they are put in the context scope chain |
| 9028 | // explicitly via a with-statement. |
| 9029 | Object* constructor = holder->map()->constructor(); |
| 9030 | if (constructor != context_extension_function) return holder; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9031 | // Fall back to using the global object as the implicit receiver if |
| 9032 | // the property turns out to be a local variable allocated in a |
| 9033 | // context extension object - introduced via eval. Implicit global |
| 9034 | // receivers are indicated with the hole value. |
| 9035 | return isolate->heap()->the_hole_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9036 | } |
| 9037 | |
| 9038 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9039 | static ObjectPair LoadContextSlotHelper(Arguments args, |
| 9040 | Isolate* isolate, |
| 9041 | bool throw_error) { |
| 9042 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9043 | ASSERT_EQ(2, args.length()); |
| 9044 | |
| 9045 | if (!args[0]->IsContext() || !args[1]->IsString()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9046 | return MakePair(isolate->ThrowIllegalOperation(), NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9047 | } |
| 9048 | Handle<Context> context = args.at<Context>(0); |
| 9049 | Handle<String> name = args.at<String>(1); |
| 9050 | |
| 9051 | int index; |
| 9052 | PropertyAttributes attributes; |
| 9053 | ContextLookupFlags flags = FOLLOW_CHAINS; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9054 | BindingFlags binding_flags; |
| 9055 | Handle<Object> holder = context->Lookup(name, |
| 9056 | flags, |
| 9057 | &index, |
| 9058 | &attributes, |
| 9059 | &binding_flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9060 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9061 | // 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] | 9062 | if (index >= 0) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9063 | ASSERT(holder->IsContext()); |
| 9064 | // If the "property" we were looking for is a local variable, the |
| 9065 | // 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] | 9066 | // |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9067 | // Use the hole as the receiver to signal that the receiver is implicit |
| 9068 | // and that the global receiver should be used (as distinguished from an |
| 9069 | // explicit receiver that happens to be a global object). |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9070 | Handle<Object> receiver = isolate->factory()->the_hole_value(); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9071 | Object* value = Context::cast(*holder)->get(index); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9072 | // Check for uninitialized bindings. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9073 | switch (binding_flags) { |
| 9074 | case MUTABLE_CHECK_INITIALIZED: |
| 9075 | case IMMUTABLE_CHECK_INITIALIZED_HARMONY: |
| 9076 | if (value->IsTheHole()) { |
| 9077 | Handle<Object> reference_error = |
| 9078 | isolate->factory()->NewReferenceError("not_defined", |
| 9079 | HandleVector(&name, 1)); |
| 9080 | return MakePair(isolate->Throw(*reference_error), NULL); |
| 9081 | } |
| 9082 | // FALLTHROUGH |
| 9083 | case MUTABLE_IS_INITIALIZED: |
| 9084 | case IMMUTABLE_IS_INITIALIZED: |
| 9085 | case IMMUTABLE_IS_INITIALIZED_HARMONY: |
| 9086 | ASSERT(!value->IsTheHole()); |
| 9087 | return MakePair(value, *receiver); |
| 9088 | case IMMUTABLE_CHECK_INITIALIZED: |
| 9089 | return MakePair(Unhole(isolate->heap(), value, attributes), *receiver); |
| 9090 | case MISSING_BINDING: |
| 9091 | UNREACHABLE(); |
| 9092 | return MakePair(NULL, NULL); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9093 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9094 | } |
| 9095 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9096 | // Otherwise, if the slot was found the holder is a context extension |
| 9097 | // object, subject of a with, or a global object. We read the named |
| 9098 | // property from it. |
| 9099 | if (!holder.is_null()) { |
| 9100 | Handle<JSObject> object = Handle<JSObject>::cast(holder); |
| 9101 | ASSERT(object->HasProperty(*name)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9102 | // GetProperty below can cause GC. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9103 | Handle<Object> receiver_handle(object->IsGlobalObject() |
| 9104 | ? GlobalObject::cast(*object)->global_receiver() |
| 9105 | : ComputeReceiverForNonGlobal(isolate, *object)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9106 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9107 | // 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] | 9108 | // GetProperty function. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9109 | MaybeObject* value = object->GetProperty(*name); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9110 | return MakePair(value, *receiver_handle); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9111 | } |
| 9112 | |
| 9113 | if (throw_error) { |
| 9114 | // The property doesn't exist - throw exception. |
| 9115 | Handle<Object> reference_error = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9116 | isolate->factory()->NewReferenceError("not_defined", |
| 9117 | HandleVector(&name, 1)); |
| 9118 | return MakePair(isolate->Throw(*reference_error), NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9119 | } else { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9120 | // The property doesn't exist - return undefined. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9121 | return MakePair(isolate->heap()->undefined_value(), |
| 9122 | isolate->heap()->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9123 | } |
| 9124 | } |
| 9125 | |
| 9126 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9127 | RUNTIME_FUNCTION(ObjectPair, Runtime_LoadContextSlot) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9128 | return LoadContextSlotHelper(args, isolate, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9129 | } |
| 9130 | |
| 9131 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9132 | RUNTIME_FUNCTION(ObjectPair, Runtime_LoadContextSlotNoReferenceError) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9133 | return LoadContextSlotHelper(args, isolate, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9134 | } |
| 9135 | |
| 9136 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9137 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StoreContextSlot) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9138 | HandleScope scope(isolate); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9139 | ASSERT(args.length() == 4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9140 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9141 | Handle<Object> value(args[0], isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9142 | CONVERT_ARG_CHECKED(Context, context, 1); |
| 9143 | CONVERT_ARG_CHECKED(String, name, 2); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9144 | CONVERT_LANGUAGE_MODE_ARG(language_mode, 3); |
| 9145 | StrictModeFlag strict_mode = (language_mode == CLASSIC_MODE) |
| 9146 | ? kNonStrictMode : kStrictMode; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9147 | |
| 9148 | int index; |
| 9149 | PropertyAttributes attributes; |
| 9150 | ContextLookupFlags flags = FOLLOW_CHAINS; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9151 | BindingFlags binding_flags; |
| 9152 | Handle<Object> holder = context->Lookup(name, |
| 9153 | flags, |
| 9154 | &index, |
| 9155 | &attributes, |
| 9156 | &binding_flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9157 | |
| 9158 | if (index >= 0) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9159 | // The property was found in a context slot. |
| 9160 | Handle<Context> context = Handle<Context>::cast(holder); |
| 9161 | if (binding_flags == MUTABLE_CHECK_INITIALIZED && |
| 9162 | context->get(index)->IsTheHole()) { |
| 9163 | Handle<Object> error = |
| 9164 | isolate->factory()->NewReferenceError("not_defined", |
| 9165 | HandleVector(&name, 1)); |
| 9166 | return isolate->Throw(*error); |
| 9167 | } |
| 9168 | // Ignore if read_only variable. |
| 9169 | if ((attributes & READ_ONLY) == 0) { |
| 9170 | // Context is a fixed array and set cannot fail. |
| 9171 | context->set(index, *value); |
| 9172 | } else if (strict_mode == kStrictMode) { |
| 9173 | // Setting read only property in strict mode. |
| 9174 | Handle<Object> error = |
| 9175 | isolate->factory()->NewTypeError("strict_cannot_assign", |
| 9176 | HandleVector(&name, 1)); |
| 9177 | return isolate->Throw(*error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9178 | } |
| 9179 | return *value; |
| 9180 | } |
| 9181 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9182 | // Slow case: The property is not in a context slot. It is either in a |
| 9183 | // context extension object, a property of the subject of a with, or a |
| 9184 | // property of the global object. |
| 9185 | Handle<JSObject> object; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9186 | |
| 9187 | if (!holder.is_null()) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9188 | // The property exists on the holder. |
| 9189 | object = Handle<JSObject>::cast(holder); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9190 | } else { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9191 | // The property was not found. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9192 | ASSERT(attributes == ABSENT); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9193 | |
| 9194 | if (strict_mode == kStrictMode) { |
| 9195 | // Throw in strict mode (assignment to undefined variable). |
| 9196 | Handle<Object> error = |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9197 | isolate->factory()->NewReferenceError( |
| 9198 | "not_defined", HandleVector(&name, 1)); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9199 | return isolate->Throw(*error); |
| 9200 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9201 | // In non-strict mode, the property is added to the global object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9202 | attributes = NONE; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9203 | object = Handle<JSObject>(isolate->context()->global()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9204 | } |
| 9205 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9206 | // 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] | 9207 | if ((attributes & READ_ONLY) == 0 || |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9208 | (object->GetLocalPropertyAttribute(*name) == ABSENT)) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9209 | RETURN_IF_EMPTY_HANDLE( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9210 | isolate, |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 9211 | JSReceiver::SetProperty(object, name, value, NONE, strict_mode)); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9212 | } else if (strict_mode == kStrictMode && (attributes & READ_ONLY) != 0) { |
| 9213 | // Setting read only property in strict mode. |
| 9214 | Handle<Object> error = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9215 | isolate->factory()->NewTypeError( |
| 9216 | "strict_cannot_assign", HandleVector(&name, 1)); |
| 9217 | return isolate->Throw(*error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9218 | } |
| 9219 | return *value; |
| 9220 | } |
| 9221 | |
| 9222 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9223 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Throw) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9224 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9225 | ASSERT(args.length() == 1); |
| 9226 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9227 | return isolate->Throw(args[0]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9228 | } |
| 9229 | |
| 9230 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9231 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ReThrow) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9232 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9233 | ASSERT(args.length() == 1); |
| 9234 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9235 | return isolate->ReThrow(args[0]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9236 | } |
| 9237 | |
| 9238 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9239 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PromoteScheduledException) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 9240 | ASSERT_EQ(0, args.length()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9241 | return isolate->PromoteScheduledException(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 9242 | } |
| 9243 | |
| 9244 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9245 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ThrowReferenceError) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9246 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9247 | ASSERT(args.length() == 1); |
| 9248 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9249 | Handle<Object> name(args[0], isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9250 | Handle<Object> reference_error = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9251 | isolate->factory()->NewReferenceError("not_defined", |
| 9252 | HandleVector(&name, 1)); |
| 9253 | return isolate->Throw(*reference_error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9254 | } |
| 9255 | |
| 9256 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9257 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StackGuard) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 9258 | ASSERT(args.length() == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9259 | |
| 9260 | // First check if this is a real stack overflow. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9261 | if (isolate->stack_guard()->IsStackOverflow()) { |
| 9262 | NoHandleAllocation na; |
| 9263 | return isolate->StackOverflow(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9264 | } |
| 9265 | |
| 9266 | return Execution::HandleStackGuardInterrupt(); |
| 9267 | } |
| 9268 | |
| 9269 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9270 | static int StackSize() { |
| 9271 | int n = 0; |
| 9272 | for (JavaScriptFrameIterator it; !it.done(); it.Advance()) n++; |
| 9273 | return n; |
| 9274 | } |
| 9275 | |
| 9276 | |
| 9277 | static void PrintTransition(Object* result) { |
| 9278 | // indentation |
| 9279 | { const int nmax = 80; |
| 9280 | int n = StackSize(); |
| 9281 | if (n <= nmax) |
| 9282 | PrintF("%4d:%*s", n, n, ""); |
| 9283 | else |
| 9284 | PrintF("%4d:%*s", n, nmax, "..."); |
| 9285 | } |
| 9286 | |
| 9287 | if (result == NULL) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9288 | JavaScriptFrame::PrintTop(stdout, true, false); |
| 9289 | PrintF(" {\n"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9290 | } else { |
| 9291 | // function result |
| 9292 | PrintF("} -> "); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9293 | result->ShortPrint(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9294 | PrintF("\n"); |
| 9295 | } |
| 9296 | } |
| 9297 | |
| 9298 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9299 | RUNTIME_FUNCTION(MaybeObject*, Runtime_TraceEnter) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9300 | ASSERT(args.length() == 0); |
| 9301 | NoHandleAllocation ha; |
| 9302 | PrintTransition(NULL); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9303 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9304 | } |
| 9305 | |
| 9306 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9307 | RUNTIME_FUNCTION(MaybeObject*, Runtime_TraceExit) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9308 | NoHandleAllocation ha; |
| 9309 | PrintTransition(args[0]); |
| 9310 | return args[0]; // return TOS |
| 9311 | } |
| 9312 | |
| 9313 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9314 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPrint) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9315 | NoHandleAllocation ha; |
| 9316 | ASSERT(args.length() == 1); |
| 9317 | |
| 9318 | #ifdef DEBUG |
| 9319 | if (args[0]->IsString()) { |
| 9320 | // If we have a string, assume it's a code "marker" |
| 9321 | // and print some interesting cpu debugging info. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9322 | JavaScriptFrameIterator it(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9323 | JavaScriptFrame* frame = it.frame(); |
| 9324 | PrintF("fp = %p, sp = %p, caller_sp = %p: ", |
| 9325 | frame->fp(), frame->sp(), frame->caller_sp()); |
| 9326 | } else { |
| 9327 | PrintF("DebugPrint: "); |
| 9328 | } |
| 9329 | args[0]->Print(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 9330 | if (args[0]->IsHeapObject()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9331 | PrintF("\n"); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 9332 | HeapObject::cast(args[0])->map()->Print(); |
| 9333 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9334 | #else |
| 9335 | // ShortPrint is available in release mode. Print is not. |
| 9336 | args[0]->ShortPrint(); |
| 9337 | #endif |
| 9338 | PrintF("\n"); |
| 9339 | Flush(); |
| 9340 | |
| 9341 | return args[0]; // return TOS |
| 9342 | } |
| 9343 | |
| 9344 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9345 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugTrace) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9346 | ASSERT(args.length() == 0); |
| 9347 | NoHandleAllocation ha; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9348 | isolate->PrintStack(); |
| 9349 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9350 | } |
| 9351 | |
| 9352 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9353 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateCurrentTime) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9354 | NoHandleAllocation ha; |
| 9355 | ASSERT(args.length() == 0); |
| 9356 | |
| 9357 | // According to ECMA-262, section 15.9.1, page 117, the precision of |
| 9358 | // the number in a Date object representing a particular instant in |
| 9359 | // time is milliseconds. Therefore, we floor the result of getting |
| 9360 | // the OS time. |
| 9361 | double millis = floor(OS::TimeCurrentMillis()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9362 | return isolate->heap()->NumberFromDouble(millis); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9363 | } |
| 9364 | |
| 9365 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9366 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateParseString) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9367 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9368 | ASSERT(args.length() == 2); |
| 9369 | |
| 9370 | CONVERT_ARG_CHECKED(String, str, 0); |
| 9371 | FlattenString(str); |
| 9372 | |
| 9373 | CONVERT_ARG_CHECKED(JSArray, output, 1); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9374 | |
| 9375 | MaybeObject* maybe_result_array = |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 9376 | output->EnsureCanContainHeapObjectElements(); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9377 | if (maybe_result_array->IsFailure()) return maybe_result_array; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9378 | RUNTIME_ASSERT(output->HasFastElements()); |
| 9379 | |
| 9380 | AssertNoAllocation no_allocation; |
| 9381 | |
| 9382 | FixedArray* output_array = FixedArray::cast(output->elements()); |
| 9383 | RUNTIME_ASSERT(output_array->length() >= DateParser::OUTPUT_SIZE); |
| 9384 | bool result; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9385 | String::FlatContent str_content = str->GetFlatContent(); |
| 9386 | if (str_content.IsAscii()) { |
| 9387 | result = DateParser::Parse(str_content.ToAsciiVector(), |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9388 | output_array, |
| 9389 | isolate->unicode_cache()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9390 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9391 | ASSERT(str_content.IsTwoByte()); |
| 9392 | result = DateParser::Parse(str_content.ToUC16Vector(), |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9393 | output_array, |
| 9394 | isolate->unicode_cache()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9395 | } |
| 9396 | |
| 9397 | if (result) { |
| 9398 | return *output; |
| 9399 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9400 | return isolate->heap()->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9401 | } |
| 9402 | } |
| 9403 | |
| 9404 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9405 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateLocalTimezone) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9406 | NoHandleAllocation ha; |
| 9407 | ASSERT(args.length() == 1); |
| 9408 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 9409 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9410 | const char* zone = OS::LocalTimezone(x); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9411 | return isolate->heap()->AllocateStringFromUtf8(CStrVector(zone)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9412 | } |
| 9413 | |
| 9414 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9415 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateLocalTimeOffset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9416 | NoHandleAllocation ha; |
| 9417 | ASSERT(args.length() == 0); |
| 9418 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9419 | return isolate->heap()->NumberFromDouble(OS::LocalTimeOffset()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9420 | } |
| 9421 | |
| 9422 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9423 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateDaylightSavingsOffset) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9424 | NoHandleAllocation ha; |
| 9425 | ASSERT(args.length() == 1); |
| 9426 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 9427 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9428 | return isolate->heap()->NumberFromDouble(OS::DaylightSavingsOffset(x)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9429 | } |
| 9430 | |
| 9431 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9432 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GlobalReceiver) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9433 | ASSERT(args.length() == 1); |
| 9434 | Object* global = args[0]; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9435 | if (!global->IsJSGlobalObject()) return isolate->heap()->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9436 | return JSGlobalObject::cast(global)->global_receiver(); |
| 9437 | } |
| 9438 | |
| 9439 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9440 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ParseJson) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9441 | HandleScope scope(isolate); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 9442 | ASSERT_EQ(1, args.length()); |
| 9443 | CONVERT_ARG_CHECKED(String, source, 0); |
| 9444 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 9445 | source = Handle<String>(source->TryFlattenGetString()); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 9446 | // Optimized fast case where we only have ASCII characters. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 9447 | Handle<Object> result; |
| 9448 | if (source->IsSeqAsciiString()) { |
| 9449 | result = JsonParser<true>::Parse(source); |
| 9450 | } else { |
| 9451 | result = JsonParser<false>::Parse(source); |
| 9452 | } |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 9453 | if (result.is_null()) { |
| 9454 | // Syntax error or stack overflow in scanner. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9455 | ASSERT(isolate->has_pending_exception()); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 9456 | return Failure::Exception(); |
| 9457 | } |
| 9458 | return *result; |
| 9459 | } |
| 9460 | |
| 9461 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9462 | bool CodeGenerationFromStringsAllowed(Isolate* isolate, |
| 9463 | Handle<Context> context) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 9464 | ASSERT(context->allow_code_gen_from_strings()->IsFalse()); |
| 9465 | // Check with callback if set. |
| 9466 | AllowCodeGenerationFromStringsCallback callback = |
| 9467 | isolate->allow_code_gen_callback(); |
| 9468 | if (callback == NULL) { |
| 9469 | // No callback set and code generation disallowed. |
| 9470 | return false; |
| 9471 | } else { |
| 9472 | // Callback set. Let it decide if code generation is allowed. |
| 9473 | VMState state(isolate, EXTERNAL); |
| 9474 | return callback(v8::Utils::ToLocal(context)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9475 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9476 | } |
| 9477 | |
| 9478 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9479 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CompileString) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9480 | HandleScope scope(isolate); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 9481 | ASSERT_EQ(1, args.length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9482 | CONVERT_ARG_CHECKED(String, source, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9483 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9484 | // Extract global context. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9485 | Handle<Context> context(isolate->context()->global_context()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9486 | |
| 9487 | // Check if global context allows code generation from |
| 9488 | // strings. Throw an exception if it doesn't. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 9489 | if (context->allow_code_gen_from_strings()->IsFalse() && |
| 9490 | !CodeGenerationFromStringsAllowed(isolate, context)) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9491 | return isolate->Throw(*isolate->factory()->NewError( |
| 9492 | "code_gen_from_strings", HandleVector<Object>(NULL, 0))); |
| 9493 | } |
| 9494 | |
| 9495 | // Compile source string in the global context. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9496 | Handle<SharedFunctionInfo> shared = Compiler::CompileEval( |
| 9497 | source, context, true, CLASSIC_MODE, RelocInfo::kNoPosition); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9498 | if (shared.is_null()) return Failure::Exception(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9499 | Handle<JSFunction> fun = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9500 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 9501 | context, |
| 9502 | NOT_TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9503 | return *fun; |
| 9504 | } |
| 9505 | |
| 9506 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9507 | static ObjectPair CompileGlobalEval(Isolate* isolate, |
| 9508 | Handle<String> source, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 9509 | Handle<Object> receiver, |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9510 | LanguageMode language_mode, |
| 9511 | int scope_position) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9512 | Handle<Context> context = Handle<Context>(isolate->context()); |
| 9513 | Handle<Context> global_context = Handle<Context>(context->global_context()); |
| 9514 | |
| 9515 | // Check if global context allows code generation from |
| 9516 | // strings. Throw an exception if it doesn't. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 9517 | if (global_context->allow_code_gen_from_strings()->IsFalse() && |
| 9518 | !CodeGenerationFromStringsAllowed(isolate, global_context)) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9519 | isolate->Throw(*isolate->factory()->NewError( |
| 9520 | "code_gen_from_strings", HandleVector<Object>(NULL, 0))); |
| 9521 | return MakePair(Failure::Exception(), NULL); |
| 9522 | } |
| 9523 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 9524 | // Deal with a normal eval call with a string argument. Compile it |
| 9525 | // and return the compiled function bound in the local context. |
| 9526 | Handle<SharedFunctionInfo> shared = Compiler::CompileEval( |
| 9527 | source, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9528 | Handle<Context>(isolate->context()), |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9529 | context->IsGlobalContext(), |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9530 | language_mode, |
| 9531 | scope_position); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 9532 | if (shared.is_null()) return MakePair(Failure::Exception(), NULL); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9533 | Handle<JSFunction> compiled = |
| 9534 | isolate->factory()->NewFunctionFromSharedFunctionInfo( |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9535 | shared, context, NOT_TENURED); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 9536 | return MakePair(*compiled, *receiver); |
| 9537 | } |
| 9538 | |
| 9539 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9540 | RUNTIME_FUNCTION(ObjectPair, Runtime_ResolvePossiblyDirectEval) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9541 | ASSERT(args.length() == 5); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9542 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9543 | HandleScope scope(isolate); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9544 | Handle<Object> callee = args.at<Object>(0); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 9545 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9546 | // If "eval" didn't refer to the original GlobalEval, it's not a |
| 9547 | // direct call to eval. |
| 9548 | // (And even if it is, but the first argument isn't a string, just let |
| 9549 | // execution default to an indirect call to eval, which will also return |
| 9550 | // the first argument without doing anything). |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9551 | if (*callee != isolate->global_context()->global_eval_fun() || |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 9552 | !args[1]->IsString()) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9553 | return MakePair(*callee, isolate->heap()->the_hole_value()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 9554 | } |
| 9555 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9556 | CONVERT_LANGUAGE_MODE_ARG(language_mode, 3); |
| 9557 | ASSERT(args[4]->IsSmi()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9558 | return CompileGlobalEval(isolate, |
| 9559 | args.at<String>(1), |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 9560 | args.at<Object>(2), |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9561 | language_mode, |
| 9562 | args.smi_at(4)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9563 | } |
| 9564 | |
| 9565 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9566 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetNewFunctionAttributes) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9567 | // This utility adjusts the property attributes for newly created Function |
| 9568 | // object ("new Function(...)") by changing the map. |
| 9569 | // All it does is changing the prototype property to enumerable |
| 9570 | // as specified in ECMA262, 15.3.5.2. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9571 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9572 | ASSERT(args.length() == 1); |
| 9573 | CONVERT_ARG_CHECKED(JSFunction, func, 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9574 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9575 | Handle<Map> map = func->shared()->is_classic_mode() |
| 9576 | ? isolate->function_instance_map() |
| 9577 | : isolate->strict_mode_function_instance_map(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9578 | |
| 9579 | ASSERT(func->map()->instance_type() == map->instance_type()); |
| 9580 | ASSERT(func->map()->instance_size() == map->instance_size()); |
| 9581 | func->set_map(*map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9582 | return *func; |
| 9583 | } |
| 9584 | |
| 9585 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9586 | RUNTIME_FUNCTION(MaybeObject*, Runtime_AllocateInNewSpace) { |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 9587 | // Allocate a block of memory in NewSpace (filled with a filler). |
| 9588 | // Use as fallback for allocation in generated code when NewSpace |
| 9589 | // is full. |
| 9590 | ASSERT(args.length() == 1); |
| 9591 | CONVERT_ARG_CHECKED(Smi, size_smi, 0); |
| 9592 | int size = size_smi->value(); |
| 9593 | RUNTIME_ASSERT(IsAligned(size, kPointerSize)); |
| 9594 | RUNTIME_ASSERT(size > 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9595 | Heap* heap = isolate->heap(); |
| 9596 | const int kMinFreeNewSpaceAfterGC = heap->InitialSemiSpaceSize() * 3/4; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 9597 | RUNTIME_ASSERT(size <= kMinFreeNewSpaceAfterGC); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9598 | Object* allocation; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9599 | { MaybeObject* maybe_allocation = heap->new_space()->AllocateRaw(size); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9600 | if (maybe_allocation->ToObject(&allocation)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9601 | heap->CreateFillerObjectAt(HeapObject::cast(allocation)->address(), size); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9602 | } |
| 9603 | return maybe_allocation; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 9604 | } |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 9605 | } |
| 9606 | |
| 9607 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 9608 | // 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] | 9609 | // array. Returns true if the element was pushed on the stack and |
| 9610 | // false otherwise. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9611 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushIfAbsent) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9612 | ASSERT(args.length() == 2); |
| 9613 | CONVERT_CHECKED(JSArray, array, args[0]); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 9614 | CONVERT_CHECKED(JSObject, element, args[1]); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9615 | RUNTIME_ASSERT(array->HasFastElements() || array->HasFastSmiOnlyElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9616 | int length = Smi::cast(array->length())->value(); |
| 9617 | FixedArray* elements = FixedArray::cast(array->elements()); |
| 9618 | for (int i = 0; i < length; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9619 | if (elements->get(i) == element) return isolate->heap()->false_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9620 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9621 | Object* obj; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9622 | // Strict not needed. Used for cycle detection in Array join implementation. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 9623 | { MaybeObject* maybe_obj = |
| 9624 | array->SetFastElement(length, element, kNonStrictMode, true); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9625 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9626 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9627 | return isolate->heap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9628 | } |
| 9629 | |
| 9630 | |
| 9631 | /** |
| 9632 | * A simple visitor visits every element of Array's. |
| 9633 | * The backend storage can be a fixed array for fast elements case, |
| 9634 | * or a dictionary for sparse array. Since Dictionary is a subtype |
| 9635 | * of FixedArray, the class can be used by both fast and slow cases. |
| 9636 | * The second parameter of the constructor, fast_elements, specifies |
| 9637 | * whether the storage is a FixedArray or Dictionary. |
| 9638 | * |
| 9639 | * An index limit is used to deal with the situation that a result array |
| 9640 | * length overflows 32-bit non-negative integer. |
| 9641 | */ |
| 9642 | class ArrayConcatVisitor { |
| 9643 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9644 | ArrayConcatVisitor(Isolate* isolate, |
| 9645 | Handle<FixedArray> storage, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9646 | bool fast_elements) : |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9647 | isolate_(isolate), |
| 9648 | storage_(Handle<FixedArray>::cast( |
| 9649 | isolate->global_handles()->Create(*storage))), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9650 | index_offset_(0u), |
| 9651 | fast_elements_(fast_elements) { } |
| 9652 | |
| 9653 | ~ArrayConcatVisitor() { |
| 9654 | clear_storage(); |
| 9655 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9656 | |
| 9657 | void visit(uint32_t i, Handle<Object> elm) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9658 | if (i >= JSObject::kMaxElementCount - index_offset_) return; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 9659 | uint32_t index = index_offset_ + i; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9660 | |
| 9661 | if (fast_elements_) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9662 | if (index < static_cast<uint32_t>(storage_->length())) { |
| 9663 | storage_->set(index, *elm); |
| 9664 | return; |
| 9665 | } |
| 9666 | // Our initial estimate of length was foiled, possibly by |
| 9667 | // getters on the arrays increasing the length of later arrays |
| 9668 | // during iteration. |
| 9669 | // This shouldn't happen in anything but pathological cases. |
| 9670 | SetDictionaryMode(index); |
| 9671 | // Fall-through to dictionary mode. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9672 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9673 | ASSERT(!fast_elements_); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 9674 | Handle<SeededNumberDictionary> dict( |
| 9675 | SeededNumberDictionary::cast(*storage_)); |
| 9676 | Handle<SeededNumberDictionary> result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9677 | isolate_->factory()->DictionaryAtNumberPut(dict, index, elm); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9678 | if (!result.is_identical_to(dict)) { |
| 9679 | // Dictionary needed to grow. |
| 9680 | clear_storage(); |
| 9681 | set_storage(*result); |
| 9682 | } |
| 9683 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9684 | |
| 9685 | void increase_index_offset(uint32_t delta) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9686 | if (JSObject::kMaxElementCount - index_offset_ < delta) { |
| 9687 | index_offset_ = JSObject::kMaxElementCount; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 9688 | } else { |
| 9689 | index_offset_ += delta; |
| 9690 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9691 | } |
| 9692 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9693 | Handle<JSArray> ToArray() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9694 | Handle<JSArray> array = isolate_->factory()->NewJSArray(0); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9695 | Handle<Object> length = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9696 | isolate_->factory()->NewNumber(static_cast<double>(index_offset_)); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9697 | Handle<Map> map; |
| 9698 | if (fast_elements_) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9699 | map = isolate_->factory()->GetElementsTransitionMap(array, |
| 9700 | FAST_ELEMENTS); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9701 | } else { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9702 | map = isolate_->factory()->GetElementsTransitionMap(array, |
| 9703 | DICTIONARY_ELEMENTS); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9704 | } |
| 9705 | array->set_map(*map); |
| 9706 | array->set_length(*length); |
| 9707 | array->set_elements(*storage_); |
| 9708 | return array; |
| 9709 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 9710 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9711 | private: |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9712 | // Convert storage to dictionary mode. |
| 9713 | void SetDictionaryMode(uint32_t index) { |
| 9714 | ASSERT(fast_elements_); |
| 9715 | Handle<FixedArray> current_storage(*storage_); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 9716 | Handle<SeededNumberDictionary> slow_storage( |
| 9717 | isolate_->factory()->NewSeededNumberDictionary( |
| 9718 | current_storage->length())); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9719 | uint32_t current_length = static_cast<uint32_t>(current_storage->length()); |
| 9720 | for (uint32_t i = 0; i < current_length; i++) { |
| 9721 | HandleScope loop_scope; |
| 9722 | Handle<Object> element(current_storage->get(i)); |
| 9723 | if (!element->IsTheHole()) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 9724 | Handle<SeededNumberDictionary> new_storage = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9725 | isolate_->factory()->DictionaryAtNumberPut(slow_storage, i, element); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9726 | if (!new_storage.is_identical_to(slow_storage)) { |
| 9727 | slow_storage = loop_scope.CloseAndEscape(new_storage); |
| 9728 | } |
| 9729 | } |
| 9730 | } |
| 9731 | clear_storage(); |
| 9732 | set_storage(*slow_storage); |
| 9733 | fast_elements_ = false; |
| 9734 | } |
| 9735 | |
| 9736 | inline void clear_storage() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9737 | isolate_->global_handles()->Destroy( |
| 9738 | Handle<Object>::cast(storage_).location()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9739 | } |
| 9740 | |
| 9741 | inline void set_storage(FixedArray* storage) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9742 | storage_ = Handle<FixedArray>::cast( |
| 9743 | isolate_->global_handles()->Create(storage)); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9744 | } |
| 9745 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9746 | Isolate* isolate_; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9747 | Handle<FixedArray> storage_; // Always a global handle. |
| 9748 | // Index after last seen index. Always less than or equal to |
| 9749 | // JSObject::kMaxElementCount. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9750 | uint32_t index_offset_; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9751 | bool fast_elements_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9752 | }; |
| 9753 | |
| 9754 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9755 | static uint32_t EstimateElementCount(Handle<JSArray> array) { |
| 9756 | uint32_t length = static_cast<uint32_t>(array->length()->Number()); |
| 9757 | int element_count = 0; |
| 9758 | switch (array->GetElementsKind()) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9759 | case FAST_SMI_ONLY_ELEMENTS: |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9760 | case FAST_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9761 | // Fast elements can't have lengths that are not representable by |
| 9762 | // a 32-bit signed integer. |
| 9763 | ASSERT(static_cast<int32_t>(FixedArray::kMaxLength) >= 0); |
| 9764 | int fast_length = static_cast<int>(length); |
| 9765 | Handle<FixedArray> elements(FixedArray::cast(array->elements())); |
| 9766 | for (int i = 0; i < fast_length; i++) { |
| 9767 | if (!elements->get(i)->IsTheHole()) element_count++; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9768 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9769 | break; |
| 9770 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9771 | case FAST_DOUBLE_ELEMENTS: |
| 9772 | // TODO(1810): Decide if it's worthwhile to implement this. |
| 9773 | UNREACHABLE(); |
| 9774 | break; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9775 | case DICTIONARY_ELEMENTS: { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 9776 | Handle<SeededNumberDictionary> dictionary( |
| 9777 | SeededNumberDictionary::cast(array->elements())); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9778 | int capacity = dictionary->Capacity(); |
| 9779 | for (int i = 0; i < capacity; i++) { |
| 9780 | Handle<Object> key(dictionary->KeyAt(i)); |
| 9781 | if (dictionary->IsKey(*key)) { |
| 9782 | element_count++; |
| 9783 | } |
| 9784 | } |
| 9785 | break; |
| 9786 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9787 | case NON_STRICT_ARGUMENTS_ELEMENTS: |
| 9788 | case EXTERNAL_BYTE_ELEMENTS: |
| 9789 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 9790 | case EXTERNAL_SHORT_ELEMENTS: |
| 9791 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 9792 | case EXTERNAL_INT_ELEMENTS: |
| 9793 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 9794 | case EXTERNAL_FLOAT_ELEMENTS: |
| 9795 | case EXTERNAL_DOUBLE_ELEMENTS: |
| 9796 | case EXTERNAL_PIXEL_ELEMENTS: |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9797 | // External arrays are always dense. |
| 9798 | return length; |
| 9799 | } |
| 9800 | // As an estimate, we assume that the prototype doesn't contain any |
| 9801 | // inherited elements. |
| 9802 | return element_count; |
| 9803 | } |
| 9804 | |
| 9805 | |
| 9806 | |
| 9807 | template<class ExternalArrayClass, class ElementType> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9808 | static void IterateExternalArrayElements(Isolate* isolate, |
| 9809 | Handle<JSObject> receiver, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9810 | bool elements_are_ints, |
| 9811 | bool elements_are_guaranteed_smis, |
| 9812 | ArrayConcatVisitor* visitor) { |
| 9813 | Handle<ExternalArrayClass> array( |
| 9814 | ExternalArrayClass::cast(receiver->elements())); |
| 9815 | uint32_t len = static_cast<uint32_t>(array->length()); |
| 9816 | |
| 9817 | ASSERT(visitor != NULL); |
| 9818 | if (elements_are_ints) { |
| 9819 | if (elements_are_guaranteed_smis) { |
| 9820 | for (uint32_t j = 0; j < len; j++) { |
| 9821 | HandleScope loop_scope; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9822 | Handle<Smi> e(Smi::FromInt(static_cast<int>(array->get_scalar(j)))); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9823 | visitor->visit(j, e); |
| 9824 | } |
| 9825 | } else { |
| 9826 | for (uint32_t j = 0; j < len; j++) { |
| 9827 | HandleScope loop_scope; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9828 | int64_t val = static_cast<int64_t>(array->get_scalar(j)); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9829 | if (Smi::IsValid(static_cast<intptr_t>(val))) { |
| 9830 | Handle<Smi> e(Smi::FromInt(static_cast<int>(val))); |
| 9831 | visitor->visit(j, e); |
| 9832 | } else { |
| 9833 | Handle<Object> e = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9834 | isolate->factory()->NewNumber(static_cast<ElementType>(val)); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9835 | visitor->visit(j, e); |
| 9836 | } |
| 9837 | } |
| 9838 | } |
| 9839 | } else { |
| 9840 | for (uint32_t j = 0; j < len; j++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9841 | HandleScope loop_scope(isolate); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 9842 | Handle<Object> e = isolate->factory()->NewNumber(array->get_scalar(j)); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9843 | visitor->visit(j, e); |
| 9844 | } |
| 9845 | } |
| 9846 | } |
| 9847 | |
| 9848 | |
| 9849 | // Used for sorting indices in a List<uint32_t>. |
| 9850 | static int compareUInt32(const uint32_t* ap, const uint32_t* bp) { |
| 9851 | uint32_t a = *ap; |
| 9852 | uint32_t b = *bp; |
| 9853 | return (a == b) ? 0 : (a < b) ? -1 : 1; |
| 9854 | } |
| 9855 | |
| 9856 | |
| 9857 | static void CollectElementIndices(Handle<JSObject> object, |
| 9858 | uint32_t range, |
| 9859 | List<uint32_t>* indices) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9860 | ElementsKind kind = object->GetElementsKind(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9861 | switch (kind) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9862 | case FAST_SMI_ONLY_ELEMENTS: |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9863 | case FAST_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9864 | Handle<FixedArray> elements(FixedArray::cast(object->elements())); |
| 9865 | uint32_t length = static_cast<uint32_t>(elements->length()); |
| 9866 | if (range < length) length = range; |
| 9867 | for (uint32_t i = 0; i < length; i++) { |
| 9868 | if (!elements->get(i)->IsTheHole()) { |
| 9869 | indices->Add(i); |
| 9870 | } |
| 9871 | } |
| 9872 | break; |
| 9873 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9874 | case FAST_DOUBLE_ELEMENTS: { |
| 9875 | // TODO(1810): Decide if it's worthwhile to implement this. |
| 9876 | UNREACHABLE(); |
| 9877 | break; |
| 9878 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9879 | case DICTIONARY_ELEMENTS: { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 9880 | Handle<SeededNumberDictionary> dict( |
| 9881 | SeededNumberDictionary::cast(object->elements())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9882 | uint32_t capacity = dict->Capacity(); |
| 9883 | for (uint32_t j = 0; j < capacity; j++) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9884 | HandleScope loop_scope; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9885 | Handle<Object> k(dict->KeyAt(j)); |
| 9886 | if (dict->IsKey(*k)) { |
| 9887 | ASSERT(k->IsNumber()); |
| 9888 | uint32_t index = static_cast<uint32_t>(k->Number()); |
| 9889 | if (index < range) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9890 | indices->Add(index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9891 | } |
| 9892 | } |
| 9893 | } |
| 9894 | break; |
| 9895 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9896 | default: { |
| 9897 | int dense_elements_length; |
| 9898 | switch (kind) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9899 | case EXTERNAL_PIXEL_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9900 | dense_elements_length = |
| 9901 | ExternalPixelArray::cast(object->elements())->length(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9902 | break; |
| 9903 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9904 | case EXTERNAL_BYTE_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9905 | dense_elements_length = |
| 9906 | ExternalByteArray::cast(object->elements())->length(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9907 | break; |
| 9908 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9909 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9910 | dense_elements_length = |
| 9911 | ExternalUnsignedByteArray::cast(object->elements())->length(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9912 | break; |
| 9913 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9914 | case EXTERNAL_SHORT_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9915 | dense_elements_length = |
| 9916 | ExternalShortArray::cast(object->elements())->length(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9917 | break; |
| 9918 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9919 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9920 | dense_elements_length = |
| 9921 | ExternalUnsignedShortArray::cast(object->elements())->length(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9922 | break; |
| 9923 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9924 | case EXTERNAL_INT_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9925 | dense_elements_length = |
| 9926 | ExternalIntArray::cast(object->elements())->length(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9927 | break; |
| 9928 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9929 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9930 | dense_elements_length = |
| 9931 | ExternalUnsignedIntArray::cast(object->elements())->length(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9932 | break; |
| 9933 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9934 | case EXTERNAL_FLOAT_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9935 | dense_elements_length = |
| 9936 | ExternalFloatArray::cast(object->elements())->length(); |
| 9937 | break; |
| 9938 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9939 | case EXTERNAL_DOUBLE_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 9940 | dense_elements_length = |
| 9941 | ExternalDoubleArray::cast(object->elements())->length(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9942 | break; |
| 9943 | } |
| 9944 | default: |
| 9945 | UNREACHABLE(); |
| 9946 | dense_elements_length = 0; |
| 9947 | break; |
| 9948 | } |
| 9949 | uint32_t length = static_cast<uint32_t>(dense_elements_length); |
| 9950 | if (range <= length) { |
| 9951 | length = range; |
| 9952 | // We will add all indices, so we might as well clear it first |
| 9953 | // and avoid duplicates. |
| 9954 | indices->Clear(); |
| 9955 | } |
| 9956 | for (uint32_t i = 0; i < length; i++) { |
| 9957 | indices->Add(i); |
| 9958 | } |
| 9959 | if (length == range) return; // All indices accounted for already. |
| 9960 | break; |
| 9961 | } |
| 9962 | } |
| 9963 | |
| 9964 | Handle<Object> prototype(object->GetPrototype()); |
| 9965 | if (prototype->IsJSObject()) { |
| 9966 | // The prototype will usually have no inherited element indices, |
| 9967 | // but we have to check. |
| 9968 | CollectElementIndices(Handle<JSObject>::cast(prototype), range, indices); |
| 9969 | } |
| 9970 | } |
| 9971 | |
| 9972 | |
| 9973 | /** |
| 9974 | * A helper function that visits elements of a JSArray in numerical |
| 9975 | * order. |
| 9976 | * |
| 9977 | * The visitor argument called for each existing element in the array |
| 9978 | * with the element index and the element's value. |
| 9979 | * Afterwards it increments the base-index of the visitor by the array |
| 9980 | * length. |
| 9981 | * Returns false if any access threw an exception, otherwise true. |
| 9982 | */ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9983 | static bool IterateElements(Isolate* isolate, |
| 9984 | Handle<JSArray> receiver, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9985 | ArrayConcatVisitor* visitor) { |
| 9986 | uint32_t length = static_cast<uint32_t>(receiver->length()->Number()); |
| 9987 | switch (receiver->GetElementsKind()) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 9988 | case FAST_SMI_ONLY_ELEMENTS: |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 9989 | case FAST_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9990 | // Run through the elements FixedArray and use HasElement and GetElement |
| 9991 | // to check the prototype for missing elements. |
| 9992 | Handle<FixedArray> elements(FixedArray::cast(receiver->elements())); |
| 9993 | int fast_length = static_cast<int>(length); |
| 9994 | ASSERT(fast_length <= elements->length()); |
| 9995 | for (int j = 0; j < fast_length; j++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9996 | HandleScope loop_scope(isolate); |
| 9997 | Handle<Object> element_value(elements->get(j), isolate); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9998 | if (!element_value->IsTheHole()) { |
| 9999 | visitor->visit(j, element_value); |
| 10000 | } else if (receiver->HasElement(j)) { |
| 10001 | // Call GetElement on receiver, not its prototype, or getters won't |
| 10002 | // have the correct receiver. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10003 | element_value = Object::GetElement(receiver, j); |
| 10004 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, element_value, false); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10005 | visitor->visit(j, element_value); |
| 10006 | } |
| 10007 | } |
| 10008 | break; |
| 10009 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10010 | case FAST_DOUBLE_ELEMENTS: { |
| 10011 | // TODO(1810): Decide if it's worthwhile to implement this. |
| 10012 | UNREACHABLE(); |
| 10013 | break; |
| 10014 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 10015 | case DICTIONARY_ELEMENTS: { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 10016 | Handle<SeededNumberDictionary> dict(receiver->element_dictionary()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10017 | List<uint32_t> indices(dict->Capacity() / 2); |
| 10018 | // Collect all indices in the object and the prototypes less |
| 10019 | // than length. This might introduce duplicates in the indices list. |
| 10020 | CollectElementIndices(receiver, length, &indices); |
| 10021 | indices.Sort(&compareUInt32); |
| 10022 | int j = 0; |
| 10023 | int n = indices.length(); |
| 10024 | while (j < n) { |
| 10025 | HandleScope loop_scope; |
| 10026 | uint32_t index = indices[j]; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10027 | Handle<Object> element = Object::GetElement(receiver, index); |
| 10028 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, element, false); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10029 | visitor->visit(index, element); |
| 10030 | // Skip to next different index (i.e., omit duplicates). |
| 10031 | do { |
| 10032 | j++; |
| 10033 | } while (j < n && indices[j] == index); |
| 10034 | } |
| 10035 | break; |
| 10036 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 10037 | case EXTERNAL_PIXEL_ELEMENTS: { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10038 | Handle<ExternalPixelArray> pixels(ExternalPixelArray::cast( |
| 10039 | receiver->elements())); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10040 | for (uint32_t j = 0; j < length; j++) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 10041 | Handle<Smi> e(Smi::FromInt(pixels->get_scalar(j))); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10042 | visitor->visit(j, e); |
| 10043 | } |
| 10044 | break; |
| 10045 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 10046 | case EXTERNAL_BYTE_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10047 | IterateExternalArrayElements<ExternalByteArray, int8_t>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10048 | isolate, receiver, true, true, visitor); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10049 | break; |
| 10050 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 10051 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10052 | IterateExternalArrayElements<ExternalUnsignedByteArray, uint8_t>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10053 | isolate, receiver, true, true, visitor); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10054 | break; |
| 10055 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 10056 | case EXTERNAL_SHORT_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10057 | IterateExternalArrayElements<ExternalShortArray, int16_t>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10058 | isolate, receiver, true, true, visitor); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10059 | break; |
| 10060 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 10061 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10062 | IterateExternalArrayElements<ExternalUnsignedShortArray, uint16_t>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10063 | isolate, receiver, true, true, visitor); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10064 | break; |
| 10065 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 10066 | case EXTERNAL_INT_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10067 | IterateExternalArrayElements<ExternalIntArray, int32_t>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10068 | isolate, receiver, true, false, visitor); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10069 | break; |
| 10070 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 10071 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10072 | IterateExternalArrayElements<ExternalUnsignedIntArray, uint32_t>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10073 | isolate, receiver, true, false, visitor); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10074 | break; |
| 10075 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 10076 | case EXTERNAL_FLOAT_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10077 | IterateExternalArrayElements<ExternalFloatArray, float>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10078 | isolate, receiver, false, false, visitor); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10079 | break; |
| 10080 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 10081 | case EXTERNAL_DOUBLE_ELEMENTS: { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 10082 | IterateExternalArrayElements<ExternalDoubleArray, double>( |
| 10083 | isolate, receiver, false, false, visitor); |
| 10084 | break; |
| 10085 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10086 | default: |
| 10087 | UNREACHABLE(); |
| 10088 | break; |
| 10089 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10090 | visitor->increase_index_offset(length); |
| 10091 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10092 | } |
| 10093 | |
| 10094 | |
| 10095 | /** |
| 10096 | * Array::concat implementation. |
| 10097 | * See ECMAScript 262, 15.4.4.4. |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10098 | * TODO(581): Fix non-compliance for very large concatenations and update to |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 10099 | * following the ECMAScript 5 specification. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10100 | */ |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10101 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayConcat) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10102 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10103 | HandleScope handle_scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10104 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10105 | CONVERT_ARG_CHECKED(JSArray, arguments, 0); |
| 10106 | int argument_count = static_cast<int>(arguments->length()->Number()); |
| 10107 | RUNTIME_ASSERT(arguments->HasFastElements()); |
| 10108 | Handle<FixedArray> elements(FixedArray::cast(arguments->elements())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10109 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10110 | // Pass 1: estimate the length and number of elements of the result. |
| 10111 | // The actual length can be larger if any of the arguments have getters |
| 10112 | // that mutate other arguments (but will otherwise be precise). |
| 10113 | // The number of elements is precise if there are no inherited elements. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10114 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10115 | uint32_t estimate_result_length = 0; |
| 10116 | uint32_t estimate_nof_elements = 0; |
| 10117 | { |
| 10118 | for (int i = 0; i < argument_count; i++) { |
| 10119 | HandleScope loop_scope; |
| 10120 | Handle<Object> obj(elements->get(i)); |
| 10121 | uint32_t length_estimate; |
| 10122 | uint32_t element_estimate; |
| 10123 | if (obj->IsJSArray()) { |
| 10124 | Handle<JSArray> array(Handle<JSArray>::cast(obj)); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10125 | // TODO(1810): Find out if it's worthwhile to properly support |
| 10126 | // arbitrary ElementsKinds. For now, pessimistically transition to |
| 10127 | // FAST_ELEMENTS. |
| 10128 | if (array->HasFastDoubleElements()) { |
| 10129 | array = Handle<JSArray>::cast( |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 10130 | JSObject::TransitionElementsKind(array, FAST_ELEMENTS)); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10131 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10132 | length_estimate = |
| 10133 | static_cast<uint32_t>(array->length()->Number()); |
| 10134 | element_estimate = |
| 10135 | EstimateElementCount(array); |
| 10136 | } else { |
| 10137 | length_estimate = 1; |
| 10138 | element_estimate = 1; |
| 10139 | } |
| 10140 | // Avoid overflows by capping at kMaxElementCount. |
| 10141 | if (JSObject::kMaxElementCount - estimate_result_length < |
| 10142 | length_estimate) { |
| 10143 | estimate_result_length = JSObject::kMaxElementCount; |
| 10144 | } else { |
| 10145 | estimate_result_length += length_estimate; |
| 10146 | } |
| 10147 | if (JSObject::kMaxElementCount - estimate_nof_elements < |
| 10148 | element_estimate) { |
| 10149 | estimate_nof_elements = JSObject::kMaxElementCount; |
| 10150 | } else { |
| 10151 | estimate_nof_elements += element_estimate; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10152 | } |
| 10153 | } |
| 10154 | } |
| 10155 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10156 | // If estimated number of elements is more than half of length, a |
| 10157 | // fixed array (fast case) is more time and space-efficient than a |
| 10158 | // dictionary. |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10159 | bool fast_case = (estimate_nof_elements * 2) >= estimate_result_length; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10160 | |
| 10161 | Handle<FixedArray> storage; |
| 10162 | if (fast_case) { |
| 10163 | // The backing storage array must have non-existing elements to |
| 10164 | // preserve holes across concat operations. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10165 | storage = isolate->factory()->NewFixedArrayWithHoles( |
| 10166 | estimate_result_length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10167 | } else { |
| 10168 | // TODO(126): move 25% pre-allocation logic into Dictionary::Allocate |
| 10169 | uint32_t at_least_space_for = estimate_nof_elements + |
| 10170 | (estimate_nof_elements >> 2); |
| 10171 | storage = Handle<FixedArray>::cast( |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 10172 | isolate->factory()->NewSeededNumberDictionary(at_least_space_for)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10173 | } |
| 10174 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10175 | ArrayConcatVisitor visitor(isolate, storage, fast_case); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10176 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10177 | for (int i = 0; i < argument_count; i++) { |
| 10178 | Handle<Object> obj(elements->get(i)); |
| 10179 | if (obj->IsJSArray()) { |
| 10180 | Handle<JSArray> array = Handle<JSArray>::cast(obj); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10181 | if (!IterateElements(isolate, array, &visitor)) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10182 | return Failure::Exception(); |
| 10183 | } |
| 10184 | } else { |
| 10185 | visitor.visit(0, obj); |
| 10186 | visitor.increase_index_offset(1); |
| 10187 | } |
| 10188 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10189 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 10190 | return *visitor.ToArray(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10191 | } |
| 10192 | |
| 10193 | |
| 10194 | // This will not allocate (flatten the string), but it may run |
| 10195 | // very slowly for very deeply nested ConsStrings. For debugging use only. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10196 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GlobalPrint) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10197 | NoHandleAllocation ha; |
| 10198 | ASSERT(args.length() == 1); |
| 10199 | |
| 10200 | CONVERT_CHECKED(String, string, args[0]); |
| 10201 | StringInputBuffer buffer(string); |
| 10202 | while (buffer.has_more()) { |
| 10203 | uint16_t character = buffer.GetNext(); |
| 10204 | PrintF("%c", character); |
| 10205 | } |
| 10206 | return string; |
| 10207 | } |
| 10208 | |
| 10209 | // Moves all own elements of an object, that are below a limit, to positions |
| 10210 | // starting at zero. All undefined values are placed after non-undefined values, |
| 10211 | // and are followed by non-existing element. Does not change the length |
| 10212 | // property. |
| 10213 | // Returns the number of non-undefined elements collected. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10214 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RemoveArrayHoles) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10215 | ASSERT(args.length() == 2); |
| 10216 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 10217 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]); |
| 10218 | return object->PrepareElementsForSort(limit); |
| 10219 | } |
| 10220 | |
| 10221 | |
| 10222 | // Move contents of argument 0 (an array) to argument 1 (an array) |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10223 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MoveArrayContents) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10224 | ASSERT(args.length() == 2); |
| 10225 | CONVERT_CHECKED(JSArray, from, args[0]); |
| 10226 | CONVERT_CHECKED(JSArray, to, args[1]); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 10227 | FixedArrayBase* new_elements = from->elements(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10228 | MaybeObject* maybe_new_map; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10229 | ElementsKind elements_kind; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10230 | if (new_elements->map() == isolate->heap()->fixed_array_map() || |
| 10231 | new_elements->map() == isolate->heap()->fixed_cow_array_map()) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10232 | elements_kind = FAST_ELEMENTS; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 10233 | } else if (new_elements->map() == |
| 10234 | isolate->heap()->fixed_double_array_map()) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10235 | elements_kind = FAST_DOUBLE_ELEMENTS; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 10236 | } else { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10237 | elements_kind = DICTIONARY_ELEMENTS; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 10238 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10239 | maybe_new_map = to->GetElementsTransitionMap(elements_kind); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10240 | Object* new_map; |
| 10241 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 10242 | to->set_map(Map::cast(new_map)); |
| 10243 | to->set_elements(new_elements); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10244 | to->set_length(from->length()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10245 | Object* obj; |
| 10246 | { MaybeObject* maybe_obj = from->ResetElements(); |
| 10247 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 10248 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 10249 | from->set_length(Smi::FromInt(0)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10250 | return to; |
| 10251 | } |
| 10252 | |
| 10253 | |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 10254 | // How many elements does this object/array have? |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10255 | RUNTIME_FUNCTION(MaybeObject*, Runtime_EstimateNumberOfElements) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10256 | ASSERT(args.length() == 1); |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 10257 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 10258 | HeapObject* elements = object->elements(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10259 | if (elements->IsDictionary()) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 10260 | int result = SeededNumberDictionary::cast(elements)->NumberOfElements(); |
| 10261 | return Smi::FromInt(result); |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 10262 | } else if (object->IsJSArray()) { |
| 10263 | return JSArray::cast(object)->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10264 | } else { |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 10265 | return Smi::FromInt(FixedArray::cast(elements)->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10266 | } |
| 10267 | } |
| 10268 | |
| 10269 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10270 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SwapElements) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10271 | HandleScope handle_scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10272 | |
| 10273 | ASSERT_EQ(3, args.length()); |
| 10274 | |
| 10275 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
| 10276 | Handle<Object> key1 = args.at<Object>(1); |
| 10277 | Handle<Object> key2 = args.at<Object>(2); |
| 10278 | |
| 10279 | uint32_t index1, index2; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10280 | if (!key1->ToArrayIndex(&index1) |
| 10281 | || !key2->ToArrayIndex(&index2)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10282 | return isolate->ThrowIllegalOperation(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10283 | } |
| 10284 | |
| 10285 | Handle<JSObject> jsobject = Handle<JSObject>::cast(object); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10286 | Handle<Object> tmp1 = Object::GetElement(jsobject, index1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10287 | RETURN_IF_EMPTY_HANDLE(isolate, tmp1); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10288 | Handle<Object> tmp2 = Object::GetElement(jsobject, index2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10289 | RETURN_IF_EMPTY_HANDLE(isolate, tmp2); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10290 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 10291 | RETURN_IF_EMPTY_HANDLE( |
| 10292 | isolate, JSObject::SetElement(jsobject, index1, tmp2, kStrictMode)); |
| 10293 | RETURN_IF_EMPTY_HANDLE( |
| 10294 | isolate, JSObject::SetElement(jsobject, index2, tmp1, kStrictMode)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10295 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10296 | return isolate->heap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10297 | } |
| 10298 | |
| 10299 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10300 | // 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] | 10301 | // might have elements. Can either return keys (positive integers) or |
| 10302 | // intervals (pair of a negative integer (-start-1) followed by a |
| 10303 | // positive (length)) or undefined values. |
| 10304 | // Intervals can span over some keys that are not in the object. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10305 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetArrayKeys) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10306 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10307 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10308 | CONVERT_ARG_CHECKED(JSObject, array, 0); |
| 10309 | CONVERT_NUMBER_CHECKED(uint32_t, length, Uint32, args[1]); |
| 10310 | if (array->elements()->IsDictionary()) { |
| 10311 | // Create an array and get all the keys into it, then remove all the |
| 10312 | // keys that are not integers in the range 0 to length-1. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10313 | bool threw = false; |
| 10314 | Handle<FixedArray> keys = |
| 10315 | GetKeysInFixedArrayFor(array, INCLUDE_PROTOS, &threw); |
| 10316 | if (threw) return Failure::Exception(); |
| 10317 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10318 | int keys_length = keys->length(); |
| 10319 | for (int i = 0; i < keys_length; i++) { |
| 10320 | Object* key = keys->get(i); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 10321 | uint32_t index = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10322 | if (!key->ToArrayIndex(&index) || index >= length) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10323 | // Zap invalid keys. |
| 10324 | keys->set_undefined(i); |
| 10325 | } |
| 10326 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10327 | return *isolate->factory()->NewJSArrayWithElements(keys); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10328 | } else { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10329 | ASSERT(array->HasFastElements() || |
| 10330 | array->HasFastSmiOnlyElements() || |
| 10331 | array->HasFastDoubleElements()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10332 | Handle<FixedArray> single_interval = isolate->factory()->NewFixedArray(2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10333 | // -1 means start of array. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 10334 | single_interval->set(0, Smi::FromInt(-1)); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 10335 | FixedArrayBase* elements = FixedArrayBase::cast(array->elements()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10336 | uint32_t actual_length = |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 10337 | static_cast<uint32_t>(elements->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10338 | uint32_t min_length = actual_length < length ? actual_length : length; |
| 10339 | Handle<Object> length_object = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10340 | isolate->factory()->NewNumber(static_cast<double>(min_length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10341 | single_interval->set(1, *length_object); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10342 | return *isolate->factory()->NewJSArrayWithElements(single_interval); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10343 | } |
| 10344 | } |
| 10345 | |
| 10346 | |
| 10347 | // DefineAccessor takes an optional final argument which is the |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 10348 | // property attributes (e.g. DONT_ENUM, DONT_DELETE). IMPORTANT: due |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10349 | // to the way accessors are implemented, it is set for both the getter |
| 10350 | // and setter on the first call to DefineAccessor and ignored on |
| 10351 | // subsequent calls. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10352 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DefineAccessor) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10353 | RUNTIME_ASSERT(args.length() == 4 || args.length() == 5); |
| 10354 | // Compute attributes. |
| 10355 | PropertyAttributes attributes = NONE; |
| 10356 | if (args.length() == 5) { |
| 10357 | CONVERT_CHECKED(Smi, attrs, args[4]); |
| 10358 | int value = attrs->value(); |
| 10359 | // Only attribute bits should be set. |
| 10360 | ASSERT((value & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 10361 | attributes = static_cast<PropertyAttributes>(value); |
| 10362 | } |
| 10363 | |
| 10364 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 10365 | CONVERT_CHECKED(String, name, args[1]); |
| 10366 | CONVERT_CHECKED(Smi, flag, args[2]); |
| 10367 | CONVERT_CHECKED(JSFunction, fun, args[3]); |
| 10368 | return obj->DefineAccessor(name, flag->value() == 0, fun, attributes); |
| 10369 | } |
| 10370 | |
| 10371 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10372 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LookupAccessor) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10373 | ASSERT(args.length() == 3); |
| 10374 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 10375 | CONVERT_CHECKED(String, name, args[1]); |
| 10376 | CONVERT_CHECKED(Smi, flag, args[2]); |
| 10377 | return obj->LookupAccessor(name, flag->value() == 0); |
| 10378 | } |
| 10379 | |
| 10380 | |
| 10381 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10382 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugBreak) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10383 | ASSERT(args.length() == 0); |
| 10384 | return Execution::DebugBreakHelper(); |
| 10385 | } |
| 10386 | |
| 10387 | |
| 10388 | // Helper functions for wrapping and unwrapping stack frame ids. |
| 10389 | static Smi* WrapFrameId(StackFrame::Id id) { |
| 10390 | ASSERT(IsAligned(OffsetFrom(id), static_cast<intptr_t>(4))); |
| 10391 | return Smi::FromInt(id >> 2); |
| 10392 | } |
| 10393 | |
| 10394 | |
| 10395 | static StackFrame::Id UnwrapFrameId(Smi* wrapped) { |
| 10396 | return static_cast<StackFrame::Id>(wrapped->value() << 2); |
| 10397 | } |
| 10398 | |
| 10399 | |
| 10400 | // Adds a JavaScript function as a debug event listener. |
| 10401 | // args[0]: debug event listener function to set or null or undefined for |
| 10402 | // clearing the event listener function |
| 10403 | // args[1]: object supplied during callback |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10404 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetDebugEventListener) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10405 | ASSERT(args.length() == 2); |
| 10406 | RUNTIME_ASSERT(args[0]->IsJSFunction() || |
| 10407 | args[0]->IsUndefined() || |
| 10408 | args[0]->IsNull()); |
| 10409 | Handle<Object> callback = args.at<Object>(0); |
| 10410 | Handle<Object> data = args.at<Object>(1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10411 | isolate->debugger()->SetEventListener(callback, data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10412 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10413 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10414 | } |
| 10415 | |
| 10416 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10417 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Break) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10418 | ASSERT(args.length() == 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10419 | isolate->stack_guard()->DebugBreak(); |
| 10420 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10421 | } |
| 10422 | |
| 10423 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10424 | static MaybeObject* DebugLookupResultValue(Heap* heap, |
| 10425 | Object* receiver, |
| 10426 | String* name, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10427 | LookupResult* result, |
| 10428 | bool* caught_exception) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10429 | Object* value; |
| 10430 | switch (result->type()) { |
| 10431 | case NORMAL: |
| 10432 | value = result->holder()->GetNormalizedProperty(result); |
| 10433 | if (value->IsTheHole()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10434 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10435 | } |
| 10436 | return value; |
| 10437 | case FIELD: |
| 10438 | value = |
| 10439 | JSObject::cast( |
| 10440 | result->holder())->FastPropertyAt(result->GetFieldIndex()); |
| 10441 | if (value->IsTheHole()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10442 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10443 | } |
| 10444 | return value; |
| 10445 | case CONSTANT_FUNCTION: |
| 10446 | return result->GetConstantFunction(); |
| 10447 | case CALLBACKS: { |
| 10448 | Object* structure = result->GetCallbackObject(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 10449 | if (structure->IsForeign() || structure->IsAccessorInfo()) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10450 | MaybeObject* maybe_value = result->holder()->GetPropertyWithCallback( |
| 10451 | receiver, structure, name); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10452 | if (!maybe_value->ToObject(&value)) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 10453 | if (maybe_value->IsRetryAfterGC()) return maybe_value; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10454 | ASSERT(maybe_value->IsException()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10455 | maybe_value = heap->isolate()->pending_exception(); |
| 10456 | heap->isolate()->clear_pending_exception(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10457 | if (caught_exception != NULL) { |
| 10458 | *caught_exception = true; |
| 10459 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10460 | return maybe_value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10461 | } |
| 10462 | return value; |
| 10463 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10464 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10465 | } |
| 10466 | } |
| 10467 | case INTERCEPTOR: |
| 10468 | case MAP_TRANSITION: |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 10469 | case ELEMENTS_TRANSITION: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10470 | case CONSTANT_TRANSITION: |
| 10471 | case NULL_DESCRIPTOR: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10472 | return heap->undefined_value(); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10473 | case HANDLER: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10474 | UNREACHABLE(); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10475 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10476 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10477 | UNREACHABLE(); // keep the compiler happy |
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 | } |
| 10480 | |
| 10481 | |
| 10482 | // Get debugger related details for an object property. |
| 10483 | // args[0]: object holding property |
| 10484 | // args[1]: name of the property |
| 10485 | // |
| 10486 | // The array returned contains the following information: |
| 10487 | // 0: Property value |
| 10488 | // 1: Property details |
| 10489 | // 2: Property value is exception |
| 10490 | // 3: Getter function if defined |
| 10491 | // 4: Setter function if defined |
| 10492 | // Items 2-4 are only filled if the property has either a getter or a setter |
| 10493 | // defined through __defineGetter__ and/or __defineSetter__. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10494 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetPropertyDetails) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10495 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10496 | |
| 10497 | ASSERT(args.length() == 2); |
| 10498 | |
| 10499 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 10500 | CONVERT_ARG_CHECKED(String, name, 1); |
| 10501 | |
| 10502 | // Make sure to set the current context to the context before the debugger was |
| 10503 | // entered (if the debugger is entered). The reason for switching context here |
| 10504 | // is that for some property lookups (accessors and interceptors) callbacks |
| 10505 | // into the embedding application can occour, and the embedding application |
| 10506 | // could have the assumption that its own global context is the current |
| 10507 | // context and not some internal debugger context. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10508 | SaveContext save(isolate); |
| 10509 | if (isolate->debug()->InDebugger()) { |
| 10510 | isolate->set_context(*isolate->debug()->debugger_entry()->GetContext()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10511 | } |
| 10512 | |
| 10513 | // Skip the global proxy as it has no properties and always delegates to the |
| 10514 | // real global object. |
| 10515 | if (obj->IsJSGlobalProxy()) { |
| 10516 | obj = Handle<JSObject>(JSObject::cast(obj->GetPrototype())); |
| 10517 | } |
| 10518 | |
| 10519 | |
| 10520 | // Check if the name is trivially convertible to an index and get the element |
| 10521 | // if so. |
| 10522 | uint32_t index; |
| 10523 | if (name->AsArrayIndex(&index)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10524 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(2); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10525 | Object* element_or_char; |
| 10526 | { MaybeObject* maybe_element_or_char = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10527 | Runtime::GetElementOrCharAt(isolate, obj, index); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10528 | if (!maybe_element_or_char->ToObject(&element_or_char)) { |
| 10529 | return maybe_element_or_char; |
| 10530 | } |
| 10531 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 10532 | details->set(0, element_or_char); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10533 | details->set(1, PropertyDetails(NONE, NORMAL).AsSmi()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10534 | return *isolate->factory()->NewJSArrayWithElements(details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10535 | } |
| 10536 | |
| 10537 | // Find the number of objects making up this. |
| 10538 | int length = LocalPrototypeChainLength(*obj); |
| 10539 | |
| 10540 | // Try local lookup on each of the objects. |
| 10541 | Handle<JSObject> jsproto = obj; |
| 10542 | for (int i = 0; i < length; i++) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10543 | LookupResult result(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10544 | jsproto->LocalLookup(*name, &result); |
| 10545 | if (result.IsProperty()) { |
| 10546 | // LookupResult is not GC safe as it holds raw object pointers. |
| 10547 | // GC can happen later in this code so put the required fields into |
| 10548 | // local variables using handles when required for later use. |
| 10549 | PropertyType result_type = result.type(); |
| 10550 | Handle<Object> result_callback_obj; |
| 10551 | if (result_type == CALLBACKS) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10552 | result_callback_obj = Handle<Object>(result.GetCallbackObject(), |
| 10553 | isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10554 | } |
| 10555 | Smi* property_details = result.GetPropertyDetails().AsSmi(); |
| 10556 | // DebugLookupResultValue can cause GC so details from LookupResult needs |
| 10557 | // to be copied to handles before this. |
| 10558 | bool caught_exception = false; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10559 | Object* raw_value; |
| 10560 | { MaybeObject* maybe_raw_value = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10561 | DebugLookupResultValue(isolate->heap(), *obj, *name, |
| 10562 | &result, &caught_exception); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10563 | if (!maybe_raw_value->ToObject(&raw_value)) return maybe_raw_value; |
| 10564 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10565 | Handle<Object> value(raw_value, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10566 | |
| 10567 | // If the callback object is a fixed array then it contains JavaScript |
| 10568 | // getter and/or setter. |
| 10569 | bool hasJavaScriptAccessors = result_type == CALLBACKS && |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 10570 | result_callback_obj->IsAccessorPair(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10571 | Handle<FixedArray> details = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10572 | isolate->factory()->NewFixedArray(hasJavaScriptAccessors ? 5 : 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10573 | details->set(0, *value); |
| 10574 | details->set(1, property_details); |
| 10575 | if (hasJavaScriptAccessors) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10576 | details->set(2, isolate->heap()->ToBoolean(caught_exception)); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 10577 | details->set(3, AccessorPair::cast(*result_callback_obj)->getter()); |
| 10578 | details->set(4, AccessorPair::cast(*result_callback_obj)->setter()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10579 | } |
| 10580 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10581 | return *isolate->factory()->NewJSArrayWithElements(details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10582 | } |
| 10583 | if (i < length - 1) { |
| 10584 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 10585 | } |
| 10586 | } |
| 10587 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10588 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10589 | } |
| 10590 | |
| 10591 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10592 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetProperty) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10593 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10594 | |
| 10595 | ASSERT(args.length() == 2); |
| 10596 | |
| 10597 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 10598 | CONVERT_ARG_CHECKED(String, name, 1); |
| 10599 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10600 | LookupResult result(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10601 | obj->Lookup(*name, &result); |
| 10602 | if (result.IsProperty()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10603 | return DebugLookupResultValue(isolate->heap(), *obj, *name, &result, NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10604 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10605 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10606 | } |
| 10607 | |
| 10608 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10609 | // Return the property type calculated from the property details. |
| 10610 | // args[0]: smi with property details. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10611 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPropertyTypeFromDetails) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10612 | ASSERT(args.length() == 1); |
| 10613 | CONVERT_CHECKED(Smi, details, args[0]); |
| 10614 | PropertyType type = PropertyDetails(details).type(); |
| 10615 | return Smi::FromInt(static_cast<int>(type)); |
| 10616 | } |
| 10617 | |
| 10618 | |
| 10619 | // Return the property attribute calculated from the property details. |
| 10620 | // args[0]: smi with property details. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10621 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPropertyAttributesFromDetails) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10622 | ASSERT(args.length() == 1); |
| 10623 | CONVERT_CHECKED(Smi, details, args[0]); |
| 10624 | PropertyAttributes attributes = PropertyDetails(details).attributes(); |
| 10625 | return Smi::FromInt(static_cast<int>(attributes)); |
| 10626 | } |
| 10627 | |
| 10628 | |
| 10629 | // Return the property insertion index calculated from the property details. |
| 10630 | // args[0]: smi with property details. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10631 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPropertyIndexFromDetails) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10632 | ASSERT(args.length() == 1); |
| 10633 | CONVERT_CHECKED(Smi, details, args[0]); |
| 10634 | int index = PropertyDetails(details).index(); |
| 10635 | return Smi::FromInt(index); |
| 10636 | } |
| 10637 | |
| 10638 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10639 | // Return property value from named interceptor. |
| 10640 | // args[0]: object |
| 10641 | // args[1]: property name |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10642 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugNamedInterceptorPropertyValue) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10643 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10644 | ASSERT(args.length() == 2); |
| 10645 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 10646 | RUNTIME_ASSERT(obj->HasNamedInterceptor()); |
| 10647 | CONVERT_ARG_CHECKED(String, name, 1); |
| 10648 | |
| 10649 | PropertyAttributes attributes; |
| 10650 | return obj->GetPropertyWithInterceptor(*obj, *name, &attributes); |
| 10651 | } |
| 10652 | |
| 10653 | |
| 10654 | // Return element value from indexed interceptor. |
| 10655 | // args[0]: object |
| 10656 | // args[1]: index |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10657 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugIndexedInterceptorElementValue) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10658 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10659 | ASSERT(args.length() == 2); |
| 10660 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 10661 | RUNTIME_ASSERT(obj->HasIndexedInterceptor()); |
| 10662 | CONVERT_NUMBER_CHECKED(uint32_t, index, Uint32, args[1]); |
| 10663 | |
| 10664 | return obj->GetElementWithInterceptor(*obj, index); |
| 10665 | } |
| 10666 | |
| 10667 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10668 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CheckExecutionState) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10669 | ASSERT(args.length() >= 1); |
| 10670 | CONVERT_NUMBER_CHECKED(int, break_id, Int32, args[0]); |
| 10671 | // Check that the break id is valid. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10672 | if (isolate->debug()->break_id() == 0 || |
| 10673 | break_id != isolate->debug()->break_id()) { |
| 10674 | return isolate->Throw( |
| 10675 | isolate->heap()->illegal_execution_state_symbol()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10676 | } |
| 10677 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10678 | return isolate->heap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10679 | } |
| 10680 | |
| 10681 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10682 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFrameCount) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10683 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10684 | ASSERT(args.length() == 1); |
| 10685 | |
| 10686 | // Check arguments. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10687 | Object* result; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10688 | { MaybeObject* maybe_result = Runtime_CheckExecutionState( |
| 10689 | RUNTIME_ARGUMENTS(isolate, args)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10690 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 10691 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10692 | |
| 10693 | // Count all frames which are relevant to debugging stack trace. |
| 10694 | int n = 0; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10695 | StackFrame::Id id = isolate->debug()->break_frame_id(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10696 | if (id == StackFrame::NO_ID) { |
| 10697 | // If there is no JavaScript stack frame count is 0. |
| 10698 | return Smi::FromInt(0); |
| 10699 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10700 | |
| 10701 | for (JavaScriptFrameIterator it(isolate, id); !it.done(); it.Advance()) { |
| 10702 | n += it.frame()->GetInlineCount(); |
| 10703 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10704 | return Smi::FromInt(n); |
| 10705 | } |
| 10706 | |
| 10707 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10708 | class FrameInspector { |
| 10709 | public: |
| 10710 | FrameInspector(JavaScriptFrame* frame, |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 10711 | int inlined_jsframe_index, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10712 | Isolate* isolate) |
| 10713 | : frame_(frame), deoptimized_frame_(NULL), isolate_(isolate) { |
| 10714 | // Calculate the deoptimized frame. |
| 10715 | if (frame->is_optimized()) { |
| 10716 | deoptimized_frame_ = Deoptimizer::DebuggerInspectableFrame( |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 10717 | frame, inlined_jsframe_index, isolate); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10718 | } |
| 10719 | has_adapted_arguments_ = frame_->has_adapted_arguments(); |
| 10720 | is_optimized_ = frame_->is_optimized(); |
| 10721 | } |
| 10722 | |
| 10723 | ~FrameInspector() { |
| 10724 | // Get rid of the calculated deoptimized frame if any. |
| 10725 | if (deoptimized_frame_ != NULL) { |
| 10726 | Deoptimizer::DeleteDebuggerInspectableFrame(deoptimized_frame_, |
| 10727 | isolate_); |
| 10728 | } |
| 10729 | } |
| 10730 | |
| 10731 | int GetParametersCount() { |
| 10732 | return is_optimized_ |
| 10733 | ? deoptimized_frame_->parameters_count() |
| 10734 | : frame_->ComputeParametersCount(); |
| 10735 | } |
| 10736 | int expression_count() { return deoptimized_frame_->expression_count(); } |
| 10737 | Object* GetFunction() { |
| 10738 | return is_optimized_ |
| 10739 | ? deoptimized_frame_->GetFunction() |
| 10740 | : frame_->function(); |
| 10741 | } |
| 10742 | Object* GetParameter(int index) { |
| 10743 | return is_optimized_ |
| 10744 | ? deoptimized_frame_->GetParameter(index) |
| 10745 | : frame_->GetParameter(index); |
| 10746 | } |
| 10747 | Object* GetExpression(int index) { |
| 10748 | return is_optimized_ |
| 10749 | ? deoptimized_frame_->GetExpression(index) |
| 10750 | : frame_->GetExpression(index); |
| 10751 | } |
| 10752 | |
| 10753 | // To inspect all the provided arguments the frame might need to be |
| 10754 | // replaced with the arguments frame. |
| 10755 | void SetArgumentsFrame(JavaScriptFrame* frame) { |
| 10756 | ASSERT(has_adapted_arguments_); |
| 10757 | frame_ = frame; |
| 10758 | is_optimized_ = frame_->is_optimized(); |
| 10759 | ASSERT(!is_optimized_); |
| 10760 | } |
| 10761 | |
| 10762 | private: |
| 10763 | JavaScriptFrame* frame_; |
| 10764 | DeoptimizedFrameInfo* deoptimized_frame_; |
| 10765 | Isolate* isolate_; |
| 10766 | bool is_optimized_; |
| 10767 | bool has_adapted_arguments_; |
| 10768 | |
| 10769 | DISALLOW_COPY_AND_ASSIGN(FrameInspector); |
| 10770 | }; |
| 10771 | |
| 10772 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10773 | static const int kFrameDetailsFrameIdIndex = 0; |
| 10774 | static const int kFrameDetailsReceiverIndex = 1; |
| 10775 | static const int kFrameDetailsFunctionIndex = 2; |
| 10776 | static const int kFrameDetailsArgumentCountIndex = 3; |
| 10777 | static const int kFrameDetailsLocalCountIndex = 4; |
| 10778 | static const int kFrameDetailsSourcePositionIndex = 5; |
| 10779 | static const int kFrameDetailsConstructCallIndex = 6; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10780 | static const int kFrameDetailsAtReturnIndex = 7; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10781 | static const int kFrameDetailsFlagsIndex = 8; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10782 | static const int kFrameDetailsFirstDynamicIndex = 9; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10783 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10784 | |
| 10785 | static SaveContext* FindSavedContextForFrame(Isolate* isolate, |
| 10786 | JavaScriptFrame* frame) { |
| 10787 | SaveContext* save = isolate->save_context(); |
| 10788 | while (save != NULL && !save->IsBelowFrame(frame)) { |
| 10789 | save = save->prev(); |
| 10790 | } |
| 10791 | ASSERT(save != NULL); |
| 10792 | return save; |
| 10793 | } |
| 10794 | |
| 10795 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10796 | // Return an array with frame details |
| 10797 | // args[0]: number: break id |
| 10798 | // args[1]: number: frame index |
| 10799 | // |
| 10800 | // The array returned contains the following information: |
| 10801 | // 0: Frame id |
| 10802 | // 1: Receiver |
| 10803 | // 2: Function |
| 10804 | // 3: Argument count |
| 10805 | // 4: Local count |
| 10806 | // 5: Source position |
| 10807 | // 6: Constructor call |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10808 | // 7: Is at return |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10809 | // 8: Flags |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10810 | // Arguments name, value |
| 10811 | // Locals name, value |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10812 | // Return value if any |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10813 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFrameDetails) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10814 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10815 | ASSERT(args.length() == 2); |
| 10816 | |
| 10817 | // Check arguments. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10818 | Object* check; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10819 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 10820 | RUNTIME_ARGUMENTS(isolate, args)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10821 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 10822 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10823 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[1]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10824 | Heap* heap = isolate->heap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10825 | |
| 10826 | // Find the relevant frame with the requested index. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10827 | StackFrame::Id id = isolate->debug()->break_frame_id(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10828 | if (id == StackFrame::NO_ID) { |
| 10829 | // If there are no JavaScript stack frames return undefined. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10830 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10831 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10832 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10833 | int count = 0; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10834 | JavaScriptFrameIterator it(isolate, id); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10835 | for (; !it.done(); it.Advance()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10836 | if (index < count + it.frame()->GetInlineCount()) break; |
| 10837 | count += it.frame()->GetInlineCount(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10838 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10839 | if (it.done()) return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10840 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 10841 | bool is_optimized = it.frame()->is_optimized(); |
| 10842 | |
| 10843 | int inlined_jsframe_index = 0; // Inlined frame index in optimized frame. |
| 10844 | if (is_optimized) { |
| 10845 | inlined_jsframe_index = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10846 | it.frame()->GetInlineCount() - (index - count) - 1; |
| 10847 | } |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 10848 | FrameInspector frame_inspector(it.frame(), inlined_jsframe_index, isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 10849 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10850 | // Traverse the saved contexts chain to find the active context for the |
| 10851 | // selected frame. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10852 | SaveContext* save = FindSavedContextForFrame(isolate, it.frame()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10853 | |
| 10854 | // Get the frame id. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10855 | Handle<Object> frame_id(WrapFrameId(it.frame()->id()), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10856 | |
| 10857 | // Find source position. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10858 | int position = |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10859 | it.frame()->LookupCode()->SourcePosition(it.frame()->pc()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10860 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10861 | // Check for constructor frame. Inlined frames cannot be construct calls. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 10862 | bool inlined_frame = is_optimized && inlined_jsframe_index != 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10863 | bool constructor = !inlined_frame && it.frame()->IsConstructor(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10864 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 10865 | // Get scope info and read from it for local variable information. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 10866 | Handle<JSFunction> function(JSFunction::cast(frame_inspector.GetFunction())); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10867 | Handle<SharedFunctionInfo> shared(function->shared()); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10868 | Handle<ScopeInfo> scope_info(shared->scope_info()); |
| 10869 | ASSERT(*scope_info != ScopeInfo::Empty()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10870 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10871 | // Get the locals names and values into a temporary array. |
| 10872 | // |
| 10873 | // TODO(1240907): Hide compiler-introduced stack variables |
| 10874 | // (e.g. .result)? For users of the debugger, they will probably be |
| 10875 | // confusing. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10876 | Handle<FixedArray> locals = |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10877 | isolate->factory()->NewFixedArray(scope_info->LocalCount() * 2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10878 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 10879 | // Fill in the values of the locals. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10880 | int i = 0; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10881 | for (; i < scope_info->StackLocalCount(); ++i) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10882 | // Use the value from the stack. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10883 | locals->set(i * 2, scope_info->LocalName(i)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10884 | locals->set(i * 2 + 1, frame_inspector.GetExpression(i)); |
| 10885 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10886 | if (i < scope_info->LocalCount()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10887 | // Get the context containing declarations. |
| 10888 | Handle<Context> context( |
| 10889 | Context::cast(it.frame()->context())->declaration_context()); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10890 | for (; i < scope_info->LocalCount(); ++i) { |
| 10891 | Handle<String> name(scope_info->LocalName(i)); |
| 10892 | VariableMode mode; |
| 10893 | InitializationFlag init_flag; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10894 | locals->set(i * 2, *name); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10895 | locals->set(i * 2 + 1, context->get( |
| 10896 | scope_info->ContextSlotIndex(*name, &mode, &init_flag))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10897 | } |
| 10898 | } |
| 10899 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 10900 | // Check whether this frame is positioned at return. If not top |
| 10901 | // frame or if the frame is optimized it cannot be at a return. |
| 10902 | bool at_return = false; |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 10903 | if (!is_optimized && index == 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10904 | at_return = isolate->debug()->IsBreakAtReturn(it.frame()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 10905 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10906 | |
| 10907 | // If positioned just before return find the value to be returned and add it |
| 10908 | // to the frame information. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10909 | Handle<Object> return_value = isolate->factory()->undefined_value(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10910 | if (at_return) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10911 | StackFrameIterator it2(isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10912 | Address internal_frame_sp = NULL; |
| 10913 | while (!it2.done()) { |
| 10914 | if (it2.frame()->is_internal()) { |
| 10915 | internal_frame_sp = it2.frame()->sp(); |
| 10916 | } else { |
| 10917 | if (it2.frame()->is_java_script()) { |
| 10918 | if (it2.frame()->id() == it.frame()->id()) { |
| 10919 | // The internal frame just before the JavaScript frame contains the |
| 10920 | // value to return on top. A debug break at return will create an |
| 10921 | // internal frame to store the return value (eax/rax/r0) before |
| 10922 | // entering the debug break exit frame. |
| 10923 | if (internal_frame_sp != NULL) { |
| 10924 | return_value = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10925 | Handle<Object>(Memory::Object_at(internal_frame_sp), |
| 10926 | isolate); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10927 | break; |
| 10928 | } |
| 10929 | } |
| 10930 | } |
| 10931 | |
| 10932 | // Indicate that the previous frame was not an internal frame. |
| 10933 | internal_frame_sp = NULL; |
| 10934 | } |
| 10935 | it2.Advance(); |
| 10936 | } |
| 10937 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10938 | |
| 10939 | // Now advance to the arguments adapter frame (if any). It contains all |
| 10940 | // the provided parameters whereas the function frame always have the number |
| 10941 | // of arguments matching the functions parameters. The rest of the |
| 10942 | // information (except for what is collected above) is the same. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 10943 | if ((inlined_jsframe_index == 0) && it.frame()->has_adapted_arguments()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10944 | it.AdvanceToArgumentsFrame(); |
| 10945 | frame_inspector.SetArgumentsFrame(it.frame()); |
| 10946 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10947 | |
| 10948 | // Find the number of arguments to fill. At least fill the number of |
| 10949 | // parameters for the function and fill more if more parameters are provided. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10950 | int argument_count = scope_info->ParameterCount(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10951 | if (argument_count < frame_inspector.GetParametersCount()) { |
| 10952 | argument_count = frame_inspector.GetParametersCount(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10953 | } |
| 10954 | |
| 10955 | // Calculate the size of the result. |
| 10956 | int details_size = kFrameDetailsFirstDynamicIndex + |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10957 | 2 * (argument_count + scope_info->LocalCount()) + |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10958 | (at_return ? 1 : 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10959 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(details_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10960 | |
| 10961 | // Add the frame id. |
| 10962 | details->set(kFrameDetailsFrameIdIndex, *frame_id); |
| 10963 | |
| 10964 | // Add the function (same as in function frame). |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10965 | details->set(kFrameDetailsFunctionIndex, frame_inspector.GetFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10966 | |
| 10967 | // Add the arguments count. |
| 10968 | details->set(kFrameDetailsArgumentCountIndex, Smi::FromInt(argument_count)); |
| 10969 | |
| 10970 | // Add the locals count |
| 10971 | details->set(kFrameDetailsLocalCountIndex, |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 10972 | Smi::FromInt(scope_info->LocalCount())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10973 | |
| 10974 | // Add the source position. |
| 10975 | if (position != RelocInfo::kNoPosition) { |
| 10976 | details->set(kFrameDetailsSourcePositionIndex, Smi::FromInt(position)); |
| 10977 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10978 | details->set(kFrameDetailsSourcePositionIndex, heap->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10979 | } |
| 10980 | |
| 10981 | // Add the constructor information. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10982 | details->set(kFrameDetailsConstructCallIndex, heap->ToBoolean(constructor)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10983 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10984 | // Add the at return information. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10985 | details->set(kFrameDetailsAtReturnIndex, heap->ToBoolean(at_return)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 10986 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10987 | // Add flags to indicate information on whether this frame is |
| 10988 | // bit 0: invoked in the debugger context. |
| 10989 | // bit 1: optimized frame. |
| 10990 | // bit 2: inlined in optimized frame |
| 10991 | int flags = 0; |
| 10992 | if (*save->context() == *isolate->debug()->debug_context()) { |
| 10993 | flags |= 1 << 0; |
| 10994 | } |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 10995 | if (is_optimized) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10996 | flags |= 1 << 1; |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 10997 | flags |= inlined_jsframe_index << 2; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 10998 | } |
| 10999 | details->set(kFrameDetailsFlagsIndex, Smi::FromInt(flags)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11000 | |
| 11001 | // Fill the dynamic part. |
| 11002 | int details_index = kFrameDetailsFirstDynamicIndex; |
| 11003 | |
| 11004 | // Add arguments name and value. |
| 11005 | for (int i = 0; i < argument_count; i++) { |
| 11006 | // Name of the argument. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11007 | if (i < scope_info->ParameterCount()) { |
| 11008 | details->set(details_index++, scope_info->ParameterName(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11009 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11010 | details->set(details_index++, heap->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11011 | } |
| 11012 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11013 | // Parameter value. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 11014 | if (i < frame_inspector.GetParametersCount()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11015 | // Get the value from the stack. |
| 11016 | details->set(details_index++, frame_inspector.GetParameter(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11017 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11018 | details->set(details_index++, heap->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11019 | } |
| 11020 | } |
| 11021 | |
| 11022 | // Add locals name and value from the temporary copy from the function frame. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11023 | for (int i = 0; i < scope_info->LocalCount() * 2; i++) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11024 | details->set(details_index++, locals->get(i)); |
| 11025 | } |
| 11026 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 11027 | // Add the value being returned. |
| 11028 | if (at_return) { |
| 11029 | details->set(details_index++, *return_value); |
| 11030 | } |
| 11031 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11032 | // Add the receiver (same as in function frame). |
| 11033 | // THIS MUST BE DONE LAST SINCE WE MIGHT ADVANCE |
| 11034 | // THE FRAME ITERATOR TO WRAP THE RECEIVER. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11035 | Handle<Object> receiver(it.frame()->receiver(), isolate); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11036 | if (!receiver->IsJSObject() && |
| 11037 | shared->is_classic_mode() && |
| 11038 | !shared->native()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11039 | // If the receiver is not a JSObject and the function is not a |
| 11040 | // builtin or strict-mode we have hit an optimization where a |
| 11041 | // value object is not converted into a wrapped JS objects. To |
| 11042 | // hide this optimization from the debugger, we wrap the receiver |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11043 | // by creating correct wrapper object based on the calling frame's |
| 11044 | // global context. |
| 11045 | it.Advance(); |
| 11046 | Handle<Context> calling_frames_global_context( |
| 11047 | Context::cast(Context::cast(it.frame()->context())->global_context())); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11048 | receiver = |
| 11049 | isolate->factory()->ToObject(receiver, calling_frames_global_context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11050 | } |
| 11051 | details->set(kFrameDetailsReceiverIndex, *receiver); |
| 11052 | |
| 11053 | ASSERT_EQ(details_size, details_index); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11054 | return *isolate->factory()->NewJSArrayWithElements(details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11055 | } |
| 11056 | |
| 11057 | |
| 11058 | // 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] | 11059 | static bool CopyContextLocalsToScopeObject( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11060 | Isolate* isolate, |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11061 | Handle<ScopeInfo> scope_info, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 11062 | Handle<Context> context, |
| 11063 | Handle<JSObject> scope_object) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11064 | // Fill all context locals to the context extension. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11065 | for (int i = 0; i < scope_info->ContextLocalCount(); i++) { |
| 11066 | VariableMode mode; |
| 11067 | InitializationFlag init_flag; |
| 11068 | int context_index = scope_info->ContextSlotIndex( |
| 11069 | scope_info->ContextLocalName(i), &mode, &init_flag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11070 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11071 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 11072 | isolate, |
| 11073 | SetProperty(scope_object, |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11074 | Handle<String>(scope_info->ContextLocalName(i)), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11075 | Handle<Object>(context->get(context_index), isolate), |
| 11076 | NONE, |
| 11077 | kNonStrictMode), |
| 11078 | false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11079 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 11080 | |
| 11081 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11082 | } |
| 11083 | |
| 11084 | |
| 11085 | // Create a plain JSObject which materializes the local scope for the specified |
| 11086 | // frame. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 11087 | static Handle<JSObject> MaterializeLocalScopeWithFrameInspector( |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11088 | Isolate* isolate, |
| 11089 | JavaScriptFrame* frame, |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 11090 | FrameInspector* frame_inspector) { |
| 11091 | Handle<JSFunction> function(JSFunction::cast(frame_inspector->GetFunction())); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 11092 | Handle<SharedFunctionInfo> shared(function->shared()); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11093 | Handle<ScopeInfo> scope_info(shared->scope_info()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11094 | |
| 11095 | // Allocate and initialize a JSObject with all the arguments, stack locals |
| 11096 | // heap locals and extension properties of the debugged function. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11097 | Handle<JSObject> local_scope = |
| 11098 | isolate->factory()->NewJSObject(isolate->object_function()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11099 | |
| 11100 | // First fill all parameters. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11101 | for (int i = 0; i < scope_info->ParameterCount(); ++i) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 11102 | Handle<Object> value( |
| 11103 | i < frame_inspector->GetParametersCount() ? |
| 11104 | frame_inspector->GetParameter(i) : isolate->heap()->undefined_value()); |
| 11105 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 11106 | RETURN_IF_EMPTY_HANDLE_VALUE( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11107 | isolate, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 11108 | SetProperty(local_scope, |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11109 | Handle<String>(scope_info->ParameterName(i)), |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 11110 | value, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 11111 | NONE, |
| 11112 | kNonStrictMode), |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 11113 | Handle<JSObject>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11114 | } |
| 11115 | |
| 11116 | // Second fill all stack locals. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11117 | for (int i = 0; i < scope_info->StackLocalCount(); ++i) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 11118 | RETURN_IF_EMPTY_HANDLE_VALUE( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11119 | isolate, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 11120 | SetProperty(local_scope, |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11121 | Handle<String>(scope_info->StackLocalName(i)), |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 11122 | Handle<Object>(frame_inspector->GetExpression(i)), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 11123 | NONE, |
| 11124 | kNonStrictMode), |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 11125 | Handle<JSObject>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11126 | } |
| 11127 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11128 | if (scope_info->HasContext()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11129 | // Third fill all context locals. |
| 11130 | Handle<Context> frame_context(Context::cast(frame->context())); |
| 11131 | Handle<Context> function_context(frame_context->declaration_context()); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11132 | if (!CopyContextLocalsToScopeObject( |
| 11133 | isolate, scope_info, function_context, local_scope)) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11134 | return Handle<JSObject>(); |
| 11135 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11136 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11137 | // Finally copy any properties from the function context extension. |
| 11138 | // These will be variables introduced by eval. |
| 11139 | if (function_context->closure() == *function) { |
| 11140 | if (function_context->has_extension() && |
| 11141 | !function_context->IsGlobalContext()) { |
| 11142 | Handle<JSObject> ext(JSObject::cast(function_context->extension())); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11143 | bool threw = false; |
| 11144 | Handle<FixedArray> keys = |
| 11145 | GetKeysInFixedArrayFor(ext, INCLUDE_PROTOS, &threw); |
| 11146 | if (threw) return Handle<JSObject>(); |
| 11147 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11148 | for (int i = 0; i < keys->length(); i++) { |
| 11149 | // Names of variables introduced by eval are strings. |
| 11150 | ASSERT(keys->get(i)->IsString()); |
| 11151 | Handle<String> key(String::cast(keys->get(i))); |
| 11152 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 11153 | isolate, |
| 11154 | SetProperty(local_scope, |
| 11155 | key, |
| 11156 | GetProperty(ext, key), |
| 11157 | NONE, |
| 11158 | kNonStrictMode), |
| 11159 | Handle<JSObject>()); |
| 11160 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11161 | } |
| 11162 | } |
| 11163 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11164 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11165 | return local_scope; |
| 11166 | } |
| 11167 | |
| 11168 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 11169 | static Handle<JSObject> MaterializeLocalScope( |
| 11170 | Isolate* isolate, |
| 11171 | JavaScriptFrame* frame, |
| 11172 | int inlined_jsframe_index) { |
| 11173 | FrameInspector frame_inspector(frame, inlined_jsframe_index, isolate); |
| 11174 | return MaterializeLocalScopeWithFrameInspector(isolate, |
| 11175 | frame, |
| 11176 | &frame_inspector); |
| 11177 | } |
| 11178 | |
| 11179 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11180 | // Create a plain JSObject which materializes the closure content for the |
| 11181 | // context. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11182 | static Handle<JSObject> MaterializeClosure(Isolate* isolate, |
| 11183 | Handle<Context> context) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11184 | ASSERT(context->IsFunctionContext()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11185 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 11186 | Handle<SharedFunctionInfo> shared(context->closure()->shared()); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11187 | Handle<ScopeInfo> scope_info(shared->scope_info()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11188 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 11189 | // Allocate and initialize a JSObject with all the content of this function |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11190 | // closure. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11191 | Handle<JSObject> closure_scope = |
| 11192 | isolate->factory()->NewJSObject(isolate->object_function()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11193 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11194 | // Fill all context locals to the context extension. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11195 | if (!CopyContextLocalsToScopeObject( |
| 11196 | isolate, scope_info, context, closure_scope)) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 11197 | return Handle<JSObject>(); |
| 11198 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11199 | |
| 11200 | // Finally copy any properties from the function context extension. This will |
| 11201 | // be variables introduced by eval. |
| 11202 | if (context->has_extension()) { |
| 11203 | Handle<JSObject> ext(JSObject::cast(context->extension())); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11204 | bool threw = false; |
| 11205 | Handle<FixedArray> keys = |
| 11206 | GetKeysInFixedArrayFor(ext, INCLUDE_PROTOS, &threw); |
| 11207 | if (threw) return Handle<JSObject>(); |
| 11208 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11209 | for (int i = 0; i < keys->length(); i++) { |
| 11210 | // Names of variables introduced by eval are strings. |
| 11211 | ASSERT(keys->get(i)->IsString()); |
| 11212 | Handle<String> key(String::cast(keys->get(i))); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11213 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 11214 | isolate, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 11215 | SetProperty(closure_scope, |
| 11216 | key, |
| 11217 | GetProperty(ext, key), |
| 11218 | NONE, |
| 11219 | kNonStrictMode), |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 11220 | Handle<JSObject>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11221 | } |
| 11222 | } |
| 11223 | |
| 11224 | return closure_scope; |
| 11225 | } |
| 11226 | |
| 11227 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11228 | // Create a plain JSObject which materializes the scope for the specified |
| 11229 | // catch context. |
| 11230 | static Handle<JSObject> MaterializeCatchScope(Isolate* isolate, |
| 11231 | Handle<Context> context) { |
| 11232 | ASSERT(context->IsCatchContext()); |
| 11233 | Handle<String> name(String::cast(context->extension())); |
| 11234 | Handle<Object> thrown_object(context->get(Context::THROWN_OBJECT_INDEX)); |
| 11235 | Handle<JSObject> catch_scope = |
| 11236 | isolate->factory()->NewJSObject(isolate->object_function()); |
| 11237 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 11238 | isolate, |
| 11239 | SetProperty(catch_scope, name, thrown_object, NONE, kNonStrictMode), |
| 11240 | Handle<JSObject>()); |
| 11241 | return catch_scope; |
| 11242 | } |
| 11243 | |
| 11244 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11245 | // Create a plain JSObject which materializes the block scope for the specified |
| 11246 | // block context. |
| 11247 | static Handle<JSObject> MaterializeBlockScope( |
| 11248 | Isolate* isolate, |
| 11249 | Handle<Context> context) { |
| 11250 | ASSERT(context->IsBlockContext()); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11251 | Handle<ScopeInfo> scope_info(ScopeInfo::cast(context->extension())); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11252 | |
| 11253 | // Allocate and initialize a JSObject with all the arguments, stack locals |
| 11254 | // heap locals and extension properties of the debugged function. |
| 11255 | Handle<JSObject> block_scope = |
| 11256 | isolate->factory()->NewJSObject(isolate->object_function()); |
| 11257 | |
| 11258 | // Fill all context locals. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11259 | if (!CopyContextLocalsToScopeObject( |
| 11260 | isolate, scope_info, context, block_scope)) { |
| 11261 | return Handle<JSObject>(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11262 | } |
| 11263 | |
| 11264 | return block_scope; |
| 11265 | } |
| 11266 | |
| 11267 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11268 | // Iterate over the actual scopes visible from a stack frame. The iteration |
| 11269 | // proceeds from the innermost visible nested scope outwards. All scopes are |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11270 | // backed by an actual context except the local scope, which is inserted |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11271 | // "artificially" in the context chain. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11272 | class ScopeIterator { |
| 11273 | public: |
| 11274 | enum ScopeType { |
| 11275 | ScopeTypeGlobal = 0, |
| 11276 | ScopeTypeLocal, |
| 11277 | ScopeTypeWith, |
| 11278 | ScopeTypeClosure, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11279 | ScopeTypeCatch, |
| 11280 | ScopeTypeBlock |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11281 | }; |
| 11282 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11283 | ScopeIterator(Isolate* isolate, |
| 11284 | JavaScriptFrame* frame, |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 11285 | int inlined_jsframe_index) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11286 | : isolate_(isolate), |
| 11287 | frame_(frame), |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 11288 | inlined_jsframe_index_(inlined_jsframe_index), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11289 | function_(JSFunction::cast(frame->function())), |
| 11290 | context_(Context::cast(frame->context())), |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11291 | nested_scope_chain_(4) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11292 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11293 | // Catch the case when the debugger stops in an internal function. |
| 11294 | Handle<SharedFunctionInfo> shared_info(function_->shared()); |
| 11295 | Handle<ScopeInfo> scope_info(shared_info->scope_info()); |
| 11296 | if (shared_info->script() == isolate->heap()->undefined_value()) { |
| 11297 | while (context_->closure() == *function_) { |
| 11298 | context_ = Handle<Context>(context_->previous(), isolate_); |
| 11299 | } |
| 11300 | return; |
| 11301 | } |
| 11302 | |
| 11303 | // Get the debug info (create it if it does not exist). |
| 11304 | if (!isolate->debug()->EnsureDebugInfo(shared_info)) { |
| 11305 | // Return if ensuring debug info failed. |
| 11306 | return; |
| 11307 | } |
| 11308 | Handle<DebugInfo> debug_info = Debug::GetDebugInfo(shared_info); |
| 11309 | |
| 11310 | // Find the break point where execution has stopped. |
| 11311 | BreakLocationIterator break_location_iterator(debug_info, |
| 11312 | ALL_BREAK_LOCATIONS); |
| 11313 | break_location_iterator.FindBreakLocationFromAddress(frame->pc()); |
| 11314 | if (break_location_iterator.IsExit()) { |
| 11315 | // We are within the return sequence. At the momemt it is not possible to |
| 11316 | // get a source position which is consistent with the current scope chain. |
| 11317 | // Thus all nested with, catch and block contexts are skipped and we only |
| 11318 | // provide the function scope. |
| 11319 | if (scope_info->HasContext()) { |
| 11320 | context_ = Handle<Context>(context_->declaration_context(), isolate_); |
| 11321 | } else { |
| 11322 | while (context_->closure() == *function_) { |
| 11323 | context_ = Handle<Context>(context_->previous(), isolate_); |
| 11324 | } |
| 11325 | } |
| 11326 | if (scope_info->Type() != EVAL_SCOPE) nested_scope_chain_.Add(scope_info); |
| 11327 | } else { |
| 11328 | // Reparse the code and analyze the scopes. |
| 11329 | ZoneScope zone_scope(isolate, DELETE_ON_EXIT); |
| 11330 | Handle<Script> script(Script::cast(shared_info->script())); |
| 11331 | Scope* scope = NULL; |
| 11332 | |
| 11333 | // Check whether we are in global, eval or function code. |
| 11334 | Handle<ScopeInfo> scope_info(shared_info->scope_info()); |
| 11335 | if (scope_info->Type() != FUNCTION_SCOPE) { |
| 11336 | // Global or eval code. |
| 11337 | CompilationInfo info(script); |
| 11338 | if (scope_info->Type() == GLOBAL_SCOPE) { |
| 11339 | info.MarkAsGlobal(); |
| 11340 | } else { |
| 11341 | ASSERT(scope_info->Type() == EVAL_SCOPE); |
| 11342 | info.MarkAsEval(); |
| 11343 | info.SetCallingContext(Handle<Context>(function_->context())); |
| 11344 | } |
| 11345 | if (ParserApi::Parse(&info, kNoParsingFlags) && Scope::Analyze(&info)) { |
| 11346 | scope = info.function()->scope(); |
| 11347 | } |
| 11348 | } else { |
| 11349 | // Function code |
| 11350 | CompilationInfo info(shared_info); |
| 11351 | if (ParserApi::Parse(&info, kNoParsingFlags) && Scope::Analyze(&info)) { |
| 11352 | scope = info.function()->scope(); |
| 11353 | } |
| 11354 | } |
| 11355 | |
| 11356 | // Retrieve the scope chain for the current position. |
| 11357 | if (scope != NULL) { |
| 11358 | int source_position = shared_info->code()->SourcePosition(frame_->pc()); |
| 11359 | scope->GetNestedScopeChain(&nested_scope_chain_, source_position); |
| 11360 | } else { |
| 11361 | // A failed reparse indicates that the preparser has diverged from the |
| 11362 | // parser or that the preparse data given to the initial parse has been |
| 11363 | // faulty. We fail in debug mode but in release mode we only provide the |
| 11364 | // information we get from the context chain but nothing about |
| 11365 | // completely stack allocated scopes or stack allocated locals. |
| 11366 | UNREACHABLE(); |
| 11367 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11368 | } |
| 11369 | } |
| 11370 | |
| 11371 | // More scopes? |
| 11372 | bool Done() { return context_.is_null(); } |
| 11373 | |
| 11374 | // Move to the next scope. |
| 11375 | void Next() { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11376 | ScopeType scope_type = Type(); |
| 11377 | if (scope_type == ScopeTypeGlobal) { |
| 11378 | // The global scope is always the last in the chain. |
| 11379 | ASSERT(context_->IsGlobalContext()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11380 | context_ = Handle<Context>(); |
| 11381 | return; |
| 11382 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11383 | if (nested_scope_chain_.is_empty()) { |
| 11384 | context_ = Handle<Context>(context_->previous(), isolate_); |
| 11385 | } else { |
| 11386 | if (nested_scope_chain_.last()->HasContext()) { |
| 11387 | ASSERT(context_->previous() != NULL); |
| 11388 | context_ = Handle<Context>(context_->previous(), isolate_); |
| 11389 | } |
| 11390 | nested_scope_chain_.RemoveLast(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11391 | } |
| 11392 | } |
| 11393 | |
| 11394 | // Return the type of the current scope. |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 11395 | ScopeType Type() { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11396 | if (!nested_scope_chain_.is_empty()) { |
| 11397 | Handle<ScopeInfo> scope_info = nested_scope_chain_.last(); |
| 11398 | switch (scope_info->Type()) { |
| 11399 | case FUNCTION_SCOPE: |
| 11400 | ASSERT(context_->IsFunctionContext() || |
| 11401 | !scope_info->HasContext()); |
| 11402 | return ScopeTypeLocal; |
| 11403 | case GLOBAL_SCOPE: |
| 11404 | ASSERT(context_->IsGlobalContext()); |
| 11405 | return ScopeTypeGlobal; |
| 11406 | case WITH_SCOPE: |
| 11407 | ASSERT(context_->IsWithContext()); |
| 11408 | return ScopeTypeWith; |
| 11409 | case CATCH_SCOPE: |
| 11410 | ASSERT(context_->IsCatchContext()); |
| 11411 | return ScopeTypeCatch; |
| 11412 | case BLOCK_SCOPE: |
| 11413 | ASSERT(!scope_info->HasContext() || |
| 11414 | context_->IsBlockContext()); |
| 11415 | return ScopeTypeBlock; |
| 11416 | case EVAL_SCOPE: |
| 11417 | UNREACHABLE(); |
| 11418 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11419 | } |
| 11420 | if (context_->IsGlobalContext()) { |
| 11421 | ASSERT(context_->global()->IsGlobalObject()); |
| 11422 | return ScopeTypeGlobal; |
| 11423 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11424 | if (context_->IsFunctionContext()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11425 | return ScopeTypeClosure; |
| 11426 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11427 | if (context_->IsCatchContext()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11428 | return ScopeTypeCatch; |
| 11429 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11430 | if (context_->IsBlockContext()) { |
| 11431 | return ScopeTypeBlock; |
| 11432 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11433 | ASSERT(context_->IsWithContext()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11434 | return ScopeTypeWith; |
| 11435 | } |
| 11436 | |
| 11437 | // Return the JavaScript object with the content of the current scope. |
| 11438 | Handle<JSObject> ScopeObject() { |
| 11439 | switch (Type()) { |
| 11440 | case ScopeIterator::ScopeTypeGlobal: |
| 11441 | return Handle<JSObject>(CurrentContext()->global()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11442 | case ScopeIterator::ScopeTypeLocal: |
| 11443 | // Materialize the content of the local scope into a JSObject. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11444 | ASSERT(nested_scope_chain_.length() == 1); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 11445 | return MaterializeLocalScope(isolate_, frame_, inlined_jsframe_index_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11446 | case ScopeIterator::ScopeTypeWith: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11447 | // Return the with object. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11448 | return Handle<JSObject>(JSObject::cast(CurrentContext()->extension())); |
| 11449 | case ScopeIterator::ScopeTypeCatch: |
| 11450 | return MaterializeCatchScope(isolate_, CurrentContext()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11451 | case ScopeIterator::ScopeTypeClosure: |
| 11452 | // Materialize the content of the closure scope into a JSObject. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11453 | return MaterializeClosure(isolate_, CurrentContext()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 11454 | case ScopeIterator::ScopeTypeBlock: |
| 11455 | return MaterializeBlockScope(isolate_, CurrentContext()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11456 | } |
| 11457 | UNREACHABLE(); |
| 11458 | return Handle<JSObject>(); |
| 11459 | } |
| 11460 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11461 | Handle<ScopeInfo> CurrentScopeInfo() { |
| 11462 | if (!nested_scope_chain_.is_empty()) { |
| 11463 | return nested_scope_chain_.last(); |
| 11464 | } else if (context_->IsBlockContext()) { |
| 11465 | return Handle<ScopeInfo>(ScopeInfo::cast(context_->extension())); |
| 11466 | } else if (context_->IsFunctionContext()) { |
| 11467 | return Handle<ScopeInfo>(context_->closure()->shared()->scope_info()); |
| 11468 | } |
| 11469 | return Handle<ScopeInfo>::null(); |
| 11470 | } |
| 11471 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11472 | // Return the context for this scope. For the local context there might not |
| 11473 | // be an actual context. |
| 11474 | Handle<Context> CurrentContext() { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11475 | if (Type() == ScopeTypeGlobal || |
| 11476 | nested_scope_chain_.is_empty()) { |
| 11477 | return context_; |
| 11478 | } else if (nested_scope_chain_.last()->HasContext()) { |
| 11479 | return context_; |
| 11480 | } else { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11481 | return Handle<Context>(); |
| 11482 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11483 | } |
| 11484 | |
| 11485 | #ifdef DEBUG |
| 11486 | // Debug print of the content of the current scope. |
| 11487 | void DebugPrint() { |
| 11488 | switch (Type()) { |
| 11489 | case ScopeIterator::ScopeTypeGlobal: |
| 11490 | PrintF("Global:\n"); |
| 11491 | CurrentContext()->Print(); |
| 11492 | break; |
| 11493 | |
| 11494 | case ScopeIterator::ScopeTypeLocal: { |
| 11495 | PrintF("Local:\n"); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11496 | function_->shared()->scope_info()->Print(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11497 | if (!CurrentContext().is_null()) { |
| 11498 | CurrentContext()->Print(); |
| 11499 | if (CurrentContext()->has_extension()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11500 | Handle<Object> extension(CurrentContext()->extension()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11501 | if (extension->IsJSContextExtensionObject()) { |
| 11502 | extension->Print(); |
| 11503 | } |
| 11504 | } |
| 11505 | } |
| 11506 | break; |
| 11507 | } |
| 11508 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11509 | case ScopeIterator::ScopeTypeWith: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11510 | PrintF("With:\n"); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11511 | CurrentContext()->extension()->Print(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11512 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11513 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11514 | case ScopeIterator::ScopeTypeCatch: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11515 | PrintF("Catch:\n"); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11516 | CurrentContext()->extension()->Print(); |
| 11517 | CurrentContext()->get(Context::THROWN_OBJECT_INDEX)->Print(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11518 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11519 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11520 | case ScopeIterator::ScopeTypeClosure: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11521 | PrintF("Closure:\n"); |
| 11522 | CurrentContext()->Print(); |
| 11523 | if (CurrentContext()->has_extension()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11524 | Handle<Object> extension(CurrentContext()->extension()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11525 | if (extension->IsJSContextExtensionObject()) { |
| 11526 | extension->Print(); |
| 11527 | } |
| 11528 | } |
| 11529 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11530 | |
| 11531 | default: |
| 11532 | UNREACHABLE(); |
| 11533 | } |
| 11534 | PrintF("\n"); |
| 11535 | } |
| 11536 | #endif |
| 11537 | |
| 11538 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11539 | Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11540 | JavaScriptFrame* frame_; |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 11541 | int inlined_jsframe_index_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11542 | Handle<JSFunction> function_; |
| 11543 | Handle<Context> context_; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11544 | List<Handle<ScopeInfo> > nested_scope_chain_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11545 | |
| 11546 | DISALLOW_IMPLICIT_CONSTRUCTORS(ScopeIterator); |
| 11547 | }; |
| 11548 | |
| 11549 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11550 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetScopeCount) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11551 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11552 | ASSERT(args.length() == 2); |
| 11553 | |
| 11554 | // Check arguments. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11555 | Object* check; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11556 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 11557 | RUNTIME_ARGUMENTS(isolate, args)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11558 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 11559 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11560 | CONVERT_CHECKED(Smi, wrapped_id, args[1]); |
| 11561 | |
| 11562 | // Get the frame where the debugging is performed. |
| 11563 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11564 | JavaScriptFrameIterator it(isolate, id); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11565 | JavaScriptFrame* frame = it.frame(); |
| 11566 | |
| 11567 | // Count the visible scopes. |
| 11568 | int n = 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11569 | for (ScopeIterator it(isolate, frame, 0); |
| 11570 | !it.Done(); |
| 11571 | it.Next()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11572 | n++; |
| 11573 | } |
| 11574 | |
| 11575 | return Smi::FromInt(n); |
| 11576 | } |
| 11577 | |
| 11578 | |
| 11579 | static const int kScopeDetailsTypeIndex = 0; |
| 11580 | static const int kScopeDetailsObjectIndex = 1; |
| 11581 | static const int kScopeDetailsSize = 2; |
| 11582 | |
| 11583 | // Return an array with scope details |
| 11584 | // args[0]: number: break id |
| 11585 | // args[1]: number: frame index |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11586 | // args[2]: number: inlined frame index |
| 11587 | // args[3]: number: scope index |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11588 | // |
| 11589 | // The array returned contains the following information: |
| 11590 | // 0: Scope type |
| 11591 | // 1: Scope object |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11592 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetScopeDetails) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11593 | HandleScope scope(isolate); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11594 | ASSERT(args.length() == 4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11595 | |
| 11596 | // Check arguments. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11597 | Object* check; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11598 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 11599 | RUNTIME_ARGUMENTS(isolate, args)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11600 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 11601 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11602 | CONVERT_CHECKED(Smi, wrapped_id, args[1]); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 11603 | CONVERT_NUMBER_CHECKED(int, inlined_jsframe_index, Int32, args[2]); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11604 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[3]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11605 | |
| 11606 | // Get the frame where the debugging is performed. |
| 11607 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11608 | JavaScriptFrameIterator frame_it(isolate, id); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11609 | JavaScriptFrame* frame = frame_it.frame(); |
| 11610 | |
| 11611 | // Find the requested scope. |
| 11612 | int n = 0; |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 11613 | ScopeIterator it(isolate, frame, inlined_jsframe_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11614 | for (; !it.Done() && n < index; it.Next()) { |
| 11615 | n++; |
| 11616 | } |
| 11617 | if (it.Done()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11618 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11619 | } |
| 11620 | |
| 11621 | // Calculate the size of the result. |
| 11622 | int details_size = kScopeDetailsSize; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11623 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(details_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11624 | |
| 11625 | // Fill in scope details. |
| 11626 | details->set(kScopeDetailsTypeIndex, Smi::FromInt(it.Type())); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 11627 | Handle<JSObject> scope_object = it.ScopeObject(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11628 | RETURN_IF_EMPTY_HANDLE(isolate, scope_object); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 11629 | details->set(kScopeDetailsObjectIndex, *scope_object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11630 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11631 | return *isolate->factory()->NewJSArrayWithElements(details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11632 | } |
| 11633 | |
| 11634 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11635 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPrintScopes) { |
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() == 0); |
| 11638 | |
| 11639 | #ifdef DEBUG |
| 11640 | // Print the scopes for the top frame. |
| 11641 | StackFrameLocator locator; |
| 11642 | JavaScriptFrame* frame = locator.FindJavaScriptFrame(0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 11643 | for (ScopeIterator it(isolate, frame, 0); |
| 11644 | !it.Done(); |
| 11645 | it.Next()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11646 | it.DebugPrint(); |
| 11647 | } |
| 11648 | #endif |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11649 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11650 | } |
| 11651 | |
| 11652 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11653 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetThreadCount) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11654 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11655 | ASSERT(args.length() == 1); |
| 11656 | |
| 11657 | // Check arguments. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11658 | Object* result; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11659 | { MaybeObject* maybe_result = Runtime_CheckExecutionState( |
| 11660 | RUNTIME_ARGUMENTS(isolate, args)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11661 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 11662 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11663 | |
| 11664 | // Count all archived V8 threads. |
| 11665 | int n = 0; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11666 | for (ThreadState* thread = |
| 11667 | isolate->thread_manager()->FirstThreadStateInUse(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11668 | thread != NULL; |
| 11669 | thread = thread->Next()) { |
| 11670 | n++; |
| 11671 | } |
| 11672 | |
| 11673 | // Total number of threads is current thread and archived threads. |
| 11674 | return Smi::FromInt(n + 1); |
| 11675 | } |
| 11676 | |
| 11677 | |
| 11678 | static const int kThreadDetailsCurrentThreadIndex = 0; |
| 11679 | static const int kThreadDetailsThreadIdIndex = 1; |
| 11680 | static const int kThreadDetailsSize = 2; |
| 11681 | |
| 11682 | // Return an array with thread details |
| 11683 | // args[0]: number: break id |
| 11684 | // args[1]: number: thread index |
| 11685 | // |
| 11686 | // The array returned contains the following information: |
| 11687 | // 0: Is current thread? |
| 11688 | // 1: Thread id |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11689 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetThreadDetails) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11690 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11691 | ASSERT(args.length() == 2); |
| 11692 | |
| 11693 | // Check arguments. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11694 | Object* check; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11695 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 11696 | RUNTIME_ARGUMENTS(isolate, args)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11697 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 11698 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11699 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[1]); |
| 11700 | |
| 11701 | // Allocate array for result. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11702 | Handle<FixedArray> details = |
| 11703 | isolate->factory()->NewFixedArray(kThreadDetailsSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11704 | |
| 11705 | // Thread index 0 is current thread. |
| 11706 | if (index == 0) { |
| 11707 | // Fill the details. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11708 | details->set(kThreadDetailsCurrentThreadIndex, |
| 11709 | isolate->heap()->true_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11710 | details->set(kThreadDetailsThreadIdIndex, |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11711 | Smi::FromInt(ThreadId::Current().ToInteger())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11712 | } else { |
| 11713 | // Find the thread with the requested index. |
| 11714 | int n = 1; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11715 | ThreadState* thread = |
| 11716 | isolate->thread_manager()->FirstThreadStateInUse(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11717 | while (index != n && thread != NULL) { |
| 11718 | thread = thread->Next(); |
| 11719 | n++; |
| 11720 | } |
| 11721 | if (thread == NULL) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11722 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11723 | } |
| 11724 | |
| 11725 | // Fill the details. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11726 | details->set(kThreadDetailsCurrentThreadIndex, |
| 11727 | isolate->heap()->false_value()); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11728 | details->set(kThreadDetailsThreadIdIndex, |
| 11729 | Smi::FromInt(thread->id().ToInteger())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11730 | } |
| 11731 | |
| 11732 | // Convert to JS array and return. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11733 | return *isolate->factory()->NewJSArrayWithElements(details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11734 | } |
| 11735 | |
| 11736 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 11737 | // Sets the disable break state |
| 11738 | // args[0]: disable break state |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11739 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetDisableBreak) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11740 | HandleScope scope(isolate); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 11741 | ASSERT(args.length() == 1); |
| 11742 | CONVERT_BOOLEAN_CHECKED(disable_break, args[0]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11743 | isolate->debug()->set_disable_break(disable_break); |
| 11744 | return isolate->heap()->undefined_value(); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 11745 | } |
| 11746 | |
| 11747 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11748 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetBreakLocations) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11749 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11750 | ASSERT(args.length() == 1); |
| 11751 | |
| 11752 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
| 11753 | Handle<SharedFunctionInfo> shared(fun->shared()); |
| 11754 | // Find the number of break points |
| 11755 | Handle<Object> break_locations = Debug::GetSourceBreakLocations(shared); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11756 | if (break_locations->IsUndefined()) return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11757 | // Return array as JS array |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11758 | return *isolate->factory()->NewJSArrayWithElements( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11759 | Handle<FixedArray>::cast(break_locations)); |
| 11760 | } |
| 11761 | |
| 11762 | |
| 11763 | // Set a break point in a function |
| 11764 | // args[0]: function |
| 11765 | // args[1]: number: break source position (within the function source) |
| 11766 | // args[2]: number: break point object |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11767 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetFunctionBreakPoint) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11768 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11769 | ASSERT(args.length() == 3); |
| 11770 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
| 11771 | Handle<SharedFunctionInfo> shared(fun->shared()); |
| 11772 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 11773 | RUNTIME_ASSERT(source_position >= 0); |
| 11774 | Handle<Object> break_point_object_arg = args.at<Object>(2); |
| 11775 | |
| 11776 | // Set break point. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11777 | isolate->debug()->SetBreakPoint(shared, break_point_object_arg, |
| 11778 | &source_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11779 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 11780 | return Smi::FromInt(source_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11781 | } |
| 11782 | |
| 11783 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11784 | Object* Runtime::FindSharedFunctionInfoInScript(Isolate* isolate, |
| 11785 | Handle<Script> script, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11786 | int position) { |
| 11787 | // Iterate the heap looking for SharedFunctionInfo generated from the |
| 11788 | // script. The inner most SharedFunctionInfo containing the source position |
| 11789 | // for the requested break point is found. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 11790 | // NOTE: This might require several heap iterations. If the SharedFunctionInfo |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11791 | // which is found is not compiled it is compiled and the heap is iterated |
| 11792 | // again as the compilation might create inner functions from the newly |
| 11793 | // compiled function and the actual requested break point might be in one of |
| 11794 | // these functions. |
| 11795 | bool done = false; |
| 11796 | // The current candidate for the source position: |
| 11797 | int target_start_position = RelocInfo::kNoPosition; |
| 11798 | Handle<SharedFunctionInfo> target; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11799 | while (!done) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11800 | { // Extra scope for iterator and no-allocation. |
| 11801 | isolate->heap()->EnsureHeapIsIterable(); |
| 11802 | AssertNoAllocation no_alloc_during_heap_iteration; |
| 11803 | HeapIterator iterator; |
| 11804 | for (HeapObject* obj = iterator.next(); |
| 11805 | obj != NULL; obj = iterator.next()) { |
| 11806 | if (obj->IsSharedFunctionInfo()) { |
| 11807 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(obj)); |
| 11808 | if (shared->script() == *script) { |
| 11809 | // If the SharedFunctionInfo found has the requested script data and |
| 11810 | // contains the source position it is a candidate. |
| 11811 | int start_position = shared->function_token_position(); |
| 11812 | if (start_position == RelocInfo::kNoPosition) { |
| 11813 | start_position = shared->start_position(); |
| 11814 | } |
| 11815 | if (start_position <= position && |
| 11816 | position <= shared->end_position()) { |
| 11817 | // If there is no candidate or this function is within the current |
| 11818 | // candidate this is the new candidate. |
| 11819 | if (target.is_null()) { |
| 11820 | target_start_position = start_position; |
| 11821 | target = shared; |
| 11822 | } else { |
| 11823 | if (target_start_position == start_position && |
| 11824 | shared->end_position() == target->end_position()) { |
| 11825 | // If a top-level function contain only one function |
| 11826 | // declartion the source for the top-level and the |
| 11827 | // function is the same. In that case prefer the non |
| 11828 | // top-level function. |
| 11829 | if (!shared->is_toplevel()) { |
| 11830 | target_start_position = start_position; |
| 11831 | target = shared; |
| 11832 | } |
| 11833 | } else if (target_start_position <= start_position && |
| 11834 | shared->end_position() <= target->end_position()) { |
| 11835 | // This containment check includes equality as a function |
| 11836 | // inside a top-level function can share either start or end |
| 11837 | // position with the top-level function. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11838 | target_start_position = start_position; |
| 11839 | target = shared; |
| 11840 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11841 | } |
| 11842 | } |
| 11843 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11844 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11845 | } // End for loop. |
| 11846 | } // End No allocation scope. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11847 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11848 | if (target.is_null()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11849 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11850 | } |
| 11851 | |
| 11852 | // If the candidate found is compiled we are done. NOTE: when lazy |
| 11853 | // compilation of inner functions is introduced some additional checking |
| 11854 | // needs to be done here to compile inner functions. |
| 11855 | done = target->is_compiled(); |
| 11856 | if (!done) { |
| 11857 | // If the candidate is not compiled compile it to reveal any inner |
| 11858 | // functions which might contain the requested source position. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11859 | SharedFunctionInfo::CompileLazy(target, KEEP_EXCEPTION); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11860 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 11861 | } // End while loop. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11862 | |
| 11863 | return *target; |
| 11864 | } |
| 11865 | |
| 11866 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 11867 | // Changes the state of a break point in a script and returns source position |
| 11868 | // where break point was set. NOTE: Regarding performance see the NOTE for |
| 11869 | // GetScriptFromScriptData. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11870 | // args[0]: script to set break point in |
| 11871 | // args[1]: number: break source position (within the script source) |
| 11872 | // args[2]: number: break point object |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11873 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetScriptBreakPoint) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11874 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11875 | ASSERT(args.length() == 3); |
| 11876 | CONVERT_ARG_CHECKED(JSValue, wrapper, 0); |
| 11877 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 11878 | RUNTIME_ASSERT(source_position >= 0); |
| 11879 | Handle<Object> break_point_object_arg = args.at<Object>(2); |
| 11880 | |
| 11881 | // Get the script from the script wrapper. |
| 11882 | RUNTIME_ASSERT(wrapper->value()->IsScript()); |
| 11883 | Handle<Script> script(Script::cast(wrapper->value())); |
| 11884 | |
| 11885 | Object* result = Runtime::FindSharedFunctionInfoInScript( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11886 | isolate, script, source_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11887 | if (!result->IsUndefined()) { |
| 11888 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result)); |
| 11889 | // Find position within function. The script position might be before the |
| 11890 | // source position of the first function. |
| 11891 | int position; |
| 11892 | if (shared->start_position() > source_position) { |
| 11893 | position = 0; |
| 11894 | } else { |
| 11895 | position = source_position - shared->start_position(); |
| 11896 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11897 | isolate->debug()->SetBreakPoint(shared, break_point_object_arg, &position); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 11898 | position += shared->start_position(); |
| 11899 | return Smi::FromInt(position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11900 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11901 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11902 | } |
| 11903 | |
| 11904 | |
| 11905 | // Clear a break point |
| 11906 | // args[0]: number: break point object |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11907 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ClearBreakPoint) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11908 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11909 | ASSERT(args.length() == 1); |
| 11910 | Handle<Object> break_point_object_arg = args.at<Object>(0); |
| 11911 | |
| 11912 | // Clear break point. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11913 | isolate->debug()->ClearBreakPoint(break_point_object_arg); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11914 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11915 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11916 | } |
| 11917 | |
| 11918 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 11919 | // Change the state of break on exceptions. |
| 11920 | // args[0]: Enum value indicating whether to affect caught/uncaught exceptions. |
| 11921 | // args[1]: Boolean indicating on/off. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11922 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ChangeBreakOnException) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11923 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11924 | ASSERT(args.length() == 2); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 11925 | RUNTIME_ASSERT(args[0]->IsNumber()); |
| 11926 | CONVERT_BOOLEAN_CHECKED(enable, args[1]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11927 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 11928 | // If the number doesn't match an enum value, the ChangeBreakOnException |
| 11929 | // function will default to affecting caught exceptions. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11930 | ExceptionBreakType type = |
| 11931 | static_cast<ExceptionBreakType>(NumberToUint32(args[0])); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 11932 | // Update break point state. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11933 | isolate->debug()->ChangeBreakOnException(type, enable); |
| 11934 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11935 | } |
| 11936 | |
| 11937 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 11938 | // Returns the state of break on exceptions |
| 11939 | // args[0]: boolean indicating uncaught exceptions |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11940 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsBreakOnException) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11941 | HandleScope scope(isolate); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 11942 | ASSERT(args.length() == 1); |
| 11943 | RUNTIME_ASSERT(args[0]->IsNumber()); |
| 11944 | |
| 11945 | ExceptionBreakType type = |
| 11946 | static_cast<ExceptionBreakType>(NumberToUint32(args[0])); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11947 | bool result = isolate->debug()->IsBreakOnException(type); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 11948 | return Smi::FromInt(result); |
| 11949 | } |
| 11950 | |
| 11951 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11952 | // Prepare for stepping |
| 11953 | // args[0]: break id for checking execution state |
| 11954 | // args[1]: step action from the enumeration StepAction |
| 11955 | // args[2]: number of times to perform the step, for step out it is the number |
| 11956 | // of frames to step down. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11957 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PrepareStep) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11958 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11959 | ASSERT(args.length() == 3); |
| 11960 | // Check arguments. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11961 | Object* check; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11962 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 11963 | RUNTIME_ARGUMENTS(isolate, args)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 11964 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 11965 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11966 | if (!args[1]->IsNumber() || !args[2]->IsNumber()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11967 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11968 | } |
| 11969 | |
| 11970 | // Get the step action and check validity. |
| 11971 | StepAction step_action = static_cast<StepAction>(NumberToInt32(args[1])); |
| 11972 | if (step_action != StepIn && |
| 11973 | step_action != StepNext && |
| 11974 | step_action != StepOut && |
| 11975 | step_action != StepInMin && |
| 11976 | step_action != StepMin) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11977 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11978 | } |
| 11979 | |
| 11980 | // Get the number of steps. |
| 11981 | int step_count = NumberToInt32(args[2]); |
| 11982 | if (step_count < 1) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11983 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11984 | } |
| 11985 | |
| 11986 | // Clear all current stepping setup. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11987 | isolate->debug()->ClearStepping(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11988 | |
| 11989 | // Prepare step. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11990 | isolate->debug()->PrepareStep(static_cast<StepAction>(step_action), |
| 11991 | step_count); |
| 11992 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11993 | } |
| 11994 | |
| 11995 | |
| 11996 | // Clear all stepping set by PrepareStep. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 11997 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ClearStepping) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 11998 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11999 | ASSERT(args.length() == 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12000 | isolate->debug()->ClearStepping(); |
| 12001 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12002 | } |
| 12003 | |
| 12004 | |
| 12005 | // Creates a copy of the with context chain. The copy of the context chain is |
| 12006 | // is linked to the function context supplied. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12007 | static Handle<Context> CopyNestedScopeContextChain(Isolate* isolate, |
| 12008 | Handle<JSFunction> function, |
| 12009 | Handle<Context> base, |
| 12010 | JavaScriptFrame* frame, |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 12011 | int inlined_jsframe_index) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12012 | HandleScope scope(isolate); |
| 12013 | List<Handle<ScopeInfo> > scope_chain; |
| 12014 | List<Handle<Context> > context_chain; |
| 12015 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 12016 | ScopeIterator it(isolate, frame, inlined_jsframe_index); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12017 | for (; it.Type() != ScopeIterator::ScopeTypeGlobal && |
| 12018 | it.Type() != ScopeIterator::ScopeTypeLocal ; it.Next()) { |
| 12019 | ASSERT(!it.Done()); |
| 12020 | scope_chain.Add(it.CurrentScopeInfo()); |
| 12021 | context_chain.Add(it.CurrentContext()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12022 | } |
| 12023 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12024 | // At the end of the chain. Return the base context to link to. |
| 12025 | Handle<Context> context = base; |
| 12026 | |
| 12027 | // Iteratively copy and or materialize the nested contexts. |
| 12028 | while (!scope_chain.is_empty()) { |
| 12029 | Handle<ScopeInfo> scope_info = scope_chain.RemoveLast(); |
| 12030 | Handle<Context> current = context_chain.RemoveLast(); |
| 12031 | ASSERT(!(scope_info->HasContext() & current.is_null())); |
| 12032 | |
| 12033 | if (scope_info->Type() == CATCH_SCOPE) { |
| 12034 | Handle<String> name(String::cast(current->extension())); |
| 12035 | Handle<Object> thrown_object(current->get(Context::THROWN_OBJECT_INDEX)); |
| 12036 | context = |
| 12037 | isolate->factory()->NewCatchContext(function, |
| 12038 | context, |
| 12039 | name, |
| 12040 | thrown_object); |
| 12041 | } else if (scope_info->Type() == BLOCK_SCOPE) { |
| 12042 | // Materialize the contents of the block scope into a JSObject. |
| 12043 | Handle<JSObject> block_scope_object = |
| 12044 | MaterializeBlockScope(isolate, current); |
| 12045 | if (block_scope_object.is_null()) { |
| 12046 | return Handle<Context>::null(); |
| 12047 | } |
| 12048 | // Allocate a new function context for the debug evaluation and set the |
| 12049 | // extension object. |
| 12050 | Handle<Context> new_context = |
| 12051 | isolate->factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, |
| 12052 | function); |
| 12053 | new_context->set_extension(*block_scope_object); |
| 12054 | new_context->set_previous(*context); |
| 12055 | context = new_context; |
| 12056 | } else { |
| 12057 | ASSERT(scope_info->Type() == WITH_SCOPE); |
| 12058 | ASSERT(current->IsWithContext()); |
| 12059 | Handle<JSObject> extension(JSObject::cast(current->extension())); |
| 12060 | context = |
| 12061 | isolate->factory()->NewWithContext(function, context, extension); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 12062 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12063 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12064 | |
| 12065 | return scope.CloseAndEscape(context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12066 | } |
| 12067 | |
| 12068 | |
| 12069 | // Helper function to find or create the arguments object for |
| 12070 | // Runtime_DebugEvaluate. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12071 | static Handle<Object> GetArgumentsObject(Isolate* isolate, |
| 12072 | JavaScriptFrame* frame, |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 12073 | FrameInspector* frame_inspector, |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12074 | Handle<ScopeInfo> scope_info, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12075 | Handle<Context> function_context) { |
| 12076 | // Try to find the value of 'arguments' to pass as parameter. If it is not |
| 12077 | // found (that is the debugged function does not reference 'arguments' and |
| 12078 | // does not support eval) then create an 'arguments' object. |
| 12079 | int index; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12080 | if (scope_info->StackLocalCount() > 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12081 | index = scope_info->StackSlotIndex(isolate->heap()->arguments_symbol()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12082 | if (index != -1) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12083 | return Handle<Object>(frame->GetExpression(index), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12084 | } |
| 12085 | } |
| 12086 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12087 | if (scope_info->HasHeapAllocatedLocals()) { |
| 12088 | VariableMode mode; |
| 12089 | InitializationFlag init_flag; |
| 12090 | index = scope_info->ContextSlotIndex( |
| 12091 | isolate->heap()->arguments_symbol(), &mode, &init_flag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12092 | if (index != -1) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12093 | return Handle<Object>(function_context->get(index), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12094 | } |
| 12095 | } |
| 12096 | |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 12097 | Handle<JSFunction> function(JSFunction::cast(frame_inspector->GetFunction())); |
| 12098 | int length = frame_inspector->GetParametersCount(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12099 | Handle<JSObject> arguments = |
| 12100 | isolate->factory()->NewArgumentsObject(function, length); |
| 12101 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(length); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 12102 | |
| 12103 | AssertNoAllocation no_gc; |
| 12104 | WriteBarrierMode mode = array->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12105 | for (int i = 0; i < length; i++) { |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 12106 | array->set(i, frame_inspector->GetParameter(i), mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12107 | } |
| 12108 | arguments->set_elements(*array); |
| 12109 | return arguments; |
| 12110 | } |
| 12111 | |
| 12112 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12113 | static const char kSourceStr[] = |
| 12114 | "(function(arguments,__source__){return eval(__source__);})"; |
| 12115 | |
| 12116 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12117 | // Evaluate a piece of JavaScript in the context of a stack frame for |
| 12118 | // debugging. This is accomplished by creating a new context which in its |
| 12119 | // extension part has all the parameters and locals of the function on the |
| 12120 | // stack frame. A function which calls eval with the code to evaluate is then |
| 12121 | // compiled in this context and called in this context. As this context |
| 12122 | // replaces the context of the function on the stack frame a new (empty) |
| 12123 | // function is created as well to be used as the closure for the context. |
| 12124 | // This function and the context acts as replacements for the function on the |
| 12125 | // stack frame presenting the same view of the values of parameters and |
| 12126 | // local variables as if the piece of JavaScript was evaluated at the point |
| 12127 | // where the function on the stack frame is currently stopped. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12128 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugEvaluate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12129 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12130 | |
| 12131 | // Check the execution state and decode arguments frame and source to be |
| 12132 | // evaluated. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12133 | ASSERT(args.length() == 6); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 12134 | Object* check_result; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12135 | { MaybeObject* maybe_check_result = Runtime_CheckExecutionState( |
| 12136 | RUNTIME_ARGUMENTS(isolate, args)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 12137 | if (!maybe_check_result->ToObject(&check_result)) { |
| 12138 | return maybe_check_result; |
| 12139 | } |
| 12140 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12141 | CONVERT_CHECKED(Smi, wrapped_id, args[1]); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 12142 | CONVERT_NUMBER_CHECKED(int, inlined_jsframe_index, Int32, args[2]); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12143 | CONVERT_ARG_CHECKED(String, source, 3); |
| 12144 | CONVERT_BOOLEAN_CHECKED(disable_break, args[4]); |
| 12145 | Handle<Object> additional_context(args[5]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12146 | |
| 12147 | // Handle the processing of break. |
| 12148 | DisableBreak disable_break_save(disable_break); |
| 12149 | |
| 12150 | // Get the frame where the debugging is performed. |
| 12151 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12152 | JavaScriptFrameIterator it(isolate, id); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12153 | JavaScriptFrame* frame = it.frame(); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 12154 | FrameInspector frame_inspector(frame, inlined_jsframe_index, isolate); |
| 12155 | Handle<JSFunction> function(JSFunction::cast(frame_inspector.GetFunction())); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12156 | Handle<ScopeInfo> scope_info(function->shared()->scope_info()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12157 | |
| 12158 | // Traverse the saved contexts chain to find the active context for the |
| 12159 | // selected frame. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12160 | SaveContext* save = FindSavedContextForFrame(isolate, frame); |
| 12161 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12162 | SaveContext savex(isolate); |
| 12163 | isolate->set_context(*(save->context())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12164 | |
| 12165 | // Create the (empty) function replacing the function on the stack frame for |
| 12166 | // the purpose of evaluating in the context created below. It is important |
| 12167 | // that this function does not describe any parameters and local variables |
| 12168 | // in the context. If it does then this will cause problems with the lookup |
| 12169 | // in Context::Lookup, where context slots for parameters and local variables |
| 12170 | // are looked at before the extension object. |
| 12171 | Handle<JSFunction> go_between = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12172 | isolate->factory()->NewFunction(isolate->factory()->empty_string(), |
| 12173 | isolate->factory()->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12174 | go_between->set_context(function->context()); |
| 12175 | #ifdef DEBUG |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12176 | Handle<ScopeInfo> go_between_scope_info(go_between->shared()->scope_info()); |
| 12177 | ASSERT(go_between_scope_info->ParameterCount() == 0); |
| 12178 | ASSERT(go_between_scope_info->ContextLocalCount() == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12179 | #endif |
| 12180 | |
| 12181 | // Materialize the content of the local scope into a JSObject. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 12182 | Handle<JSObject> local_scope = MaterializeLocalScopeWithFrameInspector( |
| 12183 | isolate, frame, &frame_inspector); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12184 | RETURN_IF_EMPTY_HANDLE(isolate, local_scope); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12185 | |
| 12186 | // Allocate a new context for the debug evaluation and set the extension |
| 12187 | // object build. |
| 12188 | Handle<Context> context = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12189 | isolate->factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, |
| 12190 | go_between); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12191 | context->set_extension(*local_scope); |
| 12192 | // Copy any with contexts present and chain them in front of this context. |
| 12193 | Handle<Context> frame_context(Context::cast(frame->context())); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 12194 | Handle<Context> function_context; |
| 12195 | // Get the function's context if it has one. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12196 | if (scope_info->HasContext()) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 12197 | function_context = Handle<Context>(frame_context->declaration_context()); |
| 12198 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12199 | context = CopyNestedScopeContextChain(isolate, |
| 12200 | go_between, |
| 12201 | context, |
| 12202 | frame, |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 12203 | inlined_jsframe_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12204 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12205 | if (additional_context->IsJSObject()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12206 | Handle<JSObject> extension = Handle<JSObject>::cast(additional_context); |
| 12207 | context = |
| 12208 | isolate->factory()->NewWithContext(go_between, context, extension); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12209 | } |
| 12210 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12211 | // Wrap the evaluation statement in a new function compiled in the newly |
| 12212 | // created context. The function has one parameter which has to be called |
| 12213 | // 'arguments'. This it to have access to what would have been 'arguments' in |
| 12214 | // the function being debugged. |
| 12215 | // function(arguments,__source__) {return eval(__source__);} |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12216 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12217 | Handle<String> function_source = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12218 | isolate->factory()->NewStringFromAscii( |
| 12219 | Vector<const char>(kSourceStr, sizeof(kSourceStr) - 1)); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 12220 | |
| 12221 | // Currently, the eval code will be executed in non-strict mode, |
| 12222 | // even in the strict code context. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12223 | Handle<SharedFunctionInfo> shared = |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12224 | Compiler::CompileEval(function_source, |
| 12225 | context, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 12226 | context->IsGlobalContext(), |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12227 | CLASSIC_MODE, |
| 12228 | RelocInfo::kNoPosition); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12229 | if (shared.is_null()) return Failure::Exception(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12230 | Handle<JSFunction> compiled_function = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12231 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12232 | |
| 12233 | // Invoke the result of the compilation to get the evaluation function. |
| 12234 | bool has_pending_exception; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12235 | Handle<Object> receiver(frame->receiver(), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12236 | Handle<Object> evaluation_function = |
| 12237 | Execution::Call(compiled_function, receiver, 0, NULL, |
| 12238 | &has_pending_exception); |
| 12239 | if (has_pending_exception) return Failure::Exception(); |
| 12240 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12241 | Handle<Object> arguments = GetArgumentsObject(isolate, |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12242 | frame, |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 12243 | &frame_inspector, |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12244 | scope_info, |
| 12245 | function_context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12246 | |
| 12247 | // Invoke the evaluation function and return the result. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12248 | Handle<Object> argv[] = { arguments, source }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12249 | Handle<Object> result = |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12250 | Execution::Call(Handle<JSFunction>::cast(evaluation_function), |
| 12251 | receiver, |
| 12252 | ARRAY_SIZE(argv), |
| 12253 | argv, |
| 12254 | &has_pending_exception); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12255 | if (has_pending_exception) return Failure::Exception(); |
| 12256 | |
| 12257 | // Skip the global proxy as it has no properties and always delegates to the |
| 12258 | // real global object. |
| 12259 | if (result->IsJSGlobalProxy()) { |
| 12260 | result = Handle<JSObject>(JSObject::cast(result->GetPrototype())); |
| 12261 | } |
| 12262 | |
| 12263 | return *result; |
| 12264 | } |
| 12265 | |
| 12266 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12267 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugEvaluateGlobal) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12268 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12269 | |
| 12270 | // Check the execution state and decode arguments frame and source to be |
| 12271 | // evaluated. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12272 | ASSERT(args.length() == 4); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 12273 | Object* check_result; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12274 | { MaybeObject* maybe_check_result = Runtime_CheckExecutionState( |
| 12275 | RUNTIME_ARGUMENTS(isolate, args)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 12276 | if (!maybe_check_result->ToObject(&check_result)) { |
| 12277 | return maybe_check_result; |
| 12278 | } |
| 12279 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12280 | CONVERT_ARG_CHECKED(String, source, 1); |
| 12281 | CONVERT_BOOLEAN_CHECKED(disable_break, args[2]); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12282 | Handle<Object> additional_context(args[3]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12283 | |
| 12284 | // Handle the processing of break. |
| 12285 | DisableBreak disable_break_save(disable_break); |
| 12286 | |
| 12287 | // Enter the top context from before the debugger was invoked. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12288 | SaveContext save(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12289 | SaveContext* top = &save; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12290 | while (top != NULL && *top->context() == *isolate->debug()->debug_context()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12291 | top = top->prev(); |
| 12292 | } |
| 12293 | if (top != NULL) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12294 | isolate->set_context(*top->context()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12295 | } |
| 12296 | |
| 12297 | // Get the global context now set to the top context from before the |
| 12298 | // debugger was invoked. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12299 | Handle<Context> context = isolate->global_context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12300 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12301 | bool is_global = true; |
| 12302 | |
| 12303 | if (additional_context->IsJSObject()) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12304 | // Create a new with context with the additional context information between |
| 12305 | // the context of the debugged function and the eval code to be executed. |
| 12306 | context = isolate->factory()->NewWithContext( |
| 12307 | Handle<JSFunction>(context->closure()), |
| 12308 | context, |
| 12309 | Handle<JSObject>::cast(additional_context)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12310 | is_global = false; |
| 12311 | } |
| 12312 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12313 | // Compile the source to be evaluated. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 12314 | // Currently, the eval code will be executed in non-strict mode, |
| 12315 | // even in the strict code context. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12316 | Handle<SharedFunctionInfo> shared = |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12317 | Compiler::CompileEval(source, |
| 12318 | context, |
| 12319 | is_global, |
| 12320 | CLASSIC_MODE, |
| 12321 | RelocInfo::kNoPosition); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12322 | if (shared.is_null()) return Failure::Exception(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12323 | Handle<JSFunction> compiled_function = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12324 | Handle<JSFunction>( |
| 12325 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 12326 | context)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12327 | |
| 12328 | // Invoke the result of the compilation to get the evaluation function. |
| 12329 | bool has_pending_exception; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12330 | Handle<Object> receiver = isolate->global(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12331 | Handle<Object> result = |
| 12332 | Execution::Call(compiled_function, receiver, 0, NULL, |
| 12333 | &has_pending_exception); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12334 | // Clear the oneshot breakpoints so that the debugger does not step further. |
| 12335 | isolate->debug()->ClearStepping(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12336 | if (has_pending_exception) return Failure::Exception(); |
| 12337 | return *result; |
| 12338 | } |
| 12339 | |
| 12340 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12341 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetLoadedScripts) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12342 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12343 | ASSERT(args.length() == 0); |
| 12344 | |
| 12345 | // Fill the script objects. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12346 | Handle<FixedArray> instances = isolate->debug()->GetLoadedScripts(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12347 | |
| 12348 | // Convert the script objects to proper JS objects. |
| 12349 | for (int i = 0; i < instances->length(); i++) { |
| 12350 | Handle<Script> script = Handle<Script>(Script::cast(instances->get(i))); |
| 12351 | // Get the script wrapper in a local handle before calling GetScriptWrapper, |
| 12352 | // because using |
| 12353 | // instances->set(i, *GetScriptWrapper(script)) |
| 12354 | // is unsafe as GetScriptWrapper might call GC and the C++ compiler might |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame^] | 12355 | // already have dereferenced the instances handle. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12356 | Handle<JSValue> wrapper = GetScriptWrapper(script); |
| 12357 | instances->set(i, *wrapper); |
| 12358 | } |
| 12359 | |
| 12360 | // Return result as a JS array. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12361 | Handle<JSObject> result = |
| 12362 | isolate->factory()->NewJSObject(isolate->array_function()); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12363 | isolate->factory()->SetContent(Handle<JSArray>::cast(result), instances); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12364 | return *result; |
| 12365 | } |
| 12366 | |
| 12367 | |
| 12368 | // Helper function used by Runtime_DebugReferencedBy below. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12369 | static int DebugReferencedBy(HeapIterator* iterator, |
| 12370 | JSObject* target, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12371 | Object* instance_filter, int max_references, |
| 12372 | FixedArray* instances, int instances_size, |
| 12373 | JSFunction* arguments_function) { |
| 12374 | NoHandleAllocation ha; |
| 12375 | AssertNoAllocation no_alloc; |
| 12376 | |
| 12377 | // Iterate the heap. |
| 12378 | int count = 0; |
| 12379 | JSObject* last = NULL; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 12380 | HeapObject* heap_obj = NULL; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12381 | while (((heap_obj = iterator->next()) != NULL) && |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12382 | (max_references == 0 || count < max_references)) { |
| 12383 | // Only look at all JSObjects. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12384 | if (heap_obj->IsJSObject()) { |
| 12385 | // Skip context extension objects and argument arrays as these are |
| 12386 | // checked in the context of functions using them. |
| 12387 | JSObject* obj = JSObject::cast(heap_obj); |
| 12388 | if (obj->IsJSContextExtensionObject() || |
| 12389 | obj->map()->constructor() == arguments_function) { |
| 12390 | continue; |
| 12391 | } |
| 12392 | |
| 12393 | // Check if the JS object has a reference to the object looked for. |
| 12394 | if (obj->ReferencesObject(target)) { |
| 12395 | // Check instance filter if supplied. This is normally used to avoid |
| 12396 | // references from mirror objects (see Runtime_IsInPrototypeChain). |
| 12397 | if (!instance_filter->IsUndefined()) { |
| 12398 | Object* V = obj; |
| 12399 | while (true) { |
| 12400 | Object* prototype = V->GetPrototype(); |
| 12401 | if (prototype->IsNull()) { |
| 12402 | break; |
| 12403 | } |
| 12404 | if (instance_filter == prototype) { |
| 12405 | obj = NULL; // Don't add this object. |
| 12406 | break; |
| 12407 | } |
| 12408 | V = prototype; |
| 12409 | } |
| 12410 | } |
| 12411 | |
| 12412 | if (obj != NULL) { |
| 12413 | // Valid reference found add to instance array if supplied an update |
| 12414 | // count. |
| 12415 | if (instances != NULL && count < instances_size) { |
| 12416 | instances->set(count, obj); |
| 12417 | } |
| 12418 | last = obj; |
| 12419 | count++; |
| 12420 | } |
| 12421 | } |
| 12422 | } |
| 12423 | } |
| 12424 | |
| 12425 | // Check for circular reference only. This can happen when the object is only |
| 12426 | // referenced from mirrors and has a circular reference in which case the |
| 12427 | // object is not really alive and would have been garbage collected if not |
| 12428 | // referenced from the mirror. |
| 12429 | if (count == 1 && last == target) { |
| 12430 | count = 0; |
| 12431 | } |
| 12432 | |
| 12433 | // Return the number of referencing objects found. |
| 12434 | return count; |
| 12435 | } |
| 12436 | |
| 12437 | |
| 12438 | // Scan the heap for objects with direct references to an object |
| 12439 | // args[0]: the object to find references to |
| 12440 | // args[1]: constructor function for instances to exclude (Mirror) |
| 12441 | // args[2]: the the maximum number of objects to return |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12442 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugReferencedBy) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12443 | ASSERT(args.length() == 3); |
| 12444 | |
| 12445 | // First perform a full GC in order to avoid references from dead objects. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12446 | isolate->heap()->CollectAllGarbage(Heap::kMakeHeapIterableMask); |
| 12447 | // The heap iterator reserves the right to do a GC to make the heap iterable. |
| 12448 | // Due to the GC above we know it won't need to do that, but it seems cleaner |
| 12449 | // to get the heap iterator constructed before we start having unprotected |
| 12450 | // Object* locals that are not protected by handles. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12451 | |
| 12452 | // Check parameters. |
| 12453 | CONVERT_CHECKED(JSObject, target, args[0]); |
| 12454 | Object* instance_filter = args[1]; |
| 12455 | RUNTIME_ASSERT(instance_filter->IsUndefined() || |
| 12456 | instance_filter->IsJSObject()); |
| 12457 | CONVERT_NUMBER_CHECKED(int32_t, max_references, Int32, args[2]); |
| 12458 | RUNTIME_ASSERT(max_references >= 0); |
| 12459 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12460 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12461 | // Get the constructor function for context extension and arguments array. |
| 12462 | JSObject* arguments_boilerplate = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12463 | isolate->context()->global_context()->arguments_boilerplate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12464 | JSFunction* arguments_function = |
| 12465 | JSFunction::cast(arguments_boilerplate->map()->constructor()); |
| 12466 | |
| 12467 | // Get the number of referencing objects. |
| 12468 | int count; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12469 | HeapIterator heap_iterator; |
| 12470 | count = DebugReferencedBy(&heap_iterator, |
| 12471 | target, instance_filter, max_references, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12472 | NULL, 0, arguments_function); |
| 12473 | |
| 12474 | // Allocate an array to hold the result. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 12475 | Object* object; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12476 | { MaybeObject* maybe_object = isolate->heap()->AllocateFixedArray(count); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 12477 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 12478 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12479 | FixedArray* instances = FixedArray::cast(object); |
| 12480 | |
| 12481 | // Fill the referencing objects. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12482 | // AllocateFixedArray above does not make the heap non-iterable. |
| 12483 | ASSERT(HEAP->IsHeapIterable()); |
| 12484 | HeapIterator heap_iterator2; |
| 12485 | count = DebugReferencedBy(&heap_iterator2, |
| 12486 | target, instance_filter, max_references, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12487 | instances, count, arguments_function); |
| 12488 | |
| 12489 | // Return result as JS array. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 12490 | Object* result; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12491 | MaybeObject* maybe_result = isolate->heap()->AllocateJSObject( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12492 | isolate->context()->global_context()->array_function()); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12493 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 12494 | return JSArray::cast(result)->SetContent(instances); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12495 | } |
| 12496 | |
| 12497 | |
| 12498 | // Helper function used by Runtime_DebugConstructedBy below. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12499 | static int DebugConstructedBy(HeapIterator* iterator, |
| 12500 | JSFunction* constructor, |
| 12501 | int max_references, |
| 12502 | FixedArray* instances, |
| 12503 | int instances_size) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12504 | AssertNoAllocation no_alloc; |
| 12505 | |
| 12506 | // Iterate the heap. |
| 12507 | int count = 0; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 12508 | HeapObject* heap_obj = NULL; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12509 | while (((heap_obj = iterator->next()) != NULL) && |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12510 | (max_references == 0 || count < max_references)) { |
| 12511 | // Only look at all JSObjects. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12512 | if (heap_obj->IsJSObject()) { |
| 12513 | JSObject* obj = JSObject::cast(heap_obj); |
| 12514 | if (obj->map()->constructor() == constructor) { |
| 12515 | // Valid reference found add to instance array if supplied an update |
| 12516 | // count. |
| 12517 | if (instances != NULL && count < instances_size) { |
| 12518 | instances->set(count, obj); |
| 12519 | } |
| 12520 | count++; |
| 12521 | } |
| 12522 | } |
| 12523 | } |
| 12524 | |
| 12525 | // Return the number of referencing objects found. |
| 12526 | return count; |
| 12527 | } |
| 12528 | |
| 12529 | |
| 12530 | // Scan the heap for objects constructed by a specific function. |
| 12531 | // args[0]: the constructor to find instances of |
| 12532 | // args[1]: the the maximum number of objects to return |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12533 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugConstructedBy) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12534 | ASSERT(args.length() == 2); |
| 12535 | |
| 12536 | // First perform a full GC in order to avoid dead objects. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12537 | isolate->heap()->CollectAllGarbage(Heap::kMakeHeapIterableMask); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12538 | |
| 12539 | // Check parameters. |
| 12540 | CONVERT_CHECKED(JSFunction, constructor, args[0]); |
| 12541 | CONVERT_NUMBER_CHECKED(int32_t, max_references, Int32, args[1]); |
| 12542 | RUNTIME_ASSERT(max_references >= 0); |
| 12543 | |
| 12544 | // Get the number of referencing objects. |
| 12545 | int count; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12546 | HeapIterator heap_iterator; |
| 12547 | count = DebugConstructedBy(&heap_iterator, |
| 12548 | constructor, |
| 12549 | max_references, |
| 12550 | NULL, |
| 12551 | 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12552 | |
| 12553 | // Allocate an array to hold the result. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 12554 | Object* object; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12555 | { MaybeObject* maybe_object = isolate->heap()->AllocateFixedArray(count); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 12556 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 12557 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12558 | FixedArray* instances = FixedArray::cast(object); |
| 12559 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12560 | ASSERT(HEAP->IsHeapIterable()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12561 | // Fill the referencing objects. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12562 | HeapIterator heap_iterator2; |
| 12563 | count = DebugConstructedBy(&heap_iterator2, |
| 12564 | constructor, |
| 12565 | max_references, |
| 12566 | instances, |
| 12567 | count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12568 | |
| 12569 | // Return result as JS array. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 12570 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12571 | { MaybeObject* maybe_result = isolate->heap()->AllocateJSObject( |
| 12572 | isolate->context()->global_context()->array_function()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 12573 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 12574 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12575 | return JSArray::cast(result)->SetContent(instances); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12576 | } |
| 12577 | |
| 12578 | |
| 12579 | // Find the effective prototype object as returned by __proto__. |
| 12580 | // args[0]: the object to find the prototype for. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12581 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetPrototype) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12582 | ASSERT(args.length() == 1); |
| 12583 | |
| 12584 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 12585 | |
| 12586 | // Use the __proto__ accessor. |
| 12587 | return Accessors::ObjectPrototype.getter(obj, NULL); |
| 12588 | } |
| 12589 | |
| 12590 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12591 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SystemBreak) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12592 | ASSERT(args.length() == 0); |
| 12593 | CPU::DebugBreak(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12594 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12595 | } |
| 12596 | |
| 12597 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12598 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugDisassembleFunction) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12599 | #ifdef DEBUG |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12600 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12601 | ASSERT(args.length() == 1); |
| 12602 | // Get the function and make sure it is compiled. |
| 12603 | CONVERT_ARG_CHECKED(JSFunction, func, 0); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 12604 | Handle<SharedFunctionInfo> shared(func->shared()); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12605 | if (!SharedFunctionInfo::EnsureCompiled(shared, KEEP_EXCEPTION)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12606 | return Failure::Exception(); |
| 12607 | } |
| 12608 | func->code()->PrintLn(); |
| 12609 | #endif // DEBUG |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12610 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12611 | } |
| 12612 | |
| 12613 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12614 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugDisassembleConstructor) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12615 | #ifdef DEBUG |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12616 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12617 | ASSERT(args.length() == 1); |
| 12618 | // Get the function and make sure it is compiled. |
| 12619 | CONVERT_ARG_CHECKED(JSFunction, func, 0); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 12620 | Handle<SharedFunctionInfo> shared(func->shared()); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12621 | if (!SharedFunctionInfo::EnsureCompiled(shared, KEEP_EXCEPTION)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12622 | return Failure::Exception(); |
| 12623 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 12624 | shared->construct_stub()->PrintLn(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12625 | #endif // DEBUG |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12626 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12627 | } |
| 12628 | |
| 12629 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12630 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetInferredName) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12631 | NoHandleAllocation ha; |
| 12632 | ASSERT(args.length() == 1); |
| 12633 | |
| 12634 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 12635 | return f->shared()->inferred_name(); |
| 12636 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 12637 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12638 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12639 | static int FindSharedFunctionInfosForScript(HeapIterator* iterator, |
| 12640 | Script* script, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12641 | FixedArray* buffer) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12642 | AssertNoAllocation no_allocations; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12643 | int counter = 0; |
| 12644 | int buffer_size = buffer->length(); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12645 | for (HeapObject* obj = iterator->next(); |
| 12646 | obj != NULL; |
| 12647 | obj = iterator->next()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12648 | ASSERT(obj != NULL); |
| 12649 | if (!obj->IsSharedFunctionInfo()) { |
| 12650 | continue; |
| 12651 | } |
| 12652 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(obj); |
| 12653 | if (shared->script() != script) { |
| 12654 | continue; |
| 12655 | } |
| 12656 | if (counter < buffer_size) { |
| 12657 | buffer->set(counter, shared); |
| 12658 | } |
| 12659 | counter++; |
| 12660 | } |
| 12661 | return counter; |
| 12662 | } |
| 12663 | |
| 12664 | // For a script finds all SharedFunctionInfo's in the heap that points |
| 12665 | // to this script. Returns JSArray of SharedFunctionInfo wrapped |
| 12666 | // in OpaqueReferences. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12667 | RUNTIME_FUNCTION(MaybeObject*, |
| 12668 | Runtime_LiveEditFindSharedFunctionInfosForScript) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12669 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12670 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12671 | CONVERT_CHECKED(JSValue, script_value, args[0]); |
| 12672 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12673 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12674 | Handle<Script> script = Handle<Script>(Script::cast(script_value->value())); |
| 12675 | |
| 12676 | const int kBufferSize = 32; |
| 12677 | |
| 12678 | Handle<FixedArray> array; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12679 | array = isolate->factory()->NewFixedArray(kBufferSize); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12680 | int number; |
| 12681 | { |
| 12682 | isolate->heap()->EnsureHeapIsIterable(); |
| 12683 | AssertNoAllocation no_allocations; |
| 12684 | HeapIterator heap_iterator; |
| 12685 | Script* scr = *script; |
| 12686 | FixedArray* arr = *array; |
| 12687 | number = FindSharedFunctionInfosForScript(&heap_iterator, scr, arr); |
| 12688 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12689 | if (number > kBufferSize) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12690 | array = isolate->factory()->NewFixedArray(number); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 12691 | isolate->heap()->EnsureHeapIsIterable(); |
| 12692 | AssertNoAllocation no_allocations; |
| 12693 | HeapIterator heap_iterator; |
| 12694 | Script* scr = *script; |
| 12695 | FixedArray* arr = *array; |
| 12696 | FindSharedFunctionInfosForScript(&heap_iterator, scr, arr); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12697 | } |
| 12698 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12699 | Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(array); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12700 | result->set_length(Smi::FromInt(number)); |
| 12701 | |
| 12702 | LiveEdit::WrapSharedFunctionInfos(result); |
| 12703 | |
| 12704 | return *result; |
| 12705 | } |
| 12706 | |
| 12707 | // For a script calculates compilation information about all its functions. |
| 12708 | // The script source is explicitly specified by the second argument. |
| 12709 | // The source of the actual script is not used, however it is important that |
| 12710 | // all generated code keeps references to this particular instance of script. |
| 12711 | // Returns a JSArray of compilation infos. The array is ordered so that |
| 12712 | // each function with all its descendant is always stored in a continues range |
| 12713 | // 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] | 12714 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditGatherCompileInfo) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12715 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12716 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12717 | CONVERT_CHECKED(JSValue, script, args[0]); |
| 12718 | CONVERT_ARG_CHECKED(String, source, 1); |
| 12719 | Handle<Script> script_handle = Handle<Script>(Script::cast(script->value())); |
| 12720 | |
| 12721 | JSArray* result = LiveEdit::GatherCompileInfo(script_handle, source); |
| 12722 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12723 | if (isolate->has_pending_exception()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12724 | return Failure::Exception(); |
| 12725 | } |
| 12726 | |
| 12727 | return result; |
| 12728 | } |
| 12729 | |
| 12730 | // Changes the source of the script to a new_source. |
| 12731 | // If old_script_name is provided (i.e. is a String), also creates a copy of |
| 12732 | // the script with its original source and sends notification to debugger. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12733 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditReplaceScript) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12734 | ASSERT(args.length() == 3); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12735 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12736 | CONVERT_CHECKED(JSValue, original_script_value, args[0]); |
| 12737 | CONVERT_ARG_CHECKED(String, new_source, 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12738 | Handle<Object> old_script_name(args[2], isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12739 | |
| 12740 | CONVERT_CHECKED(Script, original_script_pointer, |
| 12741 | original_script_value->value()); |
| 12742 | Handle<Script> original_script(original_script_pointer); |
| 12743 | |
| 12744 | Object* old_script = LiveEdit::ChangeScriptSource(original_script, |
| 12745 | new_source, |
| 12746 | old_script_name); |
| 12747 | |
| 12748 | if (old_script->IsScript()) { |
| 12749 | Handle<Script> script_handle(Script::cast(old_script)); |
| 12750 | return *(GetScriptWrapper(script_handle)); |
| 12751 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12752 | return isolate->heap()->null_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12753 | } |
| 12754 | } |
| 12755 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12756 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12757 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditFunctionSourceUpdated) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12758 | ASSERT(args.length() == 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12759 | HandleScope scope(isolate); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12760 | CONVERT_ARG_CHECKED(JSArray, shared_info, 0); |
| 12761 | return LiveEdit::FunctionSourceUpdated(shared_info); |
| 12762 | } |
| 12763 | |
| 12764 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12765 | // Replaces code of SharedFunctionInfo with a new one. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12766 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditReplaceFunctionCode) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12767 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12768 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12769 | CONVERT_ARG_CHECKED(JSArray, new_compile_info, 0); |
| 12770 | CONVERT_ARG_CHECKED(JSArray, shared_info, 1); |
| 12771 | |
| 12772 | return LiveEdit::ReplaceFunctionCode(new_compile_info, shared_info); |
| 12773 | } |
| 12774 | |
| 12775 | // Connects SharedFunctionInfo to another script. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12776 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditFunctionSetScript) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12777 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12778 | HandleScope scope(isolate); |
| 12779 | Handle<Object> function_object(args[0], isolate); |
| 12780 | Handle<Object> script_object(args[1], isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12781 | |
| 12782 | if (function_object->IsJSValue()) { |
| 12783 | Handle<JSValue> function_wrapper = Handle<JSValue>::cast(function_object); |
| 12784 | if (script_object->IsJSValue()) { |
| 12785 | CONVERT_CHECKED(Script, script, JSValue::cast(*script_object)->value()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12786 | script_object = Handle<Object>(script, isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12787 | } |
| 12788 | |
| 12789 | LiveEdit::SetFunctionScript(function_wrapper, script_object); |
| 12790 | } else { |
| 12791 | // Just ignore this. We may not have a SharedFunctionInfo for some functions |
| 12792 | // and we check it in this function. |
| 12793 | } |
| 12794 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12795 | return isolate->heap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12796 | } |
| 12797 | |
| 12798 | |
| 12799 | // In a code of a parent function replaces original function as embedded object |
| 12800 | // with a substitution one. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12801 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditReplaceRefToNestedFunction) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12802 | ASSERT(args.length() == 3); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12803 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12804 | |
| 12805 | CONVERT_ARG_CHECKED(JSValue, parent_wrapper, 0); |
| 12806 | CONVERT_ARG_CHECKED(JSValue, orig_wrapper, 1); |
| 12807 | CONVERT_ARG_CHECKED(JSValue, subst_wrapper, 2); |
| 12808 | |
| 12809 | LiveEdit::ReplaceRefToNestedFunction(parent_wrapper, orig_wrapper, |
| 12810 | subst_wrapper); |
| 12811 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12812 | return isolate->heap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12813 | } |
| 12814 | |
| 12815 | |
| 12816 | // Updates positions of a shared function info (first parameter) according |
| 12817 | // to script source change. Text change is described in second parameter as |
| 12818 | // array of groups of 3 numbers: |
| 12819 | // (change_begin, change_end, change_end_new_position). |
| 12820 | // 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] | 12821 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditPatchFunctionPositions) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12822 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12823 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12824 | CONVERT_ARG_CHECKED(JSArray, shared_array, 0); |
| 12825 | CONVERT_ARG_CHECKED(JSArray, position_change_array, 1); |
| 12826 | |
| 12827 | return LiveEdit::PatchFunctionPositions(shared_array, position_change_array); |
| 12828 | } |
| 12829 | |
| 12830 | |
| 12831 | // For array of SharedFunctionInfo's (each wrapped in JSValue) |
| 12832 | // checks that none of them have activations on stacks (of any thread). |
| 12833 | // Returns array of the same length with corresponding results of |
| 12834 | // LiveEdit::FunctionPatchabilityStatus type. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12835 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditCheckAndDropActivations) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12836 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12837 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12838 | CONVERT_ARG_CHECKED(JSArray, shared_array, 0); |
| 12839 | CONVERT_BOOLEAN_CHECKED(do_drop, args[1]); |
| 12840 | |
| 12841 | return *LiveEdit::CheckAndDropActivations(shared_array, do_drop); |
| 12842 | } |
| 12843 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 12844 | // Compares 2 strings line-by-line, then token-wise and returns diff in form |
| 12845 | // of JSArray of triplets (pos1, pos1_end, pos2_end) describing list |
| 12846 | // of diff chunks. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12847 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditCompareStrings) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12848 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12849 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12850 | CONVERT_ARG_CHECKED(String, s1, 0); |
| 12851 | CONVERT_ARG_CHECKED(String, s2, 1); |
| 12852 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 12853 | return *LiveEdit::CompareStrings(s1, s2); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12854 | } |
| 12855 | |
| 12856 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12857 | // A testing entry. Returns statement position which is the closest to |
| 12858 | // source_position. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12859 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFunctionCodePositionFromSource) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12860 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12861 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12862 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 12863 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 12864 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12865 | Handle<Code> code(function->code(), isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12866 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12867 | if (code->kind() != Code::FUNCTION && |
| 12868 | code->kind() != Code::OPTIMIZED_FUNCTION) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12869 | return isolate->heap()->undefined_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 12870 | } |
| 12871 | |
| 12872 | RelocIterator it(*code, RelocInfo::ModeMask(RelocInfo::STATEMENT_POSITION)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12873 | int closest_pc = 0; |
| 12874 | int distance = kMaxInt; |
| 12875 | while (!it.done()) { |
| 12876 | int statement_position = static_cast<int>(it.rinfo()->data()); |
| 12877 | // Check if this break point is closer that what was previously found. |
| 12878 | if (source_position <= statement_position && |
| 12879 | statement_position - source_position < distance) { |
| 12880 | closest_pc = |
| 12881 | static_cast<int>(it.rinfo()->pc() - code->instruction_start()); |
| 12882 | distance = statement_position - source_position; |
| 12883 | // Check whether we can't get any closer. |
| 12884 | if (distance == 0) break; |
| 12885 | } |
| 12886 | it.next(); |
| 12887 | } |
| 12888 | |
| 12889 | return Smi::FromInt(closest_pc); |
| 12890 | } |
| 12891 | |
| 12892 | |
| 12893 | // Calls specified function with or without entering the debugger. |
| 12894 | // This is used in unit tests to run code as if debugger is entered or simply |
| 12895 | // to have a stack with C++ frame in the middle. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12896 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ExecuteInDebugContext) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12897 | ASSERT(args.length() == 2); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12898 | HandleScope scope(isolate); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12899 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 12900 | CONVERT_BOOLEAN_CHECKED(without_debugger, args[1]); |
| 12901 | |
| 12902 | Handle<Object> result; |
| 12903 | bool pending_exception; |
| 12904 | { |
| 12905 | if (without_debugger) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12906 | result = Execution::Call(function, isolate->global(), 0, NULL, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12907 | &pending_exception); |
| 12908 | } else { |
| 12909 | EnterDebugger enter_debugger; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12910 | result = Execution::Call(function, isolate->global(), 0, NULL, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12911 | &pending_exception); |
| 12912 | } |
| 12913 | } |
| 12914 | if (!pending_exception) { |
| 12915 | return *result; |
| 12916 | } else { |
| 12917 | return Failure::Exception(); |
| 12918 | } |
| 12919 | } |
| 12920 | |
| 12921 | |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 12922 | // Sets a v8 flag. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12923 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetFlags) { |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 12924 | CONVERT_CHECKED(String, arg, args[0]); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 12925 | SmartArrayPointer<char> flags = |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 12926 | arg->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 12927 | FlagList::SetFlagsFromString(*flags, StrLength(*flags)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12928 | return isolate->heap()->undefined_value(); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 12929 | } |
| 12930 | |
| 12931 | |
| 12932 | // Performs a GC. |
| 12933 | // Presently, it only does a full GC. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12934 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CollectGarbage) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12935 | isolate->heap()->CollectAllGarbage(true); |
| 12936 | return isolate->heap()->undefined_value(); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 12937 | } |
| 12938 | |
| 12939 | |
| 12940 | // Gets the current heap usage. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12941 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetHeapUsage) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12942 | int usage = static_cast<int>(isolate->heap()->SizeOfObjects()); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 12943 | if (!Smi::IsValid(usage)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12944 | return *isolate->factory()->NewNumberFromInt(usage); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 12945 | } |
| 12946 | return Smi::FromInt(usage); |
| 12947 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12948 | |
| 12949 | |
| 12950 | // Captures a live object list from the present heap. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12951 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasLOLEnabled) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12952 | #ifdef LIVE_OBJECT_LIST |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12953 | return isolate->heap()->true_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12954 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12955 | return isolate->heap()->false_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12956 | #endif |
| 12957 | } |
| 12958 | |
| 12959 | |
| 12960 | // Captures a live object list from the present heap. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12961 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CaptureLOL) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12962 | #ifdef LIVE_OBJECT_LIST |
| 12963 | return LiveObjectList::Capture(); |
| 12964 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12965 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12966 | #endif |
| 12967 | } |
| 12968 | |
| 12969 | |
| 12970 | // Deletes the specified live object list. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12971 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeleteLOL) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12972 | #ifdef LIVE_OBJECT_LIST |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12973 | CONVERT_SMI_ARG_CHECKED(id, 0); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12974 | bool success = LiveObjectList::Delete(id); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12975 | return isolate->heap()->ToBoolean(success); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12976 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12977 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12978 | #endif |
| 12979 | } |
| 12980 | |
| 12981 | |
| 12982 | // Generates the response to a debugger request for a dump of the objects |
| 12983 | // contained in the difference between the captured live object lists |
| 12984 | // specified by id1 and id2. |
| 12985 | // If id1 is 0 (i.e. not a valid lol), then the whole of lol id2 will be |
| 12986 | // dumped. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 12987 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DumpLOL) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12988 | #ifdef LIVE_OBJECT_LIST |
| 12989 | HandleScope scope; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 12990 | CONVERT_SMI_ARG_CHECKED(id1, 0); |
| 12991 | CONVERT_SMI_ARG_CHECKED(id2, 1); |
| 12992 | CONVERT_SMI_ARG_CHECKED(start, 2); |
| 12993 | CONVERT_SMI_ARG_CHECKED(count, 3); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12994 | CONVERT_ARG_CHECKED(JSObject, filter_obj, 4); |
| 12995 | EnterDebugger enter_debugger; |
| 12996 | return LiveObjectList::Dump(id1, id2, start, count, filter_obj); |
| 12997 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 12998 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 12999 | #endif |
| 13000 | } |
| 13001 | |
| 13002 | |
| 13003 | // Gets the specified object as requested by the debugger. |
| 13004 | // This is only used for obj ids shown in live object lists. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13005 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLObj) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13006 | #ifdef LIVE_OBJECT_LIST |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13007 | CONVERT_SMI_ARG_CHECKED(obj_id, 0); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13008 | Object* result = LiveObjectList::GetObj(obj_id); |
| 13009 | return result; |
| 13010 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13011 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13012 | #endif |
| 13013 | } |
| 13014 | |
| 13015 | |
| 13016 | // Gets the obj id for the specified address if valid. |
| 13017 | // This is only used for obj ids shown in live object lists. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13018 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLObjId) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13019 | #ifdef LIVE_OBJECT_LIST |
| 13020 | HandleScope scope; |
| 13021 | CONVERT_ARG_CHECKED(String, address, 0); |
| 13022 | Object* result = LiveObjectList::GetObjId(address); |
| 13023 | return result; |
| 13024 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13025 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13026 | #endif |
| 13027 | } |
| 13028 | |
| 13029 | |
| 13030 | // Gets the retainers that references the specified object alive. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13031 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLObjRetainers) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13032 | #ifdef LIVE_OBJECT_LIST |
| 13033 | HandleScope scope; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13034 | CONVERT_SMI_ARG_CHECKED(obj_id, 0); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13035 | RUNTIME_ASSERT(args[1]->IsUndefined() || args[1]->IsJSObject()); |
| 13036 | RUNTIME_ASSERT(args[2]->IsUndefined() || args[2]->IsBoolean()); |
| 13037 | RUNTIME_ASSERT(args[3]->IsUndefined() || args[3]->IsSmi()); |
| 13038 | RUNTIME_ASSERT(args[4]->IsUndefined() || args[4]->IsSmi()); |
| 13039 | CONVERT_ARG_CHECKED(JSObject, filter_obj, 5); |
| 13040 | |
| 13041 | Handle<JSObject> instance_filter; |
| 13042 | if (args[1]->IsJSObject()) { |
| 13043 | instance_filter = args.at<JSObject>(1); |
| 13044 | } |
| 13045 | bool verbose = false; |
| 13046 | if (args[2]->IsBoolean()) { |
| 13047 | verbose = args[2]->IsTrue(); |
| 13048 | } |
| 13049 | int start = 0; |
| 13050 | if (args[3]->IsSmi()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13051 | start = args.smi_at(3); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13052 | } |
| 13053 | int limit = Smi::kMaxValue; |
| 13054 | if (args[4]->IsSmi()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13055 | limit = args.smi_at(4); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13056 | } |
| 13057 | |
| 13058 | return LiveObjectList::GetObjRetainers(obj_id, |
| 13059 | instance_filter, |
| 13060 | verbose, |
| 13061 | start, |
| 13062 | limit, |
| 13063 | filter_obj); |
| 13064 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13065 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13066 | #endif |
| 13067 | } |
| 13068 | |
| 13069 | |
| 13070 | // Gets the reference path between 2 objects. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13071 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLPath) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13072 | #ifdef LIVE_OBJECT_LIST |
| 13073 | HandleScope scope; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13074 | CONVERT_SMI_ARG_CHECKED(obj_id1, 0); |
| 13075 | CONVERT_SMI_ARG_CHECKED(obj_id2, 1); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13076 | RUNTIME_ASSERT(args[2]->IsUndefined() || args[2]->IsJSObject()); |
| 13077 | |
| 13078 | Handle<JSObject> instance_filter; |
| 13079 | if (args[2]->IsJSObject()) { |
| 13080 | instance_filter = args.at<JSObject>(2); |
| 13081 | } |
| 13082 | |
| 13083 | Object* result = |
| 13084 | LiveObjectList::GetPath(obj_id1, obj_id2, instance_filter); |
| 13085 | return result; |
| 13086 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13087 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13088 | #endif |
| 13089 | } |
| 13090 | |
| 13091 | |
| 13092 | // Generates the response to a debugger request for a list of all |
| 13093 | // previously captured live object lists. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13094 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InfoLOL) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13095 | #ifdef LIVE_OBJECT_LIST |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13096 | CONVERT_SMI_ARG_CHECKED(start, 0); |
| 13097 | CONVERT_SMI_ARG_CHECKED(count, 1); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13098 | return LiveObjectList::Info(start, count); |
| 13099 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13100 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13101 | #endif |
| 13102 | } |
| 13103 | |
| 13104 | |
| 13105 | // Gets a dump of the specified object as requested by the debugger. |
| 13106 | // This is only used for obj ids shown in live object lists. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13107 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PrintLOLObj) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13108 | #ifdef LIVE_OBJECT_LIST |
| 13109 | HandleScope scope; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13110 | CONVERT_SMI_ARG_CHECKED(obj_id, 0); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13111 | Object* result = LiveObjectList::PrintObj(obj_id); |
| 13112 | return result; |
| 13113 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13114 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13115 | #endif |
| 13116 | } |
| 13117 | |
| 13118 | |
| 13119 | // Resets and releases all previously captured live object lists. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13120 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ResetLOL) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13121 | #ifdef LIVE_OBJECT_LIST |
| 13122 | LiveObjectList::Reset(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13123 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13124 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13125 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13126 | #endif |
| 13127 | } |
| 13128 | |
| 13129 | |
| 13130 | // Generates the response to a debugger request for a summary of the types |
| 13131 | // of objects in the difference between the captured live object lists |
| 13132 | // specified by id1 and id2. |
| 13133 | // If id1 is 0 (i.e. not a valid lol), then the whole of lol id2 will be |
| 13134 | // summarized. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13135 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SummarizeLOL) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13136 | #ifdef LIVE_OBJECT_LIST |
| 13137 | HandleScope scope; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13138 | CONVERT_SMI_ARG_CHECKED(id1, 0); |
| 13139 | CONVERT_SMI_ARG_CHECKED(id2, 1); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13140 | CONVERT_ARG_CHECKED(JSObject, filter_obj, 2); |
| 13141 | |
| 13142 | EnterDebugger enter_debugger; |
| 13143 | return LiveObjectList::Summarize(id1, id2, filter_obj); |
| 13144 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13145 | return isolate->heap()->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13146 | #endif |
| 13147 | } |
| 13148 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13149 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 13150 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 13151 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13152 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ProfilerResume) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 13153 | NoHandleAllocation ha; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13154 | v8::V8::ResumeProfiler(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13155 | return isolate->heap()->undefined_value(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 13156 | } |
| 13157 | |
| 13158 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13159 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ProfilerPause) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 13160 | NoHandleAllocation ha; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13161 | v8::V8::PauseProfiler(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13162 | return isolate->heap()->undefined_value(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 13163 | } |
| 13164 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13165 | |
| 13166 | // Finds the script object from the script data. NOTE: This operation uses |
| 13167 | // heap traversal to find the function generated for the source position |
| 13168 | // for the requested break point. For lazily compiled functions several heap |
| 13169 | // traversals might be required rendering this operation as a rather slow |
| 13170 | // operation. However for setting break points which is normally done through |
| 13171 | // some kind of user interaction the performance is not crucial. |
| 13172 | static Handle<Object> Runtime_GetScriptFromScriptName( |
| 13173 | Handle<String> script_name) { |
| 13174 | // Scan the heap for Script objects to find the script with the requested |
| 13175 | // script data. |
| 13176 | Handle<Script> script; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 13177 | script_name->GetHeap()->EnsureHeapIsIterable(); |
| 13178 | AssertNoAllocation no_allocation_during_heap_iteration; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13179 | HeapIterator iterator; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 13180 | HeapObject* obj = NULL; |
| 13181 | while (script.is_null() && ((obj = iterator.next()) != NULL)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13182 | // If a script is found check if it has the script data requested. |
| 13183 | if (obj->IsScript()) { |
| 13184 | if (Script::cast(obj)->name()->IsString()) { |
| 13185 | if (String::cast(Script::cast(obj)->name())->Equals(*script_name)) { |
| 13186 | script = Handle<Script>(Script::cast(obj)); |
| 13187 | } |
| 13188 | } |
| 13189 | } |
| 13190 | } |
| 13191 | |
| 13192 | // If no script with the requested script data is found return undefined. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13193 | if (script.is_null()) return FACTORY->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13194 | |
| 13195 | // Return the script found. |
| 13196 | return GetScriptWrapper(script); |
| 13197 | } |
| 13198 | |
| 13199 | |
| 13200 | // Get the script object from script data. NOTE: Regarding performance |
| 13201 | // see the NOTE for GetScriptFromScriptData. |
| 13202 | // args[0]: script data for the script to find the source for |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13203 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetScript) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13204 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13205 | |
| 13206 | ASSERT(args.length() == 1); |
| 13207 | |
| 13208 | CONVERT_CHECKED(String, script_name, args[0]); |
| 13209 | |
| 13210 | // Find the requested script. |
| 13211 | Handle<Object> result = |
| 13212 | Runtime_GetScriptFromScriptName(Handle<String>(script_name)); |
| 13213 | return *result; |
| 13214 | } |
| 13215 | |
| 13216 | |
| 13217 | // Determines whether the given stack frame should be displayed in |
| 13218 | // a stack trace. The caller is the error constructor that asked |
| 13219 | // for the stack trace to be collected. The first time a construct |
| 13220 | // call to this function is encountered it is skipped. The seen_caller |
| 13221 | // in/out parameter is used to remember if the caller has been seen |
| 13222 | // yet. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13223 | static bool ShowFrameInStackTrace(StackFrame* raw_frame, |
| 13224 | Object* caller, |
| 13225 | bool* seen_caller) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13226 | // Only display JS frames. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 13227 | if (!raw_frame->is_java_script()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13228 | return false; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 13229 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13230 | JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame); |
| 13231 | Object* raw_fun = frame->function(); |
| 13232 | // Not sure when this can happen but skip it just in case. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 13233 | if (!raw_fun->IsJSFunction()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13234 | return false; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 13235 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13236 | if ((raw_fun == caller) && !(*seen_caller)) { |
| 13237 | *seen_caller = true; |
| 13238 | return false; |
| 13239 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13240 | // Skip all frames until we've seen the caller. |
| 13241 | if (!(*seen_caller)) return false; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 13242 | // Also, skip non-visible built-in functions and any call with the builtins |
| 13243 | // object as receiver, so as to not reveal either the builtins object or |
| 13244 | // an internal function. |
| 13245 | // The --builtins-in-stack-traces command line flag allows including |
| 13246 | // internal call sites in the stack trace for debugging purposes. |
| 13247 | if (!FLAG_builtins_in_stack_traces) { |
| 13248 | JSFunction* fun = JSFunction::cast(raw_fun); |
| 13249 | if (frame->receiver()->IsJSBuiltinsObject() || |
| 13250 | (fun->IsBuiltin() && !fun->shared()->native())) { |
| 13251 | return false; |
| 13252 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13253 | } |
| 13254 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13255 | } |
| 13256 | |
| 13257 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13258 | // Collect the raw data for a stack trace. Returns an array of 4 |
| 13259 | // element segments each containing a receiver, function, code and |
| 13260 | // native code offset. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13261 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CollectStackTrace) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13262 | ASSERT_EQ(args.length(), 2); |
| 13263 | Handle<Object> caller = args.at<Object>(0); |
| 13264 | CONVERT_NUMBER_CHECKED(int32_t, limit, Int32, args[1]); |
| 13265 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13266 | HandleScope scope(isolate); |
| 13267 | Factory* factory = isolate->factory(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13268 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 13269 | limit = Max(limit, 0); // Ensure that limit is not negative. |
| 13270 | int initial_size = Min(limit, 10); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13271 | Handle<FixedArray> elements = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13272 | factory->NewFixedArrayWithHoles(initial_size * 4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13273 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13274 | StackFrameIterator iter(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13275 | // If the caller parameter is a function we skip frames until we're |
| 13276 | // under it before starting to collect. |
| 13277 | bool seen_caller = !caller->IsJSFunction(); |
| 13278 | int cursor = 0; |
| 13279 | int frames_seen = 0; |
| 13280 | while (!iter.done() && frames_seen < limit) { |
| 13281 | StackFrame* raw_frame = iter.frame(); |
| 13282 | if (ShowFrameInStackTrace(raw_frame, *caller, &seen_caller)) { |
| 13283 | frames_seen++; |
| 13284 | JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13285 | // Set initial size to the maximum inlining level + 1 for the outermost |
| 13286 | // function. |
| 13287 | List<FrameSummary> frames(Compiler::kMaxInliningLevels + 1); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13288 | frame->Summarize(&frames); |
| 13289 | for (int i = frames.length() - 1; i >= 0; i--) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13290 | if (cursor + 4 > elements->length()) { |
| 13291 | int new_capacity = JSObject::NewElementsCapacity(elements->length()); |
| 13292 | Handle<FixedArray> new_elements = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13293 | factory->NewFixedArrayWithHoles(new_capacity); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13294 | for (int i = 0; i < cursor; i++) { |
| 13295 | new_elements->set(i, elements->get(i)); |
| 13296 | } |
| 13297 | elements = new_elements; |
| 13298 | } |
| 13299 | ASSERT(cursor + 4 <= elements->length()); |
| 13300 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 13301 | Handle<Object> recv = frames[i].receiver(); |
| 13302 | Handle<JSFunction> fun = frames[i].function(); |
| 13303 | Handle<Code> code = frames[i].code(); |
| 13304 | Handle<Smi> offset(Smi::FromInt(frames[i].offset())); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13305 | elements->set(cursor++, *recv); |
| 13306 | elements->set(cursor++, *fun); |
| 13307 | elements->set(cursor++, *code); |
| 13308 | elements->set(cursor++, *offset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13309 | } |
| 13310 | } |
| 13311 | iter.Advance(); |
| 13312 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13313 | Handle<JSArray> result = factory->NewJSArrayWithElements(elements); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 13314 | result->set_length(Smi::FromInt(cursor)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13315 | return *result; |
| 13316 | } |
| 13317 | |
| 13318 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 13319 | // Returns V8 version as a string. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13320 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetV8Version) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 13321 | ASSERT_EQ(args.length(), 0); |
| 13322 | |
| 13323 | NoHandleAllocation ha; |
| 13324 | |
| 13325 | const char* version_string = v8::V8::GetVersion(); |
| 13326 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13327 | return isolate->heap()->AllocateStringFromAscii(CStrVector(version_string), |
| 13328 | NOT_TENURED); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 13329 | } |
| 13330 | |
| 13331 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13332 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Abort) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13333 | ASSERT(args.length() == 2); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13334 | OS::PrintError("abort: %s\n", |
| 13335 | reinterpret_cast<char*>(args[0]) + args.smi_at(1)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13336 | isolate->PrintStack(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13337 | OS::Abort(); |
| 13338 | UNREACHABLE(); |
| 13339 | return NULL; |
| 13340 | } |
| 13341 | |
| 13342 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13343 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFromCache) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13344 | // This is only called from codegen, so checks might be more lax. |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13345 | CONVERT_CHECKED(JSFunctionResultCache, cache, args[0]); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13346 | Object* key = args[1]; |
| 13347 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13348 | int finger_index = cache->finger_index(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13349 | Object* o = cache->get(finger_index); |
| 13350 | if (o == key) { |
| 13351 | // The fastest case: hit the same place again. |
| 13352 | return cache->get(finger_index + 1); |
| 13353 | } |
| 13354 | |
| 13355 | for (int i = finger_index - 2; |
| 13356 | i >= JSFunctionResultCache::kEntriesIndex; |
| 13357 | i -= 2) { |
| 13358 | o = cache->get(i); |
| 13359 | if (o == key) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13360 | cache->set_finger_index(i); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13361 | return cache->get(i + 1); |
| 13362 | } |
| 13363 | } |
| 13364 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13365 | int size = cache->size(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13366 | ASSERT(size <= cache->length()); |
| 13367 | |
| 13368 | for (int i = size - 2; i > finger_index; i -= 2) { |
| 13369 | o = cache->get(i); |
| 13370 | if (o == key) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13371 | cache->set_finger_index(i); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13372 | return cache->get(i + 1); |
| 13373 | } |
| 13374 | } |
| 13375 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13376 | // 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] | 13377 | HandleScope scope(isolate); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13378 | |
| 13379 | Handle<JSFunctionResultCache> cache_handle(cache); |
| 13380 | Handle<Object> key_handle(key); |
| 13381 | Handle<Object> value; |
| 13382 | { |
| 13383 | Handle<JSFunction> factory(JSFunction::cast( |
| 13384 | cache_handle->get(JSFunctionResultCache::kFactoryIndex))); |
| 13385 | // TODO(antonm): consider passing a receiver when constructing a cache. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13386 | Handle<Object> receiver(isolate->global_context()->global()); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13387 | // This handle is nor shared, nor used later, so it's safe. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 13388 | Handle<Object> argv[] = { key_handle }; |
| 13389 | bool pending_exception; |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13390 | value = Execution::Call(factory, |
| 13391 | receiver, |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 13392 | ARRAY_SIZE(argv), |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13393 | argv, |
| 13394 | &pending_exception); |
| 13395 | if (pending_exception) return Failure::Exception(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13396 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13397 | |
| 13398 | #ifdef DEBUG |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 13399 | if (FLAG_verify_heap) { |
| 13400 | cache_handle->JSFunctionResultCacheVerify(); |
| 13401 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13402 | #endif |
| 13403 | |
| 13404 | // Function invocation may have cleared the cache. Reread all the data. |
| 13405 | finger_index = cache_handle->finger_index(); |
| 13406 | size = cache_handle->size(); |
| 13407 | |
| 13408 | // If we have spare room, put new data into it, otherwise evict post finger |
| 13409 | // entry which is likely to be the least recently used. |
| 13410 | int index = -1; |
| 13411 | if (size < cache_handle->length()) { |
| 13412 | cache_handle->set_size(size + JSFunctionResultCache::kEntrySize); |
| 13413 | index = size; |
| 13414 | } else { |
| 13415 | index = finger_index + JSFunctionResultCache::kEntrySize; |
| 13416 | if (index == cache_handle->length()) { |
| 13417 | index = JSFunctionResultCache::kEntriesIndex; |
| 13418 | } |
| 13419 | } |
| 13420 | |
| 13421 | ASSERT(index % 2 == 0); |
| 13422 | ASSERT(index >= JSFunctionResultCache::kEntriesIndex); |
| 13423 | ASSERT(index < cache_handle->length()); |
| 13424 | |
| 13425 | cache_handle->set(index, *key_handle); |
| 13426 | cache_handle->set(index + 1, *value); |
| 13427 | cache_handle->set_finger_index(index); |
| 13428 | |
| 13429 | #ifdef DEBUG |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 13430 | if (FLAG_verify_heap) { |
| 13431 | cache_handle->JSFunctionResultCacheVerify(); |
| 13432 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 13433 | #endif |
| 13434 | |
| 13435 | return *value; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13436 | } |
| 13437 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 13438 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13439 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewMessageObject) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13440 | HandleScope scope(isolate); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 13441 | CONVERT_ARG_CHECKED(String, type, 0); |
| 13442 | CONVERT_ARG_CHECKED(JSArray, arguments, 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13443 | return *isolate->factory()->NewJSMessageObject( |
| 13444 | type, |
| 13445 | arguments, |
| 13446 | 0, |
| 13447 | 0, |
| 13448 | isolate->factory()->undefined_value(), |
| 13449 | isolate->factory()->undefined_value(), |
| 13450 | isolate->factory()->undefined_value()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 13451 | } |
| 13452 | |
| 13453 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13454 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetType) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 13455 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 13456 | return message->type(); |
| 13457 | } |
| 13458 | |
| 13459 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13460 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetArguments) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 13461 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 13462 | return message->arguments(); |
| 13463 | } |
| 13464 | |
| 13465 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13466 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetStartPosition) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 13467 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 13468 | return Smi::FromInt(message->start_position()); |
| 13469 | } |
| 13470 | |
| 13471 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13472 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetScript) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 13473 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 13474 | return message->script(); |
| 13475 | } |
| 13476 | |
| 13477 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13478 | #ifdef DEBUG |
| 13479 | // ListNatives is ONLY used by the fuzz-natives.js in debug mode |
| 13480 | // Exclude the code in release mode. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13481 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ListNatives) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13482 | ASSERT(args.length() == 0); |
| 13483 | HandleScope scope; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13484 | #define COUNT_ENTRY(Name, argc, ressize) + 1 |
| 13485 | int entry_count = 0 |
| 13486 | RUNTIME_FUNCTION_LIST(COUNT_ENTRY) |
| 13487 | INLINE_FUNCTION_LIST(COUNT_ENTRY) |
| 13488 | INLINE_RUNTIME_FUNCTION_LIST(COUNT_ENTRY); |
| 13489 | #undef COUNT_ENTRY |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13490 | Factory* factory = isolate->factory(); |
| 13491 | Handle<FixedArray> elements = factory->NewFixedArray(entry_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13492 | int index = 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13493 | bool inline_runtime_functions = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13494 | #define ADD_ENTRY(Name, argc, ressize) \ |
| 13495 | { \ |
| 13496 | HandleScope inner; \ |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13497 | Handle<String> name; \ |
| 13498 | /* Inline runtime functions have an underscore in front of the name. */ \ |
| 13499 | if (inline_runtime_functions) { \ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13500 | name = factory->NewStringFromAscii( \ |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13501 | Vector<const char>("_" #Name, StrLength("_" #Name))); \ |
| 13502 | } else { \ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13503 | name = factory->NewStringFromAscii( \ |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13504 | Vector<const char>(#Name, StrLength(#Name))); \ |
| 13505 | } \ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13506 | Handle<FixedArray> pair_elements = factory->NewFixedArray(2); \ |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13507 | pair_elements->set(0, *name); \ |
| 13508 | pair_elements->set(1, Smi::FromInt(argc)); \ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13509 | Handle<JSArray> pair = factory->NewJSArrayWithElements(pair_elements); \ |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13510 | elements->set(index++, *pair); \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13511 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13512 | inline_runtime_functions = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13513 | RUNTIME_FUNCTION_LIST(ADD_ENTRY) |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13514 | inline_runtime_functions = true; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13515 | INLINE_FUNCTION_LIST(ADD_ENTRY) |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 13516 | INLINE_RUNTIME_FUNCTION_LIST(ADD_ENTRY) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13517 | #undef ADD_ENTRY |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 13518 | ASSERT_EQ(index, entry_count); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13519 | Handle<JSArray> result = factory->NewJSArrayWithElements(elements); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13520 | return *result; |
| 13521 | } |
| 13522 | #endif |
| 13523 | |
| 13524 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13525 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Log) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13526 | ASSERT(args.length() == 2); |
| 13527 | CONVERT_CHECKED(String, format, args[0]); |
| 13528 | CONVERT_CHECKED(JSArray, elms, args[1]); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 13529 | String::FlatContent format_content = format->GetFlatContent(); |
| 13530 | RUNTIME_ASSERT(format_content.IsAscii()); |
| 13531 | Vector<const char> chars = format_content.ToAsciiVector(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13532 | LOGGER->LogRuntime(chars, elms); |
| 13533 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13534 | } |
| 13535 | |
| 13536 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 13537 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IS_VAR) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13538 | UNREACHABLE(); // implemented as macro in the parser |
| 13539 | return NULL; |
| 13540 | } |
| 13541 | |
| 13542 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13543 | #define ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(Name) \ |
| 13544 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Has##Name) { \ |
| 13545 | CONVERT_CHECKED(JSObject, obj, args[0]); \ |
| 13546 | return isolate->heap()->ToBoolean(obj->Has##Name()); \ |
| 13547 | } |
| 13548 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 13549 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(FastSmiOnlyElements) |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 13550 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(FastElements) |
| 13551 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(FastDoubleElements) |
| 13552 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(DictionaryElements) |
| 13553 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalPixelElements) |
| 13554 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalArrayElements) |
| 13555 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalByteElements) |
| 13556 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalUnsignedByteElements) |
| 13557 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalShortElements) |
| 13558 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalUnsignedShortElements) |
| 13559 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalIntElements) |
| 13560 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalUnsignedIntElements) |
| 13561 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalFloatElements) |
| 13562 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalDoubleElements) |
| 13563 | |
| 13564 | #undef ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION |
| 13565 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 13566 | |
| 13567 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HaveSameMap) { |
| 13568 | ASSERT(args.length() == 2); |
| 13569 | CONVERT_CHECKED(JSObject, obj1, args[0]); |
| 13570 | CONVERT_CHECKED(JSObject, obj2, args[1]); |
| 13571 | return isolate->heap()->ToBoolean(obj1->map() == obj2->map()); |
| 13572 | } |
| 13573 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13574 | // ---------------------------------------------------------------------------- |
| 13575 | // Implementation of Runtime |
| 13576 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13577 | #define F(name, number_of_args, result_size) \ |
| 13578 | { Runtime::k##name, Runtime::RUNTIME, #name, \ |
| 13579 | FUNCTION_ADDR(Runtime_##name), number_of_args, result_size }, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13580 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13581 | |
| 13582 | #define I(name, number_of_args, result_size) \ |
| 13583 | { Runtime::kInline##name, Runtime::INLINE, \ |
| 13584 | "_" #name, NULL, number_of_args, result_size }, |
| 13585 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13586 | static const Runtime::Function kIntrinsicFunctions[] = { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13587 | RUNTIME_FUNCTION_LIST(F) |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13588 | INLINE_FUNCTION_LIST(I) |
| 13589 | INLINE_RUNTIME_FUNCTION_LIST(I) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13590 | }; |
| 13591 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13592 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13593 | MaybeObject* Runtime::InitializeIntrinsicFunctionNames(Heap* heap, |
| 13594 | Object* dictionary) { |
| 13595 | ASSERT(Isolate::Current()->heap() == heap); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13596 | ASSERT(dictionary != NULL); |
| 13597 | ASSERT(StringDictionary::cast(dictionary)->NumberOfElements() == 0); |
| 13598 | for (int i = 0; i < kNumFunctions; ++i) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 13599 | Object* name_symbol; |
| 13600 | { MaybeObject* maybe_name_symbol = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13601 | heap->LookupAsciiSymbol(kIntrinsicFunctions[i].name); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 13602 | if (!maybe_name_symbol->ToObject(&name_symbol)) return maybe_name_symbol; |
| 13603 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13604 | StringDictionary* string_dictionary = StringDictionary::cast(dictionary); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 13605 | { MaybeObject* maybe_dictionary = string_dictionary->Add( |
| 13606 | String::cast(name_symbol), |
| 13607 | Smi::FromInt(i), |
| 13608 | PropertyDetails(NONE, NORMAL)); |
| 13609 | if (!maybe_dictionary->ToObject(&dictionary)) { |
| 13610 | // Non-recoverable failure. Calling code must restart heap |
| 13611 | // initialization. |
| 13612 | return maybe_dictionary; |
| 13613 | } |
| 13614 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13615 | } |
| 13616 | return dictionary; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13617 | } |
| 13618 | |
| 13619 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13620 | const Runtime::Function* Runtime::FunctionForSymbol(Handle<String> name) { |
| 13621 | Heap* heap = name->GetHeap(); |
| 13622 | int entry = heap->intrinsic_function_names()->FindEntry(*name); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13623 | if (entry != kNotFound) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13624 | Object* smi_index = heap->intrinsic_function_names()->ValueAt(entry); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13625 | int function_index = Smi::cast(smi_index)->value(); |
| 13626 | return &(kIntrinsicFunctions[function_index]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13627 | } |
| 13628 | return NULL; |
| 13629 | } |
| 13630 | |
| 13631 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13632 | const Runtime::Function* Runtime::FunctionForId(Runtime::FunctionId id) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13633 | return &(kIntrinsicFunctions[static_cast<int>(id)]); |
| 13634 | } |
| 13635 | |
| 13636 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13637 | void Runtime::PerformGC(Object* result) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13638 | Isolate* isolate = Isolate::Current(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13639 | Failure* failure = Failure::cast(result); |
| 13640 | if (failure->IsRetryAfterGC()) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 13641 | if (isolate->heap()->new_space()->AddFreshPage()) { |
| 13642 | return; |
| 13643 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13644 | // Try to do a garbage collection; ignore it if it fails. The C |
| 13645 | // entry stub will throw an out-of-memory exception in that case. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13646 | isolate->heap()->CollectGarbage(failure->allocation_space()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13647 | } else { |
| 13648 | // Handle last resort GC and make sure to allow future allocations |
| 13649 | // to grow the heap without causing GCs (if possible). |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13650 | isolate->counters()->gc_last_resort_from_js()->Increment(); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 13651 | isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13652 | } |
| 13653 | } |
| 13654 | |
| 13655 | |
| 13656 | } } // namespace v8::internal |